PR fortran/95980 - ICE on using sync images with -fcheck=bounds
[gcc.git] / gcc / fortran / resolve.c
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2020 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "bitmap.h"
26 #include "gfortran.h"
27 #include "arith.h" /* For gfc_compare_expr(). */
28 #include "dependency.h"
29 #include "data.h"
30 #include "target-memory.h" /* for gfc_simplify_transfer */
31 #include "constructor.h"
32
33 /* Types used in equivalence statements. */
34
35 enum seq_type
36 {
37 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
38 };
39
40 /* Stack to keep track of the nesting of blocks as we move through the
41 code. See resolve_branch() and gfc_resolve_code(). */
42
43 typedef struct code_stack
44 {
45 struct gfc_code *head, *current;
46 struct code_stack *prev;
47
48 /* This bitmap keeps track of the targets valid for a branch from
49 inside this block except for END {IF|SELECT}s of enclosing
50 blocks. */
51 bitmap reachable_labels;
52 }
53 code_stack;
54
55 static code_stack *cs_base = NULL;
56
57
58 /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
59
60 static int forall_flag;
61 int gfc_do_concurrent_flag;
62
63 /* True when we are resolving an expression that is an actual argument to
64 a procedure. */
65 static bool actual_arg = false;
66 /* True when we are resolving an expression that is the first actual argument
67 to a procedure. */
68 static bool first_actual_arg = false;
69
70
71 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
72
73 static int omp_workshare_flag;
74
75 /* True if we are processing a formal arglist. The corresponding function
76 resets the flag each time that it is read. */
77 static bool formal_arg_flag = false;
78
79 /* True if we are resolving a specification expression. */
80 static bool specification_expr = false;
81
82 /* The id of the last entry seen. */
83 static int current_entry_id;
84
85 /* We use bitmaps to determine if a branch target is valid. */
86 static bitmap_obstack labels_obstack;
87
88 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
89 static bool inquiry_argument = false;
90
91
92 bool
93 gfc_is_formal_arg (void)
94 {
95 return formal_arg_flag;
96 }
97
98 /* Is the symbol host associated? */
99 static bool
100 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
101 {
102 for (ns = ns->parent; ns; ns = ns->parent)
103 {
104 if (sym->ns == ns)
105 return true;
106 }
107
108 return false;
109 }
110
111 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
112 an ABSTRACT derived-type. If where is not NULL, an error message with that
113 locus is printed, optionally using name. */
114
115 static bool
116 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
117 {
118 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
119 {
120 if (where)
121 {
122 if (name)
123 gfc_error ("%qs at %L is of the ABSTRACT type %qs",
124 name, where, ts->u.derived->name);
125 else
126 gfc_error ("ABSTRACT type %qs used at %L",
127 ts->u.derived->name, where);
128 }
129
130 return false;
131 }
132
133 return true;
134 }
135
136
137 static bool
138 check_proc_interface (gfc_symbol *ifc, locus *where)
139 {
140 /* Several checks for F08:C1216. */
141 if (ifc->attr.procedure)
142 {
143 gfc_error ("Interface %qs at %L is declared "
144 "in a later PROCEDURE statement", ifc->name, where);
145 return false;
146 }
147 if (ifc->generic)
148 {
149 /* For generic interfaces, check if there is
150 a specific procedure with the same name. */
151 gfc_interface *gen = ifc->generic;
152 while (gen && strcmp (gen->sym->name, ifc->name) != 0)
153 gen = gen->next;
154 if (!gen)
155 {
156 gfc_error ("Interface %qs at %L may not be generic",
157 ifc->name, where);
158 return false;
159 }
160 }
161 if (ifc->attr.proc == PROC_ST_FUNCTION)
162 {
163 gfc_error ("Interface %qs at %L may not be a statement function",
164 ifc->name, where);
165 return false;
166 }
167 if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
168 || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
169 ifc->attr.intrinsic = 1;
170 if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
171 {
172 gfc_error ("Intrinsic procedure %qs not allowed in "
173 "PROCEDURE statement at %L", ifc->name, where);
174 return false;
175 }
176 if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
177 {
178 gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
179 return false;
180 }
181 return true;
182 }
183
184
185 static void resolve_symbol (gfc_symbol *sym);
186
187
188 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
189
190 static bool
191 resolve_procedure_interface (gfc_symbol *sym)
192 {
193 gfc_symbol *ifc = sym->ts.interface;
194
195 if (!ifc)
196 return true;
197
198 if (ifc == sym)
199 {
200 gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
201 sym->name, &sym->declared_at);
202 return false;
203 }
204 if (!check_proc_interface (ifc, &sym->declared_at))
205 return false;
206
207 if (ifc->attr.if_source || ifc->attr.intrinsic)
208 {
209 /* Resolve interface and copy attributes. */
210 resolve_symbol (ifc);
211 if (ifc->attr.intrinsic)
212 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
213
214 if (ifc->result)
215 {
216 sym->ts = ifc->result->ts;
217 sym->attr.allocatable = ifc->result->attr.allocatable;
218 sym->attr.pointer = ifc->result->attr.pointer;
219 sym->attr.dimension = ifc->result->attr.dimension;
220 sym->attr.class_ok = ifc->result->attr.class_ok;
221 sym->as = gfc_copy_array_spec (ifc->result->as);
222 sym->result = sym;
223 }
224 else
225 {
226 sym->ts = ifc->ts;
227 sym->attr.allocatable = ifc->attr.allocatable;
228 sym->attr.pointer = ifc->attr.pointer;
229 sym->attr.dimension = ifc->attr.dimension;
230 sym->attr.class_ok = ifc->attr.class_ok;
231 sym->as = gfc_copy_array_spec (ifc->as);
232 }
233 sym->ts.interface = ifc;
234 sym->attr.function = ifc->attr.function;
235 sym->attr.subroutine = ifc->attr.subroutine;
236
237 sym->attr.pure = ifc->attr.pure;
238 sym->attr.elemental = ifc->attr.elemental;
239 sym->attr.contiguous = ifc->attr.contiguous;
240 sym->attr.recursive = ifc->attr.recursive;
241 sym->attr.always_explicit = ifc->attr.always_explicit;
242 sym->attr.ext_attr |= ifc->attr.ext_attr;
243 sym->attr.is_bind_c = ifc->attr.is_bind_c;
244 /* Copy char length. */
245 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
246 {
247 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
248 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
249 && !gfc_resolve_expr (sym->ts.u.cl->length))
250 return false;
251 }
252 }
253
254 return true;
255 }
256
257
258 /* Resolve types of formal argument lists. These have to be done early so that
259 the formal argument lists of module procedures can be copied to the
260 containing module before the individual procedures are resolved
261 individually. We also resolve argument lists of procedures in interface
262 blocks because they are self-contained scoping units.
263
264 Since a dummy argument cannot be a non-dummy procedure, the only
265 resort left for untyped names are the IMPLICIT types. */
266
267 void
268 gfc_resolve_formal_arglist (gfc_symbol *proc)
269 {
270 gfc_formal_arglist *f;
271 gfc_symbol *sym;
272 bool saved_specification_expr;
273 int i;
274
275 if (proc->result != NULL)
276 sym = proc->result;
277 else
278 sym = proc;
279
280 if (gfc_elemental (proc)
281 || sym->attr.pointer || sym->attr.allocatable
282 || (sym->as && sym->as->rank != 0))
283 {
284 proc->attr.always_explicit = 1;
285 sym->attr.always_explicit = 1;
286 }
287
288 formal_arg_flag = true;
289
290 for (f = proc->formal; f; f = f->next)
291 {
292 gfc_array_spec *as;
293
294 sym = f->sym;
295
296 if (sym == NULL)
297 {
298 /* Alternate return placeholder. */
299 if (gfc_elemental (proc))
300 gfc_error ("Alternate return specifier in elemental subroutine "
301 "%qs at %L is not allowed", proc->name,
302 &proc->declared_at);
303 if (proc->attr.function)
304 gfc_error ("Alternate return specifier in function "
305 "%qs at %L is not allowed", proc->name,
306 &proc->declared_at);
307 continue;
308 }
309 else if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
310 && !resolve_procedure_interface (sym))
311 return;
312
313 if (strcmp (proc->name, sym->name) == 0)
314 {
315 gfc_error ("Self-referential argument "
316 "%qs at %L is not allowed", sym->name,
317 &proc->declared_at);
318 return;
319 }
320
321 if (sym->attr.if_source != IFSRC_UNKNOWN)
322 gfc_resolve_formal_arglist (sym);
323
324 if (sym->attr.subroutine || sym->attr.external)
325 {
326 if (sym->attr.flavor == FL_UNKNOWN)
327 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
328 }
329 else
330 {
331 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
332 && (!sym->attr.function || sym->result == sym))
333 gfc_set_default_type (sym, 1, sym->ns);
334 }
335
336 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
337 ? CLASS_DATA (sym)->as : sym->as;
338
339 saved_specification_expr = specification_expr;
340 specification_expr = true;
341 gfc_resolve_array_spec (as, 0);
342 specification_expr = saved_specification_expr;
343
344 /* We can't tell if an array with dimension (:) is assumed or deferred
345 shape until we know if it has the pointer or allocatable attributes.
346 */
347 if (as && as->rank > 0 && as->type == AS_DEFERRED
348 && ((sym->ts.type != BT_CLASS
349 && !(sym->attr.pointer || sym->attr.allocatable))
350 || (sym->ts.type == BT_CLASS
351 && !(CLASS_DATA (sym)->attr.class_pointer
352 || CLASS_DATA (sym)->attr.allocatable)))
353 && sym->attr.flavor != FL_PROCEDURE)
354 {
355 as->type = AS_ASSUMED_SHAPE;
356 for (i = 0; i < as->rank; i++)
357 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
358 }
359
360 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
361 || (as && as->type == AS_ASSUMED_RANK)
362 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
363 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
364 && (CLASS_DATA (sym)->attr.class_pointer
365 || CLASS_DATA (sym)->attr.allocatable
366 || CLASS_DATA (sym)->attr.target))
367 || sym->attr.optional)
368 {
369 proc->attr.always_explicit = 1;
370 if (proc->result)
371 proc->result->attr.always_explicit = 1;
372 }
373
374 /* If the flavor is unknown at this point, it has to be a variable.
375 A procedure specification would have already set the type. */
376
377 if (sym->attr.flavor == FL_UNKNOWN)
378 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
379
380 if (gfc_pure (proc))
381 {
382 if (sym->attr.flavor == FL_PROCEDURE)
383 {
384 /* F08:C1279. */
385 if (!gfc_pure (sym))
386 {
387 gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
388 "also be PURE", sym->name, &sym->declared_at);
389 continue;
390 }
391 }
392 else if (!sym->attr.pointer)
393 {
394 if (proc->attr.function && sym->attr.intent != INTENT_IN)
395 {
396 if (sym->attr.value)
397 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
398 " of pure function %qs at %L with VALUE "
399 "attribute but without INTENT(IN)",
400 sym->name, proc->name, &sym->declared_at);
401 else
402 gfc_error ("Argument %qs of pure function %qs at %L must "
403 "be INTENT(IN) or VALUE", sym->name, proc->name,
404 &sym->declared_at);
405 }
406
407 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
408 {
409 if (sym->attr.value)
410 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
411 " of pure subroutine %qs at %L with VALUE "
412 "attribute but without INTENT", sym->name,
413 proc->name, &sym->declared_at);
414 else
415 gfc_error ("Argument %qs of pure subroutine %qs at %L "
416 "must have its INTENT specified or have the "
417 "VALUE attribute", sym->name, proc->name,
418 &sym->declared_at);
419 }
420 }
421
422 /* F08:C1278a. */
423 if (sym->ts.type == BT_CLASS && sym->attr.intent == INTENT_OUT)
424 {
425 gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L"
426 " may not be polymorphic", sym->name, proc->name,
427 &sym->declared_at);
428 continue;
429 }
430 }
431
432 if (proc->attr.implicit_pure)
433 {
434 if (sym->attr.flavor == FL_PROCEDURE)
435 {
436 if (!gfc_pure (sym))
437 proc->attr.implicit_pure = 0;
438 }
439 else if (!sym->attr.pointer)
440 {
441 if (proc->attr.function && sym->attr.intent != INTENT_IN
442 && !sym->value)
443 proc->attr.implicit_pure = 0;
444
445 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
446 && !sym->value)
447 proc->attr.implicit_pure = 0;
448 }
449 }
450
451 if (gfc_elemental (proc))
452 {
453 /* F08:C1289. */
454 if (sym->attr.codimension
455 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
456 && CLASS_DATA (sym)->attr.codimension))
457 {
458 gfc_error ("Coarray dummy argument %qs at %L to elemental "
459 "procedure", sym->name, &sym->declared_at);
460 continue;
461 }
462
463 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
464 && CLASS_DATA (sym)->as))
465 {
466 gfc_error ("Argument %qs of elemental procedure at %L must "
467 "be scalar", sym->name, &sym->declared_at);
468 continue;
469 }
470
471 if (sym->attr.allocatable
472 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
473 && CLASS_DATA (sym)->attr.allocatable))
474 {
475 gfc_error ("Argument %qs of elemental procedure at %L cannot "
476 "have the ALLOCATABLE attribute", sym->name,
477 &sym->declared_at);
478 continue;
479 }
480
481 if (sym->attr.pointer
482 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
483 && CLASS_DATA (sym)->attr.class_pointer))
484 {
485 gfc_error ("Argument %qs of elemental procedure at %L cannot "
486 "have the POINTER attribute", sym->name,
487 &sym->declared_at);
488 continue;
489 }
490
491 if (sym->attr.flavor == FL_PROCEDURE)
492 {
493 gfc_error ("Dummy procedure %qs not allowed in elemental "
494 "procedure %qs at %L", sym->name, proc->name,
495 &sym->declared_at);
496 continue;
497 }
498
499 /* Fortran 2008 Corrigendum 1, C1290a. */
500 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
501 {
502 gfc_error ("Argument %qs of elemental procedure %qs at %L must "
503 "have its INTENT specified or have the VALUE "
504 "attribute", sym->name, proc->name,
505 &sym->declared_at);
506 continue;
507 }
508 }
509
510 /* Each dummy shall be specified to be scalar. */
511 if (proc->attr.proc == PROC_ST_FUNCTION)
512 {
513 if (sym->as != NULL)
514 {
515 /* F03:C1263 (R1238) The function-name and each dummy-arg-name
516 shall be specified, explicitly or implicitly, to be scalar. */
517 gfc_error ("Argument '%s' of statement function '%s' at %L "
518 "must be scalar", sym->name, proc->name,
519 &proc->declared_at);
520 continue;
521 }
522
523 if (sym->ts.type == BT_CHARACTER)
524 {
525 gfc_charlen *cl = sym->ts.u.cl;
526 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
527 {
528 gfc_error ("Character-valued argument %qs of statement "
529 "function at %L must have constant length",
530 sym->name, &sym->declared_at);
531 continue;
532 }
533 }
534 }
535 }
536 formal_arg_flag = false;
537 }
538
539
540 /* Work function called when searching for symbols that have argument lists
541 associated with them. */
542
543 static void
544 find_arglists (gfc_symbol *sym)
545 {
546 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
547 || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
548 return;
549
550 gfc_resolve_formal_arglist (sym);
551 }
552
553
554 /* Given a namespace, resolve all formal argument lists within the namespace.
555 */
556
557 static void
558 resolve_formal_arglists (gfc_namespace *ns)
559 {
560 if (ns == NULL)
561 return;
562
563 gfc_traverse_ns (ns, find_arglists);
564 }
565
566
567 static void
568 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
569 {
570 bool t;
571
572 if (sym && sym->attr.flavor == FL_PROCEDURE
573 && sym->ns->parent
574 && sym->ns->parent->proc_name
575 && sym->ns->parent->proc_name->attr.flavor == FL_PROCEDURE
576 && !strcmp (sym->name, sym->ns->parent->proc_name->name))
577 gfc_error ("Contained procedure %qs at %L has the same name as its "
578 "encompassing procedure", sym->name, &sym->declared_at);
579
580 /* If this namespace is not a function or an entry master function,
581 ignore it. */
582 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
583 || sym->attr.entry_master)
584 return;
585
586 if (!sym->result)
587 return;
588
589 /* Try to find out of what the return type is. */
590 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
591 {
592 t = gfc_set_default_type (sym->result, 0, ns);
593
594 if (!t && !sym->result->attr.untyped)
595 {
596 if (sym->result == sym)
597 gfc_error ("Contained function %qs at %L has no IMPLICIT type",
598 sym->name, &sym->declared_at);
599 else if (!sym->result->attr.proc_pointer)
600 gfc_error ("Result %qs of contained function %qs at %L has "
601 "no IMPLICIT type", sym->result->name, sym->name,
602 &sym->result->declared_at);
603 sym->result->attr.untyped = 1;
604 }
605 }
606
607 /* Fortran 2008 Draft Standard, page 535, C418, on type-param-value
608 type, lists the only ways a character length value of * can be used:
609 dummy arguments of procedures, named constants, function results and
610 in allocate statements if the allocate_object is an assumed length dummy
611 in external functions. Internal function results and results of module
612 procedures are not on this list, ergo, not permitted. */
613
614 if (sym->result->ts.type == BT_CHARACTER)
615 {
616 gfc_charlen *cl = sym->result->ts.u.cl;
617 if ((!cl || !cl->length) && !sym->result->ts.deferred)
618 {
619 /* See if this is a module-procedure and adapt error message
620 accordingly. */
621 bool module_proc;
622 gcc_assert (ns->parent && ns->parent->proc_name);
623 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
624
625 gfc_error (module_proc
626 ? G_("Character-valued module procedure %qs at %L"
627 " must not be assumed length")
628 : G_("Character-valued internal function %qs at %L"
629 " must not be assumed length"),
630 sym->name, &sym->declared_at);
631 }
632 }
633 }
634
635
636 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
637 introduce duplicates. */
638
639 static void
640 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
641 {
642 gfc_formal_arglist *f, *new_arglist;
643 gfc_symbol *new_sym;
644
645 for (; new_args != NULL; new_args = new_args->next)
646 {
647 new_sym = new_args->sym;
648 /* See if this arg is already in the formal argument list. */
649 for (f = proc->formal; f; f = f->next)
650 {
651 if (new_sym == f->sym)
652 break;
653 }
654
655 if (f)
656 continue;
657
658 /* Add a new argument. Argument order is not important. */
659 new_arglist = gfc_get_formal_arglist ();
660 new_arglist->sym = new_sym;
661 new_arglist->next = proc->formal;
662 proc->formal = new_arglist;
663 }
664 }
665
666
667 /* Flag the arguments that are not present in all entries. */
668
669 static void
670 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
671 {
672 gfc_formal_arglist *f, *head;
673 head = new_args;
674
675 for (f = proc->formal; f; f = f->next)
676 {
677 if (f->sym == NULL)
678 continue;
679
680 for (new_args = head; new_args; new_args = new_args->next)
681 {
682 if (new_args->sym == f->sym)
683 break;
684 }
685
686 if (new_args)
687 continue;
688
689 f->sym->attr.not_always_present = 1;
690 }
691 }
692
693
694 /* Resolve alternate entry points. If a symbol has multiple entry points we
695 create a new master symbol for the main routine, and turn the existing
696 symbol into an entry point. */
697
698 static void
699 resolve_entries (gfc_namespace *ns)
700 {
701 gfc_namespace *old_ns;
702 gfc_code *c;
703 gfc_symbol *proc;
704 gfc_entry_list *el;
705 char name[GFC_MAX_SYMBOL_LEN + 1];
706 static int master_count = 0;
707
708 if (ns->proc_name == NULL)
709 return;
710
711 /* No need to do anything if this procedure doesn't have alternate entry
712 points. */
713 if (!ns->entries)
714 return;
715
716 /* We may already have resolved alternate entry points. */
717 if (ns->proc_name->attr.entry_master)
718 return;
719
720 /* If this isn't a procedure something has gone horribly wrong. */
721 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
722
723 /* Remember the current namespace. */
724 old_ns = gfc_current_ns;
725
726 gfc_current_ns = ns;
727
728 /* Add the main entry point to the list of entry points. */
729 el = gfc_get_entry_list ();
730 el->sym = ns->proc_name;
731 el->id = 0;
732 el->next = ns->entries;
733 ns->entries = el;
734 ns->proc_name->attr.entry = 1;
735
736 /* If it is a module function, it needs to be in the right namespace
737 so that gfc_get_fake_result_decl can gather up the results. The
738 need for this arose in get_proc_name, where these beasts were
739 left in their own namespace, to keep prior references linked to
740 the entry declaration.*/
741 if (ns->proc_name->attr.function
742 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
743 el->sym->ns = ns;
744
745 /* Do the same for entries where the master is not a module
746 procedure. These are retained in the module namespace because
747 of the module procedure declaration. */
748 for (el = el->next; el; el = el->next)
749 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
750 && el->sym->attr.mod_proc)
751 el->sym->ns = ns;
752 el = ns->entries;
753
754 /* Add an entry statement for it. */
755 c = gfc_get_code (EXEC_ENTRY);
756 c->ext.entry = el;
757 c->next = ns->code;
758 ns->code = c;
759
760 /* Create a new symbol for the master function. */
761 /* Give the internal function a unique name (within this file).
762 Also include the function name so the user has some hope of figuring
763 out what is going on. */
764 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
765 master_count++, ns->proc_name->name);
766 gfc_get_ha_symbol (name, &proc);
767 gcc_assert (proc != NULL);
768
769 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
770 if (ns->proc_name->attr.subroutine)
771 gfc_add_subroutine (&proc->attr, proc->name, NULL);
772 else
773 {
774 gfc_symbol *sym;
775 gfc_typespec *ts, *fts;
776 gfc_array_spec *as, *fas;
777 gfc_add_function (&proc->attr, proc->name, NULL);
778 proc->result = proc;
779 fas = ns->entries->sym->as;
780 fas = fas ? fas : ns->entries->sym->result->as;
781 fts = &ns->entries->sym->result->ts;
782 if (fts->type == BT_UNKNOWN)
783 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
784 for (el = ns->entries->next; el; el = el->next)
785 {
786 ts = &el->sym->result->ts;
787 as = el->sym->as;
788 as = as ? as : el->sym->result->as;
789 if (ts->type == BT_UNKNOWN)
790 ts = gfc_get_default_type (el->sym->result->name, NULL);
791
792 if (! gfc_compare_types (ts, fts)
793 || (el->sym->result->attr.dimension
794 != ns->entries->sym->result->attr.dimension)
795 || (el->sym->result->attr.pointer
796 != ns->entries->sym->result->attr.pointer))
797 break;
798 else if (as && fas && ns->entries->sym->result != el->sym->result
799 && gfc_compare_array_spec (as, fas) == 0)
800 gfc_error ("Function %s at %L has entries with mismatched "
801 "array specifications", ns->entries->sym->name,
802 &ns->entries->sym->declared_at);
803 /* The characteristics need to match and thus both need to have
804 the same string length, i.e. both len=*, or both len=4.
805 Having both len=<variable> is also possible, but difficult to
806 check at compile time. */
807 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
808 && (((ts->u.cl->length && !fts->u.cl->length)
809 ||(!ts->u.cl->length && fts->u.cl->length))
810 || (ts->u.cl->length
811 && ts->u.cl->length->expr_type
812 != fts->u.cl->length->expr_type)
813 || (ts->u.cl->length
814 && ts->u.cl->length->expr_type == EXPR_CONSTANT
815 && mpz_cmp (ts->u.cl->length->value.integer,
816 fts->u.cl->length->value.integer) != 0)))
817 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
818 "entries returning variables of different "
819 "string lengths", ns->entries->sym->name,
820 &ns->entries->sym->declared_at);
821 }
822
823 if (el == NULL)
824 {
825 sym = ns->entries->sym->result;
826 /* All result types the same. */
827 proc->ts = *fts;
828 if (sym->attr.dimension)
829 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
830 if (sym->attr.pointer)
831 gfc_add_pointer (&proc->attr, NULL);
832 }
833 else
834 {
835 /* Otherwise the result will be passed through a union by
836 reference. */
837 proc->attr.mixed_entry_master = 1;
838 for (el = ns->entries; el; el = el->next)
839 {
840 sym = el->sym->result;
841 if (sym->attr.dimension)
842 {
843 if (el == ns->entries)
844 gfc_error ("FUNCTION result %s cannot be an array in "
845 "FUNCTION %s at %L", sym->name,
846 ns->entries->sym->name, &sym->declared_at);
847 else
848 gfc_error ("ENTRY result %s cannot be an array in "
849 "FUNCTION %s at %L", sym->name,
850 ns->entries->sym->name, &sym->declared_at);
851 }
852 else if (sym->attr.pointer)
853 {
854 if (el == ns->entries)
855 gfc_error ("FUNCTION result %s cannot be a POINTER in "
856 "FUNCTION %s at %L", sym->name,
857 ns->entries->sym->name, &sym->declared_at);
858 else
859 gfc_error ("ENTRY result %s cannot be a POINTER in "
860 "FUNCTION %s at %L", sym->name,
861 ns->entries->sym->name, &sym->declared_at);
862 }
863 else
864 {
865 ts = &sym->ts;
866 if (ts->type == BT_UNKNOWN)
867 ts = gfc_get_default_type (sym->name, NULL);
868 switch (ts->type)
869 {
870 case BT_INTEGER:
871 if (ts->kind == gfc_default_integer_kind)
872 sym = NULL;
873 break;
874 case BT_REAL:
875 if (ts->kind == gfc_default_real_kind
876 || ts->kind == gfc_default_double_kind)
877 sym = NULL;
878 break;
879 case BT_COMPLEX:
880 if (ts->kind == gfc_default_complex_kind)
881 sym = NULL;
882 break;
883 case BT_LOGICAL:
884 if (ts->kind == gfc_default_logical_kind)
885 sym = NULL;
886 break;
887 case BT_UNKNOWN:
888 /* We will issue error elsewhere. */
889 sym = NULL;
890 break;
891 default:
892 break;
893 }
894 if (sym)
895 {
896 if (el == ns->entries)
897 gfc_error ("FUNCTION result %s cannot be of type %s "
898 "in FUNCTION %s at %L", sym->name,
899 gfc_typename (ts), ns->entries->sym->name,
900 &sym->declared_at);
901 else
902 gfc_error ("ENTRY result %s cannot be of type %s "
903 "in FUNCTION %s at %L", sym->name,
904 gfc_typename (ts), ns->entries->sym->name,
905 &sym->declared_at);
906 }
907 }
908 }
909 }
910 }
911 proc->attr.access = ACCESS_PRIVATE;
912 proc->attr.entry_master = 1;
913
914 /* Merge all the entry point arguments. */
915 for (el = ns->entries; el; el = el->next)
916 merge_argument_lists (proc, el->sym->formal);
917
918 /* Check the master formal arguments for any that are not
919 present in all entry points. */
920 for (el = ns->entries; el; el = el->next)
921 check_argument_lists (proc, el->sym->formal);
922
923 /* Use the master function for the function body. */
924 ns->proc_name = proc;
925
926 /* Finalize the new symbols. */
927 gfc_commit_symbols ();
928
929 /* Restore the original namespace. */
930 gfc_current_ns = old_ns;
931 }
932
933
934 /* Resolve common variables. */
935 static void
936 resolve_common_vars (gfc_common_head *common_block, bool named_common)
937 {
938 gfc_symbol *csym = common_block->head;
939
940 for (; csym; csym = csym->common_next)
941 {
942 /* gfc_add_in_common may have been called before, but the reported errors
943 have been ignored to continue parsing.
944 We do the checks again here. */
945 if (!csym->attr.use_assoc)
946 {
947 gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
948 gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
949 &common_block->where);
950 }
951
952 if (csym->value || csym->attr.data)
953 {
954 if (!csym->ns->is_block_data)
955 gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
956 "but only in BLOCK DATA initialization is "
957 "allowed", csym->name, &csym->declared_at);
958 else if (!named_common)
959 gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
960 "in a blank COMMON but initialization is only "
961 "allowed in named common blocks", csym->name,
962 &csym->declared_at);
963 }
964
965 if (UNLIMITED_POLY (csym))
966 gfc_error_now ("%qs in cannot appear in COMMON at %L "
967 "[F2008:C5100]", csym->name, &csym->declared_at);
968
969 if (csym->ts.type != BT_DERIVED)
970 continue;
971
972 if (!(csym->ts.u.derived->attr.sequence
973 || csym->ts.u.derived->attr.is_bind_c))
974 gfc_error_now ("Derived type variable %qs in COMMON at %L "
975 "has neither the SEQUENCE nor the BIND(C) "
976 "attribute", csym->name, &csym->declared_at);
977 if (csym->ts.u.derived->attr.alloc_comp)
978 gfc_error_now ("Derived type variable %qs in COMMON at %L "
979 "has an ultimate component that is "
980 "allocatable", csym->name, &csym->declared_at);
981 if (gfc_has_default_initializer (csym->ts.u.derived))
982 gfc_error_now ("Derived type variable %qs in COMMON at %L "
983 "may not have default initializer", csym->name,
984 &csym->declared_at);
985
986 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
987 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
988 }
989 }
990
991 /* Resolve common blocks. */
992 static void
993 resolve_common_blocks (gfc_symtree *common_root)
994 {
995 gfc_symbol *sym;
996 gfc_gsymbol * gsym;
997
998 if (common_root == NULL)
999 return;
1000
1001 if (common_root->left)
1002 resolve_common_blocks (common_root->left);
1003 if (common_root->right)
1004 resolve_common_blocks (common_root->right);
1005
1006 resolve_common_vars (common_root->n.common, true);
1007
1008 /* The common name is a global name - in Fortran 2003 also if it has a
1009 C binding name, since Fortran 2008 only the C binding name is a global
1010 identifier. */
1011 if (!common_root->n.common->binding_label
1012 || gfc_notification_std (GFC_STD_F2008))
1013 {
1014 gsym = gfc_find_gsymbol (gfc_gsym_root,
1015 common_root->n.common->name);
1016
1017 if (gsym && gfc_notification_std (GFC_STD_F2008)
1018 && gsym->type == GSYM_COMMON
1019 && ((common_root->n.common->binding_label
1020 && (!gsym->binding_label
1021 || strcmp (common_root->n.common->binding_label,
1022 gsym->binding_label) != 0))
1023 || (!common_root->n.common->binding_label
1024 && gsym->binding_label)))
1025 {
1026 gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
1027 "identifier and must thus have the same binding name "
1028 "as the same-named COMMON block at %L: %s vs %s",
1029 common_root->n.common->name, &common_root->n.common->where,
1030 &gsym->where,
1031 common_root->n.common->binding_label
1032 ? common_root->n.common->binding_label : "(blank)",
1033 gsym->binding_label ? gsym->binding_label : "(blank)");
1034 return;
1035 }
1036
1037 if (gsym && gsym->type != GSYM_COMMON
1038 && !common_root->n.common->binding_label)
1039 {
1040 gfc_error ("COMMON block %qs at %L uses the same global identifier "
1041 "as entity at %L",
1042 common_root->n.common->name, &common_root->n.common->where,
1043 &gsym->where);
1044 return;
1045 }
1046 if (gsym && gsym->type != GSYM_COMMON)
1047 {
1048 gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
1049 "%L sharing the identifier with global non-COMMON-block "
1050 "entity at %L", common_root->n.common->name,
1051 &common_root->n.common->where, &gsym->where);
1052 return;
1053 }
1054 if (!gsym)
1055 {
1056 gsym = gfc_get_gsymbol (common_root->n.common->name, false);
1057 gsym->type = GSYM_COMMON;
1058 gsym->where = common_root->n.common->where;
1059 gsym->defined = 1;
1060 }
1061 gsym->used = 1;
1062 }
1063
1064 if (common_root->n.common->binding_label)
1065 {
1066 gsym = gfc_find_gsymbol (gfc_gsym_root,
1067 common_root->n.common->binding_label);
1068 if (gsym && gsym->type != GSYM_COMMON)
1069 {
1070 gfc_error ("COMMON block at %L with binding label %qs uses the same "
1071 "global identifier as entity at %L",
1072 &common_root->n.common->where,
1073 common_root->n.common->binding_label, &gsym->where);
1074 return;
1075 }
1076 if (!gsym)
1077 {
1078 gsym = gfc_get_gsymbol (common_root->n.common->binding_label, true);
1079 gsym->type = GSYM_COMMON;
1080 gsym->where = common_root->n.common->where;
1081 gsym->defined = 1;
1082 }
1083 gsym->used = 1;
1084 }
1085
1086 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1087 if (sym == NULL)
1088 return;
1089
1090 if (sym->attr.flavor == FL_PARAMETER)
1091 gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
1092 sym->name, &common_root->n.common->where, &sym->declared_at);
1093
1094 if (sym->attr.external)
1095 gfc_error ("COMMON block %qs at %L cannot have the EXTERNAL attribute",
1096 sym->name, &common_root->n.common->where);
1097
1098 if (sym->attr.intrinsic)
1099 gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
1100 sym->name, &common_root->n.common->where);
1101 else if (sym->attr.result
1102 || gfc_is_function_return_value (sym, gfc_current_ns))
1103 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1104 "that is also a function result", sym->name,
1105 &common_root->n.common->where);
1106 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1107 && sym->attr.proc != PROC_ST_FUNCTION)
1108 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1109 "that is also a global procedure", sym->name,
1110 &common_root->n.common->where);
1111 }
1112
1113
1114 /* Resolve contained function types. Because contained functions can call one
1115 another, they have to be worked out before any of the contained procedures
1116 can be resolved.
1117
1118 The good news is that if a function doesn't already have a type, the only
1119 way it can get one is through an IMPLICIT type or a RESULT variable, because
1120 by definition contained functions are contained namespace they're contained
1121 in, not in a sibling or parent namespace. */
1122
1123 static void
1124 resolve_contained_functions (gfc_namespace *ns)
1125 {
1126 gfc_namespace *child;
1127 gfc_entry_list *el;
1128
1129 resolve_formal_arglists (ns);
1130
1131 for (child = ns->contained; child; child = child->sibling)
1132 {
1133 /* Resolve alternate entry points first. */
1134 resolve_entries (child);
1135
1136 /* Then check function return types. */
1137 resolve_contained_fntype (child->proc_name, child);
1138 for (el = child->entries; el; el = el->next)
1139 resolve_contained_fntype (el->sym, child);
1140 }
1141 }
1142
1143
1144
1145 /* A Parameterized Derived Type constructor must contain values for
1146 the PDT KIND parameters or they must have a default initializer.
1147 Go through the constructor picking out the KIND expressions,
1148 storing them in 'param_list' and then call gfc_get_pdt_instance
1149 to obtain the PDT instance. */
1150
1151 static gfc_actual_arglist *param_list, *param_tail, *param;
1152
1153 static bool
1154 get_pdt_spec_expr (gfc_component *c, gfc_expr *expr)
1155 {
1156 param = gfc_get_actual_arglist ();
1157 if (!param_list)
1158 param_list = param_tail = param;
1159 else
1160 {
1161 param_tail->next = param;
1162 param_tail = param_tail->next;
1163 }
1164
1165 param_tail->name = c->name;
1166 if (expr)
1167 param_tail->expr = gfc_copy_expr (expr);
1168 else if (c->initializer)
1169 param_tail->expr = gfc_copy_expr (c->initializer);
1170 else
1171 {
1172 param_tail->spec_type = SPEC_ASSUMED;
1173 if (c->attr.pdt_kind)
1174 {
1175 gfc_error ("The KIND parameter %qs in the PDT constructor "
1176 "at %C has no value", param->name);
1177 return false;
1178 }
1179 }
1180
1181 return true;
1182 }
1183
1184 static bool
1185 get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr,
1186 gfc_symbol *derived)
1187 {
1188 gfc_constructor *cons = NULL;
1189 gfc_component *comp;
1190 bool t = true;
1191
1192 if (expr && expr->expr_type == EXPR_STRUCTURE)
1193 cons = gfc_constructor_first (expr->value.constructor);
1194 else if (constr)
1195 cons = *constr;
1196 gcc_assert (cons);
1197
1198 comp = derived->components;
1199
1200 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1201 {
1202 if (cons->expr
1203 && cons->expr->expr_type == EXPR_STRUCTURE
1204 && comp->ts.type == BT_DERIVED)
1205 {
1206 t = get_pdt_constructor (cons->expr, NULL, comp->ts.u.derived);
1207 if (!t)
1208 return t;
1209 }
1210 else if (comp->ts.type == BT_DERIVED)
1211 {
1212 t = get_pdt_constructor (NULL, &cons, comp->ts.u.derived);
1213 if (!t)
1214 return t;
1215 }
1216 else if ((comp->attr.pdt_kind || comp->attr.pdt_len)
1217 && derived->attr.pdt_template)
1218 {
1219 t = get_pdt_spec_expr (comp, cons->expr);
1220 if (!t)
1221 return t;
1222 }
1223 }
1224 return t;
1225 }
1226
1227
1228 static bool resolve_fl_derived0 (gfc_symbol *sym);
1229 static bool resolve_fl_struct (gfc_symbol *sym);
1230
1231
1232 /* Resolve all of the elements of a structure constructor and make sure that
1233 the types are correct. The 'init' flag indicates that the given
1234 constructor is an initializer. */
1235
1236 static bool
1237 resolve_structure_cons (gfc_expr *expr, int init)
1238 {
1239 gfc_constructor *cons;
1240 gfc_component *comp;
1241 bool t;
1242 symbol_attribute a;
1243
1244 t = true;
1245
1246 if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
1247 {
1248 if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
1249 resolve_fl_derived0 (expr->ts.u.derived);
1250 else
1251 resolve_fl_struct (expr->ts.u.derived);
1252
1253 /* If this is a Parameterized Derived Type template, find the
1254 instance corresponding to the PDT kind parameters. */
1255 if (expr->ts.u.derived->attr.pdt_template)
1256 {
1257 param_list = NULL;
1258 t = get_pdt_constructor (expr, NULL, expr->ts.u.derived);
1259 if (!t)
1260 return t;
1261 gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL);
1262
1263 expr->param_list = gfc_copy_actual_arglist (param_list);
1264
1265 if (param_list)
1266 gfc_free_actual_arglist (param_list);
1267
1268 if (!expr->ts.u.derived->attr.pdt_type)
1269 return false;
1270 }
1271 }
1272
1273 cons = gfc_constructor_first (expr->value.constructor);
1274
1275 /* A constructor may have references if it is the result of substituting a
1276 parameter variable. In this case we just pull out the component we
1277 want. */
1278 if (expr->ref)
1279 comp = expr->ref->u.c.sym->components;
1280 else
1281 comp = expr->ts.u.derived->components;
1282
1283 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1284 {
1285 int rank;
1286
1287 if (!cons->expr)
1288 continue;
1289
1290 /* Unions use an EXPR_NULL contrived expression to tell the translation
1291 phase to generate an initializer of the appropriate length.
1292 Ignore it here. */
1293 if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
1294 continue;
1295
1296 if (!gfc_resolve_expr (cons->expr))
1297 {
1298 t = false;
1299 continue;
1300 }
1301
1302 rank = comp->as ? comp->as->rank : 0;
1303 if (comp->ts.type == BT_CLASS
1304 && !comp->ts.u.derived->attr.unlimited_polymorphic
1305 && CLASS_DATA (comp)->as)
1306 rank = CLASS_DATA (comp)->as->rank;
1307
1308 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1309 && (comp->attr.allocatable || cons->expr->rank))
1310 {
1311 gfc_error ("The rank of the element in the structure "
1312 "constructor at %L does not match that of the "
1313 "component (%d/%d)", &cons->expr->where,
1314 cons->expr->rank, rank);
1315 t = false;
1316 }
1317
1318 /* If we don't have the right type, try to convert it. */
1319
1320 if (!comp->attr.proc_pointer &&
1321 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1322 {
1323 if (strcmp (comp->name, "_extends") == 0)
1324 {
1325 /* Can afford to be brutal with the _extends initializer.
1326 The derived type can get lost because it is PRIVATE
1327 but it is not usage constrained by the standard. */
1328 cons->expr->ts = comp->ts;
1329 }
1330 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1331 {
1332 gfc_error ("The element in the structure constructor at %L, "
1333 "for pointer component %qs, is %s but should be %s",
1334 &cons->expr->where, comp->name,
1335 gfc_basic_typename (cons->expr->ts.type),
1336 gfc_basic_typename (comp->ts.type));
1337 t = false;
1338 }
1339 else
1340 {
1341 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1342 if (t)
1343 t = t2;
1344 }
1345 }
1346
1347 /* For strings, the length of the constructor should be the same as
1348 the one of the structure, ensure this if the lengths are known at
1349 compile time and when we are dealing with PARAMETER or structure
1350 constructors. */
1351 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1352 && comp->ts.u.cl->length
1353 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1354 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1355 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1356 && cons->expr->rank != 0
1357 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1358 comp->ts.u.cl->length->value.integer) != 0)
1359 {
1360 if (cons->expr->expr_type == EXPR_VARIABLE
1361 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1362 {
1363 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1364 to make use of the gfc_resolve_character_array_constructor
1365 machinery. The expression is later simplified away to
1366 an array of string literals. */
1367 gfc_expr *para = cons->expr;
1368 cons->expr = gfc_get_expr ();
1369 cons->expr->ts = para->ts;
1370 cons->expr->where = para->where;
1371 cons->expr->expr_type = EXPR_ARRAY;
1372 cons->expr->rank = para->rank;
1373 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1374 gfc_constructor_append_expr (&cons->expr->value.constructor,
1375 para, &cons->expr->where);
1376 }
1377
1378 if (cons->expr->expr_type == EXPR_ARRAY)
1379 {
1380 /* Rely on the cleanup of the namespace to deal correctly with
1381 the old charlen. (There was a block here that attempted to
1382 remove the charlen but broke the chain in so doing.) */
1383 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1384 cons->expr->ts.u.cl->length_from_typespec = true;
1385 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1386 gfc_resolve_character_array_constructor (cons->expr);
1387 }
1388 }
1389
1390 if (cons->expr->expr_type == EXPR_NULL
1391 && !(comp->attr.pointer || comp->attr.allocatable
1392 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1393 || (comp->ts.type == BT_CLASS
1394 && (CLASS_DATA (comp)->attr.class_pointer
1395 || CLASS_DATA (comp)->attr.allocatable))))
1396 {
1397 t = false;
1398 gfc_error ("The NULL in the structure constructor at %L is "
1399 "being applied to component %qs, which is neither "
1400 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1401 comp->name);
1402 }
1403
1404 if (comp->attr.proc_pointer && comp->ts.interface)
1405 {
1406 /* Check procedure pointer interface. */
1407 gfc_symbol *s2 = NULL;
1408 gfc_component *c2;
1409 const char *name;
1410 char err[200];
1411
1412 c2 = gfc_get_proc_ptr_comp (cons->expr);
1413 if (c2)
1414 {
1415 s2 = c2->ts.interface;
1416 name = c2->name;
1417 }
1418 else if (cons->expr->expr_type == EXPR_FUNCTION)
1419 {
1420 s2 = cons->expr->symtree->n.sym->result;
1421 name = cons->expr->symtree->n.sym->result->name;
1422 }
1423 else if (cons->expr->expr_type != EXPR_NULL)
1424 {
1425 s2 = cons->expr->symtree->n.sym;
1426 name = cons->expr->symtree->n.sym->name;
1427 }
1428
1429 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1430 err, sizeof (err), NULL, NULL))
1431 {
1432 gfc_error_opt (0, "Interface mismatch for procedure-pointer "
1433 "component %qs in structure constructor at %L:"
1434 " %s", comp->name, &cons->expr->where, err);
1435 return false;
1436 }
1437 }
1438
1439 if (!comp->attr.pointer || comp->attr.proc_pointer
1440 || cons->expr->expr_type == EXPR_NULL)
1441 continue;
1442
1443 a = gfc_expr_attr (cons->expr);
1444
1445 if (!a.pointer && !a.target)
1446 {
1447 t = false;
1448 gfc_error ("The element in the structure constructor at %L, "
1449 "for pointer component %qs should be a POINTER or "
1450 "a TARGET", &cons->expr->where, comp->name);
1451 }
1452
1453 if (init)
1454 {
1455 /* F08:C461. Additional checks for pointer initialization. */
1456 if (a.allocatable)
1457 {
1458 t = false;
1459 gfc_error ("Pointer initialization target at %L "
1460 "must not be ALLOCATABLE", &cons->expr->where);
1461 }
1462 if (!a.save)
1463 {
1464 t = false;
1465 gfc_error ("Pointer initialization target at %L "
1466 "must have the SAVE attribute", &cons->expr->where);
1467 }
1468 }
1469
1470 /* F2003, C1272 (3). */
1471 bool impure = cons->expr->expr_type == EXPR_VARIABLE
1472 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1473 || gfc_is_coindexed (cons->expr));
1474 if (impure && gfc_pure (NULL))
1475 {
1476 t = false;
1477 gfc_error ("Invalid expression in the structure constructor for "
1478 "pointer component %qs at %L in PURE procedure",
1479 comp->name, &cons->expr->where);
1480 }
1481
1482 if (impure)
1483 gfc_unset_implicit_pure (NULL);
1484 }
1485
1486 return t;
1487 }
1488
1489
1490 /****************** Expression name resolution ******************/
1491
1492 /* Returns 0 if a symbol was not declared with a type or
1493 attribute declaration statement, nonzero otherwise. */
1494
1495 static int
1496 was_declared (gfc_symbol *sym)
1497 {
1498 symbol_attribute a;
1499
1500 a = sym->attr;
1501
1502 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1503 return 1;
1504
1505 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1506 || a.optional || a.pointer || a.save || a.target || a.volatile_
1507 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1508 || a.asynchronous || a.codimension)
1509 return 1;
1510
1511 return 0;
1512 }
1513
1514
1515 /* Determine if a symbol is generic or not. */
1516
1517 static int
1518 generic_sym (gfc_symbol *sym)
1519 {
1520 gfc_symbol *s;
1521
1522 if (sym->attr.generic ||
1523 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1524 return 1;
1525
1526 if (was_declared (sym) || sym->ns->parent == NULL)
1527 return 0;
1528
1529 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1530
1531 if (s != NULL)
1532 {
1533 if (s == sym)
1534 return 0;
1535 else
1536 return generic_sym (s);
1537 }
1538
1539 return 0;
1540 }
1541
1542
1543 /* Determine if a symbol is specific or not. */
1544
1545 static int
1546 specific_sym (gfc_symbol *sym)
1547 {
1548 gfc_symbol *s;
1549
1550 if (sym->attr.if_source == IFSRC_IFBODY
1551 || sym->attr.proc == PROC_MODULE
1552 || sym->attr.proc == PROC_INTERNAL
1553 || sym->attr.proc == PROC_ST_FUNCTION
1554 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1555 || sym->attr.external)
1556 return 1;
1557
1558 if (was_declared (sym) || sym->ns->parent == NULL)
1559 return 0;
1560
1561 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1562
1563 return (s == NULL) ? 0 : specific_sym (s);
1564 }
1565
1566
1567 /* Figure out if the procedure is specific, generic or unknown. */
1568
1569 enum proc_type
1570 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
1571
1572 static proc_type
1573 procedure_kind (gfc_symbol *sym)
1574 {
1575 if (generic_sym (sym))
1576 return PTYPE_GENERIC;
1577
1578 if (specific_sym (sym))
1579 return PTYPE_SPECIFIC;
1580
1581 return PTYPE_UNKNOWN;
1582 }
1583
1584 /* Check references to assumed size arrays. The flag need_full_assumed_size
1585 is nonzero when matching actual arguments. */
1586
1587 static int need_full_assumed_size = 0;
1588
1589 static bool
1590 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1591 {
1592 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1593 return false;
1594
1595 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1596 What should it be? */
1597 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1598 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1599 && (e->ref->u.ar.type == AR_FULL))
1600 {
1601 gfc_error ("The upper bound in the last dimension must "
1602 "appear in the reference to the assumed size "
1603 "array %qs at %L", sym->name, &e->where);
1604 return true;
1605 }
1606 return false;
1607 }
1608
1609
1610 /* Look for bad assumed size array references in argument expressions
1611 of elemental and array valued intrinsic procedures. Since this is
1612 called from procedure resolution functions, it only recurses at
1613 operators. */
1614
1615 static bool
1616 resolve_assumed_size_actual (gfc_expr *e)
1617 {
1618 if (e == NULL)
1619 return false;
1620
1621 switch (e->expr_type)
1622 {
1623 case EXPR_VARIABLE:
1624 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1625 return true;
1626 break;
1627
1628 case EXPR_OP:
1629 if (resolve_assumed_size_actual (e->value.op.op1)
1630 || resolve_assumed_size_actual (e->value.op.op2))
1631 return true;
1632 break;
1633
1634 default:
1635 break;
1636 }
1637 return false;
1638 }
1639
1640
1641 /* Check a generic procedure, passed as an actual argument, to see if
1642 there is a matching specific name. If none, it is an error, and if
1643 more than one, the reference is ambiguous. */
1644 static int
1645 count_specific_procs (gfc_expr *e)
1646 {
1647 int n;
1648 gfc_interface *p;
1649 gfc_symbol *sym;
1650
1651 n = 0;
1652 sym = e->symtree->n.sym;
1653
1654 for (p = sym->generic; p; p = p->next)
1655 if (strcmp (sym->name, p->sym->name) == 0)
1656 {
1657 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1658 sym->name);
1659 n++;
1660 }
1661
1662 if (n > 1)
1663 gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
1664 &e->where);
1665
1666 if (n == 0)
1667 gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1668 "argument at %L", sym->name, &e->where);
1669
1670 return n;
1671 }
1672
1673
1674 /* See if a call to sym could possibly be a not allowed RECURSION because of
1675 a missing RECURSIVE declaration. This means that either sym is the current
1676 context itself, or sym is the parent of a contained procedure calling its
1677 non-RECURSIVE containing procedure.
1678 This also works if sym is an ENTRY. */
1679
1680 static bool
1681 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1682 {
1683 gfc_symbol* proc_sym;
1684 gfc_symbol* context_proc;
1685 gfc_namespace* real_context;
1686
1687 if (sym->attr.flavor == FL_PROGRAM
1688 || gfc_fl_struct (sym->attr.flavor))
1689 return false;
1690
1691 /* If we've got an ENTRY, find real procedure. */
1692 if (sym->attr.entry && sym->ns->entries)
1693 proc_sym = sym->ns->entries->sym;
1694 else
1695 proc_sym = sym;
1696
1697 /* If sym is RECURSIVE, all is well of course. */
1698 if (proc_sym->attr.recursive || flag_recursive)
1699 return false;
1700
1701 /* Find the context procedure's "real" symbol if it has entries.
1702 We look for a procedure symbol, so recurse on the parents if we don't
1703 find one (like in case of a BLOCK construct). */
1704 for (real_context = context; ; real_context = real_context->parent)
1705 {
1706 /* We should find something, eventually! */
1707 gcc_assert (real_context);
1708
1709 context_proc = (real_context->entries ? real_context->entries->sym
1710 : real_context->proc_name);
1711
1712 /* In some special cases, there may not be a proc_name, like for this
1713 invalid code:
1714 real(bad_kind()) function foo () ...
1715 when checking the call to bad_kind ().
1716 In these cases, we simply return here and assume that the
1717 call is ok. */
1718 if (!context_proc)
1719 return false;
1720
1721 if (context_proc->attr.flavor != FL_LABEL)
1722 break;
1723 }
1724
1725 /* A call from sym's body to itself is recursion, of course. */
1726 if (context_proc == proc_sym)
1727 return true;
1728
1729 /* The same is true if context is a contained procedure and sym the
1730 containing one. */
1731 if (context_proc->attr.contained)
1732 {
1733 gfc_symbol* parent_proc;
1734
1735 gcc_assert (context->parent);
1736 parent_proc = (context->parent->entries ? context->parent->entries->sym
1737 : context->parent->proc_name);
1738
1739 if (parent_proc == proc_sym)
1740 return true;
1741 }
1742
1743 return false;
1744 }
1745
1746
1747 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1748 its typespec and formal argument list. */
1749
1750 bool
1751 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1752 {
1753 gfc_intrinsic_sym* isym = NULL;
1754 const char* symstd;
1755
1756 if (sym->resolve_symbol_called >= 2)
1757 return true;
1758
1759 sym->resolve_symbol_called = 2;
1760
1761 /* Already resolved. */
1762 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1763 return true;
1764
1765 /* We already know this one is an intrinsic, so we don't call
1766 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1767 gfc_find_subroutine directly to check whether it is a function or
1768 subroutine. */
1769
1770 if (sym->intmod_sym_id && sym->attr.subroutine)
1771 {
1772 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1773 isym = gfc_intrinsic_subroutine_by_id (id);
1774 }
1775 else if (sym->intmod_sym_id)
1776 {
1777 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1778 isym = gfc_intrinsic_function_by_id (id);
1779 }
1780 else if (!sym->attr.subroutine)
1781 isym = gfc_find_function (sym->name);
1782
1783 if (isym && !sym->attr.subroutine)
1784 {
1785 if (sym->ts.type != BT_UNKNOWN && warn_surprising
1786 && !sym->attr.implicit_type)
1787 gfc_warning (OPT_Wsurprising,
1788 "Type specified for intrinsic function %qs at %L is"
1789 " ignored", sym->name, &sym->declared_at);
1790
1791 if (!sym->attr.function &&
1792 !gfc_add_function(&sym->attr, sym->name, loc))
1793 return false;
1794
1795 sym->ts = isym->ts;
1796 }
1797 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1798 {
1799 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1800 {
1801 gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1802 " specifier", sym->name, &sym->declared_at);
1803 return false;
1804 }
1805
1806 if (!sym->attr.subroutine &&
1807 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1808 return false;
1809 }
1810 else
1811 {
1812 gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
1813 &sym->declared_at);
1814 return false;
1815 }
1816
1817 gfc_copy_formal_args_intr (sym, isym, NULL);
1818
1819 sym->attr.pure = isym->pure;
1820 sym->attr.elemental = isym->elemental;
1821
1822 /* Check it is actually available in the standard settings. */
1823 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1824 {
1825 gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1826 "available in the current standard settings but %s. Use "
1827 "an appropriate %<-std=*%> option or enable "
1828 "%<-fall-intrinsics%> in order to use it.",
1829 sym->name, &sym->declared_at, symstd);
1830 return false;
1831 }
1832
1833 return true;
1834 }
1835
1836
1837 /* Resolve a procedure expression, like passing it to a called procedure or as
1838 RHS for a procedure pointer assignment. */
1839
1840 static bool
1841 resolve_procedure_expression (gfc_expr* expr)
1842 {
1843 gfc_symbol* sym;
1844
1845 if (expr->expr_type != EXPR_VARIABLE)
1846 return true;
1847 gcc_assert (expr->symtree);
1848
1849 sym = expr->symtree->n.sym;
1850
1851 if (sym->attr.intrinsic)
1852 gfc_resolve_intrinsic (sym, &expr->where);
1853
1854 if (sym->attr.flavor != FL_PROCEDURE
1855 || (sym->attr.function && sym->result == sym))
1856 return true;
1857
1858 /* A non-RECURSIVE procedure that is used as procedure expression within its
1859 own body is in danger of being called recursively. */
1860 if (is_illegal_recursion (sym, gfc_current_ns))
1861 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
1862 " itself recursively. Declare it RECURSIVE or use"
1863 " %<-frecursive%>", sym->name, &expr->where);
1864
1865 return true;
1866 }
1867
1868
1869 /* Check that name is not a derived type. */
1870
1871 static bool
1872 is_dt_name (const char *name)
1873 {
1874 gfc_symbol *dt_list, *dt_first;
1875
1876 dt_list = dt_first = gfc_derived_types;
1877 for (; dt_list; dt_list = dt_list->dt_next)
1878 {
1879 if (strcmp(dt_list->name, name) == 0)
1880 return true;
1881 if (dt_first == dt_list->dt_next)
1882 break;
1883 }
1884 return false;
1885 }
1886
1887
1888 /* Resolve an actual argument list. Most of the time, this is just
1889 resolving the expressions in the list.
1890 The exception is that we sometimes have to decide whether arguments
1891 that look like procedure arguments are really simple variable
1892 references. */
1893
1894 static bool
1895 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1896 bool no_formal_args)
1897 {
1898 gfc_symbol *sym;
1899 gfc_symtree *parent_st;
1900 gfc_expr *e;
1901 gfc_component *comp;
1902 int save_need_full_assumed_size;
1903 bool return_value = false;
1904 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1905
1906 actual_arg = true;
1907 first_actual_arg = true;
1908
1909 for (; arg; arg = arg->next)
1910 {
1911 e = arg->expr;
1912 if (e == NULL)
1913 {
1914 /* Check the label is a valid branching target. */
1915 if (arg->label)
1916 {
1917 if (arg->label->defined == ST_LABEL_UNKNOWN)
1918 {
1919 gfc_error ("Label %d referenced at %L is never defined",
1920 arg->label->value, &arg->label->where);
1921 goto cleanup;
1922 }
1923 }
1924 first_actual_arg = false;
1925 continue;
1926 }
1927
1928 if (e->expr_type == EXPR_VARIABLE
1929 && e->symtree->n.sym->attr.generic
1930 && no_formal_args
1931 && count_specific_procs (e) != 1)
1932 goto cleanup;
1933
1934 if (e->ts.type != BT_PROCEDURE)
1935 {
1936 save_need_full_assumed_size = need_full_assumed_size;
1937 if (e->expr_type != EXPR_VARIABLE)
1938 need_full_assumed_size = 0;
1939 if (!gfc_resolve_expr (e))
1940 goto cleanup;
1941 need_full_assumed_size = save_need_full_assumed_size;
1942 goto argument_list;
1943 }
1944
1945 /* See if the expression node should really be a variable reference. */
1946
1947 sym = e->symtree->n.sym;
1948
1949 if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name))
1950 {
1951 gfc_error ("Derived type %qs is used as an actual "
1952 "argument at %L", sym->name, &e->where);
1953 goto cleanup;
1954 }
1955
1956 if (sym->attr.flavor == FL_PROCEDURE
1957 || sym->attr.intrinsic
1958 || sym->attr.external)
1959 {
1960 int actual_ok;
1961
1962 /* If a procedure is not already determined to be something else
1963 check if it is intrinsic. */
1964 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1965 sym->attr.intrinsic = 1;
1966
1967 if (sym->attr.proc == PROC_ST_FUNCTION)
1968 {
1969 gfc_error ("Statement function %qs at %L is not allowed as an "
1970 "actual argument", sym->name, &e->where);
1971 }
1972
1973 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1974 sym->attr.subroutine);
1975 if (sym->attr.intrinsic && actual_ok == 0)
1976 {
1977 gfc_error ("Intrinsic %qs at %L is not allowed as an "
1978 "actual argument", sym->name, &e->where);
1979 }
1980
1981 if (sym->attr.contained && !sym->attr.use_assoc
1982 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1983 {
1984 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
1985 " used as actual argument at %L",
1986 sym->name, &e->where))
1987 goto cleanup;
1988 }
1989
1990 if (sym->attr.elemental && !sym->attr.intrinsic)
1991 {
1992 gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
1993 "allowed as an actual argument at %L", sym->name,
1994 &e->where);
1995 }
1996
1997 /* Check if a generic interface has a specific procedure
1998 with the same name before emitting an error. */
1999 if (sym->attr.generic && count_specific_procs (e) != 1)
2000 goto cleanup;
2001
2002 /* Just in case a specific was found for the expression. */
2003 sym = e->symtree->n.sym;
2004
2005 /* If the symbol is the function that names the current (or
2006 parent) scope, then we really have a variable reference. */
2007
2008 if (gfc_is_function_return_value (sym, sym->ns))
2009 goto got_variable;
2010
2011 /* If all else fails, see if we have a specific intrinsic. */
2012 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
2013 {
2014 gfc_intrinsic_sym *isym;
2015
2016 isym = gfc_find_function (sym->name);
2017 if (isym == NULL || !isym->specific)
2018 {
2019 gfc_error ("Unable to find a specific INTRINSIC procedure "
2020 "for the reference %qs at %L", sym->name,
2021 &e->where);
2022 goto cleanup;
2023 }
2024 sym->ts = isym->ts;
2025 sym->attr.intrinsic = 1;
2026 sym->attr.function = 1;
2027 }
2028
2029 if (!gfc_resolve_expr (e))
2030 goto cleanup;
2031 goto argument_list;
2032 }
2033
2034 /* See if the name is a module procedure in a parent unit. */
2035
2036 if (was_declared (sym) || sym->ns->parent == NULL)
2037 goto got_variable;
2038
2039 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
2040 {
2041 gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
2042 goto cleanup;
2043 }
2044
2045 if (parent_st == NULL)
2046 goto got_variable;
2047
2048 sym = parent_st->n.sym;
2049 e->symtree = parent_st; /* Point to the right thing. */
2050
2051 if (sym->attr.flavor == FL_PROCEDURE
2052 || sym->attr.intrinsic
2053 || sym->attr.external)
2054 {
2055 if (!gfc_resolve_expr (e))
2056 goto cleanup;
2057 goto argument_list;
2058 }
2059
2060 got_variable:
2061 e->expr_type = EXPR_VARIABLE;
2062 e->ts = sym->ts;
2063 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
2064 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2065 && CLASS_DATA (sym)->as))
2066 {
2067 e->rank = sym->ts.type == BT_CLASS
2068 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
2069 e->ref = gfc_get_ref ();
2070 e->ref->type = REF_ARRAY;
2071 e->ref->u.ar.type = AR_FULL;
2072 e->ref->u.ar.as = sym->ts.type == BT_CLASS
2073 ? CLASS_DATA (sym)->as : sym->as;
2074 }
2075
2076 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
2077 primary.c (match_actual_arg). If above code determines that it
2078 is a variable instead, it needs to be resolved as it was not
2079 done at the beginning of this function. */
2080 save_need_full_assumed_size = need_full_assumed_size;
2081 if (e->expr_type != EXPR_VARIABLE)
2082 need_full_assumed_size = 0;
2083 if (!gfc_resolve_expr (e))
2084 goto cleanup;
2085 need_full_assumed_size = save_need_full_assumed_size;
2086
2087 argument_list:
2088 /* Check argument list functions %VAL, %LOC and %REF. There is
2089 nothing to do for %REF. */
2090 if (arg->name && arg->name[0] == '%')
2091 {
2092 if (strcmp ("%VAL", arg->name) == 0)
2093 {
2094 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
2095 {
2096 gfc_error ("By-value argument at %L is not of numeric "
2097 "type", &e->where);
2098 goto cleanup;
2099 }
2100
2101 if (e->rank)
2102 {
2103 gfc_error ("By-value argument at %L cannot be an array or "
2104 "an array section", &e->where);
2105 goto cleanup;
2106 }
2107
2108 /* Intrinsics are still PROC_UNKNOWN here. However,
2109 since same file external procedures are not resolvable
2110 in gfortran, it is a good deal easier to leave them to
2111 intrinsic.c. */
2112 if (ptype != PROC_UNKNOWN
2113 && ptype != PROC_DUMMY
2114 && ptype != PROC_EXTERNAL
2115 && ptype != PROC_MODULE)
2116 {
2117 gfc_error ("By-value argument at %L is not allowed "
2118 "in this context", &e->where);
2119 goto cleanup;
2120 }
2121 }
2122
2123 /* Statement functions have already been excluded above. */
2124 else if (strcmp ("%LOC", arg->name) == 0
2125 && e->ts.type == BT_PROCEDURE)
2126 {
2127 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
2128 {
2129 gfc_error ("Passing internal procedure at %L by location "
2130 "not allowed", &e->where);
2131 goto cleanup;
2132 }
2133 }
2134 }
2135
2136 comp = gfc_get_proc_ptr_comp(e);
2137 if (e->expr_type == EXPR_VARIABLE
2138 && comp && comp->attr.elemental)
2139 {
2140 gfc_error ("ELEMENTAL procedure pointer component %qs is not "
2141 "allowed as an actual argument at %L", comp->name,
2142 &e->where);
2143 }
2144
2145 /* Fortran 2008, C1237. */
2146 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
2147 && gfc_has_ultimate_pointer (e))
2148 {
2149 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2150 "component", &e->where);
2151 goto cleanup;
2152 }
2153
2154 first_actual_arg = false;
2155 }
2156
2157 return_value = true;
2158
2159 cleanup:
2160 actual_arg = actual_arg_sav;
2161 first_actual_arg = first_actual_arg_sav;
2162
2163 return return_value;
2164 }
2165
2166
2167 /* Do the checks of the actual argument list that are specific to elemental
2168 procedures. If called with c == NULL, we have a function, otherwise if
2169 expr == NULL, we have a subroutine. */
2170
2171 static bool
2172 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2173 {
2174 gfc_actual_arglist *arg0;
2175 gfc_actual_arglist *arg;
2176 gfc_symbol *esym = NULL;
2177 gfc_intrinsic_sym *isym = NULL;
2178 gfc_expr *e = NULL;
2179 gfc_intrinsic_arg *iformal = NULL;
2180 gfc_formal_arglist *eformal = NULL;
2181 bool formal_optional = false;
2182 bool set_by_optional = false;
2183 int i;
2184 int rank = 0;
2185
2186 /* Is this an elemental procedure? */
2187 if (expr && expr->value.function.actual != NULL)
2188 {
2189 if (expr->value.function.esym != NULL
2190 && expr->value.function.esym->attr.elemental)
2191 {
2192 arg0 = expr->value.function.actual;
2193 esym = expr->value.function.esym;
2194 }
2195 else if (expr->value.function.isym != NULL
2196 && expr->value.function.isym->elemental)
2197 {
2198 arg0 = expr->value.function.actual;
2199 isym = expr->value.function.isym;
2200 }
2201 else
2202 return true;
2203 }
2204 else if (c && c->ext.actual != NULL)
2205 {
2206 arg0 = c->ext.actual;
2207
2208 if (c->resolved_sym)
2209 esym = c->resolved_sym;
2210 else
2211 esym = c->symtree->n.sym;
2212 gcc_assert (esym);
2213
2214 if (!esym->attr.elemental)
2215 return true;
2216 }
2217 else
2218 return true;
2219
2220 /* The rank of an elemental is the rank of its array argument(s). */
2221 for (arg = arg0; arg; arg = arg->next)
2222 {
2223 if (arg->expr != NULL && arg->expr->rank != 0)
2224 {
2225 rank = arg->expr->rank;
2226 if (arg->expr->expr_type == EXPR_VARIABLE
2227 && arg->expr->symtree->n.sym->attr.optional)
2228 set_by_optional = true;
2229
2230 /* Function specific; set the result rank and shape. */
2231 if (expr)
2232 {
2233 expr->rank = rank;
2234 if (!expr->shape && arg->expr->shape)
2235 {
2236 expr->shape = gfc_get_shape (rank);
2237 for (i = 0; i < rank; i++)
2238 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2239 }
2240 }
2241 break;
2242 }
2243 }
2244
2245 /* If it is an array, it shall not be supplied as an actual argument
2246 to an elemental procedure unless an array of the same rank is supplied
2247 as an actual argument corresponding to a nonoptional dummy argument of
2248 that elemental procedure(12.4.1.5). */
2249 formal_optional = false;
2250 if (isym)
2251 iformal = isym->formal;
2252 else
2253 eformal = esym->formal;
2254
2255 for (arg = arg0; arg; arg = arg->next)
2256 {
2257 if (eformal)
2258 {
2259 if (eformal->sym && eformal->sym->attr.optional)
2260 formal_optional = true;
2261 eformal = eformal->next;
2262 }
2263 else if (isym && iformal)
2264 {
2265 if (iformal->optional)
2266 formal_optional = true;
2267 iformal = iformal->next;
2268 }
2269 else if (isym)
2270 formal_optional = true;
2271
2272 if (pedantic && arg->expr != NULL
2273 && arg->expr->expr_type == EXPR_VARIABLE
2274 && arg->expr->symtree->n.sym->attr.optional
2275 && formal_optional
2276 && arg->expr->rank
2277 && (set_by_optional || arg->expr->rank != rank)
2278 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2279 {
2280 bool t = false;
2281 gfc_actual_arglist *a;
2282
2283 /* Scan the argument list for a non-optional argument with the
2284 same rank as arg. */
2285 for (a = arg0; a; a = a->next)
2286 if (a != arg
2287 && a->expr->rank == arg->expr->rank
2288 && !a->expr->symtree->n.sym->attr.optional)
2289 {
2290 t = true;
2291 break;
2292 }
2293
2294 if (!t)
2295 gfc_warning (OPT_Wpedantic,
2296 "%qs at %L is an array and OPTIONAL; If it is not "
2297 "present, then it cannot be the actual argument of "
2298 "an ELEMENTAL procedure unless there is a non-optional"
2299 " argument with the same rank "
2300 "(Fortran 2018, 15.5.2.12)",
2301 arg->expr->symtree->n.sym->name, &arg->expr->where);
2302 }
2303 }
2304
2305 for (arg = arg0; arg; arg = arg->next)
2306 {
2307 if (arg->expr == NULL || arg->expr->rank == 0)
2308 continue;
2309
2310 /* Being elemental, the last upper bound of an assumed size array
2311 argument must be present. */
2312 if (resolve_assumed_size_actual (arg->expr))
2313 return false;
2314
2315 /* Elemental procedure's array actual arguments must conform. */
2316 if (e != NULL)
2317 {
2318 if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
2319 return false;
2320 }
2321 else
2322 e = arg->expr;
2323 }
2324
2325 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2326 is an array, the intent inout/out variable needs to be also an array. */
2327 if (rank > 0 && esym && expr == NULL)
2328 for (eformal = esym->formal, arg = arg0; arg && eformal;
2329 arg = arg->next, eformal = eformal->next)
2330 if ((eformal->sym->attr.intent == INTENT_OUT
2331 || eformal->sym->attr.intent == INTENT_INOUT)
2332 && arg->expr && arg->expr->rank == 0)
2333 {
2334 gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2335 "ELEMENTAL subroutine %qs is a scalar, but another "
2336 "actual argument is an array", &arg->expr->where,
2337 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2338 : "INOUT", eformal->sym->name, esym->name);
2339 return false;
2340 }
2341 return true;
2342 }
2343
2344
2345 /* This function does the checking of references to global procedures
2346 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2347 77 and 95 standards. It checks for a gsymbol for the name, making
2348 one if it does not already exist. If it already exists, then the
2349 reference being resolved must correspond to the type of gsymbol.
2350 Otherwise, the new symbol is equipped with the attributes of the
2351 reference. The corresponding code that is called in creating
2352 global entities is parse.c.
2353
2354 In addition, for all but -std=legacy, the gsymbols are used to
2355 check the interfaces of external procedures from the same file.
2356 The namespace of the gsymbol is resolved and then, once this is
2357 done the interface is checked. */
2358
2359
2360 static bool
2361 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2362 {
2363 if (!gsym_ns->proc_name->attr.recursive)
2364 return true;
2365
2366 if (sym->ns == gsym_ns)
2367 return false;
2368
2369 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2370 return false;
2371
2372 return true;
2373 }
2374
2375 static bool
2376 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2377 {
2378 if (gsym_ns->entries)
2379 {
2380 gfc_entry_list *entry = gsym_ns->entries;
2381
2382 for (; entry; entry = entry->next)
2383 {
2384 if (strcmp (sym->name, entry->sym->name) == 0)
2385 {
2386 if (strcmp (gsym_ns->proc_name->name,
2387 sym->ns->proc_name->name) == 0)
2388 return false;
2389
2390 if (sym->ns->parent
2391 && strcmp (gsym_ns->proc_name->name,
2392 sym->ns->parent->proc_name->name) == 0)
2393 return false;
2394 }
2395 }
2396 }
2397 return true;
2398 }
2399
2400
2401 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2402
2403 bool
2404 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2405 {
2406 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2407
2408 for ( ; arg; arg = arg->next)
2409 {
2410 if (!arg->sym)
2411 continue;
2412
2413 if (arg->sym->attr.allocatable) /* (2a) */
2414 {
2415 strncpy (errmsg, _("allocatable argument"), err_len);
2416 return true;
2417 }
2418 else if (arg->sym->attr.asynchronous)
2419 {
2420 strncpy (errmsg, _("asynchronous argument"), err_len);
2421 return true;
2422 }
2423 else if (arg->sym->attr.optional)
2424 {
2425 strncpy (errmsg, _("optional argument"), err_len);
2426 return true;
2427 }
2428 else if (arg->sym->attr.pointer)
2429 {
2430 strncpy (errmsg, _("pointer argument"), err_len);
2431 return true;
2432 }
2433 else if (arg->sym->attr.target)
2434 {
2435 strncpy (errmsg, _("target argument"), err_len);
2436 return true;
2437 }
2438 else if (arg->sym->attr.value)
2439 {
2440 strncpy (errmsg, _("value argument"), err_len);
2441 return true;
2442 }
2443 else if (arg->sym->attr.volatile_)
2444 {
2445 strncpy (errmsg, _("volatile argument"), err_len);
2446 return true;
2447 }
2448 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2449 {
2450 strncpy (errmsg, _("assumed-shape argument"), err_len);
2451 return true;
2452 }
2453 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2454 {
2455 strncpy (errmsg, _("assumed-rank argument"), err_len);
2456 return true;
2457 }
2458 else if (arg->sym->attr.codimension) /* (2c) */
2459 {
2460 strncpy (errmsg, _("coarray argument"), err_len);
2461 return true;
2462 }
2463 else if (false) /* (2d) TODO: parametrized derived type */
2464 {
2465 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2466 return true;
2467 }
2468 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2469 {
2470 strncpy (errmsg, _("polymorphic argument"), err_len);
2471 return true;
2472 }
2473 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2474 {
2475 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2476 return true;
2477 }
2478 else if (arg->sym->ts.type == BT_ASSUMED)
2479 {
2480 /* As assumed-type is unlimited polymorphic (cf. above).
2481 See also TS 29113, Note 6.1. */
2482 strncpy (errmsg, _("assumed-type argument"), err_len);
2483 return true;
2484 }
2485 }
2486
2487 if (sym->attr.function)
2488 {
2489 gfc_symbol *res = sym->result ? sym->result : sym;
2490
2491 if (res->attr.dimension) /* (3a) */
2492 {
2493 strncpy (errmsg, _("array result"), err_len);
2494 return true;
2495 }
2496 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2497 {
2498 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2499 return true;
2500 }
2501 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2502 && res->ts.u.cl->length
2503 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2504 {
2505 strncpy (errmsg, _("result with non-constant character length"), err_len);
2506 return true;
2507 }
2508 }
2509
2510 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2511 {
2512 strncpy (errmsg, _("elemental procedure"), err_len);
2513 return true;
2514 }
2515 else if (sym->attr.is_bind_c) /* (5) */
2516 {
2517 strncpy (errmsg, _("bind(c) procedure"), err_len);
2518 return true;
2519 }
2520
2521 return false;
2522 }
2523
2524
2525 static void
2526 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
2527 {
2528 gfc_gsymbol * gsym;
2529 gfc_namespace *ns;
2530 enum gfc_symbol_type type;
2531 char reason[200];
2532
2533 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2534
2535 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
2536 sym->binding_label != NULL);
2537
2538 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2539 gfc_global_used (gsym, where);
2540
2541 if ((sym->attr.if_source == IFSRC_UNKNOWN
2542 || sym->attr.if_source == IFSRC_IFBODY)
2543 && gsym->type != GSYM_UNKNOWN
2544 && !gsym->binding_label
2545 && gsym->ns
2546 && gsym->ns->proc_name
2547 && not_in_recursive (sym, gsym->ns)
2548 && not_entry_self_reference (sym, gsym->ns))
2549 {
2550 gfc_symbol *def_sym;
2551 def_sym = gsym->ns->proc_name;
2552
2553 if (gsym->ns->resolved != -1)
2554 {
2555
2556 /* Resolve the gsymbol namespace if needed. */
2557 if (!gsym->ns->resolved)
2558 {
2559 gfc_symbol *old_dt_list;
2560
2561 /* Stash away derived types so that the backend_decls
2562 do not get mixed up. */
2563 old_dt_list = gfc_derived_types;
2564 gfc_derived_types = NULL;
2565
2566 gfc_resolve (gsym->ns);
2567
2568 /* Store the new derived types with the global namespace. */
2569 if (gfc_derived_types)
2570 gsym->ns->derived_types = gfc_derived_types;
2571
2572 /* Restore the derived types of this namespace. */
2573 gfc_derived_types = old_dt_list;
2574 }
2575
2576 /* Make sure that translation for the gsymbol occurs before
2577 the procedure currently being resolved. */
2578 ns = gfc_global_ns_list;
2579 for (; ns && ns != gsym->ns; ns = ns->sibling)
2580 {
2581 if (ns->sibling == gsym->ns)
2582 {
2583 ns->sibling = gsym->ns->sibling;
2584 gsym->ns->sibling = gfc_global_ns_list;
2585 gfc_global_ns_list = gsym->ns;
2586 break;
2587 }
2588 }
2589
2590 /* This can happen if a binding name has been specified. */
2591 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2592 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2593
2594 if (def_sym->attr.entry_master || def_sym->attr.entry)
2595 {
2596 gfc_entry_list *entry;
2597 for (entry = gsym->ns->entries; entry; entry = entry->next)
2598 if (strcmp (entry->sym->name, sym->name) == 0)
2599 {
2600 def_sym = entry->sym;
2601 break;
2602 }
2603 }
2604 }
2605
2606 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2607 {
2608 gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2609 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2610 gfc_typename (&def_sym->ts));
2611 goto done;
2612 }
2613
2614 if (sym->attr.if_source == IFSRC_UNKNOWN
2615 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2616 {
2617 gfc_error ("Explicit interface required for %qs at %L: %s",
2618 sym->name, &sym->declared_at, reason);
2619 goto done;
2620 }
2621
2622 bool bad_result_characteristics;
2623 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2624 reason, sizeof(reason), NULL, NULL,
2625 &bad_result_characteristics))
2626 {
2627 /* Turn erros into warnings with -std=gnu and -std=legacy,
2628 unless a function returns a wrong type, which can lead
2629 to all kinds of ICEs and wrong code. */
2630
2631 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
2632 && !bad_result_characteristics)
2633 gfc_errors_to_warnings (true);
2634
2635 gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
2636 sym->name, &sym->declared_at, reason);
2637 sym->error = 1;
2638 gfc_errors_to_warnings (false);
2639 goto done;
2640 }
2641 }
2642
2643 done:
2644
2645 if (gsym->type == GSYM_UNKNOWN)
2646 {
2647 gsym->type = type;
2648 gsym->where = *where;
2649 }
2650
2651 gsym->used = 1;
2652 }
2653
2654
2655 /************* Function resolution *************/
2656
2657 /* Resolve a function call known to be generic.
2658 Section 14.1.2.4.1. */
2659
2660 static match
2661 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2662 {
2663 gfc_symbol *s;
2664
2665 if (sym->attr.generic)
2666 {
2667 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2668 if (s != NULL)
2669 {
2670 expr->value.function.name = s->name;
2671 expr->value.function.esym = s;
2672
2673 if (s->ts.type != BT_UNKNOWN)
2674 expr->ts = s->ts;
2675 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2676 expr->ts = s->result->ts;
2677
2678 if (s->as != NULL)
2679 expr->rank = s->as->rank;
2680 else if (s->result != NULL && s->result->as != NULL)
2681 expr->rank = s->result->as->rank;
2682
2683 gfc_set_sym_referenced (expr->value.function.esym);
2684
2685 return MATCH_YES;
2686 }
2687
2688 /* TODO: Need to search for elemental references in generic
2689 interface. */
2690 }
2691
2692 if (sym->attr.intrinsic)
2693 return gfc_intrinsic_func_interface (expr, 0);
2694
2695 return MATCH_NO;
2696 }
2697
2698
2699 static bool
2700 resolve_generic_f (gfc_expr *expr)
2701 {
2702 gfc_symbol *sym;
2703 match m;
2704 gfc_interface *intr = NULL;
2705
2706 sym = expr->symtree->n.sym;
2707
2708 for (;;)
2709 {
2710 m = resolve_generic_f0 (expr, sym);
2711 if (m == MATCH_YES)
2712 return true;
2713 else if (m == MATCH_ERROR)
2714 return false;
2715
2716 generic:
2717 if (!intr)
2718 for (intr = sym->generic; intr; intr = intr->next)
2719 if (gfc_fl_struct (intr->sym->attr.flavor))
2720 break;
2721
2722 if (sym->ns->parent == NULL)
2723 break;
2724 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2725
2726 if (sym == NULL)
2727 break;
2728 if (!generic_sym (sym))
2729 goto generic;
2730 }
2731
2732 /* Last ditch attempt. See if the reference is to an intrinsic
2733 that possesses a matching interface. 14.1.2.4 */
2734 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2735 {
2736 if (gfc_init_expr_flag)
2737 gfc_error ("Function %qs in initialization expression at %L "
2738 "must be an intrinsic function",
2739 expr->symtree->n.sym->name, &expr->where);
2740 else
2741 gfc_error ("There is no specific function for the generic %qs "
2742 "at %L", expr->symtree->n.sym->name, &expr->where);
2743 return false;
2744 }
2745
2746 if (intr)
2747 {
2748 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2749 NULL, false))
2750 return false;
2751 if (!gfc_use_derived (expr->ts.u.derived))
2752 return false;
2753 return resolve_structure_cons (expr, 0);
2754 }
2755
2756 m = gfc_intrinsic_func_interface (expr, 0);
2757 if (m == MATCH_YES)
2758 return true;
2759
2760 if (m == MATCH_NO)
2761 gfc_error ("Generic function %qs at %L is not consistent with a "
2762 "specific intrinsic interface", expr->symtree->n.sym->name,
2763 &expr->where);
2764
2765 return false;
2766 }
2767
2768
2769 /* Resolve a function call known to be specific. */
2770
2771 static match
2772 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2773 {
2774 match m;
2775
2776 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2777 {
2778 if (sym->attr.dummy)
2779 {
2780 sym->attr.proc = PROC_DUMMY;
2781 goto found;
2782 }
2783
2784 sym->attr.proc = PROC_EXTERNAL;
2785 goto found;
2786 }
2787
2788 if (sym->attr.proc == PROC_MODULE
2789 || sym->attr.proc == PROC_ST_FUNCTION
2790 || sym->attr.proc == PROC_INTERNAL)
2791 goto found;
2792
2793 if (sym->attr.intrinsic)
2794 {
2795 m = gfc_intrinsic_func_interface (expr, 1);
2796 if (m == MATCH_YES)
2797 return MATCH_YES;
2798 if (m == MATCH_NO)
2799 gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
2800 "with an intrinsic", sym->name, &expr->where);
2801
2802 return MATCH_ERROR;
2803 }
2804
2805 return MATCH_NO;
2806
2807 found:
2808 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2809
2810 if (sym->result)
2811 expr->ts = sym->result->ts;
2812 else
2813 expr->ts = sym->ts;
2814 expr->value.function.name = sym->name;
2815 expr->value.function.esym = sym;
2816 /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2817 error(s). */
2818 if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
2819 return MATCH_ERROR;
2820 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2821 expr->rank = CLASS_DATA (sym)->as->rank;
2822 else if (sym->as != NULL)
2823 expr->rank = sym->as->rank;
2824
2825 return MATCH_YES;
2826 }
2827
2828
2829 static bool
2830 resolve_specific_f (gfc_expr *expr)
2831 {
2832 gfc_symbol *sym;
2833 match m;
2834
2835 sym = expr->symtree->n.sym;
2836
2837 for (;;)
2838 {
2839 m = resolve_specific_f0 (sym, expr);
2840 if (m == MATCH_YES)
2841 return true;
2842 if (m == MATCH_ERROR)
2843 return false;
2844
2845 if (sym->ns->parent == NULL)
2846 break;
2847
2848 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2849
2850 if (sym == NULL)
2851 break;
2852 }
2853
2854 gfc_error ("Unable to resolve the specific function %qs at %L",
2855 expr->symtree->n.sym->name, &expr->where);
2856
2857 return true;
2858 }
2859
2860 /* Recursively append candidate SYM to CANDIDATES. Store the number of
2861 candidates in CANDIDATES_LEN. */
2862
2863 static void
2864 lookup_function_fuzzy_find_candidates (gfc_symtree *sym,
2865 char **&candidates,
2866 size_t &candidates_len)
2867 {
2868 gfc_symtree *p;
2869
2870 if (sym == NULL)
2871 return;
2872 if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external)
2873 && sym->n.sym->attr.flavor == FL_PROCEDURE)
2874 vec_push (candidates, candidates_len, sym->name);
2875
2876 p = sym->left;
2877 if (p)
2878 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
2879
2880 p = sym->right;
2881 if (p)
2882 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
2883 }
2884
2885
2886 /* Lookup function FN fuzzily, taking names in SYMROOT into account. */
2887
2888 const char*
2889 gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot)
2890 {
2891 char **candidates = NULL;
2892 size_t candidates_len = 0;
2893 lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len);
2894 return gfc_closest_fuzzy_match (fn, candidates);
2895 }
2896
2897
2898 /* Resolve a procedure call not known to be generic nor specific. */
2899
2900 static bool
2901 resolve_unknown_f (gfc_expr *expr)
2902 {
2903 gfc_symbol *sym;
2904 gfc_typespec *ts;
2905
2906 sym = expr->symtree->n.sym;
2907
2908 if (sym->attr.dummy)
2909 {
2910 sym->attr.proc = PROC_DUMMY;
2911 expr->value.function.name = sym->name;
2912 goto set_type;
2913 }
2914
2915 /* See if we have an intrinsic function reference. */
2916
2917 if (gfc_is_intrinsic (sym, 0, expr->where))
2918 {
2919 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2920 return true;
2921 return false;
2922 }
2923
2924 /* The reference is to an external name. */
2925
2926 sym->attr.proc = PROC_EXTERNAL;
2927 expr->value.function.name = sym->name;
2928 expr->value.function.esym = expr->symtree->n.sym;
2929
2930 if (sym->as != NULL)
2931 expr->rank = sym->as->rank;
2932
2933 /* Type of the expression is either the type of the symbol or the
2934 default type of the symbol. */
2935
2936 set_type:
2937 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2938
2939 if (sym->ts.type != BT_UNKNOWN)
2940 expr->ts = sym->ts;
2941 else
2942 {
2943 ts = gfc_get_default_type (sym->name, sym->ns);
2944
2945 if (ts->type == BT_UNKNOWN)
2946 {
2947 const char *guessed
2948 = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
2949 if (guessed)
2950 gfc_error ("Function %qs at %L has no IMPLICIT type"
2951 "; did you mean %qs?",
2952 sym->name, &expr->where, guessed);
2953 else
2954 gfc_error ("Function %qs at %L has no IMPLICIT type",
2955 sym->name, &expr->where);
2956 return false;
2957 }
2958 else
2959 expr->ts = *ts;
2960 }
2961
2962 return true;
2963 }
2964
2965
2966 /* Return true, if the symbol is an external procedure. */
2967 static bool
2968 is_external_proc (gfc_symbol *sym)
2969 {
2970 if (!sym->attr.dummy && !sym->attr.contained
2971 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
2972 && sym->attr.proc != PROC_ST_FUNCTION
2973 && !sym->attr.proc_pointer
2974 && !sym->attr.use_assoc
2975 && sym->name)
2976 return true;
2977
2978 return false;
2979 }
2980
2981
2982 /* Figure out if a function reference is pure or not. Also set the name
2983 of the function for a potential error message. Return nonzero if the
2984 function is PURE, zero if not. */
2985 static int
2986 pure_stmt_function (gfc_expr *, gfc_symbol *);
2987
2988 int
2989 gfc_pure_function (gfc_expr *e, const char **name)
2990 {
2991 int pure;
2992 gfc_component *comp;
2993
2994 *name = NULL;
2995
2996 if (e->symtree != NULL
2997 && e->symtree->n.sym != NULL
2998 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2999 return pure_stmt_function (e, e->symtree->n.sym);
3000
3001 comp = gfc_get_proc_ptr_comp (e);
3002 if (comp)
3003 {
3004 pure = gfc_pure (comp->ts.interface);
3005 *name = comp->name;
3006 }
3007 else if (e->value.function.esym)
3008 {
3009 pure = gfc_pure (e->value.function.esym);
3010 *name = e->value.function.esym->name;
3011 }
3012 else if (e->value.function.isym)
3013 {
3014 pure = e->value.function.isym->pure
3015 || e->value.function.isym->elemental;
3016 *name = e->value.function.isym->name;
3017 }
3018 else
3019 {
3020 /* Implicit functions are not pure. */
3021 pure = 0;
3022 *name = e->value.function.name;
3023 }
3024
3025 return pure;
3026 }
3027
3028
3029 /* Check if the expression is a reference to an implicitly pure function. */
3030
3031 int
3032 gfc_implicit_pure_function (gfc_expr *e)
3033 {
3034 gfc_component *comp = gfc_get_proc_ptr_comp (e);
3035 if (comp)
3036 return gfc_implicit_pure (comp->ts.interface);
3037 else if (e->value.function.esym)
3038 return gfc_implicit_pure (e->value.function.esym);
3039 else
3040 return 0;
3041 }
3042
3043
3044 static bool
3045 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
3046 int *f ATTRIBUTE_UNUSED)
3047 {
3048 const char *name;
3049
3050 /* Don't bother recursing into other statement functions
3051 since they will be checked individually for purity. */
3052 if (e->expr_type != EXPR_FUNCTION
3053 || !e->symtree
3054 || e->symtree->n.sym == sym
3055 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3056 return false;
3057
3058 return gfc_pure_function (e, &name) ? false : true;
3059 }
3060
3061
3062 static int
3063 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
3064 {
3065 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
3066 }
3067
3068
3069 /* Check if an impure function is allowed in the current context. */
3070
3071 static bool check_pure_function (gfc_expr *e)
3072 {
3073 const char *name = NULL;
3074 if (!gfc_pure_function (e, &name) && name)
3075 {
3076 if (forall_flag)
3077 {
3078 gfc_error ("Reference to impure function %qs at %L inside a "
3079 "FORALL %s", name, &e->where,
3080 forall_flag == 2 ? "mask" : "block");
3081 return false;
3082 }
3083 else if (gfc_do_concurrent_flag)
3084 {
3085 gfc_error ("Reference to impure function %qs at %L inside a "
3086 "DO CONCURRENT %s", name, &e->where,
3087 gfc_do_concurrent_flag == 2 ? "mask" : "block");
3088 return false;
3089 }
3090 else if (gfc_pure (NULL))
3091 {
3092 gfc_error ("Reference to impure function %qs at %L "
3093 "within a PURE procedure", name, &e->where);
3094 return false;
3095 }
3096 if (!gfc_implicit_pure_function (e))
3097 gfc_unset_implicit_pure (NULL);
3098 }
3099 return true;
3100 }
3101
3102
3103 /* Update current procedure's array_outer_dependency flag, considering
3104 a call to procedure SYM. */
3105
3106 static void
3107 update_current_proc_array_outer_dependency (gfc_symbol *sym)
3108 {
3109 /* Check to see if this is a sibling function that has not yet
3110 been resolved. */
3111 gfc_namespace *sibling = gfc_current_ns->sibling;
3112 for (; sibling; sibling = sibling->sibling)
3113 {
3114 if (sibling->proc_name == sym)
3115 {
3116 gfc_resolve (sibling);
3117 break;
3118 }
3119 }
3120
3121 /* If SYM has references to outer arrays, so has the procedure calling
3122 SYM. If SYM is a procedure pointer, we can assume the worst. */
3123 if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
3124 && gfc_current_ns->proc_name)
3125 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3126 }
3127
3128
3129 /* Resolve a function call, which means resolving the arguments, then figuring
3130 out which entity the name refers to. */
3131
3132 static bool
3133 resolve_function (gfc_expr *expr)
3134 {
3135 gfc_actual_arglist *arg;
3136 gfc_symbol *sym;
3137 bool t;
3138 int temp;
3139 procedure_type p = PROC_INTRINSIC;
3140 bool no_formal_args;
3141
3142 sym = NULL;
3143 if (expr->symtree)
3144 sym = expr->symtree->n.sym;
3145
3146 /* If this is a procedure pointer component, it has already been resolved. */
3147 if (gfc_is_proc_ptr_comp (expr))
3148 return true;
3149
3150 /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
3151 another caf_get. */
3152 if (sym && sym->attr.intrinsic
3153 && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
3154 || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
3155 return true;
3156
3157 if (expr->ref)
3158 {
3159 gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name,
3160 &expr->where);
3161 return false;
3162 }
3163
3164 if (sym && sym->attr.intrinsic
3165 && !gfc_resolve_intrinsic (sym, &expr->where))
3166 return false;
3167
3168 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
3169 {
3170 gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
3171 return false;
3172 }
3173
3174 /* If this is a deferred TBP with an abstract interface (which may
3175 of course be referenced), expr->value.function.esym will be set. */
3176 if (sym && sym->attr.abstract && !expr->value.function.esym)
3177 {
3178 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3179 sym->name, &expr->where);
3180 return false;
3181 }
3182
3183 /* If this is a deferred TBP with an abstract interface, its result
3184 cannot be an assumed length character (F2003: C418). */
3185 if (sym && sym->attr.abstract && sym->attr.function
3186 && sym->result->ts.u.cl
3187 && sym->result->ts.u.cl->length == NULL
3188 && !sym->result->ts.deferred)
3189 {
3190 gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
3191 "character length result (F2008: C418)", sym->name,
3192 &sym->declared_at);
3193 return false;
3194 }
3195
3196 /* Switch off assumed size checking and do this again for certain kinds
3197 of procedure, once the procedure itself is resolved. */
3198 need_full_assumed_size++;
3199
3200 if (expr->symtree && expr->symtree->n.sym)
3201 p = expr->symtree->n.sym->attr.proc;
3202
3203 if (expr->value.function.isym && expr->value.function.isym->inquiry)
3204 inquiry_argument = true;
3205 no_formal_args = sym && is_external_proc (sym)
3206 && gfc_sym_get_dummy_args (sym) == NULL;
3207
3208 if (!resolve_actual_arglist (expr->value.function.actual,
3209 p, no_formal_args))
3210 {
3211 inquiry_argument = false;
3212 return false;
3213 }
3214
3215 inquiry_argument = false;
3216
3217 /* Resume assumed_size checking. */
3218 need_full_assumed_size--;
3219
3220 /* If the procedure is external, check for usage. */
3221 if (sym && is_external_proc (sym))
3222 resolve_global_procedure (sym, &expr->where, 0);
3223
3224 if (sym && sym->ts.type == BT_CHARACTER
3225 && sym->ts.u.cl
3226 && sym->ts.u.cl->length == NULL
3227 && !sym->attr.dummy
3228 && !sym->ts.deferred
3229 && expr->value.function.esym == NULL
3230 && !sym->attr.contained)
3231 {
3232 /* Internal procedures are taken care of in resolve_contained_fntype. */
3233 gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
3234 "be used at %L since it is not a dummy argument",
3235 sym->name, &expr->where);
3236 return false;
3237 }
3238
3239 /* See if function is already resolved. */
3240
3241 if (expr->value.function.name != NULL
3242 || expr->value.function.isym != NULL)
3243 {
3244 if (expr->ts.type == BT_UNKNOWN)
3245 expr->ts = sym->ts;
3246 t = true;
3247 }
3248 else
3249 {
3250 /* Apply the rules of section 14.1.2. */
3251
3252 switch (procedure_kind (sym))
3253 {
3254 case PTYPE_GENERIC:
3255 t = resolve_generic_f (expr);
3256 break;
3257
3258 case PTYPE_SPECIFIC:
3259 t = resolve_specific_f (expr);
3260 break;
3261
3262 case PTYPE_UNKNOWN:
3263 t = resolve_unknown_f (expr);
3264 break;
3265
3266 default:
3267 gfc_internal_error ("resolve_function(): bad function type");
3268 }
3269 }
3270
3271 /* If the expression is still a function (it might have simplified),
3272 then we check to see if we are calling an elemental function. */
3273
3274 if (expr->expr_type != EXPR_FUNCTION)
3275 return t;
3276
3277 /* Walk the argument list looking for invalid BOZ. */
3278 for (arg = expr->value.function.actual; arg; arg = arg->next)
3279 if (arg->expr && arg->expr->ts.type == BT_BOZ)
3280 {
3281 gfc_error ("A BOZ literal constant at %L cannot appear as an "
3282 "actual argument in a function reference",
3283 &arg->expr->where);
3284 return false;
3285 }
3286
3287 temp = need_full_assumed_size;
3288 need_full_assumed_size = 0;
3289
3290 if (!resolve_elemental_actual (expr, NULL))
3291 return false;
3292
3293 if (omp_workshare_flag
3294 && expr->value.function.esym
3295 && ! gfc_elemental (expr->value.function.esym))
3296 {
3297 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3298 "in WORKSHARE construct", expr->value.function.esym->name,
3299 &expr->where);
3300 t = false;
3301 }
3302
3303 #define GENERIC_ID expr->value.function.isym->id
3304 else if (expr->value.function.actual != NULL
3305 && expr->value.function.isym != NULL
3306 && GENERIC_ID != GFC_ISYM_LBOUND
3307 && GENERIC_ID != GFC_ISYM_LCOBOUND
3308 && GENERIC_ID != GFC_ISYM_UCOBOUND
3309 && GENERIC_ID != GFC_ISYM_LEN
3310 && GENERIC_ID != GFC_ISYM_LOC
3311 && GENERIC_ID != GFC_ISYM_C_LOC
3312 && GENERIC_ID != GFC_ISYM_PRESENT)
3313 {
3314 /* Array intrinsics must also have the last upper bound of an
3315 assumed size array argument. UBOUND and SIZE have to be
3316 excluded from the check if the second argument is anything
3317 than a constant. */
3318
3319 for (arg = expr->value.function.actual; arg; arg = arg->next)
3320 {
3321 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3322 && arg == expr->value.function.actual
3323 && arg->next != NULL && arg->next->expr)
3324 {
3325 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3326 break;
3327
3328 if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
3329 break;
3330
3331 if ((int)mpz_get_si (arg->next->expr->value.integer)
3332 < arg->expr->rank)
3333 break;
3334 }
3335
3336 if (arg->expr != NULL
3337 && arg->expr->rank > 0
3338 && resolve_assumed_size_actual (arg->expr))
3339 return false;
3340 }
3341 }
3342 #undef GENERIC_ID
3343
3344 need_full_assumed_size = temp;
3345
3346 if (!check_pure_function(expr))
3347 t = false;
3348
3349 /* Functions without the RECURSIVE attribution are not allowed to
3350 * call themselves. */
3351 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3352 {
3353 gfc_symbol *esym;
3354 esym = expr->value.function.esym;
3355
3356 if (is_illegal_recursion (esym, gfc_current_ns))
3357 {
3358 if (esym->attr.entry && esym->ns->entries)
3359 gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3360 " function %qs is not RECURSIVE",
3361 esym->name, &expr->where, esym->ns->entries->sym->name);
3362 else
3363 gfc_error ("Function %qs at %L cannot be called recursively, as it"
3364 " is not RECURSIVE", esym->name, &expr->where);
3365
3366 t = false;
3367 }
3368 }
3369
3370 /* Character lengths of use associated functions may contains references to
3371 symbols not referenced from the current program unit otherwise. Make sure
3372 those symbols are marked as referenced. */
3373
3374 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3375 && expr->value.function.esym->attr.use_assoc)
3376 {
3377 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3378 }
3379
3380 /* Make sure that the expression has a typespec that works. */
3381 if (expr->ts.type == BT_UNKNOWN)
3382 {
3383 if (expr->symtree->n.sym->result
3384 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3385 && !expr->symtree->n.sym->result->attr.proc_pointer)
3386 expr->ts = expr->symtree->n.sym->result->ts;
3387 }
3388
3389 if (!expr->ref && !expr->value.function.isym)
3390 {
3391 if (expr->value.function.esym)
3392 update_current_proc_array_outer_dependency (expr->value.function.esym);
3393 else
3394 update_current_proc_array_outer_dependency (sym);
3395 }
3396 else if (expr->ref)
3397 /* typebound procedure: Assume the worst. */
3398 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3399
3400 return t;
3401 }
3402
3403
3404 /************* Subroutine resolution *************/
3405
3406 static bool
3407 pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3408 {
3409 if (gfc_pure (sym))
3410 return true;
3411
3412 if (forall_flag)
3413 {
3414 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3415 name, loc);
3416 return false;
3417 }
3418 else if (gfc_do_concurrent_flag)
3419 {
3420 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3421 "PURE", name, loc);
3422 return false;
3423 }
3424 else if (gfc_pure (NULL))
3425 {
3426 gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3427 return false;
3428 }
3429
3430 gfc_unset_implicit_pure (NULL);
3431 return true;
3432 }
3433
3434
3435 static match
3436 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3437 {
3438 gfc_symbol *s;
3439
3440 if (sym->attr.generic)
3441 {
3442 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3443 if (s != NULL)
3444 {
3445 c->resolved_sym = s;
3446 if (!pure_subroutine (s, s->name, &c->loc))
3447 return MATCH_ERROR;
3448 return MATCH_YES;
3449 }
3450
3451 /* TODO: Need to search for elemental references in generic interface. */
3452 }
3453
3454 if (sym->attr.intrinsic)
3455 return gfc_intrinsic_sub_interface (c, 0);
3456
3457 return MATCH_NO;
3458 }
3459
3460
3461 static bool
3462 resolve_generic_s (gfc_code *c)
3463 {
3464 gfc_symbol *sym;
3465 match m;
3466
3467 sym = c->symtree->n.sym;
3468
3469 for (;;)
3470 {
3471 m = resolve_generic_s0 (c, sym);
3472 if (m == MATCH_YES)
3473 return true;
3474 else if (m == MATCH_ERROR)
3475 return false;
3476
3477 generic:
3478 if (sym->ns->parent == NULL)
3479 break;
3480 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3481
3482 if (sym == NULL)
3483 break;
3484 if (!generic_sym (sym))
3485 goto generic;
3486 }
3487
3488 /* Last ditch attempt. See if the reference is to an intrinsic
3489 that possesses a matching interface. 14.1.2.4 */
3490 sym = c->symtree->n.sym;
3491
3492 if (!gfc_is_intrinsic (sym, 1, c->loc))
3493 {
3494 gfc_error ("There is no specific subroutine for the generic %qs at %L",
3495 sym->name, &c->loc);
3496 return false;
3497 }
3498
3499 m = gfc_intrinsic_sub_interface (c, 0);
3500 if (m == MATCH_YES)
3501 return true;
3502 if (m == MATCH_NO)
3503 gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3504 "intrinsic subroutine interface", sym->name, &c->loc);
3505
3506 return false;
3507 }
3508
3509
3510 /* Resolve a subroutine call known to be specific. */
3511
3512 static match
3513 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3514 {
3515 match m;
3516
3517 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3518 {
3519 if (sym->attr.dummy)
3520 {
3521 sym->attr.proc = PROC_DUMMY;
3522 goto found;
3523 }
3524
3525 sym->attr.proc = PROC_EXTERNAL;
3526 goto found;
3527 }
3528
3529 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3530 goto found;
3531
3532 if (sym->attr.intrinsic)
3533 {
3534 m = gfc_intrinsic_sub_interface (c, 1);
3535 if (m == MATCH_YES)
3536 return MATCH_YES;
3537 if (m == MATCH_NO)
3538 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3539 "with an intrinsic", sym->name, &c->loc);
3540
3541 return MATCH_ERROR;
3542 }
3543
3544 return MATCH_NO;
3545
3546 found:
3547 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3548
3549 c->resolved_sym = sym;
3550 if (!pure_subroutine (sym, sym->name, &c->loc))
3551 return MATCH_ERROR;
3552
3553 return MATCH_YES;
3554 }
3555
3556
3557 static bool
3558 resolve_specific_s (gfc_code *c)
3559 {
3560 gfc_symbol *sym;
3561 match m;
3562
3563 sym = c->symtree->n.sym;
3564
3565 for (;;)
3566 {
3567 m = resolve_specific_s0 (c, sym);
3568 if (m == MATCH_YES)
3569 return true;
3570 if (m == MATCH_ERROR)
3571 return false;
3572
3573 if (sym->ns->parent == NULL)
3574 break;
3575
3576 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3577
3578 if (sym == NULL)
3579 break;
3580 }
3581
3582 sym = c->symtree->n.sym;
3583 gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3584 sym->name, &c->loc);
3585
3586 return false;
3587 }
3588
3589
3590 /* Resolve a subroutine call not known to be generic nor specific. */
3591
3592 static bool
3593 resolve_unknown_s (gfc_code *c)
3594 {
3595 gfc_symbol *sym;
3596
3597 sym = c->symtree->n.sym;
3598
3599 if (sym->attr.dummy)
3600 {
3601 sym->attr.proc = PROC_DUMMY;
3602 goto found;
3603 }
3604
3605 /* See if we have an intrinsic function reference. */
3606
3607 if (gfc_is_intrinsic (sym, 1, c->loc))
3608 {
3609 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3610 return true;
3611 return false;
3612 }
3613
3614 /* The reference is to an external name. */
3615
3616 found:
3617 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3618
3619 c->resolved_sym = sym;
3620
3621 return pure_subroutine (sym, sym->name, &c->loc);
3622 }
3623
3624
3625 /* Resolve a subroutine call. Although it was tempting to use the same code
3626 for functions, subroutines and functions are stored differently and this
3627 makes things awkward. */
3628
3629 static bool
3630 resolve_call (gfc_code *c)
3631 {
3632 bool t;
3633 procedure_type ptype = PROC_INTRINSIC;
3634 gfc_symbol *csym, *sym;
3635 bool no_formal_args;
3636
3637 csym = c->symtree ? c->symtree->n.sym : NULL;
3638
3639 if (csym && csym->ts.type != BT_UNKNOWN)
3640 {
3641 gfc_error ("%qs at %L has a type, which is not consistent with "
3642 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3643 return false;
3644 }
3645
3646 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3647 {
3648 gfc_symtree *st;
3649 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3650 sym = st ? st->n.sym : NULL;
3651 if (sym && csym != sym
3652 && sym->ns == gfc_current_ns
3653 && sym->attr.flavor == FL_PROCEDURE
3654 && sym->attr.contained)
3655 {
3656 sym->refs++;
3657 if (csym->attr.generic)
3658 c->symtree->n.sym = sym;
3659 else
3660 c->symtree = st;
3661 csym = c->symtree->n.sym;
3662 }
3663 }
3664
3665 /* If this ia a deferred TBP, c->expr1 will be set. */
3666 if (!c->expr1 && csym)
3667 {
3668 if (csym->attr.abstract)
3669 {
3670 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3671 csym->name, &c->loc);
3672 return false;
3673 }
3674
3675 /* Subroutines without the RECURSIVE attribution are not allowed to
3676 call themselves. */
3677 if (is_illegal_recursion (csym, gfc_current_ns))
3678 {
3679 if (csym->attr.entry && csym->ns->entries)
3680 gfc_error ("ENTRY %qs at %L cannot be called recursively, "
3681 "as subroutine %qs is not RECURSIVE",
3682 csym->name, &c->loc, csym->ns->entries->sym->name);
3683 else
3684 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
3685 "as it is not RECURSIVE", csym->name, &c->loc);
3686
3687 t = false;
3688 }
3689 }
3690
3691 /* Switch off assumed size checking and do this again for certain kinds
3692 of procedure, once the procedure itself is resolved. */
3693 need_full_assumed_size++;
3694
3695 if (csym)
3696 ptype = csym->attr.proc;
3697
3698 no_formal_args = csym && is_external_proc (csym)
3699 && gfc_sym_get_dummy_args (csym) == NULL;
3700 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3701 return false;
3702
3703 /* Resume assumed_size checking. */
3704 need_full_assumed_size--;
3705
3706 /* If external, check for usage. */
3707 if (csym && is_external_proc (csym))
3708 resolve_global_procedure (csym, &c->loc, 1);
3709
3710 t = true;
3711 if (c->resolved_sym == NULL)
3712 {
3713 c->resolved_isym = NULL;
3714 switch (procedure_kind (csym))
3715 {
3716 case PTYPE_GENERIC:
3717 t = resolve_generic_s (c);
3718 break;
3719
3720 case PTYPE_SPECIFIC:
3721 t = resolve_specific_s (c);
3722 break;
3723
3724 case PTYPE_UNKNOWN:
3725 t = resolve_unknown_s (c);
3726 break;
3727
3728 default:
3729 gfc_internal_error ("resolve_subroutine(): bad function type");
3730 }
3731 }
3732
3733 /* Some checks of elemental subroutine actual arguments. */
3734 if (!resolve_elemental_actual (NULL, c))
3735 return false;
3736
3737 if (!c->expr1)
3738 update_current_proc_array_outer_dependency (csym);
3739 else
3740 /* Typebound procedure: Assume the worst. */
3741 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3742
3743 return t;
3744 }
3745
3746
3747 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3748 op1->shape and op2->shape are non-NULL return true if their shapes
3749 match. If both op1->shape and op2->shape are non-NULL return false
3750 if their shapes do not match. If either op1->shape or op2->shape is
3751 NULL, return true. */
3752
3753 static bool
3754 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3755 {
3756 bool t;
3757 int i;
3758
3759 t = true;
3760
3761 if (op1->shape != NULL && op2->shape != NULL)
3762 {
3763 for (i = 0; i < op1->rank; i++)
3764 {
3765 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3766 {
3767 gfc_error ("Shapes for operands at %L and %L are not conformable",
3768 &op1->where, &op2->where);
3769 t = false;
3770 break;
3771 }
3772 }
3773 }
3774
3775 return t;
3776 }
3777
3778 /* Convert a logical operator to the corresponding bitwise intrinsic call.
3779 For example A .AND. B becomes IAND(A, B). */
3780 static gfc_expr *
3781 logical_to_bitwise (gfc_expr *e)
3782 {
3783 gfc_expr *tmp, *op1, *op2;
3784 gfc_isym_id isym;
3785 gfc_actual_arglist *args = NULL;
3786
3787 gcc_assert (e->expr_type == EXPR_OP);
3788
3789 isym = GFC_ISYM_NONE;
3790 op1 = e->value.op.op1;
3791 op2 = e->value.op.op2;
3792
3793 switch (e->value.op.op)
3794 {
3795 case INTRINSIC_NOT:
3796 isym = GFC_ISYM_NOT;
3797 break;
3798 case INTRINSIC_AND:
3799 isym = GFC_ISYM_IAND;
3800 break;
3801 case INTRINSIC_OR:
3802 isym = GFC_ISYM_IOR;
3803 break;
3804 case INTRINSIC_NEQV:
3805 isym = GFC_ISYM_IEOR;
3806 break;
3807 case INTRINSIC_EQV:
3808 /* "Bitwise eqv" is just the complement of NEQV === IEOR.
3809 Change the old expression to NEQV, which will get replaced by IEOR,
3810 and wrap it in NOT. */
3811 tmp = gfc_copy_expr (e);
3812 tmp->value.op.op = INTRINSIC_NEQV;
3813 tmp = logical_to_bitwise (tmp);
3814 isym = GFC_ISYM_NOT;
3815 op1 = tmp;
3816 op2 = NULL;
3817 break;
3818 default:
3819 gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
3820 }
3821
3822 /* Inherit the original operation's operands as arguments. */
3823 args = gfc_get_actual_arglist ();
3824 args->expr = op1;
3825 if (op2)
3826 {
3827 args->next = gfc_get_actual_arglist ();
3828 args->next->expr = op2;
3829 }
3830
3831 /* Convert the expression to a function call. */
3832 e->expr_type = EXPR_FUNCTION;
3833 e->value.function.actual = args;
3834 e->value.function.isym = gfc_intrinsic_function_by_id (isym);
3835 e->value.function.name = e->value.function.isym->name;
3836 e->value.function.esym = NULL;
3837
3838 /* Make up a pre-resolved function call symtree if we need to. */
3839 if (!e->symtree || !e->symtree->n.sym)
3840 {
3841 gfc_symbol *sym;
3842 gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
3843 sym = e->symtree->n.sym;
3844 sym->result = sym;
3845 sym->attr.flavor = FL_PROCEDURE;
3846 sym->attr.function = 1;
3847 sym->attr.elemental = 1;
3848 sym->attr.pure = 1;
3849 sym->attr.referenced = 1;
3850 gfc_intrinsic_symbol (sym);
3851 gfc_commit_symbol (sym);
3852 }
3853
3854 args->name = e->value.function.isym->formal->name;
3855 if (e->value.function.isym->formal->next)
3856 args->next->name = e->value.function.isym->formal->next->name;
3857
3858 return e;
3859 }
3860
3861 /* Recursively append candidate UOP to CANDIDATES. Store the number of
3862 candidates in CANDIDATES_LEN. */
3863 static void
3864 lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
3865 char **&candidates,
3866 size_t &candidates_len)
3867 {
3868 gfc_symtree *p;
3869
3870 if (uop == NULL)
3871 return;
3872
3873 /* Not sure how to properly filter here. Use all for a start.
3874 n.uop.op is NULL for empty interface operators (is that legal?) disregard
3875 these as i suppose they don't make terribly sense. */
3876
3877 if (uop->n.uop->op != NULL)
3878 vec_push (candidates, candidates_len, uop->name);
3879
3880 p = uop->left;
3881 if (p)
3882 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
3883
3884 p = uop->right;
3885 if (p)
3886 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
3887 }
3888
3889 /* Lookup user-operator OP fuzzily, taking names in UOP into account. */
3890
3891 static const char*
3892 lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
3893 {
3894 char **candidates = NULL;
3895 size_t candidates_len = 0;
3896 lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
3897 return gfc_closest_fuzzy_match (op, candidates);
3898 }
3899
3900
3901 /* Callback finding an impure function as an operand to an .and. or
3902 .or. expression. Remember the last function warned about to
3903 avoid double warnings when recursing. */
3904
3905 static int
3906 impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
3907 void *data)
3908 {
3909 gfc_expr *f = *e;
3910 const char *name;
3911 static gfc_expr *last = NULL;
3912 bool *found = (bool *) data;
3913
3914 if (f->expr_type == EXPR_FUNCTION)
3915 {
3916 *found = 1;
3917 if (f != last && !gfc_pure_function (f, &name)
3918 && !gfc_implicit_pure_function (f))
3919 {
3920 if (name)
3921 gfc_warning (OPT_Wfunction_elimination,
3922 "Impure function %qs at %L might not be evaluated",
3923 name, &f->where);
3924 else
3925 gfc_warning (OPT_Wfunction_elimination,
3926 "Impure function at %L might not be evaluated",
3927 &f->where);
3928 }
3929 last = f;
3930 }
3931
3932 return 0;
3933 }
3934
3935 /* Return true if TYPE is character based, false otherwise. */
3936
3937 static int
3938 is_character_based (bt type)
3939 {
3940 return type == BT_CHARACTER || type == BT_HOLLERITH;
3941 }
3942
3943
3944 /* If expression is a hollerith, convert it to character and issue a warning
3945 for the conversion. */
3946
3947 static void
3948 convert_hollerith_to_character (gfc_expr *e)
3949 {
3950 if (e->ts.type == BT_HOLLERITH)
3951 {
3952 gfc_typespec t;
3953 gfc_clear_ts (&t);
3954 t.type = BT_CHARACTER;
3955 t.kind = e->ts.kind;
3956 gfc_convert_type_warn (e, &t, 2, 1);
3957 }
3958 }
3959
3960 /* Convert to numeric and issue a warning for the conversion. */
3961
3962 static void
3963 convert_to_numeric (gfc_expr *a, gfc_expr *b)
3964 {
3965 gfc_typespec t;
3966 gfc_clear_ts (&t);
3967 t.type = b->ts.type;
3968 t.kind = b->ts.kind;
3969 gfc_convert_type_warn (a, &t, 2, 1);
3970 }
3971
3972 /* Resolve an operator expression node. This can involve replacing the
3973 operation with a user defined function call. */
3974
3975 static bool
3976 resolve_operator (gfc_expr *e)
3977 {
3978 gfc_expr *op1, *op2;
3979 char msg[200];
3980 bool dual_locus_error;
3981 bool t = true;
3982
3983 /* Resolve all subnodes-- give them types. */
3984
3985 switch (e->value.op.op)
3986 {
3987 default:
3988 if (!gfc_resolve_expr (e->value.op.op2))
3989 return false;
3990
3991 /* Fall through. */
3992
3993 case INTRINSIC_NOT:
3994 case INTRINSIC_UPLUS:
3995 case INTRINSIC_UMINUS:
3996 case INTRINSIC_PARENTHESES:
3997 if (!gfc_resolve_expr (e->value.op.op1))
3998 return false;
3999 if (e->value.op.op1
4000 && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
4001 {
4002 gfc_error ("BOZ literal constant at %L cannot be an operand of "
4003 "unary operator %qs", &e->value.op.op1->where,
4004 gfc_op2string (e->value.op.op));
4005 return false;
4006 }
4007 break;
4008 }
4009
4010 /* Typecheck the new node. */
4011
4012 op1 = e->value.op.op1;
4013 op2 = e->value.op.op2;
4014 if (op1 == NULL && op2 == NULL)
4015 return false;
4016
4017 dual_locus_error = false;
4018
4019 /* op1 and op2 cannot both be BOZ. */
4020 if (op1 && op1->ts.type == BT_BOZ
4021 && op2 && op2->ts.type == BT_BOZ)
4022 {
4023 gfc_error ("Operands at %L and %L cannot appear as operands of "
4024 "binary operator %qs", &op1->where, &op2->where,
4025 gfc_op2string (e->value.op.op));
4026 return false;
4027 }
4028
4029 if ((op1 && op1->expr_type == EXPR_NULL)
4030 || (op2 && op2->expr_type == EXPR_NULL))
4031 {
4032 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
4033 goto bad_op;
4034 }
4035
4036 switch (e->value.op.op)
4037 {
4038 case INTRINSIC_UPLUS:
4039 case INTRINSIC_UMINUS:
4040 if (op1->ts.type == BT_INTEGER
4041 || op1->ts.type == BT_REAL
4042 || op1->ts.type == BT_COMPLEX)
4043 {
4044 e->ts = op1->ts;
4045 break;
4046 }
4047
4048 sprintf (msg, _("Operand of unary numeric operator %%<%s%%> at %%L is %s"),
4049 gfc_op2string (e->value.op.op), gfc_typename (e));
4050 goto bad_op;
4051
4052 case INTRINSIC_PLUS:
4053 case INTRINSIC_MINUS:
4054 case INTRINSIC_TIMES:
4055 case INTRINSIC_DIVIDE:
4056 case INTRINSIC_POWER:
4057 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4058 {
4059 gfc_type_convert_binary (e, 1);
4060 break;
4061 }
4062
4063 if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
4064 sprintf (msg,
4065 _("Unexpected derived-type entities in binary intrinsic "
4066 "numeric operator %%<%s%%> at %%L"),
4067 gfc_op2string (e->value.op.op));
4068 else
4069 sprintf (msg,
4070 _("Operands of binary numeric operator %%<%s%%> at %%L are %s/%s"),
4071 gfc_op2string (e->value.op.op), gfc_typename (op1),
4072 gfc_typename (op2));
4073 goto bad_op;
4074
4075 case INTRINSIC_CONCAT:
4076 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4077 && op1->ts.kind == op2->ts.kind)
4078 {
4079 e->ts.type = BT_CHARACTER;
4080 e->ts.kind = op1->ts.kind;
4081 break;
4082 }
4083
4084 sprintf (msg,
4085 _("Operands of string concatenation operator at %%L are %s/%s"),
4086 gfc_typename (op1), gfc_typename (op2));
4087 goto bad_op;
4088
4089 case INTRINSIC_AND:
4090 case INTRINSIC_OR:
4091 case INTRINSIC_EQV:
4092 case INTRINSIC_NEQV:
4093 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4094 {
4095 e->ts.type = BT_LOGICAL;
4096 e->ts.kind = gfc_kind_max (op1, op2);
4097 if (op1->ts.kind < e->ts.kind)
4098 gfc_convert_type (op1, &e->ts, 2);
4099 else if (op2->ts.kind < e->ts.kind)
4100 gfc_convert_type (op2, &e->ts, 2);
4101
4102 if (flag_frontend_optimize &&
4103 (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
4104 {
4105 /* Warn about short-circuiting
4106 with impure function as second operand. */
4107 bool op2_f = false;
4108 gfc_expr_walker (&op2, impure_function_callback, &op2_f);
4109 }
4110 break;
4111 }
4112
4113 /* Logical ops on integers become bitwise ops with -fdec. */
4114 else if (flag_dec
4115 && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
4116 {
4117 e->ts.type = BT_INTEGER;
4118 e->ts.kind = gfc_kind_max (op1, op2);
4119 if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
4120 gfc_convert_type (op1, &e->ts, 1);
4121 if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
4122 gfc_convert_type (op2, &e->ts, 1);
4123 e = logical_to_bitwise (e);
4124 goto simplify_op;
4125 }
4126
4127 sprintf (msg, _("Operands of logical operator %%<%s%%> at %%L are %s/%s"),
4128 gfc_op2string (e->value.op.op), gfc_typename (op1),
4129 gfc_typename (op2));
4130
4131 goto bad_op;
4132
4133 case INTRINSIC_NOT:
4134 /* Logical ops on integers become bitwise ops with -fdec. */
4135 if (flag_dec && op1->ts.type == BT_INTEGER)
4136 {
4137 e->ts.type = BT_INTEGER;
4138 e->ts.kind = op1->ts.kind;
4139 e = logical_to_bitwise (e);
4140 goto simplify_op;
4141 }
4142
4143 if (op1->ts.type == BT_LOGICAL)
4144 {
4145 e->ts.type = BT_LOGICAL;
4146 e->ts.kind = op1->ts.kind;
4147 break;
4148 }
4149
4150 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
4151 gfc_typename (op1));
4152 goto bad_op;
4153
4154 case INTRINSIC_GT:
4155 case INTRINSIC_GT_OS:
4156 case INTRINSIC_GE:
4157 case INTRINSIC_GE_OS:
4158 case INTRINSIC_LT:
4159 case INTRINSIC_LT_OS:
4160 case INTRINSIC_LE:
4161 case INTRINSIC_LE_OS:
4162 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
4163 {
4164 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
4165 goto bad_op;
4166 }
4167
4168 /* Fall through. */
4169
4170 case INTRINSIC_EQ:
4171 case INTRINSIC_EQ_OS:
4172 case INTRINSIC_NE:
4173 case INTRINSIC_NE_OS:
4174
4175 if (flag_dec
4176 && is_character_based (op1->ts.type)
4177 && is_character_based (op2->ts.type))
4178 {
4179 convert_hollerith_to_character (op1);
4180 convert_hollerith_to_character (op2);
4181 }
4182
4183 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4184 && op1->ts.kind == op2->ts.kind)
4185 {
4186 e->ts.type = BT_LOGICAL;
4187 e->ts.kind = gfc_default_logical_kind;
4188 break;
4189 }
4190
4191 /* If op1 is BOZ, then op2 is not!. Try to convert to type of op2. */
4192 if (op1->ts.type == BT_BOZ)
4193 {
4194 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
4195 "as an operand of a relational operator"),
4196 &op1->where))
4197 return false;
4198
4199 if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
4200 return false;
4201
4202 if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
4203 return false;
4204 }
4205
4206 /* If op2 is BOZ, then op1 is not!. Try to convert to type of op2. */
4207 if (op2->ts.type == BT_BOZ)
4208 {
4209 if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
4210 " as an operand of a relational operator"),
4211 &op2->where))
4212 return false;
4213
4214 if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
4215 return false;
4216
4217 if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
4218 return false;
4219 }
4220 if (flag_dec
4221 && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
4222 convert_to_numeric (op1, op2);
4223
4224 if (flag_dec
4225 && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
4226 convert_to_numeric (op2, op1);
4227
4228 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4229 {
4230 gfc_type_convert_binary (e, 1);
4231
4232 e->ts.type = BT_LOGICAL;
4233 e->ts.kind = gfc_default_logical_kind;
4234
4235 if (warn_compare_reals)
4236 {
4237 gfc_intrinsic_op op = e->value.op.op;
4238
4239 /* Type conversion has made sure that the types of op1 and op2
4240 agree, so it is only necessary to check the first one. */
4241 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
4242 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
4243 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
4244 {
4245 const char *msg;
4246
4247 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
4248 msg = G_("Equality comparison for %s at %L");
4249 else
4250 msg = G_("Inequality comparison for %s at %L");
4251
4252 gfc_warning (OPT_Wcompare_reals, msg,
4253 gfc_typename (op1), &op1->where);
4254 }
4255 }
4256
4257 break;
4258 }
4259
4260 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4261 sprintf (msg,
4262 _("Logicals at %%L must be compared with %s instead of %s"),
4263 (e->value.op.op == INTRINSIC_EQ
4264 || e->value.op.op == INTRINSIC_EQ_OS)
4265 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
4266 else
4267 sprintf (msg,
4268 _("Operands of comparison operator %%<%s%%> at %%L are %s/%s"),
4269 gfc_op2string (e->value.op.op), gfc_typename (op1),
4270 gfc_typename (op2));
4271
4272 goto bad_op;
4273
4274 case INTRINSIC_USER:
4275 if (e->value.op.uop->op == NULL)
4276 {
4277 const char *name = e->value.op.uop->name;
4278 const char *guessed;
4279 guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
4280 if (guessed)
4281 sprintf (msg, _("Unknown operator %%<%s%%> at %%L; did you mean '%s'?"),
4282 name, guessed);
4283 else
4284 sprintf (msg, _("Unknown operator %%<%s%%> at %%L"), name);
4285 }
4286 else if (op2 == NULL)
4287 sprintf (msg, _("Operand of user operator %%<%s%%> at %%L is %s"),
4288 e->value.op.uop->name, gfc_typename (op1));
4289 else
4290 {
4291 sprintf (msg, _("Operands of user operator %%<%s%%> at %%L are %s/%s"),
4292 e->value.op.uop->name, gfc_typename (op1),
4293 gfc_typename (op2));
4294 e->value.op.uop->op->sym->attr.referenced = 1;
4295 }
4296
4297 goto bad_op;
4298
4299 case INTRINSIC_PARENTHESES:
4300 e->ts = op1->ts;
4301 if (e->ts.type == BT_CHARACTER)
4302 e->ts.u.cl = op1->ts.u.cl;
4303 break;
4304
4305 default:
4306 gfc_internal_error ("resolve_operator(): Bad intrinsic");
4307 }
4308
4309 /* Deal with arrayness of an operand through an operator. */
4310
4311 switch (e->value.op.op)
4312 {
4313 case INTRINSIC_PLUS:
4314 case INTRINSIC_MINUS:
4315 case INTRINSIC_TIMES:
4316 case INTRINSIC_DIVIDE:
4317 case INTRINSIC_POWER:
4318 case INTRINSIC_CONCAT:
4319 case INTRINSIC_AND:
4320 case INTRINSIC_OR:
4321 case INTRINSIC_EQV:
4322 case INTRINSIC_NEQV:
4323 case INTRINSIC_EQ:
4324 case INTRINSIC_EQ_OS:
4325 case INTRINSIC_NE:
4326 case INTRINSIC_NE_OS:
4327 case INTRINSIC_GT:
4328 case INTRINSIC_GT_OS:
4329 case INTRINSIC_GE:
4330 case INTRINSIC_GE_OS:
4331 case INTRINSIC_LT:
4332 case INTRINSIC_LT_OS:
4333 case INTRINSIC_LE:
4334 case INTRINSIC_LE_OS:
4335
4336 if (op1->rank == 0 && op2->rank == 0)
4337 e->rank = 0;
4338
4339 if (op1->rank == 0 && op2->rank != 0)
4340 {
4341 e->rank = op2->rank;
4342
4343 if (e->shape == NULL)
4344 e->shape = gfc_copy_shape (op2->shape, op2->rank);
4345 }
4346
4347 if (op1->rank != 0 && op2->rank == 0)
4348 {
4349 e->rank = op1->rank;
4350
4351 if (e->shape == NULL)
4352 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4353 }
4354
4355 if (op1->rank != 0 && op2->rank != 0)
4356 {
4357 if (op1->rank == op2->rank)
4358 {
4359 e->rank = op1->rank;
4360 if (e->shape == NULL)
4361 {
4362 t = compare_shapes (op1, op2);
4363 if (!t)
4364 e->shape = NULL;
4365 else
4366 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4367 }
4368 }
4369 else
4370 {
4371 /* Allow higher level expressions to work. */
4372 e->rank = 0;
4373
4374 /* Try user-defined operators, and otherwise throw an error. */
4375 dual_locus_error = true;
4376 sprintf (msg,
4377 _("Inconsistent ranks for operator at %%L and %%L"));
4378 goto bad_op;
4379 }
4380 }
4381
4382 break;
4383
4384 case INTRINSIC_PARENTHESES:
4385 case INTRINSIC_NOT:
4386 case INTRINSIC_UPLUS:
4387 case INTRINSIC_UMINUS:
4388 /* Simply copy arrayness attribute */
4389 e->rank = op1->rank;
4390
4391 if (e->shape == NULL)
4392 e->shape = gfc_copy_shape (op1->shape, op1->rank);
4393
4394 break;
4395
4396 default:
4397 break;
4398 }
4399
4400 simplify_op:
4401
4402 /* Attempt to simplify the expression. */
4403 if (t)
4404 {
4405 t = gfc_simplify_expr (e, 0);
4406 /* Some calls do not succeed in simplification and return false
4407 even though there is no error; e.g. variable references to
4408 PARAMETER arrays. */
4409 if (!gfc_is_constant_expr (e))
4410 t = true;
4411 }
4412 return t;
4413
4414 bad_op:
4415
4416 {
4417 match m = gfc_extend_expr (e);
4418 if (m == MATCH_YES)
4419 return true;
4420 if (m == MATCH_ERROR)
4421 return false;
4422 }
4423
4424 if (dual_locus_error)
4425 gfc_error (msg, &op1->where, &op2->where);
4426 else
4427 gfc_error (msg, &e->where);
4428
4429 return false;
4430 }
4431
4432
4433 /************** Array resolution subroutines **************/
4434
4435 enum compare_result
4436 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
4437
4438 /* Compare two integer expressions. */
4439
4440 static compare_result
4441 compare_bound (gfc_expr *a, gfc_expr *b)
4442 {
4443 int i;
4444
4445 if (a == NULL || a->expr_type != EXPR_CONSTANT
4446 || b == NULL || b->expr_type != EXPR_CONSTANT)
4447 return CMP_UNKNOWN;
4448
4449 /* If either of the types isn't INTEGER, we must have
4450 raised an error earlier. */
4451
4452 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
4453 return CMP_UNKNOWN;
4454
4455 i = mpz_cmp (a->value.integer, b->value.integer);
4456
4457 if (i < 0)
4458 return CMP_LT;
4459 if (i > 0)
4460 return CMP_GT;
4461 return CMP_EQ;
4462 }
4463
4464
4465 /* Compare an integer expression with an integer. */
4466
4467 static compare_result
4468 compare_bound_int (gfc_expr *a, int b)
4469 {
4470 int i;
4471
4472 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4473 return CMP_UNKNOWN;
4474
4475 if (a->ts.type != BT_INTEGER)
4476 gfc_internal_error ("compare_bound_int(): Bad expression");
4477
4478 i = mpz_cmp_si (a->value.integer, b);
4479
4480 if (i < 0)
4481 return CMP_LT;
4482 if (i > 0)
4483 return CMP_GT;
4484 return CMP_EQ;
4485 }
4486
4487
4488 /* Compare an integer expression with a mpz_t. */
4489
4490 static compare_result
4491 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
4492 {
4493 int i;
4494
4495 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4496 return CMP_UNKNOWN;
4497
4498 if (a->ts.type != BT_INTEGER)
4499 gfc_internal_error ("compare_bound_int(): Bad expression");
4500
4501 i = mpz_cmp (a->value.integer, b);
4502
4503 if (i < 0)
4504 return CMP_LT;
4505 if (i > 0)
4506 return CMP_GT;
4507 return CMP_EQ;
4508 }
4509
4510
4511 /* Compute the last value of a sequence given by a triplet.
4512 Return 0 if it wasn't able to compute the last value, or if the
4513 sequence if empty, and 1 otherwise. */
4514
4515 static int
4516 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4517 gfc_expr *stride, mpz_t last)
4518 {
4519 mpz_t rem;
4520
4521 if (start == NULL || start->expr_type != EXPR_CONSTANT
4522 || end == NULL || end->expr_type != EXPR_CONSTANT
4523 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4524 return 0;
4525
4526 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4527 || (stride != NULL && stride->ts.type != BT_INTEGER))
4528 return 0;
4529
4530 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
4531 {
4532 if (compare_bound (start, end) == CMP_GT)
4533 return 0;
4534 mpz_set (last, end->value.integer);
4535 return 1;
4536 }
4537
4538 if (compare_bound_int (stride, 0) == CMP_GT)
4539 {
4540 /* Stride is positive */
4541 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4542 return 0;
4543 }
4544 else
4545 {
4546 /* Stride is negative */
4547 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4548 return 0;
4549 }
4550
4551 mpz_init (rem);
4552 mpz_sub (rem, end->value.integer, start->value.integer);
4553 mpz_tdiv_r (rem, rem, stride->value.integer);
4554 mpz_sub (last, end->value.integer, rem);
4555 mpz_clear (rem);
4556
4557 return 1;
4558 }
4559
4560
4561 /* Compare a single dimension of an array reference to the array
4562 specification. */
4563
4564 static bool
4565 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4566 {
4567 mpz_t last_value;
4568
4569 if (ar->dimen_type[i] == DIMEN_STAR)
4570 {
4571 gcc_assert (ar->stride[i] == NULL);
4572 /* This implies [*] as [*:] and [*:3] are not possible. */
4573 if (ar->start[i] == NULL)
4574 {
4575 gcc_assert (ar->end[i] == NULL);
4576 return true;
4577 }
4578 }
4579
4580 /* Given start, end and stride values, calculate the minimum and
4581 maximum referenced indexes. */
4582
4583 switch (ar->dimen_type[i])
4584 {
4585 case DIMEN_VECTOR:
4586 case DIMEN_THIS_IMAGE:
4587 break;
4588
4589 case DIMEN_STAR:
4590 case DIMEN_ELEMENT:
4591 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4592 {
4593 if (i < as->rank)
4594 gfc_warning (0, "Array reference at %L is out of bounds "
4595 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4596 mpz_get_si (ar->start[i]->value.integer),
4597 mpz_get_si (as->lower[i]->value.integer), i+1);
4598 else
4599 gfc_warning (0, "Array reference at %L is out of bounds "
4600 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4601 mpz_get_si (ar->start[i]->value.integer),
4602 mpz_get_si (as->lower[i]->value.integer),
4603 i + 1 - as->rank);
4604 return true;
4605 }
4606 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4607 {
4608 if (i < as->rank)
4609 gfc_warning (0, "Array reference at %L is out of bounds "
4610 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4611 mpz_get_si (ar->start[i]->value.integer),
4612 mpz_get_si (as->upper[i]->value.integer), i+1);
4613 else
4614 gfc_warning (0, "Array reference at %L is out of bounds "
4615 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4616 mpz_get_si (ar->start[i]->value.integer),
4617 mpz_get_si (as->upper[i]->value.integer),
4618 i + 1 - as->rank);
4619 return true;
4620 }
4621
4622 break;
4623
4624 case DIMEN_RANGE:
4625 {
4626 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4627 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4628
4629 compare_result comp_start_end = compare_bound (AR_START, AR_END);
4630
4631 /* Check for zero stride, which is not allowed. */
4632 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4633 {
4634 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4635 return false;
4636 }
4637
4638 /* if start == len || (stride > 0 && start < len)
4639 || (stride < 0 && start > len),
4640 then the array section contains at least one element. In this
4641 case, there is an out-of-bounds access if
4642 (start < lower || start > upper). */
4643 if (compare_bound (AR_START, AR_END) == CMP_EQ
4644 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4645 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4646 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4647 && comp_start_end == CMP_GT))
4648 {
4649 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4650 {
4651 gfc_warning (0, "Lower array reference at %L is out of bounds "
4652 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4653 mpz_get_si (AR_START->value.integer),
4654 mpz_get_si (as->lower[i]->value.integer), i+1);
4655 return true;
4656 }
4657 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4658 {
4659 gfc_warning (0, "Lower array reference at %L is out of bounds "
4660 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4661 mpz_get_si (AR_START->value.integer),
4662 mpz_get_si (as->upper[i]->value.integer), i+1);
4663 return true;
4664 }
4665 }
4666
4667 /* If we can compute the highest index of the array section,
4668 then it also has to be between lower and upper. */
4669 mpz_init (last_value);
4670 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4671 last_value))
4672 {
4673 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4674 {
4675 gfc_warning (0, "Upper array reference at %L is out of bounds "
4676 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4677 mpz_get_si (last_value),
4678 mpz_get_si (as->lower[i]->value.integer), i+1);
4679 mpz_clear (last_value);
4680 return true;
4681 }
4682 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4683 {
4684 gfc_warning (0, "Upper array reference at %L is out of bounds "
4685 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4686 mpz_get_si (last_value),
4687 mpz_get_si (as->upper[i]->value.integer), i+1);
4688 mpz_clear (last_value);
4689 return true;
4690 }
4691 }
4692 mpz_clear (last_value);
4693
4694 #undef AR_START
4695 #undef AR_END
4696 }
4697 break;
4698
4699 default:
4700 gfc_internal_error ("check_dimension(): Bad array reference");
4701 }
4702
4703 return true;
4704 }
4705
4706
4707 /* Compare an array reference with an array specification. */
4708
4709 static bool
4710 compare_spec_to_ref (gfc_array_ref *ar)
4711 {
4712 gfc_array_spec *as;
4713 int i;
4714
4715 as = ar->as;
4716 i = as->rank - 1;
4717 /* TODO: Full array sections are only allowed as actual parameters. */
4718 if (as->type == AS_ASSUMED_SIZE
4719 && (/*ar->type == AR_FULL
4720 ||*/ (ar->type == AR_SECTION
4721 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4722 {
4723 gfc_error ("Rightmost upper bound of assumed size array section "
4724 "not specified at %L", &ar->where);
4725 return false;
4726 }
4727
4728 if (ar->type == AR_FULL)
4729 return true;
4730
4731 if (as->rank != ar->dimen)
4732 {
4733 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4734 &ar->where, ar->dimen, as->rank);
4735 return false;
4736 }
4737
4738 /* ar->codimen == 0 is a local array. */
4739 if (as->corank != ar->codimen && ar->codimen != 0)
4740 {
4741 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4742 &ar->where, ar->codimen, as->corank);
4743 return false;
4744 }
4745
4746 for (i = 0; i < as->rank; i++)
4747 if (!check_dimension (i, ar, as))
4748 return false;
4749
4750 /* Local access has no coarray spec. */
4751 if (ar->codimen != 0)
4752 for (i = as->rank; i < as->rank + as->corank; i++)
4753 {
4754 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4755 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4756 {
4757 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4758 i + 1 - as->rank, &ar->where);
4759 return false;
4760 }
4761 if (!check_dimension (i, ar, as))
4762 return false;
4763 }
4764
4765 return true;
4766 }
4767
4768
4769 /* Resolve one part of an array index. */
4770
4771 static bool
4772 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4773 int force_index_integer_kind)
4774 {
4775 gfc_typespec ts;
4776
4777 if (index == NULL)
4778 return true;
4779
4780 if (!gfc_resolve_expr (index))
4781 return false;
4782
4783 if (check_scalar && index->rank != 0)
4784 {
4785 gfc_error ("Array index at %L must be scalar", &index->where);
4786 return false;
4787 }
4788
4789 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4790 {
4791 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4792 &index->where, gfc_basic_typename (index->ts.type));
4793 return false;
4794 }
4795
4796 if (index->ts.type == BT_REAL)
4797 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4798 &index->where))
4799 return false;
4800
4801 if ((index->ts.kind != gfc_index_integer_kind
4802 && force_index_integer_kind)
4803 || index->ts.type != BT_INTEGER)
4804 {
4805 gfc_clear_ts (&ts);
4806 ts.type = BT_INTEGER;
4807 ts.kind = gfc_index_integer_kind;
4808
4809 gfc_convert_type_warn (index, &ts, 2, 0);
4810 }
4811
4812 return true;
4813 }
4814
4815 /* Resolve one part of an array index. */
4816
4817 bool
4818 gfc_resolve_index (gfc_expr *index, int check_scalar)
4819 {
4820 return gfc_resolve_index_1 (index, check_scalar, 1);
4821 }
4822
4823 /* Resolve a dim argument to an intrinsic function. */
4824
4825 bool
4826 gfc_resolve_dim_arg (gfc_expr *dim)
4827 {
4828 if (dim == NULL)
4829 return true;
4830
4831 if (!gfc_resolve_expr (dim))
4832 return false;
4833
4834 if (dim->rank != 0)
4835 {
4836 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4837 return false;
4838
4839 }
4840
4841 if (dim->ts.type != BT_INTEGER)
4842 {
4843 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4844 return false;
4845 }
4846
4847 if (dim->ts.kind != gfc_index_integer_kind)
4848 {
4849 gfc_typespec ts;
4850
4851 gfc_clear_ts (&ts);
4852 ts.type = BT_INTEGER;
4853 ts.kind = gfc_index_integer_kind;
4854
4855 gfc_convert_type_warn (dim, &ts, 2, 0);
4856 }
4857
4858 return true;
4859 }
4860
4861 /* Given an expression that contains array references, update those array
4862 references to point to the right array specifications. While this is
4863 filled in during matching, this information is difficult to save and load
4864 in a module, so we take care of it here.
4865
4866 The idea here is that the original array reference comes from the
4867 base symbol. We traverse the list of reference structures, setting
4868 the stored reference to references. Component references can
4869 provide an additional array specification. */
4870
4871 static void
4872 find_array_spec (gfc_expr *e)
4873 {
4874 gfc_array_spec *as;
4875 gfc_component *c;
4876 gfc_ref *ref;
4877 bool class_as = false;
4878
4879 if (e->symtree->n.sym->ts.type == BT_CLASS)
4880 {
4881 as = CLASS_DATA (e->symtree->n.sym)->as;
4882 class_as = true;
4883 }
4884 else
4885 as = e->symtree->n.sym->as;
4886
4887 for (ref = e->ref; ref; ref = ref->next)
4888 switch (ref->type)
4889 {
4890 case REF_ARRAY:
4891 if (as == NULL)
4892 gfc_internal_error ("find_array_spec(): Missing spec");
4893
4894 ref->u.ar.as = as;
4895 as = NULL;
4896 break;
4897
4898 case REF_COMPONENT:
4899 c = ref->u.c.component;
4900 if (c->attr.dimension)
4901 {
4902 if (as != NULL && !(class_as && as == c->as))
4903 gfc_internal_error ("find_array_spec(): unused as(1)");
4904 as = c->as;
4905 }
4906
4907 break;
4908
4909 case REF_SUBSTRING:
4910 case REF_INQUIRY:
4911 break;
4912 }
4913
4914 if (as != NULL)
4915 gfc_internal_error ("find_array_spec(): unused as(2)");
4916 }
4917
4918
4919 /* Resolve an array reference. */
4920
4921 static bool
4922 resolve_array_ref (gfc_array_ref *ar)
4923 {
4924 int i, check_scalar;
4925 gfc_expr *e;
4926
4927 for (i = 0; i < ar->dimen + ar->codimen; i++)
4928 {
4929 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4930
4931 /* Do not force gfc_index_integer_kind for the start. We can
4932 do fine with any integer kind. This avoids temporary arrays
4933 created for indexing with a vector. */
4934 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4935 return false;
4936 if (!gfc_resolve_index (ar->end[i], check_scalar))
4937 return false;
4938 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4939 return false;
4940
4941 e = ar->start[i];
4942
4943 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4944 switch (e->rank)
4945 {
4946 case 0:
4947 ar->dimen_type[i] = DIMEN_ELEMENT;
4948 break;
4949
4950 case 1:
4951 ar->dimen_type[i] = DIMEN_VECTOR;
4952 if (e->expr_type == EXPR_VARIABLE
4953 && e->symtree->n.sym->ts.type == BT_DERIVED)
4954 ar->start[i] = gfc_get_parentheses (e);
4955 break;
4956
4957 default:
4958 gfc_error ("Array index at %L is an array of rank %d",
4959 &ar->c_where[i], e->rank);
4960 return false;
4961 }
4962
4963 /* Fill in the upper bound, which may be lower than the
4964 specified one for something like a(2:10:5), which is
4965 identical to a(2:7:5). Only relevant for strides not equal
4966 to one. Don't try a division by zero. */
4967 if (ar->dimen_type[i] == DIMEN_RANGE
4968 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4969 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4970 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4971 {
4972 mpz_t size, end;
4973
4974 if (gfc_ref_dimen_size (ar, i, &size, &end))
4975 {
4976 if (ar->end[i] == NULL)
4977 {
4978 ar->end[i] =
4979 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4980 &ar->where);
4981 mpz_set (ar->end[i]->value.integer, end);
4982 }
4983 else if (ar->end[i]->ts.type == BT_INTEGER
4984 && ar->end[i]->expr_type == EXPR_CONSTANT)
4985 {
4986 mpz_set (ar->end[i]->value.integer, end);
4987 }
4988 else
4989 gcc_unreachable ();
4990
4991 mpz_clear (size);
4992 mpz_clear (end);
4993 }
4994 }
4995 }
4996
4997 if (ar->type == AR_FULL)
4998 {
4999 if (ar->as->rank == 0)
5000 ar->type = AR_ELEMENT;
5001
5002 /* Make sure array is the same as array(:,:), this way
5003 we don't need to special case all the time. */
5004 ar->dimen = ar->as->rank;
5005 for (i = 0; i < ar->dimen; i++)
5006 {
5007 ar->dimen_type[i] = DIMEN_RANGE;
5008
5009 gcc_assert (ar->start[i] == NULL);
5010 gcc_assert (ar->end[i] == NULL);
5011 gcc_assert (ar->stride[i] == NULL);
5012 }
5013 }
5014
5015 /* If the reference type is unknown, figure out what kind it is. */
5016
5017 if (ar->type == AR_UNKNOWN)
5018 {
5019 ar->type = AR_ELEMENT;
5020 for (i = 0; i < ar->dimen; i++)
5021 if (ar->dimen_type[i] == DIMEN_RANGE
5022 || ar->dimen_type[i] == DIMEN_VECTOR)
5023 {
5024 ar->type = AR_SECTION;
5025 break;
5026 }
5027 }
5028
5029 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
5030 return false;
5031
5032 if (ar->as->corank && ar->codimen == 0)
5033 {
5034 int n;
5035 ar->codimen = ar->as->corank;
5036 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
5037 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
5038 }
5039
5040 return true;
5041 }
5042
5043
5044 static bool
5045 resolve_substring (gfc_ref *ref, bool *equal_length)
5046 {
5047 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
5048
5049 if (ref->u.ss.start != NULL)
5050 {
5051 if (!gfc_resolve_expr (ref->u.ss.start))
5052 return false;
5053
5054 if (ref->u.ss.start->ts.type != BT_INTEGER)
5055 {
5056 gfc_error ("Substring start index at %L must be of type INTEGER",
5057 &ref->u.ss.start->where);
5058 return false;
5059 }
5060
5061 if (ref->u.ss.start->rank != 0)
5062 {
5063 gfc_error ("Substring start index at %L must be scalar",
5064 &ref->u.ss.start->where);
5065 return false;
5066 }
5067
5068 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
5069 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5070 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5071 {
5072 gfc_error ("Substring start index at %L is less than one",
5073 &ref->u.ss.start->where);
5074 return false;
5075 }
5076 }
5077
5078 if (ref->u.ss.end != NULL)
5079 {
5080 if (!gfc_resolve_expr (ref->u.ss.end))
5081 return false;
5082
5083 if (ref->u.ss.end->ts.type != BT_INTEGER)
5084 {
5085 gfc_error ("Substring end index at %L must be of type INTEGER",
5086 &ref->u.ss.end->where);
5087 return false;
5088 }
5089
5090 if (ref->u.ss.end->rank != 0)
5091 {
5092 gfc_error ("Substring end index at %L must be scalar",
5093 &ref->u.ss.end->where);
5094 return false;
5095 }
5096
5097 if (ref->u.ss.length != NULL
5098 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
5099 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5100 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5101 {
5102 gfc_error ("Substring end index at %L exceeds the string length",
5103 &ref->u.ss.start->where);
5104 return false;
5105 }
5106
5107 if (compare_bound_mpz_t (ref->u.ss.end,
5108 gfc_integer_kinds[k].huge) == CMP_GT
5109 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5110 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5111 {
5112 gfc_error ("Substring end index at %L is too large",
5113 &ref->u.ss.end->where);
5114 return false;
5115 }
5116 /* If the substring has the same length as the original
5117 variable, the reference itself can be deleted. */
5118
5119 if (ref->u.ss.length != NULL
5120 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
5121 && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
5122 *equal_length = true;
5123 }
5124
5125 return true;
5126 }
5127
5128
5129 /* This function supplies missing substring charlens. */
5130
5131 void
5132 gfc_resolve_substring_charlen (gfc_expr *e)
5133 {
5134 gfc_ref *char_ref;
5135 gfc_expr *start, *end;
5136 gfc_typespec *ts = NULL;
5137 mpz_t diff;
5138
5139 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
5140 {
5141 if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
5142 break;
5143 if (char_ref->type == REF_COMPONENT)
5144 ts = &char_ref->u.c.component->ts;
5145 }
5146
5147 if (!char_ref || char_ref->type == REF_INQUIRY)
5148 return;
5149
5150 gcc_assert (char_ref->next == NULL);
5151
5152 if (e->ts.u.cl)
5153 {
5154 if (e->ts.u.cl->length)
5155 gfc_free_expr (e->ts.u.cl->length);
5156 else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
5157 return;
5158 }
5159
5160 if (!e->ts.u.cl)
5161 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5162
5163 if (char_ref->u.ss.start)
5164 start = gfc_copy_expr (char_ref->u.ss.start);
5165 else
5166 start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
5167
5168 if (char_ref->u.ss.end)
5169 end = gfc_copy_expr (char_ref->u.ss.end);
5170 else if (e->expr_type == EXPR_VARIABLE)
5171 {
5172 if (!ts)
5173 ts = &e->symtree->n.sym->ts;
5174 end = gfc_copy_expr (ts->u.cl->length);
5175 }
5176 else
5177 end = NULL;
5178
5179 if (!start || !end)
5180 {
5181 gfc_free_expr (start);
5182 gfc_free_expr (end);
5183 return;
5184 }
5185
5186 /* Length = (end - start + 1).
5187 Check first whether it has a constant length. */
5188 if (gfc_dep_difference (end, start, &diff))
5189 {
5190 gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
5191 &e->where);
5192
5193 mpz_add_ui (len->value.integer, diff, 1);
5194 mpz_clear (diff);
5195 e->ts.u.cl->length = len;
5196 /* The check for length < 0 is handled below */
5197 }
5198 else
5199 {
5200 e->ts.u.cl->length = gfc_subtract (end, start);
5201 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
5202 gfc_get_int_expr (gfc_charlen_int_kind,
5203 NULL, 1));
5204 }
5205
5206 /* F2008, 6.4.1: Both the starting point and the ending point shall
5207 be within the range 1, 2, ..., n unless the starting point exceeds
5208 the ending point, in which case the substring has length zero. */
5209
5210 if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
5211 mpz_set_si (e->ts.u.cl->length->value.integer, 0);
5212
5213 e->ts.u.cl->length->ts.type = BT_INTEGER;
5214 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5215
5216 /* Make sure that the length is simplified. */
5217 gfc_simplify_expr (e->ts.u.cl->length, 1);
5218 gfc_resolve_expr (e->ts.u.cl->length);
5219 }
5220
5221
5222 /* Resolve subtype references. */
5223
5224 bool
5225 gfc_resolve_ref (gfc_expr *expr)
5226 {
5227 int current_part_dimension, n_components, seen_part_dimension, dim;
5228 gfc_ref *ref, **prev, *array_ref;
5229 bool equal_length;
5230
5231 for (ref = expr->ref; ref; ref = ref->next)
5232 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
5233 {
5234 find_array_spec (expr);
5235 break;
5236 }
5237
5238 for (prev = &expr->ref; *prev != NULL;
5239 prev = *prev == NULL ? prev : &(*prev)->next)
5240 switch ((*prev)->type)
5241 {
5242 case REF_ARRAY:
5243 if (!resolve_array_ref (&(*prev)->u.ar))
5244 return false;
5245 break;
5246
5247 case REF_COMPONENT:
5248 case REF_INQUIRY:
5249 break;
5250
5251 case REF_SUBSTRING:
5252 equal_length = false;
5253 if (!resolve_substring (*prev, &equal_length))
5254 return false;
5255
5256 if (expr->expr_type != EXPR_SUBSTRING && equal_length)
5257 {
5258 /* Remove the reference and move the charlen, if any. */
5259 ref = *prev;
5260 *prev = ref->next;
5261 ref->next = NULL;
5262 expr->ts.u.cl = ref->u.ss.length;
5263 ref->u.ss.length = NULL;
5264 gfc_free_ref_list (ref);
5265 }
5266 break;
5267 }
5268
5269 /* Check constraints on part references. */
5270
5271 current_part_dimension = 0;
5272 seen_part_dimension = 0;
5273 n_components = 0;
5274 array_ref = NULL;
5275
5276 for (ref = expr->ref; ref; ref = ref->next)
5277 {
5278 switch (ref->type)
5279 {
5280 case REF_ARRAY:
5281 array_ref = ref;
5282 switch (ref->u.ar.type)
5283 {
5284 case AR_FULL:
5285 /* Coarray scalar. */
5286 if (ref->u.ar.as->rank == 0)
5287 {
5288 current_part_dimension = 0;
5289 break;
5290 }
5291 /* Fall through. */
5292 case AR_SECTION:
5293 current_part_dimension = 1;
5294 break;
5295
5296 case AR_ELEMENT:
5297 array_ref = NULL;
5298 current_part_dimension = 0;
5299 break;
5300
5301 case AR_UNKNOWN:
5302 gfc_internal_error ("resolve_ref(): Bad array reference");
5303 }
5304
5305 break;
5306
5307 case REF_COMPONENT:
5308 if (current_part_dimension || seen_part_dimension)
5309 {
5310 /* F03:C614. */
5311 if (ref->u.c.component->attr.pointer
5312 || ref->u.c.component->attr.proc_pointer
5313 || (ref->u.c.component->ts.type == BT_CLASS
5314 && CLASS_DATA (ref->u.c.component)->attr.pointer))
5315 {
5316 gfc_error ("Component to the right of a part reference "
5317 "with nonzero rank must not have the POINTER "
5318 "attribute at %L", &expr->where);
5319 return false;
5320 }
5321 else if (ref->u.c.component->attr.allocatable
5322 || (ref->u.c.component->ts.type == BT_CLASS
5323 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
5324
5325 {
5326 gfc_error ("Component to the right of a part reference "
5327 "with nonzero rank must not have the ALLOCATABLE "
5328 "attribute at %L", &expr->where);
5329 return false;
5330 }
5331 }
5332
5333 n_components++;
5334 break;
5335
5336 case REF_SUBSTRING:
5337 break;
5338
5339 case REF_INQUIRY:
5340 /* Implement requirement in note 9.7 of F2018 that the result of the
5341 LEN inquiry be a scalar. */
5342 if (ref->u.i == INQUIRY_LEN && array_ref && expr->ts.deferred)
5343 {
5344 array_ref->u.ar.type = AR_ELEMENT;
5345 expr->rank = 0;
5346 /* INQUIRY_LEN is not evaluated from the rest of the expr
5347 but directly from the string length. This means that setting
5348 the array indices to one does not matter but might trigger
5349 a runtime bounds error. Suppress the check. */
5350 expr->no_bounds_check = 1;
5351 for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
5352 {
5353 array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
5354 if (array_ref->u.ar.start[dim])
5355 gfc_free_expr (array_ref->u.ar.start[dim]);
5356 array_ref->u.ar.start[dim]
5357 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
5358 if (array_ref->u.ar.end[dim])
5359 gfc_free_expr (array_ref->u.ar.end[dim]);
5360 if (array_ref->u.ar.stride[dim])
5361 gfc_free_expr (array_ref->u.ar.stride[dim]);
5362 }
5363 }
5364 break;
5365 }
5366
5367 if (((ref->type == REF_COMPONENT && n_components > 1)
5368 || ref->next == NULL)
5369 && current_part_dimension
5370 && seen_part_dimension)
5371 {
5372 gfc_error ("Two or more part references with nonzero rank must "
5373 "not be specified at %L", &expr->where);
5374 return false;
5375 }
5376
5377 if (ref->type == REF_COMPONENT)
5378 {
5379 if (current_part_dimension)
5380 seen_part_dimension = 1;
5381
5382 /* reset to make sure */
5383 current_part_dimension = 0;
5384 }
5385 }
5386
5387 return true;
5388 }
5389
5390
5391 /* Given an expression, determine its shape. This is easier than it sounds.
5392 Leaves the shape array NULL if it is not possible to determine the shape. */
5393
5394 static void
5395 expression_shape (gfc_expr *e)
5396 {
5397 mpz_t array[GFC_MAX_DIMENSIONS];
5398 int i;
5399
5400 if (e->rank <= 0 || e->shape != NULL)
5401 return;
5402
5403 for (i = 0; i < e->rank; i++)
5404 if (!gfc_array_dimen_size (e, i, &array[i]))
5405 goto fail;
5406
5407 e->shape = gfc_get_shape (e->rank);
5408
5409 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
5410
5411 return;
5412
5413 fail:
5414 for (i--; i >= 0; i--)
5415 mpz_clear (array[i]);
5416 }
5417
5418
5419 /* Given a variable expression node, compute the rank of the expression by
5420 examining the base symbol and any reference structures it may have. */
5421
5422 void
5423 gfc_expression_rank (gfc_expr *e)
5424 {
5425 gfc_ref *ref;
5426 int i, rank;
5427
5428 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
5429 could lead to serious confusion... */
5430 gcc_assert (e->expr_type != EXPR_COMPCALL);
5431
5432 if (e->ref == NULL)
5433 {
5434 if (e->expr_type == EXPR_ARRAY)
5435 goto done;
5436 /* Constructors can have a rank different from one via RESHAPE(). */
5437
5438 e->rank = ((e->symtree == NULL || e->symtree->n.sym->as == NULL)
5439 ? 0 : e->symtree->n.sym->as->rank);
5440 goto done;
5441 }
5442
5443 rank = 0;
5444
5445 for (ref = e->ref; ref; ref = ref->next)
5446 {
5447 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
5448 && ref->u.c.component->attr.function && !ref->next)
5449 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
5450
5451 if (ref->type != REF_ARRAY)
5452 continue;
5453
5454 if (ref->u.ar.type == AR_FULL)
5455 {
5456 rank = ref->u.ar.as->rank;
5457 break;
5458 }
5459
5460 if (ref->u.ar.type == AR_SECTION)
5461 {
5462 /* Figure out the rank of the section. */
5463 if (rank != 0)
5464 gfc_internal_error ("gfc_expression_rank(): Two array specs");
5465
5466 for (i = 0; i < ref->u.ar.dimen; i++)
5467 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5468 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
5469 rank++;
5470
5471 break;
5472 }
5473 }
5474
5475 e->rank = rank;
5476
5477 done:
5478 expression_shape (e);
5479 }
5480
5481
5482 static void
5483 add_caf_get_intrinsic (gfc_expr *e)
5484 {
5485 gfc_expr *wrapper, *tmp_expr;
5486 gfc_ref *ref;
5487 int n;
5488
5489 for (ref = e->ref; ref; ref = ref->next)
5490 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5491 break;
5492 if (ref == NULL)
5493 return;
5494
5495 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
5496 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
5497 return;
5498
5499 tmp_expr = XCNEW (gfc_expr);
5500 *tmp_expr = *e;
5501 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
5502 "caf_get", tmp_expr->where, 1, tmp_expr);
5503 wrapper->ts = e->ts;
5504 wrapper->rank = e->rank;
5505 if (e->rank)
5506 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
5507 *e = *wrapper;
5508 free (wrapper);
5509 }
5510
5511
5512 static void
5513 remove_caf_get_intrinsic (gfc_expr *e)
5514 {
5515 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym
5516 && e->value.function.isym->id == GFC_ISYM_CAF_GET);
5517 gfc_expr *e2 = e->value.function.actual->expr;
5518 e->value.function.actual->expr = NULL;
5519 gfc_free_actual_arglist (e->value.function.actual);
5520 gfc_free_shape (&e->shape, e->rank);
5521 *e = *e2;
5522 free (e2);
5523 }
5524
5525
5526 /* Resolve a variable expression. */
5527
5528 static bool
5529 resolve_variable (gfc_expr *e)
5530 {
5531 gfc_symbol *sym;
5532 bool t;
5533
5534 t = true;
5535
5536 if (e->symtree == NULL)
5537 return false;
5538 sym = e->symtree->n.sym;
5539
5540 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
5541 as ts.type is set to BT_ASSUMED in resolve_symbol. */
5542 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
5543 {
5544 if (!actual_arg || inquiry_argument)
5545 {
5546 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
5547 "be used as actual argument", sym->name, &e->where);
5548 return false;
5549 }
5550 }
5551 /* TS 29113, 407b. */
5552 else if (e->ts.type == BT_ASSUMED)
5553 {
5554 if (!actual_arg)
5555 {
5556 gfc_error ("Assumed-type variable %s at %L may only be used "
5557 "as actual argument", sym->name, &e->where);
5558 return false;
5559 }
5560 else if (inquiry_argument && !first_actual_arg)
5561 {
5562 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5563 for all inquiry functions in resolve_function; the reason is
5564 that the function-name resolution happens too late in that
5565 function. */
5566 gfc_error ("Assumed-type variable %s at %L as actual argument to "
5567 "an inquiry function shall be the first argument",
5568 sym->name, &e->where);
5569 return false;
5570 }
5571 }
5572 /* TS 29113, C535b. */
5573 else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5574 && CLASS_DATA (sym)->as
5575 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5576 || (sym->ts.type != BT_CLASS && sym->as
5577 && sym->as->type == AS_ASSUMED_RANK))
5578 && !sym->attr.select_rank_temporary)
5579 {
5580 if (!actual_arg
5581 && !(cs_base && cs_base->current
5582 && cs_base->current->op == EXEC_SELECT_RANK))
5583 {
5584 gfc_error ("Assumed-rank variable %s at %L may only be used as "
5585 "actual argument", sym->name, &e->where);
5586 return false;
5587 }
5588 else if (inquiry_argument && !first_actual_arg)
5589 {
5590 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5591 for all inquiry functions in resolve_function; the reason is
5592 that the function-name resolution happens too late in that
5593 function. */
5594 gfc_error ("Assumed-rank variable %s at %L as actual argument "
5595 "to an inquiry function shall be the first argument",
5596 sym->name, &e->where);
5597 return false;
5598 }
5599 }
5600
5601 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
5602 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5603 && e->ref->next == NULL))
5604 {
5605 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
5606 "a subobject reference", sym->name, &e->ref->u.ar.where);
5607 return false;
5608 }
5609 /* TS 29113, 407b. */
5610 else if (e->ts.type == BT_ASSUMED && e->ref
5611 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5612 && e->ref->next == NULL))
5613 {
5614 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
5615 "reference", sym->name, &e->ref->u.ar.where);
5616 return false;
5617 }
5618
5619 /* TS 29113, C535b. */
5620 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5621 && CLASS_DATA (sym)->as
5622 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5623 || (sym->ts.type != BT_CLASS && sym->as
5624 && sym->as->type == AS_ASSUMED_RANK))
5625 && e->ref
5626 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5627 && e->ref->next == NULL))
5628 {
5629 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
5630 "reference", sym->name, &e->ref->u.ar.where);
5631 return false;
5632 }
5633
5634 /* For variables that are used in an associate (target => object) where
5635 the object's basetype is array valued while the target is scalar,
5636 the ts' type of the component refs is still array valued, which
5637 can't be translated that way. */
5638 if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
5639 && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
5640 && CLASS_DATA (sym->assoc->target)->as)
5641 {
5642 gfc_ref *ref = e->ref;
5643 while (ref)
5644 {
5645 switch (ref->type)
5646 {
5647 case REF_COMPONENT:
5648 ref->u.c.sym = sym->ts.u.derived;
5649 /* Stop the loop. */
5650 ref = NULL;
5651 break;
5652 default:
5653 ref = ref->next;
5654 break;
5655 }
5656 }
5657 }
5658
5659 /* If this is an associate-name, it may be parsed with an array reference
5660 in error even though the target is scalar. Fail directly in this case.
5661 TODO Understand why class scalar expressions must be excluded. */
5662 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
5663 {
5664 if (sym->ts.type == BT_CLASS)
5665 gfc_fix_class_refs (e);
5666 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
5667 return false;
5668 else if (sym->attr.dimension && (!e->ref || e->ref->type != REF_ARRAY))
5669 {
5670 /* This can happen because the parser did not detect that the
5671 associate name is an array and the expression had no array
5672 part_ref. */
5673 gfc_ref *ref = gfc_get_ref ();
5674 ref->type = REF_ARRAY;
5675 ref->u.ar = *gfc_get_array_ref();
5676 ref->u.ar.type = AR_FULL;
5677 if (sym->as)
5678 {
5679 ref->u.ar.as = sym->as;
5680 ref->u.ar.dimen = sym->as->rank;
5681 }
5682 ref->next = e->ref;
5683 e->ref = ref;
5684
5685 }
5686 }
5687
5688 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
5689 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
5690
5691 /* On the other hand, the parser may not have known this is an array;
5692 in this case, we have to add a FULL reference. */
5693 if (sym->assoc && sym->attr.dimension && !e->ref)
5694 {
5695 e->ref = gfc_get_ref ();
5696 e->ref->type = REF_ARRAY;
5697 e->ref->u.ar.type = AR_FULL;
5698 e->ref->u.ar.dimen = 0;
5699 }
5700
5701 /* Like above, but for class types, where the checking whether an array
5702 ref is present is more complicated. Furthermore make sure not to add
5703 the full array ref to _vptr or _len refs. */
5704 if (sym->assoc && sym->ts.type == BT_CLASS
5705 && CLASS_DATA (sym)->attr.dimension
5706 && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
5707 {
5708 gfc_ref *ref, *newref;
5709
5710 newref = gfc_get_ref ();
5711 newref->type = REF_ARRAY;
5712 newref->u.ar.type = AR_FULL;
5713 newref->u.ar.dimen = 0;
5714 /* Because this is an associate var and the first ref either is a ref to
5715 the _data component or not, no traversal of the ref chain is
5716 needed. The array ref needs to be inserted after the _data ref,
5717 or when that is not present, which may happend for polymorphic
5718 types, then at the first position. */
5719 ref = e->ref;
5720 if (!ref)
5721 e->ref = newref;
5722 else if (ref->type == REF_COMPONENT
5723 && strcmp ("_data", ref->u.c.component->name) == 0)
5724 {
5725 if (!ref->next || ref->next->type != REF_ARRAY)
5726 {
5727 newref->next = ref->next;
5728 ref->next = newref;
5729 }
5730 else
5731 /* Array ref present already. */
5732 gfc_free_ref_list (newref);
5733 }
5734 else if (ref->type == REF_ARRAY)
5735 /* Array ref present already. */
5736 gfc_free_ref_list (newref);
5737 else
5738 {
5739 newref->next = ref;
5740 e->ref = newref;
5741 }
5742 }
5743
5744 if (e->ref && !gfc_resolve_ref (e))
5745 return false;
5746
5747 if (sym->attr.flavor == FL_PROCEDURE
5748 && (!sym->attr.function
5749 || (sym->attr.function && sym->result
5750 && sym->result->attr.proc_pointer
5751 && !sym->result->attr.function)))
5752 {
5753 e->ts.type = BT_PROCEDURE;
5754 goto resolve_procedure;
5755 }
5756
5757 if (sym->ts.type != BT_UNKNOWN)
5758 gfc_variable_attr (e, &e->ts);
5759 else if (sym->attr.flavor == FL_PROCEDURE
5760 && sym->attr.function && sym->result
5761 && sym->result->ts.type != BT_UNKNOWN
5762 && sym->result->attr.proc_pointer)
5763 e->ts = sym->result->ts;
5764 else
5765 {
5766 /* Must be a simple variable reference. */
5767 if (!gfc_set_default_type (sym, 1, sym->ns))
5768 return false;
5769 e->ts = sym->ts;
5770 }
5771
5772 if (check_assumed_size_reference (sym, e))
5773 return false;
5774
5775 /* Deal with forward references to entries during gfc_resolve_code, to
5776 satisfy, at least partially, 12.5.2.5. */
5777 if (gfc_current_ns->entries
5778 && current_entry_id == sym->entry_id
5779 && cs_base
5780 && cs_base->current
5781 && cs_base->current->op != EXEC_ENTRY)
5782 {
5783 gfc_entry_list *entry;
5784 gfc_formal_arglist *formal;
5785 int n;
5786 bool seen, saved_specification_expr;
5787
5788 /* If the symbol is a dummy... */
5789 if (sym->attr.dummy && sym->ns == gfc_current_ns)
5790 {
5791 entry = gfc_current_ns->entries;
5792 seen = false;
5793
5794 /* ...test if the symbol is a parameter of previous entries. */
5795 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5796 for (formal = entry->sym->formal; formal; formal = formal->next)
5797 {
5798 if (formal->sym && sym->name == formal->sym->name)
5799 {
5800 seen = true;
5801 break;
5802 }
5803 }
5804
5805 /* If it has not been seen as a dummy, this is an error. */
5806 if (!seen)
5807 {
5808 if (specification_expr)
5809 gfc_error ("Variable %qs, used in a specification expression"
5810 ", is referenced at %L before the ENTRY statement "
5811 "in which it is a parameter",
5812 sym->name, &cs_base->current->loc);
5813 else
5814 gfc_error ("Variable %qs is used at %L before the ENTRY "
5815 "statement in which it is a parameter",
5816 sym->name, &cs_base->current->loc);
5817 t = false;
5818 }
5819 }
5820
5821 /* Now do the same check on the specification expressions. */
5822 saved_specification_expr = specification_expr;
5823 specification_expr = true;
5824 if (sym->ts.type == BT_CHARACTER
5825 && !gfc_resolve_expr (sym->ts.u.cl->length))
5826 t = false;
5827
5828 if (sym->as)
5829 for (n = 0; n < sym->as->rank; n++)
5830 {
5831 if (!gfc_resolve_expr (sym->as->lower[n]))
5832 t = false;
5833 if (!gfc_resolve_expr (sym->as->upper[n]))
5834 t = false;
5835 }
5836 specification_expr = saved_specification_expr;
5837
5838 if (t)
5839 /* Update the symbol's entry level. */
5840 sym->entry_id = current_entry_id + 1;
5841 }
5842
5843 /* If a symbol has been host_associated mark it. This is used latter,
5844 to identify if aliasing is possible via host association. */
5845 if (sym->attr.flavor == FL_VARIABLE
5846 && gfc_current_ns->parent
5847 && (gfc_current_ns->parent == sym->ns
5848 || (gfc_current_ns->parent->parent
5849 && gfc_current_ns->parent->parent == sym->ns)))
5850 sym->attr.host_assoc = 1;
5851
5852 if (gfc_current_ns->proc_name
5853 && sym->attr.dimension
5854 && (sym->ns != gfc_current_ns
5855 || sym->attr.use_assoc
5856 || sym->attr.in_common))
5857 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
5858
5859 resolve_procedure:
5860 if (t && !resolve_procedure_expression (e))
5861 t = false;
5862
5863 /* F2008, C617 and C1229. */
5864 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5865 && gfc_is_coindexed (e))
5866 {
5867 gfc_ref *ref, *ref2 = NULL;
5868
5869 for (ref = e->ref; ref; ref = ref->next)
5870 {
5871 if (ref->type == REF_COMPONENT)
5872 ref2 = ref;
5873 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5874 break;
5875 }
5876
5877 for ( ; ref; ref = ref->next)
5878 if (ref->type == REF_COMPONENT)
5879 break;
5880
5881 /* Expression itself is not coindexed object. */
5882 if (ref && e->ts.type == BT_CLASS)
5883 {
5884 gfc_error ("Polymorphic subobject of coindexed object at %L",
5885 &e->where);
5886 t = false;
5887 }
5888
5889 /* Expression itself is coindexed object. */
5890 if (ref == NULL)
5891 {
5892 gfc_component *c;
5893 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5894 for ( ; c; c = c->next)
5895 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5896 {
5897 gfc_error ("Coindexed object with polymorphic allocatable "
5898 "subcomponent at %L", &e->where);
5899 t = false;
5900 break;
5901 }
5902 }
5903 }
5904
5905 if (t)
5906 gfc_expression_rank (e);
5907
5908 if (t && flag_coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e))
5909 add_caf_get_intrinsic (e);
5910
5911 /* Simplify cases where access to a parameter array results in a
5912 single constant. Suppress errors since those will have been
5913 issued before, as warnings. */
5914 if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
5915 {
5916 gfc_push_suppress_errors ();
5917 gfc_simplify_expr (e, 1);
5918 gfc_pop_suppress_errors ();
5919 }
5920
5921 return t;
5922 }
5923
5924
5925 /* Checks to see that the correct symbol has been host associated.
5926 The only situation where this arises is that in which a twice
5927 contained function is parsed after the host association is made.
5928 Therefore, on detecting this, change the symbol in the expression
5929 and convert the array reference into an actual arglist if the old
5930 symbol is a variable. */
5931 static bool
5932 check_host_association (gfc_expr *e)
5933 {
5934 gfc_symbol *sym, *old_sym;
5935 gfc_symtree *st;
5936 int n;
5937 gfc_ref *ref;
5938 gfc_actual_arglist *arg, *tail = NULL;
5939 bool retval = e->expr_type == EXPR_FUNCTION;
5940
5941 /* If the expression is the result of substitution in
5942 interface.c(gfc_extend_expr) because there is no way in
5943 which the host association can be wrong. */
5944 if (e->symtree == NULL
5945 || e->symtree->n.sym == NULL
5946 || e->user_operator)
5947 return retval;
5948
5949 old_sym = e->symtree->n.sym;
5950
5951 if (gfc_current_ns->parent
5952 && old_sym->ns != gfc_current_ns)
5953 {
5954 /* Use the 'USE' name so that renamed module symbols are
5955 correctly handled. */
5956 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5957
5958 if (sym && old_sym != sym
5959 && sym->ts.type == old_sym->ts.type
5960 && sym->attr.flavor == FL_PROCEDURE
5961 && sym->attr.contained)
5962 {
5963 /* Clear the shape, since it might not be valid. */
5964 gfc_free_shape (&e->shape, e->rank);
5965
5966 /* Give the expression the right symtree! */
5967 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5968 gcc_assert (st != NULL);
5969
5970 if (old_sym->attr.flavor == FL_PROCEDURE
5971 || e->expr_type == EXPR_FUNCTION)
5972 {
5973 /* Original was function so point to the new symbol, since
5974 the actual argument list is already attached to the
5975 expression. */
5976 e->value.function.esym = NULL;
5977 e->symtree = st;
5978 }
5979 else
5980 {
5981 /* Original was variable so convert array references into
5982 an actual arglist. This does not need any checking now
5983 since resolve_function will take care of it. */
5984 e->value.function.actual = NULL;
5985 e->expr_type = EXPR_FUNCTION;
5986 e->symtree = st;
5987
5988 /* Ambiguity will not arise if the array reference is not
5989 the last reference. */
5990 for (ref = e->ref; ref; ref = ref->next)
5991 if (ref->type == REF_ARRAY && ref->next == NULL)
5992 break;
5993
5994 gcc_assert (ref->type == REF_ARRAY);
5995
5996 /* Grab the start expressions from the array ref and
5997 copy them into actual arguments. */
5998 for (n = 0; n < ref->u.ar.dimen; n++)
5999 {
6000 arg = gfc_get_actual_arglist ();
6001 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
6002 if (e->value.function.actual == NULL)
6003 tail = e->value.function.actual = arg;
6004 else
6005 {
6006 tail->next = arg;
6007 tail = arg;
6008 }
6009 }
6010
6011 /* Dump the reference list and set the rank. */
6012 gfc_free_ref_list (e->ref);
6013 e->ref = NULL;
6014 e->rank = sym->as ? sym->as->rank : 0;
6015 }
6016
6017 gfc_resolve_expr (e);
6018 sym->refs++;
6019 }
6020 }
6021 /* This might have changed! */
6022 return e->expr_type == EXPR_FUNCTION;
6023 }
6024
6025
6026 static void
6027 gfc_resolve_character_operator (gfc_expr *e)
6028 {
6029 gfc_expr *op1 = e->value.op.op1;
6030 gfc_expr *op2 = e->value.op.op2;
6031 gfc_expr *e1 = NULL;
6032 gfc_expr *e2 = NULL;
6033
6034 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
6035
6036 if (op1->ts.u.cl && op1->ts.u.cl->length)
6037 e1 = gfc_copy_expr (op1->ts.u.cl->length);
6038 else if (op1->expr_type == EXPR_CONSTANT)
6039 e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6040 op1->value.character.length);
6041
6042 if (op2->ts.u.cl && op2->ts.u.cl->length)
6043 e2 = gfc_copy_expr (op2->ts.u.cl->length);
6044 else if (op2->expr_type == EXPR_CONSTANT)
6045 e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6046 op2->value.character.length);
6047
6048 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6049
6050 if (!e1 || !e2)
6051 {
6052 gfc_free_expr (e1);
6053 gfc_free_expr (e2);
6054
6055 return;
6056 }
6057
6058 e->ts.u.cl->length = gfc_add (e1, e2);
6059 e->ts.u.cl->length->ts.type = BT_INTEGER;
6060 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
6061 gfc_simplify_expr (e->ts.u.cl->length, 0);
6062 gfc_resolve_expr (e->ts.u.cl->length);
6063
6064 return;
6065 }
6066
6067
6068 /* Ensure that an character expression has a charlen and, if possible, a
6069 length expression. */
6070
6071 static void
6072 fixup_charlen (gfc_expr *e)
6073 {
6074 /* The cases fall through so that changes in expression type and the need
6075 for multiple fixes are picked up. In all circumstances, a charlen should
6076 be available for the middle end to hang a backend_decl on. */
6077 switch (e->expr_type)
6078 {
6079 case EXPR_OP:
6080 gfc_resolve_character_operator (e);
6081 /* FALLTHRU */
6082
6083 case EXPR_ARRAY:
6084 if (e->expr_type == EXPR_ARRAY)
6085 gfc_resolve_character_array_constructor (e);
6086 /* FALLTHRU */
6087
6088 case EXPR_SUBSTRING:
6089 if (!e->ts.u.cl && e->ref)
6090 gfc_resolve_substring_charlen (e);
6091 /* FALLTHRU */
6092
6093 default:
6094 if (!e->ts.u.cl)
6095 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6096
6097 break;
6098 }
6099 }
6100
6101
6102 /* Update an actual argument to include the passed-object for type-bound
6103 procedures at the right position. */
6104
6105 static gfc_actual_arglist*
6106 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
6107 const char *name)
6108 {
6109 gcc_assert (argpos > 0);
6110
6111 if (argpos == 1)
6112 {
6113 gfc_actual_arglist* result;
6114
6115 result = gfc_get_actual_arglist ();
6116 result->expr = po;
6117 result->next = lst;
6118 if (name)
6119 result->name = name;
6120
6121 return result;
6122 }
6123
6124 if (lst)
6125 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
6126 else
6127 lst = update_arglist_pass (NULL, po, argpos - 1, name);
6128 return lst;
6129 }
6130
6131
6132 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
6133
6134 static gfc_expr*
6135 extract_compcall_passed_object (gfc_expr* e)
6136 {
6137 gfc_expr* po;
6138
6139 if (e->expr_type == EXPR_UNKNOWN)
6140 {
6141 gfc_error ("Error in typebound call at %L",
6142 &e->where);
6143 return NULL;
6144 }
6145
6146 gcc_assert (e->expr_type == EXPR_COMPCALL);
6147
6148 if (e->value.compcall.base_object)
6149 po = gfc_copy_expr (e->value.compcall.base_object);
6150 else
6151 {
6152 po = gfc_get_expr ();
6153 po->expr_type = EXPR_VARIABLE;
6154 po->symtree = e->symtree;
6155 po->ref = gfc_copy_ref (e->ref);
6156 po->where = e->where;
6157 }
6158
6159 if (!gfc_resolve_expr (po))
6160 return NULL;
6161
6162 return po;
6163 }
6164
6165
6166 /* Update the arglist of an EXPR_COMPCALL expression to include the
6167 passed-object. */
6168
6169 static bool
6170 update_compcall_arglist (gfc_expr* e)
6171 {
6172 gfc_expr* po;
6173 gfc_typebound_proc* tbp;
6174
6175 tbp = e->value.compcall.tbp;
6176
6177 if (tbp->error)
6178 return false;
6179
6180 po = extract_compcall_passed_object (e);
6181 if (!po)
6182 return false;
6183
6184 if (tbp->nopass || e->value.compcall.ignore_pass)
6185 {
6186 gfc_free_expr (po);
6187 return true;
6188 }
6189
6190 if (tbp->pass_arg_num <= 0)
6191 return false;
6192
6193 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
6194 tbp->pass_arg_num,
6195 tbp->pass_arg);
6196
6197 return true;
6198 }
6199
6200
6201 /* Extract the passed object from a PPC call (a copy of it). */
6202
6203 static gfc_expr*
6204 extract_ppc_passed_object (gfc_expr *e)
6205 {
6206 gfc_expr *po;
6207 gfc_ref **ref;
6208
6209 po = gfc_get_expr ();
6210 po->expr_type = EXPR_VARIABLE;
6211 po->symtree = e->symtree;
6212 po->ref = gfc_copy_ref (e->ref);
6213 po->where = e->where;
6214
6215 /* Remove PPC reference. */
6216 ref = &po->ref;
6217 while ((*ref)->next)
6218 ref = &(*ref)->next;
6219 gfc_free_ref_list (*ref);
6220 *ref = NULL;
6221
6222 if (!gfc_resolve_expr (po))
6223 return NULL;
6224
6225 return po;
6226 }
6227
6228
6229 /* Update the actual arglist of a procedure pointer component to include the
6230 passed-object. */
6231
6232 static bool
6233 update_ppc_arglist (gfc_expr* e)
6234 {
6235 gfc_expr* po;
6236 gfc_component *ppc;
6237 gfc_typebound_proc* tb;
6238
6239 ppc = gfc_get_proc_ptr_comp (e);
6240 if (!ppc)
6241 return false;
6242
6243 tb = ppc->tb;
6244
6245 if (tb->error)
6246 return false;
6247 else if (tb->nopass)
6248 return true;
6249
6250 po = extract_ppc_passed_object (e);
6251 if (!po)
6252 return false;
6253
6254 /* F08:R739. */
6255 if (po->rank != 0)
6256 {
6257 gfc_error ("Passed-object at %L must be scalar", &e->where);
6258 return false;
6259 }
6260
6261 /* F08:C611. */
6262 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
6263 {
6264 gfc_error ("Base object for procedure-pointer component call at %L is of"
6265 " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
6266 return false;
6267 }
6268
6269 gcc_assert (tb->pass_arg_num > 0);
6270 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
6271 tb->pass_arg_num,
6272 tb->pass_arg);
6273
6274 return true;
6275 }
6276
6277
6278 /* Check that the object a TBP is called on is valid, i.e. it must not be
6279 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
6280
6281 static bool
6282 check_typebound_baseobject (gfc_expr* e)
6283 {
6284 gfc_expr* base;
6285 bool return_value = false;
6286
6287 base = extract_compcall_passed_object (e);
6288 if (!base)
6289 return false;
6290
6291 if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
6292 {
6293 gfc_error ("Error in typebound call at %L", &e->where);
6294 goto cleanup;
6295 }
6296
6297 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
6298 return false;
6299
6300 /* F08:C611. */
6301 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
6302 {
6303 gfc_error ("Base object for type-bound procedure call at %L is of"
6304 " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
6305 goto cleanup;
6306 }
6307
6308 /* F08:C1230. If the procedure called is NOPASS,
6309 the base object must be scalar. */
6310 if (e->value.compcall.tbp->nopass && base->rank != 0)
6311 {
6312 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
6313 " be scalar", &e->where);
6314 goto cleanup;
6315 }
6316
6317 return_value = true;
6318
6319 cleanup:
6320 gfc_free_expr (base);
6321 return return_value;
6322 }
6323
6324
6325 /* Resolve a call to a type-bound procedure, either function or subroutine,
6326 statically from the data in an EXPR_COMPCALL expression. The adapted
6327 arglist and the target-procedure symtree are returned. */
6328
6329 static bool
6330 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
6331 gfc_actual_arglist** actual)
6332 {
6333 gcc_assert (e->expr_type == EXPR_COMPCALL);
6334 gcc_assert (!e->value.compcall.tbp->is_generic);
6335
6336 /* Update the actual arglist for PASS. */
6337 if (!update_compcall_arglist (e))
6338 return false;
6339
6340 *actual = e->value.compcall.actual;
6341 *target = e->value.compcall.tbp->u.specific;
6342
6343 gfc_free_ref_list (e->ref);
6344 e->ref = NULL;
6345 e->value.compcall.actual = NULL;
6346
6347 /* If we find a deferred typebound procedure, check for derived types
6348 that an overriding typebound procedure has not been missed. */
6349 if (e->value.compcall.name
6350 && !e->value.compcall.tbp->non_overridable
6351 && e->value.compcall.base_object
6352 && e->value.compcall.base_object->ts.type == BT_DERIVED)
6353 {
6354 gfc_symtree *st;
6355 gfc_symbol *derived;
6356
6357 /* Use the derived type of the base_object. */
6358 derived = e->value.compcall.base_object->ts.u.derived;
6359 st = NULL;
6360
6361 /* If necessary, go through the inheritance chain. */
6362 while (!st && derived)
6363 {
6364 /* Look for the typebound procedure 'name'. */
6365 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
6366 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
6367 e->value.compcall.name);
6368 if (!st)
6369 derived = gfc_get_derived_super_type (derived);
6370 }
6371
6372 /* Now find the specific name in the derived type namespace. */
6373 if (st && st->n.tb && st->n.tb->u.specific)
6374 gfc_find_sym_tree (st->n.tb->u.specific->name,
6375 derived->ns, 1, &st);
6376 if (st)
6377 *target = st;
6378 }
6379 return true;
6380 }
6381
6382
6383 /* Get the ultimate declared type from an expression. In addition,
6384 return the last class/derived type reference and the copy of the
6385 reference list. If check_types is set true, derived types are
6386 identified as well as class references. */
6387 static gfc_symbol*
6388 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
6389 gfc_expr *e, bool check_types)
6390 {
6391 gfc_symbol *declared;
6392 gfc_ref *ref;
6393
6394 declared = NULL;
6395 if (class_ref)
6396 *class_ref = NULL;
6397 if (new_ref)
6398 *new_ref = gfc_copy_ref (e->ref);
6399
6400 for (ref = e->ref; ref; ref = ref->next)
6401 {
6402 if (ref->type != REF_COMPONENT)
6403 continue;
6404
6405 if ((ref->u.c.component->ts.type == BT_CLASS
6406 || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
6407 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
6408 {
6409 declared = ref->u.c.component->ts.u.derived;
6410 if (class_ref)
6411 *class_ref = ref;
6412 }
6413 }
6414
6415 if (declared == NULL)
6416 declared = e->symtree->n.sym->ts.u.derived;
6417
6418 return declared;
6419 }
6420
6421
6422 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
6423 which of the specific bindings (if any) matches the arglist and transform
6424 the expression into a call of that binding. */
6425
6426 static bool
6427 resolve_typebound_generic_call (gfc_expr* e, const char **name)
6428 {
6429 gfc_typebound_proc* genproc;
6430 const char* genname;
6431 gfc_symtree *st;
6432 gfc_symbol *derived;
6433
6434 gcc_assert (e->expr_type == EXPR_COMPCALL);
6435 genname = e->value.compcall.name;
6436 genproc = e->value.compcall.tbp;
6437
6438 if (!genproc->is_generic)
6439 return true;
6440
6441 /* Try the bindings on this type and in the inheritance hierarchy. */
6442 for (; genproc; genproc = genproc->overridden)
6443 {
6444 gfc_tbp_generic* g;
6445
6446 gcc_assert (genproc->is_generic);
6447 for (g = genproc->u.generic; g; g = g->next)
6448 {
6449 gfc_symbol* target;
6450 gfc_actual_arglist* args;
6451 bool matches;
6452
6453 gcc_assert (g->specific);
6454
6455 if (g->specific->error)
6456 continue;
6457
6458 target = g->specific->u.specific->n.sym;
6459
6460 /* Get the right arglist by handling PASS/NOPASS. */
6461 args = gfc_copy_actual_arglist (e->value.compcall.actual);
6462 if (!g->specific->nopass)
6463 {
6464 gfc_expr* po;
6465 po = extract_compcall_passed_object (e);
6466 if (!po)
6467 {
6468 gfc_free_actual_arglist (args);
6469 return false;
6470 }
6471
6472 gcc_assert (g->specific->pass_arg_num > 0);
6473 gcc_assert (!g->specific->error);
6474 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
6475 g->specific->pass_arg);
6476 }
6477 resolve_actual_arglist (args, target->attr.proc,
6478 is_external_proc (target)
6479 && gfc_sym_get_dummy_args (target) == NULL);
6480
6481 /* Check if this arglist matches the formal. */
6482 matches = gfc_arglist_matches_symbol (&args, target);
6483
6484 /* Clean up and break out of the loop if we've found it. */
6485 gfc_free_actual_arglist (args);
6486 if (matches)
6487 {
6488 e->value.compcall.tbp = g->specific;
6489 genname = g->specific_st->name;
6490 /* Pass along the name for CLASS methods, where the vtab
6491 procedure pointer component has to be referenced. */
6492 if (name)
6493 *name = genname;
6494 goto success;
6495 }
6496 }
6497 }
6498
6499 /* Nothing matching found! */
6500 gfc_error ("Found no matching specific binding for the call to the GENERIC"
6501 " %qs at %L", genname, &e->where);
6502 return false;
6503
6504 success:
6505 /* Make sure that we have the right specific instance for the name. */
6506 derived = get_declared_from_expr (NULL, NULL, e, true);
6507
6508 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
6509 if (st)
6510 e->value.compcall.tbp = st->n.tb;
6511
6512 return true;
6513 }
6514
6515
6516 /* Resolve a call to a type-bound subroutine. */
6517
6518 static bool
6519 resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
6520 {
6521 gfc_actual_arglist* newactual;
6522 gfc_symtree* target;
6523
6524 /* Check that's really a SUBROUTINE. */
6525 if (!c->expr1->value.compcall.tbp->subroutine)
6526 {
6527 if (!c->expr1->value.compcall.tbp->is_generic
6528 && c->expr1->value.compcall.tbp->u.specific
6529 && c->expr1->value.compcall.tbp->u.specific->n.sym
6530 && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
6531 c->expr1->value.compcall.tbp->subroutine = 1;
6532 else
6533 {
6534 gfc_error ("%qs at %L should be a SUBROUTINE",
6535 c->expr1->value.compcall.name, &c->loc);
6536 return false;
6537 }
6538 }
6539
6540 if (!check_typebound_baseobject (c->expr1))
6541 return false;
6542
6543 /* Pass along the name for CLASS methods, where the vtab
6544 procedure pointer component has to be referenced. */
6545 if (name)
6546 *name = c->expr1->value.compcall.name;
6547
6548 if (!resolve_typebound_generic_call (c->expr1, name))
6549 return false;
6550
6551 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
6552 if (overridable)
6553 *overridable = !c->expr1->value.compcall.tbp->non_overridable;
6554
6555 /* Transform into an ordinary EXEC_CALL for now. */
6556
6557 if (!resolve_typebound_static (c->expr1, &target, &newactual))
6558 return false;
6559
6560 c->ext.actual = newactual;
6561 c->symtree = target;
6562 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
6563
6564 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
6565
6566 gfc_free_expr (c->expr1);
6567 c->expr1 = gfc_get_expr ();
6568 c->expr1->expr_type = EXPR_FUNCTION;
6569 c->expr1->symtree = target;
6570 c->expr1->where = c->loc;
6571
6572 return resolve_call (c);
6573 }
6574
6575
6576 /* Resolve a component-call expression. */
6577 static bool
6578 resolve_compcall (gfc_expr* e, const char **name)
6579 {
6580 gfc_actual_arglist* newactual;
6581 gfc_symtree* target;
6582
6583 /* Check that's really a FUNCTION. */
6584 if (!e->value.compcall.tbp->function)
6585 {
6586 gfc_error ("%qs at %L should be a FUNCTION",
6587 e->value.compcall.name, &e->where);
6588 return false;
6589 }
6590
6591
6592 /* These must not be assign-calls! */
6593 gcc_assert (!e->value.compcall.assign);
6594
6595 if (!check_typebound_baseobject (e))
6596 return false;
6597
6598 /* Pass along the name for CLASS methods, where the vtab
6599 procedure pointer component has to be referenced. */
6600 if (name)
6601 *name = e->value.compcall.name;
6602
6603 if (!resolve_typebound_generic_call (e, name))
6604 return false;
6605 gcc_assert (!e->value.compcall.tbp->is_generic);
6606
6607 /* Take the rank from the function's symbol. */
6608 if (e->value.compcall.tbp->u.specific->n.sym->as)
6609 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
6610
6611 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
6612 arglist to the TBP's binding target. */
6613
6614 if (!resolve_typebound_static (e, &target, &newactual))
6615 return false;
6616
6617 e->value.function.actual = newactual;
6618 e->value.function.name = NULL;
6619 e->value.function.esym = target->n.sym;
6620 e->value.function.isym = NULL;
6621 e->symtree = target;
6622 e->ts = target->n.sym->ts;
6623 e->expr_type = EXPR_FUNCTION;
6624
6625 /* Resolution is not necessary if this is a class subroutine; this
6626 function only has to identify the specific proc. Resolution of
6627 the call will be done next in resolve_typebound_call. */
6628 return gfc_resolve_expr (e);
6629 }
6630
6631
6632 static bool resolve_fl_derived (gfc_symbol *sym);
6633
6634
6635 /* Resolve a typebound function, or 'method'. First separate all
6636 the non-CLASS references by calling resolve_compcall directly. */
6637
6638 static bool
6639 resolve_typebound_function (gfc_expr* e)
6640 {
6641 gfc_symbol *declared;
6642 gfc_component *c;
6643 gfc_ref *new_ref;
6644 gfc_ref *class_ref;
6645 gfc_symtree *st;
6646 const char *name;
6647 gfc_typespec ts;
6648 gfc_expr *expr;
6649 bool overridable;
6650
6651 st = e->symtree;
6652
6653 /* Deal with typebound operators for CLASS objects. */
6654 expr = e->value.compcall.base_object;
6655 overridable = !e->value.compcall.tbp->non_overridable;
6656 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
6657 {
6658 /* Since the typebound operators are generic, we have to ensure
6659 that any delays in resolution are corrected and that the vtab
6660 is present. */
6661 ts = expr->ts;
6662 declared = ts.u.derived;
6663 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6664 if (c->ts.u.derived == NULL)
6665 c->ts.u.derived = gfc_find_derived_vtab (declared);
6666
6667 if (!resolve_compcall (e, &name))
6668 return false;
6669
6670 /* Use the generic name if it is there. */
6671 name = name ? name : e->value.function.esym->name;
6672 e->symtree = expr->symtree;
6673 e->ref = gfc_copy_ref (expr->ref);
6674 get_declared_from_expr (&class_ref, NULL, e, false);
6675
6676 /* Trim away the extraneous references that emerge from nested
6677 use of interface.c (extend_expr). */
6678 if (class_ref && class_ref->next)
6679 {
6680 gfc_free_ref_list (class_ref->next);
6681 class_ref->next = NULL;
6682 }
6683 else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
6684 {
6685 gfc_free_ref_list (e->ref);
6686 e->ref = NULL;
6687 }
6688
6689 gfc_add_vptr_component (e);
6690 gfc_add_component_ref (e, name);
6691 e->value.function.esym = NULL;
6692 if (expr->expr_type != EXPR_VARIABLE)
6693 e->base_expr = expr;
6694 return true;
6695 }
6696
6697 if (st == NULL)
6698 return resolve_compcall (e, NULL);
6699
6700 if (!gfc_resolve_ref (e))
6701 return false;
6702
6703 /* Get the CLASS declared type. */
6704 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
6705
6706 if (!resolve_fl_derived (declared))
6707 return false;
6708
6709 /* Weed out cases of the ultimate component being a derived type. */
6710 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6711 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6712 {
6713 gfc_free_ref_list (new_ref);
6714 return resolve_compcall (e, NULL);
6715 }
6716
6717 c = gfc_find_component (declared, "_data", true, true, NULL);
6718
6719 /* Treat the call as if it is a typebound procedure, in order to roll
6720 out the correct name for the specific function. */
6721 if (!resolve_compcall (e, &name))
6722 {
6723 gfc_free_ref_list (new_ref);
6724 return false;
6725 }
6726 ts = e->ts;
6727
6728 if (overridable)
6729 {
6730 /* Convert the expression to a procedure pointer component call. */
6731 e->value.function.esym = NULL;
6732 e->symtree = st;
6733
6734 if (new_ref)
6735 e->ref = new_ref;
6736
6737 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6738 gfc_add_vptr_component (e);
6739 gfc_add_component_ref (e, name);
6740
6741 /* Recover the typespec for the expression. This is really only
6742 necessary for generic procedures, where the additional call
6743 to gfc_add_component_ref seems to throw the collection of the
6744 correct typespec. */
6745 e->ts = ts;
6746 }
6747 else if (new_ref)
6748 gfc_free_ref_list (new_ref);
6749
6750 return true;
6751 }
6752
6753 /* Resolve a typebound subroutine, or 'method'. First separate all
6754 the non-CLASS references by calling resolve_typebound_call
6755 directly. */
6756
6757 static bool
6758 resolve_typebound_subroutine (gfc_code *code)
6759 {
6760 gfc_symbol *declared;
6761 gfc_component *c;
6762 gfc_ref *new_ref;
6763 gfc_ref *class_ref;
6764 gfc_symtree *st;
6765 const char *name;
6766 gfc_typespec ts;
6767 gfc_expr *expr;
6768 bool overridable;
6769
6770 st = code->expr1->symtree;
6771
6772 /* Deal with typebound operators for CLASS objects. */
6773 expr = code->expr1->value.compcall.base_object;
6774 overridable = !code->expr1->value.compcall.tbp->non_overridable;
6775 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
6776 {
6777 /* If the base_object is not a variable, the corresponding actual
6778 argument expression must be stored in e->base_expression so
6779 that the corresponding tree temporary can be used as the base
6780 object in gfc_conv_procedure_call. */
6781 if (expr->expr_type != EXPR_VARIABLE)
6782 {
6783 gfc_actual_arglist *args;
6784
6785 args= code->expr1->value.function.actual;
6786 for (; args; args = args->next)
6787 if (expr == args->expr)
6788 expr = args->expr;
6789 }
6790
6791 /* Since the typebound operators are generic, we have to ensure
6792 that any delays in resolution are corrected and that the vtab
6793 is present. */
6794 declared = expr->ts.u.derived;
6795 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6796 if (c->ts.u.derived == NULL)
6797 c->ts.u.derived = gfc_find_derived_vtab (declared);
6798
6799 if (!resolve_typebound_call (code, &name, NULL))
6800 return false;
6801
6802 /* Use the generic name if it is there. */
6803 name = name ? name : code->expr1->value.function.esym->name;
6804 code->expr1->symtree = expr->symtree;
6805 code->expr1->ref = gfc_copy_ref (expr->ref);
6806
6807 /* Trim away the extraneous references that emerge from nested
6808 use of interface.c (extend_expr). */
6809 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
6810 if (class_ref && class_ref->next)
6811 {
6812 gfc_free_ref_list (class_ref->next);
6813 class_ref->next = NULL;
6814 }
6815 else if (code->expr1->ref && !class_ref)
6816 {
6817 gfc_free_ref_list (code->expr1->ref);
6818 code->expr1->ref = NULL;
6819 }
6820
6821 /* Now use the procedure in the vtable. */
6822 gfc_add_vptr_component (code->expr1);
6823 gfc_add_component_ref (code->expr1, name);
6824 code->expr1->value.function.esym = NULL;
6825 if (expr->expr_type != EXPR_VARIABLE)
6826 code->expr1->base_expr = expr;
6827 return true;
6828 }
6829
6830 if (st == NULL)
6831 return resolve_typebound_call (code, NULL, NULL);
6832
6833 if (!gfc_resolve_ref (code->expr1))
6834 return false;
6835
6836 /* Get the CLASS declared type. */
6837 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
6838
6839 /* Weed out cases of the ultimate component being a derived type. */
6840 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6841 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6842 {
6843 gfc_free_ref_list (new_ref);
6844 return resolve_typebound_call (code, NULL, NULL);
6845 }
6846
6847 if (!resolve_typebound_call (code, &name, &overridable))
6848 {
6849 gfc_free_ref_list (new_ref);
6850 return false;
6851 }
6852 ts = code->expr1->ts;
6853
6854 if (overridable)
6855 {
6856 /* Convert the expression to a procedure pointer component call. */
6857 code->expr1->value.function.esym = NULL;
6858 code->expr1->symtree = st;
6859
6860 if (new_ref)
6861 code->expr1->ref = new_ref;
6862
6863 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6864 gfc_add_vptr_component (code->expr1);
6865 gfc_add_component_ref (code->expr1, name);
6866
6867 /* Recover the typespec for the expression. This is really only
6868 necessary for generic procedures, where the additional call
6869 to gfc_add_component_ref seems to throw the collection of the
6870 correct typespec. */
6871 code->expr1->ts = ts;
6872 }
6873 else if (new_ref)
6874 gfc_free_ref_list (new_ref);
6875
6876 return true;
6877 }
6878
6879
6880 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6881
6882 static bool
6883 resolve_ppc_call (gfc_code* c)
6884 {
6885 gfc_component *comp;
6886
6887 comp = gfc_get_proc_ptr_comp (c->expr1);
6888 gcc_assert (comp != NULL);
6889
6890 c->resolved_sym = c->expr1->symtree->n.sym;
6891 c->expr1->expr_type = EXPR_VARIABLE;
6892
6893 if (!comp->attr.subroutine)
6894 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
6895
6896 if (!gfc_resolve_ref (c->expr1))
6897 return false;
6898
6899 if (!update_ppc_arglist (c->expr1))
6900 return false;
6901
6902 c->ext.actual = c->expr1->value.compcall.actual;
6903
6904 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6905 !(comp->ts.interface
6906 && comp->ts.interface->formal)))
6907 return false;
6908
6909 if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
6910 return false;
6911
6912 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6913
6914 return true;
6915 }
6916
6917
6918 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6919
6920 static bool
6921 resolve_expr_ppc (gfc_expr* e)
6922 {
6923 gfc_component *comp;
6924
6925 comp = gfc_get_proc_ptr_comp (e);
6926 gcc_assert (comp != NULL);
6927
6928 /* Convert to EXPR_FUNCTION. */
6929 e->expr_type = EXPR_FUNCTION;
6930 e->value.function.isym = NULL;
6931 e->value.function.actual = e->value.compcall.actual;
6932 e->ts = comp->ts;
6933 if (comp->as != NULL)
6934 e->rank = comp->as->rank;
6935
6936 if (!comp->attr.function)
6937 gfc_add_function (&comp->attr, comp->name, &e->where);
6938
6939 if (!gfc_resolve_ref (e))
6940 return false;
6941
6942 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6943 !(comp->ts.interface
6944 && comp->ts.interface->formal)))
6945 return false;
6946
6947 if (!update_ppc_arglist (e))
6948 return false;
6949
6950 if (!check_pure_function(e))
6951 return false;
6952
6953 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6954
6955 return true;
6956 }
6957
6958
6959 static bool
6960 gfc_is_expandable_expr (gfc_expr *e)
6961 {
6962 gfc_constructor *con;
6963
6964 if (e->expr_type == EXPR_ARRAY)
6965 {
6966 /* Traverse the constructor looking for variables that are flavor
6967 parameter. Parameters must be expanded since they are fully used at
6968 compile time. */
6969 con = gfc_constructor_first (e->value.constructor);
6970 for (; con; con = gfc_constructor_next (con))
6971 {
6972 if (con->expr->expr_type == EXPR_VARIABLE
6973 && con->expr->symtree
6974 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6975 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6976 return true;
6977 if (con->expr->expr_type == EXPR_ARRAY
6978 && gfc_is_expandable_expr (con->expr))
6979 return true;
6980 }
6981 }
6982
6983 return false;
6984 }
6985
6986
6987 /* Sometimes variables in specification expressions of the result
6988 of module procedures in submodules wind up not being the 'real'
6989 dummy. Find this, if possible, in the namespace of the first
6990 formal argument. */
6991
6992 static void
6993 fixup_unique_dummy (gfc_expr *e)
6994 {
6995 gfc_symtree *st = NULL;
6996 gfc_symbol *s = NULL;
6997
6998 if (e->symtree->n.sym->ns->proc_name
6999 && e->symtree->n.sym->ns->proc_name->formal)
7000 s = e->symtree->n.sym->ns->proc_name->formal->sym;
7001
7002 if (s != NULL)
7003 st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
7004
7005 if (st != NULL
7006 && st->n.sym != NULL
7007 && st->n.sym->attr.dummy)
7008 e->symtree = st;
7009 }
7010
7011 /* Resolve an expression. That is, make sure that types of operands agree
7012 with their operators, intrinsic operators are converted to function calls
7013 for overloaded types and unresolved function references are resolved. */
7014
7015 bool
7016 gfc_resolve_expr (gfc_expr *e)
7017 {
7018 bool t;
7019 bool inquiry_save, actual_arg_save, first_actual_arg_save;
7020
7021 if (e == NULL || e->do_not_resolve_again)
7022 return true;
7023
7024 /* inquiry_argument only applies to variables. */
7025 inquiry_save = inquiry_argument;
7026 actual_arg_save = actual_arg;
7027 first_actual_arg_save = first_actual_arg;
7028
7029 if (e->expr_type != EXPR_VARIABLE)
7030 {
7031 inquiry_argument = false;
7032 actual_arg = false;
7033 first_actual_arg = false;
7034 }
7035 else if (e->symtree != NULL
7036 && *e->symtree->name == '@'
7037 && e->symtree->n.sym->attr.dummy)
7038 {
7039 /* Deal with submodule specification expressions that are not
7040 found to be referenced in module.c(read_cleanup). */
7041 fixup_unique_dummy (e);
7042 }
7043
7044 switch (e->expr_type)
7045 {
7046 case EXPR_OP:
7047 t = resolve_operator (e);
7048 break;
7049
7050 case EXPR_FUNCTION:
7051 case EXPR_VARIABLE:
7052
7053 if (check_host_association (e))
7054 t = resolve_function (e);
7055 else
7056 t = resolve_variable (e);
7057
7058 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
7059 && e->ref->type != REF_SUBSTRING)
7060 gfc_resolve_substring_charlen (e);
7061
7062 break;
7063
7064 case EXPR_COMPCALL:
7065 t = resolve_typebound_function (e);
7066 break;
7067
7068 case EXPR_SUBSTRING:
7069 t = gfc_resolve_ref (e);
7070 break;
7071
7072 case EXPR_CONSTANT:
7073 case EXPR_NULL:
7074 t = true;
7075 break;
7076
7077 case EXPR_PPC:
7078 t = resolve_expr_ppc (e);
7079 break;
7080
7081 case EXPR_ARRAY:
7082 t = false;
7083 if (!gfc_resolve_ref (e))
7084 break;
7085
7086 t = gfc_resolve_array_constructor (e);
7087 /* Also try to expand a constructor. */
7088 if (t)
7089 {
7090 gfc_expression_rank (e);
7091 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
7092 gfc_expand_constructor (e, false);
7093 }
7094
7095 /* This provides the opportunity for the length of constructors with
7096 character valued function elements to propagate the string length
7097 to the expression. */
7098 if (t && e->ts.type == BT_CHARACTER)
7099 {
7100 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
7101 here rather then add a duplicate test for it above. */
7102 gfc_expand_constructor (e, false);
7103 t = gfc_resolve_character_array_constructor (e);
7104 }
7105
7106 break;
7107
7108 case EXPR_STRUCTURE:
7109 t = gfc_resolve_ref (e);
7110 if (!t)
7111 break;
7112
7113 t = resolve_structure_cons (e, 0);
7114 if (!t)
7115 break;
7116
7117 t = gfc_simplify_expr (e, 0);
7118 break;
7119
7120 default:
7121 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
7122 }
7123
7124 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
7125 fixup_charlen (e);
7126
7127 inquiry_argument = inquiry_save;
7128 actual_arg = actual_arg_save;
7129 first_actual_arg = first_actual_arg_save;
7130
7131 /* For some reason, resolving these expressions a second time mangles
7132 the typespec of the expression itself. */
7133 if (t && e->expr_type == EXPR_VARIABLE
7134 && e->symtree->n.sym->attr.select_rank_temporary
7135 && UNLIMITED_POLY (e->symtree->n.sym))
7136 e->do_not_resolve_again = 1;
7137
7138 return t;
7139 }
7140
7141
7142 /* Resolve an expression from an iterator. They must be scalar and have
7143 INTEGER or (optionally) REAL type. */
7144
7145 static bool
7146 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
7147 const char *name_msgid)
7148 {
7149 if (!gfc_resolve_expr (expr))
7150 return false;
7151
7152 if (expr->rank != 0)
7153 {
7154 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
7155 return false;
7156 }
7157
7158 if (expr->ts.type != BT_INTEGER)
7159 {
7160 if (expr->ts.type == BT_REAL)
7161 {
7162 if (real_ok)
7163 return gfc_notify_std (GFC_STD_F95_DEL,
7164 "%s at %L must be integer",
7165 _(name_msgid), &expr->where);
7166 else
7167 {
7168 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
7169 &expr->where);
7170 return false;
7171 }
7172 }
7173 else
7174 {
7175 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
7176 return false;
7177 }
7178 }
7179 return true;
7180 }
7181
7182
7183 /* Resolve the expressions in an iterator structure. If REAL_OK is
7184 false allow only INTEGER type iterators, otherwise allow REAL types.
7185 Set own_scope to true for ac-implied-do and data-implied-do as those
7186 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
7187
7188 bool
7189 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
7190 {
7191 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
7192 return false;
7193
7194 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
7195 _("iterator variable")))
7196 return false;
7197
7198 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
7199 "Start expression in DO loop"))
7200 return false;
7201
7202 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
7203 "End expression in DO loop"))
7204 return false;
7205
7206 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
7207 "Step expression in DO loop"))
7208 return false;
7209
7210 /* Convert start, end, and step to the same type as var. */
7211 if (iter->start->ts.kind != iter->var->ts.kind
7212 || iter->start->ts.type != iter->var->ts.type)
7213 gfc_convert_type (iter->start, &iter->var->ts, 1);
7214
7215 if (iter->end->ts.kind != iter->var->ts.kind
7216 || iter->end->ts.type != iter->var->ts.type)
7217 gfc_convert_type (iter->end, &iter->var->ts, 1);
7218
7219 if (iter->step->ts.kind != iter->var->ts.kind
7220 || iter->step->ts.type != iter->var->ts.type)
7221 gfc_convert_type (iter->step, &iter->var->ts, 1);
7222
7223 if (iter->step->expr_type == EXPR_CONSTANT)
7224 {
7225 if ((iter->step->ts.type == BT_INTEGER
7226 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
7227 || (iter->step->ts.type == BT_REAL
7228 && mpfr_sgn (iter->step->value.real) == 0))
7229 {
7230 gfc_error ("Step expression in DO loop at %L cannot be zero",
7231 &iter->step->where);
7232 return false;
7233 }
7234 }
7235
7236 if (iter->start->expr_type == EXPR_CONSTANT
7237 && iter->end->expr_type == EXPR_CONSTANT
7238 && iter->step->expr_type == EXPR_CONSTANT)
7239 {
7240 int sgn, cmp;
7241 if (iter->start->ts.type == BT_INTEGER)
7242 {
7243 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
7244 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
7245 }
7246 else
7247 {
7248 sgn = mpfr_sgn (iter->step->value.real);
7249 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
7250 }
7251 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
7252 gfc_warning (OPT_Wzerotrip,
7253 "DO loop at %L will be executed zero times",
7254 &iter->step->where);
7255 }
7256
7257 if (iter->end->expr_type == EXPR_CONSTANT
7258 && iter->end->ts.type == BT_INTEGER
7259 && iter->step->expr_type == EXPR_CONSTANT
7260 && iter->step->ts.type == BT_INTEGER
7261 && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
7262 || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
7263 {
7264 bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
7265 int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
7266
7267 if (is_step_positive
7268 && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
7269 gfc_warning (OPT_Wundefined_do_loop,
7270 "DO loop at %L is undefined as it overflows",
7271 &iter->step->where);
7272 else if (!is_step_positive
7273 && mpz_cmp (iter->end->value.integer,
7274 gfc_integer_kinds[k].min_int) == 0)
7275 gfc_warning (OPT_Wundefined_do_loop,
7276 "DO loop at %L is undefined as it underflows",
7277 &iter->step->where);
7278 }
7279
7280 return true;
7281 }
7282
7283
7284 /* Traversal function for find_forall_index. f == 2 signals that
7285 that variable itself is not to be checked - only the references. */
7286
7287 static bool
7288 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
7289 {
7290 if (expr->expr_type != EXPR_VARIABLE)
7291 return false;
7292
7293 /* A scalar assignment */
7294 if (!expr->ref || *f == 1)
7295 {
7296 if (expr->symtree->n.sym == sym)
7297 return true;
7298 else
7299 return false;
7300 }
7301
7302 if (*f == 2)
7303 *f = 1;
7304 return false;
7305 }
7306
7307
7308 /* Check whether the FORALL index appears in the expression or not.
7309 Returns true if SYM is found in EXPR. */
7310
7311 bool
7312 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
7313 {
7314 if (gfc_traverse_expr (expr, sym, forall_index, f))
7315 return true;
7316 else
7317 return false;
7318 }
7319
7320
7321 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
7322 to be a scalar INTEGER variable. The subscripts and stride are scalar
7323 INTEGERs, and if stride is a constant it must be nonzero.
7324 Furthermore "A subscript or stride in a forall-triplet-spec shall
7325 not contain a reference to any index-name in the
7326 forall-triplet-spec-list in which it appears." (7.5.4.1) */
7327
7328 static void
7329 resolve_forall_iterators (gfc_forall_iterator *it)
7330 {
7331 gfc_forall_iterator *iter, *iter2;
7332
7333 for (iter = it; iter; iter = iter->next)
7334 {
7335 if (gfc_resolve_expr (iter->var)
7336 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
7337 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
7338 &iter->var->where);
7339
7340 if (gfc_resolve_expr (iter->start)
7341 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
7342 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
7343 &iter->start->where);
7344 if (iter->var->ts.kind != iter->start->ts.kind)
7345 gfc_convert_type (iter->start, &iter->var->ts, 1);
7346
7347 if (gfc_resolve_expr (iter->end)
7348 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
7349 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
7350 &iter->end->where);
7351 if (iter->var->ts.kind != iter->end->ts.kind)
7352 gfc_convert_type (iter->end, &iter->var->ts, 1);
7353
7354 if (gfc_resolve_expr (iter->stride))
7355 {
7356 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
7357 gfc_error ("FORALL stride expression at %L must be a scalar %s",
7358 &iter->stride->where, "INTEGER");
7359
7360 if (iter->stride->expr_type == EXPR_CONSTANT
7361 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
7362 gfc_error ("FORALL stride expression at %L cannot be zero",
7363 &iter->stride->where);
7364 }
7365 if (iter->var->ts.kind != iter->stride->ts.kind)
7366 gfc_convert_type (iter->stride, &iter->var->ts, 1);
7367 }
7368
7369 for (iter = it; iter; iter = iter->next)
7370 for (iter2 = iter; iter2; iter2 = iter2->next)
7371 {
7372 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
7373 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
7374 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
7375 gfc_error ("FORALL index %qs may not appear in triplet "
7376 "specification at %L", iter->var->symtree->name,
7377 &iter2->start->where);
7378 }
7379 }
7380
7381
7382 /* Given a pointer to a symbol that is a derived type, see if it's
7383 inaccessible, i.e. if it's defined in another module and the components are
7384 PRIVATE. The search is recursive if necessary. Returns zero if no
7385 inaccessible components are found, nonzero otherwise. */
7386
7387 static int
7388 derived_inaccessible (gfc_symbol *sym)
7389 {
7390 gfc_component *c;
7391
7392 if (sym->attr.use_assoc && sym->attr.private_comp)
7393 return 1;
7394
7395 for (c = sym->components; c; c = c->next)
7396 {
7397 /* Prevent an infinite loop through this function. */
7398 if (c->ts.type == BT_DERIVED && c->attr.pointer
7399 && sym == c->ts.u.derived)
7400 continue;
7401
7402 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
7403 return 1;
7404 }
7405
7406 return 0;
7407 }
7408
7409
7410 /* Resolve the argument of a deallocate expression. The expression must be
7411 a pointer or a full array. */
7412
7413 static bool
7414 resolve_deallocate_expr (gfc_expr *e)
7415 {
7416 symbol_attribute attr;
7417 int allocatable, pointer;
7418 gfc_ref *ref;
7419 gfc_symbol *sym;
7420 gfc_component *c;
7421 bool unlimited;
7422
7423 if (!gfc_resolve_expr (e))
7424 return false;
7425
7426 if (e->expr_type != EXPR_VARIABLE)
7427 goto bad;
7428
7429 sym = e->symtree->n.sym;
7430 unlimited = UNLIMITED_POLY(sym);
7431
7432 if (sym->ts.type == BT_CLASS)
7433 {
7434 allocatable = CLASS_DATA (sym)->attr.allocatable;
7435 pointer = CLASS_DATA (sym)->attr.class_pointer;
7436 }
7437 else
7438 {
7439 allocatable = sym->attr.allocatable;
7440 pointer = sym->attr.pointer;
7441 }
7442 for (ref = e->ref; ref; ref = ref->next)
7443 {
7444 switch (ref->type)
7445 {
7446 case REF_ARRAY:
7447 if (ref->u.ar.type != AR_FULL
7448 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
7449 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
7450 allocatable = 0;
7451 break;
7452
7453 case REF_COMPONENT:
7454 c = ref->u.c.component;
7455 if (c->ts.type == BT_CLASS)
7456 {
7457 allocatable = CLASS_DATA (c)->attr.allocatable;
7458 pointer = CLASS_DATA (c)->attr.class_pointer;
7459 }
7460 else
7461 {
7462 allocatable = c->attr.allocatable;
7463 pointer = c->attr.pointer;
7464 }
7465 break;
7466
7467 case REF_SUBSTRING:
7468 case REF_INQUIRY:
7469 allocatable = 0;
7470 break;
7471 }
7472 }
7473
7474 attr = gfc_expr_attr (e);
7475
7476 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
7477 {
7478 bad:
7479 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7480 &e->where);
7481 return false;
7482 }
7483
7484 /* F2008, C644. */
7485 if (gfc_is_coindexed (e))
7486 {
7487 gfc_error ("Coindexed allocatable object at %L", &e->where);
7488 return false;
7489 }
7490
7491 if (pointer
7492 && !gfc_check_vardef_context (e, true, true, false,
7493 _("DEALLOCATE object")))
7494 return false;
7495 if (!gfc_check_vardef_context (e, false, true, false,
7496 _("DEALLOCATE object")))
7497 return false;
7498
7499 return true;
7500 }
7501
7502
7503 /* Returns true if the expression e contains a reference to the symbol sym. */
7504 static bool
7505 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
7506 {
7507 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
7508 return true;
7509
7510 return false;
7511 }
7512
7513 bool
7514 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
7515 {
7516 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
7517 }
7518
7519
7520 /* Given the expression node e for an allocatable/pointer of derived type to be
7521 allocated, get the expression node to be initialized afterwards (needed for
7522 derived types with default initializers, and derived types with allocatable
7523 components that need nullification.) */
7524
7525 gfc_expr *
7526 gfc_expr_to_initialize (gfc_expr *e)
7527 {
7528 gfc_expr *result;
7529 gfc_ref *ref;
7530 int i;
7531
7532 result = gfc_copy_expr (e);
7533
7534 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
7535 for (ref = result->ref; ref; ref = ref->next)
7536 if (ref->type == REF_ARRAY && ref->next == NULL)
7537 {
7538 if (ref->u.ar.dimen == 0
7539 && ref->u.ar.as && ref->u.ar.as->corank)
7540 return result;
7541
7542 ref->u.ar.type = AR_FULL;
7543
7544 for (i = 0; i < ref->u.ar.dimen; i++)
7545 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
7546
7547 break;
7548 }
7549
7550 gfc_free_shape (&result->shape, result->rank);
7551
7552 /* Recalculate rank, shape, etc. */
7553 gfc_resolve_expr (result);
7554 return result;
7555 }
7556
7557
7558 /* If the last ref of an expression is an array ref, return a copy of the
7559 expression with that one removed. Otherwise, a copy of the original
7560 expression. This is used for allocate-expressions and pointer assignment
7561 LHS, where there may be an array specification that needs to be stripped
7562 off when using gfc_check_vardef_context. */
7563
7564 static gfc_expr*
7565 remove_last_array_ref (gfc_expr* e)
7566 {
7567 gfc_expr* e2;
7568 gfc_ref** r;
7569
7570 e2 = gfc_copy_expr (e);
7571 for (r = &e2->ref; *r; r = &(*r)->next)
7572 if ((*r)->type == REF_ARRAY && !(*r)->next)
7573 {
7574 gfc_free_ref_list (*r);
7575 *r = NULL;
7576 break;
7577 }
7578
7579 return e2;
7580 }
7581
7582
7583 /* Used in resolve_allocate_expr to check that a allocation-object and
7584 a source-expr are conformable. This does not catch all possible
7585 cases; in particular a runtime checking is needed. */
7586
7587 static bool
7588 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
7589 {
7590 gfc_ref *tail;
7591 for (tail = e2->ref; tail && tail->next; tail = tail->next);
7592
7593 /* First compare rank. */
7594 if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
7595 || (!tail && e1->rank != e2->rank))
7596 {
7597 gfc_error ("Source-expr at %L must be scalar or have the "
7598 "same rank as the allocate-object at %L",
7599 &e1->where, &e2->where);
7600 return false;
7601 }
7602
7603 if (e1->shape)
7604 {
7605 int i;
7606 mpz_t s;
7607
7608 mpz_init (s);
7609
7610 for (i = 0; i < e1->rank; i++)
7611 {
7612 if (tail->u.ar.start[i] == NULL)
7613 break;
7614
7615 if (tail->u.ar.end[i])
7616 {
7617 mpz_set (s, tail->u.ar.end[i]->value.integer);
7618 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
7619 mpz_add_ui (s, s, 1);
7620 }
7621 else
7622 {
7623 mpz_set (s, tail->u.ar.start[i]->value.integer);
7624 }
7625
7626 if (mpz_cmp (e1->shape[i], s) != 0)
7627 {
7628 gfc_error ("Source-expr at %L and allocate-object at %L must "
7629 "have the same shape", &e1->where, &e2->where);
7630 mpz_clear (s);
7631 return false;
7632 }
7633 }
7634
7635 mpz_clear (s);
7636 }
7637
7638 return true;
7639 }
7640
7641
7642 /* Resolve the expression in an ALLOCATE statement, doing the additional
7643 checks to see whether the expression is OK or not. The expression must
7644 have a trailing array reference that gives the size of the array. */
7645
7646 static bool
7647 resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
7648 {
7649 int i, pointer, allocatable, dimension, is_abstract;
7650 int codimension;
7651 bool coindexed;
7652 bool unlimited;
7653 symbol_attribute attr;
7654 gfc_ref *ref, *ref2;
7655 gfc_expr *e2;
7656 gfc_array_ref *ar;
7657 gfc_symbol *sym = NULL;
7658 gfc_alloc *a;
7659 gfc_component *c;
7660 bool t;
7661
7662 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
7663 checking of coarrays. */
7664 for (ref = e->ref; ref; ref = ref->next)
7665 if (ref->next == NULL)
7666 break;
7667
7668 if (ref && ref->type == REF_ARRAY)
7669 ref->u.ar.in_allocate = true;
7670
7671 if (!gfc_resolve_expr (e))
7672 goto failure;
7673
7674 /* Make sure the expression is allocatable or a pointer. If it is
7675 pointer, the next-to-last reference must be a pointer. */
7676
7677 ref2 = NULL;
7678 if (e->symtree)
7679 sym = e->symtree->n.sym;
7680
7681 /* Check whether ultimate component is abstract and CLASS. */
7682 is_abstract = 0;
7683
7684 /* Is the allocate-object unlimited polymorphic? */
7685 unlimited = UNLIMITED_POLY(e);
7686
7687 if (e->expr_type != EXPR_VARIABLE)
7688 {
7689 allocatable = 0;
7690 attr = gfc_expr_attr (e);
7691 pointer = attr.pointer;
7692 dimension = attr.dimension;
7693 codimension = attr.codimension;
7694 }
7695 else
7696 {
7697 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
7698 {
7699 allocatable = CLASS_DATA (sym)->attr.allocatable;
7700 pointer = CLASS_DATA (sym)->attr.class_pointer;
7701 dimension = CLASS_DATA (sym)->attr.dimension;
7702 codimension = CLASS_DATA (sym)->attr.codimension;
7703 is_abstract = CLASS_DATA (sym)->attr.abstract;
7704 }
7705 else
7706 {
7707 allocatable = sym->attr.allocatable;
7708 pointer = sym->attr.pointer;
7709 dimension = sym->attr.dimension;
7710 codimension = sym->attr.codimension;
7711 }
7712
7713 coindexed = false;
7714
7715 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
7716 {
7717 switch (ref->type)
7718 {
7719 case REF_ARRAY:
7720 if (ref->u.ar.codimen > 0)
7721 {
7722 int n;
7723 for (n = ref->u.ar.dimen;
7724 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
7725 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
7726 {
7727 coindexed = true;
7728 break;
7729 }
7730 }
7731
7732 if (ref->next != NULL)
7733 pointer = 0;
7734 break;
7735
7736 case REF_COMPONENT:
7737 /* F2008, C644. */
7738 if (coindexed)
7739 {
7740 gfc_error ("Coindexed allocatable object at %L",
7741 &e->where);
7742 goto failure;
7743 }
7744
7745 c = ref->u.c.component;
7746 if (c->ts.type == BT_CLASS)
7747 {
7748 allocatable = CLASS_DATA (c)->attr.allocatable;
7749 pointer = CLASS_DATA (c)->attr.class_pointer;
7750 dimension = CLASS_DATA (c)->attr.dimension;
7751 codimension = CLASS_DATA (c)->attr.codimension;
7752 is_abstract = CLASS_DATA (c)->attr.abstract;
7753 }
7754 else
7755 {
7756 allocatable = c->attr.allocatable;
7757 pointer = c->attr.pointer;
7758 dimension = c->attr.dimension;
7759 codimension = c->attr.codimension;
7760 is_abstract = c->attr.abstract;
7761 }
7762 break;
7763
7764 case REF_SUBSTRING:
7765 case REF_INQUIRY:
7766 allocatable = 0;
7767 pointer = 0;
7768 break;
7769 }
7770 }
7771 }
7772
7773 /* Check for F08:C628. */
7774 if (allocatable == 0 && pointer == 0 && !unlimited)
7775 {
7776 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7777 &e->where);
7778 goto failure;
7779 }
7780
7781 /* Some checks for the SOURCE tag. */
7782 if (code->expr3)
7783 {
7784 /* Check F03:C631. */
7785 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
7786 {
7787 gfc_error ("Type of entity at %L is type incompatible with "
7788 "source-expr at %L", &e->where, &code->expr3->where);
7789 goto failure;
7790 }
7791
7792 /* Check F03:C632 and restriction following Note 6.18. */
7793 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
7794 goto failure;
7795
7796 /* Check F03:C633. */
7797 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
7798 {
7799 gfc_error ("The allocate-object at %L and the source-expr at %L "
7800 "shall have the same kind type parameter",
7801 &e->where, &code->expr3->where);
7802 goto failure;
7803 }
7804
7805 /* Check F2008, C642. */
7806 if (code->expr3->ts.type == BT_DERIVED
7807 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
7808 || (code->expr3->ts.u.derived->from_intmod
7809 == INTMOD_ISO_FORTRAN_ENV
7810 && code->expr3->ts.u.derived->intmod_sym_id
7811 == ISOFORTRAN_LOCK_TYPE)))
7812 {
7813 gfc_error ("The source-expr at %L shall neither be of type "
7814 "LOCK_TYPE nor have a LOCK_TYPE component if "
7815 "allocate-object at %L is a coarray",
7816 &code->expr3->where, &e->where);
7817 goto failure;
7818 }
7819
7820 /* Check TS18508, C702/C703. */
7821 if (code->expr3->ts.type == BT_DERIVED
7822 && ((codimension && gfc_expr_attr (code->expr3).event_comp)
7823 || (code->expr3->ts.u.derived->from_intmod
7824 == INTMOD_ISO_FORTRAN_ENV
7825 && code->expr3->ts.u.derived->intmod_sym_id
7826 == ISOFORTRAN_EVENT_TYPE)))
7827 {
7828 gfc_error ("The source-expr at %L shall neither be of type "
7829 "EVENT_TYPE nor have a EVENT_TYPE component if "
7830 "allocate-object at %L is a coarray",
7831 &code->expr3->where, &e->where);
7832 goto failure;
7833 }
7834 }
7835
7836 /* Check F08:C629. */
7837 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
7838 && !code->expr3)
7839 {
7840 gcc_assert (e->ts.type == BT_CLASS);
7841 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
7842 "type-spec or source-expr", sym->name, &e->where);
7843 goto failure;
7844 }
7845
7846 /* Check F08:C632. */
7847 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
7848 && !UNLIMITED_POLY (e))
7849 {
7850 int cmp;
7851
7852 if (!e->ts.u.cl->length)
7853 goto failure;
7854
7855 cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
7856 code->ext.alloc.ts.u.cl->length);
7857 if (cmp == 1 || cmp == -1 || cmp == -3)
7858 {
7859 gfc_error ("Allocating %s at %L with type-spec requires the same "
7860 "character-length parameter as in the declaration",
7861 sym->name, &e->where);
7862 goto failure;
7863 }
7864 }
7865
7866 /* In the variable definition context checks, gfc_expr_attr is used
7867 on the expression. This is fooled by the array specification
7868 present in e, thus we have to eliminate that one temporarily. */
7869 e2 = remove_last_array_ref (e);
7870 t = true;
7871 if (t && pointer)
7872 t = gfc_check_vardef_context (e2, true, true, false,
7873 _("ALLOCATE object"));
7874 if (t)
7875 t = gfc_check_vardef_context (e2, false, true, false,
7876 _("ALLOCATE object"));
7877 gfc_free_expr (e2);
7878 if (!t)
7879 goto failure;
7880
7881 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
7882 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
7883 {
7884 /* For class arrays, the initialization with SOURCE is done
7885 using _copy and trans_call. It is convenient to exploit that
7886 when the allocated type is different from the declared type but
7887 no SOURCE exists by setting expr3. */
7888 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
7889 }
7890 else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
7891 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
7892 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
7893 {
7894 /* We have to zero initialize the integer variable. */
7895 code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
7896 }
7897
7898 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
7899 {
7900 /* Make sure the vtab symbol is present when
7901 the module variables are generated. */
7902 gfc_typespec ts = e->ts;
7903 if (code->expr3)
7904 ts = code->expr3->ts;
7905 else if (code->ext.alloc.ts.type == BT_DERIVED)
7906 ts = code->ext.alloc.ts;
7907
7908 /* Finding the vtab also publishes the type's symbol. Therefore this
7909 statement is necessary. */
7910 gfc_find_derived_vtab (ts.u.derived);
7911 }
7912 else if (unlimited && !UNLIMITED_POLY (code->expr3))
7913 {
7914 /* Again, make sure the vtab symbol is present when
7915 the module variables are generated. */
7916 gfc_typespec *ts = NULL;
7917 if (code->expr3)
7918 ts = &code->expr3->ts;
7919 else
7920 ts = &code->ext.alloc.ts;
7921
7922 gcc_assert (ts);
7923
7924 /* Finding the vtab also publishes the type's symbol. Therefore this
7925 statement is necessary. */
7926 gfc_find_vtab (ts);
7927 }
7928
7929 if (dimension == 0 && codimension == 0)
7930 goto success;
7931
7932 /* Make sure the last reference node is an array specification. */
7933
7934 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
7935 || (dimension && ref2->u.ar.dimen == 0))
7936 {
7937 /* F08:C633. */
7938 if (code->expr3)
7939 {
7940 if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
7941 "in ALLOCATE statement at %L", &e->where))
7942 goto failure;
7943 if (code->expr3->rank != 0)
7944 *array_alloc_wo_spec = true;
7945 else
7946 {
7947 gfc_error ("Array specification or array-valued SOURCE= "
7948 "expression required in ALLOCATE statement at %L",
7949 &e->where);
7950 goto failure;
7951 }
7952 }
7953 else
7954 {
7955 gfc_error ("Array specification required in ALLOCATE statement "
7956 "at %L", &e->where);
7957 goto failure;
7958 }
7959 }
7960
7961 /* Make sure that the array section reference makes sense in the
7962 context of an ALLOCATE specification. */
7963
7964 ar = &ref2->u.ar;
7965
7966 if (codimension)
7967 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
7968 {
7969 switch (ar->dimen_type[i])
7970 {
7971 case DIMEN_THIS_IMAGE:
7972 gfc_error ("Coarray specification required in ALLOCATE statement "
7973 "at %L", &e->where);
7974 goto failure;
7975
7976 case DIMEN_RANGE:
7977 if (ar->start[i] == 0 || ar->end[i] == 0)
7978 {
7979 /* If ar->stride[i] is NULL, we issued a previous error. */
7980 if (ar->stride[i] == NULL)
7981 gfc_error ("Bad array specification in ALLOCATE statement "
7982 "at %L", &e->where);
7983 goto failure;
7984 }
7985 else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
7986 {
7987 gfc_error ("Upper cobound is less than lower cobound at %L",
7988 &ar->start[i]->where);
7989 goto failure;
7990 }
7991 break;
7992
7993 case DIMEN_ELEMENT:
7994 if (ar->start[i]->expr_type == EXPR_CONSTANT)
7995 {
7996 gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
7997 if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
7998 {
7999 gfc_error ("Upper cobound is less than lower cobound "
8000 "of 1 at %L", &ar->start[i]->where);
8001 goto failure;
8002 }
8003 }
8004 break;
8005
8006 case DIMEN_STAR:
8007 break;
8008
8009 default:
8010 gfc_error ("Bad array specification in ALLOCATE statement at %L",
8011 &e->where);
8012 goto failure;
8013
8014 }
8015 }
8016 for (i = 0; i < ar->dimen; i++)
8017 {
8018 if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
8019 goto check_symbols;
8020
8021 switch (ar->dimen_type[i])
8022 {
8023 case DIMEN_ELEMENT:
8024 break;
8025
8026 case DIMEN_RANGE:
8027 if (ar->start[i] != NULL
8028 && ar->end[i] != NULL
8029 && ar->stride[i] == NULL)
8030 break;
8031
8032 /* Fall through. */
8033
8034 case DIMEN_UNKNOWN:
8035 case DIMEN_VECTOR:
8036 case DIMEN_STAR:
8037 case DIMEN_THIS_IMAGE:
8038 gfc_error ("Bad array specification in ALLOCATE statement at %L",
8039 &e->where);
8040 goto failure;
8041 }
8042
8043 check_symbols:
8044 for (a = code->ext.alloc.list; a; a = a->next)
8045 {
8046 sym = a->expr->symtree->n.sym;
8047
8048 /* TODO - check derived type components. */
8049 if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
8050 continue;
8051
8052 if ((ar->start[i] != NULL
8053 && gfc_find_sym_in_expr (sym, ar->start[i]))
8054 || (ar->end[i] != NULL
8055 && gfc_find_sym_in_expr (sym, ar->end[i])))
8056 {
8057 gfc_error ("%qs must not appear in the array specification at "
8058 "%L in the same ALLOCATE statement where it is "
8059 "itself allocated", sym->name, &ar->where);
8060 goto failure;
8061 }
8062 }
8063 }
8064
8065 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
8066 {
8067 if (ar->dimen_type[i] == DIMEN_ELEMENT
8068 || ar->dimen_type[i] == DIMEN_RANGE)
8069 {
8070 if (i == (ar->dimen + ar->codimen - 1))
8071 {
8072 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
8073 "statement at %L", &e->where);
8074 goto failure;
8075 }
8076 continue;
8077 }
8078
8079 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
8080 && ar->stride[i] == NULL)
8081 break;
8082
8083 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
8084 &e->where);
8085 goto failure;
8086 }
8087
8088 success:
8089 return true;
8090
8091 failure:
8092 return false;
8093 }
8094
8095
8096 static void
8097 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
8098 {
8099 gfc_expr *stat, *errmsg, *pe, *qe;
8100 gfc_alloc *a, *p, *q;
8101
8102 stat = code->expr1;
8103 errmsg = code->expr2;
8104
8105 /* Check the stat variable. */
8106 if (stat)
8107 {
8108 gfc_check_vardef_context (stat, false, false, false,
8109 _("STAT variable"));
8110
8111 if ((stat->ts.type != BT_INTEGER
8112 && !(stat->ref && (stat->ref->type == REF_ARRAY
8113 || stat->ref->type == REF_COMPONENT)))
8114 || stat->rank > 0)
8115 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
8116 "variable", &stat->where);
8117
8118 for (p = code->ext.alloc.list; p; p = p->next)
8119 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
8120 {
8121 gfc_ref *ref1, *ref2;
8122 bool found = true;
8123
8124 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
8125 ref1 = ref1->next, ref2 = ref2->next)
8126 {
8127 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
8128 continue;
8129 if (ref1->u.c.component->name != ref2->u.c.component->name)
8130 {
8131 found = false;
8132 break;
8133 }
8134 }
8135
8136 if (found)
8137 {
8138 gfc_error ("Stat-variable at %L shall not be %sd within "
8139 "the same %s statement", &stat->where, fcn, fcn);
8140 break;
8141 }
8142 }
8143 }
8144
8145 /* Check the errmsg variable. */
8146 if (errmsg)
8147 {
8148 if (!stat)
8149 gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
8150 &errmsg->where);
8151
8152 gfc_check_vardef_context (errmsg, false, false, false,
8153 _("ERRMSG variable"));
8154
8155 /* F18:R928 alloc-opt is ERRMSG = errmsg-variable
8156 F18:R930 errmsg-variable is scalar-default-char-variable
8157 F18:R906 default-char-variable is variable
8158 F18:C906 default-char-variable shall be default character. */
8159 if ((errmsg->ts.type != BT_CHARACTER
8160 && !(errmsg->ref
8161 && (errmsg->ref->type == REF_ARRAY
8162 || errmsg->ref->type == REF_COMPONENT)))
8163 || errmsg->rank > 0
8164 || errmsg->ts.kind != gfc_default_character_kind)
8165 gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
8166 "variable", &errmsg->where);
8167
8168 for (p = code->ext.alloc.list; p; p = p->next)
8169 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
8170 {
8171 gfc_ref *ref1, *ref2;
8172 bool found = true;
8173
8174 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
8175 ref1 = ref1->next, ref2 = ref2->next)
8176 {
8177 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
8178 continue;
8179 if (ref1->u.c.component->name != ref2->u.c.component->name)
8180 {
8181 found = false;
8182 break;
8183 }
8184 }
8185
8186 if (found)
8187 {
8188 gfc_error ("Errmsg-variable at %L shall not be %sd within "
8189 "the same %s statement", &errmsg->where, fcn, fcn);
8190 break;
8191 }
8192 }
8193 }
8194
8195 /* Check that an allocate-object appears only once in the statement. */
8196
8197 for (p = code->ext.alloc.list; p; p = p->next)
8198 {
8199 pe = p->expr;
8200 for (q = p->next; q; q = q->next)
8201 {
8202 qe = q->expr;
8203 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
8204 {
8205 /* This is a potential collision. */
8206 gfc_ref *pr = pe->ref;
8207 gfc_ref *qr = qe->ref;
8208
8209 /* Follow the references until
8210 a) They start to differ, in which case there is no error;
8211 you can deallocate a%b and a%c in a single statement
8212 b) Both of them stop, which is an error
8213 c) One of them stops, which is also an error. */
8214 while (1)
8215 {
8216 if (pr == NULL && qr == NULL)
8217 {
8218 gfc_error ("Allocate-object at %L also appears at %L",
8219 &pe->where, &qe->where);
8220 break;
8221 }
8222 else if (pr != NULL && qr == NULL)
8223 {
8224 gfc_error ("Allocate-object at %L is subobject of"
8225 " object at %L", &pe->where, &qe->where);
8226 break;
8227 }
8228 else if (pr == NULL && qr != NULL)
8229 {
8230 gfc_error ("Allocate-object at %L is subobject of"
8231 " object at %L", &qe->where, &pe->where);
8232 break;
8233 }
8234 /* Here, pr != NULL && qr != NULL */
8235 gcc_assert(pr->type == qr->type);
8236 if (pr->type == REF_ARRAY)
8237 {
8238 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
8239 which are legal. */
8240 gcc_assert (qr->type == REF_ARRAY);
8241
8242 if (pr->next && qr->next)
8243 {
8244 int i;
8245 gfc_array_ref *par = &(pr->u.ar);
8246 gfc_array_ref *qar = &(qr->u.ar);
8247
8248 for (i=0; i<par->dimen; i++)
8249 {
8250 if ((par->start[i] != NULL
8251 || qar->start[i] != NULL)
8252 && gfc_dep_compare_expr (par->start[i],
8253 qar->start[i]) != 0)
8254 goto break_label;
8255 }
8256 }
8257 }
8258 else
8259 {
8260 if (pr->u.c.component->name != qr->u.c.component->name)
8261 break;
8262 }
8263
8264 pr = pr->next;
8265 qr = qr->next;
8266 }
8267 break_label:
8268 ;
8269 }
8270 }
8271 }
8272
8273 if (strcmp (fcn, "ALLOCATE") == 0)
8274 {
8275 bool arr_alloc_wo_spec = false;
8276
8277 /* Resolving the expr3 in the loop over all objects to allocate would
8278 execute loop invariant code for each loop item. Therefore do it just
8279 once here. */
8280 if (code->expr3 && code->expr3->mold
8281 && code->expr3->ts.type == BT_DERIVED)
8282 {
8283 /* Default initialization via MOLD (non-polymorphic). */
8284 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
8285 if (rhs != NULL)
8286 {
8287 gfc_resolve_expr (rhs);
8288 gfc_free_expr (code->expr3);
8289 code->expr3 = rhs;
8290 }
8291 }
8292 for (a = code->ext.alloc.list; a; a = a->next)
8293 resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
8294
8295 if (arr_alloc_wo_spec && code->expr3)
8296 {
8297 /* Mark the allocate to have to take the array specification
8298 from the expr3. */
8299 code->ext.alloc.arr_spec_from_expr3 = 1;
8300 }
8301 }
8302 else
8303 {
8304 for (a = code->ext.alloc.list; a; a = a->next)
8305 resolve_deallocate_expr (a->expr);
8306 }
8307 }
8308
8309
8310 /************ SELECT CASE resolution subroutines ************/
8311
8312 /* Callback function for our mergesort variant. Determines interval
8313 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
8314 op1 > op2. Assumes we're not dealing with the default case.
8315 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
8316 There are nine situations to check. */
8317
8318 static int
8319 compare_cases (const gfc_case *op1, const gfc_case *op2)
8320 {
8321 int retval;
8322
8323 if (op1->low == NULL) /* op1 = (:L) */
8324 {
8325 /* op2 = (:N), so overlap. */
8326 retval = 0;
8327 /* op2 = (M:) or (M:N), L < M */
8328 if (op2->low != NULL
8329 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8330 retval = -1;
8331 }
8332 else if (op1->high == NULL) /* op1 = (K:) */
8333 {
8334 /* op2 = (M:), so overlap. */
8335 retval = 0;
8336 /* op2 = (:N) or (M:N), K > N */
8337 if (op2->high != NULL
8338 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8339 retval = 1;
8340 }
8341 else /* op1 = (K:L) */
8342 {
8343 if (op2->low == NULL) /* op2 = (:N), K > N */
8344 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8345 ? 1 : 0;
8346 else if (op2->high == NULL) /* op2 = (M:), L < M */
8347 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8348 ? -1 : 0;
8349 else /* op2 = (M:N) */
8350 {
8351 retval = 0;
8352 /* L < M */
8353 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
8354 retval = -1;
8355 /* K > N */
8356 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
8357 retval = 1;
8358 }
8359 }
8360
8361 return retval;
8362 }
8363
8364
8365 /* Merge-sort a double linked case list, detecting overlap in the
8366 process. LIST is the head of the double linked case list before it
8367 is sorted. Returns the head of the sorted list if we don't see any
8368 overlap, or NULL otherwise. */
8369
8370 static gfc_case *
8371 check_case_overlap (gfc_case *list)
8372 {
8373 gfc_case *p, *q, *e, *tail;
8374 int insize, nmerges, psize, qsize, cmp, overlap_seen;
8375
8376 /* If the passed list was empty, return immediately. */
8377 if (!list)
8378 return NULL;
8379
8380 overlap_seen = 0;
8381 insize = 1;
8382
8383 /* Loop unconditionally. The only exit from this loop is a return
8384 statement, when we've finished sorting the case list. */
8385 for (;;)
8386 {
8387 p = list;
8388 list = NULL;
8389 tail = NULL;
8390
8391 /* Count the number of merges we do in this pass. */
8392 nmerges = 0;
8393
8394 /* Loop while there exists a merge to be done. */
8395 while (p)
8396 {
8397 int i;
8398
8399 /* Count this merge. */
8400 nmerges++;
8401
8402 /* Cut the list in two pieces by stepping INSIZE places
8403 forward in the list, starting from P. */
8404 psize = 0;
8405 q = p;
8406 for (i = 0; i < insize; i++)
8407 {
8408 psize++;
8409 q = q->right;
8410 if (!q)
8411 break;
8412 }
8413 qsize = insize;
8414
8415 /* Now we have two lists. Merge them! */
8416 while (psize > 0 || (qsize > 0 && q != NULL))
8417 {
8418 /* See from which the next case to merge comes from. */
8419 if (psize == 0)
8420 {
8421 /* P is empty so the next case must come from Q. */
8422 e = q;
8423 q = q->right;
8424 qsize--;
8425 }
8426 else if (qsize == 0 || q == NULL)
8427 {
8428 /* Q is empty. */
8429 e = p;
8430 p = p->right;
8431 psize--;
8432 }
8433 else
8434 {
8435 cmp = compare_cases (p, q);
8436 if (cmp < 0)
8437 {
8438 /* The whole case range for P is less than the
8439 one for Q. */
8440 e = p;
8441 p = p->right;
8442 psize--;
8443 }
8444 else if (cmp > 0)
8445 {
8446 /* The whole case range for Q is greater than
8447 the case range for P. */
8448 e = q;
8449 q = q->right;
8450 qsize--;
8451 }
8452 else
8453 {
8454 /* The cases overlap, or they are the same
8455 element in the list. Either way, we must
8456 issue an error and get the next case from P. */
8457 /* FIXME: Sort P and Q by line number. */
8458 gfc_error ("CASE label at %L overlaps with CASE "
8459 "label at %L", &p->where, &q->where);
8460 overlap_seen = 1;
8461 e = p;
8462 p = p->right;
8463 psize--;
8464 }
8465 }
8466
8467 /* Add the next element to the merged list. */
8468 if (tail)
8469 tail->right = e;
8470 else
8471 list = e;
8472 e->left = tail;
8473 tail = e;
8474 }
8475
8476 /* P has now stepped INSIZE places along, and so has Q. So
8477 they're the same. */
8478 p = q;
8479 }
8480 tail->right = NULL;
8481
8482 /* If we have done only one merge or none at all, we've
8483 finished sorting the cases. */
8484 if (nmerges <= 1)
8485 {
8486 if (!overlap_seen)
8487 return list;
8488 else
8489 return NULL;
8490 }
8491
8492 /* Otherwise repeat, merging lists twice the size. */
8493 insize *= 2;
8494 }
8495 }
8496
8497
8498 /* Check to see if an expression is suitable for use in a CASE statement.
8499 Makes sure that all case expressions are scalar constants of the same
8500 type. Return false if anything is wrong. */
8501
8502 static bool
8503 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
8504 {
8505 if (e == NULL) return true;
8506
8507 if (e->ts.type != case_expr->ts.type)
8508 {
8509 gfc_error ("Expression in CASE statement at %L must be of type %s",
8510 &e->where, gfc_basic_typename (case_expr->ts.type));
8511 return false;
8512 }
8513
8514 /* C805 (R808) For a given case-construct, each case-value shall be of
8515 the same type as case-expr. For character type, length differences
8516 are allowed, but the kind type parameters shall be the same. */
8517
8518 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
8519 {
8520 gfc_error ("Expression in CASE statement at %L must be of kind %d",
8521 &e->where, case_expr->ts.kind);
8522 return false;
8523 }
8524
8525 /* Convert the case value kind to that of case expression kind,
8526 if needed */
8527
8528 if (e->ts.kind != case_expr->ts.kind)
8529 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
8530
8531 if (e->rank != 0)
8532 {
8533 gfc_error ("Expression in CASE statement at %L must be scalar",
8534 &e->where);
8535 return false;
8536 }
8537
8538 return true;
8539 }
8540
8541
8542 /* Given a completely parsed select statement, we:
8543
8544 - Validate all expressions and code within the SELECT.
8545 - Make sure that the selection expression is not of the wrong type.
8546 - Make sure that no case ranges overlap.
8547 - Eliminate unreachable cases and unreachable code resulting from
8548 removing case labels.
8549
8550 The standard does allow unreachable cases, e.g. CASE (5:3). But
8551 they are a hassle for code generation, and to prevent that, we just
8552 cut them out here. This is not necessary for overlapping cases
8553 because they are illegal and we never even try to generate code.
8554
8555 We have the additional caveat that a SELECT construct could have
8556 been a computed GOTO in the source code. Fortunately we can fairly
8557 easily work around that here: The case_expr for a "real" SELECT CASE
8558 is in code->expr1, but for a computed GOTO it is in code->expr2. All
8559 we have to do is make sure that the case_expr is a scalar integer
8560 expression. */
8561
8562 static void
8563 resolve_select (gfc_code *code, bool select_type)
8564 {
8565 gfc_code *body;
8566 gfc_expr *case_expr;
8567 gfc_case *cp, *default_case, *tail, *head;
8568 int seen_unreachable;
8569 int seen_logical;
8570 int ncases;
8571 bt type;
8572 bool t;
8573
8574 if (code->expr1 == NULL)
8575 {
8576 /* This was actually a computed GOTO statement. */
8577 case_expr = code->expr2;
8578 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
8579 gfc_error ("Selection expression in computed GOTO statement "
8580 "at %L must be a scalar integer expression",
8581 &case_expr->where);
8582
8583 /* Further checking is not necessary because this SELECT was built
8584 by the compiler, so it should always be OK. Just move the
8585 case_expr from expr2 to expr so that we can handle computed
8586 GOTOs as normal SELECTs from here on. */
8587 code->expr1 = code->expr2;
8588 code->expr2 = NULL;
8589 return;
8590 }
8591
8592 case_expr = code->expr1;
8593 type = case_expr->ts.type;
8594
8595 /* F08:C830. */
8596 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
8597 {
8598 gfc_error ("Argument of SELECT statement at %L cannot be %s",
8599 &case_expr->where, gfc_typename (case_expr));
8600
8601 /* Punt. Going on here just produce more garbage error messages. */
8602 return;
8603 }
8604
8605 /* F08:R842. */
8606 if (!select_type && case_expr->rank != 0)
8607 {
8608 gfc_error ("Argument of SELECT statement at %L must be a scalar "
8609 "expression", &case_expr->where);
8610
8611 /* Punt. */
8612 return;
8613 }
8614
8615 /* Raise a warning if an INTEGER case value exceeds the range of
8616 the case-expr. Later, all expressions will be promoted to the
8617 largest kind of all case-labels. */
8618
8619 if (type == BT_INTEGER)
8620 for (body = code->block; body; body = body->block)
8621 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8622 {
8623 if (cp->low
8624 && gfc_check_integer_range (cp->low->value.integer,
8625 case_expr->ts.kind) != ARITH_OK)
8626 gfc_warning (0, "Expression in CASE statement at %L is "
8627 "not in the range of %s", &cp->low->where,
8628 gfc_typename (case_expr));
8629
8630 if (cp->high
8631 && cp->low != cp->high
8632 && gfc_check_integer_range (cp->high->value.integer,
8633 case_expr->ts.kind) != ARITH_OK)
8634 gfc_warning (0, "Expression in CASE statement at %L is "
8635 "not in the range of %s", &cp->high->where,
8636 gfc_typename (case_expr));
8637 }
8638
8639 /* PR 19168 has a long discussion concerning a mismatch of the kinds
8640 of the SELECT CASE expression and its CASE values. Walk the lists
8641 of case values, and if we find a mismatch, promote case_expr to
8642 the appropriate kind. */
8643
8644 if (type == BT_LOGICAL || type == BT_INTEGER)
8645 {
8646 for (body = code->block; body; body = body->block)
8647 {
8648 /* Walk the case label list. */
8649 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8650 {
8651 /* Intercept the DEFAULT case. It does not have a kind. */
8652 if (cp->low == NULL && cp->high == NULL)
8653 continue;
8654
8655 /* Unreachable case ranges are discarded, so ignore. */
8656 if (cp->low != NULL && cp->high != NULL
8657 && cp->low != cp->high
8658 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8659 continue;
8660
8661 if (cp->low != NULL
8662 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
8663 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
8664
8665 if (cp->high != NULL
8666 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
8667 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
8668 }
8669 }
8670 }
8671
8672 /* Assume there is no DEFAULT case. */
8673 default_case = NULL;
8674 head = tail = NULL;
8675 ncases = 0;
8676 seen_logical = 0;
8677
8678 for (body = code->block; body; body = body->block)
8679 {
8680 /* Assume the CASE list is OK, and all CASE labels can be matched. */
8681 t = true;
8682 seen_unreachable = 0;
8683
8684 /* Walk the case label list, making sure that all case labels
8685 are legal. */
8686 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8687 {
8688 /* Count the number of cases in the whole construct. */
8689 ncases++;
8690
8691 /* Intercept the DEFAULT case. */
8692 if (cp->low == NULL && cp->high == NULL)
8693 {
8694 if (default_case != NULL)
8695 {
8696 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8697 "by a second DEFAULT CASE at %L",
8698 &default_case->where, &cp->where);
8699 t = false;
8700 break;
8701 }
8702 else
8703 {
8704 default_case = cp;
8705 continue;
8706 }
8707 }
8708
8709 /* Deal with single value cases and case ranges. Errors are
8710 issued from the validation function. */
8711 if (!validate_case_label_expr (cp->low, case_expr)
8712 || !validate_case_label_expr (cp->high, case_expr))
8713 {
8714 t = false;
8715 break;
8716 }
8717
8718 if (type == BT_LOGICAL
8719 && ((cp->low == NULL || cp->high == NULL)
8720 || cp->low != cp->high))
8721 {
8722 gfc_error ("Logical range in CASE statement at %L is not "
8723 "allowed", &cp->low->where);
8724 t = false;
8725 break;
8726 }
8727
8728 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
8729 {
8730 int value;
8731 value = cp->low->value.logical == 0 ? 2 : 1;
8732 if (value & seen_logical)
8733 {
8734 gfc_error ("Constant logical value in CASE statement "
8735 "is repeated at %L",
8736 &cp->low->where);
8737 t = false;
8738 break;
8739 }
8740 seen_logical |= value;
8741 }
8742
8743 if (cp->low != NULL && cp->high != NULL
8744 && cp->low != cp->high
8745 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8746 {
8747 if (warn_surprising)
8748 gfc_warning (OPT_Wsurprising,
8749 "Range specification at %L can never be matched",
8750 &cp->where);
8751
8752 cp->unreachable = 1;
8753 seen_unreachable = 1;
8754 }
8755 else
8756 {
8757 /* If the case range can be matched, it can also overlap with
8758 other cases. To make sure it does not, we put it in a
8759 double linked list here. We sort that with a merge sort
8760 later on to detect any overlapping cases. */
8761 if (!head)
8762 {
8763 head = tail = cp;
8764 head->right = head->left = NULL;
8765 }
8766 else
8767 {
8768 tail->right = cp;
8769 tail->right->left = tail;
8770 tail = tail->right;
8771 tail->right = NULL;
8772 }
8773 }
8774 }
8775
8776 /* It there was a failure in the previous case label, give up
8777 for this case label list. Continue with the next block. */
8778 if (!t)
8779 continue;
8780
8781 /* See if any case labels that are unreachable have been seen.
8782 If so, we eliminate them. This is a bit of a kludge because
8783 the case lists for a single case statement (label) is a
8784 single forward linked lists. */
8785 if (seen_unreachable)
8786 {
8787 /* Advance until the first case in the list is reachable. */
8788 while (body->ext.block.case_list != NULL
8789 && body->ext.block.case_list->unreachable)
8790 {
8791 gfc_case *n = body->ext.block.case_list;
8792 body->ext.block.case_list = body->ext.block.case_list->next;
8793 n->next = NULL;
8794 gfc_free_case_list (n);
8795 }
8796
8797 /* Strip all other unreachable cases. */
8798 if (body->ext.block.case_list)
8799 {
8800 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
8801 {
8802 if (cp->next->unreachable)
8803 {
8804 gfc_case *n = cp->next;
8805 cp->next = cp->next->next;
8806 n->next = NULL;
8807 gfc_free_case_list (n);
8808 }
8809 }
8810 }
8811 }
8812 }
8813
8814 /* See if there were overlapping cases. If the check returns NULL,
8815 there was overlap. In that case we don't do anything. If head
8816 is non-NULL, we prepend the DEFAULT case. The sorted list can
8817 then used during code generation for SELECT CASE constructs with
8818 a case expression of a CHARACTER type. */
8819 if (head)
8820 {
8821 head = check_case_overlap (head);
8822
8823 /* Prepend the default_case if it is there. */
8824 if (head != NULL && default_case)
8825 {
8826 default_case->left = NULL;
8827 default_case->right = head;
8828 head->left = default_case;
8829 }
8830 }
8831
8832 /* Eliminate dead blocks that may be the result if we've seen
8833 unreachable case labels for a block. */
8834 for (body = code; body && body->block; body = body->block)
8835 {
8836 if (body->block->ext.block.case_list == NULL)
8837 {
8838 /* Cut the unreachable block from the code chain. */
8839 gfc_code *c = body->block;
8840 body->block = c->block;
8841
8842 /* Kill the dead block, but not the blocks below it. */
8843 c->block = NULL;
8844 gfc_free_statements (c);
8845 }
8846 }
8847
8848 /* More than two cases is legal but insane for logical selects.
8849 Issue a warning for it. */
8850 if (warn_surprising && type == BT_LOGICAL && ncases > 2)
8851 gfc_warning (OPT_Wsurprising,
8852 "Logical SELECT CASE block at %L has more that two cases",
8853 &code->loc);
8854 }
8855
8856
8857 /* Check if a derived type is extensible. */
8858
8859 bool
8860 gfc_type_is_extensible (gfc_symbol *sym)
8861 {
8862 return !(sym->attr.is_bind_c || sym->attr.sequence
8863 || (sym->attr.is_class
8864 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
8865 }
8866
8867
8868 static void
8869 resolve_types (gfc_namespace *ns);
8870
8871 /* Resolve an associate-name: Resolve target and ensure the type-spec is
8872 correct as well as possibly the array-spec. */
8873
8874 static void
8875 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
8876 {
8877 gfc_expr* target;
8878
8879 gcc_assert (sym->assoc);
8880 gcc_assert (sym->attr.flavor == FL_VARIABLE);
8881
8882 /* If this is for SELECT TYPE, the target may not yet be set. In that
8883 case, return. Resolution will be called later manually again when
8884 this is done. */
8885 target = sym->assoc->target;
8886 if (!target)
8887 return;
8888 gcc_assert (!sym->assoc->dangling);
8889
8890 if (resolve_target && !gfc_resolve_expr (target))
8891 return;
8892
8893 /* For variable targets, we get some attributes from the target. */
8894 if (target->expr_type == EXPR_VARIABLE)
8895 {
8896 gfc_symbol *tsym, *dsym;
8897
8898 gcc_assert (target->symtree);
8899 tsym = target->symtree->n.sym;
8900
8901 if (gfc_expr_attr (target).proc_pointer)
8902 {
8903 gfc_error ("Associating entity %qs at %L is a procedure pointer",
8904 tsym->name, &target->where);
8905 return;
8906 }
8907
8908 if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
8909 && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
8910 && dsym->attr.flavor == FL_DERIVED)
8911 {
8912 gfc_error ("Derived type %qs cannot be used as a variable at %L",
8913 tsym->name, &target->where);
8914 return;
8915 }
8916
8917 if (tsym->attr.flavor == FL_PROCEDURE)
8918 {
8919 bool is_error = true;
8920 if (tsym->attr.function && tsym->result == tsym)
8921 for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
8922 if (tsym == ns->proc_name)
8923 {
8924 is_error = false;
8925 break;
8926 }
8927 if (is_error)
8928 {
8929 gfc_error ("Associating entity %qs at %L is a procedure name",
8930 tsym->name, &target->where);
8931 return;
8932 }
8933 }
8934
8935 sym->attr.asynchronous = tsym->attr.asynchronous;
8936 sym->attr.volatile_ = tsym->attr.volatile_;
8937
8938 sym->attr.target = tsym->attr.target
8939 || gfc_expr_attr (target).pointer;
8940 if (is_subref_array (target))
8941 sym->attr.subref_array_pointer = 1;
8942 }
8943 else if (target->ts.type == BT_PROCEDURE)
8944 {
8945 gfc_error ("Associating selector-expression at %L yields a procedure",
8946 &target->where);
8947 return;
8948 }
8949
8950 if (target->expr_type == EXPR_NULL)
8951 {
8952 gfc_error ("Selector at %L cannot be NULL()", &target->where);
8953 return;
8954 }
8955 else if (target->ts.type == BT_UNKNOWN)
8956 {
8957 gfc_error ("Selector at %L has no type", &target->where);
8958 return;
8959 }
8960
8961 /* Get type if this was not already set. Note that it can be
8962 some other type than the target in case this is a SELECT TYPE
8963 selector! So we must not update when the type is already there. */
8964 if (sym->ts.type == BT_UNKNOWN)
8965 sym->ts = target->ts;
8966
8967 gcc_assert (sym->ts.type != BT_UNKNOWN);
8968
8969 /* See if this is a valid association-to-variable. */
8970 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
8971 && !gfc_has_vector_subscript (target));
8972
8973 /* Finally resolve if this is an array or not. */
8974 if (sym->attr.dimension && target->rank == 0)
8975 {
8976 /* primary.c makes the assumption that a reference to an associate
8977 name followed by a left parenthesis is an array reference. */
8978 if (sym->ts.type != BT_CHARACTER)
8979 gfc_error ("Associate-name %qs at %L is used as array",
8980 sym->name, &sym->declared_at);
8981 sym->attr.dimension = 0;
8982 return;
8983 }
8984
8985
8986 /* We cannot deal with class selectors that need temporaries. */
8987 if (target->ts.type == BT_CLASS
8988 && gfc_ref_needs_temporary_p (target->ref))
8989 {
8990 gfc_error ("CLASS selector at %L needs a temporary which is not "
8991 "yet implemented", &target->where);
8992 return;
8993 }
8994
8995 if (target->ts.type == BT_CLASS)
8996 gfc_fix_class_refs (target);
8997
8998 if (target->rank != 0 && !sym->attr.select_rank_temporary)
8999 {
9000 gfc_array_spec *as;
9001 /* The rank may be incorrectly guessed at parsing, therefore make sure
9002 it is corrected now. */
9003 if (sym->ts.type != BT_CLASS && (!sym->as || sym->assoc->rankguessed))
9004 {
9005 if (!sym->as)
9006 sym->as = gfc_get_array_spec ();
9007 as = sym->as;
9008 as->rank = target->rank;
9009 as->type = AS_DEFERRED;
9010 as->corank = gfc_get_corank (target);
9011 sym->attr.dimension = 1;
9012 if (as->corank != 0)
9013 sym->attr.codimension = 1;
9014 }
9015 else if (sym->ts.type == BT_CLASS && (!CLASS_DATA (sym)->as || sym->assoc->rankguessed))
9016 {
9017 if (!CLASS_DATA (sym)->as)
9018 CLASS_DATA (sym)->as = gfc_get_array_spec ();
9019 as = CLASS_DATA (sym)->as;
9020 as->rank = target->rank;
9021 as->type = AS_DEFERRED;
9022 as->corank = gfc_get_corank (target);
9023 CLASS_DATA (sym)->attr.dimension = 1;
9024 if (as->corank != 0)
9025 CLASS_DATA (sym)->attr.codimension = 1;
9026 }
9027 }
9028 else if (!sym->attr.select_rank_temporary)
9029 {
9030 /* target's rank is 0, but the type of the sym is still array valued,
9031 which has to be corrected. */
9032 if (sym->ts.type == BT_CLASS
9033 && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
9034 {
9035 gfc_array_spec *as;
9036 symbol_attribute attr;
9037 /* The associated variable's type is still the array type
9038 correct this now. */
9039 gfc_typespec *ts = &target->ts;
9040 gfc_ref *ref;
9041 gfc_component *c;
9042 for (ref = target->ref; ref != NULL; ref = ref->next)
9043 {
9044 switch (ref->type)
9045 {
9046 case REF_COMPONENT:
9047 ts = &ref->u.c.component->ts;
9048 break;
9049 case REF_ARRAY:
9050 if (ts->type == BT_CLASS)
9051 ts = &ts->u.derived->components->ts;
9052 break;
9053 default:
9054 break;
9055 }
9056 }
9057 /* Create a scalar instance of the current class type. Because the
9058 rank of a class array goes into its name, the type has to be
9059 rebuild. The alternative of (re-)setting just the attributes
9060 and as in the current type, destroys the type also in other
9061 places. */
9062 as = NULL;
9063 sym->ts = *ts;
9064 sym->ts.type = BT_CLASS;
9065 attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
9066 attr.class_ok = 0;
9067 attr.associate_var = 1;
9068 attr.dimension = attr.codimension = 0;
9069 attr.class_pointer = 1;
9070 if (!gfc_build_class_symbol (&sym->ts, &attr, &as))
9071 gcc_unreachable ();
9072 /* Make sure the _vptr is set. */
9073 c = gfc_find_component (sym->ts.u.derived, "_vptr", true, true, NULL);
9074 if (c->ts.u.derived == NULL)
9075 c->ts.u.derived = gfc_find_derived_vtab (sym->ts.u.derived);
9076 CLASS_DATA (sym)->attr.pointer = 1;
9077 CLASS_DATA (sym)->attr.class_pointer = 1;
9078 gfc_set_sym_referenced (sym->ts.u.derived);
9079 gfc_commit_symbol (sym->ts.u.derived);
9080 /* _vptr now has the _vtab in it, change it to the _vtype. */
9081 if (c->ts.u.derived->attr.vtab)
9082 c->ts.u.derived = c->ts.u.derived->ts.u.derived;
9083 c->ts.u.derived->ns->types_resolved = 0;
9084 resolve_types (c->ts.u.derived->ns);
9085 }
9086 }
9087
9088 /* Mark this as an associate variable. */
9089 sym->attr.associate_var = 1;
9090
9091 /* Fix up the type-spec for CHARACTER types. */
9092 if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
9093 {
9094 if (!sym->ts.u.cl)
9095 sym->ts.u.cl = target->ts.u.cl;
9096
9097 if (sym->ts.deferred && target->expr_type == EXPR_VARIABLE
9098 && target->symtree->n.sym->attr.dummy
9099 && sym->ts.u.cl == target->ts.u.cl)
9100 {
9101 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
9102 sym->ts.deferred = 1;
9103 }
9104
9105 if (!sym->ts.u.cl->length
9106 && !sym->ts.deferred
9107 && target->expr_type == EXPR_CONSTANT)
9108 {
9109 sym->ts.u.cl->length =
9110 gfc_get_int_expr (gfc_charlen_int_kind, NULL,
9111 target->value.character.length);
9112 }
9113 else if ((!sym->ts.u.cl->length
9114 || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
9115 && target->expr_type != EXPR_VARIABLE)
9116 {
9117 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
9118 sym->ts.deferred = 1;
9119
9120 /* This is reset in trans-stmt.c after the assignment
9121 of the target expression to the associate name. */
9122 sym->attr.allocatable = 1;
9123 }
9124 }
9125
9126 /* If the target is a good class object, so is the associate variable. */
9127 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
9128 sym->attr.class_ok = 1;
9129 }
9130
9131
9132 /* Ensure that SELECT TYPE expressions have the correct rank and a full
9133 array reference, where necessary. The symbols are artificial and so
9134 the dimension attribute and arrayspec can also be set. In addition,
9135 sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
9136 This is corrected here as well.*/
9137
9138 static void
9139 fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2,
9140 int rank, gfc_ref *ref)
9141 {
9142 gfc_ref *nref = (*expr1)->ref;
9143 gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
9144 gfc_symbol *sym2 = expr2 ? expr2->symtree->n.sym : NULL;
9145 (*expr1)->rank = rank;
9146 if (sym1->ts.type == BT_CLASS)
9147 {
9148 if ((*expr1)->ts.type != BT_CLASS)
9149 (*expr1)->ts = sym1->ts;
9150
9151 CLASS_DATA (sym1)->attr.dimension = 1;
9152 if (CLASS_DATA (sym1)->as == NULL && sym2)
9153 CLASS_DATA (sym1)->as
9154 = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
9155 }
9156 else
9157 {
9158 sym1->attr.dimension = 1;
9159 if (sym1->as == NULL && sym2)
9160 sym1->as = gfc_copy_array_spec (sym2->as);
9161 }
9162
9163 for (; nref; nref = nref->next)
9164 if (nref->next == NULL)
9165 break;
9166
9167 if (ref && nref && nref->type != REF_ARRAY)
9168 nref->next = gfc_copy_ref (ref);
9169 else if (ref && !nref)
9170 (*expr1)->ref = gfc_copy_ref (ref);
9171 }
9172
9173
9174 static gfc_expr *
9175 build_loc_call (gfc_expr *sym_expr)
9176 {
9177 gfc_expr *loc_call;
9178 loc_call = gfc_get_expr ();
9179 loc_call->expr_type = EXPR_FUNCTION;
9180 gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
9181 loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
9182 loc_call->symtree->n.sym->attr.intrinsic = 1;
9183 loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
9184 gfc_commit_symbol (loc_call->symtree->n.sym);
9185 loc_call->ts.type = BT_INTEGER;
9186 loc_call->ts.kind = gfc_index_integer_kind;
9187 loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
9188 loc_call->value.function.actual = gfc_get_actual_arglist ();
9189 loc_call->value.function.actual->expr = sym_expr;
9190 loc_call->where = sym_expr->where;
9191 return loc_call;
9192 }
9193
9194 /* Resolve a SELECT TYPE statement. */
9195
9196 static void
9197 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
9198 {
9199 gfc_symbol *selector_type;
9200 gfc_code *body, *new_st, *if_st, *tail;
9201 gfc_code *class_is = NULL, *default_case = NULL;
9202 gfc_case *c;
9203 gfc_symtree *st;
9204 char name[GFC_MAX_SYMBOL_LEN];
9205 gfc_namespace *ns;
9206 int error = 0;
9207 int rank = 0;
9208 gfc_ref* ref = NULL;
9209 gfc_expr *selector_expr = NULL;
9210
9211 ns = code->ext.block.ns;
9212 gfc_resolve (ns);
9213
9214 /* Check for F03:C813. */
9215 if (code->expr1->ts.type != BT_CLASS
9216 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
9217 {
9218 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
9219 "at %L", &code->loc);
9220 return;
9221 }
9222
9223 if (!code->expr1->symtree->n.sym->attr.class_ok)
9224 return;
9225
9226 if (code->expr2)
9227 {
9228 gfc_ref *ref2 = NULL;
9229 for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
9230 if (ref->type == REF_COMPONENT
9231 && ref->u.c.component->ts.type == BT_CLASS)
9232 ref2 = ref;
9233
9234 if (ref2)
9235 {
9236 if (code->expr1->symtree->n.sym->attr.untyped)
9237 code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
9238 selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
9239 }
9240 else
9241 {
9242 if (code->expr1->symtree->n.sym->attr.untyped)
9243 code->expr1->symtree->n.sym->ts = code->expr2->ts;
9244 selector_type = CLASS_DATA (code->expr2)
9245 ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
9246 }
9247
9248 if (code->expr2->rank && CLASS_DATA (code->expr1)->as)
9249 CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
9250
9251 /* F2008: C803 The selector expression must not be coindexed. */
9252 if (gfc_is_coindexed (code->expr2))
9253 {
9254 gfc_error ("Selector at %L must not be coindexed",
9255 &code->expr2->where);
9256 return;
9257 }
9258
9259 }
9260 else
9261 {
9262 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
9263
9264 if (gfc_is_coindexed (code->expr1))
9265 {
9266 gfc_error ("Selector at %L must not be coindexed",
9267 &code->expr1->where);
9268 return;
9269 }
9270 }
9271
9272 /* Loop over TYPE IS / CLASS IS cases. */
9273 for (body = code->block; body; body = body->block)
9274 {
9275 c = body->ext.block.case_list;
9276
9277 if (!error)
9278 {
9279 /* Check for repeated cases. */
9280 for (tail = code->block; tail; tail = tail->block)
9281 {
9282 gfc_case *d = tail->ext.block.case_list;
9283 if (tail == body)
9284 break;
9285
9286 if (c->ts.type == d->ts.type
9287 && ((c->ts.type == BT_DERIVED
9288 && c->ts.u.derived && d->ts.u.derived
9289 && !strcmp (c->ts.u.derived->name,
9290 d->ts.u.derived->name))
9291 || c->ts.type == BT_UNKNOWN
9292 || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9293 && c->ts.kind == d->ts.kind)))
9294 {
9295 gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
9296 &c->where, &d->where);
9297 return;
9298 }
9299 }
9300 }
9301
9302 /* Check F03:C815. */
9303 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9304 && !selector_type->attr.unlimited_polymorphic
9305 && !gfc_type_is_extensible (c->ts.u.derived))
9306 {
9307 gfc_error ("Derived type %qs at %L must be extensible",
9308 c->ts.u.derived->name, &c->where);
9309 error++;
9310 continue;
9311 }
9312
9313 /* Check F03:C816. */
9314 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
9315 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
9316 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
9317 {
9318 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9319 gfc_error ("Derived type %qs at %L must be an extension of %qs",
9320 c->ts.u.derived->name, &c->where, selector_type->name);
9321 else
9322 gfc_error ("Unexpected intrinsic type %qs at %L",
9323 gfc_basic_typename (c->ts.type), &c->where);
9324 error++;
9325 continue;
9326 }
9327
9328 /* Check F03:C814. */
9329 if (c->ts.type == BT_CHARACTER
9330 && (c->ts.u.cl->length != NULL || c->ts.deferred))
9331 {
9332 gfc_error ("The type-spec at %L shall specify that each length "
9333 "type parameter is assumed", &c->where);
9334 error++;
9335 continue;
9336 }
9337
9338 /* Intercept the DEFAULT case. */
9339 if (c->ts.type == BT_UNKNOWN)
9340 {
9341 /* Check F03:C818. */
9342 if (default_case)
9343 {
9344 gfc_error ("The DEFAULT CASE at %L cannot be followed "
9345 "by a second DEFAULT CASE at %L",
9346 &default_case->ext.block.case_list->where, &c->where);
9347 error++;
9348 continue;
9349 }
9350
9351 default_case = body;
9352 }
9353 }
9354
9355 if (error > 0)
9356 return;
9357
9358 /* Transform SELECT TYPE statement to BLOCK and associate selector to
9359 target if present. If there are any EXIT statements referring to the
9360 SELECT TYPE construct, this is no problem because the gfc_code
9361 reference stays the same and EXIT is equally possible from the BLOCK
9362 it is changed to. */
9363 code->op = EXEC_BLOCK;
9364 if (code->expr2)
9365 {
9366 gfc_association_list* assoc;
9367
9368 assoc = gfc_get_association_list ();
9369 assoc->st = code->expr1->symtree;
9370 assoc->target = gfc_copy_expr (code->expr2);
9371 assoc->target->where = code->expr2->where;
9372 /* assoc->variable will be set by resolve_assoc_var. */
9373
9374 code->ext.block.assoc = assoc;
9375 code->expr1->symtree->n.sym->assoc = assoc;
9376
9377 resolve_assoc_var (code->expr1->symtree->n.sym, false);
9378 }
9379 else
9380 code->ext.block.assoc = NULL;
9381
9382 /* Ensure that the selector rank and arrayspec are available to
9383 correct expressions in which they might be missing. */
9384 if (code->expr2 && code->expr2->rank)
9385 {
9386 rank = code->expr2->rank;
9387 for (ref = code->expr2->ref; ref; ref = ref->next)
9388 if (ref->next == NULL)
9389 break;
9390 if (ref && ref->type == REF_ARRAY)
9391 ref = gfc_copy_ref (ref);
9392
9393 /* Fixup expr1 if necessary. */
9394 if (rank)
9395 fixup_array_ref (&code->expr1, code->expr2, rank, ref);
9396 }
9397 else if (code->expr1->rank)
9398 {
9399 rank = code->expr1->rank;
9400 for (ref = code->expr1->ref; ref; ref = ref->next)
9401 if (ref->next == NULL)
9402 break;
9403 if (ref && ref->type == REF_ARRAY)
9404 ref = gfc_copy_ref (ref);
9405 }
9406
9407 /* Add EXEC_SELECT to switch on type. */
9408 new_st = gfc_get_code (code->op);
9409 new_st->expr1 = code->expr1;
9410 new_st->expr2 = code->expr2;
9411 new_st->block = code->block;
9412 code->expr1 = code->expr2 = NULL;
9413 code->block = NULL;
9414 if (!ns->code)
9415 ns->code = new_st;
9416 else
9417 ns->code->next = new_st;
9418 code = new_st;
9419 code->op = EXEC_SELECT_TYPE;
9420
9421 /* Use the intrinsic LOC function to generate an integer expression
9422 for the vtable of the selector. Note that the rank of the selector
9423 expression has to be set to zero. */
9424 gfc_add_vptr_component (code->expr1);
9425 code->expr1->rank = 0;
9426 code->expr1 = build_loc_call (code->expr1);
9427 selector_expr = code->expr1->value.function.actual->expr;
9428
9429 /* Loop over TYPE IS / CLASS IS cases. */
9430 for (body = code->block; body; body = body->block)
9431 {
9432 gfc_symbol *vtab;
9433 gfc_expr *e;
9434 c = body->ext.block.case_list;
9435
9436 /* Generate an index integer expression for address of the
9437 TYPE/CLASS vtable and store it in c->low. The hash expression
9438 is stored in c->high and is used to resolve intrinsic cases. */
9439 if (c->ts.type != BT_UNKNOWN)
9440 {
9441 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9442 {
9443 vtab = gfc_find_derived_vtab (c->ts.u.derived);
9444 gcc_assert (vtab);
9445 c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
9446 c->ts.u.derived->hash_value);
9447 }
9448 else
9449 {
9450 vtab = gfc_find_vtab (&c->ts);
9451 gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
9452 e = CLASS_DATA (vtab)->initializer;
9453 c->high = gfc_copy_expr (e);
9454 if (c->high->ts.kind != gfc_integer_4_kind)
9455 {
9456 gfc_typespec ts;
9457 ts.kind = gfc_integer_4_kind;
9458 ts.type = BT_INTEGER;
9459 gfc_convert_type_warn (c->high, &ts, 2, 0);
9460 }
9461 }
9462
9463 e = gfc_lval_expr_from_sym (vtab);
9464 c->low = build_loc_call (e);
9465 }
9466 else
9467 continue;
9468
9469 /* Associate temporary to selector. This should only be done
9470 when this case is actually true, so build a new ASSOCIATE
9471 that does precisely this here (instead of using the
9472 'global' one). */
9473
9474 if (c->ts.type == BT_CLASS)
9475 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
9476 else if (c->ts.type == BT_DERIVED)
9477 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
9478 else if (c->ts.type == BT_CHARACTER)
9479 {
9480 HOST_WIDE_INT charlen = 0;
9481 if (c->ts.u.cl && c->ts.u.cl->length
9482 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9483 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
9484 snprintf (name, sizeof (name),
9485 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
9486 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
9487 }
9488 else
9489 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
9490 c->ts.kind);
9491
9492 st = gfc_find_symtree (ns->sym_root, name);
9493 gcc_assert (st->n.sym->assoc);
9494 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
9495 st->n.sym->assoc->target->where = selector_expr->where;
9496 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
9497 {
9498 gfc_add_data_component (st->n.sym->assoc->target);
9499 /* Fixup the target expression if necessary. */
9500 if (rank)
9501 fixup_array_ref (&st->n.sym->assoc->target, NULL, rank, ref);
9502 }
9503
9504 new_st = gfc_get_code (EXEC_BLOCK);
9505 new_st->ext.block.ns = gfc_build_block_ns (ns);
9506 new_st->ext.block.ns->code = body->next;
9507 body->next = new_st;
9508
9509 /* Chain in the new list only if it is marked as dangling. Otherwise
9510 there is a CASE label overlap and this is already used. Just ignore,
9511 the error is diagnosed elsewhere. */
9512 if (st->n.sym->assoc->dangling)
9513 {
9514 new_st->ext.block.assoc = st->n.sym->assoc;
9515 st->n.sym->assoc->dangling = 0;
9516 }
9517
9518 resolve_assoc_var (st->n.sym, false);
9519 }
9520
9521 /* Take out CLASS IS cases for separate treatment. */
9522 body = code;
9523 while (body && body->block)
9524 {
9525 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
9526 {
9527 /* Add to class_is list. */
9528 if (class_is == NULL)
9529 {
9530 class_is = body->block;
9531 tail = class_is;
9532 }
9533 else
9534 {
9535 for (tail = class_is; tail->block; tail = tail->block) ;
9536 tail->block = body->block;
9537 tail = tail->block;
9538 }
9539 /* Remove from EXEC_SELECT list. */
9540 body->block = body->block->block;
9541 tail->block = NULL;
9542 }
9543 else
9544 body = body->block;
9545 }
9546
9547 if (class_is)
9548 {
9549 gfc_symbol *vtab;
9550
9551 if (!default_case)
9552 {
9553 /* Add a default case to hold the CLASS IS cases. */
9554 for (tail = code; tail->block; tail = tail->block) ;
9555 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
9556 tail = tail->block;
9557 tail->ext.block.case_list = gfc_get_case ();
9558 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
9559 tail->next = NULL;
9560 default_case = tail;
9561 }
9562
9563 /* More than one CLASS IS block? */
9564 if (class_is->block)
9565 {
9566 gfc_code **c1,*c2;
9567 bool swapped;
9568 /* Sort CLASS IS blocks by extension level. */
9569 do
9570 {
9571 swapped = false;
9572 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
9573 {
9574 c2 = (*c1)->block;
9575 /* F03:C817 (check for doubles). */
9576 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
9577 == c2->ext.block.case_list->ts.u.derived->hash_value)
9578 {
9579 gfc_error ("Double CLASS IS block in SELECT TYPE "
9580 "statement at %L",
9581 &c2->ext.block.case_list->where);
9582 return;
9583 }
9584 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
9585 < c2->ext.block.case_list->ts.u.derived->attr.extension)
9586 {
9587 /* Swap. */
9588 (*c1)->block = c2->block;
9589 c2->block = *c1;
9590 *c1 = c2;
9591 swapped = true;
9592 }
9593 }
9594 }
9595 while (swapped);
9596 }
9597
9598 /* Generate IF chain. */
9599 if_st = gfc_get_code (EXEC_IF);
9600 new_st = if_st;
9601 for (body = class_is; body; body = body->block)
9602 {
9603 new_st->block = gfc_get_code (EXEC_IF);
9604 new_st = new_st->block;
9605 /* Set up IF condition: Call _gfortran_is_extension_of. */
9606 new_st->expr1 = gfc_get_expr ();
9607 new_st->expr1->expr_type = EXPR_FUNCTION;
9608 new_st->expr1->ts.type = BT_LOGICAL;
9609 new_st->expr1->ts.kind = 4;
9610 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
9611 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
9612 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
9613 /* Set up arguments. */
9614 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
9615 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
9616 new_st->expr1->value.function.actual->expr->where = code->loc;
9617 new_st->expr1->where = code->loc;
9618 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
9619 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
9620 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
9621 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
9622 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
9623 new_st->expr1->value.function.actual->next->expr->where = code->loc;
9624 new_st->next = body->next;
9625 }
9626 if (default_case->next)
9627 {
9628 new_st->block = gfc_get_code (EXEC_IF);
9629 new_st = new_st->block;
9630 new_st->next = default_case->next;
9631 }
9632
9633 /* Replace CLASS DEFAULT code by the IF chain. */
9634 default_case->next = if_st;
9635 }
9636
9637 /* Resolve the internal code. This cannot be done earlier because
9638 it requires that the sym->assoc of selectors is set already. */
9639 gfc_current_ns = ns;
9640 gfc_resolve_blocks (code->block, gfc_current_ns);
9641 gfc_current_ns = old_ns;
9642
9643 if (ref)
9644 free (ref);
9645 }
9646
9647
9648 /* Resolve a SELECT RANK statement. */
9649
9650 static void
9651 resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
9652 {
9653 gfc_namespace *ns;
9654 gfc_code *body, *new_st, *tail;
9655 gfc_case *c;
9656 char tname[GFC_MAX_SYMBOL_LEN + 7];
9657 char name[2 * GFC_MAX_SYMBOL_LEN];
9658 gfc_symtree *st;
9659 gfc_expr *selector_expr = NULL;
9660 int case_value;
9661 HOST_WIDE_INT charlen = 0;
9662
9663 ns = code->ext.block.ns;
9664 gfc_resolve (ns);
9665
9666 code->op = EXEC_BLOCK;
9667 if (code->expr2)
9668 {
9669 gfc_association_list* assoc;
9670
9671 assoc = gfc_get_association_list ();
9672 assoc->st = code->expr1->symtree;
9673 assoc->target = gfc_copy_expr (code->expr2);
9674 assoc->target->where = code->expr2->where;
9675 /* assoc->variable will be set by resolve_assoc_var. */
9676
9677 code->ext.block.assoc = assoc;
9678 code->expr1->symtree->n.sym->assoc = assoc;
9679
9680 resolve_assoc_var (code->expr1->symtree->n.sym, false);
9681 }
9682 else
9683 code->ext.block.assoc = NULL;
9684
9685 /* Loop over RANK cases. Note that returning on the errors causes a
9686 cascade of further errors because the case blocks do not compile
9687 correctly. */
9688 for (body = code->block; body; body = body->block)
9689 {
9690 c = body->ext.block.case_list;
9691 if (c->low)
9692 case_value = (int) mpz_get_si (c->low->value.integer);
9693 else
9694 case_value = -2;
9695
9696 /* Check for repeated cases. */
9697 for (tail = code->block; tail; tail = tail->block)
9698 {
9699 gfc_case *d = tail->ext.block.case_list;
9700 int case_value2;
9701
9702 if (tail == body)
9703 break;
9704
9705 /* Check F2018: C1153. */
9706 if (!c->low && !d->low)
9707 gfc_error ("RANK DEFAULT at %L is repeated at %L",
9708 &c->where, &d->where);
9709
9710 if (!c->low || !d->low)
9711 continue;
9712
9713 /* Check F2018: C1153. */
9714 case_value2 = (int) mpz_get_si (d->low->value.integer);
9715 if ((case_value == case_value2) && case_value == -1)
9716 gfc_error ("RANK (*) at %L is repeated at %L",
9717 &c->where, &d->where);
9718 else if (case_value == case_value2)
9719 gfc_error ("RANK (%i) at %L is repeated at %L",
9720 case_value, &c->where, &d->where);
9721 }
9722
9723 if (!c->low)
9724 continue;
9725
9726 /* Check F2018: C1155. */
9727 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
9728 || gfc_expr_attr (code->expr1).pointer))
9729 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9730 "allocatable selector at %L", &c->where, &code->expr1->where);
9731
9732 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
9733 || gfc_expr_attr (code->expr1).pointer))
9734 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9735 "allocatable selector at %L", &c->where, &code->expr1->where);
9736 }
9737
9738 /* Add EXEC_SELECT to switch on rank. */
9739 new_st = gfc_get_code (code->op);
9740 new_st->expr1 = code->expr1;
9741 new_st->expr2 = code->expr2;
9742 new_st->block = code->block;
9743 code->expr1 = code->expr2 = NULL;
9744 code->block = NULL;
9745 if (!ns->code)
9746 ns->code = new_st;
9747 else
9748 ns->code->next = new_st;
9749 code = new_st;
9750 code->op = EXEC_SELECT_RANK;
9751
9752 selector_expr = code->expr1;
9753
9754 /* Loop over SELECT RANK cases. */
9755 for (body = code->block; body; body = body->block)
9756 {
9757 c = body->ext.block.case_list;
9758 int case_value;
9759
9760 /* Pass on the default case. */
9761 if (c->low == NULL)
9762 continue;
9763
9764 /* Associate temporary to selector. This should only be done
9765 when this case is actually true, so build a new ASSOCIATE
9766 that does precisely this here (instead of using the
9767 'global' one). */
9768 if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
9769 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9770 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
9771
9772 if (c->ts.type == BT_CLASS)
9773 sprintf (tname, "class_%s", c->ts.u.derived->name);
9774 else if (c->ts.type == BT_DERIVED)
9775 sprintf (tname, "type_%s", c->ts.u.derived->name);
9776 else if (c->ts.type != BT_CHARACTER)
9777 sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
9778 else
9779 sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
9780 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
9781
9782 case_value = (int) mpz_get_si (c->low->value.integer);
9783 if (case_value >= 0)
9784 sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
9785 else
9786 sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
9787
9788 st = gfc_find_symtree (ns->sym_root, name);
9789 gcc_assert (st->n.sym->assoc);
9790
9791 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
9792 st->n.sym->assoc->target->where = selector_expr->where;
9793
9794 new_st = gfc_get_code (EXEC_BLOCK);
9795 new_st->ext.block.ns = gfc_build_block_ns (ns);
9796 new_st->ext.block.ns->code = body->next;
9797 body->next = new_st;
9798
9799 /* Chain in the new list only if it is marked as dangling. Otherwise
9800 there is a CASE label overlap and this is already used. Just ignore,
9801 the error is diagnosed elsewhere. */
9802 if (st->n.sym->assoc->dangling)
9803 {
9804 new_st->ext.block.assoc = st->n.sym->assoc;
9805 st->n.sym->assoc->dangling = 0;
9806 }
9807
9808 resolve_assoc_var (st->n.sym, false);
9809 }
9810
9811 gfc_current_ns = ns;
9812 gfc_resolve_blocks (code->block, gfc_current_ns);
9813 gfc_current_ns = old_ns;
9814 }
9815
9816
9817 /* Resolve a transfer statement. This is making sure that:
9818 -- a derived type being transferred has only non-pointer components
9819 -- a derived type being transferred doesn't have private components, unless
9820 it's being transferred from the module where the type was defined
9821 -- we're not trying to transfer a whole assumed size array. */
9822
9823 static void
9824 resolve_transfer (gfc_code *code)
9825 {
9826 gfc_symbol *sym, *derived;
9827 gfc_ref *ref;
9828 gfc_expr *exp;
9829 bool write = false;
9830 bool formatted = false;
9831 gfc_dt *dt = code->ext.dt;
9832 gfc_symbol *dtio_sub = NULL;
9833
9834 exp = code->expr1;
9835
9836 while (exp != NULL && exp->expr_type == EXPR_OP
9837 && exp->value.op.op == INTRINSIC_PARENTHESES)
9838 exp = exp->value.op.op1;
9839
9840 if (exp && exp->expr_type == EXPR_NULL
9841 && code->ext.dt)
9842 {
9843 gfc_error ("Invalid context for NULL () intrinsic at %L",
9844 &exp->where);
9845 return;
9846 }
9847
9848 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
9849 && exp->expr_type != EXPR_FUNCTION
9850 && exp->expr_type != EXPR_STRUCTURE))
9851 return;
9852
9853 /* If we are reading, the variable will be changed. Note that
9854 code->ext.dt may be NULL if the TRANSFER is related to
9855 an INQUIRE statement -- but in this case, we are not reading, either. */
9856 if (dt && dt->dt_io_kind->value.iokind == M_READ
9857 && !gfc_check_vardef_context (exp, false, false, false,
9858 _("item in READ")))
9859 return;
9860
9861 const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
9862 || exp->expr_type == EXPR_FUNCTION
9863 ? &exp->ts : &exp->symtree->n.sym->ts;
9864
9865 /* Go to actual component transferred. */
9866 for (ref = exp->ref; ref; ref = ref->next)
9867 if (ref->type == REF_COMPONENT)
9868 ts = &ref->u.c.component->ts;
9869
9870 if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
9871 && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
9872 {
9873 derived = ts->u.derived;
9874
9875 /* Determine when to use the formatted DTIO procedure. */
9876 if (dt && (dt->format_expr || dt->format_label))
9877 formatted = true;
9878
9879 write = dt->dt_io_kind->value.iokind == M_WRITE
9880 || dt->dt_io_kind->value.iokind == M_PRINT;
9881 dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
9882
9883 if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
9884 {
9885 dt->udtio = exp;
9886 sym = exp->symtree->n.sym->ns->proc_name;
9887 /* Check to see if this is a nested DTIO call, with the
9888 dummy as the io-list object. */
9889 if (sym && sym == dtio_sub && sym->formal
9890 && sym->formal->sym == exp->symtree->n.sym
9891 && exp->ref == NULL)
9892 {
9893 if (!sym->attr.recursive)
9894 {
9895 gfc_error ("DTIO %s procedure at %L must be recursive",
9896 sym->name, &sym->declared_at);
9897 return;
9898 }
9899 }
9900 }
9901 }
9902
9903 if (ts->type == BT_CLASS && dtio_sub == NULL)
9904 {
9905 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
9906 "it is processed by a defined input/output procedure",
9907 &code->loc);
9908 return;
9909 }
9910
9911 if (ts->type == BT_DERIVED)
9912 {
9913 /* Check that transferred derived type doesn't contain POINTER
9914 components unless it is processed by a defined input/output
9915 procedure". */
9916 if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
9917 {
9918 gfc_error ("Data transfer element at %L cannot have POINTER "
9919 "components unless it is processed by a defined "
9920 "input/output procedure", &code->loc);
9921 return;
9922 }
9923
9924 /* F08:C935. */
9925 if (ts->u.derived->attr.proc_pointer_comp)
9926 {
9927 gfc_error ("Data transfer element at %L cannot have "
9928 "procedure pointer components", &code->loc);
9929 return;
9930 }
9931
9932 if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
9933 {
9934 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
9935 "components unless it is processed by a defined "
9936 "input/output procedure", &code->loc);
9937 return;
9938 }
9939
9940 /* C_PTR and C_FUNPTR have private components which means they cannot
9941 be printed. However, if -std=gnu and not -pedantic, allow
9942 the component to be printed to help debugging. */
9943 if (ts->u.derived->ts.f90_type == BT_VOID)
9944 {
9945 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
9946 "cannot have PRIVATE components", &code->loc))
9947 return;
9948 }
9949 else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
9950 {
9951 gfc_error ("Data transfer element at %L cannot have "
9952 "PRIVATE components unless it is processed by "
9953 "a defined input/output procedure", &code->loc);
9954 return;
9955 }
9956 }
9957
9958 if (exp->expr_type == EXPR_STRUCTURE)
9959 return;
9960
9961 sym = exp->symtree->n.sym;
9962
9963 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
9964 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
9965 {
9966 gfc_error ("Data transfer element at %L cannot be a full reference to "
9967 "an assumed-size array", &code->loc);
9968 return;
9969 }
9970 }
9971
9972
9973 /*********** Toplevel code resolution subroutines ***********/
9974
9975 /* Find the set of labels that are reachable from this block. We also
9976 record the last statement in each block. */
9977
9978 static void
9979 find_reachable_labels (gfc_code *block)
9980 {
9981 gfc_code *c;
9982
9983 if (!block)
9984 return;
9985
9986 cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
9987
9988 /* Collect labels in this block. We don't keep those corresponding
9989 to END {IF|SELECT}, these are checked in resolve_branch by going
9990 up through the code_stack. */
9991 for (c = block; c; c = c->next)
9992 {
9993 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
9994 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
9995 }
9996
9997 /* Merge with labels from parent block. */
9998 if (cs_base->prev)
9999 {
10000 gcc_assert (cs_base->prev->reachable_labels);
10001 bitmap_ior_into (cs_base->reachable_labels,
10002 cs_base->prev->reachable_labels);
10003 }
10004 }
10005
10006
10007 static void
10008 resolve_lock_unlock_event (gfc_code *code)
10009 {
10010 if (code->expr1->expr_type == EXPR_FUNCTION
10011 && code->expr1->value.function.isym
10012 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
10013 remove_caf_get_intrinsic (code->expr1);
10014
10015 if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
10016 && (code->expr1->ts.type != BT_DERIVED
10017 || code->expr1->expr_type != EXPR_VARIABLE
10018 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
10019 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
10020 || code->expr1->rank != 0
10021 || (!gfc_is_coarray (code->expr1) &&
10022 !gfc_is_coindexed (code->expr1))))
10023 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
10024 &code->expr1->where);
10025 else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
10026 && (code->expr1->ts.type != BT_DERIVED
10027 || code->expr1->expr_type != EXPR_VARIABLE
10028 || code->expr1->ts.u.derived->from_intmod
10029 != INTMOD_ISO_FORTRAN_ENV
10030 || code->expr1->ts.u.derived->intmod_sym_id
10031 != ISOFORTRAN_EVENT_TYPE
10032 || code->expr1->rank != 0))
10033 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
10034 &code->expr1->where);
10035 else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
10036 && !gfc_is_coindexed (code->expr1))
10037 gfc_error ("Event variable argument at %L must be a coarray or coindexed",
10038 &code->expr1->where);
10039 else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
10040 gfc_error ("Event variable argument at %L must be a coarray but not "
10041 "coindexed", &code->expr1->where);
10042
10043 /* Check STAT. */
10044 if (code->expr2
10045 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
10046 || code->expr2->expr_type != EXPR_VARIABLE))
10047 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10048 &code->expr2->where);
10049
10050 if (code->expr2
10051 && !gfc_check_vardef_context (code->expr2, false, false, false,
10052 _("STAT variable")))
10053 return;
10054
10055 /* Check ERRMSG. */
10056 if (code->expr3
10057 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
10058 || code->expr3->expr_type != EXPR_VARIABLE))
10059 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10060 &code->expr3->where);
10061
10062 if (code->expr3
10063 && !gfc_check_vardef_context (code->expr3, false, false, false,
10064 _("ERRMSG variable")))
10065 return;
10066
10067 /* Check for LOCK the ACQUIRED_LOCK. */
10068 if (code->op != EXEC_EVENT_WAIT && code->expr4
10069 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
10070 || code->expr4->expr_type != EXPR_VARIABLE))
10071 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
10072 "variable", &code->expr4->where);
10073
10074 if (code->op != EXEC_EVENT_WAIT && code->expr4
10075 && !gfc_check_vardef_context (code->expr4, false, false, false,
10076 _("ACQUIRED_LOCK variable")))
10077 return;
10078
10079 /* Check for EVENT WAIT the UNTIL_COUNT. */
10080 if (code->op == EXEC_EVENT_WAIT && code->expr4)
10081 {
10082 if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
10083 || code->expr4->rank != 0)
10084 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
10085 "expression", &code->expr4->where);
10086 }
10087 }
10088
10089
10090 static void
10091 resolve_critical (gfc_code *code)
10092 {
10093 gfc_symtree *symtree;
10094 gfc_symbol *lock_type;
10095 char name[GFC_MAX_SYMBOL_LEN];
10096 static int serial = 0;
10097
10098 if (flag_coarray != GFC_FCOARRAY_LIB)
10099 return;
10100
10101 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
10102 GFC_PREFIX ("lock_type"));
10103 if (symtree)
10104 lock_type = symtree->n.sym;
10105 else
10106 {
10107 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
10108 false) != 0)
10109 gcc_unreachable ();
10110 lock_type = symtree->n.sym;
10111 lock_type->attr.flavor = FL_DERIVED;
10112 lock_type->attr.zero_comp = 1;
10113 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
10114 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
10115 }
10116
10117 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
10118 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
10119 gcc_unreachable ();
10120
10121 code->resolved_sym = symtree->n.sym;
10122 symtree->n.sym->attr.flavor = FL_VARIABLE;
10123 symtree->n.sym->attr.referenced = 1;
10124 symtree->n.sym->attr.artificial = 1;
10125 symtree->n.sym->attr.codimension = 1;
10126 symtree->n.sym->ts.type = BT_DERIVED;
10127 symtree->n.sym->ts.u.derived = lock_type;
10128 symtree->n.sym->as = gfc_get_array_spec ();
10129 symtree->n.sym->as->corank = 1;
10130 symtree->n.sym->as->type = AS_EXPLICIT;
10131 symtree->n.sym->as->cotype = AS_EXPLICIT;
10132 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
10133 NULL, 1);
10134 gfc_commit_symbols();
10135 }
10136
10137
10138 static void
10139 resolve_sync (gfc_code *code)
10140 {
10141 /* Check imageset. The * case matches expr1 == NULL. */
10142 if (code->expr1)
10143 {
10144 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
10145 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
10146 "INTEGER expression", &code->expr1->where);
10147 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
10148 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
10149 gfc_error ("Imageset argument at %L must between 1 and num_images()",
10150 &code->expr1->where);
10151 else if (code->expr1->expr_type == EXPR_ARRAY
10152 && gfc_simplify_expr (code->expr1, 0))
10153 {
10154 gfc_constructor *cons;
10155 cons = gfc_constructor_first (code->expr1->value.constructor);
10156 for (; cons; cons = gfc_constructor_next (cons))
10157 if (cons->expr->expr_type == EXPR_CONSTANT
10158 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
10159 gfc_error ("Imageset argument at %L must between 1 and "
10160 "num_images()", &cons->expr->where);
10161 }
10162 }
10163
10164 /* Check STAT. */
10165 gfc_resolve_expr (code->expr2);
10166 if (code->expr2
10167 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
10168 || code->expr2->expr_type != EXPR_VARIABLE))
10169 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10170 &code->expr2->where);
10171
10172 /* Check ERRMSG. */
10173 gfc_resolve_expr (code->expr3);
10174 if (code->expr3
10175 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
10176 || code->expr3->expr_type != EXPR_VARIABLE))
10177 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10178 &code->expr3->where);
10179 }
10180
10181
10182 /* Given a branch to a label, see if the branch is conforming.
10183 The code node describes where the branch is located. */
10184
10185 static void
10186 resolve_branch (gfc_st_label *label, gfc_code *code)
10187 {
10188 code_stack *stack;
10189
10190 if (label == NULL)
10191 return;
10192
10193 /* Step one: is this a valid branching target? */
10194
10195 if (label->defined == ST_LABEL_UNKNOWN)
10196 {
10197 gfc_error ("Label %d referenced at %L is never defined", label->value,
10198 &code->loc);
10199 return;
10200 }
10201
10202 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
10203 {
10204 gfc_error ("Statement at %L is not a valid branch target statement "
10205 "for the branch statement at %L", &label->where, &code->loc);
10206 return;
10207 }
10208
10209 /* Step two: make sure this branch is not a branch to itself ;-) */
10210
10211 if (code->here == label)
10212 {
10213 gfc_warning (0,
10214 "Branch at %L may result in an infinite loop", &code->loc);
10215 return;
10216 }
10217
10218 /* Step three: See if the label is in the same block as the
10219 branching statement. The hard work has been done by setting up
10220 the bitmap reachable_labels. */
10221
10222 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
10223 {
10224 /* Check now whether there is a CRITICAL construct; if so, check
10225 whether the label is still visible outside of the CRITICAL block,
10226 which is invalid. */
10227 for (stack = cs_base; stack; stack = stack->prev)
10228 {
10229 if (stack->current->op == EXEC_CRITICAL
10230 && bitmap_bit_p (stack->reachable_labels, label->value))
10231 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
10232 "label at %L", &code->loc, &label->where);
10233 else if (stack->current->op == EXEC_DO_CONCURRENT
10234 && bitmap_bit_p (stack->reachable_labels, label->value))
10235 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
10236 "for label at %L", &code->loc, &label->where);
10237 }
10238
10239 return;
10240 }
10241
10242 /* Step four: If we haven't found the label in the bitmap, it may
10243 still be the label of the END of the enclosing block, in which
10244 case we find it by going up the code_stack. */
10245
10246 for (stack = cs_base; stack; stack = stack->prev)
10247 {
10248 if (stack->current->next && stack->current->next->here == label)
10249 break;
10250 if (stack->current->op == EXEC_CRITICAL)
10251 {
10252 /* Note: A label at END CRITICAL does not leave the CRITICAL
10253 construct as END CRITICAL is still part of it. */
10254 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
10255 " at %L", &code->loc, &label->where);
10256 return;
10257 }
10258 else if (stack->current->op == EXEC_DO_CONCURRENT)
10259 {
10260 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
10261 "label at %L", &code->loc, &label->where);
10262 return;
10263 }
10264 }
10265
10266 if (stack)
10267 {
10268 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
10269 return;
10270 }
10271
10272 /* The label is not in an enclosing block, so illegal. This was
10273 allowed in Fortran 66, so we allow it as extension. No
10274 further checks are necessary in this case. */
10275 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
10276 "as the GOTO statement at %L", &label->where,
10277 &code->loc);
10278 return;
10279 }
10280
10281
10282 /* Check whether EXPR1 has the same shape as EXPR2. */
10283
10284 static bool
10285 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
10286 {
10287 mpz_t shape[GFC_MAX_DIMENSIONS];
10288 mpz_t shape2[GFC_MAX_DIMENSIONS];
10289 bool result = false;
10290 int i;
10291
10292 /* Compare the rank. */
10293 if (expr1->rank != expr2->rank)
10294 return result;
10295
10296 /* Compare the size of each dimension. */
10297 for (i=0; i<expr1->rank; i++)
10298 {
10299 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
10300 goto ignore;
10301
10302 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
10303 goto ignore;
10304
10305 if (mpz_cmp (shape[i], shape2[i]))
10306 goto over;
10307 }
10308
10309 /* When either of the two expression is an assumed size array, we
10310 ignore the comparison of dimension sizes. */
10311 ignore:
10312 result = true;
10313
10314 over:
10315 gfc_clear_shape (shape, i);
10316 gfc_clear_shape (shape2, i);
10317 return result;
10318 }
10319
10320
10321 /* Check whether a WHERE assignment target or a WHERE mask expression
10322 has the same shape as the outmost WHERE mask expression. */
10323
10324 static void
10325 resolve_where (gfc_code *code, gfc_expr *mask)
10326 {
10327 gfc_code *cblock;
10328 gfc_code *cnext;
10329 gfc_expr *e = NULL;
10330
10331 cblock = code->block;
10332
10333 /* Store the first WHERE mask-expr of the WHERE statement or construct.
10334 In case of nested WHERE, only the outmost one is stored. */
10335 if (mask == NULL) /* outmost WHERE */
10336 e = cblock->expr1;
10337 else /* inner WHERE */
10338 e = mask;
10339
10340 while (cblock)
10341 {
10342 if (cblock->expr1)
10343 {
10344 /* Check if the mask-expr has a consistent shape with the
10345 outmost WHERE mask-expr. */
10346 if (!resolve_where_shape (cblock->expr1, e))
10347 gfc_error ("WHERE mask at %L has inconsistent shape",
10348 &cblock->expr1->where);
10349 }
10350
10351 /* the assignment statement of a WHERE statement, or the first
10352 statement in where-body-construct of a WHERE construct */
10353 cnext = cblock->next;
10354 while (cnext)
10355 {
10356 switch (cnext->op)
10357 {
10358 /* WHERE assignment statement */
10359 case EXEC_ASSIGN:
10360
10361 /* Check shape consistent for WHERE assignment target. */
10362 if (e && !resolve_where_shape (cnext->expr1, e))
10363 gfc_error ("WHERE assignment target at %L has "
10364 "inconsistent shape", &cnext->expr1->where);
10365 break;
10366
10367
10368 case EXEC_ASSIGN_CALL:
10369 resolve_call (cnext);
10370 if (!cnext->resolved_sym->attr.elemental)
10371 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10372 &cnext->ext.actual->expr->where);
10373 break;
10374
10375 /* WHERE or WHERE construct is part of a where-body-construct */
10376 case EXEC_WHERE:
10377 resolve_where (cnext, e);
10378 break;
10379
10380 default:
10381 gfc_error ("Unsupported statement inside WHERE at %L",
10382 &cnext->loc);
10383 }
10384 /* the next statement within the same where-body-construct */
10385 cnext = cnext->next;
10386 }
10387 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10388 cblock = cblock->block;
10389 }
10390 }
10391
10392
10393 /* Resolve assignment in FORALL construct.
10394 NVAR is the number of FORALL index variables, and VAR_EXPR records the
10395 FORALL index variables. */
10396
10397 static void
10398 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
10399 {
10400 int n;
10401
10402 for (n = 0; n < nvar; n++)
10403 {
10404 gfc_symbol *forall_index;
10405
10406 forall_index = var_expr[n]->symtree->n.sym;
10407
10408 /* Check whether the assignment target is one of the FORALL index
10409 variable. */
10410 if ((code->expr1->expr_type == EXPR_VARIABLE)
10411 && (code->expr1->symtree->n.sym == forall_index))
10412 gfc_error ("Assignment to a FORALL index variable at %L",
10413 &code->expr1->where);
10414 else
10415 {
10416 /* If one of the FORALL index variables doesn't appear in the
10417 assignment variable, then there could be a many-to-one
10418 assignment. Emit a warning rather than an error because the
10419 mask could be resolving this problem. */
10420 if (!find_forall_index (code->expr1, forall_index, 0))
10421 gfc_warning (0, "The FORALL with index %qs is not used on the "
10422 "left side of the assignment at %L and so might "
10423 "cause multiple assignment to this object",
10424 var_expr[n]->symtree->name, &code->expr1->where);
10425 }
10426 }
10427 }
10428
10429
10430 /* Resolve WHERE statement in FORALL construct. */
10431
10432 static void
10433 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
10434 gfc_expr **var_expr)
10435 {
10436 gfc_code *cblock;
10437 gfc_code *cnext;
10438
10439 cblock = code->block;
10440 while (cblock)
10441 {
10442 /* the assignment statement of a WHERE statement, or the first
10443 statement in where-body-construct of a WHERE construct */
10444 cnext = cblock->next;
10445 while (cnext)
10446 {
10447 switch (cnext->op)
10448 {
10449 /* WHERE assignment statement */
10450 case EXEC_ASSIGN:
10451 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
10452 break;
10453
10454 /* WHERE operator assignment statement */
10455 case EXEC_ASSIGN_CALL:
10456 resolve_call (cnext);
10457 if (!cnext->resolved_sym->attr.elemental)
10458 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10459 &cnext->ext.actual->expr->where);
10460 break;
10461
10462 /* WHERE or WHERE construct is part of a where-body-construct */
10463 case EXEC_WHERE:
10464 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
10465 break;
10466
10467 default:
10468 gfc_error ("Unsupported statement inside WHERE at %L",
10469 &cnext->loc);
10470 }
10471 /* the next statement within the same where-body-construct */
10472 cnext = cnext->next;
10473 }
10474 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10475 cblock = cblock->block;
10476 }
10477 }
10478
10479
10480 /* Traverse the FORALL body to check whether the following errors exist:
10481 1. For assignment, check if a many-to-one assignment happens.
10482 2. For WHERE statement, check the WHERE body to see if there is any
10483 many-to-one assignment. */
10484
10485 static void
10486 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
10487 {
10488 gfc_code *c;
10489
10490 c = code->block->next;
10491 while (c)
10492 {
10493 switch (c->op)
10494 {
10495 case EXEC_ASSIGN:
10496 case EXEC_POINTER_ASSIGN:
10497 gfc_resolve_assign_in_forall (c, nvar, var_expr);
10498 break;
10499
10500 case EXEC_ASSIGN_CALL:
10501 resolve_call (c);
10502 break;
10503
10504 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
10505 there is no need to handle it here. */
10506 case EXEC_FORALL:
10507 break;
10508 case EXEC_WHERE:
10509 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
10510 break;
10511 default:
10512 break;
10513 }
10514 /* The next statement in the FORALL body. */
10515 c = c->next;
10516 }
10517 }
10518
10519
10520 /* Counts the number of iterators needed inside a forall construct, including
10521 nested forall constructs. This is used to allocate the needed memory
10522 in gfc_resolve_forall. */
10523
10524 static int
10525 gfc_count_forall_iterators (gfc_code *code)
10526 {
10527 int max_iters, sub_iters, current_iters;
10528 gfc_forall_iterator *fa;
10529
10530 gcc_assert(code->op == EXEC_FORALL);
10531 max_iters = 0;
10532 current_iters = 0;
10533
10534 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
10535 current_iters ++;
10536
10537 code = code->block->next;
10538
10539 while (code)
10540 {
10541 if (code->op == EXEC_FORALL)
10542 {
10543 sub_iters = gfc_count_forall_iterators (code);
10544 if (sub_iters > max_iters)
10545 max_iters = sub_iters;
10546 }
10547 code = code->next;
10548 }
10549
10550 return current_iters + max_iters;
10551 }
10552
10553
10554 /* Given a FORALL construct, first resolve the FORALL iterator, then call
10555 gfc_resolve_forall_body to resolve the FORALL body. */
10556
10557 static void
10558 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
10559 {
10560 static gfc_expr **var_expr;
10561 static int total_var = 0;
10562 static int nvar = 0;
10563 int i, old_nvar, tmp;
10564 gfc_forall_iterator *fa;
10565
10566 old_nvar = nvar;
10567
10568 if (!gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
10569 return;
10570
10571 /* Start to resolve a FORALL construct */
10572 if (forall_save == 0)
10573 {
10574 /* Count the total number of FORALL indices in the nested FORALL
10575 construct in order to allocate the VAR_EXPR with proper size. */
10576 total_var = gfc_count_forall_iterators (code);
10577
10578 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
10579 var_expr = XCNEWVEC (gfc_expr *, total_var);
10580 }
10581
10582 /* The information about FORALL iterator, including FORALL indices start, end
10583 and stride. An outer FORALL indice cannot appear in start, end or stride. */
10584 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
10585 {
10586 /* Fortran 20008: C738 (R753). */
10587 if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
10588 {
10589 gfc_error ("FORALL index-name at %L must be a scalar variable "
10590 "of type integer", &fa->var->where);
10591 continue;
10592 }
10593
10594 /* Check if any outer FORALL index name is the same as the current
10595 one. */
10596 for (i = 0; i < nvar; i++)
10597 {
10598 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
10599 gfc_error ("An outer FORALL construct already has an index "
10600 "with this name %L", &fa->var->where);
10601 }
10602
10603 /* Record the current FORALL index. */
10604 var_expr[nvar] = gfc_copy_expr (fa->var);
10605
10606 nvar++;
10607
10608 /* No memory leak. */
10609 gcc_assert (nvar <= total_var);
10610 }
10611
10612 /* Resolve the FORALL body. */
10613 gfc_resolve_forall_body (code, nvar, var_expr);
10614
10615 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
10616 gfc_resolve_blocks (code->block, ns);
10617
10618 tmp = nvar;
10619 nvar = old_nvar;
10620 /* Free only the VAR_EXPRs allocated in this frame. */
10621 for (i = nvar; i < tmp; i++)
10622 gfc_free_expr (var_expr[i]);
10623
10624 if (nvar == 0)
10625 {
10626 /* We are in the outermost FORALL construct. */
10627 gcc_assert (forall_save == 0);
10628
10629 /* VAR_EXPR is not needed any more. */
10630 free (var_expr);
10631 total_var = 0;
10632 }
10633 }
10634
10635
10636 /* Resolve a BLOCK construct statement. */
10637
10638 static void
10639 resolve_block_construct (gfc_code* code)
10640 {
10641 /* Resolve the BLOCK's namespace. */
10642 gfc_resolve (code->ext.block.ns);
10643
10644 /* For an ASSOCIATE block, the associations (and their targets) are already
10645 resolved during resolve_symbol. */
10646 }
10647
10648
10649 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
10650 DO code nodes. */
10651
10652 void
10653 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
10654 {
10655 bool t;
10656
10657 for (; b; b = b->block)
10658 {
10659 t = gfc_resolve_expr (b->expr1);
10660 if (!gfc_resolve_expr (b->expr2))
10661 t = false;
10662
10663 switch (b->op)
10664 {
10665 case EXEC_IF:
10666 if (t && b->expr1 != NULL
10667 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
10668 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
10669 &b->expr1->where);
10670 break;
10671
10672 case EXEC_WHERE:
10673 if (t
10674 && b->expr1 != NULL
10675 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
10676 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
10677 &b->expr1->where);
10678 break;
10679
10680 case EXEC_GOTO:
10681 resolve_branch (b->label1, b);
10682 break;
10683
10684 case EXEC_BLOCK:
10685 resolve_block_construct (b);
10686 break;
10687
10688 case EXEC_SELECT:
10689 case EXEC_SELECT_TYPE:
10690 case EXEC_SELECT_RANK:
10691 case EXEC_FORALL:
10692 case EXEC_DO:
10693 case EXEC_DO_WHILE:
10694 case EXEC_DO_CONCURRENT:
10695 case EXEC_CRITICAL:
10696 case EXEC_READ:
10697 case EXEC_WRITE:
10698 case EXEC_IOLENGTH:
10699 case EXEC_WAIT:
10700 break;
10701
10702 case EXEC_OMP_ATOMIC:
10703 case EXEC_OACC_ATOMIC:
10704 {
10705 gfc_omp_atomic_op aop
10706 = (gfc_omp_atomic_op) (b->ext.omp_atomic & GFC_OMP_ATOMIC_MASK);
10707
10708 /* Verify this before calling gfc_resolve_code, which might
10709 change it. */
10710 gcc_assert (b->next && b->next->op == EXEC_ASSIGN);
10711 gcc_assert (((aop != GFC_OMP_ATOMIC_CAPTURE)
10712 && b->next->next == NULL)
10713 || ((aop == GFC_OMP_ATOMIC_CAPTURE)
10714 && b->next->next != NULL
10715 && b->next->next->op == EXEC_ASSIGN
10716 && b->next->next->next == NULL));
10717 }
10718 break;
10719
10720 case EXEC_OACC_PARALLEL_LOOP:
10721 case EXEC_OACC_PARALLEL:
10722 case EXEC_OACC_KERNELS_LOOP:
10723 case EXEC_OACC_KERNELS:
10724 case EXEC_OACC_SERIAL_LOOP:
10725 case EXEC_OACC_SERIAL:
10726 case EXEC_OACC_DATA:
10727 case EXEC_OACC_HOST_DATA:
10728 case EXEC_OACC_LOOP:
10729 case EXEC_OACC_UPDATE:
10730 case EXEC_OACC_WAIT:
10731 case EXEC_OACC_CACHE:
10732 case EXEC_OACC_ENTER_DATA:
10733 case EXEC_OACC_EXIT_DATA:
10734 case EXEC_OACC_ROUTINE:
10735 case EXEC_OMP_CRITICAL:
10736 case EXEC_OMP_DISTRIBUTE:
10737 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
10738 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
10739 case EXEC_OMP_DISTRIBUTE_SIMD:
10740 case EXEC_OMP_DO:
10741 case EXEC_OMP_DO_SIMD:
10742 case EXEC_OMP_MASTER:
10743 case EXEC_OMP_ORDERED:
10744 case EXEC_OMP_PARALLEL:
10745 case EXEC_OMP_PARALLEL_DO:
10746 case EXEC_OMP_PARALLEL_DO_SIMD:
10747 case EXEC_OMP_PARALLEL_SECTIONS:
10748 case EXEC_OMP_PARALLEL_WORKSHARE:
10749 case EXEC_OMP_SECTIONS:
10750 case EXEC_OMP_SIMD:
10751 case EXEC_OMP_SINGLE:
10752 case EXEC_OMP_TARGET:
10753 case EXEC_OMP_TARGET_DATA:
10754 case EXEC_OMP_TARGET_ENTER_DATA:
10755 case EXEC_OMP_TARGET_EXIT_DATA:
10756 case EXEC_OMP_TARGET_PARALLEL:
10757 case EXEC_OMP_TARGET_PARALLEL_DO:
10758 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
10759 case EXEC_OMP_TARGET_SIMD:
10760 case EXEC_OMP_TARGET_TEAMS:
10761 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10762 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10763 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10764 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10765 case EXEC_OMP_TARGET_UPDATE:
10766 case EXEC_OMP_TASK:
10767 case EXEC_OMP_TASKGROUP:
10768 case EXEC_OMP_TASKLOOP:
10769 case EXEC_OMP_TASKLOOP_SIMD:
10770 case EXEC_OMP_TASKWAIT:
10771 case EXEC_OMP_TASKYIELD:
10772 case EXEC_OMP_TEAMS:
10773 case EXEC_OMP_TEAMS_DISTRIBUTE:
10774 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10775 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10776 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10777 case EXEC_OMP_WORKSHARE:
10778 break;
10779
10780 default:
10781 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
10782 }
10783
10784 gfc_resolve_code (b->next, ns);
10785 }
10786 }
10787
10788
10789 /* Does everything to resolve an ordinary assignment. Returns true
10790 if this is an interface assignment. */
10791 static bool
10792 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
10793 {
10794 bool rval = false;
10795 gfc_expr *lhs;
10796 gfc_expr *rhs;
10797 int n;
10798 gfc_ref *ref;
10799 symbol_attribute attr;
10800
10801 if (gfc_extend_assign (code, ns))
10802 {
10803 gfc_expr** rhsptr;
10804
10805 if (code->op == EXEC_ASSIGN_CALL)
10806 {
10807 lhs = code->ext.actual->expr;
10808 rhsptr = &code->ext.actual->next->expr;
10809 }
10810 else
10811 {
10812 gfc_actual_arglist* args;
10813 gfc_typebound_proc* tbp;
10814
10815 gcc_assert (code->op == EXEC_COMPCALL);
10816
10817 args = code->expr1->value.compcall.actual;
10818 lhs = args->expr;
10819 rhsptr = &args->next->expr;
10820
10821 tbp = code->expr1->value.compcall.tbp;
10822 gcc_assert (!tbp->is_generic);
10823 }
10824
10825 /* Make a temporary rhs when there is a default initializer
10826 and rhs is the same symbol as the lhs. */
10827 if ((*rhsptr)->expr_type == EXPR_VARIABLE
10828 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
10829 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
10830 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
10831 *rhsptr = gfc_get_parentheses (*rhsptr);
10832
10833 return true;
10834 }
10835
10836 lhs = code->expr1;
10837 rhs = code->expr2;
10838
10839 if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
10840 && rhs->ts.type == BT_CHARACTER
10841 && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
10842 {
10843 /* Use of -fdec-char-conversions allows assignment of character data
10844 to non-character variables. This not permited for nonconstant
10845 strings. */
10846 gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
10847 gfc_typename (lhs), &rhs->where);
10848 return false;
10849 }
10850
10851 /* Handle the case of a BOZ literal on the RHS. */
10852 if (rhs->ts.type == BT_BOZ)
10853 {
10854 if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
10855 "statement value nor an actual argument of "
10856 "INT/REAL/DBLE/CMPLX intrinsic subprogram",
10857 &rhs->where))
10858 return false;
10859
10860 switch (lhs->ts.type)
10861 {
10862 case BT_INTEGER:
10863 if (!gfc_boz2int (rhs, lhs->ts.kind))
10864 return false;
10865 break;
10866 case BT_REAL:
10867 if (!gfc_boz2real (rhs, lhs->ts.kind))
10868 return false;
10869 break;
10870 default:
10871 gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
10872 return false;
10873 }
10874 }
10875
10876 if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
10877 {
10878 HOST_WIDE_INT llen = 0, rlen = 0;
10879 if (lhs->ts.u.cl != NULL
10880 && lhs->ts.u.cl->length != NULL
10881 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10882 llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
10883
10884 if (rhs->expr_type == EXPR_CONSTANT)
10885 rlen = rhs->value.character.length;
10886
10887 else if (rhs->ts.u.cl != NULL
10888 && rhs->ts.u.cl->length != NULL
10889 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10890 rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
10891
10892 if (rlen && llen && rlen > llen)
10893 gfc_warning_now (OPT_Wcharacter_truncation,
10894 "CHARACTER expression will be truncated "
10895 "in assignment (%ld/%ld) at %L",
10896 (long) llen, (long) rlen, &code->loc);
10897 }
10898
10899 /* Ensure that a vector index expression for the lvalue is evaluated
10900 to a temporary if the lvalue symbol is referenced in it. */
10901 if (lhs->rank)
10902 {
10903 for (ref = lhs->ref; ref; ref= ref->next)
10904 if (ref->type == REF_ARRAY)
10905 {
10906 for (n = 0; n < ref->u.ar.dimen; n++)
10907 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
10908 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
10909 ref->u.ar.start[n]))
10910 ref->u.ar.start[n]
10911 = gfc_get_parentheses (ref->u.ar.start[n]);
10912 }
10913 }
10914
10915 if (gfc_pure (NULL))
10916 {
10917 if (lhs->ts.type == BT_DERIVED
10918 && lhs->expr_type == EXPR_VARIABLE
10919 && lhs->ts.u.derived->attr.pointer_comp
10920 && rhs->expr_type == EXPR_VARIABLE
10921 && (gfc_impure_variable (rhs->symtree->n.sym)
10922 || gfc_is_coindexed (rhs)))
10923 {
10924 /* F2008, C1283. */
10925 if (gfc_is_coindexed (rhs))
10926 gfc_error ("Coindexed expression at %L is assigned to "
10927 "a derived type variable with a POINTER "
10928 "component in a PURE procedure",
10929 &rhs->where);
10930 else
10931 /* F2008, C1283 (4). */
10932 gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
10933 "shall not be used as the expr at %L of an intrinsic "
10934 "assignment statement in which the variable is of a "
10935 "derived type if the derived type has a pointer "
10936 "component at any level of component selection.",
10937 &rhs->where);
10938 return rval;
10939 }
10940
10941 /* Fortran 2008, C1283. */
10942 if (gfc_is_coindexed (lhs))
10943 {
10944 gfc_error ("Assignment to coindexed variable at %L in a PURE "
10945 "procedure", &rhs->where);
10946 return rval;
10947 }
10948 }
10949
10950 if (gfc_implicit_pure (NULL))
10951 {
10952 if (lhs->expr_type == EXPR_VARIABLE
10953 && lhs->symtree->n.sym != gfc_current_ns->proc_name
10954 && lhs->symtree->n.sym->ns != gfc_current_ns)
10955 gfc_unset_implicit_pure (NULL);
10956
10957 if (lhs->ts.type == BT_DERIVED
10958 && lhs->expr_type == EXPR_VARIABLE
10959 && lhs->ts.u.derived->attr.pointer_comp
10960 && rhs->expr_type == EXPR_VARIABLE
10961 && (gfc_impure_variable (rhs->symtree->n.sym)
10962 || gfc_is_coindexed (rhs)))
10963 gfc_unset_implicit_pure (NULL);
10964
10965 /* Fortran 2008, C1283. */
10966 if (gfc_is_coindexed (lhs))
10967 gfc_unset_implicit_pure (NULL);
10968 }
10969
10970 /* F2008, 7.2.1.2. */
10971 attr = gfc_expr_attr (lhs);
10972 if (lhs->ts.type == BT_CLASS && attr.allocatable)
10973 {
10974 if (attr.codimension)
10975 {
10976 gfc_error ("Assignment to polymorphic coarray at %L is not "
10977 "permitted", &lhs->where);
10978 return false;
10979 }
10980 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
10981 "polymorphic variable at %L", &lhs->where))
10982 return false;
10983 if (!flag_realloc_lhs)
10984 {
10985 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
10986 "requires %<-frealloc-lhs%>", &lhs->where);
10987 return false;
10988 }
10989 }
10990 else if (lhs->ts.type == BT_CLASS)
10991 {
10992 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
10993 "assignment at %L - check that there is a matching specific "
10994 "subroutine for '=' operator", &lhs->where);
10995 return false;
10996 }
10997
10998 bool lhs_coindexed = gfc_is_coindexed (lhs);
10999
11000 /* F2008, Section 7.2.1.2. */
11001 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
11002 {
11003 gfc_error ("Coindexed variable must not have an allocatable ultimate "
11004 "component in assignment at %L", &lhs->where);
11005 return false;
11006 }
11007
11008 /* Assign the 'data' of a class object to a derived type. */
11009 if (lhs->ts.type == BT_DERIVED
11010 && rhs->ts.type == BT_CLASS
11011 && rhs->expr_type != EXPR_ARRAY)
11012 gfc_add_data_component (rhs);
11013
11014 /* Make sure there is a vtable and, in particular, a _copy for the
11015 rhs type. */
11016 if (UNLIMITED_POLY (lhs) && lhs->rank && rhs->ts.type != BT_CLASS)
11017 gfc_find_vtab (&rhs->ts);
11018
11019 bool caf_convert_to_send = flag_coarray == GFC_FCOARRAY_LIB
11020 && (lhs_coindexed
11021 || (code->expr2->expr_type == EXPR_FUNCTION
11022 && code->expr2->value.function.isym
11023 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
11024 && (code->expr1->rank == 0 || code->expr2->rank != 0)
11025 && !gfc_expr_attr (rhs).allocatable
11026 && !gfc_has_vector_subscript (rhs)));
11027
11028 gfc_check_assign (lhs, rhs, 1, !caf_convert_to_send);
11029
11030 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
11031 Additionally, insert this code when the RHS is a CAF as we then use the
11032 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
11033 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
11034 noncoindexed array and the RHS is a coindexed scalar, use the normal code
11035 path. */
11036 if (caf_convert_to_send)
11037 {
11038 if (code->expr2->expr_type == EXPR_FUNCTION
11039 && code->expr2->value.function.isym
11040 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
11041 remove_caf_get_intrinsic (code->expr2);
11042 code->op = EXEC_CALL;
11043 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
11044 code->resolved_sym = code->symtree->n.sym;
11045 code->resolved_sym->attr.flavor = FL_PROCEDURE;
11046 code->resolved_sym->attr.intrinsic = 1;
11047 code->resolved_sym->attr.subroutine = 1;
11048 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
11049 gfc_commit_symbol (code->resolved_sym);
11050 code->ext.actual = gfc_get_actual_arglist ();
11051 code->ext.actual->expr = lhs;
11052 code->ext.actual->next = gfc_get_actual_arglist ();
11053 code->ext.actual->next->expr = rhs;
11054 code->expr1 = NULL;
11055 code->expr2 = NULL;
11056 }
11057
11058 return false;
11059 }
11060
11061
11062 /* Add a component reference onto an expression. */
11063
11064 static void
11065 add_comp_ref (gfc_expr *e, gfc_component *c)
11066 {
11067 gfc_ref **ref;
11068 ref = &(e->ref);
11069 while (*ref)
11070 ref = &((*ref)->next);
11071 *ref = gfc_get_ref ();
11072 (*ref)->type = REF_COMPONENT;
11073 (*ref)->u.c.sym = e->ts.u.derived;
11074 (*ref)->u.c.component = c;
11075 e->ts = c->ts;
11076
11077 /* Add a full array ref, as necessary. */
11078 if (c->as)
11079 {
11080 gfc_add_full_array_ref (e, c->as);
11081 e->rank = c->as->rank;
11082 }
11083 }
11084
11085
11086 /* Build an assignment. Keep the argument 'op' for future use, so that
11087 pointer assignments can be made. */
11088
11089 static gfc_code *
11090 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
11091 gfc_component *comp1, gfc_component *comp2, locus loc)
11092 {
11093 gfc_code *this_code;
11094
11095 this_code = gfc_get_code (op);
11096 this_code->next = NULL;
11097 this_code->expr1 = gfc_copy_expr (expr1);
11098 this_code->expr2 = gfc_copy_expr (expr2);
11099 this_code->loc = loc;
11100 if (comp1 && comp2)
11101 {
11102 add_comp_ref (this_code->expr1, comp1);
11103 add_comp_ref (this_code->expr2, comp2);
11104 }
11105
11106 return this_code;
11107 }
11108
11109
11110 /* Makes a temporary variable expression based on the characteristics of
11111 a given variable expression. */
11112
11113 static gfc_expr*
11114 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
11115 {
11116 static int serial = 0;
11117 char name[GFC_MAX_SYMBOL_LEN];
11118 gfc_symtree *tmp;
11119 gfc_array_spec *as;
11120 gfc_array_ref *aref;
11121 gfc_ref *ref;
11122
11123 sprintf (name, GFC_PREFIX("DA%d"), serial++);
11124 gfc_get_sym_tree (name, ns, &tmp, false);
11125 gfc_add_type (tmp->n.sym, &e->ts, NULL);
11126
11127 if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
11128 tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
11129 NULL,
11130 e->value.character.length);
11131
11132 as = NULL;
11133 ref = NULL;
11134 aref = NULL;
11135
11136 /* Obtain the arrayspec for the temporary. */
11137 if (e->rank && e->expr_type != EXPR_ARRAY
11138 && e->expr_type != EXPR_FUNCTION
11139 && e->expr_type != EXPR_OP)
11140 {
11141 aref = gfc_find_array_ref (e);
11142 if (e->expr_type == EXPR_VARIABLE
11143 && e->symtree->n.sym->as == aref->as)
11144 as = aref->as;
11145 else
11146 {
11147 for (ref = e->ref; ref; ref = ref->next)
11148 if (ref->type == REF_COMPONENT
11149 && ref->u.c.component->as == aref->as)
11150 {
11151 as = aref->as;
11152 break;
11153 }
11154 }
11155 }
11156
11157 /* Add the attributes and the arrayspec to the temporary. */
11158 tmp->n.sym->attr = gfc_expr_attr (e);
11159 tmp->n.sym->attr.function = 0;
11160 tmp->n.sym->attr.result = 0;
11161 tmp->n.sym->attr.flavor = FL_VARIABLE;
11162 tmp->n.sym->attr.dummy = 0;
11163 tmp->n.sym->attr.intent = INTENT_UNKNOWN;
11164
11165 if (as)
11166 {
11167 tmp->n.sym->as = gfc_copy_array_spec (as);
11168 if (!ref)
11169 ref = e->ref;
11170 if (as->type == AS_DEFERRED)
11171 tmp->n.sym->attr.allocatable = 1;
11172 }
11173 else if (e->rank && (e->expr_type == EXPR_ARRAY
11174 || e->expr_type == EXPR_FUNCTION
11175 || e->expr_type == EXPR_OP))
11176 {
11177 tmp->n.sym->as = gfc_get_array_spec ();
11178 tmp->n.sym->as->type = AS_DEFERRED;
11179 tmp->n.sym->as->rank = e->rank;
11180 tmp->n.sym->attr.allocatable = 1;
11181 tmp->n.sym->attr.dimension = 1;
11182 }
11183 else
11184 tmp->n.sym->attr.dimension = 0;
11185
11186 gfc_set_sym_referenced (tmp->n.sym);
11187 gfc_commit_symbol (tmp->n.sym);
11188 e = gfc_lval_expr_from_sym (tmp->n.sym);
11189
11190 /* Should the lhs be a section, use its array ref for the
11191 temporary expression. */
11192 if (aref && aref->type != AR_FULL)
11193 {
11194 gfc_free_ref_list (e->ref);
11195 e->ref = gfc_copy_ref (ref);
11196 }
11197 return e;
11198 }
11199
11200
11201 /* Add one line of code to the code chain, making sure that 'head' and
11202 'tail' are appropriately updated. */
11203
11204 static void
11205 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
11206 {
11207 gcc_assert (this_code);
11208 if (*head == NULL)
11209 *head = *tail = *this_code;
11210 else
11211 *tail = gfc_append_code (*tail, *this_code);
11212 *this_code = NULL;
11213 }
11214
11215
11216 /* Counts the potential number of part array references that would
11217 result from resolution of typebound defined assignments. */
11218
11219 static int
11220 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
11221 {
11222 gfc_component *c;
11223 int c_depth = 0, t_depth;
11224
11225 for (c= derived->components; c; c = c->next)
11226 {
11227 if ((!gfc_bt_struct (c->ts.type)
11228 || c->attr.pointer
11229 || c->attr.allocatable
11230 || c->attr.proc_pointer_comp
11231 || c->attr.class_pointer
11232 || c->attr.proc_pointer)
11233 && !c->attr.defined_assign_comp)
11234 continue;
11235
11236 if (c->as && c_depth == 0)
11237 c_depth = 1;
11238
11239 if (c->ts.u.derived->attr.defined_assign_comp)
11240 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
11241 c->as ? 1 : 0);
11242 else
11243 t_depth = 0;
11244
11245 c_depth = t_depth > c_depth ? t_depth : c_depth;
11246 }
11247 return depth + c_depth;
11248 }
11249
11250
11251 /* Implement 7.2.1.3 of the F08 standard:
11252 "An intrinsic assignment where the variable is of derived type is
11253 performed as if each component of the variable were assigned from the
11254 corresponding component of expr using pointer assignment (7.2.2) for
11255 each pointer component, defined assignment for each nonpointer
11256 nonallocatable component of a type that has a type-bound defined
11257 assignment consistent with the component, intrinsic assignment for
11258 each other nonpointer nonallocatable component, ..."
11259
11260 The pointer assignments are taken care of by the intrinsic
11261 assignment of the structure itself. This function recursively adds
11262 defined assignments where required. The recursion is accomplished
11263 by calling gfc_resolve_code.
11264
11265 When the lhs in a defined assignment has intent INOUT, we need a
11266 temporary for the lhs. In pseudo-code:
11267
11268 ! Only call function lhs once.
11269 if (lhs is not a constant or an variable)
11270 temp_x = expr2
11271 expr2 => temp_x
11272 ! Do the intrinsic assignment
11273 expr1 = expr2
11274 ! Now do the defined assignments
11275 do over components with typebound defined assignment [%cmp]
11276 #if one component's assignment procedure is INOUT
11277 t1 = expr1
11278 #if expr2 non-variable
11279 temp_x = expr2
11280 expr2 => temp_x
11281 # endif
11282 expr1 = expr2
11283 # for each cmp
11284 t1%cmp {defined=} expr2%cmp
11285 expr1%cmp = t1%cmp
11286 #else
11287 expr1 = expr2
11288
11289 # for each cmp
11290 expr1%cmp {defined=} expr2%cmp
11291 #endif
11292 */
11293
11294 /* The temporary assignments have to be put on top of the additional
11295 code to avoid the result being changed by the intrinsic assignment.
11296 */
11297 static int component_assignment_level = 0;
11298 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
11299
11300 static void
11301 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
11302 {
11303 gfc_component *comp1, *comp2;
11304 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
11305 gfc_expr *t1;
11306 int error_count, depth;
11307
11308 gfc_get_errors (NULL, &error_count);
11309
11310 /* Filter out continuing processing after an error. */
11311 if (error_count
11312 || (*code)->expr1->ts.type != BT_DERIVED
11313 || (*code)->expr2->ts.type != BT_DERIVED)
11314 return;
11315
11316 /* TODO: Handle more than one part array reference in assignments. */
11317 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
11318 (*code)->expr1->rank ? 1 : 0);
11319 if (depth > 1)
11320 {
11321 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
11322 "done because multiple part array references would "
11323 "occur in intermediate expressions.", &(*code)->loc);
11324 return;
11325 }
11326
11327 component_assignment_level++;
11328
11329 /* Create a temporary so that functions get called only once. */
11330 if ((*code)->expr2->expr_type != EXPR_VARIABLE
11331 && (*code)->expr2->expr_type != EXPR_CONSTANT)
11332 {
11333 gfc_expr *tmp_expr;
11334
11335 /* Assign the rhs to the temporary. */
11336 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
11337 this_code = build_assignment (EXEC_ASSIGN,
11338 tmp_expr, (*code)->expr2,
11339 NULL, NULL, (*code)->loc);
11340 /* Add the code and substitute the rhs expression. */
11341 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
11342 gfc_free_expr ((*code)->expr2);
11343 (*code)->expr2 = tmp_expr;
11344 }
11345
11346 /* Do the intrinsic assignment. This is not needed if the lhs is one
11347 of the temporaries generated here, since the intrinsic assignment
11348 to the final result already does this. */
11349 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
11350 {
11351 this_code = build_assignment (EXEC_ASSIGN,
11352 (*code)->expr1, (*code)->expr2,
11353 NULL, NULL, (*code)->loc);
11354 add_code_to_chain (&this_code, &head, &tail);
11355 }
11356
11357 comp1 = (*code)->expr1->ts.u.derived->components;
11358 comp2 = (*code)->expr2->ts.u.derived->components;
11359
11360 t1 = NULL;
11361 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
11362 {
11363 bool inout = false;
11364
11365 /* The intrinsic assignment does the right thing for pointers
11366 of all kinds and allocatable components. */
11367 if (!gfc_bt_struct (comp1->ts.type)
11368 || comp1->attr.pointer
11369 || comp1->attr.allocatable
11370 || comp1->attr.proc_pointer_comp
11371 || comp1->attr.class_pointer
11372 || comp1->attr.proc_pointer)
11373 continue;
11374
11375 /* Make an assigment for this component. */
11376 this_code = build_assignment (EXEC_ASSIGN,
11377 (*code)->expr1, (*code)->expr2,
11378 comp1, comp2, (*code)->loc);
11379
11380 /* Convert the assignment if there is a defined assignment for
11381 this type. Otherwise, using the call from gfc_resolve_code,
11382 recurse into its components. */
11383 gfc_resolve_code (this_code, ns);
11384
11385 if (this_code->op == EXEC_ASSIGN_CALL)
11386 {
11387 gfc_formal_arglist *dummy_args;
11388 gfc_symbol *rsym;
11389 /* Check that there is a typebound defined assignment. If not,
11390 then this must be a module defined assignment. We cannot
11391 use the defined_assign_comp attribute here because it must
11392 be this derived type that has the defined assignment and not
11393 a parent type. */
11394 if (!(comp1->ts.u.derived->f2k_derived
11395 && comp1->ts.u.derived->f2k_derived
11396 ->tb_op[INTRINSIC_ASSIGN]))
11397 {
11398 gfc_free_statements (this_code);
11399 this_code = NULL;
11400 continue;
11401 }
11402
11403 /* If the first argument of the subroutine has intent INOUT
11404 a temporary must be generated and used instead. */
11405 rsym = this_code->resolved_sym;
11406 dummy_args = gfc_sym_get_dummy_args (rsym);
11407 if (dummy_args
11408 && dummy_args->sym->attr.intent == INTENT_INOUT)
11409 {
11410 gfc_code *temp_code;
11411 inout = true;
11412
11413 /* Build the temporary required for the assignment and put
11414 it at the head of the generated code. */
11415 if (!t1)
11416 {
11417 t1 = get_temp_from_expr ((*code)->expr1, ns);
11418 temp_code = build_assignment (EXEC_ASSIGN,
11419 t1, (*code)->expr1,
11420 NULL, NULL, (*code)->loc);
11421
11422 /* For allocatable LHS, check whether it is allocated. Note
11423 that allocatable components with defined assignment are
11424 not yet support. See PR 57696. */
11425 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
11426 {
11427 gfc_code *block;
11428 gfc_expr *e =
11429 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
11430 block = gfc_get_code (EXEC_IF);
11431 block->block = gfc_get_code (EXEC_IF);
11432 block->block->expr1
11433 = gfc_build_intrinsic_call (ns,
11434 GFC_ISYM_ALLOCATED, "allocated",
11435 (*code)->loc, 1, e);
11436 block->block->next = temp_code;
11437 temp_code = block;
11438 }
11439 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
11440 }
11441
11442 /* Replace the first actual arg with the component of the
11443 temporary. */
11444 gfc_free_expr (this_code->ext.actual->expr);
11445 this_code->ext.actual->expr = gfc_copy_expr (t1);
11446 add_comp_ref (this_code->ext.actual->expr, comp1);
11447
11448 /* If the LHS variable is allocatable and wasn't allocated and
11449 the temporary is allocatable, pointer assign the address of
11450 the freshly allocated LHS to the temporary. */
11451 if ((*code)->expr1->symtree->n.sym->attr.allocatable
11452 && gfc_expr_attr ((*code)->expr1).allocatable)
11453 {
11454 gfc_code *block;
11455 gfc_expr *cond;
11456
11457 cond = gfc_get_expr ();
11458 cond->ts.type = BT_LOGICAL;
11459 cond->ts.kind = gfc_default_logical_kind;
11460 cond->expr_type = EXPR_OP;
11461 cond->where = (*code)->loc;
11462 cond->value.op.op = INTRINSIC_NOT;
11463 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
11464 GFC_ISYM_ALLOCATED, "allocated",
11465 (*code)->loc, 1, gfc_copy_expr (t1));
11466 block = gfc_get_code (EXEC_IF);
11467 block->block = gfc_get_code (EXEC_IF);
11468 block->block->expr1 = cond;
11469 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
11470 t1, (*code)->expr1,
11471 NULL, NULL, (*code)->loc);
11472 add_code_to_chain (&block, &head, &tail);
11473 }
11474 }
11475 }
11476 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
11477 {
11478 /* Don't add intrinsic assignments since they are already
11479 effected by the intrinsic assignment of the structure. */
11480 gfc_free_statements (this_code);
11481 this_code = NULL;
11482 continue;
11483 }
11484
11485 add_code_to_chain (&this_code, &head, &tail);
11486
11487 if (t1 && inout)
11488 {
11489 /* Transfer the value to the final result. */
11490 this_code = build_assignment (EXEC_ASSIGN,
11491 (*code)->expr1, t1,
11492 comp1, comp2, (*code)->loc);
11493 add_code_to_chain (&this_code, &head, &tail);
11494 }
11495 }
11496
11497 /* Put the temporary assignments at the top of the generated code. */
11498 if (tmp_head && component_assignment_level == 1)
11499 {
11500 gfc_append_code (tmp_head, head);
11501 head = tmp_head;
11502 tmp_head = tmp_tail = NULL;
11503 }
11504
11505 // If we did a pointer assignment - thus, we need to ensure that the LHS is
11506 // not accidentally deallocated. Hence, nullify t1.
11507 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
11508 && gfc_expr_attr ((*code)->expr1).allocatable)
11509 {
11510 gfc_code *block;
11511 gfc_expr *cond;
11512 gfc_expr *e;
11513
11514 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
11515 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
11516 (*code)->loc, 2, gfc_copy_expr (t1), e);
11517 block = gfc_get_code (EXEC_IF);
11518 block->block = gfc_get_code (EXEC_IF);
11519 block->block->expr1 = cond;
11520 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
11521 t1, gfc_get_null_expr (&(*code)->loc),
11522 NULL, NULL, (*code)->loc);
11523 gfc_append_code (tail, block);
11524 tail = block;
11525 }
11526
11527 /* Now attach the remaining code chain to the input code. Step on
11528 to the end of the new code since resolution is complete. */
11529 gcc_assert ((*code)->op == EXEC_ASSIGN);
11530 tail->next = (*code)->next;
11531 /* Overwrite 'code' because this would place the intrinsic assignment
11532 before the temporary for the lhs is created. */
11533 gfc_free_expr ((*code)->expr1);
11534 gfc_free_expr ((*code)->expr2);
11535 **code = *head;
11536 if (head != tail)
11537 free (head);
11538 *code = tail;
11539
11540 component_assignment_level--;
11541 }
11542
11543
11544 /* F2008: Pointer function assignments are of the form:
11545 ptr_fcn (args) = expr
11546 This function breaks these assignments into two statements:
11547 temporary_pointer => ptr_fcn(args)
11548 temporary_pointer = expr */
11549
11550 static bool
11551 resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
11552 {
11553 gfc_expr *tmp_ptr_expr;
11554 gfc_code *this_code;
11555 gfc_component *comp;
11556 gfc_symbol *s;
11557
11558 if ((*code)->expr1->expr_type != EXPR_FUNCTION)
11559 return false;
11560
11561 /* Even if standard does not support this feature, continue to build
11562 the two statements to avoid upsetting frontend_passes.c. */
11563 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
11564 "%L", &(*code)->loc);
11565
11566 comp = gfc_get_proc_ptr_comp ((*code)->expr1);
11567
11568 if (comp)
11569 s = comp->ts.interface;
11570 else
11571 s = (*code)->expr1->symtree->n.sym;
11572
11573 if (s == NULL || !s->result->attr.pointer)
11574 {
11575 gfc_error ("The function result on the lhs of the assignment at "
11576 "%L must have the pointer attribute.",
11577 &(*code)->expr1->where);
11578 (*code)->op = EXEC_NOP;
11579 return false;
11580 }
11581
11582 tmp_ptr_expr = get_temp_from_expr ((*code)->expr2, ns);
11583
11584 /* get_temp_from_expression is set up for ordinary assignments. To that
11585 end, where array bounds are not known, arrays are made allocatable.
11586 Change the temporary to a pointer here. */
11587 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
11588 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
11589 tmp_ptr_expr->where = (*code)->loc;
11590
11591 this_code = build_assignment (EXEC_ASSIGN,
11592 tmp_ptr_expr, (*code)->expr2,
11593 NULL, NULL, (*code)->loc);
11594 this_code->next = (*code)->next;
11595 (*code)->next = this_code;
11596 (*code)->op = EXEC_POINTER_ASSIGN;
11597 (*code)->expr2 = (*code)->expr1;
11598 (*code)->expr1 = tmp_ptr_expr;
11599
11600 return true;
11601 }
11602
11603
11604 /* Deferred character length assignments from an operator expression
11605 require a temporary because the character length of the lhs can
11606 change in the course of the assignment. */
11607
11608 static bool
11609 deferred_op_assign (gfc_code **code, gfc_namespace *ns)
11610 {
11611 gfc_expr *tmp_expr;
11612 gfc_code *this_code;
11613
11614 if (!((*code)->expr1->ts.type == BT_CHARACTER
11615 && (*code)->expr1->ts.deferred && (*code)->expr1->rank
11616 && (*code)->expr2->expr_type == EXPR_OP))
11617 return false;
11618
11619 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
11620 return false;
11621
11622 if (gfc_expr_attr ((*code)->expr1).pointer)
11623 return false;
11624
11625 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
11626 tmp_expr->where = (*code)->loc;
11627
11628 /* A new charlen is required to ensure that the variable string
11629 length is different to that of the original lhs. */
11630 tmp_expr->ts.u.cl = gfc_get_charlen();
11631 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
11632 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
11633 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
11634
11635 tmp_expr->symtree->n.sym->ts.deferred = 1;
11636
11637 this_code = build_assignment (EXEC_ASSIGN,
11638 (*code)->expr1,
11639 gfc_copy_expr (tmp_expr),
11640 NULL, NULL, (*code)->loc);
11641
11642 (*code)->expr1 = tmp_expr;
11643
11644 this_code->next = (*code)->next;
11645 (*code)->next = this_code;
11646
11647 return true;
11648 }
11649
11650
11651 /* Given a block of code, recursively resolve everything pointed to by this
11652 code block. */
11653
11654 void
11655 gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
11656 {
11657 int omp_workshare_save;
11658 int forall_save, do_concurrent_save;
11659 code_stack frame;
11660 bool t;
11661
11662 frame.prev = cs_base;
11663 frame.head = code;
11664 cs_base = &frame;
11665
11666 find_reachable_labels (code);
11667
11668 for (; code; code = code->next)
11669 {
11670 frame.current = code;
11671 forall_save = forall_flag;
11672 do_concurrent_save = gfc_do_concurrent_flag;
11673
11674 if (code->op == EXEC_FORALL)
11675 {
11676 forall_flag = 1;
11677 gfc_resolve_forall (code, ns, forall_save);
11678 forall_flag = 2;
11679 }
11680 else if (code->block)
11681 {
11682 omp_workshare_save = -1;
11683 switch (code->op)
11684 {
11685 case EXEC_OACC_PARALLEL_LOOP:
11686 case EXEC_OACC_PARALLEL:
11687 case EXEC_OACC_KERNELS_LOOP:
11688 case EXEC_OACC_KERNELS:
11689 case EXEC_OACC_SERIAL_LOOP:
11690 case EXEC_OACC_SERIAL:
11691 case EXEC_OACC_DATA:
11692 case EXEC_OACC_HOST_DATA:
11693 case EXEC_OACC_LOOP:
11694 gfc_resolve_oacc_blocks (code, ns);
11695 break;
11696 case EXEC_OMP_PARALLEL_WORKSHARE:
11697 omp_workshare_save = omp_workshare_flag;
11698 omp_workshare_flag = 1;
11699 gfc_resolve_omp_parallel_blocks (code, ns);
11700 break;
11701 case EXEC_OMP_PARALLEL:
11702 case EXEC_OMP_PARALLEL_DO:
11703 case EXEC_OMP_PARALLEL_DO_SIMD:
11704 case EXEC_OMP_PARALLEL_SECTIONS:
11705 case EXEC_OMP_TARGET_PARALLEL:
11706 case EXEC_OMP_TARGET_PARALLEL_DO:
11707 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
11708 case EXEC_OMP_TARGET_TEAMS:
11709 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
11710 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
11711 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11712 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
11713 case EXEC_OMP_TASK:
11714 case EXEC_OMP_TASKLOOP:
11715 case EXEC_OMP_TASKLOOP_SIMD:
11716 case EXEC_OMP_TEAMS:
11717 case EXEC_OMP_TEAMS_DISTRIBUTE:
11718 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
11719 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11720 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
11721 omp_workshare_save = omp_workshare_flag;
11722 omp_workshare_flag = 0;
11723 gfc_resolve_omp_parallel_blocks (code, ns);
11724 break;
11725 case EXEC_OMP_DISTRIBUTE:
11726 case EXEC_OMP_DISTRIBUTE_SIMD:
11727 case EXEC_OMP_DO:
11728 case EXEC_OMP_DO_SIMD:
11729 case EXEC_OMP_SIMD:
11730 case EXEC_OMP_TARGET_SIMD:
11731 gfc_resolve_omp_do_blocks (code, ns);
11732 break;
11733 case EXEC_SELECT_TYPE:
11734 /* Blocks are handled in resolve_select_type because we have
11735 to transform the SELECT TYPE into ASSOCIATE first. */
11736 break;
11737 case EXEC_DO_CONCURRENT:
11738 gfc_do_concurrent_flag = 1;
11739 gfc_resolve_blocks (code->block, ns);
11740 gfc_do_concurrent_flag = 2;
11741 break;
11742 case EXEC_OMP_WORKSHARE:
11743 omp_workshare_save = omp_workshare_flag;
11744 omp_workshare_flag = 1;
11745 /* FALL THROUGH */
11746 default:
11747 gfc_resolve_blocks (code->block, ns);
11748 break;
11749 }
11750
11751 if (omp_workshare_save != -1)
11752 omp_workshare_flag = omp_workshare_save;
11753 }
11754 start:
11755 t = true;
11756 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
11757 t = gfc_resolve_expr (code->expr1);
11758 forall_flag = forall_save;
11759 gfc_do_concurrent_flag = do_concurrent_save;
11760
11761 if (!gfc_resolve_expr (code->expr2))
11762 t = false;
11763
11764 if (code->op == EXEC_ALLOCATE
11765 && !gfc_resolve_expr (code->expr3))
11766 t = false;
11767
11768 switch (code->op)
11769 {
11770 case EXEC_NOP:
11771 case EXEC_END_BLOCK:
11772 case EXEC_END_NESTED_BLOCK:
11773 case EXEC_CYCLE:
11774 case EXEC_PAUSE:
11775 case EXEC_STOP:
11776 case EXEC_ERROR_STOP:
11777 case EXEC_EXIT:
11778 case EXEC_CONTINUE:
11779 case EXEC_DT_END:
11780 case EXEC_ASSIGN_CALL:
11781 break;
11782
11783 case EXEC_CRITICAL:
11784 resolve_critical (code);
11785 break;
11786
11787 case EXEC_SYNC_ALL:
11788 case EXEC_SYNC_IMAGES:
11789 case EXEC_SYNC_MEMORY:
11790 resolve_sync (code);
11791 break;
11792
11793 case EXEC_LOCK:
11794 case EXEC_UNLOCK:
11795 case EXEC_EVENT_POST:
11796 case EXEC_EVENT_WAIT:
11797 resolve_lock_unlock_event (code);
11798 break;
11799
11800 case EXEC_FAIL_IMAGE:
11801 case EXEC_FORM_TEAM:
11802 case EXEC_CHANGE_TEAM:
11803 case EXEC_END_TEAM:
11804 case EXEC_SYNC_TEAM:
11805 break;
11806
11807 case EXEC_ENTRY:
11808 /* Keep track of which entry we are up to. */
11809 current_entry_id = code->ext.entry->id;
11810 break;
11811
11812 case EXEC_WHERE:
11813 resolve_where (code, NULL);
11814 break;
11815
11816 case EXEC_GOTO:
11817 if (code->expr1 != NULL)
11818 {
11819 if (code->expr1->expr_type != EXPR_VARIABLE
11820 || code->expr1->ts.type != BT_INTEGER
11821 || (code->expr1->ref
11822 && code->expr1->ref->type == REF_ARRAY)
11823 || code->expr1->symtree == NULL
11824 || (code->expr1->symtree->n.sym
11825 && (code->expr1->symtree->n.sym->attr.flavor
11826 == FL_PARAMETER)))
11827 gfc_error ("ASSIGNED GOTO statement at %L requires a "
11828 "scalar INTEGER variable", &code->expr1->where);
11829 else if (code->expr1->symtree->n.sym
11830 && code->expr1->symtree->n.sym->attr.assign != 1)
11831 gfc_error ("Variable %qs has not been assigned a target "
11832 "label at %L", code->expr1->symtree->n.sym->name,
11833 &code->expr1->where);
11834 }
11835 else
11836 resolve_branch (code->label1, code);
11837 break;
11838
11839 case EXEC_RETURN:
11840 if (code->expr1 != NULL
11841 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
11842 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
11843 "INTEGER return specifier", &code->expr1->where);
11844 break;
11845
11846 case EXEC_INIT_ASSIGN:
11847 case EXEC_END_PROCEDURE:
11848 break;
11849
11850 case EXEC_ASSIGN:
11851 if (!t)
11852 break;
11853
11854 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
11855 the LHS. */
11856 if (code->expr1->expr_type == EXPR_FUNCTION
11857 && code->expr1->value.function.isym
11858 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
11859 remove_caf_get_intrinsic (code->expr1);
11860
11861 /* If this is a pointer function in an lvalue variable context,
11862 the new code will have to be resolved afresh. This is also the
11863 case with an error, where the code is transformed into NOP to
11864 prevent ICEs downstream. */
11865 if (resolve_ptr_fcn_assign (&code, ns)
11866 || code->op == EXEC_NOP)
11867 goto start;
11868
11869 if (!gfc_check_vardef_context (code->expr1, false, false, false,
11870 _("assignment")))
11871 break;
11872
11873 if (resolve_ordinary_assign (code, ns))
11874 {
11875 if (code->op == EXEC_COMPCALL)
11876 goto compcall;
11877 else
11878 goto call;
11879 }
11880
11881 /* Check for dependencies in deferred character length array
11882 assignments and generate a temporary, if necessary. */
11883 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
11884 break;
11885
11886 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
11887 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
11888 && code->expr1->ts.u.derived
11889 && code->expr1->ts.u.derived->attr.defined_assign_comp)
11890 generate_component_assignments (&code, ns);
11891
11892 break;
11893
11894 case EXEC_LABEL_ASSIGN:
11895 if (code->label1->defined == ST_LABEL_UNKNOWN)
11896 gfc_error ("Label %d referenced at %L is never defined",
11897 code->label1->value, &code->label1->where);
11898 if (t
11899 && (code->expr1->expr_type != EXPR_VARIABLE
11900 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
11901 || code->expr1->symtree->n.sym->ts.kind
11902 != gfc_default_integer_kind
11903 || code->expr1->symtree->n.sym->as != NULL))
11904 gfc_error ("ASSIGN statement at %L requires a scalar "
11905 "default INTEGER variable", &code->expr1->where);
11906 break;
11907
11908 case EXEC_POINTER_ASSIGN:
11909 {
11910 gfc_expr* e;
11911
11912 if (!t)
11913 break;
11914
11915 /* This is both a variable definition and pointer assignment
11916 context, so check both of them. For rank remapping, a final
11917 array ref may be present on the LHS and fool gfc_expr_attr
11918 used in gfc_check_vardef_context. Remove it. */
11919 e = remove_last_array_ref (code->expr1);
11920 t = gfc_check_vardef_context (e, true, false, false,
11921 _("pointer assignment"));
11922 if (t)
11923 t = gfc_check_vardef_context (e, false, false, false,
11924 _("pointer assignment"));
11925 gfc_free_expr (e);
11926
11927 t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
11928
11929 if (!t)
11930 break;
11931
11932 /* Assigning a class object always is a regular assign. */
11933 if (code->expr2->ts.type == BT_CLASS
11934 && code->expr1->ts.type == BT_CLASS
11935 && !CLASS_DATA (code->expr2)->attr.dimension
11936 && !(gfc_expr_attr (code->expr1).proc_pointer
11937 && code->expr2->expr_type == EXPR_VARIABLE
11938 && code->expr2->symtree->n.sym->attr.flavor
11939 == FL_PROCEDURE))
11940 code->op = EXEC_ASSIGN;
11941 break;
11942 }
11943
11944 case EXEC_ARITHMETIC_IF:
11945 {
11946 gfc_expr *e = code->expr1;
11947
11948 gfc_resolve_expr (e);
11949 if (e->expr_type == EXPR_NULL)
11950 gfc_error ("Invalid NULL at %L", &e->where);
11951
11952 if (t && (e->rank > 0
11953 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
11954 gfc_error ("Arithmetic IF statement at %L requires a scalar "
11955 "REAL or INTEGER expression", &e->where);
11956
11957 resolve_branch (code->label1, code);
11958 resolve_branch (code->label2, code);
11959 resolve_branch (code->label3, code);
11960 }
11961 break;
11962
11963 case EXEC_IF:
11964 if (t && code->expr1 != NULL
11965 && (code->expr1->ts.type != BT_LOGICAL
11966 || code->expr1->rank != 0))
11967 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
11968 &code->expr1->where);
11969 break;
11970
11971 case EXEC_CALL:
11972 call:
11973 resolve_call (code);
11974 break;
11975
11976 case EXEC_COMPCALL:
11977 compcall:
11978 resolve_typebound_subroutine (code);
11979 break;
11980
11981 case EXEC_CALL_PPC:
11982 resolve_ppc_call (code);
11983 break;
11984
11985 case EXEC_SELECT:
11986 /* Select is complicated. Also, a SELECT construct could be
11987 a transformed computed GOTO. */
11988 resolve_select (code, false);
11989 break;
11990
11991 case EXEC_SELECT_TYPE:
11992 resolve_select_type (code, ns);
11993 break;
11994
11995 case EXEC_SELECT_RANK:
11996 resolve_select_rank (code, ns);
11997 break;
11998
11999 case EXEC_BLOCK:
12000 resolve_block_construct (code);
12001 break;
12002
12003 case EXEC_DO:
12004 if (code->ext.iterator != NULL)
12005 {
12006 gfc_iterator *iter = code->ext.iterator;
12007 if (gfc_resolve_iterator (iter, true, false))
12008 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
12009 true);
12010 }
12011 break;
12012
12013 case EXEC_DO_WHILE:
12014 if (code->expr1 == NULL)
12015 gfc_internal_error ("gfc_resolve_code(): No expression on "
12016 "DO WHILE");
12017 if (t
12018 && (code->expr1->rank != 0
12019 || code->expr1->ts.type != BT_LOGICAL))
12020 gfc_error ("Exit condition of DO WHILE loop at %L must be "
12021 "a scalar LOGICAL expression", &code->expr1->where);
12022 break;
12023
12024 case EXEC_ALLOCATE:
12025 if (t)
12026 resolve_allocate_deallocate (code, "ALLOCATE");
12027
12028 break;
12029
12030 case EXEC_DEALLOCATE:
12031 if (t)
12032 resolve_allocate_deallocate (code, "DEALLOCATE");
12033
12034 break;
12035
12036 case EXEC_OPEN:
12037 if (!gfc_resolve_open (code->ext.open, &code->loc))
12038 break;
12039
12040 resolve_branch (code->ext.open->err, code);
12041 break;
12042
12043 case EXEC_CLOSE:
12044 if (!gfc_resolve_close (code->ext.close, &code->loc))
12045 break;
12046
12047 resolve_branch (code->ext.close->err, code);
12048 break;
12049
12050 case EXEC_BACKSPACE:
12051 case EXEC_ENDFILE:
12052 case EXEC_REWIND:
12053 case EXEC_FLUSH:
12054 if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
12055 break;
12056
12057 resolve_branch (code->ext.filepos->err, code);
12058 break;
12059
12060 case EXEC_INQUIRE:
12061 if (!gfc_resolve_inquire (code->ext.inquire))
12062 break;
12063
12064 resolve_branch (code->ext.inquire->err, code);
12065 break;
12066
12067 case EXEC_IOLENGTH:
12068 gcc_assert (code->ext.inquire != NULL);
12069 if (!gfc_resolve_inquire (code->ext.inquire))
12070 break;
12071
12072 resolve_branch (code->ext.inquire->err, code);
12073 break;
12074
12075 case EXEC_WAIT:
12076 if (!gfc_resolve_wait (code->ext.wait))
12077 break;
12078
12079 resolve_branch (code->ext.wait->err, code);
12080 resolve_branch (code->ext.wait->end, code);
12081 resolve_branch (code->ext.wait->eor, code);
12082 break;
12083
12084 case EXEC_READ:
12085 case EXEC_WRITE:
12086 if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
12087 break;
12088
12089 resolve_branch (code->ext.dt->err, code);
12090 resolve_branch (code->ext.dt->end, code);
12091 resolve_branch (code->ext.dt->eor, code);
12092 break;
12093
12094 case EXEC_TRANSFER:
12095 resolve_transfer (code);
12096 break;
12097
12098 case EXEC_DO_CONCURRENT:
12099 case EXEC_FORALL:
12100 resolve_forall_iterators (code->ext.forall_iterator);
12101
12102 if (code->expr1 != NULL
12103 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
12104 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
12105 "expression", &code->expr1->where);
12106 break;
12107
12108 case EXEC_OACC_PARALLEL_LOOP:
12109 case EXEC_OACC_PARALLEL:
12110 case EXEC_OACC_KERNELS_LOOP:
12111 case EXEC_OACC_KERNELS:
12112 case EXEC_OACC_SERIAL_LOOP:
12113 case EXEC_OACC_SERIAL:
12114 case EXEC_OACC_DATA:
12115 case EXEC_OACC_HOST_DATA:
12116 case EXEC_OACC_LOOP:
12117 case EXEC_OACC_UPDATE:
12118 case EXEC_OACC_WAIT:
12119 case EXEC_OACC_CACHE:
12120 case EXEC_OACC_ENTER_DATA:
12121 case EXEC_OACC_EXIT_DATA:
12122 case EXEC_OACC_ATOMIC:
12123 case EXEC_OACC_DECLARE:
12124 gfc_resolve_oacc_directive (code, ns);
12125 break;
12126
12127 case EXEC_OMP_ATOMIC:
12128 case EXEC_OMP_BARRIER:
12129 case EXEC_OMP_CANCEL:
12130 case EXEC_OMP_CANCELLATION_POINT:
12131 case EXEC_OMP_CRITICAL:
12132 case EXEC_OMP_FLUSH:
12133 case EXEC_OMP_DISTRIBUTE:
12134 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
12135 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
12136 case EXEC_OMP_DISTRIBUTE_SIMD:
12137 case EXEC_OMP_DO:
12138 case EXEC_OMP_DO_SIMD:
12139 case EXEC_OMP_MASTER:
12140 case EXEC_OMP_ORDERED:
12141 case EXEC_OMP_SECTIONS:
12142 case EXEC_OMP_SIMD:
12143 case EXEC_OMP_SINGLE:
12144 case EXEC_OMP_TARGET:
12145 case EXEC_OMP_TARGET_DATA:
12146 case EXEC_OMP_TARGET_ENTER_DATA:
12147 case EXEC_OMP_TARGET_EXIT_DATA:
12148 case EXEC_OMP_TARGET_PARALLEL:
12149 case EXEC_OMP_TARGET_PARALLEL_DO:
12150 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
12151 case EXEC_OMP_TARGET_SIMD:
12152 case EXEC_OMP_TARGET_TEAMS:
12153 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
12154 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
12155 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12156 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
12157 case EXEC_OMP_TARGET_UPDATE:
12158 case EXEC_OMP_TASK:
12159 case EXEC_OMP_TASKGROUP:
12160 case EXEC_OMP_TASKLOOP:
12161 case EXEC_OMP_TASKLOOP_SIMD:
12162 case EXEC_OMP_TASKWAIT:
12163 case EXEC_OMP_TASKYIELD:
12164 case EXEC_OMP_TEAMS:
12165 case EXEC_OMP_TEAMS_DISTRIBUTE:
12166 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
12167 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12168 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
12169 case EXEC_OMP_WORKSHARE:
12170 gfc_resolve_omp_directive (code, ns);
12171 break;
12172
12173 case EXEC_OMP_PARALLEL:
12174 case EXEC_OMP_PARALLEL_DO:
12175 case EXEC_OMP_PARALLEL_DO_SIMD:
12176 case EXEC_OMP_PARALLEL_SECTIONS:
12177 case EXEC_OMP_PARALLEL_WORKSHARE:
12178 omp_workshare_save = omp_workshare_flag;
12179 omp_workshare_flag = 0;
12180 gfc_resolve_omp_directive (code, ns);
12181 omp_workshare_flag = omp_workshare_save;
12182 break;
12183
12184 default:
12185 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
12186 }
12187 }
12188
12189 cs_base = frame.prev;
12190 }
12191
12192
12193 /* Resolve initial values and make sure they are compatible with
12194 the variable. */
12195
12196 static void
12197 resolve_values (gfc_symbol *sym)
12198 {
12199 bool t;
12200
12201 if (sym->value == NULL)
12202 return;
12203
12204 if (sym->value->expr_type == EXPR_STRUCTURE)
12205 t= resolve_structure_cons (sym->value, 1);
12206 else
12207 t = gfc_resolve_expr (sym->value);
12208
12209 if (!t)
12210 return;
12211
12212 gfc_check_assign_symbol (sym, NULL, sym->value);
12213 }
12214
12215
12216 /* Verify any BIND(C) derived types in the namespace so we can report errors
12217 for them once, rather than for each variable declared of that type. */
12218
12219 static void
12220 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
12221 {
12222 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
12223 && derived_sym->attr.is_bind_c == 1)
12224 verify_bind_c_derived_type (derived_sym);
12225
12226 return;
12227 }
12228
12229
12230 /* Check the interfaces of DTIO procedures associated with derived
12231 type 'sym'. These procedures can either have typebound bindings or
12232 can appear in DTIO generic interfaces. */
12233
12234 static void
12235 gfc_verify_DTIO_procedures (gfc_symbol *sym)
12236 {
12237 if (!sym || sym->attr.flavor != FL_DERIVED)
12238 return;
12239
12240 gfc_check_dtio_interfaces (sym);
12241
12242 return;
12243 }
12244
12245 /* Verify that any binding labels used in a given namespace do not collide
12246 with the names or binding labels of any global symbols. Multiple INTERFACE
12247 for the same procedure are permitted. */
12248
12249 static void
12250 gfc_verify_binding_labels (gfc_symbol *sym)
12251 {
12252 gfc_gsymbol *gsym;
12253 const char *module;
12254
12255 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
12256 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
12257 return;
12258
12259 gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
12260
12261 if (sym->module)
12262 module = sym->module;
12263 else if (sym->ns && sym->ns->proc_name
12264 && sym->ns->proc_name->attr.flavor == FL_MODULE)
12265 module = sym->ns->proc_name->name;
12266 else if (sym->ns && sym->ns->parent
12267 && sym->ns && sym->ns->parent->proc_name
12268 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
12269 module = sym->ns->parent->proc_name->name;
12270 else
12271 module = NULL;
12272
12273 if (!gsym
12274 || (!gsym->defined
12275 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
12276 {
12277 if (!gsym)
12278 gsym = gfc_get_gsymbol (sym->binding_label, true);
12279 gsym->where = sym->declared_at;
12280 gsym->sym_name = sym->name;
12281 gsym->binding_label = sym->binding_label;
12282 gsym->ns = sym->ns;
12283 gsym->mod_name = module;
12284 if (sym->attr.function)
12285 gsym->type = GSYM_FUNCTION;
12286 else if (sym->attr.subroutine)
12287 gsym->type = GSYM_SUBROUTINE;
12288 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
12289 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
12290 return;
12291 }
12292
12293 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
12294 {
12295 gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
12296 "identifier as entity at %L", sym->name,
12297 sym->binding_label, &sym->declared_at, &gsym->where);
12298 /* Clear the binding label to prevent checking multiple times. */
12299 sym->binding_label = NULL;
12300 return;
12301 }
12302
12303 if (sym->attr.flavor == FL_VARIABLE && module
12304 && (strcmp (module, gsym->mod_name) != 0
12305 || strcmp (sym->name, gsym->sym_name) != 0))
12306 {
12307 /* This can only happen if the variable is defined in a module - if it
12308 isn't the same module, reject it. */
12309 gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
12310 "uses the same global identifier as entity at %L from module %qs",
12311 sym->name, module, sym->binding_label,
12312 &sym->declared_at, &gsym->where, gsym->mod_name);
12313 sym->binding_label = NULL;
12314 return;
12315 }
12316
12317 if ((sym->attr.function || sym->attr.subroutine)
12318 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
12319 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
12320 && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
12321 && (module != gsym->mod_name
12322 || strcmp (gsym->sym_name, sym->name) != 0
12323 || (module && strcmp (module, gsym->mod_name) != 0)))
12324 {
12325 /* Print an error if the procedure is defined multiple times; we have to
12326 exclude references to the same procedure via module association or
12327 multiple checks for the same procedure. */
12328 gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
12329 "global identifier as entity at %L", sym->name,
12330 sym->binding_label, &sym->declared_at, &gsym->where);
12331 sym->binding_label = NULL;
12332 }
12333 }
12334
12335
12336 /* Resolve an index expression. */
12337
12338 static bool
12339 resolve_index_expr (gfc_expr *e)
12340 {
12341 if (!gfc_resolve_expr (e))
12342 return false;
12343
12344 if (!gfc_simplify_expr (e, 0))
12345 return false;
12346
12347 if (!gfc_specification_expr (e))
12348 return false;
12349
12350 return true;
12351 }
12352
12353
12354 /* Resolve a charlen structure. */
12355
12356 static bool
12357 resolve_charlen (gfc_charlen *cl)
12358 {
12359 int k;
12360 bool saved_specification_expr;
12361
12362 if (cl->resolved)
12363 return true;
12364
12365 cl->resolved = 1;
12366 saved_specification_expr = specification_expr;
12367 specification_expr = true;
12368
12369 if (cl->length_from_typespec)
12370 {
12371 if (!gfc_resolve_expr (cl->length))
12372 {
12373 specification_expr = saved_specification_expr;
12374 return false;
12375 }
12376
12377 if (!gfc_simplify_expr (cl->length, 0))
12378 {
12379 specification_expr = saved_specification_expr;
12380 return false;
12381 }
12382
12383 /* cl->length has been resolved. It should have an integer type. */
12384 if (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0)
12385 {
12386 gfc_error ("Scalar INTEGER expression expected at %L",
12387 &cl->length->where);
12388 return false;
12389 }
12390 }
12391 else
12392 {
12393 if (!resolve_index_expr (cl->length))
12394 {
12395 specification_expr = saved_specification_expr;
12396 return false;
12397 }
12398 }
12399
12400 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
12401 a negative value, the length of character entities declared is zero. */
12402 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
12403 && mpz_sgn (cl->length->value.integer) < 0)
12404 gfc_replace_expr (cl->length,
12405 gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
12406
12407 /* Check that the character length is not too large. */
12408 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
12409 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
12410 && cl->length->ts.type == BT_INTEGER
12411 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
12412 {
12413 gfc_error ("String length at %L is too large", &cl->length->where);
12414 specification_expr = saved_specification_expr;
12415 return false;
12416 }
12417
12418 specification_expr = saved_specification_expr;
12419 return true;
12420 }
12421
12422
12423 /* Test for non-constant shape arrays. */
12424
12425 static bool
12426 is_non_constant_shape_array (gfc_symbol *sym)
12427 {
12428 gfc_expr *e;
12429 int i;
12430 bool not_constant;
12431
12432 not_constant = false;
12433 if (sym->as != NULL)
12434 {
12435 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
12436 has not been simplified; parameter array references. Do the
12437 simplification now. */
12438 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
12439 {
12440 if (i == GFC_MAX_DIMENSIONS)
12441 break;
12442
12443 e = sym->as->lower[i];
12444 if (e && (!resolve_index_expr(e)
12445 || !gfc_is_constant_expr (e)))
12446 not_constant = true;
12447 e = sym->as->upper[i];
12448 if (e && (!resolve_index_expr(e)
12449 || !gfc_is_constant_expr (e)))
12450 not_constant = true;
12451 }
12452 }
12453 return not_constant;
12454 }
12455
12456 /* Given a symbol and an initialization expression, add code to initialize
12457 the symbol to the function entry. */
12458 static void
12459 build_init_assign (gfc_symbol *sym, gfc_expr *init)
12460 {
12461 gfc_expr *lval;
12462 gfc_code *init_st;
12463 gfc_namespace *ns = sym->ns;
12464
12465 /* Search for the function namespace if this is a contained
12466 function without an explicit result. */
12467 if (sym->attr.function && sym == sym->result
12468 && sym->name != sym->ns->proc_name->name)
12469 {
12470 ns = ns->contained;
12471 for (;ns; ns = ns->sibling)
12472 if (strcmp (ns->proc_name->name, sym->name) == 0)
12473 break;
12474 }
12475
12476 if (ns == NULL)
12477 {
12478 gfc_free_expr (init);
12479 return;
12480 }
12481
12482 /* Build an l-value expression for the result. */
12483 lval = gfc_lval_expr_from_sym (sym);
12484
12485 /* Add the code at scope entry. */
12486 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
12487 init_st->next = ns->code;
12488 ns->code = init_st;
12489
12490 /* Assign the default initializer to the l-value. */
12491 init_st->loc = sym->declared_at;
12492 init_st->expr1 = lval;
12493 init_st->expr2 = init;
12494 }
12495
12496
12497 /* Whether or not we can generate a default initializer for a symbol. */
12498
12499 static bool
12500 can_generate_init (gfc_symbol *sym)
12501 {
12502 symbol_attribute *a;
12503 if (!sym)
12504 return false;
12505 a = &sym->attr;
12506
12507 /* These symbols should never have a default initialization. */
12508 return !(
12509 a->allocatable
12510 || a->external
12511 || a->pointer
12512 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
12513 && (CLASS_DATA (sym)->attr.class_pointer
12514 || CLASS_DATA (sym)->attr.proc_pointer))
12515 || a->in_equivalence
12516 || a->in_common
12517 || a->data
12518 || sym->module
12519 || a->cray_pointee
12520 || a->cray_pointer
12521 || sym->assoc
12522 || (!a->referenced && !a->result)
12523 || (a->dummy && a->intent != INTENT_OUT)
12524 || (a->function && sym != sym->result)
12525 );
12526 }
12527
12528
12529 /* Assign the default initializer to a derived type variable or result. */
12530
12531 static void
12532 apply_default_init (gfc_symbol *sym)
12533 {
12534 gfc_expr *init = NULL;
12535
12536 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
12537 return;
12538
12539 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
12540 init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
12541
12542 if (init == NULL && sym->ts.type != BT_CLASS)
12543 return;
12544
12545 build_init_assign (sym, init);
12546 sym->attr.referenced = 1;
12547 }
12548
12549
12550 /* Build an initializer for a local. Returns null if the symbol should not have
12551 a default initialization. */
12552
12553 static gfc_expr *
12554 build_default_init_expr (gfc_symbol *sym)
12555 {
12556 /* These symbols should never have a default initialization. */
12557 if (sym->attr.allocatable
12558 || sym->attr.external
12559 || sym->attr.dummy
12560 || sym->attr.pointer
12561 || sym->attr.in_equivalence
12562 || sym->attr.in_common
12563 || sym->attr.data
12564 || sym->module
12565 || sym->attr.cray_pointee
12566 || sym->attr.cray_pointer
12567 || sym->assoc)
12568 return NULL;
12569
12570 /* Get the appropriate init expression. */
12571 return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
12572 }
12573
12574 /* Add an initialization expression to a local variable. */
12575 static void
12576 apply_default_init_local (gfc_symbol *sym)
12577 {
12578 gfc_expr *init = NULL;
12579
12580 /* The symbol should be a variable or a function return value. */
12581 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
12582 || (sym->attr.function && sym->result != sym))
12583 return;
12584
12585 /* Try to build the initializer expression. If we can't initialize
12586 this symbol, then init will be NULL. */
12587 init = build_default_init_expr (sym);
12588 if (init == NULL)
12589 return;
12590
12591 /* For saved variables, we don't want to add an initializer at function
12592 entry, so we just add a static initializer. Note that automatic variables
12593 are stack allocated even with -fno-automatic; we have also to exclude
12594 result variable, which are also nonstatic. */
12595 if (!sym->attr.automatic
12596 && (sym->attr.save || sym->ns->save_all
12597 || (flag_max_stack_var_size == 0 && !sym->attr.result
12598 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
12599 && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
12600 {
12601 /* Don't clobber an existing initializer! */
12602 gcc_assert (sym->value == NULL);
12603 sym->value = init;
12604 return;
12605 }
12606
12607 build_init_assign (sym, init);
12608 }
12609
12610
12611 /* Resolution of common features of flavors variable and procedure. */
12612
12613 static bool
12614 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
12615 {
12616 gfc_array_spec *as;
12617
12618 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
12619 as = CLASS_DATA (sym)->as;
12620 else
12621 as = sym->as;
12622
12623 /* Constraints on deferred shape variable. */
12624 if (as == NULL || as->type != AS_DEFERRED)
12625 {
12626 bool pointer, allocatable, dimension;
12627
12628 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
12629 {
12630 pointer = CLASS_DATA (sym)->attr.class_pointer;
12631 allocatable = CLASS_DATA (sym)->attr.allocatable;
12632 dimension = CLASS_DATA (sym)->attr.dimension;
12633 }
12634 else
12635 {
12636 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
12637 allocatable = sym->attr.allocatable;
12638 dimension = sym->attr.dimension;
12639 }
12640
12641 if (allocatable)
12642 {
12643 if (dimension && as->type != AS_ASSUMED_RANK)
12644 {
12645 gfc_error ("Allocatable array %qs at %L must have a deferred "
12646 "shape or assumed rank", sym->name, &sym->declared_at);
12647 return false;
12648 }
12649 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
12650 "%qs at %L may not be ALLOCATABLE",
12651 sym->name, &sym->declared_at))
12652 return false;
12653 }
12654
12655 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
12656 {
12657 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
12658 "assumed rank", sym->name, &sym->declared_at);
12659 sym->error = 1;
12660 return false;
12661 }
12662 }
12663 else
12664 {
12665 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
12666 && sym->ts.type != BT_CLASS && !sym->assoc)
12667 {
12668 gfc_error ("Array %qs at %L cannot have a deferred shape",
12669 sym->name, &sym->declared_at);
12670 return false;
12671 }
12672 }
12673
12674 /* Constraints on polymorphic variables. */
12675 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
12676 {
12677 /* F03:C502. */
12678 if (sym->attr.class_ok
12679 && !sym->attr.select_type_temporary
12680 && !UNLIMITED_POLY (sym)
12681 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
12682 {
12683 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
12684 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
12685 &sym->declared_at);
12686 return false;
12687 }
12688
12689 /* F03:C509. */
12690 /* Assume that use associated symbols were checked in the module ns.
12691 Class-variables that are associate-names are also something special
12692 and excepted from the test. */
12693 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
12694 {
12695 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
12696 "or pointer", sym->name, &sym->declared_at);
12697 return false;
12698 }
12699 }
12700
12701 return true;
12702 }
12703
12704
12705 /* Additional checks for symbols with flavor variable and derived
12706 type. To be called from resolve_fl_variable. */
12707
12708 static bool
12709 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
12710 {
12711 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
12712
12713 /* Check to see if a derived type is blocked from being host
12714 associated by the presence of another class I symbol in the same
12715 namespace. 14.6.1.3 of the standard and the discussion on
12716 comp.lang.fortran. */
12717 if (sym->ns != sym->ts.u.derived->ns
12718 && !sym->ts.u.derived->attr.use_assoc
12719 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
12720 {
12721 gfc_symbol *s;
12722 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
12723 if (s && s->attr.generic)
12724 s = gfc_find_dt_in_generic (s);
12725 if (s && !gfc_fl_struct (s->attr.flavor))
12726 {
12727 gfc_error ("The type %qs cannot be host associated at %L "
12728 "because it is blocked by an incompatible object "
12729 "of the same name declared at %L",
12730 sym->ts.u.derived->name, &sym->declared_at,
12731 &s->declared_at);
12732 return false;
12733 }
12734 }
12735
12736 /* 4th constraint in section 11.3: "If an object of a type for which
12737 component-initialization is specified (R429) appears in the
12738 specification-part of a module and does not have the ALLOCATABLE
12739 or POINTER attribute, the object shall have the SAVE attribute."
12740
12741 The check for initializers is performed with
12742 gfc_has_default_initializer because gfc_default_initializer generates
12743 a hidden default for allocatable components. */
12744 if (!(sym->value || no_init_flag) && sym->ns->proc_name
12745 && sym->ns->proc_name->attr.flavor == FL_MODULE
12746 && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
12747 && !sym->attr.pointer && !sym->attr.allocatable
12748 && gfc_has_default_initializer (sym->ts.u.derived)
12749 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
12750 "%qs at %L, needed due to the default "
12751 "initialization", sym->name, &sym->declared_at))
12752 return false;
12753
12754 /* Assign default initializer. */
12755 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
12756 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
12757 sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
12758
12759 return true;
12760 }
12761
12762
12763 /* F2008, C402 (R401): A colon shall not be used as a type-param-value
12764 except in the declaration of an entity or component that has the POINTER
12765 or ALLOCATABLE attribute. */
12766
12767 static bool
12768 deferred_requirements (gfc_symbol *sym)
12769 {
12770 if (sym->ts.deferred
12771 && !(sym->attr.pointer
12772 || sym->attr.allocatable
12773 || sym->attr.associate_var
12774 || sym->attr.omp_udr_artificial_var))
12775 {
12776 /* If a function has a result variable, only check the variable. */
12777 if (sym->result && sym->name != sym->result->name)
12778 return true;
12779
12780 gfc_error ("Entity %qs at %L has a deferred type parameter and "
12781 "requires either the POINTER or ALLOCATABLE attribute",
12782 sym->name, &sym->declared_at);
12783 return false;
12784 }
12785 return true;
12786 }
12787
12788
12789 /* Resolve symbols with flavor variable. */
12790
12791 static bool
12792 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
12793 {
12794 const char *auto_save_msg = "Automatic object %qs at %L cannot have the "
12795 "SAVE attribute";
12796
12797 if (!resolve_fl_var_and_proc (sym, mp_flag))
12798 return false;
12799
12800 /* Set this flag to check that variables are parameters of all entries.
12801 This check is effected by the call to gfc_resolve_expr through
12802 is_non_constant_shape_array. */
12803 bool saved_specification_expr = specification_expr;
12804 specification_expr = true;
12805
12806 if (sym->ns->proc_name
12807 && (sym->ns->proc_name->attr.flavor == FL_MODULE
12808 || sym->ns->proc_name->attr.is_main_program)
12809 && !sym->attr.use_assoc
12810 && !sym->attr.allocatable
12811 && !sym->attr.pointer
12812 && is_non_constant_shape_array (sym))
12813 {
12814 /* F08:C541. The shape of an array defined in a main program or module
12815 * needs to be constant. */
12816 gfc_error ("The module or main program array %qs at %L must "
12817 "have constant shape", sym->name, &sym->declared_at);
12818 specification_expr = saved_specification_expr;
12819 return false;
12820 }
12821
12822 /* Constraints on deferred type parameter. */
12823 if (!deferred_requirements (sym))
12824 return false;
12825
12826 if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
12827 {
12828 /* Make sure that character string variables with assumed length are
12829 dummy arguments. */
12830 gfc_expr *e = NULL;
12831
12832 if (sym->ts.u.cl)
12833 e = sym->ts.u.cl->length;
12834 else
12835 return false;
12836
12837 if (e == NULL && !sym->attr.dummy && !sym->attr.result
12838 && !sym->ts.deferred && !sym->attr.select_type_temporary
12839 && !sym->attr.omp_udr_artificial_var)
12840 {
12841 gfc_error ("Entity with assumed character length at %L must be a "
12842 "dummy argument or a PARAMETER", &sym->declared_at);
12843 specification_expr = saved_specification_expr;
12844 return false;
12845 }
12846
12847 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
12848 {
12849 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
12850 specification_expr = saved_specification_expr;
12851 return false;
12852 }
12853
12854 if (!gfc_is_constant_expr (e)
12855 && !(e->expr_type == EXPR_VARIABLE
12856 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
12857 {
12858 if (!sym->attr.use_assoc && sym->ns->proc_name
12859 && (sym->ns->proc_name->attr.flavor == FL_MODULE
12860 || sym->ns->proc_name->attr.is_main_program))
12861 {
12862 gfc_error ("%qs at %L must have constant character length "
12863 "in this context", sym->name, &sym->declared_at);
12864 specification_expr = saved_specification_expr;
12865 return false;
12866 }
12867 if (sym->attr.in_common)
12868 {
12869 gfc_error ("COMMON variable %qs at %L must have constant "
12870 "character length", sym->name, &sym->declared_at);
12871 specification_expr = saved_specification_expr;
12872 return false;
12873 }
12874 }
12875 }
12876
12877 if (sym->value == NULL && sym->attr.referenced)
12878 apply_default_init_local (sym); /* Try to apply a default initialization. */
12879
12880 /* Determine if the symbol may not have an initializer. */
12881 int no_init_flag = 0, automatic_flag = 0;
12882 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
12883 || sym->attr.intrinsic || sym->attr.result)
12884 no_init_flag = 1;
12885 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
12886 && is_non_constant_shape_array (sym))
12887 {
12888 no_init_flag = automatic_flag = 1;
12889
12890 /* Also, they must not have the SAVE attribute.
12891 SAVE_IMPLICIT is checked below. */
12892 if (sym->as && sym->attr.codimension)
12893 {
12894 int corank = sym->as->corank;
12895 sym->as->corank = 0;
12896 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
12897 sym->as->corank = corank;
12898 }
12899 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
12900 {
12901 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
12902 specification_expr = saved_specification_expr;
12903 return false;
12904 }
12905 }
12906
12907 /* Ensure that any initializer is simplified. */
12908 if (sym->value)
12909 gfc_simplify_expr (sym->value, 1);
12910
12911 /* Reject illegal initializers. */
12912 if (!sym->mark && sym->value)
12913 {
12914 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
12915 && CLASS_DATA (sym)->attr.allocatable))
12916 gfc_error ("Allocatable %qs at %L cannot have an initializer",
12917 sym->name, &sym->declared_at);
12918 else if (sym->attr.external)
12919 gfc_error ("External %qs at %L cannot have an initializer",
12920 sym->name, &sym->declared_at);
12921 else if (sym->attr.dummy
12922 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
12923 gfc_error ("Dummy %qs at %L cannot have an initializer",
12924 sym->name, &sym->declared_at);
12925 else if (sym->attr.intrinsic)
12926 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
12927 sym->name, &sym->declared_at);
12928 else if (sym->attr.result)
12929 gfc_error ("Function result %qs at %L cannot have an initializer",
12930 sym->name, &sym->declared_at);
12931 else if (automatic_flag)
12932 gfc_error ("Automatic array %qs at %L cannot have an initializer",
12933 sym->name, &sym->declared_at);
12934 else
12935 goto no_init_error;
12936 specification_expr = saved_specification_expr;
12937 return false;
12938 }
12939
12940 no_init_error:
12941 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
12942 {
12943 bool res = resolve_fl_variable_derived (sym, no_init_flag);
12944 specification_expr = saved_specification_expr;
12945 return res;
12946 }
12947
12948 specification_expr = saved_specification_expr;
12949 return true;
12950 }
12951
12952
12953 /* Compare the dummy characteristics of a module procedure interface
12954 declaration with the corresponding declaration in a submodule. */
12955 static gfc_formal_arglist *new_formal;
12956 static char errmsg[200];
12957
12958 static void
12959 compare_fsyms (gfc_symbol *sym)
12960 {
12961 gfc_symbol *fsym;
12962
12963 if (sym == NULL || new_formal == NULL)
12964 return;
12965
12966 fsym = new_formal->sym;
12967
12968 if (sym == fsym)
12969 return;
12970
12971 if (strcmp (sym->name, fsym->name) == 0)
12972 {
12973 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
12974 gfc_error ("%s at %L", errmsg, &fsym->declared_at);
12975 }
12976 }
12977
12978
12979 /* Resolve a procedure. */
12980
12981 static bool
12982 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
12983 {
12984 gfc_formal_arglist *arg;
12985
12986 if (sym->attr.function
12987 && !resolve_fl_var_and_proc (sym, mp_flag))
12988 return false;
12989
12990 /* Constraints on deferred type parameter. */
12991 if (!deferred_requirements (sym))
12992 return false;
12993
12994 if (sym->ts.type == BT_CHARACTER)
12995 {
12996 gfc_charlen *cl = sym->ts.u.cl;
12997
12998 if (cl && cl->length && gfc_is_constant_expr (cl->length)
12999 && !resolve_charlen (cl))
13000 return false;
13001
13002 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
13003 && sym->attr.proc == PROC_ST_FUNCTION)
13004 {
13005 gfc_error ("Character-valued statement function %qs at %L must "
13006 "have constant length", sym->name, &sym->declared_at);
13007 return false;
13008 }
13009 }
13010
13011 /* Ensure that derived type for are not of a private type. Internal
13012 module procedures are excluded by 2.2.3.3 - i.e., they are not
13013 externally accessible and can access all the objects accessible in
13014 the host. */
13015 if (!(sym->ns->parent && sym->ns->parent->proc_name
13016 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
13017 && gfc_check_symbol_access (sym))
13018 {
13019 gfc_interface *iface;
13020
13021 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
13022 {
13023 if (arg->sym
13024 && arg->sym->ts.type == BT_DERIVED
13025 && arg->sym->ts.u.derived
13026 && !arg->sym->ts.u.derived->attr.use_assoc
13027 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
13028 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
13029 "and cannot be a dummy argument"
13030 " of %qs, which is PUBLIC at %L",
13031 arg->sym->name, sym->name,
13032 &sym->declared_at))
13033 {
13034 /* Stop this message from recurring. */
13035 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
13036 return false;
13037 }
13038 }
13039
13040 /* PUBLIC interfaces may expose PRIVATE procedures that take types
13041 PRIVATE to the containing module. */
13042 for (iface = sym->generic; iface; iface = iface->next)
13043 {
13044 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
13045 {
13046 if (arg->sym
13047 && arg->sym->ts.type == BT_DERIVED
13048 && !arg->sym->ts.u.derived->attr.use_assoc
13049 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
13050 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
13051 "PUBLIC interface %qs at %L "
13052 "takes dummy arguments of %qs which "
13053 "is PRIVATE", iface->sym->name,
13054 sym->name, &iface->sym->declared_at,
13055 gfc_typename(&arg->sym->ts)))
13056 {
13057 /* Stop this message from recurring. */
13058 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
13059 return false;
13060 }
13061 }
13062 }
13063 }
13064
13065 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
13066 && !sym->attr.proc_pointer)
13067 {
13068 gfc_error ("Function %qs at %L cannot have an initializer",
13069 sym->name, &sym->declared_at);
13070
13071 /* Make sure no second error is issued for this. */
13072 sym->value->error = 1;
13073 return false;
13074 }
13075
13076 /* An external symbol may not have an initializer because it is taken to be
13077 a procedure. Exception: Procedure Pointers. */
13078 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
13079 {
13080 gfc_error ("External object %qs at %L may not have an initializer",
13081 sym->name, &sym->declared_at);
13082 return false;
13083 }
13084
13085 /* An elemental function is required to return a scalar 12.7.1 */
13086 if (sym->attr.elemental && sym->attr.function
13087 && (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)))
13088 {
13089 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
13090 "result", sym->name, &sym->declared_at);
13091 /* Reset so that the error only occurs once. */
13092 sym->attr.elemental = 0;
13093 return false;
13094 }
13095
13096 if (sym->attr.proc == PROC_ST_FUNCTION
13097 && (sym->attr.allocatable || sym->attr.pointer))
13098 {
13099 gfc_error ("Statement function %qs at %L may not have pointer or "
13100 "allocatable attribute", sym->name, &sym->declared_at);
13101 return false;
13102 }
13103
13104 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
13105 char-len-param shall not be array-valued, pointer-valued, recursive
13106 or pure. ....snip... A character value of * may only be used in the
13107 following ways: (i) Dummy arg of procedure - dummy associates with
13108 actual length; (ii) To declare a named constant; or (iii) External
13109 function - but length must be declared in calling scoping unit. */
13110 if (sym->attr.function
13111 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
13112 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
13113 {
13114 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
13115 || (sym->attr.recursive) || (sym->attr.pure))
13116 {
13117 if (sym->as && sym->as->rank)
13118 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13119 "array-valued", sym->name, &sym->declared_at);
13120
13121 if (sym->attr.pointer)
13122 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13123 "pointer-valued", sym->name, &sym->declared_at);
13124
13125 if (sym->attr.pure)
13126 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13127 "pure", sym->name, &sym->declared_at);
13128
13129 if (sym->attr.recursive)
13130 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13131 "recursive", sym->name, &sym->declared_at);
13132
13133 return false;
13134 }
13135
13136 /* Appendix B.2 of the standard. Contained functions give an
13137 error anyway. Deferred character length is an F2003 feature.
13138 Don't warn on intrinsic conversion functions, which start
13139 with two underscores. */
13140 if (!sym->attr.contained && !sym->ts.deferred
13141 && (sym->name[0] != '_' || sym->name[1] != '_'))
13142 gfc_notify_std (GFC_STD_F95_OBS,
13143 "CHARACTER(*) function %qs at %L",
13144 sym->name, &sym->declared_at);
13145 }
13146
13147 /* F2008, C1218. */
13148 if (sym->attr.elemental)
13149 {
13150 if (sym->attr.proc_pointer)
13151 {
13152 const char* name = (sym->attr.result ? sym->ns->proc_name->name
13153 : sym->name);
13154 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
13155 name, &sym->declared_at);
13156 return false;
13157 }
13158 if (sym->attr.dummy)
13159 {
13160 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
13161 sym->name, &sym->declared_at);
13162 return false;
13163 }
13164 }
13165
13166 /* F2018, C15100: "The result of an elemental function shall be scalar,
13167 and shall not have the POINTER or ALLOCATABLE attribute." The scalar
13168 pointer is tested and caught elsewhere. */
13169 if (sym->attr.elemental && sym->result
13170 && (sym->result->attr.allocatable || sym->result->attr.pointer))
13171 {
13172 gfc_error ("Function result variable %qs at %L of elemental "
13173 "function %qs shall not have an ALLOCATABLE or POINTER "
13174 "attribute", sym->result->name,
13175 &sym->result->declared_at, sym->name);
13176 return false;
13177 }
13178
13179 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
13180 {
13181 gfc_formal_arglist *curr_arg;
13182 int has_non_interop_arg = 0;
13183
13184 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13185 sym->common_block))
13186 {
13187 /* Clear these to prevent looking at them again if there was an
13188 error. */
13189 sym->attr.is_bind_c = 0;
13190 sym->attr.is_c_interop = 0;
13191 sym->ts.is_c_interop = 0;
13192 }
13193 else
13194 {
13195 /* So far, no errors have been found. */
13196 sym->attr.is_c_interop = 1;
13197 sym->ts.is_c_interop = 1;
13198 }
13199
13200 curr_arg = gfc_sym_get_dummy_args (sym);
13201 while (curr_arg != NULL)
13202 {
13203 /* Skip implicitly typed dummy args here. */
13204 if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
13205 if (!gfc_verify_c_interop_param (curr_arg->sym))
13206 /* If something is found to fail, record the fact so we
13207 can mark the symbol for the procedure as not being
13208 BIND(C) to try and prevent multiple errors being
13209 reported. */
13210 has_non_interop_arg = 1;
13211
13212 curr_arg = curr_arg->next;
13213 }
13214
13215 /* See if any of the arguments were not interoperable and if so, clear
13216 the procedure symbol to prevent duplicate error messages. */
13217 if (has_non_interop_arg != 0)
13218 {
13219 sym->attr.is_c_interop = 0;
13220 sym->ts.is_c_interop = 0;
13221 sym->attr.is_bind_c = 0;
13222 }
13223 }
13224
13225 if (!sym->attr.proc_pointer)
13226 {
13227 if (sym->attr.save == SAVE_EXPLICIT)
13228 {
13229 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
13230 "in %qs at %L", sym->name, &sym->declared_at);
13231 return false;
13232 }
13233 if (sym->attr.intent)
13234 {
13235 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
13236 "in %qs at %L", sym->name, &sym->declared_at);
13237 return false;
13238 }
13239 if (sym->attr.subroutine && sym->attr.result)
13240 {
13241 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
13242 "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
13243 return false;
13244 }
13245 if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
13246 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
13247 || sym->attr.contained))
13248 {
13249 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
13250 "in %qs at %L", sym->name, &sym->declared_at);
13251 return false;
13252 }
13253 if (strcmp ("ppr@", sym->name) == 0)
13254 {
13255 gfc_error ("Procedure pointer result %qs at %L "
13256 "is missing the pointer attribute",
13257 sym->ns->proc_name->name, &sym->declared_at);
13258 return false;
13259 }
13260 }
13261
13262 /* Assume that a procedure whose body is not known has references
13263 to external arrays. */
13264 if (sym->attr.if_source != IFSRC_DECL)
13265 sym->attr.array_outer_dependency = 1;
13266
13267 /* Compare the characteristics of a module procedure with the
13268 interface declaration. Ideally this would be done with
13269 gfc_compare_interfaces but, at present, the formal interface
13270 cannot be copied to the ts.interface. */
13271 if (sym->attr.module_procedure
13272 && sym->attr.if_source == IFSRC_DECL)
13273 {
13274 gfc_symbol *iface;
13275 char name[2*GFC_MAX_SYMBOL_LEN + 1];
13276 char *module_name;
13277 char *submodule_name;
13278 strcpy (name, sym->ns->proc_name->name);
13279 module_name = strtok (name, ".");
13280 submodule_name = strtok (NULL, ".");
13281
13282 iface = sym->tlink;
13283 sym->tlink = NULL;
13284
13285 /* Make sure that the result uses the correct charlen for deferred
13286 length results. */
13287 if (iface && sym->result
13288 && iface->ts.type == BT_CHARACTER
13289 && iface->ts.deferred)
13290 sym->result->ts.u.cl = iface->ts.u.cl;
13291
13292 if (iface == NULL)
13293 goto check_formal;
13294
13295 /* Check the procedure characteristics. */
13296 if (sym->attr.elemental != iface->attr.elemental)
13297 {
13298 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
13299 "PROCEDURE at %L and its interface in %s",
13300 &sym->declared_at, module_name);
13301 return false;
13302 }
13303
13304 if (sym->attr.pure != iface->attr.pure)
13305 {
13306 gfc_error ("Mismatch in PURE attribute between MODULE "
13307 "PROCEDURE at %L and its interface in %s",
13308 &sym->declared_at, module_name);
13309 return false;
13310 }
13311
13312 if (sym->attr.recursive != iface->attr.recursive)
13313 {
13314 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
13315 "PROCEDURE at %L and its interface in %s",
13316 &sym->declared_at, module_name);
13317 return false;
13318 }
13319
13320 /* Check the result characteristics. */
13321 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
13322 {
13323 gfc_error ("%s between the MODULE PROCEDURE declaration "
13324 "in MODULE %qs and the declaration at %L in "
13325 "(SUB)MODULE %qs",
13326 errmsg, module_name, &sym->declared_at,
13327 submodule_name ? submodule_name : module_name);
13328 return false;
13329 }
13330
13331 check_formal:
13332 /* Check the characteristics of the formal arguments. */
13333 if (sym->formal && sym->formal_ns)
13334 {
13335 for (arg = sym->formal; arg && arg->sym; arg = arg->next)
13336 {
13337 new_formal = arg;
13338 gfc_traverse_ns (sym->formal_ns, compare_fsyms);
13339 }
13340 }
13341 }
13342 return true;
13343 }
13344
13345
13346 /* Resolve a list of finalizer procedures. That is, after they have hopefully
13347 been defined and we now know their defined arguments, check that they fulfill
13348 the requirements of the standard for procedures used as finalizers. */
13349
13350 static bool
13351 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
13352 {
13353 gfc_finalizer* list;
13354 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
13355 bool result = true;
13356 bool seen_scalar = false;
13357 gfc_symbol *vtab;
13358 gfc_component *c;
13359 gfc_symbol *parent = gfc_get_derived_super_type (derived);
13360
13361 if (parent)
13362 gfc_resolve_finalizers (parent, finalizable);
13363
13364 /* Ensure that derived-type components have a their finalizers resolved. */
13365 bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
13366 for (c = derived->components; c; c = c->next)
13367 if (c->ts.type == BT_DERIVED
13368 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
13369 {
13370 bool has_final2 = false;
13371 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
13372 return false; /* Error. */
13373 has_final = has_final || has_final2;
13374 }
13375 /* Return early if not finalizable. */
13376 if (!has_final)
13377 {
13378 if (finalizable)
13379 *finalizable = false;
13380 return true;
13381 }
13382
13383 /* Walk over the list of finalizer-procedures, check them, and if any one
13384 does not fit in with the standard's definition, print an error and remove
13385 it from the list. */
13386 prev_link = &derived->f2k_derived->finalizers;
13387 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
13388 {
13389 gfc_formal_arglist *dummy_args;
13390 gfc_symbol* arg;
13391 gfc_finalizer* i;
13392 int my_rank;
13393
13394 /* Skip this finalizer if we already resolved it. */
13395 if (list->proc_tree)
13396 {
13397 if (list->proc_tree->n.sym->formal->sym->as == NULL
13398 || list->proc_tree->n.sym->formal->sym->as->rank == 0)
13399 seen_scalar = true;
13400 prev_link = &(list->next);
13401 continue;
13402 }
13403
13404 /* Check this exists and is a SUBROUTINE. */
13405 if (!list->proc_sym->attr.subroutine)
13406 {
13407 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
13408 list->proc_sym->name, &list->where);
13409 goto error;
13410 }
13411
13412 /* We should have exactly one argument. */
13413 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
13414 if (!dummy_args || dummy_args->next)
13415 {
13416 gfc_error ("FINAL procedure at %L must have exactly one argument",
13417 &list->where);
13418 goto error;
13419 }
13420 arg = dummy_args->sym;
13421
13422 /* This argument must be of our type. */
13423 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
13424 {
13425 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
13426 &arg->declared_at, derived->name);
13427 goto error;
13428 }
13429
13430 /* It must neither be a pointer nor allocatable nor optional. */
13431 if (arg->attr.pointer)
13432 {
13433 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
13434 &arg->declared_at);
13435 goto error;
13436 }
13437 if (arg->attr.allocatable)
13438 {
13439 gfc_error ("Argument of FINAL procedure at %L must not be"
13440 " ALLOCATABLE", &arg->declared_at);
13441 goto error;
13442 }
13443 if (arg->attr.optional)
13444 {
13445 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
13446 &arg->declared_at);
13447 goto error;
13448 }
13449
13450 /* It must not be INTENT(OUT). */
13451 if (arg->attr.intent == INTENT_OUT)
13452 {
13453 gfc_error ("Argument of FINAL procedure at %L must not be"
13454 " INTENT(OUT)", &arg->declared_at);
13455 goto error;
13456 }
13457
13458 /* Warn if the procedure is non-scalar and not assumed shape. */
13459 if (warn_surprising && arg->as && arg->as->rank != 0
13460 && arg->as->type != AS_ASSUMED_SHAPE)
13461 gfc_warning (OPT_Wsurprising,
13462 "Non-scalar FINAL procedure at %L should have assumed"
13463 " shape argument", &arg->declared_at);
13464
13465 /* Check that it does not match in kind and rank with a FINAL procedure
13466 defined earlier. To really loop over the *earlier* declarations,
13467 we need to walk the tail of the list as new ones were pushed at the
13468 front. */
13469 /* TODO: Handle kind parameters once they are implemented. */
13470 my_rank = (arg->as ? arg->as->rank : 0);
13471 for (i = list->next; i; i = i->next)
13472 {
13473 gfc_formal_arglist *dummy_args;
13474
13475 /* Argument list might be empty; that is an error signalled earlier,
13476 but we nevertheless continued resolving. */
13477 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
13478 if (dummy_args)
13479 {
13480 gfc_symbol* i_arg = dummy_args->sym;
13481 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
13482 if (i_rank == my_rank)
13483 {
13484 gfc_error ("FINAL procedure %qs declared at %L has the same"
13485 " rank (%d) as %qs",
13486 list->proc_sym->name, &list->where, my_rank,
13487 i->proc_sym->name);
13488 goto error;
13489 }
13490 }
13491 }
13492
13493 /* Is this the/a scalar finalizer procedure? */
13494 if (my_rank == 0)
13495 seen_scalar = true;
13496
13497 /* Find the symtree for this procedure. */
13498 gcc_assert (!list->proc_tree);
13499 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
13500
13501 prev_link = &list->next;
13502 continue;
13503
13504 /* Remove wrong nodes immediately from the list so we don't risk any
13505 troubles in the future when they might fail later expectations. */
13506 error:
13507 i = list;
13508 *prev_link = list->next;
13509 gfc_free_finalizer (i);
13510 result = false;
13511 }
13512
13513 if (result == false)
13514 return false;
13515
13516 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
13517 were nodes in the list, must have been for arrays. It is surely a good
13518 idea to have a scalar version there if there's something to finalize. */
13519 if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
13520 gfc_warning (OPT_Wsurprising,
13521 "Only array FINAL procedures declared for derived type %qs"
13522 " defined at %L, suggest also scalar one",
13523 derived->name, &derived->declared_at);
13524
13525 vtab = gfc_find_derived_vtab (derived);
13526 c = vtab->ts.u.derived->components->next->next->next->next->next;
13527 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
13528
13529 if (finalizable)
13530 *finalizable = true;
13531
13532 return true;
13533 }
13534
13535
13536 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
13537
13538 static bool
13539 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
13540 const char* generic_name, locus where)
13541 {
13542 gfc_symbol *sym1, *sym2;
13543 const char *pass1, *pass2;
13544 gfc_formal_arglist *dummy_args;
13545
13546 gcc_assert (t1->specific && t2->specific);
13547 gcc_assert (!t1->specific->is_generic);
13548 gcc_assert (!t2->specific->is_generic);
13549 gcc_assert (t1->is_operator == t2->is_operator);
13550
13551 sym1 = t1->specific->u.specific->n.sym;
13552 sym2 = t2->specific->u.specific->n.sym;
13553
13554 if (sym1 == sym2)
13555 return true;
13556
13557 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
13558 if (sym1->attr.subroutine != sym2->attr.subroutine
13559 || sym1->attr.function != sym2->attr.function)
13560 {
13561 gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
13562 " GENERIC %qs at %L",
13563 sym1->name, sym2->name, generic_name, &where);
13564 return false;
13565 }
13566
13567 /* Determine PASS arguments. */
13568 if (t1->specific->nopass)
13569 pass1 = NULL;
13570 else if (t1->specific->pass_arg)
13571 pass1 = t1->specific->pass_arg;
13572 else
13573 {
13574 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
13575 if (dummy_args)
13576 pass1 = dummy_args->sym->name;
13577 else
13578 pass1 = NULL;
13579 }
13580 if (t2->specific->nopass)
13581 pass2 = NULL;
13582 else if (t2->specific->pass_arg)
13583 pass2 = t2->specific->pass_arg;
13584 else
13585 {
13586 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
13587 if (dummy_args)
13588 pass2 = dummy_args->sym->name;
13589 else
13590 pass2 = NULL;
13591 }
13592
13593 /* Compare the interfaces. */
13594 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
13595 NULL, 0, pass1, pass2))
13596 {
13597 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
13598 sym1->name, sym2->name, generic_name, &where);
13599 return false;
13600 }
13601
13602 return true;
13603 }
13604
13605
13606 /* Worker function for resolving a generic procedure binding; this is used to
13607 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
13608
13609 The difference between those cases is finding possible inherited bindings
13610 that are overridden, as one has to look for them in tb_sym_root,
13611 tb_uop_root or tb_op, respectively. Thus the caller must already find
13612 the super-type and set p->overridden correctly. */
13613
13614 static bool
13615 resolve_tb_generic_targets (gfc_symbol* super_type,
13616 gfc_typebound_proc* p, const char* name)
13617 {
13618 gfc_tbp_generic* target;
13619 gfc_symtree* first_target;
13620 gfc_symtree* inherited;
13621
13622 gcc_assert (p && p->is_generic);
13623
13624 /* Try to find the specific bindings for the symtrees in our target-list. */
13625 gcc_assert (p->u.generic);
13626 for (target = p->u.generic; target; target = target->next)
13627 if (!target->specific)
13628 {
13629 gfc_typebound_proc* overridden_tbp;
13630 gfc_tbp_generic* g;
13631 const char* target_name;
13632
13633 target_name = target->specific_st->name;
13634
13635 /* Defined for this type directly. */
13636 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
13637 {
13638 target->specific = target->specific_st->n.tb;
13639 goto specific_found;
13640 }
13641
13642 /* Look for an inherited specific binding. */
13643 if (super_type)
13644 {
13645 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
13646 true, NULL);
13647
13648 if (inherited)
13649 {
13650 gcc_assert (inherited->n.tb);
13651 target->specific = inherited->n.tb;
13652 goto specific_found;
13653 }
13654 }
13655
13656 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
13657 " at %L", target_name, name, &p->where);
13658 return false;
13659
13660 /* Once we've found the specific binding, check it is not ambiguous with
13661 other specifics already found or inherited for the same GENERIC. */
13662 specific_found:
13663 gcc_assert (target->specific);
13664
13665 /* This must really be a specific binding! */
13666 if (target->specific->is_generic)
13667 {
13668 gfc_error ("GENERIC %qs at %L must target a specific binding,"
13669 " %qs is GENERIC, too", name, &p->where, target_name);
13670 return false;
13671 }
13672
13673 /* Check those already resolved on this type directly. */
13674 for (g = p->u.generic; g; g = g->next)
13675 if (g != target && g->specific
13676 && !check_generic_tbp_ambiguity (target, g, name, p->where))
13677 return false;
13678
13679 /* Check for ambiguity with inherited specific targets. */
13680 for (overridden_tbp = p->overridden; overridden_tbp;
13681 overridden_tbp = overridden_tbp->overridden)
13682 if (overridden_tbp->is_generic)
13683 {
13684 for (g = overridden_tbp->u.generic; g; g = g->next)
13685 {
13686 gcc_assert (g->specific);
13687 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
13688 return false;
13689 }
13690 }
13691 }
13692
13693 /* If we attempt to "overwrite" a specific binding, this is an error. */
13694 if (p->overridden && !p->overridden->is_generic)
13695 {
13696 gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
13697 " the same name", name, &p->where);
13698 return false;
13699 }
13700
13701 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
13702 all must have the same attributes here. */
13703 first_target = p->u.generic->specific->u.specific;
13704 gcc_assert (first_target);
13705 p->subroutine = first_target->n.sym->attr.subroutine;
13706 p->function = first_target->n.sym->attr.function;
13707
13708 return true;
13709 }
13710
13711
13712 /* Resolve a GENERIC procedure binding for a derived type. */
13713
13714 static bool
13715 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
13716 {
13717 gfc_symbol* super_type;
13718
13719 /* Find the overridden binding if any. */
13720 st->n.tb->overridden = NULL;
13721 super_type = gfc_get_derived_super_type (derived);
13722 if (super_type)
13723 {
13724 gfc_symtree* overridden;
13725 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
13726 true, NULL);
13727
13728 if (overridden && overridden->n.tb)
13729 st->n.tb->overridden = overridden->n.tb;
13730 }
13731
13732 /* Resolve using worker function. */
13733 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
13734 }
13735
13736
13737 /* Retrieve the target-procedure of an operator binding and do some checks in
13738 common for intrinsic and user-defined type-bound operators. */
13739
13740 static gfc_symbol*
13741 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
13742 {
13743 gfc_symbol* target_proc;
13744
13745 gcc_assert (target->specific && !target->specific->is_generic);
13746 target_proc = target->specific->u.specific->n.sym;
13747 gcc_assert (target_proc);
13748
13749 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
13750 if (target->specific->nopass)
13751 {
13752 gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
13753 return NULL;
13754 }
13755
13756 return target_proc;
13757 }
13758
13759
13760 /* Resolve a type-bound intrinsic operator. */
13761
13762 static bool
13763 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
13764 gfc_typebound_proc* p)
13765 {
13766 gfc_symbol* super_type;
13767 gfc_tbp_generic* target;
13768
13769 /* If there's already an error here, do nothing (but don't fail again). */
13770 if (p->error)
13771 return true;
13772
13773 /* Operators should always be GENERIC bindings. */
13774 gcc_assert (p->is_generic);
13775
13776 /* Look for an overridden binding. */
13777 super_type = gfc_get_derived_super_type (derived);
13778 if (super_type && super_type->f2k_derived)
13779 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
13780 op, true, NULL);
13781 else
13782 p->overridden = NULL;
13783
13784 /* Resolve general GENERIC properties using worker function. */
13785 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
13786 goto error;
13787
13788 /* Check the targets to be procedures of correct interface. */
13789 for (target = p->u.generic; target; target = target->next)
13790 {
13791 gfc_symbol* target_proc;
13792
13793 target_proc = get_checked_tb_operator_target (target, p->where);
13794 if (!target_proc)
13795 goto error;
13796
13797 if (!gfc_check_operator_interface (target_proc, op, p->where))
13798 goto error;
13799
13800 /* Add target to non-typebound operator list. */
13801 if (!target->specific->deferred && !derived->attr.use_assoc
13802 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
13803 {
13804 gfc_interface *head, *intr;
13805
13806 /* Preempt 'gfc_check_new_interface' for submodules, where the
13807 mechanism for handling module procedures winds up resolving
13808 operator interfaces twice and would otherwise cause an error. */
13809 for (intr = derived->ns->op[op]; intr; intr = intr->next)
13810 if (intr->sym == target_proc
13811 && target_proc->attr.used_in_submodule)
13812 return true;
13813
13814 if (!gfc_check_new_interface (derived->ns->op[op],
13815 target_proc, p->where))
13816 return false;
13817 head = derived->ns->op[op];
13818 intr = gfc_get_interface ();
13819 intr->sym = target_proc;
13820 intr->where = p->where;
13821 intr->next = head;
13822 derived->ns->op[op] = intr;
13823 }
13824 }
13825
13826 return true;
13827
13828 error:
13829 p->error = 1;
13830 return false;
13831 }
13832
13833
13834 /* Resolve a type-bound user operator (tree-walker callback). */
13835
13836 static gfc_symbol* resolve_bindings_derived;
13837 static bool resolve_bindings_result;
13838
13839 static bool check_uop_procedure (gfc_symbol* sym, locus where);
13840
13841 static void
13842 resolve_typebound_user_op (gfc_symtree* stree)
13843 {
13844 gfc_symbol* super_type;
13845 gfc_tbp_generic* target;
13846
13847 gcc_assert (stree && stree->n.tb);
13848
13849 if (stree->n.tb->error)
13850 return;
13851
13852 /* Operators should always be GENERIC bindings. */
13853 gcc_assert (stree->n.tb->is_generic);
13854
13855 /* Find overridden procedure, if any. */
13856 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
13857 if (super_type && super_type->f2k_derived)
13858 {
13859 gfc_symtree* overridden;
13860 overridden = gfc_find_typebound_user_op (super_type, NULL,
13861 stree->name, true, NULL);
13862
13863 if (overridden && overridden->n.tb)
13864 stree->n.tb->overridden = overridden->n.tb;
13865 }
13866 else
13867 stree->n.tb->overridden = NULL;
13868
13869 /* Resolve basically using worker function. */
13870 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
13871 goto error;
13872
13873 /* Check the targets to be functions of correct interface. */
13874 for (target = stree->n.tb->u.generic; target; target = target->next)
13875 {
13876 gfc_symbol* target_proc;
13877
13878 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
13879 if (!target_proc)
13880 goto error;
13881
13882 if (!check_uop_procedure (target_proc, stree->n.tb->where))
13883 goto error;
13884 }
13885
13886 return;
13887
13888 error:
13889 resolve_bindings_result = false;
13890 stree->n.tb->error = 1;
13891 }
13892
13893
13894 /* Resolve the type-bound procedures for a derived type. */
13895
13896 static void
13897 resolve_typebound_procedure (gfc_symtree* stree)
13898 {
13899 gfc_symbol* proc;
13900 locus where;
13901 gfc_symbol* me_arg;
13902 gfc_symbol* super_type;
13903 gfc_component* comp;
13904
13905 gcc_assert (stree);
13906
13907 /* Undefined specific symbol from GENERIC target definition. */
13908 if (!stree->n.tb)
13909 return;
13910
13911 if (stree->n.tb->error)
13912 return;
13913
13914 /* If this is a GENERIC binding, use that routine. */
13915 if (stree->n.tb->is_generic)
13916 {
13917 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
13918 goto error;
13919 return;
13920 }
13921
13922 /* Get the target-procedure to check it. */
13923 gcc_assert (!stree->n.tb->is_generic);
13924 gcc_assert (stree->n.tb->u.specific);
13925 proc = stree->n.tb->u.specific->n.sym;
13926 where = stree->n.tb->where;
13927
13928 /* Default access should already be resolved from the parser. */
13929 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
13930
13931 if (stree->n.tb->deferred)
13932 {
13933 if (!check_proc_interface (proc, &where))
13934 goto error;
13935 }
13936 else
13937 {
13938 /* If proc has not been resolved at this point, proc->name may
13939 actually be a USE associated entity. See PR fortran/89647. */
13940 if (!proc->resolve_symbol_called
13941 && proc->attr.function == 0 && proc->attr.subroutine == 0)
13942 {
13943 gfc_symbol *tmp;
13944 gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
13945 if (tmp && tmp->attr.use_assoc)
13946 {
13947 proc->module = tmp->module;
13948 proc->attr.proc = tmp->attr.proc;
13949 proc->attr.function = tmp->attr.function;
13950 proc->attr.subroutine = tmp->attr.subroutine;
13951 proc->attr.use_assoc = tmp->attr.use_assoc;
13952 proc->ts = tmp->ts;
13953 proc->result = tmp->result;
13954 }
13955 }
13956
13957 /* Check for F08:C465. */
13958 if ((!proc->attr.subroutine && !proc->attr.function)
13959 || (proc->attr.proc != PROC_MODULE
13960 && proc->attr.if_source != IFSRC_IFBODY)
13961 || proc->attr.abstract)
13962 {
13963 gfc_error ("%qs must be a module procedure or an external "
13964 "procedure with an explicit interface at %L",
13965 proc->name, &where);
13966 goto error;
13967 }
13968 }
13969
13970 stree->n.tb->subroutine = proc->attr.subroutine;
13971 stree->n.tb->function = proc->attr.function;
13972
13973 /* Find the super-type of the current derived type. We could do this once and
13974 store in a global if speed is needed, but as long as not I believe this is
13975 more readable and clearer. */
13976 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
13977
13978 /* If PASS, resolve and check arguments if not already resolved / loaded
13979 from a .mod file. */
13980 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
13981 {
13982 gfc_formal_arglist *dummy_args;
13983
13984 dummy_args = gfc_sym_get_dummy_args (proc);
13985 if (stree->n.tb->pass_arg)
13986 {
13987 gfc_formal_arglist *i;
13988
13989 /* If an explicit passing argument name is given, walk the arg-list
13990 and look for it. */
13991
13992 me_arg = NULL;
13993 stree->n.tb->pass_arg_num = 1;
13994 for (i = dummy_args; i; i = i->next)
13995 {
13996 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
13997 {
13998 me_arg = i->sym;
13999 break;
14000 }
14001 ++stree->n.tb->pass_arg_num;
14002 }
14003
14004 if (!me_arg)
14005 {
14006 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
14007 " argument %qs",
14008 proc->name, stree->n.tb->pass_arg, &where,
14009 stree->n.tb->pass_arg);
14010 goto error;
14011 }
14012 }
14013 else
14014 {
14015 /* Otherwise, take the first one; there should in fact be at least
14016 one. */
14017 stree->n.tb->pass_arg_num = 1;
14018 if (!dummy_args)
14019 {
14020 gfc_error ("Procedure %qs with PASS at %L must have at"
14021 " least one argument", proc->name, &where);
14022 goto error;
14023 }
14024 me_arg = dummy_args->sym;
14025 }
14026
14027 /* Now check that the argument-type matches and the passed-object
14028 dummy argument is generally fine. */
14029
14030 gcc_assert (me_arg);
14031
14032 if (me_arg->ts.type != BT_CLASS)
14033 {
14034 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14035 " at %L", proc->name, &where);
14036 goto error;
14037 }
14038
14039 if (CLASS_DATA (me_arg)->ts.u.derived
14040 != resolve_bindings_derived)
14041 {
14042 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14043 " the derived-type %qs", me_arg->name, proc->name,
14044 me_arg->name, &where, resolve_bindings_derived->name);
14045 goto error;
14046 }
14047
14048 gcc_assert (me_arg->ts.type == BT_CLASS);
14049 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
14050 {
14051 gfc_error ("Passed-object dummy argument of %qs at %L must be"
14052 " scalar", proc->name, &where);
14053 goto error;
14054 }
14055 if (CLASS_DATA (me_arg)->attr.allocatable)
14056 {
14057 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14058 " be ALLOCATABLE", proc->name, &where);
14059 goto error;
14060 }
14061 if (CLASS_DATA (me_arg)->attr.class_pointer)
14062 {
14063 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14064 " be POINTER", proc->name, &where);
14065 goto error;
14066 }
14067 }
14068
14069 /* If we are extending some type, check that we don't override a procedure
14070 flagged NON_OVERRIDABLE. */
14071 stree->n.tb->overridden = NULL;
14072 if (super_type)
14073 {
14074 gfc_symtree* overridden;
14075 overridden = gfc_find_typebound_proc (super_type, NULL,
14076 stree->name, true, NULL);
14077
14078 if (overridden)
14079 {
14080 if (overridden->n.tb)
14081 stree->n.tb->overridden = overridden->n.tb;
14082
14083 if (!gfc_check_typebound_override (stree, overridden))
14084 goto error;
14085 }
14086 }
14087
14088 /* See if there's a name collision with a component directly in this type. */
14089 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
14090 if (!strcmp (comp->name, stree->name))
14091 {
14092 gfc_error ("Procedure %qs at %L has the same name as a component of"
14093 " %qs",
14094 stree->name, &where, resolve_bindings_derived->name);
14095 goto error;
14096 }
14097
14098 /* Try to find a name collision with an inherited component. */
14099 if (super_type && gfc_find_component (super_type, stree->name, true, true,
14100 NULL))
14101 {
14102 gfc_error ("Procedure %qs at %L has the same name as an inherited"
14103 " component of %qs",
14104 stree->name, &where, resolve_bindings_derived->name);
14105 goto error;
14106 }
14107
14108 stree->n.tb->error = 0;
14109 return;
14110
14111 error:
14112 resolve_bindings_result = false;
14113 stree->n.tb->error = 1;
14114 }
14115
14116
14117 static bool
14118 resolve_typebound_procedures (gfc_symbol* derived)
14119 {
14120 int op;
14121 gfc_symbol* super_type;
14122
14123 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
14124 return true;
14125
14126 super_type = gfc_get_derived_super_type (derived);
14127 if (super_type)
14128 resolve_symbol (super_type);
14129
14130 resolve_bindings_derived = derived;
14131 resolve_bindings_result = true;
14132
14133 if (derived->f2k_derived->tb_sym_root)
14134 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
14135 &resolve_typebound_procedure);
14136
14137 if (derived->f2k_derived->tb_uop_root)
14138 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
14139 &resolve_typebound_user_op);
14140
14141 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
14142 {
14143 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
14144 if (p && !resolve_typebound_intrinsic_op (derived,
14145 (gfc_intrinsic_op)op, p))
14146 resolve_bindings_result = false;
14147 }
14148
14149 return resolve_bindings_result;
14150 }
14151
14152
14153 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
14154 to give all identical derived types the same backend_decl. */
14155 static void
14156 add_dt_to_dt_list (gfc_symbol *derived)
14157 {
14158 if (!derived->dt_next)
14159 {
14160 if (gfc_derived_types)
14161 {
14162 derived->dt_next = gfc_derived_types->dt_next;
14163 gfc_derived_types->dt_next = derived;
14164 }
14165 else
14166 {
14167 derived->dt_next = derived;
14168 }
14169 gfc_derived_types = derived;
14170 }
14171 }
14172
14173
14174 /* Ensure that a derived-type is really not abstract, meaning that every
14175 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
14176
14177 static bool
14178 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
14179 {
14180 if (!st)
14181 return true;
14182
14183 if (!ensure_not_abstract_walker (sub, st->left))
14184 return false;
14185 if (!ensure_not_abstract_walker (sub, st->right))
14186 return false;
14187
14188 if (st->n.tb && st->n.tb->deferred)
14189 {
14190 gfc_symtree* overriding;
14191 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
14192 if (!overriding)
14193 return false;
14194 gcc_assert (overriding->n.tb);
14195 if (overriding->n.tb->deferred)
14196 {
14197 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
14198 " %qs is DEFERRED and not overridden",
14199 sub->name, &sub->declared_at, st->name);
14200 return false;
14201 }
14202 }
14203
14204 return true;
14205 }
14206
14207 static bool
14208 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
14209 {
14210 /* The algorithm used here is to recursively travel up the ancestry of sub
14211 and for each ancestor-type, check all bindings. If any of them is
14212 DEFERRED, look it up starting from sub and see if the found (overriding)
14213 binding is not DEFERRED.
14214 This is not the most efficient way to do this, but it should be ok and is
14215 clearer than something sophisticated. */
14216
14217 gcc_assert (ancestor && !sub->attr.abstract);
14218
14219 if (!ancestor->attr.abstract)
14220 return true;
14221
14222 /* Walk bindings of this ancestor. */
14223 if (ancestor->f2k_derived)
14224 {
14225 bool t;
14226 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
14227 if (!t)
14228 return false;
14229 }
14230
14231 /* Find next ancestor type and recurse on it. */
14232 ancestor = gfc_get_derived_super_type (ancestor);
14233 if (ancestor)
14234 return ensure_not_abstract (sub, ancestor);
14235
14236 return true;
14237 }
14238
14239
14240 /* This check for typebound defined assignments is done recursively
14241 since the order in which derived types are resolved is not always in
14242 order of the declarations. */
14243
14244 static void
14245 check_defined_assignments (gfc_symbol *derived)
14246 {
14247 gfc_component *c;
14248
14249 for (c = derived->components; c; c = c->next)
14250 {
14251 if (!gfc_bt_struct (c->ts.type)
14252 || c->attr.pointer
14253 || c->attr.allocatable
14254 || c->attr.proc_pointer_comp
14255 || c->attr.class_pointer
14256 || c->attr.proc_pointer)
14257 continue;
14258
14259 if (c->ts.u.derived->attr.defined_assign_comp
14260 || (c->ts.u.derived->f2k_derived
14261 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
14262 {
14263 derived->attr.defined_assign_comp = 1;
14264 return;
14265 }
14266
14267 check_defined_assignments (c->ts.u.derived);
14268 if (c->ts.u.derived->attr.defined_assign_comp)
14269 {
14270 derived->attr.defined_assign_comp = 1;
14271 return;
14272 }
14273 }
14274 }
14275
14276
14277 /* Resolve a single component of a derived type or structure. */
14278
14279 static bool
14280 resolve_component (gfc_component *c, gfc_symbol *sym)
14281 {
14282 gfc_symbol *super_type;
14283 symbol_attribute *attr;
14284
14285 if (c->attr.artificial)
14286 return true;
14287
14288 /* Do not allow vtype components to be resolved in nameless namespaces
14289 such as block data because the procedure pointers will cause ICEs
14290 and vtables are not needed in these contexts. */
14291 if (sym->attr.vtype && sym->attr.use_assoc
14292 && sym->ns->proc_name == NULL)
14293 return true;
14294
14295 /* F2008, C442. */
14296 if ((!sym->attr.is_class || c != sym->components)
14297 && c->attr.codimension
14298 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
14299 {
14300 gfc_error ("Coarray component %qs at %L must be allocatable with "
14301 "deferred shape", c->name, &c->loc);
14302 return false;
14303 }
14304
14305 /* F2008, C443. */
14306 if (c->attr.codimension && c->ts.type == BT_DERIVED
14307 && c->ts.u.derived->ts.is_iso_c)
14308 {
14309 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
14310 "shall not be a coarray", c->name, &c->loc);
14311 return false;
14312 }
14313
14314 /* F2008, C444. */
14315 if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
14316 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
14317 || c->attr.allocatable))
14318 {
14319 gfc_error ("Component %qs at %L with coarray component "
14320 "shall be a nonpointer, nonallocatable scalar",
14321 c->name, &c->loc);
14322 return false;
14323 }
14324
14325 /* F2008, C448. */
14326 if (c->ts.type == BT_CLASS)
14327 {
14328 if (CLASS_DATA (c))
14329 {
14330 attr = &(CLASS_DATA (c)->attr);
14331
14332 /* Fix up contiguous attribute. */
14333 if (c->attr.contiguous)
14334 attr->contiguous = 1;
14335 }
14336 else
14337 attr = NULL;
14338 }
14339 else
14340 attr = &c->attr;
14341
14342 if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
14343 {
14344 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
14345 "is not an array pointer", c->name, &c->loc);
14346 return false;
14347 }
14348
14349 /* F2003, 15.2.1 - length has to be one. */
14350 if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
14351 && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
14352 || !gfc_is_constant_expr (c->ts.u.cl->length)
14353 || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
14354 {
14355 gfc_error ("Component %qs of BIND(C) type at %L must have length one",
14356 c->name, &c->loc);
14357 return false;
14358 }
14359
14360 if (c->attr.proc_pointer && c->ts.interface)
14361 {
14362 gfc_symbol *ifc = c->ts.interface;
14363
14364 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
14365 {
14366 c->tb->error = 1;
14367 return false;
14368 }
14369
14370 if (ifc->attr.if_source || ifc->attr.intrinsic)
14371 {
14372 /* Resolve interface and copy attributes. */
14373 if (ifc->formal && !ifc->formal_ns)
14374 resolve_symbol (ifc);
14375 if (ifc->attr.intrinsic)
14376 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
14377
14378 if (ifc->result)
14379 {
14380 c->ts = ifc->result->ts;
14381 c->attr.allocatable = ifc->result->attr.allocatable;
14382 c->attr.pointer = ifc->result->attr.pointer;
14383 c->attr.dimension = ifc->result->attr.dimension;
14384 c->as = gfc_copy_array_spec (ifc->result->as);
14385 c->attr.class_ok = ifc->result->attr.class_ok;
14386 }
14387 else
14388 {
14389 c->ts = ifc->ts;
14390 c->attr.allocatable = ifc->attr.allocatable;
14391 c->attr.pointer = ifc->attr.pointer;
14392 c->attr.dimension = ifc->attr.dimension;
14393 c->as = gfc_copy_array_spec (ifc->as);
14394 c->attr.class_ok = ifc->attr.class_ok;
14395 }
14396 c->ts.interface = ifc;
14397 c->attr.function = ifc->attr.function;
14398 c->attr.subroutine = ifc->attr.subroutine;
14399
14400 c->attr.pure = ifc->attr.pure;
14401 c->attr.elemental = ifc->attr.elemental;
14402 c->attr.recursive = ifc->attr.recursive;
14403 c->attr.always_explicit = ifc->attr.always_explicit;
14404 c->attr.ext_attr |= ifc->attr.ext_attr;
14405 /* Copy char length. */
14406 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
14407 {
14408 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
14409 if (cl->length && !cl->resolved
14410 && !gfc_resolve_expr (cl->length))
14411 {
14412 c->tb->error = 1;
14413 return false;
14414 }
14415 c->ts.u.cl = cl;
14416 }
14417 }
14418 }
14419 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
14420 {
14421 /* Since PPCs are not implicitly typed, a PPC without an explicit
14422 interface must be a subroutine. */
14423 gfc_add_subroutine (&c->attr, c->name, &c->loc);
14424 }
14425
14426 /* Procedure pointer components: Check PASS arg. */
14427 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
14428 && !sym->attr.vtype)
14429 {
14430 gfc_symbol* me_arg;
14431
14432 if (c->tb->pass_arg)
14433 {
14434 gfc_formal_arglist* i;
14435
14436 /* If an explicit passing argument name is given, walk the arg-list
14437 and look for it. */
14438
14439 me_arg = NULL;
14440 c->tb->pass_arg_num = 1;
14441 for (i = c->ts.interface->formal; i; i = i->next)
14442 {
14443 if (!strcmp (i->sym->name, c->tb->pass_arg))
14444 {
14445 me_arg = i->sym;
14446 break;
14447 }
14448 c->tb->pass_arg_num++;
14449 }
14450
14451 if (!me_arg)
14452 {
14453 gfc_error ("Procedure pointer component %qs with PASS(%s) "
14454 "at %L has no argument %qs", c->name,
14455 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
14456 c->tb->error = 1;
14457 return false;
14458 }
14459 }
14460 else
14461 {
14462 /* Otherwise, take the first one; there should in fact be at least
14463 one. */
14464 c->tb->pass_arg_num = 1;
14465 if (!c->ts.interface->formal)
14466 {
14467 gfc_error ("Procedure pointer component %qs with PASS at %L "
14468 "must have at least one argument",
14469 c->name, &c->loc);
14470 c->tb->error = 1;
14471 return false;
14472 }
14473 me_arg = c->ts.interface->formal->sym;
14474 }
14475
14476 /* Now check that the argument-type matches. */
14477 gcc_assert (me_arg);
14478 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
14479 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
14480 || (me_arg->ts.type == BT_CLASS
14481 && CLASS_DATA (me_arg)->ts.u.derived != sym))
14482 {
14483 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14484 " the derived type %qs", me_arg->name, c->name,
14485 me_arg->name, &c->loc, sym->name);
14486 c->tb->error = 1;
14487 return false;
14488 }
14489
14490 /* Check for F03:C453. */
14491 if (CLASS_DATA (me_arg)->attr.dimension)
14492 {
14493 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14494 "must be scalar", me_arg->name, c->name, me_arg->name,
14495 &c->loc);
14496 c->tb->error = 1;
14497 return false;
14498 }
14499
14500 if (CLASS_DATA (me_arg)->attr.class_pointer)
14501 {
14502 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14503 "may not have the POINTER attribute", me_arg->name,
14504 c->name, me_arg->name, &c->loc);
14505 c->tb->error = 1;
14506 return false;
14507 }
14508
14509 if (CLASS_DATA (me_arg)->attr.allocatable)
14510 {
14511 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14512 "may not be ALLOCATABLE", me_arg->name, c->name,
14513 me_arg->name, &c->loc);
14514 c->tb->error = 1;
14515 return false;
14516 }
14517
14518 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
14519 {
14520 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14521 " at %L", c->name, &c->loc);
14522 return false;
14523 }
14524
14525 }
14526
14527 /* Check type-spec if this is not the parent-type component. */
14528 if (((sym->attr.is_class
14529 && (!sym->components->ts.u.derived->attr.extension
14530 || c != sym->components->ts.u.derived->components))
14531 || (!sym->attr.is_class
14532 && (!sym->attr.extension || c != sym->components)))
14533 && !sym->attr.vtype
14534 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
14535 return false;
14536
14537 super_type = gfc_get_derived_super_type (sym);
14538
14539 /* If this type is an extension, set the accessibility of the parent
14540 component. */
14541 if (super_type
14542 && ((sym->attr.is_class
14543 && c == sym->components->ts.u.derived->components)
14544 || (!sym->attr.is_class && c == sym->components))
14545 && strcmp (super_type->name, c->name) == 0)
14546 c->attr.access = super_type->attr.access;
14547
14548 /* If this type is an extension, see if this component has the same name
14549 as an inherited type-bound procedure. */
14550 if (super_type && !sym->attr.is_class
14551 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
14552 {
14553 gfc_error ("Component %qs of %qs at %L has the same name as an"
14554 " inherited type-bound procedure",
14555 c->name, sym->name, &c->loc);
14556 return false;
14557 }
14558
14559 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
14560 && !c->ts.deferred)
14561 {
14562 if (c->ts.u.cl->length == NULL
14563 || (!resolve_charlen(c->ts.u.cl))
14564 || !gfc_is_constant_expr (c->ts.u.cl->length))
14565 {
14566 gfc_error ("Character length of component %qs needs to "
14567 "be a constant specification expression at %L",
14568 c->name,
14569 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
14570 return false;
14571 }
14572 }
14573
14574 if (c->ts.type == BT_CHARACTER && c->ts.deferred
14575 && !c->attr.pointer && !c->attr.allocatable)
14576 {
14577 gfc_error ("Character component %qs of %qs at %L with deferred "
14578 "length must be a POINTER or ALLOCATABLE",
14579 c->name, sym->name, &c->loc);
14580 return false;
14581 }
14582
14583 /* Add the hidden deferred length field. */
14584 if (c->ts.type == BT_CHARACTER
14585 && (c->ts.deferred || c->attr.pdt_string)
14586 && !c->attr.function
14587 && !sym->attr.is_class)
14588 {
14589 char name[GFC_MAX_SYMBOL_LEN+9];
14590 gfc_component *strlen;
14591 sprintf (name, "_%s_length", c->name);
14592 strlen = gfc_find_component (sym, name, true, true, NULL);
14593 if (strlen == NULL)
14594 {
14595 if (!gfc_add_component (sym, name, &strlen))
14596 return false;
14597 strlen->ts.type = BT_INTEGER;
14598 strlen->ts.kind = gfc_charlen_int_kind;
14599 strlen->attr.access = ACCESS_PRIVATE;
14600 strlen->attr.artificial = 1;
14601 }
14602 }
14603
14604 if (c->ts.type == BT_DERIVED
14605 && sym->component_access != ACCESS_PRIVATE
14606 && gfc_check_symbol_access (sym)
14607 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
14608 && !c->ts.u.derived->attr.use_assoc
14609 && !gfc_check_symbol_access (c->ts.u.derived)
14610 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
14611 "PRIVATE type and cannot be a component of "
14612 "%qs, which is PUBLIC at %L", c->name,
14613 sym->name, &sym->declared_at))
14614 return false;
14615
14616 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
14617 {
14618 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
14619 "type %s", c->name, &c->loc, sym->name);
14620 return false;
14621 }
14622
14623 if (sym->attr.sequence)
14624 {
14625 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
14626 {
14627 gfc_error ("Component %s of SEQUENCE type declared at %L does "
14628 "not have the SEQUENCE attribute",
14629 c->ts.u.derived->name, &sym->declared_at);
14630 return false;
14631 }
14632 }
14633
14634 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
14635 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
14636 else if (c->ts.type == BT_CLASS && c->attr.class_ok
14637 && CLASS_DATA (c)->ts.u.derived->attr.generic)
14638 CLASS_DATA (c)->ts.u.derived
14639 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
14640
14641 /* If an allocatable component derived type is of the same type as
14642 the enclosing derived type, we need a vtable generating so that
14643 the __deallocate procedure is created. */
14644 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
14645 && c->ts.u.derived == sym && c->attr.allocatable == 1)
14646 gfc_find_vtab (&c->ts);
14647
14648 /* Ensure that all the derived type components are put on the
14649 derived type list; even in formal namespaces, where derived type
14650 pointer components might not have been declared. */
14651 if (c->ts.type == BT_DERIVED
14652 && c->ts.u.derived
14653 && c->ts.u.derived->components
14654 && c->attr.pointer
14655 && sym != c->ts.u.derived)
14656 add_dt_to_dt_list (c->ts.u.derived);
14657
14658 if (!gfc_resolve_array_spec (c->as,
14659 !(c->attr.pointer || c->attr.proc_pointer
14660 || c->attr.allocatable)))
14661 return false;
14662
14663 if (c->initializer && !sym->attr.vtype
14664 && !c->attr.pdt_kind && !c->attr.pdt_len
14665 && !gfc_check_assign_symbol (sym, c, c->initializer))
14666 return false;
14667
14668 return true;
14669 }
14670
14671
14672 /* Be nice about the locus for a structure expression - show the locus of the
14673 first non-null sub-expression if we can. */
14674
14675 static locus *
14676 cons_where (gfc_expr *struct_expr)
14677 {
14678 gfc_constructor *cons;
14679
14680 gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
14681
14682 cons = gfc_constructor_first (struct_expr->value.constructor);
14683 for (; cons; cons = gfc_constructor_next (cons))
14684 {
14685 if (cons->expr && cons->expr->expr_type != EXPR_NULL)
14686 return &cons->expr->where;
14687 }
14688
14689 return &struct_expr->where;
14690 }
14691
14692 /* Resolve the components of a structure type. Much less work than derived
14693 types. */
14694
14695 static bool
14696 resolve_fl_struct (gfc_symbol *sym)
14697 {
14698 gfc_component *c;
14699 gfc_expr *init = NULL;
14700 bool success;
14701
14702 /* Make sure UNIONs do not have overlapping initializers. */
14703 if (sym->attr.flavor == FL_UNION)
14704 {
14705 for (c = sym->components; c; c = c->next)
14706 {
14707 if (init && c->initializer)
14708 {
14709 gfc_error ("Conflicting initializers in union at %L and %L",
14710 cons_where (init), cons_where (c->initializer));
14711 gfc_free_expr (c->initializer);
14712 c->initializer = NULL;
14713 }
14714 if (init == NULL)
14715 init = c->initializer;
14716 }
14717 }
14718
14719 success = true;
14720 for (c = sym->components; c; c = c->next)
14721 if (!resolve_component (c, sym))
14722 success = false;
14723
14724 if (!success)
14725 return false;
14726
14727 if (sym->components)
14728 add_dt_to_dt_list (sym);
14729
14730 return true;
14731 }
14732
14733
14734 /* Resolve the components of a derived type. This does not have to wait until
14735 resolution stage, but can be done as soon as the dt declaration has been
14736 parsed. */
14737
14738 static bool
14739 resolve_fl_derived0 (gfc_symbol *sym)
14740 {
14741 gfc_symbol* super_type;
14742 gfc_component *c;
14743 gfc_formal_arglist *f;
14744 bool success;
14745
14746 if (sym->attr.unlimited_polymorphic)
14747 return true;
14748
14749 super_type = gfc_get_derived_super_type (sym);
14750
14751 /* F2008, C432. */
14752 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
14753 {
14754 gfc_error ("As extending type %qs at %L has a coarray component, "
14755 "parent type %qs shall also have one", sym->name,
14756 &sym->declared_at, super_type->name);
14757 return false;
14758 }
14759
14760 /* Ensure the extended type gets resolved before we do. */
14761 if (super_type && !resolve_fl_derived0 (super_type))
14762 return false;
14763
14764 /* An ABSTRACT type must be extensible. */
14765 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
14766 {
14767 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
14768 sym->name, &sym->declared_at);
14769 return false;
14770 }
14771
14772 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
14773 : sym->components;
14774
14775 success = true;
14776 for ( ; c != NULL; c = c->next)
14777 if (!resolve_component (c, sym))
14778 success = false;
14779
14780 if (!success)
14781 return false;
14782
14783 /* Now add the caf token field, where needed. */
14784 if (flag_coarray != GFC_FCOARRAY_NONE
14785 && !sym->attr.is_class && !sym->attr.vtype)
14786 {
14787 for (c = sym->components; c; c = c->next)
14788 if (!c->attr.dimension && !c->attr.codimension
14789 && (c->attr.allocatable || c->attr.pointer))
14790 {
14791 char name[GFC_MAX_SYMBOL_LEN+9];
14792 gfc_component *token;
14793 sprintf (name, "_caf_%s", c->name);
14794 token = gfc_find_component (sym, name, true, true, NULL);
14795 if (token == NULL)
14796 {
14797 if (!gfc_add_component (sym, name, &token))
14798 return false;
14799 token->ts.type = BT_VOID;
14800 token->ts.kind = gfc_default_integer_kind;
14801 token->attr.access = ACCESS_PRIVATE;
14802 token->attr.artificial = 1;
14803 token->attr.caf_token = 1;
14804 }
14805 }
14806 }
14807
14808 check_defined_assignments (sym);
14809
14810 if (!sym->attr.defined_assign_comp && super_type)
14811 sym->attr.defined_assign_comp
14812 = super_type->attr.defined_assign_comp;
14813
14814 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
14815 all DEFERRED bindings are overridden. */
14816 if (super_type && super_type->attr.abstract && !sym->attr.abstract
14817 && !sym->attr.is_class
14818 && !ensure_not_abstract (sym, super_type))
14819 return false;
14820
14821 /* Check that there is a component for every PDT parameter. */
14822 if (sym->attr.pdt_template)
14823 {
14824 for (f = sym->formal; f; f = f->next)
14825 {
14826 if (!f->sym)
14827 continue;
14828 c = gfc_find_component (sym, f->sym->name, true, true, NULL);
14829 if (c == NULL)
14830 {
14831 gfc_error ("Parameterized type %qs does not have a component "
14832 "corresponding to parameter %qs at %L", sym->name,
14833 f->sym->name, &sym->declared_at);
14834 break;
14835 }
14836 }
14837 }
14838
14839 /* Add derived type to the derived type list. */
14840 add_dt_to_dt_list (sym);
14841
14842 return true;
14843 }
14844
14845
14846 /* The following procedure does the full resolution of a derived type,
14847 including resolution of all type-bound procedures (if present). In contrast
14848 to 'resolve_fl_derived0' this can only be done after the module has been
14849 parsed completely. */
14850
14851 static bool
14852 resolve_fl_derived (gfc_symbol *sym)
14853 {
14854 gfc_symbol *gen_dt = NULL;
14855
14856 if (sym->attr.unlimited_polymorphic)
14857 return true;
14858
14859 if (!sym->attr.is_class)
14860 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
14861 if (gen_dt && gen_dt->generic && gen_dt->generic->next
14862 && (!gen_dt->generic->sym->attr.use_assoc
14863 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
14864 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
14865 "%qs at %L being the same name as derived "
14866 "type at %L", sym->name,
14867 gen_dt->generic->sym == sym
14868 ? gen_dt->generic->next->sym->name
14869 : gen_dt->generic->sym->name,
14870 gen_dt->generic->sym == sym
14871 ? &gen_dt->generic->next->sym->declared_at
14872 : &gen_dt->generic->sym->declared_at,
14873 &sym->declared_at))
14874 return false;
14875
14876 if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
14877 {
14878 gfc_error ("Derived type %qs at %L has not been declared",
14879 sym->name, &sym->declared_at);
14880 return false;
14881 }
14882
14883 /* Resolve the finalizer procedures. */
14884 if (!gfc_resolve_finalizers (sym, NULL))
14885 return false;
14886
14887 if (sym->attr.is_class && sym->ts.u.derived == NULL)
14888 {
14889 /* Fix up incomplete CLASS symbols. */
14890 gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
14891 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
14892
14893 /* Nothing more to do for unlimited polymorphic entities. */
14894 if (data->ts.u.derived->attr.unlimited_polymorphic)
14895 return true;
14896 else if (vptr->ts.u.derived == NULL)
14897 {
14898 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
14899 gcc_assert (vtab);
14900 vptr->ts.u.derived = vtab->ts.u.derived;
14901 if (!resolve_fl_derived0 (vptr->ts.u.derived))
14902 return false;
14903 }
14904 }
14905
14906 if (!resolve_fl_derived0 (sym))
14907 return false;
14908
14909 /* Resolve the type-bound procedures. */
14910 if (!resolve_typebound_procedures (sym))
14911 return false;
14912
14913 /* Generate module vtables subject to their accessibility and their not
14914 being vtables or pdt templates. If this is not done class declarations
14915 in external procedures wind up with their own version and so SELECT TYPE
14916 fails because the vptrs do not have the same address. */
14917 if (gfc_option.allow_std & GFC_STD_F2003
14918 && sym->ns->proc_name
14919 && sym->ns->proc_name->attr.flavor == FL_MODULE
14920 && sym->attr.access != ACCESS_PRIVATE
14921 && !(sym->attr.use_assoc || sym->attr.vtype || sym->attr.pdt_template))
14922 {
14923 gfc_symbol *vtab = gfc_find_derived_vtab (sym);
14924 gfc_set_sym_referenced (vtab);
14925 }
14926
14927 return true;
14928 }
14929
14930
14931 static bool
14932 resolve_fl_namelist (gfc_symbol *sym)
14933 {
14934 gfc_namelist *nl;
14935 gfc_symbol *nlsym;
14936
14937 for (nl = sym->namelist; nl; nl = nl->next)
14938 {
14939 /* Check again, the check in match only works if NAMELIST comes
14940 after the decl. */
14941 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
14942 {
14943 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
14944 "allowed", nl->sym->name, sym->name, &sym->declared_at);
14945 return false;
14946 }
14947
14948 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
14949 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
14950 "with assumed shape in namelist %qs at %L",
14951 nl->sym->name, sym->name, &sym->declared_at))
14952 return false;
14953
14954 if (is_non_constant_shape_array (nl->sym)
14955 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
14956 "with nonconstant shape in namelist %qs at %L",
14957 nl->sym->name, sym->name, &sym->declared_at))
14958 return false;
14959
14960 if (nl->sym->ts.type == BT_CHARACTER
14961 && (nl->sym->ts.u.cl->length == NULL
14962 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
14963 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
14964 "nonconstant character length in "
14965 "namelist %qs at %L", nl->sym->name,
14966 sym->name, &sym->declared_at))
14967 return false;
14968
14969 }
14970
14971 /* Reject PRIVATE objects in a PUBLIC namelist. */
14972 if (gfc_check_symbol_access (sym))
14973 {
14974 for (nl = sym->namelist; nl; nl = nl->next)
14975 {
14976 if (!nl->sym->attr.use_assoc
14977 && !is_sym_host_assoc (nl->sym, sym->ns)
14978 && !gfc_check_symbol_access (nl->sym))
14979 {
14980 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
14981 "cannot be member of PUBLIC namelist %qs at %L",
14982 nl->sym->name, sym->name, &sym->declared_at);
14983 return false;
14984 }
14985
14986 if (nl->sym->ts.type == BT_DERIVED
14987 && (nl->sym->ts.u.derived->attr.alloc_comp
14988 || nl->sym->ts.u.derived->attr.pointer_comp))
14989 {
14990 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
14991 "namelist %qs at %L with ALLOCATABLE "
14992 "or POINTER components", nl->sym->name,
14993 sym->name, &sym->declared_at))
14994 return false;
14995 return true;
14996 }
14997
14998 /* Types with private components that came here by USE-association. */
14999 if (nl->sym->ts.type == BT_DERIVED
15000 && derived_inaccessible (nl->sym->ts.u.derived))
15001 {
15002 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
15003 "components and cannot be member of namelist %qs at %L",
15004 nl->sym->name, sym->name, &sym->declared_at);
15005 return false;
15006 }
15007
15008 /* Types with private components that are defined in the same module. */
15009 if (nl->sym->ts.type == BT_DERIVED
15010 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
15011 && nl->sym->ts.u.derived->attr.private_comp)
15012 {
15013 gfc_error ("NAMELIST object %qs has PRIVATE components and "
15014 "cannot be a member of PUBLIC namelist %qs at %L",
15015 nl->sym->name, sym->name, &sym->declared_at);
15016 return false;
15017 }
15018 }
15019 }
15020
15021
15022 /* 14.1.2 A module or internal procedure represent local entities
15023 of the same type as a namelist member and so are not allowed. */
15024 for (nl = sym->namelist; nl; nl = nl->next)
15025 {
15026 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
15027 continue;
15028
15029 if (nl->sym->attr.function && nl->sym == nl->sym->result)
15030 if ((nl->sym == sym->ns->proc_name)
15031 ||
15032 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
15033 continue;
15034
15035 nlsym = NULL;
15036 if (nl->sym->name)
15037 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
15038 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
15039 {
15040 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
15041 "attribute in %qs at %L", nlsym->name,
15042 &sym->declared_at);
15043 return false;
15044 }
15045 }
15046
15047 return true;
15048 }
15049
15050
15051 static bool
15052 resolve_fl_parameter (gfc_symbol *sym)
15053 {
15054 /* A parameter array's shape needs to be constant. */
15055 if (sym->as != NULL
15056 && (sym->as->type == AS_DEFERRED
15057 || is_non_constant_shape_array (sym)))
15058 {
15059 gfc_error ("Parameter array %qs at %L cannot be automatic "
15060 "or of deferred shape", sym->name, &sym->declared_at);
15061 return false;
15062 }
15063
15064 /* Constraints on deferred type parameter. */
15065 if (!deferred_requirements (sym))
15066 return false;
15067
15068 /* Make sure a parameter that has been implicitly typed still
15069 matches the implicit type, since PARAMETER statements can precede
15070 IMPLICIT statements. */
15071 if (sym->attr.implicit_type
15072 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
15073 sym->ns)))
15074 {
15075 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
15076 "later IMPLICIT type", sym->name, &sym->declared_at);
15077 return false;
15078 }
15079
15080 /* Make sure the types of derived parameters are consistent. This
15081 type checking is deferred until resolution because the type may
15082 refer to a derived type from the host. */
15083 if (sym->ts.type == BT_DERIVED
15084 && !gfc_compare_types (&sym->ts, &sym->value->ts))
15085 {
15086 gfc_error ("Incompatible derived type in PARAMETER at %L",
15087 &sym->value->where);
15088 return false;
15089 }
15090
15091 /* F03:C509,C514. */
15092 if (sym->ts.type == BT_CLASS)
15093 {
15094 gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
15095 sym->name, &sym->declared_at);
15096 return false;
15097 }
15098
15099 return true;
15100 }
15101
15102
15103 /* Called by resolve_symbol to check PDTs. */
15104
15105 static void
15106 resolve_pdt (gfc_symbol* sym)
15107 {
15108 gfc_symbol *derived = NULL;
15109 gfc_actual_arglist *param;
15110 gfc_component *c;
15111 bool const_len_exprs = true;
15112 bool assumed_len_exprs = false;
15113 symbol_attribute *attr;
15114
15115 if (sym->ts.type == BT_DERIVED)
15116 {
15117 derived = sym->ts.u.derived;
15118 attr = &(sym->attr);
15119 }
15120 else if (sym->ts.type == BT_CLASS)
15121 {
15122 derived = CLASS_DATA (sym)->ts.u.derived;
15123 attr = &(CLASS_DATA (sym)->attr);
15124 }
15125 else
15126 gcc_unreachable ();
15127
15128 gcc_assert (derived->attr.pdt_type);
15129
15130 for (param = sym->param_list; param; param = param->next)
15131 {
15132 c = gfc_find_component (derived, param->name, false, true, NULL);
15133 gcc_assert (c);
15134 if (c->attr.pdt_kind)
15135 continue;
15136
15137 if (param->expr && !gfc_is_constant_expr (param->expr)
15138 && c->attr.pdt_len)
15139 const_len_exprs = false;
15140 else if (param->spec_type == SPEC_ASSUMED)
15141 assumed_len_exprs = true;
15142
15143 if (param->spec_type == SPEC_DEFERRED
15144 && !attr->allocatable && !attr->pointer)
15145 gfc_error ("The object %qs at %L has a deferred LEN "
15146 "parameter %qs and is neither allocatable "
15147 "nor a pointer", sym->name, &sym->declared_at,
15148 param->name);
15149
15150 }
15151
15152 if (!const_len_exprs
15153 && (sym->ns->proc_name->attr.is_main_program
15154 || sym->ns->proc_name->attr.flavor == FL_MODULE
15155 || sym->attr.save != SAVE_NONE))
15156 gfc_error ("The AUTOMATIC object %qs at %L must not have the "
15157 "SAVE attribute or be a variable declared in the "
15158 "main program, a module or a submodule(F08/C513)",
15159 sym->name, &sym->declared_at);
15160
15161 if (assumed_len_exprs && !(sym->attr.dummy
15162 || sym->attr.select_type_temporary || sym->attr.associate_var))
15163 gfc_error ("The object %qs at %L with ASSUMED type parameters "
15164 "must be a dummy or a SELECT TYPE selector(F08/4.2)",
15165 sym->name, &sym->declared_at);
15166 }
15167
15168
15169 /* Do anything necessary to resolve a symbol. Right now, we just
15170 assume that an otherwise unknown symbol is a variable. This sort
15171 of thing commonly happens for symbols in module. */
15172
15173 static void
15174 resolve_symbol (gfc_symbol *sym)
15175 {
15176 int check_constant, mp_flag;
15177 gfc_symtree *symtree;
15178 gfc_symtree *this_symtree;
15179 gfc_namespace *ns;
15180 gfc_component *c;
15181 symbol_attribute class_attr;
15182 gfc_array_spec *as;
15183 bool saved_specification_expr;
15184
15185 if (sym->resolve_symbol_called >= 1)
15186 return;
15187 sym->resolve_symbol_called = 1;
15188
15189 /* No symbol will ever have union type; only components can be unions.
15190 Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
15191 (just like derived type declaration symbols have flavor FL_DERIVED). */
15192 gcc_assert (sym->ts.type != BT_UNION);
15193
15194 /* Coarrayed polymorphic objects with allocatable or pointer components are
15195 yet unsupported for -fcoarray=lib. */
15196 if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
15197 && sym->ts.u.derived && CLASS_DATA (sym)
15198 && CLASS_DATA (sym)->attr.codimension
15199 && CLASS_DATA (sym)->ts.u.derived
15200 && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
15201 || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
15202 {
15203 gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
15204 "type coarrays at %L are unsupported", &sym->declared_at);
15205 return;
15206 }
15207
15208 if (sym->attr.artificial)
15209 return;
15210
15211 if (sym->attr.unlimited_polymorphic)
15212 return;
15213
15214 if (sym->attr.flavor == FL_UNKNOWN
15215 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
15216 && !sym->attr.generic && !sym->attr.external
15217 && sym->attr.if_source == IFSRC_UNKNOWN
15218 && sym->ts.type == BT_UNKNOWN))
15219 {
15220
15221 /* If we find that a flavorless symbol is an interface in one of the
15222 parent namespaces, find its symtree in this namespace, free the
15223 symbol and set the symtree to point to the interface symbol. */
15224 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
15225 {
15226 symtree = gfc_find_symtree (ns->sym_root, sym->name);
15227 if (symtree && (symtree->n.sym->generic ||
15228 (symtree->n.sym->attr.flavor == FL_PROCEDURE
15229 && sym->ns->construct_entities)))
15230 {
15231 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
15232 sym->name);
15233 if (this_symtree->n.sym == sym)
15234 {
15235 symtree->n.sym->refs++;
15236 gfc_release_symbol (sym);
15237 this_symtree->n.sym = symtree->n.sym;
15238 return;
15239 }
15240 }
15241 }
15242
15243 /* Otherwise give it a flavor according to such attributes as
15244 it has. */
15245 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
15246 && sym->attr.intrinsic == 0)
15247 sym->attr.flavor = FL_VARIABLE;
15248 else if (sym->attr.flavor == FL_UNKNOWN)
15249 {
15250 sym->attr.flavor = FL_PROCEDURE;
15251 if (sym->attr.dimension)
15252 sym->attr.function = 1;
15253 }
15254 }
15255
15256 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
15257 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
15258
15259 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
15260 && !resolve_procedure_interface (sym))
15261 return;
15262
15263 if (sym->attr.is_protected && !sym->attr.proc_pointer
15264 && (sym->attr.procedure || sym->attr.external))
15265 {
15266 if (sym->attr.external)
15267 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
15268 "at %L", &sym->declared_at);
15269 else
15270 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
15271 "at %L", &sym->declared_at);
15272
15273 return;
15274 }
15275
15276 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
15277 return;
15278
15279 else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
15280 && !resolve_fl_struct (sym))
15281 return;
15282
15283 /* Symbols that are module procedures with results (functions) have
15284 the types and array specification copied for type checking in
15285 procedures that call them, as well as for saving to a module
15286 file. These symbols can't stand the scrutiny that their results
15287 can. */
15288 mp_flag = (sym->result != NULL && sym->result != sym);
15289
15290 /* Make sure that the intrinsic is consistent with its internal
15291 representation. This needs to be done before assigning a default
15292 type to avoid spurious warnings. */
15293 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
15294 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
15295 return;
15296
15297 /* Resolve associate names. */
15298 if (sym->assoc)
15299 resolve_assoc_var (sym, true);
15300
15301 /* Assign default type to symbols that need one and don't have one. */
15302 if (sym->ts.type == BT_UNKNOWN)
15303 {
15304 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
15305 {
15306 gfc_set_default_type (sym, 1, NULL);
15307 }
15308
15309 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
15310 && !sym->attr.function && !sym->attr.subroutine
15311 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
15312 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
15313
15314 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
15315 {
15316 /* The specific case of an external procedure should emit an error
15317 in the case that there is no implicit type. */
15318 if (!mp_flag)
15319 {
15320 if (!sym->attr.mixed_entry_master)
15321 gfc_set_default_type (sym, sym->attr.external, NULL);
15322 }
15323 else
15324 {
15325 /* Result may be in another namespace. */
15326 resolve_symbol (sym->result);
15327
15328 if (!sym->result->attr.proc_pointer)
15329 {
15330 sym->ts = sym->result->ts;
15331 sym->as = gfc_copy_array_spec (sym->result->as);
15332 sym->attr.dimension = sym->result->attr.dimension;
15333 sym->attr.pointer = sym->result->attr.pointer;
15334 sym->attr.allocatable = sym->result->attr.allocatable;
15335 sym->attr.contiguous = sym->result->attr.contiguous;
15336 }
15337 }
15338 }
15339 }
15340 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
15341 {
15342 bool saved_specification_expr = specification_expr;
15343 specification_expr = true;
15344 gfc_resolve_array_spec (sym->result->as, false);
15345 specification_expr = saved_specification_expr;
15346 }
15347
15348 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
15349 {
15350 as = CLASS_DATA (sym)->as;
15351 class_attr = CLASS_DATA (sym)->attr;
15352 class_attr.pointer = class_attr.class_pointer;
15353 }
15354 else
15355 {
15356 class_attr = sym->attr;
15357 as = sym->as;
15358 }
15359
15360 /* F2008, C530. */
15361 if (sym->attr.contiguous
15362 && (!class_attr.dimension
15363 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
15364 && !class_attr.pointer)))
15365 {
15366 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
15367 "array pointer or an assumed-shape or assumed-rank array",
15368 sym->name, &sym->declared_at);
15369 return;
15370 }
15371
15372 /* Assumed size arrays and assumed shape arrays must be dummy
15373 arguments. Array-spec's of implied-shape should have been resolved to
15374 AS_EXPLICIT already. */
15375
15376 if (as)
15377 {
15378 /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
15379 specification expression. */
15380 if (as->type == AS_IMPLIED_SHAPE)
15381 {
15382 int i;
15383 for (i=0; i<as->rank; i++)
15384 {
15385 if (as->lower[i] != NULL && as->upper[i] == NULL)
15386 {
15387 gfc_error ("Bad specification for assumed size array at %L",
15388 &as->lower[i]->where);
15389 return;
15390 }
15391 }
15392 gcc_unreachable();
15393 }
15394
15395 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
15396 || as->type == AS_ASSUMED_SHAPE)
15397 && !sym->attr.dummy && !sym->attr.select_type_temporary)
15398 {
15399 if (as->type == AS_ASSUMED_SIZE)
15400 gfc_error ("Assumed size array at %L must be a dummy argument",
15401 &sym->declared_at);
15402 else
15403 gfc_error ("Assumed shape array at %L must be a dummy argument",
15404 &sym->declared_at);
15405 return;
15406 }
15407 /* TS 29113, C535a. */
15408 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
15409 && !sym->attr.select_type_temporary
15410 && !(cs_base && cs_base->current
15411 && cs_base->current->op == EXEC_SELECT_RANK))
15412 {
15413 gfc_error ("Assumed-rank array at %L must be a dummy argument",
15414 &sym->declared_at);
15415 return;
15416 }
15417 if (as->type == AS_ASSUMED_RANK
15418 && (sym->attr.codimension || sym->attr.value))
15419 {
15420 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
15421 "CODIMENSION attribute", &sym->declared_at);
15422 return;
15423 }
15424 }
15425
15426 /* Make sure symbols with known intent or optional are really dummy
15427 variable. Because of ENTRY statement, this has to be deferred
15428 until resolution time. */
15429
15430 if (!sym->attr.dummy
15431 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
15432 {
15433 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
15434 return;
15435 }
15436
15437 if (sym->attr.value && !sym->attr.dummy)
15438 {
15439 gfc_error ("%qs at %L cannot have the VALUE attribute because "
15440 "it is not a dummy argument", sym->name, &sym->declared_at);
15441 return;
15442 }
15443
15444 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
15445 {
15446 gfc_charlen *cl = sym->ts.u.cl;
15447 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
15448 {
15449 gfc_error ("Character dummy variable %qs at %L with VALUE "
15450 "attribute must have constant length",
15451 sym->name, &sym->declared_at);
15452 return;
15453 }
15454
15455 if (sym->ts.is_c_interop
15456 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
15457 {
15458 gfc_error ("C interoperable character dummy variable %qs at %L "
15459 "with VALUE attribute must have length one",
15460 sym->name, &sym->declared_at);
15461 return;
15462 }
15463 }
15464
15465 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
15466 && sym->ts.u.derived->attr.generic)
15467 {
15468 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
15469 if (!sym->ts.u.derived)
15470 {
15471 gfc_error ("The derived type %qs at %L is of type %qs, "
15472 "which has not been defined", sym->name,
15473 &sym->declared_at, sym->ts.u.derived->name);
15474 sym->ts.type = BT_UNKNOWN;
15475 return;
15476 }
15477 }
15478
15479 /* Use the same constraints as TYPE(*), except for the type check
15480 and that only scalars and assumed-size arrays are permitted. */
15481 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
15482 {
15483 if (!sym->attr.dummy)
15484 {
15485 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
15486 "a dummy argument", sym->name, &sym->declared_at);
15487 return;
15488 }
15489
15490 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
15491 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
15492 && sym->ts.type != BT_COMPLEX)
15493 {
15494 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
15495 "of type TYPE(*) or of an numeric intrinsic type",
15496 sym->name, &sym->declared_at);
15497 return;
15498 }
15499
15500 if (sym->attr.allocatable || sym->attr.codimension
15501 || sym->attr.pointer || sym->attr.value)
15502 {
15503 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
15504 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
15505 "attribute", sym->name, &sym->declared_at);
15506 return;
15507 }
15508
15509 if (sym->attr.intent == INTENT_OUT)
15510 {
15511 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
15512 "have the INTENT(OUT) attribute",
15513 sym->name, &sym->declared_at);
15514 return;
15515 }
15516 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
15517 {
15518 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
15519 "either be a scalar or an assumed-size array",
15520 sym->name, &sym->declared_at);
15521 return;
15522 }
15523
15524 /* Set the type to TYPE(*) and add a dimension(*) to ensure
15525 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
15526 packing. */
15527 sym->ts.type = BT_ASSUMED;
15528 sym->as = gfc_get_array_spec ();
15529 sym->as->type = AS_ASSUMED_SIZE;
15530 sym->as->rank = 1;
15531 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
15532 }
15533 else if (sym->ts.type == BT_ASSUMED)
15534 {
15535 /* TS 29113, C407a. */
15536 if (!sym->attr.dummy)
15537 {
15538 gfc_error ("Assumed type of variable %s at %L is only permitted "
15539 "for dummy variables", sym->name, &sym->declared_at);
15540 return;
15541 }
15542 if (sym->attr.allocatable || sym->attr.codimension
15543 || sym->attr.pointer || sym->attr.value)
15544 {
15545 gfc_error ("Assumed-type variable %s at %L may not have the "
15546 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
15547 sym->name, &sym->declared_at);
15548 return;
15549 }
15550 if (sym->attr.intent == INTENT_OUT)
15551 {
15552 gfc_error ("Assumed-type variable %s at %L may not have the "
15553 "INTENT(OUT) attribute",
15554 sym->name, &sym->declared_at);
15555 return;
15556 }
15557 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
15558 {
15559 gfc_error ("Assumed-type variable %s at %L shall not be an "
15560 "explicit-shape array", sym->name, &sym->declared_at);
15561 return;
15562 }
15563 }
15564
15565 /* If the symbol is marked as bind(c), that it is declared at module level
15566 scope and verify its type and kind. Do not do the latter for symbols
15567 that are implicitly typed because that is handled in
15568 gfc_set_default_type. Handle dummy arguments and procedure definitions
15569 separately. Also, anything that is use associated is not handled here
15570 but instead is handled in the module it is declared in. Finally, derived
15571 type definitions are allowed to be BIND(C) since that only implies that
15572 they're interoperable, and they are checked fully for interoperability
15573 when a variable is declared of that type. */
15574 if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
15575 && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
15576 && sym->attr.flavor != FL_DERIVED)
15577 {
15578 bool t = true;
15579
15580 /* First, make sure the variable is declared at the
15581 module-level scope (J3/04-007, Section 15.3). */
15582 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
15583 sym->attr.in_common == 0)
15584 {
15585 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
15586 "is neither a COMMON block nor declared at the "
15587 "module level scope", sym->name, &(sym->declared_at));
15588 t = false;
15589 }
15590 else if (sym->ts.type == BT_CHARACTER
15591 && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
15592 || !gfc_is_constant_expr (sym->ts.u.cl->length)
15593 || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
15594 {
15595 gfc_error ("BIND(C) Variable %qs at %L must have length one",
15596 sym->name, &sym->declared_at);
15597 t = false;
15598 }
15599 else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
15600 {
15601 t = verify_com_block_vars_c_interop (sym->common_head);
15602 }
15603 else if (sym->attr.implicit_type == 0)
15604 {
15605 /* If type() declaration, we need to verify that the components
15606 of the given type are all C interoperable, etc. */
15607 if (sym->ts.type == BT_DERIVED &&
15608 sym->ts.u.derived->attr.is_c_interop != 1)
15609 {
15610 /* Make sure the user marked the derived type as BIND(C). If
15611 not, call the verify routine. This could print an error
15612 for the derived type more than once if multiple variables
15613 of that type are declared. */
15614 if (sym->ts.u.derived->attr.is_bind_c != 1)
15615 verify_bind_c_derived_type (sym->ts.u.derived);
15616 t = false;
15617 }
15618
15619 /* Verify the variable itself as C interoperable if it
15620 is BIND(C). It is not possible for this to succeed if
15621 the verify_bind_c_derived_type failed, so don't have to handle
15622 any error returned by verify_bind_c_derived_type. */
15623 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
15624 sym->common_block);
15625 }
15626
15627 if (!t)
15628 {
15629 /* clear the is_bind_c flag to prevent reporting errors more than
15630 once if something failed. */
15631 sym->attr.is_bind_c = 0;
15632 return;
15633 }
15634 }
15635
15636 /* If a derived type symbol has reached this point, without its
15637 type being declared, we have an error. Notice that most
15638 conditions that produce undefined derived types have already
15639 been dealt with. However, the likes of:
15640 implicit type(t) (t) ..... call foo (t) will get us here if
15641 the type is not declared in the scope of the implicit
15642 statement. Change the type to BT_UNKNOWN, both because it is so
15643 and to prevent an ICE. */
15644 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
15645 && sym->ts.u.derived->components == NULL
15646 && !sym->ts.u.derived->attr.zero_comp)
15647 {
15648 gfc_error ("The derived type %qs at %L is of type %qs, "
15649 "which has not been defined", sym->name,
15650 &sym->declared_at, sym->ts.u.derived->name);
15651 sym->ts.type = BT_UNKNOWN;
15652 return;
15653 }
15654
15655 /* Make sure that the derived type has been resolved and that the
15656 derived type is visible in the symbol's namespace, if it is a
15657 module function and is not PRIVATE. */
15658 if (sym->ts.type == BT_DERIVED
15659 && sym->ts.u.derived->attr.use_assoc
15660 && sym->ns->proc_name
15661 && sym->ns->proc_name->attr.flavor == FL_MODULE
15662 && !resolve_fl_derived (sym->ts.u.derived))
15663 return;
15664
15665 /* Unless the derived-type declaration is use associated, Fortran 95
15666 does not allow public entries of private derived types.
15667 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
15668 161 in 95-006r3. */
15669 if (sym->ts.type == BT_DERIVED
15670 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
15671 && !sym->ts.u.derived->attr.use_assoc
15672 && gfc_check_symbol_access (sym)
15673 && !gfc_check_symbol_access (sym->ts.u.derived)
15674 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
15675 "derived type %qs",
15676 (sym->attr.flavor == FL_PARAMETER)
15677 ? "parameter" : "variable",
15678 sym->name, &sym->declared_at,
15679 sym->ts.u.derived->name))
15680 return;
15681
15682 /* F2008, C1302. */
15683 if (sym->ts.type == BT_DERIVED
15684 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
15685 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
15686 || sym->ts.u.derived->attr.lock_comp)
15687 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
15688 {
15689 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
15690 "type LOCK_TYPE must be a coarray", sym->name,
15691 &sym->declared_at);
15692 return;
15693 }
15694
15695 /* TS18508, C702/C703. */
15696 if (sym->ts.type == BT_DERIVED
15697 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
15698 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
15699 || sym->ts.u.derived->attr.event_comp)
15700 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
15701 {
15702 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
15703 "type EVENT_TYPE must be a coarray", sym->name,
15704 &sym->declared_at);
15705 return;
15706 }
15707
15708 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
15709 default initialization is defined (5.1.2.4.4). */
15710 if (sym->ts.type == BT_DERIVED
15711 && sym->attr.dummy
15712 && sym->attr.intent == INTENT_OUT
15713 && sym->as
15714 && sym->as->type == AS_ASSUMED_SIZE)
15715 {
15716 for (c = sym->ts.u.derived->components; c; c = c->next)
15717 {
15718 if (c->initializer)
15719 {
15720 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
15721 "ASSUMED SIZE and so cannot have a default initializer",
15722 sym->name, &sym->declared_at);
15723 return;
15724 }
15725 }
15726 }
15727
15728 /* F2008, C542. */
15729 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
15730 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
15731 {
15732 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
15733 "INTENT(OUT)", sym->name, &sym->declared_at);
15734 return;
15735 }
15736
15737 /* TS18508. */
15738 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
15739 && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
15740 {
15741 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
15742 "INTENT(OUT)", sym->name, &sym->declared_at);
15743 return;
15744 }
15745
15746 /* F2008, C525. */
15747 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15748 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15749 && CLASS_DATA (sym)->attr.coarray_comp))
15750 || class_attr.codimension)
15751 && (sym->attr.result || sym->result == sym))
15752 {
15753 gfc_error ("Function result %qs at %L shall not be a coarray or have "
15754 "a coarray component", sym->name, &sym->declared_at);
15755 return;
15756 }
15757
15758 /* F2008, C524. */
15759 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
15760 && sym->ts.u.derived->ts.is_iso_c)
15761 {
15762 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
15763 "shall not be a coarray", sym->name, &sym->declared_at);
15764 return;
15765 }
15766
15767 /* F2008, C525. */
15768 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15769 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15770 && CLASS_DATA (sym)->attr.coarray_comp))
15771 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
15772 || class_attr.allocatable))
15773 {
15774 gfc_error ("Variable %qs at %L with coarray component shall be a "
15775 "nonpointer, nonallocatable scalar, which is not a coarray",
15776 sym->name, &sym->declared_at);
15777 return;
15778 }
15779
15780 /* F2008, C526. The function-result case was handled above. */
15781 if (class_attr.codimension
15782 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
15783 || sym->attr.select_type_temporary
15784 || sym->attr.associate_var
15785 || (sym->ns->save_all && !sym->attr.automatic)
15786 || sym->ns->proc_name->attr.flavor == FL_MODULE
15787 || sym->ns->proc_name->attr.is_main_program
15788 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
15789 {
15790 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
15791 "nor a dummy argument", sym->name, &sym->declared_at);
15792 return;
15793 }
15794 /* F2008, C528. */
15795 else if (class_attr.codimension && !sym->attr.select_type_temporary
15796 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
15797 {
15798 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
15799 "deferred shape", sym->name, &sym->declared_at);
15800 return;
15801 }
15802 else if (class_attr.codimension && class_attr.allocatable && as
15803 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
15804 {
15805 gfc_error ("Allocatable coarray variable %qs at %L must have "
15806 "deferred shape", sym->name, &sym->declared_at);
15807 return;
15808 }
15809
15810 /* F2008, C541. */
15811 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15812 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15813 && CLASS_DATA (sym)->attr.coarray_comp))
15814 || (class_attr.codimension && class_attr.allocatable))
15815 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
15816 {
15817 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
15818 "allocatable coarray or have coarray components",
15819 sym->name, &sym->declared_at);
15820 return;
15821 }
15822
15823 if (class_attr.codimension && sym->attr.dummy
15824 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
15825 {
15826 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
15827 "procedure %qs", sym->name, &sym->declared_at,
15828 sym->ns->proc_name->name);
15829 return;
15830 }
15831
15832 if (sym->ts.type == BT_LOGICAL
15833 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
15834 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
15835 && sym->ns->proc_name->attr.is_bind_c)))
15836 {
15837 int i;
15838 for (i = 0; gfc_logical_kinds[i].kind; i++)
15839 if (gfc_logical_kinds[i].kind == sym->ts.kind)
15840 break;
15841 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
15842 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
15843 "%L with non-C_Bool kind in BIND(C) procedure "
15844 "%qs", sym->name, &sym->declared_at,
15845 sym->ns->proc_name->name))
15846 return;
15847 else if (!gfc_logical_kinds[i].c_bool
15848 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
15849 "%qs at %L with non-C_Bool kind in "
15850 "BIND(C) procedure %qs", sym->name,
15851 &sym->declared_at,
15852 sym->attr.function ? sym->name
15853 : sym->ns->proc_name->name))
15854 return;
15855 }
15856
15857 switch (sym->attr.flavor)
15858 {
15859 case FL_VARIABLE:
15860 if (!resolve_fl_variable (sym, mp_flag))
15861 return;
15862 break;
15863
15864 case FL_PROCEDURE:
15865 if (sym->formal && !sym->formal_ns)
15866 {
15867 /* Check that none of the arguments are a namelist. */
15868 gfc_formal_arglist *formal = sym->formal;
15869
15870 for (; formal; formal = formal->next)
15871 if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
15872 {
15873 gfc_error ("Namelist %qs cannot be an argument to "
15874 "subroutine or function at %L",
15875 formal->sym->name, &sym->declared_at);
15876 return;
15877 }
15878 }
15879
15880 if (!resolve_fl_procedure (sym, mp_flag))
15881 return;
15882 break;
15883
15884 case FL_NAMELIST:
15885 if (!resolve_fl_namelist (sym))
15886 return;
15887 break;
15888
15889 case FL_PARAMETER:
15890 if (!resolve_fl_parameter (sym))
15891 return;
15892 break;
15893
15894 default:
15895 break;
15896 }
15897
15898 /* Resolve array specifier. Check as well some constraints
15899 on COMMON blocks. */
15900
15901 check_constant = sym->attr.in_common && !sym->attr.pointer;
15902
15903 /* Set the formal_arg_flag so that check_conflict will not throw
15904 an error for host associated variables in the specification
15905 expression for an array_valued function. */
15906 if ((sym->attr.function || sym->attr.result) && sym->as)
15907 formal_arg_flag = true;
15908
15909 saved_specification_expr = specification_expr;
15910 specification_expr = true;
15911 gfc_resolve_array_spec (sym->as, check_constant);
15912 specification_expr = saved_specification_expr;
15913
15914 formal_arg_flag = false;
15915
15916 /* Resolve formal namespaces. */
15917 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
15918 && !sym->attr.contained && !sym->attr.intrinsic)
15919 gfc_resolve (sym->formal_ns);
15920
15921 /* Make sure the formal namespace is present. */
15922 if (sym->formal && !sym->formal_ns)
15923 {
15924 gfc_formal_arglist *formal = sym->formal;
15925 while (formal && !formal->sym)
15926 formal = formal->next;
15927
15928 if (formal)
15929 {
15930 sym->formal_ns = formal->sym->ns;
15931 if (sym->formal_ns && sym->ns != formal->sym->ns)
15932 sym->formal_ns->refs++;
15933 }
15934 }
15935
15936 /* Check threadprivate restrictions. */
15937 if (sym->attr.threadprivate && !sym->attr.save
15938 && !(sym->ns->save_all && !sym->attr.automatic)
15939 && (!sym->attr.in_common
15940 && sym->module == NULL
15941 && (sym->ns->proc_name == NULL
15942 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
15943 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
15944
15945 /* Check omp declare target restrictions. */
15946 if (sym->attr.omp_declare_target
15947 && sym->attr.flavor == FL_VARIABLE
15948 && !sym->attr.save
15949 && !(sym->ns->save_all && !sym->attr.automatic)
15950 && (!sym->attr.in_common
15951 && sym->module == NULL
15952 && (sym->ns->proc_name == NULL
15953 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
15954 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
15955 sym->name, &sym->declared_at);
15956
15957 /* If we have come this far we can apply default-initializers, as
15958 described in 14.7.5, to those variables that have not already
15959 been assigned one. */
15960 if (sym->ts.type == BT_DERIVED
15961 && !sym->value
15962 && !sym->attr.allocatable
15963 && !sym->attr.alloc_comp)
15964 {
15965 symbol_attribute *a = &sym->attr;
15966
15967 if ((!a->save && !a->dummy && !a->pointer
15968 && !a->in_common && !a->use_assoc
15969 && a->referenced
15970 && !((a->function || a->result)
15971 && (!a->dimension
15972 || sym->ts.u.derived->attr.alloc_comp
15973 || sym->ts.u.derived->attr.pointer_comp))
15974 && !(a->function && sym != sym->result))
15975 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
15976 apply_default_init (sym);
15977 else if (a->function && sym->result && a->access != ACCESS_PRIVATE
15978 && (sym->ts.u.derived->attr.alloc_comp
15979 || sym->ts.u.derived->attr.pointer_comp))
15980 /* Mark the result symbol to be referenced, when it has allocatable
15981 components. */
15982 sym->result->attr.referenced = 1;
15983 }
15984
15985 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
15986 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
15987 && !CLASS_DATA (sym)->attr.class_pointer
15988 && !CLASS_DATA (sym)->attr.allocatable)
15989 apply_default_init (sym);
15990
15991 /* If this symbol has a type-spec, check it. */
15992 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
15993 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
15994 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
15995 return;
15996
15997 if (sym->param_list)
15998 resolve_pdt (sym);
15999 }
16000
16001
16002 /************* Resolve DATA statements *************/
16003
16004 static struct
16005 {
16006 gfc_data_value *vnode;
16007 mpz_t left;
16008 }
16009 values;
16010
16011
16012 /* Advance the values structure to point to the next value in the data list. */
16013
16014 static bool
16015 next_data_value (void)
16016 {
16017 while (mpz_cmp_ui (values.left, 0) == 0)
16018 {
16019
16020 if (values.vnode->next == NULL)
16021 return false;
16022
16023 values.vnode = values.vnode->next;
16024 mpz_set (values.left, values.vnode->repeat);
16025 }
16026
16027 return true;
16028 }
16029
16030
16031 static bool
16032 check_data_variable (gfc_data_variable *var, locus *where)
16033 {
16034 gfc_expr *e;
16035 mpz_t size;
16036 mpz_t offset;
16037 bool t;
16038 ar_type mark = AR_UNKNOWN;
16039 int i;
16040 mpz_t section_index[GFC_MAX_DIMENSIONS];
16041 gfc_ref *ref;
16042 gfc_array_ref *ar;
16043 gfc_symbol *sym;
16044 int has_pointer;
16045
16046 if (!gfc_resolve_expr (var->expr))
16047 return false;
16048
16049 ar = NULL;
16050 mpz_init_set_si (offset, 0);
16051 e = var->expr;
16052
16053 if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
16054 && e->value.function.isym->id == GFC_ISYM_CAF_GET)
16055 e = e->value.function.actual->expr;
16056
16057 if (e->expr_type != EXPR_VARIABLE)
16058 {
16059 gfc_error ("Expecting definable entity near %L", where);
16060 return false;
16061 }
16062
16063 sym = e->symtree->n.sym;
16064
16065 if (sym->ns->is_block_data && !sym->attr.in_common)
16066 {
16067 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
16068 sym->name, &sym->declared_at);
16069 return false;
16070 }
16071
16072 if (e->ref == NULL && sym->as)
16073 {
16074 gfc_error ("DATA array %qs at %L must be specified in a previous"
16075 " declaration", sym->name, where);
16076 return false;
16077 }
16078
16079 if (gfc_is_coindexed (e))
16080 {
16081 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
16082 where);
16083 return false;
16084 }
16085
16086 has_pointer = sym->attr.pointer;
16087
16088 for (ref = e->ref; ref; ref = ref->next)
16089 {
16090 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
16091 has_pointer = 1;
16092
16093 if (has_pointer)
16094 {
16095 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
16096 {
16097 gfc_error ("DATA element %qs at %L is a pointer and so must "
16098 "be a full array", sym->name, where);
16099 return false;
16100 }
16101
16102 if (values.vnode->expr->expr_type == EXPR_CONSTANT)
16103 {
16104 gfc_error ("DATA object near %L has the pointer attribute "
16105 "and the corresponding DATA value is not a valid "
16106 "initial-data-target", where);
16107 return false;
16108 }
16109 }
16110 }
16111
16112 if (e->rank == 0 || has_pointer)
16113 {
16114 mpz_init_set_ui (size, 1);
16115 ref = NULL;
16116 }
16117 else
16118 {
16119 ref = e->ref;
16120
16121 /* Find the array section reference. */
16122 for (ref = e->ref; ref; ref = ref->next)
16123 {
16124 if (ref->type != REF_ARRAY)
16125 continue;
16126 if (ref->u.ar.type == AR_ELEMENT)
16127 continue;
16128 break;
16129 }
16130 gcc_assert (ref);
16131
16132 /* Set marks according to the reference pattern. */
16133 switch (ref->u.ar.type)
16134 {
16135 case AR_FULL:
16136 mark = AR_FULL;
16137 break;
16138
16139 case AR_SECTION:
16140 ar = &ref->u.ar;
16141 /* Get the start position of array section. */
16142 gfc_get_section_index (ar, section_index, &offset);
16143 mark = AR_SECTION;
16144 break;
16145
16146 default:
16147 gcc_unreachable ();
16148 }
16149
16150 if (!gfc_array_size (e, &size))
16151 {
16152 gfc_error ("Nonconstant array section at %L in DATA statement",
16153 where);
16154 mpz_clear (offset);
16155 return false;
16156 }
16157 }
16158
16159 t = true;
16160
16161 while (mpz_cmp_ui (size, 0) > 0)
16162 {
16163 if (!next_data_value ())
16164 {
16165 gfc_error ("DATA statement at %L has more variables than values",
16166 where);
16167 t = false;
16168 break;
16169 }
16170
16171 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
16172 if (!t)
16173 break;
16174
16175 /* If we have more than one element left in the repeat count,
16176 and we have more than one element left in the target variable,
16177 then create a range assignment. */
16178 /* FIXME: Only done for full arrays for now, since array sections
16179 seem tricky. */
16180 if (mark == AR_FULL && ref && ref->next == NULL
16181 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
16182 {
16183 mpz_t range;
16184
16185 if (mpz_cmp (size, values.left) >= 0)
16186 {
16187 mpz_init_set (range, values.left);
16188 mpz_sub (size, size, values.left);
16189 mpz_set_ui (values.left, 0);
16190 }
16191 else
16192 {
16193 mpz_init_set (range, size);
16194 mpz_sub (values.left, values.left, size);
16195 mpz_set_ui (size, 0);
16196 }
16197
16198 t = gfc_assign_data_value (var->expr, values.vnode->expr,
16199 offset, &range);
16200
16201 mpz_add (offset, offset, range);
16202 mpz_clear (range);
16203
16204 if (!t)
16205 break;
16206 }
16207
16208 /* Assign initial value to symbol. */
16209 else
16210 {
16211 mpz_sub_ui (values.left, values.left, 1);
16212 mpz_sub_ui (size, size, 1);
16213
16214 t = gfc_assign_data_value (var->expr, values.vnode->expr,
16215 offset, NULL);
16216 if (!t)
16217 break;
16218
16219 if (mark == AR_FULL)
16220 mpz_add_ui (offset, offset, 1);
16221
16222 /* Modify the array section indexes and recalculate the offset
16223 for next element. */
16224 else if (mark == AR_SECTION)
16225 gfc_advance_section (section_index, ar, &offset);
16226 }
16227 }
16228
16229 if (mark == AR_SECTION)
16230 {
16231 for (i = 0; i < ar->dimen; i++)
16232 mpz_clear (section_index[i]);
16233 }
16234
16235 mpz_clear (size);
16236 mpz_clear (offset);
16237
16238 return t;
16239 }
16240
16241
16242 static bool traverse_data_var (gfc_data_variable *, locus *);
16243
16244 /* Iterate over a list of elements in a DATA statement. */
16245
16246 static bool
16247 traverse_data_list (gfc_data_variable *var, locus *where)
16248 {
16249 mpz_t trip;
16250 iterator_stack frame;
16251 gfc_expr *e, *start, *end, *step;
16252 bool retval = true;
16253
16254 mpz_init (frame.value);
16255 mpz_init (trip);
16256
16257 start = gfc_copy_expr (var->iter.start);
16258 end = gfc_copy_expr (var->iter.end);
16259 step = gfc_copy_expr (var->iter.step);
16260
16261 if (!gfc_simplify_expr (start, 1)
16262 || start->expr_type != EXPR_CONSTANT)
16263 {
16264 gfc_error ("start of implied-do loop at %L could not be "
16265 "simplified to a constant value", &start->where);
16266 retval = false;
16267 goto cleanup;
16268 }
16269 if (!gfc_simplify_expr (end, 1)
16270 || end->expr_type != EXPR_CONSTANT)
16271 {
16272 gfc_error ("end of implied-do loop at %L could not be "
16273 "simplified to a constant value", &start->where);
16274 retval = false;
16275 goto cleanup;
16276 }
16277 if (!gfc_simplify_expr (step, 1)
16278 || step->expr_type != EXPR_CONSTANT)
16279 {
16280 gfc_error ("step of implied-do loop at %L could not be "
16281 "simplified to a constant value", &start->where);
16282 retval = false;
16283 goto cleanup;
16284 }
16285
16286 mpz_set (trip, end->value.integer);
16287 mpz_sub (trip, trip, start->value.integer);
16288 mpz_add (trip, trip, step->value.integer);
16289
16290 mpz_div (trip, trip, step->value.integer);
16291
16292 mpz_set (frame.value, start->value.integer);
16293
16294 frame.prev = iter_stack;
16295 frame.variable = var->iter.var->symtree;
16296 iter_stack = &frame;
16297
16298 while (mpz_cmp_ui (trip, 0) > 0)
16299 {
16300 if (!traverse_data_var (var->list, where))
16301 {
16302 retval = false;
16303 goto cleanup;
16304 }
16305
16306 e = gfc_copy_expr (var->expr);
16307 if (!gfc_simplify_expr (e, 1))
16308 {
16309 gfc_free_expr (e);
16310 retval = false;
16311 goto cleanup;
16312 }
16313
16314 mpz_add (frame.value, frame.value, step->value.integer);
16315
16316 mpz_sub_ui (trip, trip, 1);
16317 }
16318
16319 cleanup:
16320 mpz_clear (frame.value);
16321 mpz_clear (trip);
16322
16323 gfc_free_expr (start);
16324 gfc_free_expr (end);
16325 gfc_free_expr (step);
16326
16327 iter_stack = frame.prev;
16328 return retval;
16329 }
16330
16331
16332 /* Type resolve variables in the variable list of a DATA statement. */
16333
16334 static bool
16335 traverse_data_var (gfc_data_variable *var, locus *where)
16336 {
16337 bool t;
16338
16339 for (; var; var = var->next)
16340 {
16341 if (var->expr == NULL)
16342 t = traverse_data_list (var, where);
16343 else
16344 t = check_data_variable (var, where);
16345
16346 if (!t)
16347 return false;
16348 }
16349
16350 return true;
16351 }
16352
16353
16354 /* Resolve the expressions and iterators associated with a data statement.
16355 This is separate from the assignment checking because data lists should
16356 only be resolved once. */
16357
16358 static bool
16359 resolve_data_variables (gfc_data_variable *d)
16360 {
16361 for (; d; d = d->next)
16362 {
16363 if (d->list == NULL)
16364 {
16365 if (!gfc_resolve_expr (d->expr))
16366 return false;
16367 }
16368 else
16369 {
16370 if (!gfc_resolve_iterator (&d->iter, false, true))
16371 return false;
16372
16373 if (!resolve_data_variables (d->list))
16374 return false;
16375 }
16376 }
16377
16378 return true;
16379 }
16380
16381
16382 /* Resolve a single DATA statement. We implement this by storing a pointer to
16383 the value list into static variables, and then recursively traversing the
16384 variables list, expanding iterators and such. */
16385
16386 static void
16387 resolve_data (gfc_data *d)
16388 {
16389
16390 if (!resolve_data_variables (d->var))
16391 return;
16392
16393 values.vnode = d->value;
16394 if (d->value == NULL)
16395 mpz_set_ui (values.left, 0);
16396 else
16397 mpz_set (values.left, d->value->repeat);
16398
16399 if (!traverse_data_var (d->var, &d->where))
16400 return;
16401
16402 /* At this point, we better not have any values left. */
16403
16404 if (next_data_value ())
16405 gfc_error ("DATA statement at %L has more values than variables",
16406 &d->where);
16407 }
16408
16409
16410 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
16411 accessed by host or use association, is a dummy argument to a pure function,
16412 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
16413 is storage associated with any such variable, shall not be used in the
16414 following contexts: (clients of this function). */
16415
16416 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
16417 procedure. Returns zero if assignment is OK, nonzero if there is a
16418 problem. */
16419 int
16420 gfc_impure_variable (gfc_symbol *sym)
16421 {
16422 gfc_symbol *proc;
16423 gfc_namespace *ns;
16424
16425 if (sym->attr.use_assoc || sym->attr.in_common)
16426 return 1;
16427
16428 /* Check if the symbol's ns is inside the pure procedure. */
16429 for (ns = gfc_current_ns; ns; ns = ns->parent)
16430 {
16431 if (ns == sym->ns)
16432 break;
16433 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
16434 return 1;
16435 }
16436
16437 proc = sym->ns->proc_name;
16438 if (sym->attr.dummy
16439 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
16440 || proc->attr.function))
16441 return 1;
16442
16443 /* TODO: Sort out what can be storage associated, if anything, and include
16444 it here. In principle equivalences should be scanned but it does not
16445 seem to be possible to storage associate an impure variable this way. */
16446 return 0;
16447 }
16448
16449
16450 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
16451 current namespace is inside a pure procedure. */
16452
16453 int
16454 gfc_pure (gfc_symbol *sym)
16455 {
16456 symbol_attribute attr;
16457 gfc_namespace *ns;
16458
16459 if (sym == NULL)
16460 {
16461 /* Check if the current namespace or one of its parents
16462 belongs to a pure procedure. */
16463 for (ns = gfc_current_ns; ns; ns = ns->parent)
16464 {
16465 sym = ns->proc_name;
16466 if (sym == NULL)
16467 return 0;
16468 attr = sym->attr;
16469 if (attr.flavor == FL_PROCEDURE && attr.pure)
16470 return 1;
16471 }
16472 return 0;
16473 }
16474
16475 attr = sym->attr;
16476
16477 return attr.flavor == FL_PROCEDURE && attr.pure;
16478 }
16479
16480
16481 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
16482 checks if the current namespace is implicitly pure. Note that this
16483 function returns false for a PURE procedure. */
16484
16485 int
16486 gfc_implicit_pure (gfc_symbol *sym)
16487 {
16488 gfc_namespace *ns;
16489
16490 if (sym == NULL)
16491 {
16492 /* Check if the current procedure is implicit_pure. Walk up
16493 the procedure list until we find a procedure. */
16494 for (ns = gfc_current_ns; ns; ns = ns->parent)
16495 {
16496 sym = ns->proc_name;
16497 if (sym == NULL)
16498 return 0;
16499
16500 if (sym->attr.flavor == FL_PROCEDURE)
16501 break;
16502 }
16503 }
16504
16505 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
16506 && !sym->attr.pure;
16507 }
16508
16509
16510 void
16511 gfc_unset_implicit_pure (gfc_symbol *sym)
16512 {
16513 gfc_namespace *ns;
16514
16515 if (sym == NULL)
16516 {
16517 /* Check if the current procedure is implicit_pure. Walk up
16518 the procedure list until we find a procedure. */
16519 for (ns = gfc_current_ns; ns; ns = ns->parent)
16520 {
16521 sym = ns->proc_name;
16522 if (sym == NULL)
16523 return;
16524
16525 if (sym->attr.flavor == FL_PROCEDURE)
16526 break;
16527 }
16528 }
16529
16530 if (sym->attr.flavor == FL_PROCEDURE)
16531 sym->attr.implicit_pure = 0;
16532 else
16533 sym->attr.pure = 0;
16534 }
16535
16536
16537 /* Test whether the current procedure is elemental or not. */
16538
16539 int
16540 gfc_elemental (gfc_symbol *sym)
16541 {
16542 symbol_attribute attr;
16543
16544 if (sym == NULL)
16545 sym = gfc_current_ns->proc_name;
16546 if (sym == NULL)
16547 return 0;
16548 attr = sym->attr;
16549
16550 return attr.flavor == FL_PROCEDURE && attr.elemental;
16551 }
16552
16553
16554 /* Warn about unused labels. */
16555
16556 static void
16557 warn_unused_fortran_label (gfc_st_label *label)
16558 {
16559 if (label == NULL)
16560 return;
16561
16562 warn_unused_fortran_label (label->left);
16563
16564 if (label->defined == ST_LABEL_UNKNOWN)
16565 return;
16566
16567 switch (label->referenced)
16568 {
16569 case ST_LABEL_UNKNOWN:
16570 gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
16571 label->value, &label->where);
16572 break;
16573
16574 case ST_LABEL_BAD_TARGET:
16575 gfc_warning (OPT_Wunused_label,
16576 "Label %d at %L defined but cannot be used",
16577 label->value, &label->where);
16578 break;
16579
16580 default:
16581 break;
16582 }
16583
16584 warn_unused_fortran_label (label->right);
16585 }
16586
16587
16588 /* Returns the sequence type of a symbol or sequence. */
16589
16590 static seq_type
16591 sequence_type (gfc_typespec ts)
16592 {
16593 seq_type result;
16594 gfc_component *c;
16595
16596 switch (ts.type)
16597 {
16598 case BT_DERIVED:
16599
16600 if (ts.u.derived->components == NULL)
16601 return SEQ_NONDEFAULT;
16602
16603 result = sequence_type (ts.u.derived->components->ts);
16604 for (c = ts.u.derived->components->next; c; c = c->next)
16605 if (sequence_type (c->ts) != result)
16606 return SEQ_MIXED;
16607
16608 return result;
16609
16610 case BT_CHARACTER:
16611 if (ts.kind != gfc_default_character_kind)
16612 return SEQ_NONDEFAULT;
16613
16614 return SEQ_CHARACTER;
16615
16616 case BT_INTEGER:
16617 if (ts.kind != gfc_default_integer_kind)
16618 return SEQ_NONDEFAULT;
16619
16620 return SEQ_NUMERIC;
16621
16622 case BT_REAL:
16623 if (!(ts.kind == gfc_default_real_kind
16624 || ts.kind == gfc_default_double_kind))
16625 return SEQ_NONDEFAULT;
16626
16627 return SEQ_NUMERIC;
16628
16629 case BT_COMPLEX:
16630 if (ts.kind != gfc_default_complex_kind)
16631 return SEQ_NONDEFAULT;
16632
16633 return SEQ_NUMERIC;
16634
16635 case BT_LOGICAL:
16636 if (ts.kind != gfc_default_logical_kind)
16637 return SEQ_NONDEFAULT;
16638
16639 return SEQ_NUMERIC;
16640
16641 default:
16642 return SEQ_NONDEFAULT;
16643 }
16644 }
16645
16646
16647 /* Resolve derived type EQUIVALENCE object. */
16648
16649 static bool
16650 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
16651 {
16652 gfc_component *c = derived->components;
16653
16654 if (!derived)
16655 return true;
16656
16657 /* Shall not be an object of nonsequence derived type. */
16658 if (!derived->attr.sequence)
16659 {
16660 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
16661 "attribute to be an EQUIVALENCE object", sym->name,
16662 &e->where);
16663 return false;
16664 }
16665
16666 /* Shall not have allocatable components. */
16667 if (derived->attr.alloc_comp)
16668 {
16669 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
16670 "components to be an EQUIVALENCE object",sym->name,
16671 &e->where);
16672 return false;
16673 }
16674
16675 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
16676 {
16677 gfc_error ("Derived type variable %qs at %L with default "
16678 "initialization cannot be in EQUIVALENCE with a variable "
16679 "in COMMON", sym->name, &e->where);
16680 return false;
16681 }
16682
16683 for (; c ; c = c->next)
16684 {
16685 if (gfc_bt_struct (c->ts.type)
16686 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
16687 return false;
16688
16689 /* Shall not be an object of sequence derived type containing a pointer
16690 in the structure. */
16691 if (c->attr.pointer)
16692 {
16693 gfc_error ("Derived type variable %qs at %L with pointer "
16694 "component(s) cannot be an EQUIVALENCE object",
16695 sym->name, &e->where);
16696 return false;
16697 }
16698 }
16699 return true;
16700 }
16701
16702
16703 /* Resolve equivalence object.
16704 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
16705 an allocatable array, an object of nonsequence derived type, an object of
16706 sequence derived type containing a pointer at any level of component
16707 selection, an automatic object, a function name, an entry name, a result
16708 name, a named constant, a structure component, or a subobject of any of
16709 the preceding objects. A substring shall not have length zero. A
16710 derived type shall not have components with default initialization nor
16711 shall two objects of an equivalence group be initialized.
16712 Either all or none of the objects shall have an protected attribute.
16713 The simple constraints are done in symbol.c(check_conflict) and the rest
16714 are implemented here. */
16715
16716 static void
16717 resolve_equivalence (gfc_equiv *eq)
16718 {
16719 gfc_symbol *sym;
16720 gfc_symbol *first_sym;
16721 gfc_expr *e;
16722 gfc_ref *r;
16723 locus *last_where = NULL;
16724 seq_type eq_type, last_eq_type;
16725 gfc_typespec *last_ts;
16726 int object, cnt_protected;
16727 const char *msg;
16728
16729 last_ts = &eq->expr->symtree->n.sym->ts;
16730
16731 first_sym = eq->expr->symtree->n.sym;
16732
16733 cnt_protected = 0;
16734
16735 for (object = 1; eq; eq = eq->eq, object++)
16736 {
16737 e = eq->expr;
16738
16739 e->ts = e->symtree->n.sym->ts;
16740 /* match_varspec might not know yet if it is seeing
16741 array reference or substring reference, as it doesn't
16742 know the types. */
16743 if (e->ref && e->ref->type == REF_ARRAY)
16744 {
16745 gfc_ref *ref = e->ref;
16746 sym = e->symtree->n.sym;
16747
16748 if (sym->attr.dimension)
16749 {
16750 ref->u.ar.as = sym->as;
16751 ref = ref->next;
16752 }
16753
16754 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
16755 if (e->ts.type == BT_CHARACTER
16756 && ref
16757 && ref->type == REF_ARRAY
16758 && ref->u.ar.dimen == 1
16759 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
16760 && ref->u.ar.stride[0] == NULL)
16761 {
16762 gfc_expr *start = ref->u.ar.start[0];
16763 gfc_expr *end = ref->u.ar.end[0];
16764 void *mem = NULL;
16765
16766 /* Optimize away the (:) reference. */
16767 if (start == NULL && end == NULL)
16768 {
16769 if (e->ref == ref)
16770 e->ref = ref->next;
16771 else
16772 e->ref->next = ref->next;
16773 mem = ref;
16774 }
16775 else
16776 {
16777 ref->type = REF_SUBSTRING;
16778 if (start == NULL)
16779 start = gfc_get_int_expr (gfc_charlen_int_kind,
16780 NULL, 1);
16781 ref->u.ss.start = start;
16782 if (end == NULL && e->ts.u.cl)
16783 end = gfc_copy_expr (e->ts.u.cl->length);
16784 ref->u.ss.end = end;
16785 ref->u.ss.length = e->ts.u.cl;
16786 e->ts.u.cl = NULL;
16787 }
16788 ref = ref->next;
16789 free (mem);
16790 }
16791
16792 /* Any further ref is an error. */
16793 if (ref)
16794 {
16795 gcc_assert (ref->type == REF_ARRAY);
16796 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
16797 &ref->u.ar.where);
16798 continue;
16799 }
16800 }
16801
16802 if (!gfc_resolve_expr (e))
16803 continue;
16804
16805 sym = e->symtree->n.sym;
16806
16807 if (sym->attr.is_protected)
16808 cnt_protected++;
16809 if (cnt_protected > 0 && cnt_protected != object)
16810 {
16811 gfc_error ("Either all or none of the objects in the "
16812 "EQUIVALENCE set at %L shall have the "
16813 "PROTECTED attribute",
16814 &e->where);
16815 break;
16816 }
16817
16818 /* Shall not equivalence common block variables in a PURE procedure. */
16819 if (sym->ns->proc_name
16820 && sym->ns->proc_name->attr.pure
16821 && sym->attr.in_common)
16822 {
16823 /* Need to check for symbols that may have entered the pure
16824 procedure via a USE statement. */
16825 bool saw_sym = false;
16826 if (sym->ns->use_stmts)
16827 {
16828 gfc_use_rename *r;
16829 for (r = sym->ns->use_stmts->rename; r; r = r->next)
16830 if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
16831 }
16832 else
16833 saw_sym = true;
16834
16835 if (saw_sym)
16836 gfc_error ("COMMON block member %qs at %L cannot be an "
16837 "EQUIVALENCE object in the pure procedure %qs",
16838 sym->name, &e->where, sym->ns->proc_name->name);
16839 break;
16840 }
16841
16842 /* Shall not be a named constant. */
16843 if (e->expr_type == EXPR_CONSTANT)
16844 {
16845 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
16846 "object", sym->name, &e->where);
16847 continue;
16848 }
16849
16850 if (e->ts.type == BT_DERIVED
16851 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
16852 continue;
16853
16854 /* Check that the types correspond correctly:
16855 Note 5.28:
16856 A numeric sequence structure may be equivalenced to another sequence
16857 structure, an object of default integer type, default real type, double
16858 precision real type, default logical type such that components of the
16859 structure ultimately only become associated to objects of the same
16860 kind. A character sequence structure may be equivalenced to an object
16861 of default character kind or another character sequence structure.
16862 Other objects may be equivalenced only to objects of the same type and
16863 kind parameters. */
16864
16865 /* Identical types are unconditionally OK. */
16866 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
16867 goto identical_types;
16868
16869 last_eq_type = sequence_type (*last_ts);
16870 eq_type = sequence_type (sym->ts);
16871
16872 /* Since the pair of objects is not of the same type, mixed or
16873 non-default sequences can be rejected. */
16874
16875 msg = "Sequence %s with mixed components in EQUIVALENCE "
16876 "statement at %L with different type objects";
16877 if ((object ==2
16878 && last_eq_type == SEQ_MIXED
16879 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
16880 || (eq_type == SEQ_MIXED
16881 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
16882 continue;
16883
16884 msg = "Non-default type object or sequence %s in EQUIVALENCE "
16885 "statement at %L with objects of different type";
16886 if ((object ==2
16887 && last_eq_type == SEQ_NONDEFAULT
16888 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
16889 || (eq_type == SEQ_NONDEFAULT
16890 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
16891 continue;
16892
16893 msg ="Non-CHARACTER object %qs in default CHARACTER "
16894 "EQUIVALENCE statement at %L";
16895 if (last_eq_type == SEQ_CHARACTER
16896 && eq_type != SEQ_CHARACTER
16897 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
16898 continue;
16899
16900 msg ="Non-NUMERIC object %qs in default NUMERIC "
16901 "EQUIVALENCE statement at %L";
16902 if (last_eq_type == SEQ_NUMERIC
16903 && eq_type != SEQ_NUMERIC
16904 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
16905 continue;
16906
16907 identical_types:
16908
16909 last_ts =&sym->ts;
16910 last_where = &e->where;
16911
16912 if (!e->ref)
16913 continue;
16914
16915 /* Shall not be an automatic array. */
16916 if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
16917 {
16918 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
16919 "an EQUIVALENCE object", sym->name, &e->where);
16920 continue;
16921 }
16922
16923 r = e->ref;
16924 while (r)
16925 {
16926 /* Shall not be a structure component. */
16927 if (r->type == REF_COMPONENT)
16928 {
16929 gfc_error ("Structure component %qs at %L cannot be an "
16930 "EQUIVALENCE object",
16931 r->u.c.component->name, &e->where);
16932 break;
16933 }
16934
16935 /* A substring shall not have length zero. */
16936 if (r->type == REF_SUBSTRING)
16937 {
16938 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
16939 {
16940 gfc_error ("Substring at %L has length zero",
16941 &r->u.ss.start->where);
16942 break;
16943 }
16944 }
16945 r = r->next;
16946 }
16947 }
16948 }
16949
16950
16951 /* Function called by resolve_fntype to flag other symbols used in the
16952 length type parameter specification of function results. */
16953
16954 static bool
16955 flag_fn_result_spec (gfc_expr *expr,
16956 gfc_symbol *sym,
16957 int *f ATTRIBUTE_UNUSED)
16958 {
16959 gfc_namespace *ns;
16960 gfc_symbol *s;
16961
16962 if (expr->expr_type == EXPR_VARIABLE)
16963 {
16964 s = expr->symtree->n.sym;
16965 for (ns = s->ns; ns; ns = ns->parent)
16966 if (!ns->parent)
16967 break;
16968
16969 if (sym == s)
16970 {
16971 gfc_error ("Self reference in character length expression "
16972 "for %qs at %L", sym->name, &expr->where);
16973 return true;
16974 }
16975
16976 if (!s->fn_result_spec
16977 && s->attr.flavor == FL_PARAMETER)
16978 {
16979 /* Function contained in a module.... */
16980 if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
16981 {
16982 gfc_symtree *st;
16983 s->fn_result_spec = 1;
16984 /* Make sure that this symbol is translated as a module
16985 variable. */
16986 st = gfc_get_unique_symtree (ns);
16987 st->n.sym = s;
16988 s->refs++;
16989 }
16990 /* ... which is use associated and called. */
16991 else if (s->attr.use_assoc || s->attr.used_in_submodule
16992 ||
16993 /* External function matched with an interface. */
16994 (s->ns->proc_name
16995 && ((s->ns == ns
16996 && s->ns->proc_name->attr.if_source == IFSRC_DECL)
16997 || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
16998 && s->ns->proc_name->attr.function))
16999 s->fn_result_spec = 1;
17000 }
17001 }
17002 return false;
17003 }
17004
17005
17006 /* Resolve function and ENTRY types, issue diagnostics if needed. */
17007
17008 static void
17009 resolve_fntype (gfc_namespace *ns)
17010 {
17011 gfc_entry_list *el;
17012 gfc_symbol *sym;
17013
17014 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
17015 return;
17016
17017 /* If there are any entries, ns->proc_name is the entry master
17018 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
17019 if (ns->entries)
17020 sym = ns->entries->sym;
17021 else
17022 sym = ns->proc_name;
17023 if (sym->result == sym
17024 && sym->ts.type == BT_UNKNOWN
17025 && !gfc_set_default_type (sym, 0, NULL)
17026 && !sym->attr.untyped)
17027 {
17028 gfc_error ("Function %qs at %L has no IMPLICIT type",
17029 sym->name, &sym->declared_at);
17030 sym->attr.untyped = 1;
17031 }
17032
17033 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
17034 && !sym->attr.contained
17035 && !gfc_check_symbol_access (sym->ts.u.derived)
17036 && gfc_check_symbol_access (sym))
17037 {
17038 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
17039 "%L of PRIVATE type %qs", sym->name,
17040 &sym->declared_at, sym->ts.u.derived->name);
17041 }
17042
17043 if (ns->entries)
17044 for (el = ns->entries->next; el; el = el->next)
17045 {
17046 if (el->sym->result == el->sym
17047 && el->sym->ts.type == BT_UNKNOWN
17048 && !gfc_set_default_type (el->sym, 0, NULL)
17049 && !el->sym->attr.untyped)
17050 {
17051 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
17052 el->sym->name, &el->sym->declared_at);
17053 el->sym->attr.untyped = 1;
17054 }
17055 }
17056
17057 if (sym->ts.type == BT_CHARACTER)
17058 gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
17059 }
17060
17061
17062 /* 12.3.2.1.1 Defined operators. */
17063
17064 static bool
17065 check_uop_procedure (gfc_symbol *sym, locus where)
17066 {
17067 gfc_formal_arglist *formal;
17068
17069 if (!sym->attr.function)
17070 {
17071 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
17072 sym->name, &where);
17073 return false;
17074 }
17075
17076 if (sym->ts.type == BT_CHARACTER
17077 && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
17078 && !(sym->result && ((sym->result->ts.u.cl
17079 && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
17080 {
17081 gfc_error ("User operator procedure %qs at %L cannot be assumed "
17082 "character length", sym->name, &where);
17083 return false;
17084 }
17085
17086 formal = gfc_sym_get_dummy_args (sym);
17087 if (!formal || !formal->sym)
17088 {
17089 gfc_error ("User operator procedure %qs at %L must have at least "
17090 "one argument", sym->name, &where);
17091 return false;
17092 }
17093
17094 if (formal->sym->attr.intent != INTENT_IN)
17095 {
17096 gfc_error ("First argument of operator interface at %L must be "
17097 "INTENT(IN)", &where);
17098 return false;
17099 }
17100
17101 if (formal->sym->attr.optional)
17102 {
17103 gfc_error ("First argument of operator interface at %L cannot be "
17104 "optional", &where);
17105 return false;
17106 }
17107
17108 formal = formal->next;
17109 if (!formal || !formal->sym)
17110 return true;
17111
17112 if (formal->sym->attr.intent != INTENT_IN)
17113 {
17114 gfc_error ("Second argument of operator interface at %L must be "
17115 "INTENT(IN)", &where);
17116 return false;
17117 }
17118
17119 if (formal->sym->attr.optional)
17120 {
17121 gfc_error ("Second argument of operator interface at %L cannot be "
17122 "optional", &where);
17123 return false;
17124 }
17125
17126 if (formal->next)
17127 {
17128 gfc_error ("Operator interface at %L must have, at most, two "
17129 "arguments", &where);
17130 return false;
17131 }
17132
17133 return true;
17134 }
17135
17136 static void
17137 gfc_resolve_uops (gfc_symtree *symtree)
17138 {
17139 gfc_interface *itr;
17140
17141 if (symtree == NULL)
17142 return;
17143
17144 gfc_resolve_uops (symtree->left);
17145 gfc_resolve_uops (symtree->right);
17146
17147 for (itr = symtree->n.uop->op; itr; itr = itr->next)
17148 check_uop_procedure (itr->sym, itr->sym->declared_at);
17149 }
17150
17151
17152 /* Examine all of the expressions associated with a program unit,
17153 assign types to all intermediate expressions, make sure that all
17154 assignments are to compatible types and figure out which names
17155 refer to which functions or subroutines. It doesn't check code
17156 block, which is handled by gfc_resolve_code. */
17157
17158 static void
17159 resolve_types (gfc_namespace *ns)
17160 {
17161 gfc_namespace *n;
17162 gfc_charlen *cl;
17163 gfc_data *d;
17164 gfc_equiv *eq;
17165 gfc_namespace* old_ns = gfc_current_ns;
17166 bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
17167
17168 if (ns->types_resolved)
17169 return;
17170
17171 /* Check that all IMPLICIT types are ok. */
17172 if (!ns->seen_implicit_none)
17173 {
17174 unsigned letter;
17175 for (letter = 0; letter != GFC_LETTERS; ++letter)
17176 if (ns->set_flag[letter]
17177 && !resolve_typespec_used (&ns->default_type[letter],
17178 &ns->implicit_loc[letter], NULL))
17179 return;
17180 }
17181
17182 gfc_current_ns = ns;
17183
17184 resolve_entries (ns);
17185
17186 resolve_common_vars (&ns->blank_common, false);
17187 resolve_common_blocks (ns->common_root);
17188
17189 resolve_contained_functions (ns);
17190
17191 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
17192 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
17193 gfc_resolve_formal_arglist (ns->proc_name);
17194
17195 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
17196
17197 for (cl = ns->cl_list; cl; cl = cl->next)
17198 resolve_charlen (cl);
17199
17200 gfc_traverse_ns (ns, resolve_symbol);
17201
17202 resolve_fntype (ns);
17203
17204 for (n = ns->contained; n; n = n->sibling)
17205 {
17206 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
17207 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
17208 "also be PURE", n->proc_name->name,
17209 &n->proc_name->declared_at);
17210
17211 resolve_types (n);
17212 }
17213
17214 forall_flag = 0;
17215 gfc_do_concurrent_flag = 0;
17216 gfc_check_interfaces (ns);
17217
17218 gfc_traverse_ns (ns, resolve_values);
17219
17220 if (ns->save_all || (!flag_automatic && !recursive))
17221 gfc_save_all (ns);
17222
17223 iter_stack = NULL;
17224 for (d = ns->data; d; d = d->next)
17225 resolve_data (d);
17226
17227 iter_stack = NULL;
17228 gfc_traverse_ns (ns, gfc_formalize_init_value);
17229
17230 gfc_traverse_ns (ns, gfc_verify_binding_labels);
17231
17232 for (eq = ns->equiv; eq; eq = eq->next)
17233 resolve_equivalence (eq);
17234
17235 /* Warn about unused labels. */
17236 if (warn_unused_label)
17237 warn_unused_fortran_label (ns->st_labels);
17238
17239 gfc_resolve_uops (ns->uop_root);
17240
17241 gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
17242
17243 gfc_resolve_omp_declare_simd (ns);
17244
17245 gfc_resolve_omp_udrs (ns->omp_udr_root);
17246
17247 ns->types_resolved = 1;
17248
17249 gfc_current_ns = old_ns;
17250 }
17251
17252
17253 /* Call gfc_resolve_code recursively. */
17254
17255 static void
17256 resolve_codes (gfc_namespace *ns)
17257 {
17258 gfc_namespace *n;
17259 bitmap_obstack old_obstack;
17260
17261 if (ns->resolved == 1)
17262 return;
17263
17264 for (n = ns->contained; n; n = n->sibling)
17265 resolve_codes (n);
17266
17267 gfc_current_ns = ns;
17268
17269 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
17270 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
17271 cs_base = NULL;
17272
17273 /* Set to an out of range value. */
17274 current_entry_id = -1;
17275
17276 old_obstack = labels_obstack;
17277 bitmap_obstack_initialize (&labels_obstack);
17278
17279 gfc_resolve_oacc_declare (ns);
17280 gfc_resolve_oacc_routines (ns);
17281 gfc_resolve_omp_local_vars (ns);
17282 gfc_resolve_code (ns->code, ns);
17283
17284 bitmap_obstack_release (&labels_obstack);
17285 labels_obstack = old_obstack;
17286 }
17287
17288
17289 /* This function is called after a complete program unit has been compiled.
17290 Its purpose is to examine all of the expressions associated with a program
17291 unit, assign types to all intermediate expressions, make sure that all
17292 assignments are to compatible types and figure out which names refer to
17293 which functions or subroutines. */
17294
17295 void
17296 gfc_resolve (gfc_namespace *ns)
17297 {
17298 gfc_namespace *old_ns;
17299 code_stack *old_cs_base;
17300 struct gfc_omp_saved_state old_omp_state;
17301
17302 if (ns->resolved)
17303 return;
17304
17305 ns->resolved = -1;
17306 old_ns = gfc_current_ns;
17307 old_cs_base = cs_base;
17308
17309 /* As gfc_resolve can be called during resolution of an OpenMP construct
17310 body, we should clear any state associated to it, so that say NS's
17311 DO loops are not interpreted as OpenMP loops. */
17312 if (!ns->construct_entities)
17313 gfc_omp_save_and_clear_state (&old_omp_state);
17314
17315 resolve_types (ns);
17316 component_assignment_level = 0;
17317 resolve_codes (ns);
17318
17319 gfc_current_ns = old_ns;
17320 cs_base = old_cs_base;
17321 ns->resolved = 1;
17322
17323 gfc_run_passes (ns);
17324
17325 if (!ns->construct_entities)
17326 gfc_omp_restore_state (&old_omp_state);
17327 }