PR fortran/96085 - ICE in gfc_finish_var_decl, at fortran/trans-decl.c:694
[gcc.git] / gcc / fortran / resolve.c
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2020 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "bitmap.h"
26 #include "gfortran.h"
27 #include "arith.h" /* For gfc_compare_expr(). */
28 #include "dependency.h"
29 #include "data.h"
30 #include "target-memory.h" /* for gfc_simplify_transfer */
31 #include "constructor.h"
32
33 /* Types used in equivalence statements. */
34
35 enum seq_type
36 {
37 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
38 };
39
40 /* Stack to keep track of the nesting of blocks as we move through the
41 code. See resolve_branch() and gfc_resolve_code(). */
42
43 typedef struct code_stack
44 {
45 struct gfc_code *head, *current;
46 struct code_stack *prev;
47
48 /* This bitmap keeps track of the targets valid for a branch from
49 inside this block except for END {IF|SELECT}s of enclosing
50 blocks. */
51 bitmap reachable_labels;
52 }
53 code_stack;
54
55 static code_stack *cs_base = NULL;
56
57
58 /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
59
60 static int forall_flag;
61 int gfc_do_concurrent_flag;
62
63 /* True when we are resolving an expression that is an actual argument to
64 a procedure. */
65 static bool actual_arg = false;
66 /* True when we are resolving an expression that is the first actual argument
67 to a procedure. */
68 static bool first_actual_arg = false;
69
70
71 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
72
73 static int omp_workshare_flag;
74
75 /* True if we are processing a formal arglist. The corresponding function
76 resets the flag each time that it is read. */
77 static bool formal_arg_flag = false;
78
79 /* True if we are resolving a specification expression. */
80 static bool specification_expr = false;
81
82 /* The id of the last entry seen. */
83 static int current_entry_id;
84
85 /* We use bitmaps to determine if a branch target is valid. */
86 static bitmap_obstack labels_obstack;
87
88 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
89 static bool inquiry_argument = false;
90
91
92 bool
93 gfc_is_formal_arg (void)
94 {
95 return formal_arg_flag;
96 }
97
98 /* Is the symbol host associated? */
99 static bool
100 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
101 {
102 for (ns = ns->parent; ns; ns = ns->parent)
103 {
104 if (sym->ns == ns)
105 return true;
106 }
107
108 return false;
109 }
110
111 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
112 an ABSTRACT derived-type. If where is not NULL, an error message with that
113 locus is printed, optionally using name. */
114
115 static bool
116 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
117 {
118 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
119 {
120 if (where)
121 {
122 if (name)
123 gfc_error ("%qs at %L is of the ABSTRACT type %qs",
124 name, where, ts->u.derived->name);
125 else
126 gfc_error ("ABSTRACT type %qs used at %L",
127 ts->u.derived->name, where);
128 }
129
130 return false;
131 }
132
133 return true;
134 }
135
136
137 static bool
138 check_proc_interface (gfc_symbol *ifc, locus *where)
139 {
140 /* Several checks for F08:C1216. */
141 if (ifc->attr.procedure)
142 {
143 gfc_error ("Interface %qs at %L is declared "
144 "in a later PROCEDURE statement", ifc->name, where);
145 return false;
146 }
147 if (ifc->generic)
148 {
149 /* For generic interfaces, check if there is
150 a specific procedure with the same name. */
151 gfc_interface *gen = ifc->generic;
152 while (gen && strcmp (gen->sym->name, ifc->name) != 0)
153 gen = gen->next;
154 if (!gen)
155 {
156 gfc_error ("Interface %qs at %L may not be generic",
157 ifc->name, where);
158 return false;
159 }
160 }
161 if (ifc->attr.proc == PROC_ST_FUNCTION)
162 {
163 gfc_error ("Interface %qs at %L may not be a statement function",
164 ifc->name, where);
165 return false;
166 }
167 if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
168 || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
169 ifc->attr.intrinsic = 1;
170 if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
171 {
172 gfc_error ("Intrinsic procedure %qs not allowed in "
173 "PROCEDURE statement at %L", ifc->name, where);
174 return false;
175 }
176 if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
177 {
178 gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
179 return false;
180 }
181 return true;
182 }
183
184
185 static void resolve_symbol (gfc_symbol *sym);
186
187
188 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
189
190 static bool
191 resolve_procedure_interface (gfc_symbol *sym)
192 {
193 gfc_symbol *ifc = sym->ts.interface;
194
195 if (!ifc)
196 return true;
197
198 if (ifc == sym)
199 {
200 gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
201 sym->name, &sym->declared_at);
202 return false;
203 }
204 if (!check_proc_interface (ifc, &sym->declared_at))
205 return false;
206
207 if (ifc->attr.if_source || ifc->attr.intrinsic)
208 {
209 /* Resolve interface and copy attributes. */
210 resolve_symbol (ifc);
211 if (ifc->attr.intrinsic)
212 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
213
214 if (ifc->result)
215 {
216 sym->ts = ifc->result->ts;
217 sym->attr.allocatable = ifc->result->attr.allocatable;
218 sym->attr.pointer = ifc->result->attr.pointer;
219 sym->attr.dimension = ifc->result->attr.dimension;
220 sym->attr.class_ok = ifc->result->attr.class_ok;
221 sym->as = gfc_copy_array_spec (ifc->result->as);
222 sym->result = sym;
223 }
224 else
225 {
226 sym->ts = ifc->ts;
227 sym->attr.allocatable = ifc->attr.allocatable;
228 sym->attr.pointer = ifc->attr.pointer;
229 sym->attr.dimension = ifc->attr.dimension;
230 sym->attr.class_ok = ifc->attr.class_ok;
231 sym->as = gfc_copy_array_spec (ifc->as);
232 }
233 sym->ts.interface = ifc;
234 sym->attr.function = ifc->attr.function;
235 sym->attr.subroutine = ifc->attr.subroutine;
236
237 sym->attr.pure = ifc->attr.pure;
238 sym->attr.elemental = ifc->attr.elemental;
239 sym->attr.contiguous = ifc->attr.contiguous;
240 sym->attr.recursive = ifc->attr.recursive;
241 sym->attr.always_explicit = ifc->attr.always_explicit;
242 sym->attr.ext_attr |= ifc->attr.ext_attr;
243 sym->attr.is_bind_c = ifc->attr.is_bind_c;
244 /* Copy char length. */
245 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
246 {
247 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
248 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
249 && !gfc_resolve_expr (sym->ts.u.cl->length))
250 return false;
251 }
252 }
253
254 return true;
255 }
256
257
258 /* Resolve types of formal argument lists. These have to be done early so that
259 the formal argument lists of module procedures can be copied to the
260 containing module before the individual procedures are resolved
261 individually. We also resolve argument lists of procedures in interface
262 blocks because they are self-contained scoping units.
263
264 Since a dummy argument cannot be a non-dummy procedure, the only
265 resort left for untyped names are the IMPLICIT types. */
266
267 void
268 gfc_resolve_formal_arglist (gfc_symbol *proc)
269 {
270 gfc_formal_arglist *f;
271 gfc_symbol *sym;
272 bool saved_specification_expr;
273 int i;
274
275 if (proc->result != NULL)
276 sym = proc->result;
277 else
278 sym = proc;
279
280 if (gfc_elemental (proc)
281 || sym->attr.pointer || sym->attr.allocatable
282 || (sym->as && sym->as->rank != 0))
283 {
284 proc->attr.always_explicit = 1;
285 sym->attr.always_explicit = 1;
286 }
287
288 formal_arg_flag = true;
289
290 for (f = proc->formal; f; f = f->next)
291 {
292 gfc_array_spec *as;
293
294 sym = f->sym;
295
296 if (sym == NULL)
297 {
298 /* Alternate return placeholder. */
299 if (gfc_elemental (proc))
300 gfc_error ("Alternate return specifier in elemental subroutine "
301 "%qs at %L is not allowed", proc->name,
302 &proc->declared_at);
303 if (proc->attr.function)
304 gfc_error ("Alternate return specifier in function "
305 "%qs at %L is not allowed", proc->name,
306 &proc->declared_at);
307 continue;
308 }
309 else if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
310 && !resolve_procedure_interface (sym))
311 return;
312
313 if (strcmp (proc->name, sym->name) == 0)
314 {
315 gfc_error ("Self-referential argument "
316 "%qs at %L is not allowed", sym->name,
317 &proc->declared_at);
318 return;
319 }
320
321 if (sym->attr.if_source != IFSRC_UNKNOWN)
322 gfc_resolve_formal_arglist (sym);
323
324 if (sym->attr.subroutine || sym->attr.external)
325 {
326 if (sym->attr.flavor == FL_UNKNOWN)
327 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
328 }
329 else
330 {
331 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
332 && (!sym->attr.function || sym->result == sym))
333 gfc_set_default_type (sym, 1, sym->ns);
334 }
335
336 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
337 ? CLASS_DATA (sym)->as : sym->as;
338
339 saved_specification_expr = specification_expr;
340 specification_expr = true;
341 gfc_resolve_array_spec (as, 0);
342 specification_expr = saved_specification_expr;
343
344 /* We can't tell if an array with dimension (:) is assumed or deferred
345 shape until we know if it has the pointer or allocatable attributes.
346 */
347 if (as && as->rank > 0 && as->type == AS_DEFERRED
348 && ((sym->ts.type != BT_CLASS
349 && !(sym->attr.pointer || sym->attr.allocatable))
350 || (sym->ts.type == BT_CLASS
351 && !(CLASS_DATA (sym)->attr.class_pointer
352 || CLASS_DATA (sym)->attr.allocatable)))
353 && sym->attr.flavor != FL_PROCEDURE)
354 {
355 as->type = AS_ASSUMED_SHAPE;
356 for (i = 0; i < as->rank; i++)
357 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
358 }
359
360 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
361 || (as && as->type == AS_ASSUMED_RANK)
362 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
363 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
364 && (CLASS_DATA (sym)->attr.class_pointer
365 || CLASS_DATA (sym)->attr.allocatable
366 || CLASS_DATA (sym)->attr.target))
367 || sym->attr.optional)
368 {
369 proc->attr.always_explicit = 1;
370 if (proc->result)
371 proc->result->attr.always_explicit = 1;
372 }
373
374 /* If the flavor is unknown at this point, it has to be a variable.
375 A procedure specification would have already set the type. */
376
377 if (sym->attr.flavor == FL_UNKNOWN)
378 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
379
380 if (gfc_pure (proc))
381 {
382 if (sym->attr.flavor == FL_PROCEDURE)
383 {
384 /* F08:C1279. */
385 if (!gfc_pure (sym))
386 {
387 gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
388 "also be PURE", sym->name, &sym->declared_at);
389 continue;
390 }
391 }
392 else if (!sym->attr.pointer)
393 {
394 if (proc->attr.function && sym->attr.intent != INTENT_IN)
395 {
396 if (sym->attr.value)
397 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
398 " of pure function %qs at %L with VALUE "
399 "attribute but without INTENT(IN)",
400 sym->name, proc->name, &sym->declared_at);
401 else
402 gfc_error ("Argument %qs of pure function %qs at %L must "
403 "be INTENT(IN) or VALUE", sym->name, proc->name,
404 &sym->declared_at);
405 }
406
407 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
408 {
409 if (sym->attr.value)
410 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
411 " of pure subroutine %qs at %L with VALUE "
412 "attribute but without INTENT", sym->name,
413 proc->name, &sym->declared_at);
414 else
415 gfc_error ("Argument %qs of pure subroutine %qs at %L "
416 "must have its INTENT specified or have the "
417 "VALUE attribute", sym->name, proc->name,
418 &sym->declared_at);
419 }
420 }
421
422 /* F08:C1278a. */
423 if (sym->ts.type == BT_CLASS && sym->attr.intent == INTENT_OUT)
424 {
425 gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L"
426 " may not be polymorphic", sym->name, proc->name,
427 &sym->declared_at);
428 continue;
429 }
430 }
431
432 if (proc->attr.implicit_pure)
433 {
434 if (sym->attr.flavor == FL_PROCEDURE)
435 {
436 if (!gfc_pure (sym))
437 proc->attr.implicit_pure = 0;
438 }
439 else if (!sym->attr.pointer)
440 {
441 if (proc->attr.function && sym->attr.intent != INTENT_IN
442 && !sym->value)
443 proc->attr.implicit_pure = 0;
444
445 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
446 && !sym->value)
447 proc->attr.implicit_pure = 0;
448 }
449 }
450
451 if (gfc_elemental (proc))
452 {
453 /* F08:C1289. */
454 if (sym->attr.codimension
455 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
456 && CLASS_DATA (sym)->attr.codimension))
457 {
458 gfc_error ("Coarray dummy argument %qs at %L to elemental "
459 "procedure", sym->name, &sym->declared_at);
460 continue;
461 }
462
463 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
464 && CLASS_DATA (sym)->as))
465 {
466 gfc_error ("Argument %qs of elemental procedure at %L must "
467 "be scalar", sym->name, &sym->declared_at);
468 continue;
469 }
470
471 if (sym->attr.allocatable
472 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
473 && CLASS_DATA (sym)->attr.allocatable))
474 {
475 gfc_error ("Argument %qs of elemental procedure at %L cannot "
476 "have the ALLOCATABLE attribute", sym->name,
477 &sym->declared_at);
478 continue;
479 }
480
481 if (sym->attr.pointer
482 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
483 && CLASS_DATA (sym)->attr.class_pointer))
484 {
485 gfc_error ("Argument %qs of elemental procedure at %L cannot "
486 "have the POINTER attribute", sym->name,
487 &sym->declared_at);
488 continue;
489 }
490
491 if (sym->attr.flavor == FL_PROCEDURE)
492 {
493 gfc_error ("Dummy procedure %qs not allowed in elemental "
494 "procedure %qs at %L", sym->name, proc->name,
495 &sym->declared_at);
496 continue;
497 }
498
499 /* Fortran 2008 Corrigendum 1, C1290a. */
500 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
501 {
502 gfc_error ("Argument %qs of elemental procedure %qs at %L must "
503 "have its INTENT specified or have the VALUE "
504 "attribute", sym->name, proc->name,
505 &sym->declared_at);
506 continue;
507 }
508 }
509
510 /* Each dummy shall be specified to be scalar. */
511 if (proc->attr.proc == PROC_ST_FUNCTION)
512 {
513 if (sym->as != NULL)
514 {
515 /* F03:C1263 (R1238) The function-name and each dummy-arg-name
516 shall be specified, explicitly or implicitly, to be scalar. */
517 gfc_error ("Argument '%s' of statement function '%s' at %L "
518 "must be scalar", sym->name, proc->name,
519 &proc->declared_at);
520 continue;
521 }
522
523 if (sym->ts.type == BT_CHARACTER)
524 {
525 gfc_charlen *cl = sym->ts.u.cl;
526 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
527 {
528 gfc_error ("Character-valued argument %qs of statement "
529 "function at %L must have constant length",
530 sym->name, &sym->declared_at);
531 continue;
532 }
533 }
534 }
535 }
536 formal_arg_flag = false;
537 }
538
539
540 /* Work function called when searching for symbols that have argument lists
541 associated with them. */
542
543 static void
544 find_arglists (gfc_symbol *sym)
545 {
546 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
547 || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
548 return;
549
550 gfc_resolve_formal_arglist (sym);
551 }
552
553
554 /* Given a namespace, resolve all formal argument lists within the namespace.
555 */
556
557 static void
558 resolve_formal_arglists (gfc_namespace *ns)
559 {
560 if (ns == NULL)
561 return;
562
563 gfc_traverse_ns (ns, find_arglists);
564 }
565
566
567 static void
568 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
569 {
570 bool t;
571
572 if (sym && sym->attr.flavor == FL_PROCEDURE
573 && sym->ns->parent
574 && sym->ns->parent->proc_name
575 && sym->ns->parent->proc_name->attr.flavor == FL_PROCEDURE
576 && !strcmp (sym->name, sym->ns->parent->proc_name->name))
577 gfc_error ("Contained procedure %qs at %L has the same name as its "
578 "encompassing procedure", sym->name, &sym->declared_at);
579
580 /* If this namespace is not a function or an entry master function,
581 ignore it. */
582 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
583 || sym->attr.entry_master)
584 return;
585
586 if (!sym->result)
587 return;
588
589 /* Try to find out of what the return type is. */
590 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
591 {
592 t = gfc_set_default_type (sym->result, 0, ns);
593
594 if (!t && !sym->result->attr.untyped)
595 {
596 if (sym->result == sym)
597 gfc_error ("Contained function %qs at %L has no IMPLICIT type",
598 sym->name, &sym->declared_at);
599 else if (!sym->result->attr.proc_pointer)
600 gfc_error ("Result %qs of contained function %qs at %L has "
601 "no IMPLICIT type", sym->result->name, sym->name,
602 &sym->result->declared_at);
603 sym->result->attr.untyped = 1;
604 }
605 }
606
607 /* Fortran 2008 Draft Standard, page 535, C418, on type-param-value
608 type, lists the only ways a character length value of * can be used:
609 dummy arguments of procedures, named constants, function results and
610 in allocate statements if the allocate_object is an assumed length dummy
611 in external functions. Internal function results and results of module
612 procedures are not on this list, ergo, not permitted. */
613
614 if (sym->result->ts.type == BT_CHARACTER)
615 {
616 gfc_charlen *cl = sym->result->ts.u.cl;
617 if ((!cl || !cl->length) && !sym->result->ts.deferred)
618 {
619 /* See if this is a module-procedure and adapt error message
620 accordingly. */
621 bool module_proc;
622 gcc_assert (ns->parent && ns->parent->proc_name);
623 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
624
625 gfc_error (module_proc
626 ? G_("Character-valued module procedure %qs at %L"
627 " must not be assumed length")
628 : G_("Character-valued internal function %qs at %L"
629 " must not be assumed length"),
630 sym->name, &sym->declared_at);
631 }
632 }
633 }
634
635
636 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
637 introduce duplicates. */
638
639 static void
640 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
641 {
642 gfc_formal_arglist *f, *new_arglist;
643 gfc_symbol *new_sym;
644
645 for (; new_args != NULL; new_args = new_args->next)
646 {
647 new_sym = new_args->sym;
648 /* See if this arg is already in the formal argument list. */
649 for (f = proc->formal; f; f = f->next)
650 {
651 if (new_sym == f->sym)
652 break;
653 }
654
655 if (f)
656 continue;
657
658 /* Add a new argument. Argument order is not important. */
659 new_arglist = gfc_get_formal_arglist ();
660 new_arglist->sym = new_sym;
661 new_arglist->next = proc->formal;
662 proc->formal = new_arglist;
663 }
664 }
665
666
667 /* Flag the arguments that are not present in all entries. */
668
669 static void
670 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
671 {
672 gfc_formal_arglist *f, *head;
673 head = new_args;
674
675 for (f = proc->formal; f; f = f->next)
676 {
677 if (f->sym == NULL)
678 continue;
679
680 for (new_args = head; new_args; new_args = new_args->next)
681 {
682 if (new_args->sym == f->sym)
683 break;
684 }
685
686 if (new_args)
687 continue;
688
689 f->sym->attr.not_always_present = 1;
690 }
691 }
692
693
694 /* Resolve alternate entry points. If a symbol has multiple entry points we
695 create a new master symbol for the main routine, and turn the existing
696 symbol into an entry point. */
697
698 static void
699 resolve_entries (gfc_namespace *ns)
700 {
701 gfc_namespace *old_ns;
702 gfc_code *c;
703 gfc_symbol *proc;
704 gfc_entry_list *el;
705 char name[GFC_MAX_SYMBOL_LEN + 1];
706 static int master_count = 0;
707
708 if (ns->proc_name == NULL)
709 return;
710
711 /* No need to do anything if this procedure doesn't have alternate entry
712 points. */
713 if (!ns->entries)
714 return;
715
716 /* We may already have resolved alternate entry points. */
717 if (ns->proc_name->attr.entry_master)
718 return;
719
720 /* If this isn't a procedure something has gone horribly wrong. */
721 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
722
723 /* Remember the current namespace. */
724 old_ns = gfc_current_ns;
725
726 gfc_current_ns = ns;
727
728 /* Add the main entry point to the list of entry points. */
729 el = gfc_get_entry_list ();
730 el->sym = ns->proc_name;
731 el->id = 0;
732 el->next = ns->entries;
733 ns->entries = el;
734 ns->proc_name->attr.entry = 1;
735
736 /* If it is a module function, it needs to be in the right namespace
737 so that gfc_get_fake_result_decl can gather up the results. The
738 need for this arose in get_proc_name, where these beasts were
739 left in their own namespace, to keep prior references linked to
740 the entry declaration.*/
741 if (ns->proc_name->attr.function
742 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
743 el->sym->ns = ns;
744
745 /* Do the same for entries where the master is not a module
746 procedure. These are retained in the module namespace because
747 of the module procedure declaration. */
748 for (el = el->next; el; el = el->next)
749 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
750 && el->sym->attr.mod_proc)
751 el->sym->ns = ns;
752 el = ns->entries;
753
754 /* Add an entry statement for it. */
755 c = gfc_get_code (EXEC_ENTRY);
756 c->ext.entry = el;
757 c->next = ns->code;
758 ns->code = c;
759
760 /* Create a new symbol for the master function. */
761 /* Give the internal function a unique name (within this file).
762 Also include the function name so the user has some hope of figuring
763 out what is going on. */
764 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
765 master_count++, ns->proc_name->name);
766 gfc_get_ha_symbol (name, &proc);
767 gcc_assert (proc != NULL);
768
769 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
770 if (ns->proc_name->attr.subroutine)
771 gfc_add_subroutine (&proc->attr, proc->name, NULL);
772 else
773 {
774 gfc_symbol *sym;
775 gfc_typespec *ts, *fts;
776 gfc_array_spec *as, *fas;
777 gfc_add_function (&proc->attr, proc->name, NULL);
778 proc->result = proc;
779 fas = ns->entries->sym->as;
780 fas = fas ? fas : ns->entries->sym->result->as;
781 fts = &ns->entries->sym->result->ts;
782 if (fts->type == BT_UNKNOWN)
783 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
784 for (el = ns->entries->next; el; el = el->next)
785 {
786 ts = &el->sym->result->ts;
787 as = el->sym->as;
788 as = as ? as : el->sym->result->as;
789 if (ts->type == BT_UNKNOWN)
790 ts = gfc_get_default_type (el->sym->result->name, NULL);
791
792 if (! gfc_compare_types (ts, fts)
793 || (el->sym->result->attr.dimension
794 != ns->entries->sym->result->attr.dimension)
795 || (el->sym->result->attr.pointer
796 != ns->entries->sym->result->attr.pointer))
797 break;
798 else if (as && fas && ns->entries->sym->result != el->sym->result
799 && gfc_compare_array_spec (as, fas) == 0)
800 gfc_error ("Function %s at %L has entries with mismatched "
801 "array specifications", ns->entries->sym->name,
802 &ns->entries->sym->declared_at);
803 /* The characteristics need to match and thus both need to have
804 the same string length, i.e. both len=*, or both len=4.
805 Having both len=<variable> is also possible, but difficult to
806 check at compile time. */
807 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
808 && (((ts->u.cl->length && !fts->u.cl->length)
809 ||(!ts->u.cl->length && fts->u.cl->length))
810 || (ts->u.cl->length
811 && ts->u.cl->length->expr_type
812 != fts->u.cl->length->expr_type)
813 || (ts->u.cl->length
814 && ts->u.cl->length->expr_type == EXPR_CONSTANT
815 && mpz_cmp (ts->u.cl->length->value.integer,
816 fts->u.cl->length->value.integer) != 0)))
817 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
818 "entries returning variables of different "
819 "string lengths", ns->entries->sym->name,
820 &ns->entries->sym->declared_at);
821 }
822
823 if (el == NULL)
824 {
825 sym = ns->entries->sym->result;
826 /* All result types the same. */
827 proc->ts = *fts;
828 if (sym->attr.dimension)
829 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
830 if (sym->attr.pointer)
831 gfc_add_pointer (&proc->attr, NULL);
832 }
833 else
834 {
835 /* Otherwise the result will be passed through a union by
836 reference. */
837 proc->attr.mixed_entry_master = 1;
838 for (el = ns->entries; el; el = el->next)
839 {
840 sym = el->sym->result;
841 if (sym->attr.dimension)
842 {
843 if (el == ns->entries)
844 gfc_error ("FUNCTION result %s cannot be an array in "
845 "FUNCTION %s at %L", sym->name,
846 ns->entries->sym->name, &sym->declared_at);
847 else
848 gfc_error ("ENTRY result %s cannot be an array in "
849 "FUNCTION %s at %L", sym->name,
850 ns->entries->sym->name, &sym->declared_at);
851 }
852 else if (sym->attr.pointer)
853 {
854 if (el == ns->entries)
855 gfc_error ("FUNCTION result %s cannot be a POINTER in "
856 "FUNCTION %s at %L", sym->name,
857 ns->entries->sym->name, &sym->declared_at);
858 else
859 gfc_error ("ENTRY result %s cannot be a POINTER in "
860 "FUNCTION %s at %L", sym->name,
861 ns->entries->sym->name, &sym->declared_at);
862 }
863 else
864 {
865 ts = &sym->ts;
866 if (ts->type == BT_UNKNOWN)
867 ts = gfc_get_default_type (sym->name, NULL);
868 switch (ts->type)
869 {
870 case BT_INTEGER:
871 if (ts->kind == gfc_default_integer_kind)
872 sym = NULL;
873 break;
874 case BT_REAL:
875 if (ts->kind == gfc_default_real_kind
876 || ts->kind == gfc_default_double_kind)
877 sym = NULL;
878 break;
879 case BT_COMPLEX:
880 if (ts->kind == gfc_default_complex_kind)
881 sym = NULL;
882 break;
883 case BT_LOGICAL:
884 if (ts->kind == gfc_default_logical_kind)
885 sym = NULL;
886 break;
887 case BT_UNKNOWN:
888 /* We will issue error elsewhere. */
889 sym = NULL;
890 break;
891 default:
892 break;
893 }
894 if (sym)
895 {
896 if (el == ns->entries)
897 gfc_error ("FUNCTION result %s cannot be of type %s "
898 "in FUNCTION %s at %L", sym->name,
899 gfc_typename (ts), ns->entries->sym->name,
900 &sym->declared_at);
901 else
902 gfc_error ("ENTRY result %s cannot be of type %s "
903 "in FUNCTION %s at %L", sym->name,
904 gfc_typename (ts), ns->entries->sym->name,
905 &sym->declared_at);
906 }
907 }
908 }
909 }
910 }
911 proc->attr.access = ACCESS_PRIVATE;
912 proc->attr.entry_master = 1;
913
914 /* Merge all the entry point arguments. */
915 for (el = ns->entries; el; el = el->next)
916 merge_argument_lists (proc, el->sym->formal);
917
918 /* Check the master formal arguments for any that are not
919 present in all entry points. */
920 for (el = ns->entries; el; el = el->next)
921 check_argument_lists (proc, el->sym->formal);
922
923 /* Use the master function for the function body. */
924 ns->proc_name = proc;
925
926 /* Finalize the new symbols. */
927 gfc_commit_symbols ();
928
929 /* Restore the original namespace. */
930 gfc_current_ns = old_ns;
931 }
932
933
934 /* Resolve common variables. */
935 static void
936 resolve_common_vars (gfc_common_head *common_block, bool named_common)
937 {
938 gfc_symbol *csym = common_block->head;
939
940 for (; csym; csym = csym->common_next)
941 {
942 /* gfc_add_in_common may have been called before, but the reported errors
943 have been ignored to continue parsing.
944 We do the checks again here. */
945 if (!csym->attr.use_assoc)
946 {
947 gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
948 gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
949 &common_block->where);
950 }
951
952 if (csym->value || csym->attr.data)
953 {
954 if (!csym->ns->is_block_data)
955 gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
956 "but only in BLOCK DATA initialization is "
957 "allowed", csym->name, &csym->declared_at);
958 else if (!named_common)
959 gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
960 "in a blank COMMON but initialization is only "
961 "allowed in named common blocks", csym->name,
962 &csym->declared_at);
963 }
964
965 if (UNLIMITED_POLY (csym))
966 gfc_error_now ("%qs in cannot appear in COMMON at %L "
967 "[F2008:C5100]", csym->name, &csym->declared_at);
968
969 if (csym->ts.type != BT_DERIVED)
970 continue;
971
972 if (!(csym->ts.u.derived->attr.sequence
973 || csym->ts.u.derived->attr.is_bind_c))
974 gfc_error_now ("Derived type variable %qs in COMMON at %L "
975 "has neither the SEQUENCE nor the BIND(C) "
976 "attribute", csym->name, &csym->declared_at);
977 if (csym->ts.u.derived->attr.alloc_comp)
978 gfc_error_now ("Derived type variable %qs in COMMON at %L "
979 "has an ultimate component that is "
980 "allocatable", csym->name, &csym->declared_at);
981 if (gfc_has_default_initializer (csym->ts.u.derived))
982 gfc_error_now ("Derived type variable %qs in COMMON at %L "
983 "may not have default initializer", csym->name,
984 &csym->declared_at);
985
986 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
987 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
988 }
989 }
990
991 /* Resolve common blocks. */
992 static void
993 resolve_common_blocks (gfc_symtree *common_root)
994 {
995 gfc_symbol *sym;
996 gfc_gsymbol * gsym;
997
998 if (common_root == NULL)
999 return;
1000
1001 if (common_root->left)
1002 resolve_common_blocks (common_root->left);
1003 if (common_root->right)
1004 resolve_common_blocks (common_root->right);
1005
1006 resolve_common_vars (common_root->n.common, true);
1007
1008 /* The common name is a global name - in Fortran 2003 also if it has a
1009 C binding name, since Fortran 2008 only the C binding name is a global
1010 identifier. */
1011 if (!common_root->n.common->binding_label
1012 || gfc_notification_std (GFC_STD_F2008))
1013 {
1014 gsym = gfc_find_gsymbol (gfc_gsym_root,
1015 common_root->n.common->name);
1016
1017 if (gsym && gfc_notification_std (GFC_STD_F2008)
1018 && gsym->type == GSYM_COMMON
1019 && ((common_root->n.common->binding_label
1020 && (!gsym->binding_label
1021 || strcmp (common_root->n.common->binding_label,
1022 gsym->binding_label) != 0))
1023 || (!common_root->n.common->binding_label
1024 && gsym->binding_label)))
1025 {
1026 gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
1027 "identifier and must thus have the same binding name "
1028 "as the same-named COMMON block at %L: %s vs %s",
1029 common_root->n.common->name, &common_root->n.common->where,
1030 &gsym->where,
1031 common_root->n.common->binding_label
1032 ? common_root->n.common->binding_label : "(blank)",
1033 gsym->binding_label ? gsym->binding_label : "(blank)");
1034 return;
1035 }
1036
1037 if (gsym && gsym->type != GSYM_COMMON
1038 && !common_root->n.common->binding_label)
1039 {
1040 gfc_error ("COMMON block %qs at %L uses the same global identifier "
1041 "as entity at %L",
1042 common_root->n.common->name, &common_root->n.common->where,
1043 &gsym->where);
1044 return;
1045 }
1046 if (gsym && gsym->type != GSYM_COMMON)
1047 {
1048 gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
1049 "%L sharing the identifier with global non-COMMON-block "
1050 "entity at %L", common_root->n.common->name,
1051 &common_root->n.common->where, &gsym->where);
1052 return;
1053 }
1054 if (!gsym)
1055 {
1056 gsym = gfc_get_gsymbol (common_root->n.common->name, false);
1057 gsym->type = GSYM_COMMON;
1058 gsym->where = common_root->n.common->where;
1059 gsym->defined = 1;
1060 }
1061 gsym->used = 1;
1062 }
1063
1064 if (common_root->n.common->binding_label)
1065 {
1066 gsym = gfc_find_gsymbol (gfc_gsym_root,
1067 common_root->n.common->binding_label);
1068 if (gsym && gsym->type != GSYM_COMMON)
1069 {
1070 gfc_error ("COMMON block at %L with binding label %qs uses the same "
1071 "global identifier as entity at %L",
1072 &common_root->n.common->where,
1073 common_root->n.common->binding_label, &gsym->where);
1074 return;
1075 }
1076 if (!gsym)
1077 {
1078 gsym = gfc_get_gsymbol (common_root->n.common->binding_label, true);
1079 gsym->type = GSYM_COMMON;
1080 gsym->where = common_root->n.common->where;
1081 gsym->defined = 1;
1082 }
1083 gsym->used = 1;
1084 }
1085
1086 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1087 if (sym == NULL)
1088 return;
1089
1090 if (sym->attr.flavor == FL_PARAMETER)
1091 gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
1092 sym->name, &common_root->n.common->where, &sym->declared_at);
1093
1094 if (sym->attr.external)
1095 gfc_error ("COMMON block %qs at %L cannot have the EXTERNAL attribute",
1096 sym->name, &common_root->n.common->where);
1097
1098 if (sym->attr.intrinsic)
1099 gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
1100 sym->name, &common_root->n.common->where);
1101 else if (sym->attr.result
1102 || gfc_is_function_return_value (sym, gfc_current_ns))
1103 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1104 "that is also a function result", sym->name,
1105 &common_root->n.common->where);
1106 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1107 && sym->attr.proc != PROC_ST_FUNCTION)
1108 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1109 "that is also a global procedure", sym->name,
1110 &common_root->n.common->where);
1111 }
1112
1113
1114 /* Resolve contained function types. Because contained functions can call one
1115 another, they have to be worked out before any of the contained procedures
1116 can be resolved.
1117
1118 The good news is that if a function doesn't already have a type, the only
1119 way it can get one is through an IMPLICIT type or a RESULT variable, because
1120 by definition contained functions are contained namespace they're contained
1121 in, not in a sibling or parent namespace. */
1122
1123 static void
1124 resolve_contained_functions (gfc_namespace *ns)
1125 {
1126 gfc_namespace *child;
1127 gfc_entry_list *el;
1128
1129 resolve_formal_arglists (ns);
1130
1131 for (child = ns->contained; child; child = child->sibling)
1132 {
1133 /* Resolve alternate entry points first. */
1134 resolve_entries (child);
1135
1136 /* Then check function return types. */
1137 resolve_contained_fntype (child->proc_name, child);
1138 for (el = child->entries; el; el = el->next)
1139 resolve_contained_fntype (el->sym, child);
1140 }
1141 }
1142
1143
1144
1145 /* A Parameterized Derived Type constructor must contain values for
1146 the PDT KIND parameters or they must have a default initializer.
1147 Go through the constructor picking out the KIND expressions,
1148 storing them in 'param_list' and then call gfc_get_pdt_instance
1149 to obtain the PDT instance. */
1150
1151 static gfc_actual_arglist *param_list, *param_tail, *param;
1152
1153 static bool
1154 get_pdt_spec_expr (gfc_component *c, gfc_expr *expr)
1155 {
1156 param = gfc_get_actual_arglist ();
1157 if (!param_list)
1158 param_list = param_tail = param;
1159 else
1160 {
1161 param_tail->next = param;
1162 param_tail = param_tail->next;
1163 }
1164
1165 param_tail->name = c->name;
1166 if (expr)
1167 param_tail->expr = gfc_copy_expr (expr);
1168 else if (c->initializer)
1169 param_tail->expr = gfc_copy_expr (c->initializer);
1170 else
1171 {
1172 param_tail->spec_type = SPEC_ASSUMED;
1173 if (c->attr.pdt_kind)
1174 {
1175 gfc_error ("The KIND parameter %qs in the PDT constructor "
1176 "at %C has no value", param->name);
1177 return false;
1178 }
1179 }
1180
1181 return true;
1182 }
1183
1184 static bool
1185 get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr,
1186 gfc_symbol *derived)
1187 {
1188 gfc_constructor *cons = NULL;
1189 gfc_component *comp;
1190 bool t = true;
1191
1192 if (expr && expr->expr_type == EXPR_STRUCTURE)
1193 cons = gfc_constructor_first (expr->value.constructor);
1194 else if (constr)
1195 cons = *constr;
1196 gcc_assert (cons);
1197
1198 comp = derived->components;
1199
1200 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1201 {
1202 if (cons->expr
1203 && cons->expr->expr_type == EXPR_STRUCTURE
1204 && comp->ts.type == BT_DERIVED)
1205 {
1206 t = get_pdt_constructor (cons->expr, NULL, comp->ts.u.derived);
1207 if (!t)
1208 return t;
1209 }
1210 else if (comp->ts.type == BT_DERIVED)
1211 {
1212 t = get_pdt_constructor (NULL, &cons, comp->ts.u.derived);
1213 if (!t)
1214 return t;
1215 }
1216 else if ((comp->attr.pdt_kind || comp->attr.pdt_len)
1217 && derived->attr.pdt_template)
1218 {
1219 t = get_pdt_spec_expr (comp, cons->expr);
1220 if (!t)
1221 return t;
1222 }
1223 }
1224 return t;
1225 }
1226
1227
1228 static bool resolve_fl_derived0 (gfc_symbol *sym);
1229 static bool resolve_fl_struct (gfc_symbol *sym);
1230
1231
1232 /* Resolve all of the elements of a structure constructor and make sure that
1233 the types are correct. The 'init' flag indicates that the given
1234 constructor is an initializer. */
1235
1236 static bool
1237 resolve_structure_cons (gfc_expr *expr, int init)
1238 {
1239 gfc_constructor *cons;
1240 gfc_component *comp;
1241 bool t;
1242 symbol_attribute a;
1243
1244 t = true;
1245
1246 if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
1247 {
1248 if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
1249 resolve_fl_derived0 (expr->ts.u.derived);
1250 else
1251 resolve_fl_struct (expr->ts.u.derived);
1252
1253 /* If this is a Parameterized Derived Type template, find the
1254 instance corresponding to the PDT kind parameters. */
1255 if (expr->ts.u.derived->attr.pdt_template)
1256 {
1257 param_list = NULL;
1258 t = get_pdt_constructor (expr, NULL, expr->ts.u.derived);
1259 if (!t)
1260 return t;
1261 gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL);
1262
1263 expr->param_list = gfc_copy_actual_arglist (param_list);
1264
1265 if (param_list)
1266 gfc_free_actual_arglist (param_list);
1267
1268 if (!expr->ts.u.derived->attr.pdt_type)
1269 return false;
1270 }
1271 }
1272
1273 cons = gfc_constructor_first (expr->value.constructor);
1274
1275 /* A constructor may have references if it is the result of substituting a
1276 parameter variable. In this case we just pull out the component we
1277 want. */
1278 if (expr->ref)
1279 comp = expr->ref->u.c.sym->components;
1280 else
1281 comp = expr->ts.u.derived->components;
1282
1283 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1284 {
1285 int rank;
1286
1287 if (!cons->expr)
1288 continue;
1289
1290 /* Unions use an EXPR_NULL contrived expression to tell the translation
1291 phase to generate an initializer of the appropriate length.
1292 Ignore it here. */
1293 if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
1294 continue;
1295
1296 if (!gfc_resolve_expr (cons->expr))
1297 {
1298 t = false;
1299 continue;
1300 }
1301
1302 rank = comp->as ? comp->as->rank : 0;
1303 if (comp->ts.type == BT_CLASS
1304 && !comp->ts.u.derived->attr.unlimited_polymorphic
1305 && CLASS_DATA (comp)->as)
1306 rank = CLASS_DATA (comp)->as->rank;
1307
1308 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1309 && (comp->attr.allocatable || cons->expr->rank))
1310 {
1311 gfc_error ("The rank of the element in the structure "
1312 "constructor at %L does not match that of the "
1313 "component (%d/%d)", &cons->expr->where,
1314 cons->expr->rank, rank);
1315 t = false;
1316 }
1317
1318 /* If we don't have the right type, try to convert it. */
1319
1320 if (!comp->attr.proc_pointer &&
1321 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1322 {
1323 if (strcmp (comp->name, "_extends") == 0)
1324 {
1325 /* Can afford to be brutal with the _extends initializer.
1326 The derived type can get lost because it is PRIVATE
1327 but it is not usage constrained by the standard. */
1328 cons->expr->ts = comp->ts;
1329 }
1330 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1331 {
1332 gfc_error ("The element in the structure constructor at %L, "
1333 "for pointer component %qs, is %s but should be %s",
1334 &cons->expr->where, comp->name,
1335 gfc_basic_typename (cons->expr->ts.type),
1336 gfc_basic_typename (comp->ts.type));
1337 t = false;
1338 }
1339 else
1340 {
1341 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1342 if (t)
1343 t = t2;
1344 }
1345 }
1346
1347 /* For strings, the length of the constructor should be the same as
1348 the one of the structure, ensure this if the lengths are known at
1349 compile time and when we are dealing with PARAMETER or structure
1350 constructors. */
1351 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1352 && comp->ts.u.cl->length
1353 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1354 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1355 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1356 && cons->expr->rank != 0
1357 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1358 comp->ts.u.cl->length->value.integer) != 0)
1359 {
1360 if (cons->expr->expr_type == EXPR_VARIABLE
1361 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1362 {
1363 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1364 to make use of the gfc_resolve_character_array_constructor
1365 machinery. The expression is later simplified away to
1366 an array of string literals. */
1367 gfc_expr *para = cons->expr;
1368 cons->expr = gfc_get_expr ();
1369 cons->expr->ts = para->ts;
1370 cons->expr->where = para->where;
1371 cons->expr->expr_type = EXPR_ARRAY;
1372 cons->expr->rank = para->rank;
1373 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1374 gfc_constructor_append_expr (&cons->expr->value.constructor,
1375 para, &cons->expr->where);
1376 }
1377
1378 if (cons->expr->expr_type == EXPR_ARRAY)
1379 {
1380 /* Rely on the cleanup of the namespace to deal correctly with
1381 the old charlen. (There was a block here that attempted to
1382 remove the charlen but broke the chain in so doing.) */
1383 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1384 cons->expr->ts.u.cl->length_from_typespec = true;
1385 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1386 gfc_resolve_character_array_constructor (cons->expr);
1387 }
1388 }
1389
1390 if (cons->expr->expr_type == EXPR_NULL
1391 && !(comp->attr.pointer || comp->attr.allocatable
1392 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1393 || (comp->ts.type == BT_CLASS
1394 && (CLASS_DATA (comp)->attr.class_pointer
1395 || CLASS_DATA (comp)->attr.allocatable))))
1396 {
1397 t = false;
1398 gfc_error ("The NULL in the structure constructor at %L is "
1399 "being applied to component %qs, which is neither "
1400 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1401 comp->name);
1402 }
1403
1404 if (comp->attr.proc_pointer && comp->ts.interface)
1405 {
1406 /* Check procedure pointer interface. */
1407 gfc_symbol *s2 = NULL;
1408 gfc_component *c2;
1409 const char *name;
1410 char err[200];
1411
1412 c2 = gfc_get_proc_ptr_comp (cons->expr);
1413 if (c2)
1414 {
1415 s2 = c2->ts.interface;
1416 name = c2->name;
1417 }
1418 else if (cons->expr->expr_type == EXPR_FUNCTION)
1419 {
1420 s2 = cons->expr->symtree->n.sym->result;
1421 name = cons->expr->symtree->n.sym->result->name;
1422 }
1423 else if (cons->expr->expr_type != EXPR_NULL)
1424 {
1425 s2 = cons->expr->symtree->n.sym;
1426 name = cons->expr->symtree->n.sym->name;
1427 }
1428
1429 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1430 err, sizeof (err), NULL, NULL))
1431 {
1432 gfc_error_opt (0, "Interface mismatch for procedure-pointer "
1433 "component %qs in structure constructor at %L:"
1434 " %s", comp->name, &cons->expr->where, err);
1435 return false;
1436 }
1437 }
1438
1439 if (!comp->attr.pointer || comp->attr.proc_pointer
1440 || cons->expr->expr_type == EXPR_NULL)
1441 continue;
1442
1443 a = gfc_expr_attr (cons->expr);
1444
1445 if (!a.pointer && !a.target)
1446 {
1447 t = false;
1448 gfc_error ("The element in the structure constructor at %L, "
1449 "for pointer component %qs should be a POINTER or "
1450 "a TARGET", &cons->expr->where, comp->name);
1451 }
1452
1453 if (init)
1454 {
1455 /* F08:C461. Additional checks for pointer initialization. */
1456 if (a.allocatable)
1457 {
1458 t = false;
1459 gfc_error ("Pointer initialization target at %L "
1460 "must not be ALLOCATABLE", &cons->expr->where);
1461 }
1462 if (!a.save)
1463 {
1464 t = false;
1465 gfc_error ("Pointer initialization target at %L "
1466 "must have the SAVE attribute", &cons->expr->where);
1467 }
1468 }
1469
1470 /* F2003, C1272 (3). */
1471 bool impure = cons->expr->expr_type == EXPR_VARIABLE
1472 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1473 || gfc_is_coindexed (cons->expr));
1474 if (impure && gfc_pure (NULL))
1475 {
1476 t = false;
1477 gfc_error ("Invalid expression in the structure constructor for "
1478 "pointer component %qs at %L in PURE procedure",
1479 comp->name, &cons->expr->where);
1480 }
1481
1482 if (impure)
1483 gfc_unset_implicit_pure (NULL);
1484 }
1485
1486 return t;
1487 }
1488
1489
1490 /****************** Expression name resolution ******************/
1491
1492 /* Returns 0 if a symbol was not declared with a type or
1493 attribute declaration statement, nonzero otherwise. */
1494
1495 static int
1496 was_declared (gfc_symbol *sym)
1497 {
1498 symbol_attribute a;
1499
1500 a = sym->attr;
1501
1502 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1503 return 1;
1504
1505 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1506 || a.optional || a.pointer || a.save || a.target || a.volatile_
1507 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1508 || a.asynchronous || a.codimension)
1509 return 1;
1510
1511 return 0;
1512 }
1513
1514
1515 /* Determine if a symbol is generic or not. */
1516
1517 static int
1518 generic_sym (gfc_symbol *sym)
1519 {
1520 gfc_symbol *s;
1521
1522 if (sym->attr.generic ||
1523 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1524 return 1;
1525
1526 if (was_declared (sym) || sym->ns->parent == NULL)
1527 return 0;
1528
1529 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1530
1531 if (s != NULL)
1532 {
1533 if (s == sym)
1534 return 0;
1535 else
1536 return generic_sym (s);
1537 }
1538
1539 return 0;
1540 }
1541
1542
1543 /* Determine if a symbol is specific or not. */
1544
1545 static int
1546 specific_sym (gfc_symbol *sym)
1547 {
1548 gfc_symbol *s;
1549
1550 if (sym->attr.if_source == IFSRC_IFBODY
1551 || sym->attr.proc == PROC_MODULE
1552 || sym->attr.proc == PROC_INTERNAL
1553 || sym->attr.proc == PROC_ST_FUNCTION
1554 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1555 || sym->attr.external)
1556 return 1;
1557
1558 if (was_declared (sym) || sym->ns->parent == NULL)
1559 return 0;
1560
1561 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1562
1563 return (s == NULL) ? 0 : specific_sym (s);
1564 }
1565
1566
1567 /* Figure out if the procedure is specific, generic or unknown. */
1568
1569 enum proc_type
1570 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
1571
1572 static proc_type
1573 procedure_kind (gfc_symbol *sym)
1574 {
1575 if (generic_sym (sym))
1576 return PTYPE_GENERIC;
1577
1578 if (specific_sym (sym))
1579 return PTYPE_SPECIFIC;
1580
1581 return PTYPE_UNKNOWN;
1582 }
1583
1584 /* Check references to assumed size arrays. The flag need_full_assumed_size
1585 is nonzero when matching actual arguments. */
1586
1587 static int need_full_assumed_size = 0;
1588
1589 static bool
1590 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1591 {
1592 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1593 return false;
1594
1595 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1596 What should it be? */
1597 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1598 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1599 && (e->ref->u.ar.type == AR_FULL))
1600 {
1601 gfc_error ("The upper bound in the last dimension must "
1602 "appear in the reference to the assumed size "
1603 "array %qs at %L", sym->name, &e->where);
1604 return true;
1605 }
1606 return false;
1607 }
1608
1609
1610 /* Look for bad assumed size array references in argument expressions
1611 of elemental and array valued intrinsic procedures. Since this is
1612 called from procedure resolution functions, it only recurses at
1613 operators. */
1614
1615 static bool
1616 resolve_assumed_size_actual (gfc_expr *e)
1617 {
1618 if (e == NULL)
1619 return false;
1620
1621 switch (e->expr_type)
1622 {
1623 case EXPR_VARIABLE:
1624 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1625 return true;
1626 break;
1627
1628 case EXPR_OP:
1629 if (resolve_assumed_size_actual (e->value.op.op1)
1630 || resolve_assumed_size_actual (e->value.op.op2))
1631 return true;
1632 break;
1633
1634 default:
1635 break;
1636 }
1637 return false;
1638 }
1639
1640
1641 /* Check a generic procedure, passed as an actual argument, to see if
1642 there is a matching specific name. If none, it is an error, and if
1643 more than one, the reference is ambiguous. */
1644 static int
1645 count_specific_procs (gfc_expr *e)
1646 {
1647 int n;
1648 gfc_interface *p;
1649 gfc_symbol *sym;
1650
1651 n = 0;
1652 sym = e->symtree->n.sym;
1653
1654 for (p = sym->generic; p; p = p->next)
1655 if (strcmp (sym->name, p->sym->name) == 0)
1656 {
1657 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1658 sym->name);
1659 n++;
1660 }
1661
1662 if (n > 1)
1663 gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
1664 &e->where);
1665
1666 if (n == 0)
1667 gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1668 "argument at %L", sym->name, &e->where);
1669
1670 return n;
1671 }
1672
1673
1674 /* See if a call to sym could possibly be a not allowed RECURSION because of
1675 a missing RECURSIVE declaration. This means that either sym is the current
1676 context itself, or sym is the parent of a contained procedure calling its
1677 non-RECURSIVE containing procedure.
1678 This also works if sym is an ENTRY. */
1679
1680 static bool
1681 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1682 {
1683 gfc_symbol* proc_sym;
1684 gfc_symbol* context_proc;
1685 gfc_namespace* real_context;
1686
1687 if (sym->attr.flavor == FL_PROGRAM
1688 || gfc_fl_struct (sym->attr.flavor))
1689 return false;
1690
1691 /* If we've got an ENTRY, find real procedure. */
1692 if (sym->attr.entry && sym->ns->entries)
1693 proc_sym = sym->ns->entries->sym;
1694 else
1695 proc_sym = sym;
1696
1697 /* If sym is RECURSIVE, all is well of course. */
1698 if (proc_sym->attr.recursive || flag_recursive)
1699 return false;
1700
1701 /* Find the context procedure's "real" symbol if it has entries.
1702 We look for a procedure symbol, so recurse on the parents if we don't
1703 find one (like in case of a BLOCK construct). */
1704 for (real_context = context; ; real_context = real_context->parent)
1705 {
1706 /* We should find something, eventually! */
1707 gcc_assert (real_context);
1708
1709 context_proc = (real_context->entries ? real_context->entries->sym
1710 : real_context->proc_name);
1711
1712 /* In some special cases, there may not be a proc_name, like for this
1713 invalid code:
1714 real(bad_kind()) function foo () ...
1715 when checking the call to bad_kind ().
1716 In these cases, we simply return here and assume that the
1717 call is ok. */
1718 if (!context_proc)
1719 return false;
1720
1721 if (context_proc->attr.flavor != FL_LABEL)
1722 break;
1723 }
1724
1725 /* A call from sym's body to itself is recursion, of course. */
1726 if (context_proc == proc_sym)
1727 return true;
1728
1729 /* The same is true if context is a contained procedure and sym the
1730 containing one. */
1731 if (context_proc->attr.contained)
1732 {
1733 gfc_symbol* parent_proc;
1734
1735 gcc_assert (context->parent);
1736 parent_proc = (context->parent->entries ? context->parent->entries->sym
1737 : context->parent->proc_name);
1738
1739 if (parent_proc == proc_sym)
1740 return true;
1741 }
1742
1743 return false;
1744 }
1745
1746
1747 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1748 its typespec and formal argument list. */
1749
1750 bool
1751 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1752 {
1753 gfc_intrinsic_sym* isym = NULL;
1754 const char* symstd;
1755
1756 if (sym->resolve_symbol_called >= 2)
1757 return true;
1758
1759 sym->resolve_symbol_called = 2;
1760
1761 /* Already resolved. */
1762 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1763 return true;
1764
1765 /* We already know this one is an intrinsic, so we don't call
1766 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1767 gfc_find_subroutine directly to check whether it is a function or
1768 subroutine. */
1769
1770 if (sym->intmod_sym_id && sym->attr.subroutine)
1771 {
1772 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1773 isym = gfc_intrinsic_subroutine_by_id (id);
1774 }
1775 else if (sym->intmod_sym_id)
1776 {
1777 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1778 isym = gfc_intrinsic_function_by_id (id);
1779 }
1780 else if (!sym->attr.subroutine)
1781 isym = gfc_find_function (sym->name);
1782
1783 if (isym && !sym->attr.subroutine)
1784 {
1785 if (sym->ts.type != BT_UNKNOWN && warn_surprising
1786 && !sym->attr.implicit_type)
1787 gfc_warning (OPT_Wsurprising,
1788 "Type specified for intrinsic function %qs at %L is"
1789 " ignored", sym->name, &sym->declared_at);
1790
1791 if (!sym->attr.function &&
1792 !gfc_add_function(&sym->attr, sym->name, loc))
1793 return false;
1794
1795 sym->ts = isym->ts;
1796 }
1797 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1798 {
1799 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1800 {
1801 gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1802 " specifier", sym->name, &sym->declared_at);
1803 return false;
1804 }
1805
1806 if (!sym->attr.subroutine &&
1807 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1808 return false;
1809 }
1810 else
1811 {
1812 gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
1813 &sym->declared_at);
1814 return false;
1815 }
1816
1817 gfc_copy_formal_args_intr (sym, isym, NULL);
1818
1819 sym->attr.pure = isym->pure;
1820 sym->attr.elemental = isym->elemental;
1821
1822 /* Check it is actually available in the standard settings. */
1823 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1824 {
1825 gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1826 "available in the current standard settings but %s. Use "
1827 "an appropriate %<-std=*%> option or enable "
1828 "%<-fall-intrinsics%> in order to use it.",
1829 sym->name, &sym->declared_at, symstd);
1830 return false;
1831 }
1832
1833 return true;
1834 }
1835
1836
1837 /* Resolve a procedure expression, like passing it to a called procedure or as
1838 RHS for a procedure pointer assignment. */
1839
1840 static bool
1841 resolve_procedure_expression (gfc_expr* expr)
1842 {
1843 gfc_symbol* sym;
1844
1845 if (expr->expr_type != EXPR_VARIABLE)
1846 return true;
1847 gcc_assert (expr->symtree);
1848
1849 sym = expr->symtree->n.sym;
1850
1851 if (sym->attr.intrinsic)
1852 gfc_resolve_intrinsic (sym, &expr->where);
1853
1854 if (sym->attr.flavor != FL_PROCEDURE
1855 || (sym->attr.function && sym->result == sym))
1856 return true;
1857
1858 /* A non-RECURSIVE procedure that is used as procedure expression within its
1859 own body is in danger of being called recursively. */
1860 if (is_illegal_recursion (sym, gfc_current_ns))
1861 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
1862 " itself recursively. Declare it RECURSIVE or use"
1863 " %<-frecursive%>", sym->name, &expr->where);
1864
1865 return true;
1866 }
1867
1868
1869 /* Check that name is not a derived type. */
1870
1871 static bool
1872 is_dt_name (const char *name)
1873 {
1874 gfc_symbol *dt_list, *dt_first;
1875
1876 dt_list = dt_first = gfc_derived_types;
1877 for (; dt_list; dt_list = dt_list->dt_next)
1878 {
1879 if (strcmp(dt_list->name, name) == 0)
1880 return true;
1881 if (dt_first == dt_list->dt_next)
1882 break;
1883 }
1884 return false;
1885 }
1886
1887
1888 /* Resolve an actual argument list. Most of the time, this is just
1889 resolving the expressions in the list.
1890 The exception is that we sometimes have to decide whether arguments
1891 that look like procedure arguments are really simple variable
1892 references. */
1893
1894 static bool
1895 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1896 bool no_formal_args)
1897 {
1898 gfc_symbol *sym;
1899 gfc_symtree *parent_st;
1900 gfc_expr *e;
1901 gfc_component *comp;
1902 int save_need_full_assumed_size;
1903 bool return_value = false;
1904 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1905
1906 actual_arg = true;
1907 first_actual_arg = true;
1908
1909 for (; arg; arg = arg->next)
1910 {
1911 e = arg->expr;
1912 if (e == NULL)
1913 {
1914 /* Check the label is a valid branching target. */
1915 if (arg->label)
1916 {
1917 if (arg->label->defined == ST_LABEL_UNKNOWN)
1918 {
1919 gfc_error ("Label %d referenced at %L is never defined",
1920 arg->label->value, &arg->label->where);
1921 goto cleanup;
1922 }
1923 }
1924 first_actual_arg = false;
1925 continue;
1926 }
1927
1928 if (e->expr_type == EXPR_VARIABLE
1929 && e->symtree->n.sym->attr.generic
1930 && no_formal_args
1931 && count_specific_procs (e) != 1)
1932 goto cleanup;
1933
1934 if (e->ts.type != BT_PROCEDURE)
1935 {
1936 save_need_full_assumed_size = need_full_assumed_size;
1937 if (e->expr_type != EXPR_VARIABLE)
1938 need_full_assumed_size = 0;
1939 if (!gfc_resolve_expr (e))
1940 goto cleanup;
1941 need_full_assumed_size = save_need_full_assumed_size;
1942 goto argument_list;
1943 }
1944
1945 /* See if the expression node should really be a variable reference. */
1946
1947 sym = e->symtree->n.sym;
1948
1949 if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name))
1950 {
1951 gfc_error ("Derived type %qs is used as an actual "
1952 "argument at %L", sym->name, &e->where);
1953 goto cleanup;
1954 }
1955
1956 if (sym->attr.flavor == FL_PROCEDURE
1957 || sym->attr.intrinsic
1958 || sym->attr.external)
1959 {
1960 int actual_ok;
1961
1962 /* If a procedure is not already determined to be something else
1963 check if it is intrinsic. */
1964 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1965 sym->attr.intrinsic = 1;
1966
1967 if (sym->attr.proc == PROC_ST_FUNCTION)
1968 {
1969 gfc_error ("Statement function %qs at %L is not allowed as an "
1970 "actual argument", sym->name, &e->where);
1971 }
1972
1973 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1974 sym->attr.subroutine);
1975 if (sym->attr.intrinsic && actual_ok == 0)
1976 {
1977 gfc_error ("Intrinsic %qs at %L is not allowed as an "
1978 "actual argument", sym->name, &e->where);
1979 }
1980
1981 if (sym->attr.contained && !sym->attr.use_assoc
1982 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1983 {
1984 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
1985 " used as actual argument at %L",
1986 sym->name, &e->where))
1987 goto cleanup;
1988 }
1989
1990 if (sym->attr.elemental && !sym->attr.intrinsic)
1991 {
1992 gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
1993 "allowed as an actual argument at %L", sym->name,
1994 &e->where);
1995 }
1996
1997 /* Check if a generic interface has a specific procedure
1998 with the same name before emitting an error. */
1999 if (sym->attr.generic && count_specific_procs (e) != 1)
2000 goto cleanup;
2001
2002 /* Just in case a specific was found for the expression. */
2003 sym = e->symtree->n.sym;
2004
2005 /* If the symbol is the function that names the current (or
2006 parent) scope, then we really have a variable reference. */
2007
2008 if (gfc_is_function_return_value (sym, sym->ns))
2009 goto got_variable;
2010
2011 /* If all else fails, see if we have a specific intrinsic. */
2012 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
2013 {
2014 gfc_intrinsic_sym *isym;
2015
2016 isym = gfc_find_function (sym->name);
2017 if (isym == NULL || !isym->specific)
2018 {
2019 gfc_error ("Unable to find a specific INTRINSIC procedure "
2020 "for the reference %qs at %L", sym->name,
2021 &e->where);
2022 goto cleanup;
2023 }
2024 sym->ts = isym->ts;
2025 sym->attr.intrinsic = 1;
2026 sym->attr.function = 1;
2027 }
2028
2029 if (!gfc_resolve_expr (e))
2030 goto cleanup;
2031 goto argument_list;
2032 }
2033
2034 /* See if the name is a module procedure in a parent unit. */
2035
2036 if (was_declared (sym) || sym->ns->parent == NULL)
2037 goto got_variable;
2038
2039 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
2040 {
2041 gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
2042 goto cleanup;
2043 }
2044
2045 if (parent_st == NULL)
2046 goto got_variable;
2047
2048 sym = parent_st->n.sym;
2049 e->symtree = parent_st; /* Point to the right thing. */
2050
2051 if (sym->attr.flavor == FL_PROCEDURE
2052 || sym->attr.intrinsic
2053 || sym->attr.external)
2054 {
2055 if (!gfc_resolve_expr (e))
2056 goto cleanup;
2057 goto argument_list;
2058 }
2059
2060 got_variable:
2061 e->expr_type = EXPR_VARIABLE;
2062 e->ts = sym->ts;
2063 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
2064 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2065 && CLASS_DATA (sym)->as))
2066 {
2067 e->rank = sym->ts.type == BT_CLASS
2068 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
2069 e->ref = gfc_get_ref ();
2070 e->ref->type = REF_ARRAY;
2071 e->ref->u.ar.type = AR_FULL;
2072 e->ref->u.ar.as = sym->ts.type == BT_CLASS
2073 ? CLASS_DATA (sym)->as : sym->as;
2074 }
2075
2076 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
2077 primary.c (match_actual_arg). If above code determines that it
2078 is a variable instead, it needs to be resolved as it was not
2079 done at the beginning of this function. */
2080 save_need_full_assumed_size = need_full_assumed_size;
2081 if (e->expr_type != EXPR_VARIABLE)
2082 need_full_assumed_size = 0;
2083 if (!gfc_resolve_expr (e))
2084 goto cleanup;
2085 need_full_assumed_size = save_need_full_assumed_size;
2086
2087 argument_list:
2088 /* Check argument list functions %VAL, %LOC and %REF. There is
2089 nothing to do for %REF. */
2090 if (arg->name && arg->name[0] == '%')
2091 {
2092 if (strcmp ("%VAL", arg->name) == 0)
2093 {
2094 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
2095 {
2096 gfc_error ("By-value argument at %L is not of numeric "
2097 "type", &e->where);
2098 goto cleanup;
2099 }
2100
2101 if (e->rank)
2102 {
2103 gfc_error ("By-value argument at %L cannot be an array or "
2104 "an array section", &e->where);
2105 goto cleanup;
2106 }
2107
2108 /* Intrinsics are still PROC_UNKNOWN here. However,
2109 since same file external procedures are not resolvable
2110 in gfortran, it is a good deal easier to leave them to
2111 intrinsic.c. */
2112 if (ptype != PROC_UNKNOWN
2113 && ptype != PROC_DUMMY
2114 && ptype != PROC_EXTERNAL
2115 && ptype != PROC_MODULE)
2116 {
2117 gfc_error ("By-value argument at %L is not allowed "
2118 "in this context", &e->where);
2119 goto cleanup;
2120 }
2121 }
2122
2123 /* Statement functions have already been excluded above. */
2124 else if (strcmp ("%LOC", arg->name) == 0
2125 && e->ts.type == BT_PROCEDURE)
2126 {
2127 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
2128 {
2129 gfc_error ("Passing internal procedure at %L by location "
2130 "not allowed", &e->where);
2131 goto cleanup;
2132 }
2133 }
2134 }
2135
2136 comp = gfc_get_proc_ptr_comp(e);
2137 if (e->expr_type == EXPR_VARIABLE
2138 && comp && comp->attr.elemental)
2139 {
2140 gfc_error ("ELEMENTAL procedure pointer component %qs is not "
2141 "allowed as an actual argument at %L", comp->name,
2142 &e->where);
2143 }
2144
2145 /* Fortran 2008, C1237. */
2146 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
2147 && gfc_has_ultimate_pointer (e))
2148 {
2149 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2150 "component", &e->where);
2151 goto cleanup;
2152 }
2153
2154 first_actual_arg = false;
2155 }
2156
2157 return_value = true;
2158
2159 cleanup:
2160 actual_arg = actual_arg_sav;
2161 first_actual_arg = first_actual_arg_sav;
2162
2163 return return_value;
2164 }
2165
2166
2167 /* Do the checks of the actual argument list that are specific to elemental
2168 procedures. If called with c == NULL, we have a function, otherwise if
2169 expr == NULL, we have a subroutine. */
2170
2171 static bool
2172 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2173 {
2174 gfc_actual_arglist *arg0;
2175 gfc_actual_arglist *arg;
2176 gfc_symbol *esym = NULL;
2177 gfc_intrinsic_sym *isym = NULL;
2178 gfc_expr *e = NULL;
2179 gfc_intrinsic_arg *iformal = NULL;
2180 gfc_formal_arglist *eformal = NULL;
2181 bool formal_optional = false;
2182 bool set_by_optional = false;
2183 int i;
2184 int rank = 0;
2185
2186 /* Is this an elemental procedure? */
2187 if (expr && expr->value.function.actual != NULL)
2188 {
2189 if (expr->value.function.esym != NULL
2190 && expr->value.function.esym->attr.elemental)
2191 {
2192 arg0 = expr->value.function.actual;
2193 esym = expr->value.function.esym;
2194 }
2195 else if (expr->value.function.isym != NULL
2196 && expr->value.function.isym->elemental)
2197 {
2198 arg0 = expr->value.function.actual;
2199 isym = expr->value.function.isym;
2200 }
2201 else
2202 return true;
2203 }
2204 else if (c && c->ext.actual != NULL)
2205 {
2206 arg0 = c->ext.actual;
2207
2208 if (c->resolved_sym)
2209 esym = c->resolved_sym;
2210 else
2211 esym = c->symtree->n.sym;
2212 gcc_assert (esym);
2213
2214 if (!esym->attr.elemental)
2215 return true;
2216 }
2217 else
2218 return true;
2219
2220 /* The rank of an elemental is the rank of its array argument(s). */
2221 for (arg = arg0; arg; arg = arg->next)
2222 {
2223 if (arg->expr != NULL && arg->expr->rank != 0)
2224 {
2225 rank = arg->expr->rank;
2226 if (arg->expr->expr_type == EXPR_VARIABLE
2227 && arg->expr->symtree->n.sym->attr.optional)
2228 set_by_optional = true;
2229
2230 /* Function specific; set the result rank and shape. */
2231 if (expr)
2232 {
2233 expr->rank = rank;
2234 if (!expr->shape && arg->expr->shape)
2235 {
2236 expr->shape = gfc_get_shape (rank);
2237 for (i = 0; i < rank; i++)
2238 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2239 }
2240 }
2241 break;
2242 }
2243 }
2244
2245 /* If it is an array, it shall not be supplied as an actual argument
2246 to an elemental procedure unless an array of the same rank is supplied
2247 as an actual argument corresponding to a nonoptional dummy argument of
2248 that elemental procedure(12.4.1.5). */
2249 formal_optional = false;
2250 if (isym)
2251 iformal = isym->formal;
2252 else
2253 eformal = esym->formal;
2254
2255 for (arg = arg0; arg; arg = arg->next)
2256 {
2257 if (eformal)
2258 {
2259 if (eformal->sym && eformal->sym->attr.optional)
2260 formal_optional = true;
2261 eformal = eformal->next;
2262 }
2263 else if (isym && iformal)
2264 {
2265 if (iformal->optional)
2266 formal_optional = true;
2267 iformal = iformal->next;
2268 }
2269 else if (isym)
2270 formal_optional = true;
2271
2272 if (pedantic && arg->expr != NULL
2273 && arg->expr->expr_type == EXPR_VARIABLE
2274 && arg->expr->symtree->n.sym->attr.optional
2275 && formal_optional
2276 && arg->expr->rank
2277 && (set_by_optional || arg->expr->rank != rank)
2278 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2279 {
2280 bool t = false;
2281 gfc_actual_arglist *a;
2282
2283 /* Scan the argument list for a non-optional argument with the
2284 same rank as arg. */
2285 for (a = arg0; a; a = a->next)
2286 if (a != arg
2287 && a->expr->rank == arg->expr->rank
2288 && !a->expr->symtree->n.sym->attr.optional)
2289 {
2290 t = true;
2291 break;
2292 }
2293
2294 if (!t)
2295 gfc_warning (OPT_Wpedantic,
2296 "%qs at %L is an array and OPTIONAL; If it is not "
2297 "present, then it cannot be the actual argument of "
2298 "an ELEMENTAL procedure unless there is a non-optional"
2299 " argument with the same rank "
2300 "(Fortran 2018, 15.5.2.12)",
2301 arg->expr->symtree->n.sym->name, &arg->expr->where);
2302 }
2303 }
2304
2305 for (arg = arg0; arg; arg = arg->next)
2306 {
2307 if (arg->expr == NULL || arg->expr->rank == 0)
2308 continue;
2309
2310 /* Being elemental, the last upper bound of an assumed size array
2311 argument must be present. */
2312 if (resolve_assumed_size_actual (arg->expr))
2313 return false;
2314
2315 /* Elemental procedure's array actual arguments must conform. */
2316 if (e != NULL)
2317 {
2318 if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
2319 return false;
2320 }
2321 else
2322 e = arg->expr;
2323 }
2324
2325 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2326 is an array, the intent inout/out variable needs to be also an array. */
2327 if (rank > 0 && esym && expr == NULL)
2328 for (eformal = esym->formal, arg = arg0; arg && eformal;
2329 arg = arg->next, eformal = eformal->next)
2330 if ((eformal->sym->attr.intent == INTENT_OUT
2331 || eformal->sym->attr.intent == INTENT_INOUT)
2332 && arg->expr && arg->expr->rank == 0)
2333 {
2334 gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2335 "ELEMENTAL subroutine %qs is a scalar, but another "
2336 "actual argument is an array", &arg->expr->where,
2337 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2338 : "INOUT", eformal->sym->name, esym->name);
2339 return false;
2340 }
2341 return true;
2342 }
2343
2344
2345 /* This function does the checking of references to global procedures
2346 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2347 77 and 95 standards. It checks for a gsymbol for the name, making
2348 one if it does not already exist. If it already exists, then the
2349 reference being resolved must correspond to the type of gsymbol.
2350 Otherwise, the new symbol is equipped with the attributes of the
2351 reference. The corresponding code that is called in creating
2352 global entities is parse.c.
2353
2354 In addition, for all but -std=legacy, the gsymbols are used to
2355 check the interfaces of external procedures from the same file.
2356 The namespace of the gsymbol is resolved and then, once this is
2357 done the interface is checked. */
2358
2359
2360 static bool
2361 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2362 {
2363 if (!gsym_ns->proc_name->attr.recursive)
2364 return true;
2365
2366 if (sym->ns == gsym_ns)
2367 return false;
2368
2369 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2370 return false;
2371
2372 return true;
2373 }
2374
2375 static bool
2376 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2377 {
2378 if (gsym_ns->entries)
2379 {
2380 gfc_entry_list *entry = gsym_ns->entries;
2381
2382 for (; entry; entry = entry->next)
2383 {
2384 if (strcmp (sym->name, entry->sym->name) == 0)
2385 {
2386 if (strcmp (gsym_ns->proc_name->name,
2387 sym->ns->proc_name->name) == 0)
2388 return false;
2389
2390 if (sym->ns->parent
2391 && strcmp (gsym_ns->proc_name->name,
2392 sym->ns->parent->proc_name->name) == 0)
2393 return false;
2394 }
2395 }
2396 }
2397 return true;
2398 }
2399
2400
2401 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2402
2403 bool
2404 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2405 {
2406 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2407
2408 for ( ; arg; arg = arg->next)
2409 {
2410 if (!arg->sym)
2411 continue;
2412
2413 if (arg->sym->attr.allocatable) /* (2a) */
2414 {
2415 strncpy (errmsg, _("allocatable argument"), err_len);
2416 return true;
2417 }
2418 else if (arg->sym->attr.asynchronous)
2419 {
2420 strncpy (errmsg, _("asynchronous argument"), err_len);
2421 return true;
2422 }
2423 else if (arg->sym->attr.optional)
2424 {
2425 strncpy (errmsg, _("optional argument"), err_len);
2426 return true;
2427 }
2428 else if (arg->sym->attr.pointer)
2429 {
2430 strncpy (errmsg, _("pointer argument"), err_len);
2431 return true;
2432 }
2433 else if (arg->sym->attr.target)
2434 {
2435 strncpy (errmsg, _("target argument"), err_len);
2436 return true;
2437 }
2438 else if (arg->sym->attr.value)
2439 {
2440 strncpy (errmsg, _("value argument"), err_len);
2441 return true;
2442 }
2443 else if (arg->sym->attr.volatile_)
2444 {
2445 strncpy (errmsg, _("volatile argument"), err_len);
2446 return true;
2447 }
2448 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2449 {
2450 strncpy (errmsg, _("assumed-shape argument"), err_len);
2451 return true;
2452 }
2453 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2454 {
2455 strncpy (errmsg, _("assumed-rank argument"), err_len);
2456 return true;
2457 }
2458 else if (arg->sym->attr.codimension) /* (2c) */
2459 {
2460 strncpy (errmsg, _("coarray argument"), err_len);
2461 return true;
2462 }
2463 else if (false) /* (2d) TODO: parametrized derived type */
2464 {
2465 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2466 return true;
2467 }
2468 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2469 {
2470 strncpy (errmsg, _("polymorphic argument"), err_len);
2471 return true;
2472 }
2473 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2474 {
2475 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2476 return true;
2477 }
2478 else if (arg->sym->ts.type == BT_ASSUMED)
2479 {
2480 /* As assumed-type is unlimited polymorphic (cf. above).
2481 See also TS 29113, Note 6.1. */
2482 strncpy (errmsg, _("assumed-type argument"), err_len);
2483 return true;
2484 }
2485 }
2486
2487 if (sym->attr.function)
2488 {
2489 gfc_symbol *res = sym->result ? sym->result : sym;
2490
2491 if (res->attr.dimension) /* (3a) */
2492 {
2493 strncpy (errmsg, _("array result"), err_len);
2494 return true;
2495 }
2496 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2497 {
2498 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2499 return true;
2500 }
2501 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2502 && res->ts.u.cl->length
2503 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2504 {
2505 strncpy (errmsg, _("result with non-constant character length"), err_len);
2506 return true;
2507 }
2508 }
2509
2510 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2511 {
2512 strncpy (errmsg, _("elemental procedure"), err_len);
2513 return true;
2514 }
2515 else if (sym->attr.is_bind_c) /* (5) */
2516 {
2517 strncpy (errmsg, _("bind(c) procedure"), err_len);
2518 return true;
2519 }
2520
2521 return false;
2522 }
2523
2524
2525 static void
2526 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
2527 {
2528 gfc_gsymbol * gsym;
2529 gfc_namespace *ns;
2530 enum gfc_symbol_type type;
2531 char reason[200];
2532
2533 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2534
2535 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
2536 sym->binding_label != NULL);
2537
2538 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2539 gfc_global_used (gsym, where);
2540
2541 if ((sym->attr.if_source == IFSRC_UNKNOWN
2542 || sym->attr.if_source == IFSRC_IFBODY)
2543 && gsym->type != GSYM_UNKNOWN
2544 && !gsym->binding_label
2545 && gsym->ns
2546 && gsym->ns->proc_name
2547 && not_in_recursive (sym, gsym->ns)
2548 && not_entry_self_reference (sym, gsym->ns))
2549 {
2550 gfc_symbol *def_sym;
2551 def_sym = gsym->ns->proc_name;
2552
2553 if (gsym->ns->resolved != -1)
2554 {
2555
2556 /* Resolve the gsymbol namespace if needed. */
2557 if (!gsym->ns->resolved)
2558 {
2559 gfc_symbol *old_dt_list;
2560
2561 /* Stash away derived types so that the backend_decls
2562 do not get mixed up. */
2563 old_dt_list = gfc_derived_types;
2564 gfc_derived_types = NULL;
2565
2566 gfc_resolve (gsym->ns);
2567
2568 /* Store the new derived types with the global namespace. */
2569 if (gfc_derived_types)
2570 gsym->ns->derived_types = gfc_derived_types;
2571
2572 /* Restore the derived types of this namespace. */
2573 gfc_derived_types = old_dt_list;
2574 }
2575
2576 /* Make sure that translation for the gsymbol occurs before
2577 the procedure currently being resolved. */
2578 ns = gfc_global_ns_list;
2579 for (; ns && ns != gsym->ns; ns = ns->sibling)
2580 {
2581 if (ns->sibling == gsym->ns)
2582 {
2583 ns->sibling = gsym->ns->sibling;
2584 gsym->ns->sibling = gfc_global_ns_list;
2585 gfc_global_ns_list = gsym->ns;
2586 break;
2587 }
2588 }
2589
2590 /* This can happen if a binding name has been specified. */
2591 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2592 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2593
2594 if (def_sym->attr.entry_master || def_sym->attr.entry)
2595 {
2596 gfc_entry_list *entry;
2597 for (entry = gsym->ns->entries; entry; entry = entry->next)
2598 if (strcmp (entry->sym->name, sym->name) == 0)
2599 {
2600 def_sym = entry->sym;
2601 break;
2602 }
2603 }
2604 }
2605
2606 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2607 {
2608 gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2609 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2610 gfc_typename (&def_sym->ts));
2611 goto done;
2612 }
2613
2614 if (sym->attr.if_source == IFSRC_UNKNOWN
2615 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2616 {
2617 gfc_error ("Explicit interface required for %qs at %L: %s",
2618 sym->name, &sym->declared_at, reason);
2619 goto done;
2620 }
2621
2622 bool bad_result_characteristics;
2623 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2624 reason, sizeof(reason), NULL, NULL,
2625 &bad_result_characteristics))
2626 {
2627 /* Turn erros into warnings with -std=gnu and -std=legacy,
2628 unless a function returns a wrong type, which can lead
2629 to all kinds of ICEs and wrong code. */
2630
2631 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
2632 && !bad_result_characteristics)
2633 gfc_errors_to_warnings (true);
2634
2635 gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
2636 sym->name, &sym->declared_at, reason);
2637 sym->error = 1;
2638 gfc_errors_to_warnings (false);
2639 goto done;
2640 }
2641 }
2642
2643 done:
2644
2645 if (gsym->type == GSYM_UNKNOWN)
2646 {
2647 gsym->type = type;
2648 gsym->where = *where;
2649 }
2650
2651 gsym->used = 1;
2652 }
2653
2654
2655 /************* Function resolution *************/
2656
2657 /* Resolve a function call known to be generic.
2658 Section 14.1.2.4.1. */
2659
2660 static match
2661 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2662 {
2663 gfc_symbol *s;
2664
2665 if (sym->attr.generic)
2666 {
2667 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2668 if (s != NULL)
2669 {
2670 expr->value.function.name = s->name;
2671 expr->value.function.esym = s;
2672
2673 if (s->ts.type != BT_UNKNOWN)
2674 expr->ts = s->ts;
2675 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2676 expr->ts = s->result->ts;
2677
2678 if (s->as != NULL)
2679 expr->rank = s->as->rank;
2680 else if (s->result != NULL && s->result->as != NULL)
2681 expr->rank = s->result->as->rank;
2682
2683 gfc_set_sym_referenced (expr->value.function.esym);
2684
2685 return MATCH_YES;
2686 }
2687
2688 /* TODO: Need to search for elemental references in generic
2689 interface. */
2690 }
2691
2692 if (sym->attr.intrinsic)
2693 return gfc_intrinsic_func_interface (expr, 0);
2694
2695 return MATCH_NO;
2696 }
2697
2698
2699 static bool
2700 resolve_generic_f (gfc_expr *expr)
2701 {
2702 gfc_symbol *sym;
2703 match m;
2704 gfc_interface *intr = NULL;
2705
2706 sym = expr->symtree->n.sym;
2707
2708 for (;;)
2709 {
2710 m = resolve_generic_f0 (expr, sym);
2711 if (m == MATCH_YES)
2712 return true;
2713 else if (m == MATCH_ERROR)
2714 return false;
2715
2716 generic:
2717 if (!intr)
2718 for (intr = sym->generic; intr; intr = intr->next)
2719 if (gfc_fl_struct (intr->sym->attr.flavor))
2720 break;
2721
2722 if (sym->ns->parent == NULL)
2723 break;
2724 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2725
2726 if (sym == NULL)
2727 break;
2728 if (!generic_sym (sym))
2729 goto generic;
2730 }
2731
2732 /* Last ditch attempt. See if the reference is to an intrinsic
2733 that possesses a matching interface. 14.1.2.4 */
2734 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2735 {
2736 if (gfc_init_expr_flag)
2737 gfc_error ("Function %qs in initialization expression at %L "
2738 "must be an intrinsic function",
2739 expr->symtree->n.sym->name, &expr->where);
2740 else
2741 gfc_error ("There is no specific function for the generic %qs "
2742 "at %L", expr->symtree->n.sym->name, &expr->where);
2743 return false;
2744 }
2745
2746 if (intr)
2747 {
2748 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2749 NULL, false))
2750 return false;
2751 if (!gfc_use_derived (expr->ts.u.derived))
2752 return false;
2753 return resolve_structure_cons (expr, 0);
2754 }
2755
2756 m = gfc_intrinsic_func_interface (expr, 0);
2757 if (m == MATCH_YES)
2758 return true;
2759
2760 if (m == MATCH_NO)
2761 gfc_error ("Generic function %qs at %L is not consistent with a "
2762 "specific intrinsic interface", expr->symtree->n.sym->name,
2763 &expr->where);
2764
2765 return false;
2766 }
2767
2768
2769 /* Resolve a function call known to be specific. */
2770
2771 static match
2772 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2773 {
2774 match m;
2775
2776 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2777 {
2778 if (sym->attr.dummy)
2779 {
2780 sym->attr.proc = PROC_DUMMY;
2781 goto found;
2782 }
2783
2784 sym->attr.proc = PROC_EXTERNAL;
2785 goto found;
2786 }
2787
2788 if (sym->attr.proc == PROC_MODULE
2789 || sym->attr.proc == PROC_ST_FUNCTION
2790 || sym->attr.proc == PROC_INTERNAL)
2791 goto found;
2792
2793 if (sym->attr.intrinsic)
2794 {
2795 m = gfc_intrinsic_func_interface (expr, 1);
2796 if (m == MATCH_YES)
2797 return MATCH_YES;
2798 if (m == MATCH_NO)
2799 gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
2800 "with an intrinsic", sym->name, &expr->where);
2801
2802 return MATCH_ERROR;
2803 }
2804
2805 return MATCH_NO;
2806
2807 found:
2808 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2809
2810 if (sym->result)
2811 expr->ts = sym->result->ts;
2812 else
2813 expr->ts = sym->ts;
2814 expr->value.function.name = sym->name;
2815 expr->value.function.esym = sym;
2816 /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2817 error(s). */
2818 if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
2819 return MATCH_ERROR;
2820 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2821 expr->rank = CLASS_DATA (sym)->as->rank;
2822 else if (sym->as != NULL)
2823 expr->rank = sym->as->rank;
2824
2825 return MATCH_YES;
2826 }
2827
2828
2829 static bool
2830 resolve_specific_f (gfc_expr *expr)
2831 {
2832 gfc_symbol *sym;
2833 match m;
2834
2835 sym = expr->symtree->n.sym;
2836
2837 for (;;)
2838 {
2839 m = resolve_specific_f0 (sym, expr);
2840 if (m == MATCH_YES)
2841 return true;
2842 if (m == MATCH_ERROR)
2843 return false;
2844
2845 if (sym->ns->parent == NULL)
2846 break;
2847
2848 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2849
2850 if (sym == NULL)
2851 break;
2852 }
2853
2854 gfc_error ("Unable to resolve the specific function %qs at %L",
2855 expr->symtree->n.sym->name, &expr->where);
2856
2857 return true;
2858 }
2859
2860 /* Recursively append candidate SYM to CANDIDATES. Store the number of
2861 candidates in CANDIDATES_LEN. */
2862
2863 static void
2864 lookup_function_fuzzy_find_candidates (gfc_symtree *sym,
2865 char **&candidates,
2866 size_t &candidates_len)
2867 {
2868 gfc_symtree *p;
2869
2870 if (sym == NULL)
2871 return;
2872 if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external)
2873 && sym->n.sym->attr.flavor == FL_PROCEDURE)
2874 vec_push (candidates, candidates_len, sym->name);
2875
2876 p = sym->left;
2877 if (p)
2878 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
2879
2880 p = sym->right;
2881 if (p)
2882 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
2883 }
2884
2885
2886 /* Lookup function FN fuzzily, taking names in SYMROOT into account. */
2887
2888 const char*
2889 gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot)
2890 {
2891 char **candidates = NULL;
2892 size_t candidates_len = 0;
2893 lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len);
2894 return gfc_closest_fuzzy_match (fn, candidates);
2895 }
2896
2897
2898 /* Resolve a procedure call not known to be generic nor specific. */
2899
2900 static bool
2901 resolve_unknown_f (gfc_expr *expr)
2902 {
2903 gfc_symbol *sym;
2904 gfc_typespec *ts;
2905
2906 sym = expr->symtree->n.sym;
2907
2908 if (sym->attr.dummy)
2909 {
2910 sym->attr.proc = PROC_DUMMY;
2911 expr->value.function.name = sym->name;
2912 goto set_type;
2913 }
2914
2915 /* See if we have an intrinsic function reference. */
2916
2917 if (gfc_is_intrinsic (sym, 0, expr->where))
2918 {
2919 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2920 return true;
2921 return false;
2922 }
2923
2924 /* The reference is to an external name. */
2925
2926 sym->attr.proc = PROC_EXTERNAL;
2927 expr->value.function.name = sym->name;
2928 expr->value.function.esym = expr->symtree->n.sym;
2929
2930 if (sym->as != NULL)
2931 expr->rank = sym->as->rank;
2932
2933 /* Type of the expression is either the type of the symbol or the
2934 default type of the symbol. */
2935
2936 set_type:
2937 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2938
2939 if (sym->ts.type != BT_UNKNOWN)
2940 expr->ts = sym->ts;
2941 else
2942 {
2943 ts = gfc_get_default_type (sym->name, sym->ns);
2944
2945 if (ts->type == BT_UNKNOWN)
2946 {
2947 const char *guessed
2948 = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
2949 if (guessed)
2950 gfc_error ("Function %qs at %L has no IMPLICIT type"
2951 "; did you mean %qs?",
2952 sym->name, &expr->where, guessed);
2953 else
2954 gfc_error ("Function %qs at %L has no IMPLICIT type",
2955 sym->name, &expr->where);
2956 return false;
2957 }
2958 else
2959 expr->ts = *ts;
2960 }
2961
2962 return true;
2963 }
2964
2965
2966 /* Return true, if the symbol is an external procedure. */
2967 static bool
2968 is_external_proc (gfc_symbol *sym)
2969 {
2970 if (!sym->attr.dummy && !sym->attr.contained
2971 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
2972 && sym->attr.proc != PROC_ST_FUNCTION
2973 && !sym->attr.proc_pointer
2974 && !sym->attr.use_assoc
2975 && sym->name)
2976 return true;
2977
2978 return false;
2979 }
2980
2981
2982 /* Figure out if a function reference is pure or not. Also set the name
2983 of the function for a potential error message. Return nonzero if the
2984 function is PURE, zero if not. */
2985 static int
2986 pure_stmt_function (gfc_expr *, gfc_symbol *);
2987
2988 int
2989 gfc_pure_function (gfc_expr *e, const char **name)
2990 {
2991 int pure;
2992 gfc_component *comp;
2993
2994 *name = NULL;
2995
2996 if (e->symtree != NULL
2997 && e->symtree->n.sym != NULL
2998 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2999 return pure_stmt_function (e, e->symtree->n.sym);
3000
3001 comp = gfc_get_proc_ptr_comp (e);
3002 if (comp)
3003 {
3004 pure = gfc_pure (comp->ts.interface);
3005 *name = comp->name;
3006 }
3007 else if (e->value.function.esym)
3008 {
3009 pure = gfc_pure (e->value.function.esym);
3010 *name = e->value.function.esym->name;
3011 }
3012 else if (e->value.function.isym)
3013 {
3014 pure = e->value.function.isym->pure
3015 || e->value.function.isym->elemental;
3016 *name = e->value.function.isym->name;
3017 }
3018 else
3019 {
3020 /* Implicit functions are not pure. */
3021 pure = 0;
3022 *name = e->value.function.name;
3023 }
3024
3025 return pure;
3026 }
3027
3028
3029 /* Check if the expression is a reference to an implicitly pure function. */
3030
3031 int
3032 gfc_implicit_pure_function (gfc_expr *e)
3033 {
3034 gfc_component *comp = gfc_get_proc_ptr_comp (e);
3035 if (comp)
3036 return gfc_implicit_pure (comp->ts.interface);
3037 else if (e->value.function.esym)
3038 return gfc_implicit_pure (e->value.function.esym);
3039 else
3040 return 0;
3041 }
3042
3043
3044 static bool
3045 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
3046 int *f ATTRIBUTE_UNUSED)
3047 {
3048 const char *name;
3049
3050 /* Don't bother recursing into other statement functions
3051 since they will be checked individually for purity. */
3052 if (e->expr_type != EXPR_FUNCTION
3053 || !e->symtree
3054 || e->symtree->n.sym == sym
3055 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3056 return false;
3057
3058 return gfc_pure_function (e, &name) ? false : true;
3059 }
3060
3061
3062 static int
3063 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
3064 {
3065 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
3066 }
3067
3068
3069 /* Check if an impure function is allowed in the current context. */
3070
3071 static bool check_pure_function (gfc_expr *e)
3072 {
3073 const char *name = NULL;
3074 if (!gfc_pure_function (e, &name) && name)
3075 {
3076 if (forall_flag)
3077 {
3078 gfc_error ("Reference to impure function %qs at %L inside a "
3079 "FORALL %s", name, &e->where,
3080 forall_flag == 2 ? "mask" : "block");
3081 return false;
3082 }
3083 else if (gfc_do_concurrent_flag)
3084 {
3085 gfc_error ("Reference to impure function %qs at %L inside a "
3086 "DO CONCURRENT %s", name, &e->where,
3087 gfc_do_concurrent_flag == 2 ? "mask" : "block");
3088 return false;
3089 }
3090 else if (gfc_pure (NULL))
3091 {
3092 gfc_error ("Reference to impure function %qs at %L "
3093 "within a PURE procedure", name, &e->where);
3094 return false;
3095 }
3096 if (!gfc_implicit_pure_function (e))
3097 gfc_unset_implicit_pure (NULL);
3098 }
3099 return true;
3100 }
3101
3102
3103 /* Update current procedure's array_outer_dependency flag, considering
3104 a call to procedure SYM. */
3105
3106 static void
3107 update_current_proc_array_outer_dependency (gfc_symbol *sym)
3108 {
3109 /* Check to see if this is a sibling function that has not yet
3110 been resolved. */
3111 gfc_namespace *sibling = gfc_current_ns->sibling;
3112 for (; sibling; sibling = sibling->sibling)
3113 {
3114 if (sibling->proc_name == sym)
3115 {
3116 gfc_resolve (sibling);
3117 break;
3118 }
3119 }
3120
3121 /* If SYM has references to outer arrays, so has the procedure calling
3122 SYM. If SYM is a procedure pointer, we can assume the worst. */
3123 if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
3124 && gfc_current_ns->proc_name)
3125 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3126 }
3127
3128
3129 /* Resolve a function call, which means resolving the arguments, then figuring
3130 out which entity the name refers to. */
3131
3132 static bool
3133 resolve_function (gfc_expr *expr)
3134 {
3135 gfc_actual_arglist *arg;
3136 gfc_symbol *sym;
3137 bool t;
3138 int temp;
3139 procedure_type p = PROC_INTRINSIC;
3140 bool no_formal_args;
3141
3142 sym = NULL;
3143 if (expr->symtree)
3144 sym = expr->symtree->n.sym;
3145
3146 /* If this is a procedure pointer component, it has already been resolved. */
3147 if (gfc_is_proc_ptr_comp (expr))
3148 return true;
3149
3150 /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
3151 another caf_get. */
3152 if (sym && sym->attr.intrinsic
3153 && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
3154 || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
3155 return true;
3156
3157 if (expr->ref)
3158 {
3159 gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name,
3160 &expr->where);
3161 return false;
3162 }
3163
3164 if (sym && sym->attr.intrinsic
3165 && !gfc_resolve_intrinsic (sym, &expr->where))
3166 return false;
3167
3168 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
3169 {
3170 gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
3171 return false;
3172 }
3173
3174 /* If this is a deferred TBP with an abstract interface (which may
3175 of course be referenced), expr->value.function.esym will be set. */
3176 if (sym && sym->attr.abstract && !expr->value.function.esym)
3177 {
3178 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3179 sym->name, &expr->where);
3180 return false;
3181 }
3182
3183 /* If this is a deferred TBP with an abstract interface, its result
3184 cannot be an assumed length character (F2003: C418). */
3185 if (sym && sym->attr.abstract && sym->attr.function
3186 && sym->result->ts.u.cl
3187 && sym->result->ts.u.cl->length == NULL
3188 && !sym->result->ts.deferred)
3189 {
3190 gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
3191 "character length result (F2008: C418)", sym->name,
3192 &sym->declared_at);
3193 return false;
3194 }
3195
3196 /* Switch off assumed size checking and do this again for certain kinds
3197 of procedure, once the procedure itself is resolved. */
3198 need_full_assumed_size++;
3199
3200 if (expr->symtree && expr->symtree->n.sym)
3201 p = expr->symtree->n.sym->attr.proc;
3202
3203 if (expr->value.function.isym && expr->value.function.isym->inquiry)
3204 inquiry_argument = true;
3205 no_formal_args = sym && is_external_proc (sym)
3206 && gfc_sym_get_dummy_args (sym) == NULL;
3207
3208 if (!resolve_actual_arglist (expr->value.function.actual,
3209 p, no_formal_args))
3210 {
3211 inquiry_argument = false;
3212 return false;
3213 }
3214
3215 inquiry_argument = false;
3216
3217 /* Resume assumed_size checking. */
3218 need_full_assumed_size--;
3219
3220 /* If the procedure is external, check for usage. */
3221 if (sym && is_external_proc (sym))
3222 resolve_global_procedure (sym, &expr->where, 0);
3223
3224 if (sym && sym->ts.type == BT_CHARACTER
3225 && sym->ts.u.cl
3226 && sym->ts.u.cl->length == NULL
3227 && !sym->attr.dummy
3228 && !sym->ts.deferred
3229 && expr->value.function.esym == NULL
3230 && !sym->attr.contained)
3231 {
3232 /* Internal procedures are taken care of in resolve_contained_fntype. */
3233 gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
3234 "be used at %L since it is not a dummy argument",
3235 sym->name, &expr->where);
3236 return false;
3237 }
3238
3239 /* See if function is already resolved. */
3240
3241 if (expr->value.function.name != NULL
3242 || expr->value.function.isym != NULL)
3243 {
3244 if (expr->ts.type == BT_UNKNOWN)
3245 expr->ts = sym->ts;
3246 t = true;
3247 }
3248 else
3249 {
3250 /* Apply the rules of section 14.1.2. */
3251
3252 switch (procedure_kind (sym))
3253 {
3254 case PTYPE_GENERIC:
3255 t = resolve_generic_f (expr);
3256 break;
3257
3258 case PTYPE_SPECIFIC:
3259 t = resolve_specific_f (expr);
3260 break;
3261
3262 case PTYPE_UNKNOWN:
3263 t = resolve_unknown_f (expr);
3264 break;
3265
3266 default:
3267 gfc_internal_error ("resolve_function(): bad function type");
3268 }
3269 }
3270
3271 /* If the expression is still a function (it might have simplified),
3272 then we check to see if we are calling an elemental function. */
3273
3274 if (expr->expr_type != EXPR_FUNCTION)
3275 return t;
3276
3277 /* Walk the argument list looking for invalid BOZ. */
3278 for (arg = expr->value.function.actual; arg; arg = arg->next)
3279 if (arg->expr && arg->expr->ts.type == BT_BOZ)
3280 {
3281 gfc_error ("A BOZ literal constant at %L cannot appear as an "
3282 "actual argument in a function reference",
3283 &arg->expr->where);
3284 return false;
3285 }
3286
3287 temp = need_full_assumed_size;
3288 need_full_assumed_size = 0;
3289
3290 if (!resolve_elemental_actual (expr, NULL))
3291 return false;
3292
3293 if (omp_workshare_flag
3294 && expr->value.function.esym
3295 && ! gfc_elemental (expr->value.function.esym))
3296 {
3297 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3298 "in WORKSHARE construct", expr->value.function.esym->name,
3299 &expr->where);
3300 t = false;
3301 }
3302
3303 #define GENERIC_ID expr->value.function.isym->id
3304 else if (expr->value.function.actual != NULL
3305 && expr->value.function.isym != NULL
3306 && GENERIC_ID != GFC_ISYM_LBOUND
3307 && GENERIC_ID != GFC_ISYM_LCOBOUND
3308 && GENERIC_ID != GFC_ISYM_UCOBOUND
3309 && GENERIC_ID != GFC_ISYM_LEN
3310 && GENERIC_ID != GFC_ISYM_LOC
3311 && GENERIC_ID != GFC_ISYM_C_LOC
3312 && GENERIC_ID != GFC_ISYM_PRESENT)
3313 {
3314 /* Array intrinsics must also have the last upper bound of an
3315 assumed size array argument. UBOUND and SIZE have to be
3316 excluded from the check if the second argument is anything
3317 than a constant. */
3318
3319 for (arg = expr->value.function.actual; arg; arg = arg->next)
3320 {
3321 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3322 && arg == expr->value.function.actual
3323 && arg->next != NULL && arg->next->expr)
3324 {
3325 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3326 break;
3327
3328 if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
3329 break;
3330
3331 if ((int)mpz_get_si (arg->next->expr->value.integer)
3332 < arg->expr->rank)
3333 break;
3334 }
3335
3336 if (arg->expr != NULL
3337 && arg->expr->rank > 0
3338 && resolve_assumed_size_actual (arg->expr))
3339 return false;
3340 }
3341 }
3342 #undef GENERIC_ID
3343
3344 need_full_assumed_size = temp;
3345
3346 if (!check_pure_function(expr))
3347 t = false;
3348
3349 /* Functions without the RECURSIVE attribution are not allowed to
3350 * call themselves. */
3351 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3352 {
3353 gfc_symbol *esym;
3354 esym = expr->value.function.esym;
3355
3356 if (is_illegal_recursion (esym, gfc_current_ns))
3357 {
3358 if (esym->attr.entry && esym->ns->entries)
3359 gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3360 " function %qs is not RECURSIVE",
3361 esym->name, &expr->where, esym->ns->entries->sym->name);
3362 else
3363 gfc_error ("Function %qs at %L cannot be called recursively, as it"
3364 " is not RECURSIVE", esym->name, &expr->where);
3365
3366 t = false;
3367 }
3368 }
3369
3370 /* Character lengths of use associated functions may contains references to
3371 symbols not referenced from the current program unit otherwise. Make sure
3372 those symbols are marked as referenced. */
3373
3374 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3375 && expr->value.function.esym->attr.use_assoc)
3376 {
3377 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3378 }
3379
3380 /* Make sure that the expression has a typespec that works. */
3381 if (expr->ts.type == BT_UNKNOWN)
3382 {
3383 if (expr->symtree->n.sym->result
3384 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3385 && !expr->symtree->n.sym->result->attr.proc_pointer)
3386 expr->ts = expr->symtree->n.sym->result->ts;
3387 }
3388
3389 if (!expr->ref && !expr->value.function.isym)
3390 {
3391 if (expr->value.function.esym)
3392 update_current_proc_array_outer_dependency (expr->value.function.esym);
3393 else
3394 update_current_proc_array_outer_dependency (sym);
3395 }
3396 else if (expr->ref)
3397 /* typebound procedure: Assume the worst. */
3398 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3399
3400 return t;
3401 }
3402
3403
3404 /************* Subroutine resolution *************/
3405
3406 static bool
3407 pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3408 {
3409 if (gfc_pure (sym))
3410 return true;
3411
3412 if (forall_flag)
3413 {
3414 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3415 name, loc);
3416 return false;
3417 }
3418 else if (gfc_do_concurrent_flag)
3419 {
3420 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3421 "PURE", name, loc);
3422 return false;
3423 }
3424 else if (gfc_pure (NULL))
3425 {
3426 gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3427 return false;
3428 }
3429
3430 gfc_unset_implicit_pure (NULL);
3431 return true;
3432 }
3433
3434
3435 static match
3436 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3437 {
3438 gfc_symbol *s;
3439
3440 if (sym->attr.generic)
3441 {
3442 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3443 if (s != NULL)
3444 {
3445 c->resolved_sym = s;
3446 if (!pure_subroutine (s, s->name, &c->loc))
3447 return MATCH_ERROR;
3448 return MATCH_YES;
3449 }
3450
3451 /* TODO: Need to search for elemental references in generic interface. */
3452 }
3453
3454 if (sym->attr.intrinsic)
3455 return gfc_intrinsic_sub_interface (c, 0);
3456
3457 return MATCH_NO;
3458 }
3459
3460
3461 static bool
3462 resolve_generic_s (gfc_code *c)
3463 {
3464 gfc_symbol *sym;
3465 match m;
3466
3467 sym = c->symtree->n.sym;
3468
3469 for (;;)
3470 {
3471 m = resolve_generic_s0 (c, sym);
3472 if (m == MATCH_YES)
3473 return true;
3474 else if (m == MATCH_ERROR)
3475 return false;
3476
3477 generic:
3478 if (sym->ns->parent == NULL)
3479 break;
3480 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3481
3482 if (sym == NULL)
3483 break;
3484 if (!generic_sym (sym))
3485 goto generic;
3486 }
3487
3488 /* Last ditch attempt. See if the reference is to an intrinsic
3489 that possesses a matching interface. 14.1.2.4 */
3490 sym = c->symtree->n.sym;
3491
3492 if (!gfc_is_intrinsic (sym, 1, c->loc))
3493 {
3494 gfc_error ("There is no specific subroutine for the generic %qs at %L",
3495 sym->name, &c->loc);
3496 return false;
3497 }
3498
3499 m = gfc_intrinsic_sub_interface (c, 0);
3500 if (m == MATCH_YES)
3501 return true;
3502 if (m == MATCH_NO)
3503 gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3504 "intrinsic subroutine interface", sym->name, &c->loc);
3505
3506 return false;
3507 }
3508
3509
3510 /* Resolve a subroutine call known to be specific. */
3511
3512 static match
3513 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3514 {
3515 match m;
3516
3517 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3518 {
3519 if (sym->attr.dummy)
3520 {
3521 sym->attr.proc = PROC_DUMMY;
3522 goto found;
3523 }
3524
3525 sym->attr.proc = PROC_EXTERNAL;
3526 goto found;
3527 }
3528
3529 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3530 goto found;
3531
3532 if (sym->attr.intrinsic)
3533 {
3534 m = gfc_intrinsic_sub_interface (c, 1);
3535 if (m == MATCH_YES)
3536 return MATCH_YES;
3537 if (m == MATCH_NO)
3538 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3539 "with an intrinsic", sym->name, &c->loc);
3540
3541 return MATCH_ERROR;
3542 }
3543
3544 return MATCH_NO;
3545
3546 found:
3547 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3548
3549 c->resolved_sym = sym;
3550 if (!pure_subroutine (sym, sym->name, &c->loc))
3551 return MATCH_ERROR;
3552
3553 return MATCH_YES;
3554 }
3555
3556
3557 static bool
3558 resolve_specific_s (gfc_code *c)
3559 {
3560 gfc_symbol *sym;
3561 match m;
3562
3563 sym = c->symtree->n.sym;
3564
3565 for (;;)
3566 {
3567 m = resolve_specific_s0 (c, sym);
3568 if (m == MATCH_YES)
3569 return true;
3570 if (m == MATCH_ERROR)
3571 return false;
3572
3573 if (sym->ns->parent == NULL)
3574 break;
3575
3576 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3577
3578 if (sym == NULL)
3579 break;
3580 }
3581
3582 sym = c->symtree->n.sym;
3583 gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3584 sym->name, &c->loc);
3585
3586 return false;
3587 }
3588
3589
3590 /* Resolve a subroutine call not known to be generic nor specific. */
3591
3592 static bool
3593 resolve_unknown_s (gfc_code *c)
3594 {
3595 gfc_symbol *sym;
3596
3597 sym = c->symtree->n.sym;
3598
3599 if (sym->attr.dummy)
3600 {
3601 sym->attr.proc = PROC_DUMMY;
3602 goto found;
3603 }
3604
3605 /* See if we have an intrinsic function reference. */
3606
3607 if (gfc_is_intrinsic (sym, 1, c->loc))
3608 {
3609 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3610 return true;
3611 return false;
3612 }
3613
3614 /* The reference is to an external name. */
3615
3616 found:
3617 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3618
3619 c->resolved_sym = sym;
3620
3621 return pure_subroutine (sym, sym->name, &c->loc);
3622 }
3623
3624
3625 /* Resolve a subroutine call. Although it was tempting to use the same code
3626 for functions, subroutines and functions are stored differently and this
3627 makes things awkward. */
3628
3629 static bool
3630 resolve_call (gfc_code *c)
3631 {
3632 bool t;
3633 procedure_type ptype = PROC_INTRINSIC;
3634 gfc_symbol *csym, *sym;
3635 bool no_formal_args;
3636
3637 csym = c->symtree ? c->symtree->n.sym : NULL;
3638
3639 if (csym && csym->ts.type != BT_UNKNOWN)
3640 {
3641 gfc_error ("%qs at %L has a type, which is not consistent with "
3642 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3643 return false;
3644 }
3645
3646 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3647 {
3648 gfc_symtree *st;
3649 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3650 sym = st ? st->n.sym : NULL;
3651 if (sym && csym != sym
3652 && sym->ns == gfc_current_ns
3653 && sym->attr.flavor == FL_PROCEDURE
3654 && sym->attr.contained)
3655 {
3656 sym->refs++;
3657 if (csym->attr.generic)
3658 c->symtree->n.sym = sym;
3659 else
3660 c->symtree = st;
3661 csym = c->symtree->n.sym;
3662 }
3663 }
3664
3665 /* If this ia a deferred TBP, c->expr1 will be set. */
3666 if (!c->expr1 && csym)
3667 {
3668 if (csym->attr.abstract)
3669 {
3670 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3671 csym->name, &c->loc);
3672 return false;
3673 }
3674
3675 /* Subroutines without the RECURSIVE attribution are not allowed to
3676 call themselves. */
3677 if (is_illegal_recursion (csym, gfc_current_ns))
3678 {
3679 if (csym->attr.entry && csym->ns->entries)
3680 gfc_error ("ENTRY %qs at %L cannot be called recursively, "
3681 "as subroutine %qs is not RECURSIVE",
3682 csym->name, &c->loc, csym->ns->entries->sym->name);
3683 else
3684 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
3685 "as it is not RECURSIVE", csym->name, &c->loc);
3686
3687 t = false;
3688 }
3689 }
3690
3691 /* Switch off assumed size checking and do this again for certain kinds
3692 of procedure, once the procedure itself is resolved. */
3693 need_full_assumed_size++;
3694
3695 if (csym)
3696 ptype = csym->attr.proc;
3697
3698 no_formal_args = csym && is_external_proc (csym)
3699 && gfc_sym_get_dummy_args (csym) == NULL;
3700 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3701 return false;
3702
3703 /* Resume assumed_size checking. */
3704 need_full_assumed_size--;
3705
3706 /* If external, check for usage. */
3707 if (csym && is_external_proc (csym))
3708 resolve_global_procedure (csym, &c->loc, 1);
3709
3710 t = true;
3711 if (c->resolved_sym == NULL)
3712 {
3713 c->resolved_isym = NULL;
3714 switch (procedure_kind (csym))
3715 {
3716 case PTYPE_GENERIC:
3717 t = resolve_generic_s (c);
3718 break;
3719
3720 case PTYPE_SPECIFIC:
3721 t = resolve_specific_s (c);
3722 break;
3723
3724 case PTYPE_UNKNOWN:
3725 t = resolve_unknown_s (c);
3726 break;
3727
3728 default:
3729 gfc_internal_error ("resolve_subroutine(): bad function type");
3730 }
3731 }
3732
3733 /* Some checks of elemental subroutine actual arguments. */
3734 if (!resolve_elemental_actual (NULL, c))
3735 return false;
3736
3737 if (!c->expr1)
3738 update_current_proc_array_outer_dependency (csym);
3739 else
3740 /* Typebound procedure: Assume the worst. */
3741 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3742
3743 return t;
3744 }
3745
3746
3747 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3748 op1->shape and op2->shape are non-NULL return true if their shapes
3749 match. If both op1->shape and op2->shape are non-NULL return false
3750 if their shapes do not match. If either op1->shape or op2->shape is
3751 NULL, return true. */
3752
3753 static bool
3754 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3755 {
3756 bool t;
3757 int i;
3758
3759 t = true;
3760
3761 if (op1->shape != NULL && op2->shape != NULL)
3762 {
3763 for (i = 0; i < op1->rank; i++)
3764 {
3765 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3766 {
3767 gfc_error ("Shapes for operands at %L and %L are not conformable",
3768 &op1->where, &op2->where);
3769 t = false;
3770 break;
3771 }
3772 }
3773 }
3774
3775 return t;
3776 }
3777
3778 /* Convert a logical operator to the corresponding bitwise intrinsic call.
3779 For example A .AND. B becomes IAND(A, B). */
3780 static gfc_expr *
3781 logical_to_bitwise (gfc_expr *e)
3782 {
3783 gfc_expr *tmp, *op1, *op2;
3784 gfc_isym_id isym;
3785 gfc_actual_arglist *args = NULL;
3786
3787 gcc_assert (e->expr_type == EXPR_OP);
3788
3789 isym = GFC_ISYM_NONE;
3790 op1 = e->value.op.op1;
3791 op2 = e->value.op.op2;
3792
3793 switch (e->value.op.op)
3794 {
3795 case INTRINSIC_NOT:
3796 isym = GFC_ISYM_NOT;
3797 break;
3798 case INTRINSIC_AND:
3799 isym = GFC_ISYM_IAND;
3800 break;
3801 case INTRINSIC_OR:
3802 isym = GFC_ISYM_IOR;
3803 break;
3804 case INTRINSIC_NEQV:
3805 isym = GFC_ISYM_IEOR;
3806 break;
3807 case INTRINSIC_EQV:
3808 /* "Bitwise eqv" is just the complement of NEQV === IEOR.
3809 Change the old expression to NEQV, which will get replaced by IEOR,
3810 and wrap it in NOT. */
3811 tmp = gfc_copy_expr (e);
3812 tmp->value.op.op = INTRINSIC_NEQV;
3813 tmp = logical_to_bitwise (tmp);
3814 isym = GFC_ISYM_NOT;
3815 op1 = tmp;
3816 op2 = NULL;
3817 break;
3818 default:
3819 gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
3820 }
3821
3822 /* Inherit the original operation's operands as arguments. */
3823 args = gfc_get_actual_arglist ();
3824 args->expr = op1;
3825 if (op2)
3826 {
3827 args->next = gfc_get_actual_arglist ();
3828 args->next->expr = op2;
3829 }
3830
3831 /* Convert the expression to a function call. */
3832 e->expr_type = EXPR_FUNCTION;
3833 e->value.function.actual = args;
3834 e->value.function.isym = gfc_intrinsic_function_by_id (isym);
3835 e->value.function.name = e->value.function.isym->name;
3836 e->value.function.esym = NULL;
3837
3838 /* Make up a pre-resolved function call symtree if we need to. */
3839 if (!e->symtree || !e->symtree->n.sym)
3840 {
3841 gfc_symbol *sym;
3842 gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
3843 sym = e->symtree->n.sym;
3844 sym->result = sym;
3845 sym->attr.flavor = FL_PROCEDURE;
3846 sym->attr.function = 1;
3847 sym->attr.elemental = 1;
3848 sym->attr.pure = 1;
3849 sym->attr.referenced = 1;
3850 gfc_intrinsic_symbol (sym);
3851 gfc_commit_symbol (sym);
3852 }
3853
3854 args->name = e->value.function.isym->formal->name;
3855 if (e->value.function.isym->formal->next)
3856 args->next->name = e->value.function.isym->formal->next->name;
3857
3858 return e;
3859 }
3860
3861 /* Recursively append candidate UOP to CANDIDATES. Store the number of
3862 candidates in CANDIDATES_LEN. */
3863 static void
3864 lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
3865 char **&candidates,
3866 size_t &candidates_len)
3867 {
3868 gfc_symtree *p;
3869
3870 if (uop == NULL)
3871 return;
3872
3873 /* Not sure how to properly filter here. Use all for a start.
3874 n.uop.op is NULL for empty interface operators (is that legal?) disregard
3875 these as i suppose they don't make terribly sense. */
3876
3877 if (uop->n.uop->op != NULL)
3878 vec_push (candidates, candidates_len, uop->name);
3879
3880 p = uop->left;
3881 if (p)
3882 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
3883
3884 p = uop->right;
3885 if (p)
3886 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
3887 }
3888
3889 /* Lookup user-operator OP fuzzily, taking names in UOP into account. */
3890
3891 static const char*
3892 lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
3893 {
3894 char **candidates = NULL;
3895 size_t candidates_len = 0;
3896 lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
3897 return gfc_closest_fuzzy_match (op, candidates);
3898 }
3899
3900
3901 /* Callback finding an impure function as an operand to an .and. or
3902 .or. expression. Remember the last function warned about to
3903 avoid double warnings when recursing. */
3904
3905 static int
3906 impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
3907 void *data)
3908 {
3909 gfc_expr *f = *e;
3910 const char *name;
3911 static gfc_expr *last = NULL;
3912 bool *found = (bool *) data;
3913
3914 if (f->expr_type == EXPR_FUNCTION)
3915 {
3916 *found = 1;
3917 if (f != last && !gfc_pure_function (f, &name)
3918 && !gfc_implicit_pure_function (f))
3919 {
3920 if (name)
3921 gfc_warning (OPT_Wfunction_elimination,
3922 "Impure function %qs at %L might not be evaluated",
3923 name, &f->where);
3924 else
3925 gfc_warning (OPT_Wfunction_elimination,
3926 "Impure function at %L might not be evaluated",
3927 &f->where);
3928 }
3929 last = f;
3930 }
3931
3932 return 0;
3933 }
3934
3935 /* Return true if TYPE is character based, false otherwise. */
3936
3937 static int
3938 is_character_based (bt type)
3939 {
3940 return type == BT_CHARACTER || type == BT_HOLLERITH;
3941 }
3942
3943
3944 /* If expression is a hollerith, convert it to character and issue a warning
3945 for the conversion. */
3946
3947 static void
3948 convert_hollerith_to_character (gfc_expr *e)
3949 {
3950 if (e->ts.type == BT_HOLLERITH)
3951 {
3952 gfc_typespec t;
3953 gfc_clear_ts (&t);
3954 t.type = BT_CHARACTER;
3955 t.kind = e->ts.kind;
3956 gfc_convert_type_warn (e, &t, 2, 1);
3957 }
3958 }
3959
3960 /* Convert to numeric and issue a warning for the conversion. */
3961
3962 static void
3963 convert_to_numeric (gfc_expr *a, gfc_expr *b)
3964 {
3965 gfc_typespec t;
3966 gfc_clear_ts (&t);
3967 t.type = b->ts.type;
3968 t.kind = b->ts.kind;
3969 gfc_convert_type_warn (a, &t, 2, 1);
3970 }
3971
3972 /* Resolve an operator expression node. This can involve replacing the
3973 operation with a user defined function call. */
3974
3975 static bool
3976 resolve_operator (gfc_expr *e)
3977 {
3978 gfc_expr *op1, *op2;
3979 char msg[200];
3980 bool dual_locus_error;
3981 bool t = true;
3982
3983 /* Resolve all subnodes-- give them types. */
3984
3985 switch (e->value.op.op)
3986 {
3987 default:
3988 if (!gfc_resolve_expr (e->value.op.op2))
3989 return false;
3990
3991 /* Fall through. */
3992
3993 case INTRINSIC_NOT:
3994 case INTRINSIC_UPLUS:
3995 case INTRINSIC_UMINUS:
3996 case INTRINSIC_PARENTHESES:
3997 if (!gfc_resolve_expr (e->value.op.op1))
3998 return false;
3999 if (e->value.op.op1
4000 && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
4001 {
4002 gfc_error ("BOZ literal constant at %L cannot be an operand of "
4003 "unary operator %qs", &e->value.op.op1->where,
4004 gfc_op2string (e->value.op.op));
4005 return false;
4006 }
4007 break;
4008 }
4009
4010 /* Typecheck the new node. */
4011
4012 op1 = e->value.op.op1;
4013 op2 = e->value.op.op2;
4014 if (op1 == NULL && op2 == NULL)
4015 return false;
4016
4017 dual_locus_error = false;
4018
4019 /* op1 and op2 cannot both be BOZ. */
4020 if (op1 && op1->ts.type == BT_BOZ
4021 && op2 && op2->ts.type == BT_BOZ)
4022 {
4023 gfc_error ("Operands at %L and %L cannot appear as operands of "
4024 "binary operator %qs", &op1->where, &op2->where,
4025 gfc_op2string (e->value.op.op));
4026 return false;
4027 }
4028
4029 if ((op1 && op1->expr_type == EXPR_NULL)
4030 || (op2 && op2->expr_type == EXPR_NULL))
4031 {
4032 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
4033 goto bad_op;
4034 }
4035
4036 switch (e->value.op.op)
4037 {
4038 case INTRINSIC_UPLUS:
4039 case INTRINSIC_UMINUS:
4040 if (op1->ts.type == BT_INTEGER
4041 || op1->ts.type == BT_REAL
4042 || op1->ts.type == BT_COMPLEX)
4043 {
4044 e->ts = op1->ts;
4045 break;
4046 }
4047
4048 sprintf (msg, _("Operand of unary numeric operator %%<%s%%> at %%L is %s"),
4049 gfc_op2string (e->value.op.op), gfc_typename (e));
4050 goto bad_op;
4051
4052 case INTRINSIC_PLUS:
4053 case INTRINSIC_MINUS:
4054 case INTRINSIC_TIMES:
4055 case INTRINSIC_DIVIDE:
4056 case INTRINSIC_POWER:
4057 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4058 {
4059 gfc_type_convert_binary (e, 1);
4060 break;
4061 }
4062
4063 if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
4064 sprintf (msg,
4065 _("Unexpected derived-type entities in binary intrinsic "
4066 "numeric operator %%<%s%%> at %%L"),
4067 gfc_op2string (e->value.op.op));
4068 else
4069 sprintf (msg,
4070 _("Operands of binary numeric operator %%<%s%%> at %%L are %s/%s"),
4071 gfc_op2string (e->value.op.op), gfc_typename (op1),
4072 gfc_typename (op2));
4073 goto bad_op;
4074
4075 case INTRINSIC_CONCAT:
4076 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4077 && op1->ts.kind == op2->ts.kind)
4078 {
4079 e->ts.type = BT_CHARACTER;
4080 e->ts.kind = op1->ts.kind;
4081 break;
4082 }
4083
4084 sprintf (msg,
4085 _("Operands of string concatenation operator at %%L are %s/%s"),
4086 gfc_typename (op1), gfc_typename (op2));
4087 goto bad_op;
4088
4089 case INTRINSIC_AND:
4090 case INTRINSIC_OR:
4091 case INTRINSIC_EQV:
4092 case INTRINSIC_NEQV:
4093 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4094 {
4095 e->ts.type = BT_LOGICAL;
4096 e->ts.kind = gfc_kind_max (op1, op2);
4097 if (op1->ts.kind < e->ts.kind)
4098 gfc_convert_type (op1, &e->ts, 2);
4099 else if (op2->ts.kind < e->ts.kind)
4100 gfc_convert_type (op2, &e->ts, 2);
4101
4102 if (flag_frontend_optimize &&
4103 (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
4104 {
4105 /* Warn about short-circuiting
4106 with impure function as second operand. */
4107 bool op2_f = false;
4108 gfc_expr_walker (&op2, impure_function_callback, &op2_f);
4109 }
4110 break;
4111 }
4112
4113 /* Logical ops on integers become bitwise ops with -fdec. */
4114 else if (flag_dec
4115 && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
4116 {
4117 e->ts.type = BT_INTEGER;
4118 e->ts.kind = gfc_kind_max (op1, op2);
4119 if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
4120 gfc_convert_type (op1, &e->ts, 1);
4121 if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
4122 gfc_convert_type (op2, &e->ts, 1);
4123 e = logical_to_bitwise (e);
4124 goto simplify_op;
4125 }
4126
4127 sprintf (msg, _("Operands of logical operator %%<%s%%> at %%L are %s/%s"),
4128 gfc_op2string (e->value.op.op), gfc_typename (op1),
4129 gfc_typename (op2));
4130
4131 goto bad_op;
4132
4133 case INTRINSIC_NOT:
4134 /* Logical ops on integers become bitwise ops with -fdec. */
4135 if (flag_dec && op1->ts.type == BT_INTEGER)
4136 {
4137 e->ts.type = BT_INTEGER;
4138 e->ts.kind = op1->ts.kind;
4139 e = logical_to_bitwise (e);
4140 goto simplify_op;
4141 }
4142
4143 if (op1->ts.type == BT_LOGICAL)
4144 {
4145 e->ts.type = BT_LOGICAL;
4146 e->ts.kind = op1->ts.kind;
4147 break;
4148 }
4149
4150 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
4151 gfc_typename (op1));
4152 goto bad_op;
4153
4154 case INTRINSIC_GT:
4155 case INTRINSIC_GT_OS:
4156 case INTRINSIC_GE:
4157 case INTRINSIC_GE_OS:
4158 case INTRINSIC_LT:
4159 case INTRINSIC_LT_OS:
4160 case INTRINSIC_LE:
4161 case INTRINSIC_LE_OS:
4162 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
4163 {
4164 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
4165 goto bad_op;
4166 }
4167
4168 /* Fall through. */
4169
4170 case INTRINSIC_EQ:
4171 case INTRINSIC_EQ_OS:
4172 case INTRINSIC_NE:
4173 case INTRINSIC_NE_OS:
4174
4175 if (flag_dec
4176 && is_character_based (op1->ts.type)
4177 && is_character_based (op2->ts.type))
4178 {
4179 convert_hollerith_to_character (op1);
4180 convert_hollerith_to_character (op2);
4181 }
4182
4183 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4184 && op1->ts.kind == op2->ts.kind)
4185 {
4186 e->ts.type = BT_LOGICAL;
4187 e->ts.kind = gfc_default_logical_kind;
4188 break;
4189 }
4190
4191 /* If op1 is BOZ, then op2 is not!. Try to convert to type of op2. */
4192 if (op1->ts.type == BT_BOZ)
4193 {
4194 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
4195 "as an operand of a relational operator"),
4196 &op1->where))
4197 return false;
4198
4199 if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
4200 return false;
4201
4202 if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
4203 return false;
4204 }
4205
4206 /* If op2 is BOZ, then op1 is not!. Try to convert to type of op2. */
4207 if (op2->ts.type == BT_BOZ)
4208 {
4209 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
4210 " as an operand of a relational operator"),
4211 &op2->where))
4212 return false;
4213
4214 if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
4215 return false;
4216
4217 if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
4218 return false;
4219 }
4220 if (flag_dec
4221 && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
4222 convert_to_numeric (op1, op2);
4223
4224 if (flag_dec
4225 && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
4226 convert_to_numeric (op2, op1);
4227
4228 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4229 {
4230 gfc_type_convert_binary (e, 1);
4231
4232 e->ts.type = BT_LOGICAL;
4233 e->ts.kind = gfc_default_logical_kind;
4234
4235 if (warn_compare_reals)
4236 {
4237 gfc_intrinsic_op op = e->value.op.op;
4238
4239 /* Type conversion has made sure that the types of op1 and op2
4240 agree, so it is only necessary to check the first one. */
4241 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
4242 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
4243 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
4244 {
4245 const char *msg;
4246
4247 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
4248 msg = G_("Equality comparison for %s at %L");
4249 else
4250 msg = G_("Inequality comparison for %s at %L");
4251
4252 gfc_warning (OPT_Wcompare_reals, msg,
4253 gfc_typename (op1), &op1->where);
4254 }
4255 }
4256
4257 break;
4258 }
4259
4260 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4261 sprintf (msg,
4262 _("Logicals at %%L must be compared with %s instead of %s"),
4263 (e->value.op.op == INTRINSIC_EQ
4264 || e->value.op.op == INTRINSIC_EQ_OS)
4265 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
4266 else
4267 sprintf (msg,
4268 _("Operands of comparison operator %%<%s%%> at %%L are %s/%s"),
4269 gfc_op2string (e->value.op.op), gfc_typename (op1),
4270 gfc_typename (op2));
4271
4272 goto bad_op;
4273
4274 case INTRINSIC_USER:
4275 if (e->value.op.uop->op == NULL)
4276 {
4277 const char *name = e->value.op.uop->name;
4278 const char *guessed;
4279 guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
4280 if (guessed)
4281 sprintf (msg, _("Unknown operator %%<%s%%> at %%L; did you mean '%s'?"),
4282 name, guessed);
4283 else
4284 sprintf (msg, _("Unknown operator %%<%s%%> at %%L"), name);
4285 }
4286 else if (op2 == NULL)
4287 sprintf (msg, _("Operand of user operator %%<%s%%> at %%L is %s"),
4288 e->value.op.uop->name, gfc_typename (op1));
4289 else
4290 {
4291 sprintf (msg, _("Operands of user operator %%<%s%%> at %%L are %s/%s"),
4292 e->value.op.uop->name, gfc_typename (op1),
4293 gfc_typename (op2));
4294 e->value.op.uop->op->sym->attr.referenced = 1;
4295 }
4296
4297 goto bad_op;
4298
4299 case INTRINSIC_PARENTHESES:
4300 e->ts = op1->ts;
4301 if (e->ts.type == BT_CHARACTER)
4302 e->ts.u.cl = op1->ts.u.cl;
4303 break;
4304
4305 default:
4306 gfc_internal_error ("resolve_operator(): Bad intrinsic");
4307 }
4308
4309 /* Deal with arrayness of an operand through an operator. */
4310
4311 switch (e->value.op.op)
4312 {
4313 case INTRINSIC_PLUS:
4314 case INTRINSIC_MINUS:
4315 case INTRINSIC_TIMES:
4316 case INTRINSIC_DIVIDE:
4317 case INTRINSIC_POWER:
4318 case INTRINSIC_CONCAT:
4319 case INTRINSIC_AND:
4320 case INTRINSIC_OR:
4321 case INTRINSIC_EQV:
4322 case INTRINSIC_NEQV:
4323 case INTRINSIC_EQ:
4324 case INTRINSIC_EQ_OS:
4325 case INTRINSIC_NE:
4326 case INTRINSIC_NE_OS:
4327 case INTRINSIC_GT:
4328 case INTRINSIC_GT_OS:
4329 case INTRINSIC_GE:
4330 case INTRINSIC_GE_OS:
4331 case INTRINSIC_LT:
4332 case INTRINSIC_LT_OS:
4333 case INTRINSIC_LE:
4334 case INTRINSIC_LE_OS:
4335
4336 if (op1->rank == 0 && op2->rank == 0)
4337 e->rank = 0;
4338
4339 if (op1->rank == 0 && op2->rank != 0)
4340 {
4341 e->rank = op2->rank;
4342
4343 if (e->shape == NULL)
4344 e->shape = gfc_copy_shape (op2->shape, op2->rank);
4345 }
4346
4347 if (op1->rank != 0 && op2->rank == 0)
4348 {
4349 e->rank = op1->rank;
4350
4351 if (e->shape == NULL)
4352 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4353 }
4354
4355 if (op1->rank != 0 && op2->rank != 0)
4356 {
4357 if (op1->rank == op2->rank)
4358 {
4359 e->rank = op1->rank;
4360 if (e->shape == NULL)
4361 {
4362 t = compare_shapes (op1, op2);
4363 if (!t)
4364 e->shape = NULL;
4365 else
4366 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4367 }
4368 }
4369 else
4370 {
4371 /* Allow higher level expressions to work. */
4372 e->rank = 0;
4373
4374 /* Try user-defined operators, and otherwise throw an error. */
4375 dual_locus_error = true;
4376 sprintf (msg,
4377 _("Inconsistent ranks for operator at %%L and %%L"));
4378 goto bad_op;
4379 }
4380 }
4381
4382 break;
4383
4384 case INTRINSIC_PARENTHESES:
4385 case INTRINSIC_NOT:
4386 case INTRINSIC_UPLUS:
4387 case INTRINSIC_UMINUS:
4388 /* Simply copy arrayness attribute */
4389 e->rank = op1->rank;
4390
4391 if (e->shape == NULL)
4392 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4393
4394 break;
4395
4396 default:
4397 break;
4398 }
4399
4400 simplify_op:
4401
4402 /* Attempt to simplify the expression. */
4403 if (t)
4404 {
4405 t = gfc_simplify_expr (e, 0);
4406 /* Some calls do not succeed in simplification and return false
4407 even though there is no error; e.g. variable references to
4408 PARAMETER arrays. */
4409 if (!gfc_is_constant_expr (e))
4410 t = true;
4411 }
4412 return t;
4413
4414 bad_op:
4415
4416 {
4417 match m = gfc_extend_expr (e);
4418 if (m == MATCH_YES)
4419 return true;
4420 if (m == MATCH_ERROR)
4421 return false;
4422 }
4423
4424 if (dual_locus_error)
4425 gfc_error (msg, &op1->where, &op2->where);
4426 else
4427 gfc_error (msg, &e->where);
4428
4429 return false;
4430 }
4431
4432
4433 /************** Array resolution subroutines **************/
4434
4435 enum compare_result
4436 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
4437
4438 /* Compare two integer expressions. */
4439
4440 static compare_result
4441 compare_bound (gfc_expr *a, gfc_expr *b)
4442 {
4443 int i;
4444
4445 if (a == NULL || a->expr_type != EXPR_CONSTANT
4446 || b == NULL || b->expr_type != EXPR_CONSTANT)
4447 return CMP_UNKNOWN;
4448
4449 /* If either of the types isn't INTEGER, we must have
4450 raised an error earlier. */
4451
4452 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
4453 return CMP_UNKNOWN;
4454
4455 i = mpz_cmp (a->value.integer, b->value.integer);
4456
4457 if (i < 0)
4458 return CMP_LT;
4459 if (i > 0)
4460 return CMP_GT;
4461 return CMP_EQ;
4462 }
4463
4464
4465 /* Compare an integer expression with an integer. */
4466
4467 static compare_result
4468 compare_bound_int (gfc_expr *a, int b)
4469 {
4470 int i;
4471
4472 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4473 return CMP_UNKNOWN;
4474
4475 if (a->ts.type != BT_INTEGER)
4476 gfc_internal_error ("compare_bound_int(): Bad expression");
4477
4478 i = mpz_cmp_si (a->value.integer, b);
4479
4480 if (i < 0)
4481 return CMP_LT;
4482 if (i > 0)
4483 return CMP_GT;
4484 return CMP_EQ;
4485 }
4486
4487
4488 /* Compare an integer expression with a mpz_t. */
4489
4490 static compare_result
4491 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
4492 {
4493 int i;
4494
4495 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4496 return CMP_UNKNOWN;
4497
4498 if (a->ts.type != BT_INTEGER)
4499 gfc_internal_error ("compare_bound_int(): Bad expression");
4500
4501 i = mpz_cmp (a->value.integer, b);
4502
4503 if (i < 0)
4504 return CMP_LT;
4505 if (i > 0)
4506 return CMP_GT;
4507 return CMP_EQ;
4508 }
4509
4510
4511 /* Compute the last value of a sequence given by a triplet.
4512 Return 0 if it wasn't able to compute the last value, or if the
4513 sequence if empty, and 1 otherwise. */
4514
4515 static int
4516 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4517 gfc_expr *stride, mpz_t last)
4518 {
4519 mpz_t rem;
4520
4521 if (start == NULL || start->expr_type != EXPR_CONSTANT
4522 || end == NULL || end->expr_type != EXPR_CONSTANT
4523 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4524 return 0;
4525
4526 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4527 || (stride != NULL && stride->ts.type != BT_INTEGER))
4528 return 0;
4529
4530 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
4531 {
4532 if (compare_bound (start, end) == CMP_GT)
4533 return 0;
4534 mpz_set (last, end->value.integer);
4535 return 1;
4536 }
4537
4538 if (compare_bound_int (stride, 0) == CMP_GT)
4539 {
4540 /* Stride is positive */
4541 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4542 return 0;
4543 }
4544 else
4545 {
4546 /* Stride is negative */
4547 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4548 return 0;
4549 }
4550
4551 mpz_init (rem);
4552 mpz_sub (rem, end->value.integer, start->value.integer);
4553 mpz_tdiv_r (rem, rem, stride->value.integer);
4554 mpz_sub (last, end->value.integer, rem);
4555 mpz_clear (rem);
4556
4557 return 1;
4558 }
4559
4560
4561 /* Compare a single dimension of an array reference to the array
4562 specification. */
4563
4564 static bool
4565 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4566 {
4567 mpz_t last_value;
4568
4569 if (ar->dimen_type[i] == DIMEN_STAR)
4570 {
4571 gcc_assert (ar->stride[i] == NULL);
4572 /* This implies [*] as [*:] and [*:3] are not possible. */
4573 if (ar->start[i] == NULL)
4574 {
4575 gcc_assert (ar->end[i] == NULL);
4576 return true;
4577 }
4578 }
4579
4580 /* Given start, end and stride values, calculate the minimum and
4581 maximum referenced indexes. */
4582
4583 switch (ar->dimen_type[i])
4584 {
4585 case DIMEN_VECTOR:
4586 case DIMEN_THIS_IMAGE:
4587 break;
4588
4589 case DIMEN_STAR:
4590 case DIMEN_ELEMENT:
4591 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4592 {
4593 if (i < as->rank)
4594 gfc_warning (0, "Array reference at %L is out of bounds "
4595 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4596 mpz_get_si (ar->start[i]->value.integer),
4597 mpz_get_si (as->lower[i]->value.integer), i+1);
4598 else
4599 gfc_warning (0, "Array reference at %L is out of bounds "
4600 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4601 mpz_get_si (ar->start[i]->value.integer),
4602 mpz_get_si (as->lower[i]->value.integer),
4603 i + 1 - as->rank);
4604 return true;
4605 }
4606 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4607 {
4608 if (i < as->rank)
4609 gfc_warning (0, "Array reference at %L is out of bounds "
4610 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4611 mpz_get_si (ar->start[i]->value.integer),
4612 mpz_get_si (as->upper[i]->value.integer), i+1);
4613 else
4614 gfc_warning (0, "Array reference at %L is out of bounds "
4615 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4616 mpz_get_si (ar->start[i]->value.integer),
4617 mpz_get_si (as->upper[i]->value.integer),
4618 i + 1 - as->rank);
4619 return true;
4620 }
4621
4622 break;
4623
4624 case DIMEN_RANGE:
4625 {
4626 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4627 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4628
4629 compare_result comp_start_end = compare_bound (AR_START, AR_END);
4630
4631 /* Check for zero stride, which is not allowed. */
4632 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4633 {
4634 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4635 return false;
4636 }
4637
4638 /* if start == len || (stride > 0 && start < len)
4639 || (stride < 0 && start > len),
4640 then the array section contains at least one element. In this
4641 case, there is an out-of-bounds access if
4642 (start < lower || start > upper). */
4643 if (compare_bound (AR_START, AR_END) == CMP_EQ
4644 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4645 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4646 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4647 && comp_start_end == CMP_GT))
4648 {
4649 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4650 {
4651 gfc_warning (0, "Lower array reference at %L is out of bounds "
4652 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4653 mpz_get_si (AR_START->value.integer),
4654 mpz_get_si (as->lower[i]->value.integer), i+1);
4655 return true;
4656 }
4657 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4658 {
4659 gfc_warning (0, "Lower array reference at %L is out of bounds "
4660 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4661 mpz_get_si (AR_START->value.integer),
4662 mpz_get_si (as->upper[i]->value.integer), i+1);
4663 return true;
4664 }
4665 }
4666
4667 /* If we can compute the highest index of the array section,
4668 then it also has to be between lower and upper. */
4669 mpz_init (last_value);
4670 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4671 last_value))
4672 {
4673 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4674 {
4675 gfc_warning (0, "Upper array reference at %L is out of bounds "
4676 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4677 mpz_get_si (last_value),
4678 mpz_get_si (as->lower[i]->value.integer), i+1);
4679 mpz_clear (last_value);
4680 return true;
4681 }
4682 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4683 {
4684 gfc_warning (0, "Upper array reference at %L is out of bounds "
4685 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4686 mpz_get_si (last_value),
4687 mpz_get_si (as->upper[i]->value.integer), i+1);
4688 mpz_clear (last_value);
4689 return true;
4690 }
4691 }
4692 mpz_clear (last_value);
4693
4694 #undef AR_START
4695 #undef AR_END
4696 }
4697 break;
4698
4699 default:
4700 gfc_internal_error ("check_dimension(): Bad array reference");
4701 }
4702
4703 return true;
4704 }
4705
4706
4707 /* Compare an array reference with an array specification. */
4708
4709 static bool
4710 compare_spec_to_ref (gfc_array_ref *ar)
4711 {
4712 gfc_array_spec *as;
4713 int i;
4714
4715 as = ar->as;
4716 i = as->rank - 1;
4717 /* TODO: Full array sections are only allowed as actual parameters. */
4718 if (as->type == AS_ASSUMED_SIZE
4719 && (/*ar->type == AR_FULL
4720 ||*/ (ar->type == AR_SECTION
4721 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4722 {
4723 gfc_error ("Rightmost upper bound of assumed size array section "
4724 "not specified at %L", &ar->where);
4725 return false;
4726 }
4727
4728 if (ar->type == AR_FULL)
4729 return true;
4730
4731 if (as->rank != ar->dimen)
4732 {
4733 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4734 &ar->where, ar->dimen, as->rank);
4735 return false;
4736 }
4737
4738 /* ar->codimen == 0 is a local array. */
4739 if (as->corank != ar->codimen && ar->codimen != 0)
4740 {
4741 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4742 &ar->where, ar->codimen, as->corank);
4743 return false;
4744 }
4745
4746 for (i = 0; i < as->rank; i++)
4747 if (!check_dimension (i, ar, as))
4748 return false;
4749
4750 /* Local access has no coarray spec. */
4751 if (ar->codimen != 0)
4752 for (i = as->rank; i < as->rank + as->corank; i++)
4753 {
4754 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4755 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4756 {
4757 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4758 i + 1 - as->rank, &ar->where);
4759 return false;
4760 }
4761 if (!check_dimension (i, ar, as))
4762 return false;
4763 }
4764
4765 return true;
4766 }
4767
4768
4769 /* Resolve one part of an array index. */
4770
4771 static bool
4772 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4773 int force_index_integer_kind)
4774 {
4775 gfc_typespec ts;
4776
4777 if (index == NULL)
4778 return true;
4779
4780 if (!gfc_resolve_expr (index))
4781 return false;
4782
4783 if (check_scalar && index->rank != 0)
4784 {
4785 gfc_error ("Array index at %L must be scalar", &index->where);
4786 return false;
4787 }
4788
4789 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4790 {
4791 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4792 &index->where, gfc_basic_typename (index->ts.type));
4793 return false;
4794 }
4795
4796 if (index->ts.type == BT_REAL)
4797 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4798 &index->where))
4799 return false;
4800
4801 if ((index->ts.kind != gfc_index_integer_kind
4802 && force_index_integer_kind)
4803 || index->ts.type != BT_INTEGER)
4804 {
4805 gfc_clear_ts (&ts);
4806 ts.type = BT_INTEGER;
4807 ts.kind = gfc_index_integer_kind;
4808
4809 gfc_convert_type_warn (index, &ts, 2, 0);
4810 }
4811
4812 return true;
4813 }
4814
4815 /* Resolve one part of an array index. */
4816
4817 bool
4818 gfc_resolve_index (gfc_expr *index, int check_scalar)
4819 {
4820 return gfc_resolve_index_1 (index, check_scalar, 1);
4821 }
4822
4823 /* Resolve a dim argument to an intrinsic function. */
4824
4825 bool
4826 gfc_resolve_dim_arg (gfc_expr *dim)
4827 {
4828 if (dim == NULL)
4829 return true;
4830
4831 if (!gfc_resolve_expr (dim))
4832 return false;
4833
4834 if (dim->rank != 0)
4835 {
4836 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4837 return false;
4838
4839 }
4840
4841 if (dim->ts.type != BT_INTEGER)
4842 {
4843 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4844 return false;
4845 }
4846
4847 if (dim->ts.kind != gfc_index_integer_kind)
4848 {
4849 gfc_typespec ts;
4850
4851 gfc_clear_ts (&ts);
4852 ts.type = BT_INTEGER;
4853 ts.kind = gfc_index_integer_kind;
4854
4855 gfc_convert_type_warn (dim, &ts, 2, 0);
4856 }
4857
4858 return true;
4859 }
4860
4861 /* Given an expression that contains array references, update those array
4862 references to point to the right array specifications. While this is
4863 filled in during matching, this information is difficult to save and load
4864 in a module, so we take care of it here.
4865
4866 The idea here is that the original array reference comes from the
4867 base symbol. We traverse the list of reference structures, setting
4868 the stored reference to references. Component references can
4869 provide an additional array specification. */
4870
4871 static void
4872 find_array_spec (gfc_expr *e)
4873 {
4874 gfc_array_spec *as;
4875 gfc_component *c;
4876 gfc_ref *ref;
4877 bool class_as = false;
4878
4879 if (e->symtree->n.sym->ts.type == BT_CLASS)
4880 {
4881 as = CLASS_DATA (e->symtree->n.sym)->as;
4882 class_as = true;
4883 }
4884 else
4885 as = e->symtree->n.sym->as;
4886
4887 for (ref = e->ref; ref; ref = ref->next)
4888 switch (ref->type)
4889 {
4890 case REF_ARRAY:
4891 if (as == NULL)
4892 gfc_internal_error ("find_array_spec(): Missing spec");
4893
4894 ref->u.ar.as = as;
4895 as = NULL;
4896 break;
4897
4898 case REF_COMPONENT:
4899 c = ref->u.c.component;
4900 if (c->attr.dimension)
4901 {
4902 if (as != NULL && !(class_as && as == c->as))
4903 gfc_internal_error ("find_array_spec(): unused as(1)");
4904 as = c->as;
4905 }
4906
4907 break;
4908
4909 case REF_SUBSTRING:
4910 case REF_INQUIRY:
4911 break;
4912 }
4913
4914 if (as != NULL)
4915 gfc_internal_error ("find_array_spec(): unused as(2)");
4916 }
4917
4918
4919 /* Resolve an array reference. */
4920
4921 static bool
4922 resolve_array_ref (gfc_array_ref *ar)
4923 {
4924 int i, check_scalar;
4925 gfc_expr *e;
4926
4927 for (i = 0; i < ar->dimen + ar->codimen; i++)
4928 {
4929 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4930
4931 /* Do not force gfc_index_integer_kind for the start. We can
4932 do fine with any integer kind. This avoids temporary arrays
4933 created for indexing with a vector. */
4934 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4935 return false;
4936 if (!gfc_resolve_index (ar->end[i], check_scalar))
4937 return false;
4938 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4939 return false;
4940
4941 e = ar->start[i];
4942
4943 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4944 switch (e->rank)
4945 {
4946 case 0:
4947 ar->dimen_type[i] = DIMEN_ELEMENT;
4948 break;
4949
4950 case 1:
4951 ar->dimen_type[i] = DIMEN_VECTOR;
4952 if (e->expr_type == EXPR_VARIABLE
4953 && e->symtree->n.sym->ts.type == BT_DERIVED)
4954 ar->start[i] = gfc_get_parentheses (e);
4955 break;
4956
4957 default:
4958 gfc_error ("Array index at %L is an array of rank %d",
4959 &ar->c_where[i], e->rank);
4960 return false;
4961 }
4962
4963 /* Fill in the upper bound, which may be lower than the
4964 specified one for something like a(2:10:5), which is
4965 identical to a(2:7:5). Only relevant for strides not equal
4966 to one. Don't try a division by zero. */
4967 if (ar->dimen_type[i] == DIMEN_RANGE
4968 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4969 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4970 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4971 {
4972 mpz_t size, end;
4973
4974 if (gfc_ref_dimen_size (ar, i, &size, &end))
4975 {
4976 if (ar->end[i] == NULL)
4977 {
4978 ar->end[i] =
4979 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4980 &ar->where);
4981 mpz_set (ar->end[i]->value.integer, end);
4982 }
4983 else if (ar->end[i]->ts.type == BT_INTEGER
4984 && ar->end[i]->expr_type == EXPR_CONSTANT)
4985 {
4986 mpz_set (ar->end[i]->value.integer, end);
4987 }
4988 else
4989 gcc_unreachable ();
4990
4991 mpz_clear (size);
4992 mpz_clear (end);
4993 }
4994 }
4995 }
4996
4997 if (ar->type == AR_FULL)
4998 {
4999 if (ar->as->rank == 0)
5000 ar->type = AR_ELEMENT;
5001
5002 /* Make sure array is the same as array(:,:), this way
5003 we don't need to special case all the time. */
5004 ar->dimen = ar->as->rank;
5005 for (i = 0; i < ar->dimen; i++)
5006 {
5007 ar->dimen_type[i] = DIMEN_RANGE;
5008
5009 gcc_assert (ar->start[i] == NULL);
5010 gcc_assert (ar->end[i] == NULL);
5011 gcc_assert (ar->stride[i] == NULL);
5012 }
5013 }
5014
5015 /* If the reference type is unknown, figure out what kind it is. */
5016
5017 if (ar->type == AR_UNKNOWN)
5018 {
5019 ar->type = AR_ELEMENT;
5020 for (i = 0; i < ar->dimen; i++)
5021 if (ar->dimen_type[i] == DIMEN_RANGE
5022 || ar->dimen_type[i] == DIMEN_VECTOR)
5023 {
5024 ar->type = AR_SECTION;
5025 break;
5026 }
5027 }
5028
5029 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
5030 return false;
5031
5032 if (ar->as->corank && ar->codimen == 0)
5033 {
5034 int n;
5035 ar->codimen = ar->as->corank;
5036 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
5037 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
5038 }
5039
5040 return true;
5041 }
5042
5043
5044 static bool
5045 resolve_substring (gfc_ref *ref, bool *equal_length)
5046 {
5047 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
5048
5049 if (ref->u.ss.start != NULL)
5050 {
5051 if (!gfc_resolve_expr (ref->u.ss.start))
5052 return false;
5053
5054 if (ref->u.ss.start->ts.type != BT_INTEGER)
5055 {
5056 gfc_error ("Substring start index at %L must be of type INTEGER",
5057 &ref->u.ss.start->where);
5058 return false;
5059 }
5060
5061 if (ref->u.ss.start->rank != 0)
5062 {
5063 gfc_error ("Substring start index at %L must be scalar",
5064 &ref->u.ss.start->where);
5065 return false;
5066 }
5067
5068 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
5069 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5070 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5071 {
5072 gfc_error ("Substring start index at %L is less than one",
5073 &ref->u.ss.start->where);
5074 return false;
5075 }
5076 }
5077
5078 if (ref->u.ss.end != NULL)
5079 {
5080 if (!gfc_resolve_expr (ref->u.ss.end))
5081 return false;
5082
5083 if (ref->u.ss.end->ts.type != BT_INTEGER)
5084 {
5085 gfc_error ("Substring end index at %L must be of type INTEGER",
5086 &ref->u.ss.end->where);
5087 return false;
5088 }
5089
5090 if (ref->u.ss.end->rank != 0)
5091 {
5092 gfc_error ("Substring end index at %L must be scalar",
5093 &ref->u.ss.end->where);
5094 return false;
5095 }
5096
5097 if (ref->u.ss.length != NULL
5098 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
5099 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5100 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5101 {
5102 gfc_error ("Substring end index at %L exceeds the string length",
5103 &ref->u.ss.start->where);
5104 return false;
5105 }
5106
5107 if (compare_bound_mpz_t (ref->u.ss.end,
5108 gfc_integer_kinds[k].huge) == CMP_GT
5109 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5110 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5111 {
5112 gfc_error ("Substring end index at %L is too large",
5113 &ref->u.ss.end->where);
5114 return false;
5115 }
5116 /* If the substring has the same length as the original
5117 variable, the reference itself can be deleted. */
5118
5119 if (ref->u.ss.length != NULL
5120 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
5121 && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
5122 *equal_length = true;
5123 }
5124
5125 return true;
5126 }
5127
5128
5129 /* This function supplies missing substring charlens. */
5130
5131 void
5132 gfc_resolve_substring_charlen (gfc_expr *e)
5133 {
5134 gfc_ref *char_ref;
5135 gfc_expr *start, *end;
5136 gfc_typespec *ts = NULL;
5137 mpz_t diff;
5138
5139 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
5140 {
5141 if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
5142 break;
5143 if (char_ref->type == REF_COMPONENT)
5144 ts = &char_ref->u.c.component->ts;
5145 }
5146
5147 if (!char_ref || char_ref->type == REF_INQUIRY)
5148 return;
5149
5150 gcc_assert (char_ref->next == NULL);
5151
5152 if (e->ts.u.cl)
5153 {
5154 if (e->ts.u.cl->length)
5155 gfc_free_expr (e->ts.u.cl->length);
5156 else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
5157 return;
5158 }
5159
5160 if (!e->ts.u.cl)
5161 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5162
5163 if (char_ref->u.ss.start)
5164 start = gfc_copy_expr (char_ref->u.ss.start);
5165 else
5166 start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
5167
5168 if (char_ref->u.ss.end)
5169 end = gfc_copy_expr (char_ref->u.ss.end);
5170 else if (e->expr_type == EXPR_VARIABLE)
5171 {
5172 if (!ts)
5173 ts = &e->symtree->n.sym->ts;
5174 end = gfc_copy_expr (ts->u.cl->length);
5175 }
5176 else
5177 end = NULL;
5178
5179 if (!start || !end)
5180 {
5181 gfc_free_expr (start);
5182 gfc_free_expr (end);
5183 return;
5184 }
5185
5186 /* Length = (end - start + 1).
5187 Check first whether it has a constant length. */
5188 if (gfc_dep_difference (end, start, &diff))
5189 {
5190 gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
5191 &e->where);
5192
5193 mpz_add_ui (len->value.integer, diff, 1);
5194 mpz_clear (diff);
5195 e->ts.u.cl->length = len;
5196 /* The check for length < 0 is handled below */
5197 }
5198 else
5199 {
5200 e->ts.u.cl->length = gfc_subtract (end, start);
5201 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
5202 gfc_get_int_expr (gfc_charlen_int_kind,
5203 NULL, 1));
5204 }
5205
5206 /* F2008, 6.4.1: Both the starting point and the ending point shall
5207 be within the range 1, 2, ..., n unless the starting point exceeds
5208 the ending point, in which case the substring has length zero. */
5209
5210 if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
5211 mpz_set_si (e->ts.u.cl->length->value.integer, 0);
5212
5213 e->ts.u.cl->length->ts.type = BT_INTEGER;
5214 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5215
5216 /* Make sure that the length is simplified. */
5217 gfc_simplify_expr (e->ts.u.cl->length, 1);
5218 gfc_resolve_expr (e->ts.u.cl->length);
5219 }
5220
5221
5222 /* Resolve subtype references. */
5223
5224 bool
5225 gfc_resolve_ref (gfc_expr *expr)
5226 {
5227 int current_part_dimension, n_components, seen_part_dimension, dim;
5228 gfc_ref *ref, **prev, *array_ref;
5229 bool equal_length;
5230
5231 for (ref = expr->ref; ref; ref = ref->next)
5232 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
5233 {
5234 find_array_spec (expr);
5235 break;
5236 }
5237
5238 for (prev = &expr->ref; *prev != NULL;
5239 prev = *prev == NULL ? prev : &(*prev)->next)
5240 switch ((*prev)->type)
5241 {
5242 case REF_ARRAY:
5243 if (!resolve_array_ref (&(*prev)->u.ar))
5244 return false;
5245 break;
5246
5247 case REF_COMPONENT:
5248 case REF_INQUIRY:
5249 break;
5250
5251 case REF_SUBSTRING:
5252 equal_length = false;
5253 if (!resolve_substring (*prev, &equal_length))
5254 return false;
5255
5256 if (expr->expr_type != EXPR_SUBSTRING && equal_length)
5257 {
5258 /* Remove the reference and move the charlen, if any. */
5259 ref = *prev;
5260 *prev = ref->next;
5261 ref->next = NULL;
5262 expr->ts.u.cl = ref->u.ss.length;
5263 ref->u.ss.length = NULL;
5264 gfc_free_ref_list (ref);
5265 }
5266 break;
5267 }
5268
5269 /* Check constraints on part references. */
5270
5271 current_part_dimension = 0;
5272 seen_part_dimension = 0;
5273 n_components = 0;
5274 array_ref = NULL;
5275
5276 for (ref = expr->ref; ref; ref = ref->next)
5277 {
5278 switch (ref->type)
5279 {
5280 case REF_ARRAY:
5281 array_ref = ref;
5282 switch (ref->u.ar.type)
5283 {
5284 case AR_FULL:
5285 /* Coarray scalar. */
5286 if (ref->u.ar.as->rank == 0)
5287 {
5288 current_part_dimension = 0;
5289 break;
5290 }
5291 /* Fall through. */
5292 case AR_SECTION:
5293 current_part_dimension = 1;
5294 break;
5295
5296 case AR_ELEMENT:
5297 array_ref = NULL;
5298 current_part_dimension = 0;
5299 break;
5300
5301 case AR_UNKNOWN:
5302 gfc_internal_error ("resolve_ref(): Bad array reference");
5303 }
5304
5305 break;
5306
5307 case REF_COMPONENT:
5308 if (current_part_dimension || seen_part_dimension)
5309 {
5310 /* F03:C614. */
5311 if (ref->u.c.component->attr.pointer
5312 || ref->u.c.component->attr.proc_pointer
5313 || (ref->u.c.component->ts.type == BT_CLASS
5314 && CLASS_DATA (ref->u.c.component)->attr.pointer))
5315 {
5316 gfc_error ("Component to the right of a part reference "
5317 "with nonzero rank must not have the POINTER "
5318 "attribute at %L", &expr->where);
5319 return false;
5320 }
5321 else if (ref->u.c.component->attr.allocatable
5322 || (ref->u.c.component->ts.type == BT_CLASS
5323 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
5324
5325 {
5326 gfc_error ("Component to the right of a part reference "
5327 "with nonzero rank must not have the ALLOCATABLE "
5328 "attribute at %L", &expr->where);
5329 return false;
5330 }
5331 }
5332
5333 n_components++;
5334 break;
5335
5336 case REF_SUBSTRING:
5337 break;
5338
5339 case REF_INQUIRY:
5340 /* Implement requirement in note 9.7 of F2018 that the result of the
5341 LEN inquiry be a scalar. */
5342 if (ref->u.i == INQUIRY_LEN && array_ref && expr->ts.deferred)
5343 {
5344 array_ref->u.ar.type = AR_ELEMENT;
5345 expr->rank = 0;
5346 /* INQUIRY_LEN is not evaluated from the rest of the expr
5347 but directly from the string length. This means that setting
5348 the array indices to one does not matter but might trigger
5349 a runtime bounds error. Suppress the check. */
5350 expr->no_bounds_check = 1;
5351 for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
5352 {
5353 array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
5354 if (array_ref->u.ar.start[dim])
5355 gfc_free_expr (array_ref->u.ar.start[dim]);
5356 array_ref->u.ar.start[dim]
5357 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
5358 if (array_ref->u.ar.end[dim])
5359 gfc_free_expr (array_ref->u.ar.end[dim]);
5360 if (array_ref->u.ar.stride[dim])
5361 gfc_free_expr (array_ref->u.ar.stride[dim]);
5362 }
5363 }
5364 break;
5365 }
5366
5367 if (((ref->type == REF_COMPONENT && n_components > 1)
5368 || ref->next == NULL)
5369 && current_part_dimension
5370 && seen_part_dimension)
5371 {
5372 gfc_error ("Two or more part references with nonzero rank must "
5373 "not be specified at %L", &expr->where);
5374 return false;
5375 }
5376
5377 if (ref->type == REF_COMPONENT)
5378 {
5379 if (current_part_dimension)
5380 seen_part_dimension = 1;
5381
5382 /* reset to make sure */
5383 current_part_dimension = 0;
5384 }
5385 }
5386
5387 return true;
5388 }
5389
5390
5391 /* Given an expression, determine its shape. This is easier than it sounds.
5392 Leaves the shape array NULL if it is not possible to determine the shape. */
5393
5394 static void
5395 expression_shape (gfc_expr *e)
5396 {
5397 mpz_t array[GFC_MAX_DIMENSIONS];
5398 int i;
5399
5400 if (e->rank <= 0 || e->shape != NULL)
5401 return;
5402
5403 for (i = 0; i < e->rank; i++)
5404 if (!gfc_array_dimen_size (e, i, &array[i]))
5405 goto fail;
5406
5407 e->shape = gfc_get_shape (e->rank);
5408
5409 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
5410
5411 return;
5412
5413 fail:
5414 for (i--; i >= 0; i--)
5415 mpz_clear (array[i]);
5416 }
5417
5418
5419 /* Given a variable expression node, compute the rank of the expression by
5420 examining the base symbol and any reference structures it may have. */
5421
5422 void
5423 gfc_expression_rank (gfc_expr *e)
5424 {
5425 gfc_ref *ref;
5426 int i, rank;
5427
5428 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
5429 could lead to serious confusion... */
5430 gcc_assert (e->expr_type != EXPR_COMPCALL);
5431
5432 if (e->ref == NULL)
5433 {
5434 if (e->expr_type == EXPR_ARRAY)
5435 goto done;
5436 /* Constructors can have a rank different from one via RESHAPE(). */
5437
5438 e->rank = ((e->symtree == NULL || e->symtree->n.sym->as == NULL)
5439 ? 0 : e->symtree->n.sym->as->rank);
5440 goto done;
5441 }
5442
5443 rank = 0;
5444
5445 for (ref = e->ref; ref; ref = ref->next)
5446 {
5447 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
5448 && ref->u.c.component->attr.function && !ref->next)
5449 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
5450
5451 if (ref->type != REF_ARRAY)
5452 continue;
5453
5454 if (ref->u.ar.type == AR_FULL)
5455 {
5456 rank = ref->u.ar.as->rank;
5457 break;
5458 }
5459
5460 if (ref->u.ar.type == AR_SECTION)
5461 {
5462 /* Figure out the rank of the section. */
5463 if (rank != 0)
5464 gfc_internal_error ("gfc_expression_rank(): Two array specs");
5465
5466 for (i = 0; i < ref->u.ar.dimen; i++)
5467 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5468 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
5469 rank++;
5470
5471 break;
5472 }
5473 }
5474
5475 e->rank = rank;
5476
5477 done:
5478 expression_shape (e);
5479 }
5480
5481
5482 static void
5483 add_caf_get_intrinsic (gfc_expr *e)
5484 {
5485 gfc_expr *wrapper, *tmp_expr;
5486 gfc_ref *ref;
5487 int n;
5488
5489 for (ref = e->ref; ref; ref = ref->next)
5490 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5491 break;
5492 if (ref == NULL)
5493 return;
5494
5495 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
5496 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
5497 return;
5498
5499 tmp_expr = XCNEW (gfc_expr);
5500 *tmp_expr = *e;
5501 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
5502 "caf_get", tmp_expr->where, 1, tmp_expr);
5503 wrapper->ts = e->ts;
5504 wrapper->rank = e->rank;
5505 if (e->rank)
5506 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
5507 *e = *wrapper;
5508 free (wrapper);
5509 }
5510
5511
5512 static void
5513 remove_caf_get_intrinsic (gfc_expr *e)
5514 {
5515 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym
5516 && e->value.function.isym->id == GFC_ISYM_CAF_GET);
5517 gfc_expr *e2 = e->value.function.actual->expr;
5518 e->value.function.actual->expr = NULL;
5519 gfc_free_actual_arglist (e->value.function.actual);
5520 gfc_free_shape (&e->shape, e->rank);
5521 *e = *e2;
5522 free (e2);
5523 }
5524
5525
5526 /* Resolve a variable expression. */
5527
5528 static bool
5529 resolve_variable (gfc_expr *e)
5530 {
5531 gfc_symbol *sym;
5532 bool t;
5533
5534 t = true;
5535
5536 if (e->symtree == NULL)
5537 return false;
5538 sym = e->symtree->n.sym;
5539
5540 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
5541 as ts.type is set to BT_ASSUMED in resolve_symbol. */
5542 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
5543 {
5544 if (!actual_arg || inquiry_argument)
5545 {
5546 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
5547 "be used as actual argument", sym->name, &e->where);
5548 return false;
5549 }
5550 }
5551 /* TS 29113, 407b. */
5552 else if (e->ts.type == BT_ASSUMED)
5553 {
5554 if (!actual_arg)
5555 {
5556 gfc_error ("Assumed-type variable %s at %L may only be used "
5557 "as actual argument", sym->name, &e->where);
5558 return false;
5559 }
5560 else if (inquiry_argument && !first_actual_arg)
5561 {
5562 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5563 for all inquiry functions in resolve_function; the reason is
5564 that the function-name resolution happens too late in that
5565 function. */
5566 gfc_error ("Assumed-type variable %s at %L as actual argument to "
5567 "an inquiry function shall be the first argument",
5568 sym->name, &e->where);
5569 return false;
5570 }
5571 }
5572 /* TS 29113, C535b. */
5573 else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5574 && CLASS_DATA (sym)->as
5575 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5576 || (sym->ts.type != BT_CLASS && sym->as
5577 && sym->as->type == AS_ASSUMED_RANK))
5578 && !sym->attr.select_rank_temporary)
5579 {
5580 if (!actual_arg
5581 && !(cs_base && cs_base->current
5582 && cs_base->current->op == EXEC_SELECT_RANK))
5583 {
5584 gfc_error ("Assumed-rank variable %s at %L may only be used as "
5585 "actual argument", sym->name, &e->where);
5586 return false;
5587 }
5588 else if (inquiry_argument && !first_actual_arg)
5589 {
5590 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5591 for all inquiry functions in resolve_function; the reason is
5592 that the function-name resolution happens too late in that
5593 function. */
5594 gfc_error ("Assumed-rank variable %s at %L as actual argument "
5595 "to an inquiry function shall be the first argument",
5596 sym->name, &e->where);
5597 return false;
5598 }
5599 }
5600
5601 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
5602 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5603 && e->ref->next == NULL))
5604 {
5605 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
5606 "a subobject reference", sym->name, &e->ref->u.ar.where);
5607 return false;
5608 }
5609 /* TS 29113, 407b. */
5610 else if (e->ts.type == BT_ASSUMED && e->ref
5611 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5612 && e->ref->next == NULL))
5613 {
5614 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
5615 "reference", sym->name, &e->ref->u.ar.where);
5616 return false;
5617 }
5618
5619 /* TS 29113, C535b. */
5620 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5621 && CLASS_DATA (sym)->as
5622 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5623 || (sym->ts.type != BT_CLASS && sym->as
5624 && sym->as->type == AS_ASSUMED_RANK))
5625 && e->ref
5626 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5627 && e->ref->next == NULL))
5628 {
5629 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
5630 "reference", sym->name, &e->ref->u.ar.where);
5631 return false;
5632 }
5633
5634 /* For variables that are used in an associate (target => object) where
5635 the object's basetype is array valued while the target is scalar,
5636 the ts' type of the component refs is still array valued, which
5637 can't be translated that way. */
5638 if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
5639 && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
5640 && CLASS_DATA (sym->assoc->target)->as)
5641 {
5642 gfc_ref *ref = e->ref;
5643 while (ref)
5644 {
5645 switch (ref->type)
5646 {
5647 case REF_COMPONENT:
5648 ref->u.c.sym = sym->ts.u.derived;
5649 /* Stop the loop. */
5650 ref = NULL;
5651 break;
5652 default:
5653 ref = ref->next;
5654 break;
5655 }
5656 }
5657 }
5658
5659 /* If this is an associate-name, it may be parsed with an array reference
5660 in error even though the target is scalar. Fail directly in this case.
5661 TODO Understand why class scalar expressions must be excluded. */
5662 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
5663 {
5664 if (sym->ts.type == BT_CLASS)
5665 gfc_fix_class_refs (e);
5666 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
5667 return false;
5668 else if (sym->attr.dimension && (!e->ref || e->ref->type != REF_ARRAY))
5669 {
5670 /* This can happen because the parser did not detect that the
5671 associate name is an array and the expression had no array
5672 part_ref. */
5673 gfc_ref *ref = gfc_get_ref ();
5674 ref->type = REF_ARRAY;
5675 ref->u.ar = *gfc_get_array_ref();
5676 ref->u.ar.type = AR_FULL;
5677 if (sym->as)
5678 {
5679 ref->u.ar.as = sym->as;
5680 ref->u.ar.dimen = sym->as->rank;
5681 }
5682 ref->next = e->ref;
5683 e->ref = ref;
5684
5685 }
5686 }
5687
5688 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
5689 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
5690
5691 /* On the other hand, the parser may not have known this is an array;
5692 in this case, we have to add a FULL reference. */
5693 if (sym->assoc && sym->attr.dimension && !e->ref)
5694 {
5695 e->ref = gfc_get_ref ();
5696 e->ref->type = REF_ARRAY;
5697 e->ref->u.ar.type = AR_FULL;
5698 e->ref->u.ar.dimen = 0;
5699 }
5700
5701 /* Like above, but for class types, where the checking whether an array
5702 ref is present is more complicated. Furthermore make sure not to add
5703 the full array ref to _vptr or _len refs. */
5704 if (sym->assoc && sym->ts.type == BT_CLASS
5705 && CLASS_DATA (sym)->attr.dimension
5706 && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
5707 {
5708 gfc_ref *ref, *newref;
5709
5710 newref = gfc_get_ref ();
5711 newref->type = REF_ARRAY;
5712 newref->u.ar.type = AR_FULL;
5713 newref->u.ar.dimen = 0;
5714 /* Because this is an associate var and the first ref either is a ref to
5715 the _data component or not, no traversal of the ref chain is
5716 needed. The array ref needs to be inserted after the _data ref,
5717 or when that is not present, which may happend for polymorphic
5718 types, then at the first position. */
5719 ref = e->ref;
5720 if (!ref)
5721 e->ref = newref;
5722 else if (ref->type == REF_COMPONENT
5723 && strcmp ("_data", ref->u.c.component->name) == 0)
5724 {
5725 if (!ref->next || ref->next->type != REF_ARRAY)
5726 {
5727 newref->next = ref->next;
5728 ref->next = newref;
5729 }
5730 else
5731 /* Array ref present already. */
5732 gfc_free_ref_list (newref);
5733 }
5734 else if (ref->type == REF_ARRAY)
5735 /* Array ref present already. */
5736 gfc_free_ref_list (newref);
5737 else
5738 {
5739 newref->next = ref;
5740 e->ref = newref;
5741 }
5742 }
5743
5744 if (e->ref && !gfc_resolve_ref (e))
5745 return false;
5746
5747 if (sym->attr.flavor == FL_PROCEDURE
5748 && (!sym->attr.function
5749 || (sym->attr.function && sym->result
5750 && sym->result->attr.proc_pointer
5751 && !sym->result->attr.function)))
5752 {
5753 e->ts.type = BT_PROCEDURE;
5754 goto resolve_procedure;
5755 }
5756
5757 if (sym->ts.type != BT_UNKNOWN)
5758 gfc_variable_attr (e, &e->ts);
5759 else if (sym->attr.flavor == FL_PROCEDURE
5760 && sym->attr.function && sym->result
5761 && sym->result->ts.type != BT_UNKNOWN
5762 && sym->result->attr.proc_pointer)
5763 e->ts = sym->result->ts;
5764 else
5765 {
5766 /* Must be a simple variable reference. */
5767 if (!gfc_set_default_type (sym, 1, sym->ns))
5768 return false;
5769 e->ts = sym->ts;
5770 }
5771
5772 if (check_assumed_size_reference (sym, e))
5773 return false;
5774
5775 /* Deal with forward references to entries during gfc_resolve_code, to
5776 satisfy, at least partially, 12.5.2.5. */
5777 if (gfc_current_ns->entries
5778 && current_entry_id == sym->entry_id
5779 && cs_base
5780 && cs_base->current
5781 && cs_base->current->op != EXEC_ENTRY)
5782 {
5783 gfc_entry_list *entry;
5784 gfc_formal_arglist *formal;
5785 int n;
5786 bool seen, saved_specification_expr;
5787
5788 /* If the symbol is a dummy... */
5789 if (sym->attr.dummy && sym->ns == gfc_current_ns)
5790 {
5791 entry = gfc_current_ns->entries;
5792 seen = false;
5793
5794 /* ...test if the symbol is a parameter of previous entries. */
5795 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5796 for (formal = entry->sym->formal; formal; formal = formal->next)
5797 {
5798 if (formal->sym && sym->name == formal->sym->name)
5799 {
5800 seen = true;
5801 break;
5802 }
5803 }
5804
5805 /* If it has not been seen as a dummy, this is an error. */
5806 if (!seen)
5807 {
5808 if (specification_expr)
5809 gfc_error ("Variable %qs, used in a specification expression"
5810 ", is referenced at %L before the ENTRY statement "
5811 "in which it is a parameter",
5812 sym->name, &cs_base->current->loc);
5813 else
5814 gfc_error ("Variable %qs is used at %L before the ENTRY "
5815 "statement in which it is a parameter",
5816 sym->name, &cs_base->current->loc);
5817 t = false;
5818 }
5819 }
5820
5821 /* Now do the same check on the specification expressions. */
5822 saved_specification_expr = specification_expr;
5823 specification_expr = true;
5824 if (sym->ts.type == BT_CHARACTER
5825 && !gfc_resolve_expr (sym->ts.u.cl->length))
5826 t = false;
5827
5828 if (sym->as)
5829 for (n = 0; n < sym->as->rank; n++)
5830 {
5831 if (!gfc_resolve_expr (sym->as->lower[n]))
5832 t = false;
5833 if (!gfc_resolve_expr (sym->as->upper[n]))
5834 t = false;
5835 }
5836 specification_expr = saved_specification_expr;
5837
5838 if (t)
5839 /* Update the symbol's entry level. */
5840 sym->entry_id = current_entry_id + 1;
5841 }
5842
5843 /* If a symbol has been host_associated mark it. This is used latter,
5844 to identify if aliasing is possible via host association. */
5845 if (sym->attr.flavor == FL_VARIABLE
5846 && gfc_current_ns->parent
5847 && (gfc_current_ns->parent == sym->ns
5848 || (gfc_current_ns->parent->parent
5849 && gfc_current_ns->parent->parent == sym->ns)))
5850 sym->attr.host_assoc = 1;
5851
5852 if (gfc_current_ns->proc_name
5853 && sym->attr.dimension
5854 && (sym->ns != gfc_current_ns
5855 || sym->attr.use_assoc
5856 || sym->attr.in_common))
5857 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
5858
5859 resolve_procedure:
5860 if (t && !resolve_procedure_expression (e))
5861 t = false;
5862
5863 /* F2008, C617 and C1229. */
5864 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5865 && gfc_is_coindexed (e))
5866 {
5867 gfc_ref *ref, *ref2 = NULL;
5868
5869 for (ref = e->ref; ref; ref = ref->next)
5870 {
5871 if (ref->type == REF_COMPONENT)
5872 ref2 = ref;
5873 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5874 break;
5875 }
5876
5877 for ( ; ref; ref = ref->next)
5878 if (ref->type == REF_COMPONENT)
5879 break;
5880
5881 /* Expression itself is not coindexed object. */
5882 if (ref && e->ts.type == BT_CLASS)
5883 {
5884 gfc_error ("Polymorphic subobject of coindexed object at %L",
5885 &e->where);
5886 t = false;
5887 }
5888
5889 /* Expression itself is coindexed object. */
5890 if (ref == NULL)
5891 {
5892 gfc_component *c;
5893 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5894 for ( ; c; c = c->next)
5895 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5896 {
5897 gfc_error ("Coindexed object with polymorphic allocatable "
5898 "subcomponent at %L", &e->where);
5899 t = false;
5900 break;
5901 }
5902 }
5903 }
5904
5905 if (t)
5906 gfc_expression_rank (e);
5907
5908 if (t && flag_coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e))
5909 add_caf_get_intrinsic (e);
5910
5911 /* Simplify cases where access to a parameter array results in a
5912 single constant. Suppress errors since those will have been
5913 issued before, as warnings. */
5914 if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
5915 {
5916 gfc_push_suppress_errors ();
5917 gfc_simplify_expr (e, 1);
5918 gfc_pop_suppress_errors ();
5919 }
5920
5921 return t;
5922 }
5923
5924
5925 /* Checks to see that the correct symbol has been host associated.
5926 The only situation where this arises is that in which a twice
5927 contained function is parsed after the host association is made.
5928 Therefore, on detecting this, change the symbol in the expression
5929 and convert the array reference into an actual arglist if the old
5930 symbol is a variable. */
5931 static bool
5932 check_host_association (gfc_expr *e)
5933 {
5934 gfc_symbol *sym, *old_sym;
5935 gfc_symtree *st;
5936 int n;
5937 gfc_ref *ref;
5938 gfc_actual_arglist *arg, *tail = NULL;
5939 bool retval = e->expr_type == EXPR_FUNCTION;
5940
5941 /* If the expression is the result of substitution in
5942 interface.c(gfc_extend_expr) because there is no way in
5943 which the host association can be wrong. */
5944 if (e->symtree == NULL
5945 || e->symtree->n.sym == NULL
5946 || e->user_operator)
5947 return retval;
5948
5949 old_sym = e->symtree->n.sym;
5950
5951 if (gfc_current_ns->parent
5952 && old_sym->ns != gfc_current_ns)
5953 {
5954 /* Use the 'USE' name so that renamed module symbols are
5955 correctly handled. */
5956 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5957
5958 if (sym && old_sym != sym
5959 && sym->ts.type == old_sym->ts.type
5960 && sym->attr.flavor == FL_PROCEDURE
5961 && sym->attr.contained)
5962 {
5963 /* Clear the shape, since it might not be valid. */
5964 gfc_free_shape (&e->shape, e->rank);
5965
5966 /* Give the expression the right symtree! */
5967 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5968 gcc_assert (st != NULL);
5969
5970 if (old_sym->attr.flavor == FL_PROCEDURE
5971 || e->expr_type == EXPR_FUNCTION)
5972 {
5973 /* Original was function so point to the new symbol, since
5974 the actual argument list is already attached to the
5975 expression. */
5976 e->value.function.esym = NULL;
5977 e->symtree = st;
5978 }
5979 else
5980 {
5981 /* Original was variable so convert array references into
5982 an actual arglist. This does not need any checking now
5983 since resolve_function will take care of it. */
5984 e->value.function.actual = NULL;
5985 e->expr_type = EXPR_FUNCTION;
5986 e->symtree = st;
5987
5988 /* Ambiguity will not arise if the array reference is not
5989 the last reference. */
5990 for (ref = e->ref; ref; ref = ref->next)
5991 if (ref->type == REF_ARRAY && ref->next == NULL)
5992 break;
5993
5994 gcc_assert (ref->type == REF_ARRAY);
5995
5996 /* Grab the start expressions from the array ref and
5997 copy them into actual arguments. */
5998 for (n = 0; n < ref->u.ar.dimen; n++)
5999 {
6000 arg = gfc_get_actual_arglist ();
6001 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
6002 if (e->value.function.actual == NULL)
6003 tail = e->value.function.actual = arg;
6004 else
6005 {
6006 tail->next = arg;
6007 tail = arg;
6008 }
6009 }
6010
6011 /* Dump the reference list and set the rank. */
6012 gfc_free_ref_list (e->ref);
6013 e->ref = NULL;
6014 e->rank = sym->as ? sym->as->rank : 0;
6015 }
6016
6017 gfc_resolve_expr (e);
6018 sym->refs++;
6019 }
6020 }
6021 /* This might have changed! */
6022 return e->expr_type == EXPR_FUNCTION;
6023 }
6024
6025
6026 static void
6027 gfc_resolve_character_operator (gfc_expr *e)
6028 {
6029 gfc_expr *op1 = e->value.op.op1;
6030 gfc_expr *op2 = e->value.op.op2;
6031 gfc_expr *e1 = NULL;
6032 gfc_expr *e2 = NULL;
6033
6034 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
6035
6036 if (op1->ts.u.cl && op1->ts.u.cl->length)
6037 e1 = gfc_copy_expr (op1->ts.u.cl->length);
6038 else if (op1->expr_type == EXPR_CONSTANT)
6039 e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6040 op1->value.character.length);
6041
6042 if (op2->ts.u.cl && op2->ts.u.cl->length)
6043 e2 = gfc_copy_expr (op2->ts.u.cl->length);
6044 else if (op2->expr_type == EXPR_CONSTANT)
6045 e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6046 op2->value.character.length);
6047
6048 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6049
6050 if (!e1 || !e2)
6051 {
6052 gfc_free_expr (e1);
6053 gfc_free_expr (e2);
6054
6055 return;
6056 }
6057
6058 e->ts.u.cl->length = gfc_add (e1, e2);
6059 e->ts.u.cl->length->ts.type = BT_INTEGER;
6060 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
6061 gfc_simplify_expr (e->ts.u.cl->length, 0);
6062 gfc_resolve_expr (e->ts.u.cl->length);
6063
6064 return;
6065 }
6066
6067
6068 /* Ensure that an character expression has a charlen and, if possible, a
6069 length expression. */
6070
6071 static void
6072 fixup_charlen (gfc_expr *e)
6073 {
6074 /* The cases fall through so that changes in expression type and the need
6075 for multiple fixes are picked up. In all circumstances, a charlen should
6076 be available for the middle end to hang a backend_decl on. */
6077 switch (e->expr_type)
6078 {
6079 case EXPR_OP:
6080 gfc_resolve_character_operator (e);
6081 /* FALLTHRU */
6082
6083 case EXPR_ARRAY:
6084 if (e->expr_type == EXPR_ARRAY)
6085 gfc_resolve_character_array_constructor (e);
6086 /* FALLTHRU */
6087
6088 case EXPR_SUBSTRING:
6089 if (!e->ts.u.cl && e->ref)
6090 gfc_resolve_substring_charlen (e);
6091 /* FALLTHRU */
6092
6093 default:
6094 if (!e->ts.u.cl)
6095 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6096
6097 break;
6098 }
6099 }
6100
6101
6102 /* Update an actual argument to include the passed-object for type-bound
6103 procedures at the right position. */
6104
6105 static gfc_actual_arglist*
6106 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
6107 const char *name)
6108 {
6109 gcc_assert (argpos > 0);
6110
6111 if (argpos == 1)
6112 {
6113 gfc_actual_arglist* result;
6114
6115 result = gfc_get_actual_arglist ();
6116 result->expr = po;
6117 result->next = lst;
6118 if (name)
6119 result->name = name;
6120
6121 return result;
6122 }
6123
6124 if (lst)
6125 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
6126 else
6127 lst = update_arglist_pass (NULL, po, argpos - 1, name);
6128 return lst;
6129 }
6130
6131
6132 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
6133
6134 static gfc_expr*
6135 extract_compcall_passed_object (gfc_expr* e)
6136 {
6137 gfc_expr* po;
6138
6139 if (e->expr_type == EXPR_UNKNOWN)
6140 {
6141 gfc_error ("Error in typebound call at %L",
6142 &e->where);
6143 return NULL;
6144 }
6145
6146 gcc_assert (e->expr_type == EXPR_COMPCALL);
6147
6148 if (e->value.compcall.base_object)
6149 po = gfc_copy_expr (e->value.compcall.base_object);
6150 else
6151 {
6152 po = gfc_get_expr ();
6153 po->expr_type = EXPR_VARIABLE;
6154 po->symtree = e->symtree;
6155 po->ref = gfc_copy_ref (e->ref);
6156 po->where = e->where;
6157 }
6158
6159 if (!gfc_resolve_expr (po))
6160 return NULL;
6161
6162 return po;
6163 }
6164
6165
6166 /* Update the arglist of an EXPR_COMPCALL expression to include the
6167 passed-object. */
6168
6169 static bool
6170 update_compcall_arglist (gfc_expr* e)
6171 {
6172 gfc_expr* po;
6173 gfc_typebound_proc* tbp;
6174
6175 tbp = e->value.compcall.tbp;
6176
6177 if (tbp->error)
6178 return false;
6179
6180 po = extract_compcall_passed_object (e);
6181 if (!po)
6182 return false;
6183
6184 if (tbp->nopass || e->value.compcall.ignore_pass)
6185 {
6186 gfc_free_expr (po);
6187 return true;
6188 }
6189
6190 if (tbp->pass_arg_num <= 0)
6191 return false;
6192
6193 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
6194 tbp->pass_arg_num,
6195 tbp->pass_arg);
6196
6197 return true;
6198 }
6199
6200
6201 /* Extract the passed object from a PPC call (a copy of it). */
6202
6203 static gfc_expr*
6204 extract_ppc_passed_object (gfc_expr *e)
6205 {
6206 gfc_expr *po;
6207 gfc_ref **ref;
6208
6209 po = gfc_get_expr ();
6210 po->expr_type = EXPR_VARIABLE;
6211 po->symtree = e->symtree;
6212 po->ref = gfc_copy_ref (e->ref);
6213 po->where = e->where;
6214
6215 /* Remove PPC reference. */
6216 ref = &po->ref;
6217 while ((*ref)->next)
6218 ref = &(*ref)->next;
6219 gfc_free_ref_list (*ref);
6220 *ref = NULL;
6221
6222 if (!gfc_resolve_expr (po))
6223 return NULL;
6224
6225 return po;
6226 }
6227
6228
6229 /* Update the actual arglist of a procedure pointer component to include the
6230 passed-object. */
6231
6232 static bool
6233 update_ppc_arglist (gfc_expr* e)
6234 {
6235 gfc_expr* po;
6236 gfc_component *ppc;
6237 gfc_typebound_proc* tb;
6238
6239 ppc = gfc_get_proc_ptr_comp (e);
6240 if (!ppc)
6241 return false;
6242
6243 tb = ppc->tb;
6244
6245 if (tb->error)
6246 return false;
6247 else if (tb->nopass)
6248 return true;
6249
6250 po = extract_ppc_passed_object (e);
6251 if (!po)
6252 return false;
6253
6254 /* F08:R739. */
6255 if (po->rank != 0)
6256 {
6257 gfc_error ("Passed-object at %L must be scalar", &e->where);
6258 return false;
6259 }
6260
6261 /* F08:C611. */
6262 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
6263 {
6264 gfc_error ("Base object for procedure-pointer component call at %L is of"
6265 " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
6266 return false;
6267 }
6268
6269 gcc_assert (tb->pass_arg_num > 0);
6270 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
6271 tb->pass_arg_num,
6272 tb->pass_arg);
6273
6274 return true;
6275 }
6276
6277
6278 /* Check that the object a TBP is called on is valid, i.e. it must not be
6279 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
6280
6281 static bool
6282 check_typebound_baseobject (gfc_expr* e)
6283 {
6284 gfc_expr* base;
6285 bool return_value = false;
6286
6287 base = extract_compcall_passed_object (e);
6288 if (!base)
6289 return false;
6290
6291 if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
6292 {
6293 gfc_error ("Error in typebound call at %L", &e->where);
6294 goto cleanup;
6295 }
6296
6297 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
6298 return false;
6299
6300 /* F08:C611. */
6301 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
6302 {
6303 gfc_error ("Base object for type-bound procedure call at %L is of"
6304 " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
6305 goto cleanup;
6306 }
6307
6308 /* F08:C1230. If the procedure called is NOPASS,
6309 the base object must be scalar. */
6310 if (e->value.compcall.tbp->nopass && base->rank != 0)
6311 {
6312 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
6313 " be scalar", &e->where);
6314 goto cleanup;
6315 }
6316
6317 return_value = true;
6318
6319 cleanup:
6320 gfc_free_expr (base);
6321 return return_value;
6322 }
6323
6324
6325 /* Resolve a call to a type-bound procedure, either function or subroutine,
6326 statically from the data in an EXPR_COMPCALL expression. The adapted
6327 arglist and the target-procedure symtree are returned. */
6328
6329 static bool
6330 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
6331 gfc_actual_arglist** actual)
6332 {
6333 gcc_assert (e->expr_type == EXPR_COMPCALL);
6334 gcc_assert (!e->value.compcall.tbp->is_generic);
6335
6336 /* Update the actual arglist for PASS. */
6337 if (!update_compcall_arglist (e))
6338 return false;
6339
6340 *actual = e->value.compcall.actual;
6341 *target = e->value.compcall.tbp->u.specific;
6342
6343 gfc_free_ref_list (e->ref);
6344 e->ref = NULL;
6345 e->value.compcall.actual = NULL;
6346
6347 /* If we find a deferred typebound procedure, check for derived types
6348 that an overriding typebound procedure has not been missed. */
6349 if (e->value.compcall.name
6350 && !e->value.compcall.tbp->non_overridable
6351 && e->value.compcall.base_object
6352 && e->value.compcall.base_object->ts.type == BT_DERIVED)
6353 {
6354 gfc_symtree *st;
6355 gfc_symbol *derived;
6356
6357 /* Use the derived type of the base_object. */
6358 derived = e->value.compcall.base_object->ts.u.derived;
6359 st = NULL;
6360
6361 /* If necessary, go through the inheritance chain. */
6362 while (!st && derived)
6363 {
6364 /* Look for the typebound procedure 'name'. */
6365 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
6366 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
6367 e->value.compcall.name);
6368 if (!st)
6369 derived = gfc_get_derived_super_type (derived);
6370 }
6371
6372 /* Now find the specific name in the derived type namespace. */
6373 if (st && st->n.tb && st->n.tb->u.specific)
6374 gfc_find_sym_tree (st->n.tb->u.specific->name,
6375 derived->ns, 1, &st);
6376 if (st)
6377 *target = st;
6378 }
6379 return true;
6380 }
6381
6382
6383 /* Get the ultimate declared type from an expression. In addition,
6384 return the last class/derived type reference and the copy of the
6385 reference list. If check_types is set true, derived types are
6386 identified as well as class references. */
6387 static gfc_symbol*
6388 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
6389 gfc_expr *e, bool check_types)
6390 {
6391 gfc_symbol *declared;
6392 gfc_ref *ref;
6393
6394 declared = NULL;
6395 if (class_ref)
6396 *class_ref = NULL;
6397 if (new_ref)
6398 *new_ref = gfc_copy_ref (e->ref);
6399
6400 for (ref = e->ref; ref; ref = ref->next)
6401 {
6402 if (ref->type != REF_COMPONENT)
6403 continue;
6404
6405 if ((ref->u.c.component->ts.type == BT_CLASS
6406 || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
6407 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
6408 {
6409 declared = ref->u.c.component->ts.u.derived;
6410 if (class_ref)
6411 *class_ref = ref;
6412 }
6413 }
6414
6415 if (declared == NULL)
6416 declared = e->symtree->n.sym->ts.u.derived;
6417
6418 return declared;
6419 }
6420
6421
6422 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
6423 which of the specific bindings (if any) matches the arglist and transform
6424 the expression into a call of that binding. */
6425
6426 static bool
6427 resolve_typebound_generic_call (gfc_expr* e, const char **name)
6428 {
6429 gfc_typebound_proc* genproc;
6430 const char* genname;
6431 gfc_symtree *st;
6432 gfc_symbol *derived;
6433
6434 gcc_assert (e->expr_type == EXPR_COMPCALL);
6435 genname = e->value.compcall.name;
6436 genproc = e->value.compcall.tbp;
6437
6438 if (!genproc->is_generic)
6439 return true;
6440
6441 /* Try the bindings on this type and in the inheritance hierarchy. */
6442 for (; genproc; genproc = genproc->overridden)
6443 {
6444 gfc_tbp_generic* g;
6445
6446 gcc_assert (genproc->is_generic);
6447 for (g = genproc->u.generic; g; g = g->next)
6448 {
6449 gfc_symbol* target;
6450 gfc_actual_arglist* args;
6451 bool matches;
6452
6453 gcc_assert (g->specific);
6454
6455 if (g->specific->error)
6456 continue;
6457
6458 target = g->specific->u.specific->n.sym;
6459
6460 /* Get the right arglist by handling PASS/NOPASS. */
6461 args = gfc_copy_actual_arglist (e->value.compcall.actual);
6462 if (!g->specific->nopass)
6463 {
6464 gfc_expr* po;
6465 po = extract_compcall_passed_object (e);
6466 if (!po)
6467 {
6468 gfc_free_actual_arglist (args);
6469 return false;
6470 }
6471
6472 gcc_assert (g->specific->pass_arg_num > 0);
6473 gcc_assert (!g->specific->error);
6474 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
6475 g->specific->pass_arg);
6476 }
6477 resolve_actual_arglist (args, target->attr.proc,
6478 is_external_proc (target)
6479 && gfc_sym_get_dummy_args (target) == NULL);
6480
6481 /* Check if this arglist matches the formal. */
6482 matches = gfc_arglist_matches_symbol (&args, target);
6483
6484 /* Clean up and break out of the loop if we've found it. */
6485 gfc_free_actual_arglist (args);
6486 if (matches)
6487 {
6488 e->value.compcall.tbp = g->specific;
6489 genname = g->specific_st->name;
6490 /* Pass along the name for CLASS methods, where the vtab
6491 procedure pointer component has to be referenced. */
6492 if (name)
6493 *name = genname;
6494 goto success;
6495 }
6496 }
6497 }
6498
6499 /* Nothing matching found! */
6500 gfc_error ("Found no matching specific binding for the call to the GENERIC"
6501 " %qs at %L", genname, &e->where);
6502 return false;
6503
6504 success:
6505 /* Make sure that we have the right specific instance for the name. */
6506 derived = get_declared_from_expr (NULL, NULL, e, true);
6507
6508 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
6509 if (st)
6510 e->value.compcall.tbp = st->n.tb;
6511
6512 return true;
6513 }
6514
6515
6516 /* Resolve a call to a type-bound subroutine. */
6517
6518 static bool
6519 resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
6520 {
6521 gfc_actual_arglist* newactual;
6522 gfc_symtree* target;
6523
6524 /* Check that's really a SUBROUTINE. */
6525 if (!c->expr1->value.compcall.tbp->subroutine)
6526 {
6527 if (!c->expr1->value.compcall.tbp->is_generic
6528 && c->expr1->value.compcall.tbp->u.specific
6529 && c->expr1->value.compcall.tbp->u.specific->n.sym
6530 && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
6531 c->expr1->value.compcall.tbp->subroutine = 1;
6532 else
6533 {
6534 gfc_error ("%qs at %L should be a SUBROUTINE",
6535 c->expr1->value.compcall.name, &c->loc);
6536 return false;
6537 }
6538 }
6539
6540 if (!check_typebound_baseobject (c->expr1))
6541 return false;
6542
6543 /* Pass along the name for CLASS methods, where the vtab
6544 procedure pointer component has to be referenced. */
6545 if (name)
6546 *name = c->expr1->value.compcall.name;
6547
6548 if (!resolve_typebound_generic_call (c->expr1, name))
6549 return false;
6550
6551 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
6552 if (overridable)
6553 *overridable = !c->expr1->value.compcall.tbp->non_overridable;
6554
6555 /* Transform into an ordinary EXEC_CALL for now. */
6556
6557 if (!resolve_typebound_static (c->expr1, &target, &newactual))
6558 return false;
6559
6560 c->ext.actual = newactual;
6561 c->symtree = target;
6562 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
6563
6564 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
6565
6566 gfc_free_expr (c->expr1);
6567 c->expr1 = gfc_get_expr ();
6568 c->expr1->expr_type = EXPR_FUNCTION;
6569 c->expr1->symtree = target;
6570 c->expr1->where = c->loc;
6571
6572 return resolve_call (c);
6573 }
6574
6575
6576 /* Resolve a component-call expression. */
6577 static bool
6578 resolve_compcall (gfc_expr* e, const char **name)
6579 {
6580 gfc_actual_arglist* newactual;
6581 gfc_symtree* target;
6582
6583 /* Check that's really a FUNCTION. */
6584 if (!e->value.compcall.tbp->function)
6585 {
6586 gfc_error ("%qs at %L should be a FUNCTION",
6587 e->value.compcall.name, &e->where);
6588 return false;
6589 }
6590
6591
6592 /* These must not be assign-calls! */
6593 gcc_assert (!e->value.compcall.assign);
6594
6595 if (!check_typebound_baseobject (e))
6596 return false;
6597
6598 /* Pass along the name for CLASS methods, where the vtab
6599 procedure pointer component has to be referenced. */
6600 if (name)
6601 *name = e->value.compcall.name;
6602
6603 if (!resolve_typebound_generic_call (e, name))
6604 return false;
6605 gcc_assert (!e->value.compcall.tbp->is_generic);
6606
6607 /* Take the rank from the function's symbol. */
6608 if (e->value.compcall.tbp->u.specific->n.sym->as)
6609 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
6610
6611 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
6612 arglist to the TBP's binding target. */
6613
6614 if (!resolve_typebound_static (e, &target, &newactual))
6615 return false;
6616
6617 e->value.function.actual = newactual;
6618 e->value.function.name = NULL;
6619 e->value.function.esym = target->n.sym;
6620 e->value.function.isym = NULL;
6621 e->symtree = target;
6622 e->ts = target->n.sym->ts;
6623 e->expr_type = EXPR_FUNCTION;
6624
6625 /* Resolution is not necessary if this is a class subroutine; this
6626 function only has to identify the specific proc. Resolution of
6627 the call will be done next in resolve_typebound_call. */
6628 return gfc_resolve_expr (e);
6629 }
6630
6631
6632 static bool resolve_fl_derived (gfc_symbol *sym);
6633
6634
6635 /* Resolve a typebound function, or 'method'. First separate all
6636 the non-CLASS references by calling resolve_compcall directly. */
6637
6638 static bool
6639 resolve_typebound_function (gfc_expr* e)
6640 {
6641 gfc_symbol *declared;
6642 gfc_component *c;
6643 gfc_ref *new_ref;
6644 gfc_ref *class_ref;
6645 gfc_symtree *st;
6646 const char *name;
6647 gfc_typespec ts;
6648 gfc_expr *expr;
6649 bool overridable;
6650
6651 st = e->symtree;
6652
6653 /* Deal with typebound operators for CLASS objects. */
6654 expr = e->value.compcall.base_object;
6655 overridable = !e->value.compcall.tbp->non_overridable;
6656 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
6657 {
6658 /* Since the typebound operators are generic, we have to ensure
6659 that any delays in resolution are corrected and that the vtab
6660 is present. */
6661 ts = expr->ts;
6662 declared = ts.u.derived;
6663 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6664 if (c->ts.u.derived == NULL)
6665 c->ts.u.derived = gfc_find_derived_vtab (declared);
6666
6667 if (!resolve_compcall (e, &name))
6668 return false;
6669
6670 /* Use the generic name if it is there. */
6671 name = name ? name : e->value.function.esym->name;
6672 e->symtree = expr->symtree;
6673 e->ref = gfc_copy_ref (expr->ref);
6674 get_declared_from_expr (&class_ref, NULL, e, false);
6675
6676 /* Trim away the extraneous references that emerge from nested
6677 use of interface.c (extend_expr). */
6678 if (class_ref && class_ref->next)
6679 {
6680 gfc_free_ref_list (class_ref->next);
6681 class_ref->next = NULL;
6682 }
6683 else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
6684 {
6685 gfc_free_ref_list (e->ref);
6686 e->ref = NULL;
6687 }
6688
6689 gfc_add_vptr_component (e);
6690 gfc_add_component_ref (e, name);
6691 e->value.function.esym = NULL;
6692 if (expr->expr_type != EXPR_VARIABLE)
6693 e->base_expr = expr;
6694 return true;
6695 }
6696
6697 if (st == NULL)
6698 return resolve_compcall (e, NULL);
6699
6700 if (!gfc_resolve_ref (e))
6701 return false;
6702
6703 /* Get the CLASS declared type. */
6704 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
6705
6706 if (!resolve_fl_derived (declared))
6707 return false;
6708
6709 /* Weed out cases of the ultimate component being a derived type. */
6710 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6711 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6712 {
6713 gfc_free_ref_list (new_ref);
6714 return resolve_compcall (e, NULL);
6715 }
6716
6717 c = gfc_find_component (declared, "_data", true, true, NULL);
6718
6719 /* Treat the call as if it is a typebound procedure, in order to roll
6720 out the correct name for the specific function. */
6721 if (!resolve_compcall (e, &name))
6722 {
6723 gfc_free_ref_list (new_ref);
6724 return false;
6725 }
6726 ts = e->ts;
6727
6728 if (overridable)
6729 {
6730 /* Convert the expression to a procedure pointer component call. */
6731 e->value.function.esym = NULL;
6732 e->symtree = st;
6733
6734 if (new_ref)
6735 e->ref = new_ref;
6736
6737 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6738 gfc_add_vptr_component (e);
6739 gfc_add_component_ref (e, name);
6740
6741 /* Recover the typespec for the expression. This is really only
6742 necessary for generic procedures, where the additional call
6743 to gfc_add_component_ref seems to throw the collection of the
6744 correct typespec. */
6745 e->ts = ts;
6746 }
6747 else if (new_ref)
6748 gfc_free_ref_list (new_ref);
6749
6750 return true;
6751 }
6752
6753 /* Resolve a typebound subroutine, or 'method'. First separate all
6754 the non-CLASS references by calling resolve_typebound_call
6755 directly. */
6756
6757 static bool
6758 resolve_typebound_subroutine (gfc_code *code)
6759 {
6760 gfc_symbol *declared;
6761 gfc_component *c;
6762 gfc_ref *new_ref;
6763 gfc_ref *class_ref;
6764 gfc_symtree *st;
6765 const char *name;
6766 gfc_typespec ts;
6767 gfc_expr *expr;
6768 bool overridable;
6769
6770 st = code->expr1->symtree;
6771
6772 /* Deal with typebound operators for CLASS objects. */
6773 expr = code->expr1->value.compcall.base_object;
6774 overridable = !code->expr1->value.compcall.tbp->non_overridable;
6775 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
6776 {
6777 /* If the base_object is not a variable, the corresponding actual
6778 argument expression must be stored in e->base_expression so
6779 that the corresponding tree temporary can be used as the base
6780 object in gfc_conv_procedure_call. */
6781 if (expr->expr_type != EXPR_VARIABLE)
6782 {
6783 gfc_actual_arglist *args;
6784
6785 args= code->expr1->value.function.actual;
6786 for (; args; args = args->next)
6787 if (expr == args->expr)
6788 expr = args->expr;
6789 }
6790
6791 /* Since the typebound operators are generic, we have to ensure
6792 that any delays in resolution are corrected and that the vtab
6793 is present. */
6794 declared = expr->ts.u.derived;
6795 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6796 if (c->ts.u.derived == NULL)
6797 c->ts.u.derived = gfc_find_derived_vtab (declared);
6798
6799 if (!resolve_typebound_call (code, &name, NULL))
6800 return false;
6801
6802 /* Use the generic name if it is there. */
6803 name = name ? name : code->expr1->value.function.esym->name;
6804 code->expr1->symtree = expr->symtree;
6805 code->expr1->ref = gfc_copy_ref (expr->ref);
6806
6807 /* Trim away the extraneous references that emerge from nested
6808 use of interface.c (extend_expr). */
6809 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
6810 if (class_ref && class_ref->next)
6811 {
6812 gfc_free_ref_list (class_ref->next);
6813 class_ref->next = NULL;
6814 }
6815 else if (code->expr1->ref && !class_ref)
6816 {
6817 gfc_free_ref_list (code->expr1->ref);
6818 code->expr1->ref = NULL;
6819 }
6820
6821 /* Now use the procedure in the vtable. */
6822 gfc_add_vptr_component (code->expr1);
6823 gfc_add_component_ref (code->expr1, name);
6824 code->expr1->value.function.esym = NULL;
6825 if (expr->expr_type != EXPR_VARIABLE)
6826 code->expr1->base_expr = expr;
6827 return true;
6828 }
6829
6830 if (st == NULL)
6831 return resolve_typebound_call (code, NULL, NULL);
6832
6833 if (!gfc_resolve_ref (code->expr1))
6834 return false;
6835
6836 /* Get the CLASS declared type. */
6837 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
6838
6839 /* Weed out cases of the ultimate component being a derived type. */
6840 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6841 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6842 {
6843 gfc_free_ref_list (new_ref);
6844 return resolve_typebound_call (code, NULL, NULL);
6845 }
6846
6847 if (!resolve_typebound_call (code, &name, &overridable))
6848 {
6849 gfc_free_ref_list (new_ref);
6850 return false;
6851 }
6852 ts = code->expr1->ts;
6853
6854 if (overridable)
6855 {
6856 /* Convert the expression to a procedure pointer component call. */
6857 code->expr1->value.function.esym = NULL;
6858 code->expr1->symtree = st;
6859
6860 if (new_ref)
6861 code->expr1->ref = new_ref;
6862
6863 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6864 gfc_add_vptr_component (code->expr1);
6865 gfc_add_component_ref (code->expr1, name);
6866
6867 /* Recover the typespec for the expression. This is really only
6868 necessary for generic procedures, where the additional call
6869 to gfc_add_component_ref seems to throw the collection of the
6870 correct typespec. */
6871 code->expr1->ts = ts;
6872 }
6873 else if (new_ref)
6874 gfc_free_ref_list (new_ref);
6875
6876 return true;
6877 }
6878
6879
6880 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6881
6882 static bool
6883 resolve_ppc_call (gfc_code* c)
6884 {
6885 gfc_component *comp;
6886
6887 comp = gfc_get_proc_ptr_comp (c->expr1);
6888 gcc_assert (comp != NULL);
6889
6890 c->resolved_sym = c->expr1->symtree->n.sym;
6891 c->expr1->expr_type = EXPR_VARIABLE;
6892
6893 if (!comp->attr.subroutine)
6894 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
6895
6896 if (!gfc_resolve_ref (c->expr1))
6897 return false;
6898
6899 if (!update_ppc_arglist (c->expr1))
6900 return false;
6901
6902 c->ext.actual = c->expr1->value.compcall.actual;
6903
6904 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6905 !(comp->ts.interface
6906 && comp->ts.interface->formal)))
6907 return false;
6908
6909 if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
6910 return false;
6911
6912 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6913
6914 return true;
6915 }
6916
6917
6918 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6919
6920 static bool
6921 resolve_expr_ppc (gfc_expr* e)
6922 {
6923 gfc_component *comp;
6924
6925 comp = gfc_get_proc_ptr_comp (e);
6926 gcc_assert (comp != NULL);
6927
6928 /* Convert to EXPR_FUNCTION. */
6929 e->expr_type = EXPR_FUNCTION;
6930 e->value.function.isym = NULL;
6931 e->value.function.actual = e->value.compcall.actual;
6932 e->ts = comp->ts;
6933 if (comp->as != NULL)
6934 e->rank = comp->as->rank;
6935
6936 if (!comp->attr.function)
6937 gfc_add_function (&comp->attr, comp->name, &e->where);
6938
6939 if (!gfc_resolve_ref (e))
6940 return false;
6941
6942 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6943 !(comp->ts.interface
6944 && comp->ts.interface->formal)))
6945 return false;
6946
6947 if (!update_ppc_arglist (e))
6948 return false;
6949
6950 if (!check_pure_function(e))
6951 return false;
6952
6953 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6954
6955 return true;
6956 }
6957
6958
6959 static bool
6960 gfc_is_expandable_expr (gfc_expr *e)
6961 {
6962 gfc_constructor *con;
6963
6964 if (e->expr_type == EXPR_ARRAY)
6965 {
6966 /* Traverse the constructor looking for variables that are flavor
6967 parameter. Parameters must be expanded since they are fully used at
6968 compile time. */
6969 con = gfc_constructor_first (e->value.constructor);
6970 for (; con; con = gfc_constructor_next (con))
6971 {
6972 if (con->expr->expr_type == EXPR_VARIABLE
6973 && con->expr->symtree
6974 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6975 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6976 return true;
6977 if (con->expr->expr_type == EXPR_ARRAY
6978 && gfc_is_expandable_expr (con->expr))
6979 return true;
6980 }
6981 }
6982
6983 return false;
6984 }
6985
6986
6987 /* Sometimes variables in specification expressions of the result
6988 of module procedures in submodules wind up not being the 'real'
6989 dummy. Find this, if possible, in the namespace of the first
6990 formal argument. */
6991
6992 static void
6993 fixup_unique_dummy (gfc_expr *e)
6994 {
6995 gfc_symtree *st = NULL;
6996 gfc_symbol *s = NULL;
6997
6998 if (e->symtree->n.sym->ns->proc_name
6999 && e->symtree->n.sym->ns->proc_name->formal)
7000 s = e->symtree->n.sym->ns->proc_name->formal->sym;
7001
7002 if (s != NULL)
7003 st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
7004
7005 if (st != NULL
7006 && st->n.sym != NULL
7007 && st->n.sym->attr.dummy)
7008 e->symtree = st;
7009 }
7010
7011 /* Resolve an expression. That is, make sure that types of operands agree
7012 with their operators, intrinsic operators are converted to function calls
7013 for overloaded types and unresolved function references are resolved. */
7014
7015 bool
7016 gfc_resolve_expr (gfc_expr *e)
7017 {
7018 bool t;
7019 bool inquiry_save, actual_arg_save, first_actual_arg_save;
7020
7021 if (e == NULL || e->do_not_resolve_again)
7022 return true;
7023
7024 /* inquiry_argument only applies to variables. */
7025 inquiry_save = inquiry_argument;
7026 actual_arg_save = actual_arg;
7027 first_actual_arg_save = first_actual_arg;
7028
7029 if (e->expr_type != EXPR_VARIABLE)
7030 {
7031 inquiry_argument = false;
7032 actual_arg = false;
7033 first_actual_arg = false;
7034 }
7035 else if (e->symtree != NULL
7036 && *e->symtree->name == '@'
7037 && e->symtree->n.sym->attr.dummy)
7038 {
7039 /* Deal with submodule specification expressions that are not
7040 found to be referenced in module.c(read_cleanup). */
7041 fixup_unique_dummy (e);
7042 }
7043
7044 switch (e->expr_type)
7045 {
7046 case EXPR_OP:
7047 t = resolve_operator (e);
7048 break;
7049
7050 case EXPR_FUNCTION:
7051 case EXPR_VARIABLE:
7052
7053 if (check_host_association (e))
7054 t = resolve_function (e);
7055 else
7056 t = resolve_variable (e);
7057
7058 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
7059 && e->ref->type != REF_SUBSTRING)
7060 gfc_resolve_substring_charlen (e);
7061
7062 break;
7063
7064 case EXPR_COMPCALL:
7065 t = resolve_typebound_function (e);
7066 break;
7067
7068 case EXPR_SUBSTRING:
7069 t = gfc_resolve_ref (e);
7070 break;
7071
7072 case EXPR_CONSTANT:
7073 case EXPR_NULL:
7074 t = true;
7075 break;
7076
7077 case EXPR_PPC:
7078 t = resolve_expr_ppc (e);
7079 break;
7080
7081 case EXPR_ARRAY:
7082 t = false;
7083 if (!gfc_resolve_ref (e))
7084 break;
7085
7086 t = gfc_resolve_array_constructor (e);
7087 /* Also try to expand a constructor. */
7088 if (t)
7089 {
7090 gfc_expression_rank (e);
7091 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
7092 gfc_expand_constructor (e, false);
7093 }
7094
7095 /* This provides the opportunity for the length of constructors with
7096 character valued function elements to propagate the string length
7097 to the expression. */
7098 if (t && e->ts.type == BT_CHARACTER)
7099 {
7100 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
7101 here rather then add a duplicate test for it above. */
7102 gfc_expand_constructor (e, false);
7103 t = gfc_resolve_character_array_constructor (e);
7104 }
7105
7106 break;
7107
7108 case EXPR_STRUCTURE:
7109 t = gfc_resolve_ref (e);
7110 if (!t)
7111 break;
7112
7113 t = resolve_structure_cons (e, 0);
7114 if (!t)
7115 break;
7116
7117 t = gfc_simplify_expr (e, 0);
7118 break;
7119
7120 default:
7121 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
7122 }
7123
7124 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
7125 fixup_charlen (e);
7126
7127 inquiry_argument = inquiry_save;
7128 actual_arg = actual_arg_save;
7129 first_actual_arg = first_actual_arg_save;
7130
7131 /* For some reason, resolving these expressions a second time mangles
7132 the typespec of the expression itself. */
7133 if (t && e->expr_type == EXPR_VARIABLE
7134 && e->symtree->n.sym->attr.select_rank_temporary
7135 && UNLIMITED_POLY (e->symtree->n.sym))
7136 e->do_not_resolve_again = 1;
7137
7138 return t;
7139 }
7140
7141
7142 /* Resolve an expression from an iterator. They must be scalar and have
7143 INTEGER or (optionally) REAL type. */
7144
7145 static bool
7146 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
7147 const char *name_msgid)
7148 {
7149 if (!gfc_resolve_expr (expr))
7150 return false;
7151
7152 if (expr->rank != 0)
7153 {
7154 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
7155 return false;
7156 }
7157
7158 if (expr->ts.type != BT_INTEGER)
7159 {
7160 if (expr->ts.type == BT_REAL)
7161 {
7162 if (real_ok)
7163 return gfc_notify_std (GFC_STD_F95_DEL,
7164 "%s at %L must be integer",
7165 _(name_msgid), &expr->where);
7166 else
7167 {
7168 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
7169 &expr->where);
7170 return false;
7171 }
7172 }
7173 else
7174 {
7175 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
7176 return false;
7177 }
7178 }
7179 return true;
7180 }
7181
7182
7183 /* Resolve the expressions in an iterator structure. If REAL_OK is
7184 false allow only INTEGER type iterators, otherwise allow REAL types.
7185 Set own_scope to true for ac-implied-do and data-implied-do as those
7186 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
7187
7188 bool
7189 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
7190 {
7191 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
7192 return false;
7193
7194 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
7195 _("iterator variable")))
7196 return false;
7197
7198 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
7199 "Start expression in DO loop"))
7200 return false;
7201
7202 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
7203 "End expression in DO loop"))
7204 return false;
7205
7206 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
7207 "Step expression in DO loop"))
7208 return false;
7209
7210 /* Convert start, end, and step to the same type as var. */
7211 if (iter->start->ts.kind != iter->var->ts.kind
7212 || iter->start->ts.type != iter->var->ts.type)
7213 gfc_convert_type (iter->start, &iter->var->ts, 1);
7214
7215 if (iter->end->ts.kind != iter->var->ts.kind
7216 || iter->end->ts.type != iter->var->ts.type)
7217 gfc_convert_type (iter->end, &iter->var->ts, 1);
7218
7219 if (iter->step->ts.kind != iter->var->ts.kind
7220 || iter->step->ts.type != iter->var->ts.type)
7221 gfc_convert_type (iter->step, &iter->var->ts, 1);
7222
7223 if (iter->step->expr_type == EXPR_CONSTANT)
7224 {
7225 if ((iter->step->ts.type == BT_INTEGER
7226 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
7227 || (iter->step->ts.type == BT_REAL
7228 && mpfr_sgn (iter->step->value.real) == 0))
7229 {
7230 gfc_error ("Step expression in DO loop at %L cannot be zero",
7231 &iter->step->where);
7232 return false;
7233 }
7234 }
7235
7236 if (iter->start->expr_type == EXPR_CONSTANT
7237 && iter->end->expr_type == EXPR_CONSTANT
7238 && iter->step->expr_type == EXPR_CONSTANT)
7239 {
7240 int sgn, cmp;
7241 if (iter->start->ts.type == BT_INTEGER)
7242 {
7243 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
7244 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
7245 }
7246 else
7247 {
7248 sgn = mpfr_sgn (iter->step->value.real);
7249 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
7250 }
7251 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
7252 gfc_warning (OPT_Wzerotrip,
7253 "DO loop at %L will be executed zero times",
7254 &iter->step->where);
7255 }
7256
7257 if (iter->end->expr_type == EXPR_CONSTANT
7258 && iter->end->ts.type == BT_INTEGER
7259 && iter->step->expr_type == EXPR_CONSTANT
7260 && iter->step->ts.type == BT_INTEGER
7261 && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
7262 || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
7263 {
7264 bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
7265 int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
7266
7267 if (is_step_positive
7268 && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
7269 gfc_warning (OPT_Wundefined_do_loop,
7270 "DO loop at %L is undefined as it overflows",
7271 &iter->step->where);
7272 else if (!is_step_positive
7273 && mpz_cmp (iter->end->value.integer,
7274 gfc_integer_kinds[k].min_int) == 0)
7275 gfc_warning (OPT_Wundefined_do_loop,
7276 "DO loop at %L is undefined as it underflows",
7277 &iter->step->where);
7278 }
7279
7280 return true;
7281 }
7282
7283
7284 /* Traversal function for find_forall_index. f == 2 signals that
7285 that variable itself is not to be checked - only the references. */
7286
7287 static bool
7288 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
7289 {
7290 if (expr->expr_type != EXPR_VARIABLE)
7291 return false;
7292
7293 /* A scalar assignment */
7294 if (!expr->ref || *f == 1)
7295 {
7296 if (expr->symtree->n.sym == sym)
7297 return true;
7298 else
7299 return false;
7300 }
7301
7302 if (*f == 2)
7303 *f = 1;
7304 return false;
7305 }
7306
7307
7308 /* Check whether the FORALL index appears in the expression or not.
7309 Returns true if SYM is found in EXPR. */
7310
7311 bool
7312 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
7313 {
7314 if (gfc_traverse_expr (expr, sym, forall_index, f))
7315 return true;
7316 else
7317 return false;
7318 }
7319
7320
7321 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
7322 to be a scalar INTEGER variable. The subscripts and stride are scalar
7323 INTEGERs, and if stride is a constant it must be nonzero.
7324 Furthermore "A subscript or stride in a forall-triplet-spec shall
7325 not contain a reference to any index-name in the
7326 forall-triplet-spec-list in which it appears." (7.5.4.1) */
7327
7328 static void
7329 resolve_forall_iterators (gfc_forall_iterator *it)
7330 {
7331 gfc_forall_iterator *iter, *iter2;
7332
7333 for (iter = it; iter; iter = iter->next)
7334 {
7335 if (gfc_resolve_expr (iter->var)
7336 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
7337 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
7338 &iter->var->where);
7339
7340 if (gfc_resolve_expr (iter->start)
7341 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
7342 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
7343 &iter->start->where);
7344 if (iter->var->ts.kind != iter->start->ts.kind)
7345 gfc_convert_type (iter->start, &iter->var->ts, 1);
7346
7347 if (gfc_resolve_expr (iter->end)
7348 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
7349 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
7350 &iter->end->where);
7351 if (iter->var->ts.kind != iter->end->ts.kind)
7352 gfc_convert_type (iter->end, &iter->var->ts, 1);
7353
7354 if (gfc_resolve_expr (iter->stride))
7355 {
7356 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
7357 gfc_error ("FORALL stride expression at %L must be a scalar %s",
7358 &iter->stride->where, "INTEGER");
7359
7360 if (iter->stride->expr_type == EXPR_CONSTANT
7361 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
7362 gfc_error ("FORALL stride expression at %L cannot be zero",
7363 &iter->stride->where);
7364 }
7365 if (iter->var->ts.kind != iter->stride->ts.kind)
7366 gfc_convert_type (iter->stride, &iter->var->ts, 1);
7367 }
7368
7369 for (iter = it; iter; iter = iter->next)
7370 for (iter2 = iter; iter2; iter2 = iter2->next)
7371 {
7372 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
7373 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
7374 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
7375 gfc_error ("FORALL index %qs may not appear in triplet "
7376 "specification at %L", iter->var->symtree->name,
7377 &iter2->start->where);
7378 }
7379 }
7380
7381
7382 /* Given a pointer to a symbol that is a derived type, see if it's
7383 inaccessible, i.e. if it's defined in another module and the components are
7384 PRIVATE. The search is recursive if necessary. Returns zero if no
7385 inaccessible components are found, nonzero otherwise. */
7386
7387 static int
7388 derived_inaccessible (gfc_symbol *sym)
7389 {
7390 gfc_component *c;
7391
7392 if (sym->attr.use_assoc && sym->attr.private_comp)
7393 return 1;
7394
7395 for (c = sym->components; c; c = c->next)
7396 {
7397 /* Prevent an infinite loop through this function. */
7398 if (c->ts.type == BT_DERIVED && c->attr.pointer
7399 && sym == c->ts.u.derived)
7400 continue;
7401
7402 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
7403 return 1;
7404 }
7405
7406 return 0;
7407 }
7408
7409
7410 /* Resolve the argument of a deallocate expression. The expression must be
7411 a pointer or a full array. */
7412
7413 static bool
7414 resolve_deallocate_expr (gfc_expr *e)
7415 {
7416 symbol_attribute attr;
7417 int allocatable, pointer;
7418 gfc_ref *ref;
7419 gfc_symbol *sym;
7420 gfc_component *c;
7421 bool unlimited;
7422
7423 if (!gfc_resolve_expr (e))
7424 return false;
7425
7426 if (e->expr_type != EXPR_VARIABLE)
7427 goto bad;
7428
7429 sym = e->symtree->n.sym;
7430 unlimited = UNLIMITED_POLY(sym);
7431
7432 if (sym->ts.type == BT_CLASS)
7433 {
7434 allocatable = CLASS_DATA (sym)->attr.allocatable;
7435 pointer = CLASS_DATA (sym)->attr.class_pointer;
7436 }
7437 else
7438 {
7439 allocatable = sym->attr.allocatable;
7440 pointer = sym->attr.pointer;
7441 }
7442 for (ref = e->ref; ref; ref = ref->next)
7443 {
7444 switch (ref->type)
7445 {
7446 case REF_ARRAY:
7447 if (ref->u.ar.type != AR_FULL
7448 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
7449 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
7450 allocatable = 0;
7451 break;
7452
7453 case REF_COMPONENT:
7454 c = ref->u.c.component;
7455 if (c->ts.type == BT_CLASS)
7456 {
7457 allocatable = CLASS_DATA (c)->attr.allocatable;
7458 pointer = CLASS_DATA (c)->attr.class_pointer;
7459 }
7460 else
7461 {
7462 allocatable = c->attr.allocatable;
7463 pointer = c->attr.pointer;
7464 }
7465 break;
7466
7467 case REF_SUBSTRING:
7468 case REF_INQUIRY:
7469 allocatable = 0;
7470 break;
7471 }
7472 }
7473
7474 attr = gfc_expr_attr (e);
7475
7476 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
7477 {
7478 bad:
7479 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7480 &e->where);
7481 return false;
7482 }
7483
7484 /* F2008, C644. */
7485 if (gfc_is_coindexed (e))
7486 {
7487 gfc_error ("Coindexed allocatable object at %L", &e->where);
7488 return false;
7489 }
7490
7491 if (pointer
7492 && !gfc_check_vardef_context (e, true, true, false,
7493 _("DEALLOCATE object")))
7494 return false;
7495 if (!gfc_check_vardef_context (e, false, true, false,
7496 _("DEALLOCATE object")))
7497 return false;
7498
7499 return true;
7500 }
7501
7502
7503 /* Returns true if the expression e contains a reference to the symbol sym. */
7504 static bool
7505 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
7506 {
7507 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
7508 return true;
7509
7510 return false;
7511 }
7512
7513 bool
7514 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
7515 {
7516 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
7517 }
7518
7519
7520 /* Given the expression node e for an allocatable/pointer of derived type to be
7521 allocated, get the expression node to be initialized afterwards (needed for
7522 derived types with default initializers, and derived types with allocatable
7523 components that need nullification.) */
7524
7525 gfc_expr *
7526 gfc_expr_to_initialize (gfc_expr *e)
7527 {
7528 gfc_expr *result;
7529 gfc_ref *ref;
7530 int i;
7531
7532 result = gfc_copy_expr (e);
7533
7534 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
7535 for (ref = result->ref; ref; ref = ref->next)
7536 if (ref->type == REF_ARRAY && ref->next == NULL)
7537 {
7538 if (ref->u.ar.dimen == 0
7539 && ref->u.ar.as && ref->u.ar.as->corank)
7540 return result;
7541
7542 ref->u.ar.type = AR_FULL;
7543
7544 for (i = 0; i < ref->u.ar.dimen; i++)
7545 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
7546
7547 break;
7548 }
7549
7550 gfc_free_shape (&result->shape, result->rank);
7551
7552 /* Recalculate rank, shape, etc. */
7553 gfc_resolve_expr (result);
7554 return result;
7555 }
7556
7557
7558 /* If the last ref of an expression is an array ref, return a copy of the
7559 expression with that one removed. Otherwise, a copy of the original
7560 expression. This is used for allocate-expressions and pointer assignment
7561 LHS, where there may be an array specification that needs to be stripped
7562 off when using gfc_check_vardef_context. */
7563
7564 static gfc_expr*
7565 remove_last_array_ref (gfc_expr* e)
7566 {
7567 gfc_expr* e2;
7568 gfc_ref** r;
7569
7570 e2 = gfc_copy_expr (e);
7571 for (r = &e2->ref; *r; r = &(*r)->next)
7572 if ((*r)->type == REF_ARRAY && !(*r)->next)
7573 {
7574 gfc_free_ref_list (*r);
7575 *r = NULL;
7576 break;
7577 }
7578
7579 return e2;
7580 }
7581
7582
7583 /* Used in resolve_allocate_expr to check that a allocation-object and
7584 a source-expr are conformable. This does not catch all possible
7585 cases; in particular a runtime checking is needed. */
7586
7587 static bool
7588 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
7589 {
7590 gfc_ref *tail;
7591 for (tail = e2->ref; tail && tail->next; tail = tail->next);
7592
7593 /* First compare rank. */
7594 if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
7595 || (!tail && e1->rank != e2->rank))
7596 {
7597 gfc_error ("Source-expr at %L must be scalar or have the "
7598 "same rank as the allocate-object at %L",
7599 &e1->where, &e2->where);
7600 return false;
7601 }
7602
7603 if (e1->shape)
7604 {
7605 int i;
7606 mpz_t s;
7607
7608 mpz_init (s);
7609
7610 for (i = 0; i < e1->rank; i++)
7611 {
7612 if (tail->u.ar.start[i] == NULL)
7613 break;
7614
7615 if (tail->u.ar.end[i])
7616 {
7617 mpz_set (s, tail->u.ar.end[i]->value.integer);
7618 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
7619 mpz_add_ui (s, s, 1);
7620 }
7621 else
7622 {
7623 mpz_set (s, tail->u.ar.start[i]->value.integer);
7624 }
7625
7626 if (mpz_cmp (e1->shape[i], s) != 0)
7627 {
7628 gfc_error ("Source-expr at %L and allocate-object at %L must "
7629 "have the same shape", &e1->where, &e2->where);
7630 mpz_clear (s);
7631 return false;
7632 }
7633 }
7634
7635 mpz_clear (s);
7636 }
7637
7638 return true;
7639 }
7640
7641
7642 /* Resolve the expression in an ALLOCATE statement, doing the additional
7643 checks to see whether the expression is OK or not. The expression must
7644 have a trailing array reference that gives the size of the array. */
7645
7646 static bool
7647 resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
7648 {
7649 int i, pointer, allocatable, dimension, is_abstract;
7650 int codimension;
7651 bool coindexed;
7652 bool unlimited;
7653 symbol_attribute attr;
7654 gfc_ref *ref, *ref2;
7655 gfc_expr *e2;
7656 gfc_array_ref *ar;
7657 gfc_symbol *sym = NULL;
7658 gfc_alloc *a;
7659 gfc_component *c;
7660 bool t;
7661
7662 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
7663 checking of coarrays. */
7664 for (ref = e->ref; ref; ref = ref->next)
7665 if (ref->next == NULL)
7666 break;
7667
7668 if (ref && ref->type == REF_ARRAY)
7669 ref->u.ar.in_allocate = true;
7670
7671 if (!gfc_resolve_expr (e))
7672 goto failure;
7673
7674 /* Make sure the expression is allocatable or a pointer. If it is
7675 pointer, the next-to-last reference must be a pointer. */
7676
7677 ref2 = NULL;
7678 if (e->symtree)
7679 sym = e->symtree->n.sym;
7680
7681 /* Check whether ultimate component is abstract and CLASS. */
7682 is_abstract = 0;
7683
7684 /* Is the allocate-object unlimited polymorphic? */
7685 unlimited = UNLIMITED_POLY(e);
7686
7687 if (e->expr_type != EXPR_VARIABLE)
7688 {
7689 allocatable = 0;
7690 attr = gfc_expr_attr (e);
7691 pointer = attr.pointer;
7692 dimension = attr.dimension;
7693 codimension = attr.codimension;
7694 }
7695 else
7696 {
7697 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
7698 {
7699 allocatable = CLASS_DATA (sym)->attr.allocatable;
7700 pointer = CLASS_DATA (sym)->attr.class_pointer;
7701 dimension = CLASS_DATA (sym)->attr.dimension;
7702 codimension = CLASS_DATA (sym)->attr.codimension;
7703 is_abstract = CLASS_DATA (sym)->attr.abstract;
7704 }
7705 else
7706 {
7707 allocatable = sym->attr.allocatable;
7708 pointer = sym->attr.pointer;
7709 dimension = sym->attr.dimension;
7710 codimension = sym->attr.codimension;
7711 }
7712
7713 coindexed = false;
7714
7715 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
7716 {
7717 switch (ref->type)
7718 {
7719 case REF_ARRAY:
7720 if (ref->u.ar.codimen > 0)
7721 {
7722 int n;
7723 for (n = ref->u.ar.dimen;
7724 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
7725 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
7726 {
7727 coindexed = true;
7728 break;
7729 }
7730 }
7731
7732 if (ref->next != NULL)
7733 pointer = 0;
7734 break;
7735
7736 case REF_COMPONENT:
7737 /* F2008, C644. */
7738 if (coindexed)
7739 {
7740 gfc_error ("Coindexed allocatable object at %L",
7741 &e->where);
7742 goto failure;
7743 }
7744
7745 c = ref->u.c.component;
7746 if (c->ts.type == BT_CLASS)
7747 {
7748 allocatable = CLASS_DATA (c)->attr.allocatable;
7749 pointer = CLASS_DATA (c)->attr.class_pointer;
7750 dimension = CLASS_DATA (c)->attr.dimension;
7751 codimension = CLASS_DATA (c)->attr.codimension;
7752 is_abstract = CLASS_DATA (c)->attr.abstract;
7753 }
7754 else
7755 {
7756 allocatable = c->attr.allocatable;
7757 pointer = c->attr.pointer;
7758 dimension = c->attr.dimension;
7759 codimension = c->attr.codimension;
7760 is_abstract = c->attr.abstract;
7761 }
7762 break;
7763
7764 case REF_SUBSTRING:
7765 case REF_INQUIRY:
7766 allocatable = 0;
7767 pointer = 0;
7768 break;
7769 }
7770 }
7771 }
7772
7773 /* Check for F08:C628. */
7774 if (allocatable == 0 && pointer == 0 && !unlimited)
7775 {
7776 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7777 &e->where);
7778 goto failure;
7779 }
7780
7781 /* Some checks for the SOURCE tag. */
7782 if (code->expr3)
7783 {
7784 /* Check F03:C631. */
7785 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
7786 {
7787 gfc_error ("Type of entity at %L is type incompatible with "
7788 "source-expr at %L", &e->where, &code->expr3->where);
7789 goto failure;
7790 }
7791
7792 /* Check F03:C632 and restriction following Note 6.18. */
7793 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
7794 goto failure;
7795
7796 /* Check F03:C633. */
7797 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
7798 {
7799 gfc_error ("The allocate-object at %L and the source-expr at %L "
7800 "shall have the same kind type parameter",
7801 &e->where, &code->expr3->where);
7802 goto failure;
7803 }
7804
7805 /* Check F2008, C642. */
7806 if (code->expr3->ts.type == BT_DERIVED
7807 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
7808 || (code->expr3->ts.u.derived->from_intmod
7809 == INTMOD_ISO_FORTRAN_ENV
7810 && code->expr3->ts.u.derived->intmod_sym_id
7811 == ISOFORTRAN_LOCK_TYPE)))
7812 {
7813 gfc_error ("The source-expr at %L shall neither be of type "
7814 "LOCK_TYPE nor have a LOCK_TYPE component if "
7815 "allocate-object at %L is a coarray",
7816 &code->expr3->where, &e->where);
7817 goto failure;
7818 }
7819
7820 /* Check TS18508, C702/C703. */
7821 if (code->expr3->ts.type == BT_DERIVED
7822 && ((codimension && gfc_expr_attr (code->expr3).event_comp)
7823 || (code->expr3->ts.u.derived->from_intmod
7824 == INTMOD_ISO_FORTRAN_ENV
7825 && code->expr3->ts.u.derived->intmod_sym_id
7826 == ISOFORTRAN_EVENT_TYPE)))
7827 {
7828 gfc_error ("The source-expr at %L shall neither be of type "
7829 "EVENT_TYPE nor have a EVENT_TYPE component if "
7830 "allocate-object at %L is a coarray",
7831 &code->expr3->where, &e->where);
7832 goto failure;
7833 }
7834 }
7835
7836 /* Check F08:C629. */
7837 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
7838 && !code->expr3)
7839 {
7840 gcc_assert (e->ts.type == BT_CLASS);
7841 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
7842 "type-spec or source-expr", sym->name, &e->where);
7843 goto failure;
7844 }
7845
7846 /* Check F08:C632. */
7847 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
7848 && !UNLIMITED_POLY (e))
7849 {
7850 int cmp;
7851
7852 if (!e->ts.u.cl->length)
7853 goto failure;
7854
7855 cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
7856 code->ext.alloc.ts.u.cl->length);
7857 if (cmp == 1 || cmp == -1 || cmp == -3)
7858 {
7859 gfc_error ("Allocating %s at %L with type-spec requires the same "
7860 "character-length parameter as in the declaration",
7861 sym->name, &e->where);
7862 goto failure;
7863 }
7864 }
7865
7866 /* In the variable definition context checks, gfc_expr_attr is used
7867 on the expression. This is fooled by the array specification
7868 present in e, thus we have to eliminate that one temporarily. */
7869 e2 = remove_last_array_ref (e);
7870 t = true;
7871 if (t && pointer)
7872 t = gfc_check_vardef_context (e2, true, true, false,
7873 _("ALLOCATE object"));
7874 if (t)
7875 t = gfc_check_vardef_context (e2, false, true, false,
7876 _("ALLOCATE object"));
7877 gfc_free_expr (e2);
7878 if (!t)
7879 goto failure;
7880
7881 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
7882 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
7883 {
7884 /* For class arrays, the initialization with SOURCE is done
7885 using _copy and trans_call. It is convenient to exploit that
7886 when the allocated type is different from the declared type but
7887 no SOURCE exists by setting expr3. */
7888 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
7889 }
7890 else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
7891 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
7892 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
7893 {
7894 /* We have to zero initialize the integer variable. */
7895 code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
7896 }
7897
7898 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
7899 {
7900 /* Make sure the vtab symbol is present when
7901 the module variables are generated. */
7902 gfc_typespec ts = e->ts;
7903 if (code->expr3)
7904 ts = code->expr3->ts;
7905 else if (code->ext.alloc.ts.type == BT_DERIVED)
7906 ts = code->ext.alloc.ts;
7907
7908 /* Finding the vtab also publishes the type's symbol. Therefore this
7909 statement is necessary. */
7910 gfc_find_derived_vtab (ts.u.derived);
7911 }
7912 else if (unlimited && !UNLIMITED_POLY (code->expr3))
7913 {
7914 /* Again, make sure the vtab symbol is present when
7915 the module variables are generated. */
7916 gfc_typespec *ts = NULL;
7917 if (code->expr3)
7918 ts = &code->expr3->ts;
7919 else
7920 ts = &code->ext.alloc.ts;
7921
7922 gcc_assert (ts);
7923
7924 /* Finding the vtab also publishes the type's symbol. Therefore this
7925 statement is necessary. */
7926 gfc_find_vtab (ts);
7927 }
7928
7929 if (dimension == 0 && codimension == 0)
7930 goto success;
7931
7932 /* Make sure the last reference node is an array specification. */
7933
7934 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
7935 || (dimension && ref2->u.ar.dimen == 0))
7936 {
7937 /* F08:C633. */
7938 if (code->expr3)
7939 {
7940 if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
7941 "in ALLOCATE statement at %L", &e->where))
7942 goto failure;
7943 if (code->expr3->rank != 0)
7944 *array_alloc_wo_spec = true;
7945 else
7946 {
7947 gfc_error ("Array specification or array-valued SOURCE= "
7948 "expression required in ALLOCATE statement at %L",
7949 &e->where);
7950 goto failure;
7951 }
7952 }
7953 else
7954 {
7955 gfc_error ("Array specification required in ALLOCATE statement "
7956 "at %L", &e->where);
7957 goto failure;
7958 }
7959 }
7960
7961 /* Make sure that the array section reference makes sense in the
7962 context of an ALLOCATE specification. */
7963
7964 ar = &ref2->u.ar;
7965
7966 if (codimension)
7967 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
7968 {
7969 switch (ar->dimen_type[i])
7970 {
7971 case DIMEN_THIS_IMAGE:
7972 gfc_error ("Coarray specification required in ALLOCATE statement "
7973 "at %L", &e->where);
7974 goto failure;
7975
7976 case DIMEN_RANGE:
7977 if (ar->start[i] == 0 || ar->end[i] == 0)
7978 {
7979 /* If ar->stride[i] is NULL, we issued a previous error. */
7980 if (ar->stride[i] == NULL)
7981 gfc_error ("Bad array specification in ALLOCATE statement "
7982 "at %L", &e->where);
7983 goto failure;
7984 }
7985 else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
7986 {
7987 gfc_error ("Upper cobound is less than lower cobound at %L",
7988 &ar->start[i]->where);
7989 goto failure;
7990 }
7991 break;
7992
7993 case DIMEN_ELEMENT:
7994 if (ar->start[i]->expr_type == EXPR_CONSTANT)
7995 {
7996 gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
7997 if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
7998 {
7999 gfc_error ("Upper cobound is less than lower cobound "
8000 "of 1 at %L", &ar->start[i]->where);
8001 goto failure;
8002 }
8003 }
8004 break;
8005
8006 case DIMEN_STAR:
8007 break;
8008
8009 default:
8010 gfc_error ("Bad array specification in ALLOCATE statement at %L",
8011 &e->where);
8012 goto failure;
8013
8014 }
8015 }
8016 for (i = 0; i < ar->dimen; i++)
8017 {
8018 if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
8019 goto check_symbols;
8020
8021 switch (ar->dimen_type[i])
8022 {
8023 case DIMEN_ELEMENT:
8024 break;
8025
8026 case DIMEN_RANGE:
8027 if (ar->start[i] != NULL
8028 && ar->end[i] != NULL
8029 && ar->stride[i] == NULL)
8030 break;
8031
8032 /* Fall through. */
8033
8034 case DIMEN_UNKNOWN:
8035 case DIMEN_VECTOR:
8036 case DIMEN_STAR:
8037 case DIMEN_THIS_IMAGE:
8038 gfc_error ("Bad array specification in ALLOCATE statement at %L",
8039 &e->where);
8040 goto failure;
8041 }
8042
8043 check_symbols:
8044 for (a = code->ext.alloc.list; a; a = a->next)
8045 {
8046 sym = a->expr->symtree->n.sym;
8047
8048 /* TODO - check derived type components. */
8049 if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
8050 continue;
8051
8052 if ((ar->start[i] != NULL
8053 && gfc_find_sym_in_expr (sym, ar->start[i]))
8054 || (ar->end[i] != NULL
8055 && gfc_find_sym_in_expr (sym, ar->end[i])))
8056 {
8057 gfc_error ("%qs must not appear in the array specification at "
8058 "%L in the same ALLOCATE statement where it is "
8059 "itself allocated", sym->name, &ar->where);
8060 goto failure;
8061 }
8062 }
8063 }
8064
8065 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
8066 {
8067 if (ar->dimen_type[i] == DIMEN_ELEMENT
8068 || ar->dimen_type[i] == DIMEN_RANGE)
8069 {
8070 if (i == (ar->dimen + ar->codimen - 1))
8071 {
8072 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
8073 "statement at %L", &e->where);
8074 goto failure;
8075 }
8076 continue;
8077 }
8078
8079 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
8080 && ar->stride[i] == NULL)
8081 break;
8082
8083 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
8084 &e->where);
8085 goto failure;
8086 }
8087
8088 success:
8089 return true;
8090
8091 failure:
8092 return false;
8093 }
8094
8095
8096 static void
8097 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
8098 {
8099 gfc_expr *stat, *errmsg, *pe, *qe;
8100 gfc_alloc *a, *p, *q;
8101
8102 stat = code->expr1;
8103 errmsg = code->expr2;
8104
8105 /* Check the stat variable. */
8106 if (stat)
8107 {
8108 gfc_check_vardef_context (stat, false, false, false,
8109 _("STAT variable"));
8110
8111 if ((stat->ts.type != BT_INTEGER
8112 && !(stat->ref && (stat->ref->type == REF_ARRAY
8113 || stat->ref->type == REF_COMPONENT)))
8114 || stat->rank > 0)
8115 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
8116 "variable", &stat->where);
8117
8118 for (p = code->ext.alloc.list; p; p = p->next)
8119 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
8120 {
8121 gfc_ref *ref1, *ref2;
8122 bool found = true;
8123
8124 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
8125 ref1 = ref1->next, ref2 = ref2->next)
8126 {
8127 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
8128 continue;
8129 if (ref1->u.c.component->name != ref2->u.c.component->name)
8130 {
8131 found = false;
8132 break;
8133 }
8134 }
8135
8136 if (found)
8137 {
8138 gfc_error ("Stat-variable at %L shall not be %sd within "
8139 "the same %s statement", &stat->where, fcn, fcn);
8140 break;
8141 }
8142 }
8143 }
8144
8145 /* Check the errmsg variable. */
8146 if (errmsg)
8147 {
8148 if (!stat)
8149 gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
8150 &errmsg->where);
8151
8152 gfc_check_vardef_context (errmsg, false, false, false,
8153 _("ERRMSG variable"));
8154
8155 /* F18:R928 alloc-opt is ERRMSG = errmsg-variable
8156 F18:R930 errmsg-variable is scalar-default-char-variable
8157 F18:R906 default-char-variable is variable
8158 F18:C906 default-char-variable shall be default character. */
8159 if ((errmsg->ts.type != BT_CHARACTER
8160 && !(errmsg->ref
8161 && (errmsg->ref->type == REF_ARRAY
8162 || errmsg->ref->type == REF_COMPONENT)))
8163 || errmsg->rank > 0
8164 || errmsg->ts.kind != gfc_default_character_kind)
8165 gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
8166 "variable", &errmsg->where);
8167
8168 for (p = code->ext.alloc.list; p; p = p->next)
8169 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
8170 {
8171 gfc_ref *ref1, *ref2;
8172 bool found = true;
8173
8174 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
8175 ref1 = ref1->next, ref2 = ref2->next)
8176 {
8177 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
8178 continue;
8179 if (ref1->u.c.component->name != ref2->u.c.component->name)
8180 {
8181 found = false;
8182 break;
8183 }
8184 }
8185
8186 if (found)
8187 {
8188 gfc_error ("Errmsg-variable at %L shall not be %sd within "
8189 "the same %s statement", &errmsg->where, fcn, fcn);
8190 break;
8191 }
8192 }
8193 }
8194
8195 /* Check that an allocate-object appears only once in the statement. */
8196
8197 for (p = code->ext.alloc.list; p; p = p->next)
8198 {
8199 pe = p->expr;
8200 for (q = p->next; q; q = q->next)
8201 {
8202 qe = q->expr;
8203 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
8204 {
8205 /* This is a potential collision. */
8206 gfc_ref *pr = pe->ref;
8207 gfc_ref *qr = qe->ref;
8208
8209 /* Follow the references until
8210 a) They start to differ, in which case there is no error;
8211 you can deallocate a%b and a%c in a single statement
8212 b) Both of them stop, which is an error
8213 c) One of them stops, which is also an error. */
8214 while (1)
8215 {
8216 if (pr == NULL && qr == NULL)
8217 {
8218 gfc_error ("Allocate-object at %L also appears at %L",
8219 &pe->where, &qe->where);
8220 break;
8221 }
8222 else if (pr != NULL && qr == NULL)
8223 {
8224 gfc_error ("Allocate-object at %L is subobject of"
8225 " object at %L", &pe->where, &qe->where);
8226 break;
8227 }
8228 else if (pr == NULL && qr != NULL)
8229 {
8230 gfc_error ("Allocate-object at %L is subobject of"
8231 " object at %L", &qe->where, &pe->where);
8232 break;
8233 }
8234 /* Here, pr != NULL && qr != NULL */
8235 gcc_assert(pr->type == qr->type);
8236 if (pr->type == REF_ARRAY)
8237 {
8238 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
8239 which are legal. */
8240 gcc_assert (qr->type == REF_ARRAY);
8241
8242 if (pr->next && qr->next)
8243 {
8244 int i;
8245 gfc_array_ref *par = &(pr->u.ar);
8246 gfc_array_ref *qar = &(qr->u.ar);
8247
8248 for (i=0; i<par->dimen; i++)
8249 {
8250 if ((par->start[i] != NULL
8251 || qar->start[i] != NULL)
8252 && gfc_dep_compare_expr (par->start[i],
8253 qar->start[i]) != 0)
8254 goto break_label;
8255 }
8256 }
8257 }
8258 else
8259 {
8260 if (pr->u.c.component->name != qr->u.c.component->name)
8261 break;
8262 }
8263
8264 pr = pr->next;
8265 qr = qr->next;
8266 }
8267 break_label:
8268 ;
8269 }
8270 }
8271 }
8272
8273 if (strcmp (fcn, "ALLOCATE") == 0)
8274 {
8275 bool arr_alloc_wo_spec = false;
8276
8277 /* Resolving the expr3 in the loop over all objects to allocate would
8278 execute loop invariant code for each loop item. Therefore do it just
8279 once here. */
8280 if (code->expr3 && code->expr3->mold
8281 && code->expr3->ts.type == BT_DERIVED)
8282 {
8283 /* Default initialization via MOLD (non-polymorphic). */
8284 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
8285 if (rhs != NULL)
8286 {
8287 gfc_resolve_expr (rhs);
8288 gfc_free_expr (code->expr3);
8289 code->expr3 = rhs;
8290 }
8291 }
8292 for (a = code->ext.alloc.list; a; a = a->next)
8293 resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
8294
8295 if (arr_alloc_wo_spec && code->expr3)
8296 {
8297 /* Mark the allocate to have to take the array specification
8298 from the expr3. */
8299 code->ext.alloc.arr_spec_from_expr3 = 1;
8300 }
8301 }
8302 else
8303 {
8304 for (a = code->ext.alloc.list; a; a = a->next)
8305 resolve_deallocate_expr (a->expr);
8306 }
8307 }
8308
8309
8310 /************ SELECT CASE resolution subroutines ************/
8311
8312 /* Callback function for our mergesort variant. Determines interval
8313 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
8314 op1 > op2. Assumes we're not dealing with the default case.
8315 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
8316 There are nine situations to check. */
8317
8318 static int
8319 compare_cases (const gfc_case *op1, const gfc_case *op2)
8320 {
8321 int retval;
8322
8323 if (op1->low == NULL) /* op1 = (:L) */
8324 {
8325 /* op2 = (:N), so overlap. */
8326 retval = 0;
8327 /* op2 = (M:) or (M:N), L < M */
8328 if (op2->low != NULL
8329 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8330 retval = -1;
8331 }
8332 else if (op1->high == NULL) /* op1 = (K:) */
8333 {
8334 /* op2 = (M:), so overlap. */
8335 retval = 0;
8336 /* op2 = (:N) or (M:N), K > N */
8337 if (op2->high != NULL
8338 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8339 retval = 1;
8340 }
8341 else /* op1 = (K:L) */
8342 {
8343 if (op2->low == NULL) /* op2 = (:N), K > N */
8344 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8345 ? 1 : 0;
8346 else if (op2->high == NULL) /* op2 = (M:), L < M */
8347 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8348 ? -1 : 0;
8349 else /* op2 = (M:N) */
8350 {
8351 retval = 0;
8352 /* L < M */
8353 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8354 retval = -1;
8355 /* K > N */
8356 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8357 retval = 1;
8358 }
8359 }
8360
8361 return retval;
8362 }
8363
8364
8365 /* Merge-sort a double linked case list, detecting overlap in the
8366 process. LIST is the head of the double linked case list before it
8367 is sorted. Returns the head of the sorted list if we don't see any
8368 overlap, or NULL otherwise. */
8369
8370 static gfc_case *
8371 check_case_overlap (gfc_case *list)
8372 {
8373 gfc_case *p, *q, *e, *tail;
8374 int insize, nmerges, psize, qsize, cmp, overlap_seen;
8375
8376 /* If the passed list was empty, return immediately. */
8377 if (!list)
8378 return NULL;
8379
8380 overlap_seen = 0;
8381 insize = 1;
8382
8383 /* Loop unconditionally. The only exit from this loop is a return
8384 statement, when we've finished sorting the case list. */
8385 for (;;)
8386 {
8387 p = list;
8388 list = NULL;
8389 tail = NULL;
8390
8391 /* Count the number of merges we do in this pass. */
8392 nmerges = 0;
8393
8394 /* Loop while there exists a merge to be done. */
8395 while (p)
8396 {
8397 int i;
8398
8399 /* Count this merge. */
8400 nmerges++;
8401
8402 /* Cut the list in two pieces by stepping INSIZE places
8403 forward in the list, starting from P. */
8404 psize = 0;
8405 q = p;
8406 for (i = 0; i < insize; i++)
8407 {
8408 psize++;
8409 q = q->right;
8410 if (!q)
8411 break;
8412 }
8413 qsize = insize;
8414
8415 /* Now we have two lists. Merge them! */
8416 while (psize > 0 || (qsize > 0 && q != NULL))
8417 {
8418 /* See from which the next case to merge comes from. */
8419 if (psize == 0)
8420 {
8421 /* P is empty so the next case must come from Q. */
8422 e = q;
8423 q = q->right;
8424 qsize--;
8425 }
8426 else if (qsize == 0 || q == NULL)
8427 {
8428 /* Q is empty. */
8429 e = p;
8430 p = p->right;
8431 psize--;
8432 }
8433 else
8434 {
8435 cmp = compare_cases (p, q);
8436 if (cmp < 0)
8437 {
8438 /* The whole case range for P is less than the
8439 one for Q. */
8440 e = p;
8441 p = p->right;
8442 psize--;
8443 }
8444 else if (cmp > 0)
8445 {
8446 /* The whole case range for Q is greater than
8447 the case range for P. */
8448 e = q;
8449 q = q->right;
8450 qsize--;
8451 }
8452 else
8453 {
8454 /* The cases overlap, or they are the same
8455 element in the list. Either way, we must
8456 issue an error and get the next case from P. */
8457 /* FIXME: Sort P and Q by line number. */
8458 gfc_error ("CASE label at %L overlaps with CASE "
8459 "label at %L", &p->where, &q->where);
8460 overlap_seen = 1;
8461 e = p;
8462 p = p->right;
8463 psize--;
8464 }
8465 }
8466
8467 /* Add the next element to the merged list. */
8468 if (tail)
8469 tail->right = e;
8470 else
8471 list = e;
8472 e->left = tail;
8473 tail = e;
8474 }
8475
8476 /* P has now stepped INSIZE places along, and so has Q. So
8477 they're the same. */
8478 p = q;
8479 }
8480 tail->right = NULL;
8481
8482 /* If we have done only one merge or none at all, we've
8483 finished sorting the cases. */
8484 if (nmerges <= 1)
8485 {
8486 if (!overlap_seen)
8487 return list;
8488 else
8489 return NULL;
8490 }
8491
8492 /* Otherwise repeat, merging lists twice the size. */
8493 insize *= 2;
8494 }
8495 }
8496
8497
8498 /* Check to see if an expression is suitable for use in a CASE statement.
8499 Makes sure that all case expressions are scalar constants of the same
8500 type. Return false if anything is wrong. */
8501
8502 static bool
8503 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
8504 {
8505 if (e == NULL) return true;
8506
8507 if (e->ts.type != case_expr->ts.type)
8508 {
8509 gfc_error ("Expression in CASE statement at %L must be of type %s",
8510 &e->where, gfc_basic_typename (case_expr->ts.type));
8511 return false;
8512 }
8513
8514 /* C805 (R808) For a given case-construct, each case-value shall be of
8515 the same type as case-expr. For character type, length differences
8516 are allowed, but the kind type parameters shall be the same. */
8517
8518 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
8519 {
8520 gfc_error ("Expression in CASE statement at %L must be of kind %d",
8521 &e->where, case_expr->ts.kind);
8522 return false;
8523 }
8524
8525 /* Convert the case value kind to that of case expression kind,
8526 if needed */
8527
8528 if (e->ts.kind != case_expr->ts.kind)
8529 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
8530
8531 if (e->rank != 0)
8532 {
8533 gfc_error ("Expression in CASE statement at %L must be scalar",
8534 &e->where);
8535 return false;
8536 }
8537
8538 return true;
8539 }
8540
8541
8542 /* Given a completely parsed select statement, we:
8543
8544 - Validate all expressions and code within the SELECT.
8545 - Make sure that the selection expression is not of the wrong type.
8546 - Make sure that no case ranges overlap.
8547 - Eliminate unreachable cases and unreachable code resulting from
8548 removing case labels.
8549
8550 The standard does allow unreachable cases, e.g. CASE (5:3). But
8551 they are a hassle for code generation, and to prevent that, we just
8552 cut them out here. This is not necessary for overlapping cases
8553 because they are illegal and we never even try to generate code.
8554
8555 We have the additional caveat that a SELECT construct could have
8556 been a computed GOTO in the source code. Fortunately we can fairly
8557 easily work around that here: The case_expr for a "real" SELECT CASE
8558 is in code->expr1, but for a computed GOTO it is in code->expr2. All
8559 we have to do is make sure that the case_expr is a scalar integer
8560 expression. */
8561
8562 static void
8563 resolve_select (gfc_code *code, bool select_type)
8564 {
8565 gfc_code *body;
8566 gfc_expr *case_expr;
8567 gfc_case *cp, *default_case, *tail, *head;
8568 int seen_unreachable;
8569 int seen_logical;
8570 int ncases;
8571 bt type;
8572 bool t;
8573
8574 if (code->expr1 == NULL)
8575 {
8576 /* This was actually a computed GOTO statement. */
8577 case_expr = code->expr2;
8578 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
8579 gfc_error ("Selection expression in computed GOTO statement "
8580 "at %L must be a scalar integer expression",
8581 &case_expr->where);
8582
8583 /* Further checking is not necessary because this SELECT was built
8584 by the compiler, so it should always be OK. Just move the
8585 case_expr from expr2 to expr so that we can handle computed
8586 GOTOs as normal SELECTs from here on. */
8587 code->expr1 = code->expr2;
8588 code->expr2 = NULL;
8589 return;
8590 }
8591
8592 case_expr = code->expr1;
8593 type = case_expr->ts.type;
8594
8595 /* F08:C830. */
8596 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
8597 {
8598 gfc_error ("Argument of SELECT statement at %L cannot be %s",
8599 &case_expr->where, gfc_typename (case_expr));
8600
8601 /* Punt. Going on here just produce more garbage error messages. */
8602 return;
8603 }
8604
8605 /* F08:R842. */
8606 if (!select_type && case_expr->rank != 0)
8607 {
8608 gfc_error ("Argument of SELECT statement at %L must be a scalar "
8609 "expression", &case_expr->where);
8610
8611 /* Punt. */
8612 return;
8613 }
8614
8615 /* Raise a warning if an INTEGER case value exceeds the range of
8616 the case-expr. Later, all expressions will be promoted to the
8617 largest kind of all case-labels. */
8618
8619 if (type == BT_INTEGER)
8620 for (body = code->block; body; body = body->block)
8621 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8622 {
8623 if (cp->low
8624 && gfc_check_integer_range (cp->low->value.integer,
8625 case_expr->ts.kind) != ARITH_OK)
8626 gfc_warning (0, "Expression in CASE statement at %L is "
8627 "not in the range of %s", &cp->low->where,
8628 gfc_typename (case_expr));
8629
8630 if (cp->high
8631 && cp->low != cp->high
8632 && gfc_check_integer_range (cp->high->value.integer,
8633 case_expr->ts.kind) != ARITH_OK)
8634 gfc_warning (0, "Expression in CASE statement at %L is "
8635 "not in the range of %s", &cp->high->where,
8636 gfc_typename (case_expr));
8637 }
8638
8639 /* PR 19168 has a long discussion concerning a mismatch of the kinds
8640 of the SELECT CASE expression and its CASE values. Walk the lists
8641 of case values, and if we find a mismatch, promote case_expr to
8642 the appropriate kind. */
8643
8644 if (type == BT_LOGICAL || type == BT_INTEGER)
8645 {
8646 for (body = code->block; body; body = body->block)
8647 {
8648 /* Walk the case label list. */
8649 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8650 {
8651 /* Intercept the DEFAULT case. It does not have a kind. */
8652 if (cp->low == NULL && cp->high == NULL)
8653 continue;
8654
8655 /* Unreachable case ranges are discarded, so ignore. */
8656 if (cp->low != NULL && cp->high != NULL
8657 && cp->low != cp->high
8658 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8659 continue;
8660
8661 if (cp->low != NULL
8662 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
8663 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
8664
8665 if (cp->high != NULL
8666 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
8667 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
8668 }
8669 }
8670 }
8671
8672 /* Assume there is no DEFAULT case. */
8673 default_case = NULL;
8674 head = tail = NULL;
8675 ncases = 0;
8676 seen_logical = 0;
8677
8678 for (body = code->block; body; body = body->block)
8679 {
8680 /* Assume the CASE list is OK, and all CASE labels can be matched. */
8681 t = true;
8682 seen_unreachable = 0;
8683
8684 /* Walk the case label list, making sure that all case labels
8685 are legal. */
8686 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8687 {
8688 /* Count the number of cases in the whole construct. */
8689 ncases++;
8690
8691 /* Intercept the DEFAULT case. */
8692 if (cp->low == NULL && cp->high == NULL)
8693 {
8694 if (default_case != NULL)
8695 {
8696 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8697 "by a second DEFAULT CASE at %L",
8698 &default_case->where, &cp->where);
8699 t = false;
8700 break;
8701 }
8702 else
8703 {
8704 default_case = cp;
8705 continue;
8706 }
8707 }
8708
8709 /* Deal with single value cases and case ranges. Errors are
8710 issued from the validation function. */
8711 if (!validate_case_label_expr (cp->low, case_expr)
8712 || !validate_case_label_expr (cp->high, case_expr))
8713 {
8714 t = false;
8715 break;
8716 }
8717
8718 if (type == BT_LOGICAL
8719 && ((cp->low == NULL || cp->high == NULL)
8720 || cp->low != cp->high))
8721 {
8722 gfc_error ("Logical range in CASE statement at %L is not "
8723 "allowed", &cp->low->where);
8724 t = false;
8725 break;
8726 }
8727
8728 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
8729 {
8730 int value;
8731 value = cp->low->value.logical == 0 ? 2 : 1;
8732 if (value & seen_logical)
8733 {
8734 gfc_error ("Constant logical value in CASE statement "
8735 "is repeated at %L",
8736 &cp->low->where);
8737 t = false;
8738 break;
8739 }
8740 seen_logical |= value;
8741 }
8742
8743 if (cp->low != NULL && cp->high != NULL
8744 && cp->low != cp->high
8745 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8746 {
8747 if (warn_surprising)
8748 gfc_warning (OPT_Wsurprising,
8749 "Range specification at %L can never be matched",
8750 &cp->where);
8751
8752 cp->unreachable = 1;
8753 seen_unreachable = 1;
8754 }
8755 else
8756 {
8757 /* If the case range can be matched, it can also overlap with
8758 other cases. To make sure it does not, we put it in a
8759 double linked list here. We sort that with a merge sort
8760 later on to detect any overlapping cases. */
8761 if (!head)
8762 {
8763 head = tail = cp;
8764 head->right = head->left = NULL;
8765 }
8766 else
8767 {
8768 tail->right = cp;
8769 tail->right->left = tail;
8770 tail = tail->right;
8771 tail->right = NULL;
8772 }
8773 }
8774 }
8775
8776 /* It there was a failure in the previous case label, give up
8777 for this case label list. Continue with the next block. */
8778 if (!t)
8779 continue;
8780
8781 /* See if any case labels that are unreachable have been seen.
8782 If so, we eliminate them. This is a bit of a kludge because
8783 the case lists for a single case statement (label) is a
8784 single forward linked lists. */
8785 if (seen_unreachable)
8786 {
8787 /* Advance until the first case in the list is reachable. */
8788 while (body->ext.block.case_list != NULL
8789 && body->ext.block.case_list->unreachable)
8790 {
8791 gfc_case *n = body->ext.block.case_list;
8792 body->ext.block.case_list = body->ext.block.case_list->next;
8793 n->next = NULL;
8794 gfc_free_case_list (n);
8795 }
8796
8797 /* Strip all other unreachable cases. */
8798 if (body->ext.block.case_list)
8799 {
8800 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
8801 {
8802 if (cp->next->unreachable)
8803 {
8804 gfc_case *n = cp->next;
8805 cp->next = cp->next->next;
8806 n->next = NULL;
8807 gfc_free_case_list (n);
8808 }
8809 }
8810 }
8811 }
8812 }
8813
8814 /* See if there were overlapping cases. If the check returns NULL,
8815 there was overlap. In that case we don't do anything. If head
8816 is non-NULL, we prepend the DEFAULT case. The sorted list can
8817 then used during code generation for SELECT CASE constructs with
8818 a case expression of a CHARACTER type. */
8819 if (head)
8820 {
8821 head = check_case_overlap (head);
8822
8823 /* Prepend the default_case if it is there. */
8824 if (head != NULL && default_case)
8825 {
8826 default_case->left = NULL;
8827 default_case->right = head;
8828 head->left = default_case;
8829 }
8830 }
8831
8832 /* Eliminate dead blocks that may be the result if we've seen
8833 unreachable case labels for a block. */
8834 for (body = code; body && body->block; body = body->block)
8835 {
8836 if (body->block->ext.block.case_list == NULL)
8837 {
8838 /* Cut the unreachable block from the code chain. */
8839 gfc_code *c = body->block;
8840 body->block = c->block;
8841
8842 /* Kill the dead block, but not the blocks below it. */
8843 c->block = NULL;
8844 gfc_free_statements (c);
8845 }
8846 }
8847
8848 /* More than two cases is legal but insane for logical selects.
8849 Issue a warning for it. */
8850 if (warn_surprising && type == BT_LOGICAL && ncases > 2)
8851 gfc_warning (OPT_Wsurprising,
8852 "Logical SELECT CASE block at %L has more that two cases",
8853 &code->loc);
8854 }
8855
8856
8857 /* Check if a derived type is extensible. */
8858
8859 bool
8860 gfc_type_is_extensible (gfc_symbol *sym)
8861 {
8862 return !(sym->attr.is_bind_c || sym->attr.sequence
8863 || (sym->attr.is_class
8864 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
8865 }
8866
8867
8868 static void
8869 resolve_types (gfc_namespace *ns);
8870
8871 /* Resolve an associate-name: Resolve target and ensure the type-spec is
8872 correct as well as possibly the array-spec. */
8873
8874 static void
8875 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
8876 {
8877 gfc_expr* target;
8878
8879 gcc_assert (sym->assoc);
8880 gcc_assert (sym->attr.flavor == FL_VARIABLE);
8881
8882 /* If this is for SELECT TYPE, the target may not yet be set. In that
8883 case, return. Resolution will be called later manually again when
8884 this is done. */
8885 target = sym->assoc->target;
8886 if (!target)
8887 return;
8888 gcc_assert (!sym->assoc->dangling);
8889
8890 if (resolve_target && !gfc_resolve_expr (target))
8891 return;
8892
8893 /* For variable targets, we get some attributes from the target. */
8894 if (target->expr_type == EXPR_VARIABLE)
8895 {
8896 gfc_symbol *tsym, *dsym;
8897
8898 gcc_assert (target->symtree);
8899 tsym = target->symtree->n.sym;
8900
8901 if (gfc_expr_attr (target).proc_pointer)
8902 {
8903 gfc_error ("Associating entity %qs at %L is a procedure pointer",
8904 tsym->name, &target->where);
8905 return;
8906 }
8907
8908 if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
8909 && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
8910 && dsym->attr.flavor == FL_DERIVED)
8911 {
8912 gfc_error ("Derived type %qs cannot be used as a variable at %L",
8913 tsym->name, &target->where);
8914 return;
8915 }
8916
8917 if (tsym->attr.flavor == FL_PROCEDURE)
8918 {
8919 bool is_error = true;
8920 if (tsym->attr.function && tsym->result == tsym)
8921 for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
8922 if (tsym == ns->proc_name)
8923 {
8924 is_error = false;
8925 break;
8926 }
8927 if (is_error)
8928 {
8929 gfc_error ("Associating entity %qs at %L is a procedure name",
8930 tsym->name, &target->where);
8931 return;
8932 }
8933 }
8934
8935 sym->attr.asynchronous = tsym->attr.asynchronous;
8936 sym->attr.volatile_ = tsym->attr.volatile_;
8937
8938 sym->attr.target = tsym->attr.target
8939 || gfc_expr_attr (target).pointer;
8940 if (is_subref_array (target))
8941 sym->attr.subref_array_pointer = 1;
8942 }
8943 else if (target->ts.type == BT_PROCEDURE)
8944 {
8945 gfc_error ("Associating selector-expression at %L yields a procedure",
8946 &target->where);
8947 return;
8948 }
8949
8950 if (target->expr_type == EXPR_NULL)
8951 {
8952 gfc_error ("Selector at %L cannot be NULL()", &target->where);
8953 return;
8954 }
8955 else if (target->ts.type == BT_UNKNOWN)
8956 {
8957 gfc_error ("Selector at %L has no type", &target->where);
8958 return;
8959 }
8960
8961 /* Get type if this was not already set. Note that it can be
8962 some other type than the target in case this is a SELECT TYPE
8963 selector! So we must not update when the type is already there. */
8964 if (sym->ts.type == BT_UNKNOWN)
8965 sym->ts = target->ts;
8966
8967 gcc_assert (sym->ts.type != BT_UNKNOWN);
8968
8969 /* See if this is a valid association-to-variable. */
8970 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
8971 && !gfc_has_vector_subscript (target));
8972
8973 /* Finally resolve if this is an array or not. */
8974 if (sym->attr.dimension && target->rank == 0)
8975 {
8976 /* primary.c makes the assumption that a reference to an associate
8977 name followed by a left parenthesis is an array reference. */
8978 if (sym->ts.type != BT_CHARACTER)
8979 gfc_error ("Associate-name %qs at %L is used as array",
8980 sym->name, &sym->declared_at);
8981 sym->attr.dimension = 0;
8982 return;
8983 }
8984
8985
8986 /* We cannot deal with class selectors that need temporaries. */
8987 if (target->ts.type == BT_CLASS
8988 && gfc_ref_needs_temporary_p (target->ref))
8989 {
8990 gfc_error ("CLASS selector at %L needs a temporary which is not "
8991 "yet implemented", &target->where);
8992 return;
8993 }
8994
8995 if (target->ts.type == BT_CLASS)
8996 gfc_fix_class_refs (target);
8997
8998 if (target->rank != 0 && !sym->attr.select_rank_temporary)
8999 {
9000 gfc_array_spec *as;
9001 /* The rank may be incorrectly guessed at parsing, therefore make sure
9002 it is corrected now. */
9003 if (sym->ts.type != BT_CLASS && (!sym->as || sym->assoc->rankguessed))
9004 {
9005 if (!sym->as)
9006 sym->as = gfc_get_array_spec ();
9007 as = sym->as;
9008 as->rank = target->rank;
9009 as->type = AS_DEFERRED;
9010 as->corank = gfc_get_corank (target);
9011 sym->attr.dimension = 1;
9012 if (as->corank != 0)
9013 sym->attr.codimension = 1;
9014 }
9015 else if (sym->ts.type == BT_CLASS && (!CLASS_DATA (sym)->as || sym->assoc->rankguessed))
9016 {
9017 if (!CLASS_DATA (sym)->as)
9018 CLASS_DATA (sym)->as = gfc_get_array_spec ();
9019 as = CLASS_DATA (sym)->as;
9020 as->rank = target->rank;
9021 as->type = AS_DEFERRED;
9022 as->corank = gfc_get_corank (target);
9023 CLASS_DATA (sym)->attr.dimension = 1;
9024 if (as->corank != 0)
9025 CLASS_DATA (sym)->attr.codimension = 1;
9026 }
9027 }
9028 else if (!sym->attr.select_rank_temporary)
9029 {
9030 /* target's rank is 0, but the type of the sym is still array valued,
9031 which has to be corrected. */
9032 if (sym->ts.type == BT_CLASS
9033 && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
9034 {
9035 gfc_array_spec *as;
9036 symbol_attribute attr;
9037 /* The associated variable's type is still the array type
9038 correct this now. */
9039 gfc_typespec *ts = &target->ts;
9040 gfc_ref *ref;
9041 gfc_component *c;
9042 for (ref = target->ref; ref != NULL; ref = ref->next)
9043 {
9044 switch (ref->type)
9045 {
9046 case REF_COMPONENT:
9047 ts = &ref->u.c.component->ts;
9048 break;
9049 case REF_ARRAY:
9050 if (ts->type == BT_CLASS)
9051 ts = &ts->u.derived->components->ts;
9052 break;
9053 default:
9054 break;
9055 }
9056 }
9057 /* Create a scalar instance of the current class type. Because the
9058 rank of a class array goes into its name, the type has to be
9059 rebuild. The alternative of (re-)setting just the attributes
9060 and as in the current type, destroys the type also in other
9061 places. */
9062 as = NULL;
9063 sym->ts = *ts;
9064 sym->ts.type = BT_CLASS;
9065 attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
9066 attr.class_ok = 0;
9067 attr.associate_var = 1;
9068 attr.dimension = attr.codimension = 0;
9069 attr.class_pointer = 1;
9070 if (!gfc_build_class_symbol (&sym->ts, &attr, &as))
9071 gcc_unreachable ();
9072 /* Make sure the _vptr is set. */
9073 c = gfc_find_component (sym->ts.u.derived, "_vptr", true, true, NULL);
9074 if (c->ts.u.derived == NULL)
9075 c->ts.u.derived = gfc_find_derived_vtab (sym->ts.u.derived);
9076 CLASS_DATA (sym)->attr.pointer = 1;
9077 CLASS_DATA (sym)->attr.class_pointer = 1;
9078 gfc_set_sym_referenced (sym->ts.u.derived);
9079 gfc_commit_symbol (sym->ts.u.derived);
9080 /* _vptr now has the _vtab in it, change it to the _vtype. */
9081 if (c->ts.u.derived->attr.vtab)
9082 c->ts.u.derived = c->ts.u.derived->ts.u.derived;
9083 c->ts.u.derived->ns->types_resolved = 0;
9084 resolve_types (c->ts.u.derived->ns);
9085 }
9086 }
9087
9088 /* Mark this as an associate variable. */
9089 sym->attr.associate_var = 1;
9090
9091 /* Fix up the type-spec for CHARACTER types. */
9092 if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
9093 {
9094 if (!sym->ts.u.cl)
9095 sym->ts.u.cl = target->ts.u.cl;
9096
9097 if (sym->ts.deferred && target->expr_type == EXPR_VARIABLE
9098 && target->symtree->n.sym->attr.dummy
9099 && sym->ts.u.cl == target->ts.u.cl)
9100 {
9101 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
9102 sym->ts.deferred = 1;
9103 }
9104
9105 if (!sym->ts.u.cl->length
9106 && !sym->ts.deferred
9107 && target->expr_type == EXPR_CONSTANT)
9108 {
9109 sym->ts.u.cl->length =
9110 gfc_get_int_expr (gfc_charlen_int_kind, NULL,
9111 target->value.character.length);
9112 }
9113 else if ((!sym->ts.u.cl->length
9114 || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
9115 && target->expr_type != EXPR_VARIABLE)
9116 {
9117 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
9118 sym->ts.deferred = 1;
9119
9120 /* This is reset in trans-stmt.c after the assignment
9121 of the target expression to the associate name. */
9122 sym->attr.allocatable = 1;
9123 }
9124 }
9125
9126 /* If the target is a good class object, so is the associate variable. */
9127 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
9128 sym->attr.class_ok = 1;
9129 }
9130
9131
9132 /* Ensure that SELECT TYPE expressions have the correct rank and a full
9133 array reference, where necessary. The symbols are artificial and so
9134 the dimension attribute and arrayspec can also be set. In addition,
9135 sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
9136 This is corrected here as well.*/
9137
9138 static void
9139 fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2,
9140 int rank, gfc_ref *ref)
9141 {
9142 gfc_ref *nref = (*expr1)->ref;
9143 gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
9144 gfc_symbol *sym2 = expr2 ? expr2->symtree->n.sym : NULL;
9145 (*expr1)->rank = rank;
9146 if (sym1->ts.type == BT_CLASS)
9147 {
9148 if ((*expr1)->ts.type != BT_CLASS)
9149 (*expr1)->ts = sym1->ts;
9150
9151 CLASS_DATA (sym1)->attr.dimension = 1;
9152 if (CLASS_DATA (sym1)->as == NULL && sym2)
9153 CLASS_DATA (sym1)->as
9154 = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
9155 }
9156 else
9157 {
9158 sym1->attr.dimension = 1;
9159 if (sym1->as == NULL && sym2)
9160 sym1->as = gfc_copy_array_spec (sym2->as);
9161 }
9162
9163 for (; nref; nref = nref->next)
9164 if (nref->next == NULL)
9165 break;
9166
9167 if (ref && nref && nref->type != REF_ARRAY)
9168 nref->next = gfc_copy_ref (ref);
9169 else if (ref && !nref)
9170 (*expr1)->ref = gfc_copy_ref (ref);
9171 }
9172
9173
9174 static gfc_expr *
9175 build_loc_call (gfc_expr *sym_expr)
9176 {
9177 gfc_expr *loc_call;
9178 loc_call = gfc_get_expr ();
9179 loc_call->expr_type = EXPR_FUNCTION;
9180 gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
9181 loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
9182 loc_call->symtree->n.sym->attr.intrinsic = 1;
9183 loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
9184 gfc_commit_symbol (loc_call->symtree->n.sym);
9185 loc_call->ts.type = BT_INTEGER;
9186 loc_call->ts.kind = gfc_index_integer_kind;
9187 loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
9188 loc_call->value.function.actual = gfc_get_actual_arglist ();
9189 loc_call->value.function.actual->expr = sym_expr;
9190 loc_call->where = sym_expr->where;
9191 return loc_call;
9192 }
9193
9194 /* Resolve a SELECT TYPE statement. */
9195
9196 static void
9197 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
9198 {
9199 gfc_symbol *selector_type;
9200 gfc_code *body, *new_st, *if_st, *tail;
9201 gfc_code *class_is = NULL, *default_case = NULL;
9202 gfc_case *c;
9203 gfc_symtree *st;
9204 char name[GFC_MAX_SYMBOL_LEN];
9205 gfc_namespace *ns;
9206 int error = 0;
9207 int rank = 0;
9208 gfc_ref* ref = NULL;
9209 gfc_expr *selector_expr = NULL;
9210
9211 ns = code->ext.block.ns;
9212 gfc_resolve (ns);
9213
9214 /* Check for F03:C813. */
9215 if (code->expr1->ts.type != BT_CLASS
9216 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
9217 {
9218 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
9219 "at %L", &code->loc);
9220 return;
9221 }
9222
9223 if (!code->expr1->symtree->n.sym->attr.class_ok)
9224 return;
9225
9226 if (code->expr2)
9227 {
9228 gfc_ref *ref2 = NULL;
9229 for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
9230 if (ref->type == REF_COMPONENT
9231 && ref->u.c.component->ts.type == BT_CLASS)
9232 ref2 = ref;
9233
9234 if (ref2)
9235 {
9236 if (code->expr1->symtree->n.sym->attr.untyped)
9237 code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
9238 selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
9239 }
9240 else
9241 {
9242 if (code->expr1->symtree->n.sym->attr.untyped)
9243 code->expr1->symtree->n.sym->ts = code->expr2->ts;
9244 selector_type = CLASS_DATA (code->expr2)
9245 ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
9246 }
9247
9248 if (code->expr2->rank && CLASS_DATA (code->expr1)->as)
9249 CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
9250
9251 /* F2008: C803 The selector expression must not be coindexed. */
9252 if (gfc_is_coindexed (code->expr2))
9253 {
9254 gfc_error ("Selector at %L must not be coindexed",
9255 &code->expr2->where);
9256 return;
9257 }
9258
9259 }
9260 else
9261 {
9262 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
9263
9264 if (gfc_is_coindexed (code->expr1))
9265 {
9266 gfc_error ("Selector at %L must not be coindexed",
9267 &code->expr1->where);
9268 return;
9269 }
9270 }
9271
9272 /* Loop over TYPE IS / CLASS IS cases. */
9273 for (body = code->block; body; body = body->block)
9274 {
9275 c = body->ext.block.case_list;
9276
9277 if (!error)
9278 {
9279 /* Check for repeated cases. */
9280 for (tail = code->block; tail; tail = tail->block)
9281 {
9282 gfc_case *d = tail->ext.block.case_list;
9283 if (tail == body)
9284 break;
9285
9286 if (c->ts.type == d->ts.type
9287 && ((c->ts.type == BT_DERIVED
9288 && c->ts.u.derived && d->ts.u.derived
9289 && !strcmp (c->ts.u.derived->name,
9290 d->ts.u.derived->name))
9291 || c->ts.type == BT_UNKNOWN
9292 || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9293 && c->ts.kind == d->ts.kind)))
9294 {
9295 gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
9296 &c->where, &d->where);
9297 return;
9298 }
9299 }
9300 }
9301
9302 /* Check F03:C815. */
9303 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9304 && !selector_type->attr.unlimited_polymorphic
9305 && !gfc_type_is_extensible (c->ts.u.derived))
9306 {
9307 gfc_error ("Derived type %qs at %L must be extensible",
9308 c->ts.u.derived->name, &c->where);
9309 error++;
9310 continue;
9311 }
9312
9313 /* Check F03:C816. */
9314 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
9315 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
9316 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
9317 {
9318 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9319 gfc_error ("Derived type %qs at %L must be an extension of %qs",
9320 c->ts.u.derived->name, &c->where, selector_type->name);
9321 else
9322 gfc_error ("Unexpected intrinsic type %qs at %L",
9323 gfc_basic_typename (c->ts.type), &c->where);
9324 error++;
9325 continue;
9326 }
9327
9328 /* Check F03:C814. */
9329 if (c->ts.type == BT_CHARACTER
9330 && (c->ts.u.cl->length != NULL || c->ts.deferred))
9331 {
9332 gfc_error ("The type-spec at %L shall specify that each length "
9333 "type parameter is assumed", &c->where);
9334 error++;
9335 continue;
9336 }
9337
9338 /* Intercept the DEFAULT case. */
9339 if (c->ts.type == BT_UNKNOWN)
9340 {
9341 /* Check F03:C818. */
9342 if (default_case)
9343 {
9344 gfc_error ("The DEFAULT CASE at %L cannot be followed "
9345 "by a second DEFAULT CASE at %L",
9346 &default_case->ext.block.case_list->where, &c->where);
9347 error++;
9348 continue;
9349 }
9350
9351 default_case = body;
9352 }
9353 }
9354
9355 if (error > 0)
9356 return;
9357
9358 /* Transform SELECT TYPE statement to BLOCK and associate selector to
9359 target if present. If there are any EXIT statements referring to the
9360 SELECT TYPE construct, this is no problem because the gfc_code
9361 reference stays the same and EXIT is equally possible from the BLOCK
9362 it is changed to. */
9363 code->op = EXEC_BLOCK;
9364 if (code->expr2)
9365 {
9366 gfc_association_list* assoc;
9367
9368 assoc = gfc_get_association_list ();
9369 assoc->st = code->expr1->symtree;
9370 assoc->target = gfc_copy_expr (code->expr2);
9371 assoc->target->where = code->expr2->where;
9372 /* assoc->variable will be set by resolve_assoc_var. */
9373
9374 code->ext.block.assoc = assoc;
9375 code->expr1->symtree->n.sym->assoc = assoc;
9376
9377 resolve_assoc_var (code->expr1->symtree->n.sym, false);
9378 }
9379 else
9380 code->ext.block.assoc = NULL;
9381
9382 /* Ensure that the selector rank and arrayspec are available to
9383 correct expressions in which they might be missing. */
9384 if (code->expr2 && code->expr2->rank)
9385 {
9386 rank = code->expr2->rank;
9387 for (ref = code->expr2->ref; ref; ref = ref->next)
9388 if (ref->next == NULL)
9389 break;
9390 if (ref && ref->type == REF_ARRAY)
9391 ref = gfc_copy_ref (ref);
9392
9393 /* Fixup expr1 if necessary. */
9394 if (rank)
9395 fixup_array_ref (&code->expr1, code->expr2, rank, ref);
9396 }
9397 else if (code->expr1->rank)
9398 {
9399 rank = code->expr1->rank;
9400 for (ref = code->expr1->ref; ref; ref = ref->next)
9401 if (ref->next == NULL)
9402 break;
9403 if (ref && ref->type == REF_ARRAY)
9404 ref = gfc_copy_ref (ref);
9405 }
9406
9407 /* Add EXEC_SELECT to switch on type. */
9408 new_st = gfc_get_code (code->op);
9409 new_st->expr1 = code->expr1;
9410 new_st->expr2 = code->expr2;
9411 new_st->block = code->block;
9412 code->expr1 = code->expr2 = NULL;
9413 code->block = NULL;
9414 if (!ns->code)
9415 ns->code = new_st;
9416 else
9417 ns->code->next = new_st;
9418 code = new_st;
9419 code->op = EXEC_SELECT_TYPE;
9420
9421 /* Use the intrinsic LOC function to generate an integer expression
9422 for the vtable of the selector. Note that the rank of the selector
9423 expression has to be set to zero. */
9424 gfc_add_vptr_component (code->expr1);
9425 code->expr1->rank = 0;
9426 code->expr1 = build_loc_call (code->expr1);
9427 selector_expr = code->expr1->value.function.actual->expr;
9428
9429 /* Loop over TYPE IS / CLASS IS cases. */
9430 for (body = code->block; body; body = body->block)
9431 {
9432 gfc_symbol *vtab;
9433 gfc_expr *e;
9434 c = body->ext.block.case_list;
9435
9436 /* Generate an index integer expression for address of the
9437 TYPE/CLASS vtable and store it in c->low. The hash expression
9438 is stored in c->high and is used to resolve intrinsic cases. */
9439 if (c->ts.type != BT_UNKNOWN)
9440 {
9441 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9442 {
9443 vtab = gfc_find_derived_vtab (c->ts.u.derived);
9444 gcc_assert (vtab);
9445 c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
9446 c->ts.u.derived->hash_value);
9447 }
9448 else
9449 {
9450 vtab = gfc_find_vtab (&c->ts);
9451 gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
9452 e = CLASS_DATA (vtab)->initializer;
9453 c->high = gfc_copy_expr (e);
9454 if (c->high->ts.kind != gfc_integer_4_kind)
9455 {
9456 gfc_typespec ts;
9457 ts.kind = gfc_integer_4_kind;
9458 ts.type = BT_INTEGER;
9459 gfc_convert_type_warn (c->high, &ts, 2, 0);
9460 }
9461 }
9462
9463 e = gfc_lval_expr_from_sym (vtab);
9464 c->low = build_loc_call (e);
9465 }
9466 else
9467 continue;
9468
9469 /* Associate temporary to selector. This should only be done
9470 when this case is actually true, so build a new ASSOCIATE
9471 that does precisely this here (instead of using the
9472 'global' one). */
9473
9474 if (c->ts.type == BT_CLASS)
9475 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
9476 else if (c->ts.type == BT_DERIVED)
9477 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
9478 else if (c->ts.type == BT_CHARACTER)
9479 {
9480 HOST_WIDE_INT charlen = 0;
9481 if (c->ts.u.cl && c->ts.u.cl->length
9482 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9483 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
9484 snprintf (name, sizeof (name),
9485 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
9486 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
9487 }
9488 else
9489 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
9490 c->ts.kind);
9491
9492 st = gfc_find_symtree (ns->sym_root, name);
9493 gcc_assert (st->n.sym->assoc);
9494 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
9495 st->n.sym->assoc->target->where = selector_expr->where;
9496 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
9497 {
9498 gfc_add_data_component (st->n.sym->assoc->target);
9499 /* Fixup the target expression if necessary. */
9500 if (rank)
9501 fixup_array_ref (&st->n.sym->assoc->target, NULL, rank, ref);
9502 }
9503
9504 new_st = gfc_get_code (EXEC_BLOCK);
9505 new_st->ext.block.ns = gfc_build_block_ns (ns);
9506 new_st->ext.block.ns->code = body->next;
9507 body->next = new_st;
9508
9509 /* Chain in the new list only if it is marked as dangling. Otherwise
9510 there is a CASE label overlap and this is already used. Just ignore,
9511 the error is diagnosed elsewhere. */
9512 if (st->n.sym->assoc->dangling)
9513 {
9514 new_st->ext.block.assoc = st->n.sym->assoc;
9515 st->n.sym->assoc->dangling = 0;
9516 }
9517
9518 resolve_assoc_var (st->n.sym, false);
9519 }
9520
9521 /* Take out CLASS IS cases for separate treatment. */
9522 body = code;
9523 while (body && body->block)
9524 {
9525 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
9526 {
9527 /* Add to class_is list. */
9528 if (class_is == NULL)
9529 {
9530 class_is = body->block;
9531 tail = class_is;
9532 }
9533 else
9534 {
9535 for (tail = class_is; tail->block; tail = tail->block) ;
9536 tail->block = body->block;
9537 tail = tail->block;
9538 }
9539 /* Remove from EXEC_SELECT list. */
9540 body->block = body->block->block;
9541 tail->block = NULL;
9542 }
9543 else
9544 body = body->block;
9545 }
9546
9547 if (class_is)
9548 {
9549 gfc_symbol *vtab;
9550
9551 if (!default_case)
9552 {
9553 /* Add a default case to hold the CLASS IS cases. */
9554 for (tail = code; tail->block; tail = tail->block) ;
9555 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
9556 tail = tail->block;
9557 tail->ext.block.case_list = gfc_get_case ();
9558 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
9559 tail->next = NULL;
9560 default_case = tail;
9561 }
9562
9563 /* More than one CLASS IS block? */
9564 if (class_is->block)
9565 {
9566 gfc_code **c1,*c2;
9567 bool swapped;
9568 /* Sort CLASS IS blocks by extension level. */
9569 do
9570 {
9571 swapped = false;
9572 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
9573 {
9574 c2 = (*c1)->block;
9575 /* F03:C817 (check for doubles). */
9576 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
9577 == c2->ext.block.case_list->ts.u.derived->hash_value)
9578 {
9579 gfc_error ("Double CLASS IS block in SELECT TYPE "
9580 "statement at %L",
9581 &c2->ext.block.case_list->where);
9582 return;
9583 }
9584 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
9585 < c2->ext.block.case_list->ts.u.derived->attr.extension)
9586 {
9587 /* Swap. */
9588 (*c1)->block = c2->block;
9589 c2->block = *c1;
9590 *c1 = c2;
9591 swapped = true;
9592 }
9593 }
9594 }
9595 while (swapped);
9596 }
9597
9598 /* Generate IF chain. */
9599 if_st = gfc_get_code (EXEC_IF);
9600 new_st = if_st;
9601 for (body = class_is; body; body = body->block)
9602 {
9603 new_st->block = gfc_get_code (EXEC_IF);
9604 new_st = new_st->block;
9605 /* Set up IF condition: Call _gfortran_is_extension_of. */
9606 new_st->expr1 = gfc_get_expr ();
9607 new_st->expr1->expr_type = EXPR_FUNCTION;
9608 new_st->expr1->ts.type = BT_LOGICAL;
9609 new_st->expr1->ts.kind = 4;
9610 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
9611 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
9612 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
9613 /* Set up arguments. */
9614 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
9615 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
9616 new_st->expr1->value.function.actual->expr->where = code->loc;
9617 new_st->expr1->where = code->loc;
9618 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
9619 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
9620 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
9621 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
9622 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
9623 new_st->expr1->value.function.actual->next->expr->where = code->loc;
9624 new_st->next = body->next;
9625 }
9626 if (default_case->next)
9627 {
9628 new_st->block = gfc_get_code (EXEC_IF);
9629 new_st = new_st->block;
9630 new_st->next = default_case->next;
9631 }
9632
9633 /* Replace CLASS DEFAULT code by the IF chain. */
9634 default_case->next = if_st;
9635 }
9636
9637 /* Resolve the internal code. This cannot be done earlier because
9638 it requires that the sym->assoc of selectors is set already. */
9639 gfc_current_ns = ns;
9640 gfc_resolve_blocks (code->block, gfc_current_ns);
9641 gfc_current_ns = old_ns;
9642
9643 if (ref)
9644 free (ref);
9645 }
9646
9647
9648 /* Resolve a SELECT RANK statement. */
9649
9650 static void
9651 resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
9652 {
9653 gfc_namespace *ns;
9654 gfc_code *body, *new_st, *tail;
9655 gfc_case *c;
9656 char tname[GFC_MAX_SYMBOL_LEN + 7];
9657 char name[2 * GFC_MAX_SYMBOL_LEN];
9658 gfc_symtree *st;
9659 gfc_expr *selector_expr = NULL;
9660 int case_value;
9661 HOST_WIDE_INT charlen = 0;
9662
9663 ns = code->ext.block.ns;
9664 gfc_resolve (ns);
9665
9666 code->op = EXEC_BLOCK;
9667 if (code->expr2)
9668 {
9669 gfc_association_list* assoc;
9670
9671 assoc = gfc_get_association_list ();
9672 assoc->st = code->expr1->symtree;
9673 assoc->target = gfc_copy_expr (code->expr2);
9674 assoc->target->where = code->expr2->where;
9675 /* assoc->variable will be set by resolve_assoc_var. */
9676
9677 code->ext.block.assoc = assoc;
9678 code->expr1->symtree->n.sym->assoc = assoc;
9679
9680 resolve_assoc_var (code->expr1->symtree->n.sym, false);
9681 }
9682 else
9683 code->ext.block.assoc = NULL;
9684
9685 /* Loop over RANK cases. Note that returning on the errors causes a
9686 cascade of further errors because the case blocks do not compile
9687 correctly. */
9688 for (body = code->block; body; body = body->block)
9689 {
9690 c = body->ext.block.case_list;
9691 if (c->low)
9692 case_value = (int) mpz_get_si (c->low->value.integer);
9693 else
9694 case_value = -2;
9695
9696 /* Check for repeated cases. */
9697 for (tail = code->block; tail; tail = tail->block)
9698 {
9699 gfc_case *d = tail->ext.block.case_list;
9700 int case_value2;
9701
9702 if (tail == body)
9703 break;
9704
9705 /* Check F2018: C1153. */
9706 if (!c->low && !d->low)
9707 gfc_error ("RANK DEFAULT at %L is repeated at %L",
9708 &c->where, &d->where);
9709
9710 if (!c->low || !d->low)
9711 continue;
9712
9713 /* Check F2018: C1153. */
9714 case_value2 = (int) mpz_get_si (d->low->value.integer);
9715 if ((case_value == case_value2) && case_value == -1)
9716 gfc_error ("RANK (*) at %L is repeated at %L",
9717 &c->where, &d->where);
9718 else if (case_value == case_value2)
9719 gfc_error ("RANK (%i) at %L is repeated at %L",
9720 case_value, &c->where, &d->where);
9721 }
9722
9723 if (!c->low)
9724 continue;
9725
9726 /* Check F2018: C1155. */
9727 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
9728 || gfc_expr_attr (code->expr1).pointer))
9729 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9730 "allocatable selector at %L", &c->where, &code->expr1->where);
9731
9732 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
9733 || gfc_expr_attr (code->expr1).pointer))
9734 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9735 "allocatable selector at %L", &c->where, &code->expr1->where);
9736 }
9737
9738 /* Add EXEC_SELECT to switch on rank. */
9739 new_st = gfc_get_code (code->op);
9740 new_st->expr1 = code->expr1;
9741 new_st->expr2 = code->expr2;
9742 new_st->block = code->block;
9743 code->expr1 = code->expr2 = NULL;
9744 code->block = NULL;
9745 if (!ns->code)
9746 ns->code = new_st;
9747 else
9748 ns->code->next = new_st;
9749 code = new_st;
9750 code->op = EXEC_SELECT_RANK;
9751
9752 selector_expr = code->expr1;
9753
9754 /* Loop over SELECT RANK cases. */
9755 for (body = code->block; body; body = body->block)
9756 {
9757 c = body->ext.block.case_list;
9758 int case_value;
9759
9760 /* Pass on the default case. */
9761 if (c->low == NULL)
9762 continue;
9763
9764 /* Associate temporary to selector. This should only be done
9765 when this case is actually true, so build a new ASSOCIATE
9766 that does precisely this here (instead of using the
9767 'global' one). */
9768 if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
9769 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9770 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
9771
9772 if (c->ts.type == BT_CLASS)
9773 sprintf (tname, "class_%s", c->ts.u.derived->name);
9774 else if (c->ts.type == BT_DERIVED)
9775 sprintf (tname, "type_%s", c->ts.u.derived->name);
9776 else if (c->ts.type != BT_CHARACTER)
9777 sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
9778 else
9779 sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
9780 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
9781
9782 case_value = (int) mpz_get_si (c->low->value.integer);
9783 if (case_value >= 0)
9784 sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
9785 else
9786 sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
9787
9788 st = gfc_find_symtree (ns->sym_root, name);
9789 gcc_assert (st->n.sym->assoc);
9790
9791 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
9792 st->n.sym->assoc->target->where = selector_expr->where;
9793
9794 new_st = gfc_get_code (EXEC_BLOCK);
9795 new_st->ext.block.ns = gfc_build_block_ns (ns);
9796 new_st->ext.block.ns->code = body->next;
9797 body->next = new_st;
9798
9799 /* Chain in the new list only if it is marked as dangling. Otherwise
9800 there is a CASE label overlap and this is already used. Just ignore,
9801 the error is diagnosed elsewhere. */
9802 if (st->n.sym->assoc->dangling)
9803 {
9804 new_st->ext.block.assoc = st->n.sym->assoc;
9805 st->n.sym->assoc->dangling = 0;
9806 }
9807
9808 resolve_assoc_var (st->n.sym, false);
9809 }
9810
9811 gfc_current_ns = ns;
9812 gfc_resolve_blocks (code->block, gfc_current_ns);
9813 gfc_current_ns = old_ns;
9814 }
9815
9816
9817 /* Resolve a transfer statement. This is making sure that:
9818 -- a derived type being transferred has only non-pointer components
9819 -- a derived type being transferred doesn't have private components, unless
9820 it's being transferred from the module where the type was defined
9821 -- we're not trying to transfer a whole assumed size array. */
9822
9823 static void
9824 resolve_transfer (gfc_code *code)
9825 {
9826 gfc_symbol *sym, *derived;
9827 gfc_ref *ref;
9828 gfc_expr *exp;
9829 bool write = false;
9830 bool formatted = false;
9831 gfc_dt *dt = code->ext.dt;
9832 gfc_symbol *dtio_sub = NULL;
9833
9834 exp = code->expr1;
9835
9836 while (exp != NULL && exp->expr_type == EXPR_OP
9837 && exp->value.op.op == INTRINSIC_PARENTHESES)
9838 exp = exp->value.op.op1;
9839
9840 if (exp && exp->expr_type == EXPR_NULL
9841 && code->ext.dt)
9842 {
9843 gfc_error ("Invalid context for NULL () intrinsic at %L",
9844 &exp->where);
9845 return;
9846 }
9847
9848 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
9849 && exp->expr_type != EXPR_FUNCTION
9850 && exp->expr_type != EXPR_STRUCTURE))
9851 return;
9852
9853 /* If we are reading, the variable will be changed. Note that
9854 code->ext.dt may be NULL if the TRANSFER is related to
9855 an INQUIRE statement -- but in this case, we are not reading, either. */
9856 if (dt && dt->dt_io_kind->value.iokind == M_READ
9857 && !gfc_check_vardef_context (exp, false, false, false,
9858 _("item in READ")))
9859 return;
9860
9861 const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
9862 || exp->expr_type == EXPR_FUNCTION
9863 ? &exp->ts : &exp->symtree->n.sym->ts;
9864
9865 /* Go to actual component transferred. */
9866 for (ref = exp->ref; ref; ref = ref->next)
9867 if (ref->type == REF_COMPONENT)
9868 ts = &ref->u.c.component->ts;
9869
9870 if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
9871 && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
9872 {
9873 derived = ts->u.derived;
9874
9875 /* Determine when to use the formatted DTIO procedure. */
9876 if (dt && (dt->format_expr || dt->format_label))
9877 formatted = true;
9878
9879 write = dt->dt_io_kind->value.iokind == M_WRITE
9880 || dt->dt_io_kind->value.iokind == M_PRINT;
9881 dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
9882
9883 if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
9884 {
9885 dt->udtio = exp;
9886 sym = exp->symtree->n.sym->ns->proc_name;
9887 /* Check to see if this is a nested DTIO call, with the
9888 dummy as the io-list object. */
9889 if (sym && sym == dtio_sub && sym->formal
9890 && sym->formal->sym == exp->symtree->n.sym
9891 && exp->ref == NULL)
9892 {
9893 if (!sym->attr.recursive)
9894 {
9895 gfc_error ("DTIO %s procedure at %L must be recursive",
9896 sym->name, &sym->declared_at);
9897 return;
9898 }
9899 }
9900 }
9901 }
9902
9903 if (ts->type == BT_CLASS && dtio_sub == NULL)
9904 {
9905 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
9906 "it is processed by a defined input/output procedure",
9907 &code->loc);
9908 return;
9909 }
9910
9911 if (ts->type == BT_DERIVED)
9912 {
9913 /* Check that transferred derived type doesn't contain POINTER
9914 components unless it is processed by a defined input/output
9915 procedure". */
9916 if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
9917 {
9918 gfc_error ("Data transfer element at %L cannot have POINTER "
9919 "components unless it is processed by a defined "
9920 "input/output procedure", &code->loc);
9921 return;
9922 }
9923
9924 /* F08:C935. */
9925 if (ts->u.derived->attr.proc_pointer_comp)
9926 {
9927 gfc_error ("Data transfer element at %L cannot have "
9928 "procedure pointer components", &code->loc);
9929 return;
9930 }
9931
9932 if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
9933 {
9934 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
9935 "components unless it is processed by a defined "
9936 "input/output procedure", &code->loc);
9937 return;
9938 }
9939
9940 /* C_PTR and C_FUNPTR have private components which means they cannot
9941 be printed. However, if -std=gnu and not -pedantic, allow
9942 the component to be printed to help debugging. */
9943 if (ts->u.derived->ts.f90_type == BT_VOID)
9944 {
9945 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
9946 "cannot have PRIVATE components", &code->loc))
9947 return;
9948 }
9949 else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
9950 {
9951 gfc_error ("Data transfer element at %L cannot have "
9952 "PRIVATE components unless it is processed by "
9953 "a defined input/output procedure", &code->loc);
9954 return;
9955 }
9956 }
9957
9958 if (exp->expr_type == EXPR_STRUCTURE)
9959 return;
9960
9961 sym = exp->symtree->n.sym;
9962
9963 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
9964 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
9965 {
9966 gfc_error ("Data transfer element at %L cannot be a full reference to "
9967 "an assumed-size array", &code->loc);
9968 return;
9969 }
9970 }
9971
9972
9973 /*********** Toplevel code resolution subroutines ***********/
9974
9975 /* Find the set of labels that are reachable from this block. We also
9976 record the last statement in each block. */
9977
9978 static void
9979 find_reachable_labels (gfc_code *block)
9980 {
9981 gfc_code *c;
9982
9983 if (!block)
9984 return;
9985
9986 cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
9987
9988 /* Collect labels in this block. We don't keep those corresponding
9989 to END {IF|SELECT}, these are checked in resolve_branch by going
9990 up through the code_stack. */
9991 for (c = block; c; c = c->next)
9992 {
9993 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
9994 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
9995 }
9996
9997 /* Merge with labels from parent block. */
9998 if (cs_base->prev)
9999 {
10000 gcc_assert (cs_base->prev->reachable_labels);
10001 bitmap_ior_into (cs_base->reachable_labels,
10002 cs_base->prev->reachable_labels);
10003 }
10004 }
10005
10006
10007 static void
10008 resolve_lock_unlock_event (gfc_code *code)
10009 {
10010 if (code->expr1->expr_type == EXPR_FUNCTION
10011 && code->expr1->value.function.isym
10012 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
10013 remove_caf_get_intrinsic (code->expr1);
10014
10015 if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
10016 && (code->expr1->ts.type != BT_DERIVED
10017 || code->expr1->expr_type != EXPR_VARIABLE
10018 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
10019 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
10020 || code->expr1->rank != 0
10021 || (!gfc_is_coarray (code->expr1) &&
10022 !gfc_is_coindexed (code->expr1))))
10023 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
10024 &code->expr1->where);
10025 else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
10026 && (code->expr1->ts.type != BT_DERIVED
10027 || code->expr1->expr_type != EXPR_VARIABLE
10028 || code->expr1->ts.u.derived->from_intmod
10029 != INTMOD_ISO_FORTRAN_ENV
10030 || code->expr1->ts.u.derived->intmod_sym_id
10031 != ISOFORTRAN_EVENT_TYPE
10032 || code->expr1->rank != 0))
10033 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
10034 &code->expr1->where);
10035 else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
10036 && !gfc_is_coindexed (code->expr1))
10037 gfc_error ("Event variable argument at %L must be a coarray or coindexed",
10038 &code->expr1->where);
10039 else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
10040 gfc_error ("Event variable argument at %L must be a coarray but not "
10041 "coindexed", &code->expr1->where);
10042
10043 /* Check STAT. */
10044 if (code->expr2
10045 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
10046 || code->expr2->expr_type != EXPR_VARIABLE))
10047 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10048 &code->expr2->where);
10049
10050 if (code->expr2
10051 && !gfc_check_vardef_context (code->expr2, false, false, false,
10052 _("STAT variable")))
10053 return;
10054
10055 /* Check ERRMSG. */
10056 if (code->expr3
10057 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
10058 || code->expr3->expr_type != EXPR_VARIABLE))
10059 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10060 &code->expr3->where);
10061
10062 if (code->expr3
10063 && !gfc_check_vardef_context (code->expr3, false, false, false,
10064 _("ERRMSG variable")))
10065 return;
10066
10067 /* Check for LOCK the ACQUIRED_LOCK. */
10068 if (code->op != EXEC_EVENT_WAIT && code->expr4
10069 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
10070 || code->expr4->expr_type != EXPR_VARIABLE))
10071 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
10072 "variable", &code->expr4->where);
10073
10074 if (code->op != EXEC_EVENT_WAIT && code->expr4
10075 && !gfc_check_vardef_context (code->expr4, false, false, false,
10076 _("ACQUIRED_LOCK variable")))
10077 return;
10078
10079 /* Check for EVENT WAIT the UNTIL_COUNT. */
10080 if (code->op == EXEC_EVENT_WAIT && code->expr4)
10081 {
10082 if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
10083 || code->expr4->rank != 0)
10084 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
10085 "expression", &code->expr4->where);
10086 }
10087 }
10088
10089
10090 static void
10091 resolve_critical (gfc_code *code)
10092 {
10093 gfc_symtree *symtree;
10094 gfc_symbol *lock_type;
10095 char name[GFC_MAX_SYMBOL_LEN];
10096 static int serial = 0;
10097
10098 if (flag_coarray != GFC_FCOARRAY_LIB)
10099 return;
10100
10101 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
10102 GFC_PREFIX ("lock_type"));
10103 if (symtree)
10104 lock_type = symtree->n.sym;
10105 else
10106 {
10107 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
10108 false) != 0)
10109 gcc_unreachable ();
10110 lock_type = symtree->n.sym;
10111 lock_type->attr.flavor = FL_DERIVED;
10112 lock_type->attr.zero_comp = 1;
10113 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
10114 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
10115 }
10116
10117 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
10118 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
10119 gcc_unreachable ();
10120
10121 code->resolved_sym = symtree->n.sym;
10122 symtree->n.sym->attr.flavor = FL_VARIABLE;
10123 symtree->n.sym->attr.referenced = 1;
10124 symtree->n.sym->attr.artificial = 1;
10125 symtree->n.sym->attr.codimension = 1;
10126 symtree->n.sym->ts.type = BT_DERIVED;
10127 symtree->n.sym->ts.u.derived = lock_type;
10128 symtree->n.sym->as = gfc_get_array_spec ();
10129 symtree->n.sym->as->corank = 1;
10130 symtree->n.sym->as->type = AS_EXPLICIT;
10131 symtree->n.sym->as->cotype = AS_EXPLICIT;
10132 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
10133 NULL, 1);
10134 gfc_commit_symbols();
10135 }
10136
10137
10138 static void
10139 resolve_sync (gfc_code *code)
10140 {
10141 /* Check imageset. The * case matches expr1 == NULL. */
10142 if (code->expr1)
10143 {
10144 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
10145 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
10146 "INTEGER expression", &code->expr1->where);
10147 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
10148 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
10149 gfc_error ("Imageset argument at %L must between 1 and num_images()",
10150 &code->expr1->where);
10151 else if (code->expr1->expr_type == EXPR_ARRAY
10152 && gfc_simplify_expr (code->expr1, 0))
10153 {
10154 gfc_constructor *cons;
10155 cons = gfc_constructor_first (code->expr1->value.constructor);
10156 for (; cons; cons = gfc_constructor_next (cons))
10157 if (cons->expr->expr_type == EXPR_CONSTANT
10158 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
10159 gfc_error ("Imageset argument at %L must between 1 and "
10160 "num_images()", &cons->expr->where);
10161 }
10162 }
10163
10164 /* Check STAT. */
10165 gfc_resolve_expr (code->expr2);
10166 if (code->expr2
10167 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
10168 || code->expr2->expr_type != EXPR_VARIABLE))
10169 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10170 &code->expr2->where);
10171
10172 /* Check ERRMSG. */
10173 gfc_resolve_expr (code->expr3);
10174 if (code->expr3
10175 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
10176 || code->expr3->expr_type != EXPR_VARIABLE))
10177 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10178 &code->expr3->where);
10179 }
10180
10181
10182 /* Given a branch to a label, see if the branch is conforming.
10183 The code node describes where the branch is located. */
10184
10185 static void
10186 resolve_branch (gfc_st_label *label, gfc_code *code)
10187 {
10188 code_stack *stack;
10189
10190 if (label == NULL)
10191 return;
10192
10193 /* Step one: is this a valid branching target? */
10194
10195 if (label->defined == ST_LABEL_UNKNOWN)
10196 {
10197 gfc_error ("Label %d referenced at %L is never defined", label->value,
10198 &code->loc);
10199 return;
10200 }
10201
10202 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
10203 {
10204 gfc_error ("Statement at %L is not a valid branch target statement "
10205 "for the branch statement at %L", &label->where, &code->loc);
10206 return;
10207 }
10208
10209 /* Step two: make sure this branch is not a branch to itself ;-) */
10210
10211 if (code->here == label)
10212 {
10213 gfc_warning (0,
10214 "Branch at %L may result in an infinite loop", &code->loc);
10215 return;
10216 }
10217
10218 /* Step three: See if the label is in the same block as the
10219 branching statement. The hard work has been done by setting up
10220 the bitmap reachable_labels. */
10221
10222 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
10223 {
10224 /* Check now whether there is a CRITICAL construct; if so, check
10225 whether the label is still visible outside of the CRITICAL block,
10226 which is invalid. */
10227 for (stack = cs_base; stack; stack = stack->prev)
10228 {
10229 if (stack->current->op == EXEC_CRITICAL
10230 && bitmap_bit_p (stack->reachable_labels, label->value))
10231 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
10232 "label at %L", &code->loc, &label->where);
10233 else if (stack->current->op == EXEC_DO_CONCURRENT
10234 && bitmap_bit_p (stack->reachable_labels, label->value))
10235 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
10236 "for label at %L", &code->loc, &label->where);
10237 }
10238
10239 return;
10240 }
10241
10242 /* Step four: If we haven't found the label in the bitmap, it may
10243 still be the label of the END of the enclosing block, in which
10244 case we find it by going up the code_stack. */
10245
10246 for (stack = cs_base; stack; stack = stack->prev)
10247 {
10248 if (stack->current->next && stack->current->next->here == label)
10249 break;
10250 if (stack->current->op == EXEC_CRITICAL)
10251 {
10252 /* Note: A label at END CRITICAL does not leave the CRITICAL
10253 construct as END CRITICAL is still part of it. */
10254 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
10255 " at %L", &code->loc, &label->where);
10256 return;
10257 }
10258 else if (stack->current->op == EXEC_DO_CONCURRENT)
10259 {
10260 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
10261 "label at %L", &code->loc, &label->where);
10262 return;
10263 }
10264 }
10265
10266 if (stack)
10267 {
10268 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
10269 return;
10270 }
10271
10272 /* The label is not in an enclosing block, so illegal. This was
10273 allowed in Fortran 66, so we allow it as extension. No
10274 further checks are necessary in this case. */
10275 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
10276 "as the GOTO statement at %L", &label->where,
10277 &code->loc);
10278 return;
10279 }
10280
10281
10282 /* Check whether EXPR1 has the same shape as EXPR2. */
10283
10284 static bool
10285 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
10286 {
10287 mpz_t shape[GFC_MAX_DIMENSIONS];
10288 mpz_t shape2[GFC_MAX_DIMENSIONS];
10289 bool result = false;
10290 int i;
10291
10292 /* Compare the rank. */
10293 if (expr1->rank != expr2->rank)
10294 return result;
10295
10296 /* Compare the size of each dimension. */
10297 for (i=0; i<expr1->rank; i++)
10298 {
10299 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
10300 goto ignore;
10301
10302 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
10303 goto ignore;
10304
10305 if (mpz_cmp (shape[i], shape2[i]))
10306 goto over;
10307 }
10308
10309 /* When either of the two expression is an assumed size array, we
10310 ignore the comparison of dimension sizes. */
10311 ignore:
10312 result = true;
10313
10314 over:
10315 gfc_clear_shape (shape, i);
10316 gfc_clear_shape (shape2, i);
10317 return result;
10318 }
10319
10320
10321 /* Check whether a WHERE assignment target or a WHERE mask expression
10322 has the same shape as the outmost WHERE mask expression. */
10323
10324 static void
10325 resolve_where (gfc_code *code, gfc_expr *mask)
10326 {
10327 gfc_code *cblock;
10328 gfc_code *cnext;
10329 gfc_expr *e = NULL;
10330
10331 cblock = code->block;
10332
10333 /* Store the first WHERE mask-expr of the WHERE statement or construct.
10334 In case of nested WHERE, only the outmost one is stored. */
10335 if (mask == NULL) /* outmost WHERE */
10336 e = cblock->expr1;
10337 else /* inner WHERE */
10338 e = mask;
10339
10340 while (cblock)
10341 {
10342 if (cblock->expr1)
10343 {
10344 /* Check if the mask-expr has a consistent shape with the
10345 outmost WHERE mask-expr. */
10346 if (!resolve_where_shape (cblock->expr1, e))
10347 gfc_error ("WHERE mask at %L has inconsistent shape",
10348 &cblock->expr1->where);
10349 }
10350
10351 /* the assignment statement of a WHERE statement, or the first
10352 statement in where-body-construct of a WHERE construct */
10353 cnext = cblock->next;
10354 while (cnext)
10355 {
10356 switch (cnext->op)
10357 {
10358 /* WHERE assignment statement */
10359 case EXEC_ASSIGN:
10360
10361 /* Check shape consistent for WHERE assignment target. */
10362 if (e && !resolve_where_shape (cnext->expr1, e))
10363 gfc_error ("WHERE assignment target at %L has "
10364 "inconsistent shape", &cnext->expr1->where);
10365 break;
10366
10367
10368 case EXEC_ASSIGN_CALL:
10369 resolve_call (cnext);
10370 if (!cnext->resolved_sym->attr.elemental)
10371 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10372 &cnext->ext.actual->expr->where);
10373 break;
10374
10375 /* WHERE or WHERE construct is part of a where-body-construct */
10376 case EXEC_WHERE:
10377 resolve_where (cnext, e);
10378 break;
10379
10380 default:
10381 gfc_error ("Unsupported statement inside WHERE at %L",
10382 &cnext->loc);
10383 }
10384 /* the next statement within the same where-body-construct */
10385 cnext = cnext->next;
10386 }
10387 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10388 cblock = cblock->block;
10389 }
10390 }
10391
10392
10393 /* Resolve assignment in FORALL construct.
10394 NVAR is the number of FORALL index variables, and VAR_EXPR records the
10395 FORALL index variables. */
10396
10397 static void
10398 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
10399 {
10400 int n;
10401
10402 for (n = 0; n < nvar; n++)
10403 {
10404 gfc_symbol *forall_index;
10405
10406 forall_index = var_expr[n]->symtree->n.sym;
10407
10408 /* Check whether the assignment target is one of the FORALL index
10409 variable. */
10410 if ((code->expr1->expr_type == EXPR_VARIABLE)
10411 && (code->expr1->symtree->n.sym == forall_index))
10412 gfc_error ("Assignment to a FORALL index variable at %L",
10413 &code->expr1->where);
10414 else
10415 {
10416 /* If one of the FORALL index variables doesn't appear in the
10417 assignment variable, then there could be a many-to-one
10418 assignment. Emit a warning rather than an error because the
10419 mask could be resolving this problem. */
10420 if (!find_forall_index (code->expr1, forall_index, 0))
10421 gfc_warning (0, "The FORALL with index %qs is not used on the "
10422 "left side of the assignment at %L and so might "
10423 "cause multiple assignment to this object",
10424 var_expr[n]->symtree->name, &code->expr1->where);
10425 }
10426 }
10427 }
10428
10429
10430 /* Resolve WHERE statement in FORALL construct. */
10431
10432 static void
10433 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
10434 gfc_expr **var_expr)
10435 {
10436 gfc_code *cblock;
10437 gfc_code *cnext;
10438
10439 cblock = code->block;
10440 while (cblock)
10441 {
10442 /* the assignment statement of a WHERE statement, or the first
10443 statement in where-body-construct of a WHERE construct */
10444 cnext = cblock->next;
10445 while (cnext)
10446 {
10447 switch (cnext->op)
10448 {
10449 /* WHERE assignment statement */
10450 case EXEC_ASSIGN:
10451 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
10452 break;
10453
10454 /* WHERE operator assignment statement */
10455 case EXEC_ASSIGN_CALL:
10456 resolve_call (cnext);
10457 if (!cnext->resolved_sym->attr.elemental)
10458 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10459 &cnext->ext.actual->expr->where);
10460 break;
10461
10462 /* WHERE or WHERE construct is part of a where-body-construct */
10463 case EXEC_WHERE:
10464 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
10465 break;
10466
10467 default:
10468 gfc_error ("Unsupported statement inside WHERE at %L",
10469 &cnext->loc);
10470 }
10471 /* the next statement within the same where-body-construct */
10472 cnext = cnext->next;
10473 }
10474 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10475 cblock = cblock->block;
10476 }
10477 }
10478
10479
10480 /* Traverse the FORALL body to check whether the following errors exist:
10481 1. For assignment, check if a many-to-one assignment happens.
10482 2. For WHERE statement, check the WHERE body to see if there is any
10483 many-to-one assignment. */
10484
10485 static void
10486 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
10487 {
10488 gfc_code *c;
10489
10490 c = code->block->next;
10491 while (c)
10492 {
10493 switch (c->op)
10494 {
10495 case EXEC_ASSIGN:
10496 case EXEC_POINTER_ASSIGN:
10497 gfc_resolve_assign_in_forall (c, nvar, var_expr);
10498 break;
10499
10500 case EXEC_ASSIGN_CALL:
10501 resolve_call (c);
10502 break;
10503
10504 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
10505 there is no need to handle it here. */
10506 case EXEC_FORALL:
10507 break;
10508 case EXEC_WHERE:
10509 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
10510 break;
10511 default:
10512 break;
10513 }
10514 /* The next statement in the FORALL body. */
10515 c = c->next;
10516 }
10517 }
10518
10519
10520 /* Counts the number of iterators needed inside a forall construct, including
10521 nested forall constructs. This is used to allocate the needed memory
10522 in gfc_resolve_forall. */
10523
10524 static int
10525 gfc_count_forall_iterators (gfc_code *code)
10526 {
10527 int max_iters, sub_iters, current_iters;
10528 gfc_forall_iterator *fa;
10529
10530 gcc_assert(code->op == EXEC_FORALL);
10531 max_iters = 0;
10532 current_iters = 0;
10533
10534 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
10535 current_iters ++;
10536
10537 code = code->block->next;
10538
10539 while (code)
10540 {
10541 if (code->op == EXEC_FORALL)
10542 {
10543 sub_iters = gfc_count_forall_iterators (code);
10544 if (sub_iters > max_iters)
10545 max_iters = sub_iters;
10546 }
10547 code = code->next;
10548 }
10549
10550 return current_iters + max_iters;
10551 }
10552
10553
10554 /* Given a FORALL construct, first resolve the FORALL iterator, then call
10555 gfc_resolve_forall_body to resolve the FORALL body. */
10556
10557 static void
10558 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
10559 {
10560 static gfc_expr **var_expr;
10561 static int total_var = 0;
10562 static int nvar = 0;
10563 int i, old_nvar, tmp;
10564 gfc_forall_iterator *fa;
10565
10566 old_nvar = nvar;
10567
10568 if (!gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
10569 return;
10570
10571 /* Start to resolve a FORALL construct */
10572 if (forall_save == 0)
10573 {
10574 /* Count the total number of FORALL indices in the nested FORALL
10575 construct in order to allocate the VAR_EXPR with proper size. */
10576 total_var = gfc_count_forall_iterators (code);
10577
10578 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
10579 var_expr = XCNEWVEC (gfc_expr *, total_var);
10580 }
10581
10582 /* The information about FORALL iterator, including FORALL indices start, end
10583 and stride. An outer FORALL indice cannot appear in start, end or stride. */
10584 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
10585 {
10586 /* Fortran 20008: C738 (R753). */
10587 if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
10588 {
10589 gfc_error ("FORALL index-name at %L must be a scalar variable "
10590 "of type integer", &fa->var->where);
10591 continue;
10592 }
10593
10594 /* Check if any outer FORALL index name is the same as the current
10595 one. */
10596 for (i = 0; i < nvar; i++)
10597 {
10598 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
10599 gfc_error ("An outer FORALL construct already has an index "
10600 "with this name %L", &fa->var->where);
10601 }
10602
10603 /* Record the current FORALL index. */
10604 var_expr[nvar] = gfc_copy_expr (fa->var);
10605
10606 nvar++;
10607
10608 /* No memory leak. */
10609 gcc_assert (nvar <= total_var);
10610 }
10611
10612 /* Resolve the FORALL body. */
10613 gfc_resolve_forall_body (code, nvar, var_expr);
10614
10615 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
10616 gfc_resolve_blocks (code->block, ns);
10617
10618 tmp = nvar;
10619 nvar = old_nvar;
10620 /* Free only the VAR_EXPRs allocated in this frame. */
10621 for (i = nvar; i < tmp; i++)
10622 gfc_free_expr (var_expr[i]);
10623
10624 if (nvar == 0)
10625 {
10626 /* We are in the outermost FORALL construct. */
10627 gcc_assert (forall_save == 0);
10628
10629 /* VAR_EXPR is not needed any more. */
10630 free (var_expr);
10631 total_var = 0;
10632 }
10633 }
10634
10635
10636 /* Resolve a BLOCK construct statement. */
10637
10638 static void
10639 resolve_block_construct (gfc_code* code)
10640 {
10641 /* Resolve the BLOCK's namespace. */
10642 gfc_resolve (code->ext.block.ns);
10643
10644 /* For an ASSOCIATE block, the associations (and their targets) are already
10645 resolved during resolve_symbol. */
10646 }
10647
10648
10649 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
10650 DO code nodes. */
10651
10652 void
10653 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
10654 {
10655 bool t;
10656
10657 for (; b; b = b->block)
10658 {
10659 t = gfc_resolve_expr (b->expr1);
10660 if (!gfc_resolve_expr (b->expr2))
10661 t = false;
10662
10663 switch (b->op)
10664 {
10665 case EXEC_IF:
10666 if (t && b->expr1 != NULL
10667 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
10668 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
10669 &b->expr1->where);
10670 break;
10671
10672 case EXEC_WHERE:
10673 if (t
10674 && b->expr1 != NULL
10675 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
10676 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
10677 &b->expr1->where);
10678 break;
10679
10680 case EXEC_GOTO:
10681 resolve_branch (b->label1, b);
10682 break;
10683
10684 case EXEC_BLOCK:
10685 resolve_block_construct (b);
10686 break;
10687
10688 case EXEC_SELECT:
10689 case EXEC_SELECT_TYPE:
10690 case EXEC_SELECT_RANK:
10691 case EXEC_FORALL:
10692 case EXEC_DO:
10693 case EXEC_DO_WHILE:
10694 case EXEC_DO_CONCURRENT:
10695 case EXEC_CRITICAL:
10696 case EXEC_READ:
10697 case EXEC_WRITE:
10698 case EXEC_IOLENGTH:
10699 case EXEC_WAIT:
10700 break;
10701
10702 case EXEC_OMP_ATOMIC:
10703 case EXEC_OACC_ATOMIC:
10704 {
10705 gfc_omp_atomic_op aop
10706 = (gfc_omp_atomic_op) (b->ext.omp_atomic & GFC_OMP_ATOMIC_MASK);
10707
10708 /* Verify this before calling gfc_resolve_code, which might
10709 change it. */
10710 gcc_assert (b->next && b->next->op == EXEC_ASSIGN);
10711 gcc_assert (((aop != GFC_OMP_ATOMIC_CAPTURE)
10712 && b->next->next == NULL)
10713 || ((aop == GFC_OMP_ATOMIC_CAPTURE)
10714 && b->next->next != NULL
10715 && b->next->next->op == EXEC_ASSIGN
10716 && b->next->next->next == NULL));
10717 }
10718 break;
10719
10720 case EXEC_OACC_PARALLEL_LOOP:
10721 case EXEC_OACC_PARALLEL:
10722 case EXEC_OACC_KERNELS_LOOP:
10723 case EXEC_OACC_KERNELS:
10724 case EXEC_OACC_SERIAL_LOOP:
10725 case EXEC_OACC_SERIAL:
10726 case EXEC_OACC_DATA:
10727 case EXEC_OACC_HOST_DATA:
10728 case EXEC_OACC_LOOP:
10729 case EXEC_OACC_UPDATE:
10730 case EXEC_OACC_WAIT:
10731 case EXEC_OACC_CACHE:
10732 case EXEC_OACC_ENTER_DATA:
10733 case EXEC_OACC_EXIT_DATA:
10734 case EXEC_OACC_ROUTINE:
10735 case EXEC_OMP_CRITICAL:
10736 case EXEC_OMP_DISTRIBUTE:
10737 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
10738 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
10739 case EXEC_OMP_DISTRIBUTE_SIMD:
10740 case EXEC_OMP_DO:
10741 case EXEC_OMP_DO_SIMD:
10742 case EXEC_OMP_MASTER:
10743 case EXEC_OMP_ORDERED:
10744 case EXEC_OMP_PARALLEL:
10745 case EXEC_OMP_PARALLEL_DO:
10746 case EXEC_OMP_PARALLEL_DO_SIMD:
10747 case EXEC_OMP_PARALLEL_SECTIONS:
10748 case EXEC_OMP_PARALLEL_WORKSHARE:
10749 case EXEC_OMP_SECTIONS:
10750 case EXEC_OMP_SIMD:
10751 case EXEC_OMP_SINGLE:
10752 case EXEC_OMP_TARGET:
10753 case EXEC_OMP_TARGET_DATA:
10754 case EXEC_OMP_TARGET_ENTER_DATA:
10755 case EXEC_OMP_TARGET_EXIT_DATA:
10756 case EXEC_OMP_TARGET_PARALLEL:
10757 case EXEC_OMP_TARGET_PARALLEL_DO:
10758 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
10759 case EXEC_OMP_TARGET_SIMD:
10760 case EXEC_OMP_TARGET_TEAMS:
10761 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10762 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10763 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10764 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10765 case EXEC_OMP_TARGET_UPDATE:
10766 case EXEC_OMP_TASK:
10767 case EXEC_OMP_TASKGROUP:
10768 case EXEC_OMP_TASKLOOP:
10769 case EXEC_OMP_TASKLOOP_SIMD:
10770 case EXEC_OMP_TASKWAIT:
10771 case EXEC_OMP_TASKYIELD:
10772 case EXEC_OMP_TEAMS:
10773 case EXEC_OMP_TEAMS_DISTRIBUTE:
10774 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10775 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10776 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10777 case EXEC_OMP_WORKSHARE:
10778 break;
10779
10780 default:
10781 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
10782 }
10783
10784 gfc_resolve_code (b->next, ns);
10785 }
10786 }
10787
10788
10789 /* Does everything to resolve an ordinary assignment. Returns true
10790 if this is an interface assignment. */
10791 static bool
10792 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
10793 {
10794 bool rval = false;
10795 gfc_expr *lhs;
10796 gfc_expr *rhs;
10797 int n;
10798 gfc_ref *ref;
10799 symbol_attribute attr;
10800
10801 if (gfc_extend_assign (code, ns))
10802 {
10803 gfc_expr** rhsptr;
10804
10805 if (code->op == EXEC_ASSIGN_CALL)
10806 {
10807 lhs = code->ext.actual->expr;
10808 rhsptr = &code->ext.actual->next->expr;
10809 }
10810 else
10811 {
10812 gfc_actual_arglist* args;
10813 gfc_typebound_proc* tbp;
10814
10815 gcc_assert (code->op == EXEC_COMPCALL);
10816
10817 args = code->expr1->value.compcall.actual;
10818 lhs = args->expr;
10819 rhsptr = &args->next->expr;
10820
10821 tbp = code->expr1->value.compcall.tbp;
10822 gcc_assert (!tbp->is_generic);
10823 }
10824
10825 /* Make a temporary rhs when there is a default initializer
10826 and rhs is the same symbol as the lhs. */
10827 if ((*rhsptr)->expr_type == EXPR_VARIABLE
10828 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
10829 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
10830 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
10831 *rhsptr = gfc_get_parentheses (*rhsptr);
10832
10833 return true;
10834 }
10835
10836 lhs = code->expr1;
10837 rhs = code->expr2;
10838
10839 if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
10840 && rhs->ts.type == BT_CHARACTER
10841 && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
10842 {
10843 /* Use of -fdec-char-conversions allows assignment of character data
10844 to non-character variables. This not permited for nonconstant
10845 strings. */
10846 gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
10847 gfc_typename (lhs), &rhs->where);
10848 return false;
10849 }
10850
10851 /* Handle the case of a BOZ literal on the RHS. */
10852 if (rhs->ts.type == BT_BOZ)
10853 {
10854 if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
10855 "statement value nor an actual argument of "
10856 "INT/REAL/DBLE/CMPLX intrinsic subprogram",
10857 &rhs->where))
10858 return false;
10859
10860 switch (lhs->ts.type)
10861 {
10862 case BT_INTEGER:
10863 if (!gfc_boz2int (rhs, lhs->ts.kind))
10864 return false;
10865 break;
10866 case BT_REAL:
10867 if (!gfc_boz2real (rhs, lhs->ts.kind))
10868 return false;
10869 break;
10870 default:
10871 gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
10872 return false;
10873 }
10874 }
10875
10876 if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
10877 {
10878 HOST_WIDE_INT llen = 0, rlen = 0;
10879 if (lhs->ts.u.cl != NULL
10880 && lhs->ts.u.cl->length != NULL
10881 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10882 llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
10883
10884 if (rhs->expr_type == EXPR_CONSTANT)
10885 rlen = rhs->value.character.length;
10886
10887 else if (rhs->ts.u.cl != NULL
10888 && rhs->ts.u.cl->length != NULL
10889 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10890 rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
10891
10892 if (rlen && llen && rlen > llen)
10893 gfc_warning_now (OPT_Wcharacter_truncation,
10894 "CHARACTER expression will be truncated "
10895 "in assignment (%ld/%ld) at %L",
10896 (long) llen, (long) rlen, &code->loc);
10897 }
10898
10899 /* Ensure that a vector index expression for the lvalue is evaluated
10900 to a temporary if the lvalue symbol is referenced in it. */
10901 if (lhs->rank)
10902 {
10903 for (ref = lhs->ref; ref; ref= ref->next)
10904 if (ref->type == REF_ARRAY)
10905 {
10906 for (n = 0; n < ref->u.ar.dimen; n++)
10907 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
10908 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
10909 ref->u.ar.start[n]))
10910 ref->u.ar.start[n]
10911 = gfc_get_parentheses (ref->u.ar.start[n]);
10912 }
10913 }
10914
10915 if (gfc_pure (NULL))
10916 {
10917 if (lhs->ts.type == BT_DERIVED
10918 && lhs->expr_type == EXPR_VARIABLE
10919 && lhs->ts.u.derived->attr.pointer_comp
10920 && rhs->expr_type == EXPR_VARIABLE
10921 && (gfc_impure_variable (rhs->symtree->n.sym)
10922 || gfc_is_coindexed (rhs)))
10923 {
10924 /* F2008, C1283. */
10925 if (gfc_is_coindexed (rhs))
10926 gfc_error ("Coindexed expression at %L is assigned to "
10927 "a derived type variable with a POINTER "
10928 "component in a PURE procedure",
10929 &rhs->where);
10930 else
10931 /* F2008, C1283 (4). */
10932 gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
10933 "shall not be used as the expr at %L of an intrinsic "
10934 "assignment statement in which the variable is of a "
10935 "derived type if the derived type has a pointer "
10936 "component at any level of component selection.",
10937 &rhs->where);
10938 return rval;
10939 }
10940
10941 /* Fortran 2008, C1283. */
10942 if (gfc_is_coindexed (lhs))
10943 {
10944 gfc_error ("Assignment to coindexed variable at %L in a PURE "
10945 "procedure", &rhs->where);
10946 return rval;
10947 }
10948 }
10949
10950 if (gfc_implicit_pure (NULL))
10951 {
10952 if (lhs->expr_type == EXPR_VARIABLE
10953 && lhs->symtree->n.sym != gfc_current_ns->proc_name
10954 && lhs->symtree->n.sym->ns != gfc_current_ns)
10955 gfc_unset_implicit_pure (NULL);
10956
10957 if (lhs->ts.type == BT_DERIVED
10958 && lhs->expr_type == EXPR_VARIABLE
10959 && lhs->ts.u.derived->attr.pointer_comp
10960 && rhs->expr_type == EXPR_VARIABLE
10961 && (gfc_impure_variable (rhs->symtree->n.sym)
10962 || gfc_is_coindexed (rhs)))
10963 gfc_unset_implicit_pure (NULL);
10964
10965 /* Fortran 2008, C1283. */
10966 if (gfc_is_coindexed (lhs))
10967 gfc_unset_implicit_pure (NULL);
10968 }
10969
10970 /* F2008, 7.2.1.2. */
10971 attr = gfc_expr_attr (lhs);
10972 if (lhs->ts.type == BT_CLASS && attr.allocatable)
10973 {
10974 if (attr.codimension)
10975 {
10976 gfc_error ("Assignment to polymorphic coarray at %L is not "
10977 "permitted", &lhs->where);
10978 return false;
10979 }
10980 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
10981 "polymorphic variable at %L", &lhs->where))
10982 return false;
10983 if (!flag_realloc_lhs)
10984 {
10985 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
10986 "requires %<-frealloc-lhs%>", &lhs->where);
10987 return false;
10988 }
10989 }
10990 else if (lhs->ts.type == BT_CLASS)
10991 {
10992 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
10993 "assignment at %L - check that there is a matching specific "
10994 "subroutine for '=' operator", &lhs->where);
10995 return false;
10996 }
10997
10998 bool lhs_coindexed = gfc_is_coindexed (lhs);
10999
11000 /* F2008, Section 7.2.1.2. */
11001 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
11002 {
11003 gfc_error ("Coindexed variable must not have an allocatable ultimate "
11004 "component in assignment at %L", &lhs->where);
11005 return false;
11006 }
11007
11008 /* Assign the 'data' of a class object to a derived type. */
11009 if (lhs->ts.type == BT_DERIVED
11010 && rhs->ts.type == BT_CLASS
11011 && rhs->expr_type != EXPR_ARRAY)
11012 gfc_add_data_component (rhs);
11013
11014 /* Make sure there is a vtable and, in particular, a _copy for the
11015 rhs type. */
11016 if (UNLIMITED_POLY (lhs) && lhs->rank && rhs->ts.type != BT_CLASS)
11017 gfc_find_vtab (&rhs->ts);
11018
11019 bool caf_convert_to_send = flag_coarray == GFC_FCOARRAY_LIB
11020 && (lhs_coindexed
11021 || (code->expr2->expr_type == EXPR_FUNCTION
11022 && code->expr2->value.function.isym
11023 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
11024 && (code->expr1->rank == 0 || code->expr2->rank != 0)
11025 && !gfc_expr_attr (rhs).allocatable
11026 && !gfc_has_vector_subscript (rhs)));
11027
11028 gfc_check_assign (lhs, rhs, 1, !caf_convert_to_send);
11029
11030 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
11031 Additionally, insert this code when the RHS is a CAF as we then use the
11032 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
11033 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
11034 noncoindexed array and the RHS is a coindexed scalar, use the normal code
11035 path. */
11036 if (caf_convert_to_send)
11037 {
11038 if (code->expr2->expr_type == EXPR_FUNCTION
11039 && code->expr2->value.function.isym
11040 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
11041 remove_caf_get_intrinsic (code->expr2);
11042 code->op = EXEC_CALL;
11043 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
11044 code->resolved_sym = code->symtree->n.sym;
11045 code->resolved_sym->attr.flavor = FL_PROCEDURE;
11046 code->resolved_sym->attr.intrinsic = 1;
11047 code->resolved_sym->attr.subroutine = 1;
11048 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
11049 gfc_commit_symbol (code->resolved_sym);
11050 code->ext.actual = gfc_get_actual_arglist ();
11051 code->ext.actual->expr = lhs;
11052 code->ext.actual->next = gfc_get_actual_arglist ();
11053 code->ext.actual->next->expr = rhs;
11054 code->expr1 = NULL;
11055 code->expr2 = NULL;
11056 }
11057
11058 return false;
11059 }
11060
11061
11062 /* Add a component reference onto an expression. */
11063
11064 static void
11065 add_comp_ref (gfc_expr *e, gfc_component *c)
11066 {
11067 gfc_ref **ref;
11068 ref = &(e->ref);
11069 while (*ref)
11070 ref = &((*ref)->next);
11071 *ref = gfc_get_ref ();
11072 (*ref)->type = REF_COMPONENT;
11073 (*ref)->u.c.sym = e->ts.u.derived;
11074 (*ref)->u.c.component = c;
11075 e->ts = c->ts;
11076
11077 /* Add a full array ref, as necessary. */
11078 if (c->as)
11079 {
11080 gfc_add_full_array_ref (e, c->as);
11081 e->rank = c->as->rank;
11082 }
11083 }
11084
11085
11086 /* Build an assignment. Keep the argument 'op' for future use, so that
11087 pointer assignments can be made. */
11088
11089 static gfc_code *
11090 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
11091 gfc_component *comp1, gfc_component *comp2, locus loc)
11092 {
11093 gfc_code *this_code;
11094
11095 this_code = gfc_get_code (op);
11096 this_code->next = NULL;
11097 this_code->expr1 = gfc_copy_expr (expr1);
11098 this_code->expr2 = gfc_copy_expr (expr2);
11099 this_code->loc = loc;
11100 if (comp1 && comp2)
11101 {
11102 add_comp_ref (this_code->expr1, comp1);
11103 add_comp_ref (this_code->expr2, comp2);
11104 }
11105
11106 return this_code;
11107 }
11108
11109
11110 /* Makes a temporary variable expression based on the characteristics of
11111 a given variable expression. */
11112
11113 static gfc_expr*
11114 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
11115 {
11116 static int serial = 0;
11117 char name[GFC_MAX_SYMBOL_LEN];
11118 gfc_symtree *tmp;
11119 gfc_array_spec *as;
11120 gfc_array_ref *aref;
11121 gfc_ref *ref;
11122
11123 sprintf (name, GFC_PREFIX("DA%d"), serial++);
11124 gfc_get_sym_tree (name, ns, &tmp, false);
11125 gfc_add_type (tmp->n.sym, &e->ts, NULL);
11126
11127 if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
11128 tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
11129 NULL,
11130 e->value.character.length);
11131
11132 as = NULL;
11133 ref = NULL;
11134 aref = NULL;
11135
11136 /* Obtain the arrayspec for the temporary. */
11137 if (e->rank && e->expr_type != EXPR_ARRAY
11138 && e->expr_type != EXPR_FUNCTION
11139 && e->expr_type != EXPR_OP)
11140 {
11141 aref = gfc_find_array_ref (e);
11142 if (e->expr_type == EXPR_VARIABLE
11143 && e->symtree->n.sym->as == aref->as)
11144 as = aref->as;
11145 else
11146 {
11147 for (ref = e->ref; ref; ref = ref->next)
11148 if (ref->type == REF_COMPONENT
11149 && ref->u.c.component->as == aref->as)
11150 {
11151 as = aref->as;
11152 break;
11153 }
11154 }
11155 }
11156
11157 /* Add the attributes and the arrayspec to the temporary. */
11158 tmp->n.sym->attr = gfc_expr_attr (e);
11159 tmp->n.sym->attr.function = 0;
11160 tmp->n.sym->attr.result = 0;
11161 tmp->n.sym->attr.flavor = FL_VARIABLE;
11162 tmp->n.sym->attr.dummy = 0;
11163 tmp->n.sym->attr.intent = INTENT_UNKNOWN;
11164
11165 if (as)
11166 {
11167 tmp->n.sym->as = gfc_copy_array_spec (as);
11168 if (!ref)
11169 ref = e->ref;
11170 if (as->type == AS_DEFERRED)
11171 tmp->n.sym->attr.allocatable = 1;
11172 }
11173 else if (e->rank && (e->expr_type == EXPR_ARRAY
11174 || e->expr_type == EXPR_FUNCTION
11175 || e->expr_type == EXPR_OP))
11176 {
11177 tmp->n.sym->as = gfc_get_array_spec ();
11178 tmp->n.sym->as->type = AS_DEFERRED;
11179 tmp->n.sym->as->rank = e->rank;
11180 tmp->n.sym->attr.allocatable = 1;
11181 tmp->n.sym->attr.dimension = 1;
11182 }
11183 else
11184 tmp->n.sym->attr.dimension = 0;
11185
11186 gfc_set_sym_referenced (tmp->n.sym);
11187 gfc_commit_symbol (tmp->n.sym);
11188 e = gfc_lval_expr_from_sym (tmp->n.sym);
11189
11190 /* Should the lhs be a section, use its array ref for the
11191 temporary expression. */
11192 if (aref && aref->type != AR_FULL)
11193 {
11194 gfc_free_ref_list (e->ref);
11195 e->ref = gfc_copy_ref (ref);
11196 }
11197 return e;
11198 }
11199
11200
11201 /* Add one line of code to the code chain, making sure that 'head' and
11202 'tail' are appropriately updated. */
11203
11204 static void
11205 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
11206 {
11207 gcc_assert (this_code);
11208 if (*head == NULL)
11209 *head = *tail = *this_code;
11210 else
11211 *tail = gfc_append_code (*tail, *this_code);
11212 *this_code = NULL;
11213 }
11214
11215
11216 /* Counts the potential number of part array references that would
11217 result from resolution of typebound defined assignments. */
11218
11219 static int
11220 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
11221 {
11222 gfc_component *c;
11223 int c_depth = 0, t_depth;
11224
11225 for (c= derived->components; c; c = c->next)
11226 {
11227 if ((!gfc_bt_struct (c->ts.type)
11228 || c->attr.pointer
11229 || c->attr.allocatable
11230 || c->attr.proc_pointer_comp
11231 || c->attr.class_pointer
11232 || c->attr.proc_pointer)
11233 && !c->attr.defined_assign_comp)
11234 continue;
11235
11236 if (c->as && c_depth == 0)
11237 c_depth = 1;
11238
11239 if (c->ts.u.derived->attr.defined_assign_comp)
11240 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
11241 c->as ? 1 : 0);
11242 else
11243 t_depth = 0;
11244
11245 c_depth = t_depth > c_depth ? t_depth : c_depth;
11246 }
11247 return depth + c_depth;
11248 }
11249
11250
11251 /* Implement 7.2.1.3 of the F08 standard:
11252 "An intrinsic assignment where the variable is of derived type is
11253 performed as if each component of the variable were assigned from the
11254 corresponding component of expr using pointer assignment (7.2.2) for
11255 each pointer component, defined assignment for each nonpointer
11256 nonallocatable component of a type that has a type-bound defined
11257 assignment consistent with the component, intrinsic assignment for
11258 each other nonpointer nonallocatable component, ..."
11259
11260 The pointer assignments are taken care of by the intrinsic
11261 assignment of the structure itself. This function recursively adds
11262 defined assignments where required. The recursion is accomplished
11263 by calling gfc_resolve_code.
11264
11265 When the lhs in a defined assignment has intent INOUT, we need a
11266 temporary for the lhs. In pseudo-code:
11267
11268 ! Only call function lhs once.
11269 if (lhs is not a constant or an variable)
11270 temp_x = expr2
11271 expr2 => temp_x
11272 ! Do the intrinsic assignment
11273 expr1 = expr2
11274 ! Now do the defined assignments
11275 do over components with typebound defined assignment [%cmp]
11276 #if one component's assignment procedure is INOUT
11277 t1 = expr1
11278 #if expr2 non-variable
11279 temp_x = expr2
11280 expr2 => temp_x
11281 # endif
11282 expr1 = expr2
11283 # for each cmp
11284 t1%cmp {defined=} expr2%cmp
11285 expr1%cmp = t1%cmp
11286 #else
11287 expr1 = expr2
11288
11289 # for each cmp
11290 expr1%cmp {defined=} expr2%cmp
11291 #endif
11292 */
11293
11294 /* The temporary assignments have to be put on top of the additional
11295 code to avoid the result being changed by the intrinsic assignment.
11296 */
11297 static int component_assignment_level = 0;
11298 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
11299
11300 static void
11301 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
11302 {
11303 gfc_component *comp1, *comp2;
11304 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
11305 gfc_expr *t1;
11306 int error_count, depth;
11307
11308 gfc_get_errors (NULL, &error_count);
11309
11310 /* Filter out continuing processing after an error. */
11311 if (error_count
11312 || (*code)->expr1->ts.type != BT_DERIVED
11313 || (*code)->expr2->ts.type != BT_DERIVED)
11314 return;
11315
11316 /* TODO: Handle more than one part array reference in assignments. */
11317 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
11318 (*code)->expr1->rank ? 1 : 0);
11319 if (depth > 1)
11320 {
11321 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
11322 "done because multiple part array references would "
11323 "occur in intermediate expressions.", &(*code)->loc);
11324 return;
11325 }
11326
11327 component_assignment_level++;
11328
11329 /* Create a temporary so that functions get called only once. */
11330 if ((*code)->expr2->expr_type != EXPR_VARIABLE
11331 && (*code)->expr2->expr_type != EXPR_CONSTANT)
11332 {
11333 gfc_expr *tmp_expr;
11334
11335 /* Assign the rhs to the temporary. */
11336 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
11337 this_code = build_assignment (EXEC_ASSIGN,
11338 tmp_expr, (*code)->expr2,
11339 NULL, NULL, (*code)->loc);
11340 /* Add the code and substitute the rhs expression. */
11341 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
11342 gfc_free_expr ((*code)->expr2);
11343 (*code)->expr2 = tmp_expr;
11344 }
11345
11346 /* Do the intrinsic assignment. This is not needed if the lhs is one
11347 of the temporaries generated here, since the intrinsic assignment
11348 to the final result already does this. */
11349 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
11350 {
11351 this_code = build_assignment (EXEC_ASSIGN,
11352 (*code)->expr1, (*code)->expr2,
11353 NULL, NULL, (*code)->loc);
11354 add_code_to_chain (&this_code, &head, &tail);
11355 }
11356
11357 comp1 = (*code)->expr1->ts.u.derived->components;
11358 comp2 = (*code)->expr2->ts.u.derived->components;
11359
11360 t1 = NULL;
11361 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
11362 {
11363 bool inout = false;
11364
11365 /* The intrinsic assignment does the right thing for pointers
11366 of all kinds and allocatable components. */
11367 if (!gfc_bt_struct (comp1->ts.type)
11368 || comp1->attr.pointer
11369 || comp1->attr.allocatable
11370 || comp1->attr.proc_pointer_comp
11371 || comp1->attr.class_pointer
11372 || comp1->attr.proc_pointer)
11373 continue;
11374
11375 /* Make an assigment for this component. */
11376 this_code = build_assignment (EXEC_ASSIGN,
11377 (*code)->expr1, (*code)->expr2,
11378 comp1, comp2, (*code)->loc);
11379
11380 /* Convert the assignment if there is a defined assignment for
11381 this type. Otherwise, using the call from gfc_resolve_code,
11382 recurse into its components. */
11383 gfc_resolve_code (this_code, ns);
11384
11385 if (this_code->op == EXEC_ASSIGN_CALL)
11386 {
11387 gfc_formal_arglist *dummy_args;
11388 gfc_symbol *rsym;
11389 /* Check that there is a typebound defined assignment. If not,
11390 then this must be a module defined assignment. We cannot
11391 use the defined_assign_comp attribute here because it must
11392 be this derived type that has the defined assignment and not
11393 a parent type. */
11394 if (!(comp1->ts.u.derived->f2k_derived
11395 && comp1->ts.u.derived->f2k_derived
11396 ->tb_op[INTRINSIC_ASSIGN]))
11397 {
11398 gfc_free_statements (this_code);
11399 this_code = NULL;
11400 continue;
11401 }
11402
11403 /* If the first argument of the subroutine has intent INOUT
11404 a temporary must be generated and used instead. */
11405 rsym = this_code->resolved_sym;
11406 dummy_args = gfc_sym_get_dummy_args (rsym);
11407 if (dummy_args
11408 && dummy_args->sym->attr.intent == INTENT_INOUT)
11409 {
11410 gfc_code *temp_code;
11411 inout = true;
11412
11413 /* Build the temporary required for the assignment and put
11414 it at the head of the generated code. */
11415 if (!t1)
11416 {
11417 t1 = get_temp_from_expr ((*code)->expr1, ns);
11418 temp_code = build_assignment (EXEC_ASSIGN,
11419 t1, (*code)->expr1,
11420 NULL, NULL, (*code)->loc);
11421
11422 /* For allocatable LHS, check whether it is allocated. Note
11423 that allocatable components with defined assignment are
11424 not yet support. See PR 57696. */
11425 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
11426 {
11427 gfc_code *block;
11428 gfc_expr *e =
11429 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
11430 block = gfc_get_code (EXEC_IF);
11431 block->block = gfc_get_code (EXEC_IF);
11432 block->block->expr1
11433 = gfc_build_intrinsic_call (ns,
11434 GFC_ISYM_ALLOCATED, "allocated",
11435 (*code)->loc, 1, e);
11436 block->block->next = temp_code;
11437 temp_code = block;
11438 }
11439 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
11440 }
11441
11442 /* Replace the first actual arg with the component of the
11443 temporary. */
11444 gfc_free_expr (this_code->ext.actual->expr);
11445 this_code->ext.actual->expr = gfc_copy_expr (t1);
11446 add_comp_ref (this_code->ext.actual->expr, comp1);
11447
11448 /* If the LHS variable is allocatable and wasn't allocated and
11449 the temporary is allocatable, pointer assign the address of
11450 the freshly allocated LHS to the temporary. */
11451 if ((*code)->expr1->symtree->n.sym->attr.allocatable
11452 && gfc_expr_attr ((*code)->expr1).allocatable)
11453 {
11454 gfc_code *block;
11455 gfc_expr *cond;
11456
11457 cond = gfc_get_expr ();
11458 cond->ts.type = BT_LOGICAL;
11459 cond->ts.kind = gfc_default_logical_kind;
11460 cond->expr_type = EXPR_OP;
11461 cond->where = (*code)->loc;
11462 cond->value.op.op = INTRINSIC_NOT;
11463 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
11464 GFC_ISYM_ALLOCATED, "allocated",
11465 (*code)->loc, 1, gfc_copy_expr (t1));
11466 block = gfc_get_code (EXEC_IF);
11467 block->block = gfc_get_code (EXEC_IF);
11468 block->block->expr1 = cond;
11469 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
11470 t1, (*code)->expr1,
11471 NULL, NULL, (*code)->loc);
11472 add_code_to_chain (&block, &head, &tail);
11473 }
11474 }
11475 }
11476 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
11477 {
11478 /* Don't add intrinsic assignments since they are already
11479 effected by the intrinsic assignment of the structure. */
11480 gfc_free_statements (this_code);
11481 this_code = NULL;
11482 continue;
11483 }
11484
11485 add_code_to_chain (&this_code, &head, &tail);
11486
11487 if (t1 && inout)
11488 {
11489 /* Transfer the value to the final result. */
11490 this_code = build_assignment (EXEC_ASSIGN,
11491 (*code)->expr1, t1,
11492 comp1, comp2, (*code)->loc);
11493 add_code_to_chain (&this_code, &head, &tail);
11494 }
11495 }
11496
11497 /* Put the temporary assignments at the top of the generated code. */
11498 if (tmp_head && component_assignment_level == 1)
11499 {
11500 gfc_append_code (tmp_head, head);
11501 head = tmp_head;
11502 tmp_head = tmp_tail = NULL;
11503 }
11504
11505 // If we did a pointer assignment - thus, we need to ensure that the LHS is
11506 // not accidentally deallocated. Hence, nullify t1.
11507 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
11508 && gfc_expr_attr ((*code)->expr1).allocatable)
11509 {
11510 gfc_code *block;
11511 gfc_expr *cond;
11512 gfc_expr *e;
11513
11514 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
11515 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
11516 (*code)->loc, 2, gfc_copy_expr (t1), e);
11517 block = gfc_get_code (EXEC_IF);
11518 block->block = gfc_get_code (EXEC_IF);
11519 block->block->expr1 = cond;
11520 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
11521 t1, gfc_get_null_expr (&(*code)->loc),
11522 NULL, NULL, (*code)->loc);
11523 gfc_append_code (tail, block);
11524 tail = block;
11525 }
11526
11527 /* Now attach the remaining code chain to the input code. Step on
11528 to the end of the new code since resolution is complete. */
11529 gcc_assert ((*code)->op == EXEC_ASSIGN);
11530 tail->next = (*code)->next;
11531 /* Overwrite 'code' because this would place the intrinsic assignment
11532 before the temporary for the lhs is created. */
11533 gfc_free_expr ((*code)->expr1);
11534 gfc_free_expr ((*code)->expr2);
11535 **code = *head;
11536 if (head != tail)
11537 free (head);
11538 *code = tail;
11539
11540 component_assignment_level--;
11541 }
11542
11543
11544 /* F2008: Pointer function assignments are of the form:
11545 ptr_fcn (args) = expr
11546 This function breaks these assignments into two statements:
11547 temporary_pointer => ptr_fcn(args)
11548 temporary_pointer = expr */
11549
11550 static bool
11551 resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
11552 {
11553 gfc_expr *tmp_ptr_expr;
11554 gfc_code *this_code;
11555 gfc_component *comp;
11556 gfc_symbol *s;
11557
11558 if ((*code)->expr1->expr_type != EXPR_FUNCTION)
11559 return false;
11560
11561 /* Even if standard does not support this feature, continue to build
11562 the two statements to avoid upsetting frontend_passes.c. */
11563 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
11564 "%L", &(*code)->loc);
11565
11566 comp = gfc_get_proc_ptr_comp ((*code)->expr1);
11567
11568 if (comp)
11569 s = comp->ts.interface;
11570 else
11571 s = (*code)->expr1->symtree->n.sym;
11572
11573 if (s == NULL || !s->result->attr.pointer)
11574 {
11575 gfc_error ("The function result on the lhs of the assignment at "
11576 "%L must have the pointer attribute.",
11577 &(*code)->expr1->where);
11578 (*code)->op = EXEC_NOP;
11579 return false;
11580 }
11581
11582 tmp_ptr_expr = get_temp_from_expr ((*code)->expr2, ns);
11583
11584 /* get_temp_from_expression is set up for ordinary assignments. To that
11585 end, where array bounds are not known, arrays are made allocatable.
11586 Change the temporary to a pointer here. */
11587 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
11588 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
11589 tmp_ptr_expr->where = (*code)->loc;
11590
11591 this_code = build_assignment (EXEC_ASSIGN,
11592 tmp_ptr_expr, (*code)->expr2,
11593 NULL, NULL, (*code)->loc);
11594 this_code->next = (*code)->next;
11595 (*code)->next = this_code;
11596 (*code)->op = EXEC_POINTER_ASSIGN;
11597 (*code)->expr2 = (*code)->expr1;
11598 (*code)->expr1 = tmp_ptr_expr;
11599
11600 return true;
11601 }
11602
11603
11604 /* Deferred character length assignments from an operator expression
11605 require a temporary because the character length of the lhs can
11606 change in the course of the assignment. */
11607
11608 static bool
11609 deferred_op_assign (gfc_code **code, gfc_namespace *ns)
11610 {
11611 gfc_expr *tmp_expr;
11612 gfc_code *this_code;
11613
11614 if (!((*code)->expr1->ts.type == BT_CHARACTER
11615 && (*code)->expr1->ts.deferred && (*code)->expr1->rank
11616 && (*code)->expr2->expr_type == EXPR_OP))
11617 return false;
11618
11619 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
11620 return false;
11621
11622 if (gfc_expr_attr ((*code)->expr1).pointer)
11623 return false;
11624
11625 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
11626 tmp_expr->where = (*code)->loc;
11627
11628 /* A new charlen is required to ensure that the variable string
11629 length is different to that of the original lhs. */
11630 tmp_expr->ts.u.cl = gfc_get_charlen();
11631 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
11632 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
11633 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
11634
11635 tmp_expr->symtree->n.sym->ts.deferred = 1;
11636
11637 this_code = build_assignment (EXEC_ASSIGN,
11638 (*code)->expr1,
11639 gfc_copy_expr (tmp_expr),
11640 NULL, NULL, (*code)->loc);
11641
11642 (*code)->expr1 = tmp_expr;
11643
11644 this_code->next = (*code)->next;
11645 (*code)->next = this_code;
11646
11647 return true;
11648 }
11649
11650
11651 /* Given a block of code, recursively resolve everything pointed to by this
11652 code block. */
11653
11654 void
11655 gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
11656 {
11657 int omp_workshare_save;
11658 int forall_save, do_concurrent_save;
11659 code_stack frame;
11660 bool t;
11661
11662 frame.prev = cs_base;
11663 frame.head = code;
11664 cs_base = &frame;
11665
11666 find_reachable_labels (code);
11667
11668 for (; code; code = code->next)
11669 {
11670 frame.current = code;
11671 forall_save = forall_flag;
11672 do_concurrent_save = gfc_do_concurrent_flag;
11673
11674 if (code->op == EXEC_FORALL)
11675 {
11676 forall_flag = 1;
11677 gfc_resolve_forall (code, ns, forall_save);
11678 forall_flag = 2;
11679 }
11680 else if (code->block)
11681 {
11682 omp_workshare_save = -1;
11683 switch (code->op)
11684 {
11685 case EXEC_OACC_PARALLEL_LOOP:
11686 case EXEC_OACC_PARALLEL:
11687 case EXEC_OACC_KERNELS_LOOP:
11688 case EXEC_OACC_KERNELS:
11689 case EXEC_OACC_SERIAL_LOOP:
11690 case EXEC_OACC_SERIAL:
11691 case EXEC_OACC_DATA:
11692 case EXEC_OACC_HOST_DATA:
11693 case EXEC_OACC_LOOP:
11694 gfc_resolve_oacc_blocks (code, ns);
11695 break;
11696 case EXEC_OMP_PARALLEL_WORKSHARE:
11697 omp_workshare_save = omp_workshare_flag;
11698 omp_workshare_flag = 1;
11699 gfc_resolve_omp_parallel_blocks (code, ns);
11700 break;
11701 case EXEC_OMP_PARALLEL:
11702 case EXEC_OMP_PARALLEL_DO:
11703 case EXEC_OMP_PARALLEL_DO_SIMD:
11704 case EXEC_OMP_PARALLEL_SECTIONS:
11705 case EXEC_OMP_TARGET_PARALLEL:
11706 case EXEC_OMP_TARGET_PARALLEL_DO:
11707 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
11708 case EXEC_OMP_TARGET_TEAMS:
11709 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
11710 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
11711 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11712 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
11713 case EXEC_OMP_TASK:
11714 case EXEC_OMP_TASKLOOP:
11715 case EXEC_OMP_TASKLOOP_SIMD:
11716 case EXEC_OMP_TEAMS:
11717 case EXEC_OMP_TEAMS_DISTRIBUTE:
11718 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
11719 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11720 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
11721 omp_workshare_save = omp_workshare_flag;
11722 omp_workshare_flag = 0;
11723 gfc_resolve_omp_parallel_blocks (code, ns);
11724 break;
11725 case EXEC_OMP_DISTRIBUTE:
11726 case EXEC_OMP_DISTRIBUTE_SIMD:
11727 case EXEC_OMP_DO:
11728 case EXEC_OMP_DO_SIMD:
11729 case EXEC_OMP_SIMD:
11730 case EXEC_OMP_TARGET_SIMD:
11731 gfc_resolve_omp_do_blocks (code, ns);
11732 break;
11733 case EXEC_SELECT_TYPE:
11734 /* Blocks are handled in resolve_select_type because we have
11735 to transform the SELECT TYPE into ASSOCIATE first. */
11736 break;
11737 case EXEC_DO_CONCURRENT:
11738 gfc_do_concurrent_flag = 1;
11739 gfc_resolve_blocks (code->block, ns);
11740 gfc_do_concurrent_flag = 2;
11741 break;
11742 case EXEC_OMP_WORKSHARE:
11743 omp_workshare_save = omp_workshare_flag;
11744 omp_workshare_flag = 1;
11745 /* FALL THROUGH */
11746 default:
11747 gfc_resolve_blocks (code->block, ns);
11748 break;
11749 }
11750
11751 if (omp_workshare_save != -1)
11752 omp_workshare_flag = omp_workshare_save;
11753 }
11754 start:
11755 t = true;
11756 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
11757 t = gfc_resolve_expr (code->expr1);
11758 forall_flag = forall_save;
11759 gfc_do_concurrent_flag = do_concurrent_save;
11760
11761 if (!gfc_resolve_expr (code->expr2))
11762 t = false;
11763
11764 if (code->op == EXEC_ALLOCATE
11765 && !gfc_resolve_expr (code->expr3))
11766 t = false;
11767
11768 switch (code->op)
11769 {
11770 case EXEC_NOP:
11771 case EXEC_END_BLOCK:
11772 case EXEC_END_NESTED_BLOCK:
11773 case EXEC_CYCLE:
11774 case EXEC_PAUSE:
11775 case EXEC_STOP:
11776 case EXEC_ERROR_STOP:
11777 case EXEC_EXIT:
11778 case EXEC_CONTINUE:
11779 case EXEC_DT_END:
11780 case EXEC_ASSIGN_CALL:
11781 break;
11782
11783 case EXEC_CRITICAL:
11784 resolve_critical (code);
11785 break;
11786
11787 case EXEC_SYNC_ALL:
11788 case EXEC_SYNC_IMAGES:
11789 case EXEC_SYNC_MEMORY:
11790 resolve_sync (code);
11791 break;
11792
11793 case EXEC_LOCK:
11794 case EXEC_UNLOCK:
11795 case EXEC_EVENT_POST:
11796 case EXEC_EVENT_WAIT:
11797 resolve_lock_unlock_event (code);
11798 break;
11799
11800 case EXEC_FAIL_IMAGE:
11801 case EXEC_FORM_TEAM:
11802 case EXEC_CHANGE_TEAM:
11803 case EXEC_END_TEAM:
11804 case EXEC_SYNC_TEAM:
11805 break;
11806
11807 case EXEC_ENTRY:
11808 /* Keep track of which entry we are up to. */
11809 current_entry_id = code->ext.entry->id;
11810 break;
11811
11812 case EXEC_WHERE:
11813 resolve_where (code, NULL);
11814 break;
11815
11816 case EXEC_GOTO:
11817 if (code->expr1 != NULL)
11818 {
11819 if (code->expr1->expr_type != EXPR_VARIABLE
11820 || code->expr1->ts.type != BT_INTEGER
11821 || (code->expr1->ref
11822 && code->expr1->ref->type == REF_ARRAY)
11823 || code->expr1->symtree == NULL
11824 || (code->expr1->symtree->n.sym
11825 && (code->expr1->symtree->n.sym->attr.flavor
11826 == FL_PARAMETER)))
11827 gfc_error ("ASSIGNED GOTO statement at %L requires a "
11828 "scalar INTEGER variable", &code->expr1->where);
11829 else if (code->expr1->symtree->n.sym
11830 && code->expr1->symtree->n.sym->attr.assign != 1)
11831 gfc_error ("Variable %qs has not been assigned a target "
11832 "label at %L", code->expr1->symtree->n.sym->name,
11833 &code->expr1->where);
11834 }
11835 else
11836 resolve_branch (code->label1, code);
11837 break;
11838
11839 case EXEC_RETURN:
11840 if (code->expr1 != NULL
11841 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
11842 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
11843 "INTEGER return specifier", &code->expr1->where);
11844 break;
11845
11846 case EXEC_INIT_ASSIGN:
11847 case EXEC_END_PROCEDURE:
11848 break;
11849
11850 case EXEC_ASSIGN:
11851 if (!t)
11852 break;
11853
11854 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
11855 the LHS. */
11856 if (code->expr1->expr_type == EXPR_FUNCTION
11857 && code->expr1->value.function.isym
11858 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
11859 remove_caf_get_intrinsic (code->expr1);
11860
11861 /* If this is a pointer function in an lvalue variable context,
11862 the new code will have to be resolved afresh. This is also the
11863 case with an error, where the code is transformed into NOP to
11864 prevent ICEs downstream. */
11865 if (resolve_ptr_fcn_assign (&code, ns)
11866 || code->op == EXEC_NOP)
11867 goto start;
11868
11869 if (!gfc_check_vardef_context (code->expr1, false, false, false,
11870 _("assignment")))
11871 break;
11872
11873 if (resolve_ordinary_assign (code, ns))
11874 {
11875 if (code->op == EXEC_COMPCALL)
11876 goto compcall;
11877 else
11878 goto call;
11879 }
11880
11881 /* Check for dependencies in deferred character length array
11882 assignments and generate a temporary, if necessary. */
11883 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
11884 break;
11885
11886 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
11887 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
11888 && code->expr1->ts.u.derived
11889 && code->expr1->ts.u.derived->attr.defined_assign_comp)
11890 generate_component_assignments (&code, ns);
11891
11892 break;
11893
11894 case EXEC_LABEL_ASSIGN:
11895 if (code->label1->defined == ST_LABEL_UNKNOWN)
11896 gfc_error ("Label %d referenced at %L is never defined",
11897 code->label1->value, &code->label1->where);
11898 if (t
11899 && (code->expr1->expr_type != EXPR_VARIABLE
11900 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
11901 || code->expr1->symtree->n.sym->ts.kind
11902 != gfc_default_integer_kind
11903 || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
11904 || code->expr1->symtree->n.sym->as != NULL))
11905 gfc_error ("ASSIGN statement at %L requires a scalar "
11906 "default INTEGER variable", &code->expr1->where);
11907 break;
11908
11909 case EXEC_POINTER_ASSIGN:
11910 {
11911 gfc_expr* e;
11912
11913 if (!t)
11914 break;
11915
11916 /* This is both a variable definition and pointer assignment
11917 context, so check both of them. For rank remapping, a final
11918 array ref may be present on the LHS and fool gfc_expr_attr
11919 used in gfc_check_vardef_context. Remove it. */
11920 e = remove_last_array_ref (code->expr1);
11921 t = gfc_check_vardef_context (e, true, false, false,
11922 _("pointer assignment"));
11923 if (t)
11924 t = gfc_check_vardef_context (e, false, false, false,
11925 _("pointer assignment"));
11926 gfc_free_expr (e);
11927
11928 t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
11929
11930 if (!t)
11931 break;
11932
11933 /* Assigning a class object always is a regular assign. */
11934 if (code->expr2->ts.type == BT_CLASS
11935 && code->expr1->ts.type == BT_CLASS
11936 && !CLASS_DATA (code->expr2)->attr.dimension
11937 && !(gfc_expr_attr (code->expr1).proc_pointer
11938 && code->expr2->expr_type == EXPR_VARIABLE
11939 && code->expr2->symtree->n.sym->attr.flavor
11940 == FL_PROCEDURE))
11941 code->op = EXEC_ASSIGN;
11942 break;
11943 }
11944
11945 case EXEC_ARITHMETIC_IF:
11946 {
11947 gfc_expr *e = code->expr1;
11948
11949 gfc_resolve_expr (e);
11950 if (e->expr_type == EXPR_NULL)
11951 gfc_error ("Invalid NULL at %L", &e->where);
11952
11953 if (t && (e->rank > 0
11954 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
11955 gfc_error ("Arithmetic IF statement at %L requires a scalar "
11956 "REAL or INTEGER expression", &e->where);
11957
11958 resolve_branch (code->label1, code);
11959 resolve_branch (code->label2, code);
11960 resolve_branch (code->label3, code);
11961 }
11962 break;
11963
11964 case EXEC_IF:
11965 if (t && code->expr1 != NULL
11966 && (code->expr1->ts.type != BT_LOGICAL
11967 || code->expr1->rank != 0))
11968 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
11969 &code->expr1->where);
11970 break;
11971
11972 case EXEC_CALL:
11973 call:
11974 resolve_call (code);
11975 break;
11976
11977 case EXEC_COMPCALL:
11978 compcall:
11979 resolve_typebound_subroutine (code);
11980 break;
11981
11982 case EXEC_CALL_PPC:
11983 resolve_ppc_call (code);
11984 break;
11985
11986 case EXEC_SELECT:
11987 /* Select is complicated. Also, a SELECT construct could be
11988 a transformed computed GOTO. */
11989 resolve_select (code, false);
11990 break;
11991
11992 case EXEC_SELECT_TYPE:
11993 resolve_select_type (code, ns);
11994 break;
11995
11996 case EXEC_SELECT_RANK:
11997 resolve_select_rank (code, ns);
11998 break;
11999
12000 case EXEC_BLOCK:
12001 resolve_block_construct (code);
12002 break;
12003
12004 case EXEC_DO:
12005 if (code->ext.iterator != NULL)
12006 {
12007 gfc_iterator *iter = code->ext.iterator;
12008 if (gfc_resolve_iterator (iter, true, false))
12009 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
12010 true);
12011 }
12012 break;
12013
12014 case EXEC_DO_WHILE:
12015 if (code->expr1 == NULL)
12016 gfc_internal_error ("gfc_resolve_code(): No expression on "
12017 "DO WHILE");
12018 if (t
12019 && (code->expr1->rank != 0
12020 || code->expr1->ts.type != BT_LOGICAL))
12021 gfc_error ("Exit condition of DO WHILE loop at %L must be "
12022 "a scalar LOGICAL expression", &code->expr1->where);
12023 break;
12024
12025 case EXEC_ALLOCATE:
12026 if (t)
12027 resolve_allocate_deallocate (code, "ALLOCATE");
12028
12029 break;
12030
12031 case EXEC_DEALLOCATE:
12032 if (t)
12033 resolve_allocate_deallocate (code, "DEALLOCATE");
12034
12035 break;
12036
12037 case EXEC_OPEN:
12038 if (!gfc_resolve_open (code->ext.open, &code->loc))
12039 break;
12040
12041 resolve_branch (code->ext.open->err, code);
12042 break;
12043
12044 case EXEC_CLOSE:
12045 if (!gfc_resolve_close (code->ext.close, &code->loc))
12046 break;
12047
12048 resolve_branch (code->ext.close->err, code);
12049 break;
12050
12051 case EXEC_BACKSPACE:
12052 case EXEC_ENDFILE:
12053 case EXEC_REWIND:
12054 case EXEC_FLUSH:
12055 if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
12056 break;
12057
12058 resolve_branch (code->ext.filepos->err, code);
12059 break;
12060
12061 case EXEC_INQUIRE:
12062 if (!gfc_resolve_inquire (code->ext.inquire))
12063 break;
12064
12065 resolve_branch (code->ext.inquire->err, code);
12066 break;
12067
12068 case EXEC_IOLENGTH:
12069 gcc_assert (code->ext.inquire != NULL);
12070 if (!gfc_resolve_inquire (code->ext.inquire))
12071 break;
12072
12073 resolve_branch (code->ext.inquire->err, code);
12074 break;
12075
12076 case EXEC_WAIT:
12077 if (!gfc_resolve_wait (code->ext.wait))
12078 break;
12079
12080 resolve_branch (code->ext.wait->err, code);
12081 resolve_branch (code->ext.wait->end, code);
12082 resolve_branch (code->ext.wait->eor, code);
12083 break;
12084
12085 case EXEC_READ:
12086 case EXEC_WRITE:
12087 if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
12088 break;
12089
12090 resolve_branch (code->ext.dt->err, code);
12091 resolve_branch (code->ext.dt->end, code);
12092 resolve_branch (code->ext.dt->eor, code);
12093 break;
12094
12095 case EXEC_TRANSFER:
12096 resolve_transfer (code);
12097 break;
12098
12099 case EXEC_DO_CONCURRENT:
12100 case EXEC_FORALL:
12101 resolve_forall_iterators (code->ext.forall_iterator);
12102
12103 if (code->expr1 != NULL
12104 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
12105 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
12106 "expression", &code->expr1->where);
12107 break;
12108
12109 case EXEC_OACC_PARALLEL_LOOP:
12110 case EXEC_OACC_PARALLEL:
12111 case EXEC_OACC_KERNELS_LOOP:
12112 case EXEC_OACC_KERNELS:
12113 case EXEC_OACC_SERIAL_LOOP:
12114 case EXEC_OACC_SERIAL:
12115 case EXEC_OACC_DATA:
12116 case EXEC_OACC_HOST_DATA:
12117 case EXEC_OACC_LOOP:
12118 case EXEC_OACC_UPDATE:
12119 case EXEC_OACC_WAIT:
12120 case EXEC_OACC_CACHE:
12121 case EXEC_OACC_ENTER_DATA:
12122 case EXEC_OACC_EXIT_DATA:
12123 case EXEC_OACC_ATOMIC:
12124 case EXEC_OACC_DECLARE:
12125 gfc_resolve_oacc_directive (code, ns);
12126 break;
12127
12128 case EXEC_OMP_ATOMIC:
12129 case EXEC_OMP_BARRIER:
12130 case EXEC_OMP_CANCEL:
12131 case EXEC_OMP_CANCELLATION_POINT:
12132 case EXEC_OMP_CRITICAL:
12133 case EXEC_OMP_FLUSH:
12134 case EXEC_OMP_DISTRIBUTE:
12135 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
12136 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
12137 case EXEC_OMP_DISTRIBUTE_SIMD:
12138 case EXEC_OMP_DO:
12139 case EXEC_OMP_DO_SIMD:
12140 case EXEC_OMP_MASTER:
12141 case EXEC_OMP_ORDERED:
12142 case EXEC_OMP_SECTIONS:
12143 case EXEC_OMP_SIMD:
12144 case EXEC_OMP_SINGLE:
12145 case EXEC_OMP_TARGET:
12146 case EXEC_OMP_TARGET_DATA:
12147 case EXEC_OMP_TARGET_ENTER_DATA:
12148 case EXEC_OMP_TARGET_EXIT_DATA:
12149 case EXEC_OMP_TARGET_PARALLEL:
12150 case EXEC_OMP_TARGET_PARALLEL_DO:
12151 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
12152 case EXEC_OMP_TARGET_SIMD:
12153 case EXEC_OMP_TARGET_TEAMS:
12154 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
12155 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
12156 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12157 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
12158 case EXEC_OMP_TARGET_UPDATE:
12159 case EXEC_OMP_TASK:
12160 case EXEC_OMP_TASKGROUP:
12161 case EXEC_OMP_TASKLOOP:
12162 case EXEC_OMP_TASKLOOP_SIMD:
12163 case EXEC_OMP_TASKWAIT:
12164 case EXEC_OMP_TASKYIELD:
12165 case EXEC_OMP_TEAMS:
12166 case EXEC_OMP_TEAMS_DISTRIBUTE:
12167 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
12168 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12169 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
12170 case EXEC_OMP_WORKSHARE:
12171 gfc_resolve_omp_directive (code, ns);
12172 break;
12173
12174 case EXEC_OMP_PARALLEL:
12175 case EXEC_OMP_PARALLEL_DO:
12176 case EXEC_OMP_PARALLEL_DO_SIMD:
12177 case EXEC_OMP_PARALLEL_SECTIONS:
12178 case EXEC_OMP_PARALLEL_WORKSHARE:
12179 omp_workshare_save = omp_workshare_flag;
12180 omp_workshare_flag = 0;
12181 gfc_resolve_omp_directive (code, ns);
12182 omp_workshare_flag = omp_workshare_save;
12183 break;
12184
12185 default:
12186 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
12187 }
12188 }
12189
12190 cs_base = frame.prev;
12191 }
12192
12193
12194 /* Resolve initial values and make sure they are compatible with
12195 the variable. */
12196
12197 static void
12198 resolve_values (gfc_symbol *sym)
12199 {
12200 bool t;
12201
12202 if (sym->value == NULL)
12203 return;
12204
12205 if (sym->value->expr_type == EXPR_STRUCTURE)
12206 t= resolve_structure_cons (sym->value, 1);
12207 else
12208 t = gfc_resolve_expr (sym->value);
12209
12210 if (!t)
12211 return;
12212
12213 gfc_check_assign_symbol (sym, NULL, sym->value);
12214 }
12215
12216
12217 /* Verify any BIND(C) derived types in the namespace so we can report errors
12218 for them once, rather than for each variable declared of that type. */
12219
12220 static void
12221 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
12222 {
12223 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
12224 && derived_sym->attr.is_bind_c == 1)
12225 verify_bind_c_derived_type (derived_sym);
12226
12227 return;
12228 }
12229
12230
12231 /* Check the interfaces of DTIO procedures associated with derived
12232 type 'sym'. These procedures can either have typebound bindings or
12233 can appear in DTIO generic interfaces. */
12234
12235 static void
12236 gfc_verify_DTIO_procedures (gfc_symbol *sym)
12237 {
12238 if (!sym || sym->attr.flavor != FL_DERIVED)
12239 return;
12240
12241 gfc_check_dtio_interfaces (sym);
12242
12243 return;
12244 }
12245
12246 /* Verify that any binding labels used in a given namespace do not collide
12247 with the names or binding labels of any global symbols. Multiple INTERFACE
12248 for the same procedure are permitted. */
12249
12250 static void
12251 gfc_verify_binding_labels (gfc_symbol *sym)
12252 {
12253 gfc_gsymbol *gsym;
12254 const char *module;
12255
12256 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
12257 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
12258 return;
12259
12260 gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
12261
12262 if (sym->module)
12263 module = sym->module;
12264 else if (sym->ns && sym->ns->proc_name
12265 && sym->ns->proc_name->attr.flavor == FL_MODULE)
12266 module = sym->ns->proc_name->name;
12267 else if (sym->ns && sym->ns->parent
12268 && sym->ns && sym->ns->parent->proc_name
12269 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
12270 module = sym->ns->parent->proc_name->name;
12271 else
12272 module = NULL;
12273
12274 if (!gsym
12275 || (!gsym->defined
12276 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
12277 {
12278 if (!gsym)
12279 gsym = gfc_get_gsymbol (sym->binding_label, true);
12280 gsym->where = sym->declared_at;
12281 gsym->sym_name = sym->name;
12282 gsym->binding_label = sym->binding_label;
12283 gsym->ns = sym->ns;
12284 gsym->mod_name = module;
12285 if (sym->attr.function)
12286 gsym->type = GSYM_FUNCTION;
12287 else if (sym->attr.subroutine)
12288 gsym->type = GSYM_SUBROUTINE;
12289 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
12290 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
12291 return;
12292 }
12293
12294 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
12295 {
12296 gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
12297 "identifier as entity at %L", sym->name,
12298 sym->binding_label, &sym->declared_at, &gsym->where);
12299 /* Clear the binding label to prevent checking multiple times. */
12300 sym->binding_label = NULL;
12301 return;
12302 }
12303
12304 if (sym->attr.flavor == FL_VARIABLE && module
12305 && (strcmp (module, gsym->mod_name) != 0
12306 || strcmp (sym->name, gsym->sym_name) != 0))
12307 {
12308 /* This can only happen if the variable is defined in a module - if it
12309 isn't the same module, reject it. */
12310 gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
12311 "uses the same global identifier as entity at %L from module %qs",
12312 sym->name, module, sym->binding_label,
12313 &sym->declared_at, &gsym->where, gsym->mod_name);
12314 sym->binding_label = NULL;
12315 return;
12316 }
12317
12318 if ((sym->attr.function || sym->attr.subroutine)
12319 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
12320 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
12321 && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
12322 && (module != gsym->mod_name
12323 || strcmp (gsym->sym_name, sym->name) != 0
12324 || (module && strcmp (module, gsym->mod_name) != 0)))
12325 {
12326 /* Print an error if the procedure is defined multiple times; we have to
12327 exclude references to the same procedure via module association or
12328 multiple checks for the same procedure. */
12329 gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
12330 "global identifier as entity at %L", sym->name,
12331 sym->binding_label, &sym->declared_at, &gsym->where);
12332 sym->binding_label = NULL;
12333 }
12334 }
12335
12336
12337 /* Resolve an index expression. */
12338
12339 static bool
12340 resolve_index_expr (gfc_expr *e)
12341 {
12342 if (!gfc_resolve_expr (e))
12343 return false;
12344
12345 if (!gfc_simplify_expr (e, 0))
12346 return false;
12347
12348 if (!gfc_specification_expr (e))
12349 return false;
12350
12351 return true;
12352 }
12353
12354
12355 /* Resolve a charlen structure. */
12356
12357 static bool
12358 resolve_charlen (gfc_charlen *cl)
12359 {
12360 int k;
12361 bool saved_specification_expr;
12362
12363 if (cl->resolved)
12364 return true;
12365
12366 cl->resolved = 1;
12367 saved_specification_expr = specification_expr;
12368 specification_expr = true;
12369
12370 if (cl->length_from_typespec)
12371 {
12372 if (!gfc_resolve_expr (cl->length))
12373 {
12374 specification_expr = saved_specification_expr;
12375 return false;
12376 }
12377
12378 if (!gfc_simplify_expr (cl->length, 0))
12379 {
12380 specification_expr = saved_specification_expr;
12381 return false;
12382 }
12383
12384 /* cl->length has been resolved. It should have an integer type. */
12385 if (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0)
12386 {
12387 gfc_error ("Scalar INTEGER expression expected at %L",
12388 &cl->length->where);
12389 return false;
12390 }
12391 }
12392 else
12393 {
12394 if (!resolve_index_expr (cl->length))
12395 {
12396 specification_expr = saved_specification_expr;
12397 return false;
12398 }
12399 }
12400
12401 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
12402 a negative value, the length of character entities declared is zero. */
12403 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
12404 && mpz_sgn (cl->length->value.integer) < 0)
12405 gfc_replace_expr (cl->length,
12406 gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
12407
12408 /* Check that the character length is not too large. */
12409 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
12410 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
12411 && cl->length->ts.type == BT_INTEGER
12412 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
12413 {
12414 gfc_error ("String length at %L is too large", &cl->length->where);
12415 specification_expr = saved_specification_expr;
12416 return false;
12417 }
12418
12419 specification_expr = saved_specification_expr;
12420 return true;
12421 }
12422
12423
12424 /* Test for non-constant shape arrays. */
12425
12426 static bool
12427 is_non_constant_shape_array (gfc_symbol *sym)
12428 {
12429 gfc_expr *e;
12430 int i;
12431 bool not_constant;
12432
12433 not_constant = false;
12434 if (sym->as != NULL)
12435 {
12436 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
12437 has not been simplified; parameter array references. Do the
12438 simplification now. */
12439 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
12440 {
12441 if (i == GFC_MAX_DIMENSIONS)
12442 break;
12443
12444 e = sym->as->lower[i];
12445 if (e && (!resolve_index_expr(e)
12446 || !gfc_is_constant_expr (e)))
12447 not_constant = true;
12448 e = sym->as->upper[i];
12449 if (e && (!resolve_index_expr(e)
12450 || !gfc_is_constant_expr (e)))
12451 not_constant = true;
12452 }
12453 }
12454 return not_constant;
12455 }
12456
12457 /* Given a symbol and an initialization expression, add code to initialize
12458 the symbol to the function entry. */
12459 static void
12460 build_init_assign (gfc_symbol *sym, gfc_expr *init)
12461 {
12462 gfc_expr *lval;
12463 gfc_code *init_st;
12464 gfc_namespace *ns = sym->ns;
12465
12466 /* Search for the function namespace if this is a contained
12467 function without an explicit result. */
12468 if (sym->attr.function && sym == sym->result
12469 && sym->name != sym->ns->proc_name->name)
12470 {
12471 ns = ns->contained;
12472 for (;ns; ns = ns->sibling)
12473 if (strcmp (ns->proc_name->name, sym->name) == 0)
12474 break;
12475 }
12476
12477 if (ns == NULL)
12478 {
12479 gfc_free_expr (init);
12480 return;
12481 }
12482
12483 /* Build an l-value expression for the result. */
12484 lval = gfc_lval_expr_from_sym (sym);
12485
12486 /* Add the code at scope entry. */
12487 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
12488 init_st->next = ns->code;
12489 ns->code = init_st;
12490
12491 /* Assign the default initializer to the l-value. */
12492 init_st->loc = sym->declared_at;
12493 init_st->expr1 = lval;
12494 init_st->expr2 = init;
12495 }
12496
12497
12498 /* Whether or not we can generate a default initializer for a symbol. */
12499
12500 static bool
12501 can_generate_init (gfc_symbol *sym)
12502 {
12503 symbol_attribute *a;
12504 if (!sym)
12505 return false;
12506 a = &sym->attr;
12507
12508 /* These symbols should never have a default initialization. */
12509 return !(
12510 a->allocatable
12511 || a->external
12512 || a->pointer
12513 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
12514 && (CLASS_DATA (sym)->attr.class_pointer
12515 || CLASS_DATA (sym)->attr.proc_pointer))
12516 || a->in_equivalence
12517 || a->in_common
12518 || a->data
12519 || sym->module
12520 || a->cray_pointee
12521 || a->cray_pointer
12522 || sym->assoc
12523 || (!a->referenced && !a->result)
12524 || (a->dummy && a->intent != INTENT_OUT)
12525 || (a->function && sym != sym->result)
12526 );
12527 }
12528
12529
12530 /* Assign the default initializer to a derived type variable or result. */
12531
12532 static void
12533 apply_default_init (gfc_symbol *sym)
12534 {
12535 gfc_expr *init = NULL;
12536
12537 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
12538 return;
12539
12540 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
12541 init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
12542
12543 if (init == NULL && sym->ts.type != BT_CLASS)
12544 return;
12545
12546 build_init_assign (sym, init);
12547 sym->attr.referenced = 1;
12548 }
12549
12550
12551 /* Build an initializer for a local. Returns null if the symbol should not have
12552 a default initialization. */
12553
12554 static gfc_expr *
12555 build_default_init_expr (gfc_symbol *sym)
12556 {
12557 /* These symbols should never have a default initialization. */
12558 if (sym->attr.allocatable
12559 || sym->attr.external
12560 || sym->attr.dummy
12561 || sym->attr.pointer
12562 || sym->attr.in_equivalence
12563 || sym->attr.in_common
12564 || sym->attr.data
12565 || sym->module
12566 || sym->attr.cray_pointee
12567 || sym->attr.cray_pointer
12568 || sym->assoc)
12569 return NULL;
12570
12571 /* Get the appropriate init expression. */
12572 return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
12573 }
12574
12575 /* Add an initialization expression to a local variable. */
12576 static void
12577 apply_default_init_local (gfc_symbol *sym)
12578 {
12579 gfc_expr *init = NULL;
12580
12581 /* The symbol should be a variable or a function return value. */
12582 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
12583 || (sym->attr.function && sym->result != sym))
12584 return;
12585
12586 /* Try to build the initializer expression. If we can't initialize
12587 this symbol, then init will be NULL. */
12588 init = build_default_init_expr (sym);
12589 if (init == NULL)
12590 return;
12591
12592 /* For saved variables, we don't want to add an initializer at function
12593 entry, so we just add a static initializer. Note that automatic variables
12594 are stack allocated even with -fno-automatic; we have also to exclude
12595 result variable, which are also nonstatic. */
12596 if (!sym->attr.automatic
12597 && (sym->attr.save || sym->ns->save_all
12598 || (flag_max_stack_var_size == 0 && !sym->attr.result
12599 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
12600 && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
12601 {
12602 /* Don't clobber an existing initializer! */
12603 gcc_assert (sym->value == NULL);
12604 sym->value = init;
12605 return;
12606 }
12607
12608 build_init_assign (sym, init);
12609 }
12610
12611
12612 /* Resolution of common features of flavors variable and procedure. */
12613
12614 static bool
12615 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
12616 {
12617 gfc_array_spec *as;
12618
12619 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
12620 as = CLASS_DATA (sym)->as;
12621 else
12622 as = sym->as;
12623
12624 /* Constraints on deferred shape variable. */
12625 if (as == NULL || as->type != AS_DEFERRED)
12626 {
12627 bool pointer, allocatable, dimension;
12628
12629 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
12630 {
12631 pointer = CLASS_DATA (sym)->attr.class_pointer;
12632 allocatable = CLASS_DATA (sym)->attr.allocatable;
12633 dimension = CLASS_DATA (sym)->attr.dimension;
12634 }
12635 else
12636 {
12637 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
12638 allocatable = sym->attr.allocatable;
12639 dimension = sym->attr.dimension;
12640 }
12641
12642 if (allocatable)
12643 {
12644 if (dimension && as->type != AS_ASSUMED_RANK)
12645 {
12646 gfc_error ("Allocatable array %qs at %L must have a deferred "
12647 "shape or assumed rank", sym->name, &sym->declared_at);
12648 return false;
12649 }
12650 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
12651 "%qs at %L may not be ALLOCATABLE",
12652 sym->name, &sym->declared_at))
12653 return false;
12654 }
12655
12656 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
12657 {
12658 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
12659 "assumed rank", sym->name, &sym->declared_at);
12660 sym->error = 1;
12661 return false;
12662 }
12663 }
12664 else
12665 {
12666 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
12667 && sym->ts.type != BT_CLASS && !sym->assoc)
12668 {
12669 gfc_error ("Array %qs at %L cannot have a deferred shape",
12670 sym->name, &sym->declared_at);
12671 return false;
12672 }
12673 }
12674
12675 /* Constraints on polymorphic variables. */
12676 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
12677 {
12678 /* F03:C502. */
12679 if (sym->attr.class_ok
12680 && !sym->attr.select_type_temporary
12681 && !UNLIMITED_POLY (sym)
12682 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
12683 {
12684 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
12685 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
12686 &sym->declared_at);
12687 return false;
12688 }
12689
12690 /* F03:C509. */
12691 /* Assume that use associated symbols were checked in the module ns.
12692 Class-variables that are associate-names are also something special
12693 and excepted from the test. */
12694 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
12695 {
12696 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
12697 "or pointer", sym->name, &sym->declared_at);
12698 return false;
12699 }
12700 }
12701
12702 return true;
12703 }
12704
12705
12706 /* Additional checks for symbols with flavor variable and derived
12707 type. To be called from resolve_fl_variable. */
12708
12709 static bool
12710 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
12711 {
12712 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
12713
12714 /* Check to see if a derived type is blocked from being host
12715 associated by the presence of another class I symbol in the same
12716 namespace. 14.6.1.3 of the standard and the discussion on
12717 comp.lang.fortran. */
12718 if (sym->ns != sym->ts.u.derived->ns
12719 && !sym->ts.u.derived->attr.use_assoc
12720 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
12721 {
12722 gfc_symbol *s;
12723 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
12724 if (s && s->attr.generic)
12725 s = gfc_find_dt_in_generic (s);
12726 if (s && !gfc_fl_struct (s->attr.flavor))
12727 {
12728 gfc_error ("The type %qs cannot be host associated at %L "
12729 "because it is blocked by an incompatible object "
12730 "of the same name declared at %L",
12731 sym->ts.u.derived->name, &sym->declared_at,
12732 &s->declared_at);
12733 return false;
12734 }
12735 }
12736
12737 /* 4th constraint in section 11.3: "If an object of a type for which
12738 component-initialization is specified (R429) appears in the
12739 specification-part of a module and does not have the ALLOCATABLE
12740 or POINTER attribute, the object shall have the SAVE attribute."
12741
12742 The check for initializers is performed with
12743 gfc_has_default_initializer because gfc_default_initializer generates
12744 a hidden default for allocatable components. */
12745 if (!(sym->value || no_init_flag) && sym->ns->proc_name
12746 && sym->ns->proc_name->attr.flavor == FL_MODULE
12747 && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
12748 && !sym->attr.pointer && !sym->attr.allocatable
12749 && gfc_has_default_initializer (sym->ts.u.derived)
12750 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
12751 "%qs at %L, needed due to the default "
12752 "initialization", sym->name, &sym->declared_at))
12753 return false;
12754
12755 /* Assign default initializer. */
12756 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
12757 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
12758 sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
12759
12760 return true;
12761 }
12762
12763
12764 /* F2008, C402 (R401): A colon shall not be used as a type-param-value
12765 except in the declaration of an entity or component that has the POINTER
12766 or ALLOCATABLE attribute. */
12767
12768 static bool
12769 deferred_requirements (gfc_symbol *sym)
12770 {
12771 if (sym->ts.deferred
12772 && !(sym->attr.pointer
12773 || sym->attr.allocatable
12774 || sym->attr.associate_var
12775 || sym->attr.omp_udr_artificial_var))
12776 {
12777 /* If a function has a result variable, only check the variable. */
12778 if (sym->result && sym->name != sym->result->name)
12779 return true;
12780
12781 gfc_error ("Entity %qs at %L has a deferred type parameter and "
12782 "requires either the POINTER or ALLOCATABLE attribute",
12783 sym->name, &sym->declared_at);
12784 return false;
12785 }
12786 return true;
12787 }
12788
12789
12790 /* Resolve symbols with flavor variable. */
12791
12792 static bool
12793 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
12794 {
12795 const char *auto_save_msg = "Automatic object %qs at %L cannot have the "
12796 "SAVE attribute";
12797
12798 if (!resolve_fl_var_and_proc (sym, mp_flag))
12799 return false;
12800
12801 /* Set this flag to check that variables are parameters of all entries.
12802 This check is effected by the call to gfc_resolve_expr through
12803 is_non_constant_shape_array. */
12804 bool saved_specification_expr = specification_expr;
12805 specification_expr = true;
12806
12807 if (sym->ns->proc_name
12808 && (sym->ns->proc_name->attr.flavor == FL_MODULE
12809 || sym->ns->proc_name->attr.is_main_program)
12810 && !sym->attr.use_assoc
12811 && !sym->attr.allocatable
12812 && !sym->attr.pointer
12813 && is_non_constant_shape_array (sym))
12814 {
12815 /* F08:C541. The shape of an array defined in a main program or module
12816 * needs to be constant. */
12817 gfc_error ("The module or main program array %qs at %L must "
12818 "have constant shape", sym->name, &sym->declared_at);
12819 specification_expr = saved_specification_expr;
12820 return false;
12821 }
12822
12823 /* Constraints on deferred type parameter. */
12824 if (!deferred_requirements (sym))
12825 return false;
12826
12827 if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
12828 {
12829 /* Make sure that character string variables with assumed length are
12830 dummy arguments. */
12831 gfc_expr *e = NULL;
12832
12833 if (sym->ts.u.cl)
12834 e = sym->ts.u.cl->length;
12835 else
12836 return false;
12837
12838 if (e == NULL && !sym->attr.dummy && !sym->attr.result
12839 && !sym->ts.deferred && !sym->attr.select_type_temporary
12840 && !sym->attr.omp_udr_artificial_var)
12841 {
12842 gfc_error ("Entity with assumed character length at %L must be a "
12843 "dummy argument or a PARAMETER", &sym->declared_at);
12844 specification_expr = saved_specification_expr;
12845 return false;
12846 }
12847
12848 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
12849 {
12850 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
12851 specification_expr = saved_specification_expr;
12852 return false;
12853 }
12854
12855 if (!gfc_is_constant_expr (e)
12856 && !(e->expr_type == EXPR_VARIABLE
12857 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
12858 {
12859 if (!sym->attr.use_assoc && sym->ns->proc_name
12860 && (sym->ns->proc_name->attr.flavor == FL_MODULE
12861 || sym->ns->proc_name->attr.is_main_program))
12862 {
12863 gfc_error ("%qs at %L must have constant character length "
12864 "in this context", sym->name, &sym->declared_at);
12865 specification_expr = saved_specification_expr;
12866 return false;
12867 }
12868 if (sym->attr.in_common)
12869 {
12870 gfc_error ("COMMON variable %qs at %L must have constant "
12871 "character length", sym->name, &sym->declared_at);
12872 specification_expr = saved_specification_expr;
12873 return false;
12874 }
12875 }
12876 }
12877
12878 if (sym->value == NULL && sym->attr.referenced)
12879 apply_default_init_local (sym); /* Try to apply a default initialization. */
12880
12881 /* Determine if the symbol may not have an initializer. */
12882 int no_init_flag = 0, automatic_flag = 0;
12883 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
12884 || sym->attr.intrinsic || sym->attr.result)
12885 no_init_flag = 1;
12886 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
12887 && is_non_constant_shape_array (sym))
12888 {
12889 no_init_flag = automatic_flag = 1;
12890
12891 /* Also, they must not have the SAVE attribute.
12892 SAVE_IMPLICIT is checked below. */
12893 if (sym->as && sym->attr.codimension)
12894 {
12895 int corank = sym->as->corank;
12896 sym->as->corank = 0;
12897 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
12898 sym->as->corank = corank;
12899 }
12900 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
12901 {
12902 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
12903 specification_expr = saved_specification_expr;
12904 return false;
12905 }
12906 }
12907
12908 /* Ensure that any initializer is simplified. */
12909 if (sym->value)
12910 gfc_simplify_expr (sym->value, 1);
12911
12912 /* Reject illegal initializers. */
12913 if (!sym->mark && sym->value)
12914 {
12915 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
12916 && CLASS_DATA (sym)->attr.allocatable))
12917 gfc_error ("Allocatable %qs at %L cannot have an initializer",
12918 sym->name, &sym->declared_at);
12919 else if (sym->attr.external)
12920 gfc_error ("External %qs at %L cannot have an initializer",
12921 sym->name, &sym->declared_at);
12922 else if (sym->attr.dummy
12923 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
12924 gfc_error ("Dummy %qs at %L cannot have an initializer",
12925 sym->name, &sym->declared_at);
12926 else if (sym->attr.intrinsic)
12927 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
12928 sym->name, &sym->declared_at);
12929 else if (sym->attr.result)
12930 gfc_error ("Function result %qs at %L cannot have an initializer",
12931 sym->name, &sym->declared_at);
12932 else if (automatic_flag)
12933 gfc_error ("Automatic array %qs at %L cannot have an initializer",
12934 sym->name, &sym->declared_at);
12935 else
12936 goto no_init_error;
12937 specification_expr = saved_specification_expr;
12938 return false;
12939 }
12940
12941 no_init_error:
12942 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
12943 {
12944 bool res = resolve_fl_variable_derived (sym, no_init_flag);
12945 specification_expr = saved_specification_expr;
12946 return res;
12947 }
12948
12949 specification_expr = saved_specification_expr;
12950 return true;
12951 }
12952
12953
12954 /* Compare the dummy characteristics of a module procedure interface
12955 declaration with the corresponding declaration in a submodule. */
12956 static gfc_formal_arglist *new_formal;
12957 static char errmsg[200];
12958
12959 static void
12960 compare_fsyms (gfc_symbol *sym)
12961 {
12962 gfc_symbol *fsym;
12963
12964 if (sym == NULL || new_formal == NULL)
12965 return;
12966
12967 fsym = new_formal->sym;
12968
12969 if (sym == fsym)
12970 return;
12971
12972 if (strcmp (sym->name, fsym->name) == 0)
12973 {
12974 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
12975 gfc_error ("%s at %L", errmsg, &fsym->declared_at);
12976 }
12977 }
12978
12979
12980 /* Resolve a procedure. */
12981
12982 static bool
12983 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
12984 {
12985 gfc_formal_arglist *arg;
12986
12987 if (sym->attr.function
12988 && !resolve_fl_var_and_proc (sym, mp_flag))
12989 return false;
12990
12991 /* Constraints on deferred type parameter. */
12992 if (!deferred_requirements (sym))
12993 return false;
12994
12995 if (sym->ts.type == BT_CHARACTER)
12996 {
12997 gfc_charlen *cl = sym->ts.u.cl;
12998
12999 if (cl && cl->length && gfc_is_constant_expr (cl->length)
13000 && !resolve_charlen (cl))
13001 return false;
13002
13003 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
13004 && sym->attr.proc == PROC_ST_FUNCTION)
13005 {
13006 gfc_error ("Character-valued statement function %qs at %L must "
13007 "have constant length", sym->name, &sym->declared_at);
13008 return false;
13009 }
13010 }
13011
13012 /* Ensure that derived type for are not of a private type. Internal
13013 module procedures are excluded by 2.2.3.3 - i.e., they are not
13014 externally accessible and can access all the objects accessible in
13015 the host. */
13016 if (!(sym->ns->parent && sym->ns->parent->proc_name
13017 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
13018 && gfc_check_symbol_access (sym))
13019 {
13020 gfc_interface *iface;
13021
13022 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
13023 {
13024 if (arg->sym
13025 && arg->sym->ts.type == BT_DERIVED
13026 && arg->sym->ts.u.derived
13027 && !arg->sym->ts.u.derived->attr.use_assoc
13028 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
13029 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
13030 "and cannot be a dummy argument"
13031 " of %qs, which is PUBLIC at %L",
13032 arg->sym->name, sym->name,
13033 &sym->declared_at))
13034 {
13035 /* Stop this message from recurring. */
13036 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
13037 return false;
13038 }
13039 }
13040
13041 /* PUBLIC interfaces may expose PRIVATE procedures that take types
13042 PRIVATE to the containing module. */
13043 for (iface = sym->generic; iface; iface = iface->next)
13044 {
13045 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
13046 {
13047 if (arg->sym
13048 && arg->sym->ts.type == BT_DERIVED
13049 && !arg->sym->ts.u.derived->attr.use_assoc
13050 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
13051 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
13052 "PUBLIC interface %qs at %L "
13053 "takes dummy arguments of %qs which "
13054 "is PRIVATE", iface->sym->name,
13055 sym->name, &iface->sym->declared_at,
13056 gfc_typename(&arg->sym->ts)))
13057 {
13058 /* Stop this message from recurring. */
13059 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
13060 return false;
13061 }
13062 }
13063 }
13064 }
13065
13066 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
13067 && !sym->attr.proc_pointer)
13068 {
13069 gfc_error ("Function %qs at %L cannot have an initializer",
13070 sym->name, &sym->declared_at);
13071
13072 /* Make sure no second error is issued for this. */
13073 sym->value->error = 1;
13074 return false;
13075 }
13076
13077 /* An external symbol may not have an initializer because it is taken to be
13078 a procedure. Exception: Procedure Pointers. */
13079 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
13080 {
13081 gfc_error ("External object %qs at %L may not have an initializer",
13082 sym->name, &sym->declared_at);
13083 return false;
13084 }
13085
13086 /* An elemental function is required to return a scalar 12.7.1 */
13087 if (sym->attr.elemental && sym->attr.function
13088 && (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)))
13089 {
13090 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
13091 "result", sym->name, &sym->declared_at);
13092 /* Reset so that the error only occurs once. */
13093 sym->attr.elemental = 0;
13094 return false;
13095 }
13096
13097 if (sym->attr.proc == PROC_ST_FUNCTION
13098 && (sym->attr.allocatable || sym->attr.pointer))
13099 {
13100 gfc_error ("Statement function %qs at %L may not have pointer or "
13101 "allocatable attribute", sym->name, &sym->declared_at);
13102 return false;
13103 }
13104
13105 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
13106 char-len-param shall not be array-valued, pointer-valued, recursive
13107 or pure. ....snip... A character value of * may only be used in the
13108 following ways: (i) Dummy arg of procedure - dummy associates with
13109 actual length; (ii) To declare a named constant; or (iii) External
13110 function - but length must be declared in calling scoping unit. */
13111 if (sym->attr.function
13112 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
13113 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
13114 {
13115 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
13116 || (sym->attr.recursive) || (sym->attr.pure))
13117 {
13118 if (sym->as && sym->as->rank)
13119 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13120 "array-valued", sym->name, &sym->declared_at);
13121
13122 if (sym->attr.pointer)
13123 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13124 "pointer-valued", sym->name, &sym->declared_at);
13125
13126 if (sym->attr.pure)
13127 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13128 "pure", sym->name, &sym->declared_at);
13129
13130 if (sym->attr.recursive)
13131 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13132 "recursive", sym->name, &sym->declared_at);
13133
13134 return false;
13135 }
13136
13137 /* Appendix B.2 of the standard. Contained functions give an
13138 error anyway. Deferred character length is an F2003 feature.
13139 Don't warn on intrinsic conversion functions, which start
13140 with two underscores. */
13141 if (!sym->attr.contained && !sym->ts.deferred
13142 && (sym->name[0] != '_' || sym->name[1] != '_'))
13143 gfc_notify_std (GFC_STD_F95_OBS,
13144 "CHARACTER(*) function %qs at %L",
13145 sym->name, &sym->declared_at);
13146 }
13147
13148 /* F2008, C1218. */
13149 if (sym->attr.elemental)
13150 {
13151 if (sym->attr.proc_pointer)
13152 {
13153 const char* name = (sym->attr.result ? sym->ns->proc_name->name
13154 : sym->name);
13155 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
13156 name, &sym->declared_at);
13157 return false;
13158 }
13159 if (sym->attr.dummy)
13160 {
13161 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
13162 sym->name, &sym->declared_at);
13163 return false;
13164 }
13165 }
13166
13167 /* F2018, C15100: "The result of an elemental function shall be scalar,
13168 and shall not have the POINTER or ALLOCATABLE attribute." The scalar
13169 pointer is tested and caught elsewhere. */
13170 if (sym->attr.elemental && sym->result
13171 && (sym->result->attr.allocatable || sym->result->attr.pointer))
13172 {
13173 gfc_error ("Function result variable %qs at %L of elemental "
13174 "function %qs shall not have an ALLOCATABLE or POINTER "
13175 "attribute", sym->result->name,
13176 &sym->result->declared_at, sym->name);
13177 return false;
13178 }
13179
13180 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
13181 {
13182 gfc_formal_arglist *curr_arg;
13183 int has_non_interop_arg = 0;
13184
13185 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13186 sym->common_block))
13187 {
13188 /* Clear these to prevent looking at them again if there was an
13189 error. */
13190 sym->attr.is_bind_c = 0;
13191 sym->attr.is_c_interop = 0;
13192 sym->ts.is_c_interop = 0;
13193 }
13194 else
13195 {
13196 /* So far, no errors have been found. */
13197 sym->attr.is_c_interop = 1;
13198 sym->ts.is_c_interop = 1;
13199 }
13200
13201 curr_arg = gfc_sym_get_dummy_args (sym);
13202 while (curr_arg != NULL)
13203 {
13204 /* Skip implicitly typed dummy args here. */
13205 if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
13206 if (!gfc_verify_c_interop_param (curr_arg->sym))
13207 /* If something is found to fail, record the fact so we
13208 can mark the symbol for the procedure as not being
13209 BIND(C) to try and prevent multiple errors being
13210 reported. */
13211 has_non_interop_arg = 1;
13212
13213 curr_arg = curr_arg->next;
13214 }
13215
13216 /* See if any of the arguments were not interoperable and if so, clear
13217 the procedure symbol to prevent duplicate error messages. */
13218 if (has_non_interop_arg != 0)
13219 {
13220 sym->attr.is_c_interop = 0;
13221 sym->ts.is_c_interop = 0;
13222 sym->attr.is_bind_c = 0;
13223 }
13224 }
13225
13226 if (!sym->attr.proc_pointer)
13227 {
13228 if (sym->attr.save == SAVE_EXPLICIT)
13229 {
13230 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
13231 "in %qs at %L", sym->name, &sym->declared_at);
13232 return false;
13233 }
13234 if (sym->attr.intent)
13235 {
13236 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
13237 "in %qs at %L", sym->name, &sym->declared_at);
13238 return false;
13239 }
13240 if (sym->attr.subroutine && sym->attr.result)
13241 {
13242 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
13243 "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
13244 return false;
13245 }
13246 if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
13247 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
13248 || sym->attr.contained))
13249 {
13250 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
13251 "in %qs at %L", sym->name, &sym->declared_at);
13252 return false;
13253 }
13254 if (strcmp ("ppr@", sym->name) == 0)
13255 {
13256 gfc_error ("Procedure pointer result %qs at %L "
13257 "is missing the pointer attribute",
13258 sym->ns->proc_name->name, &sym->declared_at);
13259 return false;
13260 }
13261 }
13262
13263 /* Assume that a procedure whose body is not known has references
13264 to external arrays. */
13265 if (sym->attr.if_source != IFSRC_DECL)
13266 sym->attr.array_outer_dependency = 1;
13267
13268 /* Compare the characteristics of a module procedure with the
13269 interface declaration. Ideally this would be done with
13270 gfc_compare_interfaces but, at present, the formal interface
13271 cannot be copied to the ts.interface. */
13272 if (sym->attr.module_procedure
13273 && sym->attr.if_source == IFSRC_DECL)
13274 {
13275 gfc_symbol *iface;
13276 char name[2*GFC_MAX_SYMBOL_LEN + 1];
13277 char *module_name;
13278 char *submodule_name;
13279 strcpy (name, sym->ns->proc_name->name);
13280 module_name = strtok (name, ".");
13281 submodule_name = strtok (NULL, ".");
13282
13283 iface = sym->tlink;
13284 sym->tlink = NULL;
13285
13286 /* Make sure that the result uses the correct charlen for deferred
13287 length results. */
13288 if (iface && sym->result
13289 && iface->ts.type == BT_CHARACTER
13290 && iface->ts.deferred)
13291 sym->result->ts.u.cl = iface->ts.u.cl;
13292
13293 if (iface == NULL)
13294 goto check_formal;
13295
13296 /* Check the procedure characteristics. */
13297 if (sym->attr.elemental != iface->attr.elemental)
13298 {
13299 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
13300 "PROCEDURE at %L and its interface in %s",
13301 &sym->declared_at, module_name);
13302 return false;
13303 }
13304
13305 if (sym->attr.pure != iface->attr.pure)
13306 {
13307 gfc_error ("Mismatch in PURE attribute between MODULE "
13308 "PROCEDURE at %L and its interface in %s",
13309 &sym->declared_at, module_name);
13310 return false;
13311 }
13312
13313 if (sym->attr.recursive != iface->attr.recursive)
13314 {
13315 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
13316 "PROCEDURE at %L and its interface in %s",
13317 &sym->declared_at, module_name);
13318 return false;
13319 }
13320
13321 /* Check the result characteristics. */
13322 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
13323 {
13324 gfc_error ("%s between the MODULE PROCEDURE declaration "
13325 "in MODULE %qs and the declaration at %L in "
13326 "(SUB)MODULE %qs",
13327 errmsg, module_name, &sym->declared_at,
13328 submodule_name ? submodule_name : module_name);
13329 return false;
13330 }
13331
13332 check_formal:
13333 /* Check the characteristics of the formal arguments. */
13334 if (sym->formal && sym->formal_ns)
13335 {
13336 for (arg = sym->formal; arg && arg->sym; arg = arg->next)
13337 {
13338 new_formal = arg;
13339 gfc_traverse_ns (sym->formal_ns, compare_fsyms);
13340 }
13341 }
13342 }
13343 return true;
13344 }
13345
13346
13347 /* Resolve a list of finalizer procedures. That is, after they have hopefully
13348 been defined and we now know their defined arguments, check that they fulfill
13349 the requirements of the standard for procedures used as finalizers. */
13350
13351 static bool
13352 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
13353 {
13354 gfc_finalizer* list;
13355 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
13356 bool result = true;
13357 bool seen_scalar = false;
13358 gfc_symbol *vtab;
13359 gfc_component *c;
13360 gfc_symbol *parent = gfc_get_derived_super_type (derived);
13361
13362 if (parent)
13363 gfc_resolve_finalizers (parent, finalizable);
13364
13365 /* Ensure that derived-type components have a their finalizers resolved. */
13366 bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
13367 for (c = derived->components; c; c = c->next)
13368 if (c->ts.type == BT_DERIVED
13369 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
13370 {
13371 bool has_final2 = false;
13372 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
13373 return false; /* Error. */
13374 has_final = has_final || has_final2;
13375 }
13376 /* Return early if not finalizable. */
13377 if (!has_final)
13378 {
13379 if (finalizable)
13380 *finalizable = false;
13381 return true;
13382 }
13383
13384 /* Walk over the list of finalizer-procedures, check them, and if any one
13385 does not fit in with the standard's definition, print an error and remove
13386 it from the list. */
13387 prev_link = &derived->f2k_derived->finalizers;
13388 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
13389 {
13390 gfc_formal_arglist *dummy_args;
13391 gfc_symbol* arg;
13392 gfc_finalizer* i;
13393 int my_rank;
13394
13395 /* Skip this finalizer if we already resolved it. */
13396 if (list->proc_tree)
13397 {
13398 if (list->proc_tree->n.sym->formal->sym->as == NULL
13399 || list->proc_tree->n.sym->formal->sym->as->rank == 0)
13400 seen_scalar = true;
13401 prev_link = &(list->next);
13402 continue;
13403 }
13404
13405 /* Check this exists and is a SUBROUTINE. */
13406 if (!list->proc_sym->attr.subroutine)
13407 {
13408 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
13409 list->proc_sym->name, &list->where);
13410 goto error;
13411 }
13412
13413 /* We should have exactly one argument. */
13414 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
13415 if (!dummy_args || dummy_args->next)
13416 {
13417 gfc_error ("FINAL procedure at %L must have exactly one argument",
13418 &list->where);
13419 goto error;
13420 }
13421 arg = dummy_args->sym;
13422
13423 /* This argument must be of our type. */
13424 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
13425 {
13426 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
13427 &arg->declared_at, derived->name);
13428 goto error;
13429 }
13430
13431 /* It must neither be a pointer nor allocatable nor optional. */
13432 if (arg->attr.pointer)
13433 {
13434 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
13435 &arg->declared_at);
13436 goto error;
13437 }
13438 if (arg->attr.allocatable)
13439 {
13440 gfc_error ("Argument of FINAL procedure at %L must not be"
13441 " ALLOCATABLE", &arg->declared_at);
13442 goto error;
13443 }
13444 if (arg->attr.optional)
13445 {
13446 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
13447 &arg->declared_at);
13448 goto error;
13449 }
13450
13451 /* It must not be INTENT(OUT). */
13452 if (arg->attr.intent == INTENT_OUT)
13453 {
13454 gfc_error ("Argument of FINAL procedure at %L must not be"
13455 " INTENT(OUT)", &arg->declared_at);
13456 goto error;
13457 }
13458
13459 /* Warn if the procedure is non-scalar and not assumed shape. */
13460 if (warn_surprising && arg->as && arg->as->rank != 0
13461 && arg->as->type != AS_ASSUMED_SHAPE)
13462 gfc_warning (OPT_Wsurprising,
13463 "Non-scalar FINAL procedure at %L should have assumed"
13464 " shape argument", &arg->declared_at);
13465
13466 /* Check that it does not match in kind and rank with a FINAL procedure
13467 defined earlier. To really loop over the *earlier* declarations,
13468 we need to walk the tail of the list as new ones were pushed at the
13469 front. */
13470 /* TODO: Handle kind parameters once they are implemented. */
13471 my_rank = (arg->as ? arg->as->rank : 0);
13472 for (i = list->next; i; i = i->next)
13473 {
13474 gfc_formal_arglist *dummy_args;
13475
13476 /* Argument list might be empty; that is an error signalled earlier,
13477 but we nevertheless continued resolving. */
13478 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
13479 if (dummy_args)
13480 {
13481 gfc_symbol* i_arg = dummy_args->sym;
13482 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
13483 if (i_rank == my_rank)
13484 {
13485 gfc_error ("FINAL procedure %qs declared at %L has the same"
13486 " rank (%d) as %qs",
13487 list->proc_sym->name, &list->where, my_rank,
13488 i->proc_sym->name);
13489 goto error;
13490 }
13491 }
13492 }
13493
13494 /* Is this the/a scalar finalizer procedure? */
13495 if (my_rank == 0)
13496 seen_scalar = true;
13497
13498 /* Find the symtree for this procedure. */
13499 gcc_assert (!list->proc_tree);
13500 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
13501
13502 prev_link = &list->next;
13503 continue;
13504
13505 /* Remove wrong nodes immediately from the list so we don't risk any
13506 troubles in the future when they might fail later expectations. */
13507 error:
13508 i = list;
13509 *prev_link = list->next;
13510 gfc_free_finalizer (i);
13511 result = false;
13512 }
13513
13514 if (result == false)
13515 return false;
13516
13517 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
13518 were nodes in the list, must have been for arrays. It is surely a good
13519 idea to have a scalar version there if there's something to finalize. */
13520 if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
13521 gfc_warning (OPT_Wsurprising,
13522 "Only array FINAL procedures declared for derived type %qs"
13523 " defined at %L, suggest also scalar one",
13524 derived->name, &derived->declared_at);
13525
13526 vtab = gfc_find_derived_vtab (derived);
13527 c = vtab->ts.u.derived->components->next->next->next->next->next;
13528 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
13529
13530 if (finalizable)
13531 *finalizable = true;
13532
13533 return true;
13534 }
13535
13536
13537 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
13538
13539 static bool
13540 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
13541 const char* generic_name, locus where)
13542 {
13543 gfc_symbol *sym1, *sym2;
13544 const char *pass1, *pass2;
13545 gfc_formal_arglist *dummy_args;
13546
13547 gcc_assert (t1->specific && t2->specific);
13548 gcc_assert (!t1->specific->is_generic);
13549 gcc_assert (!t2->specific->is_generic);
13550 gcc_assert (t1->is_operator == t2->is_operator);
13551
13552 sym1 = t1->specific->u.specific->n.sym;
13553 sym2 = t2->specific->u.specific->n.sym;
13554
13555 if (sym1 == sym2)
13556 return true;
13557
13558 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
13559 if (sym1->attr.subroutine != sym2->attr.subroutine
13560 || sym1->attr.function != sym2->attr.function)
13561 {
13562 gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
13563 " GENERIC %qs at %L",
13564 sym1->name, sym2->name, generic_name, &where);
13565 return false;
13566 }
13567
13568 /* Determine PASS arguments. */
13569 if (t1->specific->nopass)
13570 pass1 = NULL;
13571 else if (t1->specific->pass_arg)
13572 pass1 = t1->specific->pass_arg;
13573 else
13574 {
13575 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
13576 if (dummy_args)
13577 pass1 = dummy_args->sym->name;
13578 else
13579 pass1 = NULL;
13580 }
13581 if (t2->specific->nopass)
13582 pass2 = NULL;
13583 else if (t2->specific->pass_arg)
13584 pass2 = t2->specific->pass_arg;
13585 else
13586 {
13587 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
13588 if (dummy_args)
13589 pass2 = dummy_args->sym->name;
13590 else
13591 pass2 = NULL;
13592 }
13593
13594 /* Compare the interfaces. */
13595 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
13596 NULL, 0, pass1, pass2))
13597 {
13598 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
13599 sym1->name, sym2->name, generic_name, &where);
13600 return false;
13601 }
13602
13603 return true;
13604 }
13605
13606
13607 /* Worker function for resolving a generic procedure binding; this is used to
13608 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
13609
13610 The difference between those cases is finding possible inherited bindings
13611 that are overridden, as one has to look for them in tb_sym_root,
13612 tb_uop_root or tb_op, respectively. Thus the caller must already find
13613 the super-type and set p->overridden correctly. */
13614
13615 static bool
13616 resolve_tb_generic_targets (gfc_symbol* super_type,
13617 gfc_typebound_proc* p, const char* name)
13618 {
13619 gfc_tbp_generic* target;
13620 gfc_symtree* first_target;
13621 gfc_symtree* inherited;
13622
13623 gcc_assert (p && p->is_generic);
13624
13625 /* Try to find the specific bindings for the symtrees in our target-list. */
13626 gcc_assert (p->u.generic);
13627 for (target = p->u.generic; target; target = target->next)
13628 if (!target->specific)
13629 {
13630 gfc_typebound_proc* overridden_tbp;
13631 gfc_tbp_generic* g;
13632 const char* target_name;
13633
13634 target_name = target->specific_st->name;
13635
13636 /* Defined for this type directly. */
13637 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
13638 {
13639 target->specific = target->specific_st->n.tb;
13640 goto specific_found;
13641 }
13642
13643 /* Look for an inherited specific binding. */
13644 if (super_type)
13645 {
13646 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
13647 true, NULL);
13648
13649 if (inherited)
13650 {
13651 gcc_assert (inherited->n.tb);
13652 target->specific = inherited->n.tb;
13653 goto specific_found;
13654 }
13655 }
13656
13657 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
13658 " at %L", target_name, name, &p->where);
13659 return false;
13660
13661 /* Once we've found the specific binding, check it is not ambiguous with
13662 other specifics already found or inherited for the same GENERIC. */
13663 specific_found:
13664 gcc_assert (target->specific);
13665
13666 /* This must really be a specific binding! */
13667 if (target->specific->is_generic)
13668 {
13669 gfc_error ("GENERIC %qs at %L must target a specific binding,"
13670 " %qs is GENERIC, too", name, &p->where, target_name);
13671 return false;
13672 }
13673
13674 /* Check those already resolved on this type directly. */
13675 for (g = p->u.generic; g; g = g->next)
13676 if (g != target && g->specific
13677 && !check_generic_tbp_ambiguity (target, g, name, p->where))
13678 return false;
13679
13680 /* Check for ambiguity with inherited specific targets. */
13681 for (overridden_tbp = p->overridden; overridden_tbp;
13682 overridden_tbp = overridden_tbp->overridden)
13683 if (overridden_tbp->is_generic)
13684 {
13685 for (g = overridden_tbp->u.generic; g; g = g->next)
13686 {
13687 gcc_assert (g->specific);
13688 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
13689 return false;
13690 }
13691 }
13692 }
13693
13694 /* If we attempt to "overwrite" a specific binding, this is an error. */
13695 if (p->overridden && !p->overridden->is_generic)
13696 {
13697 gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
13698 " the same name", name, &p->where);
13699 return false;
13700 }
13701
13702 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
13703 all must have the same attributes here. */
13704 first_target = p->u.generic->specific->u.specific;
13705 gcc_assert (first_target);
13706 p->subroutine = first_target->n.sym->attr.subroutine;
13707 p->function = first_target->n.sym->attr.function;
13708
13709 return true;
13710 }
13711
13712
13713 /* Resolve a GENERIC procedure binding for a derived type. */
13714
13715 static bool
13716 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
13717 {
13718 gfc_symbol* super_type;
13719
13720 /* Find the overridden binding if any. */
13721 st->n.tb->overridden = NULL;
13722 super_type = gfc_get_derived_super_type (derived);
13723 if (super_type)
13724 {
13725 gfc_symtree* overridden;
13726 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
13727 true, NULL);
13728
13729 if (overridden && overridden->n.tb)
13730 st->n.tb->overridden = overridden->n.tb;
13731 }
13732
13733 /* Resolve using worker function. */
13734 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
13735 }
13736
13737
13738 /* Retrieve the target-procedure of an operator binding and do some checks in
13739 common for intrinsic and user-defined type-bound operators. */
13740
13741 static gfc_symbol*
13742 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
13743 {
13744 gfc_symbol* target_proc;
13745
13746 gcc_assert (target->specific && !target->specific->is_generic);
13747 target_proc = target->specific->u.specific->n.sym;
13748 gcc_assert (target_proc);
13749
13750 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
13751 if (target->specific->nopass)
13752 {
13753 gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
13754 return NULL;
13755 }
13756
13757 return target_proc;
13758 }
13759
13760
13761 /* Resolve a type-bound intrinsic operator. */
13762
13763 static bool
13764 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
13765 gfc_typebound_proc* p)
13766 {
13767 gfc_symbol* super_type;
13768 gfc_tbp_generic* target;
13769
13770 /* If there's already an error here, do nothing (but don't fail again). */
13771 if (p->error)
13772 return true;
13773
13774 /* Operators should always be GENERIC bindings. */
13775 gcc_assert (p->is_generic);
13776
13777 /* Look for an overridden binding. */
13778 super_type = gfc_get_derived_super_type (derived);
13779 if (super_type && super_type->f2k_derived)
13780 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
13781 op, true, NULL);
13782 else
13783 p->overridden = NULL;
13784
13785 /* Resolve general GENERIC properties using worker function. */
13786 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
13787 goto error;
13788
13789 /* Check the targets to be procedures of correct interface. */
13790 for (target = p->u.generic; target; target = target->next)
13791 {
13792 gfc_symbol* target_proc;
13793
13794 target_proc = get_checked_tb_operator_target (target, p->where);
13795 if (!target_proc)
13796 goto error;
13797
13798 if (!gfc_check_operator_interface (target_proc, op, p->where))
13799 goto error;
13800
13801 /* Add target to non-typebound operator list. */
13802 if (!target->specific->deferred && !derived->attr.use_assoc
13803 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
13804 {
13805 gfc_interface *head, *intr;
13806
13807 /* Preempt 'gfc_check_new_interface' for submodules, where the
13808 mechanism for handling module procedures winds up resolving
13809 operator interfaces twice and would otherwise cause an error. */
13810 for (intr = derived->ns->op[op]; intr; intr = intr->next)
13811 if (intr->sym == target_proc
13812 && target_proc->attr.used_in_submodule)
13813 return true;
13814
13815 if (!gfc_check_new_interface (derived->ns->op[op],
13816 target_proc, p->where))
13817 return false;
13818 head = derived->ns->op[op];
13819 intr = gfc_get_interface ();
13820 intr->sym = target_proc;
13821 intr->where = p->where;
13822 intr->next = head;
13823 derived->ns->op[op] = intr;
13824 }
13825 }
13826
13827 return true;
13828
13829 error:
13830 p->error = 1;
13831 return false;
13832 }
13833
13834
13835 /* Resolve a type-bound user operator (tree-walker callback). */
13836
13837 static gfc_symbol* resolve_bindings_derived;
13838 static bool resolve_bindings_result;
13839
13840 static bool check_uop_procedure (gfc_symbol* sym, locus where);
13841
13842 static void
13843 resolve_typebound_user_op (gfc_symtree* stree)
13844 {
13845 gfc_symbol* super_type;
13846 gfc_tbp_generic* target;
13847
13848 gcc_assert (stree && stree->n.tb);
13849
13850 if (stree->n.tb->error)
13851 return;
13852
13853 /* Operators should always be GENERIC bindings. */
13854 gcc_assert (stree->n.tb->is_generic);
13855
13856 /* Find overridden procedure, if any. */
13857 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
13858 if (super_type && super_type->f2k_derived)
13859 {
13860 gfc_symtree* overridden;
13861 overridden = gfc_find_typebound_user_op (super_type, NULL,
13862 stree->name, true, NULL);
13863
13864 if (overridden && overridden->n.tb)
13865 stree->n.tb->overridden = overridden->n.tb;
13866 }
13867 else
13868 stree->n.tb->overridden = NULL;
13869
13870 /* Resolve basically using worker function. */
13871 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
13872 goto error;
13873
13874 /* Check the targets to be functions of correct interface. */
13875 for (target = stree->n.tb->u.generic; target; target = target->next)
13876 {
13877 gfc_symbol* target_proc;
13878
13879 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
13880 if (!target_proc)
13881 goto error;
13882
13883 if (!check_uop_procedure (target_proc, stree->n.tb->where))
13884 goto error;
13885 }
13886
13887 return;
13888
13889 error:
13890 resolve_bindings_result = false;
13891 stree->n.tb->error = 1;
13892 }
13893
13894
13895 /* Resolve the type-bound procedures for a derived type. */
13896
13897 static void
13898 resolve_typebound_procedure (gfc_symtree* stree)
13899 {
13900 gfc_symbol* proc;
13901 locus where;
13902 gfc_symbol* me_arg;
13903 gfc_symbol* super_type;
13904 gfc_component* comp;
13905
13906 gcc_assert (stree);
13907
13908 /* Undefined specific symbol from GENERIC target definition. */
13909 if (!stree->n.tb)
13910 return;
13911
13912 if (stree->n.tb->error)
13913 return;
13914
13915 /* If this is a GENERIC binding, use that routine. */
13916 if (stree->n.tb->is_generic)
13917 {
13918 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
13919 goto error;
13920 return;
13921 }
13922
13923 /* Get the target-procedure to check it. */
13924 gcc_assert (!stree->n.tb->is_generic);
13925 gcc_assert (stree->n.tb->u.specific);
13926 proc = stree->n.tb->u.specific->n.sym;
13927 where = stree->n.tb->where;
13928
13929 /* Default access should already be resolved from the parser. */
13930 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
13931
13932 if (stree->n.tb->deferred)
13933 {
13934 if (!check_proc_interface (proc, &where))
13935 goto error;
13936 }
13937 else
13938 {
13939 /* If proc has not been resolved at this point, proc->name may
13940 actually be a USE associated entity. See PR fortran/89647. */
13941 if (!proc->resolve_symbol_called
13942 && proc->attr.function == 0 && proc->attr.subroutine == 0)
13943 {
13944 gfc_symbol *tmp;
13945 gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
13946 if (tmp && tmp->attr.use_assoc)
13947 {
13948 proc->module = tmp->module;
13949 proc->attr.proc = tmp->attr.proc;
13950 proc->attr.function = tmp->attr.function;
13951 proc->attr.subroutine = tmp->attr.subroutine;
13952 proc->attr.use_assoc = tmp->attr.use_assoc;
13953 proc->ts = tmp->ts;
13954 proc->result = tmp->result;
13955 }
13956 }
13957
13958 /* Check for F08:C465. */
13959 if ((!proc->attr.subroutine && !proc->attr.function)
13960 || (proc->attr.proc != PROC_MODULE
13961 && proc->attr.if_source != IFSRC_IFBODY)
13962 || proc->attr.abstract)
13963 {
13964 gfc_error ("%qs must be a module procedure or an external "
13965 "procedure with an explicit interface at %L",
13966 proc->name, &where);
13967 goto error;
13968 }
13969 }
13970
13971 stree->n.tb->subroutine = proc->attr.subroutine;
13972 stree->n.tb->function = proc->attr.function;
13973
13974 /* Find the super-type of the current derived type. We could do this once and
13975 store in a global if speed is needed, but as long as not I believe this is
13976 more readable and clearer. */
13977 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
13978
13979 /* If PASS, resolve and check arguments if not already resolved / loaded
13980 from a .mod file. */
13981 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
13982 {
13983 gfc_formal_arglist *dummy_args;
13984
13985 dummy_args = gfc_sym_get_dummy_args (proc);
13986 if (stree->n.tb->pass_arg)
13987 {
13988 gfc_formal_arglist *i;
13989
13990 /* If an explicit passing argument name is given, walk the arg-list
13991 and look for it. */
13992
13993 me_arg = NULL;
13994 stree->n.tb->pass_arg_num = 1;
13995 for (i = dummy_args; i; i = i->next)
13996 {
13997 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
13998 {
13999 me_arg = i->sym;
14000 break;
14001 }
14002 ++stree->n.tb->pass_arg_num;
14003 }
14004
14005 if (!me_arg)
14006 {
14007 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
14008 " argument %qs",
14009 proc->name, stree->n.tb->pass_arg, &where,
14010 stree->n.tb->pass_arg);
14011 goto error;
14012 }
14013 }
14014 else
14015 {
14016 /* Otherwise, take the first one; there should in fact be at least
14017 one. */
14018 stree->n.tb->pass_arg_num = 1;
14019 if (!dummy_args)
14020 {
14021 gfc_error ("Procedure %qs with PASS at %L must have at"
14022 " least one argument", proc->name, &where);
14023 goto error;
14024 }
14025 me_arg = dummy_args->sym;
14026 }
14027
14028 /* Now check that the argument-type matches and the passed-object
14029 dummy argument is generally fine. */
14030
14031 gcc_assert (me_arg);
14032
14033 if (me_arg->ts.type != BT_CLASS)
14034 {
14035 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14036 " at %L", proc->name, &where);
14037 goto error;
14038 }
14039
14040 if (CLASS_DATA (me_arg)->ts.u.derived
14041 != resolve_bindings_derived)
14042 {
14043 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14044 " the derived-type %qs", me_arg->name, proc->name,
14045 me_arg->name, &where, resolve_bindings_derived->name);
14046 goto error;
14047 }
14048
14049 gcc_assert (me_arg->ts.type == BT_CLASS);
14050 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
14051 {
14052 gfc_error ("Passed-object dummy argument of %qs at %L must be"
14053 " scalar", proc->name, &where);
14054 goto error;
14055 }
14056 if (CLASS_DATA (me_arg)->attr.allocatable)
14057 {
14058 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14059 " be ALLOCATABLE", proc->name, &where);
14060 goto error;
14061 }
14062 if (CLASS_DATA (me_arg)->attr.class_pointer)
14063 {
14064 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14065 " be POINTER", proc->name, &where);
14066 goto error;
14067 }
14068 }
14069
14070 /* If we are extending some type, check that we don't override a procedure
14071 flagged NON_OVERRIDABLE. */
14072 stree->n.tb->overridden = NULL;
14073 if (super_type)
14074 {
14075 gfc_symtree* overridden;
14076 overridden = gfc_find_typebound_proc (super_type, NULL,
14077 stree->name, true, NULL);
14078
14079 if (overridden)
14080 {
14081 if (overridden->n.tb)
14082 stree->n.tb->overridden = overridden->n.tb;
14083
14084 if (!gfc_check_typebound_override (stree, overridden))
14085 goto error;
14086 }
14087 }
14088
14089 /* See if there's a name collision with a component directly in this type. */
14090 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
14091 if (!strcmp (comp->name, stree->name))
14092 {
14093 gfc_error ("Procedure %qs at %L has the same name as a component of"
14094 " %qs",
14095 stree->name, &where, resolve_bindings_derived->name);
14096 goto error;
14097 }
14098
14099 /* Try to find a name collision with an inherited component. */
14100 if (super_type && gfc_find_component (super_type, stree->name, true, true,
14101 NULL))
14102 {
14103 gfc_error ("Procedure %qs at %L has the same name as an inherited"
14104 " component of %qs",
14105 stree->name, &where, resolve_bindings_derived->name);
14106 goto error;
14107 }
14108
14109 stree->n.tb->error = 0;
14110 return;
14111
14112 error:
14113 resolve_bindings_result = false;
14114 stree->n.tb->error = 1;
14115 }
14116
14117
14118 static bool
14119 resolve_typebound_procedures (gfc_symbol* derived)
14120 {
14121 int op;
14122 gfc_symbol* super_type;
14123
14124 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
14125 return true;
14126
14127 super_type = gfc_get_derived_super_type (derived);
14128 if (super_type)
14129 resolve_symbol (super_type);
14130
14131 resolve_bindings_derived = derived;
14132 resolve_bindings_result = true;
14133
14134 if (derived->f2k_derived->tb_sym_root)
14135 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
14136 &resolve_typebound_procedure);
14137
14138 if (derived->f2k_derived->tb_uop_root)
14139 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
14140 &resolve_typebound_user_op);
14141
14142 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
14143 {
14144 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
14145 if (p && !resolve_typebound_intrinsic_op (derived,
14146 (gfc_intrinsic_op)op, p))
14147 resolve_bindings_result = false;
14148 }
14149
14150 return resolve_bindings_result;
14151 }
14152
14153
14154 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
14155 to give all identical derived types the same backend_decl. */
14156 static void
14157 add_dt_to_dt_list (gfc_symbol *derived)
14158 {
14159 if (!derived->dt_next)
14160 {
14161 if (gfc_derived_types)
14162 {
14163 derived->dt_next = gfc_derived_types->dt_next;
14164 gfc_derived_types->dt_next = derived;
14165 }
14166 else
14167 {
14168 derived->dt_next = derived;
14169 }
14170 gfc_derived_types = derived;
14171 }
14172 }
14173
14174
14175 /* Ensure that a derived-type is really not abstract, meaning that every
14176 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
14177
14178 static bool
14179 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
14180 {
14181 if (!st)
14182 return true;
14183
14184 if (!ensure_not_abstract_walker (sub, st->left))
14185 return false;
14186 if (!ensure_not_abstract_walker (sub, st->right))
14187 return false;
14188
14189 if (st->n.tb && st->n.tb->deferred)
14190 {
14191 gfc_symtree* overriding;
14192 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
14193 if (!overriding)
14194 return false;
14195 gcc_assert (overriding->n.tb);
14196 if (overriding->n.tb->deferred)
14197 {
14198 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
14199 " %qs is DEFERRED and not overridden",
14200 sub->name, &sub->declared_at, st->name);
14201 return false;
14202 }
14203 }
14204
14205 return true;
14206 }
14207
14208 static bool
14209 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
14210 {
14211 /* The algorithm used here is to recursively travel up the ancestry of sub
14212 and for each ancestor-type, check all bindings. If any of them is
14213 DEFERRED, look it up starting from sub and see if the found (overriding)
14214 binding is not DEFERRED.
14215 This is not the most efficient way to do this, but it should be ok and is
14216 clearer than something sophisticated. */
14217
14218 gcc_assert (ancestor && !sub->attr.abstract);
14219
14220 if (!ancestor->attr.abstract)
14221 return true;
14222
14223 /* Walk bindings of this ancestor. */
14224 if (ancestor->f2k_derived)
14225 {
14226 bool t;
14227 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
14228 if (!t)
14229 return false;
14230 }
14231
14232 /* Find next ancestor type and recurse on it. */
14233 ancestor = gfc_get_derived_super_type (ancestor);
14234 if (ancestor)
14235 return ensure_not_abstract (sub, ancestor);
14236
14237 return true;
14238 }
14239
14240
14241 /* This check for typebound defined assignments is done recursively
14242 since the order in which derived types are resolved is not always in
14243 order of the declarations. */
14244
14245 static void
14246 check_defined_assignments (gfc_symbol *derived)
14247 {
14248 gfc_component *c;
14249
14250 for (c = derived->components; c; c = c->next)
14251 {
14252 if (!gfc_bt_struct (c->ts.type)
14253 || c->attr.pointer
14254 || c->attr.allocatable
14255 || c->attr.proc_pointer_comp
14256 || c->attr.class_pointer
14257 || c->attr.proc_pointer)
14258 continue;
14259
14260 if (c->ts.u.derived->attr.defined_assign_comp
14261 || (c->ts.u.derived->f2k_derived
14262 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
14263 {
14264 derived->attr.defined_assign_comp = 1;
14265 return;
14266 }
14267
14268 check_defined_assignments (c->ts.u.derived);
14269 if (c->ts.u.derived->attr.defined_assign_comp)
14270 {
14271 derived->attr.defined_assign_comp = 1;
14272 return;
14273 }
14274 }
14275 }
14276
14277
14278 /* Resolve a single component of a derived type or structure. */
14279
14280 static bool
14281 resolve_component (gfc_component *c, gfc_symbol *sym)
14282 {
14283 gfc_symbol *super_type;
14284 symbol_attribute *attr;
14285
14286 if (c->attr.artificial)
14287 return true;
14288
14289 /* Do not allow vtype components to be resolved in nameless namespaces
14290 such as block data because the procedure pointers will cause ICEs
14291 and vtables are not needed in these contexts. */
14292 if (sym->attr.vtype && sym->attr.use_assoc
14293 && sym->ns->proc_name == NULL)
14294 return true;
14295
14296 /* F2008, C442. */
14297 if ((!sym->attr.is_class || c != sym->components)
14298 && c->attr.codimension
14299 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
14300 {
14301 gfc_error ("Coarray component %qs at %L must be allocatable with "
14302 "deferred shape", c->name, &c->loc);
14303 return false;
14304 }
14305
14306 /* F2008, C443. */
14307 if (c->attr.codimension && c->ts.type == BT_DERIVED
14308 && c->ts.u.derived->ts.is_iso_c)
14309 {
14310 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
14311 "shall not be a coarray", c->name, &c->loc);
14312 return false;
14313 }
14314
14315 /* F2008, C444. */
14316 if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
14317 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
14318 || c->attr.allocatable))
14319 {
14320 gfc_error ("Component %qs at %L with coarray component "
14321 "shall be a nonpointer, nonallocatable scalar",
14322 c->name, &c->loc);
14323 return false;
14324 }
14325
14326 /* F2008, C448. */
14327 if (c->ts.type == BT_CLASS)
14328 {
14329 if (CLASS_DATA (c))
14330 {
14331 attr = &(CLASS_DATA (c)->attr);
14332
14333 /* Fix up contiguous attribute. */
14334 if (c->attr.contiguous)
14335 attr->contiguous = 1;
14336 }
14337 else
14338 attr = NULL;
14339 }
14340 else
14341 attr = &c->attr;
14342
14343 if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
14344 {
14345 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
14346 "is not an array pointer", c->name, &c->loc);
14347 return false;
14348 }
14349
14350 /* F2003, 15.2.1 - length has to be one. */
14351 if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
14352 && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
14353 || !gfc_is_constant_expr (c->ts.u.cl->length)
14354 || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
14355 {
14356 gfc_error ("Component %qs of BIND(C) type at %L must have length one",
14357 c->name, &c->loc);
14358 return false;
14359 }
14360
14361 if (c->attr.proc_pointer && c->ts.interface)
14362 {
14363 gfc_symbol *ifc = c->ts.interface;
14364
14365 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
14366 {
14367 c->tb->error = 1;
14368 return false;
14369 }
14370
14371 if (ifc->attr.if_source || ifc->attr.intrinsic)
14372 {
14373 /* Resolve interface and copy attributes. */
14374 if (ifc->formal && !ifc->formal_ns)
14375 resolve_symbol (ifc);
14376 if (ifc->attr.intrinsic)
14377 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
14378
14379 if (ifc->result)
14380 {
14381 c->ts = ifc->result->ts;
14382 c->attr.allocatable = ifc->result->attr.allocatable;
14383 c->attr.pointer = ifc->result->attr.pointer;
14384 c->attr.dimension = ifc->result->attr.dimension;
14385 c->as = gfc_copy_array_spec (ifc->result->as);
14386 c->attr.class_ok = ifc->result->attr.class_ok;
14387 }
14388 else
14389 {
14390 c->ts = ifc->ts;
14391 c->attr.allocatable = ifc->attr.allocatable;
14392 c->attr.pointer = ifc->attr.pointer;
14393 c->attr.dimension = ifc->attr.dimension;
14394 c->as = gfc_copy_array_spec (ifc->as);
14395 c->attr.class_ok = ifc->attr.class_ok;
14396 }
14397 c->ts.interface = ifc;
14398 c->attr.function = ifc->attr.function;
14399 c->attr.subroutine = ifc->attr.subroutine;
14400
14401 c->attr.pure = ifc->attr.pure;
14402 c->attr.elemental = ifc->attr.elemental;
14403 c->attr.recursive = ifc->attr.recursive;
14404 c->attr.always_explicit = ifc->attr.always_explicit;
14405 c->attr.ext_attr |= ifc->attr.ext_attr;
14406 /* Copy char length. */
14407 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
14408 {
14409 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
14410 if (cl->length && !cl->resolved
14411 && !gfc_resolve_expr (cl->length))
14412 {
14413 c->tb->error = 1;
14414 return false;
14415 }
14416 c->ts.u.cl = cl;
14417 }
14418 }
14419 }
14420 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
14421 {
14422 /* Since PPCs are not implicitly typed, a PPC without an explicit
14423 interface must be a subroutine. */
14424 gfc_add_subroutine (&c->attr, c->name, &c->loc);
14425 }
14426
14427 /* Procedure pointer components: Check PASS arg. */
14428 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
14429 && !sym->attr.vtype)
14430 {
14431 gfc_symbol* me_arg;
14432
14433 if (c->tb->pass_arg)
14434 {
14435 gfc_formal_arglist* i;
14436
14437 /* If an explicit passing argument name is given, walk the arg-list
14438 and look for it. */
14439
14440 me_arg = NULL;
14441 c->tb->pass_arg_num = 1;
14442 for (i = c->ts.interface->formal; i; i = i->next)
14443 {
14444 if (!strcmp (i->sym->name, c->tb->pass_arg))
14445 {
14446 me_arg = i->sym;
14447 break;
14448 }
14449 c->tb->pass_arg_num++;
14450 }
14451
14452 if (!me_arg)
14453 {
14454 gfc_error ("Procedure pointer component %qs with PASS(%s) "
14455 "at %L has no argument %qs", c->name,
14456 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
14457 c->tb->error = 1;
14458 return false;
14459 }
14460 }
14461 else
14462 {
14463 /* Otherwise, take the first one; there should in fact be at least
14464 one. */
14465 c->tb->pass_arg_num = 1;
14466 if (!c->ts.interface->formal)
14467 {
14468 gfc_error ("Procedure pointer component %qs with PASS at %L "
14469 "must have at least one argument",
14470 c->name, &c->loc);
14471 c->tb->error = 1;
14472 return false;
14473 }
14474 me_arg = c->ts.interface->formal->sym;
14475 }
14476
14477 /* Now check that the argument-type matches. */
14478 gcc_assert (me_arg);
14479 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
14480 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
14481 || (me_arg->ts.type == BT_CLASS
14482 && CLASS_DATA (me_arg)->ts.u.derived != sym))
14483 {
14484 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14485 " the derived type %qs", me_arg->name, c->name,
14486 me_arg->name, &c->loc, sym->name);
14487 c->tb->error = 1;
14488 return false;
14489 }
14490
14491 /* Check for F03:C453. */
14492 if (CLASS_DATA (me_arg)->attr.dimension)
14493 {
14494 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14495 "must be scalar", me_arg->name, c->name, me_arg->name,
14496 &c->loc);
14497 c->tb->error = 1;
14498 return false;
14499 }
14500
14501 if (CLASS_DATA (me_arg)->attr.class_pointer)
14502 {
14503 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14504 "may not have the POINTER attribute", me_arg->name,
14505 c->name, me_arg->name, &c->loc);
14506 c->tb->error = 1;
14507 return false;
14508 }
14509
14510 if (CLASS_DATA (me_arg)->attr.allocatable)
14511 {
14512 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14513 "may not be ALLOCATABLE", me_arg->name, c->name,
14514 me_arg->name, &c->loc);
14515 c->tb->error = 1;
14516 return false;
14517 }
14518
14519 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
14520 {
14521 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14522 " at %L", c->name, &c->loc);
14523 return false;
14524 }
14525
14526 }
14527
14528 /* Check type-spec if this is not the parent-type component. */
14529 if (((sym->attr.is_class
14530 && (!sym->components->ts.u.derived->attr.extension
14531 || c != sym->components->ts.u.derived->components))
14532 || (!sym->attr.is_class
14533 && (!sym->attr.extension || c != sym->components)))
14534 && !sym->attr.vtype
14535 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
14536 return false;
14537
14538 super_type = gfc_get_derived_super_type (sym);
14539
14540 /* If this type is an extension, set the accessibility of the parent
14541 component. */
14542 if (super_type
14543 && ((sym->attr.is_class
14544 && c == sym->components->ts.u.derived->components)
14545 || (!sym->attr.is_class && c == sym->components))
14546 && strcmp (super_type->name, c->name) == 0)
14547 c->attr.access = super_type->attr.access;
14548
14549 /* If this type is an extension, see if this component has the same name
14550 as an inherited type-bound procedure. */
14551 if (super_type && !sym->attr.is_class
14552 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
14553 {
14554 gfc_error ("Component %qs of %qs at %L has the same name as an"
14555 " inherited type-bound procedure",
14556 c->name, sym->name, &c->loc);
14557 return false;
14558 }
14559
14560 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
14561 && !c->ts.deferred)
14562 {
14563 if (c->ts.u.cl->length == NULL
14564 || (!resolve_charlen(c->ts.u.cl))
14565 || !gfc_is_constant_expr (c->ts.u.cl->length))
14566 {
14567 gfc_error ("Character length of component %qs needs to "
14568 "be a constant specification expression at %L",
14569 c->name,
14570 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
14571 return false;
14572 }
14573 }
14574
14575 if (c->ts.type == BT_CHARACTER && c->ts.deferred
14576 && !c->attr.pointer && !c->attr.allocatable)
14577 {
14578 gfc_error ("Character component %qs of %qs at %L with deferred "
14579 "length must be a POINTER or ALLOCATABLE",
14580 c->name, sym->name, &c->loc);
14581 return false;
14582 }
14583
14584 /* Add the hidden deferred length field. */
14585 if (c->ts.type == BT_CHARACTER
14586 && (c->ts.deferred || c->attr.pdt_string)
14587 && !c->attr.function
14588 && !sym->attr.is_class)
14589 {
14590 char name[GFC_MAX_SYMBOL_LEN+9];
14591 gfc_component *strlen;
14592 sprintf (name, "_%s_length", c->name);
14593 strlen = gfc_find_component (sym, name, true, true, NULL);
14594 if (strlen == NULL)
14595 {
14596 if (!gfc_add_component (sym, name, &strlen))
14597 return false;
14598 strlen->ts.type = BT_INTEGER;
14599 strlen->ts.kind = gfc_charlen_int_kind;
14600 strlen->attr.access = ACCESS_PRIVATE;
14601 strlen->attr.artificial = 1;
14602 }
14603 }
14604
14605 if (c->ts.type == BT_DERIVED
14606 && sym->component_access != ACCESS_PRIVATE
14607 && gfc_check_symbol_access (sym)
14608 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
14609 && !c->ts.u.derived->attr.use_assoc
14610 && !gfc_check_symbol_access (c->ts.u.derived)
14611 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
14612 "PRIVATE type and cannot be a component of "
14613 "%qs, which is PUBLIC at %L", c->name,
14614 sym->name, &sym->declared_at))
14615 return false;
14616
14617 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
14618 {
14619 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
14620 "type %s", c->name, &c->loc, sym->name);
14621 return false;
14622 }
14623
14624 if (sym->attr.sequence)
14625 {
14626 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
14627 {
14628 gfc_error ("Component %s of SEQUENCE type declared at %L does "
14629 "not have the SEQUENCE attribute",
14630 c->ts.u.derived->name, &sym->declared_at);
14631 return false;
14632 }
14633 }
14634
14635 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
14636 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
14637 else if (c->ts.type == BT_CLASS && c->attr.class_ok
14638 && CLASS_DATA (c)->ts.u.derived->attr.generic)
14639 CLASS_DATA (c)->ts.u.derived
14640 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
14641
14642 /* If an allocatable component derived type is of the same type as
14643 the enclosing derived type, we need a vtable generating so that
14644 the __deallocate procedure is created. */
14645 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
14646 && c->ts.u.derived == sym && c->attr.allocatable == 1)
14647 gfc_find_vtab (&c->ts);
14648
14649 /* Ensure that all the derived type components are put on the
14650 derived type list; even in formal namespaces, where derived type
14651 pointer components might not have been declared. */
14652 if (c->ts.type == BT_DERIVED
14653 && c->ts.u.derived
14654 && c->ts.u.derived->components
14655 && c->attr.pointer
14656 && sym != c->ts.u.derived)
14657 add_dt_to_dt_list (c->ts.u.derived);
14658
14659 if (!gfc_resolve_array_spec (c->as,
14660 !(c->attr.pointer || c->attr.proc_pointer
14661 || c->attr.allocatable)))
14662 return false;
14663
14664 if (c->initializer && !sym->attr.vtype
14665 && !c->attr.pdt_kind && !c->attr.pdt_len
14666 && !gfc_check_assign_symbol (sym, c, c->initializer))
14667 return false;
14668
14669 return true;
14670 }
14671
14672
14673 /* Be nice about the locus for a structure expression - show the locus of the
14674 first non-null sub-expression if we can. */
14675
14676 static locus *
14677 cons_where (gfc_expr *struct_expr)
14678 {
14679 gfc_constructor *cons;
14680
14681 gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
14682
14683 cons = gfc_constructor_first (struct_expr->value.constructor);
14684 for (; cons; cons = gfc_constructor_next (cons))
14685 {
14686 if (cons->expr && cons->expr->expr_type != EXPR_NULL)
14687 return &cons->expr->where;
14688 }
14689
14690 return &struct_expr->where;
14691 }
14692
14693 /* Resolve the components of a structure type. Much less work than derived
14694 types. */
14695
14696 static bool
14697 resolve_fl_struct (gfc_symbol *sym)
14698 {
14699 gfc_component *c;
14700 gfc_expr *init = NULL;
14701 bool success;
14702
14703 /* Make sure UNIONs do not have overlapping initializers. */
14704 if (sym->attr.flavor == FL_UNION)
14705 {
14706 for (c = sym->components; c; c = c->next)
14707 {
14708 if (init && c->initializer)
14709 {
14710 gfc_error ("Conflicting initializers in union at %L and %L",
14711 cons_where (init), cons_where (c->initializer));
14712 gfc_free_expr (c->initializer);
14713 c->initializer = NULL;
14714 }
14715 if (init == NULL)
14716 init = c->initializer;
14717 }
14718 }
14719
14720 success = true;
14721 for (c = sym->components; c; c = c->next)
14722 if (!resolve_component (c, sym))
14723 success = false;
14724
14725 if (!success)
14726 return false;
14727
14728 if (sym->components)
14729 add_dt_to_dt_list (sym);
14730
14731 return true;
14732 }
14733
14734
14735 /* Resolve the components of a derived type. This does not have to wait until
14736 resolution stage, but can be done as soon as the dt declaration has been
14737 parsed. */
14738
14739 static bool
14740 resolve_fl_derived0 (gfc_symbol *sym)
14741 {
14742 gfc_symbol* super_type;
14743 gfc_component *c;
14744 gfc_formal_arglist *f;
14745 bool success;
14746
14747 if (sym->attr.unlimited_polymorphic)
14748 return true;
14749
14750 super_type = gfc_get_derived_super_type (sym);
14751
14752 /* F2008, C432. */
14753 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
14754 {
14755 gfc_error ("As extending type %qs at %L has a coarray component, "
14756 "parent type %qs shall also have one", sym->name,
14757 &sym->declared_at, super_type->name);
14758 return false;
14759 }
14760
14761 /* Ensure the extended type gets resolved before we do. */
14762 if (super_type && !resolve_fl_derived0 (super_type))
14763 return false;
14764
14765 /* An ABSTRACT type must be extensible. */
14766 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
14767 {
14768 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
14769 sym->name, &sym->declared_at);
14770 return false;
14771 }
14772
14773 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
14774 : sym->components;
14775
14776 success = true;
14777 for ( ; c != NULL; c = c->next)
14778 if (!resolve_component (c, sym))
14779 success = false;
14780
14781 if (!success)
14782 return false;
14783
14784 /* Now add the caf token field, where needed. */
14785 if (flag_coarray != GFC_FCOARRAY_NONE
14786 && !sym->attr.is_class && !sym->attr.vtype)
14787 {
14788 for (c = sym->components; c; c = c->next)
14789 if (!c->attr.dimension && !c->attr.codimension
14790 && (c->attr.allocatable || c->attr.pointer))
14791 {
14792 char name[GFC_MAX_SYMBOL_LEN+9];
14793 gfc_component *token;
14794 sprintf (name, "_caf_%s", c->name);
14795 token = gfc_find_component (sym, name, true, true, NULL);
14796 if (token == NULL)
14797 {
14798 if (!gfc_add_component (sym, name, &token))
14799 return false;
14800 token->ts.type = BT_VOID;
14801 token->ts.kind = gfc_default_integer_kind;
14802 token->attr.access = ACCESS_PRIVATE;
14803 token->attr.artificial = 1;
14804 token->attr.caf_token = 1;
14805 }
14806 }
14807 }
14808
14809 check_defined_assignments (sym);
14810
14811 if (!sym->attr.defined_assign_comp && super_type)
14812 sym->attr.defined_assign_comp
14813 = super_type->attr.defined_assign_comp;
14814
14815 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
14816 all DEFERRED bindings are overridden. */
14817 if (super_type && super_type->attr.abstract && !sym->attr.abstract
14818 && !sym->attr.is_class
14819 && !ensure_not_abstract (sym, super_type))
14820 return false;
14821
14822 /* Check that there is a component for every PDT parameter. */
14823 if (sym->attr.pdt_template)
14824 {
14825 for (f = sym->formal; f; f = f->next)
14826 {
14827 if (!f->sym)
14828 continue;
14829 c = gfc_find_component (sym, f->sym->name, true, true, NULL);
14830 if (c == NULL)
14831 {
14832 gfc_error ("Parameterized type %qs does not have a component "
14833 "corresponding to parameter %qs at %L", sym->name,
14834 f->sym->name, &sym->declared_at);
14835 break;
14836 }
14837 }
14838 }
14839
14840 /* Add derived type to the derived type list. */
14841 add_dt_to_dt_list (sym);
14842
14843 return true;
14844 }
14845
14846
14847 /* The following procedure does the full resolution of a derived type,
14848 including resolution of all type-bound procedures (if present). In contrast
14849 to 'resolve_fl_derived0' this can only be done after the module has been
14850 parsed completely. */
14851
14852 static bool
14853 resolve_fl_derived (gfc_symbol *sym)
14854 {
14855 gfc_symbol *gen_dt = NULL;
14856
14857 if (sym->attr.unlimited_polymorphic)
14858 return true;
14859
14860 if (!sym->attr.is_class)
14861 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
14862 if (gen_dt && gen_dt->generic && gen_dt->generic->next
14863 && (!gen_dt->generic->sym->attr.use_assoc
14864 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
14865 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
14866 "%qs at %L being the same name as derived "
14867 "type at %L", sym->name,
14868 gen_dt->generic->sym == sym
14869 ? gen_dt->generic->next->sym->name
14870 : gen_dt->generic->sym->name,
14871 gen_dt->generic->sym == sym
14872 ? &gen_dt->generic->next->sym->declared_at
14873 : &gen_dt->generic->sym->declared_at,
14874 &sym->declared_at))
14875 return false;
14876
14877 if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
14878 {
14879 gfc_error ("Derived type %qs at %L has not been declared",
14880 sym->name, &sym->declared_at);
14881 return false;
14882 }
14883
14884 /* Resolve the finalizer procedures. */
14885 if (!gfc_resolve_finalizers (sym, NULL))
14886 return false;
14887
14888 if (sym->attr.is_class && sym->ts.u.derived == NULL)
14889 {
14890 /* Fix up incomplete CLASS symbols. */
14891 gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
14892 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
14893
14894 /* Nothing more to do for unlimited polymorphic entities. */
14895 if (data->ts.u.derived->attr.unlimited_polymorphic)
14896 return true;
14897 else if (vptr->ts.u.derived == NULL)
14898 {
14899 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
14900 gcc_assert (vtab);
14901 vptr->ts.u.derived = vtab->ts.u.derived;
14902 if (!resolve_fl_derived0 (vptr->ts.u.derived))
14903 return false;
14904 }
14905 }
14906
14907 if (!resolve_fl_derived0 (sym))
14908 return false;
14909
14910 /* Resolve the type-bound procedures. */
14911 if (!resolve_typebound_procedures (sym))
14912 return false;
14913
14914 /* Generate module vtables subject to their accessibility and their not
14915 being vtables or pdt templates. If this is not done class declarations
14916 in external procedures wind up with their own version and so SELECT TYPE
14917 fails because the vptrs do not have the same address. */
14918 if (gfc_option.allow_std & GFC_STD_F2003
14919 && sym->ns->proc_name
14920 && sym->ns->proc_name->attr.flavor == FL_MODULE
14921 && sym->attr.access != ACCESS_PRIVATE
14922 && !(sym->attr.use_assoc || sym->attr.vtype || sym->attr.pdt_template))
14923 {
14924 gfc_symbol *vtab = gfc_find_derived_vtab (sym);
14925 gfc_set_sym_referenced (vtab);
14926 }
14927
14928 return true;
14929 }
14930
14931
14932 static bool
14933 resolve_fl_namelist (gfc_symbol *sym)
14934 {
14935 gfc_namelist *nl;
14936 gfc_symbol *nlsym;
14937
14938 for (nl = sym->namelist; nl; nl = nl->next)
14939 {
14940 /* Check again, the check in match only works if NAMELIST comes
14941 after the decl. */
14942 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
14943 {
14944 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
14945 "allowed", nl->sym->name, sym->name, &sym->declared_at);
14946 return false;
14947 }
14948
14949 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
14950 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
14951 "with assumed shape in namelist %qs at %L",
14952 nl->sym->name, sym->name, &sym->declared_at))
14953 return false;
14954
14955 if (is_non_constant_shape_array (nl->sym)
14956 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
14957 "with nonconstant shape in namelist %qs at %L",
14958 nl->sym->name, sym->name, &sym->declared_at))
14959 return false;
14960
14961 if (nl->sym->ts.type == BT_CHARACTER
14962 && (nl->sym->ts.u.cl->length == NULL
14963 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
14964 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
14965 "nonconstant character length in "
14966 "namelist %qs at %L", nl->sym->name,
14967 sym->name, &sym->declared_at))
14968 return false;
14969
14970 }
14971
14972 /* Reject PRIVATE objects in a PUBLIC namelist. */
14973 if (gfc_check_symbol_access (sym))
14974 {
14975 for (nl = sym->namelist; nl; nl = nl->next)
14976 {
14977 if (!nl->sym->attr.use_assoc
14978 && !is_sym_host_assoc (nl->sym, sym->ns)
14979 && !gfc_check_symbol_access (nl->sym))
14980 {
14981 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
14982 "cannot be member of PUBLIC namelist %qs at %L",
14983 nl->sym->name, sym->name, &sym->declared_at);
14984 return false;
14985 }
14986
14987 if (nl->sym->ts.type == BT_DERIVED
14988 && (nl->sym->ts.u.derived->attr.alloc_comp
14989 || nl->sym->ts.u.derived->attr.pointer_comp))
14990 {
14991 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
14992 "namelist %qs at %L with ALLOCATABLE "
14993 "or POINTER components", nl->sym->name,
14994 sym->name, &sym->declared_at))
14995 return false;
14996 return true;
14997 }
14998
14999 /* Types with private components that came here by USE-association. */
15000 if (nl->sym->ts.type == BT_DERIVED
15001 && derived_inaccessible (nl->sym->ts.u.derived))
15002 {
15003 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
15004 "components and cannot be member of namelist %qs at %L",
15005 nl->sym->name, sym->name, &sym->declared_at);
15006 return false;
15007 }
15008
15009 /* Types with private components that are defined in the same module. */
15010 if (nl->sym->ts.type == BT_DERIVED
15011 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
15012 && nl->sym->ts.u.derived->attr.private_comp)
15013 {
15014 gfc_error ("NAMELIST object %qs has PRIVATE components and "
15015 "cannot be a member of PUBLIC namelist %qs at %L",
15016 nl->sym->name, sym->name, &sym->declared_at);
15017 return false;
15018 }
15019 }
15020 }
15021
15022
15023 /* 14.1.2 A module or internal procedure represent local entities
15024 of the same type as a namelist member and so are not allowed. */
15025 for (nl = sym->namelist; nl; nl = nl->next)
15026 {
15027 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
15028 continue;
15029
15030 if (nl->sym->attr.function && nl->sym == nl->sym->result)
15031 if ((nl->sym == sym->ns->proc_name)
15032 ||
15033 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
15034 continue;
15035
15036 nlsym = NULL;
15037 if (nl->sym->name)
15038 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
15039 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
15040 {
15041 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
15042 "attribute in %qs at %L", nlsym->name,
15043 &sym->declared_at);
15044 return false;
15045 }
15046 }
15047
15048 return true;
15049 }
15050
15051
15052 static bool
15053 resolve_fl_parameter (gfc_symbol *sym)
15054 {
15055 /* A parameter array's shape needs to be constant. */
15056 if (sym->as != NULL
15057 && (sym->as->type == AS_DEFERRED
15058 || is_non_constant_shape_array (sym)))
15059 {
15060 gfc_error ("Parameter array %qs at %L cannot be automatic "
15061 "or of deferred shape", sym->name, &sym->declared_at);
15062 return false;
15063 }
15064
15065 /* Constraints on deferred type parameter. */
15066 if (!deferred_requirements (sym))
15067 return false;
15068
15069 /* Make sure a parameter that has been implicitly typed still
15070 matches the implicit type, since PARAMETER statements can precede
15071 IMPLICIT statements. */
15072 if (sym->attr.implicit_type
15073 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
15074 sym->ns)))
15075 {
15076 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
15077 "later IMPLICIT type", sym->name, &sym->declared_at);
15078 return false;
15079 }
15080
15081 /* Make sure the types of derived parameters are consistent. This
15082 type checking is deferred until resolution because the type may
15083 refer to a derived type from the host. */
15084 if (sym->ts.type == BT_DERIVED
15085 && !gfc_compare_types (&sym->ts, &sym->value->ts))
15086 {
15087 gfc_error ("Incompatible derived type in PARAMETER at %L",
15088 &sym->value->where);
15089 return false;
15090 }
15091
15092 /* F03:C509,C514. */
15093 if (sym->ts.type == BT_CLASS)
15094 {
15095 gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
15096 sym->name, &sym->declared_at);
15097 return false;
15098 }
15099
15100 return true;
15101 }
15102
15103
15104 /* Called by resolve_symbol to check PDTs. */
15105
15106 static void
15107 resolve_pdt (gfc_symbol* sym)
15108 {
15109 gfc_symbol *derived = NULL;
15110 gfc_actual_arglist *param;
15111 gfc_component *c;
15112 bool const_len_exprs = true;
15113 bool assumed_len_exprs = false;
15114 symbol_attribute *attr;
15115
15116 if (sym->ts.type == BT_DERIVED)
15117 {
15118 derived = sym->ts.u.derived;
15119 attr = &(sym->attr);
15120 }
15121 else if (sym->ts.type == BT_CLASS)
15122 {
15123 derived = CLASS_DATA (sym)->ts.u.derived;
15124 attr = &(CLASS_DATA (sym)->attr);
15125 }
15126 else
15127 gcc_unreachable ();
15128
15129 gcc_assert (derived->attr.pdt_type);
15130
15131 for (param = sym->param_list; param; param = param->next)
15132 {
15133 c = gfc_find_component (derived, param->name, false, true, NULL);
15134 gcc_assert (c);
15135 if (c->attr.pdt_kind)
15136 continue;
15137
15138 if (param->expr && !gfc_is_constant_expr (param->expr)
15139 && c->attr.pdt_len)
15140 const_len_exprs = false;
15141 else if (param->spec_type == SPEC_ASSUMED)
15142 assumed_len_exprs = true;
15143
15144 if (param->spec_type == SPEC_DEFERRED
15145 && !attr->allocatable && !attr->pointer)
15146 gfc_error ("The object %qs at %L has a deferred LEN "
15147 "parameter %qs and is neither allocatable "
15148 "nor a pointer", sym->name, &sym->declared_at,
15149 param->name);
15150
15151 }
15152
15153 if (!const_len_exprs
15154 && (sym->ns->proc_name->attr.is_main_program
15155 || sym->ns->proc_name->attr.flavor == FL_MODULE
15156 || sym->attr.save != SAVE_NONE))
15157 gfc_error ("The AUTOMATIC object %qs at %L must not have the "
15158 "SAVE attribute or be a variable declared in the "
15159 "main program, a module or a submodule(F08/C513)",
15160 sym->name, &sym->declared_at);
15161
15162 if (assumed_len_exprs && !(sym->attr.dummy
15163 || sym->attr.select_type_temporary || sym->attr.associate_var))
15164 gfc_error ("The object %qs at %L with ASSUMED type parameters "
15165 "must be a dummy or a SELECT TYPE selector(F08/4.2)",
15166 sym->name, &sym->declared_at);
15167 }
15168
15169
15170 /* Do anything necessary to resolve a symbol. Right now, we just
15171 assume that an otherwise unknown symbol is a variable. This sort
15172 of thing commonly happens for symbols in module. */
15173
15174 static void
15175 resolve_symbol (gfc_symbol *sym)
15176 {
15177 int check_constant, mp_flag;
15178 gfc_symtree *symtree;
15179 gfc_symtree *this_symtree;
15180 gfc_namespace *ns;
15181 gfc_component *c;
15182 symbol_attribute class_attr;
15183 gfc_array_spec *as;
15184 bool saved_specification_expr;
15185
15186 if (sym->resolve_symbol_called >= 1)
15187 return;
15188 sym->resolve_symbol_called = 1;
15189
15190 /* No symbol will ever have union type; only components can be unions.
15191 Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
15192 (just like derived type declaration symbols have flavor FL_DERIVED). */
15193 gcc_assert (sym->ts.type != BT_UNION);
15194
15195 /* Coarrayed polymorphic objects with allocatable or pointer components are
15196 yet unsupported for -fcoarray=lib. */
15197 if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
15198 && sym->ts.u.derived && CLASS_DATA (sym)
15199 && CLASS_DATA (sym)->attr.codimension
15200 && CLASS_DATA (sym)->ts.u.derived
15201 && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
15202 || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
15203 {
15204 gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
15205 "type coarrays at %L are unsupported", &sym->declared_at);
15206 return;
15207 }
15208
15209 if (sym->attr.artificial)
15210 return;
15211
15212 if (sym->attr.unlimited_polymorphic)
15213 return;
15214
15215 if (sym->attr.flavor == FL_UNKNOWN
15216 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
15217 && !sym->attr.generic && !sym->attr.external
15218 && sym->attr.if_source == IFSRC_UNKNOWN
15219 && sym->ts.type == BT_UNKNOWN))
15220 {
15221
15222 /* If we find that a flavorless symbol is an interface in one of the
15223 parent namespaces, find its symtree in this namespace, free the
15224 symbol and set the symtree to point to the interface symbol. */
15225 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
15226 {
15227 symtree = gfc_find_symtree (ns->sym_root, sym->name);
15228 if (symtree && (symtree->n.sym->generic ||
15229 (symtree->n.sym->attr.flavor == FL_PROCEDURE
15230 && sym->ns->construct_entities)))
15231 {
15232 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
15233 sym->name);
15234 if (this_symtree->n.sym == sym)
15235 {
15236 symtree->n.sym->refs++;
15237 gfc_release_symbol (sym);
15238 this_symtree->n.sym = symtree->n.sym;
15239 return;
15240 }
15241 }
15242 }
15243
15244 /* Otherwise give it a flavor according to such attributes as
15245 it has. */
15246 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
15247 && sym->attr.intrinsic == 0)
15248 sym->attr.flavor = FL_VARIABLE;
15249 else if (sym->attr.flavor == FL_UNKNOWN)
15250 {
15251 sym->attr.flavor = FL_PROCEDURE;
15252 if (sym->attr.dimension)
15253 sym->attr.function = 1;
15254 }
15255 }
15256
15257 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
15258 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
15259
15260 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
15261 && !resolve_procedure_interface (sym))
15262 return;
15263
15264 if (sym->attr.is_protected && !sym->attr.proc_pointer
15265 && (sym->attr.procedure || sym->attr.external))
15266 {
15267 if (sym->attr.external)
15268 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
15269 "at %L", &sym->declared_at);
15270 else
15271 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
15272 "at %L", &sym->declared_at);
15273
15274 return;
15275 }
15276
15277 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
15278 return;
15279
15280 else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
15281 && !resolve_fl_struct (sym))
15282 return;
15283
15284 /* Symbols that are module procedures with results (functions) have
15285 the types and array specification copied for type checking in
15286 procedures that call them, as well as for saving to a module
15287 file. These symbols can't stand the scrutiny that their results
15288 can. */
15289 mp_flag = (sym->result != NULL && sym->result != sym);
15290
15291 /* Make sure that the intrinsic is consistent with its internal
15292 representation. This needs to be done before assigning a default
15293 type to avoid spurious warnings. */
15294 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
15295 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
15296 return;
15297
15298 /* Resolve associate names. */
15299 if (sym->assoc)
15300 resolve_assoc_var (sym, true);
15301
15302 /* Assign default type to symbols that need one and don't have one. */
15303 if (sym->ts.type == BT_UNKNOWN)
15304 {
15305 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
15306 {
15307 gfc_set_default_type (sym, 1, NULL);
15308 }
15309
15310 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
15311 && !sym->attr.function && !sym->attr.subroutine
15312 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
15313 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
15314
15315 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
15316 {
15317 /* The specific case of an external procedure should emit an error
15318 in the case that there is no implicit type. */
15319 if (!mp_flag)
15320 {
15321 if (!sym->attr.mixed_entry_master)
15322 gfc_set_default_type (sym, sym->attr.external, NULL);
15323 }
15324 else
15325 {
15326 /* Result may be in another namespace. */
15327 resolve_symbol (sym->result);
15328
15329 if (!sym->result->attr.proc_pointer)
15330 {
15331 sym->ts = sym->result->ts;
15332 sym->as = gfc_copy_array_spec (sym->result->as);
15333 sym->attr.dimension = sym->result->attr.dimension;
15334 sym->attr.pointer = sym->result->attr.pointer;
15335 sym->attr.allocatable = sym->result->attr.allocatable;
15336 sym->attr.contiguous = sym->result->attr.contiguous;
15337 }
15338 }
15339 }
15340 }
15341 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
15342 {
15343 bool saved_specification_expr = specification_expr;
15344 specification_expr = true;
15345 gfc_resolve_array_spec (sym->result->as, false);
15346 specification_expr = saved_specification_expr;
15347 }
15348
15349 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
15350 {
15351 as = CLASS_DATA (sym)->as;
15352 class_attr = CLASS_DATA (sym)->attr;
15353 class_attr.pointer = class_attr.class_pointer;
15354 }
15355 else
15356 {
15357 class_attr = sym->attr;
15358 as = sym->as;
15359 }
15360
15361 /* F2008, C530. */
15362 if (sym->attr.contiguous
15363 && (!class_attr.dimension
15364 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
15365 && !class_attr.pointer)))
15366 {
15367 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
15368 "array pointer or an assumed-shape or assumed-rank array",
15369 sym->name, &sym->declared_at);
15370 return;
15371 }
15372
15373 /* Assumed size arrays and assumed shape arrays must be dummy
15374 arguments. Array-spec's of implied-shape should have been resolved to
15375 AS_EXPLICIT already. */
15376
15377 if (as)
15378 {
15379 /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
15380 specification expression. */
15381 if (as->type == AS_IMPLIED_SHAPE)
15382 {
15383 int i;
15384 for (i=0; i<as->rank; i++)
15385 {
15386 if (as->lower[i] != NULL && as->upper[i] == NULL)
15387 {
15388 gfc_error ("Bad specification for assumed size array at %L",
15389 &as->lower[i]->where);
15390 return;
15391 }
15392 }
15393 gcc_unreachable();
15394 }
15395
15396 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
15397 || as->type == AS_ASSUMED_SHAPE)
15398 && !sym->attr.dummy && !sym->attr.select_type_temporary)
15399 {
15400 if (as->type == AS_ASSUMED_SIZE)
15401 gfc_error ("Assumed size array at %L must be a dummy argument",
15402 &sym->declared_at);
15403 else
15404 gfc_error ("Assumed shape array at %L must be a dummy argument",
15405 &sym->declared_at);
15406 return;
15407 }
15408 /* TS 29113, C535a. */
15409 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
15410 && !sym->attr.select_type_temporary
15411 && !(cs_base && cs_base->current
15412 && cs_base->current->op == EXEC_SELECT_RANK))
15413 {
15414 gfc_error ("Assumed-rank array at %L must be a dummy argument",
15415 &sym->declared_at);
15416 return;
15417 }
15418 if (as->type == AS_ASSUMED_RANK
15419 && (sym->attr.codimension || sym->attr.value))
15420 {
15421 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
15422 "CODIMENSION attribute", &sym->declared_at);
15423 return;
15424 }
15425 }
15426
15427 /* Make sure symbols with known intent or optional are really dummy
15428 variable. Because of ENTRY statement, this has to be deferred
15429 until resolution time. */
15430
15431 if (!sym->attr.dummy
15432 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
15433 {
15434 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
15435 return;
15436 }
15437
15438 if (sym->attr.value && !sym->attr.dummy)
15439 {
15440 gfc_error ("%qs at %L cannot have the VALUE attribute because "
15441 "it is not a dummy argument", sym->name, &sym->declared_at);
15442 return;
15443 }
15444
15445 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
15446 {
15447 gfc_charlen *cl = sym->ts.u.cl;
15448 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
15449 {
15450 gfc_error ("Character dummy variable %qs at %L with VALUE "
15451 "attribute must have constant length",
15452 sym->name, &sym->declared_at);
15453 return;
15454 }
15455
15456 if (sym->ts.is_c_interop
15457 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
15458 {
15459 gfc_error ("C interoperable character dummy variable %qs at %L "
15460 "with VALUE attribute must have length one",
15461 sym->name, &sym->declared_at);
15462 return;
15463 }
15464 }
15465
15466 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
15467 && sym->ts.u.derived->attr.generic)
15468 {
15469 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
15470 if (!sym->ts.u.derived)
15471 {
15472 gfc_error ("The derived type %qs at %L is of type %qs, "
15473 "which has not been defined", sym->name,
15474 &sym->declared_at, sym->ts.u.derived->name);
15475 sym->ts.type = BT_UNKNOWN;
15476 return;
15477 }
15478 }
15479
15480 /* Use the same constraints as TYPE(*), except for the type check
15481 and that only scalars and assumed-size arrays are permitted. */
15482 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
15483 {
15484 if (!sym->attr.dummy)
15485 {
15486 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
15487 "a dummy argument", sym->name, &sym->declared_at);
15488 return;
15489 }
15490
15491 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
15492 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
15493 && sym->ts.type != BT_COMPLEX)
15494 {
15495 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
15496 "of type TYPE(*) or of an numeric intrinsic type",
15497 sym->name, &sym->declared_at);
15498 return;
15499 }
15500
15501 if (sym->attr.allocatable || sym->attr.codimension
15502 || sym->attr.pointer || sym->attr.value)
15503 {
15504 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
15505 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
15506 "attribute", sym->name, &sym->declared_at);
15507 return;
15508 }
15509
15510 if (sym->attr.intent == INTENT_OUT)
15511 {
15512 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
15513 "have the INTENT(OUT) attribute",
15514 sym->name, &sym->declared_at);
15515 return;
15516 }
15517 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
15518 {
15519 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
15520 "either be a scalar or an assumed-size array",
15521 sym->name, &sym->declared_at);
15522 return;
15523 }
15524
15525 /* Set the type to TYPE(*) and add a dimension(*) to ensure
15526 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
15527 packing. */
15528 sym->ts.type = BT_ASSUMED;
15529 sym->as = gfc_get_array_spec ();
15530 sym->as->type = AS_ASSUMED_SIZE;
15531 sym->as->rank = 1;
15532 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
15533 }
15534 else if (sym->ts.type == BT_ASSUMED)
15535 {
15536 /* TS 29113, C407a. */
15537 if (!sym->attr.dummy)
15538 {
15539 gfc_error ("Assumed type of variable %s at %L is only permitted "
15540 "for dummy variables", sym->name, &sym->declared_at);
15541 return;
15542 }
15543 if (sym->attr.allocatable || sym->attr.codimension
15544 || sym->attr.pointer || sym->attr.value)
15545 {
15546 gfc_error ("Assumed-type variable %s at %L may not have the "
15547 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
15548 sym->name, &sym->declared_at);
15549 return;
15550 }
15551 if (sym->attr.intent == INTENT_OUT)
15552 {
15553 gfc_error ("Assumed-type variable %s at %L may not have the "
15554 "INTENT(OUT) attribute",
15555 sym->name, &sym->declared_at);
15556 return;
15557 }
15558 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
15559 {
15560 gfc_error ("Assumed-type variable %s at %L shall not be an "
15561 "explicit-shape array", sym->name, &sym->declared_at);
15562 return;
15563 }
15564 }
15565
15566 /* If the symbol is marked as bind(c), that it is declared at module level
15567 scope and verify its type and kind. Do not do the latter for symbols
15568 that are implicitly typed because that is handled in
15569 gfc_set_default_type. Handle dummy arguments and procedure definitions
15570 separately. Also, anything that is use associated is not handled here
15571 but instead is handled in the module it is declared in. Finally, derived
15572 type definitions are allowed to be BIND(C) since that only implies that
15573 they're interoperable, and they are checked fully for interoperability
15574 when a variable is declared of that type. */
15575 if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
15576 && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
15577 && sym->attr.flavor != FL_DERIVED)
15578 {
15579 bool t = true;
15580
15581 /* First, make sure the variable is declared at the
15582 module-level scope (J3/04-007, Section 15.3). */
15583 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
15584 sym->attr.in_common == 0)
15585 {
15586 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
15587 "is neither a COMMON block nor declared at the "
15588 "module level scope", sym->name, &(sym->declared_at));
15589 t = false;
15590 }
15591 else if (sym->ts.type == BT_CHARACTER
15592 && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
15593 || !gfc_is_constant_expr (sym->ts.u.cl->length)
15594 || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
15595 {
15596 gfc_error ("BIND(C) Variable %qs at %L must have length one",
15597 sym->name, &sym->declared_at);
15598 t = false;
15599 }
15600 else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
15601 {
15602 t = verify_com_block_vars_c_interop (sym->common_head);
15603 }
15604 else if (sym->attr.implicit_type == 0)
15605 {
15606 /* If type() declaration, we need to verify that the components
15607 of the given type are all C interoperable, etc. */
15608 if (sym->ts.type == BT_DERIVED &&
15609 sym->ts.u.derived->attr.is_c_interop != 1)
15610 {
15611 /* Make sure the user marked the derived type as BIND(C). If
15612 not, call the verify routine. This could print an error
15613 for the derived type more than once if multiple variables
15614 of that type are declared. */
15615 if (sym->ts.u.derived->attr.is_bind_c != 1)
15616 verify_bind_c_derived_type (sym->ts.u.derived);
15617 t = false;
15618 }
15619
15620 /* Verify the variable itself as C interoperable if it
15621 is BIND(C). It is not possible for this to succeed if
15622 the verify_bind_c_derived_type failed, so don't have to handle
15623 any error returned by verify_bind_c_derived_type. */
15624 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
15625 sym->common_block);
15626 }
15627
15628 if (!t)
15629 {
15630 /* clear the is_bind_c flag to prevent reporting errors more than
15631 once if something failed. */
15632 sym->attr.is_bind_c = 0;
15633 return;
15634 }
15635 }
15636
15637 /* If a derived type symbol has reached this point, without its
15638 type being declared, we have an error. Notice that most
15639 conditions that produce undefined derived types have already
15640 been dealt with. However, the likes of:
15641 implicit type(t) (t) ..... call foo (t) will get us here if
15642 the type is not declared in the scope of the implicit
15643 statement. Change the type to BT_UNKNOWN, both because it is so
15644 and to prevent an ICE. */
15645 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
15646 && sym->ts.u.derived->components == NULL
15647 && !sym->ts.u.derived->attr.zero_comp)
15648 {
15649 gfc_error ("The derived type %qs at %L is of type %qs, "
15650 "which has not been defined", sym->name,
15651 &sym->declared_at, sym->ts.u.derived->name);
15652 sym->ts.type = BT_UNKNOWN;
15653 return;
15654 }
15655
15656 /* Make sure that the derived type has been resolved and that the
15657 derived type is visible in the symbol's namespace, if it is a
15658 module function and is not PRIVATE. */
15659 if (sym->ts.type == BT_DERIVED
15660 && sym->ts.u.derived->attr.use_assoc
15661 && sym->ns->proc_name
15662 && sym->ns->proc_name->attr.flavor == FL_MODULE
15663 && !resolve_fl_derived (sym->ts.u.derived))
15664 return;
15665
15666 /* Unless the derived-type declaration is use associated, Fortran 95
15667 does not allow public entries of private derived types.
15668 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
15669 161 in 95-006r3. */
15670 if (sym->ts.type == BT_DERIVED
15671 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
15672 && !sym->ts.u.derived->attr.use_assoc
15673 && gfc_check_symbol_access (sym)
15674 && !gfc_check_symbol_access (sym->ts.u.derived)
15675 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
15676 "derived type %qs",
15677 (sym->attr.flavor == FL_PARAMETER)
15678 ? "parameter" : "variable",
15679 sym->name, &sym->declared_at,
15680 sym->ts.u.derived->name))
15681 return;
15682
15683 /* F2008, C1302. */
15684 if (sym->ts.type == BT_DERIVED
15685 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
15686 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
15687 || sym->ts.u.derived->attr.lock_comp)
15688 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
15689 {
15690 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
15691 "type LOCK_TYPE must be a coarray", sym->name,
15692 &sym->declared_at);
15693 return;
15694 }
15695
15696 /* TS18508, C702/C703. */
15697 if (sym->ts.type == BT_DERIVED
15698 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
15699 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
15700 || sym->ts.u.derived->attr.event_comp)
15701 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
15702 {
15703 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
15704 "type EVENT_TYPE must be a coarray", sym->name,
15705 &sym->declared_at);
15706 return;
15707 }
15708
15709 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
15710 default initialization is defined (5.1.2.4.4). */
15711 if (sym->ts.type == BT_DERIVED
15712 && sym->attr.dummy
15713 && sym->attr.intent == INTENT_OUT
15714 && sym->as
15715 && sym->as->type == AS_ASSUMED_SIZE)
15716 {
15717 for (c = sym->ts.u.derived->components; c; c = c->next)
15718 {
15719 if (c->initializer)
15720 {
15721 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
15722 "ASSUMED SIZE and so cannot have a default initializer",
15723 sym->name, &sym->declared_at);
15724 return;
15725 }
15726 }
15727 }
15728
15729 /* F2008, C542. */
15730 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
15731 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
15732 {
15733 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
15734 "INTENT(OUT)", sym->name, &sym->declared_at);
15735 return;
15736 }
15737
15738 /* TS18508. */
15739 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
15740 && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
15741 {
15742 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
15743 "INTENT(OUT)", sym->name, &sym->declared_at);
15744 return;
15745 }
15746
15747 /* F2008, C525. */
15748 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15749 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15750 && CLASS_DATA (sym)->attr.coarray_comp))
15751 || class_attr.codimension)
15752 && (sym->attr.result || sym->result == sym))
15753 {
15754 gfc_error ("Function result %qs at %L shall not be a coarray or have "
15755 "a coarray component", sym->name, &sym->declared_at);
15756 return;
15757 }
15758
15759 /* F2008, C524. */
15760 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
15761 && sym->ts.u.derived->ts.is_iso_c)
15762 {
15763 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
15764 "shall not be a coarray", sym->name, &sym->declared_at);
15765 return;
15766 }
15767
15768 /* F2008, C525. */
15769 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15770 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15771 && CLASS_DATA (sym)->attr.coarray_comp))
15772 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
15773 || class_attr.allocatable))
15774 {
15775 gfc_error ("Variable %qs at %L with coarray component shall be a "
15776 "nonpointer, nonallocatable scalar, which is not a coarray",
15777 sym->name, &sym->declared_at);
15778 return;
15779 }
15780
15781 /* F2008, C526. The function-result case was handled above. */
15782 if (class_attr.codimension
15783 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
15784 || sym->attr.select_type_temporary
15785 || sym->attr.associate_var
15786 || (sym->ns->save_all && !sym->attr.automatic)
15787 || sym->ns->proc_name->attr.flavor == FL_MODULE
15788 || sym->ns->proc_name->attr.is_main_program
15789 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
15790 {
15791 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
15792 "nor a dummy argument", sym->name, &sym->declared_at);
15793 return;
15794 }
15795 /* F2008, C528. */
15796 else if (class_attr.codimension && !sym->attr.select_type_temporary
15797 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
15798 {
15799 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
15800 "deferred shape", sym->name, &sym->declared_at);
15801 return;
15802 }
15803 else if (class_attr.codimension && class_attr.allocatable && as
15804 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
15805 {
15806 gfc_error ("Allocatable coarray variable %qs at %L must have "
15807 "deferred shape", sym->name, &sym->declared_at);
15808 return;
15809 }
15810
15811 /* F2008, C541. */
15812 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15813 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15814 && CLASS_DATA (sym)->attr.coarray_comp))
15815 || (class_attr.codimension && class_attr.allocatable))
15816 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
15817 {
15818 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
15819 "allocatable coarray or have coarray components",
15820 sym->name, &sym->declared_at);
15821 return;
15822 }
15823
15824 if (class_attr.codimension && sym->attr.dummy
15825 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
15826 {
15827 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
15828 "procedure %qs", sym->name, &sym->declared_at,
15829 sym->ns->proc_name->name);
15830 return;
15831 }
15832
15833 if (sym->ts.type == BT_LOGICAL
15834 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
15835 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
15836 && sym->ns->proc_name->attr.is_bind_c)))
15837 {
15838 int i;
15839 for (i = 0; gfc_logical_kinds[i].kind; i++)
15840 if (gfc_logical_kinds[i].kind == sym->ts.kind)
15841 break;
15842 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
15843 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
15844 "%L with non-C_Bool kind in BIND(C) procedure "
15845 "%qs", sym->name, &sym->declared_at,
15846 sym->ns->proc_name->name))
15847 return;
15848 else if (!gfc_logical_kinds[i].c_bool
15849 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
15850 "%qs at %L with non-C_Bool kind in "
15851 "BIND(C) procedure %qs", sym->name,
15852 &sym->declared_at,
15853 sym->attr.function ? sym->name
15854 : sym->ns->proc_name->name))
15855 return;
15856 }
15857
15858 switch (sym->attr.flavor)
15859 {
15860 case FL_VARIABLE:
15861 if (!resolve_fl_variable (sym, mp_flag))
15862 return;
15863 break;
15864
15865 case FL_PROCEDURE:
15866 if (sym->formal && !sym->formal_ns)
15867 {
15868 /* Check that none of the arguments are a namelist. */
15869 gfc_formal_arglist *formal = sym->formal;
15870
15871 for (; formal; formal = formal->next)
15872 if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
15873 {
15874 gfc_error ("Namelist %qs cannot be an argument to "
15875 "subroutine or function at %L",
15876 formal->sym->name, &sym->declared_at);
15877 return;
15878 }
15879 }
15880
15881 if (!resolve_fl_procedure (sym, mp_flag))
15882 return;
15883 break;
15884
15885 case FL_NAMELIST:
15886 if (!resolve_fl_namelist (sym))
15887 return;
15888 break;
15889
15890 case FL_PARAMETER:
15891 if (!resolve_fl_parameter (sym))
15892 return;
15893 break;
15894
15895 default:
15896 break;
15897 }
15898
15899 /* Resolve array specifier. Check as well some constraints
15900 on COMMON blocks. */
15901
15902 check_constant = sym->attr.in_common && !sym->attr.pointer;
15903
15904 /* Set the formal_arg_flag so that check_conflict will not throw
15905 an error for host associated variables in the specification
15906 expression for an array_valued function. */
15907 if ((sym->attr.function || sym->attr.result) && sym->as)
15908 formal_arg_flag = true;
15909
15910 saved_specification_expr = specification_expr;
15911 specification_expr = true;
15912 gfc_resolve_array_spec (sym->as, check_constant);
15913 specification_expr = saved_specification_expr;
15914
15915 formal_arg_flag = false;
15916
15917 /* Resolve formal namespaces. */
15918 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
15919 && !sym->attr.contained && !sym->attr.intrinsic)
15920 gfc_resolve (sym->formal_ns);
15921
15922 /* Make sure the formal namespace is present. */
15923 if (sym->formal && !sym->formal_ns)
15924 {
15925 gfc_formal_arglist *formal = sym->formal;
15926 while (formal && !formal->sym)
15927 formal = formal->next;
15928
15929 if (formal)
15930 {
15931 sym->formal_ns = formal->sym->ns;
15932 if (sym->formal_ns && sym->ns != formal->sym->ns)
15933 sym->formal_ns->refs++;
15934 }
15935 }
15936
15937 /* Check threadprivate restrictions. */
15938 if (sym->attr.threadprivate && !sym->attr.save
15939 && !(sym->ns->save_all && !sym->attr.automatic)
15940 && (!sym->attr.in_common
15941 && sym->module == NULL
15942 && (sym->ns->proc_name == NULL
15943 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
15944 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
15945
15946 /* Check omp declare target restrictions. */
15947 if (sym->attr.omp_declare_target
15948 && sym->attr.flavor == FL_VARIABLE
15949 && !sym->attr.save
15950 && !(sym->ns->save_all && !sym->attr.automatic)
15951 && (!sym->attr.in_common
15952 && sym->module == NULL
15953 && (sym->ns->proc_name == NULL
15954 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
15955 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
15956 sym->name, &sym->declared_at);
15957
15958 /* If we have come this far we can apply default-initializers, as
15959 described in 14.7.5, to those variables that have not already
15960 been assigned one. */
15961 if (sym->ts.type == BT_DERIVED
15962 && !sym->value
15963 && !sym->attr.allocatable
15964 && !sym->attr.alloc_comp)
15965 {
15966 symbol_attribute *a = &sym->attr;
15967
15968 if ((!a->save && !a->dummy && !a->pointer
15969 && !a->in_common && !a->use_assoc
15970 && a->referenced
15971 && !((a->function || a->result)
15972 && (!a->dimension
15973 || sym->ts.u.derived->attr.alloc_comp
15974 || sym->ts.u.derived->attr.pointer_comp))
15975 && !(a->function && sym != sym->result))
15976 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
15977 apply_default_init (sym);
15978 else if (a->function && sym->result && a->access != ACCESS_PRIVATE
15979 && (sym->ts.u.derived->attr.alloc_comp
15980 || sym->ts.u.derived->attr.pointer_comp))
15981 /* Mark the result symbol to be referenced, when it has allocatable
15982 components. */
15983 sym->result->attr.referenced = 1;
15984 }
15985
15986 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
15987 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
15988 && !CLASS_DATA (sym)->attr.class_pointer
15989 && !CLASS_DATA (sym)->attr.allocatable)
15990 apply_default_init (sym);
15991
15992 /* If this symbol has a type-spec, check it. */
15993 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
15994 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
15995 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
15996 return;
15997
15998 if (sym->param_list)
15999 resolve_pdt (sym);
16000 }
16001
16002
16003 /************* Resolve DATA statements *************/
16004
16005 static struct
16006 {
16007 gfc_data_value *vnode;
16008 mpz_t left;
16009 }
16010 values;
16011
16012
16013 /* Advance the values structure to point to the next value in the data list. */
16014
16015 static bool
16016 next_data_value (void)
16017 {
16018 while (mpz_cmp_ui (values.left, 0) == 0)
16019 {
16020
16021 if (values.vnode->next == NULL)
16022 return false;
16023
16024 values.vnode = values.vnode->next;
16025 mpz_set (values.left, values.vnode->repeat);
16026 }
16027
16028 return true;
16029 }
16030
16031
16032 static bool
16033 check_data_variable (gfc_data_variable *var, locus *where)
16034 {
16035 gfc_expr *e;
16036 mpz_t size;
16037 mpz_t offset;
16038 bool t;
16039 ar_type mark = AR_UNKNOWN;
16040 int i;
16041 mpz_t section_index[GFC_MAX_DIMENSIONS];
16042 gfc_ref *ref;
16043 gfc_array_ref *ar;
16044 gfc_symbol *sym;
16045 int has_pointer;
16046
16047 if (!gfc_resolve_expr (var->expr))
16048 return false;
16049
16050 ar = NULL;
16051 mpz_init_set_si (offset, 0);
16052 e = var->expr;
16053
16054 if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
16055 && e->value.function.isym->id == GFC_ISYM_CAF_GET)
16056 e = e->value.function.actual->expr;
16057
16058 if (e->expr_type != EXPR_VARIABLE)
16059 {
16060 gfc_error ("Expecting definable entity near %L", where);
16061 return false;
16062 }
16063
16064 sym = e->symtree->n.sym;
16065
16066 if (sym->ns->is_block_data && !sym->attr.in_common)
16067 {
16068 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
16069 sym->name, &sym->declared_at);
16070 return false;
16071 }
16072
16073 if (e->ref == NULL && sym->as)
16074 {
16075 gfc_error ("DATA array %qs at %L must be specified in a previous"
16076 " declaration", sym->name, where);
16077 return false;
16078 }
16079
16080 if (gfc_is_coindexed (e))
16081 {
16082 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
16083 where);
16084 return false;
16085 }
16086
16087 has_pointer = sym->attr.pointer;
16088
16089 for (ref = e->ref; ref; ref = ref->next)
16090 {
16091 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
16092 has_pointer = 1;
16093
16094 if (has_pointer)
16095 {
16096 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
16097 {
16098 gfc_error ("DATA element %qs at %L is a pointer and so must "
16099 "be a full array", sym->name, where);
16100 return false;
16101 }
16102
16103 if (values.vnode->expr->expr_type == EXPR_CONSTANT)
16104 {
16105 gfc_error ("DATA object near %L has the pointer attribute "
16106 "and the corresponding DATA value is not a valid "
16107 "initial-data-target", where);
16108 return false;
16109 }
16110 }
16111 }
16112
16113 if (e->rank == 0 || has_pointer)
16114 {
16115 mpz_init_set_ui (size, 1);
16116 ref = NULL;
16117 }
16118 else
16119 {
16120 ref = e->ref;
16121
16122 /* Find the array section reference. */
16123 for (ref = e->ref; ref; ref = ref->next)
16124 {
16125 if (ref->type != REF_ARRAY)
16126 continue;
16127 if (ref->u.ar.type == AR_ELEMENT)
16128 continue;
16129 break;
16130 }
16131 gcc_assert (ref);
16132
16133 /* Set marks according to the reference pattern. */
16134 switch (ref->u.ar.type)
16135 {
16136 case AR_FULL:
16137 mark = AR_FULL;
16138 break;
16139
16140 case AR_SECTION:
16141 ar = &ref->u.ar;
16142 /* Get the start position of array section. */
16143 gfc_get_section_index (ar, section_index, &offset);
16144 mark = AR_SECTION;
16145 break;
16146
16147 default:
16148 gcc_unreachable ();
16149 }
16150
16151 if (!gfc_array_size (e, &size))
16152 {
16153 gfc_error ("Nonconstant array section at %L in DATA statement",
16154 where);
16155 mpz_clear (offset);
16156 return false;
16157 }
16158 }
16159
16160 t = true;
16161
16162 while (mpz_cmp_ui (size, 0) > 0)
16163 {
16164 if (!next_data_value ())
16165 {
16166 gfc_error ("DATA statement at %L has more variables than values",
16167 where);
16168 t = false;
16169 break;
16170 }
16171
16172 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
16173 if (!t)
16174 break;
16175
16176 /* If we have more than one element left in the repeat count,
16177 and we have more than one element left in the target variable,
16178 then create a range assignment. */
16179 /* FIXME: Only done for full arrays for now, since array sections
16180 seem tricky. */
16181 if (mark == AR_FULL && ref && ref->next == NULL
16182 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
16183 {
16184 mpz_t range;
16185
16186 if (mpz_cmp (size, values.left) >= 0)
16187 {
16188 mpz_init_set (range, values.left);
16189 mpz_sub (size, size, values.left);
16190 mpz_set_ui (values.left, 0);
16191 }
16192 else
16193 {
16194 mpz_init_set (range, size);
16195 mpz_sub (values.left, values.left, size);
16196 mpz_set_ui (size, 0);
16197 }
16198
16199 t = gfc_assign_data_value (var->expr, values.vnode->expr,
16200 offset, &range);
16201
16202 mpz_add (offset, offset, range);
16203 mpz_clear (range);
16204
16205 if (!t)
16206 break;
16207 }
16208
16209 /* Assign initial value to symbol. */
16210 else
16211 {
16212 mpz_sub_ui (values.left, values.left, 1);
16213 mpz_sub_ui (size, size, 1);
16214
16215 t = gfc_assign_data_value (var->expr, values.vnode->expr,
16216 offset, NULL);
16217 if (!t)
16218 break;
16219
16220 if (mark == AR_FULL)
16221 mpz_add_ui (offset, offset, 1);
16222
16223 /* Modify the array section indexes and recalculate the offset
16224 for next element. */
16225 else if (mark == AR_SECTION)
16226 gfc_advance_section (section_index, ar, &offset);
16227 }
16228 }
16229
16230 if (mark == AR_SECTION)
16231 {
16232 for (i = 0; i < ar->dimen; i++)
16233 mpz_clear (section_index[i]);
16234 }
16235
16236 mpz_clear (size);
16237 mpz_clear (offset);
16238
16239 return t;
16240 }
16241
16242
16243 static bool traverse_data_var (gfc_data_variable *, locus *);
16244
16245 /* Iterate over a list of elements in a DATA statement. */
16246
16247 static bool
16248 traverse_data_list (gfc_data_variable *var, locus *where)
16249 {
16250 mpz_t trip;
16251 iterator_stack frame;
16252 gfc_expr *e, *start, *end, *step;
16253 bool retval = true;
16254
16255 mpz_init (frame.value);
16256 mpz_init (trip);
16257
16258 start = gfc_copy_expr (var->iter.start);
16259 end = gfc_copy_expr (var->iter.end);
16260 step = gfc_copy_expr (var->iter.step);
16261
16262 if (!gfc_simplify_expr (start, 1)
16263 || start->expr_type != EXPR_CONSTANT)
16264 {
16265 gfc_error ("start of implied-do loop at %L could not be "
16266 "simplified to a constant value", &start->where);
16267 retval = false;
16268 goto cleanup;
16269 }
16270 if (!gfc_simplify_expr (end, 1)
16271 || end->expr_type != EXPR_CONSTANT)
16272 {
16273 gfc_error ("end of implied-do loop at %L could not be "
16274 "simplified to a constant value", &start->where);
16275 retval = false;
16276 goto cleanup;
16277 }
16278 if (!gfc_simplify_expr (step, 1)
16279 || step->expr_type != EXPR_CONSTANT)
16280 {
16281 gfc_error ("step of implied-do loop at %L could not be "
16282 "simplified to a constant value", &start->where);
16283 retval = false;
16284 goto cleanup;
16285 }
16286
16287 mpz_set (trip, end->value.integer);
16288 mpz_sub (trip, trip, start->value.integer);
16289 mpz_add (trip, trip, step->value.integer);
16290
16291 mpz_div (trip, trip, step->value.integer);
16292
16293 mpz_set (frame.value, start->value.integer);
16294
16295 frame.prev = iter_stack;
16296 frame.variable = var->iter.var->symtree;
16297 iter_stack = &frame;
16298
16299 while (mpz_cmp_ui (trip, 0) > 0)
16300 {
16301 if (!traverse_data_var (var->list, where))
16302 {
16303 retval = false;
16304 goto cleanup;
16305 }
16306
16307 e = gfc_copy_expr (var->expr);
16308 if (!gfc_simplify_expr (e, 1))
16309 {
16310 gfc_free_expr (e);
16311 retval = false;
16312 goto cleanup;
16313 }
16314
16315 mpz_add (frame.value, frame.value, step->value.integer);
16316
16317 mpz_sub_ui (trip, trip, 1);
16318 }
16319
16320 cleanup:
16321 mpz_clear (frame.value);
16322 mpz_clear (trip);
16323
16324 gfc_free_expr (start);
16325 gfc_free_expr (end);
16326 gfc_free_expr (step);
16327
16328 iter_stack = frame.prev;
16329 return retval;
16330 }
16331
16332
16333 /* Type resolve variables in the variable list of a DATA statement. */
16334
16335 static bool
16336 traverse_data_var (gfc_data_variable *var, locus *where)
16337 {
16338 bool t;
16339
16340 for (; var; var = var->next)
16341 {
16342 if (var->expr == NULL)
16343 t = traverse_data_list (var, where);
16344 else
16345 t = check_data_variable (var, where);
16346
16347 if (!t)
16348 return false;
16349 }
16350
16351 return true;
16352 }
16353
16354
16355 /* Resolve the expressions and iterators associated with a data statement.
16356 This is separate from the assignment checking because data lists should
16357 only be resolved once. */
16358
16359 static bool
16360 resolve_data_variables (gfc_data_variable *d)
16361 {
16362 for (; d; d = d->next)
16363 {
16364 if (d->list == NULL)
16365 {
16366 if (!gfc_resolve_expr (d->expr))
16367 return false;
16368 }
16369 else
16370 {
16371 if (!gfc_resolve_iterator (&d->iter, false, true))
16372 return false;
16373
16374 if (!resolve_data_variables (d->list))
16375 return false;
16376 }
16377 }
16378
16379 return true;
16380 }
16381
16382
16383 /* Resolve a single DATA statement. We implement this by storing a pointer to
16384 the value list into static variables, and then recursively traversing the
16385 variables list, expanding iterators and such. */
16386
16387 static void
16388 resolve_data (gfc_data *d)
16389 {
16390
16391 if (!resolve_data_variables (d->var))
16392 return;
16393
16394 values.vnode = d->value;
16395 if (d->value == NULL)
16396 mpz_set_ui (values.left, 0);
16397 else
16398 mpz_set (values.left, d->value->repeat);
16399
16400 if (!traverse_data_var (d->var, &d->where))
16401 return;
16402
16403 /* At this point, we better not have any values left. */
16404
16405 if (next_data_value ())
16406 gfc_error ("DATA statement at %L has more values than variables",
16407 &d->where);
16408 }
16409
16410
16411 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
16412 accessed by host or use association, is a dummy argument to a pure function,
16413 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
16414 is storage associated with any such variable, shall not be used in the
16415 following contexts: (clients of this function). */
16416
16417 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
16418 procedure. Returns zero if assignment is OK, nonzero if there is a
16419 problem. */
16420 int
16421 gfc_impure_variable (gfc_symbol *sym)
16422 {
16423 gfc_symbol *proc;
16424 gfc_namespace *ns;
16425
16426 if (sym->attr.use_assoc || sym->attr.in_common)
16427 return 1;
16428
16429 /* Check if the symbol's ns is inside the pure procedure. */
16430 for (ns = gfc_current_ns; ns; ns = ns->parent)
16431 {
16432 if (ns == sym->ns)
16433 break;
16434 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
16435 return 1;
16436 }
16437
16438 proc = sym->ns->proc_name;
16439 if (sym->attr.dummy
16440 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
16441 || proc->attr.function))
16442 return 1;
16443
16444 /* TODO: Sort out what can be storage associated, if anything, and include
16445 it here. In principle equivalences should be scanned but it does not
16446 seem to be possible to storage associate an impure variable this way. */
16447 return 0;
16448 }
16449
16450
16451 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
16452 current namespace is inside a pure procedure. */
16453
16454 int
16455 gfc_pure (gfc_symbol *sym)
16456 {
16457 symbol_attribute attr;
16458 gfc_namespace *ns;
16459
16460 if (sym == NULL)
16461 {
16462 /* Check if the current namespace or one of its parents
16463 belongs to a pure procedure. */
16464 for (ns = gfc_current_ns; ns; ns = ns->parent)
16465 {
16466 sym = ns->proc_name;
16467 if (sym == NULL)
16468 return 0;
16469 attr = sym->attr;
16470 if (attr.flavor == FL_PROCEDURE && attr.pure)
16471 return 1;
16472 }
16473 return 0;
16474 }
16475
16476 attr = sym->attr;
16477
16478 return attr.flavor == FL_PROCEDURE && attr.pure;
16479 }
16480
16481
16482 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
16483 checks if the current namespace is implicitly pure. Note that this
16484 function returns false for a PURE procedure. */
16485
16486 int
16487 gfc_implicit_pure (gfc_symbol *sym)
16488 {
16489 gfc_namespace *ns;
16490
16491 if (sym == NULL)
16492 {
16493 /* Check if the current procedure is implicit_pure. Walk up
16494 the procedure list until we find a procedure. */
16495 for (ns = gfc_current_ns; ns; ns = ns->parent)
16496 {
16497 sym = ns->proc_name;
16498 if (sym == NULL)
16499 return 0;
16500
16501 if (sym->attr.flavor == FL_PROCEDURE)
16502 break;
16503 }
16504 }
16505
16506 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
16507 && !sym->attr.pure;
16508 }
16509
16510
16511 void
16512 gfc_unset_implicit_pure (gfc_symbol *sym)
16513 {
16514 gfc_namespace *ns;
16515
16516 if (sym == NULL)
16517 {
16518 /* Check if the current procedure is implicit_pure. Walk up
16519 the procedure list until we find a procedure. */
16520 for (ns = gfc_current_ns; ns; ns = ns->parent)
16521 {
16522 sym = ns->proc_name;
16523 if (sym == NULL)
16524 return;
16525
16526 if (sym->attr.flavor == FL_PROCEDURE)
16527 break;
16528 }
16529 }
16530
16531 if (sym->attr.flavor == FL_PROCEDURE)
16532 sym->attr.implicit_pure = 0;
16533 else
16534 sym->attr.pure = 0;
16535 }
16536
16537
16538 /* Test whether the current procedure is elemental or not. */
16539
16540 int
16541 gfc_elemental (gfc_symbol *sym)
16542 {
16543 symbol_attribute attr;
16544
16545 if (sym == NULL)
16546 sym = gfc_current_ns->proc_name;
16547 if (sym == NULL)
16548 return 0;
16549 attr = sym->attr;
16550
16551 return attr.flavor == FL_PROCEDURE && attr.elemental;
16552 }
16553
16554
16555 /* Warn about unused labels. */
16556
16557 static void
16558 warn_unused_fortran_label (gfc_st_label *label)
16559 {
16560 if (label == NULL)
16561 return;
16562
16563 warn_unused_fortran_label (label->left);
16564
16565 if (label->defined == ST_LABEL_UNKNOWN)
16566 return;
16567
16568 switch (label->referenced)
16569 {
16570 case ST_LABEL_UNKNOWN:
16571 gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
16572 label->value, &label->where);
16573 break;
16574
16575 case ST_LABEL_BAD_TARGET:
16576 gfc_warning (OPT_Wunused_label,
16577 "Label %d at %L defined but cannot be used",
16578 label->value, &label->where);
16579 break;
16580
16581 default:
16582 break;
16583 }
16584
16585 warn_unused_fortran_label (label->right);
16586 }
16587
16588
16589 /* Returns the sequence type of a symbol or sequence. */
16590
16591 static seq_type
16592 sequence_type (gfc_typespec ts)
16593 {
16594 seq_type result;
16595 gfc_component *c;
16596
16597 switch (ts.type)
16598 {
16599 case BT_DERIVED:
16600
16601 if (ts.u.derived->components == NULL)
16602 return SEQ_NONDEFAULT;
16603
16604 result = sequence_type (ts.u.derived->components->ts);
16605 for (c = ts.u.derived->components->next; c; c = c->next)
16606 if (sequence_type (c->ts) != result)
16607 return SEQ_MIXED;
16608
16609 return result;
16610
16611 case BT_CHARACTER:
16612 if (ts.kind != gfc_default_character_kind)
16613 return SEQ_NONDEFAULT;
16614
16615 return SEQ_CHARACTER;
16616
16617 case BT_INTEGER:
16618 if (ts.kind != gfc_default_integer_kind)
16619 return SEQ_NONDEFAULT;
16620
16621 return SEQ_NUMERIC;
16622
16623 case BT_REAL:
16624 if (!(ts.kind == gfc_default_real_kind
16625 || ts.kind == gfc_default_double_kind))
16626 return SEQ_NONDEFAULT;
16627
16628 return SEQ_NUMERIC;
16629
16630 case BT_COMPLEX:
16631 if (ts.kind != gfc_default_complex_kind)
16632 return SEQ_NONDEFAULT;
16633
16634 return SEQ_NUMERIC;
16635
16636 case BT_LOGICAL:
16637 if (ts.kind != gfc_default_logical_kind)
16638 return SEQ_NONDEFAULT;
16639
16640 return SEQ_NUMERIC;
16641
16642 default:
16643 return SEQ_NONDEFAULT;
16644 }
16645 }
16646
16647
16648 /* Resolve derived type EQUIVALENCE object. */
16649
16650 static bool
16651 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
16652 {
16653 gfc_component *c = derived->components;
16654
16655 if (!derived)
16656 return true;
16657
16658 /* Shall not be an object of nonsequence derived type. */
16659 if (!derived->attr.sequence)
16660 {
16661 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
16662 "attribute to be an EQUIVALENCE object", sym->name,
16663 &e->where);
16664 return false;
16665 }
16666
16667 /* Shall not have allocatable components. */
16668 if (derived->attr.alloc_comp)
16669 {
16670 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
16671 "components to be an EQUIVALENCE object",sym->name,
16672 &e->where);
16673 return false;
16674 }
16675
16676 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
16677 {
16678 gfc_error ("Derived type variable %qs at %L with default "
16679 "initialization cannot be in EQUIVALENCE with a variable "
16680 "in COMMON", sym->name, &e->where);
16681 return false;
16682 }
16683
16684 for (; c ; c = c->next)
16685 {
16686 if (gfc_bt_struct (c->ts.type)
16687 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
16688 return false;
16689
16690 /* Shall not be an object of sequence derived type containing a pointer
16691 in the structure. */
16692 if (c->attr.pointer)
16693 {
16694 gfc_error ("Derived type variable %qs at %L with pointer "
16695 "component(s) cannot be an EQUIVALENCE object",
16696 sym->name, &e->where);
16697 return false;
16698 }
16699 }
16700 return true;
16701 }
16702
16703
16704 /* Resolve equivalence object.
16705 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
16706 an allocatable array, an object of nonsequence derived type, an object of
16707 sequence derived type containing a pointer at any level of component
16708 selection, an automatic object, a function name, an entry name, a result
16709 name, a named constant, a structure component, or a subobject of any of
16710 the preceding objects. A substring shall not have length zero. A
16711 derived type shall not have components with default initialization nor
16712 shall two objects of an equivalence group be initialized.
16713 Either all or none of the objects shall have an protected attribute.
16714 The simple constraints are done in symbol.c(check_conflict) and the rest
16715 are implemented here. */
16716
16717 static void
16718 resolve_equivalence (gfc_equiv *eq)
16719 {
16720 gfc_symbol *sym;
16721 gfc_symbol *first_sym;
16722 gfc_expr *e;
16723 gfc_ref *r;
16724 locus *last_where = NULL;
16725 seq_type eq_type, last_eq_type;
16726 gfc_typespec *last_ts;
16727 int object, cnt_protected;
16728 const char *msg;
16729
16730 last_ts = &eq->expr->symtree->n.sym->ts;
16731
16732 first_sym = eq->expr->symtree->n.sym;
16733
16734 cnt_protected = 0;
16735
16736 for (object = 1; eq; eq = eq->eq, object++)
16737 {
16738 e = eq->expr;
16739
16740 e->ts = e->symtree->n.sym->ts;
16741 /* match_varspec might not know yet if it is seeing
16742 array reference or substring reference, as it doesn't
16743 know the types. */
16744 if (e->ref && e->ref->type == REF_ARRAY)
16745 {
16746 gfc_ref *ref = e->ref;
16747 sym = e->symtree->n.sym;
16748
16749 if (sym->attr.dimension)
16750 {
16751 ref->u.ar.as = sym->as;
16752 ref = ref->next;
16753 }
16754
16755 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
16756 if (e->ts.type == BT_CHARACTER
16757 && ref
16758 && ref->type == REF_ARRAY
16759 && ref->u.ar.dimen == 1
16760 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
16761 && ref->u.ar.stride[0] == NULL)
16762 {
16763 gfc_expr *start = ref->u.ar.start[0];
16764 gfc_expr *end = ref->u.ar.end[0];
16765 void *mem = NULL;
16766
16767 /* Optimize away the (:) reference. */
16768 if (start == NULL && end == NULL)
16769 {
16770 if (e->ref == ref)
16771 e->ref = ref->next;
16772 else
16773 e->ref->next = ref->next;
16774 mem = ref;
16775 }
16776 else
16777 {
16778 ref->type = REF_SUBSTRING;
16779 if (start == NULL)
16780 start = gfc_get_int_expr (gfc_charlen_int_kind,
16781 NULL, 1);
16782 ref->u.ss.start = start;
16783 if (end == NULL && e->ts.u.cl)
16784 end = gfc_copy_expr (e->ts.u.cl->length);
16785 ref->u.ss.end = end;
16786 ref->u.ss.length = e->ts.u.cl;
16787 e->ts.u.cl = NULL;
16788 }
16789 ref = ref->next;
16790 free (mem);
16791 }
16792
16793 /* Any further ref is an error. */
16794 if (ref)
16795 {
16796 gcc_assert (ref->type == REF_ARRAY);
16797 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
16798 &ref->u.ar.where);
16799 continue;
16800 }
16801 }
16802
16803 if (!gfc_resolve_expr (e))
16804 continue;
16805
16806 sym = e->symtree->n.sym;
16807
16808 if (sym->attr.is_protected)
16809 cnt_protected++;
16810 if (cnt_protected > 0 && cnt_protected != object)
16811 {
16812 gfc_error ("Either all or none of the objects in the "
16813 "EQUIVALENCE set at %L shall have the "
16814 "PROTECTED attribute",
16815 &e->where);
16816 break;
16817 }
16818
16819 /* Shall not equivalence common block variables in a PURE procedure. */
16820 if (sym->ns->proc_name
16821 && sym->ns->proc_name->attr.pure
16822 && sym->attr.in_common)
16823 {
16824 /* Need to check for symbols that may have entered the pure
16825 procedure via a USE statement. */
16826 bool saw_sym = false;
16827 if (sym->ns->use_stmts)
16828 {
16829 gfc_use_rename *r;
16830 for (r = sym->ns->use_stmts->rename; r; r = r->next)
16831 if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
16832 }
16833 else
16834 saw_sym = true;
16835
16836 if (saw_sym)
16837 gfc_error ("COMMON block member %qs at %L cannot be an "
16838 "EQUIVALENCE object in the pure procedure %qs",
16839 sym->name, &e->where, sym->ns->proc_name->name);
16840 break;
16841 }
16842
16843 /* Shall not be a named constant. */
16844 if (e->expr_type == EXPR_CONSTANT)
16845 {
16846 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
16847 "object", sym->name, &e->where);
16848 continue;
16849 }
16850
16851 if (e->ts.type == BT_DERIVED
16852 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
16853 continue;
16854
16855 /* Check that the types correspond correctly:
16856 Note 5.28:
16857 A numeric sequence structure may be equivalenced to another sequence
16858 structure, an object of default integer type, default real type, double
16859 precision real type, default logical type such that components of the
16860 structure ultimately only become associated to objects of the same
16861 kind. A character sequence structure may be equivalenced to an object
16862 of default character kind or another character sequence structure.
16863 Other objects may be equivalenced only to objects of the same type and
16864 kind parameters. */
16865
16866 /* Identical types are unconditionally OK. */
16867 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
16868 goto identical_types;
16869
16870 last_eq_type = sequence_type (*last_ts);
16871 eq_type = sequence_type (sym->ts);
16872
16873 /* Since the pair of objects is not of the same type, mixed or
16874 non-default sequences can be rejected. */
16875
16876 msg = "Sequence %s with mixed components in EQUIVALENCE "
16877 "statement at %L with different type objects";
16878 if ((object ==2
16879 && last_eq_type == SEQ_MIXED
16880 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
16881 || (eq_type == SEQ_MIXED
16882 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
16883 continue;
16884
16885 msg = "Non-default type object or sequence %s in EQUIVALENCE "
16886 "statement at %L with objects of different type";
16887 if ((object ==2
16888 && last_eq_type == SEQ_NONDEFAULT
16889 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
16890 || (eq_type == SEQ_NONDEFAULT
16891 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
16892 continue;
16893
16894 msg ="Non-CHARACTER object %qs in default CHARACTER "
16895 "EQUIVALENCE statement at %L";
16896 if (last_eq_type == SEQ_CHARACTER
16897 && eq_type != SEQ_CHARACTER
16898 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
16899 continue;
16900
16901 msg ="Non-NUMERIC object %qs in default NUMERIC "
16902 "EQUIVALENCE statement at %L";
16903 if (last_eq_type == SEQ_NUMERIC
16904 && eq_type != SEQ_NUMERIC
16905 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
16906 continue;
16907
16908 identical_types:
16909
16910 last_ts =&sym->ts;
16911 last_where = &e->where;
16912
16913 if (!e->ref)
16914 continue;
16915
16916 /* Shall not be an automatic array. */
16917 if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
16918 {
16919 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
16920 "an EQUIVALENCE object", sym->name, &e->where);
16921 continue;
16922 }
16923
16924 r = e->ref;
16925 while (r)
16926 {
16927 /* Shall not be a structure component. */
16928 if (r->type == REF_COMPONENT)
16929 {
16930 gfc_error ("Structure component %qs at %L cannot be an "
16931 "EQUIVALENCE object",
16932 r->u.c.component->name, &e->where);
16933 break;
16934 }
16935
16936 /* A substring shall not have length zero. */
16937 if (r->type == REF_SUBSTRING)
16938 {
16939 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
16940 {
16941 gfc_error ("Substring at %L has length zero",
16942 &r->u.ss.start->where);
16943 break;
16944 }
16945 }
16946 r = r->next;
16947 }
16948 }
16949 }
16950
16951
16952 /* Function called by resolve_fntype to flag other symbols used in the
16953 length type parameter specification of function results. */
16954
16955 static bool
16956 flag_fn_result_spec (gfc_expr *expr,
16957 gfc_symbol *sym,
16958 int *f ATTRIBUTE_UNUSED)
16959 {
16960 gfc_namespace *ns;
16961 gfc_symbol *s;
16962
16963 if (expr->expr_type == EXPR_VARIABLE)
16964 {
16965 s = expr->symtree->n.sym;
16966 for (ns = s->ns; ns; ns = ns->parent)
16967 if (!ns->parent)
16968 break;
16969
16970 if (sym == s)
16971 {
16972 gfc_error ("Self reference in character length expression "
16973 "for %qs at %L", sym->name, &expr->where);
16974 return true;
16975 }
16976
16977 if (!s->fn_result_spec
16978 && s->attr.flavor == FL_PARAMETER)
16979 {
16980 /* Function contained in a module.... */
16981 if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
16982 {
16983 gfc_symtree *st;
16984 s->fn_result_spec = 1;
16985 /* Make sure that this symbol is translated as a module
16986 variable. */
16987 st = gfc_get_unique_symtree (ns);
16988 st->n.sym = s;
16989 s->refs++;
16990 }
16991 /* ... which is use associated and called. */
16992 else if (s->attr.use_assoc || s->attr.used_in_submodule
16993 ||
16994 /* External function matched with an interface. */
16995 (s->ns->proc_name
16996 && ((s->ns == ns
16997 && s->ns->proc_name->attr.if_source == IFSRC_DECL)
16998 || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
16999 && s->ns->proc_name->attr.function))
17000 s->fn_result_spec = 1;
17001 }
17002 }
17003 return false;
17004 }
17005
17006
17007 /* Resolve function and ENTRY types, issue diagnostics if needed. */
17008
17009 static void
17010 resolve_fntype (gfc_namespace *ns)
17011 {
17012 gfc_entry_list *el;
17013 gfc_symbol *sym;
17014
17015 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
17016 return;
17017
17018 /* If there are any entries, ns->proc_name is the entry master
17019 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
17020 if (ns->entries)
17021 sym = ns->entries->sym;
17022 else
17023 sym = ns->proc_name;
17024 if (sym->result == sym
17025 && sym->ts.type == BT_UNKNOWN
17026 && !gfc_set_default_type (sym, 0, NULL)
17027 && !sym->attr.untyped)
17028 {
17029 gfc_error ("Function %qs at %L has no IMPLICIT type",
17030 sym->name, &sym->declared_at);
17031 sym->attr.untyped = 1;
17032 }
17033
17034 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
17035 && !sym->attr.contained
17036 && !gfc_check_symbol_access (sym->ts.u.derived)
17037 && gfc_check_symbol_access (sym))
17038 {
17039 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
17040 "%L of PRIVATE type %qs", sym->name,
17041 &sym->declared_at, sym->ts.u.derived->name);
17042 }
17043
17044 if (ns->entries)
17045 for (el = ns->entries->next; el; el = el->next)
17046 {
17047 if (el->sym->result == el->sym
17048 && el->sym->ts.type == BT_UNKNOWN
17049 && !gfc_set_default_type (el->sym, 0, NULL)
17050 && !el->sym->attr.untyped)
17051 {
17052 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
17053 el->sym->name, &el->sym->declared_at);
17054 el->sym->attr.untyped = 1;
17055 }
17056 }
17057
17058 if (sym->ts.type == BT_CHARACTER)
17059 gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
17060 }
17061
17062
17063 /* 12.3.2.1.1 Defined operators. */
17064
17065 static bool
17066 check_uop_procedure (gfc_symbol *sym, locus where)
17067 {
17068 gfc_formal_arglist *formal;
17069
17070 if (!sym->attr.function)
17071 {
17072 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
17073 sym->name, &where);
17074 return false;
17075 }
17076
17077 if (sym->ts.type == BT_CHARACTER
17078 && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
17079 && !(sym->result && ((sym->result->ts.u.cl
17080 && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
17081 {
17082 gfc_error ("User operator procedure %qs at %L cannot be assumed "
17083 "character length", sym->name, &where);
17084 return false;
17085 }
17086
17087 formal = gfc_sym_get_dummy_args (sym);
17088 if (!formal || !formal->sym)
17089 {
17090 gfc_error ("User operator procedure %qs at %L must have at least "
17091 "one argument", sym->name, &where);
17092 return false;
17093 }
17094
17095 if (formal->sym->attr.intent != INTENT_IN)
17096 {
17097 gfc_error ("First argument of operator interface at %L must be "
17098 "INTENT(IN)", &where);
17099 return false;
17100 }
17101
17102 if (formal->sym->attr.optional)
17103 {
17104 gfc_error ("First argument of operator interface at %L cannot be "
17105 "optional", &where);
17106 return false;
17107 }
17108
17109 formal = formal->next;
17110 if (!formal || !formal->sym)
17111 return true;
17112
17113 if (formal->sym->attr.intent != INTENT_IN)
17114 {
17115 gfc_error ("Second argument of operator interface at %L must be "
17116 "INTENT(IN)", &where);
17117 return false;
17118 }
17119
17120 if (formal->sym->attr.optional)
17121 {
17122 gfc_error ("Second argument of operator interface at %L cannot be "
17123 "optional", &where);
17124 return false;
17125 }
17126
17127 if (formal->next)
17128 {
17129 gfc_error ("Operator interface at %L must have, at most, two "
17130 "arguments", &where);
17131 return false;
17132 }
17133
17134 return true;
17135 }
17136
17137 static void
17138 gfc_resolve_uops (gfc_symtree *symtree)
17139 {
17140 gfc_interface *itr;
17141
17142 if (symtree == NULL)
17143 return;
17144
17145 gfc_resolve_uops (symtree->left);
17146 gfc_resolve_uops (symtree->right);
17147
17148 for (itr = symtree->n.uop->op; itr; itr = itr->next)
17149 check_uop_procedure (itr->sym, itr->sym->declared_at);
17150 }
17151
17152
17153 /* Examine all of the expressions associated with a program unit,
17154 assign types to all intermediate expressions, make sure that all
17155 assignments are to compatible types and figure out which names
17156 refer to which functions or subroutines. It doesn't check code
17157 block, which is handled by gfc_resolve_code. */
17158
17159 static void
17160 resolve_types (gfc_namespace *ns)
17161 {
17162 gfc_namespace *n;
17163 gfc_charlen *cl;
17164 gfc_data *d;
17165 gfc_equiv *eq;
17166 gfc_namespace* old_ns = gfc_current_ns;
17167 bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
17168
17169 if (ns->types_resolved)
17170 return;
17171
17172 /* Check that all IMPLICIT types are ok. */
17173 if (!ns->seen_implicit_none)
17174 {
17175 unsigned letter;
17176 for (letter = 0; letter != GFC_LETTERS; ++letter)
17177 if (ns->set_flag[letter]
17178 && !resolve_typespec_used (&ns->default_type[letter],
17179 &ns->implicit_loc[letter], NULL))
17180 return;
17181 }
17182
17183 gfc_current_ns = ns;
17184
17185 resolve_entries (ns);
17186
17187 resolve_common_vars (&ns->blank_common, false);
17188 resolve_common_blocks (ns->common_root);
17189
17190 resolve_contained_functions (ns);
17191
17192 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
17193 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
17194 gfc_resolve_formal_arglist (ns->proc_name);
17195
17196 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
17197
17198 for (cl = ns->cl_list; cl; cl = cl->next)
17199 resolve_charlen (cl);
17200
17201 gfc_traverse_ns (ns, resolve_symbol);
17202
17203 resolve_fntype (ns);
17204
17205 for (n = ns->contained; n; n = n->sibling)
17206 {
17207 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
17208 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
17209 "also be PURE", n->proc_name->name,
17210 &n->proc_name->declared_at);
17211
17212 resolve_types (n);
17213 }
17214
17215 forall_flag = 0;
17216 gfc_do_concurrent_flag = 0;
17217 gfc_check_interfaces (ns);
17218
17219 gfc_traverse_ns (ns, resolve_values);
17220
17221 if (ns->save_all || (!flag_automatic && !recursive))
17222 gfc_save_all (ns);
17223
17224 iter_stack = NULL;
17225 for (d = ns->data; d; d = d->next)
17226 resolve_data (d);
17227
17228 iter_stack = NULL;
17229 gfc_traverse_ns (ns, gfc_formalize_init_value);
17230
17231 gfc_traverse_ns (ns, gfc_verify_binding_labels);
17232
17233 for (eq = ns->equiv; eq; eq = eq->next)
17234 resolve_equivalence (eq);
17235
17236 /* Warn about unused labels. */
17237 if (warn_unused_label)
17238 warn_unused_fortran_label (ns->st_labels);
17239
17240 gfc_resolve_uops (ns->uop_root);
17241
17242 gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
17243
17244 gfc_resolve_omp_declare_simd (ns);
17245
17246 gfc_resolve_omp_udrs (ns->omp_udr_root);
17247
17248 ns->types_resolved = 1;
17249
17250 gfc_current_ns = old_ns;
17251 }
17252
17253
17254 /* Call gfc_resolve_code recursively. */
17255
17256 static void
17257 resolve_codes (gfc_namespace *ns)
17258 {
17259 gfc_namespace *n;
17260 bitmap_obstack old_obstack;
17261
17262 if (ns->resolved == 1)
17263 return;
17264
17265 for (n = ns->contained; n; n = n->sibling)
17266 resolve_codes (n);
17267
17268 gfc_current_ns = ns;
17269
17270 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
17271 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
17272 cs_base = NULL;
17273
17274 /* Set to an out of range value. */
17275 current_entry_id = -1;
17276
17277 old_obstack = labels_obstack;
17278 bitmap_obstack_initialize (&labels_obstack);
17279
17280 gfc_resolve_oacc_declare (ns);
17281 gfc_resolve_oacc_routines (ns);
17282 gfc_resolve_omp_local_vars (ns);
17283 gfc_resolve_code (ns->code, ns);
17284
17285 bitmap_obstack_release (&labels_obstack);
17286 labels_obstack = old_obstack;
17287 }
17288
17289
17290 /* This function is called after a complete program unit has been compiled.
17291 Its purpose is to examine all of the expressions associated with a program
17292 unit, assign types to all intermediate expressions, make sure that all
17293 assignments are to compatible types and figure out which names refer to
17294 which functions or subroutines. */
17295
17296 void
17297 gfc_resolve (gfc_namespace *ns)
17298 {
17299 gfc_namespace *old_ns;
17300 code_stack *old_cs_base;
17301 struct gfc_omp_saved_state old_omp_state;
17302
17303 if (ns->resolved)
17304 return;
17305
17306 ns->resolved = -1;
17307 old_ns = gfc_current_ns;
17308 old_cs_base = cs_base;
17309
17310 /* As gfc_resolve can be called during resolution of an OpenMP construct
17311 body, we should clear any state associated to it, so that say NS's
17312 DO loops are not interpreted as OpenMP loops. */
17313 if (!ns->construct_entities)
17314 gfc_omp_save_and_clear_state (&old_omp_state);
17315
17316 resolve_types (ns);
17317 component_assignment_level = 0;
17318 resolve_codes (ns);
17319
17320 gfc_current_ns = old_ns;
17321 cs_base = old_cs_base;
17322 ns->resolved = 1;
17323
17324 gfc_run_passes (ns);
17325
17326 if (!ns->construct_entities)
17327 gfc_omp_restore_state (&old_omp_state);
17328 }