Debian Codegraph @

gcc/fortran/trans-expr.cc

raw
1 /* Expression translation
2 Copyright (C) 2002-2025 Free Software Foundation, Inc.
3 Contributed by Paul Brook <[email protected]>
4 and Steven Bosscher <[email protected]>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* trans-expr.cc-- generate GENERIC trees for gfc_expr. */
23
24 #define INCLUDE_MEMORY
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "options.h"
29 #include "tree.h"
30 #include "gfortran.h"
31 #include "trans.h"
32 #include "stringpool.h"
33 #include "diagnostic-core.h" /* For fatal_error. */
34 #include "fold-const.h"
35 #include "langhooks.h"
36 #include "arith.h"
37 #include "constructor.h"
38 #include "trans-const.h"
39 #include "trans-types.h"
40 #include "trans-array.h"
41 /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
42 #include "trans-stmt.h"
43 #include "dependency.h"
44 #include "gimplify.h"
45 #include "tm.h" /* For CHAR_TYPE_SIZE. */
46
47
48 /* Calculate the number of characters in a string. */
49
50 static tree
51 gfc_get_character_len (tree type)
52 {
53 tree len;
54
55 gcc_assert (type && TREE_CODE (type) == ARRAY_TYPE
56 && TYPE_STRING_FLAG (type));
57
58 len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
59 len = (len) ? (len) : (integer_zero_node);
60 return fold_convert (gfc_charlen_type_node, len);
61 }
62
63
64
65 /* Calculate the number of bytes in a string. */
66
67 tree
68 gfc_get_character_len_in_bytes (tree type)
69 {
70 tree tmp, len;
71
72 gcc_assert (type && TREE_CODE (type) == ARRAY_TYPE
73 && TYPE_STRING_FLAG (type));
74
75 tmp = TYPE_SIZE_UNIT (TREE_TYPE (type));
76 tmp = (tmp && !integer_zerop (tmp))
77 ? (fold_convert (gfc_charlen_type_node, tmp)) : (NULL_TREE);
78 len = gfc_get_character_len (type);
79 if (tmp && len && !integer_zerop (len))
80 len = fold_build2_loc (input_location, MULT_EXPR,
81 gfc_charlen_type_node, len, tmp);
82 return len;
83 }
84
85
86 /* Convert a scalar to an array descriptor. To be used for assumed-rank
87 arrays. */
88
89 static tree
90 get_scalar_to_descriptor_type (tree scalar, symbol_attribute attr)
91 {
92 enum gfc_array_kind akind;
93
94 if (attr.pointer)
95 akind = GFC_ARRAY_POINTER_CONT;
96 else if (attr.allocatable)
97 akind = GFC_ARRAY_ALLOCATABLE;
98 else
99 akind = GFC_ARRAY_ASSUMED_SHAPE_CONT;
100
101 if (POINTER_TYPE_P (TREE_TYPE (scalar)))
102 scalar = TREE_TYPE (scalar);
103 return gfc_get_array_type_bounds (TREE_TYPE (scalar), 0, 0, NULL, NULL, 1,
104 akind, !(attr.pointer || attr.target));
105 }
106
107 tree
108 gfc_conv_scalar_to_descriptor (gfc_se *se, tree scalar, symbol_attribute attr)
109 {
110 tree desc, type, etype;
111
112 type = get_scalar_to_descriptor_type (scalar, attr);
113 etype = TREE_TYPE (scalar);
114 desc = gfc_create_var (type, "desc");
115 DECL_ARTIFICIAL (desc) = 1;
116
117 if (CONSTANT_CLASS_P (scalar))
118 {
119 tree tmp;
120 tmp = gfc_create_var (TREE_TYPE (scalar), "scalar");
121 gfc_add_modify (&se->pre, tmp, scalar);
122 scalar = tmp;
123 }
124 if (!POINTER_TYPE_P (TREE_TYPE (scalar)))
125 scalar = gfc_build_addr_expr (NULL_TREE, scalar);
126 else if (TREE_TYPE (etype) && TREE_CODE (TREE_TYPE (etype)) == ARRAY_TYPE)
127 etype = TREE_TYPE (etype);
128 gfc_add_modify (&se->pre, gfc_conv_descriptor_dtype (desc),
129 gfc_get_dtype_rank_type (0, etype));
130 gfc_conv_descriptor_data_set (&se->pre, desc, scalar);
131 gfc_conv_descriptor_span_set (&se->pre, desc,
132 gfc_conv_descriptor_elem_len (desc));
133
134 /* Copy pointer address back - but only if it could have changed and
135 if the actual argument is a pointer and not, e.g., NULL(). */
136 if ((attr.pointer || attr.allocatable) && attr.intent != INTENT_IN)
137 gfc_add_modify (&se->post, scalar,
138 fold_convert (TREE_TYPE (scalar),
139 gfc_conv_descriptor_data_get (desc)));
140 return desc;
141 }
142
143
144 /* Get the coarray token from the ultimate array or component ref.
145 Returns a NULL_TREE, when the ref object is not allocatable or pointer. */
146
147 tree
148 gfc_get_ultimate_alloc_ptr_comps_caf_token (gfc_se *outerse, gfc_expr *expr)
149 {
150 gfc_symbol *sym = expr->symtree->n.sym;
151 bool is_coarray = sym->ts.type == BT_CLASS
152 ? CLASS_DATA (sym)->attr.codimension
153 : sym->attr.codimension;
154 gfc_expr *caf_expr = gfc_copy_expr (expr);
155 gfc_ref *ref = caf_expr->ref, *last_caf_ref = NULL;
156
157 while (ref)
158 {
159 if (ref->type == REF_COMPONENT
160 && (ref->u.c.component->attr.allocatable
161 || ref->u.c.component->attr.pointer)
162 && (is_coarray || ref->u.c.component->attr.codimension))
163 last_caf_ref = ref;
164 ref = ref->next;
165 }
166
167 if (last_caf_ref == NULL)
168 {
169 gfc_free_expr (caf_expr);
170 return NULL_TREE;
171 }
172
173 tree comp = last_caf_ref->u.c.component->caf_token
174 ? gfc_comp_caf_token (last_caf_ref->u.c.component)
175 : NULL_TREE,
176 caf;
177 gfc_se se;
178 bool comp_ref = !last_caf_ref->u.c.component->attr.dimension;
179 if (comp == NULL_TREE && comp_ref)
180 {
181 gfc_free_expr (caf_expr);
182 return NULL_TREE;
183 }
184 gfc_init_se (&se, outerse);
185 gfc_free_ref_list (last_caf_ref->next);
186 last_caf_ref->next = NULL;
187 caf_expr->rank = comp_ref ? 0 : last_caf_ref->u.c.component->as->rank;
188 caf_expr->corank = last_caf_ref->u.c.component->as
189 ? last_caf_ref->u.c.component->as->corank
190 : expr->corank;
191 se.want_pointer = comp_ref;
192 gfc_conv_expr (&se, caf_expr);
193 gfc_add_block_to_block (&outerse->pre, &se.pre);
194
195 if (TREE_CODE (se.expr) == COMPONENT_REF && comp_ref)
196 se.expr = TREE_OPERAND (se.expr, 0);
197 gfc_free_expr (caf_expr);
198
199 if (comp_ref)
200 caf = fold_build3_loc (input_location, COMPONENT_REF,
201 TREE_TYPE (comp), se.expr, comp, NULL_TREE);
202 else
203 caf = gfc_conv_descriptor_token (se.expr);
204 return gfc_build_addr_expr (NULL_TREE, caf);
205 }
206
207
208 /* This is the seed for an eventual trans-class.c
209
210 The following parameters should not be used directly since they might
211 in future implementations. Use the corresponding APIs. */
212 #define CLASS_DATA_FIELD 0
213 #define CLASS_VPTR_FIELD 1
214 #define CLASS_LEN_FIELD 2
215 #define VTABLE_HASH_FIELD 0
216 #define VTABLE_SIZE_FIELD 1
217 #define VTABLE_EXTENDS_FIELD 2
218 #define VTABLE_DEF_INIT_FIELD 3
219 #define VTABLE_COPY_FIELD 4
220 #define VTABLE_FINAL_FIELD 5
221 #define VTABLE_DEALLOCATE_FIELD 6
222
223
224 tree
225 gfc_class_set_static_fields (tree decl, tree vptr, tree data)
226 {
227 tree tmp;
228 tree field;
229 vec<constructor_elt, va_gc> *init = NULL;
230
231 field = TYPE_FIELDS (TREE_TYPE (decl));
232 tmp = gfc_advance_chain (field, CLASS_DATA_FIELD);
233 CONSTRUCTOR_APPEND_ELT (init, tmp, data);
234
235 tmp = gfc_advance_chain (field, CLASS_VPTR_FIELD);
236 CONSTRUCTOR_APPEND_ELT (init, tmp, vptr);
237
238 return build_constructor (TREE_TYPE (decl), init);
239 }
240
241
242 tree
243 gfc_class_data_get (tree decl)
244 {
245 tree data;
246 if (POINTER_TYPE_P (TREE_TYPE (decl)))
247 decl = build_fold_indirect_ref_loc (input_location, decl);
248 data = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
249 CLASS_DATA_FIELD);
250 return fold_build3_loc (input_location, COMPONENT_REF,
251 TREE_TYPE (data), decl, data,
252 NULL_TREE);
253 }
254
255
256 tree
257 gfc_class_vptr_get (tree decl)
258 {
259 tree vptr;
260 /* For class arrays decl may be a temporary descriptor handle, the vptr is
261 then available through the saved descriptor. */
262 if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
263 && GFC_DECL_SAVED_DESCRIPTOR (decl))
264 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
265 if (POINTER_TYPE_P (TREE_TYPE (decl)))
266 decl = build_fold_indirect_ref_loc (input_location, decl);
267 vptr = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
268 CLASS_VPTR_FIELD);
269 return fold_build3_loc (input_location, COMPONENT_REF,
270 TREE_TYPE (vptr), decl, vptr,
271 NULL_TREE);
272 }
273
274
275 tree
276 gfc_class_len_get (tree decl)
277 {
278 tree len;
279 /* For class arrays decl may be a temporary descriptor handle, the len is
280 then available through the saved descriptor. */
281 if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
282 && GFC_DECL_SAVED_DESCRIPTOR (decl))
283 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
284 if (POINTER_TYPE_P (TREE_TYPE (decl)))
285 decl = build_fold_indirect_ref_loc (input_location, decl);
286 len = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
287 CLASS_LEN_FIELD);
288 return fold_build3_loc (input_location, COMPONENT_REF,
289 TREE_TYPE (len), decl, len,
290 NULL_TREE);
291 }
292
293
294 /* Try to get the _len component of a class. When the class is not unlimited
295 poly, i.e. no _len field exists, then return a zero node. */
296
297 static tree
298 gfc_class_len_or_zero_get (tree decl)
299 {
300 tree len;
301 /* For class arrays decl may be a temporary descriptor handle, the vptr is
302 then available through the saved descriptor. */
303 if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
304 && GFC_DECL_SAVED_DESCRIPTOR (decl))
305 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
306 if (POINTER_TYPE_P (TREE_TYPE (decl)))
307 decl = build_fold_indirect_ref_loc (input_location, decl);
308 len = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
309 CLASS_LEN_FIELD);
310 return len != NULL_TREE ? fold_build3_loc (input_location, COMPONENT_REF,
311 TREE_TYPE (len), decl, len,
312 NULL_TREE)
313 : build_zero_cst (gfc_charlen_type_node);
314 }
315
316
317 tree
318 gfc_resize_class_size_with_len (stmtblock_t * block, tree class_expr, tree size)
319 {
320 tree tmp;
321 tree tmp2;
322 tree type;
323
324 tmp = gfc_class_len_or_zero_get (class_expr);
325
326 /* Include the len value in the element size if present. */
327 if (!integer_zerop (tmp))
328 {
329 type = TREE_TYPE (size);
330 if (block)
331 {
332 size = gfc_evaluate_now (size, block);
333 tmp = gfc_evaluate_now (fold_convert (type , tmp), block);
334 }
335 else
336 tmp = fold_convert (type , tmp);
337 tmp2 = fold_build2_loc (input_location, MULT_EXPR,
338 type, size, tmp);
339 tmp = fold_build2_loc (input_location, GT_EXPR,
340 logical_type_node, tmp,
341 build_zero_cst (type));
342 size = fold_build3_loc (input_location, COND_EXPR,
343 type, tmp, tmp2, size);
344 }
345 else
346 return size;
347
348 if (block)
349 size = gfc_evaluate_now (size, block);
350
351 return size;
352 }
353
354
355 /* Get the specified FIELD from the VPTR. */
356
357 static tree
358 vptr_field_get (tree vptr, int fieldno)
359 {
360 tree field;
361 vptr = build_fold_indirect_ref_loc (input_location, vptr);
362 field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (vptr)),
363 fieldno);
364 field = fold_build3_loc (input_location, COMPONENT_REF,
365 TREE_TYPE (field), vptr, field,
366 NULL_TREE);
367 gcc_assert (field);
368 return field;
369 }
370
371
372 /* Get the field from the class' vptr. */
373
374 static tree
375 class_vtab_field_get (tree decl, int fieldno)
376 {
377 tree vptr;
378 vptr = gfc_class_vptr_get (decl);
379 return vptr_field_get (vptr, fieldno);
380 }
381
382
383 /* Define a macro for creating the class_vtab_* and vptr_* accessors in
384 unison. */
385 #define VTAB_GET_FIELD_GEN(name, field) tree \
386 gfc_class_vtab_## name ##_get (tree cl) \
387 { \
388 return class_vtab_field_get (cl, field); \
389 } \
390 \
391 tree \
392 gfc_vptr_## name ##_get (tree vptr) \
393 { \
394 return vptr_field_get (vptr, field); \
395 }
396
397 VTAB_GET_FIELD_GEN (hash, VTABLE_HASH_FIELD)
398 VTAB_GET_FIELD_GEN (extends, VTABLE_EXTENDS_FIELD)
399 VTAB_GET_FIELD_GEN (def_init, VTABLE_DEF_INIT_FIELD)
400 VTAB_GET_FIELD_GEN (copy, VTABLE_COPY_FIELD)
401 VTAB_GET_FIELD_GEN (final, VTABLE_FINAL_FIELD)
402 VTAB_GET_FIELD_GEN (deallocate, VTABLE_DEALLOCATE_FIELD)
403 #undef VTAB_GET_FIELD_GEN
404
405 /* The size field is returned as an array index type. Therefore treat
406 it and only it specially. */
407
408 tree
409 gfc_class_vtab_size_get (tree cl)
410 {
411 tree size;
412 size = class_vtab_field_get (cl, VTABLE_SIZE_FIELD);
413 /* Always return size as an array index type. */
414 size = fold_convert (gfc_array_index_type, size);
415 gcc_assert (size);
416 return size;
417 }
418
419 tree
420 gfc_vptr_size_get (tree vptr)
421 {
422 tree size;
423 size = vptr_field_get (vptr, VTABLE_SIZE_FIELD);
424 /* Always return size as an array index type. */
425 size = fold_convert (gfc_array_index_type, size);
426 gcc_assert (size);
427 return size;
428 }
429
430
431 #undef CLASS_DATA_FIELD
432 #undef CLASS_VPTR_FIELD
433 #undef CLASS_LEN_FIELD
434 #undef VTABLE_HASH_FIELD
435 #undef VTABLE_SIZE_FIELD
436 #undef VTABLE_EXTENDS_FIELD
437 #undef VTABLE_DEF_INIT_FIELD
438 #undef VTABLE_COPY_FIELD
439 #undef VTABLE_FINAL_FIELD
440
441
442 /* IF ts is null (default), search for the last _class ref in the chain
443 of references of the expression and cut the chain there. Although
444 this routine is similiar to class.cc:gfc_add_component_ref (), there
445 is a significant difference: gfc_add_component_ref () concentrates
446 on an array ref that is the last ref in the chain and is oblivious
447 to the kind of refs following.
448 ELSE IF ts is non-null the cut is at the class entity or component
449 that is followed by an array reference, which is not an element.
450 These calls come from trans-array.cc:build_class_array_ref, which
451 handles scalarized class array references.*/
452
453 gfc_expr *
454 gfc_find_and_cut_at_last_class_ref (gfc_expr *e, bool is_mold,
455 gfc_typespec **ts)
456 {
457 gfc_expr *base_expr;
458 gfc_ref *ref, *class_ref, *tail = NULL, *array_ref;
459
460 /* Find the last class reference. */
461 class_ref = NULL;
462 array_ref = NULL;
463
464 if (ts)
465 {
466 if (e->symtree
467 && e->symtree->n.sym->ts.type == BT_CLASS)
468 *ts = &e->symtree->n.sym->ts;
469 else
470 *ts = NULL;
471 }
472
473 for (ref = e->ref; ref; ref = ref->next)
474 {
475 if (ts)
476 {
477 if (ref->type == REF_COMPONENT
478 && ref->u.c.component->ts.type == BT_CLASS
479 && ref->next && ref->next->type == REF_COMPONENT
480 && !strcmp (ref->next->u.c.component->name, "_data")
481 && ref->next->next
482 && ref->next->next->type == REF_ARRAY
483 && ref->next->next->u.ar.type != AR_ELEMENT)
484 {
485 *ts = &ref->u.c.component->ts;
486 class_ref = ref;
487 break;
488 }
489
490 if (ref->next == NULL)
491 break;
492 }
493 else
494 {
495 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
496 array_ref = ref;
497
498 if (ref->type == REF_COMPONENT
499 && ref->u.c.component->ts.type == BT_CLASS)
500 {
501 /* Component to the right of a part reference with nonzero
502 rank must not have the ALLOCATABLE attribute. If attempts
503 are made to reference such a component reference, an error
504 results followed by an ICE. */
505 if (array_ref
506 && CLASS_DATA (ref->u.c.component)->attr.allocatable)
507 return NULL;
508 class_ref = ref;
509 }
510 }
511 }
512
513 if (ts && *ts == NULL)
514 return NULL;
515
516 /* Remove and store all subsequent references after the
517 CLASS reference. */
518 if (class_ref)
519 {
520 tail = class_ref->next;
521 class_ref->next = NULL;
522 }
523 else if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
524 {
525 tail = e->ref;
526 e->ref = NULL;
527 }
528
529 if (is_mold)
530 base_expr = gfc_expr_to_initialize (e);
531 else
532 base_expr = gfc_copy_expr (e);
533
534 /* Restore the original tail expression. */
535 if (class_ref)
536 {
537 gfc_free_ref_list (class_ref->next);
538 class_ref->next = tail;
539 }
540 else if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
541 {
542 gfc_free_ref_list (e->ref);
543 e->ref = tail;
544 }
545 return base_expr;
546 }
547
548 /* Reset the vptr to the declared type, e.g. after deallocation.
549 Use the variable in CLASS_CONTAINER if available. Otherwise, recreate
550 one with e or class_type. At least one of the two has to be set. The
551 generated assignment code is added at the end of BLOCK. */
552
553 void
554 gfc_reset_vptr (stmtblock_t *block, gfc_expr *e, tree class_container,
555 gfc_symbol *class_type)
556 {
557 tree vptr = NULL_TREE;
558
559 if (class_container != NULL_TREE)
560 vptr = gfc_get_vptr_from_expr (class_container);
561
562 if (vptr == NULL_TREE)
563 {
564 gfc_se se;
565 gcc_assert (e);
566
567 /* Evaluate the expression and obtain the vptr from it. */
568 gfc_init_se (&se, NULL);
569 if (e->rank)
570 gfc_conv_expr_descriptor (&se, e);
571 else
572 gfc_conv_expr (&se, e);
573 gfc_add_block_to_block (block, &se.pre);
574
575 vptr = gfc_get_vptr_from_expr (se.expr);
576 }
577
578 /* If a vptr is not found, we can do nothing more. */
579 if (vptr == NULL_TREE)
580 return;
581
582 if (UNLIMITED_POLY (e)
583 || UNLIMITED_POLY (class_type)
584 /* When the class_type's source is not a symbol (e.g. a component's ts),
585 then look at the _data-components type. */
586 || (class_type != NULL && class_type->ts.type == BT_UNKNOWN
587 && class_type->components && class_type->components->ts.u.derived
588 && class_type->components->ts.u.derived->attr.unlimited_polymorphic))
589 gfc_add_modify (block, vptr, build_int_cst (TREE_TYPE (vptr), 0));
590 else
591 {
592 gfc_symbol *vtab, *type = nullptr;
593 tree vtable;
594
595 if (e)
596 type = e->ts.u.derived;
597 else if (class_type)
598 {
599 if (class_type->ts.type == BT_CLASS)
600 type = CLASS_DATA (class_type)->ts.u.derived;
601 else
602 type = class_type;
603 }
604 gcc_assert (type);
605 /* Return the vptr to the address of the declared type. */
606 vtab = gfc_find_derived_vtab (type);
607 vtable = vtab->backend_decl;
608 if (vtable == NULL_TREE)
609 vtable = gfc_get_symbol_decl (vtab);
610 vtable = gfc_build_addr_expr (NULL, vtable);
611 vtable = fold_convert (TREE_TYPE (vptr), vtable);
612 gfc_add_modify (block, vptr, vtable);
613 }
614 }
615
616 /* Set the vptr of a class in to from the type given in from. If from is NULL,
617 then reset the vptr to the default or to. */
618
619 void
620 gfc_class_set_vptr (stmtblock_t *block, tree to, tree from)
621 {
622 tree tmp, vptr_ref;
623 gfc_symbol *type;
624
625 vptr_ref = gfc_get_vptr_from_expr (to);
626 if (POINTER_TYPE_P (TREE_TYPE (from))
627 && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (from))))
628 {
629 gfc_add_modify (block, vptr_ref,
630 fold_convert (TREE_TYPE (vptr_ref),
631 gfc_get_vptr_from_expr (from)));
632 return;
633 }
634 tmp = gfc_get_vptr_from_expr (from);
635 if (tmp)
636 {
637 gfc_add_modify (block, vptr_ref,
638 fold_convert (TREE_TYPE (vptr_ref), tmp));
639 return;
640 }
641 if (VAR_P (from)
642 && strncmp (IDENTIFIER_POINTER (DECL_NAME (from)), "__vtab", 6) == 0)
643 {
644 gfc_add_modify (block, vptr_ref,
645 gfc_build_addr_expr (TREE_TYPE (vptr_ref), from));
646 return;
647 }
648 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (from)))
649 && GFC_CLASS_TYPE_P (
650 TREE_TYPE (TREE_OPERAND (TREE_OPERAND (from, 0), 0))))
651 {
652 gfc_add_modify (block, vptr_ref,
653 fold_convert (TREE_TYPE (vptr_ref),
654 gfc_get_vptr_from_expr (TREE_OPERAND (
655 TREE_OPERAND (from, 0), 0))));
656 return;
657 }
658
659 /* If nothing of the above matches, set the vtype according to the type. */
660 tmp = TREE_TYPE (from);
661 if (POINTER_TYPE_P (tmp))
662 tmp = TREE_TYPE (tmp);
663 gfc_find_symbol (IDENTIFIER_POINTER (TYPE_NAME (tmp)), gfc_current_ns, 1,
664 &type);
665 tmp = gfc_find_derived_vtab (type)->backend_decl;
666 gcc_assert (tmp);
667 gfc_add_modify (block, vptr_ref,
668 gfc_build_addr_expr (TREE_TYPE (vptr_ref), tmp));
669 }
670
671 /* Reset the len for unlimited polymorphic objects. */
672
673 void
674 gfc_reset_len (stmtblock_t *block, gfc_expr *expr)
675 {
676 gfc_expr *e;
677 gfc_se se_len;
678 e = gfc_find_and_cut_at_last_class_ref (expr);
679 if (e == NULL)
680 return;
681 gfc_add_len_component (e);
682 gfc_init_se (&se_len, NULL);
683 gfc_conv_expr (&se_len, e);
684 gfc_add_modify (block, se_len.expr,
685 fold_convert (TREE_TYPE (se_len.expr), integer_zero_node));
686 gfc_free_expr (e);
687 }
688
689
690 /* Obtain the last class reference in a gfc_expr. Return NULL_TREE if no class
691 reference is found. Note that it is up to the caller to avoid using this
692 for expressions other than variables. */
693
694 tree
695 gfc_get_class_from_gfc_expr (gfc_expr *e)
696 {
697 gfc_expr *class_expr;
698 gfc_se cse;
699 class_expr = gfc_find_and_cut_at_last_class_ref (e);
700 if (class_expr == NULL)
701 return NULL_TREE;
702 gfc_init_se (&cse, NULL);
703 gfc_conv_expr (&cse, class_expr);
704 gfc_free_expr (class_expr);
705 return cse.expr;
706 }
707
708
709 /* Obtain the last class reference in an expression.
710 Return NULL_TREE if no class reference is found. */
711
712 tree
713 gfc_get_class_from_expr (tree expr)
714 {
715 tree tmp;
716 tree type;
717
718 for (tmp = expr; tmp; tmp = TREE_OPERAND (tmp, 0))
719 {
720 if (CONSTANT_CLASS_P (tmp))
721 return NULL_TREE;
722
723 type = TREE_TYPE (tmp);
724 while (type)
725 {
726 if (GFC_CLASS_TYPE_P (type))
727 return tmp;
728 if (type != TYPE_CANONICAL (type))
729 type = TYPE_CANONICAL (type);
730 else
731 type = NULL_TREE;
732 }
733 if (VAR_P (tmp) || TREE_CODE (tmp) == PARM_DECL)
734 break;
735 }
736
737 if (POINTER_TYPE_P (TREE_TYPE (tmp)))
738 tmp = build_fold_indirect_ref_loc (input_location, tmp);
739
740 if (GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
741 return tmp;
742
743 return NULL_TREE;
744 }
745
746
747 /* Obtain the vptr of the last class reference in an expression.
748 Return NULL_TREE if no class reference is found. */
749
750 tree
751 gfc_get_vptr_from_expr (tree expr)
752 {
753 tree tmp;
754
755 tmp = gfc_get_class_from_expr (expr);
756
757 if (tmp != NULL_TREE)
758 return gfc_class_vptr_get (tmp);
759
760 return NULL_TREE;
761 }
762
763 void
764 gfc_class_array_data_assign (stmtblock_t *block, tree lhs_desc, tree rhs_desc,
765 bool lhs_type)
766 {
767 tree tmp, tmp2, type;
768
769 gfc_conv_descriptor_data_set (block, lhs_desc,
770 gfc_conv_descriptor_data_get (rhs_desc));
771 gfc_conv_descriptor_offset_set (block, lhs_desc,
772 gfc_conv_descriptor_offset_get (rhs_desc));
773
774 gfc_add_modify (block, gfc_conv_descriptor_dtype (lhs_desc),
775 gfc_conv_descriptor_dtype (rhs_desc));
776
777 /* Assign the dimension as range-ref. */
778 tmp = gfc_get_descriptor_dimension (lhs_desc);
779 tmp2 = gfc_get_descriptor_dimension (rhs_desc);
780
781 type = lhs_type ? TREE_TYPE (tmp) : TREE_TYPE (tmp2);
782 tmp = build4_loc (input_location, ARRAY_RANGE_REF, type, tmp,
783 gfc_index_zero_node, NULL_TREE, NULL_TREE);
784 tmp2 = build4_loc (input_location, ARRAY_RANGE_REF, type, tmp2,
785 gfc_index_zero_node, NULL_TREE, NULL_TREE);
786 gfc_add_modify (block, tmp, tmp2);
787 }
788
789 /* Takes a derived type expression and returns the address of a temporary
790 class object of the 'declared' type. If opt_vptr_src is not NULL, this is
791 used for the temporary class object.
792 optional_alloc_ptr is false when the dummy is neither allocatable
793 nor a pointer; that's only relevant for the optional handling.
794 The optional argument 'derived_array' is used to preserve the parmse
795 expression for deallocation of allocatable components. Assumed rank
796 formal arguments made this necessary. */
797 void
798 gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym,
799 tree opt_vptr_src, bool optional,
800 bool optional_alloc_ptr, const char *proc_name,
801 tree *derived_array)
802 {
803 tree cond_optional = NULL_TREE;
804 gfc_ss *ss;
805 tree ctree;
806 tree var;
807 tree tmp;
808 tree packed = NULL_TREE;
809
810 /* The derived type needs to be converted to a temporary CLASS object. */
811 tmp = gfc_typenode_for_spec (&fsym->ts);
812 var = gfc_create_var (tmp, "class");
813
814 /* Set the vptr. */
815 if (opt_vptr_src)
816 gfc_class_set_vptr (&parmse->pre, var, opt_vptr_src);
817 else
818 gfc_reset_vptr (&parmse->pre, e, var);
819
820 /* Now set the data field. */
821 ctree = gfc_class_data_get (var);
822
823 if (flag_coarray == GFC_FCOARRAY_LIB && CLASS_DATA (fsym)->attr.codimension)
824 {
825 tree token;
826 tmp = gfc_get_tree_for_caf_expr (e);
827 if (POINTER_TYPE_P (TREE_TYPE (tmp)))
828 tmp = build_fold_indirect_ref (tmp);
829 gfc_get_caf_token_offset (parmse, &token, nullptr, tmp, NULL_TREE, e);
830 gfc_add_modify (&parmse->pre, gfc_conv_descriptor_token (ctree), token);
831 }
832
833 if (optional)
834 cond_optional = gfc_conv_expr_present (e->symtree->n.sym);
835
836 /* Set the _len as early as possible. */
837 if (fsym->ts.u.derived->components->ts.type == BT_DERIVED
838 && fsym->ts.u.derived->components->ts.u.derived->attr
839 .unlimited_polymorphic)
840 {
841 /* Take care about initializing the _len component correctly. */
842 tree len_tree = gfc_class_len_get (var);
843 if (UNLIMITED_POLY (e))
844 {
845 gfc_expr *len;
846 gfc_se se;
847
848 len = gfc_find_and_cut_at_last_class_ref (e);
849 gfc_add_len_component (len);
850 gfc_init_se (&se, NULL);
851 gfc_conv_expr (&se, len);
852 if (optional)
853 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se.expr),
854 cond_optional, se.expr,
855 fold_convert (TREE_TYPE (se.expr),
856 integer_zero_node));
857 else
858 tmp = se.expr;
859 gfc_free_expr (len);
860 }
861 else
862 tmp = integer_zero_node;
863 gfc_add_modify (&parmse->pre, len_tree,
864 fold_convert (TREE_TYPE (len_tree), tmp));
865 }
866
867 if (parmse->expr && POINTER_TYPE_P (TREE_TYPE (parmse->expr)))
868 {
869 /* If there is a ready made pointer to a derived type, use it
870 rather than evaluating the expression again. */
871 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
872 gfc_add_modify (&parmse->pre, ctree, tmp);
873 }
874 else if (parmse->ss && parmse->ss->info && parmse->ss->info->useflags)
875 {
876 /* For an array reference in an elemental procedure call we need
877 to retain the ss to provide the scalarized array reference. */
878 gfc_conv_expr_reference (parmse, e);
879 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
880 if (optional)
881 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
882 cond_optional, tmp,
883 fold_convert (TREE_TYPE (tmp), null_pointer_node));
884 gfc_add_modify (&parmse->pre, ctree, tmp);
885 }
886 else
887 {
888 ss = gfc_walk_expr (e);
889 if (ss == gfc_ss_terminator)
890 {
891 parmse->ss = NULL;
892 gfc_conv_expr_reference (parmse, e);
893
894 /* Scalar to an assumed-rank array. */
895 if (fsym->ts.u.derived->components->as)
896 {
897 tree type;
898 type = get_scalar_to_descriptor_type (parmse->expr,
899 gfc_expr_attr (e));
900 gfc_add_modify (&parmse->pre, gfc_conv_descriptor_dtype (ctree),
901 gfc_get_dtype (type));
902 if (optional)
903 parmse->expr = build3_loc (input_location, COND_EXPR,
904 TREE_TYPE (parmse->expr),
905 cond_optional, parmse->expr,
906 fold_convert (TREE_TYPE (parmse->expr),
907 null_pointer_node));
908 gfc_conv_descriptor_data_set (&parmse->pre, ctree, parmse->expr);
909 }
910 else
911 {
912 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
913 if (optional)
914 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
915 cond_optional, tmp,
916 fold_convert (TREE_TYPE (tmp),
917 null_pointer_node));
918 gfc_add_modify (&parmse->pre, ctree, tmp);
919 }
920 }
921 else
922 {
923 stmtblock_t block;
924 gfc_init_block (&block);
925 gfc_ref *ref;
926 int dim;
927 tree lbshift = NULL_TREE;
928
929 /* Array refs with sections indicate, that a for a formal argument
930 expecting contiguous repacking needs to be done. */
931 for (ref = e->ref; ref; ref = ref->next)
932 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
933 break;
934 if (IS_CLASS_ARRAY (fsym)
935 && (CLASS_DATA (fsym)->as->type == AS_EXPLICIT
936 || CLASS_DATA (fsym)->as->type == AS_ASSUMED_SIZE)
937 && (ref || e->rank != fsym->ts.u.derived->components->as->rank))
938 fsym->attr.contiguous = 1;
939
940 /* Detect any array references with vector subscripts. */
941 for (ref = e->ref; ref; ref = ref->next)
942 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT
943 && ref->u.ar.type != AR_FULL)
944 {
945 for (dim = 0; dim < ref->u.ar.dimen; dim++)
946 if (ref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
947 break;
948 if (dim < ref->u.ar.dimen)
949 break;
950 }
951 /* Array references with vector subscripts and non-variable
952 expressions need be converted to a one-based descriptor. */
953 if (ref || e->expr_type != EXPR_VARIABLE)
954 lbshift = gfc_index_one_node;
955
956 parmse->expr = var;
957 gfc_conv_array_parameter (parmse, e, false, fsym, proc_name, nullptr,
958 &lbshift, &packed);
959
960 if (derived_array && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (parmse->expr)))
961 {
962 *derived_array
963 = gfc_create_var (TREE_TYPE (parmse->expr), "array");
964 gfc_add_modify (&block, *derived_array, parmse->expr);
965 }
966
967 if (optional)
968 {
969 tmp = gfc_finish_block (&block);
970
971 gfc_init_block (&block);
972 gfc_conv_descriptor_data_set (&block, ctree, null_pointer_node);
973 if (derived_array && *derived_array != NULL_TREE)
974 gfc_conv_descriptor_data_set (&block, *derived_array,
975 null_pointer_node);
976
977 tmp = build3_v (COND_EXPR, cond_optional, tmp,
978 gfc_finish_block (&block));
979 gfc_add_expr_to_block (&parmse->pre, tmp);
980 }
981 else
982 gfc_add_block_to_block (&parmse->pre, &block);
983 }
984 }
985
986 /* Pass the address of the class object. */
987 if (packed)
988 parmse->expr = packed;
989 else
990 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
991
992 if (optional && optional_alloc_ptr)
993 parmse->expr
994 = build3_loc (input_location, COND_EXPR, TREE_TYPE (parmse->expr),
995 cond_optional, parmse->expr,
996 fold_convert (TREE_TYPE (parmse->expr), null_pointer_node));
997 }
998
999 /* Create a new class container, which is required as scalar coarrays
1000 have an array descriptor while normal scalars haven't. Optionally,
1001 NULL pointer checks are added if the argument is OPTIONAL. */
1002
1003 static void
1004 class_scalar_coarray_to_class (gfc_se *parmse, gfc_expr *e,
1005 gfc_typespec class_ts, bool optional)
1006 {
1007 tree var, ctree, tmp;
1008 stmtblock_t block;
1009 gfc_ref *ref;
1010 gfc_ref *class_ref;
1011
1012 gfc_init_block (&block);
1013
1014 class_ref = NULL;
1015 for (ref = e->ref; ref; ref = ref->next)
1016 {
1017 if (ref->type == REF_COMPONENT
1018 && ref->u.c.component->ts.type == BT_CLASS)
1019 class_ref = ref;
1020 }
1021
1022 if (class_ref == NULL
1023 && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
1024 tmp = e->symtree->n.sym->backend_decl;
1025 else
1026 {
1027 /* Remove everything after the last class reference, convert the
1028 expression and then recover its tailend once more. */
1029 gfc_se tmpse;
1030 ref = class_ref->next;
1031 class_ref->next = NULL;
1032 gfc_init_se (&tmpse, NULL);
1033 gfc_conv_expr (&tmpse, e);
1034 class_ref->next = ref;
1035 tmp = tmpse.expr;
1036 }
1037
1038 var = gfc_typenode_for_spec (&class_ts);
1039 var = gfc_create_var (var, "class");
1040
1041 ctree = gfc_class_vptr_get (var);
1042 gfc_add_modify (&block, ctree,
1043 fold_convert (TREE_TYPE (ctree), gfc_class_vptr_get (tmp)));
1044
1045 ctree = gfc_class_data_get (var);
1046 tmp = gfc_conv_descriptor_data_get (
1047 gfc_class_data_get (GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (tmp)))
1048 ? tmp
1049 : GFC_DECL_SAVED_DESCRIPTOR (tmp)));
1050 gfc_add_modify (&block, ctree, fold_convert (TREE_TYPE (ctree), tmp));
1051
1052 /* Pass the address of the class object. */
1053 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
1054
1055 if (optional)
1056 {
1057 tree cond = gfc_conv_expr_present (e->symtree->n.sym);
1058 tree tmp2;
1059
1060 tmp = gfc_finish_block (&block);
1061
1062 gfc_init_block (&block);
1063 tmp2 = gfc_class_data_get (var);
1064 gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
1065 null_pointer_node));
1066 tmp2 = gfc_finish_block (&block);
1067
1068 tmp = build3_loc (input_location, COND_EXPR, void_type_node,
1069 cond, tmp, tmp2);
1070 gfc_add_expr_to_block (&parmse->pre, tmp);
1071 }
1072 else
1073 gfc_add_block_to_block (&parmse->pre, &block);
1074 }
1075
1076
1077 /* Takes an intrinsic type expression and returns the address of a temporary
1078 class object of the 'declared' type. */
1079 void
1080 gfc_conv_intrinsic_to_class (gfc_se *parmse, gfc_expr *e,
1081 gfc_typespec class_ts)
1082 {
1083 gfc_symbol *vtab;
1084 gfc_ss *ss;
1085 tree ctree;
1086 tree var;
1087 tree tmp;
1088 int dim;
1089 bool unlimited_poly;
1090
1091 unlimited_poly = class_ts.type == BT_CLASS
1092 && class_ts.u.derived->components->ts.type == BT_DERIVED
1093 && class_ts.u.derived->components->ts.u.derived
1094 ->attr.unlimited_polymorphic;
1095
1096 /* The intrinsic type needs to be converted to a temporary
1097 CLASS object. */
1098 tmp = gfc_typenode_for_spec (&class_ts);
1099 var = gfc_create_var (tmp, "class");
1100
1101 /* Force a temporary for component or substring references. */
1102 if (unlimited_poly
1103 && class_ts.u.derived->components->attr.dimension
1104 && !class_ts.u.derived->components->attr.allocatable
1105 && !class_ts.u.derived->components->attr.class_pointer
1106 && is_subref_array (e))
1107 parmse->force_tmp = 1;
1108
1109 /* Set the vptr. */
1110 ctree = gfc_class_vptr_get (var);
1111
1112 vtab = gfc_find_vtab (&e->ts);
1113 gcc_assert (vtab);
1114 tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
1115 gfc_add_modify (&parmse->pre, ctree,
1116 fold_convert (TREE_TYPE (ctree), tmp));
1117
1118 /* Now set the data field. */
1119 ctree = gfc_class_data_get (var);
1120 if (parmse->ss && parmse->ss->info->useflags)
1121 {
1122 /* For an array reference in an elemental procedure call we need
1123 to retain the ss to provide the scalarized array reference. */
1124 gfc_conv_expr_reference (parmse, e);
1125 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
1126 gfc_add_modify (&parmse->pre, ctree, tmp);
1127 }
1128 else
1129 {
1130 ss = gfc_walk_expr (e);
1131 if (ss == gfc_ss_terminator)
1132 {
1133 parmse->ss = NULL;
1134 gfc_conv_expr_reference (parmse, e);
1135 if (class_ts.u.derived->components->as
1136 && class_ts.u.derived->components->as->type == AS_ASSUMED_RANK)
1137 {
1138 tmp = gfc_conv_scalar_to_descriptor (parmse, parmse->expr,
1139 gfc_expr_attr (e));
1140 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
1141 TREE_TYPE (ctree), tmp);
1142 }
1143 else
1144 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
1145 gfc_add_modify (&parmse->pre, ctree, tmp);
1146 }
1147 else
1148 {
1149 parmse->ss = ss;
1150 parmse->use_offset = 1;
1151 gfc_conv_expr_descriptor (parmse, e);
1152
1153 /* Array references with vector subscripts and non-variable expressions
1154 need be converted to a one-based descriptor. */
1155 if (e->expr_type != EXPR_VARIABLE)
1156 {
1157 for (dim = 0; dim < e->rank; ++dim)
1158 gfc_conv_shift_descriptor_lbound (&parmse->pre, parmse->expr,
1159 dim, gfc_index_one_node);
1160 }
1161
1162 if (class_ts.u.derived->components->as->rank != e->rank)
1163 {
1164 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
1165 TREE_TYPE (ctree), parmse->expr);
1166 gfc_add_modify (&parmse->pre, ctree, tmp);
1167 }
1168 else
1169 gfc_add_modify (&parmse->pre, ctree, parmse->expr);
1170 }
1171 }
1172
1173 gcc_assert (class_ts.type == BT_CLASS);
1174 if (unlimited_poly)
1175 {
1176 ctree = gfc_class_len_get (var);
1177 /* When the actual arg is a char array, then set the _len component of the
1178 unlimited polymorphic entity to the length of the string. */
1179 if (e->ts.type == BT_CHARACTER)
1180 {
1181 /* Start with parmse->string_length because this seems to be set to a
1182 correct value more often. */
1183 if (parmse->string_length)
1184 tmp = parmse->string_length;
1185 /* When the string_length is not yet set, then try the backend_decl of
1186 the cl. */
1187 else if (e->ts.u.cl->backend_decl)
1188 tmp = e->ts.u.cl->backend_decl;
1189 /* If both of the above approaches fail, then try to generate an
1190 expression from the input, which is only feasible currently, when the
1191 expression can be evaluated to a constant one. */
1192 else
1193 {
1194 /* Try to simplify the expression. */
1195 gfc_simplify_expr (e, 0);
1196 if (e->expr_type == EXPR_CONSTANT && !e->ts.u.cl->resolved)
1197 {
1198 /* Amazingly all data is present to compute the length of a
1199 constant string, but the expression is not yet there. */
1200 e->ts.u.cl->length = gfc_get_constant_expr (BT_INTEGER,
1201 gfc_charlen_int_kind,
1202 &e->where);
1203 mpz_set_ui (e->ts.u.cl->length->value.integer,
1204 e->value.character.length);
1205 gfc_conv_const_charlen (e->ts.u.cl);
1206 e->ts.u.cl->resolved = 1;
1207 tmp = e->ts.u.cl->backend_decl;
1208 }
1209 else
1210 {
1211 gfc_error ("Cannot compute the length of the char array "
1212 "at %L.", &e->where);
1213 }
1214 }
1215 }
1216 else
1217 tmp = integer_zero_node;
1218
1219 gfc_add_modify (&parmse->pre, ctree, fold_convert (TREE_TYPE (ctree), tmp));
1220 }
1221
1222 /* Pass the address of the class object. */
1223 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
1224 }
1225
1226
1227 /* Takes a scalarized class array expression and returns the
1228 address of a temporary scalar class object of the 'declared'
1229 type.
1230 OOP-TODO: This could be improved by adding code that branched on
1231 the dynamic type being the same as the declared type. In this case
1232 the original class expression can be passed directly.
1233 optional_alloc_ptr is false when the dummy is neither allocatable
1234 nor a pointer; that's relevant for the optional handling.
1235 Set copyback to true if class container's _data and _vtab pointers
1236 might get modified. */
1237
1238 void
1239 gfc_conv_class_to_class (gfc_se *parmse, gfc_expr *e, gfc_typespec class_ts,
1240 bool elemental, bool copyback, bool optional,
1241 bool optional_alloc_ptr)
1242 {
1243 tree ctree;
1244 tree var;
1245 tree tmp;
1246 tree vptr;
1247 tree cond = NULL_TREE;
1248 tree slen = NULL_TREE;
1249 gfc_ref *ref;
1250 gfc_ref *class_ref;
1251 stmtblock_t block;
1252 bool full_array = false;
1253
1254 /* If this is the data field of a class temporary, the class expression
1255 can be obtained and returned directly. */
1256 if (e->expr_type != EXPR_VARIABLE
1257 && TREE_CODE (parmse->expr) == COMPONENT_REF
1258 && !GFC_CLASS_TYPE_P (TREE_TYPE (parmse->expr))
1259 && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (parmse->expr, 0))))
1260 {
1261 parmse->expr = TREE_OPERAND (parmse->expr, 0);
1262 if (!VAR_P (parmse->expr))
1263 parmse->expr = gfc_evaluate_now (parmse->expr, &parmse->pre);
1264 parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
1265 return;
1266 }
1267
1268 gfc_init_block (&block);
1269
1270 class_ref = NULL;
1271 for (ref = e->ref; ref; ref = ref->next)
1272 {
1273 if (ref->type == REF_COMPONENT
1274 && ref->u.c.component->ts.type == BT_CLASS)
1275 class_ref = ref;
1276
1277 if (ref->next == NULL)
1278 break;
1279 }
1280
1281 if ((ref == NULL || class_ref == ref)
1282 && !(gfc_is_class_array_function (e) && parmse->class_vptr != NULL_TREE)
1283 && (!class_ts.u.derived->components->as
1284 || class_ts.u.derived->components->as->rank != -1))
1285 return;
1286
1287 /* Test for FULL_ARRAY. */
1288 if (e->rank == 0
1289 && ((gfc_expr_attr (e).codimension && gfc_expr_attr (e).dimension)
1290 || (class_ts.u.derived->components->as
1291 && class_ts.u.derived->components->as->type == AS_ASSUMED_RANK)))
1292 full_array = true;
1293 else
1294 gfc_is_class_array_ref (e, &full_array);
1295
1296 /* The derived type needs to be converted to a temporary
1297 CLASS object. */
1298 tmp = gfc_typenode_for_spec (&class_ts);
1299 var = gfc_create_var (tmp, "class");
1300
1301 /* Set the data. */
1302 ctree = gfc_class_data_get (var);
1303 if (class_ts.u.derived->components->as
1304 && e->rank != class_ts.u.derived->components->as->rank)
1305 {
1306 if (e->rank == 0)
1307 {
1308 tree type = get_scalar_to_descriptor_type (parmse->expr,
1309 gfc_expr_attr (e));
1310 gfc_add_modify (&block, gfc_conv_descriptor_dtype (ctree),
1311 gfc_get_dtype (type));
1312
1313 tmp = gfc_class_data_get (parmse->expr);
1314 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
1315 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
1316
1317 gfc_conv_descriptor_data_set (&block, ctree, tmp);
1318 }
1319 else
1320 gfc_class_array_data_assign (&block, ctree, parmse->expr, false);
1321 }
1322 else
1323 {
1324 if (TREE_TYPE (parmse->expr) != TREE_TYPE (ctree))
1325 parmse->expr = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
1326 TREE_TYPE (ctree), parmse->expr);
1327 gfc_add_modify (&block, ctree, parmse->expr);
1328 }
1329
1330 /* Return the data component, except in the case of scalarized array
1331 references, where nullification of the cannot occur and so there
1332 is no need. */
1333 if (!elemental && full_array && copyback)
1334 {
1335 if (class_ts.u.derived->components->as
1336 && e->rank != class_ts.u.derived->components->as->rank)
1337 {
1338 if (e->rank == 0)
1339 {
1340 tmp = gfc_class_data_get (parmse->expr);
1341 gfc_add_modify (&parmse->post, tmp,
1342 fold_convert (TREE_TYPE (tmp),
1343 gfc_conv_descriptor_data_get (ctree)));
1344 }
1345 else
1346 gfc_class_array_data_assign (&parmse->post, parmse->expr, ctree,
1347 true);
1348 }
1349 else
1350 gfc_add_modify (&parmse->post, parmse->expr, ctree);
1351 }
1352
1353 /* Set the vptr. */
1354 ctree = gfc_class_vptr_get (var);
1355
1356 /* The vptr is the second field of the actual argument.
1357 First we have to find the corresponding class reference. */
1358
1359 tmp = NULL_TREE;
1360 if (gfc_is_class_array_function (e)
1361 && parmse->class_vptr != NULL_TREE)
1362 tmp = parmse->class_vptr;
1363 else if (class_ref == NULL
1364 && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
1365 {
1366 tmp = e->symtree->n.sym->backend_decl;
1367
1368 if (TREE_CODE (tmp) == FUNCTION_DECL)
1369 tmp = gfc_get_fake_result_decl (e->symtree->n.sym, 0);
1370
1371 if (DECL_LANG_SPECIFIC (tmp) && GFC_DECL_SAVED_DESCRIPTOR (tmp))
1372 tmp = GFC_DECL_SAVED_DESCRIPTOR (tmp);
1373
1374 slen = build_zero_cst (size_type_node);
1375 }
1376 else if (parmse->class_container != NULL_TREE)
1377 /* Don't redundantly evaluate the expression if the required information
1378 is already available. */
1379 tmp = parmse->class_container;
1380 else
1381 {
1382 /* Remove everything after the last class reference, convert the
1383 expression and then recover its tailend once more. */
1384 gfc_se tmpse;
1385 ref = class_ref->next;
1386 class_ref->next = NULL;
1387 gfc_init_se (&tmpse, NULL);
1388 gfc_conv_expr (&tmpse, e);
1389 class_ref->next = ref;
1390 tmp = tmpse.expr;
1391 slen = tmpse.string_length;
1392 }
1393
1394 gcc_assert (tmp != NULL_TREE);
1395
1396 /* Dereference if needs be. */
1397 if (TREE_CODE (TREE_TYPE (tmp)) == REFERENCE_TYPE)
1398 tmp = build_fold_indirect_ref_loc (input_location, tmp);
1399
1400 if (!(gfc_is_class_array_function (e) && parmse->class_vptr))
1401 vptr = gfc_class_vptr_get (tmp);
1402 else
1403 vptr = tmp;
1404
1405 gfc_add_modify (&block, ctree,
1406 fold_convert (TREE_TYPE (ctree), vptr));
1407
1408 /* Return the vptr component, except in the case of scalarized array
1409 references, where the dynamic type cannot change. */
1410 if (!elemental && full_array && copyback)
1411 gfc_add_modify (&parmse->post, vptr,
1412 fold_convert (TREE_TYPE (vptr), ctree));
1413
1414 /* For unlimited polymorphic objects also set the _len component. */
1415 if (class_ts.type == BT_CLASS
1416 && class_ts.u.derived->components
1417 && class_ts.u.derived->components->ts.u
1418 .derived->attr.unlimited_polymorphic)
1419 {
1420 ctree = gfc_class_len_get (var);
1421 if (UNLIMITED_POLY (e))
1422 tmp = gfc_class_len_get (tmp);
1423 else if (e->ts.type == BT_CHARACTER)
1424 {
1425 gcc_assert (slen != NULL_TREE);
1426 tmp = slen;
1427 }
1428 else
1429 tmp = build_zero_cst (size_type_node);
1430 gfc_add_modify (&parmse->pre, ctree,
1431 fold_convert (TREE_TYPE (ctree), tmp));
1432
1433 /* Return the len component, except in the case of scalarized array
1434 references, where the dynamic type cannot change. */
1435 if (!elemental && full_array && copyback
1436 && (UNLIMITED_POLY (e) || VAR_P (tmp)))
1437 gfc_add_modify (&parmse->post, tmp,
1438 fold_convert (TREE_TYPE (tmp), ctree));
1439 }
1440
1441 if (optional)
1442 {
1443 tree tmp2;
1444
1445 cond = gfc_conv_expr_present (e->symtree->n.sym);
1446 /* parmse->pre may contain some preparatory instructions for the
1447 temporary array descriptor. Those may only be executed when the
1448 optional argument is set, therefore add parmse->pre's instructions
1449 to block, which is later guarded by an if (optional_arg_given). */
1450 gfc_add_block_to_block (&parmse->pre, &block);
1451 block.head = parmse->pre.head;
1452 parmse->pre.head = NULL_TREE;
1453 tmp = gfc_finish_block (&block);
1454
1455 if (optional_alloc_ptr)
1456 tmp2 = build_empty_stmt (input_location);
1457 else
1458 {
1459 gfc_init_block (&block);
1460
1461 tmp2 = gfc_conv_descriptor_data_get (gfc_class_data_get (var));
1462 gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
1463 null_pointer_node));
1464 tmp2 = gfc_finish_block (&block);
1465 }
1466
1467 tmp = build3_loc (input_location, COND_EXPR, void_type_node,
1468 cond, tmp, tmp2);
1469 gfc_add_expr_to_block (&parmse->pre, tmp);
1470
1471 if (!elemental && full_array && copyback)
1472 {
1473 tmp2 = build_empty_stmt (input_location);
1474 tmp = gfc_finish_block (&parmse->post);
1475 tmp = build3_loc (input_location, COND_EXPR, void_type_node,
1476 cond, tmp, tmp2);
1477 gfc_add_expr_to_block (&parmse->post, tmp);
1478 }
1479 }
1480 else
1481 gfc_add_block_to_block (&parmse->pre, &block);
1482
1483 /* Pass the address of the class object. */
1484 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
1485
1486 if (optional && optional_alloc_ptr)
1487 parmse->expr = build3_loc (input_location, COND_EXPR,
1488 TREE_TYPE (parmse->expr),
1489 cond, parmse->expr,
1490 fold_convert (TREE_TYPE (parmse->expr),
1491 null_pointer_node));
1492 }
1493
1494
1495 /* Given a class array declaration and an index, returns the address
1496 of the referenced element. */
1497
1498 static tree
1499 gfc_get_class_array_ref (tree index, tree class_decl, tree data_comp,
1500 bool unlimited)
1501 {
1502 tree data, size, tmp, ctmp, offset, ptr;
1503
1504 data = data_comp != NULL_TREE ? data_comp :
1505 gfc_class_data_get (class_decl);
1506 size = gfc_class_vtab_size_get (class_decl);
1507
1508 if (unlimited)
1509 {
1510 tmp = fold_convert (gfc_array_index_type,
1511 gfc_class_len_get (class_decl));
1512 ctmp = fold_build2_loc (input_location, MULT_EXPR,
1513 gfc_array_index_type, size, tmp);
1514 tmp = fold_build2_loc (input_location, GT_EXPR,
1515 logical_type_node, tmp,
1516 build_zero_cst (TREE_TYPE (tmp)));
1517 size = fold_build3_loc (input_location, COND_EXPR,
1518 gfc_array_index_type, tmp, ctmp, size);
1519 }
1520
1521 offset = fold_build2_loc (input_location, MULT_EXPR,
1522 gfc_array_index_type,
1523 index, size);
1524
1525 data = gfc_conv_descriptor_data_get (data);
1526 ptr = fold_convert (pvoid_type_node, data);
1527 ptr = fold_build_pointer_plus_loc (input_location, ptr, offset);
1528 return fold_convert (TREE_TYPE (data), ptr);
1529 }
1530
1531
1532 /* Copies one class expression to another, assuming that if either
1533 'to' or 'from' are arrays they are packed. Should 'from' be
1534 NULL_TREE, the initialization expression for 'to' is used, assuming
1535 that the _vptr is set. */
1536
1537 tree
1538 gfc_copy_class_to_class (tree from, tree to, tree nelems, bool unlimited)
1539 {
1540 tree fcn;
1541 tree fcn_type;
1542 tree from_data;
1543 tree from_len;
1544 tree to_data;
1545 tree to_len;
1546 tree to_ref;
1547 tree from_ref;
1548 vec<tree, va_gc> *args;
1549 tree tmp;
1550 tree stdcopy;
1551 tree extcopy;
1552 tree index;
1553 bool is_from_desc = false, is_to_class = false;
1554
1555 args = NULL;
1556 /* To prevent warnings on uninitialized variables. */
1557 from_len = to_len = NULL_TREE;
1558
1559 if (from != NULL_TREE)
1560 fcn = gfc_class_vtab_copy_get (from);
1561 else
1562 fcn = gfc_class_vtab_copy_get (to);
1563
1564 fcn_type = TREE_TYPE (TREE_TYPE (fcn));
1565
1566 if (from != NULL_TREE)
1567 {
1568 is_from_desc = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from));
1569 if (is_from_desc)
1570 {
1571 from_data = from;
1572 from = GFC_DECL_SAVED_DESCRIPTOR (from);
1573 }
1574 else
1575 {
1576 /* Check that from is a class. When the class is part of a coarray,
1577 then from is a common pointer and is to be used as is. */
1578 tmp = POINTER_TYPE_P (TREE_TYPE (from))
1579 ? build_fold_indirect_ref (from) : from;
1580 from_data =
1581 (GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
1582 || (DECL_P (tmp) && GFC_DECL_CLASS (tmp)))
1583 ? gfc_class_data_get (from) : from;
1584 is_from_desc = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data));
1585 }
1586 }
1587 else
1588 from_data = gfc_class_vtab_def_init_get (to);
1589
1590 if (unlimited)
1591 {
1592 if (from != NULL_TREE && unlimited)
1593 from_len = gfc_class_len_or_zero_get (from);
1594 else
1595 from_len = build_zero_cst (size_type_node);
1596 }
1597
1598 if (GFC_CLASS_TYPE_P (TREE_TYPE (to)))
1599 {
1600 is_to_class = true;
1601 to_data = gfc_class_data_get (to);
1602 if (unlimited)
1603 to_len = gfc_class_len_get (to);
1604 }
1605 else
1606 /* When to is a BT_DERIVED and not a BT_CLASS, then to_data == to. */
1607 to_data = to;
1608
1609 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (to_data)))
1610 {
1611 stmtblock_t loopbody;
1612 stmtblock_t body;
1613 stmtblock_t ifbody;
1614 gfc_loopinfo loop;
1615
1616 gfc_init_block (&body);
1617 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1618 gfc_array_index_type, nelems,
1619 gfc_index_one_node);
1620 nelems = gfc_evaluate_now (tmp, &body);
1621 index = gfc_create_var (gfc_array_index_type, "S");
1622
1623 if (is_from_desc)
1624 {
1625 from_ref = gfc_get_class_array_ref (index, from, from_data,
1626 unlimited);
1627 vec_safe_push (args, from_ref);
1628 }
1629 else
1630 vec_safe_push (args, from_data);
1631
1632 if (is_to_class)
1633 to_ref = gfc_get_class_array_ref (index, to, to_data, unlimited);
1634 else
1635 {
1636 tmp = gfc_conv_array_data (to);
1637 tmp = build_fold_indirect_ref_loc (input_location, tmp);
1638 to_ref = gfc_build_addr_expr (NULL_TREE,
1639 gfc_build_array_ref (tmp, index, to));
1640 }
1641 vec_safe_push (args, to_ref);
1642
1643 /* Add bounds check. */
1644 if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) > 0 && is_from_desc)
1645 {
1646 const char *name = "<<unknown>>";
1647 int dim, rank;
1648
1649 if (DECL_P (to))
1650 name = IDENTIFIER_POINTER (DECL_NAME (to));
1651
1652 rank = GFC_TYPE_ARRAY_RANK (TREE_TYPE (from_data));
1653 for (dim = 1; dim <= rank; dim++)
1654 {
1655 tree from_len, to_len, cond;
1656 char *msg;
1657
1658 from_len = gfc_conv_descriptor_size (from_data, dim);
1659 from_len = fold_convert (long_integer_type_node, from_len);
1660 to_len = gfc_conv_descriptor_size (to_data, dim);
1661 to_len = fold_convert (long_integer_type_node, to_len);
1662 msg = xasprintf ("Array bound mismatch for dimension %d "
1663 "of array '%s' (%%ld/%%ld)",
1664 dim, name);
1665 cond = fold_build2_loc (input_location, NE_EXPR,
1666 logical_type_node, from_len, to_len);
1667 gfc_trans_runtime_check (true, false, cond, &body,
1668 NULL, msg, to_len, from_len);
1669 free (msg);
1670 }
1671 }
1672
1673 tmp = build_call_vec (fcn_type, fcn, args);
1674
1675 /* Build the body of the loop. */
1676 gfc_init_block (&loopbody);
1677 gfc_add_expr_to_block (&loopbody, tmp);
1678
1679 /* Build the loop and return. */
1680 gfc_init_loopinfo (&loop);
1681 loop.dimen = 1;
1682 loop.from[0] = gfc_index_zero_node;
1683 loop.loopvar[0] = index;
1684 loop.to[0] = nelems;
1685 gfc_trans_scalarizing_loops (&loop, &loopbody);
1686 gfc_init_block (&ifbody);
1687 gfc_add_block_to_block (&ifbody, &loop.pre);
1688 stdcopy = gfc_finish_block (&ifbody);
1689 /* In initialization mode from_len is a constant zero. */
1690 if (unlimited && !integer_zerop (from_len))
1691 {
1692 vec_safe_push (args, from_len);
1693 vec_safe_push (args, to_len);
1694 tmp = build_call_vec (fcn_type, fcn, args);
1695 /* Build the body of the loop. */
1696 gfc_init_block (&loopbody);
1697 gfc_add_expr_to_block (&loopbody, tmp);
1698
1699 /* Build the loop and return. */
1700 gfc_init_loopinfo (&loop);
1701 loop.dimen = 1;
1702 loop.from[0] = gfc_index_zero_node;
1703 loop.loopvar[0] = index;
1704 loop.to[0] = nelems;
1705 gfc_trans_scalarizing_loops (&loop, &loopbody);
1706 gfc_init_block (&ifbody);
1707 gfc_add_block_to_block (&ifbody, &loop.pre);
1708 extcopy = gfc_finish_block (&ifbody);
1709
1710 tmp = fold_build2_loc (input_location, GT_EXPR,
1711 logical_type_node, from_len,
1712 build_zero_cst (TREE_TYPE (from_len)));
1713 tmp = fold_build3_loc (input_location, COND_EXPR,
1714 void_type_node, tmp, extcopy, stdcopy);
1715 gfc_add_expr_to_block (&body, tmp);
1716 tmp = gfc_finish_block (&body);
1717 }
1718 else
1719 {
1720 gfc_add_expr_to_block (&body, stdcopy);
1721 tmp = gfc_finish_block (&body);
1722 }
1723 gfc_cleanup_loop (&loop);
1724 }
1725 else
1726 {
1727 gcc_assert (!is_from_desc);
1728 vec_safe_push (args, from_data);
1729 vec_safe_push (args, to_data);
1730 stdcopy = build_call_vec (fcn_type, fcn, args);
1731
1732 /* In initialization mode from_len is a constant zero. */
1733 if (unlimited && !integer_zerop (from_len))
1734 {
1735 vec_safe_push (args, from_len);
1736 vec_safe_push (args, to_len);
1737 extcopy = build_call_vec (fcn_type, unshare_expr (fcn), args);
1738 tmp = fold_build2_loc (input_location, GT_EXPR,
1739 logical_type_node, from_len,
1740 build_zero_cst (TREE_TYPE (from_len)));
1741 tmp = fold_build3_loc (input_location, COND_EXPR,
1742 void_type_node, tmp, extcopy, stdcopy);
1743 }
1744 else
1745 tmp = stdcopy;
1746 }
1747
1748 /* Only copy _def_init to to_data, when it is not a NULL-pointer. */
1749 if (from == NULL_TREE)
1750 {
1751 tree cond;
1752 cond = fold_build2_loc (input_location, NE_EXPR,
1753 logical_type_node,
1754 from_data, null_pointer_node);
1755 tmp = fold_build3_loc (input_location, COND_EXPR,
1756 void_type_node, cond,
1757 tmp, build_empty_stmt (input_location));
1758 }
1759
1760 return tmp;
1761 }
1762
1763
1764 static tree
1765 gfc_trans_class_array_init_assign (gfc_expr *rhs, gfc_expr *lhs, gfc_expr *obj)
1766 {
1767 gfc_actual_arglist *actual;
1768 gfc_expr *ppc;
1769 gfc_code *ppc_code;
1770 tree res;
1771
1772 actual = gfc_get_actual_arglist ();
1773 actual->expr = gfc_copy_expr (rhs);
1774 actual->next = gfc_get_actual_arglist ();
1775 actual->next->expr = gfc_copy_expr (lhs);
1776 ppc = gfc_copy_expr (obj);
1777 gfc_add_vptr_component (ppc);
1778 gfc_add_component_ref (ppc, "_copy");
1779 ppc_code = gfc_get_code (EXEC_CALL);
1780 ppc_code->resolved_sym = ppc->symtree->n.sym;
1781 /* Although '_copy' is set to be elemental in class.cc, it is
1782 not staying that way. Find out why, sometime.... */
1783 ppc_code->resolved_sym->attr.elemental = 1;
1784 ppc_code->ext.actual = actual;
1785 ppc_code->expr1 = ppc;
1786 /* Since '_copy' is elemental, the scalarizer will take care
1787 of arrays in gfc_trans_call. */
1788 res = gfc_trans_call (ppc_code, false, NULL, NULL, false);
1789 gfc_free_statements (ppc_code);
1790
1791 if (UNLIMITED_POLY(obj))
1792 {
1793 /* Check if rhs is non-NULL. */
1794 gfc_se src;
1795 gfc_init_se (&src, NULL);
1796 gfc_conv_expr (&src, rhs);
1797 src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
1798 tree cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1799 src.expr, fold_convert (TREE_TYPE (src.expr),
1800 null_pointer_node));
1801 res = build3_loc (input_location, COND_EXPR, TREE_TYPE (res), cond, res,
1802 build_empty_stmt (input_location));
1803 }
1804
1805 return res;
1806 }
1807
1808 /* Special case for initializing a polymorphic dummy with INTENT(OUT).
1809 A MEMCPY is needed to copy the full data from the default initializer
1810 of the dynamic type. */
1811
1812 tree
1813 gfc_trans_class_init_assign (gfc_code *code)
1814 {
1815 stmtblock_t block;
1816 tree tmp;
1817 bool cmp_flag = true;
1818 gfc_se dst,src,memsz;
1819 gfc_expr *lhs, *rhs, *sz;
1820 gfc_component *cmp;
1821 gfc_symbol *sym;
1822 gfc_ref *ref;
1823
1824 gfc_start_block (&block);
1825
1826 lhs = gfc_copy_expr (code->expr1);
1827
1828 rhs = gfc_copy_expr (code->expr1);
1829 gfc_add_vptr_component (rhs);
1830
1831 /* Make sure that the component backend_decls have been built, which
1832 will not have happened if the derived types concerned have not
1833 been referenced. */
1834 gfc_get_derived_type (rhs->ts.u.derived);
1835 gfc_add_def_init_component (rhs);
1836 /* The _def_init is always scalar. */
1837 rhs->rank = 0;
1838
1839 /* Check def_init for initializers. If this is an INTENT(OUT) dummy with all
1840 default initializer components NULL, use the passed value even though
1841 F2018(8.5.10) asserts that it should considered to be undefined. This is
1842 needed for consistency with other brands. */
1843 sym = code->expr1->expr_type == EXPR_VARIABLE ? code->expr1->symtree->n.sym
1844 : NULL;
1845 if (code->op != EXEC_ALLOCATE
1846 && sym && sym->attr.dummy
1847 && sym->attr.intent == INTENT_OUT)
1848 {
1849 ref = rhs->ref;
1850 while (ref && ref->next)
1851 ref = ref->next;
1852 cmp = ref->u.c.component->ts.u.derived->components;
1853 for (; cmp; cmp = cmp->next)
1854 {
1855 if (cmp->initializer)
1856 break;
1857 else if (!cmp->next)
1858 cmp_flag = false;
1859 }
1860 }
1861
1862 if (code->expr1->ts.type == BT_CLASS
1863 && CLASS_DATA (code->expr1)->attr.dimension)
1864 {
1865 gfc_array_spec *tmparr = gfc_get_array_spec ();
1866 *tmparr = *CLASS_DATA (code->expr1)->as;
1867 /* Adding the array ref to the class expression results in correct
1868 indexing to the dynamic type. */
1869 gfc_add_full_array_ref (lhs, tmparr);
1870 tmp = gfc_trans_class_array_init_assign (rhs, lhs, code->expr1);
1871 }
1872 else if (cmp_flag)
1873 {
1874 /* Scalar initialization needs the _data component. */
1875 gfc_add_data_component (lhs);
1876 sz = gfc_copy_expr (code->expr1);
1877 gfc_add_vptr_component (sz);
1878 gfc_add_size_component (sz);
1879
1880 gfc_init_se (&dst, NULL);
1881 gfc_init_se (&src, NULL);
1882 gfc_init_se (&memsz, NULL);
1883 gfc_conv_expr (&dst, lhs);
1884 gfc_conv_expr (&src, rhs);
1885 gfc_conv_expr (&memsz, sz);
1886 gfc_add_block_to_block (&block, &src.pre);
1887 src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
1888
1889 tmp = gfc_build_memcpy_call (dst.expr, src.expr, memsz.expr);
1890
1891 if (UNLIMITED_POLY(code->expr1))
1892 {
1893 /* Check if _def_init is non-NULL. */
1894 tree cond = fold_build2_loc (input_location, NE_EXPR,
1895 logical_type_node, src.expr,
1896 fold_convert (TREE_TYPE (src.expr),
1897 null_pointer_node));
1898 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
1899 tmp, build_empty_stmt (input_location));
1900 }
1901 }
1902 else
1903 tmp = build_empty_stmt (input_location);
1904
1905 if (code->expr1->symtree->n.sym->attr.dummy
1906 && (code->expr1->symtree->n.sym->attr.optional
1907 || code->expr1->symtree->n.sym->ns->proc_name->attr.entry_master))
1908 {
1909 tree present = gfc_conv_expr_present (code->expr1->symtree->n.sym);
1910 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
1911 present, tmp,
1912 build_empty_stmt (input_location));
1913 }
1914
1915 gfc_add_expr_to_block (&block, tmp);
1916 gfc_free_expr (lhs);
1917 gfc_free_expr (rhs);
1918
1919 return gfc_finish_block (&block);
1920 }
1921
1922
1923 /* Class valued elemental function calls or class array elements arriving
1924 in gfc_trans_scalar_assign come here. Wherever possible the vptr copy
1925 is used to ensure that the rhs dynamic type is assigned to the lhs. */
1926
1927 static bool
1928 trans_scalar_class_assign (stmtblock_t *block, gfc_se *lse, gfc_se *rse)
1929 {
1930 tree fcn;
1931 tree rse_expr;
1932 tree class_data;
1933 tree tmp;
1934 tree zero;
1935 tree cond;
1936 tree final_cond;
1937 stmtblock_t inner_block;
1938 bool is_descriptor;
1939 bool not_call_expr = TREE_CODE (rse->expr) != CALL_EXPR;
1940 bool not_lhs_array_type;
1941
1942 /* Temporaries arising from dependencies in assignment get cast as a
1943 character type of the dynamic size of the rhs. Use the vptr copy
1944 for this case. */
1945 tmp = TREE_TYPE (lse->expr);
1946 not_lhs_array_type = !(tmp && TREE_CODE (tmp) == ARRAY_TYPE
1947 && TYPE_MAX_VALUE (TYPE_DOMAIN (tmp)) != NULL_TREE);
1948
1949 /* Use ordinary assignment if the rhs is not a call expression or
1950 the lhs is not a class entity or an array(ie. character) type. */
1951 if ((not_call_expr && gfc_get_class_from_expr (lse->expr) == NULL_TREE)
1952 && not_lhs_array_type)
1953 return false;
1954
1955 /* Ordinary assignment can be used if both sides are class expressions
1956 since the dynamic type is preserved by copying the vptr. This
1957 should only occur, where temporaries are involved. */
1958 if (GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
1959 && GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
1960 return false;
1961
1962 /* Fix the class expression and the class data of the rhs. */
1963 if (!GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr))
1964 || not_call_expr)
1965 {
1966 tmp = gfc_get_class_from_expr (rse->expr);
1967 if (tmp == NULL_TREE)
1968 return false;
1969 rse_expr = gfc_evaluate_now (tmp, block);
1970 }
1971 else
1972 rse_expr = gfc_evaluate_now (rse->expr, block);
1973
1974 class_data = gfc_class_data_get (rse_expr);
1975
1976 /* Check that the rhs data is not null. */
1977 is_descriptor = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (class_data));
1978 if (is_descriptor)
1979 class_data = gfc_conv_descriptor_data_get (class_data);
1980 class_data = gfc_evaluate_now (class_data, block);
1981
1982 zero = build_int_cst (TREE_TYPE (class_data), 0);
1983 cond = fold_build2_loc (input_location, NE_EXPR,
1984 logical_type_node,
1985 class_data, zero);
1986
1987 /* Copy the rhs to the lhs. */
1988 fcn = gfc_vptr_copy_get (gfc_class_vptr_get (rse_expr));
1989 fcn = build_fold_indirect_ref_loc (input_location, fcn);
1990 tmp = gfc_evaluate_now (gfc_build_addr_expr (NULL, rse->expr), block);
1991 tmp = is_descriptor ? tmp : class_data;
1992 tmp = build_call_expr_loc (input_location, fcn, 2, tmp,
1993 gfc_build_addr_expr (NULL, lse->expr));
1994 gfc_add_expr_to_block (block, tmp);
1995
1996 /* Only elemental function results need to be finalised and freed. */
1997 if (not_call_expr)
1998 return true;
1999
2000 /* Finalize the class data if needed. */
2001 gfc_init_block (&inner_block);
2002 fcn = gfc_vptr_final_get (gfc_class_vptr_get (rse_expr));
2003 zero = build_int_cst (TREE_TYPE (fcn), 0);
2004 final_cond = fold_build2_loc (input_location, NE_EXPR,
2005 logical_type_node, fcn, zero);
2006 fcn = build_fold_indirect_ref_loc (input_location, fcn);
2007 tmp = build_call_expr_loc (input_location, fcn, 1, class_data);
2008 tmp = build3_v (COND_EXPR, final_cond,
2009 tmp, build_empty_stmt (input_location));
2010 gfc_add_expr_to_block (&inner_block, tmp);
2011
2012 /* Free the class data. */
2013 tmp = gfc_call_free (class_data);
2014 tmp = build3_v (COND_EXPR, cond, tmp,
2015 build_empty_stmt (input_location));
2016 gfc_add_expr_to_block (&inner_block, tmp);
2017
2018 /* Finish the inner block and subject it to the condition on the
2019 class data being non-zero. */
2020 tmp = gfc_finish_block (&inner_block);
2021 tmp = build3_v (COND_EXPR, cond, tmp,
2022 build_empty_stmt (input_location));
2023 gfc_add_expr_to_block (block, tmp);
2024
2025 return true;
2026 }
2027
2028 /* End of prototype trans-class.c */
2029
2030
2031 static void
2032 realloc_lhs_warning (bt type, bool array, locus *where)
2033 {
2034 if (array && type != BT_CLASS && type != BT_DERIVED && warn_realloc_lhs)
2035 gfc_warning (OPT_Wrealloc_lhs,
2036 "Code for reallocating the allocatable array at %L will "
2037 "be added", where);
2038 else if (warn_realloc_lhs_all)
2039 gfc_warning (OPT_Wrealloc_lhs_all,
2040 "Code for reallocating the allocatable variable at %L "
2041 "will be added", where);
2042 }
2043
2044
2045 static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
2046 gfc_expr *);
2047
2048 /* Copy the scalarization loop variables. */
2049
2050 static void
2051 gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
2052 {
2053 dest->ss = src->ss;
2054 dest->loop = src->loop;
2055 }
2056
2057
2058 /* Initialize a simple expression holder.
2059
2060 Care must be taken when multiple se are created with the same parent.
2061 The child se must be kept in sync. The easiest way is to delay creation
2062 of a child se until after the previous se has been translated. */
2063
2064 void
2065 gfc_init_se (gfc_se * se, gfc_se * parent)
2066 {
2067 memset (se, 0, sizeof (gfc_se));
2068 gfc_init_block (&se->pre);
2069 gfc_init_block (&se->finalblock);
2070 gfc_init_block (&se->post);
2071
2072 se->parent = parent;
2073
2074 if (parent)
2075 gfc_copy_se_loopvars (se, parent);
2076 }
2077
2078
2079 /* Advances to the next SS in the chain. Use this rather than setting
2080 se->ss = se->ss->next because all the parents needs to be kept in sync.
2081 See gfc_init_se. */
2082
2083 void
2084 gfc_advance_se_ss_chain (gfc_se * se)
2085 {
2086 gfc_se *p;
2087
2088 gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
2089
2090 p = se;
2091 /* Walk down the parent chain. */
2092 while (p != NULL)
2093 {
2094 /* Simple consistency check. */
2095 gcc_assert (p->parent == NULL || p->parent->ss == p->ss
2096 || p->parent->ss->nested_ss == p->ss);
2097
2098 p->ss = p->ss->next;
2099
2100 p = p->parent;
2101 }
2102 }
2103
2104
2105 /* Ensures the result of the expression as either a temporary variable
2106 or a constant so that it can be used repeatedly. */
2107
2108 void
2109 gfc_make_safe_expr (gfc_se * se)
2110 {
2111 tree var;
2112
2113 if (CONSTANT_CLASS_P (se->expr))
2114 return;
2115
2116 /* We need a temporary for this result. */
2117 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
2118 gfc_add_modify (&se->pre, var, se->expr);
2119 se->expr = var;
2120 }
2121
2122
2123 /* Return an expression which determines if a dummy parameter is present.
2124 Also used for arguments to procedures with multiple entry points. */
2125
2126 tree
2127 gfc_conv_expr_present (gfc_symbol * sym, bool use_saved_desc)
2128 {
2129 tree decl, orig_decl, cond;
2130
2131 gcc_assert (sym->attr.dummy);
2132 orig_decl = decl = gfc_get_symbol_decl (sym);
2133
2134 /* Intrinsic scalars and derived types with VALUE attribute which are passed
2135 by value use a hidden argument to denote the presence status. */
2136 if (sym->attr.value && !sym->attr.dimension && sym->ts.type != BT_CLASS)
2137 {
2138 char name[GFC_MAX_SYMBOL_LEN + 2];
2139 tree tree_name;
2140
2141 gcc_assert (TREE_CODE (decl) == PARM_DECL);
2142 name[0] = '.';
2143 strcpy (&name[1], sym->name);
2144 tree_name = get_identifier (name);
2145
2146 /* Walk function argument list to find hidden arg. */
2147 cond = DECL_ARGUMENTS (DECL_CONTEXT (decl));
2148 for ( ; cond != NULL_TREE; cond = TREE_CHAIN (cond))
2149 if (DECL_NAME (cond) == tree_name
2150 && DECL_ARTIFICIAL (cond))
2151 break;
2152
2153 gcc_assert (cond);
2154 return cond;
2155 }
2156
2157 /* Assumed-shape arrays use a local variable for the array data;
2158 the actual PARAM_DECL is in a saved decl. As the local variable
2159 is NULL, it can be checked instead, unless use_saved_desc is
2160 requested. */
2161
2162 if (use_saved_desc && TREE_CODE (decl) != PARM_DECL)
2163 {
2164 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
2165 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
2166 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
2167 }
2168
2169 cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, decl,
2170 fold_convert (TREE_TYPE (decl), null_pointer_node));
2171
2172 /* Fortran 2008 allows to pass null pointers and non-associated pointers
2173 as actual argument to denote absent dummies. For array descriptors,
2174 we thus also need to check the array descriptor. For BT_CLASS, it
2175 can also occur for scalars and F2003 due to type->class wrapping and
2176 class->class wrapping. Note further that BT_CLASS always uses an
2177 array descriptor for arrays, also for explicit-shape/assumed-size.
2178 For assumed-rank arrays, no local variable is generated, hence,
2179 the following also applies with !use_saved_desc. */
2180
2181 if ((use_saved_desc || TREE_CODE (orig_decl) == PARM_DECL)
2182 && !sym->attr.allocatable
2183 && ((sym->ts.type != BT_CLASS && !sym->attr.pointer)
2184 || (sym->ts.type == BT_CLASS
2185 && !CLASS_DATA (sym)->attr.allocatable
2186 && !CLASS_DATA (sym)->attr.class_pointer))
2187 && ((gfc_option.allow_std & GFC_STD_F2008) != 0
2188 || sym->ts.type == BT_CLASS))
2189 {
2190 tree tmp;
2191
2192 if ((sym->as && (sym->as->type == AS_ASSUMED_SHAPE
2193 || sym->as->type == AS_ASSUMED_RANK
2194 || sym->attr.codimension))
2195 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
2196 {
2197 tmp = build_fold_indirect_ref_loc (input_location, decl);
2198 if (sym->ts.type == BT_CLASS)
2199 tmp = gfc_class_data_get (tmp);
2200 tmp = gfc_conv_array_data (tmp);
2201 }
2202 else if (sym->ts.type == BT_CLASS)
2203 tmp = gfc_class_data_get (decl);
2204 else
2205 tmp = NULL_TREE;
2206
2207 if (tmp != NULL_TREE)
2208 {
2209 tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
2210 fold_convert (TREE_TYPE (tmp), null_pointer_node));
2211 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2212 logical_type_node, cond, tmp);
2213 }
2214 }
2215
2216 return cond;
2217 }
2218
2219
2220 /* Converts a missing, dummy argument into a null or zero. */
2221
2222 void
2223 gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
2224 {
2225 tree present;
2226 tree tmp;
2227
2228 present = gfc_conv_expr_present (arg->symtree->n.sym);
2229
2230 if (kind > 0)
2231 {
2232 /* Create a temporary and convert it to the correct type. */
2233 tmp = gfc_get_int_type (kind);
2234 tmp = fold_convert (tmp, build_fold_indirect_ref_loc (input_location,
2235 se->expr));
2236
2237 /* Test for a NULL value. */
2238 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
2239 tmp, fold_convert (TREE_TYPE (tmp), integer_one_node));
2240 tmp = gfc_evaluate_now (tmp, &se->pre);
2241 se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
2242 }
2243 else
2244 {
2245 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
2246 present, se->expr,
2247 build_zero_cst (TREE_TYPE (se->expr)));
2248 tmp = gfc_evaluate_now (tmp, &se->pre);
2249 se->expr = tmp;
2250 }
2251
2252 if (ts.type == BT_CHARACTER)
2253 {
2254 /* Handle deferred-length dummies that pass the character length by
2255 reference so that the value can be returned. */
2256 if (ts.deferred && INDIRECT_REF_P (se->string_length))
2257 {
2258 tmp = gfc_build_addr_expr (NULL_TREE, se->string_length);
2259 tmp = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
2260 present, tmp, null_pointer_node);
2261 tmp = gfc_evaluate_now (tmp, &se->pre);
2262 tmp = build_fold_indirect_ref_loc (input_location, tmp);
2263 }
2264 else
2265 {
2266 tmp = build_int_cst (gfc_charlen_type_node, 0);
2267 tmp = fold_build3_loc (input_location, COND_EXPR,
2268 gfc_charlen_type_node,
2269 present, se->string_length, tmp);
2270 tmp = gfc_evaluate_now (tmp, &se->pre);
2271 }
2272 se->string_length = tmp;
2273 }
2274 return;
2275 }
2276
2277
2278 /* Get the character length of an expression, looking through gfc_refs
2279 if necessary. */
2280
2281 tree
2282 gfc_get_expr_charlen (gfc_expr *e)
2283 {
2284 gfc_ref *r;
2285 tree length;
2286 tree previous = NULL_TREE;
2287 gfc_se se;
2288
2289 gcc_assert (e->expr_type == EXPR_VARIABLE
2290 && e->ts.type == BT_CHARACTER);
2291
2292 length = NULL; /* To silence compiler warning. */
2293
2294 if (is_subref_array (e) && e->ts.u.cl->length)
2295 {
2296 gfc_se tmpse;
2297 gfc_init_se (&tmpse, NULL);
2298 gfc_conv_expr_type (&tmpse, e->ts.u.cl->length, gfc_charlen_type_node);
2299 e->ts.u.cl->backend_decl = tmpse.expr;
2300 return tmpse.expr;
2301 }
2302
2303 /* First candidate: if the variable is of type CHARACTER, the
2304 expression's length could be the length of the character
2305 variable. */
2306 if (e->symtree->n.sym->ts.type == BT_CHARACTER)
2307 length = e->symtree->n.sym->ts.u.cl->backend_decl;
2308
2309 /* Look through the reference chain for component references. */
2310 for (r = e->ref; r; r = r->next)
2311 {
2312 previous = length;
2313 switch (r->type)
2314 {
2315 case REF_COMPONENT:
2316 if (r->u.c.component->ts.type == BT_CHARACTER)
2317 length = r->u.c.component->ts.u.cl->backend_decl;
2318 break;
2319
2320 case REF_ARRAY:
2321 /* Do nothing. */
2322 break;
2323
2324 case REF_SUBSTRING:
2325 gfc_init_se (&se, NULL);
2326 gfc_conv_expr_type (&se, r->u.ss.start, gfc_charlen_type_node);
2327 length = se.expr;
2328 if (r->u.ss.end)
2329 gfc_conv_expr_type (&se, r->u.ss.end, gfc_charlen_type_node);
2330 else
2331 se.expr = previous;
2332 length = fold_build2_loc (input_location, MINUS_EXPR,
2333 gfc_charlen_type_node,
2334 se.expr, length);
2335 length = fold_build2_loc (input_location, PLUS_EXPR,
2336 gfc_charlen_type_node, length,
2337 gfc_index_one_node);
2338 break;
2339
2340 default:
2341 gcc_unreachable ();
2342 break;
2343 }
2344 }
2345
2346 gcc_assert (length != NULL);
2347 return length;
2348 }
2349
2350
2351 /* Return for an expression the backend decl of the coarray. */
2352
2353 tree
2354 gfc_get_tree_for_caf_expr (gfc_expr *expr)
2355 {
2356 tree caf_decl;
2357 bool found = false;
2358 gfc_ref *ref;
2359
2360 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE);
2361
2362 /* Not-implemented diagnostic. */
2363 if (expr->symtree->n.sym->ts.type == BT_CLASS
2364 && UNLIMITED_POLY (expr->symtree->n.sym)
2365 && CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
2366 gfc_error ("Sorry, coindexed access to an unlimited polymorphic object at "
2367 "%L is not supported", &expr->where);
2368
2369 for (ref = expr->ref; ref; ref = ref->next)
2370 if (ref->type == REF_COMPONENT)
2371 {
2372 if (ref->u.c.component->ts.type == BT_CLASS
2373 && UNLIMITED_POLY (ref->u.c.component)
2374 && CLASS_DATA (ref->u.c.component)->attr.codimension)
2375 gfc_error ("Sorry, coindexed access to an unlimited polymorphic "
2376 "component at %L is not supported", &expr->where);
2377 }
2378
2379 /* Make sure the backend_decl is present before accessing it. */
2380 caf_decl = expr->symtree->n.sym->backend_decl == NULL_TREE
2381 ? gfc_get_symbol_decl (expr->symtree->n.sym)
2382 : expr->symtree->n.sym->backend_decl;
2383
2384 if (expr->symtree->n.sym->ts.type == BT_CLASS)
2385 {
2386 if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
2387 && GFC_DECL_SAVED_DESCRIPTOR (caf_decl))
2388 caf_decl = GFC_DECL_SAVED_DESCRIPTOR (caf_decl);
2389
2390 if (expr->ref && expr->ref->type == REF_ARRAY)
2391 {
2392 caf_decl = gfc_class_data_get (caf_decl);
2393 if (CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
2394 return caf_decl;
2395 }
2396 else if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
2397 && GFC_DECL_TOKEN (caf_decl)
2398 && CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
2399 return caf_decl;
2400
2401 for (ref = expr->ref; ref; ref = ref->next)
2402 {
2403 if (ref->type == REF_COMPONENT
2404 && strcmp (ref->u.c.component->name, "_data") != 0)
2405 {
2406 caf_decl = gfc_class_data_get (caf_decl);
2407 if (CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
2408 return caf_decl;
2409 break;
2410 }
2411 else if (ref->type == REF_ARRAY && ref->u.ar.dimen)
2412 break;
2413 }
2414 }
2415 if (expr->symtree->n.sym->attr.codimension)
2416 return caf_decl;
2417
2418 /* The following code assumes that the coarray is a component reachable via
2419 only scalar components/variables; the Fortran standard guarantees this. */
2420
2421 for (ref = expr->ref; ref; ref = ref->next)
2422 if (ref->type == REF_COMPONENT)
2423 {
2424 gfc_component *comp = ref->u.c.component;
2425
2426 if (POINTER_TYPE_P (TREE_TYPE (caf_decl)))
2427 caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
2428 caf_decl = fold_build3_loc (input_location, COMPONENT_REF,
2429 TREE_TYPE (comp->backend_decl), caf_decl,
2430 comp->backend_decl, NULL_TREE);
2431 if (comp->ts.type == BT_CLASS)
2432 {
2433 caf_decl = gfc_class_data_get (caf_decl);
2434 if (CLASS_DATA (comp)->attr.codimension)
2435 {
2436 found = true;
2437 break;
2438 }
2439 }
2440 if (comp->attr.codimension)
2441 {
2442 found = true;
2443 break;
2444 }
2445 }
2446 gcc_assert (found && caf_decl);
2447 return caf_decl;
2448 }
2449
2450
2451 /* Obtain the Coarray token - and optionally also the offset. */
2452
2453 void
2454 gfc_get_caf_token_offset (gfc_se *se, tree *token, tree *offset, tree caf_decl,
2455 tree se_expr, gfc_expr *expr)
2456 {
2457 tree tmp;
2458
2459 gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
2460
2461 /* Coarray token. */
2462 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
2463 *token = gfc_conv_descriptor_token (caf_decl);
2464 else if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
2465 && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
2466 *token = GFC_DECL_TOKEN (caf_decl);
2467 else
2468 {
2469 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (caf_decl))
2470 && GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl)) != NULL_TREE);
2471 *token = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl));
2472 }
2473
2474 if (offset == NULL)
2475 return;
2476
2477 /* Offset between the coarray base address and the address wanted. */
2478 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl))
2479 && (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_ALLOCATABLE
2480 || GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_POINTER))
2481 *offset = build_int_cst (gfc_array_index_type, 0);
2482 else if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
2483 && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
2484 *offset = GFC_DECL_CAF_OFFSET (caf_decl);
2485 else if (GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl)) != NULL_TREE)
2486 *offset = GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl));
2487 else
2488 *offset = build_int_cst (gfc_array_index_type, 0);
2489
2490 if (POINTER_TYPE_P (TREE_TYPE (se_expr))
2491 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se_expr))))
2492 {
2493 tmp = build_fold_indirect_ref_loc (input_location, se_expr);
2494 tmp = gfc_conv_descriptor_data_get (tmp);
2495 }
2496 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se_expr)))
2497 tmp = gfc_conv_descriptor_data_get (se_expr);
2498 else
2499 {
2500 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se_expr)));
2501 tmp = se_expr;
2502 }
2503
2504 *offset = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
2505 *offset, fold_convert (gfc_array_index_type, tmp));
2506
2507 if (expr->symtree->n.sym->ts.type == BT_DERIVED
2508 && expr->symtree->n.sym->attr.codimension
2509 && expr->symtree->n.sym->ts.u.derived->attr.alloc_comp)
2510 {
2511 gfc_expr *base_expr = gfc_copy_expr (expr);
2512 gfc_ref *ref = base_expr->ref;
2513 gfc_se base_se;
2514
2515 // Iterate through the refs until the last one.
2516 while (ref->next)
2517 ref = ref->next;
2518
2519 if (ref->type == REF_ARRAY
2520 && ref->u.ar.type != AR_FULL)
2521 {
2522 const int ranksum = ref->u.ar.dimen + ref->u.ar.codimen;
2523 int i;
2524 for (i = 0; i < ranksum; ++i)
2525 {
2526 ref->u.ar.start[i] = NULL;
2527 ref->u.ar.end[i] = NULL;
2528 }
2529 ref->u.ar.type = AR_FULL;
2530 }
2531 gfc_init_se (&base_se, NULL);
2532 if (gfc_caf_attr (base_expr).dimension)
2533 {
2534 gfc_conv_expr_descriptor (&base_se, base_expr);
2535 tmp = gfc_conv_descriptor_data_get (base_se.expr);
2536 }
2537 else
2538 {
2539 gfc_conv_expr (&base_se, base_expr);
2540 tmp = base_se.expr;
2541 }
2542
2543 gfc_free_expr (base_expr);
2544 gfc_add_block_to_block (&se->pre, &base_se.pre);
2545 gfc_add_block_to_block (&se->post, &base_se.post);
2546 }
2547 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
2548 tmp = gfc_conv_descriptor_data_get (caf_decl);
2549 else if (INDIRECT_REF_P (caf_decl))
2550 tmp = TREE_OPERAND (caf_decl, 0);
2551 else
2552 {
2553 gcc_assert (POINTER_TYPE_P (TREE_TYPE (caf_decl)));
2554 tmp = caf_decl;
2555 }
2556
2557 *offset = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2558 fold_convert (gfc_array_index_type, *offset),
2559 fold_convert (gfc_array_index_type, tmp));
2560 }
2561
2562
2563 /* Convert the coindex of a coarray into an image index; the result is
2564 image_num = (idx(1)-lcobound(1)+1) + (idx(2)-lcobound(2))*extent(1)
2565 + (idx(3)-lcobound(3))*extend(1)*extent(2) + ... */
2566
2567 tree
2568 gfc_caf_get_image_index (stmtblock_t *block, gfc_expr *e, tree desc)
2569 {
2570 gfc_ref *ref;
2571 tree lbound, ubound, extent, tmp, img_idx;
2572 gfc_se se;
2573 int i;
2574
2575 for (ref = e->ref; ref; ref = ref->next)
2576 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
2577 break;
2578 gcc_assert (ref != NULL);
2579
2580 if (ref->u.ar.dimen_type[ref->u.ar.dimen] == DIMEN_THIS_IMAGE)
2581 {
2582 return build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
2583 integer_zero_node);
2584 }
2585
2586 img_idx = build_zero_cst (gfc_array_index_type);
2587 extent = build_one_cst (gfc_array_index_type);
2588 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
2589 for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
2590 {
2591 gfc_init_se (&se, NULL);
2592 gfc_conv_expr_type (&se, ref->u.ar.start[i], gfc_array_index_type);
2593 gfc_add_block_to_block (block, &se.pre);
2594 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
2595 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2596 TREE_TYPE (lbound), se.expr, lbound);
2597 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
2598 extent, tmp);
2599 img_idx = fold_build2_loc (input_location, PLUS_EXPR,
2600 TREE_TYPE (tmp), img_idx, tmp);
2601 if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
2602 {
2603 ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
2604 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
2605 extent = fold_build2_loc (input_location, MULT_EXPR,
2606 TREE_TYPE (tmp), extent, tmp);
2607 }
2608 }
2609 else
2610 for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
2611 {
2612 gfc_init_se (&se, NULL);
2613 gfc_conv_expr_type (&se, ref->u.ar.start[i], gfc_array_index_type);
2614 gfc_add_block_to_block (block, &se.pre);
2615 lbound = GFC_TYPE_ARRAY_LBOUND (TREE_TYPE (desc), i);
2616 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2617 TREE_TYPE (lbound), se.expr, lbound);
2618 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
2619 extent, tmp);
2620 img_idx = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
2621 img_idx, tmp);
2622 if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
2623 {
2624 ubound = GFC_TYPE_ARRAY_UBOUND (TREE_TYPE (desc), i);
2625 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2626 TREE_TYPE (ubound), ubound, lbound);
2627 tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
2628 tmp, build_one_cst (TREE_TYPE (tmp)));
2629 extent = fold_build2_loc (input_location, MULT_EXPR,
2630 TREE_TYPE (tmp), extent, tmp);
2631 }
2632 }
2633 img_idx = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (img_idx),
2634 img_idx, build_one_cst (TREE_TYPE (img_idx)));
2635 return fold_convert (integer_type_node, img_idx);
2636 }
2637
2638
2639 /* For each character array constructor subexpression without a ts.u.cl->length,
2640 replace it by its first element (if there aren't any elements, the length
2641 should already be set to zero). */
2642
2643 static void
2644 flatten_array_ctors_without_strlen (gfc_expr* e)
2645 {
2646 gfc_actual_arglist* arg;
2647 gfc_constructor* c;
2648
2649 if (!e)
2650 return;
2651
2652 switch (e->expr_type)
2653 {
2654
2655 case EXPR_OP:
2656 flatten_array_ctors_without_strlen (e->value.op.op1);
2657 flatten_array_ctors_without_strlen (e->value.op.op2);
2658 break;
2659
2660 case EXPR_COMPCALL:
2661 /* TODO: Implement as with EXPR_FUNCTION when needed. */
2662 gcc_unreachable ();
2663
2664 case EXPR_FUNCTION:
2665 for (arg = e->value.function.actual; arg; arg = arg->next)
2666 flatten_array_ctors_without_strlen (arg->expr);
2667 break;
2668
2669 case EXPR_ARRAY:
2670
2671 /* We've found what we're looking for. */
2672 if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
2673 {
2674 gfc_constructor *c;
2675 gfc_expr* new_expr;
2676
2677 gcc_assert (e->value.constructor);
2678
2679 c = gfc_constructor_first (e->value.constructor);
2680 new_expr = c->expr;
2681 c->expr = NULL;
2682
2683 flatten_array_ctors_without_strlen (new_expr);
2684 gfc_replace_expr (e, new_expr);
2685 break;
2686 }
2687
2688 /* Otherwise, fall through to handle constructor elements. */
2689 gcc_fallthrough ();
2690 case EXPR_STRUCTURE:
2691 for (c = gfc_constructor_first (e->value.constructor);
2692 c; c = gfc_constructor_next (c))
2693 flatten_array_ctors_without_strlen (c->expr);
2694 break;
2695
2696 default:
2697 break;
2698
2699 }
2700 }
2701
2702
2703 /* Generate code to initialize a string length variable. Returns the
2704 value. For array constructors, cl->length might be NULL and in this case,
2705 the first element of the constructor is needed. expr is the original
2706 expression so we can access it but can be NULL if this is not needed. */
2707
2708 void
2709 gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
2710 {
2711 gfc_se se;
2712
2713 gfc_init_se (&se, NULL);
2714
2715 if (!cl->length && cl->backend_decl && VAR_P (cl->backend_decl))
2716 return;
2717
2718 /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
2719 "flatten" array constructors by taking their first element; all elements
2720 should be the same length or a cl->length should be present. */
2721 if (!cl->length)
2722 {
2723 gfc_expr* expr_flat;
2724 if (!expr)
2725 return;
2726 expr_flat = gfc_copy_expr (expr);
2727 flatten_array_ctors_without_strlen (expr_flat);
2728 gfc_resolve_expr (expr_flat);
2729 if (expr_flat->rank)
2730 gfc_conv_expr_descriptor (&se, expr_flat);
2731 else
2732 gfc_conv_expr (&se, expr_flat);
2733 if (expr_flat->expr_type != EXPR_VARIABLE)
2734 gfc_add_block_to_block (pblock, &se.pre);
2735 se.expr = convert (gfc_charlen_type_node, se.string_length);
2736 gfc_add_block_to_block (pblock, &se.post);
2737 gfc_free_expr (expr_flat);
2738 }
2739 else
2740 {
2741 /* Convert cl->length. */
2742 gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
2743 se.expr = fold_build2_loc (input_location, MAX_EXPR,
2744 gfc_charlen_type_node, se.expr,
2745 build_zero_cst (TREE_TYPE (se.expr)));
2746 gfc_add_block_to_block (pblock, &se.pre);
2747 }
2748
2749 if (cl->backend_decl && VAR_P (cl->backend_decl))
2750 gfc_add_modify (pblock, cl->backend_decl, se.expr);
2751 else
2752 cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
2753 }
2754
2755
2756 static void
2757 gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
2758 const char *name, locus *where)
2759 {
2760 tree tmp;
2761 tree type;
2762 tree fault;
2763 gfc_se start;
2764 gfc_se end;
2765 char *msg;
2766 mpz_t length;
2767
2768 type = gfc_get_character_type (kind, ref->u.ss.length);
2769 type = build_pointer_type (type);
2770
2771 gfc_init_se (&start, se);
2772 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
2773 gfc_add_block_to_block (&se->pre, &start.pre);
2774
2775 if (integer_onep (start.expr))
2776 gfc_conv_string_parameter (se);
2777 else
2778 {
2779 tmp = start.expr;
2780 STRIP_NOPS (tmp);
2781 /* Avoid multiple evaluation of substring start. */
2782 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
2783 start.expr = gfc_evaluate_now (start.expr, &se->pre);
2784
2785 /* Change the start of the string. */
2786 if (((TREE_CODE (TREE_TYPE (se->expr)) == ARRAY_TYPE
2787 || TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE)
2788 && TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
2789 || (POINTER_TYPE_P (TREE_TYPE (se->expr))
2790 && TREE_CODE (TREE_TYPE (TREE_TYPE (se->expr))) != ARRAY_TYPE))
2791 tmp = se->expr;
2792 else
2793 tmp = build_fold_indirect_ref_loc (input_location,
2794 se->expr);
2795 /* For BIND(C), a BT_CHARACTER is not an ARRAY_TYPE. */
2796 if (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE)
2797 {
2798 tmp = gfc_build_array_ref (tmp, start.expr, NULL_TREE, true);
2799 se->expr = gfc_build_addr_expr (type, tmp);
2800 }
2801 else if (POINTER_TYPE_P (TREE_TYPE (tmp)))
2802 {
2803 tree diff;
2804 diff = fold_build2 (MINUS_EXPR, size_type_node, start.expr,
2805 build_one_cst (size_type_node));
2806 se->expr
2807 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (tmp), tmp, diff);
2808 }
2809 }
2810
2811 /* Length = end + 1 - start. */
2812 gfc_init_se (&end, se);
2813 if (ref->u.ss.end == NULL)
2814 end.expr = se->string_length;
2815 else
2816 {
2817 gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
2818 gfc_add_block_to_block (&se->pre, &end.pre);
2819 }
2820 tmp = end.expr;
2821 STRIP_NOPS (tmp);
2822 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
2823 end.expr = gfc_evaluate_now (end.expr, &se->pre);
2824
2825 if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2826 && !gfc_contains_implied_index_p (ref->u.ss.start)
2827 && !gfc_contains_implied_index_p (ref->u.ss.end))
2828 {
2829 tree nonempty = fold_build2_loc (input_location, LE_EXPR,
2830 logical_type_node, start.expr,
2831 end.expr);
2832
2833 /* Check lower bound. */
2834 fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2835 start.expr,
2836 build_one_cst (TREE_TYPE (start.expr)));
2837 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2838 logical_type_node, nonempty, fault);
2839 if (name)
2840 msg = xasprintf ("Substring out of bounds: lower bound (%%ld) of '%s' "
2841 "is less than one", name);
2842 else
2843 msg = xasprintf ("Substring out of bounds: lower bound (%%ld) "
2844 "is less than one");
2845 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
2846 fold_convert (long_integer_type_node,
2847 start.expr));
2848 free (msg);
2849
2850 /* Check upper bound. */
2851 fault = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2852 end.expr, se->string_length);
2853 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2854 logical_type_node, nonempty, fault);
2855 if (name)
2856 msg = xasprintf ("Substring out of bounds: upper bound (%%ld) of '%s' "
2857 "exceeds string length (%%ld)", name);
2858 else
2859 msg = xasprintf ("Substring out of bounds: upper bound (%%ld) "
2860 "exceeds string length (%%ld)");
2861 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
2862 fold_convert (long_integer_type_node, end.expr),
2863 fold_convert (long_integer_type_node,
2864 se->string_length));
2865 free (msg);
2866 }
2867
2868 /* Try to calculate the length from the start and end expressions. */
2869 if (ref->u.ss.end
2870 && gfc_dep_difference (ref->u.ss.end, ref->u.ss.start, &length))
2871 {
2872 HOST_WIDE_INT i_len;
2873
2874 i_len = gfc_mpz_get_hwi (length) + 1;
2875 if (i_len < 0)
2876 i_len = 0;
2877
2878 tmp = build_int_cst (gfc_charlen_type_node, i_len);
2879 mpz_clear (length); /* Was initialized by gfc_dep_difference. */
2880 }
2881 else
2882 {
2883 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
2884 fold_convert (gfc_charlen_type_node, end.expr),
2885 fold_convert (gfc_charlen_type_node, start.expr));
2886 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
2887 build_int_cst (gfc_charlen_type_node, 1), tmp);
2888 tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
2889 tmp, build_int_cst (gfc_charlen_type_node, 0));
2890 }
2891
2892 se->string_length = tmp;
2893 }
2894
2895
2896 /* Convert a derived type component reference. */
2897
2898 void
2899 gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
2900 {
2901 gfc_component *c;
2902 tree tmp;
2903 tree decl;
2904 tree field;
2905 tree context;
2906
2907 c = ref->u.c.component;
2908
2909 if (c->backend_decl == NULL_TREE
2910 && ref->u.c.sym != NULL)
2911 gfc_get_derived_type (ref->u.c.sym);
2912
2913 field = c->backend_decl;
2914 gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
2915 decl = se->expr;
2916 context = DECL_FIELD_CONTEXT (field);
2917
2918 /* Components can correspond to fields of different containing
2919 types, as components are created without context, whereas
2920 a concrete use of a component has the type of decl as context.
2921 So, if the type doesn't match, we search the corresponding
2922 FIELD_DECL in the parent type. To not waste too much time
2923 we cache this result in norestrict_decl.
2924 On the other hand, if the context is a UNION or a MAP (a
2925 RECORD_TYPE within a UNION_TYPE) always use the given FIELD_DECL. */
2926
2927 if (context != TREE_TYPE (decl)
2928 && !( TREE_CODE (TREE_TYPE (field)) == UNION_TYPE /* Field is union */
2929 || TREE_CODE (context) == UNION_TYPE)) /* Field is map */
2930 {
2931 tree f2 = c->norestrict_decl;
2932 if (!f2 || DECL_FIELD_CONTEXT (f2) != TREE_TYPE (decl))
2933 for (f2 = TYPE_FIELDS (TREE_TYPE (decl)); f2; f2 = DECL_CHAIN (f2))
2934 if (TREE_CODE (f2) == FIELD_DECL
2935 && DECL_NAME (f2) == DECL_NAME (field))
2936 break;
2937 gcc_assert (f2);
2938 c->norestrict_decl = f2;
2939 field = f2;
2940 }
2941
2942 if (ref->u.c.sym && ref->u.c.sym->ts.type == BT_CLASS
2943 && strcmp ("_data", c->name) == 0)
2944 {
2945 /* Found a ref to the _data component. Store the associated ref to
2946 the vptr in se->class_vptr. */
2947 se->class_vptr = gfc_class_vptr_get (decl);
2948 }
2949 else
2950 se->class_vptr = NULL_TREE;
2951
2952 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
2953 decl, field, NULL_TREE);
2954
2955 se->expr = tmp;
2956
2957 /* Allocatable deferred char arrays are to be handled by the gfc_deferred_
2958 strlen () conditional below. */
2959 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
2960 && !c->ts.deferred
2961 && !c->attr.pdt_string)
2962 {
2963 tmp = c->ts.u.cl->backend_decl;
2964 /* Components must always be constant length. */
2965 gcc_assert (tmp && INTEGER_CST_P (tmp));
2966 se->string_length = tmp;
2967 }
2968
2969 if (gfc_deferred_strlen (c, &field))
2970 {
2971 tmp = fold_build3_loc (input_location, COMPONENT_REF,
2972 TREE_TYPE (field),
2973 decl, field, NULL_TREE);
2974 se->string_length = tmp;
2975 }
2976
2977 if (((c->attr.pointer || c->attr.allocatable)
2978 && (!c->attr.dimension && !c->attr.codimension)
2979 && c->ts.type != BT_CHARACTER)
2980 || c->attr.proc_pointer)
2981 se->expr = build_fold_indirect_ref_loc (input_location,
2982 se->expr);
2983 }
2984
2985
2986 /* This function deals with component references to components of the
2987 parent type for derived type extensions. */
2988 void
2989 conv_parent_component_references (gfc_se * se, gfc_ref * ref)
2990 {
2991 gfc_component *c;
2992 gfc_component *cmp;
2993 gfc_symbol *dt;
2994 gfc_ref parent;
2995
2996 dt = ref->u.c.sym;
2997 c = ref->u.c.component;
2998
2999 /* Return if the component is in this type, i.e. not in the parent type. */
3000 for (cmp = dt->components; cmp; cmp = cmp->next)
3001 if (c == cmp)
3002 return;
3003
3004 /* Build a gfc_ref to recursively call gfc_conv_component_ref. */
3005 parent.type = REF_COMPONENT;
3006 parent.next = NULL;
3007 parent.u.c.sym = dt;
3008 parent.u.c.component = dt->components;
3009
3010 if (dt->backend_decl == NULL)
3011 gfc_get_derived_type (dt);
3012
3013 /* Build the reference and call self. */
3014 gfc_conv_component_ref (se, &parent);
3015 parent.u.c.sym = dt->components->ts.u.derived;
3016 parent.u.c.component = c;
3017 conv_parent_component_references (se, &parent);
3018 }
3019
3020
3021 static void
3022 conv_inquiry (gfc_se * se, gfc_ref * ref, gfc_expr *expr, gfc_typespec *ts)
3023 {
3024 tree res = se->expr;
3025
3026 switch (ref->u.i)
3027 {
3028 case INQUIRY_RE:
3029 res = fold_build1_loc (input_location, REALPART_EXPR,
3030 TREE_TYPE (TREE_TYPE (res)), res);
3031 break;
3032
3033 case INQUIRY_IM:
3034 res = fold_build1_loc (input_location, IMAGPART_EXPR,
3035 TREE_TYPE (TREE_TYPE (res)), res);
3036 break;
3037
3038 case INQUIRY_KIND:
3039 res = build_int_cst (gfc_typenode_for_spec (&expr->ts),
3040 ts->kind);
3041 se->string_length = NULL_TREE;
3042 break;
3043
3044 case INQUIRY_LEN:
3045 res = fold_convert (gfc_typenode_for_spec (&expr->ts),
3046 se->string_length);
3047 se->string_length = NULL_TREE;
3048 break;
3049
3050 default:
3051 gcc_unreachable ();
3052 }
3053 se->expr = res;
3054 }
3055
3056 /* Dereference VAR where needed if it is a pointer, reference, etc.
3057 according to Fortran semantics. */
3058
3059 tree
3060 gfc_maybe_dereference_var (gfc_symbol *sym, tree var, bool descriptor_only_p,
3061 bool is_classarray)
3062 {
3063 if (!POINTER_TYPE_P (TREE_TYPE (var)))
3064 return var;
3065 if (is_CFI_desc (sym, NULL))
3066 return build_fold_indirect_ref_loc (input_location, var);
3067
3068 /* Characters are entirely different from other types, they are treated
3069 separately. */
3070 if (sym->ts.type == BT_CHARACTER)
3071 {
3072 /* Dereference character pointer dummy arguments
3073 or results. */
3074 if ((sym->attr.pointer || sym->attr.allocatable
3075 || (sym->as && sym->as->type == AS_ASSUMED_RANK))
3076 && (sym->attr.dummy
3077 || sym->attr.function
3078 || sym->attr.result))
3079 var = build_fold_indirect_ref_loc (input_location, var);
3080 }
3081 else if (!sym->attr.value)
3082 {
3083 /* Dereference temporaries for class array dummy arguments. */
3084 if (sym->attr.dummy && is_classarray
3085 && GFC_ARRAY_TYPE_P (TREE_TYPE (var)))
3086 {
3087 if (!descriptor_only_p)
3088 var = GFC_DECL_SAVED_DESCRIPTOR (var);
3089
3090 var = build_fold_indirect_ref_loc (input_location, var);
3091 }
3092
3093 /* Dereference non-character scalar dummy arguments. */
3094 if (sym->attr.dummy && !sym->attr.dimension
3095 && !(sym->attr.codimension && sym->attr.allocatable)
3096 && (sym->ts.type != BT_CLASS
3097 || (!CLASS_DATA (sym)->attr.dimension
3098 && !(CLASS_DATA (sym)->attr.codimension
3099 && CLASS_DATA (sym)->attr.allocatable))))
3100 var = build_fold_indirect_ref_loc (input_location, var);
3101
3102 /* Dereference scalar hidden result. */
3103 if (flag_f2c && sym->ts.type == BT_COMPLEX
3104 && (sym->attr.function || sym->attr.result)
3105 && !sym->attr.dimension && !sym->attr.pointer
3106 && !sym->attr.always_explicit)
3107 var = build_fold_indirect_ref_loc (input_location, var);
3108
3109 /* Dereference non-character, non-class pointer variables.
3110 These must be dummies, results, or scalars. */
3111 if (!is_classarray
3112 && (sym->attr.pointer || sym->attr.allocatable
3113 || gfc_is_associate_pointer (sym)
3114 || (sym->as && sym->as->type == AS_ASSUMED_RANK))
3115 && (sym->attr.dummy
3116 || sym->attr.function
3117 || sym->attr.result
3118 || (!sym->attr.dimension
3119 && (!sym->attr.codimension || !sym->attr.allocatable))))
3120 var = build_fold_indirect_ref_loc (input_location, var);
3121 /* Now treat the class array pointer variables accordingly. */
3122 else if (sym->ts.type == BT_CLASS
3123 && sym->attr.dummy
3124 && (CLASS_DATA (sym)->attr.dimension
3125 || CLASS_DATA (sym)->attr.codimension)
3126 && ((CLASS_DATA (sym)->as
3127 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
3128 || CLASS_DATA (sym)->attr.allocatable
3129 || CLASS_DATA (sym)->attr.class_pointer))
3130 var = build_fold_indirect_ref_loc (input_location, var);
3131 /* And the case where a non-dummy, non-result, non-function,
3132 non-allocable and non-pointer classarray is present. This case was
3133 previously covered by the first if, but with introducing the
3134 condition !is_classarray there, that case has to be covered
3135 explicitly. */
3136 else if (sym->ts.type == BT_CLASS
3137 && !sym->attr.dummy
3138 && !sym->attr.function
3139 && !sym->attr.result
3140 && (CLASS_DATA (sym)->attr.dimension
3141 || CLASS_DATA (sym)->attr.codimension)
3142 && (sym->assoc
3143 || !CLASS_DATA (sym)->attr.allocatable)
3144 && !CLASS_DATA (sym)->attr.class_pointer)
3145 var = build_fold_indirect_ref_loc (input_location, var);
3146 }
3147
3148 return var;
3149 }
3150
3151 /* Return the contents of a variable. Also handles reference/pointer
3152 variables (all Fortran pointer references are implicit). */
3153
3154 static void
3155 gfc_conv_variable (gfc_se * se, gfc_expr * expr)
3156 {
3157 gfc_ss *ss;
3158 gfc_ref *ref;
3159 gfc_symbol *sym;
3160 tree parent_decl = NULL_TREE;
3161 int parent_flag;
3162 bool return_value;
3163 bool alternate_entry;
3164 bool entry_master;
3165 bool is_classarray;
3166 bool first_time = true;
3167
3168 sym = expr->symtree->n.sym;
3169 is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
3170 ss = se->ss;
3171 if (ss != NULL)
3172 {
3173 gfc_ss_info *ss_info = ss->info;
3174
3175 /* Check that something hasn't gone horribly wrong. */
3176 gcc_assert (ss != gfc_ss_terminator);
3177 gcc_assert (ss_info->expr == expr);
3178
3179 /* A scalarized term. We already know the descriptor. */
3180 se->expr = ss_info->data.array.descriptor;
3181 se->string_length = ss_info->string_length;
3182 ref = ss_info->data.array.ref;
3183 if (ref)
3184 gcc_assert (ref->type == REF_ARRAY
3185 && ref->u.ar.type != AR_ELEMENT);
3186 else
3187 gfc_conv_tmp_array_ref (se);
3188 }
3189 else
3190 {
3191 tree se_expr = NULL_TREE;
3192
3193 se->expr = gfc_get_symbol_decl (sym);
3194
3195 /* Deal with references to a parent results or entries by storing
3196 the current_function_decl and moving to the parent_decl. */
3197 return_value = sym->attr.function && sym->result == sym;
3198 alternate_entry = sym->attr.function && sym->attr.entry
3199 && sym->result == sym;
3200 entry_master = sym->attr.result
3201 && sym->ns->proc_name->attr.entry_master
3202 && !gfc_return_by_reference (sym->ns->proc_name);
3203 if (current_function_decl)
3204 parent_decl = DECL_CONTEXT (current_function_decl);
3205
3206 if ((se->expr == parent_decl && return_value)
3207 || (sym->ns && sym->ns->proc_name
3208 && parent_decl
3209 && sym->ns->proc_name->backend_decl == parent_decl
3210 && (alternate_entry || entry_master)))
3211 parent_flag = 1;
3212 else
3213 parent_flag = 0;
3214
3215 /* Special case for assigning the return value of a function.
3216 Self recursive functions must have an explicit return value. */
3217 if (return_value && (se->expr == current_function_decl || parent_flag))
3218 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
3219
3220 /* Similarly for alternate entry points. */
3221 else if (alternate_entry
3222 && (sym->ns->proc_name->backend_decl == current_function_decl
3223 || parent_flag))
3224 {
3225 gfc_entry_list *el = NULL;
3226
3227 for (el = sym->ns->entries; el; el = el->next)
3228 if (sym == el->sym)
3229 {
3230 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
3231 break;
3232 }
3233 }
3234
3235 else if (entry_master
3236 && (sym->ns->proc_name->backend_decl == current_function_decl
3237 || parent_flag))
3238 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
3239
3240 if (se_expr)
3241 se->expr = se_expr;
3242
3243 /* Procedure actual arguments. Look out for temporary variables
3244 with the same attributes as function values. */
3245 else if (!sym->attr.temporary
3246 && sym->attr.flavor == FL_PROCEDURE
3247 && se->expr != current_function_decl)
3248 {
3249 if (!sym->attr.dummy && !sym->attr.proc_pointer)
3250 {
3251 gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
3252 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
3253 }
3254 return;
3255 }
3256
3257 if (sym->ts.type == BT_CLASS
3258 && sym->attr.class_ok
3259 && sym->ts.u.derived->attr.is_class)
3260 {
3261 if (is_classarray && DECL_LANG_SPECIFIC (se->expr)
3262 && GFC_DECL_SAVED_DESCRIPTOR (se->expr))
3263 se->class_container = GFC_DECL_SAVED_DESCRIPTOR (se->expr);
3264 else
3265 se->class_container = se->expr;
3266 }
3267
3268 /* Dereference the expression, where needed. */
3269 if (se->class_container && CLASS_DATA (sym)->attr.codimension
3270 && !CLASS_DATA (sym)->attr.dimension)
3271 se->expr
3272 = gfc_maybe_dereference_var (sym, se->class_container,
3273 se->descriptor_only, is_classarray);
3274 else
3275 se->expr
3276 = gfc_maybe_dereference_var (sym, se->expr, se->descriptor_only,
3277 is_classarray);
3278
3279 ref = expr->ref;
3280 }
3281
3282 /* For character variables, also get the length. */
3283 if (sym->ts.type == BT_CHARACTER)
3284 {
3285 /* If the character length of an entry isn't set, get the length from
3286 the master function instead. */
3287 if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
3288 se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
3289 else
3290 se->string_length = sym->ts.u.cl->backend_decl;
3291 gcc_assert (se->string_length);
3292
3293 /* For coarray strings return the pointer to the data and not the
3294 descriptor. */
3295 if (sym->attr.codimension && sym->attr.associate_var
3296 && !se->descriptor_only
3297 && TREE_CODE (TREE_TYPE (se->expr)) != ARRAY_TYPE)
3298 se->expr = gfc_conv_descriptor_data_get (se->expr);
3299 }
3300
3301 /* F202Y: Runtime warning that an assumed rank object is associated
3302 with an assumed size object. */
3303 if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
3304 && (gfc_option.allow_std & GFC_STD_F202Y)
3305 && expr->rank == -1 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr)))
3306 {
3307 tree dim, lower, upper, cond;
3308 char *msg;
3309
3310 dim = fold_convert (signed_char_type_node,
3311 gfc_conv_descriptor_rank (se->expr));
3312 dim = fold_build2_loc (input_location, MINUS_EXPR, signed_char_type_node,
3313 dim, build_int_cst (signed_char_type_node, 1));
3314 lower = gfc_conv_descriptor_lbound_get (se->expr, dim);
3315 upper = gfc_conv_descriptor_ubound_get (se->expr, dim);
3316
3317 msg = xasprintf ("Assumed rank object %s is associated with an "
3318 "assumed size object", sym->name);
3319 cond = fold_build2_loc (input_location, LT_EXPR,
3320 logical_type_node, upper, lower);
3321 gfc_trans_runtime_check (false, true, cond, &se->pre,
3322 &gfc_current_locus, msg);
3323 free (msg);
3324 }
3325
3326 /* Some expressions leak through that haven't been fixed up. */
3327 if (IS_INFERRED_TYPE (expr) && expr->ref)
3328 gfc_fixup_inferred_type_refs (expr);
3329
3330 gfc_typespec *ts = &sym->ts;
3331 while (ref)
3332 {
3333 switch (ref->type)
3334 {
3335 case REF_ARRAY:
3336 /* Return the descriptor if that's what we want and this is an array
3337 section reference. */
3338 if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
3339 return;
3340 /* TODO: Pointers to single elements of array sections, eg elemental subs. */
3341 /* Return the descriptor for array pointers and allocations. */
3342 if (se->want_pointer
3343 && ref->next == NULL && (se->descriptor_only))
3344 return;
3345
3346 gfc_conv_array_ref (se, &ref->u.ar, expr, &expr->where);
3347 /* Return a pointer to an element. */
3348 break;
3349
3350 case REF_COMPONENT:
3351 ts = &ref->u.c.component->ts;
3352 if (first_time && IS_CLASS_ARRAY (sym) && sym->attr.dummy
3353 && se->descriptor_only && !CLASS_DATA (sym)->attr.allocatable
3354 && !CLASS_DATA (sym)->attr.class_pointer && CLASS_DATA (sym)->as
3355 && CLASS_DATA (sym)->as->type != AS_ASSUMED_RANK
3356 && strcmp ("_data", ref->u.c.component->name) == 0)
3357 /* Skip the first ref of a _data component, because for class
3358 arrays that one is already done by introducing a temporary
3359 array descriptor. */
3360 break;
3361
3362 if (ref->u.c.sym->attr.extension)
3363 conv_parent_component_references (se, ref);
3364
3365 gfc_conv_component_ref (se, ref);
3366
3367 if (ref->u.c.component->ts.type == BT_CLASS
3368 && ref->u.c.component->attr.class_ok
3369 && ref->u.c.component->ts.u.derived->attr.is_class)
3370 se->class_container = se->expr;
3371 else if (!(ref->u.c.sym->attr.flavor == FL_DERIVED
3372 && ref->u.c.sym->attr.is_class))
3373 se->class_container = NULL_TREE;
3374
3375 if (!ref->next && ref->u.c.sym->attr.codimension
3376 && se->want_pointer && se->descriptor_only)
3377 return;
3378
3379 break;
3380
3381 case REF_SUBSTRING:
3382 gfc_conv_substring (se, ref, expr->ts.kind,
3383 expr->symtree->name, &expr->where);
3384 break;
3385
3386 case REF_INQUIRY:
3387 conv_inquiry (se, ref, expr, ts);
3388 break;
3389
3390 default:
3391 gcc_unreachable ();
3392 break;
3393 }
3394 first_time = false;
3395 ref = ref->next;
3396 }
3397 /* Pointer assignment, allocation or pass by reference. Arrays are handled
3398 separately. */
3399 if (se->want_pointer)
3400 {
3401 if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr))
3402 gfc_conv_string_parameter (se);
3403 else
3404 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
3405 }
3406 }
3407
3408
3409 /* Unary ops are easy... Or they would be if ! was a valid op. */
3410
3411 static void
3412 gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
3413 {
3414 gfc_se operand;
3415 tree type;
3416
3417 gcc_assert (expr->ts.type != BT_CHARACTER);
3418 /* Initialize the operand. */
3419 gfc_init_se (&operand, se);
3420 gfc_conv_expr_val (&operand, expr->value.op.op1);
3421 gfc_add_block_to_block (&se->pre, &operand.pre);
3422
3423 type = gfc_typenode_for_spec (&expr->ts);
3424
3425 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
3426 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
3427 All other unary operators have an equivalent GIMPLE unary operator. */
3428 if (code == TRUTH_NOT_EXPR)
3429 se->expr = fold_build2_loc (input_location, EQ_EXPR, type, operand.expr,
3430 build_int_cst (type, 0));
3431 else
3432 se->expr = fold_build1_loc (input_location, code, type, operand.expr);
3433
3434 }
3435
3436 /* Expand power operator to optimal multiplications when a value is raised
3437 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
3438 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
3439 Programming", 3rd Edition, 1998. */
3440
3441 /* This code is mostly duplicated from expand_powi in the backend.
3442 We establish the "optimal power tree" lookup table with the defined size.
3443 The items in the table are the exponents used to calculate the index
3444 exponents. Any integer n less than the value can get an "addition chain",
3445 with the first node being one. */
3446 #define POWI_TABLE_SIZE 256
3447
3448 /* The table is from builtins.cc. */
3449 static const unsigned char powi_table[POWI_TABLE_SIZE] =
3450 {
3451 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
3452 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
3453 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
3454 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
3455 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
3456 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
3457 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
3458 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
3459 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
3460 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
3461 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
3462 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
3463 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
3464 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
3465 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
3466 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
3467 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
3468 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
3469 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
3470 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
3471 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
3472 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
3473 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
3474 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
3475 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
3476 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
3477 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
3478 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
3479 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
3480 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
3481 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
3482 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
3483 };
3484
3485 /* If n is larger than lookup table's max index, we use the "window
3486 method". */
3487 #define POWI_WINDOW_SIZE 3
3488
3489 /* Recursive function to expand the power operator. The temporary
3490 values are put in tmpvar. The function returns tmpvar[1] ** n. */
3491 static tree
3492 gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
3493 {
3494 tree op0;
3495 tree op1;
3496 tree tmp;
3497 int digit;
3498
3499 if (n < POWI_TABLE_SIZE)
3500 {
3501 if (tmpvar[n])
3502 return tmpvar[n];
3503
3504 op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
3505 op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
3506 }
3507 else if (n & 1)
3508 {
3509 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
3510 op0 = gfc_conv_powi (se, n - digit, tmpvar);
3511 op1 = gfc_conv_powi (se, digit, tmpvar);
3512 }
3513 else
3514 {
3515 op0 = gfc_conv_powi (se, n >> 1, tmpvar);
3516 op1 = op0;
3517 }
3518
3519 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
3520 tmp = gfc_evaluate_now (tmp, &se->pre);
3521
3522 if (n < POWI_TABLE_SIZE)
3523 tmpvar[n] = tmp;
3524
3525 return tmp;
3526 }
3527
3528
3529 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
3530 return 1. Else return 0 and a call to runtime library functions
3531 will have to be built. */
3532 static int
3533 gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
3534 {
3535 tree cond;
3536 tree tmp;
3537 tree type;
3538 tree vartmp[POWI_TABLE_SIZE];
3539 HOST_WIDE_INT m;
3540 unsigned HOST_WIDE_INT n;
3541 int sgn;
3542 wi::tree_to_wide_ref wrhs = wi::to_wide (rhs);
3543
3544 /* If exponent is too large, we won't expand it anyway, so don't bother
3545 with large integer values. */
3546 if (!wi::fits_shwi_p (wrhs))
3547 return 0;
3548
3549 m = wrhs.to_shwi ();
3550 /* Use the wide_int's routine to reliably get the absolute value on all
3551 platforms. Then convert it to a HOST_WIDE_INT like above. */
3552 n = wi::abs (wrhs).to_shwi ();
3553
3554 type = TREE_TYPE (lhs);
3555 sgn = tree_int_cst_sgn (rhs);
3556
3557 if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
3558 || optimize_size) && (m > 2 || m < -1))
3559 return 0;
3560
3561 /* rhs == 0 */
3562 if (sgn == 0)
3563 {
3564 se->expr = gfc_build_const (type, integer_one_node);
3565 return 1;
3566 }
3567
3568 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
3569 if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
3570 {
3571 tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3572 lhs, build_int_cst (TREE_TYPE (lhs), -1));
3573 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3574 lhs, build_int_cst (TREE_TYPE (lhs), 1));
3575
3576 /* If rhs is even,
3577 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
3578 if ((n & 1) == 0)
3579 {
3580 tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
3581 logical_type_node, tmp, cond);
3582 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
3583 tmp, build_int_cst (type, 1),
3584 build_int_cst (type, 0));
3585 return 1;
3586 }
3587 /* If rhs is odd,
3588 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
3589 tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp,
3590 build_int_cst (type, -1),
3591 build_int_cst (type, 0));
3592 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
3593 cond, build_int_cst (type, 1), tmp);
3594 return 1;
3595 }
3596
3597 memset (vartmp, 0, sizeof (vartmp));
3598 vartmp[1] = lhs;
3599 if (sgn == -1)
3600 {
3601 tmp = gfc_build_const (type, integer_one_node);
3602 vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
3603 vartmp[1]);
3604 }
3605
3606 se->expr = gfc_conv_powi (se, n, vartmp);
3607
3608 return 1;
3609 }
3610
3611 /* Convert lhs**rhs, for constant rhs, when both are unsigned.
3612 Method:
3613 if (rhs == 0) ! Checked here.
3614 return 1;
3615 if (lhs & 1 == 1) ! odd_cnd
3616 {
3617 if (bit_size(rhs) < bit_size(lhs)) ! Checked here.
3618 return lhs ** rhs;
3619
3620 mask = 1 << (bit_size(a) - 1) / 2;
3621 return lhs ** (n & rhs);
3622 }
3623 if (rhs > bit_size(lhs)) ! Checked here.
3624 return 0;
3625
3626 return lhs ** rhs;
3627 */
3628
3629 static int
3630 gfc_conv_cst_uint_power (gfc_se * se, tree lhs, tree rhs)
3631 {
3632 tree type = TREE_TYPE (lhs);
3633 tree tmp, is_odd, odd_branch, even_branch;
3634 unsigned HOST_WIDE_INT lhs_prec, rhs_prec;
3635 wi::tree_to_wide_ref wrhs = wi::to_wide (rhs);
3636 unsigned HOST_WIDE_INT n, n_odd;
3637 tree vartmp_odd[POWI_TABLE_SIZE], vartmp_even[POWI_TABLE_SIZE];
3638
3639 /* Anything ** 0 is one. */
3640 if (integer_zerop (rhs))
3641 {
3642 se->expr = build_int_cst (type, 1);
3643 return 1;
3644 }
3645
3646 if (!wi::fits_uhwi_p (wrhs))
3647 return 0;
3648
3649 n = wrhs.to_uhwi ();
3650
3651 /* tmp = a & 1; . */
3652 tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type,
3653 lhs, build_int_cst (type, 1));
3654 is_odd = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3655 tmp, build_int_cst (type, 1));
3656
3657 lhs_prec = TYPE_PRECISION (type);
3658 rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs));
3659
3660 if (rhs_prec >= lhs_prec && lhs_prec <= HOST_BITS_PER_WIDE_INT)
3661 {
3662 unsigned HOST_WIDE_INT mask = (HOST_WIDE_INT_1U << (lhs_prec - 1)) - 1;
3663 n_odd = n & mask;
3664 }
3665 else
3666 n_odd = n;
3667
3668 memset (vartmp_odd, 0, sizeof (vartmp_odd));
3669 vartmp_odd[0] = build_int_cst (type, 1);
3670 vartmp_odd[1] = lhs;
3671 odd_branch = gfc_conv_powi (se, n_odd, vartmp_odd);
3672 even_branch = NULL_TREE;
3673
3674 if (n > lhs_prec)
3675 even_branch = build_int_cst (type, 0);
3676 else
3677 {
3678 if (n_odd != n)
3679 {
3680 memset (vartmp_even, 0, sizeof (vartmp_even));
3681 vartmp_even[0] = build_int_cst (type, 1);
3682 vartmp_even[1] = lhs;
3683 even_branch = gfc_conv_powi (se, n, vartmp_even);
3684 }
3685 }
3686 if (even_branch != NULL_TREE)
3687 se->expr = fold_build3_loc (input_location, COND_EXPR, type, is_odd,
3688 odd_branch, even_branch);
3689 else
3690 se->expr = odd_branch;
3691
3692 return 1;
3693 }
3694
3695 /* Power op (**). Constant integer exponent and powers of 2 have special
3696 handling. */
3697
3698 static void
3699 gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
3700 {
3701 tree gfc_int4_type_node;
3702 int kind;
3703 int ikind;
3704 int res_ikind_1, res_ikind_2;
3705 gfc_se lse;
3706 gfc_se rse;
3707 tree fndecl = NULL;
3708
3709 gfc_init_se (&lse, se);
3710 gfc_conv_expr_val (&lse, expr->value.op.op1);
3711 lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
3712 gfc_add_block_to_block (&se->pre, &lse.pre);
3713
3714 gfc_init_se (&rse, se);
3715 gfc_conv_expr_val (&rse, expr->value.op.op2);
3716 gfc_add_block_to_block (&se->pre, &rse.pre);
3717
3718 if (expr->value.op.op2->expr_type == EXPR_CONSTANT)
3719 {
3720 if (expr->value.op.op2->ts.type == BT_INTEGER)
3721 {
3722 if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
3723 return;
3724 }
3725 else if (expr->value.op.op2->ts.type == BT_UNSIGNED)
3726 {
3727 if (gfc_conv_cst_uint_power (se, lse.expr, rse.expr))
3728 return;
3729 }
3730 }
3731
3732 if ((expr->value.op.op2->ts.type == BT_INTEGER
3733 || expr->value.op.op2->ts.type == BT_UNSIGNED)
3734 && expr->value.op.op2->expr_type == EXPR_CONSTANT)
3735 if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
3736 return;
3737
3738 if (INTEGER_CST_P (lse.expr)
3739 && TREE_CODE (TREE_TYPE (rse.expr)) == INTEGER_TYPE
3740 && expr->value.op.op2->ts.type == BT_INTEGER)
3741 {
3742 wi::tree_to_wide_ref wlhs = wi::to_wide (lse.expr);
3743 HOST_WIDE_INT v;
3744 unsigned HOST_WIDE_INT w;
3745 int kind, ikind, bit_size;
3746
3747 v = wlhs.to_shwi ();
3748 w = absu_hwi (v);
3749
3750 kind = expr->value.op.op1->ts.kind;
3751 ikind = gfc_validate_kind (BT_INTEGER, kind, false);
3752 bit_size = gfc_integer_kinds[ikind].bit_size;
3753
3754 if (v == 1)
3755 {
3756 /* 1**something is always 1. */
3757 se->expr = build_int_cst (TREE_TYPE (lse.expr), 1);
3758 return;
3759 }
3760 else if (v == -1)
3761 {
3762 /* (-1)**n is 1 - ((n & 1) << 1) */
3763 tree type;
3764 tree tmp;
3765
3766 type = TREE_TYPE (lse.expr);
3767 tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type,
3768 rse.expr, build_int_cst (type, 1));
3769 tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3770 tmp, build_int_cst (type, 1));
3771 tmp = fold_build2_loc (input_location, MINUS_EXPR, type,
3772 build_int_cst (type, 1), tmp);
3773 se->expr = tmp;
3774 return;
3775 }
3776 else if (w > 0 && ((w & (w-1)) == 0) && ((w >> (bit_size-1)) == 0))
3777 {
3778 /* Here v is +/- 2**e. The further simplification uses
3779 2**n = 1<<n, 4**n = 1<<(n+n), 8**n = 1 <<(3*n), 16**n =
3780 1<<(4*n), etc., but we have to make sure to return zero
3781 if the number of bits is too large. */
3782 tree lshift;
3783 tree type;
3784 tree shift;
3785 tree ge;
3786 tree cond;
3787 tree num_bits;
3788 tree cond2;
3789 tree tmp1;
3790
3791 type = TREE_TYPE (lse.expr);
3792
3793 if (w == 2)
3794 shift = rse.expr;
3795 else if (w == 4)
3796 shift = fold_build2_loc (input_location, PLUS_EXPR,
3797 TREE_TYPE (rse.expr),
3798 rse.expr, rse.expr);
3799 else
3800 {
3801 /* use popcount for fast log2(w) */
3802 int e = wi::popcount (w-1);
3803 shift = fold_build2_loc (input_location, MULT_EXPR,
3804 TREE_TYPE (rse.expr),
3805 build_int_cst (TREE_TYPE (rse.expr), e),
3806 rse.expr);
3807 }
3808
3809 lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3810 build_int_cst (type, 1), shift);
3811 ge = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
3812 rse.expr, build_int_cst (type, 0));
3813 cond = fold_build3_loc (input_location, COND_EXPR, type, ge, lshift,
3814 build_int_cst (type, 0));
3815 num_bits = build_int_cst (TREE_TYPE (rse.expr), TYPE_PRECISION (type));
3816 cond2 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
3817 rse.expr, num_bits);
3818 tmp1 = fold_build3_loc (input_location, COND_EXPR, type, cond2,
3819 build_int_cst (type, 0), cond);
3820 if (v > 0)
3821 {
3822 se->expr = tmp1;
3823 }
3824 else
3825 {
3826 /* for v < 0, calculate v**n = |v|**n * (-1)**n */
3827 tree tmp2;
3828 tmp2 = fold_build2_loc (input_location, BIT_AND_EXPR, type,
3829 rse.expr, build_int_cst (type, 1));
3830 tmp2 = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3831 tmp2, build_int_cst (type, 1));
3832 tmp2 = fold_build2_loc (input_location, MINUS_EXPR, type,
3833 build_int_cst (type, 1), tmp2);
3834 se->expr = fold_build2_loc (input_location, MULT_EXPR, type,
3835 tmp1, tmp2);
3836 }
3837 return;
3838 }
3839 }
3840 /* Handle unsigned separate from signed above, things would be too
3841 complicated otherwise. */
3842
3843 if (INTEGER_CST_P (lse.expr) && expr->value.op.op1->ts.type == BT_UNSIGNED)
3844 {
3845 gfc_expr * op1 = expr->value.op.op1;
3846 tree type;
3847
3848 type = TREE_TYPE (lse.expr);
3849
3850 if (mpz_cmp_ui (op1->value.integer, 1) == 0)
3851 {
3852 /* 1**something is always 1. */
3853 se->expr = build_int_cst (type, 1);
3854 return;
3855 }
3856
3857 /* Simplify 2u**x to a shift, with the value set to zero if it falls
3858 outside the range. */
3859 if (mpz_popcount (op1->value.integer) == 1)
3860 {
3861 tree prec_m1, lim, shift, lshift, cond, tmp;
3862 tree rtype = TREE_TYPE (rse.expr);
3863 int e = mpz_scan1 (op1->value.integer, 0);
3864
3865 shift = fold_build2_loc (input_location, MULT_EXPR,
3866 rtype, build_int_cst (rtype, e),
3867 rse.expr);
3868 lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3869 build_int_cst (type, 1), shift);
3870 prec_m1 = fold_build2_loc (input_location, MINUS_EXPR, rtype,
3871 build_int_cst (rtype, TYPE_PRECISION (type)),
3872 build_int_cst (rtype, 1));
3873 lim = fold_build2_loc (input_location, TRUNC_DIV_EXPR, rtype,
3874 prec_m1, build_int_cst (rtype, e));
3875 cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3876 rse.expr, lim);
3877 tmp = fold_build3_loc (input_location, COND_EXPR, type, cond,
3878 build_int_cst (type, 0), lshift);
3879 se->expr = tmp;
3880 return;
3881 }
3882 }
3883
3884 gfc_int4_type_node = gfc_get_int_type (4);
3885
3886 /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
3887 library routine. But in the end, we have to convert the result back
3888 if this case applies -- with res_ikind_K, we keep track whether operand K
3889 falls into this case. */
3890 res_ikind_1 = -1;
3891 res_ikind_2 = -1;
3892
3893 kind = expr->value.op.op1->ts.kind;
3894 switch (expr->value.op.op2->ts.type)
3895 {
3896 case BT_INTEGER:
3897 ikind = expr->value.op.op2->ts.kind;
3898 switch (ikind)
3899 {
3900 case 1:
3901 case 2:
3902 rse.expr = convert (gfc_int4_type_node, rse.expr);
3903 res_ikind_2 = ikind;
3904 /* Fall through. */
3905
3906 case 4:
3907 ikind = 0;
3908 break;
3909
3910 case 8:
3911 ikind = 1;
3912 break;
3913
3914 case 16:
3915 ikind = 2;
3916 break;
3917
3918 default:
3919 gcc_unreachable ();
3920 }
3921 switch (kind)
3922 {
3923 case 1:
3924 case 2:
3925 if (expr->value.op.op1->ts.type == BT_INTEGER)
3926 {
3927 lse.expr = convert (gfc_int4_type_node, lse.expr);
3928 res_ikind_1 = kind;
3929 }
3930 else
3931 gcc_unreachable ();
3932 /* Fall through. */
3933
3934 case 4:
3935 kind = 0;
3936 break;
3937
3938 case 8:
3939 kind = 1;
3940 break;
3941
3942 case 10:
3943 kind = 2;
3944 break;
3945
3946 case 16:
3947 kind = 3;
3948 break;
3949
3950 default:
3951 gcc_unreachable ();
3952 }
3953
3954 switch (expr->value.op.op1->ts.type)
3955 {
3956 case BT_INTEGER:
3957 if (kind == 3) /* Case 16 was not handled properly above. */
3958 kind = 2;
3959 fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
3960 break;
3961
3962 case BT_REAL:
3963 /* Use builtins for real ** int4. */
3964 if (ikind == 0)
3965 {
3966 switch (kind)
3967 {
3968 case 0:
3969 fndecl = builtin_decl_explicit (BUILT_IN_POWIF);
3970 break;
3971
3972 case 1:
3973 fndecl = builtin_decl_explicit (BUILT_IN_POWI);
3974 break;
3975
3976 case 2:
3977 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
3978 break;
3979
3980 case 3:
3981 /* Use the __builtin_powil() only if real(kind=16) is
3982 actually the C long double type. */
3983 if (!gfc_real16_is_float128)
3984 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
3985 break;
3986
3987 default:
3988 gcc_unreachable ();
3989 }
3990 }
3991
3992 /* If we don't have a good builtin for this, go for the
3993 library function. */
3994 if (!fndecl)
3995 fndecl = gfor_fndecl_math_powi[kind][ikind].real;
3996 break;
3997
3998 case BT_COMPLEX:
3999 fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
4000 break;
4001
4002 default:
4003 gcc_unreachable ();
4004 }
4005 break;
4006
4007 case BT_REAL:
4008 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
4009 break;
4010
4011 case BT_COMPLEX:
4012 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
4013 break;
4014
4015 case BT_UNSIGNED:
4016 {
4017 /* Valid kinds for unsigned are 1, 2, 4, 8, 16. Instead of using a
4018 large switch statement, let's just use __builtin_ctz. */
4019 int base = __builtin_ctz (expr->value.op.op1->ts.kind);
4020 int expon = __builtin_ctz (expr->value.op.op2->ts.kind);
4021 fndecl = gfor_fndecl_unsigned_pow_list[base][expon];
4022 }
4023 break;
4024
4025 default:
4026 gcc_unreachable ();
4027 break;
4028 }
4029
4030 se->expr = build_call_expr_loc (input_location,
4031 fndecl, 2, lse.expr, rse.expr);
4032
4033 /* Convert the result back if it is of wrong integer kind. */
4034 if (res_ikind_1 != -1 && res_ikind_2 != -1)
4035 {
4036 /* We want the maximum of both operand kinds as result. */
4037 if (res_ikind_1 < res_ikind_2)
4038 res_ikind_1 = res_ikind_2;
4039 se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr);
4040 }
4041 }
4042
4043
4044 /* Generate code to allocate a string temporary. */
4045
4046 tree
4047 gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
4048 {
4049 tree var;
4050 tree tmp;
4051
4052 if (gfc_can_put_var_on_stack (len))
4053 {
4054 /* Create a temporary variable to hold the result. */
4055 tmp = fold_build2_loc (input_location, MINUS_EXPR,
4056 TREE_TYPE (len), len,
4057 build_int_cst (TREE_TYPE (len), 1));
4058 tmp = build_range_type (gfc_charlen_type_node, size_zero_node, tmp);
4059
4060 if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
4061 tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
4062 else
4063 tmp = build_array_type (TREE_TYPE (type), tmp);
4064
4065 var = gfc_create_var (tmp, "str");
4066 var = gfc_build_addr_expr (type, var);
4067 }
4068 else
4069 {
4070 /* Allocate a temporary to hold the result. */
4071 var = gfc_create_var (type, "pstr");
4072 gcc_assert (POINTER_TYPE_P (type));
4073 tmp = TREE_TYPE (type);
4074 if (TREE_CODE (tmp) == ARRAY_TYPE)
4075 tmp = TREE_TYPE (tmp);
4076 tmp = TYPE_SIZE_UNIT (tmp);
4077 tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
4078 fold_convert (size_type_node, len),
4079 fold_convert (size_type_node, tmp));
4080 tmp = gfc_call_malloc (&se->pre, type, tmp);
4081 gfc_add_modify (&se->pre, var, tmp);
4082
4083 /* Free the temporary afterwards. */
4084 tmp = gfc_call_free (var);
4085 gfc_add_expr_to_block (&se->post, tmp);
4086 }
4087
4088 return var;
4089 }
4090
4091
4092 /* Handle a string concatenation operation. A temporary will be allocated to
4093 hold the result. */
4094
4095 static void
4096 gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
4097 {
4098 gfc_se lse, rse;
4099 tree len, type, var, tmp, fndecl;
4100
4101 gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
4102 && expr->value.op.op2->ts.type == BT_CHARACTER);
4103 gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
4104
4105 gfc_init_se (&lse, se);
4106 gfc_conv_expr (&lse, expr->value.op.op1);
4107 gfc_conv_string_parameter (&lse);
4108 gfc_init_se (&rse, se);
4109 gfc_conv_expr (&rse, expr->value.op.op2);
4110 gfc_conv_string_parameter (&rse);
4111
4112 gfc_add_block_to_block (&se->pre, &lse.pre);
4113 gfc_add_block_to_block (&se->pre, &rse.pre);
4114
4115 type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
4116 len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
4117 if (len == NULL_TREE)
4118 {
4119 len = fold_build2_loc (input_location, PLUS_EXPR,
4120 gfc_charlen_type_node,
4121 fold_convert (gfc_charlen_type_node,
4122 lse.string_length),
4123 fold_convert (gfc_charlen_type_node,
4124 rse.string_length));
4125 }
4126
4127 type = build_pointer_type (type);
4128
4129 var = gfc_conv_string_tmp (se, type, len);
4130
4131 /* Do the actual concatenation. */
4132 if (expr->ts.kind == 1)
4133 fndecl = gfor_fndecl_concat_string;
4134 else if (expr->ts.kind == 4)
4135 fndecl = gfor_fndecl_concat_string_char4;
4136 else
4137 gcc_unreachable ();
4138
4139 tmp = build_call_expr_loc (input_location,
4140 fndecl, 6, len, var, lse.string_length, lse.expr,
4141 rse.string_length, rse.expr);
4142 gfc_add_expr_to_block (&se->pre, tmp);
4143
4144 /* Add the cleanup for the operands. */
4145 gfc_add_block_to_block (&se->pre, &rse.post);
4146 gfc_add_block_to_block (&se->pre, &lse.post);
4147
4148 se->expr = var;
4149 se->string_length = len;
4150 }
4151
4152 /* Translates an op expression. Common (binary) cases are handled by this
4153 function, others are passed on. Recursion is used in either case.
4154 We use the fact that (op1.ts == op2.ts) (except for the power
4155 operator **).
4156 Operators need no special handling for scalarized expressions as long as
4157 they call gfc_conv_simple_val to get their operands.
4158 Character strings get special handling. */
4159
4160 static void
4161 gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
4162 {
4163 enum tree_code code;
4164 gfc_se lse;
4165 gfc_se rse;
4166 tree tmp, type;
4167 int lop;
4168 int checkstring;
4169
4170 checkstring = 0;
4171 lop = 0;
4172 switch (expr->value.op.op)
4173 {
4174 case INTRINSIC_PARENTHESES:
4175 if ((expr->ts.type == BT_REAL || expr->ts.type == BT_COMPLEX)
4176 && flag_protect_parens)
4177 {
4178 gfc_conv_unary_op (PAREN_EXPR, se, expr);
4179 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
4180 return;
4181 }
4182
4183 /* Fallthrough. */
4184 case INTRINSIC_UPLUS:
4185 gfc_conv_expr (se, expr->value.op.op1);
4186 return;
4187
4188 case INTRINSIC_UMINUS:
4189 gfc_conv_unary_op (NEGATE_EXPR, se, expr);
4190 return;
4191
4192 case INTRINSIC_NOT:
4193 gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
4194 return;
4195
4196 case INTRINSIC_PLUS:
4197 code = PLUS_EXPR;
4198 break;
4199
4200 case INTRINSIC_MINUS:
4201 code = MINUS_EXPR;
4202 break;
4203
4204 case INTRINSIC_TIMES:
4205 code = MULT_EXPR;
4206 break;
4207
4208 case INTRINSIC_DIVIDE:
4209 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
4210 an integer or unsigned, we must round towards zero, so we use a
4211 TRUNC_DIV_EXPR. */
4212 if (expr->ts.type == BT_INTEGER || expr->ts.type == BT_UNSIGNED)
4213 code = TRUNC_DIV_EXPR;
4214 else
4215 code = RDIV_EXPR;
4216 break;
4217
4218 case INTRINSIC_POWER:
4219 gfc_conv_power_op (se, expr);
4220 return;
4221
4222 case INTRINSIC_CONCAT:
4223 gfc_conv_concat_op (se, expr);
4224 return;
4225
4226 case INTRINSIC_AND:
4227 code = flag_frontend_optimize ? TRUTH_ANDIF_EXPR : TRUTH_AND_EXPR;
4228 lop = 1;
4229 break;
4230
4231 case INTRINSIC_OR:
4232 code = flag_frontend_optimize ? TRUTH_ORIF_EXPR : TRUTH_OR_EXPR;
4233 lop = 1;
4234 break;
4235
4236 /* EQV and NEQV only work on logicals, but since we represent them
4237 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
4238 case INTRINSIC_EQ:
4239 case INTRINSIC_EQ_OS:
4240 case INTRINSIC_EQV:
4241 code = EQ_EXPR;
4242 checkstring = 1;
4243 lop = 1;
4244 break;
4245
4246 case INTRINSIC_NE:
4247 case INTRINSIC_NE_OS:
4248 case INTRINSIC_NEQV:
4249 code = NE_EXPR;
4250 checkstring = 1;
4251 lop = 1;
4252 break;
4253
4254 case INTRINSIC_GT:
4255 case INTRINSIC_GT_OS:
4256 code = GT_EXPR;
4257 checkstring = 1;
4258 lop = 1;
4259 break;
4260
4261 case INTRINSIC_GE:
4262 case INTRINSIC_GE_OS:
4263 code = GE_EXPR;
4264 checkstring = 1;
4265 lop = 1;
4266 break;
4267
4268 case INTRINSIC_LT:
4269 case INTRINSIC_LT_OS:
4270 code = LT_EXPR;
4271 checkstring = 1;
4272 lop = 1;
4273 break;
4274
4275 case INTRINSIC_LE:
4276 case INTRINSIC_LE_OS:
4277 code = LE_EXPR;
4278 checkstring = 1;
4279 lop = 1;
4280 break;
4281
4282 case INTRINSIC_USER:
4283 case INTRINSIC_ASSIGN:
4284 /* These should be converted into function calls by the frontend. */
4285 gcc_unreachable ();
4286
4287 default:
4288 fatal_error (input_location, "Unknown intrinsic op");
4289 return;
4290 }
4291
4292 /* The only exception to this is **, which is handled separately anyway. */
4293 gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
4294
4295 if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
4296 checkstring = 0;
4297
4298 /* lhs */
4299 gfc_init_se (&lse, se);
4300 gfc_conv_expr (&lse, expr->value.op.op1);
4301 gfc_add_block_to_block (&se->pre, &lse.pre);
4302
4303 /* rhs */
4304 gfc_init_se (&rse, se);
4305 gfc_conv_expr (&rse, expr->value.op.op2);
4306 gfc_add_block_to_block (&se->pre, &rse.pre);
4307
4308 if (checkstring)
4309 {
4310 gfc_conv_string_parameter (&lse);
4311 gfc_conv_string_parameter (&rse);
4312
4313 lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
4314 rse.string_length, rse.expr,
4315 expr->value.op.op1->ts.kind,
4316 code);
4317 rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
4318 gfc_add_block_to_block (&lse.post, &rse.post);
4319 }
4320
4321 type = gfc_typenode_for_spec (&expr->ts);
4322
4323 if (lop)
4324 {
4325 // Inhibit overeager optimization of Cray pointer comparisons (PR106692).
4326 if (expr->value.op.op1->expr_type == EXPR_VARIABLE
4327 && expr->value.op.op1->ts.type == BT_INTEGER
4328 && expr->value.op.op1->symtree
4329 && expr->value.op.op1->symtree->n.sym->attr.cray_pointer)
4330 TREE_THIS_VOLATILE (lse.expr) = 1;
4331
4332 if (expr->value.op.op2->expr_type == EXPR_VARIABLE
4333 && expr->value.op.op2->ts.type == BT_INTEGER
4334 && expr->value.op.op2->symtree
4335 && expr->value.op.op2->symtree->n.sym->attr.cray_pointer)
4336 TREE_THIS_VOLATILE (rse.expr) = 1;
4337
4338 /* The result of logical ops is always logical_type_node. */
4339 tmp = fold_build2_loc (input_location, code, logical_type_node,
4340 lse.expr, rse.expr);
4341 se->expr = convert (type, tmp);
4342 }
4343 else
4344 se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
4345
4346 /* Add the post blocks. */
4347 gfc_add_block_to_block (&se->post, &rse.post);
4348 gfc_add_block_to_block (&se->post, &lse.post);
4349 }
4350
4351 /* If a string's length is one, we convert it to a single character. */
4352
4353 tree
4354 gfc_string_to_single_character (tree len, tree str, int kind)
4355 {
4356
4357 if (len == NULL
4358 || !tree_fits_uhwi_p (len)
4359 || !POINTER_TYPE_P (TREE_TYPE (str)))
4360 return NULL_TREE;
4361
4362 if (TREE_INT_CST_LOW (len) == 1)
4363 {
4364 str = fold_convert (gfc_get_pchar_type (kind), str);
4365 return build_fold_indirect_ref_loc (input_location, str);
4366 }
4367
4368 if (kind == 1
4369 && TREE_CODE (str) == ADDR_EXPR
4370 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
4371 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
4372 && array_ref_low_bound (TREE_OPERAND (str, 0))
4373 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
4374 && TREE_INT_CST_LOW (len) > 1
4375 && TREE_INT_CST_LOW (len)
4376 == (unsigned HOST_WIDE_INT)
4377 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
4378 {
4379 tree ret = fold_convert (gfc_get_pchar_type (kind), str);
4380 ret = build_fold_indirect_ref_loc (input_location, ret);
4381 if (TREE_CODE (ret) == INTEGER_CST)
4382 {
4383 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
4384 int i, length = TREE_STRING_LENGTH (string_cst);
4385 const char *ptr = TREE_STRING_POINTER (string_cst);
4386
4387 for (i = 1; i < length; i++)
4388 if (ptr[i] != ' ')
4389 return NULL_TREE;
4390
4391 return ret;
4392 }
4393 }
4394
4395 return NULL_TREE;
4396 }
4397
4398
4399 static void
4400 conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
4401 {
4402 gcc_assert (expr);
4403
4404 /* We used to modify the tree here. Now it is done earlier in
4405 the front-end, so we only check it here to avoid regressions. */
4406 if (sym->backend_decl)
4407 {
4408 gcc_assert (TREE_CODE (TREE_TYPE (sym->backend_decl)) == INTEGER_TYPE);
4409 gcc_assert (TYPE_UNSIGNED (TREE_TYPE (sym->backend_decl)) == 1);
4410 gcc_assert (TYPE_PRECISION (TREE_TYPE (sym->backend_decl)) == CHAR_TYPE_SIZE);
4411 gcc_assert (DECL_BY_REFERENCE (sym->backend_decl) == 0);
4412 }
4413
4414 /* If we have a constant character expression, make it into an
4415 integer of type C char. */
4416 if ((*expr)->expr_type == EXPR_CONSTANT)
4417 {
4418 gfc_typespec ts;
4419 gfc_clear_ts (&ts);
4420
4421 gfc_expr *tmp = gfc_get_int_expr (gfc_default_character_kind, NULL,
4422 (*expr)->value.character.string[0]);
4423 gfc_replace_expr (*expr, tmp);
4424 }
4425 else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
4426 {
4427 if ((*expr)->ref == NULL)
4428 {
4429 se->expr = gfc_string_to_single_character
4430 (integer_one_node,
4431 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
4432 gfc_get_symbol_decl
4433 ((*expr)->symtree->n.sym)),
4434 (*expr)->ts.kind);
4435 }
4436 else
4437 {
4438 gfc_conv_variable (se, *expr);
4439 se->expr = gfc_string_to_single_character
4440 (integer_one_node,
4441 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
4442 se->expr),
4443 (*expr)->ts.kind);
4444 }
4445 }
4446 }
4447
4448 /* Helper function for gfc_build_compare_string. Return LEN_TRIM value
4449 if STR is a string literal, otherwise return -1. */
4450
4451 static int
4452 gfc_optimize_len_trim (tree len, tree str, int kind)
4453 {
4454 if (kind == 1
4455 && TREE_CODE (str) == ADDR_EXPR
4456 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
4457 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
4458 && array_ref_low_bound (TREE_OPERAND (str, 0))
4459 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
4460 && tree_fits_uhwi_p (len)
4461 && tree_to_uhwi (len) >= 1
4462 && tree_to_uhwi (len)
4463 == (unsigned HOST_WIDE_INT)
4464 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
4465 {
4466 tree folded = fold_convert (gfc_get_pchar_type (kind), str);
4467 folded = build_fold_indirect_ref_loc (input_location, folded);
4468 if (TREE_CODE (folded) == INTEGER_CST)
4469 {
4470 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
4471 int length = TREE_STRING_LENGTH (string_cst);
4472 const char *ptr = TREE_STRING_POINTER (string_cst);
4473
4474 for (; length > 0; length--)
4475 if (ptr[length - 1] != ' ')
4476 break;
4477
4478 return length;
4479 }
4480 }
4481 return -1;
4482 }
4483
4484 /* Helper to build a call to memcmp. */
4485
4486 static tree
4487 build_memcmp_call (tree s1, tree s2, tree n)
4488 {
4489 tree tmp;
4490
4491 if (!POINTER_TYPE_P (TREE_TYPE (s1)))
4492 s1 = gfc_build_addr_expr (pvoid_type_node, s1);
4493 else
4494 s1 = fold_convert (pvoid_type_node, s1);
4495
4496 if (!POINTER_TYPE_P (TREE_TYPE (s2)))
4497 s2 = gfc_build_addr_expr (pvoid_type_node, s2);
4498 else
4499 s2 = fold_convert (pvoid_type_node, s2);
4500
4501 n = fold_convert (size_type_node, n);
4502
4503 tmp = build_call_expr_loc (input_location,
4504 builtin_decl_explicit (BUILT_IN_MEMCMP),
4505 3, s1, s2, n);
4506
4507 return fold_convert (integer_type_node, tmp);
4508 }
4509
4510 /* Compare two strings. If they are all single characters, the result is the
4511 subtraction of them. Otherwise, we build a library call. */
4512
4513 tree
4514 gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
4515 enum tree_code code)
4516 {
4517 tree sc1;
4518 tree sc2;
4519 tree fndecl;
4520
4521 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
4522 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
4523
4524 sc1 = gfc_string_to_single_character (len1, str1, kind);
4525 sc2 = gfc_string_to_single_character (len2, str2, kind);
4526
4527 if (sc1 != NULL_TREE && sc2 != NULL_TREE)
4528 {
4529 /* Deal with single character specially. */
4530 sc1 = fold_convert (integer_type_node, sc1);
4531 sc2 = fold_convert (integer_type_node, sc2);
4532 return fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
4533 sc1, sc2);
4534 }
4535
4536 if ((code == EQ_EXPR || code == NE_EXPR)
4537 && optimize
4538 && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
4539 {
4540 /* If one string is a string literal with LEN_TRIM longer
4541 than the length of the second string, the strings
4542 compare unequal. */
4543 int len = gfc_optimize_len_trim (len1, str1, kind);
4544 if (len > 0 && compare_tree_int (len2, len) < 0)
4545 return integer_one_node;
4546 len = gfc_optimize_len_trim (len2, str2, kind);
4547 if (len > 0 && compare_tree_int (len1, len) < 0)
4548 return integer_one_node;
4549 }
4550
4551 /* We can compare via memcpy if the strings are known to be equal
4552 in length and they are
4553 - kind=1
4554 - kind=4 and the comparison is for (in)equality. */
4555
4556 if (INTEGER_CST_P (len1) && INTEGER_CST_P (len2)
4557 && tree_int_cst_equal (len1, len2)
4558 && (kind == 1 || code == EQ_EXPR || code == NE_EXPR))
4559 {
4560 tree tmp;
4561 tree chartype;
4562
4563 chartype = gfc_get_char_type (kind);
4564 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE(len1),
4565 fold_convert (TREE_TYPE(len1),
4566 TYPE_SIZE_UNIT(chartype)),
4567 len1);
4568 return build_memcmp_call (str1, str2, tmp);
4569 }
4570
4571 /* Build a call for the comparison. */
4572 if (kind == 1)
4573 fndecl = gfor_fndecl_compare_string;
4574 else if (kind == 4)
4575 fndecl = gfor_fndecl_compare_string_char4;
4576 else
4577 gcc_unreachable ();
4578
4579 return build_call_expr_loc (input_location, fndecl, 4,
4580 len1, str1, len2, str2);
4581 }
4582
4583
4584 /* Return the backend_decl for a procedure pointer component. */
4585
4586 static tree
4587 get_proc_ptr_comp (gfc_expr *e)
4588 {
4589 gfc_se comp_se;
4590 gfc_expr *e2;
4591 expr_t old_type;
4592
4593 gfc_init_se (&comp_se, NULL);
4594 e2 = gfc_copy_expr (e);
4595 /* We have to restore the expr type later so that gfc_free_expr frees
4596 the exact same thing that was allocated.
4597 TODO: This is ugly. */
4598 old_type = e2->expr_type;
4599 e2->expr_type = EXPR_VARIABLE;
4600 gfc_conv_expr (&comp_se, e2);
4601 e2->expr_type = old_type;
4602 gfc_free_expr (e2);
4603 return build_fold_addr_expr_loc (input_location, comp_se.expr);
4604 }
4605
4606
4607 /* Convert a typebound function reference from a class object. */
4608 static void
4609 conv_base_obj_fcn_val (gfc_se * se, tree base_object, gfc_expr * expr)
4610 {
4611 gfc_ref *ref;
4612 tree var;
4613
4614 if (!VAR_P (base_object))
4615 {
4616 var = gfc_create_var (TREE_TYPE (base_object), NULL);
4617 gfc_add_modify (&se->pre, var, base_object);
4618 }
4619 se->expr = gfc_class_vptr_get (base_object);
4620 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
4621 ref = expr->ref;
4622 while (ref && ref->next)
4623 ref = ref->next;
4624 gcc_assert (ref && ref->type == REF_COMPONENT);
4625 if (ref->u.c.sym->attr.extension)
4626 conv_parent_component_references (se, ref);
4627 gfc_conv_component_ref (se, ref);
4628 se->expr = build_fold_addr_expr_loc (input_location, se->expr);
4629 }
4630
4631 static tree
4632 get_builtin_fn (gfc_symbol * sym)
4633 {
4634 if (!gfc_option.disable_omp_is_initial_device
4635 && flag_openmp && sym->attr.function && sym->ts.type == BT_LOGICAL
4636 && !strcmp (sym->name, "omp_is_initial_device"))
4637 return builtin_decl_explicit (BUILT_IN_OMP_IS_INITIAL_DEVICE);
4638
4639 if (!gfc_option.disable_acc_on_device
4640 && flag_openacc && sym->attr.function && sym->ts.type == BT_LOGICAL
4641 && !strcmp (sym->name, "acc_on_device_h"))
4642 return builtin_decl_explicit (BUILT_IN_ACC_ON_DEVICE);
4643
4644 return NULL_TREE;
4645 }
4646
4647 static tree
4648 update_builtin_function (tree fn_call, gfc_symbol *sym)
4649 {
4650 tree fn = TREE_OPERAND (CALL_EXPR_FN (fn_call), 0);
4651
4652 if (DECL_FUNCTION_CODE (fn) == BUILT_IN_OMP_IS_INITIAL_DEVICE)
4653 /* In Fortran omp_is_initial_device returns logical(4)
4654 but the builtin uses 'int'. */
4655 return fold_convert (TREE_TYPE (TREE_TYPE (sym->backend_decl)), fn_call);
4656
4657 else if (DECL_FUNCTION_CODE (fn) == BUILT_IN_ACC_ON_DEVICE)
4658 {
4659 /* Likewise for the return type; additionally, the argument it a
4660 call-by-value int, Fortran has a by-reference 'integer(4)'. */
4661 tree arg = build_fold_indirect_ref_loc (input_location,
4662 CALL_EXPR_ARG (fn_call, 0));
4663 CALL_EXPR_ARG (fn_call, 0) = fold_convert (integer_type_node, arg);
4664 return fold_convert (TREE_TYPE (TREE_TYPE (sym->backend_decl)), fn_call);
4665 }
4666 return fn_call;
4667 }
4668
4669 static void
4670 conv_function_val (gfc_se * se, bool *is_builtin, gfc_symbol * sym,
4671 gfc_expr * expr, gfc_actual_arglist *actual_args)
4672 {
4673 tree tmp;
4674
4675 if (gfc_is_proc_ptr_comp (expr))
4676 tmp = get_proc_ptr_comp (expr);
4677 else if (sym->attr.dummy)
4678 {
4679 tmp = gfc_get_symbol_decl (sym);
4680 if (sym->attr.proc_pointer)
4681 tmp = build_fold_indirect_ref_loc (input_location,
4682 tmp);
4683 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
4684 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
4685 }
4686 else
4687 {
4688 if (!sym->backend_decl)
4689 sym->backend_decl = gfc_get_extern_function_decl (sym, actual_args);
4690
4691 if ((tmp = get_builtin_fn (sym)) != NULL_TREE)
4692 *is_builtin = true;
4693 else
4694 {
4695 TREE_USED (sym->backend_decl) = 1;
4696 tmp = sym->backend_decl;
4697 }
4698
4699 if (sym->attr.cray_pointee)
4700 {
4701 /* TODO - make the cray pointee a pointer to a procedure,
4702 assign the pointer to it and use it for the call. This
4703 will do for now! */
4704 tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
4705 gfc_get_symbol_decl (sym->cp_pointer));
4706 tmp = gfc_evaluate_now (tmp, &se->pre);
4707 }
4708
4709 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
4710 {
4711 gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
4712 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
4713 }
4714 }
4715 se->expr = tmp;
4716 }
4717
4718
4719 /* Initialize MAPPING. */
4720
4721 void
4722 gfc_init_interface_mapping (gfc_interface_mapping * mapping)
4723 {
4724 mapping->syms = NULL;
4725 mapping->charlens = NULL;
4726 }
4727
4728
4729 /* Free all memory held by MAPPING (but not MAPPING itself). */
4730
4731 void
4732 gfc_free_interface_mapping (gfc_interface_mapping * mapping)
4733 {
4734 gfc_interface_sym_mapping *sym;
4735 gfc_interface_sym_mapping *nextsym;
4736 gfc_charlen *cl;
4737 gfc_charlen *nextcl;
4738
4739 for (sym = mapping->syms; sym; sym = nextsym)
4740 {
4741 nextsym = sym->next;
4742 sym->new_sym->n.sym->formal = NULL;
4743 gfc_free_symbol (sym->new_sym->n.sym);
4744 gfc_free_expr (sym->expr);
4745 free (sym->new_sym);
4746 free (sym);
4747 }
4748 for (cl = mapping->charlens; cl; cl = nextcl)
4749 {
4750 nextcl = cl->next;
4751 gfc_free_expr (cl->length);
4752 free (cl);
4753 }
4754 }
4755
4756
4757 /* Return a copy of gfc_charlen CL. Add the returned structure to
4758 MAPPING so that it will be freed by gfc_free_interface_mapping. */
4759
4760 static gfc_charlen *
4761 gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
4762 gfc_charlen * cl)
4763 {
4764 gfc_charlen *new_charlen;
4765
4766 new_charlen = gfc_get_charlen ();
4767 new_charlen->next = mapping->charlens;
4768 new_charlen->length = gfc_copy_expr (cl->length);
4769
4770 mapping->charlens = new_charlen;
4771 return new_charlen;
4772 }
4773
4774
4775 /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
4776 array variable that can be used as the actual argument for dummy
4777 argument SYM, except in the case of assumed rank dummies of
4778 non-intrinsic functions where the descriptor must be passed. Add any
4779 initialization code to BLOCK. PACKED is as for gfc_get_nodesc_array_type
4780 and DATA points to the first element in the passed array. */
4781
4782 static tree
4783 gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
4784 gfc_packed packed, tree data, tree len,
4785 bool assumed_rank_formal)
4786 {
4787 tree type;
4788 tree var;
4789
4790 if (len != NULL_TREE && (TREE_CONSTANT (len) || VAR_P (len)))
4791 type = gfc_get_character_type_len (sym->ts.kind, len);
4792 else
4793 type = gfc_typenode_for_spec (&sym->ts);
4794
4795 if (assumed_rank_formal)
4796 type = TREE_TYPE (data);
4797 else
4798 type = gfc_get_nodesc_array_type (type, sym->as, packed,
4799 !sym->attr.target && !sym->attr.pointer
4800 && !sym->attr.proc_pointer);
4801
4802 var = gfc_create_var (type, "ifm");
4803 gfc_add_modify (block, var, fold_convert (type, data));
4804
4805 return var;
4806 }
4807
4808
4809 /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
4810 and offset of descriptorless array type TYPE given that it has the same
4811 size as DESC. Add any set-up code to BLOCK. */
4812
4813 static void
4814 gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
4815 {
4816 int n;
4817 tree dim;
4818 tree offset;
4819 tree tmp;
4820
4821 offset = gfc_index_zero_node;
4822 for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
4823 {
4824 dim = gfc_rank_cst[n];
4825 GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
4826 if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
4827 {
4828 GFC_TYPE_ARRAY_LBOUND (type, n)
4829 = gfc_conv_descriptor_lbound_get (desc, dim);
4830 GFC_TYPE_ARRAY_UBOUND (type, n)
4831 = gfc_conv_descriptor_ubound_get (desc, dim);
4832 }
4833 else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
4834 {
4835 tmp = fold_build2_loc (input_location, MINUS_EXPR,
4836 gfc_array_index_type,
4837 gfc_conv_descriptor_ubound_get (desc, dim),
4838 gfc_conv_descriptor_lbound_get (desc, dim));
4839 tmp = fold_build2_loc (input_location, PLUS_EXPR,
4840 gfc_array_index_type,
4841 GFC_TYPE_ARRAY_LBOUND (type, n), tmp);
4842 tmp = gfc_evaluate_now (tmp, block);
4843 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
4844 }
4845 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
4846 GFC_TYPE_ARRAY_LBOUND (type, n),
4847 GFC_TYPE_ARRAY_STRIDE (type, n));
4848 offset = fold_build2_loc (input_location, MINUS_EXPR,
4849 gfc_array_index_type, offset, tmp);
4850 }
4851 offset = gfc_evaluate_now (offset, block);
4852 GFC_TYPE_ARRAY_OFFSET (type) = offset;
4853 }
4854
4855
4856 /* Extend MAPPING so that it maps dummy argument SYM to the value stored
4857 in SE. The caller may still use se->expr and se->string_length after
4858 calling this function. */
4859
4860 void
4861 gfc_add_interface_mapping (gfc_interface_mapping * mapping,
4862 gfc_symbol * sym, gfc_se * se,
4863 gfc_expr *expr)
4864 {
4865 gfc_interface_sym_mapping *sm;
4866 tree desc;
4867 tree tmp;
4868 tree value;
4869 gfc_symbol *new_sym;
4870 gfc_symtree *root;
4871 gfc_symtree *new_symtree;
4872
4873 /* Create a new symbol to represent the actual argument. */
4874 new_sym = gfc_new_symbol (sym->name, NULL);
4875 new_sym->ts = sym->ts;
4876 new_sym->as = gfc_copy_array_spec (sym->as);
4877 new_sym->attr.referenced = 1;
4878 new_sym->attr.dimension = sym->attr.dimension;
4879 new_sym->attr.contiguous = sym->attr.contiguous;
4880 new_sym->attr.codimension = sym->attr.codimension;
4881 new_sym->attr.pointer = sym->attr.pointer;
4882 new_sym->attr.allocatable = sym->attr.allocatable;
4883 new_sym->attr.flavor = sym->attr.flavor;
4884 new_sym->attr.function = sym->attr.function;
4885 new_sym->attr.dummy = 0;
4886
4887 /* Ensure that the interface is available and that
4888 descriptors are passed for array actual arguments. */
4889 if (sym->attr.flavor == FL_PROCEDURE)
4890 {
4891 new_sym->formal = expr->symtree->n.sym->formal;
4892 new_sym->attr.always_explicit
4893 = expr->symtree->n.sym->attr.always_explicit;
4894 }
4895
4896 /* Create a fake symtree for it. */
4897 root = NULL;
4898 new_symtree = gfc_new_symtree (&root, sym->name);
4899 new_symtree->n.sym = new_sym;
4900 gcc_assert (new_symtree == root);
4901
4902 /* Create a dummy->actual mapping. */
4903 sm = XCNEW (gfc_interface_sym_mapping);
4904 sm->next = mapping->syms;
4905 sm->old = sym;
4906 sm->new_sym = new_symtree;
4907 sm->expr = gfc_copy_expr (expr);
4908 mapping->syms = sm;
4909
4910 /* Stabilize the argument's value. */
4911 if (!sym->attr.function && se)
4912 se->expr = gfc_evaluate_now (se->expr, &se->pre);
4913
4914 if (sym->ts.type == BT_CHARACTER)
4915 {
4916 /* Create a copy of the dummy argument's length. */
4917 new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
4918 sm->expr->ts.u.cl = new_sym->ts.u.cl;
4919
4920 /* If the length is specified as "*", record the length that
4921 the caller is passing. We should use the callee's length
4922 in all other cases. */
4923 if (!new_sym->ts.u.cl->length && se)
4924 {
4925 se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
4926 new_sym->ts.u.cl->backend_decl = se->string_length;
4927 }
4928 }
4929
4930 if (!se)
4931 return;
4932
4933 /* Use the passed value as-is if the argument is a function. */
4934 if (sym->attr.flavor == FL_PROCEDURE)
4935 value = se->expr;
4936
4937 /* If the argument is a pass-by-value scalar, use the value as is. */
4938 else if (!sym->attr.dimension && sym->attr.value)
4939 value = se->expr;
4940
4941 /* If the argument is either a string or a pointer to a string,
4942 convert it to a boundless character type. */
4943 else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
4944 {
4945 se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
4946 tmp = gfc_get_character_type_len (sym->ts.kind, se->string_length);
4947 tmp = build_pointer_type (tmp);
4948 if (sym->attr.pointer)
4949 value = build_fold_indirect_ref_loc (input_location,
4950 se->expr);
4951 else
4952 value = se->expr;
4953 value = fold_convert (tmp, value);
4954 }
4955
4956 /* If the argument is a scalar, a pointer to an array or an allocatable,
4957 dereference it. */
4958 else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
4959 value = build_fold_indirect_ref_loc (input_location,
4960 se->expr);
4961
4962 /* For character(*), use the actual argument's descriptor. */
4963 else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
4964 value = build_fold_indirect_ref_loc (input_location,
4965 se->expr);
4966
4967 /* If the argument is an array descriptor, use it to determine
4968 information about the actual argument's shape. */
4969 else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
4970 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
4971 {
4972 bool assumed_rank_formal = false;
4973
4974 /* Get the actual argument's descriptor. */
4975 desc = build_fold_indirect_ref_loc (input_location,
4976 se->expr);
4977
4978 /* Create the replacement variable. */
4979 if (sym->as && sym->as->type == AS_ASSUMED_RANK
4980 && !(sym->ns && sym->ns->proc_name
4981 && sym->ns->proc_name->attr.proc == PROC_INTRINSIC))
4982 {
4983 assumed_rank_formal = true;
4984 tmp = desc;
4985 }
4986 else
4987 tmp = gfc_conv_descriptor_data_get (desc);
4988
4989 value = gfc_get_interface_mapping_array (&se->pre, sym,
4990 PACKED_NO, tmp,
4991 se->string_length,
4992 assumed_rank_formal);
4993
4994 /* Use DESC to work out the upper bounds, strides and offset. */
4995 gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
4996 }
4997 else
4998 /* Otherwise we have a packed array. */
4999 value = gfc_get_interface_mapping_array (&se->pre, sym,
5000 PACKED_FULL, se->expr,
5001 se->string_length,
5002 false);
5003
5004 new_sym->backend_decl = value;
5005 }
5006
5007
5008 /* Called once all dummy argument mappings have been added to MAPPING,
5009 but before the mapping is used to evaluate expressions. Pre-evaluate
5010 the length of each argument, adding any initialization code to PRE and
5011 any finalization code to POST. */
5012
5013 static void
5014 gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
5015 stmtblock_t * pre, stmtblock_t * post)
5016 {
5017 gfc_interface_sym_mapping *sym;
5018 gfc_expr *expr;
5019 gfc_se se;
5020
5021 for (sym = mapping->syms; sym; sym = sym->next)
5022 if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
5023 && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
5024 {
5025 expr = sym->new_sym->n.sym->ts.u.cl->length;
5026 gfc_apply_interface_mapping_to_expr (mapping, expr);
5027 gfc_init_se (&se, NULL);
5028 gfc_conv_expr (&se, expr);
5029 se.expr = fold_convert (gfc_charlen_type_node, se.expr);
5030 se.expr = gfc_evaluate_now (se.expr, &se.pre);
5031 gfc_add_block_to_block (pre, &se.pre);
5032 gfc_add_block_to_block (post, &se.post);
5033
5034 sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
5035 }
5036 }
5037
5038
5039 /* Like gfc_apply_interface_mapping_to_expr, but applied to
5040 constructor C. */
5041
5042 static void
5043 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
5044 gfc_constructor_base base)
5045 {
5046 gfc_constructor *c;
5047 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
5048 {
5049 gfc_apply_interface_mapping_to_expr (mapping, c->expr);
5050 if (c->iterator)
5051 {
5052 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
5053 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
5054 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
5055 }
5056 }
5057 }
5058
5059
5060 /* Like gfc_apply_interface_mapping_to_expr, but applied to
5061 reference REF. */
5062
5063 static void
5064 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
5065 gfc_ref * ref)
5066 {
5067 int n;
5068
5069 for (; ref; ref = ref->next)
5070 switch (ref->type)
5071 {
5072 case REF_ARRAY:
5073 for (n = 0; n < ref->u.ar.dimen; n++)
5074 {
5075 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
5076 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
5077 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
5078 }
5079 break;
5080
5081 case REF_COMPONENT:
5082 case REF_INQUIRY:
5083 break;
5084
5085 case REF_SUBSTRING:
5086 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
5087 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
5088 break;
5089 }
5090 }
5091
5092
5093 /* Convert intrinsic function calls into result expressions. */
5094
5095 static bool
5096 gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
5097 {
5098 gfc_symbol *sym;
5099 gfc_expr *new_expr;
5100 gfc_expr *arg1;
5101 gfc_expr *arg2;
5102 int d, dup;
5103
5104 arg1 = expr->value.function.actual->expr;
5105 if (expr->value.function.actual->next)
5106 arg2 = expr->value.function.actual->next->expr;
5107 else
5108 arg2 = NULL;
5109
5110 sym = arg1->symtree->n.sym;
5111
5112 if (sym->attr.dummy)
5113 return false;
5114
5115 new_expr = NULL;
5116
5117 switch (expr->value.function.isym->id)
5118 {
5119 case GFC_ISYM_LEN:
5120 /* TODO figure out why this condition is necessary. */
5121 if (sym->attr.function
5122 && (arg1->ts.u.cl->length == NULL
5123 || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
5124 && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
5125 return false;
5126
5127 new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
5128 break;
5129
5130 case GFC_ISYM_LEN_TRIM:
5131 new_expr = gfc_copy_expr (arg1);
5132 gfc_apply_interface_mapping_to_expr (mapping, new_expr);
5133
5134 if (!new_expr)
5135 return false;
5136
5137 gfc_replace_expr (arg1, new_expr);
5138 return true;
5139
5140 case GFC_ISYM_SIZE:
5141 if (!sym->as || sym->as->rank == 0)
5142 return false;
5143
5144 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
5145 {
5146 dup = mpz_get_si (arg2->value.integer);
5147 d = dup - 1;
5148 }
5149 else
5150 {
5151 dup = sym->as->rank;
5152 d = 0;
5153 }
5154
5155 for (; d < dup; d++)
5156 {
5157 gfc_expr *tmp;
5158
5159 if (!sym->as->upper[d] || !sym->as->lower[d])
5160 {
5161 gfc_free_expr (new_expr);
5162 return false;
5163 }
5164
5165 tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
5166 gfc_get_int_expr (gfc_default_integer_kind,
5167 NULL, 1));
5168 tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
5169 if (new_expr)
5170 new_expr = gfc_multiply (new_expr, tmp);
5171 else
5172 new_expr = tmp;
5173 }
5174 break;
5175
5176 case GFC_ISYM_LBOUND:
5177 case GFC_ISYM_UBOUND:
5178 /* TODO These implementations of lbound and ubound do not limit if
5179 the size < 0, according to F95's 13.14.53 and 13.14.113. */
5180
5181 if (!sym->as || sym->as->rank == 0)
5182 return false;
5183
5184 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
5185 d = mpz_get_si (arg2->value.integer) - 1;
5186 else
5187 return false;
5188
5189 if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
5190 {
5191 if (sym->as->lower[d])
5192 new_expr = gfc_copy_expr (sym->as->lower[d]);
5193 }
5194 else
5195 {
5196 if (sym->as->upper[d])
5197 new_expr = gfc_copy_expr (sym->as->upper[d]);
5198 }
5199 break;
5200
5201 default:
5202 break;
5203 }
5204
5205 gfc_apply_interface_mapping_to_expr (mapping, new_expr);
5206 if (!new_expr)
5207 return false;
5208
5209 gfc_replace_expr (expr, new_expr);
5210 return true;
5211 }
5212
5213
5214 static void
5215 gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
5216 gfc_interface_mapping * mapping)
5217 {
5218 gfc_formal_arglist *f;
5219 gfc_actual_arglist *actual;
5220
5221 actual = expr->value.function.actual;
5222 f = gfc_sym_get_dummy_args (map_expr->symtree->n.sym);
5223
5224 for (; f && actual; f = f->next, actual = actual->next)
5225 {
5226 if (!actual->expr)
5227 continue;
5228
5229 gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
5230 }
5231
5232 if (map_expr->symtree->n.sym->attr.dimension)
5233 {
5234 int d;
5235 gfc_array_spec *as;
5236
5237 as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
5238
5239 for (d = 0; d < as->rank; d++)
5240 {
5241 gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
5242 gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
5243 }
5244
5245 expr->value.function.esym->as = as;
5246 }
5247
5248 if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
5249 {
5250 expr->value.function.esym->ts.u.cl->length
5251 = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
5252
5253 gfc_apply_interface_mapping_to_expr (mapping,
5254 expr->value.function.esym->ts.u.cl->length);
5255 }
5256 }
5257
5258
5259 /* EXPR is a copy of an expression that appeared in the interface
5260 associated with MAPPING. Walk it recursively looking for references to
5261 dummy arguments that MAPPING maps to actual arguments. Replace each such
5262 reference with a reference to the associated actual argument. */
5263
5264 static void
5265 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
5266 gfc_expr * expr)
5267 {
5268 gfc_interface_sym_mapping *sym;
5269 gfc_actual_arglist *actual;
5270
5271 if (!expr)
5272 return;
5273
5274 /* Copying an expression does not copy its length, so do that here. */
5275 if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
5276 {
5277 expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
5278 gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
5279 }
5280
5281 /* Apply the mapping to any references. */
5282 gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
5283
5284 /* ...and to the expression's symbol, if it has one. */
5285 /* TODO Find out why the condition on expr->symtree had to be moved into
5286 the loop rather than being outside it, as originally. */
5287 for (sym = mapping->syms; sym; sym = sym->next)
5288 if (expr->symtree && !strcmp (sym->old->name, expr->symtree->n.sym->name))
5289 {
5290 if (sym->new_sym->n.sym->backend_decl)
5291 expr->symtree = sym->new_sym;
5292 else if (sym->expr)
5293 gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
5294 }
5295
5296 /* ...and to subexpressions in expr->value. */
5297 switch (expr->expr_type)
5298 {
5299 case EXPR_VARIABLE:
5300 case EXPR_CONSTANT:
5301 case EXPR_NULL:
5302 case EXPR_SUBSTRING:
5303 break;
5304
5305 case EXPR_OP:
5306 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
5307 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
5308 break;
5309
5310 case EXPR_FUNCTION:
5311 for (actual = expr->value.function.actual; actual; actual = actual->next)
5312 gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
5313
5314 if (expr->value.function.esym == NULL
5315 && expr->value.function.isym != NULL
5316 && expr->value.function.actual
5317 && expr->value.function.actual->expr
5318 && expr->value.function.actual->expr->symtree
5319 && gfc_map_intrinsic_function (expr, mapping))
5320 break;
5321
5322 for (sym = mapping->syms; sym; sym = sym->next)
5323 if (sym->old == expr->value.function.esym)
5324 {
5325 expr->value.function.esym = sym->new_sym->n.sym;
5326 gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
5327 expr->value.function.esym->result = sym->new_sym->n.sym;
5328 }
5329 break;
5330
5331 case EXPR_ARRAY:
5332 case EXPR_STRUCTURE:
5333 gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
5334 break;
5335
5336 case EXPR_COMPCALL:
5337 case EXPR_PPC:
5338 case EXPR_UNKNOWN:
5339 gcc_unreachable ();
5340 break;
5341 }
5342
5343 return;
5344 }
5345
5346
5347 /* Evaluate interface expression EXPR using MAPPING. Store the result
5348 in SE. */
5349
5350 void
5351 gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
5352 gfc_se * se, gfc_expr * expr)
5353 {
5354 expr = gfc_copy_expr (expr);
5355 gfc_apply_interface_mapping_to_expr (mapping, expr);
5356 gfc_conv_expr (se, expr);
5357 se->expr = gfc_evaluate_now (se->expr, &se->pre);
5358 gfc_free_expr (expr);
5359 }
5360
5361
5362 /* Returns a reference to a temporary array into which a component of
5363 an actual argument derived type array is copied and then returned
5364 after the function call. */
5365 void
5366 gfc_conv_subref_array_arg (gfc_se *se, gfc_expr * expr, int g77,
5367 sym_intent intent, bool formal_ptr,
5368 const gfc_symbol *fsym, const char *proc_name,
5369 gfc_symbol *sym, bool check_contiguous)
5370 {
5371 gfc_se lse;
5372 gfc_se rse;
5373 gfc_ss *lss;
5374 gfc_ss *rss;
5375 gfc_loopinfo loop;
5376 gfc_loopinfo loop2;
5377 gfc_array_info *info;
5378 tree offset;
5379 tree tmp_index;
5380 tree tmp;
5381 tree base_type;
5382 tree size;
5383 stmtblock_t body;
5384 int n;
5385 int dimen;
5386 gfc_se work_se;
5387 gfc_se *parmse;
5388 bool pass_optional;
5389 bool readonly;
5390
5391 pass_optional = fsym && fsym->attr.optional && sym && sym->attr.optional;
5392
5393 if (pass_optional || check_contiguous)
5394 {
5395 gfc_init_se (&work_se, NULL);
5396 parmse = &work_se;
5397 }
5398 else
5399 parmse = se;
5400
5401 if (gfc_option.rtcheck & GFC_RTCHECK_ARRAY_TEMPS)
5402 {
5403 /* We will create a temporary array, so let us warn. */
5404 char * msg;
5405
5406 if (fsym && proc_name)
5407 msg = xasprintf ("An array temporary was created for argument "
5408 "'%s' of procedure '%s'", fsym->name, proc_name);
5409 else
5410 msg = xasprintf ("An array temporary was created");
5411
5412 tmp = build_int_cst (logical_type_node, 1);
5413 gfc_trans_runtime_check (false, true, tmp, &parmse->pre,
5414 &expr->where, msg);
5415 free (msg);
5416 }
5417
5418 gfc_init_se (&lse, NULL);
5419 gfc_init_se (&rse, NULL);
5420
5421 /* Walk the argument expression. */
5422 rss = gfc_walk_expr (expr);
5423
5424 gcc_assert (rss != gfc_ss_terminator);
5425
5426 /* Initialize the scalarizer. */
5427 gfc_init_loopinfo (&loop);
5428 gfc_add_ss_to_loop (&loop, rss);
5429
5430 /* Calculate the bounds of the scalarization. */
5431 gfc_conv_ss_startstride (&loop);
5432
5433 /* Build an ss for the temporary. */
5434 if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
5435 gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
5436
5437 base_type = gfc_typenode_for_spec (&expr->ts);
5438 if (GFC_ARRAY_TYPE_P (base_type)
5439 || GFC_DESCRIPTOR_TYPE_P (base_type))
5440 base_type = gfc_get_element_type (base_type);
5441
5442 if (expr->ts.type == BT_CLASS)
5443 base_type = gfc_typenode_for_spec (&CLASS_DATA (expr)->ts);
5444
5445 loop.temp_ss = gfc_get_temp_ss (base_type, ((expr->ts.type == BT_CHARACTER)
5446 ? expr->ts.u.cl->backend_decl
5447 : NULL),
5448 loop.dimen);
5449
5450 parmse->string_length = loop.temp_ss->info->string_length;
5451
5452 /* Associate the SS with the loop. */
5453 gfc_add_ss_to_loop (&loop, loop.temp_ss);
5454
5455 /* Setup the scalarizing loops. */
5456 gfc_conv_loop_setup (&loop, &expr->where);
5457
5458 /* Pass the temporary descriptor back to the caller. */
5459 info = &loop.temp_ss->info->data.array;
5460 parmse->expr = info->descriptor;
5461
5462 /* Setup the gfc_se structures. */
5463 gfc_copy_loopinfo_to_se (&lse, &loop);
5464 gfc_copy_loopinfo_to_se (&rse, &loop);
5465
5466 rse.ss = rss;
5467 lse.ss = loop.temp_ss;
5468 gfc_mark_ss_chain_used (rss, 1);
5469 gfc_mark_ss_chain_used (loop.temp_ss, 1);
5470
5471 /* Start the scalarized loop body. */
5472 gfc_start_scalarized_body (&loop, &body);
5473
5474 /* Translate the expression. */
5475 gfc_conv_expr (&rse, expr);
5476
5477 gfc_conv_tmp_array_ref (&lse);
5478
5479 if (intent != INTENT_OUT)
5480 {
5481 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false);
5482 gfc_add_expr_to_block (&body, tmp);
5483 gcc_assert (rse.ss == gfc_ss_terminator);
5484 gfc_trans_scalarizing_loops (&loop, &body);
5485 }
5486 else
5487 {
5488 /* Make sure that the temporary declaration survives by merging
5489 all the loop declarations into the current context. */
5490 for (n = 0; n < loop.dimen; n++)
5491 {
5492 gfc_merge_block_scope (&body);
5493 body = loop.code[loop.order[n]];
5494 }
5495 gfc_merge_block_scope (&body);
5496 }
5497
5498 /* Add the post block after the second loop, so that any
5499 freeing of allocated memory is done at the right time. */
5500 gfc_add_block_to_block (&parmse->pre, &loop.pre);
5501
5502 /**********Copy the temporary back again.*********/
5503
5504 gfc_init_se (&lse, NULL);
5505 gfc_init_se (&rse, NULL);
5506
5507 /* Walk the argument expression. */
5508 lss = gfc_walk_expr (expr);
5509 rse.ss = loop.temp_ss;
5510 lse.ss = lss;
5511
5512 /* Initialize the scalarizer. */
5513 gfc_init_loopinfo (&loop2);
5514 gfc_add_ss_to_loop (&loop2, lss);
5515
5516 dimen = rse.ss->dimen;
5517
5518 /* Skip the write-out loop for this case. */
5519 if (gfc_is_class_array_function (expr))
5520 goto class_array_fcn;
5521
5522 /* Calculate the bounds of the scalarization. */
5523 gfc_conv_ss_startstride (&loop2);
5524
5525 /* Setup the scalarizing loops. */
5526 gfc_conv_loop_setup (&loop2, &expr->where);
5527
5528 gfc_copy_loopinfo_to_se (&lse, &loop2);
5529 gfc_copy_loopinfo_to_se (&rse, &loop2);
5530
5531 gfc_mark_ss_chain_used (lss, 1);
5532 gfc_mark_ss_chain_used (loop.temp_ss, 1);
5533
5534 /* Declare the variable to hold the temporary offset and start the
5535 scalarized loop body. */
5536 offset = gfc_create_var (gfc_array_index_type, NULL);
5537 gfc_start_scalarized_body (&loop2, &body);
5538
5539 /* Build the offsets for the temporary from the loop variables. The
5540 temporary array has lbounds of zero and strides of one in all
5541 dimensions, so this is very simple. The offset is only computed
5542 outside the innermost loop, so the overall transfer could be
5543 optimized further. */
5544 info = &rse.ss->info->data.array;
5545
5546 tmp_index = gfc_index_zero_node;
5547 for (n = dimen - 1; n > 0; n--)
5548 {
5549 tree tmp_str;
5550 tmp = rse.loop->loopvar[n];
5551 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5552 tmp, rse.loop->from[n]);
5553 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5554 tmp, tmp_index);
5555
5556 tmp_str = fold_build2_loc (input_location, MINUS_EXPR,
5557 gfc_array_index_type,
5558 rse.loop->to[n-1], rse.loop->from[n-1]);
5559 tmp_str = fold_build2_loc (input_location, PLUS_EXPR,
5560 gfc_array_index_type,
5561 tmp_str, gfc_index_one_node);
5562
5563 tmp_index = fold_build2_loc (input_location, MULT_EXPR,
5564 gfc_array_index_type, tmp, tmp_str);
5565 }
5566
5567 tmp_index = fold_build2_loc (input_location, MINUS_EXPR,
5568 gfc_array_index_type,
5569 tmp_index, rse.loop->from[0]);
5570 gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
5571
5572 tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
5573 gfc_array_index_type,
5574 rse.loop->loopvar[0], offset);
5575
5576 /* Now use the offset for the reference. */
5577 tmp = build_fold_indirect_ref_loc (input_location,
5578 info->data);
5579 rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
5580
5581 if (expr->ts.type == BT_CHARACTER)
5582 rse.string_length = expr->ts.u.cl->backend_decl;
5583
5584 gfc_conv_expr (&lse, expr);
5585
5586 gcc_assert (lse.ss == gfc_ss_terminator);
5587
5588 /* Do not do deallocations when we are looking at a g77-style argument. */
5589
5590 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, !g77);
5591 gfc_add_expr_to_block (&body, tmp);
5592
5593 /* Generate the copying loops. */
5594 gfc_trans_scalarizing_loops (&loop2, &body);
5595
5596 /* Wrap the whole thing up by adding the second loop to the post-block
5597 and following it by the post-block of the first loop. In this way,
5598 if the temporary needs freeing, it is done after use!
5599 If input expr is read-only, e.g. a PARAMETER array, copying back
5600 modified values is undefined behavior. */
5601 readonly = (expr->expr_type == EXPR_VARIABLE
5602 && expr->symtree
5603 && expr->symtree->n.sym->attr.flavor == FL_PARAMETER);
5604
5605 if ((intent != INTENT_IN) && !readonly)
5606 {
5607 gfc_add_block_to_block (&parmse->post, &loop2.pre);
5608 gfc_add_block_to_block (&parmse->post, &loop2.post);
5609 }
5610
5611 class_array_fcn:
5612
5613 gfc_add_block_to_block (&parmse->post, &loop.post);
5614
5615 gfc_cleanup_loop (&loop);
5616 gfc_cleanup_loop (&loop2);
5617
5618 /* Pass the string length to the argument expression. */
5619 if (expr->ts.type == BT_CHARACTER)
5620 parmse->string_length = expr->ts.u.cl->backend_decl;
5621
5622 /* Determine the offset for pointer formal arguments and set the
5623 lbounds to one. */
5624 if (formal_ptr)
5625 {
5626 size = gfc_index_one_node;
5627 offset = gfc_index_zero_node;
5628 for (n = 0; n < dimen; n++)
5629 {
5630 tmp = gfc_conv_descriptor_ubound_get (parmse->expr,
5631 gfc_rank_cst[n]);
5632 tmp = fold_build2_loc (input_location, PLUS_EXPR,
5633 gfc_array_index_type, tmp,
5634 gfc_index_one_node);
5635 gfc_conv_descriptor_ubound_set (&parmse->pre,
5636 parmse->expr,
5637 gfc_rank_cst[n],
5638 tmp);
5639 gfc_conv_descriptor_lbound_set (&parmse->pre,
5640 parmse->expr,
5641 gfc_rank_cst[n],
5642 gfc_index_one_node);
5643 size = gfc_evaluate_now (size, &parmse->pre);
5644 offset = fold_build2_loc (input_location, MINUS_EXPR,
5645 gfc_array_index_type,
5646 offset, size);
5647 offset = gfc_evaluate_now (offset, &parmse->pre);
5648 tmp = fold_build2_loc (input_location, MINUS_EXPR,
5649 gfc_array_index_type,
5650 rse.loop->to[n], rse.loop->from[n]);
5651 tmp = fold_build2_loc (input_location, PLUS_EXPR,
5652 gfc_array_index_type,
5653 tmp, gfc_index_one_node);
5654 size = fold_build2_loc (input_location, MULT_EXPR,
5655 gfc_array_index_type, size, tmp);
5656 }
5657
5658 gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
5659 offset);
5660 }
5661
5662 /* We want either the address for the data or the address of the descriptor,
5663 depending on the mode of passing array arguments. */
5664 if (g77)
5665 parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
5666 else
5667 parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
5668
5669 /* Basically make this into
5670
5671 if (present)
5672 {
5673 if (contiguous)
5674 {
5675 pointer = a;
5676 }
5677 else
5678 {
5679 parmse->pre();
5680 pointer = parmse->expr;
5681 }
5682 }
5683 else
5684 pointer = NULL;
5685
5686 foo (pointer);
5687 if (present && !contiguous)
5688 se->post();
5689
5690 */
5691
5692 if (pass_optional || check_contiguous)
5693 {
5694 tree type;
5695 stmtblock_t else_block;
5696 tree pre_stmts, post_stmts;
5697 tree pointer;
5698 tree else_stmt;
5699 tree present_var = NULL_TREE;
5700 tree cont_var = NULL_TREE;
5701 tree post_cond;
5702
5703 type = TREE_TYPE (parmse->expr);
5704 if (POINTER_TYPE_P (type) && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
5705 type = TREE_TYPE (type);
5706 pointer = gfc_create_var (type, "arg_ptr");
5707
5708 if (check_contiguous)
5709 {
5710 gfc_se cont_se, array_se;
5711 stmtblock_t if_block, else_block;
5712 tree if_stmt, else_stmt;
5713 mpz_t size;
5714 bool size_set;
5715
5716 cont_var = gfc_create_var (boolean_type_node, "contiguous");
5717
5718 /* If the size is known to be one at compile-time, set
5719 cont_var to true unconditionally. This may look
5720 inelegant, but we're only doing this during
5721 optimization, so the statements will be optimized away,
5722 and this saves complexity here. */
5723
5724 size_set = gfc_array_size (expr, &size);
5725 if (size_set && mpz_cmp_ui (size, 1) == 0)
5726 {
5727 gfc_add_modify (&se->pre, cont_var,
5728 build_one_cst (boolean_type_node));
5729 }
5730 else
5731 {
5732 /* cont_var = is_contiguous (expr); . */
5733 gfc_init_se (&cont_se, parmse);
5734 gfc_conv_is_contiguous_expr (&cont_se, expr);
5735 gfc_add_block_to_block (&se->pre, &(&cont_se)->pre);
5736 gfc_add_modify (&se->pre, cont_var, cont_se.expr);
5737 gfc_add_block_to_block (&se->pre, &(&cont_se)->post);
5738 }
5739
5740 if (size_set)
5741 mpz_clear (size);
5742
5743 /* arrayse->expr = descriptor of a. */
5744 gfc_init_se (&array_se, se);
5745 gfc_conv_expr_descriptor (&array_se, expr);
5746 gfc_add_block_to_block (&se->pre, &(&array_se)->pre);
5747 gfc_add_block_to_block (&se->pre, &(&array_se)->post);
5748
5749 /* if_stmt = { descriptor ? pointer = a : pointer = &a[0]; } . */
5750 gfc_init_block (&if_block);
5751 if (GFC_DESCRIPTOR_TYPE_P (type))
5752 gfc_add_modify (&if_block, pointer, array_se.expr);
5753 else
5754 {
5755 tmp = gfc_conv_array_data (array_se.expr);
5756 tmp = fold_convert (type, tmp);
5757 gfc_add_modify (&if_block, pointer, tmp);
5758 }
5759 if_stmt = gfc_finish_block (&if_block);
5760
5761 /* else_stmt = { parmse->pre(); pointer = parmse->expr; } . */
5762 gfc_init_block (&else_block);
5763 gfc_add_block_to_block (&else_block, &parmse->pre);
5764 tmp = (GFC_DESCRIPTOR_TYPE_P (type)
5765 ? build_fold_indirect_ref_loc (input_location, parmse->expr)
5766 : parmse->expr);
5767 gfc_add_modify (&else_block, pointer, tmp);
5768 else_stmt = gfc_finish_block (&else_block);
5769
5770 /* And put the above into an if statement. */
5771 pre_stmts = fold_build3_loc (input_location, COND_EXPR, void_type_node,
5772 gfc_likely (cont_var,
5773 PRED_FORTRAN_CONTIGUOUS),
5774 if_stmt, else_stmt);
5775 }
5776 else
5777 {
5778 /* pointer = pramse->expr; . */
5779 gfc_add_modify (&parmse->pre, pointer, parmse->expr);
5780 pre_stmts = gfc_finish_block (&parmse->pre);
5781 }
5782
5783 if (pass_optional)
5784 {
5785 present_var = gfc_create_var (boolean_type_node, "present");
5786
5787 /* present_var = present(sym); . */
5788 tmp = gfc_conv_expr_present (sym);
5789 tmp = fold_convert (boolean_type_node, tmp);
5790 gfc_add_modify (&se->pre, present_var, tmp);
5791
5792 /* else_stmt = { pointer = NULL; } . */
5793 gfc_init_block (&else_block);
5794 if (GFC_DESCRIPTOR_TYPE_P (type))
5795 gfc_conv_descriptor_data_set (&else_block, pointer,
5796 null_pointer_node);
5797 else
5798 gfc_add_modify (&else_block, pointer, build_int_cst (type, 0));
5799 else_stmt = gfc_finish_block (&else_block);
5800
5801 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
5802 gfc_likely (present_var,
5803 PRED_FORTRAN_ABSENT_DUMMY),
5804 pre_stmts, else_stmt);
5805 gfc_add_expr_to_block (&se->pre, tmp);
5806 }
5807 else
5808 gfc_add_expr_to_block (&se->pre, pre_stmts);
5809
5810 post_stmts = gfc_finish_block (&parmse->post);
5811
5812 /* Put together the post stuff, plus the optional
5813 deallocation. */
5814 if (check_contiguous)
5815 {
5816 /* !cont_var. */
5817 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
5818 cont_var,
5819 build_zero_cst (boolean_type_node));
5820 tmp = gfc_unlikely (tmp, PRED_FORTRAN_CONTIGUOUS);
5821
5822 if (pass_optional)
5823 {
5824 tree present_likely = gfc_likely (present_var,
5825 PRED_FORTRAN_ABSENT_DUMMY);
5826 post_cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
5827 boolean_type_node, present_likely,
5828 tmp);
5829 }
5830 else
5831 post_cond = tmp;
5832 }
5833 else
5834 {
5835 gcc_assert (pass_optional);
5836 post_cond = present_var;
5837 }
5838
5839 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, post_cond,
5840 post_stmts, build_empty_stmt (input_location));
5841 gfc_add_expr_to_block (&se->post, tmp);
5842 if (GFC_DESCRIPTOR_TYPE_P (type))
5843 {
5844 type = TREE_TYPE (parmse->expr);
5845 if (POINTER_TYPE_P (type))
5846 {
5847 pointer = gfc_build_addr_expr (type, pointer);
5848 if (pass_optional)
5849 {
5850 tmp = gfc_likely (present_var, PRED_FORTRAN_ABSENT_DUMMY);
5851 pointer = fold_build3_loc (input_location, COND_EXPR, type,
5852 tmp, pointer,
5853 fold_convert (type,
5854 null_pointer_node));
5855 }
5856 }
5857 else
5858 gcc_assert (!pass_optional);
5859 }
5860 se->expr = pointer;
5861 }
5862
5863 return;
5864 }
5865
5866
5867 /* Generate the code for argument list functions. */
5868
5869 static void
5870 conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
5871 {
5872 /* Pass by value for g77 %VAL(arg), pass the address
5873 indirectly for %LOC, else by reference. Thus %REF
5874 is a "do-nothing" and %LOC is the same as an F95
5875 pointer. */
5876 if (strcmp (name, "%VAL") == 0)
5877 gfc_conv_expr (se, expr);
5878 else if (strcmp (name, "%LOC") == 0)
5879 {
5880 gfc_conv_expr_reference (se, expr);
5881 se->expr = gfc_build_addr_expr (NULL, se->expr);
5882 }
5883 else if (strcmp (name, "%REF") == 0)
5884 gfc_conv_expr_reference (se, expr);
5885 else
5886 gfc_error ("Unknown argument list function at %L", &expr->where);
5887 }
5888
5889
5890 /* This function tells whether the middle-end representation of the expression
5891 E given as input may point to data otherwise accessible through a variable
5892 (sub-)reference.
5893 It is assumed that the only expressions that may alias are variables,
5894 and array constructors if ARRAY_MAY_ALIAS is true and some of its elements
5895 may alias.
5896 This function is used to decide whether freeing an expression's allocatable
5897 components is safe or should be avoided.
5898
5899 If ARRAY_MAY_ALIAS is true, an array constructor may alias if some of
5900 its elements are copied from a variable. This ARRAY_MAY_ALIAS trick
5901 is necessary because for array constructors, aliasing depends on how
5902 the array is used:
5903 - If E is an array constructor used as argument to an elemental procedure,
5904 the array, which is generated through shallow copy by the scalarizer,
5905 is used directly and can alias the expressions it was copied from.
5906 - If E is an array constructor used as argument to a non-elemental
5907 procedure,the scalarizer is used in gfc_conv_expr_descriptor to generate
5908 the array as in the previous case, but then that array is used
5909 to initialize a new descriptor through deep copy. There is no alias
5910 possible in that case.
5911 Thus, the ARRAY_MAY_ALIAS flag is necessary to distinguish the two cases
5912 above. */
5913
5914 static bool
5915 expr_may_alias_variables (gfc_expr *e, bool array_may_alias)
5916 {
5917 gfc_constructor *c;
5918
5919 if (e->expr_type == EXPR_VARIABLE)
5920 return true;
5921 else if (e->expr_type == EXPR_FUNCTION)
5922 {
5923 gfc_symbol *proc_ifc = gfc_get_proc_ifc_for_expr (e);
5924
5925 if (proc_ifc->result != NULL
5926 && ((proc_ifc->result->ts.type == BT_CLASS
5927 && proc_ifc->result->ts.u.derived->attr.is_class
5928 && CLASS_DATA (proc_ifc->result)->attr.class_pointer)
5929 || proc_ifc->result->attr.pointer))
5930 return true;
5931 else
5932 return false;
5933 }
5934 else if (e->expr_type != EXPR_ARRAY || !array_may_alias)
5935 return false;
5936
5937 for (c = gfc_constructor_first (e->value.constructor);
5938 c; c = gfc_constructor_next (c))
5939 if (c->expr
5940 && expr_may_alias_variables (c->expr, array_may_alias))
5941 return true;
5942
5943 return false;
5944 }
5945
5946
5947 /* A helper function to set the dtype for unallocated or unassociated
5948 entities. */
5949
5950 static void
5951 set_dtype_for_unallocated (gfc_se *parmse, gfc_expr *e)
5952 {
5953 tree tmp;
5954 tree desc;
5955 tree cond;
5956 tree type;
5957 stmtblock_t block;
5958
5959 /* TODO Figure out how to handle optional dummies. */
5960 if (e && e->expr_type == EXPR_VARIABLE
5961 && e->symtree->n.sym->attr.optional)
5962 return;
5963
5964 desc = parmse->expr;
5965 if (desc == NULL_TREE)
5966 return;
5967
5968 if (POINTER_TYPE_P (TREE_TYPE (desc)))
5969 desc = build_fold_indirect_ref_loc (input_location, desc);
5970 if (GFC_CLASS_TYPE_P (TREE_TYPE (desc)))
5971 desc = gfc_class_data_get (desc);
5972 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
5973 return;
5974
5975 gfc_init_block (&block);
5976 tmp = gfc_conv_descriptor_data_get (desc);
5977 cond = fold_build2_loc (input_location, EQ_EXPR,
5978 logical_type_node, tmp,
5979 build_int_cst (TREE_TYPE (tmp), 0));
5980 tmp = gfc_conv_descriptor_dtype (desc);
5981 type = gfc_get_element_type (TREE_TYPE (desc));
5982 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
5983 TREE_TYPE (tmp), tmp,
5984 gfc_get_dtype_rank_type (e->rank, type));
5985 gfc_add_expr_to_block (&block, tmp);
5986 cond = build3_v (COND_EXPR, cond,
5987 gfc_finish_block (&block),
5988 build_empty_stmt (input_location));
5989 gfc_add_expr_to_block (&parmse->pre, cond);
5990 }
5991
5992
5993
5994 /* Provide an interface between gfortran array descriptors and the F2018:18.4
5995 ISO_Fortran_binding array descriptors. */
5996
5997 static void
5998 gfc_conv_gfc_desc_to_cfi_desc (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym)
5999 {
6000 stmtblock_t block, block2;
6001 tree cfi, gfc, tmp, tmp2;
6002 tree present = NULL;
6003 tree gfc_strlen = NULL;
6004 tree rank;
6005 gfc_se se;
6006
6007 if (fsym->attr.optional
6008 && e->expr_type == EXPR_VARIABLE
6009 && e->symtree->n.sym->attr.optional)
6010 present = gfc_conv_expr_present (e->symtree->n.sym);
6011
6012 gfc_init_block (&block);
6013
6014 /* Convert original argument to a tree. */
6015 gfc_init_se (&se, NULL);
6016 if (e->rank == 0)
6017 {
6018 se.want_pointer = 1;
6019 gfc_conv_expr (&se, e);
6020 gfc = se.expr;
6021 /* gfc_conv_constant ignores se.want_poiner, e.g. for string_cst. */
6022 if (!POINTER_TYPE_P (TREE_TYPE (gfc)))
6023 gfc = gfc_build_addr_expr (NULL, gfc);
6024 }
6025 else
6026 {
6027 /* If the actual argument can be noncontiguous, copy-in/out is required,
6028 if the dummy has either the CONTIGUOUS attribute or is an assumed-
6029 length assumed-length/assumed-size CHARACTER array. This only
6030 applies if the actual argument is a "variable"; if it's some
6031 non-lvalue expression, we are going to evaluate it to a
6032 temporary below anyway. */
6033 se.force_no_tmp = 1;
6034 if ((fsym->attr.contiguous
6035 || (fsym->ts.type == BT_CHARACTER && !fsym->ts.u.cl->length
6036 && (fsym->as->type == AS_ASSUMED_SIZE
6037 || fsym->as->type == AS_EXPLICIT)))
6038 && !gfc_is_simply_contiguous (e, false, true)
6039 && gfc_expr_is_variable (e))
6040 {
6041 bool optional = fsym->attr.optional;
6042 fsym->attr.optional = 0;
6043 gfc_conv_subref_array_arg (&se, e, false, fsym->attr.intent,
6044 fsym->attr.pointer, fsym,
6045 fsym->ns->proc_name->name, NULL,
6046 /* check_contiguous= */ true);
6047 fsym->attr.optional = optional;
6048 }
6049 else
6050 gfc_conv_expr_descriptor (&se, e);
6051 gfc = se.expr;
6052 /* For dt(:)%var the elem_len*stride != sm, hence, GFC uses
6053 elem_len = sizeof(dt) and base_addr = dt(lb) instead.
6054 gfc_get_dataptr_offset fixes the base_addr; for elem_len, see below.
6055 While sm is fine as it uses span*stride and not elem_len. */
6056 if (POINTER_TYPE_P (TREE_TYPE (gfc)))
6057 gfc = build_fold_indirect_ref_loc (input_location, gfc);
6058 else if (is_subref_array (e) && e->ts.type != BT_CHARACTER)
6059 gfc_get_dataptr_offset (&se.pre, gfc, gfc, NULL, true, e);
6060 }
6061 if (e->ts.type == BT_CHARACTER)
6062 {
6063 if (se.string_length)
6064 gfc_strlen = se.string_length;
6065 else if (e->ts.u.cl->backend_decl)
6066 gfc_strlen = e->ts.u.cl->backend_decl;
6067 else
6068 gcc_unreachable ();
6069 }
6070 gfc_add_block_to_block (&block, &se.pre);
6071
6072 /* Create array descriptor and set version, rank, attribute, type. */
6073 cfi = gfc_create_var (gfc_get_cfi_type (e->rank < 0
6074 ? GFC_MAX_DIMENSIONS : e->rank,
6075 false), "cfi");
6076 /* Convert to CFI_cdesc_t, which has dim[] to avoid TBAA issues,*/
6077 if (fsym->attr.dimension && fsym->as->type == AS_ASSUMED_RANK)
6078 {
6079 tmp = gfc_get_cfi_type (-1, !fsym->attr.pointer && !fsym->attr.target);
6080 tmp = build_pointer_type (tmp);
6081 parmse->expr = cfi = gfc_build_addr_expr (tmp, cfi);
6082 cfi = build_fold_indirect_ref_loc (input_location, cfi);
6083 }
6084 else
6085 parmse->expr = gfc_build_addr_expr (NULL, cfi);
6086
6087 tmp = gfc_get_cfi_desc_version (cfi);
6088 gfc_add_modify (&block, tmp,
6089 build_int_cst (TREE_TYPE (tmp), CFI_VERSION));
6090 if (e->rank < 0)
6091 rank = fold_convert (signed_char_type_node, gfc_conv_descriptor_rank (gfc));
6092 else
6093 rank = build_int_cst (signed_char_type_node, e->rank);
6094 tmp = gfc_get_cfi_desc_rank (cfi);
6095 gfc_add_modify (&block, tmp, rank);
6096 int itype = CFI_type_other;
6097 if (e->ts.f90_type == BT_VOID)
6098 itype = (e->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
6099 ? CFI_type_cfunptr : CFI_type_cptr);
6100 else
6101 {
6102 if (e->expr_type == EXPR_NULL && e->ts.type == BT_UNKNOWN)
6103 e->ts = fsym->ts;
6104 switch (e->ts.type)
6105 {
6106 case BT_INTEGER:
6107 case BT_LOGICAL:
6108 case BT_REAL:
6109 case BT_COMPLEX:
6110 itype = CFI_type_from_type_kind (e->ts.type, e->ts.kind);
6111 break;
6112 case BT_CHARACTER:
6113 itype = CFI_type_from_type_kind (CFI_type_Character, e->ts.kind);
6114 break;
6115 case BT_DERIVED:
6116 itype = CFI_type_struct;
6117 break;
6118 case BT_VOID:
6119 itype = (e->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
6120 ? CFI_type_cfunptr : CFI_type_cptr);
6121 break;
6122 case BT_ASSUMED:
6123 itype = CFI_type_other; // FIXME: Or CFI_type_cptr ?
6124 break;
6125 case BT_CLASS:
6126 if (fsym->ts.type == BT_ASSUMED)
6127 {
6128 // F2017: 7.3.2.2: "An entity that is declared using the TYPE(*)
6129 // type specifier is assumed-type and is an unlimited polymorphic
6130 // entity." The actual argument _data component is passed.
6131 itype = CFI_type_other; // FIXME: Or CFI_type_cptr ?
6132 break;
6133 }
6134 else
6135 gcc_unreachable ();
6136
6137 case BT_UNSIGNED:
6138 gfc_internal_error ("Unsigned not yet implemented");
6139
6140 case BT_PROCEDURE:
6141 case BT_HOLLERITH:
6142 case BT_UNION:
6143 case BT_BOZ:
6144 case BT_UNKNOWN:
6145 // FIXME: Really unreachable? Or reachable for type(*) ? If so, CFI_type_other?
6146 gcc_unreachable ();
6147 }
6148 }
6149
6150 tmp = gfc_get_cfi_desc_type (cfi);
6151 gfc_add_modify (&block, tmp,
6152 build_int_cst (TREE_TYPE (tmp), itype));
6153
6154 int attr = CFI_attribute_other;
6155 if (fsym->attr.pointer)
6156 attr = CFI_attribute_pointer;
6157 else if (fsym->attr.allocatable)
6158 attr = CFI_attribute_allocatable;
6159 tmp = gfc_get_cfi_desc_attribute (cfi);
6160 gfc_add_modify (&block, tmp,
6161 build_int_cst (TREE_TYPE (tmp), attr));
6162
6163 /* The cfi-base_addr assignment could be skipped for 'pointer, intent(out)'.
6164 That is very sensible for undefined pointers, but the C code might assume
6165 that the pointer retains the value, in particular, if it was NULL. */
6166 if (e->rank == 0)
6167 {
6168 tmp = gfc_get_cfi_desc_base_addr (cfi);
6169 gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), gfc));
6170 }
6171 else
6172 {
6173 tmp = gfc_get_cfi_desc_base_addr (cfi);
6174 tmp2 = gfc_conv_descriptor_data_get (gfc);
6175 gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), tmp2));
6176 }
6177
6178 /* Set elem_len if known - must be before the next if block.
6179 Note that allocatable implies 'len=:'. */
6180 if (e->ts.type != BT_ASSUMED && e->ts.type != BT_CHARACTER )
6181 {
6182 /* Length is known at compile time; use 'block' for it. */
6183 tmp = size_in_bytes (gfc_typenode_for_spec (&e->ts));
6184 tmp2 = gfc_get_cfi_desc_elem_len (cfi);
6185 gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
6186 }
6187
6188 if (fsym->attr.pointer && fsym->attr.intent == INTENT_OUT)
6189 goto done;
6190
6191 /* When allocatable + intent out, free the cfi descriptor. */
6192 if (fsym->attr.allocatable && fsym->attr.intent == INTENT_OUT)
6193 {
6194 tmp = gfc_get_cfi_desc_base_addr (cfi);
6195 tree call = builtin_decl_explicit (BUILT_IN_FREE);
6196 call = build_call_expr_loc (input_location, call, 1, tmp);
6197 gfc_add_expr_to_block (&block, fold_convert (void_type_node, call));
6198 gfc_add_modify (&block, tmp,
6199 fold_convert (TREE_TYPE (tmp), null_pointer_node));
6200 goto done;
6201 }
6202
6203 /* If not unallocated/unassociated. */
6204 gfc_init_block (&block2);
6205
6206 /* Set elem_len, which may be only known at run time. */
6207 if (e->ts.type == BT_CHARACTER
6208 && (e->expr_type != EXPR_NULL || gfc_strlen != NULL_TREE))
6209 {
6210 gcc_assert (gfc_strlen);
6211 tmp = gfc_strlen;
6212 if (e->ts.kind != 1)
6213 tmp = fold_build2_loc (input_location, MULT_EXPR,
6214 gfc_charlen_type_node, tmp,
6215 build_int_cst (gfc_charlen_type_node,
6216 e->ts.kind));
6217 tmp2 = gfc_get_cfi_desc_elem_len (cfi);
6218 gfc_add_modify (&block2, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
6219 }
6220 else if (e->ts.type == BT_ASSUMED)
6221 {
6222 tmp = gfc_conv_descriptor_elem_len (gfc);
6223 tmp2 = gfc_get_cfi_desc_elem_len (cfi);
6224 gfc_add_modify (&block2, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
6225 }
6226
6227 if (e->ts.type == BT_ASSUMED)
6228 {
6229 /* Note: type(*) implies assumed-shape/assumed-rank if fsym requires
6230 an CFI descriptor. Use the type in the descriptor as it provide
6231 mode information. (Quality of implementation feature.) */
6232 tree cond;
6233 tree ctype = gfc_get_cfi_desc_type (cfi);
6234 tree type = fold_convert (TREE_TYPE (ctype),
6235 gfc_conv_descriptor_type (gfc));
6236 tree kind = fold_convert (TREE_TYPE (ctype),
6237 gfc_conv_descriptor_elem_len (gfc));
6238 kind = fold_build2_loc (input_location, LSHIFT_EXPR, TREE_TYPE (type),
6239 kind, build_int_cst (TREE_TYPE (type),
6240 CFI_type_kind_shift));
6241
6242 /* if (BT_VOID) CFI_type_cptr else CFI_type_other */
6243 /* Note: BT_VOID is could also be CFI_type_funcptr, but assume c_ptr. */
6244 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6245 build_int_cst (TREE_TYPE (type), BT_VOID));
6246 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, ctype,
6247 build_int_cst (TREE_TYPE (type), CFI_type_cptr));
6248 tmp2 = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
6249 ctype,
6250 build_int_cst (TREE_TYPE (type), CFI_type_other));
6251 tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6252 tmp, tmp2);
6253 /* if (BT_DERIVED) CFI_type_struct else < tmp2 > */
6254 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6255 build_int_cst (TREE_TYPE (type), BT_DERIVED));
6256 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, ctype,
6257 build_int_cst (TREE_TYPE (type), CFI_type_struct));
6258 tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6259 tmp, tmp2);
6260 /* if (BT_CHARACTER) CFI_type_Character + kind=1 else < tmp2 > */
6261 /* Note: could also be kind=4, with cfi->elem_len = gfc->elem_len*4. */
6262 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6263 build_int_cst (TREE_TYPE (type), BT_CHARACTER));
6264 tmp = build_int_cst (TREE_TYPE (type),
6265 CFI_type_from_type_kind (CFI_type_Character, 1));
6266 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
6267 ctype, tmp);
6268 tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6269 tmp, tmp2);
6270 /* if (BT_COMPLEX) CFI_type_Complex + kind/2 else < tmp2 > */
6271 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6272 build_int_cst (TREE_TYPE (type), BT_COMPLEX));
6273 tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR, TREE_TYPE (type),
6274 kind, build_int_cst (TREE_TYPE (type), 2));
6275 tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (type), tmp,
6276 build_int_cst (TREE_TYPE (type),
6277 CFI_type_Complex));
6278 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
6279 ctype, tmp);
6280 tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6281 tmp, tmp2);
6282 /* if (BT_INTEGER || BT_LOGICAL || BT_REAL) type + kind else <tmp2> */
6283 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6284 build_int_cst (TREE_TYPE (type), BT_INTEGER));
6285 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6286 build_int_cst (TREE_TYPE (type), BT_LOGICAL));
6287 cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
6288 cond, tmp);
6289 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6290 build_int_cst (TREE_TYPE (type), BT_REAL));
6291 cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
6292 cond, tmp);
6293 tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (type),
6294 type, kind);
6295 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
6296 ctype, tmp);
6297 tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6298 tmp, tmp2);
6299 gfc_add_expr_to_block (&block2, tmp2);
6300 }
6301
6302 if (e->rank != 0)
6303 {
6304 /* Loop: for (i = 0; i < rank; ++i). */
6305 tree idx = gfc_create_var (TREE_TYPE (rank), "idx");
6306 /* Loop body. */
6307 stmtblock_t loop_body;
6308 gfc_init_block (&loop_body);
6309 /* cfi->dim[i].lower_bound = (allocatable/pointer)
6310 ? gfc->dim[i].lbound : 0 */
6311 if (fsym->attr.pointer || fsym->attr.allocatable)
6312 tmp = gfc_conv_descriptor_lbound_get (gfc, idx);
6313 else
6314 tmp = gfc_index_zero_node;
6315 gfc_add_modify (&loop_body, gfc_get_cfi_dim_lbound (cfi, idx), tmp);
6316 /* cfi->dim[i].extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1. */
6317 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6318 gfc_conv_descriptor_ubound_get (gfc, idx),
6319 gfc_conv_descriptor_lbound_get (gfc, idx));
6320 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
6321 tmp, gfc_index_one_node);
6322 gfc_add_modify (&loop_body, gfc_get_cfi_dim_extent (cfi, idx), tmp);
6323 /* d->dim[n].sm = gfc->dim[i].stride * gfc->span); */
6324 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6325 gfc_conv_descriptor_stride_get (gfc, idx),
6326 gfc_conv_descriptor_span_get (gfc));
6327 gfc_add_modify (&loop_body, gfc_get_cfi_dim_sm (cfi, idx), tmp);
6328
6329 /* Generate loop. */
6330 gfc_simple_for_loop (&block2, idx, build_int_cst (TREE_TYPE (idx), 0),
6331 rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
6332 gfc_finish_block (&loop_body));
6333
6334 if (e->expr_type == EXPR_VARIABLE
6335 && e->ref
6336 && e->ref->u.ar.type == AR_FULL
6337 && e->symtree->n.sym->attr.dummy
6338 && e->symtree->n.sym->as
6339 && e->symtree->n.sym->as->type == AS_ASSUMED_SIZE)
6340 {
6341 tmp = gfc_get_cfi_dim_extent (cfi, gfc_rank_cst[e->rank-1]),
6342 gfc_add_modify (&block2, tmp, build_int_cst (TREE_TYPE (tmp), -1));
6343 }
6344 }
6345
6346 if (fsym->attr.allocatable || fsym->attr.pointer)
6347 {
6348 tmp = gfc_get_cfi_desc_base_addr (cfi),
6349 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
6350 tmp, null_pointer_node);
6351 tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
6352 build_empty_stmt (input_location));
6353 gfc_add_expr_to_block (&block, tmp);
6354 }
6355 else
6356 gfc_add_block_to_block (&block, &block2);
6357
6358
6359 done:
6360 if (present)
6361 {
6362 parmse->expr = build3_loc (input_location, COND_EXPR,
6363 TREE_TYPE (parmse->expr),
6364 present, parmse->expr, null_pointer_node);
6365 tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block),
6366 build_empty_stmt (input_location));
6367 gfc_add_expr_to_block (&parmse->pre, tmp);
6368 }
6369 else
6370 gfc_add_block_to_block (&parmse->pre, &block);
6371
6372 gfc_init_block (&block);
6373
6374 if ((!fsym->attr.allocatable && !fsym->attr.pointer)
6375 || fsym->attr.intent == INTENT_IN)
6376 goto post_call;
6377
6378 gfc_init_block (&block2);
6379 if (e->rank == 0)
6380 {
6381 tmp = gfc_get_cfi_desc_base_addr (cfi);
6382 gfc_add_modify (&block, gfc, fold_convert (TREE_TYPE (gfc), tmp));
6383 }
6384 else
6385 {
6386 tmp = gfc_get_cfi_desc_base_addr (cfi);
6387 gfc_conv_descriptor_data_set (&block, gfc, tmp);
6388
6389 if (fsym->attr.allocatable)
6390 {
6391 /* gfc->span = cfi->elem_len. */
6392 tmp = fold_convert (gfc_array_index_type,
6393 gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]));
6394 }
6395 else
6396 {
6397 /* gfc->span = ((cfi->dim[0].sm % cfi->elem_len)
6398 ? cfi->dim[0].sm : cfi->elem_len). */
6399 tmp = gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]);
6400 tmp2 = fold_convert (gfc_array_index_type,
6401 gfc_get_cfi_desc_elem_len (cfi));
6402 tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR,
6403 gfc_array_index_type, tmp, tmp2);
6404 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
6405 tmp, gfc_index_zero_node);
6406 tmp = build3_loc (input_location, COND_EXPR, gfc_array_index_type, tmp,
6407 gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]), tmp2);
6408 }
6409 gfc_conv_descriptor_span_set (&block2, gfc, tmp);
6410
6411 /* Calculate offset + set lbound, ubound and stride. */
6412 gfc_conv_descriptor_offset_set (&block2, gfc, gfc_index_zero_node);
6413 /* Loop: for (i = 0; i < rank; ++i). */
6414 tree idx = gfc_create_var (TREE_TYPE (rank), "idx");
6415 /* Loop body. */
6416 stmtblock_t loop_body;
6417 gfc_init_block (&loop_body);
6418 /* gfc->dim[i].lbound = ... */
6419 tmp = gfc_get_cfi_dim_lbound (cfi, idx);
6420 gfc_conv_descriptor_lbound_set (&loop_body, gfc, idx, tmp);
6421
6422 /* gfc->dim[i].ubound = gfc->dim[i].lbound + cfi->dim[i].extent - 1. */
6423 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6424 gfc_conv_descriptor_lbound_get (gfc, idx),
6425 gfc_index_one_node);
6426 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
6427 gfc_get_cfi_dim_extent (cfi, idx), tmp);
6428 gfc_conv_descriptor_ubound_set (&loop_body, gfc, idx, tmp);
6429
6430 /* gfc->dim[i].stride = cfi->dim[i].sm / cfi>elem_len */
6431 tmp = gfc_get_cfi_dim_sm (cfi, idx);
6432 tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
6433 gfc_array_index_type, tmp,
6434 fold_convert (gfc_array_index_type,
6435 gfc_get_cfi_desc_elem_len (cfi)));
6436 gfc_conv_descriptor_stride_set (&loop_body, gfc, idx, tmp);
6437
6438 /* gfc->offset -= gfc->dim[i].stride * gfc->dim[i].lbound. */
6439 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6440 gfc_conv_descriptor_stride_get (gfc, idx),
6441 gfc_conv_descriptor_lbound_get (gfc, idx));
6442 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6443 gfc_conv_descriptor_offset_get (gfc), tmp);
6444 gfc_conv_descriptor_offset_set (&loop_body, gfc, tmp);
6445 /* Generate loop. */
6446 gfc_simple_for_loop (&block2, idx, build_int_cst (TREE_TYPE (idx), 0),
6447 rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
6448 gfc_finish_block (&loop_body));
6449 }
6450
6451 if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
6452 {
6453 tmp = fold_convert (gfc_charlen_type_node,
6454 gfc_get_cfi_desc_elem_len (cfi));
6455 if (e->ts.kind != 1)
6456 tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
6457 gfc_charlen_type_node, tmp,
6458 build_int_cst (gfc_charlen_type_node,
6459 e->ts.kind));
6460 gfc_add_modify (&block2, gfc_strlen, tmp);
6461 }
6462
6463 tmp = gfc_get_cfi_desc_base_addr (cfi),
6464 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
6465 tmp, null_pointer_node);
6466 tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
6467 build_empty_stmt (input_location));
6468 gfc_add_expr_to_block (&block, tmp);
6469
6470 post_call:
6471 gfc_add_block_to_block (&block, &se.post);
6472 if (present && block.head)
6473 {
6474 tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block),
6475 build_empty_stmt (input_location));
6476 gfc_add_expr_to_block (&parmse->post, tmp);
6477 }
6478 else if (block.head)
6479 gfc_add_block_to_block (&parmse->post, &block);
6480 }
6481
6482
6483 /* Create "conditional temporary" to handle scalar dummy variables with the
6484 OPTIONAL+VALUE attribute that shall not be dereferenced. Use null value
6485 as fallback. Does not handle CLASS. */
6486
6487 static void
6488 conv_cond_temp (gfc_se * parmse, gfc_expr * e, tree cond)
6489 {
6490 tree temp;
6491 gcc_assert (e && e->ts.type != BT_CLASS);
6492 gcc_assert (e->rank == 0);
6493 temp = gfc_create_var (TREE_TYPE (parmse->expr), "condtemp");
6494 TREE_STATIC (temp) = 1;
6495 TREE_CONSTANT (temp) = 1;
6496 TREE_READONLY (temp) = 1;
6497 DECL_INITIAL (temp) = build_zero_cst (TREE_TYPE (temp));
6498 parmse->expr = fold_build3_loc (input_location, COND_EXPR,
6499 TREE_TYPE (parmse->expr),
6500 cond, parmse->expr, temp);
6501 parmse->expr = gfc_evaluate_now (parmse->expr, &parmse->pre);
6502 }
6503
6504
6505 /* Helper function for the handling of (currently) scalar dummy variables
6506 with the VALUE attribute. Argument parmse should already be set up. */
6507 static void
6508 conv_dummy_value (gfc_se * parmse, gfc_expr * e, gfc_symbol * fsym,
6509 vec<tree, va_gc> *& optionalargs)
6510 {
6511 tree tmp;
6512
6513 gcc_assert (fsym && fsym->attr.value && !fsym->attr.dimension);
6514
6515 /* Absent actual argument for optional scalar dummy. */
6516 if ((e == NULL || e->expr_type == EXPR_NULL) && fsym->attr.optional)
6517 {
6518 /* For scalar arguments with VALUE attribute which are passed by
6519 value, pass "0" and a hidden argument for the optional status. */
6520 if (fsym->ts.type == BT_CHARACTER)
6521 {
6522 /* Pass a NULL pointer for an absent CHARACTER arg and a length of
6523 zero. */
6524 parmse->expr = null_pointer_node;
6525 parmse->string_length = build_int_cst (gfc_charlen_type_node, 0);
6526 }
6527 else if (gfc_bt_struct (fsym->ts.type)
6528 && !(fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING))
6529 {
6530 /* Pass null struct. Types c_ptr and c_funptr from ISO_C_BINDING
6531 are pointers and passed as such below. */
6532 tree temp = gfc_create_var (gfc_sym_type (fsym), "absent");
6533 TREE_CONSTANT (temp) = 1;
6534 TREE_READONLY (temp) = 1;
6535 DECL_INITIAL (temp) = build_zero_cst (TREE_TYPE (temp));
6536 parmse->expr = temp;
6537 }
6538 else
6539 parmse->expr = fold_convert (gfc_sym_type (fsym),
6540 integer_zero_node);
6541 vec_safe_push (optionalargs, boolean_false_node);
6542
6543 return;
6544 }
6545
6546 /* gfortran argument passing conventions:
6547 actual arguments to CHARACTER(len=1),VALUE
6548 dummy arguments are actually passed by value.
6549 Strings are truncated to length 1. */
6550 if (gfc_length_one_character_type_p (&fsym->ts))
6551 {
6552 if (e->expr_type == EXPR_CONSTANT
6553 && e->value.character.length > 1)
6554 {
6555 e->value.character.length = 1;
6556 gfc_conv_expr (parmse, e);
6557 }
6558
6559 tree slen1 = build_int_cst (gfc_charlen_type_node, 1);
6560 gfc_conv_string_parameter (parmse);
6561 parmse->expr = gfc_string_to_single_character (slen1, parmse->expr,
6562 e->ts.kind);
6563 /* Truncate resulting string to length 1. */
6564 parmse->string_length = slen1;
6565 }
6566
6567 if (fsym->attr.optional && fsym->ts.type != BT_CLASS)
6568 {
6569 /* F2018:15.5.2.12 Argument presence and
6570 restrictions on arguments not present. */
6571 if (e->expr_type == EXPR_VARIABLE
6572 && e->rank == 0
6573 && (gfc_expr_attr (e).allocatable
6574 || gfc_expr_attr (e).pointer))
6575 {
6576 gfc_se argse;
6577 tree cond;
6578 gfc_init_se (&argse, NULL);
6579 argse.want_pointer = 1;
6580 gfc_conv_expr (&argse, e);
6581 cond = fold_convert (TREE_TYPE (argse.expr), null_pointer_node);
6582 cond = fold_build2_loc (input_location, NE_EXPR,
6583 logical_type_node,
6584 argse.expr, cond);
6585 vec_safe_push (optionalargs,
6586 fold_convert (boolean_type_node, cond));
6587 /* Create "conditional temporary". */
6588 conv_cond_temp (parmse, e, cond);
6589 }
6590 else if (e->expr_type != EXPR_VARIABLE
6591 || !e->symtree->n.sym->attr.optional
6592 || (e->ref != NULL && e->ref->type != REF_ARRAY))
6593 vec_safe_push (optionalargs, boolean_true_node);
6594 else
6595 {
6596 tmp = gfc_conv_expr_present (e->symtree->n.sym);
6597 if (gfc_bt_struct (fsym->ts.type)
6598 && !(fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING))
6599 conv_cond_temp (parmse, e, tmp);
6600 else if (e->ts.type != BT_CHARACTER && !e->symtree->n.sym->attr.value)
6601 parmse->expr
6602 = fold_build3_loc (input_location, COND_EXPR,
6603 TREE_TYPE (parmse->expr),
6604 tmp, parmse->expr,
6605 fold_convert (TREE_TYPE (parmse->expr),
6606 integer_zero_node));
6607
6608 vec_safe_push (optionalargs,
6609 fold_convert (boolean_type_node, tmp));
6610 }
6611 }
6612 }
6613
6614
6615 /* Helper function for the handling of NULL() actual arguments associated with
6616 non-optional dummy variables. Argument parmse should already be set up. */
6617 static void
6618 conv_null_actual (gfc_se * parmse, gfc_expr * e, gfc_symbol * fsym)
6619 {
6620 gcc_assert (fsym && e->expr_type == EXPR_NULL);
6621
6622 /* Obtain the character length for a NULL() actual with a character
6623 MOLD argument. Otherwise substitute a suitable dummy length.
6624 Here we handle only non-optional dummies of non-bind(c) procedures. */
6625 if (fsym->ts.type == BT_CHARACTER)
6626 {
6627 if (e->ts.type == BT_CHARACTER
6628 && e->symtree->n.sym->ts.type == BT_CHARACTER)
6629 {
6630 /* MOLD is present. Substitute a temporary character NULL pointer.
6631 For an assumed-rank dummy we need a descriptor that passes the
6632 correct rank. */
6633 if (fsym->as && fsym->as->type == AS_ASSUMED_RANK)
6634 {
6635 tree rank;
6636 tree tmp = parmse->expr;
6637 tmp = gfc_conv_scalar_to_descriptor (parmse, tmp, fsym->attr);
6638 rank = gfc_conv_descriptor_rank (tmp);
6639 gfc_add_modify (&parmse->pre, rank,
6640 build_int_cst (TREE_TYPE (rank), e->rank));
6641 parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
6642 }
6643 else
6644 {
6645 tree tmp = gfc_create_var (TREE_TYPE (parmse->expr), "null");
6646 gfc_add_modify (&parmse->pre, tmp,
6647 build_zero_cst (TREE_TYPE (tmp)));
6648 parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
6649 }
6650
6651 /* Ensure that a usable length is available. */
6652 if (parmse->string_length == NULL_TREE)
6653 {
6654 gfc_typespec *ts = &e->symtree->n.sym->ts;
6655
6656 if (ts->u.cl->length != NULL
6657 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
6658 gfc_conv_const_charlen (ts->u.cl);
6659
6660 if (ts->u.cl->backend_decl)
6661 parmse->string_length = ts->u.cl->backend_decl;
6662 }
6663 }
6664 else if (e->ts.type == BT_UNKNOWN && parmse->string_length == NULL_TREE)
6665 {
6666 /* MOLD is not present. Pass length of associated dummy character
6667 argument if constant, or zero. */
6668 if (fsym->ts.u.cl->length != NULL
6669 && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
6670 {
6671 gfc_conv_const_charlen (fsym->ts.u.cl);
6672 parmse->string_length = fsym->ts.u.cl->backend_decl;
6673 }
6674 else
6675 {
6676 parmse->string_length = gfc_create_var (gfc_charlen_type_node,
6677 "slen");
6678 gfc_add_modify (&parmse->pre, parmse->string_length,
6679 build_zero_cst (gfc_charlen_type_node));
6680 }
6681 }
6682 }
6683 else if (fsym->ts.type == BT_DERIVED)
6684 {
6685 if (e->ts.type != BT_UNKNOWN)
6686 /* MOLD is present. Pass a corresponding temporary NULL pointer.
6687 For an assumed-rank dummy we provide a descriptor that passes
6688 the correct rank. */
6689 {
6690 tree rank;
6691 tree tmp = parmse->expr;
6692
6693 tmp = gfc_conv_scalar_to_descriptor (parmse, tmp, gfc_expr_attr (e));
6694 rank = gfc_conv_descriptor_rank (tmp);
6695 gfc_add_modify (&parmse->pre, rank,
6696 build_int_cst (TREE_TYPE (rank), e->rank));
6697 gfc_conv_descriptor_data_set (&parmse->pre, tmp, null_pointer_node);
6698 parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
6699 }
6700 else
6701 /* MOLD is not present. Use attributes from dummy argument, which is
6702 not allowed to be assumed-rank. */
6703 {
6704 int dummy_rank;
6705 tree tmp = parmse->expr;
6706
6707 if ((fsym->attr.allocatable || fsym->attr.pointer)
6708 && fsym->attr.intent == INTENT_UNKNOWN)
6709 fsym->attr.intent = INTENT_IN;
6710 tmp = gfc_conv_scalar_to_descriptor (parmse, tmp, fsym->attr);
6711 dummy_rank = fsym->as ? fsym->as->rank : 0;
6712 if (dummy_rank > 0)
6713 {
6714 tree rank = gfc_conv_descriptor_rank (tmp);
6715 gfc_add_modify (&parmse->pre, rank,
6716 build_int_cst (TREE_TYPE (rank), dummy_rank));
6717 }
6718 gfc_conv_descriptor_data_set (&parmse->pre, tmp, null_pointer_node);
6719 parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
6720 }
6721 }
6722 }
6723
6724
6725 /* Generate code for a procedure call. Note can return se->post != NULL.
6726 If se->direct_byref is set then se->expr contains the return parameter.
6727 Return nonzero, if the call has alternate specifiers.
6728 'expr' is only needed for procedure pointer components. */
6729
6730 int
6731 gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
6732 gfc_actual_arglist * args, gfc_expr * expr,
6733 vec<tree, va_gc> *append_args)
6734 {
6735 gfc_interface_mapping mapping;
6736 vec<tree, va_gc> *arglist;
6737 vec<tree, va_gc> *retargs;
6738 tree tmp;
6739 tree fntype;
6740 gfc_se parmse;
6741 gfc_array_info *info;
6742 int byref;
6743 int parm_kind;
6744 tree type;
6745 tree var;
6746 tree len;
6747 tree base_object;
6748 vec<tree, va_gc> *stringargs;
6749 vec<tree, va_gc> *optionalargs;
6750 tree result = NULL;
6751 gfc_formal_arglist *formal;
6752 gfc_actual_arglist *arg;
6753 int has_alternate_specifier = 0;
6754 bool need_interface_mapping;
6755 bool is_builtin;
6756 bool callee_alloc;
6757 bool ulim_copy;
6758 gfc_typespec ts;
6759 gfc_charlen cl;
6760 gfc_expr *e;
6761 gfc_symbol *fsym;
6762 enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
6763 gfc_component *comp = NULL;
6764 int arglen;
6765 unsigned int argc;
6766 tree arg1_cntnr = NULL_TREE;
6767 arglist = NULL;
6768 retargs = NULL;
6769 stringargs = NULL;
6770 optionalargs = NULL;
6771 var = NULL_TREE;
6772 len = NULL_TREE;
6773 gfc_clear_ts (&ts);
6774 gfc_intrinsic_sym *isym = expr && expr->rank ?
6775 expr->value.function.isym : NULL;
6776
6777 comp = gfc_get_proc_ptr_comp (expr);
6778
6779 bool elemental_proc = (comp
6780 && comp->ts.interface
6781 && comp->ts.interface->attr.elemental)
6782 || (comp && comp->attr.elemental)
6783 || sym->attr.elemental;
6784
6785 if (se->ss != NULL)
6786 {
6787 if (!elemental_proc)
6788 {
6789 gcc_assert (se->ss->info->type == GFC_SS_FUNCTION);
6790 if (se->ss->info->useflags)
6791 {
6792 gcc_assert ((!comp && gfc_return_by_reference (sym)
6793 && sym->result->attr.dimension)
6794 || (comp && comp->attr.dimension)
6795 || gfc_is_class_array_function (expr));
6796 gcc_assert (se->loop != NULL);
6797 /* Access the previously obtained result. */
6798 gfc_conv_tmp_array_ref (se);
6799 return 0;
6800 }
6801 }
6802 info = &se->ss->info->data.array;
6803 }
6804 else
6805 info = NULL;
6806
6807 stmtblock_t post, clobbers, dealloc_blk;
6808 gfc_init_block (&post);
6809 gfc_init_block (&clobbers);
6810 gfc_init_block (&dealloc_blk);
6811 gfc_init_interface_mapping (&mapping);
6812 if (!comp)
6813 {
6814 formal = gfc_sym_get_dummy_args (sym);
6815 need_interface_mapping = sym->attr.dimension ||
6816 (sym->ts.type == BT_CHARACTER
6817 && sym->ts.u.cl->length
6818 && sym->ts.u.cl->length->expr_type
6819 != EXPR_CONSTANT);
6820 }
6821 else
6822 {
6823 formal = comp->ts.interface ? comp->ts.interface->formal : NULL;
6824 need_interface_mapping = comp->attr.dimension ||
6825 (comp->ts.type == BT_CHARACTER
6826 && comp->ts.u.cl->length
6827 && comp->ts.u.cl->length->expr_type
6828 != EXPR_CONSTANT);
6829 }
6830
6831 base_object = NULL_TREE;
6832 /* For _vprt->_copy () routines no formal symbol is present. Nevertheless
6833 is the third and fourth argument to such a function call a value
6834 denoting the number of elements to copy (i.e., most of the time the
6835 length of a deferred length string). */
6836 ulim_copy = (formal == NULL)
6837 && UNLIMITED_POLY (sym)
6838 && comp && (strcmp ("_copy", comp->name) == 0);
6839
6840 /* Scan for allocatable actual arguments passed to allocatable dummy
6841 arguments with INTENT(OUT). As the corresponding actual arguments are
6842 deallocated before execution of the procedure, we evaluate actual
6843 argument expressions to avoid problems with possible dependencies. */
6844 bool force_eval_args = false;
6845 gfc_formal_arglist *tmp_formal;
6846 for (arg = args, tmp_formal = formal; arg != NULL;
6847 arg = arg->next, tmp_formal = tmp_formal ? tmp_formal->next : NULL)
6848 {
6849 e = arg->expr;
6850 fsym = tmp_formal ? tmp_formal->sym : NULL;
6851 if (e && fsym
6852 && e->expr_type == EXPR_VARIABLE
6853 && fsym->attr.intent == INTENT_OUT
6854 && (fsym->ts.type == BT_CLASS && fsym->attr.class_ok
6855 ? CLASS_DATA (fsym)->attr.allocatable
6856 : fsym->attr.allocatable)
6857 && e->symtree
6858 && e->symtree->n.sym
6859 && gfc_variable_attr (e, NULL).allocatable)
6860 {
6861 force_eval_args = true;
6862 break;
6863 }
6864 }
6865
6866 /* Evaluate the arguments. */
6867 for (arg = args, argc = 0; arg != NULL;
6868 arg = arg->next, formal = formal ? formal->next : NULL, ++argc)
6869 {
6870 bool finalized = false;
6871 tree derived_array = NULL_TREE;
6872 symbol_attribute *attr;
6873
6874 e = arg->expr;
6875 fsym = formal ? formal->sym : NULL;
6876 parm_kind = MISSING;
6877
6878 attr = fsym ? &(fsym->ts.type == BT_CLASS ? CLASS_DATA (fsym)->attr
6879 : fsym->attr)
6880 : nullptr;
6881 /* If the procedure requires an explicit interface, the actual
6882 argument is passed according to the corresponding formal
6883 argument. If the corresponding formal argument is a POINTER,
6884 ALLOCATABLE or assumed shape, we do not use g77's calling
6885 convention, and pass the address of the array descriptor
6886 instead. Otherwise we use g77's calling convention, in other words
6887 pass the array data pointer without descriptor. */
6888 bool nodesc_arg = fsym != NULL
6889 && !(fsym->attr.pointer || fsym->attr.allocatable)
6890 && fsym->as
6891 && fsym->as->type != AS_ASSUMED_SHAPE
6892 && fsym->as->type != AS_ASSUMED_RANK;
6893 if (comp)
6894 nodesc_arg = nodesc_arg || !comp->attr.always_explicit;
6895 else
6896 nodesc_arg
6897 = nodesc_arg
6898 || !(sym->attr.always_explicit || (attr && attr->codimension));
6899
6900 /* Class array expressions are sometimes coming completely unadorned
6901 with either arrayspec or _data component. Correct that here.
6902 OOP-TODO: Move this to the frontend. */
6903 if (e && e->expr_type == EXPR_VARIABLE
6904 && !e->ref
6905 && e->ts.type == BT_CLASS
6906 && (CLASS_DATA (e)->attr.codimension
6907 || CLASS_DATA (e)->attr.dimension))
6908 {
6909 gfc_typespec temp_ts = e->ts;
6910 gfc_add_class_array_ref (e);
6911 e->ts = temp_ts;
6912 }
6913
6914 if (e == NULL
6915 || (e->expr_type == EXPR_NULL
6916 && fsym
6917 && fsym->attr.value
6918 && fsym->attr.optional
6919 && !fsym->attr.dimension
6920 && fsym->ts.type != BT_CLASS))
6921 {
6922 if (se->ignore_optional)
6923 {
6924 /* Some intrinsics have already been resolved to the correct
6925 parameters. */
6926 continue;
6927 }
6928 else if (arg->label)
6929 {
6930 has_alternate_specifier = 1;
6931 continue;
6932 }
6933 else
6934 {
6935 gfc_init_se (&parmse, NULL);
6936
6937 /* For scalar arguments with VALUE attribute which are passed by
6938 value, pass "0" and a hidden argument gives the optional
6939 status. */
6940 if (fsym && fsym->attr.optional && fsym->attr.value
6941 && !fsym->attr.dimension && fsym->ts.type != BT_CLASS)
6942 {
6943 conv_dummy_value (&parmse, e, fsym, optionalargs);
6944 }
6945 else
6946 {
6947 /* Pass a NULL pointer for an absent arg. */
6948 parmse.expr = null_pointer_node;
6949
6950 /* Is it an absent character dummy? */
6951 bool absent_char = false;
6952 gfc_dummy_arg * const dummy_arg = arg->associated_dummy;
6953
6954 /* Fall back to inferred type only if no formal. */
6955 if (fsym)
6956 absent_char = (fsym->ts.type == BT_CHARACTER);
6957 else if (dummy_arg)
6958 absent_char = (gfc_dummy_arg_get_typespec (*dummy_arg).type
6959 == BT_CHARACTER);
6960 if (absent_char)
6961 parmse.string_length = build_int_cst (gfc_charlen_type_node,
6962 0);
6963 }
6964 }
6965 }
6966 else if (e->expr_type == EXPR_NULL
6967 && (e->ts.type == BT_UNKNOWN || e->ts.type == BT_DERIVED)
6968 && fsym && attr && (attr->pointer || attr->allocatable)
6969 && fsym->ts.type == BT_DERIVED)
6970 {
6971 gfc_init_se (&parmse, NULL);
6972 gfc_conv_expr_reference (&parmse, e);
6973 conv_null_actual (&parmse, e, fsym);
6974 }
6975 else if (arg->expr->expr_type == EXPR_NULL
6976 && fsym && !fsym->attr.pointer
6977 && (fsym->ts.type != BT_CLASS
6978 || !CLASS_DATA (fsym)->attr.class_pointer))
6979 {
6980 /* Pass a NULL pointer to denote an absent arg. */
6981 gcc_assert (fsym->attr.optional && !fsym->attr.allocatable
6982 && (fsym->ts.type != BT_CLASS
6983 || !CLASS_DATA (fsym)->attr.allocatable));
6984 gfc_init_se (&parmse, NULL);
6985 parmse.expr = null_pointer_node;
6986 if (fsym->ts.type == BT_CHARACTER)
6987 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
6988 }
6989 else if (fsym && fsym->ts.type == BT_CLASS
6990 && e->ts.type == BT_DERIVED)
6991 {
6992 /* The derived type needs to be converted to a temporary
6993 CLASS object. */
6994 gfc_init_se (&parmse, se);
6995 gfc_conv_derived_to_class (&parmse, e, fsym, NULL_TREE,
6996 fsym->attr.optional
6997 && e->expr_type == EXPR_VARIABLE
6998 && e->symtree->n.sym->attr.optional,
6999 CLASS_DATA (fsym)->attr.class_pointer
7000 || CLASS_DATA (fsym)->attr.allocatable,
7001 sym->name, &derived_array);
7002 }
7003 else if (UNLIMITED_POLY (fsym) && e->ts.type != BT_CLASS
7004 && e->ts.type != BT_PROCEDURE
7005 && (gfc_expr_attr (e).flavor != FL_PROCEDURE
7006 || gfc_expr_attr (e).proc != PROC_UNKNOWN))
7007 {
7008 /* The intrinsic type needs to be converted to a temporary
7009 CLASS object for the unlimited polymorphic formal. */
7010 gfc_find_vtab (&e->ts);
7011 gfc_init_se (&parmse, se);
7012 gfc_conv_intrinsic_to_class (&parmse, e, fsym->ts);
7013
7014 }
7015 else if (se->ss && se->ss->info->useflags)
7016 {
7017 gfc_ss *ss;
7018
7019 ss = se->ss;
7020
7021 /* An elemental function inside a scalarized loop. */
7022 gfc_init_se (&parmse, se);
7023 parm_kind = ELEMENTAL;
7024
7025 /* When no fsym is present, ulim_copy is set and this is a third or
7026 fourth argument, use call-by-value instead of by reference to
7027 hand the length properties to the copy routine (i.e., most of the
7028 time this will be a call to a __copy_character_* routine where the
7029 third and fourth arguments are the lengths of a deferred length
7030 char array). */
7031 if ((fsym && fsym->attr.value)
7032 || (ulim_copy && (argc == 2 || argc == 3)))
7033 gfc_conv_expr (&parmse, e);
7034 else if (e->expr_type == EXPR_ARRAY)
7035 {
7036 gfc_conv_expr (&parmse, e);
7037 if (e->ts.type != BT_CHARACTER)
7038 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
7039 }
7040 else
7041 gfc_conv_expr_reference (&parmse, e);
7042
7043 if (e->ts.type == BT_CHARACTER && !e->rank
7044 && e->expr_type == EXPR_FUNCTION)
7045 parmse.expr = build_fold_indirect_ref_loc (input_location,
7046 parmse.expr);
7047
7048 if (fsym && fsym->ts.type == BT_DERIVED
7049 && gfc_is_class_container_ref (e))
7050 {
7051 parmse.expr = gfc_class_data_get (parmse.expr);
7052
7053 if (fsym->attr.optional && e->expr_type == EXPR_VARIABLE
7054 && e->symtree->n.sym->attr.optional)
7055 {
7056 tree cond = gfc_conv_expr_present (e->symtree->n.sym);
7057 parmse.expr = build3_loc (input_location, COND_EXPR,
7058 TREE_TYPE (parmse.expr),
7059 cond, parmse.expr,
7060 fold_convert (TREE_TYPE (parmse.expr),
7061 null_pointer_node));
7062 }
7063 }
7064
7065 /* Scalar dummy arguments of intrinsic type or derived type with
7066 VALUE attribute. */
7067 if (fsym
7068 && fsym->attr.value
7069 && fsym->ts.type != BT_CLASS)
7070 conv_dummy_value (&parmse, e, fsym, optionalargs);
7071
7072 /* If we are passing an absent array as optional dummy to an
7073 elemental procedure, make sure that we pass NULL when the data
7074 pointer is NULL. We need this extra conditional because of
7075 scalarization which passes arrays elements to the procedure,
7076 ignoring the fact that the array can be absent/unallocated/... */
7077 else if (ss->info->can_be_null_ref
7078 && ss->info->type != GFC_SS_REFERENCE)
7079 {
7080 tree descriptor_data;
7081
7082 descriptor_data = ss->info->data.array.data;
7083 tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7084 descriptor_data,
7085 fold_convert (TREE_TYPE (descriptor_data),
7086 null_pointer_node));
7087 parmse.expr
7088 = fold_build3_loc (input_location, COND_EXPR,
7089 TREE_TYPE (parmse.expr),
7090 gfc_unlikely (tmp, PRED_FORTRAN_ABSENT_DUMMY),
7091 fold_convert (TREE_TYPE (parmse.expr),
7092 null_pointer_node),
7093 parmse.expr);
7094 }
7095
7096 /* The scalarizer does not repackage the reference to a class
7097 array - instead it returns a pointer to the data element. */
7098 if (fsym && fsym->ts.type == BT_CLASS && e->ts.type == BT_CLASS)
7099 gfc_conv_class_to_class (&parmse, e, fsym->ts, true,
7100 fsym->attr.intent != INTENT_IN
7101 && (CLASS_DATA (fsym)->attr.class_pointer
7102 || CLASS_DATA (fsym)->attr.allocatable),
7103 fsym->attr.optional
7104 && e->expr_type == EXPR_VARIABLE
7105 && e->symtree->n.sym->attr.optional,
7106 CLASS_DATA (fsym)->attr.class_pointer
7107 || CLASS_DATA (fsym)->attr.allocatable);
7108 }
7109 else
7110 {
7111 bool scalar;
7112 gfc_ss *argss;
7113
7114 gfc_init_se (&parmse, NULL);
7115
7116 /* Check whether the expression is a scalar or not; we cannot use
7117 e->rank as it can be nonzero for functions arguments. */
7118 argss = gfc_walk_expr (e);
7119 scalar = argss == gfc_ss_terminator;
7120 if (!scalar)
7121 gfc_free_ss_chain (argss);
7122
7123 /* Special handling for passing scalar polymorphic coarrays;
7124 otherwise one passes "class->_data.data" instead of "&class". */
7125 if (e->rank == 0 && e->ts.type == BT_CLASS
7126 && fsym && fsym->ts.type == BT_CLASS
7127 && CLASS_DATA (fsym)->attr.codimension
7128 && !CLASS_DATA (fsym)->attr.dimension)
7129 {
7130 gfc_add_class_array_ref (e);
7131 parmse.want_coarray = 1;
7132 scalar = false;
7133 }
7134
7135 /* A scalar or transformational function. */
7136 if (scalar)
7137 {
7138 if (e->expr_type == EXPR_VARIABLE
7139 && e->symtree->n.sym->attr.cray_pointee
7140 && fsym && fsym->attr.flavor == FL_PROCEDURE)
7141 {
7142 /* The Cray pointer needs to be converted to a pointer to
7143 a type given by the expression. */
7144 gfc_conv_expr (&parmse, e);
7145 type = build_pointer_type (TREE_TYPE (parmse.expr));
7146 tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
7147 parmse.expr = convert (type, tmp);
7148 }
7149
7150 else if (sym->attr.is_bind_c && e && is_CFI_desc (fsym, NULL))
7151 /* Implement F2018, 18.3.6, list item (5), bullet point 2. */
7152 gfc_conv_gfc_desc_to_cfi_desc (&parmse, e, fsym);
7153
7154 else if (fsym && fsym->attr.value)
7155 {
7156 if (fsym->ts.type == BT_CHARACTER
7157 && fsym->ts.is_c_interop
7158 && fsym->ns->proc_name != NULL
7159 && fsym->ns->proc_name->attr.is_bind_c)
7160 {
7161 parmse.expr = NULL;
7162 conv_scalar_char_value (fsym, &parmse, &e);
7163 if (parmse.expr == NULL)
7164 gfc_conv_expr (&parmse, e);
7165 }
7166 else
7167 {
7168 gfc_conv_expr (&parmse, e);
7169 conv_dummy_value (&parmse, e, fsym, optionalargs);
7170 }
7171 }
7172
7173 else if (arg->name && arg->name[0] == '%')
7174 /* Argument list functions %VAL, %LOC and %REF are signalled
7175 through arg->name. */
7176 conv_arglist_function (&parmse, arg->expr, arg->name);
7177 else if ((e->expr_type == EXPR_FUNCTION)
7178 && ((e->value.function.esym
7179 && e->value.function.esym->result->attr.pointer)
7180 || (!e->value.function.esym
7181 && e->symtree->n.sym->attr.pointer))
7182 && fsym && fsym->attr.target)
7183 /* Make sure the function only gets called once. */
7184 gfc_conv_expr_reference (&parmse, e);
7185 else if (e->expr_type == EXPR_FUNCTION
7186 && e->symtree->n.sym->result
7187 && e->symtree->n.sym->result != e->symtree->n.sym
7188 && e->symtree->n.sym->result->attr.proc_pointer)
7189 {
7190 /* Functions returning procedure pointers. */
7191 gfc_conv_expr (&parmse, e);
7192 if (fsym && fsym->attr.proc_pointer)
7193 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
7194 }
7195
7196 else
7197 {
7198 bool defer_to_dealloc_blk = false;
7199 if (e->ts.type == BT_CLASS && fsym
7200 && fsym->ts.type == BT_CLASS
7201 && (!CLASS_DATA (fsym)->as
7202 || CLASS_DATA (fsym)->as->type != AS_ASSUMED_RANK)
7203 && CLASS_DATA (e)->attr.codimension)
7204 {
7205 gcc_assert (!CLASS_DATA (fsym)->attr.codimension);
7206 gcc_assert (!CLASS_DATA (fsym)->as);
7207 gfc_add_class_array_ref (e);
7208 parmse.want_coarray = 1;
7209 gfc_conv_expr_reference (&parmse, e);
7210 class_scalar_coarray_to_class (&parmse, e, fsym->ts,
7211 fsym->attr.optional
7212 && e->expr_type == EXPR_VARIABLE);
7213 }
7214 else if (e->ts.type == BT_CLASS && fsym
7215 && fsym->ts.type == BT_CLASS
7216 && !CLASS_DATA (fsym)->as
7217 && !CLASS_DATA (e)->as
7218 && strcmp (fsym->ts.u.derived->name,
7219 e->ts.u.derived->name))
7220 {
7221 type = gfc_typenode_for_spec (&fsym->ts);
7222 var = gfc_create_var (type, fsym->name);
7223 gfc_conv_expr (&parmse, e);
7224 if (fsym->attr.optional
7225 && e->expr_type == EXPR_VARIABLE
7226 && e->symtree->n.sym->attr.optional)
7227 {
7228 stmtblock_t block;
7229 tree cond;
7230 tmp = gfc_build_addr_expr (NULL_TREE, parmse.expr);
7231 cond = fold_build2_loc (input_location, NE_EXPR,
7232 logical_type_node, tmp,
7233 fold_convert (TREE_TYPE (tmp),
7234 null_pointer_node));
7235 gfc_start_block (&block);
7236 gfc_add_modify (&block, var,
7237 fold_build1_loc (input_location,
7238 VIEW_CONVERT_EXPR,
7239 type, parmse.expr));
7240 gfc_add_expr_to_block (&parmse.pre,
7241 fold_build3_loc (input_location,
7242 COND_EXPR, void_type_node,
7243 cond, gfc_finish_block (&block),
7244 build_empty_stmt (input_location)));
7245 parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
7246 parmse.expr = build3_loc (input_location, COND_EXPR,
7247 TREE_TYPE (parmse.expr),
7248 cond, parmse.expr,
7249 fold_convert (TREE_TYPE (parmse.expr),
7250 null_pointer_node));
7251 }
7252 else
7253 {
7254 /* Since the internal representation of unlimited
7255 polymorphic expressions includes an extra field
7256 that other class objects do not, a cast to the
7257 formal type does not work. */
7258 if (!UNLIMITED_POLY (e) && UNLIMITED_POLY (fsym))
7259 {
7260 tree efield;
7261
7262 /* Evaluate arguments just once, when they have
7263 side effects. */
7264 if (TREE_SIDE_EFFECTS (parmse.expr))
7265 {
7266 tree cldata, zero;
7267
7268 parmse.expr = gfc_evaluate_now (parmse.expr,
7269 &parmse.pre);
7270
7271 /* Prevent memory leak, when old component
7272 was allocated already. */
7273 cldata = gfc_class_data_get (parmse.expr);
7274 zero = build_int_cst (TREE_TYPE (cldata),
7275 0);
7276 tmp = fold_build2_loc (input_location, NE_EXPR,
7277 logical_type_node,
7278 cldata, zero);
7279 tmp = build3_v (COND_EXPR, tmp,
7280 gfc_call_free (cldata),
7281 build_empty_stmt (
7282 input_location));
7283 gfc_add_expr_to_block (&parmse.finalblock,
7284 tmp);
7285 gfc_add_modify (&parmse.finalblock,
7286 cldata, zero);
7287 }
7288
7289 /* Set the _data field. */
7290 tmp = gfc_class_data_get (var);
7291 efield = fold_convert (TREE_TYPE (tmp),
7292 gfc_class_data_get (parmse.expr));
7293 gfc_add_modify (&parmse.pre, tmp, efield);
7294
7295 /* Set the _vptr field. */
7296 tmp = gfc_class_vptr_get (var);
7297 efield = fold_convert (TREE_TYPE (tmp),
7298 gfc_class_vptr_get (parmse.expr));
7299 gfc_add_modify (&parmse.pre, tmp, efield);
7300
7301 /* Set the _len field. */
7302 tmp = gfc_class_len_get (var);
7303 gfc_add_modify (&parmse.pre, tmp,
7304 build_int_cst (TREE_TYPE (tmp), 0));
7305 }
7306 else
7307 {
7308 tmp = fold_build1_loc (input_location,
7309 VIEW_CONVERT_EXPR,
7310 type, parmse.expr);
7311 gfc_add_modify (&parmse.pre, var, tmp);
7312 ;
7313 }
7314 parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
7315 }
7316 }
7317 else
7318 {
7319 gfc_conv_expr_reference (&parmse, e);
7320
7321 gfc_symbol *dsym = fsym;
7322 gfc_dummy_arg *dummy;
7323
7324 /* Use associated dummy as fallback for formal
7325 argument if there is no explicit interface. */
7326 if (dsym == NULL
7327 && (dummy = arg->associated_dummy)
7328 && dummy->intrinsicness == GFC_NON_INTRINSIC_DUMMY_ARG
7329 && dummy->u.non_intrinsic->sym)
7330 dsym = dummy->u.non_intrinsic->sym;
7331
7332 if (dsym
7333 && dsym->attr.intent == INTENT_OUT
7334 && !dsym->attr.allocatable
7335 && !dsym->attr.pointer
7336 && e->expr_type == EXPR_VARIABLE
7337 && e->ref == NULL
7338 && e->symtree
7339 && e->symtree->n.sym
7340 && !e->symtree->n.sym->attr.dimension
7341 && e->ts.type != BT_CHARACTER
7342 && e->ts.type != BT_CLASS
7343 && (e->ts.type != BT_DERIVED
7344 || (dsym->ts.type == BT_DERIVED
7345 && e->ts.u.derived == dsym->ts.u.derived
7346 /* Types with allocatable components are
7347 excluded from clobbering because we need
7348 the unclobbered pointers to free the
7349 allocatable components in the callee.
7350 Same goes for finalizable types or types
7351 with finalizable components, we need to
7352 pass the unclobbered values to the
7353 finalization routines.
7354 For parameterized types, it's less clear
7355 but they may not have a constant size
7356 so better exclude them in any case. */
7357 && !e->ts.u.derived->attr.alloc_comp
7358 && !e->ts.u.derived->attr.pdt_type
7359 && !gfc_is_finalizable (e->ts.u.derived, NULL)))
7360 && e->ts.type != BT_PROCEDURE
7361 && !sym->attr.elemental)
7362 {
7363 tree var;
7364 var = build_fold_indirect_ref_loc (input_location,
7365 parmse.expr);
7366 tree clobber = build_clobber (TREE_TYPE (var));
7367 gfc_add_modify (&clobbers, var, clobber);
7368 }
7369 }
7370 /* Catch base objects that are not variables. */
7371 if (e->ts.type == BT_CLASS
7372 && e->expr_type != EXPR_VARIABLE
7373 && expr && e == expr->base_expr)
7374 base_object = build_fold_indirect_ref_loc (input_location,
7375 parmse.expr);
7376
7377 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
7378 allocated on entry, it must be deallocated. */
7379 if (fsym && fsym->attr.intent == INTENT_OUT
7380 && (fsym->attr.allocatable
7381 || (fsym->ts.type == BT_CLASS
7382 && CLASS_DATA (fsym)->attr.allocatable))
7383 && !is_CFI_desc (fsym, NULL))
7384 {
7385 stmtblock_t block;
7386 tree ptr;
7387
7388 defer_to_dealloc_blk = true;
7389
7390 parmse.expr = gfc_evaluate_data_ref_now (parmse.expr,
7391 &parmse.pre);
7392
7393 if (parmse.class_container != NULL_TREE)
7394 parmse.class_container
7395 = gfc_evaluate_data_ref_now (parmse.class_container,
7396 &parmse.pre);
7397
7398 gfc_init_block (&block);
7399 ptr = parmse.expr;
7400 if (e->ts.type == BT_CLASS)
7401 ptr = gfc_class_data_get (ptr);
7402
7403 tree cls = parmse.class_container;
7404 tmp = gfc_deallocate_scalar_with_status (ptr, NULL_TREE,
7405 NULL_TREE, true,
7406 e, e->ts, cls);
7407 gfc_add_expr_to_block (&block, tmp);
7408 gfc_add_modify (&block, ptr,
7409 fold_convert (TREE_TYPE (ptr),
7410 null_pointer_node));
7411
7412 if (fsym->ts.type == BT_CLASS)
7413 gfc_reset_vptr (&block, nullptr,
7414 build_fold_indirect_ref (parmse.expr),
7415 fsym->ts.u.derived);
7416
7417 if (fsym->attr.optional
7418 && e->expr_type == EXPR_VARIABLE
7419 && e->symtree->n.sym->attr.optional)
7420 {
7421 tmp = fold_build3_loc (input_location, COND_EXPR,
7422 void_type_node,
7423 gfc_conv_expr_present (e->symtree->n.sym),
7424 gfc_finish_block (&block),
7425 build_empty_stmt (input_location));
7426 }
7427 else
7428 tmp = gfc_finish_block (&block);
7429
7430 gfc_add_expr_to_block (&dealloc_blk, tmp);
7431 }
7432
7433 /* A class array element needs converting back to be a
7434 class object, if the formal argument is a class object. */
7435 if (fsym && fsym->ts.type == BT_CLASS
7436 && e->ts.type == BT_CLASS
7437 && ((CLASS_DATA (fsym)->as
7438 && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
7439 || CLASS_DATA (e)->attr.dimension))
7440 {
7441 gfc_se class_se = parmse;
7442 gfc_init_block (&class_se.pre);
7443 gfc_init_block (&class_se.post);
7444
7445 gfc_conv_class_to_class (&class_se, e, fsym->ts, false,
7446 fsym->attr.intent != INTENT_IN
7447 && (CLASS_DATA (fsym)->attr.class_pointer
7448 || CLASS_DATA (fsym)->attr.allocatable),
7449 fsym->attr.optional
7450 && e->expr_type == EXPR_VARIABLE
7451 && e->symtree->n.sym->attr.optional,
7452 CLASS_DATA (fsym)->attr.class_pointer
7453 || CLASS_DATA (fsym)->attr.allocatable);
7454
7455 parmse.expr = class_se.expr;
7456 stmtblock_t *class_pre_block = defer_to_dealloc_blk
7457 ? &dealloc_blk
7458 : &parmse.pre;
7459 gfc_add_block_to_block (class_pre_block, &class_se.pre);
7460 gfc_add_block_to_block (&parmse.post, &class_se.post);
7461 }
7462
7463 if (fsym && (fsym->ts.type == BT_DERIVED
7464 || fsym->ts.type == BT_ASSUMED)
7465 && e->ts.type == BT_CLASS
7466 && !CLASS_DATA (e)->attr.dimension
7467 && !CLASS_DATA (e)->attr.codimension)
7468 {
7469 parmse.expr = gfc_class_data_get (parmse.expr);
7470 /* The result is a class temporary, whose _data component
7471 must be freed to avoid a memory leak. */
7472 if (e->expr_type == EXPR_FUNCTION
7473 && CLASS_DATA (e)->attr.allocatable)
7474 {
7475 tree zero;
7476
7477 /* Finalize the expression. */
7478 gfc_finalize_tree_expr (&parmse, NULL,
7479 gfc_expr_attr (e), e->rank);
7480 gfc_add_block_to_block (&parmse.post,
7481 &parmse.finalblock);
7482
7483 /* Then free the class _data. */
7484 zero = build_int_cst (TREE_TYPE (parmse.expr), 0);
7485 tmp = fold_build2_loc (input_location, NE_EXPR,
7486 logical_type_node,
7487 parmse.expr, zero);
7488 tmp = build3_v (COND_EXPR, tmp,
7489 gfc_call_free (parmse.expr),
7490 build_empty_stmt (input_location));
7491 gfc_add_expr_to_block (&parmse.post, tmp);
7492 gfc_add_modify (&parmse.post, parmse.expr, zero);
7493 }
7494 }
7495
7496 /* Wrap scalar variable in a descriptor. We need to convert
7497 the address of a pointer back to the pointer itself before,
7498 we can assign it to the data field. */
7499
7500 if (fsym && fsym->as && fsym->as->type == AS_ASSUMED_RANK
7501 && fsym->ts.type != BT_CLASS && e->expr_type != EXPR_NULL)
7502 {
7503 tmp = parmse.expr;
7504 if (TREE_CODE (tmp) == ADDR_EXPR)
7505 tmp = TREE_OPERAND (tmp, 0);
7506 parmse.expr = gfc_conv_scalar_to_descriptor (&parmse, tmp,
7507 fsym->attr);
7508 parmse.expr = gfc_build_addr_expr (NULL_TREE,
7509 parmse.expr);
7510 }
7511 else if (fsym && e->expr_type != EXPR_NULL
7512 && ((fsym->attr.pointer
7513 && fsym->attr.flavor != FL_PROCEDURE)
7514 || (fsym->attr.proc_pointer
7515 && !(e->expr_type == EXPR_VARIABLE
7516 && e->symtree->n.sym->attr.dummy))
7517 || (fsym->attr.proc_pointer
7518 && e->expr_type == EXPR_VARIABLE
7519 && gfc_is_proc_ptr_comp (e))
7520 || (fsym->attr.allocatable
7521 && fsym->attr.flavor != FL_PROCEDURE)))
7522 {
7523 /* Scalar pointer dummy args require an extra level of
7524 indirection. The null pointer already contains
7525 this level of indirection. */
7526 parm_kind = SCALAR_POINTER;
7527 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
7528 }
7529 }
7530 }
7531 else if (e->ts.type == BT_CLASS
7532 && fsym && fsym->ts.type == BT_CLASS
7533 && (CLASS_DATA (fsym)->attr.dimension
7534 || CLASS_DATA (fsym)->attr.codimension))
7535 {
7536 /* Pass a class array. */
7537 parmse.use_offset = 1;
7538 gfc_conv_expr_descriptor (&parmse, e);
7539 bool defer_to_dealloc_blk = false;
7540
7541 if (fsym->attr.optional
7542 && e->expr_type == EXPR_VARIABLE
7543 && e->symtree->n.sym->attr.optional)
7544 {
7545 stmtblock_t block;
7546
7547 gfc_init_block (&block);
7548 gfc_add_block_to_block (&block, &parmse.pre);
7549
7550 tree t = fold_build3_loc (input_location, COND_EXPR,
7551 void_type_node,
7552 gfc_conv_expr_present (e->symtree->n.sym),
7553 gfc_finish_block (&block),
7554 build_empty_stmt (input_location));
7555
7556 gfc_add_expr_to_block (&parmse.pre, t);
7557 }
7558
7559 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
7560 allocated on entry, it must be deallocated. */
7561 if (fsym->attr.intent == INTENT_OUT
7562 && CLASS_DATA (fsym)->attr.allocatable)
7563 {
7564 stmtblock_t block;
7565 tree ptr;
7566
7567 /* In case the data reference to deallocate is dependent on
7568 its own content, save the resulting pointer to a variable
7569 and only use that variable from now on, before the
7570 expression becomes invalid. */
7571 parmse.expr = gfc_evaluate_data_ref_now (parmse.expr,
7572 &parmse.pre);
7573
7574 if (parmse.class_container != NULL_TREE)
7575 parmse.class_container
7576 = gfc_evaluate_data_ref_now (parmse.class_container,
7577 &parmse.pre);
7578
7579 gfc_init_block (&block);
7580 ptr = parmse.expr;
7581 ptr = gfc_class_data_get (ptr);
7582
7583 tree cls = parmse.class_container;
7584 tmp = gfc_deallocate_with_status (ptr, NULL_TREE,
7585 NULL_TREE, NULL_TREE,
7586 NULL_TREE, true, e,
7587 GFC_CAF_COARRAY_NOCOARRAY,
7588 cls);
7589 gfc_add_expr_to_block (&block, tmp);
7590 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
7591 void_type_node, ptr,
7592 null_pointer_node);
7593 gfc_add_expr_to_block (&block, tmp);
7594 gfc_reset_vptr (&block, e, parmse.class_container);
7595
7596 if (fsym->attr.optional
7597 && e->expr_type == EXPR_VARIABLE
7598 && (!e->ref
7599 || (e->ref->type == REF_ARRAY
7600 && e->ref->u.ar.type != AR_FULL))
7601 && e->symtree->n.sym->attr.optional)
7602 {
7603 tmp = fold_build3_loc (input_location, COND_EXPR,
7604 void_type_node,
7605 gfc_conv_expr_present (e->symtree->n.sym),
7606 gfc_finish_block (&block),
7607 build_empty_stmt (input_location));
7608 }
7609 else
7610 tmp = gfc_finish_block (&block);
7611
7612 gfc_add_expr_to_block (&dealloc_blk, tmp);
7613 defer_to_dealloc_blk = true;
7614 }
7615
7616 gfc_se class_se = parmse;
7617 gfc_init_block (&class_se.pre);
7618 gfc_init_block (&class_se.post);
7619
7620 if (e->expr_type != EXPR_VARIABLE)
7621 {
7622 int n;
7623 /* Set the bounds and offset correctly. */
7624 for (n = 0; n < e->rank; n++)
7625 gfc_conv_shift_descriptor_lbound (&class_se.pre,
7626 class_se.expr,
7627 n, gfc_index_one_node);
7628 }
7629
7630 /* The conversion does not repackage the reference to a class
7631 array - _data descriptor. */
7632 gfc_conv_class_to_class (&class_se, e, fsym->ts, false,
7633 fsym->attr.intent != INTENT_IN
7634 && (CLASS_DATA (fsym)->attr.class_pointer
7635 || CLASS_DATA (fsym)->attr.allocatable),
7636 fsym->attr.optional
7637 && e->expr_type == EXPR_VARIABLE
7638 && e->symtree->n.sym->attr.optional,
7639 CLASS_DATA (fsym)->attr.class_pointer
7640 || CLASS_DATA (fsym)->attr.allocatable);
7641
7642 parmse.expr = class_se.expr;
7643 stmtblock_t *class_pre_block = defer_to_dealloc_blk
7644 ? &dealloc_blk
7645 : &parmse.pre;
7646 gfc_add_block_to_block (class_pre_block, &class_se.pre);
7647 gfc_add_block_to_block (&parmse.post, &class_se.post);
7648
7649 if (e->expr_type == EXPR_OP
7650 && POINTER_TYPE_P (TREE_TYPE (parmse.expr))
7651 && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (parmse.expr, 0))))
7652 {
7653 tree cond;
7654 tree dealloc_expr = gfc_finish_block (&parmse.post);
7655 tmp = TREE_OPERAND (parmse.expr, 0);
7656 gfc_init_block (&parmse.post);
7657 cond = gfc_class_data_get (tmp);
7658 tmp = gfc_deallocate_alloc_comp_no_caf (e->ts.u.derived,
7659 tmp, e->rank, true);
7660 gfc_add_expr_to_block (&parmse.post, tmp);
7661 cond = gfc_class_data_get (TREE_OPERAND (parmse.expr, 0));
7662 cond = gfc_conv_descriptor_data_get (cond);
7663 cond = fold_build2_loc (input_location, NE_EXPR,
7664 logical_type_node, cond,
7665 build_int_cst (TREE_TYPE (cond), 0));
7666 tmp = build3_v (COND_EXPR, cond, dealloc_expr,
7667 build_empty_stmt (input_location));
7668
7669 /* This specific case should not be processed further and so
7670 bundle everything up and proceed to the next argument. */
7671 if (fsym && need_interface_mapping && e)
7672 gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
7673 gfc_add_expr_to_block (&parmse.post, tmp);
7674 gfc_add_block_to_block (&se->pre, &parmse.pre);
7675 gfc_add_block_to_block (&post, &parmse.post);
7676 gfc_add_block_to_block (&se->finalblock, &parmse.finalblock);
7677 vec_safe_push (arglist, parmse.expr);
7678 continue;
7679 }
7680 }
7681 else
7682 {
7683 /* If the argument is a function call that may not create
7684 a temporary for the result, we have to check that we
7685 can do it, i.e. that there is no alias between this
7686 argument and another one. */
7687 if (gfc_get_noncopying_intrinsic_argument (e) != NULL)
7688 {
7689 gfc_expr *iarg;
7690 sym_intent intent;
7691
7692 if (fsym != NULL)
7693 intent = fsym->attr.intent;
7694 else
7695 intent = INTENT_UNKNOWN;
7696
7697 if (gfc_check_fncall_dependency (e, intent, sym, args,
7698 NOT_ELEMENTAL))
7699 parmse.force_tmp = 1;
7700
7701 iarg = e->value.function.actual->expr;
7702
7703 /* Temporary needed if aliasing due to host association. */
7704 if (sym->attr.contained
7705 && !sym->attr.pure
7706 && !sym->attr.implicit_pure
7707 && !sym->attr.use_assoc
7708 && iarg->expr_type == EXPR_VARIABLE
7709 && sym->ns == iarg->symtree->n.sym->ns)
7710 parmse.force_tmp = 1;
7711
7712 /* Ditto within module. */
7713 if (sym->attr.use_assoc
7714 && !sym->attr.pure
7715 && !sym->attr.implicit_pure
7716 && iarg->expr_type == EXPR_VARIABLE
7717 && sym->module == iarg->symtree->n.sym->module)
7718 parmse.force_tmp = 1;
7719 }
7720
7721 /* Special case for assumed-rank arrays: when passing an
7722 argument to a nonallocatable/nonpointer dummy, the bounds have
7723 to be reset as otherwise a last-dim ubound of -1 is
7724 indistinguishable from an assumed-size array in the callee. */
7725 if (!sym->attr.is_bind_c && e && fsym && fsym->as
7726 && fsym->as->type == AS_ASSUMED_RANK
7727 && e->rank != -1
7728 && e->expr_type == EXPR_VARIABLE
7729 && ((fsym->ts.type == BT_CLASS
7730 && !CLASS_DATA (fsym)->attr.class_pointer
7731 && !CLASS_DATA (fsym)->attr.allocatable)
7732 || (fsym->ts.type != BT_CLASS
7733 && !fsym->attr.pointer && !fsym->attr.allocatable)))
7734 {
7735 /* Change AR_FULL to a (:,:,:) ref to force bounds update. */
7736 gfc_ref *ref;
7737 for (ref = e->ref; ref->next; ref = ref->next)
7738 {
7739 if (ref->next->type == REF_INQUIRY)
7740 break;
7741 if (ref->type == REF_ARRAY
7742 && ref->u.ar.type != AR_ELEMENT)
7743 break;
7744 };
7745 if (ref->u.ar.type == AR_FULL
7746 && ref->u.ar.as->type != AS_ASSUMED_SIZE)
7747 ref->u.ar.type = AR_SECTION;
7748 }
7749
7750 if (sym->attr.is_bind_c && e && is_CFI_desc (fsym, NULL))
7751 /* Implement F2018, 18.3.6, list item (5), bullet point 2. */
7752 gfc_conv_gfc_desc_to_cfi_desc (&parmse, e, fsym);
7753
7754 else if (e->expr_type == EXPR_VARIABLE
7755 && is_subref_array (e)
7756 && !(fsym && fsym->attr.pointer))
7757 /* The actual argument is a component reference to an
7758 array of derived types. In this case, the argument
7759 is converted to a temporary, which is passed and then
7760 written back after the procedure call. */
7761 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
7762 fsym ? fsym->attr.intent : INTENT_INOUT,
7763 fsym && fsym->attr.pointer);
7764
7765 else if (e->ts.type == BT_CLASS && CLASS_DATA (e)->as
7766 && CLASS_DATA (e)->as->type == AS_ASSUMED_SIZE
7767 && nodesc_arg && fsym->ts.type == BT_DERIVED)
7768 /* An assumed size class actual argument being passed to
7769 a 'no descriptor' formal argument just requires the
7770 data pointer to be passed. For class dummy arguments
7771 this is stored in the symbol backend decl.. */
7772 parmse.expr = e->symtree->n.sym->backend_decl;
7773
7774 else if (gfc_is_class_array_ref (e, NULL)
7775 && fsym && fsym->ts.type == BT_DERIVED)
7776 /* The actual argument is a component reference to an
7777 array of derived types. In this case, the argument
7778 is converted to a temporary, which is passed and then
7779 written back after the procedure call.
7780 OOP-TODO: Insert code so that if the dynamic type is
7781 the same as the declared type, copy-in/copy-out does
7782 not occur. */
7783 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
7784 fsym->attr.intent,
7785 fsym->attr.pointer);
7786
7787 else if (gfc_is_class_array_function (e)
7788 && fsym && fsym->ts.type == BT_DERIVED)
7789 /* See previous comment. For function actual argument,
7790 the write out is not needed so the intent is set as
7791 intent in. */
7792 {
7793 e->must_finalize = 1;
7794 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
7795 INTENT_IN, fsym->attr.pointer);
7796 }
7797 else if (fsym && fsym->attr.contiguous
7798 && (fsym->attr.target
7799 ? gfc_is_not_contiguous (e)
7800 : !gfc_is_simply_contiguous (e, false, true))
7801 && gfc_expr_is_variable (e)
7802 && e->rank != -1)
7803 {
7804 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
7805 fsym->attr.intent,
7806 fsym->attr.pointer);
7807 }
7808 else
7809 /* This is where we introduce a temporary to store the
7810 result of a non-lvalue array expression. */
7811 gfc_conv_array_parameter (&parmse, e, nodesc_arg, fsym,
7812 sym->name, NULL);
7813
7814 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
7815 allocated on entry, it must be deallocated.
7816 CFI descriptors are handled elsewhere. */
7817 if (fsym && fsym->attr.allocatable
7818 && fsym->attr.intent == INTENT_OUT
7819 && !is_CFI_desc (fsym, NULL))
7820 {
7821 if (fsym->ts.type == BT_DERIVED
7822 && fsym->ts.u.derived->attr.alloc_comp)
7823 {
7824 // deallocate the components first
7825 tmp = gfc_deallocate_alloc_comp (fsym->ts.u.derived,
7826 parmse.expr, e->rank);
7827 /* But check whether dummy argument is optional. */
7828 if (tmp != NULL_TREE
7829 && fsym->attr.optional
7830 && e->expr_type == EXPR_VARIABLE
7831 && e->symtree->n.sym->attr.optional)
7832 {
7833 tree present;
7834 present = gfc_conv_expr_present (e->symtree->n.sym);
7835 tmp = build3_v (COND_EXPR, present, tmp,
7836 build_empty_stmt (input_location));
7837 }
7838 if (tmp != NULL_TREE)
7839 gfc_add_expr_to_block (&dealloc_blk, tmp);
7840 }
7841
7842 tmp = parmse.expr;
7843 /* With bind(C), the actual argument is replaced by a bind-C
7844 descriptor; in this case, the data component arrives here,
7845 which shall not be dereferenced, but still freed and
7846 nullified. */
7847 if (TREE_TYPE(tmp) != pvoid_type_node)
7848 tmp = build_fold_indirect_ref_loc (input_location,
7849 parmse.expr);
7850 tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
7851 NULL_TREE, NULL_TREE, true,
7852 e,
7853 GFC_CAF_COARRAY_NOCOARRAY);
7854 if (fsym->attr.optional
7855 && e->expr_type == EXPR_VARIABLE
7856 && e->symtree->n.sym->attr.optional)
7857 tmp = fold_build3_loc (input_location, COND_EXPR,
7858 void_type_node,
7859 gfc_conv_expr_present (e->symtree->n.sym),
7860 tmp, build_empty_stmt (input_location));
7861 gfc_add_expr_to_block (&dealloc_blk, tmp);
7862 }
7863 }
7864 }
7865 /* Special case for an assumed-rank dummy argument. */
7866 if (!sym->attr.is_bind_c && e && fsym && e->rank > 0
7867 && (fsym->ts.type == BT_CLASS
7868 ? (CLASS_DATA (fsym)->as
7869 && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
7870 : (fsym->as && fsym->as->type == AS_ASSUMED_RANK)))
7871 {
7872 if (fsym->ts.type == BT_CLASS
7873 ? (CLASS_DATA (fsym)->attr.class_pointer
7874 || CLASS_DATA (fsym)->attr.allocatable)
7875 : (fsym->attr.pointer || fsym->attr.allocatable))
7876 {
7877 /* Unallocated allocatable arrays and unassociated pointer
7878 arrays need their dtype setting if they are argument
7879 associated with assumed rank dummies to set the rank. */
7880 set_dtype_for_unallocated (&parmse, e);
7881 }
7882 else if (e->expr_type == EXPR_VARIABLE
7883 && e->symtree->n.sym->attr.dummy
7884 && (e->ts.type == BT_CLASS
7885 ? (e->ref && e->ref->next
7886 && e->ref->next->type == REF_ARRAY
7887 && e->ref->next->u.ar.type == AR_FULL
7888 && e->ref->next->u.ar.as->type == AS_ASSUMED_SIZE)
7889 : (e->ref && e->ref->type == REF_ARRAY
7890 && e->ref->u.ar.type == AR_FULL
7891 && e->ref->u.ar.as->type == AS_ASSUMED_SIZE)))
7892 {
7893 /* Assumed-size actual to assumed-rank dummy requires
7894 dim[rank-1].ubound = -1. */
7895 tree minus_one;
7896 tmp = build_fold_indirect_ref_loc (input_location, parmse.expr);
7897 if (fsym->ts.type == BT_CLASS)
7898 tmp = gfc_class_data_get (tmp);
7899 minus_one = build_int_cst (gfc_array_index_type, -1);
7900 gfc_conv_descriptor_ubound_set (&parmse.pre, tmp,
7901 gfc_rank_cst[e->rank - 1],
7902 minus_one);
7903 }
7904 }
7905
7906 /* The case with fsym->attr.optional is that of a user subroutine
7907 with an interface indicating an optional argument. When we call
7908 an intrinsic subroutine, however, fsym is NULL, but we might still
7909 have an optional argument, so we proceed to the substitution
7910 just in case. Arguments passed to bind(c) procedures via CFI
7911 descriptors are handled elsewhere. */
7912 if (e && (fsym == NULL || fsym->attr.optional)
7913 && !(sym->attr.is_bind_c && is_CFI_desc (fsym, NULL)))
7914 {
7915 /* If an optional argument is itself an optional dummy argument,
7916 check its presence and substitute a null if absent. This is
7917 only needed when passing an array to an elemental procedure
7918 as then array elements are accessed - or no NULL pointer is
7919 allowed and a "1" or "0" should be passed if not present.
7920 When passing a non-array-descriptor full array to a
7921 non-array-descriptor dummy, no check is needed. For
7922 array-descriptor actual to array-descriptor dummy, see
7923 PR 41911 for why a check has to be inserted.
7924 fsym == NULL is checked as intrinsics required the descriptor
7925 but do not always set fsym.
7926 Also, it is necessary to pass a NULL pointer to library routines
7927 which usually ignore optional arguments, so they can handle
7928 these themselves. */
7929 if (e->expr_type == EXPR_VARIABLE
7930 && e->symtree->n.sym->attr.optional
7931 && (((e->rank != 0 && elemental_proc)
7932 || e->representation.length || e->ts.type == BT_CHARACTER
7933 || (e->rank == 0 && e->symtree->n.sym->attr.value)
7934 || (e->rank != 0
7935 && (fsym == NULL
7936 || (fsym->as
7937 && (fsym->as->type == AS_ASSUMED_SHAPE
7938 || fsym->as->type == AS_ASSUMED_RANK
7939 || fsym->as->type == AS_DEFERRED)))))
7940 || se->ignore_optional))
7941 gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
7942 e->representation.length);
7943 }
7944
7945 /* Make the class container for the first argument available with class
7946 valued transformational functions. */
7947 if (argc == 0 && e && e->ts.type == BT_CLASS
7948 && isym && isym->transformational
7949 && se->ss && se->ss->info)
7950 {
7951 arg1_cntnr = parmse.expr;
7952 if (POINTER_TYPE_P (TREE_TYPE (arg1_cntnr)))
7953 arg1_cntnr = build_fold_indirect_ref_loc (input_location, arg1_cntnr);
7954 arg1_cntnr = gfc_get_class_from_expr (arg1_cntnr);
7955 se->ss->info->class_container = arg1_cntnr;
7956 }
7957
7958 /* Obtain the character length of an assumed character length procedure
7959 from the typespec of the actual argument. */
7960 if (e
7961 && parmse.string_length == NULL_TREE
7962 && e->ts.type == BT_PROCEDURE
7963 && e->symtree->n.sym->ts.type == BT_CHARACTER
7964 && e->symtree->n.sym->ts.u.cl->length != NULL
7965 && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7966 {
7967 gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
7968 parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
7969 }
7970
7971 if (fsym && e)
7972 {
7973 /* Obtain the character length for a NULL() actual with a character
7974 MOLD argument. Otherwise substitute a suitable dummy length.
7975 Here we handle non-optional dummies of non-bind(c) procedures. */
7976 if (e->expr_type == EXPR_NULL
7977 && fsym->ts.type == BT_CHARACTER
7978 && !fsym->attr.optional
7979 && !(sym->attr.is_bind_c && is_CFI_desc (fsym, NULL)))
7980 conv_null_actual (&parmse, e, fsym);
7981 }
7982
7983 /* If any actual argument of the procedure is allocatable and passed
7984 to an allocatable dummy with INTENT(OUT), we conservatively
7985 evaluate actual argument expressions before deallocations are
7986 performed and the procedure is executed. May create temporaries.
7987 This ensures we conform to F2023:15.5.3, 15.5.4. */
7988 if (e && fsym && force_eval_args
7989 && fsym->attr.intent != INTENT_OUT
7990 && !gfc_is_constant_expr (e))
7991 parmse.expr = gfc_evaluate_now (parmse.expr, &parmse.pre);
7992
7993 if (fsym && need_interface_mapping && e)
7994 gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
7995
7996 gfc_add_block_to_block (&se->pre, &parmse.pre);
7997 gfc_add_block_to_block (&post, &parmse.post);
7998 gfc_add_block_to_block (&se->finalblock, &parmse.finalblock);
7999
8000 /* Allocated allocatable components of derived types must be
8001 deallocated for non-variable scalars, array arguments to elemental
8002 procedures, and array arguments with descriptor to non-elemental
8003 procedures. As bounds information for descriptorless arrays is no
8004 longer available here, they are dealt with in trans-array.cc
8005 (gfc_conv_array_parameter). */
8006 if (e && (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS)
8007 && e->ts.u.derived->attr.alloc_comp
8008 && (e->rank == 0 || elemental_proc || !nodesc_arg)
8009 && !expr_may_alias_variables (e, elemental_proc))
8010 {
8011 int parm_rank;
8012 /* It is known the e returns a structure type with at least one
8013 allocatable component. When e is a function, ensure that the
8014 function is called once only by using a temporary variable. */
8015 if (!DECL_P (parmse.expr) && e->expr_type == EXPR_FUNCTION)
8016 parmse.expr = gfc_evaluate_now_loc (input_location,
8017 parmse.expr, &se->pre);
8018
8019 if ((fsym && fsym->attr.value) || e->expr_type == EXPR_ARRAY)
8020 tmp = parmse.expr;
8021 else
8022 tmp = build_fold_indirect_ref_loc (input_location,
8023 parmse.expr);
8024
8025 parm_rank = e->rank;
8026 switch (parm_kind)
8027 {
8028 case (ELEMENTAL):
8029 case (SCALAR):
8030 parm_rank = 0;
8031 break;
8032
8033 case (SCALAR_POINTER):
8034 tmp = build_fold_indirect_ref_loc (input_location,
8035 tmp);
8036 break;
8037 }
8038
8039 if (e->ts.type == BT_DERIVED && fsym && fsym->ts.type == BT_CLASS)
8040 {
8041 /* The derived type is passed to gfc_deallocate_alloc_comp.
8042 Therefore, class actuals can be handled correctly but derived
8043 types passed to class formals need the _data component. */
8044 tmp = gfc_class_data_get (tmp);
8045 if (!CLASS_DATA (fsym)->attr.dimension)
8046 {
8047 if (UNLIMITED_POLY (fsym))
8048 {
8049 tree type = gfc_typenode_for_spec (&e->ts);
8050 type = build_pointer_type (type);
8051 tmp = fold_convert (type, tmp);
8052 }
8053 tmp = build_fold_indirect_ref_loc (input_location, tmp);
8054 }
8055 }
8056
8057 if (e->expr_type == EXPR_OP
8058 && e->value.op.op == INTRINSIC_PARENTHESES
8059 && e->value.op.op1->expr_type == EXPR_VARIABLE)
8060 {
8061 tree local_tmp;
8062 local_tmp = gfc_evaluate_now (tmp, &se->pre);
8063 local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp,
8064 parm_rank, 0);
8065 gfc_add_expr_to_block (&se->post, local_tmp);
8066 }
8067
8068 /* Items of array expressions passed to a polymorphic formal arguments
8069 create their own clean up, so prevent double free. */
8070 if (!finalized && !e->must_finalize
8071 && !(e->expr_type == EXPR_ARRAY && fsym
8072 && fsym->ts.type == BT_CLASS))
8073 {
8074 bool scalar_res_outside_loop;
8075 scalar_res_outside_loop = e->expr_type == EXPR_FUNCTION
8076 && parm_rank == 0
8077 && parmse.loop;
8078
8079 /* Scalars passed to an assumed rank argument are converted to
8080 a descriptor. Obtain the data field before deallocating any
8081 allocatable components. */
8082 if (parm_rank == 0 && e->expr_type != EXPR_ARRAY
8083 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
8084 tmp = gfc_conv_descriptor_data_get (tmp);
8085
8086 if (scalar_res_outside_loop)
8087 {
8088 /* Go through the ss chain to find the argument and use
8089 the stored value. */
8090 gfc_ss *tmp_ss = parmse.loop->ss;
8091 for (; tmp_ss; tmp_ss = tmp_ss->next)
8092 if (tmp_ss->info
8093 && tmp_ss->info->expr == e
8094 && tmp_ss->info->data.scalar.value != NULL_TREE)
8095 {
8096 tmp = tmp_ss->info->data.scalar.value;
8097 break;
8098 }
8099 }
8100
8101 STRIP_NOPS (tmp);
8102
8103 if (derived_array != NULL_TREE)
8104 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived,
8105 derived_array,
8106 parm_rank);
8107 else if ((e->ts.type == BT_CLASS
8108 && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
8109 || e->ts.type == BT_DERIVED)
8110 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp,
8111 parm_rank);
8112 else if (e->ts.type == BT_CLASS)
8113 tmp = gfc_deallocate_alloc_comp (CLASS_DATA (e)->ts.u.derived,
8114 tmp, parm_rank);
8115
8116 if (scalar_res_outside_loop)
8117 gfc_add_expr_to_block (&parmse.loop->post, tmp);
8118 else
8119 gfc_prepend_expr_to_block (&post, tmp);
8120 }
8121 }
8122
8123 /* Add argument checking of passing an unallocated/NULL actual to
8124 a nonallocatable/nonpointer dummy. */
8125
8126 if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
8127 {
8128 symbol_attribute attr;
8129 char *msg;
8130 tree cond;
8131 tree tmp;
8132 symbol_attribute fsym_attr;
8133
8134 if (fsym)
8135 {
8136 if (fsym->ts.type == BT_CLASS)
8137 {
8138 fsym_attr = CLASS_DATA (fsym)->attr;
8139 fsym_attr.pointer = fsym_attr.class_pointer;
8140 }
8141 else
8142 fsym_attr = fsym->attr;
8143 }
8144
8145 if (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
8146 attr = gfc_expr_attr (e);
8147 else
8148 goto end_pointer_check;
8149
8150 /* In Fortran 2008 it's allowed to pass a NULL pointer/nonallocated
8151 allocatable to an optional dummy, cf. 12.5.2.12. */
8152 if (fsym != NULL && fsym->attr.optional && !attr.proc_pointer
8153 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
8154 goto end_pointer_check;
8155
8156 if (attr.optional)
8157 {
8158 /* If the actual argument is an optional pointer/allocatable and
8159 the formal argument takes an nonpointer optional value,
8160 it is invalid to pass a non-present argument on, even
8161 though there is no technical reason for this in gfortran.
8162 See Fortran 2003, Section 12.4.1.6 item (7)+(8). */
8163 tree present, null_ptr, type;
8164
8165 if (attr.allocatable
8166 && (fsym == NULL || !fsym_attr.allocatable))
8167 msg = xasprintf ("Allocatable actual argument '%s' is not "
8168 "allocated or not present",
8169 e->symtree->n.sym->name);
8170 else if (attr.pointer
8171 && (fsym == NULL || !fsym_attr.pointer))
8172 msg = xasprintf ("Pointer actual argument '%s' is not "
8173 "associated or not present",
8174 e->symtree->n.sym->name);
8175 else if (attr.proc_pointer && !e->value.function.actual
8176 && (fsym == NULL || !fsym_attr.proc_pointer))
8177 msg = xasprintf ("Proc-pointer actual argument '%s' is not "
8178 "associated or not present",
8179 e->symtree->n.sym->name);
8180 else
8181 goto end_pointer_check;
8182
8183 present = gfc_conv_expr_present (e->symtree->n.sym);
8184 type = TREE_TYPE (present);
8185 present = fold_build2_loc (input_location, EQ_EXPR,
8186 logical_type_node, present,
8187 fold_convert (type,
8188 null_pointer_node));
8189 type = TREE_TYPE (parmse.expr);
8190 null_ptr = fold_build2_loc (input_location, EQ_EXPR,
8191 logical_type_node, parmse.expr,
8192 fold_convert (type,
8193 null_pointer_node));
8194 cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
8195 logical_type_node, present, null_ptr);
8196 }
8197 else
8198 {
8199 if (attr.allocatable
8200 && (fsym == NULL || !fsym_attr.allocatable))
8201 msg = xasprintf ("Allocatable actual argument '%s' is not "
8202 "allocated", e->symtree->n.sym->name);
8203 else if (attr.pointer
8204 && (fsym == NULL || !fsym_attr.pointer))
8205 msg = xasprintf ("Pointer actual argument '%s' is not "
8206 "associated", e->symtree->n.sym->name);
8207 else if (attr.proc_pointer && !e->value.function.actual
8208 && (fsym == NULL
8209 || (!fsym_attr.proc_pointer && !fsym_attr.optional)))
8210 msg = xasprintf ("Proc-pointer actual argument '%s' is not "
8211 "associated", e->symtree->n.sym->name);
8212 else
8213 goto end_pointer_check;
8214
8215 tmp = parmse.expr;
8216 if (fsym && fsym->ts.type == BT_CLASS && !attr.proc_pointer)
8217 {
8218 if (POINTER_TYPE_P (TREE_TYPE (tmp)))
8219 tmp = build_fold_indirect_ref_loc (input_location, tmp);
8220 tmp = gfc_class_data_get (tmp);
8221 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
8222 tmp = gfc_conv_descriptor_data_get (tmp);
8223 }
8224
8225 /* If the argument is passed by value, we need to strip the
8226 INDIRECT_REF. */
8227 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
8228 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
8229
8230 cond = fold_build2_loc (input_location, EQ_EXPR,
8231 logical_type_node, tmp,
8232 fold_convert (TREE_TYPE (tmp),
8233 null_pointer_node));
8234 }
8235
8236 gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
8237 msg);
8238 free (msg);
8239 }
8240 end_pointer_check:
8241
8242 /* Deferred length dummies pass the character length by reference
8243 so that the value can be returned. */
8244 if (parmse.string_length && fsym && fsym->ts.deferred)
8245 {
8246 if (INDIRECT_REF_P (parmse.string_length))
8247 {
8248 /* In chains of functions/procedure calls the string_length already
8249 is a pointer to the variable holding the length. Therefore
8250 remove the deref on call. */
8251 tmp = parmse.string_length;
8252 parmse.string_length = TREE_OPERAND (parmse.string_length, 0);
8253 }
8254 else
8255 {
8256 tmp = parmse.string_length;
8257 if (!VAR_P (tmp) && TREE_CODE (tmp) != COMPONENT_REF)
8258 tmp = gfc_evaluate_now (parmse.string_length, &se->pre);
8259 parmse.string_length = gfc_build_addr_expr (NULL_TREE, tmp);
8260 }
8261
8262 if (e && e->expr_type == EXPR_VARIABLE
8263 && fsym->attr.allocatable
8264 && e->ts.u.cl->backend_decl
8265 && VAR_P (e->ts.u.cl->backend_decl))
8266 {
8267 if (INDIRECT_REF_P (tmp))
8268 tmp = TREE_OPERAND (tmp, 0);
8269 gfc_add_modify (&se->post, e->ts.u.cl->backend_decl,
8270 fold_convert (gfc_charlen_type_node, tmp));
8271 }
8272 }
8273
8274 /* Character strings are passed as two parameters, a length and a
8275 pointer - except for Bind(c) and c_ptrs which only passe the pointer.
8276 An unlimited polymorphic formal argument likewise does not
8277 need the length. */
8278 if (parmse.string_length != NULL_TREE
8279 && !sym->attr.is_bind_c
8280 && !(fsym && fsym->ts.type == BT_DERIVED && fsym->ts.u.derived
8281 && fsym->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
8282 && fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING )
8283 && !(fsym && fsym->ts.type == BT_ASSUMED)
8284 && !(fsym && UNLIMITED_POLY (fsym)))
8285 vec_safe_push (stringargs, parmse.string_length);
8286
8287 /* When calling __copy for character expressions to unlimited
8288 polymorphic entities, the dst argument needs a string length. */
8289 if (sym->name[0] == '_' && e && e->ts.type == BT_CHARACTER
8290 && startswith (sym->name, "__vtab_CHARACTER")
8291 && arg->next && arg->next->expr
8292 && (arg->next->expr->ts.type == BT_DERIVED
8293 || arg->next->expr->ts.type == BT_CLASS)
8294 && arg->next->expr->ts.u.derived->attr.unlimited_polymorphic)
8295 vec_safe_push (stringargs, parmse.string_length);
8296
8297 /* For descriptorless coarrays and assumed-shape coarray dummies, we
8298 pass the token and the offset as additional arguments. */
8299 if (fsym && e == NULL && flag_coarray == GFC_FCOARRAY_LIB
8300 && attr->codimension && !attr->allocatable)
8301 {
8302 /* Token and offset. */
8303 vec_safe_push (stringargs, null_pointer_node);
8304 vec_safe_push (stringargs, build_int_cst (gfc_array_index_type, 0));
8305 gcc_assert (fsym->attr.optional);
8306 }
8307 else if (fsym && flag_coarray == GFC_FCOARRAY_LIB && attr->codimension
8308 && !attr->allocatable)
8309 {
8310 tree caf_decl, caf_type, caf_desc = NULL_TREE;
8311 tree offset, tmp2;
8312
8313 caf_decl = gfc_get_tree_for_caf_expr (e);
8314 caf_type = TREE_TYPE (caf_decl);
8315 if (POINTER_TYPE_P (caf_type)
8316 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_type)))
8317 caf_desc = TREE_TYPE (caf_type);
8318 else if (GFC_DESCRIPTOR_TYPE_P (caf_type))
8319 caf_desc = caf_type;
8320
8321 if (caf_desc
8322 && (GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_ALLOCATABLE
8323 || GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_POINTER))
8324 {
8325 tmp = POINTER_TYPE_P (TREE_TYPE (caf_decl))
8326 ? build_fold_indirect_ref (caf_decl)
8327 : caf_decl;
8328 tmp = gfc_conv_descriptor_token (tmp);
8329 }
8330 else if (DECL_LANG_SPECIFIC (caf_decl)
8331 && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
8332 tmp = GFC_DECL_TOKEN (caf_decl);
8333 else
8334 {
8335 gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
8336 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
8337 tmp = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
8338 }
8339
8340 vec_safe_push (stringargs, tmp);
8341
8342 if (caf_desc
8343 && GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_ALLOCATABLE)
8344 offset = build_int_cst (gfc_array_index_type, 0);
8345 else if (DECL_LANG_SPECIFIC (caf_decl)
8346 && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
8347 offset = GFC_DECL_CAF_OFFSET (caf_decl);
8348 else if (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) != NULL_TREE)
8349 offset = GFC_TYPE_ARRAY_CAF_OFFSET (caf_type);
8350 else
8351 offset = build_int_cst (gfc_array_index_type, 0);
8352
8353 if (caf_desc)
8354 {
8355 tmp = POINTER_TYPE_P (TREE_TYPE (caf_decl))
8356 ? build_fold_indirect_ref (caf_decl)
8357 : caf_decl;
8358 tmp = gfc_conv_descriptor_data_get (tmp);
8359 }
8360 else
8361 {
8362 gcc_assert (POINTER_TYPE_P (caf_type));
8363 tmp = caf_decl;
8364 }
8365
8366 tmp2 = fsym->ts.type == BT_CLASS
8367 ? gfc_class_data_get (parmse.expr) : parmse.expr;
8368 if ((fsym->ts.type != BT_CLASS
8369 && (fsym->as->type == AS_ASSUMED_SHAPE
8370 || fsym->as->type == AS_ASSUMED_RANK))
8371 || (fsym->ts.type == BT_CLASS
8372 && (CLASS_DATA (fsym)->as->type == AS_ASSUMED_SHAPE
8373 || CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)))
8374 {
8375 if (fsym->ts.type == BT_CLASS)
8376 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (tmp2)));
8377 else
8378 {
8379 gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
8380 tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
8381 }
8382 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)));
8383 tmp2 = gfc_conv_descriptor_data_get (tmp2);
8384 }
8385 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
8386 tmp2 = gfc_conv_descriptor_data_get (tmp2);
8387 else
8388 {
8389 gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
8390 }
8391
8392 tmp = fold_build2_loc (input_location, MINUS_EXPR,
8393 gfc_array_index_type,
8394 fold_convert (gfc_array_index_type, tmp2),
8395 fold_convert (gfc_array_index_type, tmp));
8396 offset = fold_build2_loc (input_location, PLUS_EXPR,
8397 gfc_array_index_type, offset, tmp);
8398
8399 vec_safe_push (stringargs, offset);
8400 }
8401
8402 vec_safe_push (arglist, parmse.expr);
8403 }
8404
8405 gfc_add_block_to_block (&se->pre, &dealloc_blk);
8406 gfc_add_block_to_block (&se->pre, &clobbers);
8407 gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
8408
8409 if (comp)
8410 ts = comp->ts;
8411 else if (sym->ts.type == BT_CLASS)
8412 ts = CLASS_DATA (sym)->ts;
8413 else
8414 ts = sym->ts;
8415
8416 if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
8417 se->string_length = build_int_cst (gfc_charlen_type_node, 1);
8418 else if (ts.type == BT_CHARACTER)
8419 {
8420 if (ts.u.cl->length == NULL)
8421 {
8422 /* Assumed character length results are not allowed by C418 of the 2003
8423 standard and are trapped in resolve.cc; except in the case of SPREAD
8424 (and other intrinsics?) and dummy functions. In the case of SPREAD,
8425 we take the character length of the first argument for the result.
8426 For dummies, we have to look through the formal argument list for
8427 this function and use the character length found there.
8428 Likewise, we handle the case of deferred-length character dummy
8429 arguments to intrinsics that determine the characteristics of
8430 the result, which cannot be deferred-length. */
8431 if (expr->value.function.isym)
8432 ts.deferred = false;
8433 if (ts.deferred)
8434 cl.backend_decl = gfc_create_var (gfc_charlen_type_node, "slen");
8435 else if (!sym->attr.dummy)
8436 cl.backend_decl = (*stringargs)[0];
8437 else
8438 {
8439 formal = gfc_sym_get_dummy_args (sym->ns->proc_name);
8440 for (; formal; formal = formal->next)
8441 if (strcmp (formal->sym->name, sym->name) == 0)
8442 cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
8443 }
8444 len = cl.backend_decl;
8445 }
8446 else
8447 {
8448 tree tmp;
8449
8450 /* Calculate the length of the returned string. */
8451 gfc_init_se (&parmse, NULL);
8452 if (need_interface_mapping)
8453 gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
8454 else
8455 gfc_conv_expr (&parmse, ts.u.cl->length);
8456 gfc_add_block_to_block (&se->pre, &parmse.pre);
8457 gfc_add_block_to_block (&se->post, &parmse.post);
8458 tmp = parmse.expr;
8459 /* TODO: It would be better to have the charlens as
8460 gfc_charlen_type_node already when the interface is
8461 created instead of converting it here (see PR 84615). */
8462 tmp = fold_build2_loc (input_location, MAX_EXPR,
8463 gfc_charlen_type_node,
8464 fold_convert (gfc_charlen_type_node, tmp),
8465 build_zero_cst (gfc_charlen_type_node));
8466 cl.backend_decl = tmp;
8467 }
8468
8469 /* Set up a charlen structure for it. */
8470 cl.next = NULL;
8471 cl.length = NULL;
8472 ts.u.cl = &cl;
8473
8474 len = cl.backend_decl;
8475 }
8476
8477 byref = (comp && (comp->attr.dimension
8478 || (comp->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)))
8479 || (!comp && gfc_return_by_reference (sym));
8480
8481 if (byref)
8482 {
8483 if (se->direct_byref)
8484 {
8485 /* Sometimes, too much indirection can be applied; e.g. for
8486 function_result = array_valued_recursive_function. */
8487 if (TREE_TYPE (TREE_TYPE (se->expr))
8488 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
8489 && GFC_DESCRIPTOR_TYPE_P
8490 (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
8491 se->expr = build_fold_indirect_ref_loc (input_location,
8492 se->expr);
8493
8494 /* If the lhs of an assignment x = f(..) is allocatable and
8495 f2003 is allowed, we must do the automatic reallocation.
8496 TODO - deal with intrinsics, without using a temporary. */
8497 if (flag_realloc_lhs
8498 && se->ss && se->ss->loop_chain
8499 && se->ss->loop_chain->is_alloc_lhs
8500 && !expr->value.function.isym
8501 && sym->result->as != NULL)
8502 {
8503 /* Evaluate the bounds of the result, if known. */
8504 gfc_set_loop_bounds_from_array_spec (&mapping, se,
8505 sym->result->as);
8506
8507 /* Perform the automatic reallocation. */
8508 tmp = gfc_alloc_allocatable_for_assignment (se->loop,
8509 expr, NULL);
8510 gfc_add_expr_to_block (&se->pre, tmp);
8511
8512 /* Pass the temporary as the first argument. */
8513 result = info->descriptor;
8514 }
8515 else
8516 result = build_fold_indirect_ref_loc (input_location,
8517 se->expr);
8518 vec_safe_push (retargs, se->expr);
8519 }
8520 else if (comp && comp->attr.dimension)
8521 {
8522 gcc_assert (se->loop && info);
8523
8524 /* Set the type of the array. vtable charlens are not always reliable.
8525 Use the interface, if possible. */
8526 if (comp->ts.type == BT_CHARACTER
8527 && expr->symtree->n.sym->ts.type == BT_CLASS
8528 && comp->ts.interface && comp->ts.interface->result)
8529 tmp = gfc_typenode_for_spec (&comp->ts.interface->result->ts);
8530 else
8531 tmp = gfc_typenode_for_spec (&comp->ts);
8532 gcc_assert (se->ss->dimen == se->loop->dimen);
8533
8534 /* Evaluate the bounds of the result, if known. */
8535 gfc_set_loop_bounds_from_array_spec (&mapping, se, comp->as);
8536
8537 /* If the lhs of an assignment x = f(..) is allocatable and
8538 f2003 is allowed, we must not generate the function call
8539 here but should just send back the results of the mapping.
8540 This is signalled by the function ss being flagged. */
8541 if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
8542 {
8543 gfc_free_interface_mapping (&mapping);
8544 return has_alternate_specifier;
8545 }
8546
8547 /* Create a temporary to store the result. In case the function
8548 returns a pointer, the temporary will be a shallow copy and
8549 mustn't be deallocated. */
8550 callee_alloc = comp->attr.allocatable || comp->attr.pointer;
8551 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
8552 tmp, NULL_TREE, false,
8553 !comp->attr.pointer, callee_alloc,
8554 &se->ss->info->expr->where);
8555
8556 /* Pass the temporary as the first argument. */
8557 result = info->descriptor;
8558 tmp = gfc_build_addr_expr (NULL_TREE, result);
8559 vec_safe_push (retargs, tmp);
8560 }
8561 else if (!comp && sym->result->attr.dimension)
8562 {
8563 gcc_assert (se->loop && info);
8564
8565 /* Set the type of the array. */
8566 tmp = gfc_typenode_for_spec (&ts);
8567 tmp = arg1_cntnr ? TREE_TYPE (arg1_cntnr) : tmp;
8568 gcc_assert (se->ss->dimen == se->loop->dimen);
8569
8570 /* Evaluate the bounds of the result, if known. */
8571 gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
8572
8573 /* If the lhs of an assignment x = f(..) is allocatable and
8574 f2003 is allowed, we must not generate the function call
8575 here but should just send back the results of the mapping.
8576 This is signalled by the function ss being flagged. */
8577 if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
8578 {
8579 gfc_free_interface_mapping (&mapping);
8580 return has_alternate_specifier;
8581 }
8582
8583 /* Create a temporary to store the result. In case the function
8584 returns a pointer, the temporary will be a shallow copy and
8585 mustn't be deallocated. */
8586 callee_alloc = sym->attr.allocatable || sym->attr.pointer;
8587 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
8588 tmp, NULL_TREE, false,
8589 !sym->attr.pointer, callee_alloc,
8590 &se->ss->info->expr->where);
8591
8592 /* Pass the temporary as the first argument. */
8593 result = info->descriptor;
8594 tmp = gfc_build_addr_expr (NULL_TREE, result);
8595 vec_safe_push (retargs, tmp);
8596 }
8597 else if (ts.type == BT_CHARACTER)
8598 {
8599 /* Pass the string length. */
8600 type = gfc_get_character_type (ts.kind, ts.u.cl);
8601 type = build_pointer_type (type);
8602
8603 /* Emit a DECL_EXPR for the VLA type. */
8604 tmp = TREE_TYPE (type);
8605 if (TYPE_SIZE (tmp)
8606 && TREE_CODE (TYPE_SIZE (tmp)) != INTEGER_CST)
8607 {
8608 tmp = build_decl (input_location, TYPE_DECL, NULL_TREE, tmp);
8609 DECL_ARTIFICIAL (tmp) = 1;
8610 DECL_IGNORED_P (tmp) = 1;
8611 tmp = fold_build1_loc (input_location, DECL_EXPR,
8612 TREE_TYPE (tmp), tmp);
8613 gfc_add_expr_to_block (&se->pre, tmp);
8614 }
8615
8616 /* Return an address to a char[0:len-1]* temporary for
8617 character pointers. */
8618 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
8619 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
8620 {
8621 var = gfc_create_var (type, "pstr");
8622
8623 if ((!comp && sym->attr.allocatable)
8624 || (comp && comp->attr.allocatable))
8625 {
8626 gfc_add_modify (&se->pre, var,
8627 fold_convert (TREE_TYPE (var),
8628 null_pointer_node));
8629 tmp = gfc_call_free (var);
8630 gfc_add_expr_to_block (&se->post, tmp);
8631 }
8632
8633 /* Provide an address expression for the function arguments. */
8634 var = gfc_build_addr_expr (NULL_TREE, var);
8635 }
8636 else
8637 var = gfc_conv_string_tmp (se, type, len);
8638
8639 vec_safe_push (retargs, var);
8640 }
8641 else
8642 {
8643 gcc_assert (flag_f2c && ts.type == BT_COMPLEX);
8644
8645 type = gfc_get_complex_type (ts.kind);
8646 var = gfc_build_addr_expr (NULL_TREE, gfc_create_var (type, "cmplx"));
8647 vec_safe_push (retargs, var);
8648 }
8649
8650 /* Add the string length to the argument list. */
8651 if (ts.type == BT_CHARACTER && ts.deferred)
8652 {
8653 tmp = len;
8654 if (!VAR_P (tmp))
8655 tmp = gfc_evaluate_now (len, &se->pre);
8656 TREE_STATIC (tmp) = 1;
8657 gfc_add_modify (&se->pre, tmp,
8658 build_int_cst (TREE_TYPE (tmp), 0));
8659 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
8660 vec_safe_push (retargs, tmp);
8661 }
8662 else if (ts.type == BT_CHARACTER)
8663 vec_safe_push (retargs, len);
8664 }
8665
8666 gfc_free_interface_mapping (&mapping);
8667
8668 /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS. */
8669 arglen = (vec_safe_length (arglist) + vec_safe_length (optionalargs)
8670 + vec_safe_length (stringargs) + vec_safe_length (append_args));
8671 vec_safe_reserve (retargs, arglen);
8672
8673 /* Add the return arguments. */
8674 vec_safe_splice (retargs, arglist);
8675
8676 /* Add the hidden present status for optional+value to the arguments. */
8677 vec_safe_splice (retargs, optionalargs);
8678
8679 /* Add the hidden string length parameters to the arguments. */
8680 vec_safe_splice (retargs, stringargs);
8681
8682 /* We may want to append extra arguments here. This is used e.g. for
8683 calls to libgfortran_matmul_??, which need extra information. */
8684 vec_safe_splice (retargs, append_args);
8685
8686 arglist = retargs;
8687
8688 /* Generate the actual call. */
8689 is_builtin = false;
8690 if (base_object == NULL_TREE)
8691 conv_function_val (se, &is_builtin, sym, expr, args);
8692 else
8693 conv_base_obj_fcn_val (se, base_object, expr);
8694
8695 /* If there are alternate return labels, function type should be
8696 integer. Can't modify the type in place though, since it can be shared
8697 with other functions. For dummy arguments, the typing is done to
8698 this result, even if it has to be repeated for each call. */
8699 if (has_alternate_specifier
8700 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
8701 {
8702 if (!sym->attr.dummy)
8703 {
8704 TREE_TYPE (sym->backend_decl)
8705 = build_function_type (integer_type_node,
8706 TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
8707 se->expr = gfc_build_addr_expr (NULL_TREE, sym->backend_decl);
8708 }
8709 else
8710 TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
8711 }
8712
8713 fntype = TREE_TYPE (TREE_TYPE (se->expr));
8714 se->expr = build_call_vec (TREE_TYPE (fntype), se->expr, arglist);
8715
8716 if (is_builtin)
8717 se->expr = update_builtin_function (se->expr, sym);
8718
8719 /* Allocatable scalar function results must be freed and nullified
8720 after use. This necessitates the creation of a temporary to
8721 hold the result to prevent duplicate calls. */
8722 symbol_attribute attr = comp ? comp->attr : sym->attr;
8723 bool allocatable = attr.allocatable && !attr.dimension;
8724 gfc_symbol *der = comp ?
8725 comp->ts.type == BT_DERIVED ? comp->ts.u.derived : NULL
8726 :
8727 sym->ts.type == BT_DERIVED ? sym->ts.u.derived : NULL;
8728 bool finalizable = der != NULL && der->ns->proc_name
8729 && gfc_is_finalizable (der, NULL);
8730
8731 if (!byref && finalizable)
8732 gfc_finalize_tree_expr (se, der, attr, expr->rank);
8733
8734 if (!byref && sym->ts.type != BT_CHARACTER
8735 && allocatable && !finalizable)
8736 {
8737 tmp = gfc_create_var (TREE_TYPE (se->expr), NULL);
8738 gfc_add_modify (&se->pre, tmp, se->expr);
8739 se->expr = tmp;
8740 tmp = gfc_call_free (tmp);
8741 gfc_add_expr_to_block (&post, tmp);
8742 gfc_add_modify (&post, se->expr, build_int_cst (TREE_TYPE (se->expr), 0));
8743 }
8744
8745 /* If we have a pointer function, but we don't want a pointer, e.g.
8746 something like
8747 x = f()
8748 where f is pointer valued, we have to dereference the result. */
8749 if (!se->want_pointer && !byref
8750 && ((!comp && (sym->attr.pointer || sym->attr.allocatable))
8751 || (comp && (comp->attr.pointer || comp->attr.allocatable))))
8752 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
8753
8754 /* f2c calling conventions require a scalar default real function to
8755 return a double precision result. Convert this back to default
8756 real. We only care about the cases that can happen in Fortran 77.
8757 */
8758 if (flag_f2c && sym->ts.type == BT_REAL
8759 && sym->ts.kind == gfc_default_real_kind
8760 && !sym->attr.pointer
8761 && !sym->attr.allocatable
8762 && !sym->attr.always_explicit)
8763 se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
8764
8765 /* A pure function may still have side-effects - it may modify its
8766 parameters. */
8767 TREE_SIDE_EFFECTS (se->expr) = 1;
8768 #if 0
8769 if (!sym->attr.pure)
8770 TREE_SIDE_EFFECTS (se->expr) = 1;
8771 #endif
8772
8773 if (byref)
8774 {
8775 /* Add the function call to the pre chain. There is no expression. */
8776 gfc_add_expr_to_block (&se->pre, se->expr);
8777 se->expr = NULL_TREE;
8778
8779 if (!se->direct_byref)
8780 {
8781 if ((sym->attr.dimension && !comp) || (comp && comp->attr.dimension))
8782 {
8783 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
8784 {
8785 /* Check the data pointer hasn't been modified. This would
8786 happen in a function returning a pointer. */
8787 tmp = gfc_conv_descriptor_data_get (info->descriptor);
8788 tmp = fold_build2_loc (input_location, NE_EXPR,
8789 logical_type_node,
8790 tmp, info->data);
8791 gfc_trans_runtime_check (true, false, tmp, &se->pre, NULL,
8792 gfc_msg_fault);
8793 }
8794 se->expr = info->descriptor;
8795 /* Bundle in the string length. */
8796 se->string_length = len;
8797
8798 if (finalizable)
8799 gfc_finalize_tree_expr (se, der, attr, expr->rank);
8800 }
8801 else if (ts.type == BT_CHARACTER)
8802 {
8803 /* Dereference for character pointer results. */
8804 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
8805 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
8806 se->expr = build_fold_indirect_ref_loc (input_location, var);
8807 else
8808 se->expr = var;
8809
8810 se->string_length = len;
8811 }
8812 else
8813 {
8814 gcc_assert (ts.type == BT_COMPLEX && flag_f2c);
8815 se->expr = build_fold_indirect_ref_loc (input_location, var);
8816 }
8817 }
8818 }
8819
8820 /* Associate the rhs class object's meta-data with the result, when the
8821 result is a temporary. */
8822 if (args && args->expr && args->expr->ts.type == BT_CLASS
8823 && sym->ts.type == BT_CLASS && result != NULL_TREE && DECL_P (result)
8824 && !GFC_CLASS_TYPE_P (TREE_TYPE (result)))
8825 {
8826 gfc_se parmse;
8827 gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (args->expr);
8828
8829 gfc_init_se (&parmse, NULL);
8830 parmse.data_not_needed = 1;
8831 gfc_conv_expr (&parmse, class_expr);
8832 if (!DECL_LANG_SPECIFIC (result))
8833 gfc_allocate_lang_decl (result);
8834 GFC_DECL_SAVED_DESCRIPTOR (result) = parmse.expr;
8835 gfc_free_expr (class_expr);
8836 /* -fcheck= can add diagnostic code, which has to be placed before
8837 the call. */
8838 if (parmse.pre.head != NULL)
8839 gfc_add_expr_to_block (&se->pre, parmse.pre.head);
8840 gcc_assert (parmse.post.head == NULL_TREE);
8841 }
8842
8843 /* Follow the function call with the argument post block. */
8844 if (byref)
8845 {
8846 gfc_add_block_to_block (&se->pre, &post);
8847
8848 /* Transformational functions of derived types with allocatable
8849 components must have the result allocatable components copied when the
8850 argument is actually given. This is unnecessry for REDUCE because the
8851 wrapper for the OPERATION function takes care of this. */
8852 arg = expr->value.function.actual;
8853 if (result && arg && expr->rank
8854 && isym && isym->transformational
8855 && isym->id != GFC_ISYM_REDUCE
8856 && arg->expr
8857 && arg->expr->ts.type == BT_DERIVED
8858 && arg->expr->ts.u.derived->attr.alloc_comp)
8859 {
8860 tree tmp2;
8861 /* Copy the allocatable components. We have to use a
8862 temporary here to prevent source allocatable components
8863 from being corrupted. */
8864 tmp2 = gfc_evaluate_now (result, &se->pre);
8865 tmp = gfc_copy_alloc_comp (arg->expr->ts.u.derived,
8866 result, tmp2, expr->rank, 0);
8867 gfc_add_expr_to_block (&se->pre, tmp);
8868 tmp = gfc_copy_allocatable_data (result, tmp2, TREE_TYPE(tmp2),
8869 expr->rank);
8870 gfc_add_expr_to_block (&se->pre, tmp);
8871
8872 /* Finally free the temporary's data field. */
8873 tmp = gfc_conv_descriptor_data_get (tmp2);
8874 tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
8875 NULL_TREE, NULL_TREE, true,
8876 NULL, GFC_CAF_COARRAY_NOCOARRAY);
8877 gfc_add_expr_to_block (&se->pre, tmp);
8878 }
8879 }
8880 else
8881 {
8882 /* For a function with a class array result, save the result as
8883 a temporary, set the info fields needed by the scalarizer and
8884 call the finalization function of the temporary. Note that the
8885 nullification of allocatable components needed by the result
8886 is done in gfc_trans_assignment_1. */
8887 if (expr && (gfc_is_class_array_function (expr)
8888 || gfc_is_alloc_class_scalar_function (expr))
8889 && se->expr && GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
8890 && expr->must_finalize)
8891 {
8892 /* TODO Eliminate the doubling of temporaries. This
8893 one is necessary to ensure no memory leakage. */
8894 se->expr = gfc_evaluate_now (se->expr, &se->pre);
8895
8896 /* Finalize the result, if necessary. */
8897 attr = expr->value.function.esym
8898 ? CLASS_DATA (expr->value.function.esym->result)->attr
8899 : CLASS_DATA (expr)->attr;
8900 if (!((gfc_is_class_array_function (expr)
8901 || gfc_is_alloc_class_scalar_function (expr))
8902 && attr.pointer))
8903 gfc_finalize_tree_expr (se, NULL, attr, expr->rank);
8904 }
8905 gfc_add_block_to_block (&se->post, &post);
8906 }
8907
8908 return has_alternate_specifier;
8909 }
8910
8911
8912 /* Fill a character string with spaces. */
8913
8914 static tree
8915 fill_with_spaces (tree start, tree type, tree size)
8916 {
8917 stmtblock_t block, loop;
8918 tree i, el, exit_label, cond, tmp;
8919
8920 /* For a simple char type, we can call memset(). */
8921 if (compare_tree_int (TYPE_SIZE_UNIT (type), 1) == 0)
8922 return build_call_expr_loc (input_location,
8923 builtin_decl_explicit (BUILT_IN_MEMSET),
8924 3, start,
8925 build_int_cst (gfc_get_int_type (gfc_c_int_kind),
8926 lang_hooks.to_target_charset (' ')),
8927 fold_convert (size_type_node, size));
8928
8929 /* Otherwise, we use a loop:
8930 for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
8931 *el = (type) ' ';
8932 */
8933
8934 /* Initialize variables. */
8935 gfc_init_block (&block);
8936 i = gfc_create_var (sizetype, "i");
8937 gfc_add_modify (&block, i, fold_convert (sizetype, size));
8938 el = gfc_create_var (build_pointer_type (type), "el");
8939 gfc_add_modify (&block, el, fold_convert (TREE_TYPE (el), start));
8940 exit_label = gfc_build_label_decl (NULL_TREE);
8941 TREE_USED (exit_label) = 1;
8942
8943
8944 /* Loop body. */
8945 gfc_init_block (&loop);
8946
8947 /* Exit condition. */
8948 cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, i,
8949 build_zero_cst (sizetype));
8950 tmp = build1_v (GOTO_EXPR, exit_label);
8951 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
8952 build_empty_stmt (input_location));
8953 gfc_add_expr_to_block (&loop, tmp);
8954
8955 /* Assignment. */
8956 gfc_add_modify (&loop,
8957 fold_build1_loc (input_location, INDIRECT_REF, type, el),
8958 build_int_cst (type, lang_hooks.to_target_charset (' ')));
8959
8960 /* Increment loop variables. */
8961 gfc_add_modify (&loop, i,
8962 fold_build2_loc (input_location, MINUS_EXPR, sizetype, i,
8963 TYPE_SIZE_UNIT (type)));
8964 gfc_add_modify (&loop, el,
8965 fold_build_pointer_plus_loc (input_location,
8966 el, TYPE_SIZE_UNIT (type)));
8967
8968 /* Making the loop... actually loop! */
8969 tmp = gfc_finish_block (&loop);
8970 tmp = build1_v (LOOP_EXPR, tmp);
8971 gfc_add_expr_to_block (&block, tmp);
8972
8973 /* The exit label. */
8974 tmp = build1_v (LABEL_EXPR, exit_label);
8975 gfc_add_expr_to_block (&block, tmp);
8976
8977
8978 return gfc_finish_block (&block);
8979 }
8980
8981
8982 /* Generate code to copy a string. */
8983
8984 void
8985 gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
8986 int dkind, tree slength, tree src, int skind)
8987 {
8988 tree tmp, dlen, slen;
8989 tree dsc;
8990 tree ssc;
8991 tree cond;
8992 tree cond2;
8993 tree tmp2;
8994 tree tmp3;
8995 tree tmp4;
8996 tree chartype;
8997 stmtblock_t tempblock;
8998
8999 gcc_assert (dkind == skind);
9000
9001 if (slength != NULL_TREE)
9002 {
9003 slen = gfc_evaluate_now (fold_convert (gfc_charlen_type_node, slength), block);
9004 ssc = gfc_string_to_single_character (slen, src, skind);
9005 }
9006 else
9007 {
9008 slen = build_one_cst (gfc_charlen_type_node);
9009 ssc = src;
9010 }
9011
9012 if (dlength != NULL_TREE)
9013 {
9014 dlen = gfc_evaluate_now (fold_convert (gfc_charlen_type_node, dlength), block);
9015 dsc = gfc_string_to_single_character (dlen, dest, dkind);
9016 }
9017 else
9018 {
9019 dlen = build_one_cst (gfc_charlen_type_node);
9020 dsc = dest;
9021 }
9022
9023 /* Assign directly if the types are compatible. */
9024 if (dsc != NULL_TREE && ssc != NULL_TREE
9025 && TREE_TYPE (dsc) == TREE_TYPE (ssc))
9026 {
9027 gfc_add_modify (block, dsc, ssc);
9028 return;
9029 }
9030
9031 /* The string copy algorithm below generates code like
9032
9033 if (destlen > 0)
9034 {
9035 if (srclen < destlen)
9036 {
9037 memmove (dest, src, srclen);
9038 // Pad with spaces.
9039 memset (&dest[srclen], ' ', destlen - srclen);
9040 }
9041 else
9042 {
9043 // Truncate if too long.
9044 memmove (dest, src, destlen);
9045 }
9046 }
9047 */
9048
9049 /* Do nothing if the destination length is zero. */
9050 cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node, dlen,
9051 build_zero_cst (TREE_TYPE (dlen)));
9052
9053 /* For non-default character kinds, we have to multiply the string
9054 length by the base type size. */
9055 chartype = gfc_get_char_type (dkind);
9056 slen = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (slen),
9057 slen,
9058 fold_convert (TREE_TYPE (slen),
9059 TYPE_SIZE_UNIT (chartype)));
9060 dlen = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (dlen),
9061 dlen,
9062 fold_convert (TREE_TYPE (dlen),
9063 TYPE_SIZE_UNIT (chartype)));
9064
9065 if (dlength && POINTER_TYPE_P (TREE_TYPE (dest)))
9066 dest = fold_convert (pvoid_type_node, dest);
9067 else
9068 dest = gfc_build_addr_expr (pvoid_type_node, dest);
9069
9070 if (slength && POINTER_TYPE_P (TREE_TYPE (src)))
9071 src = fold_convert (pvoid_type_node, src);
9072 else
9073 src = gfc_build_addr_expr (pvoid_type_node, src);
9074
9075 /* Truncate string if source is too long. */
9076 cond2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node, slen,
9077 dlen);
9078
9079 /* Pre-evaluate pointers unless one of the IF arms will be optimized away. */
9080 if (!CONSTANT_CLASS_P (cond2))
9081 {
9082 dest = gfc_evaluate_now (dest, block);
9083 src = gfc_evaluate_now (src, block);
9084 }
9085
9086 /* Copy and pad with spaces. */
9087 tmp3 = build_call_expr_loc (input_location,
9088 builtin_decl_explicit (BUILT_IN_MEMMOVE),
9089 3, dest, src,
9090 fold_convert (size_type_node, slen));
9091
9092 /* Wstringop-overflow appears at -O3 even though this warning is not
9093 explicitly available in fortran nor can it be switched off. If the
9094 source length is a constant, its negative appears as a very large
9095 positive number and triggers the warning in BUILTIN_MEMSET. Fixing
9096 the result of the MINUS_EXPR suppresses this spurious warning. */
9097 tmp = fold_build2_loc (input_location, MINUS_EXPR,
9098 TREE_TYPE(dlen), dlen, slen);
9099 if (slength && TREE_CONSTANT (slength))
9100 tmp = gfc_evaluate_now (tmp, block);
9101
9102 tmp4 = fold_build_pointer_plus_loc (input_location, dest, slen);
9103 tmp4 = fill_with_spaces (tmp4, chartype, tmp);
9104
9105 gfc_init_block (&tempblock);
9106 gfc_add_expr_to_block (&tempblock, tmp3);
9107 gfc_add_expr_to_block (&tempblock, tmp4);
9108 tmp3 = gfc_finish_block (&tempblock);
9109
9110 /* The truncated memmove if the slen >= dlen. */
9111 tmp2 = build_call_expr_loc (input_location,
9112 builtin_decl_explicit (BUILT_IN_MEMMOVE),
9113 3, dest, src,
9114 fold_convert (size_type_node, dlen));
9115
9116 /* The whole copy_string function is there. */
9117 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
9118 tmp3, tmp2);
9119 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
9120 build_empty_stmt (input_location));
9121 gfc_add_expr_to_block (block, tmp);
9122 }
9123
9124
9125 /* Translate a statement function.
9126 The value of a statement function reference is obtained by evaluating the
9127 expression using the values of the actual arguments for the values of the
9128 corresponding dummy arguments. */
9129
9130 static void
9131 gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
9132 {
9133 gfc_symbol *sym;
9134 gfc_symbol *fsym;
9135 gfc_formal_arglist *fargs;
9136 gfc_actual_arglist *args;
9137 gfc_se lse;
9138 gfc_se rse;
9139 gfc_saved_var *saved_vars;
9140 tree *temp_vars;
9141 tree type;
9142 tree tmp;
9143 int n;
9144
9145 sym = expr->symtree->n.sym;
9146 args = expr->value.function.actual;
9147 gfc_init_se (&lse, NULL);
9148 gfc_init_se (&rse, NULL);
9149
9150 n = 0;
9151 for (fargs = gfc_sym_get_dummy_args (sym); fargs; fargs = fargs->next)
9152 n++;
9153 saved_vars = XCNEWVEC (gfc_saved_var, n);
9154 temp_vars = XCNEWVEC (tree, n);
9155
9156 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
9157 fargs = fargs->next, n++)
9158 {
9159 /* Each dummy shall be specified, explicitly or implicitly, to be
9160 scalar. */
9161 gcc_assert (fargs->sym->attr.dimension == 0);
9162 fsym = fargs->sym;
9163
9164 if (fsym->ts.type == BT_CHARACTER)
9165 {
9166 /* Copy string arguments. */
9167 tree arglen;
9168
9169 gcc_assert (fsym->ts.u.cl && fsym->ts.u.cl->length
9170 && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT);
9171
9172 /* Create a temporary to hold the value. */
9173 if (fsym->ts.u.cl->backend_decl == NULL_TREE)
9174 fsym->ts.u.cl->backend_decl
9175 = gfc_conv_constant_to_tree (fsym->ts.u.cl->length);
9176
9177 type = gfc_get_character_type (fsym->ts.kind, fsym->ts.u.cl);
9178 temp_vars[n] = gfc_create_var (type, fsym->name);
9179
9180 arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
9181
9182 gfc_conv_expr (&rse, args->expr);
9183 gfc_conv_string_parameter (&rse);
9184 gfc_add_block_to_block (&se->pre, &lse.pre);
9185 gfc_add_block_to_block (&se->pre, &rse.pre);
9186
9187 gfc_trans_string_copy (&se->pre, arglen, temp_vars[n], fsym->ts.kind,
9188 rse.string_length, rse.expr, fsym->ts.kind);
9189 gfc_add_block_to_block (&se->pre, &lse.post);
9190 gfc_add_block_to_block (&se->pre, &rse.post);
9191 }
9192 else
9193 {
9194 /* For everything else, just evaluate the expression. */
9195
9196 /* Create a temporary to hold the value. */
9197 type = gfc_typenode_for_spec (&fsym->ts);
9198 temp_vars[n] = gfc_create_var (type, fsym->name);
9199
9200 gfc_conv_expr (&lse, args->expr);
9201
9202 gfc_add_block_to_block (&se->pre, &lse.pre);
9203 gfc_add_modify (&se->pre, temp_vars[n], lse.expr);
9204 gfc_add_block_to_block (&se->pre, &lse.post);
9205 }
9206
9207 args = args->next;
9208 }
9209
9210 /* Use the temporary variables in place of the real ones. */
9211 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
9212 fargs = fargs->next, n++)
9213 gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
9214
9215 gfc_conv_expr (se, sym->value);
9216
9217 if (sym->ts.type == BT_CHARACTER)
9218 {
9219 gfc_conv_const_charlen (sym->ts.u.cl);
9220
9221 /* Force the expression to the correct length. */
9222 if (!INTEGER_CST_P (se->string_length)
9223 || tree_int_cst_lt (se->string_length,
9224 sym->ts.u.cl->backend_decl))
9225 {
9226 type = gfc_get_character_type (sym->ts.kind, sym->ts.u.cl);
9227 tmp = gfc_create_var (type, sym->name);
9228 tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
9229 gfc_trans_string_copy (&se->pre, sym->ts.u.cl->backend_decl, tmp,
9230 sym->ts.kind, se->string_length, se->expr,
9231 sym->ts.kind);
9232 se->expr = tmp;
9233 }
9234 se->string_length = sym->ts.u.cl->backend_decl;
9235 }
9236
9237 /* Restore the original variables. */
9238 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
9239 fargs = fargs->next, n++)
9240 gfc_restore_sym (fargs->sym, &saved_vars[n]);
9241 free (temp_vars);
9242 free (saved_vars);
9243 }
9244
9245
9246 /* Translate a function expression. */
9247
9248 static void
9249 gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
9250 {
9251 gfc_symbol *sym;
9252
9253 if (expr->value.function.isym)
9254 {
9255 gfc_conv_intrinsic_function (se, expr);
9256 return;
9257 }
9258
9259 /* expr.value.function.esym is the resolved (specific) function symbol for
9260 most functions. However this isn't set for dummy procedures. */
9261 sym = expr->value.function.esym;
9262 if (!sym)
9263 sym = expr->symtree->n.sym;
9264
9265 /* The IEEE_ARITHMETIC functions are caught here. */
9266 if (sym->from_intmod == INTMOD_IEEE_ARITHMETIC)
9267 if (gfc_conv_ieee_arithmetic_function (se, expr))
9268 return;
9269
9270 /* We distinguish statement functions from general functions to improve
9271 runtime performance. */
9272 if (sym->attr.proc == PROC_ST_FUNCTION)
9273 {
9274 gfc_conv_statement_function (se, expr);
9275 return;
9276 }
9277
9278 gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
9279 NULL);
9280 }
9281
9282
9283 /* Determine whether the given EXPR_CONSTANT is a zero initializer. */
9284
9285 static bool
9286 is_zero_initializer_p (gfc_expr * expr)
9287 {
9288 if (expr->expr_type != EXPR_CONSTANT)
9289 return false;
9290
9291 /* We ignore constants with prescribed memory representations for now. */
9292 if (expr->representation.string)
9293 return false;
9294
9295 switch (expr->ts.type)
9296 {
9297 case BT_INTEGER:
9298 return mpz_cmp_si (expr->value.integer, 0) == 0;
9299
9300 case BT_REAL:
9301 return mpfr_zero_p (expr->value.real)
9302 && MPFR_SIGN (expr->value.real) >= 0;
9303
9304 case BT_LOGICAL:
9305 return expr->value.logical == 0;
9306
9307 case BT_COMPLEX:
9308 return mpfr_zero_p (mpc_realref (expr->value.complex))
9309 && MPFR_SIGN (mpc_realref (expr->value.complex)) >= 0
9310 && mpfr_zero_p (mpc_imagref (expr->value.complex))
9311 && MPFR_SIGN (mpc_imagref (expr->value.complex)) >= 0;
9312
9313 default:
9314 break;
9315 }
9316 return false;
9317 }
9318
9319
9320 static void
9321 gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
9322 {
9323 gfc_ss *ss;
9324
9325 ss = se->ss;
9326 gcc_assert (ss != NULL && ss != gfc_ss_terminator);
9327 gcc_assert (ss->info->expr == expr && ss->info->type == GFC_SS_CONSTRUCTOR);
9328
9329 gfc_conv_tmp_array_ref (se);
9330 }
9331
9332
9333 /* Build a static initializer. EXPR is the expression for the initial value.
9334 The other parameters describe the variable of the component being
9335 initialized. EXPR may be null. */
9336
9337 tree
9338 gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
9339 bool array, bool pointer, bool procptr)
9340 {
9341 gfc_se se;
9342
9343 if (flag_coarray != GFC_FCOARRAY_LIB && ts->type == BT_DERIVED
9344 && ts->u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
9345 && ts->u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
9346 return build_constructor (type, NULL);
9347
9348 if (!(expr || pointer || procptr))
9349 return NULL_TREE;
9350
9351 /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
9352 (these are the only two iso_c_binding derived types that can be
9353 used as initialization expressions). If so, we need to modify
9354 the 'expr' to be that for a (void *). */
9355 if (expr != NULL && expr->ts.type == BT_DERIVED
9356 && expr->ts.is_iso_c && expr->ts.u.derived)
9357 {
9358 if (TREE_CODE (type) == ARRAY_TYPE)
9359 return build_constructor (type, NULL);
9360 else if (POINTER_TYPE_P (type))
9361 return build_int_cst (type, 0);
9362 else
9363 gcc_unreachable ();
9364 }
9365
9366 if (array && !procptr)
9367 {
9368 tree ctor;
9369 /* Arrays need special handling. */
9370 if (pointer)
9371 ctor = gfc_build_null_descriptor (type);
9372 /* Special case assigning an array to zero. */
9373 else if (is_zero_initializer_p (expr))
9374 ctor = build_constructor (type, NULL);
9375 else
9376 ctor = gfc_conv_array_initializer (type, expr);
9377 TREE_STATIC (ctor) = 1;
9378 return ctor;
9379 }
9380 else if (pointer || procptr)
9381 {
9382 if (ts->type == BT_CLASS && !procptr)
9383 {
9384 gfc_init_se (&se, NULL);
9385 gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
9386 gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
9387 TREE_STATIC (se.expr) = 1;
9388 return se.expr;
9389 }
9390 else if (!expr || expr->expr_type == EXPR_NULL)
9391 return fold_convert (type, null_pointer_node);
9392 else
9393 {
9394 gfc_init_se (&se, NULL);
9395 se.want_pointer = 1;
9396 gfc_conv_expr (&se, expr);
9397 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
9398 return se.expr;
9399 }
9400 }
9401 else
9402 {
9403 switch (ts->type)
9404 {
9405 case_bt_struct:
9406 case BT_CLASS:
9407 gfc_init_se (&se, NULL);
9408 if (ts->type == BT_CLASS && expr->expr_type == EXPR_NULL)
9409 gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
9410 else
9411 gfc_conv_structure (&se, expr, 1);
9412 gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
9413 TREE_STATIC (se.expr) = 1;
9414 return se.expr;
9415
9416 case BT_CHARACTER:
9417 if (expr->expr_type == EXPR_CONSTANT)
9418 {
9419 tree ctor = gfc_conv_string_init (ts->u.cl->backend_decl, expr);
9420 TREE_STATIC (ctor) = 1;
9421 return ctor;
9422 }
9423
9424 /* Fallthrough. */
9425 default:
9426 gfc_init_se (&se, NULL);
9427 gfc_conv_constant (&se, expr);
9428 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
9429 return se.expr;
9430 }
9431 }
9432 }
9433
9434 static tree
9435 gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
9436 {
9437 gfc_se rse;
9438 gfc_se lse;
9439 gfc_ss *rss;
9440 gfc_ss *lss;
9441 gfc_array_info *lss_array;
9442 stmtblock_t body;
9443 stmtblock_t block;
9444 gfc_loopinfo loop;
9445 int n;
9446 tree tmp;
9447
9448 gfc_start_block (&block);
9449
9450 /* Initialize the scalarizer. */
9451 gfc_init_loopinfo (&loop);
9452
9453 gfc_init_se (&lse, NULL);
9454 gfc_init_se (&rse, NULL);
9455
9456 /* Walk the rhs. */
9457 rss = gfc_walk_expr (expr);
9458 if (rss == gfc_ss_terminator)
9459 /* The rhs is scalar. Add a ss for the expression. */
9460 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr);
9461
9462 /* Create a SS for the destination. */
9463 lss = gfc_get_array_ss (gfc_ss_terminator, NULL, cm->as->rank,
9464 GFC_SS_COMPONENT);
9465 lss_array = &lss->info->data.array;
9466 lss_array->shape = gfc_get_shape (cm->as->rank);
9467 lss_array->descriptor = dest;
9468 lss_array->data = gfc_conv_array_data (dest);
9469 lss_array->offset = gfc_conv_array_offset (dest);
9470 for (n = 0; n < cm->as->rank; n++)
9471 {
9472 lss_array->start[n] = gfc_conv_array_lbound (dest, n);
9473 lss_array->stride[n] = gfc_index_one_node;
9474
9475 mpz_init (lss_array->shape[n]);
9476 mpz_sub (lss_array->shape[n], cm->as->upper[n]->value.integer,
9477 cm->as->lower[n]->value.integer);
9478 mpz_add_ui (lss_array->shape[n], lss_array->shape[n], 1);
9479 }
9480
9481 /* Associate the SS with the loop. */
9482 gfc_add_ss_to_loop (&loop, lss);
9483 gfc_add_ss_to_loop (&loop, rss);
9484
9485 /* Calculate the bounds of the scalarization. */
9486 gfc_conv_ss_startstride (&loop);
9487
9488 /* Setup the scalarizing loops. */
9489 gfc_conv_loop_setup (&loop, &expr->where);
9490
9491 /* Setup the gfc_se structures. */
9492 gfc_copy_loopinfo_to_se (&lse, &loop);
9493 gfc_copy_loopinfo_to_se (&rse, &loop);
9494
9495 rse.ss = rss;
9496 gfc_mark_ss_chain_used (rss, 1);
9497 lse.ss = lss;
9498 gfc_mark_ss_chain_used (lss, 1);
9499
9500 /* Start the scalarized loop body. */
9501 gfc_start_scalarized_body (&loop, &body);
9502
9503 gfc_conv_tmp_array_ref (&lse);
9504 if (cm->ts.type == BT_CHARACTER)
9505 lse.string_length = cm->ts.u.cl->backend_decl;
9506
9507 gfc_conv_expr (&rse, expr);
9508
9509 tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false);
9510 gfc_add_expr_to_block (&body, tmp);
9511
9512 gcc_assert (rse.ss == gfc_ss_terminator);
9513
9514 /* Generate the copying loops. */
9515 gfc_trans_scalarizing_loops (&loop, &body);
9516
9517 /* Wrap the whole thing up. */
9518 gfc_add_block_to_block (&block, &loop.pre);
9519 gfc_add_block_to_block (&block, &loop.post);
9520
9521 gcc_assert (lss_array->shape != NULL);
9522 gfc_free_shape (&lss_array->shape, cm->as->rank);
9523 gfc_cleanup_loop (&loop);
9524
9525 return gfc_finish_block (&block);
9526 }
9527
9528
9529 static tree
9530 gfc_trans_alloc_subarray_assign (tree dest, gfc_component * cm,
9531 gfc_expr * expr)
9532 {
9533 gfc_se se;
9534 stmtblock_t block;
9535 tree offset;
9536 int n;
9537 tree tmp;
9538 tree tmp2;
9539 gfc_array_spec *as;
9540 gfc_expr *arg = NULL;
9541
9542 gfc_start_block (&block);
9543 gfc_init_se (&se, NULL);
9544
9545 /* Get the descriptor for the expressions. */
9546 se.want_pointer = 0;
9547 gfc_conv_expr_descriptor (&se, expr);
9548 gfc_add_block_to_block (&block, &se.pre);
9549 gfc_add_modify (&block, dest, se.expr);
9550 if (cm->ts.type == BT_CHARACTER
9551 && gfc_deferred_strlen (cm, &tmp))
9552 {
9553 tmp = fold_build3_loc (input_location, COMPONENT_REF,
9554 TREE_TYPE (tmp),
9555 TREE_OPERAND (dest, 0),
9556 tmp, NULL_TREE);
9557 gfc_add_modify (&block, tmp,
9558 fold_convert (TREE_TYPE (tmp),
9559 se.string_length));
9560 cm->ts.u.cl->backend_decl = gfc_create_var (gfc_charlen_type_node,
9561 "slen");
9562 gfc_add_modify (&block, cm->ts.u.cl->backend_decl, se.string_length);
9563 }
9564
9565 /* Deal with arrays of derived types with allocatable components. */
9566 if (gfc_bt_struct (cm->ts.type)
9567 && cm->ts.u.derived->attr.alloc_comp)
9568 // TODO: Fix caf_mode
9569 tmp = gfc_copy_alloc_comp (cm->ts.u.derived,
9570 se.expr, dest,
9571 cm->as->rank, 0);
9572 else if (cm->ts.type == BT_CLASS && expr->ts.type == BT_DERIVED
9573 && CLASS_DATA(cm)->attr.allocatable)
9574 {
9575 if (cm->ts.u.derived->attr.alloc_comp)
9576 // TODO: Fix caf_mode
9577 tmp = gfc_copy_alloc_comp (expr->ts.u.derived,
9578 se.expr, dest,
9579 expr->rank, 0);
9580 else
9581 {
9582 tmp = TREE_TYPE (dest);
9583 tmp = gfc_duplicate_allocatable (dest, se.expr,
9584 tmp, expr->rank, NULL_TREE);
9585 }
9586 }
9587 else if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
9588 tmp = gfc_duplicate_allocatable (dest, se.expr,
9589 gfc_typenode_for_spec (&cm->ts),
9590 cm->as->rank, NULL_TREE);
9591 else
9592 tmp = gfc_duplicate_allocatable (dest, se.expr,
9593 TREE_TYPE(cm->backend_decl),
9594 cm->as->rank, NULL_TREE);
9595
9596
9597 gfc_add_expr_to_block (&block, tmp);
9598 gfc_add_block_to_block (&block, &se.post);
9599
9600 if (expr->expr_type != EXPR_VARIABLE)
9601 gfc_conv_descriptor_data_set (&block, se.expr,
9602 null_pointer_node);
9603
9604 /* We need to know if the argument of a conversion function is a
9605 variable, so that the correct lower bound can be used. */
9606 if (expr->expr_type == EXPR_FUNCTION
9607 && expr->value.function.isym
9608 && expr->value.function.isym->conversion
9609 && expr->value.function.actual->expr
9610 && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE)
9611 arg = expr->value.function.actual->expr;
9612
9613 /* Obtain the array spec of full array references. */
9614 if (arg)
9615 as = gfc_get_full_arrayspec_from_expr (arg);
9616 else
9617 as = gfc_get_full_arrayspec_from_expr (expr);
9618
9619 /* Shift the lbound and ubound of temporaries to being unity,
9620 rather than zero, based. Always calculate the offset. */
9621 offset = gfc_conv_descriptor_offset_get (dest);
9622 gfc_add_modify (&block, offset, gfc_index_zero_node);
9623 tmp2 =gfc_create_var (gfc_array_index_type, NULL);
9624
9625 for (n = 0; n < expr->rank; n++)
9626 {
9627 tree span;
9628 tree lbound;
9629
9630 /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
9631 TODO It looks as if gfc_conv_expr_descriptor should return
9632 the correct bounds and that the following should not be
9633 necessary. This would simplify gfc_conv_intrinsic_bound
9634 as well. */
9635 if (as && as->lower[n])
9636 {
9637 gfc_se lbse;
9638 gfc_init_se (&lbse, NULL);
9639 gfc_conv_expr (&lbse, as->lower[n]);
9640 gfc_add_block_to_block (&block, &lbse.pre);
9641 lbound = gfc_evaluate_now (lbse.expr, &block);
9642 }
9643 else if (as && arg)
9644 {
9645 tmp = gfc_get_symbol_decl (arg->symtree->n.sym);
9646 lbound = gfc_conv_descriptor_lbound_get (tmp,
9647 gfc_rank_cst[n]);
9648 }
9649 else if (as)
9650 lbound = gfc_conv_descriptor_lbound_get (dest,
9651 gfc_rank_cst[n]);
9652 else
9653 lbound = gfc_index_one_node;
9654
9655 lbound = fold_convert (gfc_array_index_type, lbound);
9656
9657 /* Shift the bounds and set the offset accordingly. */
9658 tmp = gfc_conv_descriptor_ubound_get (dest, gfc_rank_cst[n]);
9659 span = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
9660 tmp, gfc_conv_descriptor_lbound_get (dest, gfc_rank_cst[n]));
9661 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
9662 span, lbound);
9663 gfc_conv_descriptor_ubound_set (&block, dest,
9664 gfc_rank_cst[n], tmp);
9665 gfc_conv_descriptor_lbound_set (&block, dest,
9666 gfc_rank_cst[n], lbound);
9667
9668 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
9669 gfc_conv_descriptor_lbound_get (dest,
9670 gfc_rank_cst[n]),
9671 gfc_conv_descriptor_stride_get (dest,
9672 gfc_rank_cst[n]));
9673 gfc_add_modify (&block, tmp2, tmp);
9674 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
9675 offset, tmp2);
9676 gfc_conv_descriptor_offset_set (&block, dest, tmp);
9677 }
9678
9679 if (arg)
9680 {
9681 /* If a conversion expression has a null data pointer
9682 argument, nullify the allocatable component. */
9683 tree non_null_expr;
9684 tree null_expr;
9685
9686 if (arg->symtree->n.sym->attr.allocatable
9687 || arg->symtree->n.sym->attr.pointer)
9688 {
9689 non_null_expr = gfc_finish_block (&block);
9690 gfc_start_block (&block);
9691 gfc_conv_descriptor_data_set (&block, dest,
9692 null_pointer_node);
9693 null_expr = gfc_finish_block (&block);
9694 tmp = gfc_conv_descriptor_data_get (arg->symtree->n.sym->backend_decl);
9695 tmp = build2_loc (input_location, EQ_EXPR, logical_type_node, tmp,
9696 fold_convert (TREE_TYPE (tmp), null_pointer_node));
9697 return build3_v (COND_EXPR, tmp,
9698 null_expr, non_null_expr);
9699 }
9700 }
9701
9702 return gfc_finish_block (&block);
9703 }
9704
9705
9706 /* Allocate or reallocate scalar component, as necessary. */
9707
9708 static void
9709 alloc_scalar_allocatable_subcomponent (stmtblock_t *block, tree comp,
9710 gfc_component *cm, gfc_expr *expr2,
9711 tree slen)
9712 {
9713 tree tmp;
9714 tree ptr;
9715 tree size;
9716 tree size_in_bytes;
9717 tree lhs_cl_size = NULL_TREE;
9718 gfc_se se;
9719
9720 if (!comp)
9721 return;
9722
9723 if (!expr2 || expr2->rank)
9724 return;
9725
9726 realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
9727
9728 if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
9729 {
9730 gcc_assert (expr2->ts.type == BT_CHARACTER);
9731 size = expr2->ts.u.cl->backend_decl;
9732 if (!size || !VAR_P (size))
9733 size = gfc_create_var (TREE_TYPE (slen), "slen");
9734 gfc_add_modify (block, size, slen);
9735
9736 gfc_deferred_strlen (cm, &tmp);
9737 lhs_cl_size = fold_build3_loc (input_location, COMPONENT_REF,
9738 gfc_charlen_type_node,
9739 TREE_OPERAND (comp, 0),
9740 tmp, NULL_TREE);
9741
9742 tmp = TREE_TYPE (gfc_typenode_for_spec (&cm->ts));
9743 tmp = TYPE_SIZE_UNIT (tmp);
9744 size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
9745 TREE_TYPE (tmp), tmp,
9746 fold_convert (TREE_TYPE (tmp), size));
9747 }
9748 else if (cm->ts.type == BT_CLASS)
9749 {
9750 if (expr2->ts.type != BT_CLASS)
9751 {
9752 if (expr2->ts.type == BT_CHARACTER)
9753 {
9754 gfc_init_se (&se, NULL);
9755 gfc_conv_expr (&se, expr2);
9756 size = build_int_cst (gfc_charlen_type_node, expr2->ts.kind);
9757 size = fold_build2_loc (input_location, MULT_EXPR,
9758 gfc_charlen_type_node,
9759 se.string_length, size);
9760 size = fold_convert (size_type_node, size);
9761 }
9762 else
9763 {
9764 if (expr2->ts.type == BT_DERIVED)
9765 tmp = gfc_get_symbol_decl (expr2->ts.u.derived);
9766 else
9767 tmp = gfc_typenode_for_spec (&expr2->ts);
9768 size = TYPE_SIZE_UNIT (tmp);
9769 }
9770 }
9771 else
9772 {
9773 gfc_expr *e2vtab;
9774 e2vtab = gfc_find_and_cut_at_last_class_ref (expr2);
9775 gfc_add_vptr_component (e2vtab);
9776 gfc_add_size_component (e2vtab);
9777 gfc_init_se (&se, NULL);
9778 gfc_conv_expr (&se, e2vtab);
9779 gfc_add_block_to_block (block, &se.pre);
9780 size = fold_convert (size_type_node, se.expr);
9781 gfc_free_expr (e2vtab);
9782 }
9783 size_in_bytes = size;
9784 }
9785 else
9786 {
9787 /* Otherwise use the length in bytes of the rhs. */
9788 size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&cm->ts));
9789 size_in_bytes = size;
9790 }
9791
9792 size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
9793 size_in_bytes, size_one_node);
9794
9795 if (cm->ts.type == BT_DERIVED && cm->ts.u.derived->attr.alloc_comp)
9796 {
9797 tmp = build_call_expr_loc (input_location,
9798 builtin_decl_explicit (BUILT_IN_CALLOC),
9799 2, build_one_cst (size_type_node),
9800 size_in_bytes);
9801 tmp = fold_convert (TREE_TYPE (comp), tmp);
9802 gfc_add_modify (block, comp, tmp);
9803 }
9804 else
9805 {
9806 tmp = build_call_expr_loc (input_location,
9807 builtin_decl_explicit (BUILT_IN_MALLOC),
9808 1, size_in_bytes);
9809 if (GFC_CLASS_TYPE_P (TREE_TYPE (comp)))
9810 ptr = gfc_class_data_get (comp);
9811 else
9812 ptr = comp;
9813 tmp = fold_convert (TREE_TYPE (ptr), tmp);
9814 gfc_add_modify (block, ptr, tmp);
9815 }
9816
9817 if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
9818 /* Update the lhs character length. */
9819 gfc_add_modify (block, lhs_cl_size,
9820 fold_convert (TREE_TYPE (lhs_cl_size), size));
9821 }
9822
9823
9824 /* Assign a single component of a derived type constructor. */
9825
9826 static tree
9827 gfc_trans_subcomponent_assign (tree dest, gfc_component * cm,
9828 gfc_expr * expr, bool init)
9829 {
9830 gfc_se se;
9831 gfc_se lse;
9832 stmtblock_t block;
9833 tree tmp;
9834 tree vtab;
9835
9836 gfc_start_block (&block);
9837
9838 if (cm->attr.pointer || cm->attr.proc_pointer)
9839 {
9840 /* Only care about pointers here, not about allocatables. */
9841 gfc_init_se (&se, NULL);
9842 /* Pointer component. */
9843 if ((cm->attr.dimension || cm->attr.codimension)
9844 && !cm->attr.proc_pointer)
9845 {
9846 /* Array pointer. */
9847 if (expr->expr_type == EXPR_NULL)
9848 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
9849 else
9850 {
9851 se.direct_byref = 1;
9852 se.expr = dest;
9853 gfc_conv_expr_descriptor (&se, expr);
9854 gfc_add_block_to_block (&block, &se.pre);
9855 gfc_add_block_to_block (&block, &se.post);
9856 }
9857 }
9858 else
9859 {
9860 /* Scalar pointers. */
9861 se.want_pointer = 1;
9862 gfc_conv_expr (&se, expr);
9863 gfc_add_block_to_block (&block, &se.pre);
9864
9865 if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
9866 && expr->symtree->n.sym->attr.dummy)
9867 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
9868
9869 gfc_add_modify (&block, dest,
9870 fold_convert (TREE_TYPE (dest), se.expr));
9871 gfc_add_block_to_block (&block, &se.post);
9872 }
9873 }
9874 else if (cm->ts.type == BT_CLASS && expr->expr_type == EXPR_NULL)
9875 {
9876 /* NULL initialization for CLASS components. */
9877 tmp = gfc_trans_structure_assign (dest,
9878 gfc_class_initializer (&cm->ts, expr),
9879 false);
9880 gfc_add_expr_to_block (&block, tmp);
9881 }
9882 else if ((cm->attr.dimension || cm->attr.codimension)
9883 && !cm->attr.proc_pointer)
9884 {
9885 if (cm->attr.allocatable && expr->expr_type == EXPR_NULL)
9886 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
9887 else if (cm->attr.allocatable || cm->attr.pdt_array)
9888 {
9889 tmp = gfc_trans_alloc_subarray_assign (dest, cm, expr);
9890 gfc_add_expr_to_block (&block, tmp);
9891 }
9892 else
9893 {
9894 tmp = gfc_trans_subarray_assign (dest, cm, expr);
9895 gfc_add_expr_to_block (&block, tmp);
9896 }
9897 }
9898 else if (cm->ts.type == BT_CLASS
9899 && CLASS_DATA (cm)->attr.dimension
9900 && CLASS_DATA (cm)->attr.allocatable
9901 && expr->ts.type == BT_DERIVED)
9902 {
9903 vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
9904 vtab = gfc_build_addr_expr (NULL_TREE, vtab);
9905 tmp = gfc_class_vptr_get (dest);
9906 gfc_add_modify (&block, tmp,
9907 fold_convert (TREE_TYPE (tmp), vtab));
9908 tmp = gfc_class_data_get (dest);
9909 tmp = gfc_trans_alloc_subarray_assign (tmp, cm, expr);
9910 gfc_add_expr_to_block (&block, tmp);
9911 }
9912 else if (cm->attr.allocatable && expr->expr_type == EXPR_NULL
9913 && (init
9914 || (cm->ts.type == BT_CHARACTER
9915 && !(cm->ts.deferred || cm->attr.pdt_string))))
9916 {
9917 /* NULL initialization for allocatable components.
9918 Deferred-length character is dealt with later. */
9919 gfc_add_modify (&block, dest, fold_convert (TREE_TYPE (dest),
9920 null_pointer_node));
9921 }
9922 else if (init && (cm->attr.allocatable
9923 || (cm->ts.type == BT_CLASS && CLASS_DATA (cm)->attr.allocatable
9924 && expr->ts.type != BT_CLASS)))
9925 {
9926 tree size;
9927
9928 gfc_init_se (&se, NULL);
9929 gfc_conv_expr (&se, expr);
9930
9931 /* The remainder of these instructions follow the if (cm->attr.pointer)
9932 if (!cm->attr.dimension) part above. */
9933 gfc_add_block_to_block (&block, &se.pre);
9934 /* Take care about non-array allocatable components here. The alloc_*
9935 routine below is motivated by the alloc_scalar_allocatable_for_
9936 assignment() routine, but with the realloc portions removed and
9937 different input. */
9938 alloc_scalar_allocatable_subcomponent (&block, dest, cm, expr,
9939 se.string_length);
9940
9941 if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
9942 && expr->symtree->n.sym->attr.dummy)
9943 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
9944
9945 if (cm->ts.type == BT_CLASS)
9946 {
9947 tmp = gfc_class_data_get (dest);
9948 tmp = build_fold_indirect_ref_loc (input_location, tmp);
9949 vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
9950 vtab = gfc_build_addr_expr (NULL_TREE, vtab);
9951 gfc_add_modify (&block, gfc_class_vptr_get (dest),
9952 fold_convert (TREE_TYPE (gfc_class_vptr_get (dest)), vtab));
9953 }
9954 else
9955 tmp = build_fold_indirect_ref_loc (input_location, dest);
9956
9957 /* For deferred strings insert a memcpy. */
9958 if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
9959 {
9960 gcc_assert (se.string_length || expr->ts.u.cl->backend_decl);
9961 size = size_of_string_in_bytes (cm->ts.kind, se.string_length
9962 ? se.string_length
9963 : expr->ts.u.cl->backend_decl);
9964 tmp = gfc_build_memcpy_call (tmp, se.expr, size);
9965 gfc_add_expr_to_block (&block, tmp);
9966 }
9967 else if (cm->ts.type == BT_CLASS)
9968 {
9969 /* Fix the expression for memcpy. */
9970 if (expr->expr_type != EXPR_VARIABLE)
9971 se.expr = gfc_evaluate_now (se.expr, &block);
9972
9973 if (expr->ts.type == BT_CHARACTER)
9974 {
9975 size = build_int_cst (gfc_charlen_type_node, expr->ts.kind);
9976 size = fold_build2_loc (input_location, MULT_EXPR,
9977 gfc_charlen_type_node,
9978 se.string_length, size);
9979 size = fold_convert (size_type_node, size);
9980 }
9981 else
9982 size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr->ts));
9983
9984 /* Now copy the expression to the constructor component _data. */
9985 gfc_add_expr_to_block (&block,
9986 gfc_build_memcpy_call (tmp, se.expr, size));
9987
9988 /* Fill the unlimited polymorphic _len field. */
9989 if (UNLIMITED_POLY (cm) && expr->ts.type == BT_CHARACTER)
9990 {
9991 tmp = gfc_class_len_get (gfc_get_class_from_expr (tmp));
9992 gfc_add_modify (&block, tmp,
9993 fold_convert (TREE_TYPE (tmp),
9994 se.string_length));
9995 }
9996 }
9997 else
9998 gfc_add_modify (&block, tmp,
9999 fold_convert (TREE_TYPE (tmp), se.expr));
10000 gfc_add_block_to_block (&block, &se.post);
10001 }
10002 else if (expr->ts.type == BT_UNION)
10003 {
10004 tree tmp;
10005 gfc_constructor *c = gfc_constructor_first (expr->value.constructor);
10006 /* We mark that the entire union should be initialized with a contrived
10007 EXPR_NULL expression at the beginning. */
10008 if (c != NULL && c->n.component == NULL
10009 && c->expr != NULL && c->expr->expr_type == EXPR_NULL)
10010 {
10011 tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
10012 dest, build_constructor (TREE_TYPE (dest), NULL));
10013 gfc_add_expr_to_block (&block, tmp);
10014 c = gfc_constructor_next (c);
10015 }
10016 /* The following constructor expression, if any, represents a specific
10017 map intializer, as given by the user. */
10018 if (c != NULL && c->expr != NULL)
10019 {
10020 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
10021 tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
10022 gfc_add_expr_to_block (&block, tmp);
10023 }
10024 }
10025 else if (expr->ts.type == BT_DERIVED && expr->ts.f90_type != BT_VOID)
10026 {
10027 if (expr->expr_type != EXPR_STRUCTURE)
10028 {
10029 tree dealloc = NULL_TREE;
10030 gfc_init_se (&se, NULL);
10031 gfc_conv_expr (&se, expr);
10032 gfc_add_block_to_block (&block, &se.pre);
10033 /* Prevent repeat evaluations in gfc_copy_alloc_comp by fixing the
10034 expression in a temporary variable and deallocate the allocatable
10035 components. Then we can the copy the expression to the result. */
10036 if (cm->ts.u.derived->attr.alloc_comp
10037 && expr->expr_type != EXPR_VARIABLE)
10038 {
10039 se.expr = gfc_evaluate_now (se.expr, &block);
10040 dealloc = gfc_deallocate_alloc_comp (cm->ts.u.derived, se.expr,
10041 expr->rank);
10042 }
10043 gfc_add_modify (&block, dest,
10044 fold_convert (TREE_TYPE (dest), se.expr));
10045 if (cm->ts.u.derived->attr.alloc_comp
10046 && expr->expr_type != EXPR_NULL)
10047 {
10048 // TODO: Fix caf_mode
10049 tmp = gfc_copy_alloc_comp (cm->ts.u.derived, se.expr,
10050 dest, expr->rank, 0);
10051 gfc_add_expr_to_block (&block, tmp);
10052 if (dealloc != NULL_TREE)
10053 gfc_add_expr_to_block (&block, dealloc);
10054 }
10055 gfc_add_block_to_block (&block, &se.post);
10056 }
10057 else
10058 {
10059 /* Nested constructors. */
10060 tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
10061 gfc_add_expr_to_block (&block, tmp);
10062 }
10063 }
10064 else if (gfc_deferred_strlen (cm, &tmp))
10065 {
10066 tree strlen;
10067 strlen = tmp;
10068 gcc_assert (strlen);
10069 strlen = fold_build3_loc (input_location, COMPONENT_REF,
10070 TREE_TYPE (strlen),
10071 TREE_OPERAND (dest, 0),
10072 strlen, NULL_TREE);
10073
10074 if (expr->expr_type == EXPR_NULL)
10075 {
10076 tmp = build_int_cst (TREE_TYPE (cm->backend_decl), 0);
10077 gfc_add_modify (&block, dest, tmp);
10078 tmp = build_int_cst (TREE_TYPE (strlen), 0);
10079 gfc_add_modify (&block, strlen, tmp);
10080 }
10081 else
10082 {
10083 tree size;
10084 gfc_init_se (&se, NULL);
10085 gfc_conv_expr (&se, expr);
10086 size = size_of_string_in_bytes (cm->ts.kind, se.string_length);
10087 size = fold_convert (size_type_node, size);
10088 tmp = build_call_expr_loc (input_location,
10089 builtin_decl_explicit (BUILT_IN_MALLOC),
10090 1, size);
10091 gfc_add_modify (&block, dest,
10092 fold_convert (TREE_TYPE (dest), tmp));
10093 gfc_add_modify (&block, strlen,
10094 fold_convert (TREE_TYPE (strlen), se.string_length));
10095 tmp = gfc_build_memcpy_call (dest, se.expr, size);
10096 gfc_add_expr_to_block (&block, tmp);
10097 }
10098 }
10099 else if (!cm->attr.artificial)
10100 {
10101 /* Scalar component (excluding deferred parameters). */
10102 gfc_init_se (&se, NULL);
10103 gfc_init_se (&lse, NULL);
10104
10105 gfc_conv_expr (&se, expr);
10106 if (cm->ts.type == BT_CHARACTER)
10107 lse.string_length = cm->ts.u.cl->backend_decl;
10108 lse.expr = dest;
10109 tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, false, false);
10110 gfc_add_expr_to_block (&block, tmp);
10111 }
10112 return gfc_finish_block (&block);
10113 }
10114
10115 /* Assign a derived type constructor to a variable. */
10116
10117 tree
10118 gfc_trans_structure_assign (tree dest, gfc_expr * expr, bool init, bool coarray)
10119 {
10120 gfc_constructor *c;
10121 gfc_component *cm;
10122 stmtblock_t block;
10123 tree field;
10124 tree tmp;
10125 gfc_se se;
10126
10127 gfc_start_block (&block);
10128
10129 if (expr->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
10130 && (expr->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
10131 || expr->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
10132 {
10133 gfc_se lse;
10134
10135 gfc_init_se (&se, NULL);
10136 gfc_init_se (&lse, NULL);
10137 gfc_conv_expr (&se, gfc_constructor_first (expr->value.constructor)->expr);
10138 lse.expr = dest;
10139 gfc_add_modify (&block, lse.expr,
10140 fold_convert (TREE_TYPE (lse.expr), se.expr));
10141
10142 return gfc_finish_block (&block);
10143 }
10144
10145 /* Make sure that the derived type has been completely built. */
10146 if (!expr->ts.u.derived->backend_decl
10147 || !TYPE_FIELDS (expr->ts.u.derived->backend_decl))
10148 {
10149 tmp = gfc_typenode_for_spec (&expr->ts);
10150 gcc_assert (tmp);
10151 }
10152
10153 cm = expr->ts.u.derived->components;
10154
10155
10156 if (coarray)
10157 gfc_init_se (&se, NULL);
10158
10159 for (c = gfc_constructor_first (expr->value.constructor);
10160 c; c = gfc_constructor_next (c), cm = cm->next)
10161 {
10162 /* Skip absent members in default initializers. */
10163 if (!c->expr && !cm->attr.allocatable)
10164 continue;
10165
10166 /* Register the component with the caf-lib before it is initialized.
10167 Register only allocatable components, that are not coarray'ed
10168 components (%comp[*]). Only register when the constructor is the
10169 null-expression. */
10170 if (coarray && !cm->attr.codimension
10171 && (cm->attr.allocatable || cm->attr.pointer)
10172 && (!c->expr || c->expr->expr_type == EXPR_NULL))
10173 {
10174 tree token, desc, size;
10175 bool is_array = cm->ts.type == BT_CLASS
10176 ? CLASS_DATA (cm)->attr.dimension : cm->attr.dimension;
10177
10178 field = cm->backend_decl;
10179 field = fold_build3_loc (input_location, COMPONENT_REF,
10180 TREE_TYPE (field), dest, field, NULL_TREE);
10181 if (cm->ts.type == BT_CLASS)
10182 field = gfc_class_data_get (field);
10183
10184 token
10185 = is_array
10186 ? gfc_conv_descriptor_token (field)
10187 : fold_build3_loc (input_location, COMPONENT_REF,
10188 TREE_TYPE (gfc_comp_caf_token (cm)), dest,
10189 gfc_comp_caf_token (cm), NULL_TREE);
10190
10191 if (is_array)
10192 {
10193 /* The _caf_register routine looks at the rank of the array
10194 descriptor to decide whether the data registered is an array
10195 or not. */
10196 int rank = cm->ts.type == BT_CLASS ? CLASS_DATA (cm)->as->rank
10197 : cm->as->rank;
10198 /* When the rank is not known just set a positive rank, which
10199 suffices to recognize the data as array. */
10200 if (rank < 0)
10201 rank = 1;
10202 size = build_zero_cst (size_type_node);
10203 desc = field;
10204 gfc_add_modify (&block, gfc_conv_descriptor_rank (desc),
10205 build_int_cst (signed_char_type_node, rank));
10206 }
10207 else
10208 {
10209 desc = gfc_conv_scalar_to_descriptor (&se, field,
10210 cm->ts.type == BT_CLASS
10211 ? CLASS_DATA (cm)->attr
10212 : cm->attr);
10213 size = TYPE_SIZE_UNIT (TREE_TYPE (field));
10214 }
10215 gfc_add_block_to_block (&block, &se.pre);
10216 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_register,
10217 7, size, build_int_cst (
10218 integer_type_node,
10219 GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY),
10220 gfc_build_addr_expr (pvoid_type_node,
10221 token),
10222 gfc_build_addr_expr (NULL_TREE, desc),
10223 null_pointer_node, null_pointer_node,
10224 integer_zero_node);
10225 gfc_add_expr_to_block (&block, tmp);
10226 }
10227 field = cm->backend_decl;
10228 gcc_assert(field);
10229 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
10230 dest, field, NULL_TREE);
10231 if (!c->expr)
10232 {
10233 gfc_expr *e = gfc_get_null_expr (NULL);
10234 tmp = gfc_trans_subcomponent_assign (tmp, cm, e, init);
10235 gfc_free_expr (e);
10236 }
10237 else
10238 tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr, init);
10239 gfc_add_expr_to_block (&block, tmp);
10240 }
10241 return gfc_finish_block (&block);
10242 }
10243
10244 static void
10245 gfc_conv_union_initializer (vec<constructor_elt, va_gc> *&v,
10246 gfc_component *un, gfc_expr *init)
10247 {
10248 gfc_constructor *ctor;
10249
10250 if (un->ts.type != BT_UNION || un == NULL || init == NULL)
10251 return;
10252
10253 ctor = gfc_constructor_first (init->value.constructor);
10254
10255 if (ctor == NULL || ctor->expr == NULL)
10256 return;
10257
10258 gcc_assert (init->expr_type == EXPR_STRUCTURE);
10259
10260 /* If we have an 'initialize all' constructor, do it first. */
10261 if (ctor->expr->expr_type == EXPR_NULL)
10262 {
10263 tree union_type = TREE_TYPE (un->backend_decl);
10264 tree val = build_constructor (union_type, NULL);
10265 CONSTRUCTOR_APPEND_ELT (v, un->backend_decl, val);
10266 ctor = gfc_constructor_next (ctor);
10267 }
10268
10269 /* Add the map initializer on top. */
10270 if (ctor != NULL && ctor->expr != NULL)
10271 {
10272 gcc_assert (ctor->expr->expr_type == EXPR_STRUCTURE);
10273 tree val = gfc_conv_initializer (ctor->expr, &un->ts,
10274 TREE_TYPE (un->backend_decl),
10275 un->attr.dimension, un->attr.pointer,
10276 un->attr.proc_pointer);
10277 CONSTRUCTOR_APPEND_ELT (v, un->backend_decl, val);
10278 }
10279 }
10280
10281 /* Build an expression for a constructor. If init is nonzero then
10282 this is part of a static variable initializer. */
10283
10284 void
10285 gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
10286 {
10287 gfc_constructor *c;
10288 gfc_component *cm;
10289 tree val;
10290 tree type;
10291 tree tmp;
10292 vec<constructor_elt, va_gc> *v = NULL;
10293
10294 gcc_assert (se->ss == NULL);
10295 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
10296 type = gfc_typenode_for_spec (&expr->ts);
10297
10298 if (!init)
10299 {
10300 /* Create a temporary variable and fill it in. */
10301 se->expr = gfc_create_var (type, expr->ts.u.derived->name);
10302 /* The symtree in expr is NULL, if the code to generate is for
10303 initializing the static members only. */
10304 tmp = gfc_trans_structure_assign (se->expr, expr, expr->symtree != NULL,
10305 se->want_coarray);
10306 gfc_add_expr_to_block (&se->pre, tmp);
10307 return;
10308 }
10309
10310 cm = expr->ts.u.derived->components;
10311
10312 for (c = gfc_constructor_first (expr->value.constructor);
10313 c && cm; c = gfc_constructor_next (c), cm = cm->next)
10314 {
10315 /* Skip absent members in default initializers and allocatable
10316 components. Although the latter have a default initializer
10317 of EXPR_NULL,... by default, the static nullify is not needed
10318 since this is done every time we come into scope. */
10319 if (!c->expr || (cm->attr.allocatable && cm->attr.flavor != FL_PROCEDURE))
10320 continue;
10321
10322 if (cm->initializer && cm->initializer->expr_type != EXPR_NULL
10323 && strcmp (cm->name, "_extends") == 0
10324 && cm->initializer->symtree)
10325 {
10326 tree vtab;
10327 gfc_symbol *vtabs;
10328 vtabs = cm->initializer->symtree->n.sym;
10329 vtab = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtabs));
10330 vtab = unshare_expr_without_location (vtab);
10331 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, vtab);
10332 }
10333 else if (cm->ts.u.derived && strcmp (cm->name, "_size") == 0)
10334 {
10335 val = TYPE_SIZE_UNIT (gfc_get_derived_type (cm->ts.u.derived));
10336 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
10337 fold_convert (TREE_TYPE (cm->backend_decl),
10338 val));
10339 }
10340 else if (cm->ts.type == BT_INTEGER && strcmp (cm->name, "_len") == 0)
10341 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
10342 fold_convert (TREE_TYPE (cm->backend_decl),
10343 integer_zero_node));
10344 else if (cm->ts.type == BT_UNION)
10345 gfc_conv_union_initializer (v, cm, c->expr);
10346 else
10347 {
10348 val = gfc_conv_initializer (c->expr, &cm->ts,
10349 TREE_TYPE (cm->backend_decl),
10350 cm->attr.dimension, cm->attr.pointer,
10351 cm->attr.proc_pointer);
10352 val = unshare_expr_without_location (val);
10353
10354 /* Append it to the constructor list. */
10355 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
10356 }
10357 }
10358
10359 se->expr = build_constructor (type, v);
10360 if (init)
10361 TREE_CONSTANT (se->expr) = 1;
10362 }
10363
10364
10365 /* Translate a substring expression. */
10366
10367 static void
10368 gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
10369 {
10370 gfc_ref *ref;
10371
10372 ref = expr->ref;
10373
10374 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
10375
10376 se->expr = gfc_build_wide_string_const (expr->ts.kind,
10377 expr->value.character.length,
10378 expr->value.character.string);
10379
10380 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
10381 TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
10382
10383 if (ref)
10384 gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
10385 }
10386
10387
10388 /* Entry point for expression translation. Evaluates a scalar quantity.
10389 EXPR is the expression to be translated, and SE is the state structure if
10390 called from within the scalarized. */
10391
10392 void
10393 gfc_conv_expr (gfc_se * se, gfc_expr * expr)
10394 {
10395 gfc_ss *ss;
10396
10397 ss = se->ss;
10398 if (ss && ss->info->expr == expr
10399 && (ss->info->type == GFC_SS_SCALAR
10400 || ss->info->type == GFC_SS_REFERENCE))
10401 {
10402 gfc_ss_info *ss_info;
10403
10404 ss_info = ss->info;
10405 /* Substitute a scalar expression evaluated outside the scalarization
10406 loop. */
10407 se->expr = ss_info->data.scalar.value;
10408 if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
10409 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
10410
10411 se->string_length = ss_info->string_length;
10412 gfc_advance_se_ss_chain (se);
10413 return;
10414 }
10415
10416 /* We need to convert the expressions for the iso_c_binding derived types.
10417 C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
10418 null_pointer_node. C_PTR and C_FUNPTR are converted to match the
10419 typespec for the C_PTR and C_FUNPTR symbols, which has already been
10420 updated to be an integer with a kind equal to the size of a (void *). */
10421 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->ts.f90_type == BT_VOID
10422 && expr->ts.u.derived->attr.is_bind_c)
10423 {
10424 if (expr->expr_type == EXPR_VARIABLE
10425 && (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
10426 || expr->symtree->n.sym->intmod_sym_id
10427 == ISOCBINDING_NULL_FUNPTR))
10428 {
10429 /* Set expr_type to EXPR_NULL, which will result in
10430 null_pointer_node being used below. */
10431 expr->expr_type = EXPR_NULL;
10432 }
10433 else
10434 {
10435 /* Update the type/kind of the expression to be what the new
10436 type/kind are for the updated symbols of C_PTR/C_FUNPTR. */
10437 expr->ts.type = BT_INTEGER;
10438 expr->ts.f90_type = BT_VOID;
10439 expr->ts.kind = gfc_index_integer_kind;
10440 }
10441 }
10442
10443 gfc_fix_class_refs (expr);
10444
10445 switch (expr->expr_type)
10446 {
10447 case EXPR_OP:
10448 gfc_conv_expr_op (se, expr);
10449 break;
10450
10451 case EXPR_FUNCTION:
10452 gfc_conv_function_expr (se, expr);
10453 break;
10454
10455 case EXPR_CONSTANT:
10456 gfc_conv_constant (se, expr);
10457 break;
10458
10459 case EXPR_VARIABLE:
10460 gfc_conv_variable (se, expr);
10461 break;
10462
10463 case EXPR_NULL:
10464 se->expr = null_pointer_node;
10465 break;
10466
10467 case EXPR_SUBSTRING:
10468 gfc_conv_substring_expr (se, expr);
10469 break;
10470
10471 case EXPR_STRUCTURE:
10472 gfc_conv_structure (se, expr, 0);
10473 /* F2008 4.5.6.3 para 5: If an executable construct references a
10474 structure constructor or array constructor, the entity created by
10475 the constructor is finalized after execution of the innermost
10476 executable construct containing the reference. This, in fact,
10477 was later deleted by the Combined Techical Corrigenda 1 TO 4 for
10478 fortran 2008 (f08/0011). */
10479 if ((gfc_option.allow_std & (GFC_STD_F2008 | GFC_STD_F2003))
10480 && !(gfc_option.allow_std & GFC_STD_GNU)
10481 && expr->must_finalize
10482 && gfc_may_be_finalized (expr->ts))
10483 {
10484 locus loc;
10485 gfc_locus_from_location (&loc, input_location);
10486 gfc_warning (0, "The structure constructor at %L has been"
10487 " finalized. This feature was removed by f08/0011."
10488 " Use -std=f2018 or -std=gnu to eliminate the"
10489 " finalization.", &loc);
10490 symbol_attribute attr;
10491 attr.allocatable = attr.pointer = 0;
10492 gfc_finalize_tree_expr (se, expr->ts.u.derived, attr, 0);
10493 gfc_add_block_to_block (&se->post, &se->finalblock);
10494 }
10495 break;
10496
10497 case EXPR_ARRAY:
10498 gfc_conv_array_constructor_expr (se, expr);
10499 gfc_add_block_to_block (&se->post, &se->finalblock);
10500 break;
10501
10502 default:
10503 gcc_unreachable ();
10504 break;
10505 }
10506 }
10507
10508 /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
10509 of an assignment. */
10510 void
10511 gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
10512 {
10513 gfc_conv_expr (se, expr);
10514 /* All numeric lvalues should have empty post chains. If not we need to
10515 figure out a way of rewriting an lvalue so that it has no post chain. */
10516 gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
10517 }
10518
10519 /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
10520 numeric expressions. Used for scalar values where inserting cleanup code
10521 is inconvenient. */
10522 void
10523 gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
10524 {
10525 tree val;
10526
10527 gcc_assert (expr->ts.type != BT_CHARACTER);
10528 gfc_conv_expr (se, expr);
10529 if (se->post.head)
10530 {
10531 val = gfc_create_var (TREE_TYPE (se->expr), NULL);
10532 gfc_add_modify (&se->pre, val, se->expr);
10533 se->expr = val;
10534 gfc_add_block_to_block (&se->pre, &se->post);
10535 }
10536 }
10537
10538 /* Helper to translate an expression and convert it to a particular type. */
10539 void
10540 gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
10541 {
10542 gfc_conv_expr_val (se, expr);
10543 se->expr = convert (type, se->expr);
10544 }
10545
10546
10547 /* Converts an expression so that it can be passed by reference. Scalar
10548 values only. */
10549
10550 void
10551 gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
10552 {
10553 gfc_ss *ss;
10554 tree var;
10555
10556 ss = se->ss;
10557 if (ss && ss->info->expr == expr
10558 && ss->info->type == GFC_SS_REFERENCE)
10559 {
10560 /* Returns a reference to the scalar evaluated outside the loop
10561 for this case. */
10562 gfc_conv_expr (se, expr);
10563
10564 if (expr->ts.type == BT_CHARACTER
10565 && expr->expr_type != EXPR_FUNCTION)
10566 gfc_conv_string_parameter (se);
10567 else
10568 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
10569
10570 return;
10571 }
10572
10573 if (expr->ts.type == BT_CHARACTER)
10574 {
10575 gfc_conv_expr (se, expr);
10576 gfc_conv_string_parameter (se);
10577 return;
10578 }
10579
10580 if (expr->expr_type == EXPR_VARIABLE)
10581 {
10582 se->want_pointer = 1;
10583 gfc_conv_expr (se, expr);
10584 if (se->post.head)
10585 {
10586 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
10587 gfc_add_modify (&se->pre, var, se->expr);
10588 gfc_add_block_to_block (&se->pre, &se->post);
10589 se->expr = var;
10590 }
10591 return;
10592 }
10593
10594 if (expr->expr_type == EXPR_FUNCTION
10595 && ((expr->value.function.esym
10596 && expr->value.function.esym->result
10597 && expr->value.function.esym->result->attr.pointer
10598 && !expr->value.function.esym->result->attr.dimension)
10599 || (!expr->value.function.esym && !expr->ref
10600 && expr->symtree->n.sym->attr.pointer
10601 && !expr->symtree->n.sym->attr.dimension)))
10602 {
10603 se->want_pointer = 1;
10604 gfc_conv_expr (se, expr);
10605 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
10606 gfc_add_modify (&se->pre, var, se->expr);
10607 se->expr = var;
10608 return;
10609 }
10610
10611 gfc_conv_expr (se, expr);
10612
10613 /* Create a temporary var to hold the value. */
10614 if (TREE_CONSTANT (se->expr))
10615 {
10616 tree tmp = se->expr;
10617 STRIP_TYPE_NOPS (tmp);
10618 var = build_decl (input_location,
10619 CONST_DECL, NULL, TREE_TYPE (tmp));
10620 DECL_INITIAL (var) = tmp;
10621 TREE_STATIC (var) = 1;
10622 pushdecl (var);
10623 }
10624 else
10625 {
10626 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
10627 gfc_add_modify (&se->pre, var, se->expr);
10628 }
10629
10630 if (!expr->must_finalize)
10631 gfc_add_block_to_block (&se->pre, &se->post);
10632
10633 /* Take the address of that value. */
10634 se->expr = gfc_build_addr_expr (NULL_TREE, var);
10635 }
10636
10637
10638 /* Get the _len component for an unlimited polymorphic expression. */
10639
10640 static tree
10641 trans_get_upoly_len (stmtblock_t *block, gfc_expr *expr)
10642 {
10643 gfc_se se;
10644 gfc_ref *ref = expr->ref;
10645
10646 gfc_init_se (&se, NULL);
10647 while (ref && ref->next)
10648 ref = ref->next;
10649 gfc_add_len_component (expr);
10650 gfc_conv_expr (&se, expr);
10651 gfc_add_block_to_block (block, &se.pre);
10652 gcc_assert (se.post.head == NULL_TREE);
10653 if (ref)
10654 {
10655 gfc_free_ref_list (ref->next);
10656 ref->next = NULL;
10657 }
10658 else
10659 {
10660 gfc_free_ref_list (expr->ref);
10661 expr->ref = NULL;
10662 }
10663 return se.expr;
10664 }
10665
10666
10667 /* Assign _vptr and _len components as appropriate. BLOCK should be a
10668 statement-list outside of the scalarizer-loop. When code is generated, that
10669 depends on the scalarized expression, it is added to RSE.PRE.
10670 Returns le's _vptr tree and when set the len expressions in to_lenp and
10671 from_lenp to form a le%_vptr%_copy (re, le, [from_lenp, to_lenp])
10672 expression. */
10673
10674 static tree
10675 trans_class_vptr_len_assignment (stmtblock_t *block, gfc_expr * le,
10676 gfc_expr * re, gfc_se *rse,
10677 tree * to_lenp, tree * from_lenp,
10678 tree * from_vptrp)
10679 {
10680 gfc_se se;
10681 gfc_expr * vptr_expr;
10682 tree tmp, to_len = NULL_TREE, from_len = NULL_TREE, lhs_vptr;
10683 bool set_vptr = false, temp_rhs = false;
10684 stmtblock_t *pre = block;
10685 tree class_expr = NULL_TREE;
10686 tree from_vptr = NULL_TREE;
10687
10688 /* Create a temporary for complicated expressions. */
10689 if (re->expr_type != EXPR_VARIABLE && re->expr_type != EXPR_NULL
10690 && rse->expr != NULL_TREE)
10691 {
10692 if (!DECL_P (rse->expr))
10693 {
10694 if (re->ts.type == BT_CLASS && !GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
10695 class_expr = gfc_get_class_from_expr (rse->expr);
10696
10697 if (rse->loop)
10698 pre = &rse->loop->pre;
10699 else
10700 pre = &rse->pre;
10701
10702 if (class_expr != NULL_TREE && UNLIMITED_POLY (re))
10703 tmp = gfc_evaluate_now (TREE_OPERAND (rse->expr, 0), &rse->pre);
10704 else
10705 tmp = gfc_evaluate_now (rse->expr, &rse->pre);
10706
10707 rse->expr = tmp;
10708 }
10709 else
10710 pre = &rse->pre;
10711
10712 temp_rhs = true;
10713 }
10714
10715 /* Get the _vptr for the left-hand side expression. */
10716 gfc_init_se (&se, NULL);
10717 vptr_expr = gfc_find_and_cut_at_last_class_ref (le);
10718 if (vptr_expr != NULL && gfc_expr_attr (vptr_expr).class_ok)
10719 {
10720 /* Care about _len for unlimited polymorphic entities. */
10721 if (UNLIMITED_POLY (vptr_expr)
10722 || (vptr_expr->ts.type == BT_DERIVED
10723 && vptr_expr->ts.u.derived->attr.unlimited_polymorphic))
10724 to_len = trans_get_upoly_len (block, vptr_expr);
10725 gfc_add_vptr_component (vptr_expr);
10726 set_vptr = true;
10727 }
10728 else
10729 vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&le->ts));
10730 se.want_pointer = 1;
10731 gfc_conv_expr (&se, vptr_expr);
10732 gfc_free_expr (vptr_expr);
10733 gfc_add_block_to_block (block, &se.pre);
10734 gcc_assert (se.post.head == NULL_TREE);
10735 lhs_vptr = se.expr;
10736 STRIP_NOPS (lhs_vptr);
10737
10738 /* Set the _vptr only when the left-hand side of the assignment is a
10739 class-object. */
10740 if (set_vptr)
10741 {
10742 /* Get the vptr from the rhs expression only, when it is variable.
10743 Functions are expected to be assigned to a temporary beforehand. */
10744 vptr_expr = (re->expr_type == EXPR_VARIABLE && re->ts.type == BT_CLASS)
10745 ? gfc_find_and_cut_at_last_class_ref (re)
10746 : NULL;
10747 if (vptr_expr != NULL && vptr_expr->ts.type == BT_CLASS)
10748 {
10749 if (to_len != NULL_TREE)
10750 {
10751 /* Get the _len information from the rhs. */
10752 if (UNLIMITED_POLY (vptr_expr)
10753 || (vptr_expr->ts.type == BT_DERIVED
10754 && vptr_expr->ts.u.derived->attr.unlimited_polymorphic))
10755 from_len = trans_get_upoly_len (block, vptr_expr);
10756 }
10757 gfc_add_vptr_component (vptr_expr);
10758 }
10759 else
10760 {
10761 if (re->expr_type == EXPR_VARIABLE
10762 && DECL_P (re->symtree->n.sym->backend_decl)
10763 && DECL_LANG_SPECIFIC (re->symtree->n.sym->backend_decl)
10764 && GFC_DECL_SAVED_DESCRIPTOR (re->symtree->n.sym->backend_decl)
10765 && GFC_CLASS_TYPE_P (TREE_TYPE (GFC_DECL_SAVED_DESCRIPTOR (
10766 re->symtree->n.sym->backend_decl))))
10767 {
10768 vptr_expr = NULL;
10769 se.expr = gfc_class_vptr_get (GFC_DECL_SAVED_DESCRIPTOR (
10770 re->symtree->n.sym->backend_decl));
10771 if (to_len && UNLIMITED_POLY (re))
10772 from_len = gfc_class_len_get (GFC_DECL_SAVED_DESCRIPTOR (
10773 re->symtree->n.sym->backend_decl));
10774 }
10775 else if (temp_rhs && re->ts.type == BT_CLASS)
10776 {
10777 vptr_expr = NULL;
10778 if (class_expr)
10779 tmp = class_expr;
10780 else if (!GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
10781 tmp = gfc_get_class_from_expr (rse->expr);
10782 else
10783 tmp = rse->expr;
10784
10785 se.expr = gfc_class_vptr_get (tmp);
10786 from_vptr = se.expr;
10787 if (UNLIMITED_POLY (re))
10788 from_len = gfc_class_len_get (tmp);
10789
10790 }
10791 else if (re->expr_type != EXPR_NULL)
10792 /* Only when rhs is non-NULL use its declared type for vptr
10793 initialisation. */
10794 vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&re->ts));
10795 else
10796 /* When the rhs is NULL use the vtab of lhs' declared type. */
10797 vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&le->ts));
10798 }
10799
10800 if (vptr_expr)
10801 {
10802 gfc_init_se (&se, NULL);
10803 se.want_pointer = 1;
10804 gfc_conv_expr (&se, vptr_expr);
10805 gfc_free_expr (vptr_expr);
10806 gfc_add_block_to_block (block, &se.pre);
10807 gcc_assert (se.post.head == NULL_TREE);
10808 from_vptr = se.expr;
10809 }
10810 gfc_add_modify (pre, lhs_vptr, fold_convert (TREE_TYPE (lhs_vptr),
10811 se.expr));
10812
10813 if (to_len != NULL_TREE)
10814 {
10815 /* The _len component needs to be set. Figure how to get the
10816 value of the right-hand side. */
10817 if (from_len == NULL_TREE)
10818 {
10819 if (rse->string_length != NULL_TREE)
10820 from_len = rse->string_length;
10821 else if (re->ts.type == BT_CHARACTER && re->ts.u.cl->length)
10822 {
10823 gfc_init_se (&se, NULL);
10824 gfc_conv_expr (&se, re->ts.u.cl->length);
10825 gfc_add_block_to_block (block, &se.pre);
10826 gcc_assert (se.post.head == NULL_TREE);
10827 from_len = gfc_evaluate_now (se.expr, block);
10828 }
10829 else
10830 from_len = build_zero_cst (gfc_charlen_type_node);
10831 }
10832 gfc_add_modify (pre, to_len, fold_convert (TREE_TYPE (to_len),
10833 from_len));
10834 }
10835 }
10836
10837 /* Return the _len and _vptr trees only, when requested. */
10838 if (to_lenp)
10839 *to_lenp = to_len;
10840 if (from_lenp)
10841 *from_lenp = from_len;
10842 if (from_vptrp)
10843 *from_vptrp = from_vptr;
10844 return lhs_vptr;
10845 }
10846
10847
10848 /* Assign tokens for pointer components. */
10849
10850 static void
10851 trans_caf_token_assign (gfc_se *lse, gfc_se *rse, gfc_expr *expr1,
10852 gfc_expr *expr2)
10853 {
10854 symbol_attribute lhs_attr, rhs_attr;
10855 tree tmp, lhs_tok, rhs_tok;
10856 /* Flag to indicated component refs on the rhs. */
10857 bool rhs_cr;
10858
10859 lhs_attr = gfc_caf_attr (expr1);
10860 if (expr2->expr_type != EXPR_NULL)
10861 {
10862 rhs_attr = gfc_caf_attr (expr2, false, &rhs_cr);
10863 if (lhs_attr.codimension && rhs_attr.codimension)
10864 {
10865 lhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (lse, expr1);
10866 lhs_tok = build_fold_indirect_ref (lhs_tok);
10867
10868 if (rhs_cr)
10869 rhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (rse, expr2);
10870 else
10871 {
10872 tree caf_decl;
10873 caf_decl = gfc_get_tree_for_caf_expr (expr2);
10874 gfc_get_caf_token_offset (rse, &rhs_tok, NULL, caf_decl,
10875 NULL_TREE, NULL);
10876 }
10877 tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
10878 lhs_tok,
10879 fold_convert (TREE_TYPE (lhs_tok), rhs_tok));
10880 gfc_prepend_expr_to_block (&lse->post, tmp);
10881 }
10882 }
10883 else if (lhs_attr.codimension)
10884 {
10885 lhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (lse, expr1);
10886 if (!lhs_tok)
10887 {
10888 lhs_tok = gfc_get_tree_for_caf_expr (expr1);
10889 lhs_tok = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (lhs_tok));
10890 }
10891 else
10892 lhs_tok = build_fold_indirect_ref (lhs_tok);
10893 tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
10894 lhs_tok, null_pointer_node);
10895 gfc_prepend_expr_to_block (&lse->post, tmp);
10896 }
10897 }
10898
10899
10900 /* Do everything that is needed for a CLASS function expr2. */
10901
10902 static tree
10903 trans_class_pointer_fcn (stmtblock_t *block, gfc_se *lse, gfc_se *rse,
10904 gfc_expr *expr1, gfc_expr *expr2)
10905 {
10906 tree expr1_vptr = NULL_TREE;
10907 tree tmp;
10908
10909 gfc_conv_function_expr (rse, expr2);
10910 rse->expr = gfc_evaluate_now (rse->expr, &rse->pre);
10911
10912 if (expr1->ts.type != BT_CLASS)
10913 rse->expr = gfc_class_data_get (rse->expr);
10914 else
10915 {
10916 expr1_vptr = trans_class_vptr_len_assignment (block, expr1,
10917 expr2, rse,
10918 NULL, NULL, NULL);
10919 gfc_add_block_to_block (block, &rse->pre);
10920 tmp = gfc_create_var (TREE_TYPE (rse->expr), "ptrtemp");
10921 gfc_add_modify (&lse->pre, tmp, rse->expr);
10922
10923 gfc_add_modify (&lse->pre, expr1_vptr,
10924 fold_convert (TREE_TYPE (expr1_vptr),
10925 gfc_class_vptr_get (tmp)));
10926 rse->expr = gfc_class_data_get (tmp);
10927 }
10928
10929 return expr1_vptr;
10930 }
10931
10932
10933 tree
10934 gfc_trans_pointer_assign (gfc_code * code)
10935 {
10936 return gfc_trans_pointer_assignment (code->expr1, code->expr2);
10937 }
10938
10939
10940 /* Generate code for a pointer assignment. */
10941
10942 tree
10943 gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
10944 {
10945 gfc_se lse;
10946 gfc_se rse;
10947 stmtblock_t block;
10948 tree desc;
10949 tree tmp;
10950 tree expr1_vptr = NULL_TREE;
10951 bool scalar, non_proc_ptr_assign;
10952 gfc_ss *ss;
10953
10954 gfc_start_block (&block);
10955
10956 gfc_init_se (&lse, NULL);
10957
10958 /* Usually testing whether this is not a proc pointer assignment. */
10959 non_proc_ptr_assign
10960 = !(gfc_expr_attr (expr1).proc_pointer
10961 && ((expr2->expr_type == EXPR_VARIABLE
10962 && expr2->symtree->n.sym->attr.flavor == FL_PROCEDURE)
10963 || expr2->expr_type == EXPR_NULL));
10964
10965 /* Check whether the expression is a scalar or not; we cannot use
10966 expr1->rank as it can be nonzero for proc pointers. */
10967 ss = gfc_walk_expr (expr1);
10968 scalar = ss == gfc_ss_terminator;
10969 if (!scalar)
10970 gfc_free_ss_chain (ss);
10971
10972 if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS
10973 && expr2->expr_type != EXPR_FUNCTION && non_proc_ptr_assign)
10974 {
10975 gfc_add_data_component (expr2);
10976 /* The following is required as gfc_add_data_component doesn't
10977 update ts.type if there is a trailing REF_ARRAY. */
10978 expr2->ts.type = BT_DERIVED;
10979 }
10980
10981 if (scalar)
10982 {
10983 /* Scalar pointers. */
10984 lse.want_pointer = 1;
10985 gfc_conv_expr (&lse, expr1);
10986 gfc_init_se (&rse, NULL);
10987 rse.want_pointer = 1;
10988 if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
10989 trans_class_pointer_fcn (&block, &lse, &rse, expr1, expr2);
10990 else
10991 gfc_conv_expr (&rse, expr2);
10992
10993 if (non_proc_ptr_assign && expr1->ts.type == BT_CLASS)
10994 {
10995 trans_class_vptr_len_assignment (&block, expr1, expr2, &rse, NULL,
10996 NULL, NULL);
10997 lse.expr = gfc_class_data_get (lse.expr);
10998 }
10999
11000 if (expr1->symtree->n.sym->attr.proc_pointer
11001 && expr1->symtree->n.sym->attr.dummy)
11002 lse.expr = build_fold_indirect_ref_loc (input_location,
11003 lse.expr);
11004
11005 if (expr2->symtree && expr2->symtree->n.sym->attr.proc_pointer
11006 && expr2->symtree->n.sym->attr.dummy)
11007 rse.expr = build_fold_indirect_ref_loc (input_location,
11008 rse.expr);
11009
11010 gfc_add_block_to_block (&block, &lse.pre);
11011 gfc_add_block_to_block (&block, &rse.pre);
11012
11013 /* Check character lengths if character expression. The test is only
11014 really added if -fbounds-check is enabled. Exclude deferred
11015 character length lefthand sides. */
11016 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL
11017 && !expr1->ts.deferred
11018 && !expr1->symtree->n.sym->attr.proc_pointer
11019 && !gfc_is_proc_ptr_comp (expr1))
11020 {
11021 gcc_assert (expr2->ts.type == BT_CHARACTER);
11022 gcc_assert (lse.string_length && rse.string_length);
11023 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
11024 lse.string_length, rse.string_length,
11025 &block);
11026 }
11027
11028 /* The assignment to an deferred character length sets the string
11029 length to that of the rhs. */
11030 if (expr1->ts.deferred)
11031 {
11032 if (expr2->expr_type != EXPR_NULL && lse.string_length != NULL)
11033 gfc_add_modify (&block, lse.string_length,
11034 fold_convert (TREE_TYPE (lse.string_length),
11035 rse.string_length));
11036 else if (lse.string_length != NULL)
11037 gfc_add_modify (&block, lse.string_length,
11038 build_zero_cst (TREE_TYPE (lse.string_length)));
11039 }
11040
11041 gfc_add_modify (&block, lse.expr,
11042 fold_convert (TREE_TYPE (lse.expr), rse.expr));
11043
11044 if (flag_coarray == GFC_FCOARRAY_LIB)
11045 {
11046 if (expr1->ref)
11047 /* Also set the tokens for pointer components in derived typed
11048 coarrays. */
11049 trans_caf_token_assign (&lse, &rse, expr1, expr2);
11050 else if (gfc_caf_attr (expr1).codimension)
11051 {
11052 tree lhs_caf_decl, rhs_caf_decl, lhs_tok, rhs_tok;
11053
11054 lhs_caf_decl = gfc_get_tree_for_caf_expr (expr1);
11055 rhs_caf_decl = gfc_get_tree_for_caf_expr (expr2);
11056 gfc_get_caf_token_offset (&lse, &lhs_tok, nullptr, lhs_caf_decl,
11057 NULL_TREE, expr1);
11058 gfc_get_caf_token_offset (&rse, &rhs_tok, nullptr, rhs_caf_decl,
11059 NULL_TREE, expr2);
11060 gfc_add_modify (&block, lhs_tok, rhs_tok);
11061 }
11062 }
11063
11064 gfc_add_block_to_block (&block, &rse.post);
11065 gfc_add_block_to_block (&block, &lse.post);
11066 }
11067 else
11068 {
11069 gfc_ref* remap;
11070 bool rank_remap;
11071 tree strlen_lhs;
11072 tree strlen_rhs = NULL_TREE;
11073
11074 /* Array pointer. Find the last reference on the LHS and if it is an
11075 array section ref, we're dealing with bounds remapping. In this case,
11076 set it to AR_FULL so that gfc_conv_expr_descriptor does
11077 not see it and process the bounds remapping afterwards explicitly. */
11078 for (remap = expr1->ref; remap; remap = remap->next)
11079 if (!remap->next && remap->type == REF_ARRAY
11080 && remap->u.ar.type == AR_SECTION)
11081 break;
11082 rank_remap = (remap && remap->u.ar.end[0]);
11083
11084 if (remap && expr2->expr_type == EXPR_NULL)
11085 {
11086 gfc_error ("If bounds remapping is specified at %L, "
11087 "the pointer target shall not be NULL", &expr1->where);
11088 return NULL_TREE;
11089 }
11090
11091 gfc_init_se (&lse, NULL);
11092 if (remap)
11093 lse.descriptor_only = 1;
11094 gfc_conv_expr_descriptor (&lse, expr1);
11095 strlen_lhs = lse.string_length;
11096 desc = lse.expr;
11097
11098 if (expr2->expr_type == EXPR_NULL)
11099 {
11100 /* Just set the data pointer to null. */
11101 gfc_conv_descriptor_data_set (&lse.pre, lse.expr, null_pointer_node);
11102 }
11103 else if (rank_remap)
11104 {
11105 /* If we are rank-remapping, just get the RHS's descriptor and
11106 process this later on. */
11107 gfc_init_se (&rse, NULL);
11108 rse.direct_byref = 1;
11109 rse.byref_noassign = 1;
11110
11111 if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
11112 expr1_vptr = trans_class_pointer_fcn (&block, &lse, &rse,
11113 expr1, expr2);
11114 else if (expr2->expr_type == EXPR_FUNCTION)
11115 {
11116 tree bound[GFC_MAX_DIMENSIONS];
11117 int i;
11118
11119 for (i = 0; i < expr2->rank; i++)
11120 bound[i] = NULL_TREE;
11121 tmp = gfc_typenode_for_spec (&expr2->ts);
11122 tmp = gfc_get_array_type_bounds (tmp, expr2->rank, 0,
11123 bound, bound, 0,
11124 GFC_ARRAY_POINTER_CONT, false);
11125 tmp = gfc_create_var (tmp, "ptrtemp");
11126 rse.descriptor_only = 0;
11127 rse.expr = tmp;
11128 rse.direct_byref = 1;
11129 gfc_conv_expr_descriptor (&rse, expr2);
11130 strlen_rhs = rse.string_length;
11131 rse.expr = tmp;
11132 }
11133 else
11134 {
11135 gfc_conv_expr_descriptor (&rse, expr2);
11136 strlen_rhs = rse.string_length;
11137 if (expr1->ts.type == BT_CLASS)
11138 expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
11139 expr2, &rse,
11140 NULL, NULL,
11141 NULL);
11142 }
11143 }
11144 else if (expr2->expr_type == EXPR_VARIABLE)
11145 {
11146 /* Assign directly to the LHS's descriptor. */
11147 lse.descriptor_only = 0;
11148 lse.direct_byref = 1;
11149 gfc_conv_expr_descriptor (&lse, expr2);
11150 strlen_rhs = lse.string_length;
11151 gfc_init_se (&rse, NULL);
11152
11153 if (expr1->ts.type == BT_CLASS)
11154 {
11155 rse.expr = NULL_TREE;
11156 rse.string_length = strlen_rhs;
11157 trans_class_vptr_len_assignment (&block, expr1, expr2, &rse,
11158 NULL, NULL, NULL);
11159 }
11160
11161 if (remap == NULL)
11162 {
11163 /* If the target is not a whole array, use the target array
11164 reference for remap. */
11165 for (remap = expr2->ref; remap; remap = remap->next)
11166 if (remap->type == REF_ARRAY
11167 && remap->u.ar.type == AR_FULL
11168 && remap->next)
11169 break;
11170 }
11171 }
11172 else if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
11173 {
11174 gfc_init_se (&rse, NULL);
11175 rse.want_pointer = 1;
11176 gfc_conv_function_expr (&rse, expr2);
11177 if (expr1->ts.type != BT_CLASS)
11178 {
11179 rse.expr = gfc_class_data_get (rse.expr);
11180 gfc_add_modify (&lse.pre, desc, rse.expr);
11181 /* Set the lhs span. */
11182 tmp = TREE_TYPE (rse.expr);
11183 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (tmp));
11184 tmp = fold_convert (gfc_array_index_type, tmp);
11185 gfc_conv_descriptor_span_set (&lse.pre, desc, tmp);
11186 }
11187 else
11188 {
11189 expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
11190 expr2, &rse, NULL,
11191 NULL, NULL);
11192 gfc_add_block_to_block (&block, &rse.pre);
11193 tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
11194 gfc_add_modify (&lse.pre, tmp, rse.expr);
11195
11196 gfc_add_modify (&lse.pre, expr1_vptr,
11197 fold_convert (TREE_TYPE (expr1_vptr),
11198 gfc_class_vptr_get (tmp)));
11199 rse.expr = gfc_class_data_get (tmp);
11200 gfc_add_modify (&lse.pre, desc, rse.expr);
11201 }
11202 }
11203 else
11204 {
11205 /* Assign to a temporary descriptor and then copy that
11206 temporary to the pointer. */
11207 tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
11208 lse.descriptor_only = 0;
11209 lse.expr = tmp;
11210 lse.direct_byref = 1;
11211 gfc_conv_expr_descriptor (&lse, expr2);
11212 strlen_rhs = lse.string_length;
11213 gfc_add_modify (&lse.pre, desc, tmp);
11214 }
11215
11216 if (expr1->ts.type == BT_CHARACTER
11217 && expr1->ts.deferred)
11218 {
11219 gfc_symbol *psym = expr1->symtree->n.sym;
11220 tmp = NULL_TREE;
11221 if (psym->ts.type == BT_CHARACTER
11222 && psym->ts.u.cl->backend_decl)
11223 tmp = psym->ts.u.cl->backend_decl;
11224 else if (expr1->ts.u.cl->backend_decl
11225 && VAR_P (expr1->ts.u.cl->backend_decl))
11226 tmp = expr1->ts.u.cl->backend_decl;
11227 else if (TREE_CODE (lse.expr) == COMPONENT_REF)
11228 {
11229 gfc_ref *ref = expr1->ref;
11230 for (;ref; ref = ref->next)
11231 {
11232 if (ref->type == REF_COMPONENT
11233 && ref->u.c.component->ts.type == BT_CHARACTER
11234 && gfc_deferred_strlen (ref->u.c.component, &tmp))
11235 tmp = fold_build3_loc (input_location, COMPONENT_REF,
11236 TREE_TYPE (tmp),
11237 TREE_OPERAND (lse.expr, 0),
11238 tmp, NULL_TREE);
11239 }
11240 }
11241
11242 gcc_assert (tmp);
11243
11244 if (expr2->expr_type != EXPR_NULL)
11245 gfc_add_modify (&block, tmp,
11246 fold_convert (TREE_TYPE (tmp), strlen_rhs));
11247 else
11248 gfc_add_modify (&block, tmp, build_zero_cst (TREE_TYPE (tmp)));
11249 }
11250
11251 gfc_add_block_to_block (&block, &lse.pre);
11252 if (rank_remap)
11253 gfc_add_block_to_block (&block, &rse.pre);
11254
11255 /* If we do bounds remapping, update LHS descriptor accordingly. */
11256 if (remap)
11257 {
11258 int dim;
11259 gcc_assert (remap->u.ar.dimen == expr1->rank);
11260
11261 /* Always set dtype. */
11262 tree dtype = gfc_conv_descriptor_dtype (desc);
11263 tmp = gfc_get_dtype (TREE_TYPE (desc));
11264 gfc_add_modify (&block, dtype, tmp);
11265
11266 /* For unlimited polymorphic LHS use elem_len from RHS. */
11267 if (UNLIMITED_POLY (expr1) && expr2->ts.type != BT_CLASS)
11268 {
11269 tree elem_len;
11270 tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
11271 elem_len = fold_convert (gfc_array_index_type, tmp);
11272 elem_len = gfc_evaluate_now (elem_len, &block);
11273 tmp = gfc_conv_descriptor_elem_len (desc);
11274 gfc_add_modify (&block, tmp,
11275 fold_convert (TREE_TYPE (tmp), elem_len));
11276 }
11277
11278 if (rank_remap)
11279 {
11280 /* Do rank remapping. We already have the RHS's descriptor
11281 converted in rse and now have to build the correct LHS
11282 descriptor for it. */
11283
11284 tree data, span;
11285 tree offs, stride;
11286 tree lbound, ubound;
11287
11288 /* Copy data pointer. */
11289 data = gfc_conv_descriptor_data_get (rse.expr);
11290 gfc_conv_descriptor_data_set (&block, desc, data);
11291
11292 /* Copy the span. */
11293 if (VAR_P (rse.expr)
11294 && GFC_DECL_PTR_ARRAY_P (rse.expr))
11295 span = gfc_conv_descriptor_span_get (rse.expr);
11296 else
11297 {
11298 tmp = TREE_TYPE (rse.expr);
11299 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (tmp));
11300 span = fold_convert (gfc_array_index_type, tmp);
11301 }
11302 gfc_conv_descriptor_span_set (&block, desc, span);
11303
11304 /* Copy offset but adjust it such that it would correspond
11305 to a lbound of zero. */
11306 if (expr2->rank == -1)
11307 gfc_conv_descriptor_offset_set (&block, desc,
11308 gfc_index_zero_node);
11309 else
11310 {
11311 offs = gfc_conv_descriptor_offset_get (rse.expr);
11312 for (dim = 0; dim < expr2->rank; ++dim)
11313 {
11314 stride = gfc_conv_descriptor_stride_get (rse.expr,
11315 gfc_rank_cst[dim]);
11316 lbound = gfc_conv_descriptor_lbound_get (rse.expr,
11317 gfc_rank_cst[dim]);
11318 tmp = fold_build2_loc (input_location, MULT_EXPR,
11319 gfc_array_index_type, stride,
11320 lbound);
11321 offs = fold_build2_loc (input_location, PLUS_EXPR,
11322 gfc_array_index_type, offs, tmp);
11323 }
11324 gfc_conv_descriptor_offset_set (&block, desc, offs);
11325 }
11326 /* Set the bounds as declared for the LHS and calculate strides as
11327 well as another offset update accordingly. */
11328 stride = gfc_conv_descriptor_stride_get (rse.expr,
11329 gfc_rank_cst[0]);
11330 for (dim = 0; dim < expr1->rank; ++dim)
11331 {
11332 gfc_se lower_se;
11333 gfc_se upper_se;
11334
11335 gcc_assert (remap->u.ar.start[dim] && remap->u.ar.end[dim]);
11336
11337 if (remap->u.ar.start[dim]->expr_type != EXPR_CONSTANT
11338 || remap->u.ar.start[dim]->expr_type != EXPR_VARIABLE)
11339 gfc_resolve_expr (remap->u.ar.start[dim]);
11340 if (remap->u.ar.end[dim]->expr_type != EXPR_CONSTANT
11341 || remap->u.ar.end[dim]->expr_type != EXPR_VARIABLE)
11342 gfc_resolve_expr (remap->u.ar.end[dim]);
11343
11344 /* Convert declared bounds. */
11345 gfc_init_se (&lower_se, NULL);
11346 gfc_init_se (&upper_se, NULL);
11347 gfc_conv_expr (&lower_se, remap->u.ar.start[dim]);
11348 gfc_conv_expr (&upper_se, remap->u.ar.end[dim]);
11349
11350 gfc_add_block_to_block (&block, &lower_se.pre);
11351 gfc_add_block_to_block (&block, &upper_se.pre);
11352
11353 lbound = fold_convert (gfc_array_index_type, lower_se.expr);
11354 ubound = fold_convert (gfc_array_index_type, upper_se.expr);
11355
11356 lbound = gfc_evaluate_now (lbound, &block);
11357 ubound = gfc_evaluate_now (ubound, &block);
11358
11359 gfc_add_block_to_block (&block, &lower_se.post);
11360 gfc_add_block_to_block (&block, &upper_se.post);
11361
11362 /* Set bounds in descriptor. */
11363 gfc_conv_descriptor_lbound_set (&block, desc,
11364 gfc_rank_cst[dim], lbound);
11365 gfc_conv_descriptor_ubound_set (&block, desc,
11366 gfc_rank_cst[dim], ubound);
11367
11368 /* Set stride. */
11369 stride = gfc_evaluate_now (stride, &block);
11370 gfc_conv_descriptor_stride_set (&block, desc,
11371 gfc_rank_cst[dim], stride);
11372
11373 /* Update offset. */
11374 offs = gfc_conv_descriptor_offset_get (desc);
11375 tmp = fold_build2_loc (input_location, MULT_EXPR,
11376 gfc_array_index_type, lbound, stride);
11377 offs = fold_build2_loc (input_location, MINUS_EXPR,
11378 gfc_array_index_type, offs, tmp);
11379 offs = gfc_evaluate_now (offs, &block);
11380 gfc_conv_descriptor_offset_set (&block, desc, offs);
11381
11382 /* Update stride. */
11383 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
11384 stride = fold_build2_loc (input_location, MULT_EXPR,
11385 gfc_array_index_type, stride, tmp);
11386 }
11387 }
11388 else
11389 {
11390 /* Bounds remapping. Just shift the lower bounds. */
11391
11392 gcc_assert (expr1->rank == expr2->rank);
11393
11394 for (dim = 0; dim < remap->u.ar.dimen; ++dim)
11395 {
11396 gfc_se lbound_se;
11397
11398 gcc_assert (!remap->u.ar.end[dim]);
11399 gfc_init_se (&lbound_se, NULL);
11400 if (remap->u.ar.start[dim])
11401 {
11402 gfc_conv_expr (&lbound_se, remap->u.ar.start[dim]);
11403 gfc_add_block_to_block (&block, &lbound_se.pre);
11404 }
11405 else
11406 /* This remap arises from a target that is not a whole
11407 array. The start expressions will be NULL but we need
11408 the lbounds to be one. */
11409 lbound_se.expr = gfc_index_one_node;
11410 gfc_conv_shift_descriptor_lbound (&block, desc,
11411 dim, lbound_se.expr);
11412 gfc_add_block_to_block (&block, &lbound_se.post);
11413 }
11414 }
11415 }
11416
11417 /* If rank remapping was done, check with -fcheck=bounds that
11418 the target is at least as large as the pointer. */
11419 if (rank_remap && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
11420 && expr2->rank != -1)
11421 {
11422 tree lsize, rsize;
11423 tree fault;
11424 const char* msg;
11425
11426 lsize = gfc_conv_descriptor_size (lse.expr, expr1->rank);
11427 rsize = gfc_conv_descriptor_size (rse.expr, expr2->rank);
11428
11429 lsize = gfc_evaluate_now (lsize, &block);
11430 rsize = gfc_evaluate_now (rsize, &block);
11431 fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
11432 rsize, lsize);
11433
11434 msg = _("Target of rank remapping is too small (%ld < %ld)");
11435 gfc_trans_runtime_check (true, false, fault, &block, &expr2->where,
11436 msg, rsize, lsize);
11437 }
11438
11439 /* Check string lengths if applicable. The check is only really added
11440 to the output code if -fbounds-check is enabled. */
11441 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
11442 {
11443 gcc_assert (expr2->ts.type == BT_CHARACTER);
11444 gcc_assert (strlen_lhs && strlen_rhs);
11445 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
11446 strlen_lhs, strlen_rhs, &block);
11447 }
11448
11449 gfc_add_block_to_block (&block, &lse.post);
11450 if (rank_remap)
11451 gfc_add_block_to_block (&block, &rse.post);
11452 }
11453
11454 return gfc_finish_block (&block);
11455 }
11456
11457
11458 /* Makes sure se is suitable for passing as a function string parameter. */
11459 /* TODO: Need to check all callers of this function. It may be abused. */
11460
11461 void
11462 gfc_conv_string_parameter (gfc_se * se)
11463 {
11464 tree type;
11465
11466 if (TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE
11467 && integer_onep (se->string_length))
11468 {
11469 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
11470 return;
11471 }
11472
11473 if (TREE_CODE (se->expr) == STRING_CST)
11474 {
11475 type = TREE_TYPE (TREE_TYPE (se->expr));
11476 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
11477 return;
11478 }
11479
11480 if ((TREE_CODE (TREE_TYPE (se->expr)) == ARRAY_TYPE
11481 || TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE)
11482 && TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
11483 {
11484 type = TREE_TYPE (se->expr);
11485 if (TREE_CODE (se->expr) != INDIRECT_REF)
11486 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
11487 else
11488 {
11489 if (TREE_CODE (type) == ARRAY_TYPE)
11490 type = TREE_TYPE (type);
11491 type = gfc_get_character_type_len_for_eltype (type,
11492 se->string_length);
11493 type = build_pointer_type (type);
11494 se->expr = gfc_build_addr_expr (type, se->expr);
11495 }
11496 }
11497
11498 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
11499 }
11500
11501
11502 /* Generate code for assignment of scalar variables. Includes character
11503 strings and derived types with allocatable components.
11504 If you know that the LHS has no allocations, set dealloc to false.
11505
11506 DEEP_COPY has no effect if the typespec TS is not a derived type with
11507 allocatable components. Otherwise, if it is set, an explicit copy of each
11508 allocatable component is made. This is necessary as a simple copy of the
11509 whole object would copy array descriptors as is, so that the lhs's
11510 allocatable components would point to the rhs's after the assignment.
11511 Typically, setting DEEP_COPY is necessary if the rhs is a variable, and not
11512 necessary if the rhs is a non-pointer function, as the allocatable components
11513 are not accessible by other means than the function's result after the
11514 function has returned. It is even more subtle when temporaries are involved,
11515 as the two following examples show:
11516 1. When we evaluate an array constructor, a temporary is created. Thus
11517 there is theoretically no alias possible. However, no deep copy is
11518 made for this temporary, so that if the constructor is made of one or
11519 more variable with allocatable components, those components still point
11520 to the variable's: DEEP_COPY should be set for the assignment from the
11521 temporary to the lhs in that case.
11522 2. When assigning a scalar to an array, we evaluate the scalar value out
11523 of the loop, store it into a temporary variable, and assign from that.
11524 In that case, deep copying when assigning to the temporary would be a
11525 waste of resources; however deep copies should happen when assigning from
11526 the temporary to each array element: again DEEP_COPY should be set for
11527 the assignment from the temporary to the lhs. */
11528
11529 tree
11530 gfc_trans_scalar_assign (gfc_se *lse, gfc_se *rse, gfc_typespec ts,
11531 bool deep_copy, bool dealloc, bool in_coarray,
11532 bool assoc_assign)
11533 {
11534 stmtblock_t block;
11535 tree tmp;
11536 tree cond;
11537
11538 gfc_init_block (&block);
11539
11540 if (ts.type == BT_CHARACTER)
11541 {
11542 tree rlen = NULL;
11543 tree llen = NULL;
11544
11545 if (lse->string_length != NULL_TREE)
11546 {
11547 gfc_conv_string_parameter (lse);
11548 gfc_add_block_to_block (&block, &lse->pre);
11549 llen = lse->string_length;
11550 }
11551
11552 if (rse->string_length != NULL_TREE)
11553 {
11554 gfc_conv_string_parameter (rse);
11555 gfc_add_block_to_block (&block, &rse->pre);
11556 rlen = rse->string_length;
11557 }
11558
11559 gfc_trans_string_copy (&block, llen, lse->expr, ts.kind, rlen,
11560 rse->expr, ts.kind);
11561 }
11562 else if (gfc_bt_struct (ts.type)
11563 && (ts.u.derived->attr.alloc_comp
11564 || (deep_copy && ts.u.derived->attr.pdt_type)))
11565 {
11566 tree tmp_var = NULL_TREE;
11567 cond = NULL_TREE;
11568
11569 /* Are the rhs and the lhs the same? */
11570 if (deep_copy)
11571 {
11572 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
11573 gfc_build_addr_expr (NULL_TREE, lse->expr),
11574 gfc_build_addr_expr (NULL_TREE, rse->expr));
11575 cond = gfc_evaluate_now (cond, &lse->pre);
11576 }
11577
11578 /* Deallocate the lhs allocated components as long as it is not
11579 the same as the rhs. This must be done following the assignment
11580 to prevent deallocating data that could be used in the rhs
11581 expression. */
11582 if (dealloc)
11583 {
11584 tmp_var = gfc_evaluate_now (lse->expr, &lse->pre);
11585 tmp = gfc_deallocate_alloc_comp_no_caf (ts.u.derived, tmp_var,
11586 0, gfc_may_be_finalized (ts));
11587 if (deep_copy)
11588 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
11589 tmp);
11590 gfc_add_expr_to_block (&lse->post, tmp);
11591 }
11592
11593 gfc_add_block_to_block (&block, &rse->pre);
11594 gfc_add_block_to_block (&block, &lse->finalblock);
11595 gfc_add_block_to_block (&block, &lse->pre);
11596
11597 gfc_add_modify (&block, lse->expr,
11598 fold_convert (TREE_TYPE (lse->expr), rse->expr));
11599
11600 /* Restore pointer address of coarray components. */
11601 if (ts.u.derived->attr.coarray_comp && deep_copy && tmp_var != NULL_TREE)
11602 {
11603 tmp = gfc_reassign_alloc_comp_caf (ts.u.derived, tmp_var, lse->expr);
11604 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
11605 tmp);
11606 gfc_add_expr_to_block (&block, tmp);
11607 }
11608
11609 /* Do a deep copy if the rhs is a variable, if it is not the
11610 same as the lhs. */
11611 if (deep_copy)
11612 {
11613 int caf_mode = in_coarray ? (GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
11614 | GFC_STRUCTURE_CAF_MODE_IN_COARRAY) : 0;
11615 tmp = gfc_copy_alloc_comp (ts.u.derived, rse->expr, lse->expr, 0,
11616 caf_mode);
11617 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
11618 tmp);
11619 gfc_add_expr_to_block (&block, tmp);
11620 }
11621 }
11622 else if (gfc_bt_struct (ts.type))
11623 {
11624 gfc_add_block_to_block (&block, &rse->pre);
11625 gfc_add_block_to_block (&block, &lse->finalblock);
11626 gfc_add_block_to_block (&block, &lse->pre);
11627 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
11628 TREE_TYPE (lse->expr), rse->expr);
11629 gfc_add_modify (&block, lse->expr, tmp);
11630 }
11631 /* If possible use the rhs vptr copy with trans_scalar_class_assign.... */
11632 else if (ts.type == BT_CLASS)
11633 {
11634 gfc_add_block_to_block (&block, &lse->pre);
11635 gfc_add_block_to_block (&block, &rse->pre);
11636 gfc_add_block_to_block (&block, &lse->finalblock);
11637
11638 if (!trans_scalar_class_assign (&block, lse, rse))
11639 {
11640 /* ...otherwise assignment suffices. Note the use of VIEW_CONVERT_EXPR
11641 for the lhs which ensures that class data rhs cast as a string assigns
11642 correctly. */
11643 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
11644 TREE_TYPE (rse->expr), lse->expr);
11645 gfc_add_modify (&block, tmp, rse->expr);
11646 }
11647 }
11648 else if (ts.type != BT_CLASS)
11649 {
11650 gfc_add_block_to_block (&block, &lse->pre);
11651 gfc_add_block_to_block (&block, &rse->pre);
11652
11653 if (in_coarray)
11654 {
11655 if (flag_coarray == GFC_FCOARRAY_LIB && assoc_assign)
11656 {
11657 gfc_add_modify (&block, gfc_conv_descriptor_token (lse->expr),
11658 TYPE_LANG_SPECIFIC (
11659 TREE_TYPE (TREE_TYPE (rse->expr)))
11660 ->caf_token);
11661 }
11662 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (lse->expr)))
11663 lse->expr = gfc_conv_array_data (lse->expr);
11664 if (flag_coarray == GFC_FCOARRAY_SINGLE && assoc_assign
11665 && !POINTER_TYPE_P (TREE_TYPE (rse->expr)))
11666 rse->expr = gfc_build_addr_expr (NULL_TREE, rse->expr);
11667 }
11668 gfc_add_modify (&block, lse->expr,
11669 fold_convert (TREE_TYPE (lse->expr), rse->expr));
11670 }
11671
11672 gfc_add_block_to_block (&block, &lse->post);
11673 gfc_add_block_to_block (&block, &rse->post);
11674
11675 return gfc_finish_block (&block);
11676 }
11677
11678
11679 /* There are quite a lot of restrictions on the optimisation in using an
11680 array function assign without a temporary. */
11681
11682 static bool
11683 arrayfunc_assign_needs_temporary (gfc_expr * expr1, gfc_expr * expr2)
11684 {
11685 gfc_ref * ref;
11686 bool seen_array_ref;
11687 bool c = false;
11688 gfc_symbol *sym = expr1->symtree->n.sym;
11689
11690 /* Play it safe with class functions assigned to a derived type. */
11691 if (gfc_is_class_array_function (expr2)
11692 && expr1->ts.type == BT_DERIVED)
11693 return true;
11694
11695 /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
11696 if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
11697 return true;
11698
11699 /* Elemental functions are scalarized so that they don't need a
11700 temporary in gfc_trans_assignment_1, so return a true. Otherwise,
11701 they would need special treatment in gfc_trans_arrayfunc_assign. */
11702 if (expr2->value.function.esym != NULL
11703 && expr2->value.function.esym->attr.elemental)
11704 return true;
11705
11706 /* Need a temporary if rhs is not FULL or a contiguous section. */
11707 if (expr1->ref && !(gfc_full_array_ref_p (expr1->ref, &c) || c))
11708 return true;
11709
11710 /* Need a temporary if EXPR1 can't be expressed as a descriptor. */
11711 if (gfc_ref_needs_temporary_p (expr1->ref))
11712 return true;
11713
11714 /* Functions returning pointers or allocatables need temporaries. */
11715 if (gfc_expr_attr (expr2).pointer
11716 || gfc_expr_attr (expr2).allocatable)
11717 return true;
11718
11719 /* Character array functions need temporaries unless the
11720 character lengths are the same. */
11721 if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
11722 {
11723 if (UNLIMITED_POLY (expr1))
11724 return true;
11725
11726 if (expr1->ts.u.cl->length == NULL
11727 || expr1->ts.u.cl->length->expr_type != EXPR_CONSTANT)
11728 return true;
11729
11730 if (expr2->ts.u.cl->length == NULL
11731 || expr2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
11732 return true;
11733
11734 if (mpz_cmp (expr1->ts.u.cl->length->value.integer,
11735 expr2->ts.u.cl->length->value.integer) != 0)
11736 return true;
11737 }
11738
11739 /* Check that no LHS component references appear during an array
11740 reference. This is needed because we do not have the means to
11741 span any arbitrary stride with an array descriptor. This check
11742 is not needed for the rhs because the function result has to be
11743 a complete type. */
11744 seen_array_ref = false;
11745 for (ref = expr1->ref; ref; ref = ref->next)
11746 {
11747 if (ref->type == REF_ARRAY)
11748 seen_array_ref= true;
11749 else if (ref->type == REF_COMPONENT && seen_array_ref)
11750 return true;
11751 }
11752
11753 /* Check for a dependency. */
11754 if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
11755 expr2->value.function.esym,
11756 expr2->value.function.actual,
11757 NOT_ELEMENTAL))
11758 return true;
11759
11760 /* If we have reached here with an intrinsic function, we do not
11761 need a temporary except in the particular case that reallocation
11762 on assignment is active and the lhs is allocatable and a target,
11763 or a pointer which may be a subref pointer. FIXME: The last
11764 condition can go away when we use span in the intrinsics
11765 directly.*/
11766 if (expr2->value.function.isym)
11767 return (flag_realloc_lhs && sym->attr.allocatable && sym->attr.target)
11768 || (sym->attr.pointer && sym->attr.subref_array_pointer);
11769
11770 /* If the LHS is a dummy, we need a temporary if it is not
11771 INTENT(OUT). */
11772 if (sym->attr.dummy && sym->attr.intent != INTENT_OUT)
11773 return true;
11774
11775 /* If the lhs has been host_associated, is in common, a pointer or is
11776 a target and the function is not using a RESULT variable, aliasing
11777 can occur and a temporary is needed. */
11778 if ((sym->attr.host_assoc
11779 || sym->attr.in_common
11780 || sym->attr.pointer
11781 || sym->attr.cray_pointee
11782 || sym->attr.target)
11783 && expr2->symtree != NULL
11784 && expr2->symtree->n.sym == expr2->symtree->n.sym->result)
11785 return true;
11786
11787 /* A PURE function can unconditionally be called without a temporary. */
11788 if (expr2->value.function.esym != NULL
11789 && expr2->value.function.esym->attr.pure)
11790 return false;
11791
11792 /* Implicit_pure functions are those which could legally be declared
11793 to be PURE. */
11794 if (expr2->value.function.esym != NULL
11795 && expr2->value.function.esym->attr.implicit_pure)
11796 return false;
11797
11798 if (!sym->attr.use_assoc
11799 && !sym->attr.in_common
11800 && !sym->attr.pointer
11801 && !sym->attr.target
11802 && !sym->attr.cray_pointee
11803 && expr2->value.function.esym)
11804 {
11805 /* A temporary is not needed if the function is not contained and
11806 the variable is local or host associated and not a pointer or
11807 a target. */
11808 if (!expr2->value.function.esym->attr.contained)
11809 return false;
11810
11811 /* A temporary is not needed if the lhs has never been host
11812 associated and the procedure is contained. */
11813 else if (!sym->attr.host_assoc)
11814 return false;
11815
11816 /* A temporary is not needed if the variable is local and not
11817 a pointer, a target or a result. */
11818 if (sym->ns->parent
11819 && expr2->value.function.esym->ns == sym->ns->parent)
11820 return false;
11821 }
11822
11823 /* Default to temporary use. */
11824 return true;
11825 }
11826
11827
11828 /* Provide the loop info so that the lhs descriptor can be built for
11829 reallocatable assignments from extrinsic function calls. */
11830
11831 static void
11832 realloc_lhs_loop_for_fcn_call (gfc_se *se, locus *where, gfc_ss **ss,
11833 gfc_loopinfo *loop)
11834 {
11835 /* Signal that the function call should not be made by
11836 gfc_conv_loop_setup. */
11837 se->ss->is_alloc_lhs = 1;
11838 gfc_init_loopinfo (loop);
11839 gfc_add_ss_to_loop (loop, *ss);
11840 gfc_add_ss_to_loop (loop, se->ss);
11841 gfc_conv_ss_startstride (loop);
11842 gfc_conv_loop_setup (loop, where);
11843 gfc_copy_loopinfo_to_se (se, loop);
11844 gfc_add_block_to_block (&se->pre, &loop->pre);
11845 gfc_add_block_to_block (&se->pre, &loop->post);
11846 se->ss->is_alloc_lhs = 0;
11847 }
11848
11849
11850 /* For assignment to a reallocatable lhs from intrinsic functions,
11851 replace the se.expr (ie. the result) with a temporary descriptor.
11852 Null the data field so that the library allocates space for the
11853 result. Free the data of the original descriptor after the function,
11854 in case it appears in an argument expression and transfer the
11855 result to the original descriptor. */
11856
11857 static void
11858 fcncall_realloc_result (gfc_se *se, int rank, tree dtype)
11859 {
11860 tree desc;
11861 tree res_desc;
11862 tree tmp;
11863 tree offset;
11864 tree zero_cond;
11865 tree not_same_shape;
11866 stmtblock_t shape_block;
11867 int n;
11868
11869 /* Use the allocation done by the library. Substitute the lhs
11870 descriptor with a copy, whose data field is nulled.*/
11871 desc = build_fold_indirect_ref_loc (input_location, se->expr);
11872 if (POINTER_TYPE_P (TREE_TYPE (desc)))
11873 desc = build_fold_indirect_ref_loc (input_location, desc);
11874
11875 /* Unallocated, the descriptor does not have a dtype. */
11876 tmp = gfc_conv_descriptor_dtype (desc);
11877 if (dtype != NULL_TREE)
11878 gfc_add_modify (&se->pre, tmp, dtype);
11879 else
11880 gfc_add_modify (&se->pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
11881
11882 res_desc = gfc_evaluate_now (desc, &se->pre);
11883 gfc_conv_descriptor_data_set (&se->pre, res_desc, null_pointer_node);
11884 se->expr = gfc_build_addr_expr (NULL_TREE, res_desc);
11885
11886 /* Free the lhs after the function call and copy the result data to
11887 the lhs descriptor. */
11888 tmp = gfc_conv_descriptor_data_get (desc);
11889 zero_cond = fold_build2_loc (input_location, EQ_EXPR,
11890 logical_type_node, tmp,
11891 build_int_cst (TREE_TYPE (tmp), 0));
11892 zero_cond = gfc_evaluate_now (zero_cond, &se->post);
11893 tmp = gfc_call_free (tmp);
11894 gfc_add_expr_to_block (&se->post, tmp);
11895
11896 tmp = gfc_conv_descriptor_data_get (res_desc);
11897 gfc_conv_descriptor_data_set (&se->post, desc, tmp);
11898
11899 /* Check that the shapes are the same between lhs and expression.
11900 The evaluation of the shape is done in 'shape_block' to avoid
11901 unitialized warnings from the lhs bounds. */
11902 not_same_shape = boolean_false_node;
11903 gfc_start_block (&shape_block);
11904 for (n = 0 ; n < rank; n++)
11905 {
11906 tree tmp1;
11907 tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
11908 tmp1 = gfc_conv_descriptor_lbound_get (res_desc, gfc_rank_cst[n]);
11909 tmp = fold_build2_loc (input_location, MINUS_EXPR,
11910 gfc_array_index_type, tmp, tmp1);
11911 tmp1 = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
11912 tmp = fold_build2_loc (input_location, MINUS_EXPR,
11913 gfc_array_index_type, tmp, tmp1);
11914 tmp1 = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
11915 tmp = fold_build2_loc (input_location, PLUS_EXPR,
11916 gfc_array_index_type, tmp, tmp1);
11917 tmp = fold_build2_loc (input_location, NE_EXPR,
11918 logical_type_node, tmp,
11919 gfc_index_zero_node);
11920 tmp = gfc_evaluate_now (tmp, &shape_block);
11921 if (n == 0)
11922 not_same_shape = tmp;
11923 else
11924 not_same_shape = fold_build2_loc (input_location, TRUTH_OR_EXPR,
11925 logical_type_node, tmp,
11926 not_same_shape);
11927 }
11928
11929 /* 'zero_cond' being true is equal to lhs not being allocated or the
11930 shapes being different. */
11931 tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR, logical_type_node,
11932 zero_cond, not_same_shape);
11933 gfc_add_modify (&shape_block, zero_cond, tmp);
11934 tmp = gfc_finish_block (&shape_block);
11935 tmp = build3_v (COND_EXPR, zero_cond,
11936 build_empty_stmt (input_location), tmp);
11937 gfc_add_expr_to_block (&se->post, tmp);
11938
11939 /* Now reset the bounds returned from the function call to bounds based
11940 on the lhs lbounds, except where the lhs is not allocated or the shapes
11941 of 'variable and 'expr' are different. Set the offset accordingly. */
11942 offset = gfc_index_zero_node;
11943 for (n = 0 ; n < rank; n++)
11944 {
11945 tree lbound;
11946
11947 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
11948 lbound = fold_build3_loc (input_location, COND_EXPR,
11949 gfc_array_index_type, zero_cond,
11950 gfc_index_one_node, lbound);
11951 lbound = gfc_evaluate_now (lbound, &se->post);
11952
11953 tmp = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
11954 tmp = fold_build2_loc (input_location, PLUS_EXPR,
11955 gfc_array_index_type, tmp, lbound);
11956 gfc_conv_descriptor_lbound_set (&se->post, desc,
11957 gfc_rank_cst[n], lbound);
11958 gfc_conv_descriptor_ubound_set (&se->post, desc,
11959 gfc_rank_cst[n], tmp);
11960
11961 /* Set stride and accumulate the offset. */
11962 tmp = gfc_conv_descriptor_stride_get (res_desc, gfc_rank_cst[n]);
11963 gfc_conv_descriptor_stride_set (&se->post, desc,
11964 gfc_rank_cst[n], tmp);
11965 tmp = fold_build2_loc (input_location, MULT_EXPR,
11966 gfc_array_index_type, lbound, tmp);
11967 offset = fold_build2_loc (input_location, MINUS_EXPR,
11968 gfc_array_index_type, offset, tmp);
11969 offset = gfc_evaluate_now (offset, &se->post);
11970 }
11971
11972 gfc_conv_descriptor_offset_set (&se->post, desc, offset);
11973 }
11974
11975
11976
11977 /* Try to translate array(:) = func (...), where func is a transformational
11978 array function, without using a temporary. Returns NULL if this isn't the
11979 case. */
11980
11981 static tree
11982 gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
11983 {
11984 gfc_se se;
11985 gfc_ss *ss = NULL;
11986 gfc_component *comp = NULL;
11987 gfc_loopinfo loop;
11988 tree tmp;
11989 tree lhs;
11990 gfc_se final_se;
11991 gfc_symbol *sym = expr1->symtree->n.sym;
11992 bool finalizable = gfc_may_be_finalized (expr1->ts);
11993
11994 /* If the symbol is host associated and has not been referenced in its name
11995 space, it might be lacking a backend_decl and vtable. */
11996 if (sym->backend_decl == NULL_TREE)
11997 return NULL_TREE;
11998
11999 if (arrayfunc_assign_needs_temporary (expr1, expr2))
12000 return NULL_TREE;
12001
12002 /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
12003 functions. */
12004 comp = gfc_get_proc_ptr_comp (expr2);
12005
12006 if (!(expr2->value.function.isym
12007 || (comp && comp->attr.dimension)
12008 || (!comp && gfc_return_by_reference (expr2->value.function.esym)
12009 && expr2->value.function.esym->result->attr.dimension)))
12010 return NULL_TREE;
12011
12012 gfc_init_se (&se, NULL);
12013 gfc_start_block (&se.pre);
12014 se.want_pointer = 1;
12015
12016 /* First the lhs must be finalized, if necessary. We use a copy of the symbol
12017 backend decl, stash the original away for the finalization so that the
12018 value used is that before the assignment. This is necessary because
12019 evaluation of the rhs expression using direct by reference can change
12020 the value. However, the standard mandates that the finalization must occur
12021 after evaluation of the rhs. */
12022 gfc_init_se (&final_se, NULL);
12023
12024 if (finalizable)
12025 {
12026 tmp = sym->backend_decl;
12027 lhs = sym->backend_decl;
12028 if (INDIRECT_REF_P (tmp))
12029 tmp = TREE_OPERAND (tmp, 0);
12030 sym->backend_decl = gfc_create_var (TREE_TYPE (tmp), "lhs");
12031 gfc_add_modify (&se.pre, sym->backend_decl, tmp);
12032 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
12033 {
12034 tmp = gfc_copy_alloc_comp (expr1->ts.u.derived, tmp, sym->backend_decl,
12035 expr1->rank, 0);
12036 gfc_add_expr_to_block (&final_se.pre, tmp);
12037 }
12038 }
12039
12040 if (finalizable && gfc_assignment_finalizer_call (&final_se, expr1, false))
12041 {
12042 gfc_add_block_to_block (&se.pre, &final_se.pre);
12043 gfc_add_block_to_block (&se.post, &final_se.finalblock);
12044 }
12045
12046 if (finalizable)
12047 sym->backend_decl = lhs;
12048
12049 gfc_conv_array_parameter (&se, expr1, false, NULL, NULL, NULL);
12050
12051 if (expr1->ts.type == BT_DERIVED
12052 && expr1->ts.u.derived->attr.alloc_comp)
12053 {
12054 tmp = build_fold_indirect_ref_loc (input_location, se.expr);
12055 tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, tmp,
12056 expr1->rank);
12057 gfc_add_expr_to_block (&se.pre, tmp);
12058 }
12059
12060 se.direct_byref = 1;
12061 se.ss = gfc_walk_expr (expr2);
12062 gcc_assert (se.ss != gfc_ss_terminator);
12063
12064 /* Since this is a direct by reference call, references to the lhs can be
12065 used for finalization of the function result just as long as the blocks
12066 from final_se are added at the right time. */
12067 gfc_init_se (&final_se, NULL);
12068 if (finalizable && expr2->value.function.esym)
12069 {
12070 final_se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
12071 gfc_finalize_tree_expr (&final_se, expr2->ts.u.derived,
12072 expr2->value.function.esym->attr,
12073 expr2->rank);
12074 }
12075
12076 /* Reallocate on assignment needs the loopinfo for extrinsic functions.
12077 This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
12078 Clearly, this cannot be done for an allocatable function result, since
12079 the shape of the result is unknown and, in any case, the function must
12080 correctly take care of the reallocation internally. For intrinsic
12081 calls, the array data is freed and the library takes care of allocation.
12082 TODO: Add logic of trans-array.cc: gfc_alloc_allocatable_for_assignment
12083 to the library. */
12084 if (flag_realloc_lhs
12085 && gfc_is_reallocatable_lhs (expr1)
12086 && !gfc_expr_attr (expr1).codimension
12087 && !gfc_is_coindexed (expr1)
12088 && !(expr2->value.function.esym
12089 && expr2->value.function.esym->result->attr.allocatable))
12090 {
12091 realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
12092
12093 if (!expr2->value.function.isym)
12094 {
12095 ss = gfc_walk_expr (expr1);
12096 gcc_assert (ss != gfc_ss_terminator);
12097
12098 realloc_lhs_loop_for_fcn_call (&se, &expr1->where, &ss, &loop);
12099 ss->is_alloc_lhs = 1;
12100 }
12101 else
12102 {
12103 tree dtype = NULL_TREE;
12104 tree type = gfc_typenode_for_spec (&expr2->ts);
12105 if (expr1->ts.type == BT_CLASS)
12106 {
12107 tmp = gfc_class_vptr_get (sym->backend_decl);
12108 tree tmp2 = gfc_get_symbol_decl (gfc_find_vtab (&expr2->ts));
12109 tmp2 = gfc_build_addr_expr (TREE_TYPE (tmp), tmp2);
12110 gfc_add_modify (&se.pre, tmp, tmp2);
12111 dtype = gfc_get_dtype_rank_type (expr1->rank,type);
12112 }
12113 fcncall_realloc_result (&se, expr1->rank, dtype);
12114 }
12115 }
12116
12117 gfc_conv_function_expr (&se, expr2);
12118
12119 /* Fix the result. */
12120 gfc_add_block_to_block (&se.pre, &se.post);
12121 if (finalizable)
12122 gfc_add_block_to_block (&se.pre, &final_se.pre);
12123
12124 /* Do the finalization, including final calls from function arguments. */
12125 if (finalizable)
12126 {
12127 gfc_add_block_to_block (&se.pre, &final_se.post);
12128 gfc_add_block_to_block (&se.pre, &se.finalblock);
12129 gfc_add_block_to_block (&se.pre, &final_se.finalblock);
12130 }
12131
12132 if (ss)
12133 gfc_cleanup_loop (&loop);
12134 else
12135 gfc_free_ss_chain (se.ss);
12136
12137 return gfc_finish_block (&se.pre);
12138 }
12139
12140
12141 /* Try to efficiently translate array(:) = 0. Return NULL if this
12142 can't be done. */
12143
12144 static tree
12145 gfc_trans_zero_assign (gfc_expr * expr)
12146 {
12147 tree dest, len, type;
12148 tree tmp;
12149 gfc_symbol *sym;
12150
12151 sym = expr->symtree->n.sym;
12152 dest = gfc_get_symbol_decl (sym);
12153
12154 type = TREE_TYPE (dest);
12155 if (POINTER_TYPE_P (type))
12156 type = TREE_TYPE (type);
12157 if (GFC_ARRAY_TYPE_P (type))
12158 {
12159 /* Determine the length of the array. */
12160 len = GFC_TYPE_ARRAY_SIZE (type);
12161 if (!len || TREE_CODE (len) != INTEGER_CST)
12162 return NULL_TREE;
12163 }
12164 else if (GFC_DESCRIPTOR_TYPE_P (type)
12165 && gfc_is_simply_contiguous (expr, false, false))
12166 {
12167 if (POINTER_TYPE_P (TREE_TYPE (dest)))
12168 dest = build_fold_indirect_ref_loc (input_location, dest);
12169 len = gfc_conv_descriptor_size (dest, GFC_TYPE_ARRAY_RANK (type));
12170 dest = gfc_conv_descriptor_data_get (dest);
12171 }
12172 else
12173 return NULL_TREE;
12174
12175 /* If we are zeroing a local array avoid taking its address by emitting
12176 a = {} instead. */
12177 if (!POINTER_TYPE_P (TREE_TYPE (dest)))
12178 return build2_loc (input_location, MODIFY_EXPR, void_type_node,
12179 dest, build_constructor (TREE_TYPE (dest),
12180 NULL));
12181
12182 /* Multiply len by element size. */
12183 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
12184 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
12185 len, fold_convert (gfc_array_index_type, tmp));
12186
12187 /* Convert arguments to the correct types. */
12188 dest = fold_convert (pvoid_type_node, dest);
12189 len = fold_convert (size_type_node, len);
12190
12191 /* Construct call to __builtin_memset. */
12192 tmp = build_call_expr_loc (input_location,
12193 builtin_decl_explicit (BUILT_IN_MEMSET),
12194 3, dest, integer_zero_node, len);
12195 return fold_convert (void_type_node, tmp);
12196 }
12197
12198
12199 /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
12200 that constructs the call to __builtin_memcpy. */
12201
12202 tree
12203 gfc_build_memcpy_call (tree dst, tree src, tree len)
12204 {
12205 tree tmp;
12206
12207 /* Convert arguments to the correct types. */
12208 if (!POINTER_TYPE_P (TREE_TYPE (dst)))
12209 dst = gfc_build_addr_expr (pvoid_type_node, dst);
12210 else
12211 dst = fold_convert (pvoid_type_node, dst);
12212
12213 if (!POINTER_TYPE_P (TREE_TYPE (src)))
12214 src = gfc_build_addr_expr (pvoid_type_node, src);
12215 else
12216 src = fold_convert (pvoid_type_node, src);
12217
12218 len = fold_convert (size_type_node, len);
12219
12220 /* Construct call to __builtin_memcpy. */
12221 tmp = build_call_expr_loc (input_location,
12222 builtin_decl_explicit (BUILT_IN_MEMCPY),
12223 3, dst, src, len);
12224 return fold_convert (void_type_node, tmp);
12225 }
12226
12227
12228 /* Try to efficiently translate dst(:) = src(:). Return NULL if this
12229 can't be done. EXPR1 is the destination/lhs and EXPR2 is the
12230 source/rhs, both are gfc_full_array_ref_p which have been checked for
12231 dependencies. */
12232
12233 static tree
12234 gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
12235 {
12236 tree dst, dlen, dtype;
12237 tree src, slen, stype;
12238 tree tmp;
12239
12240 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
12241 src = gfc_get_symbol_decl (expr2->symtree->n.sym);
12242
12243 dtype = TREE_TYPE (dst);
12244 if (POINTER_TYPE_P (dtype))
12245 dtype = TREE_TYPE (dtype);
12246 stype = TREE_TYPE (src);
12247 if (POINTER_TYPE_P (stype))
12248 stype = TREE_TYPE (stype);
12249
12250 if (!GFC_ARRAY_TYPE_P (dtype) || !GFC_ARRAY_TYPE_P (stype))
12251 return NULL_TREE;
12252
12253 /* Determine the lengths of the arrays. */
12254 dlen = GFC_TYPE_ARRAY_SIZE (dtype);
12255 if (!dlen || TREE_CODE (dlen) != INTEGER_CST)
12256 return NULL_TREE;
12257 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
12258 dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
12259 dlen, fold_convert (gfc_array_index_type, tmp));
12260
12261 slen = GFC_TYPE_ARRAY_SIZE (stype);
12262 if (!slen || TREE_CODE (slen) != INTEGER_CST)
12263 return NULL_TREE;
12264 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (stype));
12265 slen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
12266 slen, fold_convert (gfc_array_index_type, tmp));
12267
12268 /* Sanity check that they are the same. This should always be
12269 the case, as we should already have checked for conformance. */
12270 if (!tree_int_cst_equal (slen, dlen))
12271 return NULL_TREE;
12272
12273 return gfc_build_memcpy_call (dst, src, dlen);
12274 }
12275
12276
12277 /* Try to efficiently translate array(:) = (/ ... /). Return NULL if
12278 this can't be done. EXPR1 is the destination/lhs for which
12279 gfc_full_array_ref_p is true, and EXPR2 is the source/rhs. */
12280
12281 static tree
12282 gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
12283 {
12284 unsigned HOST_WIDE_INT nelem;
12285 tree dst, dtype;
12286 tree src, stype;
12287 tree len;
12288 tree tmp;
12289
12290 nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
12291 if (nelem == 0)
12292 return NULL_TREE;
12293
12294 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
12295 dtype = TREE_TYPE (dst);
12296 if (POINTER_TYPE_P (dtype))
12297 dtype = TREE_TYPE (dtype);
12298 if (!GFC_ARRAY_TYPE_P (dtype))
12299 return NULL_TREE;
12300
12301 /* Determine the lengths of the array. */
12302 len = GFC_TYPE_ARRAY_SIZE (dtype);
12303 if (!len || TREE_CODE (len) != INTEGER_CST)
12304 return NULL_TREE;
12305
12306 /* Confirm that the constructor is the same size. */
12307 if (compare_tree_int (len, nelem) != 0)
12308 return NULL_TREE;
12309
12310 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
12311 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
12312 fold_convert (gfc_array_index_type, tmp));
12313
12314 stype = gfc_typenode_for_spec (&expr2->ts);
12315 src = gfc_build_constant_array_constructor (expr2, stype);
12316
12317 return gfc_build_memcpy_call (dst, src, len);
12318 }
12319
12320
12321 /* Tells whether the expression is to be treated as a variable reference. */
12322
12323 bool
12324 gfc_expr_is_variable (gfc_expr *expr)
12325 {
12326 gfc_expr *arg;
12327 gfc_component *comp;
12328 gfc_symbol *func_ifc;
12329
12330 if (expr->expr_type == EXPR_VARIABLE)
12331 return true;
12332
12333 arg = gfc_get_noncopying_intrinsic_argument (expr);
12334 if (arg)
12335 {
12336 gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
12337 return gfc_expr_is_variable (arg);
12338 }
12339
12340 /* A data-pointer-returning function should be considered as a variable
12341 too. */
12342 if (expr->expr_type == EXPR_FUNCTION
12343 && expr->ref == NULL)
12344 {
12345 if (expr->value.function.isym != NULL)
12346 return false;
12347
12348 if (expr->value.function.esym != NULL)
12349 {
12350 func_ifc = expr->value.function.esym;
12351 goto found_ifc;
12352 }
12353 gcc_assert (expr->symtree);
12354 func_ifc = expr->symtree->n.sym;
12355 goto found_ifc;
12356 }
12357
12358 comp = gfc_get_proc_ptr_comp (expr);
12359 if ((expr->expr_type == EXPR_PPC || expr->expr_type == EXPR_FUNCTION)
12360 && comp)
12361 {
12362 func_ifc = comp->ts.interface;
12363 goto found_ifc;
12364 }
12365
12366 if (expr->expr_type == EXPR_COMPCALL)
12367 {
12368 gcc_assert (!expr->value.compcall.tbp->is_generic);
12369 func_ifc = expr->value.compcall.tbp->u.specific->n.sym;
12370 goto found_ifc;
12371 }
12372
12373 return false;
12374
12375 found_ifc:
12376 gcc_assert (func_ifc->attr.function
12377 && func_ifc->result != NULL);
12378 return func_ifc->result->attr.pointer;
12379 }
12380
12381
12382 /* Is the lhs OK for automatic reallocation? */
12383
12384 static bool
12385 is_scalar_reallocatable_lhs (gfc_expr *expr)
12386 {
12387 gfc_ref * ref;
12388
12389 /* An allocatable variable with no reference. */
12390 if (expr->symtree->n.sym->attr.allocatable
12391 && !expr->ref)
12392 return true;
12393
12394 /* All that can be left are allocatable components. However, we do
12395 not check for allocatable components here because the expression
12396 could be an allocatable component of a pointer component. */
12397 if (expr->symtree->n.sym->ts.type != BT_DERIVED
12398 && expr->symtree->n.sym->ts.type != BT_CLASS)
12399 return false;
12400
12401 /* Find an allocatable component ref last. */
12402 for (ref = expr->ref; ref; ref = ref->next)
12403 if (ref->type == REF_COMPONENT
12404 && !ref->next
12405 && ref->u.c.component->attr.allocatable)
12406 return true;
12407
12408 return false;
12409 }
12410
12411
12412 /* Allocate or reallocate scalar lhs, as necessary. */
12413
12414 static void
12415 alloc_scalar_allocatable_for_assignment (stmtblock_t *block,
12416 tree string_length,
12417 gfc_expr *expr1,
12418 gfc_expr *expr2)
12419
12420 {
12421 tree cond;
12422 tree tmp;
12423 tree size;
12424 tree size_in_bytes;
12425 tree jump_label1;
12426 tree jump_label2;
12427 gfc_se lse;
12428 gfc_ref *ref;
12429
12430 if (!expr1 || expr1->rank)
12431 return;
12432
12433 if (!expr2 || expr2->rank)
12434 return;
12435
12436 for (ref = expr1->ref; ref; ref = ref->next)
12437 if (ref->type == REF_SUBSTRING)
12438 return;
12439
12440 realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
12441
12442 /* Since this is a scalar lhs, we can afford to do this. That is,
12443 there is no risk of side effects being repeated. */
12444 gfc_init_se (&lse, NULL);
12445 lse.want_pointer = 1;
12446 gfc_conv_expr (&lse, expr1);
12447
12448 jump_label1 = gfc_build_label_decl (NULL_TREE);
12449 jump_label2 = gfc_build_label_decl (NULL_TREE);
12450
12451 /* Do the allocation if the lhs is NULL. Otherwise go to label 1. */
12452 tmp = build_int_cst (TREE_TYPE (lse.expr), 0);
12453 cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
12454 lse.expr, tmp);
12455 tmp = build3_v (COND_EXPR, cond,
12456 build1_v (GOTO_EXPR, jump_label1),
12457 build_empty_stmt (input_location));
12458 gfc_add_expr_to_block (block, tmp);
12459
12460 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
12461 {
12462 /* Use the rhs string length and the lhs element size. Note that 'size' is
12463 used below for the string-length comparison, only. */
12464 size = string_length;
12465 tmp = TYPE_SIZE_UNIT (gfc_get_char_type (expr1->ts.kind));
12466 size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
12467 TREE_TYPE (tmp), tmp,
12468 fold_convert (TREE_TYPE (tmp), size));
12469 }
12470 else
12471 {
12472 /* Otherwise use the length in bytes of the rhs. */
12473 size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr1->ts));
12474 size_in_bytes = size;
12475 }
12476
12477 size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
12478 size_in_bytes, size_one_node);
12479
12480 if (gfc_caf_attr (expr1).codimension && flag_coarray == GFC_FCOARRAY_LIB)
12481 {
12482 tree caf_decl, token;
12483 gfc_se caf_se;
12484 symbol_attribute attr;
12485
12486 gfc_clear_attr (&attr);
12487 gfc_init_se (&caf_se, NULL);
12488
12489 caf_decl = gfc_get_tree_for_caf_expr (expr1);
12490 gfc_get_caf_token_offset (&caf_se, &token, NULL, caf_decl, NULL_TREE,
12491 NULL);
12492 gfc_add_block_to_block (block, &caf_se.pre);
12493 gfc_allocate_allocatable (block, lse.expr, size_in_bytes,
12494 gfc_build_addr_expr (NULL_TREE, token),
12495 NULL_TREE, NULL_TREE, NULL_TREE, jump_label1,
12496 expr1, 1);
12497 }
12498 else if (expr1->ts.type == BT_DERIVED && expr1->ts.u.derived->attr.alloc_comp)
12499 {
12500 tmp = build_call_expr_loc (input_location,
12501 builtin_decl_explicit (BUILT_IN_CALLOC),
12502 2, build_one_cst (size_type_node),
12503 size_in_bytes);
12504 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
12505 gfc_add_modify (block, lse.expr, tmp);
12506 }
12507 else
12508 {
12509 tmp = build_call_expr_loc (input_location,
12510 builtin_decl_explicit (BUILT_IN_MALLOC),
12511 1, size_in_bytes);
12512 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
12513 gfc_add_modify (block, lse.expr, tmp);
12514 }
12515
12516 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
12517 {
12518 /* Deferred characters need checking for lhs and rhs string
12519 length. Other deferred parameter variables will have to
12520 come here too. */
12521 tmp = build1_v (GOTO_EXPR, jump_label2);
12522 gfc_add_expr_to_block (block, tmp);
12523 }
12524 tmp = build1_v (LABEL_EXPR, jump_label1);
12525 gfc_add_expr_to_block (block, tmp);
12526
12527 /* For a deferred length character, reallocate if lengths of lhs and
12528 rhs are different. */
12529 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
12530 {
12531 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
12532 lse.string_length,
12533 fold_convert (TREE_TYPE (lse.string_length),
12534 size));
12535 /* Jump past the realloc if the lengths are the same. */
12536 tmp = build3_v (COND_EXPR, cond,
12537 build1_v (GOTO_EXPR, jump_label2),
12538 build_empty_stmt (input_location));
12539 gfc_add_expr_to_block (block, tmp);
12540 tmp = build_call_expr_loc (input_location,
12541 builtin_decl_explicit (BUILT_IN_REALLOC),
12542 2, fold_convert (pvoid_type_node, lse.expr),
12543 size_in_bytes);
12544 tree omp_cond = NULL_TREE;
12545 if (flag_openmp_allocators)
12546 {
12547 tree omp_tmp;
12548 omp_cond = gfc_omp_call_is_alloc (lse.expr);
12549 omp_cond = gfc_evaluate_now (omp_cond, block);
12550
12551 omp_tmp = builtin_decl_explicit (BUILT_IN_GOMP_REALLOC);
12552 omp_tmp = build_call_expr_loc (input_location, omp_tmp, 4,
12553 fold_convert (pvoid_type_node,
12554 lse.expr), size_in_bytes,
12555 build_zero_cst (ptr_type_node),
12556 build_zero_cst (ptr_type_node));
12557 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
12558 omp_cond, omp_tmp, tmp);
12559 }
12560 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
12561 gfc_add_modify (block, lse.expr, tmp);
12562 if (omp_cond)
12563 gfc_add_expr_to_block (block,
12564 build3_loc (input_location, COND_EXPR,
12565 void_type_node, omp_cond,
12566 gfc_omp_call_add_alloc (lse.expr),
12567 build_empty_stmt (input_location)));
12568 tmp = build1_v (LABEL_EXPR, jump_label2);
12569 gfc_add_expr_to_block (block, tmp);
12570
12571 /* Update the lhs character length. */
12572 size = string_length;
12573 gfc_add_modify (block, lse.string_length,
12574 fold_convert (TREE_TYPE (lse.string_length), size));
12575 }
12576 }
12577
12578 /* Check for assignments of the type
12579
12580 a = a + 4
12581
12582 to make sure we do not check for reallocation unneccessarily. */
12583
12584
12585 static bool
12586 is_runtime_conformable (gfc_expr *expr1, gfc_expr *expr2)
12587 {
12588 gfc_actual_arglist *a;
12589 gfc_expr *e1, *e2;
12590
12591 switch (expr2->expr_type)
12592 {
12593 case EXPR_VARIABLE:
12594 return gfc_dep_compare_expr (expr1, expr2) == 0;
12595
12596 case EXPR_FUNCTION:
12597 if (expr2->value.function.esym
12598 && expr2->value.function.esym->attr.elemental)
12599 {
12600 for (a = expr2->value.function.actual; a != NULL; a = a->next)
12601 {
12602 e1 = a->expr;
12603 if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
12604 return false;
12605 }
12606 return true;
12607 }
12608 else if (expr2->value.function.isym
12609 && expr2->value.function.isym->elemental)
12610 {
12611 for (a = expr2->value.function.actual; a != NULL; a = a->next)
12612 {
12613 e1 = a->expr;
12614 if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
12615 return false;
12616 }
12617 return true;
12618 }
12619
12620 break;
12621
12622 case EXPR_OP:
12623 switch (expr2->value.op.op)
12624 {
12625 case INTRINSIC_NOT:
12626 case INTRINSIC_UPLUS:
12627 case INTRINSIC_UMINUS:
12628 case INTRINSIC_PARENTHESES:
12629 return is_runtime_conformable (expr1, expr2->value.op.op1);
12630
12631 case INTRINSIC_PLUS:
12632 case INTRINSIC_MINUS:
12633 case INTRINSIC_TIMES:
12634 case INTRINSIC_DIVIDE:
12635 case INTRINSIC_POWER:
12636 case INTRINSIC_AND:
12637 case INTRINSIC_OR:
12638 case INTRINSIC_EQV:
12639 case INTRINSIC_NEQV:
12640 case INTRINSIC_EQ:
12641 case INTRINSIC_NE:
12642 case INTRINSIC_GT:
12643 case INTRINSIC_GE:
12644 case INTRINSIC_LT:
12645 case INTRINSIC_LE:
12646 case INTRINSIC_EQ_OS:
12647 case INTRINSIC_NE_OS:
12648 case INTRINSIC_GT_OS:
12649 case INTRINSIC_GE_OS:
12650 case INTRINSIC_LT_OS:
12651 case INTRINSIC_LE_OS:
12652
12653 e1 = expr2->value.op.op1;
12654 e2 = expr2->value.op.op2;
12655
12656 if (e1->rank == 0 && e2->rank > 0)
12657 return is_runtime_conformable (expr1, e2);
12658 else if (e1->rank > 0 && e2->rank == 0)
12659 return is_runtime_conformable (expr1, e1);
12660 else if (e1->rank > 0 && e2->rank > 0)
12661 return is_runtime_conformable (expr1, e1)
12662 && is_runtime_conformable (expr1, e2);
12663 break;
12664
12665 default:
12666 break;
12667
12668 }
12669
12670 break;
12671
12672 default:
12673 break;
12674 }
12675 return false;
12676 }
12677
12678
12679 static tree
12680 trans_class_assignment (stmtblock_t *block, gfc_expr *lhs, gfc_expr *rhs,
12681 gfc_se *lse, gfc_se *rse, bool use_vptr_copy,
12682 bool class_realloc)
12683 {
12684 tree tmp, fcn, stdcopy, to_len, from_len, vptr, old_vptr, rhs_vptr;
12685 vec<tree, va_gc> *args = NULL;
12686 bool final_expr;
12687
12688 final_expr = gfc_assignment_finalizer_call (lse, lhs, false);
12689 if (final_expr)
12690 {
12691 if (rse->loop)
12692 gfc_prepend_expr_to_block (&rse->loop->pre,
12693 gfc_finish_block (&lse->finalblock));
12694 else
12695 gfc_add_block_to_block (block, &lse->finalblock);
12696 }
12697
12698 /* Store the old vptr so that dynamic types can be compared for
12699 reallocation to occur or not. */
12700 if (class_realloc)
12701 {
12702 tmp = lse->expr;
12703 if (!GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
12704 tmp = gfc_get_class_from_expr (tmp);
12705 }
12706
12707 vptr = trans_class_vptr_len_assignment (block, lhs, rhs, rse, &to_len,
12708 &from_len, &rhs_vptr);
12709 if (rhs_vptr == NULL_TREE)
12710 rhs_vptr = vptr;
12711
12712 /* Generate (re)allocation of the lhs. */
12713 if (class_realloc)
12714 {
12715 stmtblock_t alloc, re_alloc;
12716 tree class_han, re, size;
12717
12718 if (tmp && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
12719 old_vptr = gfc_evaluate_now (gfc_class_vptr_get (tmp), block);
12720 else
12721 old_vptr = build_int_cst (TREE_TYPE (vptr), 0);
12722
12723 size = gfc_vptr_size_get (rhs_vptr);
12724
12725 /* Take into account _len of unlimited polymorphic entities.
12726 TODO: handle class(*) allocatable function results on rhs. */
12727 if (UNLIMITED_POLY (rhs))
12728 {
12729 tree len;
12730 if (rhs->expr_type == EXPR_VARIABLE)
12731 len = trans_get_upoly_len (block, rhs);
12732 else
12733 len = gfc_class_len_get (tmp);
12734 len = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
12735 fold_convert (size_type_node, len),
12736 size_one_node);
12737 size = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (size),
12738 size, fold_convert (TREE_TYPE (size), len));
12739 }
12740 else if (rhs->ts.type == BT_CHARACTER && rse->string_length)
12741 size = fold_build2_loc (input_location, MULT_EXPR,
12742 gfc_charlen_type_node, size,
12743 rse->string_length);
12744
12745
12746 tmp = lse->expr;
12747 class_han = GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
12748 ? gfc_class_data_get (tmp) : tmp;
12749
12750 if (!POINTER_TYPE_P (TREE_TYPE (class_han)))
12751 class_han = gfc_build_addr_expr (NULL_TREE, class_han);
12752
12753 /* Allocate block. */
12754 gfc_init_block (&alloc);
12755 gfc_allocate_using_malloc (&alloc, class_han, size, NULL_TREE);
12756
12757 /* Reallocate if dynamic types are different. */
12758 gfc_init_block (&re_alloc);
12759 if (UNLIMITED_POLY (lhs) && rhs->ts.type == BT_CHARACTER)
12760 {
12761 gfc_add_expr_to_block (&re_alloc, gfc_call_free (class_han));
12762 gfc_allocate_using_malloc (&re_alloc, class_han, size, NULL_TREE);
12763 }
12764 else
12765 {
12766 tmp = fold_convert (pvoid_type_node, class_han);
12767 re = build_call_expr_loc (input_location,
12768 builtin_decl_explicit (BUILT_IN_REALLOC),
12769 2, tmp, size);
12770 re = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (tmp),
12771 tmp, re);
12772 tmp = fold_build2_loc (input_location, NE_EXPR,
12773 logical_type_node, rhs_vptr, old_vptr);
12774 re = fold_build3_loc (input_location, COND_EXPR, void_type_node,
12775 tmp, re, build_empty_stmt (input_location));
12776 gfc_add_expr_to_block (&re_alloc, re);
12777 }
12778 tree realloc_expr = lhs->ts.type == BT_CLASS ?
12779 gfc_finish_block (&re_alloc) :
12780 build_empty_stmt (input_location);
12781
12782 /* Allocate if _data is NULL, reallocate otherwise. */
12783 tmp = fold_build2_loc (input_location, EQ_EXPR,
12784 logical_type_node, class_han,
12785 build_int_cst (prvoid_type_node, 0));
12786 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
12787 gfc_unlikely (tmp,
12788 PRED_FORTRAN_FAIL_ALLOC),
12789 gfc_finish_block (&alloc),
12790 realloc_expr);
12791 gfc_add_expr_to_block (&lse->pre, tmp);
12792 }
12793
12794 fcn = gfc_vptr_copy_get (vptr);
12795
12796 tmp = GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr))
12797 ? gfc_class_data_get (rse->expr) : rse->expr;
12798 if (use_vptr_copy)
12799 {
12800 if (!POINTER_TYPE_P (TREE_TYPE (tmp))
12801 || INDIRECT_REF_P (tmp)
12802 || (rhs->ts.type == BT_DERIVED
12803 && rhs->ts.u.derived->attr.unlimited_polymorphic
12804 && !rhs->ts.u.derived->attr.pointer
12805 && !rhs->ts.u.derived->attr.allocatable)
12806 || (UNLIMITED_POLY (rhs)
12807 && !CLASS_DATA (rhs)->attr.pointer
12808 && !CLASS_DATA (rhs)->attr.allocatable))
12809 vec_safe_push (args, gfc_build_addr_expr (NULL_TREE, tmp));
12810 else
12811 vec_safe_push (args, tmp);
12812 tmp = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
12813 ? gfc_class_data_get (lse->expr) : lse->expr;
12814 if (!POINTER_TYPE_P (TREE_TYPE (tmp))
12815 || INDIRECT_REF_P (tmp)
12816 || (lhs->ts.type == BT_DERIVED
12817 && lhs->ts.u.derived->attr.unlimited_polymorphic
12818 && !lhs->ts.u.derived->attr.pointer
12819 && !lhs->ts.u.derived->attr.allocatable)
12820 || (UNLIMITED_POLY (lhs)
12821 && !CLASS_DATA (lhs)->attr.pointer
12822 && !CLASS_DATA (lhs)->attr.allocatable))
12823 vec_safe_push (args, gfc_build_addr_expr (NULL_TREE, tmp));
12824 else
12825 vec_safe_push (args, tmp);
12826
12827 stdcopy = build_call_vec (TREE_TYPE (TREE_TYPE (fcn)), fcn, args);
12828
12829 if (to_len != NULL_TREE && !integer_zerop (from_len))
12830 {
12831 tree extcopy;
12832 vec_safe_push (args, from_len);
12833 vec_safe_push (args, to_len);
12834 extcopy = build_call_vec (TREE_TYPE (TREE_TYPE (fcn)), fcn, args);
12835
12836 tmp = fold_build2_loc (input_location, GT_EXPR,
12837 logical_type_node, from_len,
12838 build_zero_cst (TREE_TYPE (from_len)));
12839 return fold_build3_loc (input_location, COND_EXPR,
12840 void_type_node, tmp,
12841 extcopy, stdcopy);
12842 }
12843 else
12844 return stdcopy;
12845 }
12846 else
12847 {
12848 tree rhst = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
12849 ? gfc_class_data_get (lse->expr) : lse->expr;
12850 stmtblock_t tblock;
12851 gfc_init_block (&tblock);
12852 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
12853 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
12854 if (!POINTER_TYPE_P (TREE_TYPE (rhst)))
12855 rhst = gfc_build_addr_expr (NULL_TREE, rhst);
12856 /* When coming from a ptr_copy lhs and rhs are swapped. */
12857 gfc_add_modify_loc (input_location, &tblock, rhst,
12858 fold_convert (TREE_TYPE (rhst), tmp));
12859 return gfc_finish_block (&tblock);
12860 }
12861 }
12862
12863 bool
12864 is_assoc_assign (gfc_expr *lhs, gfc_expr *rhs)
12865 {
12866 if (lhs->expr_type != EXPR_VARIABLE || rhs->expr_type != EXPR_VARIABLE)
12867 return false;
12868
12869 return lhs->symtree->n.sym->assoc
12870 && lhs->symtree->n.sym->assoc->target == rhs;
12871 }
12872
12873 /* Subroutine of gfc_trans_assignment that actually scalarizes the
12874 assignment. EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
12875 init_flag indicates initialization expressions and dealloc that no
12876 deallocate prior assignment is needed (if in doubt, set true).
12877 When PTR_COPY is set and expr1 is a class type, then use the _vptr-copy
12878 routine instead of a pointer assignment. Alias resolution is only done,
12879 when MAY_ALIAS is set (the default). This flag is used by ALLOCATE()
12880 where it is known, that newly allocated memory on the lhs can never be
12881 an alias of the rhs. */
12882
12883 static tree
12884 gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
12885 bool dealloc, bool use_vptr_copy, bool may_alias)
12886 {
12887 gfc_se lse;
12888 gfc_se rse;
12889 gfc_ss *lss;
12890 gfc_ss *lss_section;
12891 gfc_ss *rss;
12892 gfc_loopinfo loop;
12893 tree tmp;
12894 stmtblock_t block;
12895 stmtblock_t body;
12896 bool final_expr;
12897 bool l_is_temp;
12898 bool scalar_to_array;
12899 tree string_length;
12900 int n;
12901 bool maybe_workshare = false, lhs_refs_comp = false, rhs_refs_comp = false;
12902 symbol_attribute lhs_caf_attr, rhs_caf_attr, lhs_attr;
12903 bool is_poly_assign;
12904 bool realloc_flag;
12905 bool assoc_assign = false;
12906
12907 /* Assignment of the form lhs = rhs. */
12908 gfc_start_block (&block);
12909
12910 gfc_init_se (&lse, NULL);
12911 gfc_init_se (&rse, NULL);
12912
12913 /* Walk the lhs. */
12914 lss = gfc_walk_expr (expr1);
12915 if (gfc_is_reallocatable_lhs (expr1))
12916 {
12917 lss->no_bounds_check = 1;
12918 if (!(expr2->expr_type == EXPR_FUNCTION
12919 && expr2->value.function.isym != NULL
12920 && !(expr2->value.function.isym->elemental
12921 || expr2->value.function.isym->conversion)))
12922 lss->is_alloc_lhs = 1;
12923 }
12924 else
12925 lss->no_bounds_check = expr1->no_bounds_check;
12926
12927 rss = NULL;
12928
12929 if (expr2->expr_type != EXPR_VARIABLE
12930 && expr2->expr_type != EXPR_CONSTANT
12931 && (expr2->ts.type == BT_CLASS || gfc_may_be_finalized (expr2->ts)))
12932 {
12933 expr2->must_finalize = 1;
12934 /* F2023 7.5.6.3: If an executable construct references a nonpointer
12935 function, the result is finalized after execution of the innermost
12936 executable construct containing the reference. */
12937 if (expr2->expr_type == EXPR_FUNCTION
12938 && (gfc_expr_attr (expr2).pointer
12939 || (expr2->ts.type == BT_CLASS && CLASS_DATA (expr2)->attr.class_pointer)))
12940 expr2->must_finalize = 0;
12941 /* F2008 4.5.6.3 para 5: If an executable construct references a
12942 structure constructor or array constructor, the entity created by
12943 the constructor is finalized after execution of the innermost
12944 executable construct containing the reference.
12945 These finalizations were later deleted by the Combined Techical
12946 Corrigenda 1 TO 4 for fortran 2008 (f08/0011). */
12947 else if (gfc_notification_std (GFC_STD_F2018_DEL)
12948 && (expr2->expr_type == EXPR_STRUCTURE
12949 || expr2->expr_type == EXPR_ARRAY))
12950 expr2->must_finalize = 0;
12951 }
12952
12953
12954 /* Checking whether a class assignment is desired is quite complicated and
12955 needed at two locations, so do it once only before the information is
12956 needed. */
12957 lhs_attr = gfc_expr_attr (expr1);
12958
12959 is_poly_assign
12960 = (use_vptr_copy
12961 || ((lhs_attr.pointer || lhs_attr.allocatable) && !lhs_attr.dimension))
12962 && (expr1->ts.type == BT_CLASS || gfc_is_class_array_ref (expr1, NULL)
12963 || gfc_is_class_scalar_expr (expr1)
12964 || gfc_is_class_array_ref (expr2, NULL)
12965 || gfc_is_class_scalar_expr (expr2))
12966 && lhs_attr.flavor != FL_PROCEDURE;
12967
12968 assoc_assign = is_assoc_assign (expr1, expr2);
12969
12970 realloc_flag = flag_realloc_lhs
12971 && gfc_is_reallocatable_lhs (expr1)
12972 && expr2->rank
12973 && !is_runtime_conformable (expr1, expr2);
12974
12975 /* Only analyze the expressions for coarray properties, when in coarray-lib
12976 mode. Avoid false-positive uninitialized diagnostics with initializing
12977 the codimension flag unconditionally. */
12978 lhs_caf_attr.codimension = false;
12979 rhs_caf_attr.codimension = false;
12980 if (flag_coarray == GFC_FCOARRAY_LIB)
12981 {
12982 lhs_caf_attr = gfc_caf_attr (expr1, false, &lhs_refs_comp);
12983 rhs_caf_attr = gfc_caf_attr (expr2, false, &rhs_refs_comp);
12984 }
12985
12986 if (lss != gfc_ss_terminator)
12987 {
12988 /* The assignment needs scalarization. */
12989 lss_section = lss;
12990
12991 /* Find a non-scalar SS from the lhs. */
12992 while (lss_section != gfc_ss_terminator
12993 && lss_section->info->type != GFC_SS_SECTION)
12994 lss_section = lss_section->next;
12995
12996 gcc_assert (lss_section != gfc_ss_terminator);
12997
12998 /* Initialize the scalarizer. */
12999 gfc_init_loopinfo (&loop);
13000
13001 /* Walk the rhs. */
13002 rss = gfc_walk_expr (expr2);
13003 if (rss == gfc_ss_terminator)
13004 /* The rhs is scalar. Add a ss for the expression. */
13005 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
13006 /* When doing a class assign, then the handle to the rhs needs to be a
13007 pointer to allow for polymorphism. */
13008 if (is_poly_assign && expr2->rank == 0 && !UNLIMITED_POLY (expr2))
13009 rss->info->type = GFC_SS_REFERENCE;
13010
13011 rss->no_bounds_check = expr2->no_bounds_check;
13012 /* Associate the SS with the loop. */
13013 gfc_add_ss_to_loop (&loop, lss);
13014 gfc_add_ss_to_loop (&loop, rss);
13015
13016 /* Calculate the bounds of the scalarization. */
13017 gfc_conv_ss_startstride (&loop);
13018 /* Enable loop reversal. */
13019 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
13020 loop.reverse[n] = GFC_ENABLE_REVERSE;
13021 /* Resolve any data dependencies in the statement. */
13022 if (may_alias)
13023 gfc_conv_resolve_dependencies (&loop, lss, rss);
13024 /* Setup the scalarizing loops. */
13025 gfc_conv_loop_setup (&loop, &expr2->where);
13026
13027 /* Setup the gfc_se structures. */
13028 gfc_copy_loopinfo_to_se (&lse, &loop);
13029 gfc_copy_loopinfo_to_se (&rse, &loop);
13030
13031 rse.ss = rss;
13032 gfc_mark_ss_chain_used (rss, 1);
13033 if (loop.temp_ss == NULL)
13034 {
13035 lse.ss = lss;
13036 gfc_mark_ss_chain_used (lss, 1);
13037 }
13038 else
13039 {
13040 lse.ss = loop.temp_ss;
13041 gfc_mark_ss_chain_used (lss, 3);
13042 gfc_mark_ss_chain_used (loop.temp_ss, 3);
13043 }
13044
13045 /* Allow the scalarizer to workshare array assignments. */
13046 if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_BODY))
13047 == OMPWS_WORKSHARE_FLAG
13048 && loop.temp_ss == NULL)
13049 {
13050 maybe_workshare = true;
13051 ompws_flags |= OMPWS_SCALARIZER_WS | OMPWS_SCALARIZER_BODY;
13052 }
13053
13054 /* Start the scalarized loop body. */
13055 gfc_start_scalarized_body (&loop, &body);
13056 }
13057 else
13058 gfc_init_block (&body);
13059
13060 l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
13061
13062 /* Translate the expression. */
13063 rse.want_coarray = flag_coarray == GFC_FCOARRAY_LIB
13064 && (init_flag || assoc_assign) && lhs_caf_attr.codimension;
13065 rse.want_pointer = rse.want_coarray && !init_flag && !lhs_caf_attr.dimension;
13066 gfc_conv_expr (&rse, expr2);
13067
13068 /* Deal with the case of a scalar class function assigned to a derived type.
13069 */
13070 if (gfc_is_alloc_class_scalar_function (expr2)
13071 && expr1->ts.type == BT_DERIVED)
13072 {
13073 rse.expr = gfc_class_data_get (rse.expr);
13074 rse.expr = build_fold_indirect_ref_loc (input_location, rse.expr);
13075 }
13076
13077 /* Stabilize a string length for temporaries. */
13078 if (expr2->ts.type == BT_CHARACTER && !expr1->ts.deferred
13079 && !(VAR_P (rse.string_length)
13080 || TREE_CODE (rse.string_length) == PARM_DECL
13081 || INDIRECT_REF_P (rse.string_length)))
13082 string_length = gfc_evaluate_now (rse.string_length, &rse.pre);
13083 else if (expr2->ts.type == BT_CHARACTER)
13084 {
13085 if (expr1->ts.deferred
13086 && gfc_expr_attr (expr1).allocatable
13087 && gfc_check_dependency (expr1, expr2, true))
13088 rse.string_length =
13089 gfc_evaluate_now_function_scope (rse.string_length, &rse.pre);
13090 string_length = rse.string_length;
13091 }
13092 else
13093 string_length = NULL_TREE;
13094
13095 if (l_is_temp)
13096 {
13097 gfc_conv_tmp_array_ref (&lse);
13098 if (expr2->ts.type == BT_CHARACTER)
13099 lse.string_length = string_length;
13100 }
13101 else
13102 {
13103 gfc_conv_expr (&lse, expr1);
13104 /* For some expression (e.g. complex numbers) fold_convert uses a
13105 SAVE_EXPR, which is hazardous on the lhs, because the value is
13106 not updated when assigned to. */
13107 if (TREE_CODE (lse.expr) == SAVE_EXPR)
13108 lse.expr = TREE_OPERAND (lse.expr, 0);
13109
13110 if (gfc_option.rtcheck & GFC_RTCHECK_MEM && !init_flag
13111 && gfc_expr_attr (expr1).allocatable && expr1->rank && !expr2->rank)
13112 {
13113 tree cond;
13114 const char* msg;
13115
13116 tmp = INDIRECT_REF_P (lse.expr)
13117 ? gfc_build_addr_expr (NULL_TREE, lse.expr) : lse.expr;
13118 STRIP_NOPS (tmp);
13119
13120 /* We should only get array references here. */
13121 gcc_assert (TREE_CODE (tmp) == POINTER_PLUS_EXPR
13122 || TREE_CODE (tmp) == ARRAY_REF);
13123
13124 /* 'tmp' is either the pointer to the array(POINTER_PLUS_EXPR)
13125 or the array itself(ARRAY_REF). */
13126 tmp = TREE_OPERAND (tmp, 0);
13127
13128 /* Provide the address of the array. */
13129 if (TREE_CODE (lse.expr) == ARRAY_REF)
13130 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
13131
13132 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
13133 tmp, build_int_cst (TREE_TYPE (tmp), 0));
13134 msg = _("Assignment of scalar to unallocated array");
13135 gfc_trans_runtime_check (true, false, cond, &loop.pre,
13136 &expr1->where, msg);
13137 }
13138
13139 /* Deallocate the lhs parameterized components if required. */
13140 if (dealloc && expr2->expr_type == EXPR_FUNCTION
13141 && !expr1->symtree->n.sym->attr.associate_var)
13142 {
13143 if (expr1->ts.type == BT_DERIVED
13144 && expr1->ts.u.derived
13145 && expr1->ts.u.derived->attr.pdt_type)
13146 {
13147 tmp = gfc_deallocate_pdt_comp (expr1->ts.u.derived, lse.expr,
13148 expr1->rank);
13149 gfc_add_expr_to_block (&lse.pre, tmp);
13150 }
13151 else if (expr1->ts.type == BT_CLASS
13152 && CLASS_DATA (expr1)->ts.u.derived
13153 && CLASS_DATA (expr1)->ts.u.derived->attr.pdt_type)
13154 {
13155 tmp = gfc_class_data_get (lse.expr);
13156 tmp = gfc_deallocate_pdt_comp (CLASS_DATA (expr1)->ts.u.derived,
13157 tmp, expr1->rank);
13158 gfc_add_expr_to_block (&lse.pre, tmp);
13159 }
13160 }
13161 }
13162
13163 /* Assignments of scalar derived types with allocatable components
13164 to arrays must be done with a deep copy and the rhs temporary
13165 must have its components deallocated afterwards. */
13166 scalar_to_array = (expr2->ts.type == BT_DERIVED
13167 && expr2->ts.u.derived->attr.alloc_comp
13168 && !gfc_expr_is_variable (expr2)
13169 && expr1->rank && !expr2->rank);
13170 scalar_to_array |= (expr1->ts.type == BT_DERIVED
13171 && expr1->rank
13172 && expr1->ts.u.derived->attr.alloc_comp
13173 && gfc_is_alloc_class_scalar_function (expr2));
13174 if (scalar_to_array && dealloc)
13175 {
13176 tmp = gfc_deallocate_alloc_comp_no_caf (expr2->ts.u.derived, rse.expr, 0);
13177 gfc_prepend_expr_to_block (&loop.post, tmp);
13178 }
13179
13180 /* When assigning a character function result to a deferred-length variable,
13181 the function call must happen before the (re)allocation of the lhs -
13182 otherwise the character length of the result is not known.
13183 NOTE 1: This relies on having the exact dependence of the length type
13184 parameter available to the caller; gfortran saves it in the .mod files.
13185 NOTE 2: Vector array references generate an index temporary that must
13186 not go outside the loop. Otherwise, variables should not generate
13187 a pre block.
13188 NOTE 3: The concatenation operation generates a temporary pointer,
13189 whose allocation must go to the innermost loop.
13190 NOTE 4: Elemental functions may generate a temporary, too. */
13191 if (flag_realloc_lhs
13192 && expr2->ts.type == BT_CHARACTER && expr1->ts.deferred
13193 && !(lss != gfc_ss_terminator
13194 && rss != gfc_ss_terminator
13195 && ((expr2->expr_type == EXPR_VARIABLE && expr2->rank)
13196 || (expr2->expr_type == EXPR_FUNCTION
13197 && expr2->value.function.esym != NULL
13198 && expr2->value.function.esym->attr.elemental)
13199 || (expr2->expr_type == EXPR_FUNCTION
13200 && expr2->value.function.isym != NULL
13201 && expr2->value.function.isym->elemental)
13202 || (expr2->expr_type == EXPR_OP
13203 && expr2->value.op.op == INTRINSIC_CONCAT))))
13204 gfc_add_block_to_block (&block, &rse.pre);
13205
13206 /* Nullify the allocatable components corresponding to those of the lhs
13207 derived type, so that the finalization of the function result does not
13208 affect the lhs of the assignment. Prepend is used to ensure that the
13209 nullification occurs before the call to the finalizer. In the case of
13210 a scalar to array assignment, this is done in gfc_trans_scalar_assign
13211 as part of the deep copy. */
13212 if (!scalar_to_array && expr1->ts.type == BT_DERIVED
13213 && (gfc_is_class_array_function (expr2)
13214 || gfc_is_alloc_class_scalar_function (expr2)))
13215 {
13216 tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, rse.expr, 0);
13217 gfc_prepend_expr_to_block (&rse.post, tmp);
13218 if (lss != gfc_ss_terminator && rss == gfc_ss_terminator)
13219 gfc_add_block_to_block (&loop.post, &rse.post);
13220 }
13221
13222 tmp = NULL_TREE;
13223
13224 if (is_poly_assign)
13225 {
13226 tmp = trans_class_assignment (&body, expr1, expr2, &lse, &rse,
13227 use_vptr_copy || (lhs_attr.allocatable
13228 && !lhs_attr.dimension),
13229 !realloc_flag && flag_realloc_lhs
13230 && !lhs_attr.pointer);
13231 if (expr2->expr_type == EXPR_FUNCTION
13232 && expr2->ts.type == BT_DERIVED
13233 && expr2->ts.u.derived->attr.alloc_comp)
13234 {
13235 tree tmp2 = gfc_deallocate_alloc_comp (expr2->ts.u.derived,
13236 rse.expr, expr2->rank);
13237 if (lss == gfc_ss_terminator)
13238 gfc_add_expr_to_block (&rse.post, tmp2);
13239 else
13240 gfc_add_expr_to_block (&loop.post, tmp2);
13241 }
13242
13243 expr1->must_finalize = 0;
13244 }
13245 else if (!is_poly_assign && expr2->must_finalize
13246 && expr1->ts.type == BT_CLASS
13247 && expr2->ts.type == BT_CLASS)
13248 {
13249 /* This case comes about when the scalarizer provides array element
13250 references. Use the vptr copy function, since this does a deep
13251 copy of allocatable components, without which the finalizer call
13252 will deallocate the components. */
13253 tmp = gfc_get_vptr_from_expr (rse.expr);
13254 if (tmp != NULL_TREE)
13255 {
13256 tree fcn = gfc_vptr_copy_get (tmp);
13257 if (POINTER_TYPE_P (TREE_TYPE (fcn)))
13258 fcn = build_fold_indirect_ref_loc (input_location, fcn);
13259 tmp = build_call_expr_loc (input_location,
13260 fcn, 2,
13261 gfc_build_addr_expr (NULL, rse.expr),
13262 gfc_build_addr_expr (NULL, lse.expr));
13263 }
13264 }
13265
13266 /* Comply with F2018 (7.5.6.3). Make sure that any finalization code is added
13267 after evaluation of the rhs and before reallocation. */
13268 final_expr = gfc_assignment_finalizer_call (&lse, expr1, init_flag);
13269 if (final_expr && !(expr2->expr_type == EXPR_VARIABLE
13270 && expr2->symtree->n.sym->attr.artificial))
13271 {
13272 if (lss == gfc_ss_terminator)
13273 {
13274 gfc_add_block_to_block (&block, &rse.pre);
13275 gfc_add_block_to_block (&block, &lse.finalblock);
13276 }
13277 else
13278 {
13279 gfc_add_block_to_block (&body, &rse.pre);
13280 gfc_add_block_to_block (&loop.code[expr1->rank - 1],
13281 &lse.finalblock);
13282 }
13283 }
13284 else
13285 gfc_add_block_to_block (&body, &rse.pre);
13286
13287 if (flag_coarray != GFC_FCOARRAY_NONE && expr1->ts.type == BT_CHARACTER
13288 && assoc_assign)
13289 tmp = gfc_trans_pointer_assignment (expr1, expr2);
13290
13291 /* If nothing else works, do it the old fashioned way! */
13292 if (tmp == NULL_TREE)
13293 tmp
13294 = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
13295 gfc_expr_is_variable (expr2) || scalar_to_array
13296 || expr2->expr_type == EXPR_ARRAY,
13297 !(l_is_temp || init_flag) && dealloc,
13298 expr1->symtree->n.sym->attr.codimension,
13299 assoc_assign);
13300
13301 /* Add the lse pre block to the body */
13302 gfc_add_block_to_block (&body, &lse.pre);
13303 gfc_add_expr_to_block (&body, tmp);
13304
13305 /* Add the post blocks to the body. Scalar finalization must appear before
13306 the post block in case any dellocations are done. */
13307 if (rse.finalblock.head
13308 && (!l_is_temp || (expr2->expr_type == EXPR_FUNCTION
13309 && gfc_expr_attr (expr2).elemental)))
13310 {
13311 gfc_add_block_to_block (&body, &rse.finalblock);
13312 gfc_add_block_to_block (&body, &rse.post);
13313 }
13314 else
13315 gfc_add_block_to_block (&body, &rse.post);
13316
13317 gfc_add_block_to_block (&body, &lse.post);
13318
13319 if (lss == gfc_ss_terminator)
13320 {
13321 /* F2003: Add the code for reallocation on assignment. */
13322 if (flag_realloc_lhs && is_scalar_reallocatable_lhs (expr1)
13323 && !is_poly_assign)
13324 alloc_scalar_allocatable_for_assignment (&block, string_length,
13325 expr1, expr2);
13326
13327 /* Use the scalar assignment as is. */
13328 gfc_add_block_to_block (&block, &body);
13329 }
13330 else
13331 {
13332 gcc_assert (lse.ss == gfc_ss_terminator
13333 && rse.ss == gfc_ss_terminator);
13334
13335 if (l_is_temp)
13336 {
13337 gfc_trans_scalarized_loop_boundary (&loop, &body);
13338
13339 /* We need to copy the temporary to the actual lhs. */
13340 gfc_init_se (&lse, NULL);
13341 gfc_init_se (&rse, NULL);
13342 gfc_copy_loopinfo_to_se (&lse, &loop);
13343 gfc_copy_loopinfo_to_se (&rse, &loop);
13344
13345 rse.ss = loop.temp_ss;
13346 lse.ss = lss;
13347
13348 gfc_conv_tmp_array_ref (&rse);
13349 gfc_conv_expr (&lse, expr1);
13350
13351 gcc_assert (lse.ss == gfc_ss_terminator
13352 && rse.ss == gfc_ss_terminator);
13353
13354 if (expr2->ts.type == BT_CHARACTER)
13355 rse.string_length = string_length;
13356
13357 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
13358 false, dealloc);
13359 gfc_add_expr_to_block (&body, tmp);
13360 }
13361
13362 /* F2003: Allocate or reallocate lhs of allocatable array. */
13363 if (realloc_flag)
13364 {
13365 realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
13366 ompws_flags &= ~OMPWS_SCALARIZER_WS;
13367 tmp = gfc_alloc_allocatable_for_assignment (&loop, expr1, expr2);
13368 if (tmp != NULL_TREE)
13369 gfc_add_expr_to_block (&loop.code[expr1->rank - 1], tmp);
13370 }
13371
13372 if (maybe_workshare)
13373 ompws_flags &= ~OMPWS_SCALARIZER_BODY;
13374
13375 /* Generate the copying loops. */
13376 gfc_trans_scalarizing_loops (&loop, &body);
13377
13378 /* Wrap the whole thing up. */
13379 gfc_add_block_to_block (&block, &loop.pre);
13380 gfc_add_block_to_block (&block, &loop.post);
13381
13382 gfc_cleanup_loop (&loop);
13383 }
13384
13385 return gfc_finish_block (&block);
13386 }
13387
13388
13389 /* Check whether EXPR is a copyable array. */
13390
13391 static bool
13392 copyable_array_p (gfc_expr * expr)
13393 {
13394 if (expr->expr_type != EXPR_VARIABLE)
13395 return false;
13396
13397 /* First check it's an array. */
13398 if (expr->rank < 1 || !expr->ref || expr->ref->next)
13399 return false;
13400
13401 if (!gfc_full_array_ref_p (expr->ref, NULL))
13402 return false;
13403
13404 /* Next check that it's of a simple enough type. */
13405 switch (expr->ts.type)
13406 {
13407 case BT_INTEGER:
13408 case BT_REAL:
13409 case BT_COMPLEX:
13410 case BT_LOGICAL:
13411 return true;
13412
13413 case BT_CHARACTER:
13414 return false;
13415
13416 case_bt_struct:
13417 return !expr->ts.u.derived->attr.alloc_comp;
13418
13419 default:
13420 break;
13421 }
13422
13423 return false;
13424 }
13425
13426 /* Translate an assignment. */
13427
13428 tree
13429 gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
13430 bool dealloc, bool use_vptr_copy, bool may_alias)
13431 {
13432 tree tmp;
13433
13434 /* Special case a single function returning an array. */
13435 if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
13436 {
13437 tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
13438 if (tmp)
13439 return tmp;
13440 }
13441
13442 /* Special case assigning an array to zero. */
13443 if (copyable_array_p (expr1)
13444 && is_zero_initializer_p (expr2))
13445 {
13446 tmp = gfc_trans_zero_assign (expr1);
13447 if (tmp)
13448 return tmp;
13449 }
13450
13451 /* Special case copying one array to another. */
13452 if (copyable_array_p (expr1)
13453 && copyable_array_p (expr2)
13454 && gfc_compare_types (&expr1->ts, &expr2->ts)
13455 && !gfc_check_dependency (expr1, expr2, 0))
13456 {
13457 tmp = gfc_trans_array_copy (expr1, expr2);
13458 if (tmp)
13459 return tmp;
13460 }
13461
13462 /* Special case initializing an array from a constant array constructor. */
13463 if (copyable_array_p (expr1)
13464 && expr2->expr_type == EXPR_ARRAY
13465 && gfc_compare_types (&expr1->ts, &expr2->ts))
13466 {
13467 tmp = gfc_trans_array_constructor_copy (expr1, expr2);
13468 if (tmp)
13469 return tmp;
13470 }
13471
13472 if (UNLIMITED_POLY (expr1) && expr1->rank)
13473 use_vptr_copy = true;
13474
13475 /* Fallback to the scalarizer to generate explicit loops. */
13476 return gfc_trans_assignment_1 (expr1, expr2, init_flag, dealloc,
13477 use_vptr_copy, may_alias);
13478 }
13479
13480 tree
13481 gfc_trans_init_assign (gfc_code * code)
13482 {
13483 return gfc_trans_assignment (code->expr1, code->expr2, true, false, true);
13484 }
13485
13486 tree
13487 gfc_trans_assign (gfc_code * code)
13488 {
13489 return gfc_trans_assignment (code->expr1, code->expr2, false, true);
13490 }
13491
13492 /* Generate a simple loop for internal use of the form
13493 for (var = begin; var <cond> end; var += step)
13494 body; */
13495 void
13496 gfc_simple_for_loop (stmtblock_t *block, tree var, tree begin, tree end,
13497 enum tree_code cond, tree step, tree body)
13498 {
13499 tree tmp;
13500
13501 /* var = begin. */
13502 gfc_add_modify (block, var, begin);
13503
13504 /* Loop: for (var = begin; var <cond> end; var += step). */
13505 tree label_loop = gfc_build_label_decl (NULL_TREE);
13506 tree label_cond = gfc_build_label_decl (NULL_TREE);
13507 TREE_USED (label_loop) = 1;
13508 TREE_USED (label_cond) = 1;
13509
13510 gfc_add_expr_to_block (block, build1_v (GOTO_EXPR, label_cond));
13511 gfc_add_expr_to_block (block, build1_v (LABEL_EXPR, label_loop));
13512
13513 /* Loop body. */
13514 gfc_add_expr_to_block (block, body);
13515
13516 /* End of loop body. */
13517 tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (var), var, step);
13518 gfc_add_modify (block, var, tmp);
13519 gfc_add_expr_to_block (block, build1_v (LABEL_EXPR, label_cond));
13520 tmp = fold_build2_loc (input_location, cond, boolean_type_node, var, end);
13521 tmp = build3_v (COND_EXPR, tmp, build1_v (GOTO_EXPR, label_loop),
13522 build_empty_stmt (input_location));
13523 gfc_add_expr_to_block (block, tmp);
13524 }