]> gcc.gnu.org Git - gcc.git/blob - gcc/fortran/resolve.cc
Fortran: Fix bugs and missing features in finalization [PR37336]
[gcc.git] / gcc / fortran / resolve.cc
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2023 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 %qs of statement function %qs 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
808 && (el->sym->result->attr.allocatable
809 != ns->entries->sym->result->attr.allocatable))
810 {
811 gfc_error ("Function %s at %L has entry %s with mismatched "
812 "characteristics", ns->entries->sym->name,
813 &ns->entries->sym->declared_at, el->sym->name);
814 goto cleanup;
815 }
816 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
817 && (((ts->u.cl->length && !fts->u.cl->length)
818 ||(!ts->u.cl->length && fts->u.cl->length))
819 || (ts->u.cl->length
820 && ts->u.cl->length->expr_type
821 != fts->u.cl->length->expr_type)
822 || (ts->u.cl->length
823 && ts->u.cl->length->expr_type == EXPR_CONSTANT
824 && mpz_cmp (ts->u.cl->length->value.integer,
825 fts->u.cl->length->value.integer) != 0)))
826 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
827 "entries returning variables of different "
828 "string lengths", ns->entries->sym->name,
829 &ns->entries->sym->declared_at);
830 }
831
832 if (el == NULL)
833 {
834 sym = ns->entries->sym->result;
835 /* All result types the same. */
836 proc->ts = *fts;
837 if (sym->attr.dimension)
838 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
839 if (sym->attr.pointer)
840 gfc_add_pointer (&proc->attr, NULL);
841 }
842 else
843 {
844 /* Otherwise the result will be passed through a union by
845 reference. */
846 proc->attr.mixed_entry_master = 1;
847 for (el = ns->entries; el; el = el->next)
848 {
849 sym = el->sym->result;
850 if (sym->attr.dimension)
851 {
852 if (el == ns->entries)
853 gfc_error ("FUNCTION result %s cannot be an array in "
854 "FUNCTION %s at %L", sym->name,
855 ns->entries->sym->name, &sym->declared_at);
856 else
857 gfc_error ("ENTRY result %s cannot be an array in "
858 "FUNCTION %s at %L", sym->name,
859 ns->entries->sym->name, &sym->declared_at);
860 }
861 else if (sym->attr.pointer)
862 {
863 if (el == ns->entries)
864 gfc_error ("FUNCTION result %s cannot be a POINTER in "
865 "FUNCTION %s at %L", sym->name,
866 ns->entries->sym->name, &sym->declared_at);
867 else
868 gfc_error ("ENTRY result %s cannot be a POINTER in "
869 "FUNCTION %s at %L", sym->name,
870 ns->entries->sym->name, &sym->declared_at);
871 }
872 else
873 {
874 ts = &sym->ts;
875 if (ts->type == BT_UNKNOWN)
876 ts = gfc_get_default_type (sym->name, NULL);
877 switch (ts->type)
878 {
879 case BT_INTEGER:
880 if (ts->kind == gfc_default_integer_kind)
881 sym = NULL;
882 break;
883 case BT_REAL:
884 if (ts->kind == gfc_default_real_kind
885 || ts->kind == gfc_default_double_kind)
886 sym = NULL;
887 break;
888 case BT_COMPLEX:
889 if (ts->kind == gfc_default_complex_kind)
890 sym = NULL;
891 break;
892 case BT_LOGICAL:
893 if (ts->kind == gfc_default_logical_kind)
894 sym = NULL;
895 break;
896 case BT_UNKNOWN:
897 /* We will issue error elsewhere. */
898 sym = NULL;
899 break;
900 default:
901 break;
902 }
903 if (sym)
904 {
905 if (el == ns->entries)
906 gfc_error ("FUNCTION result %s cannot be of type %s "
907 "in FUNCTION %s at %L", sym->name,
908 gfc_typename (ts), ns->entries->sym->name,
909 &sym->declared_at);
910 else
911 gfc_error ("ENTRY result %s cannot be of type %s "
912 "in FUNCTION %s at %L", sym->name,
913 gfc_typename (ts), ns->entries->sym->name,
914 &sym->declared_at);
915 }
916 }
917 }
918 }
919 }
920
921 cleanup:
922 proc->attr.access = ACCESS_PRIVATE;
923 proc->attr.entry_master = 1;
924
925 /* Merge all the entry point arguments. */
926 for (el = ns->entries; el; el = el->next)
927 merge_argument_lists (proc, el->sym->formal);
928
929 /* Check the master formal arguments for any that are not
930 present in all entry points. */
931 for (el = ns->entries; el; el = el->next)
932 check_argument_lists (proc, el->sym->formal);
933
934 /* Use the master function for the function body. */
935 ns->proc_name = proc;
936
937 /* Finalize the new symbols. */
938 gfc_commit_symbols ();
939
940 /* Restore the original namespace. */
941 gfc_current_ns = old_ns;
942 }
943
944
945 /* Resolve common variables. */
946 static void
947 resolve_common_vars (gfc_common_head *common_block, bool named_common)
948 {
949 gfc_symbol *csym = common_block->head;
950 gfc_gsymbol *gsym;
951
952 for (; csym; csym = csym->common_next)
953 {
954 gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
955 if (gsym && (gsym->type == GSYM_MODULE || gsym->type == GSYM_PROGRAM))
956 {
957 if (csym->common_block)
958 gfc_error_now ("Global entity %qs at %L cannot appear in a "
959 "COMMON block at %L", gsym->name,
960 &gsym->where, &csym->common_block->where);
961 else
962 gfc_error_now ("Global entity %qs at %L cannot appear in a "
963 "COMMON block", gsym->name, &gsym->where);
964 }
965
966 /* gfc_add_in_common may have been called before, but the reported errors
967 have been ignored to continue parsing.
968 We do the checks again here. */
969 if (!csym->attr.use_assoc)
970 {
971 gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
972 gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
973 &common_block->where);
974 }
975
976 if (csym->value || csym->attr.data)
977 {
978 if (!csym->ns->is_block_data)
979 gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
980 "but only in BLOCK DATA initialization is "
981 "allowed", csym->name, &csym->declared_at);
982 else if (!named_common)
983 gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
984 "in a blank COMMON but initialization is only "
985 "allowed in named common blocks", csym->name,
986 &csym->declared_at);
987 }
988
989 if (UNLIMITED_POLY (csym))
990 gfc_error_now ("%qs at %L cannot appear in COMMON "
991 "[F2008:C5100]", csym->name, &csym->declared_at);
992
993 if (csym->ts.type != BT_DERIVED)
994 continue;
995
996 if (!(csym->ts.u.derived->attr.sequence
997 || csym->ts.u.derived->attr.is_bind_c))
998 gfc_error_now ("Derived type variable %qs in COMMON at %L "
999 "has neither the SEQUENCE nor the BIND(C) "
1000 "attribute", csym->name, &csym->declared_at);
1001 if (csym->ts.u.derived->attr.alloc_comp)
1002 gfc_error_now ("Derived type variable %qs in COMMON at %L "
1003 "has an ultimate component that is "
1004 "allocatable", csym->name, &csym->declared_at);
1005 if (gfc_has_default_initializer (csym->ts.u.derived))
1006 gfc_error_now ("Derived type variable %qs in COMMON at %L "
1007 "may not have default initializer", csym->name,
1008 &csym->declared_at);
1009
1010 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
1011 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
1012 }
1013 }
1014
1015 /* Resolve common blocks. */
1016 static void
1017 resolve_common_blocks (gfc_symtree *common_root)
1018 {
1019 gfc_symbol *sym;
1020 gfc_gsymbol * gsym;
1021
1022 if (common_root == NULL)
1023 return;
1024
1025 if (common_root->left)
1026 resolve_common_blocks (common_root->left);
1027 if (common_root->right)
1028 resolve_common_blocks (common_root->right);
1029
1030 resolve_common_vars (common_root->n.common, true);
1031
1032 /* The common name is a global name - in Fortran 2003 also if it has a
1033 C binding name, since Fortran 2008 only the C binding name is a global
1034 identifier. */
1035 if (!common_root->n.common->binding_label
1036 || gfc_notification_std (GFC_STD_F2008))
1037 {
1038 gsym = gfc_find_gsymbol (gfc_gsym_root,
1039 common_root->n.common->name);
1040
1041 if (gsym && gfc_notification_std (GFC_STD_F2008)
1042 && gsym->type == GSYM_COMMON
1043 && ((common_root->n.common->binding_label
1044 && (!gsym->binding_label
1045 || strcmp (common_root->n.common->binding_label,
1046 gsym->binding_label) != 0))
1047 || (!common_root->n.common->binding_label
1048 && gsym->binding_label)))
1049 {
1050 gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
1051 "identifier and must thus have the same binding name "
1052 "as the same-named COMMON block at %L: %s vs %s",
1053 common_root->n.common->name, &common_root->n.common->where,
1054 &gsym->where,
1055 common_root->n.common->binding_label
1056 ? common_root->n.common->binding_label : "(blank)",
1057 gsym->binding_label ? gsym->binding_label : "(blank)");
1058 return;
1059 }
1060
1061 if (gsym && gsym->type != GSYM_COMMON
1062 && !common_root->n.common->binding_label)
1063 {
1064 gfc_error ("COMMON block %qs at %L uses the same global identifier "
1065 "as entity at %L",
1066 common_root->n.common->name, &common_root->n.common->where,
1067 &gsym->where);
1068 return;
1069 }
1070 if (gsym && gsym->type != GSYM_COMMON)
1071 {
1072 gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
1073 "%L sharing the identifier with global non-COMMON-block "
1074 "entity at %L", common_root->n.common->name,
1075 &common_root->n.common->where, &gsym->where);
1076 return;
1077 }
1078 if (!gsym)
1079 {
1080 gsym = gfc_get_gsymbol (common_root->n.common->name, false);
1081 gsym->type = GSYM_COMMON;
1082 gsym->where = common_root->n.common->where;
1083 gsym->defined = 1;
1084 }
1085 gsym->used = 1;
1086 }
1087
1088 if (common_root->n.common->binding_label)
1089 {
1090 gsym = gfc_find_gsymbol (gfc_gsym_root,
1091 common_root->n.common->binding_label);
1092 if (gsym && gsym->type != GSYM_COMMON)
1093 {
1094 gfc_error ("COMMON block at %L with binding label %qs uses the same "
1095 "global identifier as entity at %L",
1096 &common_root->n.common->where,
1097 common_root->n.common->binding_label, &gsym->where);
1098 return;
1099 }
1100 if (!gsym)
1101 {
1102 gsym = gfc_get_gsymbol (common_root->n.common->binding_label, true);
1103 gsym->type = GSYM_COMMON;
1104 gsym->where = common_root->n.common->where;
1105 gsym->defined = 1;
1106 }
1107 gsym->used = 1;
1108 }
1109
1110 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1111 if (sym == NULL)
1112 return;
1113
1114 if (sym->attr.flavor == FL_PARAMETER)
1115 gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
1116 sym->name, &common_root->n.common->where, &sym->declared_at);
1117
1118 if (sym->attr.external)
1119 gfc_error ("COMMON block %qs at %L cannot have the EXTERNAL attribute",
1120 sym->name, &common_root->n.common->where);
1121
1122 if (sym->attr.intrinsic)
1123 gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
1124 sym->name, &common_root->n.common->where);
1125 else if (sym->attr.result
1126 || gfc_is_function_return_value (sym, gfc_current_ns))
1127 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1128 "that is also a function result", sym->name,
1129 &common_root->n.common->where);
1130 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1131 && sym->attr.proc != PROC_ST_FUNCTION)
1132 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1133 "that is also a global procedure", sym->name,
1134 &common_root->n.common->where);
1135 }
1136
1137
1138 /* Resolve contained function types. Because contained functions can call one
1139 another, they have to be worked out before any of the contained procedures
1140 can be resolved.
1141
1142 The good news is that if a function doesn't already have a type, the only
1143 way it can get one is through an IMPLICIT type or a RESULT variable, because
1144 by definition contained functions are contained namespace they're contained
1145 in, not in a sibling or parent namespace. */
1146
1147 static void
1148 resolve_contained_functions (gfc_namespace *ns)
1149 {
1150 gfc_namespace *child;
1151 gfc_entry_list *el;
1152
1153 resolve_formal_arglists (ns);
1154
1155 for (child = ns->contained; child; child = child->sibling)
1156 {
1157 /* Resolve alternate entry points first. */
1158 resolve_entries (child);
1159
1160 /* Then check function return types. */
1161 resolve_contained_fntype (child->proc_name, child);
1162 for (el = child->entries; el; el = el->next)
1163 resolve_contained_fntype (el->sym, child);
1164 }
1165 }
1166
1167
1168
1169 /* A Parameterized Derived Type constructor must contain values for
1170 the PDT KIND parameters or they must have a default initializer.
1171 Go through the constructor picking out the KIND expressions,
1172 storing them in 'param_list' and then call gfc_get_pdt_instance
1173 to obtain the PDT instance. */
1174
1175 static gfc_actual_arglist *param_list, *param_tail, *param;
1176
1177 static bool
1178 get_pdt_spec_expr (gfc_component *c, gfc_expr *expr)
1179 {
1180 param = gfc_get_actual_arglist ();
1181 if (!param_list)
1182 param_list = param_tail = param;
1183 else
1184 {
1185 param_tail->next = param;
1186 param_tail = param_tail->next;
1187 }
1188
1189 param_tail->name = c->name;
1190 if (expr)
1191 param_tail->expr = gfc_copy_expr (expr);
1192 else if (c->initializer)
1193 param_tail->expr = gfc_copy_expr (c->initializer);
1194 else
1195 {
1196 param_tail->spec_type = SPEC_ASSUMED;
1197 if (c->attr.pdt_kind)
1198 {
1199 gfc_error ("The KIND parameter %qs in the PDT constructor "
1200 "at %C has no value", param->name);
1201 return false;
1202 }
1203 }
1204
1205 return true;
1206 }
1207
1208 static bool
1209 get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr,
1210 gfc_symbol *derived)
1211 {
1212 gfc_constructor *cons = NULL;
1213 gfc_component *comp;
1214 bool t = true;
1215
1216 if (expr && expr->expr_type == EXPR_STRUCTURE)
1217 cons = gfc_constructor_first (expr->value.constructor);
1218 else if (constr)
1219 cons = *constr;
1220 gcc_assert (cons);
1221
1222 comp = derived->components;
1223
1224 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1225 {
1226 if (cons->expr
1227 && cons->expr->expr_type == EXPR_STRUCTURE
1228 && comp->ts.type == BT_DERIVED)
1229 {
1230 t = get_pdt_constructor (cons->expr, NULL, comp->ts.u.derived);
1231 if (!t)
1232 return t;
1233 }
1234 else if (comp->ts.type == BT_DERIVED)
1235 {
1236 t = get_pdt_constructor (NULL, &cons, comp->ts.u.derived);
1237 if (!t)
1238 return t;
1239 }
1240 else if ((comp->attr.pdt_kind || comp->attr.pdt_len)
1241 && derived->attr.pdt_template)
1242 {
1243 t = get_pdt_spec_expr (comp, cons->expr);
1244 if (!t)
1245 return t;
1246 }
1247 }
1248 return t;
1249 }
1250
1251
1252 static bool resolve_fl_derived0 (gfc_symbol *sym);
1253 static bool resolve_fl_struct (gfc_symbol *sym);
1254
1255
1256 /* Resolve all of the elements of a structure constructor and make sure that
1257 the types are correct. The 'init' flag indicates that the given
1258 constructor is an initializer. */
1259
1260 static bool
1261 resolve_structure_cons (gfc_expr *expr, int init)
1262 {
1263 gfc_constructor *cons;
1264 gfc_component *comp;
1265 bool t;
1266 symbol_attribute a;
1267
1268 t = true;
1269
1270 if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
1271 {
1272 if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
1273 resolve_fl_derived0 (expr->ts.u.derived);
1274 else
1275 resolve_fl_struct (expr->ts.u.derived);
1276
1277 /* If this is a Parameterized Derived Type template, find the
1278 instance corresponding to the PDT kind parameters. */
1279 if (expr->ts.u.derived->attr.pdt_template)
1280 {
1281 param_list = NULL;
1282 t = get_pdt_constructor (expr, NULL, expr->ts.u.derived);
1283 if (!t)
1284 return t;
1285 gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL);
1286
1287 expr->param_list = gfc_copy_actual_arglist (param_list);
1288
1289 if (param_list)
1290 gfc_free_actual_arglist (param_list);
1291
1292 if (!expr->ts.u.derived->attr.pdt_type)
1293 return false;
1294 }
1295 }
1296
1297 /* A constructor may have references if it is the result of substituting a
1298 parameter variable. In this case we just pull out the component we
1299 want. */
1300 if (expr->ref)
1301 comp = expr->ref->u.c.sym->components;
1302 else if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS
1303 || expr->ts.type == BT_UNION)
1304 && expr->ts.u.derived)
1305 comp = expr->ts.u.derived->components;
1306 else
1307 return false;
1308
1309 cons = gfc_constructor_first (expr->value.constructor);
1310
1311 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1312 {
1313 int rank;
1314
1315 if (!cons->expr)
1316 continue;
1317
1318 /* Unions use an EXPR_NULL contrived expression to tell the translation
1319 phase to generate an initializer of the appropriate length.
1320 Ignore it here. */
1321 if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
1322 continue;
1323
1324 if (!gfc_resolve_expr (cons->expr))
1325 {
1326 t = false;
1327 continue;
1328 }
1329
1330 rank = comp->as ? comp->as->rank : 0;
1331 if (comp->ts.type == BT_CLASS
1332 && !comp->ts.u.derived->attr.unlimited_polymorphic
1333 && CLASS_DATA (comp)->as)
1334 rank = CLASS_DATA (comp)->as->rank;
1335
1336 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1337 && (comp->attr.allocatable || cons->expr->rank))
1338 {
1339 gfc_error ("The rank of the element in the structure "
1340 "constructor at %L does not match that of the "
1341 "component (%d/%d)", &cons->expr->where,
1342 cons->expr->rank, rank);
1343 t = false;
1344 }
1345
1346 /* If we don't have the right type, try to convert it. */
1347
1348 if (!comp->attr.proc_pointer &&
1349 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1350 {
1351 if (strcmp (comp->name, "_extends") == 0)
1352 {
1353 /* Can afford to be brutal with the _extends initializer.
1354 The derived type can get lost because it is PRIVATE
1355 but it is not usage constrained by the standard. */
1356 cons->expr->ts = comp->ts;
1357 }
1358 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1359 {
1360 gfc_error ("The element in the structure constructor at %L, "
1361 "for pointer component %qs, is %s but should be %s",
1362 &cons->expr->where, comp->name,
1363 gfc_basic_typename (cons->expr->ts.type),
1364 gfc_basic_typename (comp->ts.type));
1365 t = false;
1366 }
1367 else
1368 {
1369 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1370 if (t)
1371 t = t2;
1372 }
1373 }
1374
1375 /* For strings, the length of the constructor should be the same as
1376 the one of the structure, ensure this if the lengths are known at
1377 compile time and when we are dealing with PARAMETER or structure
1378 constructors. */
1379 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1380 && comp->ts.u.cl->length
1381 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1382 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1383 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1384 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1385 comp->ts.u.cl->length->value.integer) != 0)
1386 {
1387 if (comp->attr.pointer)
1388 {
1389 HOST_WIDE_INT la, lb;
1390 la = gfc_mpz_get_hwi (comp->ts.u.cl->length->value.integer);
1391 lb = gfc_mpz_get_hwi (cons->expr->ts.u.cl->length->value.integer);
1392 gfc_error ("Unequal character lengths (%wd/%wd) for pointer "
1393 "component %qs in constructor at %L",
1394 la, lb, comp->name, &cons->expr->where);
1395 t = false;
1396 }
1397
1398 if (cons->expr->expr_type == EXPR_VARIABLE
1399 && cons->expr->rank != 0
1400 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1401 {
1402 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1403 to make use of the gfc_resolve_character_array_constructor
1404 machinery. The expression is later simplified away to
1405 an array of string literals. */
1406 gfc_expr *para = cons->expr;
1407 cons->expr = gfc_get_expr ();
1408 cons->expr->ts = para->ts;
1409 cons->expr->where = para->where;
1410 cons->expr->expr_type = EXPR_ARRAY;
1411 cons->expr->rank = para->rank;
1412 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1413 gfc_constructor_append_expr (&cons->expr->value.constructor,
1414 para, &cons->expr->where);
1415 }
1416
1417 if (cons->expr->expr_type == EXPR_ARRAY)
1418 {
1419 /* Rely on the cleanup of the namespace to deal correctly with
1420 the old charlen. (There was a block here that attempted to
1421 remove the charlen but broke the chain in so doing.) */
1422 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1423 cons->expr->ts.u.cl->length_from_typespec = true;
1424 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1425 gfc_resolve_character_array_constructor (cons->expr);
1426 }
1427 }
1428
1429 if (cons->expr->expr_type == EXPR_NULL
1430 && !(comp->attr.pointer || comp->attr.allocatable
1431 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1432 || (comp->ts.type == BT_CLASS
1433 && (CLASS_DATA (comp)->attr.class_pointer
1434 || CLASS_DATA (comp)->attr.allocatable))))
1435 {
1436 t = false;
1437 gfc_error ("The NULL in the structure constructor at %L is "
1438 "being applied to component %qs, which is neither "
1439 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1440 comp->name);
1441 }
1442
1443 if (comp->attr.proc_pointer && comp->ts.interface)
1444 {
1445 /* Check procedure pointer interface. */
1446 gfc_symbol *s2 = NULL;
1447 gfc_component *c2;
1448 const char *name;
1449 char err[200];
1450
1451 c2 = gfc_get_proc_ptr_comp (cons->expr);
1452 if (c2)
1453 {
1454 s2 = c2->ts.interface;
1455 name = c2->name;
1456 }
1457 else if (cons->expr->expr_type == EXPR_FUNCTION)
1458 {
1459 s2 = cons->expr->symtree->n.sym->result;
1460 name = cons->expr->symtree->n.sym->result->name;
1461 }
1462 else if (cons->expr->expr_type != EXPR_NULL)
1463 {
1464 s2 = cons->expr->symtree->n.sym;
1465 name = cons->expr->symtree->n.sym->name;
1466 }
1467
1468 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1469 err, sizeof (err), NULL, NULL))
1470 {
1471 gfc_error_opt (0, "Interface mismatch for procedure-pointer "
1472 "component %qs in structure constructor at %L:"
1473 " %s", comp->name, &cons->expr->where, err);
1474 return false;
1475 }
1476 }
1477
1478 /* Validate shape, except for dynamic or PDT arrays. */
1479 if (cons->expr->expr_type == EXPR_ARRAY && rank == cons->expr->rank
1480 && comp->as && !comp->attr.allocatable && !comp->attr.pointer
1481 && !comp->attr.pdt_array)
1482 {
1483 mpz_t len;
1484 mpz_init (len);
1485 for (int n = 0; n < rank; n++)
1486 {
1487 if (comp->as->upper[n]->expr_type != EXPR_CONSTANT
1488 || comp->as->lower[n]->expr_type != EXPR_CONSTANT)
1489 {
1490 gfc_error ("Bad array spec of component %qs referenced in "
1491 "structure constructor at %L",
1492 comp->name, &cons->expr->where);
1493 t = false;
1494 break;
1495 };
1496 if (cons->expr->shape == NULL)
1497 continue;
1498 mpz_set_ui (len, 1);
1499 mpz_add (len, len, comp->as->upper[n]->value.integer);
1500 mpz_sub (len, len, comp->as->lower[n]->value.integer);
1501 if (mpz_cmp (cons->expr->shape[n], len) != 0)
1502 {
1503 gfc_error ("The shape of component %qs in the structure "
1504 "constructor at %L differs from the shape of the "
1505 "declared component for dimension %d (%ld/%ld)",
1506 comp->name, &cons->expr->where, n+1,
1507 mpz_get_si (cons->expr->shape[n]),
1508 mpz_get_si (len));
1509 t = false;
1510 }
1511 }
1512 mpz_clear (len);
1513 }
1514
1515 if (!comp->attr.pointer || comp->attr.proc_pointer
1516 || cons->expr->expr_type == EXPR_NULL)
1517 continue;
1518
1519 a = gfc_expr_attr (cons->expr);
1520
1521 if (!a.pointer && !a.target)
1522 {
1523 t = false;
1524 gfc_error ("The element in the structure constructor at %L, "
1525 "for pointer component %qs should be a POINTER or "
1526 "a TARGET", &cons->expr->where, comp->name);
1527 }
1528
1529 if (init)
1530 {
1531 /* F08:C461. Additional checks for pointer initialization. */
1532 if (a.allocatable)
1533 {
1534 t = false;
1535 gfc_error ("Pointer initialization target at %L "
1536 "must not be ALLOCATABLE", &cons->expr->where);
1537 }
1538 if (!a.save)
1539 {
1540 t = false;
1541 gfc_error ("Pointer initialization target at %L "
1542 "must have the SAVE attribute", &cons->expr->where);
1543 }
1544 }
1545
1546 /* F2003, C1272 (3). */
1547 bool impure = cons->expr->expr_type == EXPR_VARIABLE
1548 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1549 || gfc_is_coindexed (cons->expr));
1550 if (impure && gfc_pure (NULL))
1551 {
1552 t = false;
1553 gfc_error ("Invalid expression in the structure constructor for "
1554 "pointer component %qs at %L in PURE procedure",
1555 comp->name, &cons->expr->where);
1556 }
1557
1558 if (impure)
1559 gfc_unset_implicit_pure (NULL);
1560 }
1561
1562 return t;
1563 }
1564
1565
1566 /****************** Expression name resolution ******************/
1567
1568 /* Returns 0 if a symbol was not declared with a type or
1569 attribute declaration statement, nonzero otherwise. */
1570
1571 static int
1572 was_declared (gfc_symbol *sym)
1573 {
1574 symbol_attribute a;
1575
1576 a = sym->attr;
1577
1578 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1579 return 1;
1580
1581 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1582 || a.optional || a.pointer || a.save || a.target || a.volatile_
1583 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1584 || a.asynchronous || a.codimension)
1585 return 1;
1586
1587 return 0;
1588 }
1589
1590
1591 /* Determine if a symbol is generic or not. */
1592
1593 static int
1594 generic_sym (gfc_symbol *sym)
1595 {
1596 gfc_symbol *s;
1597
1598 if (sym->attr.generic ||
1599 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1600 return 1;
1601
1602 if (was_declared (sym) || sym->ns->parent == NULL)
1603 return 0;
1604
1605 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1606
1607 if (s != NULL)
1608 {
1609 if (s == sym)
1610 return 0;
1611 else
1612 return generic_sym (s);
1613 }
1614
1615 return 0;
1616 }
1617
1618
1619 /* Determine if a symbol is specific or not. */
1620
1621 static int
1622 specific_sym (gfc_symbol *sym)
1623 {
1624 gfc_symbol *s;
1625
1626 if (sym->attr.if_source == IFSRC_IFBODY
1627 || sym->attr.proc == PROC_MODULE
1628 || sym->attr.proc == PROC_INTERNAL
1629 || sym->attr.proc == PROC_ST_FUNCTION
1630 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1631 || sym->attr.external)
1632 return 1;
1633
1634 if (was_declared (sym) || sym->ns->parent == NULL)
1635 return 0;
1636
1637 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1638
1639 return (s == NULL) ? 0 : specific_sym (s);
1640 }
1641
1642
1643 /* Figure out if the procedure is specific, generic or unknown. */
1644
1645 enum proc_type
1646 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
1647
1648 static proc_type
1649 procedure_kind (gfc_symbol *sym)
1650 {
1651 if (generic_sym (sym))
1652 return PTYPE_GENERIC;
1653
1654 if (specific_sym (sym))
1655 return PTYPE_SPECIFIC;
1656
1657 return PTYPE_UNKNOWN;
1658 }
1659
1660 /* Check references to assumed size arrays. The flag need_full_assumed_size
1661 is nonzero when matching actual arguments. */
1662
1663 static int need_full_assumed_size = 0;
1664
1665 static bool
1666 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1667 {
1668 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1669 return false;
1670
1671 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1672 What should it be? */
1673 if (e->ref
1674 && e->ref->u.ar.as
1675 && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1676 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1677 && (e->ref->u.ar.type == AR_FULL))
1678 {
1679 gfc_error ("The upper bound in the last dimension must "
1680 "appear in the reference to the assumed size "
1681 "array %qs at %L", sym->name, &e->where);
1682 return true;
1683 }
1684 return false;
1685 }
1686
1687
1688 /* Look for bad assumed size array references in argument expressions
1689 of elemental and array valued intrinsic procedures. Since this is
1690 called from procedure resolution functions, it only recurses at
1691 operators. */
1692
1693 static bool
1694 resolve_assumed_size_actual (gfc_expr *e)
1695 {
1696 if (e == NULL)
1697 return false;
1698
1699 switch (e->expr_type)
1700 {
1701 case EXPR_VARIABLE:
1702 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1703 return true;
1704 break;
1705
1706 case EXPR_OP:
1707 if (resolve_assumed_size_actual (e->value.op.op1)
1708 || resolve_assumed_size_actual (e->value.op.op2))
1709 return true;
1710 break;
1711
1712 default:
1713 break;
1714 }
1715 return false;
1716 }
1717
1718
1719 /* Check a generic procedure, passed as an actual argument, to see if
1720 there is a matching specific name. If none, it is an error, and if
1721 more than one, the reference is ambiguous. */
1722 static int
1723 count_specific_procs (gfc_expr *e)
1724 {
1725 int n;
1726 gfc_interface *p;
1727 gfc_symbol *sym;
1728
1729 n = 0;
1730 sym = e->symtree->n.sym;
1731
1732 for (p = sym->generic; p; p = p->next)
1733 if (strcmp (sym->name, p->sym->name) == 0)
1734 {
1735 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1736 sym->name);
1737 n++;
1738 }
1739
1740 if (n > 1)
1741 gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
1742 &e->where);
1743
1744 if (n == 0)
1745 gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1746 "argument at %L", sym->name, &e->where);
1747
1748 return n;
1749 }
1750
1751
1752 /* See if a call to sym could possibly be a not allowed RECURSION because of
1753 a missing RECURSIVE declaration. This means that either sym is the current
1754 context itself, or sym is the parent of a contained procedure calling its
1755 non-RECURSIVE containing procedure.
1756 This also works if sym is an ENTRY. */
1757
1758 static bool
1759 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1760 {
1761 gfc_symbol* proc_sym;
1762 gfc_symbol* context_proc;
1763 gfc_namespace* real_context;
1764
1765 if (sym->attr.flavor == FL_PROGRAM
1766 || gfc_fl_struct (sym->attr.flavor))
1767 return false;
1768
1769 /* If we've got an ENTRY, find real procedure. */
1770 if (sym->attr.entry && sym->ns->entries)
1771 proc_sym = sym->ns->entries->sym;
1772 else
1773 proc_sym = sym;
1774
1775 /* If sym is RECURSIVE, all is well of course. */
1776 if (proc_sym->attr.recursive || flag_recursive)
1777 return false;
1778
1779 /* Find the context procedure's "real" symbol if it has entries.
1780 We look for a procedure symbol, so recurse on the parents if we don't
1781 find one (like in case of a BLOCK construct). */
1782 for (real_context = context; ; real_context = real_context->parent)
1783 {
1784 /* We should find something, eventually! */
1785 gcc_assert (real_context);
1786
1787 context_proc = (real_context->entries ? real_context->entries->sym
1788 : real_context->proc_name);
1789
1790 /* In some special cases, there may not be a proc_name, like for this
1791 invalid code:
1792 real(bad_kind()) function foo () ...
1793 when checking the call to bad_kind ().
1794 In these cases, we simply return here and assume that the
1795 call is ok. */
1796 if (!context_proc)
1797 return false;
1798
1799 if (context_proc->attr.flavor != FL_LABEL)
1800 break;
1801 }
1802
1803 /* A call from sym's body to itself is recursion, of course. */
1804 if (context_proc == proc_sym)
1805 return true;
1806
1807 /* The same is true if context is a contained procedure and sym the
1808 containing one. */
1809 if (context_proc->attr.contained)
1810 {
1811 gfc_symbol* parent_proc;
1812
1813 gcc_assert (context->parent);
1814 parent_proc = (context->parent->entries ? context->parent->entries->sym
1815 : context->parent->proc_name);
1816
1817 if (parent_proc == proc_sym)
1818 return true;
1819 }
1820
1821 return false;
1822 }
1823
1824
1825 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1826 its typespec and formal argument list. */
1827
1828 bool
1829 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1830 {
1831 gfc_intrinsic_sym* isym = NULL;
1832 const char* symstd;
1833
1834 if (sym->resolve_symbol_called >= 2)
1835 return true;
1836
1837 sym->resolve_symbol_called = 2;
1838
1839 /* Already resolved. */
1840 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1841 return true;
1842
1843 /* We already know this one is an intrinsic, so we don't call
1844 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1845 gfc_find_subroutine directly to check whether it is a function or
1846 subroutine. */
1847
1848 if (sym->intmod_sym_id && sym->attr.subroutine)
1849 {
1850 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1851 isym = gfc_intrinsic_subroutine_by_id (id);
1852 }
1853 else if (sym->intmod_sym_id)
1854 {
1855 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1856 isym = gfc_intrinsic_function_by_id (id);
1857 }
1858 else if (!sym->attr.subroutine)
1859 isym = gfc_find_function (sym->name);
1860
1861 if (isym && !sym->attr.subroutine)
1862 {
1863 if (sym->ts.type != BT_UNKNOWN && warn_surprising
1864 && !sym->attr.implicit_type)
1865 gfc_warning (OPT_Wsurprising,
1866 "Type specified for intrinsic function %qs at %L is"
1867 " ignored", sym->name, &sym->declared_at);
1868
1869 if (!sym->attr.function &&
1870 !gfc_add_function(&sym->attr, sym->name, loc))
1871 return false;
1872
1873 sym->ts = isym->ts;
1874 }
1875 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1876 {
1877 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1878 {
1879 gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1880 " specifier", sym->name, &sym->declared_at);
1881 return false;
1882 }
1883
1884 if (!sym->attr.subroutine &&
1885 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1886 return false;
1887 }
1888 else
1889 {
1890 gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
1891 &sym->declared_at);
1892 return false;
1893 }
1894
1895 gfc_copy_formal_args_intr (sym, isym, NULL);
1896
1897 sym->attr.pure = isym->pure;
1898 sym->attr.elemental = isym->elemental;
1899
1900 /* Check it is actually available in the standard settings. */
1901 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1902 {
1903 gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1904 "available in the current standard settings but %s. Use "
1905 "an appropriate %<-std=*%> option or enable "
1906 "%<-fall-intrinsics%> in order to use it.",
1907 sym->name, &sym->declared_at, symstd);
1908 return false;
1909 }
1910
1911 return true;
1912 }
1913
1914
1915 /* Resolve a procedure expression, like passing it to a called procedure or as
1916 RHS for a procedure pointer assignment. */
1917
1918 static bool
1919 resolve_procedure_expression (gfc_expr* expr)
1920 {
1921 gfc_symbol* sym;
1922
1923 if (expr->expr_type != EXPR_VARIABLE)
1924 return true;
1925 gcc_assert (expr->symtree);
1926
1927 sym = expr->symtree->n.sym;
1928
1929 if (sym->attr.intrinsic)
1930 gfc_resolve_intrinsic (sym, &expr->where);
1931
1932 if (sym->attr.flavor != FL_PROCEDURE
1933 || (sym->attr.function && sym->result == sym))
1934 return true;
1935
1936 /* A non-RECURSIVE procedure that is used as procedure expression within its
1937 own body is in danger of being called recursively. */
1938 if (is_illegal_recursion (sym, gfc_current_ns))
1939 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
1940 " itself recursively. Declare it RECURSIVE or use"
1941 " %<-frecursive%>", sym->name, &expr->where);
1942
1943 return true;
1944 }
1945
1946
1947 /* Check that name is not a derived type. */
1948
1949 static bool
1950 is_dt_name (const char *name)
1951 {
1952 gfc_symbol *dt_list, *dt_first;
1953
1954 dt_list = dt_first = gfc_derived_types;
1955 for (; dt_list; dt_list = dt_list->dt_next)
1956 {
1957 if (strcmp(dt_list->name, name) == 0)
1958 return true;
1959 if (dt_first == dt_list->dt_next)
1960 break;
1961 }
1962 return false;
1963 }
1964
1965
1966 /* Resolve an actual argument list. Most of the time, this is just
1967 resolving the expressions in the list.
1968 The exception is that we sometimes have to decide whether arguments
1969 that look like procedure arguments are really simple variable
1970 references. */
1971
1972 static bool
1973 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1974 bool no_formal_args)
1975 {
1976 gfc_symbol *sym;
1977 gfc_symtree *parent_st;
1978 gfc_expr *e;
1979 gfc_component *comp;
1980 int save_need_full_assumed_size;
1981 bool return_value = false;
1982 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1983
1984 actual_arg = true;
1985 first_actual_arg = true;
1986
1987 for (; arg; arg = arg->next)
1988 {
1989 e = arg->expr;
1990 if (e == NULL)
1991 {
1992 /* Check the label is a valid branching target. */
1993 if (arg->label)
1994 {
1995 if (arg->label->defined == ST_LABEL_UNKNOWN)
1996 {
1997 gfc_error ("Label %d referenced at %L is never defined",
1998 arg->label->value, &arg->label->where);
1999 goto cleanup;
2000 }
2001 }
2002 first_actual_arg = false;
2003 continue;
2004 }
2005
2006 if (e->expr_type == EXPR_VARIABLE
2007 && e->symtree->n.sym->attr.generic
2008 && no_formal_args
2009 && count_specific_procs (e) != 1)
2010 goto cleanup;
2011
2012 if (e->ts.type != BT_PROCEDURE)
2013 {
2014 save_need_full_assumed_size = need_full_assumed_size;
2015 if (e->expr_type != EXPR_VARIABLE)
2016 need_full_assumed_size = 0;
2017 if (!gfc_resolve_expr (e))
2018 goto cleanup;
2019 need_full_assumed_size = save_need_full_assumed_size;
2020 goto argument_list;
2021 }
2022
2023 /* See if the expression node should really be a variable reference. */
2024
2025 sym = e->symtree->n.sym;
2026
2027 if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name))
2028 {
2029 gfc_error ("Derived type %qs is used as an actual "
2030 "argument at %L", sym->name, &e->where);
2031 goto cleanup;
2032 }
2033
2034 if (sym->attr.flavor == FL_PROCEDURE
2035 || sym->attr.intrinsic
2036 || sym->attr.external)
2037 {
2038 int actual_ok;
2039
2040 /* If a procedure is not already determined to be something else
2041 check if it is intrinsic. */
2042 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
2043 sym->attr.intrinsic = 1;
2044
2045 if (sym->attr.proc == PROC_ST_FUNCTION)
2046 {
2047 gfc_error ("Statement function %qs at %L is not allowed as an "
2048 "actual argument", sym->name, &e->where);
2049 }
2050
2051 actual_ok = gfc_intrinsic_actual_ok (sym->name,
2052 sym->attr.subroutine);
2053 if (sym->attr.intrinsic && actual_ok == 0)
2054 {
2055 gfc_error ("Intrinsic %qs at %L is not allowed as an "
2056 "actual argument", sym->name, &e->where);
2057 }
2058
2059 if (sym->attr.contained && !sym->attr.use_assoc
2060 && sym->ns->proc_name->attr.flavor != FL_MODULE)
2061 {
2062 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
2063 " used as actual argument at %L",
2064 sym->name, &e->where))
2065 goto cleanup;
2066 }
2067
2068 if (sym->attr.elemental && !sym->attr.intrinsic)
2069 {
2070 gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
2071 "allowed as an actual argument at %L", sym->name,
2072 &e->where);
2073 }
2074
2075 /* Check if a generic interface has a specific procedure
2076 with the same name before emitting an error. */
2077 if (sym->attr.generic && count_specific_procs (e) != 1)
2078 goto cleanup;
2079
2080 /* Just in case a specific was found for the expression. */
2081 sym = e->symtree->n.sym;
2082
2083 /* If the symbol is the function that names the current (or
2084 parent) scope, then we really have a variable reference. */
2085
2086 if (gfc_is_function_return_value (sym, sym->ns))
2087 goto got_variable;
2088
2089 /* If all else fails, see if we have a specific intrinsic. */
2090 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
2091 {
2092 gfc_intrinsic_sym *isym;
2093
2094 isym = gfc_find_function (sym->name);
2095 if (isym == NULL || !isym->specific)
2096 {
2097 gfc_error ("Unable to find a specific INTRINSIC procedure "
2098 "for the reference %qs at %L", sym->name,
2099 &e->where);
2100 goto cleanup;
2101 }
2102 sym->ts = isym->ts;
2103 sym->attr.intrinsic = 1;
2104 sym->attr.function = 1;
2105 }
2106
2107 if (!gfc_resolve_expr (e))
2108 goto cleanup;
2109 goto argument_list;
2110 }
2111
2112 /* See if the name is a module procedure in a parent unit. */
2113
2114 if (was_declared (sym) || sym->ns->parent == NULL)
2115 goto got_variable;
2116
2117 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
2118 {
2119 gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
2120 goto cleanup;
2121 }
2122
2123 if (parent_st == NULL)
2124 goto got_variable;
2125
2126 sym = parent_st->n.sym;
2127 e->symtree = parent_st; /* Point to the right thing. */
2128
2129 if (sym->attr.flavor == FL_PROCEDURE
2130 || sym->attr.intrinsic
2131 || sym->attr.external)
2132 {
2133 if (!gfc_resolve_expr (e))
2134 goto cleanup;
2135 goto argument_list;
2136 }
2137
2138 got_variable:
2139 e->expr_type = EXPR_VARIABLE;
2140 e->ts = sym->ts;
2141 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
2142 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2143 && CLASS_DATA (sym)->as))
2144 {
2145 e->rank = sym->ts.type == BT_CLASS
2146 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
2147 e->ref = gfc_get_ref ();
2148 e->ref->type = REF_ARRAY;
2149 e->ref->u.ar.type = AR_FULL;
2150 e->ref->u.ar.as = sym->ts.type == BT_CLASS
2151 ? CLASS_DATA (sym)->as : sym->as;
2152 }
2153
2154 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
2155 primary.cc (match_actual_arg). If above code determines that it
2156 is a variable instead, it needs to be resolved as it was not
2157 done at the beginning of this function. */
2158 save_need_full_assumed_size = need_full_assumed_size;
2159 if (e->expr_type != EXPR_VARIABLE)
2160 need_full_assumed_size = 0;
2161 if (!gfc_resolve_expr (e))
2162 goto cleanup;
2163 need_full_assumed_size = save_need_full_assumed_size;
2164
2165 argument_list:
2166 /* Check argument list functions %VAL, %LOC and %REF. There is
2167 nothing to do for %REF. */
2168 if (arg->name && arg->name[0] == '%')
2169 {
2170 if (strcmp ("%VAL", arg->name) == 0)
2171 {
2172 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
2173 {
2174 gfc_error ("By-value argument at %L is not of numeric "
2175 "type", &e->where);
2176 goto cleanup;
2177 }
2178
2179 if (e->rank)
2180 {
2181 gfc_error ("By-value argument at %L cannot be an array or "
2182 "an array section", &e->where);
2183 goto cleanup;
2184 }
2185
2186 /* Intrinsics are still PROC_UNKNOWN here. However,
2187 since same file external procedures are not resolvable
2188 in gfortran, it is a good deal easier to leave them to
2189 intrinsic.cc. */
2190 if (ptype != PROC_UNKNOWN
2191 && ptype != PROC_DUMMY
2192 && ptype != PROC_EXTERNAL
2193 && ptype != PROC_MODULE)
2194 {
2195 gfc_error ("By-value argument at %L is not allowed "
2196 "in this context", &e->where);
2197 goto cleanup;
2198 }
2199 }
2200
2201 /* Statement functions have already been excluded above. */
2202 else if (strcmp ("%LOC", arg->name) == 0
2203 && e->ts.type == BT_PROCEDURE)
2204 {
2205 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
2206 {
2207 gfc_error ("Passing internal procedure at %L by location "
2208 "not allowed", &e->where);
2209 goto cleanup;
2210 }
2211 }
2212 }
2213
2214 comp = gfc_get_proc_ptr_comp(e);
2215 if (e->expr_type == EXPR_VARIABLE
2216 && comp && comp->attr.elemental)
2217 {
2218 gfc_error ("ELEMENTAL procedure pointer component %qs is not "
2219 "allowed as an actual argument at %L", comp->name,
2220 &e->where);
2221 }
2222
2223 /* Fortran 2008, C1237. */
2224 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
2225 && gfc_has_ultimate_pointer (e))
2226 {
2227 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2228 "component", &e->where);
2229 goto cleanup;
2230 }
2231
2232 first_actual_arg = false;
2233 }
2234
2235 return_value = true;
2236
2237 cleanup:
2238 actual_arg = actual_arg_sav;
2239 first_actual_arg = first_actual_arg_sav;
2240
2241 return return_value;
2242 }
2243
2244
2245 /* Do the checks of the actual argument list that are specific to elemental
2246 procedures. If called with c == NULL, we have a function, otherwise if
2247 expr == NULL, we have a subroutine. */
2248
2249 static bool
2250 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2251 {
2252 gfc_actual_arglist *arg0;
2253 gfc_actual_arglist *arg;
2254 gfc_symbol *esym = NULL;
2255 gfc_intrinsic_sym *isym = NULL;
2256 gfc_expr *e = NULL;
2257 gfc_intrinsic_arg *iformal = NULL;
2258 gfc_formal_arglist *eformal = NULL;
2259 bool formal_optional = false;
2260 bool set_by_optional = false;
2261 int i;
2262 int rank = 0;
2263
2264 /* Is this an elemental procedure? */
2265 if (expr && expr->value.function.actual != NULL)
2266 {
2267 if (expr->value.function.esym != NULL
2268 && expr->value.function.esym->attr.elemental)
2269 {
2270 arg0 = expr->value.function.actual;
2271 esym = expr->value.function.esym;
2272 }
2273 else if (expr->value.function.isym != NULL
2274 && expr->value.function.isym->elemental)
2275 {
2276 arg0 = expr->value.function.actual;
2277 isym = expr->value.function.isym;
2278 }
2279 else
2280 return true;
2281 }
2282 else if (c && c->ext.actual != NULL)
2283 {
2284 arg0 = c->ext.actual;
2285
2286 if (c->resolved_sym)
2287 esym = c->resolved_sym;
2288 else
2289 esym = c->symtree->n.sym;
2290 gcc_assert (esym);
2291
2292 if (!esym->attr.elemental)
2293 return true;
2294 }
2295 else
2296 return true;
2297
2298 /* The rank of an elemental is the rank of its array argument(s). */
2299 for (arg = arg0; arg; arg = arg->next)
2300 {
2301 if (arg->expr != NULL && arg->expr->rank != 0)
2302 {
2303 rank = arg->expr->rank;
2304 if (arg->expr->expr_type == EXPR_VARIABLE
2305 && arg->expr->symtree->n.sym->attr.optional)
2306 set_by_optional = true;
2307
2308 /* Function specific; set the result rank and shape. */
2309 if (expr)
2310 {
2311 expr->rank = rank;
2312 if (!expr->shape && arg->expr->shape)
2313 {
2314 expr->shape = gfc_get_shape (rank);
2315 for (i = 0; i < rank; i++)
2316 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2317 }
2318 }
2319 break;
2320 }
2321 }
2322
2323 /* If it is an array, it shall not be supplied as an actual argument
2324 to an elemental procedure unless an array of the same rank is supplied
2325 as an actual argument corresponding to a nonoptional dummy argument of
2326 that elemental procedure(12.4.1.5). */
2327 formal_optional = false;
2328 if (isym)
2329 iformal = isym->formal;
2330 else
2331 eformal = esym->formal;
2332
2333 for (arg = arg0; arg; arg = arg->next)
2334 {
2335 if (eformal)
2336 {
2337 if (eformal->sym && eformal->sym->attr.optional)
2338 formal_optional = true;
2339 eformal = eformal->next;
2340 }
2341 else if (isym && iformal)
2342 {
2343 if (iformal->optional)
2344 formal_optional = true;
2345 iformal = iformal->next;
2346 }
2347 else if (isym)
2348 formal_optional = true;
2349
2350 if (pedantic && arg->expr != NULL
2351 && arg->expr->expr_type == EXPR_VARIABLE
2352 && arg->expr->symtree->n.sym->attr.optional
2353 && formal_optional
2354 && arg->expr->rank
2355 && (set_by_optional || arg->expr->rank != rank)
2356 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2357 {
2358 bool t = false;
2359 gfc_actual_arglist *a;
2360
2361 /* Scan the argument list for a non-optional argument with the
2362 same rank as arg. */
2363 for (a = arg0; a; a = a->next)
2364 if (a != arg
2365 && a->expr->rank == arg->expr->rank
2366 && !a->expr->symtree->n.sym->attr.optional)
2367 {
2368 t = true;
2369 break;
2370 }
2371
2372 if (!t)
2373 gfc_warning (OPT_Wpedantic,
2374 "%qs at %L is an array and OPTIONAL; If it is not "
2375 "present, then it cannot be the actual argument of "
2376 "an ELEMENTAL procedure unless there is a non-optional"
2377 " argument with the same rank "
2378 "(Fortran 2018, 15.5.2.12)",
2379 arg->expr->symtree->n.sym->name, &arg->expr->where);
2380 }
2381 }
2382
2383 for (arg = arg0; arg; arg = arg->next)
2384 {
2385 if (arg->expr == NULL || arg->expr->rank == 0)
2386 continue;
2387
2388 /* Being elemental, the last upper bound of an assumed size array
2389 argument must be present. */
2390 if (resolve_assumed_size_actual (arg->expr))
2391 return false;
2392
2393 /* Elemental procedure's array actual arguments must conform. */
2394 if (e != NULL)
2395 {
2396 if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
2397 return false;
2398 }
2399 else
2400 e = arg->expr;
2401 }
2402
2403 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2404 is an array, the intent inout/out variable needs to be also an array. */
2405 if (rank > 0 && esym && expr == NULL)
2406 for (eformal = esym->formal, arg = arg0; arg && eformal;
2407 arg = arg->next, eformal = eformal->next)
2408 if (eformal->sym
2409 && (eformal->sym->attr.intent == INTENT_OUT
2410 || eformal->sym->attr.intent == INTENT_INOUT)
2411 && arg->expr && arg->expr->rank == 0)
2412 {
2413 gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2414 "ELEMENTAL subroutine %qs is a scalar, but another "
2415 "actual argument is an array", &arg->expr->where,
2416 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2417 : "INOUT", eformal->sym->name, esym->name);
2418 return false;
2419 }
2420 return true;
2421 }
2422
2423
2424 /* This function does the checking of references to global procedures
2425 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2426 77 and 95 standards. It checks for a gsymbol for the name, making
2427 one if it does not already exist. If it already exists, then the
2428 reference being resolved must correspond to the type of gsymbol.
2429 Otherwise, the new symbol is equipped with the attributes of the
2430 reference. The corresponding code that is called in creating
2431 global entities is parse.cc.
2432
2433 In addition, for all but -std=legacy, the gsymbols are used to
2434 check the interfaces of external procedures from the same file.
2435 The namespace of the gsymbol is resolved and then, once this is
2436 done the interface is checked. */
2437
2438
2439 static bool
2440 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2441 {
2442 if (!gsym_ns->proc_name->attr.recursive)
2443 return true;
2444
2445 if (sym->ns == gsym_ns)
2446 return false;
2447
2448 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2449 return false;
2450
2451 return true;
2452 }
2453
2454 static bool
2455 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2456 {
2457 if (gsym_ns->entries)
2458 {
2459 gfc_entry_list *entry = gsym_ns->entries;
2460
2461 for (; entry; entry = entry->next)
2462 {
2463 if (strcmp (sym->name, entry->sym->name) == 0)
2464 {
2465 if (strcmp (gsym_ns->proc_name->name,
2466 sym->ns->proc_name->name) == 0)
2467 return false;
2468
2469 if (sym->ns->parent
2470 && strcmp (gsym_ns->proc_name->name,
2471 sym->ns->parent->proc_name->name) == 0)
2472 return false;
2473 }
2474 }
2475 }
2476 return true;
2477 }
2478
2479
2480 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2481
2482 bool
2483 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2484 {
2485 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2486
2487 for ( ; arg; arg = arg->next)
2488 {
2489 if (!arg->sym)
2490 continue;
2491
2492 if (arg->sym->attr.allocatable) /* (2a) */
2493 {
2494 strncpy (errmsg, _("allocatable argument"), err_len);
2495 return true;
2496 }
2497 else if (arg->sym->attr.asynchronous)
2498 {
2499 strncpy (errmsg, _("asynchronous argument"), err_len);
2500 return true;
2501 }
2502 else if (arg->sym->attr.optional)
2503 {
2504 strncpy (errmsg, _("optional argument"), err_len);
2505 return true;
2506 }
2507 else if (arg->sym->attr.pointer)
2508 {
2509 strncpy (errmsg, _("pointer argument"), err_len);
2510 return true;
2511 }
2512 else if (arg->sym->attr.target)
2513 {
2514 strncpy (errmsg, _("target argument"), err_len);
2515 return true;
2516 }
2517 else if (arg->sym->attr.value)
2518 {
2519 strncpy (errmsg, _("value argument"), err_len);
2520 return true;
2521 }
2522 else if (arg->sym->attr.volatile_)
2523 {
2524 strncpy (errmsg, _("volatile argument"), err_len);
2525 return true;
2526 }
2527 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2528 {
2529 strncpy (errmsg, _("assumed-shape argument"), err_len);
2530 return true;
2531 }
2532 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2533 {
2534 strncpy (errmsg, _("assumed-rank argument"), err_len);
2535 return true;
2536 }
2537 else if (arg->sym->attr.codimension) /* (2c) */
2538 {
2539 strncpy (errmsg, _("coarray argument"), err_len);
2540 return true;
2541 }
2542 else if (false) /* (2d) TODO: parametrized derived type */
2543 {
2544 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2545 return true;
2546 }
2547 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2548 {
2549 strncpy (errmsg, _("polymorphic argument"), err_len);
2550 return true;
2551 }
2552 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2553 {
2554 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2555 return true;
2556 }
2557 else if (arg->sym->ts.type == BT_ASSUMED)
2558 {
2559 /* As assumed-type is unlimited polymorphic (cf. above).
2560 See also TS 29113, Note 6.1. */
2561 strncpy (errmsg, _("assumed-type argument"), err_len);
2562 return true;
2563 }
2564 }
2565
2566 if (sym->attr.function)
2567 {
2568 gfc_symbol *res = sym->result ? sym->result : sym;
2569
2570 if (res->attr.dimension) /* (3a) */
2571 {
2572 strncpy (errmsg, _("array result"), err_len);
2573 return true;
2574 }
2575 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2576 {
2577 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2578 return true;
2579 }
2580 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2581 && res->ts.u.cl->length
2582 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2583 {
2584 strncpy (errmsg, _("result with non-constant character length"), err_len);
2585 return true;
2586 }
2587 }
2588
2589 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2590 {
2591 strncpy (errmsg, _("elemental procedure"), err_len);
2592 return true;
2593 }
2594 else if (sym->attr.is_bind_c) /* (5) */
2595 {
2596 strncpy (errmsg, _("bind(c) procedure"), err_len);
2597 return true;
2598 }
2599
2600 return false;
2601 }
2602
2603
2604 static void
2605 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
2606 {
2607 gfc_gsymbol * gsym;
2608 gfc_namespace *ns;
2609 enum gfc_symbol_type type;
2610 char reason[200];
2611
2612 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2613
2614 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
2615 sym->binding_label != NULL);
2616
2617 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2618 gfc_global_used (gsym, where);
2619
2620 if ((sym->attr.if_source == IFSRC_UNKNOWN
2621 || sym->attr.if_source == IFSRC_IFBODY)
2622 && gsym->type != GSYM_UNKNOWN
2623 && !gsym->binding_label
2624 && gsym->ns
2625 && gsym->ns->proc_name
2626 && not_in_recursive (sym, gsym->ns)
2627 && not_entry_self_reference (sym, gsym->ns))
2628 {
2629 gfc_symbol *def_sym;
2630 def_sym = gsym->ns->proc_name;
2631
2632 if (gsym->ns->resolved != -1)
2633 {
2634
2635 /* Resolve the gsymbol namespace if needed. */
2636 if (!gsym->ns->resolved)
2637 {
2638 gfc_symbol *old_dt_list;
2639
2640 /* Stash away derived types so that the backend_decls
2641 do not get mixed up. */
2642 old_dt_list = gfc_derived_types;
2643 gfc_derived_types = NULL;
2644
2645 gfc_resolve (gsym->ns);
2646
2647 /* Store the new derived types with the global namespace. */
2648 if (gfc_derived_types)
2649 gsym->ns->derived_types = gfc_derived_types;
2650
2651 /* Restore the derived types of this namespace. */
2652 gfc_derived_types = old_dt_list;
2653 }
2654
2655 /* Make sure that translation for the gsymbol occurs before
2656 the procedure currently being resolved. */
2657 ns = gfc_global_ns_list;
2658 for (; ns && ns != gsym->ns; ns = ns->sibling)
2659 {
2660 if (ns->sibling == gsym->ns)
2661 {
2662 ns->sibling = gsym->ns->sibling;
2663 gsym->ns->sibling = gfc_global_ns_list;
2664 gfc_global_ns_list = gsym->ns;
2665 break;
2666 }
2667 }
2668
2669 /* This can happen if a binding name has been specified. */
2670 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2671 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2672
2673 if (def_sym->attr.entry_master || def_sym->attr.entry)
2674 {
2675 gfc_entry_list *entry;
2676 for (entry = gsym->ns->entries; entry; entry = entry->next)
2677 if (strcmp (entry->sym->name, sym->name) == 0)
2678 {
2679 def_sym = entry->sym;
2680 break;
2681 }
2682 }
2683 }
2684
2685 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2686 {
2687 gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2688 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2689 gfc_typename (&def_sym->ts));
2690 goto done;
2691 }
2692
2693 if (sym->attr.if_source == IFSRC_UNKNOWN
2694 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2695 {
2696 gfc_error ("Explicit interface required for %qs at %L: %s",
2697 sym->name, &sym->declared_at, reason);
2698 goto done;
2699 }
2700
2701 bool bad_result_characteristics;
2702 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2703 reason, sizeof(reason), NULL, NULL,
2704 &bad_result_characteristics))
2705 {
2706 /* Turn erros into warnings with -std=gnu and -std=legacy,
2707 unless a function returns a wrong type, which can lead
2708 to all kinds of ICEs and wrong code. */
2709
2710 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
2711 && !bad_result_characteristics)
2712 gfc_errors_to_warnings (true);
2713
2714 gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
2715 sym->name, &sym->declared_at, reason);
2716 sym->error = 1;
2717 gfc_errors_to_warnings (false);
2718 goto done;
2719 }
2720 }
2721
2722 done:
2723
2724 if (gsym->type == GSYM_UNKNOWN)
2725 {
2726 gsym->type = type;
2727 gsym->where = *where;
2728 }
2729
2730 gsym->used = 1;
2731 }
2732
2733
2734 /************* Function resolution *************/
2735
2736 /* Resolve a function call known to be generic.
2737 Section 14.1.2.4.1. */
2738
2739 static match
2740 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2741 {
2742 gfc_symbol *s;
2743
2744 if (sym->attr.generic)
2745 {
2746 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2747 if (s != NULL)
2748 {
2749 expr->value.function.name = s->name;
2750 expr->value.function.esym = s;
2751
2752 if (s->ts.type != BT_UNKNOWN)
2753 expr->ts = s->ts;
2754 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2755 expr->ts = s->result->ts;
2756
2757 if (s->as != NULL)
2758 expr->rank = s->as->rank;
2759 else if (s->result != NULL && s->result->as != NULL)
2760 expr->rank = s->result->as->rank;
2761
2762 gfc_set_sym_referenced (expr->value.function.esym);
2763
2764 return MATCH_YES;
2765 }
2766
2767 /* TODO: Need to search for elemental references in generic
2768 interface. */
2769 }
2770
2771 if (sym->attr.intrinsic)
2772 return gfc_intrinsic_func_interface (expr, 0);
2773
2774 return MATCH_NO;
2775 }
2776
2777
2778 static bool
2779 resolve_generic_f (gfc_expr *expr)
2780 {
2781 gfc_symbol *sym;
2782 match m;
2783 gfc_interface *intr = NULL;
2784
2785 sym = expr->symtree->n.sym;
2786
2787 for (;;)
2788 {
2789 m = resolve_generic_f0 (expr, sym);
2790 if (m == MATCH_YES)
2791 return true;
2792 else if (m == MATCH_ERROR)
2793 return false;
2794
2795 generic:
2796 if (!intr)
2797 for (intr = sym->generic; intr; intr = intr->next)
2798 if (gfc_fl_struct (intr->sym->attr.flavor))
2799 break;
2800
2801 if (sym->ns->parent == NULL)
2802 break;
2803 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2804
2805 if (sym == NULL)
2806 break;
2807 if (!generic_sym (sym))
2808 goto generic;
2809 }
2810
2811 /* Last ditch attempt. See if the reference is to an intrinsic
2812 that possesses a matching interface. 14.1.2.4 */
2813 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2814 {
2815 if (gfc_init_expr_flag)
2816 gfc_error ("Function %qs in initialization expression at %L "
2817 "must be an intrinsic function",
2818 expr->symtree->n.sym->name, &expr->where);
2819 else
2820 gfc_error ("There is no specific function for the generic %qs "
2821 "at %L", expr->symtree->n.sym->name, &expr->where);
2822 return false;
2823 }
2824
2825 if (intr)
2826 {
2827 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2828 NULL, false))
2829 return false;
2830 if (!gfc_use_derived (expr->ts.u.derived))
2831 return false;
2832 return resolve_structure_cons (expr, 0);
2833 }
2834
2835 m = gfc_intrinsic_func_interface (expr, 0);
2836 if (m == MATCH_YES)
2837 return true;
2838
2839 if (m == MATCH_NO)
2840 gfc_error ("Generic function %qs at %L is not consistent with a "
2841 "specific intrinsic interface", expr->symtree->n.sym->name,
2842 &expr->where);
2843
2844 return false;
2845 }
2846
2847
2848 /* Resolve a function call known to be specific. */
2849
2850 static match
2851 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2852 {
2853 match m;
2854
2855 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2856 {
2857 if (sym->attr.dummy)
2858 {
2859 sym->attr.proc = PROC_DUMMY;
2860 goto found;
2861 }
2862
2863 sym->attr.proc = PROC_EXTERNAL;
2864 goto found;
2865 }
2866
2867 if (sym->attr.proc == PROC_MODULE
2868 || sym->attr.proc == PROC_ST_FUNCTION
2869 || sym->attr.proc == PROC_INTERNAL)
2870 goto found;
2871
2872 if (sym->attr.intrinsic)
2873 {
2874 m = gfc_intrinsic_func_interface (expr, 1);
2875 if (m == MATCH_YES)
2876 return MATCH_YES;
2877 if (m == MATCH_NO)
2878 gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
2879 "with an intrinsic", sym->name, &expr->where);
2880
2881 return MATCH_ERROR;
2882 }
2883
2884 return MATCH_NO;
2885
2886 found:
2887 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2888
2889 if (sym->result)
2890 expr->ts = sym->result->ts;
2891 else
2892 expr->ts = sym->ts;
2893 expr->value.function.name = sym->name;
2894 expr->value.function.esym = sym;
2895 /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2896 error(s). */
2897 if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
2898 return MATCH_ERROR;
2899 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2900 expr->rank = CLASS_DATA (sym)->as->rank;
2901 else if (sym->as != NULL)
2902 expr->rank = sym->as->rank;
2903
2904 return MATCH_YES;
2905 }
2906
2907
2908 static bool
2909 resolve_specific_f (gfc_expr *expr)
2910 {
2911 gfc_symbol *sym;
2912 match m;
2913
2914 sym = expr->symtree->n.sym;
2915
2916 for (;;)
2917 {
2918 m = resolve_specific_f0 (sym, expr);
2919 if (m == MATCH_YES)
2920 return true;
2921 if (m == MATCH_ERROR)
2922 return false;
2923
2924 if (sym->ns->parent == NULL)
2925 break;
2926
2927 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2928
2929 if (sym == NULL)
2930 break;
2931 }
2932
2933 gfc_error ("Unable to resolve the specific function %qs at %L",
2934 expr->symtree->n.sym->name, &expr->where);
2935
2936 return true;
2937 }
2938
2939 /* Recursively append candidate SYM to CANDIDATES. Store the number of
2940 candidates in CANDIDATES_LEN. */
2941
2942 static void
2943 lookup_function_fuzzy_find_candidates (gfc_symtree *sym,
2944 char **&candidates,
2945 size_t &candidates_len)
2946 {
2947 gfc_symtree *p;
2948
2949 if (sym == NULL)
2950 return;
2951 if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external)
2952 && sym->n.sym->attr.flavor == FL_PROCEDURE)
2953 vec_push (candidates, candidates_len, sym->name);
2954
2955 p = sym->left;
2956 if (p)
2957 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
2958
2959 p = sym->right;
2960 if (p)
2961 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
2962 }
2963
2964
2965 /* Lookup function FN fuzzily, taking names in SYMROOT into account. */
2966
2967 const char*
2968 gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot)
2969 {
2970 char **candidates = NULL;
2971 size_t candidates_len = 0;
2972 lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len);
2973 return gfc_closest_fuzzy_match (fn, candidates);
2974 }
2975
2976
2977 /* Resolve a procedure call not known to be generic nor specific. */
2978
2979 static bool
2980 resolve_unknown_f (gfc_expr *expr)
2981 {
2982 gfc_symbol *sym;
2983 gfc_typespec *ts;
2984
2985 sym = expr->symtree->n.sym;
2986
2987 if (sym->attr.dummy)
2988 {
2989 sym->attr.proc = PROC_DUMMY;
2990 expr->value.function.name = sym->name;
2991 goto set_type;
2992 }
2993
2994 /* See if we have an intrinsic function reference. */
2995
2996 if (gfc_is_intrinsic (sym, 0, expr->where))
2997 {
2998 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2999 return true;
3000 return false;
3001 }
3002
3003 /* IMPLICIT NONE (external) procedures require an explicit EXTERNAL attr. */
3004 /* Intrinsics were handled above, only non-intrinsics left here. */
3005 if (sym->attr.flavor == FL_PROCEDURE
3006 && sym->attr.implicit_type
3007 && sym->ns
3008 && sym->ns->has_implicit_none_export)
3009 {
3010 gfc_error ("Missing explicit declaration with EXTERNAL attribute "
3011 "for symbol %qs at %L", sym->name, &sym->declared_at);
3012 sym->error = 1;
3013 return false;
3014 }
3015
3016 /* The reference is to an external name. */
3017
3018 sym->attr.proc = PROC_EXTERNAL;
3019 expr->value.function.name = sym->name;
3020 expr->value.function.esym = expr->symtree->n.sym;
3021
3022 if (sym->as != NULL)
3023 expr->rank = sym->as->rank;
3024
3025 /* Type of the expression is either the type of the symbol or the
3026 default type of the symbol. */
3027
3028 set_type:
3029 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
3030
3031 if (sym->ts.type != BT_UNKNOWN)
3032 expr->ts = sym->ts;
3033 else
3034 {
3035 ts = gfc_get_default_type (sym->name, sym->ns);
3036
3037 if (ts->type == BT_UNKNOWN)
3038 {
3039 const char *guessed
3040 = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
3041 if (guessed)
3042 gfc_error ("Function %qs at %L has no IMPLICIT type"
3043 "; did you mean %qs?",
3044 sym->name, &expr->where, guessed);
3045 else
3046 gfc_error ("Function %qs at %L has no IMPLICIT type",
3047 sym->name, &expr->where);
3048 return false;
3049 }
3050 else
3051 expr->ts = *ts;
3052 }
3053
3054 return true;
3055 }
3056
3057
3058 /* Return true, if the symbol is an external procedure. */
3059 static bool
3060 is_external_proc (gfc_symbol *sym)
3061 {
3062 if (!sym->attr.dummy && !sym->attr.contained
3063 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
3064 && sym->attr.proc != PROC_ST_FUNCTION
3065 && !sym->attr.proc_pointer
3066 && !sym->attr.use_assoc
3067 && sym->name)
3068 return true;
3069
3070 return false;
3071 }
3072
3073
3074 /* Figure out if a function reference is pure or not. Also set the name
3075 of the function for a potential error message. Return nonzero if the
3076 function is PURE, zero if not. */
3077 static int
3078 pure_stmt_function (gfc_expr *, gfc_symbol *);
3079
3080 int
3081 gfc_pure_function (gfc_expr *e, const char **name)
3082 {
3083 int pure;
3084 gfc_component *comp;
3085
3086 *name = NULL;
3087
3088 if (e->symtree != NULL
3089 && e->symtree->n.sym != NULL
3090 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3091 return pure_stmt_function (e, e->symtree->n.sym);
3092
3093 comp = gfc_get_proc_ptr_comp (e);
3094 if (comp)
3095 {
3096 pure = gfc_pure (comp->ts.interface);
3097 *name = comp->name;
3098 }
3099 else if (e->value.function.esym)
3100 {
3101 pure = gfc_pure (e->value.function.esym);
3102 *name = e->value.function.esym->name;
3103 }
3104 else if (e->value.function.isym)
3105 {
3106 pure = e->value.function.isym->pure
3107 || e->value.function.isym->elemental;
3108 *name = e->value.function.isym->name;
3109 }
3110 else
3111 {
3112 /* Implicit functions are not pure. */
3113 pure = 0;
3114 *name = e->value.function.name;
3115 }
3116
3117 return pure;
3118 }
3119
3120
3121 /* Check if the expression is a reference to an implicitly pure function. */
3122
3123 int
3124 gfc_implicit_pure_function (gfc_expr *e)
3125 {
3126 gfc_component *comp = gfc_get_proc_ptr_comp (e);
3127 if (comp)
3128 return gfc_implicit_pure (comp->ts.interface);
3129 else if (e->value.function.esym)
3130 return gfc_implicit_pure (e->value.function.esym);
3131 else
3132 return 0;
3133 }
3134
3135
3136 static bool
3137 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
3138 int *f ATTRIBUTE_UNUSED)
3139 {
3140 const char *name;
3141
3142 /* Don't bother recursing into other statement functions
3143 since they will be checked individually for purity. */
3144 if (e->expr_type != EXPR_FUNCTION
3145 || !e->symtree
3146 || e->symtree->n.sym == sym
3147 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3148 return false;
3149
3150 return gfc_pure_function (e, &name) ? false : true;
3151 }
3152
3153
3154 static int
3155 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
3156 {
3157 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
3158 }
3159
3160
3161 /* Check if an impure function is allowed in the current context. */
3162
3163 static bool check_pure_function (gfc_expr *e)
3164 {
3165 const char *name = NULL;
3166 if (!gfc_pure_function (e, &name) && name)
3167 {
3168 if (forall_flag)
3169 {
3170 gfc_error ("Reference to impure function %qs at %L inside a "
3171 "FORALL %s", name, &e->where,
3172 forall_flag == 2 ? "mask" : "block");
3173 return false;
3174 }
3175 else if (gfc_do_concurrent_flag)
3176 {
3177 gfc_error ("Reference to impure function %qs at %L inside a "
3178 "DO CONCURRENT %s", name, &e->where,
3179 gfc_do_concurrent_flag == 2 ? "mask" : "block");
3180 return false;
3181 }
3182 else if (gfc_pure (NULL))
3183 {
3184 gfc_error ("Reference to impure function %qs at %L "
3185 "within a PURE procedure", name, &e->where);
3186 return false;
3187 }
3188 if (!gfc_implicit_pure_function (e))
3189 gfc_unset_implicit_pure (NULL);
3190 }
3191 return true;
3192 }
3193
3194
3195 /* Update current procedure's array_outer_dependency flag, considering
3196 a call to procedure SYM. */
3197
3198 static void
3199 update_current_proc_array_outer_dependency (gfc_symbol *sym)
3200 {
3201 /* Check to see if this is a sibling function that has not yet
3202 been resolved. */
3203 gfc_namespace *sibling = gfc_current_ns->sibling;
3204 for (; sibling; sibling = sibling->sibling)
3205 {
3206 if (sibling->proc_name == sym)
3207 {
3208 gfc_resolve (sibling);
3209 break;
3210 }
3211 }
3212
3213 /* If SYM has references to outer arrays, so has the procedure calling
3214 SYM. If SYM is a procedure pointer, we can assume the worst. */
3215 if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
3216 && gfc_current_ns->proc_name)
3217 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3218 }
3219
3220
3221 /* Resolve a function call, which means resolving the arguments, then figuring
3222 out which entity the name refers to. */
3223
3224 static bool
3225 resolve_function (gfc_expr *expr)
3226 {
3227 gfc_actual_arglist *arg;
3228 gfc_symbol *sym;
3229 bool t;
3230 int temp;
3231 procedure_type p = PROC_INTRINSIC;
3232 bool no_formal_args;
3233
3234 sym = NULL;
3235 if (expr->symtree)
3236 sym = expr->symtree->n.sym;
3237
3238 /* If this is a procedure pointer component, it has already been resolved. */
3239 if (gfc_is_proc_ptr_comp (expr))
3240 return true;
3241
3242 /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
3243 another caf_get. */
3244 if (sym && sym->attr.intrinsic
3245 && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
3246 || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
3247 return true;
3248
3249 if (expr->ref)
3250 {
3251 gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name,
3252 &expr->where);
3253 return false;
3254 }
3255
3256 if (sym && sym->attr.intrinsic
3257 && !gfc_resolve_intrinsic (sym, &expr->where))
3258 return false;
3259
3260 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
3261 {
3262 gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
3263 return false;
3264 }
3265
3266 /* If this is a deferred TBP with an abstract interface (which may
3267 of course be referenced), expr->value.function.esym will be set. */
3268 if (sym && sym->attr.abstract && !expr->value.function.esym)
3269 {
3270 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3271 sym->name, &expr->where);
3272 return false;
3273 }
3274
3275 /* If this is a deferred TBP with an abstract interface, its result
3276 cannot be an assumed length character (F2003: C418). */
3277 if (sym && sym->attr.abstract && sym->attr.function
3278 && sym->result->ts.u.cl
3279 && sym->result->ts.u.cl->length == NULL
3280 && !sym->result->ts.deferred)
3281 {
3282 gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
3283 "character length result (F2008: C418)", sym->name,
3284 &sym->declared_at);
3285 return false;
3286 }
3287
3288 /* Switch off assumed size checking and do this again for certain kinds
3289 of procedure, once the procedure itself is resolved. */
3290 need_full_assumed_size++;
3291
3292 if (expr->symtree && expr->symtree->n.sym)
3293 p = expr->symtree->n.sym->attr.proc;
3294
3295 if (expr->value.function.isym && expr->value.function.isym->inquiry)
3296 inquiry_argument = true;
3297 no_formal_args = sym && is_external_proc (sym)
3298 && gfc_sym_get_dummy_args (sym) == NULL;
3299
3300 if (!resolve_actual_arglist (expr->value.function.actual,
3301 p, no_formal_args))
3302 {
3303 inquiry_argument = false;
3304 return false;
3305 }
3306
3307 inquiry_argument = false;
3308
3309 /* Resume assumed_size checking. */
3310 need_full_assumed_size--;
3311
3312 /* If the procedure is external, check for usage. */
3313 if (sym && is_external_proc (sym))
3314 resolve_global_procedure (sym, &expr->where, 0);
3315
3316 if (sym && sym->ts.type == BT_CHARACTER
3317 && sym->ts.u.cl
3318 && sym->ts.u.cl->length == NULL
3319 && !sym->attr.dummy
3320 && !sym->ts.deferred
3321 && expr->value.function.esym == NULL
3322 && !sym->attr.contained)
3323 {
3324 /* Internal procedures are taken care of in resolve_contained_fntype. */
3325 gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
3326 "be used at %L since it is not a dummy argument",
3327 sym->name, &expr->where);
3328 return false;
3329 }
3330
3331 /* See if function is already resolved. */
3332
3333 if (expr->value.function.name != NULL
3334 || expr->value.function.isym != NULL)
3335 {
3336 if (expr->ts.type == BT_UNKNOWN)
3337 expr->ts = sym->ts;
3338 t = true;
3339 }
3340 else
3341 {
3342 /* Apply the rules of section 14.1.2. */
3343
3344 switch (procedure_kind (sym))
3345 {
3346 case PTYPE_GENERIC:
3347 t = resolve_generic_f (expr);
3348 break;
3349
3350 case PTYPE_SPECIFIC:
3351 t = resolve_specific_f (expr);
3352 break;
3353
3354 case PTYPE_UNKNOWN:
3355 t = resolve_unknown_f (expr);
3356 break;
3357
3358 default:
3359 gfc_internal_error ("resolve_function(): bad function type");
3360 }
3361 }
3362
3363 /* If the expression is still a function (it might have simplified),
3364 then we check to see if we are calling an elemental function. */
3365
3366 if (expr->expr_type != EXPR_FUNCTION)
3367 return t;
3368
3369 /* Walk the argument list looking for invalid BOZ. */
3370 for (arg = expr->value.function.actual; arg; arg = arg->next)
3371 if (arg->expr && arg->expr->ts.type == BT_BOZ)
3372 {
3373 gfc_error ("A BOZ literal constant at %L cannot appear as an "
3374 "actual argument in a function reference",
3375 &arg->expr->where);
3376 return false;
3377 }
3378
3379 temp = need_full_assumed_size;
3380 need_full_assumed_size = 0;
3381
3382 if (!resolve_elemental_actual (expr, NULL))
3383 return false;
3384
3385 if (omp_workshare_flag
3386 && expr->value.function.esym
3387 && ! gfc_elemental (expr->value.function.esym))
3388 {
3389 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3390 "in WORKSHARE construct", expr->value.function.esym->name,
3391 &expr->where);
3392 t = false;
3393 }
3394
3395 #define GENERIC_ID expr->value.function.isym->id
3396 else if (expr->value.function.actual != NULL
3397 && expr->value.function.isym != NULL
3398 && GENERIC_ID != GFC_ISYM_LBOUND
3399 && GENERIC_ID != GFC_ISYM_LCOBOUND
3400 && GENERIC_ID != GFC_ISYM_UCOBOUND
3401 && GENERIC_ID != GFC_ISYM_LEN
3402 && GENERIC_ID != GFC_ISYM_LOC
3403 && GENERIC_ID != GFC_ISYM_C_LOC
3404 && GENERIC_ID != GFC_ISYM_PRESENT)
3405 {
3406 /* Array intrinsics must also have the last upper bound of an
3407 assumed size array argument. UBOUND and SIZE have to be
3408 excluded from the check if the second argument is anything
3409 than a constant. */
3410
3411 for (arg = expr->value.function.actual; arg; arg = arg->next)
3412 {
3413 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3414 && arg == expr->value.function.actual
3415 && arg->next != NULL && arg->next->expr)
3416 {
3417 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3418 break;
3419
3420 if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
3421 break;
3422
3423 if ((int)mpz_get_si (arg->next->expr->value.integer)
3424 < arg->expr->rank)
3425 break;
3426 }
3427
3428 if (arg->expr != NULL
3429 && arg->expr->rank > 0
3430 && resolve_assumed_size_actual (arg->expr))
3431 return false;
3432 }
3433 }
3434 #undef GENERIC_ID
3435
3436 need_full_assumed_size = temp;
3437
3438 if (!check_pure_function(expr))
3439 t = false;
3440
3441 /* Functions without the RECURSIVE attribution are not allowed to
3442 * call themselves. */
3443 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3444 {
3445 gfc_symbol *esym;
3446 esym = expr->value.function.esym;
3447
3448 if (is_illegal_recursion (esym, gfc_current_ns))
3449 {
3450 if (esym->attr.entry && esym->ns->entries)
3451 gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3452 " function %qs is not RECURSIVE",
3453 esym->name, &expr->where, esym->ns->entries->sym->name);
3454 else
3455 gfc_error ("Function %qs at %L cannot be called recursively, as it"
3456 " is not RECURSIVE", esym->name, &expr->where);
3457
3458 t = false;
3459 }
3460 }
3461
3462 /* Character lengths of use associated functions may contains references to
3463 symbols not referenced from the current program unit otherwise. Make sure
3464 those symbols are marked as referenced. */
3465
3466 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3467 && expr->value.function.esym->attr.use_assoc)
3468 {
3469 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3470 }
3471
3472 /* Make sure that the expression has a typespec that works. */
3473 if (expr->ts.type == BT_UNKNOWN)
3474 {
3475 if (expr->symtree->n.sym->result
3476 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3477 && !expr->symtree->n.sym->result->attr.proc_pointer)
3478 expr->ts = expr->symtree->n.sym->result->ts;
3479 }
3480
3481 /* These derived types with an incomplete namespace, arising from use
3482 association, cause gfc_get_derived_vtab to segfault. If the function
3483 namespace does not suffice, something is badly wrong. */
3484 if (expr->ts.type == BT_DERIVED
3485 && !expr->ts.u.derived->ns->proc_name)
3486 {
3487 gfc_symbol *der;
3488 gfc_find_symbol (expr->ts.u.derived->name, expr->symtree->n.sym->ns, 1, &der);
3489 if (der)
3490 {
3491 expr->ts.u.derived->refs--;
3492 expr->ts.u.derived = der;
3493 der->refs++;
3494 }
3495 else
3496 expr->ts.u.derived->ns = expr->symtree->n.sym->ns;
3497 }
3498
3499 if (!expr->ref && !expr->value.function.isym)
3500 {
3501 if (expr->value.function.esym)
3502 update_current_proc_array_outer_dependency (expr->value.function.esym);
3503 else
3504 update_current_proc_array_outer_dependency (sym);
3505 }
3506 else if (expr->ref)
3507 /* typebound procedure: Assume the worst. */
3508 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3509
3510 if (expr->value.function.esym
3511 && expr->value.function.esym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
3512 gfc_warning (OPT_Wdeprecated_declarations,
3513 "Using function %qs at %L is deprecated",
3514 sym->name, &expr->where);
3515 return t;
3516 }
3517
3518
3519 /************* Subroutine resolution *************/
3520
3521 static bool
3522 pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3523 {
3524 if (gfc_pure (sym))
3525 return true;
3526
3527 if (forall_flag)
3528 {
3529 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3530 name, loc);
3531 return false;
3532 }
3533 else if (gfc_do_concurrent_flag)
3534 {
3535 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3536 "PURE", name, loc);
3537 return false;
3538 }
3539 else if (gfc_pure (NULL))
3540 {
3541 gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3542 return false;
3543 }
3544
3545 gfc_unset_implicit_pure (NULL);
3546 return true;
3547 }
3548
3549
3550 static match
3551 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3552 {
3553 gfc_symbol *s;
3554
3555 if (sym->attr.generic)
3556 {
3557 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3558 if (s != NULL)
3559 {
3560 c->resolved_sym = s;
3561 if (!pure_subroutine (s, s->name, &c->loc))
3562 return MATCH_ERROR;
3563 return MATCH_YES;
3564 }
3565
3566 /* TODO: Need to search for elemental references in generic interface. */
3567 }
3568
3569 if (sym->attr.intrinsic)
3570 return gfc_intrinsic_sub_interface (c, 0);
3571
3572 return MATCH_NO;
3573 }
3574
3575
3576 static bool
3577 resolve_generic_s (gfc_code *c)
3578 {
3579 gfc_symbol *sym;
3580 match m;
3581
3582 sym = c->symtree->n.sym;
3583
3584 for (;;)
3585 {
3586 m = resolve_generic_s0 (c, sym);
3587 if (m == MATCH_YES)
3588 return true;
3589 else if (m == MATCH_ERROR)
3590 return false;
3591
3592 generic:
3593 if (sym->ns->parent == NULL)
3594 break;
3595 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3596
3597 if (sym == NULL)
3598 break;
3599 if (!generic_sym (sym))
3600 goto generic;
3601 }
3602
3603 /* Last ditch attempt. See if the reference is to an intrinsic
3604 that possesses a matching interface. 14.1.2.4 */
3605 sym = c->symtree->n.sym;
3606
3607 if (!gfc_is_intrinsic (sym, 1, c->loc))
3608 {
3609 gfc_error ("There is no specific subroutine for the generic %qs at %L",
3610 sym->name, &c->loc);
3611 return false;
3612 }
3613
3614 m = gfc_intrinsic_sub_interface (c, 0);
3615 if (m == MATCH_YES)
3616 return true;
3617 if (m == MATCH_NO)
3618 gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3619 "intrinsic subroutine interface", sym->name, &c->loc);
3620
3621 return false;
3622 }
3623
3624
3625 /* Resolve a subroutine call known to be specific. */
3626
3627 static match
3628 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3629 {
3630 match m;
3631
3632 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3633 {
3634 if (sym->attr.dummy)
3635 {
3636 sym->attr.proc = PROC_DUMMY;
3637 goto found;
3638 }
3639
3640 sym->attr.proc = PROC_EXTERNAL;
3641 goto found;
3642 }
3643
3644 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3645 goto found;
3646
3647 if (sym->attr.intrinsic)
3648 {
3649 m = gfc_intrinsic_sub_interface (c, 1);
3650 if (m == MATCH_YES)
3651 return MATCH_YES;
3652 if (m == MATCH_NO)
3653 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3654 "with an intrinsic", sym->name, &c->loc);
3655
3656 return MATCH_ERROR;
3657 }
3658
3659 return MATCH_NO;
3660
3661 found:
3662 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3663
3664 c->resolved_sym = sym;
3665 if (!pure_subroutine (sym, sym->name, &c->loc))
3666 return MATCH_ERROR;
3667
3668 return MATCH_YES;
3669 }
3670
3671
3672 static bool
3673 resolve_specific_s (gfc_code *c)
3674 {
3675 gfc_symbol *sym;
3676 match m;
3677
3678 sym = c->symtree->n.sym;
3679
3680 for (;;)
3681 {
3682 m = resolve_specific_s0 (c, sym);
3683 if (m == MATCH_YES)
3684 return true;
3685 if (m == MATCH_ERROR)
3686 return false;
3687
3688 if (sym->ns->parent == NULL)
3689 break;
3690
3691 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3692
3693 if (sym == NULL)
3694 break;
3695 }
3696
3697 sym = c->symtree->n.sym;
3698 gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3699 sym->name, &c->loc);
3700
3701 return false;
3702 }
3703
3704
3705 /* Resolve a subroutine call not known to be generic nor specific. */
3706
3707 static bool
3708 resolve_unknown_s (gfc_code *c)
3709 {
3710 gfc_symbol *sym;
3711
3712 sym = c->symtree->n.sym;
3713
3714 if (sym->attr.dummy)
3715 {
3716 sym->attr.proc = PROC_DUMMY;
3717 goto found;
3718 }
3719
3720 /* See if we have an intrinsic function reference. */
3721
3722 if (gfc_is_intrinsic (sym, 1, c->loc))
3723 {
3724 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3725 return true;
3726 return false;
3727 }
3728
3729 /* The reference is to an external name. */
3730
3731 found:
3732 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3733
3734 c->resolved_sym = sym;
3735
3736 return pure_subroutine (sym, sym->name, &c->loc);
3737 }
3738
3739
3740 /* Resolve a subroutine call. Although it was tempting to use the same code
3741 for functions, subroutines and functions are stored differently and this
3742 makes things awkward. */
3743
3744 static bool
3745 resolve_call (gfc_code *c)
3746 {
3747 bool t;
3748 procedure_type ptype = PROC_INTRINSIC;
3749 gfc_symbol *csym, *sym;
3750 bool no_formal_args;
3751
3752 csym = c->symtree ? c->symtree->n.sym : NULL;
3753
3754 if (csym && csym->ts.type != BT_UNKNOWN)
3755 {
3756 gfc_error ("%qs at %L has a type, which is not consistent with "
3757 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3758 return false;
3759 }
3760
3761 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3762 {
3763 gfc_symtree *st;
3764 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3765 sym = st ? st->n.sym : NULL;
3766 if (sym && csym != sym
3767 && sym->ns == gfc_current_ns
3768 && sym->attr.flavor == FL_PROCEDURE
3769 && sym->attr.contained)
3770 {
3771 sym->refs++;
3772 if (csym->attr.generic)
3773 c->symtree->n.sym = sym;
3774 else
3775 c->symtree = st;
3776 csym = c->symtree->n.sym;
3777 }
3778 }
3779
3780 /* If this ia a deferred TBP, c->expr1 will be set. */
3781 if (!c->expr1 && csym)
3782 {
3783 if (csym->attr.abstract)
3784 {
3785 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3786 csym->name, &c->loc);
3787 return false;
3788 }
3789
3790 /* Subroutines without the RECURSIVE attribution are not allowed to
3791 call themselves. */
3792 if (is_illegal_recursion (csym, gfc_current_ns))
3793 {
3794 if (csym->attr.entry && csym->ns->entries)
3795 gfc_error ("ENTRY %qs at %L cannot be called recursively, "
3796 "as subroutine %qs is not RECURSIVE",
3797 csym->name, &c->loc, csym->ns->entries->sym->name);
3798 else
3799 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
3800 "as it is not RECURSIVE", csym->name, &c->loc);
3801
3802 t = false;
3803 }
3804 }
3805
3806 /* Switch off assumed size checking and do this again for certain kinds
3807 of procedure, once the procedure itself is resolved. */
3808 need_full_assumed_size++;
3809
3810 if (csym)
3811 ptype = csym->attr.proc;
3812
3813 no_formal_args = csym && is_external_proc (csym)
3814 && gfc_sym_get_dummy_args (csym) == NULL;
3815 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3816 return false;
3817
3818 /* Resume assumed_size checking. */
3819 need_full_assumed_size--;
3820
3821 /* If external, check for usage. */
3822 if (csym && is_external_proc (csym))
3823 resolve_global_procedure (csym, &c->loc, 1);
3824
3825 t = true;
3826 if (c->resolved_sym == NULL)
3827 {
3828 c->resolved_isym = NULL;
3829 switch (procedure_kind (csym))
3830 {
3831 case PTYPE_GENERIC:
3832 t = resolve_generic_s (c);
3833 break;
3834
3835 case PTYPE_SPECIFIC:
3836 t = resolve_specific_s (c);
3837 break;
3838
3839 case PTYPE_UNKNOWN:
3840 t = resolve_unknown_s (c);
3841 break;
3842
3843 default:
3844 gfc_internal_error ("resolve_subroutine(): bad function type");
3845 }
3846 }
3847
3848 /* Some checks of elemental subroutine actual arguments. */
3849 if (!resolve_elemental_actual (NULL, c))
3850 return false;
3851
3852 if (!c->expr1)
3853 update_current_proc_array_outer_dependency (csym);
3854 else
3855 /* Typebound procedure: Assume the worst. */
3856 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3857
3858 if (c->resolved_sym
3859 && c->resolved_sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
3860 gfc_warning (OPT_Wdeprecated_declarations,
3861 "Using subroutine %qs at %L is deprecated",
3862 c->resolved_sym->name, &c->loc);
3863
3864 return t;
3865 }
3866
3867
3868 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3869 op1->shape and op2->shape are non-NULL return true if their shapes
3870 match. If both op1->shape and op2->shape are non-NULL return false
3871 if their shapes do not match. If either op1->shape or op2->shape is
3872 NULL, return true. */
3873
3874 static bool
3875 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3876 {
3877 bool t;
3878 int i;
3879
3880 t = true;
3881
3882 if (op1->shape != NULL && op2->shape != NULL)
3883 {
3884 for (i = 0; i < op1->rank; i++)
3885 {
3886 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3887 {
3888 gfc_error ("Shapes for operands at %L and %L are not conformable",
3889 &op1->where, &op2->where);
3890 t = false;
3891 break;
3892 }
3893 }
3894 }
3895
3896 return t;
3897 }
3898
3899 /* Convert a logical operator to the corresponding bitwise intrinsic call.
3900 For example A .AND. B becomes IAND(A, B). */
3901 static gfc_expr *
3902 logical_to_bitwise (gfc_expr *e)
3903 {
3904 gfc_expr *tmp, *op1, *op2;
3905 gfc_isym_id isym;
3906 gfc_actual_arglist *args = NULL;
3907
3908 gcc_assert (e->expr_type == EXPR_OP);
3909
3910 isym = GFC_ISYM_NONE;
3911 op1 = e->value.op.op1;
3912 op2 = e->value.op.op2;
3913
3914 switch (e->value.op.op)
3915 {
3916 case INTRINSIC_NOT:
3917 isym = GFC_ISYM_NOT;
3918 break;
3919 case INTRINSIC_AND:
3920 isym = GFC_ISYM_IAND;
3921 break;
3922 case INTRINSIC_OR:
3923 isym = GFC_ISYM_IOR;
3924 break;
3925 case INTRINSIC_NEQV:
3926 isym = GFC_ISYM_IEOR;
3927 break;
3928 case INTRINSIC_EQV:
3929 /* "Bitwise eqv" is just the complement of NEQV === IEOR.
3930 Change the old expression to NEQV, which will get replaced by IEOR,
3931 and wrap it in NOT. */
3932 tmp = gfc_copy_expr (e);
3933 tmp->value.op.op = INTRINSIC_NEQV;
3934 tmp = logical_to_bitwise (tmp);
3935 isym = GFC_ISYM_NOT;
3936 op1 = tmp;
3937 op2 = NULL;
3938 break;
3939 default:
3940 gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
3941 }
3942
3943 /* Inherit the original operation's operands as arguments. */
3944 args = gfc_get_actual_arglist ();
3945 args->expr = op1;
3946 if (op2)
3947 {
3948 args->next = gfc_get_actual_arglist ();
3949 args->next->expr = op2;
3950 }
3951
3952 /* Convert the expression to a function call. */
3953 e->expr_type = EXPR_FUNCTION;
3954 e->value.function.actual = args;
3955 e->value.function.isym = gfc_intrinsic_function_by_id (isym);
3956 e->value.function.name = e->value.function.isym->name;
3957 e->value.function.esym = NULL;
3958
3959 /* Make up a pre-resolved function call symtree if we need to. */
3960 if (!e->symtree || !e->symtree->n.sym)
3961 {
3962 gfc_symbol *sym;
3963 gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
3964 sym = e->symtree->n.sym;
3965 sym->result = sym;
3966 sym->attr.flavor = FL_PROCEDURE;
3967 sym->attr.function = 1;
3968 sym->attr.elemental = 1;
3969 sym->attr.pure = 1;
3970 sym->attr.referenced = 1;
3971 gfc_intrinsic_symbol (sym);
3972 gfc_commit_symbol (sym);
3973 }
3974
3975 args->name = e->value.function.isym->formal->name;
3976 if (e->value.function.isym->formal->next)
3977 args->next->name = e->value.function.isym->formal->next->name;
3978
3979 return e;
3980 }
3981
3982 /* Recursively append candidate UOP to CANDIDATES. Store the number of
3983 candidates in CANDIDATES_LEN. */
3984 static void
3985 lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
3986 char **&candidates,
3987 size_t &candidates_len)
3988 {
3989 gfc_symtree *p;
3990
3991 if (uop == NULL)
3992 return;
3993
3994 /* Not sure how to properly filter here. Use all for a start.
3995 n.uop.op is NULL for empty interface operators (is that legal?) disregard
3996 these as i suppose they don't make terribly sense. */
3997
3998 if (uop->n.uop->op != NULL)
3999 vec_push (candidates, candidates_len, uop->name);
4000
4001 p = uop->left;
4002 if (p)
4003 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
4004
4005 p = uop->right;
4006 if (p)
4007 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
4008 }
4009
4010 /* Lookup user-operator OP fuzzily, taking names in UOP into account. */
4011
4012 static const char*
4013 lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
4014 {
4015 char **candidates = NULL;
4016 size_t candidates_len = 0;
4017 lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
4018 return gfc_closest_fuzzy_match (op, candidates);
4019 }
4020
4021
4022 /* Callback finding an impure function as an operand to an .and. or
4023 .or. expression. Remember the last function warned about to
4024 avoid double warnings when recursing. */
4025
4026 static int
4027 impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
4028 void *data)
4029 {
4030 gfc_expr *f = *e;
4031 const char *name;
4032 static gfc_expr *last = NULL;
4033 bool *found = (bool *) data;
4034
4035 if (f->expr_type == EXPR_FUNCTION)
4036 {
4037 *found = 1;
4038 if (f != last && !gfc_pure_function (f, &name)
4039 && !gfc_implicit_pure_function (f))
4040 {
4041 if (name)
4042 gfc_warning (OPT_Wfunction_elimination,
4043 "Impure function %qs at %L might not be evaluated",
4044 name, &f->where);
4045 else
4046 gfc_warning (OPT_Wfunction_elimination,
4047 "Impure function at %L might not be evaluated",
4048 &f->where);
4049 }
4050 last = f;
4051 }
4052
4053 return 0;
4054 }
4055
4056 /* Return true if TYPE is character based, false otherwise. */
4057
4058 static int
4059 is_character_based (bt type)
4060 {
4061 return type == BT_CHARACTER || type == BT_HOLLERITH;
4062 }
4063
4064
4065 /* If expression is a hollerith, convert it to character and issue a warning
4066 for the conversion. */
4067
4068 static void
4069 convert_hollerith_to_character (gfc_expr *e)
4070 {
4071 if (e->ts.type == BT_HOLLERITH)
4072 {
4073 gfc_typespec t;
4074 gfc_clear_ts (&t);
4075 t.type = BT_CHARACTER;
4076 t.kind = e->ts.kind;
4077 gfc_convert_type_warn (e, &t, 2, 1);
4078 }
4079 }
4080
4081 /* Convert to numeric and issue a warning for the conversion. */
4082
4083 static void
4084 convert_to_numeric (gfc_expr *a, gfc_expr *b)
4085 {
4086 gfc_typespec t;
4087 gfc_clear_ts (&t);
4088 t.type = b->ts.type;
4089 t.kind = b->ts.kind;
4090 gfc_convert_type_warn (a, &t, 2, 1);
4091 }
4092
4093 /* Resolve an operator expression node. This can involve replacing the
4094 operation with a user defined function call. */
4095
4096 static bool
4097 resolve_operator (gfc_expr *e)
4098 {
4099 gfc_expr *op1, *op2;
4100 /* One error uses 3 names; additional space for wording (also via gettext). */
4101 char msg[3*GFC_MAX_SYMBOL_LEN + 1 + 50];
4102 bool dual_locus_error;
4103 bool t = true;
4104
4105 /* Resolve all subnodes-- give them types. */
4106
4107 switch (e->value.op.op)
4108 {
4109 default:
4110 if (!gfc_resolve_expr (e->value.op.op2))
4111 t = false;
4112
4113 /* Fall through. */
4114
4115 case INTRINSIC_NOT:
4116 case INTRINSIC_UPLUS:
4117 case INTRINSIC_UMINUS:
4118 case INTRINSIC_PARENTHESES:
4119 if (!gfc_resolve_expr (e->value.op.op1))
4120 return false;
4121 if (e->value.op.op1
4122 && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
4123 {
4124 gfc_error ("BOZ literal constant at %L cannot be an operand of "
4125 "unary operator %qs", &e->value.op.op1->where,
4126 gfc_op2string (e->value.op.op));
4127 return false;
4128 }
4129 break;
4130 }
4131
4132 /* Typecheck the new node. */
4133
4134 op1 = e->value.op.op1;
4135 op2 = e->value.op.op2;
4136 if (op1 == NULL && op2 == NULL)
4137 return false;
4138 /* Error out if op2 did not resolve. We already diagnosed op1. */
4139 if (t == false)
4140 return false;
4141
4142 dual_locus_error = false;
4143
4144 /* op1 and op2 cannot both be BOZ. */
4145 if (op1 && op1->ts.type == BT_BOZ
4146 && op2 && op2->ts.type == BT_BOZ)
4147 {
4148 gfc_error ("Operands at %L and %L cannot appear as operands of "
4149 "binary operator %qs", &op1->where, &op2->where,
4150 gfc_op2string (e->value.op.op));
4151 return false;
4152 }
4153
4154 if ((op1 && op1->expr_type == EXPR_NULL)
4155 || (op2 && op2->expr_type == EXPR_NULL))
4156 {
4157 snprintf (msg, sizeof (msg),
4158 _("Invalid context for NULL() pointer at %%L"));
4159 goto bad_op;
4160 }
4161
4162 switch (e->value.op.op)
4163 {
4164 case INTRINSIC_UPLUS:
4165 case INTRINSIC_UMINUS:
4166 if (op1->ts.type == BT_INTEGER
4167 || op1->ts.type == BT_REAL
4168 || op1->ts.type == BT_COMPLEX)
4169 {
4170 e->ts = op1->ts;
4171 break;
4172 }
4173
4174 snprintf (msg, sizeof (msg),
4175 _("Operand of unary numeric operator %%<%s%%> at %%L is %s"),
4176 gfc_op2string (e->value.op.op), gfc_typename (e));
4177 goto bad_op;
4178
4179 case INTRINSIC_PLUS:
4180 case INTRINSIC_MINUS:
4181 case INTRINSIC_TIMES:
4182 case INTRINSIC_DIVIDE:
4183 case INTRINSIC_POWER:
4184 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4185 {
4186 gfc_type_convert_binary (e, 1);
4187 break;
4188 }
4189
4190 if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
4191 snprintf (msg, sizeof (msg),
4192 _("Unexpected derived-type entities in binary intrinsic "
4193 "numeric operator %%<%s%%> at %%L"),
4194 gfc_op2string (e->value.op.op));
4195 else
4196 snprintf (msg, sizeof(msg),
4197 _("Operands of binary numeric operator %%<%s%%> at %%L are %s/%s"),
4198 gfc_op2string (e->value.op.op), gfc_typename (op1),
4199 gfc_typename (op2));
4200 goto bad_op;
4201
4202 case INTRINSIC_CONCAT:
4203 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4204 && op1->ts.kind == op2->ts.kind)
4205 {
4206 e->ts.type = BT_CHARACTER;
4207 e->ts.kind = op1->ts.kind;
4208 break;
4209 }
4210
4211 snprintf (msg, sizeof (msg),
4212 _("Operands of string concatenation operator at %%L are %s/%s"),
4213 gfc_typename (op1), gfc_typename (op2));
4214 goto bad_op;
4215
4216 case INTRINSIC_AND:
4217 case INTRINSIC_OR:
4218 case INTRINSIC_EQV:
4219 case INTRINSIC_NEQV:
4220 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4221 {
4222 e->ts.type = BT_LOGICAL;
4223 e->ts.kind = gfc_kind_max (op1, op2);
4224 if (op1->ts.kind < e->ts.kind)
4225 gfc_convert_type (op1, &e->ts, 2);
4226 else if (op2->ts.kind < e->ts.kind)
4227 gfc_convert_type (op2, &e->ts, 2);
4228
4229 if (flag_frontend_optimize &&
4230 (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
4231 {
4232 /* Warn about short-circuiting
4233 with impure function as second operand. */
4234 bool op2_f = false;
4235 gfc_expr_walker (&op2, impure_function_callback, &op2_f);
4236 }
4237 break;
4238 }
4239
4240 /* Logical ops on integers become bitwise ops with -fdec. */
4241 else if (flag_dec
4242 && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
4243 {
4244 e->ts.type = BT_INTEGER;
4245 e->ts.kind = gfc_kind_max (op1, op2);
4246 if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
4247 gfc_convert_type (op1, &e->ts, 1);
4248 if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
4249 gfc_convert_type (op2, &e->ts, 1);
4250 e = logical_to_bitwise (e);
4251 goto simplify_op;
4252 }
4253
4254 snprintf (msg, sizeof (msg),
4255 _("Operands of logical operator %%<%s%%> at %%L are %s/%s"),
4256 gfc_op2string (e->value.op.op), gfc_typename (op1),
4257 gfc_typename (op2));
4258
4259 goto bad_op;
4260
4261 case INTRINSIC_NOT:
4262 /* Logical ops on integers become bitwise ops with -fdec. */
4263 if (flag_dec && op1->ts.type == BT_INTEGER)
4264 {
4265 e->ts.type = BT_INTEGER;
4266 e->ts.kind = op1->ts.kind;
4267 e = logical_to_bitwise (e);
4268 goto simplify_op;
4269 }
4270
4271 if (op1->ts.type == BT_LOGICAL)
4272 {
4273 e->ts.type = BT_LOGICAL;
4274 e->ts.kind = op1->ts.kind;
4275 break;
4276 }
4277
4278 snprintf (msg, sizeof (msg), _("Operand of .not. operator at %%L is %s"),
4279 gfc_typename (op1));
4280 goto bad_op;
4281
4282 case INTRINSIC_GT:
4283 case INTRINSIC_GT_OS:
4284 case INTRINSIC_GE:
4285 case INTRINSIC_GE_OS:
4286 case INTRINSIC_LT:
4287 case INTRINSIC_LT_OS:
4288 case INTRINSIC_LE:
4289 case INTRINSIC_LE_OS:
4290 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
4291 {
4292 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
4293 goto bad_op;
4294 }
4295
4296 /* Fall through. */
4297
4298 case INTRINSIC_EQ:
4299 case INTRINSIC_EQ_OS:
4300 case INTRINSIC_NE:
4301 case INTRINSIC_NE_OS:
4302
4303 if (flag_dec
4304 && is_character_based (op1->ts.type)
4305 && is_character_based (op2->ts.type))
4306 {
4307 convert_hollerith_to_character (op1);
4308 convert_hollerith_to_character (op2);
4309 }
4310
4311 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4312 && op1->ts.kind == op2->ts.kind)
4313 {
4314 e->ts.type = BT_LOGICAL;
4315 e->ts.kind = gfc_default_logical_kind;
4316 break;
4317 }
4318
4319 /* If op1 is BOZ, then op2 is not!. Try to convert to type of op2. */
4320 if (op1->ts.type == BT_BOZ)
4321 {
4322 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
4323 "as an operand of a relational operator"),
4324 &op1->where))
4325 return false;
4326
4327 if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
4328 return false;
4329
4330 if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
4331 return false;
4332 }
4333
4334 /* If op2 is BOZ, then op1 is not!. Try to convert to type of op2. */
4335 if (op2->ts.type == BT_BOZ)
4336 {
4337 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
4338 " as an operand of a relational operator"),
4339 &op2->where))
4340 return false;
4341
4342 if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
4343 return false;
4344
4345 if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
4346 return false;
4347 }
4348 if (flag_dec
4349 && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
4350 convert_to_numeric (op1, op2);
4351
4352 if (flag_dec
4353 && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
4354 convert_to_numeric (op2, op1);
4355
4356 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4357 {
4358 gfc_type_convert_binary (e, 1);
4359
4360 e->ts.type = BT_LOGICAL;
4361 e->ts.kind = gfc_default_logical_kind;
4362
4363 if (warn_compare_reals)
4364 {
4365 gfc_intrinsic_op op = e->value.op.op;
4366
4367 /* Type conversion has made sure that the types of op1 and op2
4368 agree, so it is only necessary to check the first one. */
4369 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
4370 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
4371 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
4372 {
4373 const char *msg;
4374
4375 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
4376 msg = G_("Equality comparison for %s at %L");
4377 else
4378 msg = G_("Inequality comparison for %s at %L");
4379
4380 gfc_warning (OPT_Wcompare_reals, msg,
4381 gfc_typename (op1), &op1->where);
4382 }
4383 }
4384
4385 break;
4386 }
4387
4388 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4389 snprintf (msg, sizeof (msg),
4390 _("Logicals at %%L must be compared with %s instead of %s"),
4391 (e->value.op.op == INTRINSIC_EQ
4392 || e->value.op.op == INTRINSIC_EQ_OS)
4393 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
4394 else
4395 snprintf (msg, sizeof (msg),
4396 _("Operands of comparison operator %%<%s%%> at %%L are %s/%s"),
4397 gfc_op2string (e->value.op.op), gfc_typename (op1),
4398 gfc_typename (op2));
4399
4400 goto bad_op;
4401
4402 case INTRINSIC_USER:
4403 if (e->value.op.uop->op == NULL)
4404 {
4405 const char *name = e->value.op.uop->name;
4406 const char *guessed;
4407 guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
4408 if (guessed)
4409 snprintf (msg, sizeof (msg),
4410 _("Unknown operator %%<%s%%> at %%L; did you mean "
4411 "%%<%s%%>?"), name, guessed);
4412 else
4413 snprintf (msg, sizeof (msg), _("Unknown operator %%<%s%%> at %%L"),
4414 name);
4415 }
4416 else if (op2 == NULL)
4417 snprintf (msg, sizeof (msg),
4418 _("Operand of user operator %%<%s%%> at %%L is %s"),
4419 e->value.op.uop->name, gfc_typename (op1));
4420 else
4421 {
4422 snprintf (msg, sizeof (msg),
4423 _("Operands of user operator %%<%s%%> at %%L are %s/%s"),
4424 e->value.op.uop->name, gfc_typename (op1),
4425 gfc_typename (op2));
4426 e->value.op.uop->op->sym->attr.referenced = 1;
4427 }
4428
4429 goto bad_op;
4430
4431 case INTRINSIC_PARENTHESES:
4432 e->ts = op1->ts;
4433 if (e->ts.type == BT_CHARACTER)
4434 e->ts.u.cl = op1->ts.u.cl;
4435 break;
4436
4437 default:
4438 gfc_internal_error ("resolve_operator(): Bad intrinsic");
4439 }
4440
4441 /* Deal with arrayness of an operand through an operator. */
4442
4443 switch (e->value.op.op)
4444 {
4445 case INTRINSIC_PLUS:
4446 case INTRINSIC_MINUS:
4447 case INTRINSIC_TIMES:
4448 case INTRINSIC_DIVIDE:
4449 case INTRINSIC_POWER:
4450 case INTRINSIC_CONCAT:
4451 case INTRINSIC_AND:
4452 case INTRINSIC_OR:
4453 case INTRINSIC_EQV:
4454 case INTRINSIC_NEQV:
4455 case INTRINSIC_EQ:
4456 case INTRINSIC_EQ_OS:
4457 case INTRINSIC_NE:
4458 case INTRINSIC_NE_OS:
4459 case INTRINSIC_GT:
4460 case INTRINSIC_GT_OS:
4461 case INTRINSIC_GE:
4462 case INTRINSIC_GE_OS:
4463 case INTRINSIC_LT:
4464 case INTRINSIC_LT_OS:
4465 case INTRINSIC_LE:
4466 case INTRINSIC_LE_OS:
4467
4468 if (op1->rank == 0 && op2->rank == 0)
4469 e->rank = 0;
4470
4471 if (op1->rank == 0 && op2->rank != 0)
4472 {
4473 e->rank = op2->rank;
4474
4475 if (e->shape == NULL)
4476 e->shape = gfc_copy_shape (op2->shape, op2->rank);
4477 }
4478
4479 if (op1->rank != 0 && op2->rank == 0)
4480 {
4481 e->rank = op1->rank;
4482
4483 if (e->shape == NULL)
4484 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4485 }
4486
4487 if (op1->rank != 0 && op2->rank != 0)
4488 {
4489 if (op1->rank == op2->rank)
4490 {
4491 e->rank = op1->rank;
4492 if (e->shape == NULL)
4493 {
4494 t = compare_shapes (op1, op2);
4495 if (!t)
4496 e->shape = NULL;
4497 else
4498 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4499 }
4500 }
4501 else
4502 {
4503 /* Allow higher level expressions to work. */
4504 e->rank = 0;
4505
4506 /* Try user-defined operators, and otherwise throw an error. */
4507 dual_locus_error = true;
4508 snprintf (msg, sizeof (msg),
4509 _("Inconsistent ranks for operator at %%L and %%L"));
4510 goto bad_op;
4511 }
4512 }
4513
4514 break;
4515
4516 case INTRINSIC_PARENTHESES:
4517 case INTRINSIC_NOT:
4518 case INTRINSIC_UPLUS:
4519 case INTRINSIC_UMINUS:
4520 /* Simply copy arrayness attribute */
4521 e->rank = op1->rank;
4522
4523 if (e->shape == NULL)
4524 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4525
4526 break;
4527
4528 default:
4529 break;
4530 }
4531
4532 simplify_op:
4533
4534 /* Attempt to simplify the expression. */
4535 if (t)
4536 {
4537 t = gfc_simplify_expr (e, 0);
4538 /* Some calls do not succeed in simplification and return false
4539 even though there is no error; e.g. variable references to
4540 PARAMETER arrays. */
4541 if (!gfc_is_constant_expr (e))
4542 t = true;
4543 }
4544 return t;
4545
4546 bad_op:
4547
4548 {
4549 match m = gfc_extend_expr (e);
4550 if (m == MATCH_YES)
4551 return true;
4552 if (m == MATCH_ERROR)
4553 return false;
4554 }
4555
4556 if (dual_locus_error)
4557 gfc_error (msg, &op1->where, &op2->where);
4558 else
4559 gfc_error (msg, &e->where);
4560
4561 return false;
4562 }
4563
4564
4565 /************** Array resolution subroutines **************/
4566
4567 enum compare_result
4568 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
4569
4570 /* Compare two integer expressions. */
4571
4572 static compare_result
4573 compare_bound (gfc_expr *a, gfc_expr *b)
4574 {
4575 int i;
4576
4577 if (a == NULL || a->expr_type != EXPR_CONSTANT
4578 || b == NULL || b->expr_type != EXPR_CONSTANT)
4579 return CMP_UNKNOWN;
4580
4581 /* If either of the types isn't INTEGER, we must have
4582 raised an error earlier. */
4583
4584 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
4585 return CMP_UNKNOWN;
4586
4587 i = mpz_cmp (a->value.integer, b->value.integer);
4588
4589 if (i < 0)
4590 return CMP_LT;
4591 if (i > 0)
4592 return CMP_GT;
4593 return CMP_EQ;
4594 }
4595
4596
4597 /* Compare an integer expression with an integer. */
4598
4599 static compare_result
4600 compare_bound_int (gfc_expr *a, int b)
4601 {
4602 int i;
4603
4604 if (a == NULL
4605 || a->expr_type != EXPR_CONSTANT
4606 || a->ts.type != BT_INTEGER)
4607 return CMP_UNKNOWN;
4608
4609 i = mpz_cmp_si (a->value.integer, b);
4610
4611 if (i < 0)
4612 return CMP_LT;
4613 if (i > 0)
4614 return CMP_GT;
4615 return CMP_EQ;
4616 }
4617
4618
4619 /* Compare an integer expression with a mpz_t. */
4620
4621 static compare_result
4622 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
4623 {
4624 int i;
4625
4626 if (a == NULL
4627 || a->expr_type != EXPR_CONSTANT
4628 || a->ts.type != BT_INTEGER)
4629 return CMP_UNKNOWN;
4630
4631 i = mpz_cmp (a->value.integer, b);
4632
4633 if (i < 0)
4634 return CMP_LT;
4635 if (i > 0)
4636 return CMP_GT;
4637 return CMP_EQ;
4638 }
4639
4640
4641 /* Compute the last value of a sequence given by a triplet.
4642 Return 0 if it wasn't able to compute the last value, or if the
4643 sequence if empty, and 1 otherwise. */
4644
4645 static int
4646 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4647 gfc_expr *stride, mpz_t last)
4648 {
4649 mpz_t rem;
4650
4651 if (start == NULL || start->expr_type != EXPR_CONSTANT
4652 || end == NULL || end->expr_type != EXPR_CONSTANT
4653 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4654 return 0;
4655
4656 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4657 || (stride != NULL && stride->ts.type != BT_INTEGER))
4658 return 0;
4659
4660 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
4661 {
4662 if (compare_bound (start, end) == CMP_GT)
4663 return 0;
4664 mpz_set (last, end->value.integer);
4665 return 1;
4666 }
4667
4668 if (compare_bound_int (stride, 0) == CMP_GT)
4669 {
4670 /* Stride is positive */
4671 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4672 return 0;
4673 }
4674 else
4675 {
4676 /* Stride is negative */
4677 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4678 return 0;
4679 }
4680
4681 mpz_init (rem);
4682 mpz_sub (rem, end->value.integer, start->value.integer);
4683 mpz_tdiv_r (rem, rem, stride->value.integer);
4684 mpz_sub (last, end->value.integer, rem);
4685 mpz_clear (rem);
4686
4687 return 1;
4688 }
4689
4690
4691 /* Compare a single dimension of an array reference to the array
4692 specification. */
4693
4694 static bool
4695 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4696 {
4697 mpz_t last_value;
4698
4699 if (ar->dimen_type[i] == DIMEN_STAR)
4700 {
4701 gcc_assert (ar->stride[i] == NULL);
4702 /* This implies [*] as [*:] and [*:3] are not possible. */
4703 if (ar->start[i] == NULL)
4704 {
4705 gcc_assert (ar->end[i] == NULL);
4706 return true;
4707 }
4708 }
4709
4710 /* Given start, end and stride values, calculate the minimum and
4711 maximum referenced indexes. */
4712
4713 switch (ar->dimen_type[i])
4714 {
4715 case DIMEN_VECTOR:
4716 case DIMEN_THIS_IMAGE:
4717 break;
4718
4719 case DIMEN_STAR:
4720 case DIMEN_ELEMENT:
4721 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4722 {
4723 if (i < as->rank)
4724 gfc_warning (0, "Array reference at %L is out of bounds "
4725 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4726 mpz_get_si (ar->start[i]->value.integer),
4727 mpz_get_si (as->lower[i]->value.integer), i+1);
4728 else
4729 gfc_warning (0, "Array reference at %L is out of bounds "
4730 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4731 mpz_get_si (ar->start[i]->value.integer),
4732 mpz_get_si (as->lower[i]->value.integer),
4733 i + 1 - as->rank);
4734 return true;
4735 }
4736 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4737 {
4738 if (i < as->rank)
4739 gfc_warning (0, "Array reference at %L is out of bounds "
4740 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4741 mpz_get_si (ar->start[i]->value.integer),
4742 mpz_get_si (as->upper[i]->value.integer), i+1);
4743 else
4744 gfc_warning (0, "Array reference at %L is out of bounds "
4745 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4746 mpz_get_si (ar->start[i]->value.integer),
4747 mpz_get_si (as->upper[i]->value.integer),
4748 i + 1 - as->rank);
4749 return true;
4750 }
4751
4752 break;
4753
4754 case DIMEN_RANGE:
4755 {
4756 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4757 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4758
4759 compare_result comp_start_end = compare_bound (AR_START, AR_END);
4760 compare_result comp_stride_zero = compare_bound_int (ar->stride[i], 0);
4761
4762 /* Check for zero stride, which is not allowed. */
4763 if (comp_stride_zero == CMP_EQ)
4764 {
4765 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4766 return false;
4767 }
4768
4769 /* if start == end || (stride > 0 && start < end)
4770 || (stride < 0 && start > end),
4771 then the array section contains at least one element. In this
4772 case, there is an out-of-bounds access if
4773 (start < lower || start > upper). */
4774 if (comp_start_end == CMP_EQ
4775 || ((comp_stride_zero == CMP_GT || ar->stride[i] == NULL)
4776 && comp_start_end == CMP_LT)
4777 || (comp_stride_zero == CMP_LT
4778 && comp_start_end == CMP_GT))
4779 {
4780 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4781 {
4782 gfc_warning (0, "Lower array reference at %L is out of bounds "
4783 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4784 mpz_get_si (AR_START->value.integer),
4785 mpz_get_si (as->lower[i]->value.integer), i+1);
4786 return true;
4787 }
4788 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4789 {
4790 gfc_warning (0, "Lower array reference at %L is out of bounds "
4791 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4792 mpz_get_si (AR_START->value.integer),
4793 mpz_get_si (as->upper[i]->value.integer), i+1);
4794 return true;
4795 }
4796 }
4797
4798 /* If we can compute the highest index of the array section,
4799 then it also has to be between lower and upper. */
4800 mpz_init (last_value);
4801 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4802 last_value))
4803 {
4804 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4805 {
4806 gfc_warning (0, "Upper array reference at %L is out of bounds "
4807 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4808 mpz_get_si (last_value),
4809 mpz_get_si (as->lower[i]->value.integer), i+1);
4810 mpz_clear (last_value);
4811 return true;
4812 }
4813 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4814 {
4815 gfc_warning (0, "Upper array reference at %L is out of bounds "
4816 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4817 mpz_get_si (last_value),
4818 mpz_get_si (as->upper[i]->value.integer), i+1);
4819 mpz_clear (last_value);
4820 return true;
4821 }
4822 }
4823 mpz_clear (last_value);
4824
4825 #undef AR_START
4826 #undef AR_END
4827 }
4828 break;
4829
4830 default:
4831 gfc_internal_error ("check_dimension(): Bad array reference");
4832 }
4833
4834 return true;
4835 }
4836
4837
4838 /* Compare an array reference with an array specification. */
4839
4840 static bool
4841 compare_spec_to_ref (gfc_array_ref *ar)
4842 {
4843 gfc_array_spec *as;
4844 int i;
4845
4846 as = ar->as;
4847 i = as->rank - 1;
4848 /* TODO: Full array sections are only allowed as actual parameters. */
4849 if (as->type == AS_ASSUMED_SIZE
4850 && (/*ar->type == AR_FULL
4851 ||*/ (ar->type == AR_SECTION
4852 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4853 {
4854 gfc_error ("Rightmost upper bound of assumed size array section "
4855 "not specified at %L", &ar->where);
4856 return false;
4857 }
4858
4859 if (ar->type == AR_FULL)
4860 return true;
4861
4862 if (as->rank != ar->dimen)
4863 {
4864 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4865 &ar->where, ar->dimen, as->rank);
4866 return false;
4867 }
4868
4869 /* ar->codimen == 0 is a local array. */
4870 if (as->corank != ar->codimen && ar->codimen != 0)
4871 {
4872 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4873 &ar->where, ar->codimen, as->corank);
4874 return false;
4875 }
4876
4877 for (i = 0; i < as->rank; i++)
4878 if (!check_dimension (i, ar, as))
4879 return false;
4880
4881 /* Local access has no coarray spec. */
4882 if (ar->codimen != 0)
4883 for (i = as->rank; i < as->rank + as->corank; i++)
4884 {
4885 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4886 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4887 {
4888 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4889 i + 1 - as->rank, &ar->where);
4890 return false;
4891 }
4892 if (!check_dimension (i, ar, as))
4893 return false;
4894 }
4895
4896 return true;
4897 }
4898
4899
4900 /* Resolve one part of an array index. */
4901
4902 static bool
4903 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4904 int force_index_integer_kind)
4905 {
4906 gfc_typespec ts;
4907
4908 if (index == NULL)
4909 return true;
4910
4911 if (!gfc_resolve_expr (index))
4912 return false;
4913
4914 if (check_scalar && index->rank != 0)
4915 {
4916 gfc_error ("Array index at %L must be scalar", &index->where);
4917 return false;
4918 }
4919
4920 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4921 {
4922 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4923 &index->where, gfc_basic_typename (index->ts.type));
4924 return false;
4925 }
4926
4927 if (index->ts.type == BT_REAL)
4928 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4929 &index->where))
4930 return false;
4931
4932 if ((index->ts.kind != gfc_index_integer_kind
4933 && force_index_integer_kind)
4934 || index->ts.type != BT_INTEGER)
4935 {
4936 gfc_clear_ts (&ts);
4937 ts.type = BT_INTEGER;
4938 ts.kind = gfc_index_integer_kind;
4939
4940 gfc_convert_type_warn (index, &ts, 2, 0);
4941 }
4942
4943 return true;
4944 }
4945
4946 /* Resolve one part of an array index. */
4947
4948 bool
4949 gfc_resolve_index (gfc_expr *index, int check_scalar)
4950 {
4951 return gfc_resolve_index_1 (index, check_scalar, 1);
4952 }
4953
4954 /* Resolve a dim argument to an intrinsic function. */
4955
4956 bool
4957 gfc_resolve_dim_arg (gfc_expr *dim)
4958 {
4959 if (dim == NULL)
4960 return true;
4961
4962 if (!gfc_resolve_expr (dim))
4963 return false;
4964
4965 if (dim->rank != 0)
4966 {
4967 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4968 return false;
4969
4970 }
4971
4972 if (dim->ts.type != BT_INTEGER)
4973 {
4974 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4975 return false;
4976 }
4977
4978 if (dim->ts.kind != gfc_index_integer_kind)
4979 {
4980 gfc_typespec ts;
4981
4982 gfc_clear_ts (&ts);
4983 ts.type = BT_INTEGER;
4984 ts.kind = gfc_index_integer_kind;
4985
4986 gfc_convert_type_warn (dim, &ts, 2, 0);
4987 }
4988
4989 return true;
4990 }
4991
4992 /* Given an expression that contains array references, update those array
4993 references to point to the right array specifications. While this is
4994 filled in during matching, this information is difficult to save and load
4995 in a module, so we take care of it here.
4996
4997 The idea here is that the original array reference comes from the
4998 base symbol. We traverse the list of reference structures, setting
4999 the stored reference to references. Component references can
5000 provide an additional array specification. */
5001 static void
5002 resolve_assoc_var (gfc_symbol* sym, bool resolve_target);
5003
5004 static bool
5005 find_array_spec (gfc_expr *e)
5006 {
5007 gfc_array_spec *as;
5008 gfc_component *c;
5009 gfc_ref *ref;
5010 bool class_as = false;
5011
5012 if (e->symtree->n.sym->assoc)
5013 {
5014 if (e->symtree->n.sym->assoc->target)
5015 gfc_resolve_expr (e->symtree->n.sym->assoc->target);
5016 resolve_assoc_var (e->symtree->n.sym, false);
5017 }
5018
5019 if (e->symtree->n.sym->ts.type == BT_CLASS)
5020 {
5021 as = CLASS_DATA (e->symtree->n.sym)->as;
5022 class_as = true;
5023 }
5024 else
5025 as = e->symtree->n.sym->as;
5026
5027 for (ref = e->ref; ref; ref = ref->next)
5028 switch (ref->type)
5029 {
5030 case REF_ARRAY:
5031 if (as == NULL)
5032 {
5033 locus loc = ref->u.ar.where.lb ? ref->u.ar.where : e->where;
5034 gfc_error ("Invalid array reference of a non-array entity at %L",
5035 &loc);
5036 return false;
5037 }
5038
5039 ref->u.ar.as = as;
5040 as = NULL;
5041 break;
5042
5043 case REF_COMPONENT:
5044 c = ref->u.c.component;
5045 if (c->attr.dimension)
5046 {
5047 if (as != NULL && !(class_as && as == c->as))
5048 gfc_internal_error ("find_array_spec(): unused as(1)");
5049 as = c->as;
5050 }
5051
5052 break;
5053
5054 case REF_SUBSTRING:
5055 case REF_INQUIRY:
5056 break;
5057 }
5058
5059 if (as != NULL)
5060 gfc_internal_error ("find_array_spec(): unused as(2)");
5061
5062 return true;
5063 }
5064
5065
5066 /* Resolve an array reference. */
5067
5068 static bool
5069 resolve_array_ref (gfc_array_ref *ar)
5070 {
5071 int i, check_scalar;
5072 gfc_expr *e;
5073
5074 for (i = 0; i < ar->dimen + ar->codimen; i++)
5075 {
5076 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
5077
5078 /* Do not force gfc_index_integer_kind for the start. We can
5079 do fine with any integer kind. This avoids temporary arrays
5080 created for indexing with a vector. */
5081 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
5082 return false;
5083 if (!gfc_resolve_index (ar->end[i], check_scalar))
5084 return false;
5085 if (!gfc_resolve_index (ar->stride[i], check_scalar))
5086 return false;
5087
5088 e = ar->start[i];
5089
5090 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
5091 switch (e->rank)
5092 {
5093 case 0:
5094 ar->dimen_type[i] = DIMEN_ELEMENT;
5095 break;
5096
5097 case 1:
5098 ar->dimen_type[i] = DIMEN_VECTOR;
5099 if (e->expr_type == EXPR_VARIABLE
5100 && e->symtree->n.sym->ts.type == BT_DERIVED)
5101 ar->start[i] = gfc_get_parentheses (e);
5102 break;
5103
5104 default:
5105 gfc_error ("Array index at %L is an array of rank %d",
5106 &ar->c_where[i], e->rank);
5107 return false;
5108 }
5109
5110 /* Fill in the upper bound, which may be lower than the
5111 specified one for something like a(2:10:5), which is
5112 identical to a(2:7:5). Only relevant for strides not equal
5113 to one. Don't try a division by zero. */
5114 if (ar->dimen_type[i] == DIMEN_RANGE
5115 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
5116 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
5117 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
5118 {
5119 mpz_t size, end;
5120
5121 if (gfc_ref_dimen_size (ar, i, &size, &end))
5122 {
5123 if (ar->end[i] == NULL)
5124 {
5125 ar->end[i] =
5126 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
5127 &ar->where);
5128 mpz_set (ar->end[i]->value.integer, end);
5129 }
5130 else if (ar->end[i]->ts.type == BT_INTEGER
5131 && ar->end[i]->expr_type == EXPR_CONSTANT)
5132 {
5133 mpz_set (ar->end[i]->value.integer, end);
5134 }
5135 else
5136 gcc_unreachable ();
5137
5138 mpz_clear (size);
5139 mpz_clear (end);
5140 }
5141 }
5142 }
5143
5144 if (ar->type == AR_FULL)
5145 {
5146 if (ar->as->rank == 0)
5147 ar->type = AR_ELEMENT;
5148
5149 /* Make sure array is the same as array(:,:), this way
5150 we don't need to special case all the time. */
5151 ar->dimen = ar->as->rank;
5152 for (i = 0; i < ar->dimen; i++)
5153 {
5154 ar->dimen_type[i] = DIMEN_RANGE;
5155
5156 gcc_assert (ar->start[i] == NULL);
5157 gcc_assert (ar->end[i] == NULL);
5158 gcc_assert (ar->stride[i] == NULL);
5159 }
5160 }
5161
5162 /* If the reference type is unknown, figure out what kind it is. */
5163
5164 if (ar->type == AR_UNKNOWN)
5165 {
5166 ar->type = AR_ELEMENT;
5167 for (i = 0; i < ar->dimen; i++)
5168 if (ar->dimen_type[i] == DIMEN_RANGE
5169 || ar->dimen_type[i] == DIMEN_VECTOR)
5170 {
5171 ar->type = AR_SECTION;
5172 break;
5173 }
5174 }
5175
5176 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
5177 return false;
5178
5179 if (ar->as->corank && ar->codimen == 0)
5180 {
5181 int n;
5182 ar->codimen = ar->as->corank;
5183 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
5184 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
5185 }
5186
5187 return true;
5188 }
5189
5190
5191 bool
5192 gfc_resolve_substring (gfc_ref *ref, bool *equal_length)
5193 {
5194 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
5195
5196 if (ref->u.ss.start != NULL)
5197 {
5198 if (!gfc_resolve_expr (ref->u.ss.start))
5199 return false;
5200
5201 if (ref->u.ss.start->ts.type != BT_INTEGER)
5202 {
5203 gfc_error ("Substring start index at %L must be of type INTEGER",
5204 &ref->u.ss.start->where);
5205 return false;
5206 }
5207
5208 if (ref->u.ss.start->rank != 0)
5209 {
5210 gfc_error ("Substring start index at %L must be scalar",
5211 &ref->u.ss.start->where);
5212 return false;
5213 }
5214
5215 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
5216 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5217 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5218 {
5219 gfc_error ("Substring start index at %L is less than one",
5220 &ref->u.ss.start->where);
5221 return false;
5222 }
5223 }
5224
5225 if (ref->u.ss.end != NULL)
5226 {
5227 if (!gfc_resolve_expr (ref->u.ss.end))
5228 return false;
5229
5230 if (ref->u.ss.end->ts.type != BT_INTEGER)
5231 {
5232 gfc_error ("Substring end index at %L must be of type INTEGER",
5233 &ref->u.ss.end->where);
5234 return false;
5235 }
5236
5237 if (ref->u.ss.end->rank != 0)
5238 {
5239 gfc_error ("Substring end index at %L must be scalar",
5240 &ref->u.ss.end->where);
5241 return false;
5242 }
5243
5244 if (ref->u.ss.length != NULL
5245 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
5246 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5247 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5248 {
5249 gfc_error ("Substring end index at %L exceeds the string length",
5250 &ref->u.ss.start->where);
5251 return false;
5252 }
5253
5254 if (compare_bound_mpz_t (ref->u.ss.end,
5255 gfc_integer_kinds[k].huge) == CMP_GT
5256 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5257 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5258 {
5259 gfc_error ("Substring end index at %L is too large",
5260 &ref->u.ss.end->where);
5261 return false;
5262 }
5263 /* If the substring has the same length as the original
5264 variable, the reference itself can be deleted. */
5265
5266 if (ref->u.ss.length != NULL
5267 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
5268 && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
5269 *equal_length = true;
5270 }
5271
5272 return true;
5273 }
5274
5275
5276 /* This function supplies missing substring charlens. */
5277
5278 void
5279 gfc_resolve_substring_charlen (gfc_expr *e)
5280 {
5281 gfc_ref *char_ref;
5282 gfc_expr *start, *end;
5283 gfc_typespec *ts = NULL;
5284 mpz_t diff;
5285
5286 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
5287 {
5288 if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
5289 break;
5290 if (char_ref->type == REF_COMPONENT)
5291 ts = &char_ref->u.c.component->ts;
5292 }
5293
5294 if (!char_ref || char_ref->type == REF_INQUIRY)
5295 return;
5296
5297 gcc_assert (char_ref->next == NULL);
5298
5299 if (e->ts.u.cl)
5300 {
5301 if (e->ts.u.cl->length)
5302 gfc_free_expr (e->ts.u.cl->length);
5303 else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
5304 return;
5305 }
5306
5307 if (!e->ts.u.cl)
5308 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5309
5310 if (char_ref->u.ss.start)
5311 start = gfc_copy_expr (char_ref->u.ss.start);
5312 else
5313 start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
5314
5315 if (char_ref->u.ss.end)
5316 end = gfc_copy_expr (char_ref->u.ss.end);
5317 else if (e->expr_type == EXPR_VARIABLE)
5318 {
5319 if (!ts)
5320 ts = &e->symtree->n.sym->ts;
5321 end = gfc_copy_expr (ts->u.cl->length);
5322 }
5323 else
5324 end = NULL;
5325
5326 if (!start || !end)
5327 {
5328 gfc_free_expr (start);
5329 gfc_free_expr (end);
5330 return;
5331 }
5332
5333 /* Length = (end - start + 1).
5334 Check first whether it has a constant length. */
5335 if (gfc_dep_difference (end, start, &diff))
5336 {
5337 gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
5338 &e->where);
5339
5340 mpz_add_ui (len->value.integer, diff, 1);
5341 mpz_clear (diff);
5342 e->ts.u.cl->length = len;
5343 /* The check for length < 0 is handled below */
5344 }
5345 else
5346 {
5347 e->ts.u.cl->length = gfc_subtract (end, start);
5348 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
5349 gfc_get_int_expr (gfc_charlen_int_kind,
5350 NULL, 1));
5351 }
5352
5353 /* F2008, 6.4.1: Both the starting point and the ending point shall
5354 be within the range 1, 2, ..., n unless the starting point exceeds
5355 the ending point, in which case the substring has length zero. */
5356
5357 if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
5358 mpz_set_si (e->ts.u.cl->length->value.integer, 0);
5359
5360 e->ts.u.cl->length->ts.type = BT_INTEGER;
5361 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5362
5363 /* Make sure that the length is simplified. */
5364 gfc_simplify_expr (e->ts.u.cl->length, 1);
5365 gfc_resolve_expr (e->ts.u.cl->length);
5366 }
5367
5368
5369 /* Resolve subtype references. */
5370
5371 bool
5372 gfc_resolve_ref (gfc_expr *expr)
5373 {
5374 int current_part_dimension, n_components, seen_part_dimension, dim;
5375 gfc_ref *ref, **prev, *array_ref;
5376 bool equal_length;
5377
5378 for (ref = expr->ref; ref; ref = ref->next)
5379 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
5380 {
5381 if (!find_array_spec (expr))
5382 return false;
5383 break;
5384 }
5385
5386 for (prev = &expr->ref; *prev != NULL;
5387 prev = *prev == NULL ? prev : &(*prev)->next)
5388 switch ((*prev)->type)
5389 {
5390 case REF_ARRAY:
5391 if (!resolve_array_ref (&(*prev)->u.ar))
5392 return false;
5393 break;
5394
5395 case REF_COMPONENT:
5396 case REF_INQUIRY:
5397 break;
5398
5399 case REF_SUBSTRING:
5400 equal_length = false;
5401 if (!gfc_resolve_substring (*prev, &equal_length))
5402 return false;
5403
5404 if (expr->expr_type != EXPR_SUBSTRING && equal_length)
5405 {
5406 /* Remove the reference and move the charlen, if any. */
5407 ref = *prev;
5408 *prev = ref->next;
5409 ref->next = NULL;
5410 expr->ts.u.cl = ref->u.ss.length;
5411 ref->u.ss.length = NULL;
5412 gfc_free_ref_list (ref);
5413 }
5414 break;
5415 }
5416
5417 /* Check constraints on part references. */
5418
5419 current_part_dimension = 0;
5420 seen_part_dimension = 0;
5421 n_components = 0;
5422 array_ref = NULL;
5423
5424 for (ref = expr->ref; ref; ref = ref->next)
5425 {
5426 switch (ref->type)
5427 {
5428 case REF_ARRAY:
5429 array_ref = ref;
5430 switch (ref->u.ar.type)
5431 {
5432 case AR_FULL:
5433 /* Coarray scalar. */
5434 if (ref->u.ar.as->rank == 0)
5435 {
5436 current_part_dimension = 0;
5437 break;
5438 }
5439 /* Fall through. */
5440 case AR_SECTION:
5441 current_part_dimension = 1;
5442 break;
5443
5444 case AR_ELEMENT:
5445 array_ref = NULL;
5446 current_part_dimension = 0;
5447 break;
5448
5449 case AR_UNKNOWN:
5450 gfc_internal_error ("resolve_ref(): Bad array reference");
5451 }
5452
5453 break;
5454
5455 case REF_COMPONENT:
5456 if (current_part_dimension || seen_part_dimension)
5457 {
5458 /* F03:C614. */
5459 if (ref->u.c.component->attr.pointer
5460 || ref->u.c.component->attr.proc_pointer
5461 || (ref->u.c.component->ts.type == BT_CLASS
5462 && CLASS_DATA (ref->u.c.component)->attr.pointer))
5463 {
5464 gfc_error ("Component to the right of a part reference "
5465 "with nonzero rank must not have the POINTER "
5466 "attribute at %L", &expr->where);
5467 return false;
5468 }
5469 else if (ref->u.c.component->attr.allocatable
5470 || (ref->u.c.component->ts.type == BT_CLASS
5471 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
5472
5473 {
5474 gfc_error ("Component to the right of a part reference "
5475 "with nonzero rank must not have the ALLOCATABLE "
5476 "attribute at %L", &expr->where);
5477 return false;
5478 }
5479 }
5480
5481 n_components++;
5482 break;
5483
5484 case REF_SUBSTRING:
5485 break;
5486
5487 case REF_INQUIRY:
5488 /* Implement requirement in note 9.7 of F2018 that the result of the
5489 LEN inquiry be a scalar. */
5490 if (ref->u.i == INQUIRY_LEN && array_ref && expr->ts.deferred)
5491 {
5492 array_ref->u.ar.type = AR_ELEMENT;
5493 expr->rank = 0;
5494 /* INQUIRY_LEN is not evaluated from the rest of the expr
5495 but directly from the string length. This means that setting
5496 the array indices to one does not matter but might trigger
5497 a runtime bounds error. Suppress the check. */
5498 expr->no_bounds_check = 1;
5499 for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
5500 {
5501 array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
5502 if (array_ref->u.ar.start[dim])
5503 gfc_free_expr (array_ref->u.ar.start[dim]);
5504 array_ref->u.ar.start[dim]
5505 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
5506 if (array_ref->u.ar.end[dim])
5507 gfc_free_expr (array_ref->u.ar.end[dim]);
5508 if (array_ref->u.ar.stride[dim])
5509 gfc_free_expr (array_ref->u.ar.stride[dim]);
5510 }
5511 }
5512 break;
5513 }
5514
5515 if (((ref->type == REF_COMPONENT && n_components > 1)
5516 || ref->next == NULL)
5517 && current_part_dimension
5518 && seen_part_dimension)
5519 {
5520 gfc_error ("Two or more part references with nonzero rank must "
5521 "not be specified at %L", &expr->where);
5522 return false;
5523 }
5524
5525 if (ref->type == REF_COMPONENT)
5526 {
5527 if (current_part_dimension)
5528 seen_part_dimension = 1;
5529
5530 /* reset to make sure */
5531 current_part_dimension = 0;
5532 }
5533 }
5534
5535 return true;
5536 }
5537
5538
5539 /* Given an expression, determine its shape. This is easier than it sounds.
5540 Leaves the shape array NULL if it is not possible to determine the shape. */
5541
5542 static void
5543 expression_shape (gfc_expr *e)
5544 {
5545 mpz_t array[GFC_MAX_DIMENSIONS];
5546 int i;
5547
5548 if (e->rank <= 0 || e->shape != NULL)
5549 return;
5550
5551 for (i = 0; i < e->rank; i++)
5552 if (!gfc_array_dimen_size (e, i, &array[i]))
5553 goto fail;
5554
5555 e->shape = gfc_get_shape (e->rank);
5556
5557 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
5558
5559 return;
5560
5561 fail:
5562 for (i--; i >= 0; i--)
5563 mpz_clear (array[i]);
5564 }
5565
5566
5567 /* Given a variable expression node, compute the rank of the expression by
5568 examining the base symbol and any reference structures it may have. */
5569
5570 void
5571 gfc_expression_rank (gfc_expr *e)
5572 {
5573 gfc_ref *ref;
5574 int i, rank;
5575
5576 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
5577 could lead to serious confusion... */
5578 gcc_assert (e->expr_type != EXPR_COMPCALL);
5579
5580 if (e->ref == NULL)
5581 {
5582 if (e->expr_type == EXPR_ARRAY)
5583 goto done;
5584 /* Constructors can have a rank different from one via RESHAPE(). */
5585
5586 e->rank = ((e->symtree == NULL || e->symtree->n.sym->as == NULL)
5587 ? 0 : e->symtree->n.sym->as->rank);
5588 goto done;
5589 }
5590
5591 rank = 0;
5592
5593 for (ref = e->ref; ref; ref = ref->next)
5594 {
5595 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
5596 && ref->u.c.component->attr.function && !ref->next)
5597 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
5598
5599 if (ref->type != REF_ARRAY)
5600 continue;
5601
5602 if (ref->u.ar.type == AR_FULL)
5603 {
5604 rank = ref->u.ar.as->rank;
5605 break;
5606 }
5607
5608 if (ref->u.ar.type == AR_SECTION)
5609 {
5610 /* Figure out the rank of the section. */
5611 if (rank != 0)
5612 gfc_internal_error ("gfc_expression_rank(): Two array specs");
5613
5614 for (i = 0; i < ref->u.ar.dimen; i++)
5615 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5616 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
5617 rank++;
5618
5619 break;
5620 }
5621 }
5622
5623 e->rank = rank;
5624
5625 done:
5626 expression_shape (e);
5627 }
5628
5629
5630 static void
5631 add_caf_get_intrinsic (gfc_expr *e)
5632 {
5633 gfc_expr *wrapper, *tmp_expr;
5634 gfc_ref *ref;
5635 int n;
5636
5637 for (ref = e->ref; ref; ref = ref->next)
5638 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5639 break;
5640 if (ref == NULL)
5641 return;
5642
5643 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
5644 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
5645 return;
5646
5647 tmp_expr = XCNEW (gfc_expr);
5648 *tmp_expr = *e;
5649 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
5650 "caf_get", tmp_expr->where, 1, tmp_expr);
5651 wrapper->ts = e->ts;
5652 wrapper->rank = e->rank;
5653 if (e->rank)
5654 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
5655 *e = *wrapper;
5656 free (wrapper);
5657 }
5658
5659
5660 static void
5661 remove_caf_get_intrinsic (gfc_expr *e)
5662 {
5663 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym
5664 && e->value.function.isym->id == GFC_ISYM_CAF_GET);
5665 gfc_expr *e2 = e->value.function.actual->expr;
5666 e->value.function.actual->expr = NULL;
5667 gfc_free_actual_arglist (e->value.function.actual);
5668 gfc_free_shape (&e->shape, e->rank);
5669 *e = *e2;
5670 free (e2);
5671 }
5672
5673
5674 /* Resolve a variable expression. */
5675
5676 static bool
5677 resolve_variable (gfc_expr *e)
5678 {
5679 gfc_symbol *sym;
5680 bool t;
5681
5682 t = true;
5683
5684 if (e->symtree == NULL)
5685 return false;
5686 sym = e->symtree->n.sym;
5687
5688 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
5689 as ts.type is set to BT_ASSUMED in resolve_symbol. */
5690 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
5691 {
5692 if (!actual_arg || inquiry_argument)
5693 {
5694 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
5695 "be used as actual argument", sym->name, &e->where);
5696 return false;
5697 }
5698 }
5699 /* TS 29113, 407b. */
5700 else if (e->ts.type == BT_ASSUMED)
5701 {
5702 if (!actual_arg)
5703 {
5704 gfc_error ("Assumed-type variable %s at %L may only be used "
5705 "as actual argument", sym->name, &e->where);
5706 return false;
5707 }
5708 else if (inquiry_argument && !first_actual_arg)
5709 {
5710 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5711 for all inquiry functions in resolve_function; the reason is
5712 that the function-name resolution happens too late in that
5713 function. */
5714 gfc_error ("Assumed-type variable %s at %L as actual argument to "
5715 "an inquiry function shall be the first argument",
5716 sym->name, &e->where);
5717 return false;
5718 }
5719 }
5720 /* TS 29113, C535b. */
5721 else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5722 && sym->ts.u.derived && CLASS_DATA (sym)
5723 && CLASS_DATA (sym)->as
5724 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5725 || (sym->ts.type != BT_CLASS && sym->as
5726 && sym->as->type == AS_ASSUMED_RANK))
5727 && !sym->attr.select_rank_temporary)
5728 {
5729 if (!actual_arg
5730 && !(cs_base && cs_base->current
5731 && cs_base->current->op == EXEC_SELECT_RANK))
5732 {
5733 gfc_error ("Assumed-rank variable %s at %L may only be used as "
5734 "actual argument", sym->name, &e->where);
5735 return false;
5736 }
5737 else if (inquiry_argument && !first_actual_arg)
5738 {
5739 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5740 for all inquiry functions in resolve_function; the reason is
5741 that the function-name resolution happens too late in that
5742 function. */
5743 gfc_error ("Assumed-rank variable %s at %L as actual argument "
5744 "to an inquiry function shall be the first argument",
5745 sym->name, &e->where);
5746 return false;
5747 }
5748 }
5749
5750 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
5751 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5752 && e->ref->next == NULL))
5753 {
5754 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
5755 "a subobject reference", sym->name, &e->ref->u.ar.where);
5756 return false;
5757 }
5758 /* TS 29113, 407b. */
5759 else if (e->ts.type == BT_ASSUMED && e->ref
5760 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5761 && e->ref->next == NULL))
5762 {
5763 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
5764 "reference", sym->name, &e->ref->u.ar.where);
5765 return false;
5766 }
5767
5768 /* TS 29113, C535b. */
5769 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5770 && sym->ts.u.derived && CLASS_DATA (sym)
5771 && CLASS_DATA (sym)->as
5772 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5773 || (sym->ts.type != BT_CLASS && sym->as
5774 && sym->as->type == AS_ASSUMED_RANK))
5775 && e->ref
5776 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5777 && e->ref->next == NULL))
5778 {
5779 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
5780 "reference", sym->name, &e->ref->u.ar.where);
5781 return false;
5782 }
5783
5784 /* For variables that are used in an associate (target => object) where
5785 the object's basetype is array valued while the target is scalar,
5786 the ts' type of the component refs is still array valued, which
5787 can't be translated that way. */
5788 if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
5789 && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
5790 && sym->assoc->target->ts.u.derived
5791 && CLASS_DATA (sym->assoc->target)
5792 && CLASS_DATA (sym->assoc->target)->as)
5793 {
5794 gfc_ref *ref = e->ref;
5795 while (ref)
5796 {
5797 switch (ref->type)
5798 {
5799 case REF_COMPONENT:
5800 ref->u.c.sym = sym->ts.u.derived;
5801 /* Stop the loop. */
5802 ref = NULL;
5803 break;
5804 default:
5805 ref = ref->next;
5806 break;
5807 }
5808 }
5809 }
5810
5811 /* If this is an associate-name, it may be parsed with an array reference
5812 in error even though the target is scalar. Fail directly in this case.
5813 TODO Understand why class scalar expressions must be excluded. */
5814 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
5815 {
5816 if (sym->ts.type == BT_CLASS)
5817 gfc_fix_class_refs (e);
5818 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
5819 return false;
5820 else if (sym->attr.dimension && (!e->ref || e->ref->type != REF_ARRAY))
5821 {
5822 /* This can happen because the parser did not detect that the
5823 associate name is an array and the expression had no array
5824 part_ref. */
5825 gfc_ref *ref = gfc_get_ref ();
5826 ref->type = REF_ARRAY;
5827 ref->u.ar.type = AR_FULL;
5828 if (sym->as)
5829 {
5830 ref->u.ar.as = sym->as;
5831 ref->u.ar.dimen = sym->as->rank;
5832 }
5833 ref->next = e->ref;
5834 e->ref = ref;
5835
5836 }
5837 }
5838
5839 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
5840 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
5841
5842 /* On the other hand, the parser may not have known this is an array;
5843 in this case, we have to add a FULL reference. */
5844 if (sym->assoc && sym->attr.dimension && !e->ref)
5845 {
5846 e->ref = gfc_get_ref ();
5847 e->ref->type = REF_ARRAY;
5848 e->ref->u.ar.type = AR_FULL;
5849 e->ref->u.ar.dimen = 0;
5850 }
5851
5852 /* Like above, but for class types, where the checking whether an array
5853 ref is present is more complicated. Furthermore make sure not to add
5854 the full array ref to _vptr or _len refs. */
5855 if (sym->assoc && sym->ts.type == BT_CLASS && sym->ts.u.derived
5856 && CLASS_DATA (sym)
5857 && CLASS_DATA (sym)->attr.dimension
5858 && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
5859 {
5860 gfc_ref *ref, *newref;
5861
5862 newref = gfc_get_ref ();
5863 newref->type = REF_ARRAY;
5864 newref->u.ar.type = AR_FULL;
5865 newref->u.ar.dimen = 0;
5866 /* Because this is an associate var and the first ref either is a ref to
5867 the _data component or not, no traversal of the ref chain is
5868 needed. The array ref needs to be inserted after the _data ref,
5869 or when that is not present, which may happend for polymorphic
5870 types, then at the first position. */
5871 ref = e->ref;
5872 if (!ref)
5873 e->ref = newref;
5874 else if (ref->type == REF_COMPONENT
5875 && strcmp ("_data", ref->u.c.component->name) == 0)
5876 {
5877 if (!ref->next || ref->next->type != REF_ARRAY)
5878 {
5879 newref->next = ref->next;
5880 ref->next = newref;
5881 }
5882 else
5883 /* Array ref present already. */
5884 gfc_free_ref_list (newref);
5885 }
5886 else if (ref->type == REF_ARRAY)
5887 /* Array ref present already. */
5888 gfc_free_ref_list (newref);
5889 else
5890 {
5891 newref->next = ref;
5892 e->ref = newref;
5893 }
5894 }
5895
5896 if (e->ref && !gfc_resolve_ref (e))
5897 return false;
5898
5899 if (sym->attr.flavor == FL_PROCEDURE
5900 && (!sym->attr.function
5901 || (sym->attr.function && sym->result
5902 && sym->result->attr.proc_pointer
5903 && !sym->result->attr.function)))
5904 {
5905 e->ts.type = BT_PROCEDURE;
5906 goto resolve_procedure;
5907 }
5908
5909 if (sym->ts.type != BT_UNKNOWN)
5910 gfc_variable_attr (e, &e->ts);
5911 else if (sym->attr.flavor == FL_PROCEDURE
5912 && sym->attr.function && sym->result
5913 && sym->result->ts.type != BT_UNKNOWN
5914 && sym->result->attr.proc_pointer)
5915 e->ts = sym->result->ts;
5916 else
5917 {
5918 /* Must be a simple variable reference. */
5919 if (!gfc_set_default_type (sym, 1, sym->ns))
5920 return false;
5921 e->ts = sym->ts;
5922 }
5923
5924 if (check_assumed_size_reference (sym, e))
5925 return false;
5926
5927 /* Deal with forward references to entries during gfc_resolve_code, to
5928 satisfy, at least partially, 12.5.2.5. */
5929 if (gfc_current_ns->entries
5930 && current_entry_id == sym->entry_id
5931 && cs_base
5932 && cs_base->current
5933 && cs_base->current->op != EXEC_ENTRY)
5934 {
5935 gfc_entry_list *entry;
5936 gfc_formal_arglist *formal;
5937 int n;
5938 bool seen, saved_specification_expr;
5939
5940 /* If the symbol is a dummy... */
5941 if (sym->attr.dummy && sym->ns == gfc_current_ns)
5942 {
5943 entry = gfc_current_ns->entries;
5944 seen = false;
5945
5946 /* ...test if the symbol is a parameter of previous entries. */
5947 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5948 for (formal = entry->sym->formal; formal; formal = formal->next)
5949 {
5950 if (formal->sym && sym->name == formal->sym->name)
5951 {
5952 seen = true;
5953 break;
5954 }
5955 }
5956
5957 /* If it has not been seen as a dummy, this is an error. */
5958 if (!seen)
5959 {
5960 if (specification_expr)
5961 gfc_error ("Variable %qs, used in a specification expression"
5962 ", is referenced at %L before the ENTRY statement "
5963 "in which it is a parameter",
5964 sym->name, &cs_base->current->loc);
5965 else
5966 gfc_error ("Variable %qs is used at %L before the ENTRY "
5967 "statement in which it is a parameter",
5968 sym->name, &cs_base->current->loc);
5969 t = false;
5970 }
5971 }
5972
5973 /* Now do the same check on the specification expressions. */
5974 saved_specification_expr = specification_expr;
5975 specification_expr = true;
5976 if (sym->ts.type == BT_CHARACTER
5977 && !gfc_resolve_expr (sym->ts.u.cl->length))
5978 t = false;
5979
5980 if (sym->as)
5981 for (n = 0; n < sym->as->rank; n++)
5982 {
5983 if (!gfc_resolve_expr (sym->as->lower[n]))
5984 t = false;
5985 if (!gfc_resolve_expr (sym->as->upper[n]))
5986 t = false;
5987 }
5988 specification_expr = saved_specification_expr;
5989
5990 if (t)
5991 /* Update the symbol's entry level. */
5992 sym->entry_id = current_entry_id + 1;
5993 }
5994
5995 /* If a symbol has been host_associated mark it. This is used latter,
5996 to identify if aliasing is possible via host association. */
5997 if (sym->attr.flavor == FL_VARIABLE
5998 && gfc_current_ns->parent
5999 && (gfc_current_ns->parent == sym->ns
6000 || (gfc_current_ns->parent->parent
6001 && gfc_current_ns->parent->parent == sym->ns)))
6002 sym->attr.host_assoc = 1;
6003
6004 if (gfc_current_ns->proc_name
6005 && sym->attr.dimension
6006 && (sym->ns != gfc_current_ns
6007 || sym->attr.use_assoc
6008 || sym->attr.in_common))
6009 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
6010
6011 resolve_procedure:
6012 if (t && !resolve_procedure_expression (e))
6013 t = false;
6014
6015 /* F2008, C617 and C1229. */
6016 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
6017 && gfc_is_coindexed (e))
6018 {
6019 gfc_ref *ref, *ref2 = NULL;
6020
6021 for (ref = e->ref; ref; ref = ref->next)
6022 {
6023 if (ref->type == REF_COMPONENT)
6024 ref2 = ref;
6025 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
6026 break;
6027 }
6028
6029 for ( ; ref; ref = ref->next)
6030 if (ref->type == REF_COMPONENT)
6031 break;
6032
6033 /* Expression itself is not coindexed object. */
6034 if (ref && e->ts.type == BT_CLASS)
6035 {
6036 gfc_error ("Polymorphic subobject of coindexed object at %L",
6037 &e->where);
6038 t = false;
6039 }
6040
6041 /* Expression itself is coindexed object. */
6042 if (ref == NULL)
6043 {
6044 gfc_component *c;
6045 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
6046 for ( ; c; c = c->next)
6047 if (c->attr.allocatable && c->ts.type == BT_CLASS)
6048 {
6049 gfc_error ("Coindexed object with polymorphic allocatable "
6050 "subcomponent at %L", &e->where);
6051 t = false;
6052 break;
6053 }
6054 }
6055 }
6056
6057 if (t)
6058 gfc_expression_rank (e);
6059
6060 if (t && flag_coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e))
6061 add_caf_get_intrinsic (e);
6062
6063 if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym != sym->result)
6064 gfc_warning (OPT_Wdeprecated_declarations,
6065 "Using variable %qs at %L is deprecated",
6066 sym->name, &e->where);
6067 /* Simplify cases where access to a parameter array results in a
6068 single constant. Suppress errors since those will have been
6069 issued before, as warnings. */
6070 if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
6071 {
6072 gfc_push_suppress_errors ();
6073 gfc_simplify_expr (e, 1);
6074 gfc_pop_suppress_errors ();
6075 }
6076
6077 return t;
6078 }
6079
6080
6081 /* Checks to see that the correct symbol has been host associated.
6082 The only situation where this arises is that in which a twice
6083 contained function is parsed after the host association is made.
6084 Therefore, on detecting this, change the symbol in the expression
6085 and convert the array reference into an actual arglist if the old
6086 symbol is a variable. */
6087 static bool
6088 check_host_association (gfc_expr *e)
6089 {
6090 gfc_symbol *sym, *old_sym;
6091 gfc_symtree *st;
6092 int n;
6093 gfc_ref *ref;
6094 gfc_actual_arglist *arg, *tail = NULL;
6095 bool retval = e->expr_type == EXPR_FUNCTION;
6096
6097 /* If the expression is the result of substitution in
6098 interface.cc(gfc_extend_expr) because there is no way in
6099 which the host association can be wrong. */
6100 if (e->symtree == NULL
6101 || e->symtree->n.sym == NULL
6102 || e->user_operator)
6103 return retval;
6104
6105 old_sym = e->symtree->n.sym;
6106
6107 if (gfc_current_ns->parent
6108 && old_sym->ns != gfc_current_ns)
6109 {
6110 /* Use the 'USE' name so that renamed module symbols are
6111 correctly handled. */
6112 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
6113
6114 if (sym && old_sym != sym
6115 && sym->attr.flavor == FL_PROCEDURE
6116 && sym->attr.contained)
6117 {
6118 /* Clear the shape, since it might not be valid. */
6119 gfc_free_shape (&e->shape, e->rank);
6120
6121 /* Give the expression the right symtree! */
6122 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
6123 gcc_assert (st != NULL);
6124
6125 if (old_sym->attr.flavor == FL_PROCEDURE
6126 || e->expr_type == EXPR_FUNCTION)
6127 {
6128 /* Original was function so point to the new symbol, since
6129 the actual argument list is already attached to the
6130 expression. */
6131 e->value.function.esym = NULL;
6132 e->symtree = st;
6133 }
6134 else
6135 {
6136 /* Original was variable so convert array references into
6137 an actual arglist. This does not need any checking now
6138 since resolve_function will take care of it. */
6139 e->value.function.actual = NULL;
6140 e->expr_type = EXPR_FUNCTION;
6141 e->symtree = st;
6142
6143 /* Ambiguity will not arise if the array reference is not
6144 the last reference. */
6145 for (ref = e->ref; ref; ref = ref->next)
6146 if (ref->type == REF_ARRAY && ref->next == NULL)
6147 break;
6148
6149 if ((ref == NULL || ref->type != REF_ARRAY)
6150 && sym->attr.proc == PROC_INTERNAL)
6151 {
6152 gfc_error ("%qs at %L is host associated at %L into "
6153 "a contained procedure with an internal "
6154 "procedure of the same name", sym->name,
6155 &old_sym->declared_at, &e->where);
6156 return false;
6157 }
6158
6159 if (ref == NULL)
6160 return false;
6161
6162 gcc_assert (ref->type == REF_ARRAY);
6163
6164 /* Grab the start expressions from the array ref and
6165 copy them into actual arguments. */
6166 for (n = 0; n < ref->u.ar.dimen; n++)
6167 {
6168 arg = gfc_get_actual_arglist ();
6169 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
6170 if (e->value.function.actual == NULL)
6171 tail = e->value.function.actual = arg;
6172 else
6173 {
6174 tail->next = arg;
6175 tail = arg;
6176 }
6177 }
6178
6179 /* Dump the reference list and set the rank. */
6180 gfc_free_ref_list (e->ref);
6181 e->ref = NULL;
6182 e->rank = sym->as ? sym->as->rank : 0;
6183 }
6184
6185 gfc_resolve_expr (e);
6186 sym->refs++;
6187 }
6188 }
6189 /* This might have changed! */
6190 return e->expr_type == EXPR_FUNCTION;
6191 }
6192
6193
6194 static void
6195 gfc_resolve_character_operator (gfc_expr *e)
6196 {
6197 gfc_expr *op1 = e->value.op.op1;
6198 gfc_expr *op2 = e->value.op.op2;
6199 gfc_expr *e1 = NULL;
6200 gfc_expr *e2 = NULL;
6201
6202 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
6203
6204 if (op1->ts.u.cl && op1->ts.u.cl->length)
6205 e1 = gfc_copy_expr (op1->ts.u.cl->length);
6206 else if (op1->expr_type == EXPR_CONSTANT)
6207 e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6208 op1->value.character.length);
6209
6210 if (op2->ts.u.cl && op2->ts.u.cl->length)
6211 e2 = gfc_copy_expr (op2->ts.u.cl->length);
6212 else if (op2->expr_type == EXPR_CONSTANT)
6213 e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6214 op2->value.character.length);
6215
6216 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6217
6218 if (!e1 || !e2)
6219 {
6220 gfc_free_expr (e1);
6221 gfc_free_expr (e2);
6222
6223 return;
6224 }
6225
6226 e->ts.u.cl->length = gfc_add (e1, e2);
6227 e->ts.u.cl->length->ts.type = BT_INTEGER;
6228 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
6229 gfc_simplify_expr (e->ts.u.cl->length, 0);
6230 gfc_resolve_expr (e->ts.u.cl->length);
6231
6232 return;
6233 }
6234
6235
6236 /* Ensure that an character expression has a charlen and, if possible, a
6237 length expression. */
6238
6239 static void
6240 fixup_charlen (gfc_expr *e)
6241 {
6242 /* The cases fall through so that changes in expression type and the need
6243 for multiple fixes are picked up. In all circumstances, a charlen should
6244 be available for the middle end to hang a backend_decl on. */
6245 switch (e->expr_type)
6246 {
6247 case EXPR_OP:
6248 gfc_resolve_character_operator (e);
6249 /* FALLTHRU */
6250
6251 case EXPR_ARRAY:
6252 if (e->expr_type == EXPR_ARRAY)
6253 gfc_resolve_character_array_constructor (e);
6254 /* FALLTHRU */
6255
6256 case EXPR_SUBSTRING:
6257 if (!e->ts.u.cl && e->ref)
6258 gfc_resolve_substring_charlen (e);
6259 /* FALLTHRU */
6260
6261 default:
6262 if (!e->ts.u.cl)
6263 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6264
6265 break;
6266 }
6267 }
6268
6269
6270 /* Update an actual argument to include the passed-object for type-bound
6271 procedures at the right position. */
6272
6273 static gfc_actual_arglist*
6274 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
6275 const char *name)
6276 {
6277 gcc_assert (argpos > 0);
6278
6279 if (argpos == 1)
6280 {
6281 gfc_actual_arglist* result;
6282
6283 result = gfc_get_actual_arglist ();
6284 result->expr = po;
6285 result->next = lst;
6286 if (name)
6287 result->name = name;
6288
6289 return result;
6290 }
6291
6292 if (lst)
6293 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
6294 else
6295 lst = update_arglist_pass (NULL, po, argpos - 1, name);
6296 return lst;
6297 }
6298
6299
6300 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
6301
6302 static gfc_expr*
6303 extract_compcall_passed_object (gfc_expr* e)
6304 {
6305 gfc_expr* po;
6306
6307 if (e->expr_type == EXPR_UNKNOWN)
6308 {
6309 gfc_error ("Error in typebound call at %L",
6310 &e->where);
6311 return NULL;
6312 }
6313
6314 gcc_assert (e->expr_type == EXPR_COMPCALL);
6315
6316 if (e->value.compcall.base_object)
6317 po = gfc_copy_expr (e->value.compcall.base_object);
6318 else
6319 {
6320 po = gfc_get_expr ();
6321 po->expr_type = EXPR_VARIABLE;
6322 po->symtree = e->symtree;
6323 po->ref = gfc_copy_ref (e->ref);
6324 po->where = e->where;
6325 }
6326
6327 if (!gfc_resolve_expr (po))
6328 return NULL;
6329
6330 return po;
6331 }
6332
6333
6334 /* Update the arglist of an EXPR_COMPCALL expression to include the
6335 passed-object. */
6336
6337 static bool
6338 update_compcall_arglist (gfc_expr* e)
6339 {
6340 gfc_expr* po;
6341 gfc_typebound_proc* tbp;
6342
6343 tbp = e->value.compcall.tbp;
6344
6345 if (tbp->error)
6346 return false;
6347
6348 po = extract_compcall_passed_object (e);
6349 if (!po)
6350 return false;
6351
6352 if (tbp->nopass || e->value.compcall.ignore_pass)
6353 {
6354 gfc_free_expr (po);
6355 return true;
6356 }
6357
6358 if (tbp->pass_arg_num <= 0)
6359 return false;
6360
6361 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
6362 tbp->pass_arg_num,
6363 tbp->pass_arg);
6364
6365 return true;
6366 }
6367
6368
6369 /* Extract the passed object from a PPC call (a copy of it). */
6370
6371 static gfc_expr*
6372 extract_ppc_passed_object (gfc_expr *e)
6373 {
6374 gfc_expr *po;
6375 gfc_ref **ref;
6376
6377 po = gfc_get_expr ();
6378 po->expr_type = EXPR_VARIABLE;
6379 po->symtree = e->symtree;
6380 po->ref = gfc_copy_ref (e->ref);
6381 po->where = e->where;
6382
6383 /* Remove PPC reference. */
6384 ref = &po->ref;
6385 while ((*ref)->next)
6386 ref = &(*ref)->next;
6387 gfc_free_ref_list (*ref);
6388 *ref = NULL;
6389
6390 if (!gfc_resolve_expr (po))
6391 return NULL;
6392
6393 return po;
6394 }
6395
6396
6397 /* Update the actual arglist of a procedure pointer component to include the
6398 passed-object. */
6399
6400 static bool
6401 update_ppc_arglist (gfc_expr* e)
6402 {
6403 gfc_expr* po;
6404 gfc_component *ppc;
6405 gfc_typebound_proc* tb;
6406
6407 ppc = gfc_get_proc_ptr_comp (e);
6408 if (!ppc)
6409 return false;
6410
6411 tb = ppc->tb;
6412
6413 if (tb->error)
6414 return false;
6415 else if (tb->nopass)
6416 return true;
6417
6418 po = extract_ppc_passed_object (e);
6419 if (!po)
6420 return false;
6421
6422 /* F08:R739. */
6423 if (po->rank != 0)
6424 {
6425 gfc_error ("Passed-object at %L must be scalar", &e->where);
6426 return false;
6427 }
6428
6429 /* F08:C611. */
6430 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
6431 {
6432 gfc_error ("Base object for procedure-pointer component call at %L is of"
6433 " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
6434 return false;
6435 }
6436
6437 gcc_assert (tb->pass_arg_num > 0);
6438 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
6439 tb->pass_arg_num,
6440 tb->pass_arg);
6441
6442 return true;
6443 }
6444
6445
6446 /* Check that the object a TBP is called on is valid, i.e. it must not be
6447 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
6448
6449 static bool
6450 check_typebound_baseobject (gfc_expr* e)
6451 {
6452 gfc_expr* base;
6453 bool return_value = false;
6454
6455 base = extract_compcall_passed_object (e);
6456 if (!base)
6457 return false;
6458
6459 if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
6460 {
6461 gfc_error ("Error in typebound call at %L", &e->where);
6462 goto cleanup;
6463 }
6464
6465 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
6466 return false;
6467
6468 /* F08:C611. */
6469 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
6470 {
6471 gfc_error ("Base object for type-bound procedure call at %L is of"
6472 " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
6473 goto cleanup;
6474 }
6475
6476 /* F08:C1230. If the procedure called is NOPASS,
6477 the base object must be scalar. */
6478 if (e->value.compcall.tbp->nopass && base->rank != 0)
6479 {
6480 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
6481 " be scalar", &e->where);
6482 goto cleanup;
6483 }
6484
6485 return_value = true;
6486
6487 cleanup:
6488 gfc_free_expr (base);
6489 return return_value;
6490 }
6491
6492
6493 /* Resolve a call to a type-bound procedure, either function or subroutine,
6494 statically from the data in an EXPR_COMPCALL expression. The adapted
6495 arglist and the target-procedure symtree are returned. */
6496
6497 static bool
6498 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
6499 gfc_actual_arglist** actual)
6500 {
6501 gcc_assert (e->expr_type == EXPR_COMPCALL);
6502 gcc_assert (!e->value.compcall.tbp->is_generic);
6503
6504 /* Update the actual arglist for PASS. */
6505 if (!update_compcall_arglist (e))
6506 return false;
6507
6508 *actual = e->value.compcall.actual;
6509 *target = e->value.compcall.tbp->u.specific;
6510
6511 gfc_free_ref_list (e->ref);
6512 e->ref = NULL;
6513 e->value.compcall.actual = NULL;
6514
6515 /* If we find a deferred typebound procedure, check for derived types
6516 that an overriding typebound procedure has not been missed. */
6517 if (e->value.compcall.name
6518 && !e->value.compcall.tbp->non_overridable
6519 && e->value.compcall.base_object
6520 && e->value.compcall.base_object->ts.type == BT_DERIVED)
6521 {
6522 gfc_symtree *st;
6523 gfc_symbol *derived;
6524
6525 /* Use the derived type of the base_object. */
6526 derived = e->value.compcall.base_object->ts.u.derived;
6527 st = NULL;
6528
6529 /* If necessary, go through the inheritance chain. */
6530 while (!st && derived)
6531 {
6532 /* Look for the typebound procedure 'name'. */
6533 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
6534 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
6535 e->value.compcall.name);
6536 if (!st)
6537 derived = gfc_get_derived_super_type (derived);
6538 }
6539
6540 /* Now find the specific name in the derived type namespace. */
6541 if (st && st->n.tb && st->n.tb->u.specific)
6542 gfc_find_sym_tree (st->n.tb->u.specific->name,
6543 derived->ns, 1, &st);
6544 if (st)
6545 *target = st;
6546 }
6547 return true;
6548 }
6549
6550
6551 /* Get the ultimate declared type from an expression. In addition,
6552 return the last class/derived type reference and the copy of the
6553 reference list. If check_types is set true, derived types are
6554 identified as well as class references. */
6555 static gfc_symbol*
6556 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
6557 gfc_expr *e, bool check_types)
6558 {
6559 gfc_symbol *declared;
6560 gfc_ref *ref;
6561
6562 declared = NULL;
6563 if (class_ref)
6564 *class_ref = NULL;
6565 if (new_ref)
6566 *new_ref = gfc_copy_ref (e->ref);
6567
6568 for (ref = e->ref; ref; ref = ref->next)
6569 {
6570 if (ref->type != REF_COMPONENT)
6571 continue;
6572
6573 if ((ref->u.c.component->ts.type == BT_CLASS
6574 || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
6575 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
6576 {
6577 declared = ref->u.c.component->ts.u.derived;
6578 if (class_ref)
6579 *class_ref = ref;
6580 }
6581 }
6582
6583 if (declared == NULL)
6584 declared = e->symtree->n.sym->ts.u.derived;
6585
6586 return declared;
6587 }
6588
6589
6590 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
6591 which of the specific bindings (if any) matches the arglist and transform
6592 the expression into a call of that binding. */
6593
6594 static bool
6595 resolve_typebound_generic_call (gfc_expr* e, const char **name)
6596 {
6597 gfc_typebound_proc* genproc;
6598 const char* genname;
6599 gfc_symtree *st;
6600 gfc_symbol *derived;
6601
6602 gcc_assert (e->expr_type == EXPR_COMPCALL);
6603 genname = e->value.compcall.name;
6604 genproc = e->value.compcall.tbp;
6605
6606 if (!genproc->is_generic)
6607 return true;
6608
6609 /* Try the bindings on this type and in the inheritance hierarchy. */
6610 for (; genproc; genproc = genproc->overridden)
6611 {
6612 gfc_tbp_generic* g;
6613
6614 gcc_assert (genproc->is_generic);
6615 for (g = genproc->u.generic; g; g = g->next)
6616 {
6617 gfc_symbol* target;
6618 gfc_actual_arglist* args;
6619 bool matches;
6620
6621 gcc_assert (g->specific);
6622
6623 if (g->specific->error)
6624 continue;
6625
6626 target = g->specific->u.specific->n.sym;
6627
6628 /* Get the right arglist by handling PASS/NOPASS. */
6629 args = gfc_copy_actual_arglist (e->value.compcall.actual);
6630 if (!g->specific->nopass)
6631 {
6632 gfc_expr* po;
6633 po = extract_compcall_passed_object (e);
6634 if (!po)
6635 {
6636 gfc_free_actual_arglist (args);
6637 return false;
6638 }
6639
6640 gcc_assert (g->specific->pass_arg_num > 0);
6641 gcc_assert (!g->specific->error);
6642 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
6643 g->specific->pass_arg);
6644 }
6645 resolve_actual_arglist (args, target->attr.proc,
6646 is_external_proc (target)
6647 && gfc_sym_get_dummy_args (target) == NULL);
6648
6649 /* Check if this arglist matches the formal. */
6650 matches = gfc_arglist_matches_symbol (&args, target);
6651
6652 /* Clean up and break out of the loop if we've found it. */
6653 gfc_free_actual_arglist (args);
6654 if (matches)
6655 {
6656 e->value.compcall.tbp = g->specific;
6657 genname = g->specific_st->name;
6658 /* Pass along the name for CLASS methods, where the vtab
6659 procedure pointer component has to be referenced. */
6660 if (name)
6661 *name = genname;
6662 goto success;
6663 }
6664 }
6665 }
6666
6667 /* Nothing matching found! */
6668 gfc_error ("Found no matching specific binding for the call to the GENERIC"
6669 " %qs at %L", genname, &e->where);
6670 return false;
6671
6672 success:
6673 /* Make sure that we have the right specific instance for the name. */
6674 derived = get_declared_from_expr (NULL, NULL, e, true);
6675
6676 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
6677 if (st)
6678 e->value.compcall.tbp = st->n.tb;
6679
6680 return true;
6681 }
6682
6683
6684 /* Resolve a call to a type-bound subroutine. */
6685
6686 static bool
6687 resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
6688 {
6689 gfc_actual_arglist* newactual;
6690 gfc_symtree* target;
6691
6692 /* Check that's really a SUBROUTINE. */
6693 if (!c->expr1->value.compcall.tbp->subroutine)
6694 {
6695 if (!c->expr1->value.compcall.tbp->is_generic
6696 && c->expr1->value.compcall.tbp->u.specific
6697 && c->expr1->value.compcall.tbp->u.specific->n.sym
6698 && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
6699 c->expr1->value.compcall.tbp->subroutine = 1;
6700 else
6701 {
6702 gfc_error ("%qs at %L should be a SUBROUTINE",
6703 c->expr1->value.compcall.name, &c->loc);
6704 return false;
6705 }
6706 }
6707
6708 if (!check_typebound_baseobject (c->expr1))
6709 return false;
6710
6711 /* Pass along the name for CLASS methods, where the vtab
6712 procedure pointer component has to be referenced. */
6713 if (name)
6714 *name = c->expr1->value.compcall.name;
6715
6716 if (!resolve_typebound_generic_call (c->expr1, name))
6717 return false;
6718
6719 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
6720 if (overridable)
6721 *overridable = !c->expr1->value.compcall.tbp->non_overridable;
6722
6723 /* Transform into an ordinary EXEC_CALL for now. */
6724
6725 if (!resolve_typebound_static (c->expr1, &target, &newactual))
6726 return false;
6727
6728 c->ext.actual = newactual;
6729 c->symtree = target;
6730 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
6731
6732 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
6733
6734 gfc_free_expr (c->expr1);
6735 c->expr1 = gfc_get_expr ();
6736 c->expr1->expr_type = EXPR_FUNCTION;
6737 c->expr1->symtree = target;
6738 c->expr1->where = c->loc;
6739
6740 return resolve_call (c);
6741 }
6742
6743
6744 /* Resolve a component-call expression. */
6745 static bool
6746 resolve_compcall (gfc_expr* e, const char **name)
6747 {
6748 gfc_actual_arglist* newactual;
6749 gfc_symtree* target;
6750
6751 /* Check that's really a FUNCTION. */
6752 if (!e->value.compcall.tbp->function)
6753 {
6754 gfc_error ("%qs at %L should be a FUNCTION",
6755 e->value.compcall.name, &e->where);
6756 return false;
6757 }
6758
6759
6760 /* These must not be assign-calls! */
6761 gcc_assert (!e->value.compcall.assign);
6762
6763 if (!check_typebound_baseobject (e))
6764 return false;
6765
6766 /* Pass along the name for CLASS methods, where the vtab
6767 procedure pointer component has to be referenced. */
6768 if (name)
6769 *name = e->value.compcall.name;
6770
6771 if (!resolve_typebound_generic_call (e, name))
6772 return false;
6773 gcc_assert (!e->value.compcall.tbp->is_generic);
6774
6775 /* Take the rank from the function's symbol. */
6776 if (e->value.compcall.tbp->u.specific->n.sym->as)
6777 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
6778
6779 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
6780 arglist to the TBP's binding target. */
6781
6782 if (!resolve_typebound_static (e, &target, &newactual))
6783 return false;
6784
6785 e->value.function.actual = newactual;
6786 e->value.function.name = NULL;
6787 e->value.function.esym = target->n.sym;
6788 e->value.function.isym = NULL;
6789 e->symtree = target;
6790 e->ts = target->n.sym->ts;
6791 e->expr_type = EXPR_FUNCTION;
6792
6793 /* Resolution is not necessary if this is a class subroutine; this
6794 function only has to identify the specific proc. Resolution of
6795 the call will be done next in resolve_typebound_call. */
6796 return gfc_resolve_expr (e);
6797 }
6798
6799
6800 static bool resolve_fl_derived (gfc_symbol *sym);
6801
6802
6803 /* Resolve a typebound function, or 'method'. First separate all
6804 the non-CLASS references by calling resolve_compcall directly. */
6805
6806 static bool
6807 resolve_typebound_function (gfc_expr* e)
6808 {
6809 gfc_symbol *declared;
6810 gfc_component *c;
6811 gfc_ref *new_ref;
6812 gfc_ref *class_ref;
6813 gfc_symtree *st;
6814 const char *name;
6815 gfc_typespec ts;
6816 gfc_expr *expr;
6817 bool overridable;
6818
6819 st = e->symtree;
6820
6821 /* Deal with typebound operators for CLASS objects. */
6822 expr = e->value.compcall.base_object;
6823 overridable = !e->value.compcall.tbp->non_overridable;
6824 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
6825 {
6826 /* Since the typebound operators are generic, we have to ensure
6827 that any delays in resolution are corrected and that the vtab
6828 is present. */
6829 ts = expr->ts;
6830 declared = ts.u.derived;
6831 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6832 if (c->ts.u.derived == NULL)
6833 c->ts.u.derived = gfc_find_derived_vtab (declared);
6834
6835 if (!resolve_compcall (e, &name))
6836 return false;
6837
6838 /* Use the generic name if it is there. */
6839 name = name ? name : e->value.function.esym->name;
6840 e->symtree = expr->symtree;
6841 e->ref = gfc_copy_ref (expr->ref);
6842 get_declared_from_expr (&class_ref, NULL, e, false);
6843
6844 /* Trim away the extraneous references that emerge from nested
6845 use of interface.cc (extend_expr). */
6846 if (class_ref && class_ref->next)
6847 {
6848 gfc_free_ref_list (class_ref->next);
6849 class_ref->next = NULL;
6850 }
6851 else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
6852 {
6853 gfc_free_ref_list (e->ref);
6854 e->ref = NULL;
6855 }
6856
6857 gfc_add_vptr_component (e);
6858 gfc_add_component_ref (e, name);
6859 e->value.function.esym = NULL;
6860 if (expr->expr_type != EXPR_VARIABLE)
6861 e->base_expr = expr;
6862 return true;
6863 }
6864
6865 if (st == NULL)
6866 return resolve_compcall (e, NULL);
6867
6868 if (!gfc_resolve_ref (e))
6869 return false;
6870
6871 /* Get the CLASS declared type. */
6872 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
6873
6874 if (!resolve_fl_derived (declared))
6875 return false;
6876
6877 /* Weed out cases of the ultimate component being a derived type. */
6878 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6879 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6880 {
6881 gfc_free_ref_list (new_ref);
6882 return resolve_compcall (e, NULL);
6883 }
6884
6885 c = gfc_find_component (declared, "_data", true, true, NULL);
6886
6887 /* Treat the call as if it is a typebound procedure, in order to roll
6888 out the correct name for the specific function. */
6889 if (!resolve_compcall (e, &name))
6890 {
6891 gfc_free_ref_list (new_ref);
6892 return false;
6893 }
6894 ts = e->ts;
6895
6896 if (overridable)
6897 {
6898 /* Convert the expression to a procedure pointer component call. */
6899 e->value.function.esym = NULL;
6900 e->symtree = st;
6901
6902 if (new_ref)
6903 e->ref = new_ref;
6904
6905 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6906 gfc_add_vptr_component (e);
6907 gfc_add_component_ref (e, name);
6908
6909 /* Recover the typespec for the expression. This is really only
6910 necessary for generic procedures, where the additional call
6911 to gfc_add_component_ref seems to throw the collection of the
6912 correct typespec. */
6913 e->ts = ts;
6914 }
6915 else if (new_ref)
6916 gfc_free_ref_list (new_ref);
6917
6918 return true;
6919 }
6920
6921 /* Resolve a typebound subroutine, or 'method'. First separate all
6922 the non-CLASS references by calling resolve_typebound_call
6923 directly. */
6924
6925 static bool
6926 resolve_typebound_subroutine (gfc_code *code)
6927 {
6928 gfc_symbol *declared;
6929 gfc_component *c;
6930 gfc_ref *new_ref;
6931 gfc_ref *class_ref;
6932 gfc_symtree *st;
6933 const char *name;
6934 gfc_typespec ts;
6935 gfc_expr *expr;
6936 bool overridable;
6937
6938 st = code->expr1->symtree;
6939
6940 /* Deal with typebound operators for CLASS objects. */
6941 expr = code->expr1->value.compcall.base_object;
6942 overridable = !code->expr1->value.compcall.tbp->non_overridable;
6943 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
6944 {
6945 /* If the base_object is not a variable, the corresponding actual
6946 argument expression must be stored in e->base_expression so
6947 that the corresponding tree temporary can be used as the base
6948 object in gfc_conv_procedure_call. */
6949 if (expr->expr_type != EXPR_VARIABLE)
6950 {
6951 gfc_actual_arglist *args;
6952
6953 args= code->expr1->value.function.actual;
6954 for (; args; args = args->next)
6955 if (expr == args->expr)
6956 expr = args->expr;
6957 }
6958
6959 /* Since the typebound operators are generic, we have to ensure
6960 that any delays in resolution are corrected and that the vtab
6961 is present. */
6962 declared = expr->ts.u.derived;
6963 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6964 if (c->ts.u.derived == NULL)
6965 c->ts.u.derived = gfc_find_derived_vtab (declared);
6966
6967 if (!resolve_typebound_call (code, &name, NULL))
6968 return false;
6969
6970 /* Use the generic name if it is there. */
6971 name = name ? name : code->expr1->value.function.esym->name;
6972 code->expr1->symtree = expr->symtree;
6973 code->expr1->ref = gfc_copy_ref (expr->ref);
6974
6975 /* Trim away the extraneous references that emerge from nested
6976 use of interface.cc (extend_expr). */
6977 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
6978 if (class_ref && class_ref->next)
6979 {
6980 gfc_free_ref_list (class_ref->next);
6981 class_ref->next = NULL;
6982 }
6983 else if (code->expr1->ref && !class_ref)
6984 {
6985 gfc_free_ref_list (code->expr1->ref);
6986 code->expr1->ref = NULL;
6987 }
6988
6989 /* Now use the procedure in the vtable. */
6990 gfc_add_vptr_component (code->expr1);
6991 gfc_add_component_ref (code->expr1, name);
6992 code->expr1->value.function.esym = NULL;
6993 if (expr->expr_type != EXPR_VARIABLE)
6994 code->expr1->base_expr = expr;
6995 return true;
6996 }
6997
6998 if (st == NULL)
6999 return resolve_typebound_call (code, NULL, NULL);
7000
7001 if (!gfc_resolve_ref (code->expr1))
7002 return false;
7003
7004 /* Get the CLASS declared type. */
7005 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
7006
7007 /* Weed out cases of the ultimate component being a derived type. */
7008 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
7009 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
7010 {
7011 gfc_free_ref_list (new_ref);
7012 return resolve_typebound_call (code, NULL, NULL);
7013 }
7014
7015 if (!resolve_typebound_call (code, &name, &overridable))
7016 {
7017 gfc_free_ref_list (new_ref);
7018 return false;
7019 }
7020 ts = code->expr1->ts;
7021
7022 if (overridable)
7023 {
7024 /* Convert the expression to a procedure pointer component call. */
7025 code->expr1->value.function.esym = NULL;
7026 code->expr1->symtree = st;
7027
7028 if (new_ref)
7029 code->expr1->ref = new_ref;
7030
7031 /* '_vptr' points to the vtab, which contains the procedure pointers. */
7032 gfc_add_vptr_component (code->expr1);
7033 gfc_add_component_ref (code->expr1, name);
7034
7035 /* Recover the typespec for the expression. This is really only
7036 necessary for generic procedures, where the additional call
7037 to gfc_add_component_ref seems to throw the collection of the
7038 correct typespec. */
7039 code->expr1->ts = ts;
7040 }
7041 else if (new_ref)
7042 gfc_free_ref_list (new_ref);
7043
7044 return true;
7045 }
7046
7047
7048 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
7049
7050 static bool
7051 resolve_ppc_call (gfc_code* c)
7052 {
7053 gfc_component *comp;
7054
7055 comp = gfc_get_proc_ptr_comp (c->expr1);
7056 gcc_assert (comp != NULL);
7057
7058 c->resolved_sym = c->expr1->symtree->n.sym;
7059 c->expr1->expr_type = EXPR_VARIABLE;
7060
7061 if (!comp->attr.subroutine)
7062 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
7063
7064 if (!gfc_resolve_ref (c->expr1))
7065 return false;
7066
7067 if (!update_ppc_arglist (c->expr1))
7068 return false;
7069
7070 c->ext.actual = c->expr1->value.compcall.actual;
7071
7072 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
7073 !(comp->ts.interface
7074 && comp->ts.interface->formal)))
7075 return false;
7076
7077 if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
7078 return false;
7079
7080 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
7081
7082 return true;
7083 }
7084
7085
7086 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
7087
7088 static bool
7089 resolve_expr_ppc (gfc_expr* e)
7090 {
7091 gfc_component *comp;
7092
7093 comp = gfc_get_proc_ptr_comp (e);
7094 gcc_assert (comp != NULL);
7095
7096 /* Convert to EXPR_FUNCTION. */
7097 e->expr_type = EXPR_FUNCTION;
7098 e->value.function.isym = NULL;
7099 e->value.function.actual = e->value.compcall.actual;
7100 e->ts = comp->ts;
7101 if (comp->as != NULL)
7102 e->rank = comp->as->rank;
7103
7104 if (!comp->attr.function)
7105 gfc_add_function (&comp->attr, comp->name, &e->where);
7106
7107 if (!gfc_resolve_ref (e))
7108 return false;
7109
7110 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
7111 !(comp->ts.interface
7112 && comp->ts.interface->formal)))
7113 return false;
7114
7115 if (!update_ppc_arglist (e))
7116 return false;
7117
7118 if (!check_pure_function(e))
7119 return false;
7120
7121 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
7122
7123 return true;
7124 }
7125
7126
7127 static bool
7128 gfc_is_expandable_expr (gfc_expr *e)
7129 {
7130 gfc_constructor *con;
7131
7132 if (e->expr_type == EXPR_ARRAY)
7133 {
7134 /* Traverse the constructor looking for variables that are flavor
7135 parameter. Parameters must be expanded since they are fully used at
7136 compile time. */
7137 con = gfc_constructor_first (e->value.constructor);
7138 for (; con; con = gfc_constructor_next (con))
7139 {
7140 if (con->expr->expr_type == EXPR_VARIABLE
7141 && con->expr->symtree
7142 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
7143 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
7144 return true;
7145 if (con->expr->expr_type == EXPR_ARRAY
7146 && gfc_is_expandable_expr (con->expr))
7147 return true;
7148 }
7149 }
7150
7151 return false;
7152 }
7153
7154
7155 /* Sometimes variables in specification expressions of the result
7156 of module procedures in submodules wind up not being the 'real'
7157 dummy. Find this, if possible, in the namespace of the first
7158 formal argument. */
7159
7160 static void
7161 fixup_unique_dummy (gfc_expr *e)
7162 {
7163 gfc_symtree *st = NULL;
7164 gfc_symbol *s = NULL;
7165
7166 if (e->symtree->n.sym->ns->proc_name
7167 && e->symtree->n.sym->ns->proc_name->formal)
7168 s = e->symtree->n.sym->ns->proc_name->formal->sym;
7169
7170 if (s != NULL)
7171 st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
7172
7173 if (st != NULL
7174 && st->n.sym != NULL
7175 && st->n.sym->attr.dummy)
7176 e->symtree = st;
7177 }
7178
7179 /* Resolve an expression. That is, make sure that types of operands agree
7180 with their operators, intrinsic operators are converted to function calls
7181 for overloaded types and unresolved function references are resolved. */
7182
7183 bool
7184 gfc_resolve_expr (gfc_expr *e)
7185 {
7186 bool t;
7187 bool inquiry_save, actual_arg_save, first_actual_arg_save;
7188
7189 if (e == NULL || e->do_not_resolve_again)
7190 return true;
7191
7192 /* inquiry_argument only applies to variables. */
7193 inquiry_save = inquiry_argument;
7194 actual_arg_save = actual_arg;
7195 first_actual_arg_save = first_actual_arg;
7196
7197 if (e->expr_type != EXPR_VARIABLE)
7198 {
7199 inquiry_argument = false;
7200 actual_arg = false;
7201 first_actual_arg = false;
7202 }
7203 else if (e->symtree != NULL
7204 && *e->symtree->name == '@'
7205 && e->symtree->n.sym->attr.dummy)
7206 {
7207 /* Deal with submodule specification expressions that are not
7208 found to be referenced in module.cc(read_cleanup). */
7209 fixup_unique_dummy (e);
7210 }
7211
7212 switch (e->expr_type)
7213 {
7214 case EXPR_OP:
7215 t = resolve_operator (e);
7216 break;
7217
7218 case EXPR_FUNCTION:
7219 case EXPR_VARIABLE:
7220
7221 if (check_host_association (e))
7222 t = resolve_function (e);
7223 else
7224 t = resolve_variable (e);
7225
7226 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
7227 && e->ref->type != REF_SUBSTRING)
7228 gfc_resolve_substring_charlen (e);
7229
7230 break;
7231
7232 case EXPR_COMPCALL:
7233 t = resolve_typebound_function (e);
7234 break;
7235
7236 case EXPR_SUBSTRING:
7237 t = gfc_resolve_ref (e);
7238 break;
7239
7240 case EXPR_CONSTANT:
7241 case EXPR_NULL:
7242 t = true;
7243 break;
7244
7245 case EXPR_PPC:
7246 t = resolve_expr_ppc (e);
7247 break;
7248
7249 case EXPR_ARRAY:
7250 t = false;
7251 if (!gfc_resolve_ref (e))
7252 break;
7253
7254 t = gfc_resolve_array_constructor (e);
7255 /* Also try to expand a constructor. */
7256 if (t)
7257 {
7258 gfc_expression_rank (e);
7259 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
7260 gfc_expand_constructor (e, false);
7261 }
7262
7263 /* This provides the opportunity for the length of constructors with
7264 character valued function elements to propagate the string length
7265 to the expression. */
7266 if (t && e->ts.type == BT_CHARACTER)
7267 {
7268 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
7269 here rather then add a duplicate test for it above. */
7270 gfc_expand_constructor (e, false);
7271 t = gfc_resolve_character_array_constructor (e);
7272 }
7273
7274 break;
7275
7276 case EXPR_STRUCTURE:
7277 t = gfc_resolve_ref (e);
7278 if (!t)
7279 break;
7280
7281 t = resolve_structure_cons (e, 0);
7282 if (!t)
7283 break;
7284
7285 t = gfc_simplify_expr (e, 0);
7286 break;
7287
7288 default:
7289 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
7290 }
7291
7292 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
7293 fixup_charlen (e);
7294
7295 inquiry_argument = inquiry_save;
7296 actual_arg = actual_arg_save;
7297 first_actual_arg = first_actual_arg_save;
7298
7299 /* For some reason, resolving these expressions a second time mangles
7300 the typespec of the expression itself. */
7301 if (t && e->expr_type == EXPR_VARIABLE
7302 && e->symtree->n.sym->attr.select_rank_temporary
7303 && UNLIMITED_POLY (e->symtree->n.sym))
7304 e->do_not_resolve_again = 1;
7305
7306 return t;
7307 }
7308
7309
7310 /* Resolve an expression from an iterator. They must be scalar and have
7311 INTEGER or (optionally) REAL type. */
7312
7313 static bool
7314 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
7315 const char *name_msgid)
7316 {
7317 if (!gfc_resolve_expr (expr))
7318 return false;
7319
7320 if (expr->rank != 0)
7321 {
7322 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
7323 return false;
7324 }
7325
7326 if (expr->ts.type != BT_INTEGER)
7327 {
7328 if (expr->ts.type == BT_REAL)
7329 {
7330 if (real_ok)
7331 return gfc_notify_std (GFC_STD_F95_DEL,
7332 "%s at %L must be integer",
7333 _(name_msgid), &expr->where);
7334 else
7335 {
7336 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
7337 &expr->where);
7338 return false;
7339 }
7340 }
7341 else
7342 {
7343 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
7344 return false;
7345 }
7346 }
7347 return true;
7348 }
7349
7350
7351 /* Resolve the expressions in an iterator structure. If REAL_OK is
7352 false allow only INTEGER type iterators, otherwise allow REAL types.
7353 Set own_scope to true for ac-implied-do and data-implied-do as those
7354 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
7355
7356 bool
7357 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
7358 {
7359 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
7360 return false;
7361
7362 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
7363 _("iterator variable")))
7364 return false;
7365
7366 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
7367 "Start expression in DO loop"))
7368 return false;
7369
7370 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
7371 "End expression in DO loop"))
7372 return false;
7373
7374 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
7375 "Step expression in DO loop"))
7376 return false;
7377
7378 /* Convert start, end, and step to the same type as var. */
7379 if (iter->start->ts.kind != iter->var->ts.kind
7380 || iter->start->ts.type != iter->var->ts.type)
7381 gfc_convert_type (iter->start, &iter->var->ts, 1);
7382
7383 if (iter->end->ts.kind != iter->var->ts.kind
7384 || iter->end->ts.type != iter->var->ts.type)
7385 gfc_convert_type (iter->end, &iter->var->ts, 1);
7386
7387 if (iter->step->ts.kind != iter->var->ts.kind
7388 || iter->step->ts.type != iter->var->ts.type)
7389 gfc_convert_type (iter->step, &iter->var->ts, 1);
7390
7391 if (iter->step->expr_type == EXPR_CONSTANT)
7392 {
7393 if ((iter->step->ts.type == BT_INTEGER
7394 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
7395 || (iter->step->ts.type == BT_REAL
7396 && mpfr_sgn (iter->step->value.real) == 0))
7397 {
7398 gfc_error ("Step expression in DO loop at %L cannot be zero",
7399 &iter->step->where);
7400 return false;
7401 }
7402 }
7403
7404 if (iter->start->expr_type == EXPR_CONSTANT
7405 && iter->end->expr_type == EXPR_CONSTANT
7406 && iter->step->expr_type == EXPR_CONSTANT)
7407 {
7408 int sgn, cmp;
7409 if (iter->start->ts.type == BT_INTEGER)
7410 {
7411 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
7412 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
7413 }
7414 else
7415 {
7416 sgn = mpfr_sgn (iter->step->value.real);
7417 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
7418 }
7419 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
7420 gfc_warning (OPT_Wzerotrip,
7421 "DO loop at %L will be executed zero times",
7422 &iter->step->where);
7423 }
7424
7425 if (iter->end->expr_type == EXPR_CONSTANT
7426 && iter->end->ts.type == BT_INTEGER
7427 && iter->step->expr_type == EXPR_CONSTANT
7428 && iter->step->ts.type == BT_INTEGER
7429 && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
7430 || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
7431 {
7432 bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
7433 int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
7434
7435 if (is_step_positive
7436 && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
7437 gfc_warning (OPT_Wundefined_do_loop,
7438 "DO loop at %L is undefined as it overflows",
7439 &iter->step->where);
7440 else if (!is_step_positive
7441 && mpz_cmp (iter->end->value.integer,
7442 gfc_integer_kinds[k].min_int) == 0)
7443 gfc_warning (OPT_Wundefined_do_loop,
7444 "DO loop at %L is undefined as it underflows",
7445 &iter->step->where);
7446 }
7447
7448 return true;
7449 }
7450
7451
7452 /* Traversal function for find_forall_index. f == 2 signals that
7453 that variable itself is not to be checked - only the references. */
7454
7455 static bool
7456 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
7457 {
7458 if (expr->expr_type != EXPR_VARIABLE)
7459 return false;
7460
7461 /* A scalar assignment */
7462 if (!expr->ref || *f == 1)
7463 {
7464 if (expr->symtree->n.sym == sym)
7465 return true;
7466 else
7467 return false;
7468 }
7469
7470 if (*f == 2)
7471 *f = 1;
7472 return false;
7473 }
7474
7475
7476 /* Check whether the FORALL index appears in the expression or not.
7477 Returns true if SYM is found in EXPR. */
7478
7479 bool
7480 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
7481 {
7482 if (gfc_traverse_expr (expr, sym, forall_index, f))
7483 return true;
7484 else
7485 return false;
7486 }
7487
7488
7489 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
7490 to be a scalar INTEGER variable. The subscripts and stride are scalar
7491 INTEGERs, and if stride is a constant it must be nonzero.
7492 Furthermore "A subscript or stride in a forall-triplet-spec shall
7493 not contain a reference to any index-name in the
7494 forall-triplet-spec-list in which it appears." (7.5.4.1) */
7495
7496 static void
7497 resolve_forall_iterators (gfc_forall_iterator *it)
7498 {
7499 gfc_forall_iterator *iter, *iter2;
7500
7501 for (iter = it; iter; iter = iter->next)
7502 {
7503 if (gfc_resolve_expr (iter->var)
7504 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
7505 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
7506 &iter->var->where);
7507
7508 if (gfc_resolve_expr (iter->start)
7509 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
7510 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
7511 &iter->start->where);
7512 if (iter->var->ts.kind != iter->start->ts.kind)
7513 gfc_convert_type (iter->start, &iter->var->ts, 1);
7514
7515 if (gfc_resolve_expr (iter->end)
7516 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
7517 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
7518 &iter->end->where);
7519 if (iter->var->ts.kind != iter->end->ts.kind)
7520 gfc_convert_type (iter->end, &iter->var->ts, 1);
7521
7522 if (gfc_resolve_expr (iter->stride))
7523 {
7524 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
7525 gfc_error ("FORALL stride expression at %L must be a scalar %s",
7526 &iter->stride->where, "INTEGER");
7527
7528 if (iter->stride->expr_type == EXPR_CONSTANT
7529 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
7530 gfc_error ("FORALL stride expression at %L cannot be zero",
7531 &iter->stride->where);
7532 }
7533 if (iter->var->ts.kind != iter->stride->ts.kind)
7534 gfc_convert_type (iter->stride, &iter->var->ts, 1);
7535 }
7536
7537 for (iter = it; iter; iter = iter->next)
7538 for (iter2 = iter; iter2; iter2 = iter2->next)
7539 {
7540 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
7541 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
7542 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
7543 gfc_error ("FORALL index %qs may not appear in triplet "
7544 "specification at %L", iter->var->symtree->name,
7545 &iter2->start->where);
7546 }
7547 }
7548
7549
7550 /* Given a pointer to a symbol that is a derived type, see if it's
7551 inaccessible, i.e. if it's defined in another module and the components are
7552 PRIVATE. The search is recursive if necessary. Returns zero if no
7553 inaccessible components are found, nonzero otherwise. */
7554
7555 static int
7556 derived_inaccessible (gfc_symbol *sym)
7557 {
7558 gfc_component *c;
7559
7560 if (sym->attr.use_assoc && sym->attr.private_comp)
7561 return 1;
7562
7563 for (c = sym->components; c; c = c->next)
7564 {
7565 /* Prevent an infinite loop through this function. */
7566 if (c->ts.type == BT_DERIVED
7567 && (c->attr.pointer || c->attr.allocatable)
7568 && sym == c->ts.u.derived)
7569 continue;
7570
7571 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
7572 return 1;
7573 }
7574
7575 return 0;
7576 }
7577
7578
7579 /* Resolve the argument of a deallocate expression. The expression must be
7580 a pointer or a full array. */
7581
7582 static bool
7583 resolve_deallocate_expr (gfc_expr *e)
7584 {
7585 symbol_attribute attr;
7586 int allocatable, pointer;
7587 gfc_ref *ref;
7588 gfc_symbol *sym;
7589 gfc_component *c;
7590 bool unlimited;
7591
7592 if (!gfc_resolve_expr (e))
7593 return false;
7594
7595 if (e->expr_type != EXPR_VARIABLE)
7596 goto bad;
7597
7598 sym = e->symtree->n.sym;
7599 unlimited = UNLIMITED_POLY(sym);
7600
7601 if (sym->ts.type == BT_CLASS && sym->attr.class_ok && CLASS_DATA (sym))
7602 {
7603 allocatable = CLASS_DATA (sym)->attr.allocatable;
7604 pointer = CLASS_DATA (sym)->attr.class_pointer;
7605 }
7606 else
7607 {
7608 allocatable = sym->attr.allocatable;
7609 pointer = sym->attr.pointer;
7610 }
7611 for (ref = e->ref; ref; ref = ref->next)
7612 {
7613 switch (ref->type)
7614 {
7615 case REF_ARRAY:
7616 if (ref->u.ar.type != AR_FULL
7617 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
7618 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
7619 allocatable = 0;
7620 break;
7621
7622 case REF_COMPONENT:
7623 c = ref->u.c.component;
7624 if (c->ts.type == BT_CLASS)
7625 {
7626 allocatable = CLASS_DATA (c)->attr.allocatable;
7627 pointer = CLASS_DATA (c)->attr.class_pointer;
7628 }
7629 else
7630 {
7631 allocatable = c->attr.allocatable;
7632 pointer = c->attr.pointer;
7633 }
7634 break;
7635
7636 case REF_SUBSTRING:
7637 case REF_INQUIRY:
7638 allocatable = 0;
7639 break;
7640 }
7641 }
7642
7643 attr = gfc_expr_attr (e);
7644
7645 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
7646 {
7647 bad:
7648 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7649 &e->where);
7650 return false;
7651 }
7652
7653 /* F2008, C644. */
7654 if (gfc_is_coindexed (e))
7655 {
7656 gfc_error ("Coindexed allocatable object at %L", &e->where);
7657 return false;
7658 }
7659
7660 if (pointer
7661 && !gfc_check_vardef_context (e, true, true, false,
7662 _("DEALLOCATE object")))
7663 return false;
7664 if (!gfc_check_vardef_context (e, false, true, false,
7665 _("DEALLOCATE object")))
7666 return false;
7667
7668 return true;
7669 }
7670
7671
7672 /* Returns true if the expression e contains a reference to the symbol sym. */
7673 static bool
7674 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
7675 {
7676 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
7677 return true;
7678
7679 return false;
7680 }
7681
7682 bool
7683 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
7684 {
7685 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
7686 }
7687
7688
7689 /* Given the expression node e for an allocatable/pointer of derived type to be
7690 allocated, get the expression node to be initialized afterwards (needed for
7691 derived types with default initializers, and derived types with allocatable
7692 components that need nullification.) */
7693
7694 gfc_expr *
7695 gfc_expr_to_initialize (gfc_expr *e)
7696 {
7697 gfc_expr *result;
7698 gfc_ref *ref;
7699 int i;
7700
7701 result = gfc_copy_expr (e);
7702
7703 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
7704 for (ref = result->ref; ref; ref = ref->next)
7705 if (ref->type == REF_ARRAY && ref->next == NULL)
7706 {
7707 if (ref->u.ar.dimen == 0
7708 && ref->u.ar.as && ref->u.ar.as->corank)
7709 return result;
7710
7711 ref->u.ar.type = AR_FULL;
7712
7713 for (i = 0; i < ref->u.ar.dimen; i++)
7714 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
7715
7716 break;
7717 }
7718
7719 gfc_free_shape (&result->shape, result->rank);
7720
7721 /* Recalculate rank, shape, etc. */
7722 gfc_resolve_expr (result);
7723 return result;
7724 }
7725
7726
7727 /* If the last ref of an expression is an array ref, return a copy of the
7728 expression with that one removed. Otherwise, a copy of the original
7729 expression. This is used for allocate-expressions and pointer assignment
7730 LHS, where there may be an array specification that needs to be stripped
7731 off when using gfc_check_vardef_context. */
7732
7733 static gfc_expr*
7734 remove_last_array_ref (gfc_expr* e)
7735 {
7736 gfc_expr* e2;
7737 gfc_ref** r;
7738
7739 e2 = gfc_copy_expr (e);
7740 for (r = &e2->ref; *r; r = &(*r)->next)
7741 if ((*r)->type == REF_ARRAY && !(*r)->next)
7742 {
7743 gfc_free_ref_list (*r);
7744 *r = NULL;
7745 break;
7746 }
7747
7748 return e2;
7749 }
7750
7751
7752 /* Used in resolve_allocate_expr to check that a allocation-object and
7753 a source-expr are conformable. This does not catch all possible
7754 cases; in particular a runtime checking is needed. */
7755
7756 static bool
7757 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
7758 {
7759 gfc_ref *tail;
7760 for (tail = e2->ref; tail && tail->next; tail = tail->next);
7761
7762 /* First compare rank. */
7763 if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
7764 || (!tail && e1->rank != e2->rank))
7765 {
7766 gfc_error ("Source-expr at %L must be scalar or have the "
7767 "same rank as the allocate-object at %L",
7768 &e1->where, &e2->where);
7769 return false;
7770 }
7771
7772 if (e1->shape)
7773 {
7774 int i;
7775 mpz_t s;
7776
7777 mpz_init (s);
7778
7779 for (i = 0; i < e1->rank; i++)
7780 {
7781 if (tail->u.ar.start[i] == NULL)
7782 break;
7783
7784 if (tail->u.ar.end[i])
7785 {
7786 mpz_set (s, tail->u.ar.end[i]->value.integer);
7787 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
7788 mpz_add_ui (s, s, 1);
7789 }
7790 else
7791 {
7792 mpz_set (s, tail->u.ar.start[i]->value.integer);
7793 }
7794
7795 if (mpz_cmp (e1->shape[i], s) != 0)
7796 {
7797 gfc_error ("Source-expr at %L and allocate-object at %L must "
7798 "have the same shape", &e1->where, &e2->where);
7799 mpz_clear (s);
7800 return false;
7801 }
7802 }
7803
7804 mpz_clear (s);
7805 }
7806
7807 return true;
7808 }
7809
7810
7811 /* Resolve the expression in an ALLOCATE statement, doing the additional
7812 checks to see whether the expression is OK or not. The expression must
7813 have a trailing array reference that gives the size of the array. */
7814
7815 static bool
7816 resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
7817 {
7818 int i, pointer, allocatable, dimension, is_abstract;
7819 int codimension;
7820 bool coindexed;
7821 bool unlimited;
7822 symbol_attribute attr;
7823 gfc_ref *ref, *ref2;
7824 gfc_expr *e2;
7825 gfc_array_ref *ar;
7826 gfc_symbol *sym = NULL;
7827 gfc_alloc *a;
7828 gfc_component *c;
7829 bool t;
7830
7831 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
7832 checking of coarrays. */
7833 for (ref = e->ref; ref; ref = ref->next)
7834 if (ref->next == NULL)
7835 break;
7836
7837 if (ref && ref->type == REF_ARRAY)
7838 ref->u.ar.in_allocate = true;
7839
7840 if (!gfc_resolve_expr (e))
7841 goto failure;
7842
7843 /* Make sure the expression is allocatable or a pointer. If it is
7844 pointer, the next-to-last reference must be a pointer. */
7845
7846 ref2 = NULL;
7847 if (e->symtree)
7848 sym = e->symtree->n.sym;
7849
7850 /* Check whether ultimate component is abstract and CLASS. */
7851 is_abstract = 0;
7852
7853 /* Is the allocate-object unlimited polymorphic? */
7854 unlimited = UNLIMITED_POLY(e);
7855
7856 if (e->expr_type != EXPR_VARIABLE)
7857 {
7858 allocatable = 0;
7859 attr = gfc_expr_attr (e);
7860 pointer = attr.pointer;
7861 dimension = attr.dimension;
7862 codimension = attr.codimension;
7863 }
7864 else
7865 {
7866 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
7867 {
7868 allocatable = CLASS_DATA (sym)->attr.allocatable;
7869 pointer = CLASS_DATA (sym)->attr.class_pointer;
7870 dimension = CLASS_DATA (sym)->attr.dimension;
7871 codimension = CLASS_DATA (sym)->attr.codimension;
7872 is_abstract = CLASS_DATA (sym)->attr.abstract;
7873 }
7874 else
7875 {
7876 allocatable = sym->attr.allocatable;
7877 pointer = sym->attr.pointer;
7878 dimension = sym->attr.dimension;
7879 codimension = sym->attr.codimension;
7880 }
7881
7882 coindexed = false;
7883
7884 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
7885 {
7886 switch (ref->type)
7887 {
7888 case REF_ARRAY:
7889 if (ref->u.ar.codimen > 0)
7890 {
7891 int n;
7892 for (n = ref->u.ar.dimen;
7893 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
7894 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
7895 {
7896 coindexed = true;
7897 break;
7898 }
7899 }
7900
7901 if (ref->next != NULL)
7902 pointer = 0;
7903 break;
7904
7905 case REF_COMPONENT:
7906 /* F2008, C644. */
7907 if (coindexed)
7908 {
7909 gfc_error ("Coindexed allocatable object at %L",
7910 &e->where);
7911 goto failure;
7912 }
7913
7914 c = ref->u.c.component;
7915 if (c->ts.type == BT_CLASS)
7916 {
7917 allocatable = CLASS_DATA (c)->attr.allocatable;
7918 pointer = CLASS_DATA (c)->attr.class_pointer;
7919 dimension = CLASS_DATA (c)->attr.dimension;
7920 codimension = CLASS_DATA (c)->attr.codimension;
7921 is_abstract = CLASS_DATA (c)->attr.abstract;
7922 }
7923 else
7924 {
7925 allocatable = c->attr.allocatable;
7926 pointer = c->attr.pointer;
7927 dimension = c->attr.dimension;
7928 codimension = c->attr.codimension;
7929 is_abstract = c->attr.abstract;
7930 }
7931 break;
7932
7933 case REF_SUBSTRING:
7934 case REF_INQUIRY:
7935 allocatable = 0;
7936 pointer = 0;
7937 break;
7938 }
7939 }
7940 }
7941
7942 /* Check for F08:C628 (F2018:C932). Each allocate-object shall be a data
7943 pointer or an allocatable variable. */
7944 if (allocatable == 0 && pointer == 0)
7945 {
7946 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7947 &e->where);
7948 goto failure;
7949 }
7950
7951 /* Some checks for the SOURCE tag. */
7952 if (code->expr3)
7953 {
7954 /* Check F03:C631. */
7955 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
7956 {
7957 gfc_error ("Type of entity at %L is type incompatible with "
7958 "source-expr at %L", &e->where, &code->expr3->where);
7959 goto failure;
7960 }
7961
7962 /* Check F03:C632 and restriction following Note 6.18. */
7963 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
7964 goto failure;
7965
7966 /* Check F03:C633. */
7967 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
7968 {
7969 gfc_error ("The allocate-object at %L and the source-expr at %L "
7970 "shall have the same kind type parameter",
7971 &e->where, &code->expr3->where);
7972 goto failure;
7973 }
7974
7975 /* Check F2008, C642. */
7976 if (code->expr3->ts.type == BT_DERIVED
7977 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
7978 || (code->expr3->ts.u.derived->from_intmod
7979 == INTMOD_ISO_FORTRAN_ENV
7980 && code->expr3->ts.u.derived->intmod_sym_id
7981 == ISOFORTRAN_LOCK_TYPE)))
7982 {
7983 gfc_error ("The source-expr at %L shall neither be of type "
7984 "LOCK_TYPE nor have a LOCK_TYPE component if "
7985 "allocate-object at %L is a coarray",
7986 &code->expr3->where, &e->where);
7987 goto failure;
7988 }
7989
7990 /* Check TS18508, C702/C703. */
7991 if (code->expr3->ts.type == BT_DERIVED
7992 && ((codimension && gfc_expr_attr (code->expr3).event_comp)
7993 || (code->expr3->ts.u.derived->from_intmod
7994 == INTMOD_ISO_FORTRAN_ENV
7995 && code->expr3->ts.u.derived->intmod_sym_id
7996 == ISOFORTRAN_EVENT_TYPE)))
7997 {
7998 gfc_error ("The source-expr at %L shall neither be of type "
7999 "EVENT_TYPE nor have a EVENT_TYPE component if "
8000 "allocate-object at %L is a coarray",
8001 &code->expr3->where, &e->where);
8002 goto failure;
8003 }
8004 }
8005
8006 /* Check F08:C629. */
8007 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
8008 && !code->expr3)
8009 {
8010 gcc_assert (e->ts.type == BT_CLASS);
8011 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
8012 "type-spec or source-expr", sym->name, &e->where);
8013 goto failure;
8014 }
8015
8016 /* Check F08:C632. */
8017 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
8018 && !UNLIMITED_POLY (e))
8019 {
8020 int cmp;
8021
8022 if (!e->ts.u.cl->length)
8023 goto failure;
8024
8025 cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
8026 code->ext.alloc.ts.u.cl->length);
8027 if (cmp == 1 || cmp == -1 || cmp == -3)
8028 {
8029 gfc_error ("Allocating %s at %L with type-spec requires the same "
8030 "character-length parameter as in the declaration",
8031 sym->name, &e->where);
8032 goto failure;
8033 }
8034 }
8035
8036 /* In the variable definition context checks, gfc_expr_attr is used
8037 on the expression. This is fooled by the array specification
8038 present in e, thus we have to eliminate that one temporarily. */
8039 e2 = remove_last_array_ref (e);
8040 t = true;
8041 if (t && pointer)
8042 t = gfc_check_vardef_context (e2, true, true, false,
8043 _("ALLOCATE object"));
8044 if (t)
8045 t = gfc_check_vardef_context (e2, false, true, false,
8046 _("ALLOCATE object"));
8047 gfc_free_expr (e2);
8048 if (!t)
8049 goto failure;
8050
8051 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
8052 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
8053 {
8054 /* For class arrays, the initialization with SOURCE is done
8055 using _copy and trans_call. It is convenient to exploit that
8056 when the allocated type is different from the declared type but
8057 no SOURCE exists by setting expr3. */
8058 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
8059 }
8060 else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
8061 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
8062 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
8063 {
8064 /* We have to zero initialize the integer variable. */
8065 code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
8066 }
8067
8068 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
8069 {
8070 /* Make sure the vtab symbol is present when
8071 the module variables are generated. */
8072 gfc_typespec ts = e->ts;
8073 if (code->expr3)
8074 ts = code->expr3->ts;
8075 else if (code->ext.alloc.ts.type == BT_DERIVED)
8076 ts = code->ext.alloc.ts;
8077
8078 /* Finding the vtab also publishes the type's symbol. Therefore this
8079 statement is necessary. */
8080 gfc_find_derived_vtab (ts.u.derived);
8081 }
8082 else if (unlimited && !UNLIMITED_POLY (code->expr3))
8083 {
8084 /* Again, make sure the vtab symbol is present when
8085 the module variables are generated. */
8086 gfc_typespec *ts = NULL;
8087 if (code->expr3)
8088 ts = &code->expr3->ts;
8089 else
8090 ts = &code->ext.alloc.ts;
8091
8092 gcc_assert (ts);
8093
8094 /* Finding the vtab also publishes the type's symbol. Therefore this
8095 statement is necessary. */
8096 gfc_find_vtab (ts);
8097 }
8098
8099 if (dimension == 0 && codimension == 0)
8100 goto success;
8101
8102 /* Make sure the last reference node is an array specification. */
8103
8104 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
8105 || (dimension && ref2->u.ar.dimen == 0))
8106 {
8107 /* F08:C633. */
8108 if (code->expr3)
8109 {
8110 if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
8111 "in ALLOCATE statement at %L", &e->where))
8112 goto failure;
8113 if (code->expr3->rank != 0)
8114 *array_alloc_wo_spec = true;
8115 else
8116 {
8117 gfc_error ("Array specification or array-valued SOURCE= "
8118 "expression required in ALLOCATE statement at %L",
8119 &e->where);
8120 goto failure;
8121 }
8122 }
8123 else
8124 {
8125 gfc_error ("Array specification required in ALLOCATE statement "
8126 "at %L", &e->where);
8127 goto failure;
8128 }
8129 }
8130
8131 /* Make sure that the array section reference makes sense in the
8132 context of an ALLOCATE specification. */
8133
8134 ar = &ref2->u.ar;
8135
8136 if (codimension)
8137 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
8138 {
8139 switch (ar->dimen_type[i])
8140 {
8141 case DIMEN_THIS_IMAGE:
8142 gfc_error ("Coarray specification required in ALLOCATE statement "
8143 "at %L", &e->where);
8144 goto failure;
8145
8146 case DIMEN_RANGE:
8147 /* F2018:R937:
8148 * allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
8149 */
8150 if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
8151 {
8152 gfc_error ("Bad coarray specification in ALLOCATE statement "
8153 "at %L", &e->where);
8154 goto failure;
8155 }
8156 else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
8157 {
8158 gfc_error ("Upper cobound is less than lower cobound at %L",
8159 &ar->start[i]->where);
8160 goto failure;
8161 }
8162 break;
8163
8164 case DIMEN_ELEMENT:
8165 if (ar->start[i]->expr_type == EXPR_CONSTANT)
8166 {
8167 gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
8168 if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
8169 {
8170 gfc_error ("Upper cobound is less than lower cobound "
8171 "of 1 at %L", &ar->start[i]->where);
8172 goto failure;
8173 }
8174 }
8175 break;
8176
8177 case DIMEN_STAR:
8178 break;
8179
8180 default:
8181 gfc_error ("Bad array specification in ALLOCATE statement at %L",
8182 &e->where);
8183 goto failure;
8184
8185 }
8186 }
8187 for (i = 0; i < ar->dimen; i++)
8188 {
8189 if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
8190 goto check_symbols;
8191
8192 switch (ar->dimen_type[i])
8193 {
8194 case DIMEN_ELEMENT:
8195 break;
8196
8197 case DIMEN_RANGE:
8198 if (ar->start[i] != NULL
8199 && ar->end[i] != NULL
8200 && ar->stride[i] == NULL)
8201 break;
8202
8203 /* Fall through. */
8204
8205 case DIMEN_UNKNOWN:
8206 case DIMEN_VECTOR:
8207 case DIMEN_STAR:
8208 case DIMEN_THIS_IMAGE:
8209 gfc_error ("Bad array specification in ALLOCATE statement at %L",
8210 &e->where);
8211 goto failure;
8212 }
8213
8214 check_symbols:
8215 for (a = code->ext.alloc.list; a; a = a->next)
8216 {
8217 sym = a->expr->symtree->n.sym;
8218
8219 /* TODO - check derived type components. */
8220 if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
8221 continue;
8222
8223 if ((ar->start[i] != NULL
8224 && gfc_find_sym_in_expr (sym, ar->start[i]))
8225 || (ar->end[i] != NULL
8226 && gfc_find_sym_in_expr (sym, ar->end[i])))
8227 {
8228 gfc_error ("%qs must not appear in the array specification at "
8229 "%L in the same ALLOCATE statement where it is "
8230 "itself allocated", sym->name, &ar->where);
8231 goto failure;
8232 }
8233 }
8234 }
8235
8236 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
8237 {
8238 if (ar->dimen_type[i] == DIMEN_ELEMENT
8239 || ar->dimen_type[i] == DIMEN_RANGE)
8240 {
8241 if (i == (ar->dimen + ar->codimen - 1))
8242 {
8243 gfc_error ("Expected %<*%> in coindex specification in ALLOCATE "
8244 "statement at %L", &e->where);
8245 goto failure;
8246 }
8247 continue;
8248 }
8249
8250 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
8251 && ar->stride[i] == NULL)
8252 break;
8253
8254 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
8255 &e->where);
8256 goto failure;
8257 }
8258
8259 success:
8260 return true;
8261
8262 failure:
8263 return false;
8264 }
8265
8266
8267 static void
8268 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
8269 {
8270 gfc_expr *stat, *errmsg, *pe, *qe;
8271 gfc_alloc *a, *p, *q;
8272
8273 stat = code->expr1;
8274 errmsg = code->expr2;
8275
8276 /* Check the stat variable. */
8277 if (stat)
8278 {
8279 if (!gfc_check_vardef_context (stat, false, false, false,
8280 _("STAT variable")))
8281 goto done_stat;
8282
8283 if (stat->ts.type != BT_INTEGER
8284 || stat->rank > 0)
8285 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
8286 "variable", &stat->where);
8287
8288 if (stat->expr_type == EXPR_CONSTANT || stat->symtree == NULL)
8289 goto done_stat;
8290
8291 /* F2018:9.7.4: The stat-variable shall not be allocated or deallocated
8292 * within the ALLOCATE or DEALLOCATE statement in which it appears ...
8293 */
8294 for (p = code->ext.alloc.list; p; p = p->next)
8295 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
8296 {
8297 gfc_ref *ref1, *ref2;
8298 bool found = true;
8299
8300 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
8301 ref1 = ref1->next, ref2 = ref2->next)
8302 {
8303 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
8304 continue;
8305 if (ref1->u.c.component->name != ref2->u.c.component->name)
8306 {
8307 found = false;
8308 break;
8309 }
8310 }
8311
8312 if (found)
8313 {
8314 gfc_error ("Stat-variable at %L shall not be %sd within "
8315 "the same %s statement", &stat->where, fcn, fcn);
8316 break;
8317 }
8318 }
8319 }
8320
8321 done_stat:
8322
8323 /* Check the errmsg variable. */
8324 if (errmsg)
8325 {
8326 if (!stat)
8327 gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
8328 &errmsg->where);
8329
8330 if (!gfc_check_vardef_context (errmsg, false, false, false,
8331 _("ERRMSG variable")))
8332 goto done_errmsg;
8333
8334 /* F18:R928 alloc-opt is ERRMSG = errmsg-variable
8335 F18:R930 errmsg-variable is scalar-default-char-variable
8336 F18:R906 default-char-variable is variable
8337 F18:C906 default-char-variable shall be default character. */
8338 if (errmsg->ts.type != BT_CHARACTER
8339 || errmsg->rank > 0
8340 || errmsg->ts.kind != gfc_default_character_kind)
8341 gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
8342 "variable", &errmsg->where);
8343
8344 if (errmsg->expr_type == EXPR_CONSTANT || errmsg->symtree == NULL)
8345 goto done_errmsg;
8346
8347 /* F2018:9.7.5: The errmsg-variable shall not be allocated or deallocated
8348 * within the ALLOCATE or DEALLOCATE statement in which it appears ...
8349 */
8350 for (p = code->ext.alloc.list; p; p = p->next)
8351 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
8352 {
8353 gfc_ref *ref1, *ref2;
8354 bool found = true;
8355
8356 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
8357 ref1 = ref1->next, ref2 = ref2->next)
8358 {
8359 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
8360 continue;
8361 if (ref1->u.c.component->name != ref2->u.c.component->name)
8362 {
8363 found = false;
8364 break;
8365 }
8366 }
8367
8368 if (found)
8369 {
8370 gfc_error ("Errmsg-variable at %L shall not be %sd within "
8371 "the same %s statement", &errmsg->where, fcn, fcn);
8372 break;
8373 }
8374 }
8375 }
8376
8377 done_errmsg:
8378
8379 /* Check that an allocate-object appears only once in the statement. */
8380
8381 for (p = code->ext.alloc.list; p; p = p->next)
8382 {
8383 pe = p->expr;
8384 for (q = p->next; q; q = q->next)
8385 {
8386 qe = q->expr;
8387 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
8388 {
8389 /* This is a potential collision. */
8390 gfc_ref *pr = pe->ref;
8391 gfc_ref *qr = qe->ref;
8392
8393 /* Follow the references until
8394 a) They start to differ, in which case there is no error;
8395 you can deallocate a%b and a%c in a single statement
8396 b) Both of them stop, which is an error
8397 c) One of them stops, which is also an error. */
8398 while (1)
8399 {
8400 if (pr == NULL && qr == NULL)
8401 {
8402 gfc_error ("Allocate-object at %L also appears at %L",
8403 &pe->where, &qe->where);
8404 break;
8405 }
8406 else if (pr != NULL && qr == NULL)
8407 {
8408 gfc_error ("Allocate-object at %L is subobject of"
8409 " object at %L", &pe->where, &qe->where);
8410 break;
8411 }
8412 else if (pr == NULL && qr != NULL)
8413 {
8414 gfc_error ("Allocate-object at %L is subobject of"
8415 " object at %L", &qe->where, &pe->where);
8416 break;
8417 }
8418 /* Here, pr != NULL && qr != NULL */
8419 gcc_assert(pr->type == qr->type);
8420 if (pr->type == REF_ARRAY)
8421 {
8422 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
8423 which are legal. */
8424 gcc_assert (qr->type == REF_ARRAY);
8425
8426 if (pr->next && qr->next)
8427 {
8428 int i;
8429 gfc_array_ref *par = &(pr->u.ar);
8430 gfc_array_ref *qar = &(qr->u.ar);
8431
8432 for (i=0; i<par->dimen; i++)
8433 {
8434 if ((par->start[i] != NULL
8435 || qar->start[i] != NULL)
8436 && gfc_dep_compare_expr (par->start[i],
8437 qar->start[i]) != 0)
8438 goto break_label;
8439 }
8440 }
8441 }
8442 else
8443 {
8444 if (pr->u.c.component->name != qr->u.c.component->name)
8445 break;
8446 }
8447
8448 pr = pr->next;
8449 qr = qr->next;
8450 }
8451 break_label:
8452 ;
8453 }
8454 }
8455 }
8456
8457 if (strcmp (fcn, "ALLOCATE") == 0)
8458 {
8459 bool arr_alloc_wo_spec = false;
8460
8461 /* Resolving the expr3 in the loop over all objects to allocate would
8462 execute loop invariant code for each loop item. Therefore do it just
8463 once here. */
8464 if (code->expr3 && code->expr3->mold
8465 && code->expr3->ts.type == BT_DERIVED)
8466 {
8467 /* Default initialization via MOLD (non-polymorphic). */
8468 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
8469 if (rhs != NULL)
8470 {
8471 gfc_resolve_expr (rhs);
8472 gfc_free_expr (code->expr3);
8473 code->expr3 = rhs;
8474 }
8475 }
8476 for (a = code->ext.alloc.list; a; a = a->next)
8477 resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
8478
8479 if (arr_alloc_wo_spec && code->expr3)
8480 {
8481 /* Mark the allocate to have to take the array specification
8482 from the expr3. */
8483 code->ext.alloc.arr_spec_from_expr3 = 1;
8484 }
8485 }
8486 else
8487 {
8488 for (a = code->ext.alloc.list; a; a = a->next)
8489 resolve_deallocate_expr (a->expr);
8490 }
8491 }
8492
8493
8494 /************ SELECT CASE resolution subroutines ************/
8495
8496 /* Callback function for our mergesort variant. Determines interval
8497 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
8498 op1 > op2. Assumes we're not dealing with the default case.
8499 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
8500 There are nine situations to check. */
8501
8502 static int
8503 compare_cases (const gfc_case *op1, const gfc_case *op2)
8504 {
8505 int retval;
8506
8507 if (op1->low == NULL) /* op1 = (:L) */
8508 {
8509 /* op2 = (:N), so overlap. */
8510 retval = 0;
8511 /* op2 = (M:) or (M:N), L < M */
8512 if (op2->low != NULL
8513 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8514 retval = -1;
8515 }
8516 else if (op1->high == NULL) /* op1 = (K:) */
8517 {
8518 /* op2 = (M:), so overlap. */
8519 retval = 0;
8520 /* op2 = (:N) or (M:N), K > N */
8521 if (op2->high != NULL
8522 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8523 retval = 1;
8524 }
8525 else /* op1 = (K:L) */
8526 {
8527 if (op2->low == NULL) /* op2 = (:N), K > N */
8528 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8529 ? 1 : 0;
8530 else if (op2->high == NULL) /* op2 = (M:), L < M */
8531 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8532 ? -1 : 0;
8533 else /* op2 = (M:N) */
8534 {
8535 retval = 0;
8536 /* L < M */
8537 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8538 retval = -1;
8539 /* K > N */
8540 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8541 retval = 1;
8542 }
8543 }
8544
8545 return retval;
8546 }
8547
8548
8549 /* Merge-sort a double linked case list, detecting overlap in the
8550 process. LIST is the head of the double linked case list before it
8551 is sorted. Returns the head of the sorted list if we don't see any
8552 overlap, or NULL otherwise. */
8553
8554 static gfc_case *
8555 check_case_overlap (gfc_case *list)
8556 {
8557 gfc_case *p, *q, *e, *tail;
8558 int insize, nmerges, psize, qsize, cmp, overlap_seen;
8559
8560 /* If the passed list was empty, return immediately. */
8561 if (!list)
8562 return NULL;
8563
8564 overlap_seen = 0;
8565 insize = 1;
8566
8567 /* Loop unconditionally. The only exit from this loop is a return
8568 statement, when we've finished sorting the case list. */
8569 for (;;)
8570 {
8571 p = list;
8572 list = NULL;
8573 tail = NULL;
8574
8575 /* Count the number of merges we do in this pass. */
8576 nmerges = 0;
8577
8578 /* Loop while there exists a merge to be done. */
8579 while (p)
8580 {
8581 int i;
8582
8583 /* Count this merge. */
8584 nmerges++;
8585
8586 /* Cut the list in two pieces by stepping INSIZE places
8587 forward in the list, starting from P. */
8588 psize = 0;
8589 q = p;
8590 for (i = 0; i < insize; i++)
8591 {
8592 psize++;
8593 q = q->right;
8594 if (!q)
8595 break;
8596 }
8597 qsize = insize;
8598
8599 /* Now we have two lists. Merge them! */
8600 while (psize > 0 || (qsize > 0 && q != NULL))
8601 {
8602 /* See from which the next case to merge comes from. */
8603 if (psize == 0)
8604 {
8605 /* P is empty so the next case must come from Q. */
8606 e = q;
8607 q = q->right;
8608 qsize--;
8609 }
8610 else if (qsize == 0 || q == NULL)
8611 {
8612 /* Q is empty. */
8613 e = p;
8614 p = p->right;
8615 psize--;
8616 }
8617 else
8618 {
8619 cmp = compare_cases (p, q);
8620 if (cmp < 0)
8621 {
8622 /* The whole case range for P is less than the
8623 one for Q. */
8624 e = p;
8625 p = p->right;
8626 psize--;
8627 }
8628 else if (cmp > 0)
8629 {
8630 /* The whole case range for Q is greater than
8631 the case range for P. */
8632 e = q;
8633 q = q->right;
8634 qsize--;
8635 }
8636 else
8637 {
8638 /* The cases overlap, or they are the same
8639 element in the list. Either way, we must
8640 issue an error and get the next case from P. */
8641 /* FIXME: Sort P and Q by line number. */
8642 gfc_error ("CASE label at %L overlaps with CASE "
8643 "label at %L", &p->where, &q->where);
8644 overlap_seen = 1;
8645 e = p;
8646 p = p->right;
8647 psize--;
8648 }
8649 }
8650
8651 /* Add the next element to the merged list. */
8652 if (tail)
8653 tail->right = e;
8654 else
8655 list = e;
8656 e->left = tail;
8657 tail = e;
8658 }
8659
8660 /* P has now stepped INSIZE places along, and so has Q. So
8661 they're the same. */
8662 p = q;
8663 }
8664 tail->right = NULL;
8665
8666 /* If we have done only one merge or none at all, we've
8667 finished sorting the cases. */
8668 if (nmerges <= 1)
8669 {
8670 if (!overlap_seen)
8671 return list;
8672 else
8673 return NULL;
8674 }
8675
8676 /* Otherwise repeat, merging lists twice the size. */
8677 insize *= 2;
8678 }
8679 }
8680
8681
8682 /* Check to see if an expression is suitable for use in a CASE statement.
8683 Makes sure that all case expressions are scalar constants of the same
8684 type. Return false if anything is wrong. */
8685
8686 static bool
8687 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
8688 {
8689 if (e == NULL) return true;
8690
8691 if (e->ts.type != case_expr->ts.type)
8692 {
8693 gfc_error ("Expression in CASE statement at %L must be of type %s",
8694 &e->where, gfc_basic_typename (case_expr->ts.type));
8695 return false;
8696 }
8697
8698 /* C805 (R808) For a given case-construct, each case-value shall be of
8699 the same type as case-expr. For character type, length differences
8700 are allowed, but the kind type parameters shall be the same. */
8701
8702 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
8703 {
8704 gfc_error ("Expression in CASE statement at %L must be of kind %d",
8705 &e->where, case_expr->ts.kind);
8706 return false;
8707 }
8708
8709 /* Convert the case value kind to that of case expression kind,
8710 if needed */
8711
8712 if (e->ts.kind != case_expr->ts.kind)
8713 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
8714
8715 if (e->rank != 0)
8716 {
8717 gfc_error ("Expression in CASE statement at %L must be scalar",
8718 &e->where);
8719 return false;
8720 }
8721
8722 return true;
8723 }
8724
8725
8726 /* Given a completely parsed select statement, we:
8727
8728 - Validate all expressions and code within the SELECT.
8729 - Make sure that the selection expression is not of the wrong type.
8730 - Make sure that no case ranges overlap.
8731 - Eliminate unreachable cases and unreachable code resulting from
8732 removing case labels.
8733
8734 The standard does allow unreachable cases, e.g. CASE (5:3). But
8735 they are a hassle for code generation, and to prevent that, we just
8736 cut them out here. This is not necessary for overlapping cases
8737 because they are illegal and we never even try to generate code.
8738
8739 We have the additional caveat that a SELECT construct could have
8740 been a computed GOTO in the source code. Fortunately we can fairly
8741 easily work around that here: The case_expr for a "real" SELECT CASE
8742 is in code->expr1, but for a computed GOTO it is in code->expr2. All
8743 we have to do is make sure that the case_expr is a scalar integer
8744 expression. */
8745
8746 static void
8747 resolve_select (gfc_code *code, bool select_type)
8748 {
8749 gfc_code *body;
8750 gfc_expr *case_expr;
8751 gfc_case *cp, *default_case, *tail, *head;
8752 int seen_unreachable;
8753 int seen_logical;
8754 int ncases;
8755 bt type;
8756 bool t;
8757
8758 if (code->expr1 == NULL)
8759 {
8760 /* This was actually a computed GOTO statement. */
8761 case_expr = code->expr2;
8762 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
8763 gfc_error ("Selection expression in computed GOTO statement "
8764 "at %L must be a scalar integer expression",
8765 &case_expr->where);
8766
8767 /* Further checking is not necessary because this SELECT was built
8768 by the compiler, so it should always be OK. Just move the
8769 case_expr from expr2 to expr so that we can handle computed
8770 GOTOs as normal SELECTs from here on. */
8771 code->expr1 = code->expr2;
8772 code->expr2 = NULL;
8773 return;
8774 }
8775
8776 case_expr = code->expr1;
8777 type = case_expr->ts.type;
8778
8779 /* F08:C830. */
8780 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
8781 {
8782 gfc_error ("Argument of SELECT statement at %L cannot be %s",
8783 &case_expr->where, gfc_typename (case_expr));
8784
8785 /* Punt. Going on here just produce more garbage error messages. */
8786 return;
8787 }
8788
8789 /* F08:R842. */
8790 if (!select_type && case_expr->rank != 0)
8791 {
8792 gfc_error ("Argument of SELECT statement at %L must be a scalar "
8793 "expression", &case_expr->where);
8794
8795 /* Punt. */
8796 return;
8797 }
8798
8799 /* Raise a warning if an INTEGER case value exceeds the range of
8800 the case-expr. Later, all expressions will be promoted to the
8801 largest kind of all case-labels. */
8802
8803 if (type == BT_INTEGER)
8804 for (body = code->block; body; body = body->block)
8805 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8806 {
8807 if (cp->low
8808 && gfc_check_integer_range (cp->low->value.integer,
8809 case_expr->ts.kind) != ARITH_OK)
8810 gfc_warning (0, "Expression in CASE statement at %L is "
8811 "not in the range of %s", &cp->low->where,
8812 gfc_typename (case_expr));
8813
8814 if (cp->high
8815 && cp->low != cp->high
8816 && gfc_check_integer_range (cp->high->value.integer,
8817 case_expr->ts.kind) != ARITH_OK)
8818 gfc_warning (0, "Expression in CASE statement at %L is "
8819 "not in the range of %s", &cp->high->where,
8820 gfc_typename (case_expr));
8821 }
8822
8823 /* PR 19168 has a long discussion concerning a mismatch of the kinds
8824 of the SELECT CASE expression and its CASE values. Walk the lists
8825 of case values, and if we find a mismatch, promote case_expr to
8826 the appropriate kind. */
8827
8828 if (type == BT_LOGICAL || type == BT_INTEGER)
8829 {
8830 for (body = code->block; body; body = body->block)
8831 {
8832 /* Walk the case label list. */
8833 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8834 {
8835 /* Intercept the DEFAULT case. It does not have a kind. */
8836 if (cp->low == NULL && cp->high == NULL)
8837 continue;
8838
8839 /* Unreachable case ranges are discarded, so ignore. */
8840 if (cp->low != NULL && cp->high != NULL
8841 && cp->low != cp->high
8842 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8843 continue;
8844
8845 if (cp->low != NULL
8846 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
8847 gfc_convert_type_warn (case_expr, &cp->low->ts, 1, 0);
8848
8849 if (cp->high != NULL
8850 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
8851 gfc_convert_type_warn (case_expr, &cp->high->ts, 1, 0);
8852 }
8853 }
8854 }
8855
8856 /* Assume there is no DEFAULT case. */
8857 default_case = NULL;
8858 head = tail = NULL;
8859 ncases = 0;
8860 seen_logical = 0;
8861
8862 for (body = code->block; body; body = body->block)
8863 {
8864 /* Assume the CASE list is OK, and all CASE labels can be matched. */
8865 t = true;
8866 seen_unreachable = 0;
8867
8868 /* Walk the case label list, making sure that all case labels
8869 are legal. */
8870 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8871 {
8872 /* Count the number of cases in the whole construct. */
8873 ncases++;
8874
8875 /* Intercept the DEFAULT case. */
8876 if (cp->low == NULL && cp->high == NULL)
8877 {
8878 if (default_case != NULL)
8879 {
8880 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8881 "by a second DEFAULT CASE at %L",
8882 &default_case->where, &cp->where);
8883 t = false;
8884 break;
8885 }
8886 else
8887 {
8888 default_case = cp;
8889 continue;
8890 }
8891 }
8892
8893 /* Deal with single value cases and case ranges. Errors are
8894 issued from the validation function. */
8895 if (!validate_case_label_expr (cp->low, case_expr)
8896 || !validate_case_label_expr (cp->high, case_expr))
8897 {
8898 t = false;
8899 break;
8900 }
8901
8902 if (type == BT_LOGICAL
8903 && ((cp->low == NULL || cp->high == NULL)
8904 || cp->low != cp->high))
8905 {
8906 gfc_error ("Logical range in CASE statement at %L is not "
8907 "allowed",
8908 cp->low ? &cp->low->where : &cp->high->where);
8909 t = false;
8910 break;
8911 }
8912
8913 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
8914 {
8915 int value;
8916 value = cp->low->value.logical == 0 ? 2 : 1;
8917 if (value & seen_logical)
8918 {
8919 gfc_error ("Constant logical value in CASE statement "
8920 "is repeated at %L",
8921 &cp->low->where);
8922 t = false;
8923 break;
8924 }
8925 seen_logical |= value;
8926 }
8927
8928 if (cp->low != NULL && cp->high != NULL
8929 && cp->low != cp->high
8930 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8931 {
8932 if (warn_surprising)
8933 gfc_warning (OPT_Wsurprising,
8934 "Range specification at %L can never be matched",
8935 &cp->where);
8936
8937 cp->unreachable = 1;
8938 seen_unreachable = 1;
8939 }
8940 else
8941 {
8942 /* If the case range can be matched, it can also overlap with
8943 other cases. To make sure it does not, we put it in a
8944 double linked list here. We sort that with a merge sort
8945 later on to detect any overlapping cases. */
8946 if (!head)
8947 {
8948 head = tail = cp;
8949 head->right = head->left = NULL;
8950 }
8951 else
8952 {
8953 tail->right = cp;
8954 tail->right->left = tail;
8955 tail = tail->right;
8956 tail->right = NULL;
8957 }
8958 }
8959 }
8960
8961 /* It there was a failure in the previous case label, give up
8962 for this case label list. Continue with the next block. */
8963 if (!t)
8964 continue;
8965
8966 /* See if any case labels that are unreachable have been seen.
8967 If so, we eliminate them. This is a bit of a kludge because
8968 the case lists for a single case statement (label) is a
8969 single forward linked lists. */
8970 if (seen_unreachable)
8971 {
8972 /* Advance until the first case in the list is reachable. */
8973 while (body->ext.block.case_list != NULL
8974 && body->ext.block.case_list->unreachable)
8975 {
8976 gfc_case *n = body->ext.block.case_list;
8977 body->ext.block.case_list = body->ext.block.case_list->next;
8978 n->next = NULL;
8979 gfc_free_case_list (n);
8980 }
8981
8982 /* Strip all other unreachable cases. */
8983 if (body->ext.block.case_list)
8984 {
8985 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
8986 {
8987 if (cp->next->unreachable)
8988 {
8989 gfc_case *n = cp->next;
8990 cp->next = cp->next->next;
8991 n->next = NULL;
8992 gfc_free_case_list (n);
8993 }
8994 }
8995 }
8996 }
8997 }
8998
8999 /* See if there were overlapping cases. If the check returns NULL,
9000 there was overlap. In that case we don't do anything. If head
9001 is non-NULL, we prepend the DEFAULT case. The sorted list can
9002 then used during code generation for SELECT CASE constructs with
9003 a case expression of a CHARACTER type. */
9004 if (head)
9005 {
9006 head = check_case_overlap (head);
9007
9008 /* Prepend the default_case if it is there. */
9009 if (head != NULL && default_case)
9010 {
9011 default_case->left = NULL;
9012 default_case->right = head;
9013 head->left = default_case;
9014 }
9015 }
9016
9017 /* Eliminate dead blocks that may be the result if we've seen
9018 unreachable case labels for a block. */
9019 for (body = code; body && body->block; body = body->block)
9020 {
9021 if (body->block->ext.block.case_list == NULL)
9022 {
9023 /* Cut the unreachable block from the code chain. */
9024 gfc_code *c = body->block;
9025 body->block = c->block;
9026
9027 /* Kill the dead block, but not the blocks below it. */
9028 c->block = NULL;
9029 gfc_free_statements (c);
9030 }
9031 }
9032
9033 /* More than two cases is legal but insane for logical selects.
9034 Issue a warning for it. */
9035 if (warn_surprising && type == BT_LOGICAL && ncases > 2)
9036 gfc_warning (OPT_Wsurprising,
9037 "Logical SELECT CASE block at %L has more that two cases",
9038 &code->loc);
9039 }
9040
9041
9042 /* Check if a derived type is extensible. */
9043
9044 bool
9045 gfc_type_is_extensible (gfc_symbol *sym)
9046 {
9047 return !(sym->attr.is_bind_c || sym->attr.sequence
9048 || (sym->attr.is_class
9049 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
9050 }
9051
9052
9053 static void
9054 resolve_types (gfc_namespace *ns);
9055
9056 /* Resolve an associate-name: Resolve target and ensure the type-spec is
9057 correct as well as possibly the array-spec. */
9058
9059 static void
9060 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
9061 {
9062 gfc_expr* target;
9063
9064 gcc_assert (sym->assoc);
9065 gcc_assert (sym->attr.flavor == FL_VARIABLE);
9066
9067 /* If this is for SELECT TYPE, the target may not yet be set. In that
9068 case, return. Resolution will be called later manually again when
9069 this is done. */
9070 target = sym->assoc->target;
9071 if (!target)
9072 return;
9073 gcc_assert (!sym->assoc->dangling);
9074
9075 if (resolve_target && !gfc_resolve_expr (target))
9076 return;
9077
9078 /* For variable targets, we get some attributes from the target. */
9079 if (target->expr_type == EXPR_VARIABLE)
9080 {
9081 gfc_symbol *tsym, *dsym;
9082
9083 gcc_assert (target->symtree);
9084 tsym = target->symtree->n.sym;
9085
9086 if (gfc_expr_attr (target).proc_pointer)
9087 {
9088 gfc_error ("Associating entity %qs at %L is a procedure pointer",
9089 tsym->name, &target->where);
9090 return;
9091 }
9092
9093 if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
9094 && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
9095 && dsym->attr.flavor == FL_DERIVED)
9096 {
9097 gfc_error ("Derived type %qs cannot be used as a variable at %L",
9098 tsym->name, &target->where);
9099 return;
9100 }
9101
9102 if (tsym->attr.flavor == FL_PROCEDURE)
9103 {
9104 bool is_error = true;
9105 if (tsym->attr.function && tsym->result == tsym)
9106 for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
9107 if (tsym == ns->proc_name)
9108 {
9109 is_error = false;
9110 break;
9111 }
9112 if (is_error)
9113 {
9114 gfc_error ("Associating entity %qs at %L is a procedure name",
9115 tsym->name, &target->where);
9116 return;
9117 }
9118 }
9119
9120 sym->attr.asynchronous = tsym->attr.asynchronous;
9121 sym->attr.volatile_ = tsym->attr.volatile_;
9122
9123 sym->attr.target = tsym->attr.target
9124 || gfc_expr_attr (target).pointer;
9125 if (is_subref_array (target))
9126 sym->attr.subref_array_pointer = 1;
9127 }
9128 else if (target->ts.type == BT_PROCEDURE)
9129 {
9130 gfc_error ("Associating selector-expression at %L yields a procedure",
9131 &target->where);
9132 return;
9133 }
9134
9135 if (target->expr_type == EXPR_NULL)
9136 {
9137 gfc_error ("Selector at %L cannot be NULL()", &target->where);
9138 return;
9139 }
9140 else if (target->ts.type == BT_UNKNOWN)
9141 {
9142 gfc_error ("Selector at %L has no type", &target->where);
9143 return;
9144 }
9145
9146 /* Get type if this was not already set. Note that it can be
9147 some other type than the target in case this is a SELECT TYPE
9148 selector! So we must not update when the type is already there. */
9149 if (sym->ts.type == BT_UNKNOWN)
9150 sym->ts = target->ts;
9151
9152 gcc_assert (sym->ts.type != BT_UNKNOWN);
9153
9154 /* See if this is a valid association-to-variable. */
9155 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
9156 && !gfc_has_vector_subscript (target));
9157
9158 /* Finally resolve if this is an array or not. */
9159 if (sym->attr.dimension && target->rank == 0)
9160 {
9161 /* primary.cc makes the assumption that a reference to an associate
9162 name followed by a left parenthesis is an array reference. */
9163 if (sym->ts.type != BT_CHARACTER)
9164 gfc_error ("Associate-name %qs at %L is used as array",
9165 sym->name, &sym->declared_at);
9166 sym->attr.dimension = 0;
9167 return;
9168 }
9169
9170
9171 /* We cannot deal with class selectors that need temporaries. */
9172 if (target->ts.type == BT_CLASS
9173 && gfc_ref_needs_temporary_p (target->ref))
9174 {
9175 gfc_error ("CLASS selector at %L needs a temporary which is not "
9176 "yet implemented", &target->where);
9177 return;
9178 }
9179
9180 if (target->ts.type == BT_CLASS)
9181 gfc_fix_class_refs (target);
9182
9183 if (target->rank != 0 && !sym->attr.select_rank_temporary)
9184 {
9185 gfc_array_spec *as;
9186 /* The rank may be incorrectly guessed at parsing, therefore make sure
9187 it is corrected now. */
9188 if (sym->ts.type != BT_CLASS && (!sym->as || sym->assoc->rankguessed))
9189 {
9190 if (!sym->as)
9191 sym->as = gfc_get_array_spec ();
9192 as = sym->as;
9193 as->rank = target->rank;
9194 as->type = AS_DEFERRED;
9195 as->corank = gfc_get_corank (target);
9196 sym->attr.dimension = 1;
9197 if (as->corank != 0)
9198 sym->attr.codimension = 1;
9199 }
9200 else if (sym->ts.type == BT_CLASS
9201 && CLASS_DATA (sym)
9202 && (!CLASS_DATA (sym)->as || sym->assoc->rankguessed))
9203 {
9204 if (!CLASS_DATA (sym)->as)
9205 CLASS_DATA (sym)->as = gfc_get_array_spec ();
9206 as = CLASS_DATA (sym)->as;
9207 as->rank = target->rank;
9208 as->type = AS_DEFERRED;
9209 as->corank = gfc_get_corank (target);
9210 CLASS_DATA (sym)->attr.dimension = 1;
9211 if (as->corank != 0)
9212 CLASS_DATA (sym)->attr.codimension = 1;
9213 }
9214 }
9215 else if (!sym->attr.select_rank_temporary)
9216 {
9217 /* target's rank is 0, but the type of the sym is still array valued,
9218 which has to be corrected. */
9219 if (sym->ts.type == BT_CLASS && sym->ts.u.derived
9220 && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
9221 {
9222 gfc_array_spec *as;
9223 symbol_attribute attr;
9224 /* The associated variable's type is still the array type
9225 correct this now. */
9226 gfc_typespec *ts = &target->ts;
9227 gfc_ref *ref;
9228 gfc_component *c;
9229 for (ref = target->ref; ref != NULL; ref = ref->next)
9230 {
9231 switch (ref->type)
9232 {
9233 case REF_COMPONENT:
9234 ts = &ref->u.c.component->ts;
9235 break;
9236 case REF_ARRAY:
9237 if (ts->type == BT_CLASS)
9238 ts = &ts->u.derived->components->ts;
9239 break;
9240 default:
9241 break;
9242 }
9243 }
9244 /* Create a scalar instance of the current class type. Because the
9245 rank of a class array goes into its name, the type has to be
9246 rebuild. The alternative of (re-)setting just the attributes
9247 and as in the current type, destroys the type also in other
9248 places. */
9249 as = NULL;
9250 sym->ts = *ts;
9251 sym->ts.type = BT_CLASS;
9252 attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
9253 attr.class_ok = 0;
9254 attr.associate_var = 1;
9255 attr.dimension = attr.codimension = 0;
9256 attr.class_pointer = 1;
9257 if (!gfc_build_class_symbol (&sym->ts, &attr, &as))
9258 gcc_unreachable ();
9259 /* Make sure the _vptr is set. */
9260 c = gfc_find_component (sym->ts.u.derived, "_vptr", true, true, NULL);
9261 if (c->ts.u.derived == NULL)
9262 c->ts.u.derived = gfc_find_derived_vtab (sym->ts.u.derived);
9263 CLASS_DATA (sym)->attr.pointer = 1;
9264 CLASS_DATA (sym)->attr.class_pointer = 1;
9265 gfc_set_sym_referenced (sym->ts.u.derived);
9266 gfc_commit_symbol (sym->ts.u.derived);
9267 /* _vptr now has the _vtab in it, change it to the _vtype. */
9268 if (c->ts.u.derived->attr.vtab)
9269 c->ts.u.derived = c->ts.u.derived->ts.u.derived;
9270 c->ts.u.derived->ns->types_resolved = 0;
9271 resolve_types (c->ts.u.derived->ns);
9272 }
9273 }
9274
9275 /* Mark this as an associate variable. */
9276 sym->attr.associate_var = 1;
9277
9278 /* Fix up the type-spec for CHARACTER types. */
9279 if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
9280 {
9281 if (!sym->ts.u.cl)
9282 sym->ts.u.cl = target->ts.u.cl;
9283
9284 if (sym->ts.deferred
9285 && sym->ts.u.cl == target->ts.u.cl)
9286 {
9287 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
9288 sym->ts.deferred = 1;
9289 }
9290
9291 if (!sym->ts.u.cl->length
9292 && !sym->ts.deferred
9293 && target->expr_type == EXPR_CONSTANT)
9294 {
9295 sym->ts.u.cl->length =
9296 gfc_get_int_expr (gfc_charlen_int_kind, NULL,
9297 target->value.character.length);
9298 }
9299 else if ((!sym->ts.u.cl->length
9300 || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
9301 && target->expr_type != EXPR_VARIABLE)
9302 {
9303 if (!sym->ts.deferred)
9304 {
9305 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
9306 sym->ts.deferred = 1;
9307 }
9308
9309 /* This is reset in trans-stmt.cc after the assignment
9310 of the target expression to the associate name. */
9311 sym->attr.allocatable = 1;
9312 }
9313 }
9314
9315 /* If the target is a good class object, so is the associate variable. */
9316 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
9317 sym->attr.class_ok = 1;
9318 }
9319
9320
9321 /* Ensure that SELECT TYPE expressions have the correct rank and a full
9322 array reference, where necessary. The symbols are artificial and so
9323 the dimension attribute and arrayspec can also be set. In addition,
9324 sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
9325 This is corrected here as well.*/
9326
9327 static void
9328 fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2,
9329 int rank, gfc_ref *ref)
9330 {
9331 gfc_ref *nref = (*expr1)->ref;
9332 gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
9333 gfc_symbol *sym2 = expr2 ? expr2->symtree->n.sym : NULL;
9334 (*expr1)->rank = rank;
9335 if (sym1->ts.type == BT_CLASS)
9336 {
9337 if ((*expr1)->ts.type != BT_CLASS)
9338 (*expr1)->ts = sym1->ts;
9339
9340 CLASS_DATA (sym1)->attr.dimension = 1;
9341 if (CLASS_DATA (sym1)->as == NULL && sym2)
9342 CLASS_DATA (sym1)->as
9343 = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
9344 }
9345 else
9346 {
9347 sym1->attr.dimension = 1;
9348 if (sym1->as == NULL && sym2)
9349 sym1->as = gfc_copy_array_spec (sym2->as);
9350 }
9351
9352 for (; nref; nref = nref->next)
9353 if (nref->next == NULL)
9354 break;
9355
9356 if (ref && nref && nref->type != REF_ARRAY)
9357 nref->next = gfc_copy_ref (ref);
9358 else if (ref && !nref)
9359 (*expr1)->ref = gfc_copy_ref (ref);
9360 }
9361
9362
9363 static gfc_expr *
9364 build_loc_call (gfc_expr *sym_expr)
9365 {
9366 gfc_expr *loc_call;
9367 loc_call = gfc_get_expr ();
9368 loc_call->expr_type = EXPR_FUNCTION;
9369 gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
9370 loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
9371 loc_call->symtree->n.sym->attr.intrinsic = 1;
9372 loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
9373 gfc_commit_symbol (loc_call->symtree->n.sym);
9374 loc_call->ts.type = BT_INTEGER;
9375 loc_call->ts.kind = gfc_index_integer_kind;
9376 loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
9377 loc_call->value.function.actual = gfc_get_actual_arglist ();
9378 loc_call->value.function.actual->expr = sym_expr;
9379 loc_call->where = sym_expr->where;
9380 return loc_call;
9381 }
9382
9383 /* Resolve a SELECT TYPE statement. */
9384
9385 static void
9386 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
9387 {
9388 gfc_symbol *selector_type;
9389 gfc_code *body, *new_st, *if_st, *tail;
9390 gfc_code *class_is = NULL, *default_case = NULL;
9391 gfc_case *c;
9392 gfc_symtree *st;
9393 char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
9394 gfc_namespace *ns;
9395 int error = 0;
9396 int rank = 0;
9397 gfc_ref* ref = NULL;
9398 gfc_expr *selector_expr = NULL;
9399
9400 ns = code->ext.block.ns;
9401 gfc_resolve (ns);
9402
9403 /* Check for F03:C813. */
9404 if (code->expr1->ts.type != BT_CLASS
9405 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
9406 {
9407 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
9408 "at %L", &code->loc);
9409 return;
9410 }
9411
9412 if (!code->expr1->symtree->n.sym->attr.class_ok)
9413 return;
9414
9415 if (code->expr2)
9416 {
9417 gfc_ref *ref2 = NULL;
9418 for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
9419 if (ref->type == REF_COMPONENT
9420 && ref->u.c.component->ts.type == BT_CLASS)
9421 ref2 = ref;
9422
9423 if (ref2)
9424 {
9425 if (code->expr1->symtree->n.sym->attr.untyped)
9426 code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
9427 selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
9428 }
9429 else
9430 {
9431 if (code->expr1->symtree->n.sym->attr.untyped)
9432 code->expr1->symtree->n.sym->ts = code->expr2->ts;
9433 selector_type = CLASS_DATA (code->expr2)
9434 ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
9435 }
9436
9437 if (code->expr2->rank
9438 && code->expr1->ts.type == BT_CLASS
9439 && CLASS_DATA (code->expr1)->as)
9440 CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
9441
9442 /* F2008: C803 The selector expression must not be coindexed. */
9443 if (gfc_is_coindexed (code->expr2))
9444 {
9445 gfc_error ("Selector at %L must not be coindexed",
9446 &code->expr2->where);
9447 return;
9448 }
9449
9450 }
9451 else
9452 {
9453 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
9454
9455 if (gfc_is_coindexed (code->expr1))
9456 {
9457 gfc_error ("Selector at %L must not be coindexed",
9458 &code->expr1->where);
9459 return;
9460 }
9461 }
9462
9463 /* Loop over TYPE IS / CLASS IS cases. */
9464 for (body = code->block; body; body = body->block)
9465 {
9466 c = body->ext.block.case_list;
9467
9468 if (!error)
9469 {
9470 /* Check for repeated cases. */
9471 for (tail = code->block; tail; tail = tail->block)
9472 {
9473 gfc_case *d = tail->ext.block.case_list;
9474 if (tail == body)
9475 break;
9476
9477 if (c->ts.type == d->ts.type
9478 && ((c->ts.type == BT_DERIVED
9479 && c->ts.u.derived && d->ts.u.derived
9480 && !strcmp (c->ts.u.derived->name,
9481 d->ts.u.derived->name))
9482 || c->ts.type == BT_UNKNOWN
9483 || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9484 && c->ts.kind == d->ts.kind)))
9485 {
9486 gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
9487 &c->where, &d->where);
9488 return;
9489 }
9490 }
9491 }
9492
9493 /* Check F03:C815. */
9494 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9495 && selector_type
9496 && !selector_type->attr.unlimited_polymorphic
9497 && !gfc_type_is_extensible (c->ts.u.derived))
9498 {
9499 gfc_error ("Derived type %qs at %L must be extensible",
9500 c->ts.u.derived->name, &c->where);
9501 error++;
9502 continue;
9503 }
9504
9505 /* Check F03:C816. */
9506 if (c->ts.type != BT_UNKNOWN
9507 && selector_type && !selector_type->attr.unlimited_polymorphic
9508 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
9509 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
9510 {
9511 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9512 gfc_error ("Derived type %qs at %L must be an extension of %qs",
9513 c->ts.u.derived->name, &c->where, selector_type->name);
9514 else
9515 gfc_error ("Unexpected intrinsic type %qs at %L",
9516 gfc_basic_typename (c->ts.type), &c->where);
9517 error++;
9518 continue;
9519 }
9520
9521 /* Check F03:C814. */
9522 if (c->ts.type == BT_CHARACTER
9523 && (c->ts.u.cl->length != NULL || c->ts.deferred))
9524 {
9525 gfc_error ("The type-spec at %L shall specify that each length "
9526 "type parameter is assumed", &c->where);
9527 error++;
9528 continue;
9529 }
9530
9531 /* Intercept the DEFAULT case. */
9532 if (c->ts.type == BT_UNKNOWN)
9533 {
9534 /* Check F03:C818. */
9535 if (default_case)
9536 {
9537 gfc_error ("The DEFAULT CASE at %L cannot be followed "
9538 "by a second DEFAULT CASE at %L",
9539 &default_case->ext.block.case_list->where, &c->where);
9540 error++;
9541 continue;
9542 }
9543
9544 default_case = body;
9545 }
9546 }
9547
9548 if (error > 0)
9549 return;
9550
9551 /* Transform SELECT TYPE statement to BLOCK and associate selector to
9552 target if present. If there are any EXIT statements referring to the
9553 SELECT TYPE construct, this is no problem because the gfc_code
9554 reference stays the same and EXIT is equally possible from the BLOCK
9555 it is changed to. */
9556 code->op = EXEC_BLOCK;
9557 if (code->expr2)
9558 {
9559 gfc_association_list* assoc;
9560
9561 assoc = gfc_get_association_list ();
9562 assoc->st = code->expr1->symtree;
9563 assoc->target = gfc_copy_expr (code->expr2);
9564 assoc->target->where = code->expr2->where;
9565 /* assoc->variable will be set by resolve_assoc_var. */
9566
9567 code->ext.block.assoc = assoc;
9568 code->expr1->symtree->n.sym->assoc = assoc;
9569
9570 resolve_assoc_var (code->expr1->symtree->n.sym, false);
9571 }
9572 else
9573 code->ext.block.assoc = NULL;
9574
9575 /* Ensure that the selector rank and arrayspec are available to
9576 correct expressions in which they might be missing. */
9577 if (code->expr2 && code->expr2->rank)
9578 {
9579 rank = code->expr2->rank;
9580 for (ref = code->expr2->ref; ref; ref = ref->next)
9581 if (ref->next == NULL)
9582 break;
9583 if (ref && ref->type == REF_ARRAY)
9584 ref = gfc_copy_ref (ref);
9585
9586 /* Fixup expr1 if necessary. */
9587 if (rank)
9588 fixup_array_ref (&code->expr1, code->expr2, rank, ref);
9589 }
9590 else if (code->expr1->rank)
9591 {
9592 rank = code->expr1->rank;
9593 for (ref = code->expr1->ref; ref; ref = ref->next)
9594 if (ref->next == NULL)
9595 break;
9596 if (ref && ref->type == REF_ARRAY)
9597 ref = gfc_copy_ref (ref);
9598 }
9599
9600 /* Add EXEC_SELECT to switch on type. */
9601 new_st = gfc_get_code (code->op);
9602 new_st->expr1 = code->expr1;
9603 new_st->expr2 = code->expr2;
9604 new_st->block = code->block;
9605 code->expr1 = code->expr2 = NULL;
9606 code->block = NULL;
9607 if (!ns->code)
9608 ns->code = new_st;
9609 else
9610 ns->code->next = new_st;
9611 code = new_st;
9612 code->op = EXEC_SELECT_TYPE;
9613
9614 /* Use the intrinsic LOC function to generate an integer expression
9615 for the vtable of the selector. Note that the rank of the selector
9616 expression has to be set to zero. */
9617 gfc_add_vptr_component (code->expr1);
9618 code->expr1->rank = 0;
9619 code->expr1 = build_loc_call (code->expr1);
9620 selector_expr = code->expr1->value.function.actual->expr;
9621
9622 /* Loop over TYPE IS / CLASS IS cases. */
9623 for (body = code->block; body; body = body->block)
9624 {
9625 gfc_symbol *vtab;
9626 gfc_expr *e;
9627 c = body->ext.block.case_list;
9628
9629 /* Generate an index integer expression for address of the
9630 TYPE/CLASS vtable and store it in c->low. The hash expression
9631 is stored in c->high and is used to resolve intrinsic cases. */
9632 if (c->ts.type != BT_UNKNOWN)
9633 {
9634 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9635 {
9636 vtab = gfc_find_derived_vtab (c->ts.u.derived);
9637 gcc_assert (vtab);
9638 c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
9639 c->ts.u.derived->hash_value);
9640 }
9641 else
9642 {
9643 vtab = gfc_find_vtab (&c->ts);
9644 gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
9645 e = CLASS_DATA (vtab)->initializer;
9646 c->high = gfc_copy_expr (e);
9647 if (c->high->ts.kind != gfc_integer_4_kind)
9648 {
9649 gfc_typespec ts;
9650 ts.kind = gfc_integer_4_kind;
9651 ts.type = BT_INTEGER;
9652 gfc_convert_type_warn (c->high, &ts, 2, 0);
9653 }
9654 }
9655
9656 e = gfc_lval_expr_from_sym (vtab);
9657 c->low = build_loc_call (e);
9658 }
9659 else
9660 continue;
9661
9662 /* Associate temporary to selector. This should only be done
9663 when this case is actually true, so build a new ASSOCIATE
9664 that does precisely this here (instead of using the
9665 'global' one). */
9666
9667 if (c->ts.type == BT_CLASS)
9668 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
9669 else if (c->ts.type == BT_DERIVED)
9670 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
9671 else if (c->ts.type == BT_CHARACTER)
9672 {
9673 HOST_WIDE_INT charlen = 0;
9674 if (c->ts.u.cl && c->ts.u.cl->length
9675 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9676 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
9677 snprintf (name, sizeof (name),
9678 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
9679 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
9680 }
9681 else
9682 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
9683 c->ts.kind);
9684
9685 st = gfc_find_symtree (ns->sym_root, name);
9686 gcc_assert (st->n.sym->assoc);
9687 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
9688 st->n.sym->assoc->target->where = selector_expr->where;
9689 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
9690 {
9691 gfc_add_data_component (st->n.sym->assoc->target);
9692 /* Fixup the target expression if necessary. */
9693 if (rank)
9694 fixup_array_ref (&st->n.sym->assoc->target, NULL, rank, ref);
9695 }
9696
9697 new_st = gfc_get_code (EXEC_BLOCK);
9698 new_st->ext.block.ns = gfc_build_block_ns (ns);
9699 new_st->ext.block.ns->code = body->next;
9700 body->next = new_st;
9701
9702 /* Chain in the new list only if it is marked as dangling. Otherwise
9703 there is a CASE label overlap and this is already used. Just ignore,
9704 the error is diagnosed elsewhere. */
9705 if (st->n.sym->assoc->dangling)
9706 {
9707 new_st->ext.block.assoc = st->n.sym->assoc;
9708 st->n.sym->assoc->dangling = 0;
9709 }
9710
9711 resolve_assoc_var (st->n.sym, false);
9712 }
9713
9714 /* Take out CLASS IS cases for separate treatment. */
9715 body = code;
9716 while (body && body->block)
9717 {
9718 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
9719 {
9720 /* Add to class_is list. */
9721 if (class_is == NULL)
9722 {
9723 class_is = body->block;
9724 tail = class_is;
9725 }
9726 else
9727 {
9728 for (tail = class_is; tail->block; tail = tail->block) ;
9729 tail->block = body->block;
9730 tail = tail->block;
9731 }
9732 /* Remove from EXEC_SELECT list. */
9733 body->block = body->block->block;
9734 tail->block = NULL;
9735 }
9736 else
9737 body = body->block;
9738 }
9739
9740 if (class_is)
9741 {
9742 gfc_symbol *vtab;
9743
9744 if (!default_case)
9745 {
9746 /* Add a default case to hold the CLASS IS cases. */
9747 for (tail = code; tail->block; tail = tail->block) ;
9748 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
9749 tail = tail->block;
9750 tail->ext.block.case_list = gfc_get_case ();
9751 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
9752 tail->next = NULL;
9753 default_case = tail;
9754 }
9755
9756 /* More than one CLASS IS block? */
9757 if (class_is->block)
9758 {
9759 gfc_code **c1,*c2;
9760 bool swapped;
9761 /* Sort CLASS IS blocks by extension level. */
9762 do
9763 {
9764 swapped = false;
9765 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
9766 {
9767 c2 = (*c1)->block;
9768 /* F03:C817 (check for doubles). */
9769 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
9770 == c2->ext.block.case_list->ts.u.derived->hash_value)
9771 {
9772 gfc_error ("Double CLASS IS block in SELECT TYPE "
9773 "statement at %L",
9774 &c2->ext.block.case_list->where);
9775 return;
9776 }
9777 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
9778 < c2->ext.block.case_list->ts.u.derived->attr.extension)
9779 {
9780 /* Swap. */
9781 (*c1)->block = c2->block;
9782 c2->block = *c1;
9783 *c1 = c2;
9784 swapped = true;
9785 }
9786 }
9787 }
9788 while (swapped);
9789 }
9790
9791 /* Generate IF chain. */
9792 if_st = gfc_get_code (EXEC_IF);
9793 new_st = if_st;
9794 for (body = class_is; body; body = body->block)
9795 {
9796 new_st->block = gfc_get_code (EXEC_IF);
9797 new_st = new_st->block;
9798 /* Set up IF condition: Call _gfortran_is_extension_of. */
9799 new_st->expr1 = gfc_get_expr ();
9800 new_st->expr1->expr_type = EXPR_FUNCTION;
9801 new_st->expr1->ts.type = BT_LOGICAL;
9802 new_st->expr1->ts.kind = 4;
9803 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
9804 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
9805 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
9806 /* Set up arguments. */
9807 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
9808 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
9809 new_st->expr1->value.function.actual->expr->where = code->loc;
9810 new_st->expr1->where = code->loc;
9811 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
9812 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
9813 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
9814 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
9815 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
9816 new_st->expr1->value.function.actual->next->expr->where = code->loc;
9817 /* Set up types in formal arg list. */
9818 new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
9819 new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
9820 new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
9821 new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
9822
9823 new_st->next = body->next;
9824 }
9825 if (default_case->next)
9826 {
9827 new_st->block = gfc_get_code (EXEC_IF);
9828 new_st = new_st->block;
9829 new_st->next = default_case->next;
9830 }
9831
9832 /* Replace CLASS DEFAULT code by the IF chain. */
9833 default_case->next = if_st;
9834 }
9835
9836 /* Resolve the internal code. This cannot be done earlier because
9837 it requires that the sym->assoc of selectors is set already. */
9838 gfc_current_ns = ns;
9839 gfc_resolve_blocks (code->block, gfc_current_ns);
9840 gfc_current_ns = old_ns;
9841
9842 if (ref)
9843 free (ref);
9844 }
9845
9846
9847 /* Resolve a SELECT RANK statement. */
9848
9849 static void
9850 resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
9851 {
9852 gfc_namespace *ns;
9853 gfc_code *body, *new_st, *tail;
9854 gfc_case *c;
9855 char tname[GFC_MAX_SYMBOL_LEN + 7];
9856 char name[2 * GFC_MAX_SYMBOL_LEN];
9857 gfc_symtree *st;
9858 gfc_expr *selector_expr = NULL;
9859 int case_value;
9860 HOST_WIDE_INT charlen = 0;
9861
9862 ns = code->ext.block.ns;
9863 gfc_resolve (ns);
9864
9865 code->op = EXEC_BLOCK;
9866 if (code->expr2)
9867 {
9868 gfc_association_list* assoc;
9869
9870 assoc = gfc_get_association_list ();
9871 assoc->st = code->expr1->symtree;
9872 assoc->target = gfc_copy_expr (code->expr2);
9873 assoc->target->where = code->expr2->where;
9874 /* assoc->variable will be set by resolve_assoc_var. */
9875
9876 code->ext.block.assoc = assoc;
9877 code->expr1->symtree->n.sym->assoc = assoc;
9878
9879 resolve_assoc_var (code->expr1->symtree->n.sym, false);
9880 }
9881 else
9882 code->ext.block.assoc = NULL;
9883
9884 /* Loop over RANK cases. Note that returning on the errors causes a
9885 cascade of further errors because the case blocks do not compile
9886 correctly. */
9887 for (body = code->block; body; body = body->block)
9888 {
9889 c = body->ext.block.case_list;
9890 if (c->low)
9891 case_value = (int) mpz_get_si (c->low->value.integer);
9892 else
9893 case_value = -2;
9894
9895 /* Check for repeated cases. */
9896 for (tail = code->block; tail; tail = tail->block)
9897 {
9898 gfc_case *d = tail->ext.block.case_list;
9899 int case_value2;
9900
9901 if (tail == body)
9902 break;
9903
9904 /* Check F2018: C1153. */
9905 if (!c->low && !d->low)
9906 gfc_error ("RANK DEFAULT at %L is repeated at %L",
9907 &c->where, &d->where);
9908
9909 if (!c->low || !d->low)
9910 continue;
9911
9912 /* Check F2018: C1153. */
9913 case_value2 = (int) mpz_get_si (d->low->value.integer);
9914 if ((case_value == case_value2) && case_value == -1)
9915 gfc_error ("RANK (*) at %L is repeated at %L",
9916 &c->where, &d->where);
9917 else if (case_value == case_value2)
9918 gfc_error ("RANK (%i) at %L is repeated at %L",
9919 case_value, &c->where, &d->where);
9920 }
9921
9922 if (!c->low)
9923 continue;
9924
9925 /* Check F2018: C1155. */
9926 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
9927 || gfc_expr_attr (code->expr1).pointer))
9928 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9929 "allocatable selector at %L", &c->where, &code->expr1->where);
9930
9931 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
9932 || gfc_expr_attr (code->expr1).pointer))
9933 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9934 "allocatable selector at %L", &c->where, &code->expr1->where);
9935 }
9936
9937 /* Add EXEC_SELECT to switch on rank. */
9938 new_st = gfc_get_code (code->op);
9939 new_st->expr1 = code->expr1;
9940 new_st->expr2 = code->expr2;
9941 new_st->block = code->block;
9942 code->expr1 = code->expr2 = NULL;
9943 code->block = NULL;
9944 if (!ns->code)
9945 ns->code = new_st;
9946 else
9947 ns->code->next = new_st;
9948 code = new_st;
9949 code->op = EXEC_SELECT_RANK;
9950
9951 selector_expr = code->expr1;
9952
9953 /* Loop over SELECT RANK cases. */
9954 for (body = code->block; body; body = body->block)
9955 {
9956 c = body->ext.block.case_list;
9957 int case_value;
9958
9959 /* Pass on the default case. */
9960 if (c->low == NULL)
9961 continue;
9962
9963 /* Associate temporary to selector. This should only be done
9964 when this case is actually true, so build a new ASSOCIATE
9965 that does precisely this here (instead of using the
9966 'global' one). */
9967 if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
9968 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9969 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
9970
9971 if (c->ts.type == BT_CLASS)
9972 sprintf (tname, "class_%s", c->ts.u.derived->name);
9973 else if (c->ts.type == BT_DERIVED)
9974 sprintf (tname, "type_%s", c->ts.u.derived->name);
9975 else if (c->ts.type != BT_CHARACTER)
9976 sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
9977 else
9978 sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
9979 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
9980
9981 case_value = (int) mpz_get_si (c->low->value.integer);
9982 if (case_value >= 0)
9983 sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
9984 else
9985 sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
9986
9987 st = gfc_find_symtree (ns->sym_root, name);
9988 gcc_assert (st->n.sym->assoc);
9989
9990 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
9991 st->n.sym->assoc->target->where = selector_expr->where;
9992
9993 new_st = gfc_get_code (EXEC_BLOCK);
9994 new_st->ext.block.ns = gfc_build_block_ns (ns);
9995 new_st->ext.block.ns->code = body->next;
9996 body->next = new_st;
9997
9998 /* Chain in the new list only if it is marked as dangling. Otherwise
9999 there is a CASE label overlap and this is already used. Just ignore,
10000 the error is diagnosed elsewhere. */
10001 if (st->n.sym->assoc->dangling)
10002 {
10003 new_st->ext.block.assoc = st->n.sym->assoc;
10004 st->n.sym->assoc->dangling = 0;
10005 }
10006
10007 resolve_assoc_var (st->n.sym, false);
10008 }
10009
10010 gfc_current_ns = ns;
10011 gfc_resolve_blocks (code->block, gfc_current_ns);
10012 gfc_current_ns = old_ns;
10013 }
10014
10015
10016 /* Resolve a transfer statement. This is making sure that:
10017 -- a derived type being transferred has only non-pointer components
10018 -- a derived type being transferred doesn't have private components, unless
10019 it's being transferred from the module where the type was defined
10020 -- we're not trying to transfer a whole assumed size array. */
10021
10022 static void
10023 resolve_transfer (gfc_code *code)
10024 {
10025 gfc_symbol *sym, *derived;
10026 gfc_ref *ref;
10027 gfc_expr *exp;
10028 bool write = false;
10029 bool formatted = false;
10030 gfc_dt *dt = code->ext.dt;
10031 gfc_symbol *dtio_sub = NULL;
10032
10033 exp = code->expr1;
10034
10035 while (exp != NULL && exp->expr_type == EXPR_OP
10036 && exp->value.op.op == INTRINSIC_PARENTHESES)
10037 exp = exp->value.op.op1;
10038
10039 if (exp && exp->expr_type == EXPR_NULL
10040 && code->ext.dt)
10041 {
10042 gfc_error ("Invalid context for NULL () intrinsic at %L",
10043 &exp->where);
10044 return;
10045 }
10046
10047 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
10048 && exp->expr_type != EXPR_FUNCTION
10049 && exp->expr_type != EXPR_ARRAY
10050 && exp->expr_type != EXPR_STRUCTURE))
10051 return;
10052
10053 /* If we are reading, the variable will be changed. Note that
10054 code->ext.dt may be NULL if the TRANSFER is related to
10055 an INQUIRE statement -- but in this case, we are not reading, either. */
10056 if (dt && dt->dt_io_kind->value.iokind == M_READ
10057 && !gfc_check_vardef_context (exp, false, false, false,
10058 _("item in READ")))
10059 return;
10060
10061 const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
10062 || exp->expr_type == EXPR_FUNCTION
10063 || exp->expr_type == EXPR_ARRAY
10064 ? &exp->ts : &exp->symtree->n.sym->ts;
10065
10066 /* Go to actual component transferred. */
10067 for (ref = exp->ref; ref; ref = ref->next)
10068 if (ref->type == REF_COMPONENT)
10069 ts = &ref->u.c.component->ts;
10070
10071 if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
10072 && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
10073 {
10074 derived = ts->u.derived;
10075
10076 /* Determine when to use the formatted DTIO procedure. */
10077 if (dt && (dt->format_expr || dt->format_label))
10078 formatted = true;
10079
10080 write = dt->dt_io_kind->value.iokind == M_WRITE
10081 || dt->dt_io_kind->value.iokind == M_PRINT;
10082 dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
10083
10084 if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
10085 {
10086 dt->udtio = exp;
10087 sym = exp->symtree->n.sym->ns->proc_name;
10088 /* Check to see if this is a nested DTIO call, with the
10089 dummy as the io-list object. */
10090 if (sym && sym == dtio_sub && sym->formal
10091 && sym->formal->sym == exp->symtree->n.sym
10092 && exp->ref == NULL)
10093 {
10094 if (!sym->attr.recursive)
10095 {
10096 gfc_error ("DTIO %s procedure at %L must be recursive",
10097 sym->name, &sym->declared_at);
10098 return;
10099 }
10100 }
10101 }
10102 }
10103
10104 if (ts->type == BT_CLASS && dtio_sub == NULL)
10105 {
10106 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
10107 "it is processed by a defined input/output procedure",
10108 &code->loc);
10109 return;
10110 }
10111
10112 if (ts->type == BT_DERIVED)
10113 {
10114 /* Check that transferred derived type doesn't contain POINTER
10115 components unless it is processed by a defined input/output
10116 procedure". */
10117 if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
10118 {
10119 gfc_error ("Data transfer element at %L cannot have POINTER "
10120 "components unless it is processed by a defined "
10121 "input/output procedure", &code->loc);
10122 return;
10123 }
10124
10125 /* F08:C935. */
10126 if (ts->u.derived->attr.proc_pointer_comp)
10127 {
10128 gfc_error ("Data transfer element at %L cannot have "
10129 "procedure pointer components", &code->loc);
10130 return;
10131 }
10132
10133 if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
10134 {
10135 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
10136 "components unless it is processed by a defined "
10137 "input/output procedure", &code->loc);
10138 return;
10139 }
10140
10141 /* C_PTR and C_FUNPTR have private components which means they cannot
10142 be printed. However, if -std=gnu and not -pedantic, allow
10143 the component to be printed to help debugging. */
10144 if (ts->u.derived->ts.f90_type == BT_VOID)
10145 {
10146 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
10147 "cannot have PRIVATE components", &code->loc))
10148 return;
10149 }
10150 else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
10151 {
10152 gfc_error ("Data transfer element at %L cannot have "
10153 "PRIVATE components unless it is processed by "
10154 "a defined input/output procedure", &code->loc);
10155 return;
10156 }
10157 }
10158
10159 if (exp->expr_type == EXPR_STRUCTURE)
10160 return;
10161
10162 if (exp->expr_type == EXPR_ARRAY)
10163 return;
10164
10165 sym = exp->symtree->n.sym;
10166
10167 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
10168 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
10169 {
10170 gfc_error ("Data transfer element at %L cannot be a full reference to "
10171 "an assumed-size array", &code->loc);
10172 return;
10173 }
10174 }
10175
10176
10177 /*********** Toplevel code resolution subroutines ***********/
10178
10179 /* Find the set of labels that are reachable from this block. We also
10180 record the last statement in each block. */
10181
10182 static void
10183 find_reachable_labels (gfc_code *block)
10184 {
10185 gfc_code *c;
10186
10187 if (!block)
10188 return;
10189
10190 cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
10191
10192 /* Collect labels in this block. We don't keep those corresponding
10193 to END {IF|SELECT}, these are checked in resolve_branch by going
10194 up through the code_stack. */
10195 for (c = block; c; c = c->next)
10196 {
10197 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
10198 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
10199 }
10200
10201 /* Merge with labels from parent block. */
10202 if (cs_base->prev)
10203 {
10204 gcc_assert (cs_base->prev->reachable_labels);
10205 bitmap_ior_into (cs_base->reachable_labels,
10206 cs_base->prev->reachable_labels);
10207 }
10208 }
10209
10210
10211 static void
10212 resolve_lock_unlock_event (gfc_code *code)
10213 {
10214 if (code->expr1->expr_type == EXPR_FUNCTION
10215 && code->expr1->value.function.isym
10216 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
10217 remove_caf_get_intrinsic (code->expr1);
10218
10219 if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
10220 && (code->expr1->ts.type != BT_DERIVED
10221 || code->expr1->expr_type != EXPR_VARIABLE
10222 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
10223 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
10224 || code->expr1->rank != 0
10225 || (!gfc_is_coarray (code->expr1) &&
10226 !gfc_is_coindexed (code->expr1))))
10227 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
10228 &code->expr1->where);
10229 else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
10230 && (code->expr1->ts.type != BT_DERIVED
10231 || code->expr1->expr_type != EXPR_VARIABLE
10232 || code->expr1->ts.u.derived->from_intmod
10233 != INTMOD_ISO_FORTRAN_ENV
10234 || code->expr1->ts.u.derived->intmod_sym_id
10235 != ISOFORTRAN_EVENT_TYPE
10236 || code->expr1->rank != 0))
10237 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
10238 &code->expr1->where);
10239 else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
10240 && !gfc_is_coindexed (code->expr1))
10241 gfc_error ("Event variable argument at %L must be a coarray or coindexed",
10242 &code->expr1->where);
10243 else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
10244 gfc_error ("Event variable argument at %L must be a coarray but not "
10245 "coindexed", &code->expr1->where);
10246
10247 /* Check STAT. */
10248 if (code->expr2
10249 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
10250 || code->expr2->expr_type != EXPR_VARIABLE))
10251 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10252 &code->expr2->where);
10253
10254 if (code->expr2
10255 && !gfc_check_vardef_context (code->expr2, false, false, false,
10256 _("STAT variable")))
10257 return;
10258
10259 /* Check ERRMSG. */
10260 if (code->expr3
10261 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
10262 || code->expr3->expr_type != EXPR_VARIABLE))
10263 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10264 &code->expr3->where);
10265
10266 if (code->expr3
10267 && !gfc_check_vardef_context (code->expr3, false, false, false,
10268 _("ERRMSG variable")))
10269 return;
10270
10271 /* Check for LOCK the ACQUIRED_LOCK. */
10272 if (code->op != EXEC_EVENT_WAIT && code->expr4
10273 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
10274 || code->expr4->expr_type != EXPR_VARIABLE))
10275 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
10276 "variable", &code->expr4->where);
10277
10278 if (code->op != EXEC_EVENT_WAIT && code->expr4
10279 && !gfc_check_vardef_context (code->expr4, false, false, false,
10280 _("ACQUIRED_LOCK variable")))
10281 return;
10282
10283 /* Check for EVENT WAIT the UNTIL_COUNT. */
10284 if (code->op == EXEC_EVENT_WAIT && code->expr4)
10285 {
10286 if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
10287 || code->expr4->rank != 0)
10288 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
10289 "expression", &code->expr4->where);
10290 }
10291 }
10292
10293
10294 static void
10295 resolve_critical (gfc_code *code)
10296 {
10297 gfc_symtree *symtree;
10298 gfc_symbol *lock_type;
10299 char name[GFC_MAX_SYMBOL_LEN];
10300 static int serial = 0;
10301
10302 if (flag_coarray != GFC_FCOARRAY_LIB)
10303 return;
10304
10305 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
10306 GFC_PREFIX ("lock_type"));
10307 if (symtree)
10308 lock_type = symtree->n.sym;
10309 else
10310 {
10311 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
10312 false) != 0)
10313 gcc_unreachable ();
10314 lock_type = symtree->n.sym;
10315 lock_type->attr.flavor = FL_DERIVED;
10316 lock_type->attr.zero_comp = 1;
10317 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
10318 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
10319 }
10320
10321 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
10322 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
10323 gcc_unreachable ();
10324
10325 code->resolved_sym = symtree->n.sym;
10326 symtree->n.sym->attr.flavor = FL_VARIABLE;
10327 symtree->n.sym->attr.referenced = 1;
10328 symtree->n.sym->attr.artificial = 1;
10329 symtree->n.sym->attr.codimension = 1;
10330 symtree->n.sym->ts.type = BT_DERIVED;
10331 symtree->n.sym->ts.u.derived = lock_type;
10332 symtree->n.sym->as = gfc_get_array_spec ();
10333 symtree->n.sym->as->corank = 1;
10334 symtree->n.sym->as->type = AS_EXPLICIT;
10335 symtree->n.sym->as->cotype = AS_EXPLICIT;
10336 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
10337 NULL, 1);
10338 gfc_commit_symbols();
10339 }
10340
10341
10342 static void
10343 resolve_sync (gfc_code *code)
10344 {
10345 /* Check imageset. The * case matches expr1 == NULL. */
10346 if (code->expr1)
10347 {
10348 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
10349 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
10350 "INTEGER expression", &code->expr1->where);
10351 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
10352 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
10353 gfc_error ("Imageset argument at %L must between 1 and num_images()",
10354 &code->expr1->where);
10355 else if (code->expr1->expr_type == EXPR_ARRAY
10356 && gfc_simplify_expr (code->expr1, 0))
10357 {
10358 gfc_constructor *cons;
10359 cons = gfc_constructor_first (code->expr1->value.constructor);
10360 for (; cons; cons = gfc_constructor_next (cons))
10361 if (cons->expr->expr_type == EXPR_CONSTANT
10362 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
10363 gfc_error ("Imageset argument at %L must between 1 and "
10364 "num_images()", &cons->expr->where);
10365 }
10366 }
10367
10368 /* Check STAT. */
10369 gfc_resolve_expr (code->expr2);
10370 if (code->expr2)
10371 {
10372 if (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0)
10373 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10374 &code->expr2->where);
10375 else
10376 gfc_check_vardef_context (code->expr2, false, false, false,
10377 _("STAT variable"));
10378 }
10379
10380 /* Check ERRMSG. */
10381 gfc_resolve_expr (code->expr3);
10382 if (code->expr3)
10383 {
10384 if (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0)
10385 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10386 &code->expr3->where);
10387 else
10388 gfc_check_vardef_context (code->expr3, false, false, false,
10389 _("ERRMSG variable"));
10390 }
10391 }
10392
10393
10394 /* Given a branch to a label, see if the branch is conforming.
10395 The code node describes where the branch is located. */
10396
10397 static void
10398 resolve_branch (gfc_st_label *label, gfc_code *code)
10399 {
10400 code_stack *stack;
10401
10402 if (label == NULL)
10403 return;
10404
10405 /* Step one: is this a valid branching target? */
10406
10407 if (label->defined == ST_LABEL_UNKNOWN)
10408 {
10409 gfc_error ("Label %d referenced at %L is never defined", label->value,
10410 &code->loc);
10411 return;
10412 }
10413
10414 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
10415 {
10416 gfc_error ("Statement at %L is not a valid branch target statement "
10417 "for the branch statement at %L", &label->where, &code->loc);
10418 return;
10419 }
10420
10421 /* Step two: make sure this branch is not a branch to itself ;-) */
10422
10423 if (code->here == label)
10424 {
10425 gfc_warning (0,
10426 "Branch at %L may result in an infinite loop", &code->loc);
10427 return;
10428 }
10429
10430 /* Step three: See if the label is in the same block as the
10431 branching statement. The hard work has been done by setting up
10432 the bitmap reachable_labels. */
10433
10434 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
10435 {
10436 /* Check now whether there is a CRITICAL construct; if so, check
10437 whether the label is still visible outside of the CRITICAL block,
10438 which is invalid. */
10439 for (stack = cs_base; stack; stack = stack->prev)
10440 {
10441 if (stack->current->op == EXEC_CRITICAL
10442 && bitmap_bit_p (stack->reachable_labels, label->value))
10443 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
10444 "label at %L", &code->loc, &label->where);
10445 else if (stack->current->op == EXEC_DO_CONCURRENT
10446 && bitmap_bit_p (stack->reachable_labels, label->value))
10447 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
10448 "for label at %L", &code->loc, &label->where);
10449 }
10450
10451 return;
10452 }
10453
10454 /* Step four: If we haven't found the label in the bitmap, it may
10455 still be the label of the END of the enclosing block, in which
10456 case we find it by going up the code_stack. */
10457
10458 for (stack = cs_base; stack; stack = stack->prev)
10459 {
10460 if (stack->current->next && stack->current->next->here == label)
10461 break;
10462 if (stack->current->op == EXEC_CRITICAL)
10463 {
10464 /* Note: A label at END CRITICAL does not leave the CRITICAL
10465 construct as END CRITICAL is still part of it. */
10466 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
10467 " at %L", &code->loc, &label->where);
10468 return;
10469 }
10470 else if (stack->current->op == EXEC_DO_CONCURRENT)
10471 {
10472 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
10473 "label at %L", &code->loc, &label->where);
10474 return;
10475 }
10476 }
10477
10478 if (stack)
10479 {
10480 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
10481 return;
10482 }
10483
10484 /* The label is not in an enclosing block, so illegal. This was
10485 allowed in Fortran 66, so we allow it as extension. No
10486 further checks are necessary in this case. */
10487 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
10488 "as the GOTO statement at %L", &label->where,
10489 &code->loc);
10490 return;
10491 }
10492
10493
10494 /* Check whether EXPR1 has the same shape as EXPR2. */
10495
10496 static bool
10497 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
10498 {
10499 mpz_t shape[GFC_MAX_DIMENSIONS];
10500 mpz_t shape2[GFC_MAX_DIMENSIONS];
10501 bool result = false;
10502 int i;
10503
10504 /* Compare the rank. */
10505 if (expr1->rank != expr2->rank)
10506 return result;
10507
10508 /* Compare the size of each dimension. */
10509 for (i=0; i<expr1->rank; i++)
10510 {
10511 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
10512 goto ignore;
10513
10514 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
10515 goto ignore;
10516
10517 if (mpz_cmp (shape[i], shape2[i]))
10518 goto over;
10519 }
10520
10521 /* When either of the two expression is an assumed size array, we
10522 ignore the comparison of dimension sizes. */
10523 ignore:
10524 result = true;
10525
10526 over:
10527 gfc_clear_shape (shape, i);
10528 gfc_clear_shape (shape2, i);
10529 return result;
10530 }
10531
10532
10533 /* Check whether a WHERE assignment target or a WHERE mask expression
10534 has the same shape as the outmost WHERE mask expression. */
10535
10536 static void
10537 resolve_where (gfc_code *code, gfc_expr *mask)
10538 {
10539 gfc_code *cblock;
10540 gfc_code *cnext;
10541 gfc_expr *e = NULL;
10542
10543 cblock = code->block;
10544
10545 /* Store the first WHERE mask-expr of the WHERE statement or construct.
10546 In case of nested WHERE, only the outmost one is stored. */
10547 if (mask == NULL) /* outmost WHERE */
10548 e = cblock->expr1;
10549 else /* inner WHERE */
10550 e = mask;
10551
10552 while (cblock)
10553 {
10554 if (cblock->expr1)
10555 {
10556 /* Check if the mask-expr has a consistent shape with the
10557 outmost WHERE mask-expr. */
10558 if (!resolve_where_shape (cblock->expr1, e))
10559 gfc_error ("WHERE mask at %L has inconsistent shape",
10560 &cblock->expr1->where);
10561 }
10562
10563 /* the assignment statement of a WHERE statement, or the first
10564 statement in where-body-construct of a WHERE construct */
10565 cnext = cblock->next;
10566 while (cnext)
10567 {
10568 switch (cnext->op)
10569 {
10570 /* WHERE assignment statement */
10571 case EXEC_ASSIGN:
10572
10573 /* Check shape consistent for WHERE assignment target. */
10574 if (e && !resolve_where_shape (cnext->expr1, e))
10575 gfc_error ("WHERE assignment target at %L has "
10576 "inconsistent shape", &cnext->expr1->where);
10577
10578 if (cnext->op == EXEC_ASSIGN
10579 && gfc_may_be_finalized (cnext->expr1->ts))
10580 cnext->expr1->must_finalize = 1;
10581
10582 break;
10583
10584
10585 case EXEC_ASSIGN_CALL:
10586 resolve_call (cnext);
10587 if (!cnext->resolved_sym->attr.elemental)
10588 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10589 &cnext->ext.actual->expr->where);
10590 break;
10591
10592 /* WHERE or WHERE construct is part of a where-body-construct */
10593 case EXEC_WHERE:
10594 resolve_where (cnext, e);
10595 break;
10596
10597 default:
10598 gfc_error ("Unsupported statement inside WHERE at %L",
10599 &cnext->loc);
10600 }
10601 /* the next statement within the same where-body-construct */
10602 cnext = cnext->next;
10603 }
10604 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10605 cblock = cblock->block;
10606 }
10607 }
10608
10609
10610 /* Resolve assignment in FORALL construct.
10611 NVAR is the number of FORALL index variables, and VAR_EXPR records the
10612 FORALL index variables. */
10613
10614 static void
10615 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
10616 {
10617 int n;
10618
10619 for (n = 0; n < nvar; n++)
10620 {
10621 gfc_symbol *forall_index;
10622
10623 forall_index = var_expr[n]->symtree->n.sym;
10624
10625 /* Check whether the assignment target is one of the FORALL index
10626 variable. */
10627 if ((code->expr1->expr_type == EXPR_VARIABLE)
10628 && (code->expr1->symtree->n.sym == forall_index))
10629 gfc_error ("Assignment to a FORALL index variable at %L",
10630 &code->expr1->where);
10631 else
10632 {
10633 /* If one of the FORALL index variables doesn't appear in the
10634 assignment variable, then there could be a many-to-one
10635 assignment. Emit a warning rather than an error because the
10636 mask could be resolving this problem. */
10637 if (!find_forall_index (code->expr1, forall_index, 0))
10638 gfc_warning (0, "The FORALL with index %qs is not used on the "
10639 "left side of the assignment at %L and so might "
10640 "cause multiple assignment to this object",
10641 var_expr[n]->symtree->name, &code->expr1->where);
10642 }
10643 }
10644 }
10645
10646
10647 /* Resolve WHERE statement in FORALL construct. */
10648
10649 static void
10650 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
10651 gfc_expr **var_expr)
10652 {
10653 gfc_code *cblock;
10654 gfc_code *cnext;
10655
10656 cblock = code->block;
10657 while (cblock)
10658 {
10659 /* the assignment statement of a WHERE statement, or the first
10660 statement in where-body-construct of a WHERE construct */
10661 cnext = cblock->next;
10662 while (cnext)
10663 {
10664 switch (cnext->op)
10665 {
10666 /* WHERE assignment statement */
10667 case EXEC_ASSIGN:
10668 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
10669
10670 if (cnext->op == EXEC_ASSIGN
10671 && gfc_may_be_finalized (cnext->expr1->ts))
10672 cnext->expr1->must_finalize = 1;
10673
10674 break;
10675
10676 /* WHERE operator assignment statement */
10677 case EXEC_ASSIGN_CALL:
10678 resolve_call (cnext);
10679 if (!cnext->resolved_sym->attr.elemental)
10680 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10681 &cnext->ext.actual->expr->where);
10682 break;
10683
10684 /* WHERE or WHERE construct is part of a where-body-construct */
10685 case EXEC_WHERE:
10686 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
10687 break;
10688
10689 default:
10690 gfc_error ("Unsupported statement inside WHERE at %L",
10691 &cnext->loc);
10692 }
10693 /* the next statement within the same where-body-construct */
10694 cnext = cnext->next;
10695 }
10696 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10697 cblock = cblock->block;
10698 }
10699 }
10700
10701
10702 /* Traverse the FORALL body to check whether the following errors exist:
10703 1. For assignment, check if a many-to-one assignment happens.
10704 2. For WHERE statement, check the WHERE body to see if there is any
10705 many-to-one assignment. */
10706
10707 static void
10708 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
10709 {
10710 gfc_code *c;
10711
10712 c = code->block->next;
10713 while (c)
10714 {
10715 switch (c->op)
10716 {
10717 case EXEC_ASSIGN:
10718 case EXEC_POINTER_ASSIGN:
10719 gfc_resolve_assign_in_forall (c, nvar, var_expr);
10720
10721 if (c->op == EXEC_ASSIGN
10722 && gfc_may_be_finalized (c->expr1->ts))
10723 c->expr1->must_finalize = 1;
10724
10725 break;
10726
10727 case EXEC_ASSIGN_CALL:
10728 resolve_call (c);
10729 break;
10730
10731 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
10732 there is no need to handle it here. */
10733 case EXEC_FORALL:
10734 break;
10735 case EXEC_WHERE:
10736 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
10737 break;
10738 default:
10739 break;
10740 }
10741 /* The next statement in the FORALL body. */
10742 c = c->next;
10743 }
10744 }
10745
10746
10747 /* Counts the number of iterators needed inside a forall construct, including
10748 nested forall constructs. This is used to allocate the needed memory
10749 in gfc_resolve_forall. */
10750
10751 static int
10752 gfc_count_forall_iterators (gfc_code *code)
10753 {
10754 int max_iters, sub_iters, current_iters;
10755 gfc_forall_iterator *fa;
10756
10757 gcc_assert(code->op == EXEC_FORALL);
10758 max_iters = 0;
10759 current_iters = 0;
10760
10761 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
10762 current_iters ++;
10763
10764 code = code->block->next;
10765
10766 while (code)
10767 {
10768 if (code->op == EXEC_FORALL)
10769 {
10770 sub_iters = gfc_count_forall_iterators (code);
10771 if (sub_iters > max_iters)
10772 max_iters = sub_iters;
10773 }
10774 code = code->next;
10775 }
10776
10777 return current_iters + max_iters;
10778 }
10779
10780
10781 /* Given a FORALL construct, first resolve the FORALL iterator, then call
10782 gfc_resolve_forall_body to resolve the FORALL body. */
10783
10784 static void
10785 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
10786 {
10787 static gfc_expr **var_expr;
10788 static int total_var = 0;
10789 static int nvar = 0;
10790 int i, old_nvar, tmp;
10791 gfc_forall_iterator *fa;
10792
10793 old_nvar = nvar;
10794
10795 if (!gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
10796 return;
10797
10798 /* Start to resolve a FORALL construct */
10799 if (forall_save == 0)
10800 {
10801 /* Count the total number of FORALL indices in the nested FORALL
10802 construct in order to allocate the VAR_EXPR with proper size. */
10803 total_var = gfc_count_forall_iterators (code);
10804
10805 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
10806 var_expr = XCNEWVEC (gfc_expr *, total_var);
10807 }
10808
10809 /* The information about FORALL iterator, including FORALL indices start, end
10810 and stride. An outer FORALL indice cannot appear in start, end or stride. */
10811 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
10812 {
10813 /* Fortran 20008: C738 (R753). */
10814 if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
10815 {
10816 gfc_error ("FORALL index-name at %L must be a scalar variable "
10817 "of type integer", &fa->var->where);
10818 continue;
10819 }
10820
10821 /* Check if any outer FORALL index name is the same as the current
10822 one. */
10823 for (i = 0; i < nvar; i++)
10824 {
10825 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
10826 gfc_error ("An outer FORALL construct already has an index "
10827 "with this name %L", &fa->var->where);
10828 }
10829
10830 /* Record the current FORALL index. */
10831 var_expr[nvar] = gfc_copy_expr (fa->var);
10832
10833 nvar++;
10834
10835 /* No memory leak. */
10836 gcc_assert (nvar <= total_var);
10837 }
10838
10839 /* Resolve the FORALL body. */
10840 gfc_resolve_forall_body (code, nvar, var_expr);
10841
10842 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
10843 gfc_resolve_blocks (code->block, ns);
10844
10845 tmp = nvar;
10846 nvar = old_nvar;
10847 /* Free only the VAR_EXPRs allocated in this frame. */
10848 for (i = nvar; i < tmp; i++)
10849 gfc_free_expr (var_expr[i]);
10850
10851 if (nvar == 0)
10852 {
10853 /* We are in the outermost FORALL construct. */
10854 gcc_assert (forall_save == 0);
10855
10856 /* VAR_EXPR is not needed any more. */
10857 free (var_expr);
10858 total_var = 0;
10859 }
10860 }
10861
10862
10863 /* Resolve a BLOCK construct statement. */
10864 static gfc_expr*
10865 get_temp_from_expr (gfc_expr *, gfc_namespace *);
10866 static gfc_code *
10867 build_assignment (gfc_exec_op, gfc_expr *, gfc_expr *,
10868 gfc_component *, gfc_component *, locus);
10869
10870 static void
10871 resolve_block_construct (gfc_code* code)
10872 {
10873 gfc_namespace *ns = code->ext.block.ns;
10874
10875 /* For an ASSOCIATE block, the associations (and their targets) are already
10876 resolved during resolve_symbol. Resolve the BLOCK's namespace. */
10877 gfc_resolve (ns);
10878 }
10879
10880
10881 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
10882 DO code nodes. */
10883
10884 void
10885 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
10886 {
10887 bool t;
10888
10889 for (; b; b = b->block)
10890 {
10891 t = gfc_resolve_expr (b->expr1);
10892 if (!gfc_resolve_expr (b->expr2))
10893 t = false;
10894
10895 switch (b->op)
10896 {
10897 case EXEC_IF:
10898 if (t && b->expr1 != NULL
10899 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
10900 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
10901 &b->expr1->where);
10902 break;
10903
10904 case EXEC_WHERE:
10905 if (t
10906 && b->expr1 != NULL
10907 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
10908 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
10909 &b->expr1->where);
10910 break;
10911
10912 case EXEC_GOTO:
10913 resolve_branch (b->label1, b);
10914 break;
10915
10916 case EXEC_BLOCK:
10917 resolve_block_construct (b);
10918 break;
10919
10920 case EXEC_SELECT:
10921 case EXEC_SELECT_TYPE:
10922 case EXEC_SELECT_RANK:
10923 case EXEC_FORALL:
10924 case EXEC_DO:
10925 case EXEC_DO_WHILE:
10926 case EXEC_DO_CONCURRENT:
10927 case EXEC_CRITICAL:
10928 case EXEC_READ:
10929 case EXEC_WRITE:
10930 case EXEC_IOLENGTH:
10931 case EXEC_WAIT:
10932 break;
10933
10934 case EXEC_OMP_ATOMIC:
10935 case EXEC_OACC_ATOMIC:
10936 {
10937 /* Verify this before calling gfc_resolve_code, which might
10938 change it. */
10939 gcc_assert (b->op == EXEC_OMP_ATOMIC
10940 || (b->next && b->next->op == EXEC_ASSIGN));
10941 }
10942 break;
10943
10944 case EXEC_OACC_PARALLEL_LOOP:
10945 case EXEC_OACC_PARALLEL:
10946 case EXEC_OACC_KERNELS_LOOP:
10947 case EXEC_OACC_KERNELS:
10948 case EXEC_OACC_SERIAL_LOOP:
10949 case EXEC_OACC_SERIAL:
10950 case EXEC_OACC_DATA:
10951 case EXEC_OACC_HOST_DATA:
10952 case EXEC_OACC_LOOP:
10953 case EXEC_OACC_UPDATE:
10954 case EXEC_OACC_WAIT:
10955 case EXEC_OACC_CACHE:
10956 case EXEC_OACC_ENTER_DATA:
10957 case EXEC_OACC_EXIT_DATA:
10958 case EXEC_OACC_ROUTINE:
10959 case EXEC_OMP_ASSUME:
10960 case EXEC_OMP_CRITICAL:
10961 case EXEC_OMP_DISTRIBUTE:
10962 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
10963 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
10964 case EXEC_OMP_DISTRIBUTE_SIMD:
10965 case EXEC_OMP_DO:
10966 case EXEC_OMP_DO_SIMD:
10967 case EXEC_OMP_ERROR:
10968 case EXEC_OMP_LOOP:
10969 case EXEC_OMP_MASKED:
10970 case EXEC_OMP_MASKED_TASKLOOP:
10971 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
10972 case EXEC_OMP_MASTER:
10973 case EXEC_OMP_MASTER_TASKLOOP:
10974 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
10975 case EXEC_OMP_ORDERED:
10976 case EXEC_OMP_PARALLEL:
10977 case EXEC_OMP_PARALLEL_DO:
10978 case EXEC_OMP_PARALLEL_DO_SIMD:
10979 case EXEC_OMP_PARALLEL_LOOP:
10980 case EXEC_OMP_PARALLEL_MASKED:
10981 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
10982 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
10983 case EXEC_OMP_PARALLEL_MASTER:
10984 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
10985 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
10986 case EXEC_OMP_PARALLEL_SECTIONS:
10987 case EXEC_OMP_PARALLEL_WORKSHARE:
10988 case EXEC_OMP_SECTIONS:
10989 case EXEC_OMP_SIMD:
10990 case EXEC_OMP_SCOPE:
10991 case EXEC_OMP_SINGLE:
10992 case EXEC_OMP_TARGET:
10993 case EXEC_OMP_TARGET_DATA:
10994 case EXEC_OMP_TARGET_ENTER_DATA:
10995 case EXEC_OMP_TARGET_EXIT_DATA:
10996 case EXEC_OMP_TARGET_PARALLEL:
10997 case EXEC_OMP_TARGET_PARALLEL_DO:
10998 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
10999 case EXEC_OMP_TARGET_PARALLEL_LOOP:
11000 case EXEC_OMP_TARGET_SIMD:
11001 case EXEC_OMP_TARGET_TEAMS:
11002 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
11003 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
11004 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11005 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
11006 case EXEC_OMP_TARGET_TEAMS_LOOP:
11007 case EXEC_OMP_TARGET_UPDATE:
11008 case EXEC_OMP_TASK:
11009 case EXEC_OMP_TASKGROUP:
11010 case EXEC_OMP_TASKLOOP:
11011 case EXEC_OMP_TASKLOOP_SIMD:
11012 case EXEC_OMP_TASKWAIT:
11013 case EXEC_OMP_TASKYIELD:
11014 case EXEC_OMP_TEAMS:
11015 case EXEC_OMP_TEAMS_DISTRIBUTE:
11016 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
11017 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11018 case EXEC_OMP_TEAMS_LOOP:
11019 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
11020 case EXEC_OMP_WORKSHARE:
11021 break;
11022
11023 default:
11024 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
11025 }
11026
11027 gfc_resolve_code (b->next, ns);
11028 }
11029 }
11030
11031
11032 /* Does everything to resolve an ordinary assignment. Returns true
11033 if this is an interface assignment. */
11034 static bool
11035 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
11036 {
11037 bool rval = false;
11038 gfc_expr *lhs;
11039 gfc_expr *rhs;
11040 int n;
11041 gfc_ref *ref;
11042 symbol_attribute attr;
11043
11044 if (gfc_extend_assign (code, ns))
11045 {
11046 gfc_expr** rhsptr;
11047
11048 if (code->op == EXEC_ASSIGN_CALL)
11049 {
11050 lhs = code->ext.actual->expr;
11051 rhsptr = &code->ext.actual->next->expr;
11052 }
11053 else
11054 {
11055 gfc_actual_arglist* args;
11056 gfc_typebound_proc* tbp;
11057
11058 gcc_assert (code->op == EXEC_COMPCALL);
11059
11060 args = code->expr1->value.compcall.actual;
11061 lhs = args->expr;
11062 rhsptr = &args->next->expr;
11063
11064 tbp = code->expr1->value.compcall.tbp;
11065 gcc_assert (!tbp->is_generic);
11066 }
11067
11068 /* Make a temporary rhs when there is a default initializer
11069 and rhs is the same symbol as the lhs. */
11070 if ((*rhsptr)->expr_type == EXPR_VARIABLE
11071 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
11072 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
11073 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
11074 *rhsptr = gfc_get_parentheses (*rhsptr);
11075
11076 return true;
11077 }
11078
11079 lhs = code->expr1;
11080 rhs = code->expr2;
11081
11082 if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
11083 && rhs->ts.type == BT_CHARACTER
11084 && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
11085 {
11086 /* Use of -fdec-char-conversions allows assignment of character data
11087 to non-character variables. This not permited for nonconstant
11088 strings. */
11089 gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
11090 gfc_typename (lhs), &rhs->where);
11091 return false;
11092 }
11093
11094 /* Handle the case of a BOZ literal on the RHS. */
11095 if (rhs->ts.type == BT_BOZ)
11096 {
11097 if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
11098 "statement value nor an actual argument of "
11099 "INT/REAL/DBLE/CMPLX intrinsic subprogram",
11100 &rhs->where))
11101 return false;
11102
11103 switch (lhs->ts.type)
11104 {
11105 case BT_INTEGER:
11106 if (!gfc_boz2int (rhs, lhs->ts.kind))
11107 return false;
11108 break;
11109 case BT_REAL:
11110 if (!gfc_boz2real (rhs, lhs->ts.kind))
11111 return false;
11112 break;
11113 default:
11114 gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
11115 return false;
11116 }
11117 }
11118
11119 if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
11120 {
11121 HOST_WIDE_INT llen = 0, rlen = 0;
11122 if (lhs->ts.u.cl != NULL
11123 && lhs->ts.u.cl->length != NULL
11124 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
11125 llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
11126
11127 if (rhs->expr_type == EXPR_CONSTANT)
11128 rlen = rhs->value.character.length;
11129
11130 else if (rhs->ts.u.cl != NULL
11131 && rhs->ts.u.cl->length != NULL
11132 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
11133 rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
11134
11135 if (rlen && llen && rlen > llen)
11136 gfc_warning_now (OPT_Wcharacter_truncation,
11137 "CHARACTER expression will be truncated "
11138 "in assignment (%ld/%ld) at %L",
11139 (long) llen, (long) rlen, &code->loc);
11140 }
11141
11142 /* Ensure that a vector index expression for the lvalue is evaluated
11143 to a temporary if the lvalue symbol is referenced in it. */
11144 if (lhs->rank)
11145 {
11146 for (ref = lhs->ref; ref; ref= ref->next)
11147 if (ref->type == REF_ARRAY)
11148 {
11149 for (n = 0; n < ref->u.ar.dimen; n++)
11150 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
11151 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
11152 ref->u.ar.start[n]))
11153 ref->u.ar.start[n]
11154 = gfc_get_parentheses (ref->u.ar.start[n]);
11155 }
11156 }
11157
11158 if (gfc_pure (NULL))
11159 {
11160 if (lhs->ts.type == BT_DERIVED
11161 && lhs->expr_type == EXPR_VARIABLE
11162 && lhs->ts.u.derived->attr.pointer_comp
11163 && rhs->expr_type == EXPR_VARIABLE
11164 && (gfc_impure_variable (rhs->symtree->n.sym)
11165 || gfc_is_coindexed (rhs)))
11166 {
11167 /* F2008, C1283. */
11168 if (gfc_is_coindexed (rhs))
11169 gfc_error ("Coindexed expression at %L is assigned to "
11170 "a derived type variable with a POINTER "
11171 "component in a PURE procedure",
11172 &rhs->where);
11173 else
11174 /* F2008, C1283 (4). */
11175 gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
11176 "shall not be used as the expr at %L of an intrinsic "
11177 "assignment statement in which the variable is of a "
11178 "derived type if the derived type has a pointer "
11179 "component at any level of component selection.",
11180 &rhs->where);
11181 return rval;
11182 }
11183
11184 /* Fortran 2008, C1283. */
11185 if (gfc_is_coindexed (lhs))
11186 {
11187 gfc_error ("Assignment to coindexed variable at %L in a PURE "
11188 "procedure", &rhs->where);
11189 return rval;
11190 }
11191 }
11192
11193 if (gfc_implicit_pure (NULL))
11194 {
11195 if (lhs->expr_type == EXPR_VARIABLE
11196 && lhs->symtree->n.sym != gfc_current_ns->proc_name
11197 && lhs->symtree->n.sym->ns != gfc_current_ns)
11198 gfc_unset_implicit_pure (NULL);
11199
11200 if (lhs->ts.type == BT_DERIVED
11201 && lhs->expr_type == EXPR_VARIABLE
11202 && lhs->ts.u.derived->attr.pointer_comp
11203 && rhs->expr_type == EXPR_VARIABLE
11204 && (gfc_impure_variable (rhs->symtree->n.sym)
11205 || gfc_is_coindexed (rhs)))
11206 gfc_unset_implicit_pure (NULL);
11207
11208 /* Fortran 2008, C1283. */
11209 if (gfc_is_coindexed (lhs))
11210 gfc_unset_implicit_pure (NULL);
11211 }
11212
11213 /* F2008, 7.2.1.2. */
11214 attr = gfc_expr_attr (lhs);
11215 if (lhs->ts.type == BT_CLASS && attr.allocatable)
11216 {
11217 if (attr.codimension)
11218 {
11219 gfc_error ("Assignment to polymorphic coarray at %L is not "
11220 "permitted", &lhs->where);
11221 return false;
11222 }
11223 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
11224 "polymorphic variable at %L", &lhs->where))
11225 return false;
11226 if (!flag_realloc_lhs)
11227 {
11228 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
11229 "requires %<-frealloc-lhs%>", &lhs->where);
11230 return false;
11231 }
11232 }
11233 else if (lhs->ts.type == BT_CLASS)
11234 {
11235 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
11236 "assignment at %L - check that there is a matching specific "
11237 "subroutine for %<=%> operator", &lhs->where);
11238 return false;
11239 }
11240
11241 bool lhs_coindexed = gfc_is_coindexed (lhs);
11242
11243 /* F2008, Section 7.2.1.2. */
11244 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
11245 {
11246 gfc_error ("Coindexed variable must not have an allocatable ultimate "
11247 "component in assignment at %L", &lhs->where);
11248 return false;
11249 }
11250
11251 /* Assign the 'data' of a class object to a derived type. */
11252 if (lhs->ts.type == BT_DERIVED
11253 && rhs->ts.type == BT_CLASS
11254 && rhs->expr_type != EXPR_ARRAY)
11255 gfc_add_data_component (rhs);
11256
11257 /* Make sure there is a vtable and, in particular, a _copy for the
11258 rhs type. */
11259 if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS)
11260 gfc_find_vtab (&rhs->ts);
11261
11262 bool caf_convert_to_send = flag_coarray == GFC_FCOARRAY_LIB
11263 && (lhs_coindexed
11264 || (code->expr2->expr_type == EXPR_FUNCTION
11265 && code->expr2->value.function.isym
11266 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
11267 && (code->expr1->rank == 0 || code->expr2->rank != 0)
11268 && !gfc_expr_attr (rhs).allocatable
11269 && !gfc_has_vector_subscript (rhs)));
11270
11271 gfc_check_assign (lhs, rhs, 1, !caf_convert_to_send);
11272
11273 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
11274 Additionally, insert this code when the RHS is a CAF as we then use the
11275 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
11276 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
11277 noncoindexed array and the RHS is a coindexed scalar, use the normal code
11278 path. */
11279 if (caf_convert_to_send)
11280 {
11281 if (code->expr2->expr_type == EXPR_FUNCTION
11282 && code->expr2->value.function.isym
11283 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
11284 remove_caf_get_intrinsic (code->expr2);
11285 code->op = EXEC_CALL;
11286 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
11287 code->resolved_sym = code->symtree->n.sym;
11288 code->resolved_sym->attr.flavor = FL_PROCEDURE;
11289 code->resolved_sym->attr.intrinsic = 1;
11290 code->resolved_sym->attr.subroutine = 1;
11291 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
11292 gfc_commit_symbol (code->resolved_sym);
11293 code->ext.actual = gfc_get_actual_arglist ();
11294 code->ext.actual->expr = lhs;
11295 code->ext.actual->next = gfc_get_actual_arglist ();
11296 code->ext.actual->next->expr = rhs;
11297 code->expr1 = NULL;
11298 code->expr2 = NULL;
11299 }
11300
11301 return false;
11302 }
11303
11304
11305 /* Add a component reference onto an expression. */
11306
11307 static void
11308 add_comp_ref (gfc_expr *e, gfc_component *c)
11309 {
11310 gfc_ref **ref;
11311 ref = &(e->ref);
11312 while (*ref)
11313 ref = &((*ref)->next);
11314 *ref = gfc_get_ref ();
11315 (*ref)->type = REF_COMPONENT;
11316 (*ref)->u.c.sym = e->ts.u.derived;
11317 (*ref)->u.c.component = c;
11318 e->ts = c->ts;
11319
11320 /* Add a full array ref, as necessary. */
11321 if (c->as)
11322 {
11323 gfc_add_full_array_ref (e, c->as);
11324 e->rank = c->as->rank;
11325 }
11326 }
11327
11328
11329 /* Build an assignment. Keep the argument 'op' for future use, so that
11330 pointer assignments can be made. */
11331
11332 static gfc_code *
11333 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
11334 gfc_component *comp1, gfc_component *comp2, locus loc)
11335 {
11336 gfc_code *this_code;
11337
11338 this_code = gfc_get_code (op);
11339 this_code->next = NULL;
11340 this_code->expr1 = gfc_copy_expr (expr1);
11341 this_code->expr2 = gfc_copy_expr (expr2);
11342 this_code->loc = loc;
11343 if (comp1 && comp2)
11344 {
11345 add_comp_ref (this_code->expr1, comp1);
11346 add_comp_ref (this_code->expr2, comp2);
11347 }
11348
11349 return this_code;
11350 }
11351
11352
11353 /* Makes a temporary variable expression based on the characteristics of
11354 a given variable expression. */
11355
11356 static gfc_expr*
11357 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
11358 {
11359 static int serial = 0;
11360 char name[GFC_MAX_SYMBOL_LEN];
11361 gfc_symtree *tmp;
11362 gfc_array_spec *as;
11363 gfc_array_ref *aref;
11364 gfc_ref *ref;
11365
11366 sprintf (name, GFC_PREFIX("DA%d"), serial++);
11367 gfc_get_sym_tree (name, ns, &tmp, false);
11368 gfc_add_type (tmp->n.sym, &e->ts, NULL);
11369
11370 if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
11371 tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
11372 NULL,
11373 e->value.character.length);
11374
11375 as = NULL;
11376 ref = NULL;
11377 aref = NULL;
11378
11379 /* Obtain the arrayspec for the temporary. */
11380 if (e->rank && e->expr_type != EXPR_ARRAY
11381 && e->expr_type != EXPR_FUNCTION
11382 && e->expr_type != EXPR_OP)
11383 {
11384 aref = gfc_find_array_ref (e);
11385 if (e->expr_type == EXPR_VARIABLE
11386 && e->symtree->n.sym->as == aref->as)
11387 as = aref->as;
11388 else
11389 {
11390 for (ref = e->ref; ref; ref = ref->next)
11391 if (ref->type == REF_COMPONENT
11392 && ref->u.c.component->as == aref->as)
11393 {
11394 as = aref->as;
11395 break;
11396 }
11397 }
11398 }
11399
11400 /* Add the attributes and the arrayspec to the temporary. */
11401 tmp->n.sym->attr = gfc_expr_attr (e);
11402 tmp->n.sym->attr.function = 0;
11403 tmp->n.sym->attr.proc_pointer = 0;
11404 tmp->n.sym->attr.result = 0;
11405 tmp->n.sym->attr.flavor = FL_VARIABLE;
11406 tmp->n.sym->attr.dummy = 0;
11407 tmp->n.sym->attr.use_assoc = 0;
11408 tmp->n.sym->attr.intent = INTENT_UNKNOWN;
11409
11410
11411 if (as)
11412 {
11413 tmp->n.sym->as = gfc_copy_array_spec (as);
11414 if (!ref)
11415 ref = e->ref;
11416 if (as->type == AS_DEFERRED)
11417 tmp->n.sym->attr.allocatable = 1;
11418 }
11419 else if (e->rank && (e->expr_type == EXPR_ARRAY
11420 || e->expr_type == EXPR_FUNCTION
11421 || e->expr_type == EXPR_OP))
11422 {
11423 tmp->n.sym->as = gfc_get_array_spec ();
11424 tmp->n.sym->as->type = AS_DEFERRED;
11425 tmp->n.sym->as->rank = e->rank;
11426 tmp->n.sym->attr.allocatable = 1;
11427 tmp->n.sym->attr.dimension = 1;
11428 }
11429 else
11430 tmp->n.sym->attr.dimension = 0;
11431
11432 gfc_set_sym_referenced (tmp->n.sym);
11433 gfc_commit_symbol (tmp->n.sym);
11434 e = gfc_lval_expr_from_sym (tmp->n.sym);
11435
11436 /* Should the lhs be a section, use its array ref for the
11437 temporary expression. */
11438 if (aref && aref->type != AR_FULL)
11439 {
11440 gfc_free_ref_list (e->ref);
11441 e->ref = gfc_copy_ref (ref);
11442 }
11443 return e;
11444 }
11445
11446
11447 /* Add one line of code to the code chain, making sure that 'head' and
11448 'tail' are appropriately updated. */
11449
11450 static void
11451 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
11452 {
11453 gcc_assert (this_code);
11454 if (*head == NULL)
11455 *head = *tail = *this_code;
11456 else
11457 *tail = gfc_append_code (*tail, *this_code);
11458 *this_code = NULL;
11459 }
11460
11461
11462 /* Generate a final call from a variable expression */
11463
11464 static void
11465 generate_final_call (gfc_expr *tmp_expr, gfc_code **head, gfc_code **tail)
11466 {
11467 gfc_code *this_code;
11468 gfc_expr *final_expr = NULL;
11469 gfc_expr *size_expr;
11470 gfc_expr *fini_coarray;
11471
11472 gcc_assert (tmp_expr->expr_type == EXPR_VARIABLE);
11473 if (!gfc_is_finalizable (tmp_expr->ts.u.derived, &final_expr) || !final_expr)
11474 return;
11475
11476 /* Now generate the finalizer call. */
11477 this_code = gfc_get_code (EXEC_CALL);
11478 this_code->symtree = final_expr->symtree;
11479 this_code->resolved_sym = final_expr->symtree->n.sym;
11480
11481 //* Expression to be finalized */
11482 this_code->ext.actual = gfc_get_actual_arglist ();
11483 this_code->ext.actual->expr = gfc_copy_expr (tmp_expr);
11484
11485 /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE. */
11486 this_code->ext.actual->next = gfc_get_actual_arglist ();
11487 size_expr = gfc_get_expr ();
11488 size_expr->where = gfc_current_locus;
11489 size_expr->expr_type = EXPR_OP;
11490 size_expr->value.op.op = INTRINSIC_DIVIDE;
11491 size_expr->value.op.op1
11492 = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_STORAGE_SIZE,
11493 "storage_size", gfc_current_locus, 2,
11494 gfc_lval_expr_from_sym (tmp_expr->symtree->n.sym),
11495 gfc_get_int_expr (gfc_index_integer_kind,
11496 NULL, 0));
11497 size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
11498 gfc_character_storage_size);
11499 size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
11500 size_expr->ts = size_expr->value.op.op1->ts;
11501 this_code->ext.actual->next->expr = size_expr;
11502
11503 /* fini_coarray */
11504 this_code->ext.actual->next->next = gfc_get_actual_arglist ();
11505 fini_coarray = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
11506 &tmp_expr->where);
11507 fini_coarray->value.logical = (int)gfc_expr_attr (tmp_expr).codimension;
11508 this_code->ext.actual->next->next->expr = fini_coarray;
11509
11510 add_code_to_chain (&this_code, head, tail);
11511
11512 }
11513
11514 /* Counts the potential number of part array references that would
11515 result from resolution of typebound defined assignments. */
11516
11517
11518 static int
11519 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
11520 {
11521 gfc_component *c;
11522 int c_depth = 0, t_depth;
11523
11524 for (c= derived->components; c; c = c->next)
11525 {
11526 if ((!gfc_bt_struct (c->ts.type)
11527 || c->attr.pointer
11528 || c->attr.allocatable
11529 || c->attr.proc_pointer_comp
11530 || c->attr.class_pointer
11531 || c->attr.proc_pointer)
11532 && !c->attr.defined_assign_comp)
11533 continue;
11534
11535 if (c->as && c_depth == 0)
11536 c_depth = 1;
11537
11538 if (c->ts.u.derived->attr.defined_assign_comp)
11539 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
11540 c->as ? 1 : 0);
11541 else
11542 t_depth = 0;
11543
11544 c_depth = t_depth > c_depth ? t_depth : c_depth;
11545 }
11546 return depth + c_depth;
11547 }
11548
11549
11550 /* Implement 10.2.1.3 paragraph 13 of the F18 standard:
11551 "An intrinsic assignment where the variable is of derived type is performed
11552 as if each component of the variable were assigned from the corresponding
11553 component of expr using pointer assignment (10.2.2) for each pointer
11554 component, defined assignment for each nonpointer nonallocatable component
11555 of a type that has a type-bound defined assignment consistent with the
11556 component, intrinsic assignment for each other nonpointer nonallocatable
11557 component, and intrinsic assignment for each allocated coarray component.
11558 For unallocated coarray components, the corresponding component of the
11559 variable shall be unallocated. For a noncoarray allocatable component the
11560 following sequence of operations is applied.
11561 (1) If the component of the variable is allocated, it is deallocated.
11562 (2) If the component of the value of expr is allocated, the
11563 corresponding component of the variable is allocated with the same
11564 dynamic type and type parameters as the component of the value of
11565 expr. If it is an array, it is allocated with the same bounds. The
11566 value of the component of the value of expr is then assigned to the
11567 corresponding component of the variable using defined assignment if
11568 the declared type of the component has a type-bound defined
11569 assignment consistent with the component, and intrinsic assignment
11570 for the dynamic type of that component otherwise."
11571
11572 The pointer assignments are taken care of by the intrinsic assignment of the
11573 structure itself. This function recursively adds defined assignments where
11574 required. The recursion is accomplished by calling gfc_resolve_code.
11575
11576 When the lhs in a defined assignment has intent INOUT or is intent OUT
11577 and the component of 'var' is finalizable, we need a temporary for the
11578 lhs. In pseudo-code for an assignment var = expr:
11579
11580 ! Confine finalization of temporaries, as far as possible.
11581 Enclose the code for the assignment in a block
11582 ! Only call function 'expr' once.
11583 #if ('expr is not a constant or an variable)
11584 temp_expr = expr
11585 expr = temp_x
11586 ! Do the intrinsic assignment
11587 #if typeof ('var') has a typebound final subroutine
11588 finalize (var)
11589 var = expr
11590 ! Now do the component assignments
11591 #do over derived type components [%cmp]
11592 #if (cmp is a pointer of any kind)
11593 continue
11594 build the assignment
11595 resolve the code
11596 #if the code is a typebound assignment
11597 #if (arg1 is INOUT or finalizable OUT && !t1)
11598 t1 = var
11599 arg1 = t1
11600 deal with allocatation or not of var and this component
11601 #elseif the code is an assignment by itself
11602 #if this component does not need finalization
11603 delete code and continue
11604 #else
11605 remove the leading assignment
11606 #endif
11607 commit the code
11608 #if (t1 and (arg1 is INOUT or finalizable OUT))
11609 var%cmp = t1%cmp
11610 #enddo
11611 put all code chunks involving t1 to the top of the generated code
11612 insert the generated block in place of the original code
11613 */
11614
11615 static bool
11616 is_finalizable_type (gfc_typespec ts)
11617 {
11618 gfc_component *c;
11619
11620 if (ts.type != BT_DERIVED)
11621 return false;
11622
11623 /* (1) Check for FINAL subroutines. */
11624 if (ts.u.derived->f2k_derived && ts.u.derived->f2k_derived->finalizers)
11625 return true;
11626
11627 /* (2) Check for components of finalizable type. */
11628 for (c = ts.u.derived->components; c; c = c->next)
11629 if (c->ts.type == BT_DERIVED
11630 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable
11631 && c->ts.u.derived->f2k_derived
11632 && c->ts.u.derived->f2k_derived->finalizers)
11633 return true;
11634
11635 return false;
11636 }
11637
11638 /* The temporary assignments have to be put on top of the additional
11639 code to avoid the result being changed by the intrinsic assignment.
11640 */
11641 static int component_assignment_level = 0;
11642 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
11643 static bool finalizable_comp;
11644
11645 static void
11646 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
11647 {
11648 gfc_component *comp1, *comp2;
11649 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
11650 gfc_code *tmp_code = NULL;
11651 gfc_expr *t1 = NULL;
11652 gfc_expr *tmp_expr = NULL;
11653 int error_count, depth;
11654 bool finalizable_lhs;
11655
11656 gfc_get_errors (NULL, &error_count);
11657
11658 /* Filter out continuing processing after an error. */
11659 if (error_count
11660 || (*code)->expr1->ts.type != BT_DERIVED
11661 || (*code)->expr2->ts.type != BT_DERIVED)
11662 return;
11663
11664 /* TODO: Handle more than one part array reference in assignments. */
11665 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
11666 (*code)->expr1->rank ? 1 : 0);
11667 if (depth > 1)
11668 {
11669 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
11670 "done because multiple part array references would "
11671 "occur in intermediate expressions.", &(*code)->loc);
11672 return;
11673 }
11674
11675 if (!component_assignment_level)
11676 finalizable_comp = true;
11677
11678 /* Build a block so that function result temporaries are finalized
11679 locally on exiting the rather than enclosing scope. */
11680 if (!component_assignment_level)
11681 {
11682 ns = gfc_build_block_ns (ns);
11683 tmp_code = gfc_get_code (EXEC_NOP);
11684 *tmp_code = **code;
11685 tmp_code->next = NULL;
11686 (*code)->op = EXEC_BLOCK;
11687 (*code)->ext.block.ns = ns;
11688 (*code)->ext.block.assoc = NULL;
11689 (*code)->expr1 = (*code)->expr2 = NULL;
11690 ns->code = tmp_code;
11691 code = &ns->code;
11692 }
11693
11694 component_assignment_level++;
11695
11696 finalizable_lhs = is_finalizable_type ((*code)->expr1->ts);
11697
11698 /* Create a temporary so that functions get called only once. */
11699 if ((*code)->expr2->expr_type != EXPR_VARIABLE
11700 && (*code)->expr2->expr_type != EXPR_CONSTANT)
11701 {
11702 /* Assign the rhs to the temporary. */
11703 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
11704 this_code = build_assignment (EXEC_ASSIGN,
11705 tmp_expr, (*code)->expr2,
11706 NULL, NULL, (*code)->loc);
11707 this_code->expr2->must_finalize = 1;
11708 /* Add the code and substitute the rhs expression. */
11709 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
11710 gfc_free_expr ((*code)->expr2);
11711 (*code)->expr2 = tmp_expr;
11712 }
11713
11714 /* Do the intrinsic assignment. This is not needed if the lhs is one
11715 of the temporaries generated here, since the intrinsic assignment
11716 to the final result already does this. */
11717 if ((*code)->expr1->symtree->n.sym->name[2] != '.')
11718 {
11719 if (finalizable_lhs)
11720 (*code)->expr1->must_finalize = 1;
11721 this_code = build_assignment (EXEC_ASSIGN,
11722 (*code)->expr1, (*code)->expr2,
11723 NULL, NULL, (*code)->loc);
11724 add_code_to_chain (&this_code, &head, &tail);
11725 }
11726
11727 comp1 = (*code)->expr1->ts.u.derived->components;
11728 comp2 = (*code)->expr2->ts.u.derived->components;
11729
11730 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
11731 {
11732 bool inout = false;
11733 bool finalizable_out = false;
11734
11735 /* The intrinsic assignment does the right thing for pointers
11736 of all kinds and allocatable components. */
11737 if (!gfc_bt_struct (comp1->ts.type)
11738 || comp1->attr.pointer
11739 || comp1->attr.proc_pointer_comp
11740 || comp1->attr.class_pointer
11741 || comp1->attr.proc_pointer)
11742 continue;
11743
11744 finalizable_comp = is_finalizable_type (comp1->ts)
11745 && !finalizable_lhs;
11746
11747 /* Make an assignment for this component. */
11748 this_code = build_assignment (EXEC_ASSIGN,
11749 (*code)->expr1, (*code)->expr2,
11750 comp1, comp2, (*code)->loc);
11751
11752 /* Convert the assignment if there is a defined assignment for
11753 this type. Otherwise, using the call from gfc_resolve_code,
11754 recurse into its components. */
11755 gfc_resolve_code (this_code, ns);
11756
11757 if (this_code->op == EXEC_ASSIGN_CALL)
11758 {
11759 gfc_formal_arglist *dummy_args;
11760 gfc_symbol *rsym;
11761 /* Check that there is a typebound defined assignment. If not,
11762 then this must be a module defined assignment. We cannot
11763 use the defined_assign_comp attribute here because it must
11764 be this derived type that has the defined assignment and not
11765 a parent type. */
11766 if (!(comp1->ts.u.derived->f2k_derived
11767 && comp1->ts.u.derived->f2k_derived
11768 ->tb_op[INTRINSIC_ASSIGN]))
11769 {
11770 gfc_free_statements (this_code);
11771 this_code = NULL;
11772 continue;
11773 }
11774
11775 /* If the first argument of the subroutine has intent INOUT
11776 a temporary must be generated and used instead. */
11777 rsym = this_code->resolved_sym;
11778 dummy_args = gfc_sym_get_dummy_args (rsym);
11779 finalizable_out = gfc_may_be_finalized (comp1->ts)
11780 && dummy_args
11781 && dummy_args->sym->attr.intent == INTENT_OUT;
11782 inout = dummy_args
11783 && dummy_args->sym->attr.intent == INTENT_INOUT;
11784 if ((inout || finalizable_out)
11785 && !comp1->attr.allocatable)
11786 {
11787 gfc_code *temp_code;
11788 inout = true;
11789
11790 /* Build the temporary required for the assignment and put
11791 it at the head of the generated code. */
11792 if (!t1)
11793 {
11794 gfc_namespace *tmp_ns = ns;
11795 if (ns->parent && gfc_may_be_finalized (comp1->ts))
11796 tmp_ns = (*code)->expr1->symtree->n.sym->ns;
11797 t1 = get_temp_from_expr ((*code)->expr1, tmp_ns);
11798 t1->symtree->n.sym->attr.artificial = 1;
11799 temp_code = build_assignment (EXEC_ASSIGN,
11800 t1, (*code)->expr1,
11801 NULL, NULL, (*code)->loc);
11802
11803 /* For allocatable LHS, check whether it is allocated. Note
11804 that allocatable components with defined assignment are
11805 not yet support. See PR 57696. */
11806 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
11807 {
11808 gfc_code *block;
11809 gfc_expr *e =
11810 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
11811 block = gfc_get_code (EXEC_IF);
11812 block->block = gfc_get_code (EXEC_IF);
11813 block->block->expr1
11814 = gfc_build_intrinsic_call (ns,
11815 GFC_ISYM_ALLOCATED, "allocated",
11816 (*code)->loc, 1, e);
11817 block->block->next = temp_code;
11818 temp_code = block;
11819 }
11820 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
11821 }
11822
11823 /* Replace the first actual arg with the component of the
11824 temporary. */
11825 gfc_free_expr (this_code->ext.actual->expr);
11826 this_code->ext.actual->expr = gfc_copy_expr (t1);
11827 add_comp_ref (this_code->ext.actual->expr, comp1);
11828
11829 /* If the LHS variable is allocatable and wasn't allocated and
11830 the temporary is allocatable, pointer assign the address of
11831 the freshly allocated LHS to the temporary. */
11832 if ((*code)->expr1->symtree->n.sym->attr.allocatable
11833 && gfc_expr_attr ((*code)->expr1).allocatable)
11834 {
11835 gfc_code *block;
11836 gfc_expr *cond;
11837
11838 cond = gfc_get_expr ();
11839 cond->ts.type = BT_LOGICAL;
11840 cond->ts.kind = gfc_default_logical_kind;
11841 cond->expr_type = EXPR_OP;
11842 cond->where = (*code)->loc;
11843 cond->value.op.op = INTRINSIC_NOT;
11844 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
11845 GFC_ISYM_ALLOCATED, "allocated",
11846 (*code)->loc, 1, gfc_copy_expr (t1));
11847 block = gfc_get_code (EXEC_IF);
11848 block->block = gfc_get_code (EXEC_IF);
11849 block->block->expr1 = cond;
11850 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
11851 t1, (*code)->expr1,
11852 NULL, NULL, (*code)->loc);
11853 add_code_to_chain (&block, &head, &tail);
11854 }
11855 }
11856 }
11857 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
11858 {
11859 /* Don't add intrinsic assignments since they are already
11860 effected by the intrinsic assignment of the structure, unless
11861 finalization is required. */
11862 if (finalizable_comp)
11863 this_code->expr1->must_finalize = 1;
11864 else
11865 {
11866 gfc_free_statements (this_code);
11867 this_code = NULL;
11868 continue;
11869 }
11870 }
11871 else
11872 {
11873 /* Resolution has expanded an assignment of a derived type with
11874 defined assigned components. Remove the redundant, leading
11875 assignment. */
11876 gcc_assert (this_code->op == EXEC_ASSIGN);
11877 gfc_code *tmp = this_code;
11878 this_code = this_code->next;
11879 tmp->next = NULL;
11880 gfc_free_statements (tmp);
11881 }
11882
11883 add_code_to_chain (&this_code, &head, &tail);
11884
11885 if (t1 && (inout || finalizable_out))
11886 {
11887 /* Transfer the value to the final result. */
11888 this_code = build_assignment (EXEC_ASSIGN,
11889 (*code)->expr1, t1,
11890 comp1, comp2, (*code)->loc);
11891 this_code->expr1->must_finalize = 0;
11892 add_code_to_chain (&this_code, &head, &tail);
11893 }
11894 }
11895
11896 /* Put the temporary assignments at the top of the generated code. */
11897 if (tmp_head && component_assignment_level == 1)
11898 {
11899 gfc_append_code (tmp_head, head);
11900 head = tmp_head;
11901 tmp_head = tmp_tail = NULL;
11902 }
11903
11904 /* If we did a pointer assignment - thus, we need to ensure that the LHS is
11905 not accidentally deallocated. Hence, nullify t1. */
11906 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
11907 && gfc_expr_attr ((*code)->expr1).allocatable)
11908 {
11909 gfc_code *block;
11910 gfc_expr *cond;
11911 gfc_expr *e;
11912
11913 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
11914 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
11915 (*code)->loc, 2, gfc_copy_expr (t1), e);
11916 block = gfc_get_code (EXEC_IF);
11917 block->block = gfc_get_code (EXEC_IF);
11918 block->block->expr1 = cond;
11919 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
11920 t1, gfc_get_null_expr (&(*code)->loc),
11921 NULL, NULL, (*code)->loc);
11922 gfc_append_code (tail, block);
11923 tail = block;
11924 }
11925
11926 component_assignment_level--;
11927
11928 /* Make an explicit final call for the function result. */
11929 if (tmp_expr)
11930 generate_final_call (tmp_expr, &head, &tail);
11931
11932 if (tmp_code)
11933 {
11934 ns->code = head;
11935 return;
11936 }
11937
11938 /* Now attach the remaining code chain to the input code. Step on
11939 to the end of the new code since resolution is complete. */
11940 gcc_assert ((*code)->op == EXEC_ASSIGN);
11941 tail->next = (*code)->next;
11942 /* Overwrite 'code' because this would place the intrinsic assignment
11943 before the temporary for the lhs is created. */
11944 gfc_free_expr ((*code)->expr1);
11945 gfc_free_expr ((*code)->expr2);
11946 **code = *head;
11947 if (head != tail)
11948 free (head);
11949 *code = tail;
11950 }
11951
11952
11953 /* F2008: Pointer function assignments are of the form:
11954 ptr_fcn (args) = expr
11955 This function breaks these assignments into two statements:
11956 temporary_pointer => ptr_fcn(args)
11957 temporary_pointer = expr */
11958
11959 static bool
11960 resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
11961 {
11962 gfc_expr *tmp_ptr_expr;
11963 gfc_code *this_code;
11964 gfc_component *comp;
11965 gfc_symbol *s;
11966
11967 if ((*code)->expr1->expr_type != EXPR_FUNCTION)
11968 return false;
11969
11970 /* Even if standard does not support this feature, continue to build
11971 the two statements to avoid upsetting frontend_passes.c. */
11972 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
11973 "%L", &(*code)->loc);
11974
11975 comp = gfc_get_proc_ptr_comp ((*code)->expr1);
11976
11977 if (comp)
11978 s = comp->ts.interface;
11979 else
11980 s = (*code)->expr1->symtree->n.sym;
11981
11982 if (s == NULL || !s->result->attr.pointer)
11983 {
11984 gfc_error ("The function result on the lhs of the assignment at "
11985 "%L must have the pointer attribute.",
11986 &(*code)->expr1->where);
11987 (*code)->op = EXEC_NOP;
11988 return false;
11989 }
11990
11991 tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
11992
11993 /* get_temp_from_expression is set up for ordinary assignments. To that
11994 end, where array bounds are not known, arrays are made allocatable.
11995 Change the temporary to a pointer here. */
11996 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
11997 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
11998 tmp_ptr_expr->where = (*code)->loc;
11999
12000 this_code = build_assignment (EXEC_ASSIGN,
12001 tmp_ptr_expr, (*code)->expr2,
12002 NULL, NULL, (*code)->loc);
12003 this_code->next = (*code)->next;
12004 (*code)->next = this_code;
12005 (*code)->op = EXEC_POINTER_ASSIGN;
12006 (*code)->expr2 = (*code)->expr1;
12007 (*code)->expr1 = tmp_ptr_expr;
12008
12009 return true;
12010 }
12011
12012
12013 /* Deferred character length assignments from an operator expression
12014 require a temporary because the character length of the lhs can
12015 change in the course of the assignment. */
12016
12017 static bool
12018 deferred_op_assign (gfc_code **code, gfc_namespace *ns)
12019 {
12020 gfc_expr *tmp_expr;
12021 gfc_code *this_code;
12022
12023 if (!((*code)->expr1->ts.type == BT_CHARACTER
12024 && (*code)->expr1->ts.deferred && (*code)->expr1->rank
12025 && (*code)->expr2->ts.type == BT_CHARACTER
12026 && (*code)->expr2->expr_type == EXPR_OP))
12027 return false;
12028
12029 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
12030 return false;
12031
12032 if (gfc_expr_attr ((*code)->expr1).pointer)
12033 return false;
12034
12035 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
12036 tmp_expr->where = (*code)->loc;
12037
12038 /* A new charlen is required to ensure that the variable string
12039 length is different to that of the original lhs. */
12040 tmp_expr->ts.u.cl = gfc_get_charlen();
12041 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
12042 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
12043 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
12044
12045 tmp_expr->symtree->n.sym->ts.deferred = 1;
12046
12047 this_code = build_assignment (EXEC_ASSIGN,
12048 (*code)->expr1,
12049 gfc_copy_expr (tmp_expr),
12050 NULL, NULL, (*code)->loc);
12051
12052 (*code)->expr1 = tmp_expr;
12053
12054 this_code->next = (*code)->next;
12055 (*code)->next = this_code;
12056
12057 return true;
12058 }
12059
12060
12061 static bool
12062 check_team (gfc_expr *team, const char *intrinsic)
12063 {
12064 if (team->rank != 0
12065 || team->ts.type != BT_DERIVED
12066 || team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
12067 || team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
12068 {
12069 gfc_error ("TEAM argument to %qs at %L must be a scalar expression "
12070 "of type TEAM_TYPE", intrinsic, &team->where);
12071 return false;
12072 }
12073
12074 return true;
12075 }
12076
12077
12078 /* Given a block of code, recursively resolve everything pointed to by this
12079 code block. */
12080
12081 void
12082 gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
12083 {
12084 int omp_workshare_save;
12085 int forall_save, do_concurrent_save;
12086 code_stack frame;
12087 bool t;
12088
12089 frame.prev = cs_base;
12090 frame.head = code;
12091 cs_base = &frame;
12092
12093 find_reachable_labels (code);
12094
12095 for (; code; code = code->next)
12096 {
12097 frame.current = code;
12098 forall_save = forall_flag;
12099 do_concurrent_save = gfc_do_concurrent_flag;
12100
12101 if (code->op == EXEC_FORALL)
12102 {
12103 forall_flag = 1;
12104 gfc_resolve_forall (code, ns, forall_save);
12105 forall_flag = 2;
12106 }
12107 else if (code->block)
12108 {
12109 omp_workshare_save = -1;
12110 switch (code->op)
12111 {
12112 case EXEC_OACC_PARALLEL_LOOP:
12113 case EXEC_OACC_PARALLEL:
12114 case EXEC_OACC_KERNELS_LOOP:
12115 case EXEC_OACC_KERNELS:
12116 case EXEC_OACC_SERIAL_LOOP:
12117 case EXEC_OACC_SERIAL:
12118 case EXEC_OACC_DATA:
12119 case EXEC_OACC_HOST_DATA:
12120 case EXEC_OACC_LOOP:
12121 gfc_resolve_oacc_blocks (code, ns);
12122 break;
12123 case EXEC_OMP_PARALLEL_WORKSHARE:
12124 omp_workshare_save = omp_workshare_flag;
12125 omp_workshare_flag = 1;
12126 gfc_resolve_omp_parallel_blocks (code, ns);
12127 break;
12128 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
12129 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
12130 case EXEC_OMP_MASKED_TASKLOOP:
12131 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
12132 case EXEC_OMP_MASTER_TASKLOOP:
12133 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
12134 case EXEC_OMP_PARALLEL:
12135 case EXEC_OMP_PARALLEL_DO:
12136 case EXEC_OMP_PARALLEL_DO_SIMD:
12137 case EXEC_OMP_PARALLEL_LOOP:
12138 case EXEC_OMP_PARALLEL_MASKED:
12139 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
12140 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
12141 case EXEC_OMP_PARALLEL_MASTER:
12142 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
12143 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
12144 case EXEC_OMP_PARALLEL_SECTIONS:
12145 case EXEC_OMP_TARGET_PARALLEL:
12146 case EXEC_OMP_TARGET_PARALLEL_DO:
12147 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
12148 case EXEC_OMP_TARGET_PARALLEL_LOOP:
12149 case EXEC_OMP_TARGET_TEAMS:
12150 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
12151 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
12152 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12153 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
12154 case EXEC_OMP_TARGET_TEAMS_LOOP:
12155 case EXEC_OMP_TASK:
12156 case EXEC_OMP_TASKLOOP:
12157 case EXEC_OMP_TASKLOOP_SIMD:
12158 case EXEC_OMP_TEAMS:
12159 case EXEC_OMP_TEAMS_DISTRIBUTE:
12160 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
12161 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12162 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
12163 case EXEC_OMP_TEAMS_LOOP:
12164 omp_workshare_save = omp_workshare_flag;
12165 omp_workshare_flag = 0;
12166 gfc_resolve_omp_parallel_blocks (code, ns);
12167 break;
12168 case EXEC_OMP_DISTRIBUTE:
12169 case EXEC_OMP_DISTRIBUTE_SIMD:
12170 case EXEC_OMP_DO:
12171 case EXEC_OMP_DO_SIMD:
12172 case EXEC_OMP_LOOP:
12173 case EXEC_OMP_SIMD:
12174 case EXEC_OMP_TARGET_SIMD:
12175 gfc_resolve_omp_do_blocks (code, ns);
12176 break;
12177 case EXEC_SELECT_TYPE:
12178 case EXEC_SELECT_RANK:
12179 /* Blocks are handled in resolve_select_type/rank because we
12180 have to transform the SELECT TYPE into ASSOCIATE first. */
12181 break;
12182 case EXEC_DO_CONCURRENT:
12183 gfc_do_concurrent_flag = 1;
12184 gfc_resolve_blocks (code->block, ns);
12185 gfc_do_concurrent_flag = 2;
12186 break;
12187 case EXEC_OMP_WORKSHARE:
12188 omp_workshare_save = omp_workshare_flag;
12189 omp_workshare_flag = 1;
12190 /* FALL THROUGH */
12191 default:
12192 gfc_resolve_blocks (code->block, ns);
12193 break;
12194 }
12195
12196 if (omp_workshare_save != -1)
12197 omp_workshare_flag = omp_workshare_save;
12198 }
12199 start:
12200 t = true;
12201 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
12202 t = gfc_resolve_expr (code->expr1);
12203 forall_flag = forall_save;
12204 gfc_do_concurrent_flag = do_concurrent_save;
12205
12206 if (!gfc_resolve_expr (code->expr2))
12207 t = false;
12208
12209 if (code->op == EXEC_ALLOCATE
12210 && !gfc_resolve_expr (code->expr3))
12211 t = false;
12212
12213 switch (code->op)
12214 {
12215 case EXEC_NOP:
12216 case EXEC_END_BLOCK:
12217 case EXEC_END_NESTED_BLOCK:
12218 case EXEC_CYCLE:
12219 case EXEC_PAUSE:
12220 break;
12221
12222 case EXEC_STOP:
12223 case EXEC_ERROR_STOP:
12224 if (code->expr2 != NULL
12225 && (code->expr2->ts.type != BT_LOGICAL
12226 || code->expr2->rank != 0))
12227 gfc_error ("QUIET specifier at %L must be a scalar LOGICAL",
12228 &code->expr2->where);
12229 break;
12230
12231 case EXEC_EXIT:
12232 case EXEC_CONTINUE:
12233 case EXEC_DT_END:
12234 case EXEC_ASSIGN_CALL:
12235 break;
12236
12237 case EXEC_CRITICAL:
12238 resolve_critical (code);
12239 break;
12240
12241 case EXEC_SYNC_ALL:
12242 case EXEC_SYNC_IMAGES:
12243 case EXEC_SYNC_MEMORY:
12244 resolve_sync (code);
12245 break;
12246
12247 case EXEC_LOCK:
12248 case EXEC_UNLOCK:
12249 case EXEC_EVENT_POST:
12250 case EXEC_EVENT_WAIT:
12251 resolve_lock_unlock_event (code);
12252 break;
12253
12254 case EXEC_FAIL_IMAGE:
12255 break;
12256
12257 case EXEC_FORM_TEAM:
12258 if (code->expr1 != NULL
12259 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
12260 gfc_error ("TEAM NUMBER argument to FORM TEAM at %L must be "
12261 "a scalar INTEGER", &code->expr1->where);
12262 check_team (code->expr2, "FORM TEAM");
12263 break;
12264
12265 case EXEC_CHANGE_TEAM:
12266 check_team (code->expr1, "CHANGE TEAM");
12267 break;
12268
12269 case EXEC_END_TEAM:
12270 break;
12271
12272 case EXEC_SYNC_TEAM:
12273 check_team (code->expr1, "SYNC TEAM");
12274 break;
12275
12276 case EXEC_ENTRY:
12277 /* Keep track of which entry we are up to. */
12278 current_entry_id = code->ext.entry->id;
12279 break;
12280
12281 case EXEC_WHERE:
12282 resolve_where (code, NULL);
12283 break;
12284
12285 case EXEC_GOTO:
12286 if (code->expr1 != NULL)
12287 {
12288 if (code->expr1->expr_type != EXPR_VARIABLE
12289 || code->expr1->ts.type != BT_INTEGER
12290 || (code->expr1->ref
12291 && code->expr1->ref->type == REF_ARRAY)
12292 || code->expr1->symtree == NULL
12293 || (code->expr1->symtree->n.sym
12294 && (code->expr1->symtree->n.sym->attr.flavor
12295 == FL_PARAMETER)))
12296 gfc_error ("ASSIGNED GOTO statement at %L requires a "
12297 "scalar INTEGER variable", &code->expr1->where);
12298 else if (code->expr1->symtree->n.sym
12299 && code->expr1->symtree->n.sym->attr.assign != 1)
12300 gfc_error ("Variable %qs has not been assigned a target "
12301 "label at %L", code->expr1->symtree->n.sym->name,
12302 &code->expr1->where);
12303 }
12304 else
12305 resolve_branch (code->label1, code);
12306 break;
12307
12308 case EXEC_RETURN:
12309 if (code->expr1 != NULL
12310 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
12311 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
12312 "INTEGER return specifier", &code->expr1->where);
12313 break;
12314
12315 case EXEC_INIT_ASSIGN:
12316 case EXEC_END_PROCEDURE:
12317 break;
12318
12319 case EXEC_ASSIGN:
12320 if (!t)
12321 break;
12322
12323 if (code->expr1->ts.type == BT_CLASS)
12324 gfc_find_vtab (&code->expr2->ts);
12325
12326 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
12327 the LHS. */
12328 if (code->expr1->expr_type == EXPR_FUNCTION
12329 && code->expr1->value.function.isym
12330 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
12331 remove_caf_get_intrinsic (code->expr1);
12332
12333 /* If this is a pointer function in an lvalue variable context,
12334 the new code will have to be resolved afresh. This is also the
12335 case with an error, where the code is transformed into NOP to
12336 prevent ICEs downstream. */
12337 if (resolve_ptr_fcn_assign (&code, ns)
12338 || code->op == EXEC_NOP)
12339 goto start;
12340
12341 if (!gfc_check_vardef_context (code->expr1, false, false, false,
12342 _("assignment")))
12343 break;
12344
12345 if (resolve_ordinary_assign (code, ns))
12346 {
12347 if (omp_workshare_flag)
12348 {
12349 gfc_error ("Expected intrinsic assignment in OMP WORKSHARE "
12350 "at %L", &code->loc);
12351 break;
12352 }
12353 if (code->op == EXEC_COMPCALL)
12354 goto compcall;
12355 else
12356 goto call;
12357 }
12358
12359 /* Check for dependencies in deferred character length array
12360 assignments and generate a temporary, if necessary. */
12361 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
12362 break;
12363
12364 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
12365 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
12366 && code->expr1->ts.u.derived
12367 && code->expr1->ts.u.derived->attr.defined_assign_comp)
12368 generate_component_assignments (&code, ns);
12369 else if (code->op == EXEC_ASSIGN)
12370 {
12371 if (gfc_may_be_finalized (code->expr1->ts))
12372 code->expr1->must_finalize = 1;
12373 if (code->expr2->expr_type == EXPR_ARRAY
12374 && gfc_may_be_finalized (code->expr2->ts))
12375 code->expr2->must_finalize = 1;
12376 }
12377
12378 break;
12379
12380 case EXEC_LABEL_ASSIGN:
12381 if (code->label1->defined == ST_LABEL_UNKNOWN)
12382 gfc_error ("Label %d referenced at %L is never defined",
12383 code->label1->value, &code->label1->where);
12384 if (t
12385 && (code->expr1->expr_type != EXPR_VARIABLE
12386 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
12387 || code->expr1->symtree->n.sym->ts.kind
12388 != gfc_default_integer_kind
12389 || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
12390 || code->expr1->symtree->n.sym->as != NULL))
12391 gfc_error ("ASSIGN statement at %L requires a scalar "
12392 "default INTEGER variable", &code->expr1->where);
12393 break;
12394
12395 case EXEC_POINTER_ASSIGN:
12396 {
12397 gfc_expr* e;
12398
12399 if (!t)
12400 break;
12401
12402 /* This is both a variable definition and pointer assignment
12403 context, so check both of them. For rank remapping, a final
12404 array ref may be present on the LHS and fool gfc_expr_attr
12405 used in gfc_check_vardef_context. Remove it. */
12406 e = remove_last_array_ref (code->expr1);
12407 t = gfc_check_vardef_context (e, true, false, false,
12408 _("pointer assignment"));
12409 if (t)
12410 t = gfc_check_vardef_context (e, false, false, false,
12411 _("pointer assignment"));
12412 gfc_free_expr (e);
12413
12414 t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
12415
12416 if (!t)
12417 break;
12418
12419 /* Assigning a class object always is a regular assign. */
12420 if (code->expr2->ts.type == BT_CLASS
12421 && code->expr1->ts.type == BT_CLASS
12422 && CLASS_DATA (code->expr2)
12423 && !CLASS_DATA (code->expr2)->attr.dimension
12424 && !(gfc_expr_attr (code->expr1).proc_pointer
12425 && code->expr2->expr_type == EXPR_VARIABLE
12426 && code->expr2->symtree->n.sym->attr.flavor
12427 == FL_PROCEDURE))
12428 code->op = EXEC_ASSIGN;
12429 break;
12430 }
12431
12432 case EXEC_ARITHMETIC_IF:
12433 {
12434 gfc_expr *e = code->expr1;
12435
12436 gfc_resolve_expr (e);
12437 if (e->expr_type == EXPR_NULL)
12438 gfc_error ("Invalid NULL at %L", &e->where);
12439
12440 if (t && (e->rank > 0
12441 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
12442 gfc_error ("Arithmetic IF statement at %L requires a scalar "
12443 "REAL or INTEGER expression", &e->where);
12444
12445 resolve_branch (code->label1, code);
12446 resolve_branch (code->label2, code);
12447 resolve_branch (code->label3, code);
12448 }
12449 break;
12450
12451 case EXEC_IF:
12452 if (t && code->expr1 != NULL
12453 && (code->expr1->ts.type != BT_LOGICAL
12454 || code->expr1->rank != 0))
12455 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
12456 &code->expr1->where);
12457 break;
12458
12459 case EXEC_CALL:
12460 call:
12461 resolve_call (code);
12462 break;
12463
12464 case EXEC_COMPCALL:
12465 compcall:
12466 resolve_typebound_subroutine (code);
12467 break;
12468
12469 case EXEC_CALL_PPC:
12470 resolve_ppc_call (code);
12471 break;
12472
12473 case EXEC_SELECT:
12474 /* Select is complicated. Also, a SELECT construct could be
12475 a transformed computed GOTO. */
12476 resolve_select (code, false);
12477 break;
12478
12479 case EXEC_SELECT_TYPE:
12480 resolve_select_type (code, ns);
12481 break;
12482
12483 case EXEC_SELECT_RANK:
12484 resolve_select_rank (code, ns);
12485 break;
12486
12487 case EXEC_BLOCK:
12488 resolve_block_construct (code);
12489 break;
12490
12491 case EXEC_DO:
12492 if (code->ext.iterator != NULL)
12493 {
12494 gfc_iterator *iter = code->ext.iterator;
12495 if (gfc_resolve_iterator (iter, true, false))
12496 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
12497 true);
12498 }
12499 break;
12500
12501 case EXEC_DO_WHILE:
12502 if (code->expr1 == NULL)
12503 gfc_internal_error ("gfc_resolve_code(): No expression on "
12504 "DO WHILE");
12505 if (t
12506 && (code->expr1->rank != 0
12507 || code->expr1->ts.type != BT_LOGICAL))
12508 gfc_error ("Exit condition of DO WHILE loop at %L must be "
12509 "a scalar LOGICAL expression", &code->expr1->where);
12510 break;
12511
12512 case EXEC_ALLOCATE:
12513 if (t)
12514 resolve_allocate_deallocate (code, "ALLOCATE");
12515
12516 break;
12517
12518 case EXEC_DEALLOCATE:
12519 if (t)
12520 resolve_allocate_deallocate (code, "DEALLOCATE");
12521
12522 break;
12523
12524 case EXEC_OPEN:
12525 if (!gfc_resolve_open (code->ext.open, &code->loc))
12526 break;
12527
12528 resolve_branch (code->ext.open->err, code);
12529 break;
12530
12531 case EXEC_CLOSE:
12532 if (!gfc_resolve_close (code->ext.close, &code->loc))
12533 break;
12534
12535 resolve_branch (code->ext.close->err, code);
12536 break;
12537
12538 case EXEC_BACKSPACE:
12539 case EXEC_ENDFILE:
12540 case EXEC_REWIND:
12541 case EXEC_FLUSH:
12542 if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
12543 break;
12544
12545 resolve_branch (code->ext.filepos->err, code);
12546 break;
12547
12548 case EXEC_INQUIRE:
12549 if (!gfc_resolve_inquire (code->ext.inquire))
12550 break;
12551
12552 resolve_branch (code->ext.inquire->err, code);
12553 break;
12554
12555 case EXEC_IOLENGTH:
12556 gcc_assert (code->ext.inquire != NULL);
12557 if (!gfc_resolve_inquire (code->ext.inquire))
12558 break;
12559
12560 resolve_branch (code->ext.inquire->err, code);
12561 break;
12562
12563 case EXEC_WAIT:
12564 if (!gfc_resolve_wait (code->ext.wait))
12565 break;
12566
12567 resolve_branch (code->ext.wait->err, code);
12568 resolve_branch (code->ext.wait->end, code);
12569 resolve_branch (code->ext.wait->eor, code);
12570 break;
12571
12572 case EXEC_READ:
12573 case EXEC_WRITE:
12574 if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
12575 break;
12576
12577 resolve_branch (code->ext.dt->err, code);
12578 resolve_branch (code->ext.dt->end, code);
12579 resolve_branch (code->ext.dt->eor, code);
12580 break;
12581
12582 case EXEC_TRANSFER:
12583 resolve_transfer (code);
12584 break;
12585
12586 case EXEC_DO_CONCURRENT:
12587 case EXEC_FORALL:
12588 resolve_forall_iterators (code->ext.forall_iterator);
12589
12590 if (code->expr1 != NULL
12591 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
12592 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
12593 "expression", &code->expr1->where);
12594 break;
12595
12596 case EXEC_OACC_PARALLEL_LOOP:
12597 case EXEC_OACC_PARALLEL:
12598 case EXEC_OACC_KERNELS_LOOP:
12599 case EXEC_OACC_KERNELS:
12600 case EXEC_OACC_SERIAL_LOOP:
12601 case EXEC_OACC_SERIAL:
12602 case EXEC_OACC_DATA:
12603 case EXEC_OACC_HOST_DATA:
12604 case EXEC_OACC_LOOP:
12605 case EXEC_OACC_UPDATE:
12606 case EXEC_OACC_WAIT:
12607 case EXEC_OACC_CACHE:
12608 case EXEC_OACC_ENTER_DATA:
12609 case EXEC_OACC_EXIT_DATA:
12610 case EXEC_OACC_ATOMIC:
12611 case EXEC_OACC_DECLARE:
12612 gfc_resolve_oacc_directive (code, ns);
12613 break;
12614
12615 case EXEC_OMP_ASSUME:
12616 case EXEC_OMP_ATOMIC:
12617 case EXEC_OMP_BARRIER:
12618 case EXEC_OMP_CANCEL:
12619 case EXEC_OMP_CANCELLATION_POINT:
12620 case EXEC_OMP_CRITICAL:
12621 case EXEC_OMP_FLUSH:
12622 case EXEC_OMP_DEPOBJ:
12623 case EXEC_OMP_DISTRIBUTE:
12624 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
12625 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
12626 case EXEC_OMP_DISTRIBUTE_SIMD:
12627 case EXEC_OMP_DO:
12628 case EXEC_OMP_DO_SIMD:
12629 case EXEC_OMP_ERROR:
12630 case EXEC_OMP_LOOP:
12631 case EXEC_OMP_MASTER:
12632 case EXEC_OMP_MASTER_TASKLOOP:
12633 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
12634 case EXEC_OMP_MASKED:
12635 case EXEC_OMP_MASKED_TASKLOOP:
12636 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
12637 case EXEC_OMP_ORDERED:
12638 case EXEC_OMP_SCAN:
12639 case EXEC_OMP_SCOPE:
12640 case EXEC_OMP_SECTIONS:
12641 case EXEC_OMP_SIMD:
12642 case EXEC_OMP_SINGLE:
12643 case EXEC_OMP_TARGET:
12644 case EXEC_OMP_TARGET_DATA:
12645 case EXEC_OMP_TARGET_ENTER_DATA:
12646 case EXEC_OMP_TARGET_EXIT_DATA:
12647 case EXEC_OMP_TARGET_PARALLEL:
12648 case EXEC_OMP_TARGET_PARALLEL_DO:
12649 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
12650 case EXEC_OMP_TARGET_PARALLEL_LOOP:
12651 case EXEC_OMP_TARGET_SIMD:
12652 case EXEC_OMP_TARGET_TEAMS:
12653 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
12654 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
12655 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12656 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
12657 case EXEC_OMP_TARGET_TEAMS_LOOP:
12658 case EXEC_OMP_TARGET_UPDATE:
12659 case EXEC_OMP_TASK:
12660 case EXEC_OMP_TASKGROUP:
12661 case EXEC_OMP_TASKLOOP:
12662 case EXEC_OMP_TASKLOOP_SIMD:
12663 case EXEC_OMP_TASKWAIT:
12664 case EXEC_OMP_TASKYIELD:
12665 case EXEC_OMP_TEAMS:
12666 case EXEC_OMP_TEAMS_DISTRIBUTE:
12667 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
12668 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12669 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
12670 case EXEC_OMP_TEAMS_LOOP:
12671 case EXEC_OMP_WORKSHARE:
12672 gfc_resolve_omp_directive (code, ns);
12673 break;
12674
12675 case EXEC_OMP_PARALLEL:
12676 case EXEC_OMP_PARALLEL_DO:
12677 case EXEC_OMP_PARALLEL_DO_SIMD:
12678 case EXEC_OMP_PARALLEL_LOOP:
12679 case EXEC_OMP_PARALLEL_MASKED:
12680 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
12681 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
12682 case EXEC_OMP_PARALLEL_MASTER:
12683 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
12684 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
12685 case EXEC_OMP_PARALLEL_SECTIONS:
12686 case EXEC_OMP_PARALLEL_WORKSHARE:
12687 omp_workshare_save = omp_workshare_flag;
12688 omp_workshare_flag = 0;
12689 gfc_resolve_omp_directive (code, ns);
12690 omp_workshare_flag = omp_workshare_save;
12691 break;
12692
12693 default:
12694 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
12695 }
12696 }
12697
12698 cs_base = frame.prev;
12699 }
12700
12701
12702 /* Resolve initial values and make sure they are compatible with
12703 the variable. */
12704
12705 static void
12706 resolve_values (gfc_symbol *sym)
12707 {
12708 bool t;
12709
12710 if (sym->value == NULL)
12711 return;
12712
12713 if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym->attr.referenced)
12714 gfc_warning (OPT_Wdeprecated_declarations,
12715 "Using parameter %qs declared at %L is deprecated",
12716 sym->name, &sym->declared_at);
12717
12718 if (sym->value->expr_type == EXPR_STRUCTURE)
12719 t= resolve_structure_cons (sym->value, 1);
12720 else
12721 t = gfc_resolve_expr (sym->value);
12722
12723 if (!t)
12724 return;
12725
12726 gfc_check_assign_symbol (sym, NULL, sym->value);
12727 }
12728
12729
12730 /* Verify any BIND(C) derived types in the namespace so we can report errors
12731 for them once, rather than for each variable declared of that type. */
12732
12733 static void
12734 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
12735 {
12736 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
12737 && derived_sym->attr.is_bind_c == 1)
12738 verify_bind_c_derived_type (derived_sym);
12739
12740 return;
12741 }
12742
12743
12744 /* Check the interfaces of DTIO procedures associated with derived
12745 type 'sym'. These procedures can either have typebound bindings or
12746 can appear in DTIO generic interfaces. */
12747
12748 static void
12749 gfc_verify_DTIO_procedures (gfc_symbol *sym)
12750 {
12751 if (!sym || sym->attr.flavor != FL_DERIVED)
12752 return;
12753
12754 gfc_check_dtio_interfaces (sym);
12755
12756 return;
12757 }
12758
12759 /* Verify that any binding labels used in a given namespace do not collide
12760 with the names or binding labels of any global symbols. Multiple INTERFACE
12761 for the same procedure are permitted. */
12762
12763 static void
12764 gfc_verify_binding_labels (gfc_symbol *sym)
12765 {
12766 gfc_gsymbol *gsym;
12767 const char *module;
12768
12769 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
12770 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
12771 return;
12772
12773 gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
12774
12775 if (sym->module)
12776 module = sym->module;
12777 else if (sym->ns && sym->ns->proc_name
12778 && sym->ns->proc_name->attr.flavor == FL_MODULE)
12779 module = sym->ns->proc_name->name;
12780 else if (sym->ns && sym->ns->parent
12781 && sym->ns && sym->ns->parent->proc_name
12782 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
12783 module = sym->ns->parent->proc_name->name;
12784 else
12785 module = NULL;
12786
12787 if (!gsym
12788 || (!gsym->defined
12789 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
12790 {
12791 if (!gsym)
12792 gsym = gfc_get_gsymbol (sym->binding_label, true);
12793 gsym->where = sym->declared_at;
12794 gsym->sym_name = sym->name;
12795 gsym->binding_label = sym->binding_label;
12796 gsym->ns = sym->ns;
12797 gsym->mod_name = module;
12798 if (sym->attr.function)
12799 gsym->type = GSYM_FUNCTION;
12800 else if (sym->attr.subroutine)
12801 gsym->type = GSYM_SUBROUTINE;
12802 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
12803 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
12804 return;
12805 }
12806
12807 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
12808 {
12809 gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
12810 "identifier as entity at %L", sym->name,
12811 sym->binding_label, &sym->declared_at, &gsym->where);
12812 /* Clear the binding label to prevent checking multiple times. */
12813 sym->binding_label = NULL;
12814 return;
12815 }
12816
12817 if (sym->attr.flavor == FL_VARIABLE && module
12818 && (strcmp (module, gsym->mod_name) != 0
12819 || strcmp (sym->name, gsym->sym_name) != 0))
12820 {
12821 /* This can only happen if the variable is defined in a module - if it
12822 isn't the same module, reject it. */
12823 gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
12824 "uses the same global identifier as entity at %L from module %qs",
12825 sym->name, module, sym->binding_label,
12826 &sym->declared_at, &gsym->where, gsym->mod_name);
12827 sym->binding_label = NULL;
12828 return;
12829 }
12830
12831 if ((sym->attr.function || sym->attr.subroutine)
12832 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
12833 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
12834 && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
12835 && (module != gsym->mod_name
12836 || strcmp (gsym->sym_name, sym->name) != 0
12837 || (module && strcmp (module, gsym->mod_name) != 0)))
12838 {
12839 /* Print an error if the procedure is defined multiple times; we have to
12840 exclude references to the same procedure via module association or
12841 multiple checks for the same procedure. */
12842 gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
12843 "global identifier as entity at %L", sym->name,
12844 sym->binding_label, &sym->declared_at, &gsym->where);
12845 sym->binding_label = NULL;
12846 }
12847 }
12848
12849
12850 /* Resolve an index expression. */
12851
12852 static bool
12853 resolve_index_expr (gfc_expr *e)
12854 {
12855 if (!gfc_resolve_expr (e))
12856 return false;
12857
12858 if (!gfc_simplify_expr (e, 0))
12859 return false;
12860
12861 if (!gfc_specification_expr (e))
12862 return false;
12863
12864 return true;
12865 }
12866
12867
12868 /* Resolve a charlen structure. */
12869
12870 static bool
12871 resolve_charlen (gfc_charlen *cl)
12872 {
12873 int k;
12874 bool saved_specification_expr;
12875
12876 if (cl->resolved)
12877 return true;
12878
12879 cl->resolved = 1;
12880 saved_specification_expr = specification_expr;
12881 specification_expr = true;
12882
12883 if (cl->length_from_typespec)
12884 {
12885 if (!gfc_resolve_expr (cl->length))
12886 {
12887 specification_expr = saved_specification_expr;
12888 return false;
12889 }
12890
12891 if (!gfc_simplify_expr (cl->length, 0))
12892 {
12893 specification_expr = saved_specification_expr;
12894 return false;
12895 }
12896
12897 /* cl->length has been resolved. It should have an integer type. */
12898 if (cl->length
12899 && (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0))
12900 {
12901 gfc_error ("Scalar INTEGER expression expected at %L",
12902 &cl->length->where);
12903 return false;
12904 }
12905 }
12906 else
12907 {
12908 if (!resolve_index_expr (cl->length))
12909 {
12910 specification_expr = saved_specification_expr;
12911 return false;
12912 }
12913 }
12914
12915 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
12916 a negative value, the length of character entities declared is zero. */
12917 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
12918 && mpz_sgn (cl->length->value.integer) < 0)
12919 gfc_replace_expr (cl->length,
12920 gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
12921
12922 /* Check that the character length is not too large. */
12923 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
12924 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
12925 && cl->length->ts.type == BT_INTEGER
12926 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
12927 {
12928 gfc_error ("String length at %L is too large", &cl->length->where);
12929 specification_expr = saved_specification_expr;
12930 return false;
12931 }
12932
12933 specification_expr = saved_specification_expr;
12934 return true;
12935 }
12936
12937
12938 /* Test for non-constant shape arrays. */
12939
12940 static bool
12941 is_non_constant_shape_array (gfc_symbol *sym)
12942 {
12943 gfc_expr *e;
12944 int i;
12945 bool not_constant;
12946
12947 not_constant = false;
12948 if (sym->as != NULL)
12949 {
12950 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
12951 has not been simplified; parameter array references. Do the
12952 simplification now. */
12953 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
12954 {
12955 if (i == GFC_MAX_DIMENSIONS)
12956 break;
12957
12958 e = sym->as->lower[i];
12959 if (e && (!resolve_index_expr(e)
12960 || !gfc_is_constant_expr (e)))
12961 not_constant = true;
12962 e = sym->as->upper[i];
12963 if (e && (!resolve_index_expr(e)
12964 || !gfc_is_constant_expr (e)))
12965 not_constant = true;
12966 }
12967 }
12968 return not_constant;
12969 }
12970
12971 /* Given a symbol and an initialization expression, add code to initialize
12972 the symbol to the function entry. */
12973 static void
12974 build_init_assign (gfc_symbol *sym, gfc_expr *init)
12975 {
12976 gfc_expr *lval;
12977 gfc_code *init_st;
12978 gfc_namespace *ns = sym->ns;
12979
12980 /* Search for the function namespace if this is a contained
12981 function without an explicit result. */
12982 if (sym->attr.function && sym == sym->result
12983 && sym->name != sym->ns->proc_name->name)
12984 {
12985 ns = ns->contained;
12986 for (;ns; ns = ns->sibling)
12987 if (strcmp (ns->proc_name->name, sym->name) == 0)
12988 break;
12989 }
12990
12991 if (ns == NULL)
12992 {
12993 gfc_free_expr (init);
12994 return;
12995 }
12996
12997 /* Build an l-value expression for the result. */
12998 lval = gfc_lval_expr_from_sym (sym);
12999
13000 /* Add the code at scope entry. */
13001 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
13002 init_st->next = ns->code;
13003 ns->code = init_st;
13004
13005 /* Assign the default initializer to the l-value. */
13006 init_st->loc = sym->declared_at;
13007 init_st->expr1 = lval;
13008 init_st->expr2 = init;
13009 }
13010
13011
13012 /* Whether or not we can generate a default initializer for a symbol. */
13013
13014 static bool
13015 can_generate_init (gfc_symbol *sym)
13016 {
13017 symbol_attribute *a;
13018 if (!sym)
13019 return false;
13020 a = &sym->attr;
13021
13022 /* These symbols should never have a default initialization. */
13023 return !(
13024 a->allocatable
13025 || a->external
13026 || a->pointer
13027 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
13028 && (CLASS_DATA (sym)->attr.class_pointer
13029 || CLASS_DATA (sym)->attr.proc_pointer))
13030 || a->in_equivalence
13031 || a->in_common
13032 || a->data
13033 || sym->module
13034 || a->cray_pointee
13035 || a->cray_pointer
13036 || sym->assoc
13037 || (!a->referenced && !a->result)
13038 || (a->dummy && (a->intent != INTENT_OUT
13039 || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY))
13040 || (a->function && sym != sym->result)
13041 );
13042 }
13043
13044
13045 /* Assign the default initializer to a derived type variable or result. */
13046
13047 static void
13048 apply_default_init (gfc_symbol *sym)
13049 {
13050 gfc_expr *init = NULL;
13051
13052 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
13053 return;
13054
13055 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
13056 init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
13057
13058 if (init == NULL && sym->ts.type != BT_CLASS)
13059 return;
13060
13061 build_init_assign (sym, init);
13062 sym->attr.referenced = 1;
13063 }
13064
13065
13066 /* Build an initializer for a local. Returns null if the symbol should not have
13067 a default initialization. */
13068
13069 static gfc_expr *
13070 build_default_init_expr (gfc_symbol *sym)
13071 {
13072 /* These symbols should never have a default initialization. */
13073 if (sym->attr.allocatable
13074 || sym->attr.external
13075 || sym->attr.dummy
13076 || sym->attr.pointer
13077 || sym->attr.in_equivalence
13078 || sym->attr.in_common
13079 || sym->attr.data
13080 || sym->module
13081 || sym->attr.cray_pointee
13082 || sym->attr.cray_pointer
13083 || sym->assoc)
13084 return NULL;
13085
13086 /* Get the appropriate init expression. */
13087 return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
13088 }
13089
13090 /* Add an initialization expression to a local variable. */
13091 static void
13092 apply_default_init_local (gfc_symbol *sym)
13093 {
13094 gfc_expr *init = NULL;
13095
13096 /* The symbol should be a variable or a function return value. */
13097 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
13098 || (sym->attr.function && sym->result != sym))
13099 return;
13100
13101 /* Try to build the initializer expression. If we can't initialize
13102 this symbol, then init will be NULL. */
13103 init = build_default_init_expr (sym);
13104 if (init == NULL)
13105 return;
13106
13107 /* For saved variables, we don't want to add an initializer at function
13108 entry, so we just add a static initializer. Note that automatic variables
13109 are stack allocated even with -fno-automatic; we have also to exclude
13110 result variable, which are also nonstatic. */
13111 if (!sym->attr.automatic
13112 && (sym->attr.save || sym->ns->save_all
13113 || (flag_max_stack_var_size == 0 && !sym->attr.result
13114 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
13115 && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
13116 {
13117 /* Don't clobber an existing initializer! */
13118 gcc_assert (sym->value == NULL);
13119 sym->value = init;
13120 return;
13121 }
13122
13123 build_init_assign (sym, init);
13124 }
13125
13126
13127 /* Resolution of common features of flavors variable and procedure. */
13128
13129 static bool
13130 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
13131 {
13132 gfc_array_spec *as;
13133
13134 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
13135 && sym->ts.u.derived && CLASS_DATA (sym))
13136 as = CLASS_DATA (sym)->as;
13137 else
13138 as = sym->as;
13139
13140 /* Constraints on deferred shape variable. */
13141 if (as == NULL || as->type != AS_DEFERRED)
13142 {
13143 bool pointer, allocatable, dimension;
13144
13145 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
13146 && sym->ts.u.derived && CLASS_DATA (sym))
13147 {
13148 pointer = CLASS_DATA (sym)->attr.class_pointer;
13149 allocatable = CLASS_DATA (sym)->attr.allocatable;
13150 dimension = CLASS_DATA (sym)->attr.dimension;
13151 }
13152 else
13153 {
13154 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
13155 allocatable = sym->attr.allocatable;
13156 dimension = sym->attr.dimension;
13157 }
13158
13159 if (allocatable)
13160 {
13161 if (dimension && as->type != AS_ASSUMED_RANK)
13162 {
13163 gfc_error ("Allocatable array %qs at %L must have a deferred "
13164 "shape or assumed rank", sym->name, &sym->declared_at);
13165 return false;
13166 }
13167 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
13168 "%qs at %L may not be ALLOCATABLE",
13169 sym->name, &sym->declared_at))
13170 return false;
13171 }
13172
13173 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
13174 {
13175 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
13176 "assumed rank", sym->name, &sym->declared_at);
13177 sym->error = 1;
13178 return false;
13179 }
13180 }
13181 else
13182 {
13183 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
13184 && sym->ts.type != BT_CLASS && !sym->assoc)
13185 {
13186 gfc_error ("Array %qs at %L cannot have a deferred shape",
13187 sym->name, &sym->declared_at);
13188 return false;
13189 }
13190 }
13191
13192 /* Constraints on polymorphic variables. */
13193 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
13194 {
13195 /* F03:C502. */
13196 if (sym->attr.class_ok
13197 && sym->ts.u.derived
13198 && !sym->attr.select_type_temporary
13199 && !UNLIMITED_POLY (sym)
13200 && CLASS_DATA (sym)->ts.u.derived
13201 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
13202 {
13203 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
13204 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
13205 &sym->declared_at);
13206 return false;
13207 }
13208
13209 /* F03:C509. */
13210 /* Assume that use associated symbols were checked in the module ns.
13211 Class-variables that are associate-names are also something special
13212 and excepted from the test. */
13213 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
13214 {
13215 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
13216 "or pointer", sym->name, &sym->declared_at);
13217 return false;
13218 }
13219 }
13220
13221 return true;
13222 }
13223
13224
13225 /* Additional checks for symbols with flavor variable and derived
13226 type. To be called from resolve_fl_variable. */
13227
13228 static bool
13229 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
13230 {
13231 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
13232
13233 /* Check to see if a derived type is blocked from being host
13234 associated by the presence of another class I symbol in the same
13235 namespace. 14.6.1.3 of the standard and the discussion on
13236 comp.lang.fortran. */
13237 if (sym->ts.u.derived
13238 && sym->ns != sym->ts.u.derived->ns
13239 && !sym->ts.u.derived->attr.use_assoc
13240 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
13241 {
13242 gfc_symbol *s;
13243 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
13244 if (s && s->attr.generic)
13245 s = gfc_find_dt_in_generic (s);
13246 if (s && !gfc_fl_struct (s->attr.flavor))
13247 {
13248 gfc_error ("The type %qs cannot be host associated at %L "
13249 "because it is blocked by an incompatible object "
13250 "of the same name declared at %L",
13251 sym->ts.u.derived->name, &sym->declared_at,
13252 &s->declared_at);
13253 return false;
13254 }
13255 }
13256
13257 /* 4th constraint in section 11.3: "If an object of a type for which
13258 component-initialization is specified (R429) appears in the
13259 specification-part of a module and does not have the ALLOCATABLE
13260 or POINTER attribute, the object shall have the SAVE attribute."
13261
13262 The check for initializers is performed with
13263 gfc_has_default_initializer because gfc_default_initializer generates
13264 a hidden default for allocatable components. */
13265 if (!(sym->value || no_init_flag) && sym->ns->proc_name
13266 && sym->ns->proc_name->attr.flavor == FL_MODULE
13267 && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
13268 && !sym->attr.pointer && !sym->attr.allocatable
13269 && gfc_has_default_initializer (sym->ts.u.derived)
13270 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
13271 "%qs at %L, needed due to the default "
13272 "initialization", sym->name, &sym->declared_at))
13273 return false;
13274
13275 /* Assign default initializer. */
13276 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
13277 && (!no_init_flag
13278 || (sym->attr.intent == INTENT_OUT
13279 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)))
13280 sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
13281
13282 return true;
13283 }
13284
13285
13286 /* F2008, C402 (R401): A colon shall not be used as a type-param-value
13287 except in the declaration of an entity or component that has the POINTER
13288 or ALLOCATABLE attribute. */
13289
13290 static bool
13291 deferred_requirements (gfc_symbol *sym)
13292 {
13293 if (sym->ts.deferred
13294 && !(sym->attr.pointer
13295 || sym->attr.allocatable
13296 || sym->attr.associate_var
13297 || sym->attr.omp_udr_artificial_var))
13298 {
13299 /* If a function has a result variable, only check the variable. */
13300 if (sym->result && sym->name != sym->result->name)
13301 return true;
13302
13303 gfc_error ("Entity %qs at %L has a deferred type parameter and "
13304 "requires either the POINTER or ALLOCATABLE attribute",
13305 sym->name, &sym->declared_at);
13306 return false;
13307 }
13308 return true;
13309 }
13310
13311
13312 /* Resolve symbols with flavor variable. */
13313
13314 static bool
13315 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
13316 {
13317 const char *auto_save_msg = "Automatic object %qs at %L cannot have the "
13318 "SAVE attribute";
13319
13320 if (!resolve_fl_var_and_proc (sym, mp_flag))
13321 return false;
13322
13323 /* Set this flag to check that variables are parameters of all entries.
13324 This check is effected by the call to gfc_resolve_expr through
13325 is_non_constant_shape_array. */
13326 bool saved_specification_expr = specification_expr;
13327 specification_expr = true;
13328
13329 if (sym->ns->proc_name
13330 && (sym->ns->proc_name->attr.flavor == FL_MODULE
13331 || sym->ns->proc_name->attr.is_main_program)
13332 && !sym->attr.use_assoc
13333 && !sym->attr.allocatable
13334 && !sym->attr.pointer
13335 && is_non_constant_shape_array (sym))
13336 {
13337 /* F08:C541. The shape of an array defined in a main program or module
13338 * needs to be constant. */
13339 gfc_error ("The module or main program array %qs at %L must "
13340 "have constant shape", sym->name, &sym->declared_at);
13341 specification_expr = saved_specification_expr;
13342 return false;
13343 }
13344
13345 /* Constraints on deferred type parameter. */
13346 if (!deferred_requirements (sym))
13347 return false;
13348
13349 if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
13350 {
13351 /* Make sure that character string variables with assumed length are
13352 dummy arguments. */
13353 gfc_expr *e = NULL;
13354
13355 if (sym->ts.u.cl)
13356 e = sym->ts.u.cl->length;
13357 else
13358 return false;
13359
13360 if (e == NULL && !sym->attr.dummy && !sym->attr.result
13361 && !sym->ts.deferred && !sym->attr.select_type_temporary
13362 && !sym->attr.omp_udr_artificial_var)
13363 {
13364 gfc_error ("Entity with assumed character length at %L must be a "
13365 "dummy argument or a PARAMETER", &sym->declared_at);
13366 specification_expr = saved_specification_expr;
13367 return false;
13368 }
13369
13370 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
13371 {
13372 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
13373 specification_expr = saved_specification_expr;
13374 return false;
13375 }
13376
13377 if (!gfc_is_constant_expr (e)
13378 && !(e->expr_type == EXPR_VARIABLE
13379 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
13380 {
13381 if (!sym->attr.use_assoc && sym->ns->proc_name
13382 && (sym->ns->proc_name->attr.flavor == FL_MODULE
13383 || sym->ns->proc_name->attr.is_main_program))
13384 {
13385 gfc_error ("%qs at %L must have constant character length "
13386 "in this context", sym->name, &sym->declared_at);
13387 specification_expr = saved_specification_expr;
13388 return false;
13389 }
13390 if (sym->attr.in_common)
13391 {
13392 gfc_error ("COMMON variable %qs at %L must have constant "
13393 "character length", sym->name, &sym->declared_at);
13394 specification_expr = saved_specification_expr;
13395 return false;
13396 }
13397 }
13398 }
13399
13400 if (sym->value == NULL && sym->attr.referenced)
13401 apply_default_init_local (sym); /* Try to apply a default initialization. */
13402
13403 /* Determine if the symbol may not have an initializer. */
13404 int no_init_flag = 0, automatic_flag = 0;
13405 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
13406 || sym->attr.intrinsic || sym->attr.result)
13407 no_init_flag = 1;
13408 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
13409 && is_non_constant_shape_array (sym))
13410 {
13411 no_init_flag = automatic_flag = 1;
13412
13413 /* Also, they must not have the SAVE attribute.
13414 SAVE_IMPLICIT is checked below. */
13415 if (sym->as && sym->attr.codimension)
13416 {
13417 int corank = sym->as->corank;
13418 sym->as->corank = 0;
13419 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
13420 sym->as->corank = corank;
13421 }
13422 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
13423 {
13424 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
13425 specification_expr = saved_specification_expr;
13426 return false;
13427 }
13428 }
13429
13430 /* Ensure that any initializer is simplified. */
13431 if (sym->value)
13432 gfc_simplify_expr (sym->value, 1);
13433
13434 /* Reject illegal initializers. */
13435 if (!sym->mark && sym->value)
13436 {
13437 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
13438 && CLASS_DATA (sym)->attr.allocatable))
13439 gfc_error ("Allocatable %qs at %L cannot have an initializer",
13440 sym->name, &sym->declared_at);
13441 else if (sym->attr.external)
13442 gfc_error ("External %qs at %L cannot have an initializer",
13443 sym->name, &sym->declared_at);
13444 else if (sym->attr.dummy)
13445 gfc_error ("Dummy %qs at %L cannot have an initializer",
13446 sym->name, &sym->declared_at);
13447 else if (sym->attr.intrinsic)
13448 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
13449 sym->name, &sym->declared_at);
13450 else if (sym->attr.result)
13451 gfc_error ("Function result %qs at %L cannot have an initializer",
13452 sym->name, &sym->declared_at);
13453 else if (automatic_flag)
13454 gfc_error ("Automatic array %qs at %L cannot have an initializer",
13455 sym->name, &sym->declared_at);
13456 else
13457 goto no_init_error;
13458 specification_expr = saved_specification_expr;
13459 return false;
13460 }
13461
13462 no_init_error:
13463 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
13464 {
13465 bool res = resolve_fl_variable_derived (sym, no_init_flag);
13466 specification_expr = saved_specification_expr;
13467 return res;
13468 }
13469
13470 specification_expr = saved_specification_expr;
13471 return true;
13472 }
13473
13474
13475 /* Compare the dummy characteristics of a module procedure interface
13476 declaration with the corresponding declaration in a submodule. */
13477 static gfc_formal_arglist *new_formal;
13478 static char errmsg[200];
13479
13480 static void
13481 compare_fsyms (gfc_symbol *sym)
13482 {
13483 gfc_symbol *fsym;
13484
13485 if (sym == NULL || new_formal == NULL)
13486 return;
13487
13488 fsym = new_formal->sym;
13489
13490 if (sym == fsym)
13491 return;
13492
13493 if (strcmp (sym->name, fsym->name) == 0)
13494 {
13495 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
13496 gfc_error ("%s at %L", errmsg, &fsym->declared_at);
13497 }
13498 }
13499
13500
13501 /* Resolve a procedure. */
13502
13503 static bool
13504 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
13505 {
13506 gfc_formal_arglist *arg;
13507 bool allocatable_or_pointer = false;
13508
13509 if (sym->attr.function
13510 && !resolve_fl_var_and_proc (sym, mp_flag))
13511 return false;
13512
13513 /* Constraints on deferred type parameter. */
13514 if (!deferred_requirements (sym))
13515 return false;
13516
13517 if (sym->ts.type == BT_CHARACTER)
13518 {
13519 gfc_charlen *cl = sym->ts.u.cl;
13520
13521 if (cl && cl->length && gfc_is_constant_expr (cl->length)
13522 && !resolve_charlen (cl))
13523 return false;
13524
13525 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
13526 && sym->attr.proc == PROC_ST_FUNCTION)
13527 {
13528 gfc_error ("Character-valued statement function %qs at %L must "
13529 "have constant length", sym->name, &sym->declared_at);
13530 return false;
13531 }
13532 }
13533
13534 /* Ensure that derived type for are not of a private type. Internal
13535 module procedures are excluded by 2.2.3.3 - i.e., they are not
13536 externally accessible and can access all the objects accessible in
13537 the host. */
13538 if (!(sym->ns->parent && sym->ns->parent->proc_name
13539 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
13540 && gfc_check_symbol_access (sym))
13541 {
13542 gfc_interface *iface;
13543
13544 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
13545 {
13546 if (arg->sym
13547 && arg->sym->ts.type == BT_DERIVED
13548 && arg->sym->ts.u.derived
13549 && !arg->sym->ts.u.derived->attr.use_assoc
13550 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
13551 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
13552 "and cannot be a dummy argument"
13553 " of %qs, which is PUBLIC at %L",
13554 arg->sym->name, sym->name,
13555 &sym->declared_at))
13556 {
13557 /* Stop this message from recurring. */
13558 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
13559 return false;
13560 }
13561 }
13562
13563 /* PUBLIC interfaces may expose PRIVATE procedures that take types
13564 PRIVATE to the containing module. */
13565 for (iface = sym->generic; iface; iface = iface->next)
13566 {
13567 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
13568 {
13569 if (arg->sym
13570 && arg->sym->ts.type == BT_DERIVED
13571 && !arg->sym->ts.u.derived->attr.use_assoc
13572 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
13573 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
13574 "PUBLIC interface %qs at %L "
13575 "takes dummy arguments of %qs which "
13576 "is PRIVATE", iface->sym->name,
13577 sym->name, &iface->sym->declared_at,
13578 gfc_typename(&arg->sym->ts)))
13579 {
13580 /* Stop this message from recurring. */
13581 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
13582 return false;
13583 }
13584 }
13585 }
13586 }
13587
13588 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
13589 && !sym->attr.proc_pointer)
13590 {
13591 gfc_error ("Function %qs at %L cannot have an initializer",
13592 sym->name, &sym->declared_at);
13593
13594 /* Make sure no second error is issued for this. */
13595 sym->value->error = 1;
13596 return false;
13597 }
13598
13599 /* An external symbol may not have an initializer because it is taken to be
13600 a procedure. Exception: Procedure Pointers. */
13601 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
13602 {
13603 gfc_error ("External object %qs at %L may not have an initializer",
13604 sym->name, &sym->declared_at);
13605 return false;
13606 }
13607
13608 /* An elemental function is required to return a scalar 12.7.1 */
13609 if (sym->attr.elemental && sym->attr.function
13610 && (sym->as || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13611 && CLASS_DATA (sym)->as)))
13612 {
13613 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
13614 "result", sym->name, &sym->declared_at);
13615 /* Reset so that the error only occurs once. */
13616 sym->attr.elemental = 0;
13617 return false;
13618 }
13619
13620 if (sym->attr.proc == PROC_ST_FUNCTION
13621 && (sym->attr.allocatable || sym->attr.pointer))
13622 {
13623 gfc_error ("Statement function %qs at %L may not have pointer or "
13624 "allocatable attribute", sym->name, &sym->declared_at);
13625 return false;
13626 }
13627
13628 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
13629 char-len-param shall not be array-valued, pointer-valued, recursive
13630 or pure. ....snip... A character value of * may only be used in the
13631 following ways: (i) Dummy arg of procedure - dummy associates with
13632 actual length; (ii) To declare a named constant; or (iii) External
13633 function - but length must be declared in calling scoping unit. */
13634 if (sym->attr.function
13635 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
13636 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
13637 {
13638 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
13639 || (sym->attr.recursive) || (sym->attr.pure))
13640 {
13641 if (sym->as && sym->as->rank)
13642 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13643 "array-valued", sym->name, &sym->declared_at);
13644
13645 if (sym->attr.pointer)
13646 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13647 "pointer-valued", sym->name, &sym->declared_at);
13648
13649 if (sym->attr.pure)
13650 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13651 "pure", sym->name, &sym->declared_at);
13652
13653 if (sym->attr.recursive)
13654 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13655 "recursive", sym->name, &sym->declared_at);
13656
13657 return false;
13658 }
13659
13660 /* Appendix B.2 of the standard. Contained functions give an
13661 error anyway. Deferred character length is an F2003 feature.
13662 Don't warn on intrinsic conversion functions, which start
13663 with two underscores. */
13664 if (!sym->attr.contained && !sym->ts.deferred
13665 && (sym->name[0] != '_' || sym->name[1] != '_'))
13666 gfc_notify_std (GFC_STD_F95_OBS,
13667 "CHARACTER(*) function %qs at %L",
13668 sym->name, &sym->declared_at);
13669 }
13670
13671 /* F2008, C1218. */
13672 if (sym->attr.elemental)
13673 {
13674 if (sym->attr.proc_pointer)
13675 {
13676 const char* name = (sym->attr.result ? sym->ns->proc_name->name
13677 : sym->name);
13678 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
13679 name, &sym->declared_at);
13680 return false;
13681 }
13682 if (sym->attr.dummy)
13683 {
13684 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
13685 sym->name, &sym->declared_at);
13686 return false;
13687 }
13688 }
13689
13690 /* F2018, C15100: "The result of an elemental function shall be scalar,
13691 and shall not have the POINTER or ALLOCATABLE attribute." The scalar
13692 pointer is tested and caught elsewhere. */
13693 if (sym->result)
13694 allocatable_or_pointer = sym->result->ts.type == BT_CLASS
13695 && CLASS_DATA (sym->result) ?
13696 (CLASS_DATA (sym->result)->attr.allocatable
13697 || CLASS_DATA (sym->result)->attr.pointer) :
13698 (sym->result->attr.allocatable
13699 || sym->result->attr.pointer);
13700
13701 if (sym->attr.elemental && sym->result
13702 && allocatable_or_pointer)
13703 {
13704 gfc_error ("Function result variable %qs at %L of elemental "
13705 "function %qs shall not have an ALLOCATABLE or POINTER "
13706 "attribute", sym->result->name,
13707 &sym->result->declared_at, sym->name);
13708 return false;
13709 }
13710
13711 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
13712 {
13713 gfc_formal_arglist *curr_arg;
13714 int has_non_interop_arg = 0;
13715
13716 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13717 sym->common_block))
13718 {
13719 /* Clear these to prevent looking at them again if there was an
13720 error. */
13721 sym->attr.is_bind_c = 0;
13722 sym->attr.is_c_interop = 0;
13723 sym->ts.is_c_interop = 0;
13724 }
13725 else
13726 {
13727 /* So far, no errors have been found. */
13728 sym->attr.is_c_interop = 1;
13729 sym->ts.is_c_interop = 1;
13730 }
13731
13732 curr_arg = gfc_sym_get_dummy_args (sym);
13733 while (curr_arg != NULL)
13734 {
13735 /* Skip implicitly typed dummy args here. */
13736 if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
13737 if (!gfc_verify_c_interop_param (curr_arg->sym))
13738 /* If something is found to fail, record the fact so we
13739 can mark the symbol for the procedure as not being
13740 BIND(C) to try and prevent multiple errors being
13741 reported. */
13742 has_non_interop_arg = 1;
13743
13744 curr_arg = curr_arg->next;
13745 }
13746
13747 /* See if any of the arguments were not interoperable and if so, clear
13748 the procedure symbol to prevent duplicate error messages. */
13749 if (has_non_interop_arg != 0)
13750 {
13751 sym->attr.is_c_interop = 0;
13752 sym->ts.is_c_interop = 0;
13753 sym->attr.is_bind_c = 0;
13754 }
13755 }
13756
13757 if (!sym->attr.proc_pointer)
13758 {
13759 if (sym->attr.save == SAVE_EXPLICIT)
13760 {
13761 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
13762 "in %qs at %L", sym->name, &sym->declared_at);
13763 return false;
13764 }
13765 if (sym->attr.intent)
13766 {
13767 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
13768 "in %qs at %L", sym->name, &sym->declared_at);
13769 return false;
13770 }
13771 if (sym->attr.subroutine && sym->attr.result)
13772 {
13773 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
13774 "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
13775 return false;
13776 }
13777 if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
13778 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
13779 || sym->attr.contained))
13780 {
13781 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
13782 "in %qs at %L", sym->name, &sym->declared_at);
13783 return false;
13784 }
13785 if (strcmp ("ppr@", sym->name) == 0)
13786 {
13787 gfc_error ("Procedure pointer result %qs at %L "
13788 "is missing the pointer attribute",
13789 sym->ns->proc_name->name, &sym->declared_at);
13790 return false;
13791 }
13792 }
13793
13794 /* Assume that a procedure whose body is not known has references
13795 to external arrays. */
13796 if (sym->attr.if_source != IFSRC_DECL)
13797 sym->attr.array_outer_dependency = 1;
13798
13799 /* Compare the characteristics of a module procedure with the
13800 interface declaration. Ideally this would be done with
13801 gfc_compare_interfaces but, at present, the formal interface
13802 cannot be copied to the ts.interface. */
13803 if (sym->attr.module_procedure
13804 && sym->attr.if_source == IFSRC_DECL)
13805 {
13806 gfc_symbol *iface;
13807 char name[2*GFC_MAX_SYMBOL_LEN + 1];
13808 char *module_name;
13809 char *submodule_name;
13810 strcpy (name, sym->ns->proc_name->name);
13811 module_name = strtok (name, ".");
13812 submodule_name = strtok (NULL, ".");
13813
13814 iface = sym->tlink;
13815 sym->tlink = NULL;
13816
13817 /* Make sure that the result uses the correct charlen for deferred
13818 length results. */
13819 if (iface && sym->result
13820 && iface->ts.type == BT_CHARACTER
13821 && iface->ts.deferred)
13822 sym->result->ts.u.cl = iface->ts.u.cl;
13823
13824 if (iface == NULL)
13825 goto check_formal;
13826
13827 /* Check the procedure characteristics. */
13828 if (sym->attr.elemental != iface->attr.elemental)
13829 {
13830 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
13831 "PROCEDURE at %L and its interface in %s",
13832 &sym->declared_at, module_name);
13833 return false;
13834 }
13835
13836 if (sym->attr.pure != iface->attr.pure)
13837 {
13838 gfc_error ("Mismatch in PURE attribute between MODULE "
13839 "PROCEDURE at %L and its interface in %s",
13840 &sym->declared_at, module_name);
13841 return false;
13842 }
13843
13844 if (sym->attr.recursive != iface->attr.recursive)
13845 {
13846 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
13847 "PROCEDURE at %L and its interface in %s",
13848 &sym->declared_at, module_name);
13849 return false;
13850 }
13851
13852 /* Check the result characteristics. */
13853 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
13854 {
13855 gfc_error ("%s between the MODULE PROCEDURE declaration "
13856 "in MODULE %qs and the declaration at %L in "
13857 "(SUB)MODULE %qs",
13858 errmsg, module_name, &sym->declared_at,
13859 submodule_name ? submodule_name : module_name);
13860 return false;
13861 }
13862
13863 check_formal:
13864 /* Check the characteristics of the formal arguments. */
13865 if (sym->formal && sym->formal_ns)
13866 {
13867 for (arg = sym->formal; arg && arg->sym; arg = arg->next)
13868 {
13869 new_formal = arg;
13870 gfc_traverse_ns (sym->formal_ns, compare_fsyms);
13871 }
13872 }
13873 }
13874 return true;
13875 }
13876
13877
13878 /* Resolve a list of finalizer procedures. That is, after they have hopefully
13879 been defined and we now know their defined arguments, check that they fulfill
13880 the requirements of the standard for procedures used as finalizers. */
13881
13882 static bool
13883 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
13884 {
13885 gfc_finalizer* list;
13886 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
13887 bool result = true;
13888 bool seen_scalar = false;
13889 gfc_symbol *vtab;
13890 gfc_component *c;
13891 gfc_symbol *parent = gfc_get_derived_super_type (derived);
13892
13893 if (parent)
13894 gfc_resolve_finalizers (parent, finalizable);
13895
13896 /* Ensure that derived-type components have a their finalizers resolved. */
13897 bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
13898 for (c = derived->components; c; c = c->next)
13899 if (c->ts.type == BT_DERIVED
13900 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
13901 {
13902 bool has_final2 = false;
13903 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
13904 return false; /* Error. */
13905 has_final = has_final || has_final2;
13906 }
13907 /* Return early if not finalizable. */
13908 if (!has_final)
13909 {
13910 if (finalizable)
13911 *finalizable = false;
13912 return true;
13913 }
13914
13915 /* Walk over the list of finalizer-procedures, check them, and if any one
13916 does not fit in with the standard's definition, print an error and remove
13917 it from the list. */
13918 prev_link = &derived->f2k_derived->finalizers;
13919 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
13920 {
13921 gfc_formal_arglist *dummy_args;
13922 gfc_symbol* arg;
13923 gfc_finalizer* i;
13924 int my_rank;
13925
13926 /* Skip this finalizer if we already resolved it. */
13927 if (list->proc_tree)
13928 {
13929 if (list->proc_tree->n.sym->formal->sym->as == NULL
13930 || list->proc_tree->n.sym->formal->sym->as->rank == 0)
13931 seen_scalar = true;
13932 prev_link = &(list->next);
13933 continue;
13934 }
13935
13936 /* Check this exists and is a SUBROUTINE. */
13937 if (!list->proc_sym->attr.subroutine)
13938 {
13939 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
13940 list->proc_sym->name, &list->where);
13941 goto error;
13942 }
13943
13944 /* We should have exactly one argument. */
13945 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
13946 if (!dummy_args || dummy_args->next)
13947 {
13948 gfc_error ("FINAL procedure at %L must have exactly one argument",
13949 &list->where);
13950 goto error;
13951 }
13952 arg = dummy_args->sym;
13953
13954 if (arg->as && arg->as->type == AS_ASSUMED_RANK
13955 && ((list != derived->f2k_derived->finalizers) || list->next))
13956 {
13957 gfc_error ("FINAL procedure at %L with assumed rank argument must "
13958 "be the only finalizer with the same kind/type "
13959 "(F2018: C790)", &list->where);
13960 goto error;
13961 }
13962
13963 /* This argument must be of our type. */
13964 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
13965 {
13966 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
13967 &arg->declared_at, derived->name);
13968 goto error;
13969 }
13970
13971 /* It must neither be a pointer nor allocatable nor optional. */
13972 if (arg->attr.pointer)
13973 {
13974 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
13975 &arg->declared_at);
13976 goto error;
13977 }
13978 if (arg->attr.allocatable)
13979 {
13980 gfc_error ("Argument of FINAL procedure at %L must not be"
13981 " ALLOCATABLE", &arg->declared_at);
13982 goto error;
13983 }
13984 if (arg->attr.optional)
13985 {
13986 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
13987 &arg->declared_at);
13988 goto error;
13989 }
13990
13991 /* It must not be INTENT(OUT). */
13992 if (arg->attr.intent == INTENT_OUT)
13993 {
13994 gfc_error ("Argument of FINAL procedure at %L must not be"
13995 " INTENT(OUT)", &arg->declared_at);
13996 goto error;
13997 }
13998
13999 /* Warn if the procedure is non-scalar and not assumed shape. */
14000 if (warn_surprising && arg->as && arg->as->rank != 0
14001 && arg->as->type != AS_ASSUMED_SHAPE)
14002 gfc_warning (OPT_Wsurprising,
14003 "Non-scalar FINAL procedure at %L should have assumed"
14004 " shape argument", &arg->declared_at);
14005
14006 /* Check that it does not match in kind and rank with a FINAL procedure
14007 defined earlier. To really loop over the *earlier* declarations,
14008 we need to walk the tail of the list as new ones were pushed at the
14009 front. */
14010 /* TODO: Handle kind parameters once they are implemented. */
14011 my_rank = (arg->as ? arg->as->rank : 0);
14012 for (i = list->next; i; i = i->next)
14013 {
14014 gfc_formal_arglist *dummy_args;
14015
14016 /* Argument list might be empty; that is an error signalled earlier,
14017 but we nevertheless continued resolving. */
14018 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
14019 if (dummy_args)
14020 {
14021 gfc_symbol* i_arg = dummy_args->sym;
14022 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
14023 if (i_rank == my_rank)
14024 {
14025 gfc_error ("FINAL procedure %qs declared at %L has the same"
14026 " rank (%d) as %qs",
14027 list->proc_sym->name, &list->where, my_rank,
14028 i->proc_sym->name);
14029 goto error;
14030 }
14031 }
14032 }
14033
14034 /* Is this the/a scalar finalizer procedure? */
14035 if (my_rank == 0)
14036 seen_scalar = true;
14037
14038 /* Find the symtree for this procedure. */
14039 gcc_assert (!list->proc_tree);
14040 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
14041
14042 prev_link = &list->next;
14043 continue;
14044
14045 /* Remove wrong nodes immediately from the list so we don't risk any
14046 troubles in the future when they might fail later expectations. */
14047 error:
14048 i = list;
14049 *prev_link = list->next;
14050 gfc_free_finalizer (i);
14051 result = false;
14052 }
14053
14054 if (result == false)
14055 return false;
14056
14057 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
14058 were nodes in the list, must have been for arrays. It is surely a good
14059 idea to have a scalar version there if there's something to finalize. */
14060 if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
14061 gfc_warning (OPT_Wsurprising,
14062 "Only array FINAL procedures declared for derived type %qs"
14063 " defined at %L, suggest also scalar one unless an assumed"
14064 " rank finalizer has been declared",
14065 derived->name, &derived->declared_at);
14066
14067 vtab = gfc_find_derived_vtab (derived);
14068 c = vtab->ts.u.derived->components->next->next->next->next->next;
14069 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
14070
14071 if (finalizable)
14072 *finalizable = true;
14073
14074 return true;
14075 }
14076
14077
14078 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
14079
14080 static bool
14081 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
14082 const char* generic_name, locus where)
14083 {
14084 gfc_symbol *sym1, *sym2;
14085 const char *pass1, *pass2;
14086 gfc_formal_arglist *dummy_args;
14087
14088 gcc_assert (t1->specific && t2->specific);
14089 gcc_assert (!t1->specific->is_generic);
14090 gcc_assert (!t2->specific->is_generic);
14091 gcc_assert (t1->is_operator == t2->is_operator);
14092
14093 sym1 = t1->specific->u.specific->n.sym;
14094 sym2 = t2->specific->u.specific->n.sym;
14095
14096 if (sym1 == sym2)
14097 return true;
14098
14099 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
14100 if (sym1->attr.subroutine != sym2->attr.subroutine
14101 || sym1->attr.function != sym2->attr.function)
14102 {
14103 gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
14104 " GENERIC %qs at %L",
14105 sym1->name, sym2->name, generic_name, &where);
14106 return false;
14107 }
14108
14109 /* Determine PASS arguments. */
14110 if (t1->specific->nopass)
14111 pass1 = NULL;
14112 else if (t1->specific->pass_arg)
14113 pass1 = t1->specific->pass_arg;
14114 else
14115 {
14116 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
14117 if (dummy_args)
14118 pass1 = dummy_args->sym->name;
14119 else
14120 pass1 = NULL;
14121 }
14122 if (t2->specific->nopass)
14123 pass2 = NULL;
14124 else if (t2->specific->pass_arg)
14125 pass2 = t2->specific->pass_arg;
14126 else
14127 {
14128 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
14129 if (dummy_args)
14130 pass2 = dummy_args->sym->name;
14131 else
14132 pass2 = NULL;
14133 }
14134
14135 /* Compare the interfaces. */
14136 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
14137 NULL, 0, pass1, pass2))
14138 {
14139 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
14140 sym1->name, sym2->name, generic_name, &where);
14141 return false;
14142 }
14143
14144 return true;
14145 }
14146
14147
14148 /* Worker function for resolving a generic procedure binding; this is used to
14149 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
14150
14151 The difference between those cases is finding possible inherited bindings
14152 that are overridden, as one has to look for them in tb_sym_root,
14153 tb_uop_root or tb_op, respectively. Thus the caller must already find
14154 the super-type and set p->overridden correctly. */
14155
14156 static bool
14157 resolve_tb_generic_targets (gfc_symbol* super_type,
14158 gfc_typebound_proc* p, const char* name)
14159 {
14160 gfc_tbp_generic* target;
14161 gfc_symtree* first_target;
14162 gfc_symtree* inherited;
14163
14164 gcc_assert (p && p->is_generic);
14165
14166 /* Try to find the specific bindings for the symtrees in our target-list. */
14167 gcc_assert (p->u.generic);
14168 for (target = p->u.generic; target; target = target->next)
14169 if (!target->specific)
14170 {
14171 gfc_typebound_proc* overridden_tbp;
14172 gfc_tbp_generic* g;
14173 const char* target_name;
14174
14175 target_name = target->specific_st->name;
14176
14177 /* Defined for this type directly. */
14178 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
14179 {
14180 target->specific = target->specific_st->n.tb;
14181 goto specific_found;
14182 }
14183
14184 /* Look for an inherited specific binding. */
14185 if (super_type)
14186 {
14187 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
14188 true, NULL);
14189
14190 if (inherited)
14191 {
14192 gcc_assert (inherited->n.tb);
14193 target->specific = inherited->n.tb;
14194 goto specific_found;
14195 }
14196 }
14197
14198 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
14199 " at %L", target_name, name, &p->where);
14200 return false;
14201
14202 /* Once we've found the specific binding, check it is not ambiguous with
14203 other specifics already found or inherited for the same GENERIC. */
14204 specific_found:
14205 gcc_assert (target->specific);
14206
14207 /* This must really be a specific binding! */
14208 if (target->specific->is_generic)
14209 {
14210 gfc_error ("GENERIC %qs at %L must target a specific binding,"
14211 " %qs is GENERIC, too", name, &p->where, target_name);
14212 return false;
14213 }
14214
14215 /* Check those already resolved on this type directly. */
14216 for (g = p->u.generic; g; g = g->next)
14217 if (g != target && g->specific
14218 && !check_generic_tbp_ambiguity (target, g, name, p->where))
14219 return false;
14220
14221 /* Check for ambiguity with inherited specific targets. */
14222 for (overridden_tbp = p->overridden; overridden_tbp;
14223 overridden_tbp = overridden_tbp->overridden)
14224 if (overridden_tbp->is_generic)
14225 {
14226 for (g = overridden_tbp->u.generic; g; g = g->next)
14227 {
14228 gcc_assert (g->specific);
14229 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
14230 return false;
14231 }
14232 }
14233 }
14234
14235 /* If we attempt to "overwrite" a specific binding, this is an error. */
14236 if (p->overridden && !p->overridden->is_generic)
14237 {
14238 gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
14239 " the same name", name, &p->where);
14240 return false;
14241 }
14242
14243 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
14244 all must have the same attributes here. */
14245 first_target = p->u.generic->specific->u.specific;
14246 gcc_assert (first_target);
14247 p->subroutine = first_target->n.sym->attr.subroutine;
14248 p->function = first_target->n.sym->attr.function;
14249
14250 return true;
14251 }
14252
14253
14254 /* Resolve a GENERIC procedure binding for a derived type. */
14255
14256 static bool
14257 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
14258 {
14259 gfc_symbol* super_type;
14260
14261 /* Find the overridden binding if any. */
14262 st->n.tb->overridden = NULL;
14263 super_type = gfc_get_derived_super_type (derived);
14264 if (super_type)
14265 {
14266 gfc_symtree* overridden;
14267 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
14268 true, NULL);
14269
14270 if (overridden && overridden->n.tb)
14271 st->n.tb->overridden = overridden->n.tb;
14272 }
14273
14274 /* Resolve using worker function. */
14275 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
14276 }
14277
14278
14279 /* Retrieve the target-procedure of an operator binding and do some checks in
14280 common for intrinsic and user-defined type-bound operators. */
14281
14282 static gfc_symbol*
14283 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
14284 {
14285 gfc_symbol* target_proc;
14286
14287 gcc_assert (target->specific && !target->specific->is_generic);
14288 target_proc = target->specific->u.specific->n.sym;
14289 gcc_assert (target_proc);
14290
14291 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
14292 if (target->specific->nopass)
14293 {
14294 gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
14295 return NULL;
14296 }
14297
14298 return target_proc;
14299 }
14300
14301
14302 /* Resolve a type-bound intrinsic operator. */
14303
14304 static bool
14305 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
14306 gfc_typebound_proc* p)
14307 {
14308 gfc_symbol* super_type;
14309 gfc_tbp_generic* target;
14310
14311 /* If there's already an error here, do nothing (but don't fail again). */
14312 if (p->error)
14313 return true;
14314
14315 /* Operators should always be GENERIC bindings. */
14316 gcc_assert (p->is_generic);
14317
14318 /* Look for an overridden binding. */
14319 super_type = gfc_get_derived_super_type (derived);
14320 if (super_type && super_type->f2k_derived)
14321 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
14322 op, true, NULL);
14323 else
14324 p->overridden = NULL;
14325
14326 /* Resolve general GENERIC properties using worker function. */
14327 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
14328 goto error;
14329
14330 /* Check the targets to be procedures of correct interface. */
14331 for (target = p->u.generic; target; target = target->next)
14332 {
14333 gfc_symbol* target_proc;
14334
14335 target_proc = get_checked_tb_operator_target (target, p->where);
14336 if (!target_proc)
14337 goto error;
14338
14339 if (!gfc_check_operator_interface (target_proc, op, p->where))
14340 goto error;
14341
14342 /* Add target to non-typebound operator list. */
14343 if (!target->specific->deferred && !derived->attr.use_assoc
14344 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
14345 {
14346 gfc_interface *head, *intr;
14347
14348 /* Preempt 'gfc_check_new_interface' for submodules, where the
14349 mechanism for handling module procedures winds up resolving
14350 operator interfaces twice and would otherwise cause an error. */
14351 for (intr = derived->ns->op[op]; intr; intr = intr->next)
14352 if (intr->sym == target_proc
14353 && target_proc->attr.used_in_submodule)
14354 return true;
14355
14356 if (!gfc_check_new_interface (derived->ns->op[op],
14357 target_proc, p->where))
14358 return false;
14359 head = derived->ns->op[op];
14360 intr = gfc_get_interface ();
14361 intr->sym = target_proc;
14362 intr->where = p->where;
14363 intr->next = head;
14364 derived->ns->op[op] = intr;
14365 }
14366 }
14367
14368 return true;
14369
14370 error:
14371 p->error = 1;
14372 return false;
14373 }
14374
14375
14376 /* Resolve a type-bound user operator (tree-walker callback). */
14377
14378 static gfc_symbol* resolve_bindings_derived;
14379 static bool resolve_bindings_result;
14380
14381 static bool check_uop_procedure (gfc_symbol* sym, locus where);
14382
14383 static void
14384 resolve_typebound_user_op (gfc_symtree* stree)
14385 {
14386 gfc_symbol* super_type;
14387 gfc_tbp_generic* target;
14388
14389 gcc_assert (stree && stree->n.tb);
14390
14391 if (stree->n.tb->error)
14392 return;
14393
14394 /* Operators should always be GENERIC bindings. */
14395 gcc_assert (stree->n.tb->is_generic);
14396
14397 /* Find overridden procedure, if any. */
14398 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
14399 if (super_type && super_type->f2k_derived)
14400 {
14401 gfc_symtree* overridden;
14402 overridden = gfc_find_typebound_user_op (super_type, NULL,
14403 stree->name, true, NULL);
14404
14405 if (overridden && overridden->n.tb)
14406 stree->n.tb->overridden = overridden->n.tb;
14407 }
14408 else
14409 stree->n.tb->overridden = NULL;
14410
14411 /* Resolve basically using worker function. */
14412 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
14413 goto error;
14414
14415 /* Check the targets to be functions of correct interface. */
14416 for (target = stree->n.tb->u.generic; target; target = target->next)
14417 {
14418 gfc_symbol* target_proc;
14419
14420 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
14421 if (!target_proc)
14422 goto error;
14423
14424 if (!check_uop_procedure (target_proc, stree->n.tb->where))
14425 goto error;
14426 }
14427
14428 return;
14429
14430 error:
14431 resolve_bindings_result = false;
14432 stree->n.tb->error = 1;
14433 }
14434
14435
14436 /* Resolve the type-bound procedures for a derived type. */
14437
14438 static void
14439 resolve_typebound_procedure (gfc_symtree* stree)
14440 {
14441 gfc_symbol* proc;
14442 locus where;
14443 gfc_symbol* me_arg;
14444 gfc_symbol* super_type;
14445 gfc_component* comp;
14446
14447 gcc_assert (stree);
14448
14449 /* Undefined specific symbol from GENERIC target definition. */
14450 if (!stree->n.tb)
14451 return;
14452
14453 if (stree->n.tb->error)
14454 return;
14455
14456 /* If this is a GENERIC binding, use that routine. */
14457 if (stree->n.tb->is_generic)
14458 {
14459 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
14460 goto error;
14461 return;
14462 }
14463
14464 /* Get the target-procedure to check it. */
14465 gcc_assert (!stree->n.tb->is_generic);
14466 gcc_assert (stree->n.tb->u.specific);
14467 proc = stree->n.tb->u.specific->n.sym;
14468 where = stree->n.tb->where;
14469
14470 /* Default access should already be resolved from the parser. */
14471 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
14472
14473 if (stree->n.tb->deferred)
14474 {
14475 if (!check_proc_interface (proc, &where))
14476 goto error;
14477 }
14478 else
14479 {
14480 /* If proc has not been resolved at this point, proc->name may
14481 actually be a USE associated entity. See PR fortran/89647. */
14482 if (!proc->resolve_symbol_called
14483 && proc->attr.function == 0 && proc->attr.subroutine == 0)
14484 {
14485 gfc_symbol *tmp;
14486 gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
14487 if (tmp && tmp->attr.use_assoc)
14488 {
14489 proc->module = tmp->module;
14490 proc->attr.proc = tmp->attr.proc;
14491 proc->attr.function = tmp->attr.function;
14492 proc->attr.subroutine = tmp->attr.subroutine;
14493 proc->attr.use_assoc = tmp->attr.use_assoc;
14494 proc->ts = tmp->ts;
14495 proc->result = tmp->result;
14496 }
14497 }
14498
14499 /* Check for F08:C465. */
14500 if ((!proc->attr.subroutine && !proc->attr.function)
14501 || (proc->attr.proc != PROC_MODULE
14502 && proc->attr.if_source != IFSRC_IFBODY
14503 && !proc->attr.module_procedure)
14504 || proc->attr.abstract)
14505 {
14506 gfc_error ("%qs must be a module procedure or an external "
14507 "procedure with an explicit interface at %L",
14508 proc->name, &where);
14509 goto error;
14510 }
14511 }
14512
14513 stree->n.tb->subroutine = proc->attr.subroutine;
14514 stree->n.tb->function = proc->attr.function;
14515
14516 /* Find the super-type of the current derived type. We could do this once and
14517 store in a global if speed is needed, but as long as not I believe this is
14518 more readable and clearer. */
14519 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
14520
14521 /* If PASS, resolve and check arguments if not already resolved / loaded
14522 from a .mod file. */
14523 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
14524 {
14525 gfc_formal_arglist *dummy_args;
14526
14527 dummy_args = gfc_sym_get_dummy_args (proc);
14528 if (stree->n.tb->pass_arg)
14529 {
14530 gfc_formal_arglist *i;
14531
14532 /* If an explicit passing argument name is given, walk the arg-list
14533 and look for it. */
14534
14535 me_arg = NULL;
14536 stree->n.tb->pass_arg_num = 1;
14537 for (i = dummy_args; i; i = i->next)
14538 {
14539 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
14540 {
14541 me_arg = i->sym;
14542 break;
14543 }
14544 ++stree->n.tb->pass_arg_num;
14545 }
14546
14547 if (!me_arg)
14548 {
14549 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
14550 " argument %qs",
14551 proc->name, stree->n.tb->pass_arg, &where,
14552 stree->n.tb->pass_arg);
14553 goto error;
14554 }
14555 }
14556 else
14557 {
14558 /* Otherwise, take the first one; there should in fact be at least
14559 one. */
14560 stree->n.tb->pass_arg_num = 1;
14561 if (!dummy_args)
14562 {
14563 gfc_error ("Procedure %qs with PASS at %L must have at"
14564 " least one argument", proc->name, &where);
14565 goto error;
14566 }
14567 me_arg = dummy_args->sym;
14568 }
14569
14570 /* Now check that the argument-type matches and the passed-object
14571 dummy argument is generally fine. */
14572
14573 gcc_assert (me_arg);
14574
14575 if (me_arg->ts.type != BT_CLASS)
14576 {
14577 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14578 " at %L", proc->name, &where);
14579 goto error;
14580 }
14581
14582 if (CLASS_DATA (me_arg)->ts.u.derived
14583 != resolve_bindings_derived)
14584 {
14585 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14586 " the derived-type %qs", me_arg->name, proc->name,
14587 me_arg->name, &where, resolve_bindings_derived->name);
14588 goto error;
14589 }
14590
14591 gcc_assert (me_arg->ts.type == BT_CLASS);
14592 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
14593 {
14594 gfc_error ("Passed-object dummy argument of %qs at %L must be"
14595 " scalar", proc->name, &where);
14596 goto error;
14597 }
14598 if (CLASS_DATA (me_arg)->attr.allocatable)
14599 {
14600 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14601 " be ALLOCATABLE", proc->name, &where);
14602 goto error;
14603 }
14604 if (CLASS_DATA (me_arg)->attr.class_pointer)
14605 {
14606 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14607 " be POINTER", proc->name, &where);
14608 goto error;
14609 }
14610 }
14611
14612 /* If we are extending some type, check that we don't override a procedure
14613 flagged NON_OVERRIDABLE. */
14614 stree->n.tb->overridden = NULL;
14615 if (super_type)
14616 {
14617 gfc_symtree* overridden;
14618 overridden = gfc_find_typebound_proc (super_type, NULL,
14619 stree->name, true, NULL);
14620
14621 if (overridden)
14622 {
14623 if (overridden->n.tb)
14624 stree->n.tb->overridden = overridden->n.tb;
14625
14626 if (!gfc_check_typebound_override (stree, overridden))
14627 goto error;
14628 }
14629 }
14630
14631 /* See if there's a name collision with a component directly in this type. */
14632 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
14633 if (!strcmp (comp->name, stree->name))
14634 {
14635 gfc_error ("Procedure %qs at %L has the same name as a component of"
14636 " %qs",
14637 stree->name, &where, resolve_bindings_derived->name);
14638 goto error;
14639 }
14640
14641 /* Try to find a name collision with an inherited component. */
14642 if (super_type && gfc_find_component (super_type, stree->name, true, true,
14643 NULL))
14644 {
14645 gfc_error ("Procedure %qs at %L has the same name as an inherited"
14646 " component of %qs",
14647 stree->name, &where, resolve_bindings_derived->name);
14648 goto error;
14649 }
14650
14651 stree->n.tb->error = 0;
14652 return;
14653
14654 error:
14655 resolve_bindings_result = false;
14656 stree->n.tb->error = 1;
14657 }
14658
14659
14660 static bool
14661 resolve_typebound_procedures (gfc_symbol* derived)
14662 {
14663 int op;
14664 gfc_symbol* super_type;
14665
14666 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
14667 return true;
14668
14669 super_type = gfc_get_derived_super_type (derived);
14670 if (super_type)
14671 resolve_symbol (super_type);
14672
14673 resolve_bindings_derived = derived;
14674 resolve_bindings_result = true;
14675
14676 if (derived->f2k_derived->tb_sym_root)
14677 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
14678 &resolve_typebound_procedure);
14679
14680 if (derived->f2k_derived->tb_uop_root)
14681 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
14682 &resolve_typebound_user_op);
14683
14684 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
14685 {
14686 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
14687 if (p && !resolve_typebound_intrinsic_op (derived,
14688 (gfc_intrinsic_op)op, p))
14689 resolve_bindings_result = false;
14690 }
14691
14692 return resolve_bindings_result;
14693 }
14694
14695
14696 /* Add a derived type to the dt_list. The dt_list is used in trans-types.cc
14697 to give all identical derived types the same backend_decl. */
14698 static void
14699 add_dt_to_dt_list (gfc_symbol *derived)
14700 {
14701 if (!derived->dt_next)
14702 {
14703 if (gfc_derived_types)
14704 {
14705 derived->dt_next = gfc_derived_types->dt_next;
14706 gfc_derived_types->dt_next = derived;
14707 }
14708 else
14709 {
14710 derived->dt_next = derived;
14711 }
14712 gfc_derived_types = derived;
14713 }
14714 }
14715
14716
14717 /* Ensure that a derived-type is really not abstract, meaning that every
14718 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
14719
14720 static bool
14721 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
14722 {
14723 if (!st)
14724 return true;
14725
14726 if (!ensure_not_abstract_walker (sub, st->left))
14727 return false;
14728 if (!ensure_not_abstract_walker (sub, st->right))
14729 return false;
14730
14731 if (st->n.tb && st->n.tb->deferred)
14732 {
14733 gfc_symtree* overriding;
14734 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
14735 if (!overriding)
14736 return false;
14737 gcc_assert (overriding->n.tb);
14738 if (overriding->n.tb->deferred)
14739 {
14740 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
14741 " %qs is DEFERRED and not overridden",
14742 sub->name, &sub->declared_at, st->name);
14743 return false;
14744 }
14745 }
14746
14747 return true;
14748 }
14749
14750 static bool
14751 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
14752 {
14753 /* The algorithm used here is to recursively travel up the ancestry of sub
14754 and for each ancestor-type, check all bindings. If any of them is
14755 DEFERRED, look it up starting from sub and see if the found (overriding)
14756 binding is not DEFERRED.
14757 This is not the most efficient way to do this, but it should be ok and is
14758 clearer than something sophisticated. */
14759
14760 gcc_assert (ancestor && !sub->attr.abstract);
14761
14762 if (!ancestor->attr.abstract)
14763 return true;
14764
14765 /* Walk bindings of this ancestor. */
14766 if (ancestor->f2k_derived)
14767 {
14768 bool t;
14769 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
14770 if (!t)
14771 return false;
14772 }
14773
14774 /* Find next ancestor type and recurse on it. */
14775 ancestor = gfc_get_derived_super_type (ancestor);
14776 if (ancestor)
14777 return ensure_not_abstract (sub, ancestor);
14778
14779 return true;
14780 }
14781
14782
14783 /* This check for typebound defined assignments is done recursively
14784 since the order in which derived types are resolved is not always in
14785 order of the declarations. */
14786
14787 static void
14788 check_defined_assignments (gfc_symbol *derived)
14789 {
14790 gfc_component *c;
14791
14792 for (c = derived->components; c; c = c->next)
14793 {
14794 if (!gfc_bt_struct (c->ts.type)
14795 || c->attr.pointer
14796 || c->attr.proc_pointer_comp
14797 || c->attr.class_pointer
14798 || c->attr.proc_pointer)
14799 continue;
14800
14801 if (c->ts.u.derived->attr.defined_assign_comp
14802 || (c->ts.u.derived->f2k_derived
14803 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
14804 {
14805 derived->attr.defined_assign_comp = 1;
14806 return;
14807 }
14808
14809 if (c->attr.allocatable)
14810 continue;
14811
14812 check_defined_assignments (c->ts.u.derived);
14813 if (c->ts.u.derived->attr.defined_assign_comp)
14814 {
14815 derived->attr.defined_assign_comp = 1;
14816 return;
14817 }
14818 }
14819 }
14820
14821
14822 /* Resolve a single component of a derived type or structure. */
14823
14824 static bool
14825 resolve_component (gfc_component *c, gfc_symbol *sym)
14826 {
14827 gfc_symbol *super_type;
14828 symbol_attribute *attr;
14829
14830 if (c->attr.artificial)
14831 return true;
14832
14833 /* Do not allow vtype components to be resolved in nameless namespaces
14834 such as block data because the procedure pointers will cause ICEs
14835 and vtables are not needed in these contexts. */
14836 if (sym->attr.vtype && sym->attr.use_assoc
14837 && sym->ns->proc_name == NULL)
14838 return true;
14839
14840 /* F2008, C442. */
14841 if ((!sym->attr.is_class || c != sym->components)
14842 && c->attr.codimension
14843 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
14844 {
14845 gfc_error ("Coarray component %qs at %L must be allocatable with "
14846 "deferred shape", c->name, &c->loc);
14847 return false;
14848 }
14849
14850 /* F2008, C443. */
14851 if (c->attr.codimension && c->ts.type == BT_DERIVED
14852 && c->ts.u.derived->ts.is_iso_c)
14853 {
14854 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
14855 "shall not be a coarray", c->name, &c->loc);
14856 return false;
14857 }
14858
14859 /* F2008, C444. */
14860 if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
14861 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
14862 || c->attr.allocatable))
14863 {
14864 gfc_error ("Component %qs at %L with coarray component "
14865 "shall be a nonpointer, nonallocatable scalar",
14866 c->name, &c->loc);
14867 return false;
14868 }
14869
14870 /* F2008, C448. */
14871 if (c->ts.type == BT_CLASS)
14872 {
14873 if (c->attr.class_ok && CLASS_DATA (c))
14874 {
14875 attr = &(CLASS_DATA (c)->attr);
14876
14877 /* Fix up contiguous attribute. */
14878 if (c->attr.contiguous)
14879 attr->contiguous = 1;
14880 }
14881 else
14882 attr = NULL;
14883 }
14884 else
14885 attr = &c->attr;
14886
14887 if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
14888 {
14889 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
14890 "is not an array pointer", c->name, &c->loc);
14891 return false;
14892 }
14893
14894 /* F2003, 15.2.1 - length has to be one. */
14895 if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
14896 && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
14897 || !gfc_is_constant_expr (c->ts.u.cl->length)
14898 || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
14899 {
14900 gfc_error ("Component %qs of BIND(C) type at %L must have length one",
14901 c->name, &c->loc);
14902 return false;
14903 }
14904
14905 if (c->attr.proc_pointer && c->ts.interface)
14906 {
14907 gfc_symbol *ifc = c->ts.interface;
14908
14909 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
14910 {
14911 c->tb->error = 1;
14912 return false;
14913 }
14914
14915 if (ifc->attr.if_source || ifc->attr.intrinsic)
14916 {
14917 /* Resolve interface and copy attributes. */
14918 if (ifc->formal && !ifc->formal_ns)
14919 resolve_symbol (ifc);
14920 if (ifc->attr.intrinsic)
14921 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
14922
14923 if (ifc->result)
14924 {
14925 c->ts = ifc->result->ts;
14926 c->attr.allocatable = ifc->result->attr.allocatable;
14927 c->attr.pointer = ifc->result->attr.pointer;
14928 c->attr.dimension = ifc->result->attr.dimension;
14929 c->as = gfc_copy_array_spec (ifc->result->as);
14930 c->attr.class_ok = ifc->result->attr.class_ok;
14931 }
14932 else
14933 {
14934 c->ts = ifc->ts;
14935 c->attr.allocatable = ifc->attr.allocatable;
14936 c->attr.pointer = ifc->attr.pointer;
14937 c->attr.dimension = ifc->attr.dimension;
14938 c->as = gfc_copy_array_spec (ifc->as);
14939 c->attr.class_ok = ifc->attr.class_ok;
14940 }
14941 c->ts.interface = ifc;
14942 c->attr.function = ifc->attr.function;
14943 c->attr.subroutine = ifc->attr.subroutine;
14944
14945 c->attr.pure = ifc->attr.pure;
14946 c->attr.elemental = ifc->attr.elemental;
14947 c->attr.recursive = ifc->attr.recursive;
14948 c->attr.always_explicit = ifc->attr.always_explicit;
14949 c->attr.ext_attr |= ifc->attr.ext_attr;
14950 /* Copy char length. */
14951 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
14952 {
14953 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
14954 if (cl->length && !cl->resolved
14955 && !gfc_resolve_expr (cl->length))
14956 {
14957 c->tb->error = 1;
14958 return false;
14959 }
14960 c->ts.u.cl = cl;
14961 }
14962 }
14963 }
14964 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
14965 {
14966 /* Since PPCs are not implicitly typed, a PPC without an explicit
14967 interface must be a subroutine. */
14968 gfc_add_subroutine (&c->attr, c->name, &c->loc);
14969 }
14970
14971 /* Procedure pointer components: Check PASS arg. */
14972 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
14973 && !sym->attr.vtype)
14974 {
14975 gfc_symbol* me_arg;
14976
14977 if (c->tb->pass_arg)
14978 {
14979 gfc_formal_arglist* i;
14980
14981 /* If an explicit passing argument name is given, walk the arg-list
14982 and look for it. */
14983
14984 me_arg = NULL;
14985 c->tb->pass_arg_num = 1;
14986 for (i = c->ts.interface->formal; i; i = i->next)
14987 {
14988 if (!strcmp (i->sym->name, c->tb->pass_arg))
14989 {
14990 me_arg = i->sym;
14991 break;
14992 }
14993 c->tb->pass_arg_num++;
14994 }
14995
14996 if (!me_arg)
14997 {
14998 gfc_error ("Procedure pointer component %qs with PASS(%s) "
14999 "at %L has no argument %qs", c->name,
15000 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
15001 c->tb->error = 1;
15002 return false;
15003 }
15004 }
15005 else
15006 {
15007 /* Otherwise, take the first one; there should in fact be at least
15008 one. */
15009 c->tb->pass_arg_num = 1;
15010 if (!c->ts.interface->formal)
15011 {
15012 gfc_error ("Procedure pointer component %qs with PASS at %L "
15013 "must have at least one argument",
15014 c->name, &c->loc);
15015 c->tb->error = 1;
15016 return false;
15017 }
15018 me_arg = c->ts.interface->formal->sym;
15019 }
15020
15021 /* Now check that the argument-type matches. */
15022 gcc_assert (me_arg);
15023 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
15024 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
15025 || (me_arg->ts.type == BT_CLASS
15026 && CLASS_DATA (me_arg)->ts.u.derived != sym))
15027 {
15028 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
15029 " the derived type %qs", me_arg->name, c->name,
15030 me_arg->name, &c->loc, sym->name);
15031 c->tb->error = 1;
15032 return false;
15033 }
15034
15035 /* Check for F03:C453. */
15036 if (CLASS_DATA (me_arg)->attr.dimension)
15037 {
15038 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
15039 "must be scalar", me_arg->name, c->name, me_arg->name,
15040 &c->loc);
15041 c->tb->error = 1;
15042 return false;
15043 }
15044
15045 if (CLASS_DATA (me_arg)->attr.class_pointer)
15046 {
15047 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
15048 "may not have the POINTER attribute", me_arg->name,
15049 c->name, me_arg->name, &c->loc);
15050 c->tb->error = 1;
15051 return false;
15052 }
15053
15054 if (CLASS_DATA (me_arg)->attr.allocatable)
15055 {
15056 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
15057 "may not be ALLOCATABLE", me_arg->name, c->name,
15058 me_arg->name, &c->loc);
15059 c->tb->error = 1;
15060 return false;
15061 }
15062
15063 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
15064 {
15065 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
15066 " at %L", c->name, &c->loc);
15067 return false;
15068 }
15069
15070 }
15071
15072 /* Check type-spec if this is not the parent-type component. */
15073 if (((sym->attr.is_class
15074 && (!sym->components->ts.u.derived->attr.extension
15075 || c != sym->components->ts.u.derived->components))
15076 || (!sym->attr.is_class
15077 && (!sym->attr.extension || c != sym->components)))
15078 && !sym->attr.vtype
15079 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
15080 return false;
15081
15082 super_type = gfc_get_derived_super_type (sym);
15083
15084 /* If this type is an extension, set the accessibility of the parent
15085 component. */
15086 if (super_type
15087 && ((sym->attr.is_class
15088 && c == sym->components->ts.u.derived->components)
15089 || (!sym->attr.is_class && c == sym->components))
15090 && strcmp (super_type->name, c->name) == 0)
15091 c->attr.access = super_type->attr.access;
15092
15093 /* If this type is an extension, see if this component has the same name
15094 as an inherited type-bound procedure. */
15095 if (super_type && !sym->attr.is_class
15096 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
15097 {
15098 gfc_error ("Component %qs of %qs at %L has the same name as an"
15099 " inherited type-bound procedure",
15100 c->name, sym->name, &c->loc);
15101 return false;
15102 }
15103
15104 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
15105 && !c->ts.deferred)
15106 {
15107 if (c->ts.u.cl->length == NULL
15108 || (!resolve_charlen(c->ts.u.cl))
15109 || !gfc_is_constant_expr (c->ts.u.cl->length))
15110 {
15111 gfc_error ("Character length of component %qs needs to "
15112 "be a constant specification expression at %L",
15113 c->name,
15114 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
15115 return false;
15116 }
15117
15118 if (c->ts.u.cl->length && c->ts.u.cl->length->ts.type != BT_INTEGER)
15119 {
15120 if (!c->ts.u.cl->length->error)
15121 {
15122 gfc_error ("Character length expression of component %qs at %L "
15123 "must be of INTEGER type, found %s",
15124 c->name, &c->ts.u.cl->length->where,
15125 gfc_basic_typename (c->ts.u.cl->length->ts.type));
15126 c->ts.u.cl->length->error = 1;
15127 }
15128 return false;
15129 }
15130 }
15131
15132 if (c->ts.type == BT_CHARACTER && c->ts.deferred
15133 && !c->attr.pointer && !c->attr.allocatable)
15134 {
15135 gfc_error ("Character component %qs of %qs at %L with deferred "
15136 "length must be a POINTER or ALLOCATABLE",
15137 c->name, sym->name, &c->loc);
15138 return false;
15139 }
15140
15141 /* Add the hidden deferred length field. */
15142 if (c->ts.type == BT_CHARACTER
15143 && (c->ts.deferred || c->attr.pdt_string)
15144 && !c->attr.function
15145 && !sym->attr.is_class)
15146 {
15147 char name[GFC_MAX_SYMBOL_LEN+9];
15148 gfc_component *strlen;
15149 sprintf (name, "_%s_length", c->name);
15150 strlen = gfc_find_component (sym, name, true, true, NULL);
15151 if (strlen == NULL)
15152 {
15153 if (!gfc_add_component (sym, name, &strlen))
15154 return false;
15155 strlen->ts.type = BT_INTEGER;
15156 strlen->ts.kind = gfc_charlen_int_kind;
15157 strlen->attr.access = ACCESS_PRIVATE;
15158 strlen->attr.artificial = 1;
15159 }
15160 }
15161
15162 if (c->ts.type == BT_DERIVED
15163 && sym->component_access != ACCESS_PRIVATE
15164 && gfc_check_symbol_access (sym)
15165 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
15166 && !c->ts.u.derived->attr.use_assoc
15167 && !gfc_check_symbol_access (c->ts.u.derived)
15168 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
15169 "PRIVATE type and cannot be a component of "
15170 "%qs, which is PUBLIC at %L", c->name,
15171 sym->name, &sym->declared_at))
15172 return false;
15173
15174 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
15175 {
15176 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
15177 "type %s", c->name, &c->loc, sym->name);
15178 return false;
15179 }
15180
15181 if (sym->attr.sequence)
15182 {
15183 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
15184 {
15185 gfc_error ("Component %s of SEQUENCE type declared at %L does "
15186 "not have the SEQUENCE attribute",
15187 c->ts.u.derived->name, &sym->declared_at);
15188 return false;
15189 }
15190 }
15191
15192 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
15193 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
15194 else if (c->ts.type == BT_CLASS && c->attr.class_ok
15195 && CLASS_DATA (c)->ts.u.derived->attr.generic)
15196 CLASS_DATA (c)->ts.u.derived
15197 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
15198
15199 /* If an allocatable component derived type is of the same type as
15200 the enclosing derived type, we need a vtable generating so that
15201 the __deallocate procedure is created. */
15202 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
15203 && c->ts.u.derived == sym && c->attr.allocatable == 1)
15204 gfc_find_vtab (&c->ts);
15205
15206 /* Ensure that all the derived type components are put on the
15207 derived type list; even in formal namespaces, where derived type
15208 pointer components might not have been declared. */
15209 if (c->ts.type == BT_DERIVED
15210 && c->ts.u.derived
15211 && c->ts.u.derived->components
15212 && c->attr.pointer
15213 && sym != c->ts.u.derived)
15214 add_dt_to_dt_list (c->ts.u.derived);
15215
15216 if (c->as && c->as->type != AS_DEFERRED
15217 && (c->attr.pointer || c->attr.allocatable))
15218 return false;
15219
15220 if (!gfc_resolve_array_spec (c->as,
15221 !(c->attr.pointer || c->attr.proc_pointer
15222 || c->attr.allocatable)))
15223 return false;
15224
15225 if (c->initializer && !sym->attr.vtype
15226 && !c->attr.pdt_kind && !c->attr.pdt_len
15227 && !gfc_check_assign_symbol (sym, c, c->initializer))
15228 return false;
15229
15230 return true;
15231 }
15232
15233
15234 /* Be nice about the locus for a structure expression - show the locus of the
15235 first non-null sub-expression if we can. */
15236
15237 static locus *
15238 cons_where (gfc_expr *struct_expr)
15239 {
15240 gfc_constructor *cons;
15241
15242 gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
15243
15244 cons = gfc_constructor_first (struct_expr->value.constructor);
15245 for (; cons; cons = gfc_constructor_next (cons))
15246 {
15247 if (cons->expr && cons->expr->expr_type != EXPR_NULL)
15248 return &cons->expr->where;
15249 }
15250
15251 return &struct_expr->where;
15252 }
15253
15254 /* Resolve the components of a structure type. Much less work than derived
15255 types. */
15256
15257 static bool
15258 resolve_fl_struct (gfc_symbol *sym)
15259 {
15260 gfc_component *c;
15261 gfc_expr *init = NULL;
15262 bool success;
15263
15264 /* Make sure UNIONs do not have overlapping initializers. */
15265 if (sym->attr.flavor == FL_UNION)
15266 {
15267 for (c = sym->components; c; c = c->next)
15268 {
15269 if (init && c->initializer)
15270 {
15271 gfc_error ("Conflicting initializers in union at %L and %L",
15272 cons_where (init), cons_where (c->initializer));
15273 gfc_free_expr (c->initializer);
15274 c->initializer = NULL;
15275 }
15276 if (init == NULL)
15277 init = c->initializer;
15278 }
15279 }
15280
15281 success = true;
15282 for (c = sym->components; c; c = c->next)
15283 if (!resolve_component (c, sym))
15284 success = false;
15285
15286 if (!success)
15287 return false;
15288
15289 if (sym->components)
15290 add_dt_to_dt_list (sym);
15291
15292 return true;
15293 }
15294
15295
15296 /* Resolve the components of a derived type. This does not have to wait until
15297 resolution stage, but can be done as soon as the dt declaration has been
15298 parsed. */
15299
15300 static bool
15301 resolve_fl_derived0 (gfc_symbol *sym)
15302 {
15303 gfc_symbol* super_type;
15304 gfc_component *c;
15305 gfc_formal_arglist *f;
15306 bool success;
15307
15308 if (sym->attr.unlimited_polymorphic)
15309 return true;
15310
15311 super_type = gfc_get_derived_super_type (sym);
15312
15313 /* F2008, C432. */
15314 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
15315 {
15316 gfc_error ("As extending type %qs at %L has a coarray component, "
15317 "parent type %qs shall also have one", sym->name,
15318 &sym->declared_at, super_type->name);
15319 return false;
15320 }
15321
15322 /* Ensure the extended type gets resolved before we do. */
15323 if (super_type && !resolve_fl_derived0 (super_type))
15324 return false;
15325
15326 /* An ABSTRACT type must be extensible. */
15327 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
15328 {
15329 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
15330 sym->name, &sym->declared_at);
15331 return false;
15332 }
15333
15334 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
15335 : sym->components;
15336
15337 success = true;
15338 for ( ; c != NULL; c = c->next)
15339 if (!resolve_component (c, sym))
15340 success = false;
15341
15342 if (!success)
15343 return false;
15344
15345 /* Now add the caf token field, where needed. */
15346 if (flag_coarray != GFC_FCOARRAY_NONE
15347 && !sym->attr.is_class && !sym->attr.vtype)
15348 {
15349 for (c = sym->components; c; c = c->next)
15350 if (!c->attr.dimension && !c->attr.codimension
15351 && (c->attr.allocatable || c->attr.pointer))
15352 {
15353 char name[GFC_MAX_SYMBOL_LEN+9];
15354 gfc_component *token;
15355 sprintf (name, "_caf_%s", c->name);
15356 token = gfc_find_component (sym, name, true, true, NULL);
15357 if (token == NULL)
15358 {
15359 if (!gfc_add_component (sym, name, &token))
15360 return false;
15361 token->ts.type = BT_VOID;
15362 token->ts.kind = gfc_default_integer_kind;
15363 token->attr.access = ACCESS_PRIVATE;
15364 token->attr.artificial = 1;
15365 token->attr.caf_token = 1;
15366 }
15367 }
15368 }
15369
15370 check_defined_assignments (sym);
15371
15372 if (!sym->attr.defined_assign_comp && super_type)
15373 sym->attr.defined_assign_comp
15374 = super_type->attr.defined_assign_comp;
15375
15376 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
15377 all DEFERRED bindings are overridden. */
15378 if (super_type && super_type->attr.abstract && !sym->attr.abstract
15379 && !sym->attr.is_class
15380 && !ensure_not_abstract (sym, super_type))
15381 return false;
15382
15383 /* Check that there is a component for every PDT parameter. */
15384 if (sym->attr.pdt_template)
15385 {
15386 for (f = sym->formal; f; f = f->next)
15387 {
15388 if (!f->sym)
15389 continue;
15390 c = gfc_find_component (sym, f->sym->name, true, true, NULL);
15391 if (c == NULL)
15392 {
15393 gfc_error ("Parameterized type %qs does not have a component "
15394 "corresponding to parameter %qs at %L", sym->name,
15395 f->sym->name, &sym->declared_at);
15396 break;
15397 }
15398 }
15399 }
15400
15401 /* Add derived type to the derived type list. */
15402 add_dt_to_dt_list (sym);
15403
15404 return true;
15405 }
15406
15407
15408 /* The following procedure does the full resolution of a derived type,
15409 including resolution of all type-bound procedures (if present). In contrast
15410 to 'resolve_fl_derived0' this can only be done after the module has been
15411 parsed completely. */
15412
15413 static bool
15414 resolve_fl_derived (gfc_symbol *sym)
15415 {
15416 gfc_symbol *gen_dt = NULL;
15417
15418 if (sym->attr.unlimited_polymorphic)
15419 return true;
15420
15421 if (!sym->attr.is_class)
15422 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
15423 if (gen_dt && gen_dt->generic && gen_dt->generic->next
15424 && (!gen_dt->generic->sym->attr.use_assoc
15425 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
15426 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
15427 "%qs at %L being the same name as derived "
15428 "type at %L", sym->name,
15429 gen_dt->generic->sym == sym
15430 ? gen_dt->generic->next->sym->name
15431 : gen_dt->generic->sym->name,
15432 gen_dt->generic->sym == sym
15433 ? &gen_dt->generic->next->sym->declared_at
15434 : &gen_dt->generic->sym->declared_at,
15435 &sym->declared_at))
15436 return false;
15437
15438 if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
15439 {
15440 gfc_error ("Derived type %qs at %L has not been declared",
15441 sym->name, &sym->declared_at);
15442 return false;
15443 }
15444
15445 /* Resolve the finalizer procedures. */
15446 if (!gfc_resolve_finalizers (sym, NULL))
15447 return false;
15448
15449 if (sym->attr.is_class && sym->ts.u.derived == NULL)
15450 {
15451 /* Fix up incomplete CLASS symbols. */
15452 gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
15453 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
15454
15455 /* Nothing more to do for unlimited polymorphic entities. */
15456 if (data->ts.u.derived->attr.unlimited_polymorphic)
15457 {
15458 add_dt_to_dt_list (sym);
15459 return true;
15460 }
15461 else if (vptr->ts.u.derived == NULL)
15462 {
15463 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
15464 gcc_assert (vtab);
15465 vptr->ts.u.derived = vtab->ts.u.derived;
15466 if (!resolve_fl_derived0 (vptr->ts.u.derived))
15467 return false;
15468 }
15469 }
15470
15471 if (!resolve_fl_derived0 (sym))
15472 return false;
15473
15474 /* Resolve the type-bound procedures. */
15475 if (!resolve_typebound_procedures (sym))
15476 return false;
15477
15478 /* Generate module vtables subject to their accessibility and their not
15479 being vtables or pdt templates. If this is not done class declarations
15480 in external procedures wind up with their own version and so SELECT TYPE
15481 fails because the vptrs do not have the same address. */
15482 if (gfc_option.allow_std & GFC_STD_F2003
15483 && sym->ns->proc_name
15484 && sym->ns->proc_name->attr.flavor == FL_MODULE
15485 && sym->attr.access != ACCESS_PRIVATE
15486 && !(sym->attr.vtype || sym->attr.pdt_template))
15487 {
15488 gfc_symbol *vtab = gfc_find_derived_vtab (sym);
15489 gfc_set_sym_referenced (vtab);
15490 }
15491
15492 return true;
15493 }
15494
15495
15496 static bool
15497 resolve_fl_namelist (gfc_symbol *sym)
15498 {
15499 gfc_namelist *nl;
15500 gfc_symbol *nlsym;
15501
15502 for (nl = sym->namelist; nl; nl = nl->next)
15503 {
15504 /* Check again, the check in match only works if NAMELIST comes
15505 after the decl. */
15506 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
15507 {
15508 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
15509 "allowed", nl->sym->name, sym->name, &sym->declared_at);
15510 return false;
15511 }
15512
15513 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
15514 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
15515 "with assumed shape in namelist %qs at %L",
15516 nl->sym->name, sym->name, &sym->declared_at))
15517 return false;
15518
15519 if (is_non_constant_shape_array (nl->sym)
15520 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
15521 "with nonconstant shape in namelist %qs at %L",
15522 nl->sym->name, sym->name, &sym->declared_at))
15523 return false;
15524
15525 if (nl->sym->ts.type == BT_CHARACTER
15526 && (nl->sym->ts.u.cl->length == NULL
15527 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
15528 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
15529 "nonconstant character length in "
15530 "namelist %qs at %L", nl->sym->name,
15531 sym->name, &sym->declared_at))
15532 return false;
15533
15534 }
15535
15536 /* Reject PRIVATE objects in a PUBLIC namelist. */
15537 if (gfc_check_symbol_access (sym))
15538 {
15539 for (nl = sym->namelist; nl; nl = nl->next)
15540 {
15541 if (!nl->sym->attr.use_assoc
15542 && !is_sym_host_assoc (nl->sym, sym->ns)
15543 && !gfc_check_symbol_access (nl->sym))
15544 {
15545 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
15546 "cannot be member of PUBLIC namelist %qs at %L",
15547 nl->sym->name, sym->name, &sym->declared_at);
15548 return false;
15549 }
15550
15551 if (nl->sym->ts.type == BT_DERIVED
15552 && (nl->sym->ts.u.derived->attr.alloc_comp
15553 || nl->sym->ts.u.derived->attr.pointer_comp))
15554 {
15555 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
15556 "namelist %qs at %L with ALLOCATABLE "
15557 "or POINTER components", nl->sym->name,
15558 sym->name, &sym->declared_at))
15559 return false;
15560 return true;
15561 }
15562
15563 /* Types with private components that came here by USE-association. */
15564 if (nl->sym->ts.type == BT_DERIVED
15565 && derived_inaccessible (nl->sym->ts.u.derived))
15566 {
15567 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
15568 "components and cannot be member of namelist %qs at %L",
15569 nl->sym->name, sym->name, &sym->declared_at);
15570 return false;
15571 }
15572
15573 /* Types with private components that are defined in the same module. */
15574 if (nl->sym->ts.type == BT_DERIVED
15575 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
15576 && nl->sym->ts.u.derived->attr.private_comp)
15577 {
15578 gfc_error ("NAMELIST object %qs has PRIVATE components and "
15579 "cannot be a member of PUBLIC namelist %qs at %L",
15580 nl->sym->name, sym->name, &sym->declared_at);
15581 return false;
15582 }
15583 }
15584 }
15585
15586
15587 /* 14.1.2 A module or internal procedure represent local entities
15588 of the same type as a namelist member and so are not allowed. */
15589 for (nl = sym->namelist; nl; nl = nl->next)
15590 {
15591 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
15592 continue;
15593
15594 if (nl->sym->attr.function && nl->sym == nl->sym->result)
15595 if ((nl->sym == sym->ns->proc_name)
15596 ||
15597 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
15598 continue;
15599
15600 nlsym = NULL;
15601 if (nl->sym->name)
15602 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
15603 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
15604 {
15605 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
15606 "attribute in %qs at %L", nlsym->name,
15607 &sym->declared_at);
15608 return false;
15609 }
15610 }
15611
15612 return true;
15613 }
15614
15615
15616 static bool
15617 resolve_fl_parameter (gfc_symbol *sym)
15618 {
15619 /* A parameter array's shape needs to be constant. */
15620 if (sym->as != NULL
15621 && (sym->as->type == AS_DEFERRED
15622 || is_non_constant_shape_array (sym)))
15623 {
15624 gfc_error ("Parameter array %qs at %L cannot be automatic "
15625 "or of deferred shape", sym->name, &sym->declared_at);
15626 return false;
15627 }
15628
15629 /* Constraints on deferred type parameter. */
15630 if (!deferred_requirements (sym))
15631 return false;
15632
15633 /* Make sure a parameter that has been implicitly typed still
15634 matches the implicit type, since PARAMETER statements can precede
15635 IMPLICIT statements. */
15636 if (sym->attr.implicit_type
15637 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
15638 sym->ns)))
15639 {
15640 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
15641 "later IMPLICIT type", sym->name, &sym->declared_at);
15642 return false;
15643 }
15644
15645 /* Make sure the types of derived parameters are consistent. This
15646 type checking is deferred until resolution because the type may
15647 refer to a derived type from the host. */
15648 if (sym->ts.type == BT_DERIVED
15649 && !gfc_compare_types (&sym->ts, &sym->value->ts))
15650 {
15651 gfc_error ("Incompatible derived type in PARAMETER at %L",
15652 &sym->value->where);
15653 return false;
15654 }
15655
15656 /* F03:C509,C514. */
15657 if (sym->ts.type == BT_CLASS)
15658 {
15659 gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
15660 sym->name, &sym->declared_at);
15661 return false;
15662 }
15663
15664 return true;
15665 }
15666
15667
15668 /* Called by resolve_symbol to check PDTs. */
15669
15670 static void
15671 resolve_pdt (gfc_symbol* sym)
15672 {
15673 gfc_symbol *derived = NULL;
15674 gfc_actual_arglist *param;
15675 gfc_component *c;
15676 bool const_len_exprs = true;
15677 bool assumed_len_exprs = false;
15678 symbol_attribute *attr;
15679
15680 if (sym->ts.type == BT_DERIVED)
15681 {
15682 derived = sym->ts.u.derived;
15683 attr = &(sym->attr);
15684 }
15685 else if (sym->ts.type == BT_CLASS)
15686 {
15687 derived = CLASS_DATA (sym)->ts.u.derived;
15688 attr = &(CLASS_DATA (sym)->attr);
15689 }
15690 else
15691 gcc_unreachable ();
15692
15693 gcc_assert (derived->attr.pdt_type);
15694
15695 for (param = sym->param_list; param; param = param->next)
15696 {
15697 c = gfc_find_component (derived, param->name, false, true, NULL);
15698 gcc_assert (c);
15699 if (c->attr.pdt_kind)
15700 continue;
15701
15702 if (param->expr && !gfc_is_constant_expr (param->expr)
15703 && c->attr.pdt_len)
15704 const_len_exprs = false;
15705 else if (param->spec_type == SPEC_ASSUMED)
15706 assumed_len_exprs = true;
15707
15708 if (param->spec_type == SPEC_DEFERRED
15709 && !attr->allocatable && !attr->pointer)
15710 gfc_error ("The object %qs at %L has a deferred LEN "
15711 "parameter %qs and is neither allocatable "
15712 "nor a pointer", sym->name, &sym->declared_at,
15713 param->name);
15714
15715 }
15716
15717 if (!const_len_exprs
15718 && (sym->ns->proc_name->attr.is_main_program
15719 || sym->ns->proc_name->attr.flavor == FL_MODULE
15720 || sym->attr.save != SAVE_NONE))
15721 gfc_error ("The AUTOMATIC object %qs at %L must not have the "
15722 "SAVE attribute or be a variable declared in the "
15723 "main program, a module or a submodule(F08/C513)",
15724 sym->name, &sym->declared_at);
15725
15726 if (assumed_len_exprs && !(sym->attr.dummy
15727 || sym->attr.select_type_temporary || sym->attr.associate_var))
15728 gfc_error ("The object %qs at %L with ASSUMED type parameters "
15729 "must be a dummy or a SELECT TYPE selector(F08/4.2)",
15730 sym->name, &sym->declared_at);
15731 }
15732
15733
15734 /* Do anything necessary to resolve a symbol. Right now, we just
15735 assume that an otherwise unknown symbol is a variable. This sort
15736 of thing commonly happens for symbols in module. */
15737
15738 static void
15739 resolve_symbol (gfc_symbol *sym)
15740 {
15741 int check_constant, mp_flag;
15742 gfc_symtree *symtree;
15743 gfc_symtree *this_symtree;
15744 gfc_namespace *ns;
15745 gfc_component *c;
15746 symbol_attribute class_attr;
15747 gfc_array_spec *as;
15748 bool saved_specification_expr;
15749
15750 if (sym->resolve_symbol_called >= 1)
15751 return;
15752 sym->resolve_symbol_called = 1;
15753
15754 /* No symbol will ever have union type; only components can be unions.
15755 Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
15756 (just like derived type declaration symbols have flavor FL_DERIVED). */
15757 gcc_assert (sym->ts.type != BT_UNION);
15758
15759 /* Coarrayed polymorphic objects with allocatable or pointer components are
15760 yet unsupported for -fcoarray=lib. */
15761 if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
15762 && sym->ts.u.derived && CLASS_DATA (sym)
15763 && CLASS_DATA (sym)->attr.codimension
15764 && CLASS_DATA (sym)->ts.u.derived
15765 && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
15766 || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
15767 {
15768 gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
15769 "type coarrays at %L are unsupported", &sym->declared_at);
15770 return;
15771 }
15772
15773 if (sym->attr.artificial)
15774 return;
15775
15776 if (sym->attr.unlimited_polymorphic)
15777 return;
15778
15779 if (UNLIKELY (flag_openmp && strcmp (sym->name, "omp_all_memory") == 0))
15780 {
15781 gfc_error ("%<omp_all_memory%>, declared at %L, may only be used in "
15782 "the OpenMP DEPEND clause", &sym->declared_at);
15783 return;
15784 }
15785
15786 if (sym->attr.flavor == FL_UNKNOWN
15787 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
15788 && !sym->attr.generic && !sym->attr.external
15789 && sym->attr.if_source == IFSRC_UNKNOWN
15790 && sym->ts.type == BT_UNKNOWN))
15791 {
15792
15793 /* If we find that a flavorless symbol is an interface in one of the
15794 parent namespaces, find its symtree in this namespace, free the
15795 symbol and set the symtree to point to the interface symbol. */
15796 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
15797 {
15798 symtree = gfc_find_symtree (ns->sym_root, sym->name);
15799 if (symtree && (symtree->n.sym->generic ||
15800 (symtree->n.sym->attr.flavor == FL_PROCEDURE
15801 && sym->ns->construct_entities)))
15802 {
15803 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
15804 sym->name);
15805 if (this_symtree->n.sym == sym)
15806 {
15807 symtree->n.sym->refs++;
15808 gfc_release_symbol (sym);
15809 this_symtree->n.sym = symtree->n.sym;
15810 return;
15811 }
15812 }
15813 }
15814
15815 /* Otherwise give it a flavor according to such attributes as
15816 it has. */
15817 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
15818 && sym->attr.intrinsic == 0)
15819 sym->attr.flavor = FL_VARIABLE;
15820 else if (sym->attr.flavor == FL_UNKNOWN)
15821 {
15822 sym->attr.flavor = FL_PROCEDURE;
15823 if (sym->attr.dimension)
15824 sym->attr.function = 1;
15825 }
15826 }
15827
15828 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
15829 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
15830
15831 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
15832 && !resolve_procedure_interface (sym))
15833 return;
15834
15835 if (sym->attr.is_protected && !sym->attr.proc_pointer
15836 && (sym->attr.procedure || sym->attr.external))
15837 {
15838 if (sym->attr.external)
15839 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
15840 "at %L", &sym->declared_at);
15841 else
15842 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
15843 "at %L", &sym->declared_at);
15844
15845 return;
15846 }
15847
15848 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
15849 return;
15850
15851 else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
15852 && !resolve_fl_struct (sym))
15853 return;
15854
15855 /* Symbols that are module procedures with results (functions) have
15856 the types and array specification copied for type checking in
15857 procedures that call them, as well as for saving to a module
15858 file. These symbols can't stand the scrutiny that their results
15859 can. */
15860 mp_flag = (sym->result != NULL && sym->result != sym);
15861
15862 /* Make sure that the intrinsic is consistent with its internal
15863 representation. This needs to be done before assigning a default
15864 type to avoid spurious warnings. */
15865 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
15866 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
15867 return;
15868
15869 /* Resolve associate names. */
15870 if (sym->assoc)
15871 resolve_assoc_var (sym, true);
15872
15873 /* Assign default type to symbols that need one and don't have one. */
15874 if (sym->ts.type == BT_UNKNOWN)
15875 {
15876 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
15877 {
15878 gfc_set_default_type (sym, 1, NULL);
15879 }
15880
15881 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
15882 && !sym->attr.function && !sym->attr.subroutine
15883 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
15884 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
15885
15886 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
15887 {
15888 /* The specific case of an external procedure should emit an error
15889 in the case that there is no implicit type. */
15890 if (!mp_flag)
15891 {
15892 if (!sym->attr.mixed_entry_master)
15893 gfc_set_default_type (sym, sym->attr.external, NULL);
15894 }
15895 else
15896 {
15897 /* Result may be in another namespace. */
15898 resolve_symbol (sym->result);
15899
15900 if (!sym->result->attr.proc_pointer)
15901 {
15902 sym->ts = sym->result->ts;
15903 sym->as = gfc_copy_array_spec (sym->result->as);
15904 sym->attr.dimension = sym->result->attr.dimension;
15905 sym->attr.pointer = sym->result->attr.pointer;
15906 sym->attr.allocatable = sym->result->attr.allocatable;
15907 sym->attr.contiguous = sym->result->attr.contiguous;
15908 }
15909 }
15910 }
15911 }
15912 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
15913 {
15914 bool saved_specification_expr = specification_expr;
15915 bool saved_formal_arg_flag = formal_arg_flag;
15916
15917 specification_expr = true;
15918 formal_arg_flag = true;
15919 gfc_resolve_array_spec (sym->result->as, false);
15920 formal_arg_flag = saved_formal_arg_flag;
15921 specification_expr = saved_specification_expr;
15922 }
15923
15924 if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived)
15925 {
15926 as = CLASS_DATA (sym)->as;
15927 class_attr = CLASS_DATA (sym)->attr;
15928 class_attr.pointer = class_attr.class_pointer;
15929 }
15930 else
15931 {
15932 class_attr = sym->attr;
15933 as = sym->as;
15934 }
15935
15936 /* F2008, C530. */
15937 if (sym->attr.contiguous
15938 && (!class_attr.dimension
15939 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
15940 && !class_attr.pointer)))
15941 {
15942 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
15943 "array pointer or an assumed-shape or assumed-rank array",
15944 sym->name, &sym->declared_at);
15945 return;
15946 }
15947
15948 /* Assumed size arrays and assumed shape arrays must be dummy
15949 arguments. Array-spec's of implied-shape should have been resolved to
15950 AS_EXPLICIT already. */
15951
15952 if (as)
15953 {
15954 /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
15955 specification expression. */
15956 if (as->type == AS_IMPLIED_SHAPE)
15957 {
15958 int i;
15959 for (i=0; i<as->rank; i++)
15960 {
15961 if (as->lower[i] != NULL && as->upper[i] == NULL)
15962 {
15963 gfc_error ("Bad specification for assumed size array at %L",
15964 &as->lower[i]->where);
15965 return;
15966 }
15967 }
15968 gcc_unreachable();
15969 }
15970
15971 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
15972 || as->type == AS_ASSUMED_SHAPE)
15973 && !sym->attr.dummy && !sym->attr.select_type_temporary)
15974 {
15975 if (as->type == AS_ASSUMED_SIZE)
15976 gfc_error ("Assumed size array at %L must be a dummy argument",
15977 &sym->declared_at);
15978 else
15979 gfc_error ("Assumed shape array at %L must be a dummy argument",
15980 &sym->declared_at);
15981 return;
15982 }
15983 /* TS 29113, C535a. */
15984 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
15985 && !sym->attr.select_type_temporary
15986 && !(cs_base && cs_base->current
15987 && cs_base->current->op == EXEC_SELECT_RANK))
15988 {
15989 gfc_error ("Assumed-rank array at %L must be a dummy argument",
15990 &sym->declared_at);
15991 return;
15992 }
15993 if (as->type == AS_ASSUMED_RANK
15994 && (sym->attr.codimension || sym->attr.value))
15995 {
15996 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
15997 "CODIMENSION attribute", &sym->declared_at);
15998 return;
15999 }
16000 }
16001
16002 /* Make sure symbols with known intent or optional are really dummy
16003 variable. Because of ENTRY statement, this has to be deferred
16004 until resolution time. */
16005
16006 if (!sym->attr.dummy
16007 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
16008 {
16009 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
16010 return;
16011 }
16012
16013 if (sym->attr.value && !sym->attr.dummy)
16014 {
16015 gfc_error ("%qs at %L cannot have the VALUE attribute because "
16016 "it is not a dummy argument", sym->name, &sym->declared_at);
16017 return;
16018 }
16019
16020 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
16021 {
16022 gfc_charlen *cl = sym->ts.u.cl;
16023 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
16024 {
16025 gfc_error ("Character dummy variable %qs at %L with VALUE "
16026 "attribute must have constant length",
16027 sym->name, &sym->declared_at);
16028 return;
16029 }
16030
16031 if (sym->ts.is_c_interop
16032 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
16033 {
16034 gfc_error ("C interoperable character dummy variable %qs at %L "
16035 "with VALUE attribute must have length one",
16036 sym->name, &sym->declared_at);
16037 return;
16038 }
16039 }
16040
16041 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
16042 && sym->ts.u.derived->attr.generic)
16043 {
16044 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
16045 if (!sym->ts.u.derived)
16046 {
16047 gfc_error ("The derived type %qs at %L is of type %qs, "
16048 "which has not been defined", sym->name,
16049 &sym->declared_at, sym->ts.u.derived->name);
16050 sym->ts.type = BT_UNKNOWN;
16051 return;
16052 }
16053 }
16054
16055 /* Use the same constraints as TYPE(*), except for the type check
16056 and that only scalars and assumed-size arrays are permitted. */
16057 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
16058 {
16059 if (!sym->attr.dummy)
16060 {
16061 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
16062 "a dummy argument", sym->name, &sym->declared_at);
16063 return;
16064 }
16065
16066 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
16067 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
16068 && sym->ts.type != BT_COMPLEX)
16069 {
16070 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
16071 "of type TYPE(*) or of an numeric intrinsic type",
16072 sym->name, &sym->declared_at);
16073 return;
16074 }
16075
16076 if (sym->attr.allocatable || sym->attr.codimension
16077 || sym->attr.pointer || sym->attr.value)
16078 {
16079 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
16080 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
16081 "attribute", sym->name, &sym->declared_at);
16082 return;
16083 }
16084
16085 if (sym->attr.intent == INTENT_OUT)
16086 {
16087 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
16088 "have the INTENT(OUT) attribute",
16089 sym->name, &sym->declared_at);
16090 return;
16091 }
16092 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
16093 {
16094 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
16095 "either be a scalar or an assumed-size array",
16096 sym->name, &sym->declared_at);
16097 return;
16098 }
16099
16100 /* Set the type to TYPE(*) and add a dimension(*) to ensure
16101 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
16102 packing. */
16103 sym->ts.type = BT_ASSUMED;
16104 sym->as = gfc_get_array_spec ();
16105 sym->as->type = AS_ASSUMED_SIZE;
16106 sym->as->rank = 1;
16107 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
16108 }
16109 else if (sym->ts.type == BT_ASSUMED)
16110 {
16111 /* TS 29113, C407a. */
16112 if (!sym->attr.dummy)
16113 {
16114 gfc_error ("Assumed type of variable %s at %L is only permitted "
16115 "for dummy variables", sym->name, &sym->declared_at);
16116 return;
16117 }
16118 if (sym->attr.allocatable || sym->attr.codimension
16119 || sym->attr.pointer || sym->attr.value)
16120 {
16121 gfc_error ("Assumed-type variable %s at %L may not have the "
16122 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
16123 sym->name, &sym->declared_at);
16124 return;
16125 }
16126 if (sym->attr.intent == INTENT_OUT)
16127 {
16128 gfc_error ("Assumed-type variable %s at %L may not have the "
16129 "INTENT(OUT) attribute",
16130 sym->name, &sym->declared_at);
16131 return;
16132 }
16133 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
16134 {
16135 gfc_error ("Assumed-type variable %s at %L shall not be an "
16136 "explicit-shape array", sym->name, &sym->declared_at);
16137 return;
16138 }
16139 }
16140
16141 /* If the symbol is marked as bind(c), that it is declared at module level
16142 scope and verify its type and kind. Do not do the latter for symbols
16143 that are implicitly typed because that is handled in
16144 gfc_set_default_type. Handle dummy arguments and procedure definitions
16145 separately. Also, anything that is use associated is not handled here
16146 but instead is handled in the module it is declared in. Finally, derived
16147 type definitions are allowed to be BIND(C) since that only implies that
16148 they're interoperable, and they are checked fully for interoperability
16149 when a variable is declared of that type. */
16150 if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
16151 && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
16152 && sym->attr.flavor != FL_DERIVED)
16153 {
16154 bool t = true;
16155
16156 /* First, make sure the variable is declared at the
16157 module-level scope (J3/04-007, Section 15.3). */
16158 if (!(sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE)
16159 && !sym->attr.in_common)
16160 {
16161 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
16162 "is neither a COMMON block nor declared at the "
16163 "module level scope", sym->name, &(sym->declared_at));
16164 t = false;
16165 }
16166 else if (sym->ts.type == BT_CHARACTER
16167 && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
16168 || !gfc_is_constant_expr (sym->ts.u.cl->length)
16169 || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
16170 {
16171 gfc_error ("BIND(C) Variable %qs at %L must have length one",
16172 sym->name, &sym->declared_at);
16173 t = false;
16174 }
16175 else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
16176 {
16177 t = verify_com_block_vars_c_interop (sym->common_head);
16178 }
16179 else if (sym->attr.implicit_type == 0)
16180 {
16181 /* If type() declaration, we need to verify that the components
16182 of the given type are all C interoperable, etc. */
16183 if (sym->ts.type == BT_DERIVED &&
16184 sym->ts.u.derived->attr.is_c_interop != 1)
16185 {
16186 /* Make sure the user marked the derived type as BIND(C). If
16187 not, call the verify routine. This could print an error
16188 for the derived type more than once if multiple variables
16189 of that type are declared. */
16190 if (sym->ts.u.derived->attr.is_bind_c != 1)
16191 verify_bind_c_derived_type (sym->ts.u.derived);
16192 t = false;
16193 }
16194
16195 /* Verify the variable itself as C interoperable if it
16196 is BIND(C). It is not possible for this to succeed if
16197 the verify_bind_c_derived_type failed, so don't have to handle
16198 any error returned by verify_bind_c_derived_type. */
16199 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
16200 sym->common_block);
16201 }
16202
16203 if (!t)
16204 {
16205 /* clear the is_bind_c flag to prevent reporting errors more than
16206 once if something failed. */
16207 sym->attr.is_bind_c = 0;
16208 return;
16209 }
16210 }
16211
16212 /* If a derived type symbol has reached this point, without its
16213 type being declared, we have an error. Notice that most
16214 conditions that produce undefined derived types have already
16215 been dealt with. However, the likes of:
16216 implicit type(t) (t) ..... call foo (t) will get us here if
16217 the type is not declared in the scope of the implicit
16218 statement. Change the type to BT_UNKNOWN, both because it is so
16219 and to prevent an ICE. */
16220 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
16221 && sym->ts.u.derived->components == NULL
16222 && !sym->ts.u.derived->attr.zero_comp)
16223 {
16224 gfc_error ("The derived type %qs at %L is of type %qs, "
16225 "which has not been defined", sym->name,
16226 &sym->declared_at, sym->ts.u.derived->name);
16227 sym->ts.type = BT_UNKNOWN;
16228 return;
16229 }
16230
16231 /* Make sure that the derived type has been resolved and that the
16232 derived type is visible in the symbol's namespace, if it is a
16233 module function and is not PRIVATE. */
16234 if (sym->ts.type == BT_DERIVED
16235 && sym->ts.u.derived->attr.use_assoc
16236 && sym->ns->proc_name
16237 && sym->ns->proc_name->attr.flavor == FL_MODULE
16238 && !resolve_fl_derived (sym->ts.u.derived))
16239 return;
16240
16241 /* Unless the derived-type declaration is use associated, Fortran 95
16242 does not allow public entries of private derived types.
16243 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
16244 161 in 95-006r3. */
16245 if (sym->ts.type == BT_DERIVED
16246 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
16247 && !sym->ts.u.derived->attr.use_assoc
16248 && gfc_check_symbol_access (sym)
16249 && !gfc_check_symbol_access (sym->ts.u.derived)
16250 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
16251 "derived type %qs",
16252 (sym->attr.flavor == FL_PARAMETER)
16253 ? "parameter" : "variable",
16254 sym->name, &sym->declared_at,
16255 sym->ts.u.derived->name))
16256 return;
16257
16258 /* F2008, C1302. */
16259 if (sym->ts.type == BT_DERIVED
16260 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
16261 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
16262 || sym->ts.u.derived->attr.lock_comp)
16263 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
16264 {
16265 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
16266 "type LOCK_TYPE must be a coarray", sym->name,
16267 &sym->declared_at);
16268 return;
16269 }
16270
16271 /* TS18508, C702/C703. */
16272 if (sym->ts.type == BT_DERIVED
16273 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
16274 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
16275 || sym->ts.u.derived->attr.event_comp)
16276 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
16277 {
16278 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
16279 "type EVENT_TYPE must be a coarray", sym->name,
16280 &sym->declared_at);
16281 return;
16282 }
16283
16284 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
16285 default initialization is defined (5.1.2.4.4). */
16286 if (sym->ts.type == BT_DERIVED
16287 && sym->attr.dummy
16288 && sym->attr.intent == INTENT_OUT
16289 && sym->as
16290 && sym->as->type == AS_ASSUMED_SIZE)
16291 {
16292 for (c = sym->ts.u.derived->components; c; c = c->next)
16293 {
16294 if (c->initializer)
16295 {
16296 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
16297 "ASSUMED SIZE and so cannot have a default initializer",
16298 sym->name, &sym->declared_at);
16299 return;
16300 }
16301 }
16302 }
16303
16304 /* F2008, C542. */
16305 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
16306 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
16307 {
16308 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
16309 "INTENT(OUT)", sym->name, &sym->declared_at);
16310 return;
16311 }
16312
16313 /* TS18508. */
16314 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
16315 && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
16316 {
16317 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
16318 "INTENT(OUT)", sym->name, &sym->declared_at);
16319 return;
16320 }
16321
16322 /* F2008, C525. */
16323 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
16324 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
16325 && sym->ts.u.derived && CLASS_DATA (sym)
16326 && CLASS_DATA (sym)->attr.coarray_comp))
16327 || class_attr.codimension)
16328 && (sym->attr.result || sym->result == sym))
16329 {
16330 gfc_error ("Function result %qs at %L shall not be a coarray or have "
16331 "a coarray component", sym->name, &sym->declared_at);
16332 return;
16333 }
16334
16335 /* F2008, C524. */
16336 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
16337 && sym->ts.u.derived->ts.is_iso_c)
16338 {
16339 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
16340 "shall not be a coarray", sym->name, &sym->declared_at);
16341 return;
16342 }
16343
16344 /* F2008, C525. */
16345 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
16346 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
16347 && sym->ts.u.derived && CLASS_DATA (sym)
16348 && CLASS_DATA (sym)->attr.coarray_comp))
16349 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
16350 || class_attr.allocatable))
16351 {
16352 gfc_error ("Variable %qs at %L with coarray component shall be a "
16353 "nonpointer, nonallocatable scalar, which is not a coarray",
16354 sym->name, &sym->declared_at);
16355 return;
16356 }
16357
16358 /* F2008, C526. The function-result case was handled above. */
16359 if (class_attr.codimension
16360 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
16361 || sym->attr.select_type_temporary
16362 || sym->attr.associate_var
16363 || (sym->ns->save_all && !sym->attr.automatic)
16364 || sym->ns->proc_name->attr.flavor == FL_MODULE
16365 || sym->ns->proc_name->attr.is_main_program
16366 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
16367 {
16368 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
16369 "nor a dummy argument", sym->name, &sym->declared_at);
16370 return;
16371 }
16372 /* F2008, C528. */
16373 else if (class_attr.codimension && !sym->attr.select_type_temporary
16374 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
16375 {
16376 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
16377 "deferred shape", sym->name, &sym->declared_at);
16378 return;
16379 }
16380 else if (class_attr.codimension && class_attr.allocatable && as
16381 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
16382 {
16383 gfc_error ("Allocatable coarray variable %qs at %L must have "
16384 "deferred shape", sym->name, &sym->declared_at);
16385 return;
16386 }
16387
16388 /* F2008, C541. */
16389 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
16390 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
16391 && sym->ts.u.derived && CLASS_DATA (sym)
16392 && CLASS_DATA (sym)->attr.coarray_comp))
16393 || (class_attr.codimension && class_attr.allocatable))
16394 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
16395 {
16396 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
16397 "allocatable coarray or have coarray components",
16398 sym->name, &sym->declared_at);
16399 return;
16400 }
16401
16402 if (class_attr.codimension && sym->attr.dummy
16403 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
16404 {
16405 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
16406 "procedure %qs", sym->name, &sym->declared_at,
16407 sym->ns->proc_name->name);
16408 return;
16409 }
16410
16411 if (sym->ts.type == BT_LOGICAL
16412 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
16413 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
16414 && sym->ns->proc_name->attr.is_bind_c)))
16415 {
16416 int i;
16417 for (i = 0; gfc_logical_kinds[i].kind; i++)
16418 if (gfc_logical_kinds[i].kind == sym->ts.kind)
16419 break;
16420 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
16421 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
16422 "%L with non-C_Bool kind in BIND(C) procedure "
16423 "%qs", sym->name, &sym->declared_at,
16424 sym->ns->proc_name->name))
16425 return;
16426 else if (!gfc_logical_kinds[i].c_bool
16427 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
16428 "%qs at %L with non-C_Bool kind in "
16429 "BIND(C) procedure %qs", sym->name,
16430 &sym->declared_at,
16431 sym->attr.function ? sym->name
16432 : sym->ns->proc_name->name))
16433 return;
16434 }
16435
16436 switch (sym->attr.flavor)
16437 {
16438 case FL_VARIABLE:
16439 if (!resolve_fl_variable (sym, mp_flag))
16440 return;
16441 break;
16442
16443 case FL_PROCEDURE:
16444 if (sym->formal && !sym->formal_ns)
16445 {
16446 /* Check that none of the arguments are a namelist. */
16447 gfc_formal_arglist *formal = sym->formal;
16448
16449 for (; formal; formal = formal->next)
16450 if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
16451 {
16452 gfc_error ("Namelist %qs cannot be an argument to "
16453 "subroutine or function at %L",
16454 formal->sym->name, &sym->declared_at);
16455 return;
16456 }
16457 }
16458
16459 if (!resolve_fl_procedure (sym, mp_flag))
16460 return;
16461 break;
16462
16463 case FL_NAMELIST:
16464 if (!resolve_fl_namelist (sym))
16465 return;
16466 break;
16467
16468 case FL_PARAMETER:
16469 if (!resolve_fl_parameter (sym))
16470 return;
16471 break;
16472
16473 default:
16474 break;
16475 }
16476
16477 /* Resolve array specifier. Check as well some constraints
16478 on COMMON blocks. */
16479
16480 check_constant = sym->attr.in_common && !sym->attr.pointer;
16481
16482 /* Set the formal_arg_flag so that check_conflict will not throw
16483 an error for host associated variables in the specification
16484 expression for an array_valued function. */
16485 if ((sym->attr.function || sym->attr.result) && sym->as)
16486 formal_arg_flag = true;
16487
16488 saved_specification_expr = specification_expr;
16489 specification_expr = true;
16490 gfc_resolve_array_spec (sym->as, check_constant);
16491 specification_expr = saved_specification_expr;
16492
16493 formal_arg_flag = false;
16494
16495 /* Resolve formal namespaces. */
16496 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
16497 && !sym->attr.contained && !sym->attr.intrinsic)
16498 gfc_resolve (sym->formal_ns);
16499
16500 /* Make sure the formal namespace is present. */
16501 if (sym->formal && !sym->formal_ns)
16502 {
16503 gfc_formal_arglist *formal = sym->formal;
16504 while (formal && !formal->sym)
16505 formal = formal->next;
16506
16507 if (formal)
16508 {
16509 sym->formal_ns = formal->sym->ns;
16510 if (sym->formal_ns && sym->ns != formal->sym->ns)
16511 sym->formal_ns->refs++;
16512 }
16513 }
16514
16515 /* Check threadprivate restrictions. */
16516 if (sym->attr.threadprivate
16517 && !(sym->attr.save || sym->attr.data || sym->attr.in_common)
16518 && !(sym->ns->save_all && !sym->attr.automatic)
16519 && sym->module == NULL
16520 && (sym->ns->proc_name == NULL
16521 || (sym->ns->proc_name->attr.flavor != FL_MODULE
16522 && !sym->ns->proc_name->attr.is_main_program)))
16523 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
16524
16525 /* Check omp declare target restrictions. */
16526 if (sym->attr.omp_declare_target
16527 && sym->attr.flavor == FL_VARIABLE
16528 && !sym->attr.save
16529 && !(sym->ns->save_all && !sym->attr.automatic)
16530 && (!sym->attr.in_common
16531 && sym->module == NULL
16532 && (sym->ns->proc_name == NULL
16533 || (sym->ns->proc_name->attr.flavor != FL_MODULE
16534 && !sym->ns->proc_name->attr.is_main_program))))
16535 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
16536 sym->name, &sym->declared_at);
16537
16538 /* If we have come this far we can apply default-initializers, as
16539 described in 14.7.5, to those variables that have not already
16540 been assigned one. */
16541 if (sym->ts.type == BT_DERIVED
16542 && !sym->value
16543 && !sym->attr.allocatable
16544 && !sym->attr.alloc_comp)
16545 {
16546 symbol_attribute *a = &sym->attr;
16547
16548 if ((!a->save && !a->dummy && !a->pointer
16549 && !a->in_common && !a->use_assoc
16550 && a->referenced
16551 && !((a->function || a->result)
16552 && (!a->dimension
16553 || sym->ts.u.derived->attr.alloc_comp
16554 || sym->ts.u.derived->attr.pointer_comp))
16555 && !(a->function && sym != sym->result))
16556 || (a->dummy && !a->pointer && a->intent == INTENT_OUT
16557 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY))
16558 apply_default_init (sym);
16559 else if (a->function && sym->result && a->access != ACCESS_PRIVATE
16560 && (sym->ts.u.derived->attr.alloc_comp
16561 || sym->ts.u.derived->attr.pointer_comp))
16562 /* Mark the result symbol to be referenced, when it has allocatable
16563 components. */
16564 sym->result->attr.referenced = 1;
16565 }
16566
16567 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
16568 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
16569 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY
16570 && !CLASS_DATA (sym)->attr.class_pointer
16571 && !CLASS_DATA (sym)->attr.allocatable)
16572 apply_default_init (sym);
16573
16574 /* If this symbol has a type-spec, check it. */
16575 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
16576 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
16577 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
16578 return;
16579
16580 if (sym->param_list)
16581 resolve_pdt (sym);
16582
16583 if (!sym->attr.referenced
16584 && (sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED))
16585 {
16586 gfc_expr *final_expr = gfc_lval_expr_from_sym (sym);
16587 if (gfc_is_finalizable (final_expr->ts.u.derived, NULL))
16588 gfc_set_sym_referenced (sym);
16589 gfc_free_expr (final_expr);
16590 }
16591 }
16592
16593
16594 /************* Resolve DATA statements *************/
16595
16596 static struct
16597 {
16598 gfc_data_value *vnode;
16599 mpz_t left;
16600 }
16601 values;
16602
16603
16604 /* Advance the values structure to point to the next value in the data list. */
16605
16606 static bool
16607 next_data_value (void)
16608 {
16609 while (mpz_cmp_ui (values.left, 0) == 0)
16610 {
16611
16612 if (values.vnode->next == NULL)
16613 return false;
16614
16615 values.vnode = values.vnode->next;
16616 mpz_set (values.left, values.vnode->repeat);
16617 }
16618
16619 return true;
16620 }
16621
16622
16623 static bool
16624 check_data_variable (gfc_data_variable *var, locus *where)
16625 {
16626 gfc_expr *e;
16627 mpz_t size;
16628 mpz_t offset;
16629 bool t;
16630 ar_type mark = AR_UNKNOWN;
16631 int i;
16632 mpz_t section_index[GFC_MAX_DIMENSIONS];
16633 gfc_ref *ref;
16634 gfc_array_ref *ar;
16635 gfc_symbol *sym;
16636 int has_pointer;
16637
16638 if (!gfc_resolve_expr (var->expr))
16639 return false;
16640
16641 ar = NULL;
16642 mpz_init_set_si (offset, 0);
16643 e = var->expr;
16644
16645 if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
16646 && e->value.function.isym->id == GFC_ISYM_CAF_GET)
16647 e = e->value.function.actual->expr;
16648
16649 if (e->expr_type != EXPR_VARIABLE)
16650 {
16651 gfc_error ("Expecting definable entity near %L", where);
16652 return false;
16653 }
16654
16655 sym = e->symtree->n.sym;
16656
16657 if (sym->ns->is_block_data && !sym->attr.in_common)
16658 {
16659 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
16660 sym->name, &sym->declared_at);
16661 return false;
16662 }
16663
16664 if (e->ref == NULL && sym->as)
16665 {
16666 gfc_error ("DATA array %qs at %L must be specified in a previous"
16667 " declaration", sym->name, where);
16668 return false;
16669 }
16670
16671 if (gfc_is_coindexed (e))
16672 {
16673 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
16674 where);
16675 return false;
16676 }
16677
16678 has_pointer = sym->attr.pointer;
16679
16680 for (ref = e->ref; ref; ref = ref->next)
16681 {
16682 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
16683 has_pointer = 1;
16684
16685 if (has_pointer)
16686 {
16687 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
16688 {
16689 gfc_error ("DATA element %qs at %L is a pointer and so must "
16690 "be a full array", sym->name, where);
16691 return false;
16692 }
16693
16694 if (values.vnode->expr->expr_type == EXPR_CONSTANT)
16695 {
16696 gfc_error ("DATA object near %L has the pointer attribute "
16697 "and the corresponding DATA value is not a valid "
16698 "initial-data-target", where);
16699 return false;
16700 }
16701 }
16702
16703 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.allocatable)
16704 {
16705 gfc_error ("DATA element %qs at %L cannot have the ALLOCATABLE "
16706 "attribute", ref->u.c.component->name, &e->where);
16707 return false;
16708 }
16709 }
16710
16711 if (e->rank == 0 || has_pointer)
16712 {
16713 mpz_init_set_ui (size, 1);
16714 ref = NULL;
16715 }
16716 else
16717 {
16718 ref = e->ref;
16719
16720 /* Find the array section reference. */
16721 for (ref = e->ref; ref; ref = ref->next)
16722 {
16723 if (ref->type != REF_ARRAY)
16724 continue;
16725 if (ref->u.ar.type == AR_ELEMENT)
16726 continue;
16727 break;
16728 }
16729 gcc_assert (ref);
16730
16731 /* Set marks according to the reference pattern. */
16732 switch (ref->u.ar.type)
16733 {
16734 case AR_FULL:
16735 mark = AR_FULL;
16736 break;
16737
16738 case AR_SECTION:
16739 ar = &ref->u.ar;
16740 /* Get the start position of array section. */
16741 gfc_get_section_index (ar, section_index, &offset);
16742 mark = AR_SECTION;
16743 break;
16744
16745 default:
16746 gcc_unreachable ();
16747 }
16748
16749 if (!gfc_array_size (e, &size))
16750 {
16751 gfc_error ("Nonconstant array section at %L in DATA statement",
16752 where);
16753 mpz_clear (offset);
16754 return false;
16755 }
16756 }
16757
16758 t = true;
16759
16760 while (mpz_cmp_ui (size, 0) > 0)
16761 {
16762 if (!next_data_value ())
16763 {
16764 gfc_error ("DATA statement at %L has more variables than values",
16765 where);
16766 t = false;
16767 break;
16768 }
16769
16770 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
16771 if (!t)
16772 break;
16773
16774 /* If we have more than one element left in the repeat count,
16775 and we have more than one element left in the target variable,
16776 then create a range assignment. */
16777 /* FIXME: Only done for full arrays for now, since array sections
16778 seem tricky. */
16779 if (mark == AR_FULL && ref && ref->next == NULL
16780 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
16781 {
16782 mpz_t range;
16783
16784 if (mpz_cmp (size, values.left) >= 0)
16785 {
16786 mpz_init_set (range, values.left);
16787 mpz_sub (size, size, values.left);
16788 mpz_set_ui (values.left, 0);
16789 }
16790 else
16791 {
16792 mpz_init_set (range, size);
16793 mpz_sub (values.left, values.left, size);
16794 mpz_set_ui (size, 0);
16795 }
16796
16797 t = gfc_assign_data_value (var->expr, values.vnode->expr,
16798 offset, &range);
16799
16800 mpz_add (offset, offset, range);
16801 mpz_clear (range);
16802
16803 if (!t)
16804 break;
16805 }
16806
16807 /* Assign initial value to symbol. */
16808 else
16809 {
16810 mpz_sub_ui (values.left, values.left, 1);
16811 mpz_sub_ui (size, size, 1);
16812
16813 t = gfc_assign_data_value (var->expr, values.vnode->expr,
16814 offset, NULL);
16815 if (!t)
16816 break;
16817
16818 if (mark == AR_FULL)
16819 mpz_add_ui (offset, offset, 1);
16820
16821 /* Modify the array section indexes and recalculate the offset
16822 for next element. */
16823 else if (mark == AR_SECTION)
16824 gfc_advance_section (section_index, ar, &offset);
16825 }
16826 }
16827
16828 if (mark == AR_SECTION)
16829 {
16830 for (i = 0; i < ar->dimen; i++)
16831 mpz_clear (section_index[i]);
16832 }
16833
16834 mpz_clear (size);
16835 mpz_clear (offset);
16836
16837 return t;
16838 }
16839
16840
16841 static bool traverse_data_var (gfc_data_variable *, locus *);
16842
16843 /* Iterate over a list of elements in a DATA statement. */
16844
16845 static bool
16846 traverse_data_list (gfc_data_variable *var, locus *where)
16847 {
16848 mpz_t trip;
16849 iterator_stack frame;
16850 gfc_expr *e, *start, *end, *step;
16851 bool retval = true;
16852
16853 mpz_init (frame.value);
16854 mpz_init (trip);
16855
16856 start = gfc_copy_expr (var->iter.start);
16857 end = gfc_copy_expr (var->iter.end);
16858 step = gfc_copy_expr (var->iter.step);
16859
16860 if (!gfc_simplify_expr (start, 1)
16861 || start->expr_type != EXPR_CONSTANT)
16862 {
16863 gfc_error ("start of implied-do loop at %L could not be "
16864 "simplified to a constant value", &start->where);
16865 retval = false;
16866 goto cleanup;
16867 }
16868 if (!gfc_simplify_expr (end, 1)
16869 || end->expr_type != EXPR_CONSTANT)
16870 {
16871 gfc_error ("end of implied-do loop at %L could not be "
16872 "simplified to a constant value", &end->where);
16873 retval = false;
16874 goto cleanup;
16875 }
16876 if (!gfc_simplify_expr (step, 1)
16877 || step->expr_type != EXPR_CONSTANT)
16878 {
16879 gfc_error ("step of implied-do loop at %L could not be "
16880 "simplified to a constant value", &step->where);
16881 retval = false;
16882 goto cleanup;
16883 }
16884 if (mpz_cmp_si (step->value.integer, 0) == 0)
16885 {
16886 gfc_error ("step of implied-do loop at %L shall not be zero",
16887 &step->where);
16888 retval = false;
16889 goto cleanup;
16890 }
16891
16892 mpz_set (trip, end->value.integer);
16893 mpz_sub (trip, trip, start->value.integer);
16894 mpz_add (trip, trip, step->value.integer);
16895
16896 mpz_div (trip, trip, step->value.integer);
16897
16898 mpz_set (frame.value, start->value.integer);
16899
16900 frame.prev = iter_stack;
16901 frame.variable = var->iter.var->symtree;
16902 iter_stack = &frame;
16903
16904 while (mpz_cmp_ui (trip, 0) > 0)
16905 {
16906 if (!traverse_data_var (var->list, where))
16907 {
16908 retval = false;
16909 goto cleanup;
16910 }
16911
16912 e = gfc_copy_expr (var->expr);
16913 if (!gfc_simplify_expr (e, 1))
16914 {
16915 gfc_free_expr (e);
16916 retval = false;
16917 goto cleanup;
16918 }
16919
16920 mpz_add (frame.value, frame.value, step->value.integer);
16921
16922 mpz_sub_ui (trip, trip, 1);
16923 }
16924
16925 cleanup:
16926 mpz_clear (frame.value);
16927 mpz_clear (trip);
16928
16929 gfc_free_expr (start);
16930 gfc_free_expr (end);
16931 gfc_free_expr (step);
16932
16933 iter_stack = frame.prev;
16934 return retval;
16935 }
16936
16937
16938 /* Type resolve variables in the variable list of a DATA statement. */
16939
16940 static bool
16941 traverse_data_var (gfc_data_variable *var, locus *where)
16942 {
16943 bool t;
16944
16945 for (; var; var = var->next)
16946 {
16947 if (var->expr == NULL)
16948 t = traverse_data_list (var, where);
16949 else
16950 t = check_data_variable (var, where);
16951
16952 if (!t)
16953 return false;
16954 }
16955
16956 return true;
16957 }
16958
16959
16960 /* Resolve the expressions and iterators associated with a data statement.
16961 This is separate from the assignment checking because data lists should
16962 only be resolved once. */
16963
16964 static bool
16965 resolve_data_variables (gfc_data_variable *d)
16966 {
16967 for (; d; d = d->next)
16968 {
16969 if (d->list == NULL)
16970 {
16971 if (!gfc_resolve_expr (d->expr))
16972 return false;
16973 }
16974 else
16975 {
16976 if (!gfc_resolve_iterator (&d->iter, false, true))
16977 return false;
16978
16979 if (!resolve_data_variables (d->list))
16980 return false;
16981 }
16982 }
16983
16984 return true;
16985 }
16986
16987
16988 /* Resolve a single DATA statement. We implement this by storing a pointer to
16989 the value list into static variables, and then recursively traversing the
16990 variables list, expanding iterators and such. */
16991
16992 static void
16993 resolve_data (gfc_data *d)
16994 {
16995
16996 if (!resolve_data_variables (d->var))
16997 return;
16998
16999 values.vnode = d->value;
17000 if (d->value == NULL)
17001 mpz_set_ui (values.left, 0);
17002 else
17003 mpz_set (values.left, d->value->repeat);
17004
17005 if (!traverse_data_var (d->var, &d->where))
17006 return;
17007
17008 /* At this point, we better not have any values left. */
17009
17010 if (next_data_value ())
17011 gfc_error ("DATA statement at %L has more values than variables",
17012 &d->where);
17013 }
17014
17015
17016 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
17017 accessed by host or use association, is a dummy argument to a pure function,
17018 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
17019 is storage associated with any such variable, shall not be used in the
17020 following contexts: (clients of this function). */
17021
17022 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
17023 procedure. Returns zero if assignment is OK, nonzero if there is a
17024 problem. */
17025 int
17026 gfc_impure_variable (gfc_symbol *sym)
17027 {
17028 gfc_symbol *proc;
17029 gfc_namespace *ns;
17030
17031 if (sym->attr.use_assoc || sym->attr.in_common)
17032 return 1;
17033
17034 /* Check if the symbol's ns is inside the pure procedure. */
17035 for (ns = gfc_current_ns; ns; ns = ns->parent)
17036 {
17037 if (ns == sym->ns)
17038 break;
17039 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
17040 return 1;
17041 }
17042
17043 proc = sym->ns->proc_name;
17044 if (sym->attr.dummy
17045 && !sym->attr.value
17046 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
17047 || proc->attr.function))
17048 return 1;
17049
17050 /* TODO: Sort out what can be storage associated, if anything, and include
17051 it here. In principle equivalences should be scanned but it does not
17052 seem to be possible to storage associate an impure variable this way. */
17053 return 0;
17054 }
17055
17056
17057 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
17058 current namespace is inside a pure procedure. */
17059
17060 int
17061 gfc_pure (gfc_symbol *sym)
17062 {
17063 symbol_attribute attr;
17064 gfc_namespace *ns;
17065
17066 if (sym == NULL)
17067 {
17068 /* Check if the current namespace or one of its parents
17069 belongs to a pure procedure. */
17070 for (ns = gfc_current_ns; ns; ns = ns->parent)
17071 {
17072 sym = ns->proc_name;
17073 if (sym == NULL)
17074 return 0;
17075 attr = sym->attr;
17076 if (attr.flavor == FL_PROCEDURE && attr.pure)
17077 return 1;
17078 }
17079 return 0;
17080 }
17081
17082 attr = sym->attr;
17083
17084 return attr.flavor == FL_PROCEDURE && attr.pure;
17085 }
17086
17087
17088 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
17089 checks if the current namespace is implicitly pure. Note that this
17090 function returns false for a PURE procedure. */
17091
17092 int
17093 gfc_implicit_pure (gfc_symbol *sym)
17094 {
17095 gfc_namespace *ns;
17096
17097 if (sym == NULL)
17098 {
17099 /* Check if the current procedure is implicit_pure. Walk up
17100 the procedure list until we find a procedure. */
17101 for (ns = gfc_current_ns; ns; ns = ns->parent)
17102 {
17103 sym = ns->proc_name;
17104 if (sym == NULL)
17105 return 0;
17106
17107 if (sym->attr.flavor == FL_PROCEDURE)
17108 break;
17109 }
17110 }
17111
17112 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
17113 && !sym->attr.pure;
17114 }
17115
17116
17117 void
17118 gfc_unset_implicit_pure (gfc_symbol *sym)
17119 {
17120 gfc_namespace *ns;
17121
17122 if (sym == NULL)
17123 {
17124 /* Check if the current procedure is implicit_pure. Walk up
17125 the procedure list until we find a procedure. */
17126 for (ns = gfc_current_ns; ns; ns = ns->parent)
17127 {
17128 sym = ns->proc_name;
17129 if (sym == NULL)
17130 return;
17131
17132 if (sym->attr.flavor == FL_PROCEDURE)
17133 break;
17134 }
17135 }
17136
17137 if (sym->attr.flavor == FL_PROCEDURE)
17138 sym->attr.implicit_pure = 0;
17139 else
17140 sym->attr.pure = 0;
17141 }
17142
17143
17144 /* Test whether the current procedure is elemental or not. */
17145
17146 int
17147 gfc_elemental (gfc_symbol *sym)
17148 {
17149 symbol_attribute attr;
17150
17151 if (sym == NULL)
17152 sym = gfc_current_ns->proc_name;
17153 if (sym == NULL)
17154 return 0;
17155 attr = sym->attr;
17156
17157 return attr.flavor == FL_PROCEDURE && attr.elemental;
17158 }
17159
17160
17161 /* Warn about unused labels. */
17162
17163 static void
17164 warn_unused_fortran_label (gfc_st_label *label)
17165 {
17166 if (label == NULL)
17167 return;
17168
17169 warn_unused_fortran_label (label->left);
17170
17171 if (label->defined == ST_LABEL_UNKNOWN)
17172 return;
17173
17174 switch (label->referenced)
17175 {
17176 case ST_LABEL_UNKNOWN:
17177 gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
17178 label->value, &label->where);
17179 break;
17180
17181 case ST_LABEL_BAD_TARGET:
17182 gfc_warning (OPT_Wunused_label,
17183 "Label %d at %L defined but cannot be used",
17184 label->value, &label->where);
17185 break;
17186
17187 default:
17188 break;
17189 }
17190
17191 warn_unused_fortran_label (label->right);
17192 }
17193
17194
17195 /* Returns the sequence type of a symbol or sequence. */
17196
17197 static seq_type
17198 sequence_type (gfc_typespec ts)
17199 {
17200 seq_type result;
17201 gfc_component *c;
17202
17203 switch (ts.type)
17204 {
17205 case BT_DERIVED:
17206
17207 if (ts.u.derived->components == NULL)
17208 return SEQ_NONDEFAULT;
17209
17210 result = sequence_type (ts.u.derived->components->ts);
17211 for (c = ts.u.derived->components->next; c; c = c->next)
17212 if (sequence_type (c->ts) != result)
17213 return SEQ_MIXED;
17214
17215 return result;
17216
17217 case BT_CHARACTER:
17218 if (ts.kind != gfc_default_character_kind)
17219 return SEQ_NONDEFAULT;
17220
17221 return SEQ_CHARACTER;
17222
17223 case BT_INTEGER:
17224 if (ts.kind != gfc_default_integer_kind)
17225 return SEQ_NONDEFAULT;
17226
17227 return SEQ_NUMERIC;
17228
17229 case BT_REAL:
17230 if (!(ts.kind == gfc_default_real_kind
17231 || ts.kind == gfc_default_double_kind))
17232 return SEQ_NONDEFAULT;
17233
17234 return SEQ_NUMERIC;
17235
17236 case BT_COMPLEX:
17237 if (ts.kind != gfc_default_complex_kind)
17238 return SEQ_NONDEFAULT;
17239
17240 return SEQ_NUMERIC;
17241
17242 case BT_LOGICAL:
17243 if (ts.kind != gfc_default_logical_kind)
17244 return SEQ_NONDEFAULT;
17245
17246 return SEQ_NUMERIC;
17247
17248 default:
17249 return SEQ_NONDEFAULT;
17250 }
17251 }
17252
17253
17254 /* Resolve derived type EQUIVALENCE object. */
17255
17256 static bool
17257 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
17258 {
17259 gfc_component *c = derived->components;
17260
17261 if (!derived)
17262 return true;
17263
17264 /* Shall not be an object of nonsequence derived type. */
17265 if (!derived->attr.sequence)
17266 {
17267 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
17268 "attribute to be an EQUIVALENCE object", sym->name,
17269 &e->where);
17270 return false;
17271 }
17272
17273 /* Shall not have allocatable components. */
17274 if (derived->attr.alloc_comp)
17275 {
17276 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
17277 "components to be an EQUIVALENCE object",sym->name,
17278 &e->where);
17279 return false;
17280 }
17281
17282 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
17283 {
17284 gfc_error ("Derived type variable %qs at %L with default "
17285 "initialization cannot be in EQUIVALENCE with a variable "
17286 "in COMMON", sym->name, &e->where);
17287 return false;
17288 }
17289
17290 for (; c ; c = c->next)
17291 {
17292 if (gfc_bt_struct (c->ts.type)
17293 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
17294 return false;
17295
17296 /* Shall not be an object of sequence derived type containing a pointer
17297 in the structure. */
17298 if (c->attr.pointer)
17299 {
17300 gfc_error ("Derived type variable %qs at %L with pointer "
17301 "component(s) cannot be an EQUIVALENCE object",
17302 sym->name, &e->where);
17303 return false;
17304 }
17305 }
17306 return true;
17307 }
17308
17309
17310 /* Resolve equivalence object.
17311 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
17312 an allocatable array, an object of nonsequence derived type, an object of
17313 sequence derived type containing a pointer at any level of component
17314 selection, an automatic object, a function name, an entry name, a result
17315 name, a named constant, a structure component, or a subobject of any of
17316 the preceding objects. A substring shall not have length zero. A
17317 derived type shall not have components with default initialization nor
17318 shall two objects of an equivalence group be initialized.
17319 Either all or none of the objects shall have an protected attribute.
17320 The simple constraints are done in symbol.cc(check_conflict) and the rest
17321 are implemented here. */
17322
17323 static void
17324 resolve_equivalence (gfc_equiv *eq)
17325 {
17326 gfc_symbol *sym;
17327 gfc_symbol *first_sym;
17328 gfc_expr *e;
17329 gfc_ref *r;
17330 locus *last_where = NULL;
17331 seq_type eq_type, last_eq_type;
17332 gfc_typespec *last_ts;
17333 int object, cnt_protected;
17334 const char *msg;
17335
17336 last_ts = &eq->expr->symtree->n.sym->ts;
17337
17338 first_sym = eq->expr->symtree->n.sym;
17339
17340 cnt_protected = 0;
17341
17342 for (object = 1; eq; eq = eq->eq, object++)
17343 {
17344 e = eq->expr;
17345
17346 e->ts = e->symtree->n.sym->ts;
17347 /* match_varspec might not know yet if it is seeing
17348 array reference or substring reference, as it doesn't
17349 know the types. */
17350 if (e->ref && e->ref->type == REF_ARRAY)
17351 {
17352 gfc_ref *ref = e->ref;
17353 sym = e->symtree->n.sym;
17354
17355 if (sym->attr.dimension)
17356 {
17357 ref->u.ar.as = sym->as;
17358 ref = ref->next;
17359 }
17360
17361 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
17362 if (e->ts.type == BT_CHARACTER
17363 && ref
17364 && ref->type == REF_ARRAY
17365 && ref->u.ar.dimen == 1
17366 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
17367 && ref->u.ar.stride[0] == NULL)
17368 {
17369 gfc_expr *start = ref->u.ar.start[0];
17370 gfc_expr *end = ref->u.ar.end[0];
17371 void *mem = NULL;
17372
17373 /* Optimize away the (:) reference. */
17374 if (start == NULL && end == NULL)
17375 {
17376 if (e->ref == ref)
17377 e->ref = ref->next;
17378 else
17379 e->ref->next = ref->next;
17380 mem = ref;
17381 }
17382 else
17383 {
17384 ref->type = REF_SUBSTRING;
17385 if (start == NULL)
17386 start = gfc_get_int_expr (gfc_charlen_int_kind,
17387 NULL, 1);
17388 ref->u.ss.start = start;
17389 if (end == NULL && e->ts.u.cl)
17390 end = gfc_copy_expr (e->ts.u.cl->length);
17391 ref->u.ss.end = end;
17392 ref->u.ss.length = e->ts.u.cl;
17393 e->ts.u.cl = NULL;
17394 }
17395 ref = ref->next;
17396 free (mem);
17397 }
17398
17399 /* Any further ref is an error. */
17400 if (ref)
17401 {
17402 gcc_assert (ref->type == REF_ARRAY);
17403 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
17404 &ref->u.ar.where);
17405 continue;
17406 }
17407 }
17408
17409 if (!gfc_resolve_expr (e))
17410 continue;
17411
17412 sym = e->symtree->n.sym;
17413
17414 if (sym->attr.is_protected)
17415 cnt_protected++;
17416 if (cnt_protected > 0 && cnt_protected != object)
17417 {
17418 gfc_error ("Either all or none of the objects in the "
17419 "EQUIVALENCE set at %L shall have the "
17420 "PROTECTED attribute",
17421 &e->where);
17422 break;
17423 }
17424
17425 /* Shall not equivalence common block variables in a PURE procedure. */
17426 if (sym->ns->proc_name
17427 && sym->ns->proc_name->attr.pure
17428 && sym->attr.in_common)
17429 {
17430 /* Need to check for symbols that may have entered the pure
17431 procedure via a USE statement. */
17432 bool saw_sym = false;
17433 if (sym->ns->use_stmts)
17434 {
17435 gfc_use_rename *r;
17436 for (r = sym->ns->use_stmts->rename; r; r = r->next)
17437 if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
17438 }
17439 else
17440 saw_sym = true;
17441
17442 if (saw_sym)
17443 gfc_error ("COMMON block member %qs at %L cannot be an "
17444 "EQUIVALENCE object in the pure procedure %qs",
17445 sym->name, &e->where, sym->ns->proc_name->name);
17446 break;
17447 }
17448
17449 /* Shall not be a named constant. */
17450 if (e->expr_type == EXPR_CONSTANT)
17451 {
17452 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
17453 "object", sym->name, &e->where);
17454 continue;
17455 }
17456
17457 if (e->ts.type == BT_DERIVED
17458 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
17459 continue;
17460
17461 /* Check that the types correspond correctly:
17462 Note 5.28:
17463 A numeric sequence structure may be equivalenced to another sequence
17464 structure, an object of default integer type, default real type, double
17465 precision real type, default logical type such that components of the
17466 structure ultimately only become associated to objects of the same
17467 kind. A character sequence structure may be equivalenced to an object
17468 of default character kind or another character sequence structure.
17469 Other objects may be equivalenced only to objects of the same type and
17470 kind parameters. */
17471
17472 /* Identical types are unconditionally OK. */
17473 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
17474 goto identical_types;
17475
17476 last_eq_type = sequence_type (*last_ts);
17477 eq_type = sequence_type (sym->ts);
17478
17479 /* Since the pair of objects is not of the same type, mixed or
17480 non-default sequences can be rejected. */
17481
17482 msg = "Sequence %s with mixed components in EQUIVALENCE "
17483 "statement at %L with different type objects";
17484 if ((object ==2
17485 && last_eq_type == SEQ_MIXED
17486 && last_where
17487 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
17488 || (eq_type == SEQ_MIXED
17489 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
17490 continue;
17491
17492 msg = "Non-default type object or sequence %s in EQUIVALENCE "
17493 "statement at %L with objects of different type";
17494 if ((object ==2
17495 && last_eq_type == SEQ_NONDEFAULT
17496 && last_where
17497 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
17498 || (eq_type == SEQ_NONDEFAULT
17499 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
17500 continue;
17501
17502 msg ="Non-CHARACTER object %qs in default CHARACTER "
17503 "EQUIVALENCE statement at %L";
17504 if (last_eq_type == SEQ_CHARACTER
17505 && eq_type != SEQ_CHARACTER
17506 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
17507 continue;
17508
17509 msg ="Non-NUMERIC object %qs in default NUMERIC "
17510 "EQUIVALENCE statement at %L";
17511 if (last_eq_type == SEQ_NUMERIC
17512 && eq_type != SEQ_NUMERIC
17513 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
17514 continue;
17515
17516 identical_types:
17517
17518 last_ts =&sym->ts;
17519 last_where = &e->where;
17520
17521 if (!e->ref)
17522 continue;
17523
17524 /* Shall not be an automatic array. */
17525 if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
17526 {
17527 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
17528 "an EQUIVALENCE object", sym->name, &e->where);
17529 continue;
17530 }
17531
17532 r = e->ref;
17533 while (r)
17534 {
17535 /* Shall not be a structure component. */
17536 if (r->type == REF_COMPONENT)
17537 {
17538 gfc_error ("Structure component %qs at %L cannot be an "
17539 "EQUIVALENCE object",
17540 r->u.c.component->name, &e->where);
17541 break;
17542 }
17543
17544 /* A substring shall not have length zero. */
17545 if (r->type == REF_SUBSTRING)
17546 {
17547 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
17548 {
17549 gfc_error ("Substring at %L has length zero",
17550 &r->u.ss.start->where);
17551 break;
17552 }
17553 }
17554 r = r->next;
17555 }
17556 }
17557 }
17558
17559
17560 /* Function called by resolve_fntype to flag other symbols used in the
17561 length type parameter specification of function results. */
17562
17563 static bool
17564 flag_fn_result_spec (gfc_expr *expr,
17565 gfc_symbol *sym,
17566 int *f ATTRIBUTE_UNUSED)
17567 {
17568 gfc_namespace *ns;
17569 gfc_symbol *s;
17570
17571 if (expr->expr_type == EXPR_VARIABLE)
17572 {
17573 s = expr->symtree->n.sym;
17574 for (ns = s->ns; ns; ns = ns->parent)
17575 if (!ns->parent)
17576 break;
17577
17578 if (sym == s)
17579 {
17580 gfc_error ("Self reference in character length expression "
17581 "for %qs at %L", sym->name, &expr->where);
17582 return true;
17583 }
17584
17585 if (!s->fn_result_spec
17586 && s->attr.flavor == FL_PARAMETER)
17587 {
17588 /* Function contained in a module.... */
17589 if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
17590 {
17591 gfc_symtree *st;
17592 s->fn_result_spec = 1;
17593 /* Make sure that this symbol is translated as a module
17594 variable. */
17595 st = gfc_get_unique_symtree (ns);
17596 st->n.sym = s;
17597 s->refs++;
17598 }
17599 /* ... which is use associated and called. */
17600 else if (s->attr.use_assoc || s->attr.used_in_submodule
17601 ||
17602 /* External function matched with an interface. */
17603 (s->ns->proc_name
17604 && ((s->ns == ns
17605 && s->ns->proc_name->attr.if_source == IFSRC_DECL)
17606 || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
17607 && s->ns->proc_name->attr.function))
17608 s->fn_result_spec = 1;
17609 }
17610 }
17611 return false;
17612 }
17613
17614
17615 /* Resolve function and ENTRY types, issue diagnostics if needed. */
17616
17617 static void
17618 resolve_fntype (gfc_namespace *ns)
17619 {
17620 gfc_entry_list *el;
17621 gfc_symbol *sym;
17622
17623 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
17624 return;
17625
17626 /* If there are any entries, ns->proc_name is the entry master
17627 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
17628 if (ns->entries)
17629 sym = ns->entries->sym;
17630 else
17631 sym = ns->proc_name;
17632 if (sym->result == sym
17633 && sym->ts.type == BT_UNKNOWN
17634 && !gfc_set_default_type (sym, 0, NULL)
17635 && !sym->attr.untyped)
17636 {
17637 gfc_error ("Function %qs at %L has no IMPLICIT type",
17638 sym->name, &sym->declared_at);
17639 sym->attr.untyped = 1;
17640 }
17641
17642 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
17643 && !sym->attr.contained
17644 && !gfc_check_symbol_access (sym->ts.u.derived)
17645 && gfc_check_symbol_access (sym))
17646 {
17647 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
17648 "%L of PRIVATE type %qs", sym->name,
17649 &sym->declared_at, sym->ts.u.derived->name);
17650 }
17651
17652 if (ns->entries)
17653 for (el = ns->entries->next; el; el = el->next)
17654 {
17655 if (el->sym->result == el->sym
17656 && el->sym->ts.type == BT_UNKNOWN
17657 && !gfc_set_default_type (el->sym, 0, NULL)
17658 && !el->sym->attr.untyped)
17659 {
17660 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
17661 el->sym->name, &el->sym->declared_at);
17662 el->sym->attr.untyped = 1;
17663 }
17664 }
17665
17666 if (sym->ts.type == BT_CHARACTER
17667 && sym->ts.u.cl->length
17668 && sym->ts.u.cl->length->ts.type == BT_INTEGER)
17669 gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
17670 }
17671
17672
17673 /* 12.3.2.1.1 Defined operators. */
17674
17675 static bool
17676 check_uop_procedure (gfc_symbol *sym, locus where)
17677 {
17678 gfc_formal_arglist *formal;
17679
17680 if (!sym->attr.function)
17681 {
17682 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
17683 sym->name, &where);
17684 return false;
17685 }
17686
17687 if (sym->ts.type == BT_CHARACTER
17688 && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
17689 && !(sym->result && ((sym->result->ts.u.cl
17690 && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
17691 {
17692 gfc_error ("User operator procedure %qs at %L cannot be assumed "
17693 "character length", sym->name, &where);
17694 return false;
17695 }
17696
17697 formal = gfc_sym_get_dummy_args (sym);
17698 if (!formal || !formal->sym)
17699 {
17700 gfc_error ("User operator procedure %qs at %L must have at least "
17701 "one argument", sym->name, &where);
17702 return false;
17703 }
17704
17705 if (formal->sym->attr.intent != INTENT_IN)
17706 {
17707 gfc_error ("First argument of operator interface at %L must be "
17708 "INTENT(IN)", &where);
17709 return false;
17710 }
17711
17712 if (formal->sym->attr.optional)
17713 {
17714 gfc_error ("First argument of operator interface at %L cannot be "
17715 "optional", &where);
17716 return false;
17717 }
17718
17719 formal = formal->next;
17720 if (!formal || !formal->sym)
17721 return true;
17722
17723 if (formal->sym->attr.intent != INTENT_IN)
17724 {
17725 gfc_error ("Second argument of operator interface at %L must be "
17726 "INTENT(IN)", &where);
17727 return false;
17728 }
17729
17730 if (formal->sym->attr.optional)
17731 {
17732 gfc_error ("Second argument of operator interface at %L cannot be "
17733 "optional", &where);
17734 return false;
17735 }
17736
17737 if (formal->next)
17738 {
17739 gfc_error ("Operator interface at %L must have, at most, two "
17740 "arguments", &where);
17741 return false;
17742 }
17743
17744 return true;
17745 }
17746
17747 static void
17748 gfc_resolve_uops (gfc_symtree *symtree)
17749 {
17750 gfc_interface *itr;
17751
17752 if (symtree == NULL)
17753 return;
17754
17755 gfc_resolve_uops (symtree->left);
17756 gfc_resolve_uops (symtree->right);
17757
17758 for (itr = symtree->n.uop->op; itr; itr = itr->next)
17759 check_uop_procedure (itr->sym, itr->sym->declared_at);
17760 }
17761
17762
17763 /* Examine all of the expressions associated with a program unit,
17764 assign types to all intermediate expressions, make sure that all
17765 assignments are to compatible types and figure out which names
17766 refer to which functions or subroutines. It doesn't check code
17767 block, which is handled by gfc_resolve_code. */
17768
17769 static void
17770 resolve_types (gfc_namespace *ns)
17771 {
17772 gfc_namespace *n;
17773 gfc_charlen *cl;
17774 gfc_data *d;
17775 gfc_equiv *eq;
17776 gfc_namespace* old_ns = gfc_current_ns;
17777 bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
17778
17779 if (ns->types_resolved)
17780 return;
17781
17782 /* Check that all IMPLICIT types are ok. */
17783 if (!ns->seen_implicit_none)
17784 {
17785 unsigned letter;
17786 for (letter = 0; letter != GFC_LETTERS; ++letter)
17787 if (ns->set_flag[letter]
17788 && !resolve_typespec_used (&ns->default_type[letter],
17789 &ns->implicit_loc[letter], NULL))
17790 return;
17791 }
17792
17793 gfc_current_ns = ns;
17794
17795 resolve_entries (ns);
17796
17797 resolve_common_vars (&ns->blank_common, false);
17798 resolve_common_blocks (ns->common_root);
17799
17800 resolve_contained_functions (ns);
17801
17802 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
17803 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
17804 gfc_resolve_formal_arglist (ns->proc_name);
17805
17806 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
17807
17808 for (cl = ns->cl_list; cl; cl = cl->next)
17809 resolve_charlen (cl);
17810
17811 gfc_traverse_ns (ns, resolve_symbol);
17812
17813 resolve_fntype (ns);
17814
17815 for (n = ns->contained; n; n = n->sibling)
17816 {
17817 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
17818 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
17819 "also be PURE", n->proc_name->name,
17820 &n->proc_name->declared_at);
17821
17822 resolve_types (n);
17823 }
17824
17825 forall_flag = 0;
17826 gfc_do_concurrent_flag = 0;
17827 gfc_check_interfaces (ns);
17828
17829 gfc_traverse_ns (ns, resolve_values);
17830
17831 if (ns->save_all || (!flag_automatic && !recursive))
17832 gfc_save_all (ns);
17833
17834 iter_stack = NULL;
17835 for (d = ns->data; d; d = d->next)
17836 resolve_data (d);
17837
17838 iter_stack = NULL;
17839 gfc_traverse_ns (ns, gfc_formalize_init_value);
17840
17841 gfc_traverse_ns (ns, gfc_verify_binding_labels);
17842
17843 for (eq = ns->equiv; eq; eq = eq->next)
17844 resolve_equivalence (eq);
17845
17846 /* Warn about unused labels. */
17847 if (warn_unused_label)
17848 warn_unused_fortran_label (ns->st_labels);
17849
17850 gfc_resolve_uops (ns->uop_root);
17851
17852 gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
17853
17854 gfc_resolve_omp_declare_simd (ns);
17855
17856 gfc_resolve_omp_udrs (ns->omp_udr_root);
17857
17858 ns->types_resolved = 1;
17859
17860 gfc_current_ns = old_ns;
17861 }
17862
17863
17864 /* Call gfc_resolve_code recursively. */
17865
17866 static void
17867 resolve_codes (gfc_namespace *ns)
17868 {
17869 gfc_namespace *n;
17870 bitmap_obstack old_obstack;
17871
17872 if (ns->resolved == 1)
17873 return;
17874
17875 for (n = ns->contained; n; n = n->sibling)
17876 resolve_codes (n);
17877
17878 gfc_current_ns = ns;
17879
17880 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
17881 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
17882 cs_base = NULL;
17883
17884 /* Set to an out of range value. */
17885 current_entry_id = -1;
17886
17887 old_obstack = labels_obstack;
17888 bitmap_obstack_initialize (&labels_obstack);
17889
17890 gfc_resolve_oacc_declare (ns);
17891 gfc_resolve_oacc_routines (ns);
17892 gfc_resolve_omp_local_vars (ns);
17893 gfc_resolve_code (ns->code, ns);
17894
17895 bitmap_obstack_release (&labels_obstack);
17896 labels_obstack = old_obstack;
17897 }
17898
17899
17900 /* This function is called after a complete program unit has been compiled.
17901 Its purpose is to examine all of the expressions associated with a program
17902 unit, assign types to all intermediate expressions, make sure that all
17903 assignments are to compatible types and figure out which names refer to
17904 which functions or subroutines. */
17905
17906 void
17907 gfc_resolve (gfc_namespace *ns)
17908 {
17909 gfc_namespace *old_ns;
17910 code_stack *old_cs_base;
17911 struct gfc_omp_saved_state old_omp_state;
17912
17913 if (ns->resolved)
17914 return;
17915
17916 ns->resolved = -1;
17917 old_ns = gfc_current_ns;
17918 old_cs_base = cs_base;
17919
17920 /* As gfc_resolve can be called during resolution of an OpenMP construct
17921 body, we should clear any state associated to it, so that say NS's
17922 DO loops are not interpreted as OpenMP loops. */
17923 if (!ns->construct_entities)
17924 gfc_omp_save_and_clear_state (&old_omp_state);
17925
17926 resolve_types (ns);
17927 component_assignment_level = 0;
17928 resolve_codes (ns);
17929
17930 if (ns->omp_assumes)
17931 gfc_resolve_omp_assumptions (ns->omp_assumes);
17932
17933 gfc_current_ns = old_ns;
17934 cs_base = old_cs_base;
17935 ns->resolved = 1;
17936
17937 gfc_run_passes (ns);
17938
17939 if (!ns->construct_entities)
17940 gfc_omp_restore_state (&old_omp_state);
17941 }
This page took 0.869079 seconds and 5 git commands to generate.