re PR fortran/78300 ([OOP] Failure to compile a F03 code with an optional dummy proce...
[gcc.git] / gcc / fortran / resolve.c
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2016 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 /* Nonzero if we are processing a formal arglist. The corresponding function
76 resets the flag each time that it is read. */
77 static int formal_arg_flag = 0;
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 int
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 static void
268 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 = 1;
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 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 gfc_error ("Argument %qs of statement function at %L must "
516 "be scalar", sym->name, &sym->declared_at);
517 continue;
518 }
519
520 if (sym->ts.type == BT_CHARACTER)
521 {
522 gfc_charlen *cl = sym->ts.u.cl;
523 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
524 {
525 gfc_error ("Character-valued argument %qs of statement "
526 "function at %L must have constant length",
527 sym->name, &sym->declared_at);
528 continue;
529 }
530 }
531 }
532 }
533 formal_arg_flag = 0;
534 }
535
536
537 /* Work function called when searching for symbols that have argument lists
538 associated with them. */
539
540 static void
541 find_arglists (gfc_symbol *sym)
542 {
543 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
544 || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
545 return;
546
547 resolve_formal_arglist (sym);
548 }
549
550
551 /* Given a namespace, resolve all formal argument lists within the namespace.
552 */
553
554 static void
555 resolve_formal_arglists (gfc_namespace *ns)
556 {
557 if (ns == NULL)
558 return;
559
560 gfc_traverse_ns (ns, find_arglists);
561 }
562
563
564 static void
565 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
566 {
567 bool t;
568
569 /* If this namespace is not a function or an entry master function,
570 ignore it. */
571 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
572 || sym->attr.entry_master)
573 return;
574
575 /* Try to find out of what the return type is. */
576 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
577 {
578 t = gfc_set_default_type (sym->result, 0, ns);
579
580 if (!t && !sym->result->attr.untyped)
581 {
582 if (sym->result == sym)
583 gfc_error ("Contained function %qs at %L has no IMPLICIT type",
584 sym->name, &sym->declared_at);
585 else if (!sym->result->attr.proc_pointer)
586 gfc_error ("Result %qs of contained function %qs at %L has "
587 "no IMPLICIT type", sym->result->name, sym->name,
588 &sym->result->declared_at);
589 sym->result->attr.untyped = 1;
590 }
591 }
592
593 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
594 type, lists the only ways a character length value of * can be used:
595 dummy arguments of procedures, named constants, and function results
596 in external functions. Internal function results and results of module
597 procedures are not on this list, ergo, not permitted. */
598
599 if (sym->result->ts.type == BT_CHARACTER)
600 {
601 gfc_charlen *cl = sym->result->ts.u.cl;
602 if ((!cl || !cl->length) && !sym->result->ts.deferred)
603 {
604 /* See if this is a module-procedure and adapt error message
605 accordingly. */
606 bool module_proc;
607 gcc_assert (ns->parent && ns->parent->proc_name);
608 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
609
610 gfc_error ("Character-valued %s %qs at %L must not be"
611 " assumed length",
612 module_proc ? _("module procedure")
613 : _("internal function"),
614 sym->name, &sym->declared_at);
615 }
616 }
617 }
618
619
620 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
621 introduce duplicates. */
622
623 static void
624 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
625 {
626 gfc_formal_arglist *f, *new_arglist;
627 gfc_symbol *new_sym;
628
629 for (; new_args != NULL; new_args = new_args->next)
630 {
631 new_sym = new_args->sym;
632 /* See if this arg is already in the formal argument list. */
633 for (f = proc->formal; f; f = f->next)
634 {
635 if (new_sym == f->sym)
636 break;
637 }
638
639 if (f)
640 continue;
641
642 /* Add a new argument. Argument order is not important. */
643 new_arglist = gfc_get_formal_arglist ();
644 new_arglist->sym = new_sym;
645 new_arglist->next = proc->formal;
646 proc->formal = new_arglist;
647 }
648 }
649
650
651 /* Flag the arguments that are not present in all entries. */
652
653 static void
654 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
655 {
656 gfc_formal_arglist *f, *head;
657 head = new_args;
658
659 for (f = proc->formal; f; f = f->next)
660 {
661 if (f->sym == NULL)
662 continue;
663
664 for (new_args = head; new_args; new_args = new_args->next)
665 {
666 if (new_args->sym == f->sym)
667 break;
668 }
669
670 if (new_args)
671 continue;
672
673 f->sym->attr.not_always_present = 1;
674 }
675 }
676
677
678 /* Resolve alternate entry points. If a symbol has multiple entry points we
679 create a new master symbol for the main routine, and turn the existing
680 symbol into an entry point. */
681
682 static void
683 resolve_entries (gfc_namespace *ns)
684 {
685 gfc_namespace *old_ns;
686 gfc_code *c;
687 gfc_symbol *proc;
688 gfc_entry_list *el;
689 char name[GFC_MAX_SYMBOL_LEN + 1];
690 static int master_count = 0;
691
692 if (ns->proc_name == NULL)
693 return;
694
695 /* No need to do anything if this procedure doesn't have alternate entry
696 points. */
697 if (!ns->entries)
698 return;
699
700 /* We may already have resolved alternate entry points. */
701 if (ns->proc_name->attr.entry_master)
702 return;
703
704 /* If this isn't a procedure something has gone horribly wrong. */
705 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
706
707 /* Remember the current namespace. */
708 old_ns = gfc_current_ns;
709
710 gfc_current_ns = ns;
711
712 /* Add the main entry point to the list of entry points. */
713 el = gfc_get_entry_list ();
714 el->sym = ns->proc_name;
715 el->id = 0;
716 el->next = ns->entries;
717 ns->entries = el;
718 ns->proc_name->attr.entry = 1;
719
720 /* If it is a module function, it needs to be in the right namespace
721 so that gfc_get_fake_result_decl can gather up the results. The
722 need for this arose in get_proc_name, where these beasts were
723 left in their own namespace, to keep prior references linked to
724 the entry declaration.*/
725 if (ns->proc_name->attr.function
726 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
727 el->sym->ns = ns;
728
729 /* Do the same for entries where the master is not a module
730 procedure. These are retained in the module namespace because
731 of the module procedure declaration. */
732 for (el = el->next; el; el = el->next)
733 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
734 && el->sym->attr.mod_proc)
735 el->sym->ns = ns;
736 el = ns->entries;
737
738 /* Add an entry statement for it. */
739 c = gfc_get_code (EXEC_ENTRY);
740 c->ext.entry = el;
741 c->next = ns->code;
742 ns->code = c;
743
744 /* Create a new symbol for the master function. */
745 /* Give the internal function a unique name (within this file).
746 Also include the function name so the user has some hope of figuring
747 out what is going on. */
748 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
749 master_count++, ns->proc_name->name);
750 gfc_get_ha_symbol (name, &proc);
751 gcc_assert (proc != NULL);
752
753 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
754 if (ns->proc_name->attr.subroutine)
755 gfc_add_subroutine (&proc->attr, proc->name, NULL);
756 else
757 {
758 gfc_symbol *sym;
759 gfc_typespec *ts, *fts;
760 gfc_array_spec *as, *fas;
761 gfc_add_function (&proc->attr, proc->name, NULL);
762 proc->result = proc;
763 fas = ns->entries->sym->as;
764 fas = fas ? fas : ns->entries->sym->result->as;
765 fts = &ns->entries->sym->result->ts;
766 if (fts->type == BT_UNKNOWN)
767 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
768 for (el = ns->entries->next; el; el = el->next)
769 {
770 ts = &el->sym->result->ts;
771 as = el->sym->as;
772 as = as ? as : el->sym->result->as;
773 if (ts->type == BT_UNKNOWN)
774 ts = gfc_get_default_type (el->sym->result->name, NULL);
775
776 if (! gfc_compare_types (ts, fts)
777 || (el->sym->result->attr.dimension
778 != ns->entries->sym->result->attr.dimension)
779 || (el->sym->result->attr.pointer
780 != ns->entries->sym->result->attr.pointer))
781 break;
782 else if (as && fas && ns->entries->sym->result != el->sym->result
783 && gfc_compare_array_spec (as, fas) == 0)
784 gfc_error ("Function %s at %L has entries with mismatched "
785 "array specifications", ns->entries->sym->name,
786 &ns->entries->sym->declared_at);
787 /* The characteristics need to match and thus both need to have
788 the same string length, i.e. both len=*, or both len=4.
789 Having both len=<variable> is also possible, but difficult to
790 check at compile time. */
791 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
792 && (((ts->u.cl->length && !fts->u.cl->length)
793 ||(!ts->u.cl->length && fts->u.cl->length))
794 || (ts->u.cl->length
795 && ts->u.cl->length->expr_type
796 != fts->u.cl->length->expr_type)
797 || (ts->u.cl->length
798 && ts->u.cl->length->expr_type == EXPR_CONSTANT
799 && mpz_cmp (ts->u.cl->length->value.integer,
800 fts->u.cl->length->value.integer) != 0)))
801 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
802 "entries returning variables of different "
803 "string lengths", ns->entries->sym->name,
804 &ns->entries->sym->declared_at);
805 }
806
807 if (el == NULL)
808 {
809 sym = ns->entries->sym->result;
810 /* All result types the same. */
811 proc->ts = *fts;
812 if (sym->attr.dimension)
813 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
814 if (sym->attr.pointer)
815 gfc_add_pointer (&proc->attr, NULL);
816 }
817 else
818 {
819 /* Otherwise the result will be passed through a union by
820 reference. */
821 proc->attr.mixed_entry_master = 1;
822 for (el = ns->entries; el; el = el->next)
823 {
824 sym = el->sym->result;
825 if (sym->attr.dimension)
826 {
827 if (el == ns->entries)
828 gfc_error ("FUNCTION result %s can't be an array in "
829 "FUNCTION %s at %L", sym->name,
830 ns->entries->sym->name, &sym->declared_at);
831 else
832 gfc_error ("ENTRY result %s can't be an array in "
833 "FUNCTION %s at %L", sym->name,
834 ns->entries->sym->name, &sym->declared_at);
835 }
836 else if (sym->attr.pointer)
837 {
838 if (el == ns->entries)
839 gfc_error ("FUNCTION result %s can't be a POINTER in "
840 "FUNCTION %s at %L", sym->name,
841 ns->entries->sym->name, &sym->declared_at);
842 else
843 gfc_error ("ENTRY result %s can't be a POINTER in "
844 "FUNCTION %s at %L", sym->name,
845 ns->entries->sym->name, &sym->declared_at);
846 }
847 else
848 {
849 ts = &sym->ts;
850 if (ts->type == BT_UNKNOWN)
851 ts = gfc_get_default_type (sym->name, NULL);
852 switch (ts->type)
853 {
854 case BT_INTEGER:
855 if (ts->kind == gfc_default_integer_kind)
856 sym = NULL;
857 break;
858 case BT_REAL:
859 if (ts->kind == gfc_default_real_kind
860 || ts->kind == gfc_default_double_kind)
861 sym = NULL;
862 break;
863 case BT_COMPLEX:
864 if (ts->kind == gfc_default_complex_kind)
865 sym = NULL;
866 break;
867 case BT_LOGICAL:
868 if (ts->kind == gfc_default_logical_kind)
869 sym = NULL;
870 break;
871 case BT_UNKNOWN:
872 /* We will issue error elsewhere. */
873 sym = NULL;
874 break;
875 default:
876 break;
877 }
878 if (sym)
879 {
880 if (el == ns->entries)
881 gfc_error ("FUNCTION result %s can't be of type %s "
882 "in FUNCTION %s at %L", sym->name,
883 gfc_typename (ts), ns->entries->sym->name,
884 &sym->declared_at);
885 else
886 gfc_error ("ENTRY result %s can't be of type %s "
887 "in FUNCTION %s at %L", sym->name,
888 gfc_typename (ts), ns->entries->sym->name,
889 &sym->declared_at);
890 }
891 }
892 }
893 }
894 }
895 proc->attr.access = ACCESS_PRIVATE;
896 proc->attr.entry_master = 1;
897
898 /* Merge all the entry point arguments. */
899 for (el = ns->entries; el; el = el->next)
900 merge_argument_lists (proc, el->sym->formal);
901
902 /* Check the master formal arguments for any that are not
903 present in all entry points. */
904 for (el = ns->entries; el; el = el->next)
905 check_argument_lists (proc, el->sym->formal);
906
907 /* Use the master function for the function body. */
908 ns->proc_name = proc;
909
910 /* Finalize the new symbols. */
911 gfc_commit_symbols ();
912
913 /* Restore the original namespace. */
914 gfc_current_ns = old_ns;
915 }
916
917
918 /* Resolve common variables. */
919 static void
920 resolve_common_vars (gfc_common_head *common_block, bool named_common)
921 {
922 gfc_symbol *csym = common_block->head;
923
924 for (; csym; csym = csym->common_next)
925 {
926 /* gfc_add_in_common may have been called before, but the reported errors
927 have been ignored to continue parsing.
928 We do the checks again here. */
929 if (!csym->attr.use_assoc)
930 gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
931
932 if (csym->value || csym->attr.data)
933 {
934 if (!csym->ns->is_block_data)
935 gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
936 "but only in BLOCK DATA initialization is "
937 "allowed", csym->name, &csym->declared_at);
938 else if (!named_common)
939 gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
940 "in a blank COMMON but initialization is only "
941 "allowed in named common blocks", csym->name,
942 &csym->declared_at);
943 }
944
945 if (UNLIMITED_POLY (csym))
946 gfc_error_now ("%qs in cannot appear in COMMON at %L "
947 "[F2008:C5100]", csym->name, &csym->declared_at);
948
949 if (csym->ts.type != BT_DERIVED)
950 continue;
951
952 if (!(csym->ts.u.derived->attr.sequence
953 || csym->ts.u.derived->attr.is_bind_c))
954 gfc_error_now ("Derived type variable %qs in COMMON at %L "
955 "has neither the SEQUENCE nor the BIND(C) "
956 "attribute", csym->name, &csym->declared_at);
957 if (csym->ts.u.derived->attr.alloc_comp)
958 gfc_error_now ("Derived type variable %qs in COMMON at %L "
959 "has an ultimate component that is "
960 "allocatable", csym->name, &csym->declared_at);
961 if (gfc_has_default_initializer (csym->ts.u.derived))
962 gfc_error_now ("Derived type variable %qs in COMMON at %L "
963 "may not have default initializer", csym->name,
964 &csym->declared_at);
965
966 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
967 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
968 }
969 }
970
971 /* Resolve common blocks. */
972 static void
973 resolve_common_blocks (gfc_symtree *common_root)
974 {
975 gfc_symbol *sym;
976 gfc_gsymbol * gsym;
977
978 if (common_root == NULL)
979 return;
980
981 if (common_root->left)
982 resolve_common_blocks (common_root->left);
983 if (common_root->right)
984 resolve_common_blocks (common_root->right);
985
986 resolve_common_vars (common_root->n.common, true);
987
988 /* The common name is a global name - in Fortran 2003 also if it has a
989 C binding name, since Fortran 2008 only the C binding name is a global
990 identifier. */
991 if (!common_root->n.common->binding_label
992 || gfc_notification_std (GFC_STD_F2008))
993 {
994 gsym = gfc_find_gsymbol (gfc_gsym_root,
995 common_root->n.common->name);
996
997 if (gsym && gfc_notification_std (GFC_STD_F2008)
998 && gsym->type == GSYM_COMMON
999 && ((common_root->n.common->binding_label
1000 && (!gsym->binding_label
1001 || strcmp (common_root->n.common->binding_label,
1002 gsym->binding_label) != 0))
1003 || (!common_root->n.common->binding_label
1004 && gsym->binding_label)))
1005 {
1006 gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
1007 "identifier and must thus have the same binding name "
1008 "as the same-named COMMON block at %L: %s vs %s",
1009 common_root->n.common->name, &common_root->n.common->where,
1010 &gsym->where,
1011 common_root->n.common->binding_label
1012 ? common_root->n.common->binding_label : "(blank)",
1013 gsym->binding_label ? gsym->binding_label : "(blank)");
1014 return;
1015 }
1016
1017 if (gsym && gsym->type != GSYM_COMMON
1018 && !common_root->n.common->binding_label)
1019 {
1020 gfc_error ("COMMON block %qs at %L uses the same global identifier "
1021 "as entity at %L",
1022 common_root->n.common->name, &common_root->n.common->where,
1023 &gsym->where);
1024 return;
1025 }
1026 if (gsym && gsym->type != GSYM_COMMON)
1027 {
1028 gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
1029 "%L sharing the identifier with global non-COMMON-block "
1030 "entity at %L", common_root->n.common->name,
1031 &common_root->n.common->where, &gsym->where);
1032 return;
1033 }
1034 if (!gsym)
1035 {
1036 gsym = gfc_get_gsymbol (common_root->n.common->name);
1037 gsym->type = GSYM_COMMON;
1038 gsym->where = common_root->n.common->where;
1039 gsym->defined = 1;
1040 }
1041 gsym->used = 1;
1042 }
1043
1044 if (common_root->n.common->binding_label)
1045 {
1046 gsym = gfc_find_gsymbol (gfc_gsym_root,
1047 common_root->n.common->binding_label);
1048 if (gsym && gsym->type != GSYM_COMMON)
1049 {
1050 gfc_error ("COMMON block at %L with binding label %s uses the same "
1051 "global identifier as entity at %L",
1052 &common_root->n.common->where,
1053 common_root->n.common->binding_label, &gsym->where);
1054 return;
1055 }
1056 if (!gsym)
1057 {
1058 gsym = gfc_get_gsymbol (common_root->n.common->binding_label);
1059 gsym->type = GSYM_COMMON;
1060 gsym->where = common_root->n.common->where;
1061 gsym->defined = 1;
1062 }
1063 gsym->used = 1;
1064 }
1065
1066 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1067 if (sym == NULL)
1068 return;
1069
1070 if (sym->attr.flavor == FL_PARAMETER)
1071 gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
1072 sym->name, &common_root->n.common->where, &sym->declared_at);
1073
1074 if (sym->attr.external)
1075 gfc_error ("COMMON block %qs at %L can not have the EXTERNAL attribute",
1076 sym->name, &common_root->n.common->where);
1077
1078 if (sym->attr.intrinsic)
1079 gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
1080 sym->name, &common_root->n.common->where);
1081 else if (sym->attr.result
1082 || gfc_is_function_return_value (sym, gfc_current_ns))
1083 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1084 "that is also a function result", sym->name,
1085 &common_root->n.common->where);
1086 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1087 && sym->attr.proc != PROC_ST_FUNCTION)
1088 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1089 "that is also a global procedure", sym->name,
1090 &common_root->n.common->where);
1091 }
1092
1093
1094 /* Resolve contained function types. Because contained functions can call one
1095 another, they have to be worked out before any of the contained procedures
1096 can be resolved.
1097
1098 The good news is that if a function doesn't already have a type, the only
1099 way it can get one is through an IMPLICIT type or a RESULT variable, because
1100 by definition contained functions are contained namespace they're contained
1101 in, not in a sibling or parent namespace. */
1102
1103 static void
1104 resolve_contained_functions (gfc_namespace *ns)
1105 {
1106 gfc_namespace *child;
1107 gfc_entry_list *el;
1108
1109 resolve_formal_arglists (ns);
1110
1111 for (child = ns->contained; child; child = child->sibling)
1112 {
1113 /* Resolve alternate entry points first. */
1114 resolve_entries (child);
1115
1116 /* Then check function return types. */
1117 resolve_contained_fntype (child->proc_name, child);
1118 for (el = child->entries; el; el = el->next)
1119 resolve_contained_fntype (el->sym, child);
1120 }
1121 }
1122
1123
1124 static bool resolve_fl_derived0 (gfc_symbol *sym);
1125 static bool resolve_fl_struct (gfc_symbol *sym);
1126
1127
1128 /* Resolve all of the elements of a structure constructor and make sure that
1129 the types are correct. The 'init' flag indicates that the given
1130 constructor is an initializer. */
1131
1132 static bool
1133 resolve_structure_cons (gfc_expr *expr, int init)
1134 {
1135 gfc_constructor *cons;
1136 gfc_component *comp;
1137 bool t;
1138 symbol_attribute a;
1139
1140 t = true;
1141
1142 if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
1143 {
1144 if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
1145 resolve_fl_derived0 (expr->ts.u.derived);
1146 else
1147 resolve_fl_struct (expr->ts.u.derived);
1148 }
1149
1150 cons = gfc_constructor_first (expr->value.constructor);
1151
1152 /* A constructor may have references if it is the result of substituting a
1153 parameter variable. In this case we just pull out the component we
1154 want. */
1155 if (expr->ref)
1156 comp = expr->ref->u.c.sym->components;
1157 else
1158 comp = expr->ts.u.derived->components;
1159
1160 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1161 {
1162 int rank;
1163
1164 if (!cons->expr)
1165 continue;
1166
1167 /* Unions use an EXPR_NULL contrived expression to tell the translation
1168 phase to generate an initializer of the appropriate length.
1169 Ignore it here. */
1170 if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
1171 continue;
1172
1173 if (!gfc_resolve_expr (cons->expr))
1174 {
1175 t = false;
1176 continue;
1177 }
1178
1179 rank = comp->as ? comp->as->rank : 0;
1180 if (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->as)
1181 rank = CLASS_DATA (comp)->as->rank;
1182
1183 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1184 && (comp->attr.allocatable || cons->expr->rank))
1185 {
1186 gfc_error ("The rank of the element in the structure "
1187 "constructor at %L does not match that of the "
1188 "component (%d/%d)", &cons->expr->where,
1189 cons->expr->rank, rank);
1190 t = false;
1191 }
1192
1193 /* If we don't have the right type, try to convert it. */
1194
1195 if (!comp->attr.proc_pointer &&
1196 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1197 {
1198 if (strcmp (comp->name, "_extends") == 0)
1199 {
1200 /* Can afford to be brutal with the _extends initializer.
1201 The derived type can get lost because it is PRIVATE
1202 but it is not usage constrained by the standard. */
1203 cons->expr->ts = comp->ts;
1204 }
1205 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1206 {
1207 gfc_error ("The element in the structure constructor at %L, "
1208 "for pointer component %qs, is %s but should be %s",
1209 &cons->expr->where, comp->name,
1210 gfc_basic_typename (cons->expr->ts.type),
1211 gfc_basic_typename (comp->ts.type));
1212 t = false;
1213 }
1214 else
1215 {
1216 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1217 if (t)
1218 t = t2;
1219 }
1220 }
1221
1222 /* For strings, the length of the constructor should be the same as
1223 the one of the structure, ensure this if the lengths are known at
1224 compile time and when we are dealing with PARAMETER or structure
1225 constructors. */
1226 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1227 && comp->ts.u.cl->length
1228 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1229 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1230 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1231 && cons->expr->rank != 0
1232 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1233 comp->ts.u.cl->length->value.integer) != 0)
1234 {
1235 if (cons->expr->expr_type == EXPR_VARIABLE
1236 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1237 {
1238 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1239 to make use of the gfc_resolve_character_array_constructor
1240 machinery. The expression is later simplified away to
1241 an array of string literals. */
1242 gfc_expr *para = cons->expr;
1243 cons->expr = gfc_get_expr ();
1244 cons->expr->ts = para->ts;
1245 cons->expr->where = para->where;
1246 cons->expr->expr_type = EXPR_ARRAY;
1247 cons->expr->rank = para->rank;
1248 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1249 gfc_constructor_append_expr (&cons->expr->value.constructor,
1250 para, &cons->expr->where);
1251 }
1252 if (cons->expr->expr_type == EXPR_ARRAY)
1253 {
1254 gfc_constructor *p;
1255 p = gfc_constructor_first (cons->expr->value.constructor);
1256 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1257 {
1258 gfc_charlen *cl, *cl2;
1259
1260 cl2 = NULL;
1261 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1262 {
1263 if (cl == cons->expr->ts.u.cl)
1264 break;
1265 cl2 = cl;
1266 }
1267
1268 gcc_assert (cl);
1269
1270 if (cl2)
1271 cl2->next = cl->next;
1272
1273 gfc_free_expr (cl->length);
1274 free (cl);
1275 }
1276
1277 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1278 cons->expr->ts.u.cl->length_from_typespec = true;
1279 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1280 gfc_resolve_character_array_constructor (cons->expr);
1281 }
1282 }
1283
1284 if (cons->expr->expr_type == EXPR_NULL
1285 && !(comp->attr.pointer || comp->attr.allocatable
1286 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1287 || (comp->ts.type == BT_CLASS
1288 && (CLASS_DATA (comp)->attr.class_pointer
1289 || CLASS_DATA (comp)->attr.allocatable))))
1290 {
1291 t = false;
1292 gfc_error ("The NULL in the structure constructor at %L is "
1293 "being applied to component %qs, which is neither "
1294 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1295 comp->name);
1296 }
1297
1298 if (comp->attr.proc_pointer && comp->ts.interface)
1299 {
1300 /* Check procedure pointer interface. */
1301 gfc_symbol *s2 = NULL;
1302 gfc_component *c2;
1303 const char *name;
1304 char err[200];
1305
1306 c2 = gfc_get_proc_ptr_comp (cons->expr);
1307 if (c2)
1308 {
1309 s2 = c2->ts.interface;
1310 name = c2->name;
1311 }
1312 else if (cons->expr->expr_type == EXPR_FUNCTION)
1313 {
1314 s2 = cons->expr->symtree->n.sym->result;
1315 name = cons->expr->symtree->n.sym->result->name;
1316 }
1317 else if (cons->expr->expr_type != EXPR_NULL)
1318 {
1319 s2 = cons->expr->symtree->n.sym;
1320 name = cons->expr->symtree->n.sym->name;
1321 }
1322
1323 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1324 err, sizeof (err), NULL, NULL))
1325 {
1326 gfc_error (OPT_Wargument_mismatch,
1327 "Interface mismatch for procedure-pointer component "
1328 "%qs in structure constructor at %L: %s",
1329 comp->name, &cons->expr->where, err);
1330 return false;
1331 }
1332 }
1333
1334 if (!comp->attr.pointer || comp->attr.proc_pointer
1335 || cons->expr->expr_type == EXPR_NULL)
1336 continue;
1337
1338 a = gfc_expr_attr (cons->expr);
1339
1340 if (!a.pointer && !a.target)
1341 {
1342 t = false;
1343 gfc_error ("The element in the structure constructor at %L, "
1344 "for pointer component %qs should be a POINTER or "
1345 "a TARGET", &cons->expr->where, comp->name);
1346 }
1347
1348 if (init)
1349 {
1350 /* F08:C461. Additional checks for pointer initialization. */
1351 if (a.allocatable)
1352 {
1353 t = false;
1354 gfc_error ("Pointer initialization target at %L "
1355 "must not be ALLOCATABLE ", &cons->expr->where);
1356 }
1357 if (!a.save)
1358 {
1359 t = false;
1360 gfc_error ("Pointer initialization target at %L "
1361 "must have the SAVE attribute", &cons->expr->where);
1362 }
1363 }
1364
1365 /* F2003, C1272 (3). */
1366 bool impure = cons->expr->expr_type == EXPR_VARIABLE
1367 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1368 || gfc_is_coindexed (cons->expr));
1369 if (impure && gfc_pure (NULL))
1370 {
1371 t = false;
1372 gfc_error ("Invalid expression in the structure constructor for "
1373 "pointer component %qs at %L in PURE procedure",
1374 comp->name, &cons->expr->where);
1375 }
1376
1377 if (impure)
1378 gfc_unset_implicit_pure (NULL);
1379 }
1380
1381 return t;
1382 }
1383
1384
1385 /****************** Expression name resolution ******************/
1386
1387 /* Returns 0 if a symbol was not declared with a type or
1388 attribute declaration statement, nonzero otherwise. */
1389
1390 static int
1391 was_declared (gfc_symbol *sym)
1392 {
1393 symbol_attribute a;
1394
1395 a = sym->attr;
1396
1397 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1398 return 1;
1399
1400 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1401 || a.optional || a.pointer || a.save || a.target || a.volatile_
1402 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1403 || a.asynchronous || a.codimension)
1404 return 1;
1405
1406 return 0;
1407 }
1408
1409
1410 /* Determine if a symbol is generic or not. */
1411
1412 static int
1413 generic_sym (gfc_symbol *sym)
1414 {
1415 gfc_symbol *s;
1416
1417 if (sym->attr.generic ||
1418 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1419 return 1;
1420
1421 if (was_declared (sym) || sym->ns->parent == NULL)
1422 return 0;
1423
1424 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1425
1426 if (s != NULL)
1427 {
1428 if (s == sym)
1429 return 0;
1430 else
1431 return generic_sym (s);
1432 }
1433
1434 return 0;
1435 }
1436
1437
1438 /* Determine if a symbol is specific or not. */
1439
1440 static int
1441 specific_sym (gfc_symbol *sym)
1442 {
1443 gfc_symbol *s;
1444
1445 if (sym->attr.if_source == IFSRC_IFBODY
1446 || sym->attr.proc == PROC_MODULE
1447 || sym->attr.proc == PROC_INTERNAL
1448 || sym->attr.proc == PROC_ST_FUNCTION
1449 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1450 || sym->attr.external)
1451 return 1;
1452
1453 if (was_declared (sym) || sym->ns->parent == NULL)
1454 return 0;
1455
1456 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1457
1458 return (s == NULL) ? 0 : specific_sym (s);
1459 }
1460
1461
1462 /* Figure out if the procedure is specific, generic or unknown. */
1463
1464 enum proc_type
1465 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
1466
1467 static proc_type
1468 procedure_kind (gfc_symbol *sym)
1469 {
1470 if (generic_sym (sym))
1471 return PTYPE_GENERIC;
1472
1473 if (specific_sym (sym))
1474 return PTYPE_SPECIFIC;
1475
1476 return PTYPE_UNKNOWN;
1477 }
1478
1479 /* Check references to assumed size arrays. The flag need_full_assumed_size
1480 is nonzero when matching actual arguments. */
1481
1482 static int need_full_assumed_size = 0;
1483
1484 static bool
1485 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1486 {
1487 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1488 return false;
1489
1490 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1491 What should it be? */
1492 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1493 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1494 && (e->ref->u.ar.type == AR_FULL))
1495 {
1496 gfc_error ("The upper bound in the last dimension must "
1497 "appear in the reference to the assumed size "
1498 "array %qs at %L", sym->name, &e->where);
1499 return true;
1500 }
1501 return false;
1502 }
1503
1504
1505 /* Look for bad assumed size array references in argument expressions
1506 of elemental and array valued intrinsic procedures. Since this is
1507 called from procedure resolution functions, it only recurses at
1508 operators. */
1509
1510 static bool
1511 resolve_assumed_size_actual (gfc_expr *e)
1512 {
1513 if (e == NULL)
1514 return false;
1515
1516 switch (e->expr_type)
1517 {
1518 case EXPR_VARIABLE:
1519 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1520 return true;
1521 break;
1522
1523 case EXPR_OP:
1524 if (resolve_assumed_size_actual (e->value.op.op1)
1525 || resolve_assumed_size_actual (e->value.op.op2))
1526 return true;
1527 break;
1528
1529 default:
1530 break;
1531 }
1532 return false;
1533 }
1534
1535
1536 /* Check a generic procedure, passed as an actual argument, to see if
1537 there is a matching specific name. If none, it is an error, and if
1538 more than one, the reference is ambiguous. */
1539 static int
1540 count_specific_procs (gfc_expr *e)
1541 {
1542 int n;
1543 gfc_interface *p;
1544 gfc_symbol *sym;
1545
1546 n = 0;
1547 sym = e->symtree->n.sym;
1548
1549 for (p = sym->generic; p; p = p->next)
1550 if (strcmp (sym->name, p->sym->name) == 0)
1551 {
1552 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1553 sym->name);
1554 n++;
1555 }
1556
1557 if (n > 1)
1558 gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
1559 &e->where);
1560
1561 if (n == 0)
1562 gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1563 "argument at %L", sym->name, &e->where);
1564
1565 return n;
1566 }
1567
1568
1569 /* See if a call to sym could possibly be a not allowed RECURSION because of
1570 a missing RECURSIVE declaration. This means that either sym is the current
1571 context itself, or sym is the parent of a contained procedure calling its
1572 non-RECURSIVE containing procedure.
1573 This also works if sym is an ENTRY. */
1574
1575 static bool
1576 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1577 {
1578 gfc_symbol* proc_sym;
1579 gfc_symbol* context_proc;
1580 gfc_namespace* real_context;
1581
1582 if (sym->attr.flavor == FL_PROGRAM
1583 || gfc_fl_struct (sym->attr.flavor))
1584 return false;
1585
1586 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1587
1588 /* If we've got an ENTRY, find real procedure. */
1589 if (sym->attr.entry && sym->ns->entries)
1590 proc_sym = sym->ns->entries->sym;
1591 else
1592 proc_sym = sym;
1593
1594 /* If sym is RECURSIVE, all is well of course. */
1595 if (proc_sym->attr.recursive || flag_recursive)
1596 return false;
1597
1598 /* Find the context procedure's "real" symbol if it has entries.
1599 We look for a procedure symbol, so recurse on the parents if we don't
1600 find one (like in case of a BLOCK construct). */
1601 for (real_context = context; ; real_context = real_context->parent)
1602 {
1603 /* We should find something, eventually! */
1604 gcc_assert (real_context);
1605
1606 context_proc = (real_context->entries ? real_context->entries->sym
1607 : real_context->proc_name);
1608
1609 /* In some special cases, there may not be a proc_name, like for this
1610 invalid code:
1611 real(bad_kind()) function foo () ...
1612 when checking the call to bad_kind ().
1613 In these cases, we simply return here and assume that the
1614 call is ok. */
1615 if (!context_proc)
1616 return false;
1617
1618 if (context_proc->attr.flavor != FL_LABEL)
1619 break;
1620 }
1621
1622 /* A call from sym's body to itself is recursion, of course. */
1623 if (context_proc == proc_sym)
1624 return true;
1625
1626 /* The same is true if context is a contained procedure and sym the
1627 containing one. */
1628 if (context_proc->attr.contained)
1629 {
1630 gfc_symbol* parent_proc;
1631
1632 gcc_assert (context->parent);
1633 parent_proc = (context->parent->entries ? context->parent->entries->sym
1634 : context->parent->proc_name);
1635
1636 if (parent_proc == proc_sym)
1637 return true;
1638 }
1639
1640 return false;
1641 }
1642
1643
1644 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1645 its typespec and formal argument list. */
1646
1647 bool
1648 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1649 {
1650 gfc_intrinsic_sym* isym = NULL;
1651 const char* symstd;
1652
1653 if (sym->formal)
1654 return true;
1655
1656 /* Already resolved. */
1657 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1658 return true;
1659
1660 /* We already know this one is an intrinsic, so we don't call
1661 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1662 gfc_find_subroutine directly to check whether it is a function or
1663 subroutine. */
1664
1665 if (sym->intmod_sym_id && sym->attr.subroutine)
1666 {
1667 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1668 isym = gfc_intrinsic_subroutine_by_id (id);
1669 }
1670 else if (sym->intmod_sym_id)
1671 {
1672 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1673 isym = gfc_intrinsic_function_by_id (id);
1674 }
1675 else if (!sym->attr.subroutine)
1676 isym = gfc_find_function (sym->name);
1677
1678 if (isym && !sym->attr.subroutine)
1679 {
1680 if (sym->ts.type != BT_UNKNOWN && warn_surprising
1681 && !sym->attr.implicit_type)
1682 gfc_warning (OPT_Wsurprising,
1683 "Type specified for intrinsic function %qs at %L is"
1684 " ignored", sym->name, &sym->declared_at);
1685
1686 if (!sym->attr.function &&
1687 !gfc_add_function(&sym->attr, sym->name, loc))
1688 return false;
1689
1690 sym->ts = isym->ts;
1691 }
1692 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1693 {
1694 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1695 {
1696 gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1697 " specifier", sym->name, &sym->declared_at);
1698 return false;
1699 }
1700
1701 if (!sym->attr.subroutine &&
1702 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1703 return false;
1704 }
1705 else
1706 {
1707 gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
1708 &sym->declared_at);
1709 return false;
1710 }
1711
1712 gfc_copy_formal_args_intr (sym, isym, NULL);
1713
1714 sym->attr.pure = isym->pure;
1715 sym->attr.elemental = isym->elemental;
1716
1717 /* Check it is actually available in the standard settings. */
1718 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1719 {
1720 gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1721 "available in the current standard settings but %s. Use "
1722 "an appropriate %<-std=*%> option or enable "
1723 "%<-fall-intrinsics%> in order to use it.",
1724 sym->name, &sym->declared_at, symstd);
1725 return false;
1726 }
1727
1728 return true;
1729 }
1730
1731
1732 /* Resolve a procedure expression, like passing it to a called procedure or as
1733 RHS for a procedure pointer assignment. */
1734
1735 static bool
1736 resolve_procedure_expression (gfc_expr* expr)
1737 {
1738 gfc_symbol* sym;
1739
1740 if (expr->expr_type != EXPR_VARIABLE)
1741 return true;
1742 gcc_assert (expr->symtree);
1743
1744 sym = expr->symtree->n.sym;
1745
1746 if (sym->attr.intrinsic)
1747 gfc_resolve_intrinsic (sym, &expr->where);
1748
1749 if (sym->attr.flavor != FL_PROCEDURE
1750 || (sym->attr.function && sym->result == sym))
1751 return true;
1752
1753 /* A non-RECURSIVE procedure that is used as procedure expression within its
1754 own body is in danger of being called recursively. */
1755 if (is_illegal_recursion (sym, gfc_current_ns))
1756 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
1757 " itself recursively. Declare it RECURSIVE or use"
1758 " %<-frecursive%>", sym->name, &expr->where);
1759
1760 return true;
1761 }
1762
1763
1764 /* Resolve an actual argument list. Most of the time, this is just
1765 resolving the expressions in the list.
1766 The exception is that we sometimes have to decide whether arguments
1767 that look like procedure arguments are really simple variable
1768 references. */
1769
1770 static bool
1771 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1772 bool no_formal_args)
1773 {
1774 gfc_symbol *sym;
1775 gfc_symtree *parent_st;
1776 gfc_expr *e;
1777 gfc_component *comp;
1778 int save_need_full_assumed_size;
1779 bool return_value = false;
1780 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1781
1782 actual_arg = true;
1783 first_actual_arg = true;
1784
1785 for (; arg; arg = arg->next)
1786 {
1787 e = arg->expr;
1788 if (e == NULL)
1789 {
1790 /* Check the label is a valid branching target. */
1791 if (arg->label)
1792 {
1793 if (arg->label->defined == ST_LABEL_UNKNOWN)
1794 {
1795 gfc_error ("Label %d referenced at %L is never defined",
1796 arg->label->value, &arg->label->where);
1797 goto cleanup;
1798 }
1799 }
1800 first_actual_arg = false;
1801 continue;
1802 }
1803
1804 if (e->expr_type == EXPR_VARIABLE
1805 && e->symtree->n.sym->attr.generic
1806 && no_formal_args
1807 && count_specific_procs (e) != 1)
1808 goto cleanup;
1809
1810 if (e->ts.type != BT_PROCEDURE)
1811 {
1812 save_need_full_assumed_size = need_full_assumed_size;
1813 if (e->expr_type != EXPR_VARIABLE)
1814 need_full_assumed_size = 0;
1815 if (!gfc_resolve_expr (e))
1816 goto cleanup;
1817 need_full_assumed_size = save_need_full_assumed_size;
1818 goto argument_list;
1819 }
1820
1821 /* See if the expression node should really be a variable reference. */
1822
1823 sym = e->symtree->n.sym;
1824
1825 if (sym->attr.flavor == FL_PROCEDURE
1826 || sym->attr.intrinsic
1827 || sym->attr.external)
1828 {
1829 int actual_ok;
1830
1831 /* If a procedure is not already determined to be something else
1832 check if it is intrinsic. */
1833 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1834 sym->attr.intrinsic = 1;
1835
1836 if (sym->attr.proc == PROC_ST_FUNCTION)
1837 {
1838 gfc_error ("Statement function %qs at %L is not allowed as an "
1839 "actual argument", sym->name, &e->where);
1840 }
1841
1842 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1843 sym->attr.subroutine);
1844 if (sym->attr.intrinsic && actual_ok == 0)
1845 {
1846 gfc_error ("Intrinsic %qs at %L is not allowed as an "
1847 "actual argument", sym->name, &e->where);
1848 }
1849
1850 if (sym->attr.contained && !sym->attr.use_assoc
1851 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1852 {
1853 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
1854 " used as actual argument at %L",
1855 sym->name, &e->where))
1856 goto cleanup;
1857 }
1858
1859 if (sym->attr.elemental && !sym->attr.intrinsic)
1860 {
1861 gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
1862 "allowed as an actual argument at %L", sym->name,
1863 &e->where);
1864 }
1865
1866 /* Check if a generic interface has a specific procedure
1867 with the same name before emitting an error. */
1868 if (sym->attr.generic && count_specific_procs (e) != 1)
1869 goto cleanup;
1870
1871 /* Just in case a specific was found for the expression. */
1872 sym = e->symtree->n.sym;
1873
1874 /* If the symbol is the function that names the current (or
1875 parent) scope, then we really have a variable reference. */
1876
1877 if (gfc_is_function_return_value (sym, sym->ns))
1878 goto got_variable;
1879
1880 /* If all else fails, see if we have a specific intrinsic. */
1881 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1882 {
1883 gfc_intrinsic_sym *isym;
1884
1885 isym = gfc_find_function (sym->name);
1886 if (isym == NULL || !isym->specific)
1887 {
1888 gfc_error ("Unable to find a specific INTRINSIC procedure "
1889 "for the reference %qs at %L", sym->name,
1890 &e->where);
1891 goto cleanup;
1892 }
1893 sym->ts = isym->ts;
1894 sym->attr.intrinsic = 1;
1895 sym->attr.function = 1;
1896 }
1897
1898 if (!gfc_resolve_expr (e))
1899 goto cleanup;
1900 goto argument_list;
1901 }
1902
1903 /* See if the name is a module procedure in a parent unit. */
1904
1905 if (was_declared (sym) || sym->ns->parent == NULL)
1906 goto got_variable;
1907
1908 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1909 {
1910 gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
1911 goto cleanup;
1912 }
1913
1914 if (parent_st == NULL)
1915 goto got_variable;
1916
1917 sym = parent_st->n.sym;
1918 e->symtree = parent_st; /* Point to the right thing. */
1919
1920 if (sym->attr.flavor == FL_PROCEDURE
1921 || sym->attr.intrinsic
1922 || sym->attr.external)
1923 {
1924 if (!gfc_resolve_expr (e))
1925 goto cleanup;
1926 goto argument_list;
1927 }
1928
1929 got_variable:
1930 e->expr_type = EXPR_VARIABLE;
1931 e->ts = sym->ts;
1932 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
1933 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
1934 && CLASS_DATA (sym)->as))
1935 {
1936 e->rank = sym->ts.type == BT_CLASS
1937 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
1938 e->ref = gfc_get_ref ();
1939 e->ref->type = REF_ARRAY;
1940 e->ref->u.ar.type = AR_FULL;
1941 e->ref->u.ar.as = sym->ts.type == BT_CLASS
1942 ? CLASS_DATA (sym)->as : sym->as;
1943 }
1944
1945 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1946 primary.c (match_actual_arg). If above code determines that it
1947 is a variable instead, it needs to be resolved as it was not
1948 done at the beginning of this function. */
1949 save_need_full_assumed_size = need_full_assumed_size;
1950 if (e->expr_type != EXPR_VARIABLE)
1951 need_full_assumed_size = 0;
1952 if (!gfc_resolve_expr (e))
1953 goto cleanup;
1954 need_full_assumed_size = save_need_full_assumed_size;
1955
1956 argument_list:
1957 /* Check argument list functions %VAL, %LOC and %REF. There is
1958 nothing to do for %REF. */
1959 if (arg->name && arg->name[0] == '%')
1960 {
1961 if (strncmp ("%VAL", arg->name, 4) == 0)
1962 {
1963 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1964 {
1965 gfc_error ("By-value argument at %L is not of numeric "
1966 "type", &e->where);
1967 goto cleanup;
1968 }
1969
1970 if (e->rank)
1971 {
1972 gfc_error ("By-value argument at %L cannot be an array or "
1973 "an array section", &e->where);
1974 goto cleanup;
1975 }
1976
1977 /* Intrinsics are still PROC_UNKNOWN here. However,
1978 since same file external procedures are not resolvable
1979 in gfortran, it is a good deal easier to leave them to
1980 intrinsic.c. */
1981 if (ptype != PROC_UNKNOWN
1982 && ptype != PROC_DUMMY
1983 && ptype != PROC_EXTERNAL
1984 && ptype != PROC_MODULE)
1985 {
1986 gfc_error ("By-value argument at %L is not allowed "
1987 "in this context", &e->where);
1988 goto cleanup;
1989 }
1990 }
1991
1992 /* Statement functions have already been excluded above. */
1993 else if (strncmp ("%LOC", arg->name, 4) == 0
1994 && e->ts.type == BT_PROCEDURE)
1995 {
1996 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1997 {
1998 gfc_error ("Passing internal procedure at %L by location "
1999 "not allowed", &e->where);
2000 goto cleanup;
2001 }
2002 }
2003 }
2004
2005 comp = gfc_get_proc_ptr_comp(e);
2006 if (e->expr_type == EXPR_VARIABLE
2007 && comp && comp->attr.elemental)
2008 {
2009 gfc_error ("ELEMENTAL procedure pointer component %qs is not "
2010 "allowed as an actual argument at %L", comp->name,
2011 &e->where);
2012 }
2013
2014 /* Fortran 2008, C1237. */
2015 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
2016 && gfc_has_ultimate_pointer (e))
2017 {
2018 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2019 "component", &e->where);
2020 goto cleanup;
2021 }
2022
2023 first_actual_arg = false;
2024 }
2025
2026 return_value = true;
2027
2028 cleanup:
2029 actual_arg = actual_arg_sav;
2030 first_actual_arg = first_actual_arg_sav;
2031
2032 return return_value;
2033 }
2034
2035
2036 /* Do the checks of the actual argument list that are specific to elemental
2037 procedures. If called with c == NULL, we have a function, otherwise if
2038 expr == NULL, we have a subroutine. */
2039
2040 static bool
2041 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2042 {
2043 gfc_actual_arglist *arg0;
2044 gfc_actual_arglist *arg;
2045 gfc_symbol *esym = NULL;
2046 gfc_intrinsic_sym *isym = NULL;
2047 gfc_expr *e = NULL;
2048 gfc_intrinsic_arg *iformal = NULL;
2049 gfc_formal_arglist *eformal = NULL;
2050 bool formal_optional = false;
2051 bool set_by_optional = false;
2052 int i;
2053 int rank = 0;
2054
2055 /* Is this an elemental procedure? */
2056 if (expr && expr->value.function.actual != NULL)
2057 {
2058 if (expr->value.function.esym != NULL
2059 && expr->value.function.esym->attr.elemental)
2060 {
2061 arg0 = expr->value.function.actual;
2062 esym = expr->value.function.esym;
2063 }
2064 else if (expr->value.function.isym != NULL
2065 && expr->value.function.isym->elemental)
2066 {
2067 arg0 = expr->value.function.actual;
2068 isym = expr->value.function.isym;
2069 }
2070 else
2071 return true;
2072 }
2073 else if (c && c->ext.actual != NULL)
2074 {
2075 arg0 = c->ext.actual;
2076
2077 if (c->resolved_sym)
2078 esym = c->resolved_sym;
2079 else
2080 esym = c->symtree->n.sym;
2081 gcc_assert (esym);
2082
2083 if (!esym->attr.elemental)
2084 return true;
2085 }
2086 else
2087 return true;
2088
2089 /* The rank of an elemental is the rank of its array argument(s). */
2090 for (arg = arg0; arg; arg = arg->next)
2091 {
2092 if (arg->expr != NULL && arg->expr->rank != 0)
2093 {
2094 rank = arg->expr->rank;
2095 if (arg->expr->expr_type == EXPR_VARIABLE
2096 && arg->expr->symtree->n.sym->attr.optional)
2097 set_by_optional = true;
2098
2099 /* Function specific; set the result rank and shape. */
2100 if (expr)
2101 {
2102 expr->rank = rank;
2103 if (!expr->shape && arg->expr->shape)
2104 {
2105 expr->shape = gfc_get_shape (rank);
2106 for (i = 0; i < rank; i++)
2107 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2108 }
2109 }
2110 break;
2111 }
2112 }
2113
2114 /* If it is an array, it shall not be supplied as an actual argument
2115 to an elemental procedure unless an array of the same rank is supplied
2116 as an actual argument corresponding to a nonoptional dummy argument of
2117 that elemental procedure(12.4.1.5). */
2118 formal_optional = false;
2119 if (isym)
2120 iformal = isym->formal;
2121 else
2122 eformal = esym->formal;
2123
2124 for (arg = arg0; arg; arg = arg->next)
2125 {
2126 if (eformal)
2127 {
2128 if (eformal->sym && eformal->sym->attr.optional)
2129 formal_optional = true;
2130 eformal = eformal->next;
2131 }
2132 else if (isym && iformal)
2133 {
2134 if (iformal->optional)
2135 formal_optional = true;
2136 iformal = iformal->next;
2137 }
2138 else if (isym)
2139 formal_optional = true;
2140
2141 if (pedantic && arg->expr != NULL
2142 && arg->expr->expr_type == EXPR_VARIABLE
2143 && arg->expr->symtree->n.sym->attr.optional
2144 && formal_optional
2145 && arg->expr->rank
2146 && (set_by_optional || arg->expr->rank != rank)
2147 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2148 {
2149 gfc_warning (OPT_Wpedantic,
2150 "%qs at %L is an array and OPTIONAL; IF IT IS "
2151 "MISSING, it cannot be the actual argument of an "
2152 "ELEMENTAL procedure unless there is a non-optional "
2153 "argument with the same rank (12.4.1.5)",
2154 arg->expr->symtree->n.sym->name, &arg->expr->where);
2155 }
2156 }
2157
2158 for (arg = arg0; arg; arg = arg->next)
2159 {
2160 if (arg->expr == NULL || arg->expr->rank == 0)
2161 continue;
2162
2163 /* Being elemental, the last upper bound of an assumed size array
2164 argument must be present. */
2165 if (resolve_assumed_size_actual (arg->expr))
2166 return false;
2167
2168 /* Elemental procedure's array actual arguments must conform. */
2169 if (e != NULL)
2170 {
2171 if (!gfc_check_conformance (arg->expr, e, "elemental procedure"))
2172 return false;
2173 }
2174 else
2175 e = arg->expr;
2176 }
2177
2178 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2179 is an array, the intent inout/out variable needs to be also an array. */
2180 if (rank > 0 && esym && expr == NULL)
2181 for (eformal = esym->formal, arg = arg0; arg && eformal;
2182 arg = arg->next, eformal = eformal->next)
2183 if ((eformal->sym->attr.intent == INTENT_OUT
2184 || eformal->sym->attr.intent == INTENT_INOUT)
2185 && arg->expr && arg->expr->rank == 0)
2186 {
2187 gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2188 "ELEMENTAL subroutine %qs is a scalar, but another "
2189 "actual argument is an array", &arg->expr->where,
2190 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2191 : "INOUT", eformal->sym->name, esym->name);
2192 return false;
2193 }
2194 return true;
2195 }
2196
2197
2198 /* This function does the checking of references to global procedures
2199 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2200 77 and 95 standards. It checks for a gsymbol for the name, making
2201 one if it does not already exist. If it already exists, then the
2202 reference being resolved must correspond to the type of gsymbol.
2203 Otherwise, the new symbol is equipped with the attributes of the
2204 reference. The corresponding code that is called in creating
2205 global entities is parse.c.
2206
2207 In addition, for all but -std=legacy, the gsymbols are used to
2208 check the interfaces of external procedures from the same file.
2209 The namespace of the gsymbol is resolved and then, once this is
2210 done the interface is checked. */
2211
2212
2213 static bool
2214 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2215 {
2216 if (!gsym_ns->proc_name->attr.recursive)
2217 return true;
2218
2219 if (sym->ns == gsym_ns)
2220 return false;
2221
2222 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2223 return false;
2224
2225 return true;
2226 }
2227
2228 static bool
2229 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2230 {
2231 if (gsym_ns->entries)
2232 {
2233 gfc_entry_list *entry = gsym_ns->entries;
2234
2235 for (; entry; entry = entry->next)
2236 {
2237 if (strcmp (sym->name, entry->sym->name) == 0)
2238 {
2239 if (strcmp (gsym_ns->proc_name->name,
2240 sym->ns->proc_name->name) == 0)
2241 return false;
2242
2243 if (sym->ns->parent
2244 && strcmp (gsym_ns->proc_name->name,
2245 sym->ns->parent->proc_name->name) == 0)
2246 return false;
2247 }
2248 }
2249 }
2250 return true;
2251 }
2252
2253
2254 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2255
2256 bool
2257 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2258 {
2259 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2260
2261 for ( ; arg; arg = arg->next)
2262 {
2263 if (!arg->sym)
2264 continue;
2265
2266 if (arg->sym->attr.allocatable) /* (2a) */
2267 {
2268 strncpy (errmsg, _("allocatable argument"), err_len);
2269 return true;
2270 }
2271 else if (arg->sym->attr.asynchronous)
2272 {
2273 strncpy (errmsg, _("asynchronous argument"), err_len);
2274 return true;
2275 }
2276 else if (arg->sym->attr.optional)
2277 {
2278 strncpy (errmsg, _("optional argument"), err_len);
2279 return true;
2280 }
2281 else if (arg->sym->attr.pointer)
2282 {
2283 strncpy (errmsg, _("pointer argument"), err_len);
2284 return true;
2285 }
2286 else if (arg->sym->attr.target)
2287 {
2288 strncpy (errmsg, _("target argument"), err_len);
2289 return true;
2290 }
2291 else if (arg->sym->attr.value)
2292 {
2293 strncpy (errmsg, _("value argument"), err_len);
2294 return true;
2295 }
2296 else if (arg->sym->attr.volatile_)
2297 {
2298 strncpy (errmsg, _("volatile argument"), err_len);
2299 return true;
2300 }
2301 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2302 {
2303 strncpy (errmsg, _("assumed-shape argument"), err_len);
2304 return true;
2305 }
2306 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2307 {
2308 strncpy (errmsg, _("assumed-rank argument"), err_len);
2309 return true;
2310 }
2311 else if (arg->sym->attr.codimension) /* (2c) */
2312 {
2313 strncpy (errmsg, _("coarray argument"), err_len);
2314 return true;
2315 }
2316 else if (false) /* (2d) TODO: parametrized derived type */
2317 {
2318 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2319 return true;
2320 }
2321 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2322 {
2323 strncpy (errmsg, _("polymorphic argument"), err_len);
2324 return true;
2325 }
2326 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2327 {
2328 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2329 return true;
2330 }
2331 else if (arg->sym->ts.type == BT_ASSUMED)
2332 {
2333 /* As assumed-type is unlimited polymorphic (cf. above).
2334 See also TS 29113, Note 6.1. */
2335 strncpy (errmsg, _("assumed-type argument"), err_len);
2336 return true;
2337 }
2338 }
2339
2340 if (sym->attr.function)
2341 {
2342 gfc_symbol *res = sym->result ? sym->result : sym;
2343
2344 if (res->attr.dimension) /* (3a) */
2345 {
2346 strncpy (errmsg, _("array result"), err_len);
2347 return true;
2348 }
2349 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2350 {
2351 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2352 return true;
2353 }
2354 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2355 && res->ts.u.cl->length
2356 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2357 {
2358 strncpy (errmsg, _("result with non-constant character length"), err_len);
2359 return true;
2360 }
2361 }
2362
2363 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2364 {
2365 strncpy (errmsg, _("elemental procedure"), err_len);
2366 return true;
2367 }
2368 else if (sym->attr.is_bind_c) /* (5) */
2369 {
2370 strncpy (errmsg, _("bind(c) procedure"), err_len);
2371 return true;
2372 }
2373
2374 return false;
2375 }
2376
2377
2378 static void
2379 resolve_global_procedure (gfc_symbol *sym, locus *where,
2380 gfc_actual_arglist **actual, int sub)
2381 {
2382 gfc_gsymbol * gsym;
2383 gfc_namespace *ns;
2384 enum gfc_symbol_type type;
2385 char reason[200];
2386
2387 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2388
2389 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name);
2390
2391 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2392 gfc_global_used (gsym, where);
2393
2394 if ((sym->attr.if_source == IFSRC_UNKNOWN
2395 || sym->attr.if_source == IFSRC_IFBODY)
2396 && gsym->type != GSYM_UNKNOWN
2397 && !gsym->binding_label
2398 && gsym->ns
2399 && gsym->ns->resolved != -1
2400 && gsym->ns->proc_name
2401 && not_in_recursive (sym, gsym->ns)
2402 && not_entry_self_reference (sym, gsym->ns))
2403 {
2404 gfc_symbol *def_sym;
2405
2406 /* Resolve the gsymbol namespace if needed. */
2407 if (!gsym->ns->resolved)
2408 {
2409 gfc_dt_list *old_dt_list;
2410
2411 /* Stash away derived types so that the backend_decls do not
2412 get mixed up. */
2413 old_dt_list = gfc_derived_types;
2414 gfc_derived_types = NULL;
2415
2416 gfc_resolve (gsym->ns);
2417
2418 /* Store the new derived types with the global namespace. */
2419 if (gfc_derived_types)
2420 gsym->ns->derived_types = gfc_derived_types;
2421
2422 /* Restore the derived types of this namespace. */
2423 gfc_derived_types = old_dt_list;
2424 }
2425
2426 /* Make sure that translation for the gsymbol occurs before
2427 the procedure currently being resolved. */
2428 ns = gfc_global_ns_list;
2429 for (; ns && ns != gsym->ns; ns = ns->sibling)
2430 {
2431 if (ns->sibling == gsym->ns)
2432 {
2433 ns->sibling = gsym->ns->sibling;
2434 gsym->ns->sibling = gfc_global_ns_list;
2435 gfc_global_ns_list = gsym->ns;
2436 break;
2437 }
2438 }
2439
2440 def_sym = gsym->ns->proc_name;
2441
2442 /* This can happen if a binding name has been specified. */
2443 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2444 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2445
2446 if (def_sym->attr.entry_master)
2447 {
2448 gfc_entry_list *entry;
2449 for (entry = gsym->ns->entries; entry; entry = entry->next)
2450 if (strcmp (entry->sym->name, sym->name) == 0)
2451 {
2452 def_sym = entry->sym;
2453 break;
2454 }
2455 }
2456
2457 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2458 {
2459 gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2460 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2461 gfc_typename (&def_sym->ts));
2462 goto done;
2463 }
2464
2465 if (sym->attr.if_source == IFSRC_UNKNOWN
2466 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2467 {
2468 gfc_error ("Explicit interface required for %qs at %L: %s",
2469 sym->name, &sym->declared_at, reason);
2470 goto done;
2471 }
2472
2473 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU))
2474 /* Turn erros into warnings with -std=gnu and -std=legacy. */
2475 gfc_errors_to_warnings (true);
2476
2477 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2478 reason, sizeof(reason), NULL, NULL))
2479 {
2480 gfc_error (OPT_Wargument_mismatch,
2481 "Interface mismatch in global procedure %qs at %L: %s ",
2482 sym->name, &sym->declared_at, reason);
2483 goto done;
2484 }
2485
2486 if (!pedantic
2487 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2488 && !(gfc_option.warn_std & GFC_STD_GNU)))
2489 gfc_errors_to_warnings (true);
2490
2491 if (sym->attr.if_source != IFSRC_IFBODY)
2492 gfc_procedure_use (def_sym, actual, where);
2493 }
2494
2495 done:
2496 gfc_errors_to_warnings (false);
2497
2498 if (gsym->type == GSYM_UNKNOWN)
2499 {
2500 gsym->type = type;
2501 gsym->where = *where;
2502 }
2503
2504 gsym->used = 1;
2505 }
2506
2507
2508 /************* Function resolution *************/
2509
2510 /* Resolve a function call known to be generic.
2511 Section 14.1.2.4.1. */
2512
2513 static match
2514 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2515 {
2516 gfc_symbol *s;
2517
2518 if (sym->attr.generic)
2519 {
2520 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2521 if (s != NULL)
2522 {
2523 expr->value.function.name = s->name;
2524 expr->value.function.esym = s;
2525
2526 if (s->ts.type != BT_UNKNOWN)
2527 expr->ts = s->ts;
2528 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2529 expr->ts = s->result->ts;
2530
2531 if (s->as != NULL)
2532 expr->rank = s->as->rank;
2533 else if (s->result != NULL && s->result->as != NULL)
2534 expr->rank = s->result->as->rank;
2535
2536 gfc_set_sym_referenced (expr->value.function.esym);
2537
2538 return MATCH_YES;
2539 }
2540
2541 /* TODO: Need to search for elemental references in generic
2542 interface. */
2543 }
2544
2545 if (sym->attr.intrinsic)
2546 return gfc_intrinsic_func_interface (expr, 0);
2547
2548 return MATCH_NO;
2549 }
2550
2551
2552 static bool
2553 resolve_generic_f (gfc_expr *expr)
2554 {
2555 gfc_symbol *sym;
2556 match m;
2557 gfc_interface *intr = NULL;
2558
2559 sym = expr->symtree->n.sym;
2560
2561 for (;;)
2562 {
2563 m = resolve_generic_f0 (expr, sym);
2564 if (m == MATCH_YES)
2565 return true;
2566 else if (m == MATCH_ERROR)
2567 return false;
2568
2569 generic:
2570 if (!intr)
2571 for (intr = sym->generic; intr; intr = intr->next)
2572 if (gfc_fl_struct (intr->sym->attr.flavor))
2573 break;
2574
2575 if (sym->ns->parent == NULL)
2576 break;
2577 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2578
2579 if (sym == NULL)
2580 break;
2581 if (!generic_sym (sym))
2582 goto generic;
2583 }
2584
2585 /* Last ditch attempt. See if the reference is to an intrinsic
2586 that possesses a matching interface. 14.1.2.4 */
2587 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2588 {
2589 if (gfc_init_expr_flag)
2590 gfc_error ("Function %qs in initialization expression at %L "
2591 "must be an intrinsic function",
2592 expr->symtree->n.sym->name, &expr->where);
2593 else
2594 gfc_error ("There is no specific function for the generic %qs "
2595 "at %L", expr->symtree->n.sym->name, &expr->where);
2596 return false;
2597 }
2598
2599 if (intr)
2600 {
2601 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2602 NULL, false))
2603 return false;
2604 return resolve_structure_cons (expr, 0);
2605 }
2606
2607 m = gfc_intrinsic_func_interface (expr, 0);
2608 if (m == MATCH_YES)
2609 return true;
2610
2611 if (m == MATCH_NO)
2612 gfc_error ("Generic function %qs at %L is not consistent with a "
2613 "specific intrinsic interface", expr->symtree->n.sym->name,
2614 &expr->where);
2615
2616 return false;
2617 }
2618
2619
2620 /* Resolve a function call known to be specific. */
2621
2622 static match
2623 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2624 {
2625 match m;
2626
2627 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2628 {
2629 if (sym->attr.dummy)
2630 {
2631 sym->attr.proc = PROC_DUMMY;
2632 goto found;
2633 }
2634
2635 sym->attr.proc = PROC_EXTERNAL;
2636 goto found;
2637 }
2638
2639 if (sym->attr.proc == PROC_MODULE
2640 || sym->attr.proc == PROC_ST_FUNCTION
2641 || sym->attr.proc == PROC_INTERNAL)
2642 goto found;
2643
2644 if (sym->attr.intrinsic)
2645 {
2646 m = gfc_intrinsic_func_interface (expr, 1);
2647 if (m == MATCH_YES)
2648 return MATCH_YES;
2649 if (m == MATCH_NO)
2650 gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
2651 "with an intrinsic", sym->name, &expr->where);
2652
2653 return MATCH_ERROR;
2654 }
2655
2656 return MATCH_NO;
2657
2658 found:
2659 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2660
2661 if (sym->result)
2662 expr->ts = sym->result->ts;
2663 else
2664 expr->ts = sym->ts;
2665 expr->value.function.name = sym->name;
2666 expr->value.function.esym = sym;
2667 /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2668 error(s). */
2669 if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
2670 return MATCH_ERROR;
2671 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2672 expr->rank = CLASS_DATA (sym)->as->rank;
2673 else if (sym->as != NULL)
2674 expr->rank = sym->as->rank;
2675
2676 return MATCH_YES;
2677 }
2678
2679
2680 static bool
2681 resolve_specific_f (gfc_expr *expr)
2682 {
2683 gfc_symbol *sym;
2684 match m;
2685
2686 sym = expr->symtree->n.sym;
2687
2688 for (;;)
2689 {
2690 m = resolve_specific_f0 (sym, expr);
2691 if (m == MATCH_YES)
2692 return true;
2693 if (m == MATCH_ERROR)
2694 return false;
2695
2696 if (sym->ns->parent == NULL)
2697 break;
2698
2699 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2700
2701 if (sym == NULL)
2702 break;
2703 }
2704
2705 gfc_error ("Unable to resolve the specific function %qs at %L",
2706 expr->symtree->n.sym->name, &expr->where);
2707
2708 return true;
2709 }
2710
2711
2712 /* Resolve a procedure call not known to be generic nor specific. */
2713
2714 static bool
2715 resolve_unknown_f (gfc_expr *expr)
2716 {
2717 gfc_symbol *sym;
2718 gfc_typespec *ts;
2719
2720 sym = expr->symtree->n.sym;
2721
2722 if (sym->attr.dummy)
2723 {
2724 sym->attr.proc = PROC_DUMMY;
2725 expr->value.function.name = sym->name;
2726 goto set_type;
2727 }
2728
2729 /* See if we have an intrinsic function reference. */
2730
2731 if (gfc_is_intrinsic (sym, 0, expr->where))
2732 {
2733 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2734 return true;
2735 return false;
2736 }
2737
2738 /* The reference is to an external name. */
2739
2740 sym->attr.proc = PROC_EXTERNAL;
2741 expr->value.function.name = sym->name;
2742 expr->value.function.esym = expr->symtree->n.sym;
2743
2744 if (sym->as != NULL)
2745 expr->rank = sym->as->rank;
2746
2747 /* Type of the expression is either the type of the symbol or the
2748 default type of the symbol. */
2749
2750 set_type:
2751 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2752
2753 if (sym->ts.type != BT_UNKNOWN)
2754 expr->ts = sym->ts;
2755 else
2756 {
2757 ts = gfc_get_default_type (sym->name, sym->ns);
2758
2759 if (ts->type == BT_UNKNOWN)
2760 {
2761 gfc_error ("Function %qs at %L has no IMPLICIT type",
2762 sym->name, &expr->where);
2763 return false;
2764 }
2765 else
2766 expr->ts = *ts;
2767 }
2768
2769 return true;
2770 }
2771
2772
2773 /* Return true, if the symbol is an external procedure. */
2774 static bool
2775 is_external_proc (gfc_symbol *sym)
2776 {
2777 if (!sym->attr.dummy && !sym->attr.contained
2778 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
2779 && sym->attr.proc != PROC_ST_FUNCTION
2780 && !sym->attr.proc_pointer
2781 && !sym->attr.use_assoc
2782 && sym->name)
2783 return true;
2784
2785 return false;
2786 }
2787
2788
2789 /* Figure out if a function reference is pure or not. Also set the name
2790 of the function for a potential error message. Return nonzero if the
2791 function is PURE, zero if not. */
2792 static int
2793 pure_stmt_function (gfc_expr *, gfc_symbol *);
2794
2795 static int
2796 pure_function (gfc_expr *e, const char **name)
2797 {
2798 int pure;
2799 gfc_component *comp;
2800
2801 *name = NULL;
2802
2803 if (e->symtree != NULL
2804 && e->symtree->n.sym != NULL
2805 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2806 return pure_stmt_function (e, e->symtree->n.sym);
2807
2808 comp = gfc_get_proc_ptr_comp (e);
2809 if (comp)
2810 {
2811 pure = gfc_pure (comp->ts.interface);
2812 *name = comp->name;
2813 }
2814 else if (e->value.function.esym)
2815 {
2816 pure = gfc_pure (e->value.function.esym);
2817 *name = e->value.function.esym->name;
2818 }
2819 else if (e->value.function.isym)
2820 {
2821 pure = e->value.function.isym->pure
2822 || e->value.function.isym->elemental;
2823 *name = e->value.function.isym->name;
2824 }
2825 else
2826 {
2827 /* Implicit functions are not pure. */
2828 pure = 0;
2829 *name = e->value.function.name;
2830 }
2831
2832 return pure;
2833 }
2834
2835
2836 static bool
2837 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2838 int *f ATTRIBUTE_UNUSED)
2839 {
2840 const char *name;
2841
2842 /* Don't bother recursing into other statement functions
2843 since they will be checked individually for purity. */
2844 if (e->expr_type != EXPR_FUNCTION
2845 || !e->symtree
2846 || e->symtree->n.sym == sym
2847 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2848 return false;
2849
2850 return pure_function (e, &name) ? false : true;
2851 }
2852
2853
2854 static int
2855 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2856 {
2857 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2858 }
2859
2860
2861 /* Check if an impure function is allowed in the current context. */
2862
2863 static bool check_pure_function (gfc_expr *e)
2864 {
2865 const char *name = NULL;
2866 if (!pure_function (e, &name) && name)
2867 {
2868 if (forall_flag)
2869 {
2870 gfc_error ("Reference to impure function %qs at %L inside a "
2871 "FORALL %s", name, &e->where,
2872 forall_flag == 2 ? "mask" : "block");
2873 return false;
2874 }
2875 else if (gfc_do_concurrent_flag)
2876 {
2877 gfc_error ("Reference to impure function %qs at %L inside a "
2878 "DO CONCURRENT %s", name, &e->where,
2879 gfc_do_concurrent_flag == 2 ? "mask" : "block");
2880 return false;
2881 }
2882 else if (gfc_pure (NULL))
2883 {
2884 gfc_error ("Reference to impure function %qs at %L "
2885 "within a PURE procedure", name, &e->where);
2886 return false;
2887 }
2888 gfc_unset_implicit_pure (NULL);
2889 }
2890 return true;
2891 }
2892
2893
2894 /* Update current procedure's array_outer_dependency flag, considering
2895 a call to procedure SYM. */
2896
2897 static void
2898 update_current_proc_array_outer_dependency (gfc_symbol *sym)
2899 {
2900 /* Check to see if this is a sibling function that has not yet
2901 been resolved. */
2902 gfc_namespace *sibling = gfc_current_ns->sibling;
2903 for (; sibling; sibling = sibling->sibling)
2904 {
2905 if (sibling->proc_name == sym)
2906 {
2907 gfc_resolve (sibling);
2908 break;
2909 }
2910 }
2911
2912 /* If SYM has references to outer arrays, so has the procedure calling
2913 SYM. If SYM is a procedure pointer, we can assume the worst. */
2914 if (sym->attr.array_outer_dependency
2915 || sym->attr.proc_pointer)
2916 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
2917 }
2918
2919
2920 /* Resolve a function call, which means resolving the arguments, then figuring
2921 out which entity the name refers to. */
2922
2923 static bool
2924 resolve_function (gfc_expr *expr)
2925 {
2926 gfc_actual_arglist *arg;
2927 gfc_symbol *sym;
2928 bool t;
2929 int temp;
2930 procedure_type p = PROC_INTRINSIC;
2931 bool no_formal_args;
2932
2933 sym = NULL;
2934 if (expr->symtree)
2935 sym = expr->symtree->n.sym;
2936
2937 /* If this is a procedure pointer component, it has already been resolved. */
2938 if (gfc_is_proc_ptr_comp (expr))
2939 return true;
2940
2941 /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
2942 another caf_get. */
2943 if (sym && sym->attr.intrinsic
2944 && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
2945 || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
2946 return true;
2947
2948 if (sym && sym->attr.intrinsic
2949 && !gfc_resolve_intrinsic (sym, &expr->where))
2950 return false;
2951
2952 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2953 {
2954 gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
2955 return false;
2956 }
2957
2958 /* If this ia a deferred TBP with an abstract interface (which may
2959 of course be referenced), expr->value.function.esym will be set. */
2960 if (sym && sym->attr.abstract && !expr->value.function.esym)
2961 {
2962 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
2963 sym->name, &expr->where);
2964 return false;
2965 }
2966
2967 /* Switch off assumed size checking and do this again for certain kinds
2968 of procedure, once the procedure itself is resolved. */
2969 need_full_assumed_size++;
2970
2971 if (expr->symtree && expr->symtree->n.sym)
2972 p = expr->symtree->n.sym->attr.proc;
2973
2974 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2975 inquiry_argument = true;
2976 no_formal_args = sym && is_external_proc (sym)
2977 && gfc_sym_get_dummy_args (sym) == NULL;
2978
2979 if (!resolve_actual_arglist (expr->value.function.actual,
2980 p, no_formal_args))
2981 {
2982 inquiry_argument = false;
2983 return false;
2984 }
2985
2986 inquiry_argument = false;
2987
2988 /* Resume assumed_size checking. */
2989 need_full_assumed_size--;
2990
2991 /* If the procedure is external, check for usage. */
2992 if (sym && is_external_proc (sym))
2993 resolve_global_procedure (sym, &expr->where,
2994 &expr->value.function.actual, 0);
2995
2996 if (sym && sym->ts.type == BT_CHARACTER
2997 && sym->ts.u.cl
2998 && sym->ts.u.cl->length == NULL
2999 && !sym->attr.dummy
3000 && !sym->ts.deferred
3001 && expr->value.function.esym == NULL
3002 && !sym->attr.contained)
3003 {
3004 /* Internal procedures are taken care of in resolve_contained_fntype. */
3005 gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
3006 "be used at %L since it is not a dummy argument",
3007 sym->name, &expr->where);
3008 return false;
3009 }
3010
3011 /* See if function is already resolved. */
3012
3013 if (expr->value.function.name != NULL
3014 || expr->value.function.isym != NULL)
3015 {
3016 if (expr->ts.type == BT_UNKNOWN)
3017 expr->ts = sym->ts;
3018 t = true;
3019 }
3020 else
3021 {
3022 /* Apply the rules of section 14.1.2. */
3023
3024 switch (procedure_kind (sym))
3025 {
3026 case PTYPE_GENERIC:
3027 t = resolve_generic_f (expr);
3028 break;
3029
3030 case PTYPE_SPECIFIC:
3031 t = resolve_specific_f (expr);
3032 break;
3033
3034 case PTYPE_UNKNOWN:
3035 t = resolve_unknown_f (expr);
3036 break;
3037
3038 default:
3039 gfc_internal_error ("resolve_function(): bad function type");
3040 }
3041 }
3042
3043 /* If the expression is still a function (it might have simplified),
3044 then we check to see if we are calling an elemental function. */
3045
3046 if (expr->expr_type != EXPR_FUNCTION)
3047 return t;
3048
3049 temp = need_full_assumed_size;
3050 need_full_assumed_size = 0;
3051
3052 if (!resolve_elemental_actual (expr, NULL))
3053 return false;
3054
3055 if (omp_workshare_flag
3056 && expr->value.function.esym
3057 && ! gfc_elemental (expr->value.function.esym))
3058 {
3059 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3060 "in WORKSHARE construct", expr->value.function.esym->name,
3061 &expr->where);
3062 t = false;
3063 }
3064
3065 #define GENERIC_ID expr->value.function.isym->id
3066 else if (expr->value.function.actual != NULL
3067 && expr->value.function.isym != NULL
3068 && GENERIC_ID != GFC_ISYM_LBOUND
3069 && GENERIC_ID != GFC_ISYM_LCOBOUND
3070 && GENERIC_ID != GFC_ISYM_UCOBOUND
3071 && GENERIC_ID != GFC_ISYM_LEN
3072 && GENERIC_ID != GFC_ISYM_LOC
3073 && GENERIC_ID != GFC_ISYM_C_LOC
3074 && GENERIC_ID != GFC_ISYM_PRESENT)
3075 {
3076 /* Array intrinsics must also have the last upper bound of an
3077 assumed size array argument. UBOUND and SIZE have to be
3078 excluded from the check if the second argument is anything
3079 than a constant. */
3080
3081 for (arg = expr->value.function.actual; arg; arg = arg->next)
3082 {
3083 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3084 && arg == expr->value.function.actual
3085 && arg->next != NULL && arg->next->expr)
3086 {
3087 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3088 break;
3089
3090 if (arg->next->name && strncmp (arg->next->name, "kind", 4) == 0)
3091 break;
3092
3093 if ((int)mpz_get_si (arg->next->expr->value.integer)
3094 < arg->expr->rank)
3095 break;
3096 }
3097
3098 if (arg->expr != NULL
3099 && arg->expr->rank > 0
3100 && resolve_assumed_size_actual (arg->expr))
3101 return false;
3102 }
3103 }
3104 #undef GENERIC_ID
3105
3106 need_full_assumed_size = temp;
3107
3108 if (!check_pure_function(expr))
3109 t = false;
3110
3111 /* Functions without the RECURSIVE attribution are not allowed to
3112 * call themselves. */
3113 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3114 {
3115 gfc_symbol *esym;
3116 esym = expr->value.function.esym;
3117
3118 if (is_illegal_recursion (esym, gfc_current_ns))
3119 {
3120 if (esym->attr.entry && esym->ns->entries)
3121 gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3122 " function %qs is not RECURSIVE",
3123 esym->name, &expr->where, esym->ns->entries->sym->name);
3124 else
3125 gfc_error ("Function %qs at %L cannot be called recursively, as it"
3126 " is not RECURSIVE", esym->name, &expr->where);
3127
3128 t = false;
3129 }
3130 }
3131
3132 /* Character lengths of use associated functions may contains references to
3133 symbols not referenced from the current program unit otherwise. Make sure
3134 those symbols are marked as referenced. */
3135
3136 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3137 && expr->value.function.esym->attr.use_assoc)
3138 {
3139 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3140 }
3141
3142 /* Make sure that the expression has a typespec that works. */
3143 if (expr->ts.type == BT_UNKNOWN)
3144 {
3145 if (expr->symtree->n.sym->result
3146 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3147 && !expr->symtree->n.sym->result->attr.proc_pointer)
3148 expr->ts = expr->symtree->n.sym->result->ts;
3149 }
3150
3151 if (!expr->ref && !expr->value.function.isym)
3152 {
3153 if (expr->value.function.esym)
3154 update_current_proc_array_outer_dependency (expr->value.function.esym);
3155 else
3156 update_current_proc_array_outer_dependency (sym);
3157 }
3158 else if (expr->ref)
3159 /* typebound procedure: Assume the worst. */
3160 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3161
3162 return t;
3163 }
3164
3165
3166 /************* Subroutine resolution *************/
3167
3168 static bool
3169 pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3170 {
3171 if (gfc_pure (sym))
3172 return true;
3173
3174 if (forall_flag)
3175 {
3176 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3177 name, loc);
3178 return false;
3179 }
3180 else if (gfc_do_concurrent_flag)
3181 {
3182 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3183 "PURE", name, loc);
3184 return false;
3185 }
3186 else if (gfc_pure (NULL))
3187 {
3188 gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3189 return false;
3190 }
3191
3192 gfc_unset_implicit_pure (NULL);
3193 return true;
3194 }
3195
3196
3197 static match
3198 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3199 {
3200 gfc_symbol *s;
3201
3202 if (sym->attr.generic)
3203 {
3204 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3205 if (s != NULL)
3206 {
3207 c->resolved_sym = s;
3208 if (!pure_subroutine (s, s->name, &c->loc))
3209 return MATCH_ERROR;
3210 return MATCH_YES;
3211 }
3212
3213 /* TODO: Need to search for elemental references in generic interface. */
3214 }
3215
3216 if (sym->attr.intrinsic)
3217 return gfc_intrinsic_sub_interface (c, 0);
3218
3219 return MATCH_NO;
3220 }
3221
3222
3223 static bool
3224 resolve_generic_s (gfc_code *c)
3225 {
3226 gfc_symbol *sym;
3227 match m;
3228
3229 sym = c->symtree->n.sym;
3230
3231 for (;;)
3232 {
3233 m = resolve_generic_s0 (c, sym);
3234 if (m == MATCH_YES)
3235 return true;
3236 else if (m == MATCH_ERROR)
3237 return false;
3238
3239 generic:
3240 if (sym->ns->parent == NULL)
3241 break;
3242 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3243
3244 if (sym == NULL)
3245 break;
3246 if (!generic_sym (sym))
3247 goto generic;
3248 }
3249
3250 /* Last ditch attempt. See if the reference is to an intrinsic
3251 that possesses a matching interface. 14.1.2.4 */
3252 sym = c->symtree->n.sym;
3253
3254 if (!gfc_is_intrinsic (sym, 1, c->loc))
3255 {
3256 gfc_error ("There is no specific subroutine for the generic %qs at %L",
3257 sym->name, &c->loc);
3258 return false;
3259 }
3260
3261 m = gfc_intrinsic_sub_interface (c, 0);
3262 if (m == MATCH_YES)
3263 return true;
3264 if (m == MATCH_NO)
3265 gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3266 "intrinsic subroutine interface", sym->name, &c->loc);
3267
3268 return false;
3269 }
3270
3271
3272 /* Resolve a subroutine call known to be specific. */
3273
3274 static match
3275 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3276 {
3277 match m;
3278
3279 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3280 {
3281 if (sym->attr.dummy)
3282 {
3283 sym->attr.proc = PROC_DUMMY;
3284 goto found;
3285 }
3286
3287 sym->attr.proc = PROC_EXTERNAL;
3288 goto found;
3289 }
3290
3291 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3292 goto found;
3293
3294 if (sym->attr.intrinsic)
3295 {
3296 m = gfc_intrinsic_sub_interface (c, 1);
3297 if (m == MATCH_YES)
3298 return MATCH_YES;
3299 if (m == MATCH_NO)
3300 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3301 "with an intrinsic", sym->name, &c->loc);
3302
3303 return MATCH_ERROR;
3304 }
3305
3306 return MATCH_NO;
3307
3308 found:
3309 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3310
3311 c->resolved_sym = sym;
3312 if (!pure_subroutine (sym, sym->name, &c->loc))
3313 return MATCH_ERROR;
3314
3315 return MATCH_YES;
3316 }
3317
3318
3319 static bool
3320 resolve_specific_s (gfc_code *c)
3321 {
3322 gfc_symbol *sym;
3323 match m;
3324
3325 sym = c->symtree->n.sym;
3326
3327 for (;;)
3328 {
3329 m = resolve_specific_s0 (c, sym);
3330 if (m == MATCH_YES)
3331 return true;
3332 if (m == MATCH_ERROR)
3333 return false;
3334
3335 if (sym->ns->parent == NULL)
3336 break;
3337
3338 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3339
3340 if (sym == NULL)
3341 break;
3342 }
3343
3344 sym = c->symtree->n.sym;
3345 gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3346 sym->name, &c->loc);
3347
3348 return false;
3349 }
3350
3351
3352 /* Resolve a subroutine call not known to be generic nor specific. */
3353
3354 static bool
3355 resolve_unknown_s (gfc_code *c)
3356 {
3357 gfc_symbol *sym;
3358
3359 sym = c->symtree->n.sym;
3360
3361 if (sym->attr.dummy)
3362 {
3363 sym->attr.proc = PROC_DUMMY;
3364 goto found;
3365 }
3366
3367 /* See if we have an intrinsic function reference. */
3368
3369 if (gfc_is_intrinsic (sym, 1, c->loc))
3370 {
3371 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3372 return true;
3373 return false;
3374 }
3375
3376 /* The reference is to an external name. */
3377
3378 found:
3379 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3380
3381 c->resolved_sym = sym;
3382
3383 return pure_subroutine (sym, sym->name, &c->loc);
3384 }
3385
3386
3387 /* Resolve a subroutine call. Although it was tempting to use the same code
3388 for functions, subroutines and functions are stored differently and this
3389 makes things awkward. */
3390
3391 static bool
3392 resolve_call (gfc_code *c)
3393 {
3394 bool t;
3395 procedure_type ptype = PROC_INTRINSIC;
3396 gfc_symbol *csym, *sym;
3397 bool no_formal_args;
3398
3399 csym = c->symtree ? c->symtree->n.sym : NULL;
3400
3401 if (csym && csym->ts.type != BT_UNKNOWN)
3402 {
3403 gfc_error ("%qs at %L has a type, which is not consistent with "
3404 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3405 return false;
3406 }
3407
3408 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3409 {
3410 gfc_symtree *st;
3411 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3412 sym = st ? st->n.sym : NULL;
3413 if (sym && csym != sym
3414 && sym->ns == gfc_current_ns
3415 && sym->attr.flavor == FL_PROCEDURE
3416 && sym->attr.contained)
3417 {
3418 sym->refs++;
3419 if (csym->attr.generic)
3420 c->symtree->n.sym = sym;
3421 else
3422 c->symtree = st;
3423 csym = c->symtree->n.sym;
3424 }
3425 }
3426
3427 /* If this ia a deferred TBP, c->expr1 will be set. */
3428 if (!c->expr1 && csym)
3429 {
3430 if (csym->attr.abstract)
3431 {
3432 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3433 csym->name, &c->loc);
3434 return false;
3435 }
3436
3437 /* Subroutines without the RECURSIVE attribution are not allowed to
3438 call themselves. */
3439 if (is_illegal_recursion (csym, gfc_current_ns))
3440 {
3441 if (csym->attr.entry && csym->ns->entries)
3442 gfc_error ("ENTRY %qs at %L cannot be called recursively, "
3443 "as subroutine %qs is not RECURSIVE",
3444 csym->name, &c->loc, csym->ns->entries->sym->name);
3445 else
3446 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
3447 "as it is not RECURSIVE", csym->name, &c->loc);
3448
3449 t = false;
3450 }
3451 }
3452
3453 /* Switch off assumed size checking and do this again for certain kinds
3454 of procedure, once the procedure itself is resolved. */
3455 need_full_assumed_size++;
3456
3457 if (csym)
3458 ptype = csym->attr.proc;
3459
3460 no_formal_args = csym && is_external_proc (csym)
3461 && gfc_sym_get_dummy_args (csym) == NULL;
3462 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3463 return false;
3464
3465 /* Resume assumed_size checking. */
3466 need_full_assumed_size--;
3467
3468 /* If external, check for usage. */
3469 if (csym && is_external_proc (csym))
3470 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3471
3472 t = true;
3473 if (c->resolved_sym == NULL)
3474 {
3475 c->resolved_isym = NULL;
3476 switch (procedure_kind (csym))
3477 {
3478 case PTYPE_GENERIC:
3479 t = resolve_generic_s (c);
3480 break;
3481
3482 case PTYPE_SPECIFIC:
3483 t = resolve_specific_s (c);
3484 break;
3485
3486 case PTYPE_UNKNOWN:
3487 t = resolve_unknown_s (c);
3488 break;
3489
3490 default:
3491 gfc_internal_error ("resolve_subroutine(): bad function type");
3492 }
3493 }
3494
3495 /* Some checks of elemental subroutine actual arguments. */
3496 if (!resolve_elemental_actual (NULL, c))
3497 return false;
3498
3499 if (!c->expr1)
3500 update_current_proc_array_outer_dependency (csym);
3501 else
3502 /* Typebound procedure: Assume the worst. */
3503 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3504
3505 return t;
3506 }
3507
3508
3509 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3510 op1->shape and op2->shape are non-NULL return true if their shapes
3511 match. If both op1->shape and op2->shape are non-NULL return false
3512 if their shapes do not match. If either op1->shape or op2->shape is
3513 NULL, return true. */
3514
3515 static bool
3516 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3517 {
3518 bool t;
3519 int i;
3520
3521 t = true;
3522
3523 if (op1->shape != NULL && op2->shape != NULL)
3524 {
3525 for (i = 0; i < op1->rank; i++)
3526 {
3527 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3528 {
3529 gfc_error ("Shapes for operands at %L and %L are not conformable",
3530 &op1->where, &op2->where);
3531 t = false;
3532 break;
3533 }
3534 }
3535 }
3536
3537 return t;
3538 }
3539
3540 /* Convert a logical operator to the corresponding bitwise intrinsic call.
3541 For example A .AND. B becomes IAND(A, B). */
3542 static gfc_expr *
3543 logical_to_bitwise (gfc_expr *e)
3544 {
3545 gfc_expr *tmp, *op1, *op2;
3546 gfc_isym_id isym;
3547 gfc_actual_arglist *args = NULL;
3548
3549 gcc_assert (e->expr_type == EXPR_OP);
3550
3551 isym = GFC_ISYM_NONE;
3552 op1 = e->value.op.op1;
3553 op2 = e->value.op.op2;
3554
3555 switch (e->value.op.op)
3556 {
3557 case INTRINSIC_NOT:
3558 isym = GFC_ISYM_NOT;
3559 break;
3560 case INTRINSIC_AND:
3561 isym = GFC_ISYM_IAND;
3562 break;
3563 case INTRINSIC_OR:
3564 isym = GFC_ISYM_IOR;
3565 break;
3566 case INTRINSIC_NEQV:
3567 isym = GFC_ISYM_IEOR;
3568 break;
3569 case INTRINSIC_EQV:
3570 /* "Bitwise eqv" is just the complement of NEQV === IEOR.
3571 Change the old expression to NEQV, which will get replaced by IEOR,
3572 and wrap it in NOT. */
3573 tmp = gfc_copy_expr (e);
3574 tmp->value.op.op = INTRINSIC_NEQV;
3575 tmp = logical_to_bitwise (tmp);
3576 isym = GFC_ISYM_NOT;
3577 op1 = tmp;
3578 op2 = NULL;
3579 break;
3580 default:
3581 gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
3582 }
3583
3584 /* Inherit the original operation's operands as arguments. */
3585 args = gfc_get_actual_arglist ();
3586 args->expr = op1;
3587 if (op2)
3588 {
3589 args->next = gfc_get_actual_arglist ();
3590 args->next->expr = op2;
3591 }
3592
3593 /* Convert the expression to a function call. */
3594 e->expr_type = EXPR_FUNCTION;
3595 e->value.function.actual = args;
3596 e->value.function.isym = gfc_intrinsic_function_by_id (isym);
3597 e->value.function.name = e->value.function.isym->name;
3598 e->value.function.esym = NULL;
3599
3600 /* Make up a pre-resolved function call symtree if we need to. */
3601 if (!e->symtree || !e->symtree->n.sym)
3602 {
3603 gfc_symbol *sym;
3604 gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
3605 sym = e->symtree->n.sym;
3606 sym->result = sym;
3607 sym->attr.flavor = FL_PROCEDURE;
3608 sym->attr.function = 1;
3609 sym->attr.elemental = 1;
3610 sym->attr.pure = 1;
3611 sym->attr.referenced = 1;
3612 gfc_intrinsic_symbol (sym);
3613 gfc_commit_symbol (sym);
3614 }
3615
3616 args->name = e->value.function.isym->formal->name;
3617 if (e->value.function.isym->formal->next)
3618 args->next->name = e->value.function.isym->formal->next->name;
3619
3620 return e;
3621 }
3622
3623 /* Resolve an operator expression node. This can involve replacing the
3624 operation with a user defined function call. */
3625
3626 static bool
3627 resolve_operator (gfc_expr *e)
3628 {
3629 gfc_expr *op1, *op2;
3630 char msg[200];
3631 bool dual_locus_error;
3632 bool t;
3633
3634 /* Resolve all subnodes-- give them types. */
3635
3636 switch (e->value.op.op)
3637 {
3638 default:
3639 if (!gfc_resolve_expr (e->value.op.op2))
3640 return false;
3641
3642 /* Fall through. */
3643
3644 case INTRINSIC_NOT:
3645 case INTRINSIC_UPLUS:
3646 case INTRINSIC_UMINUS:
3647 case INTRINSIC_PARENTHESES:
3648 if (!gfc_resolve_expr (e->value.op.op1))
3649 return false;
3650 break;
3651 }
3652
3653 /* Typecheck the new node. */
3654
3655 op1 = e->value.op.op1;
3656 op2 = e->value.op.op2;
3657 dual_locus_error = false;
3658
3659 if ((op1 && op1->expr_type == EXPR_NULL)
3660 || (op2 && op2->expr_type == EXPR_NULL))
3661 {
3662 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3663 goto bad_op;
3664 }
3665
3666 switch (e->value.op.op)
3667 {
3668 case INTRINSIC_UPLUS:
3669 case INTRINSIC_UMINUS:
3670 if (op1->ts.type == BT_INTEGER
3671 || op1->ts.type == BT_REAL
3672 || op1->ts.type == BT_COMPLEX)
3673 {
3674 e->ts = op1->ts;
3675 break;
3676 }
3677
3678 sprintf (msg, _("Operand of unary numeric operator %%<%s%%> at %%L is %s"),
3679 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3680 goto bad_op;
3681
3682 case INTRINSIC_PLUS:
3683 case INTRINSIC_MINUS:
3684 case INTRINSIC_TIMES:
3685 case INTRINSIC_DIVIDE:
3686 case INTRINSIC_POWER:
3687 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3688 {
3689 gfc_type_convert_binary (e, 1);
3690 break;
3691 }
3692
3693 sprintf (msg,
3694 _("Operands of binary numeric operator %%<%s%%> at %%L are %s/%s"),
3695 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3696 gfc_typename (&op2->ts));
3697 goto bad_op;
3698
3699 case INTRINSIC_CONCAT:
3700 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3701 && op1->ts.kind == op2->ts.kind)
3702 {
3703 e->ts.type = BT_CHARACTER;
3704 e->ts.kind = op1->ts.kind;
3705 break;
3706 }
3707
3708 sprintf (msg,
3709 _("Operands of string concatenation operator at %%L are %s/%s"),
3710 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3711 goto bad_op;
3712
3713 case INTRINSIC_AND:
3714 case INTRINSIC_OR:
3715 case INTRINSIC_EQV:
3716 case INTRINSIC_NEQV:
3717 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3718 {
3719 e->ts.type = BT_LOGICAL;
3720 e->ts.kind = gfc_kind_max (op1, op2);
3721 if (op1->ts.kind < e->ts.kind)
3722 gfc_convert_type (op1, &e->ts, 2);
3723 else if (op2->ts.kind < e->ts.kind)
3724 gfc_convert_type (op2, &e->ts, 2);
3725 break;
3726 }
3727
3728 /* Logical ops on integers become bitwise ops with -fdec. */
3729 else if (flag_dec
3730 && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
3731 {
3732 e->ts.type = BT_INTEGER;
3733 e->ts.kind = gfc_kind_max (op1, op2);
3734 if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
3735 gfc_convert_type (op1, &e->ts, 1);
3736 if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
3737 gfc_convert_type (op2, &e->ts, 1);
3738 e = logical_to_bitwise (e);
3739 return resolve_function (e);
3740 }
3741
3742 sprintf (msg, _("Operands of logical operator %%<%s%%> at %%L are %s/%s"),
3743 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3744 gfc_typename (&op2->ts));
3745
3746 goto bad_op;
3747
3748 case INTRINSIC_NOT:
3749 /* Logical ops on integers become bitwise ops with -fdec. */
3750 if (flag_dec && op1->ts.type == BT_INTEGER)
3751 {
3752 e->ts.type = BT_INTEGER;
3753 e->ts.kind = op1->ts.kind;
3754 e = logical_to_bitwise (e);
3755 return resolve_function (e);
3756 }
3757
3758 if (op1->ts.type == BT_LOGICAL)
3759 {
3760 e->ts.type = BT_LOGICAL;
3761 e->ts.kind = op1->ts.kind;
3762 break;
3763 }
3764
3765 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3766 gfc_typename (&op1->ts));
3767 goto bad_op;
3768
3769 case INTRINSIC_GT:
3770 case INTRINSIC_GT_OS:
3771 case INTRINSIC_GE:
3772 case INTRINSIC_GE_OS:
3773 case INTRINSIC_LT:
3774 case INTRINSIC_LT_OS:
3775 case INTRINSIC_LE:
3776 case INTRINSIC_LE_OS:
3777 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3778 {
3779 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3780 goto bad_op;
3781 }
3782
3783 /* Fall through. */
3784
3785 case INTRINSIC_EQ:
3786 case INTRINSIC_EQ_OS:
3787 case INTRINSIC_NE:
3788 case INTRINSIC_NE_OS:
3789 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3790 && op1->ts.kind == op2->ts.kind)
3791 {
3792 e->ts.type = BT_LOGICAL;
3793 e->ts.kind = gfc_default_logical_kind;
3794 break;
3795 }
3796
3797 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3798 {
3799 gfc_type_convert_binary (e, 1);
3800
3801 e->ts.type = BT_LOGICAL;
3802 e->ts.kind = gfc_default_logical_kind;
3803
3804 if (warn_compare_reals)
3805 {
3806 gfc_intrinsic_op op = e->value.op.op;
3807
3808 /* Type conversion has made sure that the types of op1 and op2
3809 agree, so it is only necessary to check the first one. */
3810 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
3811 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
3812 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
3813 {
3814 const char *msg;
3815
3816 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
3817 msg = "Equality comparison for %s at %L";
3818 else
3819 msg = "Inequality comparison for %s at %L";
3820
3821 gfc_warning (OPT_Wcompare_reals, msg,
3822 gfc_typename (&op1->ts), &op1->where);
3823 }
3824 }
3825
3826 break;
3827 }
3828
3829 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3830 sprintf (msg,
3831 _("Logicals at %%L must be compared with %s instead of %s"),
3832 (e->value.op.op == INTRINSIC_EQ
3833 || e->value.op.op == INTRINSIC_EQ_OS)
3834 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3835 else
3836 sprintf (msg,
3837 _("Operands of comparison operator %%<%s%%> at %%L are %s/%s"),
3838 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3839 gfc_typename (&op2->ts));
3840
3841 goto bad_op;
3842
3843 case INTRINSIC_USER:
3844 if (e->value.op.uop->op == NULL)
3845 sprintf (msg, _("Unknown operator %%<%s%%> at %%L"),
3846 e->value.op.uop->name);
3847 else if (op2 == NULL)
3848 sprintf (msg, _("Operand of user operator %%<%s%%> at %%L is %s"),
3849 e->value.op.uop->name, gfc_typename (&op1->ts));
3850 else
3851 {
3852 sprintf (msg, _("Operands of user operator %%<%s%%> at %%L are %s/%s"),
3853 e->value.op.uop->name, gfc_typename (&op1->ts),
3854 gfc_typename (&op2->ts));
3855 e->value.op.uop->op->sym->attr.referenced = 1;
3856 }
3857
3858 goto bad_op;
3859
3860 case INTRINSIC_PARENTHESES:
3861 e->ts = op1->ts;
3862 if (e->ts.type == BT_CHARACTER)
3863 e->ts.u.cl = op1->ts.u.cl;
3864 break;
3865
3866 default:
3867 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3868 }
3869
3870 /* Deal with arrayness of an operand through an operator. */
3871
3872 t = true;
3873
3874 switch (e->value.op.op)
3875 {
3876 case INTRINSIC_PLUS:
3877 case INTRINSIC_MINUS:
3878 case INTRINSIC_TIMES:
3879 case INTRINSIC_DIVIDE:
3880 case INTRINSIC_POWER:
3881 case INTRINSIC_CONCAT:
3882 case INTRINSIC_AND:
3883 case INTRINSIC_OR:
3884 case INTRINSIC_EQV:
3885 case INTRINSIC_NEQV:
3886 case INTRINSIC_EQ:
3887 case INTRINSIC_EQ_OS:
3888 case INTRINSIC_NE:
3889 case INTRINSIC_NE_OS:
3890 case INTRINSIC_GT:
3891 case INTRINSIC_GT_OS:
3892 case INTRINSIC_GE:
3893 case INTRINSIC_GE_OS:
3894 case INTRINSIC_LT:
3895 case INTRINSIC_LT_OS:
3896 case INTRINSIC_LE:
3897 case INTRINSIC_LE_OS:
3898
3899 if (op1->rank == 0 && op2->rank == 0)
3900 e->rank = 0;
3901
3902 if (op1->rank == 0 && op2->rank != 0)
3903 {
3904 e->rank = op2->rank;
3905
3906 if (e->shape == NULL)
3907 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3908 }
3909
3910 if (op1->rank != 0 && op2->rank == 0)
3911 {
3912 e->rank = op1->rank;
3913
3914 if (e->shape == NULL)
3915 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3916 }
3917
3918 if (op1->rank != 0 && op2->rank != 0)
3919 {
3920 if (op1->rank == op2->rank)
3921 {
3922 e->rank = op1->rank;
3923 if (e->shape == NULL)
3924 {
3925 t = compare_shapes (op1, op2);
3926 if (!t)
3927 e->shape = NULL;
3928 else
3929 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3930 }
3931 }
3932 else
3933 {
3934 /* Allow higher level expressions to work. */
3935 e->rank = 0;
3936
3937 /* Try user-defined operators, and otherwise throw an error. */
3938 dual_locus_error = true;
3939 sprintf (msg,
3940 _("Inconsistent ranks for operator at %%L and %%L"));
3941 goto bad_op;
3942 }
3943 }
3944
3945 break;
3946
3947 case INTRINSIC_PARENTHESES:
3948 case INTRINSIC_NOT:
3949 case INTRINSIC_UPLUS:
3950 case INTRINSIC_UMINUS:
3951 /* Simply copy arrayness attribute */
3952 e->rank = op1->rank;
3953
3954 if (e->shape == NULL)
3955 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3956
3957 break;
3958
3959 default:
3960 break;
3961 }
3962
3963 /* Attempt to simplify the expression. */
3964 if (t)
3965 {
3966 t = gfc_simplify_expr (e, 0);
3967 /* Some calls do not succeed in simplification and return false
3968 even though there is no error; e.g. variable references to
3969 PARAMETER arrays. */
3970 if (!gfc_is_constant_expr (e))
3971 t = true;
3972 }
3973 return t;
3974
3975 bad_op:
3976
3977 {
3978 match m = gfc_extend_expr (e);
3979 if (m == MATCH_YES)
3980 return true;
3981 if (m == MATCH_ERROR)
3982 return false;
3983 }
3984
3985 if (dual_locus_error)
3986 gfc_error (msg, &op1->where, &op2->where);
3987 else
3988 gfc_error (msg, &e->where);
3989
3990 return false;
3991 }
3992
3993
3994 /************** Array resolution subroutines **************/
3995
3996 enum compare_result
3997 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
3998
3999 /* Compare two integer expressions. */
4000
4001 static compare_result
4002 compare_bound (gfc_expr *a, gfc_expr *b)
4003 {
4004 int i;
4005
4006 if (a == NULL || a->expr_type != EXPR_CONSTANT
4007 || b == NULL || b->expr_type != EXPR_CONSTANT)
4008 return CMP_UNKNOWN;
4009
4010 /* If either of the types isn't INTEGER, we must have
4011 raised an error earlier. */
4012
4013 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
4014 return CMP_UNKNOWN;
4015
4016 i = mpz_cmp (a->value.integer, b->value.integer);
4017
4018 if (i < 0)
4019 return CMP_LT;
4020 if (i > 0)
4021 return CMP_GT;
4022 return CMP_EQ;
4023 }
4024
4025
4026 /* Compare an integer expression with an integer. */
4027
4028 static compare_result
4029 compare_bound_int (gfc_expr *a, int b)
4030 {
4031 int i;
4032
4033 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4034 return CMP_UNKNOWN;
4035
4036 if (a->ts.type != BT_INTEGER)
4037 gfc_internal_error ("compare_bound_int(): Bad expression");
4038
4039 i = mpz_cmp_si (a->value.integer, b);
4040
4041 if (i < 0)
4042 return CMP_LT;
4043 if (i > 0)
4044 return CMP_GT;
4045 return CMP_EQ;
4046 }
4047
4048
4049 /* Compare an integer expression with a mpz_t. */
4050
4051 static compare_result
4052 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
4053 {
4054 int i;
4055
4056 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4057 return CMP_UNKNOWN;
4058
4059 if (a->ts.type != BT_INTEGER)
4060 gfc_internal_error ("compare_bound_int(): Bad expression");
4061
4062 i = mpz_cmp (a->value.integer, b);
4063
4064 if (i < 0)
4065 return CMP_LT;
4066 if (i > 0)
4067 return CMP_GT;
4068 return CMP_EQ;
4069 }
4070
4071
4072 /* Compute the last value of a sequence given by a triplet.
4073 Return 0 if it wasn't able to compute the last value, or if the
4074 sequence if empty, and 1 otherwise. */
4075
4076 static int
4077 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4078 gfc_expr *stride, mpz_t last)
4079 {
4080 mpz_t rem;
4081
4082 if (start == NULL || start->expr_type != EXPR_CONSTANT
4083 || end == NULL || end->expr_type != EXPR_CONSTANT
4084 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4085 return 0;
4086
4087 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4088 || (stride != NULL && stride->ts.type != BT_INTEGER))
4089 return 0;
4090
4091 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
4092 {
4093 if (compare_bound (start, end) == CMP_GT)
4094 return 0;
4095 mpz_set (last, end->value.integer);
4096 return 1;
4097 }
4098
4099 if (compare_bound_int (stride, 0) == CMP_GT)
4100 {
4101 /* Stride is positive */
4102 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4103 return 0;
4104 }
4105 else
4106 {
4107 /* Stride is negative */
4108 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4109 return 0;
4110 }
4111
4112 mpz_init (rem);
4113 mpz_sub (rem, end->value.integer, start->value.integer);
4114 mpz_tdiv_r (rem, rem, stride->value.integer);
4115 mpz_sub (last, end->value.integer, rem);
4116 mpz_clear (rem);
4117
4118 return 1;
4119 }
4120
4121
4122 /* Compare a single dimension of an array reference to the array
4123 specification. */
4124
4125 static bool
4126 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4127 {
4128 mpz_t last_value;
4129
4130 if (ar->dimen_type[i] == DIMEN_STAR)
4131 {
4132 gcc_assert (ar->stride[i] == NULL);
4133 /* This implies [*] as [*:] and [*:3] are not possible. */
4134 if (ar->start[i] == NULL)
4135 {
4136 gcc_assert (ar->end[i] == NULL);
4137 return true;
4138 }
4139 }
4140
4141 /* Given start, end and stride values, calculate the minimum and
4142 maximum referenced indexes. */
4143
4144 switch (ar->dimen_type[i])
4145 {
4146 case DIMEN_VECTOR:
4147 case DIMEN_THIS_IMAGE:
4148 break;
4149
4150 case DIMEN_STAR:
4151 case DIMEN_ELEMENT:
4152 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4153 {
4154 if (i < as->rank)
4155 gfc_warning (0, "Array reference at %L is out of bounds "
4156 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4157 mpz_get_si (ar->start[i]->value.integer),
4158 mpz_get_si (as->lower[i]->value.integer), i+1);
4159 else
4160 gfc_warning (0, "Array reference at %L is out of bounds "
4161 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4162 mpz_get_si (ar->start[i]->value.integer),
4163 mpz_get_si (as->lower[i]->value.integer),
4164 i + 1 - as->rank);
4165 return true;
4166 }
4167 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4168 {
4169 if (i < as->rank)
4170 gfc_warning (0, "Array reference at %L is out of bounds "
4171 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4172 mpz_get_si (ar->start[i]->value.integer),
4173 mpz_get_si (as->upper[i]->value.integer), i+1);
4174 else
4175 gfc_warning (0, "Array reference at %L is out of bounds "
4176 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4177 mpz_get_si (ar->start[i]->value.integer),
4178 mpz_get_si (as->upper[i]->value.integer),
4179 i + 1 - as->rank);
4180 return true;
4181 }
4182
4183 break;
4184
4185 case DIMEN_RANGE:
4186 {
4187 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4188 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4189
4190 compare_result comp_start_end = compare_bound (AR_START, AR_END);
4191
4192 /* Check for zero stride, which is not allowed. */
4193 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4194 {
4195 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4196 return false;
4197 }
4198
4199 /* if start == len || (stride > 0 && start < len)
4200 || (stride < 0 && start > len),
4201 then the array section contains at least one element. In this
4202 case, there is an out-of-bounds access if
4203 (start < lower || start > upper). */
4204 if (compare_bound (AR_START, AR_END) == CMP_EQ
4205 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4206 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4207 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4208 && comp_start_end == CMP_GT))
4209 {
4210 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4211 {
4212 gfc_warning (0, "Lower array reference at %L is out of bounds "
4213 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4214 mpz_get_si (AR_START->value.integer),
4215 mpz_get_si (as->lower[i]->value.integer), i+1);
4216 return true;
4217 }
4218 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4219 {
4220 gfc_warning (0, "Lower array reference at %L is out of bounds "
4221 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4222 mpz_get_si (AR_START->value.integer),
4223 mpz_get_si (as->upper[i]->value.integer), i+1);
4224 return true;
4225 }
4226 }
4227
4228 /* If we can compute the highest index of the array section,
4229 then it also has to be between lower and upper. */
4230 mpz_init (last_value);
4231 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4232 last_value))
4233 {
4234 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4235 {
4236 gfc_warning (0, "Upper array reference at %L is out of bounds "
4237 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4238 mpz_get_si (last_value),
4239 mpz_get_si (as->lower[i]->value.integer), i+1);
4240 mpz_clear (last_value);
4241 return true;
4242 }
4243 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4244 {
4245 gfc_warning (0, "Upper array reference at %L is out of bounds "
4246 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4247 mpz_get_si (last_value),
4248 mpz_get_si (as->upper[i]->value.integer), i+1);
4249 mpz_clear (last_value);
4250 return true;
4251 }
4252 }
4253 mpz_clear (last_value);
4254
4255 #undef AR_START
4256 #undef AR_END
4257 }
4258 break;
4259
4260 default:
4261 gfc_internal_error ("check_dimension(): Bad array reference");
4262 }
4263
4264 return true;
4265 }
4266
4267
4268 /* Compare an array reference with an array specification. */
4269
4270 static bool
4271 compare_spec_to_ref (gfc_array_ref *ar)
4272 {
4273 gfc_array_spec *as;
4274 int i;
4275
4276 as = ar->as;
4277 i = as->rank - 1;
4278 /* TODO: Full array sections are only allowed as actual parameters. */
4279 if (as->type == AS_ASSUMED_SIZE
4280 && (/*ar->type == AR_FULL
4281 ||*/ (ar->type == AR_SECTION
4282 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4283 {
4284 gfc_error ("Rightmost upper bound of assumed size array section "
4285 "not specified at %L", &ar->where);
4286 return false;
4287 }
4288
4289 if (ar->type == AR_FULL)
4290 return true;
4291
4292 if (as->rank != ar->dimen)
4293 {
4294 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4295 &ar->where, ar->dimen, as->rank);
4296 return false;
4297 }
4298
4299 /* ar->codimen == 0 is a local array. */
4300 if (as->corank != ar->codimen && ar->codimen != 0)
4301 {
4302 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4303 &ar->where, ar->codimen, as->corank);
4304 return false;
4305 }
4306
4307 for (i = 0; i < as->rank; i++)
4308 if (!check_dimension (i, ar, as))
4309 return false;
4310
4311 /* Local access has no coarray spec. */
4312 if (ar->codimen != 0)
4313 for (i = as->rank; i < as->rank + as->corank; i++)
4314 {
4315 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4316 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4317 {
4318 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4319 i + 1 - as->rank, &ar->where);
4320 return false;
4321 }
4322 if (!check_dimension (i, ar, as))
4323 return false;
4324 }
4325
4326 return true;
4327 }
4328
4329
4330 /* Resolve one part of an array index. */
4331
4332 static bool
4333 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4334 int force_index_integer_kind)
4335 {
4336 gfc_typespec ts;
4337
4338 if (index == NULL)
4339 return true;
4340
4341 if (!gfc_resolve_expr (index))
4342 return false;
4343
4344 if (check_scalar && index->rank != 0)
4345 {
4346 gfc_error ("Array index at %L must be scalar", &index->where);
4347 return false;
4348 }
4349
4350 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4351 {
4352 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4353 &index->where, gfc_basic_typename (index->ts.type));
4354 return false;
4355 }
4356
4357 if (index->ts.type == BT_REAL)
4358 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4359 &index->where))
4360 return false;
4361
4362 if ((index->ts.kind != gfc_index_integer_kind
4363 && force_index_integer_kind)
4364 || index->ts.type != BT_INTEGER)
4365 {
4366 gfc_clear_ts (&ts);
4367 ts.type = BT_INTEGER;
4368 ts.kind = gfc_index_integer_kind;
4369
4370 gfc_convert_type_warn (index, &ts, 2, 0);
4371 }
4372
4373 return true;
4374 }
4375
4376 /* Resolve one part of an array index. */
4377
4378 bool
4379 gfc_resolve_index (gfc_expr *index, int check_scalar)
4380 {
4381 return gfc_resolve_index_1 (index, check_scalar, 1);
4382 }
4383
4384 /* Resolve a dim argument to an intrinsic function. */
4385
4386 bool
4387 gfc_resolve_dim_arg (gfc_expr *dim)
4388 {
4389 if (dim == NULL)
4390 return true;
4391
4392 if (!gfc_resolve_expr (dim))
4393 return false;
4394
4395 if (dim->rank != 0)
4396 {
4397 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4398 return false;
4399
4400 }
4401
4402 if (dim->ts.type != BT_INTEGER)
4403 {
4404 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4405 return false;
4406 }
4407
4408 if (dim->ts.kind != gfc_index_integer_kind)
4409 {
4410 gfc_typespec ts;
4411
4412 gfc_clear_ts (&ts);
4413 ts.type = BT_INTEGER;
4414 ts.kind = gfc_index_integer_kind;
4415
4416 gfc_convert_type_warn (dim, &ts, 2, 0);
4417 }
4418
4419 return true;
4420 }
4421
4422 /* Given an expression that contains array references, update those array
4423 references to point to the right array specifications. While this is
4424 filled in during matching, this information is difficult to save and load
4425 in a module, so we take care of it here.
4426
4427 The idea here is that the original array reference comes from the
4428 base symbol. We traverse the list of reference structures, setting
4429 the stored reference to references. Component references can
4430 provide an additional array specification. */
4431
4432 static void
4433 find_array_spec (gfc_expr *e)
4434 {
4435 gfc_array_spec *as;
4436 gfc_component *c;
4437 gfc_ref *ref;
4438
4439 if (e->symtree->n.sym->ts.type == BT_CLASS)
4440 as = CLASS_DATA (e->symtree->n.sym)->as;
4441 else
4442 as = e->symtree->n.sym->as;
4443
4444 for (ref = e->ref; ref; ref = ref->next)
4445 switch (ref->type)
4446 {
4447 case REF_ARRAY:
4448 if (as == NULL)
4449 gfc_internal_error ("find_array_spec(): Missing spec");
4450
4451 ref->u.ar.as = as;
4452 as = NULL;
4453 break;
4454
4455 case REF_COMPONENT:
4456 c = ref->u.c.component;
4457 if (c->attr.dimension)
4458 {
4459 if (as != NULL)
4460 gfc_internal_error ("find_array_spec(): unused as(1)");
4461 as = c->as;
4462 }
4463
4464 break;
4465
4466 case REF_SUBSTRING:
4467 break;
4468 }
4469
4470 if (as != NULL)
4471 gfc_internal_error ("find_array_spec(): unused as(2)");
4472 }
4473
4474
4475 /* Resolve an array reference. */
4476
4477 static bool
4478 resolve_array_ref (gfc_array_ref *ar)
4479 {
4480 int i, check_scalar;
4481 gfc_expr *e;
4482
4483 for (i = 0; i < ar->dimen + ar->codimen; i++)
4484 {
4485 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4486
4487 /* Do not force gfc_index_integer_kind for the start. We can
4488 do fine with any integer kind. This avoids temporary arrays
4489 created for indexing with a vector. */
4490 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4491 return false;
4492 if (!gfc_resolve_index (ar->end[i], check_scalar))
4493 return false;
4494 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4495 return false;
4496
4497 e = ar->start[i];
4498
4499 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4500 switch (e->rank)
4501 {
4502 case 0:
4503 ar->dimen_type[i] = DIMEN_ELEMENT;
4504 break;
4505
4506 case 1:
4507 ar->dimen_type[i] = DIMEN_VECTOR;
4508 if (e->expr_type == EXPR_VARIABLE
4509 && e->symtree->n.sym->ts.type == BT_DERIVED)
4510 ar->start[i] = gfc_get_parentheses (e);
4511 break;
4512
4513 default:
4514 gfc_error ("Array index at %L is an array of rank %d",
4515 &ar->c_where[i], e->rank);
4516 return false;
4517 }
4518
4519 /* Fill in the upper bound, which may be lower than the
4520 specified one for something like a(2:10:5), which is
4521 identical to a(2:7:5). Only relevant for strides not equal
4522 to one. Don't try a division by zero. */
4523 if (ar->dimen_type[i] == DIMEN_RANGE
4524 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4525 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4526 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4527 {
4528 mpz_t size, end;
4529
4530 if (gfc_ref_dimen_size (ar, i, &size, &end))
4531 {
4532 if (ar->end[i] == NULL)
4533 {
4534 ar->end[i] =
4535 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4536 &ar->where);
4537 mpz_set (ar->end[i]->value.integer, end);
4538 }
4539 else if (ar->end[i]->ts.type == BT_INTEGER
4540 && ar->end[i]->expr_type == EXPR_CONSTANT)
4541 {
4542 mpz_set (ar->end[i]->value.integer, end);
4543 }
4544 else
4545 gcc_unreachable ();
4546
4547 mpz_clear (size);
4548 mpz_clear (end);
4549 }
4550 }
4551 }
4552
4553 if (ar->type == AR_FULL)
4554 {
4555 if (ar->as->rank == 0)
4556 ar->type = AR_ELEMENT;
4557
4558 /* Make sure array is the same as array(:,:), this way
4559 we don't need to special case all the time. */
4560 ar->dimen = ar->as->rank;
4561 for (i = 0; i < ar->dimen; i++)
4562 {
4563 ar->dimen_type[i] = DIMEN_RANGE;
4564
4565 gcc_assert (ar->start[i] == NULL);
4566 gcc_assert (ar->end[i] == NULL);
4567 gcc_assert (ar->stride[i] == NULL);
4568 }
4569 }
4570
4571 /* If the reference type is unknown, figure out what kind it is. */
4572
4573 if (ar->type == AR_UNKNOWN)
4574 {
4575 ar->type = AR_ELEMENT;
4576 for (i = 0; i < ar->dimen; i++)
4577 if (ar->dimen_type[i] == DIMEN_RANGE
4578 || ar->dimen_type[i] == DIMEN_VECTOR)
4579 {
4580 ar->type = AR_SECTION;
4581 break;
4582 }
4583 }
4584
4585 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
4586 return false;
4587
4588 if (ar->as->corank && ar->codimen == 0)
4589 {
4590 int n;
4591 ar->codimen = ar->as->corank;
4592 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4593 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4594 }
4595
4596 return true;
4597 }
4598
4599
4600 static bool
4601 resolve_substring (gfc_ref *ref)
4602 {
4603 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4604
4605 if (ref->u.ss.start != NULL)
4606 {
4607 if (!gfc_resolve_expr (ref->u.ss.start))
4608 return false;
4609
4610 if (ref->u.ss.start->ts.type != BT_INTEGER)
4611 {
4612 gfc_error ("Substring start index at %L must be of type INTEGER",
4613 &ref->u.ss.start->where);
4614 return false;
4615 }
4616
4617 if (ref->u.ss.start->rank != 0)
4618 {
4619 gfc_error ("Substring start index at %L must be scalar",
4620 &ref->u.ss.start->where);
4621 return false;
4622 }
4623
4624 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4625 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4626 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4627 {
4628 gfc_error ("Substring start index at %L is less than one",
4629 &ref->u.ss.start->where);
4630 return false;
4631 }
4632 }
4633
4634 if (ref->u.ss.end != NULL)
4635 {
4636 if (!gfc_resolve_expr (ref->u.ss.end))
4637 return false;
4638
4639 if (ref->u.ss.end->ts.type != BT_INTEGER)
4640 {
4641 gfc_error ("Substring end index at %L must be of type INTEGER",
4642 &ref->u.ss.end->where);
4643 return false;
4644 }
4645
4646 if (ref->u.ss.end->rank != 0)
4647 {
4648 gfc_error ("Substring end index at %L must be scalar",
4649 &ref->u.ss.end->where);
4650 return false;
4651 }
4652
4653 if (ref->u.ss.length != NULL
4654 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4655 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4656 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4657 {
4658 gfc_error ("Substring end index at %L exceeds the string length",
4659 &ref->u.ss.start->where);
4660 return false;
4661 }
4662
4663 if (compare_bound_mpz_t (ref->u.ss.end,
4664 gfc_integer_kinds[k].huge) == CMP_GT
4665 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4666 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4667 {
4668 gfc_error ("Substring end index at %L is too large",
4669 &ref->u.ss.end->where);
4670 return false;
4671 }
4672 }
4673
4674 return true;
4675 }
4676
4677
4678 /* This function supplies missing substring charlens. */
4679
4680 void
4681 gfc_resolve_substring_charlen (gfc_expr *e)
4682 {
4683 gfc_ref *char_ref;
4684 gfc_expr *start, *end;
4685 gfc_typespec *ts = NULL;
4686
4687 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4688 {
4689 if (char_ref->type == REF_SUBSTRING)
4690 break;
4691 if (char_ref->type == REF_COMPONENT)
4692 ts = &char_ref->u.c.component->ts;
4693 }
4694
4695 if (!char_ref)
4696 return;
4697
4698 gcc_assert (char_ref->next == NULL);
4699
4700 if (e->ts.u.cl)
4701 {
4702 if (e->ts.u.cl->length)
4703 gfc_free_expr (e->ts.u.cl->length);
4704 else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
4705 return;
4706 }
4707
4708 e->ts.type = BT_CHARACTER;
4709 e->ts.kind = gfc_default_character_kind;
4710
4711 if (!e->ts.u.cl)
4712 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4713
4714 if (char_ref->u.ss.start)
4715 start = gfc_copy_expr (char_ref->u.ss.start);
4716 else
4717 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4718
4719 if (char_ref->u.ss.end)
4720 end = gfc_copy_expr (char_ref->u.ss.end);
4721 else if (e->expr_type == EXPR_VARIABLE)
4722 {
4723 if (!ts)
4724 ts = &e->symtree->n.sym->ts;
4725 end = gfc_copy_expr (ts->u.cl->length);
4726 }
4727 else
4728 end = NULL;
4729
4730 if (!start || !end)
4731 {
4732 gfc_free_expr (start);
4733 gfc_free_expr (end);
4734 return;
4735 }
4736
4737 /* Length = (end - start + 1). */
4738 e->ts.u.cl->length = gfc_subtract (end, start);
4739 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4740 gfc_get_int_expr (gfc_default_integer_kind,
4741 NULL, 1));
4742
4743 /* F2008, 6.4.1: Both the starting point and the ending point shall
4744 be within the range 1, 2, ..., n unless the starting point exceeds
4745 the ending point, in which case the substring has length zero. */
4746
4747 if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
4748 mpz_set_si (e->ts.u.cl->length->value.integer, 0);
4749
4750 e->ts.u.cl->length->ts.type = BT_INTEGER;
4751 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4752
4753 /* Make sure that the length is simplified. */
4754 gfc_simplify_expr (e->ts.u.cl->length, 1);
4755 gfc_resolve_expr (e->ts.u.cl->length);
4756 }
4757
4758
4759 /* Resolve subtype references. */
4760
4761 static bool
4762 resolve_ref (gfc_expr *expr)
4763 {
4764 int current_part_dimension, n_components, seen_part_dimension;
4765 gfc_ref *ref;
4766
4767 for (ref = expr->ref; ref; ref = ref->next)
4768 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4769 {
4770 find_array_spec (expr);
4771 break;
4772 }
4773
4774 for (ref = expr->ref; ref; ref = ref->next)
4775 switch (ref->type)
4776 {
4777 case REF_ARRAY:
4778 if (!resolve_array_ref (&ref->u.ar))
4779 return false;
4780 break;
4781
4782 case REF_COMPONENT:
4783 break;
4784
4785 case REF_SUBSTRING:
4786 if (!resolve_substring (ref))
4787 return false;
4788 break;
4789 }
4790
4791 /* Check constraints on part references. */
4792
4793 current_part_dimension = 0;
4794 seen_part_dimension = 0;
4795 n_components = 0;
4796
4797 for (ref = expr->ref; ref; ref = ref->next)
4798 {
4799 switch (ref->type)
4800 {
4801 case REF_ARRAY:
4802 switch (ref->u.ar.type)
4803 {
4804 case AR_FULL:
4805 /* Coarray scalar. */
4806 if (ref->u.ar.as->rank == 0)
4807 {
4808 current_part_dimension = 0;
4809 break;
4810 }
4811 /* Fall through. */
4812 case AR_SECTION:
4813 current_part_dimension = 1;
4814 break;
4815
4816 case AR_ELEMENT:
4817 current_part_dimension = 0;
4818 break;
4819
4820 case AR_UNKNOWN:
4821 gfc_internal_error ("resolve_ref(): Bad array reference");
4822 }
4823
4824 break;
4825
4826 case REF_COMPONENT:
4827 if (current_part_dimension || seen_part_dimension)
4828 {
4829 /* F03:C614. */
4830 if (ref->u.c.component->attr.pointer
4831 || ref->u.c.component->attr.proc_pointer
4832 || (ref->u.c.component->ts.type == BT_CLASS
4833 && CLASS_DATA (ref->u.c.component)->attr.pointer))
4834 {
4835 gfc_error ("Component to the right of a part reference "
4836 "with nonzero rank must not have the POINTER "
4837 "attribute at %L", &expr->where);
4838 return false;
4839 }
4840 else if (ref->u.c.component->attr.allocatable
4841 || (ref->u.c.component->ts.type == BT_CLASS
4842 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
4843
4844 {
4845 gfc_error ("Component to the right of a part reference "
4846 "with nonzero rank must not have the ALLOCATABLE "
4847 "attribute at %L", &expr->where);
4848 return false;
4849 }
4850 }
4851
4852 n_components++;
4853 break;
4854
4855 case REF_SUBSTRING:
4856 break;
4857 }
4858
4859 if (((ref->type == REF_COMPONENT && n_components > 1)
4860 || ref->next == NULL)
4861 && current_part_dimension
4862 && seen_part_dimension)
4863 {
4864 gfc_error ("Two or more part references with nonzero rank must "
4865 "not be specified at %L", &expr->where);
4866 return false;
4867 }
4868
4869 if (ref->type == REF_COMPONENT)
4870 {
4871 if (current_part_dimension)
4872 seen_part_dimension = 1;
4873
4874 /* reset to make sure */
4875 current_part_dimension = 0;
4876 }
4877 }
4878
4879 return true;
4880 }
4881
4882
4883 /* Given an expression, determine its shape. This is easier than it sounds.
4884 Leaves the shape array NULL if it is not possible to determine the shape. */
4885
4886 static void
4887 expression_shape (gfc_expr *e)
4888 {
4889 mpz_t array[GFC_MAX_DIMENSIONS];
4890 int i;
4891
4892 if (e->rank <= 0 || e->shape != NULL)
4893 return;
4894
4895 for (i = 0; i < e->rank; i++)
4896 if (!gfc_array_dimen_size (e, i, &array[i]))
4897 goto fail;
4898
4899 e->shape = gfc_get_shape (e->rank);
4900
4901 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4902
4903 return;
4904
4905 fail:
4906 for (i--; i >= 0; i--)
4907 mpz_clear (array[i]);
4908 }
4909
4910
4911 /* Given a variable expression node, compute the rank of the expression by
4912 examining the base symbol and any reference structures it may have. */
4913
4914 void
4915 expression_rank (gfc_expr *e)
4916 {
4917 gfc_ref *ref;
4918 int i, rank;
4919
4920 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4921 could lead to serious confusion... */
4922 gcc_assert (e->expr_type != EXPR_COMPCALL);
4923
4924 if (e->ref == NULL)
4925 {
4926 if (e->expr_type == EXPR_ARRAY)
4927 goto done;
4928 /* Constructors can have a rank different from one via RESHAPE(). */
4929
4930 if (e->symtree == NULL)
4931 {
4932 e->rank = 0;
4933 goto done;
4934 }
4935
4936 e->rank = (e->symtree->n.sym->as == NULL)
4937 ? 0 : e->symtree->n.sym->as->rank;
4938 goto done;
4939 }
4940
4941 rank = 0;
4942
4943 for (ref = e->ref; ref; ref = ref->next)
4944 {
4945 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4946 && ref->u.c.component->attr.function && !ref->next)
4947 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4948
4949 if (ref->type != REF_ARRAY)
4950 continue;
4951
4952 if (ref->u.ar.type == AR_FULL)
4953 {
4954 rank = ref->u.ar.as->rank;
4955 break;
4956 }
4957
4958 if (ref->u.ar.type == AR_SECTION)
4959 {
4960 /* Figure out the rank of the section. */
4961 if (rank != 0)
4962 gfc_internal_error ("expression_rank(): Two array specs");
4963
4964 for (i = 0; i < ref->u.ar.dimen; i++)
4965 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4966 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4967 rank++;
4968
4969 break;
4970 }
4971 }
4972
4973 e->rank = rank;
4974
4975 done:
4976 expression_shape (e);
4977 }
4978
4979
4980 static void
4981 add_caf_get_intrinsic (gfc_expr *e)
4982 {
4983 gfc_expr *wrapper, *tmp_expr;
4984 gfc_ref *ref;
4985 int n;
4986
4987 for (ref = e->ref; ref; ref = ref->next)
4988 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4989 break;
4990 if (ref == NULL)
4991 return;
4992
4993 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
4994 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
4995 return;
4996
4997 tmp_expr = XCNEW (gfc_expr);
4998 *tmp_expr = *e;
4999 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
5000 "caf_get", tmp_expr->where, 1, tmp_expr);
5001 wrapper->ts = e->ts;
5002 wrapper->rank = e->rank;
5003 if (e->rank)
5004 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
5005 *e = *wrapper;
5006 free (wrapper);
5007 }
5008
5009
5010 static void
5011 remove_caf_get_intrinsic (gfc_expr *e)
5012 {
5013 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym
5014 && e->value.function.isym->id == GFC_ISYM_CAF_GET);
5015 gfc_expr *e2 = e->value.function.actual->expr;
5016 e->value.function.actual->expr = NULL;
5017 gfc_free_actual_arglist (e->value.function.actual);
5018 gfc_free_shape (&e->shape, e->rank);
5019 *e = *e2;
5020 free (e2);
5021 }
5022
5023
5024 /* Resolve a variable expression. */
5025
5026 static bool
5027 resolve_variable (gfc_expr *e)
5028 {
5029 gfc_symbol *sym;
5030 bool t;
5031
5032 t = true;
5033
5034 if (e->symtree == NULL)
5035 return false;
5036 sym = e->symtree->n.sym;
5037
5038 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
5039 as ts.type is set to BT_ASSUMED in resolve_symbol. */
5040 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
5041 {
5042 if (!actual_arg || inquiry_argument)
5043 {
5044 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
5045 "be used as actual argument", sym->name, &e->where);
5046 return false;
5047 }
5048 }
5049 /* TS 29113, 407b. */
5050 else if (e->ts.type == BT_ASSUMED)
5051 {
5052 if (!actual_arg)
5053 {
5054 gfc_error ("Assumed-type variable %s at %L may only be used "
5055 "as actual argument", sym->name, &e->where);
5056 return false;
5057 }
5058 else if (inquiry_argument && !first_actual_arg)
5059 {
5060 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5061 for all inquiry functions in resolve_function; the reason is
5062 that the function-name resolution happens too late in that
5063 function. */
5064 gfc_error ("Assumed-type variable %s at %L as actual argument to "
5065 "an inquiry function shall be the first argument",
5066 sym->name, &e->where);
5067 return false;
5068 }
5069 }
5070 /* TS 29113, C535b. */
5071 else if ((sym->ts.type == BT_CLASS && sym->attr.class_ok
5072 && CLASS_DATA (sym)->as
5073 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5074 || (sym->ts.type != BT_CLASS && sym->as
5075 && sym->as->type == AS_ASSUMED_RANK))
5076 {
5077 if (!actual_arg)
5078 {
5079 gfc_error ("Assumed-rank variable %s at %L may only be used as "
5080 "actual argument", sym->name, &e->where);
5081 return false;
5082 }
5083 else if (inquiry_argument && !first_actual_arg)
5084 {
5085 /* FIXME: It doesn't work reliably as inquiry_argument is not set
5086 for all inquiry functions in resolve_function; the reason is
5087 that the function-name resolution happens too late in that
5088 function. */
5089 gfc_error ("Assumed-rank variable %s at %L as actual argument "
5090 "to an inquiry function shall be the first argument",
5091 sym->name, &e->where);
5092 return false;
5093 }
5094 }
5095
5096 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
5097 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5098 && e->ref->next == NULL))
5099 {
5100 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
5101 "a subobject reference", sym->name, &e->ref->u.ar.where);
5102 return false;
5103 }
5104 /* TS 29113, 407b. */
5105 else if (e->ts.type == BT_ASSUMED && e->ref
5106 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5107 && e->ref->next == NULL))
5108 {
5109 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
5110 "reference", sym->name, &e->ref->u.ar.where);
5111 return false;
5112 }
5113
5114 /* TS 29113, C535b. */
5115 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
5116 && CLASS_DATA (sym)->as
5117 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
5118 || (sym->ts.type != BT_CLASS && sym->as
5119 && sym->as->type == AS_ASSUMED_RANK))
5120 && e->ref
5121 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
5122 && e->ref->next == NULL))
5123 {
5124 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
5125 "reference", sym->name, &e->ref->u.ar.where);
5126 return false;
5127 }
5128
5129 /* For variables that are used in an associate (target => object) where
5130 the object's basetype is array valued while the target is scalar,
5131 the ts' type of the component refs is still array valued, which
5132 can't be translated that way. */
5133 if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
5134 && sym->assoc->target->ts.type == BT_CLASS
5135 && CLASS_DATA (sym->assoc->target)->as)
5136 {
5137 gfc_ref *ref = e->ref;
5138 while (ref)
5139 {
5140 switch (ref->type)
5141 {
5142 case REF_COMPONENT:
5143 ref->u.c.sym = sym->ts.u.derived;
5144 /* Stop the loop. */
5145 ref = NULL;
5146 break;
5147 default:
5148 ref = ref->next;
5149 break;
5150 }
5151 }
5152 }
5153
5154 /* If this is an associate-name, it may be parsed with an array reference
5155 in error even though the target is scalar. Fail directly in this case.
5156 TODO Understand why class scalar expressions must be excluded. */
5157 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
5158 {
5159 if (sym->ts.type == BT_CLASS)
5160 gfc_fix_class_refs (e);
5161 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
5162 return false;
5163 }
5164
5165 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
5166 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
5167
5168 /* On the other hand, the parser may not have known this is an array;
5169 in this case, we have to add a FULL reference. */
5170 if (sym->assoc && sym->attr.dimension && !e->ref)
5171 {
5172 e->ref = gfc_get_ref ();
5173 e->ref->type = REF_ARRAY;
5174 e->ref->u.ar.type = AR_FULL;
5175 e->ref->u.ar.dimen = 0;
5176 }
5177
5178 /* Like above, but for class types, where the checking whether an array
5179 ref is present is more complicated. Furthermore make sure not to add
5180 the full array ref to _vptr or _len refs. */
5181 if (sym->assoc && sym->ts.type == BT_CLASS
5182 && CLASS_DATA (sym)->attr.dimension
5183 && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
5184 {
5185 gfc_ref *ref, *newref;
5186
5187 newref = gfc_get_ref ();
5188 newref->type = REF_ARRAY;
5189 newref->u.ar.type = AR_FULL;
5190 newref->u.ar.dimen = 0;
5191 /* Because this is an associate var and the first ref either is a ref to
5192 the _data component or not, no traversal of the ref chain is
5193 needed. The array ref needs to be inserted after the _data ref,
5194 or when that is not present, which may happend for polymorphic
5195 types, then at the first position. */
5196 ref = e->ref;
5197 if (!ref)
5198 e->ref = newref;
5199 else if (ref->type == REF_COMPONENT
5200 && strcmp ("_data", ref->u.c.component->name) == 0)
5201 {
5202 if (!ref->next || ref->next->type != REF_ARRAY)
5203 {
5204 newref->next = ref->next;
5205 ref->next = newref;
5206 }
5207 else
5208 /* Array ref present already. */
5209 gfc_free_ref_list (newref);
5210 }
5211 else if (ref->type == REF_ARRAY)
5212 /* Array ref present already. */
5213 gfc_free_ref_list (newref);
5214 else
5215 {
5216 newref->next = ref;
5217 e->ref = newref;
5218 }
5219 }
5220
5221 if (e->ref && !resolve_ref (e))
5222 return false;
5223
5224 if (sym->attr.flavor == FL_PROCEDURE
5225 && (!sym->attr.function
5226 || (sym->attr.function && sym->result
5227 && sym->result->attr.proc_pointer
5228 && !sym->result->attr.function)))
5229 {
5230 e->ts.type = BT_PROCEDURE;
5231 goto resolve_procedure;
5232 }
5233
5234 if (sym->ts.type != BT_UNKNOWN)
5235 gfc_variable_attr (e, &e->ts);
5236 else if (sym->attr.flavor == FL_PROCEDURE
5237 && sym->attr.function && sym->result
5238 && sym->result->ts.type != BT_UNKNOWN
5239 && sym->result->attr.proc_pointer)
5240 e->ts = sym->result->ts;
5241 else
5242 {
5243 /* Must be a simple variable reference. */
5244 if (!gfc_set_default_type (sym, 1, sym->ns))
5245 return false;
5246 e->ts = sym->ts;
5247 }
5248
5249 if (check_assumed_size_reference (sym, e))
5250 return false;
5251
5252 /* Deal with forward references to entries during gfc_resolve_code, to
5253 satisfy, at least partially, 12.5.2.5. */
5254 if (gfc_current_ns->entries
5255 && current_entry_id == sym->entry_id
5256 && cs_base
5257 && cs_base->current
5258 && cs_base->current->op != EXEC_ENTRY)
5259 {
5260 gfc_entry_list *entry;
5261 gfc_formal_arglist *formal;
5262 int n;
5263 bool seen, saved_specification_expr;
5264
5265 /* If the symbol is a dummy... */
5266 if (sym->attr.dummy && sym->ns == gfc_current_ns)
5267 {
5268 entry = gfc_current_ns->entries;
5269 seen = false;
5270
5271 /* ...test if the symbol is a parameter of previous entries. */
5272 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5273 for (formal = entry->sym->formal; formal; formal = formal->next)
5274 {
5275 if (formal->sym && sym->name == formal->sym->name)
5276 {
5277 seen = true;
5278 break;
5279 }
5280 }
5281
5282 /* If it has not been seen as a dummy, this is an error. */
5283 if (!seen)
5284 {
5285 if (specification_expr)
5286 gfc_error ("Variable %qs, used in a specification expression"
5287 ", is referenced at %L before the ENTRY statement "
5288 "in which it is a parameter",
5289 sym->name, &cs_base->current->loc);
5290 else
5291 gfc_error ("Variable %qs is used at %L before the ENTRY "
5292 "statement in which it is a parameter",
5293 sym->name, &cs_base->current->loc);
5294 t = false;
5295 }
5296 }
5297
5298 /* Now do the same check on the specification expressions. */
5299 saved_specification_expr = specification_expr;
5300 specification_expr = true;
5301 if (sym->ts.type == BT_CHARACTER
5302 && !gfc_resolve_expr (sym->ts.u.cl->length))
5303 t = false;
5304
5305 if (sym->as)
5306 for (n = 0; n < sym->as->rank; n++)
5307 {
5308 if (!gfc_resolve_expr (sym->as->lower[n]))
5309 t = false;
5310 if (!gfc_resolve_expr (sym->as->upper[n]))
5311 t = false;
5312 }
5313 specification_expr = saved_specification_expr;
5314
5315 if (t)
5316 /* Update the symbol's entry level. */
5317 sym->entry_id = current_entry_id + 1;
5318 }
5319
5320 /* If a symbol has been host_associated mark it. This is used latter,
5321 to identify if aliasing is possible via host association. */
5322 if (sym->attr.flavor == FL_VARIABLE
5323 && gfc_current_ns->parent
5324 && (gfc_current_ns->parent == sym->ns
5325 || (gfc_current_ns->parent->parent
5326 && gfc_current_ns->parent->parent == sym->ns)))
5327 sym->attr.host_assoc = 1;
5328
5329 if (gfc_current_ns->proc_name
5330 && sym->attr.dimension
5331 && (sym->ns != gfc_current_ns
5332 || sym->attr.use_assoc
5333 || sym->attr.in_common))
5334 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
5335
5336 resolve_procedure:
5337 if (t && !resolve_procedure_expression (e))
5338 t = false;
5339
5340 /* F2008, C617 and C1229. */
5341 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5342 && gfc_is_coindexed (e))
5343 {
5344 gfc_ref *ref, *ref2 = NULL;
5345
5346 for (ref = e->ref; ref; ref = ref->next)
5347 {
5348 if (ref->type == REF_COMPONENT)
5349 ref2 = ref;
5350 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5351 break;
5352 }
5353
5354 for ( ; ref; ref = ref->next)
5355 if (ref->type == REF_COMPONENT)
5356 break;
5357
5358 /* Expression itself is not coindexed object. */
5359 if (ref && e->ts.type == BT_CLASS)
5360 {
5361 gfc_error ("Polymorphic subobject of coindexed object at %L",
5362 &e->where);
5363 t = false;
5364 }
5365
5366 /* Expression itself is coindexed object. */
5367 if (ref == NULL)
5368 {
5369 gfc_component *c;
5370 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5371 for ( ; c; c = c->next)
5372 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5373 {
5374 gfc_error ("Coindexed object with polymorphic allocatable "
5375 "subcomponent at %L", &e->where);
5376 t = false;
5377 break;
5378 }
5379 }
5380 }
5381
5382 if (t)
5383 expression_rank (e);
5384
5385 if (t && flag_coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e))
5386 add_caf_get_intrinsic (e);
5387
5388 return t;
5389 }
5390
5391
5392 /* Checks to see that the correct symbol has been host associated.
5393 The only situation where this arises is that in which a twice
5394 contained function is parsed after the host association is made.
5395 Therefore, on detecting this, change the symbol in the expression
5396 and convert the array reference into an actual arglist if the old
5397 symbol is a variable. */
5398 static bool
5399 check_host_association (gfc_expr *e)
5400 {
5401 gfc_symbol *sym, *old_sym;
5402 gfc_symtree *st;
5403 int n;
5404 gfc_ref *ref;
5405 gfc_actual_arglist *arg, *tail = NULL;
5406 bool retval = e->expr_type == EXPR_FUNCTION;
5407
5408 /* If the expression is the result of substitution in
5409 interface.c(gfc_extend_expr) because there is no way in
5410 which the host association can be wrong. */
5411 if (e->symtree == NULL
5412 || e->symtree->n.sym == NULL
5413 || e->user_operator)
5414 return retval;
5415
5416 old_sym = e->symtree->n.sym;
5417
5418 if (gfc_current_ns->parent
5419 && old_sym->ns != gfc_current_ns)
5420 {
5421 /* Use the 'USE' name so that renamed module symbols are
5422 correctly handled. */
5423 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5424
5425 if (sym && old_sym != sym
5426 && sym->ts.type == old_sym->ts.type
5427 && sym->attr.flavor == FL_PROCEDURE
5428 && sym->attr.contained)
5429 {
5430 /* Clear the shape, since it might not be valid. */
5431 gfc_free_shape (&e->shape, e->rank);
5432
5433 /* Give the expression the right symtree! */
5434 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5435 gcc_assert (st != NULL);
5436
5437 if (old_sym->attr.flavor == FL_PROCEDURE
5438 || e->expr_type == EXPR_FUNCTION)
5439 {
5440 /* Original was function so point to the new symbol, since
5441 the actual argument list is already attached to the
5442 expression. */
5443 e->value.function.esym = NULL;
5444 e->symtree = st;
5445 }
5446 else
5447 {
5448 /* Original was variable so convert array references into
5449 an actual arglist. This does not need any checking now
5450 since resolve_function will take care of it. */
5451 e->value.function.actual = NULL;
5452 e->expr_type = EXPR_FUNCTION;
5453 e->symtree = st;
5454
5455 /* Ambiguity will not arise if the array reference is not
5456 the last reference. */
5457 for (ref = e->ref; ref; ref = ref->next)
5458 if (ref->type == REF_ARRAY && ref->next == NULL)
5459 break;
5460
5461 gcc_assert (ref->type == REF_ARRAY);
5462
5463 /* Grab the start expressions from the array ref and
5464 copy them into actual arguments. */
5465 for (n = 0; n < ref->u.ar.dimen; n++)
5466 {
5467 arg = gfc_get_actual_arglist ();
5468 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5469 if (e->value.function.actual == NULL)
5470 tail = e->value.function.actual = arg;
5471 else
5472 {
5473 tail->next = arg;
5474 tail = arg;
5475 }
5476 }
5477
5478 /* Dump the reference list and set the rank. */
5479 gfc_free_ref_list (e->ref);
5480 e->ref = NULL;
5481 e->rank = sym->as ? sym->as->rank : 0;
5482 }
5483
5484 gfc_resolve_expr (e);
5485 sym->refs++;
5486 }
5487 }
5488 /* This might have changed! */
5489 return e->expr_type == EXPR_FUNCTION;
5490 }
5491
5492
5493 static void
5494 gfc_resolve_character_operator (gfc_expr *e)
5495 {
5496 gfc_expr *op1 = e->value.op.op1;
5497 gfc_expr *op2 = e->value.op.op2;
5498 gfc_expr *e1 = NULL;
5499 gfc_expr *e2 = NULL;
5500
5501 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5502
5503 if (op1->ts.u.cl && op1->ts.u.cl->length)
5504 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5505 else if (op1->expr_type == EXPR_CONSTANT)
5506 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5507 op1->value.character.length);
5508
5509 if (op2->ts.u.cl && op2->ts.u.cl->length)
5510 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5511 else if (op2->expr_type == EXPR_CONSTANT)
5512 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5513 op2->value.character.length);
5514
5515 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5516
5517 if (!e1 || !e2)
5518 {
5519 gfc_free_expr (e1);
5520 gfc_free_expr (e2);
5521
5522 return;
5523 }
5524
5525 e->ts.u.cl->length = gfc_add (e1, e2);
5526 e->ts.u.cl->length->ts.type = BT_INTEGER;
5527 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5528 gfc_simplify_expr (e->ts.u.cl->length, 0);
5529 gfc_resolve_expr (e->ts.u.cl->length);
5530
5531 return;
5532 }
5533
5534
5535 /* Ensure that an character expression has a charlen and, if possible, a
5536 length expression. */
5537
5538 static void
5539 fixup_charlen (gfc_expr *e)
5540 {
5541 /* The cases fall through so that changes in expression type and the need
5542 for multiple fixes are picked up. In all circumstances, a charlen should
5543 be available for the middle end to hang a backend_decl on. */
5544 switch (e->expr_type)
5545 {
5546 case EXPR_OP:
5547 gfc_resolve_character_operator (e);
5548 /* FALLTHRU */
5549
5550 case EXPR_ARRAY:
5551 if (e->expr_type == EXPR_ARRAY)
5552 gfc_resolve_character_array_constructor (e);
5553 /* FALLTHRU */
5554
5555 case EXPR_SUBSTRING:
5556 if (!e->ts.u.cl && e->ref)
5557 gfc_resolve_substring_charlen (e);
5558 /* FALLTHRU */
5559
5560 default:
5561 if (!e->ts.u.cl)
5562 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5563
5564 break;
5565 }
5566 }
5567
5568
5569 /* Update an actual argument to include the passed-object for type-bound
5570 procedures at the right position. */
5571
5572 static gfc_actual_arglist*
5573 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5574 const char *name)
5575 {
5576 gcc_assert (argpos > 0);
5577
5578 if (argpos == 1)
5579 {
5580 gfc_actual_arglist* result;
5581
5582 result = gfc_get_actual_arglist ();
5583 result->expr = po;
5584 result->next = lst;
5585 if (name)
5586 result->name = name;
5587
5588 return result;
5589 }
5590
5591 if (lst)
5592 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5593 else
5594 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5595 return lst;
5596 }
5597
5598
5599 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5600
5601 static gfc_expr*
5602 extract_compcall_passed_object (gfc_expr* e)
5603 {
5604 gfc_expr* po;
5605
5606 gcc_assert (e->expr_type == EXPR_COMPCALL);
5607
5608 if (e->value.compcall.base_object)
5609 po = gfc_copy_expr (e->value.compcall.base_object);
5610 else
5611 {
5612 po = gfc_get_expr ();
5613 po->expr_type = EXPR_VARIABLE;
5614 po->symtree = e->symtree;
5615 po->ref = gfc_copy_ref (e->ref);
5616 po->where = e->where;
5617 }
5618
5619 if (!gfc_resolve_expr (po))
5620 return NULL;
5621
5622 return po;
5623 }
5624
5625
5626 /* Update the arglist of an EXPR_COMPCALL expression to include the
5627 passed-object. */
5628
5629 static bool
5630 update_compcall_arglist (gfc_expr* e)
5631 {
5632 gfc_expr* po;
5633 gfc_typebound_proc* tbp;
5634
5635 tbp = e->value.compcall.tbp;
5636
5637 if (tbp->error)
5638 return false;
5639
5640 po = extract_compcall_passed_object (e);
5641 if (!po)
5642 return false;
5643
5644 if (tbp->nopass || e->value.compcall.ignore_pass)
5645 {
5646 gfc_free_expr (po);
5647 return true;
5648 }
5649
5650 gcc_assert (tbp->pass_arg_num > 0);
5651 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5652 tbp->pass_arg_num,
5653 tbp->pass_arg);
5654
5655 return true;
5656 }
5657
5658
5659 /* Extract the passed object from a PPC call (a copy of it). */
5660
5661 static gfc_expr*
5662 extract_ppc_passed_object (gfc_expr *e)
5663 {
5664 gfc_expr *po;
5665 gfc_ref **ref;
5666
5667 po = gfc_get_expr ();
5668 po->expr_type = EXPR_VARIABLE;
5669 po->symtree = e->symtree;
5670 po->ref = gfc_copy_ref (e->ref);
5671 po->where = e->where;
5672
5673 /* Remove PPC reference. */
5674 ref = &po->ref;
5675 while ((*ref)->next)
5676 ref = &(*ref)->next;
5677 gfc_free_ref_list (*ref);
5678 *ref = NULL;
5679
5680 if (!gfc_resolve_expr (po))
5681 return NULL;
5682
5683 return po;
5684 }
5685
5686
5687 /* Update the actual arglist of a procedure pointer component to include the
5688 passed-object. */
5689
5690 static bool
5691 update_ppc_arglist (gfc_expr* e)
5692 {
5693 gfc_expr* po;
5694 gfc_component *ppc;
5695 gfc_typebound_proc* tb;
5696
5697 ppc = gfc_get_proc_ptr_comp (e);
5698 if (!ppc)
5699 return false;
5700
5701 tb = ppc->tb;
5702
5703 if (tb->error)
5704 return false;
5705 else if (tb->nopass)
5706 return true;
5707
5708 po = extract_ppc_passed_object (e);
5709 if (!po)
5710 return false;
5711
5712 /* F08:R739. */
5713 if (po->rank != 0)
5714 {
5715 gfc_error ("Passed-object at %L must be scalar", &e->where);
5716 return false;
5717 }
5718
5719 /* F08:C611. */
5720 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5721 {
5722 gfc_error ("Base object for procedure-pointer component call at %L is of"
5723 " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
5724 return false;
5725 }
5726
5727 gcc_assert (tb->pass_arg_num > 0);
5728 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5729 tb->pass_arg_num,
5730 tb->pass_arg);
5731
5732 return true;
5733 }
5734
5735
5736 /* Check that the object a TBP is called on is valid, i.e. it must not be
5737 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5738
5739 static bool
5740 check_typebound_baseobject (gfc_expr* e)
5741 {
5742 gfc_expr* base;
5743 bool return_value = false;
5744
5745 base = extract_compcall_passed_object (e);
5746 if (!base)
5747 return false;
5748
5749 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5750
5751 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
5752 return false;
5753
5754 /* F08:C611. */
5755 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5756 {
5757 gfc_error ("Base object for type-bound procedure call at %L is of"
5758 " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
5759 goto cleanup;
5760 }
5761
5762 /* F08:C1230. If the procedure called is NOPASS,
5763 the base object must be scalar. */
5764 if (e->value.compcall.tbp->nopass && base->rank != 0)
5765 {
5766 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5767 " be scalar", &e->where);
5768 goto cleanup;
5769 }
5770
5771 return_value = true;
5772
5773 cleanup:
5774 gfc_free_expr (base);
5775 return return_value;
5776 }
5777
5778
5779 /* Resolve a call to a type-bound procedure, either function or subroutine,
5780 statically from the data in an EXPR_COMPCALL expression. The adapted
5781 arglist and the target-procedure symtree are returned. */
5782
5783 static bool
5784 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5785 gfc_actual_arglist** actual)
5786 {
5787 gcc_assert (e->expr_type == EXPR_COMPCALL);
5788 gcc_assert (!e->value.compcall.tbp->is_generic);
5789
5790 /* Update the actual arglist for PASS. */
5791 if (!update_compcall_arglist (e))
5792 return false;
5793
5794 *actual = e->value.compcall.actual;
5795 *target = e->value.compcall.tbp->u.specific;
5796
5797 gfc_free_ref_list (e->ref);
5798 e->ref = NULL;
5799 e->value.compcall.actual = NULL;
5800
5801 /* If we find a deferred typebound procedure, check for derived types
5802 that an overriding typebound procedure has not been missed. */
5803 if (e->value.compcall.name
5804 && !e->value.compcall.tbp->non_overridable
5805 && e->value.compcall.base_object
5806 && e->value.compcall.base_object->ts.type == BT_DERIVED)
5807 {
5808 gfc_symtree *st;
5809 gfc_symbol *derived;
5810
5811 /* Use the derived type of the base_object. */
5812 derived = e->value.compcall.base_object->ts.u.derived;
5813 st = NULL;
5814
5815 /* If necessary, go through the inheritance chain. */
5816 while (!st && derived)
5817 {
5818 /* Look for the typebound procedure 'name'. */
5819 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5820 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5821 e->value.compcall.name);
5822 if (!st)
5823 derived = gfc_get_derived_super_type (derived);
5824 }
5825
5826 /* Now find the specific name in the derived type namespace. */
5827 if (st && st->n.tb && st->n.tb->u.specific)
5828 gfc_find_sym_tree (st->n.tb->u.specific->name,
5829 derived->ns, 1, &st);
5830 if (st)
5831 *target = st;
5832 }
5833 return true;
5834 }
5835
5836
5837 /* Get the ultimate declared type from an expression. In addition,
5838 return the last class/derived type reference and the copy of the
5839 reference list. If check_types is set true, derived types are
5840 identified as well as class references. */
5841 static gfc_symbol*
5842 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5843 gfc_expr *e, bool check_types)
5844 {
5845 gfc_symbol *declared;
5846 gfc_ref *ref;
5847
5848 declared = NULL;
5849 if (class_ref)
5850 *class_ref = NULL;
5851 if (new_ref)
5852 *new_ref = gfc_copy_ref (e->ref);
5853
5854 for (ref = e->ref; ref; ref = ref->next)
5855 {
5856 if (ref->type != REF_COMPONENT)
5857 continue;
5858
5859 if ((ref->u.c.component->ts.type == BT_CLASS
5860 || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
5861 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
5862 {
5863 declared = ref->u.c.component->ts.u.derived;
5864 if (class_ref)
5865 *class_ref = ref;
5866 }
5867 }
5868
5869 if (declared == NULL)
5870 declared = e->symtree->n.sym->ts.u.derived;
5871
5872 return declared;
5873 }
5874
5875
5876 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5877 which of the specific bindings (if any) matches the arglist and transform
5878 the expression into a call of that binding. */
5879
5880 static bool
5881 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5882 {
5883 gfc_typebound_proc* genproc;
5884 const char* genname;
5885 gfc_symtree *st;
5886 gfc_symbol *derived;
5887
5888 gcc_assert (e->expr_type == EXPR_COMPCALL);
5889 genname = e->value.compcall.name;
5890 genproc = e->value.compcall.tbp;
5891
5892 if (!genproc->is_generic)
5893 return true;
5894
5895 /* Try the bindings on this type and in the inheritance hierarchy. */
5896 for (; genproc; genproc = genproc->overridden)
5897 {
5898 gfc_tbp_generic* g;
5899
5900 gcc_assert (genproc->is_generic);
5901 for (g = genproc->u.generic; g; g = g->next)
5902 {
5903 gfc_symbol* target;
5904 gfc_actual_arglist* args;
5905 bool matches;
5906
5907 gcc_assert (g->specific);
5908
5909 if (g->specific->error)
5910 continue;
5911
5912 target = g->specific->u.specific->n.sym;
5913
5914 /* Get the right arglist by handling PASS/NOPASS. */
5915 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5916 if (!g->specific->nopass)
5917 {
5918 gfc_expr* po;
5919 po = extract_compcall_passed_object (e);
5920 if (!po)
5921 {
5922 gfc_free_actual_arglist (args);
5923 return false;
5924 }
5925
5926 gcc_assert (g->specific->pass_arg_num > 0);
5927 gcc_assert (!g->specific->error);
5928 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5929 g->specific->pass_arg);
5930 }
5931 resolve_actual_arglist (args, target->attr.proc,
5932 is_external_proc (target)
5933 && gfc_sym_get_dummy_args (target) == NULL);
5934
5935 /* Check if this arglist matches the formal. */
5936 matches = gfc_arglist_matches_symbol (&args, target);
5937
5938 /* Clean up and break out of the loop if we've found it. */
5939 gfc_free_actual_arglist (args);
5940 if (matches)
5941 {
5942 e->value.compcall.tbp = g->specific;
5943 genname = g->specific_st->name;
5944 /* Pass along the name for CLASS methods, where the vtab
5945 procedure pointer component has to be referenced. */
5946 if (name)
5947 *name = genname;
5948 goto success;
5949 }
5950 }
5951 }
5952
5953 /* Nothing matching found! */
5954 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5955 " %qs at %L", genname, &e->where);
5956 return false;
5957
5958 success:
5959 /* Make sure that we have the right specific instance for the name. */
5960 derived = get_declared_from_expr (NULL, NULL, e, true);
5961
5962 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
5963 if (st)
5964 e->value.compcall.tbp = st->n.tb;
5965
5966 return true;
5967 }
5968
5969
5970 /* Resolve a call to a type-bound subroutine. */
5971
5972 static bool
5973 resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
5974 {
5975 gfc_actual_arglist* newactual;
5976 gfc_symtree* target;
5977
5978 /* Check that's really a SUBROUTINE. */
5979 if (!c->expr1->value.compcall.tbp->subroutine)
5980 {
5981 gfc_error ("%qs at %L should be a SUBROUTINE",
5982 c->expr1->value.compcall.name, &c->loc);
5983 return false;
5984 }
5985
5986 if (!check_typebound_baseobject (c->expr1))
5987 return false;
5988
5989 /* Pass along the name for CLASS methods, where the vtab
5990 procedure pointer component has to be referenced. */
5991 if (name)
5992 *name = c->expr1->value.compcall.name;
5993
5994 if (!resolve_typebound_generic_call (c->expr1, name))
5995 return false;
5996
5997 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
5998 if (overridable)
5999 *overridable = !c->expr1->value.compcall.tbp->non_overridable;
6000
6001 /* Transform into an ordinary EXEC_CALL for now. */
6002
6003 if (!resolve_typebound_static (c->expr1, &target, &newactual))
6004 return false;
6005
6006 c->ext.actual = newactual;
6007 c->symtree = target;
6008 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
6009
6010 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
6011
6012 gfc_free_expr (c->expr1);
6013 c->expr1 = gfc_get_expr ();
6014 c->expr1->expr_type = EXPR_FUNCTION;
6015 c->expr1->symtree = target;
6016 c->expr1->where = c->loc;
6017
6018 return resolve_call (c);
6019 }
6020
6021
6022 /* Resolve a component-call expression. */
6023 static bool
6024 resolve_compcall (gfc_expr* e, const char **name)
6025 {
6026 gfc_actual_arglist* newactual;
6027 gfc_symtree* target;
6028
6029 /* Check that's really a FUNCTION. */
6030 if (!e->value.compcall.tbp->function)
6031 {
6032 gfc_error ("%qs at %L should be a FUNCTION",
6033 e->value.compcall.name, &e->where);
6034 return false;
6035 }
6036
6037 /* These must not be assign-calls! */
6038 gcc_assert (!e->value.compcall.assign);
6039
6040 if (!check_typebound_baseobject (e))
6041 return false;
6042
6043 /* Pass along the name for CLASS methods, where the vtab
6044 procedure pointer component has to be referenced. */
6045 if (name)
6046 *name = e->value.compcall.name;
6047
6048 if (!resolve_typebound_generic_call (e, name))
6049 return false;
6050 gcc_assert (!e->value.compcall.tbp->is_generic);
6051
6052 /* Take the rank from the function's symbol. */
6053 if (e->value.compcall.tbp->u.specific->n.sym->as)
6054 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
6055
6056 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
6057 arglist to the TBP's binding target. */
6058
6059 if (!resolve_typebound_static (e, &target, &newactual))
6060 return false;
6061
6062 e->value.function.actual = newactual;
6063 e->value.function.name = NULL;
6064 e->value.function.esym = target->n.sym;
6065 e->value.function.isym = NULL;
6066 e->symtree = target;
6067 e->ts = target->n.sym->ts;
6068 e->expr_type = EXPR_FUNCTION;
6069
6070 /* Resolution is not necessary if this is a class subroutine; this
6071 function only has to identify the specific proc. Resolution of
6072 the call will be done next in resolve_typebound_call. */
6073 return gfc_resolve_expr (e);
6074 }
6075
6076
6077 static bool resolve_fl_derived (gfc_symbol *sym);
6078
6079
6080 /* Resolve a typebound function, or 'method'. First separate all
6081 the non-CLASS references by calling resolve_compcall directly. */
6082
6083 static bool
6084 resolve_typebound_function (gfc_expr* e)
6085 {
6086 gfc_symbol *declared;
6087 gfc_component *c;
6088 gfc_ref *new_ref;
6089 gfc_ref *class_ref;
6090 gfc_symtree *st;
6091 const char *name;
6092 gfc_typespec ts;
6093 gfc_expr *expr;
6094 bool overridable;
6095
6096 st = e->symtree;
6097
6098 /* Deal with typebound operators for CLASS objects. */
6099 expr = e->value.compcall.base_object;
6100 overridable = !e->value.compcall.tbp->non_overridable;
6101 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
6102 {
6103 /* If the base_object is not a variable, the corresponding actual
6104 argument expression must be stored in e->base_expression so
6105 that the corresponding tree temporary can be used as the base
6106 object in gfc_conv_procedure_call. */
6107 if (expr->expr_type != EXPR_VARIABLE)
6108 {
6109 gfc_actual_arglist *args;
6110
6111 for (args= e->value.function.actual; args; args = args->next)
6112 {
6113 if (expr == args->expr)
6114 expr = args->expr;
6115 }
6116 }
6117
6118 /* Since the typebound operators are generic, we have to ensure
6119 that any delays in resolution are corrected and that the vtab
6120 is present. */
6121 ts = expr->ts;
6122 declared = ts.u.derived;
6123 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6124 if (c->ts.u.derived == NULL)
6125 c->ts.u.derived = gfc_find_derived_vtab (declared);
6126
6127 if (!resolve_compcall (e, &name))
6128 return false;
6129
6130 /* Use the generic name if it is there. */
6131 name = name ? name : e->value.function.esym->name;
6132 e->symtree = expr->symtree;
6133 e->ref = gfc_copy_ref (expr->ref);
6134 get_declared_from_expr (&class_ref, NULL, e, false);
6135
6136 /* Trim away the extraneous references that emerge from nested
6137 use of interface.c (extend_expr). */
6138 if (class_ref && class_ref->next)
6139 {
6140 gfc_free_ref_list (class_ref->next);
6141 class_ref->next = NULL;
6142 }
6143 else if (e->ref && !class_ref)
6144 {
6145 gfc_free_ref_list (e->ref);
6146 e->ref = NULL;
6147 }
6148
6149 gfc_add_vptr_component (e);
6150 gfc_add_component_ref (e, name);
6151 e->value.function.esym = NULL;
6152 if (expr->expr_type != EXPR_VARIABLE)
6153 e->base_expr = expr;
6154 return true;
6155 }
6156
6157 if (st == NULL)
6158 return resolve_compcall (e, NULL);
6159
6160 if (!resolve_ref (e))
6161 return false;
6162
6163 /* Get the CLASS declared type. */
6164 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
6165
6166 if (!resolve_fl_derived (declared))
6167 return false;
6168
6169 /* Weed out cases of the ultimate component being a derived type. */
6170 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6171 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6172 {
6173 gfc_free_ref_list (new_ref);
6174 return resolve_compcall (e, NULL);
6175 }
6176
6177 c = gfc_find_component (declared, "_data", true, true, NULL);
6178 declared = c->ts.u.derived;
6179
6180 /* Treat the call as if it is a typebound procedure, in order to roll
6181 out the correct name for the specific function. */
6182 if (!resolve_compcall (e, &name))
6183 {
6184 gfc_free_ref_list (new_ref);
6185 return false;
6186 }
6187 ts = e->ts;
6188
6189 if (overridable)
6190 {
6191 /* Convert the expression to a procedure pointer component call. */
6192 e->value.function.esym = NULL;
6193 e->symtree = st;
6194
6195 if (new_ref)
6196 e->ref = new_ref;
6197
6198 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6199 gfc_add_vptr_component (e);
6200 gfc_add_component_ref (e, name);
6201
6202 /* Recover the typespec for the expression. This is really only
6203 necessary for generic procedures, where the additional call
6204 to gfc_add_component_ref seems to throw the collection of the
6205 correct typespec. */
6206 e->ts = ts;
6207 }
6208 else if (new_ref)
6209 gfc_free_ref_list (new_ref);
6210
6211 return true;
6212 }
6213
6214 /* Resolve a typebound subroutine, or 'method'. First separate all
6215 the non-CLASS references by calling resolve_typebound_call
6216 directly. */
6217
6218 static bool
6219 resolve_typebound_subroutine (gfc_code *code)
6220 {
6221 gfc_symbol *declared;
6222 gfc_component *c;
6223 gfc_ref *new_ref;
6224 gfc_ref *class_ref;
6225 gfc_symtree *st;
6226 const char *name;
6227 gfc_typespec ts;
6228 gfc_expr *expr;
6229 bool overridable;
6230
6231 st = code->expr1->symtree;
6232
6233 /* Deal with typebound operators for CLASS objects. */
6234 expr = code->expr1->value.compcall.base_object;
6235 overridable = !code->expr1->value.compcall.tbp->non_overridable;
6236 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
6237 {
6238 /* If the base_object is not a variable, the corresponding actual
6239 argument expression must be stored in e->base_expression so
6240 that the corresponding tree temporary can be used as the base
6241 object in gfc_conv_procedure_call. */
6242 if (expr->expr_type != EXPR_VARIABLE)
6243 {
6244 gfc_actual_arglist *args;
6245
6246 args= code->expr1->value.function.actual;
6247 for (; args; args = args->next)
6248 if (expr == args->expr)
6249 expr = args->expr;
6250 }
6251
6252 /* Since the typebound operators are generic, we have to ensure
6253 that any delays in resolution are corrected and that the vtab
6254 is present. */
6255 declared = expr->ts.u.derived;
6256 c = gfc_find_component (declared, "_vptr", true, true, NULL);
6257 if (c->ts.u.derived == NULL)
6258 c->ts.u.derived = gfc_find_derived_vtab (declared);
6259
6260 if (!resolve_typebound_call (code, &name, NULL))
6261 return false;
6262
6263 /* Use the generic name if it is there. */
6264 name = name ? name : code->expr1->value.function.esym->name;
6265 code->expr1->symtree = expr->symtree;
6266 code->expr1->ref = gfc_copy_ref (expr->ref);
6267
6268 /* Trim away the extraneous references that emerge from nested
6269 use of interface.c (extend_expr). */
6270 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
6271 if (class_ref && class_ref->next)
6272 {
6273 gfc_free_ref_list (class_ref->next);
6274 class_ref->next = NULL;
6275 }
6276 else if (code->expr1->ref && !class_ref)
6277 {
6278 gfc_free_ref_list (code->expr1->ref);
6279 code->expr1->ref = NULL;
6280 }
6281
6282 /* Now use the procedure in the vtable. */
6283 gfc_add_vptr_component (code->expr1);
6284 gfc_add_component_ref (code->expr1, name);
6285 code->expr1->value.function.esym = NULL;
6286 if (expr->expr_type != EXPR_VARIABLE)
6287 code->expr1->base_expr = expr;
6288 return true;
6289 }
6290
6291 if (st == NULL)
6292 return resolve_typebound_call (code, NULL, NULL);
6293
6294 if (!resolve_ref (code->expr1))
6295 return false;
6296
6297 /* Get the CLASS declared type. */
6298 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
6299
6300 /* Weed out cases of the ultimate component being a derived type. */
6301 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
6302 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
6303 {
6304 gfc_free_ref_list (new_ref);
6305 return resolve_typebound_call (code, NULL, NULL);
6306 }
6307
6308 if (!resolve_typebound_call (code, &name, &overridable))
6309 {
6310 gfc_free_ref_list (new_ref);
6311 return false;
6312 }
6313 ts = code->expr1->ts;
6314
6315 if (overridable)
6316 {
6317 /* Convert the expression to a procedure pointer component call. */
6318 code->expr1->value.function.esym = NULL;
6319 code->expr1->symtree = st;
6320
6321 if (new_ref)
6322 code->expr1->ref = new_ref;
6323
6324 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6325 gfc_add_vptr_component (code->expr1);
6326 gfc_add_component_ref (code->expr1, name);
6327
6328 /* Recover the typespec for the expression. This is really only
6329 necessary for generic procedures, where the additional call
6330 to gfc_add_component_ref seems to throw the collection of the
6331 correct typespec. */
6332 code->expr1->ts = ts;
6333 }
6334 else if (new_ref)
6335 gfc_free_ref_list (new_ref);
6336
6337 return true;
6338 }
6339
6340
6341 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6342
6343 static bool
6344 resolve_ppc_call (gfc_code* c)
6345 {
6346 gfc_component *comp;
6347
6348 comp = gfc_get_proc_ptr_comp (c->expr1);
6349 gcc_assert (comp != NULL);
6350
6351 c->resolved_sym = c->expr1->symtree->n.sym;
6352 c->expr1->expr_type = EXPR_VARIABLE;
6353
6354 if (!comp->attr.subroutine)
6355 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
6356
6357 if (!resolve_ref (c->expr1))
6358 return false;
6359
6360 if (!update_ppc_arglist (c->expr1))
6361 return false;
6362
6363 c->ext.actual = c->expr1->value.compcall.actual;
6364
6365 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6366 !(comp->ts.interface
6367 && comp->ts.interface->formal)))
6368 return false;
6369
6370 if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
6371 return false;
6372
6373 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
6374
6375 return true;
6376 }
6377
6378
6379 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
6380
6381 static bool
6382 resolve_expr_ppc (gfc_expr* e)
6383 {
6384 gfc_component *comp;
6385
6386 comp = gfc_get_proc_ptr_comp (e);
6387 gcc_assert (comp != NULL);
6388
6389 /* Convert to EXPR_FUNCTION. */
6390 e->expr_type = EXPR_FUNCTION;
6391 e->value.function.isym = NULL;
6392 e->value.function.actual = e->value.compcall.actual;
6393 e->ts = comp->ts;
6394 if (comp->as != NULL)
6395 e->rank = comp->as->rank;
6396
6397 if (!comp->attr.function)
6398 gfc_add_function (&comp->attr, comp->name, &e->where);
6399
6400 if (!resolve_ref (e))
6401 return false;
6402
6403 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6404 !(comp->ts.interface
6405 && comp->ts.interface->formal)))
6406 return false;
6407
6408 if (!update_ppc_arglist (e))
6409 return false;
6410
6411 if (!check_pure_function(e))
6412 return false;
6413
6414 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6415
6416 return true;
6417 }
6418
6419
6420 static bool
6421 gfc_is_expandable_expr (gfc_expr *e)
6422 {
6423 gfc_constructor *con;
6424
6425 if (e->expr_type == EXPR_ARRAY)
6426 {
6427 /* Traverse the constructor looking for variables that are flavor
6428 parameter. Parameters must be expanded since they are fully used at
6429 compile time. */
6430 con = gfc_constructor_first (e->value.constructor);
6431 for (; con; con = gfc_constructor_next (con))
6432 {
6433 if (con->expr->expr_type == EXPR_VARIABLE
6434 && con->expr->symtree
6435 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6436 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6437 return true;
6438 if (con->expr->expr_type == EXPR_ARRAY
6439 && gfc_is_expandable_expr (con->expr))
6440 return true;
6441 }
6442 }
6443
6444 return false;
6445 }
6446
6447 /* Resolve an expression. That is, make sure that types of operands agree
6448 with their operators, intrinsic operators are converted to function calls
6449 for overloaded types and unresolved function references are resolved. */
6450
6451 bool
6452 gfc_resolve_expr (gfc_expr *e)
6453 {
6454 bool t;
6455 bool inquiry_save, actual_arg_save, first_actual_arg_save;
6456
6457 if (e == NULL)
6458 return true;
6459
6460 /* inquiry_argument only applies to variables. */
6461 inquiry_save = inquiry_argument;
6462 actual_arg_save = actual_arg;
6463 first_actual_arg_save = first_actual_arg;
6464
6465 if (e->expr_type != EXPR_VARIABLE)
6466 {
6467 inquiry_argument = false;
6468 actual_arg = false;
6469 first_actual_arg = false;
6470 }
6471
6472 switch (e->expr_type)
6473 {
6474 case EXPR_OP:
6475 t = resolve_operator (e);
6476 break;
6477
6478 case EXPR_FUNCTION:
6479 case EXPR_VARIABLE:
6480
6481 if (check_host_association (e))
6482 t = resolve_function (e);
6483 else
6484 t = resolve_variable (e);
6485
6486 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6487 && e->ref->type != REF_SUBSTRING)
6488 gfc_resolve_substring_charlen (e);
6489
6490 break;
6491
6492 case EXPR_COMPCALL:
6493 t = resolve_typebound_function (e);
6494 break;
6495
6496 case EXPR_SUBSTRING:
6497 t = resolve_ref (e);
6498 break;
6499
6500 case EXPR_CONSTANT:
6501 case EXPR_NULL:
6502 t = true;
6503 break;
6504
6505 case EXPR_PPC:
6506 t = resolve_expr_ppc (e);
6507 break;
6508
6509 case EXPR_ARRAY:
6510 t = false;
6511 if (!resolve_ref (e))
6512 break;
6513
6514 t = gfc_resolve_array_constructor (e);
6515 /* Also try to expand a constructor. */
6516 if (t)
6517 {
6518 expression_rank (e);
6519 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6520 gfc_expand_constructor (e, false);
6521 }
6522
6523 /* This provides the opportunity for the length of constructors with
6524 character valued function elements to propagate the string length
6525 to the expression. */
6526 if (t && e->ts.type == BT_CHARACTER)
6527 {
6528 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6529 here rather then add a duplicate test for it above. */
6530 gfc_expand_constructor (e, false);
6531 t = gfc_resolve_character_array_constructor (e);
6532 }
6533
6534 break;
6535
6536 case EXPR_STRUCTURE:
6537 t = resolve_ref (e);
6538 if (!t)
6539 break;
6540
6541 t = resolve_structure_cons (e, 0);
6542 if (!t)
6543 break;
6544
6545 t = gfc_simplify_expr (e, 0);
6546 break;
6547
6548 default:
6549 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6550 }
6551
6552 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
6553 fixup_charlen (e);
6554
6555 inquiry_argument = inquiry_save;
6556 actual_arg = actual_arg_save;
6557 first_actual_arg = first_actual_arg_save;
6558
6559 return t;
6560 }
6561
6562
6563 /* Resolve an expression from an iterator. They must be scalar and have
6564 INTEGER or (optionally) REAL type. */
6565
6566 static bool
6567 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6568 const char *name_msgid)
6569 {
6570 if (!gfc_resolve_expr (expr))
6571 return false;
6572
6573 if (expr->rank != 0)
6574 {
6575 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6576 return false;
6577 }
6578
6579 if (expr->ts.type != BT_INTEGER)
6580 {
6581 if (expr->ts.type == BT_REAL)
6582 {
6583 if (real_ok)
6584 return gfc_notify_std (GFC_STD_F95_DEL,
6585 "%s at %L must be integer",
6586 _(name_msgid), &expr->where);
6587 else
6588 {
6589 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6590 &expr->where);
6591 return false;
6592 }
6593 }
6594 else
6595 {
6596 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6597 return false;
6598 }
6599 }
6600 return true;
6601 }
6602
6603
6604 /* Resolve the expressions in an iterator structure. If REAL_OK is
6605 false allow only INTEGER type iterators, otherwise allow REAL types.
6606 Set own_scope to true for ac-implied-do and data-implied-do as those
6607 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
6608
6609 bool
6610 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
6611 {
6612 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
6613 return false;
6614
6615 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
6616 _("iterator variable")))
6617 return false;
6618
6619 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
6620 "Start expression in DO loop"))
6621 return false;
6622
6623 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
6624 "End expression in DO loop"))
6625 return false;
6626
6627 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
6628 "Step expression in DO loop"))
6629 return false;
6630
6631 if (iter->step->expr_type == EXPR_CONSTANT)
6632 {
6633 if ((iter->step->ts.type == BT_INTEGER
6634 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6635 || (iter->step->ts.type == BT_REAL
6636 && mpfr_sgn (iter->step->value.real) == 0))
6637 {
6638 gfc_error ("Step expression in DO loop at %L cannot be zero",
6639 &iter->step->where);
6640 return false;
6641 }
6642 }
6643
6644 /* Convert start, end, and step to the same type as var. */
6645 if (iter->start->ts.kind != iter->var->ts.kind
6646 || iter->start->ts.type != iter->var->ts.type)
6647 gfc_convert_type (iter->start, &iter->var->ts, 1);
6648
6649 if (iter->end->ts.kind != iter->var->ts.kind
6650 || iter->end->ts.type != iter->var->ts.type)
6651 gfc_convert_type (iter->end, &iter->var->ts, 1);
6652
6653 if (iter->step->ts.kind != iter->var->ts.kind
6654 || iter->step->ts.type != iter->var->ts.type)
6655 gfc_convert_type (iter->step, &iter->var->ts, 1);
6656
6657 if (iter->start->expr_type == EXPR_CONSTANT
6658 && iter->end->expr_type == EXPR_CONSTANT
6659 && iter->step->expr_type == EXPR_CONSTANT)
6660 {
6661 int sgn, cmp;
6662 if (iter->start->ts.type == BT_INTEGER)
6663 {
6664 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6665 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6666 }
6667 else
6668 {
6669 sgn = mpfr_sgn (iter->step->value.real);
6670 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6671 }
6672 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
6673 gfc_warning (OPT_Wzerotrip,
6674 "DO loop at %L will be executed zero times",
6675 &iter->step->where);
6676 }
6677
6678 if (iter->end->expr_type == EXPR_CONSTANT
6679 && iter->end->ts.type == BT_INTEGER
6680 && iter->step->expr_type == EXPR_CONSTANT
6681 && iter->step->ts.type == BT_INTEGER
6682 && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
6683 || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
6684 {
6685 bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
6686 int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
6687
6688 if (is_step_positive
6689 && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
6690 gfc_warning (OPT_Wundefined_do_loop,
6691 "DO loop at %L is undefined as it overflows",
6692 &iter->step->where);
6693 else if (!is_step_positive
6694 && mpz_cmp (iter->end->value.integer,
6695 gfc_integer_kinds[k].min_int) == 0)
6696 gfc_warning (OPT_Wundefined_do_loop,
6697 "DO loop at %L is undefined as it underflows",
6698 &iter->step->where);
6699 }
6700
6701 return true;
6702 }
6703
6704
6705 /* Traversal function for find_forall_index. f == 2 signals that
6706 that variable itself is not to be checked - only the references. */
6707
6708 static bool
6709 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6710 {
6711 if (expr->expr_type != EXPR_VARIABLE)
6712 return false;
6713
6714 /* A scalar assignment */
6715 if (!expr->ref || *f == 1)
6716 {
6717 if (expr->symtree->n.sym == sym)
6718 return true;
6719 else
6720 return false;
6721 }
6722
6723 if (*f == 2)
6724 *f = 1;
6725 return false;
6726 }
6727
6728
6729 /* Check whether the FORALL index appears in the expression or not.
6730 Returns true if SYM is found in EXPR. */
6731
6732 bool
6733 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6734 {
6735 if (gfc_traverse_expr (expr, sym, forall_index, f))
6736 return true;
6737 else
6738 return false;
6739 }
6740
6741
6742 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6743 to be a scalar INTEGER variable. The subscripts and stride are scalar
6744 INTEGERs, and if stride is a constant it must be nonzero.
6745 Furthermore "A subscript or stride in a forall-triplet-spec shall
6746 not contain a reference to any index-name in the
6747 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6748
6749 static void
6750 resolve_forall_iterators (gfc_forall_iterator *it)
6751 {
6752 gfc_forall_iterator *iter, *iter2;
6753
6754 for (iter = it; iter; iter = iter->next)
6755 {
6756 if (gfc_resolve_expr (iter->var)
6757 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6758 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6759 &iter->var->where);
6760
6761 if (gfc_resolve_expr (iter->start)
6762 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6763 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6764 &iter->start->where);
6765 if (iter->var->ts.kind != iter->start->ts.kind)
6766 gfc_convert_type (iter->start, &iter->var->ts, 1);
6767
6768 if (gfc_resolve_expr (iter->end)
6769 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6770 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6771 &iter->end->where);
6772 if (iter->var->ts.kind != iter->end->ts.kind)
6773 gfc_convert_type (iter->end, &iter->var->ts, 1);
6774
6775 if (gfc_resolve_expr (iter->stride))
6776 {
6777 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6778 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6779 &iter->stride->where, "INTEGER");
6780
6781 if (iter->stride->expr_type == EXPR_CONSTANT
6782 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
6783 gfc_error ("FORALL stride expression at %L cannot be zero",
6784 &iter->stride->where);
6785 }
6786 if (iter->var->ts.kind != iter->stride->ts.kind)
6787 gfc_convert_type (iter->stride, &iter->var->ts, 1);
6788 }
6789
6790 for (iter = it; iter; iter = iter->next)
6791 for (iter2 = iter; iter2; iter2 = iter2->next)
6792 {
6793 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
6794 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
6795 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
6796 gfc_error ("FORALL index %qs may not appear in triplet "
6797 "specification at %L", iter->var->symtree->name,
6798 &iter2->start->where);
6799 }
6800 }
6801
6802
6803 /* Given a pointer to a symbol that is a derived type, see if it's
6804 inaccessible, i.e. if it's defined in another module and the components are
6805 PRIVATE. The search is recursive if necessary. Returns zero if no
6806 inaccessible components are found, nonzero otherwise. */
6807
6808 static int
6809 derived_inaccessible (gfc_symbol *sym)
6810 {
6811 gfc_component *c;
6812
6813 if (sym->attr.use_assoc && sym->attr.private_comp)
6814 return 1;
6815
6816 for (c = sym->components; c; c = c->next)
6817 {
6818 /* Prevent an infinite loop through this function. */
6819 if (c->ts.type == BT_DERIVED && c->attr.pointer
6820 && sym == c->ts.u.derived)
6821 continue;
6822
6823 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6824 return 1;
6825 }
6826
6827 return 0;
6828 }
6829
6830
6831 /* Resolve the argument of a deallocate expression. The expression must be
6832 a pointer or a full array. */
6833
6834 static bool
6835 resolve_deallocate_expr (gfc_expr *e)
6836 {
6837 symbol_attribute attr;
6838 int allocatable, pointer;
6839 gfc_ref *ref;
6840 gfc_symbol *sym;
6841 gfc_component *c;
6842 bool unlimited;
6843
6844 if (!gfc_resolve_expr (e))
6845 return false;
6846
6847 if (e->expr_type != EXPR_VARIABLE)
6848 goto bad;
6849
6850 sym = e->symtree->n.sym;
6851 unlimited = UNLIMITED_POLY(sym);
6852
6853 if (sym->ts.type == BT_CLASS)
6854 {
6855 allocatable = CLASS_DATA (sym)->attr.allocatable;
6856 pointer = CLASS_DATA (sym)->attr.class_pointer;
6857 }
6858 else
6859 {
6860 allocatable = sym->attr.allocatable;
6861 pointer = sym->attr.pointer;
6862 }
6863 for (ref = e->ref; ref; ref = ref->next)
6864 {
6865 switch (ref->type)
6866 {
6867 case REF_ARRAY:
6868 if (ref->u.ar.type != AR_FULL
6869 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6870 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
6871 allocatable = 0;
6872 break;
6873
6874 case REF_COMPONENT:
6875 c = ref->u.c.component;
6876 if (c->ts.type == BT_CLASS)
6877 {
6878 allocatable = CLASS_DATA (c)->attr.allocatable;
6879 pointer = CLASS_DATA (c)->attr.class_pointer;
6880 }
6881 else
6882 {
6883 allocatable = c->attr.allocatable;
6884 pointer = c->attr.pointer;
6885 }
6886 break;
6887
6888 case REF_SUBSTRING:
6889 allocatable = 0;
6890 break;
6891 }
6892 }
6893
6894 attr = gfc_expr_attr (e);
6895
6896 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
6897 {
6898 bad:
6899 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6900 &e->where);
6901 return false;
6902 }
6903
6904 /* F2008, C644. */
6905 if (gfc_is_coindexed (e))
6906 {
6907 gfc_error ("Coindexed allocatable object at %L", &e->where);
6908 return false;
6909 }
6910
6911 if (pointer
6912 && !gfc_check_vardef_context (e, true, true, false,
6913 _("DEALLOCATE object")))
6914 return false;
6915 if (!gfc_check_vardef_context (e, false, true, false,
6916 _("DEALLOCATE object")))
6917 return false;
6918
6919 return true;
6920 }
6921
6922
6923 /* Returns true if the expression e contains a reference to the symbol sym. */
6924 static bool
6925 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6926 {
6927 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6928 return true;
6929
6930 return false;
6931 }
6932
6933 bool
6934 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6935 {
6936 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6937 }
6938
6939
6940 /* Given the expression node e for an allocatable/pointer of derived type to be
6941 allocated, get the expression node to be initialized afterwards (needed for
6942 derived types with default initializers, and derived types with allocatable
6943 components that need nullification.) */
6944
6945 gfc_expr *
6946 gfc_expr_to_initialize (gfc_expr *e)
6947 {
6948 gfc_expr *result;
6949 gfc_ref *ref;
6950 int i;
6951
6952 result = gfc_copy_expr (e);
6953
6954 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6955 for (ref = result->ref; ref; ref = ref->next)
6956 if (ref->type == REF_ARRAY && ref->next == NULL)
6957 {
6958 ref->u.ar.type = AR_FULL;
6959
6960 for (i = 0; i < ref->u.ar.dimen; i++)
6961 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6962
6963 break;
6964 }
6965
6966 gfc_free_shape (&result->shape, result->rank);
6967
6968 /* Recalculate rank, shape, etc. */
6969 gfc_resolve_expr (result);
6970 return result;
6971 }
6972
6973
6974 /* If the last ref of an expression is an array ref, return a copy of the
6975 expression with that one removed. Otherwise, a copy of the original
6976 expression. This is used for allocate-expressions and pointer assignment
6977 LHS, where there may be an array specification that needs to be stripped
6978 off when using gfc_check_vardef_context. */
6979
6980 static gfc_expr*
6981 remove_last_array_ref (gfc_expr* e)
6982 {
6983 gfc_expr* e2;
6984 gfc_ref** r;
6985
6986 e2 = gfc_copy_expr (e);
6987 for (r = &e2->ref; *r; r = &(*r)->next)
6988 if ((*r)->type == REF_ARRAY && !(*r)->next)
6989 {
6990 gfc_free_ref_list (*r);
6991 *r = NULL;
6992 break;
6993 }
6994
6995 return e2;
6996 }
6997
6998
6999 /* Used in resolve_allocate_expr to check that a allocation-object and
7000 a source-expr are conformable. This does not catch all possible
7001 cases; in particular a runtime checking is needed. */
7002
7003 static bool
7004 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
7005 {
7006 gfc_ref *tail;
7007 for (tail = e2->ref; tail && tail->next; tail = tail->next);
7008
7009 /* First compare rank. */
7010 if ((tail && e1->rank != tail->u.ar.as->rank)
7011 || (!tail && e1->rank != e2->rank))
7012 {
7013 gfc_error ("Source-expr at %L must be scalar or have the "
7014 "same rank as the allocate-object at %L",
7015 &e1->where, &e2->where);
7016 return false;
7017 }
7018
7019 if (e1->shape)
7020 {
7021 int i;
7022 mpz_t s;
7023
7024 mpz_init (s);
7025
7026 for (i = 0; i < e1->rank; i++)
7027 {
7028 if (tail->u.ar.start[i] == NULL)
7029 break;
7030
7031 if (tail->u.ar.end[i])
7032 {
7033 mpz_set (s, tail->u.ar.end[i]->value.integer);
7034 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
7035 mpz_add_ui (s, s, 1);
7036 }
7037 else
7038 {
7039 mpz_set (s, tail->u.ar.start[i]->value.integer);
7040 }
7041
7042 if (mpz_cmp (e1->shape[i], s) != 0)
7043 {
7044 gfc_error ("Source-expr at %L and allocate-object at %L must "
7045 "have the same shape", &e1->where, &e2->where);
7046 mpz_clear (s);
7047 return false;
7048 }
7049 }
7050
7051 mpz_clear (s);
7052 }
7053
7054 return true;
7055 }
7056
7057
7058 /* Resolve the expression in an ALLOCATE statement, doing the additional
7059 checks to see whether the expression is OK or not. The expression must
7060 have a trailing array reference that gives the size of the array. */
7061
7062 static bool
7063 resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
7064 {
7065 int i, pointer, allocatable, dimension, is_abstract;
7066 int codimension;
7067 bool coindexed;
7068 bool unlimited;
7069 symbol_attribute attr;
7070 gfc_ref *ref, *ref2;
7071 gfc_expr *e2;
7072 gfc_array_ref *ar;
7073 gfc_symbol *sym = NULL;
7074 gfc_alloc *a;
7075 gfc_component *c;
7076 bool t;
7077
7078 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
7079 checking of coarrays. */
7080 for (ref = e->ref; ref; ref = ref->next)
7081 if (ref->next == NULL)
7082 break;
7083
7084 if (ref && ref->type == REF_ARRAY)
7085 ref->u.ar.in_allocate = true;
7086
7087 if (!gfc_resolve_expr (e))
7088 goto failure;
7089
7090 /* Make sure the expression is allocatable or a pointer. If it is
7091 pointer, the next-to-last reference must be a pointer. */
7092
7093 ref2 = NULL;
7094 if (e->symtree)
7095 sym = e->symtree->n.sym;
7096
7097 /* Check whether ultimate component is abstract and CLASS. */
7098 is_abstract = 0;
7099
7100 /* Is the allocate-object unlimited polymorphic? */
7101 unlimited = UNLIMITED_POLY(e);
7102
7103 if (e->expr_type != EXPR_VARIABLE)
7104 {
7105 allocatable = 0;
7106 attr = gfc_expr_attr (e);
7107 pointer = attr.pointer;
7108 dimension = attr.dimension;
7109 codimension = attr.codimension;
7110 }
7111 else
7112 {
7113 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
7114 {
7115 allocatable = CLASS_DATA (sym)->attr.allocatable;
7116 pointer = CLASS_DATA (sym)->attr.class_pointer;
7117 dimension = CLASS_DATA (sym)->attr.dimension;
7118 codimension = CLASS_DATA (sym)->attr.codimension;
7119 is_abstract = CLASS_DATA (sym)->attr.abstract;
7120 }
7121 else
7122 {
7123 allocatable = sym->attr.allocatable;
7124 pointer = sym->attr.pointer;
7125 dimension = sym->attr.dimension;
7126 codimension = sym->attr.codimension;
7127 }
7128
7129 coindexed = false;
7130
7131 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
7132 {
7133 switch (ref->type)
7134 {
7135 case REF_ARRAY:
7136 if (ref->u.ar.codimen > 0)
7137 {
7138 int n;
7139 for (n = ref->u.ar.dimen;
7140 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
7141 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
7142 {
7143 coindexed = true;
7144 break;
7145 }
7146 }
7147
7148 if (ref->next != NULL)
7149 pointer = 0;
7150 break;
7151
7152 case REF_COMPONENT:
7153 /* F2008, C644. */
7154 if (coindexed)
7155 {
7156 gfc_error ("Coindexed allocatable object at %L",
7157 &e->where);
7158 goto failure;
7159 }
7160
7161 c = ref->u.c.component;
7162 if (c->ts.type == BT_CLASS)
7163 {
7164 allocatable = CLASS_DATA (c)->attr.allocatable;
7165 pointer = CLASS_DATA (c)->attr.class_pointer;
7166 dimension = CLASS_DATA (c)->attr.dimension;
7167 codimension = CLASS_DATA (c)->attr.codimension;
7168 is_abstract = CLASS_DATA (c)->attr.abstract;
7169 }
7170 else
7171 {
7172 allocatable = c->attr.allocatable;
7173 pointer = c->attr.pointer;
7174 dimension = c->attr.dimension;
7175 codimension = c->attr.codimension;
7176 is_abstract = c->attr.abstract;
7177 }
7178 break;
7179
7180 case REF_SUBSTRING:
7181 allocatable = 0;
7182 pointer = 0;
7183 break;
7184 }
7185 }
7186 }
7187
7188 /* Check for F08:C628. */
7189 if (allocatable == 0 && pointer == 0 && !unlimited)
7190 {
7191 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7192 &e->where);
7193 goto failure;
7194 }
7195
7196 /* Some checks for the SOURCE tag. */
7197 if (code->expr3)
7198 {
7199 /* Check F03:C631. */
7200 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
7201 {
7202 gfc_error ("Type of entity at %L is type incompatible with "
7203 "source-expr at %L", &e->where, &code->expr3->where);
7204 goto failure;
7205 }
7206
7207 /* Check F03:C632 and restriction following Note 6.18. */
7208 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
7209 goto failure;
7210
7211 /* Check F03:C633. */
7212 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
7213 {
7214 gfc_error ("The allocate-object at %L and the source-expr at %L "
7215 "shall have the same kind type parameter",
7216 &e->where, &code->expr3->where);
7217 goto failure;
7218 }
7219
7220 /* Check F2008, C642. */
7221 if (code->expr3->ts.type == BT_DERIVED
7222 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
7223 || (code->expr3->ts.u.derived->from_intmod
7224 == INTMOD_ISO_FORTRAN_ENV
7225 && code->expr3->ts.u.derived->intmod_sym_id
7226 == ISOFORTRAN_LOCK_TYPE)))
7227 {
7228 gfc_error ("The source-expr at %L shall neither be of type "
7229 "LOCK_TYPE nor have a LOCK_TYPE component if "
7230 "allocate-object at %L is a coarray",
7231 &code->expr3->where, &e->where);
7232 goto failure;
7233 }
7234
7235 /* Check TS18508, C702/C703. */
7236 if (code->expr3->ts.type == BT_DERIVED
7237 && ((codimension && gfc_expr_attr (code->expr3).event_comp)
7238 || (code->expr3->ts.u.derived->from_intmod
7239 == INTMOD_ISO_FORTRAN_ENV
7240 && code->expr3->ts.u.derived->intmod_sym_id
7241 == ISOFORTRAN_EVENT_TYPE)))
7242 {
7243 gfc_error ("The source-expr at %L shall neither be of type "
7244 "EVENT_TYPE nor have a EVENT_TYPE component if "
7245 "allocate-object at %L is a coarray",
7246 &code->expr3->where, &e->where);
7247 goto failure;
7248 }
7249 }
7250
7251 /* Check F08:C629. */
7252 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
7253 && !code->expr3)
7254 {
7255 gcc_assert (e->ts.type == BT_CLASS);
7256 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
7257 "type-spec or source-expr", sym->name, &e->where);
7258 goto failure;
7259 }
7260
7261 /* Check F08:C632. */
7262 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
7263 && !UNLIMITED_POLY (e))
7264 {
7265 int cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
7266 code->ext.alloc.ts.u.cl->length);
7267 if (cmp == 1 || cmp == -1 || cmp == -3)
7268 {
7269 gfc_error ("Allocating %s at %L with type-spec requires the same "
7270 "character-length parameter as in the declaration",
7271 sym->name, &e->where);
7272 goto failure;
7273 }
7274 }
7275
7276 /* In the variable definition context checks, gfc_expr_attr is used
7277 on the expression. This is fooled by the array specification
7278 present in e, thus we have to eliminate that one temporarily. */
7279 e2 = remove_last_array_ref (e);
7280 t = true;
7281 if (t && pointer)
7282 t = gfc_check_vardef_context (e2, true, true, false,
7283 _("ALLOCATE object"));
7284 if (t)
7285 t = gfc_check_vardef_context (e2, false, true, false,
7286 _("ALLOCATE object"));
7287 gfc_free_expr (e2);
7288 if (!t)
7289 goto failure;
7290
7291 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
7292 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
7293 {
7294 /* For class arrays, the initialization with SOURCE is done
7295 using _copy and trans_call. It is convenient to exploit that
7296 when the allocated type is different from the declared type but
7297 no SOURCE exists by setting expr3. */
7298 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
7299 }
7300 else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
7301 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
7302 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
7303 {
7304 /* We have to zero initialize the integer variable. */
7305 code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
7306 }
7307
7308 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
7309 {
7310 /* Make sure the vtab symbol is present when
7311 the module variables are generated. */
7312 gfc_typespec ts = e->ts;
7313 if (code->expr3)
7314 ts = code->expr3->ts;
7315 else if (code->ext.alloc.ts.type == BT_DERIVED)
7316 ts = code->ext.alloc.ts;
7317
7318 /* Finding the vtab also publishes the type's symbol. Therefore this
7319 statement is necessary. */
7320 gfc_find_derived_vtab (ts.u.derived);
7321 }
7322 else if (unlimited && !UNLIMITED_POLY (code->expr3))
7323 {
7324 /* Again, make sure the vtab symbol is present when
7325 the module variables are generated. */
7326 gfc_typespec *ts = NULL;
7327 if (code->expr3)
7328 ts = &code->expr3->ts;
7329 else
7330 ts = &code->ext.alloc.ts;
7331
7332 gcc_assert (ts);
7333
7334 /* Finding the vtab also publishes the type's symbol. Therefore this
7335 statement is necessary. */
7336 gfc_find_vtab (ts);
7337 }
7338
7339 if (dimension == 0 && codimension == 0)
7340 goto success;
7341
7342 /* Make sure the last reference node is an array specification. */
7343
7344 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
7345 || (dimension && ref2->u.ar.dimen == 0))
7346 {
7347 /* F08:C633. */
7348 if (code->expr3)
7349 {
7350 if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
7351 "in ALLOCATE statement at %L", &e->where))
7352 goto failure;
7353 if (code->expr3->rank != 0)
7354 *array_alloc_wo_spec = true;
7355 else
7356 {
7357 gfc_error ("Array specification or array-valued SOURCE= "
7358 "expression required in ALLOCATE statement at %L",
7359 &e->where);
7360 goto failure;
7361 }
7362 }
7363 else
7364 {
7365 gfc_error ("Array specification required in ALLOCATE statement "
7366 "at %L", &e->where);
7367 goto failure;
7368 }
7369 }
7370
7371 /* Make sure that the array section reference makes sense in the
7372 context of an ALLOCATE specification. */
7373
7374 ar = &ref2->u.ar;
7375
7376 if (codimension)
7377 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
7378 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
7379 {
7380 gfc_error ("Coarray specification required in ALLOCATE statement "
7381 "at %L", &e->where);
7382 goto failure;
7383 }
7384
7385 for (i = 0; i < ar->dimen; i++)
7386 {
7387 if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
7388 goto check_symbols;
7389
7390 switch (ar->dimen_type[i])
7391 {
7392 case DIMEN_ELEMENT:
7393 break;
7394
7395 case DIMEN_RANGE:
7396 if (ar->start[i] != NULL
7397 && ar->end[i] != NULL
7398 && ar->stride[i] == NULL)
7399 break;
7400
7401 /* Fall through. */
7402
7403 case DIMEN_UNKNOWN:
7404 case DIMEN_VECTOR:
7405 case DIMEN_STAR:
7406 case DIMEN_THIS_IMAGE:
7407 gfc_error ("Bad array specification in ALLOCATE statement at %L",
7408 &e->where);
7409 goto failure;
7410 }
7411
7412 check_symbols:
7413 for (a = code->ext.alloc.list; a; a = a->next)
7414 {
7415 sym = a->expr->symtree->n.sym;
7416
7417 /* TODO - check derived type components. */
7418 if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
7419 continue;
7420
7421 if ((ar->start[i] != NULL
7422 && gfc_find_sym_in_expr (sym, ar->start[i]))
7423 || (ar->end[i] != NULL
7424 && gfc_find_sym_in_expr (sym, ar->end[i])))
7425 {
7426 gfc_error ("%qs must not appear in the array specification at "
7427 "%L in the same ALLOCATE statement where it is "
7428 "itself allocated", sym->name, &ar->where);
7429 goto failure;
7430 }
7431 }
7432 }
7433
7434 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
7435 {
7436 if (ar->dimen_type[i] == DIMEN_ELEMENT
7437 || ar->dimen_type[i] == DIMEN_RANGE)
7438 {
7439 if (i == (ar->dimen + ar->codimen - 1))
7440 {
7441 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
7442 "statement at %L", &e->where);
7443 goto failure;
7444 }
7445 continue;
7446 }
7447
7448 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
7449 && ar->stride[i] == NULL)
7450 break;
7451
7452 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7453 &e->where);
7454 goto failure;
7455 }
7456
7457 success:
7458 return true;
7459
7460 failure:
7461 return false;
7462 }
7463
7464
7465 static void
7466 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
7467 {
7468 gfc_expr *stat, *errmsg, *pe, *qe;
7469 gfc_alloc *a, *p, *q;
7470
7471 stat = code->expr1;
7472 errmsg = code->expr2;
7473
7474 /* Check the stat variable. */
7475 if (stat)
7476 {
7477 gfc_check_vardef_context (stat, false, false, false,
7478 _("STAT variable"));
7479
7480 if ((stat->ts.type != BT_INTEGER
7481 && !(stat->ref && (stat->ref->type == REF_ARRAY
7482 || stat->ref->type == REF_COMPONENT)))
7483 || stat->rank > 0)
7484 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7485 "variable", &stat->where);
7486
7487 for (p = code->ext.alloc.list; p; p = p->next)
7488 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
7489 {
7490 gfc_ref *ref1, *ref2;
7491 bool found = true;
7492
7493 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7494 ref1 = ref1->next, ref2 = ref2->next)
7495 {
7496 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7497 continue;
7498 if (ref1->u.c.component->name != ref2->u.c.component->name)
7499 {
7500 found = false;
7501 break;
7502 }
7503 }
7504
7505 if (found)
7506 {
7507 gfc_error ("Stat-variable at %L shall not be %sd within "
7508 "the same %s statement", &stat->where, fcn, fcn);
7509 break;
7510 }
7511 }
7512 }
7513
7514 /* Check the errmsg variable. */
7515 if (errmsg)
7516 {
7517 if (!stat)
7518 gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
7519 &errmsg->where);
7520
7521 gfc_check_vardef_context (errmsg, false, false, false,
7522 _("ERRMSG variable"));
7523
7524 if ((errmsg->ts.type != BT_CHARACTER
7525 && !(errmsg->ref
7526 && (errmsg->ref->type == REF_ARRAY
7527 || errmsg->ref->type == REF_COMPONENT)))
7528 || errmsg->rank > 0 )
7529 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7530 "variable", &errmsg->where);
7531
7532 for (p = code->ext.alloc.list; p; p = p->next)
7533 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7534 {
7535 gfc_ref *ref1, *ref2;
7536 bool found = true;
7537
7538 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7539 ref1 = ref1->next, ref2 = ref2->next)
7540 {
7541 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7542 continue;
7543 if (ref1->u.c.component->name != ref2->u.c.component->name)
7544 {
7545 found = false;
7546 break;
7547 }
7548 }
7549
7550 if (found)
7551 {
7552 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7553 "the same %s statement", &errmsg->where, fcn, fcn);
7554 break;
7555 }
7556 }
7557 }
7558
7559 /* Check that an allocate-object appears only once in the statement. */
7560
7561 for (p = code->ext.alloc.list; p; p = p->next)
7562 {
7563 pe = p->expr;
7564 for (q = p->next; q; q = q->next)
7565 {
7566 qe = q->expr;
7567 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
7568 {
7569 /* This is a potential collision. */
7570 gfc_ref *pr = pe->ref;
7571 gfc_ref *qr = qe->ref;
7572
7573 /* Follow the references until
7574 a) They start to differ, in which case there is no error;
7575 you can deallocate a%b and a%c in a single statement
7576 b) Both of them stop, which is an error
7577 c) One of them stops, which is also an error. */
7578 while (1)
7579 {
7580 if (pr == NULL && qr == NULL)
7581 {
7582 gfc_error ("Allocate-object at %L also appears at %L",
7583 &pe->where, &qe->where);
7584 break;
7585 }
7586 else if (pr != NULL && qr == NULL)
7587 {
7588 gfc_error ("Allocate-object at %L is subobject of"
7589 " object at %L", &pe->where, &qe->where);
7590 break;
7591 }
7592 else if (pr == NULL && qr != NULL)
7593 {
7594 gfc_error ("Allocate-object at %L is subobject of"
7595 " object at %L", &qe->where, &pe->where);
7596 break;
7597 }
7598 /* Here, pr != NULL && qr != NULL */
7599 gcc_assert(pr->type == qr->type);
7600 if (pr->type == REF_ARRAY)
7601 {
7602 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7603 which are legal. */
7604 gcc_assert (qr->type == REF_ARRAY);
7605
7606 if (pr->next && qr->next)
7607 {
7608 int i;
7609 gfc_array_ref *par = &(pr->u.ar);
7610 gfc_array_ref *qar = &(qr->u.ar);
7611
7612 for (i=0; i<par->dimen; i++)
7613 {
7614 if ((par->start[i] != NULL
7615 || qar->start[i] != NULL)
7616 && gfc_dep_compare_expr (par->start[i],
7617 qar->start[i]) != 0)
7618 goto break_label;
7619 }
7620 }
7621 }
7622 else
7623 {
7624 if (pr->u.c.component->name != qr->u.c.component->name)
7625 break;
7626 }
7627
7628 pr = pr->next;
7629 qr = qr->next;
7630 }
7631 break_label:
7632 ;
7633 }
7634 }
7635 }
7636
7637 if (strcmp (fcn, "ALLOCATE") == 0)
7638 {
7639 bool arr_alloc_wo_spec = false;
7640
7641 /* Resolving the expr3 in the loop over all objects to allocate would
7642 execute loop invariant code for each loop item. Therefore do it just
7643 once here. */
7644 if (code->expr3 && code->expr3->mold
7645 && code->expr3->ts.type == BT_DERIVED)
7646 {
7647 /* Default initialization via MOLD (non-polymorphic). */
7648 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
7649 if (rhs != NULL)
7650 {
7651 gfc_resolve_expr (rhs);
7652 gfc_free_expr (code->expr3);
7653 code->expr3 = rhs;
7654 }
7655 }
7656 for (a = code->ext.alloc.list; a; a = a->next)
7657 resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
7658
7659 if (arr_alloc_wo_spec && code->expr3)
7660 {
7661 /* Mark the allocate to have to take the array specification
7662 from the expr3. */
7663 code->ext.alloc.arr_spec_from_expr3 = 1;
7664 }
7665 }
7666 else
7667 {
7668 for (a = code->ext.alloc.list; a; a = a->next)
7669 resolve_deallocate_expr (a->expr);
7670 }
7671 }
7672
7673
7674 /************ SELECT CASE resolution subroutines ************/
7675
7676 /* Callback function for our mergesort variant. Determines interval
7677 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7678 op1 > op2. Assumes we're not dealing with the default case.
7679 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7680 There are nine situations to check. */
7681
7682 static int
7683 compare_cases (const gfc_case *op1, const gfc_case *op2)
7684 {
7685 int retval;
7686
7687 if (op1->low == NULL) /* op1 = (:L) */
7688 {
7689 /* op2 = (:N), so overlap. */
7690 retval = 0;
7691 /* op2 = (M:) or (M:N), L < M */
7692 if (op2->low != NULL
7693 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7694 retval = -1;
7695 }
7696 else if (op1->high == NULL) /* op1 = (K:) */
7697 {
7698 /* op2 = (M:), so overlap. */
7699 retval = 0;
7700 /* op2 = (:N) or (M:N), K > N */
7701 if (op2->high != NULL
7702 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7703 retval = 1;
7704 }
7705 else /* op1 = (K:L) */
7706 {
7707 if (op2->low == NULL) /* op2 = (:N), K > N */
7708 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7709 ? 1 : 0;
7710 else if (op2->high == NULL) /* op2 = (M:), L < M */
7711 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7712 ? -1 : 0;
7713 else /* op2 = (M:N) */
7714 {
7715 retval = 0;
7716 /* L < M */
7717 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7718 retval = -1;
7719 /* K > N */
7720 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7721 retval = 1;
7722 }
7723 }
7724
7725 return retval;
7726 }
7727
7728
7729 /* Merge-sort a double linked case list, detecting overlap in the
7730 process. LIST is the head of the double linked case list before it
7731 is sorted. Returns the head of the sorted list if we don't see any
7732 overlap, or NULL otherwise. */
7733
7734 static gfc_case *
7735 check_case_overlap (gfc_case *list)
7736 {
7737 gfc_case *p, *q, *e, *tail;
7738 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7739
7740 /* If the passed list was empty, return immediately. */
7741 if (!list)
7742 return NULL;
7743
7744 overlap_seen = 0;
7745 insize = 1;
7746
7747 /* Loop unconditionally. The only exit from this loop is a return
7748 statement, when we've finished sorting the case list. */
7749 for (;;)
7750 {
7751 p = list;
7752 list = NULL;
7753 tail = NULL;
7754
7755 /* Count the number of merges we do in this pass. */
7756 nmerges = 0;
7757
7758 /* Loop while there exists a merge to be done. */
7759 while (p)
7760 {
7761 int i;
7762
7763 /* Count this merge. */
7764 nmerges++;
7765
7766 /* Cut the list in two pieces by stepping INSIZE places
7767 forward in the list, starting from P. */
7768 psize = 0;
7769 q = p;
7770 for (i = 0; i < insize; i++)
7771 {
7772 psize++;
7773 q = q->right;
7774 if (!q)
7775 break;
7776 }
7777 qsize = insize;
7778
7779 /* Now we have two lists. Merge them! */
7780 while (psize > 0 || (qsize > 0 && q != NULL))
7781 {
7782 /* See from which the next case to merge comes from. */
7783 if (psize == 0)
7784 {
7785 /* P is empty so the next case must come from Q. */
7786 e = q;
7787 q = q->right;
7788 qsize--;
7789 }
7790 else if (qsize == 0 || q == NULL)
7791 {
7792 /* Q is empty. */
7793 e = p;
7794 p = p->right;
7795 psize--;
7796 }
7797 else
7798 {
7799 cmp = compare_cases (p, q);
7800 if (cmp < 0)
7801 {
7802 /* The whole case range for P is less than the
7803 one for Q. */
7804 e = p;
7805 p = p->right;
7806 psize--;
7807 }
7808 else if (cmp > 0)
7809 {
7810 /* The whole case range for Q is greater than
7811 the case range for P. */
7812 e = q;
7813 q = q->right;
7814 qsize--;
7815 }
7816 else
7817 {
7818 /* The cases overlap, or they are the same
7819 element in the list. Either way, we must
7820 issue an error and get the next case from P. */
7821 /* FIXME: Sort P and Q by line number. */
7822 gfc_error ("CASE label at %L overlaps with CASE "
7823 "label at %L", &p->where, &q->where);
7824 overlap_seen = 1;
7825 e = p;
7826 p = p->right;
7827 psize--;
7828 }
7829 }
7830
7831 /* Add the next element to the merged list. */
7832 if (tail)
7833 tail->right = e;
7834 else
7835 list = e;
7836 e->left = tail;
7837 tail = e;
7838 }
7839
7840 /* P has now stepped INSIZE places along, and so has Q. So
7841 they're the same. */
7842 p = q;
7843 }
7844 tail->right = NULL;
7845
7846 /* If we have done only one merge or none at all, we've
7847 finished sorting the cases. */
7848 if (nmerges <= 1)
7849 {
7850 if (!overlap_seen)
7851 return list;
7852 else
7853 return NULL;
7854 }
7855
7856 /* Otherwise repeat, merging lists twice the size. */
7857 insize *= 2;
7858 }
7859 }
7860
7861
7862 /* Check to see if an expression is suitable for use in a CASE statement.
7863 Makes sure that all case expressions are scalar constants of the same
7864 type. Return false if anything is wrong. */
7865
7866 static bool
7867 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7868 {
7869 if (e == NULL) return true;
7870
7871 if (e->ts.type != case_expr->ts.type)
7872 {
7873 gfc_error ("Expression in CASE statement at %L must be of type %s",
7874 &e->where, gfc_basic_typename (case_expr->ts.type));
7875 return false;
7876 }
7877
7878 /* C805 (R808) For a given case-construct, each case-value shall be of
7879 the same type as case-expr. For character type, length differences
7880 are allowed, but the kind type parameters shall be the same. */
7881
7882 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7883 {
7884 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7885 &e->where, case_expr->ts.kind);
7886 return false;
7887 }
7888
7889 /* Convert the case value kind to that of case expression kind,
7890 if needed */
7891
7892 if (e->ts.kind != case_expr->ts.kind)
7893 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7894
7895 if (e->rank != 0)
7896 {
7897 gfc_error ("Expression in CASE statement at %L must be scalar",
7898 &e->where);
7899 return false;
7900 }
7901
7902 return true;
7903 }
7904
7905
7906 /* Given a completely parsed select statement, we:
7907
7908 - Validate all expressions and code within the SELECT.
7909 - Make sure that the selection expression is not of the wrong type.
7910 - Make sure that no case ranges overlap.
7911 - Eliminate unreachable cases and unreachable code resulting from
7912 removing case labels.
7913
7914 The standard does allow unreachable cases, e.g. CASE (5:3). But
7915 they are a hassle for code generation, and to prevent that, we just
7916 cut them out here. This is not necessary for overlapping cases
7917 because they are illegal and we never even try to generate code.
7918
7919 We have the additional caveat that a SELECT construct could have
7920 been a computed GOTO in the source code. Fortunately we can fairly
7921 easily work around that here: The case_expr for a "real" SELECT CASE
7922 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7923 we have to do is make sure that the case_expr is a scalar integer
7924 expression. */
7925
7926 static void
7927 resolve_select (gfc_code *code, bool select_type)
7928 {
7929 gfc_code *body;
7930 gfc_expr *case_expr;
7931 gfc_case *cp, *default_case, *tail, *head;
7932 int seen_unreachable;
7933 int seen_logical;
7934 int ncases;
7935 bt type;
7936 bool t;
7937
7938 if (code->expr1 == NULL)
7939 {
7940 /* This was actually a computed GOTO statement. */
7941 case_expr = code->expr2;
7942 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7943 gfc_error ("Selection expression in computed GOTO statement "
7944 "at %L must be a scalar integer expression",
7945 &case_expr->where);
7946
7947 /* Further checking is not necessary because this SELECT was built
7948 by the compiler, so it should always be OK. Just move the
7949 case_expr from expr2 to expr so that we can handle computed
7950 GOTOs as normal SELECTs from here on. */
7951 code->expr1 = code->expr2;
7952 code->expr2 = NULL;
7953 return;
7954 }
7955
7956 case_expr = code->expr1;
7957 type = case_expr->ts.type;
7958
7959 /* F08:C830. */
7960 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7961 {
7962 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7963 &case_expr->where, gfc_typename (&case_expr->ts));
7964
7965 /* Punt. Going on here just produce more garbage error messages. */
7966 return;
7967 }
7968
7969 /* F08:R842. */
7970 if (!select_type && case_expr->rank != 0)
7971 {
7972 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7973 "expression", &case_expr->where);
7974
7975 /* Punt. */
7976 return;
7977 }
7978
7979 /* Raise a warning if an INTEGER case value exceeds the range of
7980 the case-expr. Later, all expressions will be promoted to the
7981 largest kind of all case-labels. */
7982
7983 if (type == BT_INTEGER)
7984 for (body = code->block; body; body = body->block)
7985 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7986 {
7987 if (cp->low
7988 && gfc_check_integer_range (cp->low->value.integer,
7989 case_expr->ts.kind) != ARITH_OK)
7990 gfc_warning (0, "Expression in CASE statement at %L is "
7991 "not in the range of %s", &cp->low->where,
7992 gfc_typename (&case_expr->ts));
7993
7994 if (cp->high
7995 && cp->low != cp->high
7996 && gfc_check_integer_range (cp->high->value.integer,
7997 case_expr->ts.kind) != ARITH_OK)
7998 gfc_warning (0, "Expression in CASE statement at %L is "
7999 "not in the range of %s", &cp->high->where,
8000 gfc_typename (&case_expr->ts));
8001 }
8002
8003 /* PR 19168 has a long discussion concerning a mismatch of the kinds
8004 of the SELECT CASE expression and its CASE values. Walk the lists
8005 of case values, and if we find a mismatch, promote case_expr to
8006 the appropriate kind. */
8007
8008 if (type == BT_LOGICAL || type == BT_INTEGER)
8009 {
8010 for (body = code->block; body; body = body->block)
8011 {
8012 /* Walk the case label list. */
8013 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8014 {
8015 /* Intercept the DEFAULT case. It does not have a kind. */
8016 if (cp->low == NULL && cp->high == NULL)
8017 continue;
8018
8019 /* Unreachable case ranges are discarded, so ignore. */
8020 if (cp->low != NULL && cp->high != NULL
8021 && cp->low != cp->high
8022 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8023 continue;
8024
8025 if (cp->low != NULL
8026 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
8027 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
8028
8029 if (cp->high != NULL
8030 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
8031 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
8032 }
8033 }
8034 }
8035
8036 /* Assume there is no DEFAULT case. */
8037 default_case = NULL;
8038 head = tail = NULL;
8039 ncases = 0;
8040 seen_logical = 0;
8041
8042 for (body = code->block; body; body = body->block)
8043 {
8044 /* Assume the CASE list is OK, and all CASE labels can be matched. */
8045 t = true;
8046 seen_unreachable = 0;
8047
8048 /* Walk the case label list, making sure that all case labels
8049 are legal. */
8050 for (cp = body->ext.block.case_list; cp; cp = cp->next)
8051 {
8052 /* Count the number of cases in the whole construct. */
8053 ncases++;
8054
8055 /* Intercept the DEFAULT case. */
8056 if (cp->low == NULL && cp->high == NULL)
8057 {
8058 if (default_case != NULL)
8059 {
8060 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8061 "by a second DEFAULT CASE at %L",
8062 &default_case->where, &cp->where);
8063 t = false;
8064 break;
8065 }
8066 else
8067 {
8068 default_case = cp;
8069 continue;
8070 }
8071 }
8072
8073 /* Deal with single value cases and case ranges. Errors are
8074 issued from the validation function. */
8075 if (!validate_case_label_expr (cp->low, case_expr)
8076 || !validate_case_label_expr (cp->high, case_expr))
8077 {
8078 t = false;
8079 break;
8080 }
8081
8082 if (type == BT_LOGICAL
8083 && ((cp->low == NULL || cp->high == NULL)
8084 || cp->low != cp->high))
8085 {
8086 gfc_error ("Logical range in CASE statement at %L is not "
8087 "allowed", &cp->low->where);
8088 t = false;
8089 break;
8090 }
8091
8092 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
8093 {
8094 int value;
8095 value = cp->low->value.logical == 0 ? 2 : 1;
8096 if (value & seen_logical)
8097 {
8098 gfc_error ("Constant logical value in CASE statement "
8099 "is repeated at %L",
8100 &cp->low->where);
8101 t = false;
8102 break;
8103 }
8104 seen_logical |= value;
8105 }
8106
8107 if (cp->low != NULL && cp->high != NULL
8108 && cp->low != cp->high
8109 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
8110 {
8111 if (warn_surprising)
8112 gfc_warning (OPT_Wsurprising,
8113 "Range specification at %L can never be matched",
8114 &cp->where);
8115
8116 cp->unreachable = 1;
8117 seen_unreachable = 1;
8118 }
8119 else
8120 {
8121 /* If the case range can be matched, it can also overlap with
8122 other cases. To make sure it does not, we put it in a
8123 double linked list here. We sort that with a merge sort
8124 later on to detect any overlapping cases. */
8125 if (!head)
8126 {
8127 head = tail = cp;
8128 head->right = head->left = NULL;
8129 }
8130 else
8131 {
8132 tail->right = cp;
8133 tail->right->left = tail;
8134 tail = tail->right;
8135 tail->right = NULL;
8136 }
8137 }
8138 }
8139
8140 /* It there was a failure in the previous case label, give up
8141 for this case label list. Continue with the next block. */
8142 if (!t)
8143 continue;
8144
8145 /* See if any case labels that are unreachable have been seen.
8146 If so, we eliminate them. This is a bit of a kludge because
8147 the case lists for a single case statement (label) is a
8148 single forward linked lists. */
8149 if (seen_unreachable)
8150 {
8151 /* Advance until the first case in the list is reachable. */
8152 while (body->ext.block.case_list != NULL
8153 && body->ext.block.case_list->unreachable)
8154 {
8155 gfc_case *n = body->ext.block.case_list;
8156 body->ext.block.case_list = body->ext.block.case_list->next;
8157 n->next = NULL;
8158 gfc_free_case_list (n);
8159 }
8160
8161 /* Strip all other unreachable cases. */
8162 if (body->ext.block.case_list)
8163 {
8164 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
8165 {
8166 if (cp->next->unreachable)
8167 {
8168 gfc_case *n = cp->next;
8169 cp->next = cp->next->next;
8170 n->next = NULL;
8171 gfc_free_case_list (n);
8172 }
8173 }
8174 }
8175 }
8176 }
8177
8178 /* See if there were overlapping cases. If the check returns NULL,
8179 there was overlap. In that case we don't do anything. If head
8180 is non-NULL, we prepend the DEFAULT case. The sorted list can
8181 then used during code generation for SELECT CASE constructs with
8182 a case expression of a CHARACTER type. */
8183 if (head)
8184 {
8185 head = check_case_overlap (head);
8186
8187 /* Prepend the default_case if it is there. */
8188 if (head != NULL && default_case)
8189 {
8190 default_case->left = NULL;
8191 default_case->right = head;
8192 head->left = default_case;
8193 }
8194 }
8195
8196 /* Eliminate dead blocks that may be the result if we've seen
8197 unreachable case labels for a block. */
8198 for (body = code; body && body->block; body = body->block)
8199 {
8200 if (body->block->ext.block.case_list == NULL)
8201 {
8202 /* Cut the unreachable block from the code chain. */
8203 gfc_code *c = body->block;
8204 body->block = c->block;
8205
8206 /* Kill the dead block, but not the blocks below it. */
8207 c->block = NULL;
8208 gfc_free_statements (c);
8209 }
8210 }
8211
8212 /* More than two cases is legal but insane for logical selects.
8213 Issue a warning for it. */
8214 if (warn_surprising && type == BT_LOGICAL && ncases > 2)
8215 gfc_warning (OPT_Wsurprising,
8216 "Logical SELECT CASE block at %L has more that two cases",
8217 &code->loc);
8218 }
8219
8220
8221 /* Check if a derived type is extensible. */
8222
8223 bool
8224 gfc_type_is_extensible (gfc_symbol *sym)
8225 {
8226 return !(sym->attr.is_bind_c || sym->attr.sequence
8227 || (sym->attr.is_class
8228 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
8229 }
8230
8231
8232 static void
8233 resolve_types (gfc_namespace *ns);
8234
8235 /* Resolve an associate-name: Resolve target and ensure the type-spec is
8236 correct as well as possibly the array-spec. */
8237
8238 static void
8239 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
8240 {
8241 gfc_expr* target;
8242
8243 gcc_assert (sym->assoc);
8244 gcc_assert (sym->attr.flavor == FL_VARIABLE);
8245
8246 /* If this is for SELECT TYPE, the target may not yet be set. In that
8247 case, return. Resolution will be called later manually again when
8248 this is done. */
8249 target = sym->assoc->target;
8250 if (!target)
8251 return;
8252 gcc_assert (!sym->assoc->dangling);
8253
8254 if (resolve_target && !gfc_resolve_expr (target))
8255 return;
8256
8257 /* For variable targets, we get some attributes from the target. */
8258 if (target->expr_type == EXPR_VARIABLE)
8259 {
8260 gfc_symbol* tsym;
8261
8262 gcc_assert (target->symtree);
8263 tsym = target->symtree->n.sym;
8264
8265 sym->attr.asynchronous = tsym->attr.asynchronous;
8266 sym->attr.volatile_ = tsym->attr.volatile_;
8267
8268 sym->attr.target = tsym->attr.target
8269 || gfc_expr_attr (target).pointer;
8270 if (is_subref_array (target))
8271 sym->attr.subref_array_pointer = 1;
8272 }
8273
8274 /* Get type if this was not already set. Note that it can be
8275 some other type than the target in case this is a SELECT TYPE
8276 selector! So we must not update when the type is already there. */
8277 if (sym->ts.type == BT_UNKNOWN)
8278 sym->ts = target->ts;
8279 gcc_assert (sym->ts.type != BT_UNKNOWN);
8280
8281 /* See if this is a valid association-to-variable. */
8282 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
8283 && !gfc_has_vector_subscript (target));
8284
8285 /* Finally resolve if this is an array or not. */
8286 if (sym->attr.dimension && target->rank == 0)
8287 {
8288 /* primary.c makes the assumption that a reference to an associate
8289 name followed by a left parenthesis is an array reference. */
8290 if (sym->ts.type != BT_CHARACTER)
8291 gfc_error ("Associate-name %qs at %L is used as array",
8292 sym->name, &sym->declared_at);
8293 sym->attr.dimension = 0;
8294 return;
8295 }
8296
8297
8298 /* We cannot deal with class selectors that need temporaries. */
8299 if (target->ts.type == BT_CLASS
8300 && gfc_ref_needs_temporary_p (target->ref))
8301 {
8302 gfc_error ("CLASS selector at %L needs a temporary which is not "
8303 "yet implemented", &target->where);
8304 return;
8305 }
8306
8307 if (target->ts.type == BT_CLASS)
8308 gfc_fix_class_refs (target);
8309
8310 if (target->rank != 0)
8311 {
8312 gfc_array_spec *as;
8313 /* The rank may be incorrectly guessed at parsing, therefore make sure
8314 it is corrected now. */
8315 if (sym->ts.type != BT_CLASS && (!sym->as || sym->assoc->rankguessed))
8316 {
8317 if (!sym->as)
8318 sym->as = gfc_get_array_spec ();
8319 as = sym->as;
8320 as->rank = target->rank;
8321 as->type = AS_DEFERRED;
8322 as->corank = gfc_get_corank (target);
8323 sym->attr.dimension = 1;
8324 if (as->corank != 0)
8325 sym->attr.codimension = 1;
8326 }
8327 }
8328 else
8329 {
8330 /* target's rank is 0, but the type of the sym is still array valued,
8331 which has to be corrected. */
8332 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
8333 {
8334 gfc_array_spec *as;
8335 symbol_attribute attr;
8336 /* The associated variable's type is still the array type
8337 correct this now. */
8338 gfc_typespec *ts = &target->ts;
8339 gfc_ref *ref;
8340 gfc_component *c;
8341 for (ref = target->ref; ref != NULL; ref = ref->next)
8342 {
8343 switch (ref->type)
8344 {
8345 case REF_COMPONENT:
8346 ts = &ref->u.c.component->ts;
8347 break;
8348 case REF_ARRAY:
8349 if (ts->type == BT_CLASS)
8350 ts = &ts->u.derived->components->ts;
8351 break;
8352 default:
8353 break;
8354 }
8355 }
8356 /* Create a scalar instance of the current class type. Because the
8357 rank of a class array goes into its name, the type has to be
8358 rebuild. The alternative of (re-)setting just the attributes
8359 and as in the current type, destroys the type also in other
8360 places. */
8361 as = NULL;
8362 sym->ts = *ts;
8363 sym->ts.type = BT_CLASS;
8364 attr = CLASS_DATA (sym)->attr;
8365 attr.class_ok = 0;
8366 attr.associate_var = 1;
8367 attr.dimension = attr.codimension = 0;
8368 attr.class_pointer = 1;
8369 if (!gfc_build_class_symbol (&sym->ts, &attr, &as))
8370 gcc_unreachable ();
8371 /* Make sure the _vptr is set. */
8372 c = gfc_find_component (sym->ts.u.derived, "_vptr", true, true, NULL);
8373 if (c->ts.u.derived == NULL)
8374 c->ts.u.derived = gfc_find_derived_vtab (sym->ts.u.derived);
8375 CLASS_DATA (sym)->attr.pointer = 1;
8376 CLASS_DATA (sym)->attr.class_pointer = 1;
8377 gfc_set_sym_referenced (sym->ts.u.derived);
8378 gfc_commit_symbol (sym->ts.u.derived);
8379 /* _vptr now has the _vtab in it, change it to the _vtype. */
8380 if (c->ts.u.derived->attr.vtab)
8381 c->ts.u.derived = c->ts.u.derived->ts.u.derived;
8382 c->ts.u.derived->ns->types_resolved = 0;
8383 resolve_types (c->ts.u.derived->ns);
8384 }
8385 }
8386
8387 /* Mark this as an associate variable. */
8388 sym->attr.associate_var = 1;
8389
8390 /* Fix up the type-spec for CHARACTER types. */
8391 if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
8392 {
8393 if (!sym->ts.u.cl)
8394 sym->ts.u.cl = target->ts.u.cl;
8395
8396 if (!sym->ts.u.cl->length)
8397 sym->ts.u.cl->length
8398 = gfc_get_int_expr (gfc_default_integer_kind,
8399 NULL, target->value.character.length);
8400 }
8401
8402 /* If the target is a good class object, so is the associate variable. */
8403 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
8404 sym->attr.class_ok = 1;
8405 }
8406
8407
8408 /* Ensure that SELECT TYPE expressions have the correct rank and a full
8409 array reference, where necessary. The symbols are artificial and so
8410 the dimension attribute and arrayspec can also be set. In addition,
8411 sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
8412 This is corrected here as well.*/
8413
8414 static void
8415 fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2,
8416 int rank, gfc_ref *ref)
8417 {
8418 gfc_ref *nref = (*expr1)->ref;
8419 gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
8420 gfc_symbol *sym2 = expr2 ? expr2->symtree->n.sym : NULL;
8421 (*expr1)->rank = rank;
8422 if (sym1->ts.type == BT_CLASS)
8423 {
8424 if ((*expr1)->ts.type != BT_CLASS)
8425 (*expr1)->ts = sym1->ts;
8426
8427 CLASS_DATA (sym1)->attr.dimension = 1;
8428 if (CLASS_DATA (sym1)->as == NULL && sym2)
8429 CLASS_DATA (sym1)->as
8430 = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
8431 }
8432 else
8433 {
8434 sym1->attr.dimension = 1;
8435 if (sym1->as == NULL && sym2)
8436 sym1->as = gfc_copy_array_spec (sym2->as);
8437 }
8438
8439 for (; nref; nref = nref->next)
8440 if (nref->next == NULL)
8441 break;
8442
8443 if (ref && nref && nref->type != REF_ARRAY)
8444 nref->next = gfc_copy_ref (ref);
8445 else if (ref && !nref)
8446 (*expr1)->ref = gfc_copy_ref (ref);
8447 }
8448
8449
8450 static gfc_expr *
8451 build_loc_call (gfc_expr *sym_expr)
8452 {
8453 gfc_expr *loc_call;
8454 loc_call = gfc_get_expr ();
8455 loc_call->expr_type = EXPR_FUNCTION;
8456 gfc_get_sym_tree ("loc", gfc_current_ns, &loc_call->symtree, false);
8457 loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
8458 loc_call->symtree->n.sym->attr.intrinsic = 1;
8459 loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
8460 gfc_commit_symbol (loc_call->symtree->n.sym);
8461 loc_call->ts.type = BT_INTEGER;
8462 loc_call->ts.kind = gfc_index_integer_kind;
8463 loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
8464 loc_call->value.function.actual = gfc_get_actual_arglist ();
8465 loc_call->value.function.actual->expr = sym_expr;
8466 loc_call->where = sym_expr->where;
8467 return loc_call;
8468 }
8469
8470 /* Resolve a SELECT TYPE statement. */
8471
8472 static void
8473 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
8474 {
8475 gfc_symbol *selector_type;
8476 gfc_code *body, *new_st, *if_st, *tail;
8477 gfc_code *class_is = NULL, *default_case = NULL;
8478 gfc_case *c;
8479 gfc_symtree *st;
8480 char name[GFC_MAX_SYMBOL_LEN];
8481 gfc_namespace *ns;
8482 int error = 0;
8483 int charlen = 0;
8484 int rank = 0;
8485 gfc_ref* ref = NULL;
8486 gfc_expr *selector_expr = NULL;
8487
8488 ns = code->ext.block.ns;
8489 gfc_resolve (ns);
8490
8491 /* Check for F03:C813. */
8492 if (code->expr1->ts.type != BT_CLASS
8493 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
8494 {
8495 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
8496 "at %L", &code->loc);
8497 return;
8498 }
8499
8500 if (!code->expr1->symtree->n.sym->attr.class_ok)
8501 return;
8502
8503 if (code->expr2)
8504 {
8505 if (code->expr1->symtree->n.sym->attr.untyped)
8506 code->expr1->symtree->n.sym->ts = code->expr2->ts;
8507 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
8508
8509 /* F2008: C803 The selector expression must not be coindexed. */
8510 if (gfc_is_coindexed (code->expr2))
8511 {
8512 gfc_error ("Selector at %L must not be coindexed",
8513 &code->expr2->where);
8514 return;
8515 }
8516
8517 }
8518 else
8519 {
8520 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
8521
8522 if (gfc_is_coindexed (code->expr1))
8523 {
8524 gfc_error ("Selector at %L must not be coindexed",
8525 &code->expr1->where);
8526 return;
8527 }
8528 }
8529
8530 /* Loop over TYPE IS / CLASS IS cases. */
8531 for (body = code->block; body; body = body->block)
8532 {
8533 c = body->ext.block.case_list;
8534
8535 if (!error)
8536 {
8537 /* Check for repeated cases. */
8538 for (tail = code->block; tail; tail = tail->block)
8539 {
8540 gfc_case *d = tail->ext.block.case_list;
8541 if (tail == body)
8542 break;
8543
8544 if (c->ts.type == d->ts.type
8545 && ((c->ts.type == BT_DERIVED
8546 && c->ts.u.derived && d->ts.u.derived
8547 && !strcmp (c->ts.u.derived->name,
8548 d->ts.u.derived->name))
8549 || c->ts.type == BT_UNKNOWN
8550 || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8551 && c->ts.kind == d->ts.kind)))
8552 {
8553 gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
8554 &c->where, &d->where);
8555 return;
8556 }
8557 }
8558 }
8559
8560 /* Check F03:C815. */
8561 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8562 && !selector_type->attr.unlimited_polymorphic
8563 && !gfc_type_is_extensible (c->ts.u.derived))
8564 {
8565 gfc_error ("Derived type %qs at %L must be extensible",
8566 c->ts.u.derived->name, &c->where);
8567 error++;
8568 continue;
8569 }
8570
8571 /* Check F03:C816. */
8572 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
8573 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
8574 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
8575 {
8576 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8577 gfc_error ("Derived type %qs at %L must be an extension of %qs",
8578 c->ts.u.derived->name, &c->where, selector_type->name);
8579 else
8580 gfc_error ("Unexpected intrinsic type %qs at %L",
8581 gfc_basic_typename (c->ts.type), &c->where);
8582 error++;
8583 continue;
8584 }
8585
8586 /* Check F03:C814. */
8587 if (c->ts.type == BT_CHARACTER
8588 && (c->ts.u.cl->length != NULL || c->ts.deferred))
8589 {
8590 gfc_error ("The type-spec at %L shall specify that each length "
8591 "type parameter is assumed", &c->where);
8592 error++;
8593 continue;
8594 }
8595
8596 /* Intercept the DEFAULT case. */
8597 if (c->ts.type == BT_UNKNOWN)
8598 {
8599 /* Check F03:C818. */
8600 if (default_case)
8601 {
8602 gfc_error ("The DEFAULT CASE at %L cannot be followed "
8603 "by a second DEFAULT CASE at %L",
8604 &default_case->ext.block.case_list->where, &c->where);
8605 error++;
8606 continue;
8607 }
8608
8609 default_case = body;
8610 }
8611 }
8612
8613 if (error > 0)
8614 return;
8615
8616 /* Transform SELECT TYPE statement to BLOCK and associate selector to
8617 target if present. If there are any EXIT statements referring to the
8618 SELECT TYPE construct, this is no problem because the gfc_code
8619 reference stays the same and EXIT is equally possible from the BLOCK
8620 it is changed to. */
8621 code->op = EXEC_BLOCK;
8622 if (code->expr2)
8623 {
8624 gfc_association_list* assoc;
8625
8626 assoc = gfc_get_association_list ();
8627 assoc->st = code->expr1->symtree;
8628 assoc->target = gfc_copy_expr (code->expr2);
8629 assoc->target->where = code->expr2->where;
8630 /* assoc->variable will be set by resolve_assoc_var. */
8631
8632 code->ext.block.assoc = assoc;
8633 code->expr1->symtree->n.sym->assoc = assoc;
8634
8635 resolve_assoc_var (code->expr1->symtree->n.sym, false);
8636 }
8637 else
8638 code->ext.block.assoc = NULL;
8639
8640 /* Ensure that the selector rank and arrayspec are available to
8641 correct expressions in which they might be missing. */
8642 if (code->expr2 && code->expr2->rank)
8643 {
8644 rank = code->expr2->rank;
8645 for (ref = code->expr2->ref; ref; ref = ref->next)
8646 if (ref->next == NULL)
8647 break;
8648 if (ref && ref->type == REF_ARRAY)
8649 ref = gfc_copy_ref (ref);
8650
8651 /* Fixup expr1 if necessary. */
8652 if (rank)
8653 fixup_array_ref (&code->expr1, code->expr2, rank, ref);
8654 }
8655 else if (code->expr1->rank)
8656 {
8657 rank = code->expr1->rank;
8658 for (ref = code->expr1->ref; ref; ref = ref->next)
8659 if (ref->next == NULL)
8660 break;
8661 if (ref && ref->type == REF_ARRAY)
8662 ref = gfc_copy_ref (ref);
8663 }
8664
8665 /* Add EXEC_SELECT to switch on type. */
8666 new_st = gfc_get_code (code->op);
8667 new_st->expr1 = code->expr1;
8668 new_st->expr2 = code->expr2;
8669 new_st->block = code->block;
8670 code->expr1 = code->expr2 = NULL;
8671 code->block = NULL;
8672 if (!ns->code)
8673 ns->code = new_st;
8674 else
8675 ns->code->next = new_st;
8676 code = new_st;
8677 code->op = EXEC_SELECT_TYPE;
8678
8679 /* Use the intrinsic LOC function to generate an integer expression
8680 for the vtable of the selector. Note that the rank of the selector
8681 expression has to be set to zero. */
8682 gfc_add_vptr_component (code->expr1);
8683 code->expr1->rank = 0;
8684 code->expr1 = build_loc_call (code->expr1);
8685 selector_expr = code->expr1->value.function.actual->expr;
8686
8687 /* Loop over TYPE IS / CLASS IS cases. */
8688 for (body = code->block; body; body = body->block)
8689 {
8690 gfc_symbol *vtab;
8691 gfc_expr *e;
8692 c = body->ext.block.case_list;
8693
8694 /* Generate an index integer expression for address of the
8695 TYPE/CLASS vtable and store it in c->low. The hash expression
8696 is stored in c->high and is used to resolve intrinsic cases. */
8697 if (c->ts.type != BT_UNKNOWN)
8698 {
8699 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8700 {
8701 vtab = gfc_find_derived_vtab (c->ts.u.derived);
8702 gcc_assert (vtab);
8703 c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
8704 c->ts.u.derived->hash_value);
8705 }
8706 else
8707 {
8708 vtab = gfc_find_vtab (&c->ts);
8709 gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
8710 e = CLASS_DATA (vtab)->initializer;
8711 c->high = gfc_copy_expr (e);
8712 }
8713
8714 e = gfc_lval_expr_from_sym (vtab);
8715 c->low = build_loc_call (e);
8716 }
8717 else
8718 continue;
8719
8720 /* Associate temporary to selector. This should only be done
8721 when this case is actually true, so build a new ASSOCIATE
8722 that does precisely this here (instead of using the
8723 'global' one). */
8724
8725 if (c->ts.type == BT_CLASS)
8726 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
8727 else if (c->ts.type == BT_DERIVED)
8728 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
8729 else if (c->ts.type == BT_CHARACTER)
8730 {
8731 if (c->ts.u.cl && c->ts.u.cl->length
8732 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8733 charlen = mpz_get_si (c->ts.u.cl->length->value.integer);
8734 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (c->ts.type),
8735 charlen, c->ts.kind);
8736 }
8737 else
8738 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
8739 c->ts.kind);
8740
8741 st = gfc_find_symtree (ns->sym_root, name);
8742 gcc_assert (st->n.sym->assoc);
8743 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
8744 st->n.sym->assoc->target->where = selector_expr->where;
8745 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8746 {
8747 gfc_add_data_component (st->n.sym->assoc->target);
8748 /* Fixup the target expression if necessary. */
8749 if (rank)
8750 fixup_array_ref (&st->n.sym->assoc->target, NULL, rank, ref);
8751 }
8752
8753 new_st = gfc_get_code (EXEC_BLOCK);
8754 new_st->ext.block.ns = gfc_build_block_ns (ns);
8755 new_st->ext.block.ns->code = body->next;
8756 body->next = new_st;
8757
8758 /* Chain in the new list only if it is marked as dangling. Otherwise
8759 there is a CASE label overlap and this is already used. Just ignore,
8760 the error is diagnosed elsewhere. */
8761 if (st->n.sym->assoc->dangling)
8762 {
8763 new_st->ext.block.assoc = st->n.sym->assoc;
8764 st->n.sym->assoc->dangling = 0;
8765 }
8766
8767 resolve_assoc_var (st->n.sym, false);
8768 }
8769
8770 /* Take out CLASS IS cases for separate treatment. */
8771 body = code;
8772 while (body && body->block)
8773 {
8774 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
8775 {
8776 /* Add to class_is list. */
8777 if (class_is == NULL)
8778 {
8779 class_is = body->block;
8780 tail = class_is;
8781 }
8782 else
8783 {
8784 for (tail = class_is; tail->block; tail = tail->block) ;
8785 tail->block = body->block;
8786 tail = tail->block;
8787 }
8788 /* Remove from EXEC_SELECT list. */
8789 body->block = body->block->block;
8790 tail->block = NULL;
8791 }
8792 else
8793 body = body->block;
8794 }
8795
8796 if (class_is)
8797 {
8798 gfc_symbol *vtab;
8799
8800 if (!default_case)
8801 {
8802 /* Add a default case to hold the CLASS IS cases. */
8803 for (tail = code; tail->block; tail = tail->block) ;
8804 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
8805 tail = tail->block;
8806 tail->ext.block.case_list = gfc_get_case ();
8807 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
8808 tail->next = NULL;
8809 default_case = tail;
8810 }
8811
8812 /* More than one CLASS IS block? */
8813 if (class_is->block)
8814 {
8815 gfc_code **c1,*c2;
8816 bool swapped;
8817 /* Sort CLASS IS blocks by extension level. */
8818 do
8819 {
8820 swapped = false;
8821 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8822 {
8823 c2 = (*c1)->block;
8824 /* F03:C817 (check for doubles). */
8825 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8826 == c2->ext.block.case_list->ts.u.derived->hash_value)
8827 {
8828 gfc_error ("Double CLASS IS block in SELECT TYPE "
8829 "statement at %L",
8830 &c2->ext.block.case_list->where);
8831 return;
8832 }
8833 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8834 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8835 {
8836 /* Swap. */
8837 (*c1)->block = c2->block;
8838 c2->block = *c1;
8839 *c1 = c2;
8840 swapped = true;
8841 }
8842 }
8843 }
8844 while (swapped);
8845 }
8846
8847 /* Generate IF chain. */
8848 if_st = gfc_get_code (EXEC_IF);
8849 new_st = if_st;
8850 for (body = class_is; body; body = body->block)
8851 {
8852 new_st->block = gfc_get_code (EXEC_IF);
8853 new_st = new_st->block;
8854 /* Set up IF condition: Call _gfortran_is_extension_of. */
8855 new_st->expr1 = gfc_get_expr ();
8856 new_st->expr1->expr_type = EXPR_FUNCTION;
8857 new_st->expr1->ts.type = BT_LOGICAL;
8858 new_st->expr1->ts.kind = 4;
8859 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8860 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8861 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8862 /* Set up arguments. */
8863 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8864 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
8865 new_st->expr1->value.function.actual->expr->where = code->loc;
8866 new_st->expr1->where = code->loc;
8867 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
8868 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
8869 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8870 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8871 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8872 new_st->expr1->value.function.actual->next->expr->where = code->loc;
8873 new_st->next = body->next;
8874 }
8875 if (default_case->next)
8876 {
8877 new_st->block = gfc_get_code (EXEC_IF);
8878 new_st = new_st->block;
8879 new_st->next = default_case->next;
8880 }
8881
8882 /* Replace CLASS DEFAULT code by the IF chain. */
8883 default_case->next = if_st;
8884 }
8885
8886 /* Resolve the internal code. This can not be done earlier because
8887 it requires that the sym->assoc of selectors is set already. */
8888 gfc_current_ns = ns;
8889 gfc_resolve_blocks (code->block, gfc_current_ns);
8890 gfc_current_ns = old_ns;
8891
8892 if (ref)
8893 free (ref);
8894 }
8895
8896
8897 /* Resolve a transfer statement. This is making sure that:
8898 -- a derived type being transferred has only non-pointer components
8899 -- a derived type being transferred doesn't have private components, unless
8900 it's being transferred from the module where the type was defined
8901 -- we're not trying to transfer a whole assumed size array. */
8902
8903 static void
8904 resolve_transfer (gfc_code *code)
8905 {
8906 gfc_typespec *ts;
8907 gfc_symbol *sym, *derived;
8908 gfc_ref *ref;
8909 gfc_expr *exp;
8910 bool write = false;
8911 bool formatted = false;
8912 gfc_dt *dt = code->ext.dt;
8913 gfc_symbol *dtio_sub = NULL;
8914
8915 exp = code->expr1;
8916
8917 while (exp != NULL && exp->expr_type == EXPR_OP
8918 && exp->value.op.op == INTRINSIC_PARENTHESES)
8919 exp = exp->value.op.op1;
8920
8921 if (exp && exp->expr_type == EXPR_NULL
8922 && code->ext.dt)
8923 {
8924 gfc_error ("Invalid context for NULL () intrinsic at %L",
8925 &exp->where);
8926 return;
8927 }
8928
8929 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8930 && exp->expr_type != EXPR_FUNCTION
8931 && exp->expr_type != EXPR_STRUCTURE))
8932 return;
8933
8934 /* If we are reading, the variable will be changed. Note that
8935 code->ext.dt may be NULL if the TRANSFER is related to
8936 an INQUIRE statement -- but in this case, we are not reading, either. */
8937 if (dt && dt->dt_io_kind->value.iokind == M_READ
8938 && !gfc_check_vardef_context (exp, false, false, false,
8939 _("item in READ")))
8940 return;
8941
8942 ts = exp->expr_type == EXPR_STRUCTURE ? &exp->ts : &exp->symtree->n.sym->ts;
8943
8944 /* Go to actual component transferred. */
8945 for (ref = exp->ref; ref; ref = ref->next)
8946 if (ref->type == REF_COMPONENT)
8947 ts = &ref->u.c.component->ts;
8948
8949 if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
8950 && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
8951 {
8952 if (ts->type == BT_DERIVED)
8953 derived = ts->u.derived;
8954 else
8955 derived = ts->u.derived->components->ts.u.derived;
8956
8957 if (dt->format_expr)
8958 {
8959 char *fmt;
8960 fmt = gfc_widechar_to_char (dt->format_expr->value.character.string,
8961 -1);
8962 if (strtok (fmt, "DT") != NULL)
8963 formatted = true;
8964 }
8965 else if (dt->format_label == &format_asterisk)
8966 {
8967 /* List directed io must call the formatted DTIO procedure. */
8968 formatted = true;
8969 }
8970
8971 write = dt->dt_io_kind->value.iokind == M_WRITE
8972 || dt->dt_io_kind->value.iokind == M_PRINT;
8973 dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
8974
8975 if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
8976 {
8977 dt->udtio = exp;
8978 sym = exp->symtree->n.sym->ns->proc_name;
8979 /* Check to see if this is a nested DTIO call, with the
8980 dummy as the io-list object. */
8981 if (sym && sym == dtio_sub && sym->formal
8982 && sym->formal->sym == exp->symtree->n.sym
8983 && exp->ref == NULL)
8984 {
8985 if (!sym->attr.recursive)
8986 {
8987 gfc_error ("DTIO %s procedure at %L must be recursive",
8988 sym->name, &sym->declared_at);
8989 return;
8990 }
8991 }
8992 }
8993 }
8994
8995 if (ts->type == BT_CLASS && dtio_sub == NULL)
8996 {
8997 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8998 "it is processed by a defined input/output procedure",
8999 &code->loc);
9000 return;
9001 }
9002
9003 if (ts->type == BT_DERIVED)
9004 {
9005 /* Check that transferred derived type doesn't contain POINTER
9006 components unless it is processed by a defined input/output
9007 procedure". */
9008 if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
9009 {
9010 gfc_error ("Data transfer element at %L cannot have POINTER "
9011 "components unless it is processed by a defined "
9012 "input/output procedure", &code->loc);
9013 return;
9014 }
9015
9016 /* F08:C935. */
9017 if (ts->u.derived->attr.proc_pointer_comp)
9018 {
9019 gfc_error ("Data transfer element at %L cannot have "
9020 "procedure pointer components", &code->loc);
9021 return;
9022 }
9023
9024 if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
9025 {
9026 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
9027 "components unless it is processed by a defined "
9028 "input/output procedure", &code->loc);
9029 return;
9030 }
9031
9032 /* C_PTR and C_FUNPTR have private components which means they can not
9033 be printed. However, if -std=gnu and not -pedantic, allow
9034 the component to be printed to help debugging. */
9035 if (ts->u.derived->ts.f90_type == BT_VOID)
9036 {
9037 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
9038 "cannot have PRIVATE components", &code->loc))
9039 return;
9040 }
9041 else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
9042 {
9043 gfc_error ("Data transfer element at %L cannot have "
9044 "PRIVATE components unless it is processed by "
9045 "a defined input/output procedure", &code->loc);
9046 return;
9047 }
9048 }
9049
9050 if (exp->expr_type == EXPR_STRUCTURE)
9051 return;
9052
9053 sym = exp->symtree->n.sym;
9054
9055 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
9056 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
9057 {
9058 gfc_error ("Data transfer element at %L cannot be a full reference to "
9059 "an assumed-size array", &code->loc);
9060 return;
9061 }
9062 }
9063
9064
9065 /*********** Toplevel code resolution subroutines ***********/
9066
9067 /* Find the set of labels that are reachable from this block. We also
9068 record the last statement in each block. */
9069
9070 static void
9071 find_reachable_labels (gfc_code *block)
9072 {
9073 gfc_code *c;
9074
9075 if (!block)
9076 return;
9077
9078 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
9079
9080 /* Collect labels in this block. We don't keep those corresponding
9081 to END {IF|SELECT}, these are checked in resolve_branch by going
9082 up through the code_stack. */
9083 for (c = block; c; c = c->next)
9084 {
9085 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
9086 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
9087 }
9088
9089 /* Merge with labels from parent block. */
9090 if (cs_base->prev)
9091 {
9092 gcc_assert (cs_base->prev->reachable_labels);
9093 bitmap_ior_into (cs_base->reachable_labels,
9094 cs_base->prev->reachable_labels);
9095 }
9096 }
9097
9098
9099 static void
9100 resolve_lock_unlock_event (gfc_code *code)
9101 {
9102 if (code->expr1->expr_type == EXPR_FUNCTION
9103 && code->expr1->value.function.isym
9104 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
9105 remove_caf_get_intrinsic (code->expr1);
9106
9107 if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
9108 && (code->expr1->ts.type != BT_DERIVED
9109 || code->expr1->expr_type != EXPR_VARIABLE
9110 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
9111 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
9112 || code->expr1->rank != 0
9113 || (!gfc_is_coarray (code->expr1) &&
9114 !gfc_is_coindexed (code->expr1))))
9115 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
9116 &code->expr1->where);
9117 else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
9118 && (code->expr1->ts.type != BT_DERIVED
9119 || code->expr1->expr_type != EXPR_VARIABLE
9120 || code->expr1->ts.u.derived->from_intmod
9121 != INTMOD_ISO_FORTRAN_ENV
9122 || code->expr1->ts.u.derived->intmod_sym_id
9123 != ISOFORTRAN_EVENT_TYPE
9124 || code->expr1->rank != 0))
9125 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
9126 &code->expr1->where);
9127 else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
9128 && !gfc_is_coindexed (code->expr1))
9129 gfc_error ("Event variable argument at %L must be a coarray or coindexed",
9130 &code->expr1->where);
9131 else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
9132 gfc_error ("Event variable argument at %L must be a coarray but not "
9133 "coindexed", &code->expr1->where);
9134
9135 /* Check STAT. */
9136 if (code->expr2
9137 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
9138 || code->expr2->expr_type != EXPR_VARIABLE))
9139 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
9140 &code->expr2->where);
9141
9142 if (code->expr2
9143 && !gfc_check_vardef_context (code->expr2, false, false, false,
9144 _("STAT variable")))
9145 return;
9146
9147 /* Check ERRMSG. */
9148 if (code->expr3
9149 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
9150 || code->expr3->expr_type != EXPR_VARIABLE))
9151 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
9152 &code->expr3->where);
9153
9154 if (code->expr3
9155 && !gfc_check_vardef_context (code->expr3, false, false, false,
9156 _("ERRMSG variable")))
9157 return;
9158
9159 /* Check for LOCK the ACQUIRED_LOCK. */
9160 if (code->op != EXEC_EVENT_WAIT && code->expr4
9161 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
9162 || code->expr4->expr_type != EXPR_VARIABLE))
9163 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
9164 "variable", &code->expr4->where);
9165
9166 if (code->op != EXEC_EVENT_WAIT && code->expr4
9167 && !gfc_check_vardef_context (code->expr4, false, false, false,
9168 _("ACQUIRED_LOCK variable")))
9169 return;
9170
9171 /* Check for EVENT WAIT the UNTIL_COUNT. */
9172 if (code->op == EXEC_EVENT_WAIT && code->expr4
9173 && (code->expr4->ts.type != BT_INTEGER || code->expr4->rank != 0))
9174 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
9175 "expression", &code->expr4->where);
9176 }
9177
9178
9179 static void
9180 resolve_critical (gfc_code *code)
9181 {
9182 gfc_symtree *symtree;
9183 gfc_symbol *lock_type;
9184 char name[GFC_MAX_SYMBOL_LEN];
9185 static int serial = 0;
9186
9187 if (flag_coarray != GFC_FCOARRAY_LIB)
9188 return;
9189
9190 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
9191 GFC_PREFIX ("lock_type"));
9192 if (symtree)
9193 lock_type = symtree->n.sym;
9194 else
9195 {
9196 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
9197 false) != 0)
9198 gcc_unreachable ();
9199 lock_type = symtree->n.sym;
9200 lock_type->attr.flavor = FL_DERIVED;
9201 lock_type->attr.zero_comp = 1;
9202 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
9203 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
9204 }
9205
9206 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
9207 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
9208 gcc_unreachable ();
9209
9210 code->resolved_sym = symtree->n.sym;
9211 symtree->n.sym->attr.flavor = FL_VARIABLE;
9212 symtree->n.sym->attr.referenced = 1;
9213 symtree->n.sym->attr.artificial = 1;
9214 symtree->n.sym->attr.codimension = 1;
9215 symtree->n.sym->ts.type = BT_DERIVED;
9216 symtree->n.sym->ts.u.derived = lock_type;
9217 symtree->n.sym->as = gfc_get_array_spec ();
9218 symtree->n.sym->as->corank = 1;
9219 symtree->n.sym->as->type = AS_EXPLICIT;
9220 symtree->n.sym->as->cotype = AS_EXPLICIT;
9221 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
9222 NULL, 1);
9223 gfc_commit_symbols();
9224 }
9225
9226
9227 static void
9228 resolve_sync (gfc_code *code)
9229 {
9230 /* Check imageset. The * case matches expr1 == NULL. */
9231 if (code->expr1)
9232 {
9233 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
9234 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
9235 "INTEGER expression", &code->expr1->where);
9236 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
9237 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
9238 gfc_error ("Imageset argument at %L must between 1 and num_images()",
9239 &code->expr1->where);
9240 else if (code->expr1->expr_type == EXPR_ARRAY
9241 && gfc_simplify_expr (code->expr1, 0))
9242 {
9243 gfc_constructor *cons;
9244 cons = gfc_constructor_first (code->expr1->value.constructor);
9245 for (; cons; cons = gfc_constructor_next (cons))
9246 if (cons->expr->expr_type == EXPR_CONSTANT
9247 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
9248 gfc_error ("Imageset argument at %L must between 1 and "
9249 "num_images()", &cons->expr->where);
9250 }
9251 }
9252
9253 /* Check STAT. */
9254 if (code->expr2
9255 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
9256 || code->expr2->expr_type != EXPR_VARIABLE))
9257 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
9258 &code->expr2->where);
9259
9260 /* Check ERRMSG. */
9261 if (code->expr3
9262 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
9263 || code->expr3->expr_type != EXPR_VARIABLE))
9264 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
9265 &code->expr3->where);
9266 }
9267
9268
9269 /* Given a branch to a label, see if the branch is conforming.
9270 The code node describes where the branch is located. */
9271
9272 static void
9273 resolve_branch (gfc_st_label *label, gfc_code *code)
9274 {
9275 code_stack *stack;
9276
9277 if (label == NULL)
9278 return;
9279
9280 /* Step one: is this a valid branching target? */
9281
9282 if (label->defined == ST_LABEL_UNKNOWN)
9283 {
9284 gfc_error ("Label %d referenced at %L is never defined", label->value,
9285 &code->loc);
9286 return;
9287 }
9288
9289 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
9290 {
9291 gfc_error ("Statement at %L is not a valid branch target statement "
9292 "for the branch statement at %L", &label->where, &code->loc);
9293 return;
9294 }
9295
9296 /* Step two: make sure this branch is not a branch to itself ;-) */
9297
9298 if (code->here == label)
9299 {
9300 gfc_warning (0,
9301 "Branch at %L may result in an infinite loop", &code->loc);
9302 return;
9303 }
9304
9305 /* Step three: See if the label is in the same block as the
9306 branching statement. The hard work has been done by setting up
9307 the bitmap reachable_labels. */
9308
9309 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
9310 {
9311 /* Check now whether there is a CRITICAL construct; if so, check
9312 whether the label is still visible outside of the CRITICAL block,
9313 which is invalid. */
9314 for (stack = cs_base; stack; stack = stack->prev)
9315 {
9316 if (stack->current->op == EXEC_CRITICAL
9317 && bitmap_bit_p (stack->reachable_labels, label->value))
9318 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
9319 "label at %L", &code->loc, &label->where);
9320 else if (stack->current->op == EXEC_DO_CONCURRENT
9321 && bitmap_bit_p (stack->reachable_labels, label->value))
9322 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
9323 "for label at %L", &code->loc, &label->where);
9324 }
9325
9326 return;
9327 }
9328
9329 /* Step four: If we haven't found the label in the bitmap, it may
9330 still be the label of the END of the enclosing block, in which
9331 case we find it by going up the code_stack. */
9332
9333 for (stack = cs_base; stack; stack = stack->prev)
9334 {
9335 if (stack->current->next && stack->current->next->here == label)
9336 break;
9337 if (stack->current->op == EXEC_CRITICAL)
9338 {
9339 /* Note: A label at END CRITICAL does not leave the CRITICAL
9340 construct as END CRITICAL is still part of it. */
9341 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
9342 " at %L", &code->loc, &label->where);
9343 return;
9344 }
9345 else if (stack->current->op == EXEC_DO_CONCURRENT)
9346 {
9347 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
9348 "label at %L", &code->loc, &label->where);
9349 return;
9350 }
9351 }
9352
9353 if (stack)
9354 {
9355 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
9356 return;
9357 }
9358
9359 /* The label is not in an enclosing block, so illegal. This was
9360 allowed in Fortran 66, so we allow it as extension. No
9361 further checks are necessary in this case. */
9362 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
9363 "as the GOTO statement at %L", &label->where,
9364 &code->loc);
9365 return;
9366 }
9367
9368
9369 /* Check whether EXPR1 has the same shape as EXPR2. */
9370
9371 static bool
9372 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
9373 {
9374 mpz_t shape[GFC_MAX_DIMENSIONS];
9375 mpz_t shape2[GFC_MAX_DIMENSIONS];
9376 bool result = false;
9377 int i;
9378
9379 /* Compare the rank. */
9380 if (expr1->rank != expr2->rank)
9381 return result;
9382
9383 /* Compare the size of each dimension. */
9384 for (i=0; i<expr1->rank; i++)
9385 {
9386 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
9387 goto ignore;
9388
9389 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
9390 goto ignore;
9391
9392 if (mpz_cmp (shape[i], shape2[i]))
9393 goto over;
9394 }
9395
9396 /* When either of the two expression is an assumed size array, we
9397 ignore the comparison of dimension sizes. */
9398 ignore:
9399 result = true;
9400
9401 over:
9402 gfc_clear_shape (shape, i);
9403 gfc_clear_shape (shape2, i);
9404 return result;
9405 }
9406
9407
9408 /* Check whether a WHERE assignment target or a WHERE mask expression
9409 has the same shape as the outmost WHERE mask expression. */
9410
9411 static void
9412 resolve_where (gfc_code *code, gfc_expr *mask)
9413 {
9414 gfc_code *cblock;
9415 gfc_code *cnext;
9416 gfc_expr *e = NULL;
9417
9418 cblock = code->block;
9419
9420 /* Store the first WHERE mask-expr of the WHERE statement or construct.
9421 In case of nested WHERE, only the outmost one is stored. */
9422 if (mask == NULL) /* outmost WHERE */
9423 e = cblock->expr1;
9424 else /* inner WHERE */
9425 e = mask;
9426
9427 while (cblock)
9428 {
9429 if (cblock->expr1)
9430 {
9431 /* Check if the mask-expr has a consistent shape with the
9432 outmost WHERE mask-expr. */
9433 if (!resolve_where_shape (cblock->expr1, e))
9434 gfc_error ("WHERE mask at %L has inconsistent shape",
9435 &cblock->expr1->where);
9436 }
9437
9438 /* the assignment statement of a WHERE statement, or the first
9439 statement in where-body-construct of a WHERE construct */
9440 cnext = cblock->next;
9441 while (cnext)
9442 {
9443 switch (cnext->op)
9444 {
9445 /* WHERE assignment statement */
9446 case EXEC_ASSIGN:
9447
9448 /* Check shape consistent for WHERE assignment target. */
9449 if (e && !resolve_where_shape (cnext->expr1, e))
9450 gfc_error ("WHERE assignment target at %L has "
9451 "inconsistent shape", &cnext->expr1->where);
9452 break;
9453
9454
9455 case EXEC_ASSIGN_CALL:
9456 resolve_call (cnext);
9457 if (!cnext->resolved_sym->attr.elemental)
9458 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
9459 &cnext->ext.actual->expr->where);
9460 break;
9461
9462 /* WHERE or WHERE construct is part of a where-body-construct */
9463 case EXEC_WHERE:
9464 resolve_where (cnext, e);
9465 break;
9466
9467 default:
9468 gfc_error ("Unsupported statement inside WHERE at %L",
9469 &cnext->loc);
9470 }
9471 /* the next statement within the same where-body-construct */
9472 cnext = cnext->next;
9473 }
9474 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
9475 cblock = cblock->block;
9476 }
9477 }
9478
9479
9480 /* Resolve assignment in FORALL construct.
9481 NVAR is the number of FORALL index variables, and VAR_EXPR records the
9482 FORALL index variables. */
9483
9484 static void
9485 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
9486 {
9487 int n;
9488
9489 for (n = 0; n < nvar; n++)
9490 {
9491 gfc_symbol *forall_index;
9492
9493 forall_index = var_expr[n]->symtree->n.sym;
9494
9495 /* Check whether the assignment target is one of the FORALL index
9496 variable. */
9497 if ((code->expr1->expr_type == EXPR_VARIABLE)
9498 && (code->expr1->symtree->n.sym == forall_index))
9499 gfc_error ("Assignment to a FORALL index variable at %L",
9500 &code->expr1->where);
9501 else
9502 {
9503 /* If one of the FORALL index variables doesn't appear in the
9504 assignment variable, then there could be a many-to-one
9505 assignment. Emit a warning rather than an error because the
9506 mask could be resolving this problem. */
9507 if (!find_forall_index (code->expr1, forall_index, 0))
9508 gfc_warning (0, "The FORALL with index %qs is not used on the "
9509 "left side of the assignment at %L and so might "
9510 "cause multiple assignment to this object",
9511 var_expr[n]->symtree->name, &code->expr1->where);
9512 }
9513 }
9514 }
9515
9516
9517 /* Resolve WHERE statement in FORALL construct. */
9518
9519 static void
9520 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
9521 gfc_expr **var_expr)
9522 {
9523 gfc_code *cblock;
9524 gfc_code *cnext;
9525
9526 cblock = code->block;
9527 while (cblock)
9528 {
9529 /* the assignment statement of a WHERE statement, or the first
9530 statement in where-body-construct of a WHERE construct */
9531 cnext = cblock->next;
9532 while (cnext)
9533 {
9534 switch (cnext->op)
9535 {
9536 /* WHERE assignment statement */
9537 case EXEC_ASSIGN:
9538 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
9539 break;
9540
9541 /* WHERE operator assignment statement */
9542 case EXEC_ASSIGN_CALL:
9543 resolve_call (cnext);
9544 if (!cnext->resolved_sym->attr.elemental)
9545 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
9546 &cnext->ext.actual->expr->where);
9547 break;
9548
9549 /* WHERE or WHERE construct is part of a where-body-construct */
9550 case EXEC_WHERE:
9551 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
9552 break;
9553
9554 default:
9555 gfc_error ("Unsupported statement inside WHERE at %L",
9556 &cnext->loc);
9557 }
9558 /* the next statement within the same where-body-construct */
9559 cnext = cnext->next;
9560 }
9561 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
9562 cblock = cblock->block;
9563 }
9564 }
9565
9566
9567 /* Traverse the FORALL body to check whether the following errors exist:
9568 1. For assignment, check if a many-to-one assignment happens.
9569 2. For WHERE statement, check the WHERE body to see if there is any
9570 many-to-one assignment. */
9571
9572 static void
9573 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
9574 {
9575 gfc_code *c;
9576
9577 c = code->block->next;
9578 while (c)
9579 {
9580 switch (c->op)
9581 {
9582 case EXEC_ASSIGN:
9583 case EXEC_POINTER_ASSIGN:
9584 gfc_resolve_assign_in_forall (c, nvar, var_expr);
9585 break;
9586
9587 case EXEC_ASSIGN_CALL:
9588 resolve_call (c);
9589 break;
9590
9591 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
9592 there is no need to handle it here. */
9593 case EXEC_FORALL:
9594 break;
9595 case EXEC_WHERE:
9596 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
9597 break;
9598 default:
9599 break;
9600 }
9601 /* The next statement in the FORALL body. */
9602 c = c->next;
9603 }
9604 }
9605
9606
9607 /* Counts the number of iterators needed inside a forall construct, including
9608 nested forall constructs. This is used to allocate the needed memory
9609 in gfc_resolve_forall. */
9610
9611 static int
9612 gfc_count_forall_iterators (gfc_code *code)
9613 {
9614 int max_iters, sub_iters, current_iters;
9615 gfc_forall_iterator *fa;
9616
9617 gcc_assert(code->op == EXEC_FORALL);
9618 max_iters = 0;
9619 current_iters = 0;
9620
9621 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
9622 current_iters ++;
9623
9624 code = code->block->next;
9625
9626 while (code)
9627 {
9628 if (code->op == EXEC_FORALL)
9629 {
9630 sub_iters = gfc_count_forall_iterators (code);
9631 if (sub_iters > max_iters)
9632 max_iters = sub_iters;
9633 }
9634 code = code->next;
9635 }
9636
9637 return current_iters + max_iters;
9638 }
9639
9640
9641 /* Given a FORALL construct, first resolve the FORALL iterator, then call
9642 gfc_resolve_forall_body to resolve the FORALL body. */
9643
9644 static void
9645 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
9646 {
9647 static gfc_expr **var_expr;
9648 static int total_var = 0;
9649 static int nvar = 0;
9650 int old_nvar, tmp;
9651 gfc_forall_iterator *fa;
9652 int i;
9653
9654 old_nvar = nvar;
9655
9656 /* Start to resolve a FORALL construct */
9657 if (forall_save == 0)
9658 {
9659 /* Count the total number of FORALL index in the nested FORALL
9660 construct in order to allocate the VAR_EXPR with proper size. */
9661 total_var = gfc_count_forall_iterators (code);
9662
9663 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
9664 var_expr = XCNEWVEC (gfc_expr *, total_var);
9665 }
9666
9667 /* The information about FORALL iterator, including FORALL index start, end
9668 and stride. The FORALL index can not appear in start, end or stride. */
9669 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
9670 {
9671 /* Check if any outer FORALL index name is the same as the current
9672 one. */
9673 for (i = 0; i < nvar; i++)
9674 {
9675 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
9676 {
9677 gfc_error ("An outer FORALL construct already has an index "
9678 "with this name %L", &fa->var->where);
9679 }
9680 }
9681
9682 /* Record the current FORALL index. */
9683 var_expr[nvar] = gfc_copy_expr (fa->var);
9684
9685 nvar++;
9686
9687 /* No memory leak. */
9688 gcc_assert (nvar <= total_var);
9689 }
9690
9691 /* Resolve the FORALL body. */
9692 gfc_resolve_forall_body (code, nvar, var_expr);
9693
9694 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
9695 gfc_resolve_blocks (code->block, ns);
9696
9697 tmp = nvar;
9698 nvar = old_nvar;
9699 /* Free only the VAR_EXPRs allocated in this frame. */
9700 for (i = nvar; i < tmp; i++)
9701 gfc_free_expr (var_expr[i]);
9702
9703 if (nvar == 0)
9704 {
9705 /* We are in the outermost FORALL construct. */
9706 gcc_assert (forall_save == 0);
9707
9708 /* VAR_EXPR is not needed any more. */
9709 free (var_expr);
9710 total_var = 0;
9711 }
9712 }
9713
9714
9715 /* Resolve a BLOCK construct statement. */
9716
9717 static void
9718 resolve_block_construct (gfc_code* code)
9719 {
9720 /* Resolve the BLOCK's namespace. */
9721 gfc_resolve (code->ext.block.ns);
9722
9723 /* For an ASSOCIATE block, the associations (and their targets) are already
9724 resolved during resolve_symbol. */
9725 }
9726
9727
9728 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
9729 DO code nodes. */
9730
9731 void
9732 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
9733 {
9734 bool t;
9735
9736 for (; b; b = b->block)
9737 {
9738 t = gfc_resolve_expr (b->expr1);
9739 if (!gfc_resolve_expr (b->expr2))
9740 t = false;
9741
9742 switch (b->op)
9743 {
9744 case EXEC_IF:
9745 if (t && b->expr1 != NULL
9746 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
9747 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9748 &b->expr1->where);
9749 break;
9750
9751 case EXEC_WHERE:
9752 if (t
9753 && b->expr1 != NULL
9754 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
9755 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
9756 &b->expr1->where);
9757 break;
9758
9759 case EXEC_GOTO:
9760 resolve_branch (b->label1, b);
9761 break;
9762
9763 case EXEC_BLOCK:
9764 resolve_block_construct (b);
9765 break;
9766
9767 case EXEC_SELECT:
9768 case EXEC_SELECT_TYPE:
9769 case EXEC_FORALL:
9770 case EXEC_DO:
9771 case EXEC_DO_WHILE:
9772 case EXEC_DO_CONCURRENT:
9773 case EXEC_CRITICAL:
9774 case EXEC_READ:
9775 case EXEC_WRITE:
9776 case EXEC_IOLENGTH:
9777 case EXEC_WAIT:
9778 break;
9779
9780 case EXEC_OMP_ATOMIC:
9781 case EXEC_OACC_ATOMIC:
9782 {
9783 gfc_omp_atomic_op aop
9784 = (gfc_omp_atomic_op) (b->ext.omp_atomic & GFC_OMP_ATOMIC_MASK);
9785
9786 /* Verify this before calling gfc_resolve_code, which might
9787 change it. */
9788 gcc_assert (b->next && b->next->op == EXEC_ASSIGN);
9789 gcc_assert (((aop != GFC_OMP_ATOMIC_CAPTURE)
9790 && b->next->next == NULL)
9791 || ((aop == GFC_OMP_ATOMIC_CAPTURE)
9792 && b->next->next != NULL
9793 && b->next->next->op == EXEC_ASSIGN
9794 && b->next->next->next == NULL));
9795 }
9796 break;
9797
9798 case EXEC_OACC_PARALLEL_LOOP:
9799 case EXEC_OACC_PARALLEL:
9800 case EXEC_OACC_KERNELS_LOOP:
9801 case EXEC_OACC_KERNELS:
9802 case EXEC_OACC_DATA:
9803 case EXEC_OACC_HOST_DATA:
9804 case EXEC_OACC_LOOP:
9805 case EXEC_OACC_UPDATE:
9806 case EXEC_OACC_WAIT:
9807 case EXEC_OACC_CACHE:
9808 case EXEC_OACC_ENTER_DATA:
9809 case EXEC_OACC_EXIT_DATA:
9810 case EXEC_OACC_ROUTINE:
9811 case EXEC_OMP_CRITICAL:
9812 case EXEC_OMP_DISTRIBUTE:
9813 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
9814 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
9815 case EXEC_OMP_DISTRIBUTE_SIMD:
9816 case EXEC_OMP_DO:
9817 case EXEC_OMP_DO_SIMD:
9818 case EXEC_OMP_MASTER:
9819 case EXEC_OMP_ORDERED:
9820 case EXEC_OMP_PARALLEL:
9821 case EXEC_OMP_PARALLEL_DO:
9822 case EXEC_OMP_PARALLEL_DO_SIMD:
9823 case EXEC_OMP_PARALLEL_SECTIONS:
9824 case EXEC_OMP_PARALLEL_WORKSHARE:
9825 case EXEC_OMP_SECTIONS:
9826 case EXEC_OMP_SIMD:
9827 case EXEC_OMP_SINGLE:
9828 case EXEC_OMP_TARGET:
9829 case EXEC_OMP_TARGET_DATA:
9830 case EXEC_OMP_TARGET_ENTER_DATA:
9831 case EXEC_OMP_TARGET_EXIT_DATA:
9832 case EXEC_OMP_TARGET_PARALLEL:
9833 case EXEC_OMP_TARGET_PARALLEL_DO:
9834 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
9835 case EXEC_OMP_TARGET_SIMD:
9836 case EXEC_OMP_TARGET_TEAMS:
9837 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
9838 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
9839 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9840 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
9841 case EXEC_OMP_TARGET_UPDATE:
9842 case EXEC_OMP_TASK:
9843 case EXEC_OMP_TASKGROUP:
9844 case EXEC_OMP_TASKLOOP:
9845 case EXEC_OMP_TASKLOOP_SIMD:
9846 case EXEC_OMP_TASKWAIT:
9847 case EXEC_OMP_TASKYIELD:
9848 case EXEC_OMP_TEAMS:
9849 case EXEC_OMP_TEAMS_DISTRIBUTE:
9850 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
9851 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9852 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
9853 case EXEC_OMP_WORKSHARE:
9854 break;
9855
9856 default:
9857 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
9858 }
9859
9860 gfc_resolve_code (b->next, ns);
9861 }
9862 }
9863
9864
9865 /* Does everything to resolve an ordinary assignment. Returns true
9866 if this is an interface assignment. */
9867 static bool
9868 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
9869 {
9870 bool rval = false;
9871 gfc_expr *lhs;
9872 gfc_expr *rhs;
9873 int llen = 0;
9874 int rlen = 0;
9875 int n;
9876 gfc_ref *ref;
9877 symbol_attribute attr;
9878
9879 if (gfc_extend_assign (code, ns))
9880 {
9881 gfc_expr** rhsptr;
9882
9883 if (code->op == EXEC_ASSIGN_CALL)
9884 {
9885 lhs = code->ext.actual->expr;
9886 rhsptr = &code->ext.actual->next->expr;
9887 }
9888 else
9889 {
9890 gfc_actual_arglist* args;
9891 gfc_typebound_proc* tbp;
9892
9893 gcc_assert (code->op == EXEC_COMPCALL);
9894
9895 args = code->expr1->value.compcall.actual;
9896 lhs = args->expr;
9897 rhsptr = &args->next->expr;
9898
9899 tbp = code->expr1->value.compcall.tbp;
9900 gcc_assert (!tbp->is_generic);
9901 }
9902
9903 /* Make a temporary rhs when there is a default initializer
9904 and rhs is the same symbol as the lhs. */
9905 if ((*rhsptr)->expr_type == EXPR_VARIABLE
9906 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
9907 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
9908 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
9909 *rhsptr = gfc_get_parentheses (*rhsptr);
9910
9911 return true;
9912 }
9913
9914 lhs = code->expr1;
9915 rhs = code->expr2;
9916
9917 if (rhs->is_boz
9918 && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
9919 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
9920 &code->loc))
9921 return false;
9922
9923 /* Handle the case of a BOZ literal on the RHS. */
9924 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
9925 {
9926 int rc;
9927 if (warn_surprising)
9928 gfc_warning (OPT_Wsurprising,
9929 "BOZ literal at %L is bitwise transferred "
9930 "non-integer symbol %qs", &code->loc,
9931 lhs->symtree->n.sym->name);
9932
9933 if (!gfc_convert_boz (rhs, &lhs->ts))
9934 return false;
9935 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
9936 {
9937 if (rc == ARITH_UNDERFLOW)
9938 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
9939 ". This check can be disabled with the option "
9940 "%<-fno-range-check%>", &rhs->where);
9941 else if (rc == ARITH_OVERFLOW)
9942 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
9943 ". This check can be disabled with the option "
9944 "%<-fno-range-check%>", &rhs->where);
9945 else if (rc == ARITH_NAN)
9946 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
9947 ". This check can be disabled with the option "
9948 "%<-fno-range-check%>", &rhs->where);
9949 return false;
9950 }
9951 }
9952
9953 if (lhs->ts.type == BT_CHARACTER
9954 && warn_character_truncation)
9955 {
9956 if (lhs->ts.u.cl != NULL
9957 && lhs->ts.u.cl->length != NULL
9958 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9959 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
9960
9961 if (rhs->expr_type == EXPR_CONSTANT)
9962 rlen = rhs->value.character.length;
9963
9964 else if (rhs->ts.u.cl != NULL
9965 && rhs->ts.u.cl->length != NULL
9966 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9967 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
9968
9969 if (rlen && llen && rlen > llen)
9970 gfc_warning_now (OPT_Wcharacter_truncation,
9971 "CHARACTER expression will be truncated "
9972 "in assignment (%d/%d) at %L",
9973 llen, rlen, &code->loc);
9974 }
9975
9976 /* Ensure that a vector index expression for the lvalue is evaluated
9977 to a temporary if the lvalue symbol is referenced in it. */
9978 if (lhs->rank)
9979 {
9980 for (ref = lhs->ref; ref; ref= ref->next)
9981 if (ref->type == REF_ARRAY)
9982 {
9983 for (n = 0; n < ref->u.ar.dimen; n++)
9984 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
9985 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9986 ref->u.ar.start[n]))
9987 ref->u.ar.start[n]
9988 = gfc_get_parentheses (ref->u.ar.start[n]);
9989 }
9990 }
9991
9992 if (gfc_pure (NULL))
9993 {
9994 if (lhs->ts.type == BT_DERIVED
9995 && lhs->expr_type == EXPR_VARIABLE
9996 && lhs->ts.u.derived->attr.pointer_comp
9997 && rhs->expr_type == EXPR_VARIABLE
9998 && (gfc_impure_variable (rhs->symtree->n.sym)
9999 || gfc_is_coindexed (rhs)))
10000 {
10001 /* F2008, C1283. */
10002 if (gfc_is_coindexed (rhs))
10003 gfc_error ("Coindexed expression at %L is assigned to "
10004 "a derived type variable with a POINTER "
10005 "component in a PURE procedure",
10006 &rhs->where);
10007 else
10008 gfc_error ("The impure variable at %L is assigned to "
10009 "a derived type variable with a POINTER "
10010 "component in a PURE procedure (12.6)",
10011 &rhs->where);
10012 return rval;
10013 }
10014
10015 /* Fortran 2008, C1283. */
10016 if (gfc_is_coindexed (lhs))
10017 {
10018 gfc_error ("Assignment to coindexed variable at %L in a PURE "
10019 "procedure", &rhs->where);
10020 return rval;
10021 }
10022 }
10023
10024 if (gfc_implicit_pure (NULL))
10025 {
10026 if (lhs->expr_type == EXPR_VARIABLE
10027 && lhs->symtree->n.sym != gfc_current_ns->proc_name
10028 && lhs->symtree->n.sym->ns != gfc_current_ns)
10029 gfc_unset_implicit_pure (NULL);
10030
10031 if (lhs->ts.type == BT_DERIVED
10032 && lhs->expr_type == EXPR_VARIABLE
10033 && lhs->ts.u.derived->attr.pointer_comp
10034 && rhs->expr_type == EXPR_VARIABLE
10035 && (gfc_impure_variable (rhs->symtree->n.sym)
10036 || gfc_is_coindexed (rhs)))
10037 gfc_unset_implicit_pure (NULL);
10038
10039 /* Fortran 2008, C1283. */
10040 if (gfc_is_coindexed (lhs))
10041 gfc_unset_implicit_pure (NULL);
10042 }
10043
10044 /* F2008, 7.2.1.2. */
10045 attr = gfc_expr_attr (lhs);
10046 if (lhs->ts.type == BT_CLASS && attr.allocatable)
10047 {
10048 if (attr.codimension)
10049 {
10050 gfc_error ("Assignment to polymorphic coarray at %L is not "
10051 "permitted", &lhs->where);
10052 return false;
10053 }
10054 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
10055 "polymorphic variable at %L", &lhs->where))
10056 return false;
10057 if (!flag_realloc_lhs)
10058 {
10059 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
10060 "requires %<-frealloc-lhs%>", &lhs->where);
10061 return false;
10062 }
10063 }
10064 else if (lhs->ts.type == BT_CLASS)
10065 {
10066 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
10067 "assignment at %L - check that there is a matching specific "
10068 "subroutine for '=' operator", &lhs->where);
10069 return false;
10070 }
10071
10072 bool lhs_coindexed = gfc_is_coindexed (lhs);
10073
10074 /* F2008, Section 7.2.1.2. */
10075 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
10076 {
10077 gfc_error ("Coindexed variable must not have an allocatable ultimate "
10078 "component in assignment at %L", &lhs->where);
10079 return false;
10080 }
10081
10082 /* Assign the 'data' of a class object to a derived type. */
10083 if (lhs->ts.type == BT_DERIVED
10084 && rhs->ts.type == BT_CLASS)
10085 gfc_add_data_component (rhs);
10086
10087 bool caf_convert_to_send = flag_coarray == GFC_FCOARRAY_LIB
10088 && (lhs_coindexed
10089 || (code->expr2->expr_type == EXPR_FUNCTION
10090 && code->expr2->value.function.isym
10091 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
10092 && (code->expr1->rank == 0 || code->expr2->rank != 0)
10093 && !gfc_expr_attr (rhs).allocatable
10094 && !gfc_has_vector_subscript (rhs)));
10095
10096 gfc_check_assign (lhs, rhs, 1, !caf_convert_to_send);
10097
10098 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
10099 Additionally, insert this code when the RHS is a CAF as we then use the
10100 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
10101 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
10102 noncoindexed array and the RHS is a coindexed scalar, use the normal code
10103 path. */
10104 if (caf_convert_to_send)
10105 {
10106 if (code->expr2->expr_type == EXPR_FUNCTION
10107 && code->expr2->value.function.isym
10108 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
10109 remove_caf_get_intrinsic (code->expr2);
10110 code->op = EXEC_CALL;
10111 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
10112 code->resolved_sym = code->symtree->n.sym;
10113 code->resolved_sym->attr.flavor = FL_PROCEDURE;
10114 code->resolved_sym->attr.intrinsic = 1;
10115 code->resolved_sym->attr.subroutine = 1;
10116 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
10117 gfc_commit_symbol (code->resolved_sym);
10118 code->ext.actual = gfc_get_actual_arglist ();
10119 code->ext.actual->expr = lhs;
10120 code->ext.actual->next = gfc_get_actual_arglist ();
10121 code->ext.actual->next->expr = rhs;
10122 code->expr1 = NULL;
10123 code->expr2 = NULL;
10124 }
10125
10126 return false;
10127 }
10128
10129
10130 /* Add a component reference onto an expression. */
10131
10132 static void
10133 add_comp_ref (gfc_expr *e, gfc_component *c)
10134 {
10135 gfc_ref **ref;
10136 ref = &(e->ref);
10137 while (*ref)
10138 ref = &((*ref)->next);
10139 *ref = gfc_get_ref ();
10140 (*ref)->type = REF_COMPONENT;
10141 (*ref)->u.c.sym = e->ts.u.derived;
10142 (*ref)->u.c.component = c;
10143 e->ts = c->ts;
10144
10145 /* Add a full array ref, as necessary. */
10146 if (c->as)
10147 {
10148 gfc_add_full_array_ref (e, c->as);
10149 e->rank = c->as->rank;
10150 }
10151 }
10152
10153
10154 /* Build an assignment. Keep the argument 'op' for future use, so that
10155 pointer assignments can be made. */
10156
10157 static gfc_code *
10158 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
10159 gfc_component *comp1, gfc_component *comp2, locus loc)
10160 {
10161 gfc_code *this_code;
10162
10163 this_code = gfc_get_code (op);
10164 this_code->next = NULL;
10165 this_code->expr1 = gfc_copy_expr (expr1);
10166 this_code->expr2 = gfc_copy_expr (expr2);
10167 this_code->loc = loc;
10168 if (comp1 && comp2)
10169 {
10170 add_comp_ref (this_code->expr1, comp1);
10171 add_comp_ref (this_code->expr2, comp2);
10172 }
10173
10174 return this_code;
10175 }
10176
10177
10178 /* Makes a temporary variable expression based on the characteristics of
10179 a given variable expression. */
10180
10181 static gfc_expr*
10182 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
10183 {
10184 static int serial = 0;
10185 char name[GFC_MAX_SYMBOL_LEN];
10186 gfc_symtree *tmp;
10187 gfc_array_spec *as;
10188 gfc_array_ref *aref;
10189 gfc_ref *ref;
10190
10191 sprintf (name, GFC_PREFIX("DA%d"), serial++);
10192 gfc_get_sym_tree (name, ns, &tmp, false);
10193 gfc_add_type (tmp->n.sym, &e->ts, NULL);
10194
10195 as = NULL;
10196 ref = NULL;
10197 aref = NULL;
10198
10199 /* Obtain the arrayspec for the temporary. */
10200 if (e->rank && e->expr_type != EXPR_ARRAY
10201 && e->expr_type != EXPR_FUNCTION
10202 && e->expr_type != EXPR_OP)
10203 {
10204 aref = gfc_find_array_ref (e);
10205 if (e->expr_type == EXPR_VARIABLE
10206 && e->symtree->n.sym->as == aref->as)
10207 as = aref->as;
10208 else
10209 {
10210 for (ref = e->ref; ref; ref = ref->next)
10211 if (ref->type == REF_COMPONENT
10212 && ref->u.c.component->as == aref->as)
10213 {
10214 as = aref->as;
10215 break;
10216 }
10217 }
10218 }
10219
10220 /* Add the attributes and the arrayspec to the temporary. */
10221 tmp->n.sym->attr = gfc_expr_attr (e);
10222 tmp->n.sym->attr.function = 0;
10223 tmp->n.sym->attr.result = 0;
10224 tmp->n.sym->attr.flavor = FL_VARIABLE;
10225
10226 if (as)
10227 {
10228 tmp->n.sym->as = gfc_copy_array_spec (as);
10229 if (!ref)
10230 ref = e->ref;
10231 if (as->type == AS_DEFERRED)
10232 tmp->n.sym->attr.allocatable = 1;
10233 }
10234 else if (e->rank && (e->expr_type == EXPR_ARRAY
10235 || e->expr_type == EXPR_FUNCTION
10236 || e->expr_type == EXPR_OP))
10237 {
10238 tmp->n.sym->as = gfc_get_array_spec ();
10239 tmp->n.sym->as->type = AS_DEFERRED;
10240 tmp->n.sym->as->rank = e->rank;
10241 tmp->n.sym->attr.allocatable = 1;
10242 tmp->n.sym->attr.dimension = 1;
10243 }
10244 else
10245 tmp->n.sym->attr.dimension = 0;
10246
10247 gfc_set_sym_referenced (tmp->n.sym);
10248 gfc_commit_symbol (tmp->n.sym);
10249 e = gfc_lval_expr_from_sym (tmp->n.sym);
10250
10251 /* Should the lhs be a section, use its array ref for the
10252 temporary expression. */
10253 if (aref && aref->type != AR_FULL)
10254 {
10255 gfc_free_ref_list (e->ref);
10256 e->ref = gfc_copy_ref (ref);
10257 }
10258 return e;
10259 }
10260
10261
10262 /* Add one line of code to the code chain, making sure that 'head' and
10263 'tail' are appropriately updated. */
10264
10265 static void
10266 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
10267 {
10268 gcc_assert (this_code);
10269 if (*head == NULL)
10270 *head = *tail = *this_code;
10271 else
10272 *tail = gfc_append_code (*tail, *this_code);
10273 *this_code = NULL;
10274 }
10275
10276
10277 /* Counts the potential number of part array references that would
10278 result from resolution of typebound defined assignments. */
10279
10280 static int
10281 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
10282 {
10283 gfc_component *c;
10284 int c_depth = 0, t_depth;
10285
10286 for (c= derived->components; c; c = c->next)
10287 {
10288 if ((!gfc_bt_struct (c->ts.type)
10289 || c->attr.pointer
10290 || c->attr.allocatable
10291 || c->attr.proc_pointer_comp
10292 || c->attr.class_pointer
10293 || c->attr.proc_pointer)
10294 && !c->attr.defined_assign_comp)
10295 continue;
10296
10297 if (c->as && c_depth == 0)
10298 c_depth = 1;
10299
10300 if (c->ts.u.derived->attr.defined_assign_comp)
10301 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
10302 c->as ? 1 : 0);
10303 else
10304 t_depth = 0;
10305
10306 c_depth = t_depth > c_depth ? t_depth : c_depth;
10307 }
10308 return depth + c_depth;
10309 }
10310
10311
10312 /* Implement 7.2.1.3 of the F08 standard:
10313 "An intrinsic assignment where the variable is of derived type is
10314 performed as if each component of the variable were assigned from the
10315 corresponding component of expr using pointer assignment (7.2.2) for
10316 each pointer component, defined assignment for each nonpointer
10317 nonallocatable component of a type that has a type-bound defined
10318 assignment consistent with the component, intrinsic assignment for
10319 each other nonpointer nonallocatable component, ..."
10320
10321 The pointer assignments are taken care of by the intrinsic
10322 assignment of the structure itself. This function recursively adds
10323 defined assignments where required. The recursion is accomplished
10324 by calling gfc_resolve_code.
10325
10326 When the lhs in a defined assignment has intent INOUT, we need a
10327 temporary for the lhs. In pseudo-code:
10328
10329 ! Only call function lhs once.
10330 if (lhs is not a constant or an variable)
10331 temp_x = expr2
10332 expr2 => temp_x
10333 ! Do the intrinsic assignment
10334 expr1 = expr2
10335 ! Now do the defined assignments
10336 do over components with typebound defined assignment [%cmp]
10337 #if one component's assignment procedure is INOUT
10338 t1 = expr1
10339 #if expr2 non-variable
10340 temp_x = expr2
10341 expr2 => temp_x
10342 # endif
10343 expr1 = expr2
10344 # for each cmp
10345 t1%cmp {defined=} expr2%cmp
10346 expr1%cmp = t1%cmp
10347 #else
10348 expr1 = expr2
10349
10350 # for each cmp
10351 expr1%cmp {defined=} expr2%cmp
10352 #endif
10353 */
10354
10355 /* The temporary assignments have to be put on top of the additional
10356 code to avoid the result being changed by the intrinsic assignment.
10357 */
10358 static int component_assignment_level = 0;
10359 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
10360
10361 static void
10362 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
10363 {
10364 gfc_component *comp1, *comp2;
10365 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
10366 gfc_expr *t1;
10367 int error_count, depth;
10368
10369 gfc_get_errors (NULL, &error_count);
10370
10371 /* Filter out continuing processing after an error. */
10372 if (error_count
10373 || (*code)->expr1->ts.type != BT_DERIVED
10374 || (*code)->expr2->ts.type != BT_DERIVED)
10375 return;
10376
10377 /* TODO: Handle more than one part array reference in assignments. */
10378 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
10379 (*code)->expr1->rank ? 1 : 0);
10380 if (depth > 1)
10381 {
10382 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
10383 "done because multiple part array references would "
10384 "occur in intermediate expressions.", &(*code)->loc);
10385 return;
10386 }
10387
10388 component_assignment_level++;
10389
10390 /* Create a temporary so that functions get called only once. */
10391 if ((*code)->expr2->expr_type != EXPR_VARIABLE
10392 && (*code)->expr2->expr_type != EXPR_CONSTANT)
10393 {
10394 gfc_expr *tmp_expr;
10395
10396 /* Assign the rhs to the temporary. */
10397 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
10398 this_code = build_assignment (EXEC_ASSIGN,
10399 tmp_expr, (*code)->expr2,
10400 NULL, NULL, (*code)->loc);
10401 /* Add the code and substitute the rhs expression. */
10402 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
10403 gfc_free_expr ((*code)->expr2);
10404 (*code)->expr2 = tmp_expr;
10405 }
10406
10407 /* Do the intrinsic assignment. This is not needed if the lhs is one
10408 of the temporaries generated here, since the intrinsic assignment
10409 to the final result already does this. */
10410 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
10411 {
10412 this_code = build_assignment (EXEC_ASSIGN,
10413 (*code)->expr1, (*code)->expr2,
10414 NULL, NULL, (*code)->loc);
10415 add_code_to_chain (&this_code, &head, &tail);
10416 }
10417
10418 comp1 = (*code)->expr1->ts.u.derived->components;
10419 comp2 = (*code)->expr2->ts.u.derived->components;
10420
10421 t1 = NULL;
10422 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
10423 {
10424 bool inout = false;
10425
10426 /* The intrinsic assignment does the right thing for pointers
10427 of all kinds and allocatable components. */
10428 if (!gfc_bt_struct (comp1->ts.type)
10429 || comp1->attr.pointer
10430 || comp1->attr.allocatable
10431 || comp1->attr.proc_pointer_comp
10432 || comp1->attr.class_pointer
10433 || comp1->attr.proc_pointer)
10434 continue;
10435
10436 /* Make an assigment for this component. */
10437 this_code = build_assignment (EXEC_ASSIGN,
10438 (*code)->expr1, (*code)->expr2,
10439 comp1, comp2, (*code)->loc);
10440
10441 /* Convert the assignment if there is a defined assignment for
10442 this type. Otherwise, using the call from gfc_resolve_code,
10443 recurse into its components. */
10444 gfc_resolve_code (this_code, ns);
10445
10446 if (this_code->op == EXEC_ASSIGN_CALL)
10447 {
10448 gfc_formal_arglist *dummy_args;
10449 gfc_symbol *rsym;
10450 /* Check that there is a typebound defined assignment. If not,
10451 then this must be a module defined assignment. We cannot
10452 use the defined_assign_comp attribute here because it must
10453 be this derived type that has the defined assignment and not
10454 a parent type. */
10455 if (!(comp1->ts.u.derived->f2k_derived
10456 && comp1->ts.u.derived->f2k_derived
10457 ->tb_op[INTRINSIC_ASSIGN]))
10458 {
10459 gfc_free_statements (this_code);
10460 this_code = NULL;
10461 continue;
10462 }
10463
10464 /* If the first argument of the subroutine has intent INOUT
10465 a temporary must be generated and used instead. */
10466 rsym = this_code->resolved_sym;
10467 dummy_args = gfc_sym_get_dummy_args (rsym);
10468 if (dummy_args
10469 && dummy_args->sym->attr.intent == INTENT_INOUT)
10470 {
10471 gfc_code *temp_code;
10472 inout = true;
10473
10474 /* Build the temporary required for the assignment and put
10475 it at the head of the generated code. */
10476 if (!t1)
10477 {
10478 t1 = get_temp_from_expr ((*code)->expr1, ns);
10479 temp_code = build_assignment (EXEC_ASSIGN,
10480 t1, (*code)->expr1,
10481 NULL, NULL, (*code)->loc);
10482
10483 /* For allocatable LHS, check whether it is allocated. Note
10484 that allocatable components with defined assignment are
10485 not yet support. See PR 57696. */
10486 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
10487 {
10488 gfc_code *block;
10489 gfc_expr *e =
10490 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
10491 block = gfc_get_code (EXEC_IF);
10492 block->block = gfc_get_code (EXEC_IF);
10493 block->block->expr1
10494 = gfc_build_intrinsic_call (ns,
10495 GFC_ISYM_ALLOCATED, "allocated",
10496 (*code)->loc, 1, e);
10497 block->block->next = temp_code;
10498 temp_code = block;
10499 }
10500 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
10501 }
10502
10503 /* Replace the first actual arg with the component of the
10504 temporary. */
10505 gfc_free_expr (this_code->ext.actual->expr);
10506 this_code->ext.actual->expr = gfc_copy_expr (t1);
10507 add_comp_ref (this_code->ext.actual->expr, comp1);
10508
10509 /* If the LHS variable is allocatable and wasn't allocated and
10510 the temporary is allocatable, pointer assign the address of
10511 the freshly allocated LHS to the temporary. */
10512 if ((*code)->expr1->symtree->n.sym->attr.allocatable
10513 && gfc_expr_attr ((*code)->expr1).allocatable)
10514 {
10515 gfc_code *block;
10516 gfc_expr *cond;
10517
10518 cond = gfc_get_expr ();
10519 cond->ts.type = BT_LOGICAL;
10520 cond->ts.kind = gfc_default_logical_kind;
10521 cond->expr_type = EXPR_OP;
10522 cond->where = (*code)->loc;
10523 cond->value.op.op = INTRINSIC_NOT;
10524 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
10525 GFC_ISYM_ALLOCATED, "allocated",
10526 (*code)->loc, 1, gfc_copy_expr (t1));
10527 block = gfc_get_code (EXEC_IF);
10528 block->block = gfc_get_code (EXEC_IF);
10529 block->block->expr1 = cond;
10530 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
10531 t1, (*code)->expr1,
10532 NULL, NULL, (*code)->loc);
10533 add_code_to_chain (&block, &head, &tail);
10534 }
10535 }
10536 }
10537 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
10538 {
10539 /* Don't add intrinsic assignments since they are already
10540 effected by the intrinsic assignment of the structure. */
10541 gfc_free_statements (this_code);
10542 this_code = NULL;
10543 continue;
10544 }
10545
10546 add_code_to_chain (&this_code, &head, &tail);
10547
10548 if (t1 && inout)
10549 {
10550 /* Transfer the value to the final result. */
10551 this_code = build_assignment (EXEC_ASSIGN,
10552 (*code)->expr1, t1,
10553 comp1, comp2, (*code)->loc);
10554 add_code_to_chain (&this_code, &head, &tail);
10555 }
10556 }
10557
10558 /* Put the temporary assignments at the top of the generated code. */
10559 if (tmp_head && component_assignment_level == 1)
10560 {
10561 gfc_append_code (tmp_head, head);
10562 head = tmp_head;
10563 tmp_head = tmp_tail = NULL;
10564 }
10565
10566 // If we did a pointer assignment - thus, we need to ensure that the LHS is
10567 // not accidentally deallocated. Hence, nullify t1.
10568 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
10569 && gfc_expr_attr ((*code)->expr1).allocatable)
10570 {
10571 gfc_code *block;
10572 gfc_expr *cond;
10573 gfc_expr *e;
10574
10575 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
10576 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
10577 (*code)->loc, 2, gfc_copy_expr (t1), e);
10578 block = gfc_get_code (EXEC_IF);
10579 block->block = gfc_get_code (EXEC_IF);
10580 block->block->expr1 = cond;
10581 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
10582 t1, gfc_get_null_expr (&(*code)->loc),
10583 NULL, NULL, (*code)->loc);
10584 gfc_append_code (tail, block);
10585 tail = block;
10586 }
10587
10588 /* Now attach the remaining code chain to the input code. Step on
10589 to the end of the new code since resolution is complete. */
10590 gcc_assert ((*code)->op == EXEC_ASSIGN);
10591 tail->next = (*code)->next;
10592 /* Overwrite 'code' because this would place the intrinsic assignment
10593 before the temporary for the lhs is created. */
10594 gfc_free_expr ((*code)->expr1);
10595 gfc_free_expr ((*code)->expr2);
10596 **code = *head;
10597 if (head != tail)
10598 free (head);
10599 *code = tail;
10600
10601 component_assignment_level--;
10602 }
10603
10604
10605 /* F2008: Pointer function assignments are of the form:
10606 ptr_fcn (args) = expr
10607 This function breaks these assignments into two statements:
10608 temporary_pointer => ptr_fcn(args)
10609 temporary_pointer = expr */
10610
10611 static bool
10612 resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
10613 {
10614 gfc_expr *tmp_ptr_expr;
10615 gfc_code *this_code;
10616 gfc_component *comp;
10617 gfc_symbol *s;
10618
10619 if ((*code)->expr1->expr_type != EXPR_FUNCTION)
10620 return false;
10621
10622 /* Even if standard does not support this feature, continue to build
10623 the two statements to avoid upsetting frontend_passes.c. */
10624 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
10625 "%L", &(*code)->loc);
10626
10627 comp = gfc_get_proc_ptr_comp ((*code)->expr1);
10628
10629 if (comp)
10630 s = comp->ts.interface;
10631 else
10632 s = (*code)->expr1->symtree->n.sym;
10633
10634 if (s == NULL || !s->result->attr.pointer)
10635 {
10636 gfc_error ("The function result on the lhs of the assignment at "
10637 "%L must have the pointer attribute.",
10638 &(*code)->expr1->where);
10639 (*code)->op = EXEC_NOP;
10640 return false;
10641 }
10642
10643 tmp_ptr_expr = get_temp_from_expr ((*code)->expr2, ns);
10644
10645 /* get_temp_from_expression is set up for ordinary assignments. To that
10646 end, where array bounds are not known, arrays are made allocatable.
10647 Change the temporary to a pointer here. */
10648 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
10649 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
10650 tmp_ptr_expr->where = (*code)->loc;
10651
10652 this_code = build_assignment (EXEC_ASSIGN,
10653 tmp_ptr_expr, (*code)->expr2,
10654 NULL, NULL, (*code)->loc);
10655 this_code->next = (*code)->next;
10656 (*code)->next = this_code;
10657 (*code)->op = EXEC_POINTER_ASSIGN;
10658 (*code)->expr2 = (*code)->expr1;
10659 (*code)->expr1 = tmp_ptr_expr;
10660
10661 return true;
10662 }
10663
10664
10665 /* Deferred character length assignments from an operator expression
10666 require a temporary because the character length of the lhs can
10667 change in the course of the assignment. */
10668
10669 static bool
10670 deferred_op_assign (gfc_code **code, gfc_namespace *ns)
10671 {
10672 gfc_expr *tmp_expr;
10673 gfc_code *this_code;
10674
10675 if (!((*code)->expr1->ts.type == BT_CHARACTER
10676 && (*code)->expr1->ts.deferred && (*code)->expr1->rank
10677 && (*code)->expr2->expr_type == EXPR_OP))
10678 return false;
10679
10680 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
10681 return false;
10682
10683 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
10684 tmp_expr->where = (*code)->loc;
10685
10686 /* A new charlen is required to ensure that the variable string
10687 length is different to that of the original lhs. */
10688 tmp_expr->ts.u.cl = gfc_get_charlen();
10689 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
10690 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
10691 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
10692
10693 tmp_expr->symtree->n.sym->ts.deferred = 1;
10694
10695 this_code = build_assignment (EXEC_ASSIGN,
10696 (*code)->expr1,
10697 gfc_copy_expr (tmp_expr),
10698 NULL, NULL, (*code)->loc);
10699
10700 (*code)->expr1 = tmp_expr;
10701
10702 this_code->next = (*code)->next;
10703 (*code)->next = this_code;
10704
10705 return true;
10706 }
10707
10708
10709 /* Given a block of code, recursively resolve everything pointed to by this
10710 code block. */
10711
10712 void
10713 gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
10714 {
10715 int omp_workshare_save;
10716 int forall_save, do_concurrent_save;
10717 code_stack frame;
10718 bool t;
10719
10720 frame.prev = cs_base;
10721 frame.head = code;
10722 cs_base = &frame;
10723
10724 find_reachable_labels (code);
10725
10726 for (; code; code = code->next)
10727 {
10728 frame.current = code;
10729 forall_save = forall_flag;
10730 do_concurrent_save = gfc_do_concurrent_flag;
10731
10732 if (code->op == EXEC_FORALL)
10733 {
10734 forall_flag = 1;
10735 gfc_resolve_forall (code, ns, forall_save);
10736 forall_flag = 2;
10737 }
10738 else if (code->block)
10739 {
10740 omp_workshare_save = -1;
10741 switch (code->op)
10742 {
10743 case EXEC_OACC_PARALLEL_LOOP:
10744 case EXEC_OACC_PARALLEL:
10745 case EXEC_OACC_KERNELS_LOOP:
10746 case EXEC_OACC_KERNELS:
10747 case EXEC_OACC_DATA:
10748 case EXEC_OACC_HOST_DATA:
10749 case EXEC_OACC_LOOP:
10750 gfc_resolve_oacc_blocks (code, ns);
10751 break;
10752 case EXEC_OMP_PARALLEL_WORKSHARE:
10753 omp_workshare_save = omp_workshare_flag;
10754 omp_workshare_flag = 1;
10755 gfc_resolve_omp_parallel_blocks (code, ns);
10756 break;
10757 case EXEC_OMP_PARALLEL:
10758 case EXEC_OMP_PARALLEL_DO:
10759 case EXEC_OMP_PARALLEL_DO_SIMD:
10760 case EXEC_OMP_PARALLEL_SECTIONS:
10761 case EXEC_OMP_TARGET_PARALLEL:
10762 case EXEC_OMP_TARGET_PARALLEL_DO:
10763 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
10764 case EXEC_OMP_TARGET_TEAMS:
10765 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10766 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10767 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10768 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10769 case EXEC_OMP_TASK:
10770 case EXEC_OMP_TEAMS:
10771 case EXEC_OMP_TEAMS_DISTRIBUTE:
10772 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10773 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10774 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
10775 omp_workshare_save = omp_workshare_flag;
10776 omp_workshare_flag = 0;
10777 gfc_resolve_omp_parallel_blocks (code, ns);
10778 break;
10779 case EXEC_OMP_DISTRIBUTE:
10780 case EXEC_OMP_DISTRIBUTE_SIMD:
10781 case EXEC_OMP_DO:
10782 case EXEC_OMP_DO_SIMD:
10783 case EXEC_OMP_SIMD:
10784 case EXEC_OMP_TARGET_SIMD:
10785 case EXEC_OMP_TASKLOOP:
10786 case EXEC_OMP_TASKLOOP_SIMD:
10787 gfc_resolve_omp_do_blocks (code, ns);
10788 break;
10789 case EXEC_SELECT_TYPE:
10790 /* Blocks are handled in resolve_select_type because we have
10791 to transform the SELECT TYPE into ASSOCIATE first. */
10792 break;
10793 case EXEC_DO_CONCURRENT:
10794 gfc_do_concurrent_flag = 1;
10795 gfc_resolve_blocks (code->block, ns);
10796 gfc_do_concurrent_flag = 2;
10797 break;
10798 case EXEC_OMP_WORKSHARE:
10799 omp_workshare_save = omp_workshare_flag;
10800 omp_workshare_flag = 1;
10801 /* FALL THROUGH */
10802 default:
10803 gfc_resolve_blocks (code->block, ns);
10804 break;
10805 }
10806
10807 if (omp_workshare_save != -1)
10808 omp_workshare_flag = omp_workshare_save;
10809 }
10810 start:
10811 t = true;
10812 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
10813 t = gfc_resolve_expr (code->expr1);
10814 forall_flag = forall_save;
10815 gfc_do_concurrent_flag = do_concurrent_save;
10816
10817 if (!gfc_resolve_expr (code->expr2))
10818 t = false;
10819
10820 if (code->op == EXEC_ALLOCATE
10821 && !gfc_resolve_expr (code->expr3))
10822 t = false;
10823
10824 switch (code->op)
10825 {
10826 case EXEC_NOP:
10827 case EXEC_END_BLOCK:
10828 case EXEC_END_NESTED_BLOCK:
10829 case EXEC_CYCLE:
10830 case EXEC_PAUSE:
10831 case EXEC_STOP:
10832 case EXEC_ERROR_STOP:
10833 case EXEC_EXIT:
10834 case EXEC_CONTINUE:
10835 case EXEC_DT_END:
10836 case EXEC_ASSIGN_CALL:
10837 break;
10838
10839 case EXEC_CRITICAL:
10840 resolve_critical (code);
10841 break;
10842
10843 case EXEC_SYNC_ALL:
10844 case EXEC_SYNC_IMAGES:
10845 case EXEC_SYNC_MEMORY:
10846 resolve_sync (code);
10847 break;
10848
10849 case EXEC_LOCK:
10850 case EXEC_UNLOCK:
10851 case EXEC_EVENT_POST:
10852 case EXEC_EVENT_WAIT:
10853 resolve_lock_unlock_event (code);
10854 break;
10855
10856 case EXEC_ENTRY:
10857 /* Keep track of which entry we are up to. */
10858 current_entry_id = code->ext.entry->id;
10859 break;
10860
10861 case EXEC_WHERE:
10862 resolve_where (code, NULL);
10863 break;
10864
10865 case EXEC_GOTO:
10866 if (code->expr1 != NULL)
10867 {
10868 if (code->expr1->ts.type != BT_INTEGER)
10869 gfc_error ("ASSIGNED GOTO statement at %L requires an "
10870 "INTEGER variable", &code->expr1->where);
10871 else if (code->expr1->symtree->n.sym->attr.assign != 1)
10872 gfc_error ("Variable %qs has not been assigned a target "
10873 "label at %L", code->expr1->symtree->n.sym->name,
10874 &code->expr1->where);
10875 }
10876 else
10877 resolve_branch (code->label1, code);
10878 break;
10879
10880 case EXEC_RETURN:
10881 if (code->expr1 != NULL
10882 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
10883 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
10884 "INTEGER return specifier", &code->expr1->where);
10885 break;
10886
10887 case EXEC_INIT_ASSIGN:
10888 case EXEC_END_PROCEDURE:
10889 break;
10890
10891 case EXEC_ASSIGN:
10892 if (!t)
10893 break;
10894
10895 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
10896 the LHS. */
10897 if (code->expr1->expr_type == EXPR_FUNCTION
10898 && code->expr1->value.function.isym
10899 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
10900 remove_caf_get_intrinsic (code->expr1);
10901
10902 /* If this is a pointer function in an lvalue variable context,
10903 the new code will have to be resolved afresh. This is also the
10904 case with an error, where the code is transformed into NOP to
10905 prevent ICEs downstream. */
10906 if (resolve_ptr_fcn_assign (&code, ns)
10907 || code->op == EXEC_NOP)
10908 goto start;
10909
10910 if (!gfc_check_vardef_context (code->expr1, false, false, false,
10911 _("assignment")))
10912 break;
10913
10914 if (resolve_ordinary_assign (code, ns))
10915 {
10916 if (code->op == EXEC_COMPCALL)
10917 goto compcall;
10918 else
10919 goto call;
10920 }
10921
10922 /* Check for dependencies in deferred character length array
10923 assignments and generate a temporary, if necessary. */
10924 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
10925 break;
10926
10927 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
10928 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
10929 && code->expr1->ts.u.derived
10930 && code->expr1->ts.u.derived->attr.defined_assign_comp)
10931 generate_component_assignments (&code, ns);
10932
10933 break;
10934
10935 case EXEC_LABEL_ASSIGN:
10936 if (code->label1->defined == ST_LABEL_UNKNOWN)
10937 gfc_error ("Label %d referenced at %L is never defined",
10938 code->label1->value, &code->label1->where);
10939 if (t
10940 && (code->expr1->expr_type != EXPR_VARIABLE
10941 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
10942 || code->expr1->symtree->n.sym->ts.kind
10943 != gfc_default_integer_kind
10944 || code->expr1->symtree->n.sym->as != NULL))
10945 gfc_error ("ASSIGN statement at %L requires a scalar "
10946 "default INTEGER variable", &code->expr1->where);
10947 break;
10948
10949 case EXEC_POINTER_ASSIGN:
10950 {
10951 gfc_expr* e;
10952
10953 if (!t)
10954 break;
10955
10956 /* This is both a variable definition and pointer assignment
10957 context, so check both of them. For rank remapping, a final
10958 array ref may be present on the LHS and fool gfc_expr_attr
10959 used in gfc_check_vardef_context. Remove it. */
10960 e = remove_last_array_ref (code->expr1);
10961 t = gfc_check_vardef_context (e, true, false, false,
10962 _("pointer assignment"));
10963 if (t)
10964 t = gfc_check_vardef_context (e, false, false, false,
10965 _("pointer assignment"));
10966 gfc_free_expr (e);
10967 if (!t)
10968 break;
10969
10970 gfc_check_pointer_assign (code->expr1, code->expr2);
10971
10972 /* Assigning a class object always is a regular assign. */
10973 if (code->expr2->ts.type == BT_CLASS
10974 && !CLASS_DATA (code->expr2)->attr.dimension
10975 && !(UNLIMITED_POLY (code->expr2)
10976 && code->expr1->ts.type == BT_DERIVED
10977 && (code->expr1->ts.u.derived->attr.sequence
10978 || code->expr1->ts.u.derived->attr.is_bind_c))
10979 && !(gfc_expr_attr (code->expr1).proc_pointer
10980 && code->expr2->expr_type == EXPR_VARIABLE
10981 && code->expr2->symtree->n.sym->attr.flavor
10982 == FL_PROCEDURE))
10983 code->op = EXEC_ASSIGN;
10984 break;
10985 }
10986
10987 case EXEC_ARITHMETIC_IF:
10988 {
10989 gfc_expr *e = code->expr1;
10990
10991 gfc_resolve_expr (e);
10992 if (e->expr_type == EXPR_NULL)
10993 gfc_error ("Invalid NULL at %L", &e->where);
10994
10995 if (t && (e->rank > 0
10996 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
10997 gfc_error ("Arithmetic IF statement at %L requires a scalar "
10998 "REAL or INTEGER expression", &e->where);
10999
11000 resolve_branch (code->label1, code);
11001 resolve_branch (code->label2, code);
11002 resolve_branch (code->label3, code);
11003 }
11004 break;
11005
11006 case EXEC_IF:
11007 if (t && code->expr1 != NULL
11008 && (code->expr1->ts.type != BT_LOGICAL
11009 || code->expr1->rank != 0))
11010 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
11011 &code->expr1->where);
11012 break;
11013
11014 case EXEC_CALL:
11015 call:
11016 resolve_call (code);
11017 break;
11018
11019 case EXEC_COMPCALL:
11020 compcall:
11021 resolve_typebound_subroutine (code);
11022 break;
11023
11024 case EXEC_CALL_PPC:
11025 resolve_ppc_call (code);
11026 break;
11027
11028 case EXEC_SELECT:
11029 /* Select is complicated. Also, a SELECT construct could be
11030 a transformed computed GOTO. */
11031 resolve_select (code, false);
11032 break;
11033
11034 case EXEC_SELECT_TYPE:
11035 resolve_select_type (code, ns);
11036 break;
11037
11038 case EXEC_BLOCK:
11039 resolve_block_construct (code);
11040 break;
11041
11042 case EXEC_DO:
11043 if (code->ext.iterator != NULL)
11044 {
11045 gfc_iterator *iter = code->ext.iterator;
11046 if (gfc_resolve_iterator (iter, true, false))
11047 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
11048 }
11049 break;
11050
11051 case EXEC_DO_WHILE:
11052 if (code->expr1 == NULL)
11053 gfc_internal_error ("gfc_resolve_code(): No expression on "
11054 "DO WHILE");
11055 if (t
11056 && (code->expr1->rank != 0
11057 || code->expr1->ts.type != BT_LOGICAL))
11058 gfc_error ("Exit condition of DO WHILE loop at %L must be "
11059 "a scalar LOGICAL expression", &code->expr1->where);
11060 break;
11061
11062 case EXEC_ALLOCATE:
11063 if (t)
11064 resolve_allocate_deallocate (code, "ALLOCATE");
11065
11066 break;
11067
11068 case EXEC_DEALLOCATE:
11069 if (t)
11070 resolve_allocate_deallocate (code, "DEALLOCATE");
11071
11072 break;
11073
11074 case EXEC_OPEN:
11075 if (!gfc_resolve_open (code->ext.open))
11076 break;
11077
11078 resolve_branch (code->ext.open->err, code);
11079 break;
11080
11081 case EXEC_CLOSE:
11082 if (!gfc_resolve_close (code->ext.close))
11083 break;
11084
11085 resolve_branch (code->ext.close->err, code);
11086 break;
11087
11088 case EXEC_BACKSPACE:
11089 case EXEC_ENDFILE:
11090 case EXEC_REWIND:
11091 case EXEC_FLUSH:
11092 if (!gfc_resolve_filepos (code->ext.filepos))
11093 break;
11094
11095 resolve_branch (code->ext.filepos->err, code);
11096 break;
11097
11098 case EXEC_INQUIRE:
11099 if (!gfc_resolve_inquire (code->ext.inquire))
11100 break;
11101
11102 resolve_branch (code->ext.inquire->err, code);
11103 break;
11104
11105 case EXEC_IOLENGTH:
11106 gcc_assert (code->ext.inquire != NULL);
11107 if (!gfc_resolve_inquire (code->ext.inquire))
11108 break;
11109
11110 resolve_branch (code->ext.inquire->err, code);
11111 break;
11112
11113 case EXEC_WAIT:
11114 if (!gfc_resolve_wait (code->ext.wait))
11115 break;
11116
11117 resolve_branch (code->ext.wait->err, code);
11118 resolve_branch (code->ext.wait->end, code);
11119 resolve_branch (code->ext.wait->eor, code);
11120 break;
11121
11122 case EXEC_READ:
11123 case EXEC_WRITE:
11124 if (!gfc_resolve_dt (code->ext.dt, &code->loc))
11125 break;
11126
11127 resolve_branch (code->ext.dt->err, code);
11128 resolve_branch (code->ext.dt->end, code);
11129 resolve_branch (code->ext.dt->eor, code);
11130 break;
11131
11132 case EXEC_TRANSFER:
11133 resolve_transfer (code);
11134 break;
11135
11136 case EXEC_DO_CONCURRENT:
11137 case EXEC_FORALL:
11138 resolve_forall_iterators (code->ext.forall_iterator);
11139
11140 if (code->expr1 != NULL
11141 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
11142 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
11143 "expression", &code->expr1->where);
11144 break;
11145
11146 case EXEC_OACC_PARALLEL_LOOP:
11147 case EXEC_OACC_PARALLEL:
11148 case EXEC_OACC_KERNELS_LOOP:
11149 case EXEC_OACC_KERNELS:
11150 case EXEC_OACC_DATA:
11151 case EXEC_OACC_HOST_DATA:
11152 case EXEC_OACC_LOOP:
11153 case EXEC_OACC_UPDATE:
11154 case EXEC_OACC_WAIT:
11155 case EXEC_OACC_CACHE:
11156 case EXEC_OACC_ENTER_DATA:
11157 case EXEC_OACC_EXIT_DATA:
11158 case EXEC_OACC_ATOMIC:
11159 case EXEC_OACC_DECLARE:
11160 gfc_resolve_oacc_directive (code, ns);
11161 break;
11162
11163 case EXEC_OMP_ATOMIC:
11164 case EXEC_OMP_BARRIER:
11165 case EXEC_OMP_CANCEL:
11166 case EXEC_OMP_CANCELLATION_POINT:
11167 case EXEC_OMP_CRITICAL:
11168 case EXEC_OMP_FLUSH:
11169 case EXEC_OMP_DISTRIBUTE:
11170 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
11171 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
11172 case EXEC_OMP_DISTRIBUTE_SIMD:
11173 case EXEC_OMP_DO:
11174 case EXEC_OMP_DO_SIMD:
11175 case EXEC_OMP_MASTER:
11176 case EXEC_OMP_ORDERED:
11177 case EXEC_OMP_SECTIONS:
11178 case EXEC_OMP_SIMD:
11179 case EXEC_OMP_SINGLE:
11180 case EXEC_OMP_TARGET:
11181 case EXEC_OMP_TARGET_DATA:
11182 case EXEC_OMP_TARGET_ENTER_DATA:
11183 case EXEC_OMP_TARGET_EXIT_DATA:
11184 case EXEC_OMP_TARGET_PARALLEL:
11185 case EXEC_OMP_TARGET_PARALLEL_DO:
11186 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
11187 case EXEC_OMP_TARGET_SIMD:
11188 case EXEC_OMP_TARGET_TEAMS:
11189 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
11190 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
11191 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11192 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
11193 case EXEC_OMP_TARGET_UPDATE:
11194 case EXEC_OMP_TASK:
11195 case EXEC_OMP_TASKGROUP:
11196 case EXEC_OMP_TASKLOOP:
11197 case EXEC_OMP_TASKLOOP_SIMD:
11198 case EXEC_OMP_TASKWAIT:
11199 case EXEC_OMP_TASKYIELD:
11200 case EXEC_OMP_TEAMS:
11201 case EXEC_OMP_TEAMS_DISTRIBUTE:
11202 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
11203 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
11204 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
11205 case EXEC_OMP_WORKSHARE:
11206 gfc_resolve_omp_directive (code, ns);
11207 break;
11208
11209 case EXEC_OMP_PARALLEL:
11210 case EXEC_OMP_PARALLEL_DO:
11211 case EXEC_OMP_PARALLEL_DO_SIMD:
11212 case EXEC_OMP_PARALLEL_SECTIONS:
11213 case EXEC_OMP_PARALLEL_WORKSHARE:
11214 omp_workshare_save = omp_workshare_flag;
11215 omp_workshare_flag = 0;
11216 gfc_resolve_omp_directive (code, ns);
11217 omp_workshare_flag = omp_workshare_save;
11218 break;
11219
11220 default:
11221 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
11222 }
11223 }
11224
11225 cs_base = frame.prev;
11226 }
11227
11228
11229 /* Resolve initial values and make sure they are compatible with
11230 the variable. */
11231
11232 static void
11233 resolve_values (gfc_symbol *sym)
11234 {
11235 bool t;
11236
11237 if (sym->value == NULL)
11238 return;
11239
11240 if (sym->value->expr_type == EXPR_STRUCTURE)
11241 t= resolve_structure_cons (sym->value, 1);
11242 else
11243 t = gfc_resolve_expr (sym->value);
11244
11245 if (!t)
11246 return;
11247
11248 gfc_check_assign_symbol (sym, NULL, sym->value);
11249 }
11250
11251
11252 /* Verify any BIND(C) derived types in the namespace so we can report errors
11253 for them once, rather than for each variable declared of that type. */
11254
11255 static void
11256 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
11257 {
11258 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
11259 && derived_sym->attr.is_bind_c == 1)
11260 verify_bind_c_derived_type (derived_sym);
11261
11262 return;
11263 }
11264
11265
11266 /* Check the interfaces of DTIO procedures associated with derived
11267 type 'sym'. These procedures can either have typebound bindings or
11268 can appear in DTIO generic interfaces. */
11269
11270 static void
11271 gfc_verify_DTIO_procedures (gfc_symbol *sym)
11272 {
11273 if (!sym || sym->attr.flavor != FL_DERIVED)
11274 return;
11275
11276 gfc_check_dtio_interfaces (sym);
11277
11278 return;
11279 }
11280
11281 /* Verify that any binding labels used in a given namespace do not collide
11282 with the names or binding labels of any global symbols. Multiple INTERFACE
11283 for the same procedure are permitted. */
11284
11285 static void
11286 gfc_verify_binding_labels (gfc_symbol *sym)
11287 {
11288 gfc_gsymbol *gsym;
11289 const char *module;
11290
11291 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
11292 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
11293 return;
11294
11295 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
11296
11297 if (sym->module)
11298 module = sym->module;
11299 else if (sym->ns && sym->ns->proc_name
11300 && sym->ns->proc_name->attr.flavor == FL_MODULE)
11301 module = sym->ns->proc_name->name;
11302 else if (sym->ns && sym->ns->parent
11303 && sym->ns && sym->ns->parent->proc_name
11304 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
11305 module = sym->ns->parent->proc_name->name;
11306 else
11307 module = NULL;
11308
11309 if (!gsym
11310 || (!gsym->defined
11311 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
11312 {
11313 if (!gsym)
11314 gsym = gfc_get_gsymbol (sym->binding_label);
11315 gsym->where = sym->declared_at;
11316 gsym->sym_name = sym->name;
11317 gsym->binding_label = sym->binding_label;
11318 gsym->ns = sym->ns;
11319 gsym->mod_name = module;
11320 if (sym->attr.function)
11321 gsym->type = GSYM_FUNCTION;
11322 else if (sym->attr.subroutine)
11323 gsym->type = GSYM_SUBROUTINE;
11324 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
11325 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
11326 return;
11327 }
11328
11329 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
11330 {
11331 gfc_error ("Variable %s with binding label %s at %L uses the same global "
11332 "identifier as entity at %L", sym->name,
11333 sym->binding_label, &sym->declared_at, &gsym->where);
11334 /* Clear the binding label to prevent checking multiple times. */
11335 sym->binding_label = NULL;
11336
11337 }
11338 else if (sym->attr.flavor == FL_VARIABLE && module
11339 && (strcmp (module, gsym->mod_name) != 0
11340 || strcmp (sym->name, gsym->sym_name) != 0))
11341 {
11342 /* This can only happen if the variable is defined in a module - if it
11343 isn't the same module, reject it. */
11344 gfc_error ("Variable %s from module %s with binding label %s at %L uses "
11345 "the same global identifier as entity at %L from module %s",
11346 sym->name, module, sym->binding_label,
11347 &sym->declared_at, &gsym->where, gsym->mod_name);
11348 sym->binding_label = NULL;
11349 }
11350 else if ((sym->attr.function || sym->attr.subroutine)
11351 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
11352 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
11353 && sym != gsym->ns->proc_name
11354 && (module != gsym->mod_name
11355 || strcmp (gsym->sym_name, sym->name) != 0
11356 || (module && strcmp (module, gsym->mod_name) != 0)))
11357 {
11358 /* Print an error if the procedure is defined multiple times; we have to
11359 exclude references to the same procedure via module association or
11360 multiple checks for the same procedure. */
11361 gfc_error ("Procedure %s with binding label %s at %L uses the same "
11362 "global identifier as entity at %L", sym->name,
11363 sym->binding_label, &sym->declared_at, &gsym->where);
11364 sym->binding_label = NULL;
11365 }
11366 }
11367
11368
11369 /* Resolve an index expression. */
11370
11371 static bool
11372 resolve_index_expr (gfc_expr *e)
11373 {
11374 if (!gfc_resolve_expr (e))
11375 return false;
11376
11377 if (!gfc_simplify_expr (e, 0))
11378 return false;
11379
11380 if (!gfc_specification_expr (e))
11381 return false;
11382
11383 return true;
11384 }
11385
11386
11387 /* Resolve a charlen structure. */
11388
11389 static bool
11390 resolve_charlen (gfc_charlen *cl)
11391 {
11392 int i, k;
11393 bool saved_specification_expr;
11394
11395 if (cl->resolved)
11396 return true;
11397
11398 cl->resolved = 1;
11399 saved_specification_expr = specification_expr;
11400 specification_expr = true;
11401
11402 if (cl->length_from_typespec)
11403 {
11404 if (!gfc_resolve_expr (cl->length))
11405 {
11406 specification_expr = saved_specification_expr;
11407 return false;
11408 }
11409
11410 if (!gfc_simplify_expr (cl->length, 0))
11411 {
11412 specification_expr = saved_specification_expr;
11413 return false;
11414 }
11415 }
11416 else
11417 {
11418
11419 if (!resolve_index_expr (cl->length))
11420 {
11421 specification_expr = saved_specification_expr;
11422 return false;
11423 }
11424 }
11425
11426 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
11427 a negative value, the length of character entities declared is zero. */
11428 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
11429 gfc_replace_expr (cl->length,
11430 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
11431
11432 /* Check that the character length is not too large. */
11433 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
11434 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
11435 && cl->length->ts.type == BT_INTEGER
11436 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
11437 {
11438 gfc_error ("String length at %L is too large", &cl->length->where);
11439 specification_expr = saved_specification_expr;
11440 return false;
11441 }
11442
11443 specification_expr = saved_specification_expr;
11444 return true;
11445 }
11446
11447
11448 /* Test for non-constant shape arrays. */
11449
11450 static bool
11451 is_non_constant_shape_array (gfc_symbol *sym)
11452 {
11453 gfc_expr *e;
11454 int i;
11455 bool not_constant;
11456
11457 not_constant = false;
11458 if (sym->as != NULL)
11459 {
11460 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
11461 has not been simplified; parameter array references. Do the
11462 simplification now. */
11463 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
11464 {
11465 e = sym->as->lower[i];
11466 if (e && (!resolve_index_expr(e)
11467 || !gfc_is_constant_expr (e)))
11468 not_constant = true;
11469 e = sym->as->upper[i];
11470 if (e && (!resolve_index_expr(e)
11471 || !gfc_is_constant_expr (e)))
11472 not_constant = true;
11473 }
11474 }
11475 return not_constant;
11476 }
11477
11478 /* Given a symbol and an initialization expression, add code to initialize
11479 the symbol to the function entry. */
11480 static void
11481 build_init_assign (gfc_symbol *sym, gfc_expr *init)
11482 {
11483 gfc_expr *lval;
11484 gfc_code *init_st;
11485 gfc_namespace *ns = sym->ns;
11486
11487 /* Search for the function namespace if this is a contained
11488 function without an explicit result. */
11489 if (sym->attr.function && sym == sym->result
11490 && sym->name != sym->ns->proc_name->name)
11491 {
11492 ns = ns->contained;
11493 for (;ns; ns = ns->sibling)
11494 if (strcmp (ns->proc_name->name, sym->name) == 0)
11495 break;
11496 }
11497
11498 if (ns == NULL)
11499 {
11500 gfc_free_expr (init);
11501 return;
11502 }
11503
11504 /* Build an l-value expression for the result. */
11505 lval = gfc_lval_expr_from_sym (sym);
11506
11507 /* Add the code at scope entry. */
11508 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
11509 init_st->next = ns->code;
11510 ns->code = init_st;
11511
11512 /* Assign the default initializer to the l-value. */
11513 init_st->loc = sym->declared_at;
11514 init_st->expr1 = lval;
11515 init_st->expr2 = init;
11516 }
11517
11518
11519 /* Whether or not we can generate a default initializer for a symbol. */
11520
11521 static bool
11522 can_generate_init (gfc_symbol *sym)
11523 {
11524 symbol_attribute *a;
11525 if (!sym)
11526 return false;
11527 a = &sym->attr;
11528
11529 /* These symbols should never have a default initialization. */
11530 return !(
11531 a->allocatable
11532 || a->external
11533 || a->pointer
11534 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
11535 && (CLASS_DATA (sym)->attr.class_pointer
11536 || CLASS_DATA (sym)->attr.proc_pointer))
11537 || a->in_equivalence
11538 || a->in_common
11539 || a->data
11540 || sym->module
11541 || a->cray_pointee
11542 || a->cray_pointer
11543 || sym->assoc
11544 || (!a->referenced && !a->result)
11545 || (a->dummy && a->intent != INTENT_OUT)
11546 || (a->function && sym != sym->result)
11547 );
11548 }
11549
11550
11551 /* Assign the default initializer to a derived type variable or result. */
11552
11553 static void
11554 apply_default_init (gfc_symbol *sym)
11555 {
11556 gfc_expr *init = NULL;
11557
11558 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
11559 return;
11560
11561 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
11562 init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
11563
11564 if (init == NULL && sym->ts.type != BT_CLASS)
11565 return;
11566
11567 build_init_assign (sym, init);
11568 sym->attr.referenced = 1;
11569 }
11570
11571
11572 /* Build an initializer for a local. Returns null if the symbol should not have
11573 a default initialization. */
11574
11575 static gfc_expr *
11576 build_default_init_expr (gfc_symbol *sym)
11577 {
11578 /* These symbols should never have a default initialization. */
11579 if (sym->attr.allocatable
11580 || sym->attr.external
11581 || sym->attr.dummy
11582 || sym->attr.pointer
11583 || sym->attr.in_equivalence
11584 || sym->attr.in_common
11585 || sym->attr.data
11586 || sym->module
11587 || sym->attr.cray_pointee
11588 || sym->attr.cray_pointer
11589 || sym->assoc)
11590 return NULL;
11591
11592 /* Get the appropriate init expression. */
11593 return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
11594 }
11595
11596 /* Add an initialization expression to a local variable. */
11597 static void
11598 apply_default_init_local (gfc_symbol *sym)
11599 {
11600 gfc_expr *init = NULL;
11601
11602 /* The symbol should be a variable or a function return value. */
11603 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
11604 || (sym->attr.function && sym->result != sym))
11605 return;
11606
11607 /* Try to build the initializer expression. If we can't initialize
11608 this symbol, then init will be NULL. */
11609 init = build_default_init_expr (sym);
11610 if (init == NULL)
11611 return;
11612
11613 /* For saved variables, we don't want to add an initializer at function
11614 entry, so we just add a static initializer. Note that automatic variables
11615 are stack allocated even with -fno-automatic; we have also to exclude
11616 result variable, which are also nonstatic. */
11617 if (!sym->attr.automatic
11618 && (sym->attr.save || sym->ns->save_all
11619 || (flag_max_stack_var_size == 0 && !sym->attr.result
11620 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
11621 && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
11622 {
11623 /* Don't clobber an existing initializer! */
11624 gcc_assert (sym->value == NULL);
11625 sym->value = init;
11626 return;
11627 }
11628
11629 build_init_assign (sym, init);
11630 }
11631
11632
11633 /* Resolution of common features of flavors variable and procedure. */
11634
11635 static bool
11636 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
11637 {
11638 gfc_array_spec *as;
11639
11640 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
11641 as = CLASS_DATA (sym)->as;
11642 else
11643 as = sym->as;
11644
11645 /* Constraints on deferred shape variable. */
11646 if (as == NULL || as->type != AS_DEFERRED)
11647 {
11648 bool pointer, allocatable, dimension;
11649
11650 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
11651 {
11652 pointer = CLASS_DATA (sym)->attr.class_pointer;
11653 allocatable = CLASS_DATA (sym)->attr.allocatable;
11654 dimension = CLASS_DATA (sym)->attr.dimension;
11655 }
11656 else
11657 {
11658 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
11659 allocatable = sym->attr.allocatable;
11660 dimension = sym->attr.dimension;
11661 }
11662
11663 if (allocatable)
11664 {
11665 if (dimension && as->type != AS_ASSUMED_RANK)
11666 {
11667 gfc_error ("Allocatable array %qs at %L must have a deferred "
11668 "shape or assumed rank", sym->name, &sym->declared_at);
11669 return false;
11670 }
11671 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
11672 "%qs at %L may not be ALLOCATABLE",
11673 sym->name, &sym->declared_at))
11674 return false;
11675 }
11676
11677 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
11678 {
11679 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
11680 "assumed rank", sym->name, &sym->declared_at);
11681 return false;
11682 }
11683 }
11684 else
11685 {
11686 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
11687 && sym->ts.type != BT_CLASS && !sym->assoc)
11688 {
11689 gfc_error ("Array %qs at %L cannot have a deferred shape",
11690 sym->name, &sym->declared_at);
11691 return false;
11692 }
11693 }
11694
11695 /* Constraints on polymorphic variables. */
11696 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
11697 {
11698 /* F03:C502. */
11699 if (sym->attr.class_ok
11700 && !sym->attr.select_type_temporary
11701 && !UNLIMITED_POLY (sym)
11702 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
11703 {
11704 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
11705 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
11706 &sym->declared_at);
11707 return false;
11708 }
11709
11710 /* F03:C509. */
11711 /* Assume that use associated symbols were checked in the module ns.
11712 Class-variables that are associate-names are also something special
11713 and excepted from the test. */
11714 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
11715 {
11716 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
11717 "or pointer", sym->name, &sym->declared_at);
11718 return false;
11719 }
11720 }
11721
11722 return true;
11723 }
11724
11725
11726 /* Additional checks for symbols with flavor variable and derived
11727 type. To be called from resolve_fl_variable. */
11728
11729 static bool
11730 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
11731 {
11732 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
11733
11734 /* Check to see if a derived type is blocked from being host
11735 associated by the presence of another class I symbol in the same
11736 namespace. 14.6.1.3 of the standard and the discussion on
11737 comp.lang.fortran. */
11738 if (sym->ns != sym->ts.u.derived->ns
11739 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
11740 {
11741 gfc_symbol *s;
11742 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
11743 if (s && s->attr.generic)
11744 s = gfc_find_dt_in_generic (s);
11745 if (s && !gfc_fl_struct (s->attr.flavor))
11746 {
11747 gfc_error ("The type %qs cannot be host associated at %L "
11748 "because it is blocked by an incompatible object "
11749 "of the same name declared at %L",
11750 sym->ts.u.derived->name, &sym->declared_at,
11751 &s->declared_at);
11752 return false;
11753 }
11754 }
11755
11756 /* 4th constraint in section 11.3: "If an object of a type for which
11757 component-initialization is specified (R429) appears in the
11758 specification-part of a module and does not have the ALLOCATABLE
11759 or POINTER attribute, the object shall have the SAVE attribute."
11760
11761 The check for initializers is performed with
11762 gfc_has_default_initializer because gfc_default_initializer generates
11763 a hidden default for allocatable components. */
11764 if (!(sym->value || no_init_flag) && sym->ns->proc_name
11765 && sym->ns->proc_name->attr.flavor == FL_MODULE
11766 && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
11767 && !sym->attr.pointer && !sym->attr.allocatable
11768 && gfc_has_default_initializer (sym->ts.u.derived)
11769 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
11770 "%qs at %L, needed due to the default "
11771 "initialization", sym->name, &sym->declared_at))
11772 return false;
11773
11774 /* Assign default initializer. */
11775 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
11776 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
11777 sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
11778
11779 return true;
11780 }
11781
11782
11783 /* F2008, C402 (R401): A colon shall not be used as a type-param-value
11784 except in the declaration of an entity or component that has the POINTER
11785 or ALLOCATABLE attribute. */
11786
11787 static bool
11788 deferred_requirements (gfc_symbol *sym)
11789 {
11790 if (sym->ts.deferred
11791 && !(sym->attr.pointer
11792 || sym->attr.allocatable
11793 || sym->attr.omp_udr_artificial_var))
11794 {
11795 gfc_error ("Entity %qs at %L has a deferred type parameter and "
11796 "requires either the POINTER or ALLOCATABLE attribute",
11797 sym->name, &sym->declared_at);
11798 return false;
11799 }
11800 return true;
11801 }
11802
11803
11804 /* Resolve symbols with flavor variable. */
11805
11806 static bool
11807 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
11808 {
11809 int no_init_flag, automatic_flag;
11810 gfc_expr *e;
11811 const char *auto_save_msg;
11812 bool saved_specification_expr;
11813
11814 auto_save_msg = "Automatic object %qs at %L cannot have the "
11815 "SAVE attribute";
11816
11817 if (!resolve_fl_var_and_proc (sym, mp_flag))
11818 return false;
11819
11820 /* Set this flag to check that variables are parameters of all entries.
11821 This check is effected by the call to gfc_resolve_expr through
11822 is_non_constant_shape_array. */
11823 saved_specification_expr = specification_expr;
11824 specification_expr = true;
11825
11826 if (sym->ns->proc_name
11827 && (sym->ns->proc_name->attr.flavor == FL_MODULE
11828 || sym->ns->proc_name->attr.is_main_program)
11829 && !sym->attr.use_assoc
11830 && !sym->attr.allocatable
11831 && !sym->attr.pointer
11832 && is_non_constant_shape_array (sym))
11833 {
11834 /* The shape of a main program or module array needs to be
11835 constant. */
11836 gfc_error ("The module or main program array %qs at %L must "
11837 "have constant shape", sym->name, &sym->declared_at);
11838 specification_expr = saved_specification_expr;
11839 return false;
11840 }
11841
11842 /* Constraints on deferred type parameter. */
11843 if (!deferred_requirements (sym))
11844 return false;
11845
11846 if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
11847 {
11848 /* Make sure that character string variables with assumed length are
11849 dummy arguments. */
11850 e = sym->ts.u.cl->length;
11851 if (e == NULL && !sym->attr.dummy && !sym->attr.result
11852 && !sym->ts.deferred && !sym->attr.select_type_temporary
11853 && !sym->attr.omp_udr_artificial_var)
11854 {
11855 gfc_error ("Entity with assumed character length at %L must be a "
11856 "dummy argument or a PARAMETER", &sym->declared_at);
11857 specification_expr = saved_specification_expr;
11858 return false;
11859 }
11860
11861 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
11862 {
11863 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
11864 specification_expr = saved_specification_expr;
11865 return false;
11866 }
11867
11868 if (!gfc_is_constant_expr (e)
11869 && !(e->expr_type == EXPR_VARIABLE
11870 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
11871 {
11872 if (!sym->attr.use_assoc && sym->ns->proc_name
11873 && (sym->ns->proc_name->attr.flavor == FL_MODULE
11874 || sym->ns->proc_name->attr.is_main_program))
11875 {
11876 gfc_error ("%qs at %L must have constant character length "
11877 "in this context", sym->name, &sym->declared_at);
11878 specification_expr = saved_specification_expr;
11879 return false;
11880 }
11881 if (sym->attr.in_common)
11882 {
11883 gfc_error ("COMMON variable %qs at %L must have constant "
11884 "character length", sym->name, &sym->declared_at);
11885 specification_expr = saved_specification_expr;
11886 return false;
11887 }
11888 }
11889 }
11890
11891 if (sym->value == NULL && sym->attr.referenced)
11892 apply_default_init_local (sym); /* Try to apply a default initialization. */
11893
11894 /* Determine if the symbol may not have an initializer. */
11895 no_init_flag = automatic_flag = 0;
11896 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
11897 || sym->attr.intrinsic || sym->attr.result)
11898 no_init_flag = 1;
11899 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
11900 && is_non_constant_shape_array (sym))
11901 {
11902 no_init_flag = automatic_flag = 1;
11903
11904 /* Also, they must not have the SAVE attribute.
11905 SAVE_IMPLICIT is checked below. */
11906 if (sym->as && sym->attr.codimension)
11907 {
11908 int corank = sym->as->corank;
11909 sym->as->corank = 0;
11910 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
11911 sym->as->corank = corank;
11912 }
11913 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
11914 {
11915 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
11916 specification_expr = saved_specification_expr;
11917 return false;
11918 }
11919 }
11920
11921 /* Ensure that any initializer is simplified. */
11922 if (sym->value)
11923 gfc_simplify_expr (sym->value, 1);
11924
11925 /* Reject illegal initializers. */
11926 if (!sym->mark && sym->value)
11927 {
11928 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
11929 && CLASS_DATA (sym)->attr.allocatable))
11930 gfc_error ("Allocatable %qs at %L cannot have an initializer",
11931 sym->name, &sym->declared_at);
11932 else if (sym->attr.external)
11933 gfc_error ("External %qs at %L cannot have an initializer",
11934 sym->name, &sym->declared_at);
11935 else if (sym->attr.dummy
11936 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
11937 gfc_error ("Dummy %qs at %L cannot have an initializer",
11938 sym->name, &sym->declared_at);
11939 else if (sym->attr.intrinsic)
11940 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
11941 sym->name, &sym->declared_at);
11942 else if (sym->attr.result)
11943 gfc_error ("Function result %qs at %L cannot have an initializer",
11944 sym->name, &sym->declared_at);
11945 else if (automatic_flag)
11946 gfc_error ("Automatic array %qs at %L cannot have an initializer",
11947 sym->name, &sym->declared_at);
11948 else
11949 goto no_init_error;
11950 specification_expr = saved_specification_expr;
11951 return false;
11952 }
11953
11954 no_init_error:
11955 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
11956 {
11957 bool res = resolve_fl_variable_derived (sym, no_init_flag);
11958 specification_expr = saved_specification_expr;
11959 return res;
11960 }
11961
11962 specification_expr = saved_specification_expr;
11963 return true;
11964 }
11965
11966
11967 /* Compare the dummy characteristics of a module procedure interface
11968 declaration with the corresponding declaration in a submodule. */
11969 static gfc_formal_arglist *new_formal;
11970 static char errmsg[200];
11971
11972 static void
11973 compare_fsyms (gfc_symbol *sym)
11974 {
11975 gfc_symbol *fsym;
11976
11977 if (sym == NULL || new_formal == NULL)
11978 return;
11979
11980 fsym = new_formal->sym;
11981
11982 if (sym == fsym)
11983 return;
11984
11985 if (strcmp (sym->name, fsym->name) == 0)
11986 {
11987 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
11988 gfc_error ("%s at %L", errmsg, &fsym->declared_at);
11989 }
11990 }
11991
11992
11993 /* Resolve a procedure. */
11994
11995 static bool
11996 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
11997 {
11998 gfc_formal_arglist *arg;
11999
12000 if (sym->attr.function
12001 && !resolve_fl_var_and_proc (sym, mp_flag))
12002 return false;
12003
12004 if (sym->ts.type == BT_CHARACTER)
12005 {
12006 gfc_charlen *cl = sym->ts.u.cl;
12007
12008 if (cl && cl->length && gfc_is_constant_expr (cl->length)
12009 && !resolve_charlen (cl))
12010 return false;
12011
12012 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
12013 && sym->attr.proc == PROC_ST_FUNCTION)
12014 {
12015 gfc_error ("Character-valued statement function %qs at %L must "
12016 "have constant length", sym->name, &sym->declared_at);
12017 return false;
12018 }
12019 }
12020
12021 /* Ensure that derived type for are not of a private type. Internal
12022 module procedures are excluded by 2.2.3.3 - i.e., they are not
12023 externally accessible and can access all the objects accessible in
12024 the host. */
12025 if (!(sym->ns->parent
12026 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
12027 && gfc_check_symbol_access (sym))
12028 {
12029 gfc_interface *iface;
12030
12031 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
12032 {
12033 if (arg->sym
12034 && arg->sym->ts.type == BT_DERIVED
12035 && !arg->sym->ts.u.derived->attr.use_assoc
12036 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
12037 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
12038 "and cannot be a dummy argument"
12039 " of %qs, which is PUBLIC at %L",
12040 arg->sym->name, sym->name,
12041 &sym->declared_at))
12042 {
12043 /* Stop this message from recurring. */
12044 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
12045 return false;
12046 }
12047 }
12048
12049 /* PUBLIC interfaces may expose PRIVATE procedures that take types
12050 PRIVATE to the containing module. */
12051 for (iface = sym->generic; iface; iface = iface->next)
12052 {
12053 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
12054 {
12055 if (arg->sym
12056 && arg->sym->ts.type == BT_DERIVED
12057 && !arg->sym->ts.u.derived->attr.use_assoc
12058 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
12059 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
12060 "PUBLIC interface %qs at %L "
12061 "takes dummy arguments of %qs which "
12062 "is PRIVATE", iface->sym->name,
12063 sym->name, &iface->sym->declared_at,
12064 gfc_typename(&arg->sym->ts)))
12065 {
12066 /* Stop this message from recurring. */
12067 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
12068 return false;
12069 }
12070 }
12071 }
12072 }
12073
12074 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
12075 && !sym->attr.proc_pointer)
12076 {
12077 gfc_error ("Function %qs at %L cannot have an initializer",
12078 sym->name, &sym->declared_at);
12079 return false;
12080 }
12081
12082 /* An external symbol may not have an initializer because it is taken to be
12083 a procedure. Exception: Procedure Pointers. */
12084 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
12085 {
12086 gfc_error ("External object %qs at %L may not have an initializer",
12087 sym->name, &sym->declared_at);
12088 return false;
12089 }
12090
12091 /* An elemental function is required to return a scalar 12.7.1 */
12092 if (sym->attr.elemental && sym->attr.function && sym->as)
12093 {
12094 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
12095 "result", sym->name, &sym->declared_at);
12096 /* Reset so that the error only occurs once. */
12097 sym->attr.elemental = 0;
12098 return false;
12099 }
12100
12101 if (sym->attr.proc == PROC_ST_FUNCTION
12102 && (sym->attr.allocatable || sym->attr.pointer))
12103 {
12104 gfc_error ("Statement function %qs at %L may not have pointer or "
12105 "allocatable attribute", sym->name, &sym->declared_at);
12106 return false;
12107 }
12108
12109 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
12110 char-len-param shall not be array-valued, pointer-valued, recursive
12111 or pure. ....snip... A character value of * may only be used in the
12112 following ways: (i) Dummy arg of procedure - dummy associates with
12113 actual length; (ii) To declare a named constant; or (iii) External
12114 function - but length must be declared in calling scoping unit. */
12115 if (sym->attr.function
12116 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
12117 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
12118 {
12119 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
12120 || (sym->attr.recursive) || (sym->attr.pure))
12121 {
12122 if (sym->as && sym->as->rank)
12123 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
12124 "array-valued", sym->name, &sym->declared_at);
12125
12126 if (sym->attr.pointer)
12127 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
12128 "pointer-valued", sym->name, &sym->declared_at);
12129
12130 if (sym->attr.pure)
12131 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
12132 "pure", sym->name, &sym->declared_at);
12133
12134 if (sym->attr.recursive)
12135 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
12136 "recursive", sym->name, &sym->declared_at);
12137
12138 return false;
12139 }
12140
12141 /* Appendix B.2 of the standard. Contained functions give an
12142 error anyway. Deferred character length is an F2003 feature.
12143 Don't warn on intrinsic conversion functions, which start
12144 with two underscores. */
12145 if (!sym->attr.contained && !sym->ts.deferred
12146 && (sym->name[0] != '_' || sym->name[1] != '_'))
12147 gfc_notify_std (GFC_STD_F95_OBS,
12148 "CHARACTER(*) function %qs at %L",
12149 sym->name, &sym->declared_at);
12150 }
12151
12152 /* F2008, C1218. */
12153 if (sym->attr.elemental)
12154 {
12155 if (sym->attr.proc_pointer)
12156 {
12157 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
12158 sym->name, &sym->declared_at);
12159 return false;
12160 }
12161 if (sym->attr.dummy)
12162 {
12163 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
12164 sym->name, &sym->declared_at);
12165 return false;
12166 }
12167 }
12168
12169 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
12170 {
12171 gfc_formal_arglist *curr_arg;
12172 int has_non_interop_arg = 0;
12173
12174 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
12175 sym->common_block))
12176 {
12177 /* Clear these to prevent looking at them again if there was an
12178 error. */
12179 sym->attr.is_bind_c = 0;
12180 sym->attr.is_c_interop = 0;
12181 sym->ts.is_c_interop = 0;
12182 }
12183 else
12184 {
12185 /* So far, no errors have been found. */
12186 sym->attr.is_c_interop = 1;
12187 sym->ts.is_c_interop = 1;
12188 }
12189
12190 curr_arg = gfc_sym_get_dummy_args (sym);
12191 while (curr_arg != NULL)
12192 {
12193 /* Skip implicitly typed dummy args here. */
12194 if (curr_arg->sym->attr.implicit_type == 0)
12195 if (!gfc_verify_c_interop_param (curr_arg->sym))
12196 /* If something is found to fail, record the fact so we
12197 can mark the symbol for the procedure as not being
12198 BIND(C) to try and prevent multiple errors being
12199 reported. */
12200 has_non_interop_arg = 1;
12201
12202 curr_arg = curr_arg->next;
12203 }
12204
12205 /* See if any of the arguments were not interoperable and if so, clear
12206 the procedure symbol to prevent duplicate error messages. */
12207 if (has_non_interop_arg != 0)
12208 {
12209 sym->attr.is_c_interop = 0;
12210 sym->ts.is_c_interop = 0;
12211 sym->attr.is_bind_c = 0;
12212 }
12213 }
12214
12215 if (!sym->attr.proc_pointer)
12216 {
12217 if (sym->attr.save == SAVE_EXPLICIT)
12218 {
12219 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
12220 "in %qs at %L", sym->name, &sym->declared_at);
12221 return false;
12222 }
12223 if (sym->attr.intent)
12224 {
12225 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
12226 "in %qs at %L", sym->name, &sym->declared_at);
12227 return false;
12228 }
12229 if (sym->attr.subroutine && sym->attr.result)
12230 {
12231 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
12232 "in %qs at %L", sym->name, &sym->declared_at);
12233 return false;
12234 }
12235 if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
12236 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
12237 || sym->attr.contained))
12238 {
12239 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
12240 "in %qs at %L", sym->name, &sym->declared_at);
12241 return false;
12242 }
12243 if (strcmp ("ppr@", sym->name) == 0)
12244 {
12245 gfc_error ("Procedure pointer result %qs at %L "
12246 "is missing the pointer attribute",
12247 sym->ns->proc_name->name, &sym->declared_at);
12248 return false;
12249 }
12250 }
12251
12252 /* Assume that a procedure whose body is not known has references
12253 to external arrays. */
12254 if (sym->attr.if_source != IFSRC_DECL)
12255 sym->attr.array_outer_dependency = 1;
12256
12257 /* Compare the characteristics of a module procedure with the
12258 interface declaration. Ideally this would be done with
12259 gfc_compare_interfaces but, at present, the formal interface
12260 cannot be copied to the ts.interface. */
12261 if (sym->attr.module_procedure
12262 && sym->attr.if_source == IFSRC_DECL)
12263 {
12264 gfc_symbol *iface;
12265 char name[2*GFC_MAX_SYMBOL_LEN + 1];
12266 char *module_name;
12267 char *submodule_name;
12268 strcpy (name, sym->ns->proc_name->name);
12269 module_name = strtok (name, ".");
12270 submodule_name = strtok (NULL, ".");
12271
12272 /* Stop the dummy characteristics test from using the interface
12273 symbol instead of 'sym'. */
12274 iface = sym->ts.interface;
12275 sym->ts.interface = NULL;
12276
12277 /* Make sure that the result uses the correct charlen for deferred
12278 length results. */
12279 if (iface && sym->result
12280 && iface->ts.type == BT_CHARACTER
12281 && iface->ts.deferred)
12282 sym->result->ts.u.cl = iface->ts.u.cl;
12283
12284 if (iface == NULL)
12285 goto check_formal;
12286
12287 /* Check the procedure characteristics. */
12288 if (sym->attr.elemental != iface->attr.elemental)
12289 {
12290 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
12291 "PROCEDURE at %L and its interface in %s",
12292 &sym->declared_at, module_name);
12293 return false;
12294 }
12295
12296 if (sym->attr.pure != iface->attr.pure)
12297 {
12298 gfc_error ("Mismatch in PURE attribute between MODULE "
12299 "PROCEDURE at %L and its interface in %s",
12300 &sym->declared_at, module_name);
12301 return false;
12302 }
12303
12304 if (sym->attr.recursive != iface->attr.recursive)
12305 {
12306 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
12307 "PROCEDURE at %L and its interface in %s",
12308 &sym->declared_at, module_name);
12309 return false;
12310 }
12311
12312 /* Check the result characteristics. */
12313 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
12314 {
12315 gfc_error ("%s between the MODULE PROCEDURE declaration "
12316 "in module %s and the declaration at %L in "
12317 "SUBMODULE %s", errmsg, module_name,
12318 &sym->declared_at, submodule_name);
12319 return false;
12320 }
12321
12322 check_formal:
12323 /* Check the charcateristics of the formal arguments. */
12324 if (sym->formal && sym->formal_ns)
12325 {
12326 for (arg = sym->formal; arg && arg->sym; arg = arg->next)
12327 {
12328 new_formal = arg;
12329 gfc_traverse_ns (sym->formal_ns, compare_fsyms);
12330 }
12331 }
12332
12333 sym->ts.interface = iface;
12334 }
12335 return true;
12336 }
12337
12338
12339 /* Resolve a list of finalizer procedures. That is, after they have hopefully
12340 been defined and we now know their defined arguments, check that they fulfill
12341 the requirements of the standard for procedures used as finalizers. */
12342
12343 static bool
12344 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
12345 {
12346 gfc_finalizer* list;
12347 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
12348 bool result = true;
12349 bool seen_scalar = false;
12350 gfc_symbol *vtab;
12351 gfc_component *c;
12352 gfc_symbol *parent = gfc_get_derived_super_type (derived);
12353
12354 if (parent)
12355 gfc_resolve_finalizers (parent, finalizable);
12356
12357 /* Return early when not finalizable. Additionally, ensure that derived-type
12358 components have a their finalizables resolved. */
12359 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
12360 {
12361 bool has_final = false;
12362 for (c = derived->components; c; c = c->next)
12363 if (c->ts.type == BT_DERIVED
12364 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
12365 {
12366 bool has_final2 = false;
12367 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final))
12368 return false; /* Error. */
12369 has_final = has_final || has_final2;
12370 }
12371 if (!has_final)
12372 {
12373 if (finalizable)
12374 *finalizable = false;
12375 return true;
12376 }
12377 }
12378
12379 /* Walk over the list of finalizer-procedures, check them, and if any one
12380 does not fit in with the standard's definition, print an error and remove
12381 it from the list. */
12382 prev_link = &derived->f2k_derived->finalizers;
12383 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
12384 {
12385 gfc_formal_arglist *dummy_args;
12386 gfc_symbol* arg;
12387 gfc_finalizer* i;
12388 int my_rank;
12389
12390 /* Skip this finalizer if we already resolved it. */
12391 if (list->proc_tree)
12392 {
12393 prev_link = &(list->next);
12394 continue;
12395 }
12396
12397 /* Check this exists and is a SUBROUTINE. */
12398 if (!list->proc_sym->attr.subroutine)
12399 {
12400 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
12401 list->proc_sym->name, &list->where);
12402 goto error;
12403 }
12404
12405 /* We should have exactly one argument. */
12406 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
12407 if (!dummy_args || dummy_args->next)
12408 {
12409 gfc_error ("FINAL procedure at %L must have exactly one argument",
12410 &list->where);
12411 goto error;
12412 }
12413 arg = dummy_args->sym;
12414
12415 /* This argument must be of our type. */
12416 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
12417 {
12418 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
12419 &arg->declared_at, derived->name);
12420 goto error;
12421 }
12422
12423 /* It must neither be a pointer nor allocatable nor optional. */
12424 if (arg->attr.pointer)
12425 {
12426 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
12427 &arg->declared_at);
12428 goto error;
12429 }
12430 if (arg->attr.allocatable)
12431 {
12432 gfc_error ("Argument of FINAL procedure at %L must not be"
12433 " ALLOCATABLE", &arg->declared_at);
12434 goto error;
12435 }
12436 if (arg->attr.optional)
12437 {
12438 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
12439 &arg->declared_at);
12440 goto error;
12441 }
12442
12443 /* It must not be INTENT(OUT). */
12444 if (arg->attr.intent == INTENT_OUT)
12445 {
12446 gfc_error ("Argument of FINAL procedure at %L must not be"
12447 " INTENT(OUT)", &arg->declared_at);
12448 goto error;
12449 }
12450
12451 /* Warn if the procedure is non-scalar and not assumed shape. */
12452 if (warn_surprising && arg->as && arg->as->rank != 0
12453 && arg->as->type != AS_ASSUMED_SHAPE)
12454 gfc_warning (OPT_Wsurprising,
12455 "Non-scalar FINAL procedure at %L should have assumed"
12456 " shape argument", &arg->declared_at);
12457
12458 /* Check that it does not match in kind and rank with a FINAL procedure
12459 defined earlier. To really loop over the *earlier* declarations,
12460 we need to walk the tail of the list as new ones were pushed at the
12461 front. */
12462 /* TODO: Handle kind parameters once they are implemented. */
12463 my_rank = (arg->as ? arg->as->rank : 0);
12464 for (i = list->next; i; i = i->next)
12465 {
12466 gfc_formal_arglist *dummy_args;
12467
12468 /* Argument list might be empty; that is an error signalled earlier,
12469 but we nevertheless continued resolving. */
12470 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
12471 if (dummy_args)
12472 {
12473 gfc_symbol* i_arg = dummy_args->sym;
12474 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
12475 if (i_rank == my_rank)
12476 {
12477 gfc_error ("FINAL procedure %qs declared at %L has the same"
12478 " rank (%d) as %qs",
12479 list->proc_sym->name, &list->where, my_rank,
12480 i->proc_sym->name);
12481 goto error;
12482 }
12483 }
12484 }
12485
12486 /* Is this the/a scalar finalizer procedure? */
12487 if (!arg->as || arg->as->rank == 0)
12488 seen_scalar = true;
12489
12490 /* Find the symtree for this procedure. */
12491 gcc_assert (!list->proc_tree);
12492 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
12493
12494 prev_link = &list->next;
12495 continue;
12496
12497 /* Remove wrong nodes immediately from the list so we don't risk any
12498 troubles in the future when they might fail later expectations. */
12499 error:
12500 i = list;
12501 *prev_link = list->next;
12502 gfc_free_finalizer (i);
12503 result = false;
12504 }
12505
12506 if (result == false)
12507 return false;
12508
12509 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
12510 were nodes in the list, must have been for arrays. It is surely a good
12511 idea to have a scalar version there if there's something to finalize. */
12512 if (warn_surprising && result && !seen_scalar)
12513 gfc_warning (OPT_Wsurprising,
12514 "Only array FINAL procedures declared for derived type %qs"
12515 " defined at %L, suggest also scalar one",
12516 derived->name, &derived->declared_at);
12517
12518 vtab = gfc_find_derived_vtab (derived);
12519 c = vtab->ts.u.derived->components->next->next->next->next->next;
12520 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
12521
12522 if (finalizable)
12523 *finalizable = true;
12524
12525 return true;
12526 }
12527
12528
12529 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
12530
12531 static bool
12532 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
12533 const char* generic_name, locus where)
12534 {
12535 gfc_symbol *sym1, *sym2;
12536 const char *pass1, *pass2;
12537 gfc_formal_arglist *dummy_args;
12538
12539 gcc_assert (t1->specific && t2->specific);
12540 gcc_assert (!t1->specific->is_generic);
12541 gcc_assert (!t2->specific->is_generic);
12542 gcc_assert (t1->is_operator == t2->is_operator);
12543
12544 sym1 = t1->specific->u.specific->n.sym;
12545 sym2 = t2->specific->u.specific->n.sym;
12546
12547 if (sym1 == sym2)
12548 return true;
12549
12550 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
12551 if (sym1->attr.subroutine != sym2->attr.subroutine
12552 || sym1->attr.function != sym2->attr.function)
12553 {
12554 gfc_error ("%qs and %qs can't be mixed FUNCTION/SUBROUTINE for"
12555 " GENERIC %qs at %L",
12556 sym1->name, sym2->name, generic_name, &where);
12557 return false;
12558 }
12559
12560 /* Determine PASS arguments. */
12561 if (t1->specific->nopass)
12562 pass1 = NULL;
12563 else if (t1->specific->pass_arg)
12564 pass1 = t1->specific->pass_arg;
12565 else
12566 {
12567 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
12568 if (dummy_args)
12569 pass1 = dummy_args->sym->name;
12570 else
12571 pass1 = NULL;
12572 }
12573 if (t2->specific->nopass)
12574 pass2 = NULL;
12575 else if (t2->specific->pass_arg)
12576 pass2 = t2->specific->pass_arg;
12577 else
12578 {
12579 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
12580 if (dummy_args)
12581 pass2 = dummy_args->sym->name;
12582 else
12583 pass2 = NULL;
12584 }
12585
12586 /* Compare the interfaces. */
12587 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
12588 NULL, 0, pass1, pass2))
12589 {
12590 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
12591 sym1->name, sym2->name, generic_name, &where);
12592 return false;
12593 }
12594
12595 return true;
12596 }
12597
12598
12599 /* Worker function for resolving a generic procedure binding; this is used to
12600 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
12601
12602 The difference between those cases is finding possible inherited bindings
12603 that are overridden, as one has to look for them in tb_sym_root,
12604 tb_uop_root or tb_op, respectively. Thus the caller must already find
12605 the super-type and set p->overridden correctly. */
12606
12607 static bool
12608 resolve_tb_generic_targets (gfc_symbol* super_type,
12609 gfc_typebound_proc* p, const char* name)
12610 {
12611 gfc_tbp_generic* target;
12612 gfc_symtree* first_target;
12613 gfc_symtree* inherited;
12614
12615 gcc_assert (p && p->is_generic);
12616
12617 /* Try to find the specific bindings for the symtrees in our target-list. */
12618 gcc_assert (p->u.generic);
12619 for (target = p->u.generic; target; target = target->next)
12620 if (!target->specific)
12621 {
12622 gfc_typebound_proc* overridden_tbp;
12623 gfc_tbp_generic* g;
12624 const char* target_name;
12625
12626 target_name = target->specific_st->name;
12627
12628 /* Defined for this type directly. */
12629 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
12630 {
12631 target->specific = target->specific_st->n.tb;
12632 goto specific_found;
12633 }
12634
12635 /* Look for an inherited specific binding. */
12636 if (super_type)
12637 {
12638 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
12639 true, NULL);
12640
12641 if (inherited)
12642 {
12643 gcc_assert (inherited->n.tb);
12644 target->specific = inherited->n.tb;
12645 goto specific_found;
12646 }
12647 }
12648
12649 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
12650 " at %L", target_name, name, &p->where);
12651 return false;
12652
12653 /* Once we've found the specific binding, check it is not ambiguous with
12654 other specifics already found or inherited for the same GENERIC. */
12655 specific_found:
12656 gcc_assert (target->specific);
12657
12658 /* This must really be a specific binding! */
12659 if (target->specific->is_generic)
12660 {
12661 gfc_error ("GENERIC %qs at %L must target a specific binding,"
12662 " %qs is GENERIC, too", name, &p->where, target_name);
12663 return false;
12664 }
12665
12666 /* Check those already resolved on this type directly. */
12667 for (g = p->u.generic; g; g = g->next)
12668 if (g != target && g->specific
12669 && !check_generic_tbp_ambiguity (target, g, name, p->where))
12670 return false;
12671
12672 /* Check for ambiguity with inherited specific targets. */
12673 for (overridden_tbp = p->overridden; overridden_tbp;
12674 overridden_tbp = overridden_tbp->overridden)
12675 if (overridden_tbp->is_generic)
12676 {
12677 for (g = overridden_tbp->u.generic; g; g = g->next)
12678 {
12679 gcc_assert (g->specific);
12680 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
12681 return false;
12682 }
12683 }
12684 }
12685
12686 /* If we attempt to "overwrite" a specific binding, this is an error. */
12687 if (p->overridden && !p->overridden->is_generic)
12688 {
12689 gfc_error ("GENERIC %qs at %L can't overwrite specific binding with"
12690 " the same name", name, &p->where);
12691 return false;
12692 }
12693
12694 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
12695 all must have the same attributes here. */
12696 first_target = p->u.generic->specific->u.specific;
12697 gcc_assert (first_target);
12698 p->subroutine = first_target->n.sym->attr.subroutine;
12699 p->function = first_target->n.sym->attr.function;
12700
12701 return true;
12702 }
12703
12704
12705 /* Resolve a GENERIC procedure binding for a derived type. */
12706
12707 static bool
12708 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
12709 {
12710 gfc_symbol* super_type;
12711
12712 /* Find the overridden binding if any. */
12713 st->n.tb->overridden = NULL;
12714 super_type = gfc_get_derived_super_type (derived);
12715 if (super_type)
12716 {
12717 gfc_symtree* overridden;
12718 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
12719 true, NULL);
12720
12721 if (overridden && overridden->n.tb)
12722 st->n.tb->overridden = overridden->n.tb;
12723 }
12724
12725 /* Resolve using worker function. */
12726 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
12727 }
12728
12729
12730 /* Retrieve the target-procedure of an operator binding and do some checks in
12731 common for intrinsic and user-defined type-bound operators. */
12732
12733 static gfc_symbol*
12734 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
12735 {
12736 gfc_symbol* target_proc;
12737
12738 gcc_assert (target->specific && !target->specific->is_generic);
12739 target_proc = target->specific->u.specific->n.sym;
12740 gcc_assert (target_proc);
12741
12742 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
12743 if (target->specific->nopass)
12744 {
12745 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
12746 return NULL;
12747 }
12748
12749 return target_proc;
12750 }
12751
12752
12753 /* Resolve a type-bound intrinsic operator. */
12754
12755 static bool
12756 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
12757 gfc_typebound_proc* p)
12758 {
12759 gfc_symbol* super_type;
12760 gfc_tbp_generic* target;
12761
12762 /* If there's already an error here, do nothing (but don't fail again). */
12763 if (p->error)
12764 return true;
12765
12766 /* Operators should always be GENERIC bindings. */
12767 gcc_assert (p->is_generic);
12768
12769 /* Look for an overridden binding. */
12770 super_type = gfc_get_derived_super_type (derived);
12771 if (super_type && super_type->f2k_derived)
12772 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
12773 op, true, NULL);
12774 else
12775 p->overridden = NULL;
12776
12777 /* Resolve general GENERIC properties using worker function. */
12778 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
12779 goto error;
12780
12781 /* Check the targets to be procedures of correct interface. */
12782 for (target = p->u.generic; target; target = target->next)
12783 {
12784 gfc_symbol* target_proc;
12785
12786 target_proc = get_checked_tb_operator_target (target, p->where);
12787 if (!target_proc)
12788 goto error;
12789
12790 if (!gfc_check_operator_interface (target_proc, op, p->where))
12791 goto error;
12792
12793 /* Add target to non-typebound operator list. */
12794 if (!target->specific->deferred && !derived->attr.use_assoc
12795 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
12796 {
12797 gfc_interface *head, *intr;
12798
12799 /* Preempt 'gfc_check_new_interface' for submodules, where the
12800 mechanism for handling module procedures winds up resolving
12801 operator interfaces twice and would otherwise cause an error. */
12802 for (intr = derived->ns->op[op]; intr; intr = intr->next)
12803 if (intr->sym == target_proc
12804 && target_proc->attr.used_in_submodule)
12805 return true;
12806
12807 if (!gfc_check_new_interface (derived->ns->op[op],
12808 target_proc, p->where))
12809 return false;
12810 head = derived->ns->op[op];
12811 intr = gfc_get_interface ();
12812 intr->sym = target_proc;
12813 intr->where = p->where;
12814 intr->next = head;
12815 derived->ns->op[op] = intr;
12816 }
12817 }
12818
12819 return true;
12820
12821 error:
12822 p->error = 1;
12823 return false;
12824 }
12825
12826
12827 /* Resolve a type-bound user operator (tree-walker callback). */
12828
12829 static gfc_symbol* resolve_bindings_derived;
12830 static bool resolve_bindings_result;
12831
12832 static bool check_uop_procedure (gfc_symbol* sym, locus where);
12833
12834 static void
12835 resolve_typebound_user_op (gfc_symtree* stree)
12836 {
12837 gfc_symbol* super_type;
12838 gfc_tbp_generic* target;
12839
12840 gcc_assert (stree && stree->n.tb);
12841
12842 if (stree->n.tb->error)
12843 return;
12844
12845 /* Operators should always be GENERIC bindings. */
12846 gcc_assert (stree->n.tb->is_generic);
12847
12848 /* Find overridden procedure, if any. */
12849 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
12850 if (super_type && super_type->f2k_derived)
12851 {
12852 gfc_symtree* overridden;
12853 overridden = gfc_find_typebound_user_op (super_type, NULL,
12854 stree->name, true, NULL);
12855
12856 if (overridden && overridden->n.tb)
12857 stree->n.tb->overridden = overridden->n.tb;
12858 }
12859 else
12860 stree->n.tb->overridden = NULL;
12861
12862 /* Resolve basically using worker function. */
12863 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
12864 goto error;
12865
12866 /* Check the targets to be functions of correct interface. */
12867 for (target = stree->n.tb->u.generic; target; target = target->next)
12868 {
12869 gfc_symbol* target_proc;
12870
12871 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
12872 if (!target_proc)
12873 goto error;
12874
12875 if (!check_uop_procedure (target_proc, stree->n.tb->where))
12876 goto error;
12877 }
12878
12879 return;
12880
12881 error:
12882 resolve_bindings_result = false;
12883 stree->n.tb->error = 1;
12884 }
12885
12886
12887 /* Resolve the type-bound procedures for a derived type. */
12888
12889 static void
12890 resolve_typebound_procedure (gfc_symtree* stree)
12891 {
12892 gfc_symbol* proc;
12893 locus where;
12894 gfc_symbol* me_arg;
12895 gfc_symbol* super_type;
12896 gfc_component* comp;
12897
12898 gcc_assert (stree);
12899
12900 /* Undefined specific symbol from GENERIC target definition. */
12901 if (!stree->n.tb)
12902 return;
12903
12904 if (stree->n.tb->error)
12905 return;
12906
12907 /* If this is a GENERIC binding, use that routine. */
12908 if (stree->n.tb->is_generic)
12909 {
12910 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
12911 goto error;
12912 return;
12913 }
12914
12915 /* Get the target-procedure to check it. */
12916 gcc_assert (!stree->n.tb->is_generic);
12917 gcc_assert (stree->n.tb->u.specific);
12918 proc = stree->n.tb->u.specific->n.sym;
12919 where = stree->n.tb->where;
12920
12921 /* Default access should already be resolved from the parser. */
12922 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
12923
12924 if (stree->n.tb->deferred)
12925 {
12926 if (!check_proc_interface (proc, &where))
12927 goto error;
12928 }
12929 else
12930 {
12931 /* Check for F08:C465. */
12932 if ((!proc->attr.subroutine && !proc->attr.function)
12933 || (proc->attr.proc != PROC_MODULE
12934 && proc->attr.if_source != IFSRC_IFBODY)
12935 || proc->attr.abstract)
12936 {
12937 gfc_error ("%qs must be a module procedure or an external procedure with"
12938 " an explicit interface at %L", proc->name, &where);
12939 goto error;
12940 }
12941 }
12942
12943 stree->n.tb->subroutine = proc->attr.subroutine;
12944 stree->n.tb->function = proc->attr.function;
12945
12946 /* Find the super-type of the current derived type. We could do this once and
12947 store in a global if speed is needed, but as long as not I believe this is
12948 more readable and clearer. */
12949 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
12950
12951 /* If PASS, resolve and check arguments if not already resolved / loaded
12952 from a .mod file. */
12953 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
12954 {
12955 gfc_formal_arglist *dummy_args;
12956
12957 dummy_args = gfc_sym_get_dummy_args (proc);
12958 if (stree->n.tb->pass_arg)
12959 {
12960 gfc_formal_arglist *i;
12961
12962 /* If an explicit passing argument name is given, walk the arg-list
12963 and look for it. */
12964
12965 me_arg = NULL;
12966 stree->n.tb->pass_arg_num = 1;
12967 for (i = dummy_args; i; i = i->next)
12968 {
12969 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
12970 {
12971 me_arg = i->sym;
12972 break;
12973 }
12974 ++stree->n.tb->pass_arg_num;
12975 }
12976
12977 if (!me_arg)
12978 {
12979 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
12980 " argument %qs",
12981 proc->name, stree->n.tb->pass_arg, &where,
12982 stree->n.tb->pass_arg);
12983 goto error;
12984 }
12985 }
12986 else
12987 {
12988 /* Otherwise, take the first one; there should in fact be at least
12989 one. */
12990 stree->n.tb->pass_arg_num = 1;
12991 if (!dummy_args)
12992 {
12993 gfc_error ("Procedure %qs with PASS at %L must have at"
12994 " least one argument", proc->name, &where);
12995 goto error;
12996 }
12997 me_arg = dummy_args->sym;
12998 }
12999
13000 /* Now check that the argument-type matches and the passed-object
13001 dummy argument is generally fine. */
13002
13003 gcc_assert (me_arg);
13004
13005 if (me_arg->ts.type != BT_CLASS)
13006 {
13007 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
13008 " at %L", proc->name, &where);
13009 goto error;
13010 }
13011
13012 if (CLASS_DATA (me_arg)->ts.u.derived
13013 != resolve_bindings_derived)
13014 {
13015 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
13016 " the derived-type %qs", me_arg->name, proc->name,
13017 me_arg->name, &where, resolve_bindings_derived->name);
13018 goto error;
13019 }
13020
13021 gcc_assert (me_arg->ts.type == BT_CLASS);
13022 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
13023 {
13024 gfc_error ("Passed-object dummy argument of %qs at %L must be"
13025 " scalar", proc->name, &where);
13026 goto error;
13027 }
13028 if (CLASS_DATA (me_arg)->attr.allocatable)
13029 {
13030 gfc_error ("Passed-object dummy argument of %qs at %L must not"
13031 " be ALLOCATABLE", proc->name, &where);
13032 goto error;
13033 }
13034 if (CLASS_DATA (me_arg)->attr.class_pointer)
13035 {
13036 gfc_error ("Passed-object dummy argument of %qs at %L must not"
13037 " be POINTER", proc->name, &where);
13038 goto error;
13039 }
13040 }
13041
13042 /* If we are extending some type, check that we don't override a procedure
13043 flagged NON_OVERRIDABLE. */
13044 stree->n.tb->overridden = NULL;
13045 if (super_type)
13046 {
13047 gfc_symtree* overridden;
13048 overridden = gfc_find_typebound_proc (super_type, NULL,
13049 stree->name, true, NULL);
13050
13051 if (overridden)
13052 {
13053 if (overridden->n.tb)
13054 stree->n.tb->overridden = overridden->n.tb;
13055
13056 if (!gfc_check_typebound_override (stree, overridden))
13057 goto error;
13058 }
13059 }
13060
13061 /* See if there's a name collision with a component directly in this type. */
13062 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
13063 if (!strcmp (comp->name, stree->name))
13064 {
13065 gfc_error ("Procedure %qs at %L has the same name as a component of"
13066 " %qs",
13067 stree->name, &where, resolve_bindings_derived->name);
13068 goto error;
13069 }
13070
13071 /* Try to find a name collision with an inherited component. */
13072 if (super_type && gfc_find_component (super_type, stree->name, true, true,
13073 NULL))
13074 {
13075 gfc_error ("Procedure %qs at %L has the same name as an inherited"
13076 " component of %qs",
13077 stree->name, &where, resolve_bindings_derived->name);
13078 goto error;
13079 }
13080
13081 stree->n.tb->error = 0;
13082 return;
13083
13084 error:
13085 resolve_bindings_result = false;
13086 stree->n.tb->error = 1;
13087 }
13088
13089
13090 static bool
13091 resolve_typebound_procedures (gfc_symbol* derived)
13092 {
13093 int op;
13094 gfc_symbol* super_type;
13095
13096 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
13097 return true;
13098
13099 super_type = gfc_get_derived_super_type (derived);
13100 if (super_type)
13101 resolve_symbol (super_type);
13102
13103 resolve_bindings_derived = derived;
13104 resolve_bindings_result = true;
13105
13106 if (derived->f2k_derived->tb_sym_root)
13107 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
13108 &resolve_typebound_procedure);
13109
13110 if (derived->f2k_derived->tb_uop_root)
13111 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
13112 &resolve_typebound_user_op);
13113
13114 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
13115 {
13116 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
13117 if (p && !resolve_typebound_intrinsic_op (derived,
13118 (gfc_intrinsic_op)op, p))
13119 resolve_bindings_result = false;
13120 }
13121
13122 return resolve_bindings_result;
13123 }
13124
13125
13126 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
13127 to give all identical derived types the same backend_decl. */
13128 static void
13129 add_dt_to_dt_list (gfc_symbol *derived)
13130 {
13131 gfc_dt_list *dt_list;
13132
13133 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
13134 if (derived == dt_list->derived)
13135 return;
13136
13137 dt_list = gfc_get_dt_list ();
13138 dt_list->next = gfc_derived_types;
13139 dt_list->derived = derived;
13140 gfc_derived_types = dt_list;
13141 }
13142
13143
13144 /* Ensure that a derived-type is really not abstract, meaning that every
13145 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
13146
13147 static bool
13148 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
13149 {
13150 if (!st)
13151 return true;
13152
13153 if (!ensure_not_abstract_walker (sub, st->left))
13154 return false;
13155 if (!ensure_not_abstract_walker (sub, st->right))
13156 return false;
13157
13158 if (st->n.tb && st->n.tb->deferred)
13159 {
13160 gfc_symtree* overriding;
13161 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
13162 if (!overriding)
13163 return false;
13164 gcc_assert (overriding->n.tb);
13165 if (overriding->n.tb->deferred)
13166 {
13167 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
13168 " %qs is DEFERRED and not overridden",
13169 sub->name, &sub->declared_at, st->name);
13170 return false;
13171 }
13172 }
13173
13174 return true;
13175 }
13176
13177 static bool
13178 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
13179 {
13180 /* The algorithm used here is to recursively travel up the ancestry of sub
13181 and for each ancestor-type, check all bindings. If any of them is
13182 DEFERRED, look it up starting from sub and see if the found (overriding)
13183 binding is not DEFERRED.
13184 This is not the most efficient way to do this, but it should be ok and is
13185 clearer than something sophisticated. */
13186
13187 gcc_assert (ancestor && !sub->attr.abstract);
13188
13189 if (!ancestor->attr.abstract)
13190 return true;
13191
13192 /* Walk bindings of this ancestor. */
13193 if (ancestor->f2k_derived)
13194 {
13195 bool t;
13196 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
13197 if (!t)
13198 return false;
13199 }
13200
13201 /* Find next ancestor type and recurse on it. */
13202 ancestor = gfc_get_derived_super_type (ancestor);
13203 if (ancestor)
13204 return ensure_not_abstract (sub, ancestor);
13205
13206 return true;
13207 }
13208
13209
13210 /* This check for typebound defined assignments is done recursively
13211 since the order in which derived types are resolved is not always in
13212 order of the declarations. */
13213
13214 static void
13215 check_defined_assignments (gfc_symbol *derived)
13216 {
13217 gfc_component *c;
13218
13219 for (c = derived->components; c; c = c->next)
13220 {
13221 if (!gfc_bt_struct (c->ts.type)
13222 || c->attr.pointer
13223 || c->attr.allocatable
13224 || c->attr.proc_pointer_comp
13225 || c->attr.class_pointer
13226 || c->attr.proc_pointer)
13227 continue;
13228
13229 if (c->ts.u.derived->attr.defined_assign_comp
13230 || (c->ts.u.derived->f2k_derived
13231 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
13232 {
13233 derived->attr.defined_assign_comp = 1;
13234 return;
13235 }
13236
13237 check_defined_assignments (c->ts.u.derived);
13238 if (c->ts.u.derived->attr.defined_assign_comp)
13239 {
13240 derived->attr.defined_assign_comp = 1;
13241 return;
13242 }
13243 }
13244 }
13245
13246
13247 /* Resolve a single component of a derived type or structure. */
13248
13249 static bool
13250 resolve_component (gfc_component *c, gfc_symbol *sym)
13251 {
13252 gfc_symbol *super_type;
13253
13254 if (c->attr.artificial)
13255 return true;
13256
13257 /* F2008, C442. */
13258 if ((!sym->attr.is_class || c != sym->components)
13259 && c->attr.codimension
13260 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
13261 {
13262 gfc_error ("Coarray component %qs at %L must be allocatable with "
13263 "deferred shape", c->name, &c->loc);
13264 return false;
13265 }
13266
13267 /* F2008, C443. */
13268 if (c->attr.codimension && c->ts.type == BT_DERIVED
13269 && c->ts.u.derived->ts.is_iso_c)
13270 {
13271 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
13272 "shall not be a coarray", c->name, &c->loc);
13273 return false;
13274 }
13275
13276 /* F2008, C444. */
13277 if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
13278 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
13279 || c->attr.allocatable))
13280 {
13281 gfc_error ("Component %qs at %L with coarray component "
13282 "shall be a nonpointer, nonallocatable scalar",
13283 c->name, &c->loc);
13284 return false;
13285 }
13286
13287 /* F2008, C448. */
13288 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
13289 {
13290 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
13291 "is not an array pointer", c->name, &c->loc);
13292 return false;
13293 }
13294
13295 if (c->attr.proc_pointer && c->ts.interface)
13296 {
13297 gfc_symbol *ifc = c->ts.interface;
13298
13299 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
13300 {
13301 c->tb->error = 1;
13302 return false;
13303 }
13304
13305 if (ifc->attr.if_source || ifc->attr.intrinsic)
13306 {
13307 /* Resolve interface and copy attributes. */
13308 if (ifc->formal && !ifc->formal_ns)
13309 resolve_symbol (ifc);
13310 if (ifc->attr.intrinsic)
13311 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
13312
13313 if (ifc->result)
13314 {
13315 c->ts = ifc->result->ts;
13316 c->attr.allocatable = ifc->result->attr.allocatable;
13317 c->attr.pointer = ifc->result->attr.pointer;
13318 c->attr.dimension = ifc->result->attr.dimension;
13319 c->as = gfc_copy_array_spec (ifc->result->as);
13320 c->attr.class_ok = ifc->result->attr.class_ok;
13321 }
13322 else
13323 {
13324 c->ts = ifc->ts;
13325 c->attr.allocatable = ifc->attr.allocatable;
13326 c->attr.pointer = ifc->attr.pointer;
13327 c->attr.dimension = ifc->attr.dimension;
13328 c->as = gfc_copy_array_spec (ifc->as);
13329 c->attr.class_ok = ifc->attr.class_ok;
13330 }
13331 c->ts.interface = ifc;
13332 c->attr.function = ifc->attr.function;
13333 c->attr.subroutine = ifc->attr.subroutine;
13334
13335 c->attr.pure = ifc->attr.pure;
13336 c->attr.elemental = ifc->attr.elemental;
13337 c->attr.recursive = ifc->attr.recursive;
13338 c->attr.always_explicit = ifc->attr.always_explicit;
13339 c->attr.ext_attr |= ifc->attr.ext_attr;
13340 /* Copy char length. */
13341 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
13342 {
13343 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
13344 if (cl->length && !cl->resolved
13345 && !gfc_resolve_expr (cl->length))
13346 {
13347 c->tb->error = 1;
13348 return false;
13349 }
13350 c->ts.u.cl = cl;
13351 }
13352 }
13353 }
13354 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
13355 {
13356 /* Since PPCs are not implicitly typed, a PPC without an explicit
13357 interface must be a subroutine. */
13358 gfc_add_subroutine (&c->attr, c->name, &c->loc);
13359 }
13360
13361 /* Procedure pointer components: Check PASS arg. */
13362 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
13363 && !sym->attr.vtype)
13364 {
13365 gfc_symbol* me_arg;
13366
13367 if (c->tb->pass_arg)
13368 {
13369 gfc_formal_arglist* i;
13370
13371 /* If an explicit passing argument name is given, walk the arg-list
13372 and look for it. */
13373
13374 me_arg = NULL;
13375 c->tb->pass_arg_num = 1;
13376 for (i = c->ts.interface->formal; i; i = i->next)
13377 {
13378 if (!strcmp (i->sym->name, c->tb->pass_arg))
13379 {
13380 me_arg = i->sym;
13381 break;
13382 }
13383 c->tb->pass_arg_num++;
13384 }
13385
13386 if (!me_arg)
13387 {
13388 gfc_error ("Procedure pointer component %qs with PASS(%s) "
13389 "at %L has no argument %qs", c->name,
13390 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
13391 c->tb->error = 1;
13392 return false;
13393 }
13394 }
13395 else
13396 {
13397 /* Otherwise, take the first one; there should in fact be at least
13398 one. */
13399 c->tb->pass_arg_num = 1;
13400 if (!c->ts.interface->formal)
13401 {
13402 gfc_error ("Procedure pointer component %qs with PASS at %L "
13403 "must have at least one argument",
13404 c->name, &c->loc);
13405 c->tb->error = 1;
13406 return false;
13407 }
13408 me_arg = c->ts.interface->formal->sym;
13409 }
13410
13411 /* Now check that the argument-type matches. */
13412 gcc_assert (me_arg);
13413 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
13414 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
13415 || (me_arg->ts.type == BT_CLASS
13416 && CLASS_DATA (me_arg)->ts.u.derived != sym))
13417 {
13418 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
13419 " the derived type %qs", me_arg->name, c->name,
13420 me_arg->name, &c->loc, sym->name);
13421 c->tb->error = 1;
13422 return false;
13423 }
13424
13425 /* Check for C453. */
13426 if (me_arg->attr.dimension)
13427 {
13428 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13429 "must be scalar", me_arg->name, c->name, me_arg->name,
13430 &c->loc);
13431 c->tb->error = 1;
13432 return false;
13433 }
13434
13435 if (me_arg->attr.pointer)
13436 {
13437 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13438 "may not have the POINTER attribute", me_arg->name,
13439 c->name, me_arg->name, &c->loc);
13440 c->tb->error = 1;
13441 return false;
13442 }
13443
13444 if (me_arg->attr.allocatable)
13445 {
13446 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
13447 "may not be ALLOCATABLE", me_arg->name, c->name,
13448 me_arg->name, &c->loc);
13449 c->tb->error = 1;
13450 return false;
13451 }
13452
13453 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
13454 {
13455 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
13456 " at %L", c->name, &c->loc);
13457 return false;
13458 }
13459
13460 }
13461
13462 /* Check type-spec if this is not the parent-type component. */
13463 if (((sym->attr.is_class
13464 && (!sym->components->ts.u.derived->attr.extension
13465 || c != sym->components->ts.u.derived->components))
13466 || (!sym->attr.is_class
13467 && (!sym->attr.extension || c != sym->components)))
13468 && !sym->attr.vtype
13469 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
13470 return false;
13471
13472 super_type = gfc_get_derived_super_type (sym);
13473
13474 /* If this type is an extension, set the accessibility of the parent
13475 component. */
13476 if (super_type
13477 && ((sym->attr.is_class
13478 && c == sym->components->ts.u.derived->components)
13479 || (!sym->attr.is_class && c == sym->components))
13480 && strcmp (super_type->name, c->name) == 0)
13481 c->attr.access = super_type->attr.access;
13482
13483 /* If this type is an extension, see if this component has the same name
13484 as an inherited type-bound procedure. */
13485 if (super_type && !sym->attr.is_class
13486 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
13487 {
13488 gfc_error ("Component %qs of %qs at %L has the same name as an"
13489 " inherited type-bound procedure",
13490 c->name, sym->name, &c->loc);
13491 return false;
13492 }
13493
13494 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
13495 && !c->ts.deferred)
13496 {
13497 if (c->ts.u.cl->length == NULL
13498 || (!resolve_charlen(c->ts.u.cl))
13499 || !gfc_is_constant_expr (c->ts.u.cl->length))
13500 {
13501 gfc_error ("Character length of component %qs needs to "
13502 "be a constant specification expression at %L",
13503 c->name,
13504 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
13505 return false;
13506 }
13507 }
13508
13509 if (c->ts.type == BT_CHARACTER && c->ts.deferred
13510 && !c->attr.pointer && !c->attr.allocatable)
13511 {
13512 gfc_error ("Character component %qs of %qs at %L with deferred "
13513 "length must be a POINTER or ALLOCATABLE",
13514 c->name, sym->name, &c->loc);
13515 return false;
13516 }
13517
13518 /* Add the hidden deferred length field. */
13519 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function
13520 && !sym->attr.is_class)
13521 {
13522 char name[GFC_MAX_SYMBOL_LEN+9];
13523 gfc_component *strlen;
13524 sprintf (name, "_%s_length", c->name);
13525 strlen = gfc_find_component (sym, name, true, true, NULL);
13526 if (strlen == NULL)
13527 {
13528 if (!gfc_add_component (sym, name, &strlen))
13529 return false;
13530 strlen->ts.type = BT_INTEGER;
13531 strlen->ts.kind = gfc_charlen_int_kind;
13532 strlen->attr.access = ACCESS_PRIVATE;
13533 strlen->attr.artificial = 1;
13534 }
13535 }
13536
13537 if (c->ts.type == BT_DERIVED
13538 && sym->component_access != ACCESS_PRIVATE
13539 && gfc_check_symbol_access (sym)
13540 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
13541 && !c->ts.u.derived->attr.use_assoc
13542 && !gfc_check_symbol_access (c->ts.u.derived)
13543 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
13544 "PRIVATE type and cannot be a component of "
13545 "%qs, which is PUBLIC at %L", c->name,
13546 sym->name, &sym->declared_at))
13547 return false;
13548
13549 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
13550 {
13551 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
13552 "type %s", c->name, &c->loc, sym->name);
13553 return false;
13554 }
13555
13556 if (sym->attr.sequence)
13557 {
13558 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
13559 {
13560 gfc_error ("Component %s of SEQUENCE type declared at %L does "
13561 "not have the SEQUENCE attribute",
13562 c->ts.u.derived->name, &sym->declared_at);
13563 return false;
13564 }
13565 }
13566
13567 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
13568 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
13569 else if (c->ts.type == BT_CLASS && c->attr.class_ok
13570 && CLASS_DATA (c)->ts.u.derived->attr.generic)
13571 CLASS_DATA (c)->ts.u.derived
13572 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
13573
13574 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
13575 && c->attr.pointer && c->ts.u.derived->components == NULL
13576 && !c->ts.u.derived->attr.zero_comp)
13577 {
13578 gfc_error ("The pointer component %qs of %qs at %L is a type "
13579 "that has not been declared", c->name, sym->name,
13580 &c->loc);
13581 return false;
13582 }
13583
13584 if (c->ts.type == BT_CLASS && c->attr.class_ok
13585 && CLASS_DATA (c)->attr.class_pointer
13586 && CLASS_DATA (c)->ts.u.derived->components == NULL
13587 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
13588 && !UNLIMITED_POLY (c))
13589 {
13590 gfc_error ("The pointer component %qs of %qs at %L is a type "
13591 "that has not been declared", c->name, sym->name,
13592 &c->loc);
13593 return false;
13594 }
13595
13596 /* If an allocatable component derived type is of the same type as
13597 the enclosing derived type, we need a vtable generating so that
13598 the __deallocate procedure is created. */
13599 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
13600 && c->ts.u.derived == sym && c->attr.allocatable == 1)
13601 gfc_find_vtab (&c->ts);
13602
13603 /* Ensure that all the derived type components are put on the
13604 derived type list; even in formal namespaces, where derived type
13605 pointer components might not have been declared. */
13606 if (c->ts.type == BT_DERIVED
13607 && c->ts.u.derived
13608 && c->ts.u.derived->components
13609 && c->attr.pointer
13610 && sym != c->ts.u.derived)
13611 add_dt_to_dt_list (c->ts.u.derived);
13612
13613 if (!gfc_resolve_array_spec (c->as,
13614 !(c->attr.pointer || c->attr.proc_pointer
13615 || c->attr.allocatable)))
13616 return false;
13617
13618 if (c->initializer && !sym->attr.vtype
13619 && !gfc_check_assign_symbol (sym, c, c->initializer))
13620 return false;
13621
13622 return true;
13623 }
13624
13625
13626 /* Be nice about the locus for a structure expression - show the locus of the
13627 first non-null sub-expression if we can. */
13628
13629 static locus *
13630 cons_where (gfc_expr *struct_expr)
13631 {
13632 gfc_constructor *cons;
13633
13634 gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
13635
13636 cons = gfc_constructor_first (struct_expr->value.constructor);
13637 for (; cons; cons = gfc_constructor_next (cons))
13638 {
13639 if (cons->expr && cons->expr->expr_type != EXPR_NULL)
13640 return &cons->expr->where;
13641 }
13642
13643 return &struct_expr->where;
13644 }
13645
13646 /* Resolve the components of a structure type. Much less work than derived
13647 types. */
13648
13649 static bool
13650 resolve_fl_struct (gfc_symbol *sym)
13651 {
13652 gfc_component *c;
13653 gfc_expr *init = NULL;
13654 bool success;
13655
13656 /* Make sure UNIONs do not have overlapping initializers. */
13657 if (sym->attr.flavor == FL_UNION)
13658 {
13659 for (c = sym->components; c; c = c->next)
13660 {
13661 if (init && c->initializer)
13662 {
13663 gfc_error ("Conflicting initializers in union at %L and %L",
13664 cons_where (init), cons_where (c->initializer));
13665 gfc_free_expr (c->initializer);
13666 c->initializer = NULL;
13667 }
13668 if (init == NULL)
13669 init = c->initializer;
13670 }
13671 }
13672
13673 success = true;
13674 for (c = sym->components; c; c = c->next)
13675 if (!resolve_component (c, sym))
13676 success = false;
13677
13678 if (!success)
13679 return false;
13680
13681 if (sym->components)
13682 add_dt_to_dt_list (sym);
13683
13684 return true;
13685 }
13686
13687
13688 /* Resolve the components of a derived type. This does not have to wait until
13689 resolution stage, but can be done as soon as the dt declaration has been
13690 parsed. */
13691
13692 static bool
13693 resolve_fl_derived0 (gfc_symbol *sym)
13694 {
13695 gfc_symbol* super_type;
13696 gfc_component *c;
13697 bool success;
13698
13699 if (sym->attr.unlimited_polymorphic)
13700 return true;
13701
13702 super_type = gfc_get_derived_super_type (sym);
13703
13704 /* F2008, C432. */
13705 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
13706 {
13707 gfc_error ("As extending type %qs at %L has a coarray component, "
13708 "parent type %qs shall also have one", sym->name,
13709 &sym->declared_at, super_type->name);
13710 return false;
13711 }
13712
13713 /* Ensure the extended type gets resolved before we do. */
13714 if (super_type && !resolve_fl_derived0 (super_type))
13715 return false;
13716
13717 /* An ABSTRACT type must be extensible. */
13718 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
13719 {
13720 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
13721 sym->name, &sym->declared_at);
13722 return false;
13723 }
13724
13725 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
13726 : sym->components;
13727
13728 success = true;
13729 for ( ; c != NULL; c = c->next)
13730 if (!resolve_component (c, sym))
13731 success = false;
13732
13733 if (!success)
13734 return false;
13735
13736 check_defined_assignments (sym);
13737
13738 if (!sym->attr.defined_assign_comp && super_type)
13739 sym->attr.defined_assign_comp
13740 = super_type->attr.defined_assign_comp;
13741
13742 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
13743 all DEFERRED bindings are overridden. */
13744 if (super_type && super_type->attr.abstract && !sym->attr.abstract
13745 && !sym->attr.is_class
13746 && !ensure_not_abstract (sym, super_type))
13747 return false;
13748
13749 /* Add derived type to the derived type list. */
13750 add_dt_to_dt_list (sym);
13751
13752 return true;
13753 }
13754
13755
13756 /* The following procedure does the full resolution of a derived type,
13757 including resolution of all type-bound procedures (if present). In contrast
13758 to 'resolve_fl_derived0' this can only be done after the module has been
13759 parsed completely. */
13760
13761 static bool
13762 resolve_fl_derived (gfc_symbol *sym)
13763 {
13764 gfc_symbol *gen_dt = NULL;
13765
13766 if (sym->attr.unlimited_polymorphic)
13767 return true;
13768
13769 if (!sym->attr.is_class)
13770 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
13771 if (gen_dt && gen_dt->generic && gen_dt->generic->next
13772 && (!gen_dt->generic->sym->attr.use_assoc
13773 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
13774 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
13775 "%qs at %L being the same name as derived "
13776 "type at %L", sym->name,
13777 gen_dt->generic->sym == sym
13778 ? gen_dt->generic->next->sym->name
13779 : gen_dt->generic->sym->name,
13780 gen_dt->generic->sym == sym
13781 ? &gen_dt->generic->next->sym->declared_at
13782 : &gen_dt->generic->sym->declared_at,
13783 &sym->declared_at))
13784 return false;
13785
13786 /* Resolve the finalizer procedures. */
13787 if (!gfc_resolve_finalizers (sym, NULL))
13788 return false;
13789
13790 if (sym->attr.is_class && sym->ts.u.derived == NULL)
13791 {
13792 /* Fix up incomplete CLASS symbols. */
13793 gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
13794 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
13795
13796 /* Nothing more to do for unlimited polymorphic entities. */
13797 if (data->ts.u.derived->attr.unlimited_polymorphic)
13798 return true;
13799 else if (vptr->ts.u.derived == NULL)
13800 {
13801 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
13802 gcc_assert (vtab);
13803 vptr->ts.u.derived = vtab->ts.u.derived;
13804 }
13805 }
13806
13807 if (!resolve_fl_derived0 (sym))
13808 return false;
13809
13810 /* Resolve the type-bound procedures. */
13811 if (!resolve_typebound_procedures (sym))
13812 return false;
13813
13814 return true;
13815 }
13816
13817
13818 /* Check for formatted read and write DTIO procedures. */
13819
13820 static bool
13821 dtio_procs_present (gfc_symbol *sym)
13822 {
13823 gfc_symbol *derived;
13824
13825 if (sym->ts.type == BT_CLASS)
13826 derived = CLASS_DATA (sym)->ts.u.derived;
13827 else if (sym->ts.type == BT_DERIVED)
13828 derived = sym->ts.u.derived;
13829 else
13830 return false;
13831
13832 return gfc_find_specific_dtio_proc (derived, true, true) != NULL
13833 && gfc_find_specific_dtio_proc (derived, false, true) != NULL;
13834 }
13835
13836
13837 static bool
13838 resolve_fl_namelist (gfc_symbol *sym)
13839 {
13840 gfc_namelist *nl;
13841 gfc_symbol *nlsym;
13842 bool dtio;
13843
13844 for (nl = sym->namelist; nl; nl = nl->next)
13845 {
13846 /* Check again, the check in match only works if NAMELIST comes
13847 after the decl. */
13848 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
13849 {
13850 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
13851 "allowed", nl->sym->name, sym->name, &sym->declared_at);
13852 return false;
13853 }
13854
13855 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
13856 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
13857 "with assumed shape in namelist %qs at %L",
13858 nl->sym->name, sym->name, &sym->declared_at))
13859 return false;
13860
13861 if (is_non_constant_shape_array (nl->sym)
13862 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
13863 "with nonconstant shape in namelist %qs at %L",
13864 nl->sym->name, sym->name, &sym->declared_at))
13865 return false;
13866
13867 if (nl->sym->ts.type == BT_CHARACTER
13868 && (nl->sym->ts.u.cl->length == NULL
13869 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
13870 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
13871 "nonconstant character length in "
13872 "namelist %qs at %L", nl->sym->name,
13873 sym->name, &sym->declared_at))
13874 return false;
13875
13876 dtio = dtio_procs_present (nl->sym);
13877
13878 if (nl->sym->ts.type == BT_CLASS && !dtio)
13879 {
13880 gfc_error ("NAMELIST object %qs in namelist %qs at %L is "
13881 "polymorphic and requires a defined input/output "
13882 "procedure", nl->sym->name, sym->name, &sym->declared_at);
13883 return false;
13884 }
13885
13886 if (nl->sym->ts.type == BT_DERIVED
13887 && (nl->sym->ts.u.derived->attr.alloc_comp
13888 || nl->sym->ts.u.derived->attr.pointer_comp))
13889 {
13890 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
13891 "namelist %qs at %L with ALLOCATABLE "
13892 "or POINTER components", nl->sym->name,
13893 sym->name, &sym->declared_at))
13894 return false;
13895
13896 if (!dtio)
13897 {
13898 gfc_error ("NAMELIST object %qs in namelist %qs at %L has "
13899 "ALLOCATABLE or POINTER components and thus requires "
13900 "a defined input/output procedure", nl->sym->name,
13901 sym->name, &sym->declared_at);
13902 return false;
13903 }
13904 }
13905 }
13906
13907 /* Reject PRIVATE objects in a PUBLIC namelist. */
13908 if (gfc_check_symbol_access (sym))
13909 {
13910 for (nl = sym->namelist; nl; nl = nl->next)
13911 {
13912 if (!nl->sym->attr.use_assoc
13913 && !is_sym_host_assoc (nl->sym, sym->ns)
13914 && !gfc_check_symbol_access (nl->sym))
13915 {
13916 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
13917 "cannot be member of PUBLIC namelist %qs at %L",
13918 nl->sym->name, sym->name, &sym->declared_at);
13919 return false;
13920 }
13921
13922 /* If the derived type has specific DTIO procedures for both read and
13923 write then namelist objects with private components are OK. */
13924 if (dtio_procs_present (nl->sym))
13925 continue;
13926
13927 /* Types with private components that came here by USE-association. */
13928 if (nl->sym->ts.type == BT_DERIVED
13929 && derived_inaccessible (nl->sym->ts.u.derived))
13930 {
13931 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
13932 "components and cannot be member of namelist %qs at %L",
13933 nl->sym->name, sym->name, &sym->declared_at);
13934 return false;
13935 }
13936
13937 /* Types with private components that are defined in the same module. */
13938 if (nl->sym->ts.type == BT_DERIVED
13939 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
13940 && nl->sym->ts.u.derived->attr.private_comp)
13941 {
13942 gfc_error ("NAMELIST object %qs has PRIVATE components and "
13943 "cannot be a member of PUBLIC namelist %qs at %L",
13944 nl->sym->name, sym->name, &sym->declared_at);
13945 return false;
13946 }
13947 }
13948 }
13949
13950
13951 /* 14.1.2 A module or internal procedure represent local entities
13952 of the same type as a namelist member and so are not allowed. */
13953 for (nl = sym->namelist; nl; nl = nl->next)
13954 {
13955 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
13956 continue;
13957
13958 if (nl->sym->attr.function && nl->sym == nl->sym->result)
13959 if ((nl->sym == sym->ns->proc_name)
13960 ||
13961 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
13962 continue;
13963
13964 nlsym = NULL;
13965 if (nl->sym->name)
13966 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
13967 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
13968 {
13969 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
13970 "attribute in %qs at %L", nlsym->name,
13971 &sym->declared_at);
13972 return false;
13973 }
13974 }
13975
13976 return true;
13977 }
13978
13979
13980 static bool
13981 resolve_fl_parameter (gfc_symbol *sym)
13982 {
13983 /* A parameter array's shape needs to be constant. */
13984 if (sym->as != NULL
13985 && (sym->as->type == AS_DEFERRED
13986 || is_non_constant_shape_array (sym)))
13987 {
13988 gfc_error ("Parameter array %qs at %L cannot be automatic "
13989 "or of deferred shape", sym->name, &sym->declared_at);
13990 return false;
13991 }
13992
13993 /* Constraints on deferred type parameter. */
13994 if (!deferred_requirements (sym))
13995 return false;
13996
13997 /* Make sure a parameter that has been implicitly typed still
13998 matches the implicit type, since PARAMETER statements can precede
13999 IMPLICIT statements. */
14000 if (sym->attr.implicit_type
14001 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
14002 sym->ns)))
14003 {
14004 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
14005 "later IMPLICIT type", sym->name, &sym->declared_at);
14006 return false;
14007 }
14008
14009 /* Make sure the types of derived parameters are consistent. This
14010 type checking is deferred until resolution because the type may
14011 refer to a derived type from the host. */
14012 if (sym->ts.type == BT_DERIVED
14013 && !gfc_compare_types (&sym->ts, &sym->value->ts))
14014 {
14015 gfc_error ("Incompatible derived type in PARAMETER at %L",
14016 &sym->value->where);
14017 return false;
14018 }
14019
14020 /* F03:C509,C514. */
14021 if (sym->ts.type == BT_CLASS)
14022 {
14023 gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
14024 sym->name, &sym->declared_at);
14025 return false;
14026 }
14027
14028 return true;
14029 }
14030
14031
14032 /* Do anything necessary to resolve a symbol. Right now, we just
14033 assume that an otherwise unknown symbol is a variable. This sort
14034 of thing commonly happens for symbols in module. */
14035
14036 static void
14037 resolve_symbol (gfc_symbol *sym)
14038 {
14039 int check_constant, mp_flag;
14040 gfc_symtree *symtree;
14041 gfc_symtree *this_symtree;
14042 gfc_namespace *ns;
14043 gfc_component *c;
14044 symbol_attribute class_attr;
14045 gfc_array_spec *as;
14046 bool saved_specification_expr;
14047
14048 if (sym->resolved)
14049 return;
14050 sym->resolved = 1;
14051
14052 /* No symbol will ever have union type; only components can be unions.
14053 Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
14054 (just like derived type declaration symbols have flavor FL_DERIVED). */
14055 gcc_assert (sym->ts.type != BT_UNION);
14056
14057 /* Coarrayed polymorphic objects with allocatable or pointer components are
14058 yet unsupported for -fcoarray=lib. */
14059 if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
14060 && sym->ts.u.derived && CLASS_DATA (sym)
14061 && CLASS_DATA (sym)->attr.codimension
14062 && (sym->ts.u.derived->attr.alloc_comp
14063 || sym->ts.u.derived->attr.pointer_comp))
14064 {
14065 gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
14066 "type coarrays at %L are unsupported", &sym->declared_at);
14067 return;
14068 }
14069
14070 if (sym->attr.artificial)
14071 return;
14072
14073 if (sym->attr.unlimited_polymorphic)
14074 return;
14075
14076 if (sym->attr.flavor == FL_UNKNOWN
14077 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
14078 && !sym->attr.generic && !sym->attr.external
14079 && sym->attr.if_source == IFSRC_UNKNOWN
14080 && sym->ts.type == BT_UNKNOWN))
14081 {
14082
14083 /* If we find that a flavorless symbol is an interface in one of the
14084 parent namespaces, find its symtree in this namespace, free the
14085 symbol and set the symtree to point to the interface symbol. */
14086 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
14087 {
14088 symtree = gfc_find_symtree (ns->sym_root, sym->name);
14089 if (symtree && (symtree->n.sym->generic ||
14090 (symtree->n.sym->attr.flavor == FL_PROCEDURE
14091 && sym->ns->construct_entities)))
14092 {
14093 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
14094 sym->name);
14095 if (this_symtree->n.sym == sym)
14096 {
14097 symtree->n.sym->refs++;
14098 gfc_release_symbol (sym);
14099 this_symtree->n.sym = symtree->n.sym;
14100 return;
14101 }
14102 }
14103 }
14104
14105 /* Otherwise give it a flavor according to such attributes as
14106 it has. */
14107 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
14108 && sym->attr.intrinsic == 0)
14109 sym->attr.flavor = FL_VARIABLE;
14110 else if (sym->attr.flavor == FL_UNKNOWN)
14111 {
14112 sym->attr.flavor = FL_PROCEDURE;
14113 if (sym->attr.dimension)
14114 sym->attr.function = 1;
14115 }
14116 }
14117
14118 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
14119 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
14120
14121 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
14122 && !resolve_procedure_interface (sym))
14123 return;
14124
14125 if (sym->attr.is_protected && !sym->attr.proc_pointer
14126 && (sym->attr.procedure || sym->attr.external))
14127 {
14128 if (sym->attr.external)
14129 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
14130 "at %L", &sym->declared_at);
14131 else
14132 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
14133 "at %L", &sym->declared_at);
14134
14135 return;
14136 }
14137
14138 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
14139 return;
14140
14141 else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
14142 && !resolve_fl_struct (sym))
14143 return;
14144
14145 /* Symbols that are module procedures with results (functions) have
14146 the types and array specification copied for type checking in
14147 procedures that call them, as well as for saving to a module
14148 file. These symbols can't stand the scrutiny that their results
14149 can. */
14150 mp_flag = (sym->result != NULL && sym->result != sym);
14151
14152 /* Make sure that the intrinsic is consistent with its internal
14153 representation. This needs to be done before assigning a default
14154 type to avoid spurious warnings. */
14155 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
14156 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
14157 return;
14158
14159 /* Resolve associate names. */
14160 if (sym->assoc)
14161 resolve_assoc_var (sym, true);
14162
14163 /* Assign default type to symbols that need one and don't have one. */
14164 if (sym->ts.type == BT_UNKNOWN)
14165 {
14166 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
14167 {
14168 gfc_set_default_type (sym, 1, NULL);
14169 }
14170
14171 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
14172 && !sym->attr.function && !sym->attr.subroutine
14173 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
14174 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
14175
14176 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
14177 {
14178 /* The specific case of an external procedure should emit an error
14179 in the case that there is no implicit type. */
14180 if (!mp_flag)
14181 {
14182 if (!sym->attr.mixed_entry_master)
14183 gfc_set_default_type (sym, sym->attr.external, NULL);
14184 }
14185 else
14186 {
14187 /* Result may be in another namespace. */
14188 resolve_symbol (sym->result);
14189
14190 if (!sym->result->attr.proc_pointer)
14191 {
14192 sym->ts = sym->result->ts;
14193 sym->as = gfc_copy_array_spec (sym->result->as);
14194 sym->attr.dimension = sym->result->attr.dimension;
14195 sym->attr.pointer = sym->result->attr.pointer;
14196 sym->attr.allocatable = sym->result->attr.allocatable;
14197 sym->attr.contiguous = sym->result->attr.contiguous;
14198 }
14199 }
14200 }
14201 }
14202 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
14203 {
14204 bool saved_specification_expr = specification_expr;
14205 specification_expr = true;
14206 gfc_resolve_array_spec (sym->result->as, false);
14207 specification_expr = saved_specification_expr;
14208 }
14209
14210 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
14211 {
14212 as = CLASS_DATA (sym)->as;
14213 class_attr = CLASS_DATA (sym)->attr;
14214 class_attr.pointer = class_attr.class_pointer;
14215 }
14216 else
14217 {
14218 class_attr = sym->attr;
14219 as = sym->as;
14220 }
14221
14222 /* F2008, C530. */
14223 if (sym->attr.contiguous
14224 && (!class_attr.dimension
14225 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
14226 && !class_attr.pointer)))
14227 {
14228 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
14229 "array pointer or an assumed-shape or assumed-rank array",
14230 sym->name, &sym->declared_at);
14231 return;
14232 }
14233
14234 /* Assumed size arrays and assumed shape arrays must be dummy
14235 arguments. Array-spec's of implied-shape should have been resolved to
14236 AS_EXPLICIT already. */
14237
14238 if (as)
14239 {
14240 gcc_assert (as->type != AS_IMPLIED_SHAPE);
14241 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
14242 || as->type == AS_ASSUMED_SHAPE)
14243 && !sym->attr.dummy && !sym->attr.select_type_temporary)
14244 {
14245 if (as->type == AS_ASSUMED_SIZE)
14246 gfc_error ("Assumed size array at %L must be a dummy argument",
14247 &sym->declared_at);
14248 else
14249 gfc_error ("Assumed shape array at %L must be a dummy argument",
14250 &sym->declared_at);
14251 return;
14252 }
14253 /* TS 29113, C535a. */
14254 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
14255 && !sym->attr.select_type_temporary)
14256 {
14257 gfc_error ("Assumed-rank array at %L must be a dummy argument",
14258 &sym->declared_at);
14259 return;
14260 }
14261 if (as->type == AS_ASSUMED_RANK
14262 && (sym->attr.codimension || sym->attr.value))
14263 {
14264 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
14265 "CODIMENSION attribute", &sym->declared_at);
14266 return;
14267 }
14268 }
14269
14270 /* Make sure symbols with known intent or optional are really dummy
14271 variable. Because of ENTRY statement, this has to be deferred
14272 until resolution time. */
14273
14274 if (!sym->attr.dummy
14275 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
14276 {
14277 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
14278 return;
14279 }
14280
14281 if (sym->attr.value && !sym->attr.dummy)
14282 {
14283 gfc_error ("%qs at %L cannot have the VALUE attribute because "
14284 "it is not a dummy argument", sym->name, &sym->declared_at);
14285 return;
14286 }
14287
14288 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
14289 {
14290 gfc_charlen *cl = sym->ts.u.cl;
14291 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
14292 {
14293 gfc_error ("Character dummy variable %qs at %L with VALUE "
14294 "attribute must have constant length",
14295 sym->name, &sym->declared_at);
14296 return;
14297 }
14298
14299 if (sym->ts.is_c_interop
14300 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
14301 {
14302 gfc_error ("C interoperable character dummy variable %qs at %L "
14303 "with VALUE attribute must have length one",
14304 sym->name, &sym->declared_at);
14305 return;
14306 }
14307 }
14308
14309 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
14310 && sym->ts.u.derived->attr.generic)
14311 {
14312 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
14313 if (!sym->ts.u.derived)
14314 {
14315 gfc_error ("The derived type %qs at %L is of type %qs, "
14316 "which has not been defined", sym->name,
14317 &sym->declared_at, sym->ts.u.derived->name);
14318 sym->ts.type = BT_UNKNOWN;
14319 return;
14320 }
14321 }
14322
14323 /* Use the same constraints as TYPE(*), except for the type check
14324 and that only scalars and assumed-size arrays are permitted. */
14325 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
14326 {
14327 if (!sym->attr.dummy)
14328 {
14329 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
14330 "a dummy argument", sym->name, &sym->declared_at);
14331 return;
14332 }
14333
14334 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
14335 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
14336 && sym->ts.type != BT_COMPLEX)
14337 {
14338 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
14339 "of type TYPE(*) or of an numeric intrinsic type",
14340 sym->name, &sym->declared_at);
14341 return;
14342 }
14343
14344 if (sym->attr.allocatable || sym->attr.codimension
14345 || sym->attr.pointer || sym->attr.value)
14346 {
14347 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
14348 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
14349 "attribute", sym->name, &sym->declared_at);
14350 return;
14351 }
14352
14353 if (sym->attr.intent == INTENT_OUT)
14354 {
14355 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
14356 "have the INTENT(OUT) attribute",
14357 sym->name, &sym->declared_at);
14358 return;
14359 }
14360 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
14361 {
14362 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
14363 "either be a scalar or an assumed-size array",
14364 sym->name, &sym->declared_at);
14365 return;
14366 }
14367
14368 /* Set the type to TYPE(*) and add a dimension(*) to ensure
14369 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
14370 packing. */
14371 sym->ts.type = BT_ASSUMED;
14372 sym->as = gfc_get_array_spec ();
14373 sym->as->type = AS_ASSUMED_SIZE;
14374 sym->as->rank = 1;
14375 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
14376 }
14377 else if (sym->ts.type == BT_ASSUMED)
14378 {
14379 /* TS 29113, C407a. */
14380 if (!sym->attr.dummy)
14381 {
14382 gfc_error ("Assumed type of variable %s at %L is only permitted "
14383 "for dummy variables", sym->name, &sym->declared_at);
14384 return;
14385 }
14386 if (sym->attr.allocatable || sym->attr.codimension
14387 || sym->attr.pointer || sym->attr.value)
14388 {
14389 gfc_error ("Assumed-type variable %s at %L may not have the "
14390 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
14391 sym->name, &sym->declared_at);
14392 return;
14393 }
14394 if (sym->attr.intent == INTENT_OUT)
14395 {
14396 gfc_error ("Assumed-type variable %s at %L may not have the "
14397 "INTENT(OUT) attribute",
14398 sym->name, &sym->declared_at);
14399 return;
14400 }
14401 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
14402 {
14403 gfc_error ("Assumed-type variable %s at %L shall not be an "
14404 "explicit-shape array", sym->name, &sym->declared_at);
14405 return;
14406 }
14407 }
14408
14409 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
14410 do this for something that was implicitly typed because that is handled
14411 in gfc_set_default_type. Handle dummy arguments and procedure
14412 definitions separately. Also, anything that is use associated is not
14413 handled here but instead is handled in the module it is declared in.
14414 Finally, derived type definitions are allowed to be BIND(C) since that
14415 only implies that they're interoperable, and they are checked fully for
14416 interoperability when a variable is declared of that type. */
14417 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
14418 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
14419 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
14420 {
14421 bool t = true;
14422
14423 /* First, make sure the variable is declared at the
14424 module-level scope (J3/04-007, Section 15.3). */
14425 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
14426 sym->attr.in_common == 0)
14427 {
14428 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
14429 "is neither a COMMON block nor declared at the "
14430 "module level scope", sym->name, &(sym->declared_at));
14431 t = false;
14432 }
14433 else if (sym->common_head != NULL)
14434 {
14435 t = verify_com_block_vars_c_interop (sym->common_head);
14436 }
14437 else
14438 {
14439 /* If type() declaration, we need to verify that the components
14440 of the given type are all C interoperable, etc. */
14441 if (sym->ts.type == BT_DERIVED &&
14442 sym->ts.u.derived->attr.is_c_interop != 1)
14443 {
14444 /* Make sure the user marked the derived type as BIND(C). If
14445 not, call the verify routine. This could print an error
14446 for the derived type more than once if multiple variables
14447 of that type are declared. */
14448 if (sym->ts.u.derived->attr.is_bind_c != 1)
14449 verify_bind_c_derived_type (sym->ts.u.derived);
14450 t = false;
14451 }
14452
14453 /* Verify the variable itself as C interoperable if it
14454 is BIND(C). It is not possible for this to succeed if
14455 the verify_bind_c_derived_type failed, so don't have to handle
14456 any error returned by verify_bind_c_derived_type. */
14457 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
14458 sym->common_block);
14459 }
14460
14461 if (!t)
14462 {
14463 /* clear the is_bind_c flag to prevent reporting errors more than
14464 once if something failed. */
14465 sym->attr.is_bind_c = 0;
14466 return;
14467 }
14468 }
14469
14470 /* If a derived type symbol has reached this point, without its
14471 type being declared, we have an error. Notice that most
14472 conditions that produce undefined derived types have already
14473 been dealt with. However, the likes of:
14474 implicit type(t) (t) ..... call foo (t) will get us here if
14475 the type is not declared in the scope of the implicit
14476 statement. Change the type to BT_UNKNOWN, both because it is so
14477 and to prevent an ICE. */
14478 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
14479 && sym->ts.u.derived->components == NULL
14480 && !sym->ts.u.derived->attr.zero_comp)
14481 {
14482 gfc_error ("The derived type %qs at %L is of type %qs, "
14483 "which has not been defined", sym->name,
14484 &sym->declared_at, sym->ts.u.derived->name);
14485 sym->ts.type = BT_UNKNOWN;
14486 return;
14487 }
14488
14489 /* Make sure that the derived type has been resolved and that the
14490 derived type is visible in the symbol's namespace, if it is a
14491 module function and is not PRIVATE. */
14492 if (sym->ts.type == BT_DERIVED
14493 && sym->ts.u.derived->attr.use_assoc
14494 && sym->ns->proc_name
14495 && sym->ns->proc_name->attr.flavor == FL_MODULE
14496 && !resolve_fl_derived (sym->ts.u.derived))
14497 return;
14498
14499 /* Unless the derived-type declaration is use associated, Fortran 95
14500 does not allow public entries of private derived types.
14501 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
14502 161 in 95-006r3. */
14503 if (sym->ts.type == BT_DERIVED
14504 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
14505 && !sym->ts.u.derived->attr.use_assoc
14506 && gfc_check_symbol_access (sym)
14507 && !gfc_check_symbol_access (sym->ts.u.derived)
14508 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
14509 "derived type %qs",
14510 (sym->attr.flavor == FL_PARAMETER)
14511 ? "parameter" : "variable",
14512 sym->name, &sym->declared_at,
14513 sym->ts.u.derived->name))
14514 return;
14515
14516 /* F2008, C1302. */
14517 if (sym->ts.type == BT_DERIVED
14518 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
14519 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
14520 || sym->ts.u.derived->attr.lock_comp)
14521 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
14522 {
14523 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
14524 "type LOCK_TYPE must be a coarray", sym->name,
14525 &sym->declared_at);
14526 return;
14527 }
14528
14529 /* TS18508, C702/C703. */
14530 if (sym->ts.type == BT_DERIVED
14531 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
14532 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
14533 || sym->ts.u.derived->attr.event_comp)
14534 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
14535 {
14536 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
14537 "type LOCK_TYPE must be a coarray", sym->name,
14538 &sym->declared_at);
14539 return;
14540 }
14541
14542 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
14543 default initialization is defined (5.1.2.4.4). */
14544 if (sym->ts.type == BT_DERIVED
14545 && sym->attr.dummy
14546 && sym->attr.intent == INTENT_OUT
14547 && sym->as
14548 && sym->as->type == AS_ASSUMED_SIZE)
14549 {
14550 for (c = sym->ts.u.derived->components; c; c = c->next)
14551 {
14552 if (c->initializer)
14553 {
14554 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
14555 "ASSUMED SIZE and so cannot have a default initializer",
14556 sym->name, &sym->declared_at);
14557 return;
14558 }
14559 }
14560 }
14561
14562 /* F2008, C542. */
14563 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
14564 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
14565 {
14566 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
14567 "INTENT(OUT)", sym->name, &sym->declared_at);
14568 return;
14569 }
14570
14571 /* TS18508. */
14572 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
14573 && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
14574 {
14575 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
14576 "INTENT(OUT)", sym->name, &sym->declared_at);
14577 return;
14578 }
14579
14580 /* F2008, C525. */
14581 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14582 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14583 && CLASS_DATA (sym)->attr.coarray_comp))
14584 || class_attr.codimension)
14585 && (sym->attr.result || sym->result == sym))
14586 {
14587 gfc_error ("Function result %qs at %L shall not be a coarray or have "
14588 "a coarray component", sym->name, &sym->declared_at);
14589 return;
14590 }
14591
14592 /* F2008, C524. */
14593 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
14594 && sym->ts.u.derived->ts.is_iso_c)
14595 {
14596 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
14597 "shall not be a coarray", sym->name, &sym->declared_at);
14598 return;
14599 }
14600
14601 /* F2008, C525. */
14602 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14603 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14604 && CLASS_DATA (sym)->attr.coarray_comp))
14605 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
14606 || class_attr.allocatable))
14607 {
14608 gfc_error ("Variable %qs at %L with coarray component shall be a "
14609 "nonpointer, nonallocatable scalar, which is not a coarray",
14610 sym->name, &sym->declared_at);
14611 return;
14612 }
14613
14614 /* F2008, C526. The function-result case was handled above. */
14615 if (class_attr.codimension
14616 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
14617 || sym->attr.select_type_temporary
14618 || (sym->ns->save_all && !sym->attr.automatic)
14619 || sym->ns->proc_name->attr.flavor == FL_MODULE
14620 || sym->ns->proc_name->attr.is_main_program
14621 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
14622 {
14623 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
14624 "nor a dummy argument", sym->name, &sym->declared_at);
14625 return;
14626 }
14627 /* F2008, C528. */
14628 else if (class_attr.codimension && !sym->attr.select_type_temporary
14629 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
14630 {
14631 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
14632 "deferred shape", sym->name, &sym->declared_at);
14633 return;
14634 }
14635 else if (class_attr.codimension && class_attr.allocatable && as
14636 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
14637 {
14638 gfc_error ("Allocatable coarray variable %qs at %L must have "
14639 "deferred shape", sym->name, &sym->declared_at);
14640 return;
14641 }
14642
14643 /* F2008, C541. */
14644 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14645 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14646 && CLASS_DATA (sym)->attr.coarray_comp))
14647 || (class_attr.codimension && class_attr.allocatable))
14648 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
14649 {
14650 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
14651 "allocatable coarray or have coarray components",
14652 sym->name, &sym->declared_at);
14653 return;
14654 }
14655
14656 if (class_attr.codimension && sym->attr.dummy
14657 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
14658 {
14659 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
14660 "procedure %qs", sym->name, &sym->declared_at,
14661 sym->ns->proc_name->name);
14662 return;
14663 }
14664
14665 if (sym->ts.type == BT_LOGICAL
14666 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
14667 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
14668 && sym->ns->proc_name->attr.is_bind_c)))
14669 {
14670 int i;
14671 for (i = 0; gfc_logical_kinds[i].kind; i++)
14672 if (gfc_logical_kinds[i].kind == sym->ts.kind)
14673 break;
14674 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
14675 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
14676 "%L with non-C_Bool kind in BIND(C) procedure "
14677 "%qs", sym->name, &sym->declared_at,
14678 sym->ns->proc_name->name))
14679 return;
14680 else if (!gfc_logical_kinds[i].c_bool
14681 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
14682 "%qs at %L with non-C_Bool kind in "
14683 "BIND(C) procedure %qs", sym->name,
14684 &sym->declared_at,
14685 sym->attr.function ? sym->name
14686 : sym->ns->proc_name->name))
14687 return;
14688 }
14689
14690 switch (sym->attr.flavor)
14691 {
14692 case FL_VARIABLE:
14693 if (!resolve_fl_variable (sym, mp_flag))
14694 return;
14695 break;
14696
14697 case FL_PROCEDURE:
14698 if (sym->formal && !sym->formal_ns)
14699 {
14700 /* Check that none of the arguments are a namelist. */
14701 gfc_formal_arglist *formal = sym->formal;
14702
14703 for (; formal; formal = formal->next)
14704 if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
14705 {
14706 gfc_error ("Namelist '%s' can not be an argument to "
14707 "subroutine or function at %L",
14708 formal->sym->name, &sym->declared_at);
14709 return;
14710 }
14711 }
14712
14713 if (!resolve_fl_procedure (sym, mp_flag))
14714 return;
14715 break;
14716
14717 case FL_NAMELIST:
14718 if (!resolve_fl_namelist (sym))
14719 return;
14720 break;
14721
14722 case FL_PARAMETER:
14723 if (!resolve_fl_parameter (sym))
14724 return;
14725 break;
14726
14727 default:
14728 break;
14729 }
14730
14731 /* Resolve array specifier. Check as well some constraints
14732 on COMMON blocks. */
14733
14734 check_constant = sym->attr.in_common && !sym->attr.pointer;
14735
14736 /* Set the formal_arg_flag so that check_conflict will not throw
14737 an error for host associated variables in the specification
14738 expression for an array_valued function. */
14739 if (sym->attr.function && sym->as)
14740 formal_arg_flag = 1;
14741
14742 saved_specification_expr = specification_expr;
14743 specification_expr = true;
14744 gfc_resolve_array_spec (sym->as, check_constant);
14745 specification_expr = saved_specification_expr;
14746
14747 formal_arg_flag = 0;
14748
14749 /* Resolve formal namespaces. */
14750 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
14751 && !sym->attr.contained && !sym->attr.intrinsic)
14752 gfc_resolve (sym->formal_ns);
14753
14754 /* Make sure the formal namespace is present. */
14755 if (sym->formal && !sym->formal_ns)
14756 {
14757 gfc_formal_arglist *formal = sym->formal;
14758 while (formal && !formal->sym)
14759 formal = formal->next;
14760
14761 if (formal)
14762 {
14763 sym->formal_ns = formal->sym->ns;
14764 if (sym->ns != formal->sym->ns)
14765 sym->formal_ns->refs++;
14766 }
14767 }
14768
14769 /* Check threadprivate restrictions. */
14770 if (sym->attr.threadprivate && !sym->attr.save
14771 && !(sym->ns->save_all && !sym->attr.automatic)
14772 && (!sym->attr.in_common
14773 && sym->module == NULL
14774 && (sym->ns->proc_name == NULL
14775 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
14776 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
14777
14778 /* Check omp declare target restrictions. */
14779 if (sym->attr.omp_declare_target
14780 && sym->attr.flavor == FL_VARIABLE
14781 && !sym->attr.save
14782 && !(sym->ns->save_all && !sym->attr.automatic)
14783 && (!sym->attr.in_common
14784 && sym->module == NULL
14785 && (sym->ns->proc_name == NULL
14786 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
14787 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
14788 sym->name, &sym->declared_at);
14789
14790 /* If we have come this far we can apply default-initializers, as
14791 described in 14.7.5, to those variables that have not already
14792 been assigned one. */
14793 if (sym->ts.type == BT_DERIVED
14794 && !sym->value
14795 && !sym->attr.allocatable
14796 && !sym->attr.alloc_comp)
14797 {
14798 symbol_attribute *a = &sym->attr;
14799
14800 if ((!a->save && !a->dummy && !a->pointer
14801 && !a->in_common && !a->use_assoc
14802 && !a->result && !a->function)
14803 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
14804 apply_default_init (sym);
14805 else if (a->function && sym->result && a->access != ACCESS_PRIVATE
14806 && (sym->ts.u.derived->attr.alloc_comp
14807 || sym->ts.u.derived->attr.pointer_comp))
14808 /* Mark the result symbol to be referenced, when it has allocatable
14809 components. */
14810 sym->result->attr.referenced = 1;
14811 }
14812
14813 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
14814 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
14815 && !CLASS_DATA (sym)->attr.class_pointer
14816 && !CLASS_DATA (sym)->attr.allocatable)
14817 apply_default_init (sym);
14818
14819 /* If this symbol has a type-spec, check it. */
14820 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
14821 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
14822 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
14823 return;
14824 }
14825
14826
14827 /************* Resolve DATA statements *************/
14828
14829 static struct
14830 {
14831 gfc_data_value *vnode;
14832 mpz_t left;
14833 }
14834 values;
14835
14836
14837 /* Advance the values structure to point to the next value in the data list. */
14838
14839 static bool
14840 next_data_value (void)
14841 {
14842 while (mpz_cmp_ui (values.left, 0) == 0)
14843 {
14844
14845 if (values.vnode->next == NULL)
14846 return false;
14847
14848 values.vnode = values.vnode->next;
14849 mpz_set (values.left, values.vnode->repeat);
14850 }
14851
14852 return true;
14853 }
14854
14855
14856 static bool
14857 check_data_variable (gfc_data_variable *var, locus *where)
14858 {
14859 gfc_expr *e;
14860 mpz_t size;
14861 mpz_t offset;
14862 bool t;
14863 ar_type mark = AR_UNKNOWN;
14864 int i;
14865 mpz_t section_index[GFC_MAX_DIMENSIONS];
14866 gfc_ref *ref;
14867 gfc_array_ref *ar;
14868 gfc_symbol *sym;
14869 int has_pointer;
14870
14871 if (!gfc_resolve_expr (var->expr))
14872 return false;
14873
14874 ar = NULL;
14875 mpz_init_set_si (offset, 0);
14876 e = var->expr;
14877
14878 if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
14879 && e->value.function.isym->id == GFC_ISYM_CAF_GET)
14880 e = e->value.function.actual->expr;
14881
14882 if (e->expr_type != EXPR_VARIABLE)
14883 gfc_internal_error ("check_data_variable(): Bad expression");
14884
14885 sym = e->symtree->n.sym;
14886
14887 if (sym->ns->is_block_data && !sym->attr.in_common)
14888 {
14889 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
14890 sym->name, &sym->declared_at);
14891 }
14892
14893 if (e->ref == NULL && sym->as)
14894 {
14895 gfc_error ("DATA array %qs at %L must be specified in a previous"
14896 " declaration", sym->name, where);
14897 return false;
14898 }
14899
14900 has_pointer = sym->attr.pointer;
14901
14902 if (gfc_is_coindexed (e))
14903 {
14904 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
14905 where);
14906 return false;
14907 }
14908
14909 for (ref = e->ref; ref; ref = ref->next)
14910 {
14911 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
14912 has_pointer = 1;
14913
14914 if (has_pointer
14915 && ref->type == REF_ARRAY
14916 && ref->u.ar.type != AR_FULL)
14917 {
14918 gfc_error ("DATA element %qs at %L is a pointer and so must "
14919 "be a full array", sym->name, where);
14920 return false;
14921 }
14922 }
14923
14924 if (e->rank == 0 || has_pointer)
14925 {
14926 mpz_init_set_ui (size, 1);
14927 ref = NULL;
14928 }
14929 else
14930 {
14931 ref = e->ref;
14932
14933 /* Find the array section reference. */
14934 for (ref = e->ref; ref; ref = ref->next)
14935 {
14936 if (ref->type != REF_ARRAY)
14937 continue;
14938 if (ref->u.ar.type == AR_ELEMENT)
14939 continue;
14940 break;
14941 }
14942 gcc_assert (ref);
14943
14944 /* Set marks according to the reference pattern. */
14945 switch (ref->u.ar.type)
14946 {
14947 case AR_FULL:
14948 mark = AR_FULL;
14949 break;
14950
14951 case AR_SECTION:
14952 ar = &ref->u.ar;
14953 /* Get the start position of array section. */
14954 gfc_get_section_index (ar, section_index, &offset);
14955 mark = AR_SECTION;
14956 break;
14957
14958 default:
14959 gcc_unreachable ();
14960 }
14961
14962 if (!gfc_array_size (e, &size))
14963 {
14964 gfc_error ("Nonconstant array section at %L in DATA statement",
14965 &e->where);
14966 mpz_clear (offset);
14967 return false;
14968 }
14969 }
14970
14971 t = true;
14972
14973 while (mpz_cmp_ui (size, 0) > 0)
14974 {
14975 if (!next_data_value ())
14976 {
14977 gfc_error ("DATA statement at %L has more variables than values",
14978 where);
14979 t = false;
14980 break;
14981 }
14982
14983 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
14984 if (!t)
14985 break;
14986
14987 /* If we have more than one element left in the repeat count,
14988 and we have more than one element left in the target variable,
14989 then create a range assignment. */
14990 /* FIXME: Only done for full arrays for now, since array sections
14991 seem tricky. */
14992 if (mark == AR_FULL && ref && ref->next == NULL
14993 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
14994 {
14995 mpz_t range;
14996
14997 if (mpz_cmp (size, values.left) >= 0)
14998 {
14999 mpz_init_set (range, values.left);
15000 mpz_sub (size, size, values.left);
15001 mpz_set_ui (values.left, 0);
15002 }
15003 else
15004 {
15005 mpz_init_set (range, size);
15006 mpz_sub (values.left, values.left, size);
15007 mpz_set_ui (size, 0);
15008 }
15009
15010 t = gfc_assign_data_value (var->expr, values.vnode->expr,
15011 offset, &range);
15012
15013 mpz_add (offset, offset, range);
15014 mpz_clear (range);
15015
15016 if (!t)
15017 break;
15018 }
15019
15020 /* Assign initial value to symbol. */
15021 else
15022 {
15023 mpz_sub_ui (values.left, values.left, 1);
15024 mpz_sub_ui (size, size, 1);
15025
15026 t = gfc_assign_data_value (var->expr, values.vnode->expr,
15027 offset, NULL);
15028 if (!t)
15029 break;
15030
15031 if (mark == AR_FULL)
15032 mpz_add_ui (offset, offset, 1);
15033
15034 /* Modify the array section indexes and recalculate the offset
15035 for next element. */
15036 else if (mark == AR_SECTION)
15037 gfc_advance_section (section_index, ar, &offset);
15038 }
15039 }
15040
15041 if (mark == AR_SECTION)
15042 {
15043 for (i = 0; i < ar->dimen; i++)
15044 mpz_clear (section_index[i]);
15045 }
15046
15047 mpz_clear (size);
15048 mpz_clear (offset);
15049
15050 return t;
15051 }
15052
15053
15054 static bool traverse_data_var (gfc_data_variable *, locus *);
15055
15056 /* Iterate over a list of elements in a DATA statement. */
15057
15058 static bool
15059 traverse_data_list (gfc_data_variable *var, locus *where)
15060 {
15061 mpz_t trip;
15062 iterator_stack frame;
15063 gfc_expr *e, *start, *end, *step;
15064 bool retval = true;
15065
15066 mpz_init (frame.value);
15067 mpz_init (trip);
15068
15069 start = gfc_copy_expr (var->iter.start);
15070 end = gfc_copy_expr (var->iter.end);
15071 step = gfc_copy_expr (var->iter.step);
15072
15073 if (!gfc_simplify_expr (start, 1)
15074 || start->expr_type != EXPR_CONSTANT)
15075 {
15076 gfc_error ("start of implied-do loop at %L could not be "
15077 "simplified to a constant value", &start->where);
15078 retval = false;
15079 goto cleanup;
15080 }
15081 if (!gfc_simplify_expr (end, 1)
15082 || end->expr_type != EXPR_CONSTANT)
15083 {
15084 gfc_error ("end of implied-do loop at %L could not be "
15085 "simplified to a constant value", &start->where);
15086 retval = false;
15087 goto cleanup;
15088 }
15089 if (!gfc_simplify_expr (step, 1)
15090 || step->expr_type != EXPR_CONSTANT)
15091 {
15092 gfc_error ("step of implied-do loop at %L could not be "
15093 "simplified to a constant value", &start->where);
15094 retval = false;
15095 goto cleanup;
15096 }
15097
15098 mpz_set (trip, end->value.integer);
15099 mpz_sub (trip, trip, start->value.integer);
15100 mpz_add (trip, trip, step->value.integer);
15101
15102 mpz_div (trip, trip, step->value.integer);
15103
15104 mpz_set (frame.value, start->value.integer);
15105
15106 frame.prev = iter_stack;
15107 frame.variable = var->iter.var->symtree;
15108 iter_stack = &frame;
15109
15110 while (mpz_cmp_ui (trip, 0) > 0)
15111 {
15112 if (!traverse_data_var (var->list, where))
15113 {
15114 retval = false;
15115 goto cleanup;
15116 }
15117
15118 e = gfc_copy_expr (var->expr);
15119 if (!gfc_simplify_expr (e, 1))
15120 {
15121 gfc_free_expr (e);
15122 retval = false;
15123 goto cleanup;
15124 }
15125
15126 mpz_add (frame.value, frame.value, step->value.integer);
15127
15128 mpz_sub_ui (trip, trip, 1);
15129 }
15130
15131 cleanup:
15132 mpz_clear (frame.value);
15133 mpz_clear (trip);
15134
15135 gfc_free_expr (start);
15136 gfc_free_expr (end);
15137 gfc_free_expr (step);
15138
15139 iter_stack = frame.prev;
15140 return retval;
15141 }
15142
15143
15144 /* Type resolve variables in the variable list of a DATA statement. */
15145
15146 static bool
15147 traverse_data_var (gfc_data_variable *var, locus *where)
15148 {
15149 bool t;
15150
15151 for (; var; var = var->next)
15152 {
15153 if (var->expr == NULL)
15154 t = traverse_data_list (var, where);
15155 else
15156 t = check_data_variable (var, where);
15157
15158 if (!t)
15159 return false;
15160 }
15161
15162 return true;
15163 }
15164
15165
15166 /* Resolve the expressions and iterators associated with a data statement.
15167 This is separate from the assignment checking because data lists should
15168 only be resolved once. */
15169
15170 static bool
15171 resolve_data_variables (gfc_data_variable *d)
15172 {
15173 for (; d; d = d->next)
15174 {
15175 if (d->list == NULL)
15176 {
15177 if (!gfc_resolve_expr (d->expr))
15178 return false;
15179 }
15180 else
15181 {
15182 if (!gfc_resolve_iterator (&d->iter, false, true))
15183 return false;
15184
15185 if (!resolve_data_variables (d->list))
15186 return false;
15187 }
15188 }
15189
15190 return true;
15191 }
15192
15193
15194 /* Resolve a single DATA statement. We implement this by storing a pointer to
15195 the value list into static variables, and then recursively traversing the
15196 variables list, expanding iterators and such. */
15197
15198 static void
15199 resolve_data (gfc_data *d)
15200 {
15201
15202 if (!resolve_data_variables (d->var))
15203 return;
15204
15205 values.vnode = d->value;
15206 if (d->value == NULL)
15207 mpz_set_ui (values.left, 0);
15208 else
15209 mpz_set (values.left, d->value->repeat);
15210
15211 if (!traverse_data_var (d->var, &d->where))
15212 return;
15213
15214 /* At this point, we better not have any values left. */
15215
15216 if (next_data_value ())
15217 gfc_error ("DATA statement at %L has more values than variables",
15218 &d->where);
15219 }
15220
15221
15222 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
15223 accessed by host or use association, is a dummy argument to a pure function,
15224 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
15225 is storage associated with any such variable, shall not be used in the
15226 following contexts: (clients of this function). */
15227
15228 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
15229 procedure. Returns zero if assignment is OK, nonzero if there is a
15230 problem. */
15231 int
15232 gfc_impure_variable (gfc_symbol *sym)
15233 {
15234 gfc_symbol *proc;
15235 gfc_namespace *ns;
15236
15237 if (sym->attr.use_assoc || sym->attr.in_common)
15238 return 1;
15239
15240 /* Check if the symbol's ns is inside the pure procedure. */
15241 for (ns = gfc_current_ns; ns; ns = ns->parent)
15242 {
15243 if (ns == sym->ns)
15244 break;
15245 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
15246 return 1;
15247 }
15248
15249 proc = sym->ns->proc_name;
15250 if (sym->attr.dummy
15251 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
15252 || proc->attr.function))
15253 return 1;
15254
15255 /* TODO: Sort out what can be storage associated, if anything, and include
15256 it here. In principle equivalences should be scanned but it does not
15257 seem to be possible to storage associate an impure variable this way. */
15258 return 0;
15259 }
15260
15261
15262 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
15263 current namespace is inside a pure procedure. */
15264
15265 int
15266 gfc_pure (gfc_symbol *sym)
15267 {
15268 symbol_attribute attr;
15269 gfc_namespace *ns;
15270
15271 if (sym == NULL)
15272 {
15273 /* Check if the current namespace or one of its parents
15274 belongs to a pure procedure. */
15275 for (ns = gfc_current_ns; ns; ns = ns->parent)
15276 {
15277 sym = ns->proc_name;
15278 if (sym == NULL)
15279 return 0;
15280 attr = sym->attr;
15281 if (attr.flavor == FL_PROCEDURE && attr.pure)
15282 return 1;
15283 }
15284 return 0;
15285 }
15286
15287 attr = sym->attr;
15288
15289 return attr.flavor == FL_PROCEDURE && attr.pure;
15290 }
15291
15292
15293 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
15294 checks if the current namespace is implicitly pure. Note that this
15295 function returns false for a PURE procedure. */
15296
15297 int
15298 gfc_implicit_pure (gfc_symbol *sym)
15299 {
15300 gfc_namespace *ns;
15301
15302 if (sym == NULL)
15303 {
15304 /* Check if the current procedure is implicit_pure. Walk up
15305 the procedure list until we find a procedure. */
15306 for (ns = gfc_current_ns; ns; ns = ns->parent)
15307 {
15308 sym = ns->proc_name;
15309 if (sym == NULL)
15310 return 0;
15311
15312 if (sym->attr.flavor == FL_PROCEDURE)
15313 break;
15314 }
15315 }
15316
15317 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
15318 && !sym->attr.pure;
15319 }
15320
15321
15322 void
15323 gfc_unset_implicit_pure (gfc_symbol *sym)
15324 {
15325 gfc_namespace *ns;
15326
15327 if (sym == NULL)
15328 {
15329 /* Check if the current procedure is implicit_pure. Walk up
15330 the procedure list until we find a procedure. */
15331 for (ns = gfc_current_ns; ns; ns = ns->parent)
15332 {
15333 sym = ns->proc_name;
15334 if (sym == NULL)
15335 return;
15336
15337 if (sym->attr.flavor == FL_PROCEDURE)
15338 break;
15339 }
15340 }
15341
15342 if (sym->attr.flavor == FL_PROCEDURE)
15343 sym->attr.implicit_pure = 0;
15344 else
15345 sym->attr.pure = 0;
15346 }
15347
15348
15349 /* Test whether the current procedure is elemental or not. */
15350
15351 int
15352 gfc_elemental (gfc_symbol *sym)
15353 {
15354 symbol_attribute attr;
15355
15356 if (sym == NULL)
15357 sym = gfc_current_ns->proc_name;
15358 if (sym == NULL)
15359 return 0;
15360 attr = sym->attr;
15361
15362 return attr.flavor == FL_PROCEDURE && attr.elemental;
15363 }
15364
15365
15366 /* Warn about unused labels. */
15367
15368 static void
15369 warn_unused_fortran_label (gfc_st_label *label)
15370 {
15371 if (label == NULL)
15372 return;
15373
15374 warn_unused_fortran_label (label->left);
15375
15376 if (label->defined == ST_LABEL_UNKNOWN)
15377 return;
15378
15379 switch (label->referenced)
15380 {
15381 case ST_LABEL_UNKNOWN:
15382 gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
15383 label->value, &label->where);
15384 break;
15385
15386 case ST_LABEL_BAD_TARGET:
15387 gfc_warning (OPT_Wunused_label,
15388 "Label %d at %L defined but cannot be used",
15389 label->value, &label->where);
15390 break;
15391
15392 default:
15393 break;
15394 }
15395
15396 warn_unused_fortran_label (label->right);
15397 }
15398
15399
15400 /* Returns the sequence type of a symbol or sequence. */
15401
15402 static seq_type
15403 sequence_type (gfc_typespec ts)
15404 {
15405 seq_type result;
15406 gfc_component *c;
15407
15408 switch (ts.type)
15409 {
15410 case BT_DERIVED:
15411
15412 if (ts.u.derived->components == NULL)
15413 return SEQ_NONDEFAULT;
15414
15415 result = sequence_type (ts.u.derived->components->ts);
15416 for (c = ts.u.derived->components->next; c; c = c->next)
15417 if (sequence_type (c->ts) != result)
15418 return SEQ_MIXED;
15419
15420 return result;
15421
15422 case BT_CHARACTER:
15423 if (ts.kind != gfc_default_character_kind)
15424 return SEQ_NONDEFAULT;
15425
15426 return SEQ_CHARACTER;
15427
15428 case BT_INTEGER:
15429 if (ts.kind != gfc_default_integer_kind)
15430 return SEQ_NONDEFAULT;
15431
15432 return SEQ_NUMERIC;
15433
15434 case BT_REAL:
15435 if (!(ts.kind == gfc_default_real_kind
15436 || ts.kind == gfc_default_double_kind))
15437 return SEQ_NONDEFAULT;
15438
15439 return SEQ_NUMERIC;
15440
15441 case BT_COMPLEX:
15442 if (ts.kind != gfc_default_complex_kind)
15443 return SEQ_NONDEFAULT;
15444
15445 return SEQ_NUMERIC;
15446
15447 case BT_LOGICAL:
15448 if (ts.kind != gfc_default_logical_kind)
15449 return SEQ_NONDEFAULT;
15450
15451 return SEQ_NUMERIC;
15452
15453 default:
15454 return SEQ_NONDEFAULT;
15455 }
15456 }
15457
15458
15459 /* Resolve derived type EQUIVALENCE object. */
15460
15461 static bool
15462 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
15463 {
15464 gfc_component *c = derived->components;
15465
15466 if (!derived)
15467 return true;
15468
15469 /* Shall not be an object of nonsequence derived type. */
15470 if (!derived->attr.sequence)
15471 {
15472 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
15473 "attribute to be an EQUIVALENCE object", sym->name,
15474 &e->where);
15475 return false;
15476 }
15477
15478 /* Shall not have allocatable components. */
15479 if (derived->attr.alloc_comp)
15480 {
15481 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
15482 "components to be an EQUIVALENCE object",sym->name,
15483 &e->where);
15484 return false;
15485 }
15486
15487 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
15488 {
15489 gfc_error ("Derived type variable %qs at %L with default "
15490 "initialization cannot be in EQUIVALENCE with a variable "
15491 "in COMMON", sym->name, &e->where);
15492 return false;
15493 }
15494
15495 for (; c ; c = c->next)
15496 {
15497 if (gfc_bt_struct (c->ts.type)
15498 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
15499 return false;
15500
15501 /* Shall not be an object of sequence derived type containing a pointer
15502 in the structure. */
15503 if (c->attr.pointer)
15504 {
15505 gfc_error ("Derived type variable %qs at %L with pointer "
15506 "component(s) cannot be an EQUIVALENCE object",
15507 sym->name, &e->where);
15508 return false;
15509 }
15510 }
15511 return true;
15512 }
15513
15514
15515 /* Resolve equivalence object.
15516 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
15517 an allocatable array, an object of nonsequence derived type, an object of
15518 sequence derived type containing a pointer at any level of component
15519 selection, an automatic object, a function name, an entry name, a result
15520 name, a named constant, a structure component, or a subobject of any of
15521 the preceding objects. A substring shall not have length zero. A
15522 derived type shall not have components with default initialization nor
15523 shall two objects of an equivalence group be initialized.
15524 Either all or none of the objects shall have an protected attribute.
15525 The simple constraints are done in symbol.c(check_conflict) and the rest
15526 are implemented here. */
15527
15528 static void
15529 resolve_equivalence (gfc_equiv *eq)
15530 {
15531 gfc_symbol *sym;
15532 gfc_symbol *first_sym;
15533 gfc_expr *e;
15534 gfc_ref *r;
15535 locus *last_where = NULL;
15536 seq_type eq_type, last_eq_type;
15537 gfc_typespec *last_ts;
15538 int object, cnt_protected;
15539 const char *msg;
15540
15541 last_ts = &eq->expr->symtree->n.sym->ts;
15542
15543 first_sym = eq->expr->symtree->n.sym;
15544
15545 cnt_protected = 0;
15546
15547 for (object = 1; eq; eq = eq->eq, object++)
15548 {
15549 e = eq->expr;
15550
15551 e->ts = e->symtree->n.sym->ts;
15552 /* match_varspec might not know yet if it is seeing
15553 array reference or substring reference, as it doesn't
15554 know the types. */
15555 if (e->ref && e->ref->type == REF_ARRAY)
15556 {
15557 gfc_ref *ref = e->ref;
15558 sym = e->symtree->n.sym;
15559
15560 if (sym->attr.dimension)
15561 {
15562 ref->u.ar.as = sym->as;
15563 ref = ref->next;
15564 }
15565
15566 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
15567 if (e->ts.type == BT_CHARACTER
15568 && ref
15569 && ref->type == REF_ARRAY
15570 && ref->u.ar.dimen == 1
15571 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
15572 && ref->u.ar.stride[0] == NULL)
15573 {
15574 gfc_expr *start = ref->u.ar.start[0];
15575 gfc_expr *end = ref->u.ar.end[0];
15576 void *mem = NULL;
15577
15578 /* Optimize away the (:) reference. */
15579 if (start == NULL && end == NULL)
15580 {
15581 if (e->ref == ref)
15582 e->ref = ref->next;
15583 else
15584 e->ref->next = ref->next;
15585 mem = ref;
15586 }
15587 else
15588 {
15589 ref->type = REF_SUBSTRING;
15590 if (start == NULL)
15591 start = gfc_get_int_expr (gfc_default_integer_kind,
15592 NULL, 1);
15593 ref->u.ss.start = start;
15594 if (end == NULL && e->ts.u.cl)
15595 end = gfc_copy_expr (e->ts.u.cl->length);
15596 ref->u.ss.end = end;
15597 ref->u.ss.length = e->ts.u.cl;
15598 e->ts.u.cl = NULL;
15599 }
15600 ref = ref->next;
15601 free (mem);
15602 }
15603
15604 /* Any further ref is an error. */
15605 if (ref)
15606 {
15607 gcc_assert (ref->type == REF_ARRAY);
15608 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
15609 &ref->u.ar.where);
15610 continue;
15611 }
15612 }
15613
15614 if (!gfc_resolve_expr (e))
15615 continue;
15616
15617 sym = e->symtree->n.sym;
15618
15619 if (sym->attr.is_protected)
15620 cnt_protected++;
15621 if (cnt_protected > 0 && cnt_protected != object)
15622 {
15623 gfc_error ("Either all or none of the objects in the "
15624 "EQUIVALENCE set at %L shall have the "
15625 "PROTECTED attribute",
15626 &e->where);
15627 break;
15628 }
15629
15630 /* Shall not equivalence common block variables in a PURE procedure. */
15631 if (sym->ns->proc_name
15632 && sym->ns->proc_name->attr.pure
15633 && sym->attr.in_common)
15634 {
15635 gfc_error ("Common block member %qs at %L cannot be an EQUIVALENCE "
15636 "object in the pure procedure %qs",
15637 sym->name, &e->where, sym->ns->proc_name->name);
15638 break;
15639 }
15640
15641 /* Shall not be a named constant. */
15642 if (e->expr_type == EXPR_CONSTANT)
15643 {
15644 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
15645 "object", sym->name, &e->where);
15646 continue;
15647 }
15648
15649 if (e->ts.type == BT_DERIVED
15650 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
15651 continue;
15652
15653 /* Check that the types correspond correctly:
15654 Note 5.28:
15655 A numeric sequence structure may be equivalenced to another sequence
15656 structure, an object of default integer type, default real type, double
15657 precision real type, default logical type such that components of the
15658 structure ultimately only become associated to objects of the same
15659 kind. A character sequence structure may be equivalenced to an object
15660 of default character kind or another character sequence structure.
15661 Other objects may be equivalenced only to objects of the same type and
15662 kind parameters. */
15663
15664 /* Identical types are unconditionally OK. */
15665 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
15666 goto identical_types;
15667
15668 last_eq_type = sequence_type (*last_ts);
15669 eq_type = sequence_type (sym->ts);
15670
15671 /* Since the pair of objects is not of the same type, mixed or
15672 non-default sequences can be rejected. */
15673
15674 msg = "Sequence %s with mixed components in EQUIVALENCE "
15675 "statement at %L with different type objects";
15676 if ((object ==2
15677 && last_eq_type == SEQ_MIXED
15678 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
15679 || (eq_type == SEQ_MIXED
15680 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
15681 continue;
15682
15683 msg = "Non-default type object or sequence %s in EQUIVALENCE "
15684 "statement at %L with objects of different type";
15685 if ((object ==2
15686 && last_eq_type == SEQ_NONDEFAULT
15687 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
15688 || (eq_type == SEQ_NONDEFAULT
15689 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
15690 continue;
15691
15692 msg ="Non-CHARACTER object %qs in default CHARACTER "
15693 "EQUIVALENCE statement at %L";
15694 if (last_eq_type == SEQ_CHARACTER
15695 && eq_type != SEQ_CHARACTER
15696 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
15697 continue;
15698
15699 msg ="Non-NUMERIC object %qs in default NUMERIC "
15700 "EQUIVALENCE statement at %L";
15701 if (last_eq_type == SEQ_NUMERIC
15702 && eq_type != SEQ_NUMERIC
15703 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
15704 continue;
15705
15706 identical_types:
15707 last_ts =&sym->ts;
15708 last_where = &e->where;
15709
15710 if (!e->ref)
15711 continue;
15712
15713 /* Shall not be an automatic array. */
15714 if (e->ref->type == REF_ARRAY
15715 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
15716 {
15717 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
15718 "an EQUIVALENCE object", sym->name, &e->where);
15719 continue;
15720 }
15721
15722 r = e->ref;
15723 while (r)
15724 {
15725 /* Shall not be a structure component. */
15726 if (r->type == REF_COMPONENT)
15727 {
15728 gfc_error ("Structure component %qs at %L cannot be an "
15729 "EQUIVALENCE object",
15730 r->u.c.component->name, &e->where);
15731 break;
15732 }
15733
15734 /* A substring shall not have length zero. */
15735 if (r->type == REF_SUBSTRING)
15736 {
15737 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
15738 {
15739 gfc_error ("Substring at %L has length zero",
15740 &r->u.ss.start->where);
15741 break;
15742 }
15743 }
15744 r = r->next;
15745 }
15746 }
15747 }
15748
15749
15750 /* Resolve function and ENTRY types, issue diagnostics if needed. */
15751
15752 static void
15753 resolve_fntype (gfc_namespace *ns)
15754 {
15755 gfc_entry_list *el;
15756 gfc_symbol *sym;
15757
15758 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
15759 return;
15760
15761 /* If there are any entries, ns->proc_name is the entry master
15762 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
15763 if (ns->entries)
15764 sym = ns->entries->sym;
15765 else
15766 sym = ns->proc_name;
15767 if (sym->result == sym
15768 && sym->ts.type == BT_UNKNOWN
15769 && !gfc_set_default_type (sym, 0, NULL)
15770 && !sym->attr.untyped)
15771 {
15772 gfc_error ("Function %qs at %L has no IMPLICIT type",
15773 sym->name, &sym->declared_at);
15774 sym->attr.untyped = 1;
15775 }
15776
15777 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
15778 && !sym->attr.contained
15779 && !gfc_check_symbol_access (sym->ts.u.derived)
15780 && gfc_check_symbol_access (sym))
15781 {
15782 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
15783 "%L of PRIVATE type %qs", sym->name,
15784 &sym->declared_at, sym->ts.u.derived->name);
15785 }
15786
15787 if (ns->entries)
15788 for (el = ns->entries->next; el; el = el->next)
15789 {
15790 if (el->sym->result == el->sym
15791 && el->sym->ts.type == BT_UNKNOWN
15792 && !gfc_set_default_type (el->sym, 0, NULL)
15793 && !el->sym->attr.untyped)
15794 {
15795 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
15796 el->sym->name, &el->sym->declared_at);
15797 el->sym->attr.untyped = 1;
15798 }
15799 }
15800 }
15801
15802
15803 /* 12.3.2.1.1 Defined operators. */
15804
15805 static bool
15806 check_uop_procedure (gfc_symbol *sym, locus where)
15807 {
15808 gfc_formal_arglist *formal;
15809
15810 if (!sym->attr.function)
15811 {
15812 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
15813 sym->name, &where);
15814 return false;
15815 }
15816
15817 if (sym->ts.type == BT_CHARACTER
15818 && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
15819 && !(sym->result && ((sym->result->ts.u.cl
15820 && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
15821 {
15822 gfc_error ("User operator procedure %qs at %L cannot be assumed "
15823 "character length", sym->name, &where);
15824 return false;
15825 }
15826
15827 formal = gfc_sym_get_dummy_args (sym);
15828 if (!formal || !formal->sym)
15829 {
15830 gfc_error ("User operator procedure %qs at %L must have at least "
15831 "one argument", sym->name, &where);
15832 return false;
15833 }
15834
15835 if (formal->sym->attr.intent != INTENT_IN)
15836 {
15837 gfc_error ("First argument of operator interface at %L must be "
15838 "INTENT(IN)", &where);
15839 return false;
15840 }
15841
15842 if (formal->sym->attr.optional)
15843 {
15844 gfc_error ("First argument of operator interface at %L cannot be "
15845 "optional", &where);
15846 return false;
15847 }
15848
15849 formal = formal->next;
15850 if (!formal || !formal->sym)
15851 return true;
15852
15853 if (formal->sym->attr.intent != INTENT_IN)
15854 {
15855 gfc_error ("Second argument of operator interface at %L must be "
15856 "INTENT(IN)", &where);
15857 return false;
15858 }
15859
15860 if (formal->sym->attr.optional)
15861 {
15862 gfc_error ("Second argument of operator interface at %L cannot be "
15863 "optional", &where);
15864 return false;
15865 }
15866
15867 if (formal->next)
15868 {
15869 gfc_error ("Operator interface at %L must have, at most, two "
15870 "arguments", &where);
15871 return false;
15872 }
15873
15874 return true;
15875 }
15876
15877 static void
15878 gfc_resolve_uops (gfc_symtree *symtree)
15879 {
15880 gfc_interface *itr;
15881
15882 if (symtree == NULL)
15883 return;
15884
15885 gfc_resolve_uops (symtree->left);
15886 gfc_resolve_uops (symtree->right);
15887
15888 for (itr = symtree->n.uop->op; itr; itr = itr->next)
15889 check_uop_procedure (itr->sym, itr->sym->declared_at);
15890 }
15891
15892
15893 /* Examine all of the expressions associated with a program unit,
15894 assign types to all intermediate expressions, make sure that all
15895 assignments are to compatible types and figure out which names
15896 refer to which functions or subroutines. It doesn't check code
15897 block, which is handled by gfc_resolve_code. */
15898
15899 static void
15900 resolve_types (gfc_namespace *ns)
15901 {
15902 gfc_namespace *n;
15903 gfc_charlen *cl;
15904 gfc_data *d;
15905 gfc_equiv *eq;
15906 gfc_namespace* old_ns = gfc_current_ns;
15907
15908 if (ns->types_resolved)
15909 return;
15910
15911 /* Check that all IMPLICIT types are ok. */
15912 if (!ns->seen_implicit_none)
15913 {
15914 unsigned letter;
15915 for (letter = 0; letter != GFC_LETTERS; ++letter)
15916 if (ns->set_flag[letter]
15917 && !resolve_typespec_used (&ns->default_type[letter],
15918 &ns->implicit_loc[letter], NULL))
15919 return;
15920 }
15921
15922 gfc_current_ns = ns;
15923
15924 resolve_entries (ns);
15925
15926 resolve_common_vars (&ns->blank_common, false);
15927 resolve_common_blocks (ns->common_root);
15928
15929 resolve_contained_functions (ns);
15930
15931 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
15932 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
15933 resolve_formal_arglist (ns->proc_name);
15934
15935 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
15936
15937 for (cl = ns->cl_list; cl; cl = cl->next)
15938 resolve_charlen (cl);
15939
15940 gfc_traverse_ns (ns, resolve_symbol);
15941
15942 resolve_fntype (ns);
15943
15944 for (n = ns->contained; n; n = n->sibling)
15945 {
15946 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
15947 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
15948 "also be PURE", n->proc_name->name,
15949 &n->proc_name->declared_at);
15950
15951 resolve_types (n);
15952 }
15953
15954 forall_flag = 0;
15955 gfc_do_concurrent_flag = 0;
15956 gfc_check_interfaces (ns);
15957
15958 gfc_traverse_ns (ns, resolve_values);
15959
15960 if (ns->save_all)
15961 gfc_save_all (ns);
15962
15963 iter_stack = NULL;
15964 for (d = ns->data; d; d = d->next)
15965 resolve_data (d);
15966
15967 iter_stack = NULL;
15968 gfc_traverse_ns (ns, gfc_formalize_init_value);
15969
15970 gfc_traverse_ns (ns, gfc_verify_binding_labels);
15971
15972 for (eq = ns->equiv; eq; eq = eq->next)
15973 resolve_equivalence (eq);
15974
15975 /* Warn about unused labels. */
15976 if (warn_unused_label)
15977 warn_unused_fortran_label (ns->st_labels);
15978
15979 gfc_resolve_uops (ns->uop_root);
15980
15981 gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
15982
15983 gfc_resolve_omp_declare_simd (ns);
15984
15985 gfc_resolve_omp_udrs (ns->omp_udr_root);
15986
15987 ns->types_resolved = 1;
15988
15989 gfc_current_ns = old_ns;
15990 }
15991
15992
15993 /* Call gfc_resolve_code recursively. */
15994
15995 static void
15996 resolve_codes (gfc_namespace *ns)
15997 {
15998 gfc_namespace *n;
15999 bitmap_obstack old_obstack;
16000
16001 if (ns->resolved == 1)
16002 return;
16003
16004 for (n = ns->contained; n; n = n->sibling)
16005 resolve_codes (n);
16006
16007 gfc_current_ns = ns;
16008
16009 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
16010 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
16011 cs_base = NULL;
16012
16013 /* Set to an out of range value. */
16014 current_entry_id = -1;
16015
16016 old_obstack = labels_obstack;
16017 bitmap_obstack_initialize (&labels_obstack);
16018
16019 gfc_resolve_oacc_declare (ns);
16020 gfc_resolve_code (ns->code, ns);
16021
16022 bitmap_obstack_release (&labels_obstack);
16023 labels_obstack = old_obstack;
16024 }
16025
16026
16027 /* This function is called after a complete program unit has been compiled.
16028 Its purpose is to examine all of the expressions associated with a program
16029 unit, assign types to all intermediate expressions, make sure that all
16030 assignments are to compatible types and figure out which names refer to
16031 which functions or subroutines. */
16032
16033 void
16034 gfc_resolve (gfc_namespace *ns)
16035 {
16036 gfc_namespace *old_ns;
16037 code_stack *old_cs_base;
16038 struct gfc_omp_saved_state old_omp_state;
16039
16040 if (ns->resolved)
16041 return;
16042
16043 ns->resolved = -1;
16044 old_ns = gfc_current_ns;
16045 old_cs_base = cs_base;
16046
16047 /* As gfc_resolve can be called during resolution of an OpenMP construct
16048 body, we should clear any state associated to it, so that say NS's
16049 DO loops are not interpreted as OpenMP loops. */
16050 if (!ns->construct_entities)
16051 gfc_omp_save_and_clear_state (&old_omp_state);
16052
16053 resolve_types (ns);
16054 component_assignment_level = 0;
16055 resolve_codes (ns);
16056
16057 gfc_current_ns = old_ns;
16058 cs_base = old_cs_base;
16059 ns->resolved = 1;
16060
16061 gfc_run_passes (ns);
16062
16063 if (!ns->construct_entities)
16064 gfc_omp_restore_state (&old_omp_state);
16065 }