PR fortran/95709 - ICE in gfc_resolve_code, at fortran/resolve.c:11807
[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)->ts.u.derived;
9245 }
9246
9247 if (code->expr2->rank && CLASS_DATA (code->expr1)->as)
9248 CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
9249
9250 /* F2008: C803 The selector expression must not be coindexed. */
9251 if (gfc_is_coindexed (code->expr2))
9252 {
9253 gfc_error ("Selector at %L must not be coindexed",
9254 &code->expr2->where);
9255 return;
9256 }
9257
9258 }
9259 else
9260 {
9261 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
9262
9263 if (gfc_is_coindexed (code->expr1))
9264 {
9265 gfc_error ("Selector at %L must not be coindexed",
9266 &code->expr1->where);
9267 return;
9268 }
9269 }
9270
9271 /* Loop over TYPE IS / CLASS IS cases. */
9272 for (body = code->block; body; body = body->block)
9273 {
9274 c = body->ext.block.case_list;
9275
9276 if (!error)
9277 {
9278 /* Check for repeated cases. */
9279 for (tail = code->block; tail; tail = tail->block)
9280 {
9281 gfc_case *d = tail->ext.block.case_list;
9282 if (tail == body)
9283 break;
9284
9285 if (c->ts.type == d->ts.type
9286 && ((c->ts.type == BT_DERIVED
9287 && c->ts.u.derived && d->ts.u.derived
9288 && !strcmp (c->ts.u.derived->name,
9289 d->ts.u.derived->name))
9290 || c->ts.type == BT_UNKNOWN
9291 || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9292 && c->ts.kind == d->ts.kind)))
9293 {
9294 gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
9295 &c->where, &d->where);
9296 return;
9297 }
9298 }
9299 }
9300
9301 /* Check F03:C815. */
9302 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9303 && !selector_type->attr.unlimited_polymorphic
9304 && !gfc_type_is_extensible (c->ts.u.derived))
9305 {
9306 gfc_error ("Derived type %qs at %L must be extensible",
9307 c->ts.u.derived->name, &c->where);
9308 error++;
9309 continue;
9310 }
9311
9312 /* Check F03:C816. */
9313 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
9314 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
9315 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
9316 {
9317 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9318 gfc_error ("Derived type %qs at %L must be an extension of %qs",
9319 c->ts.u.derived->name, &c->where, selector_type->name);
9320 else
9321 gfc_error ("Unexpected intrinsic type %qs at %L",
9322 gfc_basic_typename (c->ts.type), &c->where);
9323 error++;
9324 continue;
9325 }
9326
9327 /* Check F03:C814. */
9328 if (c->ts.type == BT_CHARACTER
9329 && (c->ts.u.cl->length != NULL || c->ts.deferred))
9330 {
9331 gfc_error ("The type-spec at %L shall specify that each length "
9332 "type parameter is assumed", &c->where);
9333 error++;
9334 continue;
9335 }
9336
9337 /* Intercept the DEFAULT case. */
9338 if (c->ts.type == BT_UNKNOWN)
9339 {
9340 /* Check F03:C818. */
9341 if (default_case)
9342 {
9343 gfc_error ("The DEFAULT CASE at %L cannot be followed "
9344 "by a second DEFAULT CASE at %L",
9345 &default_case->ext.block.case_list->where, &c->where);
9346 error++;
9347 continue;
9348 }
9349
9350 default_case = body;
9351 }
9352 }
9353
9354 if (error > 0)
9355 return;
9356
9357 /* Transform SELECT TYPE statement to BLOCK and associate selector to
9358 target if present. If there are any EXIT statements referring to the
9359 SELECT TYPE construct, this is no problem because the gfc_code
9360 reference stays the same and EXIT is equally possible from the BLOCK
9361 it is changed to. */
9362 code->op = EXEC_BLOCK;
9363 if (code->expr2)
9364 {
9365 gfc_association_list* assoc;
9366
9367 assoc = gfc_get_association_list ();
9368 assoc->st = code->expr1->symtree;
9369 assoc->target = gfc_copy_expr (code->expr2);
9370 assoc->target->where = code->expr2->where;
9371 /* assoc->variable will be set by resolve_assoc_var. */
9372
9373 code->ext.block.assoc = assoc;
9374 code->expr1->symtree->n.sym->assoc = assoc;
9375
9376 resolve_assoc_var (code->expr1->symtree->n.sym, false);
9377 }
9378 else
9379 code->ext.block.assoc = NULL;
9380
9381 /* Ensure that the selector rank and arrayspec are available to
9382 correct expressions in which they might be missing. */
9383 if (code->expr2 && code->expr2->rank)
9384 {
9385 rank = code->expr2->rank;
9386 for (ref = code->expr2->ref; ref; ref = ref->next)
9387 if (ref->next == NULL)
9388 break;
9389 if (ref && ref->type == REF_ARRAY)
9390 ref = gfc_copy_ref (ref);
9391
9392 /* Fixup expr1 if necessary. */
9393 if (rank)
9394 fixup_array_ref (&code->expr1, code->expr2, rank, ref);
9395 }
9396 else if (code->expr1->rank)
9397 {
9398 rank = code->expr1->rank;
9399 for (ref = code->expr1->ref; ref; ref = ref->next)
9400 if (ref->next == NULL)
9401 break;
9402 if (ref && ref->type == REF_ARRAY)
9403 ref = gfc_copy_ref (ref);
9404 }
9405
9406 /* Add EXEC_SELECT to switch on type. */
9407 new_st = gfc_get_code (code->op);
9408 new_st->expr1 = code->expr1;
9409 new_st->expr2 = code->expr2;
9410 new_st->block = code->block;
9411 code->expr1 = code->expr2 = NULL;
9412 code->block = NULL;
9413 if (!ns->code)
9414 ns->code = new_st;
9415 else
9416 ns->code->next = new_st;
9417 code = new_st;
9418 code->op = EXEC_SELECT_TYPE;
9419
9420 /* Use the intrinsic LOC function to generate an integer expression
9421 for the vtable of the selector. Note that the rank of the selector
9422 expression has to be set to zero. */
9423 gfc_add_vptr_component (code->expr1);
9424 code->expr1->rank = 0;
9425 code->expr1 = build_loc_call (code->expr1);
9426 selector_expr = code->expr1->value.function.actual->expr;
9427
9428 /* Loop over TYPE IS / CLASS IS cases. */
9429 for (body = code->block; body; body = body->block)
9430 {
9431 gfc_symbol *vtab;
9432 gfc_expr *e;
9433 c = body->ext.block.case_list;
9434
9435 /* Generate an index integer expression for address of the
9436 TYPE/CLASS vtable and store it in c->low. The hash expression
9437 is stored in c->high and is used to resolve intrinsic cases. */
9438 if (c->ts.type != BT_UNKNOWN)
9439 {
9440 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
9441 {
9442 vtab = gfc_find_derived_vtab (c->ts.u.derived);
9443 gcc_assert (vtab);
9444 c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
9445 c->ts.u.derived->hash_value);
9446 }
9447 else
9448 {
9449 vtab = gfc_find_vtab (&c->ts);
9450 gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
9451 e = CLASS_DATA (vtab)->initializer;
9452 c->high = gfc_copy_expr (e);
9453 if (c->high->ts.kind != gfc_integer_4_kind)
9454 {
9455 gfc_typespec ts;
9456 ts.kind = gfc_integer_4_kind;
9457 ts.type = BT_INTEGER;
9458 gfc_convert_type_warn (c->high, &ts, 2, 0);
9459 }
9460 }
9461
9462 e = gfc_lval_expr_from_sym (vtab);
9463 c->low = build_loc_call (e);
9464 }
9465 else
9466 continue;
9467
9468 /* Associate temporary to selector. This should only be done
9469 when this case is actually true, so build a new ASSOCIATE
9470 that does precisely this here (instead of using the
9471 'global' one). */
9472
9473 if (c->ts.type == BT_CLASS)
9474 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
9475 else if (c->ts.type == BT_DERIVED)
9476 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
9477 else if (c->ts.type == BT_CHARACTER)
9478 {
9479 HOST_WIDE_INT charlen = 0;
9480 if (c->ts.u.cl && c->ts.u.cl->length
9481 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9482 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
9483 snprintf (name, sizeof (name),
9484 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
9485 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
9486 }
9487 else
9488 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
9489 c->ts.kind);
9490
9491 st = gfc_find_symtree (ns->sym_root, name);
9492 gcc_assert (st->n.sym->assoc);
9493 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
9494 st->n.sym->assoc->target->where = selector_expr->where;
9495 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
9496 {
9497 gfc_add_data_component (st->n.sym->assoc->target);
9498 /* Fixup the target expression if necessary. */
9499 if (rank)
9500 fixup_array_ref (&st->n.sym->assoc->target, NULL, rank, ref);
9501 }
9502
9503 new_st = gfc_get_code (EXEC_BLOCK);
9504 new_st->ext.block.ns = gfc_build_block_ns (ns);
9505 new_st->ext.block.ns->code = body->next;
9506 body->next = new_st;
9507
9508 /* Chain in the new list only if it is marked as dangling. Otherwise
9509 there is a CASE label overlap and this is already used. Just ignore,
9510 the error is diagnosed elsewhere. */
9511 if (st->n.sym->assoc->dangling)
9512 {
9513 new_st->ext.block.assoc = st->n.sym->assoc;
9514 st->n.sym->assoc->dangling = 0;
9515 }
9516
9517 resolve_assoc_var (st->n.sym, false);
9518 }
9519
9520 /* Take out CLASS IS cases for separate treatment. */
9521 body = code;
9522 while (body && body->block)
9523 {
9524 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
9525 {
9526 /* Add to class_is list. */
9527 if (class_is == NULL)
9528 {
9529 class_is = body->block;
9530 tail = class_is;
9531 }
9532 else
9533 {
9534 for (tail = class_is; tail->block; tail = tail->block) ;
9535 tail->block = body->block;
9536 tail = tail->block;
9537 }
9538 /* Remove from EXEC_SELECT list. */
9539 body->block = body->block->block;
9540 tail->block = NULL;
9541 }
9542 else
9543 body = body->block;
9544 }
9545
9546 if (class_is)
9547 {
9548 gfc_symbol *vtab;
9549
9550 if (!default_case)
9551 {
9552 /* Add a default case to hold the CLASS IS cases. */
9553 for (tail = code; tail->block; tail = tail->block) ;
9554 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
9555 tail = tail->block;
9556 tail->ext.block.case_list = gfc_get_case ();
9557 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
9558 tail->next = NULL;
9559 default_case = tail;
9560 }
9561
9562 /* More than one CLASS IS block? */
9563 if (class_is->block)
9564 {
9565 gfc_code **c1,*c2;
9566 bool swapped;
9567 /* Sort CLASS IS blocks by extension level. */
9568 do
9569 {
9570 swapped = false;
9571 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
9572 {
9573 c2 = (*c1)->block;
9574 /* F03:C817 (check for doubles). */
9575 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
9576 == c2->ext.block.case_list->ts.u.derived->hash_value)
9577 {
9578 gfc_error ("Double CLASS IS block in SELECT TYPE "
9579 "statement at %L",
9580 &c2->ext.block.case_list->where);
9581 return;
9582 }
9583 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
9584 < c2->ext.block.case_list->ts.u.derived->attr.extension)
9585 {
9586 /* Swap. */
9587 (*c1)->block = c2->block;
9588 c2->block = *c1;
9589 *c1 = c2;
9590 swapped = true;
9591 }
9592 }
9593 }
9594 while (swapped);
9595 }
9596
9597 /* Generate IF chain. */
9598 if_st = gfc_get_code (EXEC_IF);
9599 new_st = if_st;
9600 for (body = class_is; body; body = body->block)
9601 {
9602 new_st->block = gfc_get_code (EXEC_IF);
9603 new_st = new_st->block;
9604 /* Set up IF condition: Call _gfortran_is_extension_of. */
9605 new_st->expr1 = gfc_get_expr ();
9606 new_st->expr1->expr_type = EXPR_FUNCTION;
9607 new_st->expr1->ts.type = BT_LOGICAL;
9608 new_st->expr1->ts.kind = 4;
9609 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
9610 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
9611 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
9612 /* Set up arguments. */
9613 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
9614 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
9615 new_st->expr1->value.function.actual->expr->where = code->loc;
9616 new_st->expr1->where = code->loc;
9617 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
9618 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
9619 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
9620 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
9621 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
9622 new_st->expr1->value.function.actual->next->expr->where = code->loc;
9623 new_st->next = body->next;
9624 }
9625 if (default_case->next)
9626 {
9627 new_st->block = gfc_get_code (EXEC_IF);
9628 new_st = new_st->block;
9629 new_st->next = default_case->next;
9630 }
9631
9632 /* Replace CLASS DEFAULT code by the IF chain. */
9633 default_case->next = if_st;
9634 }
9635
9636 /* Resolve the internal code. This cannot be done earlier because
9637 it requires that the sym->assoc of selectors is set already. */
9638 gfc_current_ns = ns;
9639 gfc_resolve_blocks (code->block, gfc_current_ns);
9640 gfc_current_ns = old_ns;
9641
9642 if (ref)
9643 free (ref);
9644 }
9645
9646
9647 /* Resolve a SELECT RANK statement. */
9648
9649 static void
9650 resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
9651 {
9652 gfc_namespace *ns;
9653 gfc_code *body, *new_st, *tail;
9654 gfc_case *c;
9655 char tname[GFC_MAX_SYMBOL_LEN + 7];
9656 char name[2 * GFC_MAX_SYMBOL_LEN];
9657 gfc_symtree *st;
9658 gfc_expr *selector_expr = NULL;
9659 int case_value;
9660 HOST_WIDE_INT charlen = 0;
9661
9662 ns = code->ext.block.ns;
9663 gfc_resolve (ns);
9664
9665 code->op = EXEC_BLOCK;
9666 if (code->expr2)
9667 {
9668 gfc_association_list* assoc;
9669
9670 assoc = gfc_get_association_list ();
9671 assoc->st = code->expr1->symtree;
9672 assoc->target = gfc_copy_expr (code->expr2);
9673 assoc->target->where = code->expr2->where;
9674 /* assoc->variable will be set by resolve_assoc_var. */
9675
9676 code->ext.block.assoc = assoc;
9677 code->expr1->symtree->n.sym->assoc = assoc;
9678
9679 resolve_assoc_var (code->expr1->symtree->n.sym, false);
9680 }
9681 else
9682 code->ext.block.assoc = NULL;
9683
9684 /* Loop over RANK cases. Note that returning on the errors causes a
9685 cascade of further errors because the case blocks do not compile
9686 correctly. */
9687 for (body = code->block; body; body = body->block)
9688 {
9689 c = body->ext.block.case_list;
9690 if (c->low)
9691 case_value = (int) mpz_get_si (c->low->value.integer);
9692 else
9693 case_value = -2;
9694
9695 /* Check for repeated cases. */
9696 for (tail = code->block; tail; tail = tail->block)
9697 {
9698 gfc_case *d = tail->ext.block.case_list;
9699 int case_value2;
9700
9701 if (tail == body)
9702 break;
9703
9704 /* Check F2018: C1153. */
9705 if (!c->low && !d->low)
9706 gfc_error ("RANK DEFAULT at %L is repeated at %L",
9707 &c->where, &d->where);
9708
9709 if (!c->low || !d->low)
9710 continue;
9711
9712 /* Check F2018: C1153. */
9713 case_value2 = (int) mpz_get_si (d->low->value.integer);
9714 if ((case_value == case_value2) && case_value == -1)
9715 gfc_error ("RANK (*) at %L is repeated at %L",
9716 &c->where, &d->where);
9717 else if (case_value == case_value2)
9718 gfc_error ("RANK (%i) at %L is repeated at %L",
9719 case_value, &c->where, &d->where);
9720 }
9721
9722 if (!c->low)
9723 continue;
9724
9725 /* Check F2018: C1155. */
9726 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
9727 || gfc_expr_attr (code->expr1).pointer))
9728 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9729 "allocatable selector at %L", &c->where, &code->expr1->where);
9730
9731 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
9732 || gfc_expr_attr (code->expr1).pointer))
9733 gfc_error ("RANK (*) at %L cannot be used with the pointer or "
9734 "allocatable selector at %L", &c->where, &code->expr1->where);
9735 }
9736
9737 /* Add EXEC_SELECT to switch on rank. */
9738 new_st = gfc_get_code (code->op);
9739 new_st->expr1 = code->expr1;
9740 new_st->expr2 = code->expr2;
9741 new_st->block = code->block;
9742 code->expr1 = code->expr2 = NULL;
9743 code->block = NULL;
9744 if (!ns->code)
9745 ns->code = new_st;
9746 else
9747 ns->code->next = new_st;
9748 code = new_st;
9749 code->op = EXEC_SELECT_RANK;
9750
9751 selector_expr = code->expr1;
9752
9753 /* Loop over SELECT RANK cases. */
9754 for (body = code->block; body; body = body->block)
9755 {
9756 c = body->ext.block.case_list;
9757 int case_value;
9758
9759 /* Pass on the default case. */
9760 if (c->low == NULL)
9761 continue;
9762
9763 /* Associate temporary to selector. This should only be done
9764 when this case is actually true, so build a new ASSOCIATE
9765 that does precisely this here (instead of using the
9766 'global' one). */
9767 if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
9768 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9769 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
9770
9771 if (c->ts.type == BT_CLASS)
9772 sprintf (tname, "class_%s", c->ts.u.derived->name);
9773 else if (c->ts.type == BT_DERIVED)
9774 sprintf (tname, "type_%s", c->ts.u.derived->name);
9775 else if (c->ts.type != BT_CHARACTER)
9776 sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
9777 else
9778 sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
9779 gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
9780
9781 case_value = (int) mpz_get_si (c->low->value.integer);
9782 if (case_value >= 0)
9783 sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
9784 else
9785 sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
9786
9787 st = gfc_find_symtree (ns->sym_root, name);
9788 gcc_assert (st->n.sym->assoc);
9789
9790 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
9791 st->n.sym->assoc->target->where = selector_expr->where;
9792
9793 new_st = gfc_get_code (EXEC_BLOCK);
9794 new_st->ext.block.ns = gfc_build_block_ns (ns);
9795 new_st->ext.block.ns->code = body->next;
9796 body->next = new_st;
9797
9798 /* Chain in the new list only if it is marked as dangling. Otherwise
9799 there is a CASE label overlap and this is already used. Just ignore,
9800 the error is diagnosed elsewhere. */
9801 if (st->n.sym->assoc->dangling)
9802 {
9803 new_st->ext.block.assoc = st->n.sym->assoc;
9804 st->n.sym->assoc->dangling = 0;
9805 }
9806
9807 resolve_assoc_var (st->n.sym, false);
9808 }
9809
9810 gfc_current_ns = ns;
9811 gfc_resolve_blocks (code->block, gfc_current_ns);
9812 gfc_current_ns = old_ns;
9813 }
9814
9815
9816 /* Resolve a transfer statement. This is making sure that:
9817 -- a derived type being transferred has only non-pointer components
9818 -- a derived type being transferred doesn't have private components, unless
9819 it's being transferred from the module where the type was defined
9820 -- we're not trying to transfer a whole assumed size array. */
9821
9822 static void
9823 resolve_transfer (gfc_code *code)
9824 {
9825 gfc_symbol *sym, *derived;
9826 gfc_ref *ref;
9827 gfc_expr *exp;
9828 bool write = false;
9829 bool formatted = false;
9830 gfc_dt *dt = code->ext.dt;
9831 gfc_symbol *dtio_sub = NULL;
9832
9833 exp = code->expr1;
9834
9835 while (exp != NULL && exp->expr_type == EXPR_OP
9836 && exp->value.op.op == INTRINSIC_PARENTHESES)
9837 exp = exp->value.op.op1;
9838
9839 if (exp && exp->expr_type == EXPR_NULL
9840 && code->ext.dt)
9841 {
9842 gfc_error ("Invalid context for NULL () intrinsic at %L",
9843 &exp->where);
9844 return;
9845 }
9846
9847 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
9848 && exp->expr_type != EXPR_FUNCTION
9849 && exp->expr_type != EXPR_STRUCTURE))
9850 return;
9851
9852 /* If we are reading, the variable will be changed. Note that
9853 code->ext.dt may be NULL if the TRANSFER is related to
9854 an INQUIRE statement -- but in this case, we are not reading, either. */
9855 if (dt && dt->dt_io_kind->value.iokind == M_READ
9856 && !gfc_check_vardef_context (exp, false, false, false,
9857 _("item in READ")))
9858 return;
9859
9860 const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
9861 || exp->expr_type == EXPR_FUNCTION
9862 ? &exp->ts : &exp->symtree->n.sym->ts;
9863
9864 /* Go to actual component transferred. */
9865 for (ref = exp->ref; ref; ref = ref->next)
9866 if (ref->type == REF_COMPONENT)
9867 ts = &ref->u.c.component->ts;
9868
9869 if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
9870 && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
9871 {
9872 derived = ts->u.derived;
9873
9874 /* Determine when to use the formatted DTIO procedure. */
9875 if (dt && (dt->format_expr || dt->format_label))
9876 formatted = true;
9877
9878 write = dt->dt_io_kind->value.iokind == M_WRITE
9879 || dt->dt_io_kind->value.iokind == M_PRINT;
9880 dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
9881
9882 if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
9883 {
9884 dt->udtio = exp;
9885 sym = exp->symtree->n.sym->ns->proc_name;
9886 /* Check to see if this is a nested DTIO call, with the
9887 dummy as the io-list object. */
9888 if (sym && sym == dtio_sub && sym->formal
9889 && sym->formal->sym == exp->symtree->n.sym
9890 && exp->ref == NULL)
9891 {
9892 if (!sym->attr.recursive)
9893 {
9894 gfc_error ("DTIO %s procedure at %L must be recursive",
9895 sym->name, &sym->declared_at);
9896 return;
9897 }
9898 }
9899 }
9900 }
9901
9902 if (ts->type == BT_CLASS && dtio_sub == NULL)
9903 {
9904 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
9905 "it is processed by a defined input/output procedure",
9906 &code->loc);
9907 return;
9908 }
9909
9910 if (ts->type == BT_DERIVED)
9911 {
9912 /* Check that transferred derived type doesn't contain POINTER
9913 components unless it is processed by a defined input/output
9914 procedure". */
9915 if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
9916 {
9917 gfc_error ("Data transfer element at %L cannot have POINTER "
9918 "components unless it is processed by a defined "
9919 "input/output procedure", &code->loc);
9920 return;
9921 }
9922
9923 /* F08:C935. */
9924 if (ts->u.derived->attr.proc_pointer_comp)
9925 {
9926 gfc_error ("Data transfer element at %L cannot have "
9927 "procedure pointer components", &code->loc);
9928 return;
9929 }
9930
9931 if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
9932 {
9933 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
9934 "components unless it is processed by a defined "
9935 "input/output procedure", &code->loc);
9936 return;
9937 }
9938
9939 /* C_PTR and C_FUNPTR have private components which means they cannot
9940 be printed. However, if -std=gnu and not -pedantic, allow
9941 the component to be printed to help debugging. */
9942 if (ts->u.derived->ts.f90_type == BT_VOID)
9943 {
9944 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
9945 "cannot have PRIVATE components", &code->loc))
9946 return;
9947 }
9948 else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
9949 {
9950 gfc_error ("Data transfer element at %L cannot have "
9951 "PRIVATE components unless it is processed by "
9952 "a defined input/output procedure", &code->loc);
9953 return;
9954 }
9955 }
9956
9957 if (exp->expr_type == EXPR_STRUCTURE)
9958 return;
9959
9960 sym = exp->symtree->n.sym;
9961
9962 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
9963 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
9964 {
9965 gfc_error ("Data transfer element at %L cannot be a full reference to "
9966 "an assumed-size array", &code->loc);
9967 return;
9968 }
9969 }
9970
9971
9972 /*********** Toplevel code resolution subroutines ***********/
9973
9974 /* Find the set of labels that are reachable from this block. We also
9975 record the last statement in each block. */
9976
9977 static void
9978 find_reachable_labels (gfc_code *block)
9979 {
9980 gfc_code *c;
9981
9982 if (!block)
9983 return;
9984
9985 cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
9986
9987 /* Collect labels in this block. We don't keep those corresponding
9988 to END {IF|SELECT}, these are checked in resolve_branch by going
9989 up through the code_stack. */
9990 for (c = block; c; c = c->next)
9991 {
9992 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
9993 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
9994 }
9995
9996 /* Merge with labels from parent block. */
9997 if (cs_base->prev)
9998 {
9999 gcc_assert (cs_base->prev->reachable_labels);
10000 bitmap_ior_into (cs_base->reachable_labels,
10001 cs_base->prev->reachable_labels);
10002 }
10003 }
10004
10005
10006 static void
10007 resolve_lock_unlock_event (gfc_code *code)
10008 {
10009 if (code->expr1->expr_type == EXPR_FUNCTION
10010 && code->expr1->value.function.isym
10011 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
10012 remove_caf_get_intrinsic (code->expr1);
10013
10014 if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
10015 && (code->expr1->ts.type != BT_DERIVED
10016 || code->expr1->expr_type != EXPR_VARIABLE
10017 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
10018 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
10019 || code->expr1->rank != 0
10020 || (!gfc_is_coarray (code->expr1) &&
10021 !gfc_is_coindexed (code->expr1))))
10022 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
10023 &code->expr1->where);
10024 else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
10025 && (code->expr1->ts.type != BT_DERIVED
10026 || code->expr1->expr_type != EXPR_VARIABLE
10027 || code->expr1->ts.u.derived->from_intmod
10028 != INTMOD_ISO_FORTRAN_ENV
10029 || code->expr1->ts.u.derived->intmod_sym_id
10030 != ISOFORTRAN_EVENT_TYPE
10031 || code->expr1->rank != 0))
10032 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
10033 &code->expr1->where);
10034 else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
10035 && !gfc_is_coindexed (code->expr1))
10036 gfc_error ("Event variable argument at %L must be a coarray or coindexed",
10037 &code->expr1->where);
10038 else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
10039 gfc_error ("Event variable argument at %L must be a coarray but not "
10040 "coindexed", &code->expr1->where);
10041
10042 /* Check STAT. */
10043 if (code->expr2
10044 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
10045 || code->expr2->expr_type != EXPR_VARIABLE))
10046 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10047 &code->expr2->where);
10048
10049 if (code->expr2
10050 && !gfc_check_vardef_context (code->expr2, false, false, false,
10051 _("STAT variable")))
10052 return;
10053
10054 /* Check ERRMSG. */
10055 if (code->expr3
10056 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
10057 || code->expr3->expr_type != EXPR_VARIABLE))
10058 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10059 &code->expr3->where);
10060
10061 if (code->expr3
10062 && !gfc_check_vardef_context (code->expr3, false, false, false,
10063 _("ERRMSG variable")))
10064 return;
10065
10066 /* Check for LOCK the ACQUIRED_LOCK. */
10067 if (code->op != EXEC_EVENT_WAIT && code->expr4
10068 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
10069 || code->expr4->expr_type != EXPR_VARIABLE))
10070 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
10071 "variable", &code->expr4->where);
10072
10073 if (code->op != EXEC_EVENT_WAIT && code->expr4
10074 && !gfc_check_vardef_context (code->expr4, false, false, false,
10075 _("ACQUIRED_LOCK variable")))
10076 return;
10077
10078 /* Check for EVENT WAIT the UNTIL_COUNT. */
10079 if (code->op == EXEC_EVENT_WAIT && code->expr4)
10080 {
10081 if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
10082 || code->expr4->rank != 0)
10083 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
10084 "expression", &code->expr4->where);
10085 }
10086 }
10087
10088
10089 static void
10090 resolve_critical (gfc_code *code)
10091 {
10092 gfc_symtree *symtree;
10093 gfc_symbol *lock_type;
10094 char name[GFC_MAX_SYMBOL_LEN];
10095 static int serial = 0;
10096
10097 if (flag_coarray != GFC_FCOARRAY_LIB)
10098 return;
10099
10100 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
10101 GFC_PREFIX ("lock_type"));
10102 if (symtree)
10103 lock_type = symtree->n.sym;
10104 else
10105 {
10106 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
10107 false) != 0)
10108 gcc_unreachable ();
10109 lock_type = symtree->n.sym;
10110 lock_type->attr.flavor = FL_DERIVED;
10111 lock_type->attr.zero_comp = 1;
10112 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
10113 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
10114 }
10115
10116 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
10117 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
10118 gcc_unreachable ();
10119
10120 code->resolved_sym = symtree->n.sym;
10121 symtree->n.sym->attr.flavor = FL_VARIABLE;
10122 symtree->n.sym->attr.referenced = 1;
10123 symtree->n.sym->attr.artificial = 1;
10124 symtree->n.sym->attr.codimension = 1;
10125 symtree->n.sym->ts.type = BT_DERIVED;
10126 symtree->n.sym->ts.u.derived = lock_type;
10127 symtree->n.sym->as = gfc_get_array_spec ();
10128 symtree->n.sym->as->corank = 1;
10129 symtree->n.sym->as->type = AS_EXPLICIT;
10130 symtree->n.sym->as->cotype = AS_EXPLICIT;
10131 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
10132 NULL, 1);
10133 gfc_commit_symbols();
10134 }
10135
10136
10137 static void
10138 resolve_sync (gfc_code *code)
10139 {
10140 /* Check imageset. The * case matches expr1 == NULL. */
10141 if (code->expr1)
10142 {
10143 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
10144 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
10145 "INTEGER expression", &code->expr1->where);
10146 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
10147 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
10148 gfc_error ("Imageset argument at %L must between 1 and num_images()",
10149 &code->expr1->where);
10150 else if (code->expr1->expr_type == EXPR_ARRAY
10151 && gfc_simplify_expr (code->expr1, 0))
10152 {
10153 gfc_constructor *cons;
10154 cons = gfc_constructor_first (code->expr1->value.constructor);
10155 for (; cons; cons = gfc_constructor_next (cons))
10156 if (cons->expr->expr_type == EXPR_CONSTANT
10157 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
10158 gfc_error ("Imageset argument at %L must between 1 and "
10159 "num_images()", &cons->expr->where);
10160 }
10161 }
10162
10163 /* Check STAT. */
10164 gfc_resolve_expr (code->expr2);
10165 if (code->expr2
10166 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
10167 || code->expr2->expr_type != EXPR_VARIABLE))
10168 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
10169 &code->expr2->where);
10170
10171 /* Check ERRMSG. */
10172 gfc_resolve_expr (code->expr3);
10173 if (code->expr3
10174 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
10175 || code->expr3->expr_type != EXPR_VARIABLE))
10176 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
10177 &code->expr3->where);
10178 }
10179
10180
10181 /* Given a branch to a label, see if the branch is conforming.
10182 The code node describes where the branch is located. */
10183
10184 static void
10185 resolve_branch (gfc_st_label *label, gfc_code *code)
10186 {
10187 code_stack *stack;
10188
10189 if (label == NULL)
10190 return;
10191
10192 /* Step one: is this a valid branching target? */
10193
10194 if (label->defined == ST_LABEL_UNKNOWN)
10195 {
10196 gfc_error ("Label %d referenced at %L is never defined", label->value,
10197 &code->loc);
10198 return;
10199 }
10200
10201 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
10202 {
10203 gfc_error ("Statement at %L is not a valid branch target statement "
10204 "for the branch statement at %L", &label->where, &code->loc);
10205 return;
10206 }
10207
10208 /* Step two: make sure this branch is not a branch to itself ;-) */
10209
10210 if (code->here == label)
10211 {
10212 gfc_warning (0,
10213 "Branch at %L may result in an infinite loop", &code->loc);
10214 return;
10215 }
10216
10217 /* Step three: See if the label is in the same block as the
10218 branching statement. The hard work has been done by setting up
10219 the bitmap reachable_labels. */
10220
10221 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
10222 {
10223 /* Check now whether there is a CRITICAL construct; if so, check
10224 whether the label is still visible outside of the CRITICAL block,
10225 which is invalid. */
10226 for (stack = cs_base; stack; stack = stack->prev)
10227 {
10228 if (stack->current->op == EXEC_CRITICAL
10229 && bitmap_bit_p (stack->reachable_labels, label->value))
10230 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
10231 "label at %L", &code->loc, &label->where);
10232 else if (stack->current->op == EXEC_DO_CONCURRENT
10233 && bitmap_bit_p (stack->reachable_labels, label->value))
10234 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
10235 "for label at %L", &code->loc, &label->where);
10236 }
10237
10238 return;
10239 }
10240
10241 /* Step four: If we haven't found the label in the bitmap, it may
10242 still be the label of the END of the enclosing block, in which
10243 case we find it by going up the code_stack. */
10244
10245 for (stack = cs_base; stack; stack = stack->prev)
10246 {
10247 if (stack->current->next && stack->current->next->here == label)
10248 break;
10249 if (stack->current->op == EXEC_CRITICAL)
10250 {
10251 /* Note: A label at END CRITICAL does not leave the CRITICAL
10252 construct as END CRITICAL is still part of it. */
10253 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
10254 " at %L", &code->loc, &label->where);
10255 return;
10256 }
10257 else if (stack->current->op == EXEC_DO_CONCURRENT)
10258 {
10259 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
10260 "label at %L", &code->loc, &label->where);
10261 return;
10262 }
10263 }
10264
10265 if (stack)
10266 {
10267 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
10268 return;
10269 }
10270
10271 /* The label is not in an enclosing block, so illegal. This was
10272 allowed in Fortran 66, so we allow it as extension. No
10273 further checks are necessary in this case. */
10274 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
10275 "as the GOTO statement at %L", &label->where,
10276 &code->loc);
10277 return;
10278 }
10279
10280
10281 /* Check whether EXPR1 has the same shape as EXPR2. */
10282
10283 static bool
10284 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
10285 {
10286 mpz_t shape[GFC_MAX_DIMENSIONS];
10287 mpz_t shape2[GFC_MAX_DIMENSIONS];
10288 bool result = false;
10289 int i;
10290
10291 /* Compare the rank. */
10292 if (expr1->rank != expr2->rank)
10293 return result;
10294
10295 /* Compare the size of each dimension. */
10296 for (i=0; i<expr1->rank; i++)
10297 {
10298 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
10299 goto ignore;
10300
10301 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
10302 goto ignore;
10303
10304 if (mpz_cmp (shape[i], shape2[i]))
10305 goto over;
10306 }
10307
10308 /* When either of the two expression is an assumed size array, we
10309 ignore the comparison of dimension sizes. */
10310 ignore:
10311 result = true;
10312
10313 over:
10314 gfc_clear_shape (shape, i);
10315 gfc_clear_shape (shape2, i);
10316 return result;
10317 }
10318
10319
10320 /* Check whether a WHERE assignment target or a WHERE mask expression
10321 has the same shape as the outmost WHERE mask expression. */
10322
10323 static void
10324 resolve_where (gfc_code *code, gfc_expr *mask)
10325 {
10326 gfc_code *cblock;
10327 gfc_code *cnext;
10328 gfc_expr *e = NULL;
10329
10330 cblock = code->block;
10331
10332 /* Store the first WHERE mask-expr of the WHERE statement or construct.
10333 In case of nested WHERE, only the outmost one is stored. */
10334 if (mask == NULL) /* outmost WHERE */
10335 e = cblock->expr1;
10336 else /* inner WHERE */
10337 e = mask;
10338
10339 while (cblock)
10340 {
10341 if (cblock->expr1)
10342 {
10343 /* Check if the mask-expr has a consistent shape with the
10344 outmost WHERE mask-expr. */
10345 if (!resolve_where_shape (cblock->expr1, e))
10346 gfc_error ("WHERE mask at %L has inconsistent shape",
10347 &cblock->expr1->where);
10348 }
10349
10350 /* the assignment statement of a WHERE statement, or the first
10351 statement in where-body-construct of a WHERE construct */
10352 cnext = cblock->next;
10353 while (cnext)
10354 {
10355 switch (cnext->op)
10356 {
10357 /* WHERE assignment statement */
10358 case EXEC_ASSIGN:
10359
10360 /* Check shape consistent for WHERE assignment target. */
10361 if (e && !resolve_where_shape (cnext->expr1, e))
10362 gfc_error ("WHERE assignment target at %L has "
10363 "inconsistent shape", &cnext->expr1->where);
10364 break;
10365
10366
10367 case EXEC_ASSIGN_CALL:
10368 resolve_call (cnext);
10369 if (!cnext->resolved_sym->attr.elemental)
10370 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10371 &cnext->ext.actual->expr->where);
10372 break;
10373
10374 /* WHERE or WHERE construct is part of a where-body-construct */
10375 case EXEC_WHERE:
10376 resolve_where (cnext, e);
10377 break;
10378
10379 default:
10380 gfc_error ("Unsupported statement inside WHERE at %L",
10381 &cnext->loc);
10382 }
10383 /* the next statement within the same where-body-construct */
10384 cnext = cnext->next;
10385 }
10386 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10387 cblock = cblock->block;
10388 }
10389 }
10390
10391
10392 /* Resolve assignment in FORALL construct.
10393 NVAR is the number of FORALL index variables, and VAR_EXPR records the
10394 FORALL index variables. */
10395
10396 static void
10397 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
10398 {
10399 int n;
10400
10401 for (n = 0; n < nvar; n++)
10402 {
10403 gfc_symbol *forall_index;
10404
10405 forall_index = var_expr[n]->symtree->n.sym;
10406
10407 /* Check whether the assignment target is one of the FORALL index
10408 variable. */
10409 if ((code->expr1->expr_type == EXPR_VARIABLE)
10410 && (code->expr1->symtree->n.sym == forall_index))
10411 gfc_error ("Assignment to a FORALL index variable at %L",
10412 &code->expr1->where);
10413 else
10414 {
10415 /* If one of the FORALL index variables doesn't appear in the
10416 assignment variable, then there could be a many-to-one
10417 assignment. Emit a warning rather than an error because the
10418 mask could be resolving this problem. */
10419 if (!find_forall_index (code->expr1, forall_index, 0))
10420 gfc_warning (0, "The FORALL with index %qs is not used on the "
10421 "left side of the assignment at %L and so might "
10422 "cause multiple assignment to this object",
10423 var_expr[n]->symtree->name, &code->expr1->where);
10424 }
10425 }
10426 }
10427
10428
10429 /* Resolve WHERE statement in FORALL construct. */
10430
10431 static void
10432 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
10433 gfc_expr **var_expr)
10434 {
10435 gfc_code *cblock;
10436 gfc_code *cnext;
10437
10438 cblock = code->block;
10439 while (cblock)
10440 {
10441 /* the assignment statement of a WHERE statement, or the first
10442 statement in where-body-construct of a WHERE construct */
10443 cnext = cblock->next;
10444 while (cnext)
10445 {
10446 switch (cnext->op)
10447 {
10448 /* WHERE assignment statement */
10449 case EXEC_ASSIGN:
10450 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
10451 break;
10452
10453 /* WHERE operator assignment statement */
10454 case EXEC_ASSIGN_CALL:
10455 resolve_call (cnext);
10456 if (!cnext->resolved_sym->attr.elemental)
10457 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
10458 &cnext->ext.actual->expr->where);
10459 break;
10460
10461 /* WHERE or WHERE construct is part of a where-body-construct */
10462 case EXEC_WHERE:
10463 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
10464 break;
10465
10466 default:
10467 gfc_error ("Unsupported statement inside WHERE at %L",
10468 &cnext->loc);
10469 }
10470 /* the next statement within the same where-body-construct */
10471 cnext = cnext->next;
10472 }
10473 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
10474 cblock = cblock->block;
10475 }
10476 }
10477
10478
10479 /* Traverse the FORALL body to check whether the following errors exist:
10480 1. For assignment, check if a many-to-one assignment happens.
10481 2. For WHERE statement, check the WHERE body to see if there is any
10482 many-to-one assignment. */
10483
10484 static void
10485 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
10486 {
10487 gfc_code *c;
10488
10489 c = code->block->next;
10490 while (c)
10491 {
10492 switch (c->op)
10493 {
10494 case EXEC_ASSIGN:
10495 case EXEC_POINTER_ASSIGN:
10496 gfc_resolve_assign_in_forall (c, nvar, var_expr);
10497 break;
10498
10499 case EXEC_ASSIGN_CALL:
10500 resolve_call (c);
10501 break;
10502
10503 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
10504 there is no need to handle it here. */
10505 case EXEC_FORALL:
10506 break;
10507 case EXEC_WHERE:
10508 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
10509 break;
10510 default:
10511 break;
10512 }
10513 /* The next statement in the FORALL body. */
10514 c = c->next;
10515 }
10516 }
10517
10518
10519 /* Counts the number of iterators needed inside a forall construct, including
10520 nested forall constructs. This is used to allocate the needed memory
10521 in gfc_resolve_forall. */
10522
10523 static int
10524 gfc_count_forall_iterators (gfc_code *code)
10525 {
10526 int max_iters, sub_iters, current_iters;
10527 gfc_forall_iterator *fa;
10528
10529 gcc_assert(code->op == EXEC_FORALL);
10530 max_iters = 0;
10531 current_iters = 0;
10532
10533 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
10534 current_iters ++;
10535
10536 code = code->block->next;
10537
10538 while (code)
10539 {
10540 if (code->op == EXEC_FORALL)
10541 {
10542 sub_iters = gfc_count_forall_iterators (code);
10543 if (sub_iters > max_iters)
10544 max_iters = sub_iters;
10545 }
10546 code = code->next;
10547 }
10548
10549 return current_iters + max_iters;
10550 }
10551
10552
10553 /* Given a FORALL construct, first resolve the FORALL iterator, then call
10554 gfc_resolve_forall_body to resolve the FORALL body. */
10555
10556 static void
10557 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
10558 {
10559 static gfc_expr **var_expr;
10560 static int total_var = 0;
10561 static int nvar = 0;
10562 int i, old_nvar, tmp;
10563 gfc_forall_iterator *fa;
10564
10565 old_nvar = nvar;
10566
10567 if (!gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
10568 return;
10569
10570 /* Start to resolve a FORALL construct */
10571 if (forall_save == 0)
10572 {
10573 /* Count the total number of FORALL indices in the nested FORALL
10574 construct in order to allocate the VAR_EXPR with proper size. */
10575 total_var = gfc_count_forall_iterators (code);
10576
10577 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
10578 var_expr = XCNEWVEC (gfc_expr *, total_var);
10579 }
10580
10581 /* The information about FORALL iterator, including FORALL indices start, end
10582 and stride. An outer FORALL indice cannot appear in start, end or stride. */
10583 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
10584 {
10585 /* Fortran 20008: C738 (R753). */
10586 if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
10587 {
10588 gfc_error ("FORALL index-name at %L must be a scalar variable "
10589 "of type integer", &fa->var->where);
10590 continue;
10591 }
10592
10593 /* Check if any outer FORALL index name is the same as the current
10594 one. */
10595 for (i = 0; i < nvar; i++)
10596 {
10597 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
10598 gfc_error ("An outer FORALL construct already has an index "
10599 "with this name %L", &fa->var->where);
10600 }
10601
10602 /* Record the current FORALL index. */
10603 var_expr[nvar] = gfc_copy_expr (fa->var);
10604
10605 nvar++;
10606
10607 /* No memory leak. */
10608 gcc_assert (nvar <= total_var);
10609 }
10610
10611 /* Resolve the FORALL body. */
10612 gfc_resolve_forall_body (code, nvar, var_expr);
10613
10614 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
10615 gfc_resolve_blocks (code->block, ns);
10616
10617 tmp = nvar;
10618 nvar = old_nvar;
10619 /* Free only the VAR_EXPRs allocated in this frame. */
10620 for (i = nvar; i < tmp; i++)
10621 gfc_free_expr (var_expr[i]);
10622
10623 if (nvar == 0)
10624 {
10625 /* We are in the outermost FORALL construct. */
10626 gcc_assert (forall_save == 0);
10627
10628 /* VAR_EXPR is not needed any more. */
10629 free (var_expr);
10630 total_var = 0;
10631 }
10632 }
10633
10634
10635 /* Resolve a BLOCK construct statement. */
10636
10637 static void
10638 resolve_block_construct (gfc_code* code)
10639 {
10640 /* Resolve the BLOCK's namespace. */
10641 gfc_resolve (code->ext.block.ns);
10642
10643 /* For an ASSOCIATE block, the associations (and their targets) are already
10644 resolved during resolve_symbol. */
10645 }
10646
10647
10648 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
10649 DO code nodes. */
10650
10651 void
10652 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
10653 {
10654 bool t;
10655
10656 for (; b; b = b->block)
10657 {
10658 t = gfc_resolve_expr (b->expr1);
10659 if (!gfc_resolve_expr (b->expr2))
10660 t = false;
10661
10662 switch (b->op)
10663 {
10664 case EXEC_IF:
10665 if (t && b->expr1 != NULL
10666 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
10667 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
10668 &b->expr1->where);
10669 break;
10670
10671 case EXEC_WHERE:
10672 if (t
10673 && b->expr1 != NULL
10674 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
10675 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
10676 &b->expr1->where);
10677 break;
10678
10679 case EXEC_GOTO:
10680 resolve_branch (b->label1, b);
10681 break;
10682
10683 case EXEC_BLOCK:
10684 resolve_block_construct (b);
10685 break;
10686
10687 case EXEC_SELECT:
10688 case EXEC_SELECT_TYPE:
10689 case EXEC_SELECT_RANK:
10690 case EXEC_FORALL:
10691 case EXEC_DO:
10692 case EXEC_DO_WHILE:
10693 case EXEC_DO_CONCURRENT:
10694 case EXEC_CRITICAL:
10695 case EXEC_READ:
10696 case EXEC_WRITE:
10697 case EXEC_IOLENGTH:
10698 case EXEC_WAIT:
10699 break;
10700
10701 case EXEC_OMP_ATOMIC:
10702 case EXEC_OACC_ATOMIC:
10703 {
10704 gfc_omp_atomic_op aop
10705 = (gfc_omp_atomic_op) (b->ext.omp_atomic & GFC_OMP_ATOMIC_MASK);
10706
10707 /* Verify this before calling gfc_resolve_code, which might
10708 change it. */
10709 gcc_assert (b->next && b->next->op == EXEC_ASSIGN);
10710 gcc_assert (((aop != GFC_OMP_ATOMIC_CAPTURE)
10711 && b->next->next == NULL)
10712 || ((aop == GFC_OMP_ATOMIC_CAPTURE)
10713 && b->next->next != NULL
10714 && b->next->next->op == EXEC_ASSIGN
10715 && b->next->next->next == NULL));
10716 }
10717 break;
10718
10719 case EXEC_OACC_PARALLEL_LOOP:
10720 case EXEC_OACC_PARALLEL:
10721 case EXEC_OACC_KERNELS_LOOP:
10722 case EXEC_OACC_KERNELS:
10723 case EXEC_OACC_SERIAL_LOOP:
10724 case EXEC_OACC_SERIAL:
10725 case EXEC_OACC_DATA:
10726 case EXEC_OACC_HOST_DATA:
10727 case EXEC_OACC_LOOP:
10728 case EXEC_OACC_UPDATE:
10729 case EXEC_OACC_WAIT:
10730 case EXEC_OACC_CACHE:
10731 case EXEC_OACC_ENTER_DATA:
10732 case EXEC_OACC_EXIT_DATA:
10733 case EXEC_OACC_ROUTINE:
10734 case EXEC_OMP_CRITICAL:
10735 case EXEC_OMP_DISTRIBUTE:
10736 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
10737 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
10738 case EXEC_OMP_DISTRIBUTE_SIMD:
10739 case EXEC_OMP_DO:
10740 case EXEC_OMP_DO_SIMD:
10741 case EXEC_OMP_MASTER:
10742 case EXEC_OMP_ORDERED:
10743 case EXEC_OMP_PARALLEL:
10744 case EXEC_OMP_PARALLEL_DO:
10745 case EXEC_OMP_PARALLEL_DO_SIMD:
10746 case EXEC_OMP_PARALLEL_SECTIONS:
10747 case EXEC_OMP_PARALLEL_WORKSHARE:
10748 case EXEC_OMP_SECTIONS:
10749 case EXEC_OMP_SIMD:
10750 case EXEC_OMP_SINGLE:
10751 case EXEC_OMP_TARGET:
10752 case EXEC_OMP_TARGET_DATA:
10753 case EXEC_OMP_TARGET_ENTER_DATA:
10754 case EXEC_OMP_TARGET_EXIT_DATA:
10755 case EXEC_OMP_TARGET_PARALLEL:
10756 case EXEC_OMP_TARGET_PARALLEL_DO:
10757 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
10758 case EXEC_OMP_TARGET_SIMD:
10759 case EXEC_OMP_TARGET_TEAMS:
10760 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10761 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10762 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10763 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10764 case EXEC_OMP_TARGET_UPDATE:
10765 case EXEC_OMP_TASK:
10766 case EXEC_OMP_TASKGROUP:
10767 case EXEC_OMP_TASKLOOP:
10768 case EXEC_OMP_TASKLOOP_SIMD:
10769 case EXEC_OMP_TASKWAIT:
10770 case EXEC_OMP_TASKYIELD:
10771 case EXEC_OMP_TEAMS:
10772 case EXEC_OMP_TEAMS_DISTRIBUTE:
10773 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10774 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10775 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10776 case EXEC_OMP_WORKSHARE:
10777 break;
10778
10779 default:
10780 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
10781 }
10782
10783 gfc_resolve_code (b->next, ns);
10784 }
10785 }
10786
10787
10788 /* Does everything to resolve an ordinary assignment. Returns true
10789 if this is an interface assignment. */
10790 static bool
10791 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
10792 {
10793 bool rval = false;
10794 gfc_expr *lhs;
10795 gfc_expr *rhs;
10796 int n;
10797 gfc_ref *ref;
10798 symbol_attribute attr;
10799
10800 if (gfc_extend_assign (code, ns))
10801 {
10802 gfc_expr** rhsptr;
10803
10804 if (code->op == EXEC_ASSIGN_CALL)
10805 {
10806 lhs = code->ext.actual->expr;
10807 rhsptr = &code->ext.actual->next->expr;
10808 }
10809 else
10810 {
10811 gfc_actual_arglist* args;
10812 gfc_typebound_proc* tbp;
10813
10814 gcc_assert (code->op == EXEC_COMPCALL);
10815
10816 args = code->expr1->value.compcall.actual;
10817 lhs = args->expr;
10818 rhsptr = &args->next->expr;
10819
10820 tbp = code->expr1->value.compcall.tbp;
10821 gcc_assert (!tbp->is_generic);
10822 }
10823
10824 /* Make a temporary rhs when there is a default initializer
10825 and rhs is the same symbol as the lhs. */
10826 if ((*rhsptr)->expr_type == EXPR_VARIABLE
10827 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
10828 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
10829 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
10830 *rhsptr = gfc_get_parentheses (*rhsptr);
10831
10832 return true;
10833 }
10834
10835 lhs = code->expr1;
10836 rhs = code->expr2;
10837
10838 if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
10839 && rhs->ts.type == BT_CHARACTER
10840 && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
10841 {
10842 /* Use of -fdec-char-conversions allows assignment of character data
10843 to non-character variables. This not permited for nonconstant
10844 strings. */
10845 gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
10846 gfc_typename (lhs), &rhs->where);
10847 return false;
10848 }
10849
10850 /* Handle the case of a BOZ literal on the RHS. */
10851 if (rhs->ts.type == BT_BOZ)
10852 {
10853 if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
10854 "statement value nor an actual argument of "
10855 "INT/REAL/DBLE/CMPLX intrinsic subprogram",
10856 &rhs->where))
10857 return false;
10858
10859 switch (lhs->ts.type)
10860 {
10861 case BT_INTEGER:
10862 if (!gfc_boz2int (rhs, lhs->ts.kind))
10863 return false;
10864 break;
10865 case BT_REAL:
10866 if (!gfc_boz2real (rhs, lhs->ts.kind))
10867 return false;
10868 break;
10869 default:
10870 gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
10871 return false;
10872 }
10873 }
10874
10875 if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
10876 {
10877 HOST_WIDE_INT llen = 0, rlen = 0;
10878 if (lhs->ts.u.cl != NULL
10879 && lhs->ts.u.cl->length != NULL
10880 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10881 llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
10882
10883 if (rhs->expr_type == EXPR_CONSTANT)
10884 rlen = rhs->value.character.length;
10885
10886 else if (rhs->ts.u.cl != NULL
10887 && rhs->ts.u.cl->length != NULL
10888 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10889 rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
10890
10891 if (rlen && llen && rlen > llen)
10892 gfc_warning_now (OPT_Wcharacter_truncation,
10893 "CHARACTER expression will be truncated "
10894 "in assignment (%ld/%ld) at %L",
10895 (long) llen, (long) rlen, &code->loc);
10896 }
10897
10898 /* Ensure that a vector index expression for the lvalue is evaluated
10899 to a temporary if the lvalue symbol is referenced in it. */
10900 if (lhs->rank)
10901 {
10902 for (ref = lhs->ref; ref; ref= ref->next)
10903 if (ref->type == REF_ARRAY)
10904 {
10905 for (n = 0; n < ref->u.ar.dimen; n++)
10906 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
10907 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
10908 ref->u.ar.start[n]))
10909 ref->u.ar.start[n]
10910 = gfc_get_parentheses (ref->u.ar.start[n]);
10911 }
10912 }
10913
10914 if (gfc_pure (NULL))
10915 {
10916 if (lhs->ts.type == BT_DERIVED
10917 && lhs->expr_type == EXPR_VARIABLE
10918 && lhs->ts.u.derived->attr.pointer_comp
10919 && rhs->expr_type == EXPR_VARIABLE
10920 && (gfc_impure_variable (rhs->symtree->n.sym)
10921 || gfc_is_coindexed (rhs)))
10922 {
10923 /* F2008, C1283. */
10924 if (gfc_is_coindexed (rhs))
10925 gfc_error ("Coindexed expression at %L is assigned to "
10926 "a derived type variable with a POINTER "
10927 "component in a PURE procedure",
10928 &rhs->where);
10929 else
10930 /* F2008, C1283 (4). */
10931 gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
10932 "shall not be used as the expr at %L of an intrinsic "
10933 "assignment statement in which the variable is of a "
10934 "derived type if the derived type has a pointer "
10935 "component at any level of component selection.",
10936 &rhs->where);
10937 return rval;
10938 }
10939
10940 /* Fortran 2008, C1283. */
10941 if (gfc_is_coindexed (lhs))
10942 {
10943 gfc_error ("Assignment to coindexed variable at %L in a PURE "
10944 "procedure", &rhs->where);
10945 return rval;
10946 }
10947 }
10948
10949 if (gfc_implicit_pure (NULL))
10950 {
10951 if (lhs->expr_type == EXPR_VARIABLE
10952 && lhs->symtree->n.sym != gfc_current_ns->proc_name
10953 && lhs->symtree->n.sym->ns != gfc_current_ns)
10954 gfc_unset_implicit_pure (NULL);
10955
10956 if (lhs->ts.type == BT_DERIVED
10957 && lhs->expr_type == EXPR_VARIABLE
10958 && lhs->ts.u.derived->attr.pointer_comp
10959 && rhs->expr_type == EXPR_VARIABLE
10960 && (gfc_impure_variable (rhs->symtree->n.sym)
10961 || gfc_is_coindexed (rhs)))
10962 gfc_unset_implicit_pure (NULL);
10963
10964 /* Fortran 2008, C1283. */
10965 if (gfc_is_coindexed (lhs))
10966 gfc_unset_implicit_pure (NULL);
10967 }
10968
10969 /* F2008, 7.2.1.2. */
10970 attr = gfc_expr_attr (lhs);
10971 if (lhs->ts.type == BT_CLASS && attr.allocatable)
10972 {
10973 if (attr.codimension)
10974 {
10975 gfc_error ("Assignment to polymorphic coarray at %L is not "
10976 "permitted", &lhs->where);
10977 return false;
10978 }
10979 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
10980 "polymorphic variable at %L", &lhs->where))
10981 return false;
10982 if (!flag_realloc_lhs)
10983 {
10984 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
10985 "requires %<-frealloc-lhs%>", &lhs->where);
10986 return false;
10987 }
10988 }
10989 else if (lhs->ts.type == BT_CLASS)
10990 {
10991 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
10992 "assignment at %L - check that there is a matching specific "
10993 "subroutine for '=' operator", &lhs->where);
10994 return false;
10995 }
10996
10997 bool lhs_coindexed = gfc_is_coindexed (lhs);
10998
10999 /* F2008, Section 7.2.1.2. */
11000 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
11001 {
11002 gfc_error ("Coindexed variable must not have an allocatable ultimate "
11003 "component in assignment at %L", &lhs->where);
11004 return false;
11005 }
11006
11007 /* Assign the 'data' of a class object to a derived type. */
11008 if (lhs->ts.type == BT_DERIVED
11009 && rhs->ts.type == BT_CLASS
11010 && rhs->expr_type != EXPR_ARRAY)
11011 gfc_add_data_component (rhs);
11012
11013 /* Make sure there is a vtable and, in particular, a _copy for the
11014 rhs type. */
11015 if (UNLIMITED_POLY (lhs) && lhs->rank && rhs->ts.type != BT_CLASS)
11016 gfc_find_vtab (&rhs->ts);
11017
11018 bool caf_convert_to_send = flag_coarray == GFC_FCOARRAY_LIB
11019 && (lhs_coindexed
11020 || (code->expr2->expr_type == EXPR_FUNCTION
11021 && code->expr2->value.function.isym
11022 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
11023 && (code->expr1->rank == 0 || code->expr2->rank != 0)
11024 && !gfc_expr_attr (rhs).allocatable
11025 && !gfc_has_vector_subscript (rhs)));
11026
11027 gfc_check_assign (lhs, rhs, 1, !caf_convert_to_send);
11028
11029 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
11030 Additionally, insert this code when the RHS is a CAF as we then use the
11031 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
11032 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
11033 noncoindexed array and the RHS is a coindexed scalar, use the normal code
11034 path. */
11035 if (caf_convert_to_send)
11036 {
11037 if (code->expr2->expr_type == EXPR_FUNCTION
11038 && code->expr2->value.function.isym
11039 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
11040 remove_caf_get_intrinsic (code->expr2);
11041 code->op = EXEC_CALL;
11042 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
11043 code->resolved_sym = code->symtree->n.sym;
11044 code->resolved_sym->attr.flavor = FL_PROCEDURE;
11045 code->resolved_sym->attr.intrinsic = 1;
11046 code->resolved_sym->attr.subroutine = 1;
11047 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
11048 gfc_commit_symbol (code->resolved_sym);
11049 code->ext.actual = gfc_get_actual_arglist ();
11050 code->ext.actual->expr = lhs;
11051 code->ext.actual->next = gfc_get_actual_arglist ();
11052 code->ext.actual->next->expr = rhs;
11053 code->expr1 = NULL;
11054 code->expr2 = NULL;
11055 }
11056
11057 return false;
11058 }
11059
11060
11061 /* Add a component reference onto an expression. */
11062
11063 static void
11064 add_comp_ref (gfc_expr *e, gfc_component *c)
11065 {
11066 gfc_ref **ref;
11067 ref = &(e->ref);
11068 while (*ref)
11069 ref = &((*ref)->next);
11070 *ref = gfc_get_ref ();
11071 (*ref)->type = REF_COMPONENT;
11072 (*ref)->u.c.sym = e->ts.u.derived;
11073 (*ref)->u.c.component = c;
11074 e->ts = c->ts;
11075
11076 /* Add a full array ref, as necessary. */
11077 if (c->as)
11078 {
11079 gfc_add_full_array_ref (e, c->as);
11080 e->rank = c->as->rank;
11081 }
11082 }
11083
11084
11085 /* Build an assignment. Keep the argument 'op' for future use, so that
11086 pointer assignments can be made. */
11087
11088 static gfc_code *
11089 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
11090 gfc_component *comp1, gfc_component *comp2, locus loc)
11091 {
11092 gfc_code *this_code;
11093
11094 this_code = gfc_get_code (op);
11095 this_code->next = NULL;
11096 this_code->expr1 = gfc_copy_expr (expr1);
11097 this_code->expr2 = gfc_copy_expr (expr2);
11098 this_code->loc = loc;
11099 if (comp1 && comp2)
11100 {
11101 add_comp_ref (this_code->expr1, comp1);
11102 add_comp_ref (this_code->expr2, comp2);
11103 }
11104
11105 return this_code;
11106 }
11107
11108
11109 /* Makes a temporary variable expression based on the characteristics of
11110 a given variable expression. */
11111
11112 static gfc_expr*
11113 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
11114 {
11115 static int serial = 0;
11116 char name[GFC_MAX_SYMBOL_LEN];
11117 gfc_symtree *tmp;
11118 gfc_array_spec *as;
11119 gfc_array_ref *aref;
11120 gfc_ref *ref;
11121
11122 sprintf (name, GFC_PREFIX("DA%d"), serial++);
11123 gfc_get_sym_tree (name, ns, &tmp, false);
11124 gfc_add_type (tmp->n.sym, &e->ts, NULL);
11125
11126 if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
11127 tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
11128 NULL,
11129 e->value.character.length);
11130
11131 as = NULL;
11132 ref = NULL;
11133 aref = NULL;
11134
11135 /* Obtain the arrayspec for the temporary. */
11136 if (e->rank && e->expr_type != EXPR_ARRAY
11137 && e->expr_type != EXPR_FUNCTION
11138 && e->expr_type != EXPR_OP)
11139 {
11140 aref = gfc_find_array_ref (e);
11141 if (e->expr_type == EXPR_VARIABLE
11142 && e->symtree->n.sym->as == aref->as)
11143 as = aref->as;
11144 else
11145 {
11146 for (ref = e->ref; ref; ref = ref->next)
11147 if (ref->type == REF_COMPONENT
11148 && ref->u.c.component->as == aref->as)
11149 {
11150 as = aref->as;
11151 break;
11152 }
11153 }
11154 }
11155
11156 /* Add the attributes and the arrayspec to the temporary. */
11157 tmp->n.sym->attr = gfc_expr_attr (e);
11158 tmp->n.sym->attr.function = 0;
11159 tmp->n.sym->attr.result = 0;
11160 tmp->n.sym->attr.flavor = FL_VARIABLE;
11161 tmp->n.sym->attr.dummy = 0;
11162 tmp->n.sym->attr.intent = INTENT_UNKNOWN;
11163
11164 if (as)
11165 {
11166 tmp->n.sym->as = gfc_copy_array_spec (as);
11167 if (!ref)
11168 ref = e->ref;
11169 if (as->type == AS_DEFERRED)
11170 tmp->n.sym->attr.allocatable = 1;
11171 }
11172 else if (e->rank && (e->expr_type == EXPR_ARRAY
11173 || e->expr_type == EXPR_FUNCTION
11174 || e->expr_type == EXPR_OP))
11175 {
11176 tmp->n.sym->as = gfc_get_array_spec ();
11177 tmp->n.sym->as->type = AS_DEFERRED;
11178 tmp->n.sym->as->rank = e->rank;
11179 tmp->n.sym->attr.allocatable = 1;
11180 tmp->n.sym->attr.dimension = 1;
11181 }
11182 else
11183 tmp->n.sym->attr.dimension = 0;
11184
11185 gfc_set_sym_referenced (tmp->n.sym);
11186 gfc_commit_symbol (tmp->n.sym);
11187 e = gfc_lval_expr_from_sym (tmp->n.sym);
11188
11189 /* Should the lhs be a section, use its array ref for the
11190 temporary expression. */
11191 if (aref && aref->type != AR_FULL)
11192 {
11193 gfc_free_ref_list (e->ref);
11194 e->ref = gfc_copy_ref (ref);
11195 }
11196 return e;
11197 }
11198
11199
11200 /* Add one line of code to the code chain, making sure that 'head' and
11201 'tail' are appropriately updated. */
11202
11203 static void
11204 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
11205 {
11206 gcc_assert (this_code);
11207 if (*head == NULL)
11208 *head = *tail = *this_code;
11209 else
11210 *tail = gfc_append_code (*tail, *this_code);
11211 *this_code = NULL;
11212 }
11213
11214
11215 /* Counts the potential number of part array references that would
11216 result from resolution of typebound defined assignments. */
11217
11218 static int
11219 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
11220 {
11221 gfc_component *c;
11222 int c_depth = 0, t_depth;
11223
11224 for (c= derived->components; c; c = c->next)
11225 {
11226 if ((!gfc_bt_struct (c->ts.type)
11227 || c->attr.pointer
11228 || c->attr.allocatable
11229 || c->attr.proc_pointer_comp
11230 || c->attr.class_pointer
11231 || c->attr.proc_pointer)
11232 && !c->attr.defined_assign_comp)
11233 continue;
11234
11235 if (c->as && c_depth == 0)
11236 c_depth = 1;
11237
11238 if (c->ts.u.derived->attr.defined_assign_comp)
11239 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
11240 c->as ? 1 : 0);
11241 else
11242 t_depth = 0;
11243
11244 c_depth = t_depth > c_depth ? t_depth : c_depth;
11245 }
11246 return depth + c_depth;
11247 }
11248
11249
11250 /* Implement 7.2.1.3 of the F08 standard:
11251 "An intrinsic assignment where the variable is of derived type is
11252 performed as if each component of the variable were assigned from the
11253 corresponding component of expr using pointer assignment (7.2.2) for
11254 each pointer component, defined assignment for each nonpointer
11255 nonallocatable component of a type that has a type-bound defined
11256 assignment consistent with the component, intrinsic assignment for
11257 each other nonpointer nonallocatable component, ..."
11258
11259 The pointer assignments are taken care of by the intrinsic
11260 assignment of the structure itself. This function recursively adds
11261 defined assignments where required. The recursion is accomplished
11262 by calling gfc_resolve_code.
11263
11264 When the lhs in a defined assignment has intent INOUT, we need a
11265 temporary for the lhs. In pseudo-code:
11266
11267 ! Only call function lhs once.
11268 if (lhs is not a constant or an variable)
11269 temp_x = expr2
11270 expr2 => temp_x
11271 ! Do the intrinsic assignment
11272 expr1 = expr2
11273 ! Now do the defined assignments
11274 do over components with typebound defined assignment [%cmp]
11275 #if one component's assignment procedure is INOUT
11276 t1 = expr1
11277 #if expr2 non-variable
11278 temp_x = expr2
11279 expr2 => temp_x
11280 # endif
11281 expr1 = expr2
11282 # for each cmp
11283 t1%cmp {defined=} expr2%cmp
11284 expr1%cmp = t1%cmp
11285 #else
11286 expr1 = expr2
11287
11288 # for each cmp
11289 expr1%cmp {defined=} expr2%cmp
11290 #endif
11291 */
11292
11293 /* The temporary assignments have to be put on top of the additional
11294 code to avoid the result being changed by the intrinsic assignment.
11295 */
11296 static int component_assignment_level = 0;
11297 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
11298
11299 static void
11300 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
11301 {
11302 gfc_component *comp1, *comp2;
11303 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
11304 gfc_expr *t1;
11305 int error_count, depth;
11306
11307 gfc_get_errors (NULL, &error_count);
11308
11309 /* Filter out continuing processing after an error. */
11310 if (error_count
11311 || (*code)->expr1->ts.type != BT_DERIVED
11312 || (*code)->expr2->ts.type != BT_DERIVED)
11313 return;
11314
11315 /* TODO: Handle more than one part array reference in assignments. */
11316 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
11317 (*code)->expr1->rank ? 1 : 0);
11318 if (depth > 1)
11319 {
11320 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
11321 "done because multiple part array references would "
11322 "occur in intermediate expressions.", &(*code)->loc);
11323 return;
11324 }
11325
11326 component_assignment_level++;
11327
11328 /* Create a temporary so that functions get called only once. */
11329 if ((*code)->expr2->expr_type != EXPR_VARIABLE
11330 && (*code)->expr2->expr_type != EXPR_CONSTANT)
11331 {
11332 gfc_expr *tmp_expr;
11333
11334 /* Assign the rhs to the temporary. */
11335 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
11336 this_code = build_assignment (EXEC_ASSIGN,
11337 tmp_expr, (*code)->expr2,
11338 NULL, NULL, (*code)->loc);
11339 /* Add the code and substitute the rhs expression. */
11340 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
11341 gfc_free_expr ((*code)->expr2);
11342 (*code)->expr2 = tmp_expr;
11343 }
11344
11345 /* Do the intrinsic assignment. This is not needed if the lhs is one
11346 of the temporaries generated here, since the intrinsic assignment
11347 to the final result already does this. */
11348 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
11349 {
11350 this_code = build_assignment (EXEC_ASSIGN,
11351 (*code)->expr1, (*code)->expr2,
11352 NULL, NULL, (*code)->loc);
11353 add_code_to_chain (&this_code, &head, &tail);
11354 }
11355
11356 comp1 = (*code)->expr1->ts.u.derived->components;
11357 comp2 = (*code)->expr2->ts.u.derived->components;
11358
11359 t1 = NULL;
11360 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
11361 {
11362 bool inout = false;
11363
11364 /* The intrinsic assignment does the right thing for pointers
11365 of all kinds and allocatable components. */
11366 if (!gfc_bt_struct (comp1->ts.type)
11367 || comp1->attr.pointer
11368 || comp1->attr.allocatable
11369 || comp1->attr.proc_pointer_comp
11370 || comp1->attr.class_pointer
11371 || comp1->attr.proc_pointer)
11372 continue;
11373
11374 /* Make an assigment for this component. */
11375 this_code = build_assignment (EXEC_ASSIGN,
11376 (*code)->expr1, (*code)->expr2,
11377 comp1, comp2, (*code)->loc);
11378
11379 /* Convert the assignment if there is a defined assignment for
11380 this type. Otherwise, using the call from gfc_resolve_code,
11381 recurse into its components. */
11382 gfc_resolve_code (this_code, ns);
11383
11384 if (this_code->op == EXEC_ASSIGN_CALL)
11385 {
11386 gfc_formal_arglist *dummy_args;
11387 gfc_symbol *rsym;
11388 /* Check that there is a typebound defined assignment. If not,
11389 then this must be a module defined assignment. We cannot
11390 use the defined_assign_comp attribute here because it must
11391 be this derived type that has the defined assignment and not
11392 a parent type. */
11393 if (!(comp1->ts.u.derived->f2k_derived
11394 && comp1->ts.u.derived->f2k_derived
11395 ->tb_op[INTRINSIC_ASSIGN]))
11396 {
11397 gfc_free_statements (this_code);
11398 this_code = NULL;
11399 continue;
11400 }
11401
11402 /* If the first argument of the subroutine has intent INOUT
11403 a temporary must be generated and used instead. */
11404 rsym = this_code->resolved_sym;
11405 dummy_args = gfc_sym_get_dummy_args (rsym);
11406 if (dummy_args
11407 && dummy_args->sym->attr.intent == INTENT_INOUT)
11408 {
11409 gfc_code *temp_code;
11410 inout = true;
11411
11412 /* Build the temporary required for the assignment and put
11413 it at the head of the generated code. */
11414 if (!t1)
11415 {
11416 t1 = get_temp_from_expr ((*code)->expr1, ns);
11417 temp_code = build_assignment (EXEC_ASSIGN,
11418 t1, (*code)->expr1,
11419 NULL, NULL, (*code)->loc);
11420
11421 /* For allocatable LHS, check whether it is allocated. Note
11422 that allocatable components with defined assignment are
11423 not yet support. See PR 57696. */
11424 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
11425 {
11426 gfc_code *block;
11427 gfc_expr *e =
11428 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
11429 block = gfc_get_code (EXEC_IF);
11430 block->block = gfc_get_code (EXEC_IF);
11431 block->block->expr1
11432 = gfc_build_intrinsic_call (ns,
11433 GFC_ISYM_ALLOCATED, "allocated",
11434 (*code)->loc, 1, e);
11435 block->block->next = temp_code;
11436 temp_code = block;
11437 }
11438 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
11439 }
11440
11441 /* Replace the first actual arg with the component of the
11442 temporary. */
11443 gfc_free_expr (this_code->ext.actual->expr);
11444 this_code->ext.actual->expr = gfc_copy_expr (t1);
11445 add_comp_ref (this_code->ext.actual->expr, comp1);
11446
11447 /* If the LHS variable is allocatable and wasn't allocated and
11448 the temporary is allocatable, pointer assign the address of
11449 the freshly allocated LHS to the temporary. */
11450 if ((*code)->expr1->symtree->n.sym->attr.allocatable
11451 && gfc_expr_attr ((*code)->expr1).allocatable)
11452 {
11453 gfc_code *block;
11454 gfc_expr *cond;
11455
11456 cond = gfc_get_expr ();
11457 cond->ts.type = BT_LOGICAL;
11458 cond->ts.kind = gfc_default_logical_kind;
11459 cond->expr_type = EXPR_OP;
11460 cond->where = (*code)->loc;
11461 cond->value.op.op = INTRINSIC_NOT;
11462 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
11463 GFC_ISYM_ALLOCATED, "allocated",
11464 (*code)->loc, 1, gfc_copy_expr (t1));
11465 block = gfc_get_code (EXEC_IF);
11466 block->block = gfc_get_code (EXEC_IF);
11467 block->block->expr1 = cond;
11468 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
11469 t1, (*code)->expr1,
11470 NULL, NULL, (*code)->loc);
11471 add_code_to_chain (&block, &head, &tail);
11472 }
11473 }
11474 }
11475 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
11476 {
11477 /* Don't add intrinsic assignments since they are already
11478 effected by the intrinsic assignment of the structure. */
11479 gfc_free_statements (this_code);
11480 this_code = NULL;
11481 continue;
11482 }
11483
11484 add_code_to_chain (&this_code, &head, &tail);
11485
11486 if (t1 && inout)
11487 {
11488 /* Transfer the value to the final result. */
11489 this_code = build_assignment (EXEC_ASSIGN,
11490 (*code)->expr1, t1,
11491 comp1, comp2, (*code)->loc);
11492 add_code_to_chain (&this_code, &head, &tail);
11493 }
11494 }
11495
11496 /* Put the temporary assignments at the top of the generated code. */
11497 if (tmp_head && component_assignment_level == 1)
11498 {
11499 gfc_append_code (tmp_head, head);
11500 head = tmp_head;
11501 tmp_head = tmp_tail = NULL;
11502 }
11503
11504 // If we did a pointer assignment - thus, we need to ensure that the LHS is
11505 // not accidentally deallocated. Hence, nullify t1.
11506 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
11507 && gfc_expr_attr ((*code)->expr1).allocatable)
11508 {
11509 gfc_code *block;
11510 gfc_expr *cond;
11511 gfc_expr *e;
11512
11513 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
11514 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
11515 (*code)->loc, 2, gfc_copy_expr (t1), e);
11516 block = gfc_get_code (EXEC_IF);
11517 block->block = gfc_get_code (EXEC_IF);
11518 block->block->expr1 = cond;
11519 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
11520 t1, gfc_get_null_expr (&(*code)->loc),
11521 NULL, NULL, (*code)->loc);
11522 gfc_append_code (tail, block);
11523 tail = block;
11524 }
11525
11526 /* Now attach the remaining code chain to the input code. Step on
11527 to the end of the new code since resolution is complete. */
11528 gcc_assert ((*code)->op == EXEC_ASSIGN);
11529 tail->next = (*code)->next;
11530 /* Overwrite 'code' because this would place the intrinsic assignment
11531 before the temporary for the lhs is created. */
11532 gfc_free_expr ((*code)->expr1);
11533 gfc_free_expr ((*code)->expr2);
11534 **code = *head;
11535 if (head != tail)
11536 free (head);
11537 *code = tail;
11538
11539 component_assignment_level--;
11540 }
11541
11542
11543 /* F2008: Pointer function assignments are of the form:
11544 ptr_fcn (args) = expr
11545 This function breaks these assignments into two statements:
11546 temporary_pointer => ptr_fcn(args)
11547 temporary_pointer = expr */
11548
11549 static bool
11550 resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
11551 {
11552 gfc_expr *tmp_ptr_expr;
11553 gfc_code *this_code;
11554 gfc_component *comp;
11555 gfc_symbol *s;
11556
11557 if ((*code)->expr1->expr_type != EXPR_FUNCTION)
11558 return false;
11559
11560 /* Even if standard does not support this feature, continue to build
11561 the two statements to avoid upsetting frontend_passes.c. */
11562 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
11563 "%L", &(*code)->loc);
11564
11565 comp = gfc_get_proc_ptr_comp ((*code)->expr1);
11566
11567 if (comp)
11568 s = comp->ts.interface;
11569 else
11570 s = (*code)->expr1->symtree->n.sym;
11571
11572 if (s == NULL || !s->result->attr.pointer)
11573 {
11574 gfc_error ("The function result on the lhs of the assignment at "
11575 "%L must have the pointer attribute.",
11576 &(*code)->expr1->where);
11577 (*code)->op = EXEC_NOP;
11578 return false;
11579 }
11580
11581 tmp_ptr_expr = get_temp_from_expr ((*code)->expr2, ns);
11582
11583 /* get_temp_from_expression is set up for ordinary assignments. To that
11584 end, where array bounds are not known, arrays are made allocatable.
11585 Change the temporary to a pointer here. */
11586 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
11587 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
11588 tmp_ptr_expr->where = (*code)->loc;
11589
11590 this_code = build_assignment (EXEC_ASSIGN,
11591 tmp_ptr_expr, (*code)->expr2,
11592 NULL, NULL, (*code)->loc);
11593 this_code->next = (*code)->next;
11594 (*code)->next = this_code;
11595 (*code)->op = EXEC_POINTER_ASSIGN;
11596 (*code)->expr2 = (*code)->expr1;
11597 (*code)->expr1 = tmp_ptr_expr;
11598
11599 return true;
11600 }
11601
11602
11603 /* Deferred character length assignments from an operator expression
11604 require a temporary because the character length of the lhs can
11605 change in the course of the assignment. */
11606
11607 static bool
11608 deferred_op_assign (gfc_code **code, gfc_namespace *ns)
11609 {
11610 gfc_expr *tmp_expr;
11611 gfc_code *this_code;
11612
11613 if (!((*code)->expr1->ts.type == BT_CHARACTER
11614 && (*code)->expr1->ts.deferred && (*code)->expr1->rank
11615 && (*code)->expr2->expr_type == EXPR_OP))
11616 return false;
11617
11618 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
11619 return false;
11620
11621 if (gfc_expr_attr ((*code)->expr1).pointer)
11622 return false;
11623
11624 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
11625 tmp_expr->where = (*code)->loc;
11626
11627 /* A new charlen is required to ensure that the variable string
11628 length is different to that of the original lhs. */
11629 tmp_expr->ts.u.cl = gfc_get_charlen();
11630 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
11631 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
11632 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
11633
11634 tmp_expr->symtree->n.sym->ts.deferred = 1;
11635
11636 this_code = build_assignment (EXEC_ASSIGN,
11637 (*code)->expr1,
11638 gfc_copy_expr (tmp_expr),
11639 NULL, NULL, (*code)->loc);
11640
11641 (*code)->expr1 = tmp_expr;
11642
11643 this_code->next = (*code)->next;
11644 (*code)->next = this_code;
11645
11646 return true;
11647 }
11648
11649
11650 /* Given a block of code, recursively resolve everything pointed to by this
11651 code block. */
11652
11653 void
11654 gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
11655 {
11656 int omp_workshare_save;
11657 int forall_save, do_concurrent_save;
11658 code_stack frame;
11659 bool t;
11660
11661 frame.prev = cs_base;
11662 frame.head = code;
11663 cs_base = &frame;
11664
11665 find_reachable_labels (code);
11666
11667 for (; code; code = code->next)
11668 {
11669 frame.current = code;
11670 forall_save = forall_flag;
11671 do_concurrent_save = gfc_do_concurrent_flag;
11672
11673 if (code->op == EXEC_FORALL)
11674 {
11675 forall_flag = 1;
11676 gfc_resolve_forall (code, ns, forall_save);
11677 forall_flag = 2;
11678 }
11679 else if (code->block)
11680 {
11681 omp_workshare_save = -1;
11682 switch (code->op)
11683 {
11684 case EXEC_OACC_PARALLEL_LOOP:
11685 case EXEC_OACC_PARALLEL:
11686 case EXEC_OACC_KERNELS_LOOP:
11687 case EXEC_OACC_KERNELS:
11688 case EXEC_OACC_SERIAL_LOOP:
11689 case EXEC_OACC_SERIAL:
11690 case EXEC_OACC_DATA:
11691 case EXEC_OACC_HOST_DATA:
11692 case EXEC_OACC_LOOP:
11693 gfc_resolve_oacc_blocks (code, ns);
11694 break;
11695 case EXEC_OMP_PARALLEL_WORKSHARE:
11696 omp_workshare_save = omp_workshare_flag;
11697 omp_workshare_flag = 1;
11698 gfc_resolve_omp_parallel_blocks (code, ns);
11699 break;
11700 case EXEC_OMP_PARALLEL:
11701 case EXEC_OMP_PARALLEL_DO:
11702 case EXEC_OMP_PARALLEL_DO_SIMD:
11703 case EXEC_OMP_PARALLEL_SECTIONS:
11704 case EXEC_OMP_TARGET_PARALLEL:
11705 case EXEC_OMP_TARGET_PARALLEL_DO:
11706 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
11707 case EXEC_OMP_TARGET_TEAMS:
11708 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
11709 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
11710 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11711 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
11712 case EXEC_OMP_TASK:
11713 case EXEC_OMP_TASKLOOP:
11714 case EXEC_OMP_TASKLOOP_SIMD:
11715 case EXEC_OMP_TEAMS:
11716 case EXEC_OMP_TEAMS_DISTRIBUTE:
11717 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
11718 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11719 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
11720 omp_workshare_save = omp_workshare_flag;
11721 omp_workshare_flag = 0;
11722 gfc_resolve_omp_parallel_blocks (code, ns);
11723 break;
11724 case EXEC_OMP_DISTRIBUTE:
11725 case EXEC_OMP_DISTRIBUTE_SIMD:
11726 case EXEC_OMP_DO:
11727 case EXEC_OMP_DO_SIMD:
11728 case EXEC_OMP_SIMD:
11729 case EXEC_OMP_TARGET_SIMD:
11730 gfc_resolve_omp_do_blocks (code, ns);
11731 break;
11732 case EXEC_SELECT_TYPE:
11733 /* Blocks are handled in resolve_select_type because we have
11734 to transform the SELECT TYPE into ASSOCIATE first. */
11735 break;
11736 case EXEC_DO_CONCURRENT:
11737 gfc_do_concurrent_flag = 1;
11738 gfc_resolve_blocks (code->block, ns);
11739 gfc_do_concurrent_flag = 2;
11740 break;
11741 case EXEC_OMP_WORKSHARE:
11742 omp_workshare_save = omp_workshare_flag;
11743 omp_workshare_flag = 1;
11744 /* FALL THROUGH */
11745 default:
11746 gfc_resolve_blocks (code->block, ns);
11747 break;
11748 }
11749
11750 if (omp_workshare_save != -1)
11751 omp_workshare_flag = omp_workshare_save;
11752 }
11753 start:
11754 t = true;
11755 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
11756 t = gfc_resolve_expr (code->expr1);
11757 forall_flag = forall_save;
11758 gfc_do_concurrent_flag = do_concurrent_save;
11759
11760 if (!gfc_resolve_expr (code->expr2))
11761 t = false;
11762
11763 if (code->op == EXEC_ALLOCATE
11764 && !gfc_resolve_expr (code->expr3))
11765 t = false;
11766
11767 switch (code->op)
11768 {
11769 case EXEC_NOP:
11770 case EXEC_END_BLOCK:
11771 case EXEC_END_NESTED_BLOCK:
11772 case EXEC_CYCLE:
11773 case EXEC_PAUSE:
11774 case EXEC_STOP:
11775 case EXEC_ERROR_STOP:
11776 case EXEC_EXIT:
11777 case EXEC_CONTINUE:
11778 case EXEC_DT_END:
11779 case EXEC_ASSIGN_CALL:
11780 break;
11781
11782 case EXEC_CRITICAL:
11783 resolve_critical (code);
11784 break;
11785
11786 case EXEC_SYNC_ALL:
11787 case EXEC_SYNC_IMAGES:
11788 case EXEC_SYNC_MEMORY:
11789 resolve_sync (code);
11790 break;
11791
11792 case EXEC_LOCK:
11793 case EXEC_UNLOCK:
11794 case EXEC_EVENT_POST:
11795 case EXEC_EVENT_WAIT:
11796 resolve_lock_unlock_event (code);
11797 break;
11798
11799 case EXEC_FAIL_IMAGE:
11800 case EXEC_FORM_TEAM:
11801 case EXEC_CHANGE_TEAM:
11802 case EXEC_END_TEAM:
11803 case EXEC_SYNC_TEAM:
11804 break;
11805
11806 case EXEC_ENTRY:
11807 /* Keep track of which entry we are up to. */
11808 current_entry_id = code->ext.entry->id;
11809 break;
11810
11811 case EXEC_WHERE:
11812 resolve_where (code, NULL);
11813 break;
11814
11815 case EXEC_GOTO:
11816 if (code->expr1 != NULL)
11817 {
11818 if (code->expr1->expr_type != EXPR_VARIABLE
11819 || code->expr1->ts.type != BT_INTEGER
11820 || (code->expr1->ref
11821 && code->expr1->ref->type == REF_ARRAY)
11822 || code->expr1->symtree == NULL
11823 || (code->expr1->symtree->n.sym
11824 && (code->expr1->symtree->n.sym->attr.flavor
11825 == FL_PARAMETER)))
11826 gfc_error ("ASSIGNED GOTO statement at %L requires a "
11827 "scalar INTEGER variable", &code->expr1->where);
11828 else if (code->expr1->symtree->n.sym
11829 && code->expr1->symtree->n.sym->attr.assign != 1)
11830 gfc_error ("Variable %qs has not been assigned a target "
11831 "label at %L", code->expr1->symtree->n.sym->name,
11832 &code->expr1->where);
11833 }
11834 else
11835 resolve_branch (code->label1, code);
11836 break;
11837
11838 case EXEC_RETURN:
11839 if (code->expr1 != NULL
11840 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
11841 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
11842 "INTEGER return specifier", &code->expr1->where);
11843 break;
11844
11845 case EXEC_INIT_ASSIGN:
11846 case EXEC_END_PROCEDURE:
11847 break;
11848
11849 case EXEC_ASSIGN:
11850 if (!t)
11851 break;
11852
11853 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
11854 the LHS. */
11855 if (code->expr1->expr_type == EXPR_FUNCTION
11856 && code->expr1->value.function.isym
11857 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
11858 remove_caf_get_intrinsic (code->expr1);
11859
11860 /* If this is a pointer function in an lvalue variable context,
11861 the new code will have to be resolved afresh. This is also the
11862 case with an error, where the code is transformed into NOP to
11863 prevent ICEs downstream. */
11864 if (resolve_ptr_fcn_assign (&code, ns)
11865 || code->op == EXEC_NOP)
11866 goto start;
11867
11868 if (!gfc_check_vardef_context (code->expr1, false, false, false,
11869 _("assignment")))
11870 break;
11871
11872 if (resolve_ordinary_assign (code, ns))
11873 {
11874 if (code->op == EXEC_COMPCALL)
11875 goto compcall;
11876 else
11877 goto call;
11878 }
11879
11880 /* Check for dependencies in deferred character length array
11881 assignments and generate a temporary, if necessary. */
11882 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
11883 break;
11884
11885 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
11886 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
11887 && code->expr1->ts.u.derived
11888 && code->expr1->ts.u.derived->attr.defined_assign_comp)
11889 generate_component_assignments (&code, ns);
11890
11891 break;
11892
11893 case EXEC_LABEL_ASSIGN:
11894 if (code->label1->defined == ST_LABEL_UNKNOWN)
11895 gfc_error ("Label %d referenced at %L is never defined",
11896 code->label1->value, &code->label1->where);
11897 if (t
11898 && (code->expr1->expr_type != EXPR_VARIABLE
11899 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
11900 || code->expr1->symtree->n.sym->ts.kind
11901 != gfc_default_integer_kind
11902 || code->expr1->symtree->n.sym->as != NULL))
11903 gfc_error ("ASSIGN statement at %L requires a scalar "
11904 "default INTEGER variable", &code->expr1->where);
11905 break;
11906
11907 case EXEC_POINTER_ASSIGN:
11908 {
11909 gfc_expr* e;
11910
11911 if (!t)
11912 break;
11913
11914 /* This is both a variable definition and pointer assignment
11915 context, so check both of them. For rank remapping, a final
11916 array ref may be present on the LHS and fool gfc_expr_attr
11917 used in gfc_check_vardef_context. Remove it. */
11918 e = remove_last_array_ref (code->expr1);
11919 t = gfc_check_vardef_context (e, true, false, false,
11920 _("pointer assignment"));
11921 if (t)
11922 t = gfc_check_vardef_context (e, false, false, false,
11923 _("pointer assignment"));
11924 gfc_free_expr (e);
11925
11926 t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
11927
11928 if (!t)
11929 break;
11930
11931 /* Assigning a class object always is a regular assign. */
11932 if (code->expr2->ts.type == BT_CLASS
11933 && code->expr1->ts.type == BT_CLASS
11934 && !CLASS_DATA (code->expr2)->attr.dimension
11935 && !(gfc_expr_attr (code->expr1).proc_pointer
11936 && code->expr2->expr_type == EXPR_VARIABLE
11937 && code->expr2->symtree->n.sym->attr.flavor
11938 == FL_PROCEDURE))
11939 code->op = EXEC_ASSIGN;
11940 break;
11941 }
11942
11943 case EXEC_ARITHMETIC_IF:
11944 {
11945 gfc_expr *e = code->expr1;
11946
11947 gfc_resolve_expr (e);
11948 if (e->expr_type == EXPR_NULL)
11949 gfc_error ("Invalid NULL at %L", &e->where);
11950
11951 if (t && (e->rank > 0
11952 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
11953 gfc_error ("Arithmetic IF statement at %L requires a scalar "
11954 "REAL or INTEGER expression", &e->where);
11955
11956 resolve_branch (code->label1, code);
11957 resolve_branch (code->label2, code);
11958 resolve_branch (code->label3, code);
11959 }
11960 break;
11961
11962 case EXEC_IF:
11963 if (t && code->expr1 != NULL
11964 && (code->expr1->ts.type != BT_LOGICAL
11965 || code->expr1->rank != 0))
11966 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
11967 &code->expr1->where);
11968 break;
11969
11970 case EXEC_CALL:
11971 call:
11972 resolve_call (code);
11973 break;
11974
11975 case EXEC_COMPCALL:
11976 compcall:
11977 resolve_typebound_subroutine (code);
11978 break;
11979
11980 case EXEC_CALL_PPC:
11981 resolve_ppc_call (code);
11982 break;
11983
11984 case EXEC_SELECT:
11985 /* Select is complicated. Also, a SELECT construct could be
11986 a transformed computed GOTO. */
11987 resolve_select (code, false);
11988 break;
11989
11990 case EXEC_SELECT_TYPE:
11991 resolve_select_type (code, ns);
11992 break;
11993
11994 case EXEC_SELECT_RANK:
11995 resolve_select_rank (code, ns);
11996 break;
11997
11998 case EXEC_BLOCK:
11999 resolve_block_construct (code);
12000 break;
12001
12002 case EXEC_DO:
12003 if (code->ext.iterator != NULL)
12004 {
12005 gfc_iterator *iter = code->ext.iterator;
12006 if (gfc_resolve_iterator (iter, true, false))
12007 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
12008 true);
12009 }
12010 break;
12011
12012 case EXEC_DO_WHILE:
12013 if (code->expr1 == NULL)
12014 gfc_internal_error ("gfc_resolve_code(): No expression on "
12015 "DO WHILE");
12016 if (t
12017 && (code->expr1->rank != 0
12018 || code->expr1->ts.type != BT_LOGICAL))
12019 gfc_error ("Exit condition of DO WHILE loop at %L must be "
12020 "a scalar LOGICAL expression", &code->expr1->where);
12021 break;
12022
12023 case EXEC_ALLOCATE:
12024 if (t)
12025 resolve_allocate_deallocate (code, "ALLOCATE");
12026
12027 break;
12028
12029 case EXEC_DEALLOCATE:
12030 if (t)
12031 resolve_allocate_deallocate (code, "DEALLOCATE");
12032
12033 break;
12034
12035 case EXEC_OPEN:
12036 if (!gfc_resolve_open (code->ext.open, &code->loc))
12037 break;
12038
12039 resolve_branch (code->ext.open->err, code);
12040 break;
12041
12042 case EXEC_CLOSE:
12043 if (!gfc_resolve_close (code->ext.close, &code->loc))
12044 break;
12045
12046 resolve_branch (code->ext.close->err, code);
12047 break;
12048
12049 case EXEC_BACKSPACE:
12050 case EXEC_ENDFILE:
12051 case EXEC_REWIND:
12052 case EXEC_FLUSH:
12053 if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
12054 break;
12055
12056 resolve_branch (code->ext.filepos->err, code);
12057 break;
12058
12059 case EXEC_INQUIRE:
12060 if (!gfc_resolve_inquire (code->ext.inquire))
12061 break;
12062
12063 resolve_branch (code->ext.inquire->err, code);
12064 break;
12065
12066 case EXEC_IOLENGTH:
12067 gcc_assert (code->ext.inquire != NULL);
12068 if (!gfc_resolve_inquire (code->ext.inquire))
12069 break;
12070
12071 resolve_branch (code->ext.inquire->err, code);
12072 break;
12073
12074 case EXEC_WAIT:
12075 if (!gfc_resolve_wait (code->ext.wait))
12076 break;
12077
12078 resolve_branch (code->ext.wait->err, code);
12079 resolve_branch (code->ext.wait->end, code);
12080 resolve_branch (code->ext.wait->eor, code);
12081 break;
12082
12083 case EXEC_READ:
12084 case EXEC_WRITE:
12085 if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
12086 break;
12087
12088 resolve_branch (code->ext.dt->err, code);
12089 resolve_branch (code->ext.dt->end, code);
12090 resolve_branch (code->ext.dt->eor, code);
12091 break;
12092
12093 case EXEC_TRANSFER:
12094 resolve_transfer (code);
12095 break;
12096
12097 case EXEC_DO_CONCURRENT:
12098 case EXEC_FORALL:
12099 resolve_forall_iterators (code->ext.forall_iterator);
12100
12101 if (code->expr1 != NULL
12102 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
12103 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
12104 "expression", &code->expr1->where);
12105 break;
12106
12107 case EXEC_OACC_PARALLEL_LOOP:
12108 case EXEC_OACC_PARALLEL:
12109 case EXEC_OACC_KERNELS_LOOP:
12110 case EXEC_OACC_KERNELS:
12111 case EXEC_OACC_SERIAL_LOOP:
12112 case EXEC_OACC_SERIAL:
12113 case EXEC_OACC_DATA:
12114 case EXEC_OACC_HOST_DATA:
12115 case EXEC_OACC_LOOP:
12116 case EXEC_OACC_UPDATE:
12117 case EXEC_OACC_WAIT:
12118 case EXEC_OACC_CACHE:
12119 case EXEC_OACC_ENTER_DATA:
12120 case EXEC_OACC_EXIT_DATA:
12121 case EXEC_OACC_ATOMIC:
12122 case EXEC_OACC_DECLARE:
12123 gfc_resolve_oacc_directive (code, ns);
12124 break;
12125
12126 case EXEC_OMP_ATOMIC:
12127 case EXEC_OMP_BARRIER:
12128 case EXEC_OMP_CANCEL:
12129 case EXEC_OMP_CANCELLATION_POINT:
12130 case EXEC_OMP_CRITICAL:
12131 case EXEC_OMP_FLUSH:
12132 case EXEC_OMP_DISTRIBUTE:
12133 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
12134 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
12135 case EXEC_OMP_DISTRIBUTE_SIMD:
12136 case EXEC_OMP_DO:
12137 case EXEC_OMP_DO_SIMD:
12138 case EXEC_OMP_MASTER:
12139 case EXEC_OMP_ORDERED:
12140 case EXEC_OMP_SECTIONS:
12141 case EXEC_OMP_SIMD:
12142 case EXEC_OMP_SINGLE:
12143 case EXEC_OMP_TARGET:
12144 case EXEC_OMP_TARGET_DATA:
12145 case EXEC_OMP_TARGET_ENTER_DATA:
12146 case EXEC_OMP_TARGET_EXIT_DATA:
12147 case EXEC_OMP_TARGET_PARALLEL:
12148 case EXEC_OMP_TARGET_PARALLEL_DO:
12149 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
12150 case EXEC_OMP_TARGET_SIMD:
12151 case EXEC_OMP_TARGET_TEAMS:
12152 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
12153 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
12154 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12155 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
12156 case EXEC_OMP_TARGET_UPDATE:
12157 case EXEC_OMP_TASK:
12158 case EXEC_OMP_TASKGROUP:
12159 case EXEC_OMP_TASKLOOP:
12160 case EXEC_OMP_TASKLOOP_SIMD:
12161 case EXEC_OMP_TASKWAIT:
12162 case EXEC_OMP_TASKYIELD:
12163 case EXEC_OMP_TEAMS:
12164 case EXEC_OMP_TEAMS_DISTRIBUTE:
12165 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
12166 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12167 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
12168 case EXEC_OMP_WORKSHARE:
12169 gfc_resolve_omp_directive (code, ns);
12170 break;
12171
12172 case EXEC_OMP_PARALLEL:
12173 case EXEC_OMP_PARALLEL_DO:
12174 case EXEC_OMP_PARALLEL_DO_SIMD:
12175 case EXEC_OMP_PARALLEL_SECTIONS:
12176 case EXEC_OMP_PARALLEL_WORKSHARE:
12177 omp_workshare_save = omp_workshare_flag;
12178 omp_workshare_flag = 0;
12179 gfc_resolve_omp_directive (code, ns);
12180 omp_workshare_flag = omp_workshare_save;
12181 break;
12182
12183 default:
12184 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
12185 }
12186 }
12187
12188 cs_base = frame.prev;
12189 }
12190
12191
12192 /* Resolve initial values and make sure they are compatible with
12193 the variable. */
12194
12195 static void
12196 resolve_values (gfc_symbol *sym)
12197 {
12198 bool t;
12199
12200 if (sym->value == NULL)
12201 return;
12202
12203 if (sym->value->expr_type == EXPR_STRUCTURE)
12204 t= resolve_structure_cons (sym->value, 1);
12205 else
12206 t = gfc_resolve_expr (sym->value);
12207
12208 if (!t)
12209 return;
12210
12211 gfc_check_assign_symbol (sym, NULL, sym->value);
12212 }
12213
12214
12215 /* Verify any BIND(C) derived types in the namespace so we can report errors
12216 for them once, rather than for each variable declared of that type. */
12217
12218 static void
12219 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
12220 {
12221 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
12222 && derived_sym->attr.is_bind_c == 1)
12223 verify_bind_c_derived_type (derived_sym);
12224
12225 return;
12226 }
12227
12228
12229 /* Check the interfaces of DTIO procedures associated with derived
12230 type 'sym'. These procedures can either have typebound bindings or
12231 can appear in DTIO generic interfaces. */
12232
12233 static void
12234 gfc_verify_DTIO_procedures (gfc_symbol *sym)
12235 {
12236 if (!sym || sym->attr.flavor != FL_DERIVED)
12237 return;
12238
12239 gfc_check_dtio_interfaces (sym);
12240
12241 return;
12242 }
12243
12244 /* Verify that any binding labels used in a given namespace do not collide
12245 with the names or binding labels of any global symbols. Multiple INTERFACE
12246 for the same procedure are permitted. */
12247
12248 static void
12249 gfc_verify_binding_labels (gfc_symbol *sym)
12250 {
12251 gfc_gsymbol *gsym;
12252 const char *module;
12253
12254 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
12255 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
12256 return;
12257
12258 gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
12259
12260 if (sym->module)
12261 module = sym->module;
12262 else if (sym->ns && sym->ns->proc_name
12263 && sym->ns->proc_name->attr.flavor == FL_MODULE)
12264 module = sym->ns->proc_name->name;
12265 else if (sym->ns && sym->ns->parent
12266 && sym->ns && sym->ns->parent->proc_name
12267 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
12268 module = sym->ns->parent->proc_name->name;
12269 else
12270 module = NULL;
12271
12272 if (!gsym
12273 || (!gsym->defined
12274 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
12275 {
12276 if (!gsym)
12277 gsym = gfc_get_gsymbol (sym->binding_label, true);
12278 gsym->where = sym->declared_at;
12279 gsym->sym_name = sym->name;
12280 gsym->binding_label = sym->binding_label;
12281 gsym->ns = sym->ns;
12282 gsym->mod_name = module;
12283 if (sym->attr.function)
12284 gsym->type = GSYM_FUNCTION;
12285 else if (sym->attr.subroutine)
12286 gsym->type = GSYM_SUBROUTINE;
12287 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
12288 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
12289 return;
12290 }
12291
12292 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
12293 {
12294 gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
12295 "identifier as entity at %L", sym->name,
12296 sym->binding_label, &sym->declared_at, &gsym->where);
12297 /* Clear the binding label to prevent checking multiple times. */
12298 sym->binding_label = NULL;
12299 return;
12300 }
12301
12302 if (sym->attr.flavor == FL_VARIABLE && module
12303 && (strcmp (module, gsym->mod_name) != 0
12304 || strcmp (sym->name, gsym->sym_name) != 0))
12305 {
12306 /* This can only happen if the variable is defined in a module - if it
12307 isn't the same module, reject it. */
12308 gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
12309 "uses the same global identifier as entity at %L from module %qs",
12310 sym->name, module, sym->binding_label,
12311 &sym->declared_at, &gsym->where, gsym->mod_name);
12312 sym->binding_label = NULL;
12313 return;
12314 }
12315
12316 if ((sym->attr.function || sym->attr.subroutine)
12317 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
12318 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
12319 && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
12320 && (module != gsym->mod_name
12321 || strcmp (gsym->sym_name, sym->name) != 0
12322 || (module && strcmp (module, gsym->mod_name) != 0)))
12323 {
12324 /* Print an error if the procedure is defined multiple times; we have to
12325 exclude references to the same procedure via module association or
12326 multiple checks for the same procedure. */
12327 gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
12328 "global identifier as entity at %L", sym->name,
12329 sym->binding_label, &sym->declared_at, &gsym->where);
12330 sym->binding_label = NULL;
12331 }
12332 }
12333
12334
12335 /* Resolve an index expression. */
12336
12337 static bool
12338 resolve_index_expr (gfc_expr *e)
12339 {
12340 if (!gfc_resolve_expr (e))
12341 return false;
12342
12343 if (!gfc_simplify_expr (e, 0))
12344 return false;
12345
12346 if (!gfc_specification_expr (e))
12347 return false;
12348
12349 return true;
12350 }
12351
12352
12353 /* Resolve a charlen structure. */
12354
12355 static bool
12356 resolve_charlen (gfc_charlen *cl)
12357 {
12358 int k;
12359 bool saved_specification_expr;
12360
12361 if (cl->resolved)
12362 return true;
12363
12364 cl->resolved = 1;
12365 saved_specification_expr = specification_expr;
12366 specification_expr = true;
12367
12368 if (cl->length_from_typespec)
12369 {
12370 if (!gfc_resolve_expr (cl->length))
12371 {
12372 specification_expr = saved_specification_expr;
12373 return false;
12374 }
12375
12376 if (!gfc_simplify_expr (cl->length, 0))
12377 {
12378 specification_expr = saved_specification_expr;
12379 return false;
12380 }
12381
12382 /* cl->length has been resolved. It should have an integer type. */
12383 if (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0)
12384 {
12385 gfc_error ("Scalar INTEGER expression expected at %L",
12386 &cl->length->where);
12387 return false;
12388 }
12389 }
12390 else
12391 {
12392 if (!resolve_index_expr (cl->length))
12393 {
12394 specification_expr = saved_specification_expr;
12395 return false;
12396 }
12397 }
12398
12399 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
12400 a negative value, the length of character entities declared is zero. */
12401 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
12402 && mpz_sgn (cl->length->value.integer) < 0)
12403 gfc_replace_expr (cl->length,
12404 gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
12405
12406 /* Check that the character length is not too large. */
12407 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
12408 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
12409 && cl->length->ts.type == BT_INTEGER
12410 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
12411 {
12412 gfc_error ("String length at %L is too large", &cl->length->where);
12413 specification_expr = saved_specification_expr;
12414 return false;
12415 }
12416
12417 specification_expr = saved_specification_expr;
12418 return true;
12419 }
12420
12421
12422 /* Test for non-constant shape arrays. */
12423
12424 static bool
12425 is_non_constant_shape_array (gfc_symbol *sym)
12426 {
12427 gfc_expr *e;
12428 int i;
12429 bool not_constant;
12430
12431 not_constant = false;
12432 if (sym->as != NULL)
12433 {
12434 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
12435 has not been simplified; parameter array references. Do the
12436 simplification now. */
12437 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
12438 {
12439 if (i == GFC_MAX_DIMENSIONS)
12440 break;
12441
12442 e = sym->as->lower[i];
12443 if (e && (!resolve_index_expr(e)
12444 || !gfc_is_constant_expr (e)))
12445 not_constant = true;
12446 e = sym->as->upper[i];
12447 if (e && (!resolve_index_expr(e)
12448 || !gfc_is_constant_expr (e)))
12449 not_constant = true;
12450 }
12451 }
12452 return not_constant;
12453 }
12454
12455 /* Given a symbol and an initialization expression, add code to initialize
12456 the symbol to the function entry. */
12457 static void
12458 build_init_assign (gfc_symbol *sym, gfc_expr *init)
12459 {
12460 gfc_expr *lval;
12461 gfc_code *init_st;
12462 gfc_namespace *ns = sym->ns;
12463
12464 /* Search for the function namespace if this is a contained
12465 function without an explicit result. */
12466 if (sym->attr.function && sym == sym->result
12467 && sym->name != sym->ns->proc_name->name)
12468 {
12469 ns = ns->contained;
12470 for (;ns; ns = ns->sibling)
12471 if (strcmp (ns->proc_name->name, sym->name) == 0)
12472 break;
12473 }
12474
12475 if (ns == NULL)
12476 {
12477 gfc_free_expr (init);
12478 return;
12479 }
12480
12481 /* Build an l-value expression for the result. */
12482 lval = gfc_lval_expr_from_sym (sym);
12483
12484 /* Add the code at scope entry. */
12485 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
12486 init_st->next = ns->code;
12487 ns->code = init_st;
12488
12489 /* Assign the default initializer to the l-value. */
12490 init_st->loc = sym->declared_at;
12491 init_st->expr1 = lval;
12492 init_st->expr2 = init;
12493 }
12494
12495
12496 /* Whether or not we can generate a default initializer for a symbol. */
12497
12498 static bool
12499 can_generate_init (gfc_symbol *sym)
12500 {
12501 symbol_attribute *a;
12502 if (!sym)
12503 return false;
12504 a = &sym->attr;
12505
12506 /* These symbols should never have a default initialization. */
12507 return !(
12508 a->allocatable
12509 || a->external
12510 || a->pointer
12511 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
12512 && (CLASS_DATA (sym)->attr.class_pointer
12513 || CLASS_DATA (sym)->attr.proc_pointer))
12514 || a->in_equivalence
12515 || a->in_common
12516 || a->data
12517 || sym->module
12518 || a->cray_pointee
12519 || a->cray_pointer
12520 || sym->assoc
12521 || (!a->referenced && !a->result)
12522 || (a->dummy && a->intent != INTENT_OUT)
12523 || (a->function && sym != sym->result)
12524 );
12525 }
12526
12527
12528 /* Assign the default initializer to a derived type variable or result. */
12529
12530 static void
12531 apply_default_init (gfc_symbol *sym)
12532 {
12533 gfc_expr *init = NULL;
12534
12535 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
12536 return;
12537
12538 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
12539 init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
12540
12541 if (init == NULL && sym->ts.type != BT_CLASS)
12542 return;
12543
12544 build_init_assign (sym, init);
12545 sym->attr.referenced = 1;
12546 }
12547
12548
12549 /* Build an initializer for a local. Returns null if the symbol should not have
12550 a default initialization. */
12551
12552 static gfc_expr *
12553 build_default_init_expr (gfc_symbol *sym)
12554 {
12555 /* These symbols should never have a default initialization. */
12556 if (sym->attr.allocatable
12557 || sym->attr.external
12558 || sym->attr.dummy
12559 || sym->attr.pointer
12560 || sym->attr.in_equivalence
12561 || sym->attr.in_common
12562 || sym->attr.data
12563 || sym->module
12564 || sym->attr.cray_pointee
12565 || sym->attr.cray_pointer
12566 || sym->assoc)
12567 return NULL;
12568
12569 /* Get the appropriate init expression. */
12570 return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
12571 }
12572
12573 /* Add an initialization expression to a local variable. */
12574 static void
12575 apply_default_init_local (gfc_symbol *sym)
12576 {
12577 gfc_expr *init = NULL;
12578
12579 /* The symbol should be a variable or a function return value. */
12580 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
12581 || (sym->attr.function && sym->result != sym))
12582 return;
12583
12584 /* Try to build the initializer expression. If we can't initialize
12585 this symbol, then init will be NULL. */
12586 init = build_default_init_expr (sym);
12587 if (init == NULL)
12588 return;
12589
12590 /* For saved variables, we don't want to add an initializer at function
12591 entry, so we just add a static initializer. Note that automatic variables
12592 are stack allocated even with -fno-automatic; we have also to exclude
12593 result variable, which are also nonstatic. */
12594 if (!sym->attr.automatic
12595 && (sym->attr.save || sym->ns->save_all
12596 || (flag_max_stack_var_size == 0 && !sym->attr.result
12597 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
12598 && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
12599 {
12600 /* Don't clobber an existing initializer! */
12601 gcc_assert (sym->value == NULL);
12602 sym->value = init;
12603 return;
12604 }
12605
12606 build_init_assign (sym, init);
12607 }
12608
12609
12610 /* Resolution of common features of flavors variable and procedure. */
12611
12612 static bool
12613 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
12614 {
12615 gfc_array_spec *as;
12616
12617 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
12618 as = CLASS_DATA (sym)->as;
12619 else
12620 as = sym->as;
12621
12622 /* Constraints on deferred shape variable. */
12623 if (as == NULL || as->type != AS_DEFERRED)
12624 {
12625 bool pointer, allocatable, dimension;
12626
12627 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
12628 {
12629 pointer = CLASS_DATA (sym)->attr.class_pointer;
12630 allocatable = CLASS_DATA (sym)->attr.allocatable;
12631 dimension = CLASS_DATA (sym)->attr.dimension;
12632 }
12633 else
12634 {
12635 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
12636 allocatable = sym->attr.allocatable;
12637 dimension = sym->attr.dimension;
12638 }
12639
12640 if (allocatable)
12641 {
12642 if (dimension && as->type != AS_ASSUMED_RANK)
12643 {
12644 gfc_error ("Allocatable array %qs at %L must have a deferred "
12645 "shape or assumed rank", sym->name, &sym->declared_at);
12646 return false;
12647 }
12648 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
12649 "%qs at %L may not be ALLOCATABLE",
12650 sym->name, &sym->declared_at))
12651 return false;
12652 }
12653
12654 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
12655 {
12656 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
12657 "assumed rank", sym->name, &sym->declared_at);
12658 sym->error = 1;
12659 return false;
12660 }
12661 }
12662 else
12663 {
12664 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
12665 && sym->ts.type != BT_CLASS && !sym->assoc)
12666 {
12667 gfc_error ("Array %qs at %L cannot have a deferred shape",
12668 sym->name, &sym->declared_at);
12669 return false;
12670 }
12671 }
12672
12673 /* Constraints on polymorphic variables. */
12674 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
12675 {
12676 /* F03:C502. */
12677 if (sym->attr.class_ok
12678 && !sym->attr.select_type_temporary
12679 && !UNLIMITED_POLY (sym)
12680 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
12681 {
12682 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
12683 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
12684 &sym->declared_at);
12685 return false;
12686 }
12687
12688 /* F03:C509. */
12689 /* Assume that use associated symbols were checked in the module ns.
12690 Class-variables that are associate-names are also something special
12691 and excepted from the test. */
12692 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
12693 {
12694 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
12695 "or pointer", sym->name, &sym->declared_at);
12696 return false;
12697 }
12698 }
12699
12700 return true;
12701 }
12702
12703
12704 /* Additional checks for symbols with flavor variable and derived
12705 type. To be called from resolve_fl_variable. */
12706
12707 static bool
12708 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
12709 {
12710 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
12711
12712 /* Check to see if a derived type is blocked from being host
12713 associated by the presence of another class I symbol in the same
12714 namespace. 14.6.1.3 of the standard and the discussion on
12715 comp.lang.fortran. */
12716 if (sym->ns != sym->ts.u.derived->ns
12717 && !sym->ts.u.derived->attr.use_assoc
12718 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
12719 {
12720 gfc_symbol *s;
12721 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
12722 if (s && s->attr.generic)
12723 s = gfc_find_dt_in_generic (s);
12724 if (s && !gfc_fl_struct (s->attr.flavor))
12725 {
12726 gfc_error ("The type %qs cannot be host associated at %L "
12727 "because it is blocked by an incompatible object "
12728 "of the same name declared at %L",
12729 sym->ts.u.derived->name, &sym->declared_at,
12730 &s->declared_at);
12731 return false;
12732 }
12733 }
12734
12735 /* 4th constraint in section 11.3: "If an object of a type for which
12736 component-initialization is specified (R429) appears in the
12737 specification-part of a module and does not have the ALLOCATABLE
12738 or POINTER attribute, the object shall have the SAVE attribute."
12739
12740 The check for initializers is performed with
12741 gfc_has_default_initializer because gfc_default_initializer generates
12742 a hidden default for allocatable components. */
12743 if (!(sym->value || no_init_flag) && sym->ns->proc_name
12744 && sym->ns->proc_name->attr.flavor == FL_MODULE
12745 && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
12746 && !sym->attr.pointer && !sym->attr.allocatable
12747 && gfc_has_default_initializer (sym->ts.u.derived)
12748 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
12749 "%qs at %L, needed due to the default "
12750 "initialization", sym->name, &sym->declared_at))
12751 return false;
12752
12753 /* Assign default initializer. */
12754 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
12755 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
12756 sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
12757
12758 return true;
12759 }
12760
12761
12762 /* F2008, C402 (R401): A colon shall not be used as a type-param-value
12763 except in the declaration of an entity or component that has the POINTER
12764 or ALLOCATABLE attribute. */
12765
12766 static bool
12767 deferred_requirements (gfc_symbol *sym)
12768 {
12769 if (sym->ts.deferred
12770 && !(sym->attr.pointer
12771 || sym->attr.allocatable
12772 || sym->attr.associate_var
12773 || sym->attr.omp_udr_artificial_var))
12774 {
12775 /* If a function has a result variable, only check the variable. */
12776 if (sym->result && sym->name != sym->result->name)
12777 return true;
12778
12779 gfc_error ("Entity %qs at %L has a deferred type parameter and "
12780 "requires either the POINTER or ALLOCATABLE attribute",
12781 sym->name, &sym->declared_at);
12782 return false;
12783 }
12784 return true;
12785 }
12786
12787
12788 /* Resolve symbols with flavor variable. */
12789
12790 static bool
12791 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
12792 {
12793 const char *auto_save_msg = "Automatic object %qs at %L cannot have the "
12794 "SAVE attribute";
12795
12796 if (!resolve_fl_var_and_proc (sym, mp_flag))
12797 return false;
12798
12799 /* Set this flag to check that variables are parameters of all entries.
12800 This check is effected by the call to gfc_resolve_expr through
12801 is_non_constant_shape_array. */
12802 bool saved_specification_expr = specification_expr;
12803 specification_expr = true;
12804
12805 if (sym->ns->proc_name
12806 && (sym->ns->proc_name->attr.flavor == FL_MODULE
12807 || sym->ns->proc_name->attr.is_main_program)
12808 && !sym->attr.use_assoc
12809 && !sym->attr.allocatable
12810 && !sym->attr.pointer
12811 && is_non_constant_shape_array (sym))
12812 {
12813 /* F08:C541. The shape of an array defined in a main program or module
12814 * needs to be constant. */
12815 gfc_error ("The module or main program array %qs at %L must "
12816 "have constant shape", sym->name, &sym->declared_at);
12817 specification_expr = saved_specification_expr;
12818 return false;
12819 }
12820
12821 /* Constraints on deferred type parameter. */
12822 if (!deferred_requirements (sym))
12823 return false;
12824
12825 if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
12826 {
12827 /* Make sure that character string variables with assumed length are
12828 dummy arguments. */
12829 gfc_expr *e = NULL;
12830
12831 if (sym->ts.u.cl)
12832 e = sym->ts.u.cl->length;
12833 else
12834 return false;
12835
12836 if (e == NULL && !sym->attr.dummy && !sym->attr.result
12837 && !sym->ts.deferred && !sym->attr.select_type_temporary
12838 && !sym->attr.omp_udr_artificial_var)
12839 {
12840 gfc_error ("Entity with assumed character length at %L must be a "
12841 "dummy argument or a PARAMETER", &sym->declared_at);
12842 specification_expr = saved_specification_expr;
12843 return false;
12844 }
12845
12846 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
12847 {
12848 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
12849 specification_expr = saved_specification_expr;
12850 return false;
12851 }
12852
12853 if (!gfc_is_constant_expr (e)
12854 && !(e->expr_type == EXPR_VARIABLE
12855 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
12856 {
12857 if (!sym->attr.use_assoc && sym->ns->proc_name
12858 && (sym->ns->proc_name->attr.flavor == FL_MODULE
12859 || sym->ns->proc_name->attr.is_main_program))
12860 {
12861 gfc_error ("%qs at %L must have constant character length "
12862 "in this context", sym->name, &sym->declared_at);
12863 specification_expr = saved_specification_expr;
12864 return false;
12865 }
12866 if (sym->attr.in_common)
12867 {
12868 gfc_error ("COMMON variable %qs at %L must have constant "
12869 "character length", sym->name, &sym->declared_at);
12870 specification_expr = saved_specification_expr;
12871 return false;
12872 }
12873 }
12874 }
12875
12876 if (sym->value == NULL && sym->attr.referenced)
12877 apply_default_init_local (sym); /* Try to apply a default initialization. */
12878
12879 /* Determine if the symbol may not have an initializer. */
12880 int no_init_flag = 0, automatic_flag = 0;
12881 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
12882 || sym->attr.intrinsic || sym->attr.result)
12883 no_init_flag = 1;
12884 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
12885 && is_non_constant_shape_array (sym))
12886 {
12887 no_init_flag = automatic_flag = 1;
12888
12889 /* Also, they must not have the SAVE attribute.
12890 SAVE_IMPLICIT is checked below. */
12891 if (sym->as && sym->attr.codimension)
12892 {
12893 int corank = sym->as->corank;
12894 sym->as->corank = 0;
12895 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
12896 sym->as->corank = corank;
12897 }
12898 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
12899 {
12900 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
12901 specification_expr = saved_specification_expr;
12902 return false;
12903 }
12904 }
12905
12906 /* Ensure that any initializer is simplified. */
12907 if (sym->value)
12908 gfc_simplify_expr (sym->value, 1);
12909
12910 /* Reject illegal initializers. */
12911 if (!sym->mark && sym->value)
12912 {
12913 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
12914 && CLASS_DATA (sym)->attr.allocatable))
12915 gfc_error ("Allocatable %qs at %L cannot have an initializer",
12916 sym->name, &sym->declared_at);
12917 else if (sym->attr.external)
12918 gfc_error ("External %qs at %L cannot have an initializer",
12919 sym->name, &sym->declared_at);
12920 else if (sym->attr.dummy
12921 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
12922 gfc_error ("Dummy %qs at %L cannot have an initializer",
12923 sym->name, &sym->declared_at);
12924 else if (sym->attr.intrinsic)
12925 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
12926 sym->name, &sym->declared_at);
12927 else if (sym->attr.result)
12928 gfc_error ("Function result %qs at %L cannot have an initializer",
12929 sym->name, &sym->declared_at);
12930 else if (automatic_flag)
12931 gfc_error ("Automatic array %qs at %L cannot have an initializer",
12932 sym->name, &sym->declared_at);
12933 else
12934 goto no_init_error;
12935 specification_expr = saved_specification_expr;
12936 return false;
12937 }
12938
12939 no_init_error:
12940 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
12941 {
12942 bool res = resolve_fl_variable_derived (sym, no_init_flag);
12943 specification_expr = saved_specification_expr;
12944 return res;
12945 }
12946
12947 specification_expr = saved_specification_expr;
12948 return true;
12949 }
12950
12951
12952 /* Compare the dummy characteristics of a module procedure interface
12953 declaration with the corresponding declaration in a submodule. */
12954 static gfc_formal_arglist *new_formal;
12955 static char errmsg[200];
12956
12957 static void
12958 compare_fsyms (gfc_symbol *sym)
12959 {
12960 gfc_symbol *fsym;
12961
12962 if (sym == NULL || new_formal == NULL)
12963 return;
12964
12965 fsym = new_formal->sym;
12966
12967 if (sym == fsym)
12968 return;
12969
12970 if (strcmp (sym->name, fsym->name) == 0)
12971 {
12972 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
12973 gfc_error ("%s at %L", errmsg, &fsym->declared_at);
12974 }
12975 }
12976
12977
12978 /* Resolve a procedure. */
12979
12980 static bool
12981 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
12982 {
12983 gfc_formal_arglist *arg;
12984
12985 if (sym->attr.function
12986 && !resolve_fl_var_and_proc (sym, mp_flag))
12987 return false;
12988
12989 /* Constraints on deferred type parameter. */
12990 if (!deferred_requirements (sym))
12991 return false;
12992
12993 if (sym->ts.type == BT_CHARACTER)
12994 {
12995 gfc_charlen *cl = sym->ts.u.cl;
12996
12997 if (cl && cl->length && gfc_is_constant_expr (cl->length)
12998 && !resolve_charlen (cl))
12999 return false;
13000
13001 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
13002 && sym->attr.proc == PROC_ST_FUNCTION)
13003 {
13004 gfc_error ("Character-valued statement function %qs at %L must "
13005 "have constant length", sym->name, &sym->declared_at);
13006 return false;
13007 }
13008 }
13009
13010 /* Ensure that derived type for are not of a private type. Internal
13011 module procedures are excluded by 2.2.3.3 - i.e., they are not
13012 externally accessible and can access all the objects accessible in
13013 the host. */
13014 if (!(sym->ns->parent && sym->ns->parent->proc_name
13015 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
13016 && gfc_check_symbol_access (sym))
13017 {
13018 gfc_interface *iface;
13019
13020 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
13021 {
13022 if (arg->sym
13023 && arg->sym->ts.type == BT_DERIVED
13024 && arg->sym->ts.u.derived
13025 && !arg->sym->ts.u.derived->attr.use_assoc
13026 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
13027 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
13028 "and cannot be a dummy argument"
13029 " of %qs, which is PUBLIC at %L",
13030 arg->sym->name, sym->name,
13031 &sym->declared_at))
13032 {
13033 /* Stop this message from recurring. */
13034 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
13035 return false;
13036 }
13037 }
13038
13039 /* PUBLIC interfaces may expose PRIVATE procedures that take types
13040 PRIVATE to the containing module. */
13041 for (iface = sym->generic; iface; iface = iface->next)
13042 {
13043 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
13044 {
13045 if (arg->sym
13046 && arg->sym->ts.type == BT_DERIVED
13047 && !arg->sym->ts.u.derived->attr.use_assoc
13048 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
13049 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
13050 "PUBLIC interface %qs at %L "
13051 "takes dummy arguments of %qs which "
13052 "is PRIVATE", iface->sym->name,
13053 sym->name, &iface->sym->declared_at,
13054 gfc_typename(&arg->sym->ts)))
13055 {
13056 /* Stop this message from recurring. */
13057 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
13058 return false;
13059 }
13060 }
13061 }
13062 }
13063
13064 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
13065 && !sym->attr.proc_pointer)
13066 {
13067 gfc_error ("Function %qs at %L cannot have an initializer",
13068 sym->name, &sym->declared_at);
13069
13070 /* Make sure no second error is issued for this. */
13071 sym->value->error = 1;
13072 return false;
13073 }
13074
13075 /* An external symbol may not have an initializer because it is taken to be
13076 a procedure. Exception: Procedure Pointers. */
13077 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
13078 {
13079 gfc_error ("External object %qs at %L may not have an initializer",
13080 sym->name, &sym->declared_at);
13081 return false;
13082 }
13083
13084 /* An elemental function is required to return a scalar 12.7.1 */
13085 if (sym->attr.elemental && sym->attr.function
13086 && (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)))
13087 {
13088 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
13089 "result", sym->name, &sym->declared_at);
13090 /* Reset so that the error only occurs once. */
13091 sym->attr.elemental = 0;
13092 return false;
13093 }
13094
13095 if (sym->attr.proc == PROC_ST_FUNCTION
13096 && (sym->attr.allocatable || sym->attr.pointer))
13097 {
13098 gfc_error ("Statement function %qs at %L may not have pointer or "
13099 "allocatable attribute", sym->name, &sym->declared_at);
13100 return false;
13101 }
13102
13103 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
13104 char-len-param shall not be array-valued, pointer-valued, recursive
13105 or pure. ....snip... A character value of * may only be used in the
13106 following ways: (i) Dummy arg of procedure - dummy associates with
13107 actual length; (ii) To declare a named constant; or (iii) External
13108 function - but length must be declared in calling scoping unit. */
13109 if (sym->attr.function
13110 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
13111 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
13112 {
13113 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
13114 || (sym->attr.recursive) || (sym->attr.pure))
13115 {
13116 if (sym->as && sym->as->rank)
13117 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13118 "array-valued", sym->name, &sym->declared_at);
13119
13120 if (sym->attr.pointer)
13121 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13122 "pointer-valued", sym->name, &sym->declared_at);
13123
13124 if (sym->attr.pure)
13125 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13126 "pure", sym->name, &sym->declared_at);
13127
13128 if (sym->attr.recursive)
13129 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
13130 "recursive", sym->name, &sym->declared_at);
13131
13132 return false;
13133 }
13134
13135 /* Appendix B.2 of the standard. Contained functions give an
13136 error anyway. Deferred character length is an F2003 feature.
13137 Don't warn on intrinsic conversion functions, which start
13138 with two underscores. */
13139 if (!sym->attr.contained && !sym->ts.deferred
13140 && (sym->name[0] != '_' || sym->name[1] != '_'))
13141 gfc_notify_std (GFC_STD_F95_OBS,
13142 "CHARACTER(*) function %qs at %L",
13143 sym->name, &sym->declared_at);
13144 }
13145
13146 /* F2008, C1218. */
13147 if (sym->attr.elemental)
13148 {
13149 if (sym->attr.proc_pointer)
13150 {
13151 const char* name = (sym->attr.result ? sym->ns->proc_name->name
13152 : sym->name);
13153 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
13154 name, &sym->declared_at);
13155 return false;
13156 }
13157 if (sym->attr.dummy)
13158 {
13159 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
13160 sym->name, &sym->declared_at);
13161 return false;
13162 }
13163 }
13164
13165 /* F2018, C15100: "The result of an elemental function shall be scalar,
13166 and shall not have the POINTER or ALLOCATABLE attribute." The scalar
13167 pointer is tested and caught elsewhere. */
13168 if (sym->attr.elemental && sym->result
13169 && (sym->result->attr.allocatable || sym->result->attr.pointer))
13170 {
13171 gfc_error ("Function result variable %qs at %L of elemental "
13172 "function %qs shall not have an ALLOCATABLE or POINTER "
13173 "attribute", sym->result->name,
13174 &sym->result->declared_at, sym->name);
13175 return false;
13176 }
13177
13178 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
13179 {
13180 gfc_formal_arglist *curr_arg;
13181 int has_non_interop_arg = 0;
13182
13183 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13184 sym->common_block))
13185 {
13186 /* Clear these to prevent looking at them again if there was an
13187 error. */
13188 sym->attr.is_bind_c = 0;
13189 sym->attr.is_c_interop = 0;
13190 sym->ts.is_c_interop = 0;
13191 }
13192 else
13193 {
13194 /* So far, no errors have been found. */
13195 sym->attr.is_c_interop = 1;
13196 sym->ts.is_c_interop = 1;
13197 }
13198
13199 curr_arg = gfc_sym_get_dummy_args (sym);
13200 while (curr_arg != NULL)
13201 {
13202 /* Skip implicitly typed dummy args here. */
13203 if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
13204 if (!gfc_verify_c_interop_param (curr_arg->sym))
13205 /* If something is found to fail, record the fact so we
13206 can mark the symbol for the procedure as not being
13207 BIND(C) to try and prevent multiple errors being
13208 reported. */
13209 has_non_interop_arg = 1;
13210
13211 curr_arg = curr_arg->next;
13212 }
13213
13214 /* See if any of the arguments were not interoperable and if so, clear
13215 the procedure symbol to prevent duplicate error messages. */
13216 if (has_non_interop_arg != 0)
13217 {
13218 sym->attr.is_c_interop = 0;
13219 sym->ts.is_c_interop = 0;
13220 sym->attr.is_bind_c = 0;
13221 }
13222 }
13223
13224 if (!sym->attr.proc_pointer)
13225 {
13226 if (sym->attr.save == SAVE_EXPLICIT)
13227 {
13228 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
13229 "in %qs at %L", sym->name, &sym->declared_at);
13230 return false;
13231 }
13232 if (sym->attr.intent)
13233 {
13234 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
13235 "in %qs at %L", sym->name, &sym->declared_at);
13236 return false;
13237 }
13238 if (sym->attr.subroutine && sym->attr.result)
13239 {
13240 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
13241 "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
13242 return false;
13243 }
13244 if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
13245 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
13246 || sym->attr.contained))
13247 {
13248 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
13249 "in %qs at %L", sym->name, &sym->declared_at);
13250 return false;
13251 }
13252 if (strcmp ("ppr@", sym->name) == 0)
13253 {
13254 gfc_error ("Procedure pointer result %qs at %L "
13255 "is missing the pointer attribute",
13256 sym->ns->proc_name->name, &sym->declared_at);
13257 return false;
13258 }
13259 }
13260
13261 /* Assume that a procedure whose body is not known has references
13262 to external arrays. */
13263 if (sym->attr.if_source != IFSRC_DECL)
13264 sym->attr.array_outer_dependency = 1;
13265
13266 /* Compare the characteristics of a module procedure with the
13267 interface declaration. Ideally this would be done with
13268 gfc_compare_interfaces but, at present, the formal interface
13269 cannot be copied to the ts.interface. */
13270 if (sym->attr.module_procedure
13271 && sym->attr.if_source == IFSRC_DECL)
13272 {
13273 gfc_symbol *iface;
13274 char name[2*GFC_MAX_SYMBOL_LEN + 1];
13275 char *module_name;
13276 char *submodule_name;
13277 strcpy (name, sym->ns->proc_name->name);
13278 module_name = strtok (name, ".");
13279 submodule_name = strtok (NULL, ".");
13280
13281 iface = sym->tlink;
13282 sym->tlink = NULL;
13283
13284 /* Make sure that the result uses the correct charlen for deferred
13285 length results. */
13286 if (iface && sym->result
13287 && iface->ts.type == BT_CHARACTER
13288 && iface->ts.deferred)
13289 sym->result->ts.u.cl = iface->ts.u.cl;
13290
13291 if (iface == NULL)
13292 goto check_formal;
13293
13294 /* Check the procedure characteristics. */
13295 if (sym->attr.elemental != iface->attr.elemental)
13296 {
13297 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
13298 "PROCEDURE at %L and its interface in %s",
13299 &sym->declared_at, module_name);
13300 return false;
13301 }
13302
13303 if (sym->attr.pure != iface->attr.pure)
13304 {
13305 gfc_error ("Mismatch in PURE attribute between MODULE "
13306 "PROCEDURE at %L and its interface in %s",
13307 &sym->declared_at, module_name);
13308 return false;
13309 }
13310
13311 if (sym->attr.recursive != iface->attr.recursive)
13312 {
13313 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
13314 "PROCEDURE at %L and its interface in %s",
13315 &sym->declared_at, module_name);
13316 return false;
13317 }
13318
13319 /* Check the result characteristics. */
13320 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
13321 {
13322 gfc_error ("%s between the MODULE PROCEDURE declaration "
13323 "in MODULE %qs and the declaration at %L in "
13324 "(SUB)MODULE %qs",
13325 errmsg, module_name, &sym->declared_at,
13326 submodule_name ? submodule_name : module_name);
13327 return false;
13328 }
13329
13330 check_formal:
13331 /* Check the characteristics of the formal arguments. */
13332 if (sym->formal && sym->formal_ns)
13333 {
13334 for (arg = sym->formal; arg && arg->sym; arg = arg->next)
13335 {
13336 new_formal = arg;
13337 gfc_traverse_ns (sym->formal_ns, compare_fsyms);
13338 }
13339 }
13340 }
13341 return true;
13342 }
13343
13344
13345 /* Resolve a list of finalizer procedures. That is, after they have hopefully
13346 been defined and we now know their defined arguments, check that they fulfill
13347 the requirements of the standard for procedures used as finalizers. */
13348
13349 static bool
13350 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
13351 {
13352 gfc_finalizer* list;
13353 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
13354 bool result = true;
13355 bool seen_scalar = false;
13356 gfc_symbol *vtab;
13357 gfc_component *c;
13358 gfc_symbol *parent = gfc_get_derived_super_type (derived);
13359
13360 if (parent)
13361 gfc_resolve_finalizers (parent, finalizable);
13362
13363 /* Ensure that derived-type components have a their finalizers resolved. */
13364 bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
13365 for (c = derived->components; c; c = c->next)
13366 if (c->ts.type == BT_DERIVED
13367 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
13368 {
13369 bool has_final2 = false;
13370 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
13371 return false; /* Error. */
13372 has_final = has_final || has_final2;
13373 }
13374 /* Return early if not finalizable. */
13375 if (!has_final)
13376 {
13377 if (finalizable)
13378 *finalizable = false;
13379 return true;
13380 }
13381
13382 /* Walk over the list of finalizer-procedures, check them, and if any one
13383 does not fit in with the standard's definition, print an error and remove
13384 it from the list. */
13385 prev_link = &derived->f2k_derived->finalizers;
13386 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
13387 {
13388 gfc_formal_arglist *dummy_args;
13389 gfc_symbol* arg;
13390 gfc_finalizer* i;
13391 int my_rank;
13392
13393 /* Skip this finalizer if we already resolved it. */
13394 if (list->proc_tree)
13395 {
13396 if (list->proc_tree->n.sym->formal->sym->as == NULL
13397 || list->proc_tree->n.sym->formal->sym->as->rank == 0)
13398 seen_scalar = true;
13399 prev_link = &(list->next);
13400 continue;
13401 }
13402
13403 /* Check this exists and is a SUBROUTINE. */
13404 if (!list->proc_sym->attr.subroutine)
13405 {
13406 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
13407 list->proc_sym->name, &list->where);
13408 goto error;
13409 }
13410
13411 /* We should have exactly one argument. */
13412 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
13413 if (!dummy_args || dummy_args->next)
13414 {
13415 gfc_error ("FINAL procedure at %L must have exactly one argument",
13416 &list->where);
13417 goto error;
13418 }
13419 arg = dummy_args->sym;
13420
13421 /* This argument must be of our type. */
13422 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
13423 {
13424 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
13425 &arg->declared_at, derived->name);
13426 goto error;
13427 }
13428
13429 /* It must neither be a pointer nor allocatable nor optional. */
13430 if (arg->attr.pointer)
13431 {
13432 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
13433 &arg->declared_at);
13434 goto error;
13435 }
13436 if (arg->attr.allocatable)
13437 {
13438 gfc_error ("Argument of FINAL procedure at %L must not be"
13439 " ALLOCATABLE", &arg->declared_at);
13440 goto error;
13441 }
13442 if (arg->attr.optional)
13443 {
13444 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
13445 &arg->declared_at);
13446 goto error;
13447 }
13448
13449 /* It must not be INTENT(OUT). */
13450 if (arg->attr.intent == INTENT_OUT)
13451 {
13452 gfc_error ("Argument of FINAL procedure at %L must not be"
13453 " INTENT(OUT)", &arg->declared_at);
13454 goto error;
13455 }
13456
13457 /* Warn if the procedure is non-scalar and not assumed shape. */
13458 if (warn_surprising && arg->as && arg->as->rank != 0
13459 && arg->as->type != AS_ASSUMED_SHAPE)
13460 gfc_warning (OPT_Wsurprising,
13461 "Non-scalar FINAL procedure at %L should have assumed"
13462 " shape argument", &arg->declared_at);
13463
13464 /* Check that it does not match in kind and rank with a FINAL procedure
13465 defined earlier. To really loop over the *earlier* declarations,
13466 we need to walk the tail of the list as new ones were pushed at the
13467 front. */
13468 /* TODO: Handle kind parameters once they are implemented. */
13469 my_rank = (arg->as ? arg->as->rank : 0);
13470 for (i = list->next; i; i = i->next)
13471 {
13472 gfc_formal_arglist *dummy_args;
13473
13474 /* Argument list might be empty; that is an error signalled earlier,
13475 but we nevertheless continued resolving. */
13476 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
13477 if (dummy_args)
13478 {
13479 gfc_symbol* i_arg = dummy_args->sym;
13480 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
13481 if (i_rank == my_rank)
13482 {
13483 gfc_error ("FINAL procedure %qs declared at %L has the same"
13484 " rank (%d) as %qs",
13485 list->proc_sym->name, &list->where, my_rank,
13486 i->proc_sym->name);
13487 goto error;
13488 }
13489 }
13490 }
13491
13492 /* Is this the/a scalar finalizer procedure? */
13493 if (my_rank == 0)
13494 seen_scalar = true;
13495
13496 /* Find the symtree for this procedure. */
13497 gcc_assert (!list->proc_tree);
13498 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
13499
13500 prev_link = &list->next;
13501 continue;
13502
13503 /* Remove wrong nodes immediately from the list so we don't risk any
13504 troubles in the future when they might fail later expectations. */
13505 error:
13506 i = list;
13507 *prev_link = list->next;
13508 gfc_free_finalizer (i);
13509 result = false;
13510 }
13511
13512 if (result == false)
13513 return false;
13514
13515 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
13516 were nodes in the list, must have been for arrays. It is surely a good
13517 idea to have a scalar version there if there's something to finalize. */
13518 if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
13519 gfc_warning (OPT_Wsurprising,
13520 "Only array FINAL procedures declared for derived type %qs"
13521 " defined at %L, suggest also scalar one",
13522 derived->name, &derived->declared_at);
13523
13524 vtab = gfc_find_derived_vtab (derived);
13525 c = vtab->ts.u.derived->components->next->next->next->next->next;
13526 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
13527
13528 if (finalizable)
13529 *finalizable = true;
13530
13531 return true;
13532 }
13533
13534
13535 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
13536
13537 static bool
13538 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
13539 const char* generic_name, locus where)
13540 {
13541 gfc_symbol *sym1, *sym2;
13542 const char *pass1, *pass2;
13543 gfc_formal_arglist *dummy_args;
13544
13545 gcc_assert (t1->specific && t2->specific);
13546 gcc_assert (!t1->specific->is_generic);
13547 gcc_assert (!t2->specific->is_generic);
13548 gcc_assert (t1->is_operator == t2->is_operator);
13549
13550 sym1 = t1->specific->u.specific->n.sym;
13551 sym2 = t2->specific->u.specific->n.sym;
13552
13553 if (sym1 == sym2)
13554 return true;
13555
13556 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
13557 if (sym1->attr.subroutine != sym2->attr.subroutine
13558 || sym1->attr.function != sym2->attr.function)
13559 {
13560 gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
13561 " GENERIC %qs at %L",
13562 sym1->name, sym2->name, generic_name, &where);
13563 return false;
13564 }
13565
13566 /* Determine PASS arguments. */
13567 if (t1->specific->nopass)
13568 pass1 = NULL;
13569 else if (t1->specific->pass_arg)
13570 pass1 = t1->specific->pass_arg;
13571 else
13572 {
13573 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
13574 if (dummy_args)
13575 pass1 = dummy_args->sym->name;
13576 else
13577 pass1 = NULL;
13578 }
13579 if (t2->specific->nopass)
13580 pass2 = NULL;
13581 else if (t2->specific->pass_arg)
13582 pass2 = t2->specific->pass_arg;
13583 else
13584 {
13585 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
13586 if (dummy_args)
13587 pass2 = dummy_args->sym->name;
13588 else
13589 pass2 = NULL;
13590 }
13591
13592 /* Compare the interfaces. */
13593 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
13594 NULL, 0, pass1, pass2))
13595 {
13596 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
13597 sym1->name, sym2->name, generic_name, &where);
13598 return false;
13599 }
13600
13601 return true;
13602 }
13603
13604
13605 /* Worker function for resolving a generic procedure binding; this is used to
13606 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
13607
13608 The difference between those cases is finding possible inherited bindings
13609 that are overridden, as one has to look for them in tb_sym_root,
13610 tb_uop_root or tb_op, respectively. Thus the caller must already find
13611 the super-type and set p->overridden correctly. */
13612
13613 static bool
13614 resolve_tb_generic_targets (gfc_symbol* super_type,
13615 gfc_typebound_proc* p, const char* name)
13616 {
13617 gfc_tbp_generic* target;
13618 gfc_symtree* first_target;
13619 gfc_symtree* inherited;
13620
13621 gcc_assert (p && p->is_generic);
13622
13623 /* Try to find the specific bindings for the symtrees in our target-list. */
13624 gcc_assert (p->u.generic);
13625 for (target = p->u.generic; target; target = target->next)
13626 if (!target->specific)
13627 {
13628 gfc_typebound_proc* overridden_tbp;
13629 gfc_tbp_generic* g;
13630 const char* target_name;
13631
13632 target_name = target->specific_st->name;
13633
13634 /* Defined for this type directly. */
13635 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
13636 {
13637 target->specific = target->specific_st->n.tb;
13638 goto specific_found;
13639 }
13640
13641 /* Look for an inherited specific binding. */
13642 if (super_type)
13643 {
13644 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
13645 true, NULL);
13646
13647 if (inherited)
13648 {
13649 gcc_assert (inherited->n.tb);
13650 target->specific = inherited->n.tb;
13651 goto specific_found;
13652 }
13653 }
13654
13655 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
13656 " at %L", target_name, name, &p->where);
13657 return false;
13658
13659 /* Once we've found the specific binding, check it is not ambiguous with
13660 other specifics already found or inherited for the same GENERIC. */
13661 specific_found:
13662 gcc_assert (target->specific);
13663
13664 /* This must really be a specific binding! */
13665 if (target->specific->is_generic)
13666 {
13667 gfc_error ("GENERIC %qs at %L must target a specific binding,"
13668 " %qs is GENERIC, too", name, &p->where, target_name);
13669 return false;
13670 }
13671
13672 /* Check those already resolved on this type directly. */
13673 for (g = p->u.generic; g; g = g->next)
13674 if (g != target && g->specific
13675 && !check_generic_tbp_ambiguity (target, g, name, p->where))
13676 return false;
13677
13678 /* Check for ambiguity with inherited specific targets. */
13679 for (overridden_tbp = p->overridden; overridden_tbp;
13680 overridden_tbp = overridden_tbp->overridden)
13681 if (overridden_tbp->is_generic)
13682 {
13683 for (g = overridden_tbp->u.generic; g; g = g->next)
13684 {
13685 gcc_assert (g->specific);
13686 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
13687 return false;
13688 }
13689 }
13690 }
13691
13692 /* If we attempt to "overwrite" a specific binding, this is an error. */
13693 if (p->overridden && !p->overridden->is_generic)
13694 {
13695 gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
13696 " the same name", name, &p->where);
13697 return false;
13698 }
13699
13700 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
13701 all must have the same attributes here. */
13702 first_target = p->u.generic->specific->u.specific;
13703 gcc_assert (first_target);
13704 p->subroutine = first_target->n.sym->attr.subroutine;
13705 p->function = first_target->n.sym->attr.function;
13706
13707 return true;
13708 }
13709
13710
13711 /* Resolve a GENERIC procedure binding for a derived type. */
13712
13713 static bool
13714 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
13715 {
13716 gfc_symbol* super_type;
13717
13718 /* Find the overridden binding if any. */
13719 st->n.tb->overridden = NULL;
13720 super_type = gfc_get_derived_super_type (derived);
13721 if (super_type)
13722 {
13723 gfc_symtree* overridden;
13724 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
13725 true, NULL);
13726
13727 if (overridden && overridden->n.tb)
13728 st->n.tb->overridden = overridden->n.tb;
13729 }
13730
13731 /* Resolve using worker function. */
13732 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
13733 }
13734
13735
13736 /* Retrieve the target-procedure of an operator binding and do some checks in
13737 common for intrinsic and user-defined type-bound operators. */
13738
13739 static gfc_symbol*
13740 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
13741 {
13742 gfc_symbol* target_proc;
13743
13744 gcc_assert (target->specific && !target->specific->is_generic);
13745 target_proc = target->specific->u.specific->n.sym;
13746 gcc_assert (target_proc);
13747
13748 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
13749 if (target->specific->nopass)
13750 {
13751 gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
13752 return NULL;
13753 }
13754
13755 return target_proc;
13756 }
13757
13758
13759 /* Resolve a type-bound intrinsic operator. */
13760
13761 static bool
13762 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
13763 gfc_typebound_proc* p)
13764 {
13765 gfc_symbol* super_type;
13766 gfc_tbp_generic* target;
13767
13768 /* If there's already an error here, do nothing (but don't fail again). */
13769 if (p->error)
13770 return true;
13771
13772 /* Operators should always be GENERIC bindings. */
13773 gcc_assert (p->is_generic);
13774
13775 /* Look for an overridden binding. */
13776 super_type = gfc_get_derived_super_type (derived);
13777 if (super_type && super_type->f2k_derived)
13778 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
13779 op, true, NULL);
13780 else
13781 p->overridden = NULL;
13782
13783 /* Resolve general GENERIC properties using worker function. */
13784 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
13785 goto error;
13786
13787 /* Check the targets to be procedures of correct interface. */
13788 for (target = p->u.generic; target; target = target->next)
13789 {
13790 gfc_symbol* target_proc;
13791
13792 target_proc = get_checked_tb_operator_target (target, p->where);
13793 if (!target_proc)
13794 goto error;
13795
13796 if (!gfc_check_operator_interface (target_proc, op, p->where))
13797 goto error;
13798
13799 /* Add target to non-typebound operator list. */
13800 if (!target->specific->deferred && !derived->attr.use_assoc
13801 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
13802 {
13803 gfc_interface *head, *intr;
13804
13805 /* Preempt 'gfc_check_new_interface' for submodules, where the
13806 mechanism for handling module procedures winds up resolving
13807 operator interfaces twice and would otherwise cause an error. */
13808 for (intr = derived->ns->op[op]; intr; intr = intr->next)
13809 if (intr->sym == target_proc
13810 && target_proc->attr.used_in_submodule)
13811 return true;
13812
13813 if (!gfc_check_new_interface (derived->ns->op[op],
13814 target_proc, p->where))
13815 return false;
13816 head = derived->ns->op[op];
13817 intr = gfc_get_interface ();
13818 intr->sym = target_proc;
13819 intr->where = p->where;
13820 intr->next = head;
13821 derived->ns->op[op] = intr;
13822 }
13823 }
13824
13825 return true;
13826
13827 error:
13828 p->error = 1;
13829 return false;
13830 }
13831
13832
13833 /* Resolve a type-bound user operator (tree-walker callback). */
13834
13835 static gfc_symbol* resolve_bindings_derived;
13836 static bool resolve_bindings_result;
13837
13838 static bool check_uop_procedure (gfc_symbol* sym, locus where);
13839
13840 static void
13841 resolve_typebound_user_op (gfc_symtree* stree)
13842 {
13843 gfc_symbol* super_type;
13844 gfc_tbp_generic* target;
13845
13846 gcc_assert (stree && stree->n.tb);
13847
13848 if (stree->n.tb->error)
13849 return;
13850
13851 /* Operators should always be GENERIC bindings. */
13852 gcc_assert (stree->n.tb->is_generic);
13853
13854 /* Find overridden procedure, if any. */
13855 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
13856 if (super_type && super_type->f2k_derived)
13857 {
13858 gfc_symtree* overridden;
13859 overridden = gfc_find_typebound_user_op (super_type, NULL,
13860 stree->name, true, NULL);
13861
13862 if (overridden && overridden->n.tb)
13863 stree->n.tb->overridden = overridden->n.tb;
13864 }
13865 else
13866 stree->n.tb->overridden = NULL;
13867
13868 /* Resolve basically using worker function. */
13869 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
13870 goto error;
13871
13872 /* Check the targets to be functions of correct interface. */
13873 for (target = stree->n.tb->u.generic; target; target = target->next)
13874 {
13875 gfc_symbol* target_proc;
13876
13877 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
13878 if (!target_proc)
13879 goto error;
13880
13881 if (!check_uop_procedure (target_proc, stree->n.tb->where))
13882 goto error;
13883 }
13884
13885 return;
13886
13887 error:
13888 resolve_bindings_result = false;
13889 stree->n.tb->error = 1;
13890 }
13891
13892
13893 /* Resolve the type-bound procedures for a derived type. */
13894
13895 static void
13896 resolve_typebound_procedure (gfc_symtree* stree)
13897 {
13898 gfc_symbol* proc;
13899 locus where;
13900 gfc_symbol* me_arg;
13901 gfc_symbol* super_type;
13902 gfc_component* comp;
13903
13904 gcc_assert (stree);
13905
13906 /* Undefined specific symbol from GENERIC target definition. */
13907 if (!stree->n.tb)
13908 return;
13909
13910 if (stree->n.tb->error)
13911 return;
13912
13913 /* If this is a GENERIC binding, use that routine. */
13914 if (stree->n.tb->is_generic)
13915 {
13916 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
13917 goto error;
13918 return;
13919 }
13920
13921 /* Get the target-procedure to check it. */
13922 gcc_assert (!stree->n.tb->is_generic);
13923 gcc_assert (stree->n.tb->u.specific);
13924 proc = stree->n.tb->u.specific->n.sym;
13925 where = stree->n.tb->where;
13926
13927 /* Default access should already be resolved from the parser. */
13928 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
13929
13930 if (stree->n.tb->deferred)
13931 {
13932 if (!check_proc_interface (proc, &where))
13933 goto error;
13934 }
13935 else
13936 {
13937 /* If proc has not been resolved at this point, proc->name may
13938 actually be a USE associated entity. See PR fortran/89647. */
13939 if (!proc->resolve_symbol_called
13940 && proc->attr.function == 0 && proc->attr.subroutine == 0)
13941 {
13942 gfc_symbol *tmp;
13943 gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
13944 if (tmp && tmp->attr.use_assoc)
13945 {
13946 proc->module = tmp->module;
13947 proc->attr.proc = tmp->attr.proc;
13948 proc->attr.function = tmp->attr.function;
13949 proc->attr.subroutine = tmp->attr.subroutine;
13950 proc->attr.use_assoc = tmp->attr.use_assoc;
13951 proc->ts = tmp->ts;
13952 proc->result = tmp->result;
13953 }
13954 }
13955
13956 /* Check for F08:C465. */
13957 if ((!proc->attr.subroutine && !proc->attr.function)
13958 || (proc->attr.proc != PROC_MODULE
13959 && proc->attr.if_source != IFSRC_IFBODY)
13960 || proc->attr.abstract)
13961 {
13962 gfc_error ("%qs must be a module procedure or an external "
13963 "procedure with an explicit interface at %L",
13964 proc->name, &where);
13965 goto error;
13966 }
13967 }
13968
13969 stree->n.tb->subroutine = proc->attr.subroutine;
13970 stree->n.tb->function = proc->attr.function;
13971
13972 /* Find the super-type of the current derived type. We could do this once and
13973 store in a global if speed is needed, but as long as not I believe this is
13974 more readable and clearer. */
13975 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
13976
13977 /* If PASS, resolve and check arguments if not already resolved / loaded
13978 from a .mod file. */
13979 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
13980 {
13981 gfc_formal_arglist *dummy_args;
13982
13983 dummy_args = gfc_sym_get_dummy_args (proc);
13984 if (stree->n.tb->pass_arg)
13985 {
13986 gfc_formal_arglist *i;
13987
13988 /* If an explicit passing argument name is given, walk the arg-list
13989 and look for it. */
13990
13991 me_arg = NULL;
13992 stree->n.tb->pass_arg_num = 1;
13993 for (i = dummy_args; i; i = i->next)
13994 {
13995 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
13996 {
13997 me_arg = i->sym;
13998 break;
13999 }
14000 ++stree->n.tb->pass_arg_num;
14001 }
14002
14003 if (!me_arg)
14004 {
14005 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
14006 " argument %qs",
14007 proc->name, stree->n.tb->pass_arg, &where,
14008 stree->n.tb->pass_arg);
14009 goto error;
14010 }
14011 }
14012 else
14013 {
14014 /* Otherwise, take the first one; there should in fact be at least
14015 one. */
14016 stree->n.tb->pass_arg_num = 1;
14017 if (!dummy_args)
14018 {
14019 gfc_error ("Procedure %qs with PASS at %L must have at"
14020 " least one argument", proc->name, &where);
14021 goto error;
14022 }
14023 me_arg = dummy_args->sym;
14024 }
14025
14026 /* Now check that the argument-type matches and the passed-object
14027 dummy argument is generally fine. */
14028
14029 gcc_assert (me_arg);
14030
14031 if (me_arg->ts.type != BT_CLASS)
14032 {
14033 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14034 " at %L", proc->name, &where);
14035 goto error;
14036 }
14037
14038 if (CLASS_DATA (me_arg)->ts.u.derived
14039 != resolve_bindings_derived)
14040 {
14041 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14042 " the derived-type %qs", me_arg->name, proc->name,
14043 me_arg->name, &where, resolve_bindings_derived->name);
14044 goto error;
14045 }
14046
14047 gcc_assert (me_arg->ts.type == BT_CLASS);
14048 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
14049 {
14050 gfc_error ("Passed-object dummy argument of %qs at %L must be"
14051 " scalar", proc->name, &where);
14052 goto error;
14053 }
14054 if (CLASS_DATA (me_arg)->attr.allocatable)
14055 {
14056 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14057 " be ALLOCATABLE", proc->name, &where);
14058 goto error;
14059 }
14060 if (CLASS_DATA (me_arg)->attr.class_pointer)
14061 {
14062 gfc_error ("Passed-object dummy argument of %qs at %L must not"
14063 " be POINTER", proc->name, &where);
14064 goto error;
14065 }
14066 }
14067
14068 /* If we are extending some type, check that we don't override a procedure
14069 flagged NON_OVERRIDABLE. */
14070 stree->n.tb->overridden = NULL;
14071 if (super_type)
14072 {
14073 gfc_symtree* overridden;
14074 overridden = gfc_find_typebound_proc (super_type, NULL,
14075 stree->name, true, NULL);
14076
14077 if (overridden)
14078 {
14079 if (overridden->n.tb)
14080 stree->n.tb->overridden = overridden->n.tb;
14081
14082 if (!gfc_check_typebound_override (stree, overridden))
14083 goto error;
14084 }
14085 }
14086
14087 /* See if there's a name collision with a component directly in this type. */
14088 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
14089 if (!strcmp (comp->name, stree->name))
14090 {
14091 gfc_error ("Procedure %qs at %L has the same name as a component of"
14092 " %qs",
14093 stree->name, &where, resolve_bindings_derived->name);
14094 goto error;
14095 }
14096
14097 /* Try to find a name collision with an inherited component. */
14098 if (super_type && gfc_find_component (super_type, stree->name, true, true,
14099 NULL))
14100 {
14101 gfc_error ("Procedure %qs at %L has the same name as an inherited"
14102 " component of %qs",
14103 stree->name, &where, resolve_bindings_derived->name);
14104 goto error;
14105 }
14106
14107 stree->n.tb->error = 0;
14108 return;
14109
14110 error:
14111 resolve_bindings_result = false;
14112 stree->n.tb->error = 1;
14113 }
14114
14115
14116 static bool
14117 resolve_typebound_procedures (gfc_symbol* derived)
14118 {
14119 int op;
14120 gfc_symbol* super_type;
14121
14122 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
14123 return true;
14124
14125 super_type = gfc_get_derived_super_type (derived);
14126 if (super_type)
14127 resolve_symbol (super_type);
14128
14129 resolve_bindings_derived = derived;
14130 resolve_bindings_result = true;
14131
14132 if (derived->f2k_derived->tb_sym_root)
14133 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
14134 &resolve_typebound_procedure);
14135
14136 if (derived->f2k_derived->tb_uop_root)
14137 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
14138 &resolve_typebound_user_op);
14139
14140 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
14141 {
14142 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
14143 if (p && !resolve_typebound_intrinsic_op (derived,
14144 (gfc_intrinsic_op)op, p))
14145 resolve_bindings_result = false;
14146 }
14147
14148 return resolve_bindings_result;
14149 }
14150
14151
14152 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
14153 to give all identical derived types the same backend_decl. */
14154 static void
14155 add_dt_to_dt_list (gfc_symbol *derived)
14156 {
14157 if (!derived->dt_next)
14158 {
14159 if (gfc_derived_types)
14160 {
14161 derived->dt_next = gfc_derived_types->dt_next;
14162 gfc_derived_types->dt_next = derived;
14163 }
14164 else
14165 {
14166 derived->dt_next = derived;
14167 }
14168 gfc_derived_types = derived;
14169 }
14170 }
14171
14172
14173 /* Ensure that a derived-type is really not abstract, meaning that every
14174 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
14175
14176 static bool
14177 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
14178 {
14179 if (!st)
14180 return true;
14181
14182 if (!ensure_not_abstract_walker (sub, st->left))
14183 return false;
14184 if (!ensure_not_abstract_walker (sub, st->right))
14185 return false;
14186
14187 if (st->n.tb && st->n.tb->deferred)
14188 {
14189 gfc_symtree* overriding;
14190 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
14191 if (!overriding)
14192 return false;
14193 gcc_assert (overriding->n.tb);
14194 if (overriding->n.tb->deferred)
14195 {
14196 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
14197 " %qs is DEFERRED and not overridden",
14198 sub->name, &sub->declared_at, st->name);
14199 return false;
14200 }
14201 }
14202
14203 return true;
14204 }
14205
14206 static bool
14207 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
14208 {
14209 /* The algorithm used here is to recursively travel up the ancestry of sub
14210 and for each ancestor-type, check all bindings. If any of them is
14211 DEFERRED, look it up starting from sub and see if the found (overriding)
14212 binding is not DEFERRED.
14213 This is not the most efficient way to do this, but it should be ok and is
14214 clearer than something sophisticated. */
14215
14216 gcc_assert (ancestor && !sub->attr.abstract);
14217
14218 if (!ancestor->attr.abstract)
14219 return true;
14220
14221 /* Walk bindings of this ancestor. */
14222 if (ancestor->f2k_derived)
14223 {
14224 bool t;
14225 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
14226 if (!t)
14227 return false;
14228 }
14229
14230 /* Find next ancestor type and recurse on it. */
14231 ancestor = gfc_get_derived_super_type (ancestor);
14232 if (ancestor)
14233 return ensure_not_abstract (sub, ancestor);
14234
14235 return true;
14236 }
14237
14238
14239 /* This check for typebound defined assignments is done recursively
14240 since the order in which derived types are resolved is not always in
14241 order of the declarations. */
14242
14243 static void
14244 check_defined_assignments (gfc_symbol *derived)
14245 {
14246 gfc_component *c;
14247
14248 for (c = derived->components; c; c = c->next)
14249 {
14250 if (!gfc_bt_struct (c->ts.type)
14251 || c->attr.pointer
14252 || c->attr.allocatable
14253 || c->attr.proc_pointer_comp
14254 || c->attr.class_pointer
14255 || c->attr.proc_pointer)
14256 continue;
14257
14258 if (c->ts.u.derived->attr.defined_assign_comp
14259 || (c->ts.u.derived->f2k_derived
14260 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
14261 {
14262 derived->attr.defined_assign_comp = 1;
14263 return;
14264 }
14265
14266 check_defined_assignments (c->ts.u.derived);
14267 if (c->ts.u.derived->attr.defined_assign_comp)
14268 {
14269 derived->attr.defined_assign_comp = 1;
14270 return;
14271 }
14272 }
14273 }
14274
14275
14276 /* Resolve a single component of a derived type or structure. */
14277
14278 static bool
14279 resolve_component (gfc_component *c, gfc_symbol *sym)
14280 {
14281 gfc_symbol *super_type;
14282 symbol_attribute *attr;
14283
14284 if (c->attr.artificial)
14285 return true;
14286
14287 /* Do not allow vtype components to be resolved in nameless namespaces
14288 such as block data because the procedure pointers will cause ICEs
14289 and vtables are not needed in these contexts. */
14290 if (sym->attr.vtype && sym->attr.use_assoc
14291 && sym->ns->proc_name == NULL)
14292 return true;
14293
14294 /* F2008, C442. */
14295 if ((!sym->attr.is_class || c != sym->components)
14296 && c->attr.codimension
14297 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
14298 {
14299 gfc_error ("Coarray component %qs at %L must be allocatable with "
14300 "deferred shape", c->name, &c->loc);
14301 return false;
14302 }
14303
14304 /* F2008, C443. */
14305 if (c->attr.codimension && c->ts.type == BT_DERIVED
14306 && c->ts.u.derived->ts.is_iso_c)
14307 {
14308 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
14309 "shall not be a coarray", c->name, &c->loc);
14310 return false;
14311 }
14312
14313 /* F2008, C444. */
14314 if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
14315 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
14316 || c->attr.allocatable))
14317 {
14318 gfc_error ("Component %qs at %L with coarray component "
14319 "shall be a nonpointer, nonallocatable scalar",
14320 c->name, &c->loc);
14321 return false;
14322 }
14323
14324 /* F2008, C448. */
14325 if (c->ts.type == BT_CLASS)
14326 {
14327 if (CLASS_DATA (c))
14328 {
14329 attr = &(CLASS_DATA (c)->attr);
14330
14331 /* Fix up contiguous attribute. */
14332 if (c->attr.contiguous)
14333 attr->contiguous = 1;
14334 }
14335 else
14336 attr = NULL;
14337 }
14338 else
14339 attr = &c->attr;
14340
14341 if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
14342 {
14343 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
14344 "is not an array pointer", c->name, &c->loc);
14345 return false;
14346 }
14347
14348 /* F2003, 15.2.1 - length has to be one. */
14349 if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
14350 && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
14351 || !gfc_is_constant_expr (c->ts.u.cl->length)
14352 || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
14353 {
14354 gfc_error ("Component %qs of BIND(C) type at %L must have length one",
14355 c->name, &c->loc);
14356 return false;
14357 }
14358
14359 if (c->attr.proc_pointer && c->ts.interface)
14360 {
14361 gfc_symbol *ifc = c->ts.interface;
14362
14363 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
14364 {
14365 c->tb->error = 1;
14366 return false;
14367 }
14368
14369 if (ifc->attr.if_source || ifc->attr.intrinsic)
14370 {
14371 /* Resolve interface and copy attributes. */
14372 if (ifc->formal && !ifc->formal_ns)
14373 resolve_symbol (ifc);
14374 if (ifc->attr.intrinsic)
14375 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
14376
14377 if (ifc->result)
14378 {
14379 c->ts = ifc->result->ts;
14380 c->attr.allocatable = ifc->result->attr.allocatable;
14381 c->attr.pointer = ifc->result->attr.pointer;
14382 c->attr.dimension = ifc->result->attr.dimension;
14383 c->as = gfc_copy_array_spec (ifc->result->as);
14384 c->attr.class_ok = ifc->result->attr.class_ok;
14385 }
14386 else
14387 {
14388 c->ts = ifc->ts;
14389 c->attr.allocatable = ifc->attr.allocatable;
14390 c->attr.pointer = ifc->attr.pointer;
14391 c->attr.dimension = ifc->attr.dimension;
14392 c->as = gfc_copy_array_spec (ifc->as);
14393 c->attr.class_ok = ifc->attr.class_ok;
14394 }
14395 c->ts.interface = ifc;
14396 c->attr.function = ifc->attr.function;
14397 c->attr.subroutine = ifc->attr.subroutine;
14398
14399 c->attr.pure = ifc->attr.pure;
14400 c->attr.elemental = ifc->attr.elemental;
14401 c->attr.recursive = ifc->attr.recursive;
14402 c->attr.always_explicit = ifc->attr.always_explicit;
14403 c->attr.ext_attr |= ifc->attr.ext_attr;
14404 /* Copy char length. */
14405 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
14406 {
14407 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
14408 if (cl->length && !cl->resolved
14409 && !gfc_resolve_expr (cl->length))
14410 {
14411 c->tb->error = 1;
14412 return false;
14413 }
14414 c->ts.u.cl = cl;
14415 }
14416 }
14417 }
14418 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
14419 {
14420 /* Since PPCs are not implicitly typed, a PPC without an explicit
14421 interface must be a subroutine. */
14422 gfc_add_subroutine (&c->attr, c->name, &c->loc);
14423 }
14424
14425 /* Procedure pointer components: Check PASS arg. */
14426 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
14427 && !sym->attr.vtype)
14428 {
14429 gfc_symbol* me_arg;
14430
14431 if (c->tb->pass_arg)
14432 {
14433 gfc_formal_arglist* i;
14434
14435 /* If an explicit passing argument name is given, walk the arg-list
14436 and look for it. */
14437
14438 me_arg = NULL;
14439 c->tb->pass_arg_num = 1;
14440 for (i = c->ts.interface->formal; i; i = i->next)
14441 {
14442 if (!strcmp (i->sym->name, c->tb->pass_arg))
14443 {
14444 me_arg = i->sym;
14445 break;
14446 }
14447 c->tb->pass_arg_num++;
14448 }
14449
14450 if (!me_arg)
14451 {
14452 gfc_error ("Procedure pointer component %qs with PASS(%s) "
14453 "at %L has no argument %qs", c->name,
14454 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
14455 c->tb->error = 1;
14456 return false;
14457 }
14458 }
14459 else
14460 {
14461 /* Otherwise, take the first one; there should in fact be at least
14462 one. */
14463 c->tb->pass_arg_num = 1;
14464 if (!c->ts.interface->formal)
14465 {
14466 gfc_error ("Procedure pointer component %qs with PASS at %L "
14467 "must have at least one argument",
14468 c->name, &c->loc);
14469 c->tb->error = 1;
14470 return false;
14471 }
14472 me_arg = c->ts.interface->formal->sym;
14473 }
14474
14475 /* Now check that the argument-type matches. */
14476 gcc_assert (me_arg);
14477 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
14478 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
14479 || (me_arg->ts.type == BT_CLASS
14480 && CLASS_DATA (me_arg)->ts.u.derived != sym))
14481 {
14482 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
14483 " the derived type %qs", me_arg->name, c->name,
14484 me_arg->name, &c->loc, sym->name);
14485 c->tb->error = 1;
14486 return false;
14487 }
14488
14489 /* Check for F03:C453. */
14490 if (CLASS_DATA (me_arg)->attr.dimension)
14491 {
14492 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14493 "must be scalar", me_arg->name, c->name, me_arg->name,
14494 &c->loc);
14495 c->tb->error = 1;
14496 return false;
14497 }
14498
14499 if (CLASS_DATA (me_arg)->attr.class_pointer)
14500 {
14501 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14502 "may not have the POINTER attribute", me_arg->name,
14503 c->name, me_arg->name, &c->loc);
14504 c->tb->error = 1;
14505 return false;
14506 }
14507
14508 if (CLASS_DATA (me_arg)->attr.allocatable)
14509 {
14510 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
14511 "may not be ALLOCATABLE", me_arg->name, c->name,
14512 me_arg->name, &c->loc);
14513 c->tb->error = 1;
14514 return false;
14515 }
14516
14517 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
14518 {
14519 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
14520 " at %L", c->name, &c->loc);
14521 return false;
14522 }
14523
14524 }
14525
14526 /* Check type-spec if this is not the parent-type component. */
14527 if (((sym->attr.is_class
14528 && (!sym->components->ts.u.derived->attr.extension
14529 || c != sym->components->ts.u.derived->components))
14530 || (!sym->attr.is_class
14531 && (!sym->attr.extension || c != sym->components)))
14532 && !sym->attr.vtype
14533 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
14534 return false;
14535
14536 super_type = gfc_get_derived_super_type (sym);
14537
14538 /* If this type is an extension, set the accessibility of the parent
14539 component. */
14540 if (super_type
14541 && ((sym->attr.is_class
14542 && c == sym->components->ts.u.derived->components)
14543 || (!sym->attr.is_class && c == sym->components))
14544 && strcmp (super_type->name, c->name) == 0)
14545 c->attr.access = super_type->attr.access;
14546
14547 /* If this type is an extension, see if this component has the same name
14548 as an inherited type-bound procedure. */
14549 if (super_type && !sym->attr.is_class
14550 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
14551 {
14552 gfc_error ("Component %qs of %qs at %L has the same name as an"
14553 " inherited type-bound procedure",
14554 c->name, sym->name, &c->loc);
14555 return false;
14556 }
14557
14558 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
14559 && !c->ts.deferred)
14560 {
14561 if (c->ts.u.cl->length == NULL
14562 || (!resolve_charlen(c->ts.u.cl))
14563 || !gfc_is_constant_expr (c->ts.u.cl->length))
14564 {
14565 gfc_error ("Character length of component %qs needs to "
14566 "be a constant specification expression at %L",
14567 c->name,
14568 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
14569 return false;
14570 }
14571 }
14572
14573 if (c->ts.type == BT_CHARACTER && c->ts.deferred
14574 && !c->attr.pointer && !c->attr.allocatable)
14575 {
14576 gfc_error ("Character component %qs of %qs at %L with deferred "
14577 "length must be a POINTER or ALLOCATABLE",
14578 c->name, sym->name, &c->loc);
14579 return false;
14580 }
14581
14582 /* Add the hidden deferred length field. */
14583 if (c->ts.type == BT_CHARACTER
14584 && (c->ts.deferred || c->attr.pdt_string)
14585 && !c->attr.function
14586 && !sym->attr.is_class)
14587 {
14588 char name[GFC_MAX_SYMBOL_LEN+9];
14589 gfc_component *strlen;
14590 sprintf (name, "_%s_length", c->name);
14591 strlen = gfc_find_component (sym, name, true, true, NULL);
14592 if (strlen == NULL)
14593 {
14594 if (!gfc_add_component (sym, name, &strlen))
14595 return false;
14596 strlen->ts.type = BT_INTEGER;
14597 strlen->ts.kind = gfc_charlen_int_kind;
14598 strlen->attr.access = ACCESS_PRIVATE;
14599 strlen->attr.artificial = 1;
14600 }
14601 }
14602
14603 if (c->ts.type == BT_DERIVED
14604 && sym->component_access != ACCESS_PRIVATE
14605 && gfc_check_symbol_access (sym)
14606 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
14607 && !c->ts.u.derived->attr.use_assoc
14608 && !gfc_check_symbol_access (c->ts.u.derived)
14609 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
14610 "PRIVATE type and cannot be a component of "
14611 "%qs, which is PUBLIC at %L", c->name,
14612 sym->name, &sym->declared_at))
14613 return false;
14614
14615 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
14616 {
14617 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
14618 "type %s", c->name, &c->loc, sym->name);
14619 return false;
14620 }
14621
14622 if (sym->attr.sequence)
14623 {
14624 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
14625 {
14626 gfc_error ("Component %s of SEQUENCE type declared at %L does "
14627 "not have the SEQUENCE attribute",
14628 c->ts.u.derived->name, &sym->declared_at);
14629 return false;
14630 }
14631 }
14632
14633 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
14634 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
14635 else if (c->ts.type == BT_CLASS && c->attr.class_ok
14636 && CLASS_DATA (c)->ts.u.derived->attr.generic)
14637 CLASS_DATA (c)->ts.u.derived
14638 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
14639
14640 /* If an allocatable component derived type is of the same type as
14641 the enclosing derived type, we need a vtable generating so that
14642 the __deallocate procedure is created. */
14643 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
14644 && c->ts.u.derived == sym && c->attr.allocatable == 1)
14645 gfc_find_vtab (&c->ts);
14646
14647 /* Ensure that all the derived type components are put on the
14648 derived type list; even in formal namespaces, where derived type
14649 pointer components might not have been declared. */
14650 if (c->ts.type == BT_DERIVED
14651 && c->ts.u.derived
14652 && c->ts.u.derived->components
14653 && c->attr.pointer
14654 && sym != c->ts.u.derived)
14655 add_dt_to_dt_list (c->ts.u.derived);
14656
14657 if (!gfc_resolve_array_spec (c->as,
14658 !(c->attr.pointer || c->attr.proc_pointer
14659 || c->attr.allocatable)))
14660 return false;
14661
14662 if (c->initializer && !sym->attr.vtype
14663 && !c->attr.pdt_kind && !c->attr.pdt_len
14664 && !gfc_check_assign_symbol (sym, c, c->initializer))
14665 return false;
14666
14667 return true;
14668 }
14669
14670
14671 /* Be nice about the locus for a structure expression - show the locus of the
14672 first non-null sub-expression if we can. */
14673
14674 static locus *
14675 cons_where (gfc_expr *struct_expr)
14676 {
14677 gfc_constructor *cons;
14678
14679 gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
14680
14681 cons = gfc_constructor_first (struct_expr->value.constructor);
14682 for (; cons; cons = gfc_constructor_next (cons))
14683 {
14684 if (cons->expr && cons->expr->expr_type != EXPR_NULL)
14685 return &cons->expr->where;
14686 }
14687
14688 return &struct_expr->where;
14689 }
14690
14691 /* Resolve the components of a structure type. Much less work than derived
14692 types. */
14693
14694 static bool
14695 resolve_fl_struct (gfc_symbol *sym)
14696 {
14697 gfc_component *c;
14698 gfc_expr *init = NULL;
14699 bool success;
14700
14701 /* Make sure UNIONs do not have overlapping initializers. */
14702 if (sym->attr.flavor == FL_UNION)
14703 {
14704 for (c = sym->components; c; c = c->next)
14705 {
14706 if (init && c->initializer)
14707 {
14708 gfc_error ("Conflicting initializers in union at %L and %L",
14709 cons_where (init), cons_where (c->initializer));
14710 gfc_free_expr (c->initializer);
14711 c->initializer = NULL;
14712 }
14713 if (init == NULL)
14714 init = c->initializer;
14715 }
14716 }
14717
14718 success = true;
14719 for (c = sym->components; c; c = c->next)
14720 if (!resolve_component (c, sym))
14721 success = false;
14722
14723 if (!success)
14724 return false;
14725
14726 if (sym->components)
14727 add_dt_to_dt_list (sym);
14728
14729 return true;
14730 }
14731
14732
14733 /* Resolve the components of a derived type. This does not have to wait until
14734 resolution stage, but can be done as soon as the dt declaration has been
14735 parsed. */
14736
14737 static bool
14738 resolve_fl_derived0 (gfc_symbol *sym)
14739 {
14740 gfc_symbol* super_type;
14741 gfc_component *c;
14742 gfc_formal_arglist *f;
14743 bool success;
14744
14745 if (sym->attr.unlimited_polymorphic)
14746 return true;
14747
14748 super_type = gfc_get_derived_super_type (sym);
14749
14750 /* F2008, C432. */
14751 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
14752 {
14753 gfc_error ("As extending type %qs at %L has a coarray component, "
14754 "parent type %qs shall also have one", sym->name,
14755 &sym->declared_at, super_type->name);
14756 return false;
14757 }
14758
14759 /* Ensure the extended type gets resolved before we do. */
14760 if (super_type && !resolve_fl_derived0 (super_type))
14761 return false;
14762
14763 /* An ABSTRACT type must be extensible. */
14764 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
14765 {
14766 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
14767 sym->name, &sym->declared_at);
14768 return false;
14769 }
14770
14771 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
14772 : sym->components;
14773
14774 success = true;
14775 for ( ; c != NULL; c = c->next)
14776 if (!resolve_component (c, sym))
14777 success = false;
14778
14779 if (!success)
14780 return false;
14781
14782 /* Now add the caf token field, where needed. */
14783 if (flag_coarray != GFC_FCOARRAY_NONE
14784 && !sym->attr.is_class && !sym->attr.vtype)
14785 {
14786 for (c = sym->components; c; c = c->next)
14787 if (!c->attr.dimension && !c->attr.codimension
14788 && (c->attr.allocatable || c->attr.pointer))
14789 {
14790 char name[GFC_MAX_SYMBOL_LEN+9];
14791 gfc_component *token;
14792 sprintf (name, "_caf_%s", c->name);
14793 token = gfc_find_component (sym, name, true, true, NULL);
14794 if (token == NULL)
14795 {
14796 if (!gfc_add_component (sym, name, &token))
14797 return false;
14798 token->ts.type = BT_VOID;
14799 token->ts.kind = gfc_default_integer_kind;
14800 token->attr.access = ACCESS_PRIVATE;
14801 token->attr.artificial = 1;
14802 token->attr.caf_token = 1;
14803 }
14804 }
14805 }
14806
14807 check_defined_assignments (sym);
14808
14809 if (!sym->attr.defined_assign_comp && super_type)
14810 sym->attr.defined_assign_comp
14811 = super_type->attr.defined_assign_comp;
14812
14813 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
14814 all DEFERRED bindings are overridden. */
14815 if (super_type && super_type->attr.abstract && !sym->attr.abstract
14816 && !sym->attr.is_class
14817 && !ensure_not_abstract (sym, super_type))
14818 return false;
14819
14820 /* Check that there is a component for every PDT parameter. */
14821 if (sym->attr.pdt_template)
14822 {
14823 for (f = sym->formal; f; f = f->next)
14824 {
14825 if (!f->sym)
14826 continue;
14827 c = gfc_find_component (sym, f->sym->name, true, true, NULL);
14828 if (c == NULL)
14829 {
14830 gfc_error ("Parameterized type %qs does not have a component "
14831 "corresponding to parameter %qs at %L", sym->name,
14832 f->sym->name, &sym->declared_at);
14833 break;
14834 }
14835 }
14836 }
14837
14838 /* Add derived type to the derived type list. */
14839 add_dt_to_dt_list (sym);
14840
14841 return true;
14842 }
14843
14844
14845 /* The following procedure does the full resolution of a derived type,
14846 including resolution of all type-bound procedures (if present). In contrast
14847 to 'resolve_fl_derived0' this can only be done after the module has been
14848 parsed completely. */
14849
14850 static bool
14851 resolve_fl_derived (gfc_symbol *sym)
14852 {
14853 gfc_symbol *gen_dt = NULL;
14854
14855 if (sym->attr.unlimited_polymorphic)
14856 return true;
14857
14858 if (!sym->attr.is_class)
14859 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
14860 if (gen_dt && gen_dt->generic && gen_dt->generic->next
14861 && (!gen_dt->generic->sym->attr.use_assoc
14862 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
14863 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
14864 "%qs at %L being the same name as derived "
14865 "type at %L", sym->name,
14866 gen_dt->generic->sym == sym
14867 ? gen_dt->generic->next->sym->name
14868 : gen_dt->generic->sym->name,
14869 gen_dt->generic->sym == sym
14870 ? &gen_dt->generic->next->sym->declared_at
14871 : &gen_dt->generic->sym->declared_at,
14872 &sym->declared_at))
14873 return false;
14874
14875 if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
14876 {
14877 gfc_error ("Derived type %qs at %L has not been declared",
14878 sym->name, &sym->declared_at);
14879 return false;
14880 }
14881
14882 /* Resolve the finalizer procedures. */
14883 if (!gfc_resolve_finalizers (sym, NULL))
14884 return false;
14885
14886 if (sym->attr.is_class && sym->ts.u.derived == NULL)
14887 {
14888 /* Fix up incomplete CLASS symbols. */
14889 gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
14890 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
14891
14892 /* Nothing more to do for unlimited polymorphic entities. */
14893 if (data->ts.u.derived->attr.unlimited_polymorphic)
14894 return true;
14895 else if (vptr->ts.u.derived == NULL)
14896 {
14897 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
14898 gcc_assert (vtab);
14899 vptr->ts.u.derived = vtab->ts.u.derived;
14900 if (!resolve_fl_derived0 (vptr->ts.u.derived))
14901 return false;
14902 }
14903 }
14904
14905 if (!resolve_fl_derived0 (sym))
14906 return false;
14907
14908 /* Resolve the type-bound procedures. */
14909 if (!resolve_typebound_procedures (sym))
14910 return false;
14911
14912 /* Generate module vtables subject to their accessibility and their not
14913 being vtables or pdt templates. If this is not done class declarations
14914 in external procedures wind up with their own version and so SELECT TYPE
14915 fails because the vptrs do not have the same address. */
14916 if (gfc_option.allow_std & GFC_STD_F2003
14917 && sym->ns->proc_name
14918 && sym->ns->proc_name->attr.flavor == FL_MODULE
14919 && sym->attr.access != ACCESS_PRIVATE
14920 && !(sym->attr.use_assoc || sym->attr.vtype || sym->attr.pdt_template))
14921 {
14922 gfc_symbol *vtab = gfc_find_derived_vtab (sym);
14923 gfc_set_sym_referenced (vtab);
14924 }
14925
14926 return true;
14927 }
14928
14929
14930 static bool
14931 resolve_fl_namelist (gfc_symbol *sym)
14932 {
14933 gfc_namelist *nl;
14934 gfc_symbol *nlsym;
14935
14936 for (nl = sym->namelist; nl; nl = nl->next)
14937 {
14938 /* Check again, the check in match only works if NAMELIST comes
14939 after the decl. */
14940 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
14941 {
14942 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
14943 "allowed", nl->sym->name, sym->name, &sym->declared_at);
14944 return false;
14945 }
14946
14947 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
14948 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
14949 "with assumed shape in namelist %qs at %L",
14950 nl->sym->name, sym->name, &sym->declared_at))
14951 return false;
14952
14953 if (is_non_constant_shape_array (nl->sym)
14954 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
14955 "with nonconstant shape in namelist %qs at %L",
14956 nl->sym->name, sym->name, &sym->declared_at))
14957 return false;
14958
14959 if (nl->sym->ts.type == BT_CHARACTER
14960 && (nl->sym->ts.u.cl->length == NULL
14961 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
14962 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
14963 "nonconstant character length in "
14964 "namelist %qs at %L", nl->sym->name,
14965 sym->name, &sym->declared_at))
14966 return false;
14967
14968 }
14969
14970 /* Reject PRIVATE objects in a PUBLIC namelist. */
14971 if (gfc_check_symbol_access (sym))
14972 {
14973 for (nl = sym->namelist; nl; nl = nl->next)
14974 {
14975 if (!nl->sym->attr.use_assoc
14976 && !is_sym_host_assoc (nl->sym, sym->ns)
14977 && !gfc_check_symbol_access (nl->sym))
14978 {
14979 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
14980 "cannot be member of PUBLIC namelist %qs at %L",
14981 nl->sym->name, sym->name, &sym->declared_at);
14982 return false;
14983 }
14984
14985 if (nl->sym->ts.type == BT_DERIVED
14986 && (nl->sym->ts.u.derived->attr.alloc_comp
14987 || nl->sym->ts.u.derived->attr.pointer_comp))
14988 {
14989 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
14990 "namelist %qs at %L with ALLOCATABLE "
14991 "or POINTER components", nl->sym->name,
14992 sym->name, &sym->declared_at))
14993 return false;
14994 return true;
14995 }
14996
14997 /* Types with private components that came here by USE-association. */
14998 if (nl->sym->ts.type == BT_DERIVED
14999 && derived_inaccessible (nl->sym->ts.u.derived))
15000 {
15001 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
15002 "components and cannot be member of namelist %qs at %L",
15003 nl->sym->name, sym->name, &sym->declared_at);
15004 return false;
15005 }
15006
15007 /* Types with private components that are defined in the same module. */
15008 if (nl->sym->ts.type == BT_DERIVED
15009 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
15010 && nl->sym->ts.u.derived->attr.private_comp)
15011 {
15012 gfc_error ("NAMELIST object %qs has PRIVATE components and "
15013 "cannot be a member of PUBLIC namelist %qs at %L",
15014 nl->sym->name, sym->name, &sym->declared_at);
15015 return false;
15016 }
15017 }
15018 }
15019
15020
15021 /* 14.1.2 A module or internal procedure represent local entities
15022 of the same type as a namelist member and so are not allowed. */
15023 for (nl = sym->namelist; nl; nl = nl->next)
15024 {
15025 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
15026 continue;
15027
15028 if (nl->sym->attr.function && nl->sym == nl->sym->result)
15029 if ((nl->sym == sym->ns->proc_name)
15030 ||
15031 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
15032 continue;
15033
15034 nlsym = NULL;
15035 if (nl->sym->name)
15036 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
15037 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
15038 {
15039 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
15040 "attribute in %qs at %L", nlsym->name,
15041 &sym->declared_at);
15042 return false;
15043 }
15044 }
15045
15046 return true;
15047 }
15048
15049
15050 static bool
15051 resolve_fl_parameter (gfc_symbol *sym)
15052 {
15053 /* A parameter array's shape needs to be constant. */
15054 if (sym->as != NULL
15055 && (sym->as->type == AS_DEFERRED
15056 || is_non_constant_shape_array (sym)))
15057 {
15058 gfc_error ("Parameter array %qs at %L cannot be automatic "
15059 "or of deferred shape", sym->name, &sym->declared_at);
15060 return false;
15061 }
15062
15063 /* Constraints on deferred type parameter. */
15064 if (!deferred_requirements (sym))
15065 return false;
15066
15067 /* Make sure a parameter that has been implicitly typed still
15068 matches the implicit type, since PARAMETER statements can precede
15069 IMPLICIT statements. */
15070 if (sym->attr.implicit_type
15071 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
15072 sym->ns)))
15073 {
15074 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
15075 "later IMPLICIT type", sym->name, &sym->declared_at);
15076 return false;
15077 }
15078
15079 /* Make sure the types of derived parameters are consistent. This
15080 type checking is deferred until resolution because the type may
15081 refer to a derived type from the host. */
15082 if (sym->ts.type == BT_DERIVED
15083 && !gfc_compare_types (&sym->ts, &sym->value->ts))
15084 {
15085 gfc_error ("Incompatible derived type in PARAMETER at %L",
15086 &sym->value->where);
15087 return false;
15088 }
15089
15090 /* F03:C509,C514. */
15091 if (sym->ts.type == BT_CLASS)
15092 {
15093 gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
15094 sym->name, &sym->declared_at);
15095 return false;
15096 }
15097
15098 return true;
15099 }
15100
15101
15102 /* Called by resolve_symbol to check PDTs. */
15103
15104 static void
15105 resolve_pdt (gfc_symbol* sym)
15106 {
15107 gfc_symbol *derived = NULL;
15108 gfc_actual_arglist *param;
15109 gfc_component *c;
15110 bool const_len_exprs = true;
15111 bool assumed_len_exprs = false;
15112 symbol_attribute *attr;
15113
15114 if (sym->ts.type == BT_DERIVED)
15115 {
15116 derived = sym->ts.u.derived;
15117 attr = &(sym->attr);
15118 }
15119 else if (sym->ts.type == BT_CLASS)
15120 {
15121 derived = CLASS_DATA (sym)->ts.u.derived;
15122 attr = &(CLASS_DATA (sym)->attr);
15123 }
15124 else
15125 gcc_unreachable ();
15126
15127 gcc_assert (derived->attr.pdt_type);
15128
15129 for (param = sym->param_list; param; param = param->next)
15130 {
15131 c = gfc_find_component (derived, param->name, false, true, NULL);
15132 gcc_assert (c);
15133 if (c->attr.pdt_kind)
15134 continue;
15135
15136 if (param->expr && !gfc_is_constant_expr (param->expr)
15137 && c->attr.pdt_len)
15138 const_len_exprs = false;
15139 else if (param->spec_type == SPEC_ASSUMED)
15140 assumed_len_exprs = true;
15141
15142 if (param->spec_type == SPEC_DEFERRED
15143 && !attr->allocatable && !attr->pointer)
15144 gfc_error ("The object %qs at %L has a deferred LEN "
15145 "parameter %qs and is neither allocatable "
15146 "nor a pointer", sym->name, &sym->declared_at,
15147 param->name);
15148
15149 }
15150
15151 if (!const_len_exprs
15152 && (sym->ns->proc_name->attr.is_main_program
15153 || sym->ns->proc_name->attr.flavor == FL_MODULE
15154 || sym->attr.save != SAVE_NONE))
15155 gfc_error ("The AUTOMATIC object %qs at %L must not have the "
15156 "SAVE attribute or be a variable declared in the "
15157 "main program, a module or a submodule(F08/C513)",
15158 sym->name, &sym->declared_at);
15159
15160 if (assumed_len_exprs && !(sym->attr.dummy
15161 || sym->attr.select_type_temporary || sym->attr.associate_var))
15162 gfc_error ("The object %qs at %L with ASSUMED type parameters "
15163 "must be a dummy or a SELECT TYPE selector(F08/4.2)",
15164 sym->name, &sym->declared_at);
15165 }
15166
15167
15168 /* Do anything necessary to resolve a symbol. Right now, we just
15169 assume that an otherwise unknown symbol is a variable. This sort
15170 of thing commonly happens for symbols in module. */
15171
15172 static void
15173 resolve_symbol (gfc_symbol *sym)
15174 {
15175 int check_constant, mp_flag;
15176 gfc_symtree *symtree;
15177 gfc_symtree *this_symtree;
15178 gfc_namespace *ns;
15179 gfc_component *c;
15180 symbol_attribute class_attr;
15181 gfc_array_spec *as;
15182 bool saved_specification_expr;
15183
15184 if (sym->resolve_symbol_called >= 1)
15185 return;
15186 sym->resolve_symbol_called = 1;
15187
15188 /* No symbol will ever have union type; only components can be unions.
15189 Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
15190 (just like derived type declaration symbols have flavor FL_DERIVED). */
15191 gcc_assert (sym->ts.type != BT_UNION);
15192
15193 /* Coarrayed polymorphic objects with allocatable or pointer components are
15194 yet unsupported for -fcoarray=lib. */
15195 if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
15196 && sym->ts.u.derived && CLASS_DATA (sym)
15197 && CLASS_DATA (sym)->attr.codimension
15198 && CLASS_DATA (sym)->ts.u.derived
15199 && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
15200 || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
15201 {
15202 gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
15203 "type coarrays at %L are unsupported", &sym->declared_at);
15204 return;
15205 }
15206
15207 if (sym->attr.artificial)
15208 return;
15209
15210 if (sym->attr.unlimited_polymorphic)
15211 return;
15212
15213 if (sym->attr.flavor == FL_UNKNOWN
15214 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
15215 && !sym->attr.generic && !sym->attr.external
15216 && sym->attr.if_source == IFSRC_UNKNOWN
15217 && sym->ts.type == BT_UNKNOWN))
15218 {
15219
15220 /* If we find that a flavorless symbol is an interface in one of the
15221 parent namespaces, find its symtree in this namespace, free the
15222 symbol and set the symtree to point to the interface symbol. */
15223 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
15224 {
15225 symtree = gfc_find_symtree (ns->sym_root, sym->name);
15226 if (symtree && (symtree->n.sym->generic ||
15227 (symtree->n.sym->attr.flavor == FL_PROCEDURE
15228 && sym->ns->construct_entities)))
15229 {
15230 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
15231 sym->name);
15232 if (this_symtree->n.sym == sym)
15233 {
15234 symtree->n.sym->refs++;
15235 gfc_release_symbol (sym);
15236 this_symtree->n.sym = symtree->n.sym;
15237 return;
15238 }
15239 }
15240 }
15241
15242 /* Otherwise give it a flavor according to such attributes as
15243 it has. */
15244 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
15245 && sym->attr.intrinsic == 0)
15246 sym->attr.flavor = FL_VARIABLE;
15247 else if (sym->attr.flavor == FL_UNKNOWN)
15248 {
15249 sym->attr.flavor = FL_PROCEDURE;
15250 if (sym->attr.dimension)
15251 sym->attr.function = 1;
15252 }
15253 }
15254
15255 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
15256 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
15257
15258 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
15259 && !resolve_procedure_interface (sym))
15260 return;
15261
15262 if (sym->attr.is_protected && !sym->attr.proc_pointer
15263 && (sym->attr.procedure || sym->attr.external))
15264 {
15265 if (sym->attr.external)
15266 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
15267 "at %L", &sym->declared_at);
15268 else
15269 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
15270 "at %L", &sym->declared_at);
15271
15272 return;
15273 }
15274
15275 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
15276 return;
15277
15278 else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
15279 && !resolve_fl_struct (sym))
15280 return;
15281
15282 /* Symbols that are module procedures with results (functions) have
15283 the types and array specification copied for type checking in
15284 procedures that call them, as well as for saving to a module
15285 file. These symbols can't stand the scrutiny that their results
15286 can. */
15287 mp_flag = (sym->result != NULL && sym->result != sym);
15288
15289 /* Make sure that the intrinsic is consistent with its internal
15290 representation. This needs to be done before assigning a default
15291 type to avoid spurious warnings. */
15292 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
15293 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
15294 return;
15295
15296 /* Resolve associate names. */
15297 if (sym->assoc)
15298 resolve_assoc_var (sym, true);
15299
15300 /* Assign default type to symbols that need one and don't have one. */
15301 if (sym->ts.type == BT_UNKNOWN)
15302 {
15303 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
15304 {
15305 gfc_set_default_type (sym, 1, NULL);
15306 }
15307
15308 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
15309 && !sym->attr.function && !sym->attr.subroutine
15310 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
15311 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
15312
15313 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
15314 {
15315 /* The specific case of an external procedure should emit an error
15316 in the case that there is no implicit type. */
15317 if (!mp_flag)
15318 {
15319 if (!sym->attr.mixed_entry_master)
15320 gfc_set_default_type (sym, sym->attr.external, NULL);
15321 }
15322 else
15323 {
15324 /* Result may be in another namespace. */
15325 resolve_symbol (sym->result);
15326
15327 if (!sym->result->attr.proc_pointer)
15328 {
15329 sym->ts = sym->result->ts;
15330 sym->as = gfc_copy_array_spec (sym->result->as);
15331 sym->attr.dimension = sym->result->attr.dimension;
15332 sym->attr.pointer = sym->result->attr.pointer;
15333 sym->attr.allocatable = sym->result->attr.allocatable;
15334 sym->attr.contiguous = sym->result->attr.contiguous;
15335 }
15336 }
15337 }
15338 }
15339 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
15340 {
15341 bool saved_specification_expr = specification_expr;
15342 specification_expr = true;
15343 gfc_resolve_array_spec (sym->result->as, false);
15344 specification_expr = saved_specification_expr;
15345 }
15346
15347 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
15348 {
15349 as = CLASS_DATA (sym)->as;
15350 class_attr = CLASS_DATA (sym)->attr;
15351 class_attr.pointer = class_attr.class_pointer;
15352 }
15353 else
15354 {
15355 class_attr = sym->attr;
15356 as = sym->as;
15357 }
15358
15359 /* F2008, C530. */
15360 if (sym->attr.contiguous
15361 && (!class_attr.dimension
15362 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
15363 && !class_attr.pointer)))
15364 {
15365 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
15366 "array pointer or an assumed-shape or assumed-rank array",
15367 sym->name, &sym->declared_at);
15368 return;
15369 }
15370
15371 /* Assumed size arrays and assumed shape arrays must be dummy
15372 arguments. Array-spec's of implied-shape should have been resolved to
15373 AS_EXPLICIT already. */
15374
15375 if (as)
15376 {
15377 /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
15378 specification expression. */
15379 if (as->type == AS_IMPLIED_SHAPE)
15380 {
15381 int i;
15382 for (i=0; i<as->rank; i++)
15383 {
15384 if (as->lower[i] != NULL && as->upper[i] == NULL)
15385 {
15386 gfc_error ("Bad specification for assumed size array at %L",
15387 &as->lower[i]->where);
15388 return;
15389 }
15390 }
15391 gcc_unreachable();
15392 }
15393
15394 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
15395 || as->type == AS_ASSUMED_SHAPE)
15396 && !sym->attr.dummy && !sym->attr.select_type_temporary)
15397 {
15398 if (as->type == AS_ASSUMED_SIZE)
15399 gfc_error ("Assumed size array at %L must be a dummy argument",
15400 &sym->declared_at);
15401 else
15402 gfc_error ("Assumed shape array at %L must be a dummy argument",
15403 &sym->declared_at);
15404 return;
15405 }
15406 /* TS 29113, C535a. */
15407 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
15408 && !sym->attr.select_type_temporary
15409 && !(cs_base && cs_base->current
15410 && cs_base->current->op == EXEC_SELECT_RANK))
15411 {
15412 gfc_error ("Assumed-rank array at %L must be a dummy argument",
15413 &sym->declared_at);
15414 return;
15415 }
15416 if (as->type == AS_ASSUMED_RANK
15417 && (sym->attr.codimension || sym->attr.value))
15418 {
15419 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
15420 "CODIMENSION attribute", &sym->declared_at);
15421 return;
15422 }
15423 }
15424
15425 /* Make sure symbols with known intent or optional are really dummy
15426 variable. Because of ENTRY statement, this has to be deferred
15427 until resolution time. */
15428
15429 if (!sym->attr.dummy
15430 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
15431 {
15432 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
15433 return;
15434 }
15435
15436 if (sym->attr.value && !sym->attr.dummy)
15437 {
15438 gfc_error ("%qs at %L cannot have the VALUE attribute because "
15439 "it is not a dummy argument", sym->name, &sym->declared_at);
15440 return;
15441 }
15442
15443 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
15444 {
15445 gfc_charlen *cl = sym->ts.u.cl;
15446 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
15447 {
15448 gfc_error ("Character dummy variable %qs at %L with VALUE "
15449 "attribute must have constant length",
15450 sym->name, &sym->declared_at);
15451 return;
15452 }
15453
15454 if (sym->ts.is_c_interop
15455 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
15456 {
15457 gfc_error ("C interoperable character dummy variable %qs at %L "
15458 "with VALUE attribute must have length one",
15459 sym->name, &sym->declared_at);
15460 return;
15461 }
15462 }
15463
15464 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
15465 && sym->ts.u.derived->attr.generic)
15466 {
15467 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
15468 if (!sym->ts.u.derived)
15469 {
15470 gfc_error ("The derived type %qs at %L is of type %qs, "
15471 "which has not been defined", sym->name,
15472 &sym->declared_at, sym->ts.u.derived->name);
15473 sym->ts.type = BT_UNKNOWN;
15474 return;
15475 }
15476 }
15477
15478 /* Use the same constraints as TYPE(*), except for the type check
15479 and that only scalars and assumed-size arrays are permitted. */
15480 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
15481 {
15482 if (!sym->attr.dummy)
15483 {
15484 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
15485 "a dummy argument", sym->name, &sym->declared_at);
15486 return;
15487 }
15488
15489 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
15490 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
15491 && sym->ts.type != BT_COMPLEX)
15492 {
15493 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
15494 "of type TYPE(*) or of an numeric intrinsic type",
15495 sym->name, &sym->declared_at);
15496 return;
15497 }
15498
15499 if (sym->attr.allocatable || sym->attr.codimension
15500 || sym->attr.pointer || sym->attr.value)
15501 {
15502 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
15503 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
15504 "attribute", sym->name, &sym->declared_at);
15505 return;
15506 }
15507
15508 if (sym->attr.intent == INTENT_OUT)
15509 {
15510 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
15511 "have the INTENT(OUT) attribute",
15512 sym->name, &sym->declared_at);
15513 return;
15514 }
15515 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
15516 {
15517 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
15518 "either be a scalar or an assumed-size array",
15519 sym->name, &sym->declared_at);
15520 return;
15521 }
15522
15523 /* Set the type to TYPE(*) and add a dimension(*) to ensure
15524 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
15525 packing. */
15526 sym->ts.type = BT_ASSUMED;
15527 sym->as = gfc_get_array_spec ();
15528 sym->as->type = AS_ASSUMED_SIZE;
15529 sym->as->rank = 1;
15530 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
15531 }
15532 else if (sym->ts.type == BT_ASSUMED)
15533 {
15534 /* TS 29113, C407a. */
15535 if (!sym->attr.dummy)
15536 {
15537 gfc_error ("Assumed type of variable %s at %L is only permitted "
15538 "for dummy variables", sym->name, &sym->declared_at);
15539 return;
15540 }
15541 if (sym->attr.allocatable || sym->attr.codimension
15542 || sym->attr.pointer || sym->attr.value)
15543 {
15544 gfc_error ("Assumed-type variable %s at %L may not have the "
15545 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
15546 sym->name, &sym->declared_at);
15547 return;
15548 }
15549 if (sym->attr.intent == INTENT_OUT)
15550 {
15551 gfc_error ("Assumed-type variable %s at %L may not have the "
15552 "INTENT(OUT) attribute",
15553 sym->name, &sym->declared_at);
15554 return;
15555 }
15556 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
15557 {
15558 gfc_error ("Assumed-type variable %s at %L shall not be an "
15559 "explicit-shape array", sym->name, &sym->declared_at);
15560 return;
15561 }
15562 }
15563
15564 /* If the symbol is marked as bind(c), that it is declared at module level
15565 scope and verify its type and kind. Do not do the latter for symbols
15566 that are implicitly typed because that is handled in
15567 gfc_set_default_type. Handle dummy arguments and procedure definitions
15568 separately. Also, anything that is use associated is not handled here
15569 but instead is handled in the module it is declared in. Finally, derived
15570 type definitions are allowed to be BIND(C) since that only implies that
15571 they're interoperable, and they are checked fully for interoperability
15572 when a variable is declared of that type. */
15573 if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
15574 && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
15575 && sym->attr.flavor != FL_DERIVED)
15576 {
15577 bool t = true;
15578
15579 /* First, make sure the variable is declared at the
15580 module-level scope (J3/04-007, Section 15.3). */
15581 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
15582 sym->attr.in_common == 0)
15583 {
15584 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
15585 "is neither a COMMON block nor declared at the "
15586 "module level scope", sym->name, &(sym->declared_at));
15587 t = false;
15588 }
15589 else if (sym->ts.type == BT_CHARACTER
15590 && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
15591 || !gfc_is_constant_expr (sym->ts.u.cl->length)
15592 || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
15593 {
15594 gfc_error ("BIND(C) Variable %qs at %L must have length one",
15595 sym->name, &sym->declared_at);
15596 t = false;
15597 }
15598 else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
15599 {
15600 t = verify_com_block_vars_c_interop (sym->common_head);
15601 }
15602 else if (sym->attr.implicit_type == 0)
15603 {
15604 /* If type() declaration, we need to verify that the components
15605 of the given type are all C interoperable, etc. */
15606 if (sym->ts.type == BT_DERIVED &&
15607 sym->ts.u.derived->attr.is_c_interop != 1)
15608 {
15609 /* Make sure the user marked the derived type as BIND(C). If
15610 not, call the verify routine. This could print an error
15611 for the derived type more than once if multiple variables
15612 of that type are declared. */
15613 if (sym->ts.u.derived->attr.is_bind_c != 1)
15614 verify_bind_c_derived_type (sym->ts.u.derived);
15615 t = false;
15616 }
15617
15618 /* Verify the variable itself as C interoperable if it
15619 is BIND(C). It is not possible for this to succeed if
15620 the verify_bind_c_derived_type failed, so don't have to handle
15621 any error returned by verify_bind_c_derived_type. */
15622 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
15623 sym->common_block);
15624 }
15625
15626 if (!t)
15627 {
15628 /* clear the is_bind_c flag to prevent reporting errors more than
15629 once if something failed. */
15630 sym->attr.is_bind_c = 0;
15631 return;
15632 }
15633 }
15634
15635 /* If a derived type symbol has reached this point, without its
15636 type being declared, we have an error. Notice that most
15637 conditions that produce undefined derived types have already
15638 been dealt with. However, the likes of:
15639 implicit type(t) (t) ..... call foo (t) will get us here if
15640 the type is not declared in the scope of the implicit
15641 statement. Change the type to BT_UNKNOWN, both because it is so
15642 and to prevent an ICE. */
15643 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
15644 && sym->ts.u.derived->components == NULL
15645 && !sym->ts.u.derived->attr.zero_comp)
15646 {
15647 gfc_error ("The derived type %qs at %L is of type %qs, "
15648 "which has not been defined", sym->name,
15649 &sym->declared_at, sym->ts.u.derived->name);
15650 sym->ts.type = BT_UNKNOWN;
15651 return;
15652 }
15653
15654 /* Make sure that the derived type has been resolved and that the
15655 derived type is visible in the symbol's namespace, if it is a
15656 module function and is not PRIVATE. */
15657 if (sym->ts.type == BT_DERIVED
15658 && sym->ts.u.derived->attr.use_assoc
15659 && sym->ns->proc_name
15660 && sym->ns->proc_name->attr.flavor == FL_MODULE
15661 && !resolve_fl_derived (sym->ts.u.derived))
15662 return;
15663
15664 /* Unless the derived-type declaration is use associated, Fortran 95
15665 does not allow public entries of private derived types.
15666 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
15667 161 in 95-006r3. */
15668 if (sym->ts.type == BT_DERIVED
15669 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
15670 && !sym->ts.u.derived->attr.use_assoc
15671 && gfc_check_symbol_access (sym)
15672 && !gfc_check_symbol_access (sym->ts.u.derived)
15673 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
15674 "derived type %qs",
15675 (sym->attr.flavor == FL_PARAMETER)
15676 ? "parameter" : "variable",
15677 sym->name, &sym->declared_at,
15678 sym->ts.u.derived->name))
15679 return;
15680
15681 /* F2008, C1302. */
15682 if (sym->ts.type == BT_DERIVED
15683 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
15684 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
15685 || sym->ts.u.derived->attr.lock_comp)
15686 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
15687 {
15688 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
15689 "type LOCK_TYPE must be a coarray", sym->name,
15690 &sym->declared_at);
15691 return;
15692 }
15693
15694 /* TS18508, C702/C703. */
15695 if (sym->ts.type == BT_DERIVED
15696 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
15697 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
15698 || sym->ts.u.derived->attr.event_comp)
15699 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
15700 {
15701 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
15702 "type EVENT_TYPE must be a coarray", sym->name,
15703 &sym->declared_at);
15704 return;
15705 }
15706
15707 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
15708 default initialization is defined (5.1.2.4.4). */
15709 if (sym->ts.type == BT_DERIVED
15710 && sym->attr.dummy
15711 && sym->attr.intent == INTENT_OUT
15712 && sym->as
15713 && sym->as->type == AS_ASSUMED_SIZE)
15714 {
15715 for (c = sym->ts.u.derived->components; c; c = c->next)
15716 {
15717 if (c->initializer)
15718 {
15719 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
15720 "ASSUMED SIZE and so cannot have a default initializer",
15721 sym->name, &sym->declared_at);
15722 return;
15723 }
15724 }
15725 }
15726
15727 /* F2008, C542. */
15728 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
15729 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
15730 {
15731 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
15732 "INTENT(OUT)", sym->name, &sym->declared_at);
15733 return;
15734 }
15735
15736 /* TS18508. */
15737 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
15738 && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
15739 {
15740 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
15741 "INTENT(OUT)", sym->name, &sym->declared_at);
15742 return;
15743 }
15744
15745 /* F2008, C525. */
15746 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15747 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15748 && CLASS_DATA (sym)->attr.coarray_comp))
15749 || class_attr.codimension)
15750 && (sym->attr.result || sym->result == sym))
15751 {
15752 gfc_error ("Function result %qs at %L shall not be a coarray or have "
15753 "a coarray component", sym->name, &sym->declared_at);
15754 return;
15755 }
15756
15757 /* F2008, C524. */
15758 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
15759 && sym->ts.u.derived->ts.is_iso_c)
15760 {
15761 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
15762 "shall not be a coarray", sym->name, &sym->declared_at);
15763 return;
15764 }
15765
15766 /* F2008, C525. */
15767 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15768 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15769 && CLASS_DATA (sym)->attr.coarray_comp))
15770 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
15771 || class_attr.allocatable))
15772 {
15773 gfc_error ("Variable %qs at %L with coarray component shall be a "
15774 "nonpointer, nonallocatable scalar, which is not a coarray",
15775 sym->name, &sym->declared_at);
15776 return;
15777 }
15778
15779 /* F2008, C526. The function-result case was handled above. */
15780 if (class_attr.codimension
15781 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
15782 || sym->attr.select_type_temporary
15783 || sym->attr.associate_var
15784 || (sym->ns->save_all && !sym->attr.automatic)
15785 || sym->ns->proc_name->attr.flavor == FL_MODULE
15786 || sym->ns->proc_name->attr.is_main_program
15787 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
15788 {
15789 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
15790 "nor a dummy argument", sym->name, &sym->declared_at);
15791 return;
15792 }
15793 /* F2008, C528. */
15794 else if (class_attr.codimension && !sym->attr.select_type_temporary
15795 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
15796 {
15797 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
15798 "deferred shape", sym->name, &sym->declared_at);
15799 return;
15800 }
15801 else if (class_attr.codimension && class_attr.allocatable && as
15802 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
15803 {
15804 gfc_error ("Allocatable coarray variable %qs at %L must have "
15805 "deferred shape", sym->name, &sym->declared_at);
15806 return;
15807 }
15808
15809 /* F2008, C541. */
15810 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
15811 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15812 && CLASS_DATA (sym)->attr.coarray_comp))
15813 || (class_attr.codimension && class_attr.allocatable))
15814 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
15815 {
15816 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
15817 "allocatable coarray or have coarray components",
15818 sym->name, &sym->declared_at);
15819 return;
15820 }
15821
15822 if (class_attr.codimension && sym->attr.dummy
15823 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
15824 {
15825 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
15826 "procedure %qs", sym->name, &sym->declared_at,
15827 sym->ns->proc_name->name);
15828 return;
15829 }
15830
15831 if (sym->ts.type == BT_LOGICAL
15832 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
15833 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
15834 && sym->ns->proc_name->attr.is_bind_c)))
15835 {
15836 int i;
15837 for (i = 0; gfc_logical_kinds[i].kind; i++)
15838 if (gfc_logical_kinds[i].kind == sym->ts.kind)
15839 break;
15840 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
15841 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
15842 "%L with non-C_Bool kind in BIND(C) procedure "
15843 "%qs", sym->name, &sym->declared_at,
15844 sym->ns->proc_name->name))
15845 return;
15846 else if (!gfc_logical_kinds[i].c_bool
15847 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
15848 "%qs at %L with non-C_Bool kind in "
15849 "BIND(C) procedure %qs", sym->name,
15850 &sym->declared_at,
15851 sym->attr.function ? sym->name
15852 : sym->ns->proc_name->name))
15853 return;
15854 }
15855
15856 switch (sym->attr.flavor)
15857 {
15858 case FL_VARIABLE:
15859 if (!resolve_fl_variable (sym, mp_flag))
15860 return;
15861 break;
15862
15863 case FL_PROCEDURE:
15864 if (sym->formal && !sym->formal_ns)
15865 {
15866 /* Check that none of the arguments are a namelist. */
15867 gfc_formal_arglist *formal = sym->formal;
15868
15869 for (; formal; formal = formal->next)
15870 if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
15871 {
15872 gfc_error ("Namelist %qs cannot be an argument to "
15873 "subroutine or function at %L",
15874 formal->sym->name, &sym->declared_at);
15875 return;
15876 }
15877 }
15878
15879 if (!resolve_fl_procedure (sym, mp_flag))
15880 return;
15881 break;
15882
15883 case FL_NAMELIST:
15884 if (!resolve_fl_namelist (sym))
15885 return;
15886 break;
15887
15888 case FL_PARAMETER:
15889 if (!resolve_fl_parameter (sym))
15890 return;
15891 break;
15892
15893 default:
15894 break;
15895 }
15896
15897 /* Resolve array specifier. Check as well some constraints
15898 on COMMON blocks. */
15899
15900 check_constant = sym->attr.in_common && !sym->attr.pointer;
15901
15902 /* Set the formal_arg_flag so that check_conflict will not throw
15903 an error for host associated variables in the specification
15904 expression for an array_valued function. */
15905 if ((sym->attr.function || sym->attr.result) && sym->as)
15906 formal_arg_flag = true;
15907
15908 saved_specification_expr = specification_expr;
15909 specification_expr = true;
15910 gfc_resolve_array_spec (sym->as, check_constant);
15911 specification_expr = saved_specification_expr;
15912
15913 formal_arg_flag = false;
15914
15915 /* Resolve formal namespaces. */
15916 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
15917 && !sym->attr.contained && !sym->attr.intrinsic)
15918 gfc_resolve (sym->formal_ns);
15919
15920 /* Make sure the formal namespace is present. */
15921 if (sym->formal && !sym->formal_ns)
15922 {
15923 gfc_formal_arglist *formal = sym->formal;
15924 while (formal && !formal->sym)
15925 formal = formal->next;
15926
15927 if (formal)
15928 {
15929 sym->formal_ns = formal->sym->ns;
15930 if (sym->formal_ns && sym->ns != formal->sym->ns)
15931 sym->formal_ns->refs++;
15932 }
15933 }
15934
15935 /* Check threadprivate restrictions. */
15936 if (sym->attr.threadprivate && !sym->attr.save
15937 && !(sym->ns->save_all && !sym->attr.automatic)
15938 && (!sym->attr.in_common
15939 && sym->module == NULL
15940 && (sym->ns->proc_name == NULL
15941 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
15942 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
15943
15944 /* Check omp declare target restrictions. */
15945 if (sym->attr.omp_declare_target
15946 && sym->attr.flavor == FL_VARIABLE
15947 && !sym->attr.save
15948 && !(sym->ns->save_all && !sym->attr.automatic)
15949 && (!sym->attr.in_common
15950 && sym->module == NULL
15951 && (sym->ns->proc_name == NULL
15952 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
15953 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
15954 sym->name, &sym->declared_at);
15955
15956 /* If we have come this far we can apply default-initializers, as
15957 described in 14.7.5, to those variables that have not already
15958 been assigned one. */
15959 if (sym->ts.type == BT_DERIVED
15960 && !sym->value
15961 && !sym->attr.allocatable
15962 && !sym->attr.alloc_comp)
15963 {
15964 symbol_attribute *a = &sym->attr;
15965
15966 if ((!a->save && !a->dummy && !a->pointer
15967 && !a->in_common && !a->use_assoc
15968 && a->referenced
15969 && !((a->function || a->result)
15970 && (!a->dimension
15971 || sym->ts.u.derived->attr.alloc_comp
15972 || sym->ts.u.derived->attr.pointer_comp))
15973 && !(a->function && sym != sym->result))
15974 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
15975 apply_default_init (sym);
15976 else if (a->function && sym->result && a->access != ACCESS_PRIVATE
15977 && (sym->ts.u.derived->attr.alloc_comp
15978 || sym->ts.u.derived->attr.pointer_comp))
15979 /* Mark the result symbol to be referenced, when it has allocatable
15980 components. */
15981 sym->result->attr.referenced = 1;
15982 }
15983
15984 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
15985 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
15986 && !CLASS_DATA (sym)->attr.class_pointer
15987 && !CLASS_DATA (sym)->attr.allocatable)
15988 apply_default_init (sym);
15989
15990 /* If this symbol has a type-spec, check it. */
15991 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
15992 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
15993 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
15994 return;
15995
15996 if (sym->param_list)
15997 resolve_pdt (sym);
15998 }
15999
16000
16001 /************* Resolve DATA statements *************/
16002
16003 static struct
16004 {
16005 gfc_data_value *vnode;
16006 mpz_t left;
16007 }
16008 values;
16009
16010
16011 /* Advance the values structure to point to the next value in the data list. */
16012
16013 static bool
16014 next_data_value (void)
16015 {
16016 while (mpz_cmp_ui (values.left, 0) == 0)
16017 {
16018
16019 if (values.vnode->next == NULL)
16020 return false;
16021
16022 values.vnode = values.vnode->next;
16023 mpz_set (values.left, values.vnode->repeat);
16024 }
16025
16026 return true;
16027 }
16028
16029
16030 static bool
16031 check_data_variable (gfc_data_variable *var, locus *where)
16032 {
16033 gfc_expr *e;
16034 mpz_t size;
16035 mpz_t offset;
16036 bool t;
16037 ar_type mark = AR_UNKNOWN;
16038 int i;
16039 mpz_t section_index[GFC_MAX_DIMENSIONS];
16040 gfc_ref *ref;
16041 gfc_array_ref *ar;
16042 gfc_symbol *sym;
16043 int has_pointer;
16044
16045 if (!gfc_resolve_expr (var->expr))
16046 return false;
16047
16048 ar = NULL;
16049 mpz_init_set_si (offset, 0);
16050 e = var->expr;
16051
16052 if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
16053 && e->value.function.isym->id == GFC_ISYM_CAF_GET)
16054 e = e->value.function.actual->expr;
16055
16056 if (e->expr_type != EXPR_VARIABLE)
16057 {
16058 gfc_error ("Expecting definable entity near %L", where);
16059 return false;
16060 }
16061
16062 sym = e->symtree->n.sym;
16063
16064 if (sym->ns->is_block_data && !sym->attr.in_common)
16065 {
16066 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
16067 sym->name, &sym->declared_at);
16068 return false;
16069 }
16070
16071 if (e->ref == NULL && sym->as)
16072 {
16073 gfc_error ("DATA array %qs at %L must be specified in a previous"
16074 " declaration", sym->name, where);
16075 return false;
16076 }
16077
16078 if (gfc_is_coindexed (e))
16079 {
16080 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
16081 where);
16082 return false;
16083 }
16084
16085 has_pointer = sym->attr.pointer;
16086
16087 for (ref = e->ref; ref; ref = ref->next)
16088 {
16089 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
16090 has_pointer = 1;
16091
16092 if (has_pointer)
16093 {
16094 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
16095 {
16096 gfc_error ("DATA element %qs at %L is a pointer and so must "
16097 "be a full array", sym->name, where);
16098 return false;
16099 }
16100
16101 if (values.vnode->expr->expr_type == EXPR_CONSTANT)
16102 {
16103 gfc_error ("DATA object near %L has the pointer attribute "
16104 "and the corresponding DATA value is not a valid "
16105 "initial-data-target", where);
16106 return false;
16107 }
16108 }
16109 }
16110
16111 if (e->rank == 0 || has_pointer)
16112 {
16113 mpz_init_set_ui (size, 1);
16114 ref = NULL;
16115 }
16116 else
16117 {
16118 ref = e->ref;
16119
16120 /* Find the array section reference. */
16121 for (ref = e->ref; ref; ref = ref->next)
16122 {
16123 if (ref->type != REF_ARRAY)
16124 continue;
16125 if (ref->u.ar.type == AR_ELEMENT)
16126 continue;
16127 break;
16128 }
16129 gcc_assert (ref);
16130
16131 /* Set marks according to the reference pattern. */
16132 switch (ref->u.ar.type)
16133 {
16134 case AR_FULL:
16135 mark = AR_FULL;
16136 break;
16137
16138 case AR_SECTION:
16139 ar = &ref->u.ar;
16140 /* Get the start position of array section. */
16141 gfc_get_section_index (ar, section_index, &offset);
16142 mark = AR_SECTION;
16143 break;
16144
16145 default:
16146 gcc_unreachable ();
16147 }
16148
16149 if (!gfc_array_size (e, &size))
16150 {
16151 gfc_error ("Nonconstant array section at %L in DATA statement",
16152 where);
16153 mpz_clear (offset);
16154 return false;
16155 }
16156 }
16157
16158 t = true;
16159
16160 while (mpz_cmp_ui (size, 0) > 0)
16161 {
16162 if (!next_data_value ())
16163 {
16164 gfc_error ("DATA statement at %L has more variables than values",
16165 where);
16166 t = false;
16167 break;
16168 }
16169
16170 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
16171 if (!t)
16172 break;
16173
16174 /* If we have more than one element left in the repeat count,
16175 and we have more than one element left in the target variable,
16176 then create a range assignment. */
16177 /* FIXME: Only done for full arrays for now, since array sections
16178 seem tricky. */
16179 if (mark == AR_FULL && ref && ref->next == NULL
16180 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
16181 {
16182 mpz_t range;
16183
16184 if (mpz_cmp (size, values.left) >= 0)
16185 {
16186 mpz_init_set (range, values.left);
16187 mpz_sub (size, size, values.left);
16188 mpz_set_ui (values.left, 0);
16189 }
16190 else
16191 {
16192 mpz_init_set (range, size);
16193 mpz_sub (values.left, values.left, size);
16194 mpz_set_ui (size, 0);
16195 }
16196
16197 t = gfc_assign_data_value (var->expr, values.vnode->expr,
16198 offset, &range);
16199
16200 mpz_add (offset, offset, range);
16201 mpz_clear (range);
16202
16203 if (!t)
16204 break;
16205 }
16206
16207 /* Assign initial value to symbol. */
16208 else
16209 {
16210 mpz_sub_ui (values.left, values.left, 1);
16211 mpz_sub_ui (size, size, 1);
16212
16213 t = gfc_assign_data_value (var->expr, values.vnode->expr,
16214 offset, NULL);
16215 if (!t)
16216 break;
16217
16218 if (mark == AR_FULL)
16219 mpz_add_ui (offset, offset, 1);
16220
16221 /* Modify the array section indexes and recalculate the offset
16222 for next element. */
16223 else if (mark == AR_SECTION)
16224 gfc_advance_section (section_index, ar, &offset);
16225 }
16226 }
16227
16228 if (mark == AR_SECTION)
16229 {
16230 for (i = 0; i < ar->dimen; i++)
16231 mpz_clear (section_index[i]);
16232 }
16233
16234 mpz_clear (size);
16235 mpz_clear (offset);
16236
16237 return t;
16238 }
16239
16240
16241 static bool traverse_data_var (gfc_data_variable *, locus *);
16242
16243 /* Iterate over a list of elements in a DATA statement. */
16244
16245 static bool
16246 traverse_data_list (gfc_data_variable *var, locus *where)
16247 {
16248 mpz_t trip;
16249 iterator_stack frame;
16250 gfc_expr *e, *start, *end, *step;
16251 bool retval = true;
16252
16253 mpz_init (frame.value);
16254 mpz_init (trip);
16255
16256 start = gfc_copy_expr (var->iter.start);
16257 end = gfc_copy_expr (var->iter.end);
16258 step = gfc_copy_expr (var->iter.step);
16259
16260 if (!gfc_simplify_expr (start, 1)
16261 || start->expr_type != EXPR_CONSTANT)
16262 {
16263 gfc_error ("start of implied-do loop at %L could not be "
16264 "simplified to a constant value", &start->where);
16265 retval = false;
16266 goto cleanup;
16267 }
16268 if (!gfc_simplify_expr (end, 1)
16269 || end->expr_type != EXPR_CONSTANT)
16270 {
16271 gfc_error ("end of implied-do loop at %L could not be "
16272 "simplified to a constant value", &start->where);
16273 retval = false;
16274 goto cleanup;
16275 }
16276 if (!gfc_simplify_expr (step, 1)
16277 || step->expr_type != EXPR_CONSTANT)
16278 {
16279 gfc_error ("step of implied-do loop at %L could not be "
16280 "simplified to a constant value", &start->where);
16281 retval = false;
16282 goto cleanup;
16283 }
16284
16285 mpz_set (trip, end->value.integer);
16286 mpz_sub (trip, trip, start->value.integer);
16287 mpz_add (trip, trip, step->value.integer);
16288
16289 mpz_div (trip, trip, step->value.integer);
16290
16291 mpz_set (frame.value, start->value.integer);
16292
16293 frame.prev = iter_stack;
16294 frame.variable = var->iter.var->symtree;
16295 iter_stack = &frame;
16296
16297 while (mpz_cmp_ui (trip, 0) > 0)
16298 {
16299 if (!traverse_data_var (var->list, where))
16300 {
16301 retval = false;
16302 goto cleanup;
16303 }
16304
16305 e = gfc_copy_expr (var->expr);
16306 if (!gfc_simplify_expr (e, 1))
16307 {
16308 gfc_free_expr (e);
16309 retval = false;
16310 goto cleanup;
16311 }
16312
16313 mpz_add (frame.value, frame.value, step->value.integer);
16314
16315 mpz_sub_ui (trip, trip, 1);
16316 }
16317
16318 cleanup:
16319 mpz_clear (frame.value);
16320 mpz_clear (trip);
16321
16322 gfc_free_expr (start);
16323 gfc_free_expr (end);
16324 gfc_free_expr (step);
16325
16326 iter_stack = frame.prev;
16327 return retval;
16328 }
16329
16330
16331 /* Type resolve variables in the variable list of a DATA statement. */
16332
16333 static bool
16334 traverse_data_var (gfc_data_variable *var, locus *where)
16335 {
16336 bool t;
16337
16338 for (; var; var = var->next)
16339 {
16340 if (var->expr == NULL)
16341 t = traverse_data_list (var, where);
16342 else
16343 t = check_data_variable (var, where);
16344
16345 if (!t)
16346 return false;
16347 }
16348
16349 return true;
16350 }
16351
16352
16353 /* Resolve the expressions and iterators associated with a data statement.
16354 This is separate from the assignment checking because data lists should
16355 only be resolved once. */
16356
16357 static bool
16358 resolve_data_variables (gfc_data_variable *d)
16359 {
16360 for (; d; d = d->next)
16361 {
16362 if (d->list == NULL)
16363 {
16364 if (!gfc_resolve_expr (d->expr))
16365 return false;
16366 }
16367 else
16368 {
16369 if (!gfc_resolve_iterator (&d->iter, false, true))
16370 return false;
16371
16372 if (!resolve_data_variables (d->list))
16373 return false;
16374 }
16375 }
16376
16377 return true;
16378 }
16379
16380
16381 /* Resolve a single DATA statement. We implement this by storing a pointer to
16382 the value list into static variables, and then recursively traversing the
16383 variables list, expanding iterators and such. */
16384
16385 static void
16386 resolve_data (gfc_data *d)
16387 {
16388
16389 if (!resolve_data_variables (d->var))
16390 return;
16391
16392 values.vnode = d->value;
16393 if (d->value == NULL)
16394 mpz_set_ui (values.left, 0);
16395 else
16396 mpz_set (values.left, d->value->repeat);
16397
16398 if (!traverse_data_var (d->var, &d->where))
16399 return;
16400
16401 /* At this point, we better not have any values left. */
16402
16403 if (next_data_value ())
16404 gfc_error ("DATA statement at %L has more values than variables",
16405 &d->where);
16406 }
16407
16408
16409 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
16410 accessed by host or use association, is a dummy argument to a pure function,
16411 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
16412 is storage associated with any such variable, shall not be used in the
16413 following contexts: (clients of this function). */
16414
16415 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
16416 procedure. Returns zero if assignment is OK, nonzero if there is a
16417 problem. */
16418 int
16419 gfc_impure_variable (gfc_symbol *sym)
16420 {
16421 gfc_symbol *proc;
16422 gfc_namespace *ns;
16423
16424 if (sym->attr.use_assoc || sym->attr.in_common)
16425 return 1;
16426
16427 /* Check if the symbol's ns is inside the pure procedure. */
16428 for (ns = gfc_current_ns; ns; ns = ns->parent)
16429 {
16430 if (ns == sym->ns)
16431 break;
16432 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
16433 return 1;
16434 }
16435
16436 proc = sym->ns->proc_name;
16437 if (sym->attr.dummy
16438 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
16439 || proc->attr.function))
16440 return 1;
16441
16442 /* TODO: Sort out what can be storage associated, if anything, and include
16443 it here. In principle equivalences should be scanned but it does not
16444 seem to be possible to storage associate an impure variable this way. */
16445 return 0;
16446 }
16447
16448
16449 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
16450 current namespace is inside a pure procedure. */
16451
16452 int
16453 gfc_pure (gfc_symbol *sym)
16454 {
16455 symbol_attribute attr;
16456 gfc_namespace *ns;
16457
16458 if (sym == NULL)
16459 {
16460 /* Check if the current namespace or one of its parents
16461 belongs to a pure procedure. */
16462 for (ns = gfc_current_ns; ns; ns = ns->parent)
16463 {
16464 sym = ns->proc_name;
16465 if (sym == NULL)
16466 return 0;
16467 attr = sym->attr;
16468 if (attr.flavor == FL_PROCEDURE && attr.pure)
16469 return 1;
16470 }
16471 return 0;
16472 }
16473
16474 attr = sym->attr;
16475
16476 return attr.flavor == FL_PROCEDURE && attr.pure;
16477 }
16478
16479
16480 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
16481 checks if the current namespace is implicitly pure. Note that this
16482 function returns false for a PURE procedure. */
16483
16484 int
16485 gfc_implicit_pure (gfc_symbol *sym)
16486 {
16487 gfc_namespace *ns;
16488
16489 if (sym == NULL)
16490 {
16491 /* Check if the current procedure is implicit_pure. Walk up
16492 the procedure list until we find a procedure. */
16493 for (ns = gfc_current_ns; ns; ns = ns->parent)
16494 {
16495 sym = ns->proc_name;
16496 if (sym == NULL)
16497 return 0;
16498
16499 if (sym->attr.flavor == FL_PROCEDURE)
16500 break;
16501 }
16502 }
16503
16504 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
16505 && !sym->attr.pure;
16506 }
16507
16508
16509 void
16510 gfc_unset_implicit_pure (gfc_symbol *sym)
16511 {
16512 gfc_namespace *ns;
16513
16514 if (sym == NULL)
16515 {
16516 /* Check if the current procedure is implicit_pure. Walk up
16517 the procedure list until we find a procedure. */
16518 for (ns = gfc_current_ns; ns; ns = ns->parent)
16519 {
16520 sym = ns->proc_name;
16521 if (sym == NULL)
16522 return;
16523
16524 if (sym->attr.flavor == FL_PROCEDURE)
16525 break;
16526 }
16527 }
16528
16529 if (sym->attr.flavor == FL_PROCEDURE)
16530 sym->attr.implicit_pure = 0;
16531 else
16532 sym->attr.pure = 0;
16533 }
16534
16535
16536 /* Test whether the current procedure is elemental or not. */
16537
16538 int
16539 gfc_elemental (gfc_symbol *sym)
16540 {
16541 symbol_attribute attr;
16542
16543 if (sym == NULL)
16544 sym = gfc_current_ns->proc_name;
16545 if (sym == NULL)
16546 return 0;
16547 attr = sym->attr;
16548
16549 return attr.flavor == FL_PROCEDURE && attr.elemental;
16550 }
16551
16552
16553 /* Warn about unused labels. */
16554
16555 static void
16556 warn_unused_fortran_label (gfc_st_label *label)
16557 {
16558 if (label == NULL)
16559 return;
16560
16561 warn_unused_fortran_label (label->left);
16562
16563 if (label->defined == ST_LABEL_UNKNOWN)
16564 return;
16565
16566 switch (label->referenced)
16567 {
16568 case ST_LABEL_UNKNOWN:
16569 gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
16570 label->value, &label->where);
16571 break;
16572
16573 case ST_LABEL_BAD_TARGET:
16574 gfc_warning (OPT_Wunused_label,
16575 "Label %d at %L defined but cannot be used",
16576 label->value, &label->where);
16577 break;
16578
16579 default:
16580 break;
16581 }
16582
16583 warn_unused_fortran_label (label->right);
16584 }
16585
16586
16587 /* Returns the sequence type of a symbol or sequence. */
16588
16589 static seq_type
16590 sequence_type (gfc_typespec ts)
16591 {
16592 seq_type result;
16593 gfc_component *c;
16594
16595 switch (ts.type)
16596 {
16597 case BT_DERIVED:
16598
16599 if (ts.u.derived->components == NULL)
16600 return SEQ_NONDEFAULT;
16601
16602 result = sequence_type (ts.u.derived->components->ts);
16603 for (c = ts.u.derived->components->next; c; c = c->next)
16604 if (sequence_type (c->ts) != result)
16605 return SEQ_MIXED;
16606
16607 return result;
16608
16609 case BT_CHARACTER:
16610 if (ts.kind != gfc_default_character_kind)
16611 return SEQ_NONDEFAULT;
16612
16613 return SEQ_CHARACTER;
16614
16615 case BT_INTEGER:
16616 if (ts.kind != gfc_default_integer_kind)
16617 return SEQ_NONDEFAULT;
16618
16619 return SEQ_NUMERIC;
16620
16621 case BT_REAL:
16622 if (!(ts.kind == gfc_default_real_kind
16623 || ts.kind == gfc_default_double_kind))
16624 return SEQ_NONDEFAULT;
16625
16626 return SEQ_NUMERIC;
16627
16628 case BT_COMPLEX:
16629 if (ts.kind != gfc_default_complex_kind)
16630 return SEQ_NONDEFAULT;
16631
16632 return SEQ_NUMERIC;
16633
16634 case BT_LOGICAL:
16635 if (ts.kind != gfc_default_logical_kind)
16636 return SEQ_NONDEFAULT;
16637
16638 return SEQ_NUMERIC;
16639
16640 default:
16641 return SEQ_NONDEFAULT;
16642 }
16643 }
16644
16645
16646 /* Resolve derived type EQUIVALENCE object. */
16647
16648 static bool
16649 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
16650 {
16651 gfc_component *c = derived->components;
16652
16653 if (!derived)
16654 return true;
16655
16656 /* Shall not be an object of nonsequence derived type. */
16657 if (!derived->attr.sequence)
16658 {
16659 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
16660 "attribute to be an EQUIVALENCE object", sym->name,
16661 &e->where);
16662 return false;
16663 }
16664
16665 /* Shall not have allocatable components. */
16666 if (derived->attr.alloc_comp)
16667 {
16668 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
16669 "components to be an EQUIVALENCE object",sym->name,
16670 &e->where);
16671 return false;
16672 }
16673
16674 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
16675 {
16676 gfc_error ("Derived type variable %qs at %L with default "
16677 "initialization cannot be in EQUIVALENCE with a variable "
16678 "in COMMON", sym->name, &e->where);
16679 return false;
16680 }
16681
16682 for (; c ; c = c->next)
16683 {
16684 if (gfc_bt_struct (c->ts.type)
16685 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
16686 return false;
16687
16688 /* Shall not be an object of sequence derived type containing a pointer
16689 in the structure. */
16690 if (c->attr.pointer)
16691 {
16692 gfc_error ("Derived type variable %qs at %L with pointer "
16693 "component(s) cannot be an EQUIVALENCE object",
16694 sym->name, &e->where);
16695 return false;
16696 }
16697 }
16698 return true;
16699 }
16700
16701
16702 /* Resolve equivalence object.
16703 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
16704 an allocatable array, an object of nonsequence derived type, an object of
16705 sequence derived type containing a pointer at any level of component
16706 selection, an automatic object, a function name, an entry name, a result
16707 name, a named constant, a structure component, or a subobject of any of
16708 the preceding objects. A substring shall not have length zero. A
16709 derived type shall not have components with default initialization nor
16710 shall two objects of an equivalence group be initialized.
16711 Either all or none of the objects shall have an protected attribute.
16712 The simple constraints are done in symbol.c(check_conflict) and the rest
16713 are implemented here. */
16714
16715 static void
16716 resolve_equivalence (gfc_equiv *eq)
16717 {
16718 gfc_symbol *sym;
16719 gfc_symbol *first_sym;
16720 gfc_expr *e;
16721 gfc_ref *r;
16722 locus *last_where = NULL;
16723 seq_type eq_type, last_eq_type;
16724 gfc_typespec *last_ts;
16725 int object, cnt_protected;
16726 const char *msg;
16727
16728 last_ts = &eq->expr->symtree->n.sym->ts;
16729
16730 first_sym = eq->expr->symtree->n.sym;
16731
16732 cnt_protected = 0;
16733
16734 for (object = 1; eq; eq = eq->eq, object++)
16735 {
16736 e = eq->expr;
16737
16738 e->ts = e->symtree->n.sym->ts;
16739 /* match_varspec might not know yet if it is seeing
16740 array reference or substring reference, as it doesn't
16741 know the types. */
16742 if (e->ref && e->ref->type == REF_ARRAY)
16743 {
16744 gfc_ref *ref = e->ref;
16745 sym = e->symtree->n.sym;
16746
16747 if (sym->attr.dimension)
16748 {
16749 ref->u.ar.as = sym->as;
16750 ref = ref->next;
16751 }
16752
16753 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
16754 if (e->ts.type == BT_CHARACTER
16755 && ref
16756 && ref->type == REF_ARRAY
16757 && ref->u.ar.dimen == 1
16758 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
16759 && ref->u.ar.stride[0] == NULL)
16760 {
16761 gfc_expr *start = ref->u.ar.start[0];
16762 gfc_expr *end = ref->u.ar.end[0];
16763 void *mem = NULL;
16764
16765 /* Optimize away the (:) reference. */
16766 if (start == NULL && end == NULL)
16767 {
16768 if (e->ref == ref)
16769 e->ref = ref->next;
16770 else
16771 e->ref->next = ref->next;
16772 mem = ref;
16773 }
16774 else
16775 {
16776 ref->type = REF_SUBSTRING;
16777 if (start == NULL)
16778 start = gfc_get_int_expr (gfc_charlen_int_kind,
16779 NULL, 1);
16780 ref->u.ss.start = start;
16781 if (end == NULL && e->ts.u.cl)
16782 end = gfc_copy_expr (e->ts.u.cl->length);
16783 ref->u.ss.end = end;
16784 ref->u.ss.length = e->ts.u.cl;
16785 e->ts.u.cl = NULL;
16786 }
16787 ref = ref->next;
16788 free (mem);
16789 }
16790
16791 /* Any further ref is an error. */
16792 if (ref)
16793 {
16794 gcc_assert (ref->type == REF_ARRAY);
16795 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
16796 &ref->u.ar.where);
16797 continue;
16798 }
16799 }
16800
16801 if (!gfc_resolve_expr (e))
16802 continue;
16803
16804 sym = e->symtree->n.sym;
16805
16806 if (sym->attr.is_protected)
16807 cnt_protected++;
16808 if (cnt_protected > 0 && cnt_protected != object)
16809 {
16810 gfc_error ("Either all or none of the objects in the "
16811 "EQUIVALENCE set at %L shall have the "
16812 "PROTECTED attribute",
16813 &e->where);
16814 break;
16815 }
16816
16817 /* Shall not equivalence common block variables in a PURE procedure. */
16818 if (sym->ns->proc_name
16819 && sym->ns->proc_name->attr.pure
16820 && sym->attr.in_common)
16821 {
16822 /* Need to check for symbols that may have entered the pure
16823 procedure via a USE statement. */
16824 bool saw_sym = false;
16825 if (sym->ns->use_stmts)
16826 {
16827 gfc_use_rename *r;
16828 for (r = sym->ns->use_stmts->rename; r; r = r->next)
16829 if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
16830 }
16831 else
16832 saw_sym = true;
16833
16834 if (saw_sym)
16835 gfc_error ("COMMON block member %qs at %L cannot be an "
16836 "EQUIVALENCE object in the pure procedure %qs",
16837 sym->name, &e->where, sym->ns->proc_name->name);
16838 break;
16839 }
16840
16841 /* Shall not be a named constant. */
16842 if (e->expr_type == EXPR_CONSTANT)
16843 {
16844 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
16845 "object", sym->name, &e->where);
16846 continue;
16847 }
16848
16849 if (e->ts.type == BT_DERIVED
16850 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
16851 continue;
16852
16853 /* Check that the types correspond correctly:
16854 Note 5.28:
16855 A numeric sequence structure may be equivalenced to another sequence
16856 structure, an object of default integer type, default real type, double
16857 precision real type, default logical type such that components of the
16858 structure ultimately only become associated to objects of the same
16859 kind. A character sequence structure may be equivalenced to an object
16860 of default character kind or another character sequence structure.
16861 Other objects may be equivalenced only to objects of the same type and
16862 kind parameters. */
16863
16864 /* Identical types are unconditionally OK. */
16865 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
16866 goto identical_types;
16867
16868 last_eq_type = sequence_type (*last_ts);
16869 eq_type = sequence_type (sym->ts);
16870
16871 /* Since the pair of objects is not of the same type, mixed or
16872 non-default sequences can be rejected. */
16873
16874 msg = "Sequence %s with mixed components in EQUIVALENCE "
16875 "statement at %L with different type objects";
16876 if ((object ==2
16877 && last_eq_type == SEQ_MIXED
16878 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
16879 || (eq_type == SEQ_MIXED
16880 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
16881 continue;
16882
16883 msg = "Non-default type object or sequence %s in EQUIVALENCE "
16884 "statement at %L with objects of different type";
16885 if ((object ==2
16886 && last_eq_type == SEQ_NONDEFAULT
16887 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
16888 || (eq_type == SEQ_NONDEFAULT
16889 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
16890 continue;
16891
16892 msg ="Non-CHARACTER object %qs in default CHARACTER "
16893 "EQUIVALENCE statement at %L";
16894 if (last_eq_type == SEQ_CHARACTER
16895 && eq_type != SEQ_CHARACTER
16896 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
16897 continue;
16898
16899 msg ="Non-NUMERIC object %qs in default NUMERIC "
16900 "EQUIVALENCE statement at %L";
16901 if (last_eq_type == SEQ_NUMERIC
16902 && eq_type != SEQ_NUMERIC
16903 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
16904 continue;
16905
16906 identical_types:
16907
16908 last_ts =&sym->ts;
16909 last_where = &e->where;
16910
16911 if (!e->ref)
16912 continue;
16913
16914 /* Shall not be an automatic array. */
16915 if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
16916 {
16917 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
16918 "an EQUIVALENCE object", sym->name, &e->where);
16919 continue;
16920 }
16921
16922 r = e->ref;
16923 while (r)
16924 {
16925 /* Shall not be a structure component. */
16926 if (r->type == REF_COMPONENT)
16927 {
16928 gfc_error ("Structure component %qs at %L cannot be an "
16929 "EQUIVALENCE object",
16930 r->u.c.component->name, &e->where);
16931 break;
16932 }
16933
16934 /* A substring shall not have length zero. */
16935 if (r->type == REF_SUBSTRING)
16936 {
16937 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
16938 {
16939 gfc_error ("Substring at %L has length zero",
16940 &r->u.ss.start->where);
16941 break;
16942 }
16943 }
16944 r = r->next;
16945 }
16946 }
16947 }
16948
16949
16950 /* Function called by resolve_fntype to flag other symbols used in the
16951 length type parameter specification of function results. */
16952
16953 static bool
16954 flag_fn_result_spec (gfc_expr *expr,
16955 gfc_symbol *sym,
16956 int *f ATTRIBUTE_UNUSED)
16957 {
16958 gfc_namespace *ns;
16959 gfc_symbol *s;
16960
16961 if (expr->expr_type == EXPR_VARIABLE)
16962 {
16963 s = expr->symtree->n.sym;
16964 for (ns = s->ns; ns; ns = ns->parent)
16965 if (!ns->parent)
16966 break;
16967
16968 if (sym == s)
16969 {
16970 gfc_error ("Self reference in character length expression "
16971 "for %qs at %L", sym->name, &expr->where);
16972 return true;
16973 }
16974
16975 if (!s->fn_result_spec
16976 && s->attr.flavor == FL_PARAMETER)
16977 {
16978 /* Function contained in a module.... */
16979 if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
16980 {
16981 gfc_symtree *st;
16982 s->fn_result_spec = 1;
16983 /* Make sure that this symbol is translated as a module
16984 variable. */
16985 st = gfc_get_unique_symtree (ns);
16986 st->n.sym = s;
16987 s->refs++;
16988 }
16989 /* ... which is use associated and called. */
16990 else if (s->attr.use_assoc || s->attr.used_in_submodule
16991 ||
16992 /* External function matched with an interface. */
16993 (s->ns->proc_name
16994 && ((s->ns == ns
16995 && s->ns->proc_name->attr.if_source == IFSRC_DECL)
16996 || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
16997 && s->ns->proc_name->attr.function))
16998 s->fn_result_spec = 1;
16999 }
17000 }
17001 return false;
17002 }
17003
17004
17005 /* Resolve function and ENTRY types, issue diagnostics if needed. */
17006
17007 static void
17008 resolve_fntype (gfc_namespace *ns)
17009 {
17010 gfc_entry_list *el;
17011 gfc_symbol *sym;
17012
17013 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
17014 return;
17015
17016 /* If there are any entries, ns->proc_name is the entry master
17017 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
17018 if (ns->entries)
17019 sym = ns->entries->sym;
17020 else
17021 sym = ns->proc_name;
17022 if (sym->result == sym
17023 && sym->ts.type == BT_UNKNOWN
17024 && !gfc_set_default_type (sym, 0, NULL)
17025 && !sym->attr.untyped)
17026 {
17027 gfc_error ("Function %qs at %L has no IMPLICIT type",
17028 sym->name, &sym->declared_at);
17029 sym->attr.untyped = 1;
17030 }
17031
17032 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
17033 && !sym->attr.contained
17034 && !gfc_check_symbol_access (sym->ts.u.derived)
17035 && gfc_check_symbol_access (sym))
17036 {
17037 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
17038 "%L of PRIVATE type %qs", sym->name,
17039 &sym->declared_at, sym->ts.u.derived->name);
17040 }
17041
17042 if (ns->entries)
17043 for (el = ns->entries->next; el; el = el->next)
17044 {
17045 if (el->sym->result == el->sym
17046 && el->sym->ts.type == BT_UNKNOWN
17047 && !gfc_set_default_type (el->sym, 0, NULL)
17048 && !el->sym->attr.untyped)
17049 {
17050 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
17051 el->sym->name, &el->sym->declared_at);
17052 el->sym->attr.untyped = 1;
17053 }
17054 }
17055
17056 if (sym->ts.type == BT_CHARACTER)
17057 gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
17058 }
17059
17060
17061 /* 12.3.2.1.1 Defined operators. */
17062
17063 static bool
17064 check_uop_procedure (gfc_symbol *sym, locus where)
17065 {
17066 gfc_formal_arglist *formal;
17067
17068 if (!sym->attr.function)
17069 {
17070 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
17071 sym->name, &where);
17072 return false;
17073 }
17074
17075 if (sym->ts.type == BT_CHARACTER
17076 && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
17077 && !(sym->result && ((sym->result->ts.u.cl
17078 && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
17079 {
17080 gfc_error ("User operator procedure %qs at %L cannot be assumed "
17081 "character length", sym->name, &where);
17082 return false;
17083 }
17084
17085 formal = gfc_sym_get_dummy_args (sym);
17086 if (!formal || !formal->sym)
17087 {
17088 gfc_error ("User operator procedure %qs at %L must have at least "
17089 "one argument", sym->name, &where);
17090 return false;
17091 }
17092
17093 if (formal->sym->attr.intent != INTENT_IN)
17094 {
17095 gfc_error ("First argument of operator interface at %L must be "
17096 "INTENT(IN)", &where);
17097 return false;
17098 }
17099
17100 if (formal->sym->attr.optional)
17101 {
17102 gfc_error ("First argument of operator interface at %L cannot be "
17103 "optional", &where);
17104 return false;
17105 }
17106
17107 formal = formal->next;
17108 if (!formal || !formal->sym)
17109 return true;
17110
17111 if (formal->sym->attr.intent != INTENT_IN)
17112 {
17113 gfc_error ("Second argument of operator interface at %L must be "
17114 "INTENT(IN)", &where);
17115 return false;
17116 }
17117
17118 if (formal->sym->attr.optional)
17119 {
17120 gfc_error ("Second argument of operator interface at %L cannot be "
17121 "optional", &where);
17122 return false;
17123 }
17124
17125 if (formal->next)
17126 {
17127 gfc_error ("Operator interface at %L must have, at most, two "
17128 "arguments", &where);
17129 return false;
17130 }
17131
17132 return true;
17133 }
17134
17135 static void
17136 gfc_resolve_uops (gfc_symtree *symtree)
17137 {
17138 gfc_interface *itr;
17139
17140 if (symtree == NULL)
17141 return;
17142
17143 gfc_resolve_uops (symtree->left);
17144 gfc_resolve_uops (symtree->right);
17145
17146 for (itr = symtree->n.uop->op; itr; itr = itr->next)
17147 check_uop_procedure (itr->sym, itr->sym->declared_at);
17148 }
17149
17150
17151 /* Examine all of the expressions associated with a program unit,
17152 assign types to all intermediate expressions, make sure that all
17153 assignments are to compatible types and figure out which names
17154 refer to which functions or subroutines. It doesn't check code
17155 block, which is handled by gfc_resolve_code. */
17156
17157 static void
17158 resolve_types (gfc_namespace *ns)
17159 {
17160 gfc_namespace *n;
17161 gfc_charlen *cl;
17162 gfc_data *d;
17163 gfc_equiv *eq;
17164 gfc_namespace* old_ns = gfc_current_ns;
17165 bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
17166
17167 if (ns->types_resolved)
17168 return;
17169
17170 /* Check that all IMPLICIT types are ok. */
17171 if (!ns->seen_implicit_none)
17172 {
17173 unsigned letter;
17174 for (letter = 0; letter != GFC_LETTERS; ++letter)
17175 if (ns->set_flag[letter]
17176 && !resolve_typespec_used (&ns->default_type[letter],
17177 &ns->implicit_loc[letter], NULL))
17178 return;
17179 }
17180
17181 gfc_current_ns = ns;
17182
17183 resolve_entries (ns);
17184
17185 resolve_common_vars (&ns->blank_common, false);
17186 resolve_common_blocks (ns->common_root);
17187
17188 resolve_contained_functions (ns);
17189
17190 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
17191 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
17192 gfc_resolve_formal_arglist (ns->proc_name);
17193
17194 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
17195
17196 for (cl = ns->cl_list; cl; cl = cl->next)
17197 resolve_charlen (cl);
17198
17199 gfc_traverse_ns (ns, resolve_symbol);
17200
17201 resolve_fntype (ns);
17202
17203 for (n = ns->contained; n; n = n->sibling)
17204 {
17205 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
17206 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
17207 "also be PURE", n->proc_name->name,
17208 &n->proc_name->declared_at);
17209
17210 resolve_types (n);
17211 }
17212
17213 forall_flag = 0;
17214 gfc_do_concurrent_flag = 0;
17215 gfc_check_interfaces (ns);
17216
17217 gfc_traverse_ns (ns, resolve_values);
17218
17219 if (ns->save_all || (!flag_automatic && !recursive))
17220 gfc_save_all (ns);
17221
17222 iter_stack = NULL;
17223 for (d = ns->data; d; d = d->next)
17224 resolve_data (d);
17225
17226 iter_stack = NULL;
17227 gfc_traverse_ns (ns, gfc_formalize_init_value);
17228
17229 gfc_traverse_ns (ns, gfc_verify_binding_labels);
17230
17231 for (eq = ns->equiv; eq; eq = eq->next)
17232 resolve_equivalence (eq);
17233
17234 /* Warn about unused labels. */
17235 if (warn_unused_label)
17236 warn_unused_fortran_label (ns->st_labels);
17237
17238 gfc_resolve_uops (ns->uop_root);
17239
17240 gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
17241
17242 gfc_resolve_omp_declare_simd (ns);
17243
17244 gfc_resolve_omp_udrs (ns->omp_udr_root);
17245
17246 ns->types_resolved = 1;
17247
17248 gfc_current_ns = old_ns;
17249 }
17250
17251
17252 /* Call gfc_resolve_code recursively. */
17253
17254 static void
17255 resolve_codes (gfc_namespace *ns)
17256 {
17257 gfc_namespace *n;
17258 bitmap_obstack old_obstack;
17259
17260 if (ns->resolved == 1)
17261 return;
17262
17263 for (n = ns->contained; n; n = n->sibling)
17264 resolve_codes (n);
17265
17266 gfc_current_ns = ns;
17267
17268 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
17269 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
17270 cs_base = NULL;
17271
17272 /* Set to an out of range value. */
17273 current_entry_id = -1;
17274
17275 old_obstack = labels_obstack;
17276 bitmap_obstack_initialize (&labels_obstack);
17277
17278 gfc_resolve_oacc_declare (ns);
17279 gfc_resolve_oacc_routines (ns);
17280 gfc_resolve_omp_local_vars (ns);
17281 gfc_resolve_code (ns->code, ns);
17282
17283 bitmap_obstack_release (&labels_obstack);
17284 labels_obstack = old_obstack;
17285 }
17286
17287
17288 /* This function is called after a complete program unit has been compiled.
17289 Its purpose is to examine all of the expressions associated with a program
17290 unit, assign types to all intermediate expressions, make sure that all
17291 assignments are to compatible types and figure out which names refer to
17292 which functions or subroutines. */
17293
17294 void
17295 gfc_resolve (gfc_namespace *ns)
17296 {
17297 gfc_namespace *old_ns;
17298 code_stack *old_cs_base;
17299 struct gfc_omp_saved_state old_omp_state;
17300
17301 if (ns->resolved)
17302 return;
17303
17304 ns->resolved = -1;
17305 old_ns = gfc_current_ns;
17306 old_cs_base = cs_base;
17307
17308 /* As gfc_resolve can be called during resolution of an OpenMP construct
17309 body, we should clear any state associated to it, so that say NS's
17310 DO loops are not interpreted as OpenMP loops. */
17311 if (!ns->construct_entities)
17312 gfc_omp_save_and_clear_state (&old_omp_state);
17313
17314 resolve_types (ns);
17315 component_assignment_level = 0;
17316 resolve_codes (ns);
17317
17318 gfc_current_ns = old_ns;
17319 cs_base = old_cs_base;
17320 ns->resolved = 1;
17321
17322 gfc_run_passes (ns);
17323
17324 if (!ns->construct_entities)
17325 gfc_omp_restore_state (&old_omp_state);
17326 }