Branch data Line data Source code
1 : : /* Perform type resolution on the various structures.
2 : : Copyright (C) 2001-2025 Free Software Foundation, Inc.
3 : : Contributed by Andy Vaught
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it under
8 : : the terms of the GNU General Public License as published by the Free
9 : : Software Foundation; either version 3, or (at your option) any later
10 : : version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : : for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : #include "config.h"
22 : : #include "system.h"
23 : : #include "coretypes.h"
24 : : #include "options.h"
25 : : #include "bitmap.h"
26 : : #include "gfortran.h"
27 : : #include "arith.h" /* For gfc_compare_expr(). */
28 : : #include "dependency.h"
29 : : #include "data.h"
30 : : #include "target-memory.h" /* for gfc_simplify_transfer */
31 : : #include "constructor.h"
32 : :
33 : : /* Types used in equivalence statements. */
34 : :
35 : : enum seq_type
36 : : {
37 : : SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
38 : : };
39 : :
40 : : /* Stack to keep track of the nesting of blocks as we move through the
41 : : code. See resolve_branch() and gfc_resolve_code(). */
42 : :
43 : : typedef struct code_stack
44 : : {
45 : : struct gfc_code *head, *current;
46 : : struct code_stack *prev;
47 : :
48 : : /* This bitmap keeps track of the targets valid for a branch from
49 : : inside this block except for END {IF|SELECT}s of enclosing
50 : : blocks. */
51 : : bitmap reachable_labels;
52 : : }
53 : : code_stack;
54 : :
55 : : static code_stack *cs_base = NULL;
56 : :
57 : : struct check_default_none_data
58 : : {
59 : : gfc_code *code;
60 : : hash_set<gfc_symbol *> *sym_hash;
61 : : gfc_namespace *ns;
62 : : bool default_none;
63 : : };
64 : :
65 : : /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
66 : :
67 : : static int forall_flag;
68 : : int gfc_do_concurrent_flag;
69 : :
70 : : /* True when we are resolving an expression that is an actual argument to
71 : : a procedure. */
72 : : static bool actual_arg = false;
73 : : /* True when we are resolving an expression that is the first actual argument
74 : : to a procedure. */
75 : : static bool first_actual_arg = false;
76 : :
77 : :
78 : : /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
79 : :
80 : : static int omp_workshare_flag;
81 : :
82 : :
83 : : /* True if we are resolving a specification expression. */
84 : : static bool specification_expr = false;
85 : :
86 : : /* The id of the last entry seen. */
87 : : static int current_entry_id;
88 : :
89 : : /* We use bitmaps to determine if a branch target is valid. */
90 : : static bitmap_obstack labels_obstack;
91 : :
92 : : /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
93 : : static bool inquiry_argument = false;
94 : :
95 : :
96 : : /* Is the symbol host associated? */
97 : : static bool
98 : 49156 : is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
99 : : {
100 : 53561 : for (ns = ns->parent; ns; ns = ns->parent)
101 : : {
102 : 4656 : if (sym->ns == ns)
103 : : return true;
104 : : }
105 : :
106 : : return false;
107 : : }
108 : :
109 : : /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
110 : : an ABSTRACT derived-type. If where is not NULL, an error message with that
111 : : locus is printed, optionally using name. */
112 : :
113 : : static bool
114 : 1476455 : resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
115 : : {
116 : 1476455 : if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
117 : : {
118 : 5 : if (where)
119 : : {
120 : 5 : if (name)
121 : 4 : gfc_error ("%qs at %L is of the ABSTRACT type %qs",
122 : : name, where, ts->u.derived->name);
123 : : else
124 : 1 : gfc_error ("ABSTRACT type %qs used at %L",
125 : : ts->u.derived->name, where);
126 : : }
127 : :
128 : 5 : return false;
129 : : }
130 : :
131 : : return true;
132 : : }
133 : :
134 : :
135 : : static bool
136 : 5029 : check_proc_interface (gfc_symbol *ifc, locus *where)
137 : : {
138 : : /* Several checks for F08:C1216. */
139 : 5029 : if (ifc->attr.procedure)
140 : : {
141 : 2 : gfc_error ("Interface %qs at %L is declared "
142 : : "in a later PROCEDURE statement", ifc->name, where);
143 : 2 : return false;
144 : : }
145 : 5027 : if (ifc->generic)
146 : : {
147 : : /* For generic interfaces, check if there is
148 : : a specific procedure with the same name. */
149 : : gfc_interface *gen = ifc->generic;
150 : 12 : while (gen && strcmp (gen->sym->name, ifc->name) != 0)
151 : 5 : gen = gen->next;
152 : 7 : if (!gen)
153 : : {
154 : 4 : gfc_error ("Interface %qs at %L may not be generic",
155 : : ifc->name, where);
156 : 4 : return false;
157 : : }
158 : : }
159 : 5023 : if (ifc->attr.proc == PROC_ST_FUNCTION)
160 : : {
161 : 4 : gfc_error ("Interface %qs at %L may not be a statement function",
162 : : ifc->name, where);
163 : 4 : return false;
164 : : }
165 : 5019 : if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
166 : 5019 : || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
167 : 17 : ifc->attr.intrinsic = 1;
168 : 5019 : if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
169 : : {
170 : 3 : gfc_error ("Intrinsic procedure %qs not allowed in "
171 : : "PROCEDURE statement at %L", ifc->name, where);
172 : 3 : return false;
173 : : }
174 : 5016 : if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
175 : : {
176 : 7 : gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
177 : 7 : return false;
178 : : }
179 : : return true;
180 : : }
181 : :
182 : :
183 : : static void resolve_symbol (gfc_symbol *sym);
184 : :
185 : :
186 : : /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
187 : :
188 : : static bool
189 : 1997 : resolve_procedure_interface (gfc_symbol *sym)
190 : : {
191 : 1997 : gfc_symbol *ifc = sym->ts.interface;
192 : :
193 : 1997 : if (!ifc)
194 : : return true;
195 : :
196 : 1841 : if (ifc == sym)
197 : : {
198 : 2 : gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
199 : : sym->name, &sym->declared_at);
200 : 2 : return false;
201 : : }
202 : 1839 : if (!check_proc_interface (ifc, &sym->declared_at))
203 : : return false;
204 : :
205 : 1830 : if (ifc->attr.if_source || ifc->attr.intrinsic)
206 : : {
207 : : /* Resolve interface and copy attributes. */
208 : 1551 : resolve_symbol (ifc);
209 : 1551 : if (ifc->attr.intrinsic)
210 : 14 : gfc_resolve_intrinsic (ifc, &ifc->declared_at);
211 : :
212 : 1551 : if (ifc->result)
213 : : {
214 : 676 : sym->ts = ifc->result->ts;
215 : 676 : sym->attr.allocatable = ifc->result->attr.allocatable;
216 : 676 : sym->attr.pointer = ifc->result->attr.pointer;
217 : 676 : sym->attr.dimension = ifc->result->attr.dimension;
218 : 676 : sym->attr.class_ok = ifc->result->attr.class_ok;
219 : 676 : sym->as = gfc_copy_array_spec (ifc->result->as);
220 : 676 : sym->result = sym;
221 : : }
222 : : else
223 : : {
224 : 875 : sym->ts = ifc->ts;
225 : 875 : sym->attr.allocatable = ifc->attr.allocatable;
226 : 875 : sym->attr.pointer = ifc->attr.pointer;
227 : 875 : sym->attr.dimension = ifc->attr.dimension;
228 : 875 : sym->attr.class_ok = ifc->attr.class_ok;
229 : 875 : sym->as = gfc_copy_array_spec (ifc->as);
230 : : }
231 : 1551 : sym->ts.interface = ifc;
232 : 1551 : sym->attr.function = ifc->attr.function;
233 : 1551 : sym->attr.subroutine = ifc->attr.subroutine;
234 : :
235 : 1551 : sym->attr.pure = ifc->attr.pure;
236 : 1551 : sym->attr.elemental = ifc->attr.elemental;
237 : 1551 : sym->attr.contiguous = ifc->attr.contiguous;
238 : 1551 : sym->attr.recursive = ifc->attr.recursive;
239 : 1551 : sym->attr.always_explicit = ifc->attr.always_explicit;
240 : 1551 : sym->attr.ext_attr |= ifc->attr.ext_attr;
241 : 1551 : sym->attr.is_bind_c = ifc->attr.is_bind_c;
242 : : /* Copy char length. */
243 : 1551 : if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
244 : : {
245 : 45 : sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
246 : 45 : if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
247 : 53 : && !gfc_resolve_expr (sym->ts.u.cl->length))
248 : : return false;
249 : : }
250 : : }
251 : :
252 : : return true;
253 : : }
254 : :
255 : :
256 : : /* Resolve types of formal argument lists. These have to be done early so that
257 : : the formal argument lists of module procedures can be copied to the
258 : : containing module before the individual procedures are resolved
259 : : individually. We also resolve argument lists of procedures in interface
260 : : blocks because they are self-contained scoping units.
261 : :
262 : : Since a dummy argument cannot be a non-dummy procedure, the only
263 : : resort left for untyped names are the IMPLICIT types. */
264 : :
265 : : void
266 : 508032 : gfc_resolve_formal_arglist (gfc_symbol *proc)
267 : : {
268 : 508032 : gfc_formal_arglist *f;
269 : 508032 : gfc_symbol *sym;
270 : 508032 : bool saved_specification_expr;
271 : 508032 : int i;
272 : :
273 : 508032 : if (proc->result != NULL)
274 : 317127 : sym = proc->result;
275 : : else
276 : : sym = proc;
277 : :
278 : 508032 : if (gfc_elemental (proc)
279 : 346574 : || sym->attr.pointer || sym->attr.allocatable
280 : 843022 : || (sym->as && sym->as->rank != 0))
281 : : {
282 : 175317 : proc->attr.always_explicit = 1;
283 : 175317 : sym->attr.always_explicit = 1;
284 : : }
285 : :
286 : 508032 : gfc_namespace *orig_current_ns = gfc_current_ns;
287 : 508032 : gfc_current_ns = gfc_get_procedure_ns (proc);
288 : :
289 : 1317048 : for (f = proc->formal; f; f = f->next)
290 : : {
291 : 809018 : gfc_array_spec *as;
292 : :
293 : 809018 : sym = f->sym;
294 : :
295 : 809018 : if (sym == NULL)
296 : : {
297 : : /* Alternate return placeholder. */
298 : 171 : if (gfc_elemental (proc))
299 : 1 : gfc_error ("Alternate return specifier in elemental subroutine "
300 : : "%qs at %L is not allowed", proc->name,
301 : : &proc->declared_at);
302 : 171 : if (proc->attr.function)
303 : 1 : gfc_error ("Alternate return specifier in function "
304 : : "%qs at %L is not allowed", proc->name,
305 : : &proc->declared_at);
306 : 171 : continue;
307 : : }
308 : :
309 : 554 : if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
310 : 809401 : && !resolve_procedure_interface (sym))
311 : : break;
312 : :
313 : 808847 : if (strcmp (proc->name, sym->name) == 0)
314 : : {
315 : 2 : gfc_error ("Self-referential argument "
316 : : "%qs at %L is not allowed", sym->name,
317 : : &proc->declared_at);
318 : 2 : break;
319 : : }
320 : :
321 : 808845 : if (sym->attr.if_source != IFSRC_UNKNOWN)
322 : 817 : gfc_resolve_formal_arglist (sym);
323 : :
324 : 808845 : if (sym->attr.subroutine || sym->attr.external)
325 : : {
326 : 819 : if (sym->attr.flavor == FL_UNKNOWN)
327 : 9 : gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
328 : : }
329 : : else
330 : : {
331 : 808026 : if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
332 : 3660 : && (!sym->attr.function || sym->result == sym))
333 : 3622 : gfc_set_default_type (sym, 1, sym->ns);
334 : : }
335 : :
336 : 808845 : as = sym->ts.type == BT_CLASS && sym->attr.class_ok
337 : 822150 : ? CLASS_DATA (sym)->as : sym->as;
338 : :
339 : 808845 : saved_specification_expr = specification_expr;
340 : 808845 : specification_expr = true;
341 : 808845 : gfc_resolve_array_spec (as, 0);
342 : 808845 : specification_expr = saved_specification_expr;
343 : :
344 : : /* We can't tell if an array with dimension (:) is assumed or deferred
345 : : shape until we know if it has the pointer or allocatable attributes.
346 : : */
347 : 808845 : if (as && as->rank > 0 && as->type == AS_DEFERRED
348 : 11984 : && ((sym->ts.type != BT_CLASS
349 : 10915 : && !(sym->attr.pointer || sym->attr.allocatable))
350 : 5238 : || (sym->ts.type == BT_CLASS
351 : 1069 : && !(CLASS_DATA (sym)->attr.class_pointer
352 : 875 : || CLASS_DATA (sym)->attr.allocatable)))
353 : 7207 : && sym->attr.flavor != FL_PROCEDURE)
354 : : {
355 : 7206 : as->type = AS_ASSUMED_SHAPE;
356 : 16753 : for (i = 0; i < as->rank; i++)
357 : 9547 : as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
358 : : }
359 : :
360 : 125954 : if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
361 : 112740 : || (as && as->type == AS_ASSUMED_RANK)
362 : 758595 : || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
363 : 748635 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
364 : 11238 : && (CLASS_DATA (sym)->attr.class_pointer
365 : 10761 : || CLASS_DATA (sym)->attr.allocatable
366 : 9884 : || CLASS_DATA (sym)->attr.target))
367 : 747281 : || sym->attr.optional)
368 : : {
369 : 76360 : proc->attr.always_explicit = 1;
370 : 76360 : if (proc->result)
371 : 35239 : proc->result->attr.always_explicit = 1;
372 : : }
373 : :
374 : : /* If the flavor is unknown at this point, it has to be a variable.
375 : : A procedure specification would have already set the type. */
376 : :
377 : 808845 : if (sym->attr.flavor == FL_UNKNOWN)
378 : 50040 : gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
379 : :
380 : 808845 : if (gfc_pure (proc))
381 : : {
382 : 325689 : if (sym->attr.flavor == FL_PROCEDURE)
383 : : {
384 : : /* F08:C1279. */
385 : 29 : if (!gfc_pure (sym))
386 : : {
387 : 1 : gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
388 : : "also be PURE", sym->name, &sym->declared_at);
389 : 1 : continue;
390 : : }
391 : : }
392 : 325660 : else if (!sym->attr.pointer)
393 : : {
394 : 325652 : if (proc->attr.function && sym->attr.intent != INTENT_IN)
395 : : {
396 : 109 : if (sym->attr.value)
397 : 108 : gfc_notify_std (GFC_STD_F2008, "Argument %qs"
398 : : " of pure function %qs at %L with VALUE "
399 : : "attribute but without INTENT(IN)",
400 : : sym->name, proc->name, &sym->declared_at);
401 : : else
402 : 1 : gfc_error ("Argument %qs of pure function %qs at %L must "
403 : : "be INTENT(IN) or VALUE", sym->name, proc->name,
404 : : &sym->declared_at);
405 : : }
406 : :
407 : 325652 : if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
408 : : {
409 : 159 : if (sym->attr.value)
410 : 159 : gfc_notify_std (GFC_STD_F2008, "Argument %qs"
411 : : " of pure subroutine %qs at %L with VALUE "
412 : : "attribute but without INTENT", sym->name,
413 : : proc->name, &sym->declared_at);
414 : : else
415 : 0 : gfc_error ("Argument %qs of pure subroutine %qs at %L "
416 : : "must have its INTENT specified or have the "
417 : : "VALUE attribute", sym->name, proc->name,
418 : : &sym->declared_at);
419 : : }
420 : : }
421 : :
422 : : /* F08:C1278a. */
423 : 325688 : if (sym->ts.type == BT_CLASS && sym->attr.intent == INTENT_OUT)
424 : : {
425 : 1 : gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L"
426 : : " may not be polymorphic", sym->name, proc->name,
427 : : &sym->declared_at);
428 : 1 : continue;
429 : : }
430 : : }
431 : :
432 : 808843 : if (proc->attr.implicit_pure)
433 : : {
434 : 24197 : if (sym->attr.flavor == FL_PROCEDURE)
435 : : {
436 : 295 : if (!gfc_pure (sym))
437 : 275 : proc->attr.implicit_pure = 0;
438 : : }
439 : 23902 : else if (!sym->attr.pointer)
440 : : {
441 : 23127 : if (proc->attr.function && sym->attr.intent != INTENT_IN
442 : 2662 : && !sym->value)
443 : 2662 : proc->attr.implicit_pure = 0;
444 : :
445 : 23127 : if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
446 : 4108 : && !sym->value)
447 : 4108 : proc->attr.implicit_pure = 0;
448 : : }
449 : : }
450 : :
451 : 808843 : if (gfc_elemental (proc))
452 : : {
453 : : /* F08:C1289. */
454 : 300313 : if (sym->attr.codimension
455 : 300312 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
456 : 895 : && CLASS_DATA (sym)->attr.codimension))
457 : : {
458 : 3 : gfc_error ("Coarray dummy argument %qs at %L to elemental "
459 : : "procedure", sym->name, &sym->declared_at);
460 : 3 : continue;
461 : : }
462 : :
463 : 300310 : if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
464 : 893 : && CLASS_DATA (sym)->as))
465 : : {
466 : 2 : gfc_error ("Argument %qs of elemental procedure at %L must "
467 : : "be scalar", sym->name, &sym->declared_at);
468 : 2 : continue;
469 : : }
470 : :
471 : 300308 : if (sym->attr.allocatable
472 : 300307 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
473 : 892 : && CLASS_DATA (sym)->attr.allocatable))
474 : : {
475 : 2 : gfc_error ("Argument %qs of elemental procedure at %L cannot "
476 : : "have the ALLOCATABLE attribute", sym->name,
477 : : &sym->declared_at);
478 : 2 : continue;
479 : : }
480 : :
481 : 300306 : if (sym->attr.pointer
482 : 300305 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
483 : 891 : && CLASS_DATA (sym)->attr.class_pointer))
484 : : {
485 : 2 : gfc_error ("Argument %qs of elemental procedure at %L cannot "
486 : : "have the POINTER attribute", sym->name,
487 : : &sym->declared_at);
488 : 2 : continue;
489 : : }
490 : :
491 : 300304 : if (sym->attr.flavor == FL_PROCEDURE)
492 : : {
493 : 2 : gfc_error ("Dummy procedure %qs not allowed in elemental "
494 : : "procedure %qs at %L", sym->name, proc->name,
495 : : &sym->declared_at);
496 : 2 : continue;
497 : : }
498 : :
499 : : /* Fortran 2008 Corrigendum 1, C1290a. */
500 : 300302 : if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
501 : : {
502 : 2 : gfc_error ("Argument %qs of elemental procedure %qs at %L must "
503 : : "have its INTENT specified or have the VALUE "
504 : : "attribute", sym->name, proc->name,
505 : : &sym->declared_at);
506 : 2 : continue;
507 : : }
508 : : }
509 : :
510 : : /* Each dummy shall be specified to be scalar. */
511 : 808830 : if (proc->attr.proc == PROC_ST_FUNCTION)
512 : : {
513 : 305 : if (sym->as != NULL)
514 : : {
515 : : /* F03:C1263 (R1238) The function-name and each dummy-arg-name
516 : : shall be specified, explicitly or implicitly, to be scalar. */
517 : 1 : gfc_error ("Argument %qs of statement function %qs at %L "
518 : : "must be scalar", sym->name, proc->name,
519 : : &proc->declared_at);
520 : 1 : continue;
521 : : }
522 : :
523 : 304 : if (sym->ts.type == BT_CHARACTER)
524 : : {
525 : 48 : gfc_charlen *cl = sym->ts.u.cl;
526 : 48 : if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
527 : : {
528 : 0 : gfc_error ("Character-valued argument %qs of statement "
529 : : "function at %L must have constant length",
530 : : sym->name, &sym->declared_at);
531 : 0 : continue;
532 : : }
533 : : }
534 : : }
535 : : }
536 : 508032 : if (sym)
537 : 507940 : sym->formal_resolved = 1;
538 : 508032 : gfc_current_ns = orig_current_ns;
539 : 508032 : }
540 : :
541 : :
542 : : /* Work function called when searching for symbols that have argument lists
543 : : associated with them. */
544 : :
545 : : static void
546 : 1767030 : find_arglists (gfc_symbol *sym)
547 : : {
548 : 1767030 : if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
549 : 320997 : || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
550 : : return;
551 : :
552 : 319333 : gfc_resolve_formal_arglist (sym);
553 : : }
554 : :
555 : :
556 : : /* Given a namespace, resolve all formal argument lists within the namespace.
557 : : */
558 : :
559 : : static void
560 : 335260 : resolve_formal_arglists (gfc_namespace *ns)
561 : : {
562 : 0 : if (ns == NULL)
563 : : return;
564 : :
565 : 335260 : gfc_traverse_ns (ns, find_arglists);
566 : : }
567 : :
568 : :
569 : : static void
570 : 35967 : resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
571 : : {
572 : 35967 : bool t;
573 : :
574 : 35967 : if (sym && sym->attr.flavor == FL_PROCEDURE
575 : 35967 : && sym->ns->parent
576 : 1062 : && sym->ns->parent->proc_name
577 : 1062 : && sym->ns->parent->proc_name->attr.flavor == FL_PROCEDURE
578 : 1 : && !strcmp (sym->name, sym->ns->parent->proc_name->name))
579 : 0 : gfc_error ("Contained procedure %qs at %L has the same name as its "
580 : : "encompassing procedure", sym->name, &sym->declared_at);
581 : :
582 : : /* If this namespace is not a function or an entry master function,
583 : : ignore it. */
584 : 35967 : if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
585 : 10529 : || sym->attr.entry_master)
586 : 25626 : return;
587 : :
588 : 10341 : if (!sym->result)
589 : : return;
590 : :
591 : : /* Try to find out of what the return type is. */
592 : 10341 : if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
593 : : {
594 : 55 : t = gfc_set_default_type (sym->result, 0, ns);
595 : :
596 : 55 : if (!t && !sym->result->attr.untyped)
597 : : {
598 : 19 : if (sym->result == sym)
599 : 1 : gfc_error ("Contained function %qs at %L has no IMPLICIT type",
600 : : sym->name, &sym->declared_at);
601 : 18 : else if (!sym->result->attr.proc_pointer)
602 : 0 : gfc_error ("Result %qs of contained function %qs at %L has "
603 : : "no IMPLICIT type", sym->result->name, sym->name,
604 : : &sym->result->declared_at);
605 : 19 : sym->result->attr.untyped = 1;
606 : : }
607 : : }
608 : :
609 : : /* Fortran 2008 Draft Standard, page 535, C418, on type-param-value
610 : : type, lists the only ways a character length value of * can be used:
611 : : dummy arguments of procedures, named constants, function results and
612 : : in allocate statements if the allocate_object is an assumed length dummy
613 : : in external functions. Internal function results and results of module
614 : : procedures are not on this list, ergo, not permitted. */
615 : :
616 : 10341 : if (sym->result->ts.type == BT_CHARACTER)
617 : : {
618 : 1152 : gfc_charlen *cl = sym->result->ts.u.cl;
619 : 1152 : if ((!cl || !cl->length) && !sym->result->ts.deferred)
620 : : {
621 : : /* See if this is a module-procedure and adapt error message
622 : : accordingly. */
623 : 4 : bool module_proc;
624 : 4 : gcc_assert (ns->parent && ns->parent->proc_name);
625 : 4 : module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
626 : :
627 : 7 : gfc_error (module_proc
628 : : ? G_("Character-valued module procedure %qs at %L"
629 : : " must not be assumed length")
630 : : : G_("Character-valued internal function %qs at %L"
631 : : " must not be assumed length"),
632 : : sym->name, &sym->declared_at);
633 : : }
634 : : }
635 : : }
636 : :
637 : :
638 : : /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
639 : : introduce duplicates. */
640 : :
641 : : static void
642 : 1420 : merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
643 : : {
644 : 1420 : gfc_formal_arglist *f, *new_arglist;
645 : 1420 : gfc_symbol *new_sym;
646 : :
647 : 2561 : for (; new_args != NULL; new_args = new_args->next)
648 : : {
649 : 1141 : new_sym = new_args->sym;
650 : : /* See if this arg is already in the formal argument list. */
651 : 2165 : for (f = proc->formal; f; f = f->next)
652 : : {
653 : 1470 : if (new_sym == f->sym)
654 : : break;
655 : : }
656 : :
657 : 1141 : if (f)
658 : 446 : continue;
659 : :
660 : : /* Add a new argument. Argument order is not important. */
661 : 695 : new_arglist = gfc_get_formal_arglist ();
662 : 695 : new_arglist->sym = new_sym;
663 : 695 : new_arglist->next = proc->formal;
664 : 695 : proc->formal = new_arglist;
665 : : }
666 : 1420 : }
667 : :
668 : :
669 : : /* Flag the arguments that are not present in all entries. */
670 : :
671 : : static void
672 : 1420 : check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
673 : : {
674 : 1420 : gfc_formal_arglist *f, *head;
675 : 1420 : head = new_args;
676 : :
677 : 2994 : for (f = proc->formal; f; f = f->next)
678 : : {
679 : 1574 : if (f->sym == NULL)
680 : 36 : continue;
681 : :
682 : 2704 : for (new_args = head; new_args; new_args = new_args->next)
683 : : {
684 : 2262 : if (new_args->sym == f->sym)
685 : : break;
686 : : }
687 : :
688 : 1538 : if (new_args)
689 : 1096 : continue;
690 : :
691 : 442 : f->sym->attr.not_always_present = 1;
692 : : }
693 : 1420 : }
694 : :
695 : :
696 : : /* Resolve alternate entry points. If a symbol has multiple entry points we
697 : : create a new master symbol for the main routine, and turn the existing
698 : : symbol into an entry point. */
699 : :
700 : : static void
701 : 370722 : resolve_entries (gfc_namespace *ns)
702 : : {
703 : 370722 : gfc_namespace *old_ns;
704 : 370722 : gfc_code *c;
705 : 370722 : gfc_symbol *proc;
706 : 370722 : gfc_entry_list *el;
707 : : /* Provide sufficient space to hold "master.%d.%s". */
708 : 370722 : char name[GFC_MAX_SYMBOL_LEN + 1 + 18];
709 : 370722 : static int master_count = 0;
710 : :
711 : 370722 : if (ns->proc_name == NULL)
712 : 370054 : return;
713 : :
714 : : /* No need to do anything if this procedure doesn't have alternate entry
715 : : points. */
716 : 370674 : if (!ns->entries)
717 : : return;
718 : :
719 : : /* We may already have resolved alternate entry points. */
720 : 918 : if (ns->proc_name->attr.entry_master)
721 : : return;
722 : :
723 : : /* If this isn't a procedure something has gone horribly wrong. */
724 : 668 : gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
725 : :
726 : : /* Remember the current namespace. */
727 : 668 : old_ns = gfc_current_ns;
728 : :
729 : 668 : gfc_current_ns = ns;
730 : :
731 : : /* Add the main entry point to the list of entry points. */
732 : 668 : el = gfc_get_entry_list ();
733 : 668 : el->sym = ns->proc_name;
734 : 668 : el->id = 0;
735 : 668 : el->next = ns->entries;
736 : 668 : ns->entries = el;
737 : 668 : ns->proc_name->attr.entry = 1;
738 : :
739 : : /* If it is a module function, it needs to be in the right namespace
740 : : so that gfc_get_fake_result_decl can gather up the results. The
741 : : need for this arose in get_proc_name, where these beasts were
742 : : left in their own namespace, to keep prior references linked to
743 : : the entry declaration.*/
744 : 668 : if (ns->proc_name->attr.function
745 : 564 : && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
746 : 188 : el->sym->ns = ns;
747 : :
748 : : /* Do the same for entries where the master is not a module
749 : : procedure. These are retained in the module namespace because
750 : : of the module procedure declaration. */
751 : 1420 : for (el = el->next; el; el = el->next)
752 : 752 : if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
753 : 0 : && el->sym->attr.mod_proc)
754 : 0 : el->sym->ns = ns;
755 : 668 : el = ns->entries;
756 : :
757 : : /* Add an entry statement for it. */
758 : 668 : c = gfc_get_code (EXEC_ENTRY);
759 : 668 : c->ext.entry = el;
760 : 668 : c->next = ns->code;
761 : 668 : ns->code = c;
762 : :
763 : : /* Create a new symbol for the master function. */
764 : : /* Give the internal function a unique name (within this file).
765 : : Also include the function name so the user has some hope of figuring
766 : : out what is going on. */
767 : 668 : snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
768 : 668 : master_count++, ns->proc_name->name);
769 : 668 : gfc_get_ha_symbol (name, &proc);
770 : 668 : gcc_assert (proc != NULL);
771 : :
772 : 668 : gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
773 : 668 : if (ns->proc_name->attr.subroutine)
774 : 104 : gfc_add_subroutine (&proc->attr, proc->name, NULL);
775 : : else
776 : : {
777 : 564 : gfc_symbol *sym;
778 : 564 : gfc_typespec *ts, *fts;
779 : 564 : gfc_array_spec *as, *fas;
780 : 564 : gfc_add_function (&proc->attr, proc->name, NULL);
781 : 564 : proc->result = proc;
782 : 564 : fas = ns->entries->sym->as;
783 : 564 : fas = fas ? fas : ns->entries->sym->result->as;
784 : 564 : fts = &ns->entries->sym->result->ts;
785 : 564 : if (fts->type == BT_UNKNOWN)
786 : 51 : fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
787 : 1058 : for (el = ns->entries->next; el; el = el->next)
788 : : {
789 : 603 : ts = &el->sym->result->ts;
790 : 603 : as = el->sym->as;
791 : 603 : as = as ? as : el->sym->result->as;
792 : 603 : if (ts->type == BT_UNKNOWN)
793 : 61 : ts = gfc_get_default_type (el->sym->result->name, NULL);
794 : :
795 : 603 : if (! gfc_compare_types (ts, fts)
796 : 497 : || (el->sym->result->attr.dimension
797 : 497 : != ns->entries->sym->result->attr.dimension)
798 : 603 : || (el->sym->result->attr.pointer
799 : 497 : != ns->entries->sym->result->attr.pointer))
800 : : break;
801 : 65 : else if (as && fas && ns->entries->sym->result != el->sym->result
802 : 559 : && gfc_compare_array_spec (as, fas) == 0)
803 : 5 : gfc_error ("Function %s at %L has entries with mismatched "
804 : : "array specifications", ns->entries->sym->name,
805 : 5 : &ns->entries->sym->declared_at);
806 : : /* The characteristics need to match and thus both need to have
807 : : the same string length, i.e. both len=*, or both len=4.
808 : : Having both len=<variable> is also possible, but difficult to
809 : : check at compile time. */
810 : 492 : else if (ts->type == BT_CHARACTER
811 : 89 : && (el->sym->result->attr.allocatable
812 : 89 : != ns->entries->sym->result->attr.allocatable))
813 : : {
814 : 3 : gfc_error ("Function %s at %L has entry %s with mismatched "
815 : : "characteristics", ns->entries->sym->name,
816 : : &ns->entries->sym->declared_at, el->sym->name);
817 : 3 : goto cleanup;
818 : : }
819 : 489 : else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
820 : 86 : && (((ts->u.cl->length && !fts->u.cl->length)
821 : 85 : ||(!ts->u.cl->length && fts->u.cl->length))
822 : 66 : || (ts->u.cl->length
823 : 29 : && ts->u.cl->length->expr_type
824 : 29 : != fts->u.cl->length->expr_type)
825 : 66 : || (ts->u.cl->length
826 : 29 : && ts->u.cl->length->expr_type == EXPR_CONSTANT
827 : 28 : && mpz_cmp (ts->u.cl->length->value.integer,
828 : 28 : fts->u.cl->length->value.integer) != 0)))
829 : 21 : gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
830 : : "entries returning variables of different "
831 : : "string lengths", ns->entries->sym->name,
832 : 21 : &ns->entries->sym->declared_at);
833 : 468 : else if (el->sym->result->attr.allocatable
834 : 468 : != ns->entries->sym->result->attr.allocatable)
835 : : break;
836 : : }
837 : :
838 : 561 : if (el == NULL)
839 : : {
840 : 455 : sym = ns->entries->sym->result;
841 : : /* All result types the same. */
842 : 455 : proc->ts = *fts;
843 : 455 : if (sym->attr.dimension)
844 : 63 : gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
845 : 455 : if (sym->attr.pointer)
846 : 78 : gfc_add_pointer (&proc->attr, NULL);
847 : 455 : if (sym->attr.allocatable)
848 : 24 : gfc_add_allocatable (&proc->attr, NULL);
849 : : }
850 : : else
851 : : {
852 : : /* Otherwise the result will be passed through a union by
853 : : reference. */
854 : 106 : proc->attr.mixed_entry_master = 1;
855 : 340 : for (el = ns->entries; el; el = el->next)
856 : : {
857 : 234 : sym = el->sym->result;
858 : 234 : if (sym->attr.dimension)
859 : : {
860 : 1 : if (el == ns->entries)
861 : 0 : gfc_error ("FUNCTION result %s cannot be an array in "
862 : : "FUNCTION %s at %L", sym->name,
863 : 0 : ns->entries->sym->name, &sym->declared_at);
864 : : else
865 : 1 : gfc_error ("ENTRY result %s cannot be an array in "
866 : : "FUNCTION %s at %L", sym->name,
867 : 1 : ns->entries->sym->name, &sym->declared_at);
868 : : }
869 : 233 : else if (sym->attr.pointer)
870 : : {
871 : 1 : if (el == ns->entries)
872 : 1 : gfc_error ("FUNCTION result %s cannot be a POINTER in "
873 : : "FUNCTION %s at %L", sym->name,
874 : 1 : ns->entries->sym->name, &sym->declared_at);
875 : : else
876 : 0 : gfc_error ("ENTRY result %s cannot be a POINTER in "
877 : : "FUNCTION %s at %L", sym->name,
878 : 0 : ns->entries->sym->name, &sym->declared_at);
879 : : }
880 : 232 : else if (sym->attr.allocatable)
881 : : {
882 : 0 : if (el == ns->entries)
883 : 0 : gfc_error ("FUNCTION result %s cannot be ALLOCATABLE in "
884 : : "FUNCTION %s at %L", sym->name,
885 : 0 : ns->entries->sym->name, &sym->declared_at);
886 : : else
887 : 0 : gfc_error ("ENTRY result %s cannot be ALLOCATABLE in "
888 : : "FUNCTION %s at %L", sym->name,
889 : 0 : ns->entries->sym->name, &sym->declared_at);
890 : : }
891 : : else
892 : : {
893 : 232 : ts = &sym->ts;
894 : 232 : if (ts->type == BT_UNKNOWN)
895 : 9 : ts = gfc_get_default_type (sym->name, NULL);
896 : 232 : switch (ts->type)
897 : : {
898 : 84 : case BT_INTEGER:
899 : 84 : if (ts->kind == gfc_default_integer_kind)
900 : : sym = NULL;
901 : : break;
902 : 99 : case BT_REAL:
903 : 99 : if (ts->kind == gfc_default_real_kind
904 : 18 : || ts->kind == gfc_default_double_kind)
905 : : sym = NULL;
906 : : break;
907 : 19 : case BT_COMPLEX:
908 : 19 : if (ts->kind == gfc_default_complex_kind)
909 : : sym = NULL;
910 : : break;
911 : 27 : case BT_LOGICAL:
912 : 27 : if (ts->kind == gfc_default_logical_kind)
913 : : sym = NULL;
914 : : break;
915 : : case BT_UNKNOWN:
916 : : /* We will issue error elsewhere. */
917 : : sym = NULL;
918 : : break;
919 : : default:
920 : : break;
921 : : }
922 : 3 : if (sym)
923 : : {
924 : 3 : if (el == ns->entries)
925 : 1 : gfc_error ("FUNCTION result %s cannot be of type %s "
926 : : "in FUNCTION %s at %L", sym->name,
927 : 1 : gfc_typename (ts), ns->entries->sym->name,
928 : : &sym->declared_at);
929 : : else
930 : 2 : gfc_error ("ENTRY result %s cannot be of type %s "
931 : : "in FUNCTION %s at %L", sym->name,
932 : 2 : gfc_typename (ts), ns->entries->sym->name,
933 : : &sym->declared_at);
934 : : }
935 : : }
936 : : }
937 : : }
938 : : }
939 : :
940 : 106 : cleanup:
941 : 668 : proc->attr.access = ACCESS_PRIVATE;
942 : 668 : proc->attr.entry_master = 1;
943 : :
944 : : /* Merge all the entry point arguments. */
945 : 2088 : for (el = ns->entries; el; el = el->next)
946 : 1420 : merge_argument_lists (proc, el->sym->formal);
947 : :
948 : : /* Check the master formal arguments for any that are not
949 : : present in all entry points. */
950 : 2088 : for (el = ns->entries; el; el = el->next)
951 : 1420 : check_argument_lists (proc, el->sym->formal);
952 : :
953 : : /* Use the master function for the function body. */
954 : 668 : ns->proc_name = proc;
955 : :
956 : : /* Finalize the new symbols. */
957 : 668 : gfc_commit_symbols ();
958 : :
959 : : /* Restore the original namespace. */
960 : 668 : gfc_current_ns = old_ns;
961 : : }
962 : :
963 : :
964 : : /* Forward declaration. */
965 : : static bool is_non_constant_shape_array (gfc_symbol *sym);
966 : :
967 : :
968 : : /* Resolve common variables. */
969 : : static void
970 : 337157 : resolve_common_vars (gfc_common_head *common_block, bool named_common)
971 : : {
972 : 337157 : gfc_symbol *csym = common_block->head;
973 : 337157 : gfc_gsymbol *gsym;
974 : :
975 : 343128 : for (; csym; csym = csym->common_next)
976 : : {
977 : 5971 : gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
978 : 5971 : if (gsym && (gsym->type == GSYM_MODULE || gsym->type == GSYM_PROGRAM))
979 : : {
980 : 3 : if (csym->common_block)
981 : 2 : gfc_error_now ("Global entity %qs at %L cannot appear in a "
982 : : "COMMON block at %L", gsym->name,
983 : : &gsym->where, &csym->common_block->where);
984 : : else
985 : 1 : gfc_error_now ("Global entity %qs at %L cannot appear in a "
986 : : "COMMON block", gsym->name, &gsym->where);
987 : : }
988 : :
989 : : /* gfc_add_in_common may have been called before, but the reported errors
990 : : have been ignored to continue parsing.
991 : : We do the checks again here, unless the symbol is USE associated. */
992 : 5971 : if (!csym->attr.use_assoc && !csym->attr.used_in_submodule)
993 : : {
994 : 5698 : gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
995 : 5698 : gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
996 : : &common_block->where);
997 : : }
998 : :
999 : 5971 : if (csym->value || csym->attr.data)
1000 : : {
1001 : 132 : if (!csym->ns->is_block_data)
1002 : 32 : gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
1003 : : "but only in BLOCK DATA initialization is "
1004 : : "allowed", csym->name, &csym->declared_at);
1005 : 100 : else if (!named_common)
1006 : 8 : gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
1007 : : "in a blank COMMON but initialization is only "
1008 : : "allowed in named common blocks", csym->name,
1009 : : &csym->declared_at);
1010 : : }
1011 : :
1012 : 5971 : if (UNLIMITED_POLY (csym))
1013 : 1 : gfc_error_now ("%qs at %L cannot appear in COMMON "
1014 : : "[F2008:C5100]", csym->name, &csym->declared_at);
1015 : :
1016 : 5971 : if (csym->attr.dimension && is_non_constant_shape_array (csym))
1017 : : {
1018 : 1 : gfc_error_now ("Automatic object %qs at %L cannot appear in "
1019 : : "COMMON at %L", csym->name, &csym->declared_at,
1020 : : &common_block->where);
1021 : : /* Avoid confusing follow-on error. */
1022 : 1 : csym->error = 1;
1023 : : }
1024 : :
1025 : 5971 : if (csym->ts.type != BT_DERIVED)
1026 : 5924 : continue;
1027 : :
1028 : 47 : if (!(csym->ts.u.derived->attr.sequence
1029 : 3 : || csym->ts.u.derived->attr.is_bind_c))
1030 : 2 : gfc_error_now ("Derived type variable %qs in COMMON at %L "
1031 : : "has neither the SEQUENCE nor the BIND(C) "
1032 : : "attribute", csym->name, &csym->declared_at);
1033 : 47 : if (csym->ts.u.derived->attr.alloc_comp)
1034 : 3 : gfc_error_now ("Derived type variable %qs in COMMON at %L "
1035 : : "has an ultimate component that is "
1036 : : "allocatable", csym->name, &csym->declared_at);
1037 : 47 : if (gfc_has_default_initializer (csym->ts.u.derived))
1038 : 2 : gfc_error_now ("Derived type variable %qs in COMMON at %L "
1039 : : "may not have default initializer", csym->name,
1040 : : &csym->declared_at);
1041 : :
1042 : 47 : if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
1043 : 16 : gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
1044 : : }
1045 : 337157 : }
1046 : :
1047 : : /* Resolve common blocks. */
1048 : : static void
1049 : 335725 : resolve_common_blocks (gfc_symtree *common_root)
1050 : : {
1051 : 335725 : gfc_symbol *sym = NULL;
1052 : 335725 : gfc_gsymbol * gsym;
1053 : :
1054 : 335725 : if (common_root == NULL)
1055 : 335603 : return;
1056 : :
1057 : 1897 : if (common_root->left)
1058 : 213 : resolve_common_blocks (common_root->left);
1059 : 1897 : if (common_root->right)
1060 : 252 : resolve_common_blocks (common_root->right);
1061 : :
1062 : 1897 : resolve_common_vars (common_root->n.common, true);
1063 : :
1064 : : /* The common name is a global name - in Fortran 2003 also if it has a
1065 : : C binding name, since Fortran 2008 only the C binding name is a global
1066 : : identifier. */
1067 : 1897 : if (!common_root->n.common->binding_label
1068 : 1897 : || gfc_notification_std (GFC_STD_F2008))
1069 : : {
1070 : 3650 : gsym = gfc_find_gsymbol (gfc_gsym_root,
1071 : 1825 : common_root->n.common->name);
1072 : :
1073 : 820 : if (gsym && gfc_notification_std (GFC_STD_F2008)
1074 : 14 : && gsym->type == GSYM_COMMON
1075 : 1838 : && ((common_root->n.common->binding_label
1076 : 6 : && (!gsym->binding_label
1077 : 0 : || strcmp (common_root->n.common->binding_label,
1078 : : gsym->binding_label) != 0))
1079 : 7 : || (!common_root->n.common->binding_label
1080 : 7 : && gsym->binding_label)))
1081 : : {
1082 : 6 : gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
1083 : : "identifier and must thus have the same binding name "
1084 : : "as the same-named COMMON block at %L: %s vs %s",
1085 : 6 : common_root->n.common->name, &common_root->n.common->where,
1086 : : &gsym->where,
1087 : : common_root->n.common->binding_label
1088 : : ? common_root->n.common->binding_label : "(blank)",
1089 : 6 : gsym->binding_label ? gsym->binding_label : "(blank)");
1090 : 6 : return;
1091 : : }
1092 : :
1093 : 1819 : if (gsym && gsym->type != GSYM_COMMON
1094 : 1 : && !common_root->n.common->binding_label)
1095 : : {
1096 : 0 : gfc_error ("COMMON block %qs at %L uses the same global identifier "
1097 : : "as entity at %L",
1098 : 0 : common_root->n.common->name, &common_root->n.common->where,
1099 : : &gsym->where);
1100 : 0 : return;
1101 : : }
1102 : 814 : if (gsym && gsym->type != GSYM_COMMON)
1103 : : {
1104 : 1 : gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
1105 : : "%L sharing the identifier with global non-COMMON-block "
1106 : 1 : "entity at %L", common_root->n.common->name,
1107 : 1 : &common_root->n.common->where, &gsym->where);
1108 : 1 : return;
1109 : : }
1110 : 1005 : if (!gsym)
1111 : : {
1112 : 1005 : gsym = gfc_get_gsymbol (common_root->n.common->name, false);
1113 : 1005 : gsym->type = GSYM_COMMON;
1114 : 1005 : gsym->where = common_root->n.common->where;
1115 : 1005 : gsym->defined = 1;
1116 : : }
1117 : 1818 : gsym->used = 1;
1118 : : }
1119 : :
1120 : 1890 : if (common_root->n.common->binding_label)
1121 : : {
1122 : 76 : gsym = gfc_find_gsymbol (gfc_gsym_root,
1123 : : common_root->n.common->binding_label);
1124 : 76 : if (gsym && gsym->type != GSYM_COMMON)
1125 : : {
1126 : 1 : gfc_error ("COMMON block at %L with binding label %qs uses the same "
1127 : : "global identifier as entity at %L",
1128 : : &common_root->n.common->where,
1129 : 1 : common_root->n.common->binding_label, &gsym->where);
1130 : 1 : return;
1131 : : }
1132 : 57 : if (!gsym)
1133 : : {
1134 : 57 : gsym = gfc_get_gsymbol (common_root->n.common->binding_label, true);
1135 : 57 : gsym->type = GSYM_COMMON;
1136 : 57 : gsym->where = common_root->n.common->where;
1137 : 57 : gsym->defined = 1;
1138 : : }
1139 : 75 : gsym->used = 1;
1140 : : }
1141 : :
1142 : 1889 : gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1143 : 1889 : if (sym == NULL)
1144 : : return;
1145 : :
1146 : 122 : if (sym->attr.flavor == FL_PARAMETER)
1147 : 2 : gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
1148 : 2 : sym->name, &common_root->n.common->where, &sym->declared_at);
1149 : :
1150 : 122 : if (sym->attr.external)
1151 : 1 : gfc_error ("COMMON block %qs at %L cannot have the EXTERNAL attribute",
1152 : 1 : sym->name, &common_root->n.common->where);
1153 : :
1154 : 122 : if (sym->attr.intrinsic)
1155 : 2 : gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
1156 : 2 : sym->name, &common_root->n.common->where);
1157 : 120 : else if (sym->attr.result
1158 : 120 : || gfc_is_function_return_value (sym, gfc_current_ns))
1159 : 1 : gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1160 : : "that is also a function result", sym->name,
1161 : 1 : &common_root->n.common->where);
1162 : 119 : else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1163 : 5 : && sym->attr.proc != PROC_ST_FUNCTION)
1164 : 3 : gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1165 : : "that is also a global procedure", sym->name,
1166 : 3 : &common_root->n.common->where);
1167 : : }
1168 : :
1169 : :
1170 : : /* Resolve contained function types. Because contained functions can call one
1171 : : another, they have to be worked out before any of the contained procedures
1172 : : can be resolved.
1173 : :
1174 : : The good news is that if a function doesn't already have a type, the only
1175 : : way it can get one is through an IMPLICIT type or a RESULT variable, because
1176 : : by definition contained functions are contained namespace they're contained
1177 : : in, not in a sibling or parent namespace. */
1178 : :
1179 : : static void
1180 : 335260 : resolve_contained_functions (gfc_namespace *ns)
1181 : : {
1182 : 335260 : gfc_namespace *child;
1183 : 335260 : gfc_entry_list *el;
1184 : :
1185 : 335260 : resolve_formal_arglists (ns);
1186 : :
1187 : 370722 : for (child = ns->contained; child; child = child->sibling)
1188 : : {
1189 : : /* Resolve alternate entry points first. */
1190 : 35462 : resolve_entries (child);
1191 : :
1192 : : /* Then check function return types. */
1193 : 35462 : resolve_contained_fntype (child->proc_name, child);
1194 : 35967 : for (el = child->entries; el; el = el->next)
1195 : 505 : resolve_contained_fntype (el->sym, child);
1196 : : }
1197 : 335260 : }
1198 : :
1199 : :
1200 : :
1201 : : /* A Parameterized Derived Type constructor must contain values for
1202 : : the PDT KIND parameters or they must have a default initializer.
1203 : : Go through the constructor picking out the KIND expressions,
1204 : : storing them in 'param_list' and then call gfc_get_pdt_instance
1205 : : to obtain the PDT instance. */
1206 : :
1207 : : static gfc_actual_arglist *param_list, *param_tail, *param;
1208 : :
1209 : : static bool
1210 : 54 : get_pdt_spec_expr (gfc_component *c, gfc_expr *expr)
1211 : : {
1212 : 54 : param = gfc_get_actual_arglist ();
1213 : 54 : if (!param_list)
1214 : 48 : param_list = param_tail = param;
1215 : : else
1216 : : {
1217 : 6 : param_tail->next = param;
1218 : 6 : param_tail = param_tail->next;
1219 : : }
1220 : :
1221 : 54 : param_tail->name = c->name;
1222 : 54 : if (expr)
1223 : 54 : param_tail->expr = gfc_copy_expr (expr);
1224 : 0 : else if (c->initializer)
1225 : 0 : param_tail->expr = gfc_copy_expr (c->initializer);
1226 : : else
1227 : : {
1228 : 0 : param_tail->spec_type = SPEC_ASSUMED;
1229 : 0 : if (c->attr.pdt_kind)
1230 : : {
1231 : 0 : gfc_error ("The KIND parameter %qs in the PDT constructor "
1232 : : "at %C has no value", param->name);
1233 : 0 : return false;
1234 : : }
1235 : : }
1236 : :
1237 : : return true;
1238 : : }
1239 : :
1240 : : static bool
1241 : 48 : get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr,
1242 : : gfc_symbol *derived)
1243 : : {
1244 : 48 : gfc_constructor *cons = NULL;
1245 : 48 : gfc_component *comp;
1246 : 48 : bool t = true;
1247 : :
1248 : 48 : if (expr && expr->expr_type == EXPR_STRUCTURE)
1249 : 48 : cons = gfc_constructor_first (expr->value.constructor);
1250 : 0 : else if (constr)
1251 : 0 : cons = *constr;
1252 : 48 : gcc_assert (cons);
1253 : :
1254 : 48 : comp = derived->components;
1255 : :
1256 : 156 : for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1257 : : {
1258 : 108 : if (cons->expr
1259 : 108 : && cons->expr->expr_type == EXPR_STRUCTURE
1260 : 0 : && comp->ts.type == BT_DERIVED)
1261 : : {
1262 : 0 : t = get_pdt_constructor (cons->expr, NULL, comp->ts.u.derived);
1263 : 0 : if (!t)
1264 : : return t;
1265 : : }
1266 : 108 : else if (comp->ts.type == BT_DERIVED)
1267 : : {
1268 : 0 : t = get_pdt_constructor (NULL, &cons, comp->ts.u.derived);
1269 : 0 : if (!t)
1270 : : return t;
1271 : : }
1272 : 108 : else if ((comp->attr.pdt_kind || comp->attr.pdt_len)
1273 : 54 : && derived->attr.pdt_template)
1274 : : {
1275 : 54 : t = get_pdt_spec_expr (comp, cons->expr);
1276 : 54 : if (!t)
1277 : : return t;
1278 : : }
1279 : : }
1280 : : return t;
1281 : : }
1282 : :
1283 : :
1284 : : static bool resolve_fl_derived0 (gfc_symbol *sym);
1285 : : static bool resolve_fl_struct (gfc_symbol *sym);
1286 : :
1287 : :
1288 : : /* Resolve all of the elements of a structure constructor and make sure that
1289 : : the types are correct. The 'init' flag indicates that the given
1290 : : constructor is an initializer. */
1291 : :
1292 : : static bool
1293 : 60033 : resolve_structure_cons (gfc_expr *expr, int init)
1294 : : {
1295 : 60033 : gfc_constructor *cons;
1296 : 60033 : gfc_component *comp;
1297 : 60033 : bool t;
1298 : 60033 : symbol_attribute a;
1299 : :
1300 : 60033 : t = true;
1301 : :
1302 : 60033 : if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
1303 : : {
1304 : 57241 : if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
1305 : 57091 : resolve_fl_derived0 (expr->ts.u.derived);
1306 : : else
1307 : 150 : resolve_fl_struct (expr->ts.u.derived);
1308 : :
1309 : : /* If this is a Parameterized Derived Type template, find the
1310 : : instance corresponding to the PDT kind parameters. */
1311 : 57241 : if (expr->ts.u.derived->attr.pdt_template)
1312 : : {
1313 : 48 : param_list = NULL;
1314 : 48 : t = get_pdt_constructor (expr, NULL, expr->ts.u.derived);
1315 : 48 : if (!t)
1316 : : return t;
1317 : 48 : gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL);
1318 : :
1319 : 48 : expr->param_list = gfc_copy_actual_arglist (param_list);
1320 : :
1321 : 48 : if (param_list)
1322 : 48 : gfc_free_actual_arglist (param_list);
1323 : :
1324 : 48 : if (!expr->ts.u.derived->attr.pdt_type)
1325 : : return false;
1326 : : }
1327 : : }
1328 : :
1329 : : /* A constructor may have references if it is the result of substituting a
1330 : : parameter variable. In this case we just pull out the component we
1331 : : want. */
1332 : 60033 : if (expr->ref)
1333 : 160 : comp = expr->ref->u.c.sym->components;
1334 : 59873 : else if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS
1335 : : || expr->ts.type == BT_UNION)
1336 : 59871 : && expr->ts.u.derived)
1337 : 59871 : comp = expr->ts.u.derived->components;
1338 : : else
1339 : : return false;
1340 : :
1341 : 60031 : cons = gfc_constructor_first (expr->value.constructor);
1342 : :
1343 : 198044 : for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1344 : : {
1345 : 138015 : int rank;
1346 : :
1347 : 138015 : if (!cons->expr)
1348 : 9219 : continue;
1349 : :
1350 : : /* Unions use an EXPR_NULL contrived expression to tell the translation
1351 : : phase to generate an initializer of the appropriate length.
1352 : : Ignore it here. */
1353 : 128796 : if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
1354 : 15 : continue;
1355 : :
1356 : 128781 : if (!gfc_resolve_expr (cons->expr))
1357 : : {
1358 : 0 : t = false;
1359 : 0 : continue;
1360 : : }
1361 : :
1362 : 128781 : rank = comp->as ? comp->as->rank : 0;
1363 : 128781 : if (comp->ts.type == BT_CLASS
1364 : 1729 : && !comp->ts.u.derived->attr.unlimited_polymorphic
1365 : 1728 : && CLASS_DATA (comp)->as)
1366 : 511 : rank = CLASS_DATA (comp)->as->rank;
1367 : :
1368 : 128781 : if (comp->ts.type == BT_CLASS && cons->expr->ts.type != BT_CLASS)
1369 : 215 : gfc_find_vtab (&cons->expr->ts);
1370 : :
1371 : 128781 : if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1372 : 452 : && (comp->attr.allocatable || comp->attr.pointer || cons->expr->rank))
1373 : : {
1374 : 4 : gfc_error ("The rank of the element in the structure "
1375 : : "constructor at %L does not match that of the "
1376 : : "component (%d/%d)", &cons->expr->where,
1377 : : cons->expr->rank, rank);
1378 : 4 : t = false;
1379 : : }
1380 : :
1381 : : /* If we don't have the right type, try to convert it. */
1382 : :
1383 : 225444 : if (!comp->attr.proc_pointer &&
1384 : 96663 : !gfc_compare_types (&cons->expr->ts, &comp->ts))
1385 : : {
1386 : 11755 : if (strcmp (comp->name, "_extends") == 0)
1387 : : {
1388 : : /* Can afford to be brutal with the _extends initializer.
1389 : : The derived type can get lost because it is PRIVATE
1390 : : but it is not usage constrained by the standard. */
1391 : 8525 : cons->expr->ts = comp->ts;
1392 : : }
1393 : 3230 : else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1394 : : {
1395 : 2 : gfc_error ("The element in the structure constructor at %L, "
1396 : : "for pointer component %qs, is %s but should be %s",
1397 : 2 : &cons->expr->where, comp->name,
1398 : 2 : gfc_basic_typename (cons->expr->ts.type),
1399 : : gfc_basic_typename (comp->ts.type));
1400 : 2 : t = false;
1401 : : }
1402 : 3228 : else if (!UNLIMITED_POLY (comp))
1403 : : {
1404 : 3166 : bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1405 : 3166 : if (t)
1406 : 128781 : t = t2;
1407 : : }
1408 : : }
1409 : :
1410 : : /* For strings, the length of the constructor should be the same as
1411 : : the one of the structure, ensure this if the lengths are known at
1412 : : compile time and when we are dealing with PARAMETER or structure
1413 : : constructors. */
1414 : 128781 : if (cons->expr->ts.type == BT_CHARACTER
1415 : 3818 : && comp->ts.type == BT_CHARACTER
1416 : 3793 : && comp->ts.u.cl && comp->ts.u.cl->length
1417 : 2465 : && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1418 : 2436 : && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1419 : 920 : && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1420 : 920 : && cons->expr->ts.u.cl->length->ts.type == BT_INTEGER
1421 : 920 : && comp->ts.u.cl->length->ts.type == BT_INTEGER
1422 : 920 : && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1423 : 920 : comp->ts.u.cl->length->value.integer) != 0)
1424 : : {
1425 : 11 : if (comp->attr.pointer)
1426 : : {
1427 : 3 : HOST_WIDE_INT la, lb;
1428 : 3 : la = gfc_mpz_get_hwi (comp->ts.u.cl->length->value.integer);
1429 : 3 : lb = gfc_mpz_get_hwi (cons->expr->ts.u.cl->length->value.integer);
1430 : 3 : gfc_error ("Unequal character lengths (%wd/%wd) for pointer "
1431 : : "component %qs in constructor at %L",
1432 : 3 : la, lb, comp->name, &cons->expr->where);
1433 : 3 : t = false;
1434 : : }
1435 : :
1436 : 11 : if (cons->expr->expr_type == EXPR_VARIABLE
1437 : 4 : && cons->expr->rank != 0
1438 : 2 : && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1439 : : {
1440 : : /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1441 : : to make use of the gfc_resolve_character_array_constructor
1442 : : machinery. The expression is later simplified away to
1443 : : an array of string literals. */
1444 : 1 : gfc_expr *para = cons->expr;
1445 : 1 : cons->expr = gfc_get_expr ();
1446 : 1 : cons->expr->ts = para->ts;
1447 : 1 : cons->expr->where = para->where;
1448 : 1 : cons->expr->expr_type = EXPR_ARRAY;
1449 : 1 : cons->expr->rank = para->rank;
1450 : 1 : cons->expr->corank = para->corank;
1451 : 1 : cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1452 : 1 : gfc_constructor_append_expr (&cons->expr->value.constructor,
1453 : 1 : para, &cons->expr->where);
1454 : : }
1455 : :
1456 : 11 : if (cons->expr->expr_type == EXPR_ARRAY)
1457 : : {
1458 : : /* Rely on the cleanup of the namespace to deal correctly with
1459 : : the old charlen. (There was a block here that attempted to
1460 : : remove the charlen but broke the chain in so doing.) */
1461 : 5 : cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1462 : 5 : cons->expr->ts.u.cl->length_from_typespec = true;
1463 : 5 : cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1464 : 5 : gfc_resolve_character_array_constructor (cons->expr);
1465 : : }
1466 : : }
1467 : :
1468 : 128781 : if (cons->expr->expr_type == EXPR_NULL
1469 : 38380 : && !(comp->attr.pointer || comp->attr.allocatable
1470 : 19471 : || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1471 : 1091 : || (comp->ts.type == BT_CLASS
1472 : 1089 : && (CLASS_DATA (comp)->attr.class_pointer
1473 : 878 : || CLASS_DATA (comp)->attr.allocatable))))
1474 : : {
1475 : 2 : t = false;
1476 : 2 : gfc_error ("The NULL in the structure constructor at %L is "
1477 : : "being applied to component %qs, which is neither "
1478 : : "a POINTER nor ALLOCATABLE", &cons->expr->where,
1479 : : comp->name);
1480 : : }
1481 : :
1482 : 128781 : if (comp->attr.proc_pointer && comp->ts.interface)
1483 : : {
1484 : : /* Check procedure pointer interface. */
1485 : 14015 : gfc_symbol *s2 = NULL;
1486 : 14015 : gfc_component *c2;
1487 : 14015 : const char *name;
1488 : 14015 : char err[200];
1489 : :
1490 : 14015 : c2 = gfc_get_proc_ptr_comp (cons->expr);
1491 : 14015 : if (c2)
1492 : : {
1493 : 12 : s2 = c2->ts.interface;
1494 : 12 : name = c2->name;
1495 : : }
1496 : 14003 : else if (cons->expr->expr_type == EXPR_FUNCTION)
1497 : : {
1498 : 0 : s2 = cons->expr->symtree->n.sym->result;
1499 : 0 : name = cons->expr->symtree->n.sym->result->name;
1500 : : }
1501 : 14003 : else if (cons->expr->expr_type != EXPR_NULL)
1502 : : {
1503 : 13633 : s2 = cons->expr->symtree->n.sym;
1504 : 13633 : name = cons->expr->symtree->n.sym->name;
1505 : : }
1506 : :
1507 : 13645 : if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1508 : : err, sizeof (err), NULL, NULL))
1509 : : {
1510 : 2 : gfc_error_opt (0, "Interface mismatch for procedure-pointer "
1511 : : "component %qs in structure constructor at %L:"
1512 : 2 : " %s", comp->name, &cons->expr->where, err);
1513 : 2 : return false;
1514 : : }
1515 : : }
1516 : :
1517 : : /* Validate shape, except for dynamic or PDT arrays. */
1518 : 128779 : if (cons->expr->expr_type == EXPR_ARRAY && rank == cons->expr->rank
1519 : 1910 : && comp->as && !comp->attr.allocatable && !comp->attr.pointer
1520 : 1221 : && !comp->attr.pdt_array)
1521 : : {
1522 : 1221 : mpz_t len;
1523 : 1221 : mpz_init (len);
1524 : 2539 : for (int n = 0; n < rank; n++)
1525 : : {
1526 : 1319 : if (comp->as->upper[n]->expr_type != EXPR_CONSTANT
1527 : 1318 : || comp->as->lower[n]->expr_type != EXPR_CONSTANT)
1528 : : {
1529 : 1 : gfc_error ("Bad array spec of component %qs referenced in "
1530 : : "structure constructor at %L",
1531 : 1 : comp->name, &cons->expr->where);
1532 : 1 : t = false;
1533 : 1 : break;
1534 : 1318 : };
1535 : 1318 : if (cons->expr->shape == NULL)
1536 : 12 : continue;
1537 : 1306 : mpz_set_ui (len, 1);
1538 : 1306 : mpz_add (len, len, comp->as->upper[n]->value.integer);
1539 : 1306 : mpz_sub (len, len, comp->as->lower[n]->value.integer);
1540 : 1306 : if (mpz_cmp (cons->expr->shape[n], len) != 0)
1541 : : {
1542 : 9 : gfc_error ("The shape of component %qs in the structure "
1543 : : "constructor at %L differs from the shape of the "
1544 : : "declared component for dimension %d (%ld/%ld)",
1545 : : comp->name, &cons->expr->where, n+1,
1546 : : mpz_get_si (cons->expr->shape[n]),
1547 : : mpz_get_si (len));
1548 : 9 : t = false;
1549 : : }
1550 : : }
1551 : 1221 : mpz_clear (len);
1552 : : }
1553 : :
1554 : 128779 : if (!comp->attr.pointer || comp->attr.proc_pointer
1555 : 20631 : || cons->expr->expr_type == EXPR_NULL)
1556 : 119327 : continue;
1557 : :
1558 : 9452 : a = gfc_expr_attr (cons->expr);
1559 : :
1560 : 9452 : if (!a.pointer && !a.target)
1561 : : {
1562 : 1 : t = false;
1563 : 1 : gfc_error ("The element in the structure constructor at %L, "
1564 : : "for pointer component %qs should be a POINTER or "
1565 : 1 : "a TARGET", &cons->expr->where, comp->name);
1566 : : }
1567 : :
1568 : 9452 : if (init)
1569 : : {
1570 : : /* F08:C461. Additional checks for pointer initialization. */
1571 : 9384 : if (a.allocatable)
1572 : : {
1573 : 0 : t = false;
1574 : 0 : gfc_error ("Pointer initialization target at %L "
1575 : 0 : "must not be ALLOCATABLE", &cons->expr->where);
1576 : : }
1577 : 9384 : if (!a.save)
1578 : : {
1579 : 0 : t = false;
1580 : 0 : gfc_error ("Pointer initialization target at %L "
1581 : 0 : "must have the SAVE attribute", &cons->expr->where);
1582 : : }
1583 : : }
1584 : :
1585 : : /* F2023:C770: A designator that is an initial-data-target shall ...
1586 : : not have a vector subscript. */
1587 : 9452 : if (comp->attr.pointer && (a.pointer || a.target)
1588 : 18903 : && gfc_has_vector_index (cons->expr))
1589 : : {
1590 : 1 : gfc_error ("Pointer assignment target at %L has a vector subscript",
1591 : 1 : &cons->expr->where);
1592 : 1 : t = false;
1593 : : }
1594 : :
1595 : : /* F2003, C1272 (3). */
1596 : 9452 : bool impure = cons->expr->expr_type == EXPR_VARIABLE
1597 : 9452 : && (gfc_impure_variable (cons->expr->symtree->n.sym)
1598 : 9416 : || gfc_is_coindexed (cons->expr));
1599 : 33 : if (impure && gfc_pure (NULL))
1600 : : {
1601 : 1 : t = false;
1602 : 1 : gfc_error ("Invalid expression in the structure constructor for "
1603 : : "pointer component %qs at %L in PURE procedure",
1604 : 1 : comp->name, &cons->expr->where);
1605 : : }
1606 : :
1607 : 9452 : if (impure)
1608 : 33 : gfc_unset_implicit_pure (NULL);
1609 : : }
1610 : :
1611 : : return t;
1612 : : }
1613 : :
1614 : :
1615 : : /****************** Expression name resolution ******************/
1616 : :
1617 : : /* Returns 0 if a symbol was not declared with a type or
1618 : : attribute declaration statement, nonzero otherwise. */
1619 : :
1620 : : static bool
1621 : 726515 : was_declared (gfc_symbol *sym)
1622 : : {
1623 : 726515 : symbol_attribute a;
1624 : :
1625 : 726515 : a = sym->attr;
1626 : :
1627 : 726515 : if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1628 : : return 1;
1629 : :
1630 : 615506 : if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1631 : 607211 : || a.optional || a.pointer || a.save || a.target || a.volatile_
1632 : 607209 : || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1633 : 607155 : || a.asynchronous || a.codimension || a.subroutine)
1634 : 93143 : return 1;
1635 : :
1636 : : return 0;
1637 : : }
1638 : :
1639 : :
1640 : : /* Determine if a symbol is generic or not. */
1641 : :
1642 : : static int
1643 : 402923 : generic_sym (gfc_symbol *sym)
1644 : : {
1645 : 402923 : gfc_symbol *s;
1646 : :
1647 : 402923 : if (sym->attr.generic ||
1648 : 374799 : (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1649 : 29187 : return 1;
1650 : :
1651 : 373736 : if (was_declared (sym) || sym->ns->parent == NULL)
1652 : : return 0;
1653 : :
1654 : 75446 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1655 : :
1656 : 75446 : if (s != NULL)
1657 : : {
1658 : 126 : if (s == sym)
1659 : : return 0;
1660 : : else
1661 : 125 : return generic_sym (s);
1662 : : }
1663 : :
1664 : : return 0;
1665 : : }
1666 : :
1667 : :
1668 : : /* Determine if a symbol is specific or not. */
1669 : :
1670 : : static int
1671 : 373648 : specific_sym (gfc_symbol *sym)
1672 : : {
1673 : 373648 : gfc_symbol *s;
1674 : :
1675 : 373648 : if (sym->attr.if_source == IFSRC_IFBODY
1676 : 362634 : || sym->attr.proc == PROC_MODULE
1677 : : || sym->attr.proc == PROC_INTERNAL
1678 : : || sym->attr.proc == PROC_ST_FUNCTION
1679 : 287947 : || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1680 : 660864 : || sym->attr.external)
1681 : 88815 : return 1;
1682 : :
1683 : 284833 : if (was_declared (sym) || sym->ns->parent == NULL)
1684 : : return 0;
1685 : :
1686 : 75344 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1687 : :
1688 : 75344 : return (s == NULL) ? 0 : specific_sym (s);
1689 : : }
1690 : :
1691 : :
1692 : : /* Figure out if the procedure is specific, generic or unknown. */
1693 : :
1694 : : enum proc_type
1695 : : { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
1696 : :
1697 : : static proc_type
1698 : 402652 : procedure_kind (gfc_symbol *sym)
1699 : : {
1700 : 402652 : if (generic_sym (sym))
1701 : : return PTYPE_GENERIC;
1702 : :
1703 : 373608 : if (specific_sym (sym))
1704 : 88815 : return PTYPE_SPECIFIC;
1705 : :
1706 : : return PTYPE_UNKNOWN;
1707 : : }
1708 : :
1709 : : /* Check references to assumed size arrays. The flag need_full_assumed_size
1710 : : is nonzero when matching actual arguments. */
1711 : :
1712 : : static int need_full_assumed_size = 0;
1713 : :
1714 : : static bool
1715 : 1388443 : check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1716 : : {
1717 : 1388443 : if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1718 : : return false;
1719 : :
1720 : : /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1721 : : What should it be? */
1722 : 3788 : if (e->ref
1723 : 3786 : && e->ref->u.ar.as
1724 : 3785 : && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1725 : 3290 : && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1726 : 3290 : && (e->ref->u.ar.type == AR_FULL))
1727 : : {
1728 : 25 : gfc_error ("The upper bound in the last dimension must "
1729 : : "appear in the reference to the assumed size "
1730 : : "array %qs at %L", sym->name, &e->where);
1731 : 25 : return true;
1732 : : }
1733 : : return false;
1734 : : }
1735 : :
1736 : :
1737 : : /* Look for bad assumed size array references in argument expressions
1738 : : of elemental and array valued intrinsic procedures. Since this is
1739 : : called from procedure resolution functions, it only recurses at
1740 : : operators. */
1741 : :
1742 : : static bool
1743 : 224764 : resolve_assumed_size_actual (gfc_expr *e)
1744 : : {
1745 : 224764 : if (e == NULL)
1746 : : return false;
1747 : :
1748 : 224273 : switch (e->expr_type)
1749 : : {
1750 : 108083 : case EXPR_VARIABLE:
1751 : 108083 : if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1752 : : return true;
1753 : : break;
1754 : :
1755 : 47185 : case EXPR_OP:
1756 : 47185 : if (resolve_assumed_size_actual (e->value.op.op1)
1757 : 47185 : || resolve_assumed_size_actual (e->value.op.op2))
1758 : 0 : return true;
1759 : : break;
1760 : :
1761 : : default:
1762 : : break;
1763 : : }
1764 : : return false;
1765 : : }
1766 : :
1767 : :
1768 : : /* Check a generic procedure, passed as an actual argument, to see if
1769 : : there is a matching specific name. If none, it is an error, and if
1770 : : more than one, the reference is ambiguous. */
1771 : : static int
1772 : 8 : count_specific_procs (gfc_expr *e)
1773 : : {
1774 : 8 : int n;
1775 : 8 : gfc_interface *p;
1776 : 8 : gfc_symbol *sym;
1777 : :
1778 : 8 : n = 0;
1779 : 8 : sym = e->symtree->n.sym;
1780 : :
1781 : 22 : for (p = sym->generic; p; p = p->next)
1782 : 14 : if (strcmp (sym->name, p->sym->name) == 0)
1783 : : {
1784 : 8 : e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1785 : : sym->name);
1786 : 8 : n++;
1787 : : }
1788 : :
1789 : 8 : if (n > 1)
1790 : 1 : gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
1791 : : &e->where);
1792 : :
1793 : 8 : if (n == 0)
1794 : 1 : gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1795 : : "argument at %L", sym->name, &e->where);
1796 : :
1797 : 8 : return n;
1798 : : }
1799 : :
1800 : :
1801 : : /* See if a call to sym could possibly be a not allowed RECURSION because of
1802 : : a missing RECURSIVE declaration. This means that either sym is the current
1803 : : context itself, or sym is the parent of a contained procedure calling its
1804 : : non-RECURSIVE containing procedure.
1805 : : This also works if sym is an ENTRY. */
1806 : :
1807 : : static bool
1808 : 145508 : is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1809 : : {
1810 : 145508 : gfc_symbol* proc_sym;
1811 : 145508 : gfc_symbol* context_proc;
1812 : 145508 : gfc_namespace* real_context;
1813 : :
1814 : 145508 : if (sym->attr.flavor == FL_PROGRAM
1815 : : || gfc_fl_struct (sym->attr.flavor))
1816 : : return false;
1817 : :
1818 : : /* If we've got an ENTRY, find real procedure. */
1819 : 145507 : if (sym->attr.entry && sym->ns->entries)
1820 : 45 : proc_sym = sym->ns->entries->sym;
1821 : : else
1822 : : proc_sym = sym;
1823 : :
1824 : : /* If sym is RECURSIVE, all is well of course. */
1825 : 145507 : if (proc_sym->attr.recursive || flag_recursive)
1826 : : return false;
1827 : :
1828 : : /* Find the context procedure's "real" symbol if it has entries.
1829 : : We look for a procedure symbol, so recurse on the parents if we don't
1830 : : find one (like in case of a BLOCK construct). */
1831 : 1698 : for (real_context = context; ; real_context = real_context->parent)
1832 : : {
1833 : : /* We should find something, eventually! */
1834 : 124976 : gcc_assert (real_context);
1835 : :
1836 : 124976 : context_proc = (real_context->entries ? real_context->entries->sym
1837 : : : real_context->proc_name);
1838 : :
1839 : : /* In some special cases, there may not be a proc_name, like for this
1840 : : invalid code:
1841 : : real(bad_kind()) function foo () ...
1842 : : when checking the call to bad_kind ().
1843 : : In these cases, we simply return here and assume that the
1844 : : call is ok. */
1845 : 124976 : if (!context_proc)
1846 : : return false;
1847 : :
1848 : 124712 : if (context_proc->attr.flavor != FL_LABEL)
1849 : : break;
1850 : : }
1851 : :
1852 : : /* A call from sym's body to itself is recursion, of course. */
1853 : 123014 : if (context_proc == proc_sym)
1854 : : return true;
1855 : :
1856 : : /* The same is true if context is a contained procedure and sym the
1857 : : containing one. */
1858 : 123000 : if (context_proc->attr.contained)
1859 : : {
1860 : 20469 : gfc_symbol* parent_proc;
1861 : :
1862 : 20469 : gcc_assert (context->parent);
1863 : 20469 : parent_proc = (context->parent->entries ? context->parent->entries->sym
1864 : : : context->parent->proc_name);
1865 : :
1866 : 20469 : if (parent_proc == proc_sym)
1867 : 9 : return true;
1868 : : }
1869 : :
1870 : : return false;
1871 : : }
1872 : :
1873 : :
1874 : : /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1875 : : its typespec and formal argument list. */
1876 : :
1877 : : bool
1878 : 41028 : gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1879 : : {
1880 : 41028 : gfc_intrinsic_sym* isym = NULL;
1881 : 41028 : const char* symstd;
1882 : :
1883 : 41028 : if (sym->resolve_symbol_called >= 2)
1884 : : return true;
1885 : :
1886 : 31414 : sym->resolve_symbol_called = 2;
1887 : :
1888 : : /* Already resolved. */
1889 : 31414 : if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1890 : : return true;
1891 : :
1892 : : /* We already know this one is an intrinsic, so we don't call
1893 : : gfc_is_intrinsic for full checking but rather use gfc_find_function and
1894 : : gfc_find_subroutine directly to check whether it is a function or
1895 : : subroutine. */
1896 : :
1897 : 23664 : if (sym->intmod_sym_id && sym->attr.subroutine)
1898 : : {
1899 : 8527 : gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1900 : 8527 : isym = gfc_intrinsic_subroutine_by_id (id);
1901 : 8527 : }
1902 : 15137 : else if (sym->intmod_sym_id)
1903 : : {
1904 : 11771 : gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1905 : 11771 : isym = gfc_intrinsic_function_by_id (id);
1906 : : }
1907 : 3366 : else if (!sym->attr.subroutine)
1908 : 3294 : isym = gfc_find_function (sym->name);
1909 : :
1910 : 23592 : if (isym && !sym->attr.subroutine)
1911 : : {
1912 : 15026 : if (sym->ts.type != BT_UNKNOWN && warn_surprising
1913 : 24 : && !sym->attr.implicit_type)
1914 : 10 : gfc_warning (OPT_Wsurprising,
1915 : : "Type specified for intrinsic function %qs at %L is"
1916 : : " ignored", sym->name, &sym->declared_at);
1917 : :
1918 : 18587 : if (!sym->attr.function &&
1919 : 3561 : !gfc_add_function(&sym->attr, sym->name, loc))
1920 : : return false;
1921 : :
1922 : 15026 : sym->ts = isym->ts;
1923 : : }
1924 : 8638 : else if (isym || (isym = gfc_find_subroutine (sym->name)))
1925 : : {
1926 : 8635 : if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1927 : : {
1928 : 1 : gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1929 : : " specifier", sym->name, &sym->declared_at);
1930 : 1 : return false;
1931 : : }
1932 : :
1933 : 8669 : if (!sym->attr.subroutine &&
1934 : 35 : !gfc_add_subroutine(&sym->attr, sym->name, loc))
1935 : : return false;
1936 : : }
1937 : : else
1938 : : {
1939 : 3 : gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
1940 : : &sym->declared_at);
1941 : 3 : return false;
1942 : : }
1943 : :
1944 : 23659 : gfc_copy_formal_args_intr (sym, isym, NULL);
1945 : :
1946 : 23659 : sym->attr.pure = isym->pure;
1947 : 23659 : sym->attr.elemental = isym->elemental;
1948 : :
1949 : : /* Check it is actually available in the standard settings. */
1950 : 23659 : if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1951 : : {
1952 : 31 : gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1953 : : "available in the current standard settings but %s. Use "
1954 : : "an appropriate %<-std=*%> option or enable "
1955 : : "%<-fall-intrinsics%> in order to use it.",
1956 : : sym->name, &sym->declared_at, symstd);
1957 : 31 : return false;
1958 : : }
1959 : :
1960 : : return true;
1961 : : }
1962 : :
1963 : :
1964 : : /* Resolve a procedure expression, like passing it to a called procedure or as
1965 : : RHS for a procedure pointer assignment. */
1966 : :
1967 : : static bool
1968 : 1292705 : resolve_procedure_expression (gfc_expr* expr)
1969 : : {
1970 : 1292705 : gfc_symbol* sym;
1971 : :
1972 : 1292705 : if (expr->expr_type != EXPR_VARIABLE)
1973 : : return true;
1974 : 1292700 : gcc_assert (expr->symtree);
1975 : :
1976 : 1292700 : sym = expr->symtree->n.sym;
1977 : :
1978 : 1292700 : if (sym->attr.intrinsic)
1979 : 1346 : gfc_resolve_intrinsic (sym, &expr->where);
1980 : :
1981 : 1292700 : if (sym->attr.flavor != FL_PROCEDURE
1982 : 29743 : || (sym->attr.function && sym->result == sym))
1983 : : return true;
1984 : :
1985 : : /* A non-RECURSIVE procedure that is used as procedure expression within its
1986 : : own body is in danger of being called recursively. */
1987 : 14025 : if (is_illegal_recursion (sym, gfc_current_ns))
1988 : : {
1989 : 10 : if (sym->attr.use_assoc && expr->symtree->name[0] == '@')
1990 : 0 : gfc_warning (0, "Non-RECURSIVE procedure %qs from module %qs is"
1991 : : " possibly calling itself recursively in procedure %qs. "
1992 : : " Declare it RECURSIVE or use %<-frecursive%>",
1993 : 0 : sym->name, sym->module, gfc_current_ns->proc_name->name);
1994 : : else
1995 : 10 : gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
1996 : : " itself recursively. Declare it RECURSIVE or use"
1997 : : " %<-frecursive%>", sym->name, &expr->where);
1998 : : }
1999 : :
2000 : : return true;
2001 : : }
2002 : :
2003 : :
2004 : : /* Check that name is not a derived type. */
2005 : :
2006 : : static bool
2007 : 3106 : is_dt_name (const char *name)
2008 : : {
2009 : 3106 : gfc_symbol *dt_list, *dt_first;
2010 : :
2011 : 3106 : dt_list = dt_first = gfc_derived_types;
2012 : 5518 : for (; dt_list; dt_list = dt_list->dt_next)
2013 : : {
2014 : 3427 : if (strcmp(dt_list->name, name) == 0)
2015 : : return true;
2016 : 3424 : if (dt_first == dt_list->dt_next)
2017 : : break;
2018 : : }
2019 : : return false;
2020 : : }
2021 : :
2022 : :
2023 : : /* Resolve an actual argument list. Most of the time, this is just
2024 : : resolving the expressions in the list.
2025 : : The exception is that we sometimes have to decide whether arguments
2026 : : that look like procedure arguments are really simple variable
2027 : : references. */
2028 : :
2029 : : static bool
2030 : 416299 : resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
2031 : : bool no_formal_args)
2032 : : {
2033 : 416299 : gfc_symbol *sym;
2034 : 416299 : gfc_symtree *parent_st;
2035 : 416299 : gfc_expr *e;
2036 : 416299 : gfc_component *comp;
2037 : 416299 : int save_need_full_assumed_size;
2038 : 416299 : bool return_value = false;
2039 : 416299 : bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
2040 : :
2041 : 416299 : actual_arg = true;
2042 : 416299 : first_actual_arg = true;
2043 : :
2044 : 1072291 : for (; arg; arg = arg->next)
2045 : : {
2046 : 656091 : e = arg->expr;
2047 : 656091 : if (e == NULL)
2048 : : {
2049 : : /* Check the label is a valid branching target. */
2050 : 2358 : if (arg->label)
2051 : : {
2052 : 236 : if (arg->label->defined == ST_LABEL_UNKNOWN)
2053 : : {
2054 : 0 : gfc_error ("Label %d referenced at %L is never defined",
2055 : : arg->label->value, &arg->label->where);
2056 : 0 : goto cleanup;
2057 : : }
2058 : : }
2059 : 2358 : first_actual_arg = false;
2060 : 2358 : continue;
2061 : : }
2062 : :
2063 : 653733 : if (e->expr_type == EXPR_VARIABLE
2064 : 288781 : && e->symtree->n.sym->attr.generic
2065 : 8 : && no_formal_args
2066 : 653738 : && count_specific_procs (e) != 1)
2067 : 2 : goto cleanup;
2068 : :
2069 : 653731 : if (e->ts.type != BT_PROCEDURE)
2070 : : {
2071 : 582679 : save_need_full_assumed_size = need_full_assumed_size;
2072 : 582679 : if (e->expr_type != EXPR_VARIABLE)
2073 : 364952 : need_full_assumed_size = 0;
2074 : 582679 : if (!gfc_resolve_expr (e))
2075 : 60 : goto cleanup;
2076 : 582619 : need_full_assumed_size = save_need_full_assumed_size;
2077 : 582619 : goto argument_list;
2078 : : }
2079 : :
2080 : : /* See if the expression node should really be a variable reference. */
2081 : :
2082 : 71052 : sym = e->symtree->n.sym;
2083 : :
2084 : 71052 : if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name))
2085 : : {
2086 : 3 : gfc_error ("Derived type %qs is used as an actual "
2087 : : "argument at %L", sym->name, &e->where);
2088 : 3 : goto cleanup;
2089 : : }
2090 : :
2091 : 71049 : if (sym->attr.flavor == FL_PROCEDURE
2092 : 67946 : || sym->attr.intrinsic
2093 : 67946 : || sym->attr.external)
2094 : : {
2095 : 3103 : int actual_ok;
2096 : :
2097 : : /* If a procedure is not already determined to be something else
2098 : : check if it is intrinsic. */
2099 : 3103 : if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
2100 : 1254 : sym->attr.intrinsic = 1;
2101 : :
2102 : 3103 : if (sym->attr.proc == PROC_ST_FUNCTION)
2103 : : {
2104 : 2 : gfc_error ("Statement function %qs at %L is not allowed as an "
2105 : : "actual argument", sym->name, &e->where);
2106 : : }
2107 : :
2108 : 6206 : actual_ok = gfc_intrinsic_actual_ok (sym->name,
2109 : 3103 : sym->attr.subroutine);
2110 : 3103 : if (sym->attr.intrinsic && actual_ok == 0)
2111 : : {
2112 : 0 : gfc_error ("Intrinsic %qs at %L is not allowed as an "
2113 : : "actual argument", sym->name, &e->where);
2114 : : }
2115 : :
2116 : 3103 : if (sym->attr.contained && !sym->attr.use_assoc
2117 : 404 : && sym->ns->proc_name->attr.flavor != FL_MODULE)
2118 : : {
2119 : 216 : if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
2120 : : " used as actual argument at %L",
2121 : : sym->name, &e->where))
2122 : 3 : goto cleanup;
2123 : : }
2124 : :
2125 : 3100 : if (sym->attr.elemental && !sym->attr.intrinsic)
2126 : : {
2127 : 2 : gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
2128 : : "allowed as an actual argument at %L", sym->name,
2129 : : &e->where);
2130 : : }
2131 : :
2132 : : /* Check if a generic interface has a specific procedure
2133 : : with the same name before emitting an error. */
2134 : 3100 : if (sym->attr.generic && count_specific_procs (e) != 1)
2135 : 0 : goto cleanup;
2136 : :
2137 : : /* Just in case a specific was found for the expression. */
2138 : 3100 : sym = e->symtree->n.sym;
2139 : :
2140 : : /* If the symbol is the function that names the current (or
2141 : : parent) scope, then we really have a variable reference. */
2142 : :
2143 : 3100 : if (gfc_is_function_return_value (sym, sym->ns))
2144 : 0 : goto got_variable;
2145 : :
2146 : : /* If all else fails, see if we have a specific intrinsic. */
2147 : 3100 : if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
2148 : : {
2149 : 0 : gfc_intrinsic_sym *isym;
2150 : :
2151 : 0 : isym = gfc_find_function (sym->name);
2152 : 0 : if (isym == NULL || !isym->specific)
2153 : : {
2154 : 0 : gfc_error ("Unable to find a specific INTRINSIC procedure "
2155 : : "for the reference %qs at %L", sym->name,
2156 : : &e->where);
2157 : 0 : goto cleanup;
2158 : : }
2159 : 0 : sym->ts = isym->ts;
2160 : 0 : sym->attr.intrinsic = 1;
2161 : 0 : sym->attr.function = 1;
2162 : : }
2163 : :
2164 : 3100 : if (!gfc_resolve_expr (e))
2165 : 0 : goto cleanup;
2166 : 3100 : goto argument_list;
2167 : : }
2168 : :
2169 : : /* See if the name is a module procedure in a parent unit. */
2170 : :
2171 : 67946 : if (was_declared (sym) || sym->ns->parent == NULL)
2172 : 67853 : goto got_variable;
2173 : :
2174 : 93 : if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
2175 : : {
2176 : 0 : gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
2177 : 0 : goto cleanup;
2178 : : }
2179 : :
2180 : 93 : if (parent_st == NULL)
2181 : 93 : goto got_variable;
2182 : :
2183 : 0 : sym = parent_st->n.sym;
2184 : 0 : e->symtree = parent_st; /* Point to the right thing. */
2185 : :
2186 : 0 : if (sym->attr.flavor == FL_PROCEDURE
2187 : 0 : || sym->attr.intrinsic
2188 : 0 : || sym->attr.external)
2189 : : {
2190 : 0 : if (!gfc_resolve_expr (e))
2191 : 0 : goto cleanup;
2192 : 0 : goto argument_list;
2193 : : }
2194 : :
2195 : 0 : got_variable:
2196 : 67946 : e->expr_type = EXPR_VARIABLE;
2197 : 67946 : e->ts = sym->ts;
2198 : 67946 : if ((sym->as != NULL && sym->ts.type != BT_CLASS)
2199 : 35262 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2200 : 3775 : && CLASS_DATA (sym)->as))
2201 : : {
2202 : 38118 : gfc_array_spec *as
2203 : 35401 : = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as;
2204 : 35401 : e->rank = as->rank;
2205 : 35401 : e->corank = as->corank;
2206 : 35401 : e->ref = gfc_get_ref ();
2207 : 35401 : e->ref->type = REF_ARRAY;
2208 : 35401 : e->ref->u.ar.type = AR_FULL;
2209 : 35401 : e->ref->u.ar.as = as;
2210 : : }
2211 : :
2212 : : /* These symbols are set untyped by calls to gfc_set_default_type
2213 : : with 'error_flag' = false. Reset the untyped attribute so that
2214 : : the error will be generated in gfc_resolve_expr. */
2215 : 67946 : if (e->expr_type == EXPR_VARIABLE
2216 : 67946 : && sym->ts.type == BT_UNKNOWN
2217 : 36 : && sym->attr.untyped)
2218 : 5 : sym->attr.untyped = 0;
2219 : :
2220 : : /* Expressions are assigned a default ts.type of BT_PROCEDURE in
2221 : : primary.cc (match_actual_arg). If above code determines that it
2222 : : is a variable instead, it needs to be resolved as it was not
2223 : : done at the beginning of this function. */
2224 : 67946 : save_need_full_assumed_size = need_full_assumed_size;
2225 : 67946 : if (e->expr_type != EXPR_VARIABLE)
2226 : 0 : need_full_assumed_size = 0;
2227 : 67946 : if (!gfc_resolve_expr (e))
2228 : 22 : goto cleanup;
2229 : 67924 : need_full_assumed_size = save_need_full_assumed_size;
2230 : :
2231 : 653643 : argument_list:
2232 : : /* Check argument list functions %VAL, %LOC and %REF. There is
2233 : : nothing to do for %REF. */
2234 : 653643 : if (arg->name && arg->name[0] == '%')
2235 : : {
2236 : 42 : if (strcmp ("%VAL", arg->name) == 0)
2237 : : {
2238 : 28 : if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
2239 : : {
2240 : 2 : gfc_error ("By-value argument at %L is not of numeric "
2241 : : "type", &e->where);
2242 : 2 : goto cleanup;
2243 : : }
2244 : :
2245 : 26 : if (e->rank)
2246 : : {
2247 : 1 : gfc_error ("By-value argument at %L cannot be an array or "
2248 : : "an array section", &e->where);
2249 : 1 : goto cleanup;
2250 : : }
2251 : :
2252 : : /* Intrinsics are still PROC_UNKNOWN here. However,
2253 : : since same file external procedures are not resolvable
2254 : : in gfortran, it is a good deal easier to leave them to
2255 : : intrinsic.cc. */
2256 : 25 : if (ptype != PROC_UNKNOWN
2257 : 25 : && ptype != PROC_DUMMY
2258 : 9 : && ptype != PROC_EXTERNAL
2259 : 9 : && ptype != PROC_MODULE)
2260 : : {
2261 : 3 : gfc_error ("By-value argument at %L is not allowed "
2262 : : "in this context", &e->where);
2263 : 3 : goto cleanup;
2264 : : }
2265 : : }
2266 : :
2267 : : /* Statement functions have already been excluded above. */
2268 : 14 : else if (strcmp ("%LOC", arg->name) == 0
2269 : 8 : && e->ts.type == BT_PROCEDURE)
2270 : : {
2271 : 0 : if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
2272 : : {
2273 : 0 : gfc_error ("Passing internal procedure at %L by location "
2274 : : "not allowed", &e->where);
2275 : 0 : goto cleanup;
2276 : : }
2277 : : }
2278 : : }
2279 : :
2280 : 653637 : comp = gfc_get_proc_ptr_comp(e);
2281 : 653637 : if (e->expr_type == EXPR_VARIABLE
2282 : 287403 : && comp && comp->attr.elemental)
2283 : : {
2284 : 1 : gfc_error ("ELEMENTAL procedure pointer component %qs is not "
2285 : : "allowed as an actual argument at %L", comp->name,
2286 : : &e->where);
2287 : : }
2288 : :
2289 : : /* Fortran 2008, C1237. */
2290 : 287403 : if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
2291 : 653943 : && gfc_has_ultimate_pointer (e))
2292 : : {
2293 : 3 : gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2294 : : "component", &e->where);
2295 : 3 : goto cleanup;
2296 : : }
2297 : :
2298 : 653634 : first_actual_arg = false;
2299 : : }
2300 : :
2301 : : return_value = true;
2302 : :
2303 : 416299 : cleanup:
2304 : 416299 : actual_arg = actual_arg_sav;
2305 : 416299 : first_actual_arg = first_actual_arg_sav;
2306 : :
2307 : 416299 : return return_value;
2308 : : }
2309 : :
2310 : :
2311 : : /* Do the checks of the actual argument list that are specific to elemental
2312 : : procedures. If called with c == NULL, we have a function, otherwise if
2313 : : expr == NULL, we have a subroutine. */
2314 : :
2315 : : static bool
2316 : 317540 : resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2317 : : {
2318 : 317540 : gfc_actual_arglist *arg0;
2319 : 317540 : gfc_actual_arglist *arg;
2320 : 317540 : gfc_symbol *esym = NULL;
2321 : 317540 : gfc_intrinsic_sym *isym = NULL;
2322 : 317540 : gfc_expr *e = NULL;
2323 : 317540 : gfc_intrinsic_arg *iformal = NULL;
2324 : 317540 : gfc_formal_arglist *eformal = NULL;
2325 : 317540 : bool formal_optional = false;
2326 : 317540 : bool set_by_optional = false;
2327 : 317540 : int i;
2328 : 317540 : int rank = 0;
2329 : :
2330 : : /* Is this an elemental procedure? */
2331 : 317540 : if (expr && expr->value.function.actual != NULL)
2332 : : {
2333 : 230412 : if (expr->value.function.esym != NULL
2334 : 43172 : && expr->value.function.esym->attr.elemental)
2335 : : {
2336 : : arg0 = expr->value.function.actual;
2337 : : esym = expr->value.function.esym;
2338 : : }
2339 : 214140 : else if (expr->value.function.isym != NULL
2340 : 186217 : && expr->value.function.isym->elemental)
2341 : : {
2342 : : arg0 = expr->value.function.actual;
2343 : : isym = expr->value.function.isym;
2344 : : }
2345 : : else
2346 : : return true;
2347 : : }
2348 : 87128 : else if (c && c->ext.actual != NULL)
2349 : : {
2350 : 69073 : arg0 = c->ext.actual;
2351 : :
2352 : 69073 : if (c->resolved_sym)
2353 : : esym = c->resolved_sym;
2354 : : else
2355 : 313 : esym = c->symtree->n.sym;
2356 : 69073 : gcc_assert (esym);
2357 : :
2358 : 69073 : if (!esym->attr.elemental)
2359 : : return true;
2360 : : }
2361 : : else
2362 : : return true;
2363 : :
2364 : : /* The rank of an elemental is the rank of its array argument(s). */
2365 : 171509 : for (arg = arg0; arg; arg = arg->next)
2366 : : {
2367 : 111162 : if (arg->expr != NULL && arg->expr->rank != 0)
2368 : : {
2369 : 10364 : rank = arg->expr->rank;
2370 : 10364 : if (arg->expr->expr_type == EXPR_VARIABLE
2371 : 5216 : && arg->expr->symtree->n.sym->attr.optional)
2372 : 10364 : set_by_optional = true;
2373 : :
2374 : : /* Function specific; set the result rank and shape. */
2375 : 10364 : if (expr)
2376 : : {
2377 : 8196 : expr->rank = rank;
2378 : 8196 : expr->corank = arg->expr->corank;
2379 : 8196 : if (!expr->shape && arg->expr->shape)
2380 : : {
2381 : 3923 : expr->shape = gfc_get_shape (rank);
2382 : 8638 : for (i = 0; i < rank; i++)
2383 : 4715 : mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2384 : : }
2385 : : }
2386 : : break;
2387 : : }
2388 : : }
2389 : :
2390 : : /* If it is an array, it shall not be supplied as an actual argument
2391 : : to an elemental procedure unless an array of the same rank is supplied
2392 : : as an actual argument corresponding to a nonoptional dummy argument of
2393 : : that elemental procedure(12.4.1.5). */
2394 : 70711 : formal_optional = false;
2395 : 70711 : if (isym)
2396 : 48472 : iformal = isym->formal;
2397 : : else
2398 : 22239 : eformal = esym->formal;
2399 : :
2400 : 187158 : for (arg = arg0; arg; arg = arg->next)
2401 : : {
2402 : 116447 : if (eformal)
2403 : : {
2404 : 39897 : if (eformal->sym && eformal->sym->attr.optional)
2405 : 39897 : formal_optional = true;
2406 : 39897 : eformal = eformal->next;
2407 : : }
2408 : 76550 : else if (isym && iformal)
2409 : : {
2410 : 66520 : if (iformal->optional)
2411 : 13261 : formal_optional = true;
2412 : 66520 : iformal = iformal->next;
2413 : : }
2414 : 10030 : else if (isym)
2415 : 10022 : formal_optional = true;
2416 : :
2417 : 116447 : if (pedantic && arg->expr != NULL
2418 : 71060 : && arg->expr->expr_type == EXPR_VARIABLE
2419 : 34456 : && arg->expr->symtree->n.sym->attr.optional
2420 : 572 : && formal_optional
2421 : 479 : && arg->expr->rank
2422 : 153 : && (set_by_optional || arg->expr->rank != rank)
2423 : 42 : && !(isym && isym->id == GFC_ISYM_CONVERSION))
2424 : : {
2425 : 114 : bool t = false;
2426 : : gfc_actual_arglist *a;
2427 : :
2428 : : /* Scan the argument list for a non-optional argument with the
2429 : : same rank as arg. */
2430 : 114 : for (a = arg0; a; a = a->next)
2431 : 87 : if (a != arg
2432 : 45 : && a->expr->rank == arg->expr->rank
2433 : 39 : && (a->expr->expr_type != EXPR_VARIABLE
2434 : 37 : || (a->expr->expr_type == EXPR_VARIABLE
2435 : 37 : && !a->expr->symtree->n.sym->attr.optional)))
2436 : : {
2437 : : t = true;
2438 : : break;
2439 : : }
2440 : :
2441 : 42 : if (!t)
2442 : 27 : gfc_warning (OPT_Wpedantic,
2443 : : "%qs at %L is an array and OPTIONAL; If it is not "
2444 : : "present, then it cannot be the actual argument of "
2445 : : "an ELEMENTAL procedure unless there is a non-optional"
2446 : : " argument with the same rank "
2447 : : "(Fortran 2018, 15.5.2.12)",
2448 : : arg->expr->symtree->n.sym->name, &arg->expr->where);
2449 : : }
2450 : : }
2451 : :
2452 : 187147 : for (arg = arg0; arg; arg = arg->next)
2453 : : {
2454 : 116445 : if (arg->expr == NULL || arg->expr->rank == 0)
2455 : 103421 : continue;
2456 : :
2457 : : /* Being elemental, the last upper bound of an assumed size array
2458 : : argument must be present. */
2459 : 13024 : if (resolve_assumed_size_actual (arg->expr))
2460 : : return false;
2461 : :
2462 : : /* Elemental procedure's array actual arguments must conform. */
2463 : 13021 : if (e != NULL)
2464 : : {
2465 : 2660 : if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
2466 : : return false;
2467 : : }
2468 : : else
2469 : 10361 : e = arg->expr;
2470 : : }
2471 : :
2472 : : /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2473 : : is an array, the intent inout/out variable needs to be also an array. */
2474 : 70702 : if (rank > 0 && esym && expr == NULL)
2475 : 6637 : for (eformal = esym->formal, arg = arg0; arg && eformal;
2476 : 4475 : arg = arg->next, eformal = eformal->next)
2477 : 4477 : if (eformal->sym
2478 : 4476 : && (eformal->sym->attr.intent == INTENT_OUT
2479 : 3394 : || eformal->sym->attr.intent == INTENT_INOUT)
2480 : 1476 : && arg->expr && arg->expr->rank == 0)
2481 : : {
2482 : 2 : gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2483 : : "ELEMENTAL subroutine %qs is a scalar, but another "
2484 : : "actual argument is an array", &arg->expr->where,
2485 : : (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2486 : : : "INOUT", eformal->sym->name, esym->name);
2487 : 2 : return false;
2488 : : }
2489 : : return true;
2490 : : }
2491 : :
2492 : :
2493 : : /* This function does the checking of references to global procedures
2494 : : as defined in sections 18.1 and 14.1, respectively, of the Fortran
2495 : : 77 and 95 standards. It checks for a gsymbol for the name, making
2496 : : one if it does not already exist. If it already exists, then the
2497 : : reference being resolved must correspond to the type of gsymbol.
2498 : : Otherwise, the new symbol is equipped with the attributes of the
2499 : : reference. The corresponding code that is called in creating
2500 : : global entities is parse.cc.
2501 : :
2502 : : In addition, for all but -std=legacy, the gsymbols are used to
2503 : : check the interfaces of external procedures from the same file.
2504 : : The namespace of the gsymbol is resolved and then, once this is
2505 : : done the interface is checked. */
2506 : :
2507 : :
2508 : : static bool
2509 : 14817 : not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2510 : : {
2511 : 14817 : if (!gsym_ns->proc_name->attr.recursive)
2512 : : return true;
2513 : :
2514 : 151 : if (sym->ns == gsym_ns)
2515 : : return false;
2516 : :
2517 : 151 : if (sym->ns->parent && sym->ns->parent == gsym_ns)
2518 : 0 : return false;
2519 : :
2520 : : return true;
2521 : : }
2522 : :
2523 : : static bool
2524 : 14817 : not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2525 : : {
2526 : 14817 : if (gsym_ns->entries)
2527 : : {
2528 : : gfc_entry_list *entry = gsym_ns->entries;
2529 : :
2530 : 3234 : for (; entry; entry = entry->next)
2531 : : {
2532 : 2281 : if (strcmp (sym->name, entry->sym->name) == 0)
2533 : : {
2534 : 946 : if (strcmp (gsym_ns->proc_name->name,
2535 : 946 : sym->ns->proc_name->name) == 0)
2536 : : return false;
2537 : :
2538 : 946 : if (sym->ns->parent
2539 : 0 : && strcmp (gsym_ns->proc_name->name,
2540 : 0 : sym->ns->parent->proc_name->name) == 0)
2541 : : return false;
2542 : : }
2543 : : }
2544 : : }
2545 : : return true;
2546 : : }
2547 : :
2548 : :
2549 : : /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2550 : :
2551 : : bool
2552 : 15636 : gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2553 : : {
2554 : 15636 : gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2555 : :
2556 : 58646 : for ( ; arg; arg = arg->next)
2557 : : {
2558 : 27744 : if (!arg->sym)
2559 : 157 : continue;
2560 : :
2561 : 27587 : if (arg->sym->attr.allocatable) /* (2a) */
2562 : : {
2563 : 0 : strncpy (errmsg, _("allocatable argument"), err_len);
2564 : 0 : return true;
2565 : : }
2566 : 27587 : else if (arg->sym->attr.asynchronous)
2567 : : {
2568 : 0 : strncpy (errmsg, _("asynchronous argument"), err_len);
2569 : 0 : return true;
2570 : : }
2571 : 27587 : else if (arg->sym->attr.optional)
2572 : : {
2573 : 75 : strncpy (errmsg, _("optional argument"), err_len);
2574 : 75 : return true;
2575 : : }
2576 : 27512 : else if (arg->sym->attr.pointer)
2577 : : {
2578 : 12 : strncpy (errmsg, _("pointer argument"), err_len);
2579 : 12 : return true;
2580 : : }
2581 : 27500 : else if (arg->sym->attr.target)
2582 : : {
2583 : 72 : strncpy (errmsg, _("target argument"), err_len);
2584 : 72 : return true;
2585 : : }
2586 : 27428 : else if (arg->sym->attr.value)
2587 : : {
2588 : 0 : strncpy (errmsg, _("value argument"), err_len);
2589 : 0 : return true;
2590 : : }
2591 : 27428 : else if (arg->sym->attr.volatile_)
2592 : : {
2593 : 1 : strncpy (errmsg, _("volatile argument"), err_len);
2594 : 1 : return true;
2595 : : }
2596 : 27427 : else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2597 : : {
2598 : 45 : strncpy (errmsg, _("assumed-shape argument"), err_len);
2599 : 45 : return true;
2600 : : }
2601 : 27382 : else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2602 : : {
2603 : 1 : strncpy (errmsg, _("assumed-rank argument"), err_len);
2604 : 1 : return true;
2605 : : }
2606 : 27381 : else if (arg->sym->attr.codimension) /* (2c) */
2607 : : {
2608 : 1 : strncpy (errmsg, _("coarray argument"), err_len);
2609 : 1 : return true;
2610 : : }
2611 : 27380 : else if (false) /* (2d) TODO: parametrized derived type */
2612 : : {
2613 : : strncpy (errmsg, _("parametrized derived type argument"), err_len);
2614 : : return true;
2615 : : }
2616 : 27380 : else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2617 : : {
2618 : 162 : strncpy (errmsg, _("polymorphic argument"), err_len);
2619 : 162 : return true;
2620 : : }
2621 : 27218 : else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2622 : : {
2623 : 0 : strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2624 : 0 : return true;
2625 : : }
2626 : 27218 : else if (arg->sym->ts.type == BT_ASSUMED)
2627 : : {
2628 : : /* As assumed-type is unlimited polymorphic (cf. above).
2629 : : See also TS 29113, Note 6.1. */
2630 : 1 : strncpy (errmsg, _("assumed-type argument"), err_len);
2631 : 1 : return true;
2632 : : }
2633 : : }
2634 : :
2635 : 15266 : if (sym->attr.function)
2636 : : {
2637 : 3455 : gfc_symbol *res = sym->result ? sym->result : sym;
2638 : :
2639 : 3455 : if (res->attr.dimension) /* (3a) */
2640 : : {
2641 : 93 : strncpy (errmsg, _("array result"), err_len);
2642 : 93 : return true;
2643 : : }
2644 : 3362 : else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2645 : : {
2646 : 38 : strncpy (errmsg, _("pointer or allocatable result"), err_len);
2647 : 38 : return true;
2648 : : }
2649 : 3324 : else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2650 : 347 : && res->ts.u.cl->length
2651 : 166 : && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2652 : : {
2653 : 12 : strncpy (errmsg, _("result with non-constant character length"), err_len);
2654 : 12 : return true;
2655 : : }
2656 : : }
2657 : :
2658 : 15123 : if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2659 : : {
2660 : 7 : strncpy (errmsg, _("elemental procedure"), err_len);
2661 : 7 : return true;
2662 : : }
2663 : 15116 : else if (sym->attr.is_bind_c) /* (5) */
2664 : : {
2665 : 0 : strncpy (errmsg, _("bind(c) procedure"), err_len);
2666 : 0 : return true;
2667 : : }
2668 : :
2669 : : return false;
2670 : : }
2671 : :
2672 : :
2673 : : static void
2674 : 28919 : resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
2675 : : {
2676 : 28919 : gfc_gsymbol * gsym;
2677 : 28919 : gfc_namespace *ns;
2678 : 28919 : enum gfc_symbol_type type;
2679 : 28919 : char reason[200];
2680 : :
2681 : 28919 : type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2682 : :
2683 : 28919 : gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
2684 : 28919 : sym->binding_label != NULL);
2685 : :
2686 : 28919 : if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2687 : 10 : gfc_global_used (gsym, where);
2688 : :
2689 : 28919 : if ((sym->attr.if_source == IFSRC_UNKNOWN
2690 : 8869 : || sym->attr.if_source == IFSRC_IFBODY)
2691 : 24839 : && gsym->type != GSYM_UNKNOWN
2692 : 22685 : && !gsym->binding_label
2693 : 20425 : && gsym->ns
2694 : 14817 : && gsym->ns->proc_name
2695 : 14817 : && not_in_recursive (sym, gsym->ns)
2696 : 43736 : && not_entry_self_reference (sym, gsym->ns))
2697 : : {
2698 : 14817 : gfc_symbol *def_sym;
2699 : 14817 : def_sym = gsym->ns->proc_name;
2700 : :
2701 : 14817 : if (gsym->ns->resolved != -1)
2702 : : {
2703 : :
2704 : : /* Resolve the gsymbol namespace if needed. */
2705 : 14796 : if (!gsym->ns->resolved)
2706 : : {
2707 : 2753 : gfc_symbol *old_dt_list;
2708 : :
2709 : : /* Stash away derived types so that the backend_decls
2710 : : do not get mixed up. */
2711 : 2753 : old_dt_list = gfc_derived_types;
2712 : 2753 : gfc_derived_types = NULL;
2713 : :
2714 : 2753 : gfc_resolve (gsym->ns);
2715 : :
2716 : : /* Store the new derived types with the global namespace. */
2717 : 2753 : if (gfc_derived_types)
2718 : 305 : gsym->ns->derived_types = gfc_derived_types;
2719 : :
2720 : : /* Restore the derived types of this namespace. */
2721 : 2753 : gfc_derived_types = old_dt_list;
2722 : : }
2723 : :
2724 : : /* Make sure that translation for the gsymbol occurs before
2725 : : the procedure currently being resolved. */
2726 : 14796 : ns = gfc_global_ns_list;
2727 : 25094 : for (; ns && ns != gsym->ns; ns = ns->sibling)
2728 : : {
2729 : 16734 : if (ns->sibling == gsym->ns)
2730 : : {
2731 : 6436 : ns->sibling = gsym->ns->sibling;
2732 : 6436 : gsym->ns->sibling = gfc_global_ns_list;
2733 : 6436 : gfc_global_ns_list = gsym->ns;
2734 : 6436 : break;
2735 : : }
2736 : : }
2737 : :
2738 : : /* This can happen if a binding name has been specified. */
2739 : 14796 : if (gsym->binding_label && gsym->sym_name != def_sym->name)
2740 : 0 : gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2741 : :
2742 : 14796 : if (def_sym->attr.entry_master || def_sym->attr.entry)
2743 : : {
2744 : 953 : gfc_entry_list *entry;
2745 : 1659 : for (entry = gsym->ns->entries; entry; entry = entry->next)
2746 : 1659 : if (strcmp (entry->sym->name, sym->name) == 0)
2747 : : {
2748 : 953 : def_sym = entry->sym;
2749 : 953 : break;
2750 : : }
2751 : : }
2752 : : }
2753 : :
2754 : 14817 : if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2755 : : {
2756 : 6 : gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2757 : : sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2758 : 6 : gfc_typename (&def_sym->ts));
2759 : 29 : goto done;
2760 : : }
2761 : :
2762 : 14811 : if (sym->attr.if_source == IFSRC_UNKNOWN
2763 : 14811 : && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2764 : : {
2765 : 8 : gfc_error ("Explicit interface required for %qs at %L: %s",
2766 : : sym->name, &sym->declared_at, reason);
2767 : 8 : goto done;
2768 : : }
2769 : :
2770 : 14803 : bool bad_result_characteristics;
2771 : 14803 : if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2772 : : reason, sizeof(reason), NULL, NULL,
2773 : : &bad_result_characteristics))
2774 : : {
2775 : : /* Turn erros into warnings with -std=gnu and -std=legacy,
2776 : : unless a function returns a wrong type, which can lead
2777 : : to all kinds of ICEs and wrong code. */
2778 : :
2779 : 15 : if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
2780 : 2 : && !bad_result_characteristics)
2781 : 2 : gfc_errors_to_warnings (true);
2782 : :
2783 : 15 : gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
2784 : : sym->name, &sym->declared_at, reason);
2785 : 15 : sym->error = 1;
2786 : 15 : gfc_errors_to_warnings (false);
2787 : 15 : goto done;
2788 : : }
2789 : : }
2790 : :
2791 : 28919 : done:
2792 : :
2793 : 28919 : if (gsym->type == GSYM_UNKNOWN)
2794 : : {
2795 : 3843 : gsym->type = type;
2796 : 3843 : gsym->where = *where;
2797 : : }
2798 : :
2799 : 28919 : gsym->used = 1;
2800 : 28919 : }
2801 : :
2802 : :
2803 : : /************* Function resolution *************/
2804 : :
2805 : : /* Resolve a function call known to be generic.
2806 : : Section 14.1.2.4.1. */
2807 : :
2808 : : static match
2809 : 26972 : resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2810 : : {
2811 : 26972 : gfc_symbol *s;
2812 : :
2813 : 26972 : if (sym->attr.generic)
2814 : : {
2815 : 25867 : s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2816 : 25867 : if (s != NULL)
2817 : : {
2818 : 19726 : expr->value.function.name = s->name;
2819 : 19726 : expr->value.function.esym = s;
2820 : :
2821 : 19726 : if (s->ts.type != BT_UNKNOWN)
2822 : 19709 : expr->ts = s->ts;
2823 : 17 : else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2824 : 15 : expr->ts = s->result->ts;
2825 : :
2826 : 19726 : if (s->as != NULL)
2827 : : {
2828 : 54 : expr->rank = s->as->rank;
2829 : 54 : expr->corank = s->as->corank;
2830 : : }
2831 : 19672 : else if (s->result != NULL && s->result->as != NULL)
2832 : : {
2833 : 0 : expr->rank = s->result->as->rank;
2834 : 0 : expr->corank = s->result->as->corank;
2835 : : }
2836 : :
2837 : 19726 : gfc_set_sym_referenced (expr->value.function.esym);
2838 : :
2839 : 19726 : return MATCH_YES;
2840 : : }
2841 : :
2842 : : /* TODO: Need to search for elemental references in generic
2843 : : interface. */
2844 : : }
2845 : :
2846 : 7246 : if (sym->attr.intrinsic)
2847 : 1062 : return gfc_intrinsic_func_interface (expr, 0);
2848 : :
2849 : : return MATCH_NO;
2850 : : }
2851 : :
2852 : :
2853 : : static bool
2854 : 26831 : resolve_generic_f (gfc_expr *expr)
2855 : : {
2856 : 26831 : gfc_symbol *sym;
2857 : 26831 : match m;
2858 : 26831 : gfc_interface *intr = NULL;
2859 : :
2860 : 26831 : sym = expr->symtree->n.sym;
2861 : :
2862 : 26972 : for (;;)
2863 : : {
2864 : 26972 : m = resolve_generic_f0 (expr, sym);
2865 : 26972 : if (m == MATCH_YES)
2866 : : return true;
2867 : 6186 : else if (m == MATCH_ERROR)
2868 : : return false;
2869 : :
2870 : 6186 : generic:
2871 : 6189 : if (!intr)
2872 : 6145 : for (intr = sym->generic; intr; intr = intr->next)
2873 : 6061 : if (gfc_fl_struct (intr->sym->attr.flavor))
2874 : : break;
2875 : :
2876 : 6189 : if (sym->ns->parent == NULL)
2877 : : break;
2878 : 258 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2879 : :
2880 : 258 : if (sym == NULL)
2881 : : break;
2882 : 144 : if (!generic_sym (sym))
2883 : 3 : goto generic;
2884 : : }
2885 : :
2886 : : /* Last ditch attempt. See if the reference is to an intrinsic
2887 : : that possesses a matching interface. 14.1.2.4 */
2888 : 6045 : if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2889 : : {
2890 : 5 : if (gfc_init_expr_flag)
2891 : 1 : gfc_error ("Function %qs in initialization expression at %L "
2892 : : "must be an intrinsic function",
2893 : 1 : expr->symtree->n.sym->name, &expr->where);
2894 : : else
2895 : 4 : gfc_error ("There is no specific function for the generic %qs "
2896 : 4 : "at %L", expr->symtree->n.sym->name, &expr->where);
2897 : 5 : return false;
2898 : : }
2899 : :
2900 : 6040 : if (intr)
2901 : : {
2902 : 6005 : if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2903 : : NULL, false))
2904 : : return false;
2905 : 5985 : if (!gfc_use_derived (expr->ts.u.derived))
2906 : : return false;
2907 : 5985 : return resolve_structure_cons (expr, 0);
2908 : : }
2909 : :
2910 : 35 : m = gfc_intrinsic_func_interface (expr, 0);
2911 : 35 : if (m == MATCH_YES)
2912 : : return true;
2913 : :
2914 : 3 : if (m == MATCH_NO)
2915 : 3 : gfc_error ("Generic function %qs at %L is not consistent with a "
2916 : 3 : "specific intrinsic interface", expr->symtree->n.sym->name,
2917 : : &expr->where);
2918 : :
2919 : : return false;
2920 : : }
2921 : :
2922 : :
2923 : : /* Resolve a function call known to be specific. */
2924 : :
2925 : : static match
2926 : 27273 : resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2927 : : {
2928 : 27273 : match m;
2929 : :
2930 : 27273 : if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2931 : : {
2932 : 7861 : if (sym->attr.dummy)
2933 : : {
2934 : 276 : sym->attr.proc = PROC_DUMMY;
2935 : 276 : goto found;
2936 : : }
2937 : :
2938 : 7585 : sym->attr.proc = PROC_EXTERNAL;
2939 : 7585 : goto found;
2940 : : }
2941 : :
2942 : 19412 : if (sym->attr.proc == PROC_MODULE
2943 : : || sym->attr.proc == PROC_ST_FUNCTION
2944 : : || sym->attr.proc == PROC_INTERNAL)
2945 : 18674 : goto found;
2946 : :
2947 : 738 : if (sym->attr.intrinsic)
2948 : : {
2949 : 731 : m = gfc_intrinsic_func_interface (expr, 1);
2950 : 731 : if (m == MATCH_YES)
2951 : : return MATCH_YES;
2952 : 0 : if (m == MATCH_NO)
2953 : 0 : gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
2954 : : "with an intrinsic", sym->name, &expr->where);
2955 : :
2956 : 0 : return MATCH_ERROR;
2957 : : }
2958 : :
2959 : : return MATCH_NO;
2960 : :
2961 : 26535 : found:
2962 : 26535 : gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2963 : :
2964 : 26535 : if (sym->result)
2965 : 26535 : expr->ts = sym->result->ts;
2966 : : else
2967 : 0 : expr->ts = sym->ts;
2968 : 26535 : expr->value.function.name = sym->name;
2969 : 26535 : expr->value.function.esym = sym;
2970 : : /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2971 : : error(s). */
2972 : 26535 : if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
2973 : : return MATCH_ERROR;
2974 : 26534 : if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2975 : : {
2976 : 322 : expr->rank = CLASS_DATA (sym)->as->rank;
2977 : 322 : expr->corank = CLASS_DATA (sym)->as->corank;
2978 : : }
2979 : 26212 : else if (sym->as != NULL)
2980 : : {
2981 : 2261 : expr->rank = sym->as->rank;
2982 : 2261 : expr->corank = sym->as->corank;
2983 : : }
2984 : :
2985 : : return MATCH_YES;
2986 : : }
2987 : :
2988 : :
2989 : : static bool
2990 : 27266 : resolve_specific_f (gfc_expr *expr)
2991 : : {
2992 : 27266 : gfc_symbol *sym;
2993 : 27266 : match m;
2994 : :
2995 : 27266 : sym = expr->symtree->n.sym;
2996 : :
2997 : 27273 : for (;;)
2998 : : {
2999 : 27273 : m = resolve_specific_f0 (sym, expr);
3000 : 27273 : if (m == MATCH_YES)
3001 : : return true;
3002 : 8 : if (m == MATCH_ERROR)
3003 : : return false;
3004 : :
3005 : 7 : if (sym->ns->parent == NULL)
3006 : : break;
3007 : :
3008 : 7 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3009 : :
3010 : 7 : if (sym == NULL)
3011 : : break;
3012 : : }
3013 : :
3014 : 0 : gfc_error ("Unable to resolve the specific function %qs at %L",
3015 : 0 : expr->symtree->n.sym->name, &expr->where);
3016 : :
3017 : 0 : return true;
3018 : : }
3019 : :
3020 : : /* Recursively append candidate SYM to CANDIDATES. Store the number of
3021 : : candidates in CANDIDATES_LEN. */
3022 : :
3023 : : static void
3024 : 212 : lookup_function_fuzzy_find_candidates (gfc_symtree *sym,
3025 : : char **&candidates,
3026 : : size_t &candidates_len)
3027 : : {
3028 : 388 : gfc_symtree *p;
3029 : :
3030 : 388 : if (sym == NULL)
3031 : : return;
3032 : 388 : if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external)
3033 : 126 : && sym->n.sym->attr.flavor == FL_PROCEDURE)
3034 : 51 : vec_push (candidates, candidates_len, sym->name);
3035 : :
3036 : 388 : p = sym->left;
3037 : 388 : if (p)
3038 : 155 : lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
3039 : :
3040 : 388 : p = sym->right;
3041 : 388 : if (p)
3042 : : lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
3043 : : }
3044 : :
3045 : :
3046 : : /* Lookup function FN fuzzily, taking names in SYMROOT into account. */
3047 : :
3048 : : const char*
3049 : 57 : gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot)
3050 : : {
3051 : 57 : char **candidates = NULL;
3052 : 57 : size_t candidates_len = 0;
3053 : 57 : lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len);
3054 : 57 : return gfc_closest_fuzzy_match (fn, candidates);
3055 : : }
3056 : :
3057 : :
3058 : : /* Resolve a procedure call not known to be generic nor specific. */
3059 : :
3060 : : static bool
3061 : 269276 : resolve_unknown_f (gfc_expr *expr)
3062 : : {
3063 : 269276 : gfc_symbol *sym;
3064 : 269276 : gfc_typespec *ts;
3065 : :
3066 : 269276 : sym = expr->symtree->n.sym;
3067 : :
3068 : 269276 : if (sym->attr.dummy)
3069 : : {
3070 : 288 : sym->attr.proc = PROC_DUMMY;
3071 : 288 : expr->value.function.name = sym->name;
3072 : 288 : goto set_type;
3073 : : }
3074 : :
3075 : : /* See if we have an intrinsic function reference. */
3076 : :
3077 : 268988 : if (gfc_is_intrinsic (sym, 0, expr->where))
3078 : : {
3079 : 266729 : if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
3080 : : return true;
3081 : : return false;
3082 : : }
3083 : :
3084 : : /* IMPLICIT NONE (external) procedures require an explicit EXTERNAL attr. */
3085 : : /* Intrinsics were handled above, only non-intrinsics left here. */
3086 : 2259 : if (sym->attr.flavor == FL_PROCEDURE
3087 : 2256 : && sym->attr.implicit_type
3088 : 375 : && sym->ns
3089 : 375 : && sym->ns->has_implicit_none_export)
3090 : : {
3091 : 3 : gfc_error ("Missing explicit declaration with EXTERNAL attribute "
3092 : : "for symbol %qs at %L", sym->name, &sym->declared_at);
3093 : 3 : sym->error = 1;
3094 : 3 : return false;
3095 : : }
3096 : :
3097 : : /* The reference is to an external name. */
3098 : :
3099 : 2256 : sym->attr.proc = PROC_EXTERNAL;
3100 : 2256 : expr->value.function.name = sym->name;
3101 : 2256 : expr->value.function.esym = expr->symtree->n.sym;
3102 : :
3103 : 2256 : if (sym->as != NULL)
3104 : : {
3105 : 1 : expr->rank = sym->as->rank;
3106 : 1 : expr->corank = sym->as->corank;
3107 : : }
3108 : :
3109 : : /* Type of the expression is either the type of the symbol or the
3110 : : default type of the symbol. */
3111 : :
3112 : 2255 : set_type:
3113 : 2544 : gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
3114 : :
3115 : 2544 : if (sym->ts.type != BT_UNKNOWN)
3116 : 2493 : expr->ts = sym->ts;
3117 : : else
3118 : : {
3119 : 51 : ts = gfc_get_default_type (sym->name, sym->ns);
3120 : :
3121 : 51 : if (ts->type == BT_UNKNOWN)
3122 : : {
3123 : 41 : const char *guessed
3124 : 41 : = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
3125 : 41 : if (guessed)
3126 : 3 : gfc_error ("Function %qs at %L has no IMPLICIT type"
3127 : : "; did you mean %qs?",
3128 : : sym->name, &expr->where, guessed);
3129 : : else
3130 : 38 : gfc_error ("Function %qs at %L has no IMPLICIT type",
3131 : : sym->name, &expr->where);
3132 : 41 : return false;
3133 : : }
3134 : : else
3135 : 10 : expr->ts = *ts;
3136 : : }
3137 : :
3138 : : return true;
3139 : : }
3140 : :
3141 : :
3142 : : /* Return true, if the symbol is an external procedure. */
3143 : : static bool
3144 : 830402 : is_external_proc (gfc_symbol *sym)
3145 : : {
3146 : 828716 : if (!sym->attr.dummy && !sym->attr.contained
3147 : 722968 : && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
3148 : 157566 : && sym->attr.proc != PROC_ST_FUNCTION
3149 : 156971 : && !sym->attr.proc_pointer
3150 : 155863 : && !sym->attr.use_assoc
3151 : 888264 : && sym->name)
3152 : : return true;
3153 : :
3154 : : return false;
3155 : : }
3156 : :
3157 : :
3158 : : /* Figure out if a function reference is pure or not. Also set the name
3159 : : of the function for a potential error message. Return nonzero if the
3160 : : function is PURE, zero if not. */
3161 : : static bool
3162 : : pure_stmt_function (gfc_expr *, gfc_symbol *);
3163 : :
3164 : : bool
3165 : 250216 : gfc_pure_function (gfc_expr *e, const char **name)
3166 : : {
3167 : 250216 : bool pure;
3168 : 250216 : gfc_component *comp;
3169 : :
3170 : 250216 : *name = NULL;
3171 : :
3172 : 250216 : if (e->symtree != NULL
3173 : 249887 : && e->symtree->n.sym != NULL
3174 : 249887 : && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3175 : 305 : return pure_stmt_function (e, e->symtree->n.sym);
3176 : :
3177 : 249911 : comp = gfc_get_proc_ptr_comp (e);
3178 : 249911 : if (comp)
3179 : : {
3180 : 302 : pure = gfc_pure (comp->ts.interface);
3181 : 302 : *name = comp->name;
3182 : : }
3183 : 249609 : else if (e->value.function.esym)
3184 : : {
3185 : 51761 : pure = gfc_pure (e->value.function.esym);
3186 : 51761 : *name = e->value.function.esym->name;
3187 : : }
3188 : 197848 : else if (e->value.function.isym)
3189 : : {
3190 : 393622 : pure = e->value.function.isym->pure
3191 : 196811 : || e->value.function.isym->elemental;
3192 : 196811 : *name = e->value.function.isym->name;
3193 : : }
3194 : 1037 : else if (e->symtree && e->symtree->n.sym && e->symtree->n.sym->attr.dummy)
3195 : : {
3196 : : /* The function has been resolved, but esym is not yet set.
3197 : : This can happen with functions as dummy argument. */
3198 : 286 : pure = e->symtree->n.sym->attr.pure;
3199 : 286 : *name = e->symtree->n.sym->name;
3200 : : }
3201 : : else
3202 : : {
3203 : : /* Implicit functions are not pure. */
3204 : 751 : pure = 0;
3205 : 751 : *name = e->value.function.name;
3206 : : }
3207 : :
3208 : : return pure;
3209 : : }
3210 : :
3211 : :
3212 : : /* Check if the expression is a reference to an implicitly pure function. */
3213 : :
3214 : : bool
3215 : 37103 : gfc_implicit_pure_function (gfc_expr *e)
3216 : : {
3217 : 37103 : gfc_component *comp = gfc_get_proc_ptr_comp (e);
3218 : 37103 : if (comp)
3219 : 286 : return gfc_implicit_pure (comp->ts.interface);
3220 : 36817 : else if (e->value.function.esym)
3221 : 31457 : return gfc_implicit_pure (e->value.function.esym);
3222 : : else
3223 : : return 0;
3224 : : }
3225 : :
3226 : :
3227 : : static bool
3228 : 981 : impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
3229 : : int *f ATTRIBUTE_UNUSED)
3230 : : {
3231 : 981 : const char *name;
3232 : :
3233 : : /* Don't bother recursing into other statement functions
3234 : : since they will be checked individually for purity. */
3235 : 981 : if (e->expr_type != EXPR_FUNCTION
3236 : 343 : || !e->symtree
3237 : 343 : || e->symtree->n.sym == sym
3238 : 20 : || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3239 : : return false;
3240 : :
3241 : 19 : return gfc_pure_function (e, &name) ? false : true;
3242 : : }
3243 : :
3244 : :
3245 : : static bool
3246 : 305 : pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
3247 : : {
3248 : 305 : return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
3249 : : }
3250 : :
3251 : :
3252 : : /* Check if an impure function is allowed in the current context. */
3253 : :
3254 : 238491 : static bool check_pure_function (gfc_expr *e)
3255 : : {
3256 : 238491 : const char *name = NULL;
3257 : 238491 : code_stack *stack;
3258 : 238491 : bool saw_block = false;
3259 : :
3260 : : /* A BLOCK construct within a DO CONCURRENT construct leads to
3261 : : gfc_do_concurrent_flag = 0 when the check for an impure function
3262 : : occurs. Check the stack to see if the source code has a nested
3263 : : BLOCK construct. */
3264 : :
3265 : 551378 : for (stack = cs_base; stack; stack = stack->prev)
3266 : : {
3267 : 312889 : if (!saw_block && stack->current->op == EXEC_BLOCK)
3268 : : {
3269 : 6738 : saw_block = true;
3270 : 6738 : continue;
3271 : : }
3272 : :
3273 : 5087 : if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
3274 : : {
3275 : 9 : bool is_pure;
3276 : 312887 : is_pure = (e->value.function.isym
3277 : 8 : && (e->value.function.isym->pure
3278 : 1 : || e->value.function.isym->elemental))
3279 : 10 : || (e->value.function.esym
3280 : 1 : && (e->value.function.esym->attr.pure
3281 : 1 : || e->value.function.esym->attr.elemental));
3282 : 2 : if (!is_pure)
3283 : : {
3284 : 2 : gfc_error ("Reference to impure function at %L inside a "
3285 : : "DO CONCURRENT", &e->where);
3286 : 2 : return false;
3287 : : }
3288 : : }
3289 : : }
3290 : :
3291 : 238489 : if (!gfc_pure_function (e, &name) && name)
3292 : : {
3293 : 35837 : if (forall_flag)
3294 : : {
3295 : 4 : gfc_error ("Reference to impure function %qs at %L inside a "
3296 : : "FORALL %s", name, &e->where,
3297 : : forall_flag == 2 ? "mask" : "block");
3298 : 4 : return false;
3299 : : }
3300 : 35833 : else if (gfc_do_concurrent_flag)
3301 : : {
3302 : 2 : gfc_error ("Reference to impure function %qs at %L inside a "
3303 : : "DO CONCURRENT %s", name, &e->where,
3304 : : gfc_do_concurrent_flag == 2 ? "mask" : "block");
3305 : 2 : return false;
3306 : : }
3307 : 35831 : else if (gfc_pure (NULL))
3308 : : {
3309 : 5 : gfc_error ("Reference to impure function %qs at %L "
3310 : : "within a PURE procedure", name, &e->where);
3311 : 5 : return false;
3312 : : }
3313 : 35826 : if (!gfc_implicit_pure_function (e))
3314 : 29622 : gfc_unset_implicit_pure (NULL);
3315 : : }
3316 : : return true;
3317 : : }
3318 : :
3319 : :
3320 : : /* Update current procedure's array_outer_dependency flag, considering
3321 : : a call to procedure SYM. */
3322 : :
3323 : : static void
3324 : 129528 : update_current_proc_array_outer_dependency (gfc_symbol *sym)
3325 : : {
3326 : : /* Check to see if this is a sibling function that has not yet
3327 : : been resolved. */
3328 : 129528 : gfc_namespace *sibling = gfc_current_ns->sibling;
3329 : 245803 : for (; sibling; sibling = sibling->sibling)
3330 : : {
3331 : 123058 : if (sibling->proc_name == sym)
3332 : : {
3333 : 6783 : gfc_resolve (sibling);
3334 : 6783 : break;
3335 : : }
3336 : : }
3337 : :
3338 : : /* If SYM has references to outer arrays, so has the procedure calling
3339 : : SYM. If SYM is a procedure pointer, we can assume the worst. */
3340 : 129528 : if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
3341 : 66826 : && gfc_current_ns->proc_name)
3342 : 66782 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3343 : 129528 : }
3344 : :
3345 : :
3346 : : /* Resolve a function call, which means resolving the arguments, then figuring
3347 : : out which entity the name refers to. */
3348 : :
3349 : : static bool
3350 : 336311 : resolve_function (gfc_expr *expr)
3351 : : {
3352 : 336311 : gfc_actual_arglist *arg;
3353 : 336311 : gfc_symbol *sym;
3354 : 336311 : bool t;
3355 : 336311 : int temp;
3356 : 336311 : procedure_type p = PROC_INTRINSIC;
3357 : 336311 : bool no_formal_args;
3358 : :
3359 : 336311 : sym = NULL;
3360 : 336311 : if (expr->symtree)
3361 : 335982 : sym = expr->symtree->n.sym;
3362 : :
3363 : : /* If this is a procedure pointer component, it has already been resolved. */
3364 : 336311 : if (gfc_is_proc_ptr_comp (expr))
3365 : : return true;
3366 : :
3367 : : /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
3368 : : another caf_get. */
3369 : 335914 : if (sym && sym->attr.intrinsic
3370 : 8341 : && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
3371 : 8341 : || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
3372 : : return true;
3373 : :
3374 : 335914 : if (expr->ref)
3375 : : {
3376 : 1 : gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name,
3377 : : &expr->where);
3378 : 1 : return false;
3379 : : }
3380 : :
3381 : 335584 : if (sym && sym->attr.intrinsic
3382 : 344254 : && !gfc_resolve_intrinsic (sym, &expr->where))
3383 : : return false;
3384 : :
3385 : 335913 : if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
3386 : : {
3387 : 4 : gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
3388 : 4 : return false;
3389 : : }
3390 : :
3391 : : /* If this is a deferred TBP with an abstract interface (which may
3392 : : of course be referenced), expr->value.function.esym will be set. */
3393 : 335580 : if (sym && sym->attr.abstract && !expr->value.function.esym)
3394 : : {
3395 : 1 : gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3396 : : sym->name, &expr->where);
3397 : 1 : return false;
3398 : : }
3399 : :
3400 : : /* If this is a deferred TBP with an abstract interface, its result
3401 : : cannot be an assumed length character (F2003: C418). */
3402 : 335579 : if (sym && sym->attr.abstract && sym->attr.function
3403 : 191 : && sym->result->ts.u.cl
3404 : 157 : && sym->result->ts.u.cl->length == NULL
3405 : 2 : && !sym->result->ts.deferred)
3406 : : {
3407 : 1 : gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
3408 : : "character length result (F2008: C418)", sym->name,
3409 : : &sym->declared_at);
3410 : 1 : return false;
3411 : : }
3412 : :
3413 : : /* Switch off assumed size checking and do this again for certain kinds
3414 : : of procedure, once the procedure itself is resolved. */
3415 : 335907 : need_full_assumed_size++;
3416 : :
3417 : 335907 : if (expr->symtree && expr->symtree->n.sym)
3418 : 335578 : p = expr->symtree->n.sym->attr.proc;
3419 : :
3420 : 335907 : if (expr->value.function.isym && expr->value.function.isym->inquiry)
3421 : 1078 : inquiry_argument = true;
3422 : 335578 : no_formal_args = sym && is_external_proc (sym)
3423 : 349328 : && gfc_sym_get_dummy_args (sym) == NULL;
3424 : :
3425 : 335907 : if (!resolve_actual_arglist (expr->value.function.actual,
3426 : : p, no_formal_args))
3427 : : {
3428 : 66 : inquiry_argument = false;
3429 : 66 : return false;
3430 : : }
3431 : :
3432 : 335841 : inquiry_argument = false;
3433 : :
3434 : : /* Resume assumed_size checking. */
3435 : 335841 : need_full_assumed_size--;
3436 : :
3437 : : /* If the procedure is external, check for usage. */
3438 : 335841 : if (sym && is_external_proc (sym))
3439 : 13402 : resolve_global_procedure (sym, &expr->where, 0);
3440 : :
3441 : 335841 : if (sym && sym->ts.type == BT_CHARACTER
3442 : 3157 : && sym->ts.u.cl
3443 : 3097 : && sym->ts.u.cl->length == NULL
3444 : 585 : && !sym->attr.dummy
3445 : 578 : && !sym->ts.deferred
3446 : 2 : && expr->value.function.esym == NULL
3447 : 2 : && !sym->attr.contained)
3448 : : {
3449 : : /* Internal procedures are taken care of in resolve_contained_fntype. */
3450 : 1 : gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
3451 : : "be used at %L since it is not a dummy argument",
3452 : : sym->name, &expr->where);
3453 : 1 : return false;
3454 : : }
3455 : :
3456 : : /* Add and check formal interface when -fc-prototypes-external is in
3457 : : force, see comment in resolve_call(). */
3458 : :
3459 : 335840 : if (warn_external_argument_mismatch && sym && sym->attr.dummy
3460 : 18 : && sym->attr.external)
3461 : : {
3462 : 18 : if (sym->formal)
3463 : : {
3464 : 6 : bool conflict;
3465 : 6 : conflict = !gfc_compare_actual_formal (&expr->value.function.actual,
3466 : : sym->formal, 0, 0, 0, NULL);
3467 : 6 : if (conflict)
3468 : : {
3469 : 6 : sym->ext_dummy_arglist_mismatch = 1;
3470 : 6 : gfc_warning (OPT_Wexternal_argument_mismatch,
3471 : : "Different argument lists in external dummy "
3472 : : "function %s at %L and %L", sym->name,
3473 : : &expr->where, &sym->formal_at);
3474 : : }
3475 : : }
3476 : 12 : else if (!sym->formal_resolved)
3477 : : {
3478 : 6 : gfc_get_formal_from_actual_arglist (sym, expr->value.function.actual);
3479 : 6 : sym->formal_at = expr->where;
3480 : : }
3481 : : }
3482 : : /* See if function is already resolved. */
3483 : :
3484 : 335840 : if (expr->value.function.name != NULL
3485 : 324159 : || expr->value.function.isym != NULL)
3486 : : {
3487 : 12467 : if (expr->ts.type == BT_UNKNOWN)
3488 : 3 : expr->ts = sym->ts;
3489 : : t = true;
3490 : : }
3491 : : else
3492 : : {
3493 : : /* Apply the rules of section 14.1.2. */
3494 : :
3495 : 323373 : switch (procedure_kind (sym))
3496 : : {
3497 : 26831 : case PTYPE_GENERIC:
3498 : 26831 : t = resolve_generic_f (expr);
3499 : 26831 : break;
3500 : :
3501 : 27266 : case PTYPE_SPECIFIC:
3502 : 27266 : t = resolve_specific_f (expr);
3503 : 27266 : break;
3504 : :
3505 : 269276 : case PTYPE_UNKNOWN:
3506 : 269276 : t = resolve_unknown_f (expr);
3507 : 269276 : break;
3508 : :
3509 : : default:
3510 : : gfc_internal_error ("resolve_function(): bad function type");
3511 : : }
3512 : : }
3513 : :
3514 : : /* If the expression is still a function (it might have simplified),
3515 : : then we check to see if we are calling an elemental function. */
3516 : :
3517 : 335840 : if (expr->expr_type != EXPR_FUNCTION)
3518 : : return t;
3519 : :
3520 : : /* Walk the argument list looking for invalid BOZ. */
3521 : 724949 : for (arg = expr->value.function.actual; arg; arg = arg->next)
3522 : 486738 : if (arg->expr && arg->expr->ts.type == BT_BOZ)
3523 : : {
3524 : 5 : gfc_error ("A BOZ literal constant at %L cannot appear as an "
3525 : : "actual argument in a function reference",
3526 : : &arg->expr->where);
3527 : 5 : return false;
3528 : : }
3529 : :
3530 : 238211 : temp = need_full_assumed_size;
3531 : 238211 : need_full_assumed_size = 0;
3532 : :
3533 : 238211 : if (!resolve_elemental_actual (expr, NULL))
3534 : : return false;
3535 : :
3536 : 238208 : if (omp_workshare_flag
3537 : 32 : && expr->value.function.esym
3538 : 238213 : && ! gfc_elemental (expr->value.function.esym))
3539 : : {
3540 : 4 : gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3541 : 4 : "in WORKSHARE construct", expr->value.function.esym->name,
3542 : : &expr->where);
3543 : 4 : t = false;
3544 : : }
3545 : :
3546 : : #define GENERIC_ID expr->value.function.isym->id
3547 : 238204 : else if (expr->value.function.actual != NULL
3548 : 230409 : && expr->value.function.isym != NULL
3549 : 186216 : && GENERIC_ID != GFC_ISYM_LBOUND
3550 : : && GENERIC_ID != GFC_ISYM_LCOBOUND
3551 : : && GENERIC_ID != GFC_ISYM_UCOBOUND
3552 : : && GENERIC_ID != GFC_ISYM_LEN
3553 : : && GENERIC_ID != GFC_ISYM_LOC
3554 : : && GENERIC_ID != GFC_ISYM_C_LOC
3555 : : && GENERIC_ID != GFC_ISYM_PRESENT)
3556 : : {
3557 : : /* Array intrinsics must also have the last upper bound of an
3558 : : assumed size array argument. UBOUND and SIZE have to be
3559 : : excluded from the check if the second argument is anything
3560 : : than a constant. */
3561 : :
3562 : 526071 : for (arg = expr->value.function.actual; arg; arg = arg->next)
3563 : : {
3564 : 365179 : if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3565 : 44927 : && arg == expr->value.function.actual
3566 : 16579 : && arg->next != NULL && arg->next->expr)
3567 : : {
3568 : 8208 : if (arg->next->expr->expr_type != EXPR_CONSTANT)
3569 : : break;
3570 : :
3571 : 7984 : if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
3572 : : break;
3573 : :
3574 : 7984 : if ((int)mpz_get_si (arg->next->expr->value.integer)
3575 : 7984 : < arg->expr->rank)
3576 : : break;
3577 : : }
3578 : :
3579 : 362776 : if (arg->expr != NULL
3580 : 242837 : && arg->expr->rank > 0
3581 : 480146 : && resolve_assumed_size_actual (arg->expr))
3582 : : return false;
3583 : : }
3584 : : }
3585 : : #undef GENERIC_ID
3586 : :
3587 : 238205 : need_full_assumed_size = temp;
3588 : :
3589 : 238205 : if (!check_pure_function(expr))
3590 : 12 : t = false;
3591 : :
3592 : : /* Functions without the RECURSIVE attribution are not allowed to
3593 : : * call themselves. */
3594 : 238205 : if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3595 : : {
3596 : 50555 : gfc_symbol *esym;
3597 : 50555 : esym = expr->value.function.esym;
3598 : :
3599 : 50555 : if (is_illegal_recursion (esym, gfc_current_ns))
3600 : : {
3601 : 5 : if (esym->attr.entry && esym->ns->entries)
3602 : 3 : gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3603 : : " function %qs is not RECURSIVE",
3604 : 3 : esym->name, &expr->where, esym->ns->entries->sym->name);
3605 : : else
3606 : 2 : gfc_error ("Function %qs at %L cannot be called recursively, as it"
3607 : : " is not RECURSIVE", esym->name, &expr->where);
3608 : :
3609 : : t = false;
3610 : : }
3611 : : }
3612 : :
3613 : : /* Character lengths of use associated functions may contains references to
3614 : : symbols not referenced from the current program unit otherwise. Make sure
3615 : : those symbols are marked as referenced. */
3616 : :
3617 : 238205 : if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3618 : 3295 : && expr->value.function.esym->attr.use_assoc)
3619 : : {
3620 : 1236 : gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3621 : : }
3622 : :
3623 : : /* Make sure that the expression has a typespec that works. */
3624 : 238205 : if (expr->ts.type == BT_UNKNOWN)
3625 : : {
3626 : 890 : if (expr->symtree->n.sym->result
3627 : 881 : && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3628 : 559 : && !expr->symtree->n.sym->result->attr.proc_pointer)
3629 : 559 : expr->ts = expr->symtree->n.sym->result->ts;
3630 : : }
3631 : :
3632 : : /* These derived types with an incomplete namespace, arising from use
3633 : : association, cause gfc_get_derived_vtab to segfault. If the function
3634 : : namespace does not suffice, something is badly wrong. */
3635 : 238205 : if (expr->ts.type == BT_DERIVED
3636 : 8915 : && !expr->ts.u.derived->ns->proc_name)
3637 : : {
3638 : 3 : gfc_symbol *der;
3639 : 3 : gfc_find_symbol (expr->ts.u.derived->name, expr->symtree->n.sym->ns, 1, &der);
3640 : 3 : if (der)
3641 : : {
3642 : 3 : expr->ts.u.derived->refs--;
3643 : 3 : expr->ts.u.derived = der;
3644 : 3 : der->refs++;
3645 : : }
3646 : : else
3647 : 0 : expr->ts.u.derived->ns = expr->symtree->n.sym->ns;
3648 : : }
3649 : :
3650 : 238205 : if (!expr->ref && !expr->value.function.isym)
3651 : : {
3652 : 51868 : if (expr->value.function.esym)
3653 : 50831 : update_current_proc_array_outer_dependency (expr->value.function.esym);
3654 : : else
3655 : 1037 : update_current_proc_array_outer_dependency (sym);
3656 : : }
3657 : 186337 : else if (expr->ref)
3658 : : /* typebound procedure: Assume the worst. */
3659 : 0 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3660 : :
3661 : 238205 : if (expr->value.function.esym
3662 : 50831 : && expr->value.function.esym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
3663 : 2 : gfc_warning (OPT_Wdeprecated_declarations,
3664 : : "Using function %qs at %L is deprecated",
3665 : : sym->name, &expr->where);
3666 : : return t;
3667 : : }
3668 : :
3669 : :
3670 : : /************* Subroutine resolution *************/
3671 : :
3672 : : static bool
3673 : 75367 : pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3674 : : {
3675 : 75367 : code_stack *stack;
3676 : 75367 : bool saw_block = false;
3677 : :
3678 : 75367 : if (gfc_pure (sym))
3679 : : return true;
3680 : :
3681 : : /* A BLOCK construct within a DO CONCURRENT construct leads to
3682 : : gfc_do_concurrent_flag = 0 when the check for an impure subroutine
3683 : : occurs. Walk up the stack to see if the source code has a nested
3684 : : construct. */
3685 : :
3686 : 155980 : for (stack = cs_base; stack; stack = stack->prev)
3687 : : {
3688 : 86018 : if (stack->current->op == EXEC_BLOCK)
3689 : : {
3690 : 1875 : saw_block = true;
3691 : 1875 : continue;
3692 : : }
3693 : :
3694 : 84143 : if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
3695 : : {
3696 : :
3697 : 2 : bool is_pure = true;
3698 : 86018 : is_pure = sym->attr.pure || sym->attr.elemental;
3699 : :
3700 : 2 : if (!is_pure)
3701 : : {
3702 : 2 : gfc_error ("Subroutine call at %L in a DO CONCURRENT block "
3703 : : "is not PURE", loc);
3704 : 2 : return false;
3705 : : }
3706 : : }
3707 : : }
3708 : :
3709 : 69962 : if (forall_flag)
3710 : : {
3711 : 0 : gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3712 : : name, loc);
3713 : 0 : return false;
3714 : : }
3715 : 69962 : else if (gfc_do_concurrent_flag)
3716 : : {
3717 : 6 : gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3718 : : "PURE", name, loc);
3719 : 6 : return false;
3720 : : }
3721 : 69956 : else if (gfc_pure (NULL))
3722 : : {
3723 : 4 : gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3724 : 4 : return false;
3725 : : }
3726 : :
3727 : 69952 : gfc_unset_implicit_pure (NULL);
3728 : 69952 : return true;
3729 : : }
3730 : :
3731 : :
3732 : : static match
3733 : 2215 : resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3734 : : {
3735 : 2215 : gfc_symbol *s;
3736 : :
3737 : 2215 : if (sym->attr.generic)
3738 : : {
3739 : 2214 : s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3740 : 2214 : if (s != NULL)
3741 : : {
3742 : 2205 : c->resolved_sym = s;
3743 : 2205 : if (!pure_subroutine (s, s->name, &c->loc))
3744 : : return MATCH_ERROR;
3745 : 2205 : return MATCH_YES;
3746 : : }
3747 : :
3748 : : /* TODO: Need to search for elemental references in generic interface. */
3749 : : }
3750 : :
3751 : 10 : if (sym->attr.intrinsic)
3752 : 1 : return gfc_intrinsic_sub_interface (c, 0);
3753 : :
3754 : : return MATCH_NO;
3755 : : }
3756 : :
3757 : :
3758 : : static bool
3759 : 2213 : resolve_generic_s (gfc_code *c)
3760 : : {
3761 : 2213 : gfc_symbol *sym;
3762 : 2213 : match m;
3763 : :
3764 : 2213 : sym = c->symtree->n.sym;
3765 : :
3766 : 2215 : for (;;)
3767 : : {
3768 : 2215 : m = resolve_generic_s0 (c, sym);
3769 : 2215 : if (m == MATCH_YES)
3770 : : return true;
3771 : 9 : else if (m == MATCH_ERROR)
3772 : : return false;
3773 : :
3774 : 9 : generic:
3775 : 9 : if (sym->ns->parent == NULL)
3776 : : break;
3777 : 3 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3778 : :
3779 : 3 : if (sym == NULL)
3780 : : break;
3781 : 2 : if (!generic_sym (sym))
3782 : 0 : goto generic;
3783 : : }
3784 : :
3785 : : /* Last ditch attempt. See if the reference is to an intrinsic
3786 : : that possesses a matching interface. 14.1.2.4 */
3787 : 7 : sym = c->symtree->n.sym;
3788 : :
3789 : 7 : if (!gfc_is_intrinsic (sym, 1, c->loc))
3790 : : {
3791 : 4 : gfc_error ("There is no specific subroutine for the generic %qs at %L",
3792 : : sym->name, &c->loc);
3793 : 4 : return false;
3794 : : }
3795 : :
3796 : 3 : m = gfc_intrinsic_sub_interface (c, 0);
3797 : 3 : if (m == MATCH_YES)
3798 : : return true;
3799 : 1 : if (m == MATCH_NO)
3800 : 1 : gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3801 : : "intrinsic subroutine interface", sym->name, &c->loc);
3802 : :
3803 : : return false;
3804 : : }
3805 : :
3806 : :
3807 : : /* Resolve a subroutine call known to be specific. */
3808 : :
3809 : : static match
3810 : 61549 : resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3811 : : {
3812 : 61549 : match m;
3813 : :
3814 : 61549 : if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3815 : : {
3816 : 5622 : if (sym->attr.dummy)
3817 : : {
3818 : 256 : sym->attr.proc = PROC_DUMMY;
3819 : 256 : goto found;
3820 : : }
3821 : :
3822 : 5366 : sym->attr.proc = PROC_EXTERNAL;
3823 : 5366 : goto found;
3824 : : }
3825 : :
3826 : 55927 : if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3827 : 55927 : goto found;
3828 : :
3829 : 0 : if (sym->attr.intrinsic)
3830 : : {
3831 : 0 : m = gfc_intrinsic_sub_interface (c, 1);
3832 : 0 : if (m == MATCH_YES)
3833 : : return MATCH_YES;
3834 : 0 : if (m == MATCH_NO)
3835 : 0 : gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3836 : : "with an intrinsic", sym->name, &c->loc);
3837 : :
3838 : 0 : return MATCH_ERROR;
3839 : : }
3840 : :
3841 : : return MATCH_NO;
3842 : :
3843 : 61549 : found:
3844 : 61549 : gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3845 : :
3846 : 61549 : c->resolved_sym = sym;
3847 : 61549 : if (!pure_subroutine (sym, sym->name, &c->loc))
3848 : : return MATCH_ERROR;
3849 : :
3850 : : return MATCH_YES;
3851 : : }
3852 : :
3853 : :
3854 : : static bool
3855 : 61549 : resolve_specific_s (gfc_code *c)
3856 : : {
3857 : 61549 : gfc_symbol *sym;
3858 : 61549 : match m;
3859 : :
3860 : 61549 : sym = c->symtree->n.sym;
3861 : :
3862 : 61549 : for (;;)
3863 : : {
3864 : 61549 : m = resolve_specific_s0 (c, sym);
3865 : 61549 : if (m == MATCH_YES)
3866 : : return true;
3867 : 7 : if (m == MATCH_ERROR)
3868 : : return false;
3869 : :
3870 : 0 : if (sym->ns->parent == NULL)
3871 : : break;
3872 : :
3873 : 0 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3874 : :
3875 : 0 : if (sym == NULL)
3876 : : break;
3877 : : }
3878 : :
3879 : 0 : sym = c->symtree->n.sym;
3880 : 0 : gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3881 : : sym->name, &c->loc);
3882 : :
3883 : 0 : return false;
3884 : : }
3885 : :
3886 : :
3887 : : /* Resolve a subroutine call not known to be generic nor specific. */
3888 : :
3889 : : static bool
3890 : 15517 : resolve_unknown_s (gfc_code *c)
3891 : : {
3892 : 15517 : gfc_symbol *sym;
3893 : :
3894 : 15517 : sym = c->symtree->n.sym;
3895 : :
3896 : 15517 : if (sym->attr.dummy)
3897 : : {
3898 : 20 : sym->attr.proc = PROC_DUMMY;
3899 : 20 : goto found;
3900 : : }
3901 : :
3902 : : /* See if we have an intrinsic function reference. */
3903 : :
3904 : 15497 : if (gfc_is_intrinsic (sym, 1, c->loc))
3905 : : {
3906 : 4026 : if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3907 : : return true;
3908 : 309 : return false;
3909 : : }
3910 : :
3911 : : /* The reference is to an external name. */
3912 : :
3913 : 11471 : found:
3914 : 11491 : gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3915 : :
3916 : 11491 : c->resolved_sym = sym;
3917 : :
3918 : 11491 : return pure_subroutine (sym, sym->name, &c->loc);
3919 : : }
3920 : :
3921 : :
3922 : :
3923 : : static bool
3924 : 805 : check_sym_import_status (gfc_symbol *sym, gfc_symtree *s, gfc_expr *e,
3925 : : gfc_code *c, gfc_namespace *ns)
3926 : : {
3927 : 805 : locus *here;
3928 : :
3929 : : /* If the type has been imported then its vtype functions are OK. */
3930 : 805 : if (e && e->expr_type == EXPR_FUNCTION && sym->attr.vtype)
3931 : : return true;
3932 : :
3933 : : if (e)
3934 : 791 : here = &e->where;
3935 : : else
3936 : 7 : here = &c->loc;
3937 : :
3938 : 798 : if (s && !s->import_only)
3939 : 705 : s = gfc_find_symtree (ns->sym_root, sym->name);
3940 : :
3941 : 798 : if (ns->import_state == IMPORT_ONLY
3942 : 75 : && sym->ns != ns
3943 : 58 : && (!s || !s->import_only))
3944 : : {
3945 : 21 : gfc_error ("F2018: C8102 %qs at %L is host associated but does not "
3946 : : "appear in an IMPORT or IMPORT, ONLY list", sym->name, here);
3947 : 21 : return false;
3948 : : }
3949 : 777 : else if (ns->import_state == IMPORT_NONE
3950 : 27 : && sym->ns != ns)
3951 : : {
3952 : 12 : gfc_error ("F2018: C8102 %qs at %L is host associated in a scope that "
3953 : : "has IMPORT, NONE", sym->name, here);
3954 : 12 : return false;
3955 : : }
3956 : : return true;
3957 : : }
3958 : :
3959 : :
3960 : : static bool
3961 : 6918 : check_import_status (gfc_expr *e)
3962 : : {
3963 : 6918 : gfc_symtree *st;
3964 : 6918 : gfc_ref *ref;
3965 : 6918 : gfc_symbol *sym, *der;
3966 : 6918 : gfc_namespace *ns = gfc_current_ns;
3967 : :
3968 : 6918 : switch (e->expr_type)
3969 : : {
3970 : 727 : case EXPR_VARIABLE:
3971 : 727 : case EXPR_FUNCTION:
3972 : 727 : case EXPR_SUBSTRING:
3973 : 727 : sym = e->symtree ? e->symtree->n.sym : NULL;
3974 : :
3975 : : /* Check the symbol itself. */
3976 : 727 : if (sym
3977 : 727 : && !(ns->proc_name
3978 : : && (sym == ns->proc_name))
3979 : 1450 : && !check_sym_import_status (sym, e->symtree, e, NULL, ns))
3980 : : return false;
3981 : :
3982 : : /* Check the declared derived type. */
3983 : 717 : if (sym->ts.type == BT_DERIVED)
3984 : : {
3985 : 16 : der = sym->ts.u.derived;
3986 : 16 : st = gfc_find_symtree (ns->sym_root, der->name);
3987 : :
3988 : 16 : if (!check_sym_import_status (der, st, e, NULL, ns))
3989 : : return false;
3990 : : }
3991 : 701 : else if (sym->ts.type == BT_CLASS && !UNLIMITED_POLY (sym))
3992 : : {
3993 : 44 : der = CLASS_DATA (sym) ? CLASS_DATA (sym)->ts.u.derived
3994 : : : sym->ts.u.derived;
3995 : 44 : st = gfc_find_symtree (ns->sym_root, der->name);
3996 : :
3997 : 44 : if (!check_sym_import_status (der, st, e, NULL, ns))
3998 : : return false;
3999 : : }
4000 : :
4001 : : /* Check the declared derived types of component references. */
4002 : 724 : for (ref = e->ref; ref; ref = ref->next)
4003 : 20 : if (ref->type == REF_COMPONENT)
4004 : : {
4005 : 19 : gfc_component *c = ref->u.c.component;
4006 : 19 : if (c->ts.type == BT_DERIVED)
4007 : : {
4008 : 7 : der = c->ts.u.derived;
4009 : 7 : st = gfc_find_symtree (ns->sym_root, der->name);
4010 : 7 : if (!check_sym_import_status (der, st, e, NULL, ns))
4011 : : return false;
4012 : : }
4013 : 12 : else if (c->ts.type == BT_CLASS && !UNLIMITED_POLY (c))
4014 : : {
4015 : 0 : der = CLASS_DATA (c) ? CLASS_DATA (c)->ts.u.derived
4016 : : : c->ts.u.derived;
4017 : 0 : st = gfc_find_symtree (ns->sym_root, der->name);
4018 : 0 : if (!check_sym_import_status (der, st, e, NULL, ns))
4019 : : return false;
4020 : : }
4021 : : }
4022 : :
4023 : : break;
4024 : :
4025 : 8 : case EXPR_ARRAY:
4026 : 8 : case EXPR_STRUCTURE:
4027 : : /* Check the declared derived type. */
4028 : 8 : if (e->ts.type == BT_DERIVED)
4029 : : {
4030 : 8 : der = e->ts.u.derived;
4031 : 8 : st = gfc_find_symtree (ns->sym_root, der->name);
4032 : :
4033 : 8 : if (!check_sym_import_status (der, st, e, NULL, ns))
4034 : : return false;
4035 : : }
4036 : 0 : else if (e->ts.type == BT_CLASS && !UNLIMITED_POLY (e))
4037 : : {
4038 : 0 : der = CLASS_DATA (e) ? CLASS_DATA (e)->ts.u.derived
4039 : : : e->ts.u.derived;
4040 : 0 : st = gfc_find_symtree (ns->sym_root, der->name);
4041 : :
4042 : 0 : if (!check_sym_import_status (der, st, e, NULL, ns))
4043 : : return false;
4044 : : }
4045 : :
4046 : : break;
4047 : :
4048 : : /* Either not applicable or resolved away
4049 : : case EXPR_OP:
4050 : : case EXPR_UNKNOWN:
4051 : : case EXPR_CONSTANT:
4052 : : case EXPR_NULL:
4053 : : case EXPR_COMPCALL:
4054 : : case EXPR_PPC: */
4055 : :
4056 : : default:
4057 : : break;
4058 : : }
4059 : :
4060 : : return true;
4061 : : }
4062 : :
4063 : :
4064 : : /* Resolve a subroutine call. Although it was tempting to use the same code
4065 : : for functions, subroutines and functions are stored differently and this
4066 : : makes things awkward. */
4067 : :
4068 : :
4069 : : static bool
4070 : 79368 : resolve_call (gfc_code *c)
4071 : : {
4072 : 79368 : bool t;
4073 : 79368 : procedure_type ptype = PROC_INTRINSIC;
4074 : 79368 : gfc_symbol *csym, *sym;
4075 : 79368 : bool no_formal_args;
4076 : :
4077 : 79368 : csym = c->symtree ? c->symtree->n.sym : NULL;
4078 : :
4079 : 79368 : if (csym && csym->ts.type != BT_UNKNOWN)
4080 : : {
4081 : 4 : gfc_error ("%qs at %L has a type, which is not consistent with "
4082 : : "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
4083 : 4 : return false;
4084 : : }
4085 : :
4086 : 79364 : if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
4087 : : {
4088 : 16709 : gfc_symtree *st;
4089 : 16709 : gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
4090 : 16709 : sym = st ? st->n.sym : NULL;
4091 : 16709 : if (sym && csym != sym
4092 : 3 : && sym->ns == gfc_current_ns
4093 : 3 : && sym->attr.flavor == FL_PROCEDURE
4094 : 3 : && sym->attr.contained)
4095 : : {
4096 : 3 : sym->refs++;
4097 : 3 : if (csym->attr.generic)
4098 : 2 : c->symtree->n.sym = sym;
4099 : : else
4100 : 1 : c->symtree = st;
4101 : 3 : csym = c->symtree->n.sym;
4102 : : }
4103 : : }
4104 : :
4105 : : /* If this ia a deferred TBP, c->expr1 will be set. */
4106 : 79364 : if (!c->expr1 && csym)
4107 : : {
4108 : 77703 : if (csym->attr.abstract)
4109 : : {
4110 : 1 : gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
4111 : : csym->name, &c->loc);
4112 : 1 : return false;
4113 : : }
4114 : :
4115 : : /* Subroutines without the RECURSIVE attribution are not allowed to
4116 : : call themselves. */
4117 : 77702 : if (is_illegal_recursion (csym, gfc_current_ns))
4118 : : {
4119 : 4 : if (csym->attr.entry && csym->ns->entries)
4120 : 2 : gfc_error ("ENTRY %qs at %L cannot be called recursively, "
4121 : : "as subroutine %qs is not RECURSIVE",
4122 : 2 : csym->name, &c->loc, csym->ns->entries->sym->name);
4123 : : else
4124 : 2 : gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
4125 : : "as it is not RECURSIVE", csym->name, &c->loc);
4126 : :
4127 : 79363 : t = false;
4128 : : }
4129 : : }
4130 : :
4131 : : /* Switch off assumed size checking and do this again for certain kinds
4132 : : of procedure, once the procedure itself is resolved. */
4133 : 79363 : need_full_assumed_size++;
4134 : :
4135 : 79363 : if (csym)
4136 : 79363 : ptype = csym->attr.proc;
4137 : :
4138 : 79363 : no_formal_args = csym && is_external_proc (csym)
4139 : 15522 : && gfc_sym_get_dummy_args (csym) == NULL;
4140 : 79363 : if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
4141 : : return false;
4142 : :
4143 : : /* Resume assumed_size checking. */
4144 : 79330 : need_full_assumed_size--;
4145 : :
4146 : : /* If external, check for usage. */
4147 : 79330 : if (csym && is_external_proc (csym))
4148 : 15517 : resolve_global_procedure (csym, &c->loc, 1);
4149 : :
4150 : : /* If we have an external dummy argument, we want to write out its arguments
4151 : : with -fc-prototypes-external. Code like
4152 : :
4153 : : subroutine foo(a,n)
4154 : : external a
4155 : : if (n == 1) call a(1)
4156 : : if (n == 2) call a(2,3)
4157 : : end subroutine foo
4158 : :
4159 : : is actually legal Fortran, but it is not possible to generate a C23-
4160 : : compliant prototype for this, so we just record the fact here and
4161 : : handle that during -fc-prototypes-external processing. */
4162 : :
4163 : 79330 : if (warn_external_argument_mismatch && csym && csym->attr.dummy
4164 : 14 : && csym->attr.external)
4165 : : {
4166 : 14 : if (csym->formal)
4167 : : {
4168 : 6 : bool conflict;
4169 : 6 : conflict = !gfc_compare_actual_formal (&c->ext.actual, csym->formal,
4170 : : 0, 0, 0, NULL);
4171 : 6 : if (conflict)
4172 : : {
4173 : 6 : csym->ext_dummy_arglist_mismatch = 1;
4174 : 6 : gfc_warning (OPT_Wexternal_argument_mismatch,
4175 : : "Different argument lists in external dummy "
4176 : : "subroutine %s at %L and %L", csym->name,
4177 : : &c->loc, &csym->formal_at);
4178 : : }
4179 : : }
4180 : 8 : else if (!csym->formal_resolved)
4181 : : {
4182 : 7 : gfc_get_formal_from_actual_arglist (csym, c->ext.actual);
4183 : 7 : csym->formal_at = c->loc;
4184 : : }
4185 : : }
4186 : :
4187 : 79330 : t = true;
4188 : 79330 : if (c->resolved_sym == NULL)
4189 : : {
4190 : 79279 : c->resolved_isym = NULL;
4191 : 79279 : switch (procedure_kind (csym))
4192 : : {
4193 : 2213 : case PTYPE_GENERIC:
4194 : 2213 : t = resolve_generic_s (c);
4195 : 2213 : break;
4196 : :
4197 : 61549 : case PTYPE_SPECIFIC:
4198 : 61549 : t = resolve_specific_s (c);
4199 : 61549 : break;
4200 : :
4201 : 15517 : case PTYPE_UNKNOWN:
4202 : 15517 : t = resolve_unknown_s (c);
4203 : 15517 : break;
4204 : :
4205 : : default:
4206 : : gfc_internal_error ("resolve_subroutine(): bad function type");
4207 : : }
4208 : : }
4209 : :
4210 : : /* Some checks of elemental subroutine actual arguments. */
4211 : 79329 : if (!resolve_elemental_actual (NULL, c))
4212 : : return false;
4213 : :
4214 : 79321 : if (!c->expr1)
4215 : 77660 : update_current_proc_array_outer_dependency (csym);
4216 : : else
4217 : : /* Typebound procedure: Assume the worst. */
4218 : 1661 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
4219 : :
4220 : 79321 : if (c->resolved_sym
4221 : 79008 : && c->resolved_sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
4222 : 2 : gfc_warning (OPT_Wdeprecated_declarations,
4223 : : "Using subroutine %qs at %L is deprecated",
4224 : : c->resolved_sym->name, &c->loc);
4225 : :
4226 : 79321 : csym = c->resolved_sym ? c->resolved_sym : csym;
4227 : 79321 : if (t && gfc_current_ns->import_state != IMPORT_NOT_SET && !c->resolved_isym
4228 : 2 : && csym != gfc_current_ns->proc_name)
4229 : 1 : return check_sym_import_status (csym, c->symtree, NULL, c, gfc_current_ns);
4230 : :
4231 : : return t;
4232 : : }
4233 : :
4234 : :
4235 : : /* Compare the shapes of two arrays that have non-NULL shapes. If both
4236 : : op1->shape and op2->shape are non-NULL return true if their shapes
4237 : : match. If both op1->shape and op2->shape are non-NULL return false
4238 : : if their shapes do not match. If either op1->shape or op2->shape is
4239 : : NULL, return true. */
4240 : :
4241 : : static bool
4242 : 31557 : compare_shapes (gfc_expr *op1, gfc_expr *op2)
4243 : : {
4244 : 31557 : bool t;
4245 : 31557 : int i;
4246 : :
4247 : 31557 : t = true;
4248 : :
4249 : 31557 : if (op1->shape != NULL && op2->shape != NULL)
4250 : : {
4251 : 42512 : for (i = 0; i < op1->rank; i++)
4252 : : {
4253 : 22680 : if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
4254 : : {
4255 : 3 : gfc_error ("Shapes for operands at %L and %L are not conformable",
4256 : : &op1->where, &op2->where);
4257 : 3 : t = false;
4258 : 3 : break;
4259 : : }
4260 : : }
4261 : : }
4262 : :
4263 : 31557 : return t;
4264 : : }
4265 : :
4266 : : /* Convert a logical operator to the corresponding bitwise intrinsic call.
4267 : : For example A .AND. B becomes IAND(A, B). */
4268 : : static gfc_expr *
4269 : 668 : logical_to_bitwise (gfc_expr *e)
4270 : : {
4271 : 668 : gfc_expr *tmp, *op1, *op2;
4272 : 668 : gfc_isym_id isym;
4273 : 668 : gfc_actual_arglist *args = NULL;
4274 : :
4275 : 668 : gcc_assert (e->expr_type == EXPR_OP);
4276 : :
4277 : 668 : isym = GFC_ISYM_NONE;
4278 : 668 : op1 = e->value.op.op1;
4279 : 668 : op2 = e->value.op.op2;
4280 : :
4281 : 668 : switch (e->value.op.op)
4282 : : {
4283 : : case INTRINSIC_NOT:
4284 : : isym = GFC_ISYM_NOT;
4285 : : break;
4286 : 126 : case INTRINSIC_AND:
4287 : 126 : isym = GFC_ISYM_IAND;
4288 : 126 : break;
4289 : 127 : case INTRINSIC_OR:
4290 : 127 : isym = GFC_ISYM_IOR;
4291 : 127 : break;
4292 : 270 : case INTRINSIC_NEQV:
4293 : 270 : isym = GFC_ISYM_IEOR;
4294 : 270 : break;
4295 : 126 : case INTRINSIC_EQV:
4296 : : /* "Bitwise eqv" is just the complement of NEQV === IEOR.
4297 : : Change the old expression to NEQV, which will get replaced by IEOR,
4298 : : and wrap it in NOT. */
4299 : 126 : tmp = gfc_copy_expr (e);
4300 : 126 : tmp->value.op.op = INTRINSIC_NEQV;
4301 : 126 : tmp = logical_to_bitwise (tmp);
4302 : 126 : isym = GFC_ISYM_NOT;
4303 : 126 : op1 = tmp;
4304 : 126 : op2 = NULL;
4305 : 126 : break;
4306 : 0 : default:
4307 : 0 : gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
4308 : : }
4309 : :
4310 : : /* Inherit the original operation's operands as arguments. */
4311 : 668 : args = gfc_get_actual_arglist ();
4312 : 668 : args->expr = op1;
4313 : 668 : if (op2)
4314 : : {
4315 : 523 : args->next = gfc_get_actual_arglist ();
4316 : 523 : args->next->expr = op2;
4317 : : }
4318 : :
4319 : : /* Convert the expression to a function call. */
4320 : 668 : e->expr_type = EXPR_FUNCTION;
4321 : 668 : e->value.function.actual = args;
4322 : 668 : e->value.function.isym = gfc_intrinsic_function_by_id (isym);
4323 : 668 : e->value.function.name = e->value.function.isym->name;
4324 : 668 : e->value.function.esym = NULL;
4325 : :
4326 : : /* Make up a pre-resolved function call symtree if we need to. */
4327 : 668 : if (!e->symtree || !e->symtree->n.sym)
4328 : : {
4329 : 668 : gfc_symbol *sym;
4330 : 668 : gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
4331 : 668 : sym = e->symtree->n.sym;
4332 : 668 : sym->result = sym;
4333 : 668 : sym->attr.flavor = FL_PROCEDURE;
4334 : 668 : sym->attr.function = 1;
4335 : 668 : sym->attr.elemental = 1;
4336 : 668 : sym->attr.pure = 1;
4337 : 668 : sym->attr.referenced = 1;
4338 : 668 : gfc_intrinsic_symbol (sym);
4339 : 668 : gfc_commit_symbol (sym);
4340 : : }
4341 : :
4342 : 668 : args->name = e->value.function.isym->formal->name;
4343 : 668 : if (e->value.function.isym->formal->next)
4344 : 523 : args->next->name = e->value.function.isym->formal->next->name;
4345 : :
4346 : 668 : return e;
4347 : : }
4348 : :
4349 : : /* Recursively append candidate UOP to CANDIDATES. Store the number of
4350 : : candidates in CANDIDATES_LEN. */
4351 : : static void
4352 : 69 : lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
4353 : : char **&candidates,
4354 : : size_t &candidates_len)
4355 : : {
4356 : 71 : gfc_symtree *p;
4357 : :
4358 : 71 : if (uop == NULL)
4359 : : return;
4360 : :
4361 : : /* Not sure how to properly filter here. Use all for a start.
4362 : : n.uop.op is NULL for empty interface operators (is that legal?) disregard
4363 : : these as i suppose they don't make terribly sense. */
4364 : :
4365 : 71 : if (uop->n.uop->op != NULL)
4366 : 2 : vec_push (candidates, candidates_len, uop->name);
4367 : :
4368 : 71 : p = uop->left;
4369 : 71 : if (p)
4370 : 0 : lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
4371 : :
4372 : 71 : p = uop->right;
4373 : 71 : if (p)
4374 : : lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
4375 : : }
4376 : :
4377 : : /* Lookup user-operator OP fuzzily, taking names in UOP into account. */
4378 : :
4379 : : static const char*
4380 : 69 : lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
4381 : : {
4382 : 69 : char **candidates = NULL;
4383 : 69 : size_t candidates_len = 0;
4384 : 69 : lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
4385 : 69 : return gfc_closest_fuzzy_match (op, candidates);
4386 : : }
4387 : :
4388 : :
4389 : : /* Callback finding an impure function as an operand to an .and. or
4390 : : .or. expression. Remember the last function warned about to
4391 : : avoid double warnings when recursing. */
4392 : :
4393 : : static int
4394 : 190849 : impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
4395 : : void *data)
4396 : : {
4397 : 190849 : gfc_expr *f = *e;
4398 : 190849 : const char *name;
4399 : 190849 : static gfc_expr *last = NULL;
4400 : 190849 : bool *found = (bool *) data;
4401 : :
4402 : 190849 : if (f->expr_type == EXPR_FUNCTION)
4403 : : {
4404 : 11702 : *found = 1;
4405 : 11702 : if (f != last && !gfc_pure_function (f, &name)
4406 : 12977 : && !gfc_implicit_pure_function (f))
4407 : : {
4408 : 1136 : if (name)
4409 : 1136 : gfc_warning (OPT_Wfunction_elimination,
4410 : : "Impure function %qs at %L might not be evaluated",
4411 : : name, &f->where);
4412 : : else
4413 : 0 : gfc_warning (OPT_Wfunction_elimination,
4414 : : "Impure function at %L might not be evaluated",
4415 : : &f->where);
4416 : : }
4417 : 11702 : last = f;
4418 : : }
4419 : :
4420 : 190849 : return 0;
4421 : : }
4422 : :
4423 : : /* Return true if TYPE is character based, false otherwise. */
4424 : :
4425 : : static int
4426 : 1373 : is_character_based (bt type)
4427 : : {
4428 : 1373 : return type == BT_CHARACTER || type == BT_HOLLERITH;
4429 : : }
4430 : :
4431 : :
4432 : : /* If expression is a hollerith, convert it to character and issue a warning
4433 : : for the conversion. */
4434 : :
4435 : : static void
4436 : 408 : convert_hollerith_to_character (gfc_expr *e)
4437 : : {
4438 : 408 : if (e->ts.type == BT_HOLLERITH)
4439 : : {
4440 : 108 : gfc_typespec t;
4441 : 108 : gfc_clear_ts (&t);
4442 : 108 : t.type = BT_CHARACTER;
4443 : 108 : t.kind = e->ts.kind;
4444 : 108 : gfc_convert_type_warn (e, &t, 2, 1);
4445 : : }
4446 : 408 : }
4447 : :
4448 : : /* Convert to numeric and issue a warning for the conversion. */
4449 : :
4450 : : static void
4451 : 240 : convert_to_numeric (gfc_expr *a, gfc_expr *b)
4452 : : {
4453 : 240 : gfc_typespec t;
4454 : 240 : gfc_clear_ts (&t);
4455 : 240 : t.type = b->ts.type;
4456 : 240 : t.kind = b->ts.kind;
4457 : 240 : gfc_convert_type_warn (a, &t, 2, 1);
4458 : 240 : }
4459 : :
4460 : : /* Resolve an operator expression node. This can involve replacing the
4461 : : operation with a user defined function call. CHECK_INTERFACES is a
4462 : : helper macro. */
4463 : :
4464 : : #define CHECK_INTERFACES \
4465 : : { \
4466 : : match m = gfc_extend_expr (e); \
4467 : : if (m == MATCH_YES) \
4468 : : return true; \
4469 : : if (m == MATCH_ERROR) \
4470 : : return false; \
4471 : : }
4472 : :
4473 : : static bool
4474 : 518804 : resolve_operator (gfc_expr *e)
4475 : : {
4476 : 518804 : gfc_expr *op1, *op2;
4477 : : /* One error uses 3 names; additional space for wording (also via gettext). */
4478 : 518804 : bool t = true;
4479 : :
4480 : : /* Reduce stacked parentheses to single pair */
4481 : 518804 : while (e->expr_type == EXPR_OP
4482 : 518914 : && e->value.op.op == INTRINSIC_PARENTHESES
4483 : 20528 : && e->value.op.op1->expr_type == EXPR_OP
4484 : 535168 : && e->value.op.op1->value.op.op == INTRINSIC_PARENTHESES)
4485 : : {
4486 : 110 : gfc_expr *tmp = gfc_copy_expr (e->value.op.op1);
4487 : 110 : gfc_replace_expr (e, tmp);
4488 : : }
4489 : :
4490 : : /* Resolve all subnodes-- give them types. */
4491 : :
4492 : 518804 : switch (e->value.op.op)
4493 : : {
4494 : 470189 : default:
4495 : 470189 : if (!gfc_resolve_expr (e->value.op.op2))
4496 : 518804 : t = false;
4497 : :
4498 : : /* Fall through. */
4499 : :
4500 : 518804 : case INTRINSIC_NOT:
4501 : 518804 : case INTRINSIC_UPLUS:
4502 : 518804 : case INTRINSIC_UMINUS:
4503 : 518804 : case INTRINSIC_PARENTHESES:
4504 : 518804 : if (!gfc_resolve_expr (e->value.op.op1))
4505 : : return false;
4506 : 518636 : if (e->value.op.op1
4507 : 518627 : && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
4508 : : {
4509 : 0 : gfc_error ("BOZ literal constant at %L cannot be an operand of "
4510 : 0 : "unary operator %qs", &e->value.op.op1->where,
4511 : : gfc_op2string (e->value.op.op));
4512 : 0 : return false;
4513 : : }
4514 : 518636 : if (flag_unsigned && pedantic && e->ts.type == BT_UNSIGNED
4515 : 6 : && e->value.op.op == INTRINSIC_UMINUS)
4516 : : {
4517 : 2 : gfc_error ("Negation of unsigned expression at %L not permitted ",
4518 : : &e->value.op.op1->where);
4519 : 2 : return false;
4520 : : }
4521 : 518634 : break;
4522 : : }
4523 : :
4524 : : /* Typecheck the new node. */
4525 : :
4526 : 518634 : op1 = e->value.op.op1;
4527 : 518634 : op2 = e->value.op.op2;
4528 : 518634 : if (op1 == NULL && op2 == NULL)
4529 : : return false;
4530 : : /* Error out if op2 did not resolve. We already diagnosed op1. */
4531 : 518625 : if (t == false)
4532 : : return false;
4533 : :
4534 : : /* op1 and op2 cannot both be BOZ. */
4535 : 518559 : if (op1 && op1->ts.type == BT_BOZ
4536 : 0 : && op2 && op2->ts.type == BT_BOZ)
4537 : : {
4538 : 0 : gfc_error ("Operands at %L and %L cannot appear as operands of "
4539 : 0 : "binary operator %qs", &op1->where, &op2->where,
4540 : : gfc_op2string (e->value.op.op));
4541 : 0 : return false;
4542 : : }
4543 : :
4544 : 518559 : if ((op1 && op1->expr_type == EXPR_NULL)
4545 : 518557 : || (op2 && op2->expr_type == EXPR_NULL))
4546 : : {
4547 : 3 : CHECK_INTERFACES
4548 : 3 : gfc_error ("Invalid context for NULL() pointer at %L", &e->where);
4549 : 3 : return false;
4550 : : }
4551 : :
4552 : 518556 : switch (e->value.op.op)
4553 : : {
4554 : 8015 : case INTRINSIC_UPLUS:
4555 : 8015 : case INTRINSIC_UMINUS:
4556 : 8015 : if (op1->ts.type == BT_INTEGER
4557 : : || op1->ts.type == BT_REAL
4558 : : || op1->ts.type == BT_COMPLEX
4559 : : || op1->ts.type == BT_UNSIGNED)
4560 : : {
4561 : 7946 : e->ts = op1->ts;
4562 : 7946 : break;
4563 : : }
4564 : :
4565 : 69 : CHECK_INTERFACES
4566 : 43 : gfc_error ("Operand of unary numeric operator %qs at %L is %s",
4567 : : gfc_op2string (e->value.op.op), &e->where, gfc_typename (e));
4568 : 43 : return false;
4569 : :
4570 : 151263 : case INTRINSIC_POWER:
4571 : 151263 : case INTRINSIC_PLUS:
4572 : 151263 : case INTRINSIC_MINUS:
4573 : 151263 : case INTRINSIC_TIMES:
4574 : 151263 : case INTRINSIC_DIVIDE:
4575 : :
4576 : : /* UNSIGNED cannot appear in a mixed expression without explicit
4577 : : conversion. */
4578 : 151263 : if (flag_unsigned && gfc_invalid_unsigned_ops (op1, op2))
4579 : : {
4580 : 3 : CHECK_INTERFACES
4581 : 3 : gfc_error ("Operands of binary numeric operator %qs at %L are "
4582 : : "%s/%s", gfc_op2string (e->value.op.op), &e->where,
4583 : : gfc_typename (op1), gfc_typename (op2));
4584 : 3 : return false;
4585 : : }
4586 : :
4587 : 151260 : if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4588 : : {
4589 : : /* Do not perform conversions if operands are not conformable as
4590 : : required for the binary intrinsic operators (F2018:10.1.5).
4591 : : Defer to a possibly overloading user-defined operator. */
4592 : 150804 : if (!gfc_op_rank_conformable (op1, op2))
4593 : : {
4594 : 36 : CHECK_INTERFACES
4595 : 0 : gfc_error ("Inconsistent ranks for operator at %L and %L",
4596 : 0 : &op1->where, &op2->where);
4597 : 0 : return false;
4598 : : }
4599 : :
4600 : 150768 : gfc_type_convert_binary (e, 1);
4601 : 150768 : break;
4602 : : }
4603 : :
4604 : 456 : if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
4605 : : {
4606 : 227 : CHECK_INTERFACES
4607 : 2 : gfc_error ("Unexpected derived-type entities in binary intrinsic "
4608 : : "numeric operator %qs at %L",
4609 : : gfc_op2string (e->value.op.op), &e->where);
4610 : 2 : return false;
4611 : : }
4612 : : else
4613 : : {
4614 : 229 : CHECK_INTERFACES
4615 : 3 : gfc_error ("Operands of binary numeric operator %qs at %L are %s/%s",
4616 : : gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4617 : : gfc_typename (op2));
4618 : 3 : return false;
4619 : : }
4620 : :
4621 : 2142 : case INTRINSIC_CONCAT:
4622 : 2142 : if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4623 : 2117 : && op1->ts.kind == op2->ts.kind)
4624 : : {
4625 : 2108 : e->ts.type = BT_CHARACTER;
4626 : 2108 : e->ts.kind = op1->ts.kind;
4627 : 2108 : break;
4628 : : }
4629 : :
4630 : 34 : CHECK_INTERFACES
4631 : 10 : gfc_error ("Operands of string concatenation operator at %L are %s/%s",
4632 : : &e->where, gfc_typename (op1), gfc_typename (op2));
4633 : 10 : return false;
4634 : :
4635 : 68998 : case INTRINSIC_AND:
4636 : 68998 : case INTRINSIC_OR:
4637 : 68998 : case INTRINSIC_EQV:
4638 : 68998 : case INTRINSIC_NEQV:
4639 : 68998 : if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4640 : : {
4641 : 68447 : e->ts.type = BT_LOGICAL;
4642 : 68447 : e->ts.kind = gfc_kind_max (op1, op2);
4643 : 68447 : if (op1->ts.kind < e->ts.kind)
4644 : 138 : gfc_convert_type (op1, &e->ts, 2);
4645 : 68309 : else if (op2->ts.kind < e->ts.kind)
4646 : 117 : gfc_convert_type (op2, &e->ts, 2);
4647 : :
4648 : 68447 : if (flag_frontend_optimize &&
4649 : 57436 : (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
4650 : : {
4651 : : /* Warn about short-circuiting
4652 : : with impure function as second operand. */
4653 : 51465 : bool op2_f = false;
4654 : 51465 : gfc_expr_walker (&op2, impure_function_callback, &op2_f);
4655 : : }
4656 : : break;
4657 : : }
4658 : :
4659 : : /* Logical ops on integers become bitwise ops with -fdec. */
4660 : 551 : else if (flag_dec
4661 : 523 : && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
4662 : : {
4663 : 523 : e->ts.type = BT_INTEGER;
4664 : 523 : e->ts.kind = gfc_kind_max (op1, op2);
4665 : 523 : if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
4666 : 289 : gfc_convert_type (op1, &e->ts, 1);
4667 : 523 : if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
4668 : 144 : gfc_convert_type (op2, &e->ts, 1);
4669 : 523 : e = logical_to_bitwise (e);
4670 : 523 : goto simplify_op;
4671 : : }
4672 : :
4673 : 28 : CHECK_INTERFACES
4674 : 16 : gfc_error ("Operands of logical operator %qs at %L are %s/%s",
4675 : : gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4676 : : gfc_typename (op2));
4677 : 16 : return false;
4678 : :
4679 : 20166 : case INTRINSIC_NOT:
4680 : : /* Logical ops on integers become bitwise ops with -fdec. */
4681 : 20166 : if (flag_dec && op1->ts.type == BT_INTEGER)
4682 : : {
4683 : 19 : e->ts.type = BT_INTEGER;
4684 : 19 : e->ts.kind = op1->ts.kind;
4685 : 19 : e = logical_to_bitwise (e);
4686 : 19 : goto simplify_op;
4687 : : }
4688 : :
4689 : 20147 : if (op1->ts.type == BT_LOGICAL)
4690 : : {
4691 : 20141 : e->ts.type = BT_LOGICAL;
4692 : 20141 : e->ts.kind = op1->ts.kind;
4693 : 20141 : break;
4694 : : }
4695 : :
4696 : 6 : CHECK_INTERFACES
4697 : 3 : gfc_error ("Operand of .not. operator at %L is %s", &e->where,
4698 : : gfc_typename (op1));
4699 : 3 : return false;
4700 : :
4701 : 21053 : case INTRINSIC_GT:
4702 : 21053 : case INTRINSIC_GT_OS:
4703 : 21053 : case INTRINSIC_GE:
4704 : 21053 : case INTRINSIC_GE_OS:
4705 : 21053 : case INTRINSIC_LT:
4706 : 21053 : case INTRINSIC_LT_OS:
4707 : 21053 : case INTRINSIC_LE:
4708 : 21053 : case INTRINSIC_LE_OS:
4709 : 21053 : if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
4710 : : {
4711 : 18 : CHECK_INTERFACES
4712 : 0 : gfc_error ("COMPLEX quantities cannot be compared at %L", &e->where);
4713 : 0 : return false;
4714 : : }
4715 : :
4716 : : /* Fall through. */
4717 : :
4718 : 247288 : case INTRINSIC_EQ:
4719 : 247288 : case INTRINSIC_EQ_OS:
4720 : 247288 : case INTRINSIC_NE:
4721 : 247288 : case INTRINSIC_NE_OS:
4722 : :
4723 : 247288 : if (flag_dec
4724 : 1038 : && is_character_based (op1->ts.type)
4725 : 247623 : && is_character_based (op2->ts.type))
4726 : : {
4727 : 204 : convert_hollerith_to_character (op1);
4728 : 204 : convert_hollerith_to_character (op2);
4729 : : }
4730 : :
4731 : 247288 : if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4732 : 36458 : && op1->ts.kind == op2->ts.kind)
4733 : : {
4734 : 36421 : e->ts.type = BT_LOGICAL;
4735 : 36421 : e->ts.kind = gfc_default_logical_kind;
4736 : 36421 : break;
4737 : : }
4738 : :
4739 : : /* If op1 is BOZ, then op2 is not!. Try to convert to type of op2. */
4740 : 210867 : if (op1->ts.type == BT_BOZ)
4741 : : {
4742 : 0 : if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
4743 : : "as an operand of a relational operator"),
4744 : : &op1->where))
4745 : : return false;
4746 : :
4747 : 0 : if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
4748 : : return false;
4749 : :
4750 : 0 : if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
4751 : : return false;
4752 : : }
4753 : :
4754 : : /* If op2 is BOZ, then op1 is not!. Try to convert to type of op2. */
4755 : 210867 : if (op2->ts.type == BT_BOZ)
4756 : : {
4757 : 0 : if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
4758 : : " as an operand of a relational operator"),
4759 : : &op2->where))
4760 : : return false;
4761 : :
4762 : 0 : if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
4763 : : return false;
4764 : :
4765 : 0 : if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
4766 : : return false;
4767 : : }
4768 : 210867 : if (flag_dec
4769 : 210867 : && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
4770 : 120 : convert_to_numeric (op1, op2);
4771 : :
4772 : 210867 : if (flag_dec
4773 : 210867 : && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
4774 : 120 : convert_to_numeric (op2, op1);
4775 : :
4776 : 210867 : if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4777 : : {
4778 : : /* Do not perform conversions if operands are not conformable as
4779 : : required for the binary intrinsic operators (F2018:10.1.5).
4780 : : Defer to a possibly overloading user-defined operator. */
4781 : 209742 : if (!gfc_op_rank_conformable (op1, op2))
4782 : : {
4783 : 70 : CHECK_INTERFACES
4784 : 0 : gfc_error ("Inconsistent ranks for operator at %L and %L",
4785 : 0 : &op1->where, &op2->where);
4786 : 0 : return false;
4787 : : }
4788 : :
4789 : 209672 : if (flag_unsigned && gfc_invalid_unsigned_ops (op1, op2))
4790 : : {
4791 : 1 : CHECK_INTERFACES
4792 : 1 : gfc_error ("Inconsistent types for operator at %L and %L: "
4793 : 1 : "%s and %s", &op1->where, &op2->where,
4794 : : gfc_typename (op1), gfc_typename (op2));
4795 : 1 : return false;
4796 : : }
4797 : :
4798 : 209671 : gfc_type_convert_binary (e, 1);
4799 : :
4800 : 209671 : e->ts.type = BT_LOGICAL;
4801 : 209671 : e->ts.kind = gfc_default_logical_kind;
4802 : :
4803 : 209671 : if (warn_compare_reals)
4804 : : {
4805 : 69 : gfc_intrinsic_op op = e->value.op.op;
4806 : :
4807 : : /* Type conversion has made sure that the types of op1 and op2
4808 : : agree, so it is only necessary to check the first one. */
4809 : 69 : if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
4810 : 13 : && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
4811 : 6 : || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
4812 : : {
4813 : 13 : const char *msg;
4814 : :
4815 : 13 : if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
4816 : : msg = G_("Equality comparison for %s at %L");
4817 : : else
4818 : 6 : msg = G_("Inequality comparison for %s at %L");
4819 : :
4820 : 13 : gfc_warning (OPT_Wcompare_reals, msg,
4821 : : gfc_typename (op1), &op1->where);
4822 : : }
4823 : : }
4824 : :
4825 : : break;
4826 : : }
4827 : :
4828 : 1125 : if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4829 : : {
4830 : 2 : CHECK_INTERFACES
4831 : 4 : gfc_error ("Logicals at %L must be compared with %s instead of %s",
4832 : : &e->where,
4833 : 2 : (e->value.op.op == INTRINSIC_EQ || e->value.op.op == INTRINSIC_EQ_OS)
4834 : : ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
4835 : 2 : }
4836 : : else
4837 : : {
4838 : 1123 : CHECK_INTERFACES
4839 : 113 : gfc_error ("Operands of comparison operator %qs at %L are %s/%s",
4840 : : gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4841 : : gfc_typename (op2));
4842 : : }
4843 : :
4844 : : return false;
4845 : :
4846 : 294 : case INTRINSIC_USER:
4847 : 294 : if (e->value.op.uop->op == NULL)
4848 : : {
4849 : 69 : const char *name = e->value.op.uop->name;
4850 : 69 : const char *guessed;
4851 : 69 : guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
4852 : 69 : CHECK_INTERFACES
4853 : 11 : if (guessed)
4854 : 1 : gfc_error ("Unknown operator %qs at %L; did you mean "
4855 : : "%qs?", name, &e->where, guessed);
4856 : : else
4857 : 10 : gfc_error ("Unknown operator %qs at %L", name, &e->where);
4858 : : }
4859 : 225 : else if (op2 == NULL)
4860 : : {
4861 : 48 : CHECK_INTERFACES
4862 : 0 : gfc_error ("Operand of user operator %qs at %L is %s",
4863 : 0 : e->value.op.uop->name, &e->where, gfc_typename (op1));
4864 : : }
4865 : : else
4866 : : {
4867 : 177 : e->value.op.uop->op->sym->attr.referenced = 1;
4868 : 177 : CHECK_INTERFACES
4869 : 5 : gfc_error ("Operands of user operator %qs at %L are %s/%s",
4870 : 5 : e->value.op.uop->name, &e->where, gfc_typename (op1),
4871 : : gfc_typename (op2));
4872 : : }
4873 : :
4874 : : return false;
4875 : :
4876 : 20372 : case INTRINSIC_PARENTHESES:
4877 : 20372 : e->ts = op1->ts;
4878 : 20372 : if (e->ts.type == BT_CHARACTER)
4879 : 301 : e->ts.u.cl = op1->ts.u.cl;
4880 : : break;
4881 : :
4882 : 0 : default:
4883 : 0 : gfc_internal_error ("resolve_operator(): Bad intrinsic");
4884 : : }
4885 : :
4886 : : /* Deal with arrayness of an operand through an operator. */
4887 : :
4888 : 515874 : switch (e->value.op.op)
4889 : : {
4890 : 467415 : case INTRINSIC_PLUS:
4891 : 467415 : case INTRINSIC_MINUS:
4892 : 467415 : case INTRINSIC_TIMES:
4893 : 467415 : case INTRINSIC_DIVIDE:
4894 : 467415 : case INTRINSIC_POWER:
4895 : 467415 : case INTRINSIC_CONCAT:
4896 : 467415 : case INTRINSIC_AND:
4897 : 467415 : case INTRINSIC_OR:
4898 : 467415 : case INTRINSIC_EQV:
4899 : 467415 : case INTRINSIC_NEQV:
4900 : 467415 : case INTRINSIC_EQ:
4901 : 467415 : case INTRINSIC_EQ_OS:
4902 : 467415 : case INTRINSIC_NE:
4903 : 467415 : case INTRINSIC_NE_OS:
4904 : 467415 : case INTRINSIC_GT:
4905 : 467415 : case INTRINSIC_GT_OS:
4906 : 467415 : case INTRINSIC_GE:
4907 : 467415 : case INTRINSIC_GE_OS:
4908 : 467415 : case INTRINSIC_LT:
4909 : 467415 : case INTRINSIC_LT_OS:
4910 : 467415 : case INTRINSIC_LE:
4911 : 467415 : case INTRINSIC_LE_OS:
4912 : :
4913 : 467415 : if (op1->rank == 0 && op2->rank == 0)
4914 : 416682 : e->rank = 0;
4915 : :
4916 : 467415 : if (op1->rank == 0 && op2->rank != 0)
4917 : : {
4918 : 2611 : e->rank = op2->rank;
4919 : :
4920 : 2611 : if (e->shape == NULL)
4921 : 2581 : e->shape = gfc_copy_shape (op2->shape, op2->rank);
4922 : : }
4923 : :
4924 : 467415 : if (op1->rank != 0 && op2->rank == 0)
4925 : : {
4926 : 16504 : e->rank = op1->rank;
4927 : :
4928 : 16504 : if (e->shape == NULL)
4929 : 16486 : e->shape = gfc_copy_shape (op1->shape, op1->rank);
4930 : : }
4931 : :
4932 : 467415 : if (op1->rank != 0 && op2->rank != 0)
4933 : : {
4934 : 31618 : if (op1->rank == op2->rank)
4935 : : {
4936 : 31618 : e->rank = op1->rank;
4937 : 31618 : if (e->shape == NULL)
4938 : : {
4939 : 31557 : t = compare_shapes (op1, op2);
4940 : 31557 : if (!t)
4941 : 3 : e->shape = NULL;
4942 : : else
4943 : 31554 : e->shape = gfc_copy_shape (op1->shape, op1->rank);
4944 : : }
4945 : : }
4946 : : else
4947 : : {
4948 : : /* Allow higher level expressions to work. */
4949 : 0 : e->rank = 0;
4950 : :
4951 : : /* Try user-defined operators, and otherwise throw an error. */
4952 : 0 : CHECK_INTERFACES
4953 : 0 : gfc_error ("Inconsistent ranks for operator at %L and %L",
4954 : 0 : &op1->where, &op2->where);
4955 : 0 : return false;
4956 : : }
4957 : : }
4958 : : break;
4959 : :
4960 : 48459 : case INTRINSIC_PARENTHESES:
4961 : 48459 : case INTRINSIC_NOT:
4962 : 48459 : case INTRINSIC_UPLUS:
4963 : 48459 : case INTRINSIC_UMINUS:
4964 : : /* Simply copy arrayness attribute */
4965 : 48459 : e->rank = op1->rank;
4966 : 48459 : e->corank = op1->corank;
4967 : :
4968 : 48459 : if (e->shape == NULL)
4969 : 48453 : e->shape = gfc_copy_shape (op1->shape, op1->rank);
4970 : :
4971 : : break;
4972 : :
4973 : : default:
4974 : : break;
4975 : : }
4976 : :
4977 : 516416 : simplify_op:
4978 : :
4979 : : /* Attempt to simplify the expression. */
4980 : 3 : if (t)
4981 : : {
4982 : 516413 : t = gfc_simplify_expr (e, 0);
4983 : : /* Some calls do not succeed in simplification and return false
4984 : : even though there is no error; e.g. variable references to
4985 : : PARAMETER arrays. */
4986 : 516413 : if (!gfc_is_constant_expr (e))
4987 : 473589 : t = true;
4988 : : }
4989 : : return t;
4990 : : }
4991 : :
4992 : : static bool
4993 : 90 : resolve_conditional (gfc_expr *expr)
4994 : : {
4995 : 90 : gfc_expr *condition, *true_expr, *false_expr;
4996 : :
4997 : 90 : condition = expr->value.conditional.condition;
4998 : 90 : true_expr = expr->value.conditional.true_expr;
4999 : 90 : false_expr = expr->value.conditional.false_expr;
5000 : :
5001 : 180 : if (!gfc_resolve_expr (condition) || !gfc_resolve_expr (true_expr)
5002 : 180 : || !gfc_resolve_expr (false_expr))
5003 : 0 : return false;
5004 : :
5005 : 90 : if (condition->ts.type != BT_LOGICAL || condition->rank != 0)
5006 : : {
5007 : 2 : gfc_error (
5008 : : "Condition in conditional expression must be a scalar logical at %L",
5009 : : &condition->where);
5010 : 2 : return false;
5011 : : }
5012 : :
5013 : 88 : if (true_expr->ts.type != false_expr->ts.type)
5014 : : {
5015 : 1 : gfc_error ("expr at %L and expr at %L in conditional expression "
5016 : : "must have the same declared type",
5017 : : &true_expr->where, &false_expr->where);
5018 : 1 : return false;
5019 : : }
5020 : :
5021 : 87 : if (true_expr->ts.kind != false_expr->ts.kind)
5022 : : {
5023 : 1 : gfc_error ("expr at %L and expr at %L in conditional expression "
5024 : : "must have the same kind parameter",
5025 : : &true_expr->where, &false_expr->where);
5026 : 1 : return false;
5027 : : }
5028 : :
5029 : 86 : if (true_expr->rank != false_expr->rank)
5030 : : {
5031 : 1 : gfc_error ("expr at %L and expr at %L in conditional expression "
5032 : : "must have the same rank",
5033 : : &true_expr->where, &false_expr->where);
5034 : 1 : return false;
5035 : : }
5036 : :
5037 : : /* TODO: support more data types for conditional expressions */
5038 : 85 : if (true_expr->ts.type != BT_INTEGER && true_expr->ts.type != BT_LOGICAL
5039 : 85 : && true_expr->ts.type != BT_REAL && true_expr->ts.type != BT_COMPLEX)
5040 : : {
5041 : 1 : gfc_error ("Sorry, only integer, logical, real and complex types "
5042 : : "are currently supported for conditional expressions at %L",
5043 : : &expr->where);
5044 : 1 : return false;
5045 : : }
5046 : :
5047 : 84 : if (true_expr->rank > 0)
5048 : : {
5049 : 1 : gfc_error ("Sorry, array is currently unsupported for conditional "
5050 : : "expressions at %L",
5051 : : &expr->where);
5052 : 1 : return false;
5053 : : }
5054 : :
5055 : 83 : expr->ts = true_expr->ts;
5056 : 83 : expr->rank = true_expr->rank;
5057 : 83 : return true;
5058 : : }
5059 : :
5060 : : /************** Array resolution subroutines **************/
5061 : :
5062 : : enum compare_result
5063 : : { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
5064 : :
5065 : : /* Compare two integer expressions. */
5066 : :
5067 : : static compare_result
5068 : 447395 : compare_bound (gfc_expr *a, gfc_expr *b)
5069 : : {
5070 : 447395 : int i;
5071 : :
5072 : 447395 : if (a == NULL || a->expr_type != EXPR_CONSTANT
5073 : 292586 : || b == NULL || b->expr_type != EXPR_CONSTANT)
5074 : : return CMP_UNKNOWN;
5075 : :
5076 : : /* If either of the types isn't INTEGER, we must have
5077 : : raised an error earlier. */
5078 : :
5079 : 204642 : if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
5080 : : return CMP_UNKNOWN;
5081 : :
5082 : 204638 : i = mpz_cmp (a->value.integer, b->value.integer);
5083 : :
5084 : 204638 : if (i < 0)
5085 : : return CMP_LT;
5086 : 96198 : if (i > 0)
5087 : 38025 : return CMP_GT;
5088 : : return CMP_EQ;
5089 : : }
5090 : :
5091 : :
5092 : : /* Compare an integer expression with an integer. */
5093 : :
5094 : : static compare_result
5095 : 71757 : compare_bound_int (gfc_expr *a, int b)
5096 : : {
5097 : 71757 : int i;
5098 : :
5099 : 71757 : if (a == NULL
5100 : 30300 : || a->expr_type != EXPR_CONSTANT
5101 : 27507 : || a->ts.type != BT_INTEGER)
5102 : : return CMP_UNKNOWN;
5103 : :
5104 : 27507 : i = mpz_cmp_si (a->value.integer, b);
5105 : :
5106 : 27507 : if (i < 0)
5107 : : return CMP_LT;
5108 : 24224 : if (i > 0)
5109 : 21155 : return CMP_GT;
5110 : : return CMP_EQ;
5111 : : }
5112 : :
5113 : :
5114 : : /* Compare an integer expression with a mpz_t. */
5115 : :
5116 : : static compare_result
5117 : 66765 : compare_bound_mpz_t (gfc_expr *a, mpz_t b)
5118 : : {
5119 : 66765 : int i;
5120 : :
5121 : 66765 : if (a == NULL
5122 : 54833 : || a->expr_type != EXPR_CONSTANT
5123 : 52749 : || a->ts.type != BT_INTEGER)
5124 : : return CMP_UNKNOWN;
5125 : :
5126 : 52746 : i = mpz_cmp (a->value.integer, b);
5127 : :
5128 : 52746 : if (i < 0)
5129 : : return CMP_LT;
5130 : 23966 : if (i > 0)
5131 : 10270 : return CMP_GT;
5132 : : return CMP_EQ;
5133 : : }
5134 : :
5135 : :
5136 : : /* Compute the last value of a sequence given by a triplet.
5137 : : Return 0 if it wasn't able to compute the last value, or if the
5138 : : sequence if empty, and 1 otherwise. */
5139 : :
5140 : : static int
5141 : 50186 : compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
5142 : : gfc_expr *stride, mpz_t last)
5143 : : {
5144 : 50186 : mpz_t rem;
5145 : :
5146 : 50186 : if (start == NULL || start->expr_type != EXPR_CONSTANT
5147 : 35451 : || end == NULL || end->expr_type != EXPR_CONSTANT
5148 : 31044 : || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
5149 : : return 0;
5150 : :
5151 : 30725 : if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
5152 : 30724 : || (stride != NULL && stride->ts.type != BT_INTEGER))
5153 : : return 0;
5154 : :
5155 : 6212 : if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
5156 : : {
5157 : 24638 : if (compare_bound (start, end) == CMP_GT)
5158 : : return 0;
5159 : 23249 : mpz_set (last, end->value.integer);
5160 : 23249 : return 1;
5161 : : }
5162 : :
5163 : 6086 : if (compare_bound_int (stride, 0) == CMP_GT)
5164 : : {
5165 : : /* Stride is positive */
5166 : 5118 : if (mpz_cmp (start->value.integer, end->value.integer) > 0)
5167 : : return 0;
5168 : : }
5169 : : else
5170 : : {
5171 : : /* Stride is negative */
5172 : 968 : if (mpz_cmp (start->value.integer, end->value.integer) < 0)
5173 : : return 0;
5174 : : }
5175 : :
5176 : 6066 : mpz_init (rem);
5177 : 6066 : mpz_sub (rem, end->value.integer, start->value.integer);
5178 : 6066 : mpz_tdiv_r (rem, rem, stride->value.integer);
5179 : 6066 : mpz_sub (last, end->value.integer, rem);
5180 : 6066 : mpz_clear (rem);
5181 : :
5182 : 6066 : return 1;
5183 : : }
5184 : :
5185 : :
5186 : : /* Compare a single dimension of an array reference to the array
5187 : : specification. */
5188 : :
5189 : : static bool
5190 : 205803 : check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
5191 : : {
5192 : 205803 : mpz_t last_value;
5193 : :
5194 : 205803 : if (ar->dimen_type[i] == DIMEN_STAR)
5195 : : {
5196 : 413 : gcc_assert (ar->stride[i] == NULL);
5197 : : /* This implies [*] as [*:] and [*:3] are not possible. */
5198 : 413 : if (ar->start[i] == NULL)
5199 : : {
5200 : 339 : gcc_assert (ar->end[i] == NULL);
5201 : : return true;
5202 : : }
5203 : : }
5204 : :
5205 : : /* Given start, end and stride values, calculate the minimum and
5206 : : maximum referenced indexes. */
5207 : :
5208 : 205464 : switch (ar->dimen_type[i])
5209 : : {
5210 : : case DIMEN_VECTOR:
5211 : : case DIMEN_THIS_IMAGE:
5212 : : break;
5213 : :
5214 : 148914 : case DIMEN_STAR:
5215 : 148914 : case DIMEN_ELEMENT:
5216 : 148914 : if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
5217 : : {
5218 : 2 : if (i < as->rank)
5219 : 2 : gfc_warning (0, "Array reference at %L is out of bounds "
5220 : : "(%ld < %ld) in dimension %d", &ar->c_where[i],
5221 : 2 : mpz_get_si (ar->start[i]->value.integer),
5222 : 2 : mpz_get_si (as->lower[i]->value.integer), i+1);
5223 : : else
5224 : 0 : gfc_warning (0, "Array reference at %L is out of bounds "
5225 : : "(%ld < %ld) in codimension %d", &ar->c_where[i],
5226 : 0 : mpz_get_si (ar->start[i]->value.integer),
5227 : 0 : mpz_get_si (as->lower[i]->value.integer),
5228 : 0 : i + 1 - as->rank);
5229 : 2 : return true;
5230 : : }
5231 : 148912 : if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
5232 : : {
5233 : 39 : if (i < as->rank)
5234 : 39 : gfc_warning (0, "Array reference at %L is out of bounds "
5235 : : "(%ld > %ld) in dimension %d", &ar->c_where[i],
5236 : 39 : mpz_get_si (ar->start[i]->value.integer),
5237 : 39 : mpz_get_si (as->upper[i]->value.integer), i+1);
5238 : : else
5239 : 0 : gfc_warning (0, "Array reference at %L is out of bounds "
5240 : : "(%ld > %ld) in codimension %d", &ar->c_where[i],
5241 : 0 : mpz_get_si (ar->start[i]->value.integer),
5242 : 0 : mpz_get_si (as->upper[i]->value.integer),
5243 : 0 : i + 1 - as->rank);
5244 : 39 : return true;
5245 : : }
5246 : :
5247 : : break;
5248 : :
5249 : 50231 : case DIMEN_RANGE:
5250 : 50231 : {
5251 : : #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
5252 : : #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
5253 : :
5254 : 50231 : compare_result comp_start_end = compare_bound (AR_START, AR_END);
5255 : 50231 : compare_result comp_stride_zero = compare_bound_int (ar->stride[i], 0);
5256 : :
5257 : : /* Check for zero stride, which is not allowed. */
5258 : 50231 : if (comp_stride_zero == CMP_EQ)
5259 : : {
5260 : 1 : gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
5261 : 1 : return false;
5262 : : }
5263 : :
5264 : : /* if start == end || (stride > 0 && start < end)
5265 : : || (stride < 0 && start > end),
5266 : : then the array section contains at least one element. In this
5267 : : case, there is an out-of-bounds access if
5268 : : (start < lower || start > upper). */
5269 : 50230 : if (comp_start_end == CMP_EQ
5270 : 49545 : || ((comp_stride_zero == CMP_GT || ar->stride[i] == NULL)
5271 : 47270 : && comp_start_end == CMP_LT)
5272 : 21817 : || (comp_stride_zero == CMP_LT
5273 : 21817 : && comp_start_end == CMP_GT))
5274 : : {
5275 : 29361 : if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
5276 : : {
5277 : 27 : gfc_warning (0, "Lower array reference at %L is out of bounds "
5278 : : "(%ld < %ld) in dimension %d", &ar->c_where[i],
5279 : 27 : mpz_get_si (AR_START->value.integer),
5280 : 27 : mpz_get_si (as->lower[i]->value.integer), i+1);
5281 : 27 : return true;
5282 : : }
5283 : 29334 : if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
5284 : : {
5285 : 17 : gfc_warning (0, "Lower array reference at %L is out of bounds "
5286 : : "(%ld > %ld) in dimension %d", &ar->c_where[i],
5287 : 17 : mpz_get_si (AR_START->value.integer),
5288 : 17 : mpz_get_si (as->upper[i]->value.integer), i+1);
5289 : 17 : return true;
5290 : : }
5291 : : }
5292 : :
5293 : : /* If we can compute the highest index of the array section,
5294 : : then it also has to be between lower and upper. */
5295 : 50186 : mpz_init (last_value);
5296 : 50186 : if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
5297 : : last_value))
5298 : : {
5299 : 29315 : if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
5300 : : {
5301 : 3 : gfc_warning (0, "Upper array reference at %L is out of bounds "
5302 : : "(%ld < %ld) in dimension %d", &ar->c_where[i],
5303 : : mpz_get_si (last_value),
5304 : 3 : mpz_get_si (as->lower[i]->value.integer), i+1);
5305 : 3 : mpz_clear (last_value);
5306 : 3 : return true;
5307 : : }
5308 : 29312 : if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
5309 : : {
5310 : 7 : gfc_warning (0, "Upper array reference at %L is out of bounds "
5311 : : "(%ld > %ld) in dimension %d", &ar->c_where[i],
5312 : : mpz_get_si (last_value),
5313 : 7 : mpz_get_si (as->upper[i]->value.integer), i+1);
5314 : 7 : mpz_clear (last_value);
5315 : 7 : return true;
5316 : : }
5317 : : }
5318 : 50176 : mpz_clear (last_value);
5319 : :
5320 : : #undef AR_START
5321 : : #undef AR_END
5322 : : }
5323 : 50176 : break;
5324 : :
5325 : 0 : default:
5326 : 0 : gfc_internal_error ("check_dimension(): Bad array reference");
5327 : : }
5328 : :
5329 : : return true;
5330 : : }
5331 : :
5332 : :
5333 : : /* Compare an array reference with an array specification. */
5334 : :
5335 : : static bool
5336 : 410379 : compare_spec_to_ref (gfc_array_ref *ar)
5337 : : {
5338 : 410379 : gfc_array_spec *as;
5339 : 410379 : int i;
5340 : :
5341 : 410379 : as = ar->as;
5342 : 410379 : i = as->rank - 1;
5343 : : /* TODO: Full array sections are only allowed as actual parameters. */
5344 : 410379 : if (as->type == AS_ASSUMED_SIZE
5345 : 5763 : && (/*ar->type == AR_FULL
5346 : 5763 : ||*/ (ar->type == AR_SECTION
5347 : 514 : && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
5348 : : {
5349 : 5 : gfc_error ("Rightmost upper bound of assumed size array section "
5350 : : "not specified at %L", &ar->where);
5351 : 5 : return false;
5352 : : }
5353 : :
5354 : 410374 : if (ar->type == AR_FULL)
5355 : : return true;
5356 : :
5357 : 155581 : if (as->rank != ar->dimen)
5358 : : {
5359 : 28 : gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
5360 : : &ar->where, ar->dimen, as->rank);
5361 : 28 : return false;
5362 : : }
5363 : :
5364 : : /* ar->codimen == 0 is a local array. */
5365 : 155553 : if (as->corank != ar->codimen && ar->codimen != 0)
5366 : : {
5367 : 0 : gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
5368 : : &ar->where, ar->codimen, as->corank);
5369 : 0 : return false;
5370 : : }
5371 : :
5372 : 353651 : for (i = 0; i < as->rank; i++)
5373 : 198099 : if (!check_dimension (i, ar, as))
5374 : : return false;
5375 : :
5376 : : /* Local access has no coarray spec. */
5377 : 155552 : if (ar->codimen != 0)
5378 : 14745 : for (i = as->rank; i < as->rank + as->corank; i++)
5379 : : {
5380 : 7706 : if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
5381 : 5268 : && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
5382 : : {
5383 : 2 : gfc_error ("Coindex of codimension %d must be a scalar at %L",
5384 : 2 : i + 1 - as->rank, &ar->where);
5385 : 2 : return false;
5386 : : }
5387 : 7704 : if (!check_dimension (i, ar, as))
5388 : : return false;
5389 : : }
5390 : :
5391 : : return true;
5392 : : }
5393 : :
5394 : :
5395 : : /* Resolve one part of an array index. */
5396 : :
5397 : : static bool
5398 : 699463 : gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
5399 : : int force_index_integer_kind)
5400 : : {
5401 : 699463 : gfc_typespec ts;
5402 : :
5403 : 699463 : if (index == NULL)
5404 : : return true;
5405 : :
5406 : 207521 : if (!gfc_resolve_expr (index))
5407 : : return false;
5408 : :
5409 : 207498 : if (check_scalar && index->rank != 0)
5410 : : {
5411 : 2 : gfc_error ("Array index at %L must be scalar", &index->where);
5412 : 2 : return false;
5413 : : }
5414 : :
5415 : 207496 : if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
5416 : : {
5417 : 3 : gfc_error ("Array index at %L must be of INTEGER type, found %s",
5418 : : &index->where, gfc_basic_typename (index->ts.type));
5419 : 3 : return false;
5420 : : }
5421 : :
5422 : 207493 : if (index->ts.type == BT_REAL)
5423 : 336 : if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
5424 : : &index->where))
5425 : : return false;
5426 : :
5427 : 207493 : if ((index->ts.kind != gfc_index_integer_kind
5428 : 202809 : && force_index_integer_kind)
5429 : 177885 : || (index->ts.type != BT_INTEGER
5430 : : && index->ts.type != BT_UNKNOWN))
5431 : : {
5432 : 29943 : gfc_clear_ts (&ts);
5433 : 29943 : ts.type = BT_INTEGER;
5434 : 29943 : ts.kind = gfc_index_integer_kind;
5435 : :
5436 : 29943 : gfc_convert_type_warn (index, &ts, 2, 0);
5437 : : }
5438 : :
5439 : : return true;
5440 : : }
5441 : :
5442 : : /* Resolve one part of an array index. */
5443 : :
5444 : : bool
5445 : 466527 : gfc_resolve_index (gfc_expr *index, int check_scalar)
5446 : : {
5447 : 466527 : return gfc_resolve_index_1 (index, check_scalar, 1);
5448 : : }
5449 : :
5450 : : /* Resolve a dim argument to an intrinsic function. */
5451 : :
5452 : : bool
5453 : 23817 : gfc_resolve_dim_arg (gfc_expr *dim)
5454 : : {
5455 : 23817 : if (dim == NULL)
5456 : : return true;
5457 : :
5458 : 23817 : if (!gfc_resolve_expr (dim))
5459 : : return false;
5460 : :
5461 : 23817 : if (dim->rank != 0)
5462 : : {
5463 : 0 : gfc_error ("Argument dim at %L must be scalar", &dim->where);
5464 : 0 : return false;
5465 : :
5466 : : }
5467 : :
5468 : 23817 : if (dim->ts.type != BT_INTEGER)
5469 : : {
5470 : 0 : gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
5471 : 0 : return false;
5472 : : }
5473 : :
5474 : 23817 : if (dim->ts.kind != gfc_index_integer_kind)
5475 : : {
5476 : 15209 : gfc_typespec ts;
5477 : :
5478 : 15209 : gfc_clear_ts (&ts);
5479 : 15209 : ts.type = BT_INTEGER;
5480 : 15209 : ts.kind = gfc_index_integer_kind;
5481 : :
5482 : 15209 : gfc_convert_type_warn (dim, &ts, 2, 0);
5483 : : }
5484 : :
5485 : : return true;
5486 : : }
5487 : :
5488 : : /* Given an expression that contains array references, update those array
5489 : : references to point to the right array specifications. While this is
5490 : : filled in during matching, this information is difficult to save and load
5491 : : in a module, so we take care of it here.
5492 : :
5493 : : The idea here is that the original array reference comes from the
5494 : : base symbol. We traverse the list of reference structures, setting
5495 : : the stored reference to references. Component references can
5496 : : provide an additional array specification. */
5497 : : static void
5498 : : resolve_assoc_var (gfc_symbol* sym, bool resolve_target);
5499 : :
5500 : : static bool
5501 : 897 : find_array_spec (gfc_expr *e)
5502 : : {
5503 : 897 : gfc_array_spec *as;
5504 : 897 : gfc_component *c;
5505 : 897 : gfc_ref *ref;
5506 : 897 : bool class_as = false;
5507 : :
5508 : 897 : if (e->symtree->n.sym->assoc)
5509 : : {
5510 : 217 : if (e->symtree->n.sym->assoc->target)
5511 : 217 : gfc_resolve_expr (e->symtree->n.sym->assoc->target);
5512 : 217 : resolve_assoc_var (e->symtree->n.sym, false);
5513 : : }
5514 : :
5515 : 897 : if (e->symtree->n.sym->ts.type == BT_CLASS)
5516 : : {
5517 : 112 : as = CLASS_DATA (e->symtree->n.sym)->as;
5518 : 112 : class_as = true;
5519 : : }
5520 : : else
5521 : 785 : as = e->symtree->n.sym->as;
5522 : :
5523 : 2034 : for (ref = e->ref; ref; ref = ref->next)
5524 : 1144 : switch (ref->type)
5525 : : {
5526 : 899 : case REF_ARRAY:
5527 : 899 : if (as == NULL)
5528 : : {
5529 : 7 : locus loc = (GFC_LOCUS_IS_SET (ref->u.ar.where)
5530 : 14 : ? ref->u.ar.where : e->where);
5531 : 7 : gfc_error ("Invalid array reference of a non-array entity at %L",
5532 : : &loc);
5533 : 7 : return false;
5534 : : }
5535 : :
5536 : 892 : ref->u.ar.as = as;
5537 : 892 : if (ref->u.ar.dimen == -1) ref->u.ar.dimen = as->rank;
5538 : : as = NULL;
5539 : : break;
5540 : :
5541 : 221 : case REF_COMPONENT:
5542 : 221 : c = ref->u.c.component;
5543 : 221 : if (c->attr.dimension)
5544 : : {
5545 : 90 : if (as != NULL && !(class_as && as == c->as))
5546 : 0 : gfc_internal_error ("find_array_spec(): unused as(1)");
5547 : 90 : as = c->as;
5548 : : }
5549 : :
5550 : : break;
5551 : :
5552 : : case REF_SUBSTRING:
5553 : : case REF_INQUIRY:
5554 : : break;
5555 : : }
5556 : :
5557 : 890 : if (as != NULL)
5558 : 0 : gfc_internal_error ("find_array_spec(): unused as(2)");
5559 : :
5560 : : return true;
5561 : : }
5562 : :
5563 : :
5564 : : /* Resolve an array reference. */
5565 : :
5566 : : static bool
5567 : 411104 : resolve_array_ref (gfc_array_ref *ar)
5568 : : {
5569 : 411104 : int i, check_scalar;
5570 : 411104 : gfc_expr *e;
5571 : :
5572 : 644012 : for (i = 0; i < ar->dimen + ar->codimen; i++)
5573 : : {
5574 : 232936 : check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
5575 : :
5576 : : /* Do not force gfc_index_integer_kind for the start. We can
5577 : : do fine with any integer kind. This avoids temporary arrays
5578 : : created for indexing with a vector. */
5579 : 232936 : if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
5580 : : return false;
5581 : 232910 : if (!gfc_resolve_index (ar->end[i], check_scalar))
5582 : : return false;
5583 : 232908 : if (!gfc_resolve_index (ar->stride[i], check_scalar))
5584 : : return false;
5585 : :
5586 : 232908 : e = ar->start[i];
5587 : :
5588 : 232908 : if (ar->dimen_type[i] == DIMEN_UNKNOWN)
5589 : 140148 : switch (e->rank)
5590 : : {
5591 : 139284 : case 0:
5592 : 139284 : ar->dimen_type[i] = DIMEN_ELEMENT;
5593 : 139284 : break;
5594 : :
5595 : 864 : case 1:
5596 : 864 : ar->dimen_type[i] = DIMEN_VECTOR;
5597 : 864 : if (e->expr_type == EXPR_VARIABLE
5598 : 446 : && e->symtree->n.sym->ts.type == BT_DERIVED)
5599 : 13 : ar->start[i] = gfc_get_parentheses (e);
5600 : : break;
5601 : :
5602 : 0 : default:
5603 : 0 : gfc_error ("Array index at %L is an array of rank %d",
5604 : : &ar->c_where[i], e->rank);
5605 : 0 : return false;
5606 : : }
5607 : :
5608 : : /* Fill in the upper bound, which may be lower than the
5609 : : specified one for something like a(2:10:5), which is
5610 : : identical to a(2:7:5). Only relevant for strides not equal
5611 : : to one. Don't try a division by zero. */
5612 : 232908 : if (ar->dimen_type[i] == DIMEN_RANGE
5613 : 69992 : && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
5614 : 7797 : && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
5615 : 7650 : && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
5616 : : {
5617 : 7649 : mpz_t size, end;
5618 : :
5619 : 7649 : if (gfc_ref_dimen_size (ar, i, &size, &end))
5620 : : {
5621 : 6096 : if (ar->end[i] == NULL)
5622 : : {
5623 : 7864 : ar->end[i] =
5624 : 3932 : gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
5625 : : &ar->where);
5626 : 3932 : mpz_set (ar->end[i]->value.integer, end);
5627 : : }
5628 : 2164 : else if (ar->end[i]->ts.type == BT_INTEGER
5629 : 2164 : && ar->end[i]->expr_type == EXPR_CONSTANT)
5630 : : {
5631 : 2164 : mpz_set (ar->end[i]->value.integer, end);
5632 : : }
5633 : : else
5634 : 0 : gcc_unreachable ();
5635 : :
5636 : 6096 : mpz_clear (size);
5637 : 6096 : mpz_clear (end);
5638 : : }
5639 : : }
5640 : : }
5641 : :
5642 : 411076 : if (ar->type == AR_FULL)
5643 : : {
5644 : 257750 : if (ar->as->rank == 0)
5645 : 2923 : ar->type = AR_ELEMENT;
5646 : :
5647 : : /* Make sure array is the same as array(:,:), this way
5648 : : we don't need to special case all the time. */
5649 : 257750 : ar->dimen = ar->as->rank;
5650 : 616448 : for (i = 0; i < ar->dimen; i++)
5651 : : {
5652 : 358698 : ar->dimen_type[i] = DIMEN_RANGE;
5653 : :
5654 : 358698 : gcc_assert (ar->start[i] == NULL);
5655 : 358698 : gcc_assert (ar->end[i] == NULL);
5656 : 358698 : gcc_assert (ar->stride[i] == NULL);
5657 : : }
5658 : : }
5659 : :
5660 : : /* If the reference type is unknown, figure out what kind it is. */
5661 : :
5662 : 411076 : if (ar->type == AR_UNKNOWN)
5663 : : {
5664 : 141857 : ar->type = AR_ELEMENT;
5665 : 276021 : for (i = 0; i < ar->dimen; i++)
5666 : 170532 : if (ar->dimen_type[i] == DIMEN_RANGE
5667 : 170532 : || ar->dimen_type[i] == DIMEN_VECTOR)
5668 : : {
5669 : 36368 : ar->type = AR_SECTION;
5670 : 36368 : break;
5671 : : }
5672 : : }
5673 : :
5674 : 411076 : if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
5675 : : return false;
5676 : :
5677 : 411040 : if (ar->as->corank && ar->codimen == 0)
5678 : : {
5679 : 1655 : int n;
5680 : 1655 : ar->codimen = ar->as->corank;
5681 : 4546 : for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
5682 : 2891 : ar->dimen_type[n] = DIMEN_THIS_IMAGE;
5683 : : }
5684 : :
5685 : 411040 : if (ar->codimen)
5686 : : {
5687 : 10647 : if (ar->team_type == TEAM_NUMBER)
5688 : : {
5689 : 40 : if (!gfc_resolve_expr (ar->team))
5690 : : return false;
5691 : :
5692 : 40 : if (ar->team->rank != 0)
5693 : : {
5694 : 0 : gfc_error ("TEAM_NUMBER argument at %L must be scalar",
5695 : : &ar->team->where);
5696 : 0 : return false;
5697 : : }
5698 : :
5699 : 40 : if (ar->team->ts.type != BT_INTEGER)
5700 : : {
5701 : 4 : gfc_error ("TEAM_NUMBER argument at %L must be of INTEGER "
5702 : : "type, found %s",
5703 : 4 : &ar->team->where,
5704 : : gfc_basic_typename (ar->team->ts.type));
5705 : 4 : return false;
5706 : : }
5707 : : }
5708 : 10607 : else if (ar->team_type == TEAM_TEAM)
5709 : : {
5710 : 28 : if (!gfc_resolve_expr (ar->team))
5711 : : return false;
5712 : :
5713 : 28 : if (ar->team->rank != 0)
5714 : : {
5715 : 2 : gfc_error ("TEAM argument at %L must be scalar",
5716 : : &ar->team->where);
5717 : 2 : return false;
5718 : : }
5719 : :
5720 : 26 : if (ar->team->ts.type != BT_DERIVED
5721 : 24 : || ar->team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
5722 : 24 : || ar->team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
5723 : : {
5724 : 2 : gfc_error ("TEAM argument at %L must be of TEAM_TYPE from "
5725 : : "the intrinsic module ISO_FORTRAN_ENV, found %s",
5726 : 2 : &ar->team->where,
5727 : : gfc_basic_typename (ar->team->ts.type));
5728 : 2 : return false;
5729 : : }
5730 : : }
5731 : 10639 : if (ar->stat)
5732 : : {
5733 : 45 : if (!gfc_resolve_expr (ar->stat))
5734 : : return false;
5735 : :
5736 : 45 : if (ar->stat->rank != 0)
5737 : : {
5738 : 2 : gfc_error ("STAT argument at %L must be scalar",
5739 : : &ar->stat->where);
5740 : 2 : return false;
5741 : : }
5742 : :
5743 : 43 : if (ar->stat->ts.type != BT_INTEGER)
5744 : : {
5745 : 2 : gfc_error ("STAT argument at %L must be of INTEGER "
5746 : : "type, found %s",
5747 : 2 : &ar->stat->where,
5748 : : gfc_basic_typename (ar->stat->ts.type));
5749 : 2 : return false;
5750 : : }
5751 : :
5752 : 41 : if (ar->stat->expr_type != EXPR_VARIABLE)
5753 : : {
5754 : 0 : gfc_error ("STAT's expression at %L must be a variable",
5755 : : &ar->stat->where);
5756 : 0 : return false;
5757 : : }
5758 : : }
5759 : : }
5760 : : return true;
5761 : : }
5762 : :
5763 : :
5764 : : bool
5765 : 8315 : gfc_resolve_substring (gfc_ref *ref, bool *equal_length)
5766 : : {
5767 : 8315 : int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
5768 : :
5769 : 8315 : if (ref->u.ss.start != NULL)
5770 : : {
5771 : 8315 : if (!gfc_resolve_expr (ref->u.ss.start))
5772 : : return false;
5773 : :
5774 : 8315 : if (ref->u.ss.start->ts.type != BT_INTEGER)
5775 : : {
5776 : 1 : gfc_error ("Substring start index at %L must be of type INTEGER",
5777 : : &ref->u.ss.start->where);
5778 : 1 : return false;
5779 : : }
5780 : :
5781 : 8314 : if (ref->u.ss.start->rank != 0)
5782 : : {
5783 : 0 : gfc_error ("Substring start index at %L must be scalar",
5784 : : &ref->u.ss.start->where);
5785 : 0 : return false;
5786 : : }
5787 : :
5788 : 8314 : if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
5789 : 8314 : && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5790 : 37 : || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5791 : : {
5792 : 1 : gfc_error ("Substring start index at %L is less than one",
5793 : : &ref->u.ss.start->where);
5794 : 1 : return false;
5795 : : }
5796 : : }
5797 : :
5798 : 8313 : if (ref->u.ss.end != NULL)
5799 : : {
5800 : 8143 : if (!gfc_resolve_expr (ref->u.ss.end))
5801 : : return false;
5802 : :
5803 : 8143 : if (ref->u.ss.end->ts.type != BT_INTEGER)
5804 : : {
5805 : 1 : gfc_error ("Substring end index at %L must be of type INTEGER",
5806 : : &ref->u.ss.end->where);
5807 : 1 : return false;
5808 : : }
5809 : :
5810 : 8142 : if (ref->u.ss.end->rank != 0)
5811 : : {
5812 : 0 : gfc_error ("Substring end index at %L must be scalar",
5813 : : &ref->u.ss.end->where);
5814 : 0 : return false;
5815 : : }
5816 : :
5817 : 8142 : if (ref->u.ss.length != NULL
5818 : 7808 : && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
5819 : 8154 : && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5820 : 12 : || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5821 : : {
5822 : 4 : gfc_error ("Substring end index at %L exceeds the string length",
5823 : : &ref->u.ss.start->where);
5824 : 4 : return false;
5825 : : }
5826 : :
5827 : 8138 : if (compare_bound_mpz_t (ref->u.ss.end,
5828 : 8138 : gfc_integer_kinds[k].huge) == CMP_GT
5829 : 8138 : && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5830 : 7 : || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5831 : : {
5832 : 4 : gfc_error ("Substring end index at %L is too large",
5833 : : &ref->u.ss.end->where);
5834 : 4 : return false;
5835 : : }
5836 : : /* If the substring has the same length as the original
5837 : : variable, the reference itself can be deleted. */
5838 : :
5839 : 8134 : if (ref->u.ss.length != NULL
5840 : 7800 : && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
5841 : 9048 : && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
5842 : 228 : *equal_length = true;
5843 : : }
5844 : :
5845 : : return true;
5846 : : }
5847 : :
5848 : :
5849 : : /* This function supplies missing substring charlens. */
5850 : :
5851 : : void
5852 : 4543 : gfc_resolve_substring_charlen (gfc_expr *e)
5853 : : {
5854 : 4543 : gfc_ref *char_ref;
5855 : 4543 : gfc_expr *start, *end;
5856 : 4543 : gfc_typespec *ts = NULL;
5857 : 4543 : mpz_t diff;
5858 : :
5859 : 8842 : for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
5860 : : {
5861 : 7008 : if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
5862 : : break;
5863 : 4299 : if (char_ref->type == REF_COMPONENT)
5864 : 322 : ts = &char_ref->u.c.component->ts;
5865 : : }
5866 : :
5867 : 4543 : if (!char_ref || char_ref->type == REF_INQUIRY)
5868 : 1896 : return;
5869 : :
5870 : 2709 : gcc_assert (char_ref->next == NULL);
5871 : :
5872 : 2709 : if (e->ts.u.cl)
5873 : : {
5874 : 120 : if (e->ts.u.cl->length)
5875 : 108 : gfc_free_expr (e->ts.u.cl->length);
5876 : 12 : else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
5877 : : return;
5878 : : }
5879 : :
5880 : 2697 : if (!e->ts.u.cl)
5881 : 2589 : e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5882 : :
5883 : 2697 : if (char_ref->u.ss.start)
5884 : 2697 : start = gfc_copy_expr (char_ref->u.ss.start);
5885 : : else
5886 : 0 : start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
5887 : :
5888 : 2697 : if (char_ref->u.ss.end)
5889 : 2647 : end = gfc_copy_expr (char_ref->u.ss.end);
5890 : 50 : else if (e->expr_type == EXPR_VARIABLE)
5891 : : {
5892 : 50 : if (!ts)
5893 : 32 : ts = &e->symtree->n.sym->ts;
5894 : 50 : end = gfc_copy_expr (ts->u.cl->length);
5895 : : }
5896 : : else
5897 : : end = NULL;
5898 : :
5899 : 2697 : if (!start || !end)
5900 : : {
5901 : 50 : gfc_free_expr (start);
5902 : 50 : gfc_free_expr (end);
5903 : 50 : return;
5904 : : }
5905 : :
5906 : : /* Length = (end - start + 1).
5907 : : Check first whether it has a constant length. */
5908 : 2647 : if (gfc_dep_difference (end, start, &diff))
5909 : : {
5910 : 2532 : gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
5911 : : &e->where);
5912 : :
5913 : 2532 : mpz_add_ui (len->value.integer, diff, 1);
5914 : 2532 : mpz_clear (diff);
5915 : 2532 : e->ts.u.cl->length = len;
5916 : : /* The check for length < 0 is handled below */
5917 : : }
5918 : : else
5919 : : {
5920 : 115 : e->ts.u.cl->length = gfc_subtract (end, start);
5921 : 115 : e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
5922 : : gfc_get_int_expr (gfc_charlen_int_kind,
5923 : : NULL, 1));
5924 : : }
5925 : :
5926 : : /* F2008, 6.4.1: Both the starting point and the ending point shall
5927 : : be within the range 1, 2, ..., n unless the starting point exceeds
5928 : : the ending point, in which case the substring has length zero. */
5929 : :
5930 : 2647 : if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
5931 : 15 : mpz_set_si (e->ts.u.cl->length->value.integer, 0);
5932 : :
5933 : 2647 : e->ts.u.cl->length->ts.type = BT_INTEGER;
5934 : 2647 : e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5935 : :
5936 : : /* Make sure that the length is simplified. */
5937 : 2647 : gfc_simplify_expr (e->ts.u.cl->length, 1);
5938 : 2647 : gfc_resolve_expr (e->ts.u.cl->length);
5939 : : }
5940 : :
5941 : :
5942 : : /* Convert an array reference to an array element so that PDT KIND and LEN
5943 : : or inquiry references are always scalar. */
5944 : :
5945 : : static void
5946 : 20 : reset_array_ref_to_scalar (gfc_expr *expr, gfc_ref *array_ref)
5947 : : {
5948 : 20 : gfc_expr *unity = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
5949 : 20 : int dim;
5950 : :
5951 : 20 : array_ref->u.ar.type = AR_ELEMENT;
5952 : 20 : expr->rank = 0;
5953 : : /* Suppress the runtime bounds check. */
5954 : 20 : expr->no_bounds_check = 1;
5955 : 40 : for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
5956 : : {
5957 : 20 : array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
5958 : 20 : if (array_ref->u.ar.start[dim])
5959 : 0 : gfc_free_expr (array_ref->u.ar.start[dim]);
5960 : :
5961 : 20 : if (array_ref->u.ar.as && array_ref->u.ar.as->lower[dim])
5962 : 8 : array_ref->u.ar.start[dim]
5963 : 8 : = gfc_copy_expr (array_ref->u.ar.as->lower[dim]);
5964 : : else
5965 : 12 : array_ref->u.ar.start[dim] = gfc_copy_expr (unity);
5966 : :
5967 : 20 : if (array_ref->u.ar.end[dim])
5968 : 0 : gfc_free_expr (array_ref->u.ar.end[dim]);
5969 : 20 : if (array_ref->u.ar.stride[dim])
5970 : 0 : gfc_free_expr (array_ref->u.ar.stride[dim]);
5971 : : }
5972 : 20 : gfc_free_expr (unity);
5973 : 20 : }
5974 : :
5975 : :
5976 : : /* Resolve subtype references. */
5977 : :
5978 : : bool
5979 : 522857 : gfc_resolve_ref (gfc_expr *expr)
5980 : : {
5981 : 522857 : int current_part_dimension, n_components, seen_part_dimension;
5982 : 522857 : gfc_ref *ref, **prev, *array_ref;
5983 : 522857 : bool equal_length;
5984 : 522857 : gfc_symbol *last_pdt = NULL;
5985 : :
5986 : 1024015 : for (ref = expr->ref; ref; ref = ref->next)
5987 : 502055 : if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
5988 : : {
5989 : 897 : if (!find_array_spec (expr))
5990 : : return false;
5991 : : break;
5992 : : }
5993 : :
5994 : 1526879 : for (prev = &expr->ref; *prev != NULL;
5995 : 502116 : prev = *prev == NULL ? prev : &(*prev)->next)
5996 : 502200 : switch ((*prev)->type)
5997 : : {
5998 : 411104 : case REF_ARRAY:
5999 : 411104 : if (!resolve_array_ref (&(*prev)->u.ar))
6000 : : return false;
6001 : : break;
6002 : :
6003 : : case REF_COMPONENT:
6004 : : case REF_INQUIRY:
6005 : : break;
6006 : :
6007 : 8034 : case REF_SUBSTRING:
6008 : 8034 : equal_length = false;
6009 : 8034 : if (!gfc_resolve_substring (*prev, &equal_length))
6010 : : return false;
6011 : :
6012 : 8026 : if (expr->expr_type != EXPR_SUBSTRING && equal_length)
6013 : : {
6014 : : /* Remove the reference and move the charlen, if any. */
6015 : 203 : ref = *prev;
6016 : 203 : *prev = ref->next;
6017 : 203 : ref->next = NULL;
6018 : 203 : expr->ts.u.cl = ref->u.ss.length;
6019 : 203 : ref->u.ss.length = NULL;
6020 : 203 : gfc_free_ref_list (ref);
6021 : : }
6022 : : break;
6023 : : }
6024 : :
6025 : : /* Check constraints on part references. */
6026 : :
6027 : 522766 : current_part_dimension = 0;
6028 : 522766 : seen_part_dimension = 0;
6029 : 522766 : n_components = 0;
6030 : 522766 : array_ref = NULL;
6031 : :
6032 : 522766 : if (expr->expr_type == EXPR_VARIABLE
6033 : 435760 : && expr->symtree->n.sym->ts.type == BT_DERIVED
6034 : 74522 : && expr->symtree->n.sym->ts.u.derived->attr.pdt_type)
6035 : 522766 : last_pdt = expr->symtree->n.sym->ts.u.derived;
6036 : :
6037 : 1024654 : for (ref = expr->ref; ref; ref = ref->next)
6038 : : {
6039 : 501899 : switch (ref->type)
6040 : : {
6041 : 411021 : case REF_ARRAY:
6042 : 411021 : array_ref = ref;
6043 : 411021 : switch (ref->u.ar.type)
6044 : : {
6045 : 254825 : case AR_FULL:
6046 : : /* Coarray scalar. */
6047 : 254825 : if (ref->u.ar.as->rank == 0)
6048 : : {
6049 : : current_part_dimension = 0;
6050 : : break;
6051 : : }
6052 : : /* Fall through. */
6053 : 293893 : case AR_SECTION:
6054 : 293893 : current_part_dimension = 1;
6055 : 293893 : break;
6056 : :
6057 : 117128 : case AR_ELEMENT:
6058 : 117128 : array_ref = NULL;
6059 : 117128 : current_part_dimension = 0;
6060 : 117128 : break;
6061 : :
6062 : 0 : case AR_UNKNOWN:
6063 : 0 : gfc_internal_error ("resolve_ref(): Bad array reference");
6064 : : }
6065 : :
6066 : : break;
6067 : :
6068 : 82330 : case REF_COMPONENT:
6069 : 82330 : if (current_part_dimension || seen_part_dimension)
6070 : : {
6071 : : /* F03:C614. */
6072 : 5984 : if (ref->u.c.component->attr.pointer
6073 : 5981 : || ref->u.c.component->attr.proc_pointer
6074 : 5980 : || (ref->u.c.component->ts.type == BT_CLASS
6075 : 1 : && CLASS_DATA (ref->u.c.component)->attr.pointer))
6076 : : {
6077 : 4 : gfc_error ("Component to the right of a part reference "
6078 : : "with nonzero rank must not have the POINTER "
6079 : : "attribute at %L", &expr->where);
6080 : 4 : return false;
6081 : : }
6082 : 5980 : else if (ref->u.c.component->attr.allocatable
6083 : 5974 : || (ref->u.c.component->ts.type == BT_CLASS
6084 : 1 : && CLASS_DATA (ref->u.c.component)->attr.allocatable))
6085 : :
6086 : : {
6087 : 7 : gfc_error ("Component to the right of a part reference "
6088 : : "with nonzero rank must not have the ALLOCATABLE "
6089 : : "attribute at %L", &expr->where);
6090 : 7 : return false;
6091 : : }
6092 : : }
6093 : :
6094 : : /* Sometimes the component in a component reference is that of the
6095 : : pdt_template. Point to the component of pdt_type instead. This
6096 : : ensures that the component gets a backend_decl in translation. */
6097 : 82319 : if (last_pdt)
6098 : : {
6099 : 1091 : gfc_component *cmp = last_pdt->components;
6100 : 2913 : for (; cmp; cmp = cmp->next)
6101 : 2817 : if (!strcmp (cmp->name, ref->u.c.component->name))
6102 : : {
6103 : 995 : ref->u.c.component = cmp;
6104 : 995 : break;
6105 : : }
6106 : 1091 : ref->u.c.sym = last_pdt;
6107 : : }
6108 : :
6109 : : /* Convert pdt_templates, if necessary, and update 'last_pdt'. */
6110 : 82319 : if (ref->u.c.component->ts.type == BT_DERIVED)
6111 : : {
6112 : 18026 : if (ref->u.c.component->ts.u.derived->attr.pdt_template)
6113 : : {
6114 : 0 : if (gfc_get_pdt_instance (ref->u.c.component->param_list,
6115 : : &ref->u.c.component->ts.u.derived,
6116 : : NULL) != MATCH_YES)
6117 : : return false;
6118 : 0 : last_pdt = ref->u.c.component->ts.u.derived;
6119 : : }
6120 : 18026 : else if (ref->u.c.component->ts.u.derived->attr.pdt_type)
6121 : 68 : last_pdt = ref->u.c.component->ts.u.derived;
6122 : : else
6123 : : last_pdt = NULL;
6124 : : }
6125 : :
6126 : : /* The F08 standard requires(See R425, R431, R435, and in particular
6127 : : Note 6.7) that a PDT parameter reference be a scalar even if
6128 : : the designator is an array." */
6129 : 82319 : if (array_ref && last_pdt && last_pdt->attr.pdt_type
6130 : 18 : && (ref->u.c.component->attr.pdt_kind
6131 : 18 : || ref->u.c.component->attr.pdt_len))
6132 : 6 : reset_array_ref_to_scalar (expr, array_ref);
6133 : :
6134 : 82319 : n_components++;
6135 : 82319 : break;
6136 : :
6137 : : case REF_SUBSTRING:
6138 : : break;
6139 : :
6140 : 725 : case REF_INQUIRY:
6141 : : /* Implement requirement in note 9.7 of F2018 that the result of the
6142 : : LEN inquiry be a scalar. */
6143 : 725 : if (ref->u.i == INQUIRY_LEN && array_ref
6144 : 40 : && ((expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->length)
6145 : 40 : || expr->ts.type == BT_INTEGER))
6146 : 14 : reset_array_ref_to_scalar (expr, array_ref);
6147 : : break;
6148 : : }
6149 : :
6150 : 501888 : if (((ref->type == REF_COMPONENT && n_components > 1)
6151 : 490235 : || ref->next == NULL)
6152 : : && current_part_dimension
6153 : 443454 : && seen_part_dimension)
6154 : : {
6155 : 0 : gfc_error ("Two or more part references with nonzero rank must "
6156 : : "not be specified at %L", &expr->where);
6157 : 0 : return false;
6158 : : }
6159 : :
6160 : 501888 : if (ref->type == REF_COMPONENT)
6161 : : {
6162 : 82319 : if (current_part_dimension)
6163 : 5792 : seen_part_dimension = 1;
6164 : :
6165 : : /* reset to make sure */
6166 : : current_part_dimension = 0;
6167 : : }
6168 : : }
6169 : :
6170 : : return true;
6171 : : }
6172 : :
6173 : :
6174 : : /* Given an expression, determine its shape. This is easier than it sounds.
6175 : : Leaves the shape array NULL if it is not possible to determine the shape. */
6176 : :
6177 : : static void
6178 : 2544124 : expression_shape (gfc_expr *e)
6179 : : {
6180 : 2544124 : mpz_t array[GFC_MAX_DIMENSIONS];
6181 : 2544124 : int i;
6182 : :
6183 : 2544124 : if (e->rank <= 0 || e->shape != NULL)
6184 : 2375425 : return;
6185 : :
6186 : 681924 : for (i = 0; i < e->rank; i++)
6187 : 460611 : if (!gfc_array_dimen_size (e, i, &array[i]))
6188 : 168699 : goto fail;
6189 : :
6190 : 221313 : e->shape = gfc_get_shape (e->rank);
6191 : :
6192 : 221313 : memcpy (e->shape, array, e->rank * sizeof (mpz_t));
6193 : :
6194 : 221313 : return;
6195 : :
6196 : 168699 : fail:
6197 : 170370 : for (i--; i >= 0; i--)
6198 : 1671 : mpz_clear (array[i]);
6199 : : }
6200 : :
6201 : :
6202 : : /* Given a variable expression node, compute the rank of the expression by
6203 : : examining the base symbol and any reference structures it may have. */
6204 : :
6205 : : void
6206 : 2544124 : gfc_expression_rank (gfc_expr *e)
6207 : : {
6208 : 2544124 : gfc_ref *ref, *last_arr_ref = nullptr;
6209 : 2544124 : int i, rank, corank;
6210 : :
6211 : : /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
6212 : : could lead to serious confusion... */
6213 : 2544124 : gcc_assert (e->expr_type != EXPR_COMPCALL);
6214 : :
6215 : 2544124 : if (e->ref == NULL)
6216 : : {
6217 : 1881986 : if (e->expr_type == EXPR_ARRAY)
6218 : 67487 : goto done;
6219 : : /* Constructors can have a rank different from one via RESHAPE(). */
6220 : :
6221 : 1814499 : if (e->symtree != NULL)
6222 : : {
6223 : : /* After errors the ts.u.derived of a CLASS might not be set. */
6224 : 1814487 : gfc_array_spec *as = (e->symtree->n.sym->ts.type == BT_CLASS
6225 : 13667 : && e->symtree->n.sym->ts.u.derived
6226 : 13662 : && CLASS_DATA (e->symtree->n.sym))
6227 : 1814487 : ? CLASS_DATA (e->symtree->n.sym)->as
6228 : : : e->symtree->n.sym->as;
6229 : 1814487 : if (as)
6230 : : {
6231 : 580 : e->rank = as->rank;
6232 : 580 : e->corank = as->corank;
6233 : 580 : goto done;
6234 : : }
6235 : : }
6236 : 1813919 : e->rank = 0;
6237 : 1813919 : e->corank = 0;
6238 : 1813919 : goto done;
6239 : : }
6240 : :
6241 : : rank = 0;
6242 : : corank = 0;
6243 : :
6244 : 1039504 : for (ref = e->ref; ref; ref = ref->next)
6245 : : {
6246 : 758637 : if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
6247 : 547 : && ref->u.c.component->attr.function && !ref->next)
6248 : : {
6249 : 356 : rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
6250 : 356 : corank = ref->u.c.component->as ? ref->u.c.component->as->corank : 0;
6251 : : }
6252 : :
6253 : 758637 : if (ref->type != REF_ARRAY)
6254 : 147641 : continue;
6255 : :
6256 : 610996 : last_arr_ref = ref;
6257 : 610996 : if (ref->u.ar.type == AR_FULL && ref->u.ar.as)
6258 : : {
6259 : 337751 : rank = ref->u.ar.as->rank;
6260 : 337751 : break;
6261 : : }
6262 : :
6263 : 273245 : if (ref->u.ar.type == AR_SECTION)
6264 : : {
6265 : : /* Figure out the rank of the section. */
6266 : 43520 : if (rank != 0)
6267 : 0 : gfc_internal_error ("gfc_expression_rank(): Two array specs");
6268 : :
6269 : 108810 : for (i = 0; i < ref->u.ar.dimen; i++)
6270 : 65290 : if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
6271 : 65290 : || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
6272 : 57074 : rank++;
6273 : :
6274 : : break;
6275 : : }
6276 : : }
6277 : 662138 : if (last_arr_ref && last_arr_ref->u.ar.as
6278 : 594097 : && last_arr_ref->u.ar.as->rank != -1)
6279 : : {
6280 : 14912 : for (i = last_arr_ref->u.ar.as->rank;
6281 : 600913 : i < last_arr_ref->u.ar.as->rank + last_arr_ref->u.ar.as->corank; ++i)
6282 : : {
6283 : : /* For unknown dimen in non-resolved as assume full corank. */
6284 : 15651 : if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_STAR
6285 : 15178 : || (last_arr_ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
6286 : 266 : && !last_arr_ref->u.ar.as->resolved))
6287 : : {
6288 : : corank = last_arr_ref->u.ar.as->corank;
6289 : : break;
6290 : : }
6291 : 14912 : else if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_RANGE
6292 : 14912 : || last_arr_ref->u.ar.dimen_type[i] == DIMEN_VECTOR
6293 : 14829 : || last_arr_ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE)
6294 : 12912 : corank++;
6295 : 2000 : else if (last_arr_ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
6296 : 0 : gfc_internal_error ("Illegal coarray index");
6297 : : }
6298 : : }
6299 : :
6300 : 662138 : e->rank = rank;
6301 : 662138 : e->corank = corank;
6302 : :
6303 : 2544124 : done:
6304 : 2544124 : expression_shape (e);
6305 : 2544124 : }
6306 : :
6307 : :
6308 : : /* Given two expressions, check that their rank is conformable, i.e. either
6309 : : both have the same rank or at least one is a scalar. */
6310 : :
6311 : : bool
6312 : 12179799 : gfc_op_rank_conformable (gfc_expr *op1, gfc_expr *op2)
6313 : : {
6314 : 12179799 : if (op1->expr_type == EXPR_VARIABLE)
6315 : 722317 : gfc_expression_rank (op1);
6316 : 12179799 : if (op2->expr_type == EXPR_VARIABLE)
6317 : 443451 : gfc_expression_rank (op2);
6318 : :
6319 : 74394 : return (op1->rank == 0 || op2->rank == 0 || op1->rank == op2->rank)
6320 : 12253867 : && (op1->corank == 0 || op2->corank == 0 || op1->corank == op2->corank
6321 : 20 : || (!gfc_is_coindexed (op1) && !gfc_is_coindexed (op2)));
6322 : : }
6323 : :
6324 : : /* Resolve a variable expression. */
6325 : :
6326 : : static bool
6327 : 1293553 : resolve_variable (gfc_expr *e)
6328 : : {
6329 : 1293553 : gfc_symbol *sym;
6330 : 1293553 : bool t;
6331 : :
6332 : 1293553 : t = true;
6333 : :
6334 : 1293553 : if (e->symtree == NULL)
6335 : : return false;
6336 : 1293134 : sym = e->symtree->n.sym;
6337 : :
6338 : : /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
6339 : : as ts.type is set to BT_ASSUMED in resolve_symbol. */
6340 : 1293134 : if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
6341 : : {
6342 : 183 : if (!actual_arg || inquiry_argument)
6343 : : {
6344 : 2 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
6345 : : "be used as actual argument", sym->name, &e->where);
6346 : 2 : return false;
6347 : : }
6348 : : }
6349 : : /* TS 29113, 407b. */
6350 : 1292951 : else if (e->ts.type == BT_ASSUMED)
6351 : : {
6352 : 571 : if (!actual_arg)
6353 : : {
6354 : 20 : gfc_error ("Assumed-type variable %s at %L may only be used "
6355 : : "as actual argument", sym->name, &e->where);
6356 : 20 : return false;
6357 : : }
6358 : 551 : else if (inquiry_argument && !first_actual_arg)
6359 : : {
6360 : : /* FIXME: It doesn't work reliably as inquiry_argument is not set
6361 : : for all inquiry functions in resolve_function; the reason is
6362 : : that the function-name resolution happens too late in that
6363 : : function. */
6364 : 0 : gfc_error ("Assumed-type variable %s at %L as actual argument to "
6365 : : "an inquiry function shall be the first argument",
6366 : : sym->name, &e->where);
6367 : 0 : return false;
6368 : : }
6369 : : }
6370 : : /* TS 29113, C535b. */
6371 : 1292380 : else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
6372 : 35986 : && sym->ts.u.derived && CLASS_DATA (sym)
6373 : 35981 : && CLASS_DATA (sym)->as
6374 : 13761 : && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
6375 : 1291470 : || (sym->ts.type != BT_CLASS && sym->as
6376 : 353484 : && sym->as->type == AS_ASSUMED_RANK))
6377 : 7888 : && !sym->attr.select_rank_temporary
6378 : 7888 : && !(sym->assoc && sym->assoc->ar))
6379 : : {
6380 : 7888 : if (!actual_arg
6381 : 1247 : && !(cs_base && cs_base->current
6382 : 1246 : && (cs_base->current->op == EXEC_SELECT_RANK
6383 : 188 : || sym->attr.target)))
6384 : : {
6385 : 144 : gfc_error ("Assumed-rank variable %s at %L may only be used as "
6386 : : "actual argument", sym->name, &e->where);
6387 : 144 : return false;
6388 : : }
6389 : 7744 : else if (inquiry_argument && !first_actual_arg)
6390 : : {
6391 : : /* FIXME: It doesn't work reliably as inquiry_argument is not set
6392 : : for all inquiry functions in resolve_function; the reason is
6393 : : that the function-name resolution happens too late in that
6394 : : function. */
6395 : 0 : gfc_error ("Assumed-rank variable %s at %L as actual argument "
6396 : : "to an inquiry function shall be the first argument",
6397 : : sym->name, &e->where);
6398 : 0 : return false;
6399 : : }
6400 : : }
6401 : :
6402 : 1292968 : if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
6403 : 181 : && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6404 : 180 : && e->ref->next == NULL))
6405 : : {
6406 : 1 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
6407 : : "a subobject reference", sym->name, &e->ref->u.ar.where);
6408 : 1 : return false;
6409 : : }
6410 : : /* TS 29113, 407b. */
6411 : 1292967 : else if (e->ts.type == BT_ASSUMED && e->ref
6412 : 687 : && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6413 : 680 : && e->ref->next == NULL))
6414 : : {
6415 : 7 : gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
6416 : : "reference", sym->name, &e->ref->u.ar.where);
6417 : 7 : return false;
6418 : : }
6419 : :
6420 : : /* TS 29113, C535b. */
6421 : 1292960 : if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
6422 : 35986 : && sym->ts.u.derived && CLASS_DATA (sym)
6423 : 35981 : && CLASS_DATA (sym)->as
6424 : 13761 : && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
6425 : 1292050 : || (sym->ts.type != BT_CLASS && sym->as
6426 : 354020 : && sym->as->type == AS_ASSUMED_RANK))
6427 : 8028 : && !(sym->assoc && sym->assoc->ar)
6428 : 8028 : && e->ref
6429 : 8028 : && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6430 : 8024 : && e->ref->next == NULL))
6431 : : {
6432 : 4 : gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
6433 : : "reference", sym->name, &e->ref->u.ar.where);
6434 : 4 : return false;
6435 : : }
6436 : :
6437 : : /* Guessed type variables are associate_names whose selector had not been
6438 : : parsed at the time that the construct was parsed. Now the namespace is
6439 : : being resolved, the TKR of the selector will be available for fixup of
6440 : : the associate_name. */
6441 : 1292956 : if (IS_INFERRED_TYPE (e) && e->ref)
6442 : : {
6443 : 378 : gfc_fixup_inferred_type_refs (e);
6444 : : /* KIND inquiry ref returns the kind of the target. */
6445 : 378 : if (e->expr_type == EXPR_CONSTANT)
6446 : : return true;
6447 : : }
6448 : 1292578 : else if (sym->attr.select_type_temporary
6449 : 8826 : && sym->ns->assoc_name_inferred)
6450 : 92 : gfc_fixup_inferred_type_refs (e);
6451 : :
6452 : : /* For variables that are used in an associate (target => object) where
6453 : : the object's basetype is array valued while the target is scalar,
6454 : : the ts' type of the component refs is still array valued, which
6455 : : can't be translated that way. */
6456 : 1292944 : if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
6457 : 563 : && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
6458 : 563 : && sym->assoc->target->ts.u.derived
6459 : 563 : && CLASS_DATA (sym->assoc->target)
6460 : 563 : && CLASS_DATA (sym->assoc->target)->as)
6461 : : {
6462 : : gfc_ref *ref = e->ref;
6463 : 657 : while (ref)
6464 : : {
6465 : 499 : switch (ref->type)
6466 : : {
6467 : 216 : case REF_COMPONENT:
6468 : 216 : ref->u.c.sym = sym->ts.u.derived;
6469 : : /* Stop the loop. */
6470 : 216 : ref = NULL;
6471 : 216 : break;
6472 : 283 : default:
6473 : 283 : ref = ref->next;
6474 : 283 : break;
6475 : : }
6476 : : }
6477 : : }
6478 : :
6479 : : /* If this is an associate-name, it may be parsed with an array reference
6480 : : in error even though the target is scalar. Fail directly in this case.
6481 : : TODO Understand why class scalar expressions must be excluded. */
6482 : 1292944 : if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
6483 : : {
6484 : 10894 : if (sym->ts.type == BT_CLASS)
6485 : 242 : gfc_fix_class_refs (e);
6486 : 10894 : if (!sym->attr.dimension && !sym->attr.codimension && e->ref
6487 : 2025 : && e->ref->type == REF_ARRAY)
6488 : : {
6489 : : /* Unambiguously scalar! */
6490 : 3 : if (sym->assoc->target
6491 : 3 : && (sym->assoc->target->expr_type == EXPR_CONSTANT
6492 : 1 : || sym->assoc->target->expr_type == EXPR_STRUCTURE))
6493 : 2 : gfc_error ("Scalar variable %qs has an array reference at %L",
6494 : : sym->name, &e->where);
6495 : 3 : return false;
6496 : : }
6497 : 10891 : else if ((sym->attr.dimension || sym->attr.codimension)
6498 : 6797 : && (!e->ref || e->ref->type != REF_ARRAY))
6499 : : {
6500 : : /* This can happen because the parser did not detect that the
6501 : : associate name is an array and the expression had no array
6502 : : part_ref. */
6503 : 140 : gfc_ref *ref = gfc_get_ref ();
6504 : 140 : ref->type = REF_ARRAY;
6505 : 140 : ref->u.ar.type = AR_FULL;
6506 : 140 : if (sym->as)
6507 : : {
6508 : 139 : ref->u.ar.as = sym->as;
6509 : 139 : ref->u.ar.dimen = sym->as->rank;
6510 : : }
6511 : 140 : ref->next = e->ref;
6512 : 140 : e->ref = ref;
6513 : : }
6514 : : }
6515 : :
6516 : 1292941 : if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
6517 : 0 : sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
6518 : :
6519 : : /* On the other hand, the parser may not have known this is an array;
6520 : : in this case, we have to add a FULL reference. */
6521 : 1292941 : if (sym->assoc && (sym->attr.dimension || sym->attr.codimension) && !e->ref)
6522 : : {
6523 : 0 : e->ref = gfc_get_ref ();
6524 : 0 : e->ref->type = REF_ARRAY;
6525 : 0 : e->ref->u.ar.type = AR_FULL;
6526 : 0 : e->ref->u.ar.dimen = 0;
6527 : : }
6528 : :
6529 : : /* Like above, but for class types, where the checking whether an array
6530 : : ref is present is more complicated. Furthermore make sure not to add
6531 : : the full array ref to _vptr or _len refs. */
6532 : 1292941 : if (sym->assoc && sym->ts.type == BT_CLASS && sym->ts.u.derived
6533 : 968 : && CLASS_DATA (sym)
6534 : 968 : && (CLASS_DATA (sym)->attr.dimension
6535 : 417 : || CLASS_DATA (sym)->attr.codimension)
6536 : 555 : && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
6537 : : {
6538 : 531 : gfc_ref *ref, *newref;
6539 : :
6540 : 531 : newref = gfc_get_ref ();
6541 : 531 : newref->type = REF_ARRAY;
6542 : 531 : newref->u.ar.type = AR_FULL;
6543 : 531 : newref->u.ar.dimen = 0;
6544 : :
6545 : : /* Because this is an associate var and the first ref either is a ref to
6546 : : the _data component or not, no traversal of the ref chain is
6547 : : needed. The array ref needs to be inserted after the _data ref,
6548 : : or when that is not present, which may happened for polymorphic
6549 : : types, then at the first position. */
6550 : 531 : ref = e->ref;
6551 : 531 : if (!ref)
6552 : 18 : e->ref = newref;
6553 : 513 : else if (ref->type == REF_COMPONENT
6554 : 230 : && strcmp ("_data", ref->u.c.component->name) == 0)
6555 : : {
6556 : 230 : if (!ref->next || ref->next->type != REF_ARRAY)
6557 : : {
6558 : 12 : newref->next = ref->next;
6559 : 12 : ref->next = newref;
6560 : : }
6561 : : else
6562 : : /* Array ref present already. */
6563 : 218 : gfc_free_ref_list (newref);
6564 : : }
6565 : 283 : else if (ref->type == REF_ARRAY)
6566 : : /* Array ref present already. */
6567 : 283 : gfc_free_ref_list (newref);
6568 : : else
6569 : : {
6570 : 0 : newref->next = ref;
6571 : 0 : e->ref = newref;
6572 : : }
6573 : : }
6574 : 1292410 : else if (sym->assoc && sym->ts.type == BT_CHARACTER && sym->ts.deferred)
6575 : : {
6576 : 485 : gfc_ref *ref;
6577 : 908 : for (ref = e->ref; ref; ref = ref->next)
6578 : 453 : if (ref->type == REF_SUBSTRING)
6579 : : break;
6580 : 485 : if (ref == NULL)
6581 : 455 : e->ts = sym->ts;
6582 : : }
6583 : :
6584 : 1292941 : if (e->ref && !gfc_resolve_ref (e))
6585 : : return false;
6586 : :
6587 : 1292855 : if (sym->attr.flavor == FL_PROCEDURE
6588 : 29749 : && (!sym->attr.function
6589 : 17380 : || (sym->attr.function && sym->result
6590 : 16932 : && sym->result->attr.proc_pointer
6591 : 562 : && !sym->result->attr.function)))
6592 : : {
6593 : 12369 : e->ts.type = BT_PROCEDURE;
6594 : 12369 : goto resolve_procedure;
6595 : : }
6596 : :
6597 : 1280486 : if (sym->ts.type != BT_UNKNOWN)
6598 : 1279847 : gfc_variable_attr (e, &e->ts);
6599 : 639 : else if (sym->attr.flavor == FL_PROCEDURE
6600 : 12 : && sym->attr.function && sym->result
6601 : 12 : && sym->result->ts.type != BT_UNKNOWN
6602 : 10 : && sym->result->attr.proc_pointer)
6603 : 10 : e->ts = sym->result->ts;
6604 : : else
6605 : : {
6606 : : /* Must be a simple variable reference. */
6607 : 629 : if (!gfc_set_default_type (sym, 1, sym->ns))
6608 : : return false;
6609 : 503 : e->ts = sym->ts;
6610 : : }
6611 : :
6612 : 1280360 : if (check_assumed_size_reference (sym, e))
6613 : : return false;
6614 : :
6615 : : /* Deal with forward references to entries during gfc_resolve_code, to
6616 : : satisfy, at least partially, 12.5.2.5. */
6617 : 1280341 : if (gfc_current_ns->entries
6618 : 3060 : && current_entry_id == sym->entry_id
6619 : 1000 : && cs_base
6620 : 914 : && cs_base->current
6621 : 914 : && cs_base->current->op != EXEC_ENTRY)
6622 : : {
6623 : 914 : gfc_entry_list *entry;
6624 : 914 : gfc_formal_arglist *formal;
6625 : 914 : int n;
6626 : 914 : bool seen, saved_specification_expr;
6627 : :
6628 : : /* If the symbol is a dummy... */
6629 : 914 : if (sym->attr.dummy && sym->ns == gfc_current_ns)
6630 : : {
6631 : : entry = gfc_current_ns->entries;
6632 : : seen = false;
6633 : :
6634 : : /* ...test if the symbol is a parameter of previous entries. */
6635 : 1033 : for (; entry && entry->id <= current_entry_id; entry = entry->next)
6636 : 1006 : for (formal = entry->sym->formal; formal; formal = formal->next)
6637 : : {
6638 : 997 : if (formal->sym && sym->name == formal->sym->name)
6639 : : {
6640 : : seen = true;
6641 : : break;
6642 : : }
6643 : : }
6644 : :
6645 : : /* If it has not been seen as a dummy, this is an error. */
6646 : 453 : if (!seen)
6647 : : {
6648 : 3 : if (specification_expr)
6649 : 2 : gfc_error ("Variable %qs, used in a specification expression"
6650 : : ", is referenced at %L before the ENTRY statement "
6651 : : "in which it is a parameter",
6652 : : sym->name, &cs_base->current->loc);
6653 : : else
6654 : 1 : gfc_error ("Variable %qs is used at %L before the ENTRY "
6655 : : "statement in which it is a parameter",
6656 : : sym->name, &cs_base->current->loc);
6657 : : t = false;
6658 : : }
6659 : : }
6660 : :
6661 : : /* Now do the same check on the specification expressions. */
6662 : 914 : saved_specification_expr = specification_expr;
6663 : 914 : specification_expr = true;
6664 : 914 : if (sym->ts.type == BT_CHARACTER
6665 : 914 : && !gfc_resolve_expr (sym->ts.u.cl->length))
6666 : : t = false;
6667 : :
6668 : 914 : if (sym->as)
6669 : : {
6670 : 271 : for (n = 0; n < sym->as->rank; n++)
6671 : : {
6672 : 159 : if (!gfc_resolve_expr (sym->as->lower[n]))
6673 : 0 : t = false;
6674 : 159 : if (!gfc_resolve_expr (sym->as->upper[n]))
6675 : 1 : t = false;
6676 : : }
6677 : : }
6678 : 914 : specification_expr = saved_specification_expr;
6679 : :
6680 : 914 : if (t)
6681 : : /* Update the symbol's entry level. */
6682 : 909 : sym->entry_id = current_entry_id + 1;
6683 : : }
6684 : :
6685 : : /* If a symbol has been host_associated mark it. This is used latter,
6686 : : to identify if aliasing is possible via host association. */
6687 : 1280341 : if (sym->attr.flavor == FL_VARIABLE
6688 : 1244123 : && (!sym->ns->code || sym->ns->code->op != EXEC_BLOCK
6689 : 5362 : || !sym->ns->code->ext.block.assoc)
6690 : 1242613 : && gfc_current_ns->parent
6691 : 593526 : && (gfc_current_ns->parent == sym->ns
6692 : 556431 : || (gfc_current_ns->parent->parent
6693 : 10681 : && gfc_current_ns->parent->parent == sym->ns)))
6694 : 43460 : sym->attr.host_assoc = 1;
6695 : :
6696 : 1280341 : if (gfc_current_ns->proc_name
6697 : 1276967 : && sym->attr.dimension
6698 : 348634 : && (sym->ns != gfc_current_ns
6699 : 325044 : || sym->attr.use_assoc
6700 : 321093 : || sym->attr.in_common))
6701 : 32329 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
6702 : :
6703 : 1292710 : resolve_procedure:
6704 : 1292710 : if (t && !resolve_procedure_expression (e))
6705 : : t = false;
6706 : :
6707 : : /* F2008, C617 and C1229. */
6708 : 1291683 : if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
6709 : 1385678 : && gfc_is_coindexed (e))
6710 : : {
6711 : 304 : gfc_ref *ref, *ref2 = NULL;
6712 : :
6713 : 383 : for (ref = e->ref; ref; ref = ref->next)
6714 : : {
6715 : 383 : if (ref->type == REF_COMPONENT)
6716 : 79 : ref2 = ref;
6717 : 383 : if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
6718 : : break;
6719 : : }
6720 : :
6721 : 608 : for ( ; ref; ref = ref->next)
6722 : 316 : if (ref->type == REF_COMPONENT)
6723 : : break;
6724 : :
6725 : : /* Expression itself is not coindexed object. */
6726 : 304 : if (ref && e->ts.type == BT_CLASS)
6727 : : {
6728 : 3 : gfc_error ("Polymorphic subobject of coindexed object at %L",
6729 : : &e->where);
6730 : 3 : t = false;
6731 : : }
6732 : :
6733 : : /* Expression itself is coindexed object. */
6734 : 292 : if (ref == NULL)
6735 : : {
6736 : 292 : gfc_component *c;
6737 : 292 : c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
6738 : 408 : for ( ; c; c = c->next)
6739 : 116 : if (c->attr.allocatable && c->ts.type == BT_CLASS)
6740 : : {
6741 : 0 : gfc_error ("Coindexed object with polymorphic allocatable "
6742 : : "subcomponent at %L", &e->where);
6743 : 0 : t = false;
6744 : 0 : break;
6745 : : }
6746 : : }
6747 : : }
6748 : :
6749 : 1292710 : if (t)
6750 : 1292702 : gfc_expression_rank (e);
6751 : :
6752 : 1292710 : if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym != sym->result)
6753 : 3 : gfc_warning (OPT_Wdeprecated_declarations,
6754 : : "Using variable %qs at %L is deprecated",
6755 : : sym->name, &e->where);
6756 : : /* Simplify cases where access to a parameter array results in a
6757 : : single constant. Suppress errors since those will have been
6758 : : issued before, as warnings. */
6759 : 1292710 : if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
6760 : : {
6761 : 2599 : gfc_push_suppress_errors ();
6762 : 2599 : gfc_simplify_expr (e, 1);
6763 : 2599 : gfc_pop_suppress_errors ();
6764 : : }
6765 : :
6766 : : return t;
6767 : : }
6768 : :
6769 : :
6770 : : /* 'sym' was initially guessed to be derived type but has been corrected
6771 : : in resolve_assoc_var to be a class entity or the derived type correcting.
6772 : : If a class entity it will certainly need the _data reference or the
6773 : : reference derived type symbol correcting in the first component ref if
6774 : : a derived type. */
6775 : :
6776 : : void
6777 : 856 : gfc_fixup_inferred_type_refs (gfc_expr *e)
6778 : : {
6779 : 856 : gfc_ref *ref, *new_ref;
6780 : 856 : gfc_symbol *sym, *derived;
6781 : 856 : gfc_expr *target;
6782 : 856 : sym = e->symtree->n.sym;
6783 : :
6784 : : /* An associate_name whose selector is (i) a component ref of a selector
6785 : : that is a inferred type associate_name; or (ii) an intrinsic type that
6786 : : has been inferred from an inquiry ref. */
6787 : 856 : if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
6788 : : {
6789 : 282 : sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
6790 : 282 : sym->attr.codimension = sym->assoc->target->corank ? 1 : 0;
6791 : 282 : if (!sym->attr.dimension && e->ref->type == REF_ARRAY)
6792 : : {
6793 : 60 : ref = e->ref;
6794 : : /* A substring misidentified as an array section. */
6795 : 60 : if (sym->ts.type == BT_CHARACTER
6796 : 30 : && ref->u.ar.start[0] && ref->u.ar.end[0]
6797 : 6 : && !ref->u.ar.stride[0])
6798 : : {
6799 : 6 : new_ref = gfc_get_ref ();
6800 : 6 : new_ref->type = REF_SUBSTRING;
6801 : 6 : new_ref->u.ss.start = ref->u.ar.start[0];
6802 : 6 : new_ref->u.ss.end = ref->u.ar.end[0];
6803 : 6 : new_ref->u.ss.length = sym->ts.u.cl;
6804 : 6 : *ref = *new_ref;
6805 : 6 : free (new_ref);
6806 : : }
6807 : : else
6808 : : {
6809 : 54 : if (e->ref->u.ar.type == AR_UNKNOWN)
6810 : 24 : gfc_error ("Invalid array reference at %L", &e->where);
6811 : 54 : e->ref = ref->next;
6812 : 54 : free (ref);
6813 : : }
6814 : : }
6815 : :
6816 : : /* It is possible for an inquiry reference to be mistaken for a
6817 : : component reference. Correct this now. */
6818 : 282 : ref = e->ref;
6819 : 282 : if (ref && ref->type == REF_ARRAY)
6820 : 138 : ref = ref->next;
6821 : 150 : if (ref && ref->type == REF_COMPONENT
6822 : 150 : && is_inquiry_ref (ref->u.c.component->name, &new_ref))
6823 : : {
6824 : 12 : e->symtree->n.sym = sym;
6825 : 12 : *ref = *new_ref;
6826 : 12 : gfc_free_ref_list (new_ref);
6827 : : }
6828 : :
6829 : : /* The kind of the associate name is best evaluated directly from the
6830 : : selector because of the guesses made in primary.cc, when the type
6831 : : is still unknown. */
6832 : 282 : if (ref && ref->type == REF_INQUIRY && ref->u.i == INQUIRY_KIND)
6833 : : {
6834 : 24 : gfc_expr *ne = gfc_get_int_expr (gfc_default_integer_kind, &e->where,
6835 : 12 : sym->assoc->target->ts.kind);
6836 : 12 : gfc_replace_expr (e, ne);
6837 : : }
6838 : :
6839 : : /* Now that the references are all sorted out, set the expression rank
6840 : : and return. */
6841 : 282 : gfc_expression_rank (e);
6842 : 282 : return;
6843 : : }
6844 : :
6845 : 574 : derived = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->ts.u.derived
6846 : : : sym->ts.u.derived;
6847 : :
6848 : : /* Ensure that class symbols have an array spec and ensure that there
6849 : : is a _data field reference following class type references. */
6850 : 574 : if (sym->ts.type == BT_CLASS
6851 : 196 : && sym->assoc->target->ts.type == BT_CLASS)
6852 : : {
6853 : 196 : e->rank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->rank : 0;
6854 : 196 : e->corank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->corank : 0;
6855 : 196 : sym->attr.dimension = 0;
6856 : 196 : sym->attr.codimension = 0;
6857 : 196 : CLASS_DATA (sym)->attr.dimension = e->rank ? 1 : 0;
6858 : 196 : CLASS_DATA (sym)->attr.codimension = e->corank ? 1 : 0;
6859 : 196 : if (e->ref && (e->ref->type != REF_COMPONENT
6860 : 160 : || e->ref->u.c.component->name[0] != '_'))
6861 : : {
6862 : 82 : ref = gfc_get_ref ();
6863 : 82 : ref->type = REF_COMPONENT;
6864 : 82 : ref->next = e->ref;
6865 : 82 : e->ref = ref;
6866 : 82 : ref->u.c.component = gfc_find_component (sym->ts.u.derived, "_data",
6867 : : true, true, NULL);
6868 : 82 : ref->u.c.sym = sym->ts.u.derived;
6869 : : }
6870 : : }
6871 : :
6872 : : /* Proceed as far as the first component reference and ensure that the
6873 : : correct derived type is being used. */
6874 : 837 : for (ref = e->ref; ref; ref = ref->next)
6875 : 801 : if (ref->type == REF_COMPONENT)
6876 : : {
6877 : 538 : if (ref->u.c.component->name[0] != '_')
6878 : 342 : ref->u.c.sym = derived;
6879 : : else
6880 : 196 : ref->u.c.sym = sym->ts.u.derived;
6881 : : break;
6882 : : }
6883 : :
6884 : : /* Verify that the type inferrence mechanism has not introduced a spurious
6885 : : array reference. This can happen with an associate name, whose selector
6886 : : is an element of another inferred type. */
6887 : 574 : target = e->symtree->n.sym->assoc->target;
6888 : 574 : if (!(sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as)
6889 : 162 : && e != target && !target->rank)
6890 : : {
6891 : : /* First case: array ref after the scalar class or derived
6892 : : associate_name. */
6893 : 162 : if (e->ref && e->ref->type == REF_ARRAY
6894 : 7 : && e->ref->u.ar.type != AR_ELEMENT)
6895 : : {
6896 : 7 : ref = e->ref;
6897 : 7 : if (ref->u.ar.type == AR_UNKNOWN)
6898 : 1 : gfc_error ("Invalid array reference at %L", &e->where);
6899 : 7 : e->ref = ref->next;
6900 : 7 : free (ref);
6901 : :
6902 : : /* If it hasn't a ref to the '_data' field supply one. */
6903 : 7 : if (sym->ts.type == BT_CLASS
6904 : 0 : && !(e->ref->type == REF_COMPONENT
6905 : 0 : && strcmp (e->ref->u.c.component->name, "_data")))
6906 : : {
6907 : 0 : gfc_ref *new_ref;
6908 : 0 : gfc_find_component (e->symtree->n.sym->ts.u.derived,
6909 : : "_data", true, true, &new_ref);
6910 : 0 : new_ref->next = e->ref;
6911 : 0 : e->ref = new_ref;
6912 : : }
6913 : : }
6914 : : /* 2nd case: a ref to the '_data' field followed by an array ref. */
6915 : 155 : else if (e->ref && e->ref->type == REF_COMPONENT
6916 : 155 : && strcmp (e->ref->u.c.component->name, "_data") == 0
6917 : 64 : && e->ref->next && e->ref->next->type == REF_ARRAY
6918 : 0 : && e->ref->next->u.ar.type != AR_ELEMENT)
6919 : : {
6920 : 0 : ref = e->ref->next;
6921 : 0 : if (ref->u.ar.type == AR_UNKNOWN)
6922 : 0 : gfc_error ("Invalid array reference at %L", &e->where);
6923 : 0 : e->ref->next = e->ref->next->next;
6924 : 0 : free (ref);
6925 : : }
6926 : : }
6927 : :
6928 : : /* Now that all the references are OK, get the expression rank. */
6929 : 574 : gfc_expression_rank (e);
6930 : : }
6931 : :
6932 : :
6933 : : /* Checks to see that the correct symbol has been host associated.
6934 : : The only situations where this arises are:
6935 : : (i) That in which a twice contained function is parsed after
6936 : : the host association is made. On detecting this, change
6937 : : the symbol in the expression and convert the array reference
6938 : : into an actual arglist if the old symbol is a variable; or
6939 : : (ii) That in which an external function is typed but not declared
6940 : : explicitly to be external. Here, the old symbol is changed
6941 : : from a variable to an external function. */
6942 : : static bool
6943 : 1629864 : check_host_association (gfc_expr *e)
6944 : : {
6945 : 1629864 : gfc_symbol *sym, *old_sym;
6946 : 1629864 : gfc_symtree *st;
6947 : 1629864 : int n;
6948 : 1629864 : gfc_ref *ref;
6949 : 1629864 : gfc_actual_arglist *arg, *tail = NULL;
6950 : 1629864 : bool retval = e->expr_type == EXPR_FUNCTION;
6951 : :
6952 : : /* If the expression is the result of substitution in
6953 : : interface.cc(gfc_extend_expr) because there is no way in
6954 : : which the host association can be wrong. */
6955 : 1629864 : if (e->symtree == NULL
6956 : 1629116 : || e->symtree->n.sym == NULL
6957 : 1629116 : || e->user_operator)
6958 : : return retval;
6959 : :
6960 : 1627345 : old_sym = e->symtree->n.sym;
6961 : :
6962 : 1627345 : if (gfc_current_ns->parent
6963 : 714724 : && old_sym->ns != gfc_current_ns)
6964 : : {
6965 : : /* Use the 'USE' name so that renamed module symbols are
6966 : : correctly handled. */
6967 : 87628 : gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
6968 : :
6969 : 87628 : if (sym && old_sym != sym
6970 : 488 : && sym->attr.flavor == FL_PROCEDURE
6971 : 93 : && sym->attr.contained)
6972 : : {
6973 : : /* Clear the shape, since it might not be valid. */
6974 : 71 : gfc_free_shape (&e->shape, e->rank);
6975 : :
6976 : : /* Give the expression the right symtree! */
6977 : 71 : gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
6978 : 71 : gcc_assert (st != NULL);
6979 : :
6980 : 71 : if (old_sym->attr.flavor == FL_PROCEDURE
6981 : 47 : || e->expr_type == EXPR_FUNCTION)
6982 : : {
6983 : : /* Original was function so point to the new symbol, since
6984 : : the actual argument list is already attached to the
6985 : : expression. */
6986 : 30 : e->value.function.esym = NULL;
6987 : 30 : e->symtree = st;
6988 : : }
6989 : : else
6990 : : {
6991 : : /* Original was variable so convert array references into
6992 : : an actual arglist. This does not need any checking now
6993 : : since resolve_function will take care of it. */
6994 : 41 : e->value.function.actual = NULL;
6995 : 41 : e->expr_type = EXPR_FUNCTION;
6996 : 41 : e->symtree = st;
6997 : :
6998 : : /* Ambiguity will not arise if the array reference is not
6999 : : the last reference. */
7000 : 43 : for (ref = e->ref; ref; ref = ref->next)
7001 : 38 : if (ref->type == REF_ARRAY && ref->next == NULL)
7002 : : break;
7003 : :
7004 : 41 : if ((ref == NULL || ref->type != REF_ARRAY)
7005 : 5 : && sym->attr.proc == PROC_INTERNAL)
7006 : : {
7007 : 4 : gfc_error ("%qs at %L is host associated at %L into "
7008 : : "a contained procedure with an internal "
7009 : : "procedure of the same name", sym->name,
7010 : : &old_sym->declared_at, &e->where);
7011 : 4 : return false;
7012 : : }
7013 : :
7014 : 1 : if (ref == NULL)
7015 : : return false;
7016 : :
7017 : 36 : gcc_assert (ref->type == REF_ARRAY);
7018 : :
7019 : : /* Grab the start expressions from the array ref and
7020 : : copy them into actual arguments. */
7021 : 84 : for (n = 0; n < ref->u.ar.dimen; n++)
7022 : : {
7023 : 48 : arg = gfc_get_actual_arglist ();
7024 : 48 : arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
7025 : 48 : if (e->value.function.actual == NULL)
7026 : 36 : tail = e->value.function.actual = arg;
7027 : : else
7028 : : {
7029 : 12 : tail->next = arg;
7030 : 12 : tail = arg;
7031 : : }
7032 : : }
7033 : :
7034 : : /* Dump the reference list and set the rank. */
7035 : 36 : gfc_free_ref_list (e->ref);
7036 : 36 : e->ref = NULL;
7037 : 36 : e->rank = sym->as ? sym->as->rank : 0;
7038 : 36 : e->corank = sym->as ? sym->as->corank : 0;
7039 : : }
7040 : :
7041 : 66 : gfc_resolve_expr (e);
7042 : 66 : sym->refs++;
7043 : : }
7044 : : /* This case corresponds to a call, from a block or a contained
7045 : : procedure, to an external function, which has not been declared
7046 : : as being external in the main program but has been typed. */
7047 : 87557 : else if (sym && old_sym != sym
7048 : 417 : && !e->ref
7049 : 255 : && sym->ts.type == BT_UNKNOWN
7050 : 21 : && old_sym->ts.type != BT_UNKNOWN
7051 : 19 : && sym->attr.flavor == FL_PROCEDURE
7052 : 19 : && old_sym->attr.flavor == FL_VARIABLE
7053 : 7 : && sym->ns->parent == old_sym->ns
7054 : 7 : && sym->ns->proc_name
7055 : 7 : && sym->ns->proc_name->attr.proc != PROC_MODULE
7056 : 6 : && (sym->ns->proc_name->attr.flavor == FL_LABEL
7057 : 6 : || sym->ns->proc_name->attr.flavor == FL_PROCEDURE))
7058 : : {
7059 : 6 : old_sym->attr.flavor = FL_PROCEDURE;
7060 : 6 : old_sym->attr.external = 1;
7061 : 6 : old_sym->attr.function = 1;
7062 : 6 : old_sym->result = old_sym;
7063 : 6 : gfc_resolve_expr (e);
7064 : : }
7065 : : }
7066 : : /* This might have changed! */
7067 : 1627340 : return e->expr_type == EXPR_FUNCTION;
7068 : : }
7069 : :
7070 : :
7071 : : static void
7072 : 1328 : gfc_resolve_character_operator (gfc_expr *e)
7073 : : {
7074 : 1328 : gfc_expr *op1 = e->value.op.op1;
7075 : 1328 : gfc_expr *op2 = e->value.op.op2;
7076 : 1328 : gfc_expr *e1 = NULL;
7077 : 1328 : gfc_expr *e2 = NULL;
7078 : :
7079 : 1328 : gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
7080 : :
7081 : 1328 : if (op1->ts.u.cl && op1->ts.u.cl->length)
7082 : 653 : e1 = gfc_copy_expr (op1->ts.u.cl->length);
7083 : 675 : else if (op1->expr_type == EXPR_CONSTANT)
7084 : 268 : e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
7085 : 268 : op1->value.character.length);
7086 : :
7087 : 1328 : if (op2->ts.u.cl && op2->ts.u.cl->length)
7088 : 641 : e2 = gfc_copy_expr (op2->ts.u.cl->length);
7089 : 687 : else if (op2->expr_type == EXPR_CONSTANT)
7090 : 457 : e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
7091 : 457 : op2->value.character.length);
7092 : :
7093 : 1328 : e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
7094 : :
7095 : 1328 : if (!e1 || !e2)
7096 : : {
7097 : 535 : gfc_free_expr (e1);
7098 : 535 : gfc_free_expr (e2);
7099 : :
7100 : 535 : return;
7101 : : }
7102 : :
7103 : 793 : e->ts.u.cl->length = gfc_add (e1, e2);
7104 : 793 : e->ts.u.cl->length->ts.type = BT_INTEGER;
7105 : 793 : e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
7106 : 793 : gfc_simplify_expr (e->ts.u.cl->length, 0);
7107 : 793 : gfc_resolve_expr (e->ts.u.cl->length);
7108 : :
7109 : 793 : return;
7110 : : }
7111 : :
7112 : :
7113 : : /* Ensure that an character expression has a charlen and, if possible, a
7114 : : length expression. */
7115 : :
7116 : : static void
7117 : 176304 : fixup_charlen (gfc_expr *e)
7118 : : {
7119 : : /* The cases fall through so that changes in expression type and the need
7120 : : for multiple fixes are picked up. In all circumstances, a charlen should
7121 : : be available for the middle end to hang a backend_decl on. */
7122 : 176304 : switch (e->expr_type)
7123 : : {
7124 : 1328 : case EXPR_OP:
7125 : 1328 : gfc_resolve_character_operator (e);
7126 : : /* FALLTHRU */
7127 : :
7128 : 1383 : case EXPR_ARRAY:
7129 : 1383 : if (e->expr_type == EXPR_ARRAY)
7130 : 55 : gfc_resolve_character_array_constructor (e);
7131 : : /* FALLTHRU */
7132 : :
7133 : 1839 : case EXPR_SUBSTRING:
7134 : 1839 : if (!e->ts.u.cl && e->ref)
7135 : 452 : gfc_resolve_substring_charlen (e);
7136 : : /* FALLTHRU */
7137 : :
7138 : 176304 : default:
7139 : 176304 : if (!e->ts.u.cl)
7140 : 174469 : e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
7141 : :
7142 : 176304 : break;
7143 : : }
7144 : 176304 : }
7145 : :
7146 : :
7147 : : /* Update an actual argument to include the passed-object for type-bound
7148 : : procedures at the right position. */
7149 : :
7150 : : static gfc_actual_arglist*
7151 : 2891 : update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
7152 : : const char *name)
7153 : : {
7154 : 2915 : gcc_assert (argpos > 0);
7155 : :
7156 : 2915 : if (argpos == 1)
7157 : : {
7158 : 2766 : gfc_actual_arglist* result;
7159 : :
7160 : 2766 : result = gfc_get_actual_arglist ();
7161 : 2766 : result->expr = po;
7162 : 2766 : result->next = lst;
7163 : 2766 : if (name)
7164 : 514 : result->name = name;
7165 : :
7166 : 2766 : return result;
7167 : : }
7168 : :
7169 : 149 : if (lst)
7170 : 125 : lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
7171 : : else
7172 : 24 : lst = update_arglist_pass (NULL, po, argpos - 1, name);
7173 : : return lst;
7174 : : }
7175 : :
7176 : :
7177 : : /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
7178 : :
7179 : : static gfc_expr*
7180 : 6999 : extract_compcall_passed_object (gfc_expr* e)
7181 : : {
7182 : 6999 : gfc_expr* po;
7183 : :
7184 : 6999 : if (e->expr_type == EXPR_UNKNOWN)
7185 : : {
7186 : 0 : gfc_error ("Error in typebound call at %L",
7187 : : &e->where);
7188 : 0 : return NULL;
7189 : : }
7190 : :
7191 : 6999 : gcc_assert (e->expr_type == EXPR_COMPCALL);
7192 : :
7193 : 6999 : if (e->value.compcall.base_object)
7194 : 1506 : po = gfc_copy_expr (e->value.compcall.base_object);
7195 : : else
7196 : : {
7197 : 5493 : po = gfc_get_expr ();
7198 : 5493 : po->expr_type = EXPR_VARIABLE;
7199 : 5493 : po->symtree = e->symtree;
7200 : 5493 : po->ref = gfc_copy_ref (e->ref);
7201 : 5493 : po->where = e->where;
7202 : : }
7203 : :
7204 : 6999 : if (!gfc_resolve_expr (po))
7205 : : return NULL;
7206 : :
7207 : : return po;
7208 : : }
7209 : :
7210 : :
7211 : : /* Update the arglist of an EXPR_COMPCALL expression to include the
7212 : : passed-object. */
7213 : :
7214 : : static bool
7215 : 3228 : update_compcall_arglist (gfc_expr* e)
7216 : : {
7217 : 3228 : gfc_expr* po;
7218 : 3228 : gfc_typebound_proc* tbp;
7219 : :
7220 : 3228 : tbp = e->value.compcall.tbp;
7221 : :
7222 : 3228 : if (tbp->error)
7223 : : return false;
7224 : :
7225 : 3227 : po = extract_compcall_passed_object (e);
7226 : 3227 : if (!po)
7227 : : return false;
7228 : :
7229 : 3227 : if (tbp->nopass || e->value.compcall.ignore_pass)
7230 : : {
7231 : 1077 : gfc_free_expr (po);
7232 : 1077 : return true;
7233 : : }
7234 : :
7235 : 2150 : if (tbp->pass_arg_num <= 0)
7236 : : return false;
7237 : :
7238 : 2149 : e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
7239 : : tbp->pass_arg_num,
7240 : : tbp->pass_arg);
7241 : :
7242 : 2149 : return true;
7243 : : }
7244 : :
7245 : :
7246 : : /* Extract the passed object from a PPC call (a copy of it). */
7247 : :
7248 : : static gfc_expr*
7249 : 85 : extract_ppc_passed_object (gfc_expr *e)
7250 : : {
7251 : 85 : gfc_expr *po;
7252 : 85 : gfc_ref **ref;
7253 : :
7254 : 85 : po = gfc_get_expr ();
7255 : 85 : po->expr_type = EXPR_VARIABLE;
7256 : 85 : po->symtree = e->symtree;
7257 : 85 : po->ref = gfc_copy_ref (e->ref);
7258 : 85 : po->where = e->where;
7259 : :
7260 : : /* Remove PPC reference. */
7261 : 85 : ref = &po->ref;
7262 : 91 : while ((*ref)->next)
7263 : 6 : ref = &(*ref)->next;
7264 : 85 : gfc_free_ref_list (*ref);
7265 : 85 : *ref = NULL;
7266 : :
7267 : 85 : if (!gfc_resolve_expr (po))
7268 : 0 : return NULL;
7269 : :
7270 : : return po;
7271 : : }
7272 : :
7273 : :
7274 : : /* Update the actual arglist of a procedure pointer component to include the
7275 : : passed-object. */
7276 : :
7277 : : static bool
7278 : 411 : update_ppc_arglist (gfc_expr* e)
7279 : : {
7280 : 411 : gfc_expr* po;
7281 : 411 : gfc_component *ppc;
7282 : 411 : gfc_typebound_proc* tb;
7283 : :
7284 : 411 : ppc = gfc_get_proc_ptr_comp (e);
7285 : 411 : if (!ppc)
7286 : : return false;
7287 : :
7288 : 411 : tb = ppc->tb;
7289 : :
7290 : 411 : if (tb->error)
7291 : : return false;
7292 : 409 : else if (tb->nopass)
7293 : : return true;
7294 : :
7295 : 85 : po = extract_ppc_passed_object (e);
7296 : 85 : if (!po)
7297 : : return false;
7298 : :
7299 : : /* F08:R739. */
7300 : 85 : if (po->rank != 0)
7301 : : {
7302 : 0 : gfc_error ("Passed-object at %L must be scalar", &e->where);
7303 : 0 : return false;
7304 : : }
7305 : :
7306 : : /* F08:C611. */
7307 : 85 : if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
7308 : : {
7309 : 1 : gfc_error ("Base object for procedure-pointer component call at %L is of"
7310 : : " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
7311 : 1 : return false;
7312 : : }
7313 : :
7314 : 84 : gcc_assert (tb->pass_arg_num > 0);
7315 : 84 : e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
7316 : : tb->pass_arg_num,
7317 : : tb->pass_arg);
7318 : :
7319 : 84 : return true;
7320 : : }
7321 : :
7322 : :
7323 : : /* Check that the object a TBP is called on is valid, i.e. it must not be
7324 : : of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
7325 : :
7326 : : static bool
7327 : 3239 : check_typebound_baseobject (gfc_expr* e)
7328 : : {
7329 : 3239 : gfc_expr* base;
7330 : 3239 : bool return_value = false;
7331 : :
7332 : 3239 : base = extract_compcall_passed_object (e);
7333 : 3239 : if (!base)
7334 : : return false;
7335 : :
7336 : 3236 : if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
7337 : : {
7338 : 1 : gfc_error ("Error in typebound call at %L", &e->where);
7339 : 1 : goto cleanup;
7340 : : }
7341 : :
7342 : 3235 : if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
7343 : 1 : return false;
7344 : :
7345 : : /* F08:C611. */
7346 : 3234 : if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
7347 : : {
7348 : 3 : gfc_error ("Base object for type-bound procedure call at %L is of"
7349 : : " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
7350 : 3 : goto cleanup;
7351 : : }
7352 : :
7353 : : /* F08:C1230. If the procedure called is NOPASS,
7354 : : the base object must be scalar. */
7355 : 3231 : if (e->value.compcall.tbp->nopass && base->rank != 0)
7356 : : {
7357 : 1 : gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
7358 : : " be scalar", &e->where);
7359 : 1 : goto cleanup;
7360 : : }
7361 : :
7362 : : return_value = true;
7363 : :
7364 : 3235 : cleanup:
7365 : 3235 : gfc_free_expr (base);
7366 : 3235 : return return_value;
7367 : : }
7368 : :
7369 : :
7370 : : /* Resolve a call to a type-bound procedure, either function or subroutine,
7371 : : statically from the data in an EXPR_COMPCALL expression. The adapted
7372 : : arglist and the target-procedure symtree are returned. */
7373 : :
7374 : : static bool
7375 : 3228 : resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
7376 : : gfc_actual_arglist** actual)
7377 : : {
7378 : 3228 : gcc_assert (e->expr_type == EXPR_COMPCALL);
7379 : 3228 : gcc_assert (!e->value.compcall.tbp->is_generic);
7380 : :
7381 : : /* Update the actual arglist for PASS. */
7382 : 3228 : if (!update_compcall_arglist (e))
7383 : : return false;
7384 : :
7385 : 3226 : *actual = e->value.compcall.actual;
7386 : 3226 : *target = e->value.compcall.tbp->u.specific;
7387 : :
7388 : 3226 : gfc_free_ref_list (e->ref);
7389 : 3226 : e->ref = NULL;
7390 : 3226 : e->value.compcall.actual = NULL;
7391 : :
7392 : : /* If we find a deferred typebound procedure, check for derived types
7393 : : that an overriding typebound procedure has not been missed. */
7394 : 3226 : if (e->value.compcall.name
7395 : 3226 : && !e->value.compcall.tbp->non_overridable
7396 : 3210 : && e->value.compcall.base_object
7397 : 753 : && e->value.compcall.base_object->ts.type == BT_DERIVED)
7398 : : {
7399 : 466 : gfc_symtree *st;
7400 : 466 : gfc_symbol *derived;
7401 : :
7402 : : /* Use the derived type of the base_object. */
7403 : 466 : derived = e->value.compcall.base_object->ts.u.derived;
7404 : 466 : st = NULL;
7405 : :
7406 : : /* If necessary, go through the inheritance chain. */
7407 : 1399 : while (!st && derived)
7408 : : {
7409 : : /* Look for the typebound procedure 'name'. */
7410 : 467 : if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
7411 : 466 : st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
7412 : : e->value.compcall.name);
7413 : 467 : if (!st)
7414 : 1 : derived = gfc_get_derived_super_type (derived);
7415 : : }
7416 : :
7417 : : /* Now find the specific name in the derived type namespace. */
7418 : 466 : if (st && st->n.tb && st->n.tb->u.specific)
7419 : 466 : gfc_find_sym_tree (st->n.tb->u.specific->name,
7420 : 466 : derived->ns, 1, &st);
7421 : 466 : if (st)
7422 : 466 : *target = st;
7423 : : }
7424 : :
7425 : 3226 : if (is_illegal_recursion ((*target)->n.sym, gfc_current_ns)
7426 : 3226 : && !e->value.compcall.tbp->deferred)
7427 : 1 : gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
7428 : : " itself recursively. Declare it RECURSIVE or use"
7429 : : " %<-frecursive%>", (*target)->n.sym->name, &e->where);
7430 : :
7431 : : return true;
7432 : : }
7433 : :
7434 : :
7435 : : /* Get the ultimate declared type from an expression. In addition,
7436 : : return the last class/derived type reference and the copy of the
7437 : : reference list. If check_types is set true, derived types are
7438 : : identified as well as class references. */
7439 : : static gfc_symbol*
7440 : 3192 : get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
7441 : : gfc_expr *e, bool check_types)
7442 : : {
7443 : 3192 : gfc_symbol *declared;
7444 : 3192 : gfc_ref *ref;
7445 : :
7446 : 3192 : declared = NULL;
7447 : 3192 : if (class_ref)
7448 : 2797 : *class_ref = NULL;
7449 : 3192 : if (new_ref)
7450 : 2510 : *new_ref = gfc_copy_ref (e->ref);
7451 : :
7452 : 3961 : for (ref = e->ref; ref; ref = ref->next)
7453 : : {
7454 : 769 : if (ref->type != REF_COMPONENT)
7455 : 283 : continue;
7456 : :
7457 : 486 : if ((ref->u.c.component->ts.type == BT_CLASS
7458 : 240 : || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
7459 : 411 : && ref->u.c.component->attr.flavor != FL_PROCEDURE)
7460 : : {
7461 : 337 : declared = ref->u.c.component->ts.u.derived;
7462 : 337 : if (class_ref)
7463 : 319 : *class_ref = ref;
7464 : : }
7465 : : }
7466 : :
7467 : 3192 : if (declared == NULL)
7468 : 2881 : declared = e->symtree->n.sym->ts.u.derived;
7469 : :
7470 : 3192 : return declared;
7471 : : }
7472 : :
7473 : :
7474 : : /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
7475 : : which of the specific bindings (if any) matches the arglist and transform
7476 : : the expression into a call of that binding. */
7477 : :
7478 : : static bool
7479 : 3230 : resolve_typebound_generic_call (gfc_expr* e, const char **name)
7480 : : {
7481 : 3230 : gfc_typebound_proc* genproc;
7482 : 3230 : const char* genname;
7483 : 3230 : gfc_symtree *st;
7484 : 3230 : gfc_symbol *derived;
7485 : :
7486 : 3230 : gcc_assert (e->expr_type == EXPR_COMPCALL);
7487 : 3230 : genname = e->value.compcall.name;
7488 : 3230 : genproc = e->value.compcall.tbp;
7489 : :
7490 : 3230 : if (!genproc->is_generic)
7491 : : return true;
7492 : :
7493 : : /* Try the bindings on this type and in the inheritance hierarchy. */
7494 : 407 : for (; genproc; genproc = genproc->overridden)
7495 : : {
7496 : 405 : gfc_tbp_generic* g;
7497 : :
7498 : 405 : gcc_assert (genproc->is_generic);
7499 : 629 : for (g = genproc->u.generic; g; g = g->next)
7500 : : {
7501 : 619 : gfc_symbol* target;
7502 : 619 : gfc_actual_arglist* args;
7503 : 619 : bool matches;
7504 : :
7505 : 619 : gcc_assert (g->specific);
7506 : :
7507 : 619 : if (g->specific->error)
7508 : 0 : continue;
7509 : :
7510 : 619 : target = g->specific->u.specific->n.sym;
7511 : :
7512 : : /* Get the right arglist by handling PASS/NOPASS. */
7513 : 619 : args = gfc_copy_actual_arglist (e->value.compcall.actual);
7514 : 619 : if (!g->specific->nopass)
7515 : : {
7516 : 533 : gfc_expr* po;
7517 : 533 : po = extract_compcall_passed_object (e);
7518 : 533 : if (!po)
7519 : : {
7520 : 0 : gfc_free_actual_arglist (args);
7521 : 0 : return false;
7522 : : }
7523 : :
7524 : 533 : gcc_assert (g->specific->pass_arg_num > 0);
7525 : 533 : gcc_assert (!g->specific->error);
7526 : 533 : args = update_arglist_pass (args, po, g->specific->pass_arg_num,
7527 : : g->specific->pass_arg);
7528 : : }
7529 : 619 : resolve_actual_arglist (args, target->attr.proc,
7530 : 619 : is_external_proc (target)
7531 : 619 : && gfc_sym_get_dummy_args (target) == NULL);
7532 : :
7533 : : /* Check if this arglist matches the formal. */
7534 : 619 : matches = gfc_arglist_matches_symbol (&args, target);
7535 : :
7536 : : /* Clean up and break out of the loop if we've found it. */
7537 : 619 : gfc_free_actual_arglist (args);
7538 : 619 : if (matches)
7539 : : {
7540 : 395 : e->value.compcall.tbp = g->specific;
7541 : 395 : genname = g->specific_st->name;
7542 : : /* Pass along the name for CLASS methods, where the vtab
7543 : : procedure pointer component has to be referenced. */
7544 : 395 : if (name)
7545 : 159 : *name = genname;
7546 : 395 : goto success;
7547 : : }
7548 : : }
7549 : : }
7550 : :
7551 : : /* Nothing matching found! */
7552 : 2 : gfc_error ("Found no matching specific binding for the call to the GENERIC"
7553 : : " %qs at %L", genname, &e->where);
7554 : 2 : return false;
7555 : :
7556 : 395 : success:
7557 : : /* Make sure that we have the right specific instance for the name. */
7558 : 395 : derived = get_declared_from_expr (NULL, NULL, e, true);
7559 : :
7560 : 395 : st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
7561 : 395 : if (st)
7562 : 395 : e->value.compcall.tbp = st->n.tb;
7563 : :
7564 : : return true;
7565 : : }
7566 : :
7567 : :
7568 : : /* Resolve a call to a type-bound subroutine. */
7569 : :
7570 : : static bool
7571 : 1676 : resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
7572 : : {
7573 : 1676 : gfc_actual_arglist* newactual;
7574 : 1676 : gfc_symtree* target;
7575 : :
7576 : : /* Check that's really a SUBROUTINE. */
7577 : 1676 : if (!c->expr1->value.compcall.tbp->subroutine)
7578 : : {
7579 : 17 : if (!c->expr1->value.compcall.tbp->is_generic
7580 : 15 : && c->expr1->value.compcall.tbp->u.specific
7581 : 15 : && c->expr1->value.compcall.tbp->u.specific->n.sym
7582 : 15 : && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
7583 : 12 : c->expr1->value.compcall.tbp->subroutine = 1;
7584 : : else
7585 : : {
7586 : 5 : gfc_error ("%qs at %L should be a SUBROUTINE",
7587 : : c->expr1->value.compcall.name, &c->loc);
7588 : 5 : return false;
7589 : : }
7590 : : }
7591 : :
7592 : 1671 : if (!check_typebound_baseobject (c->expr1))
7593 : : return false;
7594 : :
7595 : : /* Pass along the name for CLASS methods, where the vtab
7596 : : procedure pointer component has to be referenced. */
7597 : 1664 : if (name)
7598 : 473 : *name = c->expr1->value.compcall.name;
7599 : :
7600 : 1664 : if (!resolve_typebound_generic_call (c->expr1, name))
7601 : : return false;
7602 : :
7603 : : /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
7604 : 1663 : if (overridable)
7605 : 370 : *overridable = !c->expr1->value.compcall.tbp->non_overridable;
7606 : :
7607 : : /* Transform into an ordinary EXEC_CALL for now. */
7608 : :
7609 : 1663 : if (!resolve_typebound_static (c->expr1, &target, &newactual))
7610 : : return false;
7611 : :
7612 : 1661 : c->ext.actual = newactual;
7613 : 1661 : c->symtree = target;
7614 : 1661 : c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
7615 : :
7616 : 1661 : gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
7617 : :
7618 : 1661 : gfc_free_expr (c->expr1);
7619 : 1661 : c->expr1 = gfc_get_expr ();
7620 : 1661 : c->expr1->expr_type = EXPR_FUNCTION;
7621 : 1661 : c->expr1->symtree = target;
7622 : 1661 : c->expr1->where = c->loc;
7623 : :
7624 : 1661 : return resolve_call (c);
7625 : : }
7626 : :
7627 : :
7628 : : /* Resolve a component-call expression. */
7629 : : static bool
7630 : 1587 : resolve_compcall (gfc_expr* e, const char **name)
7631 : : {
7632 : 1587 : gfc_actual_arglist* newactual;
7633 : 1587 : gfc_symtree* target;
7634 : :
7635 : : /* Check that's really a FUNCTION. */
7636 : 1587 : if (!e->value.compcall.tbp->function)
7637 : : {
7638 : 19 : if (e->symtree && e->symtree->n.sym->resolve_symbol_called)
7639 : 5 : gfc_error ("%qs at %L should be a FUNCTION", e->value.compcall.name,
7640 : : &e->where);
7641 : 19 : return false;
7642 : : }
7643 : :
7644 : :
7645 : : /* These must not be assign-calls! */
7646 : 1568 : gcc_assert (!e->value.compcall.assign);
7647 : :
7648 : 1568 : if (!check_typebound_baseobject (e))
7649 : : return false;
7650 : :
7651 : : /* Pass along the name for CLASS methods, where the vtab
7652 : : procedure pointer component has to be referenced. */
7653 : 1566 : if (name)
7654 : 858 : *name = e->value.compcall.name;
7655 : :
7656 : 1566 : if (!resolve_typebound_generic_call (e, name))
7657 : : return false;
7658 : 1565 : gcc_assert (!e->value.compcall.tbp->is_generic);
7659 : :
7660 : : /* Take the rank from the function's symbol. */
7661 : 1565 : if (e->value.compcall.tbp->u.specific->n.sym->as)
7662 : : {
7663 : 149 : e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
7664 : 149 : e->corank = e->value.compcall.tbp->u.specific->n.sym->as->corank;
7665 : : }
7666 : :
7667 : : /* For now, we simply transform it into an EXPR_FUNCTION call with the same
7668 : : arglist to the TBP's binding target. */
7669 : :
7670 : 1565 : if (!resolve_typebound_static (e, &target, &newactual))
7671 : : return false;
7672 : :
7673 : 1565 : e->value.function.actual = newactual;
7674 : 1565 : e->value.function.name = NULL;
7675 : 1565 : e->value.function.esym = target->n.sym;
7676 : 1565 : e->value.function.isym = NULL;
7677 : 1565 : e->symtree = target;
7678 : 1565 : e->ts = target->n.sym->ts;
7679 : 1565 : e->expr_type = EXPR_FUNCTION;
7680 : :
7681 : : /* Resolution is not necessary if this is a class subroutine; this
7682 : : function only has to identify the specific proc. Resolution of
7683 : : the call will be done next in resolve_typebound_call. */
7684 : 1565 : return gfc_resolve_expr (e);
7685 : : }
7686 : :
7687 : :
7688 : : static bool resolve_fl_derived (gfc_symbol *sym);
7689 : :
7690 : :
7691 : : /* Resolve a typebound function, or 'method'. First separate all
7692 : : the non-CLASS references by calling resolve_compcall directly. */
7693 : :
7694 : : static bool
7695 : 1587 : resolve_typebound_function (gfc_expr* e)
7696 : : {
7697 : 1587 : gfc_symbol *declared;
7698 : 1587 : gfc_component *c;
7699 : 1587 : gfc_ref *new_ref;
7700 : 1587 : gfc_ref *class_ref;
7701 : 1587 : gfc_symtree *st;
7702 : 1587 : const char *name;
7703 : 1587 : gfc_typespec ts;
7704 : 1587 : gfc_expr *expr;
7705 : 1587 : bool overridable;
7706 : :
7707 : 1587 : st = e->symtree;
7708 : :
7709 : : /* Deal with typebound operators for CLASS objects. */
7710 : 1587 : expr = e->value.compcall.base_object;
7711 : 1587 : overridable = !e->value.compcall.tbp->non_overridable;
7712 : 1587 : if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
7713 : : {
7714 : : /* Since the typebound operators are generic, we have to ensure
7715 : : that any delays in resolution are corrected and that the vtab
7716 : : is present. */
7717 : 184 : ts = expr->ts;
7718 : 184 : declared = ts.u.derived;
7719 : 184 : c = gfc_find_component (declared, "_vptr", true, true, NULL);
7720 : 184 : if (c->ts.u.derived == NULL)
7721 : 0 : c->ts.u.derived = gfc_find_derived_vtab (declared);
7722 : :
7723 : 184 : if (!resolve_compcall (e, &name))
7724 : : return false;
7725 : :
7726 : : /* Use the generic name if it is there. */
7727 : 184 : name = name ? name : e->value.function.esym->name;
7728 : 184 : e->symtree = expr->symtree;
7729 : 184 : e->ref = gfc_copy_ref (expr->ref);
7730 : 184 : get_declared_from_expr (&class_ref, NULL, e, false);
7731 : :
7732 : : /* Trim away the extraneous references that emerge from nested
7733 : : use of interface.cc (extend_expr). */
7734 : 184 : if (class_ref && class_ref->next)
7735 : : {
7736 : 0 : gfc_free_ref_list (class_ref->next);
7737 : 0 : class_ref->next = NULL;
7738 : : }
7739 : 184 : else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
7740 : : {
7741 : 0 : gfc_free_ref_list (e->ref);
7742 : 0 : e->ref = NULL;
7743 : : }
7744 : :
7745 : 184 : gfc_add_vptr_component (e);
7746 : 184 : gfc_add_component_ref (e, name);
7747 : 184 : e->value.function.esym = NULL;
7748 : 184 : if (expr->expr_type != EXPR_VARIABLE)
7749 : 80 : e->base_expr = expr;
7750 : 184 : return true;
7751 : : }
7752 : :
7753 : 1403 : if (st == NULL)
7754 : 153 : return resolve_compcall (e, NULL);
7755 : :
7756 : 1250 : if (!gfc_resolve_ref (e))
7757 : : return false;
7758 : :
7759 : : /* Get the CLASS declared type. */
7760 : 1250 : declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
7761 : :
7762 : 1250 : if (!resolve_fl_derived (declared))
7763 : : return false;
7764 : :
7765 : : /* Weed out cases of the ultimate component being a derived type. */
7766 : 1250 : if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
7767 : 1166 : || (!class_ref && st->n.sym->ts.type != BT_CLASS))
7768 : : {
7769 : 562 : gfc_free_ref_list (new_ref);
7770 : 562 : return resolve_compcall (e, NULL);
7771 : : }
7772 : :
7773 : 688 : c = gfc_find_component (declared, "_data", true, true, NULL);
7774 : :
7775 : : /* Treat the call as if it is a typebound procedure, in order to roll
7776 : : out the correct name for the specific function. */
7777 : 688 : if (!resolve_compcall (e, &name))
7778 : : {
7779 : 15 : gfc_free_ref_list (new_ref);
7780 : 15 : return false;
7781 : : }
7782 : 673 : ts = e->ts;
7783 : :
7784 : 673 : if (overridable)
7785 : : {
7786 : : /* Convert the expression to a procedure pointer component call. */
7787 : 671 : e->value.function.esym = NULL;
7788 : 671 : e->symtree = st;
7789 : :
7790 : 671 : if (new_ref)
7791 : 124 : e->ref = new_ref;
7792 : :
7793 : : /* '_vptr' points to the vtab, which contains the procedure pointers. */
7794 : 671 : gfc_add_vptr_component (e);
7795 : 671 : gfc_add_component_ref (e, name);
7796 : :
7797 : : /* Recover the typespec for the expression. This is really only
7798 : : necessary for generic procedures, where the additional call
7799 : : to gfc_add_component_ref seems to throw the collection of the
7800 : : correct typespec. */
7801 : 671 : e->ts = ts;
7802 : : }
7803 : 2 : else if (new_ref)
7804 : 0 : gfc_free_ref_list (new_ref);
7805 : :
7806 : : return true;
7807 : : }
7808 : :
7809 : : /* Resolve a typebound subroutine, or 'method'. First separate all
7810 : : the non-CLASS references by calling resolve_typebound_call
7811 : : directly. */
7812 : :
7813 : : static bool
7814 : 1676 : resolve_typebound_subroutine (gfc_code *code)
7815 : : {
7816 : 1676 : gfc_symbol *declared;
7817 : 1676 : gfc_component *c;
7818 : 1676 : gfc_ref *new_ref;
7819 : 1676 : gfc_ref *class_ref;
7820 : 1676 : gfc_symtree *st;
7821 : 1676 : const char *name;
7822 : 1676 : gfc_typespec ts;
7823 : 1676 : gfc_expr *expr;
7824 : 1676 : bool overridable;
7825 : :
7826 : 1676 : st = code->expr1->symtree;
7827 : :
7828 : : /* Deal with typebound operators for CLASS objects. */
7829 : 1676 : expr = code->expr1->value.compcall.base_object;
7830 : 1676 : overridable = !code->expr1->value.compcall.tbp->non_overridable;
7831 : 1676 : if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
7832 : : {
7833 : : /* If the base_object is not a variable, the corresponding actual
7834 : : argument expression must be stored in e->base_expression so
7835 : : that the corresponding tree temporary can be used as the base
7836 : : object in gfc_conv_procedure_call. */
7837 : 103 : if (expr->expr_type != EXPR_VARIABLE)
7838 : : {
7839 : : gfc_actual_arglist *args;
7840 : :
7841 : : args= code->expr1->value.function.actual;
7842 : : for (; args; args = args->next)
7843 : : if (expr == args->expr)
7844 : : expr = args->expr;
7845 : : }
7846 : :
7847 : : /* Since the typebound operators are generic, we have to ensure
7848 : : that any delays in resolution are corrected and that the vtab
7849 : : is present. */
7850 : 103 : declared = expr->ts.u.derived;
7851 : 103 : c = gfc_find_component (declared, "_vptr", true, true, NULL);
7852 : 103 : if (c->ts.u.derived == NULL)
7853 : 0 : c->ts.u.derived = gfc_find_derived_vtab (declared);
7854 : :
7855 : 103 : if (!resolve_typebound_call (code, &name, NULL))
7856 : : return false;
7857 : :
7858 : : /* Use the generic name if it is there. */
7859 : 103 : name = name ? name : code->expr1->value.function.esym->name;
7860 : 103 : code->expr1->symtree = expr->symtree;
7861 : 103 : code->expr1->ref = gfc_copy_ref (expr->ref);
7862 : :
7863 : : /* Trim away the extraneous references that emerge from nested
7864 : : use of interface.cc (extend_expr). */
7865 : 103 : get_declared_from_expr (&class_ref, NULL, code->expr1, false);
7866 : 103 : if (class_ref && class_ref->next)
7867 : : {
7868 : 0 : gfc_free_ref_list (class_ref->next);
7869 : 0 : class_ref->next = NULL;
7870 : : }
7871 : 103 : else if (code->expr1->ref && !class_ref)
7872 : : {
7873 : 12 : gfc_free_ref_list (code->expr1->ref);
7874 : 12 : code->expr1->ref = NULL;
7875 : : }
7876 : :
7877 : : /* Now use the procedure in the vtable. */
7878 : 103 : gfc_add_vptr_component (code->expr1);
7879 : 103 : gfc_add_component_ref (code->expr1, name);
7880 : 103 : code->expr1->value.function.esym = NULL;
7881 : 103 : if (expr->expr_type != EXPR_VARIABLE)
7882 : 0 : code->expr1->base_expr = expr;
7883 : 103 : return true;
7884 : : }
7885 : :
7886 : 1573 : if (st == NULL)
7887 : 313 : return resolve_typebound_call (code, NULL, NULL);
7888 : :
7889 : 1260 : if (!gfc_resolve_ref (code->expr1))
7890 : : return false;
7891 : :
7892 : : /* Get the CLASS declared type. */
7893 : 1260 : get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
7894 : :
7895 : : /* Weed out cases of the ultimate component being a derived type. */
7896 : 1260 : if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
7897 : 1197 : || (!class_ref && st->n.sym->ts.type != BT_CLASS))
7898 : : {
7899 : 885 : gfc_free_ref_list (new_ref);
7900 : 885 : return resolve_typebound_call (code, NULL, NULL);
7901 : : }
7902 : :
7903 : 375 : if (!resolve_typebound_call (code, &name, &overridable))
7904 : : {
7905 : 5 : gfc_free_ref_list (new_ref);
7906 : 5 : return false;
7907 : : }
7908 : 370 : ts = code->expr1->ts;
7909 : :
7910 : 370 : if (overridable)
7911 : : {
7912 : : /* Convert the expression to a procedure pointer component call. */
7913 : 368 : code->expr1->value.function.esym = NULL;
7914 : 368 : code->expr1->symtree = st;
7915 : :
7916 : 368 : if (new_ref)
7917 : 92 : code->expr1->ref = new_ref;
7918 : :
7919 : : /* '_vptr' points to the vtab, which contains the procedure pointers. */
7920 : 368 : gfc_add_vptr_component (code->expr1);
7921 : 368 : gfc_add_component_ref (code->expr1, name);
7922 : :
7923 : : /* Recover the typespec for the expression. This is really only
7924 : : necessary for generic procedures, where the additional call
7925 : : to gfc_add_component_ref seems to throw the collection of the
7926 : : correct typespec. */
7927 : 368 : code->expr1->ts = ts;
7928 : : }
7929 : 2 : else if (new_ref)
7930 : 0 : gfc_free_ref_list (new_ref);
7931 : :
7932 : : return true;
7933 : : }
7934 : :
7935 : :
7936 : : /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
7937 : :
7938 : : static bool
7939 : 123 : resolve_ppc_call (gfc_code* c)
7940 : : {
7941 : 123 : gfc_component *comp;
7942 : :
7943 : 123 : comp = gfc_get_proc_ptr_comp (c->expr1);
7944 : 123 : gcc_assert (comp != NULL);
7945 : :
7946 : 123 : c->resolved_sym = c->expr1->symtree->n.sym;
7947 : 123 : c->expr1->expr_type = EXPR_VARIABLE;
7948 : :
7949 : 123 : if (!comp->attr.subroutine)
7950 : 1 : gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
7951 : :
7952 : 123 : if (!gfc_resolve_ref (c->expr1))
7953 : : return false;
7954 : :
7955 : 123 : if (!update_ppc_arglist (c->expr1))
7956 : : return false;
7957 : :
7958 : 122 : c->ext.actual = c->expr1->value.compcall.actual;
7959 : :
7960 : 122 : if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
7961 : 122 : !(comp->ts.interface
7962 : 93 : && comp->ts.interface->formal)))
7963 : : return false;
7964 : :
7965 : 122 : if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
7966 : : return false;
7967 : :
7968 : 121 : gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
7969 : :
7970 : 121 : return true;
7971 : : }
7972 : :
7973 : :
7974 : : /* Resolve a Function Call to a Procedure Pointer Component (Function). */
7975 : :
7976 : : static bool
7977 : 288 : resolve_expr_ppc (gfc_expr* e)
7978 : : {
7979 : 288 : gfc_component *comp;
7980 : :
7981 : 288 : comp = gfc_get_proc_ptr_comp (e);
7982 : 288 : gcc_assert (comp != NULL);
7983 : :
7984 : : /* Convert to EXPR_FUNCTION. */
7985 : 288 : e->expr_type = EXPR_FUNCTION;
7986 : 288 : e->value.function.isym = NULL;
7987 : 288 : e->value.function.actual = e->value.compcall.actual;
7988 : 288 : e->ts = comp->ts;
7989 : 288 : if (comp->as != NULL)
7990 : : {
7991 : 28 : e->rank = comp->as->rank;
7992 : 28 : e->corank = comp->as->corank;
7993 : : }
7994 : :
7995 : 288 : if (!comp->attr.function)
7996 : 3 : gfc_add_function (&comp->attr, comp->name, &e->where);
7997 : :
7998 : 288 : if (!gfc_resolve_ref (e))
7999 : : return false;
8000 : :
8001 : 288 : if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
8002 : 288 : !(comp->ts.interface
8003 : 287 : && comp->ts.interface->formal)))
8004 : : return false;
8005 : :
8006 : 288 : if (!update_ppc_arglist (e))
8007 : : return false;
8008 : :
8009 : 286 : if (!check_pure_function(e))
8010 : : return false;
8011 : :
8012 : 285 : gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
8013 : :
8014 : 285 : return true;
8015 : : }
8016 : :
8017 : :
8018 : : static bool
8019 : 11757 : gfc_is_expandable_expr (gfc_expr *e)
8020 : : {
8021 : 11757 : gfc_constructor *con;
8022 : :
8023 : 11757 : if (e->expr_type == EXPR_ARRAY)
8024 : : {
8025 : : /* Traverse the constructor looking for variables that are flavor
8026 : : parameter. Parameters must be expanded since they are fully used at
8027 : : compile time. */
8028 : 11757 : con = gfc_constructor_first (e->value.constructor);
8029 : 31296 : for (; con; con = gfc_constructor_next (con))
8030 : : {
8031 : 13437 : if (con->expr->expr_type == EXPR_VARIABLE
8032 : 4974 : && con->expr->symtree
8033 : 4974 : && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
8034 : 4907 : || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
8035 : : return true;
8036 : 8463 : if (con->expr->expr_type == EXPR_ARRAY
8037 : 8463 : && gfc_is_expandable_expr (con->expr))
8038 : : return true;
8039 : : }
8040 : : }
8041 : :
8042 : : return false;
8043 : : }
8044 : :
8045 : :
8046 : : /* Sometimes variables in specification expressions of the result
8047 : : of module procedures in submodules wind up not being the 'real'
8048 : : dummy. Find this, if possible, in the namespace of the first
8049 : : formal argument. */
8050 : :
8051 : : static void
8052 : 3413 : fixup_unique_dummy (gfc_expr *e)
8053 : : {
8054 : 3413 : gfc_symtree *st = NULL;
8055 : 3413 : gfc_symbol *s = NULL;
8056 : :
8057 : 3413 : if (e->symtree->n.sym->ns->proc_name
8058 : 3413 : && e->symtree->n.sym->ns->proc_name->formal)
8059 : 3413 : s = e->symtree->n.sym->ns->proc_name->formal->sym;
8060 : :
8061 : 3413 : if (s != NULL)
8062 : 3413 : st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
8063 : :
8064 : 3413 : if (st != NULL
8065 : 14 : && st->n.sym != NULL
8066 : 14 : && st->n.sym->attr.dummy)
8067 : 14 : e->symtree = st;
8068 : 3413 : }
8069 : :
8070 : :
8071 : : /* Resolve an expression. That is, make sure that types of operands agree
8072 : : with their operators, intrinsic operators are converted to function calls
8073 : : for overloaded types and unresolved function references are resolved. */
8074 : :
8075 : : bool
8076 : 6957122 : gfc_resolve_expr (gfc_expr *e)
8077 : : {
8078 : 6957122 : bool t;
8079 : 6957122 : bool inquiry_save, actual_arg_save, first_actual_arg_save;
8080 : :
8081 : 6957122 : if (e == NULL || e->do_not_resolve_again)
8082 : : return true;
8083 : :
8084 : : /* inquiry_argument only applies to variables. */
8085 : 5087293 : inquiry_save = inquiry_argument;
8086 : 5087293 : actual_arg_save = actual_arg;
8087 : 5087293 : first_actual_arg_save = first_actual_arg;
8088 : :
8089 : 5087293 : if (e->expr_type != EXPR_VARIABLE)
8090 : : {
8091 : 3793704 : inquiry_argument = false;
8092 : 3793704 : actual_arg = false;
8093 : 3793704 : first_actual_arg = false;
8094 : : }
8095 : 1293589 : else if (e->symtree != NULL
8096 : 1293170 : && *e->symtree->name == '@'
8097 : 4120 : && e->symtree->n.sym->attr.dummy)
8098 : : {
8099 : : /* Deal with submodule specification expressions that are not
8100 : : found to be referenced in module.cc(read_cleanup). */
8101 : 3413 : fixup_unique_dummy (e);
8102 : : }
8103 : :
8104 : 5087293 : switch (e->expr_type)
8105 : : {
8106 : 518804 : case EXPR_OP:
8107 : 518804 : t = resolve_operator (e);
8108 : 518804 : break;
8109 : :
8110 : 90 : case EXPR_CONDITIONAL:
8111 : 90 : t = resolve_conditional (e);
8112 : 90 : break;
8113 : :
8114 : 1629864 : case EXPR_FUNCTION:
8115 : 1629864 : case EXPR_VARIABLE:
8116 : :
8117 : 1629864 : if (check_host_association (e))
8118 : 336311 : t = resolve_function (e);
8119 : : else
8120 : 1293553 : t = resolve_variable (e);
8121 : :
8122 : 1629864 : if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
8123 : 6855 : && e->ref->type != REF_SUBSTRING)
8124 : 2143 : gfc_resolve_substring_charlen (e);
8125 : :
8126 : : break;
8127 : :
8128 : 1587 : case EXPR_COMPCALL:
8129 : 1587 : t = resolve_typebound_function (e);
8130 : 1587 : break;
8131 : :
8132 : 507 : case EXPR_SUBSTRING:
8133 : 507 : t = gfc_resolve_ref (e);
8134 : 507 : break;
8135 : :
8136 : : case EXPR_CONSTANT:
8137 : : case EXPR_NULL:
8138 : : t = true;
8139 : : break;
8140 : :
8141 : 288 : case EXPR_PPC:
8142 : 288 : t = resolve_expr_ppc (e);
8143 : 288 : break;
8144 : :
8145 : 67710 : case EXPR_ARRAY:
8146 : 67710 : t = false;
8147 : 67710 : if (!gfc_resolve_ref (e))
8148 : : break;
8149 : :
8150 : 67710 : t = gfc_resolve_array_constructor (e);
8151 : : /* Also try to expand a constructor. */
8152 : 67710 : if (t)
8153 : : {
8154 : 67614 : gfc_expression_rank (e);
8155 : 67614 : if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
8156 : 62607 : gfc_expand_constructor (e, false);
8157 : : }
8158 : :
8159 : : /* This provides the opportunity for the length of constructors with
8160 : : character valued function elements to propagate the string length
8161 : : to the expression. */
8162 : 67614 : if (t && e->ts.type == BT_CHARACTER)
8163 : : {
8164 : : /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
8165 : : here rather then add a duplicate test for it above. */
8166 : 9959 : gfc_expand_constructor (e, false);
8167 : 9959 : t = gfc_resolve_character_array_constructor (e);
8168 : : }
8169 : :
8170 : : break;
8171 : :
8172 : 15989 : case EXPR_STRUCTURE:
8173 : 15989 : t = gfc_resolve_ref (e);
8174 : 15989 : if (!t)
8175 : : break;
8176 : :
8177 : 15989 : t = resolve_structure_cons (e, 0);
8178 : 15989 : if (!t)
8179 : : break;
8180 : :
8181 : 15977 : t = gfc_simplify_expr (e, 0);
8182 : 15977 : break;
8183 : :
8184 : 0 : default:
8185 : 0 : gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
8186 : : }
8187 : :
8188 : 5087293 : if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
8189 : 176304 : fixup_charlen (e);
8190 : :
8191 : 5087293 : inquiry_argument = inquiry_save;
8192 : 5087293 : actual_arg = actual_arg_save;
8193 : 5087293 : first_actual_arg = first_actual_arg_save;
8194 : :
8195 : : /* For some reason, resolving these expressions a second time mangles
8196 : : the typespec of the expression itself. */
8197 : 5087293 : if (t && e->expr_type == EXPR_VARIABLE
8198 : 1290740 : && e->symtree->n.sym->attr.select_rank_temporary
8199 : 3422 : && UNLIMITED_POLY (e->symtree->n.sym))
8200 : 83 : e->do_not_resolve_again = 1;
8201 : :
8202 : 5084824 : if (t && gfc_current_ns->import_state != IMPORT_NOT_SET)
8203 : 6918 : t = check_import_status (e);
8204 : :
8205 : : return t;
8206 : : }
8207 : :
8208 : :
8209 : : /* Resolve an expression from an iterator. They must be scalar and have
8210 : : INTEGER or (optionally) REAL type. */
8211 : :
8212 : : static bool
8213 : 148905 : gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
8214 : : const char *name_msgid)
8215 : : {
8216 : 148905 : if (!gfc_resolve_expr (expr))
8217 : : return false;
8218 : :
8219 : 148900 : if (expr->rank != 0)
8220 : : {
8221 : 0 : gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
8222 : 0 : return false;
8223 : : }
8224 : :
8225 : 148900 : if (expr->ts.type != BT_INTEGER)
8226 : : {
8227 : 274 : if (expr->ts.type == BT_REAL)
8228 : : {
8229 : 274 : if (real_ok)
8230 : 271 : return gfc_notify_std (GFC_STD_F95_DEL,
8231 : : "%s at %L must be integer",
8232 : 271 : _(name_msgid), &expr->where);
8233 : : else
8234 : : {
8235 : 3 : gfc_error ("%s at %L must be INTEGER", _(name_msgid),
8236 : : &expr->where);
8237 : 3 : return false;
8238 : : }
8239 : : }
8240 : : else
8241 : : {
8242 : 0 : gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
8243 : 0 : return false;
8244 : : }
8245 : : }
8246 : : return true;
8247 : : }
8248 : :
8249 : :
8250 : : /* Resolve the expressions in an iterator structure. If REAL_OK is
8251 : : false allow only INTEGER type iterators, otherwise allow REAL types.
8252 : : Set own_scope to true for ac-implied-do and data-implied-do as those
8253 : : have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
8254 : :
8255 : : bool
8256 : 37235 : gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
8257 : : {
8258 : 37235 : if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
8259 : : return false;
8260 : :
8261 : 37231 : if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
8262 : 37231 : _("iterator variable")))
8263 : : return false;
8264 : :
8265 : 37225 : if (!gfc_resolve_iterator_expr (iter->start, real_ok,
8266 : : "Start expression in DO loop"))
8267 : : return false;
8268 : :
8269 : 37224 : if (!gfc_resolve_iterator_expr (iter->end, real_ok,
8270 : : "End expression in DO loop"))
8271 : : return false;
8272 : :
8273 : 37221 : if (!gfc_resolve_iterator_expr (iter->step, real_ok,
8274 : : "Step expression in DO loop"))
8275 : : return false;
8276 : :
8277 : : /* Convert start, end, and step to the same type as var. */
8278 : 37220 : if (iter->start->ts.kind != iter->var->ts.kind
8279 : 36940 : || iter->start->ts.type != iter->var->ts.type)
8280 : 315 : gfc_convert_type (iter->start, &iter->var->ts, 1);
8281 : :
8282 : 37220 : if (iter->end->ts.kind != iter->var->ts.kind
8283 : 36967 : || iter->end->ts.type != iter->var->ts.type)
8284 : 278 : gfc_convert_type (iter->end, &iter->var->ts, 1);
8285 : :
8286 : 37220 : if (iter->step->ts.kind != iter->var->ts.kind
8287 : 36976 : || iter->step->ts.type != iter->var->ts.type)
8288 : 280 : gfc_convert_type (iter->step, &iter->var->ts, 1);
8289 : :
8290 : 37220 : if (iter->step->expr_type == EXPR_CONSTANT)
8291 : : {
8292 : 36098 : if ((iter->step->ts.type == BT_INTEGER
8293 : 36015 : && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
8294 : 72111 : || (iter->step->ts.type == BT_REAL
8295 : 83 : && mpfr_sgn (iter->step->value.real) == 0))
8296 : : {
8297 : 3 : gfc_error ("Step expression in DO loop at %L cannot be zero",
8298 : 3 : &iter->step->where);
8299 : 3 : return false;
8300 : : }
8301 : : }
8302 : :
8303 : 37217 : if (iter->start->expr_type == EXPR_CONSTANT
8304 : 34169 : && iter->end->expr_type == EXPR_CONSTANT
8305 : 26777 : && iter->step->expr_type == EXPR_CONSTANT)
8306 : : {
8307 : 26510 : int sgn, cmp;
8308 : 26510 : if (iter->start->ts.type == BT_INTEGER)
8309 : : {
8310 : 26456 : sgn = mpz_cmp_ui (iter->step->value.integer, 0);
8311 : 26456 : cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
8312 : : }
8313 : : else
8314 : : {
8315 : 54 : sgn = mpfr_sgn (iter->step->value.real);
8316 : 54 : cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
8317 : : }
8318 : 26510 : if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
8319 : 146 : gfc_warning (OPT_Wzerotrip,
8320 : : "DO loop at %L will be executed zero times",
8321 : 146 : &iter->step->where);
8322 : : }
8323 : :
8324 : 37217 : if (iter->end->expr_type == EXPR_CONSTANT
8325 : 27142 : && iter->end->ts.type == BT_INTEGER
8326 : 27088 : && iter->step->expr_type == EXPR_CONSTANT
8327 : 26778 : && iter->step->ts.type == BT_INTEGER
8328 : 26778 : && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
8329 : 26410 : || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
8330 : : {
8331 : 25664 : bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
8332 : 25664 : int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
8333 : :
8334 : 25664 : if (is_step_positive
8335 : 25296 : && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
8336 : 7 : gfc_warning (OPT_Wundefined_do_loop,
8337 : : "DO loop at %L is undefined as it overflows",
8338 : 7 : &iter->step->where);
8339 : : else if (!is_step_positive
8340 : 368 : && mpz_cmp (iter->end->value.integer,
8341 : 368 : gfc_integer_kinds[k].min_int) == 0)
8342 : 7 : gfc_warning (OPT_Wundefined_do_loop,
8343 : : "DO loop at %L is undefined as it underflows",
8344 : 7 : &iter->step->where);
8345 : : }
8346 : :
8347 : : return true;
8348 : : }
8349 : :
8350 : :
8351 : : /* Traversal function for find_forall_index. f == 2 signals that
8352 : : that variable itself is not to be checked - only the references. */
8353 : :
8354 : : static bool
8355 : 41684 : forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
8356 : : {
8357 : 41684 : if (expr->expr_type != EXPR_VARIABLE)
8358 : : return false;
8359 : :
8360 : : /* A scalar assignment */
8361 : 17617 : if (!expr->ref || *f == 1)
8362 : : {
8363 : 11917 : if (expr->symtree->n.sym == sym)
8364 : : return true;
8365 : : else
8366 : : return false;
8367 : : }
8368 : :
8369 : 5700 : if (*f == 2)
8370 : 1730 : *f = 1;
8371 : : return false;
8372 : : }
8373 : :
8374 : :
8375 : : /* Check whether the FORALL index appears in the expression or not.
8376 : : Returns true if SYM is found in EXPR. */
8377 : :
8378 : : bool
8379 : 26375 : find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
8380 : : {
8381 : 26375 : if (gfc_traverse_expr (expr, sym, forall_index, f))
8382 : : return true;
8383 : : else
8384 : : return false;
8385 : : }
8386 : :
8387 : : /* Check compliance with Fortran 2023's C1133 constraint for DO CONCURRENT
8388 : : This constraint specifies rules for variables in locality-specs. */
8389 : :
8390 : : static int
8391 : 589 : do_concur_locality_specs_f2023 (gfc_expr **expr, int *walk_subtrees, void *data)
8392 : : {
8393 : 589 : struct check_default_none_data *dt = (struct check_default_none_data *) data;
8394 : :
8395 : 589 : if ((*expr)->expr_type == EXPR_VARIABLE)
8396 : : {
8397 : 21 : gfc_symbol *sym = (*expr)->symtree->n.sym;
8398 : 21 : for (gfc_expr_list *list = dt->code->ext.concur.locality[LOCALITY_LOCAL];
8399 : 23 : list; list = list->next)
8400 : : {
8401 : 5 : if (list->expr->symtree->n.sym == sym)
8402 : : {
8403 : 3 : gfc_error ("Variable %qs referenced in concurrent-header at %L "
8404 : : "must not appear in LOCAL locality-spec at %L",
8405 : : sym->name, &(*expr)->where, &list->expr->where);
8406 : 3 : *walk_subtrees = 0;
8407 : 3 : return 1;
8408 : : }
8409 : : }
8410 : : }
8411 : :
8412 : 586 : *walk_subtrees = 1;
8413 : 586 : return 0;
8414 : : }
8415 : :
8416 : : static int
8417 : 3761 : check_default_none_expr (gfc_expr **e, int *, void *data)
8418 : : {
8419 : 3761 : struct check_default_none_data *d = (struct check_default_none_data*) data;
8420 : :
8421 : 3761 : if ((*e)->expr_type == EXPR_VARIABLE)
8422 : : {
8423 : 1685 : gfc_symbol *sym = (*e)->symtree->n.sym;
8424 : :
8425 : 1685 : if (d->sym_hash->contains (sym))
8426 : 1262 : sym->mark = 1;
8427 : :
8428 : 423 : else if (d->default_none)
8429 : : {
8430 : 3 : gfc_namespace *ns2 = d->ns;
8431 : 5 : while (ns2)
8432 : : {
8433 : 3 : if (ns2 == sym->ns)
8434 : : break;
8435 : 2 : ns2 = ns2->parent;
8436 : : }
8437 : 3 : if (ns2 != NULL)
8438 : : {
8439 : 1 : gfc_error ("Variable %qs at %L not specified in a locality spec "
8440 : : "of DO CONCURRENT at %L but required due to "
8441 : : "DEFAULT (NONE)",
8442 : 1 : sym->name, &(*e)->where, &d->code->loc);
8443 : 1 : d->sym_hash->add (sym);
8444 : : }
8445 : : }
8446 : : }
8447 : 3761 : return 0;
8448 : : }
8449 : :
8450 : : static void
8451 : 174 : resolve_locality_spec (gfc_code *code, gfc_namespace *ns)
8452 : : {
8453 : 174 : struct check_default_none_data data;
8454 : 174 : data.code = code;
8455 : 174 : data.sym_hash = new hash_set<gfc_symbol *>;
8456 : 174 : data.ns = ns;
8457 : 174 : data.default_none = code->ext.concur.default_none;
8458 : :
8459 : 870 : for (int locality = 0; locality < LOCALITY_NUM; locality++)
8460 : : {
8461 : 696 : const char *name;
8462 : 696 : switch (locality)
8463 : : {
8464 : : case LOCALITY_LOCAL: name = "LOCAL"; break;
8465 : 174 : case LOCALITY_LOCAL_INIT: name = "LOCAL_INIT"; break;
8466 : 174 : case LOCALITY_SHARED: name = "SHARED"; break;
8467 : 174 : case LOCALITY_REDUCE: name = "REDUCE"; break;
8468 : : default: gcc_unreachable ();
8469 : : }
8470 : :
8471 : 1082 : for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
8472 : 386 : list = list->next)
8473 : : {
8474 : 386 : gfc_expr *expr = list->expr;
8475 : :
8476 : 386 : if (locality == LOCALITY_REDUCE
8477 : 72 : && (expr->expr_type == EXPR_FUNCTION
8478 : 48 : || expr->expr_type == EXPR_OP))
8479 : 35 : continue;
8480 : :
8481 : 362 : if (!gfc_resolve_expr (expr))
8482 : 3 : continue;
8483 : :
8484 : 359 : if (expr->expr_type != EXPR_VARIABLE
8485 : 359 : || expr->symtree->n.sym->attr.flavor != FL_VARIABLE
8486 : 359 : || (expr->ref
8487 : 147 : && (expr->ref->type != REF_ARRAY
8488 : 147 : || expr->ref->u.ar.type != AR_FULL
8489 : 143 : || expr->ref->next)))
8490 : : {
8491 : 4 : gfc_error ("Expected variable name in %s locality spec at %L",
8492 : : name, &expr->where);
8493 : 4 : continue;
8494 : : }
8495 : :
8496 : 355 : gfc_symbol *sym = expr->symtree->n.sym;
8497 : :
8498 : 355 : if (data.sym_hash->contains (sym))
8499 : : {
8500 : 4 : gfc_error ("Variable %qs at %L has already been specified in a "
8501 : : "locality-spec", sym->name, &expr->where);
8502 : 4 : continue;
8503 : : }
8504 : :
8505 : 351 : for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
8506 : 702 : iter; iter = iter->next)
8507 : : {
8508 : 351 : if (iter->var->symtree->n.sym == sym)
8509 : : {
8510 : 0 : gfc_error ("Index variable %qs at %L cannot be specified in a "
8511 : : "locality-spec", sym->name, &expr->where);
8512 : 0 : continue;
8513 : : }
8514 : :
8515 : 351 : data.sym_hash->add (iter->var->symtree->n.sym);
8516 : : }
8517 : :
8518 : 351 : if (locality == LOCALITY_LOCAL
8519 : 351 : || locality == LOCALITY_LOCAL_INIT
8520 : 351 : || locality == LOCALITY_REDUCE)
8521 : : {
8522 : 197 : if (sym->attr.optional)
8523 : 3 : gfc_error ("OPTIONAL attribute not permitted for %qs in %s "
8524 : : "locality-spec at %L",
8525 : : sym->name, name, &expr->where);
8526 : :
8527 : 197 : if (sym->attr.dimension
8528 : 66 : && sym->as
8529 : 66 : && sym->as->type == AS_ASSUMED_SIZE)
8530 : 0 : gfc_error ("Assumed-size array not permitted for %qs in %s "
8531 : : "locality-spec at %L",
8532 : : sym->name, name, &expr->where);
8533 : :
8534 : 197 : gfc_check_vardef_context (expr, false, false, false, name);
8535 : : }
8536 : :
8537 : 197 : if (locality == LOCALITY_LOCAL
8538 : : || locality == LOCALITY_LOCAL_INIT)
8539 : : {
8540 : 180 : symbol_attribute attr = gfc_expr_attr (expr);
8541 : :
8542 : 180 : if (attr.allocatable)
8543 : 2 : gfc_error ("ALLOCATABLE attribute not permitted for %qs in %s "
8544 : : "locality-spec at %L",
8545 : : sym->name, name, &expr->where);
8546 : :
8547 : 178 : else if (expr->ts.type == BT_CLASS && attr.dummy && !attr.pointer)
8548 : 2 : gfc_error ("Nonpointer polymorphic dummy argument not permitted"
8549 : : " for %qs in %s locality-spec at %L",
8550 : : sym->name, name, &expr->where);
8551 : :
8552 : 176 : else if (attr.codimension)
8553 : 0 : gfc_error ("Coarray not permitted for %qs in %s locality-spec "
8554 : : "at %L",
8555 : : sym->name, name, &expr->where);
8556 : :
8557 : 176 : else if (expr->ts.type == BT_DERIVED
8558 : 176 : && gfc_is_finalizable (expr->ts.u.derived, NULL))
8559 : 0 : gfc_error ("Finalizable type not permitted for %qs in %s "
8560 : : "locality-spec at %L",
8561 : : sym->name, name, &expr->where);
8562 : :
8563 : 176 : else if (gfc_has_ultimate_allocatable (expr))
8564 : 4 : gfc_error ("Type with ultimate allocatable component not "
8565 : : "permitted for %qs in %s locality-spec at %L",
8566 : : sym->name, name, &expr->where);
8567 : : }
8568 : :
8569 : 171 : else if (locality == LOCALITY_REDUCE)
8570 : : {
8571 : 17 : if (sym->attr.asynchronous)
8572 : 1 : gfc_error ("ASYNCHRONOUS attribute not permitted for %qs in "
8573 : : "REDUCE locality-spec at %L",
8574 : : sym->name, &expr->where);
8575 : 17 : if (sym->attr.volatile_)
8576 : 1 : gfc_error ("VOLATILE attribute not permitted for %qs in REDUCE "
8577 : : "locality-spec at %L", sym->name, &expr->where);
8578 : : }
8579 : :
8580 : 351 : data.sym_hash->add (sym);
8581 : : }
8582 : :
8583 : 696 : if (locality == LOCALITY_LOCAL)
8584 : : {
8585 : 174 : gcc_assert (locality == 0);
8586 : :
8587 : 174 : for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
8588 : 359 : iter; iter = iter->next)
8589 : : {
8590 : 185 : gfc_expr_walker (&iter->start,
8591 : : do_concur_locality_specs_f2023,
8592 : : &data);
8593 : :
8594 : 185 : gfc_expr_walker (&iter->end,
8595 : : do_concur_locality_specs_f2023,
8596 : : &data);
8597 : :
8598 : 185 : gfc_expr_walker (&iter->stride,
8599 : : do_concur_locality_specs_f2023,
8600 : : &data);
8601 : : }
8602 : :
8603 : 174 : if (code->expr1)
8604 : 7 : gfc_expr_walker (&code->expr1,
8605 : : do_concur_locality_specs_f2023,
8606 : : &data);
8607 : : }
8608 : : }
8609 : :
8610 : 174 : gfc_expr *reduce_op = NULL;
8611 : :
8612 : 174 : for (gfc_expr_list *list = code->ext.concur.locality[LOCALITY_REDUCE];
8613 : 222 : list; list = list->next)
8614 : : {
8615 : 48 : gfc_expr *expr = list->expr;
8616 : :
8617 : 48 : if (expr->expr_type != EXPR_VARIABLE)
8618 : : {
8619 : 24 : reduce_op = expr;
8620 : 24 : continue;
8621 : : }
8622 : :
8623 : 24 : if (reduce_op->expr_type == EXPR_OP)
8624 : : {
8625 : 17 : switch (reduce_op->value.op.op)
8626 : : {
8627 : 17 : case INTRINSIC_PLUS:
8628 : 17 : case INTRINSIC_TIMES:
8629 : 17 : if (!gfc_numeric_ts (&expr->ts))
8630 : 3 : gfc_error ("Expected numeric type for %qs in REDUCE at %L, "
8631 : 3 : "got %s", expr->symtree->n.sym->name,
8632 : : &expr->where, gfc_basic_typename (expr->ts.type));
8633 : : break;
8634 : 0 : case INTRINSIC_AND:
8635 : 0 : case INTRINSIC_OR:
8636 : 0 : case INTRINSIC_EQV:
8637 : 0 : case INTRINSIC_NEQV:
8638 : 0 : if (expr->ts.type != BT_LOGICAL)
8639 : 0 : gfc_error ("Expected logical type for %qs in REDUCE at %L, "
8640 : 0 : "got %qs", expr->symtree->n.sym->name,
8641 : : &expr->where, gfc_basic_typename (expr->ts.type));
8642 : : break;
8643 : 0 : default:
8644 : 0 : gcc_unreachable ();
8645 : : }
8646 : : }
8647 : :
8648 : 7 : else if (reduce_op->expr_type == EXPR_FUNCTION)
8649 : : {
8650 : 7 : switch (reduce_op->value.function.isym->id)
8651 : : {
8652 : 6 : case GFC_ISYM_MIN:
8653 : 6 : case GFC_ISYM_MAX:
8654 : 6 : if (expr->ts.type != BT_INTEGER
8655 : : && expr->ts.type != BT_REAL
8656 : : && expr->ts.type != BT_CHARACTER)
8657 : 2 : gfc_error ("Expected INTEGER, REAL or CHARACTER type for %qs "
8658 : : "in REDUCE with MIN/MAX at %L, got %s",
8659 : 2 : expr->symtree->n.sym->name, &expr->where,
8660 : : gfc_basic_typename (expr->ts.type));
8661 : : break;
8662 : 1 : case GFC_ISYM_IAND:
8663 : 1 : case GFC_ISYM_IOR:
8664 : 1 : case GFC_ISYM_IEOR:
8665 : 1 : if (expr->ts.type != BT_INTEGER)
8666 : 1 : gfc_error ("Expected integer type for %qs in REDUCE with "
8667 : : "IAND/IOR/IEOR at %L, got %s",
8668 : 1 : expr->symtree->n.sym->name, &expr->where,
8669 : : gfc_basic_typename (expr->ts.type));
8670 : : break;
8671 : 0 : default:
8672 : 0 : gcc_unreachable ();
8673 : : }
8674 : : }
8675 : :
8676 : : else
8677 : 0 : gcc_unreachable ();
8678 : : }
8679 : :
8680 : 870 : for (int locality = 0; locality < LOCALITY_NUM; locality++)
8681 : : {
8682 : 1082 : for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
8683 : 386 : list = list->next)
8684 : : {
8685 : 386 : if (list->expr->expr_type == EXPR_VARIABLE)
8686 : 362 : list->expr->symtree->n.sym->mark = 0;
8687 : : }
8688 : : }
8689 : :
8690 : 174 : gfc_code_walker (&code->block->next, gfc_dummy_code_callback,
8691 : : check_default_none_expr, &data);
8692 : :
8693 : 870 : for (int locality = 0; locality < LOCALITY_NUM; locality++)
8694 : : {
8695 : 696 : gfc_expr_list **plist = &code->ext.concur.locality[locality];
8696 : 1082 : while (*plist)
8697 : : {
8698 : 386 : gfc_expr *expr = (*plist)->expr;
8699 : 386 : if (expr->expr_type == EXPR_VARIABLE)
8700 : : {
8701 : 362 : gfc_symbol *sym = expr->symtree->n.sym;
8702 : 362 : if (sym->mark == 0)
8703 : : {
8704 : 70 : gfc_warning (OPT_Wunused_variable, "Variable %qs in "
8705 : : "locality-spec at %L is not used",
8706 : : sym->name, &expr->where);
8707 : 70 : gfc_expr_list *tmp = *plist;
8708 : 70 : *plist = (*plist)->next;
8709 : 70 : gfc_free_expr (tmp->expr);
8710 : 70 : free (tmp);
8711 : 70 : continue;
8712 : 70 : }
8713 : : }
8714 : 316 : plist = &((*plist)->next);
8715 : : }
8716 : : }
8717 : 174 : }
8718 : :
8719 : : /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
8720 : : to be a scalar INTEGER variable. The subscripts and stride are scalar
8721 : : INTEGERs, and if stride is a constant it must be nonzero.
8722 : : Furthermore "A subscript or stride in a forall-triplet-spec shall
8723 : : not contain a reference to any index-name in the
8724 : : forall-triplet-spec-list in which it appears." (7.5.4.1) */
8725 : :
8726 : : static void
8727 : 2164 : resolve_forall_iterators (gfc_forall_iterator *it)
8728 : : {
8729 : 2164 : gfc_forall_iterator *iter, *iter2;
8730 : :
8731 : 6237 : for (iter = it; iter; iter = iter->next)
8732 : : {
8733 : 4073 : if (gfc_resolve_expr (iter->var)
8734 : 4073 : && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
8735 : 0 : gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
8736 : : &iter->var->where);
8737 : :
8738 : 4073 : if (gfc_resolve_expr (iter->start)
8739 : 4073 : && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
8740 : 0 : gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
8741 : : &iter->start->where);
8742 : 4073 : if (iter->var->ts.kind != iter->start->ts.kind)
8743 : 1 : gfc_convert_type (iter->start, &iter->var->ts, 1);
8744 : :
8745 : 4073 : if (gfc_resolve_expr (iter->end)
8746 : 4073 : && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
8747 : 0 : gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
8748 : : &iter->end->where);
8749 : 4073 : if (iter->var->ts.kind != iter->end->ts.kind)
8750 : 2 : gfc_convert_type (iter->end, &iter->var->ts, 1);
8751 : :
8752 : 4073 : if (gfc_resolve_expr (iter->stride))
8753 : : {
8754 : 4073 : if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
8755 : 0 : gfc_error ("FORALL stride expression at %L must be a scalar %s",
8756 : : &iter->stride->where, "INTEGER");
8757 : :
8758 : 4073 : if (iter->stride->expr_type == EXPR_CONSTANT
8759 : 4070 : && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
8760 : 1 : gfc_error ("FORALL stride expression at %L cannot be zero",
8761 : : &iter->stride->where);
8762 : : }
8763 : 4073 : if (iter->var->ts.kind != iter->stride->ts.kind)
8764 : 1 : gfc_convert_type (iter->stride, &iter->var->ts, 1);
8765 : : }
8766 : :
8767 : 6237 : for (iter = it; iter; iter = iter->next)
8768 : 10981 : for (iter2 = iter; iter2; iter2 = iter2->next)
8769 : : {
8770 : 6908 : if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
8771 : 6906 : || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
8772 : 13812 : || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
8773 : 6 : gfc_error ("FORALL index %qs may not appear in triplet "
8774 : 6 : "specification at %L", iter->var->symtree->name,
8775 : 6 : &iter2->start->where);
8776 : : }
8777 : 2164 : }
8778 : :
8779 : :
8780 : : /* Given a pointer to a symbol that is a derived type, see if it's
8781 : : inaccessible, i.e. if it's defined in another module and the components are
8782 : : PRIVATE. The search is recursive if necessary. Returns zero if no
8783 : : inaccessible components are found, nonzero otherwise. */
8784 : :
8785 : : static bool
8786 : 1329 : derived_inaccessible (gfc_symbol *sym)
8787 : : {
8788 : 1329 : gfc_component *c;
8789 : :
8790 : 1329 : if (sym->attr.use_assoc && sym->attr.private_comp)
8791 : : return 1;
8792 : :
8793 : 3949 : for (c = sym->components; c; c = c->next)
8794 : : {
8795 : : /* Prevent an infinite loop through this function. */
8796 : 2633 : if (c->ts.type == BT_DERIVED
8797 : 288 : && (c->attr.pointer || c->attr.allocatable)
8798 : 72 : && sym == c->ts.u.derived)
8799 : 72 : continue;
8800 : :
8801 : 2561 : if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
8802 : : return 1;
8803 : : }
8804 : :
8805 : : return 0;
8806 : : }
8807 : :
8808 : :
8809 : : /* Resolve the argument of a deallocate expression. The expression must be
8810 : : a pointer or a full array. */
8811 : :
8812 : : static bool
8813 : 7930 : resolve_deallocate_expr (gfc_expr *e)
8814 : : {
8815 : 7930 : symbol_attribute attr;
8816 : 7930 : int allocatable, pointer;
8817 : 7930 : gfc_ref *ref;
8818 : 7930 : gfc_symbol *sym;
8819 : 7930 : gfc_component *c;
8820 : 7930 : bool unlimited;
8821 : :
8822 : 7930 : if (!gfc_resolve_expr (e))
8823 : : return false;
8824 : :
8825 : 7930 : if (e->expr_type != EXPR_VARIABLE)
8826 : 0 : goto bad;
8827 : :
8828 : 7930 : sym = e->symtree->n.sym;
8829 : 7930 : unlimited = UNLIMITED_POLY(sym);
8830 : :
8831 : 7930 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok && CLASS_DATA (sym))
8832 : : {
8833 : 1502 : allocatable = CLASS_DATA (sym)->attr.allocatable;
8834 : 1502 : pointer = CLASS_DATA (sym)->attr.class_pointer;
8835 : : }
8836 : : else
8837 : : {
8838 : 6428 : allocatable = sym->attr.allocatable;
8839 : 6428 : pointer = sym->attr.pointer;
8840 : : }
8841 : 15878 : for (ref = e->ref; ref; ref = ref->next)
8842 : : {
8843 : 7948 : switch (ref->type)
8844 : : {
8845 : 5918 : case REF_ARRAY:
8846 : 5918 : if (ref->u.ar.type != AR_FULL
8847 : 6106 : && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
8848 : 188 : && ref->u.ar.codimen && gfc_ref_this_image (ref)))
8849 : : allocatable = 0;
8850 : : break;
8851 : :
8852 : 2030 : case REF_COMPONENT:
8853 : 2030 : c = ref->u.c.component;
8854 : 2030 : if (c->ts.type == BT_CLASS)
8855 : : {
8856 : 290 : allocatable = CLASS_DATA (c)->attr.allocatable;
8857 : 290 : pointer = CLASS_DATA (c)->attr.class_pointer;
8858 : : }
8859 : : else
8860 : : {
8861 : 1740 : allocatable = c->attr.allocatable;
8862 : 1740 : pointer = c->attr.pointer;
8863 : : }
8864 : : break;
8865 : :
8866 : : case REF_SUBSTRING:
8867 : : case REF_INQUIRY:
8868 : 489 : allocatable = 0;
8869 : : break;
8870 : : }
8871 : : }
8872 : :
8873 : 7930 : attr = gfc_expr_attr (e);
8874 : :
8875 : 7930 : if (allocatable == 0 && attr.pointer == 0 && !unlimited)
8876 : : {
8877 : 3 : bad:
8878 : 3 : gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
8879 : : &e->where);
8880 : 3 : return false;
8881 : : }
8882 : :
8883 : : /* F2008, C644. */
8884 : 7927 : if (gfc_is_coindexed (e))
8885 : : {
8886 : 1 : gfc_error ("Coindexed allocatable object at %L", &e->where);
8887 : 1 : return false;
8888 : : }
8889 : :
8890 : 7926 : if (pointer
8891 : 10196 : && !gfc_check_vardef_context (e, true, true, false,
8892 : 2270 : _("DEALLOCATE object")))
8893 : : return false;
8894 : 7924 : if (!gfc_check_vardef_context (e, false, true, false,
8895 : 7924 : _("DEALLOCATE object")))
8896 : : return false;
8897 : :
8898 : : return true;
8899 : : }
8900 : :
8901 : :
8902 : : /* Returns true if the expression e contains a reference to the symbol sym. */
8903 : : static bool
8904 : 46593 : sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
8905 : : {
8906 : 46593 : if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
8907 : 2081 : return true;
8908 : :
8909 : : return false;
8910 : : }
8911 : :
8912 : : bool
8913 : 20027 : gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
8914 : : {
8915 : 20027 : return gfc_traverse_expr (e, sym, sym_in_expr, 0);
8916 : : }
8917 : :
8918 : : /* Same as gfc_find_sym_in_expr, but do not descend into length type parameter
8919 : : of character expressions. */
8920 : : static bool
8921 : 20181 : gfc_find_var_in_expr (gfc_symbol *sym, gfc_expr *e)
8922 : : {
8923 : 0 : return gfc_traverse_expr (e, sym, sym_in_expr, -1);
8924 : : }
8925 : :
8926 : :
8927 : : /* Given the expression node e for an allocatable/pointer of derived type to be
8928 : : allocated, get the expression node to be initialized afterwards (needed for
8929 : : derived types with default initializers, and derived types with allocatable
8930 : : components that need nullification.) */
8931 : :
8932 : : gfc_expr *
8933 : 5479 : gfc_expr_to_initialize (gfc_expr *e)
8934 : : {
8935 : 5479 : gfc_expr *result;
8936 : 5479 : gfc_ref *ref;
8937 : 5479 : int i;
8938 : :
8939 : 5479 : result = gfc_copy_expr (e);
8940 : :
8941 : : /* Change the last array reference from AR_ELEMENT to AR_FULL. */
8942 : 10803 : for (ref = result->ref; ref; ref = ref->next)
8943 : 8440 : if (ref->type == REF_ARRAY && ref->next == NULL)
8944 : : {
8945 : 3116 : if (ref->u.ar.dimen == 0
8946 : 64 : && ref->u.ar.as && ref->u.ar.as->corank)
8947 : : return result;
8948 : :
8949 : 3052 : ref->u.ar.type = AR_FULL;
8950 : :
8951 : 6897 : for (i = 0; i < ref->u.ar.dimen; i++)
8952 : 3845 : ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
8953 : :
8954 : : break;
8955 : : }
8956 : :
8957 : 5415 : gfc_free_shape (&result->shape, result->rank);
8958 : :
8959 : : /* Recalculate rank, shape, etc. */
8960 : 5415 : gfc_resolve_expr (result);
8961 : 5415 : return result;
8962 : : }
8963 : :
8964 : :
8965 : : /* If the last ref of an expression is an array ref, return a copy of the
8966 : : expression with that one removed. Otherwise, a copy of the original
8967 : : expression. This is used for allocate-expressions and pointer assignment
8968 : : LHS, where there may be an array specification that needs to be stripped
8969 : : off when using gfc_check_vardef_context. */
8970 : :
8971 : : static gfc_expr*
8972 : 26705 : remove_last_array_ref (gfc_expr* e)
8973 : : {
8974 : 26705 : gfc_expr* e2;
8975 : 26705 : gfc_ref** r;
8976 : :
8977 : 26705 : e2 = gfc_copy_expr (e);
8978 : 34133 : for (r = &e2->ref; *r; r = &(*r)->next)
8979 : 23168 : if ((*r)->type == REF_ARRAY && !(*r)->next)
8980 : : {
8981 : 15740 : gfc_free_ref_list (*r);
8982 : 15740 : *r = NULL;
8983 : 15740 : break;
8984 : : }
8985 : :
8986 : 26705 : return e2;
8987 : : }
8988 : :
8989 : :
8990 : : /* Used in resolve_allocate_expr to check that a allocation-object and
8991 : : a source-expr are conformable. This does not catch all possible
8992 : : cases; in particular a runtime checking is needed. */
8993 : :
8994 : : static bool
8995 : 1835 : conformable_arrays (gfc_expr *e1, gfc_expr *e2)
8996 : : {
8997 : 1835 : gfc_ref *tail;
8998 : 1835 : bool scalar;
8999 : :
9000 : 2554 : for (tail = e2->ref; tail && tail->next; tail = tail->next);
9001 : :
9002 : : /* If MOLD= is present and is not scalar, and the allocate-object has an
9003 : : explicit-shape-spec, the ranks need not agree. This may be unintended,
9004 : : so let's emit a warning if -Wsurprising is given. */
9005 : 1835 : scalar = !tail || tail->type == REF_COMPONENT;
9006 : 1835 : if (e1->mold && e1->rank > 0
9007 : 163 : && (scalar || (tail->type == REF_ARRAY && tail->u.ar.type != AR_FULL)))
9008 : : {
9009 : 26 : if (scalar || (tail->u.ar.as && e1->rank != tail->u.ar.as->rank))
9010 : 15 : gfc_warning (OPT_Wsurprising, "Allocate-object at %L has rank %d "
9011 : : "but MOLD= expression at %L has rank %d",
9012 : 6 : &e2->where, scalar ? 0 : tail->u.ar.as->rank,
9013 : : &e1->where, e1->rank);
9014 : 29 : return true;
9015 : : }
9016 : :
9017 : : /* First compare rank. */
9018 : 1806 : if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
9019 : 2 : || (!tail && e1->rank != e2->rank))
9020 : : {
9021 : 7 : gfc_error ("Source-expr at %L must be scalar or have the "
9022 : : "same rank as the allocate-object at %L",
9023 : : &e1->where, &e2->where);
9024 : 7 : return false;
9025 : : }
9026 : :
9027 : 1799 : if (e1->shape)
9028 : : {
9029 : 1312 : int i;
9030 : 1312 : mpz_t s;
9031 : :
9032 : 1312 : mpz_init (s);
9033 : :
9034 : 3040 : for (i = 0; i < e1->rank; i++)
9035 : : {
9036 : 1318 : if (tail->u.ar.start[i] == NULL)
9037 : : break;
9038 : :
9039 : 416 : if (tail->u.ar.end[i])
9040 : : {
9041 : 54 : mpz_set (s, tail->u.ar.end[i]->value.integer);
9042 : 54 : mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
9043 : 54 : mpz_add_ui (s, s, 1);
9044 : : }
9045 : : else
9046 : : {
9047 : 362 : mpz_set (s, tail->u.ar.start[i]->value.integer);
9048 : : }
9049 : :
9050 : 416 : if (mpz_cmp (e1->shape[i], s) != 0)
9051 : : {
9052 : 0 : gfc_error ("Source-expr at %L and allocate-object at %L must "
9053 : : "have the same shape", &e1->where, &e2->where);
9054 : 0 : mpz_clear (s);
9055 : 0 : return false;
9056 : : }
9057 : : }
9058 : :
9059 : 1312 : mpz_clear (s);
9060 : : }
9061 : :
9062 : : return true;
9063 : : }
9064 : :
9065 : :
9066 : : /* Resolve the expression in an ALLOCATE statement, doing the additional
9067 : : checks to see whether the expression is OK or not. The expression must
9068 : : have a trailing array reference that gives the size of the array. */
9069 : :
9070 : : static bool
9071 : 16692 : resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
9072 : : {
9073 : 16692 : int i, pointer, allocatable, dimension, is_abstract;
9074 : 16692 : int codimension;
9075 : 16692 : bool coindexed;
9076 : 16692 : bool unlimited;
9077 : 16692 : symbol_attribute attr;
9078 : 16692 : gfc_ref *ref, *ref2;
9079 : 16692 : gfc_expr *e2;
9080 : 16692 : gfc_array_ref *ar;
9081 : 16692 : gfc_symbol *sym = NULL;
9082 : 16692 : gfc_alloc *a;
9083 : 16692 : gfc_component *c;
9084 : 16692 : bool t;
9085 : :
9086 : : /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
9087 : : checking of coarrays. */
9088 : 20930 : for (ref = e->ref; ref; ref = ref->next)
9089 : 16846 : if (ref->next == NULL)
9090 : : break;
9091 : :
9092 : 16692 : if (ref && ref->type == REF_ARRAY)
9093 : 11486 : ref->u.ar.in_allocate = true;
9094 : :
9095 : 16692 : if (!gfc_resolve_expr (e))
9096 : 1 : goto failure;
9097 : :
9098 : : /* Make sure the expression is allocatable or a pointer. If it is
9099 : : pointer, the next-to-last reference must be a pointer. */
9100 : :
9101 : 16691 : ref2 = NULL;
9102 : 16691 : if (e->symtree)
9103 : 16691 : sym = e->symtree->n.sym;
9104 : :
9105 : : /* Check whether ultimate component is abstract and CLASS. */
9106 : 33382 : is_abstract = 0;
9107 : :
9108 : : /* Is the allocate-object unlimited polymorphic? */
9109 : 16691 : unlimited = UNLIMITED_POLY(e);
9110 : :
9111 : 16691 : if (e->expr_type != EXPR_VARIABLE)
9112 : : {
9113 : 0 : allocatable = 0;
9114 : 0 : attr = gfc_expr_attr (e);
9115 : 0 : pointer = attr.pointer;
9116 : 0 : dimension = attr.dimension;
9117 : 0 : codimension = attr.codimension;
9118 : : }
9119 : : else
9120 : : {
9121 : 16691 : if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
9122 : : {
9123 : 3313 : allocatable = CLASS_DATA (sym)->attr.allocatable;
9124 : 3313 : pointer = CLASS_DATA (sym)->attr.class_pointer;
9125 : 3313 : dimension = CLASS_DATA (sym)->attr.dimension;
9126 : 3313 : codimension = CLASS_DATA (sym)->attr.codimension;
9127 : 3313 : is_abstract = CLASS_DATA (sym)->attr.abstract;
9128 : : }
9129 : : else
9130 : : {
9131 : 13378 : allocatable = sym->attr.allocatable;
9132 : 13378 : pointer = sym->attr.pointer;
9133 : 13378 : dimension = sym->attr.dimension;
9134 : 13378 : codimension = sym->attr.codimension;
9135 : : }
9136 : :
9137 : 16691 : coindexed = false;
9138 : :
9139 : 33531 : for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
9140 : : {
9141 : 16842 : switch (ref->type)
9142 : : {
9143 : 12778 : case REF_ARRAY:
9144 : 12778 : if (ref->u.ar.codimen > 0)
9145 : : {
9146 : 659 : int n;
9147 : 940 : for (n = ref->u.ar.dimen;
9148 : 940 : n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
9149 : 696 : if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
9150 : : {
9151 : : coindexed = true;
9152 : : break;
9153 : : }
9154 : : }
9155 : :
9156 : 12778 : if (ref->next != NULL)
9157 : 1294 : pointer = 0;
9158 : : break;
9159 : :
9160 : 4064 : case REF_COMPONENT:
9161 : : /* F2008, C644. */
9162 : 4064 : if (coindexed)
9163 : : {
9164 : 2 : gfc_error ("Coindexed allocatable object at %L",
9165 : : &e->where);
9166 : 2 : goto failure;
9167 : : }
9168 : :
9169 : 4062 : c = ref->u.c.component;
9170 : 4062 : if (c->ts.type == BT_CLASS)
9171 : : {
9172 : 977 : allocatable = CLASS_DATA (c)->attr.allocatable;
9173 : 977 : pointer = CLASS_DATA (c)->attr.class_pointer;
9174 : 977 : dimension = CLASS_DATA (c)->attr.dimension;
9175 : 977 : codimension = CLASS_DATA (c)->attr.codimension;
9176 : 977 : is_abstract = CLASS_DATA (c)->attr.abstract;
9177 : : }
9178 : : else
9179 : : {
9180 : 3085 : allocatable = c->attr.allocatable;
9181 : 3085 : pointer = c->attr.pointer;
9182 : 3085 : dimension = c->attr.dimension;
9183 : 3085 : codimension = c->attr.codimension;
9184 : 3085 : is_abstract = c->attr.abstract;
9185 : : }
9186 : : break;
9187 : :
9188 : 0 : case REF_SUBSTRING:
9189 : 0 : case REF_INQUIRY:
9190 : 0 : allocatable = 0;
9191 : 0 : pointer = 0;
9192 : 0 : break;
9193 : : }
9194 : : }
9195 : : }
9196 : :
9197 : : /* Check for F08:C628 (F2018:C932). Each allocate-object shall be a data
9198 : : pointer or an allocatable variable. */
9199 : 16689 : if (allocatable == 0 && pointer == 0)
9200 : : {
9201 : 4 : gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
9202 : : &e->where);
9203 : 4 : goto failure;
9204 : : }
9205 : :
9206 : : /* Some checks for the SOURCE tag. */
9207 : 16685 : if (code->expr3)
9208 : : {
9209 : : /* Check F03:C632: "The source-expr shall be a scalar or have the same
9210 : : rank as allocate-object". This would require the MOLD argument to
9211 : : NULL() as source-expr for subsequent checking. However, even the
9212 : : resulting disassociated pointer or unallocated array has no shape that
9213 : : could be used for SOURCE= or MOLD=. */
9214 : 3722 : if (code->expr3->expr_type == EXPR_NULL)
9215 : : {
9216 : 4 : gfc_error ("The intrinsic NULL cannot be used as source-expr at %L",
9217 : : &code->expr3->where);
9218 : 4 : goto failure;
9219 : : }
9220 : :
9221 : : /* Check F03:C631. */
9222 : 3718 : if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
9223 : : {
9224 : 10 : gfc_error ("Type of entity at %L is type incompatible with "
9225 : 10 : "source-expr at %L", &e->where, &code->expr3->where);
9226 : 10 : goto failure;
9227 : : }
9228 : :
9229 : : /* Check F03:C632 and restriction following Note 6.18. */
9230 : 3708 : if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
9231 : 7 : goto failure;
9232 : :
9233 : : /* Check F03:C633. */
9234 : 3701 : if (code->expr3->ts.kind != e->ts.kind && !unlimited)
9235 : : {
9236 : 1 : gfc_error ("The allocate-object at %L and the source-expr at %L "
9237 : : "shall have the same kind type parameter",
9238 : : &e->where, &code->expr3->where);
9239 : 1 : goto failure;
9240 : : }
9241 : :
9242 : : /* Check F2008, C642. */
9243 : 3700 : if (code->expr3->ts.type == BT_DERIVED
9244 : 3700 : && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
9245 : 1163 : || (code->expr3->ts.u.derived->from_intmod
9246 : : == INTMOD_ISO_FORTRAN_ENV
9247 : 0 : && code->expr3->ts.u.derived->intmod_sym_id
9248 : : == ISOFORTRAN_LOCK_TYPE)))
9249 : : {
9250 : 0 : gfc_error ("The source-expr at %L shall neither be of type "
9251 : : "LOCK_TYPE nor have a LOCK_TYPE component if "
9252 : : "allocate-object at %L is a coarray",
9253 : 0 : &code->expr3->where, &e->where);
9254 : 0 : goto failure;
9255 : : }
9256 : :
9257 : : /* Check F2008:C639: "Corresponding kind type parameters of
9258 : : allocate-object and source-expr shall have the same values." */
9259 : 3700 : if (e->ts.type == BT_CHARACTER
9260 : 758 : && !e->ts.deferred
9261 : 138 : && e->ts.u.cl->length
9262 : 138 : && code->expr3->ts.type == BT_CHARACTER
9263 : 3838 : && !gfc_check_same_strlen (e, code->expr3, "ALLOCATE with "
9264 : : "SOURCE= or MOLD= specifier"))
9265 : 17 : goto failure;
9266 : :
9267 : : /* Check TS18508, C702/C703. */
9268 : 3683 : if (code->expr3->ts.type == BT_DERIVED
9269 : 4846 : && ((codimension && gfc_expr_attr (code->expr3).event_comp)
9270 : 1163 : || (code->expr3->ts.u.derived->from_intmod
9271 : : == INTMOD_ISO_FORTRAN_ENV
9272 : 0 : && code->expr3->ts.u.derived->intmod_sym_id
9273 : : == ISOFORTRAN_EVENT_TYPE)))
9274 : : {
9275 : 0 : gfc_error ("The source-expr at %L shall neither be of type "
9276 : : "EVENT_TYPE nor have a EVENT_TYPE component if "
9277 : : "allocate-object at %L is a coarray",
9278 : 0 : &code->expr3->where, &e->where);
9279 : 0 : goto failure;
9280 : : }
9281 : : }
9282 : :
9283 : : /* Check F08:C629. */
9284 : 16646 : if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
9285 : 153 : && !code->expr3)
9286 : : {
9287 : 2 : gcc_assert (e->ts.type == BT_CLASS);
9288 : 2 : gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
9289 : : "type-spec or source-expr", sym->name, &e->where);
9290 : 2 : goto failure;
9291 : : }
9292 : :
9293 : : /* F2003:C626 (R623) A type-param-value in a type-spec shall be an asterisk
9294 : : if and only if each allocate-object is a dummy argument for which the
9295 : : corresponding type parameter is assumed. */
9296 : 16644 : if (code->ext.alloc.ts.type == BT_CHARACTER
9297 : 489 : && code->ext.alloc.ts.u.cl->length != NULL
9298 : 474 : && e->ts.type == BT_CHARACTER && !e->ts.deferred
9299 : 23 : && e->ts.u.cl->length == NULL
9300 : 2 : && e->symtree->n.sym->attr.dummy)
9301 : : {
9302 : 2 : gfc_error ("The type parameter in ALLOCATE statement with type-spec "
9303 : : "shall be an asterisk as allocate object %qs at %L is a "
9304 : : "dummy argument with assumed type parameter",
9305 : : sym->name, &e->where);
9306 : 2 : goto failure;
9307 : : }
9308 : :
9309 : : /* Check F08:C632. */
9310 : 16642 : if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
9311 : 60 : && !UNLIMITED_POLY (e))
9312 : : {
9313 : 36 : int cmp;
9314 : :
9315 : 36 : if (!e->ts.u.cl->length)
9316 : 15 : goto failure;
9317 : :
9318 : 42 : cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
9319 : 21 : code->ext.alloc.ts.u.cl->length);
9320 : 21 : if (cmp == 1 || cmp == -1 || cmp == -3)
9321 : : {
9322 : 2 : gfc_error ("Allocating %s at %L with type-spec requires the same "
9323 : : "character-length parameter as in the declaration",
9324 : : sym->name, &e->where);
9325 : 2 : goto failure;
9326 : : }
9327 : : }
9328 : :
9329 : : /* In the variable definition context checks, gfc_expr_attr is used
9330 : : on the expression. This is fooled by the array specification
9331 : : present in e, thus we have to eliminate that one temporarily. */
9332 : 16625 : e2 = remove_last_array_ref (e);
9333 : 16625 : t = true;
9334 : 16625 : if (t && pointer)
9335 : 3727 : t = gfc_check_vardef_context (e2, true, true, false,
9336 : 3727 : _("ALLOCATE object"));
9337 : 3727 : if (t)
9338 : 16617 : t = gfc_check_vardef_context (e2, false, true, false,
9339 : 16617 : _("ALLOCATE object"));
9340 : 16625 : gfc_free_expr (e2);
9341 : 16625 : if (!t)
9342 : 11 : goto failure;
9343 : :
9344 : 16614 : code->ext.alloc.expr3_not_explicit = 0;
9345 : 16614 : if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
9346 : 1574 : && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
9347 : : {
9348 : : /* For class arrays, the initialization with SOURCE is done
9349 : : using _copy and trans_call. It is convenient to exploit that
9350 : : when the allocated type is different from the declared type but
9351 : : no SOURCE exists by setting expr3. */
9352 : 284 : code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
9353 : 284 : code->ext.alloc.expr3_not_explicit = 1;
9354 : : }
9355 : 16330 : else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
9356 : 2376 : && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
9357 : 5 : && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
9358 : : {
9359 : : /* We have to zero initialize the integer variable. */
9360 : 1 : code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
9361 : 1 : code->ext.alloc.expr3_not_explicit = 1;
9362 : : }
9363 : :
9364 : 16614 : if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
9365 : : {
9366 : : /* Make sure the vtab symbol is present when
9367 : : the module variables are generated. */
9368 : 2916 : gfc_typespec ts = e->ts;
9369 : 2916 : if (code->expr3)
9370 : 1305 : ts = code->expr3->ts;
9371 : 1611 : else if (code->ext.alloc.ts.type == BT_DERIVED)
9372 : 706 : ts = code->ext.alloc.ts;
9373 : :
9374 : : /* Finding the vtab also publishes the type's symbol. Therefore this
9375 : : statement is necessary. */
9376 : 2916 : gfc_find_derived_vtab (ts.u.derived);
9377 : 2916 : }
9378 : 13698 : else if (unlimited && !UNLIMITED_POLY (code->expr3))
9379 : : {
9380 : : /* Again, make sure the vtab symbol is present when
9381 : : the module variables are generated. */
9382 : 433 : gfc_typespec *ts = NULL;
9383 : 433 : if (code->expr3)
9384 : 347 : ts = &code->expr3->ts;
9385 : : else
9386 : 86 : ts = &code->ext.alloc.ts;
9387 : :
9388 : 433 : gcc_assert (ts);
9389 : :
9390 : : /* Finding the vtab also publishes the type's symbol. Therefore this
9391 : : statement is necessary. */
9392 : 433 : gfc_find_vtab (ts);
9393 : : }
9394 : :
9395 : 16614 : if (dimension == 0 && codimension == 0)
9396 : 5159 : goto success;
9397 : :
9398 : : /* Make sure the last reference node is an array specification. */
9399 : :
9400 : 11455 : if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
9401 : 10293 : || (dimension && ref2->u.ar.dimen == 0))
9402 : : {
9403 : : /* F08:C633. */
9404 : 1162 : if (code->expr3)
9405 : : {
9406 : 1161 : if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
9407 : : "in ALLOCATE statement at %L", &e->where))
9408 : 0 : goto failure;
9409 : 1161 : if (code->expr3->rank != 0)
9410 : 1160 : *array_alloc_wo_spec = true;
9411 : : else
9412 : : {
9413 : 1 : gfc_error ("Array specification or array-valued SOURCE= "
9414 : : "expression required in ALLOCATE statement at %L",
9415 : : &e->where);
9416 : 1 : goto failure;
9417 : : }
9418 : : }
9419 : : else
9420 : : {
9421 : 1 : gfc_error ("Array specification required in ALLOCATE statement "
9422 : : "at %L", &e->where);
9423 : 1 : goto failure;
9424 : : }
9425 : : }
9426 : :
9427 : : /* Make sure that the array section reference makes sense in the
9428 : : context of an ALLOCATE specification. */
9429 : :
9430 : 11453 : ar = &ref2->u.ar;
9431 : :
9432 : 11453 : if (codimension)
9433 : 982 : for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
9434 : : {
9435 : 580 : switch (ar->dimen_type[i])
9436 : : {
9437 : 2 : case DIMEN_THIS_IMAGE:
9438 : 2 : gfc_error ("Coarray specification required in ALLOCATE statement "
9439 : : "at %L", &e->where);
9440 : 2 : goto failure;
9441 : :
9442 : 83 : case DIMEN_RANGE:
9443 : : /* F2018:R937:
9444 : : * allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
9445 : : */
9446 : 83 : if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
9447 : : {
9448 : 8 : gfc_error ("Bad coarray specification in ALLOCATE statement "
9449 : : "at %L", &e->where);
9450 : 8 : goto failure;
9451 : : }
9452 : 75 : else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
9453 : : {
9454 : 2 : gfc_error ("Upper cobound is less than lower cobound at %L",
9455 : 2 : &ar->start[i]->where);
9456 : 2 : goto failure;
9457 : : }
9458 : : break;
9459 : :
9460 : 93 : case DIMEN_ELEMENT:
9461 : 93 : if (ar->start[i]->expr_type == EXPR_CONSTANT)
9462 : : {
9463 : 85 : gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
9464 : 85 : if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
9465 : : {
9466 : 1 : gfc_error ("Upper cobound is less than lower cobound "
9467 : : "of 1 at %L", &ar->start[i]->where);
9468 : 1 : goto failure;
9469 : : }
9470 : : }
9471 : : break;
9472 : :
9473 : : case DIMEN_STAR:
9474 : : break;
9475 : :
9476 : 0 : default:
9477 : 0 : gfc_error ("Bad array specification in ALLOCATE statement at %L",
9478 : : &e->where);
9479 : 0 : goto failure;
9480 : :
9481 : : }
9482 : : }
9483 : 28217 : for (i = 0; i < ar->dimen; i++)
9484 : : {
9485 : 16781 : if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
9486 : 14121 : goto check_symbols;
9487 : :
9488 : 2660 : switch (ar->dimen_type[i])
9489 : : {
9490 : : case DIMEN_ELEMENT:
9491 : : break;
9492 : :
9493 : 2395 : case DIMEN_RANGE:
9494 : 2395 : if (ar->start[i] != NULL
9495 : 2395 : && ar->end[i] != NULL
9496 : 2394 : && ar->stride[i] == NULL)
9497 : : break;
9498 : :
9499 : : /* Fall through. */
9500 : :
9501 : 1 : case DIMEN_UNKNOWN:
9502 : 1 : case DIMEN_VECTOR:
9503 : 1 : case DIMEN_STAR:
9504 : 1 : case DIMEN_THIS_IMAGE:
9505 : 1 : gfc_error ("Bad array specification in ALLOCATE statement at %L",
9506 : : &e->where);
9507 : 1 : goto failure;
9508 : : }
9509 : :
9510 : 2394 : check_symbols:
9511 : 43902 : for (a = code->ext.alloc.list; a; a = a->next)
9512 : : {
9513 : 27125 : sym = a->expr->symtree->n.sym;
9514 : :
9515 : : /* TODO - check derived type components. */
9516 : 27125 : if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
9517 : 8982 : continue;
9518 : :
9519 : 18143 : if ((ar->start[i] != NULL
9520 : 17561 : && gfc_find_var_in_expr (sym, ar->start[i]))
9521 : 35701 : || (ar->end[i] != NULL
9522 : 2620 : && gfc_find_var_in_expr (sym, ar->end[i])))
9523 : : {
9524 : 3 : gfc_error ("%qs must not appear in the array specification at "
9525 : : "%L in the same ALLOCATE statement where it is "
9526 : : "itself allocated", sym->name, &ar->where);
9527 : 3 : goto failure;
9528 : : }
9529 : : }
9530 : : }
9531 : :
9532 : 11600 : for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
9533 : : {
9534 : 729 : if (ar->dimen_type[i] == DIMEN_ELEMENT
9535 : 565 : || ar->dimen_type[i] == DIMEN_RANGE)
9536 : : {
9537 : 164 : if (i == (ar->dimen + ar->codimen - 1))
9538 : : {
9539 : 0 : gfc_error ("Expected %<*%> in coindex specification in ALLOCATE "
9540 : : "statement at %L", &e->where);
9541 : 0 : goto failure;
9542 : : }
9543 : 164 : continue;
9544 : : }
9545 : :
9546 : 401 : if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
9547 : 401 : && ar->stride[i] == NULL)
9548 : : break;
9549 : :
9550 : 0 : gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
9551 : : &e->where);
9552 : 0 : goto failure;
9553 : : }
9554 : :
9555 : 11436 : success:
9556 : : return true;
9557 : :
9558 : : failure:
9559 : : return false;
9560 : : }
9561 : :
9562 : :
9563 : : static void
9564 : 19439 : resolve_allocate_deallocate (gfc_code *code, const char *fcn)
9565 : : {
9566 : 19439 : gfc_expr *stat, *errmsg, *pe, *qe;
9567 : 19439 : gfc_alloc *a, *p, *q;
9568 : :
9569 : 19439 : stat = code->expr1;
9570 : 19439 : errmsg = code->expr2;
9571 : :
9572 : : /* Check the stat variable. */
9573 : 19439 : if (stat)
9574 : : {
9575 : 643 : if (!gfc_check_vardef_context (stat, false, false, false,
9576 : 643 : _("STAT variable")))
9577 : 8 : goto done_stat;
9578 : :
9579 : 635 : if (stat->ts.type != BT_INTEGER
9580 : 626 : || stat->rank > 0)
9581 : 11 : gfc_error ("Stat-variable at %L must be a scalar INTEGER "
9582 : : "variable", &stat->where);
9583 : :
9584 : 635 : if (stat->expr_type == EXPR_CONSTANT || stat->symtree == NULL)
9585 : 0 : goto done_stat;
9586 : :
9587 : : /* F2018:9.7.4: The stat-variable shall not be allocated or deallocated
9588 : : * within the ALLOCATE or DEALLOCATE statement in which it appears ...
9589 : : */
9590 : 1315 : for (p = code->ext.alloc.list; p; p = p->next)
9591 : 687 : if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
9592 : : {
9593 : 9 : gfc_ref *ref1, *ref2;
9594 : 9 : bool found = true;
9595 : :
9596 : 16 : for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
9597 : 7 : ref1 = ref1->next, ref2 = ref2->next)
9598 : : {
9599 : 9 : if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
9600 : 5 : continue;
9601 : 4 : if (ref1->u.c.component->name != ref2->u.c.component->name)
9602 : : {
9603 : : found = false;
9604 : : break;
9605 : : }
9606 : : }
9607 : :
9608 : 9 : if (found)
9609 : : {
9610 : 7 : gfc_error ("Stat-variable at %L shall not be %sd within "
9611 : : "the same %s statement", &stat->where, fcn, fcn);
9612 : 7 : break;
9613 : : }
9614 : : }
9615 : : }
9616 : :
9617 : 18796 : done_stat:
9618 : :
9619 : : /* Check the errmsg variable. */
9620 : 19439 : if (errmsg)
9621 : : {
9622 : 146 : if (!stat)
9623 : 2 : gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
9624 : : &errmsg->where);
9625 : :
9626 : 146 : if (!gfc_check_vardef_context (errmsg, false, false, false,
9627 : 146 : _("ERRMSG variable")))
9628 : 6 : goto done_errmsg;
9629 : :
9630 : : /* F18:R928 alloc-opt is ERRMSG = errmsg-variable
9631 : : F18:R930 errmsg-variable is scalar-default-char-variable
9632 : : F18:R906 default-char-variable is variable
9633 : : F18:C906 default-char-variable shall be default character. */
9634 : 140 : if (errmsg->ts.type != BT_CHARACTER
9635 : 138 : || errmsg->rank > 0
9636 : 137 : || errmsg->ts.kind != gfc_default_character_kind)
9637 : 4 : gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
9638 : : "variable", &errmsg->where);
9639 : :
9640 : 140 : if (errmsg->expr_type == EXPR_CONSTANT || errmsg->symtree == NULL)
9641 : 0 : goto done_errmsg;
9642 : :
9643 : : /* F2018:9.7.5: The errmsg-variable shall not be allocated or deallocated
9644 : : * within the ALLOCATE or DEALLOCATE statement in which it appears ...
9645 : : */
9646 : 278 : for (p = code->ext.alloc.list; p; p = p->next)
9647 : 143 : if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
9648 : : {
9649 : 9 : gfc_ref *ref1, *ref2;
9650 : 9 : bool found = true;
9651 : :
9652 : 16 : for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
9653 : 7 : ref1 = ref1->next, ref2 = ref2->next)
9654 : : {
9655 : 11 : if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
9656 : 4 : continue;
9657 : 7 : if (ref1->u.c.component->name != ref2->u.c.component->name)
9658 : : {
9659 : : found = false;
9660 : : break;
9661 : : }
9662 : : }
9663 : :
9664 : 9 : if (found)
9665 : : {
9666 : 5 : gfc_error ("Errmsg-variable at %L shall not be %sd within "
9667 : : "the same %s statement", &errmsg->where, fcn, fcn);
9668 : 5 : break;
9669 : : }
9670 : : }
9671 : : }
9672 : :
9673 : 19293 : done_errmsg:
9674 : :
9675 : : /* Check that an allocate-object appears only once in the statement. */
9676 : :
9677 : 44061 : for (p = code->ext.alloc.list; p; p = p->next)
9678 : : {
9679 : 24622 : pe = p->expr;
9680 : 33729 : for (q = p->next; q; q = q->next)
9681 : : {
9682 : 9107 : qe = q->expr;
9683 : 9107 : if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
9684 : : {
9685 : : /* This is a potential collision. */
9686 : 2093 : gfc_ref *pr = pe->ref;
9687 : 2093 : gfc_ref *qr = qe->ref;
9688 : :
9689 : : /* Follow the references until
9690 : : a) They start to differ, in which case there is no error;
9691 : : you can deallocate a%b and a%c in a single statement
9692 : : b) Both of them stop, which is an error
9693 : : c) One of them stops, which is also an error. */
9694 : 4517 : while (1)
9695 : : {
9696 : 3305 : if (pr == NULL && qr == NULL)
9697 : : {
9698 : 7 : gfc_error ("Allocate-object at %L also appears at %L",
9699 : : &pe->where, &qe->where);
9700 : 7 : break;
9701 : : }
9702 : 3298 : else if (pr != NULL && qr == NULL)
9703 : : {
9704 : 2 : gfc_error ("Allocate-object at %L is subobject of"
9705 : : " object at %L", &pe->where, &qe->where);
9706 : 2 : break;
9707 : : }
9708 : 3296 : else if (pr == NULL && qr != NULL)
9709 : : {
9710 : 2 : gfc_error ("Allocate-object at %L is subobject of"
9711 : : " object at %L", &qe->where, &pe->where);
9712 : 2 : break;
9713 : : }
9714 : : /* Here, pr != NULL && qr != NULL */
9715 : 3294 : gcc_assert(pr->type == qr->type);
9716 : 3294 : if (pr->type == REF_ARRAY)
9717 : : {
9718 : : /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
9719 : : which are legal. */
9720 : 1065 : gcc_assert (qr->type == REF_ARRAY);
9721 : :
9722 : 1065 : if (pr->next && qr->next)
9723 : : {
9724 : : int i;
9725 : : gfc_array_ref *par = &(pr->u.ar);
9726 : : gfc_array_ref *qar = &(qr->u.ar);
9727 : :
9728 : 1840 : for (i=0; i<par->dimen; i++)
9729 : : {
9730 : 954 : if ((par->start[i] != NULL
9731 : 0 : || qar->start[i] != NULL)
9732 : 1908 : && gfc_dep_compare_expr (par->start[i],
9733 : 954 : qar->start[i]) != 0)
9734 : 168 : goto break_label;
9735 : : }
9736 : : }
9737 : : }
9738 : : else
9739 : : {
9740 : 2229 : if (pr->u.c.component->name != qr->u.c.component->name)
9741 : : break;
9742 : : }
9743 : :
9744 : 1212 : pr = pr->next;
9745 : 1212 : qr = qr->next;
9746 : 1212 : }
9747 : 9107 : break_label:
9748 : : ;
9749 : : }
9750 : : }
9751 : : }
9752 : :
9753 : 19439 : if (strcmp (fcn, "ALLOCATE") == 0)
9754 : : {
9755 : 13693 : bool arr_alloc_wo_spec = false;
9756 : :
9757 : : /* Resolving the expr3 in the loop over all objects to allocate would
9758 : : execute loop invariant code for each loop item. Therefore do it just
9759 : : once here. */
9760 : 13693 : if (code->expr3 && code->expr3->mold
9761 : 343 : && code->expr3->ts.type == BT_DERIVED)
9762 : : {
9763 : : /* Default initialization via MOLD (non-polymorphic). */
9764 : 20 : gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
9765 : 20 : if (rhs != NULL)
9766 : : {
9767 : 7 : gfc_resolve_expr (rhs);
9768 : 7 : gfc_free_expr (code->expr3);
9769 : 7 : code->expr3 = rhs;
9770 : : }
9771 : : }
9772 : 30385 : for (a = code->ext.alloc.list; a; a = a->next)
9773 : 16692 : resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
9774 : :
9775 : 13693 : if (arr_alloc_wo_spec && code->expr3)
9776 : : {
9777 : : /* Mark the allocate to have to take the array specification
9778 : : from the expr3. */
9779 : 1154 : code->ext.alloc.arr_spec_from_expr3 = 1;
9780 : : }
9781 : : }
9782 : : else
9783 : : {
9784 : 13676 : for (a = code->ext.alloc.list; a; a = a->next)
9785 : 7930 : resolve_deallocate_expr (a->expr);
9786 : : }
9787 : 19439 : }
9788 : :
9789 : :
9790 : : /************ SELECT CASE resolution subroutines ************/
9791 : :
9792 : : /* Callback function for our mergesort variant. Determines interval
9793 : : overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
9794 : : op1 > op2. Assumes we're not dealing with the default case.
9795 : : We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
9796 : : There are nine situations to check. */
9797 : :
9798 : : static int
9799 : 1542 : compare_cases (const gfc_case *op1, const gfc_case *op2)
9800 : : {
9801 : 1542 : int retval;
9802 : :
9803 : 1542 : if (op1->low == NULL) /* op1 = (:L) */
9804 : : {
9805 : : /* op2 = (:N), so overlap. */
9806 : 52 : retval = 0;
9807 : : /* op2 = (M:) or (M:N), L < M */
9808 : 52 : if (op2->low != NULL
9809 : 52 : && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
9810 : : retval = -1;
9811 : : }
9812 : 1490 : else if (op1->high == NULL) /* op1 = (K:) */
9813 : : {
9814 : : /* op2 = (M:), so overlap. */
9815 : 10 : retval = 0;
9816 : : /* op2 = (:N) or (M:N), K > N */
9817 : 10 : if (op2->high != NULL
9818 : 10 : && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
9819 : : retval = 1;
9820 : : }
9821 : : else /* op1 = (K:L) */
9822 : : {
9823 : 1480 : if (op2->low == NULL) /* op2 = (:N), K > N */
9824 : 18 : retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
9825 : 18 : ? 1 : 0;
9826 : 1462 : else if (op2->high == NULL) /* op2 = (M:), L < M */
9827 : 14 : retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
9828 : 10 : ? -1 : 0;
9829 : : else /* op2 = (M:N) */
9830 : : {
9831 : 1452 : retval = 0;
9832 : : /* L < M */
9833 : 1452 : if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
9834 : : retval = -1;
9835 : : /* K > N */
9836 : 412 : else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
9837 : 438 : retval = 1;
9838 : : }
9839 : : }
9840 : :
9841 : 1542 : return retval;
9842 : : }
9843 : :
9844 : :
9845 : : /* Merge-sort a double linked case list, detecting overlap in the
9846 : : process. LIST is the head of the double linked case list before it
9847 : : is sorted. Returns the head of the sorted list if we don't see any
9848 : : overlap, or NULL otherwise. */
9849 : :
9850 : : static gfc_case *
9851 : 642 : check_case_overlap (gfc_case *list)
9852 : : {
9853 : 642 : gfc_case *p, *q, *e, *tail;
9854 : 642 : int insize, nmerges, psize, qsize, cmp, overlap_seen;
9855 : :
9856 : : /* If the passed list was empty, return immediately. */
9857 : 642 : if (!list)
9858 : : return NULL;
9859 : :
9860 : : overlap_seen = 0;
9861 : : insize = 1;
9862 : :
9863 : : /* Loop unconditionally. The only exit from this loop is a return
9864 : : statement, when we've finished sorting the case list. */
9865 : 1334 : for (;;)
9866 : : {
9867 : 988 : p = list;
9868 : 988 : list = NULL;
9869 : 988 : tail = NULL;
9870 : :
9871 : : /* Count the number of merges we do in this pass. */
9872 : 988 : nmerges = 0;
9873 : :
9874 : : /* Loop while there exists a merge to be done. */
9875 : 2491 : while (p)
9876 : : {
9877 : 1503 : int i;
9878 : :
9879 : : /* Count this merge. */
9880 : 1503 : nmerges++;
9881 : :
9882 : : /* Cut the list in two pieces by stepping INSIZE places
9883 : : forward in the list, starting from P. */
9884 : 1503 : psize = 0;
9885 : 1503 : q = p;
9886 : 3150 : for (i = 0; i < insize; i++)
9887 : : {
9888 : 2206 : psize++;
9889 : 2206 : q = q->right;
9890 : 2206 : if (!q)
9891 : : break;
9892 : : }
9893 : : qsize = insize;
9894 : :
9895 : : /* Now we have two lists. Merge them! */
9896 : 4918 : while (psize > 0 || (qsize > 0 && q != NULL))
9897 : : {
9898 : : /* See from which the next case to merge comes from. */
9899 : 771 : if (psize == 0)
9900 : : {
9901 : : /* P is empty so the next case must come from Q. */
9902 : 771 : e = q;
9903 : 771 : q = q->right;
9904 : 771 : qsize--;
9905 : : }
9906 : 2644 : else if (qsize == 0 || q == NULL)
9907 : : {
9908 : : /* Q is empty. */
9909 : 1102 : e = p;
9910 : 1102 : p = p->right;
9911 : 1102 : psize--;
9912 : : }
9913 : : else
9914 : : {
9915 : 1542 : cmp = compare_cases (p, q);
9916 : 1542 : if (cmp < 0)
9917 : : {
9918 : : /* The whole case range for P is less than the
9919 : : one for Q. */
9920 : 1100 : e = p;
9921 : 1100 : p = p->right;
9922 : 1100 : psize--;
9923 : : }
9924 : 442 : else if (cmp > 0)
9925 : : {
9926 : : /* The whole case range for Q is greater than
9927 : : the case range for P. */
9928 : 438 : e = q;
9929 : 438 : q = q->right;
9930 : 438 : qsize--;
9931 : : }
9932 : : else
9933 : : {
9934 : : /* The cases overlap, or they are the same
9935 : : element in the list. Either way, we must
9936 : : issue an error and get the next case from P. */
9937 : : /* FIXME: Sort P and Q by line number. */
9938 : 4 : gfc_error ("CASE label at %L overlaps with CASE "
9939 : : "label at %L", &p->where, &q->where);
9940 : 4 : overlap_seen = 1;
9941 : 4 : e = p;
9942 : 4 : p = p->right;
9943 : 4 : psize--;
9944 : : }
9945 : : }
9946 : :
9947 : : /* Add the next element to the merged list. */
9948 : 3415 : if (tail)
9949 : 2427 : tail->right = e;
9950 : : else
9951 : : list = e;
9952 : 3415 : e->left = tail;
9953 : 3415 : tail = e;
9954 : : }
9955 : :
9956 : : /* P has now stepped INSIZE places along, and so has Q. So
9957 : : they're the same. */
9958 : : p = q;
9959 : : }
9960 : 988 : tail->right = NULL;
9961 : :
9962 : : /* If we have done only one merge or none at all, we've
9963 : : finished sorting the cases. */
9964 : 988 : if (nmerges <= 1)
9965 : : {
9966 : 642 : if (!overlap_seen)
9967 : : return list;
9968 : : else
9969 : : return NULL;
9970 : : }
9971 : :
9972 : : /* Otherwise repeat, merging lists twice the size. */
9973 : 346 : insize *= 2;
9974 : 346 : }
9975 : : }
9976 : :
9977 : :
9978 : : /* Check to see if an expression is suitable for use in a CASE statement.
9979 : : Makes sure that all case expressions are scalar constants of the same
9980 : : type. Return false if anything is wrong. */
9981 : :
9982 : : static bool
9983 : 3257 : validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
9984 : : {
9985 : 3257 : if (e == NULL) return true;
9986 : :
9987 : 3164 : if (e->ts.type != case_expr->ts.type)
9988 : : {
9989 : 4 : gfc_error ("Expression in CASE statement at %L must be of type %s",
9990 : : &e->where, gfc_basic_typename (case_expr->ts.type));
9991 : 4 : return false;
9992 : : }
9993 : :
9994 : : /* C805 (R808) For a given case-construct, each case-value shall be of
9995 : : the same type as case-expr. For character type, length differences
9996 : : are allowed, but the kind type parameters shall be the same. */
9997 : :
9998 : 3160 : if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
9999 : : {
10000 : 4 : gfc_error ("Expression in CASE statement at %L must be of kind %d",
10001 : : &e->where, case_expr->ts.kind);
10002 : 4 : return false;
10003 : : }
10004 : :
10005 : : /* Convert the case value kind to that of case expression kind,
10006 : : if needed */
10007 : :
10008 : 3156 : if (e->ts.kind != case_expr->ts.kind)
10009 : 14 : gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
10010 : :
10011 : 3156 : if (e->rank != 0)
10012 : : {
10013 : 0 : gfc_error ("Expression in CASE statement at %L must be scalar",
10014 : : &e->where);
10015 : 0 : return false;
10016 : : }
10017 : :
10018 : : return true;
10019 : : }
10020 : :
10021 : :
10022 : : /* Given a completely parsed select statement, we:
10023 : :
10024 : : - Validate all expressions and code within the SELECT.
10025 : : - Make sure that the selection expression is not of the wrong type.
10026 : : - Make sure that no case ranges overlap.
10027 : : - Eliminate unreachable cases and unreachable code resulting from
10028 : : removing case labels.
10029 : :
10030 : : The standard does allow unreachable cases, e.g. CASE (5:3). But
10031 : : they are a hassle for code generation, and to prevent that, we just
10032 : : cut them out here. This is not necessary for overlapping cases
10033 : : because they are illegal and we never even try to generate code.
10034 : :
10035 : : We have the additional caveat that a SELECT construct could have
10036 : : been a computed GOTO in the source code. Fortunately we can fairly
10037 : : easily work around that here: The case_expr for a "real" SELECT CASE
10038 : : is in code->expr1, but for a computed GOTO it is in code->expr2. All
10039 : : we have to do is make sure that the case_expr is a scalar integer
10040 : : expression. */
10041 : :
10042 : : static void
10043 : 683 : resolve_select (gfc_code *code, bool select_type)
10044 : : {
10045 : 683 : gfc_code *body;
10046 : 683 : gfc_expr *case_expr;
10047 : 683 : gfc_case *cp, *default_case, *tail, *head;
10048 : 683 : int seen_unreachable;
10049 : 683 : int seen_logical;
10050 : 683 : int ncases;
10051 : 683 : bt type;
10052 : 683 : bool t;
10053 : :
10054 : 683 : if (code->expr1 == NULL)
10055 : : {
10056 : : /* This was actually a computed GOTO statement. */
10057 : 5 : case_expr = code->expr2;
10058 : 5 : if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
10059 : 3 : gfc_error ("Selection expression in computed GOTO statement "
10060 : : "at %L must be a scalar integer expression",
10061 : : &case_expr->where);
10062 : :
10063 : : /* Further checking is not necessary because this SELECT was built
10064 : : by the compiler, so it should always be OK. Just move the
10065 : : case_expr from expr2 to expr so that we can handle computed
10066 : : GOTOs as normal SELECTs from here on. */
10067 : 5 : code->expr1 = code->expr2;
10068 : 5 : code->expr2 = NULL;
10069 : 5 : return;
10070 : : }
10071 : :
10072 : 678 : case_expr = code->expr1;
10073 : 678 : type = case_expr->ts.type;
10074 : :
10075 : : /* F08:C830. */
10076 : 678 : if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER
10077 : 6 : && (!flag_unsigned || (flag_unsigned && type != BT_UNSIGNED)))
10078 : :
10079 : : {
10080 : 0 : gfc_error ("Argument of SELECT statement at %L cannot be %s",
10081 : : &case_expr->where, gfc_typename (case_expr));
10082 : :
10083 : : /* Punt. Going on here just produce more garbage error messages. */
10084 : 0 : return;
10085 : : }
10086 : :
10087 : : /* F08:R842. */
10088 : 678 : if (!select_type && case_expr->rank != 0)
10089 : : {
10090 : 1 : gfc_error ("Argument of SELECT statement at %L must be a scalar "
10091 : : "expression", &case_expr->where);
10092 : :
10093 : : /* Punt. */
10094 : 1 : return;
10095 : : }
10096 : :
10097 : : /* Raise a warning if an INTEGER case value exceeds the range of
10098 : : the case-expr. Later, all expressions will be promoted to the
10099 : : largest kind of all case-labels. */
10100 : :
10101 : 677 : if (type == BT_INTEGER)
10102 : 1897 : for (body = code->block; body; body = body->block)
10103 : 2800 : for (cp = body->ext.block.case_list; cp; cp = cp->next)
10104 : : {
10105 : 1436 : if (cp->low
10106 : 1436 : && gfc_check_integer_range (cp->low->value.integer,
10107 : : case_expr->ts.kind) != ARITH_OK)
10108 : 6 : gfc_warning (0, "Expression in CASE statement at %L is "
10109 : 6 : "not in the range of %s", &cp->low->where,
10110 : : gfc_typename (case_expr));
10111 : :
10112 : 1436 : if (cp->high
10113 : 1153 : && cp->low != cp->high
10114 : 1544 : && gfc_check_integer_range (cp->high->value.integer,
10115 : : case_expr->ts.kind) != ARITH_OK)
10116 : 0 : gfc_warning (0, "Expression in CASE statement at %L is "
10117 : 0 : "not in the range of %s", &cp->high->where,
10118 : : gfc_typename (case_expr));
10119 : : }
10120 : :
10121 : : /* PR 19168 has a long discussion concerning a mismatch of the kinds
10122 : : of the SELECT CASE expression and its CASE values. Walk the lists
10123 : : of case values, and if we find a mismatch, promote case_expr to
10124 : : the appropriate kind. */
10125 : :
10126 : 677 : if (type == BT_LOGICAL || type == BT_INTEGER)
10127 : : {
10128 : 2083 : for (body = code->block; body; body = body->block)
10129 : : {
10130 : : /* Walk the case label list. */
10131 : 3061 : for (cp = body->ext.block.case_list; cp; cp = cp->next)
10132 : : {
10133 : : /* Intercept the DEFAULT case. It does not have a kind. */
10134 : 1571 : if (cp->low == NULL && cp->high == NULL)
10135 : 291 : continue;
10136 : :
10137 : : /* Unreachable case ranges are discarded, so ignore. */
10138 : 1235 : if (cp->low != NULL && cp->high != NULL
10139 : 1187 : && cp->low != cp->high
10140 : 1345 : && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
10141 : 33 : continue;
10142 : :
10143 : 1247 : if (cp->low != NULL
10144 : 1247 : && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
10145 : 17 : gfc_convert_type_warn (case_expr, &cp->low->ts, 1, 0);
10146 : :
10147 : 1247 : if (cp->high != NULL
10148 : 1247 : && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
10149 : 4 : gfc_convert_type_warn (case_expr, &cp->high->ts, 1, 0);
10150 : : }
10151 : : }
10152 : : }
10153 : :
10154 : : /* Assume there is no DEFAULT case. */
10155 : 677 : default_case = NULL;
10156 : 677 : head = tail = NULL;
10157 : 677 : ncases = 0;
10158 : 677 : seen_logical = 0;
10159 : :
10160 : 2472 : for (body = code->block; body; body = body->block)
10161 : : {
10162 : : /* Assume the CASE list is OK, and all CASE labels can be matched. */
10163 : 1795 : t = true;
10164 : 1795 : seen_unreachable = 0;
10165 : :
10166 : : /* Walk the case label list, making sure that all case labels
10167 : : are legal. */
10168 : 3777 : for (cp = body->ext.block.case_list; cp; cp = cp->next)
10169 : : {
10170 : : /* Count the number of cases in the whole construct. */
10171 : 1993 : ncases++;
10172 : :
10173 : : /* Intercept the DEFAULT case. */
10174 : 1993 : if (cp->low == NULL && cp->high == NULL)
10175 : : {
10176 : 361 : if (default_case != NULL)
10177 : : {
10178 : 0 : gfc_error ("The DEFAULT CASE at %L cannot be followed "
10179 : : "by a second DEFAULT CASE at %L",
10180 : : &default_case->where, &cp->where);
10181 : 0 : t = false;
10182 : 0 : break;
10183 : : }
10184 : : else
10185 : : {
10186 : 361 : default_case = cp;
10187 : 361 : continue;
10188 : : }
10189 : : }
10190 : :
10191 : : /* Deal with single value cases and case ranges. Errors are
10192 : : issued from the validation function. */
10193 : 1632 : if (!validate_case_label_expr (cp->low, case_expr)
10194 : 1632 : || !validate_case_label_expr (cp->high, case_expr))
10195 : : {
10196 : : t = false;
10197 : : break;
10198 : : }
10199 : :
10200 : 1624 : if (type == BT_LOGICAL
10201 : 78 : && ((cp->low == NULL || cp->high == NULL)
10202 : 76 : || cp->low != cp->high))
10203 : : {
10204 : 2 : gfc_error ("Logical range in CASE statement at %L is not "
10205 : : "allowed",
10206 : 1 : cp->low ? &cp->low->where : &cp->high->where);
10207 : 2 : t = false;
10208 : 2 : break;
10209 : : }
10210 : :
10211 : 76 : if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
10212 : : {
10213 : 76 : int value;
10214 : 76 : value = cp->low->value.logical == 0 ? 2 : 1;
10215 : 76 : if (value & seen_logical)
10216 : : {
10217 : 1 : gfc_error ("Constant logical value in CASE statement "
10218 : : "is repeated at %L",
10219 : : &cp->low->where);
10220 : 1 : t = false;
10221 : 1 : break;
10222 : : }
10223 : 75 : seen_logical |= value;
10224 : : }
10225 : :
10226 : 1577 : if (cp->low != NULL && cp->high != NULL
10227 : 1530 : && cp->low != cp->high
10228 : 1733 : && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
10229 : : {
10230 : 35 : if (warn_surprising)
10231 : 1 : gfc_warning (OPT_Wsurprising,
10232 : : "Range specification at %L can never be matched",
10233 : : &cp->where);
10234 : :
10235 : 35 : cp->unreachable = 1;
10236 : 35 : seen_unreachable = 1;
10237 : : }
10238 : : else
10239 : : {
10240 : : /* If the case range can be matched, it can also overlap with
10241 : : other cases. To make sure it does not, we put it in a
10242 : : double linked list here. We sort that with a merge sort
10243 : : later on to detect any overlapping cases. */
10244 : 1586 : if (!head)
10245 : : {
10246 : 642 : head = tail = cp;
10247 : 642 : head->right = head->left = NULL;
10248 : : }
10249 : : else
10250 : : {
10251 : 944 : tail->right = cp;
10252 : 944 : tail->right->left = tail;
10253 : 944 : tail = tail->right;
10254 : 944 : tail->right = NULL;
10255 : : }
10256 : : }
10257 : : }
10258 : :
10259 : : /* It there was a failure in the previous case label, give up
10260 : : for this case label list. Continue with the next block. */
10261 : 1795 : if (!t)
10262 : 11 : continue;
10263 : :
10264 : : /* See if any case labels that are unreachable have been seen.
10265 : : If so, we eliminate them. This is a bit of a kludge because
10266 : : the case lists for a single case statement (label) is a
10267 : : single forward linked lists. */
10268 : 1784 : if (seen_unreachable)
10269 : : {
10270 : : /* Advance until the first case in the list is reachable. */
10271 : 69 : while (body->ext.block.case_list != NULL
10272 : 69 : && body->ext.block.case_list->unreachable)
10273 : : {
10274 : 34 : gfc_case *n = body->ext.block.case_list;
10275 : 34 : body->ext.block.case_list = body->ext.block.case_list->next;
10276 : 34 : n->next = NULL;
10277 : 34 : gfc_free_case_list (n);
10278 : : }
10279 : :
10280 : : /* Strip all other unreachable cases. */
10281 : 35 : if (body->ext.block.case_list)
10282 : : {
10283 : 2 : for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
10284 : : {
10285 : 1 : if (cp->next->unreachable)
10286 : : {
10287 : 1 : gfc_case *n = cp->next;
10288 : 1 : cp->next = cp->next->next;
10289 : 1 : n->next = NULL;
10290 : 1 : gfc_free_case_list (n);
10291 : : }
10292 : : }
10293 : : }
10294 : : }
10295 : : }
10296 : :
10297 : : /* See if there were overlapping cases. If the check returns NULL,
10298 : : there was overlap. In that case we don't do anything. If head
10299 : : is non-NULL, we prepend the DEFAULT case. The sorted list can
10300 : : then used during code generation for SELECT CASE constructs with
10301 : : a case expression of a CHARACTER type. */
10302 : 677 : if (head)
10303 : : {
10304 : 642 : head = check_case_overlap (head);
10305 : :
10306 : : /* Prepend the default_case if it is there. */
10307 : 642 : if (head != NULL && default_case)
10308 : : {
10309 : 344 : default_case->left = NULL;
10310 : 344 : default_case->right = head;
10311 : 344 : head->left = default_case;
10312 : : }
10313 : : }
10314 : :
10315 : : /* Eliminate dead blocks that may be the result if we've seen
10316 : : unreachable case labels for a block. */
10317 : 2438 : for (body = code; body && body->block; body = body->block)
10318 : : {
10319 : 1761 : if (body->block->ext.block.case_list == NULL)
10320 : : {
10321 : : /* Cut the unreachable block from the code chain. */
10322 : 34 : gfc_code *c = body->block;
10323 : 34 : body->block = c->block;
10324 : :
10325 : : /* Kill the dead block, but not the blocks below it. */
10326 : 34 : c->block = NULL;
10327 : 34 : gfc_free_statements (c);
10328 : : }
10329 : : }
10330 : :
10331 : : /* More than two cases is legal but insane for logical selects.
10332 : : Issue a warning for it. */
10333 : 677 : if (warn_surprising && type == BT_LOGICAL && ncases > 2)
10334 : 0 : gfc_warning (OPT_Wsurprising,
10335 : : "Logical SELECT CASE block at %L has more that two cases",
10336 : : &code->loc);
10337 : : }
10338 : :
10339 : :
10340 : : /* Check if a derived type is extensible. */
10341 : :
10342 : : bool
10343 : 23030 : gfc_type_is_extensible (gfc_symbol *sym)
10344 : : {
10345 : 23030 : return !(sym->attr.is_bind_c || sym->attr.sequence
10346 : 23014 : || (sym->attr.is_class
10347 : 2070 : && sym->components->ts.u.derived->attr.unlimited_polymorphic));
10348 : : }
10349 : :
10350 : :
10351 : : static void
10352 : : resolve_types (gfc_namespace *ns);
10353 : :
10354 : : /* Resolve an associate-name: Resolve target and ensure the type-spec is
10355 : : correct as well as possibly the array-spec. */
10356 : :
10357 : : static void
10358 : 12363 : resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
10359 : : {
10360 : 12363 : gfc_expr* target;
10361 : 12363 : bool parentheses = false;
10362 : :
10363 : 12363 : gcc_assert (sym->assoc);
10364 : 12363 : gcc_assert (sym->attr.flavor == FL_VARIABLE);
10365 : :
10366 : : /* If this is for SELECT TYPE, the target may not yet be set. In that
10367 : : case, return. Resolution will be called later manually again when
10368 : : this is done. */
10369 : 12363 : target = sym->assoc->target;
10370 : 12363 : if (!target)
10371 : : return;
10372 : 7316 : gcc_assert (!sym->assoc->dangling);
10373 : :
10374 : 7316 : if (target->expr_type == EXPR_OP
10375 : 204 : && target->value.op.op == INTRINSIC_PARENTHESES
10376 : 42 : && target->value.op.op1->expr_type == EXPR_VARIABLE)
10377 : : {
10378 : 23 : sym->assoc->target = gfc_copy_expr (target->value.op.op1);
10379 : 23 : gfc_free_expr (target);
10380 : 23 : target = sym->assoc->target;
10381 : 23 : parentheses = true;
10382 : : }
10383 : :
10384 : 7316 : if (resolve_target && !gfc_resolve_expr (target))
10385 : : return;
10386 : :
10387 : 7311 : if (sym->assoc->ar)
10388 : : {
10389 : : int dim;
10390 : : gfc_array_ref *ar = sym->assoc->ar;
10391 : 67 : for (dim = 0; dim < sym->assoc->ar->dimen; dim++)
10392 : : {
10393 : 39 : if (!(ar->start[dim] && gfc_resolve_expr (ar->start[dim])
10394 : 39 : && ar->start[dim]->ts.type == BT_INTEGER)
10395 : 78 : || !(ar->end[dim] && gfc_resolve_expr (ar->end[dim])
10396 : 39 : && ar->end[dim]->ts.type == BT_INTEGER))
10397 : 0 : gfc_error ("(F202y)Missing or invalid bound in ASSOCIATE rank "
10398 : : "remapping of associate name %s at %L",
10399 : : sym->name, &sym->declared_at);
10400 : : }
10401 : : }
10402 : :
10403 : : /* For variable targets, we get some attributes from the target. */
10404 : 7311 : if (target->expr_type == EXPR_VARIABLE)
10405 : : {
10406 : 6448 : gfc_symbol *tsym, *dsym;
10407 : :
10408 : 6448 : gcc_assert (target->symtree);
10409 : 6448 : tsym = target->symtree->n.sym;
10410 : :
10411 : 6448 : if (gfc_expr_attr (target).proc_pointer)
10412 : : {
10413 : 0 : gfc_error ("Associating entity %qs at %L is a procedure pointer",
10414 : : tsym->name, &target->where);
10415 : 0 : return;
10416 : : }
10417 : :
10418 : 74 : if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
10419 : 2 : && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
10420 : 6449 : && dsym->attr.flavor == FL_DERIVED)
10421 : : {
10422 : 1 : gfc_error ("Derived type %qs cannot be used as a variable at %L",
10423 : : tsym->name, &target->where);
10424 : 1 : return;
10425 : : }
10426 : :
10427 : 6447 : if (tsym->attr.flavor == FL_PROCEDURE)
10428 : : {
10429 : 73 : bool is_error = true;
10430 : 73 : if (tsym->attr.function && tsym->result == tsym)
10431 : 141 : for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
10432 : 137 : if (tsym == ns->proc_name)
10433 : : {
10434 : : is_error = false;
10435 : : break;
10436 : : }
10437 : 64 : if (is_error)
10438 : : {
10439 : 13 : gfc_error ("Associating entity %qs at %L is a procedure name",
10440 : : tsym->name, &target->where);
10441 : 13 : return;
10442 : : }
10443 : : }
10444 : :
10445 : 6434 : sym->attr.asynchronous = tsym->attr.asynchronous;
10446 : 6434 : sym->attr.volatile_ = tsym->attr.volatile_;
10447 : :
10448 : 12868 : sym->attr.target = tsym->attr.target
10449 : 6434 : || gfc_expr_attr (target).pointer;
10450 : 6434 : if (is_subref_array (target))
10451 : 378 : sym->attr.subref_array_pointer = 1;
10452 : : }
10453 : 863 : else if (target->ts.type == BT_PROCEDURE)
10454 : : {
10455 : 0 : gfc_error ("Associating selector-expression at %L yields a procedure",
10456 : : &target->where);
10457 : 0 : return;
10458 : : }
10459 : :
10460 : 7297 : if (sym->assoc->inferred_type || IS_INFERRED_TYPE (target))
10461 : : {
10462 : : /* By now, the type of the target has been fixed up. */
10463 : 293 : symbol_attribute attr;
10464 : :
10465 : 293 : if (sym->ts.type == BT_DERIVED
10466 : 166 : && target->ts.type == BT_CLASS
10467 : 31 : && !UNLIMITED_POLY (target))
10468 : : {
10469 : : /* Inferred to be derived type but the target has type class. */
10470 : 31 : sym->ts = CLASS_DATA (target)->ts;
10471 : 31 : if (!sym->as)
10472 : 31 : sym->as = gfc_copy_array_spec (CLASS_DATA (target)->as);
10473 : 31 : attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
10474 : 31 : sym->attr.dimension = target->rank ? 1 : 0;
10475 : 31 : gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
10476 : : target->corank);
10477 : 31 : sym->as = NULL;
10478 : : }
10479 : 262 : else if (target->ts.type == BT_DERIVED
10480 : 135 : && target->symtree && target->symtree->n.sym
10481 : 111 : && target->symtree->n.sym->ts.type == BT_CLASS
10482 : 0 : && IS_INFERRED_TYPE (target)
10483 : 0 : && target->ref && target->ref->next
10484 : 0 : && target->ref->next->type == REF_ARRAY
10485 : 0 : && !target->ref->next->next)
10486 : : {
10487 : : /* A inferred type selector whose symbol has been determined to be
10488 : : a class array but which only has an array reference. Change the
10489 : : associate name and the selector to class type. */
10490 : 0 : sym->ts = target->ts;
10491 : 0 : attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
10492 : 0 : sym->attr.dimension = target->rank ? 1 : 0;
10493 : 0 : gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
10494 : : target->corank);
10495 : 0 : sym->as = NULL;
10496 : 0 : target->ts = sym->ts;
10497 : : }
10498 : 262 : else if ((target->ts.type == BT_DERIVED)
10499 : 127 : || (sym->ts.type == BT_CLASS && target->ts.type == BT_CLASS
10500 : 61 : && CLASS_DATA (target)->as && !CLASS_DATA (sym)->as))
10501 : : /* Confirmed to be either a derived type or misidentified to be a
10502 : : scalar class object, when the selector is a class array. */
10503 : 141 : sym->ts = target->ts;
10504 : : }
10505 : :
10506 : :
10507 : 7297 : if (target->expr_type == EXPR_NULL)
10508 : : {
10509 : 1 : gfc_error ("Selector at %L cannot be NULL()", &target->where);
10510 : 1 : return;
10511 : : }
10512 : 7296 : else if (target->ts.type == BT_UNKNOWN)
10513 : : {
10514 : 2 : gfc_error ("Selector at %L has no type", &target->where);
10515 : 2 : return;
10516 : : }
10517 : :
10518 : : /* Get type if this was not already set. Note that it can be
10519 : : some other type than the target in case this is a SELECT TYPE
10520 : : selector! So we must not update when the type is already there. */
10521 : 7294 : if (sym->ts.type == BT_UNKNOWN)
10522 : 220 : sym->ts = target->ts;
10523 : :
10524 : 7294 : gcc_assert (sym->ts.type != BT_UNKNOWN);
10525 : :
10526 : : /* See if this is a valid association-to-variable. */
10527 : 14588 : sym->assoc->variable = ((target->expr_type == EXPR_VARIABLE
10528 : 6434 : && !parentheses
10529 : 6413 : && !gfc_has_vector_subscript (target))
10530 : 7342 : || gfc_is_ptr_fcn (target));
10531 : :
10532 : : /* Finally resolve if this is an array or not. */
10533 : 7294 : if (target->expr_type == EXPR_FUNCTION && target->rank == 0
10534 : 137 : && (sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED))
10535 : : {
10536 : 92 : gfc_expression_rank (target);
10537 : 92 : if (target->ts.type == BT_DERIVED
10538 : 45 : && !sym->as
10539 : 45 : && target->symtree->n.sym->as)
10540 : : {
10541 : 0 : sym->as = gfc_copy_array_spec (target->symtree->n.sym->as);
10542 : 0 : sym->attr.dimension = 1;
10543 : : }
10544 : 92 : else if (target->ts.type == BT_CLASS
10545 : 47 : && CLASS_DATA (target)->as)
10546 : : {
10547 : 0 : target->rank = CLASS_DATA (target)->as->rank;
10548 : 0 : target->corank = CLASS_DATA (target)->as->corank;
10549 : 0 : if (!(sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
10550 : : {
10551 : 0 : sym->ts = target->ts;
10552 : 0 : sym->attr.dimension = 0;
10553 : : }
10554 : : }
10555 : : }
10556 : :
10557 : :
10558 : 7294 : if (sym->attr.dimension && target->rank == 0)
10559 : : {
10560 : : /* primary.cc makes the assumption that a reference to an associate
10561 : : name followed by a left parenthesis is an array reference. */
10562 : 17 : if (sym->assoc->inferred_type && sym->ts.type != BT_CLASS)
10563 : : {
10564 : 12 : gfc_expression_rank (sym->assoc->target);
10565 : 12 : sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
10566 : 12 : if (!sym->attr.dimension && sym->as)
10567 : 0 : sym->as = NULL;
10568 : : }
10569 : :
10570 : 17 : if (sym->attr.dimension && target->rank == 0)
10571 : : {
10572 : 5 : if (sym->ts.type != BT_CHARACTER)
10573 : 5 : gfc_error ("Associate-name %qs at %L is used as array",
10574 : : sym->name, &sym->declared_at);
10575 : 5 : sym->attr.dimension = 0;
10576 : 5 : return;
10577 : : }
10578 : : }
10579 : :
10580 : : /* We cannot deal with class selectors that need temporaries. */
10581 : 7289 : if (target->ts.type == BT_CLASS
10582 : 7289 : && gfc_ref_needs_temporary_p (target->ref))
10583 : : {
10584 : 1 : gfc_error ("CLASS selector at %L needs a temporary which is not "
10585 : : "yet implemented", &target->where);
10586 : 1 : return;
10587 : : }
10588 : :
10589 : 7288 : if (target->ts.type == BT_CLASS)
10590 : 2698 : gfc_fix_class_refs (target);
10591 : :
10592 : 7288 : if ((target->rank > 0 || target->corank > 0)
10593 : 2623 : && !sym->attr.select_rank_temporary)
10594 : : {
10595 : 2623 : gfc_array_spec *as;
10596 : : /* The rank may be incorrectly guessed at parsing, therefore make sure
10597 : : it is corrected now. */
10598 : 2623 : if (sym->ts.type != BT_CLASS
10599 : 2068 : && (!sym->as || sym->as->corank != target->corank))
10600 : : {
10601 : 135 : if (!sym->as)
10602 : 128 : sym->as = gfc_get_array_spec ();
10603 : 135 : as = sym->as;
10604 : 135 : as->rank = target->rank;
10605 : 135 : as->type = AS_DEFERRED;
10606 : 135 : as->corank = target->corank;
10607 : 135 : sym->attr.dimension = 1;
10608 : 135 : if (as->corank != 0)
10609 : 7 : sym->attr.codimension = 1;
10610 : : }
10611 : 2488 : else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
10612 : 554 : && (!CLASS_DATA (sym)->as
10613 : 554 : || CLASS_DATA (sym)->as->corank != target->corank))
10614 : : {
10615 : 0 : if (!CLASS_DATA (sym)->as)
10616 : 0 : CLASS_DATA (sym)->as = gfc_get_array_spec ();
10617 : 0 : as = CLASS_DATA (sym)->as;
10618 : 0 : as->rank = target->rank;
10619 : 0 : as->type = AS_DEFERRED;
10620 : 0 : as->corank = target->corank;
10621 : 0 : CLASS_DATA (sym)->attr.dimension = 1;
10622 : 0 : if (as->corank != 0)
10623 : 0 : CLASS_DATA (sym)->attr.codimension = 1;
10624 : : }
10625 : : }
10626 : 4665 : else if (!sym->attr.select_rank_temporary)
10627 : : {
10628 : : /* target's rank is 0, but the type of the sym is still array valued,
10629 : : which has to be corrected. */
10630 : 3282 : if (sym->ts.type == BT_CLASS && sym->ts.u.derived
10631 : 675 : && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
10632 : : {
10633 : 24 : gfc_array_spec *as;
10634 : 24 : symbol_attribute attr;
10635 : : /* The associated variable's type is still the array type
10636 : : correct this now. */
10637 : 24 : gfc_typespec *ts = &target->ts;
10638 : 24 : gfc_ref *ref;
10639 : : /* Internal_ref is true, when this is ref'ing only _data and co-ref.
10640 : : */
10641 : 24 : bool internal_ref = true;
10642 : :
10643 : 72 : for (ref = target->ref; ref != NULL; ref = ref->next)
10644 : : {
10645 : 48 : switch (ref->type)
10646 : : {
10647 : 24 : case REF_COMPONENT:
10648 : 24 : ts = &ref->u.c.component->ts;
10649 : 24 : internal_ref
10650 : 24 : = target->ref == ref && ref->next
10651 : 48 : && strncmp ("_data", ref->u.c.component->name, 5) == 0;
10652 : : break;
10653 : 24 : case REF_ARRAY:
10654 : 24 : if (ts->type == BT_CLASS)
10655 : 0 : ts = &ts->u.derived->components->ts;
10656 : 24 : if (internal_ref && ref->u.ar.codimen > 0)
10657 : 0 : for (int i = ref->u.ar.dimen;
10658 : : internal_ref
10659 : 0 : && i < ref->u.ar.dimen + ref->u.ar.codimen;
10660 : : ++i)
10661 : 0 : internal_ref
10662 : 0 : = ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE;
10663 : : break;
10664 : : default:
10665 : : break;
10666 : : }
10667 : : }
10668 : : /* Only rewrite the type of this symbol, when the refs are not the
10669 : : internal ones for class and co-array this-image. */
10670 : 24 : if (!internal_ref)
10671 : : {
10672 : : /* Create a scalar instance of the current class type. Because
10673 : : the rank of a class array goes into its name, the type has to
10674 : : be rebuilt. The alternative of (re-)setting just the
10675 : : attributes and as in the current type, destroys the type also
10676 : : in other places. */
10677 : 0 : as = NULL;
10678 : 0 : sym->ts = *ts;
10679 : 0 : sym->ts.type = BT_CLASS;
10680 : 0 : attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
10681 : 0 : gfc_change_class (&sym->ts, &attr, as, 0, 0);
10682 : 0 : sym->as = NULL;
10683 : : }
10684 : : }
10685 : : }
10686 : :
10687 : : /* Mark this as an associate variable. */
10688 : 7288 : sym->attr.associate_var = 1;
10689 : :
10690 : : /* Fix up the type-spec for CHARACTER types. */
10691 : 7288 : if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
10692 : : {
10693 : 442 : gfc_ref *ref;
10694 : 709 : for (ref = target->ref; ref; ref = ref->next)
10695 : 293 : if (ref->type == REF_SUBSTRING
10696 : 68 : && (ref->u.ss.start == NULL
10697 : 68 : || ref->u.ss.start->expr_type != EXPR_CONSTANT
10698 : 68 : || ref->u.ss.end == NULL
10699 : 48 : || ref->u.ss.end->expr_type != EXPR_CONSTANT))
10700 : : break;
10701 : :
10702 : 442 : if (!sym->ts.u.cl)
10703 : 122 : sym->ts.u.cl = target->ts.u.cl;
10704 : :
10705 : 442 : if (sym->ts.deferred
10706 : 189 : && sym->ts.u.cl == target->ts.u.cl)
10707 : : {
10708 : 110 : sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
10709 : 110 : sym->ts.deferred = 1;
10710 : : }
10711 : :
10712 : 442 : if (!sym->ts.u.cl->length
10713 : 326 : && !sym->ts.deferred
10714 : 137 : && target->expr_type == EXPR_CONSTANT)
10715 : : {
10716 : 30 : sym->ts.u.cl->length =
10717 : 30 : gfc_get_int_expr (gfc_charlen_int_kind, NULL,
10718 : 30 : target->value.character.length);
10719 : : }
10720 : 412 : else if (((!sym->ts.u.cl->length
10721 : 116 : || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
10722 : 302 : && target->expr_type != EXPR_VARIABLE)
10723 : 290 : || ref)
10724 : : {
10725 : 148 : if (!sym->ts.deferred)
10726 : : {
10727 : 44 : sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
10728 : 44 : sym->ts.deferred = 1;
10729 : : }
10730 : :
10731 : : /* This is reset in trans-stmt.cc after the assignment
10732 : : of the target expression to the associate name. */
10733 : 148 : if (ref && sym->as)
10734 : 26 : sym->attr.pointer = 1;
10735 : : else
10736 : 122 : sym->attr.allocatable = 1;
10737 : : }
10738 : : }
10739 : :
10740 : 7288 : if (sym->ts.type == BT_CLASS
10741 : 1375 : && IS_INFERRED_TYPE (target)
10742 : 13 : && target->ts.type == BT_DERIVED
10743 : 0 : && CLASS_DATA (sym)->ts.u.derived == target->ts.u.derived
10744 : 0 : && target->ref && target->ref->next && !target->ref->next->next
10745 : 0 : && target->ref->next->type == REF_ARRAY)
10746 : 0 : target->ts = target->symtree->n.sym->ts;
10747 : :
10748 : : /* If the target is a good class object, so is the associate variable. */
10749 : 7288 : if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
10750 : 688 : sym->attr.class_ok = 1;
10751 : : }
10752 : :
10753 : :
10754 : : /* Ensure that SELECT TYPE expressions have the correct rank and a full
10755 : : array reference, where necessary. The symbols are artificial and so
10756 : : the dimension attribute and arrayspec can also be set. In addition,
10757 : : sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
10758 : : This is corrected here as well.*/
10759 : :
10760 : : static void
10761 : 1644 : fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2, int rank, int corank,
10762 : : gfc_ref *ref)
10763 : : {
10764 : 1644 : gfc_ref *nref = (*expr1)->ref;
10765 : 1644 : gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
10766 : 1644 : gfc_symbol *sym2;
10767 : 1644 : gfc_expr *selector = gfc_copy_expr (expr2);
10768 : :
10769 : 1644 : (*expr1)->rank = rank;
10770 : 1644 : (*expr1)->corank = corank;
10771 : 1644 : if (selector)
10772 : : {
10773 : 309 : gfc_resolve_expr (selector);
10774 : 309 : if (selector->expr_type == EXPR_OP
10775 : 2 : && selector->value.op.op == INTRINSIC_PARENTHESES)
10776 : 2 : sym2 = selector->value.op.op1->symtree->n.sym;
10777 : 307 : else if (selector->expr_type == EXPR_VARIABLE
10778 : 7 : || selector->expr_type == EXPR_FUNCTION)
10779 : 307 : sym2 = selector->symtree->n.sym;
10780 : : else
10781 : 0 : gcc_unreachable ();
10782 : : }
10783 : : else
10784 : : sym2 = NULL;
10785 : :
10786 : 1644 : if (sym1->ts.type == BT_CLASS)
10787 : : {
10788 : 1644 : if ((*expr1)->ts.type != BT_CLASS)
10789 : 13 : (*expr1)->ts = sym1->ts;
10790 : :
10791 : 1644 : CLASS_DATA (sym1)->attr.dimension = rank > 0 ? 1 : 0;
10792 : 1644 : CLASS_DATA (sym1)->attr.codimension = corank > 0 ? 1 : 0;
10793 : 1644 : if (CLASS_DATA (sym1)->as == NULL && sym2)
10794 : 1 : CLASS_DATA (sym1)->as
10795 : 1 : = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
10796 : : }
10797 : : else
10798 : : {
10799 : 0 : sym1->attr.dimension = rank > 0 ? 1 : 0;
10800 : 0 : sym1->attr.codimension = corank > 0 ? 1 : 0;
10801 : 0 : if (sym1->as == NULL && sym2)
10802 : 0 : sym1->as = gfc_copy_array_spec (sym2->as);
10803 : : }
10804 : :
10805 : 2973 : for (; nref; nref = nref->next)
10806 : 2664 : if (nref->next == NULL)
10807 : : break;
10808 : :
10809 : 1644 : if (ref && nref && nref->type != REF_ARRAY)
10810 : 6 : nref->next = gfc_copy_ref (ref);
10811 : 1638 : else if (ref && !nref)
10812 : 300 : (*expr1)->ref = gfc_copy_ref (ref);
10813 : 1338 : else if (ref && nref->u.ar.codimen != corank)
10814 : : {
10815 : 912 : for (int i = nref->u.ar.dimen; i < GFC_MAX_DIMENSIONS; ++i)
10816 : 855 : nref->u.ar.dimen_type[i] = DIMEN_THIS_IMAGE;
10817 : 57 : nref->u.ar.codimen = corank;
10818 : : }
10819 : 1644 : }
10820 : :
10821 : :
10822 : : static gfc_expr *
10823 : 6543 : build_loc_call (gfc_expr *sym_expr)
10824 : : {
10825 : 6543 : gfc_expr *loc_call;
10826 : 6543 : loc_call = gfc_get_expr ();
10827 : 6543 : loc_call->expr_type = EXPR_FUNCTION;
10828 : 6543 : gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
10829 : 6543 : loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
10830 : 6543 : loc_call->symtree->n.sym->attr.intrinsic = 1;
10831 : 6543 : loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
10832 : 6543 : gfc_commit_symbol (loc_call->symtree->n.sym);
10833 : 6543 : loc_call->ts.type = BT_INTEGER;
10834 : 6543 : loc_call->ts.kind = gfc_index_integer_kind;
10835 : 6543 : loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
10836 : 6543 : loc_call->value.function.actual = gfc_get_actual_arglist ();
10837 : 6543 : loc_call->value.function.actual->expr = sym_expr;
10838 : 6543 : loc_call->where = sym_expr->where;
10839 : 6543 : return loc_call;
10840 : : }
10841 : :
10842 : : /* Resolve a SELECT TYPE statement. */
10843 : :
10844 : : static void
10845 : 2922 : resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
10846 : : {
10847 : 2922 : gfc_symbol *selector_type;
10848 : 2922 : gfc_code *body, *new_st, *if_st, *tail;
10849 : 2922 : gfc_code *class_is = NULL, *default_case = NULL;
10850 : 2922 : gfc_case *c;
10851 : 2922 : gfc_symtree *st;
10852 : 2922 : char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
10853 : 2922 : gfc_namespace *ns;
10854 : 2922 : int error = 0;
10855 : 2922 : int rank = 0, corank = 0;
10856 : 2922 : gfc_ref* ref = NULL;
10857 : 2922 : gfc_expr *selector_expr = NULL;
10858 : 2922 : gfc_code *old_code = code;
10859 : :
10860 : 2922 : ns = code->ext.block.ns;
10861 : 2922 : if (code->expr2)
10862 : : {
10863 : : /* Set this, or coarray checks in resolve will fail. */
10864 : 623 : code->expr1->symtree->n.sym->attr.select_type_temporary = 1;
10865 : : }
10866 : 2922 : gfc_resolve (ns);
10867 : :
10868 : : /* Check for F03:C813. */
10869 : 2922 : if (code->expr1->ts.type != BT_CLASS
10870 : 36 : && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
10871 : : {
10872 : 13 : gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
10873 : : "at %L", &code->loc);
10874 : 40 : return;
10875 : : }
10876 : :
10877 : : /* Prevent segfault, when class type is not initialized due to previous
10878 : : error. */
10879 : 2909 : if (!code->expr1->symtree->n.sym->attr.class_ok
10880 : 2907 : || (code->expr1->ts.type == BT_CLASS && !code->expr1->ts.u.derived))
10881 : : return;
10882 : :
10883 : 2902 : if (code->expr2)
10884 : : {
10885 : 614 : gfc_ref *ref2 = NULL;
10886 : 1440 : for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
10887 : 826 : if (ref->type == REF_COMPONENT
10888 : 425 : && ref->u.c.component->ts.type == BT_CLASS)
10889 : 826 : ref2 = ref;
10890 : :
10891 : 614 : if (ref2)
10892 : : {
10893 : 333 : if (code->expr1->symtree->n.sym->attr.untyped)
10894 : 1 : code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
10895 : 333 : selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
10896 : : }
10897 : : else
10898 : : {
10899 : 281 : if (code->expr1->symtree->n.sym->attr.untyped)
10900 : 28 : code->expr1->symtree->n.sym->ts = code->expr2->ts;
10901 : : /* Sometimes the selector expression is given the typespec of the
10902 : : '_data' field, which is logical enough but inappropriate here. */
10903 : 281 : if (code->expr2->ts.type == BT_DERIVED
10904 : 80 : && code->expr2->symtree
10905 : 80 : && code->expr2->symtree->n.sym->ts.type == BT_CLASS)
10906 : 80 : code->expr2->ts = code->expr2->symtree->n.sym->ts;
10907 : 281 : selector_type = CLASS_DATA (code->expr2)
10908 : : ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
10909 : : }
10910 : :
10911 : 614 : if (code->expr1->ts.type == BT_CLASS && CLASS_DATA (code->expr1)->as)
10912 : : {
10913 : 295 : CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
10914 : 295 : CLASS_DATA (code->expr1)->as->corank = code->expr2->corank;
10915 : 295 : CLASS_DATA (code->expr1)->as->cotype = AS_DEFERRED;
10916 : : }
10917 : :
10918 : : /* F2008: C803 The selector expression must not be coindexed. */
10919 : 614 : if (gfc_is_coindexed (code->expr2))
10920 : : {
10921 : 3 : gfc_error ("Selector at %L must not be coindexed",
10922 : 3 : &code->expr2->where);
10923 : 3 : return;
10924 : : }
10925 : :
10926 : : }
10927 : : else
10928 : : {
10929 : 2288 : selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
10930 : :
10931 : 2288 : if (gfc_is_coindexed (code->expr1))
10932 : : {
10933 : 0 : gfc_error ("Selector at %L must not be coindexed",
10934 : 0 : &code->expr1->where);
10935 : 0 : return;
10936 : : }
10937 : : }
10938 : :
10939 : : /* Loop over TYPE IS / CLASS IS cases. */
10940 : 8137 : for (body = code->block; body; body = body->block)
10941 : : {
10942 : 5239 : c = body->ext.block.case_list;
10943 : :
10944 : 5239 : if (!error)
10945 : : {
10946 : : /* Check for repeated cases. */
10947 : 8175 : for (tail = code->block; tail; tail = tail->block)
10948 : : {
10949 : 8175 : gfc_case *d = tail->ext.block.case_list;
10950 : 8175 : if (tail == body)
10951 : : break;
10952 : :
10953 : 2944 : if (c->ts.type == d->ts.type
10954 : 516 : && ((c->ts.type == BT_DERIVED
10955 : 418 : && c->ts.u.derived && d->ts.u.derived
10956 : 418 : && !strcmp (c->ts.u.derived->name,
10957 : : d->ts.u.derived->name))
10958 : 515 : || c->ts.type == BT_UNKNOWN
10959 : 515 : || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10960 : 55 : && c->ts.kind == d->ts.kind)))
10961 : : {
10962 : 1 : gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
10963 : : &c->where, &d->where);
10964 : 1 : return;
10965 : : }
10966 : : }
10967 : : }
10968 : :
10969 : : /* Check F03:C815. */
10970 : 3317 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10971 : 2244 : && selector_type
10972 : 2244 : && !selector_type->attr.unlimited_polymorphic
10973 : 7161 : && !gfc_type_is_extensible (c->ts.u.derived))
10974 : : {
10975 : 1 : gfc_error ("Derived type %qs at %L must be extensible",
10976 : 1 : c->ts.u.derived->name, &c->where);
10977 : 1 : error++;
10978 : 1 : continue;
10979 : : }
10980 : :
10981 : : /* Check F03:C816. */
10982 : 5242 : if (c->ts.type != BT_UNKNOWN
10983 : 3657 : && selector_type && !selector_type->attr.unlimited_polymorphic
10984 : 7163 : && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
10985 : 1922 : || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
10986 : : {
10987 : 5 : if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10988 : 1 : gfc_error ("Derived type %qs at %L must be an extension of %qs",
10989 : 1 : c->ts.u.derived->name, &c->where, selector_type->name);
10990 : : else
10991 : 4 : gfc_error ("Unexpected intrinsic type %qs at %L",
10992 : : gfc_basic_typename (c->ts.type), &c->where);
10993 : 5 : error++;
10994 : 5 : continue;
10995 : : }
10996 : :
10997 : : /* Check F03:C814. */
10998 : 5232 : if (c->ts.type == BT_CHARACTER
10999 : 736 : && (c->ts.u.cl->length != NULL || c->ts.deferred))
11000 : : {
11001 : 0 : gfc_error ("The type-spec at %L shall specify that each length "
11002 : : "type parameter is assumed", &c->where);
11003 : 0 : error++;
11004 : 0 : continue;
11005 : : }
11006 : :
11007 : : /* Intercept the DEFAULT case. */
11008 : 5232 : if (c->ts.type == BT_UNKNOWN)
11009 : : {
11010 : : /* Check F03:C818. */
11011 : 1580 : if (default_case)
11012 : : {
11013 : 1 : gfc_error ("The DEFAULT CASE at %L cannot be followed "
11014 : : "by a second DEFAULT CASE at %L",
11015 : 1 : &default_case->ext.block.case_list->where, &c->where);
11016 : 1 : error++;
11017 : 1 : continue;
11018 : : }
11019 : :
11020 : : default_case = body;
11021 : : }
11022 : : }
11023 : :
11024 : 2898 : if (error > 0)
11025 : : return;
11026 : :
11027 : : /* Transform SELECT TYPE statement to BLOCK and associate selector to
11028 : : target if present. If there are any EXIT statements referring to the
11029 : : SELECT TYPE construct, this is no problem because the gfc_code
11030 : : reference stays the same and EXIT is equally possible from the BLOCK
11031 : : it is changed to. */
11032 : 2896 : code->op = EXEC_BLOCK;
11033 : 2896 : if (code->expr2)
11034 : : {
11035 : 611 : gfc_association_list* assoc;
11036 : :
11037 : 611 : assoc = gfc_get_association_list ();
11038 : 611 : assoc->st = code->expr1->symtree;
11039 : 611 : assoc->target = gfc_copy_expr (code->expr2);
11040 : 611 : assoc->target->where = code->expr2->where;
11041 : : /* assoc->variable will be set by resolve_assoc_var. */
11042 : :
11043 : 611 : code->ext.block.assoc = assoc;
11044 : 611 : code->expr1->symtree->n.sym->assoc = assoc;
11045 : :
11046 : 611 : resolve_assoc_var (code->expr1->symtree->n.sym, false);
11047 : : }
11048 : : else
11049 : 2285 : code->ext.block.assoc = NULL;
11050 : :
11051 : : /* Ensure that the selector rank and arrayspec are available to
11052 : : correct expressions in which they might be missing. */
11053 : 2896 : if (code->expr2 && (code->expr2->rank || code->expr2->corank))
11054 : : {
11055 : 309 : rank = code->expr2->rank;
11056 : 309 : corank = code->expr2->corank;
11057 : 582 : for (ref = code->expr2->ref; ref; ref = ref->next)
11058 : 573 : if (ref->next == NULL)
11059 : : break;
11060 : 309 : if (ref && ref->type == REF_ARRAY)
11061 : 300 : ref = gfc_copy_ref (ref);
11062 : :
11063 : : /* Fixup expr1 if necessary. */
11064 : 309 : if (rank || corank)
11065 : 309 : fixup_array_ref (&code->expr1, code->expr2, rank, corank, ref);
11066 : : }
11067 : 2587 : else if (code->expr1->rank || code->expr1->corank)
11068 : : {
11069 : 827 : rank = code->expr1->rank;
11070 : 827 : corank = code->expr1->corank;
11071 : 827 : for (ref = code->expr1->ref; ref; ref = ref->next)
11072 : 827 : if (ref->next == NULL)
11073 : : break;
11074 : 827 : if (ref && ref->type == REF_ARRAY)
11075 : 827 : ref = gfc_copy_ref (ref);
11076 : : }
11077 : :
11078 : 2896 : gfc_expr *orig_expr1 = code->expr1;
11079 : :
11080 : : /* Add EXEC_SELECT to switch on type. */
11081 : 2896 : new_st = gfc_get_code (code->op);
11082 : 2896 : new_st->expr1 = code->expr1;
11083 : 2896 : new_st->expr2 = code->expr2;
11084 : 2896 : new_st->block = code->block;
11085 : 2896 : code->expr1 = code->expr2 = NULL;
11086 : 2896 : code->block = NULL;
11087 : 2896 : if (!ns->code)
11088 : 2896 : ns->code = new_st;
11089 : : else
11090 : 0 : ns->code->next = new_st;
11091 : 2896 : code = new_st;
11092 : 2896 : code->op = EXEC_SELECT_TYPE;
11093 : :
11094 : : /* Use the intrinsic LOC function to generate an integer expression
11095 : : for the vtable of the selector. Note that the rank of the selector
11096 : : expression has to be set to zero. */
11097 : 2896 : gfc_add_vptr_component (code->expr1);
11098 : 2896 : code->expr1->rank = 0;
11099 : 2896 : code->expr1->corank = 0;
11100 : 2896 : code->expr1 = build_loc_call (code->expr1);
11101 : 2896 : selector_expr = code->expr1->value.function.actual->expr;
11102 : :
11103 : : /* Loop over TYPE IS / CLASS IS cases. */
11104 : 8121 : for (body = code->block; body; body = body->block)
11105 : : {
11106 : 5225 : gfc_symbol *vtab;
11107 : 5225 : c = body->ext.block.case_list;
11108 : :
11109 : : /* Generate an index integer expression for address of the
11110 : : TYPE/CLASS vtable and store it in c->low. The hash expression
11111 : : is stored in c->high and is used to resolve intrinsic cases. */
11112 : 5225 : if (c->ts.type != BT_UNKNOWN)
11113 : : {
11114 : 3647 : gfc_expr *e;
11115 : 3647 : if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
11116 : : {
11117 : 2237 : vtab = gfc_find_derived_vtab (c->ts.u.derived);
11118 : 2237 : gcc_assert (vtab);
11119 : 2237 : c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
11120 : 2237 : c->ts.u.derived->hash_value);
11121 : : }
11122 : : else
11123 : : {
11124 : 1410 : vtab = gfc_find_vtab (&c->ts);
11125 : 1410 : gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
11126 : 1410 : e = CLASS_DATA (vtab)->initializer;
11127 : 1410 : c->high = gfc_copy_expr (e);
11128 : 1410 : if (c->high->ts.kind != gfc_integer_4_kind)
11129 : : {
11130 : 1 : gfc_typespec ts;
11131 : 1 : ts.kind = gfc_integer_4_kind;
11132 : 1 : ts.type = BT_INTEGER;
11133 : 1 : gfc_convert_type_warn (c->high, &ts, 2, 0);
11134 : : }
11135 : : }
11136 : :
11137 : 3647 : e = gfc_lval_expr_from_sym (vtab);
11138 : 3647 : c->low = build_loc_call (e);
11139 : : }
11140 : : else
11141 : 1578 : continue;
11142 : :
11143 : : /* Associate temporary to selector. This should only be done
11144 : : when this case is actually true, so build a new ASSOCIATE
11145 : : that does precisely this here (instead of using the
11146 : : 'global' one). */
11147 : :
11148 : : /* First check the derived type import status. */
11149 : 3647 : if (gfc_current_ns->import_state != IMPORT_NOT_SET
11150 : 6 : && (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS))
11151 : : {
11152 : 12 : st = gfc_find_symtree (gfc_current_ns->sym_root,
11153 : 6 : c->ts.u.derived->name);
11154 : 6 : if (!check_sym_import_status (c->ts.u.derived, st, NULL, old_code,
11155 : : gfc_current_ns))
11156 : 6 : error++;
11157 : : }
11158 : :
11159 : 3647 : const char * var_name = gfc_var_name_for_select_type_temp (orig_expr1);
11160 : 3647 : if (c->ts.type == BT_CLASS)
11161 : 321 : snprintf (name, sizeof (name), "__tmp_class_%s_%s",
11162 : 321 : c->ts.u.derived->name, var_name);
11163 : 3326 : else if (c->ts.type == BT_DERIVED)
11164 : 1916 : snprintf (name, sizeof (name), "__tmp_type_%s_%s",
11165 : 1916 : c->ts.u.derived->name, var_name);
11166 : 1410 : else if (c->ts.type == BT_CHARACTER)
11167 : : {
11168 : 736 : HOST_WIDE_INT charlen = 0;
11169 : 736 : if (c->ts.u.cl && c->ts.u.cl->length
11170 : 0 : && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
11171 : 0 : charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
11172 : 736 : snprintf (name, sizeof (name),
11173 : : "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d_%s",
11174 : : gfc_basic_typename (c->ts.type), charlen, c->ts.kind,
11175 : : var_name);
11176 : : }
11177 : : else
11178 : 674 : snprintf (name, sizeof (name), "__tmp_%s_%d_%s",
11179 : : gfc_basic_typename (c->ts.type), c->ts.kind, var_name);
11180 : :
11181 : 3647 : st = gfc_find_symtree (ns->sym_root, name);
11182 : 3647 : gcc_assert (st->n.sym->assoc);
11183 : 3647 : st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
11184 : 3647 : st->n.sym->assoc->target->where = selector_expr->where;
11185 : 3647 : if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
11186 : : {
11187 : 3326 : gfc_add_data_component (st->n.sym->assoc->target);
11188 : : /* Fixup the target expression if necessary. */
11189 : 3326 : if (rank || corank)
11190 : 1335 : fixup_array_ref (&st->n.sym->assoc->target, nullptr, rank, corank,
11191 : : ref);
11192 : : }
11193 : :
11194 : 3647 : new_st = gfc_get_code (EXEC_BLOCK);
11195 : 3647 : new_st->ext.block.ns = gfc_build_block_ns (ns);
11196 : 3647 : new_st->ext.block.ns->code = body->next;
11197 : 3647 : body->next = new_st;
11198 : :
11199 : : /* Chain in the new list only if it is marked as dangling. Otherwise
11200 : : there is a CASE label overlap and this is already used. Just ignore,
11201 : : the error is diagnosed elsewhere. */
11202 : 3647 : if (st->n.sym->assoc->dangling)
11203 : : {
11204 : 3646 : new_st->ext.block.assoc = st->n.sym->assoc;
11205 : 3646 : st->n.sym->assoc->dangling = 0;
11206 : : }
11207 : :
11208 : 3647 : resolve_assoc_var (st->n.sym, false);
11209 : : }
11210 : :
11211 : : /* Take out CLASS IS cases for separate treatment. */
11212 : : body = code;
11213 : 8121 : while (body && body->block)
11214 : : {
11215 : 5225 : if (body->block->ext.block.case_list->ts.type == BT_CLASS)
11216 : : {
11217 : : /* Add to class_is list. */
11218 : 321 : if (class_is == NULL)
11219 : : {
11220 : 290 : class_is = body->block;
11221 : 290 : tail = class_is;
11222 : : }
11223 : : else
11224 : : {
11225 : 43 : for (tail = class_is; tail->block; tail = tail->block) ;
11226 : 31 : tail->block = body->block;
11227 : 31 : tail = tail->block;
11228 : : }
11229 : : /* Remove from EXEC_SELECT list. */
11230 : 321 : body->block = body->block->block;
11231 : 321 : tail->block = NULL;
11232 : : }
11233 : : else
11234 : : body = body->block;
11235 : : }
11236 : :
11237 : 2896 : if (class_is)
11238 : : {
11239 : 290 : gfc_symbol *vtab;
11240 : :
11241 : 290 : if (!default_case)
11242 : : {
11243 : : /* Add a default case to hold the CLASS IS cases. */
11244 : 288 : for (tail = code; tail->block; tail = tail->block) ;
11245 : 180 : tail->block = gfc_get_code (EXEC_SELECT_TYPE);
11246 : 180 : tail = tail->block;
11247 : 180 : tail->ext.block.case_list = gfc_get_case ();
11248 : 180 : tail->ext.block.case_list->ts.type = BT_UNKNOWN;
11249 : 180 : tail->next = NULL;
11250 : 180 : default_case = tail;
11251 : : }
11252 : :
11253 : : /* More than one CLASS IS block? */
11254 : 290 : if (class_is->block)
11255 : : {
11256 : 37 : gfc_code **c1,*c2;
11257 : 37 : bool swapped;
11258 : : /* Sort CLASS IS blocks by extension level. */
11259 : 36 : do
11260 : : {
11261 : 37 : swapped = false;
11262 : 97 : for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
11263 : : {
11264 : 61 : c2 = (*c1)->block;
11265 : : /* F03:C817 (check for doubles). */
11266 : 61 : if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
11267 : 61 : == c2->ext.block.case_list->ts.u.derived->hash_value)
11268 : : {
11269 : 1 : gfc_error ("Double CLASS IS block in SELECT TYPE "
11270 : : "statement at %L",
11271 : : &c2->ext.block.case_list->where);
11272 : 1 : return;
11273 : : }
11274 : 60 : if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
11275 : 60 : < c2->ext.block.case_list->ts.u.derived->attr.extension)
11276 : : {
11277 : : /* Swap. */
11278 : 24 : (*c1)->block = c2->block;
11279 : 24 : c2->block = *c1;
11280 : 24 : *c1 = c2;
11281 : 24 : swapped = true;
11282 : : }
11283 : : }
11284 : : }
11285 : : while (swapped);
11286 : : }
11287 : :
11288 : : /* Generate IF chain. */
11289 : 289 : if_st = gfc_get_code (EXEC_IF);
11290 : 289 : new_st = if_st;
11291 : 608 : for (body = class_is; body; body = body->block)
11292 : : {
11293 : 319 : new_st->block = gfc_get_code (EXEC_IF);
11294 : 319 : new_st = new_st->block;
11295 : : /* Set up IF condition: Call _gfortran_is_extension_of. */
11296 : 319 : new_st->expr1 = gfc_get_expr ();
11297 : 319 : new_st->expr1->expr_type = EXPR_FUNCTION;
11298 : 319 : new_st->expr1->ts.type = BT_LOGICAL;
11299 : 319 : new_st->expr1->ts.kind = 4;
11300 : 319 : new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
11301 : 319 : new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
11302 : 319 : new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
11303 : : /* Set up arguments. */
11304 : 319 : new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
11305 : 319 : new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
11306 : 319 : new_st->expr1->value.function.actual->expr->where = code->loc;
11307 : 319 : new_st->expr1->where = code->loc;
11308 : 319 : gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
11309 : 319 : vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
11310 : 319 : st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
11311 : 319 : new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
11312 : 319 : new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
11313 : 319 : new_st->expr1->value.function.actual->next->expr->where = code->loc;
11314 : : /* Set up types in formal arg list. */
11315 : 319 : new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
11316 : 319 : new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
11317 : 319 : new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
11318 : 319 : new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
11319 : :
11320 : 319 : new_st->next = body->next;
11321 : : }
11322 : 289 : if (default_case->next)
11323 : : {
11324 : 110 : new_st->block = gfc_get_code (EXEC_IF);
11325 : 110 : new_st = new_st->block;
11326 : 110 : new_st->next = default_case->next;
11327 : : }
11328 : :
11329 : : /* Replace CLASS DEFAULT code by the IF chain. */
11330 : 289 : default_case->next = if_st;
11331 : : }
11332 : :
11333 : : /* Resolve the internal code. This cannot be done earlier because
11334 : : it requires that the sym->assoc of selectors is set already. */
11335 : 2895 : gfc_current_ns = ns;
11336 : 2895 : gfc_resolve_blocks (code->block, gfc_current_ns);
11337 : 2895 : gfc_current_ns = old_ns;
11338 : :
11339 : 2895 : free (ref);
11340 : : }
11341 : :
11342 : :
11343 : : /* Resolve a SELECT RANK statement. */
11344 : :
11345 : : static void
11346 : 1018 : resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
11347 : : {
11348 : 1018 : gfc_namespace *ns;
11349 : 1018 : gfc_code *body, *new_st, *tail;
11350 : 1018 : gfc_case *c;
11351 : 1018 : char tname[GFC_MAX_SYMBOL_LEN + 7];
11352 : 1018 : char name[2 * GFC_MAX_SYMBOL_LEN];
11353 : 1018 : gfc_symtree *st;
11354 : 1018 : gfc_expr *selector_expr = NULL;
11355 : 1018 : int case_value;
11356 : 1018 : HOST_WIDE_INT charlen = 0;
11357 : :
11358 : 1018 : ns = code->ext.block.ns;
11359 : 1018 : gfc_resolve (ns);
11360 : :
11361 : 1018 : code->op = EXEC_BLOCK;
11362 : 1018 : if (code->expr2)
11363 : : {
11364 : 42 : gfc_association_list* assoc;
11365 : :
11366 : 42 : assoc = gfc_get_association_list ();
11367 : 42 : assoc->st = code->expr1->symtree;
11368 : 42 : assoc->target = gfc_copy_expr (code->expr2);
11369 : 42 : assoc->target->where = code->expr2->where;
11370 : : /* assoc->variable will be set by resolve_assoc_var. */
11371 : :
11372 : 42 : code->ext.block.assoc = assoc;
11373 : 42 : code->expr1->symtree->n.sym->assoc = assoc;
11374 : :
11375 : 42 : resolve_assoc_var (code->expr1->symtree->n.sym, false);
11376 : : }
11377 : : else
11378 : 976 : code->ext.block.assoc = NULL;
11379 : :
11380 : : /* Loop over RANK cases. Note that returning on the errors causes a
11381 : : cascade of further errors because the case blocks do not compile
11382 : : correctly. */
11383 : 3320 : for (body = code->block; body; body = body->block)
11384 : : {
11385 : 2302 : c = body->ext.block.case_list;
11386 : 2302 : if (c->low)
11387 : 1383 : case_value = (int) mpz_get_si (c->low->value.integer);
11388 : : else
11389 : : case_value = -2;
11390 : :
11391 : : /* Check for repeated cases. */
11392 : 5836 : for (tail = code->block; tail; tail = tail->block)
11393 : : {
11394 : 5836 : gfc_case *d = tail->ext.block.case_list;
11395 : 5836 : int case_value2;
11396 : :
11397 : 5836 : if (tail == body)
11398 : : break;
11399 : :
11400 : : /* Check F2018: C1153. */
11401 : 3534 : if (!c->low && !d->low)
11402 : 1 : gfc_error ("RANK DEFAULT at %L is repeated at %L",
11403 : : &c->where, &d->where);
11404 : :
11405 : 3534 : if (!c->low || !d->low)
11406 : 1253 : continue;
11407 : :
11408 : : /* Check F2018: C1153. */
11409 : 2281 : case_value2 = (int) mpz_get_si (d->low->value.integer);
11410 : 2281 : if ((case_value == case_value2) && case_value == -1)
11411 : 1 : gfc_error ("RANK (*) at %L is repeated at %L",
11412 : : &c->where, &d->where);
11413 : 2280 : else if (case_value == case_value2)
11414 : 1 : gfc_error ("RANK (%i) at %L is repeated at %L",
11415 : : case_value, &c->where, &d->where);
11416 : : }
11417 : :
11418 : 2302 : if (!c->low)
11419 : 919 : continue;
11420 : :
11421 : : /* Check F2018: C1155. */
11422 : 1383 : if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
11423 : 1381 : || gfc_expr_attr (code->expr1).pointer))
11424 : 3 : gfc_error ("RANK (*) at %L cannot be used with the pointer or "
11425 : 3 : "allocatable selector at %L", &c->where, &code->expr1->where);
11426 : : }
11427 : :
11428 : : /* Add EXEC_SELECT to switch on rank. */
11429 : 1018 : new_st = gfc_get_code (code->op);
11430 : 1018 : new_st->expr1 = code->expr1;
11431 : 1018 : new_st->expr2 = code->expr2;
11432 : 1018 : new_st->block = code->block;
11433 : 1018 : code->expr1 = code->expr2 = NULL;
11434 : 1018 : code->block = NULL;
11435 : 1018 : if (!ns->code)
11436 : 1018 : ns->code = new_st;
11437 : : else
11438 : 0 : ns->code->next = new_st;
11439 : 1018 : code = new_st;
11440 : 1018 : code->op = EXEC_SELECT_RANK;
11441 : :
11442 : 1018 : selector_expr = code->expr1;
11443 : :
11444 : : /* Loop over SELECT RANK cases. */
11445 : 3320 : for (body = code->block; body; body = body->block)
11446 : : {
11447 : 2302 : c = body->ext.block.case_list;
11448 : 2302 : int case_value;
11449 : :
11450 : : /* Pass on the default case. */
11451 : 2302 : if (c->low == NULL)
11452 : 919 : continue;
11453 : :
11454 : : /* Associate temporary to selector. This should only be done
11455 : : when this case is actually true, so build a new ASSOCIATE
11456 : : that does precisely this here (instead of using the
11457 : : 'global' one). */
11458 : 1383 : if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
11459 : 265 : && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
11460 : 186 : charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
11461 : :
11462 : 1383 : if (c->ts.type == BT_CLASS)
11463 : 145 : sprintf (tname, "class_%s", c->ts.u.derived->name);
11464 : 1238 : else if (c->ts.type == BT_DERIVED)
11465 : 110 : sprintf (tname, "type_%s", c->ts.u.derived->name);
11466 : 1128 : else if (c->ts.type != BT_CHARACTER)
11467 : 569 : sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
11468 : : else
11469 : 559 : sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
11470 : : gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
11471 : :
11472 : 1383 : case_value = (int) mpz_get_si (c->low->value.integer);
11473 : 1383 : if (case_value >= 0)
11474 : 1350 : sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
11475 : : else
11476 : 33 : sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
11477 : :
11478 : 1383 : st = gfc_find_symtree (ns->sym_root, name);
11479 : 1383 : gcc_assert (st->n.sym->assoc);
11480 : :
11481 : 1383 : st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
11482 : 1383 : st->n.sym->assoc->target->where = selector_expr->where;
11483 : :
11484 : 1383 : new_st = gfc_get_code (EXEC_BLOCK);
11485 : 1383 : new_st->ext.block.ns = gfc_build_block_ns (ns);
11486 : 1383 : new_st->ext.block.ns->code = body->next;
11487 : 1383 : body->next = new_st;
11488 : :
11489 : : /* Chain in the new list only if it is marked as dangling. Otherwise
11490 : : there is a CASE label overlap and this is already used. Just ignore,
11491 : : the error is diagnosed elsewhere. */
11492 : 1383 : if (st->n.sym->assoc->dangling)
11493 : : {
11494 : 1381 : new_st->ext.block.assoc = st->n.sym->assoc;
11495 : 1381 : st->n.sym->assoc->dangling = 0;
11496 : : }
11497 : :
11498 : 1383 : resolve_assoc_var (st->n.sym, false);
11499 : : }
11500 : :
11501 : 1018 : gfc_current_ns = ns;
11502 : 1018 : gfc_resolve_blocks (code->block, gfc_current_ns);
11503 : 1018 : gfc_current_ns = old_ns;
11504 : 1018 : }
11505 : :
11506 : :
11507 : : /* Resolve a transfer statement. This is making sure that:
11508 : : -- a derived type being transferred has only non-pointer components
11509 : : -- a derived type being transferred doesn't have private components, unless
11510 : : it's being transferred from the module where the type was defined
11511 : : -- we're not trying to transfer a whole assumed size array. */
11512 : :
11513 : : static void
11514 : 45382 : resolve_transfer (gfc_code *code)
11515 : : {
11516 : 45382 : gfc_symbol *sym, *derived;
11517 : 45382 : gfc_ref *ref;
11518 : 45382 : gfc_expr *exp;
11519 : 45382 : bool write = false;
11520 : 45382 : bool formatted = false;
11521 : 45382 : gfc_dt *dt = code->ext.dt;
11522 : 45382 : gfc_symbol *dtio_sub = NULL;
11523 : :
11524 : 45382 : exp = code->expr1;
11525 : :
11526 : 90770 : while (exp != NULL && exp->expr_type == EXPR_OP
11527 : 46268 : && exp->value.op.op == INTRINSIC_PARENTHESES)
11528 : 6 : exp = exp->value.op.op1;
11529 : :
11530 : 45382 : if (exp && exp->expr_type == EXPR_NULL
11531 : 2 : && code->ext.dt)
11532 : : {
11533 : 2 : gfc_error ("Invalid context for NULL () intrinsic at %L",
11534 : : &exp->where);
11535 : 2 : return;
11536 : : }
11537 : :
11538 : : if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
11539 : : && exp->expr_type != EXPR_FUNCTION
11540 : : && exp->expr_type != EXPR_ARRAY
11541 : : && exp->expr_type != EXPR_STRUCTURE))
11542 : : return;
11543 : :
11544 : : /* If we are reading, the variable will be changed. Note that
11545 : : code->ext.dt may be NULL if the TRANSFER is related to
11546 : : an INQUIRE statement -- but in this case, we are not reading, either. */
11547 : 24670 : if (dt && dt->dt_io_kind->value.iokind == M_READ
11548 : 32078 : && !gfc_check_vardef_context (exp, false, false, false,
11549 : 7260 : _("item in READ")))
11550 : : return;
11551 : :
11552 : 24814 : const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
11553 : 24814 : || exp->expr_type == EXPR_FUNCTION
11554 : 20471 : || exp->expr_type == EXPR_ARRAY
11555 : 45285 : ? &exp->ts : &exp->symtree->n.sym->ts;
11556 : :
11557 : : /* Go to actual component transferred. */
11558 : 32070 : for (ref = exp->ref; ref; ref = ref->next)
11559 : 7256 : if (ref->type == REF_COMPONENT)
11560 : 2132 : ts = &ref->u.c.component->ts;
11561 : :
11562 : 24814 : if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
11563 : 24666 : && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
11564 : : {
11565 : 698 : derived = ts->u.derived;
11566 : :
11567 : : /* Determine when to use the formatted DTIO procedure. */
11568 : 698 : if (dt && (dt->format_expr || dt->format_label))
11569 : 623 : formatted = true;
11570 : :
11571 : 698 : write = dt->dt_io_kind->value.iokind == M_WRITE
11572 : 698 : || dt->dt_io_kind->value.iokind == M_PRINT;
11573 : 698 : dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
11574 : :
11575 : 698 : if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
11576 : : {
11577 : 431 : dt->udtio = exp;
11578 : 431 : sym = exp->symtree->n.sym->ns->proc_name;
11579 : : /* Check to see if this is a nested DTIO call, with the
11580 : : dummy as the io-list object. */
11581 : 431 : if (sym && sym == dtio_sub && sym->formal
11582 : 30 : && sym->formal->sym == exp->symtree->n.sym
11583 : 30 : && exp->ref == NULL)
11584 : : {
11585 : 0 : if (!sym->attr.recursive)
11586 : : {
11587 : 0 : gfc_error ("DTIO %s procedure at %L must be recursive",
11588 : : sym->name, &sym->declared_at);
11589 : 0 : return;
11590 : : }
11591 : : }
11592 : : }
11593 : : }
11594 : :
11595 : 24814 : if (ts->type == BT_CLASS && dtio_sub == NULL)
11596 : : {
11597 : 3 : gfc_error ("Data transfer element at %L cannot be polymorphic unless "
11598 : : "it is processed by a defined input/output procedure",
11599 : : &code->loc);
11600 : 3 : return;
11601 : : }
11602 : :
11603 : 24811 : if (ts->type == BT_DERIVED)
11604 : : {
11605 : : /* Check that transferred derived type doesn't contain POINTER
11606 : : components unless it is processed by a defined input/output
11607 : : procedure". */
11608 : 666 : if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
11609 : : {
11610 : 2 : gfc_error ("Data transfer element at %L cannot have POINTER "
11611 : : "components unless it is processed by a defined "
11612 : : "input/output procedure", &code->loc);
11613 : 2 : return;
11614 : : }
11615 : :
11616 : : /* F08:C935. */
11617 : 664 : if (ts->u.derived->attr.proc_pointer_comp)
11618 : : {
11619 : 2 : gfc_error ("Data transfer element at %L cannot have "
11620 : : "procedure pointer components", &code->loc);
11621 : 2 : return;
11622 : : }
11623 : :
11624 : 662 : if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
11625 : : {
11626 : 6 : gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
11627 : : "components unless it is processed by a defined "
11628 : : "input/output procedure", &code->loc);
11629 : 6 : return;
11630 : : }
11631 : :
11632 : : /* C_PTR and C_FUNPTR have private components which means they cannot
11633 : : be printed. However, if -std=gnu and not -pedantic, allow
11634 : : the component to be printed to help debugging. */
11635 : 656 : if (ts->u.derived->ts.f90_type == BT_VOID)
11636 : : {
11637 : 4 : gfc_error ("Data transfer element at %L "
11638 : : "cannot have PRIVATE components", &code->loc);
11639 : 4 : return;
11640 : : }
11641 : 652 : else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
11642 : : {
11643 : 4 : gfc_error ("Data transfer element at %L cannot have "
11644 : : "PRIVATE components unless it is processed by "
11645 : : "a defined input/output procedure", &code->loc);
11646 : 4 : return;
11647 : : }
11648 : : }
11649 : :
11650 : 24793 : if (exp->expr_type == EXPR_STRUCTURE)
11651 : : return;
11652 : :
11653 : 24748 : if (exp->expr_type == EXPR_ARRAY)
11654 : : return;
11655 : :
11656 : 24372 : sym = exp->symtree->n.sym;
11657 : :
11658 : 24372 : if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
11659 : 81 : && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
11660 : : {
11661 : 1 : gfc_error ("Data transfer element at %L cannot be a full reference to "
11662 : : "an assumed-size array", &code->loc);
11663 : 1 : return;
11664 : : }
11665 : : }
11666 : :
11667 : :
11668 : : /*********** Toplevel code resolution subroutines ***********/
11669 : :
11670 : : /* Find the set of labels that are reachable from this block. We also
11671 : : record the last statement in each block. */
11672 : :
11673 : : static void
11674 : 661312 : find_reachable_labels (gfc_code *block)
11675 : : {
11676 : 661312 : gfc_code *c;
11677 : :
11678 : 661312 : if (!block)
11679 : : return;
11680 : :
11681 : 414538 : cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
11682 : :
11683 : : /* Collect labels in this block. We don't keep those corresponding
11684 : : to END {IF|SELECT}, these are checked in resolve_branch by going
11685 : : up through the code_stack. */
11686 : 1521051 : for (c = block; c; c = c->next)
11687 : : {
11688 : 1106513 : if (c->here && c->op != EXEC_END_NESTED_BLOCK)
11689 : 3589 : bitmap_set_bit (cs_base->reachable_labels, c->here->value);
11690 : : }
11691 : :
11692 : : /* Merge with labels from parent block. */
11693 : 414538 : if (cs_base->prev)
11694 : : {
11695 : 340561 : gcc_assert (cs_base->prev->reachable_labels);
11696 : 340561 : bitmap_ior_into (cs_base->reachable_labels,
11697 : : cs_base->prev->reachable_labels);
11698 : : }
11699 : : }
11700 : :
11701 : : static void
11702 : 136 : resolve_lock_unlock_event (gfc_code *code)
11703 : : {
11704 : 136 : if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
11705 : 136 : && (code->expr1->ts.type != BT_DERIVED
11706 : 95 : || code->expr1->expr_type != EXPR_VARIABLE
11707 : 95 : || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
11708 : 94 : || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
11709 : 94 : || code->expr1->rank != 0
11710 : 124 : || (!gfc_is_coarray (code->expr1) &&
11711 : 31 : !gfc_is_coindexed (code->expr1))))
11712 : 4 : gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
11713 : 4 : &code->expr1->where);
11714 : 132 : else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
11715 : 39 : && (code->expr1->ts.type != BT_DERIVED
11716 : 39 : || code->expr1->expr_type != EXPR_VARIABLE
11717 : 39 : || code->expr1->ts.u.derived->from_intmod
11718 : : != INTMOD_ISO_FORTRAN_ENV
11719 : 39 : || code->expr1->ts.u.derived->intmod_sym_id
11720 : : != ISOFORTRAN_EVENT_TYPE
11721 : 39 : || code->expr1->rank != 0))
11722 : 0 : gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
11723 : : &code->expr1->where);
11724 : 23 : else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
11725 : 143 : && !gfc_is_coindexed (code->expr1))
11726 : 0 : gfc_error ("Event variable argument at %L must be a coarray or coindexed",
11727 : 0 : &code->expr1->where);
11728 : 132 : else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
11729 : 0 : gfc_error ("Event variable argument at %L must be a coarray but not "
11730 : 0 : "coindexed", &code->expr1->where);
11731 : :
11732 : : /* Check STAT. */
11733 : 136 : if (code->expr2
11734 : 38 : && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
11735 : 38 : || code->expr2->expr_type != EXPR_VARIABLE))
11736 : 0 : gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
11737 : : &code->expr2->where);
11738 : :
11739 : 136 : if (code->expr2
11740 : 174 : && !gfc_check_vardef_context (code->expr2, false, false, false,
11741 : 38 : _("STAT variable")))
11742 : : return;
11743 : :
11744 : : /* Check ERRMSG. */
11745 : 136 : if (code->expr3
11746 : 2 : && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
11747 : 2 : || code->expr3->expr_type != EXPR_VARIABLE))
11748 : 0 : gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
11749 : : &code->expr3->where);
11750 : :
11751 : 136 : if (code->expr3
11752 : 138 : && !gfc_check_vardef_context (code->expr3, false, false, false,
11753 : 2 : _("ERRMSG variable")))
11754 : : return;
11755 : :
11756 : : /* Check for LOCK the ACQUIRED_LOCK. */
11757 : 136 : if (code->op != EXEC_EVENT_WAIT && code->expr4
11758 : 16 : && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
11759 : 16 : || code->expr4->expr_type != EXPR_VARIABLE))
11760 : 0 : gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
11761 : : "variable", &code->expr4->where);
11762 : :
11763 : 120 : if (code->op != EXEC_EVENT_WAIT && code->expr4
11764 : 152 : && !gfc_check_vardef_context (code->expr4, false, false, false,
11765 : 16 : _("ACQUIRED_LOCK variable")))
11766 : : return;
11767 : :
11768 : : /* Check for EVENT WAIT the UNTIL_COUNT. */
11769 : 136 : if (code->op == EXEC_EVENT_WAIT && code->expr4)
11770 : : {
11771 : 24 : if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
11772 : 24 : || code->expr4->rank != 0)
11773 : 0 : gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
11774 : 0 : "expression", &code->expr4->where);
11775 : : }
11776 : : }
11777 : :
11778 : : static void
11779 : 195 : resolve_team_argument (gfc_expr *team)
11780 : : {
11781 : 195 : gfc_resolve_expr (team);
11782 : 195 : if (team->rank != 0 || team->ts.type != BT_DERIVED
11783 : 188 : || team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
11784 : 188 : || team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
11785 : : {
11786 : 7 : gfc_error ("TEAM argument at %L must be a scalar expression "
11787 : : "of type TEAM_TYPE from the intrinsic module ISO_FORTRAN_ENV",
11788 : : &team->where);
11789 : : }
11790 : 195 : }
11791 : :
11792 : : static void
11793 : 1228 : resolve_scalar_variable_as_arg (const char *name, bt exp_type, int exp_kind,
11794 : : gfc_expr *e)
11795 : : {
11796 : 1228 : gfc_resolve_expr (e);
11797 : 1228 : if (e
11798 : 139 : && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0
11799 : 124 : || e->expr_type != EXPR_VARIABLE))
11800 : 15 : gfc_error ("%s argument at %L must be a scalar %s variable of at least "
11801 : : "kind %d", name, &e->where, gfc_basic_typename (exp_type),
11802 : : exp_kind);
11803 : 1228 : }
11804 : :
11805 : : void
11806 : 614 : gfc_resolve_sync_stat (struct sync_stat *sync_stat)
11807 : : {
11808 : 614 : resolve_scalar_variable_as_arg ("STAT=", BT_INTEGER, 2, sync_stat->stat);
11809 : 614 : resolve_scalar_variable_as_arg ("ERRMSG=", BT_CHARACTER,
11810 : : gfc_default_character_kind,
11811 : : sync_stat->errmsg);
11812 : 614 : }
11813 : :
11814 : : static void
11815 : 214 : resolve_scalar_argument (const char *name, bt exp_type, int exp_kind,
11816 : : gfc_expr *e)
11817 : : {
11818 : 214 : gfc_resolve_expr (e);
11819 : 214 : if (e
11820 : 140 : && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0))
11821 : 3 : gfc_error ("%s argument at %L must be a scalar %s of at least kind %d",
11822 : : name, &e->where, gfc_basic_typename (exp_type), exp_kind);
11823 : 214 : }
11824 : :
11825 : : static void
11826 : 107 : resolve_form_team (gfc_code *code)
11827 : : {
11828 : 107 : resolve_scalar_argument ("TEAM NUMBER", BT_INTEGER, gfc_default_integer_kind,
11829 : : code->expr1);
11830 : 107 : resolve_team_argument (code->expr2);
11831 : 107 : resolve_scalar_argument ("NEW_INDEX=", BT_INTEGER, gfc_default_integer_kind,
11832 : : code->expr3);
11833 : 107 : gfc_resolve_sync_stat (&code->ext.sync_stat);
11834 : 107 : }
11835 : :
11836 : : static void resolve_block_construct (gfc_code *);
11837 : :
11838 : : static void
11839 : 66 : resolve_change_team (gfc_code *code)
11840 : : {
11841 : 66 : resolve_team_argument (code->expr1);
11842 : 66 : gfc_resolve_sync_stat (&code->ext.block.sync_stat);
11843 : 132 : resolve_block_construct (code);
11844 : : /* Map the coarray bounds as selected. */
11845 : 68 : for (gfc_association_list *a = code->ext.block.assoc; a; a = a->next)
11846 : 2 : if (a->ar)
11847 : : {
11848 : 2 : gfc_array_spec *src = a->ar->as, *dst;
11849 : 2 : if (a->st->n.sym->ts.type == BT_CLASS)
11850 : 0 : dst = CLASS_DATA (a->st->n.sym)->as;
11851 : : else
11852 : 2 : dst = a->st->n.sym->as;
11853 : 2 : dst->corank = src->corank;
11854 : 2 : dst->cotype = src->cotype;
11855 : 4 : for (int i = 0; i < src->corank; ++i)
11856 : : {
11857 : 2 : dst->lower[dst->rank + i] = src->lower[i];
11858 : 2 : dst->upper[dst->rank + i] = src->upper[i];
11859 : 2 : src->lower[i] = src->upper[i] = nullptr;
11860 : : }
11861 : 2 : gfc_free_array_spec (src);
11862 : 2 : free (a->ar);
11863 : 2 : a->ar = nullptr;
11864 : 2 : dst->resolved = false;
11865 : 2 : gfc_resolve_array_spec (dst, 0);
11866 : : }
11867 : 66 : }
11868 : :
11869 : : static void
11870 : 22 : resolve_sync_team (gfc_code *code)
11871 : : {
11872 : 22 : resolve_team_argument (code->expr1);
11873 : 22 : gfc_resolve_sync_stat (&code->ext.sync_stat);
11874 : 22 : }
11875 : :
11876 : : static void
11877 : 64 : resolve_end_team (gfc_code *code)
11878 : : {
11879 : 64 : gfc_resolve_sync_stat (&code->ext.sync_stat);
11880 : 64 : }
11881 : :
11882 : : static void
11883 : 50 : resolve_critical (gfc_code *code)
11884 : : {
11885 : 50 : gfc_symtree *symtree;
11886 : 50 : gfc_symbol *lock_type;
11887 : 50 : char name[GFC_MAX_SYMBOL_LEN];
11888 : 50 : static int serial = 0;
11889 : :
11890 : 50 : gfc_resolve_sync_stat (&code->ext.sync_stat);
11891 : :
11892 : 50 : if (flag_coarray != GFC_FCOARRAY_LIB)
11893 : 30 : return;
11894 : :
11895 : 20 : symtree = gfc_find_symtree (gfc_current_ns->sym_root,
11896 : : GFC_PREFIX ("lock_type"));
11897 : 20 : if (symtree)
11898 : 10 : lock_type = symtree->n.sym;
11899 : : else
11900 : : {
11901 : 10 : if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
11902 : : false) != 0)
11903 : 0 : gcc_unreachable ();
11904 : 10 : lock_type = symtree->n.sym;
11905 : 10 : lock_type->attr.flavor = FL_DERIVED;
11906 : 10 : lock_type->attr.zero_comp = 1;
11907 : 10 : lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
11908 : 10 : lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
11909 : : }
11910 : :
11911 : 20 : sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
11912 : 20 : if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
11913 : 0 : gcc_unreachable ();
11914 : :
11915 : 20 : code->resolved_sym = symtree->n.sym;
11916 : 20 : symtree->n.sym->attr.flavor = FL_VARIABLE;
11917 : 20 : symtree->n.sym->attr.referenced = 1;
11918 : 20 : symtree->n.sym->attr.artificial = 1;
11919 : 20 : symtree->n.sym->attr.codimension = 1;
11920 : 20 : symtree->n.sym->ts.type = BT_DERIVED;
11921 : 20 : symtree->n.sym->ts.u.derived = lock_type;
11922 : 20 : symtree->n.sym->as = gfc_get_array_spec ();
11923 : 20 : symtree->n.sym->as->corank = 1;
11924 : 20 : symtree->n.sym->as->type = AS_EXPLICIT;
11925 : 20 : symtree->n.sym->as->cotype = AS_EXPLICIT;
11926 : 20 : symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
11927 : : NULL, 1);
11928 : 20 : gfc_commit_symbols();
11929 : : }
11930 : :
11931 : :
11932 : : static void
11933 : 747 : resolve_sync (gfc_code *code)
11934 : : {
11935 : : /* Check imageset. The * case matches expr1 == NULL. */
11936 : 747 : if (code->expr1)
11937 : : {
11938 : 48 : if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
11939 : 1 : gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
11940 : : "INTEGER expression", &code->expr1->where);
11941 : 48 : if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
11942 : 23 : && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
11943 : 1 : gfc_error ("Imageset argument at %L must between 1 and num_images()",
11944 : : &code->expr1->where);
11945 : 47 : else if (code->expr1->expr_type == EXPR_ARRAY
11946 : 47 : && gfc_simplify_expr (code->expr1, 0))
11947 : : {
11948 : 18 : gfc_constructor *cons;
11949 : 18 : cons = gfc_constructor_first (code->expr1->value.constructor);
11950 : 54 : for (; cons; cons = gfc_constructor_next (cons))
11951 : 18 : if (cons->expr->expr_type == EXPR_CONSTANT
11952 : 18 : && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
11953 : 0 : gfc_error ("Imageset argument at %L must between 1 and "
11954 : : "num_images()", &cons->expr->where);
11955 : : }
11956 : : }
11957 : :
11958 : : /* Check STAT. */
11959 : 747 : gfc_resolve_expr (code->expr2);
11960 : 747 : if (code->expr2)
11961 : : {
11962 : 84 : if (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0)
11963 : 1 : gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
11964 : : &code->expr2->where);
11965 : : else
11966 : 83 : gfc_check_vardef_context (code->expr2, false, false, false,
11967 : 83 : _("STAT variable"));
11968 : : }
11969 : :
11970 : : /* Check ERRMSG. */
11971 : 747 : gfc_resolve_expr (code->expr3);
11972 : 747 : if (code->expr3)
11973 : : {
11974 : 75 : if (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0)
11975 : 4 : gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
11976 : : &code->expr3->where);
11977 : : else
11978 : 71 : gfc_check_vardef_context (code->expr3, false, false, false,
11979 : 71 : _("ERRMSG variable"));
11980 : : }
11981 : 747 : }
11982 : :
11983 : :
11984 : : /* Given a branch to a label, see if the branch is conforming.
11985 : : The code node describes where the branch is located. */
11986 : :
11987 : : static void
11988 : 105860 : resolve_branch (gfc_st_label *label, gfc_code *code)
11989 : : {
11990 : 105860 : code_stack *stack;
11991 : :
11992 : 105860 : if (label == NULL)
11993 : : return;
11994 : :
11995 : : /* Step one: is this a valid branching target? */
11996 : :
11997 : 2459 : if (label->defined == ST_LABEL_UNKNOWN)
11998 : : {
11999 : 4 : gfc_error ("Label %d referenced at %L is never defined", label->value,
12000 : : &code->loc);
12001 : 4 : return;
12002 : : }
12003 : :
12004 : 2455 : if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
12005 : : {
12006 : 4 : gfc_error ("Statement at %L is not a valid branch target statement "
12007 : : "for the branch statement at %L", &label->where, &code->loc);
12008 : 4 : return;
12009 : : }
12010 : :
12011 : : /* Step two: make sure this branch is not a branch to itself ;-) */
12012 : :
12013 : 2451 : if (code->here == label)
12014 : : {
12015 : 0 : gfc_warning (0, "Branch at %L may result in an infinite loop",
12016 : : &code->loc);
12017 : 0 : return;
12018 : : }
12019 : :
12020 : : /* Step three: See if the label is in the same block as the
12021 : : branching statement. The hard work has been done by setting up
12022 : : the bitmap reachable_labels. */
12023 : :
12024 : 2451 : if (bitmap_bit_p (cs_base->reachable_labels, label->value))
12025 : : {
12026 : : /* Check now whether there is a CRITICAL construct; if so, check
12027 : : whether the label is still visible outside of the CRITICAL block,
12028 : : which is invalid. */
12029 : 6264 : for (stack = cs_base; stack; stack = stack->prev)
12030 : : {
12031 : 3881 : if (stack->current->op == EXEC_CRITICAL
12032 : 3881 : && bitmap_bit_p (stack->reachable_labels, label->value))
12033 : 2 : gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
12034 : : "label at %L", &code->loc, &label->where);
12035 : 3879 : else if (stack->current->op == EXEC_DO_CONCURRENT
12036 : 3879 : && bitmap_bit_p (stack->reachable_labels, label->value))
12037 : 0 : gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
12038 : : "for label at %L", &code->loc, &label->where);
12039 : 3879 : else if (stack->current->op == EXEC_CHANGE_TEAM
12040 : 3879 : && bitmap_bit_p (stack->reachable_labels, label->value))
12041 : 1 : gfc_error ("GOTO statement at %L leaves CHANGE TEAM construct "
12042 : : "for label at %L", &code->loc, &label->where);
12043 : : }
12044 : :
12045 : : return;
12046 : : }
12047 : :
12048 : : /* Step four: If we haven't found the label in the bitmap, it may
12049 : : still be the label of the END of the enclosing block, in which
12050 : : case we find it by going up the code_stack. */
12051 : :
12052 : 167 : for (stack = cs_base; stack; stack = stack->prev)
12053 : : {
12054 : 131 : if (stack->current->next && stack->current->next->here == label)
12055 : : break;
12056 : 101 : if (stack->current->op == EXEC_CRITICAL)
12057 : : {
12058 : : /* Note: A label at END CRITICAL does not leave the CRITICAL
12059 : : construct as END CRITICAL is still part of it. */
12060 : 2 : gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
12061 : : " at %L", &code->loc, &label->where);
12062 : 2 : return;
12063 : : }
12064 : 99 : else if (stack->current->op == EXEC_DO_CONCURRENT)
12065 : : {
12066 : 0 : gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
12067 : : "label at %L", &code->loc, &label->where);
12068 : 0 : return;
12069 : : }
12070 : : }
12071 : :
12072 : 66 : if (stack)
12073 : : {
12074 : 30 : gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
12075 : : return;
12076 : : }
12077 : :
12078 : : /* The label is not in an enclosing block, so illegal. This was
12079 : : allowed in Fortran 66, so we allow it as extension. No
12080 : : further checks are necessary in this case. */
12081 : 36 : gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
12082 : : "as the GOTO statement at %L", &label->where,
12083 : : &code->loc);
12084 : 36 : return;
12085 : : }
12086 : :
12087 : :
12088 : : /* Check whether EXPR1 has the same shape as EXPR2. */
12089 : :
12090 : : static bool
12091 : 1461 : resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
12092 : : {
12093 : 1461 : mpz_t shape[GFC_MAX_DIMENSIONS];
12094 : 1461 : mpz_t shape2[GFC_MAX_DIMENSIONS];
12095 : 1461 : bool result = false;
12096 : 1461 : int i;
12097 : :
12098 : : /* Compare the rank. */
12099 : 1461 : if (expr1->rank != expr2->rank)
12100 : : return result;
12101 : :
12102 : : /* Compare the size of each dimension. */
12103 : 2795 : for (i=0; i<expr1->rank; i++)
12104 : : {
12105 : 1484 : if (!gfc_array_dimen_size (expr1, i, &shape[i]))
12106 : 150 : goto ignore;
12107 : :
12108 : 1334 : if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
12109 : 0 : goto ignore;
12110 : :
12111 : 1334 : if (mpz_cmp (shape[i], shape2[i]))
12112 : 0 : goto over;
12113 : : }
12114 : :
12115 : : /* When either of the two expression is an assumed size array, we
12116 : : ignore the comparison of dimension sizes. */
12117 : 1311 : ignore:
12118 : : result = true;
12119 : :
12120 : 1461 : over:
12121 : 1461 : gfc_clear_shape (shape, i);
12122 : 1461 : gfc_clear_shape (shape2, i);
12123 : 1461 : return result;
12124 : : }
12125 : :
12126 : :
12127 : : /* Check whether a WHERE assignment target or a WHERE mask expression
12128 : : has the same shape as the outmost WHERE mask expression. */
12129 : :
12130 : : static void
12131 : 506 : resolve_where (gfc_code *code, gfc_expr *mask)
12132 : : {
12133 : 506 : gfc_code *cblock;
12134 : 506 : gfc_code *cnext;
12135 : 506 : gfc_expr *e = NULL;
12136 : :
12137 : 506 : cblock = code->block;
12138 : :
12139 : : /* Store the first WHERE mask-expr of the WHERE statement or construct.
12140 : : In case of nested WHERE, only the outmost one is stored. */
12141 : 506 : if (mask == NULL) /* outmost WHERE */
12142 : 450 : e = cblock->expr1;
12143 : : else /* inner WHERE */
12144 : 506 : e = mask;
12145 : :
12146 : 1381 : while (cblock)
12147 : : {
12148 : 875 : if (cblock->expr1)
12149 : : {
12150 : : /* Check if the mask-expr has a consistent shape with the
12151 : : outmost WHERE mask-expr. */
12152 : 711 : if (!resolve_where_shape (cblock->expr1, e))
12153 : 0 : gfc_error ("WHERE mask at %L has inconsistent shape",
12154 : 0 : &cblock->expr1->where);
12155 : : }
12156 : :
12157 : : /* the assignment statement of a WHERE statement, or the first
12158 : : statement in where-body-construct of a WHERE construct */
12159 : 875 : cnext = cblock->next;
12160 : 1727 : while (cnext)
12161 : : {
12162 : 852 : switch (cnext->op)
12163 : : {
12164 : : /* WHERE assignment statement */
12165 : 750 : case EXEC_ASSIGN:
12166 : :
12167 : : /* Check shape consistent for WHERE assignment target. */
12168 : 750 : if (e && !resolve_where_shape (cnext->expr1, e))
12169 : 0 : gfc_error ("WHERE assignment target at %L has "
12170 : 0 : "inconsistent shape", &cnext->expr1->where);
12171 : :
12172 : 750 : if (cnext->op == EXEC_ASSIGN
12173 : 750 : && gfc_may_be_finalized (cnext->expr1->ts))
12174 : 0 : cnext->expr1->must_finalize = 1;
12175 : :
12176 : : break;
12177 : :
12178 : :
12179 : 46 : case EXEC_ASSIGN_CALL:
12180 : 46 : resolve_call (cnext);
12181 : 46 : if (!cnext->resolved_sym->attr.elemental)
12182 : 2 : gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
12183 : 2 : &cnext->ext.actual->expr->where);
12184 : : break;
12185 : :
12186 : : /* WHERE or WHERE construct is part of a where-body-construct */
12187 : 56 : case EXEC_WHERE:
12188 : 56 : resolve_where (cnext, e);
12189 : 56 : break;
12190 : :
12191 : 0 : default:
12192 : 0 : gfc_error ("Unsupported statement inside WHERE at %L",
12193 : : &cnext->loc);
12194 : : }
12195 : : /* the next statement within the same where-body-construct */
12196 : 852 : cnext = cnext->next;
12197 : : }
12198 : : /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
12199 : 875 : cblock = cblock->block;
12200 : : }
12201 : 506 : }
12202 : :
12203 : :
12204 : : /* Resolve assignment in FORALL construct.
12205 : : NVAR is the number of FORALL index variables, and VAR_EXPR records the
12206 : : FORALL index variables. */
12207 : :
12208 : : static void
12209 : 1944 : gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
12210 : : {
12211 : 1944 : int n;
12212 : :
12213 : 5871 : for (n = 0; n < nvar; n++)
12214 : : {
12215 : 3927 : gfc_symbol *forall_index;
12216 : :
12217 : 3927 : forall_index = var_expr[n]->symtree->n.sym;
12218 : :
12219 : : /* Check whether the assignment target is one of the FORALL index
12220 : : variable. */
12221 : 3927 : if ((code->expr1->expr_type == EXPR_VARIABLE)
12222 : 3927 : && (code->expr1->symtree->n.sym == forall_index))
12223 : 0 : gfc_error ("Assignment to a FORALL index variable at %L",
12224 : : &code->expr1->where);
12225 : : else
12226 : : {
12227 : : /* If one of the FORALL index variables doesn't appear in the
12228 : : assignment variable, then there could be a many-to-one
12229 : : assignment. Emit a warning rather than an error because the
12230 : : mask could be resolving this problem. */
12231 : 3927 : if (!find_forall_index (code->expr1, forall_index, 0))
12232 : 0 : gfc_warning (0, "The FORALL with index %qs is not used on the "
12233 : : "left side of the assignment at %L and so might "
12234 : : "cause multiple assignment to this object",
12235 : 0 : var_expr[n]->symtree->name, &code->expr1->where);
12236 : : }
12237 : : }
12238 : 1944 : }
12239 : :
12240 : :
12241 : : /* Resolve WHERE statement in FORALL construct. */
12242 : :
12243 : : static void
12244 : 46 : gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
12245 : : gfc_expr **var_expr)
12246 : : {
12247 : 46 : gfc_code *cblock;
12248 : 46 : gfc_code *cnext;
12249 : :
12250 : 46 : cblock = code->block;
12251 : 111 : while (cblock)
12252 : : {
12253 : : /* the assignment statement of a WHERE statement, or the first
12254 : : statement in where-body-construct of a WHERE construct */
12255 : 65 : cnext = cblock->next;
12256 : 130 : while (cnext)
12257 : : {
12258 : 65 : switch (cnext->op)
12259 : : {
12260 : : /* WHERE assignment statement */
12261 : 65 : case EXEC_ASSIGN:
12262 : 65 : gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
12263 : :
12264 : 65 : if (cnext->op == EXEC_ASSIGN
12265 : 65 : && gfc_may_be_finalized (cnext->expr1->ts))
12266 : 0 : cnext->expr1->must_finalize = 1;
12267 : :
12268 : : break;
12269 : :
12270 : : /* WHERE operator assignment statement */
12271 : 0 : case EXEC_ASSIGN_CALL:
12272 : 0 : resolve_call (cnext);
12273 : 0 : if (!cnext->resolved_sym->attr.elemental)
12274 : 0 : gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
12275 : 0 : &cnext->ext.actual->expr->where);
12276 : : break;
12277 : :
12278 : : /* WHERE or WHERE construct is part of a where-body-construct */
12279 : 0 : case EXEC_WHERE:
12280 : 0 : gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
12281 : 0 : break;
12282 : :
12283 : 0 : default:
12284 : 0 : gfc_error ("Unsupported statement inside WHERE at %L",
12285 : : &cnext->loc);
12286 : : }
12287 : : /* the next statement within the same where-body-construct */
12288 : 65 : cnext = cnext->next;
12289 : : }
12290 : : /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
12291 : 65 : cblock = cblock->block;
12292 : : }
12293 : 46 : }
12294 : :
12295 : :
12296 : : /* Traverse the FORALL body to check whether the following errors exist:
12297 : : 1. For assignment, check if a many-to-one assignment happens.
12298 : : 2. For WHERE statement, check the WHERE body to see if there is any
12299 : : many-to-one assignment. */
12300 : :
12301 : : static void
12302 : 1990 : gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
12303 : : {
12304 : 1990 : gfc_code *c;
12305 : :
12306 : 1990 : c = code->block->next;
12307 : 3993 : while (c)
12308 : : {
12309 : 2003 : switch (c->op)
12310 : : {
12311 : 1879 : case EXEC_ASSIGN:
12312 : 1879 : case EXEC_POINTER_ASSIGN:
12313 : 1879 : gfc_resolve_assign_in_forall (c, nvar, var_expr);
12314 : :
12315 : 1879 : if (c->op == EXEC_ASSIGN
12316 : 1879 : && gfc_may_be_finalized (c->expr1->ts))
12317 : 0 : c->expr1->must_finalize = 1;
12318 : :
12319 : : break;
12320 : :
12321 : 0 : case EXEC_ASSIGN_CALL:
12322 : 0 : resolve_call (c);
12323 : 0 : break;
12324 : :
12325 : : /* Because the gfc_resolve_blocks() will handle the nested FORALL,
12326 : : there is no need to handle it here. */
12327 : : case EXEC_FORALL:
12328 : : break;
12329 : 46 : case EXEC_WHERE:
12330 : 46 : gfc_resolve_where_code_in_forall(c, nvar, var_expr);
12331 : 46 : break;
12332 : : default:
12333 : : break;
12334 : : }
12335 : : /* The next statement in the FORALL body. */
12336 : 2003 : c = c->next;
12337 : : }
12338 : 1990 : }
12339 : :
12340 : :
12341 : : /* Counts the number of iterators needed inside a forall construct, including
12342 : : nested forall constructs. This is used to allocate the needed memory
12343 : : in gfc_resolve_forall. */
12344 : :
12345 : : static int
12346 : 1990 : gfc_count_forall_iterators (gfc_code *code)
12347 : : {
12348 : 1990 : int max_iters, sub_iters, current_iters;
12349 : 1990 : gfc_forall_iterator *fa;
12350 : :
12351 : 1990 : gcc_assert(code->op == EXEC_FORALL);
12352 : 1990 : max_iters = 0;
12353 : 1990 : current_iters = 0;
12354 : :
12355 : 5878 : for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
12356 : 3888 : current_iters ++;
12357 : :
12358 : 1990 : code = code->block->next;
12359 : :
12360 : 3993 : while (code)
12361 : : {
12362 : 2003 : if (code->op == EXEC_FORALL)
12363 : : {
12364 : 78 : sub_iters = gfc_count_forall_iterators (code);
12365 : 78 : if (sub_iters > max_iters)
12366 : 2003 : max_iters = sub_iters;
12367 : : }
12368 : 2003 : code = code->next;
12369 : : }
12370 : :
12371 : 1990 : return current_iters + max_iters;
12372 : : }
12373 : :
12374 : :
12375 : : /* Given a FORALL construct, first resolve the FORALL iterator, then call
12376 : : gfc_resolve_forall_body to resolve the FORALL body. */
12377 : :
12378 : : static void
12379 : 1990 : gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
12380 : : {
12381 : 1990 : static gfc_expr **var_expr;
12382 : 1990 : static int total_var = 0;
12383 : 1990 : static int nvar = 0;
12384 : 1990 : int i, old_nvar, tmp;
12385 : 1990 : gfc_forall_iterator *fa;
12386 : :
12387 : 1990 : old_nvar = nvar;
12388 : :
12389 : 1990 : if (!gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
12390 : : return;
12391 : :
12392 : : /* Start to resolve a FORALL construct */
12393 : 1990 : if (forall_save == 0)
12394 : : {
12395 : : /* Count the total number of FORALL indices in the nested FORALL
12396 : : construct in order to allocate the VAR_EXPR with proper size. */
12397 : 1912 : total_var = gfc_count_forall_iterators (code);
12398 : :
12399 : : /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
12400 : 1912 : var_expr = XCNEWVEC (gfc_expr *, total_var);
12401 : : }
12402 : :
12403 : : /* The information about FORALL iterator, including FORALL indices start, end
12404 : : and stride. An outer FORALL indice cannot appear in start, end or stride. */
12405 : 5878 : for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
12406 : : {
12407 : : /* Fortran 20008: C738 (R753). */
12408 : 3888 : if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
12409 : : {
12410 : 2 : gfc_error ("FORALL index-name at %L must be a scalar variable "
12411 : : "of type integer", &fa->var->where);
12412 : 2 : continue;
12413 : : }
12414 : :
12415 : : /* Check if any outer FORALL index name is the same as the current
12416 : : one. */
12417 : 6859 : for (i = 0; i < nvar; i++)
12418 : : {
12419 : 2973 : if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
12420 : 0 : gfc_error ("An outer FORALL construct already has an index "
12421 : : "with this name %L", &fa->var->where);
12422 : : }
12423 : :
12424 : : /* Record the current FORALL index. */
12425 : 3886 : var_expr[nvar] = gfc_copy_expr (fa->var);
12426 : :
12427 : 3886 : nvar++;
12428 : :
12429 : : /* No memory leak. */
12430 : 3886 : gcc_assert (nvar <= total_var);
12431 : : }
12432 : :
12433 : : /* Resolve the FORALL body. */
12434 : 1990 : gfc_resolve_forall_body (code, nvar, var_expr);
12435 : :
12436 : : /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
12437 : 1990 : gfc_resolve_blocks (code->block, ns);
12438 : :
12439 : 1990 : tmp = nvar;
12440 : 1990 : nvar = old_nvar;
12441 : : /* Free only the VAR_EXPRs allocated in this frame. */
12442 : 5876 : for (i = nvar; i < tmp; i++)
12443 : 3886 : gfc_free_expr (var_expr[i]);
12444 : :
12445 : 1990 : if (nvar == 0)
12446 : : {
12447 : : /* We are in the outermost FORALL construct. */
12448 : 1912 : gcc_assert (forall_save == 0);
12449 : :
12450 : : /* VAR_EXPR is not needed any more. */
12451 : 1912 : free (var_expr);
12452 : 1912 : total_var = 0;
12453 : : }
12454 : : }
12455 : :
12456 : :
12457 : : /* Resolve a BLOCK construct statement. */
12458 : :
12459 : : static void
12460 : 7671 : resolve_block_construct (gfc_code* code)
12461 : : {
12462 : 7671 : gfc_namespace *ns = code->ext.block.ns;
12463 : :
12464 : : /* For an ASSOCIATE block, the associations (and their targets) will be
12465 : : resolved by gfc_resolve_symbol, during resolution of the BLOCK's
12466 : : namespace. */
12467 : 7671 : gfc_resolve (ns);
12468 : 0 : }
12469 : :
12470 : :
12471 : : /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
12472 : : DO code nodes. */
12473 : :
12474 : : void
12475 : 323820 : gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
12476 : : {
12477 : 323820 : bool t;
12478 : :
12479 : 658842 : for (; b; b = b->block)
12480 : : {
12481 : 335022 : t = gfc_resolve_expr (b->expr1);
12482 : 335022 : if (!gfc_resolve_expr (b->expr2))
12483 : 0 : t = false;
12484 : :
12485 : 335022 : switch (b->op)
12486 : : {
12487 : 231280 : case EXEC_IF:
12488 : 231280 : if (t && b->expr1 != NULL
12489 : 227178 : && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
12490 : 0 : gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
12491 : : &b->expr1->where);
12492 : : break;
12493 : :
12494 : 761 : case EXEC_WHERE:
12495 : 761 : if (t
12496 : 761 : && b->expr1 != NULL
12497 : 628 : && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
12498 : 0 : gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
12499 : : &b->expr1->where);
12500 : : break;
12501 : :
12502 : 76 : case EXEC_GOTO:
12503 : 76 : resolve_branch (b->label1, b);
12504 : 76 : break;
12505 : :
12506 : 0 : case EXEC_BLOCK:
12507 : 0 : resolve_block_construct (b);
12508 : 0 : break;
12509 : :
12510 : : case EXEC_SELECT:
12511 : : case EXEC_SELECT_TYPE:
12512 : : case EXEC_SELECT_RANK:
12513 : : case EXEC_FORALL:
12514 : : case EXEC_DO:
12515 : : case EXEC_DO_WHILE:
12516 : : case EXEC_DO_CONCURRENT:
12517 : : case EXEC_CRITICAL:
12518 : : case EXEC_READ:
12519 : : case EXEC_WRITE:
12520 : : case EXEC_IOLENGTH:
12521 : : case EXEC_WAIT:
12522 : : break;
12523 : :
12524 : 2697 : case EXEC_OMP_ATOMIC:
12525 : 2697 : case EXEC_OACC_ATOMIC:
12526 : 2697 : {
12527 : : /* Verify this before calling gfc_resolve_code, which might
12528 : : change it. */
12529 : 2697 : gcc_assert (b->op == EXEC_OMP_ATOMIC
12530 : : || (b->next && b->next->op == EXEC_ASSIGN));
12531 : : }
12532 : : break;
12533 : :
12534 : : case EXEC_OACC_PARALLEL_LOOP:
12535 : : case EXEC_OACC_PARALLEL:
12536 : : case EXEC_OACC_KERNELS_LOOP:
12537 : : case EXEC_OACC_KERNELS:
12538 : : case EXEC_OACC_SERIAL_LOOP:
12539 : : case EXEC_OACC_SERIAL:
12540 : : case EXEC_OACC_DATA:
12541 : : case EXEC_OACC_HOST_DATA:
12542 : : case EXEC_OACC_LOOP:
12543 : : case EXEC_OACC_UPDATE:
12544 : : case EXEC_OACC_WAIT:
12545 : : case EXEC_OACC_CACHE:
12546 : : case EXEC_OACC_ENTER_DATA:
12547 : : case EXEC_OACC_EXIT_DATA:
12548 : : case EXEC_OACC_ROUTINE:
12549 : : case EXEC_OMP_ALLOCATE:
12550 : : case EXEC_OMP_ALLOCATORS:
12551 : : case EXEC_OMP_ASSUME:
12552 : : case EXEC_OMP_CRITICAL:
12553 : : case EXEC_OMP_DISPATCH:
12554 : : case EXEC_OMP_DISTRIBUTE:
12555 : : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
12556 : : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
12557 : : case EXEC_OMP_DISTRIBUTE_SIMD:
12558 : : case EXEC_OMP_DO:
12559 : : case EXEC_OMP_DO_SIMD:
12560 : : case EXEC_OMP_ERROR:
12561 : : case EXEC_OMP_LOOP:
12562 : : case EXEC_OMP_MASKED:
12563 : : case EXEC_OMP_MASKED_TASKLOOP:
12564 : : case EXEC_OMP_MASKED_TASKLOOP_SIMD:
12565 : : case EXEC_OMP_MASTER:
12566 : : case EXEC_OMP_MASTER_TASKLOOP:
12567 : : case EXEC_OMP_MASTER_TASKLOOP_SIMD:
12568 : : case EXEC_OMP_ORDERED:
12569 : : case EXEC_OMP_PARALLEL:
12570 : : case EXEC_OMP_PARALLEL_DO:
12571 : : case EXEC_OMP_PARALLEL_DO_SIMD:
12572 : : case EXEC_OMP_PARALLEL_LOOP:
12573 : : case EXEC_OMP_PARALLEL_MASKED:
12574 : : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
12575 : : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
12576 : : case EXEC_OMP_PARALLEL_MASTER:
12577 : : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
12578 : : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
12579 : : case EXEC_OMP_PARALLEL_SECTIONS:
12580 : : case EXEC_OMP_PARALLEL_WORKSHARE:
12581 : : case EXEC_OMP_SECTIONS:
12582 : : case EXEC_OMP_SIMD:
12583 : : case EXEC_OMP_SCOPE:
12584 : : case EXEC_OMP_SINGLE:
12585 : : case EXEC_OMP_TARGET:
12586 : : case EXEC_OMP_TARGET_DATA:
12587 : : case EXEC_OMP_TARGET_ENTER_DATA:
12588 : : case EXEC_OMP_TARGET_EXIT_DATA:
12589 : : case EXEC_OMP_TARGET_PARALLEL:
12590 : : case EXEC_OMP_TARGET_PARALLEL_DO:
12591 : : case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
12592 : : case EXEC_OMP_TARGET_PARALLEL_LOOP:
12593 : : case EXEC_OMP_TARGET_SIMD:
12594 : : case EXEC_OMP_TARGET_TEAMS:
12595 : : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
12596 : : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
12597 : : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12598 : : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
12599 : : case EXEC_OMP_TARGET_TEAMS_LOOP:
12600 : : case EXEC_OMP_TARGET_UPDATE:
12601 : : case EXEC_OMP_TASK:
12602 : : case EXEC_OMP_TASKGROUP:
12603 : : case EXEC_OMP_TASKLOOP:
12604 : : case EXEC_OMP_TASKLOOP_SIMD:
12605 : : case EXEC_OMP_TASKWAIT:
12606 : : case EXEC_OMP_TASKYIELD:
12607 : : case EXEC_OMP_TEAMS:
12608 : : case EXEC_OMP_TEAMS_DISTRIBUTE:
12609 : : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
12610 : : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12611 : : case EXEC_OMP_TEAMS_LOOP:
12612 : : case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
12613 : : case EXEC_OMP_TILE:
12614 : : case EXEC_OMP_UNROLL:
12615 : : case EXEC_OMP_WORKSHARE:
12616 : : break;
12617 : :
12618 : 0 : default:
12619 : 0 : gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
12620 : : }
12621 : :
12622 : 335022 : gfc_resolve_code (b->next, ns);
12623 : : }
12624 : 323820 : }
12625 : :
12626 : : bool
12627 : 0 : caf_possible_reallocate (gfc_expr *e)
12628 : : {
12629 : 0 : symbol_attribute caf_attr;
12630 : 0 : gfc_ref *last_arr_ref = nullptr;
12631 : :
12632 : 0 : caf_attr = gfc_caf_attr (e);
12633 : 0 : if (!caf_attr.codimension || !caf_attr.allocatable || !caf_attr.dimension)
12634 : : return false;
12635 : :
12636 : : /* Only full array refs can indicate a needed reallocation. */
12637 : 0 : for (gfc_ref *ref = e->ref; ref; ref = ref->next)
12638 : 0 : if (ref->type == REF_ARRAY && ref->u.ar.dimen)
12639 : 0 : last_arr_ref = ref;
12640 : :
12641 : 0 : return last_arr_ref && last_arr_ref->u.ar.type == AR_FULL;
12642 : : }
12643 : :
12644 : : /* Does everything to resolve an ordinary assignment. Returns true
12645 : : if this is an interface assignment. */
12646 : : static bool
12647 : 279978 : resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
12648 : : {
12649 : 279978 : bool rval = false;
12650 : 279978 : gfc_expr *lhs;
12651 : 279978 : gfc_expr *rhs;
12652 : 279978 : int n;
12653 : 279978 : gfc_ref *ref;
12654 : 279978 : symbol_attribute attr;
12655 : :
12656 : 279978 : if (gfc_extend_assign (code, ns))
12657 : : {
12658 : 776 : gfc_expr** rhsptr;
12659 : :
12660 : 776 : if (code->op == EXEC_ASSIGN_CALL)
12661 : : {
12662 : 360 : lhs = code->ext.actual->expr;
12663 : 360 : rhsptr = &code->ext.actual->next->expr;
12664 : : }
12665 : : else
12666 : : {
12667 : 416 : gfc_actual_arglist* args;
12668 : 416 : gfc_typebound_proc* tbp;
12669 : :
12670 : 416 : gcc_assert (code->op == EXEC_COMPCALL);
12671 : :
12672 : 416 : args = code->expr1->value.compcall.actual;
12673 : 416 : lhs = args->expr;
12674 : 416 : rhsptr = &args->next->expr;
12675 : :
12676 : 416 : tbp = code->expr1->value.compcall.tbp;
12677 : 416 : gcc_assert (!tbp->is_generic);
12678 : : }
12679 : :
12680 : : /* Make a temporary rhs when there is a default initializer
12681 : : and rhs is the same symbol as the lhs. */
12682 : 776 : if ((*rhsptr)->expr_type == EXPR_VARIABLE
12683 : 392 : && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
12684 : 339 : && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
12685 : 967 : && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
12686 : 24 : *rhsptr = gfc_get_parentheses (*rhsptr);
12687 : :
12688 : 776 : return true;
12689 : : }
12690 : :
12691 : 279202 : lhs = code->expr1;
12692 : 279202 : rhs = code->expr2;
12693 : :
12694 : 279202 : if ((lhs->symtree->n.sym->ts.type == BT_DERIVED
12695 : 260666 : || lhs->symtree->n.sym->ts.type == BT_CLASS)
12696 : 20966 : && !lhs->symtree->n.sym->attr.proc_pointer
12697 : 300168 : && gfc_expr_attr (lhs).proc_pointer)
12698 : : {
12699 : 1 : gfc_error ("Variable in the ordinary assignment at %L is a procedure "
12700 : : "pointer component",
12701 : : &lhs->where);
12702 : 1 : return false;
12703 : : }
12704 : :
12705 : 327726 : if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
12706 : 245964 : && rhs->ts.type == BT_CHARACTER
12707 : 279594 : && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
12708 : : {
12709 : : /* Use of -fdec-char-conversions allows assignment of character data
12710 : : to non-character variables. This not permitted for nonconstant
12711 : : strings. */
12712 : 29 : gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
12713 : : gfc_typename (lhs), &rhs->where);
12714 : 29 : return false;
12715 : : }
12716 : :
12717 : 279172 : if (flag_unsigned && gfc_invalid_unsigned_ops (lhs, rhs))
12718 : : {
12719 : 0 : gfc_error ("Cannot assign %s to %s at %L", gfc_typename (rhs),
12720 : : gfc_typename (lhs), &rhs->where);
12721 : 0 : return false;
12722 : : }
12723 : :
12724 : : /* Handle the case of a BOZ literal on the RHS. */
12725 : 279172 : if (rhs->ts.type == BT_BOZ)
12726 : : {
12727 : 3 : if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
12728 : : "statement value nor an actual argument of "
12729 : : "INT/REAL/DBLE/CMPLX intrinsic subprogram",
12730 : : &rhs->where))
12731 : : return false;
12732 : :
12733 : 1 : switch (lhs->ts.type)
12734 : : {
12735 : 0 : case BT_INTEGER:
12736 : 0 : if (!gfc_boz2int (rhs, lhs->ts.kind))
12737 : : return false;
12738 : : break;
12739 : 1 : case BT_REAL:
12740 : 1 : if (!gfc_boz2real (rhs, lhs->ts.kind))
12741 : : return false;
12742 : : break;
12743 : 0 : default:
12744 : 0 : gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
12745 : 0 : return false;
12746 : : }
12747 : : }
12748 : :
12749 : 279170 : if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
12750 : : {
12751 : 64 : HOST_WIDE_INT llen = 0, rlen = 0;
12752 : 64 : if (lhs->ts.u.cl != NULL
12753 : 64 : && lhs->ts.u.cl->length != NULL
12754 : 53 : && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
12755 : 53 : llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
12756 : :
12757 : 64 : if (rhs->expr_type == EXPR_CONSTANT)
12758 : 26 : rlen = rhs->value.character.length;
12759 : :
12760 : 38 : else if (rhs->ts.u.cl != NULL
12761 : 38 : && rhs->ts.u.cl->length != NULL
12762 : 35 : && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
12763 : 35 : rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
12764 : :
12765 : 64 : if (rlen && llen && rlen > llen)
12766 : 28 : gfc_warning_now (OPT_Wcharacter_truncation,
12767 : : "CHARACTER expression will be truncated "
12768 : : "in assignment (%wd/%wd) at %L",
12769 : : llen, rlen, &code->loc);
12770 : : }
12771 : :
12772 : : /* Ensure that a vector index expression for the lvalue is evaluated
12773 : : to a temporary if the lvalue symbol is referenced in it. */
12774 : 279170 : if (lhs->rank)
12775 : : {
12776 : 106925 : for (ref = lhs->ref; ref; ref= ref->next)
12777 : 56662 : if (ref->type == REF_ARRAY)
12778 : : {
12779 : 127188 : for (n = 0; n < ref->u.ar.dimen; n++)
12780 : 75476 : if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
12781 : 75701 : && gfc_find_sym_in_expr (lhs->symtree->n.sym,
12782 : 225 : ref->u.ar.start[n]))
12783 : 14 : ref->u.ar.start[n]
12784 : 14 : = gfc_get_parentheses (ref->u.ar.start[n]);
12785 : : }
12786 : : }
12787 : :
12788 : 279170 : if (gfc_pure (NULL))
12789 : : {
12790 : 3250 : if (lhs->ts.type == BT_DERIVED
12791 : 84 : && lhs->expr_type == EXPR_VARIABLE
12792 : 84 : && lhs->ts.u.derived->attr.pointer_comp
12793 : 4 : && rhs->expr_type == EXPR_VARIABLE
12794 : 3253 : && (gfc_impure_variable (rhs->symtree->n.sym)
12795 : 2 : || gfc_is_coindexed (rhs)))
12796 : : {
12797 : : /* F2008, C1283. */
12798 : 2 : if (gfc_is_coindexed (rhs))
12799 : 1 : gfc_error ("Coindexed expression at %L is assigned to "
12800 : : "a derived type variable with a POINTER "
12801 : : "component in a PURE procedure",
12802 : : &rhs->where);
12803 : : else
12804 : : /* F2008, C1283 (4). */
12805 : 1 : gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
12806 : : "shall not be used as the expr at %L of an intrinsic "
12807 : : "assignment statement in which the variable is of a "
12808 : : "derived type if the derived type has a pointer "
12809 : : "component at any level of component selection.",
12810 : : &rhs->where);
12811 : 2 : return rval;
12812 : : }
12813 : :
12814 : : /* Fortran 2008, C1283. */
12815 : 3248 : if (gfc_is_coindexed (lhs))
12816 : : {
12817 : 1 : gfc_error ("Assignment to coindexed variable at %L in a PURE "
12818 : : "procedure", &rhs->where);
12819 : 1 : return rval;
12820 : : }
12821 : : }
12822 : :
12823 : 279167 : if (gfc_implicit_pure (NULL))
12824 : : {
12825 : 7055 : if (lhs->expr_type == EXPR_VARIABLE
12826 : 7055 : && lhs->symtree->n.sym != gfc_current_ns->proc_name
12827 : 5034 : && lhs->symtree->n.sym->ns != gfc_current_ns)
12828 : 243 : gfc_unset_implicit_pure (NULL);
12829 : :
12830 : 7055 : if (lhs->ts.type == BT_DERIVED
12831 : 299 : && lhs->expr_type == EXPR_VARIABLE
12832 : 299 : && lhs->ts.u.derived->attr.pointer_comp
12833 : 7 : && rhs->expr_type == EXPR_VARIABLE
12834 : 7062 : && (gfc_impure_variable (rhs->symtree->n.sym)
12835 : 7 : || gfc_is_coindexed (rhs)))
12836 : 0 : gfc_unset_implicit_pure (NULL);
12837 : :
12838 : : /* Fortran 2008, C1283. */
12839 : 7055 : if (gfc_is_coindexed (lhs))
12840 : 0 : gfc_unset_implicit_pure (NULL);
12841 : : }
12842 : :
12843 : : /* F2008, 7.2.1.2. */
12844 : 279167 : attr = gfc_expr_attr (lhs);
12845 : 279167 : if (lhs->ts.type == BT_CLASS && attr.allocatable)
12846 : : {
12847 : 900 : if (attr.codimension)
12848 : : {
12849 : 1 : gfc_error ("Assignment to polymorphic coarray at %L is not "
12850 : : "permitted", &lhs->where);
12851 : 1 : return false;
12852 : : }
12853 : 899 : if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
12854 : : "polymorphic variable at %L", &lhs->where))
12855 : : return false;
12856 : 898 : if (!flag_realloc_lhs)
12857 : : {
12858 : 1 : gfc_error ("Assignment to an allocatable polymorphic variable at %L "
12859 : : "requires %<-frealloc-lhs%>", &lhs->where);
12860 : 1 : return false;
12861 : : }
12862 : : }
12863 : 278267 : else if (lhs->ts.type == BT_CLASS)
12864 : : {
12865 : 9 : gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
12866 : : "assignment at %L - check that there is a matching specific "
12867 : : "subroutine for %<=%> operator", &lhs->where);
12868 : 9 : return false;
12869 : : }
12870 : :
12871 : 279155 : bool lhs_coindexed = gfc_is_coindexed (lhs);
12872 : :
12873 : : /* F2008, Section 7.2.1.2. */
12874 : 279155 : if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
12875 : : {
12876 : 1 : gfc_error ("Coindexed variable must not have an allocatable ultimate "
12877 : : "component in assignment at %L", &lhs->where);
12878 : 1 : return false;
12879 : : }
12880 : :
12881 : : /* Assign the 'data' of a class object to a derived type. */
12882 : 279154 : if (lhs->ts.type == BT_DERIVED
12883 : 6695 : && rhs->ts.type == BT_CLASS
12884 : 137 : && rhs->expr_type != EXPR_ARRAY)
12885 : 131 : gfc_add_data_component (rhs);
12886 : :
12887 : : /* Make sure there is a vtable and, in particular, a _copy for the
12888 : : rhs type. */
12889 : 279154 : if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS)
12890 : 540 : gfc_find_vtab (&rhs->ts);
12891 : :
12892 : 279154 : gfc_check_assign (lhs, rhs, 1);
12893 : :
12894 : 279154 : return false;
12895 : : }
12896 : :
12897 : :
12898 : : /* Add a component reference onto an expression. */
12899 : :
12900 : : static void
12901 : 665 : add_comp_ref (gfc_expr *e, gfc_component *c)
12902 : : {
12903 : 665 : gfc_ref **ref;
12904 : 665 : ref = &(e->ref);
12905 : 889 : while (*ref)
12906 : 224 : ref = &((*ref)->next);
12907 : 665 : *ref = gfc_get_ref ();
12908 : 665 : (*ref)->type = REF_COMPONENT;
12909 : 665 : (*ref)->u.c.sym = e->ts.u.derived;
12910 : 665 : (*ref)->u.c.component = c;
12911 : 665 : e->ts = c->ts;
12912 : :
12913 : : /* Add a full array ref, as necessary. */
12914 : 665 : if (c->as)
12915 : : {
12916 : 84 : gfc_add_full_array_ref (e, c->as);
12917 : 84 : e->rank = c->as->rank;
12918 : 84 : e->corank = c->as->corank;
12919 : : }
12920 : 665 : }
12921 : :
12922 : :
12923 : : /* Build an assignment. Keep the argument 'op' for future use, so that
12924 : : pointer assignments can be made. */
12925 : :
12926 : : static gfc_code *
12927 : 898 : build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
12928 : : gfc_component *comp1, gfc_component *comp2, locus loc)
12929 : : {
12930 : 898 : gfc_code *this_code;
12931 : :
12932 : 898 : this_code = gfc_get_code (op);
12933 : 898 : this_code->next = NULL;
12934 : 898 : this_code->expr1 = gfc_copy_expr (expr1);
12935 : 898 : this_code->expr2 = gfc_copy_expr (expr2);
12936 : 898 : this_code->loc = loc;
12937 : 898 : if (comp1 && comp2)
12938 : : {
12939 : 288 : add_comp_ref (this_code->expr1, comp1);
12940 : 288 : add_comp_ref (this_code->expr2, comp2);
12941 : : }
12942 : :
12943 : 898 : return this_code;
12944 : : }
12945 : :
12946 : :
12947 : : /* Makes a temporary variable expression based on the characteristics of
12948 : : a given variable expression. */
12949 : :
12950 : : static gfc_expr*
12951 : 392 : get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
12952 : : {
12953 : 392 : static int serial = 0;
12954 : 392 : char name[GFC_MAX_SYMBOL_LEN];
12955 : 392 : gfc_symtree *tmp;
12956 : 392 : gfc_array_spec *as;
12957 : 392 : gfc_array_ref *aref;
12958 : 392 : gfc_ref *ref;
12959 : :
12960 : 392 : sprintf (name, GFC_PREFIX("DA%d"), serial++);
12961 : 392 : gfc_get_sym_tree (name, ns, &tmp, false);
12962 : 392 : gfc_add_type (tmp->n.sym, &e->ts, NULL);
12963 : :
12964 : 392 : if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
12965 : 0 : tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
12966 : : NULL,
12967 : 0 : e->value.character.length);
12968 : :
12969 : 392 : as = NULL;
12970 : 392 : ref = NULL;
12971 : 392 : aref = NULL;
12972 : :
12973 : : /* Obtain the arrayspec for the temporary. */
12974 : 392 : if (e->rank && e->expr_type != EXPR_ARRAY
12975 : : && e->expr_type != EXPR_FUNCTION
12976 : : && e->expr_type != EXPR_OP)
12977 : : {
12978 : 52 : aref = gfc_find_array_ref (e);
12979 : 52 : if (e->expr_type == EXPR_VARIABLE
12980 : 52 : && e->symtree->n.sym->as == aref->as)
12981 : : as = aref->as;
12982 : : else
12983 : : {
12984 : 0 : for (ref = e->ref; ref; ref = ref->next)
12985 : 0 : if (ref->type == REF_COMPONENT
12986 : 0 : && ref->u.c.component->as == aref->as)
12987 : : {
12988 : : as = aref->as;
12989 : : break;
12990 : : }
12991 : : }
12992 : : }
12993 : :
12994 : : /* Add the attributes and the arrayspec to the temporary. */
12995 : 392 : tmp->n.sym->attr = gfc_expr_attr (e);
12996 : 392 : tmp->n.sym->attr.function = 0;
12997 : 392 : tmp->n.sym->attr.proc_pointer = 0;
12998 : 392 : tmp->n.sym->attr.result = 0;
12999 : 392 : tmp->n.sym->attr.flavor = FL_VARIABLE;
13000 : 392 : tmp->n.sym->attr.dummy = 0;
13001 : 392 : tmp->n.sym->attr.use_assoc = 0;
13002 : 392 : tmp->n.sym->attr.intent = INTENT_UNKNOWN;
13003 : :
13004 : :
13005 : 392 : if (as)
13006 : : {
13007 : 52 : tmp->n.sym->as = gfc_copy_array_spec (as);
13008 : 52 : if (!ref)
13009 : 52 : ref = e->ref;
13010 : 52 : if (as->type == AS_DEFERRED)
13011 : 46 : tmp->n.sym->attr.allocatable = 1;
13012 : : }
13013 : 340 : else if ((e->rank || e->corank)
13014 : 48 : && (e->expr_type == EXPR_ARRAY || e->expr_type == EXPR_FUNCTION
13015 : 0 : || e->expr_type == EXPR_OP))
13016 : : {
13017 : 48 : tmp->n.sym->as = gfc_get_array_spec ();
13018 : 48 : tmp->n.sym->as->type = AS_DEFERRED;
13019 : 48 : tmp->n.sym->as->rank = e->rank;
13020 : 48 : tmp->n.sym->as->corank = e->corank;
13021 : 48 : tmp->n.sym->attr.allocatable = 1;
13022 : 48 : tmp->n.sym->attr.dimension = e->rank ? 1 : 0;
13023 : 96 : tmp->n.sym->attr.codimension = e->corank ? 1 : 0;
13024 : : }
13025 : : else
13026 : 292 : tmp->n.sym->attr.dimension = 0;
13027 : :
13028 : 392 : gfc_set_sym_referenced (tmp->n.sym);
13029 : 392 : gfc_commit_symbol (tmp->n.sym);
13030 : 392 : e = gfc_lval_expr_from_sym (tmp->n.sym);
13031 : :
13032 : : /* Should the lhs be a section, use its array ref for the
13033 : : temporary expression. */
13034 : 392 : if (aref && aref->type != AR_FULL)
13035 : : {
13036 : 6 : gfc_free_ref_list (e->ref);
13037 : 6 : e->ref = gfc_copy_ref (ref);
13038 : : }
13039 : 392 : return e;
13040 : : }
13041 : :
13042 : :
13043 : : /* Add one line of code to the code chain, making sure that 'head' and
13044 : : 'tail' are appropriately updated. */
13045 : :
13046 : : static void
13047 : 656 : add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
13048 : : {
13049 : 656 : gcc_assert (this_code);
13050 : 656 : if (*head == NULL)
13051 : 308 : *head = *tail = *this_code;
13052 : : else
13053 : 348 : *tail = gfc_append_code (*tail, *this_code);
13054 : 656 : *this_code = NULL;
13055 : 656 : }
13056 : :
13057 : :
13058 : : /* Generate a final call from a variable expression */
13059 : :
13060 : : static void
13061 : 81 : generate_final_call (gfc_expr *tmp_expr, gfc_code **head, gfc_code **tail)
13062 : : {
13063 : 81 : gfc_code *this_code;
13064 : 81 : gfc_expr *final_expr = NULL;
13065 : 81 : gfc_expr *size_expr;
13066 : 81 : gfc_expr *fini_coarray;
13067 : :
13068 : 81 : gcc_assert (tmp_expr->expr_type == EXPR_VARIABLE);
13069 : 81 : if (!gfc_is_finalizable (tmp_expr->ts.u.derived, &final_expr) || !final_expr)
13070 : 75 : return;
13071 : :
13072 : : /* Now generate the finalizer call. */
13073 : 6 : this_code = gfc_get_code (EXEC_CALL);
13074 : 6 : this_code->symtree = final_expr->symtree;
13075 : 6 : this_code->resolved_sym = final_expr->symtree->n.sym;
13076 : :
13077 : : //* Expression to be finalized */
13078 : 6 : this_code->ext.actual = gfc_get_actual_arglist ();
13079 : 6 : this_code->ext.actual->expr = gfc_copy_expr (tmp_expr);
13080 : :
13081 : : /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE. */
13082 : 6 : this_code->ext.actual->next = gfc_get_actual_arglist ();
13083 : 6 : size_expr = gfc_get_expr ();
13084 : 6 : size_expr->where = gfc_current_locus;
13085 : 6 : size_expr->expr_type = EXPR_OP;
13086 : 6 : size_expr->value.op.op = INTRINSIC_DIVIDE;
13087 : 6 : size_expr->value.op.op1
13088 : 12 : = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_STORAGE_SIZE,
13089 : : "storage_size", gfc_current_locus, 2,
13090 : 6 : gfc_lval_expr_from_sym (tmp_expr->symtree->n.sym),
13091 : : gfc_get_int_expr (gfc_index_integer_kind,
13092 : : NULL, 0));
13093 : 6 : size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
13094 : : gfc_character_storage_size);
13095 : 6 : size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
13096 : 6 : size_expr->ts = size_expr->value.op.op1->ts;
13097 : 6 : this_code->ext.actual->next->expr = size_expr;
13098 : :
13099 : : /* fini_coarray */
13100 : 6 : this_code->ext.actual->next->next = gfc_get_actual_arglist ();
13101 : 6 : fini_coarray = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
13102 : : &tmp_expr->where);
13103 : 6 : fini_coarray->value.logical = (int)gfc_expr_attr (tmp_expr).codimension;
13104 : 6 : this_code->ext.actual->next->next->expr = fini_coarray;
13105 : :
13106 : 6 : add_code_to_chain (&this_code, head, tail);
13107 : :
13108 : : }
13109 : :
13110 : : /* Counts the potential number of part array references that would
13111 : : result from resolution of typebound defined assignments. */
13112 : :
13113 : :
13114 : : static int
13115 : 243 : nonscalar_typebound_assign (gfc_symbol *derived, int depth)
13116 : : {
13117 : 243 : gfc_component *c;
13118 : 243 : int c_depth = 0, t_depth;
13119 : :
13120 : 584 : for (c= derived->components; c; c = c->next)
13121 : : {
13122 : 341 : if ((!gfc_bt_struct (c->ts.type)
13123 : 261 : || c->attr.pointer
13124 : 261 : || c->attr.allocatable
13125 : 260 : || c->attr.proc_pointer_comp
13126 : 260 : || c->attr.class_pointer
13127 : 260 : || c->attr.proc_pointer)
13128 : 81 : && !c->attr.defined_assign_comp)
13129 : 81 : continue;
13130 : :
13131 : 260 : if (c->as && c_depth == 0)
13132 : 260 : c_depth = 1;
13133 : :
13134 : 260 : if (c->ts.u.derived->attr.defined_assign_comp)
13135 : 110 : t_depth = nonscalar_typebound_assign (c->ts.u.derived,
13136 : : c->as ? 1 : 0);
13137 : : else
13138 : : t_depth = 0;
13139 : :
13140 : 260 : c_depth = t_depth > c_depth ? t_depth : c_depth;
13141 : : }
13142 : 243 : return depth + c_depth;
13143 : : }
13144 : :
13145 : :
13146 : : /* Implement 10.2.1.3 paragraph 13 of the F18 standard:
13147 : : "An intrinsic assignment where the variable is of derived type is performed
13148 : : as if each component of the variable were assigned from the corresponding
13149 : : component of expr using pointer assignment (10.2.2) for each pointer
13150 : : component, defined assignment for each nonpointer nonallocatable component
13151 : : of a type that has a type-bound defined assignment consistent with the
13152 : : component, intrinsic assignment for each other nonpointer nonallocatable
13153 : : component, and intrinsic assignment for each allocated coarray component.
13154 : : For unallocated coarray components, the corresponding component of the
13155 : : variable shall be unallocated. For a noncoarray allocatable component the
13156 : : following sequence of operations is applied.
13157 : : (1) If the component of the variable is allocated, it is deallocated.
13158 : : (2) If the component of the value of expr is allocated, the
13159 : : corresponding component of the variable is allocated with the same
13160 : : dynamic type and type parameters as the component of the value of
13161 : : expr. If it is an array, it is allocated with the same bounds. The
13162 : : value of the component of the value of expr is then assigned to the
13163 : : corresponding component of the variable using defined assignment if
13164 : : the declared type of the component has a type-bound defined
13165 : : assignment consistent with the component, and intrinsic assignment
13166 : : for the dynamic type of that component otherwise."
13167 : :
13168 : : The pointer assignments are taken care of by the intrinsic assignment of the
13169 : : structure itself. This function recursively adds defined assignments where
13170 : : required. The recursion is accomplished by calling gfc_resolve_code.
13171 : :
13172 : : When the lhs in a defined assignment has intent INOUT or is intent OUT
13173 : : and the component of 'var' is finalizable, we need a temporary for the
13174 : : lhs. In pseudo-code for an assignment var = expr:
13175 : :
13176 : : ! Confine finalization of temporaries, as far as possible.
13177 : : Enclose the code for the assignment in a block
13178 : : ! Only call function 'expr' once.
13179 : : #if ('expr is not a constant or an variable)
13180 : : temp_expr = expr
13181 : : expr = temp_x
13182 : : ! Do the intrinsic assignment
13183 : : #if typeof ('var') has a typebound final subroutine
13184 : : finalize (var)
13185 : : var = expr
13186 : : ! Now do the component assignments
13187 : : #do over derived type components [%cmp]
13188 : : #if (cmp is a pointer of any kind)
13189 : : continue
13190 : : build the assignment
13191 : : resolve the code
13192 : : #if the code is a typebound assignment
13193 : : #if (arg1 is INOUT or finalizable OUT && !t1)
13194 : : t1 = var
13195 : : arg1 = t1
13196 : : deal with allocatation or not of var and this component
13197 : : #elseif the code is an assignment by itself
13198 : : #if this component does not need finalization
13199 : : delete code and continue
13200 : : #else
13201 : : remove the leading assignment
13202 : : #endif
13203 : : commit the code
13204 : : #if (t1 and (arg1 is INOUT or finalizable OUT))
13205 : : var%cmp = t1%cmp
13206 : : #enddo
13207 : : put all code chunks involving t1 to the top of the generated code
13208 : : insert the generated block in place of the original code
13209 : : */
13210 : :
13211 : : static bool
13212 : 381 : is_finalizable_type (gfc_typespec ts)
13213 : : {
13214 : 381 : gfc_component *c;
13215 : :
13216 : 381 : if (ts.type != BT_DERIVED)
13217 : : return false;
13218 : :
13219 : : /* (1) Check for FINAL subroutines. */
13220 : 381 : if (ts.u.derived->f2k_derived && ts.u.derived->f2k_derived->finalizers)
13221 : : return true;
13222 : :
13223 : : /* (2) Check for components of finalizable type. */
13224 : 809 : for (c = ts.u.derived->components; c; c = c->next)
13225 : 470 : if (c->ts.type == BT_DERIVED
13226 : 243 : && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable
13227 : 242 : && c->ts.u.derived->f2k_derived
13228 : 242 : && c->ts.u.derived->f2k_derived->finalizers)
13229 : : return true;
13230 : :
13231 : : return false;
13232 : : }
13233 : :
13234 : : /* The temporary assignments have to be put on top of the additional
13235 : : code to avoid the result being changed by the intrinsic assignment.
13236 : : */
13237 : : static int component_assignment_level = 0;
13238 : : static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
13239 : : static bool finalizable_comp;
13240 : :
13241 : : static void
13242 : 188 : generate_component_assignments (gfc_code **code, gfc_namespace *ns)
13243 : : {
13244 : 188 : gfc_component *comp1, *comp2;
13245 : 188 : gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
13246 : 188 : gfc_code *tmp_code = NULL;
13247 : 188 : gfc_expr *t1 = NULL;
13248 : 188 : gfc_expr *tmp_expr = NULL;
13249 : 188 : int error_count, depth;
13250 : 188 : bool finalizable_lhs;
13251 : :
13252 : 188 : gfc_get_errors (NULL, &error_count);
13253 : :
13254 : : /* Filter out continuing processing after an error. */
13255 : 188 : if (error_count
13256 : 188 : || (*code)->expr1->ts.type != BT_DERIVED
13257 : 188 : || (*code)->expr2->ts.type != BT_DERIVED)
13258 : 140 : return;
13259 : :
13260 : : /* TODO: Handle more than one part array reference in assignments. */
13261 : 188 : depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
13262 : 188 : (*code)->expr1->rank ? 1 : 0);
13263 : 188 : if (depth > 1)
13264 : : {
13265 : 6 : gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
13266 : : "done because multiple part array references would "
13267 : : "occur in intermediate expressions.", &(*code)->loc);
13268 : 6 : return;
13269 : : }
13270 : :
13271 : 182 : if (!component_assignment_level)
13272 : 134 : finalizable_comp = true;
13273 : :
13274 : : /* Build a block so that function result temporaries are finalized
13275 : : locally on exiting the rather than enclosing scope. */
13276 : 182 : if (!component_assignment_level)
13277 : : {
13278 : 134 : ns = gfc_build_block_ns (ns);
13279 : 134 : tmp_code = gfc_get_code (EXEC_NOP);
13280 : 134 : *tmp_code = **code;
13281 : 134 : tmp_code->next = NULL;
13282 : 134 : (*code)->op = EXEC_BLOCK;
13283 : 134 : (*code)->ext.block.ns = ns;
13284 : 134 : (*code)->ext.block.assoc = NULL;
13285 : 134 : (*code)->expr1 = (*code)->expr2 = NULL;
13286 : 134 : ns->code = tmp_code;
13287 : 134 : code = &ns->code;
13288 : : }
13289 : :
13290 : 182 : component_assignment_level++;
13291 : :
13292 : 182 : finalizable_lhs = is_finalizable_type ((*code)->expr1->ts);
13293 : :
13294 : : /* Create a temporary so that functions get called only once. */
13295 : 182 : if ((*code)->expr2->expr_type != EXPR_VARIABLE
13296 : 182 : && (*code)->expr2->expr_type != EXPR_CONSTANT)
13297 : : {
13298 : : /* Assign the rhs to the temporary. */
13299 : 81 : tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
13300 : 81 : if (tmp_expr->symtree->n.sym->attr.pointer)
13301 : : {
13302 : : /* Use allocate on assignment for the sake of simplicity. The
13303 : : temporary must not take on the optional attribute. Assume
13304 : : that the assignment is guarded by a PRESENT condition if the
13305 : : lhs is optional. */
13306 : 25 : tmp_expr->symtree->n.sym->attr.pointer = 0;
13307 : 25 : tmp_expr->symtree->n.sym->attr.optional = 0;
13308 : 25 : tmp_expr->symtree->n.sym->attr.allocatable = 1;
13309 : : }
13310 : 162 : this_code = build_assignment (EXEC_ASSIGN,
13311 : : tmp_expr, (*code)->expr2,
13312 : 81 : NULL, NULL, (*code)->loc);
13313 : 81 : this_code->expr2->must_finalize = 1;
13314 : : /* Add the code and substitute the rhs expression. */
13315 : 81 : add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
13316 : 81 : gfc_free_expr ((*code)->expr2);
13317 : 81 : (*code)->expr2 = tmp_expr;
13318 : : }
13319 : :
13320 : : /* Do the intrinsic assignment. This is not needed if the lhs is one
13321 : : of the temporaries generated here, since the intrinsic assignment
13322 : : to the final result already does this. */
13323 : 182 : if ((*code)->expr1->symtree->n.sym->name[2] != '.')
13324 : : {
13325 : 182 : if (finalizable_lhs)
13326 : 18 : (*code)->expr1->must_finalize = 1;
13327 : 182 : this_code = build_assignment (EXEC_ASSIGN,
13328 : : (*code)->expr1, (*code)->expr2,
13329 : : NULL, NULL, (*code)->loc);
13330 : 182 : add_code_to_chain (&this_code, &head, &tail);
13331 : : }
13332 : :
13333 : 182 : comp1 = (*code)->expr1->ts.u.derived->components;
13334 : 182 : comp2 = (*code)->expr2->ts.u.derived->components;
13335 : :
13336 : 449 : for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
13337 : : {
13338 : 267 : bool inout = false;
13339 : 267 : bool finalizable_out = false;
13340 : :
13341 : : /* The intrinsic assignment does the right thing for pointers
13342 : : of all kinds and allocatable components. */
13343 : 267 : if (!gfc_bt_struct (comp1->ts.type)
13344 : 200 : || comp1->attr.pointer
13345 : 200 : || comp1->attr.allocatable
13346 : 199 : || comp1->attr.proc_pointer_comp
13347 : 199 : || comp1->attr.class_pointer
13348 : 199 : || comp1->attr.proc_pointer)
13349 : 68 : continue;
13350 : :
13351 : 398 : finalizable_comp = is_finalizable_type (comp1->ts)
13352 : 199 : && !finalizable_lhs;
13353 : :
13354 : : /* Make an assignment for this component. */
13355 : 398 : this_code = build_assignment (EXEC_ASSIGN,
13356 : : (*code)->expr1, (*code)->expr2,
13357 : 199 : comp1, comp2, (*code)->loc);
13358 : :
13359 : : /* Convert the assignment if there is a defined assignment for
13360 : : this type. Otherwise, using the call from gfc_resolve_code,
13361 : : recurse into its components. */
13362 : 199 : gfc_resolve_code (this_code, ns);
13363 : :
13364 : 199 : if (this_code->op == EXEC_ASSIGN_CALL)
13365 : : {
13366 : 144 : gfc_formal_arglist *dummy_args;
13367 : 144 : gfc_symbol *rsym;
13368 : : /* Check that there is a typebound defined assignment. If not,
13369 : : then this must be a module defined assignment. We cannot
13370 : : use the defined_assign_comp attribute here because it must
13371 : : be this derived type that has the defined assignment and not
13372 : : a parent type. */
13373 : 144 : if (!(comp1->ts.u.derived->f2k_derived
13374 : : && comp1->ts.u.derived->f2k_derived
13375 : 144 : ->tb_op[INTRINSIC_ASSIGN]))
13376 : : {
13377 : 1 : gfc_free_statements (this_code);
13378 : 1 : this_code = NULL;
13379 : 1 : continue;
13380 : : }
13381 : :
13382 : : /* If the first argument of the subroutine has intent INOUT
13383 : : a temporary must be generated and used instead. */
13384 : 143 : rsym = this_code->resolved_sym;
13385 : 143 : dummy_args = gfc_sym_get_dummy_args (rsym);
13386 : 268 : finalizable_out = gfc_may_be_finalized (comp1->ts)
13387 : 18 : && dummy_args
13388 : 161 : && dummy_args->sym->attr.intent == INTENT_OUT;
13389 : 286 : inout = dummy_args
13390 : 268 : && dummy_args->sym->attr.intent == INTENT_INOUT;
13391 : 72 : if ((inout || finalizable_out)
13392 : 89 : && !comp1->attr.allocatable)
13393 : : {
13394 : 89 : gfc_code *temp_code;
13395 : 89 : inout = true;
13396 : :
13397 : : /* Build the temporary required for the assignment and put
13398 : : it at the head of the generated code. */
13399 : 89 : if (!t1)
13400 : : {
13401 : 89 : gfc_namespace *tmp_ns = ns;
13402 : 89 : if (ns->parent && gfc_may_be_finalized (comp1->ts))
13403 : 18 : tmp_ns = (*code)->expr1->symtree->n.sym->ns;
13404 : 89 : t1 = get_temp_from_expr ((*code)->expr1, tmp_ns);
13405 : 89 : t1->symtree->n.sym->attr.artificial = 1;
13406 : 178 : temp_code = build_assignment (EXEC_ASSIGN,
13407 : : t1, (*code)->expr1,
13408 : 89 : NULL, NULL, (*code)->loc);
13409 : :
13410 : : /* For allocatable LHS, check whether it is allocated. Note
13411 : : that allocatable components with defined assignment are
13412 : : not yet support. See PR 57696. */
13413 : 89 : if ((*code)->expr1->symtree->n.sym->attr.allocatable)
13414 : : {
13415 : 24 : gfc_code *block;
13416 : 24 : gfc_expr *e =
13417 : 24 : gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
13418 : 24 : block = gfc_get_code (EXEC_IF);
13419 : 24 : block->block = gfc_get_code (EXEC_IF);
13420 : 24 : block->block->expr1
13421 : 48 : = gfc_build_intrinsic_call (ns,
13422 : : GFC_ISYM_ALLOCATED, "allocated",
13423 : 24 : (*code)->loc, 1, e);
13424 : 24 : block->block->next = temp_code;
13425 : 24 : temp_code = block;
13426 : : }
13427 : 89 : add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
13428 : : }
13429 : :
13430 : : /* Replace the first actual arg with the component of the
13431 : : temporary. */
13432 : 89 : gfc_free_expr (this_code->ext.actual->expr);
13433 : 89 : this_code->ext.actual->expr = gfc_copy_expr (t1);
13434 : 89 : add_comp_ref (this_code->ext.actual->expr, comp1);
13435 : :
13436 : : /* If the LHS variable is allocatable and wasn't allocated and
13437 : : the temporary is allocatable, pointer assign the address of
13438 : : the freshly allocated LHS to the temporary. */
13439 : 89 : if ((*code)->expr1->symtree->n.sym->attr.allocatable
13440 : 89 : && gfc_expr_attr ((*code)->expr1).allocatable)
13441 : : {
13442 : 18 : gfc_code *block;
13443 : 18 : gfc_expr *cond;
13444 : :
13445 : 18 : cond = gfc_get_expr ();
13446 : 18 : cond->ts.type = BT_LOGICAL;
13447 : 18 : cond->ts.kind = gfc_default_logical_kind;
13448 : 18 : cond->expr_type = EXPR_OP;
13449 : 18 : cond->where = (*code)->loc;
13450 : 18 : cond->value.op.op = INTRINSIC_NOT;
13451 : 18 : cond->value.op.op1 = gfc_build_intrinsic_call (ns,
13452 : : GFC_ISYM_ALLOCATED, "allocated",
13453 : 18 : (*code)->loc, 1, gfc_copy_expr (t1));
13454 : 18 : block = gfc_get_code (EXEC_IF);
13455 : 18 : block->block = gfc_get_code (EXEC_IF);
13456 : 18 : block->block->expr1 = cond;
13457 : 36 : block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
13458 : : t1, (*code)->expr1,
13459 : 18 : NULL, NULL, (*code)->loc);
13460 : 18 : add_code_to_chain (&block, &head, &tail);
13461 : : }
13462 : : }
13463 : : }
13464 : 55 : else if (this_code->op == EXEC_ASSIGN && !this_code->next)
13465 : : {
13466 : : /* Don't add intrinsic assignments since they are already
13467 : : effected by the intrinsic assignment of the structure, unless
13468 : : finalization is required. */
13469 : 7 : if (finalizable_comp)
13470 : 0 : this_code->expr1->must_finalize = 1;
13471 : : else
13472 : : {
13473 : 7 : gfc_free_statements (this_code);
13474 : 7 : this_code = NULL;
13475 : 7 : continue;
13476 : : }
13477 : : }
13478 : : else
13479 : : {
13480 : : /* Resolution has expanded an assignment of a derived type with
13481 : : defined assigned components. Remove the redundant, leading
13482 : : assignment. */
13483 : 48 : gcc_assert (this_code->op == EXEC_ASSIGN);
13484 : 48 : gfc_code *tmp = this_code;
13485 : 48 : this_code = this_code->next;
13486 : 48 : tmp->next = NULL;
13487 : 48 : gfc_free_statements (tmp);
13488 : : }
13489 : :
13490 : 191 : add_code_to_chain (&this_code, &head, &tail);
13491 : :
13492 : 191 : if (t1 && (inout || finalizable_out))
13493 : : {
13494 : : /* Transfer the value to the final result. */
13495 : 178 : this_code = build_assignment (EXEC_ASSIGN,
13496 : : (*code)->expr1, t1,
13497 : 89 : comp1, comp2, (*code)->loc);
13498 : 89 : this_code->expr1->must_finalize = 0;
13499 : 89 : add_code_to_chain (&this_code, &head, &tail);
13500 : : }
13501 : : }
13502 : :
13503 : : /* Put the temporary assignments at the top of the generated code. */
13504 : 182 : if (tmp_head && component_assignment_level == 1)
13505 : : {
13506 : 126 : gfc_append_code (tmp_head, head);
13507 : 126 : head = tmp_head;
13508 : 126 : tmp_head = tmp_tail = NULL;
13509 : : }
13510 : :
13511 : : /* If we did a pointer assignment - thus, we need to ensure that the LHS is
13512 : : not accidentally deallocated. Hence, nullify t1. */
13513 : 89 : if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
13514 : 271 : && gfc_expr_attr ((*code)->expr1).allocatable)
13515 : : {
13516 : 18 : gfc_code *block;
13517 : 18 : gfc_expr *cond;
13518 : 18 : gfc_expr *e;
13519 : :
13520 : 18 : e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
13521 : 18 : cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
13522 : 18 : (*code)->loc, 2, gfc_copy_expr (t1), e);
13523 : 18 : block = gfc_get_code (EXEC_IF);
13524 : 18 : block->block = gfc_get_code (EXEC_IF);
13525 : 18 : block->block->expr1 = cond;
13526 : 18 : block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
13527 : : t1, gfc_get_null_expr (&(*code)->loc),
13528 : 18 : NULL, NULL, (*code)->loc);
13529 : 18 : gfc_append_code (tail, block);
13530 : 18 : tail = block;
13531 : : }
13532 : :
13533 : 182 : component_assignment_level--;
13534 : :
13535 : : /* Make an explicit final call for the function result. */
13536 : 182 : if (tmp_expr)
13537 : 81 : generate_final_call (tmp_expr, &head, &tail);
13538 : :
13539 : 182 : if (tmp_code)
13540 : : {
13541 : 134 : ns->code = head;
13542 : 134 : return;
13543 : : }
13544 : :
13545 : : /* Now attach the remaining code chain to the input code. Step on
13546 : : to the end of the new code since resolution is complete. */
13547 : 48 : gcc_assert ((*code)->op == EXEC_ASSIGN);
13548 : 48 : tail->next = (*code)->next;
13549 : : /* Overwrite 'code' because this would place the intrinsic assignment
13550 : : before the temporary for the lhs is created. */
13551 : 48 : gfc_free_expr ((*code)->expr1);
13552 : 48 : gfc_free_expr ((*code)->expr2);
13553 : 48 : **code = *head;
13554 : 48 : if (head != tail)
13555 : 48 : free (head);
13556 : 48 : *code = tail;
13557 : : }
13558 : :
13559 : :
13560 : : /* F2008: Pointer function assignments are of the form:
13561 : : ptr_fcn (args) = expr
13562 : : This function breaks these assignments into two statements:
13563 : : temporary_pointer => ptr_fcn(args)
13564 : : temporary_pointer = expr */
13565 : :
13566 : : static bool
13567 : 280221 : resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
13568 : : {
13569 : 280221 : gfc_expr *tmp_ptr_expr;
13570 : 280221 : gfc_code *this_code;
13571 : 280221 : gfc_component *comp;
13572 : 280221 : gfc_symbol *s;
13573 : :
13574 : 280221 : if ((*code)->expr1->expr_type != EXPR_FUNCTION)
13575 : : return false;
13576 : :
13577 : : /* Even if standard does not support this feature, continue to build
13578 : : the two statements to avoid upsetting frontend_passes.c. */
13579 : 205 : gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
13580 : : "%L", &(*code)->loc);
13581 : :
13582 : 205 : comp = gfc_get_proc_ptr_comp ((*code)->expr1);
13583 : :
13584 : 205 : if (comp)
13585 : 6 : s = comp->ts.interface;
13586 : : else
13587 : 199 : s = (*code)->expr1->symtree->n.sym;
13588 : :
13589 : 205 : if (s == NULL || !s->result->attr.pointer)
13590 : : {
13591 : 5 : gfc_error ("The function result on the lhs of the assignment at "
13592 : : "%L must have the pointer attribute.",
13593 : 5 : &(*code)->expr1->where);
13594 : 5 : (*code)->op = EXEC_NOP;
13595 : 5 : return false;
13596 : : }
13597 : :
13598 : 200 : tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
13599 : :
13600 : : /* get_temp_from_expression is set up for ordinary assignments. To that
13601 : : end, where array bounds are not known, arrays are made allocatable.
13602 : : Change the temporary to a pointer here. */
13603 : 200 : tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
13604 : 200 : tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
13605 : 200 : tmp_ptr_expr->where = (*code)->loc;
13606 : :
13607 : : /* A new charlen is required to ensure that the variable string length
13608 : : is different to that of the original lhs for deferred results. */
13609 : 200 : if (s->result->ts.deferred && tmp_ptr_expr->ts.type == BT_CHARACTER)
13610 : : {
13611 : 60 : tmp_ptr_expr->ts.u.cl = gfc_get_charlen();
13612 : 60 : tmp_ptr_expr->ts.deferred = 1;
13613 : 60 : tmp_ptr_expr->ts.u.cl->next = gfc_current_ns->cl_list;
13614 : 60 : gfc_current_ns->cl_list = tmp_ptr_expr->ts.u.cl;
13615 : 60 : tmp_ptr_expr->symtree->n.sym->ts.u.cl = tmp_ptr_expr->ts.u.cl;
13616 : : }
13617 : :
13618 : 400 : this_code = build_assignment (EXEC_ASSIGN,
13619 : : tmp_ptr_expr, (*code)->expr2,
13620 : 200 : NULL, NULL, (*code)->loc);
13621 : 200 : this_code->next = (*code)->next;
13622 : 200 : (*code)->next = this_code;
13623 : 200 : (*code)->op = EXEC_POINTER_ASSIGN;
13624 : 200 : (*code)->expr2 = (*code)->expr1;
13625 : 200 : (*code)->expr1 = tmp_ptr_expr;
13626 : :
13627 : 200 : return true;
13628 : : }
13629 : :
13630 : :
13631 : : /* Deferred character length assignments from an operator expression
13632 : : require a temporary because the character length of the lhs can
13633 : : change in the course of the assignment. */
13634 : :
13635 : : static bool
13636 : 279202 : deferred_op_assign (gfc_code **code, gfc_namespace *ns)
13637 : : {
13638 : 279202 : gfc_expr *tmp_expr;
13639 : 279202 : gfc_code *this_code;
13640 : :
13641 : 279202 : if (!((*code)->expr1->ts.type == BT_CHARACTER
13642 : 25631 : && (*code)->expr1->ts.deferred && (*code)->expr1->rank
13643 : 740 : && (*code)->expr2->ts.type == BT_CHARACTER
13644 : 739 : && (*code)->expr2->expr_type == EXPR_OP))
13645 : : return false;
13646 : :
13647 : 34 : if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
13648 : : return false;
13649 : :
13650 : 28 : if (gfc_expr_attr ((*code)->expr1).pointer)
13651 : : return false;
13652 : :
13653 : 22 : tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
13654 : 22 : tmp_expr->where = (*code)->loc;
13655 : :
13656 : : /* A new charlen is required to ensure that the variable string
13657 : : length is different to that of the original lhs. */
13658 : 22 : tmp_expr->ts.u.cl = gfc_get_charlen();
13659 : 22 : tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
13660 : 22 : tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
13661 : 22 : (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
13662 : :
13663 : 22 : tmp_expr->symtree->n.sym->ts.deferred = 1;
13664 : :
13665 : 22 : this_code = build_assignment (EXEC_ASSIGN,
13666 : 22 : (*code)->expr1,
13667 : : gfc_copy_expr (tmp_expr),
13668 : : NULL, NULL, (*code)->loc);
13669 : :
13670 : 22 : (*code)->expr1 = tmp_expr;
13671 : :
13672 : 22 : this_code->next = (*code)->next;
13673 : 22 : (*code)->next = this_code;
13674 : :
13675 : 22 : return true;
13676 : : }
13677 : :
13678 : :
13679 : : /* Given a block of code, recursively resolve everything pointed to by this
13680 : : code block. */
13681 : :
13682 : : void
13683 : 661312 : gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
13684 : : {
13685 : 661312 : int omp_workshare_save;
13686 : 661312 : int forall_save, do_concurrent_save;
13687 : 661312 : code_stack frame;
13688 : 661312 : bool t;
13689 : :
13690 : 661312 : frame.prev = cs_base;
13691 : 661312 : frame.head = code;
13692 : 661312 : cs_base = &frame;
13693 : :
13694 : 661312 : find_reachable_labels (code);
13695 : :
13696 : 1768045 : for (; code; code = code->next)
13697 : : {
13698 : 1106734 : frame.current = code;
13699 : 1106734 : forall_save = forall_flag;
13700 : 1106734 : do_concurrent_save = gfc_do_concurrent_flag;
13701 : :
13702 : 1106734 : if (code->op == EXEC_FORALL)
13703 : : {
13704 : 1990 : forall_flag = 1;
13705 : 1990 : gfc_resolve_forall (code, ns, forall_save);
13706 : 1990 : forall_flag = 2;
13707 : : }
13708 : 1104744 : else if (code->op == EXEC_OMP_METADIRECTIVE)
13709 : 109 : for (gfc_omp_variant *variant
13710 : : = code->ext.omp_variants;
13711 : 359 : variant; variant = variant->next)
13712 : 250 : gfc_resolve_code (variant->code, ns);
13713 : 1104635 : else if (code->block)
13714 : : {
13715 : 321832 : omp_workshare_save = -1;
13716 : 321832 : switch (code->op)
13717 : : {
13718 : 10115 : case EXEC_OACC_PARALLEL_LOOP:
13719 : 10115 : case EXEC_OACC_PARALLEL:
13720 : 10115 : case EXEC_OACC_KERNELS_LOOP:
13721 : 10115 : case EXEC_OACC_KERNELS:
13722 : 10115 : case EXEC_OACC_SERIAL_LOOP:
13723 : 10115 : case EXEC_OACC_SERIAL:
13724 : 10115 : case EXEC_OACC_DATA:
13725 : 10115 : case EXEC_OACC_HOST_DATA:
13726 : 10115 : case EXEC_OACC_LOOP:
13727 : 10115 : gfc_resolve_oacc_blocks (code, ns);
13728 : 10115 : break;
13729 : 54 : case EXEC_OMP_PARALLEL_WORKSHARE:
13730 : 54 : omp_workshare_save = omp_workshare_flag;
13731 : 54 : omp_workshare_flag = 1;
13732 : 54 : gfc_resolve_omp_parallel_blocks (code, ns);
13733 : 54 : break;
13734 : 5905 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
13735 : 5905 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
13736 : 5905 : case EXEC_OMP_MASKED_TASKLOOP:
13737 : 5905 : case EXEC_OMP_MASKED_TASKLOOP_SIMD:
13738 : 5905 : case EXEC_OMP_MASTER_TASKLOOP:
13739 : 5905 : case EXEC_OMP_MASTER_TASKLOOP_SIMD:
13740 : 5905 : case EXEC_OMP_PARALLEL:
13741 : 5905 : case EXEC_OMP_PARALLEL_DO:
13742 : 5905 : case EXEC_OMP_PARALLEL_DO_SIMD:
13743 : 5905 : case EXEC_OMP_PARALLEL_LOOP:
13744 : 5905 : case EXEC_OMP_PARALLEL_MASKED:
13745 : 5905 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
13746 : 5905 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
13747 : 5905 : case EXEC_OMP_PARALLEL_MASTER:
13748 : 5905 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
13749 : 5905 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
13750 : 5905 : case EXEC_OMP_PARALLEL_SECTIONS:
13751 : 5905 : case EXEC_OMP_TARGET_PARALLEL:
13752 : 5905 : case EXEC_OMP_TARGET_PARALLEL_DO:
13753 : 5905 : case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
13754 : 5905 : case EXEC_OMP_TARGET_PARALLEL_LOOP:
13755 : 5905 : case EXEC_OMP_TARGET_TEAMS:
13756 : 5905 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
13757 : 5905 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
13758 : 5905 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
13759 : 5905 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
13760 : 5905 : case EXEC_OMP_TARGET_TEAMS_LOOP:
13761 : 5905 : case EXEC_OMP_TASK:
13762 : 5905 : case EXEC_OMP_TASKLOOP:
13763 : 5905 : case EXEC_OMP_TASKLOOP_SIMD:
13764 : 5905 : case EXEC_OMP_TEAMS:
13765 : 5905 : case EXEC_OMP_TEAMS_DISTRIBUTE:
13766 : 5905 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
13767 : 5905 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
13768 : 5905 : case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
13769 : 5905 : case EXEC_OMP_TEAMS_LOOP:
13770 : 5905 : omp_workshare_save = omp_workshare_flag;
13771 : 5905 : omp_workshare_flag = 0;
13772 : 5905 : gfc_resolve_omp_parallel_blocks (code, ns);
13773 : 5905 : break;
13774 : 3039 : case EXEC_OMP_DISTRIBUTE:
13775 : 3039 : case EXEC_OMP_DISTRIBUTE_SIMD:
13776 : 3039 : case EXEC_OMP_DO:
13777 : 3039 : case EXEC_OMP_DO_SIMD:
13778 : 3039 : case EXEC_OMP_LOOP:
13779 : 3039 : case EXEC_OMP_SIMD:
13780 : 3039 : case EXEC_OMP_TARGET_SIMD:
13781 : 3039 : case EXEC_OMP_TILE:
13782 : 3039 : case EXEC_OMP_UNROLL:
13783 : 3039 : gfc_resolve_omp_do_blocks (code, ns);
13784 : 3039 : break;
13785 : : case EXEC_SELECT_TYPE:
13786 : : case EXEC_SELECT_RANK:
13787 : : /* Blocks are handled in resolve_select_type/rank because we
13788 : : have to transform the SELECT TYPE into ASSOCIATE first. */
13789 : : break;
13790 : 174 : case EXEC_DO_CONCURRENT:
13791 : 174 : gfc_do_concurrent_flag = 1;
13792 : 174 : gfc_resolve_blocks (code->block, ns);
13793 : 174 : gfc_do_concurrent_flag = 2;
13794 : 174 : break;
13795 : 39 : case EXEC_OMP_WORKSHARE:
13796 : 39 : omp_workshare_save = omp_workshare_flag;
13797 : 39 : omp_workshare_flag = 1;
13798 : : /* FALL THROUGH */
13799 : 298630 : default:
13800 : 298630 : gfc_resolve_blocks (code->block, ns);
13801 : 298630 : break;
13802 : : }
13803 : :
13804 : 317917 : if (omp_workshare_save != -1)
13805 : 5998 : omp_workshare_flag = omp_workshare_save;
13806 : : }
13807 : 782803 : start:
13808 : 1106939 : t = true;
13809 : 1106939 : if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
13810 : 1105556 : t = gfc_resolve_expr (code->expr1);
13811 : :
13812 : 1106939 : forall_flag = forall_save;
13813 : 1106939 : gfc_do_concurrent_flag = do_concurrent_save;
13814 : :
13815 : 1106939 : if (!gfc_resolve_expr (code->expr2))
13816 : 608 : t = false;
13817 : :
13818 : 1106939 : if (code->op == EXEC_ALLOCATE
13819 : 1106939 : && !gfc_resolve_expr (code->expr3))
13820 : : t = false;
13821 : :
13822 : 1106939 : switch (code->op)
13823 : : {
13824 : : case EXEC_NOP:
13825 : : case EXEC_END_BLOCK:
13826 : : case EXEC_END_NESTED_BLOCK:
13827 : : case EXEC_CYCLE:
13828 : : case EXEC_PAUSE:
13829 : : break;
13830 : :
13831 : 212436 : case EXEC_STOP:
13832 : 212436 : case EXEC_ERROR_STOP:
13833 : 212436 : if (code->expr2 != NULL
13834 : 37 : && (code->expr2->ts.type != BT_LOGICAL
13835 : 37 : || code->expr2->rank != 0))
13836 : 0 : gfc_error ("QUIET specifier at %L must be a scalar LOGICAL",
13837 : : &code->expr2->where);
13838 : : break;
13839 : :
13840 : : case EXEC_EXIT:
13841 : : case EXEC_CONTINUE:
13842 : : case EXEC_DT_END:
13843 : : case EXEC_ASSIGN_CALL:
13844 : : break;
13845 : :
13846 : 50 : case EXEC_CRITICAL:
13847 : 50 : resolve_critical (code);
13848 : 50 : break;
13849 : :
13850 : 747 : case EXEC_SYNC_ALL:
13851 : 747 : case EXEC_SYNC_IMAGES:
13852 : 747 : case EXEC_SYNC_MEMORY:
13853 : 747 : resolve_sync (code);
13854 : 747 : break;
13855 : :
13856 : 136 : case EXEC_LOCK:
13857 : 136 : case EXEC_UNLOCK:
13858 : 136 : case EXEC_EVENT_POST:
13859 : 136 : case EXEC_EVENT_WAIT:
13860 : 136 : resolve_lock_unlock_event (code);
13861 : 136 : break;
13862 : :
13863 : : case EXEC_FAIL_IMAGE:
13864 : : break;
13865 : :
13866 : 107 : case EXEC_FORM_TEAM:
13867 : 107 : resolve_form_team (code);
13868 : 107 : break;
13869 : :
13870 : 66 : case EXEC_CHANGE_TEAM:
13871 : 66 : resolve_change_team (code);
13872 : 66 : break;
13873 : :
13874 : 64 : case EXEC_END_TEAM:
13875 : 64 : resolve_end_team (code);
13876 : 64 : break;
13877 : :
13878 : 22 : case EXEC_SYNC_TEAM:
13879 : 22 : resolve_sync_team (code);
13880 : 22 : break;
13881 : :
13882 : 1420 : case EXEC_ENTRY:
13883 : : /* Keep track of which entry we are up to. */
13884 : 1420 : current_entry_id = code->ext.entry->id;
13885 : 1420 : break;
13886 : :
13887 : 450 : case EXEC_WHERE:
13888 : 450 : resolve_where (code, NULL);
13889 : 450 : break;
13890 : :
13891 : 1249 : case EXEC_GOTO:
13892 : 1249 : if (code->expr1 != NULL)
13893 : : {
13894 : 78 : if (code->expr1->expr_type != EXPR_VARIABLE
13895 : 76 : || code->expr1->ts.type != BT_INTEGER
13896 : 76 : || (code->expr1->ref
13897 : 1 : && code->expr1->ref->type == REF_ARRAY)
13898 : 75 : || code->expr1->symtree == NULL
13899 : 75 : || (code->expr1->symtree->n.sym
13900 : 75 : && (code->expr1->symtree->n.sym->attr.flavor
13901 : 75 : == FL_PARAMETER)))
13902 : 4 : gfc_error ("ASSIGNED GOTO statement at %L requires a "
13903 : : "scalar INTEGER variable", &code->expr1->where);
13904 : 74 : else if (code->expr1->symtree->n.sym
13905 : 74 : && code->expr1->symtree->n.sym->attr.assign != 1)
13906 : 1 : gfc_error ("Variable %qs has not been assigned a target "
13907 : : "label at %L", code->expr1->symtree->n.sym->name,
13908 : : &code->expr1->where);
13909 : : }
13910 : : else
13911 : 1171 : resolve_branch (code->label1, code);
13912 : : break;
13913 : :
13914 : 3123 : case EXEC_RETURN:
13915 : 3123 : if (code->expr1 != NULL
13916 : 53 : && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
13917 : 1 : gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
13918 : : "INTEGER return specifier", &code->expr1->where);
13919 : : break;
13920 : :
13921 : : case EXEC_INIT_ASSIGN:
13922 : : case EXEC_END_PROCEDURE:
13923 : : break;
13924 : :
13925 : 281257 : case EXEC_ASSIGN:
13926 : 281257 : if (!t)
13927 : : break;
13928 : :
13929 : 280611 : if (flag_coarray == GFC_FCOARRAY_LIB
13930 : 280611 : && gfc_is_coindexed (code->expr1))
13931 : : {
13932 : : /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a
13933 : : coindexed variable. */
13934 : 390 : code->op = EXEC_CALL;
13935 : 390 : gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree,
13936 : : true);
13937 : 390 : code->resolved_sym = code->symtree->n.sym;
13938 : 390 : code->resolved_sym->attr.flavor = FL_PROCEDURE;
13939 : 390 : code->resolved_sym->attr.intrinsic = 1;
13940 : 390 : code->resolved_sym->attr.subroutine = 1;
13941 : 390 : code->resolved_isym
13942 : 390 : = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
13943 : 390 : gfc_commit_symbol (code->resolved_sym);
13944 : 390 : code->ext.actual = gfc_get_actual_arglist ();
13945 : 390 : code->ext.actual->expr = code->expr1;
13946 : 390 : code->ext.actual->next = gfc_get_actual_arglist ();
13947 : 390 : code->ext.actual->next->expr = code->expr2;
13948 : :
13949 : 390 : code->expr1 = NULL;
13950 : 390 : code->expr2 = NULL;
13951 : 390 : break;
13952 : : }
13953 : :
13954 : 280221 : if (code->expr1->ts.type == BT_CLASS)
13955 : 1015 : gfc_find_vtab (&code->expr2->ts);
13956 : :
13957 : : /* If this is a pointer function in an lvalue variable context,
13958 : : the new code will have to be resolved afresh. This is also the
13959 : : case with an error, where the code is transformed into NOP to
13960 : : prevent ICEs downstream. */
13961 : 280221 : if (resolve_ptr_fcn_assign (&code, ns)
13962 : 280221 : || code->op == EXEC_NOP)
13963 : 205 : goto start;
13964 : :
13965 : 280016 : if (!gfc_check_vardef_context (code->expr1, false, false, false,
13966 : 280016 : _("assignment")))
13967 : : break;
13968 : :
13969 : 279978 : if (resolve_ordinary_assign (code, ns))
13970 : : {
13971 : 776 : if (omp_workshare_flag)
13972 : : {
13973 : 1 : gfc_error ("Expected intrinsic assignment in OMP WORKSHARE "
13974 : 1 : "at %L", &code->loc);
13975 : 1 : break;
13976 : : }
13977 : 775 : if (code->op == EXEC_COMPCALL)
13978 : 416 : goto compcall;
13979 : : else
13980 : 359 : goto call;
13981 : : }
13982 : :
13983 : : /* Check for dependencies in deferred character length array
13984 : : assignments and generate a temporary, if necessary. */
13985 : 279202 : if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
13986 : : break;
13987 : :
13988 : : /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
13989 : 279180 : if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
13990 : 6698 : && code->expr1->ts.u.derived
13991 : 6698 : && code->expr1->ts.u.derived->attr.defined_assign_comp)
13992 : 188 : generate_component_assignments (&code, ns);
13993 : 278992 : else if (code->op == EXEC_ASSIGN)
13994 : : {
13995 : 278992 : if (gfc_may_be_finalized (code->expr1->ts))
13996 : 1129 : code->expr1->must_finalize = 1;
13997 : 278992 : if (code->expr2->expr_type == EXPR_ARRAY
13998 : 278992 : && gfc_may_be_finalized (code->expr2->ts))
13999 : 43 : code->expr2->must_finalize = 1;
14000 : : }
14001 : :
14002 : : break;
14003 : :
14004 : 126 : case EXEC_LABEL_ASSIGN:
14005 : 126 : if (code->label1->defined == ST_LABEL_UNKNOWN)
14006 : 0 : gfc_error ("Label %d referenced at %L is never defined",
14007 : : code->label1->value, &code->label1->where);
14008 : 126 : if (t
14009 : 126 : && (code->expr1->expr_type != EXPR_VARIABLE
14010 : 126 : || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
14011 : 126 : || code->expr1->symtree->n.sym->ts.kind
14012 : 126 : != gfc_default_integer_kind
14013 : 126 : || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
14014 : 125 : || code->expr1->symtree->n.sym->as != NULL))
14015 : 2 : gfc_error ("ASSIGN statement at %L requires a scalar "
14016 : : "default INTEGER variable", &code->expr1->where);
14017 : : break;
14018 : :
14019 : 10085 : case EXEC_POINTER_ASSIGN:
14020 : 10085 : {
14021 : 10085 : gfc_expr* e;
14022 : :
14023 : 10085 : if (!t)
14024 : : break;
14025 : :
14026 : : /* This is both a variable definition and pointer assignment
14027 : : context, so check both of them. For rank remapping, a final
14028 : : array ref may be present on the LHS and fool gfc_expr_attr
14029 : : used in gfc_check_vardef_context. Remove it. */
14030 : 10080 : e = remove_last_array_ref (code->expr1);
14031 : 20160 : t = gfc_check_vardef_context (e, true, false, false,
14032 : 10080 : _("pointer assignment"));
14033 : 10080 : if (t)
14034 : 10059 : t = gfc_check_vardef_context (e, false, false, false,
14035 : 10059 : _("pointer assignment"));
14036 : 10080 : gfc_free_expr (e);
14037 : :
14038 : 1116679 : t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
14039 : :
14040 : 9946 : if (!t)
14041 : : break;
14042 : :
14043 : : /* Assigning a class object always is a regular assign. */
14044 : 9946 : if (code->expr2->ts.type == BT_CLASS
14045 : 565 : && code->expr1->ts.type == BT_CLASS
14046 : 474 : && CLASS_DATA (code->expr2)
14047 : 473 : && !CLASS_DATA (code->expr2)->attr.dimension
14048 : 10565 : && !(gfc_expr_attr (code->expr1).proc_pointer
14049 : 54 : && code->expr2->expr_type == EXPR_VARIABLE
14050 : 42 : && code->expr2->symtree->n.sym->attr.flavor
14051 : 42 : == FL_PROCEDURE))
14052 : 324 : code->op = EXEC_ASSIGN;
14053 : : break;
14054 : : }
14055 : :
14056 : 72 : case EXEC_ARITHMETIC_IF:
14057 : 72 : {
14058 : 72 : gfc_expr *e = code->expr1;
14059 : :
14060 : 72 : gfc_resolve_expr (e);
14061 : 72 : if (e->expr_type == EXPR_NULL)
14062 : 1 : gfc_error ("Invalid NULL at %L", &e->where);
14063 : :
14064 : 72 : if (t && (e->rank > 0
14065 : 68 : || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
14066 : 5 : gfc_error ("Arithmetic IF statement at %L requires a scalar "
14067 : : "REAL or INTEGER expression", &e->where);
14068 : :
14069 : 72 : resolve_branch (code->label1, code);
14070 : 72 : resolve_branch (code->label2, code);
14071 : 72 : resolve_branch (code->label3, code);
14072 : : }
14073 : 72 : break;
14074 : :
14075 : 225304 : case EXEC_IF:
14076 : 225304 : if (t && code->expr1 != NULL
14077 : 0 : && (code->expr1->ts.type != BT_LOGICAL
14078 : 0 : || code->expr1->rank != 0))
14079 : 0 : gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
14080 : : &code->expr1->where);
14081 : : break;
14082 : :
14083 : 77661 : case EXEC_CALL:
14084 : 77661 : call:
14085 : 77661 : resolve_call (code);
14086 : 77661 : break;
14087 : :
14088 : 1676 : case EXEC_COMPCALL:
14089 : 1676 : compcall:
14090 : 1676 : resolve_typebound_subroutine (code);
14091 : 1676 : break;
14092 : :
14093 : 123 : case EXEC_CALL_PPC:
14094 : 123 : resolve_ppc_call (code);
14095 : 123 : break;
14096 : :
14097 : 683 : case EXEC_SELECT:
14098 : : /* Select is complicated. Also, a SELECT construct could be
14099 : : a transformed computed GOTO. */
14100 : 683 : resolve_select (code, false);
14101 : 683 : break;
14102 : :
14103 : 2922 : case EXEC_SELECT_TYPE:
14104 : 2922 : resolve_select_type (code, ns);
14105 : 2922 : break;
14106 : :
14107 : 1018 : case EXEC_SELECT_RANK:
14108 : 1018 : resolve_select_rank (code, ns);
14109 : 1018 : break;
14110 : :
14111 : 7605 : case EXEC_BLOCK:
14112 : 7605 : resolve_block_construct (code);
14113 : 7605 : break;
14114 : :
14115 : 32313 : case EXEC_DO:
14116 : 32313 : if (code->ext.iterator != NULL)
14117 : : {
14118 : 32313 : gfc_iterator *iter = code->ext.iterator;
14119 : 32313 : if (gfc_resolve_iterator (iter, true, false))
14120 : 32299 : gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
14121 : : true);
14122 : : }
14123 : : break;
14124 : :
14125 : 501 : case EXEC_DO_WHILE:
14126 : 501 : if (code->expr1 == NULL)
14127 : 0 : gfc_internal_error ("gfc_resolve_code(): No expression on "
14128 : : "DO WHILE");
14129 : 501 : if (t
14130 : 501 : && (code->expr1->rank != 0
14131 : 501 : || code->expr1->ts.type != BT_LOGICAL))
14132 : 0 : gfc_error ("Exit condition of DO WHILE loop at %L must be "
14133 : : "a scalar LOGICAL expression", &code->expr1->where);
14134 : : break;
14135 : :
14136 : 13695 : case EXEC_ALLOCATE:
14137 : 13695 : if (t)
14138 : 13693 : resolve_allocate_deallocate (code, "ALLOCATE");
14139 : :
14140 : : break;
14141 : :
14142 : 5746 : case EXEC_DEALLOCATE:
14143 : 5746 : if (t)
14144 : 5746 : resolve_allocate_deallocate (code, "DEALLOCATE");
14145 : :
14146 : : break;
14147 : :
14148 : 3879 : case EXEC_OPEN:
14149 : 3879 : if (!gfc_resolve_open (code->ext.open, &code->loc))
14150 : : break;
14151 : :
14152 : 3652 : resolve_branch (code->ext.open->err, code);
14153 : 3652 : break;
14154 : :
14155 : 3067 : case EXEC_CLOSE:
14156 : 3067 : if (!gfc_resolve_close (code->ext.close, &code->loc))
14157 : : break;
14158 : :
14159 : 3033 : resolve_branch (code->ext.close->err, code);
14160 : 3033 : break;
14161 : :
14162 : 2779 : case EXEC_BACKSPACE:
14163 : 2779 : case EXEC_ENDFILE:
14164 : 2779 : case EXEC_REWIND:
14165 : 2779 : case EXEC_FLUSH:
14166 : 2779 : if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
14167 : : break;
14168 : :
14169 : 2713 : resolve_branch (code->ext.filepos->err, code);
14170 : 2713 : break;
14171 : :
14172 : 817 : case EXEC_INQUIRE:
14173 : 817 : if (!gfc_resolve_inquire (code->ext.inquire))
14174 : : break;
14175 : :
14176 : 769 : resolve_branch (code->ext.inquire->err, code);
14177 : 769 : break;
14178 : :
14179 : 92 : case EXEC_IOLENGTH:
14180 : 92 : gcc_assert (code->ext.inquire != NULL);
14181 : 92 : if (!gfc_resolve_inquire (code->ext.inquire))
14182 : : break;
14183 : :
14184 : 90 : resolve_branch (code->ext.inquire->err, code);
14185 : 90 : break;
14186 : :
14187 : 89 : case EXEC_WAIT:
14188 : 89 : if (!gfc_resolve_wait (code->ext.wait))
14189 : : break;
14190 : :
14191 : 74 : resolve_branch (code->ext.wait->err, code);
14192 : 74 : resolve_branch (code->ext.wait->end, code);
14193 : 74 : resolve_branch (code->ext.wait->eor, code);
14194 : 74 : break;
14195 : :
14196 : 31614 : case EXEC_READ:
14197 : 31614 : case EXEC_WRITE:
14198 : 31614 : if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
14199 : : break;
14200 : :
14201 : 31306 : resolve_branch (code->ext.dt->err, code);
14202 : 31306 : resolve_branch (code->ext.dt->end, code);
14203 : 31306 : resolve_branch (code->ext.dt->eor, code);
14204 : 31306 : break;
14205 : :
14206 : 45382 : case EXEC_TRANSFER:
14207 : 45382 : resolve_transfer (code);
14208 : 45382 : break;
14209 : :
14210 : 2164 : case EXEC_DO_CONCURRENT:
14211 : 2164 : case EXEC_FORALL:
14212 : 2164 : resolve_forall_iterators (code->ext.concur.forall_iterator);
14213 : :
14214 : 2164 : if (code->expr1 != NULL
14215 : 730 : && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
14216 : 2 : gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
14217 : : "expression", &code->expr1->where);
14218 : :
14219 : 2164 : if (code->op == EXEC_DO_CONCURRENT)
14220 : 174 : resolve_locality_spec (code, ns);
14221 : : break;
14222 : :
14223 : 13160 : case EXEC_OACC_PARALLEL_LOOP:
14224 : 13160 : case EXEC_OACC_PARALLEL:
14225 : 13160 : case EXEC_OACC_KERNELS_LOOP:
14226 : 13160 : case EXEC_OACC_KERNELS:
14227 : 13160 : case EXEC_OACC_SERIAL_LOOP:
14228 : 13160 : case EXEC_OACC_SERIAL:
14229 : 13160 : case EXEC_OACC_DATA:
14230 : 13160 : case EXEC_OACC_HOST_DATA:
14231 : 13160 : case EXEC_OACC_LOOP:
14232 : 13160 : case EXEC_OACC_UPDATE:
14233 : 13160 : case EXEC_OACC_WAIT:
14234 : 13160 : case EXEC_OACC_CACHE:
14235 : 13160 : case EXEC_OACC_ENTER_DATA:
14236 : 13160 : case EXEC_OACC_EXIT_DATA:
14237 : 13160 : case EXEC_OACC_ATOMIC:
14238 : 13160 : case EXEC_OACC_DECLARE:
14239 : 13160 : gfc_resolve_oacc_directive (code, ns);
14240 : 13160 : break;
14241 : :
14242 : 16584 : case EXEC_OMP_ALLOCATE:
14243 : 16584 : case EXEC_OMP_ALLOCATORS:
14244 : 16584 : case EXEC_OMP_ASSUME:
14245 : 16584 : case EXEC_OMP_ATOMIC:
14246 : 16584 : case EXEC_OMP_BARRIER:
14247 : 16584 : case EXEC_OMP_CANCEL:
14248 : 16584 : case EXEC_OMP_CANCELLATION_POINT:
14249 : 16584 : case EXEC_OMP_CRITICAL:
14250 : 16584 : case EXEC_OMP_FLUSH:
14251 : 16584 : case EXEC_OMP_DEPOBJ:
14252 : 16584 : case EXEC_OMP_DISPATCH:
14253 : 16584 : case EXEC_OMP_DISTRIBUTE:
14254 : 16584 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
14255 : 16584 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
14256 : 16584 : case EXEC_OMP_DISTRIBUTE_SIMD:
14257 : 16584 : case EXEC_OMP_DO:
14258 : 16584 : case EXEC_OMP_DO_SIMD:
14259 : 16584 : case EXEC_OMP_ERROR:
14260 : 16584 : case EXEC_OMP_INTEROP:
14261 : 16584 : case EXEC_OMP_LOOP:
14262 : 16584 : case EXEC_OMP_MASTER:
14263 : 16584 : case EXEC_OMP_MASTER_TASKLOOP:
14264 : 16584 : case EXEC_OMP_MASTER_TASKLOOP_SIMD:
14265 : 16584 : case EXEC_OMP_MASKED:
14266 : 16584 : case EXEC_OMP_MASKED_TASKLOOP:
14267 : 16584 : case EXEC_OMP_MASKED_TASKLOOP_SIMD:
14268 : 16584 : case EXEC_OMP_METADIRECTIVE:
14269 : 16584 : case EXEC_OMP_ORDERED:
14270 : 16584 : case EXEC_OMP_SCAN:
14271 : 16584 : case EXEC_OMP_SCOPE:
14272 : 16584 : case EXEC_OMP_SECTIONS:
14273 : 16584 : case EXEC_OMP_SIMD:
14274 : 16584 : case EXEC_OMP_SINGLE:
14275 : 16584 : case EXEC_OMP_TARGET:
14276 : 16584 : case EXEC_OMP_TARGET_DATA:
14277 : 16584 : case EXEC_OMP_TARGET_ENTER_DATA:
14278 : 16584 : case EXEC_OMP_TARGET_EXIT_DATA:
14279 : 16584 : case EXEC_OMP_TARGET_PARALLEL:
14280 : 16584 : case EXEC_OMP_TARGET_PARALLEL_DO:
14281 : 16584 : case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
14282 : 16584 : case EXEC_OMP_TARGET_PARALLEL_LOOP:
14283 : 16584 : case EXEC_OMP_TARGET_SIMD:
14284 : 16584 : case EXEC_OMP_TARGET_TEAMS:
14285 : 16584 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
14286 : 16584 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
14287 : 16584 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
14288 : 16584 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
14289 : 16584 : case EXEC_OMP_TARGET_TEAMS_LOOP:
14290 : 16584 : case EXEC_OMP_TARGET_UPDATE:
14291 : 16584 : case EXEC_OMP_TASK:
14292 : 16584 : case EXEC_OMP_TASKGROUP:
14293 : 16584 : case EXEC_OMP_TASKLOOP:
14294 : 16584 : case EXEC_OMP_TASKLOOP_SIMD:
14295 : 16584 : case EXEC_OMP_TASKWAIT:
14296 : 16584 : case EXEC_OMP_TASKYIELD:
14297 : 16584 : case EXEC_OMP_TEAMS:
14298 : 16584 : case EXEC_OMP_TEAMS_DISTRIBUTE:
14299 : 16584 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
14300 : 16584 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
14301 : 16584 : case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
14302 : 16584 : case EXEC_OMP_TEAMS_LOOP:
14303 : 16584 : case EXEC_OMP_TILE:
14304 : 16584 : case EXEC_OMP_UNROLL:
14305 : 16584 : case EXEC_OMP_WORKSHARE:
14306 : 16584 : gfc_resolve_omp_directive (code, ns);
14307 : 16584 : break;
14308 : :
14309 : 3833 : case EXEC_OMP_PARALLEL:
14310 : 3833 : case EXEC_OMP_PARALLEL_DO:
14311 : 3833 : case EXEC_OMP_PARALLEL_DO_SIMD:
14312 : 3833 : case EXEC_OMP_PARALLEL_LOOP:
14313 : 3833 : case EXEC_OMP_PARALLEL_MASKED:
14314 : 3833 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
14315 : 3833 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
14316 : 3833 : case EXEC_OMP_PARALLEL_MASTER:
14317 : 3833 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
14318 : 3833 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
14319 : 3833 : case EXEC_OMP_PARALLEL_SECTIONS:
14320 : 3833 : case EXEC_OMP_PARALLEL_WORKSHARE:
14321 : 3833 : omp_workshare_save = omp_workshare_flag;
14322 : 3833 : omp_workshare_flag = 0;
14323 : 3833 : gfc_resolve_omp_directive (code, ns);
14324 : 3833 : omp_workshare_flag = omp_workshare_save;
14325 : 3833 : break;
14326 : :
14327 : 0 : default:
14328 : 0 : gfc_internal_error ("gfc_resolve_code(): Bad statement code");
14329 : : }
14330 : : }
14331 : :
14332 : 661311 : cs_base = frame.prev;
14333 : 661311 : }
14334 : :
14335 : :
14336 : : /* Resolve initial values and make sure they are compatible with
14337 : : the variable. */
14338 : :
14339 : : static void
14340 : 1794457 : resolve_values (gfc_symbol *sym)
14341 : : {
14342 : 1794457 : bool t;
14343 : :
14344 : 1794457 : if (sym->value == NULL)
14345 : : return;
14346 : :
14347 : 403107 : if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym->attr.referenced)
14348 : 4 : gfc_warning (OPT_Wdeprecated_declarations,
14349 : : "Using parameter %qs declared at %L is deprecated",
14350 : : sym->name, &sym->declared_at);
14351 : :
14352 : 403107 : if (sym->value->expr_type == EXPR_STRUCTURE)
14353 : 38059 : t= resolve_structure_cons (sym->value, 1);
14354 : : else
14355 : 365048 : t = gfc_resolve_expr (sym->value);
14356 : :
14357 : 403107 : if (!t)
14358 : : return;
14359 : :
14360 : 403105 : gfc_check_assign_symbol (sym, NULL, sym->value);
14361 : : }
14362 : :
14363 : :
14364 : : /* Verify any BIND(C) derived types in the namespace so we can report errors
14365 : : for them once, rather than for each variable declared of that type. */
14366 : :
14367 : : static void
14368 : 1767167 : resolve_bind_c_derived_types (gfc_symbol *derived_sym)
14369 : : {
14370 : 1767167 : if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
14371 : 79682 : && derived_sym->attr.is_bind_c == 1)
14372 : 26388 : verify_bind_c_derived_type (derived_sym);
14373 : :
14374 : 1767167 : return;
14375 : : }
14376 : :
14377 : :
14378 : : /* Check the interfaces of DTIO procedures associated with derived
14379 : : type 'sym'. These procedures can either have typebound bindings or
14380 : : can appear in DTIO generic interfaces. */
14381 : :
14382 : : static void
14383 : 1795425 : gfc_verify_DTIO_procedures (gfc_symbol *sym)
14384 : : {
14385 : 1795425 : if (!sym || sym->attr.flavor != FL_DERIVED)
14386 : : return;
14387 : :
14388 : 88403 : gfc_check_dtio_interfaces (sym);
14389 : :
14390 : 88403 : return;
14391 : : }
14392 : :
14393 : : /* Verify that any binding labels used in a given namespace do not collide
14394 : : with the names or binding labels of any global symbols. Multiple INTERFACE
14395 : : for the same procedure are permitted. Abstract interfaces and dummy
14396 : : arguments are not checked. */
14397 : :
14398 : : static void
14399 : 1795425 : gfc_verify_binding_labels (gfc_symbol *sym)
14400 : : {
14401 : 1795425 : gfc_gsymbol *gsym;
14402 : 1795425 : const char *module;
14403 : :
14404 : 1795425 : if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
14405 : 60418 : || sym->attr.flavor == FL_DERIVED || !sym->binding_label
14406 : 33114 : || sym->attr.abstract || sym->attr.dummy)
14407 : : return;
14408 : :
14409 : 33016 : gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
14410 : :
14411 : 33016 : if (sym->module)
14412 : : module = sym->module;
14413 : 11945 : else if (sym->ns && sym->ns->proc_name
14414 : 11945 : && sym->ns->proc_name->attr.flavor == FL_MODULE)
14415 : 4499 : module = sym->ns->proc_name->name;
14416 : 7446 : else if (sym->ns && sym->ns->parent
14417 : 358 : && sym->ns && sym->ns->parent->proc_name
14418 : 358 : && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
14419 : 272 : module = sym->ns->parent->proc_name->name;
14420 : : else
14421 : : module = NULL;
14422 : :
14423 : 33016 : if (!gsym
14424 : 11315 : || (!gsym->defined
14425 : 8485 : && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
14426 : : {
14427 : 21701 : if (!gsym)
14428 : 21701 : gsym = gfc_get_gsymbol (sym->binding_label, true);
14429 : 30186 : gsym->where = sym->declared_at;
14430 : 30186 : gsym->sym_name = sym->name;
14431 : 30186 : gsym->binding_label = sym->binding_label;
14432 : 30186 : gsym->ns = sym->ns;
14433 : 30186 : gsym->mod_name = module;
14434 : 30186 : if (sym->attr.function)
14435 : 19359 : gsym->type = GSYM_FUNCTION;
14436 : 10827 : else if (sym->attr.subroutine)
14437 : 10688 : gsym->type = GSYM_SUBROUTINE;
14438 : : /* Mark as variable/procedure as defined, unless its an INTERFACE. */
14439 : 30186 : gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
14440 : 30186 : return;
14441 : : }
14442 : :
14443 : 2830 : if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
14444 : : {
14445 : 1 : gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
14446 : : "identifier as entity at %L", sym->name,
14447 : : sym->binding_label, &sym->declared_at, &gsym->where);
14448 : : /* Clear the binding label to prevent checking multiple times. */
14449 : 1 : sym->binding_label = NULL;
14450 : 1 : return;
14451 : : }
14452 : :
14453 : 2829 : if (sym->attr.flavor == FL_VARIABLE && module
14454 : 37 : && (strcmp (module, gsym->mod_name) != 0
14455 : 35 : || strcmp (sym->name, gsym->sym_name) != 0))
14456 : : {
14457 : : /* This can only happen if the variable is defined in a module - if it
14458 : : isn't the same module, reject it. */
14459 : 3 : gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
14460 : : "uses the same global identifier as entity at %L from module %qs",
14461 : : sym->name, module, sym->binding_label,
14462 : : &sym->declared_at, &gsym->where, gsym->mod_name);
14463 : 3 : sym->binding_label = NULL;
14464 : 3 : return;
14465 : : }
14466 : :
14467 : 2826 : if ((sym->attr.function || sym->attr.subroutine)
14468 : 2790 : && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
14469 : 2788 : || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
14470 : 2475 : && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
14471 : 2087 : && (module != gsym->mod_name
14472 : 2083 : || strcmp (gsym->sym_name, sym->name) != 0
14473 : 2083 : || (module && strcmp (module, gsym->mod_name) != 0)))
14474 : : {
14475 : : /* Print an error if the procedure is defined multiple times; we have to
14476 : : exclude references to the same procedure via module association or
14477 : : multiple checks for the same procedure. */
14478 : 4 : gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
14479 : : "global identifier as entity at %L", sym->name,
14480 : : sym->binding_label, &sym->declared_at, &gsym->where);
14481 : 4 : sym->binding_label = NULL;
14482 : : }
14483 : : }
14484 : :
14485 : :
14486 : : /* Resolve an index expression. */
14487 : :
14488 : : static bool
14489 : 261014 : resolve_index_expr (gfc_expr *e)
14490 : : {
14491 : 261014 : if (!gfc_resolve_expr (e))
14492 : : return false;
14493 : :
14494 : 261004 : if (!gfc_simplify_expr (e, 0))
14495 : : return false;
14496 : :
14497 : 261002 : if (!gfc_specification_expr (e))
14498 : : return false;
14499 : :
14500 : : return true;
14501 : : }
14502 : :
14503 : :
14504 : : /* Resolve a charlen structure. */
14505 : :
14506 : : static bool
14507 : 100226 : resolve_charlen (gfc_charlen *cl)
14508 : : {
14509 : 100226 : int k;
14510 : 100226 : bool saved_specification_expr;
14511 : :
14512 : 100226 : if (cl->resolved)
14513 : : return true;
14514 : :
14515 : 92088 : cl->resolved = 1;
14516 : 92088 : saved_specification_expr = specification_expr;
14517 : 92088 : specification_expr = true;
14518 : :
14519 : 92088 : if (cl->length_from_typespec)
14520 : : {
14521 : 1090 : if (!gfc_resolve_expr (cl->length))
14522 : : {
14523 : 1 : specification_expr = saved_specification_expr;
14524 : 1 : return false;
14525 : : }
14526 : :
14527 : 1089 : if (!gfc_simplify_expr (cl->length, 0))
14528 : : {
14529 : 0 : specification_expr = saved_specification_expr;
14530 : 0 : return false;
14531 : : }
14532 : :
14533 : : /* cl->length has been resolved. It should have an integer type. */
14534 : 1089 : if (cl->length
14535 : 1088 : && (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0))
14536 : : {
14537 : 4 : gfc_error ("Scalar INTEGER expression expected at %L",
14538 : : &cl->length->where);
14539 : 4 : return false;
14540 : : }
14541 : : }
14542 : : else
14543 : : {
14544 : 90998 : if (!resolve_index_expr (cl->length))
14545 : : {
14546 : 19 : specification_expr = saved_specification_expr;
14547 : 19 : return false;
14548 : : }
14549 : : }
14550 : :
14551 : : /* F2008, 4.4.3.2: If the character length parameter value evaluates to
14552 : : a negative value, the length of character entities declared is zero. */
14553 : 92064 : if (cl->length && cl->length->expr_type == EXPR_CONSTANT
14554 : 54947 : && mpz_sgn (cl->length->value.integer) < 0)
14555 : 0 : gfc_replace_expr (cl->length,
14556 : : gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
14557 : :
14558 : : /* Check that the character length is not too large. */
14559 : 92064 : k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
14560 : 92064 : if (cl->length && cl->length->expr_type == EXPR_CONSTANT
14561 : 54947 : && cl->length->ts.type == BT_INTEGER
14562 : 54947 : && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
14563 : : {
14564 : 4 : gfc_error ("String length at %L is too large", &cl->length->where);
14565 : 4 : specification_expr = saved_specification_expr;
14566 : 4 : return false;
14567 : : }
14568 : :
14569 : 92060 : specification_expr = saved_specification_expr;
14570 : 92060 : return true;
14571 : : }
14572 : :
14573 : :
14574 : : /* Test for non-constant shape arrays. */
14575 : :
14576 : : static bool
14577 : 115374 : is_non_constant_shape_array (gfc_symbol *sym)
14578 : : {
14579 : 115374 : gfc_expr *e;
14580 : 115374 : int i;
14581 : 115374 : bool not_constant;
14582 : :
14583 : 115374 : not_constant = false;
14584 : 115374 : if (sym->as != NULL)
14585 : : {
14586 : : /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
14587 : : has not been simplified; parameter array references. Do the
14588 : : simplification now. */
14589 : 152815 : for (i = 0; i < sym->as->rank + sym->as->corank; i++)
14590 : : {
14591 : 88271 : if (i == GFC_MAX_DIMENSIONS)
14592 : : break;
14593 : :
14594 : 88269 : e = sym->as->lower[i];
14595 : 88269 : if (e && (!resolve_index_expr(e)
14596 : 85514 : || !gfc_is_constant_expr (e)))
14597 : : not_constant = true;
14598 : 88269 : e = sym->as->upper[i];
14599 : 88269 : if (e && (!resolve_index_expr(e)
14600 : 84474 : || !gfc_is_constant_expr (e)))
14601 : : not_constant = true;
14602 : : }
14603 : : }
14604 : 115374 : return not_constant;
14605 : : }
14606 : :
14607 : : /* Given a symbol and an initialization expression, add code to initialize
14608 : : the symbol to the function entry. */
14609 : : static void
14610 : 1924 : build_init_assign (gfc_symbol *sym, gfc_expr *init)
14611 : : {
14612 : 1924 : gfc_expr *lval;
14613 : 1924 : gfc_code *init_st;
14614 : 1924 : gfc_namespace *ns = sym->ns;
14615 : :
14616 : 1924 : if (sym->attr.function && sym->result == sym
14617 : 303 : && sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.pdt_type)
14618 : : {
14619 : 0 : gfc_free_expr (init);
14620 : 0 : return;
14621 : : }
14622 : :
14623 : : /* Search for the function namespace if this is a contained
14624 : : function without an explicit result. */
14625 : 1924 : if (sym->attr.function && sym == sym->result
14626 : 303 : && sym->name != sym->ns->proc_name->name)
14627 : : {
14628 : 302 : ns = ns->contained;
14629 : 1365 : for (;ns; ns = ns->sibling)
14630 : 1299 : if (strcmp (ns->proc_name->name, sym->name) == 0)
14631 : : break;
14632 : : }
14633 : :
14634 : 1924 : if (ns == NULL)
14635 : : {
14636 : 66 : gfc_free_expr (init);
14637 : 66 : return;
14638 : : }
14639 : :
14640 : : /* Build an l-value expression for the result. */
14641 : 1858 : lval = gfc_lval_expr_from_sym (sym);
14642 : :
14643 : : /* Add the code at scope entry. */
14644 : 1858 : init_st = gfc_get_code (EXEC_INIT_ASSIGN);
14645 : 1858 : init_st->next = ns->code;
14646 : 1858 : ns->code = init_st;
14647 : :
14648 : : /* Assign the default initializer to the l-value. */
14649 : 1858 : init_st->loc = sym->declared_at;
14650 : 1858 : init_st->expr1 = lval;
14651 : 1858 : init_st->expr2 = init;
14652 : : }
14653 : :
14654 : :
14655 : : /* Whether or not we can generate a default initializer for a symbol. */
14656 : :
14657 : : static bool
14658 : 28949 : can_generate_init (gfc_symbol *sym)
14659 : : {
14660 : 28949 : symbol_attribute *a;
14661 : 28949 : if (!sym)
14662 : : return false;
14663 : 28949 : a = &sym->attr;
14664 : :
14665 : : /* These symbols should never have a default initialization. */
14666 : 47505 : return !(
14667 : 28949 : a->allocatable
14668 : 28949 : || a->external
14669 : 27804 : || a->pointer
14670 : 27804 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
14671 : 5512 : && (CLASS_DATA (sym)->attr.class_pointer
14672 : 3609 : || CLASS_DATA (sym)->attr.proc_pointer))
14673 : 25901 : || a->in_equivalence
14674 : 25780 : || a->in_common
14675 : 25733 : || a->data
14676 : 25555 : || sym->module
14677 : 21816 : || a->cray_pointee
14678 : 21754 : || a->cray_pointer
14679 : 21754 : || sym->assoc
14680 : 19179 : || (!a->referenced && !a->result)
14681 : 18556 : || (a->dummy && (a->intent != INTENT_OUT
14682 : 1061 : || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY))
14683 : 18556 : || (a->function && sym != sym->result)
14684 : : );
14685 : : }
14686 : :
14687 : :
14688 : : /* Assign the default initializer to a derived type variable or result. */
14689 : :
14690 : : static void
14691 : 11065 : apply_default_init (gfc_symbol *sym)
14692 : : {
14693 : 11065 : gfc_expr *init = NULL;
14694 : :
14695 : 11065 : if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
14696 : : return;
14697 : :
14698 : 10909 : if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
14699 : 10102 : init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
14700 : :
14701 : 10909 : if (init == NULL && sym->ts.type != BT_CLASS)
14702 : : return;
14703 : :
14704 : 1542 : build_init_assign (sym, init);
14705 : 1542 : sym->attr.referenced = 1;
14706 : : }
14707 : :
14708 : :
14709 : : /* Build an initializer for a local. Returns null if the symbol should not have
14710 : : a default initialization. */
14711 : :
14712 : : static gfc_expr *
14713 : 200014 : build_default_init_expr (gfc_symbol *sym)
14714 : : {
14715 : : /* These symbols should never have a default initialization. */
14716 : 200014 : if (sym->attr.allocatable
14717 : 186663 : || sym->attr.external
14718 : 186663 : || sym->attr.dummy
14719 : 122630 : || sym->attr.pointer
14720 : 114646 : || sym->attr.in_equivalence
14721 : 112270 : || sym->attr.in_common
14722 : 109217 : || sym->attr.data
14723 : 106921 : || sym->module
14724 : 104586 : || sym->attr.cray_pointee
14725 : 104285 : || sym->attr.cray_pointer
14726 : 103983 : || sym->assoc)
14727 : : return NULL;
14728 : :
14729 : : /* Get the appropriate init expression. */
14730 : 99498 : return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
14731 : : }
14732 : :
14733 : : /* Add an initialization expression to a local variable. */
14734 : : static void
14735 : 200014 : apply_default_init_local (gfc_symbol *sym)
14736 : : {
14737 : 200014 : gfc_expr *init = NULL;
14738 : :
14739 : : /* The symbol should be a variable or a function return value. */
14740 : 200014 : if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
14741 : 200014 : || (sym->attr.function && sym->result != sym))
14742 : : return;
14743 : :
14744 : : /* Try to build the initializer expression. If we can't initialize
14745 : : this symbol, then init will be NULL. */
14746 : 200014 : init = build_default_init_expr (sym);
14747 : 200014 : if (init == NULL)
14748 : : return;
14749 : :
14750 : : /* For saved variables, we don't want to add an initializer at function
14751 : : entry, so we just add a static initializer. Note that automatic variables
14752 : : are stack allocated even with -fno-automatic; we have also to exclude
14753 : : result variable, which are also nonstatic. */
14754 : 419 : if (!sym->attr.automatic
14755 : 419 : && (sym->attr.save || sym->ns->save_all
14756 : 377 : || (flag_max_stack_var_size == 0 && !sym->attr.result
14757 : 27 : && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
14758 : 14 : && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
14759 : : {
14760 : : /* Don't clobber an existing initializer! */
14761 : 37 : gcc_assert (sym->value == NULL);
14762 : 37 : sym->value = init;
14763 : 37 : return;
14764 : : }
14765 : :
14766 : 382 : build_init_assign (sym, init);
14767 : : }
14768 : :
14769 : :
14770 : : /* Resolution of common features of flavors variable and procedure. */
14771 : :
14772 : : static bool
14773 : 946970 : resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
14774 : : {
14775 : 946970 : gfc_array_spec *as;
14776 : :
14777 : 946970 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
14778 : 18730 : && sym->ts.u.derived && CLASS_DATA (sym))
14779 : 18724 : as = CLASS_DATA (sym)->as;
14780 : : else
14781 : 928246 : as = sym->as;
14782 : :
14783 : : /* Constraints on deferred shape variable. */
14784 : 946970 : if (as == NULL || as->type != AS_DEFERRED)
14785 : : {
14786 : 923295 : bool pointer, allocatable, dimension;
14787 : :
14788 : 923295 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
14789 : 15633 : && sym->ts.u.derived && CLASS_DATA (sym))
14790 : : {
14791 : 15627 : pointer = CLASS_DATA (sym)->attr.class_pointer;
14792 : 15627 : allocatable = CLASS_DATA (sym)->attr.allocatable;
14793 : 15627 : dimension = CLASS_DATA (sym)->attr.dimension;
14794 : : }
14795 : : else
14796 : : {
14797 : 907668 : pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
14798 : 907668 : allocatable = sym->attr.allocatable;
14799 : 907668 : dimension = sym->attr.dimension;
14800 : : }
14801 : :
14802 : 923295 : if (allocatable)
14803 : : {
14804 : 7846 : if (dimension
14805 : 7846 : && as
14806 : 523 : && as->type != AS_ASSUMED_RANK
14807 : 5 : && !sym->attr.select_rank_temporary)
14808 : : {
14809 : 3 : gfc_error ("Allocatable array %qs at %L must have a deferred "
14810 : : "shape or assumed rank", sym->name, &sym->declared_at);
14811 : 3 : return false;
14812 : : }
14813 : 7843 : else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
14814 : : "%qs at %L may not be ALLOCATABLE",
14815 : : sym->name, &sym->declared_at))
14816 : : return false;
14817 : : }
14818 : :
14819 : 923291 : if (pointer && dimension && as->type != AS_ASSUMED_RANK)
14820 : : {
14821 : 4 : gfc_error ("Array pointer %qs at %L must have a deferred shape or "
14822 : : "assumed rank", sym->name, &sym->declared_at);
14823 : 4 : sym->error = 1;
14824 : 4 : return false;
14825 : : }
14826 : : }
14827 : : else
14828 : : {
14829 : 23675 : if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
14830 : 4505 : && sym->ts.type != BT_CLASS && !sym->assoc)
14831 : : {
14832 : 3 : gfc_error ("Array %qs at %L cannot have a deferred shape",
14833 : : sym->name, &sym->declared_at);
14834 : 3 : return false;
14835 : : }
14836 : : }
14837 : :
14838 : : /* Constraints on polymorphic variables. */
14839 : 946959 : if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
14840 : : {
14841 : : /* F03:C502. */
14842 : 18070 : if (sym->attr.class_ok
14843 : 18014 : && sym->ts.u.derived
14844 : 18009 : && !sym->attr.select_type_temporary
14845 : 16949 : && !UNLIMITED_POLY (sym)
14846 : 14505 : && CLASS_DATA (sym)
14847 : 14504 : && CLASS_DATA (sym)->ts.u.derived
14848 : 32573 : && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
14849 : : {
14850 : 5 : gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
14851 : 5 : CLASS_DATA (sym)->ts.u.derived->name, sym->name,
14852 : : &sym->declared_at);
14853 : 5 : return false;
14854 : : }
14855 : :
14856 : : /* F03:C509. */
14857 : : /* Assume that use associated symbols were checked in the module ns.
14858 : : Class-variables that are associate-names are also something special
14859 : : and excepted from the test. */
14860 : 18065 : if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc
14861 : 54 : && !sym->attr.select_type_temporary
14862 : 54 : && !sym->attr.select_rank_temporary)
14863 : : {
14864 : 54 : gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
14865 : : "or pointer", sym->name, &sym->declared_at);
14866 : 54 : return false;
14867 : : }
14868 : : }
14869 : :
14870 : : return true;
14871 : : }
14872 : :
14873 : :
14874 : : /* Additional checks for symbols with flavor variable and derived
14875 : : type. To be called from resolve_fl_variable. */
14876 : :
14877 : : static bool
14878 : 79286 : resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
14879 : : {
14880 : 79286 : gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
14881 : :
14882 : : /* Check to see if a derived type is blocked from being host
14883 : : associated by the presence of another class I symbol in the same
14884 : : namespace. 14.6.1.3 of the standard and the discussion on
14885 : : comp.lang.fortran. */
14886 : 79286 : if (sym->ts.u.derived
14887 : 79281 : && sym->ns != sym->ts.u.derived->ns
14888 : 45576 : && !sym->ts.u.derived->attr.use_assoc
14889 : 16668 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
14890 : : {
14891 : 15733 : gfc_symbol *s;
14892 : 15733 : gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
14893 : 15733 : if (s && s->attr.generic)
14894 : 2 : s = gfc_find_dt_in_generic (s);
14895 : 15733 : if (s && !gfc_fl_struct (s->attr.flavor))
14896 : : {
14897 : 2 : gfc_error ("The type %qs cannot be host associated at %L "
14898 : : "because it is blocked by an incompatible object "
14899 : : "of the same name declared at %L",
14900 : 2 : sym->ts.u.derived->name, &sym->declared_at,
14901 : : &s->declared_at);
14902 : 2 : return false;
14903 : : }
14904 : : }
14905 : :
14906 : : /* 4th constraint in section 11.3: "If an object of a type for which
14907 : : component-initialization is specified (R429) appears in the
14908 : : specification-part of a module and does not have the ALLOCATABLE
14909 : : or POINTER attribute, the object shall have the SAVE attribute."
14910 : :
14911 : : The check for initializers is performed with
14912 : : gfc_has_default_initializer because gfc_default_initializer generates
14913 : : a hidden default for allocatable components. */
14914 : 78693 : if (!(sym->value || no_init_flag) && sym->ns->proc_name
14915 : 17594 : && sym->ns->proc_name->attr.flavor == FL_MODULE
14916 : 356 : && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
14917 : 20 : && !sym->attr.pointer && !sym->attr.allocatable
14918 : 20 : && gfc_has_default_initializer (sym->ts.u.derived)
14919 : 79293 : && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
14920 : : "%qs at %L, needed due to the default "
14921 : : "initialization", sym->name, &sym->declared_at))
14922 : : return false;
14923 : :
14924 : : /* Assign default initializer. */
14925 : 79282 : if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
14926 : 73413 : && (!no_init_flag
14927 : 57571 : || (sym->attr.intent == INTENT_OUT
14928 : 3179 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)))
14929 : 18847 : sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
14930 : :
14931 : : return true;
14932 : : }
14933 : :
14934 : :
14935 : : /* F2008, C402 (R401): A colon shall not be used as a type-param-value
14936 : : except in the declaration of an entity or component that has the POINTER
14937 : : or ALLOCATABLE attribute. */
14938 : :
14939 : : static bool
14940 : 1471159 : deferred_requirements (gfc_symbol *sym)
14941 : : {
14942 : 1471159 : if (sym->ts.deferred
14943 : 7619 : && !(sym->attr.pointer
14944 : 2302 : || sym->attr.allocatable
14945 : 92 : || sym->attr.associate_var
14946 : 7 : || sym->attr.omp_udr_artificial_var))
14947 : : {
14948 : : /* If a function has a result variable, only check the variable. */
14949 : 7 : if (sym->result && sym->name != sym->result->name)
14950 : : return true;
14951 : :
14952 : 6 : gfc_error ("Entity %qs at %L has a deferred type parameter and "
14953 : : "requires either the POINTER or ALLOCATABLE attribute",
14954 : : sym->name, &sym->declared_at);
14955 : 6 : return false;
14956 : : }
14957 : : return true;
14958 : : }
14959 : :
14960 : :
14961 : : /* Resolve symbols with flavor variable. */
14962 : :
14963 : : static bool
14964 : 636333 : resolve_fl_variable (gfc_symbol *sym, int mp_flag)
14965 : : {
14966 : 636333 : const char *auto_save_msg = G_("Automatic object %qs at %L cannot have the "
14967 : : "SAVE attribute");
14968 : :
14969 : 636333 : if (!resolve_fl_var_and_proc (sym, mp_flag))
14970 : : return false;
14971 : :
14972 : : /* Set this flag to check that variables are parameters of all entries.
14973 : : This check is effected by the call to gfc_resolve_expr through
14974 : : is_non_constant_shape_array. */
14975 : 636273 : bool saved_specification_expr = specification_expr;
14976 : 636273 : specification_expr = true;
14977 : :
14978 : 636273 : if (sym->ns->proc_name
14979 : 636186 : && (sym->ns->proc_name->attr.flavor == FL_MODULE
14980 : 631401 : || sym->ns->proc_name->attr.is_main_program)
14981 : 80655 : && !sym->attr.use_assoc
14982 : 77641 : && !sym->attr.allocatable
14983 : 72132 : && !sym->attr.pointer
14984 : 704820 : && is_non_constant_shape_array (sym))
14985 : : {
14986 : : /* F08:C541. The shape of an array defined in a main program or module
14987 : : * needs to be constant. */
14988 : 3 : gfc_error ("The module or main program array %qs at %L must "
14989 : : "have constant shape", sym->name, &sym->declared_at);
14990 : 3 : specification_expr = saved_specification_expr;
14991 : 3 : return false;
14992 : : }
14993 : :
14994 : : /* Constraints on deferred type parameter. */
14995 : 636270 : if (!deferred_requirements (sym))
14996 : : return false;
14997 : :
14998 : 636266 : if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
14999 : : {
15000 : : /* Make sure that character string variables with assumed length are
15001 : : dummy arguments. */
15002 : 35230 : gfc_expr *e = NULL;
15003 : :
15004 : 35230 : if (sym->ts.u.cl)
15005 : 35230 : e = sym->ts.u.cl->length;
15006 : : else
15007 : : return false;
15008 : :
15009 : 35230 : if (e == NULL && !sym->attr.dummy && !sym->attr.result
15010 : 2501 : && !sym->ts.deferred && !sym->attr.select_type_temporary
15011 : 2 : && !sym->attr.omp_udr_artificial_var)
15012 : : {
15013 : 2 : gfc_error ("Entity with assumed character length at %L must be a "
15014 : : "dummy argument or a PARAMETER", &sym->declared_at);
15015 : 2 : specification_expr = saved_specification_expr;
15016 : 2 : return false;
15017 : : }
15018 : :
15019 : 20439 : if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
15020 : : {
15021 : 1 : gfc_error (auto_save_msg, sym->name, &sym->declared_at);
15022 : 1 : specification_expr = saved_specification_expr;
15023 : 1 : return false;
15024 : : }
15025 : :
15026 : 35227 : if (!gfc_is_constant_expr (e)
15027 : 35227 : && !(e->expr_type == EXPR_VARIABLE
15028 : 1388 : && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
15029 : : {
15030 : 2184 : if (!sym->attr.use_assoc && sym->ns->proc_name
15031 : 1680 : && (sym->ns->proc_name->attr.flavor == FL_MODULE
15032 : 1679 : || sym->ns->proc_name->attr.is_main_program))
15033 : : {
15034 : 3 : gfc_error ("%qs at %L must have constant character length "
15035 : : "in this context", sym->name, &sym->declared_at);
15036 : 3 : specification_expr = saved_specification_expr;
15037 : 3 : return false;
15038 : : }
15039 : 2181 : if (sym->attr.in_common)
15040 : : {
15041 : 1 : gfc_error ("COMMON variable %qs at %L must have constant "
15042 : : "character length", sym->name, &sym->declared_at);
15043 : 1 : specification_expr = saved_specification_expr;
15044 : 1 : return false;
15045 : : }
15046 : : }
15047 : : }
15048 : :
15049 : 636259 : if (sym->value == NULL && sym->attr.referenced
15050 : 201893 : && !(sym->as && sym->as->type == AS_ASSUMED_RANK))
15051 : 200014 : apply_default_init_local (sym); /* Try to apply a default initialization. */
15052 : :
15053 : : /* Determine if the symbol may not have an initializer. */
15054 : 636259 : int no_init_flag = 0, automatic_flag = 0;
15055 : 636259 : if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
15056 : 167136 : || sym->attr.intrinsic || sym->attr.result)
15057 : : no_init_flag = 1;
15058 : 135153 : else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
15059 : 169213 : && is_non_constant_shape_array (sym))
15060 : : {
15061 : 1329 : no_init_flag = automatic_flag = 1;
15062 : :
15063 : : /* Also, they must not have the SAVE attribute.
15064 : : SAVE_IMPLICIT is checked below. */
15065 : 1329 : if (sym->as && sym->attr.codimension)
15066 : : {
15067 : 7 : int corank = sym->as->corank;
15068 : 7 : sym->as->corank = 0;
15069 : 7 : no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
15070 : 7 : sym->as->corank = corank;
15071 : : }
15072 : 1329 : if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
15073 : : {
15074 : 2 : gfc_error (auto_save_msg, sym->name, &sym->declared_at);
15075 : 2 : specification_expr = saved_specification_expr;
15076 : 2 : return false;
15077 : : }
15078 : : }
15079 : :
15080 : : /* Ensure that any initializer is simplified. */
15081 : 636257 : if (sym->value)
15082 : 7646 : gfc_simplify_expr (sym->value, 1);
15083 : :
15084 : : /* Reject illegal initializers. */
15085 : 636257 : if (!sym->mark && sym->value)
15086 : : {
15087 : 7646 : if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
15088 : 67 : && CLASS_DATA (sym)->attr.allocatable))
15089 : 1 : gfc_error ("Allocatable %qs at %L cannot have an initializer",
15090 : : sym->name, &sym->declared_at);
15091 : 7645 : else if (sym->attr.external)
15092 : 0 : gfc_error ("External %qs at %L cannot have an initializer",
15093 : : sym->name, &sym->declared_at);
15094 : 7645 : else if (sym->attr.dummy)
15095 : 3 : gfc_error ("Dummy %qs at %L cannot have an initializer",
15096 : : sym->name, &sym->declared_at);
15097 : 7642 : else if (sym->attr.intrinsic)
15098 : 0 : gfc_error ("Intrinsic %qs at %L cannot have an initializer",
15099 : : sym->name, &sym->declared_at);
15100 : 7642 : else if (sym->attr.result)
15101 : 1 : gfc_error ("Function result %qs at %L cannot have an initializer",
15102 : : sym->name, &sym->declared_at);
15103 : 7641 : else if (automatic_flag)
15104 : 5 : gfc_error ("Automatic array %qs at %L cannot have an initializer",
15105 : : sym->name, &sym->declared_at);
15106 : : else
15107 : 7636 : goto no_init_error;
15108 : 10 : specification_expr = saved_specification_expr;
15109 : 10 : return false;
15110 : : }
15111 : :
15112 : 628611 : no_init_error:
15113 : 636247 : if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
15114 : : {
15115 : 79286 : bool res = resolve_fl_variable_derived (sym, no_init_flag);
15116 : 79286 : specification_expr = saved_specification_expr;
15117 : 79286 : return res;
15118 : : }
15119 : :
15120 : 556961 : specification_expr = saved_specification_expr;
15121 : 556961 : return true;
15122 : : }
15123 : :
15124 : :
15125 : : /* Compare the dummy characteristics of a module procedure interface
15126 : : declaration with the corresponding declaration in a submodule. */
15127 : : static gfc_formal_arglist *new_formal;
15128 : : static char errmsg[200];
15129 : :
15130 : : static void
15131 : 1266 : compare_fsyms (gfc_symbol *sym)
15132 : : {
15133 : 1266 : gfc_symbol *fsym;
15134 : :
15135 : 1266 : if (sym == NULL || new_formal == NULL)
15136 : : return;
15137 : :
15138 : 1266 : fsym = new_formal->sym;
15139 : :
15140 : 1266 : if (sym == fsym)
15141 : : return;
15142 : :
15143 : 1242 : if (strcmp (sym->name, fsym->name) == 0)
15144 : : {
15145 : 469 : if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
15146 : 2 : gfc_error ("%s at %L", errmsg, &fsym->declared_at);
15147 : : }
15148 : : }
15149 : :
15150 : :
15151 : : /* Resolve a procedure. */
15152 : :
15153 : : static bool
15154 : 463941 : resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
15155 : : {
15156 : 463941 : gfc_formal_arglist *arg;
15157 : 463941 : bool allocatable_or_pointer = false;
15158 : :
15159 : 463941 : if (sym->attr.function
15160 : 463941 : && !resolve_fl_var_and_proc (sym, mp_flag))
15161 : : return false;
15162 : :
15163 : : /* Constraints on deferred type parameter. */
15164 : 463931 : if (!deferred_requirements (sym))
15165 : : return false;
15166 : :
15167 : 463930 : if (sym->ts.type == BT_CHARACTER)
15168 : : {
15169 : 11246 : gfc_charlen *cl = sym->ts.u.cl;
15170 : :
15171 : 7201 : if (cl && cl->length && gfc_is_constant_expr (cl->length)
15172 : 12332 : && !resolve_charlen (cl))
15173 : : return false;
15174 : :
15175 : 11245 : if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
15176 : 10160 : && sym->attr.proc == PROC_ST_FUNCTION)
15177 : : {
15178 : 0 : gfc_error ("Character-valued statement function %qs at %L must "
15179 : : "have constant length", sym->name, &sym->declared_at);
15180 : 0 : return false;
15181 : : }
15182 : : }
15183 : :
15184 : : /* Ensure that derived type for are not of a private type. Internal
15185 : : module procedures are excluded by 2.2.3.3 - i.e., they are not
15186 : : externally accessible and can access all the objects accessible in
15187 : : the host. */
15188 : 107018 : if (!(sym->ns->parent && sym->ns->parent->proc_name
15189 : 107018 : && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
15190 : 548014 : && gfc_check_symbol_access (sym))
15191 : : {
15192 : 434207 : gfc_interface *iface;
15193 : :
15194 : 917537 : for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
15195 : : {
15196 : 483331 : if (arg->sym
15197 : 483190 : && arg->sym->ts.type == BT_DERIVED
15198 : 41861 : && arg->sym->ts.u.derived
15199 : 41861 : && !arg->sym->ts.u.derived->attr.use_assoc
15200 : 4226 : && !gfc_check_symbol_access (arg->sym->ts.u.derived)
15201 : 483340 : && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
15202 : : "and cannot be a dummy argument"
15203 : : " of %qs, which is PUBLIC at %L",
15204 : 9 : arg->sym->name, sym->name,
15205 : : &sym->declared_at))
15206 : : {
15207 : : /* Stop this message from recurring. */
15208 : 1 : arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
15209 : 1 : return false;
15210 : : }
15211 : : }
15212 : :
15213 : : /* PUBLIC interfaces may expose PRIVATE procedures that take types
15214 : : PRIVATE to the containing module. */
15215 : 618935 : for (iface = sym->generic; iface; iface = iface->next)
15216 : : {
15217 : 432442 : for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
15218 : : {
15219 : 247713 : if (arg->sym
15220 : 247681 : && arg->sym->ts.type == BT_DERIVED
15221 : 7914 : && !arg->sym->ts.u.derived->attr.use_assoc
15222 : 234 : && !gfc_check_symbol_access (arg->sym->ts.u.derived)
15223 : 247717 : && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
15224 : : "PUBLIC interface %qs at %L "
15225 : : "takes dummy arguments of %qs which "
15226 : : "is PRIVATE", iface->sym->name,
15227 : 4 : sym->name, &iface->sym->declared_at,
15228 : 4 : gfc_typename(&arg->sym->ts)))
15229 : : {
15230 : : /* Stop this message from recurring. */
15231 : 1 : arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
15232 : 1 : return false;
15233 : : }
15234 : : }
15235 : : }
15236 : : }
15237 : :
15238 : 463927 : if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
15239 : 67 : && !sym->attr.proc_pointer)
15240 : : {
15241 : 2 : gfc_error ("Function %qs at %L cannot have an initializer",
15242 : : sym->name, &sym->declared_at);
15243 : :
15244 : : /* Make sure no second error is issued for this. */
15245 : 2 : sym->value->error = 1;
15246 : 2 : return false;
15247 : : }
15248 : :
15249 : : /* An external symbol may not have an initializer because it is taken to be
15250 : : a procedure. Exception: Procedure Pointers. */
15251 : 463925 : if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
15252 : : {
15253 : 0 : gfc_error ("External object %qs at %L may not have an initializer",
15254 : : sym->name, &sym->declared_at);
15255 : 0 : return false;
15256 : : }
15257 : :
15258 : : /* An elemental function is required to return a scalar 12.7.1 */
15259 : 463925 : if (sym->attr.elemental && sym->attr.function
15260 : 86104 : && (sym->as || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15261 : 2 : && CLASS_DATA (sym)->as)))
15262 : : {
15263 : 3 : gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
15264 : : "result", sym->name, &sym->declared_at);
15265 : : /* Reset so that the error only occurs once. */
15266 : 3 : sym->attr.elemental = 0;
15267 : 3 : return false;
15268 : : }
15269 : :
15270 : 463922 : if (sym->attr.proc == PROC_ST_FUNCTION
15271 : 220 : && (sym->attr.allocatable || sym->attr.pointer))
15272 : : {
15273 : 2 : gfc_error ("Statement function %qs at %L may not have pointer or "
15274 : : "allocatable attribute", sym->name, &sym->declared_at);
15275 : 2 : return false;
15276 : : }
15277 : :
15278 : : /* 5.1.1.5 of the Standard: A function name declared with an asterisk
15279 : : char-len-param shall not be array-valued, pointer-valued, recursive
15280 : : or pure. ....snip... A character value of * may only be used in the
15281 : : following ways: (i) Dummy arg of procedure - dummy associates with
15282 : : actual length; (ii) To declare a named constant; or (iii) External
15283 : : function - but length must be declared in calling scoping unit. */
15284 : 463920 : if (sym->attr.function
15285 : 310618 : && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
15286 : 6423 : && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
15287 : : {
15288 : 180 : if ((sym->as && sym->as->rank) || (sym->attr.pointer)
15289 : 178 : || (sym->attr.recursive) || (sym->attr.pure))
15290 : : {
15291 : 4 : if (sym->as && sym->as->rank)
15292 : 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
15293 : : "array-valued", sym->name, &sym->declared_at);
15294 : :
15295 : 4 : if (sym->attr.pointer)
15296 : 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
15297 : : "pointer-valued", sym->name, &sym->declared_at);
15298 : :
15299 : 4 : if (sym->attr.pure)
15300 : 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
15301 : : "pure", sym->name, &sym->declared_at);
15302 : :
15303 : 4 : if (sym->attr.recursive)
15304 : 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
15305 : : "recursive", sym->name, &sym->declared_at);
15306 : :
15307 : 4 : return false;
15308 : : }
15309 : :
15310 : : /* Appendix B.2 of the standard. Contained functions give an
15311 : : error anyway. Deferred character length is an F2003 feature.
15312 : : Don't warn on intrinsic conversion functions, which start
15313 : : with two underscores. */
15314 : 176 : if (!sym->attr.contained && !sym->ts.deferred
15315 : 172 : && (sym->name[0] != '_' || sym->name[1] != '_'))
15316 : 172 : gfc_notify_std (GFC_STD_F95_OBS,
15317 : : "CHARACTER(*) function %qs at %L",
15318 : : sym->name, &sym->declared_at);
15319 : : }
15320 : :
15321 : : /* F2008, C1218. */
15322 : 463916 : if (sym->attr.elemental)
15323 : : {
15324 : 89274 : if (sym->attr.proc_pointer)
15325 : : {
15326 : 7 : const char* name = (sym->attr.result ? sym->ns->proc_name->name
15327 : : : sym->name);
15328 : 7 : gfc_error ("Procedure pointer %qs at %L shall not be elemental",
15329 : : name, &sym->declared_at);
15330 : 7 : return false;
15331 : : }
15332 : 89267 : if (sym->attr.dummy)
15333 : : {
15334 : 3 : gfc_error ("Dummy procedure %qs at %L shall not be elemental",
15335 : : sym->name, &sym->declared_at);
15336 : 3 : return false;
15337 : : }
15338 : : }
15339 : :
15340 : : /* F2018, C15100: "The result of an elemental function shall be scalar,
15341 : : and shall not have the POINTER or ALLOCATABLE attribute." The scalar
15342 : : pointer is tested and caught elsewhere. */
15343 : 463906 : if (sym->result)
15344 : 260994 : allocatable_or_pointer = sym->result->ts.type == BT_CLASS
15345 : 260994 : && CLASS_DATA (sym->result) ?
15346 : 1653 : (CLASS_DATA (sym->result)->attr.allocatable
15347 : 1653 : || CLASS_DATA (sym->result)->attr.pointer) :
15348 : 259341 : (sym->result->attr.allocatable
15349 : 259341 : || sym->result->attr.pointer);
15350 : :
15351 : 463906 : if (sym->attr.elemental && sym->result
15352 : 85729 : && allocatable_or_pointer)
15353 : : {
15354 : 4 : gfc_error ("Function result variable %qs at %L of elemental "
15355 : : "function %qs shall not have an ALLOCATABLE or POINTER "
15356 : : "attribute", sym->result->name,
15357 : : &sym->result->declared_at, sym->name);
15358 : 4 : return false;
15359 : : }
15360 : :
15361 : 463902 : if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
15362 : : {
15363 : 6573 : gfc_formal_arglist *curr_arg;
15364 : 6573 : int has_non_interop_arg = 0;
15365 : :
15366 : 6573 : if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
15367 : 6573 : sym->common_block))
15368 : : {
15369 : : /* Clear these to prevent looking at them again if there was an
15370 : : error. */
15371 : 2 : sym->attr.is_bind_c = 0;
15372 : 2 : sym->attr.is_c_interop = 0;
15373 : 2 : sym->ts.is_c_interop = 0;
15374 : : }
15375 : : else
15376 : : {
15377 : : /* So far, no errors have been found. */
15378 : 6571 : sym->attr.is_c_interop = 1;
15379 : 6571 : sym->ts.is_c_interop = 1;
15380 : : }
15381 : :
15382 : 6573 : curr_arg = gfc_sym_get_dummy_args (sym);
15383 : 29544 : while (curr_arg != NULL)
15384 : : {
15385 : : /* Skip implicitly typed dummy args here. */
15386 : 16398 : if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
15387 : 16342 : if (!gfc_verify_c_interop_param (curr_arg->sym))
15388 : : /* If something is found to fail, record the fact so we
15389 : : can mark the symbol for the procedure as not being
15390 : : BIND(C) to try and prevent multiple errors being
15391 : : reported. */
15392 : 16398 : has_non_interop_arg = 1;
15393 : :
15394 : 16398 : curr_arg = curr_arg->next;
15395 : : }
15396 : :
15397 : : /* See if any of the arguments were not interoperable and if so, clear
15398 : : the procedure symbol to prevent duplicate error messages. */
15399 : 6573 : if (has_non_interop_arg != 0)
15400 : : {
15401 : 128 : sym->attr.is_c_interop = 0;
15402 : 128 : sym->ts.is_c_interop = 0;
15403 : 128 : sym->attr.is_bind_c = 0;
15404 : : }
15405 : : }
15406 : :
15407 : 463902 : if (!sym->attr.proc_pointer)
15408 : : {
15409 : 462858 : if (sym->attr.save == SAVE_EXPLICIT)
15410 : : {
15411 : 5 : gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
15412 : : "in %qs at %L", sym->name, &sym->declared_at);
15413 : 5 : return false;
15414 : : }
15415 : 462853 : if (sym->attr.intent)
15416 : : {
15417 : 1 : gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
15418 : : "in %qs at %L", sym->name, &sym->declared_at);
15419 : 1 : return false;
15420 : : }
15421 : 462852 : if (sym->attr.subroutine && sym->attr.result)
15422 : : {
15423 : 2 : gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
15424 : 2 : "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
15425 : 2 : return false;
15426 : : }
15427 : 462850 : if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
15428 : 132483 : && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
15429 : 132480 : || sym->attr.contained))
15430 : : {
15431 : 3 : gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
15432 : : "in %qs at %L", sym->name, &sym->declared_at);
15433 : 3 : return false;
15434 : : }
15435 : 462847 : if (strcmp ("ppr@", sym->name) == 0)
15436 : : {
15437 : 0 : gfc_error ("Procedure pointer result %qs at %L "
15438 : : "is missing the pointer attribute",
15439 : 0 : sym->ns->proc_name->name, &sym->declared_at);
15440 : 0 : return false;
15441 : : }
15442 : : }
15443 : :
15444 : : /* Assume that a procedure whose body is not known has references
15445 : : to external arrays. */
15446 : 463891 : if (sym->attr.if_source != IFSRC_DECL)
15447 : 319858 : sym->attr.array_outer_dependency = 1;
15448 : :
15449 : : /* Compare the characteristics of a module procedure with the
15450 : : interface declaration. Ideally this would be done with
15451 : : gfc_compare_interfaces but, at present, the formal interface
15452 : : cannot be copied to the ts.interface. */
15453 : 463891 : if (sym->attr.module_procedure
15454 : 1436 : && sym->attr.if_source == IFSRC_DECL)
15455 : : {
15456 : 604 : gfc_symbol *iface;
15457 : 604 : char name[2*GFC_MAX_SYMBOL_LEN + 1];
15458 : 604 : char *module_name;
15459 : 604 : char *submodule_name;
15460 : 604 : strcpy (name, sym->ns->proc_name->name);
15461 : 604 : module_name = strtok (name, ".");
15462 : 604 : submodule_name = strtok (NULL, ".");
15463 : :
15464 : 604 : iface = sym->tlink;
15465 : 604 : sym->tlink = NULL;
15466 : :
15467 : : /* Make sure that the result uses the correct charlen for deferred
15468 : : length results. */
15469 : 604 : if (iface && sym->result
15470 : 170 : && iface->ts.type == BT_CHARACTER
15471 : 19 : && iface->ts.deferred)
15472 : 6 : sym->result->ts.u.cl = iface->ts.u.cl;
15473 : :
15474 : 6 : if (iface == NULL)
15475 : 193 : goto check_formal;
15476 : :
15477 : : /* Check the procedure characteristics. */
15478 : 411 : if (sym->attr.elemental != iface->attr.elemental)
15479 : : {
15480 : 1 : gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
15481 : : "PROCEDURE at %L and its interface in %s",
15482 : : &sym->declared_at, module_name);
15483 : 10 : return false;
15484 : : }
15485 : :
15486 : 410 : if (sym->attr.pure != iface->attr.pure)
15487 : : {
15488 : 2 : gfc_error ("Mismatch in PURE attribute between MODULE "
15489 : : "PROCEDURE at %L and its interface in %s",
15490 : : &sym->declared_at, module_name);
15491 : 2 : return false;
15492 : : }
15493 : :
15494 : 408 : if (sym->attr.recursive != iface->attr.recursive)
15495 : : {
15496 : 2 : gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
15497 : : "PROCEDURE at %L and its interface in %s",
15498 : : &sym->declared_at, module_name);
15499 : 2 : return false;
15500 : : }
15501 : :
15502 : : /* Check the result characteristics. */
15503 : 406 : if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
15504 : : {
15505 : 5 : gfc_error ("%s between the MODULE PROCEDURE declaration "
15506 : : "in MODULE %qs and the declaration at %L in "
15507 : : "(SUB)MODULE %qs",
15508 : : errmsg, module_name, &sym->declared_at,
15509 : : submodule_name ? submodule_name : module_name);
15510 : 5 : return false;
15511 : : }
15512 : :
15513 : 401 : check_formal:
15514 : : /* Check the characteristics of the formal arguments. */
15515 : 594 : if (sym->formal && sym->formal_ns)
15516 : : {
15517 : 1162 : for (arg = sym->formal; arg && arg->sym; arg = arg->next)
15518 : : {
15519 : 667 : new_formal = arg;
15520 : 667 : gfc_traverse_ns (sym->formal_ns, compare_fsyms);
15521 : : }
15522 : : }
15523 : : }
15524 : :
15525 : : /* F2018:15.4.2.2 requires an explicit interface for procedures with the
15526 : : BIND(C) attribute. */
15527 : 463881 : if (sym->attr.is_bind_c && sym->attr.if_source == IFSRC_UNKNOWN)
15528 : : {
15529 : 1 : gfc_error ("Interface of %qs at %L must be explicit",
15530 : : sym->name, &sym->declared_at);
15531 : 1 : return false;
15532 : : }
15533 : :
15534 : : return true;
15535 : : }
15536 : :
15537 : :
15538 : : /* Resolve a list of finalizer procedures. That is, after they have hopefully
15539 : : been defined and we now know their defined arguments, check that they fulfill
15540 : : the requirements of the standard for procedures used as finalizers. */
15541 : :
15542 : : static bool
15543 : 106927 : gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
15544 : : {
15545 : 106927 : gfc_finalizer* list;
15546 : 106927 : gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
15547 : 106927 : bool result = true;
15548 : 106927 : bool seen_scalar = false;
15549 : 106927 : gfc_symbol *vtab;
15550 : 106927 : gfc_component *c;
15551 : 106927 : gfc_symbol *parent = gfc_get_derived_super_type (derived);
15552 : :
15553 : 106927 : if (parent)
15554 : 14871 : gfc_resolve_finalizers (parent, finalizable);
15555 : :
15556 : : /* Ensure that derived-type components have a their finalizers resolved. */
15557 : 106927 : bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
15558 : 336278 : for (c = derived->components; c; c = c->next)
15559 : 229351 : if (c->ts.type == BT_DERIVED
15560 : 64183 : && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
15561 : : {
15562 : 7790 : bool has_final2 = false;
15563 : 7790 : if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
15564 : 0 : return false; /* Error. */
15565 : 7790 : has_final = has_final || has_final2;
15566 : : }
15567 : : /* Return early if not finalizable. */
15568 : 106927 : if (!has_final)
15569 : : {
15570 : 104663 : if (finalizable)
15571 : 7748 : *finalizable = false;
15572 : 104663 : return true;
15573 : : }
15574 : :
15575 : : /* Walk over the list of finalizer-procedures, check them, and if any one
15576 : : does not fit in with the standard's definition, print an error and remove
15577 : : it from the list. */
15578 : 2264 : prev_link = &derived->f2k_derived->finalizers;
15579 : 4672 : for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
15580 : : {
15581 : 2408 : gfc_formal_arglist *dummy_args;
15582 : 2408 : gfc_symbol* arg;
15583 : 2408 : gfc_finalizer* i;
15584 : 2408 : int my_rank;
15585 : :
15586 : : /* Skip this finalizer if we already resolved it. */
15587 : 2408 : if (list->proc_tree)
15588 : : {
15589 : 1939 : if (list->proc_tree->n.sym->formal->sym->as == NULL
15590 : 566 : || list->proc_tree->n.sym->formal->sym->as->rank == 0)
15591 : 1373 : seen_scalar = true;
15592 : 1939 : prev_link = &(list->next);
15593 : 1939 : continue;
15594 : : }
15595 : :
15596 : : /* Check this exists and is a SUBROUTINE. */
15597 : 469 : if (!list->proc_sym->attr.subroutine)
15598 : : {
15599 : 3 : gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
15600 : : list->proc_sym->name, &list->where);
15601 : 3 : goto error;
15602 : : }
15603 : :
15604 : : /* We should have exactly one argument. */
15605 : 466 : dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
15606 : 466 : if (!dummy_args || dummy_args->next)
15607 : : {
15608 : 2 : gfc_error ("FINAL procedure at %L must have exactly one argument",
15609 : : &list->where);
15610 : 2 : goto error;
15611 : : }
15612 : 464 : arg = dummy_args->sym;
15613 : :
15614 : 464 : if (!arg)
15615 : : {
15616 : 1 : gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
15617 : 1 : &list->proc_sym->declared_at, derived->name);
15618 : 1 : goto error;
15619 : : }
15620 : :
15621 : 463 : if (arg->as && arg->as->type == AS_ASSUMED_RANK
15622 : 6 : && ((list != derived->f2k_derived->finalizers) || list->next))
15623 : : {
15624 : 0 : gfc_error ("FINAL procedure at %L with assumed rank argument must "
15625 : : "be the only finalizer with the same kind/type "
15626 : : "(F2018: C790)", &list->where);
15627 : 0 : goto error;
15628 : : }
15629 : :
15630 : : /* This argument must be of our type. */
15631 : 463 : if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
15632 : : {
15633 : 2 : gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
15634 : : &arg->declared_at, derived->name);
15635 : 2 : goto error;
15636 : : }
15637 : :
15638 : : /* It must neither be a pointer nor allocatable nor optional. */
15639 : 461 : if (arg->attr.pointer)
15640 : : {
15641 : 1 : gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
15642 : : &arg->declared_at);
15643 : 1 : goto error;
15644 : : }
15645 : 460 : if (arg->attr.allocatable)
15646 : : {
15647 : 1 : gfc_error ("Argument of FINAL procedure at %L must not be"
15648 : : " ALLOCATABLE", &arg->declared_at);
15649 : 1 : goto error;
15650 : : }
15651 : 459 : if (arg->attr.optional)
15652 : : {
15653 : 1 : gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
15654 : : &arg->declared_at);
15655 : 1 : goto error;
15656 : : }
15657 : :
15658 : : /* It must not be INTENT(OUT). */
15659 : 458 : if (arg->attr.intent == INTENT_OUT)
15660 : : {
15661 : 1 : gfc_error ("Argument of FINAL procedure at %L must not be"
15662 : : " INTENT(OUT)", &arg->declared_at);
15663 : 1 : goto error;
15664 : : }
15665 : :
15666 : : /* Warn if the procedure is non-scalar and not assumed shape. */
15667 : 457 : if (warn_surprising && arg->as && arg->as->rank != 0
15668 : 3 : && arg->as->type != AS_ASSUMED_SHAPE)
15669 : 2 : gfc_warning (OPT_Wsurprising,
15670 : : "Non-scalar FINAL procedure at %L should have assumed"
15671 : : " shape argument", &arg->declared_at);
15672 : :
15673 : : /* Check that it does not match in kind and rank with a FINAL procedure
15674 : : defined earlier. To really loop over the *earlier* declarations,
15675 : : we need to walk the tail of the list as new ones were pushed at the
15676 : : front. */
15677 : : /* TODO: Handle kind parameters once they are implemented. */
15678 : 457 : my_rank = (arg->as ? arg->as->rank : 0);
15679 : 546 : for (i = list->next; i; i = i->next)
15680 : : {
15681 : 91 : gfc_formal_arglist *dummy_args;
15682 : :
15683 : : /* Argument list might be empty; that is an error signalled earlier,
15684 : : but we nevertheless continued resolving. */
15685 : 91 : dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
15686 : 91 : if (dummy_args)
15687 : : {
15688 : 89 : gfc_symbol* i_arg = dummy_args->sym;
15689 : 89 : const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
15690 : 89 : if (i_rank == my_rank)
15691 : : {
15692 : 2 : gfc_error ("FINAL procedure %qs declared at %L has the same"
15693 : : " rank (%d) as %qs",
15694 : 2 : list->proc_sym->name, &list->where, my_rank,
15695 : 2 : i->proc_sym->name);
15696 : 2 : goto error;
15697 : : }
15698 : : }
15699 : : }
15700 : :
15701 : : /* Is this the/a scalar finalizer procedure? */
15702 : 455 : if (my_rank == 0)
15703 : 335 : seen_scalar = true;
15704 : :
15705 : : /* Find the symtree for this procedure. */
15706 : 455 : gcc_assert (!list->proc_tree);
15707 : 455 : list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
15708 : :
15709 : 455 : prev_link = &list->next;
15710 : 455 : continue;
15711 : :
15712 : : /* Remove wrong nodes immediately from the list so we don't risk any
15713 : : troubles in the future when they might fail later expectations. */
15714 : 14 : error:
15715 : 14 : i = list;
15716 : 14 : *prev_link = list->next;
15717 : 14 : gfc_free_finalizer (i);
15718 : 14 : result = false;
15719 : 455 : }
15720 : :
15721 : 2264 : if (result == false)
15722 : : return false;
15723 : :
15724 : : /* Warn if we haven't seen a scalar finalizer procedure (but we know there
15725 : : were nodes in the list, must have been for arrays. It is surely a good
15726 : : idea to have a scalar version there if there's something to finalize. */
15727 : 2260 : if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
15728 : 1 : gfc_warning (OPT_Wsurprising,
15729 : : "Only array FINAL procedures declared for derived type %qs"
15730 : : " defined at %L, suggest also scalar one unless an assumed"
15731 : : " rank finalizer has been declared",
15732 : : derived->name, &derived->declared_at);
15733 : :
15734 : 2260 : vtab = gfc_find_derived_vtab (derived);
15735 : 2260 : c = vtab->ts.u.derived->components->next->next->next->next->next;
15736 : 2260 : gfc_set_sym_referenced (c->initializer->symtree->n.sym);
15737 : :
15738 : 2260 : if (finalizable)
15739 : 590 : *finalizable = true;
15740 : :
15741 : : return true;
15742 : : }
15743 : :
15744 : :
15745 : : static gfc_symbol * containing_dt;
15746 : :
15747 : : /* Helper function for check_generic_tbp_ambiguity, which ensures that passed
15748 : : arguments whose declared types are PDT instances only transmit the PASS arg
15749 : : if they match the enclosing derived type. */
15750 : :
15751 : : static bool
15752 : 1404 : check_pdt_args (gfc_tbp_generic* t, const char *pass)
15753 : : {
15754 : 1404 : gfc_formal_arglist *dummy_args;
15755 : 1404 : if (pass && containing_dt != NULL && containing_dt->attr.pdt_type)
15756 : : {
15757 : 504 : dummy_args = gfc_sym_get_dummy_args (t->specific->u.specific->n.sym);
15758 : 1134 : while (dummy_args && strcmp (pass, dummy_args->sym->name))
15759 : 126 : dummy_args = dummy_args->next;
15760 : 504 : gcc_assert (strcmp (pass, dummy_args->sym->name) == 0);
15761 : 504 : if (dummy_args->sym->ts.type == BT_CLASS
15762 : 504 : && strcmp (CLASS_DATA (dummy_args->sym)->ts.u.derived->name,
15763 : : containing_dt->name))
15764 : : return true;
15765 : : }
15766 : : return false;
15767 : : }
15768 : :
15769 : :
15770 : : /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
15771 : :
15772 : : static bool
15773 : 704 : check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
15774 : : const char* generic_name, locus where)
15775 : : {
15776 : 704 : gfc_symbol *sym1, *sym2;
15777 : 704 : const char *pass1, *pass2;
15778 : 704 : gfc_formal_arglist *dummy_args;
15779 : :
15780 : 704 : gcc_assert (t1->specific && t2->specific);
15781 : 704 : gcc_assert (!t1->specific->is_generic);
15782 : 704 : gcc_assert (!t2->specific->is_generic);
15783 : 704 : gcc_assert (t1->is_operator == t2->is_operator);
15784 : :
15785 : 704 : sym1 = t1->specific->u.specific->n.sym;
15786 : 704 : sym2 = t2->specific->u.specific->n.sym;
15787 : :
15788 : 704 : if (sym1 == sym2)
15789 : : return true;
15790 : :
15791 : : /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
15792 : 704 : if (sym1->attr.subroutine != sym2->attr.subroutine
15793 : 702 : || sym1->attr.function != sym2->attr.function)
15794 : : {
15795 : 2 : gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
15796 : : " GENERIC %qs at %L",
15797 : : sym1->name, sym2->name, generic_name, &where);
15798 : 2 : return false;
15799 : : }
15800 : :
15801 : : /* Determine PASS arguments. */
15802 : 702 : if (t1->specific->nopass)
15803 : : pass1 = NULL;
15804 : 651 : else if (t1->specific->pass_arg)
15805 : : pass1 = t1->specific->pass_arg;
15806 : : else
15807 : : {
15808 : 392 : dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
15809 : 392 : if (dummy_args)
15810 : 391 : pass1 = dummy_args->sym->name;
15811 : : else
15812 : : pass1 = NULL;
15813 : : }
15814 : 702 : if (t2->specific->nopass)
15815 : : pass2 = NULL;
15816 : 650 : else if (t2->specific->pass_arg)
15817 : : pass2 = t2->specific->pass_arg;
15818 : : else
15819 : : {
15820 : 513 : dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
15821 : 513 : if (dummy_args)
15822 : 512 : pass2 = dummy_args->sym->name;
15823 : : else
15824 : : pass2 = NULL;
15825 : : }
15826 : :
15827 : : /* Care must be taken with pdt types and templates because the declared type
15828 : : of the argument that is not 'no_pass' need not be the same as the
15829 : : containing derived type. If this is the case, subject the argument to
15830 : : the full interface check, even though it cannot be used in the type
15831 : : bound context. */
15832 : 702 : pass1 = check_pdt_args (t1, pass1) ? NULL : pass1;
15833 : 702 : pass2 = check_pdt_args (t2, pass2) ? NULL : pass2;
15834 : :
15835 : 702 : if (containing_dt != NULL && containing_dt->attr.pdt_template)
15836 : 702 : pass1 = pass2 = NULL;
15837 : :
15838 : : /* Compare the interfaces. */
15839 : 702 : if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
15840 : : NULL, 0, pass1, pass2))
15841 : : {
15842 : 8 : gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
15843 : : sym1->name, sym2->name, generic_name, &where);
15844 : 8 : return false;
15845 : : }
15846 : :
15847 : : return true;
15848 : : }
15849 : :
15850 : :
15851 : : /* Worker function for resolving a generic procedure binding; this is used to
15852 : : resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
15853 : :
15854 : : The difference between those cases is finding possible inherited bindings
15855 : : that are overridden, as one has to look for them in tb_sym_root,
15856 : : tb_uop_root or tb_op, respectively. Thus the caller must already find
15857 : : the super-type and set p->overridden correctly. */
15858 : :
15859 : : static bool
15860 : 2153 : resolve_tb_generic_targets (gfc_symbol* super_type,
15861 : : gfc_typebound_proc* p, const char* name)
15862 : : {
15863 : 2153 : gfc_tbp_generic* target;
15864 : 2153 : gfc_symtree* first_target;
15865 : 2153 : gfc_symtree* inherited;
15866 : :
15867 : 2153 : gcc_assert (p && p->is_generic);
15868 : :
15869 : : /* Try to find the specific bindings for the symtrees in our target-list. */
15870 : 2153 : gcc_assert (p->u.generic);
15871 : 4838 : for (target = p->u.generic; target; target = target->next)
15872 : 2702 : if (!target->specific)
15873 : : {
15874 : 2396 : gfc_typebound_proc* overridden_tbp;
15875 : 2396 : gfc_tbp_generic* g;
15876 : 2396 : const char* target_name;
15877 : :
15878 : 2396 : target_name = target->specific_st->name;
15879 : :
15880 : : /* Defined for this type directly. */
15881 : 2396 : if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
15882 : : {
15883 : 2387 : target->specific = target->specific_st->n.tb;
15884 : 2387 : goto specific_found;
15885 : : }
15886 : :
15887 : : /* Look for an inherited specific binding. */
15888 : 9 : if (super_type)
15889 : : {
15890 : 5 : inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
15891 : : true, NULL);
15892 : :
15893 : 5 : if (inherited)
15894 : : {
15895 : 5 : gcc_assert (inherited->n.tb);
15896 : 5 : target->specific = inherited->n.tb;
15897 : 5 : goto specific_found;
15898 : : }
15899 : : }
15900 : :
15901 : 4 : gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
15902 : : " at %L", target_name, name, &p->where);
15903 : 4 : return false;
15904 : :
15905 : : /* Once we've found the specific binding, check it is not ambiguous with
15906 : : other specifics already found or inherited for the same GENERIC. */
15907 : 2392 : specific_found:
15908 : 2392 : gcc_assert (target->specific);
15909 : :
15910 : : /* This must really be a specific binding! */
15911 : 2392 : if (target->specific->is_generic)
15912 : : {
15913 : 3 : gfc_error ("GENERIC %qs at %L must target a specific binding,"
15914 : : " %qs is GENERIC, too", name, &p->where, target_name);
15915 : 3 : return false;
15916 : : }
15917 : :
15918 : : /* Check those already resolved on this type directly. */
15919 : 6136 : for (g = p->u.generic; g; g = g->next)
15920 : 1372 : if (g != target && g->specific
15921 : 4440 : && !check_generic_tbp_ambiguity (target, g, name, p->where))
15922 : : return false;
15923 : :
15924 : : /* Check for ambiguity with inherited specific targets. */
15925 : 2398 : for (overridden_tbp = p->overridden; overridden_tbp;
15926 : 16 : overridden_tbp = overridden_tbp->overridden)
15927 : 19 : if (overridden_tbp->is_generic)
15928 : : {
15929 : 33 : for (g = overridden_tbp->u.generic; g; g = g->next)
15930 : : {
15931 : 18 : gcc_assert (g->specific);
15932 : 18 : if (!check_generic_tbp_ambiguity (target, g, name, p->where))
15933 : : return false;
15934 : : }
15935 : : }
15936 : : }
15937 : :
15938 : : /* If we attempt to "overwrite" a specific binding, this is an error. */
15939 : 2136 : if (p->overridden && !p->overridden->is_generic)
15940 : : {
15941 : 1 : gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
15942 : : " the same name", name, &p->where);
15943 : 1 : return false;
15944 : : }
15945 : :
15946 : : /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
15947 : : all must have the same attributes here. */
15948 : 2135 : first_target = p->u.generic->specific->u.specific;
15949 : 2135 : gcc_assert (first_target);
15950 : 2135 : p->subroutine = first_target->n.sym->attr.subroutine;
15951 : 2135 : p->function = first_target->n.sym->attr.function;
15952 : :
15953 : 2135 : return true;
15954 : : }
15955 : :
15956 : :
15957 : : /* Resolve a GENERIC procedure binding for a derived type. */
15958 : :
15959 : : static bool
15960 : 1141 : resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
15961 : : {
15962 : 1141 : gfc_symbol* super_type;
15963 : :
15964 : : /* Find the overridden binding if any. */
15965 : 1141 : st->n.tb->overridden = NULL;
15966 : 1141 : super_type = gfc_get_derived_super_type (derived);
15967 : 1141 : if (super_type)
15968 : : {
15969 : 40 : gfc_symtree* overridden;
15970 : 40 : overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
15971 : : true, NULL);
15972 : :
15973 : 40 : if (overridden && overridden->n.tb)
15974 : 21 : st->n.tb->overridden = overridden->n.tb;
15975 : : }
15976 : :
15977 : : /* Resolve using worker function. */
15978 : 1141 : return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
15979 : : }
15980 : :
15981 : :
15982 : : /* Retrieve the target-procedure of an operator binding and do some checks in
15983 : : common for intrinsic and user-defined type-bound operators. */
15984 : :
15985 : : static gfc_symbol*
15986 : 1076 : get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
15987 : : {
15988 : 1076 : gfc_symbol* target_proc;
15989 : :
15990 : 1076 : gcc_assert (target->specific && !target->specific->is_generic);
15991 : 1076 : target_proc = target->specific->u.specific->n.sym;
15992 : 1076 : gcc_assert (target_proc);
15993 : :
15994 : : /* F08:C468. All operator bindings must have a passed-object dummy argument. */
15995 : 1076 : if (target->specific->nopass)
15996 : : {
15997 : 2 : gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
15998 : 2 : return NULL;
15999 : : }
16000 : :
16001 : : return target_proc;
16002 : : }
16003 : :
16004 : :
16005 : : /* Resolve a type-bound intrinsic operator. */
16006 : :
16007 : : static bool
16008 : 958 : resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
16009 : : gfc_typebound_proc* p)
16010 : : {
16011 : 958 : gfc_symbol* super_type;
16012 : 958 : gfc_tbp_generic* target;
16013 : :
16014 : : /* If there's already an error here, do nothing (but don't fail again). */
16015 : 958 : if (p->error)
16016 : : return true;
16017 : :
16018 : : /* Operators should always be GENERIC bindings. */
16019 : 958 : gcc_assert (p->is_generic);
16020 : :
16021 : : /* Look for an overridden binding. */
16022 : 958 : super_type = gfc_get_derived_super_type (derived);
16023 : 958 : if (super_type && super_type->f2k_derived)
16024 : 1 : p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
16025 : : op, true, NULL);
16026 : : else
16027 : 957 : p->overridden = NULL;
16028 : :
16029 : : /* Resolve general GENERIC properties using worker function. */
16030 : 958 : if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
16031 : 1 : goto error;
16032 : :
16033 : : /* Check the targets to be procedures of correct interface. */
16034 : 1958 : for (target = p->u.generic; target; target = target->next)
16035 : : {
16036 : 1021 : gfc_symbol* target_proc;
16037 : :
16038 : 1021 : target_proc = get_checked_tb_operator_target (target, p->where);
16039 : 1021 : if (!target_proc)
16040 : 1 : goto error;
16041 : :
16042 : 1020 : if (!gfc_check_operator_interface (target_proc, op, p->where))
16043 : 3 : goto error;
16044 : :
16045 : : /* Add target to non-typebound operator list. */
16046 : 1017 : if (!target->specific->deferred && !derived->attr.use_assoc
16047 : 358 : && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
16048 : : {
16049 : 356 : gfc_interface *head, *intr;
16050 : :
16051 : : /* Preempt 'gfc_check_new_interface' for submodules, where the
16052 : : mechanism for handling module procedures winds up resolving
16053 : : operator interfaces twice and would otherwise cause an error. */
16054 : 424 : for (intr = derived->ns->op[op]; intr; intr = intr->next)
16055 : 82 : if (intr->sym == target_proc
16056 : 16 : && target_proc->attr.used_in_submodule)
16057 : : return true;
16058 : :
16059 : 342 : if (!gfc_check_new_interface (derived->ns->op[op],
16060 : : target_proc, p->where))
16061 : : return false;
16062 : 340 : head = derived->ns->op[op];
16063 : 340 : intr = gfc_get_interface ();
16064 : 340 : intr->sym = target_proc;
16065 : 340 : intr->where = p->where;
16066 : 340 : intr->next = head;
16067 : 340 : derived->ns->op[op] = intr;
16068 : : }
16069 : : }
16070 : :
16071 : : return true;
16072 : :
16073 : 5 : error:
16074 : 5 : p->error = 1;
16075 : 5 : return false;
16076 : : }
16077 : :
16078 : :
16079 : : /* Resolve a type-bound user operator (tree-walker callback). */
16080 : :
16081 : : static gfc_symbol* resolve_bindings_derived;
16082 : : static bool resolve_bindings_result;
16083 : :
16084 : : static bool check_uop_procedure (gfc_symbol* sym, locus where);
16085 : :
16086 : : static void
16087 : 54 : resolve_typebound_user_op (gfc_symtree* stree)
16088 : : {
16089 : 54 : gfc_symbol* super_type;
16090 : 54 : gfc_tbp_generic* target;
16091 : :
16092 : 54 : gcc_assert (stree && stree->n.tb);
16093 : :
16094 : 54 : if (stree->n.tb->error)
16095 : : return;
16096 : :
16097 : : /* Operators should always be GENERIC bindings. */
16098 : 54 : gcc_assert (stree->n.tb->is_generic);
16099 : :
16100 : : /* Find overridden procedure, if any. */
16101 : 54 : super_type = gfc_get_derived_super_type (resolve_bindings_derived);
16102 : 54 : if (super_type && super_type->f2k_derived)
16103 : : {
16104 : 0 : gfc_symtree* overridden;
16105 : 0 : overridden = gfc_find_typebound_user_op (super_type, NULL,
16106 : : stree->name, true, NULL);
16107 : :
16108 : 0 : if (overridden && overridden->n.tb)
16109 : 0 : stree->n.tb->overridden = overridden->n.tb;
16110 : : }
16111 : : else
16112 : 54 : stree->n.tb->overridden = NULL;
16113 : :
16114 : : /* Resolve basically using worker function. */
16115 : 54 : if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
16116 : 0 : goto error;
16117 : :
16118 : : /* Check the targets to be functions of correct interface. */
16119 : 106 : for (target = stree->n.tb->u.generic; target; target = target->next)
16120 : : {
16121 : 55 : gfc_symbol* target_proc;
16122 : :
16123 : 55 : target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
16124 : 55 : if (!target_proc)
16125 : 1 : goto error;
16126 : :
16127 : 54 : if (!check_uop_procedure (target_proc, stree->n.tb->where))
16128 : 2 : goto error;
16129 : : }
16130 : :
16131 : : return;
16132 : :
16133 : 3 : error:
16134 : 3 : resolve_bindings_result = false;
16135 : 3 : stree->n.tb->error = 1;
16136 : : }
16137 : :
16138 : :
16139 : : /* Resolve the type-bound procedures for a derived type. */
16140 : :
16141 : : static void
16142 : 9580 : resolve_typebound_procedure (gfc_symtree* stree)
16143 : : {
16144 : 9580 : gfc_symbol* proc;
16145 : 9580 : locus where;
16146 : 9580 : gfc_symbol* me_arg;
16147 : 9580 : gfc_symbol* super_type;
16148 : 9580 : gfc_component* comp;
16149 : :
16150 : 9580 : gcc_assert (stree);
16151 : :
16152 : : /* Undefined specific symbol from GENERIC target definition. */
16153 : 9580 : if (!stree->n.tb)
16154 : 9498 : return;
16155 : :
16156 : 9574 : if (stree->n.tb->error)
16157 : : return;
16158 : :
16159 : : /* If this is a GENERIC binding, use that routine. */
16160 : 9558 : if (stree->n.tb->is_generic)
16161 : : {
16162 : 1141 : if (!resolve_typebound_generic (resolve_bindings_derived, stree))
16163 : 17 : goto error;
16164 : : return;
16165 : : }
16166 : :
16167 : : /* Get the target-procedure to check it. */
16168 : 8417 : gcc_assert (!stree->n.tb->is_generic);
16169 : 8417 : gcc_assert (stree->n.tb->u.specific);
16170 : 8417 : proc = stree->n.tb->u.specific->n.sym;
16171 : 8417 : where = stree->n.tb->where;
16172 : :
16173 : : /* Default access should already be resolved from the parser. */
16174 : 8417 : gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
16175 : :
16176 : 8417 : if (stree->n.tb->deferred)
16177 : : {
16178 : 671 : if (!check_proc_interface (proc, &where))
16179 : 5 : goto error;
16180 : : }
16181 : : else
16182 : : {
16183 : : /* If proc has not been resolved at this point, proc->name may
16184 : : actually be a USE associated entity. See PR fortran/89647. */
16185 : 7746 : if (!proc->resolve_symbol_called
16186 : 5195 : && proc->attr.function == 0 && proc->attr.subroutine == 0)
16187 : : {
16188 : 11 : gfc_symbol *tmp;
16189 : 11 : gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
16190 : 11 : if (tmp && tmp->attr.use_assoc)
16191 : : {
16192 : 1 : proc->module = tmp->module;
16193 : 1 : proc->attr.proc = tmp->attr.proc;
16194 : 1 : proc->attr.function = tmp->attr.function;
16195 : 1 : proc->attr.subroutine = tmp->attr.subroutine;
16196 : 1 : proc->attr.use_assoc = tmp->attr.use_assoc;
16197 : 1 : proc->ts = tmp->ts;
16198 : 1 : proc->result = tmp->result;
16199 : : }
16200 : : }
16201 : :
16202 : : /* Check for F08:C465. */
16203 : 7746 : if ((!proc->attr.subroutine && !proc->attr.function)
16204 : 7736 : || (proc->attr.proc != PROC_MODULE
16205 : 57 : && proc->attr.if_source != IFSRC_IFBODY
16206 : 7 : && !proc->attr.module_procedure)
16207 : 7735 : || proc->attr.abstract)
16208 : : {
16209 : 12 : gfc_error ("%qs must be a module procedure or an external "
16210 : : "procedure with an explicit interface at %L",
16211 : : proc->name, &where);
16212 : 12 : goto error;
16213 : : }
16214 : : }
16215 : :
16216 : 8400 : stree->n.tb->subroutine = proc->attr.subroutine;
16217 : 8400 : stree->n.tb->function = proc->attr.function;
16218 : :
16219 : : /* Find the super-type of the current derived type. We could do this once and
16220 : : store in a global if speed is needed, but as long as not I believe this is
16221 : : more readable and clearer. */
16222 : 8400 : super_type = gfc_get_derived_super_type (resolve_bindings_derived);
16223 : :
16224 : : /* If PASS, resolve and check arguments if not already resolved / loaded
16225 : : from a .mod file. */
16226 : 8400 : if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
16227 : : {
16228 : 2687 : gfc_formal_arglist *dummy_args;
16229 : :
16230 : 2687 : dummy_args = gfc_sym_get_dummy_args (proc);
16231 : 2687 : if (stree->n.tb->pass_arg)
16232 : : {
16233 : 459 : gfc_formal_arglist *i;
16234 : :
16235 : : /* If an explicit passing argument name is given, walk the arg-list
16236 : : and look for it. */
16237 : :
16238 : 459 : me_arg = NULL;
16239 : 459 : stree->n.tb->pass_arg_num = 1;
16240 : 585 : for (i = dummy_args; i; i = i->next)
16241 : : {
16242 : 583 : if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
16243 : : {
16244 : : me_arg = i->sym;
16245 : : break;
16246 : : }
16247 : 126 : ++stree->n.tb->pass_arg_num;
16248 : : }
16249 : :
16250 : 459 : if (!me_arg)
16251 : : {
16252 : 2 : gfc_error ("Procedure %qs with PASS(%s) at %L has no"
16253 : : " argument %qs",
16254 : : proc->name, stree->n.tb->pass_arg, &where,
16255 : : stree->n.tb->pass_arg);
16256 : 2 : goto error;
16257 : : }
16258 : : }
16259 : : else
16260 : : {
16261 : : /* Otherwise, take the first one; there should in fact be at least
16262 : : one. */
16263 : 2228 : stree->n.tb->pass_arg_num = 1;
16264 : 2228 : if (!dummy_args)
16265 : : {
16266 : 2 : gfc_error ("Procedure %qs with PASS at %L must have at"
16267 : : " least one argument", proc->name, &where);
16268 : 2 : goto error;
16269 : : }
16270 : 2226 : me_arg = dummy_args->sym;
16271 : : }
16272 : :
16273 : : /* Now check that the argument-type matches and the passed-object
16274 : : dummy argument is generally fine. */
16275 : :
16276 : 2226 : gcc_assert (me_arg);
16277 : :
16278 : 2683 : if (me_arg->ts.type != BT_CLASS)
16279 : : {
16280 : 5 : gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
16281 : : " at %L", proc->name, &where);
16282 : 5 : goto error;
16283 : : }
16284 : :
16285 : : /* The derived type is not a PDT template or type. Resolve as usual. */
16286 : 2678 : if (!resolve_bindings_derived->attr.pdt_template
16287 : 2663 : && !(containing_dt && containing_dt->attr.pdt_type
16288 : 42 : && CLASS_DATA (me_arg)->ts.u.derived != containing_dt)
16289 : 2645 : && (CLASS_DATA (me_arg)->ts.u.derived != resolve_bindings_derived))
16290 : : {
16291 : 0 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
16292 : : "the derived-type %qs", me_arg->name, proc->name,
16293 : : me_arg->name, &where, resolve_bindings_derived->name);
16294 : 0 : goto error;
16295 : : }
16296 : :
16297 : 2678 : if (resolve_bindings_derived->attr.pdt_template
16298 : 2693 : && !gfc_pdt_is_instance_of (resolve_bindings_derived,
16299 : 15 : CLASS_DATA (me_arg)->ts.u.derived))
16300 : : {
16301 : 0 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
16302 : : "the parametric derived-type %qs", me_arg->name,
16303 : : proc->name, me_arg->name, &where,
16304 : : resolve_bindings_derived->name);
16305 : 0 : goto error;
16306 : : }
16307 : :
16308 : 2678 : if (resolve_bindings_derived->attr.pdt_template
16309 : 15 : && gfc_pdt_is_instance_of (resolve_bindings_derived,
16310 : 15 : CLASS_DATA (me_arg)->ts.u.derived)
16311 : 15 : && (me_arg->param_list != NULL)
16312 : 2693 : && (gfc_spec_list_type (me_arg->param_list,
16313 : 15 : CLASS_DATA(me_arg)->ts.u.derived)
16314 : : != SPEC_ASSUMED))
16315 : : {
16316 : :
16317 : : /* Add a check to verify if there are any LEN parameters in the
16318 : : first place. If there are LEN parameters, throw this error.
16319 : : If there are only KIND parameters, then don't trigger
16320 : : this error. */
16321 : 6 : gfc_component *c;
16322 : 6 : bool seen_len_param = false;
16323 : 6 : gfc_actual_arglist *me_arg_param = me_arg->param_list;
16324 : :
16325 : 6 : for (; me_arg_param; me_arg_param = me_arg_param->next)
16326 : : {
16327 : 6 : c = gfc_find_component (CLASS_DATA(me_arg)->ts.u.derived,
16328 : : me_arg_param->name, true, true, NULL);
16329 : :
16330 : 6 : gcc_assert (c != NULL);
16331 : :
16332 : 6 : if (c->attr.pdt_kind)
16333 : 0 : continue;
16334 : :
16335 : : /* Getting here implies that there is a pdt_len parameter
16336 : : in the list. */
16337 : : seen_len_param = true;
16338 : : break;
16339 : : }
16340 : :
16341 : 6 : if (seen_len_param)
16342 : : {
16343 : 6 : gfc_error ("All LEN type parameters of the passed dummy "
16344 : : "argument %qs of %qs at %L must be ASSUMED.",
16345 : : me_arg->name, proc->name, &where);
16346 : 6 : goto error;
16347 : : }
16348 : : }
16349 : :
16350 : 2672 : gcc_assert (me_arg->ts.type == BT_CLASS);
16351 : 2672 : if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
16352 : : {
16353 : 1 : gfc_error ("Passed-object dummy argument of %qs at %L must be"
16354 : : " scalar", proc->name, &where);
16355 : 1 : goto error;
16356 : : }
16357 : 2671 : if (CLASS_DATA (me_arg)->attr.allocatable)
16358 : : {
16359 : 2 : gfc_error ("Passed-object dummy argument of %qs at %L must not"
16360 : : " be ALLOCATABLE", proc->name, &where);
16361 : 2 : goto error;
16362 : : }
16363 : 2669 : if (CLASS_DATA (me_arg)->attr.class_pointer)
16364 : : {
16365 : 2 : gfc_error ("Passed-object dummy argument of %qs at %L must not"
16366 : : " be POINTER", proc->name, &where);
16367 : 2 : goto error;
16368 : : }
16369 : : }
16370 : :
16371 : : /* If we are extending some type, check that we don't override a procedure
16372 : : flagged NON_OVERRIDABLE. */
16373 : 8380 : stree->n.tb->overridden = NULL;
16374 : 8380 : if (super_type)
16375 : : {
16376 : 1480 : gfc_symtree* overridden;
16377 : 1480 : overridden = gfc_find_typebound_proc (super_type, NULL,
16378 : : stree->name, true, NULL);
16379 : :
16380 : 1480 : if (overridden)
16381 : : {
16382 : 1210 : if (overridden->n.tb)
16383 : 1210 : stree->n.tb->overridden = overridden->n.tb;
16384 : :
16385 : 1210 : if (!gfc_check_typebound_override (stree, overridden))
16386 : 26 : goto error;
16387 : : }
16388 : : }
16389 : :
16390 : : /* See if there's a name collision with a component directly in this type. */
16391 : 20165 : for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
16392 : 11812 : if (!strcmp (comp->name, stree->name))
16393 : : {
16394 : 1 : gfc_error ("Procedure %qs at %L has the same name as a component of"
16395 : : " %qs",
16396 : : stree->name, &where, resolve_bindings_derived->name);
16397 : 1 : goto error;
16398 : : }
16399 : :
16400 : : /* Try to find a name collision with an inherited component. */
16401 : 8353 : if (super_type && gfc_find_component (super_type, stree->name, true, true,
16402 : : NULL))
16403 : : {
16404 : 1 : gfc_error ("Procedure %qs at %L has the same name as an inherited"
16405 : : " component of %qs",
16406 : : stree->name, &where, resolve_bindings_derived->name);
16407 : 1 : goto error;
16408 : : }
16409 : :
16410 : 8352 : stree->n.tb->error = 0;
16411 : 8352 : return;
16412 : :
16413 : 82 : error:
16414 : 82 : resolve_bindings_result = false;
16415 : 82 : stree->n.tb->error = 1;
16416 : : }
16417 : :
16418 : :
16419 : : static bool
16420 : 82201 : resolve_typebound_procedures (gfc_symbol* derived)
16421 : : {
16422 : 82201 : int op;
16423 : 82201 : gfc_symbol* super_type;
16424 : :
16425 : 82201 : if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
16426 : : return true;
16427 : :
16428 : 4564 : super_type = gfc_get_derived_super_type (derived);
16429 : 4564 : if (super_type)
16430 : 847 : resolve_symbol (super_type);
16431 : :
16432 : 4564 : resolve_bindings_derived = derived;
16433 : 4564 : resolve_bindings_result = true;
16434 : :
16435 : 4564 : containing_dt = derived; /* Needed for checks of PDTs. */
16436 : 4564 : if (derived->f2k_derived->tb_sym_root)
16437 : 4564 : gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
16438 : : &resolve_typebound_procedure);
16439 : :
16440 : 4564 : if (derived->f2k_derived->tb_uop_root)
16441 : 50 : gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
16442 : : &resolve_typebound_user_op);
16443 : 4564 : containing_dt = NULL;
16444 : :
16445 : 132356 : for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
16446 : : {
16447 : 127792 : gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
16448 : 127792 : if (p && !resolve_typebound_intrinsic_op (derived,
16449 : : (gfc_intrinsic_op)op, p))
16450 : 7 : resolve_bindings_result = false;
16451 : : }
16452 : :
16453 : 4564 : return resolve_bindings_result;
16454 : : }
16455 : :
16456 : :
16457 : : /* Add a derived type to the dt_list. The dt_list is used in trans-types.cc
16458 : : to give all identical derived types the same backend_decl. */
16459 : : static void
16460 : 168955 : add_dt_to_dt_list (gfc_symbol *derived)
16461 : : {
16462 : 168955 : if (!derived->dt_next)
16463 : : {
16464 : 78147 : if (gfc_derived_types)
16465 : : {
16466 : 64161 : derived->dt_next = gfc_derived_types->dt_next;
16467 : 64161 : gfc_derived_types->dt_next = derived;
16468 : : }
16469 : : else
16470 : : {
16471 : 13986 : derived->dt_next = derived;
16472 : : }
16473 : 78147 : gfc_derived_types = derived;
16474 : : }
16475 : 168955 : }
16476 : :
16477 : :
16478 : : /* Ensure that a derived-type is really not abstract, meaning that every
16479 : : inherited DEFERRED binding is overridden by a non-DEFERRED one. */
16480 : :
16481 : : static bool
16482 : 6924 : ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
16483 : : {
16484 : 6924 : if (!st)
16485 : : return true;
16486 : :
16487 : 2712 : if (!ensure_not_abstract_walker (sub, st->left))
16488 : : return false;
16489 : 2712 : if (!ensure_not_abstract_walker (sub, st->right))
16490 : : return false;
16491 : :
16492 : 2711 : if (st->n.tb && st->n.tb->deferred)
16493 : : {
16494 : 1959 : gfc_symtree* overriding;
16495 : 1959 : overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
16496 : 1959 : if (!overriding)
16497 : : return false;
16498 : 1958 : gcc_assert (overriding->n.tb);
16499 : 1958 : if (overriding->n.tb->deferred)
16500 : : {
16501 : 5 : gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
16502 : : " %qs is DEFERRED and not overridden",
16503 : : sub->name, &sub->declared_at, st->name);
16504 : 5 : return false;
16505 : : }
16506 : : }
16507 : :
16508 : : return true;
16509 : : }
16510 : :
16511 : : static bool
16512 : 1352 : ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
16513 : : {
16514 : : /* The algorithm used here is to recursively travel up the ancestry of sub
16515 : : and for each ancestor-type, check all bindings. If any of them is
16516 : : DEFERRED, look it up starting from sub and see if the found (overriding)
16517 : : binding is not DEFERRED.
16518 : : This is not the most efficient way to do this, but it should be ok and is
16519 : : clearer than something sophisticated. */
16520 : :
16521 : 1501 : gcc_assert (ancestor && !sub->attr.abstract);
16522 : :
16523 : 1501 : if (!ancestor->attr.abstract)
16524 : : return true;
16525 : :
16526 : : /* Walk bindings of this ancestor. */
16527 : 1500 : if (ancestor->f2k_derived)
16528 : : {
16529 : 1500 : bool t;
16530 : 1500 : t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
16531 : 1500 : if (!t)
16532 : : return false;
16533 : : }
16534 : :
16535 : : /* Find next ancestor type and recurse on it. */
16536 : 1494 : ancestor = gfc_get_derived_super_type (ancestor);
16537 : 1494 : if (ancestor)
16538 : : return ensure_not_abstract (sub, ancestor);
16539 : :
16540 : : return true;
16541 : : }
16542 : :
16543 : :
16544 : : /* This check for typebound defined assignments is done recursively
16545 : : since the order in which derived types are resolved is not always in
16546 : : order of the declarations. */
16547 : :
16548 : : static void
16549 : 172873 : check_defined_assignments (gfc_symbol *derived)
16550 : : {
16551 : 172873 : gfc_component *c;
16552 : :
16553 : 577864 : for (c = derived->components; c; c = c->next)
16554 : : {
16555 : 406543 : if (!gfc_bt_struct (c->ts.type)
16556 : 97596 : || c->attr.pointer
16557 : 18435 : || c->attr.proc_pointer_comp
16558 : 18435 : || c->attr.class_pointer
16559 : 18429 : || c->attr.proc_pointer)
16560 : 388525 : continue;
16561 : :
16562 : 18018 : if (c->ts.u.derived->attr.defined_assign_comp
16563 : 17801 : || (c->ts.u.derived->f2k_derived
16564 : 17232 : && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
16565 : : {
16566 : 1528 : derived->attr.defined_assign_comp = 1;
16567 : 1528 : return;
16568 : : }
16569 : :
16570 : 16490 : if (c->attr.allocatable)
16571 : 5578 : continue;
16572 : :
16573 : 10912 : check_defined_assignments (c->ts.u.derived);
16574 : 10912 : if (c->ts.u.derived->attr.defined_assign_comp)
16575 : : {
16576 : 24 : derived->attr.defined_assign_comp = 1;
16577 : 24 : return;
16578 : : }
16579 : : }
16580 : : }
16581 : :
16582 : :
16583 : : /* Resolve a single component of a derived type or structure. */
16584 : :
16585 : : static bool
16586 : 388336 : resolve_component (gfc_component *c, gfc_symbol *sym)
16587 : : {
16588 : 388336 : gfc_symbol *super_type;
16589 : 388336 : symbol_attribute *attr;
16590 : :
16591 : 388336 : if (c->attr.artificial)
16592 : : return true;
16593 : :
16594 : : /* Do not allow vtype components to be resolved in nameless namespaces
16595 : : such as block data because the procedure pointers will cause ICEs
16596 : : and vtables are not needed in these contexts. */
16597 : 264875 : if (sym->attr.vtype && sym->attr.use_assoc
16598 : 46660 : && sym->ns->proc_name == NULL)
16599 : : return true;
16600 : :
16601 : : /* F2008, C442. */
16602 : 264866 : if ((!sym->attr.is_class || c != sym->components)
16603 : 264866 : && c->attr.codimension
16604 : 177 : && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
16605 : : {
16606 : 4 : gfc_error ("Coarray component %qs at %L must be allocatable with "
16607 : : "deferred shape", c->name, &c->loc);
16608 : 4 : return false;
16609 : : }
16610 : :
16611 : : /* F2008, C443. */
16612 : 264862 : if (c->attr.codimension && c->ts.type == BT_DERIVED
16613 : 77 : && c->ts.u.derived->ts.is_iso_c)
16614 : : {
16615 : 1 : gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
16616 : : "shall not be a coarray", c->name, &c->loc);
16617 : 1 : return false;
16618 : : }
16619 : :
16620 : : /* F2008, C444. */
16621 : 264861 : if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
16622 : 22 : && (c->attr.codimension || c->attr.pointer || c->attr.dimension
16623 : 20 : || c->attr.allocatable))
16624 : : {
16625 : 3 : gfc_error ("Component %qs at %L with coarray component "
16626 : : "shall be a nonpointer, nonallocatable scalar",
16627 : : c->name, &c->loc);
16628 : 3 : return false;
16629 : : }
16630 : :
16631 : : /* F2008, C448. */
16632 : 264858 : if (c->ts.type == BT_CLASS)
16633 : : {
16634 : 6627 : if (c->attr.class_ok && CLASS_DATA (c))
16635 : : {
16636 : 6619 : attr = &(CLASS_DATA (c)->attr);
16637 : :
16638 : : /* Fix up contiguous attribute. */
16639 : 6619 : if (c->attr.contiguous)
16640 : 3 : attr->contiguous = 1;
16641 : : }
16642 : : else
16643 : : attr = NULL;
16644 : : }
16645 : : else
16646 : 258231 : attr = &c->attr;
16647 : :
16648 : 264853 : if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
16649 : : {
16650 : 5 : gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
16651 : : "is not an array pointer", c->name, &c->loc);
16652 : 5 : return false;
16653 : : }
16654 : :
16655 : : /* F2003, 15.2.1 - length has to be one. */
16656 : 39820 : if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
16657 : 264872 : && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
16658 : 19 : || !gfc_is_constant_expr (c->ts.u.cl->length)
16659 : 19 : || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
16660 : : {
16661 : 1 : gfc_error ("Component %qs of BIND(C) type at %L must have length one",
16662 : : c->name, &c->loc);
16663 : 1 : return false;
16664 : : }
16665 : :
16666 : 264852 : if (c->attr.proc_pointer && c->ts.interface)
16667 : : {
16668 : 13804 : gfc_symbol *ifc = c->ts.interface;
16669 : :
16670 : 13804 : if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
16671 : : {
16672 : 6 : c->tb->error = 1;
16673 : 6 : return false;
16674 : : }
16675 : :
16676 : 13798 : if (ifc->attr.if_source || ifc->attr.intrinsic)
16677 : : {
16678 : : /* Resolve interface and copy attributes. */
16679 : 13749 : if (ifc->formal && !ifc->formal_ns)
16680 : 2490 : resolve_symbol (ifc);
16681 : 13749 : if (ifc->attr.intrinsic)
16682 : 0 : gfc_resolve_intrinsic (ifc, &ifc->declared_at);
16683 : :
16684 : 13749 : if (ifc->result)
16685 : : {
16686 : 6971 : c->ts = ifc->result->ts;
16687 : 6971 : c->attr.allocatable = ifc->result->attr.allocatable;
16688 : 6971 : c->attr.pointer = ifc->result->attr.pointer;
16689 : 6971 : c->attr.dimension = ifc->result->attr.dimension;
16690 : 6971 : c->as = gfc_copy_array_spec (ifc->result->as);
16691 : 6971 : c->attr.class_ok = ifc->result->attr.class_ok;
16692 : : }
16693 : : else
16694 : : {
16695 : 6778 : c->ts = ifc->ts;
16696 : 6778 : c->attr.allocatable = ifc->attr.allocatable;
16697 : 6778 : c->attr.pointer = ifc->attr.pointer;
16698 : 6778 : c->attr.dimension = ifc->attr.dimension;
16699 : 6778 : c->as = gfc_copy_array_spec (ifc->as);
16700 : 6778 : c->attr.class_ok = ifc->attr.class_ok;
16701 : : }
16702 : 13749 : c->ts.interface = ifc;
16703 : 13749 : c->attr.function = ifc->attr.function;
16704 : 13749 : c->attr.subroutine = ifc->attr.subroutine;
16705 : :
16706 : 13749 : c->attr.pure = ifc->attr.pure;
16707 : 13749 : c->attr.elemental = ifc->attr.elemental;
16708 : 13749 : c->attr.recursive = ifc->attr.recursive;
16709 : 13749 : c->attr.always_explicit = ifc->attr.always_explicit;
16710 : 13749 : c->attr.ext_attr |= ifc->attr.ext_attr;
16711 : : /* Copy char length. */
16712 : 13749 : if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
16713 : : {
16714 : 221 : gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
16715 : 184 : if (cl->length && !cl->resolved
16716 : 241 : && !gfc_resolve_expr (cl->length))
16717 : : {
16718 : 0 : c->tb->error = 1;
16719 : 0 : return false;
16720 : : }
16721 : 221 : c->ts.u.cl = cl;
16722 : : }
16723 : : }
16724 : : }
16725 : 251048 : else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
16726 : : {
16727 : : /* Since PPCs are not implicitly typed, a PPC without an explicit
16728 : : interface must be a subroutine. */
16729 : 115 : gfc_add_subroutine (&c->attr, c->name, &c->loc);
16730 : : }
16731 : :
16732 : : /* Procedure pointer components: Check PASS arg. */
16733 : 264846 : if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
16734 : 717 : && !sym->attr.vtype)
16735 : : {
16736 : 94 : gfc_symbol* me_arg;
16737 : :
16738 : 94 : if (c->tb->pass_arg)
16739 : : {
16740 : 19 : gfc_formal_arglist* i;
16741 : :
16742 : : /* If an explicit passing argument name is given, walk the arg-list
16743 : : and look for it. */
16744 : :
16745 : 19 : me_arg = NULL;
16746 : 19 : c->tb->pass_arg_num = 1;
16747 : 33 : for (i = c->ts.interface->formal; i; i = i->next)
16748 : : {
16749 : 32 : if (!strcmp (i->sym->name, c->tb->pass_arg))
16750 : : {
16751 : : me_arg = i->sym;
16752 : : break;
16753 : : }
16754 : 14 : c->tb->pass_arg_num++;
16755 : : }
16756 : :
16757 : 19 : if (!me_arg)
16758 : : {
16759 : 1 : gfc_error ("Procedure pointer component %qs with PASS(%s) "
16760 : : "at %L has no argument %qs", c->name,
16761 : : c->tb->pass_arg, &c->loc, c->tb->pass_arg);
16762 : 1 : c->tb->error = 1;
16763 : 1 : return false;
16764 : : }
16765 : : }
16766 : : else
16767 : : {
16768 : : /* Otherwise, take the first one; there should in fact be at least
16769 : : one. */
16770 : 75 : c->tb->pass_arg_num = 1;
16771 : 75 : if (!c->ts.interface->formal)
16772 : : {
16773 : 3 : gfc_error ("Procedure pointer component %qs with PASS at %L "
16774 : : "must have at least one argument",
16775 : : c->name, &c->loc);
16776 : 3 : c->tb->error = 1;
16777 : 3 : return false;
16778 : : }
16779 : 72 : me_arg = c->ts.interface->formal->sym;
16780 : : }
16781 : :
16782 : : /* Now check that the argument-type matches. */
16783 : 72 : gcc_assert (me_arg);
16784 : 90 : if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
16785 : 89 : || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
16786 : 89 : || (me_arg->ts.type == BT_CLASS
16787 : 81 : && CLASS_DATA (me_arg)->ts.u.derived != sym))
16788 : : {
16789 : 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
16790 : : " the derived type %qs", me_arg->name, c->name,
16791 : : me_arg->name, &c->loc, sym->name);
16792 : 1 : c->tb->error = 1;
16793 : 1 : return false;
16794 : : }
16795 : :
16796 : : /* Check for F03:C453. */
16797 : 89 : if (CLASS_DATA (me_arg)->attr.dimension)
16798 : : {
16799 : 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
16800 : : "must be scalar", me_arg->name, c->name, me_arg->name,
16801 : : &c->loc);
16802 : 1 : c->tb->error = 1;
16803 : 1 : return false;
16804 : : }
16805 : :
16806 : 88 : if (CLASS_DATA (me_arg)->attr.class_pointer)
16807 : : {
16808 : 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
16809 : : "may not have the POINTER attribute", me_arg->name,
16810 : : c->name, me_arg->name, &c->loc);
16811 : 1 : c->tb->error = 1;
16812 : 1 : return false;
16813 : : }
16814 : :
16815 : 87 : if (CLASS_DATA (me_arg)->attr.allocatable)
16816 : : {
16817 : 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
16818 : : "may not be ALLOCATABLE", me_arg->name, c->name,
16819 : : me_arg->name, &c->loc);
16820 : 1 : c->tb->error = 1;
16821 : 1 : return false;
16822 : : }
16823 : :
16824 : 86 : if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
16825 : : {
16826 : 2 : gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
16827 : : " at %L", c->name, &c->loc);
16828 : 2 : return false;
16829 : : }
16830 : :
16831 : : }
16832 : :
16833 : : /* Check type-spec if this is not the parent-type component. */
16834 : 264836 : if (((sym->attr.is_class
16835 : 11825 : && (!sym->components->ts.u.derived->attr.extension
16836 : 2318 : || c != CLASS_DATA (sym->components)))
16837 : 254307 : || (!sym->attr.is_class
16838 : 253011 : && (!sym->attr.extension || c != sym->components)))
16839 : 257013 : && !sym->attr.vtype
16840 : 418537 : && !resolve_typespec_used (&c->ts, &c->loc, c->name))
16841 : : return false;
16842 : :
16843 : 264835 : super_type = gfc_get_derived_super_type (sym);
16844 : :
16845 : : /* If this type is an extension, set the accessibility of the parent
16846 : : component. */
16847 : 264835 : if (super_type
16848 : 24487 : && ((sym->attr.is_class
16849 : 11825 : && c == CLASS_DATA (sym->components))
16850 : 16303 : || (!sym->attr.is_class && c == sym->components))
16851 : 14711 : && strcmp (super_type->name, c->name) == 0)
16852 : 6406 : c->attr.access = super_type->attr.access;
16853 : :
16854 : : /* If this type is an extension, see if this component has the same name
16855 : : as an inherited type-bound procedure. */
16856 : 24487 : if (super_type && !sym->attr.is_class
16857 : 12662 : && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
16858 : : {
16859 : 1 : gfc_error ("Component %qs of %qs at %L has the same name as an"
16860 : : " inherited type-bound procedure",
16861 : : c->name, sym->name, &c->loc);
16862 : 1 : return false;
16863 : : }
16864 : :
16865 : 264834 : if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
16866 : 9196 : && !c->ts.deferred)
16867 : : {
16868 : 7045 : if (c->ts.u.cl->length == NULL
16869 : 7039 : || (!resolve_charlen(c->ts.u.cl))
16870 : 14083 : || !gfc_is_constant_expr (c->ts.u.cl->length))
16871 : : {
16872 : 9 : gfc_error ("Character length of component %qs needs to "
16873 : : "be a constant specification expression at %L",
16874 : : c->name,
16875 : 9 : c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
16876 : 9 : return false;
16877 : : }
16878 : :
16879 : 7036 : if (c->ts.u.cl->length && c->ts.u.cl->length->ts.type != BT_INTEGER)
16880 : : {
16881 : 2 : if (!c->ts.u.cl->length->error)
16882 : : {
16883 : 1 : gfc_error ("Character length expression of component %qs at %L "
16884 : : "must be of INTEGER type, found %s",
16885 : 1 : c->name, &c->ts.u.cl->length->where,
16886 : : gfc_basic_typename (c->ts.u.cl->length->ts.type));
16887 : 1 : c->ts.u.cl->length->error = 1;
16888 : : }
16889 : 2 : return false;
16890 : : }
16891 : : }
16892 : :
16893 : 264823 : if (c->ts.type == BT_CHARACTER && c->ts.deferred
16894 : 2187 : && !c->attr.pointer && !c->attr.allocatable)
16895 : : {
16896 : 1 : gfc_error ("Character component %qs of %qs at %L with deferred "
16897 : : "length must be a POINTER or ALLOCATABLE",
16898 : : c->name, sym->name, &c->loc);
16899 : 1 : return false;
16900 : : }
16901 : :
16902 : : /* Add the hidden deferred length field. */
16903 : 264822 : if (c->ts.type == BT_CHARACTER
16904 : 9426 : && (c->ts.deferred || c->attr.pdt_string)
16905 : 2322 : && !c->attr.function
16906 : 2286 : && !sym->attr.is_class)
16907 : : {
16908 : 2139 : char name[GFC_MAX_SYMBOL_LEN+9];
16909 : 2139 : gfc_component *strlen;
16910 : 2139 : sprintf (name, "_%s_length", c->name);
16911 : 2139 : strlen = gfc_find_component (sym, name, true, true, NULL);
16912 : 2139 : if (strlen == NULL)
16913 : : {
16914 : 447 : if (!gfc_add_component (sym, name, &strlen))
16915 : 0 : return false;
16916 : 447 : strlen->ts.type = BT_INTEGER;
16917 : 447 : strlen->ts.kind = gfc_charlen_int_kind;
16918 : 447 : strlen->attr.access = ACCESS_PRIVATE;
16919 : 447 : strlen->attr.artificial = 1;
16920 : : }
16921 : : }
16922 : :
16923 : 264822 : if (c->ts.type == BT_DERIVED
16924 : 48651 : && sym->component_access != ACCESS_PRIVATE
16925 : 47679 : && gfc_check_symbol_access (sym)
16926 : 93450 : && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
16927 : 46673 : && !c->ts.u.derived->attr.use_assoc
16928 : 24928 : && !gfc_check_symbol_access (c->ts.u.derived)
16929 : 265016 : && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
16930 : : "PRIVATE type and cannot be a component of "
16931 : : "%qs, which is PUBLIC at %L", c->name,
16932 : : sym->name, &sym->declared_at))
16933 : : return false;
16934 : :
16935 : 264821 : if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
16936 : : {
16937 : 2 : gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
16938 : : "type %s", c->name, &c->loc, sym->name);
16939 : 2 : return false;
16940 : : }
16941 : :
16942 : 264819 : if (sym->attr.sequence)
16943 : : {
16944 : 2506 : if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
16945 : : {
16946 : 0 : gfc_error ("Component %s of SEQUENCE type declared at %L does "
16947 : : "not have the SEQUENCE attribute",
16948 : : c->ts.u.derived->name, &sym->declared_at);
16949 : 0 : return false;
16950 : : }
16951 : : }
16952 : :
16953 : 264819 : if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
16954 : 0 : c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
16955 : 264819 : else if (c->ts.type == BT_CLASS && c->attr.class_ok
16956 : 6959 : && CLASS_DATA (c)->ts.u.derived->attr.generic)
16957 : 0 : CLASS_DATA (c)->ts.u.derived
16958 : 0 : = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
16959 : :
16960 : : /* If an allocatable component derived type is of the same type as
16961 : : the enclosing derived type, we need a vtable generating so that
16962 : : the __deallocate procedure is created. */
16963 : 264819 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
16964 : 55620 : && c->ts.u.derived == sym && c->attr.allocatable == 1)
16965 : 160 : gfc_find_vtab (&c->ts);
16966 : :
16967 : : /* Ensure that all the derived type components are put on the
16968 : : derived type list; even in formal namespaces, where derived type
16969 : : pointer components might not have been declared. */
16970 : 264819 : if (c->ts.type == BT_DERIVED
16971 : 48650 : && c->ts.u.derived
16972 : 48650 : && c->ts.u.derived->components
16973 : 45512 : && c->attr.pointer
16974 : 31874 : && sym != c->ts.u.derived)
16975 : 4155 : add_dt_to_dt_list (c->ts.u.derived);
16976 : :
16977 : 264819 : if (c->as && c->as->type != AS_DEFERRED
16978 : 5672 : && (c->attr.pointer || c->attr.allocatable))
16979 : : return false;
16980 : :
16981 : 264805 : if (!gfc_resolve_array_spec (c->as,
16982 : 264805 : !(c->attr.pointer || c->attr.proc_pointer
16983 : 215558 : || c->attr.allocatable)))
16984 : : return false;
16985 : :
16986 : 98322 : if (c->initializer && !sym->attr.vtype
16987 : 28715 : && !c->attr.pdt_kind && !c->attr.pdt_len
16988 : 291767 : && !gfc_check_assign_symbol (sym, c, c->initializer))
16989 : : return false;
16990 : :
16991 : : return true;
16992 : : }
16993 : :
16994 : :
16995 : : /* Be nice about the locus for a structure expression - show the locus of the
16996 : : first non-null sub-expression if we can. */
16997 : :
16998 : : static locus *
16999 : 4 : cons_where (gfc_expr *struct_expr)
17000 : : {
17001 : 4 : gfc_constructor *cons;
17002 : :
17003 : 4 : gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
17004 : :
17005 : 4 : cons = gfc_constructor_first (struct_expr->value.constructor);
17006 : 12 : for (; cons; cons = gfc_constructor_next (cons))
17007 : : {
17008 : 8 : if (cons->expr && cons->expr->expr_type != EXPR_NULL)
17009 : 4 : return &cons->expr->where;
17010 : : }
17011 : :
17012 : 0 : return &struct_expr->where;
17013 : : }
17014 : :
17015 : : /* Resolve the components of a structure type. Much less work than derived
17016 : : types. */
17017 : :
17018 : : static bool
17019 : 913 : resolve_fl_struct (gfc_symbol *sym)
17020 : : {
17021 : 913 : gfc_component *c;
17022 : 913 : gfc_expr *init = NULL;
17023 : 913 : bool success;
17024 : :
17025 : : /* Make sure UNIONs do not have overlapping initializers. */
17026 : 913 : if (sym->attr.flavor == FL_UNION)
17027 : : {
17028 : 498 : for (c = sym->components; c; c = c->next)
17029 : : {
17030 : 331 : if (init && c->initializer)
17031 : : {
17032 : 2 : gfc_error ("Conflicting initializers in union at %L and %L",
17033 : : cons_where (init), cons_where (c->initializer));
17034 : 2 : gfc_free_expr (c->initializer);
17035 : 2 : c->initializer = NULL;
17036 : : }
17037 : 291 : if (init == NULL)
17038 : 291 : init = c->initializer;
17039 : : }
17040 : : }
17041 : :
17042 : 913 : success = true;
17043 : 2830 : for (c = sym->components; c; c = c->next)
17044 : 1917 : if (!resolve_component (c, sym))
17045 : 0 : success = false;
17046 : :
17047 : 913 : if (!success)
17048 : : return false;
17049 : :
17050 : 913 : if (sym->components)
17051 : 862 : add_dt_to_dt_list (sym);
17052 : :
17053 : : return true;
17054 : : }
17055 : :
17056 : : /* Figure if the derived type is using itself directly in one of its components
17057 : : or through referencing other derived types. The information is required to
17058 : : generate the __deallocate and __final type bound procedures to ensure
17059 : : freeing larger hierarchies of derived types with allocatable objects. */
17060 : :
17061 : : static void
17062 : 131518 : resolve_cyclic_derived_type (gfc_symbol *derived)
17063 : : {
17064 : 131518 : hash_set<gfc_symbol *> seen, to_examin;
17065 : 131518 : gfc_component *c;
17066 : 131518 : seen.add (derived);
17067 : 131518 : to_examin.add (derived);
17068 : 440342 : while (!to_examin.is_empty ())
17069 : : {
17070 : 179173 : gfc_symbol *cand = *to_examin.begin ();
17071 : 179173 : to_examin.remove (cand);
17072 : 480598 : for (c = cand->components; c; c = c->next)
17073 : 303292 : if (c->ts.type == BT_DERIVED)
17074 : : {
17075 : 66382 : if (c->ts.u.derived == derived)
17076 : : {
17077 : 897 : derived->attr.recursive = 1;
17078 : 1867 : return;
17079 : : }
17080 : 65485 : else if (!seen.contains (c->ts.u.derived))
17081 : : {
17082 : 43218 : seen.add (c->ts.u.derived);
17083 : 43218 : to_examin.add (c->ts.u.derived);
17084 : : }
17085 : : }
17086 : 236910 : else if (c->ts.type == BT_CLASS)
17087 : : {
17088 : 8837 : if (!c->attr.class_ok)
17089 : 7 : continue;
17090 : 8830 : if (CLASS_DATA (c)->ts.u.derived == derived)
17091 : : {
17092 : 970 : derived->attr.recursive = 1;
17093 : 970 : return;
17094 : : }
17095 : 7860 : else if (!seen.contains (CLASS_DATA (c)->ts.u.derived))
17096 : : {
17097 : 4640 : seen.add (CLASS_DATA (c)->ts.u.derived);
17098 : 4640 : to_examin.add (CLASS_DATA (c)->ts.u.derived);
17099 : : }
17100 : : }
17101 : : }
17102 : 131518 : }
17103 : :
17104 : : /* Resolve the components of a derived type. This does not have to wait until
17105 : : resolution stage, but can be done as soon as the dt declaration has been
17106 : : parsed. */
17107 : :
17108 : : static bool
17109 : 162046 : resolve_fl_derived0 (gfc_symbol *sym)
17110 : : {
17111 : 162046 : gfc_symbol* super_type;
17112 : 162046 : gfc_component *c;
17113 : 162046 : gfc_formal_arglist *f;
17114 : 162046 : bool success;
17115 : :
17116 : 162046 : if (sym->attr.unlimited_polymorphic)
17117 : : return true;
17118 : :
17119 : 162046 : super_type = gfc_get_derived_super_type (sym);
17120 : :
17121 : : /* F2008, C432. */
17122 : 162046 : if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
17123 : : {
17124 : 2 : gfc_error ("As extending type %qs at %L has a coarray component, "
17125 : : "parent type %qs shall also have one", sym->name,
17126 : : &sym->declared_at, super_type->name);
17127 : 2 : return false;
17128 : : }
17129 : :
17130 : : /* Ensure the extended type gets resolved before we do. */
17131 : 16617 : if (super_type && !resolve_fl_derived0 (super_type))
17132 : : return false;
17133 : :
17134 : : /* An ABSTRACT type must be extensible. */
17135 : 162038 : if (sym->attr.abstract && !gfc_type_is_extensible (sym))
17136 : : {
17137 : 2 : gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
17138 : : sym->name, &sym->declared_at);
17139 : 2 : return false;
17140 : : }
17141 : :
17142 : : /* Resolving components below, may create vtabs for which the cyclic type
17143 : : information needs to be present. */
17144 : 162036 : if (!sym->attr.vtype)
17145 : 131518 : resolve_cyclic_derived_type (sym);
17146 : :
17147 : 162036 : c = (sym->attr.is_class) ? CLASS_DATA (sym->components)
17148 : : : sym->components;
17149 : :
17150 : : success = true;
17151 : 548455 : for ( ; c != NULL; c = c->next)
17152 : 386419 : if (!resolve_component (c, sym))
17153 : 85 : success = false;
17154 : :
17155 : 162036 : if (!success)
17156 : : return false;
17157 : :
17158 : : /* Now add the caf token field, where needed. */
17159 : 161961 : if (flag_coarray == GFC_FCOARRAY_LIB && !sym->attr.is_class
17160 : 770 : && !sym->attr.vtype)
17161 : : {
17162 : 1883 : for (c = sym->components; c; c = c->next)
17163 : 1266 : if (!c->attr.dimension && !c->attr.codimension
17164 : 703 : && (c->attr.allocatable || c->attr.pointer))
17165 : : {
17166 : 134 : char name[GFC_MAX_SYMBOL_LEN+9];
17167 : 134 : gfc_component *token;
17168 : 134 : sprintf (name, "_caf_%s", c->name);
17169 : 134 : token = gfc_find_component (sym, name, true, true, NULL);
17170 : 134 : if (token == NULL)
17171 : : {
17172 : 75 : if (!gfc_add_component (sym, name, &token))
17173 : 0 : return false;
17174 : 75 : token->ts.type = BT_VOID;
17175 : 75 : token->ts.kind = gfc_default_integer_kind;
17176 : 75 : token->attr.access = ACCESS_PRIVATE;
17177 : 75 : token->attr.artificial = 1;
17178 : 75 : token->attr.caf_token = 1;
17179 : : }
17180 : 134 : c->caf_token = token;
17181 : : }
17182 : : }
17183 : :
17184 : 161961 : check_defined_assignments (sym);
17185 : :
17186 : 161961 : if (!sym->attr.defined_assign_comp && super_type)
17187 : 15787 : sym->attr.defined_assign_comp
17188 : 15787 : = super_type->attr.defined_assign_comp;
17189 : :
17190 : : /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
17191 : : all DEFERRED bindings are overridden. */
17192 : 16610 : if (super_type && super_type->attr.abstract && !sym->attr.abstract
17193 : 1355 : && !sym->attr.is_class
17194 : 2880 : && !ensure_not_abstract (sym, super_type))
17195 : : return false;
17196 : :
17197 : : /* Check that there is a component for every PDT parameter. */
17198 : 161955 : if (sym->attr.pdt_template)
17199 : : {
17200 : 1139 : for (f = sym->formal; f; f = f->next)
17201 : : {
17202 : 733 : if (!f->sym)
17203 : 1 : continue;
17204 : 732 : c = gfc_find_component (sym, f->sym->name, true, true, NULL);
17205 : 732 : if (c == NULL)
17206 : : {
17207 : 5 : gfc_error ("Parameterized type %qs does not have a component "
17208 : : "corresponding to parameter %qs at %L", sym->name,
17209 : 5 : f->sym->name, &sym->declared_at);
17210 : 5 : break;
17211 : : }
17212 : : }
17213 : : }
17214 : :
17215 : : /* Add derived type to the derived type list. */
17216 : 161955 : add_dt_to_dt_list (sym);
17217 : :
17218 : 161955 : return true;
17219 : : }
17220 : :
17221 : : /* The following procedure does the full resolution of a derived type,
17222 : : including resolution of all type-bound procedures (if present). In contrast
17223 : : to 'resolve_fl_derived0' this can only be done after the module has been
17224 : : parsed completely. */
17225 : :
17226 : : static bool
17227 : 84283 : resolve_fl_derived (gfc_symbol *sym)
17228 : : {
17229 : 84283 : gfc_symbol *gen_dt = NULL;
17230 : :
17231 : 84283 : if (sym->attr.unlimited_polymorphic)
17232 : : return true;
17233 : :
17234 : 84283 : if (!sym->attr.is_class)
17235 : 72211 : gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
17236 : 53894 : if (gen_dt && gen_dt->generic && gen_dt->generic->next
17237 : 2169 : && (!gen_dt->generic->sym->attr.use_assoc
17238 : 2069 : || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
17239 : 84416 : && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
17240 : : "%qs at %L being the same name as derived "
17241 : : "type at %L", sym->name,
17242 : : gen_dt->generic->sym == sym
17243 : 11 : ? gen_dt->generic->next->sym->name
17244 : : : gen_dt->generic->sym->name,
17245 : : gen_dt->generic->sym == sym
17246 : 11 : ? &gen_dt->generic->next->sym->declared_at
17247 : : : &gen_dt->generic->sym->declared_at,
17248 : : &sym->declared_at))
17249 : : return false;
17250 : :
17251 : 84279 : if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
17252 : : {
17253 : 13 : gfc_error ("Derived type %qs at %L has not been declared",
17254 : : sym->name, &sym->declared_at);
17255 : 13 : return false;
17256 : : }
17257 : :
17258 : : /* Resolve the finalizer procedures. */
17259 : 84266 : if (!gfc_resolve_finalizers (sym, NULL))
17260 : : return false;
17261 : :
17262 : 84263 : if (sym->attr.is_class && sym->ts.u.derived == NULL)
17263 : : {
17264 : : /* Fix up incomplete CLASS symbols. */
17265 : 12072 : gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
17266 : 12072 : gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
17267 : :
17268 : : /* Nothing more to do for unlimited polymorphic entities. */
17269 : 12072 : if (data->ts.u.derived->attr.unlimited_polymorphic)
17270 : : {
17271 : 1983 : add_dt_to_dt_list (sym);
17272 : 1983 : return true;
17273 : : }
17274 : 10089 : else if (vptr->ts.u.derived == NULL)
17275 : : {
17276 : 6058 : gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
17277 : 6058 : gcc_assert (vtab);
17278 : 6058 : vptr->ts.u.derived = vtab->ts.u.derived;
17279 : 6058 : if (!resolve_fl_derived0 (vptr->ts.u.derived))
17280 : : return false;
17281 : : }
17282 : : }
17283 : :
17284 : 82280 : if (!resolve_fl_derived0 (sym))
17285 : : return false;
17286 : :
17287 : : /* Resolve the type-bound procedures. */
17288 : 82201 : if (!resolve_typebound_procedures (sym))
17289 : : return false;
17290 : :
17291 : : /* Generate module vtables subject to their accessibility and their not
17292 : : being vtables or pdt templates. If this is not done class declarations
17293 : : in external procedures wind up with their own version and so SELECT TYPE
17294 : : fails because the vptrs do not have the same address. */
17295 : 82160 : if (gfc_option.allow_std & GFC_STD_F2003 && sym->ns->proc_name
17296 : 82099 : && (sym->ns->proc_name->attr.flavor == FL_MODULE
17297 : 61746 : || (sym->attr.recursive && sym->attr.alloc_comp))
17298 : 20445 : && sym->attr.access != ACCESS_PRIVATE
17299 : 20412 : && !(sym->attr.vtype || sym->attr.pdt_template))
17300 : : {
17301 : 18496 : gfc_symbol *vtab = gfc_find_derived_vtab (sym);
17302 : 18496 : gfc_set_sym_referenced (vtab);
17303 : : }
17304 : :
17305 : : return true;
17306 : : }
17307 : :
17308 : :
17309 : : static bool
17310 : 805 : resolve_fl_namelist (gfc_symbol *sym)
17311 : : {
17312 : 805 : gfc_namelist *nl;
17313 : 805 : gfc_symbol *nlsym;
17314 : :
17315 : 2894 : for (nl = sym->namelist; nl; nl = nl->next)
17316 : : {
17317 : : /* Check again, the check in match only works if NAMELIST comes
17318 : : after the decl. */
17319 : 2094 : if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
17320 : : {
17321 : 1 : gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
17322 : : "allowed", nl->sym->name, sym->name, &sym->declared_at);
17323 : 1 : return false;
17324 : : }
17325 : :
17326 : 652 : if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
17327 : 2101 : && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
17328 : : "with assumed shape in namelist %qs at %L",
17329 : : nl->sym->name, sym->name, &sym->declared_at))
17330 : : return false;
17331 : :
17332 : 2092 : if (is_non_constant_shape_array (nl->sym)
17333 : 2142 : && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
17334 : : "with nonconstant shape in namelist %qs at %L",
17335 : 50 : nl->sym->name, sym->name, &sym->declared_at))
17336 : : return false;
17337 : :
17338 : 2091 : if (nl->sym->ts.type == BT_CHARACTER
17339 : 565 : && (nl->sym->ts.u.cl->length == NULL
17340 : 526 : || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
17341 : 2173 : && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
17342 : : "nonconstant character length in "
17343 : 82 : "namelist %qs at %L", nl->sym->name,
17344 : : sym->name, &sym->declared_at))
17345 : : return false;
17346 : :
17347 : : }
17348 : :
17349 : : /* Reject PRIVATE objects in a PUBLIC namelist. */
17350 : 800 : if (gfc_check_symbol_access (sym))
17351 : : {
17352 : 2875 : for (nl = sym->namelist; nl; nl = nl->next)
17353 : : {
17354 : 2088 : if (!nl->sym->attr.use_assoc
17355 : 3952 : && !is_sym_host_assoc (nl->sym, sym->ns)
17356 : 4042 : && !gfc_check_symbol_access (nl->sym))
17357 : : {
17358 : 2 : gfc_error ("NAMELIST object %qs was declared PRIVATE and "
17359 : : "cannot be member of PUBLIC namelist %qs at %L",
17360 : 2 : nl->sym->name, sym->name, &sym->declared_at);
17361 : 2 : return false;
17362 : : }
17363 : :
17364 : 2086 : if (nl->sym->ts.type == BT_DERIVED
17365 : 466 : && (nl->sym->ts.u.derived->attr.alloc_comp
17366 : 464 : || nl->sym->ts.u.derived->attr.pointer_comp))
17367 : : {
17368 : 5 : if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
17369 : : "namelist %qs at %L with ALLOCATABLE "
17370 : : "or POINTER components", nl->sym->name,
17371 : : sym->name, &sym->declared_at))
17372 : : return false;
17373 : : return true;
17374 : : }
17375 : :
17376 : : /* Types with private components that came here by USE-association. */
17377 : 2081 : if (nl->sym->ts.type == BT_DERIVED
17378 : 2081 : && derived_inaccessible (nl->sym->ts.u.derived))
17379 : : {
17380 : 6 : gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
17381 : : "components and cannot be member of namelist %qs at %L",
17382 : : nl->sym->name, sym->name, &sym->declared_at);
17383 : 6 : return false;
17384 : : }
17385 : :
17386 : : /* Types with private components that are defined in the same module. */
17387 : 2075 : if (nl->sym->ts.type == BT_DERIVED
17388 : 910 : && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
17389 : 2353 : && nl->sym->ts.u.derived->attr.private_comp)
17390 : : {
17391 : 0 : gfc_error ("NAMELIST object %qs has PRIVATE components and "
17392 : : "cannot be a member of PUBLIC namelist %qs at %L",
17393 : : nl->sym->name, sym->name, &sym->declared_at);
17394 : 0 : return false;
17395 : : }
17396 : : }
17397 : : }
17398 : :
17399 : :
17400 : : /* 14.1.2 A module or internal procedure represent local entities
17401 : : of the same type as a namelist member and so are not allowed. */
17402 : 2859 : for (nl = sym->namelist; nl; nl = nl->next)
17403 : : {
17404 : 2075 : if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
17405 : 1516 : continue;
17406 : :
17407 : 559 : if (nl->sym->attr.function && nl->sym == nl->sym->result)
17408 : 7 : if ((nl->sym == sym->ns->proc_name)
17409 : 1 : ||
17410 : 1 : (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
17411 : 6 : continue;
17412 : :
17413 : 553 : nlsym = NULL;
17414 : 553 : if (nl->sym->name)
17415 : 553 : gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
17416 : 553 : if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
17417 : : {
17418 : 3 : gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
17419 : : "attribute in %qs at %L", nlsym->name,
17420 : : &sym->declared_at);
17421 : 3 : return false;
17422 : : }
17423 : : }
17424 : :
17425 : : return true;
17426 : : }
17427 : :
17428 : :
17429 : : static bool
17430 : 370975 : resolve_fl_parameter (gfc_symbol *sym)
17431 : : {
17432 : : /* A parameter array's shape needs to be constant. */
17433 : 370975 : if (sym->as != NULL
17434 : 370975 : && (sym->as->type == AS_DEFERRED
17435 : 6096 : || is_non_constant_shape_array (sym)))
17436 : : {
17437 : 17 : gfc_error ("Parameter array %qs at %L cannot be automatic "
17438 : : "or of deferred shape", sym->name, &sym->declared_at);
17439 : 17 : return false;
17440 : : }
17441 : :
17442 : : /* Constraints on deferred type parameter. */
17443 : 370958 : if (!deferred_requirements (sym))
17444 : : return false;
17445 : :
17446 : : /* Make sure a parameter that has been implicitly typed still
17447 : : matches the implicit type, since PARAMETER statements can precede
17448 : : IMPLICIT statements. */
17449 : 370957 : if (sym->attr.implicit_type
17450 : 371666 : && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
17451 : 709 : sym->ns)))
17452 : : {
17453 : 0 : gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
17454 : : "later IMPLICIT type", sym->name, &sym->declared_at);
17455 : 0 : return false;
17456 : : }
17457 : :
17458 : : /* Make sure the types of derived parameters are consistent. This
17459 : : type checking is deferred until resolution because the type may
17460 : : refer to a derived type from the host. */
17461 : 370957 : if (sym->ts.type == BT_DERIVED
17462 : 370957 : && !gfc_compare_types (&sym->ts, &sym->value->ts))
17463 : : {
17464 : 0 : gfc_error ("Incompatible derived type in PARAMETER at %L",
17465 : 0 : &sym->value->where);
17466 : 0 : return false;
17467 : : }
17468 : :
17469 : : /* F03:C509,C514. */
17470 : 370957 : if (sym->ts.type == BT_CLASS)
17471 : : {
17472 : 0 : gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
17473 : : sym->name, &sym->declared_at);
17474 : 0 : return false;
17475 : : }
17476 : :
17477 : : return true;
17478 : : }
17479 : :
17480 : :
17481 : : /* Called by resolve_symbol to check PDTs. */
17482 : :
17483 : : static void
17484 : 672 : resolve_pdt (gfc_symbol* sym)
17485 : : {
17486 : 672 : gfc_symbol *derived = NULL;
17487 : 672 : gfc_actual_arglist *param;
17488 : 672 : gfc_component *c;
17489 : 672 : bool const_len_exprs = true;
17490 : 672 : bool assumed_len_exprs = false;
17491 : 672 : symbol_attribute *attr;
17492 : :
17493 : 672 : if (sym->ts.type == BT_DERIVED)
17494 : : {
17495 : 505 : derived = sym->ts.u.derived;
17496 : 505 : attr = &(sym->attr);
17497 : : }
17498 : 167 : else if (sym->ts.type == BT_CLASS)
17499 : : {
17500 : 167 : derived = CLASS_DATA (sym)->ts.u.derived;
17501 : 167 : attr = &(CLASS_DATA (sym)->attr);
17502 : : }
17503 : : else
17504 : 0 : gcc_unreachable ();
17505 : :
17506 : 672 : gcc_assert (derived->attr.pdt_type);
17507 : :
17508 : 1794 : for (param = sym->param_list; param; param = param->next)
17509 : : {
17510 : 1122 : c = gfc_find_component (derived, param->name, false, true, NULL);
17511 : 1122 : gcc_assert (c);
17512 : 1122 : if (c->attr.pdt_kind)
17513 : 419 : continue;
17514 : :
17515 : 494 : if (param->expr && !gfc_is_constant_expr (param->expr)
17516 : 765 : && c->attr.pdt_len)
17517 : : const_len_exprs = false;
17518 : 641 : else if (param->spec_type == SPEC_ASSUMED)
17519 : 272 : assumed_len_exprs = true;
17520 : :
17521 : 703 : if (param->spec_type == SPEC_DEFERRED && !attr->allocatable
17522 : 9 : && ((sym->ts.type == BT_DERIVED && !attr->pointer)
17523 : 8 : || (sym->ts.type == BT_CLASS && !attr->class_pointer)))
17524 : 2 : gfc_error ("Entity %qs at %L has a deferred LEN "
17525 : : "parameter %qs and requires either the POINTER "
17526 : : "or ALLOCATABLE attribute",
17527 : : sym->name, &sym->declared_at,
17528 : : param->name);
17529 : :
17530 : : }
17531 : :
17532 : 672 : if (!const_len_exprs
17533 : 62 : && (sym->ns->proc_name->attr.is_main_program
17534 : 62 : || sym->ns->proc_name->attr.flavor == FL_MODULE
17535 : 61 : || sym->attr.save != SAVE_NONE))
17536 : 1 : gfc_error ("The AUTOMATIC object %qs at %L must not have the "
17537 : : "SAVE attribute or be a variable declared in the "
17538 : : "main program, a module or a submodule(F08/C513)",
17539 : : sym->name, &sym->declared_at);
17540 : :
17541 : 672 : if (assumed_len_exprs && !(sym->attr.dummy
17542 : 1 : || sym->attr.select_type_temporary || sym->attr.associate_var))
17543 : 1 : gfc_error ("The object %qs at %L with ASSUMED type parameters "
17544 : : "must be a dummy or a SELECT TYPE selector(F08/4.2)",
17545 : : sym->name, &sym->declared_at);
17546 : 672 : }
17547 : :
17548 : :
17549 : : /* Resolve the symbol's array spec. */
17550 : :
17551 : : static bool
17552 : 1652900 : resolve_symbol_array_spec (gfc_symbol *sym, int check_constant)
17553 : : {
17554 : 1652900 : gfc_namespace *orig_current_ns = gfc_current_ns;
17555 : 1652900 : gfc_current_ns = gfc_get_spec_ns (sym);
17556 : :
17557 : 1652900 : bool saved_specification_expr = specification_expr;
17558 : 1652900 : specification_expr = true;
17559 : :
17560 : 1652900 : bool result = gfc_resolve_array_spec (sym->as, check_constant);
17561 : :
17562 : 1652900 : specification_expr = saved_specification_expr;
17563 : 1652900 : gfc_current_ns = orig_current_ns;
17564 : :
17565 : 1652900 : return result;
17566 : : }
17567 : :
17568 : :
17569 : : /* Do anything necessary to resolve a symbol. Right now, we just
17570 : : assume that an otherwise unknown symbol is a variable. This sort
17571 : : of thing commonly happens for symbols in module. */
17572 : :
17573 : : static void
17574 : 1777578 : resolve_symbol (gfc_symbol *sym)
17575 : : {
17576 : 1777578 : int check_constant, mp_flag;
17577 : 1777578 : gfc_symtree *symtree;
17578 : 1777578 : gfc_symtree *this_symtree;
17579 : 1777578 : gfc_namespace *ns;
17580 : 1777578 : gfc_component *c;
17581 : 1777578 : symbol_attribute class_attr;
17582 : 1777578 : gfc_array_spec *as;
17583 : :
17584 : 1777578 : if (sym->resolve_symbol_called >= 1)
17585 : 155835 : return;
17586 : 1709054 : sym->resolve_symbol_called = 1;
17587 : :
17588 : : /* No symbol will ever have union type; only components can be unions.
17589 : : Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
17590 : : (just like derived type declaration symbols have flavor FL_DERIVED). */
17591 : 1709054 : gcc_assert (sym->ts.type != BT_UNION);
17592 : :
17593 : : /* Coarrayed polymorphic objects with allocatable or pointer components are
17594 : : yet unsupported for -fcoarray=lib. */
17595 : 1709054 : if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
17596 : 86 : && sym->ts.u.derived && CLASS_DATA (sym)
17597 : 86 : && CLASS_DATA (sym)->attr.codimension
17598 : 72 : && CLASS_DATA (sym)->ts.u.derived
17599 : 71 : && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
17600 : 68 : || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
17601 : : {
17602 : 6 : gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
17603 : : "type coarrays at %L are unsupported", &sym->declared_at);
17604 : 6 : return;
17605 : : }
17606 : :
17607 : 1709048 : if (sym->attr.artificial)
17608 : : return;
17609 : :
17610 : 1624356 : if (sym->attr.unlimited_polymorphic)
17611 : : return;
17612 : :
17613 : 1622916 : if (UNLIKELY (flag_openmp && strcmp (sym->name, "omp_all_memory") == 0))
17614 : : {
17615 : 4 : gfc_error ("%<omp_all_memory%>, declared at %L, may only be used in "
17616 : : "the OpenMP DEPEND clause", &sym->declared_at);
17617 : 4 : return;
17618 : : }
17619 : :
17620 : 1622912 : if (sym->attr.flavor == FL_UNKNOWN
17621 : 1602249 : || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
17622 : 433188 : && !sym->attr.generic && !sym->attr.external
17623 : 176019 : && sym->attr.if_source == IFSRC_UNKNOWN
17624 : 78885 : && sym->ts.type == BT_UNKNOWN))
17625 : : {
17626 : : /* A symbol in a common block might not have been resolved yet properly.
17627 : : Do not try to find an interface with the same name. */
17628 : 91303 : if (sym->attr.flavor == FL_UNKNOWN && !sym->attr.intrinsic
17629 : 20659 : && !sym->attr.generic && !sym->attr.external
17630 : 20608 : && sym->attr.in_common)
17631 : 2530 : goto skip_interfaces;
17632 : :
17633 : : /* If we find that a flavorless symbol is an interface in one of the
17634 : : parent namespaces, find its symtree in this namespace, free the
17635 : : symbol and set the symtree to point to the interface symbol. */
17636 : 126686 : for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
17637 : : {
17638 : 38567 : symtree = gfc_find_symtree (ns->sym_root, sym->name);
17639 : 38567 : if (symtree && (symtree->n.sym->generic ||
17640 : 680 : (symtree->n.sym->attr.flavor == FL_PROCEDURE
17641 : 591 : && sym->ns->construct_entities)))
17642 : : {
17643 : 662 : this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
17644 : : sym->name);
17645 : 662 : if (this_symtree->n.sym == sym)
17646 : : {
17647 : 654 : symtree->n.sym->refs++;
17648 : 654 : gfc_release_symbol (sym);
17649 : 654 : this_symtree->n.sym = symtree->n.sym;
17650 : 654 : return;
17651 : : }
17652 : : }
17653 : : }
17654 : :
17655 : 88119 : skip_interfaces:
17656 : : /* Otherwise give it a flavor according to such attributes as
17657 : : it has. */
17658 : 90649 : if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
17659 : 20484 : && sym->attr.intrinsic == 0)
17660 : 20480 : sym->attr.flavor = FL_VARIABLE;
17661 : 70169 : else if (sym->attr.flavor == FL_UNKNOWN)
17662 : : {
17663 : 55 : sym->attr.flavor = FL_PROCEDURE;
17664 : 55 : if (sym->attr.dimension)
17665 : 0 : sym->attr.function = 1;
17666 : : }
17667 : : }
17668 : :
17669 : 1622258 : if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
17670 : 2304 : gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
17671 : :
17672 : 1443 : if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
17673 : 1623701 : && !resolve_procedure_interface (sym))
17674 : : return;
17675 : :
17676 : 1622247 : if (sym->attr.is_protected && !sym->attr.proc_pointer
17677 : 130 : && (sym->attr.procedure || sym->attr.external))
17678 : : {
17679 : 0 : if (sym->attr.external)
17680 : 0 : gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
17681 : : "at %L", &sym->declared_at);
17682 : : else
17683 : 0 : gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
17684 : : "at %L", &sym->declared_at);
17685 : :
17686 : 0 : return;
17687 : : }
17688 : :
17689 : : /* Ensure that variables of derived or class type having a finalizer are
17690 : : marked used even when the variable is not used anything else in the scope.
17691 : : This fixes PR118730. */
17692 : 636450 : if (sym->attr.flavor == FL_VARIABLE && !sym->attr.referenced
17693 : 435875 : && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
17694 : 1670351 : && gfc_may_be_finalized (sym->ts))
17695 : 8130 : gfc_set_sym_referenced (sym);
17696 : :
17697 : 1622247 : if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
17698 : : return;
17699 : :
17700 : 1621476 : else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
17701 : 1622239 : && !resolve_fl_struct (sym))
17702 : : return;
17703 : :
17704 : : /* Symbols that are module procedures with results (functions) have
17705 : : the types and array specification copied for type checking in
17706 : : procedures that call them, as well as for saving to a module
17707 : : file. These symbols can't stand the scrutiny that their results
17708 : : can. */
17709 : 1622107 : mp_flag = (sym->result != NULL && sym->result != sym);
17710 : :
17711 : : /* Make sure that the intrinsic is consistent with its internal
17712 : : representation. This needs to be done before assigning a default
17713 : : type to avoid spurious warnings. */
17714 : 1589037 : if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
17715 : 1653397 : && !gfc_resolve_intrinsic (sym, &sym->declared_at))
17716 : : return;
17717 : :
17718 : : /* Resolve associate names. */
17719 : 1622071 : if (sym->assoc)
17720 : 6463 : resolve_assoc_var (sym, true);
17721 : :
17722 : : /* Assign default type to symbols that need one and don't have one. */
17723 : 1622071 : if (sym->ts.type == BT_UNKNOWN)
17724 : : {
17725 : 389427 : if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
17726 : : {
17727 : 11717 : gfc_set_default_type (sym, 1, NULL);
17728 : : }
17729 : :
17730 : 252216 : if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
17731 : 59769 : && !sym->attr.function && !sym->attr.subroutine
17732 : 391040 : && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
17733 : 564 : gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
17734 : :
17735 : 389427 : if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
17736 : : {
17737 : : /* The specific case of an external procedure should emit an error
17738 : : in the case that there is no implicit type. */
17739 : 98914 : if (!mp_flag)
17740 : : {
17741 : 93114 : if (!sym->attr.mixed_entry_master)
17742 : 93008 : gfc_set_default_type (sym, sym->attr.external, NULL);
17743 : : }
17744 : : else
17745 : : {
17746 : : /* Result may be in another namespace. */
17747 : 5800 : resolve_symbol (sym->result);
17748 : :
17749 : 5800 : if (!sym->result->attr.proc_pointer)
17750 : : {
17751 : 5621 : sym->ts = sym->result->ts;
17752 : 5621 : sym->as = gfc_copy_array_spec (sym->result->as);
17753 : 5621 : sym->attr.dimension = sym->result->attr.dimension;
17754 : 5621 : sym->attr.codimension = sym->result->attr.codimension;
17755 : 5621 : sym->attr.pointer = sym->result->attr.pointer;
17756 : 5621 : sym->attr.allocatable = sym->result->attr.allocatable;
17757 : 5621 : sym->attr.contiguous = sym->result->attr.contiguous;
17758 : : }
17759 : : }
17760 : : }
17761 : : }
17762 : 1232644 : else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
17763 : 31154 : resolve_symbol_array_spec (sym->result, false);
17764 : :
17765 : : /* For a CLASS-valued function with a result variable, affirm that it has
17766 : : been resolved also when looking at the symbol 'sym'. */
17767 : 420581 : if (mp_flag && sym->ts.type == BT_CLASS && sym->result->attr.class_ok)
17768 : 713 : sym->attr.class_ok = sym->result->attr.class_ok;
17769 : :
17770 : 1622071 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived
17771 : 18733 : && CLASS_DATA (sym))
17772 : : {
17773 : 18732 : as = CLASS_DATA (sym)->as;
17774 : 18732 : class_attr = CLASS_DATA (sym)->attr;
17775 : 18732 : class_attr.pointer = class_attr.class_pointer;
17776 : : }
17777 : : else
17778 : : {
17779 : 1603339 : class_attr = sym->attr;
17780 : 1603339 : as = sym->as;
17781 : : }
17782 : :
17783 : : /* F2008, C530. */
17784 : 1622071 : if (sym->attr.contiguous
17785 : 7682 : && (!class_attr.dimension
17786 : 7679 : || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
17787 : 125 : && !class_attr.pointer)))
17788 : : {
17789 : 7 : gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
17790 : : "array pointer or an assumed-shape or assumed-rank array",
17791 : : sym->name, &sym->declared_at);
17792 : 7 : return;
17793 : : }
17794 : :
17795 : : /* Assumed size arrays and assumed shape arrays must be dummy
17796 : : arguments. Array-spec's of implied-shape should have been resolved to
17797 : : AS_EXPLICIT already. */
17798 : :
17799 : 1614510 : if (as)
17800 : : {
17801 : : /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
17802 : : specification expression. */
17803 : 143167 : if (as->type == AS_IMPLIED_SHAPE)
17804 : : {
17805 : : int i;
17806 : 1 : for (i=0; i<as->rank; i++)
17807 : : {
17808 : 1 : if (as->lower[i] != NULL && as->upper[i] == NULL)
17809 : : {
17810 : 1 : gfc_error ("Bad specification for assumed size array at %L",
17811 : : &as->lower[i]->where);
17812 : 1 : return;
17813 : : }
17814 : : }
17815 : 0 : gcc_unreachable();
17816 : : }
17817 : :
17818 : 143166 : if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
17819 : 110851 : || as->type == AS_ASSUMED_SHAPE)
17820 : 43631 : && !sym->attr.dummy && !sym->attr.select_type_temporary
17821 : 8 : && !sym->attr.associate_var)
17822 : : {
17823 : 7 : if (as->type == AS_ASSUMED_SIZE)
17824 : 7 : gfc_error ("Assumed size array at %L must be a dummy argument",
17825 : : &sym->declared_at);
17826 : : else
17827 : 0 : gfc_error ("Assumed shape array at %L must be a dummy argument",
17828 : : &sym->declared_at);
17829 : 7 : return;
17830 : : }
17831 : : /* TS 29113, C535a. */
17832 : 143159 : if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
17833 : 60 : && !sym->attr.select_type_temporary
17834 : 60 : && !(cs_base && cs_base->current
17835 : 45 : && (cs_base->current->op == EXEC_SELECT_RANK
17836 : 3 : || ((gfc_option.allow_std & GFC_STD_F202Y)
17837 : 0 : && cs_base->current->op == EXEC_BLOCK))))
17838 : : {
17839 : 18 : gfc_error ("Assumed-rank array at %L must be a dummy argument",
17840 : : &sym->declared_at);
17841 : 18 : return;
17842 : : }
17843 : 143141 : if (as->type == AS_ASSUMED_RANK
17844 : 26191 : && (sym->attr.codimension || sym->attr.value))
17845 : : {
17846 : 2 : gfc_error ("Assumed-rank array at %L may not have the VALUE or "
17847 : : "CODIMENSION attribute", &sym->declared_at);
17848 : 2 : return;
17849 : : }
17850 : : }
17851 : :
17852 : : /* Make sure symbols with known intent or optional are really dummy
17853 : : variable. Because of ENTRY statement, this has to be deferred
17854 : : until resolution time. */
17855 : :
17856 : 1622036 : if (!sym->attr.dummy
17857 : 1163570 : && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
17858 : : {
17859 : 2 : gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
17860 : 2 : return;
17861 : : }
17862 : :
17863 : 1622034 : if (sym->attr.value && !sym->attr.dummy)
17864 : : {
17865 : 2 : gfc_error ("%qs at %L cannot have the VALUE attribute because "
17866 : : "it is not a dummy argument", sym->name, &sym->declared_at);
17867 : 2 : return;
17868 : : }
17869 : :
17870 : 1622032 : if (sym->attr.value && sym->ts.type == BT_CHARACTER)
17871 : : {
17872 : 616 : gfc_charlen *cl = sym->ts.u.cl;
17873 : 616 : if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
17874 : : {
17875 : 2 : gfc_error ("Character dummy variable %qs at %L with VALUE "
17876 : : "attribute must have constant length",
17877 : : sym->name, &sym->declared_at);
17878 : 2 : return;
17879 : : }
17880 : :
17881 : 614 : if (sym->ts.is_c_interop
17882 : 381 : && mpz_cmp_si (cl->length->value.integer, 1) != 0)
17883 : : {
17884 : 1 : gfc_error ("C interoperable character dummy variable %qs at %L "
17885 : : "with VALUE attribute must have length one",
17886 : : sym->name, &sym->declared_at);
17887 : 1 : return;
17888 : : }
17889 : : }
17890 : :
17891 : 1622029 : if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
17892 : 119188 : && sym->ts.u.derived->attr.generic)
17893 : : {
17894 : 20 : sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
17895 : 20 : if (!sym->ts.u.derived)
17896 : : {
17897 : 0 : gfc_error ("The derived type %qs at %L is of type %qs, "
17898 : : "which has not been defined", sym->name,
17899 : : &sym->declared_at, sym->ts.u.derived->name);
17900 : 0 : sym->ts.type = BT_UNKNOWN;
17901 : 0 : return;
17902 : : }
17903 : : }
17904 : :
17905 : : /* Use the same constraints as TYPE(*), except for the type check
17906 : : and that only scalars and assumed-size arrays are permitted. */
17907 : 1622029 : if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
17908 : : {
17909 : 12960 : if (!sym->attr.dummy)
17910 : : {
17911 : 1 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
17912 : : "a dummy argument", sym->name, &sym->declared_at);
17913 : 1 : return;
17914 : : }
17915 : :
17916 : 12959 : if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
17917 : 8 : && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
17918 : 0 : && sym->ts.type != BT_COMPLEX)
17919 : : {
17920 : 0 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
17921 : : "of type TYPE(*) or of an numeric intrinsic type",
17922 : : sym->name, &sym->declared_at);
17923 : 0 : return;
17924 : : }
17925 : :
17926 : 12959 : if (sym->attr.allocatable || sym->attr.codimension
17927 : 12957 : || sym->attr.pointer || sym->attr.value)
17928 : : {
17929 : 4 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
17930 : : "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
17931 : : "attribute", sym->name, &sym->declared_at);
17932 : 4 : return;
17933 : : }
17934 : :
17935 : 12955 : if (sym->attr.intent == INTENT_OUT)
17936 : : {
17937 : 0 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
17938 : : "have the INTENT(OUT) attribute",
17939 : : sym->name, &sym->declared_at);
17940 : 0 : return;
17941 : : }
17942 : 12955 : if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
17943 : : {
17944 : 1 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
17945 : : "either be a scalar or an assumed-size array",
17946 : : sym->name, &sym->declared_at);
17947 : 1 : return;
17948 : : }
17949 : :
17950 : : /* Set the type to TYPE(*) and add a dimension(*) to ensure
17951 : : NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
17952 : : packing. */
17953 : 12954 : sym->ts.type = BT_ASSUMED;
17954 : 12954 : sym->as = gfc_get_array_spec ();
17955 : 12954 : sym->as->type = AS_ASSUMED_SIZE;
17956 : 12954 : sym->as->rank = 1;
17957 : 12954 : sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
17958 : : }
17959 : 1609069 : else if (sym->ts.type == BT_ASSUMED)
17960 : : {
17961 : : /* TS 29113, C407a. */
17962 : 11002 : if (!sym->attr.dummy)
17963 : : {
17964 : 7 : gfc_error ("Assumed type of variable %s at %L is only permitted "
17965 : : "for dummy variables", sym->name, &sym->declared_at);
17966 : 7 : return;
17967 : : }
17968 : 10995 : if (sym->attr.allocatable || sym->attr.codimension
17969 : 10991 : || sym->attr.pointer || sym->attr.value)
17970 : : {
17971 : 8 : gfc_error ("Assumed-type variable %s at %L may not have the "
17972 : : "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
17973 : : sym->name, &sym->declared_at);
17974 : 8 : return;
17975 : : }
17976 : 10987 : if (sym->attr.intent == INTENT_OUT)
17977 : : {
17978 : 2 : gfc_error ("Assumed-type variable %s at %L may not have the "
17979 : : "INTENT(OUT) attribute",
17980 : : sym->name, &sym->declared_at);
17981 : 2 : return;
17982 : : }
17983 : 10985 : if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
17984 : : {
17985 : 3 : gfc_error ("Assumed-type variable %s at %L shall not be an "
17986 : : "explicit-shape array", sym->name, &sym->declared_at);
17987 : 3 : return;
17988 : : }
17989 : : }
17990 : :
17991 : : /* If the symbol is marked as bind(c), that it is declared at module level
17992 : : scope and verify its type and kind. Do not do the latter for symbols
17993 : : that are implicitly typed because that is handled in
17994 : : gfc_set_default_type. Handle dummy arguments and procedure definitions
17995 : : separately. Also, anything that is use associated is not handled here
17996 : : but instead is handled in the module it is declared in. Finally, derived
17997 : : type definitions are allowed to be BIND(C) since that only implies that
17998 : : they're interoperable, and they are checked fully for interoperability
17999 : : when a variable is declared of that type. */
18000 : 1622003 : if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
18001 : 7148 : && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
18002 : 567 : && sym->attr.flavor != FL_DERIVED)
18003 : : {
18004 : 167 : bool t = true;
18005 : :
18006 : : /* First, make sure the variable is declared at the
18007 : : module-level scope (J3/04-007, Section 15.3). */
18008 : 167 : if (!(sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE)
18009 : 7 : && !sym->attr.in_common)
18010 : : {
18011 : 6 : gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
18012 : : "is neither a COMMON block nor declared at the "
18013 : : "module level scope", sym->name, &(sym->declared_at));
18014 : 6 : t = false;
18015 : : }
18016 : 161 : else if (sym->ts.type == BT_CHARACTER
18017 : 161 : && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
18018 : 1 : || !gfc_is_constant_expr (sym->ts.u.cl->length)
18019 : 1 : || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
18020 : : {
18021 : 1 : gfc_error ("BIND(C) Variable %qs at %L must have length one",
18022 : 1 : sym->name, &sym->declared_at);
18023 : 1 : t = false;
18024 : : }
18025 : 160 : else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
18026 : : {
18027 : 1 : t = verify_com_block_vars_c_interop (sym->common_head);
18028 : : }
18029 : 159 : else if (sym->attr.implicit_type == 0)
18030 : : {
18031 : : /* If type() declaration, we need to verify that the components
18032 : : of the given type are all C interoperable, etc. */
18033 : 157 : if (sym->ts.type == BT_DERIVED &&
18034 : 24 : sym->ts.u.derived->attr.is_c_interop != 1)
18035 : : {
18036 : : /* Make sure the user marked the derived type as BIND(C). If
18037 : : not, call the verify routine. This could print an error
18038 : : for the derived type more than once if multiple variables
18039 : : of that type are declared. */
18040 : 14 : if (sym->ts.u.derived->attr.is_bind_c != 1)
18041 : 1 : verify_bind_c_derived_type (sym->ts.u.derived);
18042 : 157 : t = false;
18043 : : }
18044 : :
18045 : : /* Verify the variable itself as C interoperable if it
18046 : : is BIND(C). It is not possible for this to succeed if
18047 : : the verify_bind_c_derived_type failed, so don't have to handle
18048 : : any error returned by verify_bind_c_derived_type. */
18049 : 157 : t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
18050 : 157 : sym->common_block);
18051 : : }
18052 : :
18053 : 165 : if (!t)
18054 : : {
18055 : : /* clear the is_bind_c flag to prevent reporting errors more than
18056 : : once if something failed. */
18057 : 10 : sym->attr.is_bind_c = 0;
18058 : 10 : return;
18059 : : }
18060 : : }
18061 : :
18062 : : /* If a derived type symbol has reached this point, without its
18063 : : type being declared, we have an error. Notice that most
18064 : : conditions that produce undefined derived types have already
18065 : : been dealt with. However, the likes of:
18066 : : implicit type(t) (t) ..... call foo (t) will get us here if
18067 : : the type is not declared in the scope of the implicit
18068 : : statement. Change the type to BT_UNKNOWN, both because it is so
18069 : : and to prevent an ICE. */
18070 : 1621993 : if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
18071 : 119186 : && sym->ts.u.derived->components == NULL
18072 : 1082 : && !sym->ts.u.derived->attr.zero_comp)
18073 : : {
18074 : 3 : gfc_error ("The derived type %qs at %L is of type %qs, "
18075 : : "which has not been defined", sym->name,
18076 : : &sym->declared_at, sym->ts.u.derived->name);
18077 : 3 : sym->ts.type = BT_UNKNOWN;
18078 : 3 : return;
18079 : : }
18080 : :
18081 : : /* Make sure that the derived type has been resolved and that the
18082 : : derived type is visible in the symbol's namespace, if it is a
18083 : : module function and is not PRIVATE. */
18084 : 1621990 : if (sym->ts.type == BT_DERIVED
18085 : 126051 : && sym->ts.u.derived->attr.use_assoc
18086 : 109740 : && sym->ns->proc_name
18087 : 109732 : && sym->ns->proc_name->attr.flavor == FL_MODULE
18088 : 1627786 : && !resolve_fl_derived (sym->ts.u.derived))
18089 : : return;
18090 : :
18091 : : /* Unless the derived-type declaration is use associated, Fortran 95
18092 : : does not allow public entries of private derived types.
18093 : : See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
18094 : : 161 in 95-006r3. */
18095 : 1621990 : if (sym->ts.type == BT_DERIVED
18096 : 126051 : && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
18097 : 7587 : && !sym->ts.u.derived->attr.use_assoc
18098 : 1791 : && gfc_check_symbol_access (sym)
18099 : 1612 : && !gfc_check_symbol_access (sym->ts.u.derived)
18100 : 1622003 : && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
18101 : : "derived type %qs",
18102 : 13 : (sym->attr.flavor == FL_PARAMETER)
18103 : : ? "parameter" : "variable",
18104 : : sym->name, &sym->declared_at,
18105 : 13 : sym->ts.u.derived->name))
18106 : : return;
18107 : :
18108 : : /* F2008, C1302. */
18109 : 1621983 : if (sym->ts.type == BT_DERIVED
18110 : 126044 : && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
18111 : 118 : && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
18112 : 126020 : || sym->ts.u.derived->attr.lock_comp)
18113 : 37 : && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
18114 : : {
18115 : 4 : gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
18116 : : "type LOCK_TYPE must be a coarray", sym->name,
18117 : : &sym->declared_at);
18118 : 4 : return;
18119 : : }
18120 : :
18121 : : /* TS18508, C702/C703. */
18122 : 1621979 : if (sym->ts.type == BT_DERIVED
18123 : 126040 : && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
18124 : 117 : && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
18125 : 126030 : || sym->ts.u.derived->attr.event_comp)
18126 : 10 : && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
18127 : : {
18128 : 1 : gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
18129 : : "type EVENT_TYPE must be a coarray", sym->name,
18130 : : &sym->declared_at);
18131 : 1 : return;
18132 : : }
18133 : :
18134 : : /* An assumed-size array with INTENT(OUT) shall not be of a type for which
18135 : : default initialization is defined (5.1.2.4.4). */
18136 : 1621978 : if (sym->ts.type == BT_DERIVED
18137 : 126039 : && sym->attr.dummy
18138 : 43678 : && sym->attr.intent == INTENT_OUT
18139 : 2354 : && sym->as
18140 : 381 : && sym->as->type == AS_ASSUMED_SIZE)
18141 : : {
18142 : 1 : for (c = sym->ts.u.derived->components; c; c = c->next)
18143 : : {
18144 : 1 : if (c->initializer)
18145 : : {
18146 : 1 : gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
18147 : : "ASSUMED SIZE and so cannot have a default initializer",
18148 : : sym->name, &sym->declared_at);
18149 : 1 : return;
18150 : : }
18151 : : }
18152 : : }
18153 : :
18154 : : /* F2008, C542. */
18155 : 1621977 : if (sym->ts.type == BT_DERIVED && sym->attr.dummy
18156 : 43677 : && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
18157 : : {
18158 : 0 : gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
18159 : : "INTENT(OUT)", sym->name, &sym->declared_at);
18160 : 0 : return;
18161 : : }
18162 : :
18163 : : /* TS18508. */
18164 : 1621977 : if (sym->ts.type == BT_DERIVED && sym->attr.dummy
18165 : 43677 : && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
18166 : : {
18167 : 0 : gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
18168 : : "INTENT(OUT)", sym->name, &sym->declared_at);
18169 : 0 : return;
18170 : : }
18171 : :
18172 : : /* F2008, C525. */
18173 : 1621977 : if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
18174 : 1621888 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
18175 : 18736 : && sym->ts.u.derived && CLASS_DATA (sym)
18176 : 18730 : && CLASS_DATA (sym)->attr.coarray_comp))
18177 : 1621888 : || class_attr.codimension)
18178 : 1544 : && (sym->attr.result || sym->result == sym))
18179 : : {
18180 : 8 : gfc_error ("Function result %qs at %L shall not be a coarray or have "
18181 : : "a coarray component", sym->name, &sym->declared_at);
18182 : 8 : return;
18183 : : }
18184 : :
18185 : : /* F2008, C524. */
18186 : 1621969 : if (sym->attr.codimension && sym->ts.type == BT_DERIVED
18187 : 370 : && sym->ts.u.derived->ts.is_iso_c)
18188 : : {
18189 : 3 : gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
18190 : : "shall not be a coarray", sym->name, &sym->declared_at);
18191 : 3 : return;
18192 : : }
18193 : :
18194 : : /* F2008, C525. */
18195 : 1621966 : if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
18196 : 1621880 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
18197 : 18735 : && sym->ts.u.derived && CLASS_DATA (sym)
18198 : 18729 : && CLASS_DATA (sym)->attr.coarray_comp))
18199 : 86 : && (class_attr.codimension || class_attr.pointer || class_attr.dimension
18200 : 82 : || class_attr.allocatable))
18201 : : {
18202 : 4 : gfc_error ("Variable %qs at %L with coarray component shall be a "
18203 : : "nonpointer, nonallocatable scalar, which is not a coarray",
18204 : : sym->name, &sym->declared_at);
18205 : 4 : return;
18206 : : }
18207 : :
18208 : : /* F2008, C526. The function-result case was handled above. */
18209 : 1621962 : if (class_attr.codimension
18210 : 1447 : && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
18211 : 296 : || sym->attr.select_type_temporary
18212 : 225 : || sym->attr.associate_var
18213 : 212 : || (sym->ns->save_all && !sym->attr.automatic)
18214 : 212 : || sym->ns->proc_name->attr.flavor == FL_MODULE
18215 : 212 : || sym->ns->proc_name->attr.is_main_program
18216 : 5 : || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
18217 : : {
18218 : 4 : gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
18219 : : "nor a dummy argument", sym->name, &sym->declared_at);
18220 : 4 : return;
18221 : : }
18222 : : /* F2008, C528. */
18223 : 1621958 : else if (class_attr.codimension && !sym->attr.select_type_temporary
18224 : 1372 : && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
18225 : : {
18226 : 6 : gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
18227 : : "deferred shape without allocatable", sym->name,
18228 : : &sym->declared_at);
18229 : 6 : return;
18230 : : }
18231 : 1621952 : else if (class_attr.codimension && class_attr.allocatable && as
18232 : 523 : && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
18233 : : {
18234 : 9 : gfc_error ("Allocatable coarray variable %qs at %L must have "
18235 : : "deferred shape", sym->name, &sym->declared_at);
18236 : 9 : return;
18237 : : }
18238 : :
18239 : : /* F2008, C541. */
18240 : 1621943 : if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
18241 : 1621861 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
18242 : 18730 : && sym->ts.u.derived && CLASS_DATA (sym)
18243 : 18724 : && CLASS_DATA (sym)->attr.coarray_comp))
18244 : 1621861 : || (class_attr.codimension && class_attr.allocatable))
18245 : 596 : && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
18246 : : {
18247 : 3 : gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
18248 : : "allocatable coarray or have coarray components",
18249 : : sym->name, &sym->declared_at);
18250 : 3 : return;
18251 : : }
18252 : :
18253 : 1621940 : if (class_attr.codimension && sym->attr.dummy
18254 : 425 : && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
18255 : : {
18256 : 2 : gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
18257 : : "procedure %qs", sym->name, &sym->declared_at,
18258 : : sym->ns->proc_name->name);
18259 : 2 : return;
18260 : : }
18261 : :
18262 : 1621938 : if (sym->ts.type == BT_LOGICAL
18263 : 111224 : && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
18264 : 111221 : || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
18265 : 30765 : && sym->ns->proc_name->attr.is_bind_c)))
18266 : : {
18267 : : int i;
18268 : 200 : for (i = 0; gfc_logical_kinds[i].kind; i++)
18269 : 200 : if (gfc_logical_kinds[i].kind == sym->ts.kind)
18270 : : break;
18271 : 16 : if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
18272 : 181 : && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
18273 : : "%L with non-C_Bool kind in BIND(C) procedure "
18274 : : "%qs", sym->name, &sym->declared_at,
18275 : 13 : sym->ns->proc_name->name))
18276 : : return;
18277 : 167 : else if (!gfc_logical_kinds[i].c_bool
18278 : 182 : && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
18279 : : "%qs at %L with non-C_Bool kind in "
18280 : : "BIND(C) procedure %qs", sym->name,
18281 : : &sym->declared_at,
18282 : 15 : sym->attr.function ? sym->name
18283 : 13 : : sym->ns->proc_name->name))
18284 : : return;
18285 : : }
18286 : :
18287 : 1621935 : switch (sym->attr.flavor)
18288 : : {
18289 : 636333 : case FL_VARIABLE:
18290 : 636333 : if (!resolve_fl_variable (sym, mp_flag))
18291 : : return;
18292 : : break;
18293 : :
18294 : 463942 : case FL_PROCEDURE:
18295 : 463942 : if (sym->formal && !sym->formal_ns)
18296 : : {
18297 : : /* Check that none of the arguments are a namelist. */
18298 : : gfc_formal_arglist *formal = sym->formal;
18299 : :
18300 : 103305 : for (; formal; formal = formal->next)
18301 : 70250 : if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
18302 : : {
18303 : 1 : gfc_error ("Namelist %qs cannot be an argument to "
18304 : : "subroutine or function at %L",
18305 : : formal->sym->name, &sym->declared_at);
18306 : 1 : return;
18307 : : }
18308 : : }
18309 : :
18310 : 463941 : if (!resolve_fl_procedure (sym, mp_flag))
18311 : : return;
18312 : : break;
18313 : :
18314 : 805 : case FL_NAMELIST:
18315 : 805 : if (!resolve_fl_namelist (sym))
18316 : : return;
18317 : : break;
18318 : :
18319 : 370975 : case FL_PARAMETER:
18320 : 370975 : if (!resolve_fl_parameter (sym))
18321 : : return;
18322 : : break;
18323 : :
18324 : : default:
18325 : : break;
18326 : : }
18327 : :
18328 : : /* Resolve array specifier. Check as well some constraints
18329 : : on COMMON blocks. */
18330 : :
18331 : 1621746 : check_constant = sym->attr.in_common && !sym->attr.pointer && !sym->error;
18332 : :
18333 : 1621746 : resolve_symbol_array_spec (sym, check_constant);
18334 : :
18335 : : /* Resolve formal namespaces. */
18336 : 1621746 : if (sym->formal_ns && sym->formal_ns != gfc_current_ns
18337 : 254037 : && !sym->attr.contained && !sym->attr.intrinsic)
18338 : 229975 : gfc_resolve (sym->formal_ns);
18339 : :
18340 : : /* Make sure the formal namespace is present. */
18341 : 1621746 : if (sym->formal && !sym->formal_ns)
18342 : : {
18343 : : gfc_formal_arglist *formal = sym->formal;
18344 : 33283 : while (formal && !formal->sym)
18345 : 11 : formal = formal->next;
18346 : :
18347 : 33272 : if (formal)
18348 : : {
18349 : 33261 : sym->formal_ns = formal->sym->ns;
18350 : 33261 : if (sym->formal_ns && sym->ns != formal->sym->ns)
18351 : 25017 : sym->formal_ns->refs++;
18352 : : }
18353 : : }
18354 : :
18355 : : /* Check threadprivate restrictions. */
18356 : 1621746 : if (sym->attr.threadprivate
18357 : 332 : && !(sym->attr.save || sym->attr.data || sym->attr.in_common)
18358 : 32 : && !(sym->ns->save_all && !sym->attr.automatic)
18359 : 31 : && sym->module == NULL
18360 : 16 : && (sym->ns->proc_name == NULL
18361 : 16 : || (sym->ns->proc_name->attr.flavor != FL_MODULE
18362 : 3 : && !sym->ns->proc_name->attr.is_main_program)))
18363 : 1 : gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
18364 : :
18365 : : /* Check omp declare target restrictions. */
18366 : 1621746 : if (sym->attr.omp_declare_target
18367 : 1443 : && sym->attr.flavor == FL_VARIABLE
18368 : 604 : && !sym->attr.save
18369 : 195 : && !(sym->ns->save_all && !sym->attr.automatic)
18370 : 195 : && (!sym->attr.in_common
18371 : 182 : && sym->module == NULL
18372 : 92 : && (sym->ns->proc_name == NULL
18373 : 92 : || (sym->ns->proc_name->attr.flavor != FL_MODULE
18374 : 2 : && !sym->ns->proc_name->attr.is_main_program))))
18375 : 1 : gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
18376 : : sym->name, &sym->declared_at);
18377 : :
18378 : : /* If we have come this far we can apply default-initializers, as
18379 : : described in 14.7.5, to those variables that have not already
18380 : : been assigned one. */
18381 : 1621746 : if (sym->ts.type == BT_DERIVED
18382 : 126011 : && !sym->value
18383 : 101804 : && !sym->attr.allocatable
18384 : 99021 : && !sym->attr.alloc_comp)
18385 : : {
18386 : 99021 : symbol_attribute *a = &sym->attr;
18387 : :
18388 : 99021 : if ((!a->save && !a->dummy && !a->pointer
18389 : 54149 : && !a->in_common && !a->use_assoc
18390 : 9800 : && a->referenced
18391 : 7663 : && !((a->function || a->result)
18392 : 1423 : && (!a->dimension
18393 : 124 : || sym->ts.u.derived->attr.alloc_comp
18394 : 83 : || sym->ts.u.derived->attr.pointer_comp))
18395 : 6309 : && !(a->function && sym != sym->result))
18396 : 92732 : || (a->dummy && !a->pointer && a->intent == INTENT_OUT
18397 : 1528 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY))
18398 : 7718 : apply_default_init (sym);
18399 : 91303 : else if (a->function && !a->pointer && !a->allocatable && !a->use_assoc
18400 : 2544 : && sym->result)
18401 : : /* Default initialization for function results. */
18402 : 2540 : apply_default_init (sym->result);
18403 : 88763 : else if (a->function && sym->result && a->access != ACCESS_PRIVATE
18404 : 11125 : && (sym->ts.u.derived->attr.alloc_comp
18405 : 10738 : || sym->ts.u.derived->attr.pointer_comp))
18406 : : /* Mark the result symbol to be referenced, when it has allocatable
18407 : : components. */
18408 : 419 : sym->result->attr.referenced = 1;
18409 : : }
18410 : :
18411 : 1621746 : if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
18412 : 18240 : && sym->attr.dummy && sym->attr.intent == INTENT_OUT
18413 : 1180 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY
18414 : 1105 : && !CLASS_DATA (sym)->attr.class_pointer
18415 : 1079 : && !CLASS_DATA (sym)->attr.allocatable)
18416 : 807 : apply_default_init (sym);
18417 : :
18418 : : /* If this symbol has a type-spec, check it. */
18419 : 1621746 : if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
18420 : 614546 : || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
18421 : 1317785 : if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
18422 : : return;
18423 : :
18424 : 1621743 : if (sym->param_list)
18425 : 672 : resolve_pdt (sym);
18426 : : }
18427 : :
18428 : :
18429 : : /************* Resolve DATA statements *************/
18430 : :
18431 : : static struct
18432 : : {
18433 : : gfc_data_value *vnode;
18434 : : mpz_t left;
18435 : : }
18436 : : values;
18437 : :
18438 : :
18439 : : /* Advance the values structure to point to the next value in the data list. */
18440 : :
18441 : : static bool
18442 : 10856 : next_data_value (void)
18443 : : {
18444 : 16622 : while (mpz_cmp_ui (values.left, 0) == 0)
18445 : : {
18446 : :
18447 : 8179 : if (values.vnode->next == NULL)
18448 : : return false;
18449 : :
18450 : 5766 : values.vnode = values.vnode->next;
18451 : 5766 : mpz_set (values.left, values.vnode->repeat);
18452 : : }
18453 : :
18454 : : return true;
18455 : : }
18456 : :
18457 : :
18458 : : static bool
18459 : 3537 : check_data_variable (gfc_data_variable *var, locus *where)
18460 : : {
18461 : 3537 : gfc_expr *e;
18462 : 3537 : mpz_t size;
18463 : 3537 : mpz_t offset;
18464 : 3537 : bool t;
18465 : 3537 : ar_type mark = AR_UNKNOWN;
18466 : 3537 : int i;
18467 : 3537 : mpz_t section_index[GFC_MAX_DIMENSIONS];
18468 : 3537 : int vector_offset[GFC_MAX_DIMENSIONS];
18469 : 3537 : gfc_ref *ref;
18470 : 3537 : gfc_array_ref *ar;
18471 : 3537 : gfc_symbol *sym;
18472 : 3537 : int has_pointer;
18473 : :
18474 : 3537 : if (!gfc_resolve_expr (var->expr))
18475 : : return false;
18476 : :
18477 : 3537 : ar = NULL;
18478 : 3537 : e = var->expr;
18479 : :
18480 : 3537 : if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
18481 : 0 : && e->value.function.isym->id == GFC_ISYM_CAF_GET)
18482 : 0 : e = e->value.function.actual->expr;
18483 : :
18484 : 3537 : if (e->expr_type != EXPR_VARIABLE)
18485 : : {
18486 : 0 : gfc_error ("Expecting definable entity near %L", where);
18487 : 0 : return false;
18488 : : }
18489 : :
18490 : 3537 : sym = e->symtree->n.sym;
18491 : :
18492 : 3537 : if (sym->ns->is_block_data && !sym->attr.in_common)
18493 : : {
18494 : 2 : gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
18495 : : sym->name, &sym->declared_at);
18496 : 2 : return false;
18497 : : }
18498 : :
18499 : 3535 : if (e->ref == NULL && sym->as)
18500 : : {
18501 : 1 : gfc_error ("DATA array %qs at %L must be specified in a previous"
18502 : : " declaration", sym->name, where);
18503 : 1 : return false;
18504 : : }
18505 : :
18506 : 3534 : if (gfc_is_coindexed (e))
18507 : : {
18508 : 5 : gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
18509 : : where);
18510 : 5 : return false;
18511 : : }
18512 : :
18513 : 3529 : has_pointer = sym->attr.pointer;
18514 : :
18515 : 5968 : for (ref = e->ref; ref; ref = ref->next)
18516 : : {
18517 : 2443 : if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
18518 : : has_pointer = 1;
18519 : :
18520 : 2417 : if (has_pointer)
18521 : : {
18522 : 29 : if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
18523 : : {
18524 : 1 : gfc_error ("DATA element %qs at %L is a pointer and so must "
18525 : : "be a full array", sym->name, where);
18526 : 1 : return false;
18527 : : }
18528 : :
18529 : 28 : if (values.vnode->expr->expr_type == EXPR_CONSTANT)
18530 : : {
18531 : 1 : gfc_error ("DATA object near %L has the pointer attribute "
18532 : : "and the corresponding DATA value is not a valid "
18533 : : "initial-data-target", where);
18534 : 1 : return false;
18535 : : }
18536 : : }
18537 : :
18538 : 2441 : if (ref->type == REF_COMPONENT && ref->u.c.component->attr.allocatable)
18539 : : {
18540 : 1 : gfc_error ("DATA element %qs at %L cannot have the ALLOCATABLE "
18541 : : "attribute", ref->u.c.component->name, &e->where);
18542 : 1 : return false;
18543 : : }
18544 : :
18545 : : /* Reject substrings of strings of non-constant length. */
18546 : 2440 : if (ref->type == REF_SUBSTRING
18547 : 73 : && ref->u.ss.length
18548 : 73 : && ref->u.ss.length->length
18549 : 2513 : && !gfc_is_constant_expr (ref->u.ss.length->length))
18550 : 1 : goto bad_charlen;
18551 : : }
18552 : :
18553 : : /* Reject strings with deferred length or non-constant length. */
18554 : 3525 : if (e->ts.type == BT_CHARACTER
18555 : 3525 : && (e->ts.deferred
18556 : 374 : || (e->ts.u.cl->length
18557 : 323 : && !gfc_is_constant_expr (e->ts.u.cl->length))))
18558 : 5 : goto bad_charlen;
18559 : :
18560 : 3520 : mpz_init_set_si (offset, 0);
18561 : :
18562 : 3520 : if (e->rank == 0 || has_pointer)
18563 : : {
18564 : 2675 : mpz_init_set_ui (size, 1);
18565 : 2675 : ref = NULL;
18566 : : }
18567 : : else
18568 : : {
18569 : 845 : ref = e->ref;
18570 : :
18571 : : /* Find the array section reference. */
18572 : 1028 : for (ref = e->ref; ref; ref = ref->next)
18573 : : {
18574 : 1028 : if (ref->type != REF_ARRAY)
18575 : 92 : continue;
18576 : 936 : if (ref->u.ar.type == AR_ELEMENT)
18577 : 91 : continue;
18578 : : break;
18579 : : }
18580 : 845 : gcc_assert (ref);
18581 : :
18582 : : /* Set marks according to the reference pattern. */
18583 : 845 : switch (ref->u.ar.type)
18584 : : {
18585 : : case AR_FULL:
18586 : : mark = AR_FULL;
18587 : : break;
18588 : :
18589 : 149 : case AR_SECTION:
18590 : 149 : ar = &ref->u.ar;
18591 : : /* Get the start position of array section. */
18592 : 149 : gfc_get_section_index (ar, section_index, &offset, vector_offset);
18593 : 149 : mark = AR_SECTION;
18594 : 149 : break;
18595 : :
18596 : 0 : default:
18597 : 0 : gcc_unreachable ();
18598 : : }
18599 : :
18600 : 845 : if (!gfc_array_size (e, &size))
18601 : : {
18602 : 1 : gfc_error ("Nonconstant array section at %L in DATA statement",
18603 : : where);
18604 : 1 : mpz_clear (offset);
18605 : 1 : return false;
18606 : : }
18607 : : }
18608 : :
18609 : 3519 : t = true;
18610 : :
18611 : 11900 : while (mpz_cmp_ui (size, 0) > 0)
18612 : : {
18613 : 8444 : if (!next_data_value ())
18614 : : {
18615 : 1 : gfc_error ("DATA statement at %L has more variables than values",
18616 : : where);
18617 : 1 : t = false;
18618 : 1 : break;
18619 : : }
18620 : :
18621 : 8443 : t = gfc_check_assign (var->expr, values.vnode->expr, 0);
18622 : 8443 : if (!t)
18623 : : break;
18624 : :
18625 : : /* If we have more than one element left in the repeat count,
18626 : : and we have more than one element left in the target variable,
18627 : : then create a range assignment. */
18628 : : /* FIXME: Only done for full arrays for now, since array sections
18629 : : seem tricky. */
18630 : 8424 : if (mark == AR_FULL && ref && ref->next == NULL
18631 : 5364 : && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
18632 : : {
18633 : 137 : mpz_t range;
18634 : :
18635 : 137 : if (mpz_cmp (size, values.left) >= 0)
18636 : : {
18637 : 126 : mpz_init_set (range, values.left);
18638 : 126 : mpz_sub (size, size, values.left);
18639 : 126 : mpz_set_ui (values.left, 0);
18640 : : }
18641 : : else
18642 : : {
18643 : 11 : mpz_init_set (range, size);
18644 : 11 : mpz_sub (values.left, values.left, size);
18645 : 11 : mpz_set_ui (size, 0);
18646 : : }
18647 : :
18648 : 137 : t = gfc_assign_data_value (var->expr, values.vnode->expr,
18649 : : offset, &range);
18650 : :
18651 : 137 : mpz_add (offset, offset, range);
18652 : 137 : mpz_clear (range);
18653 : :
18654 : 137 : if (!t)
18655 : : break;
18656 : 129 : }
18657 : :
18658 : : /* Assign initial value to symbol. */
18659 : : else
18660 : : {
18661 : 8287 : mpz_sub_ui (values.left, values.left, 1);
18662 : 8287 : mpz_sub_ui (size, size, 1);
18663 : :
18664 : 8287 : t = gfc_assign_data_value (var->expr, values.vnode->expr,
18665 : : offset, NULL);
18666 : 8287 : if (!t)
18667 : : break;
18668 : :
18669 : 8252 : if (mark == AR_FULL)
18670 : 5259 : mpz_add_ui (offset, offset, 1);
18671 : :
18672 : : /* Modify the array section indexes and recalculate the offset
18673 : : for next element. */
18674 : 2993 : else if (mark == AR_SECTION)
18675 : 363 : gfc_advance_section (section_index, ar, &offset, vector_offset);
18676 : : }
18677 : : }
18678 : :
18679 : 3519 : if (mark == AR_SECTION)
18680 : : {
18681 : 340 : for (i = 0; i < ar->dimen; i++)
18682 : 192 : mpz_clear (section_index[i]);
18683 : : }
18684 : :
18685 : 3519 : mpz_clear (size);
18686 : 3519 : mpz_clear (offset);
18687 : :
18688 : 3519 : return t;
18689 : :
18690 : 6 : bad_charlen:
18691 : 6 : gfc_error ("Non-constant character length at %L in DATA statement",
18692 : : &e->where);
18693 : 6 : return false;
18694 : : }
18695 : :
18696 : :
18697 : : static bool traverse_data_var (gfc_data_variable *, locus *);
18698 : :
18699 : : /* Iterate over a list of elements in a DATA statement. */
18700 : :
18701 : : static bool
18702 : 236 : traverse_data_list (gfc_data_variable *var, locus *where)
18703 : : {
18704 : 236 : mpz_t trip;
18705 : 236 : iterator_stack frame;
18706 : 236 : gfc_expr *e, *start, *end, *step;
18707 : 236 : bool retval = true;
18708 : :
18709 : 236 : mpz_init (frame.value);
18710 : 236 : mpz_init (trip);
18711 : :
18712 : 236 : start = gfc_copy_expr (var->iter.start);
18713 : 236 : end = gfc_copy_expr (var->iter.end);
18714 : 236 : step = gfc_copy_expr (var->iter.step);
18715 : :
18716 : 236 : if (!gfc_simplify_expr (start, 1)
18717 : 236 : || start->expr_type != EXPR_CONSTANT)
18718 : : {
18719 : 0 : gfc_error ("start of implied-do loop at %L could not be "
18720 : : "simplified to a constant value", &start->where);
18721 : 0 : retval = false;
18722 : 0 : goto cleanup;
18723 : : }
18724 : 236 : if (!gfc_simplify_expr (end, 1)
18725 : 236 : || end->expr_type != EXPR_CONSTANT)
18726 : : {
18727 : 0 : gfc_error ("end of implied-do loop at %L could not be "
18728 : : "simplified to a constant value", &end->where);
18729 : 0 : retval = false;
18730 : 0 : goto cleanup;
18731 : : }
18732 : 236 : if (!gfc_simplify_expr (step, 1)
18733 : 236 : || step->expr_type != EXPR_CONSTANT)
18734 : : {
18735 : 0 : gfc_error ("step of implied-do loop at %L could not be "
18736 : : "simplified to a constant value", &step->where);
18737 : 0 : retval = false;
18738 : 0 : goto cleanup;
18739 : : }
18740 : 236 : if (mpz_cmp_si (step->value.integer, 0) == 0)
18741 : : {
18742 : 1 : gfc_error ("step of implied-do loop at %L shall not be zero",
18743 : : &step->where);
18744 : 1 : retval = false;
18745 : 1 : goto cleanup;
18746 : : }
18747 : :
18748 : 235 : mpz_set (trip, end->value.integer);
18749 : 235 : mpz_sub (trip, trip, start->value.integer);
18750 : 235 : mpz_add (trip, trip, step->value.integer);
18751 : :
18752 : 235 : mpz_div (trip, trip, step->value.integer);
18753 : :
18754 : 235 : mpz_set (frame.value, start->value.integer);
18755 : :
18756 : 235 : frame.prev = iter_stack;
18757 : 235 : frame.variable = var->iter.var->symtree;
18758 : 235 : iter_stack = &frame;
18759 : :
18760 : 1124 : while (mpz_cmp_ui (trip, 0) > 0)
18761 : : {
18762 : 903 : if (!traverse_data_var (var->list, where))
18763 : : {
18764 : 14 : retval = false;
18765 : 14 : goto cleanup;
18766 : : }
18767 : :
18768 : 889 : e = gfc_copy_expr (var->expr);
18769 : 889 : if (!gfc_simplify_expr (e, 1))
18770 : : {
18771 : 0 : gfc_free_expr (e);
18772 : 0 : retval = false;
18773 : 0 : goto cleanup;
18774 : : }
18775 : :
18776 : 889 : mpz_add (frame.value, frame.value, step->value.integer);
18777 : :
18778 : 889 : mpz_sub_ui (trip, trip, 1);
18779 : : }
18780 : :
18781 : 221 : cleanup:
18782 : 236 : mpz_clear (frame.value);
18783 : 236 : mpz_clear (trip);
18784 : :
18785 : 236 : gfc_free_expr (start);
18786 : 236 : gfc_free_expr (end);
18787 : 236 : gfc_free_expr (step);
18788 : :
18789 : 236 : iter_stack = frame.prev;
18790 : 236 : return retval;
18791 : : }
18792 : :
18793 : :
18794 : : /* Type resolve variables in the variable list of a DATA statement. */
18795 : :
18796 : : static bool
18797 : 3397 : traverse_data_var (gfc_data_variable *var, locus *where)
18798 : : {
18799 : 3397 : bool t;
18800 : :
18801 : 7074 : for (; var; var = var->next)
18802 : : {
18803 : 3773 : if (var->expr == NULL)
18804 : 236 : t = traverse_data_list (var, where);
18805 : : else
18806 : 3537 : t = check_data_variable (var, where);
18807 : :
18808 : 3773 : if (!t)
18809 : : return false;
18810 : : }
18811 : :
18812 : : return true;
18813 : : }
18814 : :
18815 : :
18816 : : /* Resolve the expressions and iterators associated with a data statement.
18817 : : This is separate from the assignment checking because data lists should
18818 : : only be resolved once. */
18819 : :
18820 : : static bool
18821 : 2648 : resolve_data_variables (gfc_data_variable *d)
18822 : : {
18823 : 5667 : for (; d; d = d->next)
18824 : : {
18825 : 3024 : if (d->list == NULL)
18826 : : {
18827 : 2872 : if (!gfc_resolve_expr (d->expr))
18828 : : return false;
18829 : : }
18830 : : else
18831 : : {
18832 : 152 : if (!gfc_resolve_iterator (&d->iter, false, true))
18833 : : return false;
18834 : :
18835 : 149 : if (!resolve_data_variables (d->list))
18836 : : return false;
18837 : : }
18838 : : }
18839 : :
18840 : : return true;
18841 : : }
18842 : :
18843 : :
18844 : : /* Resolve a single DATA statement. We implement this by storing a pointer to
18845 : : the value list into static variables, and then recursively traversing the
18846 : : variables list, expanding iterators and such. */
18847 : :
18848 : : static void
18849 : 2499 : resolve_data (gfc_data *d)
18850 : : {
18851 : :
18852 : 2499 : if (!resolve_data_variables (d->var))
18853 : : return;
18854 : :
18855 : 2494 : values.vnode = d->value;
18856 : 2494 : if (d->value == NULL)
18857 : 0 : mpz_set_ui (values.left, 0);
18858 : : else
18859 : 2494 : mpz_set (values.left, d->value->repeat);
18860 : :
18861 : 2494 : if (!traverse_data_var (d->var, &d->where))
18862 : : return;
18863 : :
18864 : : /* At this point, we better not have any values left. */
18865 : :
18866 : 2412 : if (next_data_value ())
18867 : 0 : gfc_error ("DATA statement at %L has more values than variables",
18868 : : &d->where);
18869 : : }
18870 : :
18871 : :
18872 : : /* 12.6 Constraint: In a pure subprogram any variable which is in common or
18873 : : accessed by host or use association, is a dummy argument to a pure function,
18874 : : is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
18875 : : is storage associated with any such variable, shall not be used in the
18876 : : following contexts: (clients of this function). */
18877 : :
18878 : : /* Determines if a variable is not 'pure', i.e., not assignable within a pure
18879 : : procedure. Returns zero if assignment is OK, nonzero if there is a
18880 : : problem. */
18881 : : bool
18882 : 53244 : gfc_impure_variable (gfc_symbol *sym)
18883 : : {
18884 : 53244 : gfc_symbol *proc;
18885 : 53244 : gfc_namespace *ns;
18886 : :
18887 : 53244 : if (sym->attr.use_assoc || sym->attr.in_common)
18888 : : return 1;
18889 : :
18890 : : /* The namespace of a module procedure interface holds the arguments and
18891 : : symbols, and so the symbol namespace can be different to that of the
18892 : : procedure. */
18893 : 52723 : if (sym->ns != gfc_current_ns
18894 : 5637 : && gfc_current_ns->proc_name->abr_modproc_decl
18895 : 36 : && sym->ns->proc_name->attr.function
18896 : 12 : && sym->attr.result
18897 : 12 : && !strcmp (sym->ns->proc_name->name, gfc_current_ns->proc_name->name))
18898 : : return 0;
18899 : :
18900 : : /* Check if the symbol's ns is inside the pure procedure. */
18901 : 57169 : for (ns = gfc_current_ns; ns; ns = ns->parent)
18902 : : {
18903 : 56890 : if (ns == sym->ns)
18904 : : break;
18905 : 5913 : if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
18906 : : return 1;
18907 : : }
18908 : :
18909 : 51256 : proc = sym->ns->proc_name;
18910 : 51256 : if (sym->attr.dummy
18911 : 5679 : && !sym->attr.value
18912 : 5557 : && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
18913 : 5362 : || proc->attr.function))
18914 : 656 : return 1;
18915 : :
18916 : : /* TODO: Sort out what can be storage associated, if anything, and include
18917 : : it here. In principle equivalences should be scanned but it does not
18918 : : seem to be possible to storage associate an impure variable this way. */
18919 : : return 0;
18920 : : }
18921 : :
18922 : :
18923 : : /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
18924 : : current namespace is inside a pure procedure. */
18925 : :
18926 : : bool
18927 : 2260742 : gfc_pure (gfc_symbol *sym)
18928 : : {
18929 : 2260742 : symbol_attribute attr;
18930 : 2260742 : gfc_namespace *ns;
18931 : :
18932 : 2260742 : if (sym == NULL)
18933 : : {
18934 : : /* Check if the current namespace or one of its parents
18935 : : belongs to a pure procedure. */
18936 : 3102178 : for (ns = gfc_current_ns; ns; ns = ns->parent)
18937 : : {
18938 : 1832087 : sym = ns->proc_name;
18939 : 1832087 : if (sym == NULL)
18940 : : return 0;
18941 : 1830973 : attr = sym->attr;
18942 : 1830973 : if (attr.flavor == FL_PROCEDURE && attr.pure)
18943 : : return 1;
18944 : : }
18945 : : return 0;
18946 : : }
18947 : :
18948 : 982579 : attr = sym->attr;
18949 : :
18950 : 982579 : return attr.flavor == FL_PROCEDURE && attr.pure;
18951 : : }
18952 : :
18953 : :
18954 : : /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
18955 : : checks if the current namespace is implicitly pure. Note that this
18956 : : function returns false for a PURE procedure. */
18957 : :
18958 : : bool
18959 : 706955 : gfc_implicit_pure (gfc_symbol *sym)
18960 : : {
18961 : 706955 : gfc_namespace *ns;
18962 : :
18963 : 706955 : if (sym == NULL)
18964 : : {
18965 : : /* Check if the current procedure is implicit_pure. Walk up
18966 : : the procedure list until we find a procedure. */
18967 : 972160 : for (ns = gfc_current_ns; ns; ns = ns->parent)
18968 : : {
18969 : 694793 : sym = ns->proc_name;
18970 : 694793 : if (sym == NULL)
18971 : : return 0;
18972 : :
18973 : 694720 : if (sym->attr.flavor == FL_PROCEDURE)
18974 : : break;
18975 : : }
18976 : : }
18977 : :
18978 : 429512 : return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
18979 : 733165 : && !sym->attr.pure;
18980 : : }
18981 : :
18982 : :
18983 : : void
18984 : 413791 : gfc_unset_implicit_pure (gfc_symbol *sym)
18985 : : {
18986 : 413791 : gfc_namespace *ns;
18987 : :
18988 : 413791 : if (sym == NULL)
18989 : : {
18990 : : /* Check if the current procedure is implicit_pure. Walk up
18991 : : the procedure list until we find a procedure. */
18992 : 675587 : for (ns = gfc_current_ns; ns; ns = ns->parent)
18993 : : {
18994 : 417898 : sym = ns->proc_name;
18995 : 417898 : if (sym == NULL)
18996 : : return;
18997 : :
18998 : 417092 : if (sym->attr.flavor == FL_PROCEDURE)
18999 : : break;
19000 : : }
19001 : : }
19002 : :
19003 : 412985 : if (sym->attr.flavor == FL_PROCEDURE)
19004 : 147383 : sym->attr.implicit_pure = 0;
19005 : : else
19006 : 265602 : sym->attr.pure = 0;
19007 : : }
19008 : :
19009 : :
19010 : : /* Test whether the current procedure is elemental or not. */
19011 : :
19012 : : bool
19013 : 1317051 : gfc_elemental (gfc_symbol *sym)
19014 : : {
19015 : 1317051 : symbol_attribute attr;
19016 : :
19017 : 1317051 : if (sym == NULL)
19018 : 0 : sym = gfc_current_ns->proc_name;
19019 : 0 : if (sym == NULL)
19020 : : return 0;
19021 : 1317051 : attr = sym->attr;
19022 : :
19023 : 1317051 : return attr.flavor == FL_PROCEDURE && attr.elemental;
19024 : : }
19025 : :
19026 : :
19027 : : /* Warn about unused labels. */
19028 : :
19029 : : static void
19030 : 4581 : warn_unused_fortran_label (gfc_st_label *label)
19031 : : {
19032 : 4584 : if (label == NULL)
19033 : : return;
19034 : :
19035 : 4 : warn_unused_fortran_label (label->left);
19036 : :
19037 : 4 : if (label->defined == ST_LABEL_UNKNOWN)
19038 : : return;
19039 : :
19040 : 3 : switch (label->referenced)
19041 : : {
19042 : 1 : case ST_LABEL_UNKNOWN:
19043 : 1 : gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
19044 : : label->value, &label->where);
19045 : 1 : break;
19046 : :
19047 : 1 : case ST_LABEL_BAD_TARGET:
19048 : 1 : gfc_warning (OPT_Wunused_label,
19049 : : "Label %d at %L defined but cannot be used",
19050 : : label->value, &label->where);
19051 : 1 : break;
19052 : :
19053 : : default:
19054 : : break;
19055 : : }
19056 : :
19057 : 3 : warn_unused_fortran_label (label->right);
19058 : : }
19059 : :
19060 : :
19061 : : /* Returns the sequence type of a symbol or sequence. */
19062 : :
19063 : : static seq_type
19064 : 1076 : sequence_type (gfc_typespec ts)
19065 : : {
19066 : 1076 : seq_type result;
19067 : 1076 : gfc_component *c;
19068 : :
19069 : 1076 : switch (ts.type)
19070 : : {
19071 : 49 : case BT_DERIVED:
19072 : :
19073 : 49 : if (ts.u.derived->components == NULL)
19074 : : return SEQ_NONDEFAULT;
19075 : :
19076 : 49 : result = sequence_type (ts.u.derived->components->ts);
19077 : 103 : for (c = ts.u.derived->components->next; c; c = c->next)
19078 : 67 : if (sequence_type (c->ts) != result)
19079 : : return SEQ_MIXED;
19080 : :
19081 : : return result;
19082 : :
19083 : 129 : case BT_CHARACTER:
19084 : 129 : if (ts.kind != gfc_default_character_kind)
19085 : 0 : return SEQ_NONDEFAULT;
19086 : :
19087 : : return SEQ_CHARACTER;
19088 : :
19089 : 240 : case BT_INTEGER:
19090 : 240 : if (ts.kind != gfc_default_integer_kind)
19091 : 25 : return SEQ_NONDEFAULT;
19092 : :
19093 : : return SEQ_NUMERIC;
19094 : :
19095 : 559 : case BT_REAL:
19096 : 559 : if (!(ts.kind == gfc_default_real_kind
19097 : 269 : || ts.kind == gfc_default_double_kind))
19098 : 0 : return SEQ_NONDEFAULT;
19099 : :
19100 : : return SEQ_NUMERIC;
19101 : :
19102 : 81 : case BT_COMPLEX:
19103 : 81 : if (ts.kind != gfc_default_complex_kind)
19104 : 48 : return SEQ_NONDEFAULT;
19105 : :
19106 : : return SEQ_NUMERIC;
19107 : :
19108 : 17 : case BT_LOGICAL:
19109 : 17 : if (ts.kind != gfc_default_logical_kind)
19110 : 0 : return SEQ_NONDEFAULT;
19111 : :
19112 : : return SEQ_NUMERIC;
19113 : :
19114 : : default:
19115 : : return SEQ_NONDEFAULT;
19116 : : }
19117 : : }
19118 : :
19119 : :
19120 : : /* Resolve derived type EQUIVALENCE object. */
19121 : :
19122 : : static bool
19123 : 80 : resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
19124 : : {
19125 : 80 : gfc_component *c = derived->components;
19126 : :
19127 : 80 : if (!derived)
19128 : : return true;
19129 : :
19130 : : /* Shall not be an object of nonsequence derived type. */
19131 : 80 : if (!derived->attr.sequence)
19132 : : {
19133 : 0 : gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
19134 : : "attribute to be an EQUIVALENCE object", sym->name,
19135 : : &e->where);
19136 : 0 : return false;
19137 : : }
19138 : :
19139 : : /* Shall not have allocatable components. */
19140 : 80 : if (derived->attr.alloc_comp)
19141 : : {
19142 : 1 : gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
19143 : : "components to be an EQUIVALENCE object",sym->name,
19144 : : &e->where);
19145 : 1 : return false;
19146 : : }
19147 : :
19148 : 79 : if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
19149 : : {
19150 : 1 : gfc_error ("Derived type variable %qs at %L with default "
19151 : : "initialization cannot be in EQUIVALENCE with a variable "
19152 : : "in COMMON", sym->name, &e->where);
19153 : 1 : return false;
19154 : : }
19155 : :
19156 : 245 : for (; c ; c = c->next)
19157 : : {
19158 : 167 : if (gfc_bt_struct (c->ts.type)
19159 : 167 : && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
19160 : : return false;
19161 : :
19162 : : /* Shall not be an object of sequence derived type containing a pointer
19163 : : in the structure. */
19164 : 167 : if (c->attr.pointer)
19165 : : {
19166 : 0 : gfc_error ("Derived type variable %qs at %L with pointer "
19167 : : "component(s) cannot be an EQUIVALENCE object",
19168 : : sym->name, &e->where);
19169 : 0 : return false;
19170 : : }
19171 : : }
19172 : : return true;
19173 : : }
19174 : :
19175 : :
19176 : : /* Resolve equivalence object.
19177 : : An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
19178 : : an allocatable array, an object of nonsequence derived type, an object of
19179 : : sequence derived type containing a pointer at any level of component
19180 : : selection, an automatic object, a function name, an entry name, a result
19181 : : name, a named constant, a structure component, or a subobject of any of
19182 : : the preceding objects. A substring shall not have length zero. A
19183 : : derived type shall not have components with default initialization nor
19184 : : shall two objects of an equivalence group be initialized.
19185 : : Either all or none of the objects shall have an protected attribute.
19186 : : The simple constraints are done in symbol.cc(check_conflict) and the rest
19187 : : are implemented here. */
19188 : :
19189 : : static void
19190 : 1565 : resolve_equivalence (gfc_equiv *eq)
19191 : : {
19192 : 1565 : gfc_symbol *sym;
19193 : 1565 : gfc_symbol *first_sym;
19194 : 1565 : gfc_expr *e;
19195 : 1565 : gfc_ref *r;
19196 : 1565 : locus *last_where = NULL;
19197 : 1565 : seq_type eq_type, last_eq_type;
19198 : 1565 : gfc_typespec *last_ts;
19199 : 1565 : int object, cnt_protected;
19200 : 1565 : const char *msg;
19201 : :
19202 : 1565 : last_ts = &eq->expr->symtree->n.sym->ts;
19203 : :
19204 : 1565 : first_sym = eq->expr->symtree->n.sym;
19205 : :
19206 : 1565 : cnt_protected = 0;
19207 : :
19208 : 4727 : for (object = 1; eq; eq = eq->eq, object++)
19209 : : {
19210 : 3171 : e = eq->expr;
19211 : :
19212 : 3171 : e->ts = e->symtree->n.sym->ts;
19213 : : /* match_varspec might not know yet if it is seeing
19214 : : array reference or substring reference, as it doesn't
19215 : : know the types. */
19216 : 3171 : if (e->ref && e->ref->type == REF_ARRAY)
19217 : : {
19218 : 2152 : gfc_ref *ref = e->ref;
19219 : 2152 : sym = e->symtree->n.sym;
19220 : :
19221 : 2152 : if (sym->attr.dimension)
19222 : : {
19223 : 1855 : ref->u.ar.as = sym->as;
19224 : 1855 : ref = ref->next;
19225 : : }
19226 : :
19227 : : /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
19228 : 2152 : if (e->ts.type == BT_CHARACTER
19229 : 592 : && ref
19230 : 371 : && ref->type == REF_ARRAY
19231 : 371 : && ref->u.ar.dimen == 1
19232 : 371 : && ref->u.ar.dimen_type[0] == DIMEN_RANGE
19233 : 371 : && ref->u.ar.stride[0] == NULL)
19234 : : {
19235 : 370 : gfc_expr *start = ref->u.ar.start[0];
19236 : 370 : gfc_expr *end = ref->u.ar.end[0];
19237 : 370 : void *mem = NULL;
19238 : :
19239 : : /* Optimize away the (:) reference. */
19240 : 370 : if (start == NULL && end == NULL)
19241 : : {
19242 : 9 : if (e->ref == ref)
19243 : 0 : e->ref = ref->next;
19244 : : else
19245 : 9 : e->ref->next = ref->next;
19246 : : mem = ref;
19247 : : }
19248 : : else
19249 : : {
19250 : 361 : ref->type = REF_SUBSTRING;
19251 : 361 : if (start == NULL)
19252 : 9 : start = gfc_get_int_expr (gfc_charlen_int_kind,
19253 : : NULL, 1);
19254 : 361 : ref->u.ss.start = start;
19255 : 361 : if (end == NULL && e->ts.u.cl)
19256 : 27 : end = gfc_copy_expr (e->ts.u.cl->length);
19257 : 361 : ref->u.ss.end = end;
19258 : 361 : ref->u.ss.length = e->ts.u.cl;
19259 : 361 : e->ts.u.cl = NULL;
19260 : : }
19261 : 370 : ref = ref->next;
19262 : 370 : free (mem);
19263 : : }
19264 : :
19265 : : /* Any further ref is an error. */
19266 : 1930 : if (ref)
19267 : : {
19268 : 1 : gcc_assert (ref->type == REF_ARRAY);
19269 : 1 : gfc_error ("Syntax error in EQUIVALENCE statement at %L",
19270 : : &ref->u.ar.where);
19271 : 1 : continue;
19272 : : }
19273 : : }
19274 : :
19275 : 3170 : if (!gfc_resolve_expr (e))
19276 : 2 : continue;
19277 : :
19278 : 3168 : sym = e->symtree->n.sym;
19279 : :
19280 : 3168 : if (sym->attr.is_protected)
19281 : 2 : cnt_protected++;
19282 : 3168 : if (cnt_protected > 0 && cnt_protected != object)
19283 : : {
19284 : 2 : gfc_error ("Either all or none of the objects in the "
19285 : : "EQUIVALENCE set at %L shall have the "
19286 : : "PROTECTED attribute",
19287 : : &e->where);
19288 : 2 : break;
19289 : : }
19290 : :
19291 : : /* Shall not equivalence common block variables in a PURE procedure. */
19292 : 3166 : if (sym->ns->proc_name
19293 : 3150 : && sym->ns->proc_name->attr.pure
19294 : 7 : && sym->attr.in_common)
19295 : : {
19296 : : /* Need to check for symbols that may have entered the pure
19297 : : procedure via a USE statement. */
19298 : 7 : bool saw_sym = false;
19299 : 7 : if (sym->ns->use_stmts)
19300 : : {
19301 : 6 : gfc_use_rename *r;
19302 : 10 : for (r = sym->ns->use_stmts->rename; r; r = r->next)
19303 : 4 : if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
19304 : : }
19305 : : else
19306 : : saw_sym = true;
19307 : :
19308 : 6 : if (saw_sym)
19309 : 3 : gfc_error ("COMMON block member %qs at %L cannot be an "
19310 : : "EQUIVALENCE object in the pure procedure %qs",
19311 : : sym->name, &e->where, sym->ns->proc_name->name);
19312 : : break;
19313 : : }
19314 : :
19315 : : /* Shall not be a named constant. */
19316 : 3159 : if (e->expr_type == EXPR_CONSTANT)
19317 : : {
19318 : 0 : gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
19319 : : "object", sym->name, &e->where);
19320 : 0 : continue;
19321 : : }
19322 : :
19323 : 3161 : if (e->ts.type == BT_DERIVED
19324 : 3159 : && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
19325 : 2 : continue;
19326 : :
19327 : : /* Check that the types correspond correctly:
19328 : : Note 5.28:
19329 : : A numeric sequence structure may be equivalenced to another sequence
19330 : : structure, an object of default integer type, default real type, double
19331 : : precision real type, default logical type such that components of the
19332 : : structure ultimately only become associated to objects of the same
19333 : : kind. A character sequence structure may be equivalenced to an object
19334 : : of default character kind or another character sequence structure.
19335 : : Other objects may be equivalenced only to objects of the same type and
19336 : : kind parameters. */
19337 : :
19338 : : /* Identical types are unconditionally OK. */
19339 : 3157 : if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
19340 : 2677 : goto identical_types;
19341 : :
19342 : 480 : last_eq_type = sequence_type (*last_ts);
19343 : 480 : eq_type = sequence_type (sym->ts);
19344 : :
19345 : : /* Since the pair of objects is not of the same type, mixed or
19346 : : non-default sequences can be rejected. */
19347 : :
19348 : 480 : msg = G_("Sequence %s with mixed components in EQUIVALENCE "
19349 : : "statement at %L with different type objects");
19350 : 481 : if ((object ==2
19351 : 480 : && last_eq_type == SEQ_MIXED
19352 : 7 : && last_where
19353 : 7 : && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
19354 : 486 : || (eq_type == SEQ_MIXED
19355 : 6 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
19356 : 1 : continue;
19357 : :
19358 : 479 : msg = G_("Non-default type object or sequence %s in EQUIVALENCE "
19359 : : "statement at %L with objects of different type");
19360 : 483 : if ((object ==2
19361 : 479 : && last_eq_type == SEQ_NONDEFAULT
19362 : 50 : && last_where
19363 : 49 : && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
19364 : 525 : || (eq_type == SEQ_NONDEFAULT
19365 : 24 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
19366 : 4 : continue;
19367 : :
19368 : 475 : msg = G_("Non-CHARACTER object %qs in default CHARACTER "
19369 : : "EQUIVALENCE statement at %L");
19370 : 479 : if (last_eq_type == SEQ_CHARACTER
19371 : 475 : && eq_type != SEQ_CHARACTER
19372 : 475 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
19373 : 4 : continue;
19374 : :
19375 : 471 : msg = G_("Non-NUMERIC object %qs in default NUMERIC "
19376 : : "EQUIVALENCE statement at %L");
19377 : 473 : if (last_eq_type == SEQ_NUMERIC
19378 : 471 : && eq_type != SEQ_NUMERIC
19379 : 471 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
19380 : 2 : continue;
19381 : :
19382 : 3146 : identical_types:
19383 : :
19384 : 3146 : last_ts =&sym->ts;
19385 : 3146 : last_where = &e->where;
19386 : :
19387 : 3146 : if (!e->ref)
19388 : 1003 : continue;
19389 : :
19390 : : /* Shall not be an automatic array. */
19391 : 2143 : if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
19392 : : {
19393 : 3 : gfc_error ("Array %qs at %L with non-constant bounds cannot be "
19394 : : "an EQUIVALENCE object", sym->name, &e->where);
19395 : 3 : continue;
19396 : : }
19397 : :
19398 : 2140 : r = e->ref;
19399 : 4326 : while (r)
19400 : : {
19401 : : /* Shall not be a structure component. */
19402 : 2187 : if (r->type == REF_COMPONENT)
19403 : : {
19404 : 0 : gfc_error ("Structure component %qs at %L cannot be an "
19405 : : "EQUIVALENCE object",
19406 : 0 : r->u.c.component->name, &e->where);
19407 : 0 : break;
19408 : : }
19409 : :
19410 : : /* A substring shall not have length zero. */
19411 : 2187 : if (r->type == REF_SUBSTRING)
19412 : : {
19413 : 341 : if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
19414 : : {
19415 : 1 : gfc_error ("Substring at %L has length zero",
19416 : : &r->u.ss.start->where);
19417 : 1 : break;
19418 : : }
19419 : : }
19420 : 2186 : r = r->next;
19421 : : }
19422 : : }
19423 : 1565 : }
19424 : :
19425 : :
19426 : : /* Function called by resolve_fntype to flag other symbols used in the
19427 : : length type parameter specification of function results. */
19428 : :
19429 : : static bool
19430 : 4088 : flag_fn_result_spec (gfc_expr *expr,
19431 : : gfc_symbol *sym,
19432 : : int *f ATTRIBUTE_UNUSED)
19433 : : {
19434 : 4088 : gfc_namespace *ns;
19435 : 4088 : gfc_symbol *s;
19436 : :
19437 : 4088 : if (expr->expr_type == EXPR_VARIABLE)
19438 : : {
19439 : 1378 : s = expr->symtree->n.sym;
19440 : 2153 : for (ns = s->ns; ns; ns = ns->parent)
19441 : 2153 : if (!ns->parent)
19442 : : break;
19443 : :
19444 : 1378 : if (sym == s)
19445 : : {
19446 : 1 : gfc_error ("Self reference in character length expression "
19447 : : "for %qs at %L", sym->name, &expr->where);
19448 : 1 : return true;
19449 : : }
19450 : :
19451 : 1377 : if (!s->fn_result_spec
19452 : 1377 : && s->attr.flavor == FL_PARAMETER)
19453 : : {
19454 : : /* Function contained in a module.... */
19455 : 63 : if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
19456 : : {
19457 : 32 : gfc_symtree *st;
19458 : 32 : s->fn_result_spec = 1;
19459 : : /* Make sure that this symbol is translated as a module
19460 : : variable. */
19461 : 32 : st = gfc_get_unique_symtree (ns);
19462 : 32 : st->n.sym = s;
19463 : 32 : s->refs++;
19464 : 32 : }
19465 : : /* ... which is use associated and called. */
19466 : 31 : else if (s->attr.use_assoc || s->attr.used_in_submodule
19467 : 0 : ||
19468 : : /* External function matched with an interface. */
19469 : 0 : (s->ns->proc_name
19470 : 0 : && ((s->ns == ns
19471 : 0 : && s->ns->proc_name->attr.if_source == IFSRC_DECL)
19472 : 0 : || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
19473 : 0 : && s->ns->proc_name->attr.function))
19474 : 31 : s->fn_result_spec = 1;
19475 : : }
19476 : : }
19477 : : return false;
19478 : : }
19479 : :
19480 : :
19481 : : /* Resolve function and ENTRY types, issue diagnostics if needed. */
19482 : :
19483 : : static void
19484 : 335260 : resolve_fntype (gfc_namespace *ns)
19485 : : {
19486 : 335260 : gfc_entry_list *el;
19487 : 335260 : gfc_symbol *sym;
19488 : :
19489 : 335260 : if (ns->proc_name == NULL || !ns->proc_name->attr.function)
19490 : : return;
19491 : :
19492 : : /* If there are any entries, ns->proc_name is the entry master
19493 : : synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
19494 : 175374 : if (ns->entries)
19495 : 564 : sym = ns->entries->sym;
19496 : : else
19497 : : sym = ns->proc_name;
19498 : 175374 : if (sym->result == sym
19499 : 142830 : && sym->ts.type == BT_UNKNOWN
19500 : 6 : && !gfc_set_default_type (sym, 0, NULL)
19501 : 175378 : && !sym->attr.untyped)
19502 : : {
19503 : 3 : gfc_error ("Function %qs at %L has no IMPLICIT type",
19504 : : sym->name, &sym->declared_at);
19505 : 3 : sym->attr.untyped = 1;
19506 : : }
19507 : :
19508 : 12994 : if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
19509 : 1624 : && !sym->attr.contained
19510 : 280 : && !gfc_check_symbol_access (sym->ts.u.derived)
19511 : 175374 : && gfc_check_symbol_access (sym))
19512 : : {
19513 : 0 : gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
19514 : : "%L of PRIVATE type %qs", sym->name,
19515 : 0 : &sym->declared_at, sym->ts.u.derived->name);
19516 : : }
19517 : :
19518 : 175374 : if (ns->entries)
19519 : 1189 : for (el = ns->entries->next; el; el = el->next)
19520 : : {
19521 : 625 : if (el->sym->result == el->sym
19522 : 413 : && el->sym->ts.type == BT_UNKNOWN
19523 : 2 : && !gfc_set_default_type (el->sym, 0, NULL)
19524 : 627 : && !el->sym->attr.untyped)
19525 : : {
19526 : 2 : gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
19527 : : el->sym->name, &el->sym->declared_at);
19528 : 2 : el->sym->attr.untyped = 1;
19529 : : }
19530 : : }
19531 : :
19532 : 175374 : if (sym->ts.type == BT_CHARACTER
19533 : 6643 : && sym->ts.u.cl->length
19534 : 1740 : && sym->ts.u.cl->length->ts.type == BT_INTEGER)
19535 : 1735 : gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
19536 : : }
19537 : :
19538 : :
19539 : : /* 12.3.2.1.1 Defined operators. */
19540 : :
19541 : : static bool
19542 : 447 : check_uop_procedure (gfc_symbol *sym, locus where)
19543 : : {
19544 : 447 : gfc_formal_arglist *formal;
19545 : :
19546 : 447 : if (!sym->attr.function)
19547 : : {
19548 : 4 : gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
19549 : : sym->name, &where);
19550 : 4 : return false;
19551 : : }
19552 : :
19553 : 443 : if (sym->ts.type == BT_CHARACTER
19554 : 15 : && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
19555 : 2 : && !(sym->result && ((sym->result->ts.u.cl
19556 : 2 : && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
19557 : : {
19558 : 2 : gfc_error ("User operator procedure %qs at %L cannot be assumed "
19559 : : "character length", sym->name, &where);
19560 : 2 : return false;
19561 : : }
19562 : :
19563 : 441 : formal = gfc_sym_get_dummy_args (sym);
19564 : 441 : if (!formal || !formal->sym)
19565 : : {
19566 : 1 : gfc_error ("User operator procedure %qs at %L must have at least "
19567 : : "one argument", sym->name, &where);
19568 : 1 : return false;
19569 : : }
19570 : :
19571 : 440 : if (formal->sym->attr.intent != INTENT_IN)
19572 : : {
19573 : 0 : gfc_error ("First argument of operator interface at %L must be "
19574 : : "INTENT(IN)", &where);
19575 : 0 : return false;
19576 : : }
19577 : :
19578 : 440 : if (formal->sym->attr.optional)
19579 : : {
19580 : 0 : gfc_error ("First argument of operator interface at %L cannot be "
19581 : : "optional", &where);
19582 : 0 : return false;
19583 : : }
19584 : :
19585 : 440 : formal = formal->next;
19586 : 440 : if (!formal || !formal->sym)
19587 : : return true;
19588 : :
19589 : 295 : if (formal->sym->attr.intent != INTENT_IN)
19590 : : {
19591 : 0 : gfc_error ("Second argument of operator interface at %L must be "
19592 : : "INTENT(IN)", &where);
19593 : 0 : return false;
19594 : : }
19595 : :
19596 : 295 : if (formal->sym->attr.optional)
19597 : : {
19598 : 1 : gfc_error ("Second argument of operator interface at %L cannot be "
19599 : : "optional", &where);
19600 : 1 : return false;
19601 : : }
19602 : :
19603 : 294 : if (formal->next)
19604 : : {
19605 : 2 : gfc_error ("Operator interface at %L must have, at most, two "
19606 : : "arguments", &where);
19607 : 2 : return false;
19608 : : }
19609 : :
19610 : : return true;
19611 : : }
19612 : :
19613 : : static void
19614 : 336016 : gfc_resolve_uops (gfc_symtree *symtree)
19615 : : {
19616 : 336016 : gfc_interface *itr;
19617 : :
19618 : 336016 : if (symtree == NULL)
19619 : : return;
19620 : :
19621 : 378 : gfc_resolve_uops (symtree->left);
19622 : 378 : gfc_resolve_uops (symtree->right);
19623 : :
19624 : 771 : for (itr = symtree->n.uop->op; itr; itr = itr->next)
19625 : 393 : check_uop_procedure (itr->sym, itr->sym->declared_at);
19626 : : }
19627 : :
19628 : :
19629 : : /* Examine all of the expressions associated with a program unit,
19630 : : assign types to all intermediate expressions, make sure that all
19631 : : assignments are to compatible types and figure out which names
19632 : : refer to which functions or subroutines. It doesn't check code
19633 : : block, which is handled by gfc_resolve_code. */
19634 : :
19635 : : static void
19636 : 337726 : resolve_types (gfc_namespace *ns)
19637 : : {
19638 : 337726 : gfc_namespace *n;
19639 : 337726 : gfc_charlen *cl;
19640 : 337726 : gfc_data *d;
19641 : 337726 : gfc_equiv *eq;
19642 : 337726 : gfc_namespace* old_ns = gfc_current_ns;
19643 : 337726 : bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
19644 : :
19645 : 337726 : if (ns->types_resolved)
19646 : : return;
19647 : :
19648 : : /* Check that all IMPLICIT types are ok. */
19649 : 335261 : if (!ns->seen_implicit_none)
19650 : : {
19651 : : unsigned letter;
19652 : 8439121 : for (letter = 0; letter != GFC_LETTERS; ++letter)
19653 : 8126561 : if (ns->set_flag[letter]
19654 : 8126561 : && !resolve_typespec_used (&ns->default_type[letter],
19655 : : &ns->implicit_loc[letter], NULL))
19656 : : return;
19657 : : }
19658 : :
19659 : 335260 : gfc_current_ns = ns;
19660 : :
19661 : 335260 : resolve_entries (ns);
19662 : :
19663 : 335260 : resolve_common_vars (&ns->blank_common, false);
19664 : 335260 : resolve_common_blocks (ns->common_root);
19665 : :
19666 : 335260 : resolve_contained_functions (ns);
19667 : :
19668 : 335260 : if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
19669 : 287006 : && ns->proc_name->attr.if_source == IFSRC_IFBODY)
19670 : 187881 : gfc_resolve_formal_arglist (ns->proc_name);
19671 : :
19672 : 335260 : gfc_traverse_ns (ns, resolve_bind_c_derived_types);
19673 : :
19674 : 427361 : for (cl = ns->cl_list; cl; cl = cl->next)
19675 : 92101 : resolve_charlen (cl);
19676 : :
19677 : 335260 : gfc_traverse_ns (ns, resolve_symbol);
19678 : :
19679 : 335260 : resolve_fntype (ns);
19680 : :
19681 : 381162 : for (n = ns->contained; n; n = n->sibling)
19682 : : {
19683 : : /* Exclude final wrappers with the test for the artificial attribute. */
19684 : 45902 : if (gfc_pure (ns->proc_name)
19685 : 5 : && !gfc_pure (n->proc_name)
19686 : 45902 : && !n->proc_name->attr.artificial)
19687 : 0 : gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
19688 : : "also be PURE", n->proc_name->name,
19689 : : &n->proc_name->declared_at);
19690 : :
19691 : 45902 : resolve_types (n);
19692 : : }
19693 : :
19694 : 335260 : forall_flag = 0;
19695 : 335260 : gfc_do_concurrent_flag = 0;
19696 : 335260 : gfc_check_interfaces (ns);
19697 : :
19698 : 335260 : gfc_traverse_ns (ns, resolve_values);
19699 : :
19700 : 335260 : if (ns->save_all || (!flag_automatic && !recursive))
19701 : 296 : gfc_save_all (ns);
19702 : :
19703 : 335260 : iter_stack = NULL;
19704 : 337759 : for (d = ns->data; d; d = d->next)
19705 : 2499 : resolve_data (d);
19706 : :
19707 : 335260 : iter_stack = NULL;
19708 : 335260 : gfc_traverse_ns (ns, gfc_formalize_init_value);
19709 : :
19710 : 335260 : gfc_traverse_ns (ns, gfc_verify_binding_labels);
19711 : :
19712 : 336825 : for (eq = ns->equiv; eq; eq = eq->next)
19713 : 1565 : resolve_equivalence (eq);
19714 : :
19715 : : /* Warn about unused labels. */
19716 : 335260 : if (warn_unused_label)
19717 : 4577 : warn_unused_fortran_label (ns->st_labels);
19718 : :
19719 : 335260 : gfc_resolve_uops (ns->uop_root);
19720 : :
19721 : 335260 : gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
19722 : :
19723 : 335260 : gfc_resolve_omp_declare (ns);
19724 : :
19725 : 335260 : gfc_resolve_omp_udrs (ns->omp_udr_root);
19726 : :
19727 : 335260 : ns->types_resolved = 1;
19728 : :
19729 : 335260 : gfc_current_ns = old_ns;
19730 : : }
19731 : :
19732 : :
19733 : : /* Call gfc_resolve_code recursively. */
19734 : :
19735 : : static void
19736 : 337781 : resolve_codes (gfc_namespace *ns)
19737 : : {
19738 : 337781 : gfc_namespace *n;
19739 : 337781 : bitmap_obstack old_obstack;
19740 : :
19741 : 337781 : if (ns->resolved == 1)
19742 : 12987 : return;
19743 : :
19744 : 370751 : for (n = ns->contained; n; n = n->sibling)
19745 : 45957 : resolve_codes (n);
19746 : :
19747 : 324794 : gfc_current_ns = ns;
19748 : :
19749 : : /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
19750 : 324794 : if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
19751 : 313192 : cs_base = NULL;
19752 : :
19753 : : /* Set to an out of range value. */
19754 : 324794 : current_entry_id = -1;
19755 : :
19756 : 324794 : old_obstack = labels_obstack;
19757 : 324794 : bitmap_obstack_initialize (&labels_obstack);
19758 : :
19759 : 324794 : gfc_resolve_oacc_declare (ns);
19760 : 324794 : gfc_resolve_oacc_routines (ns);
19761 : 324794 : gfc_resolve_omp_local_vars (ns);
19762 : 324794 : if (ns->omp_allocate)
19763 : 55 : gfc_resolve_omp_allocate (ns, ns->omp_allocate);
19764 : 324794 : gfc_resolve_code (ns->code, ns);
19765 : :
19766 : 324793 : bitmap_obstack_release (&labels_obstack);
19767 : 324793 : labels_obstack = old_obstack;
19768 : : }
19769 : :
19770 : :
19771 : : /* This function is called after a complete program unit has been compiled.
19772 : : Its purpose is to examine all of the expressions associated with a program
19773 : : unit, assign types to all intermediate expressions, make sure that all
19774 : : assignments are to compatible types and figure out which names refer to
19775 : : which functions or subroutines. */
19776 : :
19777 : : void
19778 : 296357 : gfc_resolve (gfc_namespace *ns)
19779 : : {
19780 : 296357 : gfc_namespace *old_ns;
19781 : 296357 : code_stack *old_cs_base;
19782 : 296357 : struct gfc_omp_saved_state old_omp_state;
19783 : :
19784 : 296357 : if (ns->resolved)
19785 : 4533 : return;
19786 : :
19787 : 291824 : ns->resolved = -1;
19788 : 291824 : old_ns = gfc_current_ns;
19789 : 291824 : old_cs_base = cs_base;
19790 : :
19791 : : /* As gfc_resolve can be called during resolution of an OpenMP construct
19792 : : body, we should clear any state associated to it, so that say NS's
19793 : : DO loops are not interpreted as OpenMP loops. */
19794 : 291824 : if (!ns->construct_entities)
19795 : 280222 : gfc_omp_save_and_clear_state (&old_omp_state);
19796 : :
19797 : 291824 : resolve_types (ns);
19798 : 291824 : component_assignment_level = 0;
19799 : 291824 : resolve_codes (ns);
19800 : :
19801 : 291823 : if (ns->omp_assumes)
19802 : 13 : gfc_resolve_omp_assumptions (ns->omp_assumes);
19803 : :
19804 : 291823 : gfc_current_ns = old_ns;
19805 : 291823 : cs_base = old_cs_base;
19806 : 291823 : ns->resolved = 1;
19807 : :
19808 : 291823 : gfc_run_passes (ns);
19809 : :
19810 : 291823 : if (!ns->construct_entities)
19811 : 280221 : gfc_omp_restore_state (&old_omp_state);
19812 : : }
|