]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/fortran/resolve.c
Update copyright years.
[thirdparty/gcc.git] / gcc / fortran / resolve.c
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2021 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "bitmap.h"
26 #include "gfortran.h"
27 #include "arith.h" /* For gfc_compare_expr(). */
28 #include "dependency.h"
29 #include "data.h"
30 #include "target-memory.h" /* for gfc_simplify_transfer */
31 #include "constructor.h"
32
33 /* Types used in equivalence statements. */
34
35 enum seq_type
36 {
37 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
38 };
39
40 /* Stack to keep track of the nesting of blocks as we move through the
41 code. See resolve_branch() and gfc_resolve_code(). */
42
43 typedef struct code_stack
44 {
45 struct gfc_code *head, *current;
46 struct code_stack *prev;
47
48 /* This bitmap keeps track of the targets valid for a branch from
49 inside this block except for END {IF|SELECT}s of enclosing
50 blocks. */
51 bitmap reachable_labels;
52 }
53 code_stack;
54
55 static code_stack *cs_base = NULL;
56
57
58 /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
59
60 static int forall_flag;
61 int gfc_do_concurrent_flag;
62
63 /* True when we are resolving an expression that is an actual argument to
64 a procedure. */
65 static bool actual_arg = false;
66 /* True when we are resolving an expression that is the first actual argument
67 to a procedure. */
68 static bool first_actual_arg = false;
69
70
71 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
72
73 static int omp_workshare_flag;
74
75 /* True if we are processing a formal arglist. The corresponding function
76 resets the flag each time that it is read. */
77 static bool formal_arg_flag = false;
78
79 /* True if we are resolving a specification expression. */
80 static bool specification_expr = false;
81
82 /* The id of the last entry seen. */
83 static int current_entry_id;
84
85 /* We use bitmaps to determine if a branch target is valid. */
86 static bitmap_obstack labels_obstack;
87
88 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
89 static bool inquiry_argument = false;
90
91
92 bool
93 gfc_is_formal_arg (void)
94 {
95 return formal_arg_flag;
96 }
97
98 /* Is the symbol host associated? */
99 static bool
100 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
101 {
102 for (ns = ns->parent; ns; ns = ns->parent)
103 {
104 if (sym->ns == ns)
105 return true;
106 }
107
108 return false;
109 }
110
111 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
112 an ABSTRACT derived-type. If where is not NULL, an error message with that
113 locus is printed, optionally using name. */
114
115 static bool
116 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
117 {
118 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
119 {
120 if (where)
121 {
122 if (name)
123 gfc_error ("%qs at %L is of the ABSTRACT type %qs",
124 name, where, ts->u.derived->name);
125 else
126 gfc_error ("ABSTRACT type %qs used at %L",
127 ts->u.derived->name, where);
128 }
129
130 return false;
131 }
132
133 return true;
134 }
135
136
137 static bool
138 check_proc_interface (gfc_symbol *ifc, locus *where)
139 {
140 /* Several checks for F08:C1216. */
141 if (ifc->attr.procedure)
142 {
143 gfc_error ("Interface %qs at %L is declared "
144 "in a later PROCEDURE statement", ifc->name, where);
145 return false;
146 }
147 if (ifc->generic)
148 {
149 /* For generic interfaces, check if there is
150 a specific procedure with the same name. */
151 gfc_interface *gen = ifc->generic;
152 while (gen && strcmp (gen->sym->name, ifc->name) != 0)
153 gen = gen->next;
154 if (!gen)
155 {
156 gfc_error ("Interface %qs at %L may not be generic",
157 ifc->name, where);
158 return false;
159 }
160 }
161 if (ifc->attr.proc == PROC_ST_FUNCTION)
162 {
163 gfc_error ("Interface %qs at %L may not be a statement function",
164 ifc->name, where);
165 return false;
166 }
167 if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
168 || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
169 ifc->attr.intrinsic = 1;
170 if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
171 {
172 gfc_error ("Intrinsic procedure %qs not allowed in "
173 "PROCEDURE statement at %L", ifc->name, where);
174 return false;
175 }
176 if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
177 {
178 gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
179 return false;
180 }
181 return true;
182 }
183
184
185 static void resolve_symbol (gfc_symbol *sym);
186
187
188 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
189
190 static bool
191 resolve_procedure_interface (gfc_symbol *sym)
192 {
193 gfc_symbol *ifc = sym->ts.interface;
194
195 if (!ifc)
196 return true;
197
198 if (ifc == sym)
199 {
200 gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
201 sym->name, &sym->declared_at);
202 return false;
203 }
204 if (!check_proc_interface (ifc, &sym->declared_at))
205 return false;
206
207 if (ifc->attr.if_source || ifc->attr.intrinsic)
208 {
209 /* Resolve interface and copy attributes. */
210 resolve_symbol (ifc);
211 if (ifc->attr.intrinsic)
212 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
213
214 if (ifc->result)
215 {
216 sym->ts = ifc->result->ts;
217 sym->attr.allocatable = ifc->result->attr.allocatable;
218 sym->attr.pointer = ifc->result->attr.pointer;
219 sym->attr.dimension = ifc->result->attr.dimension;
220 sym->attr.class_ok = ifc->result->attr.class_ok;
221 sym->as = gfc_copy_array_spec (ifc->result->as);
222 sym->result = sym;
223 }
224 else
225 {
226 sym->ts = ifc->ts;
227 sym->attr.allocatable = ifc->attr.allocatable;
228 sym->attr.pointer = ifc->attr.pointer;
229 sym->attr.dimension = ifc->attr.dimension;
230 sym->attr.class_ok = ifc->attr.class_ok;
231 sym->as = gfc_copy_array_spec (ifc->as);
232 }
233 sym->ts.interface = ifc;
234 sym->attr.function = ifc->attr.function;
235 sym->attr.subroutine = ifc->attr.subroutine;
236
237 sym->attr.pure = ifc->attr.pure;
238 sym->attr.elemental = ifc->attr.elemental;
239 sym->attr.contiguous = ifc->attr.contiguous;
240 sym->attr.recursive = ifc->attr.recursive;
241 sym->attr.always_explicit = ifc->attr.always_explicit;
242 sym->attr.ext_attr |= ifc->attr.ext_attr;
243 sym->attr.is_bind_c = ifc->attr.is_bind_c;
244 /* Copy char length. */
245 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
246 {
247 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
248 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
249 && !gfc_resolve_expr (sym->ts.u.cl->length))
250 return false;
251 }
252 }
253
254 return true;
255 }
256
257
258 /* Resolve types of formal argument lists. These have to be done early so that
259 the formal argument lists of module procedures can be copied to the
260 containing module before the individual procedures are resolved
261 individually. We also resolve argument lists of procedures in interface
262 blocks because they are self-contained scoping units.
263
264 Since a dummy argument cannot be a non-dummy procedure, the only
265 resort left for untyped names are the IMPLICIT types. */
266
267 void
268 gfc_resolve_formal_arglist (gfc_symbol *proc)
269 {
270 gfc_formal_arglist *f;
271 gfc_symbol *sym;
272 bool saved_specification_expr;
273 int i;
274
275 if (proc->result != NULL)
276 sym = proc->result;
277 else
278 sym = proc;
279
280 if (gfc_elemental (proc)
281 || sym->attr.pointer || sym->attr.allocatable
282 || (sym->as && sym->as->rank != 0))
283 {
284 proc->attr.always_explicit = 1;
285 sym->attr.always_explicit = 1;
286 }
287
288 formal_arg_flag = true;
289
290 for (f = proc->formal; f; f = f->next)
291 {
292 gfc_array_spec *as;
293
294 sym = f->sym;
295
296 if (sym == NULL)
297 {
298 /* Alternate return placeholder. */
299 if (gfc_elemental (proc))
300 gfc_error ("Alternate return specifier in elemental subroutine "
301 "%qs at %L is not allowed", proc->name,
302 &proc->declared_at);
303 if (proc->attr.function)
304 gfc_error ("Alternate return specifier in function "
305 "%qs at %L is not allowed", proc->name,
306 &proc->declared_at);
307 continue;
308 }
309 else if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
310 && !resolve_procedure_interface (sym))
311 return;
312
313 if (strcmp (proc->name, sym->name) == 0)
314 {
315 gfc_error ("Self-referential argument "
316 "%qs at %L is not allowed", sym->name,
317 &proc->declared_at);
318 return;
319 }
320
321 if (sym->attr.if_source != IFSRC_UNKNOWN)
322 gfc_resolve_formal_arglist (sym);
323
324 if (sym->attr.subroutine || sym->attr.external)
325 {
326 if (sym->attr.flavor == FL_UNKNOWN)
327 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
328 }
329 else
330 {
331 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
332 && (!sym->attr.function || sym->result == sym))
333 gfc_set_default_type (sym, 1, sym->ns);
334 }
335
336 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
337 ? CLASS_DATA (sym)->as : sym->as;
338
339 saved_specification_expr = specification_expr;
340 specification_expr = true;
341 gfc_resolve_array_spec (as, 0);
342 specification_expr = saved_specification_expr;
343
344 /* We can't tell if an array with dimension (:) is assumed or deferred
345 shape until we know if it has the pointer or allocatable attributes.
346 */
347 if (as && as->rank > 0 && as->type == AS_DEFERRED
348 && ((sym->ts.type != BT_CLASS
349 && !(sym->attr.pointer || sym->attr.allocatable))
350 || (sym->ts.type == BT_CLASS
351 && !(CLASS_DATA (sym)->attr.class_pointer
352 || CLASS_DATA (sym)->attr.allocatable)))
353 && sym->attr.flavor != FL_PROCEDURE)
354 {
355 as->type = AS_ASSUMED_SHAPE;
356 for (i = 0; i < as->rank; i++)
357 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
358 }
359
360 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
361 || (as && as->type == AS_ASSUMED_RANK)
362 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
363 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
364 && (CLASS_DATA (sym)->attr.class_pointer
365 || CLASS_DATA (sym)->attr.allocatable
366 || CLASS_DATA (sym)->attr.target))
367 || sym->attr.optional)
368 {
369 proc->attr.always_explicit = 1;
370 if (proc->result)
371 proc->result->attr.always_explicit = 1;
372 }
373
374 /* If the flavor is unknown at this point, it has to be a variable.
375 A procedure specification would have already set the type. */
376
377 if (sym->attr.flavor == FL_UNKNOWN)
378 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
379
380 if (gfc_pure (proc))
381 {
382 if (sym->attr.flavor == FL_PROCEDURE)
383 {
384 /* F08:C1279. */
385 if (!gfc_pure (sym))
386 {
387 gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
388 "also be PURE", sym->name, &sym->declared_at);
389 continue;
390 }
391 }
392 else if (!sym->attr.pointer)
393 {
394 if (proc->attr.function && sym->attr.intent != INTENT_IN)
395 {
396 if (sym->attr.value)
397 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
398 " of pure function %qs at %L with VALUE "
399 "attribute but without INTENT(IN)",
400 sym->name, proc->name, &sym->declared_at);
401 else
402 gfc_error ("Argument %qs of pure function %qs at %L must "
403 "be INTENT(IN) or VALUE", sym->name, proc->name,
404 &sym->declared_at);
405 }
406
407 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
408 {
409 if (sym->attr.value)
410 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
411 " of pure subroutine %qs at %L with VALUE "
412 "attribute but without INTENT", sym->name,
413 proc->name, &sym->declared_at);
414 else
415 gfc_error ("Argument %qs of pure subroutine %qs at %L "
416 "must have its INTENT specified or have the "
417 "VALUE attribute", sym->name, proc->name,
418 &sym->declared_at);
419 }
420 }
421
422 /* F08:C1278a. */
423 if (sym->ts.type == BT_CLASS && sym->attr.intent == INTENT_OUT)
424 {
425 gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L"
426 " may not be polymorphic", sym->name, proc->name,
427 &sym->declared_at);
428 continue;
429 }
430 }
431
432 if (proc->attr.implicit_pure)
433 {
434 if (sym->attr.flavor == FL_PROCEDURE)
435 {
436 if (!gfc_pure (sym))
437 proc->attr.implicit_pure = 0;
438 }
439 else if (!sym->attr.pointer)
440 {
441 if (proc->attr.function && sym->attr.intent != INTENT_IN
442 && !sym->value)
443 proc->attr.implicit_pure = 0;
444
445 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
446 && !sym->value)
447 proc->attr.implicit_pure = 0;
448 }
449 }
450
451 if (gfc_elemental (proc))
452 {
453 /* F08:C1289. */
454 if (sym->attr.codimension
455 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
456 && CLASS_DATA (sym)->attr.codimension))
457 {
458 gfc_error ("Coarray dummy argument %qs at %L to elemental "
459 "procedure", sym->name, &sym->declared_at);
460 continue;
461 }
462
463 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
464 && CLASS_DATA (sym)->as))
465 {
466 gfc_error ("Argument %qs of elemental procedure at %L must "
467 "be scalar", sym->name, &sym->declared_at);
468 continue;
469 }
470
471 if (sym->attr.allocatable
472 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
473 && CLASS_DATA (sym)->attr.allocatable))
474 {
475 gfc_error ("Argument %qs of elemental procedure at %L cannot "
476 "have the ALLOCATABLE attribute", sym->name,
477 &sym->declared_at);
478 continue;
479 }
480
481 if (sym->attr.pointer
482 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
483 && CLASS_DATA (sym)->attr.class_pointer))
484 {
485 gfc_error ("Argument %qs of elemental procedure at %L cannot "
486 "have the POINTER attribute", sym->name,
487 &sym->declared_at);
488 continue;
489 }
490
491 if (sym->attr.flavor == FL_PROCEDURE)
492 {
493 gfc_error ("Dummy procedure %qs not allowed in elemental "
494 "procedure %qs at %L", sym->name, proc->name,
495 &sym->declared_at);
496 continue;
497 }
498
499 /* Fortran 2008 Corrigendum 1, C1290a. */
500 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
501 {
502 gfc_error ("Argument %qs of elemental procedure %qs at %L must "
503 "have its INTENT specified or have the VALUE "
504 "attribute", sym->name, proc->name,
505 &sym->declared_at);
506 continue;
507 }
508 }
509
510 /* Each dummy shall be specified to be scalar. */
511 if (proc->attr.proc == PROC_ST_FUNCTION)
512 {
513 if (sym->as != NULL)
514 {
515 /* F03:C1263 (R1238) The function-name and each dummy-arg-name
516 shall be specified, explicitly or implicitly, to be scalar. */
517 gfc_error ("Argument '%s' of statement function '%s' at %L "
518 "must be scalar", sym->name, proc->name,
519 &proc->declared_at);
520 continue;
521 }
522
523 if (sym->ts.type == BT_CHARACTER)
524 {
525 gfc_charlen *cl = sym->ts.u.cl;
526 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
527 {
528 gfc_error ("Character-valued argument %qs of statement "
529 "function at %L must have constant length",
530 sym->name, &sym->declared_at);
531 continue;
532 }
533 }
534 }
535 }
536 formal_arg_flag = false;
537 }
538
539
540 /* Work function called when searching for symbols that have argument lists
541 associated with them. */
542
543 static void
544 find_arglists (gfc_symbol *sym)
545 {
546 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
547 || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
548 return;
549
550 gfc_resolve_formal_arglist (sym);
551 }
552
553
554 /* Given a namespace, resolve all formal argument lists within the namespace.
555 */
556
557 static void
558 resolve_formal_arglists (gfc_namespace *ns)
559 {
560 if (ns == NULL)
561 return;
562
563 gfc_traverse_ns (ns, find_arglists);
564 }
565
566
567 static void
568 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
569 {
570 bool t;
571
572 if (sym && sym->attr.flavor == FL_PROCEDURE
573 && sym->ns->parent
574 && sym->ns->parent->proc_name
575 && sym->ns->parent->proc_name->attr.flavor == FL_PROCEDURE
576 && !strcmp (sym->name, sym->ns->parent->proc_name->name))
577 gfc_error ("Contained procedure %qs at %L has the same name as its "
578 "encompassing procedure", sym->name, &sym->declared_at);
579
580 /* If this namespace is not a function or an entry master function,
581 ignore it. */
582 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
583 || sym->attr.entry_master)
584 return;
585
586 if (!sym->result)
587 return;
588
589 /* Try to find out of what the return type is. */
590 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
591 {
592 t = gfc_set_default_type (sym->result, 0, ns);
593
594 if (!t && !sym->result->attr.untyped)
595 {
596 if (sym->result == sym)
597 gfc_error ("Contained function %qs at %L has no IMPLICIT type",
598 sym->name, &sym->declared_at);
599 else if (!sym->result->attr.proc_pointer)
600 gfc_error ("Result %qs of contained function %qs at %L has "
601 "no IMPLICIT type", sym->result->name, sym->name,
602 &sym->result->declared_at);
603 sym->result->attr.untyped = 1;
604 }
605 }
606
607 /* Fortran 2008 Draft Standard, page 535, C418, on type-param-value
608 type, lists the only ways a character length value of * can be used:
609 dummy arguments of procedures, named constants, function results and
610 in allocate statements if the allocate_object is an assumed length dummy
611 in external functions. Internal function results and results of module
612 procedures are not on this list, ergo, not permitted. */
613
614 if (sym->result->ts.type == BT_CHARACTER)
615 {
616 gfc_charlen *cl = sym->result->ts.u.cl;
617 if ((!cl || !cl->length) && !sym->result->ts.deferred)
618 {
619 /* See if this is a module-procedure and adapt error message
620 accordingly. */
621 bool module_proc;
622 gcc_assert (ns->parent && ns->parent->proc_name);
623 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
624
625 gfc_error (module_proc
626 ? G_("Character-valued module procedure %qs at %L"
627 " must not be assumed length")
628 : G_("Character-valued internal function %qs at %L"
629 " must not be assumed length"),
630 sym->name, &sym->declared_at);
631 }
632 }
633 }
634
635
636 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
637 introduce duplicates. */
638
639 static void
640 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
641 {
642 gfc_formal_arglist *f, *new_arglist;
643 gfc_symbol *new_sym;
644
645 for (; new_args != NULL; new_args = new_args->next)
646 {
647 new_sym = new_args->sym;
648 /* See if this arg is already in the formal argument list. */
649 for (f = proc->formal; f; f = f->next)
650 {
651 if (new_sym == f->sym)
652 break;
653 }
654
655 if (f)
656 continue;
657
658 /* Add a new argument. Argument order is not important. */
659 new_arglist = gfc_get_formal_arglist ();
660 new_arglist->sym = new_sym;
661 new_arglist->next = proc->formal;
662 proc->formal = new_arglist;
663 }
664 }
665
666
667 /* Flag the arguments that are not present in all entries. */
668
669 static void
670 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
671 {
672 gfc_formal_arglist *f, *head;
673 head = new_args;
674
675 for (f = proc->formal; f; f = f->next)
676 {
677 if (f->sym == NULL)
678 continue;
679
680 for (new_args = head; new_args; new_args = new_args->next)
681 {
682 if (new_args->sym == f->sym)
683 break;
684 }
685
686 if (new_args)
687 continue;
688
689 f->sym->attr.not_always_present = 1;
690 }
691 }
692
693
694 /* Resolve alternate entry points. If a symbol has multiple entry points we
695 create a new master symbol for the main routine, and turn the existing
696 symbol into an entry point. */
697
698 static void
699 resolve_entries (gfc_namespace *ns)
700 {
701 gfc_namespace *old_ns;
702 gfc_code *c;
703 gfc_symbol *proc;
704 gfc_entry_list *el;
705 char name[GFC_MAX_SYMBOL_LEN + 1];
706 static int master_count = 0;
707
708 if (ns->proc_name == NULL)
709 return;
710
711 /* No need to do anything if this procedure doesn't have alternate entry
712 points. */
713 if (!ns->entries)
714 return;
715
716 /* We may already have resolved alternate entry points. */
717 if (ns->proc_name->attr.entry_master)
718 return;
719
720 /* If this isn't a procedure something has gone horribly wrong. */
721 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
722
723 /* Remember the current namespace. */
724 old_ns = gfc_current_ns;
725
726 gfc_current_ns = ns;
727
728 /* Add the main entry point to the list of entry points. */
729 el = gfc_get_entry_list ();
730 el->sym = ns->proc_name;
731 el->id = 0;
732 el->next = ns->entries;
733 ns->entries = el;
734 ns->proc_name->attr.entry = 1;
735
736 /* If it is a module function, it needs to be in the right namespace
737 so that gfc_get_fake_result_decl can gather up the results. The
738 need for this arose in get_proc_name, where these beasts were
739 left in their own namespace, to keep prior references linked to
740 the entry declaration.*/
741 if (ns->proc_name->attr.function
742 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
743 el->sym->ns = ns;
744
745 /* Do the same for entries where the master is not a module
746 procedure. These are retained in the module namespace because
747 of the module procedure declaration. */
748 for (el = el->next; el; el = el->next)
749 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
750 && el->sym->attr.mod_proc)
751 el->sym->ns = ns;
752 el = ns->entries;
753
754 /* Add an entry statement for it. */
755 c = gfc_get_code (EXEC_ENTRY);
756 c->ext.entry = el;
757 c->next = ns->code;
758 ns->code = c;
759
760 /* Create a new symbol for the master function. */
761 /* Give the internal function a unique name (within this file).
762 Also include the function name so the user has some hope of figuring
763 out what is going on. */
764 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
765 master_count++, ns->proc_name->name);
766 gfc_get_ha_symbol (name, &proc);
767 gcc_assert (proc != NULL);
768
769 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
770 if (ns->proc_name->attr.subroutine)
771 gfc_add_subroutine (&proc->attr, proc->name, NULL);
772 else
773 {
774 gfc_symbol *sym;
775 gfc_typespec *ts, *fts;
776 gfc_array_spec *as, *fas;
777 gfc_add_function (&proc->attr, proc->name, NULL);
778 proc->result = proc;
779 fas = ns->entries->sym->as;
780 fas = fas ? fas : ns->entries->sym->result->as;
781 fts = &ns->entries->sym->result->ts;
782 if (fts->type == BT_UNKNOWN)
783 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
784 for (el = ns->entries->next; el; el = el->next)
785 {
786 ts = &el->sym->result->ts;
787 as = el->sym->as;
788 as = as ? as : el->sym->result->as;
789 if (ts->type == BT_UNKNOWN)
790 ts = gfc_get_default_type (el->sym->result->name, NULL);
791
792 if (! gfc_compare_types (ts, fts)
793 || (el->sym->result->attr.dimension
794 != ns->entries->sym->result->attr.dimension)
795 || (el->sym->result->attr.pointer
796 != ns->entries->sym->result->attr.pointer))
797 break;
798 else if (as && fas && ns->entries->sym->result != el->sym->result
799 && gfc_compare_array_spec (as, fas) == 0)
800 gfc_error ("Function %s at %L has entries with mismatched "
801 "array specifications", ns->entries->sym->name,
802 &ns->entries->sym->declared_at);
803 /* The characteristics need to match and thus both need to have
804 the same string length, i.e. both len=*, or both len=4.
805 Having both len=<variable> is also possible, but difficult to
806 check at compile time. */
807 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
808 && (((ts->u.cl->length && !fts->u.cl->length)
809 ||(!ts->u.cl->length && fts->u.cl->length))
810 || (ts->u.cl->length
811 && ts->u.cl->length->expr_type
812 != fts->u.cl->length->expr_type)
813 || (ts->u.cl->length
814 && ts->u.cl->length->expr_type == EXPR_CONSTANT
815 && mpz_cmp (ts->u.cl->length->value.integer,
816 fts->u.cl->length->value.integer) != 0)))
817 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
818 "entries returning variables of different "
819 "string lengths", ns->entries->sym->name,
820 &ns->entries->sym->declared_at);
821 }
822
823 if (el == NULL)
824 {
825 sym = ns->entries->sym->result;
826 /* All result types the same. */
827 proc->ts = *fts;
828 if (sym->attr.dimension)
829 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
830 if (sym->attr.pointer)
831 gfc_add_pointer (&proc->attr, NULL);
832 }
833 else
834 {
835 /* Otherwise the result will be passed through a union by
836 reference. */
837 proc->attr.mixed_entry_master = 1;
838 for (el = ns->entries; el; el = el->next)
839 {
840 sym = el->sym->result;
841 if (sym->attr.dimension)
842 {
843 if (el == ns->entries)
844 gfc_error ("FUNCTION result %s cannot be an array in "
845 "FUNCTION %s at %L", sym->name,
846 ns->entries->sym->name, &sym->declared_at);
847 else
848 gfc_error ("ENTRY result %s cannot be an array in "
849 "FUNCTION %s at %L", sym->name,
850 ns->entries->sym->name, &sym->declared_at);
851 }
852 else if (sym->attr.pointer)
853 {
854 if (el == ns->entries)
855 gfc_error ("FUNCTION result %s cannot be a POINTER in "
856 "FUNCTION %s at %L", sym->name,
857 ns->entries->sym->name, &sym->declared_at);
858 else
859 gfc_error ("ENTRY result %s cannot be a POINTER in "
860 "FUNCTION %s at %L", sym->name,
861 ns->entries->sym->name, &sym->declared_at);
862 }
863 else
864 {
865 ts = &sym->ts;
866 if (ts->type == BT_UNKNOWN)
867 ts = gfc_get_default_type (sym->name, NULL);
868 switch (ts->type)
869 {
870 case BT_INTEGER:
871 if (ts->kind == gfc_default_integer_kind)
872 sym = NULL;
873 break;
874 case BT_REAL:
875 if (ts->kind == gfc_default_real_kind
876 || ts->kind == gfc_default_double_kind)
877 sym = NULL;
878 break;
879 case BT_COMPLEX:
880 if (ts->kind == gfc_default_complex_kind)
881 sym = NULL;
882 break;
883 case BT_LOGICAL:
884 if (ts->kind == gfc_default_logical_kind)
885 sym = NULL;
886 break;
887 case BT_UNKNOWN:
888 /* We will issue error elsewhere. */
889 sym = NULL;
890 break;
891 default:
892 break;
893 }
894 if (sym)
895 {
896 if (el == ns->entries)
897 gfc_error ("FUNCTION result %s cannot be of type %s "
898 "in FUNCTION %s at %L", sym->name,
899 gfc_typename (ts), ns->entries->sym->name,
900 &sym->declared_at);
901 else
902 gfc_error ("ENTRY result %s cannot be of type %s "
903 "in FUNCTION %s at %L", sym->name,
904 gfc_typename (ts), ns->entries->sym->name,
905 &sym->declared_at);
906 }
907 }
908 }
909 }
910 }
911 proc->attr.access = ACCESS_PRIVATE;
912 proc->attr.entry_master = 1;
913
914 /* Merge all the entry point arguments. */
915 for (el = ns->entries; el; el = el->next)
916 merge_argument_lists (proc, el->sym->formal);
917
918 /* Check the master formal arguments for any that are not
919 present in all entry points. */
920 for (el = ns->entries; el; el = el->next)
921 check_argument_lists (proc, el->sym->formal);
922
923 /* Use the master function for the function body. */
924 ns->proc_name = proc;
925
926 /* Finalize the new symbols. */
927 gfc_commit_symbols ();
928
929 /* Restore the original namespace. */
930 gfc_current_ns = old_ns;
931 }
932
933
934 /* Resolve common variables. */
935 static void
936 resolve_common_vars (gfc_common_head *common_block, bool named_common)
937 {
938 gfc_symbol *csym = common_block->head;
939 gfc_gsymbol *gsym;
940
941 for (; csym; csym = csym->common_next)
942 {
943 gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
944 if (gsym && (gsym->type == GSYM_MODULE || gsym->type == GSYM_PROGRAM))
945 gfc_error_now ("Global entity %qs at %L cannot appear in a "
946 "COMMON block at %L", gsym->name,
947 &gsym->where, &csym->common_block->where);
948
949 /* gfc_add_in_common may have been called before, but the reported errors
950 have been ignored to continue parsing.
951 We do the checks again here. */
952 if (!csym->attr.use_assoc)
953 {
954 gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
955 gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
956 &common_block->where);
957 }
958
959 if (csym->value || csym->attr.data)
960 {
961 if (!csym->ns->is_block_data)
962 gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
963 "but only in BLOCK DATA initialization is "
964 "allowed", csym->name, &csym->declared_at);
965 else if (!named_common)
966 gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
967 "in a blank COMMON but initialization is only "
968 "allowed in named common blocks", csym->name,
969 &csym->declared_at);
970 }
971
972 if (UNLIMITED_POLY (csym))
973 gfc_error_now ("%qs in cannot appear in COMMON at %L "
974 "[F2008:C5100]", csym->name, &csym->declared_at);
975
976 if (csym->ts.type != BT_DERIVED)
977 continue;
978
979 if (!(csym->ts.u.derived->attr.sequence
980 || csym->ts.u.derived->attr.is_bind_c))
981 gfc_error_now ("Derived type variable %qs in COMMON at %L "
982 "has neither the SEQUENCE nor the BIND(C) "
983 "attribute", csym->name, &csym->declared_at);
984 if (csym->ts.u.derived->attr.alloc_comp)
985 gfc_error_now ("Derived type variable %qs in COMMON at %L "
986 "has an ultimate component that is "
987 "allocatable", csym->name, &csym->declared_at);
988 if (gfc_has_default_initializer (csym->ts.u.derived))
989 gfc_error_now ("Derived type variable %qs in COMMON at %L "
990 "may not have default initializer", csym->name,
991 &csym->declared_at);
992
993 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
994 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
995 }
996 }
997
998 /* Resolve common blocks. */
999 static void
1000 resolve_common_blocks (gfc_symtree *common_root)
1001 {
1002 gfc_symbol *sym;
1003 gfc_gsymbol * gsym;
1004
1005 if (common_root == NULL)
1006 return;
1007
1008 if (common_root->left)
1009 resolve_common_blocks (common_root->left);
1010 if (common_root->right)
1011 resolve_common_blocks (common_root->right);
1012
1013 resolve_common_vars (common_root->n.common, true);
1014
1015 /* The common name is a global name - in Fortran 2003 also if it has a
1016 C binding name, since Fortran 2008 only the C binding name is a global
1017 identifier. */
1018 if (!common_root->n.common->binding_label
1019 || gfc_notification_std (GFC_STD_F2008))
1020 {
1021 gsym = gfc_find_gsymbol (gfc_gsym_root,
1022 common_root->n.common->name);
1023
1024 if (gsym && gfc_notification_std (GFC_STD_F2008)
1025 && gsym->type == GSYM_COMMON
1026 && ((common_root->n.common->binding_label
1027 && (!gsym->binding_label
1028 || strcmp (common_root->n.common->binding_label,
1029 gsym->binding_label) != 0))
1030 || (!common_root->n.common->binding_label
1031 && gsym->binding_label)))
1032 {
1033 gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
1034 "identifier and must thus have the same binding name "
1035 "as the same-named COMMON block at %L: %s vs %s",
1036 common_root->n.common->name, &common_root->n.common->where,
1037 &gsym->where,
1038 common_root->n.common->binding_label
1039 ? common_root->n.common->binding_label : "(blank)",
1040 gsym->binding_label ? gsym->binding_label : "(blank)");
1041 return;
1042 }
1043
1044 if (gsym && gsym->type != GSYM_COMMON
1045 && !common_root->n.common->binding_label)
1046 {
1047 gfc_error ("COMMON block %qs at %L uses the same global identifier "
1048 "as entity at %L",
1049 common_root->n.common->name, &common_root->n.common->where,
1050 &gsym->where);
1051 return;
1052 }
1053 if (gsym && gsym->type != GSYM_COMMON)
1054 {
1055 gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
1056 "%L sharing the identifier with global non-COMMON-block "
1057 "entity at %L", common_root->n.common->name,
1058 &common_root->n.common->where, &gsym->where);
1059 return;
1060 }
1061 if (!gsym)
1062 {
1063 gsym = gfc_get_gsymbol (common_root->n.common->name, false);
1064 gsym->type = GSYM_COMMON;
1065 gsym->where = common_root->n.common->where;
1066 gsym->defined = 1;
1067 }
1068 gsym->used = 1;
1069 }
1070
1071 if (common_root->n.common->binding_label)
1072 {
1073 gsym = gfc_find_gsymbol (gfc_gsym_root,
1074 common_root->n.common->binding_label);
1075 if (gsym && gsym->type != GSYM_COMMON)
1076 {
1077 gfc_error ("COMMON block at %L with binding label %qs uses the same "
1078 "global identifier as entity at %L",
1079 &common_root->n.common->where,
1080 common_root->n.common->binding_label, &gsym->where);
1081 return;
1082 }
1083 if (!gsym)
1084 {
1085 gsym = gfc_get_gsymbol (common_root->n.common->binding_label, true);
1086 gsym->type = GSYM_COMMON;
1087 gsym->where = common_root->n.common->where;
1088 gsym->defined = 1;
1089 }
1090 gsym->used = 1;
1091 }
1092
1093 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1094 if (sym == NULL)
1095 return;
1096
1097 if (sym->attr.flavor == FL_PARAMETER)
1098 gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
1099 sym->name, &common_root->n.common->where, &sym->declared_at);
1100
1101 if (sym->attr.external)
1102 gfc_error ("COMMON block %qs at %L cannot have the EXTERNAL attribute",
1103 sym->name, &common_root->n.common->where);
1104
1105 if (sym->attr.intrinsic)
1106 gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
1107 sym->name, &common_root->n.common->where);
1108 else if (sym->attr.result
1109 || gfc_is_function_return_value (sym, gfc_current_ns))
1110 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1111 "that is also a function result", sym->name,
1112 &common_root->n.common->where);
1113 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1114 && sym->attr.proc != PROC_ST_FUNCTION)
1115 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1116 "that is also a global procedure", sym->name,
1117 &common_root->n.common->where);
1118 }
1119
1120
1121 /* Resolve contained function types. Because contained functions can call one
1122 another, they have to be worked out before any of the contained procedures
1123 can be resolved.
1124
1125 The good news is that if a function doesn't already have a type, the only
1126 way it can get one is through an IMPLICIT type or a RESULT variable, because
1127 by definition contained functions are contained namespace they're contained
1128 in, not in a sibling or parent namespace. */
1129
1130 static void
1131 resolve_contained_functions (gfc_namespace *ns)
1132 {
1133 gfc_namespace *child;
1134 gfc_entry_list *el;
1135
1136 resolve_formal_arglists (ns);
1137
1138 for (child = ns->contained; child; child = child->sibling)
1139 {
1140 /* Resolve alternate entry points first. */
1141 resolve_entries (child);
1142
1143 /* Then check function return types. */
1144 resolve_contained_fntype (child->proc_name, child);
1145 for (el = child->entries; el; el = el->next)
1146 resolve_contained_fntype (el->sym, child);
1147 }
1148 }
1149
1150
1151
1152 /* A Parameterized Derived Type constructor must contain values for
1153 the PDT KIND parameters or they must have a default initializer.
1154 Go through the constructor picking out the KIND expressions,
1155 storing them in 'param_list' and then call gfc_get_pdt_instance
1156 to obtain the PDT instance. */
1157
1158 static gfc_actual_arglist *param_list, *param_tail, *param;
1159
1160 static bool
1161 get_pdt_spec_expr (gfc_component *c, gfc_expr *expr)
1162 {
1163 param = gfc_get_actual_arglist ();
1164 if (!param_list)
1165 param_list = param_tail = param;
1166 else
1167 {
1168 param_tail->next = param;
1169 param_tail = param_tail->next;
1170 }
1171
1172 param_tail->name = c->name;
1173 if (expr)
1174 param_tail->expr = gfc_copy_expr (expr);
1175 else if (c->initializer)
1176 param_tail->expr = gfc_copy_expr (c->initializer);
1177 else
1178 {
1179 param_tail->spec_type = SPEC_ASSUMED;
1180 if (c->attr.pdt_kind)
1181 {
1182 gfc_error ("The KIND parameter %qs in the PDT constructor "
1183 "at %C has no value", param->name);
1184 return false;
1185 }
1186 }
1187
1188 return true;
1189 }
1190
1191 static bool
1192 get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr,
1193 gfc_symbol *derived)
1194 {
1195 gfc_constructor *cons = NULL;
1196 gfc_component *comp;
1197 bool t = true;
1198
1199 if (expr && expr->expr_type == EXPR_STRUCTURE)
1200 cons = gfc_constructor_first (expr->value.constructor);
1201 else if (constr)
1202 cons = *constr;
1203 gcc_assert (cons);
1204
1205 comp = derived->components;
1206
1207 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1208 {
1209 if (cons->expr
1210 && cons->expr->expr_type == EXPR_STRUCTURE
1211 && comp->ts.type == BT_DERIVED)
1212 {
1213 t = get_pdt_constructor (cons->expr, NULL, comp->ts.u.derived);
1214 if (!t)
1215 return t;
1216 }
1217 else if (comp->ts.type == BT_DERIVED)
1218 {
1219 t = get_pdt_constructor (NULL, &cons, comp->ts.u.derived);
1220 if (!t)
1221 return t;
1222 }
1223 else if ((comp->attr.pdt_kind || comp->attr.pdt_len)
1224 && derived->attr.pdt_template)
1225 {
1226 t = get_pdt_spec_expr (comp, cons->expr);
1227 if (!t)
1228 return t;
1229 }
1230 }
1231 return t;
1232 }
1233
1234
1235 static bool resolve_fl_derived0 (gfc_symbol *sym);
1236 static bool resolve_fl_struct (gfc_symbol *sym);
1237
1238
1239 /* Resolve all of the elements of a structure constructor and make sure that
1240 the types are correct. The 'init' flag indicates that the given
1241 constructor is an initializer. */
1242
1243 static bool
1244 resolve_structure_cons (gfc_expr *expr, int init)
1245 {
1246 gfc_constructor *cons;
1247 gfc_component *comp;
1248 bool t;
1249 symbol_attribute a;
1250
1251 t = true;
1252
1253 if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
1254 {
1255 if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
1256 resolve_fl_derived0 (expr->ts.u.derived);
1257 else
1258 resolve_fl_struct (expr->ts.u.derived);
1259
1260 /* If this is a Parameterized Derived Type template, find the
1261 instance corresponding to the PDT kind parameters. */
1262 if (expr->ts.u.derived->attr.pdt_template)
1263 {
1264 param_list = NULL;
1265 t = get_pdt_constructor (expr, NULL, expr->ts.u.derived);
1266 if (!t)
1267 return t;
1268 gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL);
1269
1270 expr->param_list = gfc_copy_actual_arglist (param_list);
1271
1272 if (param_list)
1273 gfc_free_actual_arglist (param_list);
1274
1275 if (!expr->ts.u.derived->attr.pdt_type)
1276 return false;
1277 }
1278 }
1279
1280 cons = gfc_constructor_first (expr->value.constructor);
1281
1282 /* A constructor may have references if it is the result of substituting a
1283 parameter variable. In this case we just pull out the component we
1284 want. */
1285 if (expr->ref)
1286 comp = expr->ref->u.c.sym->components;
1287 else
1288 comp = expr->ts.u.derived->components;
1289
1290 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1291 {
1292 int rank;
1293
1294 if (!cons->expr)
1295 continue;
1296
1297 /* Unions use an EXPR_NULL contrived expression to tell the translation
1298 phase to generate an initializer of the appropriate length.
1299 Ignore it here. */
1300 if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
1301 continue;
1302
1303 if (!gfc_resolve_expr (cons->expr))
1304 {
1305 t = false;
1306 continue;
1307 }
1308
1309 rank = comp->as ? comp->as->rank : 0;
1310 if (comp->ts.type == BT_CLASS
1311 && !comp->ts.u.derived->attr.unlimited_polymorphic
1312 && CLASS_DATA (comp)->as)
1313 rank = CLASS_DATA (comp)->as->rank;
1314
1315 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1316 && (comp->attr.allocatable || cons->expr->rank))
1317 {
1318 gfc_error ("The rank of the element in the structure "
1319 "constructor at %L does not match that of the "
1320 "component (%d/%d)", &cons->expr->where,
1321 cons->expr->rank, rank);
1322 t = false;
1323 }
1324
1325 /* If we don't have the right type, try to convert it. */
1326
1327 if (!comp->attr.proc_pointer &&
1328 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1329 {
1330 if (strcmp (comp->name, "_extends") == 0)
1331 {
1332 /* Can afford to be brutal with the _extends initializer.
1333 The derived type can get lost because it is PRIVATE
1334 but it is not usage constrained by the standard. */
1335 cons->expr->ts = comp->ts;
1336 }
1337 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1338 {
1339 gfc_error ("The element in the structure constructor at %L, "
1340 "for pointer component %qs, is %s but should be %s",
1341 &cons->expr->where, comp->name,
1342 gfc_basic_typename (cons->expr->ts.type),
1343 gfc_basic_typename (comp->ts.type));
1344 t = false;
1345 }
1346 else
1347 {
1348 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1349 if (t)
1350 t = t2;
1351 }
1352 }
1353
1354 /* For strings, the length of the constructor should be the same as
1355 the one of the structure, ensure this if the lengths are known at
1356 compile time and when we are dealing with PARAMETER or structure
1357 constructors. */
1358 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1359 && comp->ts.u.cl->length
1360 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1361 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1362 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1363 && cons->expr->rank != 0
1364 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1365 comp->ts.u.cl->length->value.integer) != 0)
1366 {
1367 if (cons->expr->expr_type == EXPR_VARIABLE
1368 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1369 {
1370 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1371 to make use of the gfc_resolve_character_array_constructor
1372 machinery. The expression is later simplified away to
1373 an array of string literals. */
1374 gfc_expr *para = cons->expr;
1375 cons->expr = gfc_get_expr ();
1376 cons->expr->ts = para->ts;
1377 cons->expr->where = para->where;
1378 cons->expr->expr_type = EXPR_ARRAY;
1379 cons->expr->rank = para->rank;
1380 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1381 gfc_constructor_append_expr (&cons->expr->value.constructor,
1382 para, &cons->expr->where);
1383 }
1384
1385 if (cons->expr->expr_type == EXPR_ARRAY)
1386 {
1387 /* Rely on the cleanup of the namespace to deal correctly with
1388 the old charlen. (There was a block here that attempted to
1389 remove the charlen but broke the chain in so doing.) */
1390 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1391 cons->expr->ts.u.cl->length_from_typespec = true;
1392 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1393 gfc_resolve_character_array_constructor (cons->expr);
1394 }
1395 }
1396
1397 if (cons->expr->expr_type == EXPR_NULL
1398 && !(comp->attr.pointer || comp->attr.allocatable
1399 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1400 || (comp->ts.type == BT_CLASS
1401 && (CLASS_DATA (comp)->attr.class_pointer
1402 || CLASS_DATA (comp)->attr.allocatable))))
1403 {
1404 t = false;
1405 gfc_error ("The NULL in the structure constructor at %L is "
1406 "being applied to component %qs, which is neither "
1407 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1408 comp->name);
1409 }
1410
1411 if (comp->attr.proc_pointer && comp->ts.interface)
1412 {
1413 /* Check procedure pointer interface. */
1414 gfc_symbol *s2 = NULL;
1415 gfc_component *c2;
1416 const char *name;
1417 char err[200];
1418
1419 c2 = gfc_get_proc_ptr_comp (cons->expr);
1420 if (c2)
1421 {
1422 s2 = c2->ts.interface;
1423 name = c2->name;
1424 }
1425 else if (cons->expr->expr_type == EXPR_FUNCTION)
1426 {
1427 s2 = cons->expr->symtree->n.sym->result;
1428 name = cons->expr->symtree->n.sym->result->name;
1429 }
1430 else if (cons->expr->expr_type != EXPR_NULL)
1431 {
1432 s2 = cons->expr->symtree->n.sym;
1433 name = cons->expr->symtree->n.sym->name;
1434 }
1435
1436 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1437 err, sizeof (err), NULL, NULL))
1438 {
1439 gfc_error_opt (0, "Interface mismatch for procedure-pointer "
1440 "component %qs in structure constructor at %L:"
1441 " %s", comp->name, &cons->expr->where, err);
1442 return false;
1443 }
1444 }
1445
1446 if (!comp->attr.pointer || comp->attr.proc_pointer
1447 || cons->expr->expr_type == EXPR_NULL)
1448 continue;
1449
1450 a = gfc_expr_attr (cons->expr);
1451
1452 if (!a.pointer && !a.target)
1453 {
1454 t = false;
1455 gfc_error ("The element in the structure constructor at %L, "
1456 "for pointer component %qs should be a POINTER or "
1457 "a TARGET", &cons->expr->where, comp->name);
1458 }
1459
1460 if (init)
1461 {
1462 /* F08:C461. Additional checks for pointer initialization. */
1463 if (a.allocatable)
1464 {
1465 t = false;
1466 gfc_error ("Pointer initialization target at %L "
1467 "must not be ALLOCATABLE", &cons->expr->where);
1468 }
1469 if (!a.save)
1470 {
1471 t = false;
1472 gfc_error ("Pointer initialization target at %L "
1473 "must have the SAVE attribute", &cons->expr->where);
1474 }
1475 }
1476
1477 /* F2003, C1272 (3). */
1478 bool impure = cons->expr->expr_type == EXPR_VARIABLE
1479 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1480 || gfc_is_coindexed (cons->expr));
1481 if (impure && gfc_pure (NULL))
1482 {
1483 t = false;
1484 gfc_error ("Invalid expression in the structure constructor for "
1485 "pointer component %qs at %L in PURE procedure",
1486 comp->name, &cons->expr->where);
1487 }
1488
1489 if (impure)
1490 gfc_unset_implicit_pure (NULL);
1491 }
1492
1493 return t;
1494 }
1495
1496
1497 /****************** Expression name resolution ******************/
1498
1499 /* Returns 0 if a symbol was not declared with a type or
1500 attribute declaration statement, nonzero otherwise. */
1501
1502 static int
1503 was_declared (gfc_symbol *sym)
1504 {
1505 symbol_attribute a;
1506
1507 a = sym->attr;
1508
1509 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1510 return 1;
1511
1512 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1513 || a.optional || a.pointer || a.save || a.target || a.volatile_
1514 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1515 || a.asynchronous || a.codimension)
1516 return 1;
1517
1518 return 0;
1519 }
1520
1521
1522 /* Determine if a symbol is generic or not. */
1523
1524 static int
1525 generic_sym (gfc_symbol *sym)
1526 {
1527 gfc_symbol *s;
1528
1529 if (sym->attr.generic ||
1530 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1531 return 1;
1532
1533 if (was_declared (sym) || sym->ns->parent == NULL)
1534 return 0;
1535
1536 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1537
1538 if (s != NULL)
1539 {
1540 if (s == sym)
1541 return 0;
1542 else
1543 return generic_sym (s);
1544 }
1545
1546 return 0;
1547 }
1548
1549
1550 /* Determine if a symbol is specific or not. */
1551
1552 static int
1553 specific_sym (gfc_symbol *sym)
1554 {
1555 gfc_symbol *s;
1556
1557 if (sym->attr.if_source == IFSRC_IFBODY
1558 || sym->attr.proc == PROC_MODULE
1559 || sym->attr.proc == PROC_INTERNAL
1560 || sym->attr.proc == PROC_ST_FUNCTION
1561 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1562 || sym->attr.external)
1563 return 1;
1564
1565 if (was_declared (sym) || sym->ns->parent == NULL)
1566 return 0;
1567
1568 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1569
1570 return (s == NULL) ? 0 : specific_sym (s);
1571 }
1572
1573
1574 /* Figure out if the procedure is specific, generic or unknown. */
1575
1576 enum proc_type
1577 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
1578
1579 static proc_type
1580 procedure_kind (gfc_symbol *sym)
1581 {
1582 if (generic_sym (sym))
1583 return PTYPE_GENERIC;
1584
1585 if (specific_sym (sym))
1586 return PTYPE_SPECIFIC;
1587
1588 return PTYPE_UNKNOWN;
1589 }
1590
1591 /* Check references to assumed size arrays. The flag need_full_assumed_size
1592 is nonzero when matching actual arguments. */
1593
1594 static int need_full_assumed_size = 0;
1595
1596 static bool
1597 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1598 {
1599 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1600 return false;
1601
1602 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1603 What should it be? */
1604 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1605 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1606 && (e->ref->u.ar.type == AR_FULL))
1607 {
1608 gfc_error ("The upper bound in the last dimension must "
1609 "appear in the reference to the assumed size "
1610 "array %qs at %L", sym->name, &e->where);
1611 return true;
1612 }
1613 return false;
1614 }
1615
1616
1617 /* Look for bad assumed size array references in argument expressions
1618 of elemental and array valued intrinsic procedures. Since this is
1619 called from procedure resolution functions, it only recurses at
1620 operators. */
1621
1622 static bool
1623 resolve_assumed_size_actual (gfc_expr *e)
1624 {
1625 if (e == NULL)
1626 return false;
1627
1628 switch (e->expr_type)
1629 {
1630 case EXPR_VARIABLE:
1631 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1632 return true;
1633 break;
1634
1635 case EXPR_OP:
1636 if (resolve_assumed_size_actual (e->value.op.op1)
1637 || resolve_assumed_size_actual (e->value.op.op2))
1638 return true;
1639 break;
1640
1641 default:
1642 break;
1643 }
1644 return false;
1645 }
1646
1647
1648 /* Check a generic procedure, passed as an actual argument, to see if
1649 there is a matching specific name. If none, it is an error, and if
1650 more than one, the reference is ambiguous. */
1651 static int
1652 count_specific_procs (gfc_expr *e)
1653 {
1654 int n;
1655 gfc_interface *p;
1656 gfc_symbol *sym;
1657
1658 n = 0;
1659 sym = e->symtree->n.sym;
1660
1661 for (p = sym->generic; p; p = p->next)
1662 if (strcmp (sym->name, p->sym->name) == 0)
1663 {
1664 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1665 sym->name);
1666 n++;
1667 }
1668
1669 if (n > 1)
1670 gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
1671 &e->where);
1672
1673 if (n == 0)
1674 gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1675 "argument at %L", sym->name, &e->where);
1676
1677 return n;
1678 }
1679
1680
1681 /* See if a call to sym could possibly be a not allowed RECURSION because of
1682 a missing RECURSIVE declaration. This means that either sym is the current
1683 context itself, or sym is the parent of a contained procedure calling its
1684 non-RECURSIVE containing procedure.
1685 This also works if sym is an ENTRY. */
1686
1687 static bool
1688 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1689 {
1690 gfc_symbol* proc_sym;
1691 gfc_symbol* context_proc;
1692 gfc_namespace* real_context;
1693
1694 if (sym->attr.flavor == FL_PROGRAM
1695 || gfc_fl_struct (sym->attr.flavor))
1696 return false;
1697
1698 /* If we've got an ENTRY, find real procedure. */
1699 if (sym->attr.entry && sym->ns->entries)
1700 proc_sym = sym->ns->entries->sym;
1701 else
1702 proc_sym = sym;
1703
1704 /* If sym is RECURSIVE, all is well of course. */
1705 if (proc_sym->attr.recursive || flag_recursive)
1706 return false;
1707
1708 /* Find the context procedure's "real" symbol if it has entries.
1709 We look for a procedure symbol, so recurse on the parents if we don't
1710 find one (like in case of a BLOCK construct). */
1711 for (real_context = context; ; real_context = real_context->parent)
1712 {
1713 /* We should find something, eventually! */
1714 gcc_assert (real_context);
1715
1716 context_proc = (real_context->entries ? real_context->entries->sym
1717 : real_context->proc_name);
1718
1719 /* In some special cases, there may not be a proc_name, like for this
1720 invalid code:
1721 real(bad_kind()) function foo () ...
1722 when checking the call to bad_kind ().
1723 In these cases, we simply return here and assume that the
1724 call is ok. */
1725 if (!context_proc)
1726 return false;
1727
1728 if (context_proc->attr.flavor != FL_LABEL)
1729 break;
1730 }
1731
1732 /* A call from sym's body to itself is recursion, of course. */
1733 if (context_proc == proc_sym)
1734 return true;
1735
1736 /* The same is true if context is a contained procedure and sym the
1737 containing one. */
1738 if (context_proc->attr.contained)
1739 {
1740 gfc_symbol* parent_proc;
1741
1742 gcc_assert (context->parent);
1743 parent_proc = (context->parent->entries ? context->parent->entries->sym
1744 : context->parent->proc_name);
1745
1746 if (parent_proc == proc_sym)
1747 return true;
1748 }
1749
1750 return false;
1751 }
1752
1753
1754 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1755 its typespec and formal argument list. */
1756
1757 bool
1758 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1759 {
1760 gfc_intrinsic_sym* isym = NULL;
1761 const char* symstd;
1762
1763 if (sym->resolve_symbol_called >= 2)
1764 return true;
1765
1766 sym->resolve_symbol_called = 2;
1767
1768 /* Already resolved. */
1769 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1770 return true;
1771
1772 /* We already know this one is an intrinsic, so we don't call
1773 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1774 gfc_find_subroutine directly to check whether it is a function or
1775 subroutine. */
1776
1777 if (sym->intmod_sym_id && sym->attr.subroutine)
1778 {
1779 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1780 isym = gfc_intrinsic_subroutine_by_id (id);
1781 }
1782 else if (sym->intmod_sym_id)
1783 {
1784 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1785 isym = gfc_intrinsic_function_by_id (id);
1786 }
1787 else if (!sym->attr.subroutine)
1788 isym = gfc_find_function (sym->name);
1789
1790 if (isym && !sym->attr.subroutine)
1791 {
1792 if (sym->ts.type != BT_UNKNOWN && warn_surprising
1793 && !sym->attr.implicit_type)
1794 gfc_warning (OPT_Wsurprising,
1795 "Type specified for intrinsic function %qs at %L is"
1796 " ignored", sym->name, &sym->declared_at);
1797
1798 if (!sym->attr.function &&
1799 !gfc_add_function(&sym->attr, sym->name, loc))
1800 return false;
1801
1802 sym->ts = isym->ts;
1803 }
1804 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1805 {
1806 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1807 {
1808 gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1809 " specifier", sym->name, &sym->declared_at);
1810 return false;
1811 }
1812
1813 if (!sym->attr.subroutine &&
1814 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1815 return false;
1816 }
1817 else
1818 {
1819 gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
1820 &sym->declared_at);
1821 return false;
1822 }
1823
1824 gfc_copy_formal_args_intr (sym, isym, NULL);
1825
1826 sym->attr.pure = isym->pure;
1827 sym->attr.elemental = isym->elemental;
1828
1829 /* Check it is actually available in the standard settings. */
1830 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1831 {
1832 gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1833 "available in the current standard settings but %s. Use "
1834 "an appropriate %<-std=*%> option or enable "
1835 "%<-fall-intrinsics%> in order to use it.",
1836 sym->name, &sym->declared_at, symstd);
1837 return false;
1838 }
1839
1840 return true;
1841 }
1842
1843
1844 /* Resolve a procedure expression, like passing it to a called procedure or as
1845 RHS for a procedure pointer assignment. */
1846
1847 static bool
1848 resolve_procedure_expression (gfc_expr* expr)
1849 {
1850 gfc_symbol* sym;
1851
1852 if (expr->expr_type != EXPR_VARIABLE)
1853 return true;
1854 gcc_assert (expr->symtree);
1855
1856 sym = expr->symtree->n.sym;
1857
1858 if (sym->attr.intrinsic)
1859 gfc_resolve_intrinsic (sym, &expr->where);
1860
1861 if (sym->attr.flavor != FL_PROCEDURE
1862 || (sym->attr.function && sym->result == sym))
1863 return true;
1864
1865 /* A non-RECURSIVE procedure that is used as procedure expression within its
1866 own body is in danger of being called recursively. */
1867 if (is_illegal_recursion (sym, gfc_current_ns))
1868 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
1869 " itself recursively. Declare it RECURSIVE or use"
1870 " %<-frecursive%>", sym->name, &expr->where);
1871
1872 return true;
1873 }
1874
1875
1876 /* Check that name is not a derived type. */
1877
1878 static bool
1879 is_dt_name (const char *name)
1880 {
1881 gfc_symbol *dt_list, *dt_first;
1882
1883 dt_list = dt_first = gfc_derived_types;
1884 for (; dt_list; dt_list = dt_list->dt_next)
1885 {
1886 if (strcmp(dt_list->name, name) == 0)
1887 return true;
1888 if (dt_first == dt_list->dt_next)
1889 break;
1890 }
1891 return false;
1892 }
1893
1894
1895 /* Resolve an actual argument list. Most of the time, this is just
1896 resolving the expressions in the list.
1897 The exception is that we sometimes have to decide whether arguments
1898 that look like procedure arguments are really simple variable
1899 references. */
1900
1901 static bool
1902 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1903 bool no_formal_args)
1904 {
1905 gfc_symbol *sym;
1906 gfc_symtree *parent_st;
1907 gfc_expr *e;
1908 gfc_component *comp;
1909 int save_need_full_assumed_size;
1910 bool return_value = false;
1911 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1912
1913 actual_arg = true;
1914 first_actual_arg = true;
1915
1916 for (; arg; arg = arg->next)
1917 {
1918 e = arg->expr;
1919 if (e == NULL)
1920 {
1921 /* Check the label is a valid branching target. */
1922 if (arg->label)
1923 {
1924 if (arg->label->defined == ST_LABEL_UNKNOWN)
1925 {
1926 gfc_error ("Label %d referenced at %L is never defined",
1927 arg->label->value, &arg->label->where);
1928 goto cleanup;
1929 }
1930 }
1931 first_actual_arg = false;
1932 continue;
1933 }
1934
1935 if (e->expr_type == EXPR_VARIABLE
1936 && e->symtree->n.sym->attr.generic
1937 && no_formal_args
1938 && count_specific_procs (e) != 1)
1939 goto cleanup;
1940
1941 if (e->ts.type != BT_PROCEDURE)
1942 {
1943 save_need_full_assumed_size = need_full_assumed_size;
1944 if (e->expr_type != EXPR_VARIABLE)
1945 need_full_assumed_size = 0;
1946 if (!gfc_resolve_expr (e))
1947 goto cleanup;
1948 need_full_assumed_size = save_need_full_assumed_size;
1949 goto argument_list;
1950 }
1951
1952 /* See if the expression node should really be a variable reference. */
1953
1954 sym = e->symtree->n.sym;
1955
1956 if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name))
1957 {
1958 gfc_error ("Derived type %qs is used as an actual "
1959 "argument at %L", sym->name, &e->where);
1960 goto cleanup;
1961 }
1962
1963 if (sym->attr.flavor == FL_PROCEDURE
1964 || sym->attr.intrinsic
1965 || sym->attr.external)
1966 {
1967 int actual_ok;
1968
1969 /* If a procedure is not already determined to be something else
1970 check if it is intrinsic. */
1971 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1972 sym->attr.intrinsic = 1;
1973
1974 if (sym->attr.proc == PROC_ST_FUNCTION)
1975 {
1976 gfc_error ("Statement function %qs at %L is not allowed as an "
1977 "actual argument", sym->name, &e->where);
1978 }
1979
1980 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1981 sym->attr.subroutine);
1982 if (sym->attr.intrinsic && actual_ok == 0)
1983 {
1984 gfc_error ("Intrinsic %qs at %L is not allowed as an "
1985 "actual argument", sym->name, &e->where);
1986 }
1987
1988 if (sym->attr.contained && !sym->attr.use_assoc
1989 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1990 {
1991 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
1992 " used as actual argument at %L",
1993 sym->name, &e->where))
1994 goto cleanup;
1995 }
1996
1997 if (sym->attr.elemental && !sym->attr.intrinsic)
1998 {
1999 gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
2000 "allowed as an actual argument at %L", sym->name,
2001 &e->where);
2002 }
2003
2004 /* Check if a generic interface has a specific procedure
2005 with the same name before emitting an error. */
2006 if (sym->attr.generic && count_specific_procs (e) != 1)
2007 goto cleanup;
2008
2009 /* Just in case a specific was found for the expression. */
2010 sym = e->symtree->n.sym;
2011
2012 /* If the symbol is the function that names the current (or
2013 parent) scope, then we really have a variable reference. */
2014
2015 if (gfc_is_function_return_value (sym, sym->ns))
2016 goto got_variable;
2017
2018 /* If all else fails, see if we have a specific intrinsic. */
2019 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
2020 {
2021 gfc_intrinsic_sym *isym;
2022
2023 isym = gfc_find_function (sym->name);
2024 if (isym == NULL || !isym->specific)
2025 {
2026 gfc_error ("Unable to find a specific INTRINSIC procedure "
2027 "for the reference %qs at %L", sym->name,
2028 &e->where);
2029 goto cleanup;
2030 }
2031 sym->ts = isym->ts;
2032 sym->attr.intrinsic = 1;
2033 sym->attr.function = 1;
2034 }
2035
2036 if (!gfc_resolve_expr (e))
2037 goto cleanup;
2038 goto argument_list;
2039 }
2040
2041 /* See if the name is a module procedure in a parent unit. */
2042
2043 if (was_declared (sym) || sym->ns->parent == NULL)
2044 goto got_variable;
2045
2046 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
2047 {
2048 gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
2049 goto cleanup;
2050 }
2051
2052 if (parent_st == NULL)
2053 goto got_variable;
2054
2055 sym = parent_st->n.sym;
2056 e->symtree = parent_st; /* Point to the right thing. */
2057
2058 if (sym->attr.flavor == FL_PROCEDURE
2059 || sym->attr.intrinsic
2060 || sym->attr.external)
2061 {
2062 if (!gfc_resolve_expr (e))
2063 goto cleanup;
2064 goto argument_list;
2065 }
2066
2067 got_variable:
2068 e->expr_type = EXPR_VARIABLE;
2069 e->ts = sym->ts;
2070 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
2071 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2072 && CLASS_DATA (sym)->as))
2073 {
2074 e->rank = sym->ts.type == BT_CLASS
2075 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
2076 e->ref = gfc_get_ref ();
2077 e->ref->type = REF_ARRAY;
2078 e->ref->u.ar.type = AR_FULL;
2079 e->ref->u.ar.as = sym->ts.type == BT_CLASS
2080 ? CLASS_DATA (sym)->as : sym->as;
2081 }
2082
2083 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
2084 primary.c (match_actual_arg). If above code determines that it
2085 is a variable instead, it needs to be resolved as it was not
2086 done at the beginning of this function. */
2087 save_need_full_assumed_size = need_full_assumed_size;
2088 if (e->expr_type != EXPR_VARIABLE)
2089 need_full_assumed_size = 0;
2090 if (!gfc_resolve_expr (e))
2091 goto cleanup;
2092 need_full_assumed_size = save_need_full_assumed_size;
2093
2094 argument_list:
2095 /* Check argument list functions %VAL, %LOC and %REF. There is
2096 nothing to do for %REF. */
2097 if (arg->name && arg->name[0] == '%')
2098 {
2099 if (strcmp ("%VAL", arg->name) == 0)
2100 {
2101 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
2102 {
2103 gfc_error ("By-value argument at %L is not of numeric "
2104 "type", &e->where);
2105 goto cleanup;
2106 }
2107
2108 if (e->rank)
2109 {
2110 gfc_error ("By-value argument at %L cannot be an array or "
2111 "an array section", &e->where);
2112 goto cleanup;
2113 }
2114
2115 /* Intrinsics are still PROC_UNKNOWN here. However,
2116 since same file external procedures are not resolvable
2117 in gfortran, it is a good deal easier to leave them to
2118 intrinsic.c. */
2119 if (ptype != PROC_UNKNOWN
2120 && ptype != PROC_DUMMY
2121 && ptype != PROC_EXTERNAL
2122 && ptype != PROC_MODULE)
2123 {
2124 gfc_error ("By-value argument at %L is not allowed "
2125 "in this context", &e->where);
2126 goto cleanup;
2127 }
2128 }
2129
2130 /* Statement functions have already been excluded above. */
2131 else if (strcmp ("%LOC", arg->name) == 0
2132 && e->ts.type == BT_PROCEDURE)
2133 {
2134 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
2135 {
2136 gfc_error ("Passing internal procedure at %L by location "
2137 "not allowed", &e->where);
2138 goto cleanup;
2139 }
2140 }
2141 }
2142
2143 comp = gfc_get_proc_ptr_comp(e);
2144 if (e->expr_type == EXPR_VARIABLE
2145 && comp && comp->attr.elemental)
2146 {
2147 gfc_error ("ELEMENTAL procedure pointer component %qs is not "
2148 "allowed as an actual argument at %L", comp->name,
2149 &e->where);
2150 }
2151
2152 /* Fortran 2008, C1237. */
2153 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
2154 && gfc_has_ultimate_pointer (e))
2155 {
2156 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2157 "component", &e->where);
2158 goto cleanup;
2159 }
2160
2161 first_actual_arg = false;
2162 }
2163
2164 return_value = true;
2165
2166 cleanup:
2167 actual_arg = actual_arg_sav;
2168 first_actual_arg = first_actual_arg_sav;
2169
2170 return return_value;
2171 }
2172
2173
2174 /* Do the checks of the actual argument list that are specific to elemental
2175 procedures. If called with c == NULL, we have a function, otherwise if
2176 expr == NULL, we have a subroutine. */
2177
2178 static bool
2179 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2180 {
2181 gfc_actual_arglist *arg0;
2182 gfc_actual_arglist *arg;
2183 gfc_symbol *esym = NULL;
2184 gfc_intrinsic_sym *isym = NULL;
2185 gfc_expr *e = NULL;
2186 gfc_intrinsic_arg *iformal = NULL;
2187 gfc_formal_arglist *eformal = NULL;
2188 bool formal_optional = false;
2189 bool set_by_optional = false;
2190 int i;
2191 int rank = 0;
2192
2193 /* Is this an elemental procedure? */
2194 if (expr && expr->value.function.actual != NULL)
2195 {
2196 if (expr->value.function.esym != NULL
2197 && expr->value.function.esym->attr.elemental)
2198 {
2199 arg0 = expr->value.function.actual;
2200 esym = expr->value.function.esym;
2201 }
2202 else if (expr->value.function.isym != NULL
2203 && expr->value.function.isym->elemental)
2204 {
2205 arg0 = expr->value.function.actual;
2206 isym = expr->value.function.isym;
2207 }
2208 else
2209 return true;
2210 }
2211 else if (c && c->ext.actual != NULL)
2212 {
2213 arg0 = c->ext.actual;
2214
2215 if (c->resolved_sym)
2216 esym = c->resolved_sym;
2217 else
2218 esym = c->symtree->n.sym;
2219 gcc_assert (esym);
2220
2221 if (!esym->attr.elemental)
2222 return true;
2223 }
2224 else
2225 return true;
2226
2227 /* The rank of an elemental is the rank of its array argument(s). */
2228 for (arg = arg0; arg; arg = arg->next)
2229 {
2230 if (arg->expr != NULL && arg->expr->rank != 0)
2231 {
2232 rank = arg->expr->rank;
2233 if (arg->expr->expr_type == EXPR_VARIABLE
2234 && arg->expr->symtree->n.sym->attr.optional)
2235 set_by_optional = true;
2236
2237 /* Function specific; set the result rank and shape. */
2238 if (expr)
2239 {
2240 expr->rank = rank;
2241 if (!expr->shape && arg->expr->shape)
2242 {
2243 expr->shape = gfc_get_shape (rank);
2244 for (i = 0; i < rank; i++)
2245 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2246 }
2247 }
2248 break;
2249 }
2250 }
2251
2252 /* If it is an array, it shall not be supplied as an actual argument
2253 to an elemental procedure unless an array of the same rank is supplied
2254 as an actual argument corresponding to a nonoptional dummy argument of
2255 that elemental procedure(12.4.1.5). */
2256 formal_optional = false;
2257 if (isym)
2258 iformal = isym->formal;
2259 else
2260 eformal = esym->formal;
2261
2262 for (arg = arg0; arg; arg = arg->next)
2263 {
2264 if (eformal)
2265 {
2266 if (eformal->sym && eformal->sym->attr.optional)
2267 formal_optional = true;
2268 eformal = eformal->next;
2269 }
2270 else if (isym && iformal)
2271 {
2272 if (iformal->optional)
2273 formal_optional = true;
2274 iformal = iformal->next;
2275 }
2276 else if (isym)
2277 formal_optional = true;
2278
2279 if (pedantic && arg->expr != NULL
2280 && arg->expr->expr_type == EXPR_VARIABLE
2281 && arg->expr->symtree->n.sym->attr.optional
2282 && formal_optional
2283 && arg->expr->rank
2284 && (set_by_optional || arg->expr->rank != rank)
2285 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2286 {
2287 bool t = false;
2288 gfc_actual_arglist *a;
2289
2290 /* Scan the argument list for a non-optional argument with the
2291 same rank as arg. */
2292 for (a = arg0; a; a = a->next)
2293 if (a != arg
2294 && a->expr->rank == arg->expr->rank
2295 && !a->expr->symtree->n.sym->attr.optional)
2296 {
2297 t = true;
2298 break;
2299 }
2300
2301 if (!t)
2302 gfc_warning (OPT_Wpedantic,
2303 "%qs at %L is an array and OPTIONAL; If it is not "
2304 "present, then it cannot be the actual argument of "
2305 "an ELEMENTAL procedure unless there is a non-optional"
2306 " argument with the same rank "
2307 "(Fortran 2018, 15.5.2.12)",
2308 arg->expr->symtree->n.sym->name, &arg->expr->where);
2309 }
2310 }
2311
2312 for (arg = arg0; arg; arg = arg->next)
2313 {
2314 if (arg->expr == NULL || arg->expr->rank == 0)
2315 continue;
2316
2317 /* Being elemental, the last upper bound of an assumed size array
2318 argument must be present. */
2319 if (resolve_assumed_size_actual (arg->expr))
2320 return false;
2321
2322 /* Elemental procedure's array actual arguments must conform. */
2323 if (e != NULL)
2324 {
2325 if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
2326 return false;
2327 }
2328 else
2329 e = arg->expr;
2330 }
2331
2332 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2333 is an array, the intent inout/out variable needs to be also an array. */
2334 if (rank > 0 && esym && expr == NULL)
2335 for (eformal = esym->formal, arg = arg0; arg && eformal;
2336 arg = arg->next, eformal = eformal->next)
2337 if ((eformal->sym->attr.intent == INTENT_OUT
2338 || eformal->sym->attr.intent == INTENT_INOUT)
2339 && arg->expr && arg->expr->rank == 0)
2340 {
2341 gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2342 "ELEMENTAL subroutine %qs is a scalar, but another "
2343 "actual argument is an array", &arg->expr->where,
2344 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2345 : "INOUT", eformal->sym->name, esym->name);
2346 return false;
2347 }
2348 return true;
2349 }
2350
2351
2352 /* This function does the checking of references to global procedures
2353 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2354 77 and 95 standards. It checks for a gsymbol for the name, making
2355 one if it does not already exist. If it already exists, then the
2356 reference being resolved must correspond to the type of gsymbol.
2357 Otherwise, the new symbol is equipped with the attributes of the
2358 reference. The corresponding code that is called in creating
2359 global entities is parse.c.
2360
2361 In addition, for all but -std=legacy, the gsymbols are used to
2362 check the interfaces of external procedures from the same file.
2363 The namespace of the gsymbol is resolved and then, once this is
2364 done the interface is checked. */
2365
2366
2367 static bool
2368 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2369 {
2370 if (!gsym_ns->proc_name->attr.recursive)
2371 return true;
2372
2373 if (sym->ns == gsym_ns)
2374 return false;
2375
2376 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2377 return false;
2378
2379 return true;
2380 }
2381
2382 static bool
2383 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2384 {
2385 if (gsym_ns->entries)
2386 {
2387 gfc_entry_list *entry = gsym_ns->entries;
2388
2389 for (; entry; entry = entry->next)
2390 {
2391 if (strcmp (sym->name, entry->sym->name) == 0)
2392 {
2393 if (strcmp (gsym_ns->proc_name->name,
2394 sym->ns->proc_name->name) == 0)
2395 return false;
2396
2397 if (sym->ns->parent
2398 && strcmp (gsym_ns->proc_name->name,
2399 sym->ns->parent->proc_name->name) == 0)
2400 return false;
2401 }
2402 }
2403 }
2404 return true;
2405 }
2406
2407
2408 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2409
2410 bool
2411 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2412 {
2413 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2414
2415 for ( ; arg; arg = arg->next)
2416 {
2417 if (!arg->sym)
2418 continue;
2419
2420 if (arg->sym->attr.allocatable) /* (2a) */
2421 {
2422 strncpy (errmsg, _("allocatable argument"), err_len);
2423 return true;
2424 }
2425 else if (arg->sym->attr.asynchronous)
2426 {
2427 strncpy (errmsg, _("asynchronous argument"), err_len);
2428 return true;
2429 }
2430 else if (arg->sym->attr.optional)
2431 {
2432 strncpy (errmsg, _("optional argument"), err_len);
2433 return true;
2434 }
2435 else if (arg->sym->attr.pointer)
2436 {
2437 strncpy (errmsg, _("pointer argument"), err_len);
2438 return true;
2439 }
2440 else if (arg->sym->attr.target)
2441 {
2442 strncpy (errmsg, _("target argument"), err_len);
2443 return true;
2444 }
2445 else if (arg->sym->attr.value)
2446 {
2447 strncpy (errmsg, _("value argument"), err_len);
2448 return true;
2449 }
2450 else if (arg->sym->attr.volatile_)
2451 {
2452 strncpy (errmsg, _("volatile argument"), err_len);
2453 return true;
2454 }
2455 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2456 {
2457 strncpy (errmsg, _("assumed-shape argument"), err_len);
2458 return true;
2459 }
2460 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2461 {
2462 strncpy (errmsg, _("assumed-rank argument"), err_len);
2463 return true;
2464 }
2465 else if (arg->sym->attr.codimension) /* (2c) */
2466 {
2467 strncpy (errmsg, _("coarray argument"), err_len);
2468 return true;
2469 }
2470 else if (false) /* (2d) TODO: parametrized derived type */
2471 {
2472 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2473 return true;
2474 }
2475 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2476 {
2477 strncpy (errmsg, _("polymorphic argument"), err_len);
2478 return true;
2479 }
2480 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2481 {
2482 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2483 return true;
2484 }
2485 else if (arg->sym->ts.type == BT_ASSUMED)
2486 {
2487 /* As assumed-type is unlimited polymorphic (cf. above).
2488 See also TS 29113, Note 6.1. */
2489 strncpy (errmsg, _("assumed-type argument"), err_len);
2490 return true;
2491 }
2492 }
2493
2494 if (sym->attr.function)
2495 {
2496 gfc_symbol *res = sym->result ? sym->result : sym;
2497
2498 if (res->attr.dimension) /* (3a) */
2499 {
2500 strncpy (errmsg, _("array result"), err_len);
2501 return true;
2502 }
2503 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2504 {
2505 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2506 return true;
2507 }
2508 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2509 && res->ts.u.cl->length
2510 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2511 {
2512 strncpy (errmsg, _("result with non-constant character length"), err_len);
2513 return true;
2514 }
2515 }
2516
2517 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2518 {
2519 strncpy (errmsg, _("elemental procedure"), err_len);
2520 return true;
2521 }
2522 else if (sym->attr.is_bind_c) /* (5) */
2523 {
2524 strncpy (errmsg, _("bind(c) procedure"), err_len);
2525 return true;
2526 }
2527
2528 return false;
2529 }
2530
2531
2532 static void
2533 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
2534 {
2535 gfc_gsymbol * gsym;
2536 gfc_namespace *ns;
2537 enum gfc_symbol_type type;
2538 char reason[200];
2539
2540 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2541
2542 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
2543 sym->binding_label != NULL);
2544
2545 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2546 gfc_global_used (gsym, where);
2547
2548 if ((sym->attr.if_source == IFSRC_UNKNOWN
2549 || sym->attr.if_source == IFSRC_IFBODY)
2550 && gsym->type != GSYM_UNKNOWN
2551 && !gsym->binding_label
2552 && gsym->ns
2553 && gsym->ns->proc_name
2554 && not_in_recursive (sym, gsym->ns)
2555 && not_entry_self_reference (sym, gsym->ns))
2556 {
2557 gfc_symbol *def_sym;
2558 def_sym = gsym->ns->proc_name;
2559
2560 if (gsym->ns->resolved != -1)
2561 {
2562
2563 /* Resolve the gsymbol namespace if needed. */
2564 if (!gsym->ns->resolved)
2565 {
2566 gfc_symbol *old_dt_list;
2567
2568 /* Stash away derived types so that the backend_decls
2569 do not get mixed up. */
2570 old_dt_list = gfc_derived_types;
2571 gfc_derived_types = NULL;
2572
2573 gfc_resolve (gsym->ns);
2574
2575 /* Store the new derived types with the global namespace. */
2576 if (gfc_derived_types)
2577 gsym->ns->derived_types = gfc_derived_types;
2578
2579 /* Restore the derived types of this namespace. */
2580 gfc_derived_types = old_dt_list;
2581 }
2582
2583 /* Make sure that translation for the gsymbol occurs before
2584 the procedure currently being resolved. */
2585 ns = gfc_global_ns_list;
2586 for (; ns && ns != gsym->ns; ns = ns->sibling)
2587 {
2588 if (ns->sibling == gsym->ns)
2589 {
2590 ns->sibling = gsym->ns->sibling;
2591 gsym->ns->sibling = gfc_global_ns_list;
2592 gfc_global_ns_list = gsym->ns;
2593 break;
2594 }
2595 }
2596
2597 /* This can happen if a binding name has been specified. */
2598 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2599 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2600
2601 if (def_sym->attr.entry_master || def_sym->attr.entry)
2602 {
2603 gfc_entry_list *entry;
2604 for (entry = gsym->ns->entries; entry; entry = entry->next)
2605 if (strcmp (entry->sym->name, sym->name) == 0)
2606 {
2607 def_sym = entry->sym;
2608 break;
2609 }
2610 }
2611 }
2612
2613 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2614 {
2615 gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2616 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2617 gfc_typename (&def_sym->ts));
2618 goto done;
2619 }
2620
2621 if (sym->attr.if_source == IFSRC_UNKNOWN
2622 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2623 {
2624 gfc_error ("Explicit interface required for %qs at %L: %s",
2625 sym->name, &sym->declared_at, reason);
2626 goto done;
2627 }
2628
2629 bool bad_result_characteristics;
2630 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2631 reason, sizeof(reason), NULL, NULL,
2632 &bad_result_characteristics))
2633 {
2634 /* Turn erros into warnings with -std=gnu and -std=legacy,
2635 unless a function returns a wrong type, which can lead
2636 to all kinds of ICEs and wrong code. */
2637
2638 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
2639 && !bad_result_characteristics)
2640 gfc_errors_to_warnings (true);
2641
2642 gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
2643 sym->name, &sym->declared_at, reason);
2644 sym->error = 1;
2645 gfc_errors_to_warnings (false);
2646 goto done;
2647 }
2648 }
2649
2650 done:
2651
2652 if (gsym->type == GSYM_UNKNOWN)
2653 {
2654 gsym->type = type;
2655 gsym->where = *where;
2656 }
2657
2658 gsym->used = 1;
2659 }
2660
2661
2662 /************* Function resolution *************/
2663
2664 /* Resolve a function call known to be generic.
2665 Section 14.1.2.4.1. */
2666
2667 static match
2668 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2669 {
2670 gfc_symbol *s;
2671
2672 if (sym->attr.generic)
2673 {
2674 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2675 if (s != NULL)
2676 {
2677 expr->value.function.name = s->name;
2678 expr->value.function.esym = s;
2679
2680 if (s->ts.type != BT_UNKNOWN)
2681 expr->ts = s->ts;
2682 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2683 expr->ts = s->result->ts;
2684
2685 if (s->as != NULL)
2686 expr->rank = s->as->rank;
2687 else if (s->result != NULL && s->result->as != NULL)
2688 expr->rank = s->result->as->rank;
2689
2690 gfc_set_sym_referenced (expr->value.function.esym);
2691
2692 return MATCH_YES;
2693 }
2694
2695 /* TODO: Need to search for elemental references in generic
2696 interface. */
2697 }
2698
2699 if (sym->attr.intrinsic)
2700 return gfc_intrinsic_func_interface (expr, 0);
2701
2702 return MATCH_NO;
2703 }
2704
2705
2706 static bool
2707 resolve_generic_f (gfc_expr *expr)
2708 {
2709 gfc_symbol *sym;
2710 match m;
2711 gfc_interface *intr = NULL;
2712
2713 sym = expr->symtree->n.sym;
2714
2715 for (;;)
2716 {
2717 m = resolve_generic_f0 (expr, sym);
2718 if (m == MATCH_YES)
2719 return true;
2720 else if (m == MATCH_ERROR)
2721 return false;
2722
2723 generic:
2724 if (!intr)
2725 for (intr = sym->generic; intr; intr = intr->next)
2726 if (gfc_fl_struct (intr->sym->attr.flavor))
2727 break;
2728
2729 if (sym->ns->parent == NULL)
2730 break;
2731 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2732
2733 if (sym == NULL)
2734 break;
2735 if (!generic_sym (sym))
2736 goto generic;
2737 }
2738
2739 /* Last ditch attempt. See if the reference is to an intrinsic
2740 that possesses a matching interface. 14.1.2.4 */
2741 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2742 {
2743 if (gfc_init_expr_flag)
2744 gfc_error ("Function %qs in initialization expression at %L "
2745 "must be an intrinsic function",
2746 expr->symtree->n.sym->name, &expr->where);
2747 else
2748 gfc_error ("There is no specific function for the generic %qs "
2749 "at %L", expr->symtree->n.sym->name, &expr->where);
2750 return false;
2751 }
2752
2753 if (intr)
2754 {
2755 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2756 NULL, false))
2757 return false;
2758 if (!gfc_use_derived (expr->ts.u.derived))
2759 return false;
2760 return resolve_structure_cons (expr, 0);
2761 }
2762
2763 m = gfc_intrinsic_func_interface (expr, 0);
2764 if (m == MATCH_YES)
2765 return true;
2766
2767 if (m == MATCH_NO)
2768 gfc_error ("Generic function %qs at %L is not consistent with a "
2769 "specific intrinsic interface", expr->symtree->n.sym->name,
2770 &expr->where);
2771
2772 return false;
2773 }
2774
2775
2776 /* Resolve a function call known to be specific. */
2777
2778 static match
2779 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2780 {
2781 match m;
2782
2783 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2784 {
2785 if (sym->attr.dummy)
2786 {
2787 sym->attr.proc = PROC_DUMMY;
2788 goto found;
2789 }
2790
2791 sym->attr.proc = PROC_EXTERNAL;
2792 goto found;
2793 }
2794
2795 if (sym->attr.proc == PROC_MODULE
2796 || sym->attr.proc == PROC_ST_FUNCTION
2797 || sym->attr.proc == PROC_INTERNAL)
2798 goto found;
2799
2800 if (sym->attr.intrinsic)
2801 {
2802 m = gfc_intrinsic_func_interface (expr, 1);
2803 if (m == MATCH_YES)
2804 return MATCH_YES;
2805 if (m == MATCH_NO)
2806 gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
2807 "with an intrinsic", sym->name, &expr->where);
2808
2809 return MATCH_ERROR;
2810 }
2811
2812 return MATCH_NO;
2813
2814 found:
2815 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2816
2817 if (sym->result)
2818 expr->ts = sym->result->ts;
2819 else
2820 expr->ts = sym->ts;
2821 expr->value.function.name = sym->name;
2822 expr->value.function.esym = sym;
2823 /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2824 error(s). */
2825 if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
2826 return MATCH_ERROR;
2827 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2828 expr->rank = CLASS_DATA (sym)->as->rank;
2829 else if (sym->as != NULL)
2830 expr->rank = sym->as->rank;
2831
2832 return MATCH_YES;
2833 }
2834
2835
2836 static bool
2837 resolve_specific_f (gfc_expr *expr)
2838 {
2839 gfc_symbol *sym;
2840 match m;
2841
2842 sym = expr->symtree->n.sym;
2843
2844 for (;;)
2845 {
2846 m = resolve_specific_f0 (sym, expr);
2847 if (m == MATCH_YES)
2848 return true;
2849 if (m == MATCH_ERROR)
2850 return false;
2851
2852 if (sym->ns->parent == NULL)
2853 break;
2854
2855 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2856
2857 if (sym == NULL)
2858 break;
2859 }
2860
2861 gfc_error ("Unable to resolve the specific function %qs at %L",
2862 expr->symtree->n.sym->name, &expr->where);
2863
2864 return true;
2865 }
2866
2867 /* Recursively append candidate SYM to CANDIDATES. Store the number of
2868 candidates in CANDIDATES_LEN. */
2869
2870 static void
2871 lookup_function_fuzzy_find_candidates (gfc_symtree *sym,
2872 char **&candidates,
2873 size_t &candidates_len)
2874 {
2875 gfc_symtree *p;
2876
2877 if (sym == NULL)
2878 return;
2879 if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external)
2880 && sym->n.sym->attr.flavor == FL_PROCEDURE)
2881 vec_push (candidates, candidates_len, sym->name);
2882
2883 p = sym->left;
2884 if (p)
2885 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
2886
2887 p = sym->right;
2888 if (p)
2889 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
2890 }
2891
2892
2893 /* Lookup function FN fuzzily, taking names in SYMROOT into account. */
2894
2895 const char*
2896 gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot)
2897 {
2898 char **candidates = NULL;
2899 size_t candidates_len = 0;
2900 lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len);
2901 return gfc_closest_fuzzy_match (fn, candidates);
2902 }
2903
2904
2905 /* Resolve a procedure call not known to be generic nor specific. */
2906
2907 static bool
2908 resolve_unknown_f (gfc_expr *expr)
2909 {
2910 gfc_symbol *sym;
2911 gfc_typespec *ts;
2912
2913 sym = expr->symtree->n.sym;
2914
2915 if (sym->attr.dummy)
2916 {
2917 sym->attr.proc = PROC_DUMMY;
2918 expr->value.function.name = sym->name;
2919 goto set_type;
2920 }
2921
2922 /* See if we have an intrinsic function reference. */
2923
2924 if (gfc_is_intrinsic (sym, 0, expr->where))
2925 {
2926 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2927 return true;
2928 return false;
2929 }
2930
2931 /* The reference is to an external name. */
2932
2933 sym->attr.proc = PROC_EXTERNAL;
2934 expr->value.function.name = sym->name;
2935 expr->value.function.esym = expr->symtree->n.sym;
2936
2937 if (sym->as != NULL)
2938 expr->rank = sym->as->rank;
2939
2940 /* Type of the expression is either the type of the symbol or the
2941 default type of the symbol. */
2942
2943 set_type:
2944 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2945
2946 if (sym->ts.type != BT_UNKNOWN)
2947 expr->ts = sym->ts;
2948 else
2949 {
2950 ts = gfc_get_default_type (sym->name, sym->ns);
2951
2952 if (ts->type == BT_UNKNOWN)
2953 {
2954 const char *guessed
2955 = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
2956 if (guessed)
2957 gfc_error ("Function %qs at %L has no IMPLICIT type"
2958 "; did you mean %qs?",
2959 sym->name, &expr->where, guessed);
2960 else
2961 gfc_error ("Function %qs at %L has no IMPLICIT type",
2962 sym->name, &expr->where);
2963 return false;
2964 }
2965 else
2966 expr->ts = *ts;
2967 }
2968
2969 return true;
2970 }
2971
2972
2973 /* Return true, if the symbol is an external procedure. */
2974 static bool
2975 is_external_proc (gfc_symbol *sym)
2976 {
2977 if (!sym->attr.dummy && !sym->attr.contained
2978 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
2979 && sym->attr.proc != PROC_ST_FUNCTION
2980 && !sym->attr.proc_pointer
2981 && !sym->attr.use_assoc
2982 && sym->name)
2983 return true;
2984
2985 return false;
2986 }
2987
2988
2989 /* Figure out if a function reference is pure or not. Also set the name
2990 of the function for a potential error message. Return nonzero if the
2991 function is PURE, zero if not. */
2992 static int
2993 pure_stmt_function (gfc_expr *, gfc_symbol *);
2994
2995 int
2996 gfc_pure_function (gfc_expr *e, const char **name)
2997 {
2998 int pure;
2999 gfc_component *comp;
3000
3001 *name = NULL;
3002
3003 if (e->symtree != NULL
3004 && e->symtree->n.sym != NULL
3005 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3006 return pure_stmt_function (e, e->symtree->n.sym);
3007
3008 comp = gfc_get_proc_ptr_comp (e);
3009 if (comp)
3010 {
3011 pure = gfc_pure (comp->ts.interface);
3012 *name = comp->name;
3013 }
3014 else if (e->value.function.esym)
3015 {
3016 pure = gfc_pure (e->value.function.esym);
3017 *name = e->value.function.esym->name;
3018 }
3019 else if (e->value.function.isym)
3020 {
3021 pure = e->value.function.isym->pure
3022 || e->value.function.isym->elemental;
3023 *name = e->value.function.isym->name;
3024 }
3025 else
3026 {
3027 /* Implicit functions are not pure. */
3028 pure = 0;
3029 *name = e->value.function.name;
3030 }
3031
3032 return pure;
3033 }
3034
3035
3036 /* Check if the expression is a reference to an implicitly pure function. */
3037
3038 int
3039 gfc_implicit_pure_function (gfc_expr *e)
3040 {
3041 gfc_component *comp = gfc_get_proc_ptr_comp (e);
3042 if (comp)
3043 return gfc_implicit_pure (comp->ts.interface);
3044 else if (e->value.function.esym)
3045 return gfc_implicit_pure (e->value.function.esym);
3046 else
3047 return 0;
3048 }
3049
3050
3051 static bool
3052 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
3053 int *f ATTRIBUTE_UNUSED)
3054 {
3055 const char *name;
3056
3057 /* Don't bother recursing into other statement functions
3058 since they will be checked individually for purity. */
3059 if (e->expr_type != EXPR_FUNCTION
3060 || !e->symtree
3061 || e->symtree->n.sym == sym
3062 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3063 return false;
3064
3065 return gfc_pure_function (e, &name) ? false : true;
3066 }
3067
3068
3069 static int
3070 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
3071 {
3072 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
3073 }
3074
3075
3076 /* Check if an impure function is allowed in the current context. */
3077
3078 static bool check_pure_function (gfc_expr *e)
3079 {
3080 const char *name = NULL;
3081 if (!gfc_pure_function (e, &name) && name)
3082 {
3083 if (forall_flag)
3084 {
3085 gfc_error ("Reference to impure function %qs at %L inside a "
3086 "FORALL %s", name, &e->where,
3087 forall_flag == 2 ? "mask" : "block");
3088 return false;
3089 }
3090 else if (gfc_do_concurrent_flag)
3091 {
3092 gfc_error ("Reference to impure function %qs at %L inside a "
3093 "DO CONCURRENT %s", name, &e->where,
3094 gfc_do_concurrent_flag == 2 ? "mask" : "block");
3095 return false;
3096 }
3097 else if (gfc_pure (NULL))
3098 {
3099 gfc_error ("Reference to impure function %qs at %L "
3100 "within a PURE procedure", name, &e->where);
3101 return false;
3102 }
3103 if (!gfc_implicit_pure_function (e))
3104 gfc_unset_implicit_pure (NULL);
3105 }
3106 return true;
3107 }
3108
3109
3110 /* Update current procedure's array_outer_dependency flag, considering
3111 a call to procedure SYM. */
3112
3113 static void
3114 update_current_proc_array_outer_dependency (gfc_symbol *sym)
3115 {
3116 /* Check to see if this is a sibling function that has not yet
3117 been resolved. */
3118 gfc_namespace *sibling = gfc_current_ns->sibling;
3119 for (; sibling; sibling = sibling->sibling)
3120 {
3121 if (sibling->proc_name == sym)
3122 {
3123 gfc_resolve (sibling);
3124 break;
3125 }
3126 }
3127
3128 /* If SYM has references to outer arrays, so has the procedure calling
3129 SYM. If SYM is a procedure pointer, we can assume the worst. */
3130 if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
3131 && gfc_current_ns->proc_name)
3132 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3133 }
3134
3135
3136 /* Resolve a function call, which means resolving the arguments, then figuring
3137 out which entity the name refers to. */
3138
3139 static bool
3140 resolve_function (gfc_expr *expr)
3141 {
3142 gfc_actual_arglist *arg;
3143 gfc_symbol *sym;
3144 bool t;
3145 int temp;
3146 procedure_type p = PROC_INTRINSIC;
3147 bool no_formal_args;
3148
3149 sym = NULL;
3150 if (expr->symtree)
3151 sym = expr->symtree->n.sym;
3152
3153 /* If this is a procedure pointer component, it has already been resolved. */
3154 if (gfc_is_proc_ptr_comp (expr))
3155 return true;
3156
3157 /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
3158 another caf_get. */
3159 if (sym && sym->attr.intrinsic
3160 && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
3161 || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
3162 return true;
3163
3164 if (expr->ref)
3165 {
3166 gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name,
3167 &expr->where);
3168 return false;
3169 }
3170
3171 if (sym && sym->attr.intrinsic
3172 && !gfc_resolve_intrinsic (sym, &expr->where))
3173 return false;
3174
3175 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
3176 {
3177 gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
3178 return false;
3179 }
3180
3181 /* If this is a deferred TBP with an abstract interface (which may
3182 of course be referenced), expr->value.function.esym will be set. */
3183 if (sym && sym->attr.abstract && !expr->value.function.esym)
3184 {
3185 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3186 sym->name, &expr->where);
3187 return false;
3188 }
3189
3190 /* If this is a deferred TBP with an abstract interface, its result
3191 cannot be an assumed length character (F2003: C418). */
3192 if (sym && sym->attr.abstract && sym->attr.function
3193 && sym->result->ts.u.cl
3194 && sym->result->ts.u.cl->length == NULL
3195 && !sym->result->ts.deferred)
3196 {
3197 gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
3198 "character length result (F2008: C418)", sym->name,
3199 &sym->declared_at);
3200 return false;
3201 }
3202
3203 /* Switch off assumed size checking and do this again for certain kinds
3204 of procedure, once the procedure itself is resolved. */
3205 need_full_assumed_size++;
3206
3207 if (expr->symtree && expr->symtree->n.sym)
3208 p = expr->symtree->n.sym->attr.proc;
3209
3210 if (expr->value.function.isym && expr->value.function.isym->inquiry)
3211 inquiry_argument = true;
3212 no_formal_args = sym && is_external_proc (sym)
3213 && gfc_sym_get_dummy_args (sym) == NULL;
3214
3215 if (!resolve_actual_arglist (expr->value.function.actual,
3216 p, no_formal_args))
3217 {
3218 inquiry_argument = false;
3219 return false;
3220 }
3221
3222 inquiry_argument = false;
3223
3224 /* Resume assumed_size checking. */
3225 need_full_assumed_size--;
3226
3227 /* If the procedure is external, check for usage. */
3228 if (sym && is_external_proc (sym))
3229 resolve_global_procedure (sym, &expr->where, 0);
3230
3231 if (sym && sym->ts.type == BT_CHARACTER
3232 && sym->ts.u.cl
3233 && sym->ts.u.cl->length == NULL
3234 && !sym->attr.dummy
3235 && !sym->ts.deferred
3236 && expr->value.function.esym == NULL
3237 && !sym->attr.contained)
3238 {
3239 /* Internal procedures are taken care of in resolve_contained_fntype. */
3240 gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
3241 "be used at %L since it is not a dummy argument",
3242 sym->name, &expr->where);
3243 return false;
3244 }
3245
3246 /* See if function is already resolved. */
3247
3248 if (expr->value.function.name != NULL
3249 || expr->value.function.isym != NULL)
3250 {
3251 if (expr->ts.type == BT_UNKNOWN)
3252 expr->ts = sym->ts;
3253 t = true;
3254 }
3255 else
3256 {
3257 /* Apply the rules of section 14.1.2. */
3258
3259 switch (procedure_kind (sym))
3260 {
3261 case PTYPE_GENERIC:
3262 t = resolve_generic_f (expr);
3263 break;
3264
3265 case PTYPE_SPECIFIC:
3266 t = resolve_specific_f (expr);
3267 break;
3268
3269 case PTYPE_UNKNOWN:
3270 t = resolve_unknown_f (expr);
3271 break;
3272
3273 default:
3274 gfc_internal_error ("resolve_function(): bad function type");
3275 }
3276 }
3277
3278 /* If the expression is still a function (it might have simplified),
3279 then we check to see if we are calling an elemental function. */
3280
3281 if (expr->expr_type != EXPR_FUNCTION)
3282 return t;
3283
3284 /* Walk the argument list looking for invalid BOZ. */
3285 for (arg = expr->value.function.actual; arg; arg = arg->next)
3286 if (arg->expr && arg->expr->ts.type == BT_BOZ)
3287 {
3288 gfc_error ("A BOZ literal constant at %L cannot appear as an "
3289 "actual argument in a function reference",
3290 &arg->expr->where);
3291 return false;
3292 }
3293
3294 temp = need_full_assumed_size;
3295 need_full_assumed_size = 0;
3296
3297 if (!resolve_elemental_actual (expr, NULL))
3298 return false;
3299
3300 if (omp_workshare_flag
3301 && expr->value.function.esym
3302 && ! gfc_elemental (expr->value.function.esym))
3303 {
3304 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3305 "in WORKSHARE construct", expr->value.function.esym->name,
3306 &expr->where);
3307 t = false;
3308 }
3309
3310 #define GENERIC_ID expr->value.function.isym->id
3311 else if (expr->value.function.actual != NULL
3312 && expr->value.function.isym != NULL
3313 && GENERIC_ID != GFC_ISYM_LBOUND
3314 && GENERIC_ID != GFC_ISYM_LCOBOUND
3315 && GENERIC_ID != GFC_ISYM_UCOBOUND
3316 && GENERIC_ID != GFC_ISYM_LEN
3317 && GENERIC_ID != GFC_ISYM_LOC
3318 && GENERIC_ID != GFC_ISYM_C_LOC
3319 && GENERIC_ID != GFC_ISYM_PRESENT)
3320 {
3321 /* Array intrinsics must also have the last upper bound of an
3322 assumed size array argument. UBOUND and SIZE have to be
3323 excluded from the check if the second argument is anything
3324 than a constant. */
3325
3326 for (arg = expr->value.function.actual; arg; arg = arg->next)
3327 {
3328 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3329 && arg == expr->value.function.actual
3330 && arg->next != NULL && arg->next->expr)
3331 {
3332 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3333 break;
3334
3335 if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
3336 break;
3337
3338 if ((int)mpz_get_si (arg->next->expr->value.integer)
3339 < arg->expr->rank)
3340 break;
3341 }
3342
3343 if (arg->expr != NULL
3344 && arg->expr->rank > 0
3345 && resolve_assumed_size_actual (arg->expr))
3346 return false;
3347 }
3348 }
3349 #undef GENERIC_ID
3350
3351 need_full_assumed_size = temp;
3352
3353 if (!check_pure_function(expr))
3354 t = false;
3355
3356 /* Functions without the RECURSIVE attribution are not allowed to
3357 * call themselves. */
3358 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3359 {
3360 gfc_symbol *esym;
3361 esym = expr->value.function.esym;
3362
3363 if (is_illegal_recursion (esym, gfc_current_ns))
3364 {
3365 if (esym->attr.entry && esym->ns->entries)
3366 gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3367 " function %qs is not RECURSIVE",
3368 esym->name, &expr->where, esym->ns->entries->sym->name);
3369 else
3370 gfc_error ("Function %qs at %L cannot be called recursively, as it"
3371 " is not RECURSIVE", esym->name, &expr->where);
3372
3373 t = false;
3374 }
3375 }
3376
3377 /* Character lengths of use associated functions may contains references to
3378 symbols not referenced from the current program unit otherwise. Make sure
3379 those symbols are marked as referenced. */
3380
3381 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3382 && expr->value.function.esym->attr.use_assoc)
3383 {
3384 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3385 }
3386
3387 /* Make sure that the expression has a typespec that works. */
3388 if (expr->ts.type == BT_UNKNOWN)
3389 {
3390 if (expr->symtree->n.sym->result
3391 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3392 && !expr->symtree->n.sym->result->attr.proc_pointer)
3393 expr->ts = expr->symtree->n.sym->result->ts;
3394 }
3395
3396 if (!expr->ref && !expr->value.function.isym)
3397 {
3398 if (expr->value.function.esym)
3399 update_current_proc_array_outer_dependency (expr->value.function.esym);
3400 else
3401 update_current_proc_array_outer_dependency (sym);
3402 }
3403 else if (expr->ref)
3404 /* typebound procedure: Assume the worst. */
3405 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3406
3407 if (expr->value.function.esym
3408 && expr->value.function.esym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
3409 gfc_warning (OPT_Wdeprecated_declarations,
3410 "Using function %qs at %L is deprecated",
3411 sym->name, &expr->where);
3412 return t;
3413 }
3414
3415
3416 /************* Subroutine resolution *************/
3417
3418 static bool
3419 pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3420 {
3421 if (gfc_pure (sym))
3422 return true;
3423
3424 if (forall_flag)
3425 {
3426 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3427 name, loc);
3428 return false;
3429 }
3430 else if (gfc_do_concurrent_flag)
3431 {
3432 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3433 "PURE", name, loc);
3434 return false;
3435 }
3436 else if (gfc_pure (NULL))
3437 {
3438 gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3439 return false;
3440 }
3441
3442 gfc_unset_implicit_pure (NULL);
3443 return true;
3444 }
3445
3446
3447 static match
3448 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3449 {
3450 gfc_symbol *s;
3451
3452 if (sym->attr.generic)
3453 {
3454 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3455 if (s != NULL)
3456 {
3457 c->resolved_sym = s;
3458 if (!pure_subroutine (s, s->name, &c->loc))
3459 return MATCH_ERROR;
3460 return MATCH_YES;
3461 }
3462
3463 /* TODO: Need to search for elemental references in generic interface. */
3464 }
3465
3466 if (sym->attr.intrinsic)
3467 return gfc_intrinsic_sub_interface (c, 0);
3468
3469 return MATCH_NO;
3470 }
3471
3472
3473 static bool
3474 resolve_generic_s (gfc_code *c)
3475 {
3476 gfc_symbol *sym;
3477 match m;
3478
3479 sym = c->symtree->n.sym;
3480
3481 for (;;)
3482 {
3483 m = resolve_generic_s0 (c, sym);
3484 if (m == MATCH_YES)
3485 return true;
3486 else if (m == MATCH_ERROR)
3487 return false;
3488
3489 generic:
3490 if (sym->ns->parent == NULL)
3491 break;
3492 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3493
3494 if (sym == NULL)
3495 break;
3496 if (!generic_sym (sym))
3497 goto generic;
3498 }
3499
3500 /* Last ditch attempt. See if the reference is to an intrinsic
3501 that possesses a matching interface. 14.1.2.4 */
3502 sym = c->symtree->n.sym;
3503
3504 if (!gfc_is_intrinsic (sym, 1, c->loc))
3505 {
3506 gfc_error ("There is no specific subroutine for the generic %qs at %L",
3507 sym->name, &c->loc);
3508 return false;
3509 }
3510
3511 m = gfc_intrinsic_sub_interface (c, 0);
3512 if (m == MATCH_YES)
3513 return true;
3514 if (m == MATCH_NO)
3515 gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3516 "intrinsic subroutine interface", sym->name, &c->loc);
3517
3518 return false;
3519 }
3520
3521
3522 /* Resolve a subroutine call known to be specific. */
3523
3524 static match
3525 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3526 {
3527 match m;
3528
3529 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3530 {
3531 if (sym->attr.dummy)
3532 {
3533 sym->attr.proc = PROC_DUMMY;
3534 goto found;
3535 }
3536
3537 sym->attr.proc = PROC_EXTERNAL;
3538 goto found;
3539 }
3540
3541 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3542 goto found;
3543
3544 if (sym->attr.intrinsic)
3545 {
3546 m = gfc_intrinsic_sub_interface (c, 1);
3547 if (m == MATCH_YES)
3548 return MATCH_YES;
3549 if (m == MATCH_NO)
3550 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3551 "with an intrinsic", sym->name, &c->loc);
3552
3553 return MATCH_ERROR;
3554 }
3555
3556 return MATCH_NO;
3557
3558 found:
3559 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3560
3561 c->resolved_sym = sym;
3562 if (!pure_subroutine (sym, sym->name, &c->loc))
3563 return MATCH_ERROR;
3564
3565 return MATCH_YES;
3566 }
3567
3568
3569 static bool
3570 resolve_specific_s (gfc_code *c)
3571 {
3572 gfc_symbol *sym;
3573 match m;
3574
3575 sym = c->symtree->n.sym;
3576
3577 for (;;)
3578 {
3579 m = resolve_specific_s0 (c, sym);
3580 if (m == MATCH_YES)
3581 return true;
3582 if (m == MATCH_ERROR)
3583 return false;
3584
3585 if (sym->ns->parent == NULL)
3586 break;
3587
3588 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3589
3590 if (sym == NULL)
3591 break;
3592 }
3593
3594 sym = c->symtree->n.sym;
3595 gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3596 sym->name, &c->loc);
3597
3598 return false;
3599 }
3600
3601
3602 /* Resolve a subroutine call not known to be generic nor specific. */
3603
3604 static bool
3605 resolve_unknown_s (gfc_code *c)
3606 {
3607 gfc_symbol *sym;
3608
3609 sym = c->symtree->n.sym;
3610
3611 if (sym->attr.dummy)
3612 {
3613 sym->attr.proc = PROC_DUMMY;
3614 goto found;
3615 }
3616
3617 /* See if we have an intrinsic function reference. */
3618
3619 if (gfc_is_intrinsic (sym, 1, c->loc))
3620 {
3621 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3622 return true;
3623 return false;
3624 }
3625
3626 /* The reference is to an external name. */
3627
3628 found:
3629 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3630
3631 c->resolved_sym = sym;
3632
3633 return pure_subroutine (sym, sym->name, &c->loc);
3634 }
3635
3636
3637 /* Resolve a subroutine call. Although it was tempting to use the same code
3638 for functions, subroutines and functions are stored differently and this
3639 makes things awkward. */
3640
3641 static bool
3642 resolve_call (gfc_code *c)
3643 {
3644 bool t;
3645 procedure_type ptype = PROC_INTRINSIC;
3646 gfc_symbol *csym, *sym;
3647 bool no_formal_args;
3648
3649 csym = c->symtree ? c->symtree->n.sym : NULL;
3650
3651 if (csym && csym->ts.type != BT_UNKNOWN)
3652 {
3653 gfc_error ("%qs at %L has a type, which is not consistent with "
3654 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3655 return false;
3656 }
3657
3658 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3659 {
3660 gfc_symtree *st;
3661 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3662 sym = st ? st->n.sym : NULL;
3663 if (sym && csym != sym
3664 && sym->ns == gfc_current_ns
3665 && sym->attr.flavor == FL_PROCEDURE
3666 && sym->attr.contained)
3667 {
3668 sym->refs++;
3669 if (csym->attr.generic)
3670 c->symtree->n.sym = sym;
3671 else
3672 c->symtree = st;
3673 csym = c->symtree->n.sym;
3674 }
3675 }
3676
3677 /* If this ia a deferred TBP, c->expr1 will be set. */
3678 if (!c->expr1 && csym)
3679 {
3680 if (csym->attr.abstract)
3681 {
3682 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3683 csym->name, &c->loc);
3684 return false;
3685 }
3686
3687 /* Subroutines without the RECURSIVE attribution are not allowed to
3688 call themselves. */
3689 if (is_illegal_recursion (csym, gfc_current_ns))
3690 {
3691 if (csym->attr.entry && csym->ns->entries)
3692 gfc_error ("ENTRY %qs at %L cannot be called recursively, "
3693 "as subroutine %qs is not RECURSIVE",
3694 csym->name, &c->loc, csym->ns->entries->sym->name);
3695 else
3696 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
3697 "as it is not RECURSIVE", csym->name, &c->loc);
3698
3699 t = false;
3700 }
3701 }
3702
3703 /* Switch off assumed size checking and do this again for certain kinds
3704 of procedure, once the procedure itself is resolved. */
3705 need_full_assumed_size++;
3706
3707 if (csym)
3708 ptype = csym->attr.proc;
3709
3710 no_formal_args = csym && is_external_proc (csym)
3711 && gfc_sym_get_dummy_args (csym) == NULL;
3712 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3713 return false;
3714
3715 /* Resume assumed_size checking. */
3716 need_full_assumed_size--;
3717
3718 /* If external, check for usage. */
3719 if (csym && is_external_proc (csym))
3720 resolve_global_procedure (csym, &c->loc, 1);
3721
3722 t = true;
3723 if (c->resolved_sym == NULL)
3724 {
3725 c->resolved_isym = NULL;
3726 switch (procedure_kind (csym))
3727 {
3728 case PTYPE_GENERIC:
3729 t = resolve_generic_s (c);
3730 break;
3731
3732 case PTYPE_SPECIFIC:
3733 t = resolve_specific_s (c);
3734 break;
3735
3736 case PTYPE_UNKNOWN:
3737 t = resolve_unknown_s (c);
3738 break;
3739
3740 default:
3741 gfc_internal_error ("resolve_subroutine(): bad function type");
3742 }
3743 }
3744
3745 /* Some checks of elemental subroutine actual arguments. */
3746 if (!resolve_elemental_actual (NULL, c))
3747 return false;
3748
3749 if (!c->expr1)
3750 update_current_proc_array_outer_dependency (csym);
3751 else
3752 /* Typebound procedure: Assume the worst. */
3753 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3754
3755 if (c->resolved_sym
3756 && c->resolved_sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
3757 gfc_warning (OPT_Wdeprecated_declarations,
3758 "Using subroutine %qs at %L is deprecated",
3759 c->resolved_sym->name, &c->loc);
3760
3761 return t;
3762 }
3763
3764
3765 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3766 op1->shape and op2->shape are non-NULL return true if their shapes
3767 match. If both op1->shape and op2->shape are non-NULL return false
3768 if their shapes do not match. If either op1->shape or op2->shape is
3769 NULL, return true. */
3770
3771 static bool
3772 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3773 {
3774 bool t;
3775 int i;
3776
3777 t = true;
3778
3779 if (op1->shape != NULL && op2->shape != NULL)
3780 {
3781 for (i = 0; i < op1->rank; i++)
3782 {
3783 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3784 {
3785 gfc_error ("Shapes for operands at %L and %L are not conformable",
3786 &op1->where, &op2->where);
3787 t = false;
3788 break;
3789 }
3790 }
3791 }
3792
3793 return t;
3794 }
3795
3796 /* Convert a logical operator to the corresponding bitwise intrinsic call.
3797 For example A .AND. B becomes IAND(A, B). */
3798 static gfc_expr *
3799 logical_to_bitwise (gfc_expr *e)
3800 {
3801 gfc_expr *tmp, *op1, *op2;
3802 gfc_isym_id isym;
3803 gfc_actual_arglist *args = NULL;
3804
3805 gcc_assert (e->expr_type == EXPR_OP);
3806
3807 isym = GFC_ISYM_NONE;
3808 op1 = e->value.op.op1;
3809 op2 = e->value.op.op2;
3810
3811 switch (e->value.op.op)
3812 {
3813 case INTRINSIC_NOT:
3814 isym = GFC_ISYM_NOT;
3815 break;
3816 case INTRINSIC_AND:
3817 isym = GFC_ISYM_IAND;
3818 break;
3819 case INTRINSIC_OR:
3820 isym = GFC_ISYM_IOR;
3821 break;
3822 case INTRINSIC_NEQV:
3823 isym = GFC_ISYM_IEOR;
3824 break;
3825 case INTRINSIC_EQV:
3826 /* "Bitwise eqv" is just the complement of NEQV === IEOR.
3827 Change the old expression to NEQV, which will get replaced by IEOR,
3828 and wrap it in NOT. */
3829 tmp = gfc_copy_expr (e);
3830 tmp->value.op.op = INTRINSIC_NEQV;
3831 tmp = logical_to_bitwise (tmp);
3832 isym = GFC_ISYM_NOT;
3833 op1 = tmp;
3834 op2 = NULL;
3835 break;
3836 default:
3837 gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
3838 }
3839
3840 /* Inherit the original operation's operands as arguments. */
3841 args = gfc_get_actual_arglist ();
3842 args->expr = op1;
3843 if (op2)
3844 {
3845 args->next = gfc_get_actual_arglist ();
3846 args->next->expr = op2;
3847 }
3848
3849 /* Convert the expression to a function call. */
3850 e->expr_type = EXPR_FUNCTION;
3851 e->value.function.actual = args;
3852 e->value.function.isym = gfc_intrinsic_function_by_id (isym);
3853 e->value.function.name = e->value.function.isym->name;
3854 e->value.function.esym = NULL;
3855
3856 /* Make up a pre-resolved function call symtree if we need to. */
3857 if (!e->symtree || !e->symtree->n.sym)
3858 {
3859 gfc_symbol *sym;
3860 gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
3861 sym = e->symtree->n.sym;
3862 sym->result = sym;
3863 sym->attr.flavor = FL_PROCEDURE;
3864 sym->attr.function = 1;
3865 sym->attr.elemental = 1;
3866 sym->attr.pure = 1;
3867 sym->attr.referenced = 1;
3868 gfc_intrinsic_symbol (sym);
3869 gfc_commit_symbol (sym);
3870 }
3871
3872 args->name = e->value.function.isym->formal->name;
3873 if (e->value.function.isym->formal->next)
3874 args->next->name = e->value.function.isym->formal->next->name;
3875
3876 return e;
3877 }
3878
3879 /* Recursively append candidate UOP to CANDIDATES. Store the number of
3880 candidates in CANDIDATES_LEN. */
3881 static void
3882 lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
3883 char **&candidates,
3884 size_t &candidates_len)
3885 {
3886 gfc_symtree *p;
3887
3888 if (uop == NULL)
3889 return;
3890
3891 /* Not sure how to properly filter here. Use all for a start.
3892 n.uop.op is NULL for empty interface operators (is that legal?) disregard
3893 these as i suppose they don't make terribly sense. */
3894
3895 if (uop->n.uop->op != NULL)
3896 vec_push (candidates, candidates_len, uop->name);
3897
3898 p = uop->left;
3899 if (p)
3900 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
3901
3902 p = uop->right;
3903 if (p)
3904 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
3905 }
3906
3907 /* Lookup user-operator OP fuzzily, taking names in UOP into account. */
3908
3909 static const char*
3910 lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
3911 {
3912 char **candidates = NULL;
3913 size_t candidates_len = 0;
3914 lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
3915 return gfc_closest_fuzzy_match (op, candidates);
3916 }
3917
3918
3919 /* Callback finding an impure function as an operand to an .and. or
3920 .or. expression. Remember the last function warned about to
3921 avoid double warnings when recursing. */
3922
3923 static int
3924 impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
3925 void *data)
3926 {
3927 gfc_expr *f = *e;
3928 const char *name;
3929 static gfc_expr *last = NULL;
3930 bool *found = (bool *) data;
3931
3932 if (f->expr_type == EXPR_FUNCTION)
3933 {
3934 *found = 1;
3935 if (f != last && !gfc_pure_function (f, &name)
3936 && !gfc_implicit_pure_function (f))
3937 {
3938 if (name)
3939 gfc_warning (OPT_Wfunction_elimination,
3940 "Impure function %qs at %L might not be evaluated",
3941 name, &f->where);
3942 else
3943 gfc_warning (OPT_Wfunction_elimination,
3944 "Impure function at %L might not be evaluated",
3945 &f->where);
3946 }
3947 last = f;
3948 }
3949
3950 return 0;
3951 }
3952
3953 /* Return true if TYPE is character based, false otherwise. */
3954
3955 static int
3956 is_character_based (bt type)
3957 {
3958 return type == BT_CHARACTER || type == BT_HOLLERITH;
3959 }
3960
3961
3962 /* If expression is a hollerith, convert it to character and issue a warning
3963 for the conversion. */
3964
3965 static void
3966 convert_hollerith_to_character (gfc_expr *e)
3967 {
3968 if (e->ts.type == BT_HOLLERITH)
3969 {
3970 gfc_typespec t;
3971 gfc_clear_ts (&t);
3972 t.type = BT_CHARACTER;
3973 t.kind = e->ts.kind;
3974 gfc_convert_type_warn (e, &t, 2, 1);
3975 }
3976 }
3977
3978 /* Convert to numeric and issue a warning for the conversion. */
3979
3980 static void
3981 convert_to_numeric (gfc_expr *a, gfc_expr *b)
3982 {
3983 gfc_typespec t;
3984 gfc_clear_ts (&t);
3985 t.type = b->ts.type;
3986 t.kind = b->ts.kind;
3987 gfc_convert_type_warn (a, &t, 2, 1);
3988 }
3989
3990 /* Resolve an operator expression node. This can involve replacing the
3991 operation with a user defined function call. */
3992
3993 static bool
3994 resolve_operator (gfc_expr *e)
3995 {
3996 gfc_expr *op1, *op2;
3997 char msg[200];
3998 bool dual_locus_error;
3999 bool t = true;
4000
4001 /* Resolve all subnodes-- give them types. */
4002
4003 switch (e->value.op.op)
4004 {
4005 default:
4006 if (!gfc_resolve_expr (e->value.op.op2))
4007 return false;
4008
4009 /* Fall through. */
4010
4011 case INTRINSIC_NOT:
4012 case INTRINSIC_UPLUS:
4013 case INTRINSIC_UMINUS:
4014 case INTRINSIC_PARENTHESES:
4015 if (!gfc_resolve_expr (e->value.op.op1))
4016 return false;
4017 if (e->value.op.op1
4018 && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
4019 {
4020 gfc_error ("BOZ literal constant at %L cannot be an operand of "
4021 "unary operator %qs", &e->value.op.op1->where,
4022 gfc_op2string (e->value.op.op));
4023 return false;
4024 }
4025 break;
4026 }
4027
4028 /* Typecheck the new node. */
4029
4030 op1 = e->value.op.op1;
4031 op2 = e->value.op.op2;
4032 if (op1 == NULL && op2 == NULL)
4033 return false;
4034
4035 dual_locus_error = false;
4036
4037 /* op1 and op2 cannot both be BOZ. */
4038 if (op1 && op1->ts.type == BT_BOZ
4039 && op2 && op2->ts.type == BT_BOZ)
4040 {
4041 gfc_error ("Operands at %L and %L cannot appear as operands of "
4042 "binary operator %qs", &op1->where, &op2->where,
4043 gfc_op2string (e->value.op.op));
4044 return false;
4045 }
4046
4047 if ((op1 && op1->expr_type == EXPR_NULL)
4048 || (op2 && op2->expr_type == EXPR_NULL))
4049 {
4050 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
4051 goto bad_op;
4052 }
4053
4054 switch (e->value.op.op)
4055 {
4056 case INTRINSIC_UPLUS:
4057 case INTRINSIC_UMINUS:
4058 if (op1->ts.type == BT_INTEGER
4059 || op1->ts.type == BT_REAL
4060 || op1->ts.type == BT_COMPLEX)
4061 {
4062 e->ts = op1->ts;
4063 break;
4064 }
4065
4066 sprintf (msg, _("Operand of unary numeric operator %%<%s%%> at %%L is %s"),
4067 gfc_op2string (e->value.op.op), gfc_typename (e));
4068 goto bad_op;
4069
4070 case INTRINSIC_PLUS:
4071 case INTRINSIC_MINUS:
4072 case INTRINSIC_TIMES:
4073 case INTRINSIC_DIVIDE:
4074 case INTRINSIC_POWER:
4075 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4076 {
4077 gfc_type_convert_binary (e, 1);
4078 break;
4079 }
4080
4081 if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
4082 sprintf (msg,
4083 _("Unexpected derived-type entities in binary intrinsic "
4084 "numeric operator %%<%s%%> at %%L"),
4085 gfc_op2string (e->value.op.op));
4086 else
4087 sprintf (msg,
4088 _("Operands of binary numeric operator %%<%s%%> at %%L are %s/%s"),
4089 gfc_op2string (e->value.op.op), gfc_typename (op1),
4090 gfc_typename (op2));
4091 goto bad_op;
4092
4093 case INTRINSIC_CONCAT:
4094 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4095 && op1->ts.kind == op2->ts.kind)
4096 {
4097 e->ts.type = BT_CHARACTER;
4098 e->ts.kind = op1->ts.kind;
4099 break;
4100 }
4101
4102 sprintf (msg,
4103 _("Operands of string concatenation operator at %%L are %s/%s"),
4104 gfc_typename (op1), gfc_typename (op2));
4105 goto bad_op;
4106
4107 case INTRINSIC_AND:
4108 case INTRINSIC_OR:
4109 case INTRINSIC_EQV:
4110 case INTRINSIC_NEQV:
4111 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4112 {
4113 e->ts.type = BT_LOGICAL;
4114 e->ts.kind = gfc_kind_max (op1, op2);
4115 if (op1->ts.kind < e->ts.kind)
4116 gfc_convert_type (op1, &e->ts, 2);
4117 else if (op2->ts.kind < e->ts.kind)
4118 gfc_convert_type (op2, &e->ts, 2);
4119
4120 if (flag_frontend_optimize &&
4121 (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
4122 {
4123 /* Warn about short-circuiting
4124 with impure function as second operand. */
4125 bool op2_f = false;
4126 gfc_expr_walker (&op2, impure_function_callback, &op2_f);
4127 }
4128 break;
4129 }
4130
4131 /* Logical ops on integers become bitwise ops with -fdec. */
4132 else if (flag_dec
4133 && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
4134 {
4135 e->ts.type = BT_INTEGER;
4136 e->ts.kind = gfc_kind_max (op1, op2);
4137 if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
4138 gfc_convert_type (op1, &e->ts, 1);
4139 if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
4140 gfc_convert_type (op2, &e->ts, 1);
4141 e = logical_to_bitwise (e);
4142 goto simplify_op;
4143 }
4144
4145 sprintf (msg, _("Operands of logical operator %%<%s%%> at %%L are %s/%s"),
4146 gfc_op2string (e->value.op.op), gfc_typename (op1),
4147 gfc_typename (op2));
4148
4149 goto bad_op;
4150
4151 case INTRINSIC_NOT:
4152 /* Logical ops on integers become bitwise ops with -fdec. */
4153 if (flag_dec && op1->ts.type == BT_INTEGER)
4154 {
4155 e->ts.type = BT_INTEGER;
4156 e->ts.kind = op1->ts.kind;
4157 e = logical_to_bitwise (e);
4158 goto simplify_op;
4159 }
4160
4161 if (op1->ts.type == BT_LOGICAL)
4162 {
4163 e->ts.type = BT_LOGICAL;
4164 e->ts.kind = op1->ts.kind;
4165 break;
4166 }
4167
4168 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
4169 gfc_typename (op1));
4170 goto bad_op;
4171
4172 case INTRINSIC_GT:
4173 case INTRINSIC_GT_OS:
4174 case INTRINSIC_GE:
4175 case INTRINSIC_GE_OS:
4176 case INTRINSIC_LT:
4177 case INTRINSIC_LT_OS:
4178 case INTRINSIC_LE:
4179 case INTRINSIC_LE_OS:
4180 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
4181 {
4182 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
4183 goto bad_op;
4184 }
4185
4186 /* Fall through. */
4187
4188 case INTRINSIC_EQ:
4189 case INTRINSIC_EQ_OS:
4190 case INTRINSIC_NE:
4191 case INTRINSIC_NE_OS:
4192
4193 if (flag_dec
4194 && is_character_based (op1->ts.type)
4195 && is_character_based (op2->ts.type))
4196 {
4197 convert_hollerith_to_character (op1);
4198 convert_hollerith_to_character (op2);
4199 }
4200
4201 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4202 && op1->ts.kind == op2->ts.kind)
4203 {
4204 e->ts.type = BT_LOGICAL;
4205 e->ts.kind = gfc_default_logical_kind;
4206 break;
4207 }
4208
4209 /* If op1 is BOZ, then op2 is not!. Try to convert to type of op2. */
4210 if (op1->ts.type == BT_BOZ)
4211 {
4212 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
4213 "as an operand of a relational operator"),
4214 &op1->where))
4215 return false;
4216
4217 if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
4218 return false;
4219
4220 if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
4221 return false;
4222 }
4223
4224 /* If op2 is BOZ, then op1 is not!. Try to convert to type of op2. */
4225 if (op2->ts.type == BT_BOZ)
4226 {
4227 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
4228 " as an operand of a relational operator"),
4229 &op2->where))
4230 return false;
4231
4232 if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
4233 return false;
4234
4235 if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
4236 return false;
4237 }
4238 if (flag_dec
4239 && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
4240 convert_to_numeric (op1, op2);
4241
4242 if (flag_dec
4243 && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
4244 convert_to_numeric (op2, op1);
4245
4246 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4247 {
4248 gfc_type_convert_binary (e, 1);
4249
4250 e->ts.type = BT_LOGICAL;
4251 e->ts.kind = gfc_default_logical_kind;
4252
4253 if (warn_compare_reals)
4254 {
4255 gfc_intrinsic_op op = e->value.op.op;
4256
4257 /* Type conversion has made sure that the types of op1 and op2
4258 agree, so it is only necessary to check the first one. */
4259 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
4260 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
4261 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
4262 {
4263 const char *msg;
4264
4265 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
4266 msg = G_("Equality comparison for %s at %L");
4267 else
4268 msg = G_("Inequality comparison for %s at %L");
4269
4270 gfc_warning (OPT_Wcompare_reals, msg,
4271 gfc_typename (op1), &op1->where);
4272 }
4273 }
4274
4275 break;
4276 }
4277
4278 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4279 sprintf (msg,
4280 _("Logicals at %%L must be compared with %s instead of %s"),
4281 (e->value.op.op == INTRINSIC_EQ
4282 || e->value.op.op == INTRINSIC_EQ_OS)
4283 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
4284 else
4285 sprintf (msg,
4286 _("Operands of comparison operator %%<%s%%> at %%L are %s/%s"),
4287 gfc_op2string (e->value.op.op), gfc_typename (op1),
4288 gfc_typename (op2));
4289
4290 goto bad_op;
4291
4292 case INTRINSIC_USER:
4293 if (e->value.op.uop->op == NULL)
4294 {
4295 const char *name = e->value.op.uop->name;
4296 const char *guessed;
4297 guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
4298 if (guessed)
4299 sprintf (msg, _("Unknown operator %%<%s%%> at %%L; did you mean '%s'?"),
4300 name, guessed);
4301 else
4302 sprintf (msg, _("Unknown operator %%<%s%%> at %%L"), name);
4303 }
4304 else if (op2 == NULL)
4305 sprintf (msg, _("Operand of user operator %%<%s%%> at %%L is %s"),
4306 e->value.op.uop->name, gfc_typename (op1));
4307 else
4308 {
4309 sprintf (msg, _("Operands of user operator %%<%s%%> at %%L are %s/%s"),
4310 e->value.op.uop->name, gfc_typename (op1),
4311 gfc_typename (op2));
4312 e->value.op.uop->op->sym->attr.referenced = 1;
4313 }
4314
4315 goto bad_op;
4316
4317 case INTRINSIC_PARENTHESES:
4318 e->ts = op1->ts;
4319 if (e->ts.type == BT_CHARACTER)
4320 e->ts.u.cl = op1->ts.u.cl;
4321 break;
4322
4323 default:
4324 gfc_internal_error ("resolve_operator(): Bad intrinsic");
4325 }
4326
4327 /* Deal with arrayness of an operand through an operator. */
4328
4329 switch (e->value.op.op)
4330 {
4331 case INTRINSIC_PLUS:
4332 case INTRINSIC_MINUS:
4333 case INTRINSIC_TIMES:
4334 case INTRINSIC_DIVIDE:
4335 case INTRINSIC_POWER:
4336 case INTRINSIC_CONCAT:
4337 case INTRINSIC_AND:
4338 case INTRINSIC_OR:
4339 case INTRINSIC_EQV:
4340 case INTRINSIC_NEQV:
4341 case INTRINSIC_EQ:
4342 case INTRINSIC_EQ_OS:
4343 case INTRINSIC_NE:
4344 case INTRINSIC_NE_OS:
4345 case INTRINSIC_GT:
4346 case INTRINSIC_GT_OS:
4347 case INTRINSIC_GE:
4348 case INTRINSIC_GE_OS:
4349 case INTRINSIC_LT:
4350 case INTRINSIC_LT_OS:
4351 case INTRINSIC_LE:
4352 case INTRINSIC_LE_OS:
4353
4354 if (op1->rank == 0 && op2->rank == 0)
4355 e->rank = 0;
4356
4357 if (op1->rank == 0 && op2->rank != 0)
4358 {
4359 e->rank = op2->rank;
4360
4361 if (e->shape == NULL)
4362 e->shape = gfc_copy_shape (op2->shape, op2->rank);
4363 }
4364
4365 if (op1->rank != 0 && op2->rank == 0)
4366 {
4367 e->rank = op1->rank;
4368
4369 if (e->shape == NULL)
4370 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4371 }
4372
4373 if (op1->rank != 0 && op2->rank != 0)
4374 {
4375 if (op1->rank == op2->rank)
4376 {
4377 e->rank = op1->rank;
4378 if (e->shape == NULL)
4379 {
4380 t = compare_shapes (op1, op2);
4381 if (!t)
4382 e->shape = NULL;
4383 else
4384 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4385 }
4386 }
4387 else
4388 {
4389 /* Allow higher level expressions to work. */
4390 e->rank = 0;
4391
4392 /* Try user-defined operators, and otherwise throw an error. */
4393 dual_locus_error = true;
4394 sprintf (msg,
4395 _("Inconsistent ranks for operator at %%L and %%L"));
4396 goto bad_op;
4397 }
4398 }
4399
4400 break;
4401
4402 case INTRINSIC_PARENTHESES:
4403 case INTRINSIC_NOT:
4404 case INTRINSIC_UPLUS:
4405 case INTRINSIC_UMINUS:
4406 /* Simply copy arrayness attribute */
4407 e->rank = op1->rank;
4408
4409 if (e->shape == NULL)
4410 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4411
4412 break;
4413
4414 default:
4415 break;
4416 }
4417
4418 simplify_op:
4419
4420 /* Attempt to simplify the expression. */
4421 if (t)
4422 {
4423 t = gfc_simplify_expr (e, 0);
4424 /* Some calls do not succeed in simplification and return false
4425 even though there is no error; e.g. variable references to
4426 PARAMETER arrays. */
4427 if (!gfc_is_constant_expr (e))
4428 t = true;
4429 }
4430 return t;
4431
4432 bad_op:
4433
4434 {
4435 match m = gfc_extend_expr (e);
4436 if (m == MATCH_YES)
4437 return true;
4438 if (m == MATCH_ERROR)
4439 return false;
4440 }
4441
4442 if (dual_locus_error)
4443 gfc_error (msg, &op1->where, &op2->where);
4444 else
4445 gfc_error (msg, &e->where);
4446
4447 return false;
4448 }
4449
4450
4451 /************** Array resolution subroutines **************/
4452
4453 enum compare_result
4454 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
4455
4456 /* Compare two integer expressions. */
4457
4458 static compare_result
4459 compare_bound (gfc_expr *a, gfc_expr *b)
4460 {
4461 int i;
4462
4463 if (a == NULL || a->expr_type != EXPR_CONSTANT
4464 || b == NULL || b->expr_type != EXPR_CONSTANT)
4465 return CMP_UNKNOWN;
4466
4467 /* If either of the types isn't INTEGER, we must have
4468 raised an error earlier. */
4469
4470 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
4471 return CMP_UNKNOWN;
4472
4473 i = mpz_cmp (a->value.integer, b->value.integer);
4474
4475 if (i < 0)
4476 return CMP_LT;
4477 if (i > 0)
4478 return CMP_GT;
4479 return CMP_EQ;
4480 }
4481
4482
4483 /* Compare an integer expression with an integer. */
4484
4485 static compare_result
4486 compare_bound_int (gfc_expr *a, int b)
4487 {
4488 int i;
4489
4490 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4491 return CMP_UNKNOWN;
4492
4493 if (a->ts.type != BT_INTEGER)
4494 gfc_internal_error ("compare_bound_int(): Bad expression");
4495
4496 i = mpz_cmp_si (a->value.integer, b);
4497
4498 if (i < 0)
4499 return CMP_LT;
4500 if (i > 0)
4501 return CMP_GT;
4502 return CMP_EQ;
4503 }
4504
4505
4506 /* Compare an integer expression with a mpz_t. */
4507
4508 static compare_result
4509 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
4510 {
4511 int i;
4512
4513 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4514 return CMP_UNKNOWN;
4515
4516 if (a->ts.type != BT_INTEGER)
4517 gfc_internal_error ("compare_bound_int(): Bad expression");
4518
4519 i = mpz_cmp (a->value.integer, b);
4520
4521 if (i < 0)
4522 return CMP_LT;
4523 if (i > 0)
4524 return CMP_GT;
4525 return CMP_EQ;
4526 }
4527
4528
4529 /* Compute the last value of a sequence given by a triplet.
4530 Return 0 if it wasn't able to compute the last value, or if the
4531 sequence if empty, and 1 otherwise. */
4532
4533 static int
4534 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4535 gfc_expr *stride, mpz_t last)
4536 {
4537 mpz_t rem;
4538
4539 if (start == NULL || start->expr_type != EXPR_CONSTANT
4540 || end == NULL || end->expr_type != EXPR_CONSTANT
4541 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4542 return 0;
4543
4544 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4545 || (stride != NULL && stride->ts.type != BT_INTEGER))
4546 return 0;
4547
4548 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
4549 {
4550 if (compare_bound (start, end) == CMP_GT)
4551 return 0;
4552 mpz_set (last, end->value.integer);
4553 return 1;
4554 }
4555
4556 if (compare_bound_int (stride, 0) == CMP_GT)
4557 {
4558 /* Stride is positive */
4559 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4560 return 0;
4561 }
4562 else
4563 {
4564 /* Stride is negative */
4565 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4566 return 0;
4567 }
4568
4569 mpz_init (rem);
4570 mpz_sub (rem, end->value.integer, start->value.integer);
4571 mpz_tdiv_r (rem, rem, stride->value.integer);
4572 mpz_sub (last, end->value.integer, rem);
4573 mpz_clear (rem);
4574
4575 return 1;
4576 }
4577
4578
4579 /* Compare a single dimension of an array reference to the array
4580 specification. */
4581
4582 static bool
4583 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4584 {
4585 mpz_t last_value;
4586
4587 if (ar->dimen_type[i] == DIMEN_STAR)
4588 {
4589 gcc_assert (ar->stride[i] == NULL);
4590 /* This implies [*] as [*:] and [*:3] are not possible. */
4591 if (ar->start[i] == NULL)
4592 {
4593 gcc_assert (ar->end[i] == NULL);
4594 return true;
4595 }
4596 }
4597
4598 /* Given start, end and stride values, calculate the minimum and
4599 maximum referenced indexes. */
4600
4601 switch (ar->dimen_type[i])
4602 {
4603 case DIMEN_VECTOR:
4604 case DIMEN_THIS_IMAGE:
4605 break;
4606
4607 case DIMEN_STAR:
4608 case DIMEN_ELEMENT:
4609 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4610 {
4611 if (i < as->rank)
4612 gfc_warning (0, "Array reference at %L is out of bounds "
4613 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4614 mpz_get_si (ar->start[i]->value.integer),
4615 mpz_get_si (as->lower[i]->value.integer), i+1);
4616 else
4617 gfc_warning (0, "Array reference at %L is out of bounds "
4618 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4619 mpz_get_si (ar->start[i]->value.integer),
4620 mpz_get_si (as->lower[i]->value.integer),
4621 i + 1 - as->rank);
4622 return true;
4623 }
4624 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4625 {
4626 if (i < as->rank)
4627 gfc_warning (0, "Array reference at %L is out of bounds "
4628 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4629 mpz_get_si (ar->start[i]->value.integer),
4630 mpz_get_si (as->upper[i]->value.integer), i+1);
4631 else
4632 gfc_warning (0, "Array reference at %L is out of bounds "
4633 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4634 mpz_get_si (ar->start[i]->value.integer),
4635 mpz_get_si (as->upper[i]->value.integer),
4636 i + 1 - as->rank);
4637 return true;
4638 }
4639
4640 break;
4641
4642 case DIMEN_RANGE:
4643 {
4644 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4645 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4646
4647 compare_result comp_start_end = compare_bound (AR_START, AR_END);
4648
4649 /* Check for zero stride, which is not allowed. */
4650 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4651 {
4652 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4653 return false;
4654 }
4655
4656 /* if start == len || (stride > 0 && start < len)
4657 || (stride < 0 && start > len),
4658 then the array section contains at least one element. In this
4659 case, there is an out-of-bounds access if
4660 (start < lower || start > upper). */
4661 if (compare_bound (AR_START, AR_END) == CMP_EQ
4662 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4663 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4664 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4665 && comp_start_end == CMP_GT))
4666 {
4667 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4668 {
4669 gfc_warning (0, "Lower array reference at %L is out of bounds "
4670 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4671 mpz_get_si (AR_START->value.integer),
4672 mpz_get_si (as->lower[i]->value.integer), i+1);
4673 return true;
4674 }
4675 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4676 {
4677 gfc_warning (0, "Lower array reference at %L is out of bounds "
4678 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4679 mpz_get_si (AR_START->value.integer),
4680 mpz_get_si (as->upper[i]->value.integer), i+1);
4681 return true;
4682 }
4683 }
4684
4685 /* If we can compute the highest index of the array section,
4686 then it also has to be between lower and upper. */
4687 mpz_init (last_value);
4688 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4689 last_value))
4690 {
4691 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4692 {
4693 gfc_warning (0, "Upper array reference at %L is out of bounds "
4694 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4695 mpz_get_si (last_value),
4696 mpz_get_si (as->lower[i]->value.integer), i+1);
4697 mpz_clear (last_value);
4698 return true;
4699 }
4700 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4701 {
4702 gfc_warning (0, "Upper array reference at %L is out of bounds "
4703 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4704 mpz_get_si (last_value),
4705 mpz_get_si (as->upper[i]->value.integer), i+1);
4706 mpz_clear (last_value);
4707 return true;
4708 }
4709 }
4710 mpz_clear (last_value);
4711
4712 #undef AR_START
4713 #undef AR_END
4714 }
4715 break;
4716
4717 default:
4718 gfc_internal_error ("check_dimension(): Bad array reference");
4719 }
4720
4721 return true;
4722 }
4723
4724
4725 /* Compare an array reference with an array specification. */
4726
4727 static bool
4728 compare_spec_to_ref (gfc_array_ref *ar)
4729 {
4730 gfc_array_spec *as;
4731 int i;
4732
4733 as = ar->as;
4734 i = as->rank - 1;
4735 /* TODO: Full array sections are only allowed as actual parameters. */
4736 if (as->type == AS_ASSUMED_SIZE
4737 && (/*ar->type == AR_FULL
4738 ||*/ (ar->type == AR_SECTION
4739 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4740 {
4741 gfc_error ("Rightmost upper bound of assumed size array section "
4742 "not specified at %L", &ar->where);
4743 return false;
4744 }
4745
4746 if (ar->type == AR_FULL)
4747 return true;
4748
4749 if (as->rank != ar->dimen)
4750 {
4751 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4752 &ar->where, ar->dimen, as->rank);
4753 return false;
4754 }
4755
4756 /* ar->codimen == 0 is a local array. */
4757 if (as->corank != ar->codimen && ar->codimen != 0)
4758 {
4759 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4760 &ar->where, ar->codimen, as->corank);
4761 return false;
4762 }
4763
4764 for (i = 0; i < as->rank; i++)
4765 if (!check_dimension (i, ar, as))
4766 return false;
4767
4768 /* Local access has no coarray spec. */
4769 if (ar->codimen != 0)
4770 for (i = as->rank; i < as->rank + as->corank; i++)
4771 {
4772 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4773 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4774 {
4775 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4776 i + 1 - as->rank, &ar->where);
4777 return false;
4778 }
4779 if (!check_dimension (i, ar, as))
4780 return false;
4781 }
4782
4783 return true;
4784 }
4785
4786
4787 /* Resolve one part of an array index. */
4788
4789 static bool
4790 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4791 int force_index_integer_kind)
4792 {
4793 gfc_typespec ts;
4794
4795 if (index == NULL)
4796 return true;
4797
4798 if (!gfc_resolve_expr (index))
4799 return false;
4800
4801 if (check_scalar && index->rank != 0)
4802 {
4803 gfc_error ("Array index at %L must be scalar", &index->where);
4804 return false;
4805 }
4806
4807 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4808 {
4809 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4810 &index->where, gfc_basic_typename (index->ts.type));
4811 return false;
4812 }
4813
4814 if (index->ts.type == BT_REAL)
4815 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4816 &index->where))
4817 return false;
4818
4819 if ((index->ts.kind != gfc_index_integer_kind
4820 && force_index_integer_kind)
4821 || index->ts.type != BT_INTEGER)
4822 {
4823 gfc_clear_ts (&ts);
4824 ts.type = BT_INTEGER;
4825 ts.kind = gfc_index_integer_kind;
4826
4827 gfc_convert_type_warn (index, &ts, 2, 0);
4828 }
4829
4830 return true;
4831 }
4832
4833 /* Resolve one part of an array index. */
4834
4835 bool
4836 gfc_resolve_index (gfc_expr *index, int check_scalar)
4837 {
4838 return gfc_resolve_index_1 (index, check_scalar, 1);
4839 }
4840
4841 /* Resolve a dim argument to an intrinsic function. */
4842
4843 bool
4844 gfc_resolve_dim_arg (gfc_expr *dim)
4845 {
4846 if (dim == NULL)
4847 return true;
4848
4849 if (!gfc_resolve_expr (dim))
4850 return false;
4851
4852 if (dim->rank != 0)
4853 {
4854 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4855 return false;
4856
4857 }
4858
4859 if (dim->ts.type != BT_INTEGER)
4860 {
4861 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4862 return false;
4863 }
4864
4865 if (dim->ts.kind != gfc_index_integer_kind)
4866 {
4867 gfc_typespec ts;
4868
4869 gfc_clear_ts (&ts);
4870 ts.type = BT_INTEGER;
4871 ts.kind = gfc_index_integer_kind;
4872
4873 gfc_convert_type_warn (dim, &ts, 2, 0);
4874 }
4875
4876 return true;
4877 }
4878
4879 /* Given an expression that contains array references, update those array
4880 references to point to the right array specifications. While this is
4881 filled in during matching, this information is difficult to save and load
4882 in a module, so we take care of it here.
4883
4884 The idea here is that the original array reference comes from the
4885 base symbol. We traverse the list of reference structures, setting
4886 the stored reference to references. Component references can
4887 provide an additional array specification. */
4888
4889 static void
4890 find_array_spec (gfc_expr *e)
4891 {
4892 gfc_array_spec *as;
4893 gfc_component *c;
4894 gfc_ref *ref;
4895 bool class_as = false;
4896
4897 if (e->symtree->n.sym->ts.type == BT_CLASS)
4898 {
4899 as = CLASS_DATA (e->symtree->n.sym)->as;
4900 class_as = true;
4901 }
4902 else
4903 as = e->symtree->n.sym->as;
4904
4905 for (ref = e->ref; ref; ref = ref->next)
4906 switch (ref->type)
4907 {
4908 case REF_ARRAY:
4909 if (as == NULL)
4910 gfc_internal_error ("find_array_spec(): Missing spec");
4911
4912 ref->u.ar.as = as;
4913 as = NULL;
4914 break;
4915
4916 case REF_COMPONENT:
4917 c = ref->u.c.component;
4918 if (c->attr.dimension)
4919 {
4920 if (as != NULL && !(class_as && as == c->as))
4921 gfc_internal_error ("find_array_spec(): unused as(1)");
4922 as = c->as;
4923 }
4924
4925 break;
4926
4927 case REF_SUBSTRING:
4928 case REF_INQUIRY:
4929 break;
4930 }
4931
4932 if (as != NULL)
4933 gfc_internal_error ("find_array_spec(): unused as(2)");
4934 }
4935
4936
4937 /* Resolve an array reference. */
4938
4939 static bool
4940 resolve_array_ref (gfc_array_ref *ar)
4941 {
4942 int i, check_scalar;
4943 gfc_expr *e;
4944
4945 for (i = 0; i < ar->dimen + ar->codimen; i++)
4946 {
4947 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4948
4949 /* Do not force gfc_index_integer_kind for the start. We can
4950 do fine with any integer kind. This avoids temporary arrays
4951 created for indexing with a vector. */
4952 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4953 return false;
4954 if (!gfc_resolve_index (ar->end[i], check_scalar))
4955 return false;
4956 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4957 return false;
4958
4959 e = ar->start[i];
4960
4961 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4962 switch (e->rank)
4963 {
4964 case 0:
4965 ar->dimen_type[i] = DIMEN_ELEMENT;
4966 break;
4967
4968 case 1:
4969 ar->dimen_type[i] = DIMEN_VECTOR;
4970 if (e->expr_type == EXPR_VARIABLE
4971 && e->symtree->n.sym->ts.type == BT_DERIVED)
4972 ar->start[i] = gfc_get_parentheses (e);
4973 break;
4974
4975 default:
4976 gfc_error ("Array index at %L is an array of rank %d",
4977 &ar->c_where[i], e->rank);
4978 return false;
4979 }
4980
4981 /* Fill in the upper bound, which may be lower than the
4982 specified one for something like a(2:10:5), which is
4983 identical to a(2:7:5). Only relevant for strides not equal
4984 to one. Don't try a division by zero. */
4985 if (ar->dimen_type[i] == DIMEN_RANGE
4986 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4987 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4988 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4989 {
4990 mpz_t size, end;
4991
4992 if (gfc_ref_dimen_size (ar, i, &size, &end))
4993 {
4994 if (ar->end[i] == NULL)
4995 {
4996 ar->end[i] =
4997 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4998 &ar->where);
4999 mpz_set (ar->end[i]->value.integer, end);
5000 }
5001 else if (ar->end[i]->ts.type == BT_INTEGER
5002 && ar->end[i]->expr_type == EXPR_CONSTANT)
5003 {
5004 mpz_set (ar->end[i]->value.integer, end);
5005 }
5006 else
5007 gcc_unreachable ();
5008
5009 mpz_clear (size);
5010 mpz_clear (end);
5011 }
5012 }
5013 }
5014
5015 if (ar->type == AR_FULL)
5016 {
5017 if (ar->as->rank == 0)
5018 ar->type = AR_ELEMENT;
5019
5020 /* Make sure array is the same as array(:,:), this way
5021 we don't need to special case all the time. */
5022 ar->dimen = ar->as->rank;
5023 for (i = 0; i < ar->dimen; i++)
5024 {
5025 ar->dimen_type[i] = DIMEN_RANGE;
5026
5027 gcc_assert (ar->start[i] == NULL);
5028 gcc_assert (ar->end[i] == NULL);
5029 gcc_assert (ar->stride[i] == NULL);
5030 }
5031 }
5032
5033 /* If the reference type is unknown, figure out what kind it is. */
5034
5035 if (ar->type == AR_UNKNOWN)
5036 {
5037 ar->type = AR_ELEMENT;
5038 for (i = 0; i < ar->dimen; i++)
5039 if (ar->dimen_type[i] == DIMEN_RANGE
5040 || ar->dimen_type[i] == DIMEN_VECTOR)
5041 {
5042 ar->type = AR_SECTION;
5043 break;
5044 }
5045 }
5046
5047 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
5048 return false;
5049
5050 if (ar->as->corank && ar->codimen == 0)
5051 {
5052 int n;
5053 ar->codimen = ar->as->corank;
5054 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
5055 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
5056 }
5057
5058 return true;
5059 }
5060
5061
5062 static bool
5063 resolve_substring (gfc_ref *ref, bool *equal_length)
5064 {
5065 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
5066
5067 if (ref->u.ss.start != NULL)
5068 {
5069 if (!gfc_resolve_expr (ref->u.ss.start))
5070 return false;
5071
5072 if (ref->u.ss.start->ts.type != BT_INTEGER)
5073 {
5074 gfc_error ("Substring start index at %L must be of type INTEGER",
5075 &ref->u.ss.start->where);
5076 return false;
5077 }
5078
5079 if (ref->u.ss.start->rank != 0)
5080 {
5081 gfc_error ("Substring start index at %L must be scalar",
5082 &ref->u.ss.start->where);
5083 return false;
5084 }
5085
5086 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
5087 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5088 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5089 {
5090 gfc_error ("Substring start index at %L is less than one",
5091 &ref->u.ss.start->where);
5092 return false;
5093 }
5094 }
5095
5096 if (ref->u.ss.end != NULL)
5097 {
5098 if (!gfc_resolve_expr (ref->u.ss.end))
5099 return false;
5100
5101 if (ref->u.ss.end->ts.type != BT_INTEGER)
5102 {
5103 gfc_error ("Substring end index at %L must be of type INTEGER",
5104 &ref->u.ss.end->where);
5105 return false;
5106 }
5107
5108 if (ref->u.ss.end->rank != 0)
5109 {
5110 gfc_error ("Substring end index at %L must be scalar",
5111 &ref->u.ss.end->where);
5112 return false;
5113 }
5114
5115 if (ref->u.ss.length != NULL
5116 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
5117 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5118 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5119 {
5120 gfc_error ("Substring end index at %L exceeds the string length",
5121 &ref->u.ss.start->where);
5122 return false;
5123 }
5124
5125 if (compare_bound_mpz_t (ref->u.ss.end,
5126 gfc_integer_kinds[k].huge) == CMP_GT
5127 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5128 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5129 {
5130 gfc_error ("Substring end index at %L is too large",
5131 &ref->u.ss.end->where);
5132 return false;
5133 }
5134 /* If the substring has the same length as the original
5135 variable, the reference itself can be deleted. */
5136
5137 if (ref->u.ss.length != NULL
5138 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
5139 && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
5140 *equal_length = true;
5141 }
5142
5143 return true;
5144 }
5145
5146
5147 /* This function supplies missing substring charlens. */
5148
5149 void
5150 gfc_resolve_substring_charlen (gfc_expr *e)
5151 {
5152 gfc_ref *char_ref;
5153 gfc_expr *start, *end;
5154 gfc_typespec *ts = NULL;
5155 mpz_t diff;
5156
5157 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
5158 {
5159 if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
5160 break;
5161 if (char_ref->type == REF_COMPONENT)
5162 ts = &char_ref->u.c.component->ts;
5163 }
5164
5165 if (!char_ref || char_ref->type == REF_INQUIRY)
5166 return;
5167
5168 gcc_assert (char_ref->next == NULL);
5169
5170 if (e->ts.u.cl)
5171 {
5172 if (e->ts.u.cl->length)
5173 gfc_free_expr (e->ts.u.cl->length);
5174 else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
5175 return;
5176 }
5177
5178 if (!e->ts.u.cl)
5179 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5180
5181 if (char_ref->u.ss.start)
5182 start = gfc_copy_expr (char_ref->u.ss.start);
5183 else
5184 start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
5185
5186 if (char_ref->u.ss.end)
5187 end = gfc_copy_expr (char_ref->u.ss.end);
5188 else if (e->expr_type == EXPR_VARIABLE)
5189 {
5190 if (!ts)
5191 ts = &e->symtree->n.sym->ts;
5192 end = gfc_copy_expr (ts->u.cl->length);
5193 }
5194 else
5195 end = NULL;
5196
5197 if (!start || !end)
5198 {
5199 gfc_free_expr (start);
5200 gfc_free_expr (end);
5201 return;
5202 }
5203
5204 /* Length = (end - start + 1).
5205 Check first whether it has a constant length. */
5206 if (gfc_dep_difference (end, start, &diff))
5207 {
5208 gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
5209 &e->where);
5210
5211 mpz_add_ui (len->value.integer, diff, 1);
5212 mpz_clear (diff);
5213 e->ts.u.cl->length = len;
5214 /* The check for length < 0 is handled below */
5215 }
5216 else
5217 {
5218 e->ts.u.cl->length = gfc_subtract (end, start);
5219 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
5220 gfc_get_int_expr (gfc_charlen_int_kind,
5221 NULL, 1));
5222 }
5223
5224 /* F2008, 6.4.1: Both the starting point and the ending point shall
5225 be within the range 1, 2, ..., n unless the starting point exceeds
5226 the ending point, in which case the substring has length zero. */
5227
5228 if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
5229 mpz_set_si (e->ts.u.cl->length->value.integer, 0);
5230
5231 e->ts.u.cl->length->ts.type = BT_INTEGER;
5232 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5233
5234 /* Make sure that the length is simplified. */
5235 gfc_simplify_expr (e->ts.u.cl->length, 1);
5236 gfc_resolve_expr (e->ts.u.cl->length);
5237 }
5238
5239
5240 /* Resolve subtype references. */
5241
5242 bool
5243 gfc_resolve_ref (gfc_expr *expr)
5244 {
5245 int current_part_dimension, n_components, seen_part_dimension, dim;
5246 gfc_ref *ref, **prev, *array_ref;
5247 bool equal_length;
5248
5249 for (ref = expr->ref; ref; ref = ref->next)
5250 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
5251 {
5252 find_array_spec (expr);
5253 break;
5254 }
5255
5256 for (prev = &expr->ref; *prev != NULL;
5257 prev = *prev == NULL ? prev : &(*prev)->next)
5258 switch ((*prev)->type)
5259 {
5260 case REF_ARRAY:
5261 if (!resolve_array_ref (&(*prev)->u.ar))
5262 return false;
5263 break;
5264
5265 case REF_COMPONENT:
5266 case REF_INQUIRY:
5267 break;
5268
5269 case REF_SUBSTRING:
5270 equal_length = false;
5271 if (!resolve_substring (*prev, &equal_length))
5272 return false;
5273
5274 if (expr->expr_type != EXPR_SUBSTRING && equal_length)
5275 {
5276 /* Remove the reference and move the charlen, if any. */
5277 ref = *prev;
5278 *prev = ref->next;
5279 ref->next = NULL;
5280 expr->ts.u.cl = ref->u.ss.length;
5281 ref->u.ss.length = NULL;
5282 gfc_free_ref_list (ref);
5283 }
5284 break;
5285 }
5286
5287 /* Check constraints on part references. */
5288
5289 current_part_dimension = 0;
5290 seen_part_dimension = 0;
5291 n_components = 0;
5292 array_ref = NULL;
5293
5294 for (ref = expr->ref; ref; ref = ref->next)
5295 {
5296 switch (ref->type)
5297 {
5298 case REF_ARRAY:
5299 array_ref = ref;
5300 switch (ref->u.ar.type)
5301 {
5302 case AR_FULL:
5303 /* Coarray scalar. */
5304 if (ref->u.ar.as->rank == 0)
5305 {
5306 current_part_dimension = 0;
5307 break;
5308 }
5309 /* Fall through. */
5310 case AR_SECTION:
5311 current_part_dimension = 1;
5312 break;
5313
5314 case AR_ELEMENT:
5315 array_ref = NULL;
5316 current_part_dimension = 0;
5317 break;
5318
5319 case AR_UNKNOWN:
5320 gfc_internal_error ("resolve_ref(): Bad array reference");
5321 }
5322
5323 break;
5324
5325 case REF_COMPONENT:
5326 if (current_part_dimension || seen_part_dimension)
5327 {
5328 /* F03:C614. */
5329 if (ref->u.c.component->attr.pointer
5330 || ref->u.c.component->attr.proc_pointer
5331 || (ref->u.c.component->ts.type == BT_CLASS
5332 && CLASS_DATA (ref->u.c.component)->attr.pointer))
5333 {
5334 gfc_error ("Component to the right of a part reference "
5335 "with nonzero rank must not have the POINTER "
5336 "attribute at %L", &expr->where);
5337 return false;
5338 }
5339 else if (ref->u.c.component->attr.allocatable
5340 || (ref->u.c.component->ts.type == BT_CLASS
5341 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
5342
5343 {
5344 gfc_error ("Component to the right of a part reference "
5345 "with nonzero rank must not have the ALLOCATABLE "
5346 "attribute at %L", &expr->where);
5347 return false;
5348 }
5349 }
5350
5351 n_components++;
5352 break;
5353
5354 case REF_SUBSTRING:
5355 break;
5356
5357 case REF_INQUIRY:
5358 /* Implement requirement in note 9.7 of F2018 that the result of the
5359 LEN inquiry be a scalar. */
5360 if (ref->u.i == INQUIRY_LEN && array_ref && expr->ts.deferred)
5361 {
5362 array_ref->u.ar.type = AR_ELEMENT;
5363 expr->rank = 0;
5364 /* INQUIRY_LEN is not evaluated from the rest of the expr
5365 but directly from the string length. This means that setting
5366 the array indices to one does not matter but might trigger
5367 a runtime bounds error. Suppress the check. */
5368 expr->no_bounds_check = 1;
5369 for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
5370 {
5371 array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
5372 if (array_ref->u.ar.start[dim])
5373 gfc_free_expr (array_ref->u.ar.start[dim]);
5374 array_ref->u.ar.start[dim]
5375 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
5376 if (array_ref->u.ar.end[dim])
5377 gfc_free_expr (array_ref->u.ar.end[dim]);
5378 if (array_ref->u.ar.stride[dim])
5379 gfc_free_expr (array_ref->u.ar.stride[dim]);
5380 }
5381 }
5382 break;
5383 }
5384
5385 if (((ref->type == REF_COMPONENT && n_components > 1)
5386 || ref->next == NULL)
5387 && current_part_dimension
5388 && seen_part_dimension)
5389 {
5390 gfc_error ("Two or more part references with nonzero rank must "
5391 "not be specified at %L", &expr->where);
5392 return false;
5393 }
5394
5395 if (ref->type == REF_COMPONENT)
5396 {
5397 if (current_part_dimension)
5398 seen_part_dimension = 1;
5399
5400 /* reset to make sure */
5401 current_part_dimension = 0;
5402 }
5403 }
5404
5405 return true;
5406 }
5407
5408
5409 /* Given an expression, determine its shape. This is easier than it sounds.
5410 Leaves the shape array NULL if it is not possible to determine the shape. */
5411
5412 static void
5413 expression_shape (gfc_expr *e)
5414 {
5415 mpz_t array[GFC_MAX_DIMENSIONS];
5416 int i;
5417
5418 if (e->rank <= 0 || e->shape != NULL)
5419 return;
5420
5421 for (i = 0; i < e->rank; i++)
5422 if (!gfc_array_dimen_size (e, i, &array[i]))
5423 goto fail;
5424
5425 e->shape = gfc_get_shape (e->rank);
5426
5427 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
5428
5429 return;
5430
5431 fail:
5432 for (i--; i >= 0; i--)
5433 mpz_clear (array[i]);
5434 }
5435
5436
5437 /* Given a variable expression node, compute the rank of the expression by
5438 examining the base symbol and any reference structures it may have. */
5439
5440 void
5441 gfc_expression_rank (gfc_expr *e)
5442 {
5443 gfc_ref *ref;
5444 int i, rank;
5445
5446 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
5447 could lead to serious confusion... */
5448 gcc_assert (e->expr_type != EXPR_COMPCALL);
5449
5450 if (e->ref == NULL)
5451 {
5452 if (e->expr_type == EXPR_ARRAY)
5453 goto done;
5454 /* Constructors can have a rank different from one via RESHAPE(). */
5455
5456 e->rank = ((e->symtree == NULL || e->symtree->n.sym->as == NULL)
5457 ? 0 : e->symtree->n.sym->as->rank);
5458 goto done;
5459 }
5460
5461 rank = 0;
5462
5463 for (ref = e->ref; ref; ref = ref->next)
5464 {
5465 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
5466 && ref->u.c.component->attr.function && !ref->next)
5467 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
5468
5469 if (ref->type != REF_ARRAY)
5470 continue;
5471
5472 if (ref->u.ar.type == AR_FULL)
5473 {
5474 rank = ref->u.ar.as->rank;
5475 break;
5476 }
5477
5478 if (ref->u.ar.type == AR_SECTION)
5479 {
5480 /* Figure out the rank of the section. */
5481 if (rank != 0)
5482 gfc_internal_error ("gfc_expression_rank(): Two array specs");
5483
5484 for (i = 0; i < ref->u.ar.dimen; i++)
5485 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5486 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
5487 rank++;
5488
5489 break;
5490 }
5491 }
5492
5493 e->rank = rank;
5494
5495 done:
5496 expression_shape (e);
5497 }
5498
5499
5500 static void
5501 add_caf_get_intrinsic (gfc_expr *e)
5502 {
5503 gfc_expr *wrapper, *tmp_expr;
5504 gfc_ref *ref;
5505 int n;
5506
5507 for (ref = e->ref; ref; ref = ref->next)
5508 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5509 break;
5510 if (ref == NULL)
5511 return;
5512
5513 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
5514 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
5515 return;
5516
5517 tmp_expr = XCNEW (gfc_expr);
5518 *tmp_expr = *e;
5519 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
5520 "caf_get", tmp_expr->where, 1, tmp_expr);
5521 wrapper->ts = e->ts;
5522 wrapper->rank = e->rank;
5523 if (e->rank)
5524 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
5525 *e = *wrapper;
5526 free (wrapper);
5527 }
5528
5529
5530 static void
5531 remove_caf_get_intrinsic (gfc_expr *e)
5532 {
5533 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym
5534 && e->value.function.isym->id == GFC_ISYM_CAF_GET);
5535 gfc_expr *e2 = e->value.function.actual->expr;
5536 e->value.function.actual->expr = NULL;
5537 gfc_free_actual_arglist (e->value.function.actual);
5538 gfc_free_shape (&e->shape, e->rank);
5539 *e = *e2;
5540 free (e2);
5541 }
5542
5543
5544 /* Resolve a variable expression. */
5545
5546 static bool
5547 resolve_variable (gfc_expr *e)
5548 {
5549 gfc_symbol *sym;
5550 bool t;
5551
5552 t = true;
5553
5554 if (e->symtree == NULL)
5555 return false;
5556 sym = e->symtree->n.sym;
5557
5558 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
5559 as ts.type is set to BT_ASSUMED in resolve_symbol. */
5560 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
5561 {
5562 if (!actual_arg || inquiry_argument)
5563 {
5564 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
5565 "be used as actual argument", sym->name, &e->where);
5566 return false;
5567 }
5568 }
5569 /* TS 29113, 407b. */
5570 else if (e->ts.type == BT_ASSUMED)
5571 {
5572 if (!actual_arg)
5573 {
5574 gfc_error ("Assumed-type variable %s at %L may only be used "
5575 "as actual argument", sym->name, &e->where);
5576 return false;
5577 }
5578 else if (inquiry_argument && !first_actual_arg)
5579 {
5580 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5581 for all inquiry functions in resolve_function; the reason is
5582 that the function-name resolution happens too late in that
5583 function. */
5584 gfc_error ("Assumed-type variable %s at %L as actual argument to "
5585 "an inquiry function shall be the first argument",
5586 sym->name, &e->where);
5587 return false;
5588 }
5589 }
5590 /* TS 29113, C535b. */
5591 else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5592 && sym->ts.u.derived && CLASS_DATA (sym)
5593 && CLASS_DATA (sym)->as
5594 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5595 || (sym->ts.type != BT_CLASS && sym->as
5596 && sym->as->type == AS_ASSUMED_RANK))
5597 && !sym->attr.select_rank_temporary)
5598 {
5599 if (!actual_arg
5600 && !(cs_base && cs_base->current
5601 && cs_base->current->op == EXEC_SELECT_RANK))
5602 {
5603 gfc_error ("Assumed-rank variable %s at %L may only be used as "
5604 "actual argument", sym->name, &e->where);
5605 return false;
5606 }
5607 else if (inquiry_argument && !first_actual_arg)
5608 {
5609 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5610 for all inquiry functions in resolve_function; the reason is
5611 that the function-name resolution happens too late in that
5612 function. */
5613 gfc_error ("Assumed-rank variable %s at %L as actual argument "
5614 "to an inquiry function shall be the first argument",
5615 sym->name, &e->where);
5616 return false;
5617 }
5618 }
5619
5620 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
5621 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5622 && e->ref->next == NULL))
5623 {
5624 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
5625 "a subobject reference", sym->name, &e->ref->u.ar.where);
5626 return false;
5627 }
5628 /* TS 29113, 407b. */
5629 else if (e->ts.type == BT_ASSUMED && e->ref
5630 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5631 && e->ref->next == NULL))
5632 {
5633 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
5634 "reference", sym->name, &e->ref->u.ar.where);
5635 return false;
5636 }
5637
5638 /* TS 29113, C535b. */
5639 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5640 && sym->ts.u.derived && CLASS_DATA (sym)
5641 && CLASS_DATA (sym)->as
5642 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5643 || (sym->ts.type != BT_CLASS && sym->as
5644 && sym->as->type == AS_ASSUMED_RANK))
5645 && e->ref
5646 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5647 && e->ref->next == NULL))
5648 {
5649 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
5650 "reference", sym->name, &e->ref->u.ar.where);
5651 return false;
5652 }
5653
5654 /* For variables that are used in an associate (target => object) where
5655 the object's basetype is array valued while the target is scalar,
5656 the ts' type of the component refs is still array valued, which
5657 can't be translated that way. */
5658 if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
5659 && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
5660 && CLASS_DATA (sym->assoc->target)->as)
5661 {
5662 gfc_ref *ref = e->ref;
5663 while (ref)
5664 {
5665 switch (ref->type)
5666 {
5667 case REF_COMPONENT:
5668 ref->u.c.sym = sym->ts.u.derived;
5669 /* Stop the loop. */
5670 ref = NULL;
5671 break;
5672 default:
5673 ref = ref->next;
5674 break;
5675 }
5676 }
5677 }
5678
5679 /* If this is an associate-name, it may be parsed with an array reference
5680 in error even though the target is scalar. Fail directly in this case.
5681 TODO Understand why class scalar expressions must be excluded. */
5682 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
5683 {
5684 if (sym->ts.type == BT_CLASS)
5685 gfc_fix_class_refs (e);
5686 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
5687 return false;
5688 else if (sym->attr.dimension && (!e->ref || e->ref->type != REF_ARRAY))
5689 {
5690 /* This can happen because the parser did not detect that the
5691 associate name is an array and the expression had no array
5692 part_ref. */
5693 gfc_ref *ref = gfc_get_ref ();
5694 ref->type = REF_ARRAY;
5695 ref->u.ar = *gfc_get_array_ref();
5696 ref->u.ar.type = AR_FULL;
5697 if (sym->as)
5698 {
5699 ref->u.ar.as = sym->as;
5700 ref->u.ar.dimen = sym->as->rank;
5701 }
5702 ref->next = e->ref;
5703 e->ref = ref;
5704
5705 }
5706 }
5707
5708 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
5709 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
5710
5711 /* On the other hand, the parser may not have known this is an array;
5712 in this case, we have to add a FULL reference. */
5713 if (sym->assoc && sym->attr.dimension && !e->ref)
5714 {
5715 e->ref = gfc_get_ref ();
5716 e->ref->type = REF_ARRAY;
5717 e->ref->u.ar.type = AR_FULL;
5718 e->ref->u.ar.dimen = 0;
5719 }
5720
5721 /* Like above, but for class types, where the checking whether an array
5722 ref is present is more complicated. Furthermore make sure not to add
5723 the full array ref to _vptr or _len refs. */
5724 if (sym->assoc && sym->ts.type == BT_CLASS
5725 && CLASS_DATA (sym)->attr.dimension
5726 && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
5727 {
5728 gfc_ref *ref, *newref;
5729
5730 newref = gfc_get_ref ();
5731 newref->type = REF_ARRAY;
5732 newref->u.ar.type = AR_FULL;
5733 newref->u.ar.dimen = 0;
5734 /* Because this is an associate var and the first ref either is a ref to
5735 the _data component or not, no traversal of the ref chain is
5736 needed. The array ref needs to be inserted after the _data ref,
5737 or when that is not present, which may happend for polymorphic
5738 types, then at the first position. */
5739 ref = e->ref;
5740 if (!ref)
5741 e->ref = newref;
5742 else if (ref->type == REF_COMPONENT
5743 && strcmp ("_data", ref->u.c.component->name) == 0)
5744 {
5745 if (!ref->next || ref->next->type != REF_ARRAY)
5746 {
5747 newref->next = ref->next;
5748 ref->next = newref;
5749 }
5750 else
5751 /* Array ref present already. */
5752 gfc_free_ref_list (newref);
5753 }
5754 else if (ref->type == REF_ARRAY)
5755 /* Array ref present already. */
5756 gfc_free_ref_list (newref);
5757 else
5758 {
5759 newref->next = ref;
5760 e->ref = newref;
5761 }
5762 }
5763
5764 if (e->ref && !gfc_resolve_ref (e))
5765 return false;
5766
5767 if (sym->attr.flavor == FL_PROCEDURE
5768 && (!sym->attr.function
5769 || (sym->attr.function && sym->result
5770 && sym->result->attr.proc_pointer
5771 && !sym->result->attr.function)))
5772 {
5773 e->ts.type = BT_PROCEDURE;
5774 goto resolve_procedure;
5775 }
5776
5777 if (sym->ts.type != BT_UNKNOWN)
5778 gfc_variable_attr (e, &e->ts);
5779 else if (sym->attr.flavor == FL_PROCEDURE
5780 && sym->attr.function && sym->result
5781 && sym->result->ts.type != BT_UNKNOWN
5782 && sym->result->attr.proc_pointer)
5783 e->ts = sym->result->ts;
5784 else
5785 {
5786 /* Must be a simple variable reference. */
5787 if (!gfc_set_default_type (sym, 1, sym->ns))
5788 return false;
5789 e->ts = sym->ts;
5790 }
5791
5792 if (check_assumed_size_reference (sym, e))
5793 return false;
5794
5795 /* Deal with forward references to entries during gfc_resolve_code, to
5796 satisfy, at least partially, 12.5.2.5. */
5797 if (gfc_current_ns->entries
5798 && current_entry_id == sym->entry_id
5799 && cs_base
5800 && cs_base->current
5801 && cs_base->current->op != EXEC_ENTRY)
5802 {
5803 gfc_entry_list *entry;
5804 gfc_formal_arglist *formal;
5805 int n;
5806 bool seen, saved_specification_expr;
5807
5808 /* If the symbol is a dummy... */
5809 if (sym->attr.dummy && sym->ns == gfc_current_ns)
5810 {
5811 entry = gfc_current_ns->entries;
5812 seen = false;
5813
5814 /* ...test if the symbol is a parameter of previous entries. */
5815 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5816 for (formal = entry->sym->formal; formal; formal = formal->next)
5817 {
5818 if (formal->sym && sym->name == formal->sym->name)
5819 {
5820 seen = true;
5821 break;
5822 }
5823 }
5824
5825 /* If it has not been seen as a dummy, this is an error. */
5826 if (!seen)
5827 {
5828 if (specification_expr)
5829 gfc_error ("Variable %qs, used in a specification expression"
5830 ", is referenced at %L before the ENTRY statement "
5831 "in which it is a parameter",
5832 sym->name, &cs_base->current->loc);
5833 else
5834 gfc_error ("Variable %qs is used at %L before the ENTRY "
5835 "statement in which it is a parameter",
5836 sym->name, &cs_base->current->loc);
5837 t = false;
5838 }
5839 }
5840
5841 /* Now do the same check on the specification expressions. */
5842 saved_specification_expr = specification_expr;
5843 specification_expr = true;
5844 if (sym->ts.type == BT_CHARACTER
5845 && !gfc_resolve_expr (sym->ts.u.cl->length))
5846 t = false;
5847
5848 if (sym->as)
5849 for (n = 0; n < sym->as->rank; n++)
5850 {
5851 if (!gfc_resolve_expr (sym->as->lower[n]))
5852 t = false;
5853 if (!gfc_resolve_expr (sym->as->upper[n]))
5854 t = false;
5855 }
5856 specification_expr = saved_specification_expr;
5857
5858 if (t)
5859 /* Update the symbol's entry level. */
5860 sym->entry_id = current_entry_id + 1;
5861 }
5862
5863 /* If a symbol has been host_associated mark it. This is used latter,
5864 to identify if aliasing is possible via host association. */
5865 if (sym->attr.flavor == FL_VARIABLE
5866 && gfc_current_ns->parent
5867 && (gfc_current_ns->parent == sym->ns
5868 || (gfc_current_ns->parent->parent
5869 && gfc_current_ns->parent->parent == sym->ns)))
5870 sym->attr.host_assoc = 1;
5871
5872 if (gfc_current_ns->proc_name
5873 && sym->attr.dimension
5874 && (sym->ns != gfc_current_ns
5875 || sym->attr.use_assoc
5876 || sym->attr.in_common))
5877 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
5878
5879 resolve_procedure:
5880 if (t && !resolve_procedure_expression (e))
5881 t = false;
5882
5883 /* F2008, C617 and C1229. */
5884 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5885 && gfc_is_coindexed (e))
5886 {
5887 gfc_ref *ref, *ref2 = NULL;
5888
5889 for (ref = e->ref; ref; ref = ref->next)
5890 {
5891 if (ref->type == REF_COMPONENT)
5892 ref2 = ref;
5893 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5894 break;
5895 }
5896
5897 for ( ; ref; ref = ref->next)
5898 if (ref->type == REF_COMPONENT)
5899 break;
5900
5901 /* Expression itself is not coindexed object. */
5902 if (ref && e->ts.type == BT_CLASS)
5903 {
5904 gfc_error ("Polymorphic subobject of coindexed object at %L",
5905 &e->where);
5906 t = false;
5907 }
5908
5909 /* Expression itself is coindexed object. */
5910 if (ref == NULL)
5911 {
5912 gfc_component *c;
5913 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5914 for ( ; c; c = c->next)
5915 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5916 {
5917 gfc_error ("Coindexed object with polymorphic allocatable "
5918 "subcomponent at %L", &e->where);
5919 t = false;
5920 break;
5921 }
5922 }
5923 }
5924
5925 if (t)
5926 gfc_expression_rank (e);
5927
5928 if (t && flag_coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e))
5929 add_caf_get_intrinsic (e);
5930
5931 if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym != sym->result)
5932 gfc_warning (OPT_Wdeprecated_declarations,
5933 "Using variable %qs at %L is deprecated",
5934 sym->name, &e->where);
5935 /* Simplify cases where access to a parameter array results in a
5936 single constant. Suppress errors since those will have been
5937 issued before, as warnings. */
5938 if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
5939 {
5940 gfc_push_suppress_errors ();
5941 gfc_simplify_expr (e, 1);
5942 gfc_pop_suppress_errors ();
5943 }
5944
5945 return t;
5946 }
5947
5948
5949 /* Checks to see that the correct symbol has been host associated.
5950 The only situation where this arises is that in which a twice
5951 contained function is parsed after the host association is made.
5952 Therefore, on detecting this, change the symbol in the expression
5953 and convert the array reference into an actual arglist if the old
5954 symbol is a variable. */
5955 static bool
5956 check_host_association (gfc_expr *e)
5957 {
5958 gfc_symbol *sym, *old_sym;
5959 gfc_symtree *st;
5960 int n;
5961 gfc_ref *ref;
5962 gfc_actual_arglist *arg, *tail = NULL;
5963 bool retval = e->expr_type == EXPR_FUNCTION;
5964
5965 /* If the expression is the result of substitution in
5966 interface.c(gfc_extend_expr) because there is no way in
5967 which the host association can be wrong. */
5968 if (e->symtree == NULL
5969 || e->symtree->n.sym == NULL
5970 || e->user_operator)
5971 return retval;
5972
5973 old_sym = e->symtree->n.sym;
5974
5975 if (gfc_current_ns->parent
5976 && old_sym->ns != gfc_current_ns)
5977 {
5978 /* Use the 'USE' name so that renamed module symbols are
5979 correctly handled. */
5980 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5981
5982 if (sym && old_sym != sym
5983 && sym->ts.type == old_sym->ts.type
5984 && sym->attr.flavor == FL_PROCEDURE
5985 && sym->attr.contained)
5986 {
5987 /* Clear the shape, since it might not be valid. */
5988 gfc_free_shape (&e->shape, e->rank);
5989
5990 /* Give the expression the right symtree! */
5991 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5992 gcc_assert (st != NULL);
5993
5994 if (old_sym->attr.flavor == FL_PROCEDURE
5995 || e->expr_type == EXPR_FUNCTION)
5996 {
5997 /* Original was function so point to the new symbol, since
5998 the actual argument list is already attached to the
5999 expression. */
6000 e->value.function.esym = NULL;
6001 e->symtree = st;
6002 }
6003 else
6004 {
6005 /* Original was variable so convert array references into
6006 an actual arglist. This does not need any checking now
6007 since resolve_function will take care of it. */
6008 e->value.function.actual = NULL;
6009 e->expr_type = EXPR_FUNCTION;
6010 e->symtree = st;
6011
6012 /* Ambiguity will not arise if the array reference is not
6013 the last reference. */
6014 for (ref = e->ref; ref; ref = ref->next)
6015 if (ref->type == REF_ARRAY && ref->next == NULL)
6016 break;
6017
6018 if ((ref == NULL || ref->type != REF_ARRAY)
6019 && sym->attr.proc == PROC_INTERNAL)
6020 {
6021 gfc_error ("%qs at %L is host associated at %L into "
6022 "a contained procedure with an internal "
6023 "procedure of the same name", sym->name,
6024 &old_sym->declared_at, &e->where);
6025 return false;
6026 }
6027
6028 gcc_assert (ref->type == REF_ARRAY);
6029
6030 /* Grab the start expressions from the array ref and
6031 copy them into actual arguments. */
6032 for (n = 0; n < ref->u.ar.dimen; n++)
6033 {
6034 arg = gfc_get_actual_arglist ();
6035 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
6036 if (e->value.function.actual == NULL)
6037 tail = e->value.function.actual = arg;
6038 else
6039 {
6040 tail->next = arg;
6041 tail = arg;
6042 }
6043 }
6044
6045 /* Dump the reference list and set the rank. */
6046 gfc_free_ref_list (e->ref);
6047 e->ref = NULL;
6048 e->rank = sym->as ? sym->as->rank : 0;
6049 }
6050
6051 gfc_resolve_expr (e);
6052 sym->refs++;
6053 }
6054 }
6055 /* This might have changed! */
6056 return e->expr_type == EXPR_FUNCTION;
6057 }
6058
6059
6060 static void
6061 gfc_resolve_character_operator (gfc_expr *e)
6062 {
6063 gfc_expr *op1 = e->value.op.op1;
6064 gfc_expr *op2 = e->value.op.op2;
6065 gfc_expr *e1 = NULL;
6066 gfc_expr *e2 = NULL;
6067
6068 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
6069
6070 if (op1->ts.u.cl && op1->ts.u.cl->length)
6071 e1 = gfc_copy_expr (op1->ts.u.cl->length);
6072 else if (op1->expr_type == EXPR_CONSTANT)
6073 e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6074 op1->value.character.length);
6075
6076 if (op2->ts.u.cl && op2->ts.u.cl->length)
6077 e2 = gfc_copy_expr (op2->ts.u.cl->length);
6078 else if (op2->expr_type == EXPR_CONSTANT)
6079 e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6080 op2->value.character.length);
6081
6082 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6083
6084 if (!e1 || !e2)
6085 {
6086 gfc_free_expr (e1);
6087 gfc_free_expr (e2);
6088
6089 return;
6090 }
6091
6092 e->ts.u.cl->length = gfc_add (e1, e2);
6093 e->ts.u.cl->length->ts.type = BT_INTEGER;
6094 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
6095 gfc_simplify_expr (e->ts.u.cl->length, 0);
6096 gfc_resolve_expr (e->ts.u.cl->length);
6097
6098 return;
6099 }
6100
6101
6102 /* Ensure that an character expression has a charlen and, if possible, a
6103 length expression. */
6104
6105 static void
6106 fixup_charlen (gfc_expr *e)
6107 {
6108 /* The cases fall through so that changes in expression type and the need
6109 for multiple fixes are picked up. In all circumstances, a charlen should
6110 be available for the middle end to hang a backend_decl on. */
6111 switch (e->expr_type)
6112 {
6113 case EXPR_OP:
6114 gfc_resolve_character_operator (e);
6115 /* FALLTHRU */
6116
6117 case EXPR_ARRAY:
6118 if (e->expr_type == EXPR_ARRAY)
6119 gfc_resolve_character_array_constructor (e);
6120 /* FALLTHRU */
6121
6122 case EXPR_SUBSTRING:
6123 if (!e->ts.u.cl && e->ref)
6124 gfc_resolve_substring_charlen (e);
6125 /* FALLTHRU */
6126
6127 default:
6128 if (!e->ts.u.cl)
6129 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6130
6131 break;
6132 }
6133 }
6134
6135
6136 /* Update an actual argument to include the passed-object for type-bound
6137 procedures at the right position. */
6138
6139 static gfc_actual_arglist*
6140 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
6141 const char *name)
6142 {
6143 gcc_assert (argpos > 0);
6144
6145 if (argpos == 1)
6146 {
6147 gfc_actual_arglist* result;
6148
6149 result = gfc_get_actual_arglist ();
6150 result->expr = po;
6151 result->next = lst;
6152 if (name)
6153 result->name = name;
6154
6155 return result;
6156 }
6157
6158 if (lst)
6159 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
6160 else
6161 lst = update_arglist_pass (NULL, po, argpos - 1, name);
6162 return lst;
6163 }
6164
6165
6166 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
6167
6168 static gfc_expr*
6169 extract_compcall_passed_object (gfc_expr* e)
6170 {
6171 gfc_expr* po;
6172
6173 if (e->expr_type == EXPR_UNKNOWN)
6174 {
6175 gfc_error ("Error in typebound call at %L",
6176 &e->where);
6177 return NULL;
6178 }
6179
6180 gcc_assert (e->expr_type == EXPR_COMPCALL);
6181
6182 if (e->value.compcall.base_object)
6183 po = gfc_copy_expr (e->value.compcall.base_object);
6184 else
6185 {
6186 po = gfc_get_expr ();
6187 po->expr_type = EXPR_VARIABLE;
6188 po->symtree = e->symtree;
6189 po->ref = gfc_copy_ref (e->ref);
6190 po->where = e->where;
6191 }
6192
6193 if (!gfc_resolve_expr (po))
6194 return NULL;
6195
6196 return po;
6197 }
6198
6199
6200 /* Update the arglist of an EXPR_COMPCALL expression to include the
6201 passed-object. */
6202
6203 static bool
6204 update_compcall_arglist (gfc_expr* e)
6205 {
6206 gfc_expr* po;
6207 gfc_typebound_proc* tbp;
6208
6209 tbp = e->value.compcall.tbp;
6210
6211 if (tbp->error)
6212 return false;
6213
6214 po = extract_compcall_passed_object (e);
6215 if (!po)
6216 return false;
6217
6218 if (tbp->nopass || e->value.compcall.ignore_pass)
6219 {
6220 gfc_free_expr (po);
6221 return true;
6222 }
6223
6224 if (tbp->pass_arg_num <= 0)
6225 return false;
6226
6227 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
6228 tbp->pass_arg_num,
6229 tbp->pass_arg);
6230
6231 return true;
6232 }
6233
6234
6235 /* Extract the passed object from a PPC call (a copy of it). */
6236
6237 static gfc_expr*
6238 extract_ppc_passed_object (gfc_expr *e)
6239 {
6240 gfc_expr *po;
6241 gfc_ref **ref;
6242
6243 po = gfc_get_expr ();
6244 po->expr_type = EXPR_VARIABLE;
6245 po->symtree = e->symtree;
6246 po->ref = gfc_copy_ref (e->ref);
6247 po->where = e->where;
6248
6249 /* Remove PPC reference. */
6250 ref = &po->ref;
6251 while ((*ref)->next)
6252 ref = &(*ref)->next;
6253 gfc_free_ref_list (*ref);
6254 *ref = NULL;
6255
6256 if (!gfc_resolve_expr (po))
6257 return NULL;
6258
6259 return po;
6260 }
6261
6262
6263 /* Update the actual arglist of a procedure pointer component to include the
6264 passed-object. */
6265
6266 static bool
6267 update_ppc_arglist (gfc_expr* e)
6268 {
6269 gfc_expr* po;
6270 gfc_component *ppc;
6271 gfc_typebound_proc* tb;
6272
6273 ppc = gfc_get_proc_ptr_comp (e);
6274 if (!ppc)
6275 return false;
6276
6277 tb = ppc->tb;
6278
6279 if (tb->error)
6280 return false;
6281 else if (tb->nopass)
6282 return true;
6283
6284 po = extract_ppc_passed_object (e);
6285 if (!po)
6286 return false;
6287
6288 /* F08:R739. */
6289 if (po->rank != 0)
6290 {
6291 gfc_error ("Passed-object at %L must be scalar", &e->where);
6292 return false;
6293 }
6294
6295 /* F08:C611. */
6296 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
6297 {
6298 gfc_error ("Base object for procedure-pointer component call at %L is of"
6299 " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
6300 return false;
6301 }
6302
6303 gcc_assert (tb->pass_arg_num > 0);
6304 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
6305 tb->pass_arg_num,
6306 tb->pass_arg);
6307
6308 return true;
6309 }
6310
6311
6312 /* Check that the object a TBP is called on is valid, i.e. it must not be
6313 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
6314
6315 static bool
6316 check_typebound_baseobject (gfc_expr* e)
6317 {
6318 gfc_expr* base;
6319 bool return_value = false;
6320
6321 base = extract_compcall_passed_object (e);
6322 if (!base)
6323 return false;
6324
6325 if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
6326 {
6327 gfc_error ("Error in typebound call at %L", &e->where);
6328 goto cleanup;
6329 }
6330
6331 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
6332 return false;
6333
6334 /* F08:C611. */
6335 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
6336 {
6337 gfc_error ("Base object for type-bound procedure call at %L is of"
6338 " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
6339 goto cleanup;
6340 }
6341
6342 /* F08:C1230. If the procedure called is NOPASS,
6343 the base object must be scalar. */
6344 if (e->value.compcall.tbp->nopass && base->rank != 0)
6345 {
6346 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
6347 " be scalar", &e->where);
6348 goto cleanup;
6349 }
6350
6351 return_value = true;
6352
6353 cleanup:
6354 gfc_free_expr (base);
6355 return return_value;
6356 }
6357
6358
6359 /* Resolve a call to a type-bound procedure, either function or subroutine,
6360 statically from the data in an EXPR_COMPCALL expression. The adapted
6361 arglist and the target-procedure symtree are returned. */
6362
6363 static bool
6364 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
6365 gfc_actual_arglist** actual)
6366 {
6367 gcc_assert (e->expr_type == EXPR_COMPCALL);
6368 gcc_assert (!e->value.compcall.tbp->is_generic);
6369
6370 /* Update the actual arglist for PASS. */
6371 if (!update_compcall_arglist (e))
6372 return false;
6373
6374 *actual = e->value.compcall.actual;
6375 *target = e->value.compcall.tbp->u.specific;
6376
6377 gfc_free_ref_list (e->ref);
6378 e->ref = NULL;
6379 e->value.compcall.actual = NULL;
6380
6381 /* If we find a deferred typebound procedure, check for derived types
6382 that an overriding typebound procedure has not been missed. */
6383 if (e->value.compcall.name
6384 && !e->value.compcall.tbp->non_overridable
6385 && e->value.compcall.base_object
6386 && e->value.compcall.base_object->ts.type == BT_DERIVED)
6387 {
6388 gfc_symtree *st;
6389 gfc_symbol *derived;
6390
6391 /* Use the derived type of the base_object. */
6392 derived = e->value.compcall.base_object->ts.u.derived;
6393 st = NULL;
6394
6395 /* If necessary, go through the inheritance chain. */
6396 while (!st && derived)
6397 {
6398 /* Look for the typebound procedure 'name'. */
6399 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
6400 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
6401 e->value.compcall.name);
6402 if (!st)
6403 derived = gfc_get_derived_super_type (derived);
6404 }
6405
6406 /* Now find the specific name in the derived type namespace. */
6407 if (st && st->n.tb && st->n.tb->u.specific)
6408 gfc_find_sym_tree (st->n.tb->u.specific->name,
6409 derived->ns, 1, &st);
6410 if (st)
6411 *target = st;
6412 }
6413 return true;
6414 }
6415
6416
6417 /* Get the ultimate declared type from an expression. In addition,
6418 return the last class/derived type reference and the copy of the
6419 reference list. If check_types is set true, derived types are
6420 identified as well as class references. */
6421 static gfc_symbol*
6422 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
6423 gfc_expr *e, bool check_types)
6424 {
6425 gfc_symbol *declared;
6426 gfc_ref *ref;
6427
6428 declared = NULL;
6429 if (class_ref)
6430 *class_ref = NULL;
6431 if (new_ref)
6432 *new_ref = gfc_copy_ref (e->ref);
6433
6434 for (ref = e->ref; ref; ref = ref->next)
6435 {
6436 if (ref->type != REF_COMPONENT)
6437 continue;
6438
6439 if ((ref->u.c.component->ts.type == BT_CLASS
6440 || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
6441 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
6442 {
6443 declared = ref->u.c.component->ts.u.derived;
6444 if (class_ref)
6445 *class_ref = ref;
6446 }
6447 }
6448
6449 if (declared == NULL)
6450 declared = e->symtree->n.sym->ts.u.derived;
6451
6452 return declared;
6453 }
6454
6455
6456 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
6457 which of the specific bindings (if any) matches the arglist and transform
6458 the expression into a call of that binding. */
6459
6460 static bool
6461 resolve_typebound_generic_call (gfc_expr* e, const char **name)
6462 {
6463 gfc_typebound_proc* genproc;
6464 const char* genname;
6465 gfc_symtree *st;
6466 gfc_symbol *derived;
6467
6468 gcc_assert (e->expr_type == EXPR_COMPCALL);
6469 genname = e->value.compcall.name;
6470 genproc = e->value.compcall.tbp;
6471
6472 if (!genproc->is_generic)
6473 return true;
6474
6475 /* Try the bindings on this type and in the inheritance hierarchy. */
6476 for (; genproc; genproc = genproc->overridden)
6477 {
6478 gfc_tbp_generic* g;
6479
6480 gcc_assert (genproc->is_generic);
6481 for (g = genproc->u.generic; g; g = g->next)
6482 {
6483 gfc_symbol* target;
6484 gfc_actual_arglist* args;
6485 bool matches;
6486
6487 gcc_assert (g->specific);
6488
6489 if (g->specific->error)
6490 continue;
6491
6492 target = g->specific->u.specific->n.sym;
6493
6494 /* Get the right arglist by handling PASS/NOPASS. */
6495 args = gfc_copy_actual_arglist (e->value.compcall.actual);
6496 if (!g->specific->nopass)
6497 {
6498 gfc_expr* po;
6499 po = extract_compcall_passed_object (e);
6500 if (!po)
6501 {
6502 gfc_free_actual_arglist (args);
6503 return false;
6504 }
6505
6506 gcc_assert (g->specific->pass_arg_num > 0);
6507 gcc_assert (!g->specific->error);
6508 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
6509 g->specific->pass_arg);
6510 }
6511 resolve_actual_arglist (args, target->attr.proc,
6512 is_external_proc (target)
6513 && gfc_sym_get_dummy_args (target) == NULL);
6514
6515 /* Check if this arglist matches the formal. */
6516 matches = gfc_arglist_matches_symbol (&args, target);
6517
6518 /* Clean up and break out of the loop if we've found it. */
6519 gfc_free_actual_arglist (args);
6520 if (matches)
6521 {
6522 e->value.compcall.tbp = g->specific;
6523 genname = g->specific_st->name;
6524 /* Pass along the name for CLASS methods, where the vtab
6525 procedure pointer component has to be referenced. */
6526 if (name)
6527 *name = genname;
6528 goto success;
6529 }
6530 }
6531 }
6532
6533 /* Nothing matching found! */
6534 gfc_error ("Found no matching specific binding for the call to the GENERIC"
6535 " %qs at %L", genname, &e->where);
6536 return false;
6537
6538 success:
6539 /* Make sure that we have the right specific instance for the name. */
6540 derived = get_declared_from_expr (NULL, NULL, e, true);
6541
6542 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
6543 if (st)
6544 e->value.compcall.tbp = st->n.tb;
6545
6546 return true;
6547 }
6548
6549
6550 /* Resolve a call to a type-bound subroutine. */
6551
6552 static bool
6553 resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
6554 {
6555 gfc_actual_arglist* newactual;
6556 gfc_symtree* target;
6557
6558 /* Check that's really a SUBROUTINE. */
6559 if (!c->expr1->value.compcall.tbp->subroutine)
6560 {
6561 if (!c->expr1->value.compcall.tbp->is_generic
6562 && c->expr1->value.compcall.tbp->u.specific
6563 && c->expr1->value.compcall.tbp->u.specific->n.sym
6564 && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
6565 c->expr1->value.compcall.tbp->subroutine = 1;
6566 else
6567 {
6568 gfc_error ("%qs at %L should be a SUBROUTINE",
6569 c->expr1->value.compcall.name, &c->loc);
6570 return false;
6571 }
6572 }
6573
6574 if (!check_typebound_baseobject (c->expr1))
6575 return false;
6576
6577 /* Pass along the name for CLASS methods, where the vtab
6578 procedure pointer component has to be referenced. */
6579 if (name)
6580 *name = c->expr1->value.compcall.name;
6581
6582 if (!resolve_typebound_generic_call (c->expr1, name))
6583 return false;
6584
6585 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
6586 if (overridable)
6587 *overridable = !c->expr1->value.compcall.tbp->non_overridable;
6588
6589 /* Transform into an ordinary EXEC_CALL for now. */
6590
6591 if (!resolve_typebound_static (c->expr1, &target, &newactual))
6592 return false;
6593
6594 c->ext.actual = newactual;
6595 c->symtree = target;
6596 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
6597
6598 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
6599
6600 gfc_free_expr (c->expr1);
6601 c->expr1 = gfc_get_expr ();
6602 c->expr1->expr_type = EXPR_FUNCTION;
6603 c->expr1->symtree = target;
6604 c->expr1->where = c->loc;
6605
6606 return resolve_call (c);
6607 }
6608
6609
6610 /* Resolve a component-call expression. */
6611 static bool
6612 resolve_compcall (gfc_expr* e, const char **name)
6613 {
6614 gfc_actual_arglist* newactual;
6615 gfc_symtree* target;
6616
6617 /* Check that's really a FUNCTION. */
6618 if (!e->value.compcall.tbp->function)
6619 {
6620 gfc_error ("%qs at %L should be a FUNCTION",
6621 e->value.compcall.name, &e->where);
6622 return false;
6623 }
6624
6625
6626 /* These must not be assign-calls! */
6627 gcc_assert (!e->value.compcall.assign);
6628
6629 if (!check_typebound_baseobject (e))
6630 return false;
6631
6632 /* Pass along the name for CLASS methods, where the vtab
6633 procedure pointer component has to be referenced. */
6634 if (name)
6635 *name = e->value.compcall.name;
6636
6637 if (!resolve_typebound_generic_call (e, name))
6638 return false;
6639 gcc_assert (!e->value.compcall.tbp->is_generic);
6640
6641 /* Take the rank from the function's symbol. */
6642 if (e->value.compcall.tbp->u.specific->n.sym->as)
6643 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
6644
6645 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
6646 arglist to the TBP's binding target. */
6647
6648 if (!resolve_typebound_static (e, &target, &newactual))
6649 return false;
6650
6651 e->value.function.actual = newactual;
6652 e->value.function.name = NULL;
6653 e->value.function.esym = target->n.sym;
6654 e->value.function.isym = NULL;
6655 e->symtree = target;
6656 e->ts = target->n.sym->ts;
6657 e->expr_type = EXPR_FUNCTION;
6658
6659 /* Resolution is not necessary if this is a class subroutine; this
6660 function only has to identify the specific proc. Resolution of
6661 the call will be done next in resolve_typebound_call. */
6662 return gfc_resolve_expr (e);
6663 }
6664
6665
6666 static bool resolve_fl_derived (gfc_symbol *sym);
6667
6668
6669 /* Resolve a typebound function, or 'method'. First separate all
6670 the non-CLASS references by calling resolve_compcall directly. */
6671
6672 static bool
6673 resolve_typebound_function (gfc_expr* e)
6674 {
6675 gfc_symbol *declared;
6676 gfc_component *c;
6677 gfc_ref *new_ref;
6678 gfc_ref *class_ref;
6679 gfc_symtree *st;
6680 const char *name;
6681 gfc_typespec ts;
6682 gfc_expr *expr;
6683 bool overridable;
6684
6685 st = e->symtree;
6686
6687 /* Deal with typebound operators for CLASS objects. */
6688 expr = e->value.compcall.base_object;
6689 overridable = !e->value.compcall.tbp->non_overridable;
6690 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
6691 {
6692 /* Since the typebound operators are generic, we have to ensure
6693 that any delays in resolution are corrected and that the vtab
6694 is present. */
6695 ts = expr->ts;
6696 declared = ts.u.derived;
6697 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6698 if (c->ts.u.derived == NULL)
6699 c->ts.u.derived = gfc_find_derived_vtab (declared);
6700
6701 if (!resolve_compcall (e, &name))
6702 return false;
6703
6704 /* Use the generic name if it is there. */
6705 name = name ? name : e->value.function.esym->name;
6706 e->symtree = expr->symtree;
6707 e->ref = gfc_copy_ref (expr->ref);
6708 get_declared_from_expr (&class_ref, NULL, e, false);
6709
6710 /* Trim away the extraneous references that emerge from nested
6711 use of interface.c (extend_expr). */
6712 if (class_ref && class_ref->next)
6713 {
6714 gfc_free_ref_list (class_ref->next);
6715 class_ref->next = NULL;
6716 }
6717 else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
6718 {
6719 gfc_free_ref_list (e->ref);
6720 e->ref = NULL;
6721 }
6722
6723 gfc_add_vptr_component (e);
6724 gfc_add_component_ref (e, name);
6725 e->value.function.esym = NULL;
6726 if (expr->expr_type != EXPR_VARIABLE)
6727 e->base_expr = expr;
6728 return true;
6729 }
6730
6731 if (st == NULL)
6732 return resolve_compcall (e, NULL);
6733
6734 if (!gfc_resolve_ref (e))
6735 return false;
6736
6737 /* Get the CLASS declared type. */
6738 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
6739
6740 if (!resolve_fl_derived (declared))
6741 return false;
6742
6743 /* Weed out cases of the ultimate component being a derived type. */
6744 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6745 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6746 {
6747 gfc_free_ref_list (new_ref);
6748 return resolve_compcall (e, NULL);
6749 }
6750
6751 c = gfc_find_component (declared, "_data", true, true, NULL);
6752
6753 /* Treat the call as if it is a typebound procedure, in order to roll
6754 out the correct name for the specific function. */
6755 if (!resolve_compcall (e, &name))
6756 {
6757 gfc_free_ref_list (new_ref);
6758 return false;
6759 }
6760 ts = e->ts;
6761
6762 if (overridable)
6763 {
6764 /* Convert the expression to a procedure pointer component call. */
6765 e->value.function.esym = NULL;
6766 e->symtree = st;
6767
6768 if (new_ref)
6769 e->ref = new_ref;
6770
6771 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6772 gfc_add_vptr_component (e);
6773 gfc_add_component_ref (e, name);
6774
6775 /* Recover the typespec for the expression. This is really only
6776 necessary for generic procedures, where the additional call
6777 to gfc_add_component_ref seems to throw the collection of the
6778 correct typespec. */
6779 e->ts = ts;
6780 }
6781 else if (new_ref)
6782 gfc_free_ref_list (new_ref);
6783
6784 return true;
6785 }
6786
6787 /* Resolve a typebound subroutine, or 'method'. First separate all
6788 the non-CLASS references by calling resolve_typebound_call
6789 directly. */
6790
6791 static bool
6792 resolve_typebound_subroutine (gfc_code *code)
6793 {
6794 gfc_symbol *declared;
6795 gfc_component *c;
6796 gfc_ref *new_ref;
6797 gfc_ref *class_ref;
6798 gfc_symtree *st;
6799 const char *name;
6800 gfc_typespec ts;
6801 gfc_expr *expr;
6802 bool overridable;
6803
6804 st = code->expr1->symtree;
6805
6806 /* Deal with typebound operators for CLASS objects. */
6807 expr = code->expr1->value.compcall.base_object;
6808 overridable = !code->expr1->value.compcall.tbp->non_overridable;
6809 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
6810 {
6811 /* If the base_object is not a variable, the corresponding actual
6812 argument expression must be stored in e->base_expression so
6813 that the corresponding tree temporary can be used as the base
6814 object in gfc_conv_procedure_call. */
6815 if (expr->expr_type != EXPR_VARIABLE)
6816 {
6817 gfc_actual_arglist *args;
6818
6819 args= code->expr1->value.function.actual;
6820 for (; args; args = args->next)
6821 if (expr == args->expr)
6822 expr = args->expr;
6823 }
6824
6825 /* Since the typebound operators are generic, we have to ensure
6826 that any delays in resolution are corrected and that the vtab
6827 is present. */
6828 declared = expr->ts.u.derived;
6829 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6830 if (c->ts.u.derived == NULL)
6831 c->ts.u.derived = gfc_find_derived_vtab (declared);
6832
6833 if (!resolve_typebound_call (code, &name, NULL))
6834 return false;
6835
6836 /* Use the generic name if it is there. */
6837 name = name ? name : code->expr1->value.function.esym->name;
6838 code->expr1->symtree = expr->symtree;
6839 code->expr1->ref = gfc_copy_ref (expr->ref);
6840
6841 /* Trim away the extraneous references that emerge from nested
6842 use of interface.c (extend_expr). */
6843 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
6844 if (class_ref && class_ref->next)
6845 {
6846 gfc_free_ref_list (class_ref->next);
6847 class_ref->next = NULL;
6848 }
6849 else if (code->expr1->ref && !class_ref)
6850 {
6851 gfc_free_ref_list (code->expr1->ref);
6852 code->expr1->ref = NULL;
6853 }
6854
6855 /* Now use the procedure in the vtable. */
6856 gfc_add_vptr_component (code->expr1);
6857 gfc_add_component_ref (code->expr1, name);
6858 code->expr1->value.function.esym = NULL;
6859 if (expr->expr_type != EXPR_VARIABLE)
6860 code->expr1->base_expr = expr;
6861 return true;
6862 }
6863
6864 if (st == NULL)
6865 return resolve_typebound_call (code, NULL, NULL);
6866
6867 if (!gfc_resolve_ref (code->expr1))
6868 return false;
6869
6870 /* Get the CLASS declared type. */
6871 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
6872
6873 /* Weed out cases of the ultimate component being a derived type. */
6874 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6875 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6876 {
6877 gfc_free_ref_list (new_ref);
6878 return resolve_typebound_call (code, NULL, NULL);
6879 }
6880
6881 if (!resolve_typebound_call (code, &name, &overridable))
6882 {
6883 gfc_free_ref_list (new_ref);
6884 return false;
6885 }
6886 ts = code->expr1->ts;
6887
6888 if (overridable)
6889 {
6890 /* Convert the expression to a procedure pointer component call. */
6891 code->expr1->value.function.esym = NULL;
6892 code->expr1->symtree = st;
6893
6894 if (new_ref)
6895 code->expr1->ref = new_ref;
6896
6897 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6898 gfc_add_vptr_component (code->expr1);
6899 gfc_add_component_ref (code->expr1, name);
6900
6901 /* Recover the typespec for the expression. This is really only
6902 necessary for generic procedures, where the additional call
6903 to gfc_add_component_ref seems to throw the collection of the
6904 correct typespec. */
6905 code->expr1->ts = ts;
6906 }
6907 else if (new_ref)
6908 gfc_free_ref_list (new_ref);
6909
6910 return true;
6911 }
6912
6913
6914 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6915
6916 static bool
6917 resolve_ppc_call (gfc_code* c)
6918 {
6919 gfc_component *comp;
6920
6921 comp = gfc_get_proc_ptr_comp (c->expr1);
6922 gcc_assert (comp != NULL);
6923
6924 c->resolved_sym = c->expr1->symtree->n.sym;
6925 c->expr1->expr_type = EXPR_VARIABLE;
6926
6927 if (!comp->attr.subroutine)
6928 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
6929
6930 if (!gfc_resolve_ref (c->expr1))
6931 return false;
6932
6933 if (!update_ppc_arglist (c->expr1))
6934 return false;
6935
6936 c->ext.actual = c->expr1->value.compcall.actual;
6937
6938 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6939 !(comp->ts.interface
6940 && comp->ts.interface->formal)))
6941 return false;
6942
6943 if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
6944 return false;
6945
6946 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6947
6948 return true;
6949 }
6950
6951
6952 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6953
6954 static bool
6955 resolve_expr_ppc (gfc_expr* e)
6956 {
6957 gfc_component *comp;
6958
6959 comp = gfc_get_proc_ptr_comp (e);
6960 gcc_assert (comp != NULL);
6961
6962 /* Convert to EXPR_FUNCTION. */
6963 e->expr_type = EXPR_FUNCTION;
6964 e->value.function.isym = NULL;
6965 e->value.function.actual = e->value.compcall.actual;
6966 e->ts = comp->ts;
6967 if (comp->as != NULL)
6968 e->rank = comp->as->rank;
6969
6970 if (!comp->attr.function)
6971 gfc_add_function (&comp->attr, comp->name, &e->where);
6972
6973 if (!gfc_resolve_ref (e))
6974 return false;
6975
6976 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6977 !(comp->ts.interface
6978 && comp->ts.interface->formal)))
6979 return false;
6980
6981 if (!update_ppc_arglist (e))
6982 return false;
6983
6984 if (!check_pure_function(e))
6985 return false;
6986
6987 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6988
6989 return true;
6990 }
6991
6992
6993 static bool
6994 gfc_is_expandable_expr (gfc_expr *e)
6995 {
6996 gfc_constructor *con;
6997
6998 if (e->expr_type == EXPR_ARRAY)
6999 {
7000 /* Traverse the constructor looking for variables that are flavor
7001 parameter. Parameters must be expanded since they are fully used at
7002 compile time. */
7003 con = gfc_constructor_first (e->value.constructor);
7004 for (; con; con = gfc_constructor_next (con))
7005 {
7006 if (con->expr->expr_type == EXPR_VARIABLE
7007 && con->expr->symtree
7008 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
7009 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
7010 return true;
7011 if (con->expr->expr_type == EXPR_ARRAY
7012 && gfc_is_expandable_expr (con->expr))
7013 return true;
7014 }
7015 }
7016
7017 return false;
7018 }
7019
7020
7021 /* Sometimes variables in specification expressions of the result
7022 of module procedures in submodules wind up not being the 'real'
7023 dummy. Find this, if possible, in the namespace of the first
7024 formal argument. */
7025
7026 static void
7027 fixup_unique_dummy (gfc_expr *e)
7028 {
7029 gfc_symtree *st = NULL;
7030 gfc_symbol *s = NULL;
7031
7032 if (e->symtree->n.sym->ns->proc_name
7033 && e->symtree->n.sym->ns->proc_name->formal)
7034 s = e->symtree->n.sym->ns->proc_name->formal->sym;
7035
7036 if (s != NULL)
7037 st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
7038
7039 if (st != NULL
7040 && st->n.sym != NULL
7041 && st->n.sym->attr.dummy)
7042 e->symtree = st;
7043 }
7044
7045 /* Resolve an expression. That is, make sure that types of operands agree
7046 with their operators, intrinsic operators are converted to function calls
7047 for overloaded types and unresolved function references are resolved. */
7048
7049 bool
7050 gfc_resolve_expr (gfc_expr *e)
7051 {
7052 bool t;
7053 bool inquiry_save, actual_arg_save, first_actual_arg_save;
7054
7055 if (e == NULL || e->do_not_resolve_again)
7056 return true;
7057
7058 /* inquiry_argument only applies to variables. */
7059 inquiry_save = inquiry_argument;
7060 actual_arg_save = actual_arg;
7061 first_actual_arg_save = first_actual_arg;
7062
7063 if (e->expr_type != EXPR_VARIABLE)
7064 {
7065 inquiry_argument = false;
7066 actual_arg = false;
7067 first_actual_arg = false;
7068 }
7069 else if (e->symtree != NULL
7070 && *e->symtree->name == '@'
7071 && e->symtree->n.sym->attr.dummy)
7072 {
7073 /* Deal with submodule specification expressions that are not
7074 found to be referenced in module.c(read_cleanup). */
7075 fixup_unique_dummy (e);
7076 }
7077
7078 switch (e->expr_type)
7079 {
7080 case EXPR_OP:
7081 t = resolve_operator (e);
7082 break;
7083
7084 case EXPR_FUNCTION:
7085 case EXPR_VARIABLE:
7086
7087 if (check_host_association (e))
7088 t = resolve_function (e);
7089 else
7090 t = resolve_variable (e);
7091
7092 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
7093 && e->ref->type != REF_SUBSTRING)
7094 gfc_resolve_substring_charlen (e);
7095
7096 break;
7097
7098 case EXPR_COMPCALL:
7099 t = resolve_typebound_function (e);
7100 break;
7101
7102 case EXPR_SUBSTRING:
7103 t = gfc_resolve_ref (e);
7104 break;
7105
7106 case EXPR_CONSTANT:
7107 case EXPR_NULL:
7108 t = true;
7109 break;
7110
7111 case EXPR_PPC:
7112 t = resolve_expr_ppc (e);
7113 break;
7114
7115 case EXPR_ARRAY:
7116 t = false;
7117 if (!gfc_resolve_ref (e))
7118 break;
7119
7120 t = gfc_resolve_array_constructor (e);
7121 /* Also try to expand a constructor. */
7122 if (t)
7123 {
7124 gfc_expression_rank (e);
7125 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
7126 gfc_expand_constructor (e, false);
7127 }
7128
7129 /* This provides the opportunity for the length of constructors with
7130 character valued function elements to propagate the string length
7131 to the expression. */
7132 if (t && e->ts.type == BT_CHARACTER)
7133 {
7134 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
7135 here rather then add a duplicate test for it above. */
7136 gfc_expand_constructor (e, false);
7137 t = gfc_resolve_character_array_constructor (e);
7138 }
7139
7140 break;
7141
7142 case EXPR_STRUCTURE:
7143 t = gfc_resolve_ref (e);
7144 if (!t)
7145 break;
7146
7147 t = resolve_structure_cons (e, 0);
7148 if (!t)
7149 break;
7150
7151 t = gfc_simplify_expr (e, 0);
7152 break;
7153
7154 default:
7155 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
7156 }
7157
7158 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
7159 fixup_charlen (e);
7160
7161 inquiry_argument = inquiry_save;
7162 actual_arg = actual_arg_save;
7163 first_actual_arg = first_actual_arg_save;
7164
7165 /* For some reason, resolving these expressions a second time mangles
7166 the typespec of the expression itself. */
7167 if (t && e->expr_type == EXPR_VARIABLE
7168 && e->symtree->n.sym->attr.select_rank_temporary
7169 && UNLIMITED_POLY (e->symtree->n.sym))
7170 e->do_not_resolve_again = 1;
7171
7172 return t;
7173 }
7174
7175
7176 /* Resolve an expression from an iterator. They must be scalar and have
7177 INTEGER or (optionally) REAL type. */
7178
7179 static bool
7180 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
7181 const char *name_msgid)
7182 {
7183 if (!gfc_resolve_expr (expr))
7184 return false;
7185
7186 if (expr->rank != 0)
7187 {
7188 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
7189 return false;
7190 }
7191
7192 if (expr->ts.type != BT_INTEGER)
7193 {
7194 if (expr->ts.type == BT_REAL)
7195 {
7196 if (real_ok)
7197 return gfc_notify_std (GFC_STD_F95_DEL,
7198 "%s at %L must be integer",
7199 _(name_msgid), &expr->where);
7200 else
7201 {
7202 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
7203 &expr->where);
7204 return false;
7205 }
7206 }
7207 else
7208 {
7209 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
7210 return false;
7211 }
7212 }
7213 return true;
7214 }
7215
7216
7217 /* Resolve the expressions in an iterator structure. If REAL_OK is
7218 false allow only INTEGER type iterators, otherwise allow REAL types.
7219 Set own_scope to true for ac-implied-do and data-implied-do as those
7220 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
7221
7222 bool
7223 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
7224 {
7225 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
7226 return false;
7227
7228 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
7229 _("iterator variable")))
7230 return false;
7231
7232 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
7233 "Start expression in DO loop"))
7234 return false;
7235
7236 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
7237 "End expression in DO loop"))
7238 return false;
7239
7240 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
7241 "Step expression in DO loop"))
7242 return false;
7243
7244 /* Convert start, end, and step to the same type as var. */
7245 if (iter->start->ts.kind != iter->var->ts.kind
7246 || iter->start->ts.type != iter->var->ts.type)
7247 gfc_convert_type (iter->start, &iter->var->ts, 1);
7248
7249 if (iter->end->ts.kind != iter->var->ts.kind
7250 || iter->end->ts.type != iter->var->ts.type)
7251 gfc_convert_type (iter->end, &iter->var->ts, 1);
7252
7253 if (iter->step->ts.kind != iter->var->ts.kind
7254 || iter->step->ts.type != iter->var->ts.type)
7255 gfc_convert_type (iter->step, &iter->var->ts, 1);
7256
7257 if (iter->step->expr_type == EXPR_CONSTANT)
7258 {
7259 if ((iter->step->ts.type == BT_INTEGER
7260 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
7261 || (iter->step->ts.type == BT_REAL
7262 && mpfr_sgn (iter->step->value.real) == 0))
7263 {
7264 gfc_error ("Step expression in DO loop at %L cannot be zero",
7265 &iter->step->where);
7266 return false;
7267 }
7268 }
7269
7270 if (iter->start->expr_type == EXPR_CONSTANT
7271 && iter->end->expr_type == EXPR_CONSTANT
7272 && iter->step->expr_type == EXPR_CONSTANT)
7273 {
7274 int sgn, cmp;
7275 if (iter->start->ts.type == BT_INTEGER)
7276 {
7277 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
7278 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
7279 }
7280 else
7281 {
7282 sgn = mpfr_sgn (iter->step->value.real);
7283 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
7284 }
7285 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
7286 gfc_warning (OPT_Wzerotrip,
7287 "DO loop at %L will be executed zero times",
7288 &iter->step->where);
7289 }
7290
7291 if (iter->end->expr_type == EXPR_CONSTANT
7292 && iter->end->ts.type == BT_INTEGER
7293 && iter->step->expr_type == EXPR_CONSTANT
7294 && iter->step->ts.type == BT_INTEGER
7295 && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
7296 || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
7297 {
7298 bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
7299 int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
7300
7301 if (is_step_positive
7302 && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
7303 gfc_warning (OPT_Wundefined_do_loop,
7304 "DO loop at %L is undefined as it overflows",
7305 &iter->step->where);
7306 else if (!is_step_positive
7307 && mpz_cmp (iter->end->value.integer,
7308 gfc_integer_kinds[k].min_int) == 0)
7309 gfc_warning (OPT_Wundefined_do_loop,
7310 "DO loop at %L is undefined as it underflows",
7311 &iter->step->where);
7312 }
7313
7314 return true;
7315 }
7316
7317
7318 /* Traversal function for find_forall_index. f == 2 signals that
7319 that variable itself is not to be checked - only the references. */
7320
7321 static bool
7322 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
7323 {
7324 if (expr->expr_type != EXPR_VARIABLE)
7325 return false;
7326
7327 /* A scalar assignment */
7328 if (!expr->ref || *f == 1)
7329 {
7330 if (expr->symtree->n.sym == sym)
7331 return true;
7332 else
7333 return false;
7334 }
7335
7336 if (*f == 2)
7337 *f = 1;
7338 return false;
7339 }
7340
7341
7342 /* Check whether the FORALL index appears in the expression or not.
7343 Returns true if SYM is found in EXPR. */
7344
7345 bool
7346 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
7347 {
7348 if (gfc_traverse_expr (expr, sym, forall_index, f))
7349 return true;
7350 else
7351 return false;
7352 }
7353
7354
7355 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
7356 to be a scalar INTEGER variable. The subscripts and stride are scalar
7357 INTEGERs, and if stride is a constant it must be nonzero.
7358 Furthermore "A subscript or stride in a forall-triplet-spec shall
7359 not contain a reference to any index-name in the
7360 forall-triplet-spec-list in which it appears." (7.5.4.1) */
7361
7362 static void
7363 resolve_forall_iterators (gfc_forall_iterator *it)
7364 {
7365 gfc_forall_iterator *iter, *iter2;
7366
7367 for (iter = it; iter; iter = iter->next)
7368 {
7369 if (gfc_resolve_expr (iter->var)
7370 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
7371 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
7372 &iter->var->where);
7373
7374 if (gfc_resolve_expr (iter->start)
7375 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
7376 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
7377 &iter->start->where);
7378 if (iter->var->ts.kind != iter->start->ts.kind)
7379 gfc_convert_type (iter->start, &iter->var->ts, 1);
7380
7381 if (gfc_resolve_expr (iter->end)
7382 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
7383 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
7384 &iter->end->where);
7385 if (iter->var->ts.kind != iter->end->ts.kind)
7386 gfc_convert_type (iter->end, &iter->var->ts, 1);
7387
7388 if (gfc_resolve_expr (iter->stride))
7389 {
7390 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
7391 gfc_error ("FORALL stride expression at %L must be a scalar %s",
7392 &iter->stride->where, "INTEGER");
7393
7394 if (iter->stride->expr_type == EXPR_CONSTANT
7395 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
7396 gfc_error ("FORALL stride expression at %L cannot be zero",
7397 &iter->stride->where);
7398 }
7399 if (iter->var->ts.kind != iter->stride->ts.kind)
7400 gfc_convert_type (iter->stride, &iter->var->ts, 1);
7401 }
7402
7403 for (iter = it; iter; iter = iter->next)
7404 for (iter2 = iter; iter2; iter2 = iter2->next)
7405 {
7406 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
7407 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
7408 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
7409 gfc_error ("FORALL index %qs may not appear in triplet "
7410 "specification at %L", iter->var->symtree->name,
7411 &iter2->start->where);
7412 }
7413 }
7414
7415
7416 /* Given a pointer to a symbol that is a derived type, see if it's
7417 inaccessible, i.e. if it's defined in another module and the components are
7418 PRIVATE. The search is recursive if necessary. Returns zero if no
7419 inaccessible components are found, nonzero otherwise. */
7420
7421 static int
7422 derived_inaccessible (gfc_symbol *sym)
7423 {
7424 gfc_component *c;
7425
7426 if (sym->attr.use_assoc && sym->attr.private_comp)
7427 return 1;
7428
7429 for (c = sym->components; c; c = c->next)
7430 {
7431 /* Prevent an infinite loop through this function. */
7432 if (c->ts.type == BT_DERIVED && c->attr.pointer
7433 && sym == c->ts.u.derived)
7434 continue;
7435
7436 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
7437 return 1;
7438 }
7439
7440 return 0;
7441 }
7442
7443
7444 /* Resolve the argument of a deallocate expression. The expression must be
7445 a pointer or a full array. */
7446
7447 static bool
7448 resolve_deallocate_expr (gfc_expr *e)
7449 {
7450 symbol_attribute attr;
7451 int allocatable, pointer;
7452 gfc_ref *ref;
7453 gfc_symbol *sym;
7454 gfc_component *c;
7455 bool unlimited;
7456
7457 if (!gfc_resolve_expr (e))
7458 return false;
7459
7460 if (e->expr_type != EXPR_VARIABLE)
7461 goto bad;
7462
7463 sym = e->symtree->n.sym;
7464 unlimited = UNLIMITED_POLY(sym);
7465
7466 if (sym->ts.type == BT_CLASS)
7467 {
7468 allocatable = CLASS_DATA (sym)->attr.allocatable;
7469 pointer = CLASS_DATA (sym)->attr.class_pointer;
7470 }
7471 else
7472 {
7473 allocatable = sym->attr.allocatable;
7474 pointer = sym->attr.pointer;
7475 }
7476 for (ref = e->ref; ref; ref = ref->next)
7477 {
7478 switch (ref->type)
7479 {
7480 case REF_ARRAY:
7481 if (ref->u.ar.type != AR_FULL
7482 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
7483 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
7484 allocatable = 0;
7485 break;
7486
7487 case REF_COMPONENT:
7488 c = ref->u.c.component;
7489 if (c->ts.type == BT_CLASS)
7490 {
7491 allocatable = CLASS_DATA (c)->attr.allocatable;
7492 pointer = CLASS_DATA (c)->attr.class_pointer;
7493 }
7494 else
7495 {
7496 allocatable = c->attr.allocatable;
7497 pointer = c->attr.pointer;
7498 }
7499 break;
7500
7501 case REF_SUBSTRING:
7502 case REF_INQUIRY:
7503 allocatable = 0;
7504 break;
7505 }
7506 }
7507
7508 attr = gfc_expr_attr (e);
7509
7510 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
7511 {
7512 bad:
7513 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7514 &e->where);
7515 return false;
7516 }
7517
7518 /* F2008, C644. */
7519 if (gfc_is_coindexed (e))
7520 {
7521 gfc_error ("Coindexed allocatable object at %L", &e->where);
7522 return false;
7523 }
7524
7525 if (pointer
7526 && !gfc_check_vardef_context (e, true, true, false,
7527 _("DEALLOCATE object")))
7528 return false;
7529 if (!gfc_check_vardef_context (e, false, true, false,
7530 _("DEALLOCATE object")))
7531 return false;
7532
7533 return true;
7534 }
7535
7536
7537 /* Returns true if the expression e contains a reference to the symbol sym. */
7538 static bool
7539 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
7540 {
7541 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
7542 return true;
7543
7544 return false;
7545 }
7546
7547 bool
7548 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
7549 {
7550 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
7551 }
7552
7553
7554 /* Given the expression node e for an allocatable/pointer of derived type to be
7555 allocated, get the expression node to be initialized afterwards (needed for
7556 derived types with default initializers, and derived types with allocatable
7557 components that need nullification.) */
7558
7559 gfc_expr *
7560 gfc_expr_to_initialize (gfc_expr *e)
7561 {
7562 gfc_expr *result;
7563 gfc_ref *ref;
7564 int i;
7565
7566 result = gfc_copy_expr (e);
7567
7568 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
7569 for (ref = result->ref; ref; ref = ref->next)
7570 if (ref->type == REF_ARRAY && ref->next == NULL)
7571 {
7572 if (ref->u.ar.dimen == 0
7573 && ref->u.ar.as && ref->u.ar.as->corank)
7574 return result;
7575
7576 ref->u.ar.type = AR_FULL;
7577
7578 for (i = 0; i < ref->u.ar.dimen; i++)
7579 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
7580
7581 break;
7582 }
7583
7584 gfc_free_shape (&result->shape, result->rank);
7585
7586 /* Recalculate rank, shape, etc. */
7587 gfc_resolve_expr (result);
7588 return result;
7589 }
7590
7591
7592 /* If the last ref of an expression is an array ref, return a copy of the
7593 expression with that one removed. Otherwise, a copy of the original
7594 expression. This is used for allocate-expressions and pointer assignment
7595 LHS, where there may be an array specification that needs to be stripped
7596 off when using gfc_check_vardef_context. */
7597
7598 static gfc_expr*
7599 remove_last_array_ref (gfc_expr* e)
7600 {
7601 gfc_expr* e2;
7602 gfc_ref** r;
7603
7604 e2 = gfc_copy_expr (e);
7605 for (r = &e2->ref; *r; r = &(*r)->next)
7606 if ((*r)->type == REF_ARRAY && !(*r)->next)
7607 {
7608 gfc_free_ref_list (*r);
7609 *r = NULL;
7610 break;
7611 }
7612
7613 return e2;
7614 }
7615
7616
7617 /* Used in resolve_allocate_expr to check that a allocation-object and
7618 a source-expr are conformable. This does not catch all possible
7619 cases; in particular a runtime checking is needed. */
7620
7621 static bool
7622 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
7623 {
7624 gfc_ref *tail;
7625 for (tail = e2->ref; tail && tail->next; tail = tail->next);
7626
7627 /* First compare rank. */
7628 if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
7629 || (!tail && e1->rank != e2->rank))
7630 {
7631 gfc_error ("Source-expr at %L must be scalar or have the "
7632 "same rank as the allocate-object at %L",
7633 &e1->where, &e2->where);
7634 return false;
7635 }
7636
7637 if (e1->shape)
7638 {
7639 int i;
7640 mpz_t s;
7641
7642 mpz_init (s);
7643
7644 for (i = 0; i < e1->rank; i++)
7645 {
7646 if (tail->u.ar.start[i] == NULL)
7647 break;
7648
7649 if (tail->u.ar.end[i])
7650 {
7651 mpz_set (s, tail->u.ar.end[i]->value.integer);
7652 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
7653 mpz_add_ui (s, s, 1);
7654 }
7655 else
7656 {
7657 mpz_set (s, tail->u.ar.start[i]->value.integer);
7658 }
7659
7660 if (mpz_cmp (e1->shape[i], s) != 0)
7661 {
7662 gfc_error ("Source-expr at %L and allocate-object at %L must "
7663 "have the same shape", &e1->where, &e2->where);
7664 mpz_clear (s);
7665 return false;
7666 }
7667 }
7668
7669 mpz_clear (s);
7670 }
7671
7672 return true;
7673 }
7674
7675
7676 /* Resolve the expression in an ALLOCATE statement, doing the additional
7677 checks to see whether the expression is OK or not. The expression must
7678 have a trailing array reference that gives the size of the array. */
7679
7680 static bool
7681 resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
7682 {
7683 int i, pointer, allocatable, dimension, is_abstract;
7684 int codimension;
7685 bool coindexed;
7686 bool unlimited;
7687 symbol_attribute attr;
7688 gfc_ref *ref, *ref2;
7689 gfc_expr *e2;
7690 gfc_array_ref *ar;
7691 gfc_symbol *sym = NULL;
7692 gfc_alloc *a;
7693 gfc_component *c;
7694 bool t;
7695
7696 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
7697 checking of coarrays. */
7698 for (ref = e->ref; ref; ref = ref->next)
7699 if (ref->next == NULL)
7700 break;
7701
7702 if (ref && ref->type == REF_ARRAY)
7703 ref->u.ar.in_allocate = true;
7704
7705 if (!gfc_resolve_expr (e))
7706 goto failure;
7707
7708 /* Make sure the expression is allocatable or a pointer. If it is
7709 pointer, the next-to-last reference must be a pointer. */
7710
7711 ref2 = NULL;
7712 if (e->symtree)
7713 sym = e->symtree->n.sym;
7714
7715 /* Check whether ultimate component is abstract and CLASS. */
7716 is_abstract = 0;
7717
7718 /* Is the allocate-object unlimited polymorphic? */
7719 unlimited = UNLIMITED_POLY(e);
7720
7721 if (e->expr_type != EXPR_VARIABLE)
7722 {
7723 allocatable = 0;
7724 attr = gfc_expr_attr (e);
7725 pointer = attr.pointer;
7726 dimension = attr.dimension;
7727 codimension = attr.codimension;
7728 }
7729 else
7730 {
7731 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
7732 {
7733 allocatable = CLASS_DATA (sym)->attr.allocatable;
7734 pointer = CLASS_DATA (sym)->attr.class_pointer;
7735 dimension = CLASS_DATA (sym)->attr.dimension;
7736 codimension = CLASS_DATA (sym)->attr.codimension;
7737 is_abstract = CLASS_DATA (sym)->attr.abstract;
7738 }
7739 else
7740 {
7741 allocatable = sym->attr.allocatable;
7742 pointer = sym->attr.pointer;
7743 dimension = sym->attr.dimension;
7744 codimension = sym->attr.codimension;
7745 }
7746
7747 coindexed = false;
7748
7749 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
7750 {
7751 switch (ref->type)
7752 {
7753 case REF_ARRAY:
7754 if (ref->u.ar.codimen > 0)
7755 {
7756 int n;
7757 for (n = ref->u.ar.dimen;
7758 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
7759 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
7760 {
7761 coindexed = true;
7762 break;
7763 }
7764 }
7765
7766 if (ref->next != NULL)
7767 pointer = 0;
7768 break;
7769
7770 case REF_COMPONENT:
7771 /* F2008, C644. */
7772 if (coindexed)
7773 {
7774 gfc_error ("Coindexed allocatable object at %L",
7775 &e->where);
7776 goto failure;
7777 }
7778
7779 c = ref->u.c.component;
7780 if (c->ts.type == BT_CLASS)
7781 {
7782 allocatable = CLASS_DATA (c)->attr.allocatable;
7783 pointer = CLASS_DATA (c)->attr.class_pointer;
7784 dimension = CLASS_DATA (c)->attr.dimension;
7785 codimension = CLASS_DATA (c)->attr.codimension;
7786 is_abstract = CLASS_DATA (c)->attr.abstract;
7787 }
7788 else
7789 {
7790 allocatable = c->attr.allocatable;
7791 pointer = c->attr.pointer;
7792 dimension = c->attr.dimension;
7793 codimension = c->attr.codimension;
7794 is_abstract = c->attr.abstract;
7795 }
7796 break;
7797
7798 case REF_SUBSTRING:
7799 case REF_INQUIRY:
7800 allocatable = 0;
7801 pointer = 0;
7802 break;
7803 }
7804 }
7805 }
7806
7807 /* Check for F08:C628. */
7808 if (allocatable == 0 && pointer == 0 && !unlimited)
7809 {
7810 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7811 &e->where);
7812 goto failure;
7813 }
7814
7815 /* Some checks for the SOURCE tag. */
7816 if (code->expr3)
7817 {
7818 /* Check F03:C631. */
7819 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
7820 {
7821 gfc_error ("Type of entity at %L is type incompatible with "
7822 "source-expr at %L", &e->where, &code->expr3->where);
7823 goto failure;
7824 }
7825
7826 /* Check F03:C632 and restriction following Note 6.18. */
7827 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
7828 goto failure;
7829
7830 /* Check F03:C633. */
7831 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
7832 {
7833 gfc_error ("The allocate-object at %L and the source-expr at %L "
7834 "shall have the same kind type parameter",
7835 &e->where, &code->expr3->where);
7836 goto failure;
7837 }
7838
7839 /* Check F2008, C642. */
7840 if (code->expr3->ts.type == BT_DERIVED
7841 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
7842 || (code->expr3->ts.u.derived->from_intmod
7843 == INTMOD_ISO_FORTRAN_ENV
7844 && code->expr3->ts.u.derived->intmod_sym_id
7845 == ISOFORTRAN_LOCK_TYPE)))
7846 {
7847 gfc_error ("The source-expr at %L shall neither be of type "
7848 "LOCK_TYPE nor have a LOCK_TYPE component if "
7849 "allocate-object at %L is a coarray",
7850 &code->expr3->where, &e->where);
7851 goto failure;
7852 }
7853
7854 /* Check TS18508, C702/C703. */
7855 if (code->expr3->ts.type == BT_DERIVED
7856 && ((codimension && gfc_expr_attr (code->expr3).event_comp)
7857 || (code->expr3->ts.u.derived->from_intmod
7858 == INTMOD_ISO_FORTRAN_ENV
7859 && code->expr3->ts.u.derived->intmod_sym_id
7860 == ISOFORTRAN_EVENT_TYPE)))
7861 {
7862 gfc_error ("The source-expr at %L shall neither be of type "
7863 "EVENT_TYPE nor have a EVENT_TYPE component if "
7864 "allocate-object at %L is a coarray",
7865 &code->expr3->where, &e->where);
7866 goto failure;
7867 }
7868 }
7869
7870 /* Check F08:C629. */
7871 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
7872 && !code->expr3)
7873 {
7874 gcc_assert (e->ts.type == BT_CLASS);
7875 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
7876 "type-spec or source-expr", sym->name, &e->where);
7877 goto failure;
7878 }
7879
7880 /* Check F08:C632. */
7881 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
7882 && !UNLIMITED_POLY (e))
7883 {
7884 int cmp;
7885
7886 if (!e->ts.u.cl->length)
7887 goto failure;
7888
7889 cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
7890 code->ext.alloc.ts.u.cl->length);
7891 if (cmp == 1 || cmp == -1 || cmp == -3)
7892 {
7893 gfc_error ("Allocating %s at %L with type-spec requires the same "
7894 "character-length parameter as in the declaration",
7895 sym->name, &e->where);
7896 goto failure;
7897 }
7898 }
7899
7900 /* In the variable definition context checks, gfc_expr_attr is used
7901 on the expression. This is fooled by the array specification
7902 present in e, thus we have to eliminate that one temporarily. */
7903 e2 = remove_last_array_ref (e);
7904 t = true;
7905 if (t && pointer)
7906 t = gfc_check_vardef_context (e2, true, true, false,
7907 _("ALLOCATE object"));
7908 if (t)
7909 t = gfc_check_vardef_context (e2, false, true, false,
7910 _("ALLOCATE object"));
7911 gfc_free_expr (e2);
7912 if (!t)
7913 goto failure;
7914
7915 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
7916 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
7917 {
7918 /* For class arrays, the initialization with SOURCE is done
7919 using _copy and trans_call. It is convenient to exploit that
7920 when the allocated type is different from the declared type but
7921 no SOURCE exists by setting expr3. */
7922 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
7923 }
7924 else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
7925 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
7926 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
7927 {
7928 /* We have to zero initialize the integer variable. */
7929 code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
7930 }
7931
7932 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
7933 {
7934 /* Make sure the vtab symbol is present when
7935 the module variables are generated. */
7936 gfc_typespec ts = e->ts;
7937 if (code->expr3)
7938 ts = code->expr3->ts;
7939 else if (code->ext.alloc.ts.type == BT_DERIVED)
7940 ts = code->ext.alloc.ts;
7941
7942 /* Finding the vtab also publishes the type's symbol. Therefore this
7943 statement is necessary. */
7944 gfc_find_derived_vtab (ts.u.derived);
7945 }
7946 else if (unlimited && !UNLIMITED_POLY (code->expr3))
7947 {
7948 /* Again, make sure the vtab symbol is present when
7949 the module variables are generated. */
7950 gfc_typespec *ts = NULL;
7951 if (code->expr3)
7952 ts = &code->expr3->ts;
7953 else
7954 ts = &code->ext.alloc.ts;
7955
7956 gcc_assert (ts);
7957
7958 /* Finding the vtab also publishes the type's symbol. Therefore this
7959 statement is necessary. */
7960 gfc_find_vtab (ts);
7961 }
7962
7963 if (dimension == 0 && codimension == 0)
7964 goto success;
7965
7966 /* Make sure the last reference node is an array specification. */
7967
7968 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
7969 || (dimension && ref2->u.ar.dimen == 0))
7970 {
7971 /* F08:C633. */
7972 if (code->expr3)
7973 {
7974 if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
7975 "in ALLOCATE statement at %L", &e->where))
7976 goto failure;
7977 if (code->expr3->rank != 0)
7978 *array_alloc_wo_spec = true;
7979 else
7980 {
7981 gfc_error ("Array specification or array-valued SOURCE= "
7982 "expression required in ALLOCATE statement at %L",
7983 &e->where);
7984 goto failure;
7985 }
7986 }
7987 else
7988 {
7989 gfc_error ("Array specification required in ALLOCATE statement "
7990 "at %L", &e->where);
7991 goto failure;
7992 }
7993 }
7994
7995 /* Make sure that the array section reference makes sense in the
7996 context of an ALLOCATE specification. */
7997
7998 ar = &ref2->u.ar;
7999
8000 if (codimension)
8001 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
8002 {
8003 switch (ar->dimen_type[i])
8004 {
8005 case DIMEN_THIS_IMAGE:
8006 gfc_error ("Coarray specification required in ALLOCATE statement "
8007 "at %L", &e->where);
8008 goto failure;
8009
8010 case DIMEN_RANGE:
8011 if (ar->start[i] == 0 || ar->end[i] == 0)
8012 {
8013 /* If ar->stride[i] is NULL, we issued a previous error. */
8014 if (ar->stride[i] == NULL)
8015 gfc_error ("Bad array specification in ALLOCATE statement "
8016 "at %L", &e->where);
8017 goto failure;
8018 }
8019 else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
8020 {
8021 gfc_error ("Upper cobound is less than lower cobound at %L",
8022 &ar->start[i]->where);
8023 goto failure;
8024 }
8025 break;
8026
8027 case DIMEN_ELEMENT:
8028 if (ar->start[i]->expr_type == EXPR_CONSTANT)
8029 {
8030 gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
8031 if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
8032 {
8033 gfc_error ("Upper cobound is less than lower cobound "
8034 "of 1 at %L", &ar->start[i]->where);
8035 goto failure;
8036 }
8037 }
8038 break;
8039
8040 case DIMEN_STAR:
8041 break;
8042
8043 default:
8044 gfc_error ("Bad array specification in ALLOCATE statement at %L",
8045 &e->where);
8046 goto failure;
8047
8048 }
8049 }
8050 for (i = 0; i < ar->dimen; i++)
8051 {
8052 if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
8053 goto check_symbols;
8054
8055 switch (ar->dimen_type[i])
8056 {
8057 case DIMEN_ELEMENT:
8058 break;
8059
8060 case DIMEN_RANGE:
8061 if (ar->start[i] != NULL
8062 && ar->end[i] != NULL
8063 && ar->stride[i] == NULL)
8064 break;
8065
8066 /* Fall through. */
8067
8068 case DIMEN_UNKNOWN:
8069 case DIMEN_VECTOR:
8070 case DIMEN_STAR:
8071 case DIMEN_THIS_IMAGE:
8072 gfc_error ("Bad array specification in ALLOCATE statement at %L",
8073 &e->where);
8074 goto failure;
8075 }
8076
8077 check_symbols:
8078 for (a = code->ext.alloc.list; a; a = a->next)
8079 {
8080 sym = a->expr->symtree->n.sym;
8081
8082 /* TODO - check derived type components. */
8083 if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
8084 continue;
8085
8086 if ((ar->start[i] != NULL
8087 && gfc_find_sym_in_expr (sym, ar->start[i]))
8088 || (ar->end[i] != NULL
8089 && gfc_find_sym_in_expr (sym, ar->end[i])))
8090 {
8091 gfc_error ("%qs must not appear in the array specification at "
8092 "%L in the same ALLOCATE statement where it is "
8093 "itself allocated", sym->name, &ar->where);
8094 goto failure;
8095 }
8096 }
8097 }
8098
8099 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
8100 {
8101 if (ar->dimen_type[i] == DIMEN_ELEMENT
8102 || ar->dimen_type[i] == DIMEN_RANGE)
8103 {
8104 if (i == (ar->dimen + ar->codimen - 1))
8105 {
8106 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
8107 "statement at %L", &e->where);
8108 goto failure;
8109 }
8110 continue;
8111 }
8112
8113 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
8114 && ar->stride[i] == NULL)
8115 break;
8116
8117 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
8118 &e->where);
8119 goto failure;
8120 }
8121
8122 success:
8123 return true;
8124
8125 failure:
8126 return false;
8127 }
8128
8129
8130 static void
8131 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
8132 {
8133 gfc_expr *stat, *errmsg, *pe, *qe;
8134 gfc_alloc *a, *p, *q;
8135
8136 stat = code->expr1;
8137 errmsg = code->expr2;
8138
8139 /* Check the stat variable. */
8140 if (stat)
8141 {
8142 gfc_check_vardef_context (stat, false, false, false,
8143 _("STAT variable"));
8144
8145 if ((stat->ts.type != BT_INTEGER
8146 && !(stat->ref && (stat->ref->type == REF_ARRAY
8147 || stat->ref->type == REF_COMPONENT)))
8148 || stat->rank > 0)
8149 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
8150 "variable", &stat->where);
8151
8152 for (p = code->ext.alloc.list; p; p = p->next)
8153 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
8154 {
8155 gfc_ref *ref1, *ref2;
8156 bool found = true;
8157
8158 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
8159 ref1 = ref1->next, ref2 = ref2->next)
8160 {
8161 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
8162 continue;
8163 if (ref1->u.c.component->name != ref2->u.c.component->name)
8164 {
8165 found = false;
8166 break;
8167 }
8168 }
8169
8170 if (found)
8171 {
8172 gfc_error ("Stat-variable at %L shall not be %sd within "
8173 "the same %s statement", &stat->where, fcn, fcn);
8174 break;
8175 }
8176 }
8177 }
8178
8179 /* Check the errmsg variable. */
8180 if (errmsg)
8181 {
8182 if (!stat)
8183 gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
8184 &errmsg->where);
8185
8186 gfc_check_vardef_context (errmsg, false, false, false,
8187 _("ERRMSG variable"));
8188
8189 /* F18:R928 alloc-opt is ERRMSG = errmsg-variable
8190 F18:R930 errmsg-variable is scalar-default-char-variable
8191 F18:R906 default-char-variable is variable
8192 F18:C906 default-char-variable shall be default character. */
8193 if ((errmsg->ts.type != BT_CHARACTER
8194 && !(errmsg->ref
8195 && (errmsg->ref->type == REF_ARRAY
8196 || errmsg->ref->type == REF_COMPONENT)))
8197 || errmsg->rank > 0
8198 || errmsg->ts.kind != gfc_default_character_kind)
8199 gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
8200 "variable", &errmsg->where);
8201
8202 for (p = code->ext.alloc.list; p; p = p->next)
8203 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
8204 {
8205 gfc_ref *ref1, *ref2;
8206 bool found = true;
8207
8208 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
8209 ref1 = ref1->next, ref2 = ref2->next)
8210 {
8211 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
8212 continue;
8213 if (ref1->u.c.component->name != ref2->u.c.component->name)
8214 {
8215 found = false;
8216 break;
8217 }
8218 }
8219
8220 if (found)
8221 {
8222 gfc_error ("Errmsg-variable at %L shall not be %sd within "
8223 "the same %s statement", &errmsg->where, fcn, fcn);
8224 break;
8225 }
8226 }
8227 }
8228
8229 /* Check that an allocate-object appears only once in the statement. */
8230
8231 for (p = code->ext.alloc.list; p; p = p->next)
8232 {
8233 pe = p->expr;
8234 for (q = p->next; q; q = q->next)
8235 {
8236 qe = q->expr;
8237 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
8238 {
8239 /* This is a potential collision. */
8240 gfc_ref *pr = pe->ref;
8241 gfc_ref *qr = qe->ref;
8242
8243 /* Follow the references until
8244 a) They start to differ, in which case there is no error;
8245 you can deallocate a%b and a%c in a single statement
8246 b) Both of them stop, which is an error
8247 c) One of them stops, which is also an error. */
8248 while (1)
8249 {
8250 if (pr == NULL && qr == NULL)
8251 {
8252 gfc_error ("Allocate-object at %L also appears at %L",
8253 &pe->where, &qe->where);
8254 break;
8255 }
8256 else if (pr != NULL && qr == NULL)
8257 {
8258 gfc_error ("Allocate-object at %L is subobject of"
8259 " object at %L", &pe->where, &qe->where);
8260 break;
8261 }
8262 else if (pr == NULL && qr != NULL)
8263 {
8264 gfc_error ("Allocate-object at %L is subobject of"
8265 " object at %L", &qe->where, &pe->where);
8266 break;
8267 }
8268 /* Here, pr != NULL && qr != NULL */
8269 gcc_assert(pr->type == qr->type);
8270 if (pr->type == REF_ARRAY)
8271 {
8272 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
8273 which are legal. */
8274 gcc_assert (qr->type == REF_ARRAY);
8275
8276 if (pr->next && qr->next)
8277 {
8278 int i;
8279 gfc_array_ref *par = &(pr->u.ar);
8280 gfc_array_ref *qar = &(qr->u.ar);
8281
8282 for (i=0; i<par->dimen; i++)
8283 {
8284 if ((par->start[i] != NULL
8285 || qar->start[i] != NULL)
8286 && gfc_dep_compare_expr (par->start[i],
8287 qar->start[i]) != 0)
8288 goto break_label;
8289 }
8290 }
8291 }
8292 else
8293 {
8294 if (pr->u.c.component->name != qr->u.c.component->name)
8295 break;
8296 }
8297
8298 pr = pr->next;
8299 qr = qr->next;
8300 }
8301 break_label:
8302 ;
8303 }
8304 }
8305 }
8306
8307 if (strcmp (fcn, "ALLOCATE") == 0)
8308 {
8309 bool arr_alloc_wo_spec = false;
8310
8311 /* Resolving the expr3 in the loop over all objects to allocate would
8312 execute loop invariant code for each loop item. Therefore do it just
8313 once here. */
8314 if (code->expr3 && code->expr3->mold
8315 && code->expr3->ts.type == BT_DERIVED)
8316 {
8317 /* Default initialization via MOLD (non-polymorphic). */
8318 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
8319 if (rhs != NULL)
8320 {
8321 gfc_resolve_expr (rhs);
8322 gfc_free_expr (code->expr3);
8323 code->expr3 = rhs;
8324 }
8325 }
8326 for (a = code->ext.alloc.list; a; a = a->next)
8327 resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
8328
8329 if (arr_alloc_wo_spec && code->expr3)
8330 {
8331 /* Mark the allocate to have to take the array specification
8332 from the expr3. */
8333 code->ext.alloc.arr_spec_from_expr3 = 1;
8334 }
8335 }
8336 else
8337 {
8338 for (a = code->ext.alloc.list; a; a = a->next)
8339 resolve_deallocate_expr (a->expr);
8340 }
8341 }
8342
8343
8344 /************ SELECT CASE resolution subroutines ************/
8345
8346 /* Callback function for our mergesort variant. Determines interval
8347 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
8348 op1 > op2. Assumes we're not dealing with the default case.
8349 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
8350 There are nine situations to check. */
8351
8352 static int
8353 compare_cases (const gfc_case *op1, const gfc_case *op2)
8354 {
8355 int retval;
8356
8357 if (op1->low == NULL) /* op1 = (:L) */
8358 {
8359 /* op2 = (:N), so overlap. */
8360 retval = 0;
8361 /* op2 = (M:) or (M:N), L < M */
8362 if (op2->low != NULL
8363 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8364 retval = -1;
8365 }
8366 else if (op1->high == NULL) /* op1 = (K:) */
8367 {
8368 /* op2 = (M:), so overlap. */
8369 retval = 0;
8370 /* op2 = (:N) or (M:N), K > N */
8371 if (op2->high != NULL
8372 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8373 retval = 1;
8374 }
8375 else /* op1 = (K:L) */
8376 {
8377 if (op2->low == NULL) /* op2 = (:N), K > N */
8378 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8379 ? 1 : 0;
8380 else if (op2->high == NULL) /* op2 = (M:), L < M */
8381 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8382 ? -1 : 0;
8383 else /* op2 = (M:N) */
8384 {
8385 retval = 0;
8386 /* L < M */
8387 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8388 retval = -1;
8389 /* K > N */
8390 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8391 retval = 1;
8392 }
8393 }
8394
8395 return retval;
8396 }
8397
8398
8399 /* Merge-sort a double linked case list, detecting overlap in the
8400 process. LIST is the head of the double linked case list before it
8401 is sorted. Returns the head of the sorted list if we don't see any
8402 overlap, or NULL otherwise. */
8403
8404 static gfc_case *
8405 check_case_overlap (gfc_case *list)
8406 {
8407 gfc_case *p, *q, *e, *tail;
8408 int insize, nmerges, psize, qsize, cmp, overlap_seen;
8409
8410 /* If the passed list was empty, return immediately. */
8411 if (!list)
8412 return NULL;
8413
8414 overlap_seen = 0;
8415 insize = 1;
8416
8417 /* Loop unconditionally. The only exit from this loop is a return
8418 statement, when we've finished sorting the case list. */
8419 for (;;)
8420 {
8421 p = list;
8422 list = NULL;
8423 tail = NULL;
8424
8425 /* Count the number of merges we do in this pass. */
8426 nmerges = 0;
8427
8428 /* Loop while there exists a merge to be done. */
8429 while (p)
8430 {
8431 int i;
8432
8433 /* Count this merge. */
8434 nmerges++;
8435
8436 /* Cut the list in two pieces by stepping INSIZE places
8437 forward in the list, starting from P. */
8438 psize = 0;
8439 q = p;
8440 for (i = 0; i < insize; i++)
8441 {
8442 psize++;
8443 q = q->right;
8444 if (!q)
8445 break;
8446 }
8447 qsize = insize;
8448
8449 /* Now we have two lists. Merge them! */
8450 while (psize > 0 || (qsize > 0 && q != NULL))
8451 {
8452 /* See from which the next case to merge comes from. */
8453 if (psize == 0)
8454 {
8455 /* P is empty so the next case must come from Q. */
8456 e = q;
8457 q = q->right;
8458 qsize--;
8459 }
8460 else if (qsize == 0 || q == NULL)
8461 {
8462 /* Q is empty. */
8463 e = p;
8464 p = p->right;
8465 psize--;
8466 }
8467 else
8468 {
8469 cmp = compare_cases (p, q);
8470 if (cmp < 0)
8471 {
8472 /* The whole case range for P is less than the
8473 one for Q. */
8474 e = p;
8475 p = p->right;
8476 psize--;
8477 }
8478 else if (cmp > 0)
8479 {
8480 /* The whole case range for Q is greater than
8481 the case range for P. */
8482 e = q;
8483 q = q->right;
8484 qsize--;
8485 }
8486 else
8487 {
8488 /* The cases overlap, or they are the same
8489 element in the list. Either way, we must
8490 issue an error and get the next case from P. */
8491 /* FIXME: Sort P and Q by line number. */
8492 gfc_error ("CASE label at %L overlaps with CASE "
8493 "label at %L", &p->where, &q->where);
8494 overlap_seen = 1;
8495 e = p;
8496 p = p->right;
8497 psize--;
8498 }
8499 }
8500
8501 /* Add the next element to the merged list. */
8502 if (tail)
8503 tail->right = e;
8504 else
8505 list = e;
8506 e->left = tail;
8507 tail = e;
8508 }
8509
8510 /* P has now stepped INSIZE places along, and so has Q. So
8511 they're the same. */
8512 p = q;
8513 }
8514 tail->right = NULL;
8515
8516 /* If we have done only one merge or none at all, we've
8517 finished sorting the cases. */
8518 if (nmerges <= 1)
8519 {
8520 if (!overlap_seen)
8521 return list;
8522 else
8523 return NULL;
8524 }
8525
8526 /* Otherwise repeat, merging lists twice the size. */
8527 insize *= 2;
8528 }
8529 }
8530
8531
8532 /* Check to see if an expression is suitable for use in a CASE statement.
8533 Makes sure that all case expressions are scalar constants of the same
8534 type. Return false if anything is wrong. */
8535
8536 static bool
8537 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
8538 {
8539 if (e == NULL) return true;
8540
8541 if (e->ts.type != case_expr->ts.type)
8542 {
8543 gfc_error ("Expression in CASE statement at %L must be of type %s",
8544 &e->where, gfc_basic_typename (case_expr->ts.type));
8545 return false;
8546 }
8547
8548 /* C805 (R808) For a given case-construct, each case-value shall be of
8549 the same type as case-expr. For character type, length differences
8550 are allowed, but the kind type parameters shall be the same. */
8551
8552 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
8553 {
8554 gfc_error ("Expression in CASE statement at %L must be of kind %d",
8555 &e->where, case_expr->ts.kind);
8556 return false;
8557 }
8558
8559 /* Convert the case value kind to that of case expression kind,
8560 if needed */
8561
8562 if (e->ts.kind != case_expr->ts.kind)
8563 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
8564
8565 if (e->rank != 0)
8566 {
8567 gfc_error ("Expression in CASE statement at %L must be scalar",
8568 &e->where);
8569 return false;
8570 }
8571
8572 return true;
8573 }
8574
8575
8576 /* Given a completely parsed select statement, we:
8577
8578 - Validate all expressions and code within the SELECT.
8579 - Make sure that the selection expression is not of the wrong type.
8580 - Make sure that no case ranges overlap.
8581 - Eliminate unreachable cases and unreachable code resulting from
8582 removing case labels.
8583
8584 The standard does allow unreachable cases, e.g. CASE (5:3). But
8585 they are a hassle for code generation, and to prevent that, we just
8586 cut them out here. This is not necessary for overlapping cases
8587 because they are illegal and we never even try to generate code.
8588
8589 We have the additional caveat that a SELECT construct could have
8590 been a computed GOTO in the source code. Fortunately we can fairly
8591 easily work around that here: The case_expr for a "real" SELECT CASE
8592 is in code->expr1, but for a computed GOTO it is in code->expr2. All
8593 we have to do is make sure that the case_expr is a scalar integer
8594 expression. */
8595
8596 static void
8597 resolve_select (gfc_code *code, bool select_type)
8598 {
8599 gfc_code *body;
8600 gfc_expr *case_expr;
8601 gfc_case *cp, *default_case, *tail, *head;
8602 int seen_unreachable;
8603 int seen_logical;
8604 int ncases;
8605 bt type;
8606 bool t;
8607
8608 if (code->expr1 == NULL)
8609 {
8610 /* This was actually a computed GOTO statement. */
8611 case_expr = code->expr2;
8612 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
8613 gfc_error ("Selection expression in computed GOTO statement "
8614 "at %L must be a scalar integer expression",
8615 &case_expr->where);
8616
8617 /* Further checking is not necessary because this SELECT was built
8618 by the compiler, so it should always be OK. Just move the
8619 case_expr from expr2 to expr so that we can handle computed
8620 GOTOs as normal SELECTs from here on. */
8621 code->expr1 = code->expr2;
8622 code->expr2 = NULL;
8623 return;
8624 }
8625
8626 case_expr = code->expr1;
8627 type = case_expr->ts.type;
8628
8629 /* F08:C830. */
8630 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
8631 {
8632 gfc_error ("Argument of SELECT statement at %L cannot be %s",
8633 &case_expr->where, gfc_typename (case_expr));
8634
8635 /* Punt. Going on here just produce more garbage error messages. */
8636 return;
8637 }
8638
8639 /* F08:R842. */
8640 if (!select_type && case_expr->rank != 0)
8641 {
8642 gfc_error ("Argument of SELECT statement at %L must be a scalar "
8643 "expression", &case_expr->where);
8644
8645 /* Punt. */
8646 return;
8647 }
8648
8649 /* Raise a warning if an INTEGER case value exceeds the range of
8650 the case-expr. Later, all expressions will be promoted to the
8651 largest kind of all case-labels. */
8652
8653 if (type == BT_INTEGER)
8654 for (body = code->block; body; body = body->block)
8655 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8656 {
8657 if (cp->low
8658 && gfc_check_integer_range (cp->low->value.integer,
8659 case_expr->ts.kind) != ARITH_OK)
8660 gfc_warning (0, "Expression in CASE statement at %L is "
8661 "not in the range of %s", &cp->low->where,
8662 gfc_typename (case_expr));
8663
8664 if (cp->high
8665 && cp->low != cp->high
8666 && gfc_check_integer_range (cp->high->value.integer,
8667 case_expr->ts.kind) != ARITH_OK)
8668 gfc_warning (0, "Expression in CASE statement at %L is "
8669 "not in the range of %s", &cp->high->where,
8670 gfc_typename (case_expr));
8671 }
8672
8673 /* PR 19168 has a long discussion concerning a mismatch of the kinds
8674 of the SELECT CASE expression and its CASE values. Walk the lists
8675 of case values, and if we find a mismatch, promote case_expr to
8676 the appropriate kind. */
8677
8678 if (type == BT_LOGICAL || type == BT_INTEGER)
8679 {
8680 for (body = code->block; body; body = body->block)
8681 {
8682 /* Walk the case label list. */
8683 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8684 {
8685 /* Intercept the DEFAULT case. It does not have a kind. */
8686 if (cp->low == NULL && cp->high == NULL)
8687 continue;
8688
8689 /* Unreachable case ranges are discarded, so ignore. */
8690 if (cp->low != NULL && cp->high != NULL
8691 && cp->low != cp->high
8692 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8693 continue;
8694
8695 if (cp->low != NULL
8696 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
8697 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
8698
8699 if (cp->high != NULL
8700 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
8701 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
8702 }
8703 }
8704 }
8705
8706 /* Assume there is no DEFAULT case. */
8707 default_case = NULL;
8708 head = tail = NULL;
8709 ncases = 0;
8710 seen_logical = 0;
8711
8712 for (body = code->block; body; body = body->block)
8713 {
8714 /* Assume the CASE list is OK, and all CASE labels can be matched. */
8715 t = true;
8716 seen_unreachable = 0;
8717
8718 /* Walk the case label list, making sure that all case labels
8719 are legal. */
8720 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8721 {
8722 /* Count the number of cases in the whole construct. */
8723 ncases++;
8724
8725 /* Intercept the DEFAULT case. */
8726 if (cp->low == NULL && cp->high == NULL)
8727 {
8728 if (default_case != NULL)
8729 {
8730 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8731 "by a second DEFAULT CASE at %L",
8732 &default_case->where, &cp->where);
8733 t = false;
8734 break;
8735 }
8736 else
8737 {
8738 default_case = cp;
8739 continue;
8740 }
8741 }
8742
8743 /* Deal with single value cases and case ranges. Errors are
8744 issued from the validation function. */
8745 if (!validate_case_label_expr (cp->low, case_expr)
8746 || !validate_case_label_expr (cp->high, case_expr))
8747 {
8748 t = false;
8749 break;
8750 }
8751
8752 if (type == BT_LOGICAL
8753 && ((cp->low == NULL || cp->high == NULL)
8754 || cp->low != cp->high))
8755 {
8756 gfc_error ("Logical range in CASE statement at %L is not "
8757 "allowed", &cp->low->where);
8758 t = false;
8759 break;
8760 }
8761
8762 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
8763 {
8764 int value;
8765 value = cp->low->value.logical == 0 ? 2 : 1;
8766 if (value & seen_logical)
8767 {
8768 gfc_error ("Constant logical value in CASE statement "
8769 "is repeated at %L",
8770 &cp->low->where);
8771 t = false;
8772 break;
8773 }
8774 seen_logical |= value;
8775 }
8776
8777 if (cp->low != NULL && cp->high != NULL
8778 && cp->low != cp->high
8779 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8780 {
8781 if (warn_surprising)
8782 gfc_warning (OPT_Wsurprising,
8783 "Range specification at %L can never be matched",
8784 &cp->where);
8785
8786 cp->unreachable = 1;
8787 seen_unreachable = 1;
8788 }
8789 else
8790 {
8791 /* If the case range can be matched, it can also overlap with
8792 other cases. To make sure it does not, we put it in a
8793 double linked list here. We sort that with a merge sort
8794 later on to detect any overlapping cases. */
8795 if (!head)
8796 {
8797 head = tail = cp;
8798 head->right = head->left = NULL;
8799 }
8800 else
8801 {
8802 tail->right = cp;
8803 tail->right->left = tail;
8804 tail = tail->right;
8805 tail->right = NULL;
8806 }
8807 }
8808 }
8809
8810 /* It there was a failure in the previous case label, give up
8811 for this case label list. Continue with the next block. */
8812 if (!t)
8813 continue;
8814
8815 /* See if any case labels that are unreachable have been seen.
8816 If so, we eliminate them. This is a bit of a kludge because
8817 the case lists for a single case statement (label) is a
8818 single forward linked lists. */
8819 if (seen_unreachable)
8820 {
8821 /* Advance until the first case in the list is reachable. */
8822 while (body->ext.block.case_list != NULL
8823 && body->ext.block.case_list->unreachable)
8824 {
8825 gfc_case *n = body->ext.block.case_list;
8826 body->ext.block.case_list = body->ext.block.case_list->next;
8827 n->next = NULL;
8828 gfc_free_case_list (n);
8829 }
8830
8831 /* Strip all other unreachable cases. */
8832 if (body->ext.block.case_list)
8833 {
8834 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
8835 {
8836 if (cp->next->unreachable)
8837 {
8838 gfc_case *n = cp->next;
8839 cp->next = cp->next->next;
8840 n->next = NULL;
8841 gfc_free_case_list (n);
8842 }
8843 }
8844 }
8845 }
8846 }
8847
8848 /* See if there were overlapping cases. If the check returns NULL,
8849 there was overlap. In that case we don't do anything. If head
8850 is non-NULL, we prepend the DEFAULT case. The sorted list can
8851 then used during code generation for SELECT CASE constructs with
8852 a case expression of a CHARACTER type. */
8853 if (head)
8854 {
8855 head = check_case_overlap (head);
8856
8857 /* Prepend the default_case if it is there. */
8858 if (head != NULL && default_case)
8859 {
8860 default_case->left = NULL;
8861 default_case->right = head;
8862 head->left = default_case;
8863 }
8864 }
8865
8866 /* Eliminate dead blocks that may be the result if we've seen
8867 unreachable case labels for a block. */
8868 for (body = code; body && body->block; body = body->block)
8869 {
8870 if (body->block->ext.block.case_list == NULL)
8871 {
8872 /* Cut the unreachable block from the code chain. */
8873 gfc_code *c = body->block;
8874 body->block = c->block;
8875
8876 /* Kill the dead block, but not the blocks below it. */
8877 c->block = NULL;
8878 gfc_free_statements (c);
8879 }
8880 }
8881
8882 /* More than two cases is legal but insane for logical selects.
8883 Issue a warning for it. */
8884 if (warn_surprising && type == BT_LOGICAL && ncases > 2)
8885 gfc_warning (OPT_Wsurprising,
8886 "Logical SELECT CASE block at %L has more that two cases",
8887 &code->loc);
8888 }
8889
8890
8891 /* Check if a derived type is extensible. */
8892
8893 bool
8894 gfc_type_is_extensible (gfc_symbol *sym)
8895 {
8896 return !(sym->attr.is_bind_c || sym->attr.sequence
8897 || (sym->attr.is_class
8898 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
8899 }
8900
8901
8902 static void
8903 resolve_types (gfc_namespace *ns);
8904
8905 /* Resolve an associate-name: Resolve target and ensure the type-spec is
8906 correct as well as possibly the array-spec. */
8907
8908 static void
8909 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
8910 {
8911 gfc_expr* target;
8912
8913 gcc_assert (sym->assoc);
8914 gcc_assert (sym->attr.flavor == FL_VARIABLE);
8915
8916 /* If this is for SELECT TYPE, the target may not yet be set. In that
8917 case, return. Resolution will be called later manually again when
8918 this is done. */
8919 target = sym->assoc->target;
8920 if (!target)
8921 return;
8922 gcc_assert (!sym->assoc->dangling);
8923
8924 if (resolve_target && !gfc_resolve_expr (target))
8925 return;
8926
8927 /* For variable targets, we get some attributes from the target. */
8928 if (target->expr_type == EXPR_VARIABLE)
8929 {
8930 gfc_symbol *tsym, *dsym;
8931
8932 gcc_assert (target->symtree);
8933 tsym = target->symtree->n.sym;
8934
8935 if (gfc_expr_attr (target).proc_pointer)
8936 {
8937 gfc_error ("Associating entity %qs at %L is a procedure pointer",
8938 tsym->name, &target->where);
8939 return;
8940 }
8941
8942 if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
8943 && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
8944 && dsym->attr.flavor == FL_DERIVED)
8945 {
8946 gfc_error ("Derived type %qs cannot be used as a variable at %L",
8947 tsym->name, &target->where);
8948 return;
8949 }
8950
8951 if (tsym->attr.flavor == FL_PROCEDURE)
8952 {
8953 bool is_error = true;
8954 if (tsym->attr.function && tsym->result == tsym)
8955 for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
8956 if (tsym == ns->proc_name)
8957 {
8958 is_error = false;
8959 break;
8960 }
8961 if (is_error)
8962 {
8963 gfc_error ("Associating entity %qs at %L is a procedure name",
8964 tsym->name, &target->where);
8965 return;
8966 }
8967 }
8968
8969 sym->attr.asynchronous = tsym->attr.asynchronous;
8970 sym->attr.volatile_ = tsym->attr.volatile_;
8971
8972 sym->attr.target = tsym->attr.target
8973 || gfc_expr_attr (target).pointer;
8974 if (is_subref_array (target))
8975 sym->attr.subref_array_pointer = 1;
8976 }
8977 else if (target->ts.type == BT_PROCEDURE)
8978 {
8979 gfc_error ("Associating selector-expression at %L yields a procedure",
8980 &target->where);
8981 return;
8982 }
8983
8984 if (target->expr_type == EXPR_NULL)
8985 {
8986 gfc_error ("Selector at %L cannot be NULL()", &target->where);
8987 return;
8988 }
8989 else if (target->ts.type == BT_UNKNOWN)
8990 {
8991 gfc_error ("Selector at %L has no type", &target->where);
8992 return;
8993 }
8994
8995 /* Get type if this was not already set. Note that it can be
8996 some other type than the target in case this is a SELECT TYPE
8997 selector! So we must not update when the type is already there. */
8998 if (sym->ts.type == BT_UNKNOWN)
8999 sym->ts = target->ts;
9000
9001 gcc_assert (sym->ts.type != BT_UNKNOWN);
9002
9003 /* See if this is a valid association-to-variable. */
9004 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
9005 && !gfc_has_vector_subscript (target));
9006
9007 /* Finally resolve if this is an array or not. */
9008 if (sym->attr.dimension && target->rank == 0)
9009 {
9010 /* primary.c makes the assumption that a reference to an associate
9011 name followed by a left parenthesis is an array reference. */
9012 if (sym->ts.type != BT_CHARACTER)
9013 gfc_error ("Associate-name %qs at %L is used as array",
9014 sym->name, &sym->declared_at);
9015 sym->attr.dimension = 0;
9016 return;
9017 }
9018
9019
9020 /* We cannot deal with class selectors that need temporaries. */
9021 if (target->ts.type == BT_CLASS
9022 && gfc_ref_needs_temporary_p (target->ref))
9023 {
9024 gfc_error ("CLASS selector at %L needs a temporary which is not "
9025 "yet implemented", &target->where);
9026 return;
9027 }
9028
9029 if (target->ts.type == BT_CLASS)
9030 gfc_fix_class_refs (target);
9031
9032 if (target->rank != 0 && !sym->attr.select_rank_temporary)
9033 {
9034 gfc_array_spec *as;
9035 /* The rank may be incorrectly guessed at parsing, therefore make sure
9036 it is corrected now. */
9037 if (sym->ts.type != BT_CLASS && (!sym->as || sym->assoc->rankguessed))
9038 {
9039 if (!sym->as)
9040 sym->as = gfc_get_array_spec ();
9041 as = sym->as;
9042 as->rank = target->rank;
9043 as->type = AS_DEFERRED;
9044 as->corank = gfc_get_corank (target);
9045 sym->attr.dimension = 1;
9046 if (as->corank != 0)
9047 sym->attr.codimension = 1;
9048 }
9049 else if (sym->ts.type == BT_CLASS
9050 && CLASS_DATA (sym)
9051 && (!CLASS_DATA (sym)->as || sym->assoc->rankguessed))
9052 {
9053 if (!CLASS_DATA (sym)->as)
9054 CLASS_DATA (sym)->as = gfc_get_array_spec ();
9055 as = CLASS_DATA (sym)->as;
9056 as->rank = target->rank;
9057 as->type = AS_DEFERRED;
9058 as->corank = gfc_get_corank (target);
9059 CLASS_DATA (sym)->attr.dimension = 1;
9060 if (as->corank != 0)
9061 CLASS_DATA (sym)->attr.codimension = 1;
9062 }
9063 }
9064 else if (!sym->attr.select_rank_temporary)
9065 {
9066 /* target's rank is 0, but the type of the sym is still array valued,
9067 which has to be corrected. */
9068 if (sym->ts.type == BT_CLASS && sym->ts.u.derived
9069 && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
9070 {
9071 gfc_array_spec *as;
9072 symbol_attribute attr;
9073 /* The associated variable's type is still the array type
9074 correct this now. */
9075 gfc_typespec *ts = &target->ts;
9076 gfc_ref *ref;
9077 gfc_component *c;
9078 for (ref = target->ref; ref != NULL; ref = ref->next)
9079 {
9080 switch (ref->type)
9081 {
9082 case REF_COMPONENT:
9083 ts = &ref->u.c.component->ts;
9084 break;
9085 case REF_ARRAY:
9086 if (ts->type == BT_CLASS)
9087 ts = &ts->u.derived->components->ts;
9088 break;
9089 default:
9090 break;
9091 }
9092 }
9093 /* Create a scalar instance of the current class type. Because the
9094 rank of a class array goes into its name, the type has to be
9095 rebuild. The alternative of (re-)setting just the attributes
9096 and as in the current type, destroys the type also in other
9097 places. */
9098 as = NULL;
9099 sym->ts = *ts;
9100 sym->ts.type = BT_CLASS;
9101 attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
9102 attr.class_ok = 0;
9103 attr.associate_var = 1;
9104 attr.dimension = attr.codimension = 0;
9105 attr.class_pointer = 1;
9106 if (!gfc_build_class_symbol (&sym->ts, &attr, &as))
9107 gcc_unreachable ();
9108 /* Make sure the _vptr is set. */
9109 c = gfc_find_component (sym->ts.u.derived, "_vptr", true, true, NULL);
9110 if (c->ts.u.derived == NULL)
9111 c->ts.u.derived = gfc_find_derived_vtab (sym->ts.u.derived);
9112 CLASS_DATA (sym)->attr.pointer = 1;
9113 CLASS_DATA (sym)->attr.class_pointer = 1;
9114 gfc_set_sym_referenced (sym->ts.u.derived);
9115 gfc_commit_symbol (sym->ts.u.derived);
9116 /* _vptr now has the _vtab in it, change it to the _vtype. */
9117 if (c->ts.u.derived->attr.vtab)
9118 c->ts.u.derived = c->ts.u.derived->ts.u.derived;
9119 c->ts.u.derived->ns->types_resolved = 0;
9120 resolve_types (c->ts.u.derived->ns);
9121 }
9122 }
9123
9124 /* Mark this as an associate variable. */
9125 sym->attr.associate_var = 1;
9126
9127 /* Fix up the type-spec for CHARACTER types. */
9128 if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
9129 {
9130 if (!sym->ts.u.cl)
9131 sym->ts.u.cl = target->ts.u.cl;
9132
9133 if (sym->ts.deferred && target->expr_type == EXPR_VARIABLE
9134 && target->symtree->n.sym->attr.dummy
9135 && sym->ts.u.cl == target->ts.u.cl)
9136 {
9137 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
9138 sym->ts.deferred = 1;
9139 }
9140
9141 if (!sym->ts.u.cl->length
9142 && !sym->ts.deferred
9143 && target->expr_type == EXPR_CONSTANT)
9144 {
9145 sym->ts.u.cl->length =
9146 gfc_get_int_expr (gfc_charlen_int_kind, NULL,
9147 target->value.character.length);
9148 }
9149 else if ((!sym->ts.u.cl->length
9150 || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
9151 && target->expr_type != EXPR_VARIABLE)
9152 {
9153 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
9154 sym->ts.deferred = 1;
9155
9156 /* This is reset in trans-stmt.c after the assignment
9157 of the target expression to the associate name. */
9158 sym->attr.allocatable = 1;
9159 }
9160 }
9161
9162 /* If the target is a good class object, so is the associate variable. */
9163 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
9164 sym->attr.class_ok = 1;
9165 }
9166
9167
9168 /* Ensure that SELECT TYPE expressions have the correct rank and a full
9169 array reference, where necessary. The symbols are artificial and so
9170 the dimension attribute and arrayspec can also be set. In addition,
9171 sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
9172 This is corrected here as well.*/
9173
9174 static void
9175 fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2,
9176 int rank, gfc_ref *ref)
9177 {
9178 gfc_ref *nref = (*expr1)->ref;
9179 gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
9180 gfc_symbol *sym2 = expr2 ? expr2->symtree->n.sym : NULL;
9181 (*expr1)->rank = rank;
9182 if (sym1->ts.type == BT_CLASS)
9183 {
9184 if ((*expr1)->ts.type != BT_CLASS)
9185 (*expr1)->ts = sym1->ts;
9186
9187 CLASS_DATA (sym1)->attr.dimension = 1;
9188 if (CLASS_DATA (sym1)->as == NULL && sym2)
9189 CLASS_DATA (sym1)->as
9190 = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
9191 }
9192 else
9193 {
9194 sym1->attr.dimension = 1;
9195 if (sym1->as == NULL && sym2)
9196 sym1->as = gfc_copy_array_spec (sym2->as);
9197 }
9198
9199 for (; nref; nref = nref->next)
9200 if (nref->next == NULL)
9201 break;
9202
9203 if (ref && nref && nref->type != REF_ARRAY)
9204 nref->next = gfc_copy_ref (ref);
9205 else if (ref && !nref)
9206 (*expr1)->ref = gfc_copy_ref (ref);
9207 }
9208
9209
9210 static gfc_expr *
9211 build_loc_call (gfc_expr *sym_expr)
9212 {
9213 gfc_expr *loc_call;
9214 loc_call = gfc_get_expr ();
9215 loc_call->expr_type = EXPR_FUNCTION;
9216 gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
9217 loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
9218 loc_call->symtree->n.sym->attr.intrinsic = 1;
9219 loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
9220 gfc_commit_symbol (loc_call->symtree->n.sym);
9221 loc_call->ts.type = BT_INTEGER;
9222 loc_call->ts.kind = gfc_index_integer_kind;
9223 loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
9224 loc_call->value.function.actual = gfc_get_actual_arglist ();
9225 loc_call->value.function.actual->expr = sym_expr;
9226 loc_call->where = sym_expr->where;
9227 return loc_call;
9228 }
9229
9230 /* Resolve a SELECT TYPE statement. */
9231
9232 static void
9233 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
9234 {
9235 gfc_symbol *selector_type;
9236 gfc_code *body, *new_st, *if_st, *tail;
9237 gfc_code *class_is = NULL, *default_case = NULL;
9238 gfc_case *c;
9239 gfc_symtree *st;
9240 char name[GFC_MAX_SYMBOL_LEN];
9241 gfc_namespace *ns;
9242 int error = 0;
9243 int rank = 0;
9244 gfc_ref* ref = NULL;
9245 gfc_expr *selector_expr = NULL;
9246
9247 ns = code->ext.block.ns;
9248 gfc_resolve (ns);
9249
9250 /* Check for F03:C813. */
9251 if (code->expr1->ts.type != BT_CLASS
9252 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
9253 {
9254 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
9255 "at %L", &code->loc);
9256 return;
9257 }
9258
9259 if (!code->expr1->symtree->n.sym->attr.class_ok)
9260 return;
9261
9262 if (code->expr2)
9263 {
9264 gfc_ref *ref2 = NULL;
9265 for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
9266 if (ref->type == REF_COMPONENT
9267 && ref->u.c.component->ts.type == BT_CLASS)
9268 ref2 = ref;
9269
9270 if (ref2)
9271 {
9272 if (code->expr1->symtree->n.sym->attr.untyped)
9273 code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
9274 selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
9275 }
9276 else
9277 {
9278 if (code->expr1->symtree->n.sym->attr.untyped)
9279 code->expr1->symtree->n.sym->ts = code->expr2->ts;
9280 selector_type = CLASS_DATA (code->expr2)
9281 ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
9282 }
9283
9284 if (code->expr2->rank
9285 && code->expr1->ts.type == BT_CLASS
9286 && CLASS_DATA (code->expr1)->as)
9287 CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
9288
9289 /* F2008: C803 The selector expression must not be coindexed. */
9290 if (gfc_is_coindexed (code->expr2))
9291 {
9292 gfc_error ("Selector at %L must not be coindexed",
9293 &code->expr2->where);
9294 return;
9295 }
9296
9297 }
9298 else
9299 {
9300 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
9301
9302 if (gfc_is_coindexed (code->expr1))
9303 {
9304 gfc_error ("Selector at %L must not be coindexed",
9305 &code->expr1->where);
9306 return;
9307 }
9308 }
9309
9310 /* Loop over TYPE IS / CLASS IS cases. */
9311 for (body = code->block; body; body = body->block)
9312 {
9313 c = body->ext.block.case_list;
9314
9315 if (!error)
9316 {
9317 /* Check for repeated cases. */
9318 for (tail = code->block; tail; tail = tail->block)
9319 {
9320 gfc_case *d = tail->ext.block.case_list;
9321 if (tail == body)
9322 break;
9323
9324 if (c->ts.type == d->ts.type
9325 && ((c->ts.type == BT_DERIVED
9326 && c->ts.u.derived && d->ts.u.derived
9327 && !strcmp (c->ts.u.derived->name,
9328 d->ts.u.derived->name))
9329 || c->ts.type == BT_UNKNOWN
9330 || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9331 && c->ts.kind == d->ts.kind)))
9332 {
9333 gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
9334 &c->where, &d->where);
9335 return;
9336 }
9337 }
9338 }
9339
9340 /* Check F03:C815. */
9341 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9342 && !selector_type->attr.unlimited_polymorphic
9343 && !gfc_type_is_extensible (c->ts.u.derived))
9344 {
9345 gfc_error ("Derived type %qs at %L must be extensible",
9346 c->ts.u.derived->name, &c->where);
9347 error++;
9348 continue;
9349 }
9350
9351 /* Check F03:C816. */
9352 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
9353 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
9354 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
9355 {
9356 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9357 gfc_error ("Derived type %qs at %L must be an extension of %qs",
9358 c->ts.u.derived->name, &c->where, selector_type->name);
9359 else
9360 gfc_error ("Unexpected intrinsic type %qs at %L",
9361 gfc_basic_typename (c->ts.type), &c->where);
9362 error++;
9363 continue;
9364 }
9365
9366 /* Check F03:C814. */
9367 if (c->ts.type == BT_CHARACTER
9368 && (c->ts.u.cl->length != NULL || c->ts.deferred))
9369 {
9370 gfc_error ("The type-spec at %L shall specify that each length "
9371 "type parameter is assumed", &c->where);
9372 error++;
9373 continue;
9374 }
9375
9376 /* Intercept the DEFAULT case. */
9377 if (c->ts.type == BT_UNKNOWN)
9378 {
9379 /* Check F03:C818. */
9380 if (default_case)
9381 {
9382 gfc_error ("The DEFAULT CASE at %L cannot be followed "
9383 "by a second DEFAULT CASE at %L",
9384 &default_case->ext.block.case_list->where, &c->where);
9385 error++;
9386 continue;
9387 }
9388
9389 default_case = body;
9390 }
9391 }
9392
9393 if (error > 0)
9394 return;
9395
9396 /* Transform SELECT TYPE statement to BLOCK and associate selector to
9397 target if present. If there are any EXIT statements referring to the
9398 SELECT TYPE construct, this is no problem because the gfc_code
9399 reference stays the same and EXIT is equally possible from the BLOCK
9400 it is changed to. */
9401 code->op = EXEC_BLOCK;
9402 if (code->expr2)
9403 {
9404 gfc_association_list* assoc;
9405
9406 assoc = gfc_get_association_list ();
9407 assoc->st = code->expr1->symtree;
9408 assoc->target = gfc_copy_expr (code->expr2);
9409 assoc->target->where = code->expr2->where;
9410 /* assoc->variable will be set by resolve_assoc_var. */
9411
9412 code->ext.block.assoc = assoc;
9413 code->expr1->symtree->n.sym->assoc = assoc;
9414
9415 resolve_assoc_var (code->expr1->symtree->n.sym, false);
9416 }
9417 else
9418 code->ext.block.assoc = NULL;
9419
9420 /* Ensure that the selector rank and arrayspec are available to
9421 correct expressions in which they might be missing. */
9422 if (code->expr2 && code->expr2->rank)
9423 {
9424 rank = code->expr2->rank;
9425 for (ref = code->expr2->ref; ref; ref = ref->next)
9426 if (ref->next == NULL)
9427 break;
9428 if (ref && ref->type == REF_ARRAY)
9429 ref = gfc_copy_ref (ref);
9430
9431 /* Fixup expr1 if necessary. */
9432 if (rank)
9433 fixup_array_ref (&code->expr1, code->expr2, rank, ref);
9434 }
9435 else if (code->expr1->rank)
9436 {
9437 rank = code->expr1->rank;
9438 for (ref = code->expr1->ref; ref; ref = ref->next)
9439 if (ref->next == NULL)
9440 break;
9441 if (ref && ref->type == REF_ARRAY)
9442 ref = gfc_copy_ref (ref);
9443 }
9444
9445 /* Add EXEC_SELECT to switch on type. */
9446 new_st = gfc_get_code (code->op);
9447 new_st->expr1 = code->expr1;
9448 new_st->expr2 = code->expr2;
9449 new_st->block = code->block;
9450 code->expr1 = code->expr2 = NULL;
9451 code->block = NULL;
9452 if (!ns->code)
9453 ns->code = new_st;
9454 else
9455 ns->code->next = new_st;
9456 code = new_st;
9457 code->op = EXEC_SELECT_TYPE;
9458
9459 /* Use the intrinsic LOC function to generate an integer expression
9460 for the vtable of the selector. Note that the rank of the selector
9461 expression has to be set to zero. */
9462 gfc_add_vptr_component (code->expr1);
9463 code->expr1->rank = 0;
9464 code->expr1 = build_loc_call (code->expr1);
9465 selector_expr = code->expr1->value.function.actual->expr;
9466
9467 /* Loop over TYPE IS / CLASS IS cases. */
9468 for (body = code->block; body; body = body->block)
9469 {
9470 gfc_symbol *vtab;
9471 gfc_expr *e;
9472 c = body->ext.block.case_list;
9473
9474 /* Generate an index integer expression for address of the
9475 TYPE/CLASS vtable and store it in c->low. The hash expression
9476 is stored in c->high and is used to resolve intrinsic cases. */
9477 if (c->ts.type != BT_UNKNOWN)
9478 {
9479 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9480 {
9481 vtab = gfc_find_derived_vtab (c->ts.u.derived);
9482 gcc_assert (vtab);
9483 c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
9484 c->ts.u.derived->hash_value);
9485 }
9486 else
9487 {
9488 vtab = gfc_find_vtab (&c->ts);
9489 gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
9490 e = CLASS_DATA (vtab)->initializer;
9491 c->high = gfc_copy_expr (e);
9492 if (c->high->ts.kind != gfc_integer_4_kind)
9493 {
9494 gfc_typespec ts;
9495 ts.kind = gfc_integer_4_kind;
9496 ts.type = BT_INTEGER;
9497 gfc_convert_type_warn (c->high, &ts, 2, 0);
9498 }
9499 }
9500
9501 e = gfc_lval_expr_from_sym (vtab);
9502 c->low = build_loc_call (e);
9503 }
9504 else
9505 continue;
9506
9507 /* Associate temporary to selector. This should only be done
9508 when this case is actually true, so build a new ASSOCIATE
9509 that does precisely this here (instead of using the
9510 'global' one). */
9511
9512 if (c->ts.type == BT_CLASS)
9513 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
9514 else if (c->ts.type == BT_DERIVED)
9515 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
9516 else if (c->ts.type == BT_CHARACTER)
9517 {
9518 HOST_WIDE_INT charlen = 0;
9519 if (c->ts.u.cl && c->ts.u.cl->length
9520 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9521 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
9522 snprintf (name, sizeof (name),
9523 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
9524 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
9525 }
9526 else
9527 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
9528 c->ts.kind);
9529
9530 st = gfc_find_symtree (ns->sym_root, name);
9531 gcc_assert (st->n.sym->assoc);
9532 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
9533 st->n.sym->assoc->target->where = selector_expr->where;
9534 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
9535 {
9536 gfc_add_data_component (st->n.sym->assoc->target);
9537 /* Fixup the target expression if necessary. */
9538 if (rank)
9539 fixup_array_ref (&st->n.sym->assoc->target, NULL, rank, ref);
9540 }
9541
9542 new_st = gfc_get_code (EXEC_BLOCK);
9543 new_st->ext.block.ns = gfc_build_block_ns (ns);
9544 new_st->ext.block.ns->code = body->next;
9545 body->next = new_st;
9546
9547 /* Chain in the new list only if it is marked as dangling. Otherwise
9548 there is a CASE label overlap and this is already used. Just ignore,
9549 the error is diagnosed elsewhere. */
9550 if (st->n.sym->assoc->dangling)
9551 {
9552 new_st->ext.block.assoc = st->n.sym->assoc;
9553 st->n.sym->assoc->dangling = 0;
9554 }
9555
9556 resolve_assoc_var (st->n.sym, false);
9557 }
9558
9559 /* Take out CLASS IS cases for separate treatment. */
9560 body = code;
9561 while (body && body->block)
9562 {
9563 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
9564 {
9565 /* Add to class_is list. */
9566 if (class_is == NULL)
9567 {
9568 class_is = body->block;
9569 tail = class_is;
9570 }
9571 else
9572 {
9573 for (tail = class_is; tail->block; tail = tail->block) ;
9574 tail->block = body->block;
9575 tail = tail->block;
9576 }
9577 /* Remove from EXEC_SELECT list. */
9578 body->block = body->block->block;
9579 tail->block = NULL;
9580 }
9581 else
9582 body = body->block;
9583 }
9584
9585 if (class_is)
9586 {
9587 gfc_symbol *vtab;
9588
9589 if (!default_case)
9590 {
9591 /* Add a default case to hold the CLASS IS cases. */
9592 for (tail = code; tail->block; tail = tail->block) ;
9593 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
9594 tail = tail->block;
9595 tail->ext.block.case_list = gfc_get_case ();
9596 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
9597 tail->next = NULL;
9598 default_case = tail;
9599 }
9600
9601 /* More than one CLASS IS block? */
9602 if (class_is->block)
9603 {
9604 gfc_code **c1,*c2;
9605 bool swapped;
9606 /* Sort CLASS IS blocks by extension level. */
9607 do
9608 {
9609 swapped = false;
9610 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
9611 {
9612 c2 = (*c1)->block;
9613 /* F03:C817 (check for doubles). */
9614 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
9615 == c2->ext.block.case_list->ts.u.derived->hash_value)
9616 {
9617 gfc_error ("Double CLASS IS block in SELECT TYPE "
9618 "statement at %L",
9619 &c2->ext.block.case_list->where);
9620 return;
9621 }
9622 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
9623 < c2->ext.block.case_list->ts.u.derived->attr.extension)
9624 {
9625 /* Swap. */
9626 (*c1)->block = c2->block;
9627 c2->block = *c1;
9628 *c1 = c2;
9629 swapped = true;
9630 }
9631 }
9632 }
9633 while (swapped);
9634 }
9635
9636 /* Generate IF chain. */
9637 if_st = gfc_get_code (EXEC_IF);
9638 new_st = if_st;
9639 for (body = class_is; body; body = body->block)
9640 {
9641 new_st->block = gfc_get_code (EXEC_IF);
9642 new_st = new_st->block;
9643 /* Set up IF condition: Call _gfortran_is_extension_of. */
9644 new_st->expr1 = gfc_get_expr ();
9645 new_st->expr1->expr_type = EXPR_FUNCTION;
9646 new_st->expr1->ts.type = BT_LOGICAL;
9647 new_st->expr1->ts.kind = 4;
9648 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
9649 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
9650 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
9651 /* Set up arguments. */
9652 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
9653 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
9654 new_st->expr1->value.function.actual->expr->where = code->loc;
9655 new_st->expr1->where = code->loc;
9656 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
9657 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
9658 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
9659 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
9660 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
9661 new_st->expr1->value.function.actual->next->expr->where = code->loc;
9662 /* Set up types in formal arg list. */
9663 new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
9664 new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
9665 new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
9666 new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
9667
9668 new_st->next = body->next;
9669 }
9670 if (default_case->next)
9671 {
9672 new_st->block = gfc_get_code (EXEC_IF);
9673 new_st = new_st->block;
9674 new_st->next = default_case->next;
9675 }
9676
9677 /* Replace CLASS DEFAULT code by the IF chain. */
9678 default_case->next = if_st;
9679 }
9680
9681 /* Resolve the internal code. This cannot be done earlier because
9682 it requires that the sym->assoc of selectors is set already. */
9683 gfc_current_ns = ns;
9684 gfc_resolve_blocks (code->block, gfc_current_ns);
9685 gfc_current_ns = old_ns;
9686
9687 if (ref)
9688 free (ref);
9689 }
9690
9691
9692 /* Resolve a SELECT RANK statement. */
9693
9694 static void
9695 resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
9696 {
9697 gfc_namespace *ns;
9698 gfc_code *body, *new_st, *tail;
9699 gfc_case *c;
9700 char tname[GFC_MAX_SYMBOL_LEN + 7];
9701 char name[2 * GFC_MAX_SYMBOL_LEN];
9702 gfc_symtree *st;
9703 gfc_expr *selector_expr = NULL;
9704 int case_value;
9705 HOST_WIDE_INT charlen = 0;
9706
9707 ns = code->ext.block.ns;
9708 gfc_resolve (ns);
9709
9710 code->op = EXEC_BLOCK;
9711 if (code->expr2)
9712 {
9713 gfc_association_list* assoc;
9714
9715 assoc = gfc_get_association_list ();
9716 assoc->st = code->expr1->symtree;
9717 assoc->target = gfc_copy_expr (code->expr2);
9718 assoc->target->where = code->expr2->where;
9719 /* assoc->variable will be set by resolve_assoc_var. */
9720
9721 code->ext.block.assoc = assoc;
9722 code->expr1->symtree->n.sym->assoc = assoc;
9723
9724 resolve_assoc_var (code->expr1->symtree->n.sym, false);
9725 }
9726 else
9727 code->ext.block.assoc = NULL;
9728
9729 /* Loop over RANK cases. Note that returning on the errors causes a
9730 cascade of further errors because the case blocks do not compile
9731 correctly. */
9732 for (body = code->block; body; body = body->block)
9733 {
9734 c = body->ext.block.case_list;
9735 if (c->low)
9736 case_value = (int) mpz_get_si (c->low->value.integer);
9737 else
9738 case_value = -2;
9739
9740 /* Check for repeated cases. */
9741 for (tail = code->block; tail; tail = tail->block)
9742 {
9743 gfc_case *d = tail->ext.block.case_list;
9744 int case_value2;
9745
9746 if (tail == body)
9747 break;
9748
9749 /* Check F2018: C1153. */
9750 if (!c->low && !d->low)
9751 gfc_error ("RANK DEFAULT at %L is repeated at %L",
9752 &c->where, &d->where);
9753
9754 if (!c->low || !d->low)
9755 continue;
9756
9757 /* Check F2018: C1153. */
9758 case_value2 = (int) mpz_get_si (d->low->value.integer);
9759 if ((case_value == case_value2) && case_value == -1)
9760 gfc_error ("RANK (*) at %L is repeated at %L",
9761 &c->where, &d->where);
9762 else if (case_value == case_value2)
9763 gfc_error ("RANK (%i) at %L is repeated at %L",
9764 case_value, &c->where, &d->where);
9765 }
9766
9767 if (!c->low)
9768 continue;
9769
9770 /* Check F2018: C1155. */
9771 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
9772 || gfc_expr_attr (code->expr1).pointer))
9773 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9774 "allocatable selector at %L", &c->where, &code->expr1->where);
9775
9776 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
9777 || gfc_expr_attr (code->expr1).pointer))
9778 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9779 "allocatable selector at %L", &c->where, &code->expr1->where);
9780 }
9781
9782 /* Add EXEC_SELECT to switch on rank. */
9783 new_st = gfc_get_code (code->op);
9784 new_st->expr1 = code->expr1;
9785 new_st->expr2 = code->expr2;
9786 new_st->block = code->block;
9787 code->expr1 = code->expr2 = NULL;
9788 code->block = NULL;
9789 if (!ns->code)
9790 ns->code = new_st;
9791 else
9792 ns->code->next = new_st;
9793 code = new_st;
9794 code->op = EXEC_SELECT_RANK;
9795
9796 selector_expr = code->expr1;
9797
9798 /* Loop over SELECT RANK cases. */
9799 for (body = code->block; body; body = body->block)
9800 {
9801 c = body->ext.block.case_list;
9802 int case_value;
9803
9804 /* Pass on the default case. */
9805 if (c->low == NULL)
9806 continue;
9807
9808 /* Associate temporary to selector. This should only be done
9809 when this case is actually true, so build a new ASSOCIATE
9810 that does precisely this here (instead of using the
9811 'global' one). */
9812 if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
9813 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9814 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
9815
9816 if (c->ts.type == BT_CLASS)
9817 sprintf (tname, "class_%s", c->ts.u.derived->name);
9818 else if (c->ts.type == BT_DERIVED)
9819 sprintf (tname, "type_%s", c->ts.u.derived->name);
9820 else if (c->ts.type != BT_CHARACTER)
9821 sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
9822 else
9823 sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
9824 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
9825
9826 case_value = (int) mpz_get_si (c->low->value.integer);
9827 if (case_value >= 0)
9828 sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
9829 else
9830 sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
9831
9832 st = gfc_find_symtree (ns->sym_root, name);
9833 gcc_assert (st->n.sym->assoc);
9834
9835 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
9836 st->n.sym->assoc->target->where = selector_expr->where;
9837
9838 new_st = gfc_get_code (EXEC_BLOCK);
9839 new_st->ext.block.ns = gfc_build_block_ns (ns);
9840 new_st->ext.block.ns->code = body->next;
9841 body->next = new_st;
9842
9843 /* Chain in the new list only if it is marked as dangling. Otherwise
9844 there is a CASE label overlap and this is already used. Just ignore,
9845 the error is diagnosed elsewhere. */
9846 if (st->n.sym->assoc->dangling)
9847 {
9848 new_st->ext.block.assoc = st->n.sym->assoc;
9849 st->n.sym->assoc->dangling = 0;
9850 }
9851
9852 resolve_assoc_var (st->n.sym, false);
9853 }
9854
9855 gfc_current_ns = ns;
9856 gfc_resolve_blocks (code->block, gfc_current_ns);
9857 gfc_current_ns = old_ns;
9858 }
9859
9860
9861 /* Resolve a transfer statement. This is making sure that:
9862 -- a derived type being transferred has only non-pointer components
9863 -- a derived type being transferred doesn't have private components, unless
9864 it's being transferred from the module where the type was defined
9865 -- we're not trying to transfer a whole assumed size array. */
9866
9867 static void
9868 resolve_transfer (gfc_code *code)
9869 {
9870 gfc_symbol *sym, *derived;
9871 gfc_ref *ref;
9872 gfc_expr *exp;
9873 bool write = false;
9874 bool formatted = false;
9875 gfc_dt *dt = code->ext.dt;
9876 gfc_symbol *dtio_sub = NULL;
9877
9878 exp = code->expr1;
9879
9880 while (exp != NULL && exp->expr_type == EXPR_OP
9881 && exp->value.op.op == INTRINSIC_PARENTHESES)
9882 exp = exp->value.op.op1;
9883
9884 if (exp && exp->expr_type == EXPR_NULL
9885 && code->ext.dt)
9886 {
9887 gfc_error ("Invalid context for NULL () intrinsic at %L",
9888 &exp->where);
9889 return;
9890 }
9891
9892 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
9893 && exp->expr_type != EXPR_FUNCTION
9894 && exp->expr_type != EXPR_STRUCTURE))
9895 return;
9896
9897 /* If we are reading, the variable will be changed. Note that
9898 code->ext.dt may be NULL if the TRANSFER is related to
9899 an INQUIRE statement -- but in this case, we are not reading, either. */
9900 if (dt && dt->dt_io_kind->value.iokind == M_READ
9901 && !gfc_check_vardef_context (exp, false, false, false,
9902 _("item in READ")))
9903 return;
9904
9905 const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
9906 || exp->expr_type == EXPR_FUNCTION
9907 ? &exp->ts : &exp->symtree->n.sym->ts;
9908
9909 /* Go to actual component transferred. */
9910 for (ref = exp->ref; ref; ref = ref->next)
9911 if (ref->type == REF_COMPONENT)
9912 ts = &ref->u.c.component->ts;
9913
9914 if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
9915 && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
9916 {
9917 derived = ts->u.derived;
9918
9919 /* Determine when to use the formatted DTIO procedure. */
9920 if (dt && (dt->format_expr || dt->format_label))
9921 formatted = true;
9922
9923 write = dt->dt_io_kind->value.iokind == M_WRITE
9924 || dt->dt_io_kind->value.iokind == M_PRINT;
9925 dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
9926
9927 if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
9928 {
9929 dt->udtio = exp;
9930 sym = exp->symtree->n.sym->ns->proc_name;
9931 /* Check to see if this is a nested DTIO call, with the
9932 dummy as the io-list object. */
9933 if (sym && sym == dtio_sub && sym->formal
9934 && sym->formal->sym == exp->symtree->n.sym
9935 && exp->ref == NULL)
9936 {
9937 if (!sym->attr.recursive)
9938 {
9939 gfc_error ("DTIO %s procedure at %L must be recursive",
9940 sym->name, &sym->declared_at);
9941 return;
9942 }
9943 }
9944 }
9945 }
9946
9947 if (ts->type == BT_CLASS && dtio_sub == NULL)
9948 {
9949 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
9950 "it is processed by a defined input/output procedure",
9951 &code->loc);
9952 return;
9953 }
9954
9955 if (ts->type == BT_DERIVED)
9956 {
9957 /* Check that transferred derived type doesn't contain POINTER
9958 components unless it is processed by a defined input/output
9959 procedure". */
9960 if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
9961 {
9962 gfc_error ("Data transfer element at %L cannot have POINTER "
9963 "components unless it is processed by a defined "
9964 "input/output procedure", &code->loc);
9965 return;
9966 }
9967
9968 /* F08:C935. */
9969 if (ts->u.derived->attr.proc_pointer_comp)
9970 {
9971 gfc_error ("Data transfer element at %L cannot have "
9972 "procedure pointer components", &code->loc);
9973 return;
9974 }
9975
9976 if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
9977 {
9978 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
9979 "components unless it is processed by a defined "
9980 "input/output procedure", &code->loc);
9981 return;
9982 }
9983
9984 /* C_PTR and C_FUNPTR have private components which means they cannot
9985 be printed. However, if -std=gnu and not -pedantic, allow
9986 the component to be printed to help debugging. */
9987 if (ts->u.derived->ts.f90_type == BT_VOID)
9988 {
9989 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
9990 "cannot have PRIVATE components", &code->loc))
9991 return;
9992 }
9993 else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
9994 {
9995 gfc_error ("Data transfer element at %L cannot have "
9996 "PRIVATE components unless it is processed by "
9997 "a defined input/output procedure", &code->loc);
9998 return;
9999 }
10000 }
10001
10002 if (exp->expr_type == EXPR_STRUCTURE)
10003 return;
10004
10005 sym = exp->symtree->n.sym;
10006
10007 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
10008 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
10009 {
10010 gfc_error ("Data transfer element at %L cannot be a full reference to "
10011 "an assumed-size array", &code->loc);
10012 return;
10013 }
10014 }
10015
10016
10017 /*********** Toplevel code resolution subroutines ***********/
10018
10019 /* Find the set of labels that are reachable from this block. We also
10020 record the last statement in each block. */
10021
10022 static void
10023 find_reachable_labels (gfc_code *block)
10024 {
10025 gfc_code *c;
10026
10027 if (!block)
10028 return;
10029
10030 cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
10031
10032 /* Collect labels in this block. We don't keep those corresponding
10033 to END {IF|SELECT}, these are checked in resolve_branch by going
10034 up through the code_stack. */
10035 for (c = block; c; c = c->next)
10036 {
10037 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
10038 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
10039 }
10040
10041 /* Merge with labels from parent block. */
10042 if (cs_base->prev)
10043 {
10044 gcc_assert (cs_base->prev->reachable_labels);
10045 bitmap_ior_into (cs_base->reachable_labels,
10046 cs_base->prev->reachable_labels);
10047 }
10048 }
10049
10050
10051 static void
10052 resolve_lock_unlock_event (gfc_code *code)
10053 {
10054 if (code->expr1->expr_type == EXPR_FUNCTION
10055 && code->expr1->value.function.isym
10056 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
10057 remove_caf_get_intrinsic (code->expr1);
10058
10059 if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
10060 && (code->expr1->ts.type != BT_DERIVED
10061 || code->expr1->expr_type != EXPR_VARIABLE
10062 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
10063 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
10064 || code->expr1->rank != 0
10065 || (!gfc_is_coarray (code->expr1) &&
10066 !gfc_is_coindexed (code->expr1))))
10067 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
10068 &code->expr1->where);
10069 else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
10070 && (code->expr1->ts.type != BT_DERIVED
10071 || code->expr1->expr_type != EXPR_VARIABLE
10072 || code->expr1->ts.u.derived->from_intmod
10073 != INTMOD_ISO_FORTRAN_ENV
10074 || code->expr1->ts.u.derived->intmod_sym_id
10075 != ISOFORTRAN_EVENT_TYPE
10076 || code->expr1->rank != 0))
10077 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
10078 &code->expr1->where);
10079 else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
10080 && !gfc_is_coindexed (code->expr1))
10081 gfc_error ("Event variable argument at %L must be a coarray or coindexed",
10082 &code->expr1->where);
10083 else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
10084 gfc_error ("Event variable argument at %L must be a coarray but not "
10085 "coindexed", &code->expr1->where);
10086
10087 /* Check STAT. */
10088 if (code->expr2
10089 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
10090 || code->expr2->expr_type != EXPR_VARIABLE))
10091 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10092 &code->expr2->where);
10093
10094 if (code->expr2
10095 && !gfc_check_vardef_context (code->expr2, false, false, false,
10096 _("STAT variable")))
10097 return;
10098
10099 /* Check ERRMSG. */
10100 if (code->expr3
10101 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
10102 || code->expr3->expr_type != EXPR_VARIABLE))
10103 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10104 &code->expr3->where);
10105
10106 if (code->expr3
10107 && !gfc_check_vardef_context (code->expr3, false, false, false,
10108 _("ERRMSG variable")))
10109 return;
10110
10111 /* Check for LOCK the ACQUIRED_LOCK. */
10112 if (code->op != EXEC_EVENT_WAIT && code->expr4
10113 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
10114 || code->expr4->expr_type != EXPR_VARIABLE))
10115 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
10116 "variable", &code->expr4->where);
10117
10118 if (code->op != EXEC_EVENT_WAIT && code->expr4
10119 && !gfc_check_vardef_context (code->expr4, false, false, false,
10120 _("ACQUIRED_LOCK variable")))
10121 return;
10122
10123 /* Check for EVENT WAIT the UNTIL_COUNT. */
10124 if (code->op == EXEC_EVENT_WAIT && code->expr4)
10125 {
10126 if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
10127 || code->expr4->rank != 0)
10128 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
10129 "expression", &code->expr4->where);
10130 }
10131 }
10132
10133
10134 static void
10135 resolve_critical (gfc_code *code)
10136 {
10137 gfc_symtree *symtree;
10138 gfc_symbol *lock_type;
10139 char name[GFC_MAX_SYMBOL_LEN];
10140 static int serial = 0;
10141
10142 if (flag_coarray != GFC_FCOARRAY_LIB)
10143 return;
10144
10145 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
10146 GFC_PREFIX ("lock_type"));
10147 if (symtree)
10148 lock_type = symtree->n.sym;
10149 else
10150 {
10151 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
10152 false) != 0)
10153 gcc_unreachable ();
10154 lock_type = symtree->n.sym;
10155 lock_type->attr.flavor = FL_DERIVED;
10156 lock_type->attr.zero_comp = 1;
10157 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
10158 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
10159 }
10160
10161 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
10162 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
10163 gcc_unreachable ();
10164
10165 code->resolved_sym = symtree->n.sym;
10166 symtree->n.sym->attr.flavor = FL_VARIABLE;
10167 symtree->n.sym->attr.referenced = 1;
10168 symtree->n.sym->attr.artificial = 1;
10169 symtree->n.sym->attr.codimension = 1;
10170 symtree->n.sym->ts.type = BT_DERIVED;
10171 symtree->n.sym->ts.u.derived = lock_type;
10172 symtree->n.sym->as = gfc_get_array_spec ();
10173 symtree->n.sym->as->corank = 1;
10174 symtree->n.sym->as->type = AS_EXPLICIT;
10175 symtree->n.sym->as->cotype = AS_EXPLICIT;
10176 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
10177 NULL, 1);
10178 gfc_commit_symbols();
10179 }
10180
10181
10182 static void
10183 resolve_sync (gfc_code *code)
10184 {
10185 /* Check imageset. The * case matches expr1 == NULL. */
10186 if (code->expr1)
10187 {
10188 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
10189 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
10190 "INTEGER expression", &code->expr1->where);
10191 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
10192 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
10193 gfc_error ("Imageset argument at %L must between 1 and num_images()",
10194 &code->expr1->where);
10195 else if (code->expr1->expr_type == EXPR_ARRAY
10196 && gfc_simplify_expr (code->expr1, 0))
10197 {
10198 gfc_constructor *cons;
10199 cons = gfc_constructor_first (code->expr1->value.constructor);
10200 for (; cons; cons = gfc_constructor_next (cons))
10201 if (cons->expr->expr_type == EXPR_CONSTANT
10202 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
10203 gfc_error ("Imageset argument at %L must between 1 and "
10204 "num_images()", &cons->expr->where);
10205 }
10206 }
10207
10208 /* Check STAT. */
10209 gfc_resolve_expr (code->expr2);
10210 if (code->expr2
10211 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
10212 || code->expr2->expr_type != EXPR_VARIABLE))
10213 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10214 &code->expr2->where);
10215
10216 /* Check ERRMSG. */
10217 gfc_resolve_expr (code->expr3);
10218 if (code->expr3
10219 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
10220 || code->expr3->expr_type != EXPR_VARIABLE))
10221 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10222 &code->expr3->where);
10223 }
10224
10225
10226 /* Given a branch to a label, see if the branch is conforming.
10227 The code node describes where the branch is located. */
10228
10229 static void
10230 resolve_branch (gfc_st_label *label, gfc_code *code)
10231 {
10232 code_stack *stack;
10233
10234 if (label == NULL)
10235 return;
10236
10237 /* Step one: is this a valid branching target? */
10238
10239 if (label->defined == ST_LABEL_UNKNOWN)
10240 {
10241 gfc_error ("Label %d referenced at %L is never defined", label->value,
10242 &code->loc);
10243 return;
10244 }
10245
10246 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
10247 {
10248 gfc_error ("Statement at %L is not a valid branch target statement "
10249 "for the branch statement at %L", &label->where, &code->loc);
10250 return;
10251 }
10252
10253 /* Step two: make sure this branch is not a branch to itself ;-) */
10254
10255 if (code->here == label)
10256 {
10257 gfc_warning (0,
10258 "Branch at %L may result in an infinite loop", &code->loc);
10259 return;
10260 }
10261
10262 /* Step three: See if the label is in the same block as the
10263 branching statement. The hard work has been done by setting up
10264 the bitmap reachable_labels. */
10265
10266 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
10267 {
10268 /* Check now whether there is a CRITICAL construct; if so, check
10269 whether the label is still visible outside of the CRITICAL block,
10270 which is invalid. */
10271 for (stack = cs_base; stack; stack = stack->prev)
10272 {
10273 if (stack->current->op == EXEC_CRITICAL
10274 && bitmap_bit_p (stack->reachable_labels, label->value))
10275 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
10276 "label at %L", &code->loc, &label->where);
10277 else if (stack->current->op == EXEC_DO_CONCURRENT
10278 && bitmap_bit_p (stack->reachable_labels, label->value))
10279 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
10280 "for label at %L", &code->loc, &label->where);
10281 }
10282
10283 return;
10284 }
10285
10286 /* Step four: If we haven't found the label in the bitmap, it may
10287 still be the label of the END of the enclosing block, in which
10288 case we find it by going up the code_stack. */
10289
10290 for (stack = cs_base; stack; stack = stack->prev)
10291 {
10292 if (stack->current->next && stack->current->next->here == label)
10293 break;
10294 if (stack->current->op == EXEC_CRITICAL)
10295 {
10296 /* Note: A label at END CRITICAL does not leave the CRITICAL
10297 construct as END CRITICAL is still part of it. */
10298 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
10299 " at %L", &code->loc, &label->where);
10300 return;
10301 }
10302 else if (stack->current->op == EXEC_DO_CONCURRENT)
10303 {
10304 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
10305 "label at %L", &code->loc, &label->where);
10306 return;
10307 }
10308 }
10309
10310 if (stack)
10311 {
10312 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
10313 return;
10314 }
10315
10316 /* The label is not in an enclosing block, so illegal. This was
10317 allowed in Fortran 66, so we allow it as extension. No
10318 further checks are necessary in this case. */
10319 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
10320 "as the GOTO statement at %L", &label->where,
10321 &code->loc);
10322 return;
10323 }
10324
10325
10326 /* Check whether EXPR1 has the same shape as EXPR2. */
10327
10328 static bool
10329 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
10330 {
10331 mpz_t shape[GFC_MAX_DIMENSIONS];
10332 mpz_t shape2[GFC_MAX_DIMENSIONS];
10333 bool result = false;
10334 int i;
10335
10336 /* Compare the rank. */
10337 if (expr1->rank != expr2->rank)
10338 return result;
10339
10340 /* Compare the size of each dimension. */
10341 for (i=0; i<expr1->rank; i++)
10342 {
10343 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
10344 goto ignore;
10345
10346 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
10347 goto ignore;
10348
10349 if (mpz_cmp (shape[i], shape2[i]))
10350 goto over;
10351 }
10352
10353 /* When either of the two expression is an assumed size array, we
10354 ignore the comparison of dimension sizes. */
10355 ignore:
10356 result = true;
10357
10358 over:
10359 gfc_clear_shape (shape, i);
10360 gfc_clear_shape (shape2, i);
10361 return result;
10362 }
10363
10364
10365 /* Check whether a WHERE assignment target or a WHERE mask expression
10366 has the same shape as the outmost WHERE mask expression. */
10367
10368 static void
10369 resolve_where (gfc_code *code, gfc_expr *mask)
10370 {
10371 gfc_code *cblock;
10372 gfc_code *cnext;
10373 gfc_expr *e = NULL;
10374
10375 cblock = code->block;
10376
10377 /* Store the first WHERE mask-expr of the WHERE statement or construct.
10378 In case of nested WHERE, only the outmost one is stored. */
10379 if (mask == NULL) /* outmost WHERE */
10380 e = cblock->expr1;
10381 else /* inner WHERE */
10382 e = mask;
10383
10384 while (cblock)
10385 {
10386 if (cblock->expr1)
10387 {
10388 /* Check if the mask-expr has a consistent shape with the
10389 outmost WHERE mask-expr. */
10390 if (!resolve_where_shape (cblock->expr1, e))
10391 gfc_error ("WHERE mask at %L has inconsistent shape",
10392 &cblock->expr1->where);
10393 }
10394
10395 /* the assignment statement of a WHERE statement, or the first
10396 statement in where-body-construct of a WHERE construct */
10397 cnext = cblock->next;
10398 while (cnext)
10399 {
10400 switch (cnext->op)
10401 {
10402 /* WHERE assignment statement */
10403 case EXEC_ASSIGN:
10404
10405 /* Check shape consistent for WHERE assignment target. */
10406 if (e && !resolve_where_shape (cnext->expr1, e))
10407 gfc_error ("WHERE assignment target at %L has "
10408 "inconsistent shape", &cnext->expr1->where);
10409 break;
10410
10411
10412 case EXEC_ASSIGN_CALL:
10413 resolve_call (cnext);
10414 if (!cnext->resolved_sym->attr.elemental)
10415 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10416 &cnext->ext.actual->expr->where);
10417 break;
10418
10419 /* WHERE or WHERE construct is part of a where-body-construct */
10420 case EXEC_WHERE:
10421 resolve_where (cnext, e);
10422 break;
10423
10424 default:
10425 gfc_error ("Unsupported statement inside WHERE at %L",
10426 &cnext->loc);
10427 }
10428 /* the next statement within the same where-body-construct */
10429 cnext = cnext->next;
10430 }
10431 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10432 cblock = cblock->block;
10433 }
10434 }
10435
10436
10437 /* Resolve assignment in FORALL construct.
10438 NVAR is the number of FORALL index variables, and VAR_EXPR records the
10439 FORALL index variables. */
10440
10441 static void
10442 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
10443 {
10444 int n;
10445
10446 for (n = 0; n < nvar; n++)
10447 {
10448 gfc_symbol *forall_index;
10449
10450 forall_index = var_expr[n]->symtree->n.sym;
10451
10452 /* Check whether the assignment target is one of the FORALL index
10453 variable. */
10454 if ((code->expr1->expr_type == EXPR_VARIABLE)
10455 && (code->expr1->symtree->n.sym == forall_index))
10456 gfc_error ("Assignment to a FORALL index variable at %L",
10457 &code->expr1->where);
10458 else
10459 {
10460 /* If one of the FORALL index variables doesn't appear in the
10461 assignment variable, then there could be a many-to-one
10462 assignment. Emit a warning rather than an error because the
10463 mask could be resolving this problem. */
10464 if (!find_forall_index (code->expr1, forall_index, 0))
10465 gfc_warning (0, "The FORALL with index %qs is not used on the "
10466 "left side of the assignment at %L and so might "
10467 "cause multiple assignment to this object",
10468 var_expr[n]->symtree->name, &code->expr1->where);
10469 }
10470 }
10471 }
10472
10473
10474 /* Resolve WHERE statement in FORALL construct. */
10475
10476 static void
10477 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
10478 gfc_expr **var_expr)
10479 {
10480 gfc_code *cblock;
10481 gfc_code *cnext;
10482
10483 cblock = code->block;
10484 while (cblock)
10485 {
10486 /* the assignment statement of a WHERE statement, or the first
10487 statement in where-body-construct of a WHERE construct */
10488 cnext = cblock->next;
10489 while (cnext)
10490 {
10491 switch (cnext->op)
10492 {
10493 /* WHERE assignment statement */
10494 case EXEC_ASSIGN:
10495 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
10496 break;
10497
10498 /* WHERE operator assignment statement */
10499 case EXEC_ASSIGN_CALL:
10500 resolve_call (cnext);
10501 if (!cnext->resolved_sym->attr.elemental)
10502 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10503 &cnext->ext.actual->expr->where);
10504 break;
10505
10506 /* WHERE or WHERE construct is part of a where-body-construct */
10507 case EXEC_WHERE:
10508 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
10509 break;
10510
10511 default:
10512 gfc_error ("Unsupported statement inside WHERE at %L",
10513 &cnext->loc);
10514 }
10515 /* the next statement within the same where-body-construct */
10516 cnext = cnext->next;
10517 }
10518 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10519 cblock = cblock->block;
10520 }
10521 }
10522
10523
10524 /* Traverse the FORALL body to check whether the following errors exist:
10525 1. For assignment, check if a many-to-one assignment happens.
10526 2. For WHERE statement, check the WHERE body to see if there is any
10527 many-to-one assignment. */
10528
10529 static void
10530 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
10531 {
10532 gfc_code *c;
10533
10534 c = code->block->next;
10535 while (c)
10536 {
10537 switch (c->op)
10538 {
10539 case EXEC_ASSIGN:
10540 case EXEC_POINTER_ASSIGN:
10541 gfc_resolve_assign_in_forall (c, nvar, var_expr);
10542 break;
10543
10544 case EXEC_ASSIGN_CALL:
10545 resolve_call (c);
10546 break;
10547
10548 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
10549 there is no need to handle it here. */
10550 case EXEC_FORALL:
10551 break;
10552 case EXEC_WHERE:
10553 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
10554 break;
10555 default:
10556 break;
10557 }
10558 /* The next statement in the FORALL body. */
10559 c = c->next;
10560 }
10561 }
10562
10563
10564 /* Counts the number of iterators needed inside a forall construct, including
10565 nested forall constructs. This is used to allocate the needed memory
10566 in gfc_resolve_forall. */
10567
10568 static int
10569 gfc_count_forall_iterators (gfc_code *code)
10570 {
10571 int max_iters, sub_iters, current_iters;
10572 gfc_forall_iterator *fa;
10573
10574 gcc_assert(code->op == EXEC_FORALL);
10575 max_iters = 0;
10576 current_iters = 0;
10577
10578 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
10579 current_iters ++;
10580
10581 code = code->block->next;
10582
10583 while (code)
10584 {
10585 if (code->op == EXEC_FORALL)
10586 {
10587 sub_iters = gfc_count_forall_iterators (code);
10588 if (sub_iters > max_iters)
10589 max_iters = sub_iters;
10590 }
10591 code = code->next;
10592 }
10593
10594 return current_iters + max_iters;
10595 }
10596
10597
10598 /* Given a FORALL construct, first resolve the FORALL iterator, then call
10599 gfc_resolve_forall_body to resolve the FORALL body. */
10600
10601 static void
10602 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
10603 {
10604 static gfc_expr **var_expr;
10605 static int total_var = 0;
10606 static int nvar = 0;
10607 int i, old_nvar, tmp;
10608 gfc_forall_iterator *fa;
10609
10610 old_nvar = nvar;
10611
10612 if (!gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
10613 return;
10614
10615 /* Start to resolve a FORALL construct */
10616 if (forall_save == 0)
10617 {
10618 /* Count the total number of FORALL indices in the nested FORALL
10619 construct in order to allocate the VAR_EXPR with proper size. */
10620 total_var = gfc_count_forall_iterators (code);
10621
10622 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
10623 var_expr = XCNEWVEC (gfc_expr *, total_var);
10624 }
10625
10626 /* The information about FORALL iterator, including FORALL indices start, end
10627 and stride. An outer FORALL indice cannot appear in start, end or stride. */
10628 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
10629 {
10630 /* Fortran 20008: C738 (R753). */
10631 if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
10632 {
10633 gfc_error ("FORALL index-name at %L must be a scalar variable "
10634 "of type integer", &fa->var->where);
10635 continue;
10636 }
10637
10638 /* Check if any outer FORALL index name is the same as the current
10639 one. */
10640 for (i = 0; i < nvar; i++)
10641 {
10642 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
10643 gfc_error ("An outer FORALL construct already has an index "
10644 "with this name %L", &fa->var->where);
10645 }
10646
10647 /* Record the current FORALL index. */
10648 var_expr[nvar] = gfc_copy_expr (fa->var);
10649
10650 nvar++;
10651
10652 /* No memory leak. */
10653 gcc_assert (nvar <= total_var);
10654 }
10655
10656 /* Resolve the FORALL body. */
10657 gfc_resolve_forall_body (code, nvar, var_expr);
10658
10659 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
10660 gfc_resolve_blocks (code->block, ns);
10661
10662 tmp = nvar;
10663 nvar = old_nvar;
10664 /* Free only the VAR_EXPRs allocated in this frame. */
10665 for (i = nvar; i < tmp; i++)
10666 gfc_free_expr (var_expr[i]);
10667
10668 if (nvar == 0)
10669 {
10670 /* We are in the outermost FORALL construct. */
10671 gcc_assert (forall_save == 0);
10672
10673 /* VAR_EXPR is not needed any more. */
10674 free (var_expr);
10675 total_var = 0;
10676 }
10677 }
10678
10679
10680 /* Resolve a BLOCK construct statement. */
10681
10682 static void
10683 resolve_block_construct (gfc_code* code)
10684 {
10685 /* Resolve the BLOCK's namespace. */
10686 gfc_resolve (code->ext.block.ns);
10687
10688 /* For an ASSOCIATE block, the associations (and their targets) are already
10689 resolved during resolve_symbol. */
10690 }
10691
10692
10693 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
10694 DO code nodes. */
10695
10696 void
10697 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
10698 {
10699 bool t;
10700
10701 for (; b; b = b->block)
10702 {
10703 t = gfc_resolve_expr (b->expr1);
10704 if (!gfc_resolve_expr (b->expr2))
10705 t = false;
10706
10707 switch (b->op)
10708 {
10709 case EXEC_IF:
10710 if (t && b->expr1 != NULL
10711 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
10712 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
10713 &b->expr1->where);
10714 break;
10715
10716 case EXEC_WHERE:
10717 if (t
10718 && b->expr1 != NULL
10719 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
10720 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
10721 &b->expr1->where);
10722 break;
10723
10724 case EXEC_GOTO:
10725 resolve_branch (b->label1, b);
10726 break;
10727
10728 case EXEC_BLOCK:
10729 resolve_block_construct (b);
10730 break;
10731
10732 case EXEC_SELECT:
10733 case EXEC_SELECT_TYPE:
10734 case EXEC_SELECT_RANK:
10735 case EXEC_FORALL:
10736 case EXEC_DO:
10737 case EXEC_DO_WHILE:
10738 case EXEC_DO_CONCURRENT:
10739 case EXEC_CRITICAL:
10740 case EXEC_READ:
10741 case EXEC_WRITE:
10742 case EXEC_IOLENGTH:
10743 case EXEC_WAIT:
10744 break;
10745
10746 case EXEC_OMP_ATOMIC:
10747 case EXEC_OACC_ATOMIC:
10748 {
10749 /* Verify this before calling gfc_resolve_code, which might
10750 change it. */
10751 gcc_assert (b->next && b->next->op == EXEC_ASSIGN);
10752 gcc_assert ((!b->ext.omp_clauses->capture
10753 && b->next->next == NULL)
10754 || (b->ext.omp_clauses->capture
10755 && b->next->next != NULL
10756 && b->next->next->op == EXEC_ASSIGN
10757 && b->next->next->next == NULL));
10758 }
10759 break;
10760
10761 case EXEC_OACC_PARALLEL_LOOP:
10762 case EXEC_OACC_PARALLEL:
10763 case EXEC_OACC_KERNELS_LOOP:
10764 case EXEC_OACC_KERNELS:
10765 case EXEC_OACC_SERIAL_LOOP:
10766 case EXEC_OACC_SERIAL:
10767 case EXEC_OACC_DATA:
10768 case EXEC_OACC_HOST_DATA:
10769 case EXEC_OACC_LOOP:
10770 case EXEC_OACC_UPDATE:
10771 case EXEC_OACC_WAIT:
10772 case EXEC_OACC_CACHE:
10773 case EXEC_OACC_ENTER_DATA:
10774 case EXEC_OACC_EXIT_DATA:
10775 case EXEC_OACC_ROUTINE:
10776 case EXEC_OMP_CRITICAL:
10777 case EXEC_OMP_DISTRIBUTE:
10778 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
10779 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
10780 case EXEC_OMP_DISTRIBUTE_SIMD:
10781 case EXEC_OMP_DO:
10782 case EXEC_OMP_DO_SIMD:
10783 case EXEC_OMP_MASTER:
10784 case EXEC_OMP_ORDERED:
10785 case EXEC_OMP_PARALLEL:
10786 case EXEC_OMP_PARALLEL_DO:
10787 case EXEC_OMP_PARALLEL_DO_SIMD:
10788 case EXEC_OMP_PARALLEL_SECTIONS:
10789 case EXEC_OMP_PARALLEL_WORKSHARE:
10790 case EXEC_OMP_SECTIONS:
10791 case EXEC_OMP_SIMD:
10792 case EXEC_OMP_SINGLE:
10793 case EXEC_OMP_TARGET:
10794 case EXEC_OMP_TARGET_DATA:
10795 case EXEC_OMP_TARGET_ENTER_DATA:
10796 case EXEC_OMP_TARGET_EXIT_DATA:
10797 case EXEC_OMP_TARGET_PARALLEL:
10798 case EXEC_OMP_TARGET_PARALLEL_DO:
10799 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
10800 case EXEC_OMP_TARGET_SIMD:
10801 case EXEC_OMP_TARGET_TEAMS:
10802 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10803 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10804 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10805 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10806 case EXEC_OMP_TARGET_UPDATE:
10807 case EXEC_OMP_TASK:
10808 case EXEC_OMP_TASKGROUP:
10809 case EXEC_OMP_TASKLOOP:
10810 case EXEC_OMP_TASKLOOP_SIMD:
10811 case EXEC_OMP_TASKWAIT:
10812 case EXEC_OMP_TASKYIELD:
10813 case EXEC_OMP_TEAMS:
10814 case EXEC_OMP_TEAMS_DISTRIBUTE:
10815 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10816 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10817 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10818 case EXEC_OMP_WORKSHARE:
10819 break;
10820
10821 default:
10822 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
10823 }
10824
10825 gfc_resolve_code (b->next, ns);
10826 }
10827 }
10828
10829
10830 /* Does everything to resolve an ordinary assignment. Returns true
10831 if this is an interface assignment. */
10832 static bool
10833 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
10834 {
10835 bool rval = false;
10836 gfc_expr *lhs;
10837 gfc_expr *rhs;
10838 int n;
10839 gfc_ref *ref;
10840 symbol_attribute attr;
10841
10842 if (gfc_extend_assign (code, ns))
10843 {
10844 gfc_expr** rhsptr;
10845
10846 if (code->op == EXEC_ASSIGN_CALL)
10847 {
10848 lhs = code->ext.actual->expr;
10849 rhsptr = &code->ext.actual->next->expr;
10850 }
10851 else
10852 {
10853 gfc_actual_arglist* args;
10854 gfc_typebound_proc* tbp;
10855
10856 gcc_assert (code->op == EXEC_COMPCALL);
10857
10858 args = code->expr1->value.compcall.actual;
10859 lhs = args->expr;
10860 rhsptr = &args->next->expr;
10861
10862 tbp = code->expr1->value.compcall.tbp;
10863 gcc_assert (!tbp->is_generic);
10864 }
10865
10866 /* Make a temporary rhs when there is a default initializer
10867 and rhs is the same symbol as the lhs. */
10868 if ((*rhsptr)->expr_type == EXPR_VARIABLE
10869 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
10870 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
10871 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
10872 *rhsptr = gfc_get_parentheses (*rhsptr);
10873
10874 return true;
10875 }
10876
10877 lhs = code->expr1;
10878 rhs = code->expr2;
10879
10880 if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
10881 && rhs->ts.type == BT_CHARACTER
10882 && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
10883 {
10884 /* Use of -fdec-char-conversions allows assignment of character data
10885 to non-character variables. This not permited for nonconstant
10886 strings. */
10887 gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
10888 gfc_typename (lhs), &rhs->where);
10889 return false;
10890 }
10891
10892 /* Handle the case of a BOZ literal on the RHS. */
10893 if (rhs->ts.type == BT_BOZ)
10894 {
10895 if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
10896 "statement value nor an actual argument of "
10897 "INT/REAL/DBLE/CMPLX intrinsic subprogram",
10898 &rhs->where))
10899 return false;
10900
10901 switch (lhs->ts.type)
10902 {
10903 case BT_INTEGER:
10904 if (!gfc_boz2int (rhs, lhs->ts.kind))
10905 return false;
10906 break;
10907 case BT_REAL:
10908 if (!gfc_boz2real (rhs, lhs->ts.kind))
10909 return false;
10910 break;
10911 default:
10912 gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
10913 return false;
10914 }
10915 }
10916
10917 if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
10918 {
10919 HOST_WIDE_INT llen = 0, rlen = 0;
10920 if (lhs->ts.u.cl != NULL
10921 && lhs->ts.u.cl->length != NULL
10922 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10923 llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
10924
10925 if (rhs->expr_type == EXPR_CONSTANT)
10926 rlen = rhs->value.character.length;
10927
10928 else if (rhs->ts.u.cl != NULL
10929 && rhs->ts.u.cl->length != NULL
10930 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10931 rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
10932
10933 if (rlen && llen && rlen > llen)
10934 gfc_warning_now (OPT_Wcharacter_truncation,
10935 "CHARACTER expression will be truncated "
10936 "in assignment (%ld/%ld) at %L",
10937 (long) llen, (long) rlen, &code->loc);
10938 }
10939
10940 /* Ensure that a vector index expression for the lvalue is evaluated
10941 to a temporary if the lvalue symbol is referenced in it. */
10942 if (lhs->rank)
10943 {
10944 for (ref = lhs->ref; ref; ref= ref->next)
10945 if (ref->type == REF_ARRAY)
10946 {
10947 for (n = 0; n < ref->u.ar.dimen; n++)
10948 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
10949 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
10950 ref->u.ar.start[n]))
10951 ref->u.ar.start[n]
10952 = gfc_get_parentheses (ref->u.ar.start[n]);
10953 }
10954 }
10955
10956 if (gfc_pure (NULL))
10957 {
10958 if (lhs->ts.type == BT_DERIVED
10959 && lhs->expr_type == EXPR_VARIABLE
10960 && lhs->ts.u.derived->attr.pointer_comp
10961 && rhs->expr_type == EXPR_VARIABLE
10962 && (gfc_impure_variable (rhs->symtree->n.sym)
10963 || gfc_is_coindexed (rhs)))
10964 {
10965 /* F2008, C1283. */
10966 if (gfc_is_coindexed (rhs))
10967 gfc_error ("Coindexed expression at %L is assigned to "
10968 "a derived type variable with a POINTER "
10969 "component in a PURE procedure",
10970 &rhs->where);
10971 else
10972 /* F2008, C1283 (4). */
10973 gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
10974 "shall not be used as the expr at %L of an intrinsic "
10975 "assignment statement in which the variable is of a "
10976 "derived type if the derived type has a pointer "
10977 "component at any level of component selection.",
10978 &rhs->where);
10979 return rval;
10980 }
10981
10982 /* Fortran 2008, C1283. */
10983 if (gfc_is_coindexed (lhs))
10984 {
10985 gfc_error ("Assignment to coindexed variable at %L in a PURE "
10986 "procedure", &rhs->where);
10987 return rval;
10988 }
10989 }
10990
10991 if (gfc_implicit_pure (NULL))
10992 {
10993 if (lhs->expr_type == EXPR_VARIABLE
10994 && lhs->symtree->n.sym != gfc_current_ns->proc_name
10995 && lhs->symtree->n.sym->ns != gfc_current_ns)
10996 gfc_unset_implicit_pure (NULL);
10997
10998 if (lhs->ts.type == BT_DERIVED
10999 && lhs->expr_type == EXPR_VARIABLE
11000 && lhs->ts.u.derived->attr.pointer_comp
11001 && rhs->expr_type == EXPR_VARIABLE
11002 && (gfc_impure_variable (rhs->symtree->n.sym)
11003 || gfc_is_coindexed (rhs)))
11004 gfc_unset_implicit_pure (NULL);
11005
11006 /* Fortran 2008, C1283. */
11007 if (gfc_is_coindexed (lhs))
11008 gfc_unset_implicit_pure (NULL);
11009 }
11010
11011 /* F2008, 7.2.1.2. */
11012 attr = gfc_expr_attr (lhs);
11013 if (lhs->ts.type == BT_CLASS && attr.allocatable)
11014 {
11015 if (attr.codimension)
11016 {
11017 gfc_error ("Assignment to polymorphic coarray at %L is not "
11018 "permitted", &lhs->where);
11019 return false;
11020 }
11021 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
11022 "polymorphic variable at %L", &lhs->where))
11023 return false;
11024 if (!flag_realloc_lhs)
11025 {
11026 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
11027 "requires %<-frealloc-lhs%>", &lhs->where);
11028 return false;
11029 }
11030 }
11031 else if (lhs->ts.type == BT_CLASS)
11032 {
11033 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
11034 "assignment at %L - check that there is a matching specific "
11035 "subroutine for '=' operator", &lhs->where);
11036 return false;
11037 }
11038
11039 bool lhs_coindexed = gfc_is_coindexed (lhs);
11040
11041 /* F2008, Section 7.2.1.2. */
11042 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
11043 {
11044 gfc_error ("Coindexed variable must not have an allocatable ultimate "
11045 "component in assignment at %L", &lhs->where);
11046 return false;
11047 }
11048
11049 /* Assign the 'data' of a class object to a derived type. */
11050 if (lhs->ts.type == BT_DERIVED
11051 && rhs->ts.type == BT_CLASS
11052 && rhs->expr_type != EXPR_ARRAY)
11053 gfc_add_data_component (rhs);
11054
11055 /* Make sure there is a vtable and, in particular, a _copy for the
11056 rhs type. */
11057 if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS)
11058 gfc_find_vtab (&rhs->ts);
11059
11060 bool caf_convert_to_send = flag_coarray == GFC_FCOARRAY_LIB
11061 && (lhs_coindexed
11062 || (code->expr2->expr_type == EXPR_FUNCTION
11063 && code->expr2->value.function.isym
11064 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
11065 && (code->expr1->rank == 0 || code->expr2->rank != 0)
11066 && !gfc_expr_attr (rhs).allocatable
11067 && !gfc_has_vector_subscript (rhs)));
11068
11069 gfc_check_assign (lhs, rhs, 1, !caf_convert_to_send);
11070
11071 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
11072 Additionally, insert this code when the RHS is a CAF as we then use the
11073 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
11074 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
11075 noncoindexed array and the RHS is a coindexed scalar, use the normal code
11076 path. */
11077 if (caf_convert_to_send)
11078 {
11079 if (code->expr2->expr_type == EXPR_FUNCTION
11080 && code->expr2->value.function.isym
11081 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
11082 remove_caf_get_intrinsic (code->expr2);
11083 code->op = EXEC_CALL;
11084 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
11085 code->resolved_sym = code->symtree->n.sym;
11086 code->resolved_sym->attr.flavor = FL_PROCEDURE;
11087 code->resolved_sym->attr.intrinsic = 1;
11088 code->resolved_sym->attr.subroutine = 1;
11089 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
11090 gfc_commit_symbol (code->resolved_sym);
11091 code->ext.actual = gfc_get_actual_arglist ();
11092 code->ext.actual->expr = lhs;
11093 code->ext.actual->next = gfc_get_actual_arglist ();
11094 code->ext.actual->next->expr = rhs;
11095 code->expr1 = NULL;
11096 code->expr2 = NULL;
11097 }
11098
11099 return false;
11100 }
11101
11102
11103 /* Add a component reference onto an expression. */
11104
11105 static void
11106 add_comp_ref (gfc_expr *e, gfc_component *c)
11107 {
11108 gfc_ref **ref;
11109 ref = &(e->ref);
11110 while (*ref)
11111 ref = &((*ref)->next);
11112 *ref = gfc_get_ref ();
11113 (*ref)->type = REF_COMPONENT;
11114 (*ref)->u.c.sym = e->ts.u.derived;
11115 (*ref)->u.c.component = c;
11116 e->ts = c->ts;
11117
11118 /* Add a full array ref, as necessary. */
11119 if (c->as)
11120 {
11121 gfc_add_full_array_ref (e, c->as);
11122 e->rank = c->as->rank;
11123 }
11124 }
11125
11126
11127 /* Build an assignment. Keep the argument 'op' for future use, so that
11128 pointer assignments can be made. */
11129
11130 static gfc_code *
11131 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
11132 gfc_component *comp1, gfc_component *comp2, locus loc)
11133 {
11134 gfc_code *this_code;
11135
11136 this_code = gfc_get_code (op);
11137 this_code->next = NULL;
11138 this_code->expr1 = gfc_copy_expr (expr1);
11139 this_code->expr2 = gfc_copy_expr (expr2);
11140 this_code->loc = loc;
11141 if (comp1 && comp2)
11142 {
11143 add_comp_ref (this_code->expr1, comp1);
11144 add_comp_ref (this_code->expr2, comp2);
11145 }
11146
11147 return this_code;
11148 }
11149
11150
11151 /* Makes a temporary variable expression based on the characteristics of
11152 a given variable expression. */
11153
11154 static gfc_expr*
11155 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
11156 {
11157 static int serial = 0;
11158 char name[GFC_MAX_SYMBOL_LEN];
11159 gfc_symtree *tmp;
11160 gfc_array_spec *as;
11161 gfc_array_ref *aref;
11162 gfc_ref *ref;
11163
11164 sprintf (name, GFC_PREFIX("DA%d"), serial++);
11165 gfc_get_sym_tree (name, ns, &tmp, false);
11166 gfc_add_type (tmp->n.sym, &e->ts, NULL);
11167
11168 if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
11169 tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
11170 NULL,
11171 e->value.character.length);
11172
11173 as = NULL;
11174 ref = NULL;
11175 aref = NULL;
11176
11177 /* Obtain the arrayspec for the temporary. */
11178 if (e->rank && e->expr_type != EXPR_ARRAY
11179 && e->expr_type != EXPR_FUNCTION
11180 && e->expr_type != EXPR_OP)
11181 {
11182 aref = gfc_find_array_ref (e);
11183 if (e->expr_type == EXPR_VARIABLE
11184 && e->symtree->n.sym->as == aref->as)
11185 as = aref->as;
11186 else
11187 {
11188 for (ref = e->ref; ref; ref = ref->next)
11189 if (ref->type == REF_COMPONENT
11190 && ref->u.c.component->as == aref->as)
11191 {
11192 as = aref->as;
11193 break;
11194 }
11195 }
11196 }
11197
11198 /* Add the attributes and the arrayspec to the temporary. */
11199 tmp->n.sym->attr = gfc_expr_attr (e);
11200 tmp->n.sym->attr.function = 0;
11201 tmp->n.sym->attr.proc_pointer = 0;
11202 tmp->n.sym->attr.result = 0;
11203 tmp->n.sym->attr.flavor = FL_VARIABLE;
11204 tmp->n.sym->attr.dummy = 0;
11205 tmp->n.sym->attr.use_assoc = 0;
11206 tmp->n.sym->attr.intent = INTENT_UNKNOWN;
11207
11208 if (as)
11209 {
11210 tmp->n.sym->as = gfc_copy_array_spec (as);
11211 if (!ref)
11212 ref = e->ref;
11213 if (as->type == AS_DEFERRED)
11214 tmp->n.sym->attr.allocatable = 1;
11215 }
11216 else if (e->rank && (e->expr_type == EXPR_ARRAY
11217 || e->expr_type == EXPR_FUNCTION
11218 || e->expr_type == EXPR_OP))
11219 {
11220 tmp->n.sym->as = gfc_get_array_spec ();
11221 tmp->n.sym->as->type = AS_DEFERRED;
11222 tmp->n.sym->as->rank = e->rank;
11223 tmp->n.sym->attr.allocatable = 1;
11224 tmp->n.sym->attr.dimension = 1;
11225 }
11226 else
11227 tmp->n.sym->attr.dimension = 0;
11228
11229 gfc_set_sym_referenced (tmp->n.sym);
11230 gfc_commit_symbol (tmp->n.sym);
11231 e = gfc_lval_expr_from_sym (tmp->n.sym);
11232
11233 /* Should the lhs be a section, use its array ref for the
11234 temporary expression. */
11235 if (aref && aref->type != AR_FULL)
11236 {
11237 gfc_free_ref_list (e->ref);
11238 e->ref = gfc_copy_ref (ref);
11239 }
11240 return e;
11241 }
11242
11243
11244 /* Add one line of code to the code chain, making sure that 'head' and
11245 'tail' are appropriately updated. */
11246
11247 static void
11248 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
11249 {
11250 gcc_assert (this_code);
11251 if (*head == NULL)
11252 *head = *tail = *this_code;
11253 else
11254 *tail = gfc_append_code (*tail, *this_code);
11255 *this_code = NULL;
11256 }
11257
11258
11259 /* Counts the potential number of part array references that would
11260 result from resolution of typebound defined assignments. */
11261
11262 static int
11263 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
11264 {
11265 gfc_component *c;
11266 int c_depth = 0, t_depth;
11267
11268 for (c= derived->components; c; c = c->next)
11269 {
11270 if ((!gfc_bt_struct (c->ts.type)
11271 || c->attr.pointer
11272 || c->attr.allocatable
11273 || c->attr.proc_pointer_comp
11274 || c->attr.class_pointer
11275 || c->attr.proc_pointer)
11276 && !c->attr.defined_assign_comp)
11277 continue;
11278
11279 if (c->as && c_depth == 0)
11280 c_depth = 1;
11281
11282 if (c->ts.u.derived->attr.defined_assign_comp)
11283 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
11284 c->as ? 1 : 0);
11285 else
11286 t_depth = 0;
11287
11288 c_depth = t_depth > c_depth ? t_depth : c_depth;
11289 }
11290 return depth + c_depth;
11291 }
11292
11293
11294 /* Implement 7.2.1.3 of the F08 standard:
11295 "An intrinsic assignment where the variable is of derived type is
11296 performed as if each component of the variable were assigned from the
11297 corresponding component of expr using pointer assignment (7.2.2) for
11298 each pointer component, defined assignment for each nonpointer
11299 nonallocatable component of a type that has a type-bound defined
11300 assignment consistent with the component, intrinsic assignment for
11301 each other nonpointer nonallocatable component, ..."
11302
11303 The pointer assignments are taken care of by the intrinsic
11304 assignment of the structure itself. This function recursively adds
11305 defined assignments where required. The recursion is accomplished
11306 by calling gfc_resolve_code.
11307
11308 When the lhs in a defined assignment has intent INOUT, we need a
11309 temporary for the lhs. In pseudo-code:
11310
11311 ! Only call function lhs once.
11312 if (lhs is not a constant or an variable)
11313 temp_x = expr2
11314 expr2 => temp_x
11315 ! Do the intrinsic assignment
11316 expr1 = expr2
11317 ! Now do the defined assignments
11318 do over components with typebound defined assignment [%cmp]
11319 #if one component's assignment procedure is INOUT
11320 t1 = expr1
11321 #if expr2 non-variable
11322 temp_x = expr2
11323 expr2 => temp_x
11324 # endif
11325 expr1 = expr2
11326 # for each cmp
11327 t1%cmp {defined=} expr2%cmp
11328 expr1%cmp = t1%cmp
11329 #else
11330 expr1 = expr2
11331
11332 # for each cmp
11333 expr1%cmp {defined=} expr2%cmp
11334 #endif
11335 */
11336
11337 /* The temporary assignments have to be put on top of the additional
11338 code to avoid the result being changed by the intrinsic assignment.
11339 */
11340 static int component_assignment_level = 0;
11341 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
11342
11343 static void
11344 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
11345 {
11346 gfc_component *comp1, *comp2;
11347 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
11348 gfc_expr *t1;
11349 int error_count, depth;
11350
11351 gfc_get_errors (NULL, &error_count);
11352
11353 /* Filter out continuing processing after an error. */
11354 if (error_count
11355 || (*code)->expr1->ts.type != BT_DERIVED
11356 || (*code)->expr2->ts.type != BT_DERIVED)
11357 return;
11358
11359 /* TODO: Handle more than one part array reference in assignments. */
11360 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
11361 (*code)->expr1->rank ? 1 : 0);
11362 if (depth > 1)
11363 {
11364 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
11365 "done because multiple part array references would "
11366 "occur in intermediate expressions.", &(*code)->loc);
11367 return;
11368 }
11369
11370 component_assignment_level++;
11371
11372 /* Create a temporary so that functions get called only once. */
11373 if ((*code)->expr2->expr_type != EXPR_VARIABLE
11374 && (*code)->expr2->expr_type != EXPR_CONSTANT)
11375 {
11376 gfc_expr *tmp_expr;
11377
11378 /* Assign the rhs to the temporary. */
11379 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
11380 this_code = build_assignment (EXEC_ASSIGN,
11381 tmp_expr, (*code)->expr2,
11382 NULL, NULL, (*code)->loc);
11383 /* Add the code and substitute the rhs expression. */
11384 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
11385 gfc_free_expr ((*code)->expr2);
11386 (*code)->expr2 = tmp_expr;
11387 }
11388
11389 /* Do the intrinsic assignment. This is not needed if the lhs is one
11390 of the temporaries generated here, since the intrinsic assignment
11391 to the final result already does this. */
11392 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
11393 {
11394 this_code = build_assignment (EXEC_ASSIGN,
11395 (*code)->expr1, (*code)->expr2,
11396 NULL, NULL, (*code)->loc);
11397 add_code_to_chain (&this_code, &head, &tail);
11398 }
11399
11400 comp1 = (*code)->expr1->ts.u.derived->components;
11401 comp2 = (*code)->expr2->ts.u.derived->components;
11402
11403 t1 = NULL;
11404 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
11405 {
11406 bool inout = false;
11407
11408 /* The intrinsic assignment does the right thing for pointers
11409 of all kinds and allocatable components. */
11410 if (!gfc_bt_struct (comp1->ts.type)
11411 || comp1->attr.pointer
11412 || comp1->attr.allocatable
11413 || comp1->attr.proc_pointer_comp
11414 || comp1->attr.class_pointer
11415 || comp1->attr.proc_pointer)
11416 continue;
11417
11418 /* Make an assigment for this component. */
11419 this_code = build_assignment (EXEC_ASSIGN,
11420 (*code)->expr1, (*code)->expr2,
11421 comp1, comp2, (*code)->loc);
11422
11423 /* Convert the assignment if there is a defined assignment for
11424 this type. Otherwise, using the call from gfc_resolve_code,
11425 recurse into its components. */
11426 gfc_resolve_code (this_code, ns);
11427
11428 if (this_code->op == EXEC_ASSIGN_CALL)
11429 {
11430 gfc_formal_arglist *dummy_args;
11431 gfc_symbol *rsym;
11432 /* Check that there is a typebound defined assignment. If not,
11433 then this must be a module defined assignment. We cannot
11434 use the defined_assign_comp attribute here because it must
11435 be this derived type that has the defined assignment and not
11436 a parent type. */
11437 if (!(comp1->ts.u.derived->f2k_derived
11438 && comp1->ts.u.derived->f2k_derived
11439 ->tb_op[INTRINSIC_ASSIGN]))
11440 {
11441 gfc_free_statements (this_code);
11442 this_code = NULL;
11443 continue;
11444 }
11445
11446 /* If the first argument of the subroutine has intent INOUT
11447 a temporary must be generated and used instead. */
11448 rsym = this_code->resolved_sym;
11449 dummy_args = gfc_sym_get_dummy_args (rsym);
11450 if (dummy_args
11451 && dummy_args->sym->attr.intent == INTENT_INOUT)
11452 {
11453 gfc_code *temp_code;
11454 inout = true;
11455
11456 /* Build the temporary required for the assignment and put
11457 it at the head of the generated code. */
11458 if (!t1)
11459 {
11460 t1 = get_temp_from_expr ((*code)->expr1, ns);
11461 temp_code = build_assignment (EXEC_ASSIGN,
11462 t1, (*code)->expr1,
11463 NULL, NULL, (*code)->loc);
11464
11465 /* For allocatable LHS, check whether it is allocated. Note
11466 that allocatable components with defined assignment are
11467 not yet support. See PR 57696. */
11468 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
11469 {
11470 gfc_code *block;
11471 gfc_expr *e =
11472 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
11473 block = gfc_get_code (EXEC_IF);
11474 block->block = gfc_get_code (EXEC_IF);
11475 block->block->expr1
11476 = gfc_build_intrinsic_call (ns,
11477 GFC_ISYM_ALLOCATED, "allocated",
11478 (*code)->loc, 1, e);
11479 block->block->next = temp_code;
11480 temp_code = block;
11481 }
11482 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
11483 }
11484
11485 /* Replace the first actual arg with the component of the
11486 temporary. */
11487 gfc_free_expr (this_code->ext.actual->expr);
11488 this_code->ext.actual->expr = gfc_copy_expr (t1);
11489 add_comp_ref (this_code->ext.actual->expr, comp1);
11490
11491 /* If the LHS variable is allocatable and wasn't allocated and
11492 the temporary is allocatable, pointer assign the address of
11493 the freshly allocated LHS to the temporary. */
11494 if ((*code)->expr1->symtree->n.sym->attr.allocatable
11495 && gfc_expr_attr ((*code)->expr1).allocatable)
11496 {
11497 gfc_code *block;
11498 gfc_expr *cond;
11499
11500 cond = gfc_get_expr ();
11501 cond->ts.type = BT_LOGICAL;
11502 cond->ts.kind = gfc_default_logical_kind;
11503 cond->expr_type = EXPR_OP;
11504 cond->where = (*code)->loc;
11505 cond->value.op.op = INTRINSIC_NOT;
11506 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
11507 GFC_ISYM_ALLOCATED, "allocated",
11508 (*code)->loc, 1, gfc_copy_expr (t1));
11509 block = gfc_get_code (EXEC_IF);
11510 block->block = gfc_get_code (EXEC_IF);
11511 block->block->expr1 = cond;
11512 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
11513 t1, (*code)->expr1,
11514 NULL, NULL, (*code)->loc);
11515 add_code_to_chain (&block, &head, &tail);
11516 }
11517 }
11518 }
11519 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
11520 {
11521 /* Don't add intrinsic assignments since they are already
11522 effected by the intrinsic assignment of the structure. */
11523 gfc_free_statements (this_code);
11524 this_code = NULL;
11525 continue;
11526 }
11527
11528 add_code_to_chain (&this_code, &head, &tail);
11529
11530 if (t1 && inout)
11531 {
11532 /* Transfer the value to the final result. */
11533 this_code = build_assignment (EXEC_ASSIGN,
11534 (*code)->expr1, t1,
11535 comp1, comp2, (*code)->loc);
11536 add_code_to_chain (&this_code, &head, &tail);
11537 }
11538 }
11539
11540 /* Put the temporary assignments at the top of the generated code. */
11541 if (tmp_head && component_assignment_level == 1)
11542 {
11543 gfc_append_code (tmp_head, head);
11544 head = tmp_head;
11545 tmp_head = tmp_tail = NULL;
11546 }
11547
11548 // If we did a pointer assignment - thus, we need to ensure that the LHS is
11549 // not accidentally deallocated. Hence, nullify t1.
11550 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
11551 && gfc_expr_attr ((*code)->expr1).allocatable)
11552 {
11553 gfc_code *block;
11554 gfc_expr *cond;
11555 gfc_expr *e;
11556
11557 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
11558 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
11559 (*code)->loc, 2, gfc_copy_expr (t1), e);
11560 block = gfc_get_code (EXEC_IF);
11561 block->block = gfc_get_code (EXEC_IF);
11562 block->block->expr1 = cond;
11563 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
11564 t1, gfc_get_null_expr (&(*code)->loc),
11565 NULL, NULL, (*code)->loc);
11566 gfc_append_code (tail, block);
11567 tail = block;
11568 }
11569
11570 /* Now attach the remaining code chain to the input code. Step on
11571 to the end of the new code since resolution is complete. */
11572 gcc_assert ((*code)->op == EXEC_ASSIGN);
11573 tail->next = (*code)->next;
11574 /* Overwrite 'code' because this would place the intrinsic assignment
11575 before the temporary for the lhs is created. */
11576 gfc_free_expr ((*code)->expr1);
11577 gfc_free_expr ((*code)->expr2);
11578 **code = *head;
11579 if (head != tail)
11580 free (head);
11581 *code = tail;
11582
11583 component_assignment_level--;
11584 }
11585
11586
11587 /* F2008: Pointer function assignments are of the form:
11588 ptr_fcn (args) = expr
11589 This function breaks these assignments into two statements:
11590 temporary_pointer => ptr_fcn(args)
11591 temporary_pointer = expr */
11592
11593 static bool
11594 resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
11595 {
11596 gfc_expr *tmp_ptr_expr;
11597 gfc_code *this_code;
11598 gfc_component *comp;
11599 gfc_symbol *s;
11600
11601 if ((*code)->expr1->expr_type != EXPR_FUNCTION)
11602 return false;
11603
11604 /* Even if standard does not support this feature, continue to build
11605 the two statements to avoid upsetting frontend_passes.c. */
11606 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
11607 "%L", &(*code)->loc);
11608
11609 comp = gfc_get_proc_ptr_comp ((*code)->expr1);
11610
11611 if (comp)
11612 s = comp->ts.interface;
11613 else
11614 s = (*code)->expr1->symtree->n.sym;
11615
11616 if (s == NULL || !s->result->attr.pointer)
11617 {
11618 gfc_error ("The function result on the lhs of the assignment at "
11619 "%L must have the pointer attribute.",
11620 &(*code)->expr1->where);
11621 (*code)->op = EXEC_NOP;
11622 return false;
11623 }
11624
11625 tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
11626
11627 /* get_temp_from_expression is set up for ordinary assignments. To that
11628 end, where array bounds are not known, arrays are made allocatable.
11629 Change the temporary to a pointer here. */
11630 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
11631 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
11632 tmp_ptr_expr->where = (*code)->loc;
11633
11634 this_code = build_assignment (EXEC_ASSIGN,
11635 tmp_ptr_expr, (*code)->expr2,
11636 NULL, NULL, (*code)->loc);
11637 this_code->next = (*code)->next;
11638 (*code)->next = this_code;
11639 (*code)->op = EXEC_POINTER_ASSIGN;
11640 (*code)->expr2 = (*code)->expr1;
11641 (*code)->expr1 = tmp_ptr_expr;
11642
11643 return true;
11644 }
11645
11646
11647 /* Deferred character length assignments from an operator expression
11648 require a temporary because the character length of the lhs can
11649 change in the course of the assignment. */
11650
11651 static bool
11652 deferred_op_assign (gfc_code **code, gfc_namespace *ns)
11653 {
11654 gfc_expr *tmp_expr;
11655 gfc_code *this_code;
11656
11657 if (!((*code)->expr1->ts.type == BT_CHARACTER
11658 && (*code)->expr1->ts.deferred && (*code)->expr1->rank
11659 && (*code)->expr2->expr_type == EXPR_OP))
11660 return false;
11661
11662 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
11663 return false;
11664
11665 if (gfc_expr_attr ((*code)->expr1).pointer)
11666 return false;
11667
11668 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
11669 tmp_expr->where = (*code)->loc;
11670
11671 /* A new charlen is required to ensure that the variable string
11672 length is different to that of the original lhs. */
11673 tmp_expr->ts.u.cl = gfc_get_charlen();
11674 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
11675 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
11676 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
11677
11678 tmp_expr->symtree->n.sym->ts.deferred = 1;
11679
11680 this_code = build_assignment (EXEC_ASSIGN,
11681 (*code)->expr1,
11682 gfc_copy_expr (tmp_expr),
11683 NULL, NULL, (*code)->loc);
11684
11685 (*code)->expr1 = tmp_expr;
11686
11687 this_code->next = (*code)->next;
11688 (*code)->next = this_code;
11689
11690 return true;
11691 }
11692
11693
11694 /* Given a block of code, recursively resolve everything pointed to by this
11695 code block. */
11696
11697 void
11698 gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
11699 {
11700 int omp_workshare_save;
11701 int forall_save, do_concurrent_save;
11702 code_stack frame;
11703 bool t;
11704
11705 frame.prev = cs_base;
11706 frame.head = code;
11707 cs_base = &frame;
11708
11709 find_reachable_labels (code);
11710
11711 for (; code; code = code->next)
11712 {
11713 frame.current = code;
11714 forall_save = forall_flag;
11715 do_concurrent_save = gfc_do_concurrent_flag;
11716
11717 if (code->op == EXEC_FORALL)
11718 {
11719 forall_flag = 1;
11720 gfc_resolve_forall (code, ns, forall_save);
11721 forall_flag = 2;
11722 }
11723 else if (code->block)
11724 {
11725 omp_workshare_save = -1;
11726 switch (code->op)
11727 {
11728 case EXEC_OACC_PARALLEL_LOOP:
11729 case EXEC_OACC_PARALLEL:
11730 case EXEC_OACC_KERNELS_LOOP:
11731 case EXEC_OACC_KERNELS:
11732 case EXEC_OACC_SERIAL_LOOP:
11733 case EXEC_OACC_SERIAL:
11734 case EXEC_OACC_DATA:
11735 case EXEC_OACC_HOST_DATA:
11736 case EXEC_OACC_LOOP:
11737 gfc_resolve_oacc_blocks (code, ns);
11738 break;
11739 case EXEC_OMP_PARALLEL_WORKSHARE:
11740 omp_workshare_save = omp_workshare_flag;
11741 omp_workshare_flag = 1;
11742 gfc_resolve_omp_parallel_blocks (code, ns);
11743 break;
11744 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
11745 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
11746 case EXEC_OMP_PARALLEL:
11747 case EXEC_OMP_PARALLEL_DO:
11748 case EXEC_OMP_PARALLEL_DO_SIMD:
11749 case EXEC_OMP_PARALLEL_SECTIONS:
11750 case EXEC_OMP_TARGET_PARALLEL:
11751 case EXEC_OMP_TARGET_PARALLEL_DO:
11752 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
11753 case EXEC_OMP_TARGET_TEAMS:
11754 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
11755 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
11756 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11757 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
11758 case EXEC_OMP_TASK:
11759 case EXEC_OMP_TASKLOOP:
11760 case EXEC_OMP_TASKLOOP_SIMD:
11761 case EXEC_OMP_TEAMS:
11762 case EXEC_OMP_TEAMS_DISTRIBUTE:
11763 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
11764 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11765 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
11766 omp_workshare_save = omp_workshare_flag;
11767 omp_workshare_flag = 0;
11768 gfc_resolve_omp_parallel_blocks (code, ns);
11769 break;
11770 case EXEC_OMP_DISTRIBUTE:
11771 case EXEC_OMP_DISTRIBUTE_SIMD:
11772 case EXEC_OMP_DO:
11773 case EXEC_OMP_DO_SIMD:
11774 case EXEC_OMP_SIMD:
11775 case EXEC_OMP_TARGET_SIMD:
11776 gfc_resolve_omp_do_blocks (code, ns);
11777 break;
11778 case EXEC_SELECT_TYPE:
11779 case EXEC_SELECT_RANK:
11780 /* Blocks are handled in resolve_select_type/rank because we
11781 have to transform the SELECT TYPE into ASSOCIATE first. */
11782 break;
11783 case EXEC_DO_CONCURRENT:
11784 gfc_do_concurrent_flag = 1;
11785 gfc_resolve_blocks (code->block, ns);
11786 gfc_do_concurrent_flag = 2;
11787 break;
11788 case EXEC_OMP_WORKSHARE:
11789 omp_workshare_save = omp_workshare_flag;
11790 omp_workshare_flag = 1;
11791 /* FALL THROUGH */
11792 default:
11793 gfc_resolve_blocks (code->block, ns);
11794 break;
11795 }
11796
11797 if (omp_workshare_save != -1)
11798 omp_workshare_flag = omp_workshare_save;
11799 }
11800 start:
11801 t = true;
11802 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
11803 t = gfc_resolve_expr (code->expr1);
11804 forall_flag = forall_save;
11805 gfc_do_concurrent_flag = do_concurrent_save;
11806
11807 if (!gfc_resolve_expr (code->expr2))
11808 t = false;
11809
11810 if (code->op == EXEC_ALLOCATE
11811 && !gfc_resolve_expr (code->expr3))
11812 t = false;
11813
11814 switch (code->op)
11815 {
11816 case EXEC_NOP:
11817 case EXEC_END_BLOCK:
11818 case EXEC_END_NESTED_BLOCK:
11819 case EXEC_CYCLE:
11820 case EXEC_PAUSE:
11821 case EXEC_STOP:
11822 case EXEC_ERROR_STOP:
11823 case EXEC_EXIT:
11824 case EXEC_CONTINUE:
11825 case EXEC_DT_END:
11826 case EXEC_ASSIGN_CALL:
11827 break;
11828
11829 case EXEC_CRITICAL:
11830 resolve_critical (code);
11831 break;
11832
11833 case EXEC_SYNC_ALL:
11834 case EXEC_SYNC_IMAGES:
11835 case EXEC_SYNC_MEMORY:
11836 resolve_sync (code);
11837 break;
11838
11839 case EXEC_LOCK:
11840 case EXEC_UNLOCK:
11841 case EXEC_EVENT_POST:
11842 case EXEC_EVENT_WAIT:
11843 resolve_lock_unlock_event (code);
11844 break;
11845
11846 case EXEC_FAIL_IMAGE:
11847 case EXEC_FORM_TEAM:
11848 case EXEC_CHANGE_TEAM:
11849 case EXEC_END_TEAM:
11850 case EXEC_SYNC_TEAM:
11851 break;
11852
11853 case EXEC_ENTRY:
11854 /* Keep track of which entry we are up to. */
11855 current_entry_id = code->ext.entry->id;
11856 break;
11857
11858 case EXEC_WHERE:
11859 resolve_where (code, NULL);
11860 break;
11861
11862 case EXEC_GOTO:
11863 if (code->expr1 != NULL)
11864 {
11865 if (code->expr1->expr_type != EXPR_VARIABLE
11866 || code->expr1->ts.type != BT_INTEGER
11867 || (code->expr1->ref
11868 && code->expr1->ref->type == REF_ARRAY)
11869 || code->expr1->symtree == NULL
11870 || (code->expr1->symtree->n.sym
11871 && (code->expr1->symtree->n.sym->attr.flavor
11872 == FL_PARAMETER)))
11873 gfc_error ("ASSIGNED GOTO statement at %L requires a "
11874 "scalar INTEGER variable", &code->expr1->where);
11875 else if (code->expr1->symtree->n.sym
11876 && code->expr1->symtree->n.sym->attr.assign != 1)
11877 gfc_error ("Variable %qs has not been assigned a target "
11878 "label at %L", code->expr1->symtree->n.sym->name,
11879 &code->expr1->where);
11880 }
11881 else
11882 resolve_branch (code->label1, code);
11883 break;
11884
11885 case EXEC_RETURN:
11886 if (code->expr1 != NULL
11887 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
11888 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
11889 "INTEGER return specifier", &code->expr1->where);
11890 break;
11891
11892 case EXEC_INIT_ASSIGN:
11893 case EXEC_END_PROCEDURE:
11894 break;
11895
11896 case EXEC_ASSIGN:
11897 if (!t)
11898 break;
11899
11900 if (code->expr1->ts.type == BT_CLASS)
11901 gfc_find_vtab (&code->expr2->ts);
11902
11903 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
11904 the LHS. */
11905 if (code->expr1->expr_type == EXPR_FUNCTION
11906 && code->expr1->value.function.isym
11907 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
11908 remove_caf_get_intrinsic (code->expr1);
11909
11910 /* If this is a pointer function in an lvalue variable context,
11911 the new code will have to be resolved afresh. This is also the
11912 case with an error, where the code is transformed into NOP to
11913 prevent ICEs downstream. */
11914 if (resolve_ptr_fcn_assign (&code, ns)
11915 || code->op == EXEC_NOP)
11916 goto start;
11917
11918 if (!gfc_check_vardef_context (code->expr1, false, false, false,
11919 _("assignment")))
11920 break;
11921
11922 if (resolve_ordinary_assign (code, ns))
11923 {
11924 if (code->op == EXEC_COMPCALL)
11925 goto compcall;
11926 else
11927 goto call;
11928 }
11929
11930 /* Check for dependencies in deferred character length array
11931 assignments and generate a temporary, if necessary. */
11932 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
11933 break;
11934
11935 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
11936 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
11937 && code->expr1->ts.u.derived
11938 && code->expr1->ts.u.derived->attr.defined_assign_comp)
11939 generate_component_assignments (&code, ns);
11940
11941 break;
11942
11943 case EXEC_LABEL_ASSIGN:
11944 if (code->label1->defined == ST_LABEL_UNKNOWN)
11945 gfc_error ("Label %d referenced at %L is never defined",
11946 code->label1->value, &code->label1->where);
11947 if (t
11948 && (code->expr1->expr_type != EXPR_VARIABLE
11949 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
11950 || code->expr1->symtree->n.sym->ts.kind
11951 != gfc_default_integer_kind
11952 || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
11953 || code->expr1->symtree->n.sym->as != NULL))
11954 gfc_error ("ASSIGN statement at %L requires a scalar "
11955 "default INTEGER variable", &code->expr1->where);
11956 break;
11957
11958 case EXEC_POINTER_ASSIGN:
11959 {
11960 gfc_expr* e;
11961
11962 if (!t)
11963 break;
11964
11965 /* This is both a variable definition and pointer assignment
11966 context, so check both of them. For rank remapping, a final
11967 array ref may be present on the LHS and fool gfc_expr_attr
11968 used in gfc_check_vardef_context. Remove it. */
11969 e = remove_last_array_ref (code->expr1);
11970 t = gfc_check_vardef_context (e, true, false, false,
11971 _("pointer assignment"));
11972 if (t)
11973 t = gfc_check_vardef_context (e, false, false, false,
11974 _("pointer assignment"));
11975 gfc_free_expr (e);
11976
11977 t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
11978
11979 if (!t)
11980 break;
11981
11982 /* Assigning a class object always is a regular assign. */
11983 if (code->expr2->ts.type == BT_CLASS
11984 && code->expr1->ts.type == BT_CLASS
11985 && !CLASS_DATA (code->expr2)->attr.dimension
11986 && !(gfc_expr_attr (code->expr1).proc_pointer
11987 && code->expr2->expr_type == EXPR_VARIABLE
11988 && code->expr2->symtree->n.sym->attr.flavor
11989 == FL_PROCEDURE))
11990 code->op = EXEC_ASSIGN;
11991 break;
11992 }
11993
11994 case EXEC_ARITHMETIC_IF:
11995 {
11996 gfc_expr *e = code->expr1;
11997
11998 gfc_resolve_expr (e);
11999 if (e->expr_type == EXPR_NULL)
12000 gfc_error ("Invalid NULL at %L", &e->where);
12001
12002 if (t && (e->rank > 0
12003 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
12004 gfc_error ("Arithmetic IF statement at %L requires a scalar "
12005 "REAL or INTEGER expression", &e->where);
12006
12007 resolve_branch (code->label1, code);
12008 resolve_branch (code->label2, code);
12009 resolve_branch (code->label3, code);
12010 }
12011 break;
12012
12013 case EXEC_IF:
12014 if (t && code->expr1 != NULL
12015 && (code->expr1->ts.type != BT_LOGICAL
12016 || code->expr1->rank != 0))
12017 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
12018 &code->expr1->where);
12019 break;
12020
12021 case EXEC_CALL:
12022 call:
12023 resolve_call (code);
12024 break;
12025
12026 case EXEC_COMPCALL:
12027 compcall:
12028 resolve_typebound_subroutine (code);
12029 break;
12030
12031 case EXEC_CALL_PPC:
12032 resolve_ppc_call (code);
12033 break;
12034
12035 case EXEC_SELECT:
12036 /* Select is complicated. Also, a SELECT construct could be
12037 a transformed computed GOTO. */
12038 resolve_select (code, false);
12039 break;
12040
12041 case EXEC_SELECT_TYPE:
12042 resolve_select_type (code, ns);
12043 break;
12044
12045 case EXEC_SELECT_RANK:
12046 resolve_select_rank (code, ns);
12047 break;
12048
12049 case EXEC_BLOCK:
12050 resolve_block_construct (code);
12051 break;
12052
12053 case EXEC_DO:
12054 if (code->ext.iterator != NULL)
12055 {
12056 gfc_iterator *iter = code->ext.iterator;
12057 if (gfc_resolve_iterator (iter, true, false))
12058 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
12059 true);
12060 }
12061 break;
12062
12063 case EXEC_DO_WHILE:
12064 if (code->expr1 == NULL)
12065 gfc_internal_error ("gfc_resolve_code(): No expression on "
12066 "DO WHILE");
12067 if (t
12068 && (code->expr1->rank != 0
12069 || code->expr1->ts.type != BT_LOGICAL))
12070 gfc_error ("Exit condition of DO WHILE loop at %L must be "
12071 "a scalar LOGICAL expression", &code->expr1->where);
12072 break;
12073
12074 case EXEC_ALLOCATE:
12075 if (t)
12076 resolve_allocate_deallocate (code, "ALLOCATE");
12077
12078 break;
12079
12080 case EXEC_DEALLOCATE:
12081 if (t)
12082 resolve_allocate_deallocate (code, "DEALLOCATE");
12083
12084 break;
12085
12086 case EXEC_OPEN:
12087 if (!gfc_resolve_open (code->ext.open, &code->loc))
12088 break;
12089
12090 resolve_branch (code->ext.open->err, code);
12091 break;
12092
12093 case EXEC_CLOSE:
12094 if (!gfc_resolve_close (code->ext.close, &code->loc))
12095 break;
12096
12097 resolve_branch (code->ext.close->err, code);
12098 break;
12099
12100 case EXEC_BACKSPACE:
12101 case EXEC_ENDFILE:
12102 case EXEC_REWIND:
12103 case EXEC_FLUSH:
12104 if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
12105 break;
12106
12107 resolve_branch (code->ext.filepos->err, code);
12108 break;
12109
12110 case EXEC_INQUIRE:
12111 if (!gfc_resolve_inquire (code->ext.inquire))
12112 break;
12113
12114 resolve_branch (code->ext.inquire->err, code);
12115 break;
12116
12117 case EXEC_IOLENGTH:
12118 gcc_assert (code->ext.inquire != NULL);
12119 if (!gfc_resolve_inquire (code->ext.inquire))
12120 break;
12121
12122 resolve_branch (code->ext.inquire->err, code);
12123 break;
12124
12125 case EXEC_WAIT:
12126 if (!gfc_resolve_wait (code->ext.wait))
12127 break;
12128
12129 resolve_branch (code->ext.wait->err, code);
12130 resolve_branch (code->ext.wait->end, code);
12131 resolve_branch (code->ext.wait->eor, code);
12132 break;
12133
12134 case EXEC_READ:
12135 case EXEC_WRITE:
12136 if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
12137 break;
12138
12139 resolve_branch (code->ext.dt->err, code);
12140 resolve_branch (code->ext.dt->end, code);
12141 resolve_branch (code->ext.dt->eor, code);
12142 break;
12143
12144 case EXEC_TRANSFER:
12145 resolve_transfer (code);
12146 break;
12147
12148 case EXEC_DO_CONCURRENT:
12149 case EXEC_FORALL:
12150 resolve_forall_iterators (code->ext.forall_iterator);
12151
12152 if (code->expr1 != NULL
12153 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
12154 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
12155 "expression", &code->expr1->where);
12156 break;
12157
12158 case EXEC_OACC_PARALLEL_LOOP:
12159 case EXEC_OACC_PARALLEL:
12160 case EXEC_OACC_KERNELS_LOOP:
12161 case EXEC_OACC_KERNELS:
12162 case EXEC_OACC_SERIAL_LOOP:
12163 case EXEC_OACC_SERIAL:
12164 case EXEC_OACC_DATA:
12165 case EXEC_OACC_HOST_DATA:
12166 case EXEC_OACC_LOOP:
12167 case EXEC_OACC_UPDATE:
12168 case EXEC_OACC_WAIT:
12169 case EXEC_OACC_CACHE:
12170 case EXEC_OACC_ENTER_DATA:
12171 case EXEC_OACC_EXIT_DATA:
12172 case EXEC_OACC_ATOMIC:
12173 case EXEC_OACC_DECLARE:
12174 gfc_resolve_oacc_directive (code, ns);
12175 break;
12176
12177 case EXEC_OMP_ATOMIC:
12178 case EXEC_OMP_BARRIER:
12179 case EXEC_OMP_CANCEL:
12180 case EXEC_OMP_CANCELLATION_POINT:
12181 case EXEC_OMP_CRITICAL:
12182 case EXEC_OMP_FLUSH:
12183 case EXEC_OMP_DISTRIBUTE:
12184 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
12185 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
12186 case EXEC_OMP_DISTRIBUTE_SIMD:
12187 case EXEC_OMP_DO:
12188 case EXEC_OMP_DO_SIMD:
12189 case EXEC_OMP_MASTER:
12190 case EXEC_OMP_ORDERED:
12191 case EXEC_OMP_SCAN:
12192 case EXEC_OMP_SECTIONS:
12193 case EXEC_OMP_SIMD:
12194 case EXEC_OMP_SINGLE:
12195 case EXEC_OMP_TARGET:
12196 case EXEC_OMP_TARGET_DATA:
12197 case EXEC_OMP_TARGET_ENTER_DATA:
12198 case EXEC_OMP_TARGET_EXIT_DATA:
12199 case EXEC_OMP_TARGET_PARALLEL:
12200 case EXEC_OMP_TARGET_PARALLEL_DO:
12201 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
12202 case EXEC_OMP_TARGET_SIMD:
12203 case EXEC_OMP_TARGET_TEAMS:
12204 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
12205 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
12206 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12207 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
12208 case EXEC_OMP_TARGET_UPDATE:
12209 case EXEC_OMP_TASK:
12210 case EXEC_OMP_TASKGROUP:
12211 case EXEC_OMP_TASKLOOP:
12212 case EXEC_OMP_TASKLOOP_SIMD:
12213 case EXEC_OMP_TASKWAIT:
12214 case EXEC_OMP_TASKYIELD:
12215 case EXEC_OMP_TEAMS:
12216 case EXEC_OMP_TEAMS_DISTRIBUTE:
12217 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
12218 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12219 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
12220 case EXEC_OMP_WORKSHARE:
12221 gfc_resolve_omp_directive (code, ns);
12222 break;
12223
12224 case EXEC_OMP_PARALLEL:
12225 case EXEC_OMP_PARALLEL_DO:
12226 case EXEC_OMP_PARALLEL_DO_SIMD:
12227 case EXEC_OMP_PARALLEL_SECTIONS:
12228 case EXEC_OMP_PARALLEL_WORKSHARE:
12229 omp_workshare_save = omp_workshare_flag;
12230 omp_workshare_flag = 0;
12231 gfc_resolve_omp_directive (code, ns);
12232 omp_workshare_flag = omp_workshare_save;
12233 break;
12234
12235 default:
12236 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
12237 }
12238 }
12239
12240 cs_base = frame.prev;
12241 }
12242
12243
12244 /* Resolve initial values and make sure they are compatible with
12245 the variable. */
12246
12247 static void
12248 resolve_values (gfc_symbol *sym)
12249 {
12250 bool t;
12251
12252 if (sym->value == NULL)
12253 return;
12254
12255 if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
12256 gfc_warning (OPT_Wdeprecated_declarations,
12257 "Using parameter %qs declared at %L is deprecated",
12258 sym->name, &sym->declared_at);
12259
12260 if (sym->value->expr_type == EXPR_STRUCTURE)
12261 t= resolve_structure_cons (sym->value, 1);
12262 else
12263 t = gfc_resolve_expr (sym->value);
12264
12265 if (!t)
12266 return;
12267
12268 gfc_check_assign_symbol (sym, NULL, sym->value);
12269 }
12270
12271
12272 /* Verify any BIND(C) derived types in the namespace so we can report errors
12273 for them once, rather than for each variable declared of that type. */
12274
12275 static void
12276 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
12277 {
12278 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
12279 && derived_sym->attr.is_bind_c == 1)
12280 verify_bind_c_derived_type (derived_sym);
12281
12282 return;
12283 }
12284
12285
12286 /* Check the interfaces of DTIO procedures associated with derived
12287 type 'sym'. These procedures can either have typebound bindings or
12288 can appear in DTIO generic interfaces. */
12289
12290 static void
12291 gfc_verify_DTIO_procedures (gfc_symbol *sym)
12292 {
12293 if (!sym || sym->attr.flavor != FL_DERIVED)
12294 return;
12295
12296 gfc_check_dtio_interfaces (sym);
12297
12298 return;
12299 }
12300
12301 /* Verify that any binding labels used in a given namespace do not collide
12302 with the names or binding labels of any global symbols. Multiple INTERFACE
12303 for the same procedure are permitted. */
12304
12305 static void
12306 gfc_verify_binding_labels (gfc_symbol *sym)
12307 {
12308 gfc_gsymbol *gsym;
12309 const char *module;
12310
12311 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
12312 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
12313 return;
12314
12315 gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
12316
12317 if (sym->module)
12318 module = sym->module;
12319 else if (sym->ns && sym->ns->proc_name
12320 && sym->ns->proc_name->attr.flavor == FL_MODULE)
12321 module = sym->ns->proc_name->name;
12322 else if (sym->ns && sym->ns->parent
12323 && sym->ns && sym->ns->parent->proc_name
12324 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
12325 module = sym->ns->parent->proc_name->name;
12326 else
12327 module = NULL;
12328
12329 if (!gsym
12330 || (!gsym->defined
12331 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
12332 {
12333 if (!gsym)
12334 gsym = gfc_get_gsymbol (sym->binding_label, true);
12335 gsym->where = sym->declared_at;
12336 gsym->sym_name = sym->name;
12337 gsym->binding_label = sym->binding_label;
12338 gsym->ns = sym->ns;
12339 gsym->mod_name = module;
12340 if (sym->attr.function)
12341 gsym->type = GSYM_FUNCTION;
12342 else if (sym->attr.subroutine)
12343 gsym->type = GSYM_SUBROUTINE;
12344 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
12345 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
12346 return;
12347 }
12348
12349 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
12350 {
12351 gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
12352 "identifier as entity at %L", sym->name,
12353 sym->binding_label, &sym->declared_at, &gsym->where);
12354 /* Clear the binding label to prevent checking multiple times. */
12355 sym->binding_label = NULL;
12356 return;
12357 }
12358
12359 if (sym->attr.flavor == FL_VARIABLE && module
12360 && (strcmp (module, gsym->mod_name) != 0
12361 || strcmp (sym->name, gsym->sym_name) != 0))
12362 {
12363 /* This can only happen if the variable is defined in a module - if it
12364 isn't the same module, reject it. */
12365 gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
12366 "uses the same global identifier as entity at %L from module %qs",
12367 sym->name, module, sym->binding_label,
12368 &sym->declared_at, &gsym->where, gsym->mod_name);
12369 sym->binding_label = NULL;
12370 return;
12371 }
12372
12373 if ((sym->attr.function || sym->attr.subroutine)
12374 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
12375 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
12376 && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
12377 && (module != gsym->mod_name
12378 || strcmp (gsym->sym_name, sym->name) != 0
12379 || (module && strcmp (module, gsym->mod_name) != 0)))
12380 {
12381 /* Print an error if the procedure is defined multiple times; we have to
12382 exclude references to the same procedure via module association or
12383 multiple checks for the same procedure. */
12384 gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
12385 "global identifier as entity at %L", sym->name,
12386 sym->binding_label, &sym->declared_at, &gsym->where);
12387 sym->binding_label = NULL;
12388 }
12389 }
12390
12391
12392 /* Resolve an index expression. */
12393
12394 static bool
12395 resolve_index_expr (gfc_expr *e)
12396 {
12397 if (!gfc_resolve_expr (e))
12398 return false;
12399
12400 if (!gfc_simplify_expr (e, 0))
12401 return false;
12402
12403 if (!gfc_specification_expr (e))
12404 return false;
12405
12406 return true;
12407 }
12408
12409
12410 /* Resolve a charlen structure. */
12411
12412 static bool
12413 resolve_charlen (gfc_charlen *cl)
12414 {
12415 int k;
12416 bool saved_specification_expr;
12417
12418 if (cl->resolved)
12419 return true;
12420
12421 cl->resolved = 1;
12422 saved_specification_expr = specification_expr;
12423 specification_expr = true;
12424
12425 if (cl->length_from_typespec)
12426 {
12427 if (!gfc_resolve_expr (cl->length))
12428 {
12429 specification_expr = saved_specification_expr;
12430 return false;
12431 }
12432
12433 if (!gfc_simplify_expr (cl->length, 0))
12434 {
12435 specification_expr = saved_specification_expr;
12436 return false;
12437 }
12438
12439 /* cl->length has been resolved. It should have an integer type. */
12440 if (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0)
12441 {
12442 gfc_error ("Scalar INTEGER expression expected at %L",
12443 &cl->length->where);
12444 return false;
12445 }
12446 }
12447 else
12448 {
12449 if (!resolve_index_expr (cl->length))
12450 {
12451 specification_expr = saved_specification_expr;
12452 return false;
12453 }
12454 }
12455
12456 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
12457 a negative value, the length of character entities declared is zero. */
12458 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
12459 && mpz_sgn (cl->length->value.integer) < 0)
12460 gfc_replace_expr (cl->length,
12461 gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
12462
12463 /* Check that the character length is not too large. */
12464 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
12465 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
12466 && cl->length->ts.type == BT_INTEGER
12467 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
12468 {
12469 gfc_error ("String length at %L is too large", &cl->length->where);
12470 specification_expr = saved_specification_expr;
12471 return false;
12472 }
12473
12474 specification_expr = saved_specification_expr;
12475 return true;
12476 }
12477
12478
12479 /* Test for non-constant shape arrays. */
12480
12481 static bool
12482 is_non_constant_shape_array (gfc_symbol *sym)
12483 {
12484 gfc_expr *e;
12485 int i;
12486 bool not_constant;
12487
12488 not_constant = false;
12489 if (sym->as != NULL)
12490 {
12491 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
12492 has not been simplified; parameter array references. Do the
12493 simplification now. */
12494 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
12495 {
12496 if (i == GFC_MAX_DIMENSIONS)
12497 break;
12498
12499 e = sym->as->lower[i];
12500 if (e && (!resolve_index_expr(e)
12501 || !gfc_is_constant_expr (e)))
12502 not_constant = true;
12503 e = sym->as->upper[i];
12504 if (e && (!resolve_index_expr(e)
12505 || !gfc_is_constant_expr (e)))
12506 not_constant = true;
12507 }
12508 }
12509 return not_constant;
12510 }
12511
12512 /* Given a symbol and an initialization expression, add code to initialize
12513 the symbol to the function entry. */
12514 static void
12515 build_init_assign (gfc_symbol *sym, gfc_expr *init)
12516 {
12517 gfc_expr *lval;
12518 gfc_code *init_st;
12519 gfc_namespace *ns = sym->ns;
12520
12521 /* Search for the function namespace if this is a contained
12522 function without an explicit result. */
12523 if (sym->attr.function && sym == sym->result
12524 && sym->name != sym->ns->proc_name->name)
12525 {
12526 ns = ns->contained;
12527 for (;ns; ns = ns->sibling)
12528 if (strcmp (ns->proc_name->name, sym->name) == 0)
12529 break;
12530 }
12531
12532 if (ns == NULL)
12533 {
12534 gfc_free_expr (init);
12535 return;
12536 }
12537
12538 /* Build an l-value expression for the result. */
12539 lval = gfc_lval_expr_from_sym (sym);
12540
12541 /* Add the code at scope entry. */
12542 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
12543 init_st->next = ns->code;
12544 ns->code = init_st;
12545
12546 /* Assign the default initializer to the l-value. */
12547 init_st->loc = sym->declared_at;
12548 init_st->expr1 = lval;
12549 init_st->expr2 = init;
12550 }
12551
12552
12553 /* Whether or not we can generate a default initializer for a symbol. */
12554
12555 static bool
12556 can_generate_init (gfc_symbol *sym)
12557 {
12558 symbol_attribute *a;
12559 if (!sym)
12560 return false;
12561 a = &sym->attr;
12562
12563 /* These symbols should never have a default initialization. */
12564 return !(
12565 a->allocatable
12566 || a->external
12567 || a->pointer
12568 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
12569 && (CLASS_DATA (sym)->attr.class_pointer
12570 || CLASS_DATA (sym)->attr.proc_pointer))
12571 || a->in_equivalence
12572 || a->in_common
12573 || a->data
12574 || sym->module
12575 || a->cray_pointee
12576 || a->cray_pointer
12577 || sym->assoc
12578 || (!a->referenced && !a->result)
12579 || (a->dummy && a->intent != INTENT_OUT)
12580 || (a->function && sym != sym->result)
12581 );
12582 }
12583
12584
12585 /* Assign the default initializer to a derived type variable or result. */
12586
12587 static void
12588 apply_default_init (gfc_symbol *sym)
12589 {
12590 gfc_expr *init = NULL;
12591
12592 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
12593 return;
12594
12595 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
12596 init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
12597
12598 if (init == NULL && sym->ts.type != BT_CLASS)
12599 return;
12600
12601 build_init_assign (sym, init);
12602 sym->attr.referenced = 1;
12603 }
12604
12605
12606 /* Build an initializer for a local. Returns null if the symbol should not have
12607 a default initialization. */
12608
12609 static gfc_expr *
12610 build_default_init_expr (gfc_symbol *sym)
12611 {
12612 /* These symbols should never have a default initialization. */
12613 if (sym->attr.allocatable
12614 || sym->attr.external
12615 || sym->attr.dummy
12616 || sym->attr.pointer
12617 || sym->attr.in_equivalence
12618 || sym->attr.in_common
12619 || sym->attr.data
12620 || sym->module
12621 || sym->attr.cray_pointee
12622 || sym->attr.cray_pointer
12623 || sym->assoc)
12624 return NULL;
12625
12626 /* Get the appropriate init expression. */
12627 return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
12628 }
12629
12630 /* Add an initialization expression to a local variable. */
12631 static void
12632 apply_default_init_local (gfc_symbol *sym)
12633 {
12634 gfc_expr *init = NULL;
12635
12636 /* The symbol should be a variable or a function return value. */
12637 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
12638 || (sym->attr.function && sym->result != sym))
12639 return;
12640
12641 /* Try to build the initializer expression. If we can't initialize
12642 this symbol, then init will be NULL. */
12643 init = build_default_init_expr (sym);
12644 if (init == NULL)
12645 return;
12646
12647 /* For saved variables, we don't want to add an initializer at function
12648 entry, so we just add a static initializer. Note that automatic variables
12649 are stack allocated even with -fno-automatic; we have also to exclude
12650 result variable, which are also nonstatic. */
12651 if (!sym->attr.automatic
12652 && (sym->attr.save || sym->ns->save_all
12653 || (flag_max_stack_var_size == 0 && !sym->attr.result
12654 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
12655 && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
12656 {
12657 /* Don't clobber an existing initializer! */
12658 gcc_assert (sym->value == NULL);
12659 sym->value = init;
12660 return;
12661 }
12662
12663 build_init_assign (sym, init);
12664 }
12665
12666
12667 /* Resolution of common features of flavors variable and procedure. */
12668
12669 static bool
12670 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
12671 {
12672 gfc_array_spec *as;
12673
12674 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
12675 && sym->ts.u.derived && CLASS_DATA (sym))
12676 as = CLASS_DATA (sym)->as;
12677 else
12678 as = sym->as;
12679
12680 /* Constraints on deferred shape variable. */
12681 if (as == NULL || as->type != AS_DEFERRED)
12682 {
12683 bool pointer, allocatable, dimension;
12684
12685 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
12686 && sym->ts.u.derived && CLASS_DATA (sym))
12687 {
12688 pointer = CLASS_DATA (sym)->attr.class_pointer;
12689 allocatable = CLASS_DATA (sym)->attr.allocatable;
12690 dimension = CLASS_DATA (sym)->attr.dimension;
12691 }
12692 else
12693 {
12694 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
12695 allocatable = sym->attr.allocatable;
12696 dimension = sym->attr.dimension;
12697 }
12698
12699 if (allocatable)
12700 {
12701 if (dimension && as->type != AS_ASSUMED_RANK)
12702 {
12703 gfc_error ("Allocatable array %qs at %L must have a deferred "
12704 "shape or assumed rank", sym->name, &sym->declared_at);
12705 return false;
12706 }
12707 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
12708 "%qs at %L may not be ALLOCATABLE",
12709 sym->name, &sym->declared_at))
12710 return false;
12711 }
12712
12713 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
12714 {
12715 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
12716 "assumed rank", sym->name, &sym->declared_at);
12717 sym->error = 1;
12718 return false;
12719 }
12720 }
12721 else
12722 {
12723 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
12724 && sym->ts.type != BT_CLASS && !sym->assoc)
12725 {
12726 gfc_error ("Array %qs at %L cannot have a deferred shape",
12727 sym->name, &sym->declared_at);
12728 return false;
12729 }
12730 }
12731
12732 /* Constraints on polymorphic variables. */
12733 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
12734 {
12735 /* F03:C502. */
12736 if (sym->attr.class_ok
12737 && sym->ts.u.derived
12738 && !sym->attr.select_type_temporary
12739 && !UNLIMITED_POLY (sym)
12740 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
12741 {
12742 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
12743 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
12744 &sym->declared_at);
12745 return false;
12746 }
12747
12748 /* F03:C509. */
12749 /* Assume that use associated symbols were checked in the module ns.
12750 Class-variables that are associate-names are also something special
12751 and excepted from the test. */
12752 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
12753 {
12754 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
12755 "or pointer", sym->name, &sym->declared_at);
12756 return false;
12757 }
12758 }
12759
12760 return true;
12761 }
12762
12763
12764 /* Additional checks for symbols with flavor variable and derived
12765 type. To be called from resolve_fl_variable. */
12766
12767 static bool
12768 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
12769 {
12770 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
12771
12772 /* Check to see if a derived type is blocked from being host
12773 associated by the presence of another class I symbol in the same
12774 namespace. 14.6.1.3 of the standard and the discussion on
12775 comp.lang.fortran. */
12776 if (sym->ts.u.derived
12777 && sym->ns != sym->ts.u.derived->ns
12778 && !sym->ts.u.derived->attr.use_assoc
12779 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
12780 {
12781 gfc_symbol *s;
12782 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
12783 if (s && s->attr.generic)
12784 s = gfc_find_dt_in_generic (s);
12785 if (s && !gfc_fl_struct (s->attr.flavor))
12786 {
12787 gfc_error ("The type %qs cannot be host associated at %L "
12788 "because it is blocked by an incompatible object "
12789 "of the same name declared at %L",
12790 sym->ts.u.derived->name, &sym->declared_at,
12791 &s->declared_at);
12792 return false;
12793 }
12794 }
12795
12796 /* 4th constraint in section 11.3: "If an object of a type for which
12797 component-initialization is specified (R429) appears in the
12798 specification-part of a module and does not have the ALLOCATABLE
12799 or POINTER attribute, the object shall have the SAVE attribute."
12800
12801 The check for initializers is performed with
12802 gfc_has_default_initializer because gfc_default_initializer generates
12803 a hidden default for allocatable components. */
12804 if (!(sym->value || no_init_flag) && sym->ns->proc_name
12805 && sym->ns->proc_name->attr.flavor == FL_MODULE
12806 && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
12807 && !sym->attr.pointer && !sym->attr.allocatable
12808 && gfc_has_default_initializer (sym->ts.u.derived)
12809 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
12810 "%qs at %L, needed due to the default "
12811 "initialization", sym->name, &sym->declared_at))
12812 return false;
12813
12814 /* Assign default initializer. */
12815 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
12816 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
12817 sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
12818
12819 return true;
12820 }
12821
12822
12823 /* F2008, C402 (R401): A colon shall not be used as a type-param-value
12824 except in the declaration of an entity or component that has the POINTER
12825 or ALLOCATABLE attribute. */
12826
12827 static bool
12828 deferred_requirements (gfc_symbol *sym)
12829 {
12830 if (sym->ts.deferred
12831 && !(sym->attr.pointer
12832 || sym->attr.allocatable
12833 || sym->attr.associate_var
12834 || sym->attr.omp_udr_artificial_var))
12835 {
12836 /* If a function has a result variable, only check the variable. */
12837 if (sym->result && sym->name != sym->result->name)
12838 return true;
12839
12840 gfc_error ("Entity %qs at %L has a deferred type parameter and "
12841 "requires either the POINTER or ALLOCATABLE attribute",
12842 sym->name, &sym->declared_at);
12843 return false;
12844 }
12845 return true;
12846 }
12847
12848
12849 /* Resolve symbols with flavor variable. */
12850
12851 static bool
12852 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
12853 {
12854 const char *auto_save_msg = "Automatic object %qs at %L cannot have the "
12855 "SAVE attribute";
12856
12857 if (!resolve_fl_var_and_proc (sym, mp_flag))
12858 return false;
12859
12860 /* Set this flag to check that variables are parameters of all entries.
12861 This check is effected by the call to gfc_resolve_expr through
12862 is_non_constant_shape_array. */
12863 bool saved_specification_expr = specification_expr;
12864 specification_expr = true;
12865
12866 if (sym->ns->proc_name
12867 && (sym->ns->proc_name->attr.flavor == FL_MODULE
12868 || sym->ns->proc_name->attr.is_main_program)
12869 && !sym->attr.use_assoc
12870 && !sym->attr.allocatable
12871 && !sym->attr.pointer
12872 && is_non_constant_shape_array (sym))
12873 {
12874 /* F08:C541. The shape of an array defined in a main program or module
12875 * needs to be constant. */
12876 gfc_error ("The module or main program array %qs at %L must "
12877 "have constant shape", sym->name, &sym->declared_at);
12878 specification_expr = saved_specification_expr;
12879 return false;
12880 }
12881
12882 /* Constraints on deferred type parameter. */
12883 if (!deferred_requirements (sym))
12884 return false;
12885
12886 if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
12887 {
12888 /* Make sure that character string variables with assumed length are
12889 dummy arguments. */
12890 gfc_expr *e = NULL;
12891
12892 if (sym->ts.u.cl)
12893 e = sym->ts.u.cl->length;
12894 else
12895 return false;
12896
12897 if (e == NULL && !sym->attr.dummy && !sym->attr.result
12898 && !sym->ts.deferred && !sym->attr.select_type_temporary
12899 && !sym->attr.omp_udr_artificial_var)
12900 {
12901 gfc_error ("Entity with assumed character length at %L must be a "
12902 "dummy argument or a PARAMETER", &sym->declared_at);
12903 specification_expr = saved_specification_expr;
12904 return false;
12905 }
12906
12907 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
12908 {
12909 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
12910 specification_expr = saved_specification_expr;
12911 return false;
12912 }
12913
12914 if (!gfc_is_constant_expr (e)
12915 && !(e->expr_type == EXPR_VARIABLE
12916 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
12917 {
12918 if (!sym->attr.use_assoc && sym->ns->proc_name
12919 && (sym->ns->proc_name->attr.flavor == FL_MODULE
12920 || sym->ns->proc_name->attr.is_main_program))
12921 {
12922 gfc_error ("%qs at %L must have constant character length "
12923 "in this context", sym->name, &sym->declared_at);
12924 specification_expr = saved_specification_expr;
12925 return false;
12926 }
12927 if (sym->attr.in_common)
12928 {
12929 gfc_error ("COMMON variable %qs at %L must have constant "
12930 "character length", sym->name, &sym->declared_at);
12931 specification_expr = saved_specification_expr;
12932 return false;
12933 }
12934 }
12935 }
12936
12937 if (sym->value == NULL && sym->attr.referenced)
12938 apply_default_init_local (sym); /* Try to apply a default initialization. */
12939
12940 /* Determine if the symbol may not have an initializer. */
12941 int no_init_flag = 0, automatic_flag = 0;
12942 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
12943 || sym->attr.intrinsic || sym->attr.result)
12944 no_init_flag = 1;
12945 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
12946 && is_non_constant_shape_array (sym))
12947 {
12948 no_init_flag = automatic_flag = 1;
12949
12950 /* Also, they must not have the SAVE attribute.
12951 SAVE_IMPLICIT is checked below. */
12952 if (sym->as && sym->attr.codimension)
12953 {
12954 int corank = sym->as->corank;
12955 sym->as->corank = 0;
12956 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
12957 sym->as->corank = corank;
12958 }
12959 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
12960 {
12961 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
12962 specification_expr = saved_specification_expr;
12963 return false;
12964 }
12965 }
12966
12967 /* Ensure that any initializer is simplified. */
12968 if (sym->value)
12969 gfc_simplify_expr (sym->value, 1);
12970
12971 /* Reject illegal initializers. */
12972 if (!sym->mark && sym->value)
12973 {
12974 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
12975 && CLASS_DATA (sym)->attr.allocatable))
12976 gfc_error ("Allocatable %qs at %L cannot have an initializer",
12977 sym->name, &sym->declared_at);
12978 else if (sym->attr.external)
12979 gfc_error ("External %qs at %L cannot have an initializer",
12980 sym->name, &sym->declared_at);
12981 else if (sym->attr.dummy)
12982 gfc_error ("Dummy %qs at %L cannot have an initializer",
12983 sym->name, &sym->declared_at);
12984 else if (sym->attr.intrinsic)
12985 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
12986 sym->name, &sym->declared_at);
12987 else if (sym->attr.result)
12988 gfc_error ("Function result %qs at %L cannot have an initializer",
12989 sym->name, &sym->declared_at);
12990 else if (automatic_flag)
12991 gfc_error ("Automatic array %qs at %L cannot have an initializer",
12992 sym->name, &sym->declared_at);
12993 else
12994 goto no_init_error;
12995 specification_expr = saved_specification_expr;
12996 return false;
12997 }
12998
12999 no_init_error:
13000 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
13001 {
13002 bool res = resolve_fl_variable_derived (sym, no_init_flag);
13003 specification_expr = saved_specification_expr;
13004 return res;
13005 }
13006
13007 specification_expr = saved_specification_expr;
13008 return true;
13009 }
13010
13011
13012 /* Compare the dummy characteristics of a module procedure interface
13013 declaration with the corresponding declaration in a submodule. */
13014 static gfc_formal_arglist *new_formal;
13015 static char errmsg[200];
13016
13017 static void
13018 compare_fsyms (gfc_symbol *sym)
13019 {
13020 gfc_symbol *fsym;
13021
13022 if (sym == NULL || new_formal == NULL)
13023 return;
13024
13025 fsym = new_formal->sym;
13026
13027 if (sym == fsym)
13028 return;
13029
13030 if (strcmp (sym->name, fsym->name) == 0)
13031 {
13032 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
13033 gfc_error ("%s at %L", errmsg, &fsym->declared_at);
13034 }
13035 }
13036
13037
13038 /* Resolve a procedure. */
13039
13040 static bool
13041 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
13042 {
13043 gfc_formal_arglist *arg;
13044
13045 if (sym->attr.function
13046 && !resolve_fl_var_and_proc (sym, mp_flag))
13047 return false;
13048
13049 /* Constraints on deferred type parameter. */
13050 if (!deferred_requirements (sym))
13051 return false;
13052
13053 if (sym->ts.type == BT_CHARACTER)
13054 {
13055 gfc_charlen *cl = sym->ts.u.cl;
13056
13057 if (cl && cl->length && gfc_is_constant_expr (cl->length)
13058 && !resolve_charlen (cl))
13059 return false;
13060
13061 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
13062 && sym->attr.proc == PROC_ST_FUNCTION)
13063 {
13064 gfc_error ("Character-valued statement function %qs at %L must "
13065 "have constant length", sym->name, &sym->declared_at);
13066 return false;
13067 }
13068 }
13069
13070 /* Ensure that derived type for are not of a private type. Internal
13071 module procedures are excluded by 2.2.3.3 - i.e., they are not
13072 externally accessible and can access all the objects accessible in
13073 the host. */
13074 if (!(sym->ns->parent && sym->ns->parent->proc_name
13075 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
13076 && gfc_check_symbol_access (sym))
13077 {
13078 gfc_interface *iface;
13079
13080 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
13081 {
13082 if (arg->sym
13083 && arg->sym->ts.type == BT_DERIVED
13084 && arg->sym->ts.u.derived
13085 && !arg->sym->ts.u.derived->attr.use_assoc
13086 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
13087 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
13088 "and cannot be a dummy argument"
13089 " of %qs, which is PUBLIC at %L",
13090 arg->sym->name, sym->name,
13091 &sym->declared_at))
13092 {
13093 /* Stop this message from recurring. */
13094 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
13095 return false;
13096 }
13097 }
13098
13099 /* PUBLIC interfaces may expose PRIVATE procedures that take types
13100 PRIVATE to the containing module. */
13101 for (iface = sym->generic; iface; iface = iface->next)
13102 {
13103 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
13104 {
13105 if (arg->sym
13106 && arg->sym->ts.type == BT_DERIVED
13107 && !arg->sym->ts.u.derived->attr.use_assoc
13108 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
13109 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
13110 "PUBLIC interface %qs at %L "
13111 "takes dummy arguments of %qs which "
13112 "is PRIVATE", iface->sym->name,
13113 sym->name, &iface->sym->declared_at,
13114 gfc_typename(&arg->sym->ts)))
13115 {
13116 /* Stop this message from recurring. */
13117 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
13118 return false;
13119 }
13120 }
13121 }
13122 }
13123
13124 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
13125 && !sym->attr.proc_pointer)
13126 {
13127 gfc_error ("Function %qs at %L cannot have an initializer",
13128 sym->name, &sym->declared_at);
13129
13130 /* Make sure no second error is issued for this. */
13131 sym->value->error = 1;
13132 return false;
13133 }
13134
13135 /* An external symbol may not have an initializer because it is taken to be
13136 a procedure. Exception: Procedure Pointers. */
13137 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
13138 {
13139 gfc_error ("External object %qs at %L may not have an initializer",
13140 sym->name, &sym->declared_at);
13141 return false;
13142 }
13143
13144 /* An elemental function is required to return a scalar 12.7.1 */
13145 if (sym->attr.elemental && sym->attr.function
13146 && (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)))
13147 {
13148 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
13149 "result", sym->name, &sym->declared_at);
13150 /* Reset so that the error only occurs once. */
13151 sym->attr.elemental = 0;
13152 return false;
13153 }
13154
13155 if (sym->attr.proc == PROC_ST_FUNCTION
13156 && (sym->attr.allocatable || sym->attr.pointer))
13157 {
13158 gfc_error ("Statement function %qs at %L may not have pointer or "
13159 "allocatable attribute", sym->name, &sym->declared_at);
13160 return false;
13161 }
13162
13163 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
13164 char-len-param shall not be array-valued, pointer-valued, recursive
13165 or pure. ....snip... A character value of * may only be used in the
13166 following ways: (i) Dummy arg of procedure - dummy associates with
13167 actual length; (ii) To declare a named constant; or (iii) External
13168 function - but length must be declared in calling scoping unit. */
13169 if (sym->attr.function
13170 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
13171 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
13172 {
13173 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
13174 || (sym->attr.recursive) || (sym->attr.pure))
13175 {
13176 if (sym->as && sym->as->rank)
13177 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13178 "array-valued", sym->name, &sym->declared_at);
13179
13180 if (sym->attr.pointer)
13181 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13182 "pointer-valued", sym->name, &sym->declared_at);
13183
13184 if (sym->attr.pure)
13185 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13186 "pure", sym->name, &sym->declared_at);
13187
13188 if (sym->attr.recursive)
13189 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13190 "recursive", sym->name, &sym->declared_at);
13191
13192 return false;
13193 }
13194
13195 /* Appendix B.2 of the standard. Contained functions give an
13196 error anyway. Deferred character length is an F2003 feature.
13197 Don't warn on intrinsic conversion functions, which start
13198 with two underscores. */
13199 if (!sym->attr.contained && !sym->ts.deferred
13200 && (sym->name[0] != '_' || sym->name[1] != '_'))
13201 gfc_notify_std (GFC_STD_F95_OBS,
13202 "CHARACTER(*) function %qs at %L",
13203 sym->name, &sym->declared_at);
13204 }
13205
13206 /* F2008, C1218. */
13207 if (sym->attr.elemental)
13208 {
13209 if (sym->attr.proc_pointer)
13210 {
13211 const char* name = (sym->attr.result ? sym->ns->proc_name->name
13212 : sym->name);
13213 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
13214 name, &sym->declared_at);
13215 return false;
13216 }
13217 if (sym->attr.dummy)
13218 {
13219 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
13220 sym->name, &sym->declared_at);
13221 return false;
13222 }
13223 }
13224
13225 /* F2018, C15100: "The result of an elemental function shall be scalar,
13226 and shall not have the POINTER or ALLOCATABLE attribute." The scalar
13227 pointer is tested and caught elsewhere. */
13228 if (sym->attr.elemental && sym->result
13229 && (sym->result->attr.allocatable || sym->result->attr.pointer))
13230 {
13231 gfc_error ("Function result variable %qs at %L of elemental "
13232 "function %qs shall not have an ALLOCATABLE or POINTER "
13233 "attribute", sym->result->name,
13234 &sym->result->declared_at, sym->name);
13235 return false;
13236 }
13237
13238 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
13239 {
13240 gfc_formal_arglist *curr_arg;
13241 int has_non_interop_arg = 0;
13242
13243 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13244 sym->common_block))
13245 {
13246 /* Clear these to prevent looking at them again if there was an
13247 error. */
13248 sym->attr.is_bind_c = 0;
13249 sym->attr.is_c_interop = 0;
13250 sym->ts.is_c_interop = 0;
13251 }
13252 else
13253 {
13254 /* So far, no errors have been found. */
13255 sym->attr.is_c_interop = 1;
13256 sym->ts.is_c_interop = 1;
13257 }
13258
13259 curr_arg = gfc_sym_get_dummy_args (sym);
13260 while (curr_arg != NULL)
13261 {
13262 /* Skip implicitly typed dummy args here. */
13263 if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
13264 if (!gfc_verify_c_interop_param (curr_arg->sym))
13265 /* If something is found to fail, record the fact so we
13266 can mark the symbol for the procedure as not being
13267 BIND(C) to try and prevent multiple errors being
13268 reported. */
13269 has_non_interop_arg = 1;
13270
13271 curr_arg = curr_arg->next;
13272 }
13273
13274 /* See if any of the arguments were not interoperable and if so, clear
13275 the procedure symbol to prevent duplicate error messages. */
13276 if (has_non_interop_arg != 0)
13277 {
13278 sym->attr.is_c_interop = 0;
13279 sym->ts.is_c_interop = 0;
13280 sym->attr.is_bind_c = 0;
13281 }
13282 }
13283
13284 if (!sym->attr.proc_pointer)
13285 {
13286 if (sym->attr.save == SAVE_EXPLICIT)
13287 {
13288 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
13289 "in %qs at %L", sym->name, &sym->declared_at);
13290 return false;
13291 }
13292 if (sym->attr.intent)
13293 {
13294 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
13295 "in %qs at %L", sym->name, &sym->declared_at);
13296 return false;
13297 }
13298 if (sym->attr.subroutine && sym->attr.result)
13299 {
13300 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
13301 "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
13302 return false;
13303 }
13304 if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
13305 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
13306 || sym->attr.contained))
13307 {
13308 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
13309 "in %qs at %L", sym->name, &sym->declared_at);
13310 return false;
13311 }
13312 if (strcmp ("ppr@", sym->name) == 0)
13313 {
13314 gfc_error ("Procedure pointer result %qs at %L "
13315 "is missing the pointer attribute",
13316 sym->ns->proc_name->name, &sym->declared_at);
13317 return false;
13318 }
13319 }
13320
13321 /* Assume that a procedure whose body is not known has references
13322 to external arrays. */
13323 if (sym->attr.if_source != IFSRC_DECL)
13324 sym->attr.array_outer_dependency = 1;
13325
13326 /* Compare the characteristics of a module procedure with the
13327 interface declaration. Ideally this would be done with
13328 gfc_compare_interfaces but, at present, the formal interface
13329 cannot be copied to the ts.interface. */
13330 if (sym->attr.module_procedure
13331 && sym->attr.if_source == IFSRC_DECL)
13332 {
13333 gfc_symbol *iface;
13334 char name[2*GFC_MAX_SYMBOL_LEN + 1];
13335 char *module_name;
13336 char *submodule_name;
13337 strcpy (name, sym->ns->proc_name->name);
13338 module_name = strtok (name, ".");
13339 submodule_name = strtok (NULL, ".");
13340
13341 iface = sym->tlink;
13342 sym->tlink = NULL;
13343
13344 /* Make sure that the result uses the correct charlen for deferred
13345 length results. */
13346 if (iface && sym->result
13347 && iface->ts.type == BT_CHARACTER
13348 && iface->ts.deferred)
13349 sym->result->ts.u.cl = iface->ts.u.cl;
13350
13351 if (iface == NULL)
13352 goto check_formal;
13353
13354 /* Check the procedure characteristics. */
13355 if (sym->attr.elemental != iface->attr.elemental)
13356 {
13357 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
13358 "PROCEDURE at %L and its interface in %s",
13359 &sym->declared_at, module_name);
13360 return false;
13361 }
13362
13363 if (sym->attr.pure != iface->attr.pure)
13364 {
13365 gfc_error ("Mismatch in PURE attribute between MODULE "
13366 "PROCEDURE at %L and its interface in %s",
13367 &sym->declared_at, module_name);
13368 return false;
13369 }
13370
13371 if (sym->attr.recursive != iface->attr.recursive)
13372 {
13373 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
13374 "PROCEDURE at %L and its interface in %s",
13375 &sym->declared_at, module_name);
13376 return false;
13377 }
13378
13379 /* Check the result characteristics. */
13380 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
13381 {
13382 gfc_error ("%s between the MODULE PROCEDURE declaration "
13383 "in MODULE %qs and the declaration at %L in "
13384 "(SUB)MODULE %qs",
13385 errmsg, module_name, &sym->declared_at,
13386 submodule_name ? submodule_name : module_name);
13387 return false;
13388 }
13389
13390 check_formal:
13391 /* Check the characteristics of the formal arguments. */
13392 if (sym->formal && sym->formal_ns)
13393 {
13394 for (arg = sym->formal; arg && arg->sym; arg = arg->next)
13395 {
13396 new_formal = arg;
13397 gfc_traverse_ns (sym->formal_ns, compare_fsyms);
13398 }
13399 }
13400 }
13401 return true;
13402 }
13403
13404
13405 /* Resolve a list of finalizer procedures. That is, after they have hopefully
13406 been defined and we now know their defined arguments, check that they fulfill
13407 the requirements of the standard for procedures used as finalizers. */
13408
13409 static bool
13410 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
13411 {
13412 gfc_finalizer* list;
13413 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
13414 bool result = true;
13415 bool seen_scalar = false;
13416 gfc_symbol *vtab;
13417 gfc_component *c;
13418 gfc_symbol *parent = gfc_get_derived_super_type (derived);
13419
13420 if (parent)
13421 gfc_resolve_finalizers (parent, finalizable);
13422
13423 /* Ensure that derived-type components have a their finalizers resolved. */
13424 bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
13425 for (c = derived->components; c; c = c->next)
13426 if (c->ts.type == BT_DERIVED
13427 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
13428 {
13429 bool has_final2 = false;
13430 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
13431 return false; /* Error. */
13432 has_final = has_final || has_final2;
13433 }
13434 /* Return early if not finalizable. */
13435 if (!has_final)
13436 {
13437 if (finalizable)
13438 *finalizable = false;
13439 return true;
13440 }
13441
13442 /* Walk over the list of finalizer-procedures, check them, and if any one
13443 does not fit in with the standard's definition, print an error and remove
13444 it from the list. */
13445 prev_link = &derived->f2k_derived->finalizers;
13446 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
13447 {
13448 gfc_formal_arglist *dummy_args;
13449 gfc_symbol* arg;
13450 gfc_finalizer* i;
13451 int my_rank;
13452
13453 /* Skip this finalizer if we already resolved it. */
13454 if (list->proc_tree)
13455 {
13456 if (list->proc_tree->n.sym->formal->sym->as == NULL
13457 || list->proc_tree->n.sym->formal->sym->as->rank == 0)
13458 seen_scalar = true;
13459 prev_link = &(list->next);
13460 continue;
13461 }
13462
13463 /* Check this exists and is a SUBROUTINE. */
13464 if (!list->proc_sym->attr.subroutine)
13465 {
13466 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
13467 list->proc_sym->name, &list->where);
13468 goto error;
13469 }
13470
13471 /* We should have exactly one argument. */
13472 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
13473 if (!dummy_args || dummy_args->next)
13474 {
13475 gfc_error ("FINAL procedure at %L must have exactly one argument",
13476 &list->where);
13477 goto error;
13478 }
13479 arg = dummy_args->sym;
13480
13481 /* This argument must be of our type. */
13482 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
13483 {
13484 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
13485 &arg->declared_at, derived->name);
13486 goto error;
13487 }
13488
13489 /* It must neither be a pointer nor allocatable nor optional. */
13490 if (arg->attr.pointer)
13491 {
13492 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
13493 &arg->declared_at);
13494 goto error;
13495 }
13496 if (arg->attr.allocatable)
13497 {
13498 gfc_error ("Argument of FINAL procedure at %L must not be"
13499 " ALLOCATABLE", &arg->declared_at);
13500 goto error;
13501 }
13502 if (arg->attr.optional)
13503 {
13504 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
13505 &arg->declared_at);
13506 goto error;
13507 }
13508
13509 /* It must not be INTENT(OUT). */
13510 if (arg->attr.intent == INTENT_OUT)
13511 {
13512 gfc_error ("Argument of FINAL procedure at %L must not be"
13513 " INTENT(OUT)", &arg->declared_at);
13514 goto error;
13515 }
13516
13517 /* Warn if the procedure is non-scalar and not assumed shape. */
13518 if (warn_surprising && arg->as && arg->as->rank != 0
13519 && arg->as->type != AS_ASSUMED_SHAPE)
13520 gfc_warning (OPT_Wsurprising,
13521 "Non-scalar FINAL procedure at %L should have assumed"
13522 " shape argument", &arg->declared_at);
13523
13524 /* Check that it does not match in kind and rank with a FINAL procedure
13525 defined earlier. To really loop over the *earlier* declarations,
13526 we need to walk the tail of the list as new ones were pushed at the
13527 front. */
13528 /* TODO: Handle kind parameters once they are implemented. */
13529 my_rank = (arg->as ? arg->as->rank : 0);
13530 for (i = list->next; i; i = i->next)
13531 {
13532 gfc_formal_arglist *dummy_args;
13533
13534 /* Argument list might be empty; that is an error signalled earlier,
13535 but we nevertheless continued resolving. */
13536 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
13537 if (dummy_args)
13538 {
13539 gfc_symbol* i_arg = dummy_args->sym;
13540 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
13541 if (i_rank == my_rank)
13542 {
13543 gfc_error ("FINAL procedure %qs declared at %L has the same"
13544 " rank (%d) as %qs",
13545 list->proc_sym->name, &list->where, my_rank,
13546 i->proc_sym->name);
13547 goto error;
13548 }
13549 }
13550 }
13551
13552 /* Is this the/a scalar finalizer procedure? */
13553 if (my_rank == 0)
13554 seen_scalar = true;
13555
13556 /* Find the symtree for this procedure. */
13557 gcc_assert (!list->proc_tree);
13558 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
13559
13560 prev_link = &list->next;
13561 continue;
13562
13563 /* Remove wrong nodes immediately from the list so we don't risk any
13564 troubles in the future when they might fail later expectations. */
13565 error:
13566 i = list;
13567 *prev_link = list->next;
13568 gfc_free_finalizer (i);
13569 result = false;
13570 }
13571
13572 if (result == false)
13573 return false;
13574
13575 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
13576 were nodes in the list, must have been for arrays. It is surely a good
13577 idea to have a scalar version there if there's something to finalize. */
13578 if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
13579 gfc_warning (OPT_Wsurprising,
13580 "Only array FINAL procedures declared for derived type %qs"
13581 " defined at %L, suggest also scalar one",
13582 derived->name, &derived->declared_at);
13583
13584 vtab = gfc_find_derived_vtab (derived);
13585 c = vtab->ts.u.derived->components->next->next->next->next->next;
13586 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
13587
13588 if (finalizable)
13589 *finalizable = true;
13590
13591 return true;
13592 }
13593
13594
13595 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
13596
13597 static bool
13598 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
13599 const char* generic_name, locus where)
13600 {
13601 gfc_symbol *sym1, *sym2;
13602 const char *pass1, *pass2;
13603 gfc_formal_arglist *dummy_args;
13604
13605 gcc_assert (t1->specific && t2->specific);
13606 gcc_assert (!t1->specific->is_generic);
13607 gcc_assert (!t2->specific->is_generic);
13608 gcc_assert (t1->is_operator == t2->is_operator);
13609
13610 sym1 = t1->specific->u.specific->n.sym;
13611 sym2 = t2->specific->u.specific->n.sym;
13612
13613 if (sym1 == sym2)
13614 return true;
13615
13616 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
13617 if (sym1->attr.subroutine != sym2->attr.subroutine
13618 || sym1->attr.function != sym2->attr.function)
13619 {
13620 gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
13621 " GENERIC %qs at %L",
13622 sym1->name, sym2->name, generic_name, &where);
13623 return false;
13624 }
13625
13626 /* Determine PASS arguments. */
13627 if (t1->specific->nopass)
13628 pass1 = NULL;
13629 else if (t1->specific->pass_arg)
13630 pass1 = t1->specific->pass_arg;
13631 else
13632 {
13633 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
13634 if (dummy_args)
13635 pass1 = dummy_args->sym->name;
13636 else
13637 pass1 = NULL;
13638 }
13639 if (t2->specific->nopass)
13640 pass2 = NULL;
13641 else if (t2->specific->pass_arg)
13642 pass2 = t2->specific->pass_arg;
13643 else
13644 {
13645 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
13646 if (dummy_args)
13647 pass2 = dummy_args->sym->name;
13648 else
13649 pass2 = NULL;
13650 }
13651
13652 /* Compare the interfaces. */
13653 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
13654 NULL, 0, pass1, pass2))
13655 {
13656 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
13657 sym1->name, sym2->name, generic_name, &where);
13658 return false;
13659 }
13660
13661 return true;
13662 }
13663
13664
13665 /* Worker function for resolving a generic procedure binding; this is used to
13666 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
13667
13668 The difference between those cases is finding possible inherited bindings
13669 that are overridden, as one has to look for them in tb_sym_root,
13670 tb_uop_root or tb_op, respectively. Thus the caller must already find
13671 the super-type and set p->overridden correctly. */
13672
13673 static bool
13674 resolve_tb_generic_targets (gfc_symbol* super_type,
13675 gfc_typebound_proc* p, const char* name)
13676 {
13677 gfc_tbp_generic* target;
13678 gfc_symtree* first_target;
13679 gfc_symtree* inherited;
13680
13681 gcc_assert (p && p->is_generic);
13682
13683 /* Try to find the specific bindings for the symtrees in our target-list. */
13684 gcc_assert (p->u.generic);
13685 for (target = p->u.generic; target; target = target->next)
13686 if (!target->specific)
13687 {
13688 gfc_typebound_proc* overridden_tbp;
13689 gfc_tbp_generic* g;
13690 const char* target_name;
13691
13692 target_name = target->specific_st->name;
13693
13694 /* Defined for this type directly. */
13695 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
13696 {
13697 target->specific = target->specific_st->n.tb;
13698 goto specific_found;
13699 }
13700
13701 /* Look for an inherited specific binding. */
13702 if (super_type)
13703 {
13704 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
13705 true, NULL);
13706
13707 if (inherited)
13708 {
13709 gcc_assert (inherited->n.tb);
13710 target->specific = inherited->n.tb;
13711 goto specific_found;
13712 }
13713 }
13714
13715 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
13716 " at %L", target_name, name, &p->where);
13717 return false;
13718
13719 /* Once we've found the specific binding, check it is not ambiguous with
13720 other specifics already found or inherited for the same GENERIC. */
13721 specific_found:
13722 gcc_assert (target->specific);
13723
13724 /* This must really be a specific binding! */
13725 if (target->specific->is_generic)
13726 {
13727 gfc_error ("GENERIC %qs at %L must target a specific binding,"
13728 " %qs is GENERIC, too", name, &p->where, target_name);
13729 return false;
13730 }
13731
13732 /* Check those already resolved on this type directly. */
13733 for (g = p->u.generic; g; g = g->next)
13734 if (g != target && g->specific
13735 && !check_generic_tbp_ambiguity (target, g, name, p->where))
13736 return false;
13737
13738 /* Check for ambiguity with inherited specific targets. */
13739 for (overridden_tbp = p->overridden; overridden_tbp;
13740 overridden_tbp = overridden_tbp->overridden)
13741 if (overridden_tbp->is_generic)
13742 {
13743 for (g = overridden_tbp->u.generic; g; g = g->next)
13744 {
13745 gcc_assert (g->specific);
13746 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
13747 return false;
13748 }
13749 }
13750 }
13751
13752 /* If we attempt to "overwrite" a specific binding, this is an error. */
13753 if (p->overridden && !p->overridden->is_generic)
13754 {
13755 gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
13756 " the same name", name, &p->where);
13757 return false;
13758 }
13759
13760 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
13761 all must have the same attributes here. */
13762 first_target = p->u.generic->specific->u.specific;
13763 gcc_assert (first_target);
13764 p->subroutine = first_target->n.sym->attr.subroutine;
13765 p->function = first_target->n.sym->attr.function;
13766
13767 return true;
13768 }
13769
13770
13771 /* Resolve a GENERIC procedure binding for a derived type. */
13772
13773 static bool
13774 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
13775 {
13776 gfc_symbol* super_type;
13777
13778 /* Find the overridden binding if any. */
13779 st->n.tb->overridden = NULL;
13780 super_type = gfc_get_derived_super_type (derived);
13781 if (super_type)
13782 {
13783 gfc_symtree* overridden;
13784 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
13785 true, NULL);
13786
13787 if (overridden && overridden->n.tb)
13788 st->n.tb->overridden = overridden->n.tb;
13789 }
13790
13791 /* Resolve using worker function. */
13792 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
13793 }
13794
13795
13796 /* Retrieve the target-procedure of an operator binding and do some checks in
13797 common for intrinsic and user-defined type-bound operators. */
13798
13799 static gfc_symbol*
13800 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
13801 {
13802 gfc_symbol* target_proc;
13803
13804 gcc_assert (target->specific && !target->specific->is_generic);
13805 target_proc = target->specific->u.specific->n.sym;
13806 gcc_assert (target_proc);
13807
13808 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
13809 if (target->specific->nopass)
13810 {
13811 gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
13812 return NULL;
13813 }
13814
13815 return target_proc;
13816 }
13817
13818
13819 /* Resolve a type-bound intrinsic operator. */
13820
13821 static bool
13822 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
13823 gfc_typebound_proc* p)
13824 {
13825 gfc_symbol* super_type;
13826 gfc_tbp_generic* target;
13827
13828 /* If there's already an error here, do nothing (but don't fail again). */
13829 if (p->error)
13830 return true;
13831
13832 /* Operators should always be GENERIC bindings. */
13833 gcc_assert (p->is_generic);
13834
13835 /* Look for an overridden binding. */
13836 super_type = gfc_get_derived_super_type (derived);
13837 if (super_type && super_type->f2k_derived)
13838 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
13839 op, true, NULL);
13840 else
13841 p->overridden = NULL;
13842
13843 /* Resolve general GENERIC properties using worker function. */
13844 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
13845 goto error;
13846
13847 /* Check the targets to be procedures of correct interface. */
13848 for (target = p->u.generic; target; target = target->next)
13849 {
13850 gfc_symbol* target_proc;
13851
13852 target_proc = get_checked_tb_operator_target (target, p->where);
13853 if (!target_proc)
13854 goto error;
13855
13856 if (!gfc_check_operator_interface (target_proc, op, p->where))
13857 goto error;
13858
13859 /* Add target to non-typebound operator list. */
13860 if (!target->specific->deferred && !derived->attr.use_assoc
13861 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
13862 {
13863 gfc_interface *head, *intr;
13864
13865 /* Preempt 'gfc_check_new_interface' for submodules, where the
13866 mechanism for handling module procedures winds up resolving
13867 operator interfaces twice and would otherwise cause an error. */
13868 for (intr = derived->ns->op[op]; intr; intr = intr->next)
13869 if (intr->sym == target_proc
13870 && target_proc->attr.used_in_submodule)
13871 return true;
13872
13873 if (!gfc_check_new_interface (derived->ns->op[op],
13874 target_proc, p->where))
13875 return false;
13876 head = derived->ns->op[op];
13877 intr = gfc_get_interface ();
13878 intr->sym = target_proc;
13879 intr->where = p->where;
13880 intr->next = head;
13881 derived->ns->op[op] = intr;
13882 }
13883 }
13884
13885 return true;
13886
13887 error:
13888 p->error = 1;
13889 return false;
13890 }
13891
13892
13893 /* Resolve a type-bound user operator (tree-walker callback). */
13894
13895 static gfc_symbol* resolve_bindings_derived;
13896 static bool resolve_bindings_result;
13897
13898 static bool check_uop_procedure (gfc_symbol* sym, locus where);
13899
13900 static void
13901 resolve_typebound_user_op (gfc_symtree* stree)
13902 {
13903 gfc_symbol* super_type;
13904 gfc_tbp_generic* target;
13905
13906 gcc_assert (stree && stree->n.tb);
13907
13908 if (stree->n.tb->error)
13909 return;
13910
13911 /* Operators should always be GENERIC bindings. */
13912 gcc_assert (stree->n.tb->is_generic);
13913
13914 /* Find overridden procedure, if any. */
13915 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
13916 if (super_type && super_type->f2k_derived)
13917 {
13918 gfc_symtree* overridden;
13919 overridden = gfc_find_typebound_user_op (super_type, NULL,
13920 stree->name, true, NULL);
13921
13922 if (overridden && overridden->n.tb)
13923 stree->n.tb->overridden = overridden->n.tb;
13924 }
13925 else
13926 stree->n.tb->overridden = NULL;
13927
13928 /* Resolve basically using worker function. */
13929 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
13930 goto error;
13931
13932 /* Check the targets to be functions of correct interface. */
13933 for (target = stree->n.tb->u.generic; target; target = target->next)
13934 {
13935 gfc_symbol* target_proc;
13936
13937 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
13938 if (!target_proc)
13939 goto error;
13940
13941 if (!check_uop_procedure (target_proc, stree->n.tb->where))
13942 goto error;
13943 }
13944
13945 return;
13946
13947 error:
13948 resolve_bindings_result = false;
13949 stree->n.tb->error = 1;
13950 }
13951
13952
13953 /* Resolve the type-bound procedures for a derived type. */
13954
13955 static void
13956 resolve_typebound_procedure (gfc_symtree* stree)
13957 {
13958 gfc_symbol* proc;
13959 locus where;
13960 gfc_symbol* me_arg;
13961 gfc_symbol* super_type;
13962 gfc_component* comp;
13963
13964 gcc_assert (stree);
13965
13966 /* Undefined specific symbol from GENERIC target definition. */
13967 if (!stree->n.tb)
13968 return;
13969
13970 if (stree->n.tb->error)
13971 return;
13972
13973 /* If this is a GENERIC binding, use that routine. */
13974 if (stree->n.tb->is_generic)
13975 {
13976 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
13977 goto error;
13978 return;
13979 }
13980
13981 /* Get the target-procedure to check it. */
13982 gcc_assert (!stree->n.tb->is_generic);
13983 gcc_assert (stree->n.tb->u.specific);
13984 proc = stree->n.tb->u.specific->n.sym;
13985 where = stree->n.tb->where;
13986
13987 /* Default access should already be resolved from the parser. */
13988 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
13989
13990 if (stree->n.tb->deferred)
13991 {
13992 if (!check_proc_interface (proc, &where))
13993 goto error;
13994 }
13995 else
13996 {
13997 /* If proc has not been resolved at this point, proc->name may
13998 actually be a USE associated entity. See PR fortran/89647. */
13999 if (!proc->resolve_symbol_called
14000 && proc->attr.function == 0 && proc->attr.subroutine == 0)
14001 {
14002 gfc_symbol *tmp;
14003 gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
14004 if (tmp && tmp->attr.use_assoc)
14005 {
14006 proc->module = tmp->module;
14007 proc->attr.proc = tmp->attr.proc;
14008 proc->attr.function = tmp->attr.function;
14009 proc->attr.subroutine = tmp->attr.subroutine;
14010 proc->attr.use_assoc = tmp->attr.use_assoc;
14011 proc->ts = tmp->ts;
14012 proc->result = tmp->result;
14013 }
14014 }
14015
14016 /* Check for F08:C465. */
14017 if ((!proc->attr.subroutine && !proc->attr.function)
14018 || (proc->attr.proc != PROC_MODULE
14019 && proc->attr.if_source != IFSRC_IFBODY)
14020 || proc->attr.abstract)
14021 {
14022 gfc_error ("%qs must be a module procedure or an external "
14023 "procedure with an explicit interface at %L",
14024 proc->name, &where);
14025 goto error;
14026 }
14027 }
14028
14029 stree->n.tb->subroutine = proc->attr.subroutine;
14030 stree->n.tb->function = proc->attr.function;
14031
14032 /* Find the super-type of the current derived type. We could do this once and
14033 store in a global if speed is needed, but as long as not I believe this is
14034 more readable and clearer. */
14035 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
14036
14037 /* If PASS, resolve and check arguments if not already resolved / loaded
14038 from a .mod file. */
14039 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
14040 {
14041 gfc_formal_arglist *dummy_args;
14042
14043 dummy_args = gfc_sym_get_dummy_args (proc);
14044 if (stree->n.tb->pass_arg)
14045 {
14046 gfc_formal_arglist *i;
14047
14048 /* If an explicit passing argument name is given, walk the arg-list
14049 and look for it. */
14050
14051 me_arg = NULL;
14052 stree->n.tb->pass_arg_num = 1;
14053 for (i = dummy_args; i; i = i->next)
14054 {
14055 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
14056 {
14057 me_arg = i->sym;
14058 break;
14059 }
14060 ++stree->n.tb->pass_arg_num;
14061 }
14062
14063 if (!me_arg)
14064 {
14065 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
14066 " argument %qs",
14067 proc->name, stree->n.tb->pass_arg, &where,
14068 stree->n.tb->pass_arg);
14069 goto error;
14070 }
14071 }
14072 else
14073 {
14074 /* Otherwise, take the first one; there should in fact be at least
14075 one. */
14076 stree->n.tb->pass_arg_num = 1;
14077 if (!dummy_args)
14078 {
14079 gfc_error ("Procedure %qs with PASS at %L must have at"
14080 " least one argument", proc->name, &where);
14081 goto error;
14082 }
14083 me_arg = dummy_args->sym;
14084 }
14085
14086 /* Now check that the argument-type matches and the passed-object
14087 dummy argument is generally fine. */
14088
14089 gcc_assert (me_arg);
14090
14091 if (me_arg->ts.type != BT_CLASS)
14092 {
14093 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14094 " at %L", proc->name, &where);
14095 goto error;
14096 }
14097
14098 if (CLASS_DATA (me_arg)->ts.u.derived
14099 != resolve_bindings_derived)
14100 {
14101 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14102 " the derived-type %qs", me_arg->name, proc->name,
14103 me_arg->name, &where, resolve_bindings_derived->name);
14104 goto error;
14105 }
14106
14107 gcc_assert (me_arg->ts.type == BT_CLASS);
14108 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
14109 {
14110 gfc_error ("Passed-object dummy argument of %qs at %L must be"
14111 " scalar", proc->name, &where);
14112 goto error;
14113 }
14114 if (CLASS_DATA (me_arg)->attr.allocatable)
14115 {
14116 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14117 " be ALLOCATABLE", proc->name, &where);
14118 goto error;
14119 }
14120 if (CLASS_DATA (me_arg)->attr.class_pointer)
14121 {
14122 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14123 " be POINTER", proc->name, &where);
14124 goto error;
14125 }
14126 }
14127
14128 /* If we are extending some type, check that we don't override a procedure
14129 flagged NON_OVERRIDABLE. */
14130 stree->n.tb->overridden = NULL;
14131 if (super_type)
14132 {
14133 gfc_symtree* overridden;
14134 overridden = gfc_find_typebound_proc (super_type, NULL,
14135 stree->name, true, NULL);
14136
14137 if (overridden)
14138 {
14139 if (overridden->n.tb)
14140 stree->n.tb->overridden = overridden->n.tb;
14141
14142 if (!gfc_check_typebound_override (stree, overridden))
14143 goto error;
14144 }
14145 }
14146
14147 /* See if there's a name collision with a component directly in this type. */
14148 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
14149 if (!strcmp (comp->name, stree->name))
14150 {
14151 gfc_error ("Procedure %qs at %L has the same name as a component of"
14152 " %qs",
14153 stree->name, &where, resolve_bindings_derived->name);
14154 goto error;
14155 }
14156
14157 /* Try to find a name collision with an inherited component. */
14158 if (super_type && gfc_find_component (super_type, stree->name, true, true,
14159 NULL))
14160 {
14161 gfc_error ("Procedure %qs at %L has the same name as an inherited"
14162 " component of %qs",
14163 stree->name, &where, resolve_bindings_derived->name);
14164 goto error;
14165 }
14166
14167 stree->n.tb->error = 0;
14168 return;
14169
14170 error:
14171 resolve_bindings_result = false;
14172 stree->n.tb->error = 1;
14173 }
14174
14175
14176 static bool
14177 resolve_typebound_procedures (gfc_symbol* derived)
14178 {
14179 int op;
14180 gfc_symbol* super_type;
14181
14182 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
14183 return true;
14184
14185 super_type = gfc_get_derived_super_type (derived);
14186 if (super_type)
14187 resolve_symbol (super_type);
14188
14189 resolve_bindings_derived = derived;
14190 resolve_bindings_result = true;
14191
14192 if (derived->f2k_derived->tb_sym_root)
14193 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
14194 &resolve_typebound_procedure);
14195
14196 if (derived->f2k_derived->tb_uop_root)
14197 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
14198 &resolve_typebound_user_op);
14199
14200 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
14201 {
14202 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
14203 if (p && !resolve_typebound_intrinsic_op (derived,
14204 (gfc_intrinsic_op)op, p))
14205 resolve_bindings_result = false;
14206 }
14207
14208 return resolve_bindings_result;
14209 }
14210
14211
14212 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
14213 to give all identical derived types the same backend_decl. */
14214 static void
14215 add_dt_to_dt_list (gfc_symbol *derived)
14216 {
14217 if (!derived->dt_next)
14218 {
14219 if (gfc_derived_types)
14220 {
14221 derived->dt_next = gfc_derived_types->dt_next;
14222 gfc_derived_types->dt_next = derived;
14223 }
14224 else
14225 {
14226 derived->dt_next = derived;
14227 }
14228 gfc_derived_types = derived;
14229 }
14230 }
14231
14232
14233 /* Ensure that a derived-type is really not abstract, meaning that every
14234 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
14235
14236 static bool
14237 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
14238 {
14239 if (!st)
14240 return true;
14241
14242 if (!ensure_not_abstract_walker (sub, st->left))
14243 return false;
14244 if (!ensure_not_abstract_walker (sub, st->right))
14245 return false;
14246
14247 if (st->n.tb && st->n.tb->deferred)
14248 {
14249 gfc_symtree* overriding;
14250 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
14251 if (!overriding)
14252 return false;
14253 gcc_assert (overriding->n.tb);
14254 if (overriding->n.tb->deferred)
14255 {
14256 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
14257 " %qs is DEFERRED and not overridden",
14258 sub->name, &sub->declared_at, st->name);
14259 return false;
14260 }
14261 }
14262
14263 return true;
14264 }
14265
14266 static bool
14267 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
14268 {
14269 /* The algorithm used here is to recursively travel up the ancestry of sub
14270 and for each ancestor-type, check all bindings. If any of them is
14271 DEFERRED, look it up starting from sub and see if the found (overriding)
14272 binding is not DEFERRED.
14273 This is not the most efficient way to do this, but it should be ok and is
14274 clearer than something sophisticated. */
14275
14276 gcc_assert (ancestor && !sub->attr.abstract);
14277
14278 if (!ancestor->attr.abstract)
14279 return true;
14280
14281 /* Walk bindings of this ancestor. */
14282 if (ancestor->f2k_derived)
14283 {
14284 bool t;
14285 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
14286 if (!t)
14287 return false;
14288 }
14289
14290 /* Find next ancestor type and recurse on it. */
14291 ancestor = gfc_get_derived_super_type (ancestor);
14292 if (ancestor)
14293 return ensure_not_abstract (sub, ancestor);
14294
14295 return true;
14296 }
14297
14298
14299 /* This check for typebound defined assignments is done recursively
14300 since the order in which derived types are resolved is not always in
14301 order of the declarations. */
14302
14303 static void
14304 check_defined_assignments (gfc_symbol *derived)
14305 {
14306 gfc_component *c;
14307
14308 for (c = derived->components; c; c = c->next)
14309 {
14310 if (!gfc_bt_struct (c->ts.type)
14311 || c->attr.pointer
14312 || c->attr.allocatable
14313 || c->attr.proc_pointer_comp
14314 || c->attr.class_pointer
14315 || c->attr.proc_pointer)
14316 continue;
14317
14318 if (c->ts.u.derived->attr.defined_assign_comp
14319 || (c->ts.u.derived->f2k_derived
14320 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
14321 {
14322 derived->attr.defined_assign_comp = 1;
14323 return;
14324 }
14325
14326 check_defined_assignments (c->ts.u.derived);
14327 if (c->ts.u.derived->attr.defined_assign_comp)
14328 {
14329 derived->attr.defined_assign_comp = 1;
14330 return;
14331 }
14332 }
14333 }
14334
14335
14336 /* Resolve a single component of a derived type or structure. */
14337
14338 static bool
14339 resolve_component (gfc_component *c, gfc_symbol *sym)
14340 {
14341 gfc_symbol *super_type;
14342 symbol_attribute *attr;
14343
14344 if (c->attr.artificial)
14345 return true;
14346
14347 /* Do not allow vtype components to be resolved in nameless namespaces
14348 such as block data because the procedure pointers will cause ICEs
14349 and vtables are not needed in these contexts. */
14350 if (sym->attr.vtype && sym->attr.use_assoc
14351 && sym->ns->proc_name == NULL)
14352 return true;
14353
14354 /* F2008, C442. */
14355 if ((!sym->attr.is_class || c != sym->components)
14356 && c->attr.codimension
14357 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
14358 {
14359 gfc_error ("Coarray component %qs at %L must be allocatable with "
14360 "deferred shape", c->name, &c->loc);
14361 return false;
14362 }
14363
14364 /* F2008, C443. */
14365 if (c->attr.codimension && c->ts.type == BT_DERIVED
14366 && c->ts.u.derived->ts.is_iso_c)
14367 {
14368 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
14369 "shall not be a coarray", c->name, &c->loc);
14370 return false;
14371 }
14372
14373 /* F2008, C444. */
14374 if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
14375 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
14376 || c->attr.allocatable))
14377 {
14378 gfc_error ("Component %qs at %L with coarray component "
14379 "shall be a nonpointer, nonallocatable scalar",
14380 c->name, &c->loc);
14381 return false;
14382 }
14383
14384 /* F2008, C448. */
14385 if (c->ts.type == BT_CLASS)
14386 {
14387 if (CLASS_DATA (c))
14388 {
14389 attr = &(CLASS_DATA (c)->attr);
14390
14391 /* Fix up contiguous attribute. */
14392 if (c->attr.contiguous)
14393 attr->contiguous = 1;
14394 }
14395 else
14396 attr = NULL;
14397 }
14398 else
14399 attr = &c->attr;
14400
14401 if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
14402 {
14403 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
14404 "is not an array pointer", c->name, &c->loc);
14405 return false;
14406 }
14407
14408 /* F2003, 15.2.1 - length has to be one. */
14409 if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
14410 && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
14411 || !gfc_is_constant_expr (c->ts.u.cl->length)
14412 || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
14413 {
14414 gfc_error ("Component %qs of BIND(C) type at %L must have length one",
14415 c->name, &c->loc);
14416 return false;
14417 }
14418
14419 if (c->attr.proc_pointer && c->ts.interface)
14420 {
14421 gfc_symbol *ifc = c->ts.interface;
14422
14423 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
14424 {
14425 c->tb->error = 1;
14426 return false;
14427 }
14428
14429 if (ifc->attr.if_source || ifc->attr.intrinsic)
14430 {
14431 /* Resolve interface and copy attributes. */
14432 if (ifc->formal && !ifc->formal_ns)
14433 resolve_symbol (ifc);
14434 if (ifc->attr.intrinsic)
14435 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
14436
14437 if (ifc->result)
14438 {
14439 c->ts = ifc->result->ts;
14440 c->attr.allocatable = ifc->result->attr.allocatable;
14441 c->attr.pointer = ifc->result->attr.pointer;
14442 c->attr.dimension = ifc->result->attr.dimension;
14443 c->as = gfc_copy_array_spec (ifc->result->as);
14444 c->attr.class_ok = ifc->result->attr.class_ok;
14445 }
14446 else
14447 {
14448 c->ts = ifc->ts;
14449 c->attr.allocatable = ifc->attr.allocatable;
14450 c->attr.pointer = ifc->attr.pointer;
14451 c->attr.dimension = ifc->attr.dimension;
14452 c->as = gfc_copy_array_spec (ifc->as);
14453 c->attr.class_ok = ifc->attr.class_ok;
14454 }
14455 c->ts.interface = ifc;
14456 c->attr.function = ifc->attr.function;
14457 c->attr.subroutine = ifc->attr.subroutine;
14458
14459 c->attr.pure = ifc->attr.pure;
14460 c->attr.elemental = ifc->attr.elemental;
14461 c->attr.recursive = ifc->attr.recursive;
14462 c->attr.always_explicit = ifc->attr.always_explicit;
14463 c->attr.ext_attr |= ifc->attr.ext_attr;
14464 /* Copy char length. */
14465 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
14466 {
14467 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
14468 if (cl->length && !cl->resolved
14469 && !gfc_resolve_expr (cl->length))
14470 {
14471 c->tb->error = 1;
14472 return false;
14473 }
14474 c->ts.u.cl = cl;
14475 }
14476 }
14477 }
14478 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
14479 {
14480 /* Since PPCs are not implicitly typed, a PPC without an explicit
14481 interface must be a subroutine. */
14482 gfc_add_subroutine (&c->attr, c->name, &c->loc);
14483 }
14484
14485 /* Procedure pointer components: Check PASS arg. */
14486 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
14487 && !sym->attr.vtype)
14488 {
14489 gfc_symbol* me_arg;
14490
14491 if (c->tb->pass_arg)
14492 {
14493 gfc_formal_arglist* i;
14494
14495 /* If an explicit passing argument name is given, walk the arg-list
14496 and look for it. */
14497
14498 me_arg = NULL;
14499 c->tb->pass_arg_num = 1;
14500 for (i = c->ts.interface->formal; i; i = i->next)
14501 {
14502 if (!strcmp (i->sym->name, c->tb->pass_arg))
14503 {
14504 me_arg = i->sym;
14505 break;
14506 }
14507 c->tb->pass_arg_num++;
14508 }
14509
14510 if (!me_arg)
14511 {
14512 gfc_error ("Procedure pointer component %qs with PASS(%s) "
14513 "at %L has no argument %qs", c->name,
14514 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
14515 c->tb->error = 1;
14516 return false;
14517 }
14518 }
14519 else
14520 {
14521 /* Otherwise, take the first one; there should in fact be at least
14522 one. */
14523 c->tb->pass_arg_num = 1;
14524 if (!c->ts.interface->formal)
14525 {
14526 gfc_error ("Procedure pointer component %qs with PASS at %L "
14527 "must have at least one argument",
14528 c->name, &c->loc);
14529 c->tb->error = 1;
14530 return false;
14531 }
14532 me_arg = c->ts.interface->formal->sym;
14533 }
14534
14535 /* Now check that the argument-type matches. */
14536 gcc_assert (me_arg);
14537 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
14538 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
14539 || (me_arg->ts.type == BT_CLASS
14540 && CLASS_DATA (me_arg)->ts.u.derived != sym))
14541 {
14542 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14543 " the derived type %qs", me_arg->name, c->name,
14544 me_arg->name, &c->loc, sym->name);
14545 c->tb->error = 1;
14546 return false;
14547 }
14548
14549 /* Check for F03:C453. */
14550 if (CLASS_DATA (me_arg)->attr.dimension)
14551 {
14552 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14553 "must be scalar", me_arg->name, c->name, me_arg->name,
14554 &c->loc);
14555 c->tb->error = 1;
14556 return false;
14557 }
14558
14559 if (CLASS_DATA (me_arg)->attr.class_pointer)
14560 {
14561 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14562 "may not have the POINTER attribute", me_arg->name,
14563 c->name, me_arg->name, &c->loc);
14564 c->tb->error = 1;
14565 return false;
14566 }
14567
14568 if (CLASS_DATA (me_arg)->attr.allocatable)
14569 {
14570 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14571 "may not be ALLOCATABLE", me_arg->name, c->name,
14572 me_arg->name, &c->loc);
14573 c->tb->error = 1;
14574 return false;
14575 }
14576
14577 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
14578 {
14579 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14580 " at %L", c->name, &c->loc);
14581 return false;
14582 }
14583
14584 }
14585
14586 /* Check type-spec if this is not the parent-type component. */
14587 if (((sym->attr.is_class
14588 && (!sym->components->ts.u.derived->attr.extension
14589 || c != sym->components->ts.u.derived->components))
14590 || (!sym->attr.is_class
14591 && (!sym->attr.extension || c != sym->components)))
14592 && !sym->attr.vtype
14593 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
14594 return false;
14595
14596 super_type = gfc_get_derived_super_type (sym);
14597
14598 /* If this type is an extension, set the accessibility of the parent
14599 component. */
14600 if (super_type
14601 && ((sym->attr.is_class
14602 && c == sym->components->ts.u.derived->components)
14603 || (!sym->attr.is_class && c == sym->components))
14604 && strcmp (super_type->name, c->name) == 0)
14605 c->attr.access = super_type->attr.access;
14606
14607 /* If this type is an extension, see if this component has the same name
14608 as an inherited type-bound procedure. */
14609 if (super_type && !sym->attr.is_class
14610 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
14611 {
14612 gfc_error ("Component %qs of %qs at %L has the same name as an"
14613 " inherited type-bound procedure",
14614 c->name, sym->name, &c->loc);
14615 return false;
14616 }
14617
14618 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
14619 && !c->ts.deferred)
14620 {
14621 if (c->ts.u.cl->length == NULL
14622 || (!resolve_charlen(c->ts.u.cl))
14623 || !gfc_is_constant_expr (c->ts.u.cl->length))
14624 {
14625 gfc_error ("Character length of component %qs needs to "
14626 "be a constant specification expression at %L",
14627 c->name,
14628 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
14629 return false;
14630 }
14631 }
14632
14633 if (c->ts.type == BT_CHARACTER && c->ts.deferred
14634 && !c->attr.pointer && !c->attr.allocatable)
14635 {
14636 gfc_error ("Character component %qs of %qs at %L with deferred "
14637 "length must be a POINTER or ALLOCATABLE",
14638 c->name, sym->name, &c->loc);
14639 return false;
14640 }
14641
14642 /* Add the hidden deferred length field. */
14643 if (c->ts.type == BT_CHARACTER
14644 && (c->ts.deferred || c->attr.pdt_string)
14645 && !c->attr.function
14646 && !sym->attr.is_class)
14647 {
14648 char name[GFC_MAX_SYMBOL_LEN+9];
14649 gfc_component *strlen;
14650 sprintf (name, "_%s_length", c->name);
14651 strlen = gfc_find_component (sym, name, true, true, NULL);
14652 if (strlen == NULL)
14653 {
14654 if (!gfc_add_component (sym, name, &strlen))
14655 return false;
14656 strlen->ts.type = BT_INTEGER;
14657 strlen->ts.kind = gfc_charlen_int_kind;
14658 strlen->attr.access = ACCESS_PRIVATE;
14659 strlen->attr.artificial = 1;
14660 }
14661 }
14662
14663 if (c->ts.type == BT_DERIVED
14664 && sym->component_access != ACCESS_PRIVATE
14665 && gfc_check_symbol_access (sym)
14666 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
14667 && !c->ts.u.derived->attr.use_assoc
14668 && !gfc_check_symbol_access (c->ts.u.derived)
14669 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
14670 "PRIVATE type and cannot be a component of "
14671 "%qs, which is PUBLIC at %L", c->name,
14672 sym->name, &sym->declared_at))
14673 return false;
14674
14675 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
14676 {
14677 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
14678 "type %s", c->name, &c->loc, sym->name);
14679 return false;
14680 }
14681
14682 if (sym->attr.sequence)
14683 {
14684 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
14685 {
14686 gfc_error ("Component %s of SEQUENCE type declared at %L does "
14687 "not have the SEQUENCE attribute",
14688 c->ts.u.derived->name, &sym->declared_at);
14689 return false;
14690 }
14691 }
14692
14693 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
14694 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
14695 else if (c->ts.type == BT_CLASS && c->attr.class_ok
14696 && CLASS_DATA (c)->ts.u.derived->attr.generic)
14697 CLASS_DATA (c)->ts.u.derived
14698 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
14699
14700 /* If an allocatable component derived type is of the same type as
14701 the enclosing derived type, we need a vtable generating so that
14702 the __deallocate procedure is created. */
14703 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
14704 && c->ts.u.derived == sym && c->attr.allocatable == 1)
14705 gfc_find_vtab (&c->ts);
14706
14707 /* Ensure that all the derived type components are put on the
14708 derived type list; even in formal namespaces, where derived type
14709 pointer components might not have been declared. */
14710 if (c->ts.type == BT_DERIVED
14711 && c->ts.u.derived
14712 && c->ts.u.derived->components
14713 && c->attr.pointer
14714 && sym != c->ts.u.derived)
14715 add_dt_to_dt_list (c->ts.u.derived);
14716
14717 if (!gfc_resolve_array_spec (c->as,
14718 !(c->attr.pointer || c->attr.proc_pointer
14719 || c->attr.allocatable)))
14720 return false;
14721
14722 if (c->initializer && !sym->attr.vtype
14723 && !c->attr.pdt_kind && !c->attr.pdt_len
14724 && !gfc_check_assign_symbol (sym, c, c->initializer))
14725 return false;
14726
14727 return true;
14728 }
14729
14730
14731 /* Be nice about the locus for a structure expression - show the locus of the
14732 first non-null sub-expression if we can. */
14733
14734 static locus *
14735 cons_where (gfc_expr *struct_expr)
14736 {
14737 gfc_constructor *cons;
14738
14739 gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
14740
14741 cons = gfc_constructor_first (struct_expr->value.constructor);
14742 for (; cons; cons = gfc_constructor_next (cons))
14743 {
14744 if (cons->expr && cons->expr->expr_type != EXPR_NULL)
14745 return &cons->expr->where;
14746 }
14747
14748 return &struct_expr->where;
14749 }
14750
14751 /* Resolve the components of a structure type. Much less work than derived
14752 types. */
14753
14754 static bool
14755 resolve_fl_struct (gfc_symbol *sym)
14756 {
14757 gfc_component *c;
14758 gfc_expr *init = NULL;
14759 bool success;
14760
14761 /* Make sure UNIONs do not have overlapping initializers. */
14762 if (sym->attr.flavor == FL_UNION)
14763 {
14764 for (c = sym->components; c; c = c->next)
14765 {
14766 if (init && c->initializer)
14767 {
14768 gfc_error ("Conflicting initializers in union at %L and %L",
14769 cons_where (init), cons_where (c->initializer));
14770 gfc_free_expr (c->initializer);
14771 c->initializer = NULL;
14772 }
14773 if (init == NULL)
14774 init = c->initializer;
14775 }
14776 }
14777
14778 success = true;
14779 for (c = sym->components; c; c = c->next)
14780 if (!resolve_component (c, sym))
14781 success = false;
14782
14783 if (!success)
14784 return false;
14785
14786 if (sym->components)
14787 add_dt_to_dt_list (sym);
14788
14789 return true;
14790 }
14791
14792
14793 /* Resolve the components of a derived type. This does not have to wait until
14794 resolution stage, but can be done as soon as the dt declaration has been
14795 parsed. */
14796
14797 static bool
14798 resolve_fl_derived0 (gfc_symbol *sym)
14799 {
14800 gfc_symbol* super_type;
14801 gfc_component *c;
14802 gfc_formal_arglist *f;
14803 bool success;
14804
14805 if (sym->attr.unlimited_polymorphic)
14806 return true;
14807
14808 super_type = gfc_get_derived_super_type (sym);
14809
14810 /* F2008, C432. */
14811 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
14812 {
14813 gfc_error ("As extending type %qs at %L has a coarray component, "
14814 "parent type %qs shall also have one", sym->name,
14815 &sym->declared_at, super_type->name);
14816 return false;
14817 }
14818
14819 /* Ensure the extended type gets resolved before we do. */
14820 if (super_type && !resolve_fl_derived0 (super_type))
14821 return false;
14822
14823 /* An ABSTRACT type must be extensible. */
14824 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
14825 {
14826 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
14827 sym->name, &sym->declared_at);
14828 return false;
14829 }
14830
14831 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
14832 : sym->components;
14833
14834 success = true;
14835 for ( ; c != NULL; c = c->next)
14836 if (!resolve_component (c, sym))
14837 success = false;
14838
14839 if (!success)
14840 return false;
14841
14842 /* Now add the caf token field, where needed. */
14843 if (flag_coarray != GFC_FCOARRAY_NONE
14844 && !sym->attr.is_class && !sym->attr.vtype)
14845 {
14846 for (c = sym->components; c; c = c->next)
14847 if (!c->attr.dimension && !c->attr.codimension
14848 && (c->attr.allocatable || c->attr.pointer))
14849 {
14850 char name[GFC_MAX_SYMBOL_LEN+9];
14851 gfc_component *token;
14852 sprintf (name, "_caf_%s", c->name);
14853 token = gfc_find_component (sym, name, true, true, NULL);
14854 if (token == NULL)
14855 {
14856 if (!gfc_add_component (sym, name, &token))
14857 return false;
14858 token->ts.type = BT_VOID;
14859 token->ts.kind = gfc_default_integer_kind;
14860 token->attr.access = ACCESS_PRIVATE;
14861 token->attr.artificial = 1;
14862 token->attr.caf_token = 1;
14863 }
14864 }
14865 }
14866
14867 check_defined_assignments (sym);
14868
14869 if (!sym->attr.defined_assign_comp && super_type)
14870 sym->attr.defined_assign_comp
14871 = super_type->attr.defined_assign_comp;
14872
14873 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
14874 all DEFERRED bindings are overridden. */
14875 if (super_type && super_type->attr.abstract && !sym->attr.abstract
14876 && !sym->attr.is_class
14877 && !ensure_not_abstract (sym, super_type))
14878 return false;
14879
14880 /* Check that there is a component for every PDT parameter. */
14881 if (sym->attr.pdt_template)
14882 {
14883 for (f = sym->formal; f; f = f->next)
14884 {
14885 if (!f->sym)
14886 continue;
14887 c = gfc_find_component (sym, f->sym->name, true, true, NULL);
14888 if (c == NULL)
14889 {
14890 gfc_error ("Parameterized type %qs does not have a component "
14891 "corresponding to parameter %qs at %L", sym->name,
14892 f->sym->name, &sym->declared_at);
14893 break;
14894 }
14895 }
14896 }
14897
14898 /* Add derived type to the derived type list. */
14899 add_dt_to_dt_list (sym);
14900
14901 return true;
14902 }
14903
14904
14905 /* The following procedure does the full resolution of a derived type,
14906 including resolution of all type-bound procedures (if present). In contrast
14907 to 'resolve_fl_derived0' this can only be done after the module has been
14908 parsed completely. */
14909
14910 static bool
14911 resolve_fl_derived (gfc_symbol *sym)
14912 {
14913 gfc_symbol *gen_dt = NULL;
14914
14915 if (sym->attr.unlimited_polymorphic)
14916 return true;
14917
14918 if (!sym->attr.is_class)
14919 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
14920 if (gen_dt && gen_dt->generic && gen_dt->generic->next
14921 && (!gen_dt->generic->sym->attr.use_assoc
14922 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
14923 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
14924 "%qs at %L being the same name as derived "
14925 "type at %L", sym->name,
14926 gen_dt->generic->sym == sym
14927 ? gen_dt->generic->next->sym->name
14928 : gen_dt->generic->sym->name,
14929 gen_dt->generic->sym == sym
14930 ? &gen_dt->generic->next->sym->declared_at
14931 : &gen_dt->generic->sym->declared_at,
14932 &sym->declared_at))
14933 return false;
14934
14935 if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
14936 {
14937 gfc_error ("Derived type %qs at %L has not been declared",
14938 sym->name, &sym->declared_at);
14939 return false;
14940 }
14941
14942 /* Resolve the finalizer procedures. */
14943 if (!gfc_resolve_finalizers (sym, NULL))
14944 return false;
14945
14946 if (sym->attr.is_class && sym->ts.u.derived == NULL)
14947 {
14948 /* Fix up incomplete CLASS symbols. */
14949 gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
14950 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
14951
14952 /* Nothing more to do for unlimited polymorphic entities. */
14953 if (data->ts.u.derived->attr.unlimited_polymorphic)
14954 return true;
14955 else if (vptr->ts.u.derived == NULL)
14956 {
14957 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
14958 gcc_assert (vtab);
14959 vptr->ts.u.derived = vtab->ts.u.derived;
14960 if (!resolve_fl_derived0 (vptr->ts.u.derived))
14961 return false;
14962 }
14963 }
14964
14965 if (!resolve_fl_derived0 (sym))
14966 return false;
14967
14968 /* Resolve the type-bound procedures. */
14969 if (!resolve_typebound_procedures (sym))
14970 return false;
14971
14972 /* Generate module vtables subject to their accessibility and their not
14973 being vtables or pdt templates. If this is not done class declarations
14974 in external procedures wind up with their own version and so SELECT TYPE
14975 fails because the vptrs do not have the same address. */
14976 if (gfc_option.allow_std & GFC_STD_F2003
14977 && sym->ns->proc_name
14978 && sym->ns->proc_name->attr.flavor == FL_MODULE
14979 && sym->attr.access != ACCESS_PRIVATE
14980 && !(sym->attr.use_assoc || sym->attr.vtype || sym->attr.pdt_template))
14981 {
14982 gfc_symbol *vtab = gfc_find_derived_vtab (sym);
14983 gfc_set_sym_referenced (vtab);
14984 }
14985
14986 return true;
14987 }
14988
14989
14990 static bool
14991 resolve_fl_namelist (gfc_symbol *sym)
14992 {
14993 gfc_namelist *nl;
14994 gfc_symbol *nlsym;
14995
14996 for (nl = sym->namelist; nl; nl = nl->next)
14997 {
14998 /* Check again, the check in match only works if NAMELIST comes
14999 after the decl. */
15000 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
15001 {
15002 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
15003 "allowed", nl->sym->name, sym->name, &sym->declared_at);
15004 return false;
15005 }
15006
15007 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
15008 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
15009 "with assumed shape in namelist %qs at %L",
15010 nl->sym->name, sym->name, &sym->declared_at))
15011 return false;
15012
15013 if (is_non_constant_shape_array (nl->sym)
15014 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
15015 "with nonconstant shape in namelist %qs at %L",
15016 nl->sym->name, sym->name, &sym->declared_at))
15017 return false;
15018
15019 if (nl->sym->ts.type == BT_CHARACTER
15020 && (nl->sym->ts.u.cl->length == NULL
15021 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
15022 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
15023 "nonconstant character length in "
15024 "namelist %qs at %L", nl->sym->name,
15025 sym->name, &sym->declared_at))
15026 return false;
15027
15028 }
15029
15030 /* Reject PRIVATE objects in a PUBLIC namelist. */
15031 if (gfc_check_symbol_access (sym))
15032 {
15033 for (nl = sym->namelist; nl; nl = nl->next)
15034 {
15035 if (!nl->sym->attr.use_assoc
15036 && !is_sym_host_assoc (nl->sym, sym->ns)
15037 && !gfc_check_symbol_access (nl->sym))
15038 {
15039 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
15040 "cannot be member of PUBLIC namelist %qs at %L",
15041 nl->sym->name, sym->name, &sym->declared_at);
15042 return false;
15043 }
15044
15045 if (nl->sym->ts.type == BT_DERIVED
15046 && (nl->sym->ts.u.derived->attr.alloc_comp
15047 || nl->sym->ts.u.derived->attr.pointer_comp))
15048 {
15049 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
15050 "namelist %qs at %L with ALLOCATABLE "
15051 "or POINTER components", nl->sym->name,
15052 sym->name, &sym->declared_at))
15053 return false;
15054 return true;
15055 }
15056
15057 /* Types with private components that came here by USE-association. */
15058 if (nl->sym->ts.type == BT_DERIVED
15059 && derived_inaccessible (nl->sym->ts.u.derived))
15060 {
15061 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
15062 "components and cannot be member of namelist %qs at %L",
15063 nl->sym->name, sym->name, &sym->declared_at);
15064 return false;
15065 }
15066
15067 /* Types with private components that are defined in the same module. */
15068 if (nl->sym->ts.type == BT_DERIVED
15069 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
15070 && nl->sym->ts.u.derived->attr.private_comp)
15071 {
15072 gfc_error ("NAMELIST object %qs has PRIVATE components and "
15073 "cannot be a member of PUBLIC namelist %qs at %L",
15074 nl->sym->name, sym->name, &sym->declared_at);
15075 return false;
15076 }
15077 }
15078 }
15079
15080
15081 /* 14.1.2 A module or internal procedure represent local entities
15082 of the same type as a namelist member and so are not allowed. */
15083 for (nl = sym->namelist; nl; nl = nl->next)
15084 {
15085 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
15086 continue;
15087
15088 if (nl->sym->attr.function && nl->sym == nl->sym->result)
15089 if ((nl->sym == sym->ns->proc_name)
15090 ||
15091 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
15092 continue;
15093
15094 nlsym = NULL;
15095 if (nl->sym->name)
15096 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
15097 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
15098 {
15099 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
15100 "attribute in %qs at %L", nlsym->name,
15101 &sym->declared_at);
15102 return false;
15103 }
15104 }
15105
15106 return true;
15107 }
15108
15109
15110 static bool
15111 resolve_fl_parameter (gfc_symbol *sym)
15112 {
15113 /* A parameter array's shape needs to be constant. */
15114 if (sym->as != NULL
15115 && (sym->as->type == AS_DEFERRED
15116 || is_non_constant_shape_array (sym)))
15117 {
15118 gfc_error ("Parameter array %qs at %L cannot be automatic "
15119 "or of deferred shape", sym->name, &sym->declared_at);
15120 return false;
15121 }
15122
15123 /* Constraints on deferred type parameter. */
15124 if (!deferred_requirements (sym))
15125 return false;
15126
15127 /* Make sure a parameter that has been implicitly typed still
15128 matches the implicit type, since PARAMETER statements can precede
15129 IMPLICIT statements. */
15130 if (sym->attr.implicit_type
15131 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
15132 sym->ns)))
15133 {
15134 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
15135 "later IMPLICIT type", sym->name, &sym->declared_at);
15136 return false;
15137 }
15138
15139 /* Make sure the types of derived parameters are consistent. This
15140 type checking is deferred until resolution because the type may
15141 refer to a derived type from the host. */
15142 if (sym->ts.type == BT_DERIVED
15143 && !gfc_compare_types (&sym->ts, &sym->value->ts))
15144 {
15145 gfc_error ("Incompatible derived type in PARAMETER at %L",
15146 &sym->value->where);
15147 return false;
15148 }
15149
15150 /* F03:C509,C514. */
15151 if (sym->ts.type == BT_CLASS)
15152 {
15153 gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
15154 sym->name, &sym->declared_at);
15155 return false;
15156 }
15157
15158 return true;
15159 }
15160
15161
15162 /* Called by resolve_symbol to check PDTs. */
15163
15164 static void
15165 resolve_pdt (gfc_symbol* sym)
15166 {
15167 gfc_symbol *derived = NULL;
15168 gfc_actual_arglist *param;
15169 gfc_component *c;
15170 bool const_len_exprs = true;
15171 bool assumed_len_exprs = false;
15172 symbol_attribute *attr;
15173
15174 if (sym->ts.type == BT_DERIVED)
15175 {
15176 derived = sym->ts.u.derived;
15177 attr = &(sym->attr);
15178 }
15179 else if (sym->ts.type == BT_CLASS)
15180 {
15181 derived = CLASS_DATA (sym)->ts.u.derived;
15182 attr = &(CLASS_DATA (sym)->attr);
15183 }
15184 else
15185 gcc_unreachable ();
15186
15187 gcc_assert (derived->attr.pdt_type);
15188
15189 for (param = sym->param_list; param; param = param->next)
15190 {
15191 c = gfc_find_component (derived, param->name, false, true, NULL);
15192 gcc_assert (c);
15193 if (c->attr.pdt_kind)
15194 continue;
15195
15196 if (param->expr && !gfc_is_constant_expr (param->expr)
15197 && c->attr.pdt_len)
15198 const_len_exprs = false;
15199 else if (param->spec_type == SPEC_ASSUMED)
15200 assumed_len_exprs = true;
15201
15202 if (param->spec_type == SPEC_DEFERRED
15203 && !attr->allocatable && !attr->pointer)
15204 gfc_error ("The object %qs at %L has a deferred LEN "
15205 "parameter %qs and is neither allocatable "
15206 "nor a pointer", sym->name, &sym->declared_at,
15207 param->name);
15208
15209 }
15210
15211 if (!const_len_exprs
15212 && (sym->ns->proc_name->attr.is_main_program
15213 || sym->ns->proc_name->attr.flavor == FL_MODULE
15214 || sym->attr.save != SAVE_NONE))
15215 gfc_error ("The AUTOMATIC object %qs at %L must not have the "
15216 "SAVE attribute or be a variable declared in the "
15217 "main program, a module or a submodule(F08/C513)",
15218 sym->name, &sym->declared_at);
15219
15220 if (assumed_len_exprs && !(sym->attr.dummy
15221 || sym->attr.select_type_temporary || sym->attr.associate_var))
15222 gfc_error ("The object %qs at %L with ASSUMED type parameters "
15223 "must be a dummy or a SELECT TYPE selector(F08/4.2)",
15224 sym->name, &sym->declared_at);
15225 }
15226
15227
15228 /* Do anything necessary to resolve a symbol. Right now, we just
15229 assume that an otherwise unknown symbol is a variable. This sort
15230 of thing commonly happens for symbols in module. */
15231
15232 static void
15233 resolve_symbol (gfc_symbol *sym)
15234 {
15235 int check_constant, mp_flag;
15236 gfc_symtree *symtree;
15237 gfc_symtree *this_symtree;
15238 gfc_namespace *ns;
15239 gfc_component *c;
15240 symbol_attribute class_attr;
15241 gfc_array_spec *as;
15242 bool saved_specification_expr;
15243
15244 if (sym->resolve_symbol_called >= 1)
15245 return;
15246 sym->resolve_symbol_called = 1;
15247
15248 /* No symbol will ever have union type; only components can be unions.
15249 Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
15250 (just like derived type declaration symbols have flavor FL_DERIVED). */
15251 gcc_assert (sym->ts.type != BT_UNION);
15252
15253 /* Coarrayed polymorphic objects with allocatable or pointer components are
15254 yet unsupported for -fcoarray=lib. */
15255 if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
15256 && sym->ts.u.derived && CLASS_DATA (sym)
15257 && CLASS_DATA (sym)->attr.codimension
15258 && CLASS_DATA (sym)->ts.u.derived
15259 && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
15260 || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
15261 {
15262 gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
15263 "type coarrays at %L are unsupported", &sym->declared_at);
15264 return;
15265 }
15266
15267 if (sym->attr.artificial)
15268 return;
15269
15270 if (sym->attr.unlimited_polymorphic)
15271 return;
15272
15273 if (sym->attr.flavor == FL_UNKNOWN
15274 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
15275 && !sym->attr.generic && !sym->attr.external
15276 && sym->attr.if_source == IFSRC_UNKNOWN
15277 && sym->ts.type == BT_UNKNOWN))
15278 {
15279
15280 /* If we find that a flavorless symbol is an interface in one of the
15281 parent namespaces, find its symtree in this namespace, free the
15282 symbol and set the symtree to point to the interface symbol. */
15283 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
15284 {
15285 symtree = gfc_find_symtree (ns->sym_root, sym->name);
15286 if (symtree && (symtree->n.sym->generic ||
15287 (symtree->n.sym->attr.flavor == FL_PROCEDURE
15288 && sym->ns->construct_entities)))
15289 {
15290 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
15291 sym->name);
15292 if (this_symtree->n.sym == sym)
15293 {
15294 symtree->n.sym->refs++;
15295 gfc_release_symbol (sym);
15296 this_symtree->n.sym = symtree->n.sym;
15297 return;
15298 }
15299 }
15300 }
15301
15302 /* Otherwise give it a flavor according to such attributes as
15303 it has. */
15304 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
15305 && sym->attr.intrinsic == 0)
15306 sym->attr.flavor = FL_VARIABLE;
15307 else if (sym->attr.flavor == FL_UNKNOWN)
15308 {
15309 sym->attr.flavor = FL_PROCEDURE;
15310 if (sym->attr.dimension)
15311 sym->attr.function = 1;
15312 }
15313 }
15314
15315 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
15316 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
15317
15318 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
15319 && !resolve_procedure_interface (sym))
15320 return;
15321
15322 if (sym->attr.is_protected && !sym->attr.proc_pointer
15323 && (sym->attr.procedure || sym->attr.external))
15324 {
15325 if (sym->attr.external)
15326 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
15327 "at %L", &sym->declared_at);
15328 else
15329 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
15330 "at %L", &sym->declared_at);
15331
15332 return;
15333 }
15334
15335 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
15336 return;
15337
15338 else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
15339 && !resolve_fl_struct (sym))
15340 return;
15341
15342 /* Symbols that are module procedures with results (functions) have
15343 the types and array specification copied for type checking in
15344 procedures that call them, as well as for saving to a module
15345 file. These symbols can't stand the scrutiny that their results
15346 can. */
15347 mp_flag = (sym->result != NULL && sym->result != sym);
15348
15349 /* Make sure that the intrinsic is consistent with its internal
15350 representation. This needs to be done before assigning a default
15351 type to avoid spurious warnings. */
15352 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
15353 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
15354 return;
15355
15356 /* Resolve associate names. */
15357 if (sym->assoc)
15358 resolve_assoc_var (sym, true);
15359
15360 /* Assign default type to symbols that need one and don't have one. */
15361 if (sym->ts.type == BT_UNKNOWN)
15362 {
15363 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
15364 {
15365 gfc_set_default_type (sym, 1, NULL);
15366 }
15367
15368 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
15369 && !sym->attr.function && !sym->attr.subroutine
15370 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
15371 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
15372
15373 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
15374 {
15375 /* The specific case of an external procedure should emit an error
15376 in the case that there is no implicit type. */
15377 if (!mp_flag)
15378 {
15379 if (!sym->attr.mixed_entry_master)
15380 gfc_set_default_type (sym, sym->attr.external, NULL);
15381 }
15382 else
15383 {
15384 /* Result may be in another namespace. */
15385 resolve_symbol (sym->result);
15386
15387 if (!sym->result->attr.proc_pointer)
15388 {
15389 sym->ts = sym->result->ts;
15390 sym->as = gfc_copy_array_spec (sym->result->as);
15391 sym->attr.dimension = sym->result->attr.dimension;
15392 sym->attr.pointer = sym->result->attr.pointer;
15393 sym->attr.allocatable = sym->result->attr.allocatable;
15394 sym->attr.contiguous = sym->result->attr.contiguous;
15395 }
15396 }
15397 }
15398 }
15399 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
15400 {
15401 bool saved_specification_expr = specification_expr;
15402 bool saved_formal_arg_flag = formal_arg_flag;
15403
15404 specification_expr = true;
15405 formal_arg_flag = true;
15406 gfc_resolve_array_spec (sym->result->as, false);
15407 formal_arg_flag = saved_formal_arg_flag;
15408 specification_expr = saved_specification_expr;
15409 }
15410
15411 if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived)
15412 {
15413 as = CLASS_DATA (sym)->as;
15414 class_attr = CLASS_DATA (sym)->attr;
15415 class_attr.pointer = class_attr.class_pointer;
15416 }
15417 else
15418 {
15419 class_attr = sym->attr;
15420 as = sym->as;
15421 }
15422
15423 /* F2008, C530. */
15424 if (sym->attr.contiguous
15425 && (!class_attr.dimension
15426 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
15427 && !class_attr.pointer)))
15428 {
15429 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
15430 "array pointer or an assumed-shape or assumed-rank array",
15431 sym->name, &sym->declared_at);
15432 return;
15433 }
15434
15435 /* Assumed size arrays and assumed shape arrays must be dummy
15436 arguments. Array-spec's of implied-shape should have been resolved to
15437 AS_EXPLICIT already. */
15438
15439 if (as)
15440 {
15441 /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
15442 specification expression. */
15443 if (as->type == AS_IMPLIED_SHAPE)
15444 {
15445 int i;
15446 for (i=0; i<as->rank; i++)
15447 {
15448 if (as->lower[i] != NULL && as->upper[i] == NULL)
15449 {
15450 gfc_error ("Bad specification for assumed size array at %L",
15451 &as->lower[i]->where);
15452 return;
15453 }
15454 }
15455 gcc_unreachable();
15456 }
15457
15458 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
15459 || as->type == AS_ASSUMED_SHAPE)
15460 && !sym->attr.dummy && !sym->attr.select_type_temporary)
15461 {
15462 if (as->type == AS_ASSUMED_SIZE)
15463 gfc_error ("Assumed size array at %L must be a dummy argument",
15464 &sym->declared_at);
15465 else
15466 gfc_error ("Assumed shape array at %L must be a dummy argument",
15467 &sym->declared_at);
15468 return;
15469 }
15470 /* TS 29113, C535a. */
15471 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
15472 && !sym->attr.select_type_temporary
15473 && !(cs_base && cs_base->current
15474 && cs_base->current->op == EXEC_SELECT_RANK))
15475 {
15476 gfc_error ("Assumed-rank array at %L must be a dummy argument",
15477 &sym->declared_at);
15478 return;
15479 }
15480 if (as->type == AS_ASSUMED_RANK
15481 && (sym->attr.codimension || sym->attr.value))
15482 {
15483 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
15484 "CODIMENSION attribute", &sym->declared_at);
15485 return;
15486 }
15487 }
15488
15489 /* Make sure symbols with known intent or optional are really dummy
15490 variable. Because of ENTRY statement, this has to be deferred
15491 until resolution time. */
15492
15493 if (!sym->attr.dummy
15494 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
15495 {
15496 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
15497 return;
15498 }
15499
15500 if (sym->attr.value && !sym->attr.dummy)
15501 {
15502 gfc_error ("%qs at %L cannot have the VALUE attribute because "
15503 "it is not a dummy argument", sym->name, &sym->declared_at);
15504 return;
15505 }
15506
15507 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
15508 {
15509 gfc_charlen *cl = sym->ts.u.cl;
15510 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
15511 {
15512 gfc_error ("Character dummy variable %qs at %L with VALUE "
15513 "attribute must have constant length",
15514 sym->name, &sym->declared_at);
15515 return;
15516 }
15517
15518 if (sym->ts.is_c_interop
15519 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
15520 {
15521 gfc_error ("C interoperable character dummy variable %qs at %L "
15522 "with VALUE attribute must have length one",
15523 sym->name, &sym->declared_at);
15524 return;
15525 }
15526 }
15527
15528 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
15529 && sym->ts.u.derived->attr.generic)
15530 {
15531 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
15532 if (!sym->ts.u.derived)
15533 {
15534 gfc_error ("The derived type %qs at %L is of type %qs, "
15535 "which has not been defined", sym->name,
15536 &sym->declared_at, sym->ts.u.derived->name);
15537 sym->ts.type = BT_UNKNOWN;
15538 return;
15539 }
15540 }
15541
15542 /* Use the same constraints as TYPE(*), except for the type check
15543 and that only scalars and assumed-size arrays are permitted. */
15544 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
15545 {
15546 if (!sym->attr.dummy)
15547 {
15548 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
15549 "a dummy argument", sym->name, &sym->declared_at);
15550 return;
15551 }
15552
15553 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
15554 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
15555 && sym->ts.type != BT_COMPLEX)
15556 {
15557 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
15558 "of type TYPE(*) or of an numeric intrinsic type",
15559 sym->name, &sym->declared_at);
15560 return;
15561 }
15562
15563 if (sym->attr.allocatable || sym->attr.codimension
15564 || sym->attr.pointer || sym->attr.value)
15565 {
15566 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
15567 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
15568 "attribute", sym->name, &sym->declared_at);
15569 return;
15570 }
15571
15572 if (sym->attr.intent == INTENT_OUT)
15573 {
15574 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
15575 "have the INTENT(OUT) attribute",
15576 sym->name, &sym->declared_at);
15577 return;
15578 }
15579 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
15580 {
15581 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
15582 "either be a scalar or an assumed-size array",
15583 sym->name, &sym->declared_at);
15584 return;
15585 }
15586
15587 /* Set the type to TYPE(*) and add a dimension(*) to ensure
15588 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
15589 packing. */
15590 sym->ts.type = BT_ASSUMED;
15591 sym->as = gfc_get_array_spec ();
15592 sym->as->type = AS_ASSUMED_SIZE;
15593 sym->as->rank = 1;
15594 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
15595 }
15596 else if (sym->ts.type == BT_ASSUMED)
15597 {
15598 /* TS 29113, C407a. */
15599 if (!sym->attr.dummy)
15600 {
15601 gfc_error ("Assumed type of variable %s at %L is only permitted "
15602 "for dummy variables", sym->name, &sym->declared_at);
15603 return;
15604 }
15605 if (sym->attr.allocatable || sym->attr.codimension
15606 || sym->attr.pointer || sym->attr.value)
15607 {
15608 gfc_error ("Assumed-type variable %s at %L may not have the "
15609 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
15610 sym->name, &sym->declared_at);
15611 return;
15612 }
15613 if (sym->attr.intent == INTENT_OUT)
15614 {
15615 gfc_error ("Assumed-type variable %s at %L may not have the "
15616 "INTENT(OUT) attribute",
15617 sym->name, &sym->declared_at);
15618 return;
15619 }
15620 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
15621 {
15622 gfc_error ("Assumed-type variable %s at %L shall not be an "
15623 "explicit-shape array", sym->name, &sym->declared_at);
15624 return;
15625 }
15626 }
15627
15628 /* If the symbol is marked as bind(c), that it is declared at module level
15629 scope and verify its type and kind. Do not do the latter for symbols
15630 that are implicitly typed because that is handled in
15631 gfc_set_default_type. Handle dummy arguments and procedure definitions
15632 separately. Also, anything that is use associated is not handled here
15633 but instead is handled in the module it is declared in. Finally, derived
15634 type definitions are allowed to be BIND(C) since that only implies that
15635 they're interoperable, and they are checked fully for interoperability
15636 when a variable is declared of that type. */
15637 if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
15638 && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
15639 && sym->attr.flavor != FL_DERIVED)
15640 {
15641 bool t = true;
15642
15643 /* First, make sure the variable is declared at the
15644 module-level scope (J3/04-007, Section 15.3). */
15645 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
15646 sym->attr.in_common == 0)
15647 {
15648 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
15649 "is neither a COMMON block nor declared at the "
15650 "module level scope", sym->name, &(sym->declared_at));
15651 t = false;
15652 }
15653 else if (sym->ts.type == BT_CHARACTER
15654 && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
15655 || !gfc_is_constant_expr (sym->ts.u.cl->length)
15656 || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
15657 {
15658 gfc_error ("BIND(C) Variable %qs at %L must have length one",
15659 sym->name, &sym->declared_at);
15660 t = false;
15661 }
15662 else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
15663 {
15664 t = verify_com_block_vars_c_interop (sym->common_head);
15665 }
15666 else if (sym->attr.implicit_type == 0)
15667 {
15668 /* If type() declaration, we need to verify that the components
15669 of the given type are all C interoperable, etc. */
15670 if (sym->ts.type == BT_DERIVED &&
15671 sym->ts.u.derived->attr.is_c_interop != 1)
15672 {
15673 /* Make sure the user marked the derived type as BIND(C). If
15674 not, call the verify routine. This could print an error
15675 for the derived type more than once if multiple variables
15676 of that type are declared. */
15677 if (sym->ts.u.derived->attr.is_bind_c != 1)
15678 verify_bind_c_derived_type (sym->ts.u.derived);
15679 t = false;
15680 }
15681
15682 /* Verify the variable itself as C interoperable if it
15683 is BIND(C). It is not possible for this to succeed if
15684 the verify_bind_c_derived_type failed, so don't have to handle
15685 any error returned by verify_bind_c_derived_type. */
15686 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
15687 sym->common_block);
15688 }
15689
15690 if (!t)
15691 {
15692 /* clear the is_bind_c flag to prevent reporting errors more than
15693 once if something failed. */
15694 sym->attr.is_bind_c = 0;
15695 return;
15696 }
15697 }
15698
15699 /* If a derived type symbol has reached this point, without its
15700 type being declared, we have an error. Notice that most
15701 conditions that produce undefined derived types have already
15702 been dealt with. However, the likes of:
15703 implicit type(t) (t) ..... call foo (t) will get us here if
15704 the type is not declared in the scope of the implicit
15705 statement. Change the type to BT_UNKNOWN, both because it is so
15706 and to prevent an ICE. */
15707 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
15708 && sym->ts.u.derived->components == NULL
15709 && !sym->ts.u.derived->attr.zero_comp)
15710 {
15711 gfc_error ("The derived type %qs at %L is of type %qs, "
15712 "which has not been defined", sym->name,
15713 &sym->declared_at, sym->ts.u.derived->name);
15714 sym->ts.type = BT_UNKNOWN;
15715 return;
15716 }
15717
15718 /* Make sure that the derived type has been resolved and that the
15719 derived type is visible in the symbol's namespace, if it is a
15720 module function and is not PRIVATE. */
15721 if (sym->ts.type == BT_DERIVED
15722 && sym->ts.u.derived->attr.use_assoc
15723 && sym->ns->proc_name
15724 && sym->ns->proc_name->attr.flavor == FL_MODULE
15725 && !resolve_fl_derived (sym->ts.u.derived))
15726 return;
15727
15728 /* Unless the derived-type declaration is use associated, Fortran 95
15729 does not allow public entries of private derived types.
15730 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
15731 161 in 95-006r3. */
15732 if (sym->ts.type == BT_DERIVED
15733 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
15734 && !sym->ts.u.derived->attr.use_assoc
15735 && gfc_check_symbol_access (sym)
15736 && !gfc_check_symbol_access (sym->ts.u.derived)
15737 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
15738 "derived type %qs",
15739 (sym->attr.flavor == FL_PARAMETER)
15740 ? "parameter" : "variable",
15741 sym->name, &sym->declared_at,
15742 sym->ts.u.derived->name))
15743 return;
15744
15745 /* F2008, C1302. */
15746 if (sym->ts.type == BT_DERIVED
15747 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
15748 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
15749 || sym->ts.u.derived->attr.lock_comp)
15750 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
15751 {
15752 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
15753 "type LOCK_TYPE must be a coarray", sym->name,
15754 &sym->declared_at);
15755 return;
15756 }
15757
15758 /* TS18508, C702/C703. */
15759 if (sym->ts.type == BT_DERIVED
15760 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
15761 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
15762 || sym->ts.u.derived->attr.event_comp)
15763 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
15764 {
15765 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
15766 "type EVENT_TYPE must be a coarray", sym->name,
15767 &sym->declared_at);
15768 return;
15769 }
15770
15771 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
15772 default initialization is defined (5.1.2.4.4). */
15773 if (sym->ts.type == BT_DERIVED
15774 && sym->attr.dummy
15775 && sym->attr.intent == INTENT_OUT
15776 && sym->as
15777 && sym->as->type == AS_ASSUMED_SIZE)
15778 {
15779 for (c = sym->ts.u.derived->components; c; c = c->next)
15780 {
15781 if (c->initializer)
15782 {
15783 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
15784 "ASSUMED SIZE and so cannot have a default initializer",
15785 sym->name, &sym->declared_at);
15786 return;
15787 }
15788 }
15789 }
15790
15791 /* F2008, C542. */
15792 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
15793 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
15794 {
15795 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
15796 "INTENT(OUT)", sym->name, &sym->declared_at);
15797 return;
15798 }
15799
15800 /* TS18508. */
15801 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
15802 && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
15803 {
15804 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
15805 "INTENT(OUT)", sym->name, &sym->declared_at);
15806 return;
15807 }
15808
15809 /* F2008, C525. */
15810 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15811 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15812 && sym->ts.u.derived && CLASS_DATA (sym)
15813 && CLASS_DATA (sym)->attr.coarray_comp))
15814 || class_attr.codimension)
15815 && (sym->attr.result || sym->result == sym))
15816 {
15817 gfc_error ("Function result %qs at %L shall not be a coarray or have "
15818 "a coarray component", sym->name, &sym->declared_at);
15819 return;
15820 }
15821
15822 /* F2008, C524. */
15823 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
15824 && sym->ts.u.derived->ts.is_iso_c)
15825 {
15826 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
15827 "shall not be a coarray", sym->name, &sym->declared_at);
15828 return;
15829 }
15830
15831 /* F2008, C525. */
15832 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15833 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15834 && sym->ts.u.derived && CLASS_DATA (sym)
15835 && CLASS_DATA (sym)->attr.coarray_comp))
15836 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
15837 || class_attr.allocatable))
15838 {
15839 gfc_error ("Variable %qs at %L with coarray component shall be a "
15840 "nonpointer, nonallocatable scalar, which is not a coarray",
15841 sym->name, &sym->declared_at);
15842 return;
15843 }
15844
15845 /* F2008, C526. The function-result case was handled above. */
15846 if (class_attr.codimension
15847 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
15848 || sym->attr.select_type_temporary
15849 || sym->attr.associate_var
15850 || (sym->ns->save_all && !sym->attr.automatic)
15851 || sym->ns->proc_name->attr.flavor == FL_MODULE
15852 || sym->ns->proc_name->attr.is_main_program
15853 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
15854 {
15855 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
15856 "nor a dummy argument", sym->name, &sym->declared_at);
15857 return;
15858 }
15859 /* F2008, C528. */
15860 else if (class_attr.codimension && !sym->attr.select_type_temporary
15861 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
15862 {
15863 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
15864 "deferred shape", sym->name, &sym->declared_at);
15865 return;
15866 }
15867 else if (class_attr.codimension && class_attr.allocatable && as
15868 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
15869 {
15870 gfc_error ("Allocatable coarray variable %qs at %L must have "
15871 "deferred shape", sym->name, &sym->declared_at);
15872 return;
15873 }
15874
15875 /* F2008, C541. */
15876 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15877 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15878 && sym->ts.u.derived && CLASS_DATA (sym)
15879 && CLASS_DATA (sym)->attr.coarray_comp))
15880 || (class_attr.codimension && class_attr.allocatable))
15881 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
15882 {
15883 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
15884 "allocatable coarray or have coarray components",
15885 sym->name, &sym->declared_at);
15886 return;
15887 }
15888
15889 if (class_attr.codimension && sym->attr.dummy
15890 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
15891 {
15892 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
15893 "procedure %qs", sym->name, &sym->declared_at,
15894 sym->ns->proc_name->name);
15895 return;
15896 }
15897
15898 if (sym->ts.type == BT_LOGICAL
15899 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
15900 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
15901 && sym->ns->proc_name->attr.is_bind_c)))
15902 {
15903 int i;
15904 for (i = 0; gfc_logical_kinds[i].kind; i++)
15905 if (gfc_logical_kinds[i].kind == sym->ts.kind)
15906 break;
15907 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
15908 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
15909 "%L with non-C_Bool kind in BIND(C) procedure "
15910 "%qs", sym->name, &sym->declared_at,
15911 sym->ns->proc_name->name))
15912 return;
15913 else if (!gfc_logical_kinds[i].c_bool
15914 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
15915 "%qs at %L with non-C_Bool kind in "
15916 "BIND(C) procedure %qs", sym->name,
15917 &sym->declared_at,
15918 sym->attr.function ? sym->name
15919 : sym->ns->proc_name->name))
15920 return;
15921 }
15922
15923 switch (sym->attr.flavor)
15924 {
15925 case FL_VARIABLE:
15926 if (!resolve_fl_variable (sym, mp_flag))
15927 return;
15928 break;
15929
15930 case FL_PROCEDURE:
15931 if (sym->formal && !sym->formal_ns)
15932 {
15933 /* Check that none of the arguments are a namelist. */
15934 gfc_formal_arglist *formal = sym->formal;
15935
15936 for (; formal; formal = formal->next)
15937 if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
15938 {
15939 gfc_error ("Namelist %qs cannot be an argument to "
15940 "subroutine or function at %L",
15941 formal->sym->name, &sym->declared_at);
15942 return;
15943 }
15944 }
15945
15946 if (!resolve_fl_procedure (sym, mp_flag))
15947 return;
15948 break;
15949
15950 case FL_NAMELIST:
15951 if (!resolve_fl_namelist (sym))
15952 return;
15953 break;
15954
15955 case FL_PARAMETER:
15956 if (!resolve_fl_parameter (sym))
15957 return;
15958 break;
15959
15960 default:
15961 break;
15962 }
15963
15964 /* Resolve array specifier. Check as well some constraints
15965 on COMMON blocks. */
15966
15967 check_constant = sym->attr.in_common && !sym->attr.pointer;
15968
15969 /* Set the formal_arg_flag so that check_conflict will not throw
15970 an error for host associated variables in the specification
15971 expression for an array_valued function. */
15972 if ((sym->attr.function || sym->attr.result) && sym->as)
15973 formal_arg_flag = true;
15974
15975 saved_specification_expr = specification_expr;
15976 specification_expr = true;
15977 gfc_resolve_array_spec (sym->as, check_constant);
15978 specification_expr = saved_specification_expr;
15979
15980 formal_arg_flag = false;
15981
15982 /* Resolve formal namespaces. */
15983 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
15984 && !sym->attr.contained && !sym->attr.intrinsic)
15985 gfc_resolve (sym->formal_ns);
15986
15987 /* Make sure the formal namespace is present. */
15988 if (sym->formal && !sym->formal_ns)
15989 {
15990 gfc_formal_arglist *formal = sym->formal;
15991 while (formal && !formal->sym)
15992 formal = formal->next;
15993
15994 if (formal)
15995 {
15996 sym->formal_ns = formal->sym->ns;
15997 if (sym->formal_ns && sym->ns != formal->sym->ns)
15998 sym->formal_ns->refs++;
15999 }
16000 }
16001
16002 /* Check threadprivate restrictions. */
16003 if (sym->attr.threadprivate && !sym->attr.save
16004 && !(sym->ns->save_all && !sym->attr.automatic)
16005 && (!sym->attr.in_common
16006 && sym->module == NULL
16007 && (sym->ns->proc_name == NULL
16008 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
16009 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
16010
16011 /* Check omp declare target restrictions. */
16012 if (sym->attr.omp_declare_target
16013 && sym->attr.flavor == FL_VARIABLE
16014 && !sym->attr.save
16015 && !(sym->ns->save_all && !sym->attr.automatic)
16016 && (!sym->attr.in_common
16017 && sym->module == NULL
16018 && (sym->ns->proc_name == NULL
16019 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
16020 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
16021 sym->name, &sym->declared_at);
16022
16023 /* If we have come this far we can apply default-initializers, as
16024 described in 14.7.5, to those variables that have not already
16025 been assigned one. */
16026 if (sym->ts.type == BT_DERIVED
16027 && !sym->value
16028 && !sym->attr.allocatable
16029 && !sym->attr.alloc_comp)
16030 {
16031 symbol_attribute *a = &sym->attr;
16032
16033 if ((!a->save && !a->dummy && !a->pointer
16034 && !a->in_common && !a->use_assoc
16035 && a->referenced
16036 && !((a->function || a->result)
16037 && (!a->dimension
16038 || sym->ts.u.derived->attr.alloc_comp
16039 || sym->ts.u.derived->attr.pointer_comp))
16040 && !(a->function && sym != sym->result))
16041 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
16042 apply_default_init (sym);
16043 else if (a->function && sym->result && a->access != ACCESS_PRIVATE
16044 && (sym->ts.u.derived->attr.alloc_comp
16045 || sym->ts.u.derived->attr.pointer_comp))
16046 /* Mark the result symbol to be referenced, when it has allocatable
16047 components. */
16048 sym->result->attr.referenced = 1;
16049 }
16050
16051 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
16052 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
16053 && !CLASS_DATA (sym)->attr.class_pointer
16054 && !CLASS_DATA (sym)->attr.allocatable)
16055 apply_default_init (sym);
16056
16057 /* If this symbol has a type-spec, check it. */
16058 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
16059 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
16060 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
16061 return;
16062
16063 if (sym->param_list)
16064 resolve_pdt (sym);
16065 }
16066
16067
16068 /************* Resolve DATA statements *************/
16069
16070 static struct
16071 {
16072 gfc_data_value *vnode;
16073 mpz_t left;
16074 }
16075 values;
16076
16077
16078 /* Advance the values structure to point to the next value in the data list. */
16079
16080 static bool
16081 next_data_value (void)
16082 {
16083 while (mpz_cmp_ui (values.left, 0) == 0)
16084 {
16085
16086 if (values.vnode->next == NULL)
16087 return false;
16088
16089 values.vnode = values.vnode->next;
16090 mpz_set (values.left, values.vnode->repeat);
16091 }
16092
16093 return true;
16094 }
16095
16096
16097 static bool
16098 check_data_variable (gfc_data_variable *var, locus *where)
16099 {
16100 gfc_expr *e;
16101 mpz_t size;
16102 mpz_t offset;
16103 bool t;
16104 ar_type mark = AR_UNKNOWN;
16105 int i;
16106 mpz_t section_index[GFC_MAX_DIMENSIONS];
16107 gfc_ref *ref;
16108 gfc_array_ref *ar;
16109 gfc_symbol *sym;
16110 int has_pointer;
16111
16112 if (!gfc_resolve_expr (var->expr))
16113 return false;
16114
16115 ar = NULL;
16116 mpz_init_set_si (offset, 0);
16117 e = var->expr;
16118
16119 if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
16120 && e->value.function.isym->id == GFC_ISYM_CAF_GET)
16121 e = e->value.function.actual->expr;
16122
16123 if (e->expr_type != EXPR_VARIABLE)
16124 {
16125 gfc_error ("Expecting definable entity near %L", where);
16126 return false;
16127 }
16128
16129 sym = e->symtree->n.sym;
16130
16131 if (sym->ns->is_block_data && !sym->attr.in_common)
16132 {
16133 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
16134 sym->name, &sym->declared_at);
16135 return false;
16136 }
16137
16138 if (e->ref == NULL && sym->as)
16139 {
16140 gfc_error ("DATA array %qs at %L must be specified in a previous"
16141 " declaration", sym->name, where);
16142 return false;
16143 }
16144
16145 if (gfc_is_coindexed (e))
16146 {
16147 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
16148 where);
16149 return false;
16150 }
16151
16152 has_pointer = sym->attr.pointer;
16153
16154 for (ref = e->ref; ref; ref = ref->next)
16155 {
16156 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
16157 has_pointer = 1;
16158
16159 if (has_pointer)
16160 {
16161 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
16162 {
16163 gfc_error ("DATA element %qs at %L is a pointer and so must "
16164 "be a full array", sym->name, where);
16165 return false;
16166 }
16167
16168 if (values.vnode->expr->expr_type == EXPR_CONSTANT)
16169 {
16170 gfc_error ("DATA object near %L has the pointer attribute "
16171 "and the corresponding DATA value is not a valid "
16172 "initial-data-target", where);
16173 return false;
16174 }
16175 }
16176
16177 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.allocatable)
16178 {
16179 gfc_error ("DATA element %qs at %L cannot have the ALLOCATABLE "
16180 "attribute", ref->u.c.component->name, &e->where);
16181 return false;
16182 }
16183 }
16184
16185 if (e->rank == 0 || has_pointer)
16186 {
16187 mpz_init_set_ui (size, 1);
16188 ref = NULL;
16189 }
16190 else
16191 {
16192 ref = e->ref;
16193
16194 /* Find the array section reference. */
16195 for (ref = e->ref; ref; ref = ref->next)
16196 {
16197 if (ref->type != REF_ARRAY)
16198 continue;
16199 if (ref->u.ar.type == AR_ELEMENT)
16200 continue;
16201 break;
16202 }
16203 gcc_assert (ref);
16204
16205 /* Set marks according to the reference pattern. */
16206 switch (ref->u.ar.type)
16207 {
16208 case AR_FULL:
16209 mark = AR_FULL;
16210 break;
16211
16212 case AR_SECTION:
16213 ar = &ref->u.ar;
16214 /* Get the start position of array section. */
16215 gfc_get_section_index (ar, section_index, &offset);
16216 mark = AR_SECTION;
16217 break;
16218
16219 default:
16220 gcc_unreachable ();
16221 }
16222
16223 if (!gfc_array_size (e, &size))
16224 {
16225 gfc_error ("Nonconstant array section at %L in DATA statement",
16226 where);
16227 mpz_clear (offset);
16228 return false;
16229 }
16230 }
16231
16232 t = true;
16233
16234 while (mpz_cmp_ui (size, 0) > 0)
16235 {
16236 if (!next_data_value ())
16237 {
16238 gfc_error ("DATA statement at %L has more variables than values",
16239 where);
16240 t = false;
16241 break;
16242 }
16243
16244 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
16245 if (!t)
16246 break;
16247
16248 /* If we have more than one element left in the repeat count,
16249 and we have more than one element left in the target variable,
16250 then create a range assignment. */
16251 /* FIXME: Only done for full arrays for now, since array sections
16252 seem tricky. */
16253 if (mark == AR_FULL && ref && ref->next == NULL
16254 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
16255 {
16256 mpz_t range;
16257
16258 if (mpz_cmp (size, values.left) >= 0)
16259 {
16260 mpz_init_set (range, values.left);
16261 mpz_sub (size, size, values.left);
16262 mpz_set_ui (values.left, 0);
16263 }
16264 else
16265 {
16266 mpz_init_set (range, size);
16267 mpz_sub (values.left, values.left, size);
16268 mpz_set_ui (size, 0);
16269 }
16270
16271 t = gfc_assign_data_value (var->expr, values.vnode->expr,
16272 offset, &range);
16273
16274 mpz_add (offset, offset, range);
16275 mpz_clear (range);
16276
16277 if (!t)
16278 break;
16279 }
16280
16281 /* Assign initial value to symbol. */
16282 else
16283 {
16284 mpz_sub_ui (values.left, values.left, 1);
16285 mpz_sub_ui (size, size, 1);
16286
16287 t = gfc_assign_data_value (var->expr, values.vnode->expr,
16288 offset, NULL);
16289 if (!t)
16290 break;
16291
16292 if (mark == AR_FULL)
16293 mpz_add_ui (offset, offset, 1);
16294
16295 /* Modify the array section indexes and recalculate the offset
16296 for next element. */
16297 else if (mark == AR_SECTION)
16298 gfc_advance_section (section_index, ar, &offset);
16299 }
16300 }
16301
16302 if (mark == AR_SECTION)
16303 {
16304 for (i = 0; i < ar->dimen; i++)
16305 mpz_clear (section_index[i]);
16306 }
16307
16308 mpz_clear (size);
16309 mpz_clear (offset);
16310
16311 return t;
16312 }
16313
16314
16315 static bool traverse_data_var (gfc_data_variable *, locus *);
16316
16317 /* Iterate over a list of elements in a DATA statement. */
16318
16319 static bool
16320 traverse_data_list (gfc_data_variable *var, locus *where)
16321 {
16322 mpz_t trip;
16323 iterator_stack frame;
16324 gfc_expr *e, *start, *end, *step;
16325 bool retval = true;
16326
16327 mpz_init (frame.value);
16328 mpz_init (trip);
16329
16330 start = gfc_copy_expr (var->iter.start);
16331 end = gfc_copy_expr (var->iter.end);
16332 step = gfc_copy_expr (var->iter.step);
16333
16334 if (!gfc_simplify_expr (start, 1)
16335 || start->expr_type != EXPR_CONSTANT)
16336 {
16337 gfc_error ("start of implied-do loop at %L could not be "
16338 "simplified to a constant value", &start->where);
16339 retval = false;
16340 goto cleanup;
16341 }
16342 if (!gfc_simplify_expr (end, 1)
16343 || end->expr_type != EXPR_CONSTANT)
16344 {
16345 gfc_error ("end of implied-do loop at %L could not be "
16346 "simplified to a constant value", &end->where);
16347 retval = false;
16348 goto cleanup;
16349 }
16350 if (!gfc_simplify_expr (step, 1)
16351 || step->expr_type != EXPR_CONSTANT)
16352 {
16353 gfc_error ("step of implied-do loop at %L could not be "
16354 "simplified to a constant value", &step->where);
16355 retval = false;
16356 goto cleanup;
16357 }
16358 if (mpz_cmp_si (step->value.integer, 0) == 0)
16359 {
16360 gfc_error ("step of implied-do loop at %L shall not be zero",
16361 &step->where);
16362 retval = false;
16363 goto cleanup;
16364 }
16365
16366 mpz_set (trip, end->value.integer);
16367 mpz_sub (trip, trip, start->value.integer);
16368 mpz_add (trip, trip, step->value.integer);
16369
16370 mpz_div (trip, trip, step->value.integer);
16371
16372 mpz_set (frame.value, start->value.integer);
16373
16374 frame.prev = iter_stack;
16375 frame.variable = var->iter.var->symtree;
16376 iter_stack = &frame;
16377
16378 while (mpz_cmp_ui (trip, 0) > 0)
16379 {
16380 if (!traverse_data_var (var->list, where))
16381 {
16382 retval = false;
16383 goto cleanup;
16384 }
16385
16386 e = gfc_copy_expr (var->expr);
16387 if (!gfc_simplify_expr (e, 1))
16388 {
16389 gfc_free_expr (e);
16390 retval = false;
16391 goto cleanup;
16392 }
16393
16394 mpz_add (frame.value, frame.value, step->value.integer);
16395
16396 mpz_sub_ui (trip, trip, 1);
16397 }
16398
16399 cleanup:
16400 mpz_clear (frame.value);
16401 mpz_clear (trip);
16402
16403 gfc_free_expr (start);
16404 gfc_free_expr (end);
16405 gfc_free_expr (step);
16406
16407 iter_stack = frame.prev;
16408 return retval;
16409 }
16410
16411
16412 /* Type resolve variables in the variable list of a DATA statement. */
16413
16414 static bool
16415 traverse_data_var (gfc_data_variable *var, locus *where)
16416 {
16417 bool t;
16418
16419 for (; var; var = var->next)
16420 {
16421 if (var->expr == NULL)
16422 t = traverse_data_list (var, where);
16423 else
16424 t = check_data_variable (var, where);
16425
16426 if (!t)
16427 return false;
16428 }
16429
16430 return true;
16431 }
16432
16433
16434 /* Resolve the expressions and iterators associated with a data statement.
16435 This is separate from the assignment checking because data lists should
16436 only be resolved once. */
16437
16438 static bool
16439 resolve_data_variables (gfc_data_variable *d)
16440 {
16441 for (; d; d = d->next)
16442 {
16443 if (d->list == NULL)
16444 {
16445 if (!gfc_resolve_expr (d->expr))
16446 return false;
16447 }
16448 else
16449 {
16450 if (!gfc_resolve_iterator (&d->iter, false, true))
16451 return false;
16452
16453 if (!resolve_data_variables (d->list))
16454 return false;
16455 }
16456 }
16457
16458 return true;
16459 }
16460
16461
16462 /* Resolve a single DATA statement. We implement this by storing a pointer to
16463 the value list into static variables, and then recursively traversing the
16464 variables list, expanding iterators and such. */
16465
16466 static void
16467 resolve_data (gfc_data *d)
16468 {
16469
16470 if (!resolve_data_variables (d->var))
16471 return;
16472
16473 values.vnode = d->value;
16474 if (d->value == NULL)
16475 mpz_set_ui (values.left, 0);
16476 else
16477 mpz_set (values.left, d->value->repeat);
16478
16479 if (!traverse_data_var (d->var, &d->where))
16480 return;
16481
16482 /* At this point, we better not have any values left. */
16483
16484 if (next_data_value ())
16485 gfc_error ("DATA statement at %L has more values than variables",
16486 &d->where);
16487 }
16488
16489
16490 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
16491 accessed by host or use association, is a dummy argument to a pure function,
16492 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
16493 is storage associated with any such variable, shall not be used in the
16494 following contexts: (clients of this function). */
16495
16496 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
16497 procedure. Returns zero if assignment is OK, nonzero if there is a
16498 problem. */
16499 int
16500 gfc_impure_variable (gfc_symbol *sym)
16501 {
16502 gfc_symbol *proc;
16503 gfc_namespace *ns;
16504
16505 if (sym->attr.use_assoc || sym->attr.in_common)
16506 return 1;
16507
16508 /* Check if the symbol's ns is inside the pure procedure. */
16509 for (ns = gfc_current_ns; ns; ns = ns->parent)
16510 {
16511 if (ns == sym->ns)
16512 break;
16513 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
16514 return 1;
16515 }
16516
16517 proc = sym->ns->proc_name;
16518 if (sym->attr.dummy
16519 && !sym->attr.value
16520 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
16521 || proc->attr.function))
16522 return 1;
16523
16524 /* TODO: Sort out what can be storage associated, if anything, and include
16525 it here. In principle equivalences should be scanned but it does not
16526 seem to be possible to storage associate an impure variable this way. */
16527 return 0;
16528 }
16529
16530
16531 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
16532 current namespace is inside a pure procedure. */
16533
16534 int
16535 gfc_pure (gfc_symbol *sym)
16536 {
16537 symbol_attribute attr;
16538 gfc_namespace *ns;
16539
16540 if (sym == NULL)
16541 {
16542 /* Check if the current namespace or one of its parents
16543 belongs to a pure procedure. */
16544 for (ns = gfc_current_ns; ns; ns = ns->parent)
16545 {
16546 sym = ns->proc_name;
16547 if (sym == NULL)
16548 return 0;
16549 attr = sym->attr;
16550 if (attr.flavor == FL_PROCEDURE && attr.pure)
16551 return 1;
16552 }
16553 return 0;
16554 }
16555
16556 attr = sym->attr;
16557
16558 return attr.flavor == FL_PROCEDURE && attr.pure;
16559 }
16560
16561
16562 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
16563 checks if the current namespace is implicitly pure. Note that this
16564 function returns false for a PURE procedure. */
16565
16566 int
16567 gfc_implicit_pure (gfc_symbol *sym)
16568 {
16569 gfc_namespace *ns;
16570
16571 if (sym == NULL)
16572 {
16573 /* Check if the current procedure is implicit_pure. Walk up
16574 the procedure list until we find a procedure. */
16575 for (ns = gfc_current_ns; ns; ns = ns->parent)
16576 {
16577 sym = ns->proc_name;
16578 if (sym == NULL)
16579 return 0;
16580
16581 if (sym->attr.flavor == FL_PROCEDURE)
16582 break;
16583 }
16584 }
16585
16586 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
16587 && !sym->attr.pure;
16588 }
16589
16590
16591 void
16592 gfc_unset_implicit_pure (gfc_symbol *sym)
16593 {
16594 gfc_namespace *ns;
16595
16596 if (sym == NULL)
16597 {
16598 /* Check if the current procedure is implicit_pure. Walk up
16599 the procedure list until we find a procedure. */
16600 for (ns = gfc_current_ns; ns; ns = ns->parent)
16601 {
16602 sym = ns->proc_name;
16603 if (sym == NULL)
16604 return;
16605
16606 if (sym->attr.flavor == FL_PROCEDURE)
16607 break;
16608 }
16609 }
16610
16611 if (sym->attr.flavor == FL_PROCEDURE)
16612 sym->attr.implicit_pure = 0;
16613 else
16614 sym->attr.pure = 0;
16615 }
16616
16617
16618 /* Test whether the current procedure is elemental or not. */
16619
16620 int
16621 gfc_elemental (gfc_symbol *sym)
16622 {
16623 symbol_attribute attr;
16624
16625 if (sym == NULL)
16626 sym = gfc_current_ns->proc_name;
16627 if (sym == NULL)
16628 return 0;
16629 attr = sym->attr;
16630
16631 return attr.flavor == FL_PROCEDURE && attr.elemental;
16632 }
16633
16634
16635 /* Warn about unused labels. */
16636
16637 static void
16638 warn_unused_fortran_label (gfc_st_label *label)
16639 {
16640 if (label == NULL)
16641 return;
16642
16643 warn_unused_fortran_label (label->left);
16644
16645 if (label->defined == ST_LABEL_UNKNOWN)
16646 return;
16647
16648 switch (label->referenced)
16649 {
16650 case ST_LABEL_UNKNOWN:
16651 gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
16652 label->value, &label->where);
16653 break;
16654
16655 case ST_LABEL_BAD_TARGET:
16656 gfc_warning (OPT_Wunused_label,
16657 "Label %d at %L defined but cannot be used",
16658 label->value, &label->where);
16659 break;
16660
16661 default:
16662 break;
16663 }
16664
16665 warn_unused_fortran_label (label->right);
16666 }
16667
16668
16669 /* Returns the sequence type of a symbol or sequence. */
16670
16671 static seq_type
16672 sequence_type (gfc_typespec ts)
16673 {
16674 seq_type result;
16675 gfc_component *c;
16676
16677 switch (ts.type)
16678 {
16679 case BT_DERIVED:
16680
16681 if (ts.u.derived->components == NULL)
16682 return SEQ_NONDEFAULT;
16683
16684 result = sequence_type (ts.u.derived->components->ts);
16685 for (c = ts.u.derived->components->next; c; c = c->next)
16686 if (sequence_type (c->ts) != result)
16687 return SEQ_MIXED;
16688
16689 return result;
16690
16691 case BT_CHARACTER:
16692 if (ts.kind != gfc_default_character_kind)
16693 return SEQ_NONDEFAULT;
16694
16695 return SEQ_CHARACTER;
16696
16697 case BT_INTEGER:
16698 if (ts.kind != gfc_default_integer_kind)
16699 return SEQ_NONDEFAULT;
16700
16701 return SEQ_NUMERIC;
16702
16703 case BT_REAL:
16704 if (!(ts.kind == gfc_default_real_kind
16705 || ts.kind == gfc_default_double_kind))
16706 return SEQ_NONDEFAULT;
16707
16708 return SEQ_NUMERIC;
16709
16710 case BT_COMPLEX:
16711 if (ts.kind != gfc_default_complex_kind)
16712 return SEQ_NONDEFAULT;
16713
16714 return SEQ_NUMERIC;
16715
16716 case BT_LOGICAL:
16717 if (ts.kind != gfc_default_logical_kind)
16718 return SEQ_NONDEFAULT;
16719
16720 return SEQ_NUMERIC;
16721
16722 default:
16723 return SEQ_NONDEFAULT;
16724 }
16725 }
16726
16727
16728 /* Resolve derived type EQUIVALENCE object. */
16729
16730 static bool
16731 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
16732 {
16733 gfc_component *c = derived->components;
16734
16735 if (!derived)
16736 return true;
16737
16738 /* Shall not be an object of nonsequence derived type. */
16739 if (!derived->attr.sequence)
16740 {
16741 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
16742 "attribute to be an EQUIVALENCE object", sym->name,
16743 &e->where);
16744 return false;
16745 }
16746
16747 /* Shall not have allocatable components. */
16748 if (derived->attr.alloc_comp)
16749 {
16750 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
16751 "components to be an EQUIVALENCE object",sym->name,
16752 &e->where);
16753 return false;
16754 }
16755
16756 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
16757 {
16758 gfc_error ("Derived type variable %qs at %L with default "
16759 "initialization cannot be in EQUIVALENCE with a variable "
16760 "in COMMON", sym->name, &e->where);
16761 return false;
16762 }
16763
16764 for (; c ; c = c->next)
16765 {
16766 if (gfc_bt_struct (c->ts.type)
16767 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
16768 return false;
16769
16770 /* Shall not be an object of sequence derived type containing a pointer
16771 in the structure. */
16772 if (c->attr.pointer)
16773 {
16774 gfc_error ("Derived type variable %qs at %L with pointer "
16775 "component(s) cannot be an EQUIVALENCE object",
16776 sym->name, &e->where);
16777 return false;
16778 }
16779 }
16780 return true;
16781 }
16782
16783
16784 /* Resolve equivalence object.
16785 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
16786 an allocatable array, an object of nonsequence derived type, an object of
16787 sequence derived type containing a pointer at any level of component
16788 selection, an automatic object, a function name, an entry name, a result
16789 name, a named constant, a structure component, or a subobject of any of
16790 the preceding objects. A substring shall not have length zero. A
16791 derived type shall not have components with default initialization nor
16792 shall two objects of an equivalence group be initialized.
16793 Either all or none of the objects shall have an protected attribute.
16794 The simple constraints are done in symbol.c(check_conflict) and the rest
16795 are implemented here. */
16796
16797 static void
16798 resolve_equivalence (gfc_equiv *eq)
16799 {
16800 gfc_symbol *sym;
16801 gfc_symbol *first_sym;
16802 gfc_expr *e;
16803 gfc_ref *r;
16804 locus *last_where = NULL;
16805 seq_type eq_type, last_eq_type;
16806 gfc_typespec *last_ts;
16807 int object, cnt_protected;
16808 const char *msg;
16809
16810 last_ts = &eq->expr->symtree->n.sym->ts;
16811
16812 first_sym = eq->expr->symtree->n.sym;
16813
16814 cnt_protected = 0;
16815
16816 for (object = 1; eq; eq = eq->eq, object++)
16817 {
16818 e = eq->expr;
16819
16820 e->ts = e->symtree->n.sym->ts;
16821 /* match_varspec might not know yet if it is seeing
16822 array reference or substring reference, as it doesn't
16823 know the types. */
16824 if (e->ref && e->ref->type == REF_ARRAY)
16825 {
16826 gfc_ref *ref = e->ref;
16827 sym = e->symtree->n.sym;
16828
16829 if (sym->attr.dimension)
16830 {
16831 ref->u.ar.as = sym->as;
16832 ref = ref->next;
16833 }
16834
16835 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
16836 if (e->ts.type == BT_CHARACTER
16837 && ref
16838 && ref->type == REF_ARRAY
16839 && ref->u.ar.dimen == 1
16840 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
16841 && ref->u.ar.stride[0] == NULL)
16842 {
16843 gfc_expr *start = ref->u.ar.start[0];
16844 gfc_expr *end = ref->u.ar.end[0];
16845 void *mem = NULL;
16846
16847 /* Optimize away the (:) reference. */
16848 if (start == NULL && end == NULL)
16849 {
16850 if (e->ref == ref)
16851 e->ref = ref->next;
16852 else
16853 e->ref->next = ref->next;
16854 mem = ref;
16855 }
16856 else
16857 {
16858 ref->type = REF_SUBSTRING;
16859 if (start == NULL)
16860 start = gfc_get_int_expr (gfc_charlen_int_kind,
16861 NULL, 1);
16862 ref->u.ss.start = start;
16863 if (end == NULL && e->ts.u.cl)
16864 end = gfc_copy_expr (e->ts.u.cl->length);
16865 ref->u.ss.end = end;
16866 ref->u.ss.length = e->ts.u.cl;
16867 e->ts.u.cl = NULL;
16868 }
16869 ref = ref->next;
16870 free (mem);
16871 }
16872
16873 /* Any further ref is an error. */
16874 if (ref)
16875 {
16876 gcc_assert (ref->type == REF_ARRAY);
16877 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
16878 &ref->u.ar.where);
16879 continue;
16880 }
16881 }
16882
16883 if (!gfc_resolve_expr (e))
16884 continue;
16885
16886 sym = e->symtree->n.sym;
16887
16888 if (sym->attr.is_protected)
16889 cnt_protected++;
16890 if (cnt_protected > 0 && cnt_protected != object)
16891 {
16892 gfc_error ("Either all or none of the objects in the "
16893 "EQUIVALENCE set at %L shall have the "
16894 "PROTECTED attribute",
16895 &e->where);
16896 break;
16897 }
16898
16899 /* Shall not equivalence common block variables in a PURE procedure. */
16900 if (sym->ns->proc_name
16901 && sym->ns->proc_name->attr.pure
16902 && sym->attr.in_common)
16903 {
16904 /* Need to check for symbols that may have entered the pure
16905 procedure via a USE statement. */
16906 bool saw_sym = false;
16907 if (sym->ns->use_stmts)
16908 {
16909 gfc_use_rename *r;
16910 for (r = sym->ns->use_stmts->rename; r; r = r->next)
16911 if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
16912 }
16913 else
16914 saw_sym = true;
16915
16916 if (saw_sym)
16917 gfc_error ("COMMON block member %qs at %L cannot be an "
16918 "EQUIVALENCE object in the pure procedure %qs",
16919 sym->name, &e->where, sym->ns->proc_name->name);
16920 break;
16921 }
16922
16923 /* Shall not be a named constant. */
16924 if (e->expr_type == EXPR_CONSTANT)
16925 {
16926 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
16927 "object", sym->name, &e->where);
16928 continue;
16929 }
16930
16931 if (e->ts.type == BT_DERIVED
16932 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
16933 continue;
16934
16935 /* Check that the types correspond correctly:
16936 Note 5.28:
16937 A numeric sequence structure may be equivalenced to another sequence
16938 structure, an object of default integer type, default real type, double
16939 precision real type, default logical type such that components of the
16940 structure ultimately only become associated to objects of the same
16941 kind. A character sequence structure may be equivalenced to an object
16942 of default character kind or another character sequence structure.
16943 Other objects may be equivalenced only to objects of the same type and
16944 kind parameters. */
16945
16946 /* Identical types are unconditionally OK. */
16947 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
16948 goto identical_types;
16949
16950 last_eq_type = sequence_type (*last_ts);
16951 eq_type = sequence_type (sym->ts);
16952
16953 /* Since the pair of objects is not of the same type, mixed or
16954 non-default sequences can be rejected. */
16955
16956 msg = "Sequence %s with mixed components in EQUIVALENCE "
16957 "statement at %L with different type objects";
16958 if ((object ==2
16959 && last_eq_type == SEQ_MIXED
16960 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
16961 || (eq_type == SEQ_MIXED
16962 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
16963 continue;
16964
16965 msg = "Non-default type object or sequence %s in EQUIVALENCE "
16966 "statement at %L with objects of different type";
16967 if ((object ==2
16968 && last_eq_type == SEQ_NONDEFAULT
16969 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
16970 || (eq_type == SEQ_NONDEFAULT
16971 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
16972 continue;
16973
16974 msg ="Non-CHARACTER object %qs in default CHARACTER "
16975 "EQUIVALENCE statement at %L";
16976 if (last_eq_type == SEQ_CHARACTER
16977 && eq_type != SEQ_CHARACTER
16978 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
16979 continue;
16980
16981 msg ="Non-NUMERIC object %qs in default NUMERIC "
16982 "EQUIVALENCE statement at %L";
16983 if (last_eq_type == SEQ_NUMERIC
16984 && eq_type != SEQ_NUMERIC
16985 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
16986 continue;
16987
16988 identical_types:
16989
16990 last_ts =&sym->ts;
16991 last_where = &e->where;
16992
16993 if (!e->ref)
16994 continue;
16995
16996 /* Shall not be an automatic array. */
16997 if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
16998 {
16999 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
17000 "an EQUIVALENCE object", sym->name, &e->where);
17001 continue;
17002 }
17003
17004 r = e->ref;
17005 while (r)
17006 {
17007 /* Shall not be a structure component. */
17008 if (r->type == REF_COMPONENT)
17009 {
17010 gfc_error ("Structure component %qs at %L cannot be an "
17011 "EQUIVALENCE object",
17012 r->u.c.component->name, &e->where);
17013 break;
17014 }
17015
17016 /* A substring shall not have length zero. */
17017 if (r->type == REF_SUBSTRING)
17018 {
17019 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
17020 {
17021 gfc_error ("Substring at %L has length zero",
17022 &r->u.ss.start->where);
17023 break;
17024 }
17025 }
17026 r = r->next;
17027 }
17028 }
17029 }
17030
17031
17032 /* Function called by resolve_fntype to flag other symbols used in the
17033 length type parameter specification of function results. */
17034
17035 static bool
17036 flag_fn_result_spec (gfc_expr *expr,
17037 gfc_symbol *sym,
17038 int *f ATTRIBUTE_UNUSED)
17039 {
17040 gfc_namespace *ns;
17041 gfc_symbol *s;
17042
17043 if (expr->expr_type == EXPR_VARIABLE)
17044 {
17045 s = expr->symtree->n.sym;
17046 for (ns = s->ns; ns; ns = ns->parent)
17047 if (!ns->parent)
17048 break;
17049
17050 if (sym == s)
17051 {
17052 gfc_error ("Self reference in character length expression "
17053 "for %qs at %L", sym->name, &expr->where);
17054 return true;
17055 }
17056
17057 if (!s->fn_result_spec
17058 && s->attr.flavor == FL_PARAMETER)
17059 {
17060 /* Function contained in a module.... */
17061 if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
17062 {
17063 gfc_symtree *st;
17064 s->fn_result_spec = 1;
17065 /* Make sure that this symbol is translated as a module
17066 variable. */
17067 st = gfc_get_unique_symtree (ns);
17068 st->n.sym = s;
17069 s->refs++;
17070 }
17071 /* ... which is use associated and called. */
17072 else if (s->attr.use_assoc || s->attr.used_in_submodule
17073 ||
17074 /* External function matched with an interface. */
17075 (s->ns->proc_name
17076 && ((s->ns == ns
17077 && s->ns->proc_name->attr.if_source == IFSRC_DECL)
17078 || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
17079 && s->ns->proc_name->attr.function))
17080 s->fn_result_spec = 1;
17081 }
17082 }
17083 return false;
17084 }
17085
17086
17087 /* Resolve function and ENTRY types, issue diagnostics if needed. */
17088
17089 static void
17090 resolve_fntype (gfc_namespace *ns)
17091 {
17092 gfc_entry_list *el;
17093 gfc_symbol *sym;
17094
17095 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
17096 return;
17097
17098 /* If there are any entries, ns->proc_name is the entry master
17099 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
17100 if (ns->entries)
17101 sym = ns->entries->sym;
17102 else
17103 sym = ns->proc_name;
17104 if (sym->result == sym
17105 && sym->ts.type == BT_UNKNOWN
17106 && !gfc_set_default_type (sym, 0, NULL)
17107 && !sym->attr.untyped)
17108 {
17109 gfc_error ("Function %qs at %L has no IMPLICIT type",
17110 sym->name, &sym->declared_at);
17111 sym->attr.untyped = 1;
17112 }
17113
17114 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
17115 && !sym->attr.contained
17116 && !gfc_check_symbol_access (sym->ts.u.derived)
17117 && gfc_check_symbol_access (sym))
17118 {
17119 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
17120 "%L of PRIVATE type %qs", sym->name,
17121 &sym->declared_at, sym->ts.u.derived->name);
17122 }
17123
17124 if (ns->entries)
17125 for (el = ns->entries->next; el; el = el->next)
17126 {
17127 if (el->sym->result == el->sym
17128 && el->sym->ts.type == BT_UNKNOWN
17129 && !gfc_set_default_type (el->sym, 0, NULL)
17130 && !el->sym->attr.untyped)
17131 {
17132 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
17133 el->sym->name, &el->sym->declared_at);
17134 el->sym->attr.untyped = 1;
17135 }
17136 }
17137
17138 if (sym->ts.type == BT_CHARACTER)
17139 gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
17140 }
17141
17142
17143 /* 12.3.2.1.1 Defined operators. */
17144
17145 static bool
17146 check_uop_procedure (gfc_symbol *sym, locus where)
17147 {
17148 gfc_formal_arglist *formal;
17149
17150 if (!sym->attr.function)
17151 {
17152 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
17153 sym->name, &where);
17154 return false;
17155 }
17156
17157 if (sym->ts.type == BT_CHARACTER
17158 && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
17159 && !(sym->result && ((sym->result->ts.u.cl
17160 && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
17161 {
17162 gfc_error ("User operator procedure %qs at %L cannot be assumed "
17163 "character length", sym->name, &where);
17164 return false;
17165 }
17166
17167 formal = gfc_sym_get_dummy_args (sym);
17168 if (!formal || !formal->sym)
17169 {
17170 gfc_error ("User operator procedure %qs at %L must have at least "
17171 "one argument", sym->name, &where);
17172 return false;
17173 }
17174
17175 if (formal->sym->attr.intent != INTENT_IN)
17176 {
17177 gfc_error ("First argument of operator interface at %L must be "
17178 "INTENT(IN)", &where);
17179 return false;
17180 }
17181
17182 if (formal->sym->attr.optional)
17183 {
17184 gfc_error ("First argument of operator interface at %L cannot be "
17185 "optional", &where);
17186 return false;
17187 }
17188
17189 formal = formal->next;
17190 if (!formal || !formal->sym)
17191 return true;
17192
17193 if (formal->sym->attr.intent != INTENT_IN)
17194 {
17195 gfc_error ("Second argument of operator interface at %L must be "
17196 "INTENT(IN)", &where);
17197 return false;
17198 }
17199
17200 if (formal->sym->attr.optional)
17201 {
17202 gfc_error ("Second argument of operator interface at %L cannot be "
17203 "optional", &where);
17204 return false;
17205 }
17206
17207 if (formal->next)
17208 {
17209 gfc_error ("Operator interface at %L must have, at most, two "
17210 "arguments", &where);
17211 return false;
17212 }
17213
17214 return true;
17215 }
17216
17217 static void
17218 gfc_resolve_uops (gfc_symtree *symtree)
17219 {
17220 gfc_interface *itr;
17221
17222 if (symtree == NULL)
17223 return;
17224
17225 gfc_resolve_uops (symtree->left);
17226 gfc_resolve_uops (symtree->right);
17227
17228 for (itr = symtree->n.uop->op; itr; itr = itr->next)
17229 check_uop_procedure (itr->sym, itr->sym->declared_at);
17230 }
17231
17232
17233 /* Examine all of the expressions associated with a program unit,
17234 assign types to all intermediate expressions, make sure that all
17235 assignments are to compatible types and figure out which names
17236 refer to which functions or subroutines. It doesn't check code
17237 block, which is handled by gfc_resolve_code. */
17238
17239 static void
17240 resolve_types (gfc_namespace *ns)
17241 {
17242 gfc_namespace *n;
17243 gfc_charlen *cl;
17244 gfc_data *d;
17245 gfc_equiv *eq;
17246 gfc_namespace* old_ns = gfc_current_ns;
17247 bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
17248
17249 if (ns->types_resolved)
17250 return;
17251
17252 /* Check that all IMPLICIT types are ok. */
17253 if (!ns->seen_implicit_none)
17254 {
17255 unsigned letter;
17256 for (letter = 0; letter != GFC_LETTERS; ++letter)
17257 if (ns->set_flag[letter]
17258 && !resolve_typespec_used (&ns->default_type[letter],
17259 &ns->implicit_loc[letter], NULL))
17260 return;
17261 }
17262
17263 gfc_current_ns = ns;
17264
17265 resolve_entries (ns);
17266
17267 resolve_common_vars (&ns->blank_common, false);
17268 resolve_common_blocks (ns->common_root);
17269
17270 resolve_contained_functions (ns);
17271
17272 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
17273 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
17274 gfc_resolve_formal_arglist (ns->proc_name);
17275
17276 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
17277
17278 for (cl = ns->cl_list; cl; cl = cl->next)
17279 resolve_charlen (cl);
17280
17281 gfc_traverse_ns (ns, resolve_symbol);
17282
17283 resolve_fntype (ns);
17284
17285 for (n = ns->contained; n; n = n->sibling)
17286 {
17287 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
17288 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
17289 "also be PURE", n->proc_name->name,
17290 &n->proc_name->declared_at);
17291
17292 resolve_types (n);
17293 }
17294
17295 forall_flag = 0;
17296 gfc_do_concurrent_flag = 0;
17297 gfc_check_interfaces (ns);
17298
17299 gfc_traverse_ns (ns, resolve_values);
17300
17301 if (ns->save_all || (!flag_automatic && !recursive))
17302 gfc_save_all (ns);
17303
17304 iter_stack = NULL;
17305 for (d = ns->data; d; d = d->next)
17306 resolve_data (d);
17307
17308 iter_stack = NULL;
17309 gfc_traverse_ns (ns, gfc_formalize_init_value);
17310
17311 gfc_traverse_ns (ns, gfc_verify_binding_labels);
17312
17313 for (eq = ns->equiv; eq; eq = eq->next)
17314 resolve_equivalence (eq);
17315
17316 /* Warn about unused labels. */
17317 if (warn_unused_label)
17318 warn_unused_fortran_label (ns->st_labels);
17319
17320 gfc_resolve_uops (ns->uop_root);
17321
17322 gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
17323
17324 gfc_resolve_omp_declare_simd (ns);
17325
17326 gfc_resolve_omp_udrs (ns->omp_udr_root);
17327
17328 ns->types_resolved = 1;
17329
17330 gfc_current_ns = old_ns;
17331 }
17332
17333
17334 /* Call gfc_resolve_code recursively. */
17335
17336 static void
17337 resolve_codes (gfc_namespace *ns)
17338 {
17339 gfc_namespace *n;
17340 bitmap_obstack old_obstack;
17341
17342 if (ns->resolved == 1)
17343 return;
17344
17345 for (n = ns->contained; n; n = n->sibling)
17346 resolve_codes (n);
17347
17348 gfc_current_ns = ns;
17349
17350 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
17351 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
17352 cs_base = NULL;
17353
17354 /* Set to an out of range value. */
17355 current_entry_id = -1;
17356
17357 old_obstack = labels_obstack;
17358 bitmap_obstack_initialize (&labels_obstack);
17359
17360 gfc_resolve_oacc_declare (ns);
17361 gfc_resolve_oacc_routines (ns);
17362 gfc_resolve_omp_local_vars (ns);
17363 gfc_resolve_code (ns->code, ns);
17364
17365 bitmap_obstack_release (&labels_obstack);
17366 labels_obstack = old_obstack;
17367 }
17368
17369
17370 /* This function is called after a complete program unit has been compiled.
17371 Its purpose is to examine all of the expressions associated with a program
17372 unit, assign types to all intermediate expressions, make sure that all
17373 assignments are to compatible types and figure out which names refer to
17374 which functions or subroutines. */
17375
17376 void
17377 gfc_resolve (gfc_namespace *ns)
17378 {
17379 gfc_namespace *old_ns;
17380 code_stack *old_cs_base;
17381 struct gfc_omp_saved_state old_omp_state;
17382
17383 if (ns->resolved)
17384 return;
17385
17386 ns->resolved = -1;
17387 old_ns = gfc_current_ns;
17388 old_cs_base = cs_base;
17389
17390 /* As gfc_resolve can be called during resolution of an OpenMP construct
17391 body, we should clear any state associated to it, so that say NS's
17392 DO loops are not interpreted as OpenMP loops. */
17393 if (!ns->construct_entities)
17394 gfc_omp_save_and_clear_state (&old_omp_state);
17395
17396 resolve_types (ns);
17397 component_assignment_level = 0;
17398 resolve_codes (ns);
17399
17400 gfc_current_ns = old_ns;
17401 cs_base = old_cs_base;
17402 ns->resolved = 1;
17403
17404 gfc_run_passes (ns);
17405
17406 if (!ns->construct_entities)
17407 gfc_omp_restore_state (&old_omp_state);
17408 }