re PR fortran/57966 ([OOP] Using a TBP to specify the shape of a dummy argument)
[gcc.git] / gcc / fortran / resolve.c
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001-2013 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 "flags.h"
25 #include "gfortran.h"
26 #include "obstack.h"
27 #include "bitmap.h"
28 #include "arith.h" /* For gfc_compare_expr(). */
29 #include "dependency.h"
30 #include "data.h"
31 #include "target-memory.h" /* for gfc_simplify_transfer */
32 #include "constructor.h"
33
34 /* Types used in equivalence statements. */
35
36 typedef enum seq_type
37 {
38 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
39 }
40 seq_type;
41
42 /* Stack to keep track of the nesting of blocks as we move through the
43 code. See resolve_branch() and resolve_code(). */
44
45 typedef struct code_stack
46 {
47 struct gfc_code *head, *current;
48 struct code_stack *prev;
49
50 /* This bitmap keeps track of the targets valid for a branch from
51 inside this block except for END {IF|SELECT}s of enclosing
52 blocks. */
53 bitmap reachable_labels;
54 }
55 code_stack;
56
57 static code_stack *cs_base = NULL;
58
59
60 /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
61
62 static int forall_flag;
63 static int do_concurrent_flag;
64
65 /* True when we are resolving an expression that is an actual argument to
66 a procedure. */
67 static bool actual_arg = false;
68 /* True when we are resolving an expression that is the first actual argument
69 to a procedure. */
70 static bool first_actual_arg = false;
71
72
73 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
74
75 static int omp_workshare_flag;
76
77 /* Nonzero if we are processing a formal arglist. The corresponding function
78 resets the flag each time that it is read. */
79 static int formal_arg_flag = 0;
80
81 /* True if we are resolving a specification expression. */
82 static bool specification_expr = false;
83
84 /* The id of the last entry seen. */
85 static int current_entry_id;
86
87 /* We use bitmaps to determine if a branch target is valid. */
88 static bitmap_obstack labels_obstack;
89
90 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
91 static bool inquiry_argument = false;
92
93
94 int
95 gfc_is_formal_arg (void)
96 {
97 return formal_arg_flag;
98 }
99
100 /* Is the symbol host associated? */
101 static bool
102 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
103 {
104 for (ns = ns->parent; ns; ns = ns->parent)
105 {
106 if (sym->ns == ns)
107 return true;
108 }
109
110 return false;
111 }
112
113 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
114 an ABSTRACT derived-type. If where is not NULL, an error message with that
115 locus is printed, optionally using name. */
116
117 static bool
118 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
119 {
120 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
121 {
122 if (where)
123 {
124 if (name)
125 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
126 name, where, ts->u.derived->name);
127 else
128 gfc_error ("ABSTRACT type '%s' used at %L",
129 ts->u.derived->name, where);
130 }
131
132 return false;
133 }
134
135 return true;
136 }
137
138
139 static bool
140 check_proc_interface (gfc_symbol *ifc, locus *where)
141 {
142 /* Several checks for F08:C1216. */
143 if (ifc->attr.procedure)
144 {
145 gfc_error ("Interface '%s' at %L is declared "
146 "in a later PROCEDURE statement", ifc->name, where);
147 return false;
148 }
149 if (ifc->generic)
150 {
151 /* For generic interfaces, check if there is
152 a specific procedure with the same name. */
153 gfc_interface *gen = ifc->generic;
154 while (gen && strcmp (gen->sym->name, ifc->name) != 0)
155 gen = gen->next;
156 if (!gen)
157 {
158 gfc_error ("Interface '%s' at %L may not be generic",
159 ifc->name, where);
160 return false;
161 }
162 }
163 if (ifc->attr.proc == PROC_ST_FUNCTION)
164 {
165 gfc_error ("Interface '%s' at %L may not be a statement function",
166 ifc->name, where);
167 return false;
168 }
169 if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
170 || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
171 ifc->attr.intrinsic = 1;
172 if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
173 {
174 gfc_error ("Intrinsic procedure '%s' not allowed in "
175 "PROCEDURE statement at %L", ifc->name, where);
176 return false;
177 }
178 if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
179 {
180 gfc_error ("Interface '%s' at %L must be explicit", ifc->name, where);
181 return false;
182 }
183 return true;
184 }
185
186
187 static void resolve_symbol (gfc_symbol *sym);
188
189
190 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
191
192 static bool
193 resolve_procedure_interface (gfc_symbol *sym)
194 {
195 gfc_symbol *ifc = sym->ts.interface;
196
197 if (!ifc)
198 return true;
199
200 if (ifc == sym)
201 {
202 gfc_error ("PROCEDURE '%s' at %L may not be used as its own interface",
203 sym->name, &sym->declared_at);
204 return false;
205 }
206 if (!check_proc_interface (ifc, &sym->declared_at))
207 return false;
208
209 if (ifc->attr.if_source || ifc->attr.intrinsic)
210 {
211 /* Resolve interface and copy attributes. */
212 resolve_symbol (ifc);
213 if (ifc->attr.intrinsic)
214 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
215
216 if (ifc->result)
217 {
218 sym->ts = ifc->result->ts;
219 sym->result = sym;
220 }
221 else
222 sym->ts = ifc->ts;
223 sym->ts.interface = ifc;
224 sym->attr.function = ifc->attr.function;
225 sym->attr.subroutine = ifc->attr.subroutine;
226
227 sym->attr.allocatable = ifc->attr.allocatable;
228 sym->attr.pointer = ifc->attr.pointer;
229 sym->attr.pure = ifc->attr.pure;
230 sym->attr.elemental = ifc->attr.elemental;
231 sym->attr.dimension = ifc->attr.dimension;
232 sym->attr.contiguous = ifc->attr.contiguous;
233 sym->attr.recursive = ifc->attr.recursive;
234 sym->attr.always_explicit = ifc->attr.always_explicit;
235 sym->attr.ext_attr |= ifc->attr.ext_attr;
236 sym->attr.is_bind_c = ifc->attr.is_bind_c;
237 sym->attr.class_ok = ifc->attr.class_ok;
238 /* Copy array spec. */
239 sym->as = gfc_copy_array_spec (ifc->as);
240 /* Copy char length. */
241 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
242 {
243 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
244 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
245 && !gfc_resolve_expr (sym->ts.u.cl->length))
246 return false;
247 }
248 }
249
250 return true;
251 }
252
253
254 /* Resolve types of formal argument lists. These have to be done early so that
255 the formal argument lists of module procedures can be copied to the
256 containing module before the individual procedures are resolved
257 individually. We also resolve argument lists of procedures in interface
258 blocks because they are self-contained scoping units.
259
260 Since a dummy argument cannot be a non-dummy procedure, the only
261 resort left for untyped names are the IMPLICIT types. */
262
263 static void
264 resolve_formal_arglist (gfc_symbol *proc)
265 {
266 gfc_formal_arglist *f;
267 gfc_symbol *sym;
268 bool saved_specification_expr;
269 int i;
270
271 if (proc->result != NULL)
272 sym = proc->result;
273 else
274 sym = proc;
275
276 if (gfc_elemental (proc)
277 || sym->attr.pointer || sym->attr.allocatable
278 || (sym->as && sym->as->rank != 0))
279 {
280 proc->attr.always_explicit = 1;
281 sym->attr.always_explicit = 1;
282 }
283
284 formal_arg_flag = 1;
285
286 for (f = proc->formal; f; f = f->next)
287 {
288 gfc_array_spec *as;
289
290 sym = f->sym;
291
292 if (sym == NULL)
293 {
294 /* Alternate return placeholder. */
295 if (gfc_elemental (proc))
296 gfc_error ("Alternate return specifier in elemental subroutine "
297 "'%s' at %L is not allowed", proc->name,
298 &proc->declared_at);
299 if (proc->attr.function)
300 gfc_error ("Alternate return specifier in function "
301 "'%s' at %L is not allowed", proc->name,
302 &proc->declared_at);
303 continue;
304 }
305 else if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
306 && !resolve_procedure_interface (sym))
307 return;
308
309 if (strcmp (proc->name, sym->name) == 0)
310 {
311 gfc_error ("Self-referential argument "
312 "'%s' at %L is not allowed", sym->name,
313 &proc->declared_at);
314 return;
315 }
316
317 if (sym->attr.if_source != IFSRC_UNKNOWN)
318 resolve_formal_arglist (sym);
319
320 if (sym->attr.subroutine || sym->attr.external)
321 {
322 if (sym->attr.flavor == FL_UNKNOWN)
323 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
324 }
325 else
326 {
327 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
328 && (!sym->attr.function || sym->result == sym))
329 gfc_set_default_type (sym, 1, sym->ns);
330 }
331
332 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
333 ? CLASS_DATA (sym)->as : sym->as;
334
335 saved_specification_expr = specification_expr;
336 specification_expr = true;
337 gfc_resolve_array_spec (as, 0);
338 specification_expr = saved_specification_expr;
339
340 /* We can't tell if an array with dimension (:) is assumed or deferred
341 shape until we know if it has the pointer or allocatable attributes.
342 */
343 if (as && as->rank > 0 && as->type == AS_DEFERRED
344 && ((sym->ts.type != BT_CLASS
345 && !(sym->attr.pointer || sym->attr.allocatable))
346 || (sym->ts.type == BT_CLASS
347 && !(CLASS_DATA (sym)->attr.class_pointer
348 || CLASS_DATA (sym)->attr.allocatable)))
349 && sym->attr.flavor != FL_PROCEDURE)
350 {
351 as->type = AS_ASSUMED_SHAPE;
352 for (i = 0; i < as->rank; i++)
353 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
354 }
355
356 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
357 || (as && as->type == AS_ASSUMED_RANK)
358 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
359 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
360 && (CLASS_DATA (sym)->attr.class_pointer
361 || CLASS_DATA (sym)->attr.allocatable
362 || CLASS_DATA (sym)->attr.target))
363 || sym->attr.optional)
364 {
365 proc->attr.always_explicit = 1;
366 if (proc->result)
367 proc->result->attr.always_explicit = 1;
368 }
369
370 /* If the flavor is unknown at this point, it has to be a variable.
371 A procedure specification would have already set the type. */
372
373 if (sym->attr.flavor == FL_UNKNOWN)
374 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
375
376 if (gfc_pure (proc))
377 {
378 if (sym->attr.flavor == FL_PROCEDURE)
379 {
380 /* F08:C1279. */
381 if (!gfc_pure (sym))
382 {
383 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
384 "also be PURE", sym->name, &sym->declared_at);
385 continue;
386 }
387 }
388 else if (!sym->attr.pointer)
389 {
390 if (proc->attr.function && sym->attr.intent != INTENT_IN)
391 {
392 if (sym->attr.value)
393 gfc_notify_std (GFC_STD_F2008, "Argument '%s'"
394 " of pure function '%s' at %L with VALUE "
395 "attribute but without INTENT(IN)",
396 sym->name, proc->name, &sym->declared_at);
397 else
398 gfc_error ("Argument '%s' of pure function '%s' at %L must "
399 "be INTENT(IN) or VALUE", sym->name, proc->name,
400 &sym->declared_at);
401 }
402
403 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
404 {
405 if (sym->attr.value)
406 gfc_notify_std (GFC_STD_F2008, "Argument '%s'"
407 " of pure subroutine '%s' at %L with VALUE "
408 "attribute but without INTENT", sym->name,
409 proc->name, &sym->declared_at);
410 else
411 gfc_error ("Argument '%s' of pure subroutine '%s' at %L "
412 "must have its INTENT specified or have the "
413 "VALUE attribute", sym->name, proc->name,
414 &sym->declared_at);
415 }
416 }
417 }
418
419 if (proc->attr.implicit_pure)
420 {
421 if (sym->attr.flavor == FL_PROCEDURE)
422 {
423 if (!gfc_pure (sym))
424 proc->attr.implicit_pure = 0;
425 }
426 else if (!sym->attr.pointer)
427 {
428 if (proc->attr.function && sym->attr.intent != INTENT_IN
429 && !sym->value)
430 proc->attr.implicit_pure = 0;
431
432 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
433 && !sym->value)
434 proc->attr.implicit_pure = 0;
435 }
436 }
437
438 if (gfc_elemental (proc))
439 {
440 /* F08:C1289. */
441 if (sym->attr.codimension
442 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
443 && CLASS_DATA (sym)->attr.codimension))
444 {
445 gfc_error ("Coarray dummy argument '%s' at %L to elemental "
446 "procedure", sym->name, &sym->declared_at);
447 continue;
448 }
449
450 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
451 && CLASS_DATA (sym)->as))
452 {
453 gfc_error ("Argument '%s' of elemental procedure at %L must "
454 "be scalar", sym->name, &sym->declared_at);
455 continue;
456 }
457
458 if (sym->attr.allocatable
459 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
460 && CLASS_DATA (sym)->attr.allocatable))
461 {
462 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
463 "have the ALLOCATABLE attribute", sym->name,
464 &sym->declared_at);
465 continue;
466 }
467
468 if (sym->attr.pointer
469 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
470 && CLASS_DATA (sym)->attr.class_pointer))
471 {
472 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
473 "have the POINTER attribute", sym->name,
474 &sym->declared_at);
475 continue;
476 }
477
478 if (sym->attr.flavor == FL_PROCEDURE)
479 {
480 gfc_error ("Dummy procedure '%s' not allowed in elemental "
481 "procedure '%s' at %L", sym->name, proc->name,
482 &sym->declared_at);
483 continue;
484 }
485
486 /* Fortran 2008 Corrigendum 1, C1290a. */
487 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
488 {
489 gfc_error ("Argument '%s' of elemental procedure '%s' at %L must "
490 "have its INTENT specified or have the VALUE "
491 "attribute", sym->name, proc->name,
492 &sym->declared_at);
493 continue;
494 }
495 }
496
497 /* Each dummy shall be specified to be scalar. */
498 if (proc->attr.proc == PROC_ST_FUNCTION)
499 {
500 if (sym->as != NULL)
501 {
502 gfc_error ("Argument '%s' of statement function at %L must "
503 "be scalar", sym->name, &sym->declared_at);
504 continue;
505 }
506
507 if (sym->ts.type == BT_CHARACTER)
508 {
509 gfc_charlen *cl = sym->ts.u.cl;
510 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
511 {
512 gfc_error ("Character-valued argument '%s' of statement "
513 "function at %L must have constant length",
514 sym->name, &sym->declared_at);
515 continue;
516 }
517 }
518 }
519 }
520 formal_arg_flag = 0;
521 }
522
523
524 /* Work function called when searching for symbols that have argument lists
525 associated with them. */
526
527 static void
528 find_arglists (gfc_symbol *sym)
529 {
530 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
531 || sym->attr.flavor == FL_DERIVED || sym->attr.intrinsic)
532 return;
533
534 resolve_formal_arglist (sym);
535 }
536
537
538 /* Given a namespace, resolve all formal argument lists within the namespace.
539 */
540
541 static void
542 resolve_formal_arglists (gfc_namespace *ns)
543 {
544 if (ns == NULL)
545 return;
546
547 gfc_traverse_ns (ns, find_arglists);
548 }
549
550
551 static void
552 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
553 {
554 bool t;
555
556 /* If this namespace is not a function or an entry master function,
557 ignore it. */
558 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
559 || sym->attr.entry_master)
560 return;
561
562 /* Try to find out of what the return type is. */
563 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
564 {
565 t = gfc_set_default_type (sym->result, 0, ns);
566
567 if (!t && !sym->result->attr.untyped)
568 {
569 if (sym->result == sym)
570 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
571 sym->name, &sym->declared_at);
572 else if (!sym->result->attr.proc_pointer)
573 gfc_error ("Result '%s' of contained function '%s' at %L has "
574 "no IMPLICIT type", sym->result->name, sym->name,
575 &sym->result->declared_at);
576 sym->result->attr.untyped = 1;
577 }
578 }
579
580 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
581 type, lists the only ways a character length value of * can be used:
582 dummy arguments of procedures, named constants, and function results
583 in external functions. Internal function results and results of module
584 procedures are not on this list, ergo, not permitted. */
585
586 if (sym->result->ts.type == BT_CHARACTER)
587 {
588 gfc_charlen *cl = sym->result->ts.u.cl;
589 if ((!cl || !cl->length) && !sym->result->ts.deferred)
590 {
591 /* See if this is a module-procedure and adapt error message
592 accordingly. */
593 bool module_proc;
594 gcc_assert (ns->parent && ns->parent->proc_name);
595 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
596
597 gfc_error ("Character-valued %s '%s' at %L must not be"
598 " assumed length",
599 module_proc ? _("module procedure")
600 : _("internal function"),
601 sym->name, &sym->declared_at);
602 }
603 }
604 }
605
606
607 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
608 introduce duplicates. */
609
610 static void
611 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
612 {
613 gfc_formal_arglist *f, *new_arglist;
614 gfc_symbol *new_sym;
615
616 for (; new_args != NULL; new_args = new_args->next)
617 {
618 new_sym = new_args->sym;
619 /* See if this arg is already in the formal argument list. */
620 for (f = proc->formal; f; f = f->next)
621 {
622 if (new_sym == f->sym)
623 break;
624 }
625
626 if (f)
627 continue;
628
629 /* Add a new argument. Argument order is not important. */
630 new_arglist = gfc_get_formal_arglist ();
631 new_arglist->sym = new_sym;
632 new_arglist->next = proc->formal;
633 proc->formal = new_arglist;
634 }
635 }
636
637
638 /* Flag the arguments that are not present in all entries. */
639
640 static void
641 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
642 {
643 gfc_formal_arglist *f, *head;
644 head = new_args;
645
646 for (f = proc->formal; f; f = f->next)
647 {
648 if (f->sym == NULL)
649 continue;
650
651 for (new_args = head; new_args; new_args = new_args->next)
652 {
653 if (new_args->sym == f->sym)
654 break;
655 }
656
657 if (new_args)
658 continue;
659
660 f->sym->attr.not_always_present = 1;
661 }
662 }
663
664
665 /* Resolve alternate entry points. If a symbol has multiple entry points we
666 create a new master symbol for the main routine, and turn the existing
667 symbol into an entry point. */
668
669 static void
670 resolve_entries (gfc_namespace *ns)
671 {
672 gfc_namespace *old_ns;
673 gfc_code *c;
674 gfc_symbol *proc;
675 gfc_entry_list *el;
676 char name[GFC_MAX_SYMBOL_LEN + 1];
677 static int master_count = 0;
678
679 if (ns->proc_name == NULL)
680 return;
681
682 /* No need to do anything if this procedure doesn't have alternate entry
683 points. */
684 if (!ns->entries)
685 return;
686
687 /* We may already have resolved alternate entry points. */
688 if (ns->proc_name->attr.entry_master)
689 return;
690
691 /* If this isn't a procedure something has gone horribly wrong. */
692 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
693
694 /* Remember the current namespace. */
695 old_ns = gfc_current_ns;
696
697 gfc_current_ns = ns;
698
699 /* Add the main entry point to the list of entry points. */
700 el = gfc_get_entry_list ();
701 el->sym = ns->proc_name;
702 el->id = 0;
703 el->next = ns->entries;
704 ns->entries = el;
705 ns->proc_name->attr.entry = 1;
706
707 /* If it is a module function, it needs to be in the right namespace
708 so that gfc_get_fake_result_decl can gather up the results. The
709 need for this arose in get_proc_name, where these beasts were
710 left in their own namespace, to keep prior references linked to
711 the entry declaration.*/
712 if (ns->proc_name->attr.function
713 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
714 el->sym->ns = ns;
715
716 /* Do the same for entries where the master is not a module
717 procedure. These are retained in the module namespace because
718 of the module procedure declaration. */
719 for (el = el->next; el; el = el->next)
720 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
721 && el->sym->attr.mod_proc)
722 el->sym->ns = ns;
723 el = ns->entries;
724
725 /* Add an entry statement for it. */
726 c = gfc_get_code ();
727 c->op = EXEC_ENTRY;
728 c->ext.entry = el;
729 c->next = ns->code;
730 ns->code = c;
731
732 /* Create a new symbol for the master function. */
733 /* Give the internal function a unique name (within this file).
734 Also include the function name so the user has some hope of figuring
735 out what is going on. */
736 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
737 master_count++, ns->proc_name->name);
738 gfc_get_ha_symbol (name, &proc);
739 gcc_assert (proc != NULL);
740
741 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
742 if (ns->proc_name->attr.subroutine)
743 gfc_add_subroutine (&proc->attr, proc->name, NULL);
744 else
745 {
746 gfc_symbol *sym;
747 gfc_typespec *ts, *fts;
748 gfc_array_spec *as, *fas;
749 gfc_add_function (&proc->attr, proc->name, NULL);
750 proc->result = proc;
751 fas = ns->entries->sym->as;
752 fas = fas ? fas : ns->entries->sym->result->as;
753 fts = &ns->entries->sym->result->ts;
754 if (fts->type == BT_UNKNOWN)
755 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
756 for (el = ns->entries->next; el; el = el->next)
757 {
758 ts = &el->sym->result->ts;
759 as = el->sym->as;
760 as = as ? as : el->sym->result->as;
761 if (ts->type == BT_UNKNOWN)
762 ts = gfc_get_default_type (el->sym->result->name, NULL);
763
764 if (! gfc_compare_types (ts, fts)
765 || (el->sym->result->attr.dimension
766 != ns->entries->sym->result->attr.dimension)
767 || (el->sym->result->attr.pointer
768 != ns->entries->sym->result->attr.pointer))
769 break;
770 else if (as && fas && ns->entries->sym->result != el->sym->result
771 && gfc_compare_array_spec (as, fas) == 0)
772 gfc_error ("Function %s at %L has entries with mismatched "
773 "array specifications", ns->entries->sym->name,
774 &ns->entries->sym->declared_at);
775 /* The characteristics need to match and thus both need to have
776 the same string length, i.e. both len=*, or both len=4.
777 Having both len=<variable> is also possible, but difficult to
778 check at compile time. */
779 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
780 && (((ts->u.cl->length && !fts->u.cl->length)
781 ||(!ts->u.cl->length && fts->u.cl->length))
782 || (ts->u.cl->length
783 && ts->u.cl->length->expr_type
784 != fts->u.cl->length->expr_type)
785 || (ts->u.cl->length
786 && ts->u.cl->length->expr_type == EXPR_CONSTANT
787 && mpz_cmp (ts->u.cl->length->value.integer,
788 fts->u.cl->length->value.integer) != 0)))
789 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
790 "entries returning variables of different "
791 "string lengths", ns->entries->sym->name,
792 &ns->entries->sym->declared_at);
793 }
794
795 if (el == NULL)
796 {
797 sym = ns->entries->sym->result;
798 /* All result types the same. */
799 proc->ts = *fts;
800 if (sym->attr.dimension)
801 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
802 if (sym->attr.pointer)
803 gfc_add_pointer (&proc->attr, NULL);
804 }
805 else
806 {
807 /* Otherwise the result will be passed through a union by
808 reference. */
809 proc->attr.mixed_entry_master = 1;
810 for (el = ns->entries; el; el = el->next)
811 {
812 sym = el->sym->result;
813 if (sym->attr.dimension)
814 {
815 if (el == ns->entries)
816 gfc_error ("FUNCTION result %s can't be an array in "
817 "FUNCTION %s at %L", sym->name,
818 ns->entries->sym->name, &sym->declared_at);
819 else
820 gfc_error ("ENTRY result %s can't be an array in "
821 "FUNCTION %s at %L", sym->name,
822 ns->entries->sym->name, &sym->declared_at);
823 }
824 else if (sym->attr.pointer)
825 {
826 if (el == ns->entries)
827 gfc_error ("FUNCTION result %s can't be a POINTER in "
828 "FUNCTION %s at %L", sym->name,
829 ns->entries->sym->name, &sym->declared_at);
830 else
831 gfc_error ("ENTRY result %s can't be a POINTER in "
832 "FUNCTION %s at %L", sym->name,
833 ns->entries->sym->name, &sym->declared_at);
834 }
835 else
836 {
837 ts = &sym->ts;
838 if (ts->type == BT_UNKNOWN)
839 ts = gfc_get_default_type (sym->name, NULL);
840 switch (ts->type)
841 {
842 case BT_INTEGER:
843 if (ts->kind == gfc_default_integer_kind)
844 sym = NULL;
845 break;
846 case BT_REAL:
847 if (ts->kind == gfc_default_real_kind
848 || ts->kind == gfc_default_double_kind)
849 sym = NULL;
850 break;
851 case BT_COMPLEX:
852 if (ts->kind == gfc_default_complex_kind)
853 sym = NULL;
854 break;
855 case BT_LOGICAL:
856 if (ts->kind == gfc_default_logical_kind)
857 sym = NULL;
858 break;
859 case BT_UNKNOWN:
860 /* We will issue error elsewhere. */
861 sym = NULL;
862 break;
863 default:
864 break;
865 }
866 if (sym)
867 {
868 if (el == ns->entries)
869 gfc_error ("FUNCTION result %s can't be of type %s "
870 "in FUNCTION %s at %L", sym->name,
871 gfc_typename (ts), ns->entries->sym->name,
872 &sym->declared_at);
873 else
874 gfc_error ("ENTRY result %s can't be of type %s "
875 "in FUNCTION %s at %L", sym->name,
876 gfc_typename (ts), ns->entries->sym->name,
877 &sym->declared_at);
878 }
879 }
880 }
881 }
882 }
883 proc->attr.access = ACCESS_PRIVATE;
884 proc->attr.entry_master = 1;
885
886 /* Merge all the entry point arguments. */
887 for (el = ns->entries; el; el = el->next)
888 merge_argument_lists (proc, el->sym->formal);
889
890 /* Check the master formal arguments for any that are not
891 present in all entry points. */
892 for (el = ns->entries; el; el = el->next)
893 check_argument_lists (proc, el->sym->formal);
894
895 /* Use the master function for the function body. */
896 ns->proc_name = proc;
897
898 /* Finalize the new symbols. */
899 gfc_commit_symbols ();
900
901 /* Restore the original namespace. */
902 gfc_current_ns = old_ns;
903 }
904
905
906 /* Resolve common variables. */
907 static void
908 resolve_common_vars (gfc_symbol *sym, bool named_common)
909 {
910 gfc_symbol *csym = sym;
911
912 for (; csym; csym = csym->common_next)
913 {
914 if (csym->value || csym->attr.data)
915 {
916 if (!csym->ns->is_block_data)
917 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
918 "but only in BLOCK DATA initialization is "
919 "allowed", csym->name, &csym->declared_at);
920 else if (!named_common)
921 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
922 "in a blank COMMON but initialization is only "
923 "allowed in named common blocks", csym->name,
924 &csym->declared_at);
925 }
926
927 if (UNLIMITED_POLY (csym))
928 gfc_error_now ("'%s' in cannot appear in COMMON at %L "
929 "[F2008:C5100]", csym->name, &csym->declared_at);
930
931 if (csym->ts.type != BT_DERIVED)
932 continue;
933
934 if (!(csym->ts.u.derived->attr.sequence
935 || csym->ts.u.derived->attr.is_bind_c))
936 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
937 "has neither the SEQUENCE nor the BIND(C) "
938 "attribute", csym->name, &csym->declared_at);
939 if (csym->ts.u.derived->attr.alloc_comp)
940 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
941 "has an ultimate component that is "
942 "allocatable", csym->name, &csym->declared_at);
943 if (gfc_has_default_initializer (csym->ts.u.derived))
944 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
945 "may not have default initializer", csym->name,
946 &csym->declared_at);
947
948 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
949 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
950 }
951 }
952
953 /* Resolve common blocks. */
954 static void
955 resolve_common_blocks (gfc_symtree *common_root)
956 {
957 gfc_symbol *sym;
958 gfc_gsymbol * gsym;
959
960 if (common_root == NULL)
961 return;
962
963 if (common_root->left)
964 resolve_common_blocks (common_root->left);
965 if (common_root->right)
966 resolve_common_blocks (common_root->right);
967
968 resolve_common_vars (common_root->n.common->head, true);
969
970 /* The common name is a global name - in Fortran 2003 also if it has a
971 C binding name, since Fortran 2008 only the C binding name is a global
972 identifier. */
973 if (!common_root->n.common->binding_label
974 || gfc_notification_std (GFC_STD_F2008))
975 {
976 gsym = gfc_find_gsymbol (gfc_gsym_root,
977 common_root->n.common->name);
978
979 if (gsym && gfc_notification_std (GFC_STD_F2008)
980 && gsym->type == GSYM_COMMON
981 && ((common_root->n.common->binding_label
982 && (!gsym->binding_label
983 || strcmp (common_root->n.common->binding_label,
984 gsym->binding_label) != 0))
985 || (!common_root->n.common->binding_label
986 && gsym->binding_label)))
987 {
988 gfc_error ("In Fortran 2003 COMMON '%s' block at %L is a global "
989 "identifier and must thus have the same binding name "
990 "as the same-named COMMON block at %L: %s vs %s",
991 common_root->n.common->name, &common_root->n.common->where,
992 &gsym->where,
993 common_root->n.common->binding_label
994 ? common_root->n.common->binding_label : "(blank)",
995 gsym->binding_label ? gsym->binding_label : "(blank)");
996 return;
997 }
998
999 if (gsym && gsym->type != GSYM_COMMON
1000 && !common_root->n.common->binding_label)
1001 {
1002 gfc_error ("COMMON block '%s' at %L uses the same global identifier "
1003 "as entity at %L",
1004 common_root->n.common->name, &common_root->n.common->where,
1005 &gsym->where);
1006 return;
1007 }
1008 if (gsym && gsym->type != GSYM_COMMON)
1009 {
1010 gfc_error ("Fortran 2008: COMMON block '%s' with binding label at "
1011 "%L sharing the identifier with global non-COMMON-block "
1012 "entity at %L", common_root->n.common->name,
1013 &common_root->n.common->where, &gsym->where);
1014 return;
1015 }
1016 if (!gsym)
1017 {
1018 gsym = gfc_get_gsymbol (common_root->n.common->name);
1019 gsym->type = GSYM_COMMON;
1020 gsym->where = common_root->n.common->where;
1021 gsym->defined = 1;
1022 }
1023 gsym->used = 1;
1024 }
1025
1026 if (common_root->n.common->binding_label)
1027 {
1028 gsym = gfc_find_gsymbol (gfc_gsym_root,
1029 common_root->n.common->binding_label);
1030 if (gsym && gsym->type != GSYM_COMMON)
1031 {
1032 gfc_error ("COMMON block at %L with binding label %s uses the same "
1033 "global identifier as entity at %L",
1034 &common_root->n.common->where,
1035 common_root->n.common->binding_label, &gsym->where);
1036 return;
1037 }
1038 if (!gsym)
1039 {
1040 gsym = gfc_get_gsymbol (common_root->n.common->binding_label);
1041 gsym->type = GSYM_COMMON;
1042 gsym->where = common_root->n.common->where;
1043 gsym->defined = 1;
1044 }
1045 gsym->used = 1;
1046 }
1047
1048 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1049 if (sym == NULL)
1050 return;
1051
1052 if (sym->attr.flavor == FL_PARAMETER)
1053 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
1054 sym->name, &common_root->n.common->where, &sym->declared_at);
1055
1056 if (sym->attr.external)
1057 gfc_error ("COMMON block '%s' at %L can not have the EXTERNAL attribute",
1058 sym->name, &common_root->n.common->where);
1059
1060 if (sym->attr.intrinsic)
1061 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
1062 sym->name, &common_root->n.common->where);
1063 else if (sym->attr.result
1064 || gfc_is_function_return_value (sym, gfc_current_ns))
1065 gfc_notify_std (GFC_STD_F2003, "COMMON block '%s' at %L "
1066 "that is also a function result", sym->name,
1067 &common_root->n.common->where);
1068 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1069 && sym->attr.proc != PROC_ST_FUNCTION)
1070 gfc_notify_std (GFC_STD_F2003, "COMMON block '%s' at %L "
1071 "that is also a global procedure", sym->name,
1072 &common_root->n.common->where);
1073 }
1074
1075
1076 /* Resolve contained function types. Because contained functions can call one
1077 another, they have to be worked out before any of the contained procedures
1078 can be resolved.
1079
1080 The good news is that if a function doesn't already have a type, the only
1081 way it can get one is through an IMPLICIT type or a RESULT variable, because
1082 by definition contained functions are contained namespace they're contained
1083 in, not in a sibling or parent namespace. */
1084
1085 static void
1086 resolve_contained_functions (gfc_namespace *ns)
1087 {
1088 gfc_namespace *child;
1089 gfc_entry_list *el;
1090
1091 resolve_formal_arglists (ns);
1092
1093 for (child = ns->contained; child; child = child->sibling)
1094 {
1095 /* Resolve alternate entry points first. */
1096 resolve_entries (child);
1097
1098 /* Then check function return types. */
1099 resolve_contained_fntype (child->proc_name, child);
1100 for (el = child->entries; el; el = el->next)
1101 resolve_contained_fntype (el->sym, child);
1102 }
1103 }
1104
1105
1106 static bool resolve_fl_derived0 (gfc_symbol *sym);
1107
1108
1109 /* Resolve all of the elements of a structure constructor and make sure that
1110 the types are correct. The 'init' flag indicates that the given
1111 constructor is an initializer. */
1112
1113 static bool
1114 resolve_structure_cons (gfc_expr *expr, int init)
1115 {
1116 gfc_constructor *cons;
1117 gfc_component *comp;
1118 bool t;
1119 symbol_attribute a;
1120
1121 t = true;
1122
1123 if (expr->ts.type == BT_DERIVED)
1124 resolve_fl_derived0 (expr->ts.u.derived);
1125
1126 cons = gfc_constructor_first (expr->value.constructor);
1127
1128 /* A constructor may have references if it is the result of substituting a
1129 parameter variable. In this case we just pull out the component we
1130 want. */
1131 if (expr->ref)
1132 comp = expr->ref->u.c.sym->components;
1133 else
1134 comp = expr->ts.u.derived->components;
1135
1136 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1137 {
1138 int rank;
1139
1140 if (!cons->expr)
1141 continue;
1142
1143 if (!gfc_resolve_expr (cons->expr))
1144 {
1145 t = false;
1146 continue;
1147 }
1148
1149 rank = comp->as ? comp->as->rank : 0;
1150 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1151 && (comp->attr.allocatable || cons->expr->rank))
1152 {
1153 gfc_error ("The rank of the element in the structure "
1154 "constructor at %L does not match that of the "
1155 "component (%d/%d)", &cons->expr->where,
1156 cons->expr->rank, rank);
1157 t = false;
1158 }
1159
1160 /* If we don't have the right type, try to convert it. */
1161
1162 if (!comp->attr.proc_pointer &&
1163 !gfc_compare_types (&cons->expr->ts, &comp->ts))
1164 {
1165 if (strcmp (comp->name, "_extends") == 0)
1166 {
1167 /* Can afford to be brutal with the _extends initializer.
1168 The derived type can get lost because it is PRIVATE
1169 but it is not usage constrained by the standard. */
1170 cons->expr->ts = comp->ts;
1171 }
1172 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1173 {
1174 gfc_error ("The element in the structure constructor at %L, "
1175 "for pointer component '%s', is %s but should be %s",
1176 &cons->expr->where, comp->name,
1177 gfc_basic_typename (cons->expr->ts.type),
1178 gfc_basic_typename (comp->ts.type));
1179 t = false;
1180 }
1181 else
1182 {
1183 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1184 if (t)
1185 t = t2;
1186 }
1187 }
1188
1189 /* For strings, the length of the constructor should be the same as
1190 the one of the structure, ensure this if the lengths are known at
1191 compile time and when we are dealing with PARAMETER or structure
1192 constructors. */
1193 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1194 && comp->ts.u.cl->length
1195 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1196 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1197 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1198 && cons->expr->rank != 0
1199 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1200 comp->ts.u.cl->length->value.integer) != 0)
1201 {
1202 if (cons->expr->expr_type == EXPR_VARIABLE
1203 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1204 {
1205 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1206 to make use of the gfc_resolve_character_array_constructor
1207 machinery. The expression is later simplified away to
1208 an array of string literals. */
1209 gfc_expr *para = cons->expr;
1210 cons->expr = gfc_get_expr ();
1211 cons->expr->ts = para->ts;
1212 cons->expr->where = para->where;
1213 cons->expr->expr_type = EXPR_ARRAY;
1214 cons->expr->rank = para->rank;
1215 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1216 gfc_constructor_append_expr (&cons->expr->value.constructor,
1217 para, &cons->expr->where);
1218 }
1219 if (cons->expr->expr_type == EXPR_ARRAY)
1220 {
1221 gfc_constructor *p;
1222 p = gfc_constructor_first (cons->expr->value.constructor);
1223 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1224 {
1225 gfc_charlen *cl, *cl2;
1226
1227 cl2 = NULL;
1228 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1229 {
1230 if (cl == cons->expr->ts.u.cl)
1231 break;
1232 cl2 = cl;
1233 }
1234
1235 gcc_assert (cl);
1236
1237 if (cl2)
1238 cl2->next = cl->next;
1239
1240 gfc_free_expr (cl->length);
1241 free (cl);
1242 }
1243
1244 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1245 cons->expr->ts.u.cl->length_from_typespec = true;
1246 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1247 gfc_resolve_character_array_constructor (cons->expr);
1248 }
1249 }
1250
1251 if (cons->expr->expr_type == EXPR_NULL
1252 && !(comp->attr.pointer || comp->attr.allocatable
1253 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1254 || (comp->ts.type == BT_CLASS
1255 && (CLASS_DATA (comp)->attr.class_pointer
1256 || CLASS_DATA (comp)->attr.allocatable))))
1257 {
1258 t = false;
1259 gfc_error ("The NULL in the structure constructor at %L is "
1260 "being applied to component '%s', which is neither "
1261 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1262 comp->name);
1263 }
1264
1265 if (comp->attr.proc_pointer && comp->ts.interface)
1266 {
1267 /* Check procedure pointer interface. */
1268 gfc_symbol *s2 = NULL;
1269 gfc_component *c2;
1270 const char *name;
1271 char err[200];
1272
1273 c2 = gfc_get_proc_ptr_comp (cons->expr);
1274 if (c2)
1275 {
1276 s2 = c2->ts.interface;
1277 name = c2->name;
1278 }
1279 else if (cons->expr->expr_type == EXPR_FUNCTION)
1280 {
1281 s2 = cons->expr->symtree->n.sym->result;
1282 name = cons->expr->symtree->n.sym->result->name;
1283 }
1284 else if (cons->expr->expr_type != EXPR_NULL)
1285 {
1286 s2 = cons->expr->symtree->n.sym;
1287 name = cons->expr->symtree->n.sym->name;
1288 }
1289
1290 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1291 err, sizeof (err), NULL, NULL))
1292 {
1293 gfc_error ("Interface mismatch for procedure-pointer component "
1294 "'%s' in structure constructor at %L: %s",
1295 comp->name, &cons->expr->where, err);
1296 return false;
1297 }
1298 }
1299
1300 if (!comp->attr.pointer || comp->attr.proc_pointer
1301 || cons->expr->expr_type == EXPR_NULL)
1302 continue;
1303
1304 a = gfc_expr_attr (cons->expr);
1305
1306 if (!a.pointer && !a.target)
1307 {
1308 t = false;
1309 gfc_error ("The element in the structure constructor at %L, "
1310 "for pointer component '%s' should be a POINTER or "
1311 "a TARGET", &cons->expr->where, comp->name);
1312 }
1313
1314 if (init)
1315 {
1316 /* F08:C461. Additional checks for pointer initialization. */
1317 if (a.allocatable)
1318 {
1319 t = false;
1320 gfc_error ("Pointer initialization target at %L "
1321 "must not be ALLOCATABLE ", &cons->expr->where);
1322 }
1323 if (!a.save)
1324 {
1325 t = false;
1326 gfc_error ("Pointer initialization target at %L "
1327 "must have the SAVE attribute", &cons->expr->where);
1328 }
1329 }
1330
1331 /* F2003, C1272 (3). */
1332 if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
1333 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1334 || gfc_is_coindexed (cons->expr)))
1335 {
1336 t = false;
1337 gfc_error ("Invalid expression in the structure constructor for "
1338 "pointer component '%s' at %L in PURE procedure",
1339 comp->name, &cons->expr->where);
1340 }
1341
1342 if (gfc_implicit_pure (NULL)
1343 && cons->expr->expr_type == EXPR_VARIABLE
1344 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1345 || gfc_is_coindexed (cons->expr)))
1346 gfc_current_ns->proc_name->attr.implicit_pure = 0;
1347
1348 }
1349
1350 return t;
1351 }
1352
1353
1354 /****************** Expression name resolution ******************/
1355
1356 /* Returns 0 if a symbol was not declared with a type or
1357 attribute declaration statement, nonzero otherwise. */
1358
1359 static int
1360 was_declared (gfc_symbol *sym)
1361 {
1362 symbol_attribute a;
1363
1364 a = sym->attr;
1365
1366 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1367 return 1;
1368
1369 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1370 || a.optional || a.pointer || a.save || a.target || a.volatile_
1371 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1372 || a.asynchronous || a.codimension)
1373 return 1;
1374
1375 return 0;
1376 }
1377
1378
1379 /* Determine if a symbol is generic or not. */
1380
1381 static int
1382 generic_sym (gfc_symbol *sym)
1383 {
1384 gfc_symbol *s;
1385
1386 if (sym->attr.generic ||
1387 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1388 return 1;
1389
1390 if (was_declared (sym) || sym->ns->parent == NULL)
1391 return 0;
1392
1393 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1394
1395 if (s != NULL)
1396 {
1397 if (s == sym)
1398 return 0;
1399 else
1400 return generic_sym (s);
1401 }
1402
1403 return 0;
1404 }
1405
1406
1407 /* Determine if a symbol is specific or not. */
1408
1409 static int
1410 specific_sym (gfc_symbol *sym)
1411 {
1412 gfc_symbol *s;
1413
1414 if (sym->attr.if_source == IFSRC_IFBODY
1415 || sym->attr.proc == PROC_MODULE
1416 || sym->attr.proc == PROC_INTERNAL
1417 || sym->attr.proc == PROC_ST_FUNCTION
1418 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1419 || sym->attr.external)
1420 return 1;
1421
1422 if (was_declared (sym) || sym->ns->parent == NULL)
1423 return 0;
1424
1425 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1426
1427 return (s == NULL) ? 0 : specific_sym (s);
1428 }
1429
1430
1431 /* Figure out if the procedure is specific, generic or unknown. */
1432
1433 typedef enum
1434 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1435 proc_type;
1436
1437 static proc_type
1438 procedure_kind (gfc_symbol *sym)
1439 {
1440 if (generic_sym (sym))
1441 return PTYPE_GENERIC;
1442
1443 if (specific_sym (sym))
1444 return PTYPE_SPECIFIC;
1445
1446 return PTYPE_UNKNOWN;
1447 }
1448
1449 /* Check references to assumed size arrays. The flag need_full_assumed_size
1450 is nonzero when matching actual arguments. */
1451
1452 static int need_full_assumed_size = 0;
1453
1454 static bool
1455 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1456 {
1457 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1458 return false;
1459
1460 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1461 What should it be? */
1462 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1463 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1464 && (e->ref->u.ar.type == AR_FULL))
1465 {
1466 gfc_error ("The upper bound in the last dimension must "
1467 "appear in the reference to the assumed size "
1468 "array '%s' at %L", sym->name, &e->where);
1469 return true;
1470 }
1471 return false;
1472 }
1473
1474
1475 /* Look for bad assumed size array references in argument expressions
1476 of elemental and array valued intrinsic procedures. Since this is
1477 called from procedure resolution functions, it only recurses at
1478 operators. */
1479
1480 static bool
1481 resolve_assumed_size_actual (gfc_expr *e)
1482 {
1483 if (e == NULL)
1484 return false;
1485
1486 switch (e->expr_type)
1487 {
1488 case EXPR_VARIABLE:
1489 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1490 return true;
1491 break;
1492
1493 case EXPR_OP:
1494 if (resolve_assumed_size_actual (e->value.op.op1)
1495 || resolve_assumed_size_actual (e->value.op.op2))
1496 return true;
1497 break;
1498
1499 default:
1500 break;
1501 }
1502 return false;
1503 }
1504
1505
1506 /* Check a generic procedure, passed as an actual argument, to see if
1507 there is a matching specific name. If none, it is an error, and if
1508 more than one, the reference is ambiguous. */
1509 static int
1510 count_specific_procs (gfc_expr *e)
1511 {
1512 int n;
1513 gfc_interface *p;
1514 gfc_symbol *sym;
1515
1516 n = 0;
1517 sym = e->symtree->n.sym;
1518
1519 for (p = sym->generic; p; p = p->next)
1520 if (strcmp (sym->name, p->sym->name) == 0)
1521 {
1522 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1523 sym->name);
1524 n++;
1525 }
1526
1527 if (n > 1)
1528 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1529 &e->where);
1530
1531 if (n == 0)
1532 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1533 "argument at %L", sym->name, &e->where);
1534
1535 return n;
1536 }
1537
1538
1539 /* See if a call to sym could possibly be a not allowed RECURSION because of
1540 a missing RECURSIVE declaration. This means that either sym is the current
1541 context itself, or sym is the parent of a contained procedure calling its
1542 non-RECURSIVE containing procedure.
1543 This also works if sym is an ENTRY. */
1544
1545 static bool
1546 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1547 {
1548 gfc_symbol* proc_sym;
1549 gfc_symbol* context_proc;
1550 gfc_namespace* real_context;
1551
1552 if (sym->attr.flavor == FL_PROGRAM
1553 || sym->attr.flavor == FL_DERIVED)
1554 return false;
1555
1556 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1557
1558 /* If we've got an ENTRY, find real procedure. */
1559 if (sym->attr.entry && sym->ns->entries)
1560 proc_sym = sym->ns->entries->sym;
1561 else
1562 proc_sym = sym;
1563
1564 /* If sym is RECURSIVE, all is well of course. */
1565 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1566 return false;
1567
1568 /* Find the context procedure's "real" symbol if it has entries.
1569 We look for a procedure symbol, so recurse on the parents if we don't
1570 find one (like in case of a BLOCK construct). */
1571 for (real_context = context; ; real_context = real_context->parent)
1572 {
1573 /* We should find something, eventually! */
1574 gcc_assert (real_context);
1575
1576 context_proc = (real_context->entries ? real_context->entries->sym
1577 : real_context->proc_name);
1578
1579 /* In some special cases, there may not be a proc_name, like for this
1580 invalid code:
1581 real(bad_kind()) function foo () ...
1582 when checking the call to bad_kind ().
1583 In these cases, we simply return here and assume that the
1584 call is ok. */
1585 if (!context_proc)
1586 return false;
1587
1588 if (context_proc->attr.flavor != FL_LABEL)
1589 break;
1590 }
1591
1592 /* A call from sym's body to itself is recursion, of course. */
1593 if (context_proc == proc_sym)
1594 return true;
1595
1596 /* The same is true if context is a contained procedure and sym the
1597 containing one. */
1598 if (context_proc->attr.contained)
1599 {
1600 gfc_symbol* parent_proc;
1601
1602 gcc_assert (context->parent);
1603 parent_proc = (context->parent->entries ? context->parent->entries->sym
1604 : context->parent->proc_name);
1605
1606 if (parent_proc == proc_sym)
1607 return true;
1608 }
1609
1610 return false;
1611 }
1612
1613
1614 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1615 its typespec and formal argument list. */
1616
1617 bool
1618 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1619 {
1620 gfc_intrinsic_sym* isym = NULL;
1621 const char* symstd;
1622
1623 if (sym->formal)
1624 return true;
1625
1626 /* Already resolved. */
1627 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1628 return true;
1629
1630 /* We already know this one is an intrinsic, so we don't call
1631 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1632 gfc_find_subroutine directly to check whether it is a function or
1633 subroutine. */
1634
1635 if (sym->intmod_sym_id && sym->attr.subroutine)
1636 {
1637 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1638 isym = gfc_intrinsic_subroutine_by_id (id);
1639 }
1640 else if (sym->intmod_sym_id)
1641 {
1642 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1643 isym = gfc_intrinsic_function_by_id (id);
1644 }
1645 else if (!sym->attr.subroutine)
1646 isym = gfc_find_function (sym->name);
1647
1648 if (isym && !sym->attr.subroutine)
1649 {
1650 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1651 && !sym->attr.implicit_type)
1652 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1653 " ignored", sym->name, &sym->declared_at);
1654
1655 if (!sym->attr.function &&
1656 !gfc_add_function(&sym->attr, sym->name, loc))
1657 return false;
1658
1659 sym->ts = isym->ts;
1660 }
1661 else if (isym || (isym = gfc_find_subroutine (sym->name)))
1662 {
1663 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1664 {
1665 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1666 " specifier", sym->name, &sym->declared_at);
1667 return false;
1668 }
1669
1670 if (!sym->attr.subroutine &&
1671 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1672 return false;
1673 }
1674 else
1675 {
1676 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1677 &sym->declared_at);
1678 return false;
1679 }
1680
1681 gfc_copy_formal_args_intr (sym, isym);
1682
1683 /* Check it is actually available in the standard settings. */
1684 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1685 {
1686 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1687 " available in the current standard settings but %s. Use"
1688 " an appropriate -std=* option or enable -fall-intrinsics"
1689 " in order to use it.",
1690 sym->name, &sym->declared_at, symstd);
1691 return false;
1692 }
1693
1694 return true;
1695 }
1696
1697
1698 /* Resolve a procedure expression, like passing it to a called procedure or as
1699 RHS for a procedure pointer assignment. */
1700
1701 static bool
1702 resolve_procedure_expression (gfc_expr* expr)
1703 {
1704 gfc_symbol* sym;
1705
1706 if (expr->expr_type != EXPR_VARIABLE)
1707 return true;
1708 gcc_assert (expr->symtree);
1709
1710 sym = expr->symtree->n.sym;
1711
1712 if (sym->attr.intrinsic)
1713 gfc_resolve_intrinsic (sym, &expr->where);
1714
1715 if (sym->attr.flavor != FL_PROCEDURE
1716 || (sym->attr.function && sym->result == sym))
1717 return true;
1718
1719 /* A non-RECURSIVE procedure that is used as procedure expression within its
1720 own body is in danger of being called recursively. */
1721 if (is_illegal_recursion (sym, gfc_current_ns))
1722 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1723 " itself recursively. Declare it RECURSIVE or use"
1724 " -frecursive", sym->name, &expr->where);
1725
1726 return true;
1727 }
1728
1729
1730 /* Resolve an actual argument list. Most of the time, this is just
1731 resolving the expressions in the list.
1732 The exception is that we sometimes have to decide whether arguments
1733 that look like procedure arguments are really simple variable
1734 references. */
1735
1736 static bool
1737 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1738 bool no_formal_args)
1739 {
1740 gfc_symbol *sym;
1741 gfc_symtree *parent_st;
1742 gfc_expr *e;
1743 int save_need_full_assumed_size;
1744 bool return_value = false;
1745 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
1746
1747 actual_arg = true;
1748 first_actual_arg = true;
1749
1750 for (; arg; arg = arg->next)
1751 {
1752 e = arg->expr;
1753 if (e == NULL)
1754 {
1755 /* Check the label is a valid branching target. */
1756 if (arg->label)
1757 {
1758 if (arg->label->defined == ST_LABEL_UNKNOWN)
1759 {
1760 gfc_error ("Label %d referenced at %L is never defined",
1761 arg->label->value, &arg->label->where);
1762 goto cleanup;
1763 }
1764 }
1765 first_actual_arg = false;
1766 continue;
1767 }
1768
1769 if (e->expr_type == EXPR_VARIABLE
1770 && e->symtree->n.sym->attr.generic
1771 && no_formal_args
1772 && count_specific_procs (e) != 1)
1773 goto cleanup;
1774
1775 if (e->ts.type != BT_PROCEDURE)
1776 {
1777 save_need_full_assumed_size = need_full_assumed_size;
1778 if (e->expr_type != EXPR_VARIABLE)
1779 need_full_assumed_size = 0;
1780 if (!gfc_resolve_expr (e))
1781 goto cleanup;
1782 need_full_assumed_size = save_need_full_assumed_size;
1783 goto argument_list;
1784 }
1785
1786 /* See if the expression node should really be a variable reference. */
1787
1788 sym = e->symtree->n.sym;
1789
1790 if (sym->attr.flavor == FL_PROCEDURE
1791 || sym->attr.intrinsic
1792 || sym->attr.external)
1793 {
1794 int actual_ok;
1795
1796 /* If a procedure is not already determined to be something else
1797 check if it is intrinsic. */
1798 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1799 sym->attr.intrinsic = 1;
1800
1801 if (sym->attr.proc == PROC_ST_FUNCTION)
1802 {
1803 gfc_error ("Statement function '%s' at %L is not allowed as an "
1804 "actual argument", sym->name, &e->where);
1805 }
1806
1807 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1808 sym->attr.subroutine);
1809 if (sym->attr.intrinsic && actual_ok == 0)
1810 {
1811 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1812 "actual argument", sym->name, &e->where);
1813 }
1814
1815 if (sym->attr.contained && !sym->attr.use_assoc
1816 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1817 {
1818 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure '%s' is"
1819 " used as actual argument at %L",
1820 sym->name, &e->where))
1821 goto cleanup;
1822 }
1823
1824 if (sym->attr.elemental && !sym->attr.intrinsic)
1825 {
1826 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1827 "allowed as an actual argument at %L", sym->name,
1828 &e->where);
1829 }
1830
1831 /* Check if a generic interface has a specific procedure
1832 with the same name before emitting an error. */
1833 if (sym->attr.generic && count_specific_procs (e) != 1)
1834 goto cleanup;
1835
1836 /* Just in case a specific was found for the expression. */
1837 sym = e->symtree->n.sym;
1838
1839 /* If the symbol is the function that names the current (or
1840 parent) scope, then we really have a variable reference. */
1841
1842 if (gfc_is_function_return_value (sym, sym->ns))
1843 goto got_variable;
1844
1845 /* If all else fails, see if we have a specific intrinsic. */
1846 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1847 {
1848 gfc_intrinsic_sym *isym;
1849
1850 isym = gfc_find_function (sym->name);
1851 if (isym == NULL || !isym->specific)
1852 {
1853 gfc_error ("Unable to find a specific INTRINSIC procedure "
1854 "for the reference '%s' at %L", sym->name,
1855 &e->where);
1856 goto cleanup;
1857 }
1858 sym->ts = isym->ts;
1859 sym->attr.intrinsic = 1;
1860 sym->attr.function = 1;
1861 }
1862
1863 if (!gfc_resolve_expr (e))
1864 goto cleanup;
1865 goto argument_list;
1866 }
1867
1868 /* See if the name is a module procedure in a parent unit. */
1869
1870 if (was_declared (sym) || sym->ns->parent == NULL)
1871 goto got_variable;
1872
1873 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1874 {
1875 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1876 goto cleanup;
1877 }
1878
1879 if (parent_st == NULL)
1880 goto got_variable;
1881
1882 sym = parent_st->n.sym;
1883 e->symtree = parent_st; /* Point to the right thing. */
1884
1885 if (sym->attr.flavor == FL_PROCEDURE
1886 || sym->attr.intrinsic
1887 || sym->attr.external)
1888 {
1889 if (!gfc_resolve_expr (e))
1890 goto cleanup;
1891 goto argument_list;
1892 }
1893
1894 got_variable:
1895 e->expr_type = EXPR_VARIABLE;
1896 e->ts = sym->ts;
1897 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
1898 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
1899 && CLASS_DATA (sym)->as))
1900 {
1901 e->rank = sym->ts.type == BT_CLASS
1902 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
1903 e->ref = gfc_get_ref ();
1904 e->ref->type = REF_ARRAY;
1905 e->ref->u.ar.type = AR_FULL;
1906 e->ref->u.ar.as = sym->ts.type == BT_CLASS
1907 ? CLASS_DATA (sym)->as : sym->as;
1908 }
1909
1910 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1911 primary.c (match_actual_arg). If above code determines that it
1912 is a variable instead, it needs to be resolved as it was not
1913 done at the beginning of this function. */
1914 save_need_full_assumed_size = need_full_assumed_size;
1915 if (e->expr_type != EXPR_VARIABLE)
1916 need_full_assumed_size = 0;
1917 if (!gfc_resolve_expr (e))
1918 goto cleanup;
1919 need_full_assumed_size = save_need_full_assumed_size;
1920
1921 argument_list:
1922 /* Check argument list functions %VAL, %LOC and %REF. There is
1923 nothing to do for %REF. */
1924 if (arg->name && arg->name[0] == '%')
1925 {
1926 if (strncmp ("%VAL", arg->name, 4) == 0)
1927 {
1928 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1929 {
1930 gfc_error ("By-value argument at %L is not of numeric "
1931 "type", &e->where);
1932 goto cleanup;
1933 }
1934
1935 if (e->rank)
1936 {
1937 gfc_error ("By-value argument at %L cannot be an array or "
1938 "an array section", &e->where);
1939 goto cleanup;
1940 }
1941
1942 /* Intrinsics are still PROC_UNKNOWN here. However,
1943 since same file external procedures are not resolvable
1944 in gfortran, it is a good deal easier to leave them to
1945 intrinsic.c. */
1946 if (ptype != PROC_UNKNOWN
1947 && ptype != PROC_DUMMY
1948 && ptype != PROC_EXTERNAL
1949 && ptype != PROC_MODULE)
1950 {
1951 gfc_error ("By-value argument at %L is not allowed "
1952 "in this context", &e->where);
1953 goto cleanup;
1954 }
1955 }
1956
1957 /* Statement functions have already been excluded above. */
1958 else if (strncmp ("%LOC", arg->name, 4) == 0
1959 && e->ts.type == BT_PROCEDURE)
1960 {
1961 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1962 {
1963 gfc_error ("Passing internal procedure at %L by location "
1964 "not allowed", &e->where);
1965 goto cleanup;
1966 }
1967 }
1968 }
1969
1970 /* Fortran 2008, C1237. */
1971 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
1972 && gfc_has_ultimate_pointer (e))
1973 {
1974 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1975 "component", &e->where);
1976 goto cleanup;
1977 }
1978
1979 first_actual_arg = false;
1980 }
1981
1982 return_value = true;
1983
1984 cleanup:
1985 actual_arg = actual_arg_sav;
1986 first_actual_arg = first_actual_arg_sav;
1987
1988 return return_value;
1989 }
1990
1991
1992 /* Do the checks of the actual argument list that are specific to elemental
1993 procedures. If called with c == NULL, we have a function, otherwise if
1994 expr == NULL, we have a subroutine. */
1995
1996 static bool
1997 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1998 {
1999 gfc_actual_arglist *arg0;
2000 gfc_actual_arglist *arg;
2001 gfc_symbol *esym = NULL;
2002 gfc_intrinsic_sym *isym = NULL;
2003 gfc_expr *e = NULL;
2004 gfc_intrinsic_arg *iformal = NULL;
2005 gfc_formal_arglist *eformal = NULL;
2006 bool formal_optional = false;
2007 bool set_by_optional = false;
2008 int i;
2009 int rank = 0;
2010
2011 /* Is this an elemental procedure? */
2012 if (expr && expr->value.function.actual != NULL)
2013 {
2014 if (expr->value.function.esym != NULL
2015 && expr->value.function.esym->attr.elemental)
2016 {
2017 arg0 = expr->value.function.actual;
2018 esym = expr->value.function.esym;
2019 }
2020 else if (expr->value.function.isym != NULL
2021 && expr->value.function.isym->elemental)
2022 {
2023 arg0 = expr->value.function.actual;
2024 isym = expr->value.function.isym;
2025 }
2026 else
2027 return true;
2028 }
2029 else if (c && c->ext.actual != NULL)
2030 {
2031 arg0 = c->ext.actual;
2032
2033 if (c->resolved_sym)
2034 esym = c->resolved_sym;
2035 else
2036 esym = c->symtree->n.sym;
2037 gcc_assert (esym);
2038
2039 if (!esym->attr.elemental)
2040 return true;
2041 }
2042 else
2043 return true;
2044
2045 /* The rank of an elemental is the rank of its array argument(s). */
2046 for (arg = arg0; arg; arg = arg->next)
2047 {
2048 if (arg->expr != NULL && arg->expr->rank != 0)
2049 {
2050 rank = arg->expr->rank;
2051 if (arg->expr->expr_type == EXPR_VARIABLE
2052 && arg->expr->symtree->n.sym->attr.optional)
2053 set_by_optional = true;
2054
2055 /* Function specific; set the result rank and shape. */
2056 if (expr)
2057 {
2058 expr->rank = rank;
2059 if (!expr->shape && arg->expr->shape)
2060 {
2061 expr->shape = gfc_get_shape (rank);
2062 for (i = 0; i < rank; i++)
2063 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2064 }
2065 }
2066 break;
2067 }
2068 }
2069
2070 /* If it is an array, it shall not be supplied as an actual argument
2071 to an elemental procedure unless an array of the same rank is supplied
2072 as an actual argument corresponding to a nonoptional dummy argument of
2073 that elemental procedure(12.4.1.5). */
2074 formal_optional = false;
2075 if (isym)
2076 iformal = isym->formal;
2077 else
2078 eformal = esym->formal;
2079
2080 for (arg = arg0; arg; arg = arg->next)
2081 {
2082 if (eformal)
2083 {
2084 if (eformal->sym && eformal->sym->attr.optional)
2085 formal_optional = true;
2086 eformal = eformal->next;
2087 }
2088 else if (isym && iformal)
2089 {
2090 if (iformal->optional)
2091 formal_optional = true;
2092 iformal = iformal->next;
2093 }
2094 else if (isym)
2095 formal_optional = true;
2096
2097 if (pedantic && arg->expr != NULL
2098 && arg->expr->expr_type == EXPR_VARIABLE
2099 && arg->expr->symtree->n.sym->attr.optional
2100 && formal_optional
2101 && arg->expr->rank
2102 && (set_by_optional || arg->expr->rank != rank)
2103 && !(isym && isym->id == GFC_ISYM_CONVERSION))
2104 {
2105 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
2106 "MISSING, it cannot be the actual argument of an "
2107 "ELEMENTAL procedure unless there is a non-optional "
2108 "argument with the same rank (12.4.1.5)",
2109 arg->expr->symtree->n.sym->name, &arg->expr->where);
2110 }
2111 }
2112
2113 for (arg = arg0; arg; arg = arg->next)
2114 {
2115 if (arg->expr == NULL || arg->expr->rank == 0)
2116 continue;
2117
2118 /* Being elemental, the last upper bound of an assumed size array
2119 argument must be present. */
2120 if (resolve_assumed_size_actual (arg->expr))
2121 return false;
2122
2123 /* Elemental procedure's array actual arguments must conform. */
2124 if (e != NULL)
2125 {
2126 if (!gfc_check_conformance (arg->expr, e, "elemental procedure"))
2127 return false;
2128 }
2129 else
2130 e = arg->expr;
2131 }
2132
2133 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2134 is an array, the intent inout/out variable needs to be also an array. */
2135 if (rank > 0 && esym && expr == NULL)
2136 for (eformal = esym->formal, arg = arg0; arg && eformal;
2137 arg = arg->next, eformal = eformal->next)
2138 if ((eformal->sym->attr.intent == INTENT_OUT
2139 || eformal->sym->attr.intent == INTENT_INOUT)
2140 && arg->expr && arg->expr->rank == 0)
2141 {
2142 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
2143 "ELEMENTAL subroutine '%s' is a scalar, but another "
2144 "actual argument is an array", &arg->expr->where,
2145 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2146 : "INOUT", eformal->sym->name, esym->name);
2147 return false;
2148 }
2149 return true;
2150 }
2151
2152
2153 /* This function does the checking of references to global procedures
2154 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2155 77 and 95 standards. It checks for a gsymbol for the name, making
2156 one if it does not already exist. If it already exists, then the
2157 reference being resolved must correspond to the type of gsymbol.
2158 Otherwise, the new symbol is equipped with the attributes of the
2159 reference. The corresponding code that is called in creating
2160 global entities is parse.c.
2161
2162 In addition, for all but -std=legacy, the gsymbols are used to
2163 check the interfaces of external procedures from the same file.
2164 The namespace of the gsymbol is resolved and then, once this is
2165 done the interface is checked. */
2166
2167
2168 static bool
2169 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2170 {
2171 if (!gsym_ns->proc_name->attr.recursive)
2172 return true;
2173
2174 if (sym->ns == gsym_ns)
2175 return false;
2176
2177 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2178 return false;
2179
2180 return true;
2181 }
2182
2183 static bool
2184 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2185 {
2186 if (gsym_ns->entries)
2187 {
2188 gfc_entry_list *entry = gsym_ns->entries;
2189
2190 for (; entry; entry = entry->next)
2191 {
2192 if (strcmp (sym->name, entry->sym->name) == 0)
2193 {
2194 if (strcmp (gsym_ns->proc_name->name,
2195 sym->ns->proc_name->name) == 0)
2196 return false;
2197
2198 if (sym->ns->parent
2199 && strcmp (gsym_ns->proc_name->name,
2200 sym->ns->parent->proc_name->name) == 0)
2201 return false;
2202 }
2203 }
2204 }
2205 return true;
2206 }
2207
2208
2209 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2210
2211 bool
2212 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2213 {
2214 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2215
2216 for ( ; arg; arg = arg->next)
2217 {
2218 if (!arg->sym)
2219 continue;
2220
2221 if (arg->sym->attr.allocatable) /* (2a) */
2222 {
2223 strncpy (errmsg, _("allocatable argument"), err_len);
2224 return true;
2225 }
2226 else if (arg->sym->attr.asynchronous)
2227 {
2228 strncpy (errmsg, _("asynchronous argument"), err_len);
2229 return true;
2230 }
2231 else if (arg->sym->attr.optional)
2232 {
2233 strncpy (errmsg, _("optional argument"), err_len);
2234 return true;
2235 }
2236 else if (arg->sym->attr.pointer)
2237 {
2238 strncpy (errmsg, _("pointer argument"), err_len);
2239 return true;
2240 }
2241 else if (arg->sym->attr.target)
2242 {
2243 strncpy (errmsg, _("target argument"), err_len);
2244 return true;
2245 }
2246 else if (arg->sym->attr.value)
2247 {
2248 strncpy (errmsg, _("value argument"), err_len);
2249 return true;
2250 }
2251 else if (arg->sym->attr.volatile_)
2252 {
2253 strncpy (errmsg, _("volatile argument"), err_len);
2254 return true;
2255 }
2256 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2257 {
2258 strncpy (errmsg, _("assumed-shape argument"), err_len);
2259 return true;
2260 }
2261 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2262 {
2263 strncpy (errmsg, _("assumed-rank argument"), err_len);
2264 return true;
2265 }
2266 else if (arg->sym->attr.codimension) /* (2c) */
2267 {
2268 strncpy (errmsg, _("coarray argument"), err_len);
2269 return true;
2270 }
2271 else if (false) /* (2d) TODO: parametrized derived type */
2272 {
2273 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2274 return true;
2275 }
2276 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2277 {
2278 strncpy (errmsg, _("polymorphic argument"), err_len);
2279 return true;
2280 }
2281 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2282 {
2283 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2284 return true;
2285 }
2286 else if (arg->sym->ts.type == BT_ASSUMED)
2287 {
2288 /* As assumed-type is unlimited polymorphic (cf. above).
2289 See also TS 29113, Note 6.1. */
2290 strncpy (errmsg, _("assumed-type argument"), err_len);
2291 return true;
2292 }
2293 }
2294
2295 if (sym->attr.function)
2296 {
2297 gfc_symbol *res = sym->result ? sym->result : sym;
2298
2299 if (res->attr.dimension) /* (3a) */
2300 {
2301 strncpy (errmsg, _("array result"), err_len);
2302 return true;
2303 }
2304 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2305 {
2306 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2307 return true;
2308 }
2309 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2310 && res->ts.u.cl->length
2311 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2312 {
2313 strncpy (errmsg, _("result with non-constant character length"), err_len);
2314 return true;
2315 }
2316 }
2317
2318 if (sym->attr.elemental) /* (4) */
2319 {
2320 strncpy (errmsg, _("elemental procedure"), err_len);
2321 return true;
2322 }
2323 else if (sym->attr.is_bind_c) /* (5) */
2324 {
2325 strncpy (errmsg, _("bind(c) procedure"), err_len);
2326 return true;
2327 }
2328
2329 return false;
2330 }
2331
2332
2333 static void
2334 resolve_global_procedure (gfc_symbol *sym, locus *where,
2335 gfc_actual_arglist **actual, int sub)
2336 {
2337 gfc_gsymbol * gsym;
2338 gfc_namespace *ns;
2339 enum gfc_symbol_type type;
2340 char reason[200];
2341
2342 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2343
2344 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name);
2345
2346 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2347 gfc_global_used (gsym, where);
2348
2349 if ((sym->attr.if_source == IFSRC_UNKNOWN
2350 || sym->attr.if_source == IFSRC_IFBODY)
2351 && gsym->type != GSYM_UNKNOWN
2352 && gsym->ns
2353 && gsym->ns->resolved != -1
2354 && gsym->ns->proc_name
2355 && not_in_recursive (sym, gsym->ns)
2356 && not_entry_self_reference (sym, gsym->ns))
2357 {
2358 gfc_symbol *def_sym;
2359
2360 /* Resolve the gsymbol namespace if needed. */
2361 if (!gsym->ns->resolved)
2362 {
2363 gfc_dt_list *old_dt_list;
2364 struct gfc_omp_saved_state old_omp_state;
2365
2366 /* Stash away derived types so that the backend_decls do not
2367 get mixed up. */
2368 old_dt_list = gfc_derived_types;
2369 gfc_derived_types = NULL;
2370 /* And stash away openmp state. */
2371 gfc_omp_save_and_clear_state (&old_omp_state);
2372
2373 gfc_resolve (gsym->ns);
2374
2375 /* Store the new derived types with the global namespace. */
2376 if (gfc_derived_types)
2377 gsym->ns->derived_types = gfc_derived_types;
2378
2379 /* Restore the derived types of this namespace. */
2380 gfc_derived_types = old_dt_list;
2381 /* And openmp state. */
2382 gfc_omp_restore_state (&old_omp_state);
2383 }
2384
2385 /* Make sure that translation for the gsymbol occurs before
2386 the procedure currently being resolved. */
2387 ns = gfc_global_ns_list;
2388 for (; ns && ns != gsym->ns; ns = ns->sibling)
2389 {
2390 if (ns->sibling == gsym->ns)
2391 {
2392 ns->sibling = gsym->ns->sibling;
2393 gsym->ns->sibling = gfc_global_ns_list;
2394 gfc_global_ns_list = gsym->ns;
2395 break;
2396 }
2397 }
2398
2399 def_sym = gsym->ns->proc_name;
2400
2401 /* This can happen if a binding name has been specified. */
2402 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2403 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2404
2405 if (def_sym->attr.entry_master)
2406 {
2407 gfc_entry_list *entry;
2408 for (entry = gsym->ns->entries; entry; entry = entry->next)
2409 if (strcmp (entry->sym->name, sym->name) == 0)
2410 {
2411 def_sym = entry->sym;
2412 break;
2413 }
2414 }
2415
2416 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2417 {
2418 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2419 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2420 gfc_typename (&def_sym->ts));
2421 goto done;
2422 }
2423
2424 if (sym->attr.if_source == IFSRC_UNKNOWN
2425 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2426 {
2427 gfc_error ("Explicit interface required for '%s' at %L: %s",
2428 sym->name, &sym->declared_at, reason);
2429 goto done;
2430 }
2431
2432 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU))
2433 /* Turn erros into warnings with -std=gnu and -std=legacy. */
2434 gfc_errors_to_warnings (1);
2435
2436 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2437 reason, sizeof(reason), NULL, NULL))
2438 {
2439 gfc_error ("Interface mismatch in global procedure '%s' at %L: %s ",
2440 sym->name, &sym->declared_at, reason);
2441 goto done;
2442 }
2443
2444 if (!pedantic
2445 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2446 && !(gfc_option.warn_std & GFC_STD_GNU)))
2447 gfc_errors_to_warnings (1);
2448
2449 if (sym->attr.if_source != IFSRC_IFBODY)
2450 gfc_procedure_use (def_sym, actual, where);
2451 }
2452
2453 done:
2454 gfc_errors_to_warnings (0);
2455
2456 if (gsym->type == GSYM_UNKNOWN)
2457 {
2458 gsym->type = type;
2459 gsym->where = *where;
2460 }
2461
2462 gsym->used = 1;
2463 }
2464
2465
2466 /************* Function resolution *************/
2467
2468 /* Resolve a function call known to be generic.
2469 Section 14.1.2.4.1. */
2470
2471 static match
2472 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2473 {
2474 gfc_symbol *s;
2475
2476 if (sym->attr.generic)
2477 {
2478 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2479 if (s != NULL)
2480 {
2481 expr->value.function.name = s->name;
2482 expr->value.function.esym = s;
2483
2484 if (s->ts.type != BT_UNKNOWN)
2485 expr->ts = s->ts;
2486 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2487 expr->ts = s->result->ts;
2488
2489 if (s->as != NULL)
2490 expr->rank = s->as->rank;
2491 else if (s->result != NULL && s->result->as != NULL)
2492 expr->rank = s->result->as->rank;
2493
2494 gfc_set_sym_referenced (expr->value.function.esym);
2495
2496 return MATCH_YES;
2497 }
2498
2499 /* TODO: Need to search for elemental references in generic
2500 interface. */
2501 }
2502
2503 if (sym->attr.intrinsic)
2504 return gfc_intrinsic_func_interface (expr, 0);
2505
2506 return MATCH_NO;
2507 }
2508
2509
2510 static bool
2511 resolve_generic_f (gfc_expr *expr)
2512 {
2513 gfc_symbol *sym;
2514 match m;
2515 gfc_interface *intr = NULL;
2516
2517 sym = expr->symtree->n.sym;
2518
2519 for (;;)
2520 {
2521 m = resolve_generic_f0 (expr, sym);
2522 if (m == MATCH_YES)
2523 return true;
2524 else if (m == MATCH_ERROR)
2525 return false;
2526
2527 generic:
2528 if (!intr)
2529 for (intr = sym->generic; intr; intr = intr->next)
2530 if (intr->sym->attr.flavor == FL_DERIVED)
2531 break;
2532
2533 if (sym->ns->parent == NULL)
2534 break;
2535 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2536
2537 if (sym == NULL)
2538 break;
2539 if (!generic_sym (sym))
2540 goto generic;
2541 }
2542
2543 /* Last ditch attempt. See if the reference is to an intrinsic
2544 that possesses a matching interface. 14.1.2.4 */
2545 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2546 {
2547 gfc_error ("There is no specific function for the generic '%s' "
2548 "at %L", expr->symtree->n.sym->name, &expr->where);
2549 return false;
2550 }
2551
2552 if (intr)
2553 {
2554 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2555 NULL, false))
2556 return false;
2557 return resolve_structure_cons (expr, 0);
2558 }
2559
2560 m = gfc_intrinsic_func_interface (expr, 0);
2561 if (m == MATCH_YES)
2562 return true;
2563
2564 if (m == MATCH_NO)
2565 gfc_error ("Generic function '%s' at %L is not consistent with a "
2566 "specific intrinsic interface", expr->symtree->n.sym->name,
2567 &expr->where);
2568
2569 return false;
2570 }
2571
2572
2573 /* Resolve a function call known to be specific. */
2574
2575 static match
2576 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2577 {
2578 match m;
2579
2580 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2581 {
2582 if (sym->attr.dummy)
2583 {
2584 sym->attr.proc = PROC_DUMMY;
2585 goto found;
2586 }
2587
2588 sym->attr.proc = PROC_EXTERNAL;
2589 goto found;
2590 }
2591
2592 if (sym->attr.proc == PROC_MODULE
2593 || sym->attr.proc == PROC_ST_FUNCTION
2594 || sym->attr.proc == PROC_INTERNAL)
2595 goto found;
2596
2597 if (sym->attr.intrinsic)
2598 {
2599 m = gfc_intrinsic_func_interface (expr, 1);
2600 if (m == MATCH_YES)
2601 return MATCH_YES;
2602 if (m == MATCH_NO)
2603 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2604 "with an intrinsic", sym->name, &expr->where);
2605
2606 return MATCH_ERROR;
2607 }
2608
2609 return MATCH_NO;
2610
2611 found:
2612 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2613
2614 if (sym->result)
2615 expr->ts = sym->result->ts;
2616 else
2617 expr->ts = sym->ts;
2618 expr->value.function.name = sym->name;
2619 expr->value.function.esym = sym;
2620 if (sym->as != NULL)
2621 expr->rank = sym->as->rank;
2622
2623 return MATCH_YES;
2624 }
2625
2626
2627 static bool
2628 resolve_specific_f (gfc_expr *expr)
2629 {
2630 gfc_symbol *sym;
2631 match m;
2632
2633 sym = expr->symtree->n.sym;
2634
2635 for (;;)
2636 {
2637 m = resolve_specific_f0 (sym, expr);
2638 if (m == MATCH_YES)
2639 return true;
2640 if (m == MATCH_ERROR)
2641 return false;
2642
2643 if (sym->ns->parent == NULL)
2644 break;
2645
2646 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2647
2648 if (sym == NULL)
2649 break;
2650 }
2651
2652 gfc_error ("Unable to resolve the specific function '%s' at %L",
2653 expr->symtree->n.sym->name, &expr->where);
2654
2655 return true;
2656 }
2657
2658
2659 /* Resolve a procedure call not known to be generic nor specific. */
2660
2661 static bool
2662 resolve_unknown_f (gfc_expr *expr)
2663 {
2664 gfc_symbol *sym;
2665 gfc_typespec *ts;
2666
2667 sym = expr->symtree->n.sym;
2668
2669 if (sym->attr.dummy)
2670 {
2671 sym->attr.proc = PROC_DUMMY;
2672 expr->value.function.name = sym->name;
2673 goto set_type;
2674 }
2675
2676 /* See if we have an intrinsic function reference. */
2677
2678 if (gfc_is_intrinsic (sym, 0, expr->where))
2679 {
2680 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2681 return true;
2682 return false;
2683 }
2684
2685 /* The reference is to an external name. */
2686
2687 sym->attr.proc = PROC_EXTERNAL;
2688 expr->value.function.name = sym->name;
2689 expr->value.function.esym = expr->symtree->n.sym;
2690
2691 if (sym->as != NULL)
2692 expr->rank = sym->as->rank;
2693
2694 /* Type of the expression is either the type of the symbol or the
2695 default type of the symbol. */
2696
2697 set_type:
2698 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2699
2700 if (sym->ts.type != BT_UNKNOWN)
2701 expr->ts = sym->ts;
2702 else
2703 {
2704 ts = gfc_get_default_type (sym->name, sym->ns);
2705
2706 if (ts->type == BT_UNKNOWN)
2707 {
2708 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2709 sym->name, &expr->where);
2710 return false;
2711 }
2712 else
2713 expr->ts = *ts;
2714 }
2715
2716 return true;
2717 }
2718
2719
2720 /* Return true, if the symbol is an external procedure. */
2721 static bool
2722 is_external_proc (gfc_symbol *sym)
2723 {
2724 if (!sym->attr.dummy && !sym->attr.contained
2725 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
2726 && sym->attr.proc != PROC_ST_FUNCTION
2727 && !sym->attr.proc_pointer
2728 && !sym->attr.use_assoc
2729 && sym->name)
2730 return true;
2731
2732 return false;
2733 }
2734
2735
2736 /* Figure out if a function reference is pure or not. Also set the name
2737 of the function for a potential error message. Return nonzero if the
2738 function is PURE, zero if not. */
2739 static int
2740 pure_stmt_function (gfc_expr *, gfc_symbol *);
2741
2742 static int
2743 pure_function (gfc_expr *e, const char **name)
2744 {
2745 int pure;
2746
2747 *name = NULL;
2748
2749 if (e->symtree != NULL
2750 && e->symtree->n.sym != NULL
2751 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2752 return pure_stmt_function (e, e->symtree->n.sym);
2753
2754 if (e->value.function.esym)
2755 {
2756 pure = gfc_pure (e->value.function.esym);
2757 *name = e->value.function.esym->name;
2758 }
2759 else if (e->value.function.isym)
2760 {
2761 pure = e->value.function.isym->pure
2762 || e->value.function.isym->elemental;
2763 *name = e->value.function.isym->name;
2764 }
2765 else
2766 {
2767 /* Implicit functions are not pure. */
2768 pure = 0;
2769 *name = e->value.function.name;
2770 }
2771
2772 return pure;
2773 }
2774
2775
2776 static bool
2777 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2778 int *f ATTRIBUTE_UNUSED)
2779 {
2780 const char *name;
2781
2782 /* Don't bother recursing into other statement functions
2783 since they will be checked individually for purity. */
2784 if (e->expr_type != EXPR_FUNCTION
2785 || !e->symtree
2786 || e->symtree->n.sym == sym
2787 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2788 return false;
2789
2790 return pure_function (e, &name) ? false : true;
2791 }
2792
2793
2794 static int
2795 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2796 {
2797 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2798 }
2799
2800
2801 /* Resolve a function call, which means resolving the arguments, then figuring
2802 out which entity the name refers to. */
2803
2804 static bool
2805 resolve_function (gfc_expr *expr)
2806 {
2807 gfc_actual_arglist *arg;
2808 gfc_symbol *sym;
2809 const char *name;
2810 bool t;
2811 int temp;
2812 procedure_type p = PROC_INTRINSIC;
2813 bool no_formal_args;
2814
2815 sym = NULL;
2816 if (expr->symtree)
2817 sym = expr->symtree->n.sym;
2818
2819 /* If this is a procedure pointer component, it has already been resolved. */
2820 if (gfc_is_proc_ptr_comp (expr))
2821 return true;
2822
2823 if (sym && sym->attr.intrinsic
2824 && !gfc_resolve_intrinsic (sym, &expr->where))
2825 return false;
2826
2827 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2828 {
2829 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2830 return false;
2831 }
2832
2833 /* If this ia a deferred TBP with an abstract interface (which may
2834 of course be referenced), expr->value.function.esym will be set. */
2835 if (sym && sym->attr.abstract && !expr->value.function.esym)
2836 {
2837 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2838 sym->name, &expr->where);
2839 return false;
2840 }
2841
2842 /* Switch off assumed size checking and do this again for certain kinds
2843 of procedure, once the procedure itself is resolved. */
2844 need_full_assumed_size++;
2845
2846 if (expr->symtree && expr->symtree->n.sym)
2847 p = expr->symtree->n.sym->attr.proc;
2848
2849 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2850 inquiry_argument = true;
2851 no_formal_args = sym && is_external_proc (sym)
2852 && gfc_sym_get_dummy_args (sym) == NULL;
2853
2854 if (!resolve_actual_arglist (expr->value.function.actual,
2855 p, no_formal_args))
2856 {
2857 inquiry_argument = false;
2858 return false;
2859 }
2860
2861 inquiry_argument = false;
2862
2863 /* Resume assumed_size checking. */
2864 need_full_assumed_size--;
2865
2866 /* If the procedure is external, check for usage. */
2867 if (sym && is_external_proc (sym))
2868 resolve_global_procedure (sym, &expr->where,
2869 &expr->value.function.actual, 0);
2870
2871 if (sym && sym->ts.type == BT_CHARACTER
2872 && sym->ts.u.cl
2873 && sym->ts.u.cl->length == NULL
2874 && !sym->attr.dummy
2875 && !sym->ts.deferred
2876 && expr->value.function.esym == NULL
2877 && !sym->attr.contained)
2878 {
2879 /* Internal procedures are taken care of in resolve_contained_fntype. */
2880 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2881 "be used at %L since it is not a dummy argument",
2882 sym->name, &expr->where);
2883 return false;
2884 }
2885
2886 /* See if function is already resolved. */
2887
2888 if (expr->value.function.name != NULL)
2889 {
2890 if (expr->ts.type == BT_UNKNOWN)
2891 expr->ts = sym->ts;
2892 t = true;
2893 }
2894 else
2895 {
2896 /* Apply the rules of section 14.1.2. */
2897
2898 switch (procedure_kind (sym))
2899 {
2900 case PTYPE_GENERIC:
2901 t = resolve_generic_f (expr);
2902 break;
2903
2904 case PTYPE_SPECIFIC:
2905 t = resolve_specific_f (expr);
2906 break;
2907
2908 case PTYPE_UNKNOWN:
2909 t = resolve_unknown_f (expr);
2910 break;
2911
2912 default:
2913 gfc_internal_error ("resolve_function(): bad function type");
2914 }
2915 }
2916
2917 /* If the expression is still a function (it might have simplified),
2918 then we check to see if we are calling an elemental function. */
2919
2920 if (expr->expr_type != EXPR_FUNCTION)
2921 return t;
2922
2923 temp = need_full_assumed_size;
2924 need_full_assumed_size = 0;
2925
2926 if (!resolve_elemental_actual (expr, NULL))
2927 return false;
2928
2929 if (omp_workshare_flag
2930 && expr->value.function.esym
2931 && ! gfc_elemental (expr->value.function.esym))
2932 {
2933 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2934 "in WORKSHARE construct", expr->value.function.esym->name,
2935 &expr->where);
2936 t = false;
2937 }
2938
2939 #define GENERIC_ID expr->value.function.isym->id
2940 else if (expr->value.function.actual != NULL
2941 && expr->value.function.isym != NULL
2942 && GENERIC_ID != GFC_ISYM_LBOUND
2943 && GENERIC_ID != GFC_ISYM_LEN
2944 && GENERIC_ID != GFC_ISYM_LOC
2945 && GENERIC_ID != GFC_ISYM_C_LOC
2946 && GENERIC_ID != GFC_ISYM_PRESENT)
2947 {
2948 /* Array intrinsics must also have the last upper bound of an
2949 assumed size array argument. UBOUND and SIZE have to be
2950 excluded from the check if the second argument is anything
2951 than a constant. */
2952
2953 for (arg = expr->value.function.actual; arg; arg = arg->next)
2954 {
2955 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2956 && arg == expr->value.function.actual
2957 && arg->next != NULL && arg->next->expr)
2958 {
2959 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2960 break;
2961
2962 if (arg->next->name && strncmp (arg->next->name, "kind", 4) == 0)
2963 break;
2964
2965 if ((int)mpz_get_si (arg->next->expr->value.integer)
2966 < arg->expr->rank)
2967 break;
2968 }
2969
2970 if (arg->expr != NULL
2971 && arg->expr->rank > 0
2972 && resolve_assumed_size_actual (arg->expr))
2973 return false;
2974 }
2975 }
2976 #undef GENERIC_ID
2977
2978 need_full_assumed_size = temp;
2979 name = NULL;
2980
2981 if (!pure_function (expr, &name) && name)
2982 {
2983 if (forall_flag)
2984 {
2985 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
2986 "FORALL %s", name, &expr->where,
2987 forall_flag == 2 ? "mask" : "block");
2988 t = false;
2989 }
2990 else if (do_concurrent_flag)
2991 {
2992 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
2993 "DO CONCURRENT %s", name, &expr->where,
2994 do_concurrent_flag == 2 ? "mask" : "block");
2995 t = false;
2996 }
2997 else if (gfc_pure (NULL))
2998 {
2999 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
3000 "procedure within a PURE procedure", name, &expr->where);
3001 t = false;
3002 }
3003
3004 if (gfc_implicit_pure (NULL))
3005 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3006 }
3007
3008 /* Functions without the RECURSIVE attribution are not allowed to
3009 * call themselves. */
3010 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3011 {
3012 gfc_symbol *esym;
3013 esym = expr->value.function.esym;
3014
3015 if (is_illegal_recursion (esym, gfc_current_ns))
3016 {
3017 if (esym->attr.entry && esym->ns->entries)
3018 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3019 " function '%s' is not RECURSIVE",
3020 esym->name, &expr->where, esym->ns->entries->sym->name);
3021 else
3022 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3023 " is not RECURSIVE", esym->name, &expr->where);
3024
3025 t = false;
3026 }
3027 }
3028
3029 /* Character lengths of use associated functions may contains references to
3030 symbols not referenced from the current program unit otherwise. Make sure
3031 those symbols are marked as referenced. */
3032
3033 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3034 && expr->value.function.esym->attr.use_assoc)
3035 {
3036 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3037 }
3038
3039 /* Make sure that the expression has a typespec that works. */
3040 if (expr->ts.type == BT_UNKNOWN)
3041 {
3042 if (expr->symtree->n.sym->result
3043 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3044 && !expr->symtree->n.sym->result->attr.proc_pointer)
3045 expr->ts = expr->symtree->n.sym->result->ts;
3046 }
3047
3048 return t;
3049 }
3050
3051
3052 /************* Subroutine resolution *************/
3053
3054 static void
3055 pure_subroutine (gfc_code *c, gfc_symbol *sym)
3056 {
3057 if (gfc_pure (sym))
3058 return;
3059
3060 if (forall_flag)
3061 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3062 sym->name, &c->loc);
3063 else if (do_concurrent_flag)
3064 gfc_error ("Subroutine call to '%s' in DO CONCURRENT block at %L is not "
3065 "PURE", sym->name, &c->loc);
3066 else if (gfc_pure (NULL))
3067 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
3068 &c->loc);
3069
3070 if (gfc_implicit_pure (NULL))
3071 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3072 }
3073
3074
3075 static match
3076 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3077 {
3078 gfc_symbol *s;
3079
3080 if (sym->attr.generic)
3081 {
3082 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3083 if (s != NULL)
3084 {
3085 c->resolved_sym = s;
3086 pure_subroutine (c, s);
3087 return MATCH_YES;
3088 }
3089
3090 /* TODO: Need to search for elemental references in generic interface. */
3091 }
3092
3093 if (sym->attr.intrinsic)
3094 return gfc_intrinsic_sub_interface (c, 0);
3095
3096 return MATCH_NO;
3097 }
3098
3099
3100 static bool
3101 resolve_generic_s (gfc_code *c)
3102 {
3103 gfc_symbol *sym;
3104 match m;
3105
3106 sym = c->symtree->n.sym;
3107
3108 for (;;)
3109 {
3110 m = resolve_generic_s0 (c, sym);
3111 if (m == MATCH_YES)
3112 return true;
3113 else if (m == MATCH_ERROR)
3114 return false;
3115
3116 generic:
3117 if (sym->ns->parent == NULL)
3118 break;
3119 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3120
3121 if (sym == NULL)
3122 break;
3123 if (!generic_sym (sym))
3124 goto generic;
3125 }
3126
3127 /* Last ditch attempt. See if the reference is to an intrinsic
3128 that possesses a matching interface. 14.1.2.4 */
3129 sym = c->symtree->n.sym;
3130
3131 if (!gfc_is_intrinsic (sym, 1, c->loc))
3132 {
3133 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3134 sym->name, &c->loc);
3135 return false;
3136 }
3137
3138 m = gfc_intrinsic_sub_interface (c, 0);
3139 if (m == MATCH_YES)
3140 return true;
3141 if (m == MATCH_NO)
3142 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3143 "intrinsic subroutine interface", sym->name, &c->loc);
3144
3145 return false;
3146 }
3147
3148
3149 /* Resolve a subroutine call known to be specific. */
3150
3151 static match
3152 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3153 {
3154 match m;
3155
3156 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3157 {
3158 if (sym->attr.dummy)
3159 {
3160 sym->attr.proc = PROC_DUMMY;
3161 goto found;
3162 }
3163
3164 sym->attr.proc = PROC_EXTERNAL;
3165 goto found;
3166 }
3167
3168 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3169 goto found;
3170
3171 if (sym->attr.intrinsic)
3172 {
3173 m = gfc_intrinsic_sub_interface (c, 1);
3174 if (m == MATCH_YES)
3175 return MATCH_YES;
3176 if (m == MATCH_NO)
3177 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3178 "with an intrinsic", sym->name, &c->loc);
3179
3180 return MATCH_ERROR;
3181 }
3182
3183 return MATCH_NO;
3184
3185 found:
3186 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3187
3188 c->resolved_sym = sym;
3189 pure_subroutine (c, sym);
3190
3191 return MATCH_YES;
3192 }
3193
3194
3195 static bool
3196 resolve_specific_s (gfc_code *c)
3197 {
3198 gfc_symbol *sym;
3199 match m;
3200
3201 sym = c->symtree->n.sym;
3202
3203 for (;;)
3204 {
3205 m = resolve_specific_s0 (c, sym);
3206 if (m == MATCH_YES)
3207 return true;
3208 if (m == MATCH_ERROR)
3209 return false;
3210
3211 if (sym->ns->parent == NULL)
3212 break;
3213
3214 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3215
3216 if (sym == NULL)
3217 break;
3218 }
3219
3220 sym = c->symtree->n.sym;
3221 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3222 sym->name, &c->loc);
3223
3224 return false;
3225 }
3226
3227
3228 /* Resolve a subroutine call not known to be generic nor specific. */
3229
3230 static bool
3231 resolve_unknown_s (gfc_code *c)
3232 {
3233 gfc_symbol *sym;
3234
3235 sym = c->symtree->n.sym;
3236
3237 if (sym->attr.dummy)
3238 {
3239 sym->attr.proc = PROC_DUMMY;
3240 goto found;
3241 }
3242
3243 /* See if we have an intrinsic function reference. */
3244
3245 if (gfc_is_intrinsic (sym, 1, c->loc))
3246 {
3247 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3248 return true;
3249 return false;
3250 }
3251
3252 /* The reference is to an external name. */
3253
3254 found:
3255 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3256
3257 c->resolved_sym = sym;
3258
3259 pure_subroutine (c, sym);
3260
3261 return true;
3262 }
3263
3264
3265 /* Resolve a subroutine call. Although it was tempting to use the same code
3266 for functions, subroutines and functions are stored differently and this
3267 makes things awkward. */
3268
3269 static bool
3270 resolve_call (gfc_code *c)
3271 {
3272 bool t;
3273 procedure_type ptype = PROC_INTRINSIC;
3274 gfc_symbol *csym, *sym;
3275 bool no_formal_args;
3276
3277 csym = c->symtree ? c->symtree->n.sym : NULL;
3278
3279 if (csym && csym->ts.type != BT_UNKNOWN)
3280 {
3281 gfc_error ("'%s' at %L has a type, which is not consistent with "
3282 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3283 return false;
3284 }
3285
3286 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3287 {
3288 gfc_symtree *st;
3289 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3290 sym = st ? st->n.sym : NULL;
3291 if (sym && csym != sym
3292 && sym->ns == gfc_current_ns
3293 && sym->attr.flavor == FL_PROCEDURE
3294 && sym->attr.contained)
3295 {
3296 sym->refs++;
3297 if (csym->attr.generic)
3298 c->symtree->n.sym = sym;
3299 else
3300 c->symtree = st;
3301 csym = c->symtree->n.sym;
3302 }
3303 }
3304
3305 /* If this ia a deferred TBP, c->expr1 will be set. */
3306 if (!c->expr1 && csym)
3307 {
3308 if (csym->attr.abstract)
3309 {
3310 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3311 csym->name, &c->loc);
3312 return false;
3313 }
3314
3315 /* Subroutines without the RECURSIVE attribution are not allowed to
3316 call themselves. */
3317 if (is_illegal_recursion (csym, gfc_current_ns))
3318 {
3319 if (csym->attr.entry && csym->ns->entries)
3320 gfc_error ("ENTRY '%s' at %L cannot be called recursively, "
3321 "as subroutine '%s' is not RECURSIVE",
3322 csym->name, &c->loc, csym->ns->entries->sym->name);
3323 else
3324 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, "
3325 "as it is not RECURSIVE", csym->name, &c->loc);
3326
3327 t = false;
3328 }
3329 }
3330
3331 /* Switch off assumed size checking and do this again for certain kinds
3332 of procedure, once the procedure itself is resolved. */
3333 need_full_assumed_size++;
3334
3335 if (csym)
3336 ptype = csym->attr.proc;
3337
3338 no_formal_args = csym && is_external_proc (csym)
3339 && gfc_sym_get_dummy_args (csym) == NULL;
3340 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3341 return false;
3342
3343 /* Resume assumed_size checking. */
3344 need_full_assumed_size--;
3345
3346 /* If external, check for usage. */
3347 if (csym && is_external_proc (csym))
3348 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3349
3350 t = true;
3351 if (c->resolved_sym == NULL)
3352 {
3353 c->resolved_isym = NULL;
3354 switch (procedure_kind (csym))
3355 {
3356 case PTYPE_GENERIC:
3357 t = resolve_generic_s (c);
3358 break;
3359
3360 case PTYPE_SPECIFIC:
3361 t = resolve_specific_s (c);
3362 break;
3363
3364 case PTYPE_UNKNOWN:
3365 t = resolve_unknown_s (c);
3366 break;
3367
3368 default:
3369 gfc_internal_error ("resolve_subroutine(): bad function type");
3370 }
3371 }
3372
3373 /* Some checks of elemental subroutine actual arguments. */
3374 if (!resolve_elemental_actual (NULL, c))
3375 return false;
3376
3377 return t;
3378 }
3379
3380
3381 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3382 op1->shape and op2->shape are non-NULL return true if their shapes
3383 match. If both op1->shape and op2->shape are non-NULL return false
3384 if their shapes do not match. If either op1->shape or op2->shape is
3385 NULL, return true. */
3386
3387 static bool
3388 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3389 {
3390 bool t;
3391 int i;
3392
3393 t = true;
3394
3395 if (op1->shape != NULL && op2->shape != NULL)
3396 {
3397 for (i = 0; i < op1->rank; i++)
3398 {
3399 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3400 {
3401 gfc_error ("Shapes for operands at %L and %L are not conformable",
3402 &op1->where, &op2->where);
3403 t = false;
3404 break;
3405 }
3406 }
3407 }
3408
3409 return t;
3410 }
3411
3412
3413 /* Resolve an operator expression node. This can involve replacing the
3414 operation with a user defined function call. */
3415
3416 static bool
3417 resolve_operator (gfc_expr *e)
3418 {
3419 gfc_expr *op1, *op2;
3420 char msg[200];
3421 bool dual_locus_error;
3422 bool t;
3423
3424 /* Resolve all subnodes-- give them types. */
3425
3426 switch (e->value.op.op)
3427 {
3428 default:
3429 if (!gfc_resolve_expr (e->value.op.op2))
3430 return false;
3431
3432 /* Fall through... */
3433
3434 case INTRINSIC_NOT:
3435 case INTRINSIC_UPLUS:
3436 case INTRINSIC_UMINUS:
3437 case INTRINSIC_PARENTHESES:
3438 if (!gfc_resolve_expr (e->value.op.op1))
3439 return false;
3440 break;
3441 }
3442
3443 /* Typecheck the new node. */
3444
3445 op1 = e->value.op.op1;
3446 op2 = e->value.op.op2;
3447 dual_locus_error = false;
3448
3449 if ((op1 && op1->expr_type == EXPR_NULL)
3450 || (op2 && op2->expr_type == EXPR_NULL))
3451 {
3452 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3453 goto bad_op;
3454 }
3455
3456 switch (e->value.op.op)
3457 {
3458 case INTRINSIC_UPLUS:
3459 case INTRINSIC_UMINUS:
3460 if (op1->ts.type == BT_INTEGER
3461 || op1->ts.type == BT_REAL
3462 || op1->ts.type == BT_COMPLEX)
3463 {
3464 e->ts = op1->ts;
3465 break;
3466 }
3467
3468 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3469 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3470 goto bad_op;
3471
3472 case INTRINSIC_PLUS:
3473 case INTRINSIC_MINUS:
3474 case INTRINSIC_TIMES:
3475 case INTRINSIC_DIVIDE:
3476 case INTRINSIC_POWER:
3477 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3478 {
3479 gfc_type_convert_binary (e, 1);
3480 break;
3481 }
3482
3483 sprintf (msg,
3484 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3485 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3486 gfc_typename (&op2->ts));
3487 goto bad_op;
3488
3489 case INTRINSIC_CONCAT:
3490 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3491 && op1->ts.kind == op2->ts.kind)
3492 {
3493 e->ts.type = BT_CHARACTER;
3494 e->ts.kind = op1->ts.kind;
3495 break;
3496 }
3497
3498 sprintf (msg,
3499 _("Operands of string concatenation operator at %%L are %s/%s"),
3500 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3501 goto bad_op;
3502
3503 case INTRINSIC_AND:
3504 case INTRINSIC_OR:
3505 case INTRINSIC_EQV:
3506 case INTRINSIC_NEQV:
3507 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3508 {
3509 e->ts.type = BT_LOGICAL;
3510 e->ts.kind = gfc_kind_max (op1, op2);
3511 if (op1->ts.kind < e->ts.kind)
3512 gfc_convert_type (op1, &e->ts, 2);
3513 else if (op2->ts.kind < e->ts.kind)
3514 gfc_convert_type (op2, &e->ts, 2);
3515 break;
3516 }
3517
3518 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3519 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3520 gfc_typename (&op2->ts));
3521
3522 goto bad_op;
3523
3524 case INTRINSIC_NOT:
3525 if (op1->ts.type == BT_LOGICAL)
3526 {
3527 e->ts.type = BT_LOGICAL;
3528 e->ts.kind = op1->ts.kind;
3529 break;
3530 }
3531
3532 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3533 gfc_typename (&op1->ts));
3534 goto bad_op;
3535
3536 case INTRINSIC_GT:
3537 case INTRINSIC_GT_OS:
3538 case INTRINSIC_GE:
3539 case INTRINSIC_GE_OS:
3540 case INTRINSIC_LT:
3541 case INTRINSIC_LT_OS:
3542 case INTRINSIC_LE:
3543 case INTRINSIC_LE_OS:
3544 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3545 {
3546 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3547 goto bad_op;
3548 }
3549
3550 /* Fall through... */
3551
3552 case INTRINSIC_EQ:
3553 case INTRINSIC_EQ_OS:
3554 case INTRINSIC_NE:
3555 case INTRINSIC_NE_OS:
3556 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3557 && op1->ts.kind == op2->ts.kind)
3558 {
3559 e->ts.type = BT_LOGICAL;
3560 e->ts.kind = gfc_default_logical_kind;
3561 break;
3562 }
3563
3564 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3565 {
3566 gfc_type_convert_binary (e, 1);
3567
3568 e->ts.type = BT_LOGICAL;
3569 e->ts.kind = gfc_default_logical_kind;
3570
3571 if (gfc_option.warn_compare_reals)
3572 {
3573 gfc_intrinsic_op op = e->value.op.op;
3574
3575 /* Type conversion has made sure that the types of op1 and op2
3576 agree, so it is only necessary to check the first one. */
3577 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
3578 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
3579 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
3580 {
3581 const char *msg;
3582
3583 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
3584 msg = "Equality comparison for %s at %L";
3585 else
3586 msg = "Inequality comparison for %s at %L";
3587
3588 gfc_warning (msg, gfc_typename (&op1->ts), &op1->where);
3589 }
3590 }
3591
3592 break;
3593 }
3594
3595 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3596 sprintf (msg,
3597 _("Logicals at %%L must be compared with %s instead of %s"),
3598 (e->value.op.op == INTRINSIC_EQ
3599 || e->value.op.op == INTRINSIC_EQ_OS)
3600 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3601 else
3602 sprintf (msg,
3603 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3604 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3605 gfc_typename (&op2->ts));
3606
3607 goto bad_op;
3608
3609 case INTRINSIC_USER:
3610 if (e->value.op.uop->op == NULL)
3611 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3612 else if (op2 == NULL)
3613 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3614 e->value.op.uop->name, gfc_typename (&op1->ts));
3615 else
3616 {
3617 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3618 e->value.op.uop->name, gfc_typename (&op1->ts),
3619 gfc_typename (&op2->ts));
3620 e->value.op.uop->op->sym->attr.referenced = 1;
3621 }
3622
3623 goto bad_op;
3624
3625 case INTRINSIC_PARENTHESES:
3626 e->ts = op1->ts;
3627 if (e->ts.type == BT_CHARACTER)
3628 e->ts.u.cl = op1->ts.u.cl;
3629 break;
3630
3631 default:
3632 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3633 }
3634
3635 /* Deal with arrayness of an operand through an operator. */
3636
3637 t = true;
3638
3639 switch (e->value.op.op)
3640 {
3641 case INTRINSIC_PLUS:
3642 case INTRINSIC_MINUS:
3643 case INTRINSIC_TIMES:
3644 case INTRINSIC_DIVIDE:
3645 case INTRINSIC_POWER:
3646 case INTRINSIC_CONCAT:
3647 case INTRINSIC_AND:
3648 case INTRINSIC_OR:
3649 case INTRINSIC_EQV:
3650 case INTRINSIC_NEQV:
3651 case INTRINSIC_EQ:
3652 case INTRINSIC_EQ_OS:
3653 case INTRINSIC_NE:
3654 case INTRINSIC_NE_OS:
3655 case INTRINSIC_GT:
3656 case INTRINSIC_GT_OS:
3657 case INTRINSIC_GE:
3658 case INTRINSIC_GE_OS:
3659 case INTRINSIC_LT:
3660 case INTRINSIC_LT_OS:
3661 case INTRINSIC_LE:
3662 case INTRINSIC_LE_OS:
3663
3664 if (op1->rank == 0 && op2->rank == 0)
3665 e->rank = 0;
3666
3667 if (op1->rank == 0 && op2->rank != 0)
3668 {
3669 e->rank = op2->rank;
3670
3671 if (e->shape == NULL)
3672 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3673 }
3674
3675 if (op1->rank != 0 && op2->rank == 0)
3676 {
3677 e->rank = op1->rank;
3678
3679 if (e->shape == NULL)
3680 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3681 }
3682
3683 if (op1->rank != 0 && op2->rank != 0)
3684 {
3685 if (op1->rank == op2->rank)
3686 {
3687 e->rank = op1->rank;
3688 if (e->shape == NULL)
3689 {
3690 t = compare_shapes (op1, op2);
3691 if (!t)
3692 e->shape = NULL;
3693 else
3694 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3695 }
3696 }
3697 else
3698 {
3699 /* Allow higher level expressions to work. */
3700 e->rank = 0;
3701
3702 /* Try user-defined operators, and otherwise throw an error. */
3703 dual_locus_error = true;
3704 sprintf (msg,
3705 _("Inconsistent ranks for operator at %%L and %%L"));
3706 goto bad_op;
3707 }
3708 }
3709
3710 break;
3711
3712 case INTRINSIC_PARENTHESES:
3713 case INTRINSIC_NOT:
3714 case INTRINSIC_UPLUS:
3715 case INTRINSIC_UMINUS:
3716 /* Simply copy arrayness attribute */
3717 e->rank = op1->rank;
3718
3719 if (e->shape == NULL)
3720 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3721
3722 break;
3723
3724 default:
3725 break;
3726 }
3727
3728 /* Attempt to simplify the expression. */
3729 if (t)
3730 {
3731 t = gfc_simplify_expr (e, 0);
3732 /* Some calls do not succeed in simplification and return false
3733 even though there is no error; e.g. variable references to
3734 PARAMETER arrays. */
3735 if (!gfc_is_constant_expr (e))
3736 t = true;
3737 }
3738 return t;
3739
3740 bad_op:
3741
3742 {
3743 match m = gfc_extend_expr (e);
3744 if (m == MATCH_YES)
3745 return true;
3746 if (m == MATCH_ERROR)
3747 return false;
3748 }
3749
3750 if (dual_locus_error)
3751 gfc_error (msg, &op1->where, &op2->where);
3752 else
3753 gfc_error (msg, &e->where);
3754
3755 return false;
3756 }
3757
3758
3759 /************** Array resolution subroutines **************/
3760
3761 typedef enum
3762 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3763 comparison;
3764
3765 /* Compare two integer expressions. */
3766
3767 static comparison
3768 compare_bound (gfc_expr *a, gfc_expr *b)
3769 {
3770 int i;
3771
3772 if (a == NULL || a->expr_type != EXPR_CONSTANT
3773 || b == NULL || b->expr_type != EXPR_CONSTANT)
3774 return CMP_UNKNOWN;
3775
3776 /* If either of the types isn't INTEGER, we must have
3777 raised an error earlier. */
3778
3779 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3780 return CMP_UNKNOWN;
3781
3782 i = mpz_cmp (a->value.integer, b->value.integer);
3783
3784 if (i < 0)
3785 return CMP_LT;
3786 if (i > 0)
3787 return CMP_GT;
3788 return CMP_EQ;
3789 }
3790
3791
3792 /* Compare an integer expression with an integer. */
3793
3794 static comparison
3795 compare_bound_int (gfc_expr *a, int b)
3796 {
3797 int i;
3798
3799 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3800 return CMP_UNKNOWN;
3801
3802 if (a->ts.type != BT_INTEGER)
3803 gfc_internal_error ("compare_bound_int(): Bad expression");
3804
3805 i = mpz_cmp_si (a->value.integer, b);
3806
3807 if (i < 0)
3808 return CMP_LT;
3809 if (i > 0)
3810 return CMP_GT;
3811 return CMP_EQ;
3812 }
3813
3814
3815 /* Compare an integer expression with a mpz_t. */
3816
3817 static comparison
3818 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3819 {
3820 int i;
3821
3822 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3823 return CMP_UNKNOWN;
3824
3825 if (a->ts.type != BT_INTEGER)
3826 gfc_internal_error ("compare_bound_int(): Bad expression");
3827
3828 i = mpz_cmp (a->value.integer, b);
3829
3830 if (i < 0)
3831 return CMP_LT;
3832 if (i > 0)
3833 return CMP_GT;
3834 return CMP_EQ;
3835 }
3836
3837
3838 /* Compute the last value of a sequence given by a triplet.
3839 Return 0 if it wasn't able to compute the last value, or if the
3840 sequence if empty, and 1 otherwise. */
3841
3842 static int
3843 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3844 gfc_expr *stride, mpz_t last)
3845 {
3846 mpz_t rem;
3847
3848 if (start == NULL || start->expr_type != EXPR_CONSTANT
3849 || end == NULL || end->expr_type != EXPR_CONSTANT
3850 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3851 return 0;
3852
3853 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3854 || (stride != NULL && stride->ts.type != BT_INTEGER))
3855 return 0;
3856
3857 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
3858 {
3859 if (compare_bound (start, end) == CMP_GT)
3860 return 0;
3861 mpz_set (last, end->value.integer);
3862 return 1;
3863 }
3864
3865 if (compare_bound_int (stride, 0) == CMP_GT)
3866 {
3867 /* Stride is positive */
3868 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3869 return 0;
3870 }
3871 else
3872 {
3873 /* Stride is negative */
3874 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3875 return 0;
3876 }
3877
3878 mpz_init (rem);
3879 mpz_sub (rem, end->value.integer, start->value.integer);
3880 mpz_tdiv_r (rem, rem, stride->value.integer);
3881 mpz_sub (last, end->value.integer, rem);
3882 mpz_clear (rem);
3883
3884 return 1;
3885 }
3886
3887
3888 /* Compare a single dimension of an array reference to the array
3889 specification. */
3890
3891 static bool
3892 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
3893 {
3894 mpz_t last_value;
3895
3896 if (ar->dimen_type[i] == DIMEN_STAR)
3897 {
3898 gcc_assert (ar->stride[i] == NULL);
3899 /* This implies [*] as [*:] and [*:3] are not possible. */
3900 if (ar->start[i] == NULL)
3901 {
3902 gcc_assert (ar->end[i] == NULL);
3903 return true;
3904 }
3905 }
3906
3907 /* Given start, end and stride values, calculate the minimum and
3908 maximum referenced indexes. */
3909
3910 switch (ar->dimen_type[i])
3911 {
3912 case DIMEN_VECTOR:
3913 case DIMEN_THIS_IMAGE:
3914 break;
3915
3916 case DIMEN_STAR:
3917 case DIMEN_ELEMENT:
3918 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
3919 {
3920 if (i < as->rank)
3921 gfc_warning ("Array reference at %L is out of bounds "
3922 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3923 mpz_get_si (ar->start[i]->value.integer),
3924 mpz_get_si (as->lower[i]->value.integer), i+1);
3925 else
3926 gfc_warning ("Array reference at %L is out of bounds "
3927 "(%ld < %ld) in codimension %d", &ar->c_where[i],
3928 mpz_get_si (ar->start[i]->value.integer),
3929 mpz_get_si (as->lower[i]->value.integer),
3930 i + 1 - as->rank);
3931 return true;
3932 }
3933 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
3934 {
3935 if (i < as->rank)
3936 gfc_warning ("Array reference at %L is out of bounds "
3937 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3938 mpz_get_si (ar->start[i]->value.integer),
3939 mpz_get_si (as->upper[i]->value.integer), i+1);
3940 else
3941 gfc_warning ("Array reference at %L is out of bounds "
3942 "(%ld > %ld) in codimension %d", &ar->c_where[i],
3943 mpz_get_si (ar->start[i]->value.integer),
3944 mpz_get_si (as->upper[i]->value.integer),
3945 i + 1 - as->rank);
3946 return true;
3947 }
3948
3949 break;
3950
3951 case DIMEN_RANGE:
3952 {
3953 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3954 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3955
3956 comparison comp_start_end = compare_bound (AR_START, AR_END);
3957
3958 /* Check for zero stride, which is not allowed. */
3959 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3960 {
3961 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3962 return false;
3963 }
3964
3965 /* if start == len || (stride > 0 && start < len)
3966 || (stride < 0 && start > len),
3967 then the array section contains at least one element. In this
3968 case, there is an out-of-bounds access if
3969 (start < lower || start > upper). */
3970 if (compare_bound (AR_START, AR_END) == CMP_EQ
3971 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3972 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3973 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3974 && comp_start_end == CMP_GT))
3975 {
3976 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3977 {
3978 gfc_warning ("Lower array reference at %L is out of bounds "
3979 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3980 mpz_get_si (AR_START->value.integer),
3981 mpz_get_si (as->lower[i]->value.integer), i+1);
3982 return true;
3983 }
3984 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3985 {
3986 gfc_warning ("Lower array reference at %L is out of bounds "
3987 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3988 mpz_get_si (AR_START->value.integer),
3989 mpz_get_si (as->upper[i]->value.integer), i+1);
3990 return true;
3991 }
3992 }
3993
3994 /* If we can compute the highest index of the array section,
3995 then it also has to be between lower and upper. */
3996 mpz_init (last_value);
3997 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3998 last_value))
3999 {
4000 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4001 {
4002 gfc_warning ("Upper array reference at %L is out of bounds "
4003 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4004 mpz_get_si (last_value),
4005 mpz_get_si (as->lower[i]->value.integer), i+1);
4006 mpz_clear (last_value);
4007 return true;
4008 }
4009 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4010 {
4011 gfc_warning ("Upper array reference at %L is out of bounds "
4012 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4013 mpz_get_si (last_value),
4014 mpz_get_si (as->upper[i]->value.integer), i+1);
4015 mpz_clear (last_value);
4016 return true;
4017 }
4018 }
4019 mpz_clear (last_value);
4020
4021 #undef AR_START
4022 #undef AR_END
4023 }
4024 break;
4025
4026 default:
4027 gfc_internal_error ("check_dimension(): Bad array reference");
4028 }
4029
4030 return true;
4031 }
4032
4033
4034 /* Compare an array reference with an array specification. */
4035
4036 static bool
4037 compare_spec_to_ref (gfc_array_ref *ar)
4038 {
4039 gfc_array_spec *as;
4040 int i;
4041
4042 as = ar->as;
4043 i = as->rank - 1;
4044 /* TODO: Full array sections are only allowed as actual parameters. */
4045 if (as->type == AS_ASSUMED_SIZE
4046 && (/*ar->type == AR_FULL
4047 ||*/ (ar->type == AR_SECTION
4048 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4049 {
4050 gfc_error ("Rightmost upper bound of assumed size array section "
4051 "not specified at %L", &ar->where);
4052 return false;
4053 }
4054
4055 if (ar->type == AR_FULL)
4056 return true;
4057
4058 if (as->rank != ar->dimen)
4059 {
4060 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4061 &ar->where, ar->dimen, as->rank);
4062 return false;
4063 }
4064
4065 /* ar->codimen == 0 is a local array. */
4066 if (as->corank != ar->codimen && ar->codimen != 0)
4067 {
4068 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4069 &ar->where, ar->codimen, as->corank);
4070 return false;
4071 }
4072
4073 for (i = 0; i < as->rank; i++)
4074 if (!check_dimension (i, ar, as))
4075 return false;
4076
4077 /* Local access has no coarray spec. */
4078 if (ar->codimen != 0)
4079 for (i = as->rank; i < as->rank + as->corank; i++)
4080 {
4081 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4082 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
4083 {
4084 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4085 i + 1 - as->rank, &ar->where);
4086 return false;
4087 }
4088 if (!check_dimension (i, ar, as))
4089 return false;
4090 }
4091
4092 return true;
4093 }
4094
4095
4096 /* Resolve one part of an array index. */
4097
4098 static bool
4099 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4100 int force_index_integer_kind)
4101 {
4102 gfc_typespec ts;
4103
4104 if (index == NULL)
4105 return true;
4106
4107 if (!gfc_resolve_expr (index))
4108 return false;
4109
4110 if (check_scalar && index->rank != 0)
4111 {
4112 gfc_error ("Array index at %L must be scalar", &index->where);
4113 return false;
4114 }
4115
4116 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4117 {
4118 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4119 &index->where, gfc_basic_typename (index->ts.type));
4120 return false;
4121 }
4122
4123 if (index->ts.type == BT_REAL)
4124 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4125 &index->where))
4126 return false;
4127
4128 if ((index->ts.kind != gfc_index_integer_kind
4129 && force_index_integer_kind)
4130 || index->ts.type != BT_INTEGER)
4131 {
4132 gfc_clear_ts (&ts);
4133 ts.type = BT_INTEGER;
4134 ts.kind = gfc_index_integer_kind;
4135
4136 gfc_convert_type_warn (index, &ts, 2, 0);
4137 }
4138
4139 return true;
4140 }
4141
4142 /* Resolve one part of an array index. */
4143
4144 bool
4145 gfc_resolve_index (gfc_expr *index, int check_scalar)
4146 {
4147 return gfc_resolve_index_1 (index, check_scalar, 1);
4148 }
4149
4150 /* Resolve a dim argument to an intrinsic function. */
4151
4152 bool
4153 gfc_resolve_dim_arg (gfc_expr *dim)
4154 {
4155 if (dim == NULL)
4156 return true;
4157
4158 if (!gfc_resolve_expr (dim))
4159 return false;
4160
4161 if (dim->rank != 0)
4162 {
4163 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4164 return false;
4165
4166 }
4167
4168 if (dim->ts.type != BT_INTEGER)
4169 {
4170 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4171 return false;
4172 }
4173
4174 if (dim->ts.kind != gfc_index_integer_kind)
4175 {
4176 gfc_typespec ts;
4177
4178 gfc_clear_ts (&ts);
4179 ts.type = BT_INTEGER;
4180 ts.kind = gfc_index_integer_kind;
4181
4182 gfc_convert_type_warn (dim, &ts, 2, 0);
4183 }
4184
4185 return true;
4186 }
4187
4188 /* Given an expression that contains array references, update those array
4189 references to point to the right array specifications. While this is
4190 filled in during matching, this information is difficult to save and load
4191 in a module, so we take care of it here.
4192
4193 The idea here is that the original array reference comes from the
4194 base symbol. We traverse the list of reference structures, setting
4195 the stored reference to references. Component references can
4196 provide an additional array specification. */
4197
4198 static void
4199 find_array_spec (gfc_expr *e)
4200 {
4201 gfc_array_spec *as;
4202 gfc_component *c;
4203 gfc_ref *ref;
4204
4205 if (e->symtree->n.sym->ts.type == BT_CLASS)
4206 as = CLASS_DATA (e->symtree->n.sym)->as;
4207 else
4208 as = e->symtree->n.sym->as;
4209
4210 for (ref = e->ref; ref; ref = ref->next)
4211 switch (ref->type)
4212 {
4213 case REF_ARRAY:
4214 if (as == NULL)
4215 gfc_internal_error ("find_array_spec(): Missing spec");
4216
4217 ref->u.ar.as = as;
4218 as = NULL;
4219 break;
4220
4221 case REF_COMPONENT:
4222 c = ref->u.c.component;
4223 if (c->attr.dimension)
4224 {
4225 if (as != NULL)
4226 gfc_internal_error ("find_array_spec(): unused as(1)");
4227 as = c->as;
4228 }
4229
4230 break;
4231
4232 case REF_SUBSTRING:
4233 break;
4234 }
4235
4236 if (as != NULL)
4237 gfc_internal_error ("find_array_spec(): unused as(2)");
4238 }
4239
4240
4241 /* Resolve an array reference. */
4242
4243 static bool
4244 resolve_array_ref (gfc_array_ref *ar)
4245 {
4246 int i, check_scalar;
4247 gfc_expr *e;
4248
4249 for (i = 0; i < ar->dimen + ar->codimen; i++)
4250 {
4251 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4252
4253 /* Do not force gfc_index_integer_kind for the start. We can
4254 do fine with any integer kind. This avoids temporary arrays
4255 created for indexing with a vector. */
4256 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4257 return false;
4258 if (!gfc_resolve_index (ar->end[i], check_scalar))
4259 return false;
4260 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4261 return false;
4262
4263 e = ar->start[i];
4264
4265 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4266 switch (e->rank)
4267 {
4268 case 0:
4269 ar->dimen_type[i] = DIMEN_ELEMENT;
4270 break;
4271
4272 case 1:
4273 ar->dimen_type[i] = DIMEN_VECTOR;
4274 if (e->expr_type == EXPR_VARIABLE
4275 && e->symtree->n.sym->ts.type == BT_DERIVED)
4276 ar->start[i] = gfc_get_parentheses (e);
4277 break;
4278
4279 default:
4280 gfc_error ("Array index at %L is an array of rank %d",
4281 &ar->c_where[i], e->rank);
4282 return false;
4283 }
4284
4285 /* Fill in the upper bound, which may be lower than the
4286 specified one for something like a(2:10:5), which is
4287 identical to a(2:7:5). Only relevant for strides not equal
4288 to one. Don't try a division by zero. */
4289 if (ar->dimen_type[i] == DIMEN_RANGE
4290 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4291 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4292 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
4293 {
4294 mpz_t size, end;
4295
4296 if (gfc_ref_dimen_size (ar, i, &size, &end))
4297 {
4298 if (ar->end[i] == NULL)
4299 {
4300 ar->end[i] =
4301 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4302 &ar->where);
4303 mpz_set (ar->end[i]->value.integer, end);
4304 }
4305 else if (ar->end[i]->ts.type == BT_INTEGER
4306 && ar->end[i]->expr_type == EXPR_CONSTANT)
4307 {
4308 mpz_set (ar->end[i]->value.integer, end);
4309 }
4310 else
4311 gcc_unreachable ();
4312
4313 mpz_clear (size);
4314 mpz_clear (end);
4315 }
4316 }
4317 }
4318
4319 if (ar->type == AR_FULL)
4320 {
4321 if (ar->as->rank == 0)
4322 ar->type = AR_ELEMENT;
4323
4324 /* Make sure array is the same as array(:,:), this way
4325 we don't need to special case all the time. */
4326 ar->dimen = ar->as->rank;
4327 for (i = 0; i < ar->dimen; i++)
4328 {
4329 ar->dimen_type[i] = DIMEN_RANGE;
4330
4331 gcc_assert (ar->start[i] == NULL);
4332 gcc_assert (ar->end[i] == NULL);
4333 gcc_assert (ar->stride[i] == NULL);
4334 }
4335 }
4336
4337 /* If the reference type is unknown, figure out what kind it is. */
4338
4339 if (ar->type == AR_UNKNOWN)
4340 {
4341 ar->type = AR_ELEMENT;
4342 for (i = 0; i < ar->dimen; i++)
4343 if (ar->dimen_type[i] == DIMEN_RANGE
4344 || ar->dimen_type[i] == DIMEN_VECTOR)
4345 {
4346 ar->type = AR_SECTION;
4347 break;
4348 }
4349 }
4350
4351 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
4352 return false;
4353
4354 if (ar->as->corank && ar->codimen == 0)
4355 {
4356 int n;
4357 ar->codimen = ar->as->corank;
4358 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4359 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4360 }
4361
4362 return true;
4363 }
4364
4365
4366 static bool
4367 resolve_substring (gfc_ref *ref)
4368 {
4369 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4370
4371 if (ref->u.ss.start != NULL)
4372 {
4373 if (!gfc_resolve_expr (ref->u.ss.start))
4374 return false;
4375
4376 if (ref->u.ss.start->ts.type != BT_INTEGER)
4377 {
4378 gfc_error ("Substring start index at %L must be of type INTEGER",
4379 &ref->u.ss.start->where);
4380 return false;
4381 }
4382
4383 if (ref->u.ss.start->rank != 0)
4384 {
4385 gfc_error ("Substring start index at %L must be scalar",
4386 &ref->u.ss.start->where);
4387 return false;
4388 }
4389
4390 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4391 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4392 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4393 {
4394 gfc_error ("Substring start index at %L is less than one",
4395 &ref->u.ss.start->where);
4396 return false;
4397 }
4398 }
4399
4400 if (ref->u.ss.end != NULL)
4401 {
4402 if (!gfc_resolve_expr (ref->u.ss.end))
4403 return false;
4404
4405 if (ref->u.ss.end->ts.type != BT_INTEGER)
4406 {
4407 gfc_error ("Substring end index at %L must be of type INTEGER",
4408 &ref->u.ss.end->where);
4409 return false;
4410 }
4411
4412 if (ref->u.ss.end->rank != 0)
4413 {
4414 gfc_error ("Substring end index at %L must be scalar",
4415 &ref->u.ss.end->where);
4416 return false;
4417 }
4418
4419 if (ref->u.ss.length != NULL
4420 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4421 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4422 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4423 {
4424 gfc_error ("Substring end index at %L exceeds the string length",
4425 &ref->u.ss.start->where);
4426 return false;
4427 }
4428
4429 if (compare_bound_mpz_t (ref->u.ss.end,
4430 gfc_integer_kinds[k].huge) == CMP_GT
4431 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4432 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4433 {
4434 gfc_error ("Substring end index at %L is too large",
4435 &ref->u.ss.end->where);
4436 return false;
4437 }
4438 }
4439
4440 return true;
4441 }
4442
4443
4444 /* This function supplies missing substring charlens. */
4445
4446 void
4447 gfc_resolve_substring_charlen (gfc_expr *e)
4448 {
4449 gfc_ref *char_ref;
4450 gfc_expr *start, *end;
4451
4452 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4453 if (char_ref->type == REF_SUBSTRING)
4454 break;
4455
4456 if (!char_ref)
4457 return;
4458
4459 gcc_assert (char_ref->next == NULL);
4460
4461 if (e->ts.u.cl)
4462 {
4463 if (e->ts.u.cl->length)
4464 gfc_free_expr (e->ts.u.cl->length);
4465 else if (e->expr_type == EXPR_VARIABLE
4466 && e->symtree->n.sym->attr.dummy)
4467 return;
4468 }
4469
4470 e->ts.type = BT_CHARACTER;
4471 e->ts.kind = gfc_default_character_kind;
4472
4473 if (!e->ts.u.cl)
4474 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4475
4476 if (char_ref->u.ss.start)
4477 start = gfc_copy_expr (char_ref->u.ss.start);
4478 else
4479 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4480
4481 if (char_ref->u.ss.end)
4482 end = gfc_copy_expr (char_ref->u.ss.end);
4483 else if (e->expr_type == EXPR_VARIABLE)
4484 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4485 else
4486 end = NULL;
4487
4488 if (!start || !end)
4489 {
4490 gfc_free_expr (start);
4491 gfc_free_expr (end);
4492 return;
4493 }
4494
4495 /* Length = (end - start +1). */
4496 e->ts.u.cl->length = gfc_subtract (end, start);
4497 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4498 gfc_get_int_expr (gfc_default_integer_kind,
4499 NULL, 1));
4500
4501 e->ts.u.cl->length->ts.type = BT_INTEGER;
4502 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4503
4504 /* Make sure that the length is simplified. */
4505 gfc_simplify_expr (e->ts.u.cl->length, 1);
4506 gfc_resolve_expr (e->ts.u.cl->length);
4507 }
4508
4509
4510 /* Resolve subtype references. */
4511
4512 static bool
4513 resolve_ref (gfc_expr *expr)
4514 {
4515 int current_part_dimension, n_components, seen_part_dimension;
4516 gfc_ref *ref;
4517
4518 for (ref = expr->ref; ref; ref = ref->next)
4519 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4520 {
4521 find_array_spec (expr);
4522 break;
4523 }
4524
4525 for (ref = expr->ref; ref; ref = ref->next)
4526 switch (ref->type)
4527 {
4528 case REF_ARRAY:
4529 if (!resolve_array_ref (&ref->u.ar))
4530 return false;
4531 break;
4532
4533 case REF_COMPONENT:
4534 break;
4535
4536 case REF_SUBSTRING:
4537 if (!resolve_substring (ref))
4538 return false;
4539 break;
4540 }
4541
4542 /* Check constraints on part references. */
4543
4544 current_part_dimension = 0;
4545 seen_part_dimension = 0;
4546 n_components = 0;
4547
4548 for (ref = expr->ref; ref; ref = ref->next)
4549 {
4550 switch (ref->type)
4551 {
4552 case REF_ARRAY:
4553 switch (ref->u.ar.type)
4554 {
4555 case AR_FULL:
4556 /* Coarray scalar. */
4557 if (ref->u.ar.as->rank == 0)
4558 {
4559 current_part_dimension = 0;
4560 break;
4561 }
4562 /* Fall through. */
4563 case AR_SECTION:
4564 current_part_dimension = 1;
4565 break;
4566
4567 case AR_ELEMENT:
4568 current_part_dimension = 0;
4569 break;
4570
4571 case AR_UNKNOWN:
4572 gfc_internal_error ("resolve_ref(): Bad array reference");
4573 }
4574
4575 break;
4576
4577 case REF_COMPONENT:
4578 if (current_part_dimension || seen_part_dimension)
4579 {
4580 /* F03:C614. */
4581 if (ref->u.c.component->attr.pointer
4582 || ref->u.c.component->attr.proc_pointer
4583 || (ref->u.c.component->ts.type == BT_CLASS
4584 && CLASS_DATA (ref->u.c.component)->attr.pointer))
4585 {
4586 gfc_error ("Component to the right of a part reference "
4587 "with nonzero rank must not have the POINTER "
4588 "attribute at %L", &expr->where);
4589 return false;
4590 }
4591 else if (ref->u.c.component->attr.allocatable
4592 || (ref->u.c.component->ts.type == BT_CLASS
4593 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
4594
4595 {
4596 gfc_error ("Component to the right of a part reference "
4597 "with nonzero rank must not have the ALLOCATABLE "
4598 "attribute at %L", &expr->where);
4599 return false;
4600 }
4601 }
4602
4603 n_components++;
4604 break;
4605
4606 case REF_SUBSTRING:
4607 break;
4608 }
4609
4610 if (((ref->type == REF_COMPONENT && n_components > 1)
4611 || ref->next == NULL)
4612 && current_part_dimension
4613 && seen_part_dimension)
4614 {
4615 gfc_error ("Two or more part references with nonzero rank must "
4616 "not be specified at %L", &expr->where);
4617 return false;
4618 }
4619
4620 if (ref->type == REF_COMPONENT)
4621 {
4622 if (current_part_dimension)
4623 seen_part_dimension = 1;
4624
4625 /* reset to make sure */
4626 current_part_dimension = 0;
4627 }
4628 }
4629
4630 return true;
4631 }
4632
4633
4634 /* Given an expression, determine its shape. This is easier than it sounds.
4635 Leaves the shape array NULL if it is not possible to determine the shape. */
4636
4637 static void
4638 expression_shape (gfc_expr *e)
4639 {
4640 mpz_t array[GFC_MAX_DIMENSIONS];
4641 int i;
4642
4643 if (e->rank <= 0 || e->shape != NULL)
4644 return;
4645
4646 for (i = 0; i < e->rank; i++)
4647 if (!gfc_array_dimen_size (e, i, &array[i]))
4648 goto fail;
4649
4650 e->shape = gfc_get_shape (e->rank);
4651
4652 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4653
4654 return;
4655
4656 fail:
4657 for (i--; i >= 0; i--)
4658 mpz_clear (array[i]);
4659 }
4660
4661
4662 /* Given a variable expression node, compute the rank of the expression by
4663 examining the base symbol and any reference structures it may have. */
4664
4665 static void
4666 expression_rank (gfc_expr *e)
4667 {
4668 gfc_ref *ref;
4669 int i, rank;
4670
4671 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4672 could lead to serious confusion... */
4673 gcc_assert (e->expr_type != EXPR_COMPCALL);
4674
4675 if (e->ref == NULL)
4676 {
4677 if (e->expr_type == EXPR_ARRAY)
4678 goto done;
4679 /* Constructors can have a rank different from one via RESHAPE(). */
4680
4681 if (e->symtree == NULL)
4682 {
4683 e->rank = 0;
4684 goto done;
4685 }
4686
4687 e->rank = (e->symtree->n.sym->as == NULL)
4688 ? 0 : e->symtree->n.sym->as->rank;
4689 goto done;
4690 }
4691
4692 rank = 0;
4693
4694 for (ref = e->ref; ref; ref = ref->next)
4695 {
4696 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4697 && ref->u.c.component->attr.function && !ref->next)
4698 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4699
4700 if (ref->type != REF_ARRAY)
4701 continue;
4702
4703 if (ref->u.ar.type == AR_FULL)
4704 {
4705 rank = ref->u.ar.as->rank;
4706 break;
4707 }
4708
4709 if (ref->u.ar.type == AR_SECTION)
4710 {
4711 /* Figure out the rank of the section. */
4712 if (rank != 0)
4713 gfc_internal_error ("expression_rank(): Two array specs");
4714
4715 for (i = 0; i < ref->u.ar.dimen; i++)
4716 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4717 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4718 rank++;
4719
4720 break;
4721 }
4722 }
4723
4724 e->rank = rank;
4725
4726 done:
4727 expression_shape (e);
4728 }
4729
4730
4731 /* Resolve a variable expression. */
4732
4733 static bool
4734 resolve_variable (gfc_expr *e)
4735 {
4736 gfc_symbol *sym;
4737 bool t;
4738
4739 t = true;
4740
4741 if (e->symtree == NULL)
4742 return false;
4743 sym = e->symtree->n.sym;
4744
4745 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
4746 as ts.type is set to BT_ASSUMED in resolve_symbol. */
4747 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
4748 {
4749 if (!actual_arg || inquiry_argument)
4750 {
4751 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
4752 "be used as actual argument", sym->name, &e->where);
4753 return false;
4754 }
4755 }
4756 /* TS 29113, 407b. */
4757 else if (e->ts.type == BT_ASSUMED)
4758 {
4759 if (!actual_arg)
4760 {
4761 gfc_error ("Assumed-type variable %s at %L may only be used "
4762 "as actual argument", sym->name, &e->where);
4763 return false;
4764 }
4765 else if (inquiry_argument && !first_actual_arg)
4766 {
4767 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4768 for all inquiry functions in resolve_function; the reason is
4769 that the function-name resolution happens too late in that
4770 function. */
4771 gfc_error ("Assumed-type variable %s at %L as actual argument to "
4772 "an inquiry function shall be the first argument",
4773 sym->name, &e->where);
4774 return false;
4775 }
4776 }
4777 /* TS 29113, C535b. */
4778 else if ((sym->ts.type == BT_CLASS && sym->attr.class_ok
4779 && CLASS_DATA (sym)->as
4780 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4781 || (sym->ts.type != BT_CLASS && sym->as
4782 && sym->as->type == AS_ASSUMED_RANK))
4783 {
4784 if (!actual_arg)
4785 {
4786 gfc_error ("Assumed-rank variable %s at %L may only be used as "
4787 "actual argument", sym->name, &e->where);
4788 return false;
4789 }
4790 else if (inquiry_argument && !first_actual_arg)
4791 {
4792 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4793 for all inquiry functions in resolve_function; the reason is
4794 that the function-name resolution happens too late in that
4795 function. */
4796 gfc_error ("Assumed-rank variable %s at %L as actual argument "
4797 "to an inquiry function shall be the first argument",
4798 sym->name, &e->where);
4799 return false;
4800 }
4801 }
4802
4803 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
4804 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4805 && e->ref->next == NULL))
4806 {
4807 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
4808 "a subobject reference", sym->name, &e->ref->u.ar.where);
4809 return false;
4810 }
4811 /* TS 29113, 407b. */
4812 else if (e->ts.type == BT_ASSUMED && e->ref
4813 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4814 && e->ref->next == NULL))
4815 {
4816 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
4817 "reference", sym->name, &e->ref->u.ar.where);
4818 return false;
4819 }
4820
4821 /* TS 29113, C535b. */
4822 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
4823 && CLASS_DATA (sym)->as
4824 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4825 || (sym->ts.type != BT_CLASS && sym->as
4826 && sym->as->type == AS_ASSUMED_RANK))
4827 && e->ref
4828 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4829 && e->ref->next == NULL))
4830 {
4831 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
4832 "reference", sym->name, &e->ref->u.ar.where);
4833 return false;
4834 }
4835
4836
4837 /* If this is an associate-name, it may be parsed with an array reference
4838 in error even though the target is scalar. Fail directly in this case.
4839 TODO Understand why class scalar expressions must be excluded. */
4840 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
4841 {
4842 if (sym->ts.type == BT_CLASS)
4843 gfc_fix_class_refs (e);
4844 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
4845 return false;
4846 }
4847
4848 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
4849 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
4850
4851 /* On the other hand, the parser may not have known this is an array;
4852 in this case, we have to add a FULL reference. */
4853 if (sym->assoc && sym->attr.dimension && !e->ref)
4854 {
4855 e->ref = gfc_get_ref ();
4856 e->ref->type = REF_ARRAY;
4857 e->ref->u.ar.type = AR_FULL;
4858 e->ref->u.ar.dimen = 0;
4859 }
4860
4861 if (e->ref && !resolve_ref (e))
4862 return false;
4863
4864 if (sym->attr.flavor == FL_PROCEDURE
4865 && (!sym->attr.function
4866 || (sym->attr.function && sym->result
4867 && sym->result->attr.proc_pointer
4868 && !sym->result->attr.function)))
4869 {
4870 e->ts.type = BT_PROCEDURE;
4871 goto resolve_procedure;
4872 }
4873
4874 if (sym->ts.type != BT_UNKNOWN)
4875 gfc_variable_attr (e, &e->ts);
4876 else
4877 {
4878 /* Must be a simple variable reference. */
4879 if (!gfc_set_default_type (sym, 1, sym->ns))
4880 return false;
4881 e->ts = sym->ts;
4882 }
4883
4884 if (check_assumed_size_reference (sym, e))
4885 return false;
4886
4887 /* Deal with forward references to entries during resolve_code, to
4888 satisfy, at least partially, 12.5.2.5. */
4889 if (gfc_current_ns->entries
4890 && current_entry_id == sym->entry_id
4891 && cs_base
4892 && cs_base->current
4893 && cs_base->current->op != EXEC_ENTRY)
4894 {
4895 gfc_entry_list *entry;
4896 gfc_formal_arglist *formal;
4897 int n;
4898 bool seen, saved_specification_expr;
4899
4900 /* If the symbol is a dummy... */
4901 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4902 {
4903 entry = gfc_current_ns->entries;
4904 seen = false;
4905
4906 /* ...test if the symbol is a parameter of previous entries. */
4907 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4908 for (formal = entry->sym->formal; formal; formal = formal->next)
4909 {
4910 if (formal->sym && sym->name == formal->sym->name)
4911 seen = true;
4912 }
4913
4914 /* If it has not been seen as a dummy, this is an error. */
4915 if (!seen)
4916 {
4917 if (specification_expr)
4918 gfc_error ("Variable '%s', used in a specification expression"
4919 ", is referenced at %L before the ENTRY statement "
4920 "in which it is a parameter",
4921 sym->name, &cs_base->current->loc);
4922 else
4923 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4924 "statement in which it is a parameter",
4925 sym->name, &cs_base->current->loc);
4926 t = false;
4927 }
4928 }
4929
4930 /* Now do the same check on the specification expressions. */
4931 saved_specification_expr = specification_expr;
4932 specification_expr = true;
4933 if (sym->ts.type == BT_CHARACTER
4934 && !gfc_resolve_expr (sym->ts.u.cl->length))
4935 t = false;
4936
4937 if (sym->as)
4938 for (n = 0; n < sym->as->rank; n++)
4939 {
4940 if (!gfc_resolve_expr (sym->as->lower[n]))
4941 t = false;
4942 if (!gfc_resolve_expr (sym->as->upper[n]))
4943 t = false;
4944 }
4945 specification_expr = saved_specification_expr;
4946
4947 if (t)
4948 /* Update the symbol's entry level. */
4949 sym->entry_id = current_entry_id + 1;
4950 }
4951
4952 /* If a symbol has been host_associated mark it. This is used latter,
4953 to identify if aliasing is possible via host association. */
4954 if (sym->attr.flavor == FL_VARIABLE
4955 && gfc_current_ns->parent
4956 && (gfc_current_ns->parent == sym->ns
4957 || (gfc_current_ns->parent->parent
4958 && gfc_current_ns->parent->parent == sym->ns)))
4959 sym->attr.host_assoc = 1;
4960
4961 resolve_procedure:
4962 if (t && !resolve_procedure_expression (e))
4963 t = false;
4964
4965 /* F2008, C617 and C1229. */
4966 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
4967 && gfc_is_coindexed (e))
4968 {
4969 gfc_ref *ref, *ref2 = NULL;
4970
4971 for (ref = e->ref; ref; ref = ref->next)
4972 {
4973 if (ref->type == REF_COMPONENT)
4974 ref2 = ref;
4975 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4976 break;
4977 }
4978
4979 for ( ; ref; ref = ref->next)
4980 if (ref->type == REF_COMPONENT)
4981 break;
4982
4983 /* Expression itself is not coindexed object. */
4984 if (ref && e->ts.type == BT_CLASS)
4985 {
4986 gfc_error ("Polymorphic subobject of coindexed object at %L",
4987 &e->where);
4988 t = false;
4989 }
4990
4991 /* Expression itself is coindexed object. */
4992 if (ref == NULL)
4993 {
4994 gfc_component *c;
4995 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
4996 for ( ; c; c = c->next)
4997 if (c->attr.allocatable && c->ts.type == BT_CLASS)
4998 {
4999 gfc_error ("Coindexed object with polymorphic allocatable "
5000 "subcomponent at %L", &e->where);
5001 t = false;
5002 break;
5003 }
5004 }
5005 }
5006
5007 return t;
5008 }
5009
5010
5011 /* Checks to see that the correct symbol has been host associated.
5012 The only situation where this arises is that in which a twice
5013 contained function is parsed after the host association is made.
5014 Therefore, on detecting this, change the symbol in the expression
5015 and convert the array reference into an actual arglist if the old
5016 symbol is a variable. */
5017 static bool
5018 check_host_association (gfc_expr *e)
5019 {
5020 gfc_symbol *sym, *old_sym;
5021 gfc_symtree *st;
5022 int n;
5023 gfc_ref *ref;
5024 gfc_actual_arglist *arg, *tail = NULL;
5025 bool retval = e->expr_type == EXPR_FUNCTION;
5026
5027 /* If the expression is the result of substitution in
5028 interface.c(gfc_extend_expr) because there is no way in
5029 which the host association can be wrong. */
5030 if (e->symtree == NULL
5031 || e->symtree->n.sym == NULL
5032 || e->user_operator)
5033 return retval;
5034
5035 old_sym = e->symtree->n.sym;
5036
5037 if (gfc_current_ns->parent
5038 && old_sym->ns != gfc_current_ns)
5039 {
5040 /* Use the 'USE' name so that renamed module symbols are
5041 correctly handled. */
5042 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5043
5044 if (sym && old_sym != sym
5045 && sym->ts.type == old_sym->ts.type
5046 && sym->attr.flavor == FL_PROCEDURE
5047 && sym->attr.contained)
5048 {
5049 /* Clear the shape, since it might not be valid. */
5050 gfc_free_shape (&e->shape, e->rank);
5051
5052 /* Give the expression the right symtree! */
5053 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5054 gcc_assert (st != NULL);
5055
5056 if (old_sym->attr.flavor == FL_PROCEDURE
5057 || e->expr_type == EXPR_FUNCTION)
5058 {
5059 /* Original was function so point to the new symbol, since
5060 the actual argument list is already attached to the
5061 expression. */
5062 e->value.function.esym = NULL;
5063 e->symtree = st;
5064 }
5065 else
5066 {
5067 /* Original was variable so convert array references into
5068 an actual arglist. This does not need any checking now
5069 since resolve_function will take care of it. */
5070 e->value.function.actual = NULL;
5071 e->expr_type = EXPR_FUNCTION;
5072 e->symtree = st;
5073
5074 /* Ambiguity will not arise if the array reference is not
5075 the last reference. */
5076 for (ref = e->ref; ref; ref = ref->next)
5077 if (ref->type == REF_ARRAY && ref->next == NULL)
5078 break;
5079
5080 gcc_assert (ref->type == REF_ARRAY);
5081
5082 /* Grab the start expressions from the array ref and
5083 copy them into actual arguments. */
5084 for (n = 0; n < ref->u.ar.dimen; n++)
5085 {
5086 arg = gfc_get_actual_arglist ();
5087 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5088 if (e->value.function.actual == NULL)
5089 tail = e->value.function.actual = arg;
5090 else
5091 {
5092 tail->next = arg;
5093 tail = arg;
5094 }
5095 }
5096
5097 /* Dump the reference list and set the rank. */
5098 gfc_free_ref_list (e->ref);
5099 e->ref = NULL;
5100 e->rank = sym->as ? sym->as->rank : 0;
5101 }
5102
5103 gfc_resolve_expr (e);
5104 sym->refs++;
5105 }
5106 }
5107 /* This might have changed! */
5108 return e->expr_type == EXPR_FUNCTION;
5109 }
5110
5111
5112 static void
5113 gfc_resolve_character_operator (gfc_expr *e)
5114 {
5115 gfc_expr *op1 = e->value.op.op1;
5116 gfc_expr *op2 = e->value.op.op2;
5117 gfc_expr *e1 = NULL;
5118 gfc_expr *e2 = NULL;
5119
5120 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5121
5122 if (op1->ts.u.cl && op1->ts.u.cl->length)
5123 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5124 else if (op1->expr_type == EXPR_CONSTANT)
5125 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5126 op1->value.character.length);
5127
5128 if (op2->ts.u.cl && op2->ts.u.cl->length)
5129 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5130 else if (op2->expr_type == EXPR_CONSTANT)
5131 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5132 op2->value.character.length);
5133
5134 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5135
5136 if (!e1 || !e2)
5137 {
5138 gfc_free_expr (e1);
5139 gfc_free_expr (e2);
5140
5141 return;
5142 }
5143
5144 e->ts.u.cl->length = gfc_add (e1, e2);
5145 e->ts.u.cl->length->ts.type = BT_INTEGER;
5146 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5147 gfc_simplify_expr (e->ts.u.cl->length, 0);
5148 gfc_resolve_expr (e->ts.u.cl->length);
5149
5150 return;
5151 }
5152
5153
5154 /* Ensure that an character expression has a charlen and, if possible, a
5155 length expression. */
5156
5157 static void
5158 fixup_charlen (gfc_expr *e)
5159 {
5160 /* The cases fall through so that changes in expression type and the need
5161 for multiple fixes are picked up. In all circumstances, a charlen should
5162 be available for the middle end to hang a backend_decl on. */
5163 switch (e->expr_type)
5164 {
5165 case EXPR_OP:
5166 gfc_resolve_character_operator (e);
5167
5168 case EXPR_ARRAY:
5169 if (e->expr_type == EXPR_ARRAY)
5170 gfc_resolve_character_array_constructor (e);
5171
5172 case EXPR_SUBSTRING:
5173 if (!e->ts.u.cl && e->ref)
5174 gfc_resolve_substring_charlen (e);
5175
5176 default:
5177 if (!e->ts.u.cl)
5178 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5179
5180 break;
5181 }
5182 }
5183
5184
5185 /* Update an actual argument to include the passed-object for type-bound
5186 procedures at the right position. */
5187
5188 static gfc_actual_arglist*
5189 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5190 const char *name)
5191 {
5192 gcc_assert (argpos > 0);
5193
5194 if (argpos == 1)
5195 {
5196 gfc_actual_arglist* result;
5197
5198 result = gfc_get_actual_arglist ();
5199 result->expr = po;
5200 result->next = lst;
5201 if (name)
5202 result->name = name;
5203
5204 return result;
5205 }
5206
5207 if (lst)
5208 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5209 else
5210 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5211 return lst;
5212 }
5213
5214
5215 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5216
5217 static gfc_expr*
5218 extract_compcall_passed_object (gfc_expr* e)
5219 {
5220 gfc_expr* po;
5221
5222 gcc_assert (e->expr_type == EXPR_COMPCALL);
5223
5224 if (e->value.compcall.base_object)
5225 po = gfc_copy_expr (e->value.compcall.base_object);
5226 else
5227 {
5228 po = gfc_get_expr ();
5229 po->expr_type = EXPR_VARIABLE;
5230 po->symtree = e->symtree;
5231 po->ref = gfc_copy_ref (e->ref);
5232 po->where = e->where;
5233 }
5234
5235 if (!gfc_resolve_expr (po))
5236 return NULL;
5237
5238 return po;
5239 }
5240
5241
5242 /* Update the arglist of an EXPR_COMPCALL expression to include the
5243 passed-object. */
5244
5245 static bool
5246 update_compcall_arglist (gfc_expr* e)
5247 {
5248 gfc_expr* po;
5249 gfc_typebound_proc* tbp;
5250
5251 tbp = e->value.compcall.tbp;
5252
5253 if (tbp->error)
5254 return false;
5255
5256 po = extract_compcall_passed_object (e);
5257 if (!po)
5258 return false;
5259
5260 if (tbp->nopass || e->value.compcall.ignore_pass)
5261 {
5262 gfc_free_expr (po);
5263 return true;
5264 }
5265
5266 gcc_assert (tbp->pass_arg_num > 0);
5267 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5268 tbp->pass_arg_num,
5269 tbp->pass_arg);
5270
5271 return true;
5272 }
5273
5274
5275 /* Extract the passed object from a PPC call (a copy of it). */
5276
5277 static gfc_expr*
5278 extract_ppc_passed_object (gfc_expr *e)
5279 {
5280 gfc_expr *po;
5281 gfc_ref **ref;
5282
5283 po = gfc_get_expr ();
5284 po->expr_type = EXPR_VARIABLE;
5285 po->symtree = e->symtree;
5286 po->ref = gfc_copy_ref (e->ref);
5287 po->where = e->where;
5288
5289 /* Remove PPC reference. */
5290 ref = &po->ref;
5291 while ((*ref)->next)
5292 ref = &(*ref)->next;
5293 gfc_free_ref_list (*ref);
5294 *ref = NULL;
5295
5296 if (!gfc_resolve_expr (po))
5297 return NULL;
5298
5299 return po;
5300 }
5301
5302
5303 /* Update the actual arglist of a procedure pointer component to include the
5304 passed-object. */
5305
5306 static bool
5307 update_ppc_arglist (gfc_expr* e)
5308 {
5309 gfc_expr* po;
5310 gfc_component *ppc;
5311 gfc_typebound_proc* tb;
5312
5313 ppc = gfc_get_proc_ptr_comp (e);
5314 if (!ppc)
5315 return false;
5316
5317 tb = ppc->tb;
5318
5319 if (tb->error)
5320 return false;
5321 else if (tb->nopass)
5322 return true;
5323
5324 po = extract_ppc_passed_object (e);
5325 if (!po)
5326 return false;
5327
5328 /* F08:R739. */
5329 if (po->rank != 0)
5330 {
5331 gfc_error ("Passed-object at %L must be scalar", &e->where);
5332 return false;
5333 }
5334
5335 /* F08:C611. */
5336 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5337 {
5338 gfc_error ("Base object for procedure-pointer component call at %L is of"
5339 " ABSTRACT type '%s'", &e->where, po->ts.u.derived->name);
5340 return false;
5341 }
5342
5343 gcc_assert (tb->pass_arg_num > 0);
5344 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5345 tb->pass_arg_num,
5346 tb->pass_arg);
5347
5348 return true;
5349 }
5350
5351
5352 /* Check that the object a TBP is called on is valid, i.e. it must not be
5353 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5354
5355 static bool
5356 check_typebound_baseobject (gfc_expr* e)
5357 {
5358 gfc_expr* base;
5359 bool return_value = false;
5360
5361 base = extract_compcall_passed_object (e);
5362 if (!base)
5363 return false;
5364
5365 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5366
5367 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
5368 return false;
5369
5370 /* F08:C611. */
5371 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5372 {
5373 gfc_error ("Base object for type-bound procedure call at %L is of"
5374 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5375 goto cleanup;
5376 }
5377
5378 /* F08:C1230. If the procedure called is NOPASS,
5379 the base object must be scalar. */
5380 if (e->value.compcall.tbp->nopass && base->rank != 0)
5381 {
5382 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5383 " be scalar", &e->where);
5384 goto cleanup;
5385 }
5386
5387 return_value = true;
5388
5389 cleanup:
5390 gfc_free_expr (base);
5391 return return_value;
5392 }
5393
5394
5395 /* Resolve a call to a type-bound procedure, either function or subroutine,
5396 statically from the data in an EXPR_COMPCALL expression. The adapted
5397 arglist and the target-procedure symtree are returned. */
5398
5399 static bool
5400 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5401 gfc_actual_arglist** actual)
5402 {
5403 gcc_assert (e->expr_type == EXPR_COMPCALL);
5404 gcc_assert (!e->value.compcall.tbp->is_generic);
5405
5406 /* Update the actual arglist for PASS. */
5407 if (!update_compcall_arglist (e))
5408 return false;
5409
5410 *actual = e->value.compcall.actual;
5411 *target = e->value.compcall.tbp->u.specific;
5412
5413 gfc_free_ref_list (e->ref);
5414 e->ref = NULL;
5415 e->value.compcall.actual = NULL;
5416
5417 /* If we find a deferred typebound procedure, check for derived types
5418 that an overriding typebound procedure has not been missed. */
5419 if (e->value.compcall.name
5420 && !e->value.compcall.tbp->non_overridable
5421 && e->value.compcall.base_object
5422 && e->value.compcall.base_object->ts.type == BT_DERIVED)
5423 {
5424 gfc_symtree *st;
5425 gfc_symbol *derived;
5426
5427 /* Use the derived type of the base_object. */
5428 derived = e->value.compcall.base_object->ts.u.derived;
5429 st = NULL;
5430
5431 /* If necessary, go through the inheritance chain. */
5432 while (!st && derived)
5433 {
5434 /* Look for the typebound procedure 'name'. */
5435 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5436 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5437 e->value.compcall.name);
5438 if (!st)
5439 derived = gfc_get_derived_super_type (derived);
5440 }
5441
5442 /* Now find the specific name in the derived type namespace. */
5443 if (st && st->n.tb && st->n.tb->u.specific)
5444 gfc_find_sym_tree (st->n.tb->u.specific->name,
5445 derived->ns, 1, &st);
5446 if (st)
5447 *target = st;
5448 }
5449 return true;
5450 }
5451
5452
5453 /* Get the ultimate declared type from an expression. In addition,
5454 return the last class/derived type reference and the copy of the
5455 reference list. If check_types is set true, derived types are
5456 identified as well as class references. */
5457 static gfc_symbol*
5458 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5459 gfc_expr *e, bool check_types)
5460 {
5461 gfc_symbol *declared;
5462 gfc_ref *ref;
5463
5464 declared = NULL;
5465 if (class_ref)
5466 *class_ref = NULL;
5467 if (new_ref)
5468 *new_ref = gfc_copy_ref (e->ref);
5469
5470 for (ref = e->ref; ref; ref = ref->next)
5471 {
5472 if (ref->type != REF_COMPONENT)
5473 continue;
5474
5475 if ((ref->u.c.component->ts.type == BT_CLASS
5476 || (check_types && ref->u.c.component->ts.type == BT_DERIVED))
5477 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
5478 {
5479 declared = ref->u.c.component->ts.u.derived;
5480 if (class_ref)
5481 *class_ref = ref;
5482 }
5483 }
5484
5485 if (declared == NULL)
5486 declared = e->symtree->n.sym->ts.u.derived;
5487
5488 return declared;
5489 }
5490
5491
5492 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5493 which of the specific bindings (if any) matches the arglist and transform
5494 the expression into a call of that binding. */
5495
5496 static bool
5497 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5498 {
5499 gfc_typebound_proc* genproc;
5500 const char* genname;
5501 gfc_symtree *st;
5502 gfc_symbol *derived;
5503
5504 gcc_assert (e->expr_type == EXPR_COMPCALL);
5505 genname = e->value.compcall.name;
5506 genproc = e->value.compcall.tbp;
5507
5508 if (!genproc->is_generic)
5509 return true;
5510
5511 /* Try the bindings on this type and in the inheritance hierarchy. */
5512 for (; genproc; genproc = genproc->overridden)
5513 {
5514 gfc_tbp_generic* g;
5515
5516 gcc_assert (genproc->is_generic);
5517 for (g = genproc->u.generic; g; g = g->next)
5518 {
5519 gfc_symbol* target;
5520 gfc_actual_arglist* args;
5521 bool matches;
5522
5523 gcc_assert (g->specific);
5524
5525 if (g->specific->error)
5526 continue;
5527
5528 target = g->specific->u.specific->n.sym;
5529
5530 /* Get the right arglist by handling PASS/NOPASS. */
5531 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5532 if (!g->specific->nopass)
5533 {
5534 gfc_expr* po;
5535 po = extract_compcall_passed_object (e);
5536 if (!po)
5537 {
5538 gfc_free_actual_arglist (args);
5539 return false;
5540 }
5541
5542 gcc_assert (g->specific->pass_arg_num > 0);
5543 gcc_assert (!g->specific->error);
5544 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5545 g->specific->pass_arg);
5546 }
5547 resolve_actual_arglist (args, target->attr.proc,
5548 is_external_proc (target)
5549 && gfc_sym_get_dummy_args (target) == NULL);
5550
5551 /* Check if this arglist matches the formal. */
5552 matches = gfc_arglist_matches_symbol (&args, target);
5553
5554 /* Clean up and break out of the loop if we've found it. */
5555 gfc_free_actual_arglist (args);
5556 if (matches)
5557 {
5558 e->value.compcall.tbp = g->specific;
5559 genname = g->specific_st->name;
5560 /* Pass along the name for CLASS methods, where the vtab
5561 procedure pointer component has to be referenced. */
5562 if (name)
5563 *name = genname;
5564 goto success;
5565 }
5566 }
5567 }
5568
5569 /* Nothing matching found! */
5570 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5571 " '%s' at %L", genname, &e->where);
5572 return false;
5573
5574 success:
5575 /* Make sure that we have the right specific instance for the name. */
5576 derived = get_declared_from_expr (NULL, NULL, e, true);
5577
5578 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
5579 if (st)
5580 e->value.compcall.tbp = st->n.tb;
5581
5582 return true;
5583 }
5584
5585
5586 /* Resolve a call to a type-bound subroutine. */
5587
5588 static bool
5589 resolve_typebound_call (gfc_code* c, const char **name)
5590 {
5591 gfc_actual_arglist* newactual;
5592 gfc_symtree* target;
5593
5594 /* Check that's really a SUBROUTINE. */
5595 if (!c->expr1->value.compcall.tbp->subroutine)
5596 {
5597 gfc_error ("'%s' at %L should be a SUBROUTINE",
5598 c->expr1->value.compcall.name, &c->loc);
5599 return false;
5600 }
5601
5602 if (!check_typebound_baseobject (c->expr1))
5603 return false;
5604
5605 /* Pass along the name for CLASS methods, where the vtab
5606 procedure pointer component has to be referenced. */
5607 if (name)
5608 *name = c->expr1->value.compcall.name;
5609
5610 if (!resolve_typebound_generic_call (c->expr1, name))
5611 return false;
5612
5613 /* Transform into an ordinary EXEC_CALL for now. */
5614
5615 if (!resolve_typebound_static (c->expr1, &target, &newactual))
5616 return false;
5617
5618 c->ext.actual = newactual;
5619 c->symtree = target;
5620 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5621
5622 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5623
5624 gfc_free_expr (c->expr1);
5625 c->expr1 = gfc_get_expr ();
5626 c->expr1->expr_type = EXPR_FUNCTION;
5627 c->expr1->symtree = target;
5628 c->expr1->where = c->loc;
5629
5630 return resolve_call (c);
5631 }
5632
5633
5634 /* Resolve a component-call expression. */
5635 static bool
5636 resolve_compcall (gfc_expr* e, const char **name)
5637 {
5638 gfc_actual_arglist* newactual;
5639 gfc_symtree* target;
5640
5641 /* Check that's really a FUNCTION. */
5642 if (!e->value.compcall.tbp->function)
5643 {
5644 gfc_error ("'%s' at %L should be a FUNCTION",
5645 e->value.compcall.name, &e->where);
5646 return false;
5647 }
5648
5649 /* These must not be assign-calls! */
5650 gcc_assert (!e->value.compcall.assign);
5651
5652 if (!check_typebound_baseobject (e))
5653 return false;
5654
5655 /* Pass along the name for CLASS methods, where the vtab
5656 procedure pointer component has to be referenced. */
5657 if (name)
5658 *name = e->value.compcall.name;
5659
5660 if (!resolve_typebound_generic_call (e, name))
5661 return false;
5662 gcc_assert (!e->value.compcall.tbp->is_generic);
5663
5664 /* Take the rank from the function's symbol. */
5665 if (e->value.compcall.tbp->u.specific->n.sym->as)
5666 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5667
5668 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5669 arglist to the TBP's binding target. */
5670
5671 if (!resolve_typebound_static (e, &target, &newactual))
5672 return false;
5673
5674 e->value.function.actual = newactual;
5675 e->value.function.name = NULL;
5676 e->value.function.esym = target->n.sym;
5677 e->value.function.isym = NULL;
5678 e->symtree = target;
5679 e->ts = target->n.sym->ts;
5680 e->expr_type = EXPR_FUNCTION;
5681
5682 /* Resolution is not necessary if this is a class subroutine; this
5683 function only has to identify the specific proc. Resolution of
5684 the call will be done next in resolve_typebound_call. */
5685 return gfc_resolve_expr (e);
5686 }
5687
5688
5689 static bool resolve_fl_derived (gfc_symbol *sym);
5690
5691
5692 /* Resolve a typebound function, or 'method'. First separate all
5693 the non-CLASS references by calling resolve_compcall directly. */
5694
5695 static bool
5696 resolve_typebound_function (gfc_expr* e)
5697 {
5698 gfc_symbol *declared;
5699 gfc_component *c;
5700 gfc_ref *new_ref;
5701 gfc_ref *class_ref;
5702 gfc_symtree *st;
5703 const char *name;
5704 gfc_typespec ts;
5705 gfc_expr *expr;
5706 bool overridable;
5707
5708 st = e->symtree;
5709
5710 /* Deal with typebound operators for CLASS objects. */
5711 expr = e->value.compcall.base_object;
5712 overridable = !e->value.compcall.tbp->non_overridable;
5713 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
5714 {
5715 /* If the base_object is not a variable, the corresponding actual
5716 argument expression must be stored in e->base_expression so
5717 that the corresponding tree temporary can be used as the base
5718 object in gfc_conv_procedure_call. */
5719 if (expr->expr_type != EXPR_VARIABLE)
5720 {
5721 gfc_actual_arglist *args;
5722
5723 for (args= e->value.function.actual; args; args = args->next)
5724 {
5725 if (expr == args->expr)
5726 expr = args->expr;
5727 }
5728 }
5729
5730 /* Since the typebound operators are generic, we have to ensure
5731 that any delays in resolution are corrected and that the vtab
5732 is present. */
5733 ts = expr->ts;
5734 declared = ts.u.derived;
5735 c = gfc_find_component (declared, "_vptr", true, true);
5736 if (c->ts.u.derived == NULL)
5737 c->ts.u.derived = gfc_find_derived_vtab (declared);
5738
5739 if (!resolve_compcall (e, &name))
5740 return false;
5741
5742 /* Use the generic name if it is there. */
5743 name = name ? name : e->value.function.esym->name;
5744 e->symtree = expr->symtree;
5745 e->ref = gfc_copy_ref (expr->ref);
5746 get_declared_from_expr (&class_ref, NULL, e, false);
5747
5748 /* Trim away the extraneous references that emerge from nested
5749 use of interface.c (extend_expr). */
5750 if (class_ref && class_ref->next)
5751 {
5752 gfc_free_ref_list (class_ref->next);
5753 class_ref->next = NULL;
5754 }
5755 else if (e->ref && !class_ref)
5756 {
5757 gfc_free_ref_list (e->ref);
5758 e->ref = NULL;
5759 }
5760
5761 gfc_add_vptr_component (e);
5762 gfc_add_component_ref (e, name);
5763 e->value.function.esym = NULL;
5764 if (expr->expr_type != EXPR_VARIABLE)
5765 e->base_expr = expr;
5766 return true;
5767 }
5768
5769 if (st == NULL)
5770 return resolve_compcall (e, NULL);
5771
5772 if (!resolve_ref (e))
5773 return false;
5774
5775 /* Get the CLASS declared type. */
5776 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
5777
5778 if (!resolve_fl_derived (declared))
5779 return false;
5780
5781 /* Weed out cases of the ultimate component being a derived type. */
5782 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5783 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5784 {
5785 gfc_free_ref_list (new_ref);
5786 return resolve_compcall (e, NULL);
5787 }
5788
5789 c = gfc_find_component (declared, "_data", true, true);
5790 declared = c->ts.u.derived;
5791
5792 /* Treat the call as if it is a typebound procedure, in order to roll
5793 out the correct name for the specific function. */
5794 if (!resolve_compcall (e, &name))
5795 {
5796 gfc_free_ref_list (new_ref);
5797 return false;
5798 }
5799 ts = e->ts;
5800
5801 if (overridable)
5802 {
5803 /* Convert the expression to a procedure pointer component call. */
5804 e->value.function.esym = NULL;
5805 e->symtree = st;
5806
5807 if (new_ref)
5808 e->ref = new_ref;
5809
5810 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5811 gfc_add_vptr_component (e);
5812 gfc_add_component_ref (e, name);
5813
5814 /* Recover the typespec for the expression. This is really only
5815 necessary for generic procedures, where the additional call
5816 to gfc_add_component_ref seems to throw the collection of the
5817 correct typespec. */
5818 e->ts = ts;
5819 }
5820 else if (new_ref)
5821 gfc_free_ref_list (new_ref);
5822
5823 return true;
5824 }
5825
5826 /* Resolve a typebound subroutine, or 'method'. First separate all
5827 the non-CLASS references by calling resolve_typebound_call
5828 directly. */
5829
5830 static bool
5831 resolve_typebound_subroutine (gfc_code *code)
5832 {
5833 gfc_symbol *declared;
5834 gfc_component *c;
5835 gfc_ref *new_ref;
5836 gfc_ref *class_ref;
5837 gfc_symtree *st;
5838 const char *name;
5839 gfc_typespec ts;
5840 gfc_expr *expr;
5841 bool overridable;
5842
5843 st = code->expr1->symtree;
5844
5845 /* Deal with typebound operators for CLASS objects. */
5846 expr = code->expr1->value.compcall.base_object;
5847 overridable = !code->expr1->value.compcall.tbp->non_overridable;
5848 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
5849 {
5850 /* If the base_object is not a variable, the corresponding actual
5851 argument expression must be stored in e->base_expression so
5852 that the corresponding tree temporary can be used as the base
5853 object in gfc_conv_procedure_call. */
5854 if (expr->expr_type != EXPR_VARIABLE)
5855 {
5856 gfc_actual_arglist *args;
5857
5858 args= code->expr1->value.function.actual;
5859 for (; args; args = args->next)
5860 if (expr == args->expr)
5861 expr = args->expr;
5862 }
5863
5864 /* Since the typebound operators are generic, we have to ensure
5865 that any delays in resolution are corrected and that the vtab
5866 is present. */
5867 declared = expr->ts.u.derived;
5868 c = gfc_find_component (declared, "_vptr", true, true);
5869 if (c->ts.u.derived == NULL)
5870 c->ts.u.derived = gfc_find_derived_vtab (declared);
5871
5872 if (!resolve_typebound_call (code, &name))
5873 return false;
5874
5875 /* Use the generic name if it is there. */
5876 name = name ? name : code->expr1->value.function.esym->name;
5877 code->expr1->symtree = expr->symtree;
5878 code->expr1->ref = gfc_copy_ref (expr->ref);
5879
5880 /* Trim away the extraneous references that emerge from nested
5881 use of interface.c (extend_expr). */
5882 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
5883 if (class_ref && class_ref->next)
5884 {
5885 gfc_free_ref_list (class_ref->next);
5886 class_ref->next = NULL;
5887 }
5888 else if (code->expr1->ref && !class_ref)
5889 {
5890 gfc_free_ref_list (code->expr1->ref);
5891 code->expr1->ref = NULL;
5892 }
5893
5894 /* Now use the procedure in the vtable. */
5895 gfc_add_vptr_component (code->expr1);
5896 gfc_add_component_ref (code->expr1, name);
5897 code->expr1->value.function.esym = NULL;
5898 if (expr->expr_type != EXPR_VARIABLE)
5899 code->expr1->base_expr = expr;
5900 return true;
5901 }
5902
5903 if (st == NULL)
5904 return resolve_typebound_call (code, NULL);
5905
5906 if (!resolve_ref (code->expr1))
5907 return false;
5908
5909 /* Get the CLASS declared type. */
5910 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
5911
5912 /* Weed out cases of the ultimate component being a derived type. */
5913 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5914 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5915 {
5916 gfc_free_ref_list (new_ref);
5917 return resolve_typebound_call (code, NULL);
5918 }
5919
5920 if (!resolve_typebound_call (code, &name))
5921 {
5922 gfc_free_ref_list (new_ref);
5923 return false;
5924 }
5925 ts = code->expr1->ts;
5926
5927 if (overridable)
5928 {
5929 /* Convert the expression to a procedure pointer component call. */
5930 code->expr1->value.function.esym = NULL;
5931 code->expr1->symtree = st;
5932
5933 if (new_ref)
5934 code->expr1->ref = new_ref;
5935
5936 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5937 gfc_add_vptr_component (code->expr1);
5938 gfc_add_component_ref (code->expr1, name);
5939
5940 /* Recover the typespec for the expression. This is really only
5941 necessary for generic procedures, where the additional call
5942 to gfc_add_component_ref seems to throw the collection of the
5943 correct typespec. */
5944 code->expr1->ts = ts;
5945 }
5946 else if (new_ref)
5947 gfc_free_ref_list (new_ref);
5948
5949 return true;
5950 }
5951
5952
5953 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5954
5955 static bool
5956 resolve_ppc_call (gfc_code* c)
5957 {
5958 gfc_component *comp;
5959
5960 comp = gfc_get_proc_ptr_comp (c->expr1);
5961 gcc_assert (comp != NULL);
5962
5963 c->resolved_sym = c->expr1->symtree->n.sym;
5964 c->expr1->expr_type = EXPR_VARIABLE;
5965
5966 if (!comp->attr.subroutine)
5967 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5968
5969 if (!resolve_ref (c->expr1))
5970 return false;
5971
5972 if (!update_ppc_arglist (c->expr1))
5973 return false;
5974
5975 c->ext.actual = c->expr1->value.compcall.actual;
5976
5977 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5978 !(comp->ts.interface
5979 && comp->ts.interface->formal)))
5980 return false;
5981
5982 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
5983
5984 return true;
5985 }
5986
5987
5988 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5989
5990 static bool
5991 resolve_expr_ppc (gfc_expr* e)
5992 {
5993 gfc_component *comp;
5994
5995 comp = gfc_get_proc_ptr_comp (e);
5996 gcc_assert (comp != NULL);
5997
5998 /* Convert to EXPR_FUNCTION. */
5999 e->expr_type = EXPR_FUNCTION;
6000 e->value.function.isym = NULL;
6001 e->value.function.actual = e->value.compcall.actual;
6002 e->ts = comp->ts;
6003 if (comp->as != NULL)
6004 e->rank = comp->as->rank;
6005
6006 if (!comp->attr.function)
6007 gfc_add_function (&comp->attr, comp->name, &e->where);
6008
6009 if (!resolve_ref (e))
6010 return false;
6011
6012 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6013 !(comp->ts.interface
6014 && comp->ts.interface->formal)))
6015 return false;
6016
6017 if (!update_ppc_arglist (e))
6018 return false;
6019
6020 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
6021
6022 return true;
6023 }
6024
6025
6026 static bool
6027 gfc_is_expandable_expr (gfc_expr *e)
6028 {
6029 gfc_constructor *con;
6030
6031 if (e->expr_type == EXPR_ARRAY)
6032 {
6033 /* Traverse the constructor looking for variables that are flavor
6034 parameter. Parameters must be expanded since they are fully used at
6035 compile time. */
6036 con = gfc_constructor_first (e->value.constructor);
6037 for (; con; con = gfc_constructor_next (con))
6038 {
6039 if (con->expr->expr_type == EXPR_VARIABLE
6040 && con->expr->symtree
6041 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
6042 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6043 return true;
6044 if (con->expr->expr_type == EXPR_ARRAY
6045 && gfc_is_expandable_expr (con->expr))
6046 return true;
6047 }
6048 }
6049
6050 return false;
6051 }
6052
6053 /* Resolve an expression. That is, make sure that types of operands agree
6054 with their operators, intrinsic operators are converted to function calls
6055 for overloaded types and unresolved function references are resolved. */
6056
6057 bool
6058 gfc_resolve_expr (gfc_expr *e)
6059 {
6060 bool t;
6061 bool inquiry_save, actual_arg_save, first_actual_arg_save;
6062
6063 if (e == NULL)
6064 return true;
6065
6066 /* inquiry_argument only applies to variables. */
6067 inquiry_save = inquiry_argument;
6068 actual_arg_save = actual_arg;
6069 first_actual_arg_save = first_actual_arg;
6070
6071 if (e->expr_type != EXPR_VARIABLE)
6072 {
6073 inquiry_argument = false;
6074 actual_arg = false;
6075 first_actual_arg = false;
6076 }
6077
6078 switch (e->expr_type)
6079 {
6080 case EXPR_OP:
6081 t = resolve_operator (e);
6082 break;
6083
6084 case EXPR_FUNCTION:
6085 case EXPR_VARIABLE:
6086
6087 if (check_host_association (e))
6088 t = resolve_function (e);
6089 else
6090 {
6091 t = resolve_variable (e);
6092 if (t)
6093 expression_rank (e);
6094 }
6095
6096 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6097 && e->ref->type != REF_SUBSTRING)
6098 gfc_resolve_substring_charlen (e);
6099
6100 break;
6101
6102 case EXPR_COMPCALL:
6103 t = resolve_typebound_function (e);
6104 break;
6105
6106 case EXPR_SUBSTRING:
6107 t = resolve_ref (e);
6108 break;
6109
6110 case EXPR_CONSTANT:
6111 case EXPR_NULL:
6112 t = true;
6113 break;
6114
6115 case EXPR_PPC:
6116 t = resolve_expr_ppc (e);
6117 break;
6118
6119 case EXPR_ARRAY:
6120 t = false;
6121 if (!resolve_ref (e))
6122 break;
6123
6124 t = gfc_resolve_array_constructor (e);
6125 /* Also try to expand a constructor. */
6126 if (t)
6127 {
6128 expression_rank (e);
6129 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6130 gfc_expand_constructor (e, false);
6131 }
6132
6133 /* This provides the opportunity for the length of constructors with
6134 character valued function elements to propagate the string length
6135 to the expression. */
6136 if (t && e->ts.type == BT_CHARACTER)
6137 {
6138 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6139 here rather then add a duplicate test for it above. */
6140 gfc_expand_constructor (e, false);
6141 t = gfc_resolve_character_array_constructor (e);
6142 }
6143
6144 break;
6145
6146 case EXPR_STRUCTURE:
6147 t = resolve_ref (e);
6148 if (!t)
6149 break;
6150
6151 t = resolve_structure_cons (e, 0);
6152 if (!t)
6153 break;
6154
6155 t = gfc_simplify_expr (e, 0);
6156 break;
6157
6158 default:
6159 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6160 }
6161
6162 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
6163 fixup_charlen (e);
6164
6165 inquiry_argument = inquiry_save;
6166 actual_arg = actual_arg_save;
6167 first_actual_arg = first_actual_arg_save;
6168
6169 return t;
6170 }
6171
6172
6173 /* Resolve an expression from an iterator. They must be scalar and have
6174 INTEGER or (optionally) REAL type. */
6175
6176 static bool
6177 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6178 const char *name_msgid)
6179 {
6180 if (!gfc_resolve_expr (expr))
6181 return false;
6182
6183 if (expr->rank != 0)
6184 {
6185 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6186 return false;
6187 }
6188
6189 if (expr->ts.type != BT_INTEGER)
6190 {
6191 if (expr->ts.type == BT_REAL)
6192 {
6193 if (real_ok)
6194 return gfc_notify_std (GFC_STD_F95_DEL,
6195 "%s at %L must be integer",
6196 _(name_msgid), &expr->where);
6197 else
6198 {
6199 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6200 &expr->where);
6201 return false;
6202 }
6203 }
6204 else
6205 {
6206 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6207 return false;
6208 }
6209 }
6210 return true;
6211 }
6212
6213
6214 /* Resolve the expressions in an iterator structure. If REAL_OK is
6215 false allow only INTEGER type iterators, otherwise allow REAL types.
6216 Set own_scope to true for ac-implied-do and data-implied-do as those
6217 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
6218
6219 bool
6220 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
6221 {
6222 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
6223 return false;
6224
6225 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
6226 _("iterator variable")))
6227 return false;
6228
6229 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
6230 "Start expression in DO loop"))
6231 return false;
6232
6233 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
6234 "End expression in DO loop"))
6235 return false;
6236
6237 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
6238 "Step expression in DO loop"))
6239 return false;
6240
6241 if (iter->step->expr_type == EXPR_CONSTANT)
6242 {
6243 if ((iter->step->ts.type == BT_INTEGER
6244 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6245 || (iter->step->ts.type == BT_REAL
6246 && mpfr_sgn (iter->step->value.real) == 0))
6247 {
6248 gfc_error ("Step expression in DO loop at %L cannot be zero",
6249 &iter->step->where);
6250 return false;
6251 }
6252 }
6253
6254 /* Convert start, end, and step to the same type as var. */
6255 if (iter->start->ts.kind != iter->var->ts.kind
6256 || iter->start->ts.type != iter->var->ts.type)
6257 gfc_convert_type (iter->start, &iter->var->ts, 2);
6258
6259 if (iter->end->ts.kind != iter->var->ts.kind
6260 || iter->end->ts.type != iter->var->ts.type)
6261 gfc_convert_type (iter->end, &iter->var->ts, 2);
6262
6263 if (iter->step->ts.kind != iter->var->ts.kind
6264 || iter->step->ts.type != iter->var->ts.type)
6265 gfc_convert_type (iter->step, &iter->var->ts, 2);
6266
6267 if (iter->start->expr_type == EXPR_CONSTANT
6268 && iter->end->expr_type == EXPR_CONSTANT
6269 && iter->step->expr_type == EXPR_CONSTANT)
6270 {
6271 int sgn, cmp;
6272 if (iter->start->ts.type == BT_INTEGER)
6273 {
6274 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6275 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6276 }
6277 else
6278 {
6279 sgn = mpfr_sgn (iter->step->value.real);
6280 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6281 }
6282 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
6283 gfc_warning ("DO loop at %L will be executed zero times",
6284 &iter->step->where);
6285 }
6286
6287 return true;
6288 }
6289
6290
6291 /* Traversal function for find_forall_index. f == 2 signals that
6292 that variable itself is not to be checked - only the references. */
6293
6294 static bool
6295 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6296 {
6297 if (expr->expr_type != EXPR_VARIABLE)
6298 return false;
6299
6300 /* A scalar assignment */
6301 if (!expr->ref || *f == 1)
6302 {
6303 if (expr->symtree->n.sym == sym)
6304 return true;
6305 else
6306 return false;
6307 }
6308
6309 if (*f == 2)
6310 *f = 1;
6311 return false;
6312 }
6313
6314
6315 /* Check whether the FORALL index appears in the expression or not.
6316 Returns true if SYM is found in EXPR. */
6317
6318 bool
6319 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6320 {
6321 if (gfc_traverse_expr (expr, sym, forall_index, f))
6322 return true;
6323 else
6324 return false;
6325 }
6326
6327
6328 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6329 to be a scalar INTEGER variable. The subscripts and stride are scalar
6330 INTEGERs, and if stride is a constant it must be nonzero.
6331 Furthermore "A subscript or stride in a forall-triplet-spec shall
6332 not contain a reference to any index-name in the
6333 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6334
6335 static void
6336 resolve_forall_iterators (gfc_forall_iterator *it)
6337 {
6338 gfc_forall_iterator *iter, *iter2;
6339
6340 for (iter = it; iter; iter = iter->next)
6341 {
6342 if (gfc_resolve_expr (iter->var)
6343 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6344 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6345 &iter->var->where);
6346
6347 if (gfc_resolve_expr (iter->start)
6348 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6349 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6350 &iter->start->where);
6351 if (iter->var->ts.kind != iter->start->ts.kind)
6352 gfc_convert_type (iter->start, &iter->var->ts, 1);
6353
6354 if (gfc_resolve_expr (iter->end)
6355 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6356 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6357 &iter->end->where);
6358 if (iter->var->ts.kind != iter->end->ts.kind)
6359 gfc_convert_type (iter->end, &iter->var->ts, 1);
6360
6361 if (gfc_resolve_expr (iter->stride))
6362 {
6363 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6364 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6365 &iter->stride->where, "INTEGER");
6366
6367 if (iter->stride->expr_type == EXPR_CONSTANT
6368 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
6369 gfc_error ("FORALL stride expression at %L cannot be zero",
6370 &iter->stride->where);
6371 }
6372 if (iter->var->ts.kind != iter->stride->ts.kind)
6373 gfc_convert_type (iter->stride, &iter->var->ts, 1);
6374 }
6375
6376 for (iter = it; iter; iter = iter->next)
6377 for (iter2 = iter; iter2; iter2 = iter2->next)
6378 {
6379 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
6380 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
6381 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
6382 gfc_error ("FORALL index '%s' may not appear in triplet "
6383 "specification at %L", iter->var->symtree->name,
6384 &iter2->start->where);
6385 }
6386 }
6387
6388
6389 /* Given a pointer to a symbol that is a derived type, see if it's
6390 inaccessible, i.e. if it's defined in another module and the components are
6391 PRIVATE. The search is recursive if necessary. Returns zero if no
6392 inaccessible components are found, nonzero otherwise. */
6393
6394 static int
6395 derived_inaccessible (gfc_symbol *sym)
6396 {
6397 gfc_component *c;
6398
6399 if (sym->attr.use_assoc && sym->attr.private_comp)
6400 return 1;
6401
6402 for (c = sym->components; c; c = c->next)
6403 {
6404 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6405 return 1;
6406 }
6407
6408 return 0;
6409 }
6410
6411
6412 /* Resolve the argument of a deallocate expression. The expression must be
6413 a pointer or a full array. */
6414
6415 static bool
6416 resolve_deallocate_expr (gfc_expr *e)
6417 {
6418 symbol_attribute attr;
6419 int allocatable, pointer;
6420 gfc_ref *ref;
6421 gfc_symbol *sym;
6422 gfc_component *c;
6423 bool unlimited;
6424
6425 if (!gfc_resolve_expr (e))
6426 return false;
6427
6428 if (e->expr_type != EXPR_VARIABLE)
6429 goto bad;
6430
6431 sym = e->symtree->n.sym;
6432 unlimited = UNLIMITED_POLY(sym);
6433
6434 if (sym->ts.type == BT_CLASS)
6435 {
6436 allocatable = CLASS_DATA (sym)->attr.allocatable;
6437 pointer = CLASS_DATA (sym)->attr.class_pointer;
6438 }
6439 else
6440 {
6441 allocatable = sym->attr.allocatable;
6442 pointer = sym->attr.pointer;
6443 }
6444 for (ref = e->ref; ref; ref = ref->next)
6445 {
6446 switch (ref->type)
6447 {
6448 case REF_ARRAY:
6449 if (ref->u.ar.type != AR_FULL
6450 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6451 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
6452 allocatable = 0;
6453 break;
6454
6455 case REF_COMPONENT:
6456 c = ref->u.c.component;
6457 if (c->ts.type == BT_CLASS)
6458 {
6459 allocatable = CLASS_DATA (c)->attr.allocatable;
6460 pointer = CLASS_DATA (c)->attr.class_pointer;
6461 }
6462 else
6463 {
6464 allocatable = c->attr.allocatable;
6465 pointer = c->attr.pointer;
6466 }
6467 break;
6468
6469 case REF_SUBSTRING:
6470 allocatable = 0;
6471 break;
6472 }
6473 }
6474
6475 attr = gfc_expr_attr (e);
6476
6477 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
6478 {
6479 bad:
6480 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6481 &e->where);
6482 return false;
6483 }
6484
6485 /* F2008, C644. */
6486 if (gfc_is_coindexed (e))
6487 {
6488 gfc_error ("Coindexed allocatable object at %L", &e->where);
6489 return false;
6490 }
6491
6492 if (pointer
6493 && !gfc_check_vardef_context (e, true, true, false,
6494 _("DEALLOCATE object")))
6495 return false;
6496 if (!gfc_check_vardef_context (e, false, true, false,
6497 _("DEALLOCATE object")))
6498 return false;
6499
6500 return true;
6501 }
6502
6503
6504 /* Returns true if the expression e contains a reference to the symbol sym. */
6505 static bool
6506 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6507 {
6508 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6509 return true;
6510
6511 return false;
6512 }
6513
6514 bool
6515 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6516 {
6517 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6518 }
6519
6520
6521 /* Given the expression node e for an allocatable/pointer of derived type to be
6522 allocated, get the expression node to be initialized afterwards (needed for
6523 derived types with default initializers, and derived types with allocatable
6524 components that need nullification.) */
6525
6526 gfc_expr *
6527 gfc_expr_to_initialize (gfc_expr *e)
6528 {
6529 gfc_expr *result;
6530 gfc_ref *ref;
6531 int i;
6532
6533 result = gfc_copy_expr (e);
6534
6535 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6536 for (ref = result->ref; ref; ref = ref->next)
6537 if (ref->type == REF_ARRAY && ref->next == NULL)
6538 {
6539 ref->u.ar.type = AR_FULL;
6540
6541 for (i = 0; i < ref->u.ar.dimen; i++)
6542 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6543
6544 break;
6545 }
6546
6547 gfc_free_shape (&result->shape, result->rank);
6548
6549 /* Recalculate rank, shape, etc. */
6550 gfc_resolve_expr (result);
6551 return result;
6552 }
6553
6554
6555 /* If the last ref of an expression is an array ref, return a copy of the
6556 expression with that one removed. Otherwise, a copy of the original
6557 expression. This is used for allocate-expressions and pointer assignment
6558 LHS, where there may be an array specification that needs to be stripped
6559 off when using gfc_check_vardef_context. */
6560
6561 static gfc_expr*
6562 remove_last_array_ref (gfc_expr* e)
6563 {
6564 gfc_expr* e2;
6565 gfc_ref** r;
6566
6567 e2 = gfc_copy_expr (e);
6568 for (r = &e2->ref; *r; r = &(*r)->next)
6569 if ((*r)->type == REF_ARRAY && !(*r)->next)
6570 {
6571 gfc_free_ref_list (*r);
6572 *r = NULL;
6573 break;
6574 }
6575
6576 return e2;
6577 }
6578
6579
6580 /* Used in resolve_allocate_expr to check that a allocation-object and
6581 a source-expr are conformable. This does not catch all possible
6582 cases; in particular a runtime checking is needed. */
6583
6584 static bool
6585 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6586 {
6587 gfc_ref *tail;
6588 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6589
6590 /* First compare rank. */
6591 if (tail && e1->rank != tail->u.ar.as->rank)
6592 {
6593 gfc_error ("Source-expr at %L must be scalar or have the "
6594 "same rank as the allocate-object at %L",
6595 &e1->where, &e2->where);
6596 return false;
6597 }
6598
6599 if (e1->shape)
6600 {
6601 int i;
6602 mpz_t s;
6603
6604 mpz_init (s);
6605
6606 for (i = 0; i < e1->rank; i++)
6607 {
6608 if (tail->u.ar.start[i] == NULL)
6609 break;
6610
6611 if (tail->u.ar.end[i])
6612 {
6613 mpz_set (s, tail->u.ar.end[i]->value.integer);
6614 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6615 mpz_add_ui (s, s, 1);
6616 }
6617 else
6618 {
6619 mpz_set (s, tail->u.ar.start[i]->value.integer);
6620 }
6621
6622 if (mpz_cmp (e1->shape[i], s) != 0)
6623 {
6624 gfc_error ("Source-expr at %L and allocate-object at %L must "
6625 "have the same shape", &e1->where, &e2->where);
6626 mpz_clear (s);
6627 return false;
6628 }
6629 }
6630
6631 mpz_clear (s);
6632 }
6633
6634 return true;
6635 }
6636
6637
6638 /* Resolve the expression in an ALLOCATE statement, doing the additional
6639 checks to see whether the expression is OK or not. The expression must
6640 have a trailing array reference that gives the size of the array. */
6641
6642 static bool
6643 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6644 {
6645 int i, pointer, allocatable, dimension, is_abstract;
6646 int codimension;
6647 bool coindexed;
6648 bool unlimited;
6649 symbol_attribute attr;
6650 gfc_ref *ref, *ref2;
6651 gfc_expr *e2;
6652 gfc_array_ref *ar;
6653 gfc_symbol *sym = NULL;
6654 gfc_alloc *a;
6655 gfc_component *c;
6656 bool t;
6657
6658 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
6659 checking of coarrays. */
6660 for (ref = e->ref; ref; ref = ref->next)
6661 if (ref->next == NULL)
6662 break;
6663
6664 if (ref && ref->type == REF_ARRAY)
6665 ref->u.ar.in_allocate = true;
6666
6667 if (!gfc_resolve_expr (e))
6668 goto failure;
6669
6670 /* Make sure the expression is allocatable or a pointer. If it is
6671 pointer, the next-to-last reference must be a pointer. */
6672
6673 ref2 = NULL;
6674 if (e->symtree)
6675 sym = e->symtree->n.sym;
6676
6677 /* Check whether ultimate component is abstract and CLASS. */
6678 is_abstract = 0;
6679
6680 /* Is the allocate-object unlimited polymorphic? */
6681 unlimited = UNLIMITED_POLY(e);
6682
6683 if (e->expr_type != EXPR_VARIABLE)
6684 {
6685 allocatable = 0;
6686 attr = gfc_expr_attr (e);
6687 pointer = attr.pointer;
6688 dimension = attr.dimension;
6689 codimension = attr.codimension;
6690 }
6691 else
6692 {
6693 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
6694 {
6695 allocatable = CLASS_DATA (sym)->attr.allocatable;
6696 pointer = CLASS_DATA (sym)->attr.class_pointer;
6697 dimension = CLASS_DATA (sym)->attr.dimension;
6698 codimension = CLASS_DATA (sym)->attr.codimension;
6699 is_abstract = CLASS_DATA (sym)->attr.abstract;
6700 }
6701 else
6702 {
6703 allocatable = sym->attr.allocatable;
6704 pointer = sym->attr.pointer;
6705 dimension = sym->attr.dimension;
6706 codimension = sym->attr.codimension;
6707 }
6708
6709 coindexed = false;
6710
6711 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6712 {
6713 switch (ref->type)
6714 {
6715 case REF_ARRAY:
6716 if (ref->u.ar.codimen > 0)
6717 {
6718 int n;
6719 for (n = ref->u.ar.dimen;
6720 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
6721 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
6722 {
6723 coindexed = true;
6724 break;
6725 }
6726 }
6727
6728 if (ref->next != NULL)
6729 pointer = 0;
6730 break;
6731
6732 case REF_COMPONENT:
6733 /* F2008, C644. */
6734 if (coindexed)
6735 {
6736 gfc_error ("Coindexed allocatable object at %L",
6737 &e->where);
6738 goto failure;
6739 }
6740
6741 c = ref->u.c.component;
6742 if (c->ts.type == BT_CLASS)
6743 {
6744 allocatable = CLASS_DATA (c)->attr.allocatable;
6745 pointer = CLASS_DATA (c)->attr.class_pointer;
6746 dimension = CLASS_DATA (c)->attr.dimension;
6747 codimension = CLASS_DATA (c)->attr.codimension;
6748 is_abstract = CLASS_DATA (c)->attr.abstract;
6749 }
6750 else
6751 {
6752 allocatable = c->attr.allocatable;
6753 pointer = c->attr.pointer;
6754 dimension = c->attr.dimension;
6755 codimension = c->attr.codimension;
6756 is_abstract = c->attr.abstract;
6757 }
6758 break;
6759
6760 case REF_SUBSTRING:
6761 allocatable = 0;
6762 pointer = 0;
6763 break;
6764 }
6765 }
6766 }
6767
6768 /* Check for F08:C628. */
6769 if (allocatable == 0 && pointer == 0 && !unlimited)
6770 {
6771 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6772 &e->where);
6773 goto failure;
6774 }
6775
6776 /* Some checks for the SOURCE tag. */
6777 if (code->expr3)
6778 {
6779 /* Check F03:C631. */
6780 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6781 {
6782 gfc_error ("Type of entity at %L is type incompatible with "
6783 "source-expr at %L", &e->where, &code->expr3->where);
6784 goto failure;
6785 }
6786
6787 /* Check F03:C632 and restriction following Note 6.18. */
6788 if (code->expr3->rank > 0 && !unlimited
6789 && !conformable_arrays (code->expr3, e))
6790 goto failure;
6791
6792 /* Check F03:C633. */
6793 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
6794 {
6795 gfc_error ("The allocate-object at %L and the source-expr at %L "
6796 "shall have the same kind type parameter",
6797 &e->where, &code->expr3->where);
6798 goto failure;
6799 }
6800
6801 /* Check F2008, C642. */
6802 if (code->expr3->ts.type == BT_DERIVED
6803 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
6804 || (code->expr3->ts.u.derived->from_intmod
6805 == INTMOD_ISO_FORTRAN_ENV
6806 && code->expr3->ts.u.derived->intmod_sym_id
6807 == ISOFORTRAN_LOCK_TYPE)))
6808 {
6809 gfc_error ("The source-expr at %L shall neither be of type "
6810 "LOCK_TYPE nor have a LOCK_TYPE component if "
6811 "allocate-object at %L is a coarray",
6812 &code->expr3->where, &e->where);
6813 goto failure;
6814 }
6815 }
6816
6817 /* Check F08:C629. */
6818 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
6819 && !code->expr3)
6820 {
6821 gcc_assert (e->ts.type == BT_CLASS);
6822 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6823 "type-spec or source-expr", sym->name, &e->where);
6824 goto failure;
6825 }
6826
6827 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred)
6828 {
6829 int cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
6830 code->ext.alloc.ts.u.cl->length);
6831 if (cmp == 1 || cmp == -1 || cmp == -3)
6832 {
6833 gfc_error ("Allocating %s at %L with type-spec requires the same "
6834 "character-length parameter as in the declaration",
6835 sym->name, &e->where);
6836 goto failure;
6837 }
6838 }
6839
6840 /* In the variable definition context checks, gfc_expr_attr is used
6841 on the expression. This is fooled by the array specification
6842 present in e, thus we have to eliminate that one temporarily. */
6843 e2 = remove_last_array_ref (e);
6844 t = true;
6845 if (t && pointer)
6846 t = gfc_check_vardef_context (e2, true, true, false,
6847 _("ALLOCATE object"));
6848 if (t)
6849 t = gfc_check_vardef_context (e2, false, true, false,
6850 _("ALLOCATE object"));
6851 gfc_free_expr (e2);
6852 if (!t)
6853 goto failure;
6854
6855 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
6856 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
6857 {
6858 /* For class arrays, the initialization with SOURCE is done
6859 using _copy and trans_call. It is convenient to exploit that
6860 when the allocated type is different from the declared type but
6861 no SOURCE exists by setting expr3. */
6862 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
6863 }
6864 else if (!code->expr3)
6865 {
6866 /* Set up default initializer if needed. */
6867 gfc_typespec ts;
6868 gfc_expr *init_e;
6869
6870 if (code->ext.alloc.ts.type == BT_DERIVED)
6871 ts = code->ext.alloc.ts;
6872 else
6873 ts = e->ts;
6874
6875 if (ts.type == BT_CLASS)
6876 ts = ts.u.derived->components->ts;
6877
6878 if (ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&ts)))
6879 {
6880 gfc_code *init_st = gfc_get_code ();
6881 init_st->loc = code->loc;
6882 init_st->op = EXEC_INIT_ASSIGN;
6883 init_st->expr1 = gfc_expr_to_initialize (e);
6884 init_st->expr2 = init_e;
6885 init_st->next = code->next;
6886 code->next = init_st;
6887 }
6888 }
6889 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
6890 {
6891 /* Default initialization via MOLD (non-polymorphic). */
6892 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
6893 gfc_resolve_expr (rhs);
6894 gfc_free_expr (code->expr3);
6895 code->expr3 = rhs;
6896 }
6897
6898 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
6899 {
6900 /* Make sure the vtab symbol is present when
6901 the module variables are generated. */
6902 gfc_typespec ts = e->ts;
6903 if (code->expr3)
6904 ts = code->expr3->ts;
6905 else if (code->ext.alloc.ts.type == BT_DERIVED)
6906 ts = code->ext.alloc.ts;
6907
6908 gfc_find_derived_vtab (ts.u.derived);
6909
6910 if (dimension)
6911 e = gfc_expr_to_initialize (e);
6912 }
6913 else if (unlimited && !UNLIMITED_POLY (code->expr3))
6914 {
6915 /* Again, make sure the vtab symbol is present when
6916 the module variables are generated. */
6917 gfc_typespec *ts = NULL;
6918 if (code->expr3)
6919 ts = &code->expr3->ts;
6920 else
6921 ts = &code->ext.alloc.ts;
6922
6923 gcc_assert (ts);
6924
6925 if (ts->type == BT_CLASS || ts->type == BT_DERIVED)
6926 gfc_find_derived_vtab (ts->u.derived);
6927 else
6928 gfc_find_intrinsic_vtab (ts);
6929
6930 if (dimension)
6931 e = gfc_expr_to_initialize (e);
6932 }
6933
6934 if (dimension == 0 && codimension == 0)
6935 goto success;
6936
6937 /* Make sure the last reference node is an array specification. */
6938
6939 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
6940 || (dimension && ref2->u.ar.dimen == 0))
6941 {
6942 gfc_error ("Array specification required in ALLOCATE statement "
6943 "at %L", &e->where);
6944 goto failure;
6945 }
6946
6947 /* Make sure that the array section reference makes sense in the
6948 context of an ALLOCATE specification. */
6949
6950 ar = &ref2->u.ar;
6951
6952 if (codimension)
6953 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
6954 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
6955 {
6956 gfc_error ("Coarray specification required in ALLOCATE statement "
6957 "at %L", &e->where);
6958 goto failure;
6959 }
6960
6961 for (i = 0; i < ar->dimen; i++)
6962 {
6963 if (ref2->u.ar.type == AR_ELEMENT)
6964 goto check_symbols;
6965
6966 switch (ar->dimen_type[i])
6967 {
6968 case DIMEN_ELEMENT:
6969 break;
6970
6971 case DIMEN_RANGE:
6972 if (ar->start[i] != NULL
6973 && ar->end[i] != NULL
6974 && ar->stride[i] == NULL)
6975 break;
6976
6977 /* Fall Through... */
6978
6979 case DIMEN_UNKNOWN:
6980 case DIMEN_VECTOR:
6981 case DIMEN_STAR:
6982 case DIMEN_THIS_IMAGE:
6983 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6984 &e->where);
6985 goto failure;
6986 }
6987
6988 check_symbols:
6989 for (a = code->ext.alloc.list; a; a = a->next)
6990 {
6991 sym = a->expr->symtree->n.sym;
6992
6993 /* TODO - check derived type components. */
6994 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
6995 continue;
6996
6997 if ((ar->start[i] != NULL
6998 && gfc_find_sym_in_expr (sym, ar->start[i]))
6999 || (ar->end[i] != NULL
7000 && gfc_find_sym_in_expr (sym, ar->end[i])))
7001 {
7002 gfc_error ("'%s' must not appear in the array specification at "
7003 "%L in the same ALLOCATE statement where it is "
7004 "itself allocated", sym->name, &ar->where);
7005 goto failure;
7006 }
7007 }
7008 }
7009
7010 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
7011 {
7012 if (ar->dimen_type[i] == DIMEN_ELEMENT
7013 || ar->dimen_type[i] == DIMEN_RANGE)
7014 {
7015 if (i == (ar->dimen + ar->codimen - 1))
7016 {
7017 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
7018 "statement at %L", &e->where);
7019 goto failure;
7020 }
7021 continue;
7022 }
7023
7024 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
7025 && ar->stride[i] == NULL)
7026 break;
7027
7028 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7029 &e->where);
7030 goto failure;
7031 }
7032
7033 success:
7034 return true;
7035
7036 failure:
7037 return false;
7038 }
7039
7040 static void
7041 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
7042 {
7043 gfc_expr *stat, *errmsg, *pe, *qe;
7044 gfc_alloc *a, *p, *q;
7045
7046 stat = code->expr1;
7047 errmsg = code->expr2;
7048
7049 /* Check the stat variable. */
7050 if (stat)
7051 {
7052 gfc_check_vardef_context (stat, false, false, false,
7053 _("STAT variable"));
7054
7055 if ((stat->ts.type != BT_INTEGER
7056 && !(stat->ref && (stat->ref->type == REF_ARRAY
7057 || stat->ref->type == REF_COMPONENT)))
7058 || stat->rank > 0)
7059 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7060 "variable", &stat->where);
7061
7062 for (p = code->ext.alloc.list; p; p = p->next)
7063 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
7064 {
7065 gfc_ref *ref1, *ref2;
7066 bool found = true;
7067
7068 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7069 ref1 = ref1->next, ref2 = ref2->next)
7070 {
7071 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7072 continue;
7073 if (ref1->u.c.component->name != ref2->u.c.component->name)
7074 {
7075 found = false;
7076 break;
7077 }
7078 }
7079
7080 if (found)
7081 {
7082 gfc_error ("Stat-variable at %L shall not be %sd within "
7083 "the same %s statement", &stat->where, fcn, fcn);
7084 break;
7085 }
7086 }
7087 }
7088
7089 /* Check the errmsg variable. */
7090 if (errmsg)
7091 {
7092 if (!stat)
7093 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
7094 &errmsg->where);
7095
7096 gfc_check_vardef_context (errmsg, false, false, false,
7097 _("ERRMSG variable"));
7098
7099 if ((errmsg->ts.type != BT_CHARACTER
7100 && !(errmsg->ref
7101 && (errmsg->ref->type == REF_ARRAY
7102 || errmsg->ref->type == REF_COMPONENT)))
7103 || errmsg->rank > 0 )
7104 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7105 "variable", &errmsg->where);
7106
7107 for (p = code->ext.alloc.list; p; p = p->next)
7108 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7109 {
7110 gfc_ref *ref1, *ref2;
7111 bool found = true;
7112
7113 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7114 ref1 = ref1->next, ref2 = ref2->next)
7115 {
7116 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7117 continue;
7118 if (ref1->u.c.component->name != ref2->u.c.component->name)
7119 {
7120 found = false;
7121 break;
7122 }
7123 }
7124
7125 if (found)
7126 {
7127 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7128 "the same %s statement", &errmsg->where, fcn, fcn);
7129 break;
7130 }
7131 }
7132 }
7133
7134 /* Check that an allocate-object appears only once in the statement. */
7135
7136 for (p = code->ext.alloc.list; p; p = p->next)
7137 {
7138 pe = p->expr;
7139 for (q = p->next; q; q = q->next)
7140 {
7141 qe = q->expr;
7142 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
7143 {
7144 /* This is a potential collision. */
7145 gfc_ref *pr = pe->ref;
7146 gfc_ref *qr = qe->ref;
7147
7148 /* Follow the references until
7149 a) They start to differ, in which case there is no error;
7150 you can deallocate a%b and a%c in a single statement
7151 b) Both of them stop, which is an error
7152 c) One of them stops, which is also an error. */
7153 while (1)
7154 {
7155 if (pr == NULL && qr == NULL)
7156 {
7157 gfc_error ("Allocate-object at %L also appears at %L",
7158 &pe->where, &qe->where);
7159 break;
7160 }
7161 else if (pr != NULL && qr == NULL)
7162 {
7163 gfc_error ("Allocate-object at %L is subobject of"
7164 " object at %L", &pe->where, &qe->where);
7165 break;
7166 }
7167 else if (pr == NULL && qr != NULL)
7168 {
7169 gfc_error ("Allocate-object at %L is subobject of"
7170 " object at %L", &qe->where, &pe->where);
7171 break;
7172 }
7173 /* Here, pr != NULL && qr != NULL */
7174 gcc_assert(pr->type == qr->type);
7175 if (pr->type == REF_ARRAY)
7176 {
7177 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7178 which are legal. */
7179 gcc_assert (qr->type == REF_ARRAY);
7180
7181 if (pr->next && qr->next)
7182 {
7183 int i;
7184 gfc_array_ref *par = &(pr->u.ar);
7185 gfc_array_ref *qar = &(qr->u.ar);
7186
7187 for (i=0; i<par->dimen; i++)
7188 {
7189 if ((par->start[i] != NULL
7190 || qar->start[i] != NULL)
7191 && gfc_dep_compare_expr (par->start[i],
7192 qar->start[i]) != 0)
7193 goto break_label;
7194 }
7195 }
7196 }
7197 else
7198 {
7199 if (pr->u.c.component->name != qr->u.c.component->name)
7200 break;
7201 }
7202
7203 pr = pr->next;
7204 qr = qr->next;
7205 }
7206 break_label:
7207 ;
7208 }
7209 }
7210 }
7211
7212 if (strcmp (fcn, "ALLOCATE") == 0)
7213 {
7214 for (a = code->ext.alloc.list; a; a = a->next)
7215 resolve_allocate_expr (a->expr, code);
7216 }
7217 else
7218 {
7219 for (a = code->ext.alloc.list; a; a = a->next)
7220 resolve_deallocate_expr (a->expr);
7221 }
7222 }
7223
7224
7225 /************ SELECT CASE resolution subroutines ************/
7226
7227 /* Callback function for our mergesort variant. Determines interval
7228 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
7229 op1 > op2. Assumes we're not dealing with the default case.
7230 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7231 There are nine situations to check. */
7232
7233 static int
7234 compare_cases (const gfc_case *op1, const gfc_case *op2)
7235 {
7236 int retval;
7237
7238 if (op1->low == NULL) /* op1 = (:L) */
7239 {
7240 /* op2 = (:N), so overlap. */
7241 retval = 0;
7242 /* op2 = (M:) or (M:N), L < M */
7243 if (op2->low != NULL
7244 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7245 retval = -1;
7246 }
7247 else if (op1->high == NULL) /* op1 = (K:) */
7248 {
7249 /* op2 = (M:), so overlap. */
7250 retval = 0;
7251 /* op2 = (:N) or (M:N), K > N */
7252 if (op2->high != NULL
7253 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7254 retval = 1;
7255 }
7256 else /* op1 = (K:L) */
7257 {
7258 if (op2->low == NULL) /* op2 = (:N), K > N */
7259 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7260 ? 1 : 0;
7261 else if (op2->high == NULL) /* op2 = (M:), L < M */
7262 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7263 ? -1 : 0;
7264 else /* op2 = (M:N) */
7265 {
7266 retval = 0;
7267 /* L < M */
7268 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7269 retval = -1;
7270 /* K > N */
7271 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7272 retval = 1;
7273 }
7274 }
7275
7276 return retval;
7277 }
7278
7279
7280 /* Merge-sort a double linked case list, detecting overlap in the
7281 process. LIST is the head of the double linked case list before it
7282 is sorted. Returns the head of the sorted list if we don't see any
7283 overlap, or NULL otherwise. */
7284
7285 static gfc_case *
7286 check_case_overlap (gfc_case *list)
7287 {
7288 gfc_case *p, *q, *e, *tail;
7289 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7290
7291 /* If the passed list was empty, return immediately. */
7292 if (!list)
7293 return NULL;
7294
7295 overlap_seen = 0;
7296 insize = 1;
7297
7298 /* Loop unconditionally. The only exit from this loop is a return
7299 statement, when we've finished sorting the case list. */
7300 for (;;)
7301 {
7302 p = list;
7303 list = NULL;
7304 tail = NULL;
7305
7306 /* Count the number of merges we do in this pass. */
7307 nmerges = 0;
7308
7309 /* Loop while there exists a merge to be done. */
7310 while (p)
7311 {
7312 int i;
7313
7314 /* Count this merge. */
7315 nmerges++;
7316
7317 /* Cut the list in two pieces by stepping INSIZE places
7318 forward in the list, starting from P. */
7319 psize = 0;
7320 q = p;
7321 for (i = 0; i < insize; i++)
7322 {
7323 psize++;
7324 q = q->right;
7325 if (!q)
7326 break;
7327 }
7328 qsize = insize;
7329
7330 /* Now we have two lists. Merge them! */
7331 while (psize > 0 || (qsize > 0 && q != NULL))
7332 {
7333 /* See from which the next case to merge comes from. */
7334 if (psize == 0)
7335 {
7336 /* P is empty so the next case must come from Q. */
7337 e = q;
7338 q = q->right;
7339 qsize--;
7340 }
7341 else if (qsize == 0 || q == NULL)
7342 {
7343 /* Q is empty. */
7344 e = p;
7345 p = p->right;
7346 psize--;
7347 }
7348 else
7349 {
7350 cmp = compare_cases (p, q);
7351 if (cmp < 0)
7352 {
7353 /* The whole case range for P is less than the
7354 one for Q. */
7355 e = p;
7356 p = p->right;
7357 psize--;
7358 }
7359 else if (cmp > 0)
7360 {
7361 /* The whole case range for Q is greater than
7362 the case range for P. */
7363 e = q;
7364 q = q->right;
7365 qsize--;
7366 }
7367 else
7368 {
7369 /* The cases overlap, or they are the same
7370 element in the list. Either way, we must
7371 issue an error and get the next case from P. */
7372 /* FIXME: Sort P and Q by line number. */
7373 gfc_error ("CASE label at %L overlaps with CASE "
7374 "label at %L", &p->where, &q->where);
7375 overlap_seen = 1;
7376 e = p;
7377 p = p->right;
7378 psize--;
7379 }
7380 }
7381
7382 /* Add the next element to the merged list. */
7383 if (tail)
7384 tail->right = e;
7385 else
7386 list = e;
7387 e->left = tail;
7388 tail = e;
7389 }
7390
7391 /* P has now stepped INSIZE places along, and so has Q. So
7392 they're the same. */
7393 p = q;
7394 }
7395 tail->right = NULL;
7396
7397 /* If we have done only one merge or none at all, we've
7398 finished sorting the cases. */
7399 if (nmerges <= 1)
7400 {
7401 if (!overlap_seen)
7402 return list;
7403 else
7404 return NULL;
7405 }
7406
7407 /* Otherwise repeat, merging lists twice the size. */
7408 insize *= 2;
7409 }
7410 }
7411
7412
7413 /* Check to see if an expression is suitable for use in a CASE statement.
7414 Makes sure that all case expressions are scalar constants of the same
7415 type. Return false if anything is wrong. */
7416
7417 static bool
7418 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7419 {
7420 if (e == NULL) return true;
7421
7422 if (e->ts.type != case_expr->ts.type)
7423 {
7424 gfc_error ("Expression in CASE statement at %L must be of type %s",
7425 &e->where, gfc_basic_typename (case_expr->ts.type));
7426 return false;
7427 }
7428
7429 /* C805 (R808) For a given case-construct, each case-value shall be of
7430 the same type as case-expr. For character type, length differences
7431 are allowed, but the kind type parameters shall be the same. */
7432
7433 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7434 {
7435 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7436 &e->where, case_expr->ts.kind);
7437 return false;
7438 }
7439
7440 /* Convert the case value kind to that of case expression kind,
7441 if needed */
7442
7443 if (e->ts.kind != case_expr->ts.kind)
7444 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7445
7446 if (e->rank != 0)
7447 {
7448 gfc_error ("Expression in CASE statement at %L must be scalar",
7449 &e->where);
7450 return false;
7451 }
7452
7453 return true;
7454 }
7455
7456
7457 /* Given a completely parsed select statement, we:
7458
7459 - Validate all expressions and code within the SELECT.
7460 - Make sure that the selection expression is not of the wrong type.
7461 - Make sure that no case ranges overlap.
7462 - Eliminate unreachable cases and unreachable code resulting from
7463 removing case labels.
7464
7465 The standard does allow unreachable cases, e.g. CASE (5:3). But
7466 they are a hassle for code generation, and to prevent that, we just
7467 cut them out here. This is not necessary for overlapping cases
7468 because they are illegal and we never even try to generate code.
7469
7470 We have the additional caveat that a SELECT construct could have
7471 been a computed GOTO in the source code. Fortunately we can fairly
7472 easily work around that here: The case_expr for a "real" SELECT CASE
7473 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7474 we have to do is make sure that the case_expr is a scalar integer
7475 expression. */
7476
7477 static void
7478 resolve_select (gfc_code *code, bool select_type)
7479 {
7480 gfc_code *body;
7481 gfc_expr *case_expr;
7482 gfc_case *cp, *default_case, *tail, *head;
7483 int seen_unreachable;
7484 int seen_logical;
7485 int ncases;
7486 bt type;
7487 bool t;
7488
7489 if (code->expr1 == NULL)
7490 {
7491 /* This was actually a computed GOTO statement. */
7492 case_expr = code->expr2;
7493 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7494 gfc_error ("Selection expression in computed GOTO statement "
7495 "at %L must be a scalar integer expression",
7496 &case_expr->where);
7497
7498 /* Further checking is not necessary because this SELECT was built
7499 by the compiler, so it should always be OK. Just move the
7500 case_expr from expr2 to expr so that we can handle computed
7501 GOTOs as normal SELECTs from here on. */
7502 code->expr1 = code->expr2;
7503 code->expr2 = NULL;
7504 return;
7505 }
7506
7507 case_expr = code->expr1;
7508 type = case_expr->ts.type;
7509
7510 /* F08:C830. */
7511 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7512 {
7513 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7514 &case_expr->where, gfc_typename (&case_expr->ts));
7515
7516 /* Punt. Going on here just produce more garbage error messages. */
7517 return;
7518 }
7519
7520 /* F08:R842. */
7521 if (!select_type && case_expr->rank != 0)
7522 {
7523 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7524 "expression", &case_expr->where);
7525
7526 /* Punt. */
7527 return;
7528 }
7529
7530 /* Raise a warning if an INTEGER case value exceeds the range of
7531 the case-expr. Later, all expressions will be promoted to the
7532 largest kind of all case-labels. */
7533
7534 if (type == BT_INTEGER)
7535 for (body = code->block; body; body = body->block)
7536 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7537 {
7538 if (cp->low
7539 && gfc_check_integer_range (cp->low->value.integer,
7540 case_expr->ts.kind) != ARITH_OK)
7541 gfc_warning ("Expression in CASE statement at %L is "
7542 "not in the range of %s", &cp->low->where,
7543 gfc_typename (&case_expr->ts));
7544
7545 if (cp->high
7546 && cp->low != cp->high
7547 && gfc_check_integer_range (cp->high->value.integer,
7548 case_expr->ts.kind) != ARITH_OK)
7549 gfc_warning ("Expression in CASE statement at %L is "
7550 "not in the range of %s", &cp->high->where,
7551 gfc_typename (&case_expr->ts));
7552 }
7553
7554 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7555 of the SELECT CASE expression and its CASE values. Walk the lists
7556 of case values, and if we find a mismatch, promote case_expr to
7557 the appropriate kind. */
7558
7559 if (type == BT_LOGICAL || type == BT_INTEGER)
7560 {
7561 for (body = code->block; body; body = body->block)
7562 {
7563 /* Walk the case label list. */
7564 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7565 {
7566 /* Intercept the DEFAULT case. It does not have a kind. */
7567 if (cp->low == NULL && cp->high == NULL)
7568 continue;
7569
7570 /* Unreachable case ranges are discarded, so ignore. */
7571 if (cp->low != NULL && cp->high != NULL
7572 && cp->low != cp->high
7573 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7574 continue;
7575
7576 if (cp->low != NULL
7577 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7578 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7579
7580 if (cp->high != NULL
7581 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7582 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7583 }
7584 }
7585 }
7586
7587 /* Assume there is no DEFAULT case. */
7588 default_case = NULL;
7589 head = tail = NULL;
7590 ncases = 0;
7591 seen_logical = 0;
7592
7593 for (body = code->block; body; body = body->block)
7594 {
7595 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7596 t = true;
7597 seen_unreachable = 0;
7598
7599 /* Walk the case label list, making sure that all case labels
7600 are legal. */
7601 for (cp = body->ext.block.case_list; cp; cp = cp->next)
7602 {
7603 /* Count the number of cases in the whole construct. */
7604 ncases++;
7605
7606 /* Intercept the DEFAULT case. */
7607 if (cp->low == NULL && cp->high == NULL)
7608 {
7609 if (default_case != NULL)
7610 {
7611 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7612 "by a second DEFAULT CASE at %L",
7613 &default_case->where, &cp->where);
7614 t = false;
7615 break;
7616 }
7617 else
7618 {
7619 default_case = cp;
7620 continue;
7621 }
7622 }
7623
7624 /* Deal with single value cases and case ranges. Errors are
7625 issued from the validation function. */
7626 if (!validate_case_label_expr (cp->low, case_expr)
7627 || !validate_case_label_expr (cp->high, case_expr))
7628 {
7629 t = false;
7630 break;
7631 }
7632
7633 if (type == BT_LOGICAL
7634 && ((cp->low == NULL || cp->high == NULL)
7635 || cp->low != cp->high))
7636 {
7637 gfc_error ("Logical range in CASE statement at %L is not "
7638 "allowed", &cp->low->where);
7639 t = false;
7640 break;
7641 }
7642
7643 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7644 {
7645 int value;
7646 value = cp->low->value.logical == 0 ? 2 : 1;
7647 if (value & seen_logical)
7648 {
7649 gfc_error ("Constant logical value in CASE statement "
7650 "is repeated at %L",
7651 &cp->low->where);
7652 t = false;
7653 break;
7654 }
7655 seen_logical |= value;
7656 }
7657
7658 if (cp->low != NULL && cp->high != NULL
7659 && cp->low != cp->high
7660 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7661 {
7662 if (gfc_option.warn_surprising)
7663 gfc_warning ("Range specification at %L can never "
7664 "be matched", &cp->where);
7665
7666 cp->unreachable = 1;
7667 seen_unreachable = 1;
7668 }
7669 else
7670 {
7671 /* If the case range can be matched, it can also overlap with
7672 other cases. To make sure it does not, we put it in a
7673 double linked list here. We sort that with a merge sort
7674 later on to detect any overlapping cases. */
7675 if (!head)
7676 {
7677 head = tail = cp;
7678 head->right = head->left = NULL;
7679 }
7680 else
7681 {
7682 tail->right = cp;
7683 tail->right->left = tail;
7684 tail = tail->right;
7685 tail->right = NULL;
7686 }
7687 }
7688 }
7689
7690 /* It there was a failure in the previous case label, give up
7691 for this case label list. Continue with the next block. */
7692 if (!t)
7693 continue;
7694
7695 /* See if any case labels that are unreachable have been seen.
7696 If so, we eliminate them. This is a bit of a kludge because
7697 the case lists for a single case statement (label) is a
7698 single forward linked lists. */
7699 if (seen_unreachable)
7700 {
7701 /* Advance until the first case in the list is reachable. */
7702 while (body->ext.block.case_list != NULL
7703 && body->ext.block.case_list->unreachable)
7704 {
7705 gfc_case *n = body->ext.block.case_list;
7706 body->ext.block.case_list = body->ext.block.case_list->next;
7707 n->next = NULL;
7708 gfc_free_case_list (n);
7709 }
7710
7711 /* Strip all other unreachable cases. */
7712 if (body->ext.block.case_list)
7713 {
7714 for (cp = body->ext.block.case_list; cp->next; cp = cp->next)
7715 {
7716 if (cp->next->unreachable)
7717 {
7718 gfc_case *n = cp->next;
7719 cp->next = cp->next->next;
7720 n->next = NULL;
7721 gfc_free_case_list (n);
7722 }
7723 }
7724 }
7725 }
7726 }
7727
7728 /* See if there were overlapping cases. If the check returns NULL,
7729 there was overlap. In that case we don't do anything. If head
7730 is non-NULL, we prepend the DEFAULT case. The sorted list can
7731 then used during code generation for SELECT CASE constructs with
7732 a case expression of a CHARACTER type. */
7733 if (head)
7734 {
7735 head = check_case_overlap (head);
7736
7737 /* Prepend the default_case if it is there. */
7738 if (head != NULL && default_case)
7739 {
7740 default_case->left = NULL;
7741 default_case->right = head;
7742 head->left = default_case;
7743 }
7744 }
7745
7746 /* Eliminate dead blocks that may be the result if we've seen
7747 unreachable case labels for a block. */
7748 for (body = code; body && body->block; body = body->block)
7749 {
7750 if (body->block->ext.block.case_list == NULL)
7751 {
7752 /* Cut the unreachable block from the code chain. */
7753 gfc_code *c = body->block;
7754 body->block = c->block;
7755
7756 /* Kill the dead block, but not the blocks below it. */
7757 c->block = NULL;
7758 gfc_free_statements (c);
7759 }
7760 }
7761
7762 /* More than two cases is legal but insane for logical selects.
7763 Issue a warning for it. */
7764 if (gfc_option.warn_surprising && type == BT_LOGICAL
7765 && ncases > 2)
7766 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7767 &code->loc);
7768 }
7769
7770
7771 /* Check if a derived type is extensible. */
7772
7773 bool
7774 gfc_type_is_extensible (gfc_symbol *sym)
7775 {
7776 return !(sym->attr.is_bind_c || sym->attr.sequence
7777 || (sym->attr.is_class
7778 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
7779 }
7780
7781
7782 /* Resolve an associate-name: Resolve target and ensure the type-spec is
7783 correct as well as possibly the array-spec. */
7784
7785 static void
7786 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
7787 {
7788 gfc_expr* target;
7789
7790 gcc_assert (sym->assoc);
7791 gcc_assert (sym->attr.flavor == FL_VARIABLE);
7792
7793 /* If this is for SELECT TYPE, the target may not yet be set. In that
7794 case, return. Resolution will be called later manually again when
7795 this is done. */
7796 target = sym->assoc->target;
7797 if (!target)
7798 return;
7799 gcc_assert (!sym->assoc->dangling);
7800
7801 if (resolve_target && !gfc_resolve_expr (target))
7802 return;
7803
7804 /* For variable targets, we get some attributes from the target. */
7805 if (target->expr_type == EXPR_VARIABLE)
7806 {
7807 gfc_symbol* tsym;
7808
7809 gcc_assert (target->symtree);
7810 tsym = target->symtree->n.sym;
7811
7812 sym->attr.asynchronous = tsym->attr.asynchronous;
7813 sym->attr.volatile_ = tsym->attr.volatile_;
7814
7815 sym->attr.target = tsym->attr.target
7816 || gfc_expr_attr (target).pointer;
7817 }
7818
7819 /* Get type if this was not already set. Note that it can be
7820 some other type than the target in case this is a SELECT TYPE
7821 selector! So we must not update when the type is already there. */
7822 if (sym->ts.type == BT_UNKNOWN)
7823 sym->ts = target->ts;
7824 gcc_assert (sym->ts.type != BT_UNKNOWN);
7825
7826 /* See if this is a valid association-to-variable. */
7827 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
7828 && !gfc_has_vector_subscript (target));
7829
7830 /* Finally resolve if this is an array or not. */
7831 if (sym->attr.dimension && target->rank == 0)
7832 {
7833 gfc_error ("Associate-name '%s' at %L is used as array",
7834 sym->name, &sym->declared_at);
7835 sym->attr.dimension = 0;
7836 return;
7837 }
7838
7839 /* We cannot deal with class selectors that need temporaries. */
7840 if (target->ts.type == BT_CLASS
7841 && gfc_ref_needs_temporary_p (target->ref))
7842 {
7843 gfc_error ("CLASS selector at %L needs a temporary which is not "
7844 "yet implemented", &target->where);
7845 return;
7846 }
7847
7848 if (target->ts.type != BT_CLASS && target->rank > 0)
7849 sym->attr.dimension = 1;
7850 else if (target->ts.type == BT_CLASS)
7851 gfc_fix_class_refs (target);
7852
7853 /* The associate-name will have a correct type by now. Make absolutely
7854 sure that it has not picked up a dimension attribute. */
7855 if (sym->ts.type == BT_CLASS)
7856 sym->attr.dimension = 0;
7857
7858 if (sym->attr.dimension)
7859 {
7860 sym->as = gfc_get_array_spec ();
7861 sym->as->rank = target->rank;
7862 sym->as->type = AS_DEFERRED;
7863
7864 /* Target must not be coindexed, thus the associate-variable
7865 has no corank. */
7866 sym->as->corank = 0;
7867 }
7868
7869 /* Mark this as an associate variable. */
7870 sym->attr.associate_var = 1;
7871
7872 /* If the target is a good class object, so is the associate variable. */
7873 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
7874 sym->attr.class_ok = 1;
7875 }
7876
7877
7878 /* Resolve a SELECT TYPE statement. */
7879
7880 static void
7881 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
7882 {
7883 gfc_symbol *selector_type;
7884 gfc_code *body, *new_st, *if_st, *tail;
7885 gfc_code *class_is = NULL, *default_case = NULL;
7886 gfc_case *c;
7887 gfc_symtree *st;
7888 char name[GFC_MAX_SYMBOL_LEN];
7889 gfc_namespace *ns;
7890 int error = 0;
7891 int charlen = 0;
7892
7893 ns = code->ext.block.ns;
7894 gfc_resolve (ns);
7895
7896 /* Check for F03:C813. */
7897 if (code->expr1->ts.type != BT_CLASS
7898 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
7899 {
7900 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7901 "at %L", &code->loc);
7902 return;
7903 }
7904
7905 if (!code->expr1->symtree->n.sym->attr.class_ok)
7906 return;
7907
7908 if (code->expr2)
7909 {
7910 if (code->expr1->symtree->n.sym->attr.untyped)
7911 code->expr1->symtree->n.sym->ts = code->expr2->ts;
7912 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
7913
7914 /* F2008: C803 The selector expression must not be coindexed. */
7915 if (gfc_is_coindexed (code->expr2))
7916 {
7917 gfc_error ("Selector at %L must not be coindexed",
7918 &code->expr2->where);
7919 return;
7920 }
7921
7922 }
7923 else
7924 {
7925 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
7926
7927 if (gfc_is_coindexed (code->expr1))
7928 {
7929 gfc_error ("Selector at %L must not be coindexed",
7930 &code->expr1->where);
7931 return;
7932 }
7933 }
7934
7935 /* Loop over TYPE IS / CLASS IS cases. */
7936 for (body = code->block; body; body = body->block)
7937 {
7938 c = body->ext.block.case_list;
7939
7940 /* Check F03:C815. */
7941 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7942 && !selector_type->attr.unlimited_polymorphic
7943 && !gfc_type_is_extensible (c->ts.u.derived))
7944 {
7945 gfc_error ("Derived type '%s' at %L must be extensible",
7946 c->ts.u.derived->name, &c->where);
7947 error++;
7948 continue;
7949 }
7950
7951 /* Check F03:C816. */
7952 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
7953 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
7954 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
7955 {
7956 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7957 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7958 c->ts.u.derived->name, &c->where, selector_type->name);
7959 else
7960 gfc_error ("Unexpected intrinsic type '%s' at %L",
7961 gfc_basic_typename (c->ts.type), &c->where);
7962 error++;
7963 continue;
7964 }
7965
7966 /* Check F03:C814. */
7967 if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length != NULL)
7968 {
7969 gfc_error ("The type-spec at %L shall specify that each length "
7970 "type parameter is assumed", &c->where);
7971 error++;
7972 continue;
7973 }
7974
7975 /* Intercept the DEFAULT case. */
7976 if (c->ts.type == BT_UNKNOWN)
7977 {
7978 /* Check F03:C818. */
7979 if (default_case)
7980 {
7981 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7982 "by a second DEFAULT CASE at %L",
7983 &default_case->ext.block.case_list->where, &c->where);
7984 error++;
7985 continue;
7986 }
7987
7988 default_case = body;
7989 }
7990 }
7991
7992 if (error > 0)
7993 return;
7994
7995 /* Transform SELECT TYPE statement to BLOCK and associate selector to
7996 target if present. If there are any EXIT statements referring to the
7997 SELECT TYPE construct, this is no problem because the gfc_code
7998 reference stays the same and EXIT is equally possible from the BLOCK
7999 it is changed to. */
8000 code->op = EXEC_BLOCK;
8001 if (code->expr2)
8002 {
8003 gfc_association_list* assoc;
8004
8005 assoc = gfc_get_association_list ();
8006 assoc->st = code->expr1->symtree;
8007 assoc->target = gfc_copy_expr (code->expr2);
8008 assoc->target->where = code->expr2->where;
8009 /* assoc->variable will be set by resolve_assoc_var. */
8010
8011 code->ext.block.assoc = assoc;
8012 code->expr1->symtree->n.sym->assoc = assoc;
8013
8014 resolve_assoc_var (code->expr1->symtree->n.sym, false);
8015 }
8016 else
8017 code->ext.block.assoc = NULL;
8018
8019 /* Add EXEC_SELECT to switch on type. */
8020 new_st = gfc_get_code ();
8021 new_st->op = code->op;
8022 new_st->expr1 = code->expr1;
8023 new_st->expr2 = code->expr2;
8024 new_st->block = code->block;
8025 code->expr1 = code->expr2 = NULL;
8026 code->block = NULL;
8027 if (!ns->code)
8028 ns->code = new_st;
8029 else
8030 ns->code->next = new_st;
8031 code = new_st;
8032 code->op = EXEC_SELECT;
8033
8034 gfc_add_vptr_component (code->expr1);
8035 gfc_add_hash_component (code->expr1);
8036
8037 /* Loop over TYPE IS / CLASS IS cases. */
8038 for (body = code->block; body; body = body->block)
8039 {
8040 c = body->ext.block.case_list;
8041
8042 if (c->ts.type == BT_DERIVED)
8043 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
8044 c->ts.u.derived->hash_value);
8045 else if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8046 {
8047 gfc_symbol *ivtab;
8048 gfc_expr *e;
8049
8050 ivtab = gfc_find_intrinsic_vtab (&c->ts);
8051 gcc_assert (ivtab && CLASS_DATA (ivtab)->initializer);
8052 e = CLASS_DATA (ivtab)->initializer;
8053 c->low = c->high = gfc_copy_expr (e);
8054 }
8055
8056 else if (c->ts.type == BT_UNKNOWN)
8057 continue;
8058
8059 /* Associate temporary to selector. This should only be done
8060 when this case is actually true, so build a new ASSOCIATE
8061 that does precisely this here (instead of using the
8062 'global' one). */
8063
8064 if (c->ts.type == BT_CLASS)
8065 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
8066 else if (c->ts.type == BT_DERIVED)
8067 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
8068 else if (c->ts.type == BT_CHARACTER)
8069 {
8070 if (c->ts.u.cl && c->ts.u.cl->length
8071 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8072 charlen = mpz_get_si (c->ts.u.cl->length->value.integer);
8073 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (c->ts.type),
8074 charlen, c->ts.kind);
8075 }
8076 else
8077 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
8078 c->ts.kind);
8079
8080 st = gfc_find_symtree (ns->sym_root, name);
8081 gcc_assert (st->n.sym->assoc);
8082 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
8083 st->n.sym->assoc->target->where = code->expr1->where;
8084 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8085 gfc_add_data_component (st->n.sym->assoc->target);
8086
8087 new_st = gfc_get_code ();
8088 new_st->op = EXEC_BLOCK;
8089 new_st->ext.block.ns = gfc_build_block_ns (ns);
8090 new_st->ext.block.ns->code = body->next;
8091 body->next = new_st;
8092
8093 /* Chain in the new list only if it is marked as dangling. Otherwise
8094 there is a CASE label overlap and this is already used. Just ignore,
8095 the error is diagnosed elsewhere. */
8096 if (st->n.sym->assoc->dangling)
8097 {
8098 new_st->ext.block.assoc = st->n.sym->assoc;
8099 st->n.sym->assoc->dangling = 0;
8100 }
8101
8102 resolve_assoc_var (st->n.sym, false);
8103 }
8104
8105 /* Take out CLASS IS cases for separate treatment. */
8106 body = code;
8107 while (body && body->block)
8108 {
8109 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
8110 {
8111 /* Add to class_is list. */
8112 if (class_is == NULL)
8113 {
8114 class_is = body->block;
8115 tail = class_is;
8116 }
8117 else
8118 {
8119 for (tail = class_is; tail->block; tail = tail->block) ;
8120 tail->block = body->block;
8121 tail = tail->block;
8122 }
8123 /* Remove from EXEC_SELECT list. */
8124 body->block = body->block->block;
8125 tail->block = NULL;
8126 }
8127 else
8128 body = body->block;
8129 }
8130
8131 if (class_is)
8132 {
8133 gfc_symbol *vtab;
8134
8135 if (!default_case)
8136 {
8137 /* Add a default case to hold the CLASS IS cases. */
8138 for (tail = code; tail->block; tail = tail->block) ;
8139 tail->block = gfc_get_code ();
8140 tail = tail->block;
8141 tail->op = EXEC_SELECT_TYPE;
8142 tail->ext.block.case_list = gfc_get_case ();
8143 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
8144 tail->next = NULL;
8145 default_case = tail;
8146 }
8147
8148 /* More than one CLASS IS block? */
8149 if (class_is->block)
8150 {
8151 gfc_code **c1,*c2;
8152 bool swapped;
8153 /* Sort CLASS IS blocks by extension level. */
8154 do
8155 {
8156 swapped = false;
8157 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8158 {
8159 c2 = (*c1)->block;
8160 /* F03:C817 (check for doubles). */
8161 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8162 == c2->ext.block.case_list->ts.u.derived->hash_value)
8163 {
8164 gfc_error ("Double CLASS IS block in SELECT TYPE "
8165 "statement at %L",
8166 &c2->ext.block.case_list->where);
8167 return;
8168 }
8169 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8170 < c2->ext.block.case_list->ts.u.derived->attr.extension)
8171 {
8172 /* Swap. */
8173 (*c1)->block = c2->block;
8174 c2->block = *c1;
8175 *c1 = c2;
8176 swapped = true;
8177 }
8178 }
8179 }
8180 while (swapped);
8181 }
8182
8183 /* Generate IF chain. */
8184 if_st = gfc_get_code ();
8185 if_st->op = EXEC_IF;
8186 new_st = if_st;
8187 for (body = class_is; body; body = body->block)
8188 {
8189 new_st->block = gfc_get_code ();
8190 new_st = new_st->block;
8191 new_st->op = EXEC_IF;
8192 /* Set up IF condition: Call _gfortran_is_extension_of. */
8193 new_st->expr1 = gfc_get_expr ();
8194 new_st->expr1->expr_type = EXPR_FUNCTION;
8195 new_st->expr1->ts.type = BT_LOGICAL;
8196 new_st->expr1->ts.kind = 4;
8197 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8198 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8199 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8200 /* Set up arguments. */
8201 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8202 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
8203 new_st->expr1->value.function.actual->expr->where = code->loc;
8204 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
8205 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
8206 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8207 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8208 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8209 new_st->next = body->next;
8210 }
8211 if (default_case->next)
8212 {
8213 new_st->block = gfc_get_code ();
8214 new_st = new_st->block;
8215 new_st->op = EXEC_IF;
8216 new_st->next = default_case->next;
8217 }
8218
8219 /* Replace CLASS DEFAULT code by the IF chain. */
8220 default_case->next = if_st;
8221 }
8222
8223 /* Resolve the internal code. This can not be done earlier because
8224 it requires that the sym->assoc of selectors is set already. */
8225 gfc_current_ns = ns;
8226 gfc_resolve_blocks (code->block, gfc_current_ns);
8227 gfc_current_ns = old_ns;
8228
8229 resolve_select (code, true);
8230 }
8231
8232
8233 /* Resolve a transfer statement. This is making sure that:
8234 -- a derived type being transferred has only non-pointer components
8235 -- a derived type being transferred doesn't have private components, unless
8236 it's being transferred from the module where the type was defined
8237 -- we're not trying to transfer a whole assumed size array. */
8238
8239 static void
8240 resolve_transfer (gfc_code *code)
8241 {
8242 gfc_typespec *ts;
8243 gfc_symbol *sym;
8244 gfc_ref *ref;
8245 gfc_expr *exp;
8246
8247 exp = code->expr1;
8248
8249 while (exp != NULL && exp->expr_type == EXPR_OP
8250 && exp->value.op.op == INTRINSIC_PARENTHESES)
8251 exp = exp->value.op.op1;
8252
8253 if (exp && exp->expr_type == EXPR_NULL && exp->ts.type == BT_UNKNOWN)
8254 {
8255 gfc_error ("NULL intrinsic at %L in data transfer statement requires "
8256 "MOLD=", &exp->where);
8257 return;
8258 }
8259
8260 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8261 && exp->expr_type != EXPR_FUNCTION))
8262 return;
8263
8264 /* If we are reading, the variable will be changed. Note that
8265 code->ext.dt may be NULL if the TRANSFER is related to
8266 an INQUIRE statement -- but in this case, we are not reading, either. */
8267 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
8268 && !gfc_check_vardef_context (exp, false, false, false,
8269 _("item in READ")))
8270 return;
8271
8272 sym = exp->symtree->n.sym;
8273 ts = &sym->ts;
8274
8275 /* Go to actual component transferred. */
8276 for (ref = exp->ref; ref; ref = ref->next)
8277 if (ref->type == REF_COMPONENT)
8278 ts = &ref->u.c.component->ts;
8279
8280 if (ts->type == BT_CLASS)
8281 {
8282 /* FIXME: Test for defined input/output. */
8283 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8284 "it is processed by a defined input/output procedure",
8285 &code->loc);
8286 return;
8287 }
8288
8289 if (ts->type == BT_DERIVED)
8290 {
8291 /* Check that transferred derived type doesn't contain POINTER
8292 components. */
8293 if (ts->u.derived->attr.pointer_comp)
8294 {
8295 gfc_error ("Data transfer element at %L cannot have POINTER "
8296 "components unless it is processed by a defined "
8297 "input/output procedure", &code->loc);
8298 return;
8299 }
8300
8301 /* F08:C935. */
8302 if (ts->u.derived->attr.proc_pointer_comp)
8303 {
8304 gfc_error ("Data transfer element at %L cannot have "
8305 "procedure pointer components", &code->loc);
8306 return;
8307 }
8308
8309 if (ts->u.derived->attr.alloc_comp)
8310 {
8311 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8312 "components unless it is processed by a defined "
8313 "input/output procedure", &code->loc);
8314 return;
8315 }
8316
8317 /* C_PTR and C_FUNPTR have private components which means they can not
8318 be printed. However, if -std=gnu and not -pedantic, allow
8319 the component to be printed to help debugging. */
8320 if (ts->u.derived->ts.f90_type == BT_VOID)
8321 {
8322 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
8323 "cannot have PRIVATE components", &code->loc))
8324 return;
8325 }
8326 else if (derived_inaccessible (ts->u.derived))
8327 {
8328 gfc_error ("Data transfer element at %L cannot have "
8329 "PRIVATE components",&code->loc);
8330 return;
8331 }
8332 }
8333
8334 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
8335 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8336 {
8337 gfc_error ("Data transfer element at %L cannot be a full reference to "
8338 "an assumed-size array", &code->loc);
8339 return;
8340 }
8341 }
8342
8343
8344 /*********** Toplevel code resolution subroutines ***********/
8345
8346 /* Find the set of labels that are reachable from this block. We also
8347 record the last statement in each block. */
8348
8349 static void
8350 find_reachable_labels (gfc_code *block)
8351 {
8352 gfc_code *c;
8353
8354 if (!block)
8355 return;
8356
8357 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8358
8359 /* Collect labels in this block. We don't keep those corresponding
8360 to END {IF|SELECT}, these are checked in resolve_branch by going
8361 up through the code_stack. */
8362 for (c = block; c; c = c->next)
8363 {
8364 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
8365 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8366 }
8367
8368 /* Merge with labels from parent block. */
8369 if (cs_base->prev)
8370 {
8371 gcc_assert (cs_base->prev->reachable_labels);
8372 bitmap_ior_into (cs_base->reachable_labels,
8373 cs_base->prev->reachable_labels);
8374 }
8375 }
8376
8377
8378 static void
8379 resolve_lock_unlock (gfc_code *code)
8380 {
8381 if (code->expr1->ts.type != BT_DERIVED
8382 || code->expr1->expr_type != EXPR_VARIABLE
8383 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8384 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8385 || code->expr1->rank != 0
8386 || (!gfc_is_coarray (code->expr1) && !gfc_is_coindexed (code->expr1)))
8387 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8388 &code->expr1->where);
8389
8390 /* Check STAT. */
8391 if (code->expr2
8392 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8393 || code->expr2->expr_type != EXPR_VARIABLE))
8394 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8395 &code->expr2->where);
8396
8397 if (code->expr2
8398 && !gfc_check_vardef_context (code->expr2, false, false, false,
8399 _("STAT variable")))
8400 return;
8401
8402 /* Check ERRMSG. */
8403 if (code->expr3
8404 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8405 || code->expr3->expr_type != EXPR_VARIABLE))
8406 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8407 &code->expr3->where);
8408
8409 if (code->expr3
8410 && !gfc_check_vardef_context (code->expr3, false, false, false,
8411 _("ERRMSG variable")))
8412 return;
8413
8414 /* Check ACQUIRED_LOCK. */
8415 if (code->expr4
8416 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8417 || code->expr4->expr_type != EXPR_VARIABLE))
8418 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8419 "variable", &code->expr4->where);
8420
8421 if (code->expr4
8422 && !gfc_check_vardef_context (code->expr4, false, false, false,
8423 _("ACQUIRED_LOCK variable")))
8424 return;
8425 }
8426
8427
8428 static void
8429 resolve_sync (gfc_code *code)
8430 {
8431 /* Check imageset. The * case matches expr1 == NULL. */
8432 if (code->expr1)
8433 {
8434 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8435 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8436 "INTEGER expression", &code->expr1->where);
8437 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8438 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8439 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8440 &code->expr1->where);
8441 else if (code->expr1->expr_type == EXPR_ARRAY
8442 && gfc_simplify_expr (code->expr1, 0))
8443 {
8444 gfc_constructor *cons;
8445 cons = gfc_constructor_first (code->expr1->value.constructor);
8446 for (; cons; cons = gfc_constructor_next (cons))
8447 if (cons->expr->expr_type == EXPR_CONSTANT
8448 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8449 gfc_error ("Imageset argument at %L must between 1 and "
8450 "num_images()", &cons->expr->where);
8451 }
8452 }
8453
8454 /* Check STAT. */
8455 if (code->expr2
8456 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8457 || code->expr2->expr_type != EXPR_VARIABLE))
8458 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8459 &code->expr2->where);
8460
8461 /* Check ERRMSG. */
8462 if (code->expr3
8463 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8464 || code->expr3->expr_type != EXPR_VARIABLE))
8465 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8466 &code->expr3->where);
8467 }
8468
8469
8470 /* Given a branch to a label, see if the branch is conforming.
8471 The code node describes where the branch is located. */
8472
8473 static void
8474 resolve_branch (gfc_st_label *label, gfc_code *code)
8475 {
8476 code_stack *stack;
8477
8478 if (label == NULL)
8479 return;
8480
8481 /* Step one: is this a valid branching target? */
8482
8483 if (label->defined == ST_LABEL_UNKNOWN)
8484 {
8485 gfc_error ("Label %d referenced at %L is never defined", label->value,
8486 &label->where);
8487 return;
8488 }
8489
8490 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
8491 {
8492 gfc_error ("Statement at %L is not a valid branch target statement "
8493 "for the branch statement at %L", &label->where, &code->loc);
8494 return;
8495 }
8496
8497 /* Step two: make sure this branch is not a branch to itself ;-) */
8498
8499 if (code->here == label)
8500 {
8501 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
8502 return;
8503 }
8504
8505 /* Step three: See if the label is in the same block as the
8506 branching statement. The hard work has been done by setting up
8507 the bitmap reachable_labels. */
8508
8509 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
8510 {
8511 /* Check now whether there is a CRITICAL construct; if so, check
8512 whether the label is still visible outside of the CRITICAL block,
8513 which is invalid. */
8514 for (stack = cs_base; stack; stack = stack->prev)
8515 {
8516 if (stack->current->op == EXEC_CRITICAL
8517 && bitmap_bit_p (stack->reachable_labels, label->value))
8518 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
8519 "label at %L", &code->loc, &label->where);
8520 else if (stack->current->op == EXEC_DO_CONCURRENT
8521 && bitmap_bit_p (stack->reachable_labels, label->value))
8522 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
8523 "for label at %L", &code->loc, &label->where);
8524 }
8525
8526 return;
8527 }
8528
8529 /* Step four: If we haven't found the label in the bitmap, it may
8530 still be the label of the END of the enclosing block, in which
8531 case we find it by going up the code_stack. */
8532
8533 for (stack = cs_base; stack; stack = stack->prev)
8534 {
8535 if (stack->current->next && stack->current->next->here == label)
8536 break;
8537 if (stack->current->op == EXEC_CRITICAL)
8538 {
8539 /* Note: A label at END CRITICAL does not leave the CRITICAL
8540 construct as END CRITICAL is still part of it. */
8541 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8542 " at %L", &code->loc, &label->where);
8543 return;
8544 }
8545 else if (stack->current->op == EXEC_DO_CONCURRENT)
8546 {
8547 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
8548 "label at %L", &code->loc, &label->where);
8549 return;
8550 }
8551 }
8552
8553 if (stack)
8554 {
8555 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
8556 return;
8557 }
8558
8559 /* The label is not in an enclosing block, so illegal. This was
8560 allowed in Fortran 66, so we allow it as extension. No
8561 further checks are necessary in this case. */
8562 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
8563 "as the GOTO statement at %L", &label->where,
8564 &code->loc);
8565 return;
8566 }
8567
8568
8569 /* Check whether EXPR1 has the same shape as EXPR2. */
8570
8571 static bool
8572 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
8573 {
8574 mpz_t shape[GFC_MAX_DIMENSIONS];
8575 mpz_t shape2[GFC_MAX_DIMENSIONS];
8576 bool result = false;
8577 int i;
8578
8579 /* Compare the rank. */
8580 if (expr1->rank != expr2->rank)
8581 return result;
8582
8583 /* Compare the size of each dimension. */
8584 for (i=0; i<expr1->rank; i++)
8585 {
8586 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
8587 goto ignore;
8588
8589 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
8590 goto ignore;
8591
8592 if (mpz_cmp (shape[i], shape2[i]))
8593 goto over;
8594 }
8595
8596 /* When either of the two expression is an assumed size array, we
8597 ignore the comparison of dimension sizes. */
8598 ignore:
8599 result = true;
8600
8601 over:
8602 gfc_clear_shape (shape, i);
8603 gfc_clear_shape (shape2, i);
8604 return result;
8605 }
8606
8607
8608 /* Check whether a WHERE assignment target or a WHERE mask expression
8609 has the same shape as the outmost WHERE mask expression. */
8610
8611 static void
8612 resolve_where (gfc_code *code, gfc_expr *mask)
8613 {
8614 gfc_code *cblock;
8615 gfc_code *cnext;
8616 gfc_expr *e = NULL;
8617
8618 cblock = code->block;
8619
8620 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8621 In case of nested WHERE, only the outmost one is stored. */
8622 if (mask == NULL) /* outmost WHERE */
8623 e = cblock->expr1;
8624 else /* inner WHERE */
8625 e = mask;
8626
8627 while (cblock)
8628 {
8629 if (cblock->expr1)
8630 {
8631 /* Check if the mask-expr has a consistent shape with the
8632 outmost WHERE mask-expr. */
8633 if (!resolve_where_shape (cblock->expr1, e))
8634 gfc_error ("WHERE mask at %L has inconsistent shape",
8635 &cblock->expr1->where);
8636 }
8637
8638 /* the assignment statement of a WHERE statement, or the first
8639 statement in where-body-construct of a WHERE construct */
8640 cnext = cblock->next;
8641 while (cnext)
8642 {
8643 switch (cnext->op)
8644 {
8645 /* WHERE assignment statement */
8646 case EXEC_ASSIGN:
8647
8648 /* Check shape consistent for WHERE assignment target. */
8649 if (e && !resolve_where_shape (cnext->expr1, e))
8650 gfc_error ("WHERE assignment target at %L has "
8651 "inconsistent shape", &cnext->expr1->where);
8652 break;
8653
8654
8655 case EXEC_ASSIGN_CALL:
8656 resolve_call (cnext);
8657 if (!cnext->resolved_sym->attr.elemental)
8658 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8659 &cnext->ext.actual->expr->where);
8660 break;
8661
8662 /* WHERE or WHERE construct is part of a where-body-construct */
8663 case EXEC_WHERE:
8664 resolve_where (cnext, e);
8665 break;
8666
8667 default:
8668 gfc_error ("Unsupported statement inside WHERE at %L",
8669 &cnext->loc);
8670 }
8671 /* the next statement within the same where-body-construct */
8672 cnext = cnext->next;
8673 }
8674 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8675 cblock = cblock->block;
8676 }
8677 }
8678
8679
8680 /* Resolve assignment in FORALL construct.
8681 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8682 FORALL index variables. */
8683
8684 static void
8685 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
8686 {
8687 int n;
8688
8689 for (n = 0; n < nvar; n++)
8690 {
8691 gfc_symbol *forall_index;
8692
8693 forall_index = var_expr[n]->symtree->n.sym;
8694
8695 /* Check whether the assignment target is one of the FORALL index
8696 variable. */
8697 if ((code->expr1->expr_type == EXPR_VARIABLE)
8698 && (code->expr1->symtree->n.sym == forall_index))
8699 gfc_error ("Assignment to a FORALL index variable at %L",
8700 &code->expr1->where);
8701 else
8702 {
8703 /* If one of the FORALL index variables doesn't appear in the
8704 assignment variable, then there could be a many-to-one
8705 assignment. Emit a warning rather than an error because the
8706 mask could be resolving this problem. */
8707 if (!find_forall_index (code->expr1, forall_index, 0))
8708 gfc_warning ("The FORALL with index '%s' is not used on the "
8709 "left side of the assignment at %L and so might "
8710 "cause multiple assignment to this object",
8711 var_expr[n]->symtree->name, &code->expr1->where);
8712 }
8713 }
8714 }
8715
8716
8717 /* Resolve WHERE statement in FORALL construct. */
8718
8719 static void
8720 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
8721 gfc_expr **var_expr)
8722 {
8723 gfc_code *cblock;
8724 gfc_code *cnext;
8725
8726 cblock = code->block;
8727 while (cblock)
8728 {
8729 /* the assignment statement of a WHERE statement, or the first
8730 statement in where-body-construct of a WHERE construct */
8731 cnext = cblock->next;
8732 while (cnext)
8733 {
8734 switch (cnext->op)
8735 {
8736 /* WHERE assignment statement */
8737 case EXEC_ASSIGN:
8738 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
8739 break;
8740
8741 /* WHERE operator assignment statement */
8742 case EXEC_ASSIGN_CALL:
8743 resolve_call (cnext);
8744 if (!cnext->resolved_sym->attr.elemental)
8745 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8746 &cnext->ext.actual->expr->where);
8747 break;
8748
8749 /* WHERE or WHERE construct is part of a where-body-construct */
8750 case EXEC_WHERE:
8751 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
8752 break;
8753
8754 default:
8755 gfc_error ("Unsupported statement inside WHERE at %L",
8756 &cnext->loc);
8757 }
8758 /* the next statement within the same where-body-construct */
8759 cnext = cnext->next;
8760 }
8761 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8762 cblock = cblock->block;
8763 }
8764 }
8765
8766
8767 /* Traverse the FORALL body to check whether the following errors exist:
8768 1. For assignment, check if a many-to-one assignment happens.
8769 2. For WHERE statement, check the WHERE body to see if there is any
8770 many-to-one assignment. */
8771
8772 static void
8773 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
8774 {
8775 gfc_code *c;
8776
8777 c = code->block->next;
8778 while (c)
8779 {
8780 switch (c->op)
8781 {
8782 case EXEC_ASSIGN:
8783 case EXEC_POINTER_ASSIGN:
8784 gfc_resolve_assign_in_forall (c, nvar, var_expr);
8785 break;
8786
8787 case EXEC_ASSIGN_CALL:
8788 resolve_call (c);
8789 break;
8790
8791 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8792 there is no need to handle it here. */
8793 case EXEC_FORALL:
8794 break;
8795 case EXEC_WHERE:
8796 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
8797 break;
8798 default:
8799 break;
8800 }
8801 /* The next statement in the FORALL body. */
8802 c = c->next;
8803 }
8804 }
8805
8806
8807 /* Counts the number of iterators needed inside a forall construct, including
8808 nested forall constructs. This is used to allocate the needed memory
8809 in gfc_resolve_forall. */
8810
8811 static int
8812 gfc_count_forall_iterators (gfc_code *code)
8813 {
8814 int max_iters, sub_iters, current_iters;
8815 gfc_forall_iterator *fa;
8816
8817 gcc_assert(code->op == EXEC_FORALL);
8818 max_iters = 0;
8819 current_iters = 0;
8820
8821 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8822 current_iters ++;
8823
8824 code = code->block->next;
8825
8826 while (code)
8827 {
8828 if (code->op == EXEC_FORALL)
8829 {
8830 sub_iters = gfc_count_forall_iterators (code);
8831 if (sub_iters > max_iters)
8832 max_iters = sub_iters;
8833 }
8834 code = code->next;
8835 }
8836
8837 return current_iters + max_iters;
8838 }
8839
8840
8841 /* Given a FORALL construct, first resolve the FORALL iterator, then call
8842 gfc_resolve_forall_body to resolve the FORALL body. */
8843
8844 static void
8845 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
8846 {
8847 static gfc_expr **var_expr;
8848 static int total_var = 0;
8849 static int nvar = 0;
8850 int old_nvar, tmp;
8851 gfc_forall_iterator *fa;
8852 int i;
8853
8854 old_nvar = nvar;
8855
8856 /* Start to resolve a FORALL construct */
8857 if (forall_save == 0)
8858 {
8859 /* Count the total number of FORALL index in the nested FORALL
8860 construct in order to allocate the VAR_EXPR with proper size. */
8861 total_var = gfc_count_forall_iterators (code);
8862
8863 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
8864 var_expr = XCNEWVEC (gfc_expr *, total_var);
8865 }
8866
8867 /* The information about FORALL iterator, including FORALL index start, end
8868 and stride. The FORALL index can not appear in start, end or stride. */
8869 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8870 {
8871 /* Check if any outer FORALL index name is the same as the current
8872 one. */
8873 for (i = 0; i < nvar; i++)
8874 {
8875 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
8876 {
8877 gfc_error ("An outer FORALL construct already has an index "
8878 "with this name %L", &fa->var->where);
8879 }
8880 }
8881
8882 /* Record the current FORALL index. */
8883 var_expr[nvar] = gfc_copy_expr (fa->var);
8884
8885 nvar++;
8886
8887 /* No memory leak. */
8888 gcc_assert (nvar <= total_var);
8889 }
8890
8891 /* Resolve the FORALL body. */
8892 gfc_resolve_forall_body (code, nvar, var_expr);
8893
8894 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8895 gfc_resolve_blocks (code->block, ns);
8896
8897 tmp = nvar;
8898 nvar = old_nvar;
8899 /* Free only the VAR_EXPRs allocated in this frame. */
8900 for (i = nvar; i < tmp; i++)
8901 gfc_free_expr (var_expr[i]);
8902
8903 if (nvar == 0)
8904 {
8905 /* We are in the outermost FORALL construct. */
8906 gcc_assert (forall_save == 0);
8907
8908 /* VAR_EXPR is not needed any more. */
8909 free (var_expr);
8910 total_var = 0;
8911 }
8912 }
8913
8914
8915 /* Resolve a BLOCK construct statement. */
8916
8917 static void
8918 resolve_block_construct (gfc_code* code)
8919 {
8920 /* Resolve the BLOCK's namespace. */
8921 gfc_resolve (code->ext.block.ns);
8922
8923 /* For an ASSOCIATE block, the associations (and their targets) are already
8924 resolved during resolve_symbol. */
8925 }
8926
8927
8928 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
8929 DO code nodes. */
8930
8931 static void resolve_code (gfc_code *, gfc_namespace *);
8932
8933 void
8934 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
8935 {
8936 bool t;
8937
8938 for (; b; b = b->block)
8939 {
8940 t = gfc_resolve_expr (b->expr1);
8941 if (!gfc_resolve_expr (b->expr2))
8942 t = false;
8943
8944 switch (b->op)
8945 {
8946 case EXEC_IF:
8947 if (t && b->expr1 != NULL
8948 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
8949 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8950 &b->expr1->where);
8951 break;
8952
8953 case EXEC_WHERE:
8954 if (t
8955 && b->expr1 != NULL
8956 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
8957 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
8958 &b->expr1->where);
8959 break;
8960
8961 case EXEC_GOTO:
8962 resolve_branch (b->label1, b);
8963 break;
8964
8965 case EXEC_BLOCK:
8966 resolve_block_construct (b);
8967 break;
8968
8969 case EXEC_SELECT:
8970 case EXEC_SELECT_TYPE:
8971 case EXEC_FORALL:
8972 case EXEC_DO:
8973 case EXEC_DO_WHILE:
8974 case EXEC_DO_CONCURRENT:
8975 case EXEC_CRITICAL:
8976 case EXEC_READ:
8977 case EXEC_WRITE:
8978 case EXEC_IOLENGTH:
8979 case EXEC_WAIT:
8980 break;
8981
8982 case EXEC_OMP_ATOMIC:
8983 case EXEC_OMP_CRITICAL:
8984 case EXEC_OMP_DO:
8985 case EXEC_OMP_MASTER:
8986 case EXEC_OMP_ORDERED:
8987 case EXEC_OMP_PARALLEL:
8988 case EXEC_OMP_PARALLEL_DO:
8989 case EXEC_OMP_PARALLEL_SECTIONS:
8990 case EXEC_OMP_PARALLEL_WORKSHARE:
8991 case EXEC_OMP_SECTIONS:
8992 case EXEC_OMP_SINGLE:
8993 case EXEC_OMP_TASK:
8994 case EXEC_OMP_TASKWAIT:
8995 case EXEC_OMP_TASKYIELD:
8996 case EXEC_OMP_WORKSHARE:
8997 break;
8998
8999 default:
9000 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
9001 }
9002
9003 resolve_code (b->next, ns);
9004 }
9005 }
9006
9007
9008 /* Does everything to resolve an ordinary assignment. Returns true
9009 if this is an interface assignment. */
9010 static bool
9011 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
9012 {
9013 bool rval = false;
9014 gfc_expr *lhs;
9015 gfc_expr *rhs;
9016 int llen = 0;
9017 int rlen = 0;
9018 int n;
9019 gfc_ref *ref;
9020
9021 if (gfc_extend_assign (code, ns))
9022 {
9023 gfc_expr** rhsptr;
9024
9025 if (code->op == EXEC_ASSIGN_CALL)
9026 {
9027 lhs = code->ext.actual->expr;
9028 rhsptr = &code->ext.actual->next->expr;
9029 }
9030 else
9031 {
9032 gfc_actual_arglist* args;
9033 gfc_typebound_proc* tbp;
9034
9035 gcc_assert (code->op == EXEC_COMPCALL);
9036
9037 args = code->expr1->value.compcall.actual;
9038 lhs = args->expr;
9039 rhsptr = &args->next->expr;
9040
9041 tbp = code->expr1->value.compcall.tbp;
9042 gcc_assert (!tbp->is_generic);
9043 }
9044
9045 /* Make a temporary rhs when there is a default initializer
9046 and rhs is the same symbol as the lhs. */
9047 if ((*rhsptr)->expr_type == EXPR_VARIABLE
9048 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
9049 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
9050 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
9051 *rhsptr = gfc_get_parentheses (*rhsptr);
9052
9053 return true;
9054 }
9055
9056 lhs = code->expr1;
9057 rhs = code->expr2;
9058
9059 if (rhs->is_boz
9060 && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
9061 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
9062 &code->loc))
9063 return false;
9064
9065 /* Handle the case of a BOZ literal on the RHS. */
9066 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
9067 {
9068 int rc;
9069 if (gfc_option.warn_surprising)
9070 gfc_warning ("BOZ literal at %L is bitwise transferred "
9071 "non-integer symbol '%s'", &code->loc,
9072 lhs->symtree->n.sym->name);
9073
9074 if (!gfc_convert_boz (rhs, &lhs->ts))
9075 return false;
9076 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
9077 {
9078 if (rc == ARITH_UNDERFLOW)
9079 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
9080 ". This check can be disabled with the option "
9081 "-fno-range-check", &rhs->where);
9082 else if (rc == ARITH_OVERFLOW)
9083 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
9084 ". This check can be disabled with the option "
9085 "-fno-range-check", &rhs->where);
9086 else if (rc == ARITH_NAN)
9087 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
9088 ". This check can be disabled with the option "
9089 "-fno-range-check", &rhs->where);
9090 return false;
9091 }
9092 }
9093
9094 if (lhs->ts.type == BT_CHARACTER
9095 && gfc_option.warn_character_truncation)
9096 {
9097 if (lhs->ts.u.cl != NULL
9098 && lhs->ts.u.cl->length != NULL
9099 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9100 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
9101
9102 if (rhs->expr_type == EXPR_CONSTANT)
9103 rlen = rhs->value.character.length;
9104
9105 else if (rhs->ts.u.cl != NULL
9106 && rhs->ts.u.cl->length != NULL
9107 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9108 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
9109
9110 if (rlen && llen && rlen > llen)
9111 gfc_warning_now ("CHARACTER expression will be truncated "
9112 "in assignment (%d/%d) at %L",
9113 llen, rlen, &code->loc);
9114 }
9115
9116 /* Ensure that a vector index expression for the lvalue is evaluated
9117 to a temporary if the lvalue symbol is referenced in it. */
9118 if (lhs->rank)
9119 {
9120 for (ref = lhs->ref; ref; ref= ref->next)
9121 if (ref->type == REF_ARRAY)
9122 {
9123 for (n = 0; n < ref->u.ar.dimen; n++)
9124 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
9125 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9126 ref->u.ar.start[n]))
9127 ref->u.ar.start[n]
9128 = gfc_get_parentheses (ref->u.ar.start[n]);
9129 }
9130 }
9131
9132 if (gfc_pure (NULL))
9133 {
9134 if (lhs->ts.type == BT_DERIVED
9135 && lhs->expr_type == EXPR_VARIABLE
9136 && lhs->ts.u.derived->attr.pointer_comp
9137 && rhs->expr_type == EXPR_VARIABLE
9138 && (gfc_impure_variable (rhs->symtree->n.sym)
9139 || gfc_is_coindexed (rhs)))
9140 {
9141 /* F2008, C1283. */
9142 if (gfc_is_coindexed (rhs))
9143 gfc_error ("Coindexed expression at %L is assigned to "
9144 "a derived type variable with a POINTER "
9145 "component in a PURE procedure",
9146 &rhs->where);
9147 else
9148 gfc_error ("The impure variable at %L is assigned to "
9149 "a derived type variable with a POINTER "
9150 "component in a PURE procedure (12.6)",
9151 &rhs->where);
9152 return rval;
9153 }
9154
9155 /* Fortran 2008, C1283. */
9156 if (gfc_is_coindexed (lhs))
9157 {
9158 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9159 "procedure", &rhs->where);
9160 return rval;
9161 }
9162 }
9163
9164 if (gfc_implicit_pure (NULL))
9165 {
9166 if (lhs->expr_type == EXPR_VARIABLE
9167 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9168 && lhs->symtree->n.sym->ns != gfc_current_ns)
9169 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9170
9171 if (lhs->ts.type == BT_DERIVED
9172 && lhs->expr_type == EXPR_VARIABLE
9173 && lhs->ts.u.derived->attr.pointer_comp
9174 && rhs->expr_type == EXPR_VARIABLE
9175 && (gfc_impure_variable (rhs->symtree->n.sym)
9176 || gfc_is_coindexed (rhs)))
9177 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9178
9179 /* Fortran 2008, C1283. */
9180 if (gfc_is_coindexed (lhs))
9181 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9182 }
9183
9184 /* F03:7.4.1.2. */
9185 /* FIXME: Valid in Fortran 2008, unless the LHS is both polymorphic
9186 and coindexed; cf. F2008, 7.2.1.2 and PR 43366. */
9187 if (lhs->ts.type == BT_CLASS)
9188 {
9189 gfc_error ("Variable must not be polymorphic in intrinsic assignment at "
9190 "%L - check that there is a matching specific subroutine "
9191 "for '=' operator", &lhs->where);
9192 return false;
9193 }
9194
9195 /* F2008, Section 7.2.1.2. */
9196 if (gfc_is_coindexed (lhs) && gfc_has_ultimate_allocatable (lhs))
9197 {
9198 gfc_error ("Coindexed variable must not be have an allocatable ultimate "
9199 "component in assignment at %L", &lhs->where);
9200 return false;
9201 }
9202
9203 gfc_check_assign (lhs, rhs, 1);
9204 return false;
9205 }
9206
9207
9208 /* Add a component reference onto an expression. */
9209
9210 static void
9211 add_comp_ref (gfc_expr *e, gfc_component *c)
9212 {
9213 gfc_ref **ref;
9214 ref = &(e->ref);
9215 while (*ref)
9216 ref = &((*ref)->next);
9217 *ref = gfc_get_ref ();
9218 (*ref)->type = REF_COMPONENT;
9219 (*ref)->u.c.sym = e->ts.u.derived;
9220 (*ref)->u.c.component = c;
9221 e->ts = c->ts;
9222
9223 /* Add a full array ref, as necessary. */
9224 if (c->as)
9225 {
9226 gfc_add_full_array_ref (e, c->as);
9227 e->rank = c->as->rank;
9228 }
9229 }
9230
9231
9232 /* Build an assignment. Keep the argument 'op' for future use, so that
9233 pointer assignments can be made. */
9234
9235 static gfc_code *
9236 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
9237 gfc_component *comp1, gfc_component *comp2, locus loc)
9238 {
9239 gfc_code *this_code;
9240
9241 this_code = gfc_get_code ();
9242 this_code->op = op;
9243 this_code->next = NULL;
9244 this_code->expr1 = gfc_copy_expr (expr1);
9245 this_code->expr2 = gfc_copy_expr (expr2);
9246 this_code->loc = loc;
9247 if (comp1 && comp2)
9248 {
9249 add_comp_ref (this_code->expr1, comp1);
9250 add_comp_ref (this_code->expr2, comp2);
9251 }
9252
9253 return this_code;
9254 }
9255
9256
9257 /* Makes a temporary variable expression based on the characteristics of
9258 a given variable expression. */
9259
9260 static gfc_expr*
9261 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
9262 {
9263 static int serial = 0;
9264 char name[GFC_MAX_SYMBOL_LEN];
9265 gfc_symtree *tmp;
9266 gfc_array_spec *as;
9267 gfc_array_ref *aref;
9268 gfc_ref *ref;
9269
9270 sprintf (name, GFC_PREFIX("DA%d"), serial++);
9271 gfc_get_sym_tree (name, ns, &tmp, false);
9272 gfc_add_type (tmp->n.sym, &e->ts, NULL);
9273
9274 as = NULL;
9275 ref = NULL;
9276 aref = NULL;
9277
9278 /* This function could be expanded to support other expression type
9279 but this is not needed here. */
9280 gcc_assert (e->expr_type == EXPR_VARIABLE);
9281
9282 /* Obtain the arrayspec for the temporary. */
9283 if (e->rank)
9284 {
9285 aref = gfc_find_array_ref (e);
9286 if (e->expr_type == EXPR_VARIABLE
9287 && e->symtree->n.sym->as == aref->as)
9288 as = aref->as;
9289 else
9290 {
9291 for (ref = e->ref; ref; ref = ref->next)
9292 if (ref->type == REF_COMPONENT
9293 && ref->u.c.component->as == aref->as)
9294 {
9295 as = aref->as;
9296 break;
9297 }
9298 }
9299 }
9300
9301 /* Add the attributes and the arrayspec to the temporary. */
9302 tmp->n.sym->attr = gfc_expr_attr (e);
9303 tmp->n.sym->attr.function = 0;
9304 tmp->n.sym->attr.result = 0;
9305 tmp->n.sym->attr.flavor = FL_VARIABLE;
9306
9307 if (as)
9308 {
9309 tmp->n.sym->as = gfc_copy_array_spec (as);
9310 if (!ref)
9311 ref = e->ref;
9312 if (as->type == AS_DEFERRED)
9313 tmp->n.sym->attr.allocatable = 1;
9314 }
9315 else
9316 tmp->n.sym->attr.dimension = 0;
9317
9318 gfc_set_sym_referenced (tmp->n.sym);
9319 gfc_commit_symbol (tmp->n.sym);
9320 e = gfc_lval_expr_from_sym (tmp->n.sym);
9321
9322 /* Should the lhs be a section, use its array ref for the
9323 temporary expression. */
9324 if (aref && aref->type != AR_FULL)
9325 {
9326 gfc_free_ref_list (e->ref);
9327 e->ref = gfc_copy_ref (ref);
9328 }
9329 return e;
9330 }
9331
9332
9333 /* Add one line of code to the code chain, making sure that 'head' and
9334 'tail' are appropriately updated. */
9335
9336 static void
9337 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
9338 {
9339 gcc_assert (this_code);
9340 if (*head == NULL)
9341 *head = *tail = *this_code;
9342 else
9343 *tail = gfc_append_code (*tail, *this_code);
9344 *this_code = NULL;
9345 }
9346
9347
9348 /* Counts the potential number of part array references that would
9349 result from resolution of typebound defined assignments. */
9350
9351 static int
9352 nonscalar_typebound_assign (gfc_symbol *derived, int depth)
9353 {
9354 gfc_component *c;
9355 int c_depth = 0, t_depth;
9356
9357 for (c= derived->components; c; c = c->next)
9358 {
9359 if ((c->ts.type != BT_DERIVED
9360 || c->attr.pointer
9361 || c->attr.allocatable
9362 || c->attr.proc_pointer_comp
9363 || c->attr.class_pointer
9364 || c->attr.proc_pointer)
9365 && !c->attr.defined_assign_comp)
9366 continue;
9367
9368 if (c->as && c_depth == 0)
9369 c_depth = 1;
9370
9371 if (c->ts.u.derived->attr.defined_assign_comp)
9372 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
9373 c->as ? 1 : 0);
9374 else
9375 t_depth = 0;
9376
9377 c_depth = t_depth > c_depth ? t_depth : c_depth;
9378 }
9379 return depth + c_depth;
9380 }
9381
9382
9383 /* Implement 7.2.1.3 of the F08 standard:
9384 "An intrinsic assignment where the variable is of derived type is
9385 performed as if each component of the variable were assigned from the
9386 corresponding component of expr using pointer assignment (7.2.2) for
9387 each pointer component, defined assignment for each nonpointer
9388 nonallocatable component of a type that has a type-bound defined
9389 assignment consistent with the component, intrinsic assignment for
9390 each other nonpointer nonallocatable component, ..."
9391
9392 The pointer assignments are taken care of by the intrinsic
9393 assignment of the structure itself. This function recursively adds
9394 defined assignments where required. The recursion is accomplished
9395 by calling resolve_code.
9396
9397 When the lhs in a defined assignment has intent INOUT, we need a
9398 temporary for the lhs. In pseudo-code:
9399
9400 ! Only call function lhs once.
9401 if (lhs is not a constant or an variable)
9402 temp_x = expr2
9403 expr2 => temp_x
9404 ! Do the intrinsic assignment
9405 expr1 = expr2
9406 ! Now do the defined assignments
9407 do over components with typebound defined assignment [%cmp]
9408 #if one component's assignment procedure is INOUT
9409 t1 = expr1
9410 #if expr2 non-variable
9411 temp_x = expr2
9412 expr2 => temp_x
9413 # endif
9414 expr1 = expr2
9415 # for each cmp
9416 t1%cmp {defined=} expr2%cmp
9417 expr1%cmp = t1%cmp
9418 #else
9419 expr1 = expr2
9420
9421 # for each cmp
9422 expr1%cmp {defined=} expr2%cmp
9423 #endif
9424 */
9425
9426 /* The temporary assignments have to be put on top of the additional
9427 code to avoid the result being changed by the intrinsic assignment.
9428 */
9429 static int component_assignment_level = 0;
9430 static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
9431
9432 static void
9433 generate_component_assignments (gfc_code **code, gfc_namespace *ns)
9434 {
9435 gfc_component *comp1, *comp2;
9436 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
9437 gfc_expr *t1;
9438 int error_count, depth;
9439
9440 gfc_get_errors (NULL, &error_count);
9441
9442 /* Filter out continuing processing after an error. */
9443 if (error_count
9444 || (*code)->expr1->ts.type != BT_DERIVED
9445 || (*code)->expr2->ts.type != BT_DERIVED)
9446 return;
9447
9448 /* TODO: Handle more than one part array reference in assignments. */
9449 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
9450 (*code)->expr1->rank ? 1 : 0);
9451 if (depth > 1)
9452 {
9453 gfc_warning ("TODO: type-bound defined assignment(s) at %L not "
9454 "done because multiple part array references would "
9455 "occur in intermediate expressions.", &(*code)->loc);
9456 return;
9457 }
9458
9459 component_assignment_level++;
9460
9461 /* Create a temporary so that functions get called only once. */
9462 if ((*code)->expr2->expr_type != EXPR_VARIABLE
9463 && (*code)->expr2->expr_type != EXPR_CONSTANT)
9464 {
9465 gfc_expr *tmp_expr;
9466
9467 /* Assign the rhs to the temporary. */
9468 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
9469 this_code = build_assignment (EXEC_ASSIGN,
9470 tmp_expr, (*code)->expr2,
9471 NULL, NULL, (*code)->loc);
9472 /* Add the code and substitute the rhs expression. */
9473 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
9474 gfc_free_expr ((*code)->expr2);
9475 (*code)->expr2 = tmp_expr;
9476 }
9477
9478 /* Do the intrinsic assignment. This is not needed if the lhs is one
9479 of the temporaries generated here, since the intrinsic assignment
9480 to the final result already does this. */
9481 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
9482 {
9483 this_code = build_assignment (EXEC_ASSIGN,
9484 (*code)->expr1, (*code)->expr2,
9485 NULL, NULL, (*code)->loc);
9486 add_code_to_chain (&this_code, &head, &tail);
9487 }
9488
9489 comp1 = (*code)->expr1->ts.u.derived->components;
9490 comp2 = (*code)->expr2->ts.u.derived->components;
9491
9492 t1 = NULL;
9493 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
9494 {
9495 bool inout = false;
9496
9497 /* The intrinsic assignment does the right thing for pointers
9498 of all kinds and allocatable components. */
9499 if (comp1->ts.type != BT_DERIVED
9500 || comp1->attr.pointer
9501 || comp1->attr.allocatable
9502 || comp1->attr.proc_pointer_comp
9503 || comp1->attr.class_pointer
9504 || comp1->attr.proc_pointer)
9505 continue;
9506
9507 /* Make an assigment for this component. */
9508 this_code = build_assignment (EXEC_ASSIGN,
9509 (*code)->expr1, (*code)->expr2,
9510 comp1, comp2, (*code)->loc);
9511
9512 /* Convert the assignment if there is a defined assignment for
9513 this type. Otherwise, using the call from resolve_code,
9514 recurse into its components. */
9515 resolve_code (this_code, ns);
9516
9517 if (this_code->op == EXEC_ASSIGN_CALL)
9518 {
9519 gfc_formal_arglist *dummy_args;
9520 gfc_symbol *rsym;
9521 /* Check that there is a typebound defined assignment. If not,
9522 then this must be a module defined assignment. We cannot
9523 use the defined_assign_comp attribute here because it must
9524 be this derived type that has the defined assignment and not
9525 a parent type. */
9526 if (!(comp1->ts.u.derived->f2k_derived
9527 && comp1->ts.u.derived->f2k_derived
9528 ->tb_op[INTRINSIC_ASSIGN]))
9529 {
9530 gfc_free_statements (this_code);
9531 this_code = NULL;
9532 continue;
9533 }
9534
9535 /* If the first argument of the subroutine has intent INOUT
9536 a temporary must be generated and used instead. */
9537 rsym = this_code->resolved_sym;
9538 dummy_args = gfc_sym_get_dummy_args (rsym);
9539 if (dummy_args
9540 && dummy_args->sym->attr.intent == INTENT_INOUT)
9541 {
9542 gfc_code *temp_code;
9543 inout = true;
9544
9545 /* Build the temporary required for the assignment and put
9546 it at the head of the generated code. */
9547 if (!t1)
9548 {
9549 t1 = get_temp_from_expr ((*code)->expr1, ns);
9550 temp_code = build_assignment (EXEC_ASSIGN,
9551 t1, (*code)->expr1,
9552 NULL, NULL, (*code)->loc);
9553 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
9554 }
9555
9556 /* Replace the first actual arg with the component of the
9557 temporary. */
9558 gfc_free_expr (this_code->ext.actual->expr);
9559 this_code->ext.actual->expr = gfc_copy_expr (t1);
9560 add_comp_ref (this_code->ext.actual->expr, comp1);
9561 }
9562 }
9563 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
9564 {
9565 /* Don't add intrinsic assignments since they are already
9566 effected by the intrinsic assignment of the structure. */
9567 gfc_free_statements (this_code);
9568 this_code = NULL;
9569 continue;
9570 }
9571
9572 add_code_to_chain (&this_code, &head, &tail);
9573
9574 if (t1 && inout)
9575 {
9576 /* Transfer the value to the final result. */
9577 this_code = build_assignment (EXEC_ASSIGN,
9578 (*code)->expr1, t1,
9579 comp1, comp2, (*code)->loc);
9580 add_code_to_chain (&this_code, &head, &tail);
9581 }
9582 }
9583
9584 /* This is probably not necessary. */
9585 if (this_code)
9586 {
9587 gfc_free_statements (this_code);
9588 this_code = NULL;
9589 }
9590
9591 /* Put the temporary assignments at the top of the generated code. */
9592 if (tmp_head && component_assignment_level == 1)
9593 {
9594 gfc_append_code (tmp_head, head);
9595 head = tmp_head;
9596 tmp_head = tmp_tail = NULL;
9597 }
9598
9599 /* Now attach the remaining code chain to the input code. Step on
9600 to the end of the new code since resolution is complete. */
9601 gcc_assert ((*code)->op == EXEC_ASSIGN);
9602 tail->next = (*code)->next;
9603 /* Overwrite 'code' because this would place the intrinsic assignment
9604 before the temporary for the lhs is created. */
9605 gfc_free_expr ((*code)->expr1);
9606 gfc_free_expr ((*code)->expr2);
9607 **code = *head;
9608 free (head);
9609 *code = tail;
9610
9611 component_assignment_level--;
9612 }
9613
9614
9615 /* Given a block of code, recursively resolve everything pointed to by this
9616 code block. */
9617
9618 static void
9619 resolve_code (gfc_code *code, gfc_namespace *ns)
9620 {
9621 int omp_workshare_save;
9622 int forall_save, do_concurrent_save;
9623 code_stack frame;
9624 bool t;
9625
9626 frame.prev = cs_base;
9627 frame.head = code;
9628 cs_base = &frame;
9629
9630 find_reachable_labels (code);
9631
9632 for (; code; code = code->next)
9633 {
9634 frame.current = code;
9635 forall_save = forall_flag;
9636 do_concurrent_save = do_concurrent_flag;
9637
9638 if (code->op == EXEC_FORALL)
9639 {
9640 forall_flag = 1;
9641 gfc_resolve_forall (code, ns, forall_save);
9642 forall_flag = 2;
9643 }
9644 else if (code->block)
9645 {
9646 omp_workshare_save = -1;
9647 switch (code->op)
9648 {
9649 case EXEC_OMP_PARALLEL_WORKSHARE:
9650 omp_workshare_save = omp_workshare_flag;
9651 omp_workshare_flag = 1;
9652 gfc_resolve_omp_parallel_blocks (code, ns);
9653 break;
9654 case EXEC_OMP_PARALLEL:
9655 case EXEC_OMP_PARALLEL_DO:
9656 case EXEC_OMP_PARALLEL_SECTIONS:
9657 case EXEC_OMP_TASK:
9658 omp_workshare_save = omp_workshare_flag;
9659 omp_workshare_flag = 0;
9660 gfc_resolve_omp_parallel_blocks (code, ns);
9661 break;
9662 case EXEC_OMP_DO:
9663 gfc_resolve_omp_do_blocks (code, ns);
9664 break;
9665 case EXEC_SELECT_TYPE:
9666 /* Blocks are handled in resolve_select_type because we have
9667 to transform the SELECT TYPE into ASSOCIATE first. */
9668 break;
9669 case EXEC_DO_CONCURRENT:
9670 do_concurrent_flag = 1;
9671 gfc_resolve_blocks (code->block, ns);
9672 do_concurrent_flag = 2;
9673 break;
9674 case EXEC_OMP_WORKSHARE:
9675 omp_workshare_save = omp_workshare_flag;
9676 omp_workshare_flag = 1;
9677 /* FALL THROUGH */
9678 default:
9679 gfc_resolve_blocks (code->block, ns);
9680 break;
9681 }
9682
9683 if (omp_workshare_save != -1)
9684 omp_workshare_flag = omp_workshare_save;
9685 }
9686
9687 t = true;
9688 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
9689 t = gfc_resolve_expr (code->expr1);
9690 forall_flag = forall_save;
9691 do_concurrent_flag = do_concurrent_save;
9692
9693 if (!gfc_resolve_expr (code->expr2))
9694 t = false;
9695
9696 if (code->op == EXEC_ALLOCATE
9697 && !gfc_resolve_expr (code->expr3))
9698 t = false;
9699
9700 switch (code->op)
9701 {
9702 case EXEC_NOP:
9703 case EXEC_END_BLOCK:
9704 case EXEC_END_NESTED_BLOCK:
9705 case EXEC_CYCLE:
9706 case EXEC_PAUSE:
9707 case EXEC_STOP:
9708 case EXEC_ERROR_STOP:
9709 case EXEC_EXIT:
9710 case EXEC_CONTINUE:
9711 case EXEC_DT_END:
9712 case EXEC_ASSIGN_CALL:
9713 case EXEC_CRITICAL:
9714 break;
9715
9716 case EXEC_SYNC_ALL:
9717 case EXEC_SYNC_IMAGES:
9718 case EXEC_SYNC_MEMORY:
9719 resolve_sync (code);
9720 break;
9721
9722 case EXEC_LOCK:
9723 case EXEC_UNLOCK:
9724 resolve_lock_unlock (code);
9725 break;
9726
9727 case EXEC_ENTRY:
9728 /* Keep track of which entry we are up to. */
9729 current_entry_id = code->ext.entry->id;
9730 break;
9731
9732 case EXEC_WHERE:
9733 resolve_where (code, NULL);
9734 break;
9735
9736 case EXEC_GOTO:
9737 if (code->expr1 != NULL)
9738 {
9739 if (code->expr1->ts.type != BT_INTEGER)
9740 gfc_error ("ASSIGNED GOTO statement at %L requires an "
9741 "INTEGER variable", &code->expr1->where);
9742 else if (code->expr1->symtree->n.sym->attr.assign != 1)
9743 gfc_error ("Variable '%s' has not been assigned a target "
9744 "label at %L", code->expr1->symtree->n.sym->name,
9745 &code->expr1->where);
9746 }
9747 else
9748 resolve_branch (code->label1, code);
9749 break;
9750
9751 case EXEC_RETURN:
9752 if (code->expr1 != NULL
9753 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
9754 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
9755 "INTEGER return specifier", &code->expr1->where);
9756 break;
9757
9758 case EXEC_INIT_ASSIGN:
9759 case EXEC_END_PROCEDURE:
9760 break;
9761
9762 case EXEC_ASSIGN:
9763 if (!t)
9764 break;
9765
9766 if (!gfc_check_vardef_context (code->expr1, false, false, false,
9767 _("assignment")))
9768 break;
9769
9770 if (resolve_ordinary_assign (code, ns))
9771 {
9772 if (code->op == EXEC_COMPCALL)
9773 goto compcall;
9774 else
9775 goto call;
9776 }
9777
9778 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
9779 if (code->expr1->ts.type == BT_DERIVED
9780 && code->expr1->ts.u.derived->attr.defined_assign_comp)
9781 generate_component_assignments (&code, ns);
9782
9783 break;
9784
9785 case EXEC_LABEL_ASSIGN:
9786 if (code->label1->defined == ST_LABEL_UNKNOWN)
9787 gfc_error ("Label %d referenced at %L is never defined",
9788 code->label1->value, &code->label1->where);
9789 if (t
9790 && (code->expr1->expr_type != EXPR_VARIABLE
9791 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
9792 || code->expr1->symtree->n.sym->ts.kind
9793 != gfc_default_integer_kind
9794 || code->expr1->symtree->n.sym->as != NULL))
9795 gfc_error ("ASSIGN statement at %L requires a scalar "
9796 "default INTEGER variable", &code->expr1->where);
9797 break;
9798
9799 case EXEC_POINTER_ASSIGN:
9800 {
9801 gfc_expr* e;
9802
9803 if (!t)
9804 break;
9805
9806 /* This is both a variable definition and pointer assignment
9807 context, so check both of them. For rank remapping, a final
9808 array ref may be present on the LHS and fool gfc_expr_attr
9809 used in gfc_check_vardef_context. Remove it. */
9810 e = remove_last_array_ref (code->expr1);
9811 t = gfc_check_vardef_context (e, true, false, false,
9812 _("pointer assignment"));
9813 if (t)
9814 t = gfc_check_vardef_context (e, false, false, false,
9815 _("pointer assignment"));
9816 gfc_free_expr (e);
9817 if (!t)
9818 break;
9819
9820 gfc_check_pointer_assign (code->expr1, code->expr2);
9821 break;
9822 }
9823
9824 case EXEC_ARITHMETIC_IF:
9825 if (t
9826 && code->expr1->ts.type != BT_INTEGER
9827 && code->expr1->ts.type != BT_REAL)
9828 gfc_error ("Arithmetic IF statement at %L requires a numeric "
9829 "expression", &code->expr1->where);
9830
9831 resolve_branch (code->label1, code);
9832 resolve_branch (code->label2, code);
9833 resolve_branch (code->label3, code);
9834 break;
9835
9836 case EXEC_IF:
9837 if (t && code->expr1 != NULL
9838 && (code->expr1->ts.type != BT_LOGICAL
9839 || code->expr1->rank != 0))
9840 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9841 &code->expr1->where);
9842 break;
9843
9844 case EXEC_CALL:
9845 call:
9846 resolve_call (code);
9847 break;
9848
9849 case EXEC_COMPCALL:
9850 compcall:
9851 resolve_typebound_subroutine (code);
9852 break;
9853
9854 case EXEC_CALL_PPC:
9855 resolve_ppc_call (code);
9856 break;
9857
9858 case EXEC_SELECT:
9859 /* Select is complicated. Also, a SELECT construct could be
9860 a transformed computed GOTO. */
9861 resolve_select (code, false);
9862 break;
9863
9864 case EXEC_SELECT_TYPE:
9865 resolve_select_type (code, ns);
9866 break;
9867
9868 case EXEC_BLOCK:
9869 resolve_block_construct (code);
9870 break;
9871
9872 case EXEC_DO:
9873 if (code->ext.iterator != NULL)
9874 {
9875 gfc_iterator *iter = code->ext.iterator;
9876 if (gfc_resolve_iterator (iter, true, false))
9877 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
9878 }
9879 break;
9880
9881 case EXEC_DO_WHILE:
9882 if (code->expr1 == NULL)
9883 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
9884 if (t
9885 && (code->expr1->rank != 0
9886 || code->expr1->ts.type != BT_LOGICAL))
9887 gfc_error ("Exit condition of DO WHILE loop at %L must be "
9888 "a scalar LOGICAL expression", &code->expr1->where);
9889 break;
9890
9891 case EXEC_ALLOCATE:
9892 if (t)
9893 resolve_allocate_deallocate (code, "ALLOCATE");
9894
9895 break;
9896
9897 case EXEC_DEALLOCATE:
9898 if (t)
9899 resolve_allocate_deallocate (code, "DEALLOCATE");
9900
9901 break;
9902
9903 case EXEC_OPEN:
9904 if (!gfc_resolve_open (code->ext.open))
9905 break;
9906
9907 resolve_branch (code->ext.open->err, code);
9908 break;
9909
9910 case EXEC_CLOSE:
9911 if (!gfc_resolve_close (code->ext.close))
9912 break;
9913
9914 resolve_branch (code->ext.close->err, code);
9915 break;
9916
9917 case EXEC_BACKSPACE:
9918 case EXEC_ENDFILE:
9919 case EXEC_REWIND:
9920 case EXEC_FLUSH:
9921 if (!gfc_resolve_filepos (code->ext.filepos))
9922 break;
9923
9924 resolve_branch (code->ext.filepos->err, code);
9925 break;
9926
9927 case EXEC_INQUIRE:
9928 if (!gfc_resolve_inquire (code->ext.inquire))
9929 break;
9930
9931 resolve_branch (code->ext.inquire->err, code);
9932 break;
9933
9934 case EXEC_IOLENGTH:
9935 gcc_assert (code->ext.inquire != NULL);
9936 if (!gfc_resolve_inquire (code->ext.inquire))
9937 break;
9938
9939 resolve_branch (code->ext.inquire->err, code);
9940 break;
9941
9942 case EXEC_WAIT:
9943 if (!gfc_resolve_wait (code->ext.wait))
9944 break;
9945
9946 resolve_branch (code->ext.wait->err, code);
9947 resolve_branch (code->ext.wait->end, code);
9948 resolve_branch (code->ext.wait->eor, code);
9949 break;
9950
9951 case EXEC_READ:
9952 case EXEC_WRITE:
9953 if (!gfc_resolve_dt (code->ext.dt, &code->loc))
9954 break;
9955
9956 resolve_branch (code->ext.dt->err, code);
9957 resolve_branch (code->ext.dt->end, code);
9958 resolve_branch (code->ext.dt->eor, code);
9959 break;
9960
9961 case EXEC_TRANSFER:
9962 resolve_transfer (code);
9963 break;
9964
9965 case EXEC_DO_CONCURRENT:
9966 case EXEC_FORALL:
9967 resolve_forall_iterators (code->ext.forall_iterator);
9968
9969 if (code->expr1 != NULL
9970 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
9971 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
9972 "expression", &code->expr1->where);
9973 break;
9974
9975 case EXEC_OMP_ATOMIC:
9976 case EXEC_OMP_BARRIER:
9977 case EXEC_OMP_CRITICAL:
9978 case EXEC_OMP_FLUSH:
9979 case EXEC_OMP_DO:
9980 case EXEC_OMP_MASTER:
9981 case EXEC_OMP_ORDERED:
9982 case EXEC_OMP_SECTIONS:
9983 case EXEC_OMP_SINGLE:
9984 case EXEC_OMP_TASKWAIT:
9985 case EXEC_OMP_TASKYIELD:
9986 case EXEC_OMP_WORKSHARE:
9987 gfc_resolve_omp_directive (code, ns);
9988 break;
9989
9990 case EXEC_OMP_PARALLEL:
9991 case EXEC_OMP_PARALLEL_DO:
9992 case EXEC_OMP_PARALLEL_SECTIONS:
9993 case EXEC_OMP_PARALLEL_WORKSHARE:
9994 case EXEC_OMP_TASK:
9995 omp_workshare_save = omp_workshare_flag;
9996 omp_workshare_flag = 0;
9997 gfc_resolve_omp_directive (code, ns);
9998 omp_workshare_flag = omp_workshare_save;
9999 break;
10000
10001 default:
10002 gfc_internal_error ("resolve_code(): Bad statement code");
10003 }
10004 }
10005
10006 cs_base = frame.prev;
10007 }
10008
10009
10010 /* Resolve initial values and make sure they are compatible with
10011 the variable. */
10012
10013 static void
10014 resolve_values (gfc_symbol *sym)
10015 {
10016 bool t;
10017
10018 if (sym->value == NULL)
10019 return;
10020
10021 if (sym->value->expr_type == EXPR_STRUCTURE)
10022 t= resolve_structure_cons (sym->value, 1);
10023 else
10024 t = gfc_resolve_expr (sym->value);
10025
10026 if (!t)
10027 return;
10028
10029 gfc_check_assign_symbol (sym, NULL, sym->value);
10030 }
10031
10032
10033 /* Verify any BIND(C) derived types in the namespace so we can report errors
10034 for them once, rather than for each variable declared of that type. */
10035
10036 static void
10037 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
10038 {
10039 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
10040 && derived_sym->attr.is_bind_c == 1)
10041 verify_bind_c_derived_type (derived_sym);
10042
10043 return;
10044 }
10045
10046
10047 /* Verify that any binding labels used in a given namespace do not collide
10048 with the names or binding labels of any global symbols. Multiple INTERFACE
10049 for the same procedure are permitted. */
10050
10051 static void
10052 gfc_verify_binding_labels (gfc_symbol *sym)
10053 {
10054 gfc_gsymbol *gsym;
10055 const char *module;
10056
10057 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
10058 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
10059 return;
10060
10061 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
10062
10063 if (sym->module)
10064 module = sym->module;
10065 else if (sym->ns && sym->ns->proc_name
10066 && sym->ns->proc_name->attr.flavor == FL_MODULE)
10067 module = sym->ns->proc_name->name;
10068 else if (sym->ns && sym->ns->parent
10069 && sym->ns && sym->ns->parent->proc_name
10070 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10071 module = sym->ns->parent->proc_name->name;
10072 else
10073 module = NULL;
10074
10075 if (!gsym
10076 || (!gsym->defined
10077 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
10078 {
10079 if (!gsym)
10080 gsym = gfc_get_gsymbol (sym->binding_label);
10081 gsym->where = sym->declared_at;
10082 gsym->sym_name = sym->name;
10083 gsym->binding_label = sym->binding_label;
10084 gsym->binding_label = sym->binding_label;
10085 gsym->ns = sym->ns;
10086 gsym->mod_name = module;
10087 if (sym->attr.function)
10088 gsym->type = GSYM_FUNCTION;
10089 else if (sym->attr.subroutine)
10090 gsym->type = GSYM_SUBROUTINE;
10091 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
10092 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
10093 return;
10094 }
10095
10096 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
10097 {
10098 gfc_error ("Variable %s with binding label %s at %L uses the same global "
10099 "identifier as entity at %L", sym->name,
10100 sym->binding_label, &sym->declared_at, &gsym->where);
10101 /* Clear the binding label to prevent checking multiple times. */
10102 sym->binding_label = NULL;
10103
10104 }
10105 else if (sym->attr.flavor == FL_VARIABLE
10106 && (strcmp (module, gsym->mod_name) != 0
10107 || strcmp (sym->name, gsym->sym_name) != 0))
10108 {
10109 /* This can only happen if the variable is defined in a module - if it
10110 isn't the same module, reject it. */
10111 gfc_error ("Variable %s from module %s with binding label %s at %L uses "
10112 "the same global identifier as entity at %L from module %s",
10113 sym->name, module, sym->binding_label,
10114 &sym->declared_at, &gsym->where, gsym->mod_name);
10115 sym->binding_label = NULL;
10116 }
10117 else if ((sym->attr.function || sym->attr.subroutine)
10118 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
10119 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
10120 && sym != gsym->ns->proc_name
10121 && (strcmp (gsym->sym_name, sym->name) != 0
10122 || module != gsym->mod_name
10123 || (module && strcmp (module, gsym->mod_name) != 0)))
10124 {
10125 /* Print an error if the procdure is defined multiple times; we have to
10126 exclude references to the same procedure via module association or
10127 multiple checks for the same procedure. */
10128 gfc_error ("Procedure %s with binding label %s at %L uses the same "
10129 "global identifier as entity at %L", sym->name,
10130 sym->binding_label, &sym->declared_at, &gsym->where);
10131 sym->binding_label = NULL;
10132 }
10133 }
10134
10135
10136 /* Resolve an index expression. */
10137
10138 static bool
10139 resolve_index_expr (gfc_expr *e)
10140 {
10141 if (!gfc_resolve_expr (e))
10142 return false;
10143
10144 if (!gfc_simplify_expr (e, 0))
10145 return false;
10146
10147 if (!gfc_specification_expr (e))
10148 return false;
10149
10150 return true;
10151 }
10152
10153
10154 /* Resolve a charlen structure. */
10155
10156 static bool
10157 resolve_charlen (gfc_charlen *cl)
10158 {
10159 int i, k;
10160 bool saved_specification_expr;
10161
10162 if (cl->resolved)
10163 return true;
10164
10165 cl->resolved = 1;
10166 saved_specification_expr = specification_expr;
10167 specification_expr = true;
10168
10169 if (cl->length_from_typespec)
10170 {
10171 if (!gfc_resolve_expr (cl->length))
10172 {
10173 specification_expr = saved_specification_expr;
10174 return false;
10175 }
10176
10177 if (!gfc_simplify_expr (cl->length, 0))
10178 {
10179 specification_expr = saved_specification_expr;
10180 return false;
10181 }
10182 }
10183 else
10184 {
10185
10186 if (!resolve_index_expr (cl->length))
10187 {
10188 specification_expr = saved_specification_expr;
10189 return false;
10190 }
10191 }
10192
10193 /* "If the character length parameter value evaluates to a negative
10194 value, the length of character entities declared is zero." */
10195 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
10196 {
10197 if (gfc_option.warn_surprising)
10198 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
10199 " the length has been set to zero",
10200 &cl->length->where, i);
10201 gfc_replace_expr (cl->length,
10202 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
10203 }
10204
10205 /* Check that the character length is not too large. */
10206 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
10207 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
10208 && cl->length->ts.type == BT_INTEGER
10209 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
10210 {
10211 gfc_error ("String length at %L is too large", &cl->length->where);
10212 specification_expr = saved_specification_expr;
10213 return false;
10214 }
10215
10216 specification_expr = saved_specification_expr;
10217 return true;
10218 }
10219
10220
10221 /* Test for non-constant shape arrays. */
10222
10223 static bool
10224 is_non_constant_shape_array (gfc_symbol *sym)
10225 {
10226 gfc_expr *e;
10227 int i;
10228 bool not_constant;
10229
10230 not_constant = false;
10231 if (sym->as != NULL)
10232 {
10233 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
10234 has not been simplified; parameter array references. Do the
10235 simplification now. */
10236 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
10237 {
10238 e = sym->as->lower[i];
10239 if (e && (!resolve_index_expr(e)
10240 || !gfc_is_constant_expr (e)))
10241 not_constant = true;
10242 e = sym->as->upper[i];
10243 if (e && (!resolve_index_expr(e)
10244 || !gfc_is_constant_expr (e)))
10245 not_constant = true;
10246 }
10247 }
10248 return not_constant;
10249 }
10250
10251 /* Given a symbol and an initialization expression, add code to initialize
10252 the symbol to the function entry. */
10253 static void
10254 build_init_assign (gfc_symbol *sym, gfc_expr *init)
10255 {
10256 gfc_expr *lval;
10257 gfc_code *init_st;
10258 gfc_namespace *ns = sym->ns;
10259
10260 /* Search for the function namespace if this is a contained
10261 function without an explicit result. */
10262 if (sym->attr.function && sym == sym->result
10263 && sym->name != sym->ns->proc_name->name)
10264 {
10265 ns = ns->contained;
10266 for (;ns; ns = ns->sibling)
10267 if (strcmp (ns->proc_name->name, sym->name) == 0)
10268 break;
10269 }
10270
10271 if (ns == NULL)
10272 {
10273 gfc_free_expr (init);
10274 return;
10275 }
10276
10277 /* Build an l-value expression for the result. */
10278 lval = gfc_lval_expr_from_sym (sym);
10279
10280 /* Add the code at scope entry. */
10281 init_st = gfc_get_code ();
10282 init_st->next = ns->code;
10283 ns->code = init_st;
10284
10285 /* Assign the default initializer to the l-value. */
10286 init_st->loc = sym->declared_at;
10287 init_st->op = EXEC_INIT_ASSIGN;
10288 init_st->expr1 = lval;
10289 init_st->expr2 = init;
10290 }
10291
10292 /* Assign the default initializer to a derived type variable or result. */
10293
10294 static void
10295 apply_default_init (gfc_symbol *sym)
10296 {
10297 gfc_expr *init = NULL;
10298
10299 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10300 return;
10301
10302 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
10303 init = gfc_default_initializer (&sym->ts);
10304
10305 if (init == NULL && sym->ts.type != BT_CLASS)
10306 return;
10307
10308 build_init_assign (sym, init);
10309 sym->attr.referenced = 1;
10310 }
10311
10312 /* Build an initializer for a local integer, real, complex, logical, or
10313 character variable, based on the command line flags finit-local-zero,
10314 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
10315 null if the symbol should not have a default initialization. */
10316 static gfc_expr *
10317 build_default_init_expr (gfc_symbol *sym)
10318 {
10319 int char_len;
10320 gfc_expr *init_expr;
10321 int i;
10322
10323 /* These symbols should never have a default initialization. */
10324 if (sym->attr.allocatable
10325 || sym->attr.external
10326 || sym->attr.dummy
10327 || sym->attr.pointer
10328 || sym->attr.in_equivalence
10329 || sym->attr.in_common
10330 || sym->attr.data
10331 || sym->module
10332 || sym->attr.cray_pointee
10333 || sym->attr.cray_pointer
10334 || sym->assoc)
10335 return NULL;
10336
10337 /* Now we'll try to build an initializer expression. */
10338 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
10339 &sym->declared_at);
10340
10341 /* We will only initialize integers, reals, complex, logicals, and
10342 characters, and only if the corresponding command-line flags
10343 were set. Otherwise, we free init_expr and return null. */
10344 switch (sym->ts.type)
10345 {
10346 case BT_INTEGER:
10347 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
10348 mpz_set_si (init_expr->value.integer,
10349 gfc_option.flag_init_integer_value);
10350 else
10351 {
10352 gfc_free_expr (init_expr);
10353 init_expr = NULL;
10354 }
10355 break;
10356
10357 case BT_REAL:
10358 switch (gfc_option.flag_init_real)
10359 {
10360 case GFC_INIT_REAL_SNAN:
10361 init_expr->is_snan = 1;
10362 /* Fall through. */
10363 case GFC_INIT_REAL_NAN:
10364 mpfr_set_nan (init_expr->value.real);
10365 break;
10366
10367 case GFC_INIT_REAL_INF:
10368 mpfr_set_inf (init_expr->value.real, 1);
10369 break;
10370
10371 case GFC_INIT_REAL_NEG_INF:
10372 mpfr_set_inf (init_expr->value.real, -1);
10373 break;
10374
10375 case GFC_INIT_REAL_ZERO:
10376 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
10377 break;
10378
10379 default:
10380 gfc_free_expr (init_expr);
10381 init_expr = NULL;
10382 break;
10383 }
10384 break;
10385
10386 case BT_COMPLEX:
10387 switch (gfc_option.flag_init_real)
10388 {
10389 case GFC_INIT_REAL_SNAN:
10390 init_expr->is_snan = 1;
10391 /* Fall through. */
10392 case GFC_INIT_REAL_NAN:
10393 mpfr_set_nan (mpc_realref (init_expr->value.complex));
10394 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
10395 break;
10396
10397 case GFC_INIT_REAL_INF:
10398 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
10399 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
10400 break;
10401
10402 case GFC_INIT_REAL_NEG_INF:
10403 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
10404 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
10405 break;
10406
10407 case GFC_INIT_REAL_ZERO:
10408 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
10409 break;
10410
10411 default:
10412 gfc_free_expr (init_expr);
10413 init_expr = NULL;
10414 break;
10415 }
10416 break;
10417
10418 case BT_LOGICAL:
10419 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
10420 init_expr->value.logical = 0;
10421 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
10422 init_expr->value.logical = 1;
10423 else
10424 {
10425 gfc_free_expr (init_expr);
10426 init_expr = NULL;
10427 }
10428 break;
10429
10430 case BT_CHARACTER:
10431 /* For characters, the length must be constant in order to
10432 create a default initializer. */
10433 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10434 && sym->ts.u.cl->length
10435 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10436 {
10437 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
10438 init_expr->value.character.length = char_len;
10439 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
10440 for (i = 0; i < char_len; i++)
10441 init_expr->value.character.string[i]
10442 = (unsigned char) gfc_option.flag_init_character_value;
10443 }
10444 else
10445 {
10446 gfc_free_expr (init_expr);
10447 init_expr = NULL;
10448 }
10449 if (!init_expr && gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10450 && sym->ts.u.cl->length)
10451 {
10452 gfc_actual_arglist *arg;
10453 init_expr = gfc_get_expr ();
10454 init_expr->where = sym->declared_at;
10455 init_expr->ts = sym->ts;
10456 init_expr->expr_type = EXPR_FUNCTION;
10457 init_expr->value.function.isym =
10458 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
10459 init_expr->value.function.name = "repeat";
10460 arg = gfc_get_actual_arglist ();
10461 arg->expr = gfc_get_character_expr (sym->ts.kind, &sym->declared_at,
10462 NULL, 1);
10463 arg->expr->value.character.string[0]
10464 = gfc_option.flag_init_character_value;
10465 arg->next = gfc_get_actual_arglist ();
10466 arg->next->expr = gfc_copy_expr (sym->ts.u.cl->length);
10467 init_expr->value.function.actual = arg;
10468 }
10469 break;
10470
10471 default:
10472 gfc_free_expr (init_expr);
10473 init_expr = NULL;
10474 }
10475 return init_expr;
10476 }
10477
10478 /* Add an initialization expression to a local variable. */
10479 static void
10480 apply_default_init_local (gfc_symbol *sym)
10481 {
10482 gfc_expr *init = NULL;
10483
10484 /* The symbol should be a variable or a function return value. */
10485 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10486 || (sym->attr.function && sym->result != sym))
10487 return;
10488
10489 /* Try to build the initializer expression. If we can't initialize
10490 this symbol, then init will be NULL. */
10491 init = build_default_init_expr (sym);
10492 if (init == NULL)
10493 return;
10494
10495 /* For saved variables, we don't want to add an initializer at function
10496 entry, so we just add a static initializer. Note that automatic variables
10497 are stack allocated even with -fno-automatic; we have also to exclude
10498 result variable, which are also nonstatic. */
10499 if (sym->attr.save || sym->ns->save_all
10500 || (gfc_option.flag_max_stack_var_size == 0 && !sym->attr.result
10501 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
10502 {
10503 /* Don't clobber an existing initializer! */
10504 gcc_assert (sym->value == NULL);
10505 sym->value = init;
10506 return;
10507 }
10508
10509 build_init_assign (sym, init);
10510 }
10511
10512
10513 /* Resolution of common features of flavors variable and procedure. */
10514
10515 static bool
10516 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
10517 {
10518 gfc_array_spec *as;
10519
10520 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10521 as = CLASS_DATA (sym)->as;
10522 else
10523 as = sym->as;
10524
10525 /* Constraints on deferred shape variable. */
10526 if (as == NULL || as->type != AS_DEFERRED)
10527 {
10528 bool pointer, allocatable, dimension;
10529
10530 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10531 {
10532 pointer = CLASS_DATA (sym)->attr.class_pointer;
10533 allocatable = CLASS_DATA (sym)->attr.allocatable;
10534 dimension = CLASS_DATA (sym)->attr.dimension;
10535 }
10536 else
10537 {
10538 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
10539 allocatable = sym->attr.allocatable;
10540 dimension = sym->attr.dimension;
10541 }
10542
10543 if (allocatable)
10544 {
10545 if (dimension && as->type != AS_ASSUMED_RANK)
10546 {
10547 gfc_error ("Allocatable array '%s' at %L must have a deferred "
10548 "shape or assumed rank", sym->name, &sym->declared_at);
10549 return false;
10550 }
10551 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
10552 "'%s' at %L may not be ALLOCATABLE",
10553 sym->name, &sym->declared_at))
10554 return false;
10555 }
10556
10557 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
10558 {
10559 gfc_error ("Array pointer '%s' at %L must have a deferred shape or "
10560 "assumed rank", sym->name, &sym->declared_at);
10561 return false;
10562 }
10563 }
10564 else
10565 {
10566 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
10567 && sym->ts.type != BT_CLASS && !sym->assoc)
10568 {
10569 gfc_error ("Array '%s' at %L cannot have a deferred shape",
10570 sym->name, &sym->declared_at);
10571 return false;
10572 }
10573 }
10574
10575 /* Constraints on polymorphic variables. */
10576 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
10577 {
10578 /* F03:C502. */
10579 if (sym->attr.class_ok
10580 && !sym->attr.select_type_temporary
10581 && !UNLIMITED_POLY (sym)
10582 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
10583 {
10584 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
10585 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
10586 &sym->declared_at);
10587 return false;
10588 }
10589
10590 /* F03:C509. */
10591 /* Assume that use associated symbols were checked in the module ns.
10592 Class-variables that are associate-names are also something special
10593 and excepted from the test. */
10594 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
10595 {
10596 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
10597 "or pointer", sym->name, &sym->declared_at);
10598 return false;
10599 }
10600 }
10601
10602 return true;
10603 }
10604
10605
10606 /* Additional checks for symbols with flavor variable and derived
10607 type. To be called from resolve_fl_variable. */
10608
10609 static bool
10610 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
10611 {
10612 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
10613
10614 /* Check to see if a derived type is blocked from being host
10615 associated by the presence of another class I symbol in the same
10616 namespace. 14.6.1.3 of the standard and the discussion on
10617 comp.lang.fortran. */
10618 if (sym->ns != sym->ts.u.derived->ns
10619 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
10620 {
10621 gfc_symbol *s;
10622 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
10623 if (s && s->attr.generic)
10624 s = gfc_find_dt_in_generic (s);
10625 if (s && s->attr.flavor != FL_DERIVED)
10626 {
10627 gfc_error ("The type '%s' cannot be host associated at %L "
10628 "because it is blocked by an incompatible object "
10629 "of the same name declared at %L",
10630 sym->ts.u.derived->name, &sym->declared_at,
10631 &s->declared_at);
10632 return false;
10633 }
10634 }
10635
10636 /* 4th constraint in section 11.3: "If an object of a type for which
10637 component-initialization is specified (R429) appears in the
10638 specification-part of a module and does not have the ALLOCATABLE
10639 or POINTER attribute, the object shall have the SAVE attribute."
10640
10641 The check for initializers is performed with
10642 gfc_has_default_initializer because gfc_default_initializer generates
10643 a hidden default for allocatable components. */
10644 if (!(sym->value || no_init_flag) && sym->ns->proc_name
10645 && sym->ns->proc_name->attr.flavor == FL_MODULE
10646 && !sym->ns->save_all && !sym->attr.save
10647 && !sym->attr.pointer && !sym->attr.allocatable
10648 && gfc_has_default_initializer (sym->ts.u.derived)
10649 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
10650 "'%s' at %L, needed due to the default "
10651 "initialization", sym->name, &sym->declared_at))
10652 return false;
10653
10654 /* Assign default initializer. */
10655 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
10656 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
10657 {
10658 sym->value = gfc_default_initializer (&sym->ts);
10659 }
10660
10661 return true;
10662 }
10663
10664
10665 /* Resolve symbols with flavor variable. */
10666
10667 static bool
10668 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
10669 {
10670 int no_init_flag, automatic_flag;
10671 gfc_expr *e;
10672 const char *auto_save_msg;
10673 bool saved_specification_expr;
10674
10675 auto_save_msg = "Automatic object '%s' at %L cannot have the "
10676 "SAVE attribute";
10677
10678 if (!resolve_fl_var_and_proc (sym, mp_flag))
10679 return false;
10680
10681 /* Set this flag to check that variables are parameters of all entries.
10682 This check is effected by the call to gfc_resolve_expr through
10683 is_non_constant_shape_array. */
10684 saved_specification_expr = specification_expr;
10685 specification_expr = true;
10686
10687 if (sym->ns->proc_name
10688 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10689 || sym->ns->proc_name->attr.is_main_program)
10690 && !sym->attr.use_assoc
10691 && !sym->attr.allocatable
10692 && !sym->attr.pointer
10693 && is_non_constant_shape_array (sym))
10694 {
10695 /* The shape of a main program or module array needs to be
10696 constant. */
10697 gfc_error ("The module or main program array '%s' at %L must "
10698 "have constant shape", sym->name, &sym->declared_at);
10699 specification_expr = saved_specification_expr;
10700 return false;
10701 }
10702
10703 /* Constraints on deferred type parameter. */
10704 if (sym->ts.deferred && !(sym->attr.pointer || sym->attr.allocatable))
10705 {
10706 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
10707 "requires either the pointer or allocatable attribute",
10708 sym->name, &sym->declared_at);
10709 specification_expr = saved_specification_expr;
10710 return false;
10711 }
10712
10713 if (sym->ts.type == BT_CHARACTER)
10714 {
10715 /* Make sure that character string variables with assumed length are
10716 dummy arguments. */
10717 e = sym->ts.u.cl->length;
10718 if (e == NULL && !sym->attr.dummy && !sym->attr.result
10719 && !sym->ts.deferred && !sym->attr.select_type_temporary)
10720 {
10721 gfc_error ("Entity with assumed character length at %L must be a "
10722 "dummy argument or a PARAMETER", &sym->declared_at);
10723 specification_expr = saved_specification_expr;
10724 return false;
10725 }
10726
10727 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
10728 {
10729 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10730 specification_expr = saved_specification_expr;
10731 return false;
10732 }
10733
10734 if (!gfc_is_constant_expr (e)
10735 && !(e->expr_type == EXPR_VARIABLE
10736 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
10737 {
10738 if (!sym->attr.use_assoc && sym->ns->proc_name
10739 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10740 || sym->ns->proc_name->attr.is_main_program))
10741 {
10742 gfc_error ("'%s' at %L must have constant character length "
10743 "in this context", sym->name, &sym->declared_at);
10744 specification_expr = saved_specification_expr;
10745 return false;
10746 }
10747 if (sym->attr.in_common)
10748 {
10749 gfc_error ("COMMON variable '%s' at %L must have constant "
10750 "character length", sym->name, &sym->declared_at);
10751 specification_expr = saved_specification_expr;
10752 return false;
10753 }
10754 }
10755 }
10756
10757 if (sym->value == NULL && sym->attr.referenced)
10758 apply_default_init_local (sym); /* Try to apply a default initialization. */
10759
10760 /* Determine if the symbol may not have an initializer. */
10761 no_init_flag = automatic_flag = 0;
10762 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
10763 || sym->attr.intrinsic || sym->attr.result)
10764 no_init_flag = 1;
10765 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
10766 && is_non_constant_shape_array (sym))
10767 {
10768 no_init_flag = automatic_flag = 1;
10769
10770 /* Also, they must not have the SAVE attribute.
10771 SAVE_IMPLICIT is checked below. */
10772 if (sym->as && sym->attr.codimension)
10773 {
10774 int corank = sym->as->corank;
10775 sym->as->corank = 0;
10776 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
10777 sym->as->corank = corank;
10778 }
10779 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
10780 {
10781 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
10782 specification_expr = saved_specification_expr;
10783 return false;
10784 }
10785 }
10786
10787 /* Ensure that any initializer is simplified. */
10788 if (sym->value)
10789 gfc_simplify_expr (sym->value, 1);
10790
10791 /* Reject illegal initializers. */
10792 if (!sym->mark && sym->value)
10793 {
10794 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
10795 && CLASS_DATA (sym)->attr.allocatable))
10796 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
10797 sym->name, &sym->declared_at);
10798 else if (sym->attr.external)
10799 gfc_error ("External '%s' at %L cannot have an initializer",
10800 sym->name, &sym->declared_at);
10801 else if (sym->attr.dummy
10802 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
10803 gfc_error ("Dummy '%s' at %L cannot have an initializer",
10804 sym->name, &sym->declared_at);
10805 else if (sym->attr.intrinsic)
10806 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
10807 sym->name, &sym->declared_at);
10808 else if (sym->attr.result)
10809 gfc_error ("Function result '%s' at %L cannot have an initializer",
10810 sym->name, &sym->declared_at);
10811 else if (automatic_flag)
10812 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
10813 sym->name, &sym->declared_at);
10814 else
10815 goto no_init_error;
10816 specification_expr = saved_specification_expr;
10817 return false;
10818 }
10819
10820 no_init_error:
10821 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
10822 {
10823 bool res = resolve_fl_variable_derived (sym, no_init_flag);
10824 specification_expr = saved_specification_expr;
10825 return res;
10826 }
10827
10828 specification_expr = saved_specification_expr;
10829 return true;
10830 }
10831
10832
10833 /* Resolve a procedure. */
10834
10835 static bool
10836 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
10837 {
10838 gfc_formal_arglist *arg;
10839
10840 if (sym->attr.function
10841 && !resolve_fl_var_and_proc (sym, mp_flag))
10842 return false;
10843
10844 if (sym->ts.type == BT_CHARACTER)
10845 {
10846 gfc_charlen *cl = sym->ts.u.cl;
10847
10848 if (cl && cl->length && gfc_is_constant_expr (cl->length)
10849 && !resolve_charlen (cl))
10850 return false;
10851
10852 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10853 && sym->attr.proc == PROC_ST_FUNCTION)
10854 {
10855 gfc_error ("Character-valued statement function '%s' at %L must "
10856 "have constant length", sym->name, &sym->declared_at);
10857 return false;
10858 }
10859 }
10860
10861 /* Ensure that derived type for are not of a private type. Internal
10862 module procedures are excluded by 2.2.3.3 - i.e., they are not
10863 externally accessible and can access all the objects accessible in
10864 the host. */
10865 if (!(sym->ns->parent
10866 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10867 && gfc_check_symbol_access (sym))
10868 {
10869 gfc_interface *iface;
10870
10871 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
10872 {
10873 if (arg->sym
10874 && arg->sym->ts.type == BT_DERIVED
10875 && !arg->sym->ts.u.derived->attr.use_assoc
10876 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10877 && !gfc_notify_std (GFC_STD_F2003, "'%s' is of a PRIVATE type "
10878 "and cannot be a dummy argument"
10879 " of '%s', which is PUBLIC at %L",
10880 arg->sym->name, sym->name,
10881 &sym->declared_at))
10882 {
10883 /* Stop this message from recurring. */
10884 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10885 return false;
10886 }
10887 }
10888
10889 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10890 PRIVATE to the containing module. */
10891 for (iface = sym->generic; iface; iface = iface->next)
10892 {
10893 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
10894 {
10895 if (arg->sym
10896 && arg->sym->ts.type == BT_DERIVED
10897 && !arg->sym->ts.u.derived->attr.use_assoc
10898 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10899 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
10900 "PUBLIC interface '%s' at %L "
10901 "takes dummy arguments of '%s' which "
10902 "is PRIVATE", iface->sym->name,
10903 sym->name, &iface->sym->declared_at,
10904 gfc_typename(&arg->sym->ts)))
10905 {
10906 /* Stop this message from recurring. */
10907 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10908 return false;
10909 }
10910 }
10911 }
10912
10913 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10914 PRIVATE to the containing module. */
10915 for (iface = sym->generic; iface; iface = iface->next)
10916 {
10917 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
10918 {
10919 if (arg->sym
10920 && arg->sym->ts.type == BT_DERIVED
10921 && !arg->sym->ts.u.derived->attr.use_assoc
10922 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
10923 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
10924 "PUBLIC interface '%s' at %L takes "
10925 "dummy arguments of '%s' which is "
10926 "PRIVATE", iface->sym->name,
10927 sym->name, &iface->sym->declared_at,
10928 gfc_typename(&arg->sym->ts)))
10929 {
10930 /* Stop this message from recurring. */
10931 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10932 return false;
10933 }
10934 }
10935 }
10936 }
10937
10938 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
10939 && !sym->attr.proc_pointer)
10940 {
10941 gfc_error ("Function '%s' at %L cannot have an initializer",
10942 sym->name, &sym->declared_at);
10943 return false;
10944 }
10945
10946 /* An external symbol may not have an initializer because it is taken to be
10947 a procedure. Exception: Procedure Pointers. */
10948 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
10949 {
10950 gfc_error ("External object '%s' at %L may not have an initializer",
10951 sym->name, &sym->declared_at);
10952 return false;
10953 }
10954
10955 /* An elemental function is required to return a scalar 12.7.1 */
10956 if (sym->attr.elemental && sym->attr.function && sym->as)
10957 {
10958 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
10959 "result", sym->name, &sym->declared_at);
10960 /* Reset so that the error only occurs once. */
10961 sym->attr.elemental = 0;
10962 return false;
10963 }
10964
10965 if (sym->attr.proc == PROC_ST_FUNCTION
10966 && (sym->attr.allocatable || sym->attr.pointer))
10967 {
10968 gfc_error ("Statement function '%s' at %L may not have pointer or "
10969 "allocatable attribute", sym->name, &sym->declared_at);
10970 return false;
10971 }
10972
10973 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
10974 char-len-param shall not be array-valued, pointer-valued, recursive
10975 or pure. ....snip... A character value of * may only be used in the
10976 following ways: (i) Dummy arg of procedure - dummy associates with
10977 actual length; (ii) To declare a named constant; or (iii) External
10978 function - but length must be declared in calling scoping unit. */
10979 if (sym->attr.function
10980 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
10981 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
10982 {
10983 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
10984 || (sym->attr.recursive) || (sym->attr.pure))
10985 {
10986 if (sym->as && sym->as->rank)
10987 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10988 "array-valued", sym->name, &sym->declared_at);
10989
10990 if (sym->attr.pointer)
10991 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10992 "pointer-valued", sym->name, &sym->declared_at);
10993
10994 if (sym->attr.pure)
10995 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10996 "pure", sym->name, &sym->declared_at);
10997
10998 if (sym->attr.recursive)
10999 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11000 "recursive", sym->name, &sym->declared_at);
11001
11002 return false;
11003 }
11004
11005 /* Appendix B.2 of the standard. Contained functions give an
11006 error anyway. Fixed-form is likely to be F77/legacy. Deferred
11007 character length is an F2003 feature. */
11008 if (!sym->attr.contained
11009 && gfc_current_form != FORM_FIXED
11010 && !sym->ts.deferred)
11011 gfc_notify_std (GFC_STD_F95_OBS,
11012 "CHARACTER(*) function '%s' at %L",
11013 sym->name, &sym->declared_at);
11014 }
11015
11016 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
11017 {
11018 gfc_formal_arglist *curr_arg;
11019 int has_non_interop_arg = 0;
11020
11021 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11022 sym->common_block))
11023 {
11024 /* Clear these to prevent looking at them again if there was an
11025 error. */
11026 sym->attr.is_bind_c = 0;
11027 sym->attr.is_c_interop = 0;
11028 sym->ts.is_c_interop = 0;
11029 }
11030 else
11031 {
11032 /* So far, no errors have been found. */
11033 sym->attr.is_c_interop = 1;
11034 sym->ts.is_c_interop = 1;
11035 }
11036
11037 curr_arg = gfc_sym_get_dummy_args (sym);
11038 while (curr_arg != NULL)
11039 {
11040 /* Skip implicitly typed dummy args here. */
11041 if (curr_arg->sym->attr.implicit_type == 0)
11042 if (!gfc_verify_c_interop_param (curr_arg->sym))
11043 /* If something is found to fail, record the fact so we
11044 can mark the symbol for the procedure as not being
11045 BIND(C) to try and prevent multiple errors being
11046 reported. */
11047 has_non_interop_arg = 1;
11048
11049 curr_arg = curr_arg->next;
11050 }
11051
11052 /* See if any of the arguments were not interoperable and if so, clear
11053 the procedure symbol to prevent duplicate error messages. */
11054 if (has_non_interop_arg != 0)
11055 {
11056 sym->attr.is_c_interop = 0;
11057 sym->ts.is_c_interop = 0;
11058 sym->attr.is_bind_c = 0;
11059 }
11060 }
11061
11062 if (!sym->attr.proc_pointer)
11063 {
11064 if (sym->attr.save == SAVE_EXPLICIT)
11065 {
11066 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
11067 "in '%s' at %L", sym->name, &sym->declared_at);
11068 return false;
11069 }
11070 if (sym->attr.intent)
11071 {
11072 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
11073 "in '%s' at %L", sym->name, &sym->declared_at);
11074 return false;
11075 }
11076 if (sym->attr.subroutine && sym->attr.result)
11077 {
11078 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
11079 "in '%s' at %L", sym->name, &sym->declared_at);
11080 return false;
11081 }
11082 if (sym->attr.external && sym->attr.function
11083 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
11084 || sym->attr.contained))
11085 {
11086 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
11087 "in '%s' at %L", sym->name, &sym->declared_at);
11088 return false;
11089 }
11090 if (strcmp ("ppr@", sym->name) == 0)
11091 {
11092 gfc_error ("Procedure pointer result '%s' at %L "
11093 "is missing the pointer attribute",
11094 sym->ns->proc_name->name, &sym->declared_at);
11095 return false;
11096 }
11097 }
11098
11099 return true;
11100 }
11101
11102
11103 /* Resolve a list of finalizer procedures. That is, after they have hopefully
11104 been defined and we now know their defined arguments, check that they fulfill
11105 the requirements of the standard for procedures used as finalizers. */
11106
11107 static bool
11108 gfc_resolve_finalizers (gfc_symbol* derived)
11109 {
11110 gfc_finalizer* list;
11111 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
11112 bool result = true;
11113 bool seen_scalar = false;
11114
11115 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
11116 return true;
11117
11118 /* Walk over the list of finalizer-procedures, check them, and if any one
11119 does not fit in with the standard's definition, print an error and remove
11120 it from the list. */
11121 prev_link = &derived->f2k_derived->finalizers;
11122 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
11123 {
11124 gfc_formal_arglist *dummy_args;
11125 gfc_symbol* arg;
11126 gfc_finalizer* i;
11127 int my_rank;
11128
11129 /* Skip this finalizer if we already resolved it. */
11130 if (list->proc_tree)
11131 {
11132 prev_link = &(list->next);
11133 continue;
11134 }
11135
11136 /* Check this exists and is a SUBROUTINE. */
11137 if (!list->proc_sym->attr.subroutine)
11138 {
11139 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
11140 list->proc_sym->name, &list->where);
11141 goto error;
11142 }
11143
11144 /* We should have exactly one argument. */
11145 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
11146 if (!dummy_args || dummy_args->next)
11147 {
11148 gfc_error ("FINAL procedure at %L must have exactly one argument",
11149 &list->where);
11150 goto error;
11151 }
11152 arg = dummy_args->sym;
11153
11154 /* This argument must be of our type. */
11155 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
11156 {
11157 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
11158 &arg->declared_at, derived->name);
11159 goto error;
11160 }
11161
11162 /* It must neither be a pointer nor allocatable nor optional. */
11163 if (arg->attr.pointer)
11164 {
11165 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
11166 &arg->declared_at);
11167 goto error;
11168 }
11169 if (arg->attr.allocatable)
11170 {
11171 gfc_error ("Argument of FINAL procedure at %L must not be"
11172 " ALLOCATABLE", &arg->declared_at);
11173 goto error;
11174 }
11175 if (arg->attr.optional)
11176 {
11177 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
11178 &arg->declared_at);
11179 goto error;
11180 }
11181
11182 /* It must not be INTENT(OUT). */
11183 if (arg->attr.intent == INTENT_OUT)
11184 {
11185 gfc_error ("Argument of FINAL procedure at %L must not be"
11186 " INTENT(OUT)", &arg->declared_at);
11187 goto error;
11188 }
11189
11190 /* Warn if the procedure is non-scalar and not assumed shape. */
11191 if (gfc_option.warn_surprising && arg->as && arg->as->rank != 0
11192 && arg->as->type != AS_ASSUMED_SHAPE)
11193 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
11194 " shape argument", &arg->declared_at);
11195
11196 /* Check that it does not match in kind and rank with a FINAL procedure
11197 defined earlier. To really loop over the *earlier* declarations,
11198 we need to walk the tail of the list as new ones were pushed at the
11199 front. */
11200 /* TODO: Handle kind parameters once they are implemented. */
11201 my_rank = (arg->as ? arg->as->rank : 0);
11202 for (i = list->next; i; i = i->next)
11203 {
11204 gfc_formal_arglist *dummy_args;
11205
11206 /* Argument list might be empty; that is an error signalled earlier,
11207 but we nevertheless continued resolving. */
11208 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
11209 if (dummy_args)
11210 {
11211 gfc_symbol* i_arg = dummy_args->sym;
11212 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
11213 if (i_rank == my_rank)
11214 {
11215 gfc_error ("FINAL procedure '%s' declared at %L has the same"
11216 " rank (%d) as '%s'",
11217 list->proc_sym->name, &list->where, my_rank,
11218 i->proc_sym->name);
11219 goto error;
11220 }
11221 }
11222 }
11223
11224 /* Is this the/a scalar finalizer procedure? */
11225 if (!arg->as || arg->as->rank == 0)
11226 seen_scalar = true;
11227
11228 /* Find the symtree for this procedure. */
11229 gcc_assert (!list->proc_tree);
11230 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
11231
11232 prev_link = &list->next;
11233 continue;
11234
11235 /* Remove wrong nodes immediately from the list so we don't risk any
11236 troubles in the future when they might fail later expectations. */
11237 error:
11238 result = false;
11239 i = list;
11240 *prev_link = list->next;
11241 gfc_free_finalizer (i);
11242 }
11243
11244 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
11245 were nodes in the list, must have been for arrays. It is surely a good
11246 idea to have a scalar version there if there's something to finalize. */
11247 if (gfc_option.warn_surprising && result && !seen_scalar)
11248 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
11249 " defined at %L, suggest also scalar one",
11250 derived->name, &derived->declared_at);
11251
11252 gfc_find_derived_vtab (derived);
11253 return result;
11254 }
11255
11256
11257 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
11258
11259 static bool
11260 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
11261 const char* generic_name, locus where)
11262 {
11263 gfc_symbol *sym1, *sym2;
11264 const char *pass1, *pass2;
11265
11266 gcc_assert (t1->specific && t2->specific);
11267 gcc_assert (!t1->specific->is_generic);
11268 gcc_assert (!t2->specific->is_generic);
11269 gcc_assert (t1->is_operator == t2->is_operator);
11270
11271 sym1 = t1->specific->u.specific->n.sym;
11272 sym2 = t2->specific->u.specific->n.sym;
11273
11274 if (sym1 == sym2)
11275 return true;
11276
11277 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
11278 if (sym1->attr.subroutine != sym2->attr.subroutine
11279 || sym1->attr.function != sym2->attr.function)
11280 {
11281 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
11282 " GENERIC '%s' at %L",
11283 sym1->name, sym2->name, generic_name, &where);
11284 return false;
11285 }
11286
11287 /* Compare the interfaces. */
11288 if (t1->specific->nopass)
11289 pass1 = NULL;
11290 else if (t1->specific->pass_arg)
11291 pass1 = t1->specific->pass_arg;
11292 else
11293 pass1 = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym)->sym->name;
11294 if (t2->specific->nopass)
11295 pass2 = NULL;
11296 else if (t2->specific->pass_arg)
11297 pass2 = t2->specific->pass_arg;
11298 else
11299 pass2 = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym)->sym->name;
11300 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
11301 NULL, 0, pass1, pass2))
11302 {
11303 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
11304 sym1->name, sym2->name, generic_name, &where);
11305 return false;
11306 }
11307
11308 return true;
11309 }
11310
11311
11312 /* Worker function for resolving a generic procedure binding; this is used to
11313 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
11314
11315 The difference between those cases is finding possible inherited bindings
11316 that are overridden, as one has to look for them in tb_sym_root,
11317 tb_uop_root or tb_op, respectively. Thus the caller must already find
11318 the super-type and set p->overridden correctly. */
11319
11320 static bool
11321 resolve_tb_generic_targets (gfc_symbol* super_type,
11322 gfc_typebound_proc* p, const char* name)
11323 {
11324 gfc_tbp_generic* target;
11325 gfc_symtree* first_target;
11326 gfc_symtree* inherited;
11327
11328 gcc_assert (p && p->is_generic);
11329
11330 /* Try to find the specific bindings for the symtrees in our target-list. */
11331 gcc_assert (p->u.generic);
11332 for (target = p->u.generic; target; target = target->next)
11333 if (!target->specific)
11334 {
11335 gfc_typebound_proc* overridden_tbp;
11336 gfc_tbp_generic* g;
11337 const char* target_name;
11338
11339 target_name = target->specific_st->name;
11340
11341 /* Defined for this type directly. */
11342 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
11343 {
11344 target->specific = target->specific_st->n.tb;
11345 goto specific_found;
11346 }
11347
11348 /* Look for an inherited specific binding. */
11349 if (super_type)
11350 {
11351 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
11352 true, NULL);
11353
11354 if (inherited)
11355 {
11356 gcc_assert (inherited->n.tb);
11357 target->specific = inherited->n.tb;
11358 goto specific_found;
11359 }
11360 }
11361
11362 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
11363 " at %L", target_name, name, &p->where);
11364 return false;
11365
11366 /* Once we've found the specific binding, check it is not ambiguous with
11367 other specifics already found or inherited for the same GENERIC. */
11368 specific_found:
11369 gcc_assert (target->specific);
11370
11371 /* This must really be a specific binding! */
11372 if (target->specific->is_generic)
11373 {
11374 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
11375 " '%s' is GENERIC, too", name, &p->where, target_name);
11376 return false;
11377 }
11378
11379 /* Check those already resolved on this type directly. */
11380 for (g = p->u.generic; g; g = g->next)
11381 if (g != target && g->specific
11382 && !check_generic_tbp_ambiguity (target, g, name, p->where))
11383 return false;
11384
11385 /* Check for ambiguity with inherited specific targets. */
11386 for (overridden_tbp = p->overridden; overridden_tbp;
11387 overridden_tbp = overridden_tbp->overridden)
11388 if (overridden_tbp->is_generic)
11389 {
11390 for (g = overridden_tbp->u.generic; g; g = g->next)
11391 {
11392 gcc_assert (g->specific);
11393 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
11394 return false;
11395 }
11396 }
11397 }
11398
11399 /* If we attempt to "overwrite" a specific binding, this is an error. */
11400 if (p->overridden && !p->overridden->is_generic)
11401 {
11402 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
11403 " the same name", name, &p->where);
11404 return false;
11405 }
11406
11407 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
11408 all must have the same attributes here. */
11409 first_target = p->u.generic->specific->u.specific;
11410 gcc_assert (first_target);
11411 p->subroutine = first_target->n.sym->attr.subroutine;
11412 p->function = first_target->n.sym->attr.function;
11413
11414 return true;
11415 }
11416
11417
11418 /* Resolve a GENERIC procedure binding for a derived type. */
11419
11420 static bool
11421 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
11422 {
11423 gfc_symbol* super_type;
11424
11425 /* Find the overridden binding if any. */
11426 st->n.tb->overridden = NULL;
11427 super_type = gfc_get_derived_super_type (derived);
11428 if (super_type)
11429 {
11430 gfc_symtree* overridden;
11431 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
11432 true, NULL);
11433
11434 if (overridden && overridden->n.tb)
11435 st->n.tb->overridden = overridden->n.tb;
11436 }
11437
11438 /* Resolve using worker function. */
11439 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
11440 }
11441
11442
11443 /* Retrieve the target-procedure of an operator binding and do some checks in
11444 common for intrinsic and user-defined type-bound operators. */
11445
11446 static gfc_symbol*
11447 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
11448 {
11449 gfc_symbol* target_proc;
11450
11451 gcc_assert (target->specific && !target->specific->is_generic);
11452 target_proc = target->specific->u.specific->n.sym;
11453 gcc_assert (target_proc);
11454
11455 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
11456 if (target->specific->nopass)
11457 {
11458 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
11459 return NULL;
11460 }
11461
11462 return target_proc;
11463 }
11464
11465
11466 /* Resolve a type-bound intrinsic operator. */
11467
11468 static bool
11469 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
11470 gfc_typebound_proc* p)
11471 {
11472 gfc_symbol* super_type;
11473 gfc_tbp_generic* target;
11474
11475 /* If there's already an error here, do nothing (but don't fail again). */
11476 if (p->error)
11477 return true;
11478
11479 /* Operators should always be GENERIC bindings. */
11480 gcc_assert (p->is_generic);
11481
11482 /* Look for an overridden binding. */
11483 super_type = gfc_get_derived_super_type (derived);
11484 if (super_type && super_type->f2k_derived)
11485 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
11486 op, true, NULL);
11487 else
11488 p->overridden = NULL;
11489
11490 /* Resolve general GENERIC properties using worker function. */
11491 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
11492 goto error;
11493
11494 /* Check the targets to be procedures of correct interface. */
11495 for (target = p->u.generic; target; target = target->next)
11496 {
11497 gfc_symbol* target_proc;
11498
11499 target_proc = get_checked_tb_operator_target (target, p->where);
11500 if (!target_proc)
11501 goto error;
11502
11503 if (!gfc_check_operator_interface (target_proc, op, p->where))
11504 goto error;
11505
11506 /* Add target to non-typebound operator list. */
11507 if (!target->specific->deferred && !derived->attr.use_assoc
11508 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
11509 {
11510 gfc_interface *head, *intr;
11511 if (!gfc_check_new_interface (derived->ns->op[op], target_proc, p->where))
11512 return false;
11513 head = derived->ns->op[op];
11514 intr = gfc_get_interface ();
11515 intr->sym = target_proc;
11516 intr->where = p->where;
11517 intr->next = head;
11518 derived->ns->op[op] = intr;
11519 }
11520 }
11521
11522 return true;
11523
11524 error:
11525 p->error = 1;
11526 return false;
11527 }
11528
11529
11530 /* Resolve a type-bound user operator (tree-walker callback). */
11531
11532 static gfc_symbol* resolve_bindings_derived;
11533 static bool resolve_bindings_result;
11534
11535 static bool check_uop_procedure (gfc_symbol* sym, locus where);
11536
11537 static void
11538 resolve_typebound_user_op (gfc_symtree* stree)
11539 {
11540 gfc_symbol* super_type;
11541 gfc_tbp_generic* target;
11542
11543 gcc_assert (stree && stree->n.tb);
11544
11545 if (stree->n.tb->error)
11546 return;
11547
11548 /* Operators should always be GENERIC bindings. */
11549 gcc_assert (stree->n.tb->is_generic);
11550
11551 /* Find overridden procedure, if any. */
11552 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11553 if (super_type && super_type->f2k_derived)
11554 {
11555 gfc_symtree* overridden;
11556 overridden = gfc_find_typebound_user_op (super_type, NULL,
11557 stree->name, true, NULL);
11558
11559 if (overridden && overridden->n.tb)
11560 stree->n.tb->overridden = overridden->n.tb;
11561 }
11562 else
11563 stree->n.tb->overridden = NULL;
11564
11565 /* Resolve basically using worker function. */
11566 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
11567 goto error;
11568
11569 /* Check the targets to be functions of correct interface. */
11570 for (target = stree->n.tb->u.generic; target; target = target->next)
11571 {
11572 gfc_symbol* target_proc;
11573
11574 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
11575 if (!target_proc)
11576 goto error;
11577
11578 if (!check_uop_procedure (target_proc, stree->n.tb->where))
11579 goto error;
11580 }
11581
11582 return;
11583
11584 error:
11585 resolve_bindings_result = false;
11586 stree->n.tb->error = 1;
11587 }
11588
11589
11590 /* Resolve the type-bound procedures for a derived type. */
11591
11592 static void
11593 resolve_typebound_procedure (gfc_symtree* stree)
11594 {
11595 gfc_symbol* proc;
11596 locus where;
11597 gfc_symbol* me_arg;
11598 gfc_symbol* super_type;
11599 gfc_component* comp;
11600
11601 gcc_assert (stree);
11602
11603 /* Undefined specific symbol from GENERIC target definition. */
11604 if (!stree->n.tb)
11605 return;
11606
11607 if (stree->n.tb->error)
11608 return;
11609
11610 /* If this is a GENERIC binding, use that routine. */
11611 if (stree->n.tb->is_generic)
11612 {
11613 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
11614 goto error;
11615 return;
11616 }
11617
11618 /* Get the target-procedure to check it. */
11619 gcc_assert (!stree->n.tb->is_generic);
11620 gcc_assert (stree->n.tb->u.specific);
11621 proc = stree->n.tb->u.specific->n.sym;
11622 where = stree->n.tb->where;
11623
11624 /* Default access should already be resolved from the parser. */
11625 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
11626
11627 if (stree->n.tb->deferred)
11628 {
11629 if (!check_proc_interface (proc, &where))
11630 goto error;
11631 }
11632 else
11633 {
11634 /* Check for F08:C465. */
11635 if ((!proc->attr.subroutine && !proc->attr.function)
11636 || (proc->attr.proc != PROC_MODULE
11637 && proc->attr.if_source != IFSRC_IFBODY)
11638 || proc->attr.abstract)
11639 {
11640 gfc_error ("'%s' must be a module procedure or an external procedure with"
11641 " an explicit interface at %L", proc->name, &where);
11642 goto error;
11643 }
11644 }
11645
11646 stree->n.tb->subroutine = proc->attr.subroutine;
11647 stree->n.tb->function = proc->attr.function;
11648
11649 /* Find the super-type of the current derived type. We could do this once and
11650 store in a global if speed is needed, but as long as not I believe this is
11651 more readable and clearer. */
11652 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11653
11654 /* If PASS, resolve and check arguments if not already resolved / loaded
11655 from a .mod file. */
11656 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
11657 {
11658 gfc_formal_arglist *dummy_args;
11659
11660 dummy_args = gfc_sym_get_dummy_args (proc);
11661 if (stree->n.tb->pass_arg)
11662 {
11663 gfc_formal_arglist *i;
11664
11665 /* If an explicit passing argument name is given, walk the arg-list
11666 and look for it. */
11667
11668 me_arg = NULL;
11669 stree->n.tb->pass_arg_num = 1;
11670 for (i = dummy_args; i; i = i->next)
11671 {
11672 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
11673 {
11674 me_arg = i->sym;
11675 break;
11676 }
11677 ++stree->n.tb->pass_arg_num;
11678 }
11679
11680 if (!me_arg)
11681 {
11682 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
11683 " argument '%s'",
11684 proc->name, stree->n.tb->pass_arg, &where,
11685 stree->n.tb->pass_arg);
11686 goto error;
11687 }
11688 }
11689 else
11690 {
11691 /* Otherwise, take the first one; there should in fact be at least
11692 one. */
11693 stree->n.tb->pass_arg_num = 1;
11694 if (!dummy_args)
11695 {
11696 gfc_error ("Procedure '%s' with PASS at %L must have at"
11697 " least one argument", proc->name, &where);
11698 goto error;
11699 }
11700 me_arg = dummy_args->sym;
11701 }
11702
11703 /* Now check that the argument-type matches and the passed-object
11704 dummy argument is generally fine. */
11705
11706 gcc_assert (me_arg);
11707
11708 if (me_arg->ts.type != BT_CLASS)
11709 {
11710 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11711 " at %L", proc->name, &where);
11712 goto error;
11713 }
11714
11715 if (CLASS_DATA (me_arg)->ts.u.derived
11716 != resolve_bindings_derived)
11717 {
11718 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11719 " the derived-type '%s'", me_arg->name, proc->name,
11720 me_arg->name, &where, resolve_bindings_derived->name);
11721 goto error;
11722 }
11723
11724 gcc_assert (me_arg->ts.type == BT_CLASS);
11725 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
11726 {
11727 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
11728 " scalar", proc->name, &where);
11729 goto error;
11730 }
11731 if (CLASS_DATA (me_arg)->attr.allocatable)
11732 {
11733 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11734 " be ALLOCATABLE", proc->name, &where);
11735 goto error;
11736 }
11737 if (CLASS_DATA (me_arg)->attr.class_pointer)
11738 {
11739 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11740 " be POINTER", proc->name, &where);
11741 goto error;
11742 }
11743 }
11744
11745 /* If we are extending some type, check that we don't override a procedure
11746 flagged NON_OVERRIDABLE. */
11747 stree->n.tb->overridden = NULL;
11748 if (super_type)
11749 {
11750 gfc_symtree* overridden;
11751 overridden = gfc_find_typebound_proc (super_type, NULL,
11752 stree->name, true, NULL);
11753
11754 if (overridden)
11755 {
11756 if (overridden->n.tb)
11757 stree->n.tb->overridden = overridden->n.tb;
11758
11759 if (!gfc_check_typebound_override (stree, overridden))
11760 goto error;
11761 }
11762 }
11763
11764 /* See if there's a name collision with a component directly in this type. */
11765 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
11766 if (!strcmp (comp->name, stree->name))
11767 {
11768 gfc_error ("Procedure '%s' at %L has the same name as a component of"
11769 " '%s'",
11770 stree->name, &where, resolve_bindings_derived->name);
11771 goto error;
11772 }
11773
11774 /* Try to find a name collision with an inherited component. */
11775 if (super_type && gfc_find_component (super_type, stree->name, true, true))
11776 {
11777 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
11778 " component of '%s'",
11779 stree->name, &where, resolve_bindings_derived->name);
11780 goto error;
11781 }
11782
11783 stree->n.tb->error = 0;
11784 return;
11785
11786 error:
11787 resolve_bindings_result = false;
11788 stree->n.tb->error = 1;
11789 }
11790
11791
11792 static bool
11793 resolve_typebound_procedures (gfc_symbol* derived)
11794 {
11795 int op;
11796 gfc_symbol* super_type;
11797
11798 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
11799 return true;
11800
11801 super_type = gfc_get_derived_super_type (derived);
11802 if (super_type)
11803 resolve_symbol (super_type);
11804
11805 resolve_bindings_derived = derived;
11806 resolve_bindings_result = true;
11807
11808 /* Make sure the vtab has been generated. */
11809 gfc_find_derived_vtab (derived);
11810
11811 if (derived->f2k_derived->tb_sym_root)
11812 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
11813 &resolve_typebound_procedure);
11814
11815 if (derived->f2k_derived->tb_uop_root)
11816 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
11817 &resolve_typebound_user_op);
11818
11819 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
11820 {
11821 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
11822 if (p && !resolve_typebound_intrinsic_op (derived,
11823 (gfc_intrinsic_op)op, p))
11824 resolve_bindings_result = false;
11825 }
11826
11827 return resolve_bindings_result;
11828 }
11829
11830
11831 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
11832 to give all identical derived types the same backend_decl. */
11833 static void
11834 add_dt_to_dt_list (gfc_symbol *derived)
11835 {
11836 gfc_dt_list *dt_list;
11837
11838 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
11839 if (derived == dt_list->derived)
11840 return;
11841
11842 dt_list = gfc_get_dt_list ();
11843 dt_list->next = gfc_derived_types;
11844 dt_list->derived = derived;
11845 gfc_derived_types = dt_list;
11846 }
11847
11848
11849 /* Ensure that a derived-type is really not abstract, meaning that every
11850 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
11851
11852 static bool
11853 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
11854 {
11855 if (!st)
11856 return true;
11857
11858 if (!ensure_not_abstract_walker (sub, st->left))
11859 return false;
11860 if (!ensure_not_abstract_walker (sub, st->right))
11861 return false;
11862
11863 if (st->n.tb && st->n.tb->deferred)
11864 {
11865 gfc_symtree* overriding;
11866 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
11867 if (!overriding)
11868 return false;
11869 gcc_assert (overriding->n.tb);
11870 if (overriding->n.tb->deferred)
11871 {
11872 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
11873 " '%s' is DEFERRED and not overridden",
11874 sub->name, &sub->declared_at, st->name);
11875 return false;
11876 }
11877 }
11878
11879 return true;
11880 }
11881
11882 static bool
11883 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
11884 {
11885 /* The algorithm used here is to recursively travel up the ancestry of sub
11886 and for each ancestor-type, check all bindings. If any of them is
11887 DEFERRED, look it up starting from sub and see if the found (overriding)
11888 binding is not DEFERRED.
11889 This is not the most efficient way to do this, but it should be ok and is
11890 clearer than something sophisticated. */
11891
11892 gcc_assert (ancestor && !sub->attr.abstract);
11893
11894 if (!ancestor->attr.abstract)
11895 return true;
11896
11897 /* Walk bindings of this ancestor. */
11898 if (ancestor->f2k_derived)
11899 {
11900 bool t;
11901 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
11902 if (!t)
11903 return false;
11904 }
11905
11906 /* Find next ancestor type and recurse on it. */
11907 ancestor = gfc_get_derived_super_type (ancestor);
11908 if (ancestor)
11909 return ensure_not_abstract (sub, ancestor);
11910
11911 return true;
11912 }
11913
11914
11915 /* This check for typebound defined assignments is done recursively
11916 since the order in which derived types are resolved is not always in
11917 order of the declarations. */
11918
11919 static void
11920 check_defined_assignments (gfc_symbol *derived)
11921 {
11922 gfc_component *c;
11923
11924 for (c = derived->components; c; c = c->next)
11925 {
11926 if (c->ts.type != BT_DERIVED
11927 || c->attr.pointer
11928 || c->attr.allocatable
11929 || c->attr.proc_pointer_comp
11930 || c->attr.class_pointer
11931 || c->attr.proc_pointer)
11932 continue;
11933
11934 if (c->ts.u.derived->attr.defined_assign_comp
11935 || (c->ts.u.derived->f2k_derived
11936 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
11937 {
11938 derived->attr.defined_assign_comp = 1;
11939 return;
11940 }
11941
11942 check_defined_assignments (c->ts.u.derived);
11943 if (c->ts.u.derived->attr.defined_assign_comp)
11944 {
11945 derived->attr.defined_assign_comp = 1;
11946 return;
11947 }
11948 }
11949 }
11950
11951
11952 /* Resolve the components of a derived type. This does not have to wait until
11953 resolution stage, but can be done as soon as the dt declaration has been
11954 parsed. */
11955
11956 static bool
11957 resolve_fl_derived0 (gfc_symbol *sym)
11958 {
11959 gfc_symbol* super_type;
11960 gfc_component *c;
11961
11962 if (sym->attr.unlimited_polymorphic)
11963 return true;
11964
11965 super_type = gfc_get_derived_super_type (sym);
11966
11967 /* F2008, C432. */
11968 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
11969 {
11970 gfc_error ("As extending type '%s' at %L has a coarray component, "
11971 "parent type '%s' shall also have one", sym->name,
11972 &sym->declared_at, super_type->name);
11973 return false;
11974 }
11975
11976 /* Ensure the extended type gets resolved before we do. */
11977 if (super_type && !resolve_fl_derived0 (super_type))
11978 return false;
11979
11980 /* An ABSTRACT type must be extensible. */
11981 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
11982 {
11983 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
11984 sym->name, &sym->declared_at);
11985 return false;
11986 }
11987
11988 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
11989 : sym->components;
11990
11991 for ( ; c != NULL; c = c->next)
11992 {
11993 if (c->attr.artificial)
11994 continue;
11995
11996 /* See PRs 51550, 47545, 48654, 49050, 51075 - and 45170. */
11997 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function)
11998 {
11999 gfc_error ("Deferred-length character component '%s' at %L is not "
12000 "yet supported", c->name, &c->loc);
12001 return false;
12002 }
12003
12004 /* F2008, C442. */
12005 if ((!sym->attr.is_class || c != sym->components)
12006 && c->attr.codimension
12007 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
12008 {
12009 gfc_error ("Coarray component '%s' at %L must be allocatable with "
12010 "deferred shape", c->name, &c->loc);
12011 return false;
12012 }
12013
12014 /* F2008, C443. */
12015 if (c->attr.codimension && c->ts.type == BT_DERIVED
12016 && c->ts.u.derived->ts.is_iso_c)
12017 {
12018 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12019 "shall not be a coarray", c->name, &c->loc);
12020 return false;
12021 }
12022
12023 /* F2008, C444. */
12024 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
12025 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
12026 || c->attr.allocatable))
12027 {
12028 gfc_error ("Component '%s' at %L with coarray component "
12029 "shall be a nonpointer, nonallocatable scalar",
12030 c->name, &c->loc);
12031 return false;
12032 }
12033
12034 /* F2008, C448. */
12035 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
12036 {
12037 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
12038 "is not an array pointer", c->name, &c->loc);
12039 return false;
12040 }
12041
12042 if (c->attr.proc_pointer && c->ts.interface)
12043 {
12044 gfc_symbol *ifc = c->ts.interface;
12045
12046 if (!sym->attr.vtype
12047 && !check_proc_interface (ifc, &c->loc))
12048 return false;
12049
12050 if (ifc->attr.if_source || ifc->attr.intrinsic)
12051 {
12052 /* Resolve interface and copy attributes. */
12053 if (ifc->formal && !ifc->formal_ns)
12054 resolve_symbol (ifc);
12055 if (ifc->attr.intrinsic)
12056 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
12057
12058 if (ifc->result)
12059 {
12060 c->ts = ifc->result->ts;
12061 c->attr.allocatable = ifc->result->attr.allocatable;
12062 c->attr.pointer = ifc->result->attr.pointer;
12063 c->attr.dimension = ifc->result->attr.dimension;
12064 c->as = gfc_copy_array_spec (ifc->result->as);
12065 c->attr.class_ok = ifc->result->attr.class_ok;
12066 }
12067 else
12068 {
12069 c->ts = ifc->ts;
12070 c->attr.allocatable = ifc->attr.allocatable;
12071 c->attr.pointer = ifc->attr.pointer;
12072 c->attr.dimension = ifc->attr.dimension;
12073 c->as = gfc_copy_array_spec (ifc->as);
12074 c->attr.class_ok = ifc->attr.class_ok;
12075 }
12076 c->ts.interface = ifc;
12077 c->attr.function = ifc->attr.function;
12078 c->attr.subroutine = ifc->attr.subroutine;
12079
12080 c->attr.pure = ifc->attr.pure;
12081 c->attr.elemental = ifc->attr.elemental;
12082 c->attr.recursive = ifc->attr.recursive;
12083 c->attr.always_explicit = ifc->attr.always_explicit;
12084 c->attr.ext_attr |= ifc->attr.ext_attr;
12085 /* Copy char length. */
12086 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
12087 {
12088 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
12089 if (cl->length && !cl->resolved
12090 && !gfc_resolve_expr (cl->length))
12091 return false;
12092 c->ts.u.cl = cl;
12093 }
12094 }
12095 }
12096 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
12097 {
12098 /* Since PPCs are not implicitly typed, a PPC without an explicit
12099 interface must be a subroutine. */
12100 gfc_add_subroutine (&c->attr, c->name, &c->loc);
12101 }
12102
12103 /* Procedure pointer components: Check PASS arg. */
12104 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
12105 && !sym->attr.vtype)
12106 {
12107 gfc_symbol* me_arg;
12108
12109 if (c->tb->pass_arg)
12110 {
12111 gfc_formal_arglist* i;
12112
12113 /* If an explicit passing argument name is given, walk the arg-list
12114 and look for it. */
12115
12116 me_arg = NULL;
12117 c->tb->pass_arg_num = 1;
12118 for (i = c->ts.interface->formal; i; i = i->next)
12119 {
12120 if (!strcmp (i->sym->name, c->tb->pass_arg))
12121 {
12122 me_arg = i->sym;
12123 break;
12124 }
12125 c->tb->pass_arg_num++;
12126 }
12127
12128 if (!me_arg)
12129 {
12130 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
12131 "at %L has no argument '%s'", c->name,
12132 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
12133 c->tb->error = 1;
12134 return false;
12135 }
12136 }
12137 else
12138 {
12139 /* Otherwise, take the first one; there should in fact be at least
12140 one. */
12141 c->tb->pass_arg_num = 1;
12142 if (!c->ts.interface->formal)
12143 {
12144 gfc_error ("Procedure pointer component '%s' with PASS at %L "
12145 "must have at least one argument",
12146 c->name, &c->loc);
12147 c->tb->error = 1;
12148 return false;
12149 }
12150 me_arg = c->ts.interface->formal->sym;
12151 }
12152
12153 /* Now check that the argument-type matches. */
12154 gcc_assert (me_arg);
12155 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
12156 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
12157 || (me_arg->ts.type == BT_CLASS
12158 && CLASS_DATA (me_arg)->ts.u.derived != sym))
12159 {
12160 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
12161 " the derived type '%s'", me_arg->name, c->name,
12162 me_arg->name, &c->loc, sym->name);
12163 c->tb->error = 1;
12164 return false;
12165 }
12166
12167 /* Check for C453. */
12168 if (me_arg->attr.dimension)
12169 {
12170 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12171 "must be scalar", me_arg->name, c->name, me_arg->name,
12172 &c->loc);
12173 c->tb->error = 1;
12174 return false;
12175 }
12176
12177 if (me_arg->attr.pointer)
12178 {
12179 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12180 "may not have the POINTER attribute", me_arg->name,
12181 c->name, me_arg->name, &c->loc);
12182 c->tb->error = 1;
12183 return false;
12184 }
12185
12186 if (me_arg->attr.allocatable)
12187 {
12188 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12189 "may not be ALLOCATABLE", me_arg->name, c->name,
12190 me_arg->name, &c->loc);
12191 c->tb->error = 1;
12192 return false;
12193 }
12194
12195 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
12196 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
12197 " at %L", c->name, &c->loc);
12198
12199 }
12200
12201 /* Check type-spec if this is not the parent-type component. */
12202 if (((sym->attr.is_class
12203 && (!sym->components->ts.u.derived->attr.extension
12204 || c != sym->components->ts.u.derived->components))
12205 || (!sym->attr.is_class
12206 && (!sym->attr.extension || c != sym->components)))
12207 && !sym->attr.vtype
12208 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
12209 return false;
12210
12211 /* If this type is an extension, set the accessibility of the parent
12212 component. */
12213 if (super_type
12214 && ((sym->attr.is_class
12215 && c == sym->components->ts.u.derived->components)
12216 || (!sym->attr.is_class && c == sym->components))
12217 && strcmp (super_type->name, c->name) == 0)
12218 c->attr.access = super_type->attr.access;
12219
12220 /* If this type is an extension, see if this component has the same name
12221 as an inherited type-bound procedure. */
12222 if (super_type && !sym->attr.is_class
12223 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
12224 {
12225 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
12226 " inherited type-bound procedure",
12227 c->name, sym->name, &c->loc);
12228 return false;
12229 }
12230
12231 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
12232 && !c->ts.deferred)
12233 {
12234 if (c->ts.u.cl->length == NULL
12235 || (!resolve_charlen(c->ts.u.cl))
12236 || !gfc_is_constant_expr (c->ts.u.cl->length))
12237 {
12238 gfc_error ("Character length of component '%s' needs to "
12239 "be a constant specification expression at %L",
12240 c->name,
12241 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
12242 return false;
12243 }
12244 }
12245
12246 if (c->ts.type == BT_CHARACTER && c->ts.deferred
12247 && !c->attr.pointer && !c->attr.allocatable)
12248 {
12249 gfc_error ("Character component '%s' of '%s' at %L with deferred "
12250 "length must be a POINTER or ALLOCATABLE",
12251 c->name, sym->name, &c->loc);
12252 return false;
12253 }
12254
12255 if (c->ts.type == BT_DERIVED
12256 && sym->component_access != ACCESS_PRIVATE
12257 && gfc_check_symbol_access (sym)
12258 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
12259 && !c->ts.u.derived->attr.use_assoc
12260 && !gfc_check_symbol_access (c->ts.u.derived)
12261 && !gfc_notify_std (GFC_STD_F2003, "the component '%s' is a "
12262 "PRIVATE type and cannot be a component of "
12263 "'%s', which is PUBLIC at %L", c->name,
12264 sym->name, &sym->declared_at))
12265 return false;
12266
12267 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
12268 {
12269 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
12270 "type %s", c->name, &c->loc, sym->name);
12271 return false;
12272 }
12273
12274 if (sym->attr.sequence)
12275 {
12276 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
12277 {
12278 gfc_error ("Component %s of SEQUENCE type declared at %L does "
12279 "not have the SEQUENCE attribute",
12280 c->ts.u.derived->name, &sym->declared_at);
12281 return false;
12282 }
12283 }
12284
12285 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
12286 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
12287 else if (c->ts.type == BT_CLASS && c->attr.class_ok
12288 && CLASS_DATA (c)->ts.u.derived->attr.generic)
12289 CLASS_DATA (c)->ts.u.derived
12290 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
12291
12292 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
12293 && c->attr.pointer && c->ts.u.derived->components == NULL
12294 && !c->ts.u.derived->attr.zero_comp)
12295 {
12296 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12297 "that has not been declared", c->name, sym->name,
12298 &c->loc);
12299 return false;
12300 }
12301
12302 if (c->ts.type == BT_CLASS && c->attr.class_ok
12303 && CLASS_DATA (c)->attr.class_pointer
12304 && CLASS_DATA (c)->ts.u.derived->components == NULL
12305 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
12306 && !UNLIMITED_POLY (c))
12307 {
12308 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12309 "that has not been declared", c->name, sym->name,
12310 &c->loc);
12311 return false;
12312 }
12313
12314 /* C437. */
12315 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
12316 && (!c->attr.class_ok
12317 || !(CLASS_DATA (c)->attr.class_pointer
12318 || CLASS_DATA (c)->attr.allocatable)))
12319 {
12320 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
12321 "or pointer", c->name, &c->loc);
12322 /* Prevent a recurrence of the error. */
12323 c->ts.type = BT_UNKNOWN;
12324 return false;
12325 }
12326
12327 /* Ensure that all the derived type components are put on the
12328 derived type list; even in formal namespaces, where derived type
12329 pointer components might not have been declared. */
12330 if (c->ts.type == BT_DERIVED
12331 && c->ts.u.derived
12332 && c->ts.u.derived->components
12333 && c->attr.pointer
12334 && sym != c->ts.u.derived)
12335 add_dt_to_dt_list (c->ts.u.derived);
12336
12337 if (!gfc_resolve_array_spec (c->as,
12338 !(c->attr.pointer || c->attr.proc_pointer
12339 || c->attr.allocatable)))
12340 return false;
12341
12342 if (c->initializer && !sym->attr.vtype
12343 && !gfc_check_assign_symbol (sym, c, c->initializer))
12344 return false;
12345 }
12346
12347 check_defined_assignments (sym);
12348
12349 if (!sym->attr.defined_assign_comp && super_type)
12350 sym->attr.defined_assign_comp
12351 = super_type->attr.defined_assign_comp;
12352
12353 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
12354 all DEFERRED bindings are overridden. */
12355 if (super_type && super_type->attr.abstract && !sym->attr.abstract
12356 && !sym->attr.is_class
12357 && !ensure_not_abstract (sym, super_type))
12358 return false;
12359
12360 /* Add derived type to the derived type list. */
12361 add_dt_to_dt_list (sym);
12362
12363 /* Check if the type is finalizable. This is done in order to ensure that the
12364 finalization wrapper is generated early enough. */
12365 gfc_is_finalizable (sym, NULL);
12366
12367 return true;
12368 }
12369
12370
12371 /* The following procedure does the full resolution of a derived type,
12372 including resolution of all type-bound procedures (if present). In contrast
12373 to 'resolve_fl_derived0' this can only be done after the module has been
12374 parsed completely. */
12375
12376 static bool
12377 resolve_fl_derived (gfc_symbol *sym)
12378 {
12379 gfc_symbol *gen_dt = NULL;
12380
12381 if (sym->attr.unlimited_polymorphic)
12382 return true;
12383
12384 if (!sym->attr.is_class)
12385 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
12386 if (gen_dt && gen_dt->generic && gen_dt->generic->next
12387 && (!gen_dt->generic->sym->attr.use_assoc
12388 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
12389 && !gfc_notify_std (GFC_STD_F2003, "Generic name '%s' of function "
12390 "'%s' at %L being the same name as derived "
12391 "type at %L", sym->name,
12392 gen_dt->generic->sym == sym
12393 ? gen_dt->generic->next->sym->name
12394 : gen_dt->generic->sym->name,
12395 gen_dt->generic->sym == sym
12396 ? &gen_dt->generic->next->sym->declared_at
12397 : &gen_dt->generic->sym->declared_at,
12398 &sym->declared_at))
12399 return false;
12400
12401 /* Resolve the finalizer procedures. */
12402 if (!gfc_resolve_finalizers (sym))
12403 return false;
12404
12405 if (sym->attr.is_class && sym->ts.u.derived == NULL)
12406 {
12407 /* Fix up incomplete CLASS symbols. */
12408 gfc_component *data = gfc_find_component (sym, "_data", true, true);
12409 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
12410
12411 /* Nothing more to do for unlimited polymorphic entities. */
12412 if (data->ts.u.derived->attr.unlimited_polymorphic)
12413 return true;
12414 else if (vptr->ts.u.derived == NULL)
12415 {
12416 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
12417 gcc_assert (vtab);
12418 vptr->ts.u.derived = vtab->ts.u.derived;
12419 }
12420 }
12421
12422 if (!resolve_fl_derived0 (sym))
12423 return false;
12424
12425 /* Resolve the type-bound procedures. */
12426 if (!resolve_typebound_procedures (sym))
12427 return false;
12428
12429 return true;
12430 }
12431
12432
12433 static bool
12434 resolve_fl_namelist (gfc_symbol *sym)
12435 {
12436 gfc_namelist *nl;
12437 gfc_symbol *nlsym;
12438
12439 for (nl = sym->namelist; nl; nl = nl->next)
12440 {
12441 /* Check again, the check in match only works if NAMELIST comes
12442 after the decl. */
12443 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
12444 {
12445 gfc_error ("Assumed size array '%s' in namelist '%s' at %L is not "
12446 "allowed", nl->sym->name, sym->name, &sym->declared_at);
12447 return false;
12448 }
12449
12450 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
12451 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12452 "with assumed shape in namelist '%s' at %L",
12453 nl->sym->name, sym->name, &sym->declared_at))
12454 return false;
12455
12456 if (is_non_constant_shape_array (nl->sym)
12457 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12458 "with nonconstant shape in namelist '%s' at %L",
12459 nl->sym->name, sym->name, &sym->declared_at))
12460 return false;
12461
12462 if (nl->sym->ts.type == BT_CHARACTER
12463 && (nl->sym->ts.u.cl->length == NULL
12464 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
12465 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' with "
12466 "nonconstant character length in "
12467 "namelist '%s' at %L", nl->sym->name,
12468 sym->name, &sym->declared_at))
12469 return false;
12470
12471 /* FIXME: Once UDDTIO is implemented, the following can be
12472 removed. */
12473 if (nl->sym->ts.type == BT_CLASS)
12474 {
12475 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L is "
12476 "polymorphic and requires a defined input/output "
12477 "procedure", nl->sym->name, sym->name, &sym->declared_at);
12478 return false;
12479 }
12480
12481 if (nl->sym->ts.type == BT_DERIVED
12482 && (nl->sym->ts.u.derived->attr.alloc_comp
12483 || nl->sym->ts.u.derived->attr.pointer_comp))
12484 {
12485 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' in "
12486 "namelist '%s' at %L with ALLOCATABLE "
12487 "or POINTER components", nl->sym->name,
12488 sym->name, &sym->declared_at))
12489 return false;
12490
12491 /* FIXME: Once UDDTIO is implemented, the following can be
12492 removed. */
12493 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L has "
12494 "ALLOCATABLE or POINTER components and thus requires "
12495 "a defined input/output procedure", nl->sym->name,
12496 sym->name, &sym->declared_at);
12497 return false;
12498 }
12499 }
12500
12501 /* Reject PRIVATE objects in a PUBLIC namelist. */
12502 if (gfc_check_symbol_access (sym))
12503 {
12504 for (nl = sym->namelist; nl; nl = nl->next)
12505 {
12506 if (!nl->sym->attr.use_assoc
12507 && !is_sym_host_assoc (nl->sym, sym->ns)
12508 && !gfc_check_symbol_access (nl->sym))
12509 {
12510 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
12511 "cannot be member of PUBLIC namelist '%s' at %L",
12512 nl->sym->name, sym->name, &sym->declared_at);
12513 return false;
12514 }
12515
12516 /* Types with private components that came here by USE-association. */
12517 if (nl->sym->ts.type == BT_DERIVED
12518 && derived_inaccessible (nl->sym->ts.u.derived))
12519 {
12520 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
12521 "components and cannot be member of namelist '%s' at %L",
12522 nl->sym->name, sym->name, &sym->declared_at);
12523 return false;
12524 }
12525
12526 /* Types with private components that are defined in the same module. */
12527 if (nl->sym->ts.type == BT_DERIVED
12528 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
12529 && nl->sym->ts.u.derived->attr.private_comp)
12530 {
12531 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
12532 "cannot be a member of PUBLIC namelist '%s' at %L",
12533 nl->sym->name, sym->name, &sym->declared_at);
12534 return false;
12535 }
12536 }
12537 }
12538
12539
12540 /* 14.1.2 A module or internal procedure represent local entities
12541 of the same type as a namelist member and so are not allowed. */
12542 for (nl = sym->namelist; nl; nl = nl->next)
12543 {
12544 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
12545 continue;
12546
12547 if (nl->sym->attr.function && nl->sym == nl->sym->result)
12548 if ((nl->sym == sym->ns->proc_name)
12549 ||
12550 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
12551 continue;
12552
12553 nlsym = NULL;
12554 if (nl->sym->name)
12555 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
12556 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
12557 {
12558 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
12559 "attribute in '%s' at %L", nlsym->name,
12560 &sym->declared_at);
12561 return false;
12562 }
12563 }
12564
12565 return true;
12566 }
12567
12568
12569 static bool
12570 resolve_fl_parameter (gfc_symbol *sym)
12571 {
12572 /* A parameter array's shape needs to be constant. */
12573 if (sym->as != NULL
12574 && (sym->as->type == AS_DEFERRED
12575 || is_non_constant_shape_array (sym)))
12576 {
12577 gfc_error ("Parameter array '%s' at %L cannot be automatic "
12578 "or of deferred shape", sym->name, &sym->declared_at);
12579 return false;
12580 }
12581
12582 /* Make sure a parameter that has been implicitly typed still
12583 matches the implicit type, since PARAMETER statements can precede
12584 IMPLICIT statements. */
12585 if (sym->attr.implicit_type
12586 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
12587 sym->ns)))
12588 {
12589 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
12590 "later IMPLICIT type", sym->name, &sym->declared_at);
12591 return false;
12592 }
12593
12594 /* Make sure the types of derived parameters are consistent. This
12595 type checking is deferred until resolution because the type may
12596 refer to a derived type from the host. */
12597 if (sym->ts.type == BT_DERIVED
12598 && !gfc_compare_types (&sym->ts, &sym->value->ts))
12599 {
12600 gfc_error ("Incompatible derived type in PARAMETER at %L",
12601 &sym->value->where);
12602 return false;
12603 }
12604 return true;
12605 }
12606
12607
12608 /* Do anything necessary to resolve a symbol. Right now, we just
12609 assume that an otherwise unknown symbol is a variable. This sort
12610 of thing commonly happens for symbols in module. */
12611
12612 static void
12613 resolve_symbol (gfc_symbol *sym)
12614 {
12615 int check_constant, mp_flag;
12616 gfc_symtree *symtree;
12617 gfc_symtree *this_symtree;
12618 gfc_namespace *ns;
12619 gfc_component *c;
12620 symbol_attribute class_attr;
12621 gfc_array_spec *as;
12622 bool saved_specification_expr;
12623
12624 if (sym->resolved)
12625 return;
12626 sym->resolved = 1;
12627
12628 if (sym->attr.artificial)
12629 return;
12630
12631 if (sym->attr.unlimited_polymorphic)
12632 return;
12633
12634 if (sym->attr.flavor == FL_UNKNOWN
12635 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
12636 && !sym->attr.generic && !sym->attr.external
12637 && sym->attr.if_source == IFSRC_UNKNOWN))
12638 {
12639
12640 /* If we find that a flavorless symbol is an interface in one of the
12641 parent namespaces, find its symtree in this namespace, free the
12642 symbol and set the symtree to point to the interface symbol. */
12643 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
12644 {
12645 symtree = gfc_find_symtree (ns->sym_root, sym->name);
12646 if (symtree && (symtree->n.sym->generic ||
12647 (symtree->n.sym->attr.flavor == FL_PROCEDURE
12648 && sym->ns->construct_entities)))
12649 {
12650 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
12651 sym->name);
12652 gfc_release_symbol (sym);
12653 symtree->n.sym->refs++;
12654 this_symtree->n.sym = symtree->n.sym;
12655 return;
12656 }
12657 }
12658
12659 /* Otherwise give it a flavor according to such attributes as
12660 it has. */
12661 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
12662 && sym->attr.intrinsic == 0)
12663 sym->attr.flavor = FL_VARIABLE;
12664 else if (sym->attr.flavor == FL_UNKNOWN)
12665 {
12666 sym->attr.flavor = FL_PROCEDURE;
12667 if (sym->attr.dimension)
12668 sym->attr.function = 1;
12669 }
12670 }
12671
12672 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
12673 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
12674
12675 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
12676 && !resolve_procedure_interface (sym))
12677 return;
12678
12679 if (sym->attr.is_protected && !sym->attr.proc_pointer
12680 && (sym->attr.procedure || sym->attr.external))
12681 {
12682 if (sym->attr.external)
12683 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
12684 "at %L", &sym->declared_at);
12685 else
12686 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
12687 "at %L", &sym->declared_at);
12688
12689 return;
12690 }
12691
12692 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
12693 return;
12694
12695 /* Symbols that are module procedures with results (functions) have
12696 the types and array specification copied for type checking in
12697 procedures that call them, as well as for saving to a module
12698 file. These symbols can't stand the scrutiny that their results
12699 can. */
12700 mp_flag = (sym->result != NULL && sym->result != sym);
12701
12702 /* Make sure that the intrinsic is consistent with its internal
12703 representation. This needs to be done before assigning a default
12704 type to avoid spurious warnings. */
12705 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
12706 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
12707 return;
12708
12709 /* Resolve associate names. */
12710 if (sym->assoc)
12711 resolve_assoc_var (sym, true);
12712
12713 /* Assign default type to symbols that need one and don't have one. */
12714 if (sym->ts.type == BT_UNKNOWN)
12715 {
12716 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
12717 {
12718 gfc_set_default_type (sym, 1, NULL);
12719 }
12720
12721 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
12722 && !sym->attr.function && !sym->attr.subroutine
12723 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
12724 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
12725
12726 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12727 {
12728 /* The specific case of an external procedure should emit an error
12729 in the case that there is no implicit type. */
12730 if (!mp_flag)
12731 gfc_set_default_type (sym, sym->attr.external, NULL);
12732 else
12733 {
12734 /* Result may be in another namespace. */
12735 resolve_symbol (sym->result);
12736
12737 if (!sym->result->attr.proc_pointer)
12738 {
12739 sym->ts = sym->result->ts;
12740 sym->as = gfc_copy_array_spec (sym->result->as);
12741 sym->attr.dimension = sym->result->attr.dimension;
12742 sym->attr.pointer = sym->result->attr.pointer;
12743 sym->attr.allocatable = sym->result->attr.allocatable;
12744 sym->attr.contiguous = sym->result->attr.contiguous;
12745 }
12746 }
12747 }
12748 }
12749 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12750 {
12751 bool saved_specification_expr = specification_expr;
12752 specification_expr = true;
12753 gfc_resolve_array_spec (sym->result->as, false);
12754 specification_expr = saved_specification_expr;
12755 }
12756
12757 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
12758 {
12759 as = CLASS_DATA (sym)->as;
12760 class_attr = CLASS_DATA (sym)->attr;
12761 class_attr.pointer = class_attr.class_pointer;
12762 }
12763 else
12764 {
12765 class_attr = sym->attr;
12766 as = sym->as;
12767 }
12768
12769 /* F2008, C530. */
12770 if (sym->attr.contiguous
12771 && (!class_attr.dimension
12772 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
12773 && !class_attr.pointer)))
12774 {
12775 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
12776 "array pointer or an assumed-shape or assumed-rank array",
12777 sym->name, &sym->declared_at);
12778 return;
12779 }
12780
12781 /* Assumed size arrays and assumed shape arrays must be dummy
12782 arguments. Array-spec's of implied-shape should have been resolved to
12783 AS_EXPLICIT already. */
12784
12785 if (as)
12786 {
12787 gcc_assert (as->type != AS_IMPLIED_SHAPE);
12788 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
12789 || as->type == AS_ASSUMED_SHAPE)
12790 && !sym->attr.dummy && !sym->attr.select_type_temporary)
12791 {
12792 if (as->type == AS_ASSUMED_SIZE)
12793 gfc_error ("Assumed size array at %L must be a dummy argument",
12794 &sym->declared_at);
12795 else
12796 gfc_error ("Assumed shape array at %L must be a dummy argument",
12797 &sym->declared_at);
12798 return;
12799 }
12800 /* TS 29113, C535a. */
12801 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
12802 && !sym->attr.select_type_temporary)
12803 {
12804 gfc_error ("Assumed-rank array at %L must be a dummy argument",
12805 &sym->declared_at);
12806 return;
12807 }
12808 if (as->type == AS_ASSUMED_RANK
12809 && (sym->attr.codimension || sym->attr.value))
12810 {
12811 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
12812 "CODIMENSION attribute", &sym->declared_at);
12813 return;
12814 }
12815 }
12816
12817 /* Make sure symbols with known intent or optional are really dummy
12818 variable. Because of ENTRY statement, this has to be deferred
12819 until resolution time. */
12820
12821 if (!sym->attr.dummy
12822 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
12823 {
12824 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
12825 return;
12826 }
12827
12828 if (sym->attr.value && !sym->attr.dummy)
12829 {
12830 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
12831 "it is not a dummy argument", sym->name, &sym->declared_at);
12832 return;
12833 }
12834
12835 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
12836 {
12837 gfc_charlen *cl = sym->ts.u.cl;
12838 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
12839 {
12840 gfc_error ("Character dummy variable '%s' at %L with VALUE "
12841 "attribute must have constant length",
12842 sym->name, &sym->declared_at);
12843 return;
12844 }
12845
12846 if (sym->ts.is_c_interop
12847 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
12848 {
12849 gfc_error ("C interoperable character dummy variable '%s' at %L "
12850 "with VALUE attribute must have length one",
12851 sym->name, &sym->declared_at);
12852 return;
12853 }
12854 }
12855
12856 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
12857 && sym->ts.u.derived->attr.generic)
12858 {
12859 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
12860 if (!sym->ts.u.derived)
12861 {
12862 gfc_error ("The derived type '%s' at %L is of type '%s', "
12863 "which has not been defined", sym->name,
12864 &sym->declared_at, sym->ts.u.derived->name);
12865 sym->ts.type = BT_UNKNOWN;
12866 return;
12867 }
12868 }
12869
12870 /* Use the same constraints as TYPE(*), except for the type check
12871 and that only scalars and assumed-size arrays are permitted. */
12872 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
12873 {
12874 if (!sym->attr.dummy)
12875 {
12876 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
12877 "a dummy argument", sym->name, &sym->declared_at);
12878 return;
12879 }
12880
12881 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
12882 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
12883 && sym->ts.type != BT_COMPLEX)
12884 {
12885 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
12886 "of type TYPE(*) or of an numeric intrinsic type",
12887 sym->name, &sym->declared_at);
12888 return;
12889 }
12890
12891 if (sym->attr.allocatable || sym->attr.codimension
12892 || sym->attr.pointer || sym->attr.value)
12893 {
12894 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
12895 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
12896 "attribute", sym->name, &sym->declared_at);
12897 return;
12898 }
12899
12900 if (sym->attr.intent == INTENT_OUT)
12901 {
12902 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
12903 "have the INTENT(OUT) attribute",
12904 sym->name, &sym->declared_at);
12905 return;
12906 }
12907 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
12908 {
12909 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
12910 "either be a scalar or an assumed-size array",
12911 sym->name, &sym->declared_at);
12912 return;
12913 }
12914
12915 /* Set the type to TYPE(*) and add a dimension(*) to ensure
12916 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
12917 packing. */
12918 sym->ts.type = BT_ASSUMED;
12919 sym->as = gfc_get_array_spec ();
12920 sym->as->type = AS_ASSUMED_SIZE;
12921 sym->as->rank = 1;
12922 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
12923 }
12924 else if (sym->ts.type == BT_ASSUMED)
12925 {
12926 /* TS 29113, C407a. */
12927 if (!sym->attr.dummy)
12928 {
12929 gfc_error ("Assumed type of variable %s at %L is only permitted "
12930 "for dummy variables", sym->name, &sym->declared_at);
12931 return;
12932 }
12933 if (sym->attr.allocatable || sym->attr.codimension
12934 || sym->attr.pointer || sym->attr.value)
12935 {
12936 gfc_error ("Assumed-type variable %s at %L may not have the "
12937 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
12938 sym->name, &sym->declared_at);
12939 return;
12940 }
12941 if (sym->attr.intent == INTENT_OUT)
12942 {
12943 gfc_error ("Assumed-type variable %s at %L may not have the "
12944 "INTENT(OUT) attribute",
12945 sym->name, &sym->declared_at);
12946 return;
12947 }
12948 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
12949 {
12950 gfc_error ("Assumed-type variable %s at %L shall not be an "
12951 "explicit-shape array", sym->name, &sym->declared_at);
12952 return;
12953 }
12954 }
12955
12956 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
12957 do this for something that was implicitly typed because that is handled
12958 in gfc_set_default_type. Handle dummy arguments and procedure
12959 definitions separately. Also, anything that is use associated is not
12960 handled here but instead is handled in the module it is declared in.
12961 Finally, derived type definitions are allowed to be BIND(C) since that
12962 only implies that they're interoperable, and they are checked fully for
12963 interoperability when a variable is declared of that type. */
12964 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
12965 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
12966 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
12967 {
12968 bool t = true;
12969
12970 /* First, make sure the variable is declared at the
12971 module-level scope (J3/04-007, Section 15.3). */
12972 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
12973 sym->attr.in_common == 0)
12974 {
12975 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
12976 "is neither a COMMON block nor declared at the "
12977 "module level scope", sym->name, &(sym->declared_at));
12978 t = false;
12979 }
12980 else if (sym->common_head != NULL)
12981 {
12982 t = verify_com_block_vars_c_interop (sym->common_head);
12983 }
12984 else
12985 {
12986 /* If type() declaration, we need to verify that the components
12987 of the given type are all C interoperable, etc. */
12988 if (sym->ts.type == BT_DERIVED &&
12989 sym->ts.u.derived->attr.is_c_interop != 1)
12990 {
12991 /* Make sure the user marked the derived type as BIND(C). If
12992 not, call the verify routine. This could print an error
12993 for the derived type more than once if multiple variables
12994 of that type are declared. */
12995 if (sym->ts.u.derived->attr.is_bind_c != 1)
12996 verify_bind_c_derived_type (sym->ts.u.derived);
12997 t = false;
12998 }
12999
13000 /* Verify the variable itself as C interoperable if it
13001 is BIND(C). It is not possible for this to succeed if
13002 the verify_bind_c_derived_type failed, so don't have to handle
13003 any error returned by verify_bind_c_derived_type. */
13004 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13005 sym->common_block);
13006 }
13007
13008 if (!t)
13009 {
13010 /* clear the is_bind_c flag to prevent reporting errors more than
13011 once if something failed. */
13012 sym->attr.is_bind_c = 0;
13013 return;
13014 }
13015 }
13016
13017 /* If a derived type symbol has reached this point, without its
13018 type being declared, we have an error. Notice that most
13019 conditions that produce undefined derived types have already
13020 been dealt with. However, the likes of:
13021 implicit type(t) (t) ..... call foo (t) will get us here if
13022 the type is not declared in the scope of the implicit
13023 statement. Change the type to BT_UNKNOWN, both because it is so
13024 and to prevent an ICE. */
13025 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13026 && sym->ts.u.derived->components == NULL
13027 && !sym->ts.u.derived->attr.zero_comp)
13028 {
13029 gfc_error ("The derived type '%s' at %L is of type '%s', "
13030 "which has not been defined", sym->name,
13031 &sym->declared_at, sym->ts.u.derived->name);
13032 sym->ts.type = BT_UNKNOWN;
13033 return;
13034 }
13035
13036 /* Make sure that the derived type has been resolved and that the
13037 derived type is visible in the symbol's namespace, if it is a
13038 module function and is not PRIVATE. */
13039 if (sym->ts.type == BT_DERIVED
13040 && sym->ts.u.derived->attr.use_assoc
13041 && sym->ns->proc_name
13042 && sym->ns->proc_name->attr.flavor == FL_MODULE
13043 && !resolve_fl_derived (sym->ts.u.derived))
13044 return;
13045
13046 /* Unless the derived-type declaration is use associated, Fortran 95
13047 does not allow public entries of private derived types.
13048 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
13049 161 in 95-006r3. */
13050 if (sym->ts.type == BT_DERIVED
13051 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
13052 && !sym->ts.u.derived->attr.use_assoc
13053 && gfc_check_symbol_access (sym)
13054 && !gfc_check_symbol_access (sym->ts.u.derived)
13055 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s '%s' at %L of PRIVATE "
13056 "derived type '%s'",
13057 (sym->attr.flavor == FL_PARAMETER)
13058 ? "parameter" : "variable",
13059 sym->name, &sym->declared_at,
13060 sym->ts.u.derived->name))
13061 return;
13062
13063 /* F2008, C1302. */
13064 if (sym->ts.type == BT_DERIVED
13065 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
13066 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
13067 || sym->ts.u.derived->attr.lock_comp)
13068 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
13069 {
13070 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
13071 "type LOCK_TYPE must be a coarray", sym->name,
13072 &sym->declared_at);
13073 return;
13074 }
13075
13076 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
13077 default initialization is defined (5.1.2.4.4). */
13078 if (sym->ts.type == BT_DERIVED
13079 && sym->attr.dummy
13080 && sym->attr.intent == INTENT_OUT
13081 && sym->as
13082 && sym->as->type == AS_ASSUMED_SIZE)
13083 {
13084 for (c = sym->ts.u.derived->components; c; c = c->next)
13085 {
13086 if (c->initializer)
13087 {
13088 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
13089 "ASSUMED SIZE and so cannot have a default initializer",
13090 sym->name, &sym->declared_at);
13091 return;
13092 }
13093 }
13094 }
13095
13096 /* F2008, C542. */
13097 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
13098 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
13099 {
13100 gfc_error ("Dummy argument '%s' at %L of LOCK_TYPE shall not be "
13101 "INTENT(OUT)", sym->name, &sym->declared_at);
13102 return;
13103 }
13104
13105 /* F2008, C525. */
13106 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13107 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13108 && CLASS_DATA (sym)->attr.coarray_comp))
13109 || class_attr.codimension)
13110 && (sym->attr.result || sym->result == sym))
13111 {
13112 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
13113 "a coarray component", sym->name, &sym->declared_at);
13114 return;
13115 }
13116
13117 /* F2008, C524. */
13118 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
13119 && sym->ts.u.derived->ts.is_iso_c)
13120 {
13121 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
13122 "shall not be a coarray", sym->name, &sym->declared_at);
13123 return;
13124 }
13125
13126 /* F2008, C525. */
13127 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13128 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13129 && CLASS_DATA (sym)->attr.coarray_comp))
13130 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
13131 || class_attr.allocatable))
13132 {
13133 gfc_error ("Variable '%s' at %L with coarray component shall be a "
13134 "nonpointer, nonallocatable scalar, which is not a coarray",
13135 sym->name, &sym->declared_at);
13136 return;
13137 }
13138
13139 /* F2008, C526. The function-result case was handled above. */
13140 if (class_attr.codimension
13141 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
13142 || sym->attr.select_type_temporary
13143 || sym->ns->save_all
13144 || sym->ns->proc_name->attr.flavor == FL_MODULE
13145 || sym->ns->proc_name->attr.is_main_program
13146 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
13147 {
13148 gfc_error ("Variable '%s' at %L is a coarray and is not ALLOCATABLE, SAVE "
13149 "nor a dummy argument", sym->name, &sym->declared_at);
13150 return;
13151 }
13152 /* F2008, C528. */
13153 else if (class_attr.codimension && !sym->attr.select_type_temporary
13154 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
13155 {
13156 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
13157 "deferred shape", sym->name, &sym->declared_at);
13158 return;
13159 }
13160 else if (class_attr.codimension && class_attr.allocatable && as
13161 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
13162 {
13163 gfc_error ("Allocatable coarray variable '%s' at %L must have "
13164 "deferred shape", sym->name, &sym->declared_at);
13165 return;
13166 }
13167
13168 /* F2008, C541. */
13169 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13170 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13171 && CLASS_DATA (sym)->attr.coarray_comp))
13172 || (class_attr.codimension && class_attr.allocatable))
13173 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
13174 {
13175 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
13176 "allocatable coarray or have coarray components",
13177 sym->name, &sym->declared_at);
13178 return;
13179 }
13180
13181 if (class_attr.codimension && sym->attr.dummy
13182 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
13183 {
13184 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
13185 "procedure '%s'", sym->name, &sym->declared_at,
13186 sym->ns->proc_name->name);
13187 return;
13188 }
13189
13190 if (sym->ts.type == BT_LOGICAL
13191 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
13192 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
13193 && sym->ns->proc_name->attr.is_bind_c)))
13194 {
13195 int i;
13196 for (i = 0; gfc_logical_kinds[i].kind; i++)
13197 if (gfc_logical_kinds[i].kind == sym->ts.kind)
13198 break;
13199 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
13200 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument '%s' at "
13201 "%L with non-C_Bool kind in BIND(C) procedure "
13202 "'%s'", sym->name, &sym->declared_at,
13203 sym->ns->proc_name->name))
13204 return;
13205 else if (!gfc_logical_kinds[i].c_bool
13206 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
13207 "'%s' at %L with non-C_Bool kind in "
13208 "BIND(C) procedure '%s'", sym->name,
13209 &sym->declared_at,
13210 sym->attr.function ? sym->name
13211 : sym->ns->proc_name->name))
13212 return;
13213 }
13214
13215 switch (sym->attr.flavor)
13216 {
13217 case FL_VARIABLE:
13218 if (!resolve_fl_variable (sym, mp_flag))
13219 return;
13220 break;
13221
13222 case FL_PROCEDURE:
13223 if (!resolve_fl_procedure (sym, mp_flag))
13224 return;
13225 break;
13226
13227 case FL_NAMELIST:
13228 if (!resolve_fl_namelist (sym))
13229 return;
13230 break;
13231
13232 case FL_PARAMETER:
13233 if (!resolve_fl_parameter (sym))
13234 return;
13235 break;
13236
13237 default:
13238 break;
13239 }
13240
13241 /* Resolve array specifier. Check as well some constraints
13242 on COMMON blocks. */
13243
13244 check_constant = sym->attr.in_common && !sym->attr.pointer;
13245
13246 /* Set the formal_arg_flag so that check_conflict will not throw
13247 an error for host associated variables in the specification
13248 expression for an array_valued function. */
13249 if (sym->attr.function && sym->as)
13250 formal_arg_flag = 1;
13251
13252 saved_specification_expr = specification_expr;
13253 specification_expr = true;
13254 gfc_resolve_array_spec (sym->as, check_constant);
13255 specification_expr = saved_specification_expr;
13256
13257 formal_arg_flag = 0;
13258
13259 /* Resolve formal namespaces. */
13260 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
13261 && !sym->attr.contained && !sym->attr.intrinsic)
13262 gfc_resolve (sym->formal_ns);
13263
13264 /* Make sure the formal namespace is present. */
13265 if (sym->formal && !sym->formal_ns)
13266 {
13267 gfc_formal_arglist *formal = sym->formal;
13268 while (formal && !formal->sym)
13269 formal = formal->next;
13270
13271 if (formal)
13272 {
13273 sym->formal_ns = formal->sym->ns;
13274 if (sym->ns != formal->sym->ns)
13275 sym->formal_ns->refs++;
13276 }
13277 }
13278
13279 /* Check threadprivate restrictions. */
13280 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
13281 && (!sym->attr.in_common
13282 && sym->module == NULL
13283 && (sym->ns->proc_name == NULL
13284 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
13285 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
13286
13287 /* If we have come this far we can apply default-initializers, as
13288 described in 14.7.5, to those variables that have not already
13289 been assigned one. */
13290 if (sym->ts.type == BT_DERIVED
13291 && !sym->value
13292 && !sym->attr.allocatable
13293 && !sym->attr.alloc_comp)
13294 {
13295 symbol_attribute *a = &sym->attr;
13296
13297 if ((!a->save && !a->dummy && !a->pointer
13298 && !a->in_common && !a->use_assoc
13299 && (a->referenced || a->result)
13300 && !(a->function && sym != sym->result))
13301 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
13302 apply_default_init (sym);
13303 }
13304
13305 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
13306 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
13307 && !CLASS_DATA (sym)->attr.class_pointer
13308 && !CLASS_DATA (sym)->attr.allocatable)
13309 apply_default_init (sym);
13310
13311 /* If this symbol has a type-spec, check it. */
13312 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
13313 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
13314 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
13315 return;
13316 }
13317
13318
13319 /************* Resolve DATA statements *************/
13320
13321 static struct
13322 {
13323 gfc_data_value *vnode;
13324 mpz_t left;
13325 }
13326 values;
13327
13328
13329 /* Advance the values structure to point to the next value in the data list. */
13330
13331 static bool
13332 next_data_value (void)
13333 {
13334 while (mpz_cmp_ui (values.left, 0) == 0)
13335 {
13336
13337 if (values.vnode->next == NULL)
13338 return false;
13339
13340 values.vnode = values.vnode->next;
13341 mpz_set (values.left, values.vnode->repeat);
13342 }
13343
13344 return true;
13345 }
13346
13347
13348 static bool
13349 check_data_variable (gfc_data_variable *var, locus *where)
13350 {
13351 gfc_expr *e;
13352 mpz_t size;
13353 mpz_t offset;
13354 bool t;
13355 ar_type mark = AR_UNKNOWN;
13356 int i;
13357 mpz_t section_index[GFC_MAX_DIMENSIONS];
13358 gfc_ref *ref;
13359 gfc_array_ref *ar;
13360 gfc_symbol *sym;
13361 int has_pointer;
13362
13363 if (!gfc_resolve_expr (var->expr))
13364 return false;
13365
13366 ar = NULL;
13367 mpz_init_set_si (offset, 0);
13368 e = var->expr;
13369
13370 if (e->expr_type != EXPR_VARIABLE)
13371 gfc_internal_error ("check_data_variable(): Bad expression");
13372
13373 sym = e->symtree->n.sym;
13374
13375 if (sym->ns->is_block_data && !sym->attr.in_common)
13376 {
13377 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
13378 sym->name, &sym->declared_at);
13379 }
13380
13381 if (e->ref == NULL && sym->as)
13382 {
13383 gfc_error ("DATA array '%s' at %L must be specified in a previous"
13384 " declaration", sym->name, where);
13385 return false;
13386 }
13387
13388 has_pointer = sym->attr.pointer;
13389
13390 if (gfc_is_coindexed (e))
13391 {
13392 gfc_error ("DATA element '%s' at %L cannot have a coindex", sym->name,
13393 where);
13394 return false;
13395 }
13396
13397 for (ref = e->ref; ref; ref = ref->next)
13398 {
13399 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
13400 has_pointer = 1;
13401
13402 if (has_pointer
13403 && ref->type == REF_ARRAY
13404 && ref->u.ar.type != AR_FULL)
13405 {
13406 gfc_error ("DATA element '%s' at %L is a pointer and so must "
13407 "be a full array", sym->name, where);
13408 return false;
13409 }
13410 }
13411
13412 if (e->rank == 0 || has_pointer)
13413 {
13414 mpz_init_set_ui (size, 1);
13415 ref = NULL;
13416 }
13417 else
13418 {
13419 ref = e->ref;
13420
13421 /* Find the array section reference. */
13422 for (ref = e->ref; ref; ref = ref->next)
13423 {
13424 if (ref->type != REF_ARRAY)
13425 continue;
13426 if (ref->u.ar.type == AR_ELEMENT)
13427 continue;
13428 break;
13429 }
13430 gcc_assert (ref);
13431
13432 /* Set marks according to the reference pattern. */
13433 switch (ref->u.ar.type)
13434 {
13435 case AR_FULL:
13436 mark = AR_FULL;
13437 break;
13438
13439 case AR_SECTION:
13440 ar = &ref->u.ar;
13441 /* Get the start position of array section. */
13442 gfc_get_section_index (ar, section_index, &offset);
13443 mark = AR_SECTION;
13444 break;
13445
13446 default:
13447 gcc_unreachable ();
13448 }
13449
13450 if (!gfc_array_size (e, &size))
13451 {
13452 gfc_error ("Nonconstant array section at %L in DATA statement",
13453 &e->where);
13454 mpz_clear (offset);
13455 return false;
13456 }
13457 }
13458
13459 t = true;
13460
13461 while (mpz_cmp_ui (size, 0) > 0)
13462 {
13463 if (!next_data_value ())
13464 {
13465 gfc_error ("DATA statement at %L has more variables than values",
13466 where);
13467 t = false;
13468 break;
13469 }
13470
13471 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
13472 if (!t)
13473 break;
13474
13475 /* If we have more than one element left in the repeat count,
13476 and we have more than one element left in the target variable,
13477 then create a range assignment. */
13478 /* FIXME: Only done for full arrays for now, since array sections
13479 seem tricky. */
13480 if (mark == AR_FULL && ref && ref->next == NULL
13481 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
13482 {
13483 mpz_t range;
13484
13485 if (mpz_cmp (size, values.left) >= 0)
13486 {
13487 mpz_init_set (range, values.left);
13488 mpz_sub (size, size, values.left);
13489 mpz_set_ui (values.left, 0);
13490 }
13491 else
13492 {
13493 mpz_init_set (range, size);
13494 mpz_sub (values.left, values.left, size);
13495 mpz_set_ui (size, 0);
13496 }
13497
13498 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13499 offset, &range);
13500
13501 mpz_add (offset, offset, range);
13502 mpz_clear (range);
13503
13504 if (!t)
13505 break;
13506 }
13507
13508 /* Assign initial value to symbol. */
13509 else
13510 {
13511 mpz_sub_ui (values.left, values.left, 1);
13512 mpz_sub_ui (size, size, 1);
13513
13514 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13515 offset, NULL);
13516 if (!t)
13517 break;
13518
13519 if (mark == AR_FULL)
13520 mpz_add_ui (offset, offset, 1);
13521
13522 /* Modify the array section indexes and recalculate the offset
13523 for next element. */
13524 else if (mark == AR_SECTION)
13525 gfc_advance_section (section_index, ar, &offset);
13526 }
13527 }
13528
13529 if (mark == AR_SECTION)
13530 {
13531 for (i = 0; i < ar->dimen; i++)
13532 mpz_clear (section_index[i]);
13533 }
13534
13535 mpz_clear (size);
13536 mpz_clear (offset);
13537
13538 return t;
13539 }
13540
13541
13542 static bool traverse_data_var (gfc_data_variable *, locus *);
13543
13544 /* Iterate over a list of elements in a DATA statement. */
13545
13546 static bool
13547 traverse_data_list (gfc_data_variable *var, locus *where)
13548 {
13549 mpz_t trip;
13550 iterator_stack frame;
13551 gfc_expr *e, *start, *end, *step;
13552 bool retval = true;
13553
13554 mpz_init (frame.value);
13555 mpz_init (trip);
13556
13557 start = gfc_copy_expr (var->iter.start);
13558 end = gfc_copy_expr (var->iter.end);
13559 step = gfc_copy_expr (var->iter.step);
13560
13561 if (!gfc_simplify_expr (start, 1)
13562 || start->expr_type != EXPR_CONSTANT)
13563 {
13564 gfc_error ("start of implied-do loop at %L could not be "
13565 "simplified to a constant value", &start->where);
13566 retval = false;
13567 goto cleanup;
13568 }
13569 if (!gfc_simplify_expr (end, 1)
13570 || end->expr_type != EXPR_CONSTANT)
13571 {
13572 gfc_error ("end of implied-do loop at %L could not be "
13573 "simplified to a constant value", &start->where);
13574 retval = false;
13575 goto cleanup;
13576 }
13577 if (!gfc_simplify_expr (step, 1)
13578 || step->expr_type != EXPR_CONSTANT)
13579 {
13580 gfc_error ("step of implied-do loop at %L could not be "
13581 "simplified to a constant value", &start->where);
13582 retval = false;
13583 goto cleanup;
13584 }
13585
13586 mpz_set (trip, end->value.integer);
13587 mpz_sub (trip, trip, start->value.integer);
13588 mpz_add (trip, trip, step->value.integer);
13589
13590 mpz_div (trip, trip, step->value.integer);
13591
13592 mpz_set (frame.value, start->value.integer);
13593
13594 frame.prev = iter_stack;
13595 frame.variable = var->iter.var->symtree;
13596 iter_stack = &frame;
13597
13598 while (mpz_cmp_ui (trip, 0) > 0)
13599 {
13600 if (!traverse_data_var (var->list, where))
13601 {
13602 retval = false;
13603 goto cleanup;
13604 }
13605
13606 e = gfc_copy_expr (var->expr);
13607 if (!gfc_simplify_expr (e, 1))
13608 {
13609 gfc_free_expr (e);
13610 retval = false;
13611 goto cleanup;
13612 }
13613
13614 mpz_add (frame.value, frame.value, step->value.integer);
13615
13616 mpz_sub_ui (trip, trip, 1);
13617 }
13618
13619 cleanup:
13620 mpz_clear (frame.value);
13621 mpz_clear (trip);
13622
13623 gfc_free_expr (start);
13624 gfc_free_expr (end);
13625 gfc_free_expr (step);
13626
13627 iter_stack = frame.prev;
13628 return retval;
13629 }
13630
13631
13632 /* Type resolve variables in the variable list of a DATA statement. */
13633
13634 static bool
13635 traverse_data_var (gfc_data_variable *var, locus *where)
13636 {
13637 bool t;
13638
13639 for (; var; var = var->next)
13640 {
13641 if (var->expr == NULL)
13642 t = traverse_data_list (var, where);
13643 else
13644 t = check_data_variable (var, where);
13645
13646 if (!t)
13647 return false;
13648 }
13649
13650 return true;
13651 }
13652
13653
13654 /* Resolve the expressions and iterators associated with a data statement.
13655 This is separate from the assignment checking because data lists should
13656 only be resolved once. */
13657
13658 static bool
13659 resolve_data_variables (gfc_data_variable *d)
13660 {
13661 for (; d; d = d->next)
13662 {
13663 if (d->list == NULL)
13664 {
13665 if (!gfc_resolve_expr (d->expr))
13666 return false;
13667 }
13668 else
13669 {
13670 if (!gfc_resolve_iterator (&d->iter, false, true))
13671 return false;
13672
13673 if (!resolve_data_variables (d->list))
13674 return false;
13675 }
13676 }
13677
13678 return true;
13679 }
13680
13681
13682 /* Resolve a single DATA statement. We implement this by storing a pointer to
13683 the value list into static variables, and then recursively traversing the
13684 variables list, expanding iterators and such. */
13685
13686 static void
13687 resolve_data (gfc_data *d)
13688 {
13689
13690 if (!resolve_data_variables (d->var))
13691 return;
13692
13693 values.vnode = d->value;
13694 if (d->value == NULL)
13695 mpz_set_ui (values.left, 0);
13696 else
13697 mpz_set (values.left, d->value->repeat);
13698
13699 if (!traverse_data_var (d->var, &d->where))
13700 return;
13701
13702 /* At this point, we better not have any values left. */
13703
13704 if (next_data_value ())
13705 gfc_error ("DATA statement at %L has more values than variables",
13706 &d->where);
13707 }
13708
13709
13710 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
13711 accessed by host or use association, is a dummy argument to a pure function,
13712 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
13713 is storage associated with any such variable, shall not be used in the
13714 following contexts: (clients of this function). */
13715
13716 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
13717 procedure. Returns zero if assignment is OK, nonzero if there is a
13718 problem. */
13719 int
13720 gfc_impure_variable (gfc_symbol *sym)
13721 {
13722 gfc_symbol *proc;
13723 gfc_namespace *ns;
13724
13725 if (sym->attr.use_assoc || sym->attr.in_common)
13726 return 1;
13727
13728 /* Check if the symbol's ns is inside the pure procedure. */
13729 for (ns = gfc_current_ns; ns; ns = ns->parent)
13730 {
13731 if (ns == sym->ns)
13732 break;
13733 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
13734 return 1;
13735 }
13736
13737 proc = sym->ns->proc_name;
13738 if (sym->attr.dummy
13739 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
13740 || proc->attr.function))
13741 return 1;
13742
13743 /* TODO: Sort out what can be storage associated, if anything, and include
13744 it here. In principle equivalences should be scanned but it does not
13745 seem to be possible to storage associate an impure variable this way. */
13746 return 0;
13747 }
13748
13749
13750 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
13751 current namespace is inside a pure procedure. */
13752
13753 int
13754 gfc_pure (gfc_symbol *sym)
13755 {
13756 symbol_attribute attr;
13757 gfc_namespace *ns;
13758
13759 if (sym == NULL)
13760 {
13761 /* Check if the current namespace or one of its parents
13762 belongs to a pure procedure. */
13763 for (ns = gfc_current_ns; ns; ns = ns->parent)
13764 {
13765 sym = ns->proc_name;
13766 if (sym == NULL)
13767 return 0;
13768 attr = sym->attr;
13769 if (attr.flavor == FL_PROCEDURE && attr.pure)
13770 return 1;
13771 }
13772 return 0;
13773 }
13774
13775 attr = sym->attr;
13776
13777 return attr.flavor == FL_PROCEDURE && attr.pure;
13778 }
13779
13780
13781 /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
13782 checks if the current namespace is implicitly pure. Note that this
13783 function returns false for a PURE procedure. */
13784
13785 int
13786 gfc_implicit_pure (gfc_symbol *sym)
13787 {
13788 gfc_namespace *ns;
13789
13790 if (sym == NULL)
13791 {
13792 /* Check if the current procedure is implicit_pure. Walk up
13793 the procedure list until we find a procedure. */
13794 for (ns = gfc_current_ns; ns; ns = ns->parent)
13795 {
13796 sym = ns->proc_name;
13797 if (sym == NULL)
13798 return 0;
13799
13800 if (sym->attr.flavor == FL_PROCEDURE)
13801 break;
13802 }
13803 }
13804
13805 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
13806 && !sym->attr.pure;
13807 }
13808
13809
13810 /* Test whether the current procedure is elemental or not. */
13811
13812 int
13813 gfc_elemental (gfc_symbol *sym)
13814 {
13815 symbol_attribute attr;
13816
13817 if (sym == NULL)
13818 sym = gfc_current_ns->proc_name;
13819 if (sym == NULL)
13820 return 0;
13821 attr = sym->attr;
13822
13823 return attr.flavor == FL_PROCEDURE && attr.elemental;
13824 }
13825
13826
13827 /* Warn about unused labels. */
13828
13829 static void
13830 warn_unused_fortran_label (gfc_st_label *label)
13831 {
13832 if (label == NULL)
13833 return;
13834
13835 warn_unused_fortran_label (label->left);
13836
13837 if (label->defined == ST_LABEL_UNKNOWN)
13838 return;
13839
13840 switch (label->referenced)
13841 {
13842 case ST_LABEL_UNKNOWN:
13843 gfc_warning ("Label %d at %L defined but not used", label->value,
13844 &label->where);
13845 break;
13846
13847 case ST_LABEL_BAD_TARGET:
13848 gfc_warning ("Label %d at %L defined but cannot be used",
13849 label->value, &label->where);
13850 break;
13851
13852 default:
13853 break;
13854 }
13855
13856 warn_unused_fortran_label (label->right);
13857 }
13858
13859
13860 /* Returns the sequence type of a symbol or sequence. */
13861
13862 static seq_type
13863 sequence_type (gfc_typespec ts)
13864 {
13865 seq_type result;
13866 gfc_component *c;
13867
13868 switch (ts.type)
13869 {
13870 case BT_DERIVED:
13871
13872 if (ts.u.derived->components == NULL)
13873 return SEQ_NONDEFAULT;
13874
13875 result = sequence_type (ts.u.derived->components->ts);
13876 for (c = ts.u.derived->components->next; c; c = c->next)
13877 if (sequence_type (c->ts) != result)
13878 return SEQ_MIXED;
13879
13880 return result;
13881
13882 case BT_CHARACTER:
13883 if (ts.kind != gfc_default_character_kind)
13884 return SEQ_NONDEFAULT;
13885
13886 return SEQ_CHARACTER;
13887
13888 case BT_INTEGER:
13889 if (ts.kind != gfc_default_integer_kind)
13890 return SEQ_NONDEFAULT;
13891
13892 return SEQ_NUMERIC;
13893
13894 case BT_REAL:
13895 if (!(ts.kind == gfc_default_real_kind
13896 || ts.kind == gfc_default_double_kind))
13897 return SEQ_NONDEFAULT;
13898
13899 return SEQ_NUMERIC;
13900
13901 case BT_COMPLEX:
13902 if (ts.kind != gfc_default_complex_kind)
13903 return SEQ_NONDEFAULT;
13904
13905 return SEQ_NUMERIC;
13906
13907 case BT_LOGICAL:
13908 if (ts.kind != gfc_default_logical_kind)
13909 return SEQ_NONDEFAULT;
13910
13911 return SEQ_NUMERIC;
13912
13913 default:
13914 return SEQ_NONDEFAULT;
13915 }
13916 }
13917
13918
13919 /* Resolve derived type EQUIVALENCE object. */
13920
13921 static bool
13922 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
13923 {
13924 gfc_component *c = derived->components;
13925
13926 if (!derived)
13927 return true;
13928
13929 /* Shall not be an object of nonsequence derived type. */
13930 if (!derived->attr.sequence)
13931 {
13932 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
13933 "attribute to be an EQUIVALENCE object", sym->name,
13934 &e->where);
13935 return false;
13936 }
13937
13938 /* Shall not have allocatable components. */
13939 if (derived->attr.alloc_comp)
13940 {
13941 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
13942 "components to be an EQUIVALENCE object",sym->name,
13943 &e->where);
13944 return false;
13945 }
13946
13947 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
13948 {
13949 gfc_error ("Derived type variable '%s' at %L with default "
13950 "initialization cannot be in EQUIVALENCE with a variable "
13951 "in COMMON", sym->name, &e->where);
13952 return false;
13953 }
13954
13955 for (; c ; c = c->next)
13956 {
13957 if (c->ts.type == BT_DERIVED
13958 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
13959 return false;
13960
13961 /* Shall not be an object of sequence derived type containing a pointer
13962 in the structure. */
13963 if (c->attr.pointer)
13964 {
13965 gfc_error ("Derived type variable '%s' at %L with pointer "
13966 "component(s) cannot be an EQUIVALENCE object",
13967 sym->name, &e->where);
13968 return false;
13969 }
13970 }
13971 return true;
13972 }
13973
13974
13975 /* Resolve equivalence object.
13976 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
13977 an allocatable array, an object of nonsequence derived type, an object of
13978 sequence derived type containing a pointer at any level of component
13979 selection, an automatic object, a function name, an entry name, a result
13980 name, a named constant, a structure component, or a subobject of any of
13981 the preceding objects. A substring shall not have length zero. A
13982 derived type shall not have components with default initialization nor
13983 shall two objects of an equivalence group be initialized.
13984 Either all or none of the objects shall have an protected attribute.
13985 The simple constraints are done in symbol.c(check_conflict) and the rest
13986 are implemented here. */
13987
13988 static void
13989 resolve_equivalence (gfc_equiv *eq)
13990 {
13991 gfc_symbol *sym;
13992 gfc_symbol *first_sym;
13993 gfc_expr *e;
13994 gfc_ref *r;
13995 locus *last_where = NULL;
13996 seq_type eq_type, last_eq_type;
13997 gfc_typespec *last_ts;
13998 int object, cnt_protected;
13999 const char *msg;
14000
14001 last_ts = &eq->expr->symtree->n.sym->ts;
14002
14003 first_sym = eq->expr->symtree->n.sym;
14004
14005 cnt_protected = 0;
14006
14007 for (object = 1; eq; eq = eq->eq, object++)
14008 {
14009 e = eq->expr;
14010
14011 e->ts = e->symtree->n.sym->ts;
14012 /* match_varspec might not know yet if it is seeing
14013 array reference or substring reference, as it doesn't
14014 know the types. */
14015 if (e->ref && e->ref->type == REF_ARRAY)
14016 {
14017 gfc_ref *ref = e->ref;
14018 sym = e->symtree->n.sym;
14019
14020 if (sym->attr.dimension)
14021 {
14022 ref->u.ar.as = sym->as;
14023 ref = ref->next;
14024 }
14025
14026 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
14027 if (e->ts.type == BT_CHARACTER
14028 && ref
14029 && ref->type == REF_ARRAY
14030 && ref->u.ar.dimen == 1
14031 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
14032 && ref->u.ar.stride[0] == NULL)
14033 {
14034 gfc_expr *start = ref->u.ar.start[0];
14035 gfc_expr *end = ref->u.ar.end[0];
14036 void *mem = NULL;
14037
14038 /* Optimize away the (:) reference. */
14039 if (start == NULL && end == NULL)
14040 {
14041 if (e->ref == ref)
14042 e->ref = ref->next;
14043 else
14044 e->ref->next = ref->next;
14045 mem = ref;
14046 }
14047 else
14048 {
14049 ref->type = REF_SUBSTRING;
14050 if (start == NULL)
14051 start = gfc_get_int_expr (gfc_default_integer_kind,
14052 NULL, 1);
14053 ref->u.ss.start = start;
14054 if (end == NULL && e->ts.u.cl)
14055 end = gfc_copy_expr (e->ts.u.cl->length);
14056 ref->u.ss.end = end;
14057 ref->u.ss.length = e->ts.u.cl;
14058 e->ts.u.cl = NULL;
14059 }
14060 ref = ref->next;
14061 free (mem);
14062 }
14063
14064 /* Any further ref is an error. */
14065 if (ref)
14066 {
14067 gcc_assert (ref->type == REF_ARRAY);
14068 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
14069 &ref->u.ar.where);
14070 continue;
14071 }
14072 }
14073
14074 if (!gfc_resolve_expr (e))
14075 continue;
14076
14077 sym = e->symtree->n.sym;
14078
14079 if (sym->attr.is_protected)
14080 cnt_protected++;
14081 if (cnt_protected > 0 && cnt_protected != object)
14082 {
14083 gfc_error ("Either all or none of the objects in the "
14084 "EQUIVALENCE set at %L shall have the "
14085 "PROTECTED attribute",
14086 &e->where);
14087 break;
14088 }
14089
14090 /* Shall not equivalence common block variables in a PURE procedure. */
14091 if (sym->ns->proc_name
14092 && sym->ns->proc_name->attr.pure
14093 && sym->attr.in_common)
14094 {
14095 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
14096 "object in the pure procedure '%s'",
14097 sym->name, &e->where, sym->ns->proc_name->name);
14098 break;
14099 }
14100
14101 /* Shall not be a named constant. */
14102 if (e->expr_type == EXPR_CONSTANT)
14103 {
14104 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
14105 "object", sym->name, &e->where);
14106 continue;
14107 }
14108
14109 if (e->ts.type == BT_DERIVED
14110 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
14111 continue;
14112
14113 /* Check that the types correspond correctly:
14114 Note 5.28:
14115 A numeric sequence structure may be equivalenced to another sequence
14116 structure, an object of default integer type, default real type, double
14117 precision real type, default logical type such that components of the
14118 structure ultimately only become associated to objects of the same
14119 kind. A character sequence structure may be equivalenced to an object
14120 of default character kind or another character sequence structure.
14121 Other objects may be equivalenced only to objects of the same type and
14122 kind parameters. */
14123
14124 /* Identical types are unconditionally OK. */
14125 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
14126 goto identical_types;
14127
14128 last_eq_type = sequence_type (*last_ts);
14129 eq_type = sequence_type (sym->ts);
14130
14131 /* Since the pair of objects is not of the same type, mixed or
14132 non-default sequences can be rejected. */
14133
14134 msg = "Sequence %s with mixed components in EQUIVALENCE "
14135 "statement at %L with different type objects";
14136 if ((object ==2
14137 && last_eq_type == SEQ_MIXED
14138 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
14139 || (eq_type == SEQ_MIXED
14140 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
14141 continue;
14142
14143 msg = "Non-default type object or sequence %s in EQUIVALENCE "
14144 "statement at %L with objects of different type";
14145 if ((object ==2
14146 && last_eq_type == SEQ_NONDEFAULT
14147 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
14148 || (eq_type == SEQ_NONDEFAULT
14149 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
14150 continue;
14151
14152 msg ="Non-CHARACTER object '%s' in default CHARACTER "
14153 "EQUIVALENCE statement at %L";
14154 if (last_eq_type == SEQ_CHARACTER
14155 && eq_type != SEQ_CHARACTER
14156 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
14157 continue;
14158
14159 msg ="Non-NUMERIC object '%s' in default NUMERIC "
14160 "EQUIVALENCE statement at %L";
14161 if (last_eq_type == SEQ_NUMERIC
14162 && eq_type != SEQ_NUMERIC
14163 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
14164 continue;
14165
14166 identical_types:
14167 last_ts =&sym->ts;
14168 last_where = &e->where;
14169
14170 if (!e->ref)
14171 continue;
14172
14173 /* Shall not be an automatic array. */
14174 if (e->ref->type == REF_ARRAY
14175 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
14176 {
14177 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
14178 "an EQUIVALENCE object", sym->name, &e->where);
14179 continue;
14180 }
14181
14182 r = e->ref;
14183 while (r)
14184 {
14185 /* Shall not be a structure component. */
14186 if (r->type == REF_COMPONENT)
14187 {
14188 gfc_error ("Structure component '%s' at %L cannot be an "
14189 "EQUIVALENCE object",
14190 r->u.c.component->name, &e->where);
14191 break;
14192 }
14193
14194 /* A substring shall not have length zero. */
14195 if (r->type == REF_SUBSTRING)
14196 {
14197 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
14198 {
14199 gfc_error ("Substring at %L has length zero",
14200 &r->u.ss.start->where);
14201 break;
14202 }
14203 }
14204 r = r->next;
14205 }
14206 }
14207 }
14208
14209
14210 /* Resolve function and ENTRY types, issue diagnostics if needed. */
14211
14212 static void
14213 resolve_fntype (gfc_namespace *ns)
14214 {
14215 gfc_entry_list *el;
14216 gfc_symbol *sym;
14217
14218 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
14219 return;
14220
14221 /* If there are any entries, ns->proc_name is the entry master
14222 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
14223 if (ns->entries)
14224 sym = ns->entries->sym;
14225 else
14226 sym = ns->proc_name;
14227 if (sym->result == sym
14228 && sym->ts.type == BT_UNKNOWN
14229 && !gfc_set_default_type (sym, 0, NULL)
14230 && !sym->attr.untyped)
14231 {
14232 gfc_error ("Function '%s' at %L has no IMPLICIT type",
14233 sym->name, &sym->declared_at);
14234 sym->attr.untyped = 1;
14235 }
14236
14237 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
14238 && !sym->attr.contained
14239 && !gfc_check_symbol_access (sym->ts.u.derived)
14240 && gfc_check_symbol_access (sym))
14241 {
14242 gfc_notify_std (GFC_STD_F2003, "PUBLIC function '%s' at "
14243 "%L of PRIVATE type '%s'", sym->name,
14244 &sym->declared_at, sym->ts.u.derived->name);
14245 }
14246
14247 if (ns->entries)
14248 for (el = ns->entries->next; el; el = el->next)
14249 {
14250 if (el->sym->result == el->sym
14251 && el->sym->ts.type == BT_UNKNOWN
14252 && !gfc_set_default_type (el->sym, 0, NULL)
14253 && !el->sym->attr.untyped)
14254 {
14255 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
14256 el->sym->name, &el->sym->declared_at);
14257 el->sym->attr.untyped = 1;
14258 }
14259 }
14260 }
14261
14262
14263 /* 12.3.2.1.1 Defined operators. */
14264
14265 static bool
14266 check_uop_procedure (gfc_symbol *sym, locus where)
14267 {
14268 gfc_formal_arglist *formal;
14269
14270 if (!sym->attr.function)
14271 {
14272 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
14273 sym->name, &where);
14274 return false;
14275 }
14276
14277 if (sym->ts.type == BT_CHARACTER
14278 && !(sym->ts.u.cl && sym->ts.u.cl->length)
14279 && !(sym->result && sym->result->ts.u.cl
14280 && sym->result->ts.u.cl->length))
14281 {
14282 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
14283 "character length", sym->name, &where);
14284 return false;
14285 }
14286
14287 formal = gfc_sym_get_dummy_args (sym);
14288 if (!formal || !formal->sym)
14289 {
14290 gfc_error ("User operator procedure '%s' at %L must have at least "
14291 "one argument", sym->name, &where);
14292 return false;
14293 }
14294
14295 if (formal->sym->attr.intent != INTENT_IN)
14296 {
14297 gfc_error ("First argument of operator interface at %L must be "
14298 "INTENT(IN)", &where);
14299 return false;
14300 }
14301
14302 if (formal->sym->attr.optional)
14303 {
14304 gfc_error ("First argument of operator interface at %L cannot be "
14305 "optional", &where);
14306 return false;
14307 }
14308
14309 formal = formal->next;
14310 if (!formal || !formal->sym)
14311 return true;
14312
14313 if (formal->sym->attr.intent != INTENT_IN)
14314 {
14315 gfc_error ("Second argument of operator interface at %L must be "
14316 "INTENT(IN)", &where);
14317 return false;
14318 }
14319
14320 if (formal->sym->attr.optional)
14321 {
14322 gfc_error ("Second argument of operator interface at %L cannot be "
14323 "optional", &where);
14324 return false;
14325 }
14326
14327 if (formal->next)
14328 {
14329 gfc_error ("Operator interface at %L must have, at most, two "
14330 "arguments", &where);
14331 return false;
14332 }
14333
14334 return true;
14335 }
14336
14337 static void
14338 gfc_resolve_uops (gfc_symtree *symtree)
14339 {
14340 gfc_interface *itr;
14341
14342 if (symtree == NULL)
14343 return;
14344
14345 gfc_resolve_uops (symtree->left);
14346 gfc_resolve_uops (symtree->right);
14347
14348 for (itr = symtree->n.uop->op; itr; itr = itr->next)
14349 check_uop_procedure (itr->sym, itr->sym->declared_at);
14350 }
14351
14352
14353 /* Examine all of the expressions associated with a program unit,
14354 assign types to all intermediate expressions, make sure that all
14355 assignments are to compatible types and figure out which names
14356 refer to which functions or subroutines. It doesn't check code
14357 block, which is handled by resolve_code. */
14358
14359 static void
14360 resolve_types (gfc_namespace *ns)
14361 {
14362 gfc_namespace *n;
14363 gfc_charlen *cl;
14364 gfc_data *d;
14365 gfc_equiv *eq;
14366 gfc_namespace* old_ns = gfc_current_ns;
14367
14368 /* Check that all IMPLICIT types are ok. */
14369 if (!ns->seen_implicit_none)
14370 {
14371 unsigned letter;
14372 for (letter = 0; letter != GFC_LETTERS; ++letter)
14373 if (ns->set_flag[letter]
14374 && !resolve_typespec_used (&ns->default_type[letter],
14375 &ns->implicit_loc[letter], NULL))
14376 return;
14377 }
14378
14379 gfc_current_ns = ns;
14380
14381 resolve_entries (ns);
14382
14383 resolve_common_vars (ns->blank_common.head, false);
14384 resolve_common_blocks (ns->common_root);
14385
14386 resolve_contained_functions (ns);
14387
14388 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
14389 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
14390 resolve_formal_arglist (ns->proc_name);
14391
14392 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
14393
14394 for (cl = ns->cl_list; cl; cl = cl->next)
14395 resolve_charlen (cl);
14396
14397 gfc_traverse_ns (ns, resolve_symbol);
14398
14399 resolve_fntype (ns);
14400
14401 for (n = ns->contained; n; n = n->sibling)
14402 {
14403 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
14404 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
14405 "also be PURE", n->proc_name->name,
14406 &n->proc_name->declared_at);
14407
14408 resolve_types (n);
14409 }
14410
14411 forall_flag = 0;
14412 do_concurrent_flag = 0;
14413 gfc_check_interfaces (ns);
14414
14415 gfc_traverse_ns (ns, resolve_values);
14416
14417 if (ns->save_all)
14418 gfc_save_all (ns);
14419
14420 iter_stack = NULL;
14421 for (d = ns->data; d; d = d->next)
14422 resolve_data (d);
14423
14424 iter_stack = NULL;
14425 gfc_traverse_ns (ns, gfc_formalize_init_value);
14426
14427 gfc_traverse_ns (ns, gfc_verify_binding_labels);
14428
14429 for (eq = ns->equiv; eq; eq = eq->next)
14430 resolve_equivalence (eq);
14431
14432 /* Warn about unused labels. */
14433 if (warn_unused_label)
14434 warn_unused_fortran_label (ns->st_labels);
14435
14436 gfc_resolve_uops (ns->uop_root);
14437
14438 gfc_current_ns = old_ns;
14439 }
14440
14441
14442 /* Call resolve_code recursively. */
14443
14444 static void
14445 resolve_codes (gfc_namespace *ns)
14446 {
14447 gfc_namespace *n;
14448 bitmap_obstack old_obstack;
14449
14450 if (ns->resolved == 1)
14451 return;
14452
14453 for (n = ns->contained; n; n = n->sibling)
14454 resolve_codes (n);
14455
14456 gfc_current_ns = ns;
14457
14458 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
14459 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
14460 cs_base = NULL;
14461
14462 /* Set to an out of range value. */
14463 current_entry_id = -1;
14464
14465 old_obstack = labels_obstack;
14466 bitmap_obstack_initialize (&labels_obstack);
14467
14468 resolve_code (ns->code, ns);
14469
14470 bitmap_obstack_release (&labels_obstack);
14471 labels_obstack = old_obstack;
14472 }
14473
14474
14475 /* This function is called after a complete program unit has been compiled.
14476 Its purpose is to examine all of the expressions associated with a program
14477 unit, assign types to all intermediate expressions, make sure that all
14478 assignments are to compatible types and figure out which names refer to
14479 which functions or subroutines. */
14480
14481 void
14482 gfc_resolve (gfc_namespace *ns)
14483 {
14484 gfc_namespace *old_ns;
14485 code_stack *old_cs_base;
14486
14487 if (ns->resolved)
14488 return;
14489
14490 ns->resolved = -1;
14491 old_ns = gfc_current_ns;
14492 old_cs_base = cs_base;
14493
14494 resolve_types (ns);
14495 component_assignment_level = 0;
14496 resolve_codes (ns);
14497
14498 gfc_current_ns = old_ns;
14499 cs_base = old_cs_base;
14500 ns->resolved = 1;
14501
14502 gfc_run_passes (ns);
14503 }