Line data Source code
1 : /* Expression translation
2 : Copyright (C) 2002-2026 Free Software Foundation, Inc.
3 : Contributed by Paul Brook <paul@nowt.org>
4 : and Steven Bosscher <s.bosscher@student.tudelft.nl>
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 : #include "trans-descriptor.h"
42 : /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
43 : #include "trans-stmt.h"
44 : #include "dependency.h"
45 : #include "gimplify.h"
46 : #include "tm.h" /* For CHAR_TYPE_SIZE. */
47 :
48 :
49 : /* Calculate the number of characters in a string. */
50 :
51 : static tree
52 37142 : gfc_get_character_len (tree type)
53 : {
54 37142 : tree len;
55 :
56 37142 : gcc_assert (type && TREE_CODE (type) == ARRAY_TYPE
57 : && TYPE_STRING_FLAG (type));
58 :
59 37142 : len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
60 37142 : len = (len) ? (len) : (integer_zero_node);
61 37142 : return fold_convert (gfc_charlen_type_node, len);
62 : }
63 :
64 :
65 :
66 : /* Calculate the number of bytes in a string. */
67 :
68 : tree
69 37142 : gfc_get_character_len_in_bytes (tree type)
70 : {
71 37142 : tree tmp, len;
72 :
73 37142 : gcc_assert (type && TREE_CODE (type) == ARRAY_TYPE
74 : && TYPE_STRING_FLAG (type));
75 :
76 37142 : tmp = TYPE_SIZE_UNIT (TREE_TYPE (type));
77 74284 : tmp = (tmp && !integer_zerop (tmp))
78 74284 : ? (fold_convert (gfc_charlen_type_node, tmp)) : (NULL_TREE);
79 37142 : len = gfc_get_character_len (type);
80 37142 : if (tmp && len && !integer_zerop (len))
81 36293 : len = fold_build2_loc (input_location, MULT_EXPR,
82 : gfc_charlen_type_node, len, tmp);
83 37142 : return len;
84 : }
85 :
86 :
87 : tree
88 7061 : gfc_conv_scalar_to_descriptor (gfc_se *se, tree scalar, symbol_attribute attr)
89 : {
90 7061 : tree desc, type;
91 :
92 7061 : type = gfc_get_scalar_to_descriptor_type (TREE_TYPE (scalar), attr);
93 7061 : desc = gfc_create_var (type, "desc");
94 7061 : DECL_ARTIFICIAL (desc) = 1;
95 :
96 7061 : if (CONSTANT_CLASS_P (scalar))
97 : {
98 0 : tree tmp;
99 0 : tmp = gfc_create_var (TREE_TYPE (scalar), "scalar");
100 0 : gfc_add_modify (&se->pre, tmp, scalar);
101 0 : scalar = tmp;
102 : }
103 :
104 7061 : gfc_set_descriptor_from_scalar (&se->pre, desc, scalar);
105 :
106 : /* Copy pointer address back - but only if it could have changed and
107 : if the actual argument is a pointer and not, e.g., NULL(). */
108 7061 : if ((attr.pointer || attr.allocatable) && attr.intent != INTENT_IN)
109 2676 : gfc_add_modify (&se->post, scalar,
110 1338 : fold_convert (TREE_TYPE (scalar),
111 : gfc_conv_descriptor_data_get (desc)));
112 7061 : return desc;
113 : }
114 :
115 :
116 : /* Get the coarray token from the ultimate array or component ref.
117 : Returns a NULL_TREE, when the ref object is not allocatable or pointer. */
118 :
119 : tree
120 580 : gfc_get_ultimate_alloc_ptr_comps_caf_token (gfc_se *outerse, gfc_expr *expr)
121 : {
122 580 : gfc_symbol *sym = expr->symtree->n.sym;
123 1160 : bool is_coarray = sym->ts.type == BT_CLASS
124 580 : ? CLASS_DATA (sym)->attr.codimension
125 531 : : sym->attr.codimension;
126 580 : gfc_expr *caf_expr = gfc_copy_expr (expr);
127 580 : gfc_ref *ref = caf_expr->ref, *last_caf_ref = NULL;
128 :
129 1784 : while (ref)
130 : {
131 1204 : if (ref->type == REF_COMPONENT
132 439 : && (ref->u.c.component->attr.allocatable
133 104 : || ref->u.c.component->attr.pointer)
134 437 : && (is_coarray || ref->u.c.component->attr.codimension))
135 1204 : last_caf_ref = ref;
136 1204 : ref = ref->next;
137 : }
138 :
139 580 : if (last_caf_ref == NULL)
140 : {
141 226 : gfc_free_expr (caf_expr);
142 226 : return NULL_TREE;
143 : }
144 :
145 143 : tree comp = last_caf_ref->u.c.component->caf_token
146 354 : ? gfc_comp_caf_token (last_caf_ref->u.c.component)
147 : : NULL_TREE,
148 : caf;
149 354 : gfc_se se;
150 354 : bool comp_ref = !last_caf_ref->u.c.component->attr.dimension;
151 354 : if (comp == NULL_TREE && comp_ref)
152 : {
153 62 : gfc_free_expr (caf_expr);
154 62 : return NULL_TREE;
155 : }
156 292 : gfc_init_se (&se, outerse);
157 292 : gfc_free_ref_list (last_caf_ref->next);
158 292 : last_caf_ref->next = NULL;
159 292 : caf_expr->rank = comp_ref ? 0 : last_caf_ref->u.c.component->as->rank;
160 584 : caf_expr->corank = last_caf_ref->u.c.component->as
161 292 : ? last_caf_ref->u.c.component->as->corank
162 : : expr->corank;
163 292 : se.want_pointer = comp_ref;
164 292 : gfc_conv_expr (&se, caf_expr);
165 292 : gfc_add_block_to_block (&outerse->pre, &se.pre);
166 :
167 292 : if (TREE_CODE (se.expr) == COMPONENT_REF && comp_ref)
168 143 : se.expr = TREE_OPERAND (se.expr, 0);
169 292 : gfc_free_expr (caf_expr);
170 :
171 292 : if (comp_ref)
172 143 : caf = fold_build3_loc (input_location, COMPONENT_REF,
173 143 : TREE_TYPE (comp), se.expr, comp, NULL_TREE);
174 : else
175 149 : caf = gfc_conv_descriptor_token (se.expr);
176 292 : return gfc_build_addr_expr (NULL_TREE, caf);
177 : }
178 :
179 :
180 : /* This is the seed for an eventual trans-class.c
181 :
182 : The following parameters should not be used directly since they might
183 : in future implementations. Use the corresponding APIs. */
184 : #define CLASS_DATA_FIELD 0
185 : #define CLASS_VPTR_FIELD 1
186 : #define CLASS_LEN_FIELD 2
187 : #define VTABLE_HASH_FIELD 0
188 : #define VTABLE_SIZE_FIELD 1
189 : #define VTABLE_EXTENDS_FIELD 2
190 : #define VTABLE_DEF_INIT_FIELD 3
191 : #define VTABLE_COPY_FIELD 4
192 : #define VTABLE_FINAL_FIELD 5
193 : #define VTABLE_DEALLOCATE_FIELD 6
194 :
195 :
196 : tree
197 40 : gfc_class_set_static_fields (tree decl, tree vptr, tree data)
198 : {
199 40 : tree tmp;
200 40 : tree field;
201 40 : vec<constructor_elt, va_gc> *init = NULL;
202 :
203 40 : field = TYPE_FIELDS (TREE_TYPE (decl));
204 40 : tmp = gfc_advance_chain (field, CLASS_DATA_FIELD);
205 40 : CONSTRUCTOR_APPEND_ELT (init, tmp, data);
206 :
207 40 : tmp = gfc_advance_chain (field, CLASS_VPTR_FIELD);
208 40 : CONSTRUCTOR_APPEND_ELT (init, tmp, vptr);
209 :
210 40 : return build_constructor (TREE_TYPE (decl), init);
211 : }
212 :
213 :
214 : tree
215 36525 : gfc_class_data_get (tree decl)
216 : {
217 36525 : tree data;
218 36525 : if (POINTER_TYPE_P (TREE_TYPE (decl)))
219 5453 : decl = build_fold_indirect_ref_loc (input_location, decl);
220 36525 : data = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
221 : CLASS_DATA_FIELD);
222 36525 : return fold_build3_loc (input_location, COMPONENT_REF,
223 36525 : TREE_TYPE (data), decl, data,
224 36525 : NULL_TREE);
225 : }
226 :
227 :
228 : tree
229 52530 : gfc_class_vptr_get (tree decl)
230 : {
231 52530 : tree vptr;
232 : /* For class arrays decl may be a temporary descriptor handle, the vptr is
233 : then available through the saved descriptor. */
234 31060 : if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
235 54471 : && GFC_DECL_SAVED_DESCRIPTOR (decl))
236 1264 : decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
237 52530 : if (POINTER_TYPE_P (TREE_TYPE (decl)))
238 2373 : decl = build_fold_indirect_ref_loc (input_location, decl);
239 52530 : vptr = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
240 : CLASS_VPTR_FIELD);
241 52530 : return fold_build3_loc (input_location, COMPONENT_REF,
242 52530 : TREE_TYPE (vptr), decl, vptr,
243 52530 : NULL_TREE);
244 : }
245 :
246 :
247 : tree
248 7546 : gfc_class_len_get (tree decl)
249 : {
250 7546 : tree len;
251 : /* For class arrays decl may be a temporary descriptor handle, the len is
252 : then available through the saved descriptor. */
253 5387 : if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
254 7843 : && GFC_DECL_SAVED_DESCRIPTOR (decl))
255 127 : decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
256 7546 : if (POINTER_TYPE_P (TREE_TYPE (decl)))
257 722 : decl = build_fold_indirect_ref_loc (input_location, decl);
258 7546 : len = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
259 : CLASS_LEN_FIELD);
260 7546 : return fold_build3_loc (input_location, COMPONENT_REF,
261 7546 : TREE_TYPE (len), decl, len,
262 7546 : NULL_TREE);
263 : }
264 :
265 :
266 : /* Try to get the _len component of a class. When the class is not unlimited
267 : poly, i.e. no _len field exists, then return a zero node. */
268 :
269 : static tree
270 9870 : gfc_class_len_or_zero_get (tree decl)
271 : {
272 9870 : tree len;
273 : /* For class arrays decl may be a temporary descriptor handle, the vptr is
274 : then available through the saved descriptor. */
275 5016 : if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
276 10074 : && GFC_DECL_SAVED_DESCRIPTOR (decl))
277 0 : decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
278 9870 : if (POINTER_TYPE_P (TREE_TYPE (decl)))
279 18 : decl = build_fold_indirect_ref_loc (input_location, decl);
280 9870 : len = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
281 : CLASS_LEN_FIELD);
282 12507 : return len != NULL_TREE ? fold_build3_loc (input_location, COMPONENT_REF,
283 2637 : TREE_TYPE (len), decl, len,
284 : NULL_TREE)
285 7233 : : build_zero_cst (gfc_charlen_type_node);
286 : }
287 :
288 :
289 : tree
290 9562 : gfc_resize_class_size_with_len (stmtblock_t * block, tree class_expr, tree size)
291 : {
292 9562 : tree tmp;
293 9562 : tree tmp2;
294 9562 : tree type;
295 :
296 9562 : tmp = gfc_class_len_or_zero_get (class_expr);
297 :
298 : /* Include the len value in the element size if present. */
299 9562 : if (!integer_zerop (tmp))
300 : {
301 2383 : type = TREE_TYPE (size);
302 2383 : if (block)
303 : {
304 1186 : size = gfc_evaluate_now (size, block);
305 1186 : tmp = gfc_evaluate_now (fold_convert (type , tmp), block);
306 : }
307 : else
308 1197 : tmp = fold_convert (type , tmp);
309 2383 : tmp2 = fold_build2_loc (input_location, MULT_EXPR,
310 : type, size, tmp);
311 2383 : tmp = fold_build2_loc (input_location, GT_EXPR,
312 : logical_type_node, tmp,
313 : build_zero_cst (type));
314 2383 : size = fold_build3_loc (input_location, COND_EXPR,
315 : type, tmp, tmp2, size);
316 : }
317 : else
318 : return size;
319 :
320 2383 : if (block)
321 1186 : size = gfc_evaluate_now (size, block);
322 :
323 : return size;
324 : }
325 :
326 :
327 : /* Get the specified FIELD from the VPTR. */
328 :
329 : static tree
330 25541 : vptr_field_get (tree vptr, int fieldno)
331 : {
332 25541 : tree field;
333 25541 : vptr = build_fold_indirect_ref_loc (input_location, vptr);
334 25541 : field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (vptr)),
335 : fieldno);
336 25541 : field = fold_build3_loc (input_location, COMPONENT_REF,
337 25541 : TREE_TYPE (field), vptr, field,
338 : NULL_TREE);
339 25541 : gcc_assert (field);
340 25541 : return field;
341 : }
342 :
343 :
344 : /* Get the field from the class' vptr. */
345 :
346 : static tree
347 12018 : class_vtab_field_get (tree decl, int fieldno)
348 : {
349 12018 : tree vptr;
350 12018 : vptr = gfc_class_vptr_get (decl);
351 12018 : return vptr_field_get (vptr, fieldno);
352 : }
353 :
354 :
355 : /* Define a macro for creating the class_vtab_* and vptr_* accessors in
356 : unison. */
357 : #define VTAB_GET_FIELD_GEN(name, field) tree \
358 : gfc_class_vtab_## name ##_get (tree cl) \
359 : { \
360 : return class_vtab_field_get (cl, field); \
361 : } \
362 : \
363 : tree \
364 : gfc_vptr_## name ##_get (tree vptr) \
365 : { \
366 : return vptr_field_get (vptr, field); \
367 : }
368 :
369 183 : VTAB_GET_FIELD_GEN (hash, VTABLE_HASH_FIELD)
370 0 : VTAB_GET_FIELD_GEN (extends, VTABLE_EXTENDS_FIELD)
371 0 : VTAB_GET_FIELD_GEN (def_init, VTABLE_DEF_INIT_FIELD)
372 5091 : VTAB_GET_FIELD_GEN (copy, VTABLE_COPY_FIELD)
373 2528 : VTAB_GET_FIELD_GEN (final, VTABLE_FINAL_FIELD)
374 1209 : VTAB_GET_FIELD_GEN (deallocate, VTABLE_DEALLOCATE_FIELD)
375 : #undef VTAB_GET_FIELD_GEN
376 :
377 : /* The size field is returned as an array index type. Therefore treat
378 : it and only it specially. */
379 :
380 : tree
381 9311 : gfc_class_vtab_size_get (tree cl)
382 : {
383 9311 : tree size;
384 9311 : size = class_vtab_field_get (cl, VTABLE_SIZE_FIELD);
385 : /* Always return size as an array index type. */
386 9311 : size = fold_convert (gfc_array_index_type, size);
387 9311 : gcc_assert (size);
388 9311 : return size;
389 : }
390 :
391 : tree
392 7219 : gfc_vptr_size_get (tree vptr)
393 : {
394 7219 : tree size;
395 7219 : size = vptr_field_get (vptr, VTABLE_SIZE_FIELD);
396 : /* Always return size as an array index type. */
397 7219 : size = fold_convert (gfc_array_index_type, size);
398 7219 : gcc_assert (size);
399 7219 : return size;
400 : }
401 :
402 :
403 : #undef CLASS_DATA_FIELD
404 : #undef CLASS_VPTR_FIELD
405 : #undef CLASS_LEN_FIELD
406 : #undef VTABLE_HASH_FIELD
407 : #undef VTABLE_SIZE_FIELD
408 : #undef VTABLE_EXTENDS_FIELD
409 : #undef VTABLE_DEF_INIT_FIELD
410 : #undef VTABLE_COPY_FIELD
411 : #undef VTABLE_FINAL_FIELD
412 :
413 :
414 : /* IF ts is null (default), search for the last _class ref in the chain
415 : of references of the expression and cut the chain there. Although
416 : this routine is similar to class.cc:gfc_add_component_ref (), there
417 : is a significant difference: gfc_add_component_ref () concentrates
418 : on an array ref that is the last ref in the chain and is oblivious
419 : to the kind of refs following.
420 : ELSE IF ts is non-null the cut is at the class entity or component
421 : that is followed by an array reference, which is not an element.
422 : These calls come from trans-array.cc:build_class_array_ref, which
423 : handles scalarized class array references.*/
424 :
425 : gfc_expr *
426 10690 : gfc_find_and_cut_at_last_class_ref (gfc_expr *e, bool is_mold,
427 : gfc_typespec **ts)
428 : {
429 10690 : gfc_expr *base_expr;
430 10690 : gfc_ref *ref, *class_ref, *tail = NULL, *array_ref;
431 :
432 : /* Find the last class reference. */
433 10690 : class_ref = NULL;
434 10690 : array_ref = NULL;
435 :
436 10690 : if (ts)
437 : {
438 494 : if (e->symtree
439 494 : && e->symtree->n.sym->ts.type == BT_CLASS)
440 494 : *ts = &e->symtree->n.sym->ts;
441 : else
442 0 : *ts = NULL;
443 : }
444 :
445 26774 : for (ref = e->ref; ref; ref = ref->next)
446 : {
447 16594 : if (ts)
448 : {
449 1236 : if (ref->type == REF_COMPONENT
450 586 : && ref->u.c.component->ts.type == BT_CLASS
451 0 : && ref->next && ref->next->type == REF_COMPONENT
452 0 : && !strcmp (ref->next->u.c.component->name, "_data")
453 0 : && ref->next->next
454 0 : && ref->next->next->type == REF_ARRAY
455 0 : && ref->next->next->u.ar.type != AR_ELEMENT)
456 : {
457 0 : *ts = &ref->u.c.component->ts;
458 0 : class_ref = ref;
459 0 : break;
460 : }
461 :
462 1236 : if (ref->next == NULL)
463 : break;
464 : }
465 : else
466 : {
467 15358 : if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
468 15358 : array_ref = ref;
469 :
470 15358 : if (ref->type == REF_COMPONENT
471 9270 : && ref->u.c.component->ts.type == BT_CLASS)
472 : {
473 : /* Component to the right of a part reference with nonzero
474 : rank must not have the ALLOCATABLE attribute. If attempts
475 : are made to reference such a component reference, an error
476 : results followed by an ICE. */
477 1823 : if (array_ref
478 16 : && CLASS_DATA (ref->u.c.component)->attr.allocatable)
479 : return NULL;
480 : class_ref = ref;
481 : }
482 : }
483 : }
484 :
485 10674 : if (ts && *ts == NULL)
486 : return NULL;
487 :
488 : /* Remove and store all subsequent references after the
489 : CLASS reference. */
490 10674 : if (class_ref)
491 : {
492 1603 : tail = class_ref->next;
493 1603 : class_ref->next = NULL;
494 : }
495 9071 : else if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
496 : {
497 9071 : tail = e->ref;
498 9071 : e->ref = NULL;
499 : }
500 :
501 10674 : if (is_mold)
502 61 : base_expr = gfc_expr_to_initialize (e);
503 : else
504 10613 : base_expr = gfc_copy_expr (e);
505 :
506 : /* Restore the original tail expression. */
507 10674 : if (class_ref)
508 : {
509 1603 : gfc_free_ref_list (class_ref->next);
510 1603 : class_ref->next = tail;
511 : }
512 9071 : else if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
513 : {
514 9071 : gfc_free_ref_list (e->ref);
515 9071 : e->ref = tail;
516 : }
517 : return base_expr;
518 : }
519 :
520 : /* Reset the vptr to the declared type, e.g. after deallocation.
521 : Use the variable in CLASS_CONTAINER if available. Otherwise, recreate
522 : one with e or class_type. At least one of the two has to be set. The
523 : generated assignment code is added at the end of BLOCK. */
524 :
525 : void
526 12525 : gfc_reset_vptr (stmtblock_t *block, gfc_expr *e, tree class_container,
527 : gfc_symbol *class_type)
528 : {
529 12525 : tree vptr = NULL_TREE;
530 :
531 12525 : if (class_container != NULL_TREE)
532 7466 : vptr = gfc_get_vptr_from_expr (class_container);
533 :
534 7466 : if (vptr == NULL_TREE)
535 : {
536 5066 : gfc_se se;
537 5066 : gcc_assert (e);
538 :
539 : /* Evaluate the expression and obtain the vptr from it. */
540 5066 : gfc_init_se (&se, NULL);
541 5066 : if (e->rank)
542 2517 : gfc_conv_expr_descriptor (&se, e);
543 : else
544 2549 : gfc_conv_expr (&se, e);
545 5066 : gfc_add_block_to_block (block, &se.pre);
546 :
547 5066 : vptr = gfc_get_vptr_from_expr (se.expr);
548 : }
549 :
550 : /* If a vptr is not found, we can do nothing more. */
551 5066 : if (vptr == NULL_TREE)
552 : return;
553 :
554 12509 : if (UNLIMITED_POLY (e)
555 11371 : || UNLIMITED_POLY (class_type)
556 : /* When the class_type's source is not a symbol (e.g. a component's ts),
557 : then look at the _data-components type. */
558 2013 : || (class_type != NULL && class_type->ts.type == BT_UNKNOWN
559 2013 : && class_type->components && class_type->components->ts.u.derived
560 2007 : && class_type->components->ts.u.derived->attr.unlimited_polymorphic))
561 1324 : gfc_add_modify (block, vptr, build_int_cst (TREE_TYPE (vptr), 0));
562 : else
563 : {
564 11185 : gfc_symbol *vtab, *type = nullptr;
565 11185 : tree vtable;
566 :
567 11185 : if (e)
568 9358 : type = e->ts.u.derived;
569 1827 : else if (class_type)
570 : {
571 1827 : if (class_type->ts.type == BT_CLASS)
572 0 : type = CLASS_DATA (class_type)->ts.u.derived;
573 : else
574 : type = class_type;
575 : }
576 9358 : gcc_assert (type);
577 : /* Return the vptr to the address of the declared type. */
578 11185 : vtab = gfc_find_derived_vtab (type);
579 11185 : vtable = vtab->backend_decl;
580 11185 : if (vtable == NULL_TREE)
581 106 : vtable = gfc_get_symbol_decl (vtab);
582 11185 : vtable = gfc_build_addr_expr (NULL, vtable);
583 11185 : vtable = fold_convert (TREE_TYPE (vptr), vtable);
584 11185 : gfc_add_modify (block, vptr, vtable);
585 : }
586 : }
587 :
588 : /* Set the vptr of a class in to from the type given in from. If from is NULL,
589 : then reset the vptr to the default or to. */
590 :
591 : void
592 234 : gfc_class_set_vptr (stmtblock_t *block, tree to, tree from)
593 : {
594 234 : tree tmp, vptr_ref;
595 234 : gfc_symbol *type;
596 :
597 234 : vptr_ref = gfc_get_vptr_from_expr (to);
598 276 : if (POINTER_TYPE_P (TREE_TYPE (from))
599 234 : && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (from))))
600 : {
601 44 : gfc_add_modify (block, vptr_ref,
602 22 : fold_convert (TREE_TYPE (vptr_ref),
603 : gfc_get_vptr_from_expr (from)));
604 256 : return;
605 : }
606 212 : tmp = gfc_get_vptr_from_expr (from);
607 212 : if (tmp)
608 : {
609 170 : gfc_add_modify (block, vptr_ref,
610 170 : fold_convert (TREE_TYPE (vptr_ref), tmp));
611 170 : return;
612 : }
613 42 : if (VAR_P (from)
614 42 : && strncmp (IDENTIFIER_POINTER (DECL_NAME (from)), "__vtab", 6) == 0)
615 : {
616 42 : gfc_add_modify (block, vptr_ref,
617 42 : gfc_build_addr_expr (TREE_TYPE (vptr_ref), from));
618 42 : return;
619 : }
620 0 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (from)))
621 0 : && GFC_CLASS_TYPE_P (
622 : TREE_TYPE (TREE_OPERAND (TREE_OPERAND (from, 0), 0))))
623 : {
624 0 : gfc_add_modify (block, vptr_ref,
625 0 : fold_convert (TREE_TYPE (vptr_ref),
626 : gfc_get_vptr_from_expr (TREE_OPERAND (
627 : TREE_OPERAND (from, 0), 0))));
628 0 : return;
629 : }
630 :
631 : /* If nothing of the above matches, set the vtype according to the type. */
632 0 : tmp = TREE_TYPE (from);
633 0 : if (POINTER_TYPE_P (tmp))
634 0 : tmp = TREE_TYPE (tmp);
635 0 : gfc_find_symbol (IDENTIFIER_POINTER (TYPE_NAME (tmp)), gfc_current_ns, 1,
636 : &type);
637 0 : tmp = gfc_find_derived_vtab (type)->backend_decl;
638 0 : gcc_assert (tmp);
639 0 : gfc_add_modify (block, vptr_ref,
640 0 : gfc_build_addr_expr (TREE_TYPE (vptr_ref), tmp));
641 : }
642 :
643 : /* Reset the len for unlimited polymorphic objects. */
644 :
645 : void
646 681 : gfc_reset_len (stmtblock_t *block, gfc_expr *expr)
647 : {
648 681 : gfc_expr *e;
649 681 : gfc_se se_len;
650 681 : e = gfc_find_and_cut_at_last_class_ref (expr);
651 681 : if (e == NULL)
652 : return;
653 681 : gfc_add_len_component (e);
654 681 : gfc_init_se (&se_len, NULL);
655 681 : gfc_conv_expr (&se_len, e);
656 681 : gfc_add_modify (block, se_len.expr,
657 681 : fold_convert (TREE_TYPE (se_len.expr), integer_zero_node));
658 681 : gfc_free_expr (e);
659 : }
660 :
661 :
662 : /* Obtain the last class reference in a gfc_expr. Return NULL_TREE if no class
663 : reference is found. Note that it is up to the caller to avoid using this
664 : for expressions other than variables. */
665 :
666 : tree
667 1721 : gfc_get_class_from_gfc_expr (gfc_expr *e)
668 : {
669 1721 : gfc_expr *class_expr;
670 1721 : gfc_se cse;
671 1721 : class_expr = gfc_find_and_cut_at_last_class_ref (e);
672 1721 : if (class_expr == NULL)
673 : return NULL_TREE;
674 1721 : gfc_init_se (&cse, NULL);
675 1721 : gfc_conv_expr (&cse, class_expr);
676 1721 : gfc_free_expr (class_expr);
677 1721 : return cse.expr;
678 : }
679 :
680 :
681 : /* Obtain the last class reference in an expression.
682 : Return NULL_TREE if no class reference is found. */
683 :
684 : tree
685 113618 : gfc_get_class_from_expr (tree expr)
686 : {
687 113618 : tree tmp;
688 113618 : tree type;
689 113618 : bool array_descr_found = false;
690 113618 : bool comp_after_descr_found = false;
691 :
692 292536 : for (tmp = expr; tmp; tmp = TREE_OPERAND (tmp, 0))
693 : {
694 292536 : if (CONSTANT_CLASS_P (tmp))
695 : return NULL_TREE;
696 :
697 292499 : type = TREE_TYPE (tmp);
698 339415 : while (type)
699 : {
700 331351 : if (GFC_CLASS_TYPE_P (type))
701 : return tmp;
702 309389 : if (GFC_DESCRIPTOR_TYPE_P (type))
703 37119 : array_descr_found = true;
704 309389 : if (type != TYPE_CANONICAL (type))
705 46916 : type = TYPE_CANONICAL (type);
706 : else
707 : type = NULL_TREE;
708 : }
709 270537 : if (VAR_P (tmp) || TREE_CODE (tmp) == PARM_DECL)
710 : break;
711 :
712 : /* Avoid walking up the reference chain too far. For class arrays, the
713 : array descriptor is a direct component (through a pointer) of the class
714 : container. So there is exactly one COMPONENT_REF between a class
715 : container and its child array descriptor. After seeing an array
716 : descriptor, we can give up on the second COMPONENT_REF we see, if no
717 : class container was found until that point. */
718 178918 : if (array_descr_found)
719 : {
720 8104 : if (comp_after_descr_found)
721 : {
722 12 : if (TREE_CODE (tmp) == COMPONENT_REF)
723 : return NULL_TREE;
724 : }
725 8092 : else if (TREE_CODE (tmp) == COMPONENT_REF)
726 8104 : comp_after_descr_found = true;
727 : }
728 : }
729 :
730 91619 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
731 61272 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
732 :
733 91619 : if (GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
734 20 : return tmp;
735 :
736 : return NULL_TREE;
737 : }
738 :
739 :
740 : /* Obtain the vptr of the last class reference in an expression.
741 : Return NULL_TREE if no class reference is found. */
742 :
743 : tree
744 13192 : gfc_get_vptr_from_expr (tree expr)
745 : {
746 13192 : tree tmp;
747 :
748 13192 : tmp = gfc_get_class_from_expr (expr);
749 :
750 13192 : if (tmp != NULL_TREE)
751 13115 : return gfc_class_vptr_get (tmp);
752 :
753 : return NULL_TREE;
754 : }
755 :
756 : void
757 2014 : gfc_class_array_data_assign (stmtblock_t *block, tree lhs_desc, tree rhs_desc,
758 : bool lhs_type)
759 : {
760 2014 : tree lhs_dim, rhs_dim, type;
761 :
762 2014 : gfc_conv_descriptor_data_set (block, lhs_desc,
763 : gfc_conv_descriptor_data_get (rhs_desc));
764 2014 : gfc_conv_descriptor_offset_set (block, lhs_desc,
765 : gfc_conv_descriptor_offset_get (rhs_desc));
766 :
767 2014 : gfc_conv_descriptor_dtype_set (block, lhs_desc,
768 : gfc_conv_descriptor_dtype_get (rhs_desc));
769 2014 : gfc_conv_descriptor_span_set (block, lhs_desc,
770 : gfc_conv_descriptor_span_get (rhs_desc));
771 :
772 : /* Assign the dimension as range-ref. */
773 2014 : lhs_dim = gfc_get_descriptor_dimension (lhs_desc);
774 2014 : rhs_dim = gfc_get_descriptor_dimension (rhs_desc);
775 :
776 2014 : type = lhs_type ? TREE_TYPE (lhs_dim) : TREE_TYPE (rhs_dim);
777 2014 : lhs_dim = build4_loc (input_location, ARRAY_RANGE_REF, type, lhs_dim,
778 : gfc_index_zero_node, NULL_TREE, NULL_TREE);
779 2014 : rhs_dim = build4_loc (input_location, ARRAY_RANGE_REF, type, rhs_dim,
780 : gfc_index_zero_node, NULL_TREE, NULL_TREE);
781 2014 : gfc_add_modify (block, lhs_dim, rhs_dim);
782 :
783 : /* The corank dimensions are not copied by the ARRAY_RANGE_REF. */
784 2014 : gfc_copy_coarray_desc_part (block, lhs_desc, rhs_desc);
785 2014 : }
786 :
787 : /* Takes a derived type expression and returns the address of a temporary
788 : class object of the 'declared' type. If opt_vptr_src is not NULL, this is
789 : used for the temporary class object.
790 : optional_alloc_ptr is false when the dummy is neither allocatable
791 : nor a pointer; that's only relevant for the optional handling.
792 : The optional argument 'derived_array' is used to preserve the parmse
793 : expression for deallocation of allocatable components. Assumed rank
794 : formal arguments made this necessary. */
795 : void
796 5447 : gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym,
797 : tree opt_vptr_src, bool optional,
798 : bool optional_alloc_ptr, const char *proc_name,
799 : tree *derived_array)
800 : {
801 5447 : tree cond_optional = NULL_TREE;
802 5447 : gfc_ss *ss;
803 5447 : tree ctree;
804 5447 : tree var;
805 5447 : tree tmp;
806 5447 : tree packed = NULL_TREE;
807 :
808 : /* The derived type needs to be converted to a temporary CLASS object. */
809 5447 : tmp = gfc_typenode_for_spec (&fsym->ts);
810 5447 : var = gfc_create_var (tmp, "class");
811 :
812 : /* Set the vptr. */
813 5447 : if (opt_vptr_src)
814 128 : gfc_class_set_vptr (&parmse->pre, var, opt_vptr_src);
815 : else
816 5319 : gfc_reset_vptr (&parmse->pre, e, var);
817 :
818 : /* Now set the data field. */
819 5447 : ctree = gfc_class_data_get (var);
820 :
821 5447 : if (flag_coarray == GFC_FCOARRAY_LIB && CLASS_DATA (fsym)->attr.codimension)
822 : {
823 4 : tree token;
824 4 : tmp = gfc_get_tree_for_caf_expr (e);
825 4 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
826 2 : tmp = build_fold_indirect_ref (tmp);
827 4 : gfc_get_caf_token_offset (parmse, &token, nullptr, tmp, NULL_TREE, e);
828 4 : gfc_conv_descriptor_token_set (&parmse->pre, ctree, token);
829 : }
830 :
831 5447 : if (optional)
832 576 : cond_optional = gfc_conv_expr_present (e->symtree->n.sym);
833 :
834 : /* Set the _len as early as possible. */
835 5447 : if (fsym->ts.u.derived->components->ts.type == BT_DERIVED
836 5447 : && fsym->ts.u.derived->components->ts.u.derived->attr
837 5447 : .unlimited_polymorphic)
838 : {
839 : /* Take care about initializing the _len component correctly. */
840 392 : tree len_tree = gfc_class_len_get (var);
841 392 : if (UNLIMITED_POLY (e))
842 : {
843 12 : gfc_expr *len;
844 12 : gfc_se se;
845 :
846 12 : len = gfc_find_and_cut_at_last_class_ref (e);
847 12 : gfc_add_len_component (len);
848 12 : gfc_init_se (&se, NULL);
849 12 : gfc_conv_expr (&se, len);
850 12 : if (optional)
851 0 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se.expr),
852 : cond_optional, se.expr,
853 0 : fold_convert (TREE_TYPE (se.expr),
854 : integer_zero_node));
855 : else
856 12 : tmp = se.expr;
857 12 : gfc_free_expr (len);
858 12 : }
859 : else
860 380 : tmp = integer_zero_node;
861 392 : gfc_add_modify (&parmse->pre, len_tree,
862 392 : fold_convert (TREE_TYPE (len_tree), tmp));
863 : }
864 :
865 5447 : if (parmse->expr && POINTER_TYPE_P (TREE_TYPE (parmse->expr)))
866 : {
867 : /* If there is a ready made pointer to a derived type, use it
868 : rather than evaluating the expression again. */
869 535 : tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
870 535 : gfc_add_modify (&parmse->pre, ctree, tmp);
871 : }
872 4912 : else if (parmse->ss && parmse->ss->info && parmse->ss->info->useflags)
873 : {
874 : /* For an array reference in an elemental procedure call we need
875 : to retain the ss to provide the scalarized array reference. */
876 445 : gfc_conv_expr_reference (parmse, e);
877 445 : tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
878 445 : if (optional)
879 0 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
880 : cond_optional, tmp,
881 0 : fold_convert (TREE_TYPE (tmp), null_pointer_node));
882 445 : gfc_add_modify (&parmse->pre, ctree, tmp);
883 : }
884 : else
885 : {
886 4467 : ss = gfc_walk_expr (e);
887 4467 : if (ss == gfc_ss_terminator)
888 : {
889 3176 : parmse->ss = NULL;
890 3176 : gfc_conv_expr_reference (parmse, e);
891 :
892 : /* Scalar to an assumed-rank array. */
893 3176 : if (fsym->ts.u.derived->components->as)
894 335 : gfc_set_descriptor_from_scalar (&parmse->pre, ctree,
895 : parmse->expr, cond_optional);
896 : else
897 : {
898 2841 : tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
899 2841 : if (optional)
900 132 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
901 : cond_optional, tmp,
902 132 : fold_convert (TREE_TYPE (tmp),
903 : null_pointer_node));
904 2841 : gfc_add_modify (&parmse->pre, ctree, tmp);
905 : }
906 : }
907 : else
908 : {
909 1291 : stmtblock_t block;
910 1291 : gfc_init_block (&block);
911 1291 : gfc_ref *ref;
912 1291 : int dim;
913 1291 : tree lbshift = NULL_TREE;
914 :
915 : /* Array refs with sections indicate, that a for a formal argument
916 : expecting contiguous repacking needs to be done. */
917 2437 : for (ref = e->ref; ref; ref = ref->next)
918 1296 : if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
919 : break;
920 1291 : if (IS_CLASS_ARRAY (fsym)
921 1171 : && (CLASS_DATA (fsym)->as->type == AS_EXPLICIT
922 913 : || CLASS_DATA (fsym)->as->type == AS_ASSUMED_SIZE)
923 354 : && (ref || e->rank != fsym->ts.u.derived->components->as->rank))
924 144 : fsym->attr.contiguous = 1;
925 :
926 : /* Detect any array references with vector subscripts. */
927 2581 : for (ref = e->ref; ref; ref = ref->next)
928 1296 : if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT
929 1248 : && ref->u.ar.type != AR_FULL)
930 : {
931 336 : for (dim = 0; dim < ref->u.ar.dimen; dim++)
932 192 : if (ref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
933 : break;
934 150 : if (dim < ref->u.ar.dimen)
935 : break;
936 : }
937 : /* Array references with vector subscripts and non-variable
938 : expressions need be converted to a one-based descriptor. */
939 1291 : if (ref || e->expr_type != EXPR_VARIABLE)
940 49 : lbshift = gfc_index_one_node;
941 :
942 1291 : parmse->expr = var;
943 1291 : gfc_conv_array_parameter (parmse, e, false, fsym, proc_name, nullptr,
944 : &lbshift, &packed);
945 :
946 1291 : if (derived_array && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (parmse->expr)))
947 : {
948 1195 : *derived_array
949 1195 : = gfc_create_var (TREE_TYPE (parmse->expr), "array");
950 1195 : if (e->rank == -1)
951 : {
952 : /* Assumed-rank actual: parmse->expr physically holds only
953 : dtype.rank dims; a full struct assign reads past the end.
954 : Copy field-by-field with a runtime-sized dim[] memcpy.
955 : PR fortran/60576. */
956 78 : tree rank, dim_field, dim_size, copy_size, dst_ptr, src_ptr;
957 :
958 78 : gfc_conv_descriptor_data_set
959 78 : (&block, *derived_array,
960 : gfc_conv_descriptor_data_get (parmse->expr));
961 78 : gfc_conv_descriptor_offset_set
962 78 : (&block, *derived_array,
963 : gfc_conv_descriptor_offset_get (parmse->expr));
964 78 : tree dtype_val = gfc_conv_descriptor_dtype_get (parmse->expr);
965 78 : gfc_conv_descriptor_dtype_set (&block, *derived_array,
966 : dtype_val);
967 78 : rank = gfc_conv_descriptor_rank_get (parmse->expr);
968 78 : rank = fold_convert (size_type_node, rank);
969 78 : dim_field = gfc_get_descriptor_dimension (parmse->expr);
970 78 : dim_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (dim_field)));
971 78 : copy_size = fold_build2_loc (input_location, MULT_EXPR,
972 : size_type_node, rank, dim_size);
973 78 : dst_ptr = gfc_build_addr_expr
974 78 : (pvoid_type_node, gfc_get_descriptor_dimension (*derived_array));
975 78 : src_ptr = gfc_build_addr_expr (pvoid_type_node, dim_field);
976 78 : gfc_add_expr_to_block (&block,
977 : build_call_expr_loc (input_location,
978 : builtin_decl_explicit (BUILT_IN_MEMCPY),
979 : 3, dst_ptr, src_ptr, copy_size));
980 : }
981 : else
982 1117 : gfc_add_modify (&block, *derived_array, parmse->expr);
983 : }
984 :
985 1291 : if (optional)
986 : {
987 348 : tmp = gfc_finish_block (&block);
988 :
989 348 : gfc_init_block (&block);
990 348 : gfc_init_absent_descriptor (&block, ctree);
991 348 : if (derived_array && *derived_array != NULL_TREE)
992 348 : gfc_init_absent_descriptor (&block, *derived_array);
993 :
994 348 : tmp = build3_v (COND_EXPR, cond_optional, tmp,
995 : gfc_finish_block (&block));
996 348 : gfc_add_expr_to_block (&parmse->pre, tmp);
997 : }
998 : else
999 943 : gfc_add_block_to_block (&parmse->pre, &block);
1000 : }
1001 : }
1002 :
1003 : /* Pass the address of the class object. */
1004 5447 : if (packed)
1005 : parmse->expr = packed;
1006 : else
1007 5351 : parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
1008 :
1009 5447 : if (optional && optional_alloc_ptr)
1010 84 : parmse->expr
1011 84 : = build3_loc (input_location, COND_EXPR, TREE_TYPE (parmse->expr),
1012 : cond_optional, parmse->expr,
1013 84 : fold_convert (TREE_TYPE (parmse->expr), null_pointer_node));
1014 5447 : }
1015 :
1016 : /* Create a new class container, which is required as scalar coarrays
1017 : have an array descriptor while normal scalars haven't. Optionally,
1018 : NULL pointer checks are added if the argument is OPTIONAL. */
1019 :
1020 : static void
1021 48 : class_scalar_coarray_to_class (gfc_se *parmse, gfc_expr *e,
1022 : gfc_typespec class_ts, bool optional)
1023 : {
1024 48 : tree var, ctree, tmp;
1025 48 : stmtblock_t block;
1026 48 : gfc_ref *ref;
1027 48 : gfc_ref *class_ref;
1028 :
1029 48 : gfc_init_block (&block);
1030 :
1031 48 : class_ref = NULL;
1032 144 : for (ref = e->ref; ref; ref = ref->next)
1033 : {
1034 96 : if (ref->type == REF_COMPONENT
1035 48 : && ref->u.c.component->ts.type == BT_CLASS)
1036 96 : class_ref = ref;
1037 : }
1038 :
1039 48 : if (class_ref == NULL
1040 48 : && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
1041 48 : tmp = e->symtree->n.sym->backend_decl;
1042 : else
1043 : {
1044 : /* Remove everything after the last class reference, convert the
1045 : expression and then recover its tailend once more. */
1046 0 : gfc_se tmpse;
1047 0 : ref = class_ref->next;
1048 0 : class_ref->next = NULL;
1049 0 : gfc_init_se (&tmpse, NULL);
1050 0 : gfc_conv_expr (&tmpse, e);
1051 0 : class_ref->next = ref;
1052 0 : tmp = tmpse.expr;
1053 : }
1054 :
1055 48 : var = gfc_typenode_for_spec (&class_ts);
1056 48 : var = gfc_create_var (var, "class");
1057 :
1058 48 : ctree = gfc_class_vptr_get (var);
1059 96 : gfc_add_modify (&block, ctree,
1060 48 : fold_convert (TREE_TYPE (ctree), gfc_class_vptr_get (tmp)));
1061 :
1062 48 : ctree = gfc_class_data_get (var);
1063 48 : tmp = gfc_conv_descriptor_data_get (
1064 48 : gfc_class_data_get (GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (tmp)))
1065 : ? tmp
1066 24 : : GFC_DECL_SAVED_DESCRIPTOR (tmp)));
1067 48 : gfc_add_modify (&block, ctree, fold_convert (TREE_TYPE (ctree), tmp));
1068 :
1069 : /* Pass the address of the class object. */
1070 48 : parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
1071 :
1072 48 : if (optional)
1073 : {
1074 48 : tree cond = gfc_conv_expr_present (e->symtree->n.sym);
1075 48 : tree tmp2;
1076 :
1077 48 : tmp = gfc_finish_block (&block);
1078 :
1079 48 : gfc_init_block (&block);
1080 48 : tmp2 = gfc_class_data_get (var);
1081 48 : gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
1082 : null_pointer_node));
1083 48 : tmp2 = gfc_finish_block (&block);
1084 :
1085 48 : tmp = build3_loc (input_location, COND_EXPR, void_type_node,
1086 : cond, tmp, tmp2);
1087 48 : gfc_add_expr_to_block (&parmse->pre, tmp);
1088 : }
1089 : else
1090 0 : gfc_add_block_to_block (&parmse->pre, &block);
1091 48 : }
1092 :
1093 :
1094 : /* Takes an intrinsic type expression and returns the address of a temporary
1095 : class object of the 'declared' type. */
1096 : void
1097 954 : gfc_conv_intrinsic_to_class (gfc_se *parmse, gfc_expr *e,
1098 : gfc_typespec class_ts)
1099 : {
1100 954 : gfc_symbol *vtab;
1101 954 : gfc_ss *ss;
1102 954 : tree ctree;
1103 954 : tree var;
1104 954 : tree tmp;
1105 954 : int dim;
1106 954 : bool unlimited_poly;
1107 :
1108 1908 : unlimited_poly = class_ts.type == BT_CLASS
1109 954 : && class_ts.u.derived->components->ts.type == BT_DERIVED
1110 954 : && class_ts.u.derived->components->ts.u.derived
1111 954 : ->attr.unlimited_polymorphic;
1112 :
1113 : /* The intrinsic type needs to be converted to a temporary
1114 : CLASS object. */
1115 954 : tmp = gfc_typenode_for_spec (&class_ts);
1116 954 : var = gfc_create_var (tmp, "class");
1117 :
1118 : /* Force a temporary for component or substring references. */
1119 954 : if (unlimited_poly
1120 954 : && class_ts.u.derived->components->attr.dimension
1121 671 : && !class_ts.u.derived->components->attr.allocatable
1122 671 : && !class_ts.u.derived->components->attr.class_pointer
1123 1625 : && is_subref_array (e))
1124 17 : parmse->force_tmp = 1;
1125 :
1126 : /* Set the vptr. */
1127 954 : ctree = gfc_class_vptr_get (var);
1128 :
1129 954 : vtab = gfc_find_vtab (&e->ts);
1130 954 : gcc_assert (vtab);
1131 954 : tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
1132 954 : gfc_add_modify (&parmse->pre, ctree,
1133 954 : fold_convert (TREE_TYPE (ctree), tmp));
1134 :
1135 : /* Now set the data field. */
1136 954 : ctree = gfc_class_data_get (var);
1137 954 : if (parmse->ss && parmse->ss->info->useflags)
1138 : {
1139 : /* For an array reference in an elemental procedure call we need
1140 : to retain the ss to provide the scalarized array reference. */
1141 36 : gfc_conv_expr_reference (parmse, e);
1142 36 : tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
1143 36 : gfc_add_modify (&parmse->pre, ctree, tmp);
1144 : }
1145 : else
1146 : {
1147 918 : ss = gfc_walk_expr (e);
1148 918 : if (ss == gfc_ss_terminator)
1149 : {
1150 271 : parmse->ss = NULL;
1151 271 : gfc_conv_expr_reference (parmse, e);
1152 271 : if (class_ts.u.derived->components->as
1153 24 : && class_ts.u.derived->components->as->type == AS_ASSUMED_RANK)
1154 : {
1155 24 : tmp = gfc_conv_scalar_to_descriptor (parmse, parmse->expr,
1156 : gfc_expr_attr (e));
1157 24 : tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
1158 24 : TREE_TYPE (ctree), tmp);
1159 : }
1160 : else
1161 247 : tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
1162 271 : gfc_add_modify (&parmse->pre, ctree, tmp);
1163 : }
1164 : else
1165 : {
1166 647 : parmse->ss = ss;
1167 647 : gfc_conv_expr_descriptor (parmse, e);
1168 :
1169 : /* Array references with vector subscripts and non-variable expressions
1170 : need be converted to a one-based descriptor. */
1171 647 : if (e->expr_type != EXPR_VARIABLE)
1172 : {
1173 416 : for (dim = 0; dim < e->rank; ++dim)
1174 217 : gfc_conv_shift_descriptor_lbound (&parmse->pre, parmse->expr,
1175 : dim, gfc_index_one_node);
1176 : }
1177 :
1178 647 : if (class_ts.u.derived->components->as->rank != e->rank)
1179 : {
1180 49 : tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
1181 49 : TREE_TYPE (ctree), parmse->expr);
1182 49 : gfc_add_modify (&parmse->pre, ctree, tmp);
1183 : }
1184 : else
1185 598 : gfc_add_modify (&parmse->pre, ctree, parmse->expr);
1186 : }
1187 : }
1188 :
1189 954 : gcc_assert (class_ts.type == BT_CLASS);
1190 954 : if (unlimited_poly)
1191 : {
1192 954 : ctree = gfc_class_len_get (var);
1193 : /* When the actual arg is a char array, then set the _len component of the
1194 : unlimited polymorphic entity to the length of the string. */
1195 954 : if (e->ts.type == BT_CHARACTER)
1196 : {
1197 : /* Start with parmse->string_length because this seems to be set to a
1198 : correct value more often. */
1199 187 : if (parmse->string_length)
1200 : tmp = parmse->string_length;
1201 : /* When the string_length is not yet set, then try the backend_decl of
1202 : the cl. */
1203 0 : else if (e->ts.u.cl->backend_decl)
1204 : tmp = e->ts.u.cl->backend_decl;
1205 : /* If both of the above approaches fail, then try to generate an
1206 : expression from the input, which is only feasible currently, when the
1207 : expression can be evaluated to a constant one. */
1208 : else
1209 : {
1210 : /* Try to simplify the expression. */
1211 0 : gfc_simplify_expr (e, 0);
1212 0 : if (e->expr_type == EXPR_CONSTANT && !e->ts.u.cl->resolved)
1213 : {
1214 : /* Amazingly all data is present to compute the length of a
1215 : constant string, but the expression is not yet there. */
1216 0 : e->ts.u.cl->length = gfc_get_constant_expr (BT_INTEGER,
1217 : gfc_charlen_int_kind,
1218 : &e->where);
1219 0 : mpz_set_ui (e->ts.u.cl->length->value.integer,
1220 0 : e->value.character.length);
1221 0 : gfc_conv_const_charlen (e->ts.u.cl);
1222 0 : e->ts.u.cl->resolved = 1;
1223 0 : tmp = e->ts.u.cl->backend_decl;
1224 : }
1225 : else
1226 : {
1227 0 : gfc_error ("Cannot compute the length of the char array "
1228 : "at %L.", &e->where);
1229 : }
1230 : }
1231 : }
1232 : else
1233 767 : tmp = integer_zero_node;
1234 :
1235 954 : gfc_add_modify (&parmse->pre, ctree, fold_convert (TREE_TYPE (ctree), tmp));
1236 : }
1237 :
1238 : /* Pass the address of the class object. */
1239 954 : parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
1240 954 : }
1241 :
1242 :
1243 : /* Takes a scalarized class array expression and returns the
1244 : address of a temporary scalar class object of the 'declared'
1245 : type.
1246 : OOP-TODO: This could be improved by adding code that branched on
1247 : the dynamic type being the same as the declared type. In this case
1248 : the original class expression can be passed directly.
1249 : optional_alloc_ptr is false when the dummy is neither allocatable
1250 : nor a pointer; that's relevant for the optional handling.
1251 : Set copyback to true if class container's _data and _vtab pointers
1252 : might get modified. */
1253 :
1254 : void
1255 3963 : gfc_conv_class_to_class (gfc_se *parmse, gfc_expr *e, gfc_typespec class_ts,
1256 : bool elemental, bool copyback, bool optional,
1257 : bool optional_alloc_ptr)
1258 : {
1259 3963 : tree ctree;
1260 3963 : tree var;
1261 3963 : tree tmp;
1262 3963 : tree vptr;
1263 3963 : tree cond = NULL_TREE;
1264 3963 : tree slen = NULL_TREE;
1265 3963 : gfc_ref *ref;
1266 3963 : gfc_ref *class_ref;
1267 3963 : stmtblock_t block;
1268 3963 : bool full_array = false;
1269 :
1270 : /* If this is the data field of a class temporary, the class expression
1271 : can be obtained and returned directly. */
1272 3963 : if (e->expr_type != EXPR_VARIABLE
1273 198 : && TREE_CODE (parmse->expr) == COMPONENT_REF
1274 54 : && !GFC_CLASS_TYPE_P (TREE_TYPE (parmse->expr))
1275 4017 : && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (parmse->expr, 0))))
1276 : {
1277 54 : parmse->expr = TREE_OPERAND (parmse->expr, 0);
1278 54 : if (!VAR_P (parmse->expr))
1279 0 : parmse->expr = gfc_evaluate_now (parmse->expr, &parmse->pre);
1280 54 : parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
1281 192 : return;
1282 : }
1283 :
1284 3909 : gfc_init_block (&block);
1285 :
1286 3909 : class_ref = NULL;
1287 7957 : for (ref = e->ref; ref; ref = ref->next)
1288 : {
1289 7539 : if (ref->type == REF_COMPONENT
1290 4057 : && ref->u.c.component->ts.type == BT_CLASS)
1291 7539 : class_ref = ref;
1292 :
1293 7539 : if (ref->next == NULL)
1294 : break;
1295 : }
1296 :
1297 3909 : if ((ref == NULL || class_ref == ref)
1298 530 : && !(gfc_is_class_array_function (e) && parmse->class_vptr != NULL_TREE)
1299 4421 : && (!class_ts.u.derived->components->as
1300 421 : || class_ts.u.derived->components->as->rank != -1))
1301 : return;
1302 :
1303 : /* Test for FULL_ARRAY. */
1304 3771 : if (e->rank == 0
1305 4188 : && ((gfc_expr_attr (e).codimension && gfc_expr_attr (e).dimension)
1306 579 : || (class_ts.u.derived->components->as
1307 427 : && class_ts.u.derived->components->as->type == AS_ASSUMED_RANK)))
1308 472 : full_array = true;
1309 : else
1310 3299 : gfc_is_class_array_ref (e, &full_array);
1311 :
1312 : /* The derived type needs to be converted to a temporary
1313 : CLASS object. */
1314 3771 : tmp = gfc_typenode_for_spec (&class_ts);
1315 3771 : var = gfc_create_var (tmp, "class");
1316 :
1317 : /* Set the data. */
1318 3771 : ctree = gfc_class_data_get (var);
1319 3771 : if (class_ts.u.derived->components->as
1320 3457 : && e->rank != class_ts.u.derived->components->as->rank)
1321 : {
1322 1050 : if (e->rank == 0)
1323 417 : gfc_set_descriptor_from_scalar_class (&block, ctree, parmse->expr);
1324 : else
1325 633 : gfc_class_array_data_assign (&block, ctree, parmse->expr, false);
1326 : }
1327 : else
1328 : {
1329 2721 : if (TREE_TYPE (parmse->expr) != TREE_TYPE (ctree))
1330 1566 : parmse->expr = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
1331 1566 : TREE_TYPE (ctree), parmse->expr);
1332 2721 : gfc_add_modify (&block, ctree, parmse->expr);
1333 : }
1334 :
1335 : /* Return the data component, except in the case of scalarized array
1336 : references, where nullification of the cannot occur and so there
1337 : is no need. */
1338 3771 : if (!elemental && full_array && copyback)
1339 : {
1340 789 : if (class_ts.u.derived->components->as
1341 789 : && e->rank != class_ts.u.derived->components->as->rank)
1342 : {
1343 270 : if (e->rank == 0)
1344 : {
1345 102 : tmp = gfc_class_data_get (parmse->expr);
1346 204 : gfc_add_modify (&parmse->post, tmp,
1347 102 : fold_convert (TREE_TYPE (tmp),
1348 : gfc_conv_descriptor_data_get (ctree)));
1349 : }
1350 : else
1351 168 : gfc_class_array_data_assign (&parmse->post, parmse->expr, ctree,
1352 : true);
1353 : }
1354 : else
1355 519 : gfc_add_modify (&parmse->post, parmse->expr, ctree);
1356 : }
1357 :
1358 : /* Set the vptr. */
1359 3771 : ctree = gfc_class_vptr_get (var);
1360 :
1361 : /* The vptr is the second field of the actual argument.
1362 : First we have to find the corresponding class reference. */
1363 :
1364 3771 : tmp = NULL_TREE;
1365 3771 : if (gfc_is_class_array_function (e)
1366 3771 : && parmse->class_vptr != NULL_TREE)
1367 : tmp = parmse->class_vptr;
1368 3753 : else if (class_ref == NULL
1369 3206 : && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
1370 : {
1371 3206 : tmp = e->symtree->n.sym->backend_decl;
1372 :
1373 3206 : if (TREE_CODE (tmp) == FUNCTION_DECL)
1374 6 : tmp = gfc_get_fake_result_decl (e->symtree->n.sym, 0);
1375 :
1376 3206 : if (DECL_LANG_SPECIFIC (tmp) && GFC_DECL_SAVED_DESCRIPTOR (tmp))
1377 399 : tmp = GFC_DECL_SAVED_DESCRIPTOR (tmp);
1378 :
1379 3206 : slen = build_zero_cst (size_type_node);
1380 : }
1381 547 : else if (parmse->class_container != NULL_TREE)
1382 : /* Don't redundantly evaluate the expression if the required information
1383 : is already available. */
1384 : tmp = parmse->class_container;
1385 : else
1386 : {
1387 : /* Remove everything after the last class reference, convert the
1388 : expression and then recover its tailend once more. */
1389 24 : gfc_se tmpse;
1390 24 : ref = class_ref->next;
1391 24 : class_ref->next = NULL;
1392 24 : gfc_init_se (&tmpse, NULL);
1393 24 : gfc_conv_expr (&tmpse, e);
1394 24 : class_ref->next = ref;
1395 24 : tmp = tmpse.expr;
1396 24 : slen = tmpse.string_length;
1397 : }
1398 :
1399 3771 : gcc_assert (tmp != NULL_TREE);
1400 :
1401 : /* Dereference if needs be. */
1402 3771 : if (TREE_CODE (TREE_TYPE (tmp)) == REFERENCE_TYPE)
1403 353 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
1404 :
1405 3771 : if (!(gfc_is_class_array_function (e) && parmse->class_vptr))
1406 3753 : vptr = gfc_class_vptr_get (tmp);
1407 : else
1408 : vptr = tmp;
1409 :
1410 3771 : gfc_add_modify (&block, ctree,
1411 3771 : fold_convert (TREE_TYPE (ctree), vptr));
1412 :
1413 : /* Return the vptr component, except in the case of scalarized array
1414 : references, where the dynamic type cannot change. */
1415 3771 : if (!elemental && full_array && copyback)
1416 789 : gfc_add_modify (&parmse->post, vptr,
1417 789 : fold_convert (TREE_TYPE (vptr), ctree));
1418 :
1419 : /* For unlimited polymorphic objects also set the _len component. */
1420 3771 : if (class_ts.type == BT_CLASS
1421 3771 : && class_ts.u.derived->components
1422 3771 : && class_ts.u.derived->components->ts.u
1423 3771 : .derived->attr.unlimited_polymorphic)
1424 : {
1425 1284 : ctree = gfc_class_len_get (var);
1426 1284 : if (UNLIMITED_POLY (e))
1427 1081 : tmp = gfc_class_len_get (tmp);
1428 203 : else if (e->ts.type == BT_CHARACTER)
1429 : {
1430 0 : gcc_assert (slen != NULL_TREE);
1431 : tmp = slen;
1432 : }
1433 : else
1434 203 : tmp = build_zero_cst (size_type_node);
1435 1284 : gfc_add_modify (&parmse->pre, ctree,
1436 1284 : fold_convert (TREE_TYPE (ctree), tmp));
1437 :
1438 : /* Return the len component, except in the case of scalarized array
1439 : references, where the dynamic type cannot change. */
1440 1284 : if (!elemental && full_array && copyback
1441 283 : && (UNLIMITED_POLY (e) || VAR_P (tmp)))
1442 283 : gfc_add_modify (&parmse->post, tmp,
1443 283 : fold_convert (TREE_TYPE (tmp), ctree));
1444 : }
1445 :
1446 3771 : if (optional)
1447 : {
1448 510 : tree tmp2;
1449 :
1450 510 : cond = gfc_conv_expr_present (e->symtree->n.sym);
1451 : /* parmse->pre may contain some preparatory instructions for the
1452 : temporary array descriptor. Those may only be executed when the
1453 : optional argument is set, therefore add parmse->pre's instructions
1454 : to block, which is later guarded by an if (optional_arg_given). */
1455 510 : gfc_add_block_to_block (&parmse->pre, &block);
1456 510 : block.head = parmse->pre.head;
1457 510 : parmse->pre.head = NULL_TREE;
1458 510 : tmp = gfc_finish_block (&block);
1459 :
1460 510 : if (optional_alloc_ptr)
1461 102 : tmp2 = build_empty_stmt (input_location);
1462 : else
1463 : {
1464 408 : gfc_init_block (&block);
1465 408 : gfc_conv_descriptor_data_set (&block, gfc_class_data_get (var),
1466 : null_pointer_node);
1467 408 : tmp2 = gfc_finish_block (&block);
1468 : }
1469 :
1470 510 : tmp = build3_loc (input_location, COND_EXPR, void_type_node,
1471 : cond, tmp, tmp2);
1472 510 : gfc_add_expr_to_block (&parmse->pre, tmp);
1473 :
1474 510 : if (!elemental && full_array && copyback)
1475 : {
1476 30 : tmp2 = build_empty_stmt (input_location);
1477 30 : tmp = gfc_finish_block (&parmse->post);
1478 30 : tmp = build3_loc (input_location, COND_EXPR, void_type_node,
1479 : cond, tmp, tmp2);
1480 30 : gfc_add_expr_to_block (&parmse->post, tmp);
1481 : }
1482 : }
1483 : else
1484 3261 : gfc_add_block_to_block (&parmse->pre, &block);
1485 :
1486 : /* Pass the address of the class object. */
1487 3771 : parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
1488 :
1489 3771 : if (optional && optional_alloc_ptr)
1490 204 : parmse->expr = build3_loc (input_location, COND_EXPR,
1491 102 : TREE_TYPE (parmse->expr),
1492 : cond, parmse->expr,
1493 102 : fold_convert (TREE_TYPE (parmse->expr),
1494 : null_pointer_node));
1495 : }
1496 :
1497 :
1498 : /* Given a class array declaration and an index, returns the address
1499 : of the referenced element. */
1500 :
1501 : static tree
1502 880 : gfc_get_class_array_ref (tree index, tree class_decl, tree data_comp,
1503 : bool unlimited)
1504 : {
1505 880 : tree data, size, tmp, ctmp, offset, ptr;
1506 :
1507 880 : data = data_comp != NULL_TREE ? data_comp :
1508 0 : gfc_class_data_get (class_decl);
1509 880 : size = gfc_class_vtab_size_get (class_decl);
1510 :
1511 880 : if (unlimited)
1512 : {
1513 244 : tmp = fold_convert (gfc_array_index_type,
1514 : gfc_class_len_get (class_decl));
1515 244 : ctmp = fold_build2_loc (input_location, MULT_EXPR,
1516 : gfc_array_index_type, size, tmp);
1517 244 : tmp = fold_build2_loc (input_location, GT_EXPR,
1518 : logical_type_node, tmp,
1519 244 : build_zero_cst (TREE_TYPE (tmp)));
1520 244 : size = fold_build3_loc (input_location, COND_EXPR,
1521 : gfc_array_index_type, tmp, ctmp, size);
1522 : }
1523 :
1524 880 : offset = fold_build2_loc (input_location, MULT_EXPR,
1525 : gfc_array_index_type,
1526 : index, size);
1527 :
1528 880 : data = gfc_conv_descriptor_data_get (data);
1529 880 : ptr = fold_convert (pvoid_type_node, data);
1530 880 : ptr = fold_build_pointer_plus_loc (input_location, ptr, offset);
1531 880 : return fold_convert (TREE_TYPE (data), ptr);
1532 : }
1533 :
1534 :
1535 : /* Copies one class expression to another, assuming that if either
1536 : 'to' or 'from' are arrays they are packed. Should 'from' be
1537 : NULL_TREE, the initialization expression for 'to' is used, assuming
1538 : that the _vptr is set. */
1539 :
1540 : tree
1541 986 : gfc_copy_class_to_class (tree from, tree to, tree nelems, bool unlimited)
1542 : {
1543 986 : tree fcn;
1544 986 : tree fcn_type;
1545 986 : tree from_data;
1546 986 : tree from_len;
1547 986 : tree to_data;
1548 986 : tree to_len;
1549 986 : tree to_ref;
1550 986 : tree from_ref;
1551 986 : vec<tree, va_gc> *args;
1552 986 : tree tmp;
1553 986 : tree stdcopy;
1554 986 : tree extcopy;
1555 986 : tree index;
1556 986 : bool is_from_desc = false, is_to_class = false;
1557 :
1558 986 : args = NULL;
1559 : /* To prevent warnings on uninitialized variables. */
1560 986 : from_len = to_len = NULL_TREE;
1561 :
1562 986 : if (from != NULL_TREE)
1563 986 : fcn = gfc_class_vtab_copy_get (from);
1564 : else
1565 0 : fcn = gfc_class_vtab_copy_get (to);
1566 :
1567 986 : fcn_type = TREE_TYPE (TREE_TYPE (fcn));
1568 :
1569 986 : if (from != NULL_TREE)
1570 : {
1571 986 : is_from_desc = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from));
1572 986 : if (is_from_desc)
1573 : {
1574 0 : from_data = from;
1575 0 : from = GFC_DECL_SAVED_DESCRIPTOR (from);
1576 : }
1577 : else
1578 : {
1579 : /* Check that from is a class. When the class is part of a coarray,
1580 : then from is a common pointer and is to be used as is. */
1581 1972 : tmp = POINTER_TYPE_P (TREE_TYPE (from))
1582 986 : ? build_fold_indirect_ref (from) : from;
1583 1972 : from_data =
1584 986 : (GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
1585 0 : || (DECL_P (tmp) && GFC_DECL_CLASS (tmp)))
1586 986 : ? gfc_class_data_get (from) : from;
1587 986 : is_from_desc = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data));
1588 : }
1589 : }
1590 : else
1591 0 : from_data = gfc_class_vtab_def_init_get (to);
1592 :
1593 986 : if (unlimited)
1594 : {
1595 218 : if (from != NULL_TREE && unlimited)
1596 218 : from_len = gfc_class_len_or_zero_get (from);
1597 : else
1598 0 : from_len = build_zero_cst (size_type_node);
1599 : }
1600 :
1601 986 : if (GFC_CLASS_TYPE_P (TREE_TYPE (to)))
1602 : {
1603 986 : is_to_class = true;
1604 986 : to_data = gfc_class_data_get (to);
1605 986 : if (unlimited)
1606 218 : to_len = gfc_class_len_get (to);
1607 : }
1608 : else
1609 : /* When to is a BT_DERIVED and not a BT_CLASS, then to_data == to. */
1610 0 : to_data = to;
1611 :
1612 986 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (to_data)))
1613 : {
1614 440 : stmtblock_t loopbody;
1615 440 : stmtblock_t body;
1616 440 : stmtblock_t ifbody;
1617 440 : gfc_loopinfo loop;
1618 :
1619 440 : gfc_init_block (&body);
1620 440 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
1621 : gfc_array_index_type, nelems,
1622 : gfc_index_one_node);
1623 440 : nelems = gfc_evaluate_now (tmp, &body);
1624 440 : index = gfc_create_var (gfc_array_index_type, "S");
1625 :
1626 440 : if (is_from_desc)
1627 : {
1628 440 : from_ref = gfc_get_class_array_ref (index, from, from_data,
1629 : unlimited);
1630 440 : vec_safe_push (args, from_ref);
1631 : }
1632 : else
1633 0 : vec_safe_push (args, from_data);
1634 :
1635 440 : if (is_to_class)
1636 440 : to_ref = gfc_get_class_array_ref (index, to, to_data, unlimited);
1637 : else
1638 : {
1639 0 : tmp = gfc_conv_array_data (to);
1640 0 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
1641 0 : to_ref = gfc_build_addr_expr (NULL_TREE,
1642 : gfc_build_array_ref (tmp, index, to));
1643 : }
1644 440 : vec_safe_push (args, to_ref);
1645 :
1646 : /* Add bounds check. */
1647 440 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) > 0 && is_from_desc)
1648 : {
1649 25 : const char *name = "<<unknown>>";
1650 25 : int dim, rank;
1651 :
1652 25 : if (DECL_P (to))
1653 0 : name = IDENTIFIER_POINTER (DECL_NAME (to));
1654 :
1655 25 : rank = GFC_TYPE_ARRAY_RANK (TREE_TYPE (from_data));
1656 55 : for (dim = 1; dim <= rank; dim++)
1657 : {
1658 30 : tree from_len, to_len, cond;
1659 30 : char *msg;
1660 :
1661 30 : from_len = gfc_conv_descriptor_size (from_data, dim);
1662 30 : from_len = fold_convert (long_integer_type_node, from_len);
1663 30 : to_len = gfc_conv_descriptor_size (to_data, dim);
1664 30 : to_len = fold_convert (long_integer_type_node, to_len);
1665 30 : msg = xasprintf ("Array bound mismatch for dimension %d "
1666 : "of array '%s' (%%ld/%%ld)",
1667 : dim, name);
1668 30 : cond = fold_build2_loc (input_location, NE_EXPR,
1669 : logical_type_node, from_len, to_len);
1670 30 : gfc_trans_runtime_check (true, false, cond, &body,
1671 : NULL, msg, to_len, from_len);
1672 30 : free (msg);
1673 : }
1674 : }
1675 :
1676 440 : tmp = build_call_vec (fcn_type, fcn, args);
1677 :
1678 : /* Build the body of the loop. */
1679 440 : gfc_init_block (&loopbody);
1680 440 : gfc_add_expr_to_block (&loopbody, tmp);
1681 :
1682 : /* Build the loop and return. */
1683 440 : gfc_init_loopinfo (&loop);
1684 440 : loop.dimen = 1;
1685 440 : loop.from[0] = gfc_index_zero_node;
1686 440 : loop.loopvar[0] = index;
1687 440 : loop.to[0] = nelems;
1688 440 : gfc_trans_scalarizing_loops (&loop, &loopbody);
1689 440 : gfc_init_block (&ifbody);
1690 440 : gfc_add_block_to_block (&ifbody, &loop.pre);
1691 440 : stdcopy = gfc_finish_block (&ifbody);
1692 : /* In initialization mode from_len is a constant zero. */
1693 440 : if (unlimited && !integer_zerop (from_len))
1694 : {
1695 122 : vec_safe_push (args, from_len);
1696 122 : vec_safe_push (args, to_len);
1697 122 : tmp = build_call_vec (fcn_type, fcn, args);
1698 : /* Build the body of the loop. */
1699 122 : gfc_init_block (&loopbody);
1700 122 : gfc_add_expr_to_block (&loopbody, tmp);
1701 :
1702 : /* Build the loop and return. */
1703 122 : gfc_init_loopinfo (&loop);
1704 122 : loop.dimen = 1;
1705 122 : loop.from[0] = gfc_index_zero_node;
1706 122 : loop.loopvar[0] = index;
1707 122 : loop.to[0] = nelems;
1708 122 : gfc_trans_scalarizing_loops (&loop, &loopbody);
1709 122 : gfc_init_block (&ifbody);
1710 122 : gfc_add_block_to_block (&ifbody, &loop.pre);
1711 122 : extcopy = gfc_finish_block (&ifbody);
1712 :
1713 122 : tmp = fold_build2_loc (input_location, GT_EXPR,
1714 : logical_type_node, from_len,
1715 122 : build_zero_cst (TREE_TYPE (from_len)));
1716 122 : tmp = fold_build3_loc (input_location, COND_EXPR,
1717 : void_type_node, tmp, extcopy, stdcopy);
1718 122 : gfc_add_expr_to_block (&body, tmp);
1719 122 : tmp = gfc_finish_block (&body);
1720 : }
1721 : else
1722 : {
1723 318 : gfc_add_expr_to_block (&body, stdcopy);
1724 318 : tmp = gfc_finish_block (&body);
1725 : }
1726 440 : gfc_cleanup_loop (&loop);
1727 : }
1728 : else
1729 : {
1730 546 : gcc_assert (!is_from_desc);
1731 546 : vec_safe_push (args, from_data);
1732 546 : vec_safe_push (args, to_data);
1733 546 : stdcopy = build_call_vec (fcn_type, fcn, args);
1734 :
1735 : /* In initialization mode from_len is a constant zero. */
1736 546 : if (unlimited && !integer_zerop (from_len))
1737 : {
1738 96 : vec_safe_push (args, from_len);
1739 96 : vec_safe_push (args, to_len);
1740 96 : extcopy = build_call_vec (fcn_type, unshare_expr (fcn), args);
1741 96 : tmp = fold_build2_loc (input_location, GT_EXPR,
1742 : logical_type_node, from_len,
1743 96 : build_zero_cst (TREE_TYPE (from_len)));
1744 96 : tmp = fold_build3_loc (input_location, COND_EXPR,
1745 : void_type_node, tmp, extcopy, stdcopy);
1746 : }
1747 : else
1748 : tmp = stdcopy;
1749 : }
1750 :
1751 : /* Only copy _def_init to to_data, when it is not a NULL-pointer. */
1752 986 : if (from == NULL_TREE)
1753 : {
1754 0 : tree cond;
1755 0 : cond = fold_build2_loc (input_location, NE_EXPR,
1756 : logical_type_node,
1757 : from_data, null_pointer_node);
1758 0 : tmp = fold_build3_loc (input_location, COND_EXPR,
1759 : void_type_node, cond,
1760 : tmp, build_empty_stmt (input_location));
1761 : }
1762 :
1763 986 : return tmp;
1764 : }
1765 :
1766 :
1767 : static tree
1768 112 : gfc_trans_class_array_init_assign (gfc_expr *rhs, gfc_expr *lhs, gfc_expr *obj)
1769 : {
1770 112 : gfc_actual_arglist *actual;
1771 112 : gfc_expr *ppc;
1772 112 : gfc_code *ppc_code;
1773 112 : tree res;
1774 :
1775 112 : actual = gfc_get_actual_arglist ();
1776 112 : actual->expr = gfc_copy_expr (rhs);
1777 112 : actual->next = gfc_get_actual_arglist ();
1778 112 : actual->next->expr = gfc_copy_expr (lhs);
1779 112 : ppc = gfc_copy_expr (obj);
1780 112 : gfc_add_vptr_component (ppc);
1781 112 : gfc_add_component_ref (ppc, "_copy");
1782 112 : ppc_code = gfc_get_code (EXEC_CALL);
1783 112 : ppc_code->resolved_sym = ppc->symtree->n.sym;
1784 : /* Although '_copy' is set to be elemental in class.cc, it is
1785 : not staying that way. Find out why, sometime.... */
1786 112 : ppc_code->resolved_sym->attr.elemental = 1;
1787 112 : ppc_code->ext.actual = actual;
1788 112 : ppc_code->expr1 = ppc;
1789 : /* Since '_copy' is elemental, the scalarizer will take care
1790 : of arrays in gfc_trans_call. */
1791 112 : res = gfc_trans_call (ppc_code, false, NULL, NULL, false);
1792 112 : gfc_free_statements (ppc_code);
1793 :
1794 112 : if (UNLIMITED_POLY(obj) || IS_CLASS_PDT (obj))
1795 : {
1796 : /* Check if rhs is non-NULL. */
1797 30 : gfc_se src;
1798 30 : gfc_init_se (&src, NULL);
1799 30 : gfc_conv_expr (&src, rhs);
1800 30 : src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
1801 30 : tree cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1802 30 : src.expr, fold_convert (TREE_TYPE (src.expr),
1803 : null_pointer_node));
1804 30 : res = build3_loc (input_location, COND_EXPR, TREE_TYPE (res), cond, res,
1805 : build_empty_stmt (input_location));
1806 : }
1807 :
1808 112 : return res;
1809 : }
1810 :
1811 : /* Special case for initializing a polymorphic dummy with INTENT(OUT).
1812 : A MEMCPY is needed to copy the full data from the default initializer
1813 : of the dynamic type. */
1814 :
1815 : tree
1816 509 : gfc_trans_class_init_assign (gfc_code *code)
1817 : {
1818 509 : stmtblock_t block;
1819 509 : tree tmp;
1820 509 : bool cmp_flag = true;
1821 509 : gfc_se dst,src,memsz;
1822 509 : gfc_expr *lhs, *rhs, *sz;
1823 509 : gfc_component *cmp;
1824 509 : gfc_symbol *sym;
1825 509 : gfc_ref *ref;
1826 :
1827 509 : gfc_start_block (&block);
1828 :
1829 509 : lhs = gfc_copy_expr (code->expr1);
1830 :
1831 509 : rhs = gfc_copy_expr (code->expr1);
1832 509 : gfc_add_vptr_component (rhs);
1833 :
1834 : /* Make sure that the component backend_decls have been built, which
1835 : will not have happened if the derived types concerned have not
1836 : been referenced. */
1837 509 : gfc_get_derived_type (rhs->ts.u.derived);
1838 509 : gfc_add_def_init_component (rhs);
1839 : /* The _def_init is always scalar. */
1840 509 : rhs->rank = 0;
1841 :
1842 : /* Check def_init for initializers. If this is an INTENT(OUT) dummy with all
1843 : default initializer components NULL, use the passed value even though
1844 : F2018(8.5.10) asserts that it should considered to be undefined. This is
1845 : needed for consistency with other brands. */
1846 509 : sym = code->expr1->expr_type == EXPR_VARIABLE ? code->expr1->symtree->n.sym
1847 : : NULL;
1848 509 : if (code->op != EXEC_ALLOCATE
1849 448 : && sym && sym->attr.dummy
1850 448 : && sym->attr.intent == INTENT_OUT)
1851 : {
1852 448 : ref = rhs->ref;
1853 896 : while (ref && ref->next)
1854 : ref = ref->next;
1855 448 : cmp = ref->u.c.component->ts.u.derived->components;
1856 683 : for (; cmp; cmp = cmp->next)
1857 : {
1858 476 : if (cmp->initializer)
1859 : break;
1860 235 : else if (!cmp->next)
1861 170 : cmp_flag = false;
1862 : }
1863 : }
1864 :
1865 509 : if (code->expr1->ts.type == BT_CLASS
1866 486 : && CLASS_DATA (code->expr1)->attr.dimension)
1867 : {
1868 112 : gfc_array_spec *tmparr = gfc_get_array_spec ();
1869 112 : *tmparr = *CLASS_DATA (code->expr1)->as;
1870 : /* Adding the array ref to the class expression results in correct
1871 : indexing to the dynamic type. */
1872 112 : gfc_add_full_array_ref (lhs, tmparr);
1873 112 : tmp = gfc_trans_class_array_init_assign (rhs, lhs, code->expr1);
1874 112 : }
1875 397 : else if (cmp_flag)
1876 : {
1877 : /* Scalar initialization needs the _data component. */
1878 240 : gfc_add_data_component (lhs);
1879 240 : sz = gfc_copy_expr (code->expr1);
1880 240 : gfc_add_vptr_component (sz);
1881 240 : gfc_add_size_component (sz);
1882 :
1883 240 : gfc_init_se (&dst, NULL);
1884 240 : gfc_init_se (&src, NULL);
1885 240 : gfc_init_se (&memsz, NULL);
1886 240 : gfc_conv_expr (&dst, lhs);
1887 240 : gfc_conv_expr (&src, rhs);
1888 240 : gfc_conv_expr (&memsz, sz);
1889 240 : gfc_add_block_to_block (&block, &src.pre);
1890 240 : src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
1891 :
1892 240 : tmp = gfc_build_memcpy_call (dst.expr, src.expr, memsz.expr);
1893 :
1894 240 : if (UNLIMITED_POLY(code->expr1))
1895 : {
1896 : /* Check if _def_init is non-NULL. */
1897 7 : tree cond = fold_build2_loc (input_location, NE_EXPR,
1898 : logical_type_node, src.expr,
1899 7 : fold_convert (TREE_TYPE (src.expr),
1900 : null_pointer_node));
1901 7 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
1902 : tmp, build_empty_stmt (input_location));
1903 : }
1904 : }
1905 : else
1906 157 : tmp = build_empty_stmt (input_location);
1907 :
1908 509 : if (code->expr1->symtree->n.sym->attr.dummy
1909 458 : && (code->expr1->symtree->n.sym->attr.optional
1910 452 : || code->expr1->symtree->n.sym->ns->proc_name->attr.entry_master))
1911 : {
1912 6 : tree present = gfc_conv_expr_present (code->expr1->symtree->n.sym);
1913 6 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
1914 : present, tmp,
1915 : build_empty_stmt (input_location));
1916 : }
1917 :
1918 509 : gfc_add_expr_to_block (&block, tmp);
1919 509 : gfc_free_expr (lhs);
1920 509 : gfc_free_expr (rhs);
1921 :
1922 509 : return gfc_finish_block (&block);
1923 : }
1924 :
1925 :
1926 : /* Class valued elemental function calls or class array elements arriving
1927 : in gfc_trans_scalar_assign come here. Wherever possible the vptr copy
1928 : is used to ensure that the rhs dynamic type is assigned to the lhs. */
1929 :
1930 : static bool
1931 779 : trans_scalar_class_assign (stmtblock_t *block, gfc_se *lse, gfc_se *rse)
1932 : {
1933 779 : tree fcn;
1934 779 : tree rse_expr;
1935 779 : tree class_data;
1936 779 : tree tmp;
1937 779 : tree zero;
1938 779 : tree cond;
1939 779 : tree final_cond;
1940 779 : stmtblock_t inner_block;
1941 779 : bool is_descriptor;
1942 779 : bool not_call_expr = TREE_CODE (rse->expr) != CALL_EXPR;
1943 779 : bool not_lhs_array_type;
1944 :
1945 : /* Temporaries arising from dependencies in assignment get cast as a
1946 : character type of the dynamic size of the rhs. Use the vptr copy
1947 : for this case. */
1948 779 : tmp = TREE_TYPE (lse->expr);
1949 779 : not_lhs_array_type = !(tmp && TREE_CODE (tmp) == ARRAY_TYPE
1950 0 : && TYPE_MAX_VALUE (TYPE_DOMAIN (tmp)) != NULL_TREE);
1951 :
1952 : /* Use ordinary assignment if the rhs is not a call expression or
1953 : the lhs is not a class entity or an array(ie. character) type. */
1954 743 : if ((not_call_expr && gfc_get_class_from_expr (lse->expr) == NULL_TREE)
1955 1048 : && not_lhs_array_type)
1956 : return false;
1957 :
1958 : /* Ordinary assignment can be used if both sides are class expressions
1959 : since the dynamic type is preserved by copying the vptr. This
1960 : should only occur, where temporaries are involved. */
1961 510 : if (GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
1962 510 : && GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
1963 : return false;
1964 :
1965 : /* Fix the class expression and the class data of the rhs. */
1966 510 : if (!GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr))
1967 510 : || not_call_expr)
1968 : {
1969 510 : if (rse->class_container != NULL_TREE)
1970 : tmp = rse->class_container;
1971 : else
1972 413 : tmp = gfc_get_class_from_expr (rse->expr);
1973 510 : if (tmp == NULL_TREE)
1974 : return false;
1975 182 : rse_expr = gfc_evaluate_now (tmp, block);
1976 : }
1977 : else
1978 0 : rse_expr = gfc_evaluate_now (rse->expr, block);
1979 :
1980 182 : class_data = gfc_class_data_get (rse_expr);
1981 :
1982 : /* Check that the rhs data is not null. */
1983 182 : is_descriptor = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (class_data));
1984 182 : if (is_descriptor)
1985 182 : class_data = gfc_conv_descriptor_data_get (class_data);
1986 182 : class_data = gfc_evaluate_now (class_data, block);
1987 :
1988 : /* Copy the rhs to the lhs. */
1989 182 : fcn = gfc_vptr_copy_get (gfc_class_vptr_get (rse_expr));
1990 182 : fcn = build_fold_indirect_ref_loc (input_location, fcn);
1991 182 : tmp = gfc_evaluate_now (gfc_build_addr_expr (NULL, rse->expr), block);
1992 182 : tmp = is_descriptor ? tmp : class_data;
1993 182 : tmp = build_call_expr_loc (input_location, fcn, 2, tmp,
1994 : gfc_build_addr_expr (NULL, lse->expr));
1995 182 : gfc_add_expr_to_block (block, tmp);
1996 :
1997 : /* Only elemental function results need to be finalised and freed. */
1998 182 : if (not_call_expr)
1999 : return true;
2000 :
2001 0 : zero = build_int_cst (TREE_TYPE (class_data), 0);
2002 0 : cond = fold_build2_loc (input_location, NE_EXPR,
2003 : logical_type_node,
2004 : class_data, zero);
2005 :
2006 : /* Finalize the class data if needed. */
2007 0 : gfc_init_block (&inner_block);
2008 0 : fcn = gfc_vptr_final_get (gfc_class_vptr_get (rse_expr));
2009 0 : zero = build_int_cst (TREE_TYPE (fcn), 0);
2010 0 : final_cond = fold_build2_loc (input_location, NE_EXPR,
2011 : logical_type_node, fcn, zero);
2012 0 : fcn = build_fold_indirect_ref_loc (input_location, fcn);
2013 0 : tmp = build_call_expr_loc (input_location, fcn, 1, class_data);
2014 0 : tmp = build3_v (COND_EXPR, final_cond,
2015 : tmp, build_empty_stmt (input_location));
2016 0 : gfc_add_expr_to_block (&inner_block, tmp);
2017 :
2018 : /* Free the class data. */
2019 0 : tmp = gfc_call_free (class_data);
2020 0 : tmp = build3_v (COND_EXPR, cond, tmp,
2021 : build_empty_stmt (input_location));
2022 0 : gfc_add_expr_to_block (&inner_block, tmp);
2023 :
2024 : /* Finish the inner block and subject it to the condition on the
2025 : class data being non-zero. */
2026 0 : tmp = gfc_finish_block (&inner_block);
2027 0 : tmp = build3_v (COND_EXPR, cond, tmp,
2028 : build_empty_stmt (input_location));
2029 0 : gfc_add_expr_to_block (block, tmp);
2030 :
2031 0 : return true;
2032 : }
2033 :
2034 : /* End of prototype trans-class.c */
2035 :
2036 :
2037 : static void
2038 13292 : realloc_lhs_warning (bt type, bool array, locus *where)
2039 : {
2040 13292 : if (array && type != BT_CLASS && type != BT_DERIVED && warn_realloc_lhs)
2041 25 : gfc_warning (OPT_Wrealloc_lhs,
2042 : "Code for reallocating the allocatable array at %L will "
2043 : "be added", where);
2044 13267 : else if (warn_realloc_lhs_all)
2045 4 : gfc_warning (OPT_Wrealloc_lhs_all,
2046 : "Code for reallocating the allocatable variable at %L "
2047 : "will be added", where);
2048 13292 : }
2049 :
2050 :
2051 : static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
2052 : gfc_expr *);
2053 :
2054 : /* Copy the scalarization loop variables. */
2055 :
2056 : static void
2057 1332704 : gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
2058 : {
2059 1332704 : dest->ss = src->ss;
2060 1332704 : dest->loop = src->loop;
2061 0 : }
2062 :
2063 :
2064 : /* Initialize a simple expression holder.
2065 :
2066 : Care must be taken when multiple se are created with the same parent.
2067 : The child se must be kept in sync. The easiest way is to delay creation
2068 : of a child se until after the previous se has been translated. */
2069 :
2070 : void
2071 4853666 : gfc_init_se (gfc_se * se, gfc_se * parent)
2072 : {
2073 4853666 : memset (se, 0, sizeof (gfc_se));
2074 4853666 : gfc_init_block (&se->pre);
2075 4853666 : gfc_init_block (&se->finalblock);
2076 4853666 : gfc_init_block (&se->post);
2077 :
2078 4853666 : se->parent = parent;
2079 :
2080 4853666 : if (parent)
2081 1332704 : gfc_copy_se_loopvars (se, parent);
2082 4853666 : }
2083 :
2084 :
2085 : /* Advances to the next SS in the chain. Use this rather than setting
2086 : se->ss = se->ss->next because all the parents needs to be kept in sync.
2087 : See gfc_init_se. */
2088 :
2089 : void
2090 249997 : gfc_advance_se_ss_chain (gfc_se * se)
2091 : {
2092 249997 : gfc_se *p;
2093 :
2094 249997 : gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
2095 :
2096 : p = se;
2097 : /* Walk down the parent chain. */
2098 655548 : while (p != NULL)
2099 : {
2100 : /* Simple consistency check. */
2101 405551 : gcc_assert (p->parent == NULL || p->parent->ss == p->ss
2102 : || p->parent->ss->nested_ss == p->ss);
2103 :
2104 405551 : p->ss = p->ss->next;
2105 :
2106 405551 : p = p->parent;
2107 : }
2108 249997 : }
2109 :
2110 :
2111 : /* Ensures the result of the expression as either a temporary variable
2112 : or a constant so that it can be used repeatedly. */
2113 :
2114 : void
2115 8452 : gfc_make_safe_expr (gfc_se * se)
2116 : {
2117 8452 : tree var;
2118 :
2119 8452 : if (CONSTANT_CLASS_P (se->expr))
2120 : return;
2121 :
2122 : /* We need a temporary for this result. */
2123 304 : var = gfc_create_var (TREE_TYPE (se->expr), NULL);
2124 304 : gfc_add_modify (&se->pre, var, se->expr);
2125 304 : se->expr = var;
2126 : }
2127 :
2128 :
2129 : /* Return an expression which determines if a dummy parameter is present.
2130 : Also used for arguments to procedures with multiple entry points. */
2131 :
2132 : tree
2133 11997 : gfc_conv_expr_present (gfc_symbol * sym, bool use_saved_desc)
2134 : {
2135 11997 : tree decl, orig_decl, cond;
2136 :
2137 11997 : gcc_assert (sym->attr.dummy);
2138 11997 : orig_decl = decl = gfc_get_symbol_decl (sym);
2139 :
2140 : /* Intrinsic scalars and derived types with VALUE attribute which are passed
2141 : by value use a hidden argument to denote the presence status. */
2142 11997 : if (sym->attr.value && !sym->attr.dimension && sym->ts.type != BT_CLASS)
2143 : {
2144 1100 : char name[GFC_MAX_SYMBOL_LEN + 2];
2145 1100 : tree tree_name;
2146 :
2147 1100 : gcc_assert (TREE_CODE (decl) == PARM_DECL);
2148 1100 : name[0] = '.';
2149 1100 : strcpy (&name[1], sym->name);
2150 1100 : tree_name = get_identifier (name);
2151 :
2152 : /* Walk function argument list to find hidden arg. */
2153 1100 : cond = DECL_ARGUMENTS (DECL_CONTEXT (decl));
2154 5464 : for ( ; cond != NULL_TREE; cond = TREE_CHAIN (cond))
2155 5464 : if (DECL_NAME (cond) == tree_name
2156 5464 : && DECL_ARTIFICIAL (cond))
2157 : break;
2158 :
2159 1100 : gcc_assert (cond);
2160 1100 : return cond;
2161 : }
2162 :
2163 : /* Assumed-shape arrays use a local variable for the array data;
2164 : the actual PARAM_DECL is in a saved decl. As the local variable
2165 : is NULL, it can be checked instead, unless use_saved_desc is
2166 : requested. */
2167 :
2168 10897 : if (use_saved_desc && TREE_CODE (decl) != PARM_DECL)
2169 : {
2170 888 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
2171 : || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
2172 888 : decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
2173 : }
2174 :
2175 10897 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, decl,
2176 10897 : fold_convert (TREE_TYPE (decl), null_pointer_node));
2177 :
2178 : /* Fortran 2008 allows to pass null pointers and non-associated pointers
2179 : as actual argument to denote absent dummies. For array descriptors,
2180 : we thus also need to check the array descriptor. For BT_CLASS, it
2181 : can also occur for scalars and F2003 due to type->class wrapping and
2182 : class->class wrapping. Note further that BT_CLASS always uses an
2183 : array descriptor for arrays, also for explicit-shape/assumed-size.
2184 : For assumed-rank arrays, no local variable is generated, hence,
2185 : the following also applies with !use_saved_desc. */
2186 :
2187 10897 : if ((use_saved_desc || TREE_CODE (orig_decl) == PARM_DECL)
2188 7742 : && !sym->attr.allocatable
2189 6512 : && ((sym->ts.type != BT_CLASS && !sym->attr.pointer)
2190 2302 : || (sym->ts.type == BT_CLASS
2191 1047 : && !CLASS_DATA (sym)->attr.allocatable
2192 573 : && !CLASS_DATA (sym)->attr.class_pointer))
2193 4423 : && ((gfc_option.allow_std & GFC_STD_F2008) != 0
2194 6 : || sym->ts.type == BT_CLASS))
2195 : {
2196 4417 : tree tmp;
2197 :
2198 4417 : if ((sym->as && (sym->as->type == AS_ASSUMED_SHAPE
2199 1546 : || sym->as->type == AS_ASSUMED_RANK
2200 1458 : || sym->attr.codimension))
2201 3489 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
2202 : {
2203 1105 : tmp = build_fold_indirect_ref_loc (input_location, decl);
2204 1105 : if (sym->ts.type == BT_CLASS)
2205 177 : tmp = gfc_class_data_get (tmp);
2206 1105 : tmp = gfc_conv_array_data (tmp);
2207 : }
2208 3312 : else if (sym->ts.type == BT_CLASS)
2209 36 : tmp = gfc_class_data_get (decl);
2210 : else
2211 : tmp = NULL_TREE;
2212 :
2213 1141 : if (tmp != NULL_TREE)
2214 : {
2215 1141 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
2216 1141 : fold_convert (TREE_TYPE (tmp), null_pointer_node));
2217 1141 : cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2218 : logical_type_node, cond, tmp);
2219 : }
2220 : }
2221 :
2222 : return cond;
2223 : }
2224 :
2225 :
2226 : /* Converts a missing, dummy argument into a null or zero. */
2227 :
2228 : void
2229 892 : gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
2230 : {
2231 892 : tree present;
2232 892 : tree tmp;
2233 :
2234 892 : present = gfc_conv_expr_present (arg->symtree->n.sym);
2235 :
2236 892 : if (kind > 0)
2237 : {
2238 : /* Create a temporary and convert it to the correct type. */
2239 54 : tmp = gfc_get_int_type (kind);
2240 54 : tmp = fold_convert (tmp, build_fold_indirect_ref_loc (input_location,
2241 : se->expr));
2242 :
2243 : /* Test for a NULL value. */
2244 54 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
2245 54 : tmp, fold_convert (TREE_TYPE (tmp), integer_one_node));
2246 54 : tmp = gfc_evaluate_now (tmp, &se->pre);
2247 54 : se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
2248 : }
2249 : else
2250 : {
2251 838 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
2252 : present, se->expr,
2253 838 : build_zero_cst (TREE_TYPE (se->expr)));
2254 838 : tmp = gfc_evaluate_now (tmp, &se->pre);
2255 838 : se->expr = tmp;
2256 : }
2257 :
2258 892 : if (ts.type == BT_CHARACTER)
2259 : {
2260 : /* Handle deferred-length dummies that pass the character length by
2261 : reference so that the value can be returned. */
2262 262 : if (ts.deferred && INDIRECT_REF_P (se->string_length))
2263 : {
2264 18 : tmp = gfc_build_addr_expr (NULL_TREE, se->string_length);
2265 18 : tmp = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
2266 : present, tmp, null_pointer_node);
2267 18 : tmp = gfc_evaluate_now (tmp, &se->pre);
2268 18 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
2269 : }
2270 : else
2271 : {
2272 244 : tmp = build_int_cst (gfc_charlen_type_node, 0);
2273 244 : tmp = fold_build3_loc (input_location, COND_EXPR,
2274 : gfc_charlen_type_node,
2275 : present, se->string_length, tmp);
2276 244 : tmp = gfc_evaluate_now (tmp, &se->pre);
2277 : }
2278 262 : se->string_length = tmp;
2279 : }
2280 892 : return;
2281 : }
2282 :
2283 :
2284 : /* Get the character length of an expression, looking through gfc_refs
2285 : if necessary. */
2286 :
2287 : tree
2288 20534 : gfc_get_expr_charlen (gfc_expr *e)
2289 : {
2290 20534 : gfc_ref *r;
2291 20534 : tree length = NULL_TREE;
2292 20534 : tree previous = NULL_TREE;
2293 20534 : gfc_se se;
2294 :
2295 20534 : gcc_assert (e->ts.type == BT_CHARACTER);
2296 :
2297 39735 : if ((is_subref_array (e) || e->expr_type == EXPR_FUNCTION)
2298 20534 : && e->ts.u.cl->length)
2299 : {
2300 839 : gfc_se tmpse;
2301 839 : gfc_init_se (&tmpse, NULL);
2302 839 : gfc_conv_expr_type (&tmpse, e->ts.u.cl->length, gfc_charlen_type_node);
2303 839 : e->ts.u.cl->backend_decl = tmpse.expr;
2304 839 : return tmpse.expr;
2305 : }
2306 :
2307 : /* First candidate: if the variable is of type CHARACTER, the
2308 : expression's length could be the length of the character
2309 : variable. */
2310 19695 : if (e->symtree->n.sym->ts.type == BT_CHARACTER)
2311 19383 : length = e->symtree->n.sym->ts.u.cl->backend_decl;
2312 :
2313 : /* Look through the reference chain for component references. */
2314 39416 : for (r = e->ref; r; r = r->next)
2315 : {
2316 19721 : previous = length;
2317 19721 : switch (r->type)
2318 : {
2319 312 : case REF_COMPONENT:
2320 312 : if (r->u.c.component->ts.type == BT_CHARACTER)
2321 312 : length = r->u.c.component->ts.u.cl->backend_decl;
2322 : break;
2323 :
2324 : case REF_ARRAY:
2325 : /* Do nothing. */
2326 : break;
2327 :
2328 20 : case REF_SUBSTRING:
2329 20 : gfc_init_se (&se, NULL);
2330 20 : gfc_conv_expr_type (&se, r->u.ss.start, gfc_charlen_type_node);
2331 20 : length = se.expr;
2332 20 : if (r->u.ss.end)
2333 0 : gfc_conv_expr_type (&se, r->u.ss.end, gfc_charlen_type_node);
2334 : else
2335 20 : se.expr = previous;
2336 20 : length = fold_build2_loc (input_location, MINUS_EXPR,
2337 : gfc_charlen_type_node,
2338 : se.expr, length);
2339 20 : length = fold_build2_loc (input_location, PLUS_EXPR,
2340 : gfc_charlen_type_node, length,
2341 : gfc_index_one_node);
2342 20 : break;
2343 :
2344 0 : default:
2345 0 : gcc_unreachable ();
2346 19721 : break;
2347 : }
2348 : }
2349 :
2350 19695 : gcc_assert (length != NULL);
2351 : return length;
2352 : }
2353 :
2354 :
2355 : /* Return for an expression the backend decl of the coarray. */
2356 :
2357 : tree
2358 2170 : gfc_get_tree_for_caf_expr (gfc_expr *expr)
2359 : {
2360 2170 : tree caf_decl;
2361 2170 : bool found = false;
2362 2170 : gfc_ref *ref;
2363 :
2364 2170 : gcc_assert (expr && expr->expr_type == EXPR_VARIABLE);
2365 :
2366 : /* Not-implemented diagnostic. */
2367 2170 : if (expr->symtree->n.sym->ts.type == BT_CLASS
2368 39 : && UNLIMITED_POLY (expr->symtree->n.sym)
2369 0 : && CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
2370 0 : gfc_error ("Sorry, coindexed access to an unlimited polymorphic object at "
2371 : "%L is not supported", &expr->where);
2372 :
2373 4601 : for (ref = expr->ref; ref; ref = ref->next)
2374 2431 : if (ref->type == REF_COMPONENT)
2375 : {
2376 225 : if (ref->u.c.component->ts.type == BT_CLASS
2377 0 : && UNLIMITED_POLY (ref->u.c.component)
2378 0 : && CLASS_DATA (ref->u.c.component)->attr.codimension)
2379 0 : gfc_error ("Sorry, coindexed access to an unlimited polymorphic "
2380 : "component at %L is not supported", &expr->where);
2381 : }
2382 :
2383 : /* Make sure the backend_decl is present before accessing it. */
2384 2170 : caf_decl = expr->symtree->n.sym->backend_decl == NULL_TREE
2385 2170 : ? gfc_get_symbol_decl (expr->symtree->n.sym)
2386 : : expr->symtree->n.sym->backend_decl;
2387 :
2388 2170 : if (expr->symtree->n.sym->ts.type == BT_CLASS)
2389 : {
2390 39 : if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
2391 45 : && GFC_DECL_SAVED_DESCRIPTOR (caf_decl))
2392 6 : caf_decl = GFC_DECL_SAVED_DESCRIPTOR (caf_decl);
2393 :
2394 39 : if (expr->ref && expr->ref->type == REF_ARRAY)
2395 : {
2396 28 : caf_decl = gfc_class_data_get (caf_decl);
2397 28 : if (CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
2398 : return caf_decl;
2399 : }
2400 11 : else if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
2401 2 : && GFC_DECL_TOKEN (caf_decl)
2402 13 : && CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
2403 : return caf_decl;
2404 :
2405 23 : for (ref = expr->ref; ref; ref = ref->next)
2406 : {
2407 18 : if (ref->type == REF_COMPONENT
2408 9 : && strcmp (ref->u.c.component->name, "_data") != 0)
2409 : {
2410 0 : caf_decl = gfc_class_data_get (caf_decl);
2411 0 : if (CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
2412 : return caf_decl;
2413 : break;
2414 : }
2415 18 : else if (ref->type == REF_ARRAY && ref->u.ar.dimen)
2416 : break;
2417 : }
2418 : }
2419 2140 : if (expr->symtree->n.sym->attr.codimension)
2420 : return caf_decl;
2421 :
2422 : /* The following code assumes that the coarray is a component reachable via
2423 : only scalar components/variables; the Fortran standard guarantees this. */
2424 :
2425 76 : for (ref = expr->ref; ref; ref = ref->next)
2426 76 : if (ref->type == REF_COMPONENT)
2427 : {
2428 76 : gfc_component *comp = ref->u.c.component;
2429 :
2430 76 : if (POINTER_TYPE_P (TREE_TYPE (caf_decl)))
2431 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
2432 76 : caf_decl = fold_build3_loc (input_location, COMPONENT_REF,
2433 76 : TREE_TYPE (comp->backend_decl), caf_decl,
2434 : comp->backend_decl, NULL_TREE);
2435 76 : if (comp->ts.type == BT_CLASS)
2436 : {
2437 0 : caf_decl = gfc_class_data_get (caf_decl);
2438 0 : if (CLASS_DATA (comp)->attr.codimension)
2439 : {
2440 : found = true;
2441 : break;
2442 : }
2443 : }
2444 76 : if (comp->attr.codimension)
2445 : {
2446 : found = true;
2447 : break;
2448 : }
2449 : }
2450 76 : gcc_assert (found && caf_decl);
2451 : return caf_decl;
2452 : }
2453 :
2454 :
2455 : /* Obtain the Coarray token - and optionally also the offset. */
2456 :
2457 : void
2458 2041 : gfc_get_caf_token_offset (gfc_se *se, tree *token, tree *offset, tree caf_decl,
2459 : tree se_expr, gfc_expr *expr)
2460 : {
2461 2041 : tree tmp;
2462 :
2463 2041 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
2464 :
2465 : /* Coarray token. */
2466 2041 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
2467 652 : *token = gfc_conv_descriptor_token (caf_decl);
2468 1387 : else if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
2469 1588 : && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
2470 6 : *token = GFC_DECL_TOKEN (caf_decl);
2471 : else
2472 : {
2473 1383 : gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (caf_decl))
2474 : && GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl)) != NULL_TREE);
2475 1383 : *token = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl));
2476 : }
2477 :
2478 2041 : if (offset == NULL)
2479 : return;
2480 :
2481 : /* Offset between the coarray base address and the address wanted. */
2482 187 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl))
2483 187 : && (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_ALLOCATABLE
2484 0 : || GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_POINTER))
2485 0 : *offset = build_int_cst (gfc_array_index_type, 0);
2486 187 : else if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
2487 187 : && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
2488 0 : *offset = GFC_DECL_CAF_OFFSET (caf_decl);
2489 187 : else if (GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl)) != NULL_TREE)
2490 0 : *offset = GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl));
2491 : else
2492 187 : *offset = build_int_cst (gfc_array_index_type, 0);
2493 :
2494 187 : if (POINTER_TYPE_P (TREE_TYPE (se_expr))
2495 187 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se_expr))))
2496 : {
2497 0 : tmp = build_fold_indirect_ref_loc (input_location, se_expr);
2498 0 : tmp = gfc_conv_descriptor_data_get (tmp);
2499 : }
2500 187 : else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se_expr)))
2501 0 : tmp = gfc_conv_descriptor_data_get (se_expr);
2502 : else
2503 : {
2504 187 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (se_expr)));
2505 : tmp = se_expr;
2506 : }
2507 :
2508 187 : *offset = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
2509 : *offset, fold_convert (gfc_array_index_type, tmp));
2510 :
2511 187 : if (expr->symtree->n.sym->ts.type == BT_DERIVED
2512 0 : && expr->symtree->n.sym->attr.codimension
2513 0 : && expr->symtree->n.sym->ts.u.derived->attr.alloc_comp)
2514 : {
2515 0 : gfc_expr *base_expr = gfc_copy_expr (expr);
2516 0 : gfc_ref *ref = base_expr->ref;
2517 0 : gfc_se base_se;
2518 :
2519 : // Iterate through the refs until the last one.
2520 0 : while (ref->next)
2521 : ref = ref->next;
2522 :
2523 0 : if (ref->type == REF_ARRAY
2524 0 : && ref->u.ar.type != AR_FULL)
2525 : {
2526 0 : const int ranksum = ref->u.ar.dimen + ref->u.ar.codimen;
2527 0 : int i;
2528 0 : for (i = 0; i < ranksum; ++i)
2529 : {
2530 0 : ref->u.ar.start[i] = NULL;
2531 0 : ref->u.ar.end[i] = NULL;
2532 : }
2533 0 : ref->u.ar.type = AR_FULL;
2534 : }
2535 0 : gfc_init_se (&base_se, NULL);
2536 0 : if (gfc_caf_attr (base_expr).dimension)
2537 : {
2538 0 : gfc_conv_expr_descriptor (&base_se, base_expr);
2539 0 : tmp = gfc_conv_descriptor_data_get (base_se.expr);
2540 : }
2541 : else
2542 : {
2543 0 : gfc_conv_expr (&base_se, base_expr);
2544 0 : tmp = base_se.expr;
2545 : }
2546 :
2547 0 : gfc_free_expr (base_expr);
2548 0 : gfc_add_block_to_block (&se->pre, &base_se.pre);
2549 0 : gfc_add_block_to_block (&se->post, &base_se.post);
2550 0 : }
2551 187 : else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
2552 0 : tmp = gfc_conv_descriptor_data_get (caf_decl);
2553 187 : else if (INDIRECT_REF_P (caf_decl))
2554 0 : tmp = TREE_OPERAND (caf_decl, 0);
2555 : else
2556 : {
2557 187 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (caf_decl)));
2558 : tmp = caf_decl;
2559 : }
2560 :
2561 187 : *offset = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2562 : fold_convert (gfc_array_index_type, *offset),
2563 : fold_convert (gfc_array_index_type, tmp));
2564 : }
2565 :
2566 :
2567 : /* Convert the coindex of a coarray into an image index; the result is
2568 : image_num = (idx(1)-lcobound(1)+1) + (idx(2)-lcobound(2))*extent(1)
2569 : + (idx(3)-lcobound(3))*extend(1)*extent(2) + ... */
2570 :
2571 : tree
2572 1748 : gfc_caf_get_image_index (stmtblock_t *block, gfc_expr *e, tree desc)
2573 : {
2574 1748 : gfc_ref *ref;
2575 1748 : tree lbound, ubound, extent, tmp, img_idx;
2576 1748 : gfc_se se;
2577 1748 : int i;
2578 :
2579 1809 : for (ref = e->ref; ref; ref = ref->next)
2580 1809 : if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
2581 : break;
2582 1748 : gcc_assert (ref != NULL);
2583 :
2584 1748 : if (ref->u.ar.dimen_type[ref->u.ar.dimen] == DIMEN_THIS_IMAGE)
2585 193 : return build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
2586 193 : null_pointer_node);
2587 :
2588 1555 : img_idx = build_zero_cst (gfc_array_index_type);
2589 1555 : extent = build_one_cst (gfc_array_index_type);
2590 1555 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
2591 642 : for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
2592 : {
2593 327 : gfc_init_se (&se, NULL);
2594 327 : gfc_conv_expr_type (&se, ref->u.ar.start[i], gfc_array_index_type);
2595 327 : gfc_add_block_to_block (block, &se.pre);
2596 327 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
2597 327 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2598 327 : TREE_TYPE (lbound), se.expr, lbound);
2599 327 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
2600 : extent, tmp);
2601 327 : img_idx = fold_build2_loc (input_location, PLUS_EXPR,
2602 327 : TREE_TYPE (tmp), img_idx, tmp);
2603 327 : if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
2604 : {
2605 12 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
2606 12 : tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
2607 12 : extent = fold_build2_loc (input_location, MULT_EXPR,
2608 12 : TREE_TYPE (tmp), extent, tmp);
2609 : }
2610 : }
2611 : else
2612 2496 : for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
2613 : {
2614 1256 : gfc_init_se (&se, NULL);
2615 1256 : gfc_conv_expr_type (&se, ref->u.ar.start[i], gfc_array_index_type);
2616 1256 : gfc_add_block_to_block (block, &se.pre);
2617 1256 : lbound = GFC_TYPE_ARRAY_LBOUND (TREE_TYPE (desc), i);
2618 1256 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2619 1256 : TREE_TYPE (lbound), se.expr, lbound);
2620 1256 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
2621 : extent, tmp);
2622 1256 : img_idx = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
2623 : img_idx, tmp);
2624 1256 : if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
2625 : {
2626 16 : ubound = GFC_TYPE_ARRAY_UBOUND (TREE_TYPE (desc), i);
2627 16 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2628 16 : TREE_TYPE (ubound), ubound, lbound);
2629 16 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
2630 16 : tmp, build_one_cst (TREE_TYPE (tmp)));
2631 16 : extent = fold_build2_loc (input_location, MULT_EXPR,
2632 16 : TREE_TYPE (tmp), extent, tmp);
2633 : }
2634 : }
2635 1555 : img_idx = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (img_idx),
2636 1555 : img_idx, build_one_cst (TREE_TYPE (img_idx)));
2637 1555 : return fold_convert (integer_type_node, img_idx);
2638 : }
2639 :
2640 :
2641 : /* For each character array constructor subexpression without a ts.u.cl->length,
2642 : replace it by its first element (if there aren't any elements, the length
2643 : should already be set to zero). */
2644 :
2645 : static void
2646 110 : flatten_array_ctors_without_strlen (gfc_expr* e)
2647 : {
2648 110 : gfc_actual_arglist* arg;
2649 110 : gfc_constructor* c;
2650 :
2651 110 : if (!e)
2652 : return;
2653 :
2654 110 : switch (e->expr_type)
2655 : {
2656 :
2657 0 : case EXPR_OP:
2658 0 : flatten_array_ctors_without_strlen (e->value.op.op1);
2659 0 : flatten_array_ctors_without_strlen (e->value.op.op2);
2660 0 : break;
2661 :
2662 0 : case EXPR_COMPCALL:
2663 : /* TODO: Implement as with EXPR_FUNCTION when needed. */
2664 0 : gcc_unreachable ();
2665 :
2666 13 : case EXPR_FUNCTION:
2667 40 : for (arg = e->value.function.actual; arg; arg = arg->next)
2668 27 : flatten_array_ctors_without_strlen (arg->expr);
2669 : break;
2670 :
2671 0 : case EXPR_ARRAY:
2672 :
2673 : /* We've found what we're looking for. */
2674 0 : if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
2675 : {
2676 0 : gfc_constructor *c;
2677 0 : gfc_expr* new_expr;
2678 :
2679 0 : gcc_assert (e->value.constructor);
2680 :
2681 0 : c = gfc_constructor_first (e->value.constructor);
2682 0 : new_expr = c->expr;
2683 0 : c->expr = NULL;
2684 :
2685 0 : flatten_array_ctors_without_strlen (new_expr);
2686 0 : gfc_replace_expr (e, new_expr);
2687 0 : break;
2688 : }
2689 :
2690 : /* Otherwise, fall through to handle constructor elements. */
2691 0 : gcc_fallthrough ();
2692 0 : case EXPR_STRUCTURE:
2693 0 : for (c = gfc_constructor_first (e->value.constructor);
2694 0 : c; c = gfc_constructor_next (c))
2695 0 : flatten_array_ctors_without_strlen (c->expr);
2696 : break;
2697 :
2698 : default:
2699 : break;
2700 :
2701 : }
2702 : }
2703 :
2704 :
2705 : /* Generate code to initialize a string length variable. Returns the
2706 : value. For array constructors, cl->length might be NULL and in this case,
2707 : the first element of the constructor is needed. expr is the original
2708 : expression so we can access it but can be NULL if this is not needed. */
2709 :
2710 : void
2711 3957 : gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
2712 : {
2713 3957 : gfc_se se;
2714 :
2715 3957 : gfc_init_se (&se, NULL);
2716 :
2717 3957 : if (!cl->length && cl->backend_decl && VAR_P (cl->backend_decl))
2718 1385 : return;
2719 :
2720 : /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
2721 : "flatten" array constructors by taking their first element; all elements
2722 : should be the same length or a cl->length should be present. */
2723 2665 : if (!cl->length)
2724 : {
2725 176 : gfc_expr* expr_flat;
2726 176 : if (!expr)
2727 : return;
2728 83 : expr_flat = gfc_copy_expr (expr);
2729 83 : flatten_array_ctors_without_strlen (expr_flat);
2730 83 : gfc_resolve_expr (expr_flat);
2731 83 : if (expr_flat->rank)
2732 13 : gfc_conv_expr_descriptor (&se, expr_flat);
2733 : else
2734 70 : gfc_conv_expr (&se, expr_flat);
2735 83 : if (expr_flat->expr_type != EXPR_VARIABLE)
2736 77 : gfc_add_block_to_block (pblock, &se.pre);
2737 83 : se.expr = convert (gfc_charlen_type_node, se.string_length);
2738 83 : gfc_add_block_to_block (pblock, &se.post);
2739 83 : gfc_free_expr (expr_flat);
2740 : }
2741 : else
2742 : {
2743 : /* Convert cl->length. */
2744 2489 : gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
2745 2489 : se.expr = fold_build2_loc (input_location, MAX_EXPR,
2746 : gfc_charlen_type_node, se.expr,
2747 2489 : build_zero_cst (TREE_TYPE (se.expr)));
2748 2489 : gfc_add_block_to_block (pblock, &se.pre);
2749 : }
2750 :
2751 2572 : if (cl->backend_decl && VAR_P (cl->backend_decl))
2752 1654 : gfc_add_modify (pblock, cl->backend_decl, se.expr);
2753 : else
2754 918 : cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
2755 : }
2756 :
2757 :
2758 : static void
2759 7864 : gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
2760 : const char *name, locus *where)
2761 : {
2762 7864 : tree tmp;
2763 7864 : tree type;
2764 7864 : tree fault;
2765 7864 : gfc_se start;
2766 7864 : gfc_se end;
2767 7864 : char *msg;
2768 7864 : mpz_t length;
2769 :
2770 7864 : type = gfc_get_character_type (kind, ref->u.ss.length);
2771 7864 : type = build_pointer_type (type);
2772 :
2773 7864 : gfc_init_se (&start, se);
2774 7864 : gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
2775 7864 : gfc_add_block_to_block (&se->pre, &start.pre);
2776 :
2777 7864 : if (integer_onep (start.expr))
2778 2916 : gfc_conv_string_parameter (se);
2779 : else
2780 : {
2781 4948 : tmp = start.expr;
2782 4948 : STRIP_NOPS (tmp);
2783 : /* Avoid multiple evaluation of substring start. */
2784 4948 : if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
2785 1591 : start.expr = gfc_evaluate_now (start.expr, &se->pre);
2786 :
2787 : /* Change the start of the string. */
2788 4948 : if (((TREE_CODE (TREE_TYPE (se->expr)) == ARRAY_TYPE
2789 1112 : || TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE)
2790 3956 : && TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
2791 5940 : || (POINTER_TYPE_P (TREE_TYPE (se->expr))
2792 992 : && TREE_CODE (TREE_TYPE (TREE_TYPE (se->expr))) != ARRAY_TYPE))
2793 : tmp = se->expr;
2794 : else
2795 984 : tmp = build_fold_indirect_ref_loc (input_location,
2796 : se->expr);
2797 : /* For BIND(C), a BT_CHARACTER is not an ARRAY_TYPE. */
2798 4948 : if (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE)
2799 : {
2800 4820 : tmp = gfc_build_array_ref (tmp, start.expr, NULL_TREE, true);
2801 4820 : se->expr = gfc_build_addr_expr (type, tmp);
2802 : }
2803 128 : else if (POINTER_TYPE_P (TREE_TYPE (tmp)))
2804 : {
2805 8 : tree diff;
2806 8 : diff = fold_build2 (MINUS_EXPR, gfc_charlen_type_node, start.expr,
2807 : build_one_cst (gfc_charlen_type_node));
2808 8 : diff = fold_convert (size_type_node, diff);
2809 8 : se->expr
2810 8 : = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (tmp), tmp, diff);
2811 : }
2812 : }
2813 :
2814 : /* Length = end + 1 - start. */
2815 7864 : gfc_init_se (&end, se);
2816 7864 : if (ref->u.ss.end == NULL)
2817 202 : end.expr = se->string_length;
2818 : else
2819 : {
2820 7662 : gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
2821 7662 : gfc_add_block_to_block (&se->pre, &end.pre);
2822 : }
2823 7864 : tmp = end.expr;
2824 7864 : STRIP_NOPS (tmp);
2825 7864 : if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
2826 2229 : end.expr = gfc_evaluate_now (end.expr, &se->pre);
2827 :
2828 7864 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2829 455 : && !gfc_contains_implied_index_p (ref->u.ss.start)
2830 8319 : && !gfc_contains_implied_index_p (ref->u.ss.end))
2831 : {
2832 455 : tree nonempty = fold_build2_loc (input_location, LE_EXPR,
2833 : logical_type_node, start.expr,
2834 : end.expr);
2835 :
2836 : /* Check lower bound. */
2837 455 : fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2838 : start.expr,
2839 455 : build_one_cst (TREE_TYPE (start.expr)));
2840 455 : fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2841 : logical_type_node, nonempty, fault);
2842 455 : if (name)
2843 454 : msg = xasprintf ("Substring out of bounds: lower bound (%%ld) of '%s' "
2844 : "is less than one", name);
2845 : else
2846 1 : msg = xasprintf ("Substring out of bounds: lower bound (%%ld) "
2847 : "is less than one");
2848 455 : gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
2849 : fold_convert (long_integer_type_node,
2850 : start.expr));
2851 455 : free (msg);
2852 :
2853 : /* Check upper bound. */
2854 455 : fault = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2855 : end.expr, se->string_length);
2856 455 : fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2857 : logical_type_node, nonempty, fault);
2858 455 : if (name)
2859 454 : msg = xasprintf ("Substring out of bounds: upper bound (%%ld) of '%s' "
2860 : "exceeds string length (%%ld)", name);
2861 : else
2862 1 : msg = xasprintf ("Substring out of bounds: upper bound (%%ld) "
2863 : "exceeds string length (%%ld)");
2864 455 : gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
2865 : fold_convert (long_integer_type_node, end.expr),
2866 : fold_convert (long_integer_type_node,
2867 : se->string_length));
2868 455 : free (msg);
2869 : }
2870 :
2871 : /* Try to calculate the length from the start and end expressions. */
2872 7864 : if (ref->u.ss.end
2873 7864 : && gfc_dep_difference (ref->u.ss.end, ref->u.ss.start, &length))
2874 : {
2875 6596 : HOST_WIDE_INT i_len;
2876 :
2877 6596 : i_len = gfc_mpz_get_hwi (length) + 1;
2878 6596 : if (i_len < 0)
2879 : i_len = 0;
2880 :
2881 6596 : tmp = build_int_cst (gfc_charlen_type_node, i_len);
2882 6596 : mpz_clear (length); /* Was initialized by gfc_dep_difference. */
2883 : }
2884 : else
2885 : {
2886 1268 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
2887 : fold_convert (gfc_charlen_type_node, end.expr),
2888 : fold_convert (gfc_charlen_type_node, start.expr));
2889 1268 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
2890 : build_int_cst (gfc_charlen_type_node, 1), tmp);
2891 1268 : tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
2892 : tmp, build_int_cst (gfc_charlen_type_node, 0));
2893 : }
2894 :
2895 7864 : se->string_length = tmp;
2896 7864 : }
2897 :
2898 :
2899 : /* Convert a derived type component reference. */
2900 :
2901 : void
2902 194207 : gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
2903 : {
2904 194207 : gfc_component *c;
2905 194207 : tree tmp;
2906 194207 : tree decl;
2907 194207 : tree field;
2908 194207 : tree context;
2909 :
2910 194207 : c = ref->u.c.component;
2911 :
2912 194207 : if (c->backend_decl == NULL_TREE
2913 6 : && ref->u.c.sym != NULL)
2914 6 : gfc_get_derived_type (ref->u.c.sym);
2915 :
2916 194207 : field = c->backend_decl;
2917 194207 : gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
2918 194207 : decl = se->expr;
2919 194207 : context = DECL_FIELD_CONTEXT (field);
2920 :
2921 : /* Components can correspond to fields of different containing
2922 : types, as components are created without context, whereas
2923 : a concrete use of a component has the type of decl as context.
2924 : So, if the type doesn't match, we search the corresponding
2925 : FIELD_DECL in the parent type. To not waste too much time
2926 : we cache this result in norestrict_decl.
2927 : On the other hand, if the context is a UNION or a MAP (a
2928 : RECORD_TYPE within a UNION_TYPE) always use the given FIELD_DECL. */
2929 :
2930 194207 : if (context != TREE_TYPE (decl)
2931 194207 : && !( TREE_CODE (TREE_TYPE (field)) == UNION_TYPE /* Field is union */
2932 14774 : || TREE_CODE (context) == UNION_TYPE)) /* Field is map */
2933 : {
2934 14774 : tree f2 = c->norestrict_decl;
2935 24695 : if (!f2 || DECL_FIELD_CONTEXT (f2) != TREE_TYPE (decl))
2936 9797 : for (f2 = TYPE_FIELDS (TREE_TYPE (decl)); f2; f2 = DECL_CHAIN (f2))
2937 9797 : if (TREE_CODE (f2) == FIELD_DECL
2938 9797 : && DECL_NAME (f2) == DECL_NAME (field))
2939 : break;
2940 14774 : gcc_assert (f2);
2941 14774 : c->norestrict_decl = f2;
2942 14774 : field = f2;
2943 : }
2944 :
2945 194207 : if (ref->u.c.sym && ref->u.c.sym->ts.type == BT_CLASS
2946 0 : && strcmp ("_data", c->name) == 0)
2947 : {
2948 : /* Found a ref to the _data component. Store the associated ref to
2949 : the vptr in se->class_vptr. */
2950 0 : se->class_vptr = gfc_class_vptr_get (decl);
2951 : }
2952 : else
2953 194207 : se->class_vptr = NULL_TREE;
2954 :
2955 194207 : tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
2956 : decl, field, NULL_TREE);
2957 :
2958 194207 : se->expr = tmp;
2959 :
2960 : /* Allocatable deferred char arrays are to be handled by the gfc_deferred_
2961 : strlen () conditional below. */
2962 194207 : if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
2963 9425 : && !c->ts.deferred
2964 6099 : && !c->attr.pdt_string)
2965 : {
2966 5665 : tmp = c->ts.u.cl->backend_decl;
2967 : /* Components must always be constant length. */
2968 5665 : gcc_assert (tmp && INTEGER_CST_P (tmp));
2969 5665 : se->string_length = tmp;
2970 : }
2971 :
2972 194207 : if (gfc_deferred_strlen (c, &field))
2973 : {
2974 3760 : tmp = fold_build3_loc (input_location, COMPONENT_REF,
2975 3760 : TREE_TYPE (field),
2976 : decl, field, NULL_TREE);
2977 3760 : se->string_length = tmp;
2978 : }
2979 :
2980 194207 : if (((c->attr.pointer || c->attr.allocatable)
2981 112764 : && (!c->attr.dimension && !c->attr.codimension)
2982 61048 : && c->ts.type != BT_CHARACTER)
2983 135514 : || c->attr.proc_pointer)
2984 65655 : se->expr = build_fold_indirect_ref_loc (input_location,
2985 : se->expr);
2986 194207 : }
2987 :
2988 :
2989 : /* This function deals with component references to components of the
2990 : parent type for derived type extensions. */
2991 : void
2992 71740 : conv_parent_component_references (gfc_se * se, gfc_ref * ref)
2993 : {
2994 71740 : gfc_component *c;
2995 71740 : gfc_component *cmp;
2996 71740 : gfc_symbol *dt;
2997 71740 : gfc_ref parent;
2998 :
2999 71740 : dt = ref->u.c.sym;
3000 71740 : c = ref->u.c.component;
3001 :
3002 : /* Return if the component is in this type, i.e. not in the parent type. */
3003 125411 : for (cmp = dt->components; cmp; cmp = cmp->next)
3004 113727 : if (c == cmp)
3005 60056 : return;
3006 :
3007 : /* Build a gfc_ref to recursively call gfc_conv_component_ref. */
3008 11684 : parent.type = REF_COMPONENT;
3009 11684 : parent.next = NULL;
3010 11684 : parent.u.c.sym = dt;
3011 11684 : parent.u.c.component = dt->components;
3012 :
3013 11684 : if (dt->backend_decl == NULL)
3014 0 : gfc_get_derived_type (dt);
3015 :
3016 : /* Build the reference and call self. */
3017 11684 : gfc_conv_component_ref (se, &parent);
3018 11684 : parent.u.c.sym = dt->components->ts.u.derived;
3019 11684 : parent.u.c.component = c;
3020 11684 : conv_parent_component_references (se, &parent);
3021 : }
3022 :
3023 :
3024 : static void
3025 639 : conv_inquiry (gfc_se * se, gfc_ref * ref, gfc_expr *expr, gfc_typespec *ts)
3026 : {
3027 639 : tree res = se->expr;
3028 :
3029 639 : switch (ref->u.i)
3030 : {
3031 265 : case INQUIRY_RE:
3032 530 : res = fold_build1_loc (input_location, REALPART_EXPR,
3033 265 : TREE_TYPE (TREE_TYPE (res)), res);
3034 265 : break;
3035 :
3036 245 : case INQUIRY_IM:
3037 490 : res = fold_build1_loc (input_location, IMAGPART_EXPR,
3038 245 : TREE_TYPE (TREE_TYPE (res)), res);
3039 245 : break;
3040 :
3041 7 : case INQUIRY_KIND:
3042 7 : res = build_int_cst (gfc_typenode_for_spec (&expr->ts),
3043 7 : ts->kind);
3044 7 : se->string_length = NULL_TREE;
3045 7 : break;
3046 :
3047 122 : case INQUIRY_LEN:
3048 122 : res = fold_convert (gfc_typenode_for_spec (&expr->ts),
3049 : se->string_length);
3050 122 : se->string_length = NULL_TREE;
3051 122 : break;
3052 :
3053 0 : default:
3054 0 : gcc_unreachable ();
3055 : }
3056 639 : se->expr = res;
3057 639 : }
3058 :
3059 : /* Dereference VAR where needed if it is a pointer, reference, etc.
3060 : according to Fortran semantics. */
3061 :
3062 : tree
3063 1529438 : gfc_maybe_dereference_var (gfc_symbol *sym, tree var, bool descriptor_only_p,
3064 : bool is_classarray)
3065 : {
3066 1529438 : if (!POINTER_TYPE_P (TREE_TYPE (var)))
3067 : return var;
3068 316847 : if (is_CFI_desc (sym, NULL))
3069 11892 : return build_fold_indirect_ref_loc (input_location, var);
3070 :
3071 : /* Characters are entirely different from other types, they are treated
3072 : separately. */
3073 304955 : if (sym->ts.type == BT_CHARACTER)
3074 : {
3075 : /* Dereference character pointer dummy arguments
3076 : or results. */
3077 33812 : if ((sym->attr.pointer || sym->attr.allocatable
3078 19582 : || (sym->as && sym->as->type == AS_ASSUMED_RANK))
3079 14566 : && (sym->attr.dummy
3080 11248 : || sym->attr.function
3081 10850 : || sym->attr.result))
3082 4443 : var = build_fold_indirect_ref_loc (input_location, var);
3083 : }
3084 271143 : else if (!sym->attr.value || gfc_value_dummy_passed_byref (sym))
3085 : {
3086 : /* Dereference temporaries for class array dummy arguments. */
3087 184666 : if (sym->attr.dummy && is_classarray
3088 278791 : && GFC_ARRAY_TYPE_P (TREE_TYPE (var)))
3089 : {
3090 5881 : if (!descriptor_only_p)
3091 3050 : var = GFC_DECL_SAVED_DESCRIPTOR (var);
3092 :
3093 5881 : var = build_fold_indirect_ref_loc (input_location, var);
3094 : }
3095 :
3096 : /* Dereference non-character scalar dummy arguments. */
3097 270337 : if (sym->attr.dummy && !sym->attr.dimension
3098 110526 : && !(sym->attr.codimension && sym->attr.allocatable)
3099 110453 : && (sym->ts.type != BT_CLASS
3100 21371 : || (!CLASS_DATA (sym)->attr.dimension
3101 12394 : && !(CLASS_DATA (sym)->attr.codimension
3102 284 : && CLASS_DATA (sym)->attr.allocatable))))
3103 101335 : var = build_fold_indirect_ref_loc (input_location, var);
3104 :
3105 : /* Dereference scalar hidden result. */
3106 270337 : if (flag_f2c && sym->ts.type == BT_COMPLEX
3107 292 : && (sym->attr.function || sym->attr.result)
3108 114 : && !sym->attr.dimension && !sym->attr.pointer
3109 60 : && !sym->attr.always_explicit)
3110 36 : var = build_fold_indirect_ref_loc (input_location, var);
3111 :
3112 : /* Dereference non-character, non-class pointer variables.
3113 : These must be dummies, results, or scalars. */
3114 270337 : if (!is_classarray
3115 261435 : && (sym->attr.pointer || sym->attr.allocatable
3116 208452 : || gfc_is_associate_pointer (sym)
3117 203163 : || (sym->as && sym->as->type == AS_ASSUMED_RANK))
3118 355771 : && (sym->attr.dummy
3119 39962 : || sym->attr.function
3120 39032 : || sym->attr.result
3121 37877 : || (!sym->attr.dimension
3122 37871 : && (!sym->attr.codimension || !sym->attr.allocatable))))
3123 85428 : var = build_fold_indirect_ref_loc (input_location, var);
3124 : /* Now treat the class array pointer variables accordingly. */
3125 184909 : else if (sym->ts.type == BT_CLASS
3126 21819 : && sym->attr.dummy
3127 21371 : && (CLASS_DATA (sym)->attr.dimension
3128 12394 : || CLASS_DATA (sym)->attr.codimension)
3129 9261 : && ((CLASS_DATA (sym)->as
3130 9261 : && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
3131 8156 : || CLASS_DATA (sym)->attr.allocatable
3132 6657 : || CLASS_DATA (sym)->attr.class_pointer))
3133 3237 : var = build_fold_indirect_ref_loc (input_location, var);
3134 : /* And the case where a non-dummy, non-result, non-function,
3135 : non-allocable and non-pointer classarray is present. This case was
3136 : previously covered by the first if, but with introducing the
3137 : condition !is_classarray there, that case has to be covered
3138 : explicitly. */
3139 181672 : else if (sym->ts.type == BT_CLASS
3140 18582 : && !sym->attr.dummy
3141 448 : && !sym->attr.function
3142 448 : && !sym->attr.result
3143 448 : && (CLASS_DATA (sym)->attr.dimension
3144 4 : || CLASS_DATA (sym)->attr.codimension)
3145 448 : && (sym->assoc
3146 0 : || !CLASS_DATA (sym)->attr.allocatable)
3147 448 : && !CLASS_DATA (sym)->attr.class_pointer)
3148 448 : var = build_fold_indirect_ref_loc (input_location, var);
3149 : }
3150 :
3151 : return var;
3152 : }
3153 :
3154 : /* Return the contents of a variable. Also handles reference/pointer
3155 : variables (all Fortran pointer references are implicit). */
3156 :
3157 : static void
3158 1687262 : gfc_conv_variable (gfc_se * se, gfc_expr * expr)
3159 : {
3160 1687262 : gfc_ss *ss;
3161 1687262 : gfc_ref *ref;
3162 1687262 : gfc_symbol *sym;
3163 1687262 : tree parent_decl = NULL_TREE;
3164 1687262 : int parent_flag;
3165 1687262 : bool return_value;
3166 1687262 : bool alternate_entry;
3167 1687262 : bool entry_master;
3168 1687262 : bool is_classarray;
3169 1687262 : bool first_time = true;
3170 :
3171 1687262 : sym = expr->symtree->n.sym;
3172 1687262 : is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
3173 1687262 : ss = se->ss;
3174 1687262 : if (ss != NULL)
3175 : {
3176 136275 : gfc_ss_info *ss_info = ss->info;
3177 :
3178 : /* Check that something hasn't gone horribly wrong. */
3179 136275 : gcc_assert (ss != gfc_ss_terminator);
3180 136275 : gcc_assert (ss_info->expr == expr);
3181 :
3182 : /* A scalarized term. We already know the descriptor. */
3183 136275 : se->expr = ss_info->data.array.descriptor;
3184 136275 : se->string_length = ss_info->string_length;
3185 136275 : ref = ss_info->data.array.ref;
3186 136275 : if (ref)
3187 135921 : gcc_assert (ref->type == REF_ARRAY
3188 : && ref->u.ar.type != AR_ELEMENT);
3189 : else
3190 354 : gfc_conv_tmp_array_ref (se);
3191 136275 : se->class_container = ss_info->class_container;
3192 : }
3193 : else
3194 : {
3195 1550987 : tree se_expr = NULL_TREE;
3196 :
3197 1550987 : se->expr = gfc_get_symbol_decl (sym);
3198 :
3199 : /* Deal with references to a parent results or entries by storing
3200 : the current_function_decl and moving to the parent_decl. */
3201 1550987 : return_value = sym->attr.function && sym->result == sym;
3202 20347 : alternate_entry = sym->attr.function && sym->attr.entry
3203 1552126 : && sym->result == sym;
3204 3101974 : entry_master = sym->attr.result
3205 16212 : && sym->ns->proc_name->attr.entry_master
3206 1551368 : && !gfc_return_by_reference (sym->ns->proc_name);
3207 1550987 : if (current_function_decl)
3208 1530701 : parent_decl = DECL_CONTEXT (current_function_decl);
3209 :
3210 1550987 : if ((se->expr == parent_decl && return_value)
3211 1550870 : || (sym->ns && sym->ns->proc_name
3212 1545197 : && parent_decl
3213 1524911 : && sym->ns->proc_name->backend_decl == parent_decl
3214 42910 : && (alternate_entry || entry_master)))
3215 : parent_flag = 1;
3216 : else
3217 1550837 : parent_flag = 0;
3218 :
3219 : /* Special case for assigning the return value of a function.
3220 : Self recursive functions must have an explicit return value. */
3221 1550987 : if (return_value && (se->expr == current_function_decl || parent_flag))
3222 10763 : se_expr = gfc_get_fake_result_decl (sym, parent_flag);
3223 :
3224 : /* Similarly for alternate entry points. */
3225 1540224 : else if (alternate_entry
3226 1106 : && (sym->ns->proc_name->backend_decl == current_function_decl
3227 0 : || parent_flag))
3228 : {
3229 1106 : gfc_entry_list *el = NULL;
3230 :
3231 1705 : for (el = sym->ns->entries; el; el = el->next)
3232 1705 : if (sym == el->sym)
3233 : {
3234 1106 : se_expr = gfc_get_fake_result_decl (sym, parent_flag);
3235 1106 : break;
3236 : }
3237 : }
3238 :
3239 1539118 : else if (entry_master
3240 295 : && (sym->ns->proc_name->backend_decl == current_function_decl
3241 0 : || parent_flag))
3242 295 : se_expr = gfc_get_fake_result_decl (sym, parent_flag);
3243 :
3244 12164 : if (se_expr)
3245 12164 : se->expr = se_expr;
3246 :
3247 : /* Procedure actual arguments. Look out for temporary variables
3248 : with the same attributes as function values. */
3249 1538823 : else if (!sym->attr.temporary
3250 1538623 : && sym->attr.flavor == FL_PROCEDURE
3251 23700 : && se->expr != current_function_decl)
3252 : {
3253 23627 : if (!sym->attr.dummy && !sym->attr.proc_pointer)
3254 : {
3255 21915 : gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
3256 21915 : se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
3257 : }
3258 : return;
3259 : }
3260 :
3261 1527360 : if (sym->ts.type == BT_CLASS
3262 80043 : && sym->attr.class_ok
3263 79801 : && sym->ts.u.derived->attr.is_class)
3264 : {
3265 30582 : if (is_classarray && DECL_LANG_SPECIFIC (se->expr)
3266 87966 : && GFC_DECL_SAVED_DESCRIPTOR (se->expr))
3267 6024 : se->class_container = GFC_DECL_SAVED_DESCRIPTOR (se->expr);
3268 : else
3269 73777 : se->class_container = se->expr;
3270 : }
3271 :
3272 : /* Dereference the expression, where needed. */
3273 1527360 : if (se->class_container && CLASS_DATA (sym)->attr.codimension
3274 2114 : && !CLASS_DATA (sym)->attr.dimension)
3275 911 : se->expr
3276 911 : = gfc_maybe_dereference_var (sym, se->class_container,
3277 911 : se->descriptor_only, is_classarray);
3278 : else
3279 1526449 : se->expr
3280 1526449 : = gfc_maybe_dereference_var (sym, se->expr, se->descriptor_only,
3281 : is_classarray);
3282 :
3283 1527360 : ref = expr->ref;
3284 : }
3285 :
3286 : /* For character variables, also get the length. */
3287 1663635 : if (sym->ts.type == BT_CHARACTER)
3288 : {
3289 : /* If the character length of an entry isn't set, get the length from
3290 : the master function instead. */
3291 169733 : if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
3292 0 : se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
3293 : else
3294 169733 : se->string_length = sym->ts.u.cl->backend_decl;
3295 169733 : gcc_assert (se->string_length);
3296 :
3297 : /* For coarray strings return the pointer to the data and not the
3298 : descriptor. */
3299 5143 : if (sym->attr.codimension && sym->attr.associate_var
3300 6 : && !se->descriptor_only
3301 169739 : && TREE_CODE (TREE_TYPE (se->expr)) != ARRAY_TYPE)
3302 6 : se->expr = gfc_conv_descriptor_data_get (se->expr);
3303 : }
3304 :
3305 : /* F202Y: Runtime warning that an assumed rank object is associated
3306 : with an assumed size object. */
3307 1663635 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
3308 91454 : && (gfc_option.allow_std & GFC_STD_F202Y)
3309 1663869 : && expr->rank == -1 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr)))
3310 : {
3311 60 : tree dim, lower, upper, cond;
3312 60 : char *msg;
3313 :
3314 60 : dim = fold_convert (gfc_array_dim_rank_type,
3315 : gfc_conv_descriptor_rank_get (se->expr));
3316 60 : dim = fold_build2_loc (input_location, MINUS_EXPR,
3317 : gfc_array_dim_rank_type, dim, gfc_rank_cst[1]);
3318 60 : lower = gfc_conv_descriptor_lbound_get (se->expr, dim);
3319 60 : upper = gfc_conv_descriptor_ubound_get (se->expr, dim);
3320 :
3321 60 : msg = xasprintf ("Assumed rank object %s is associated with an "
3322 : "assumed size object", sym->name);
3323 60 : cond = fold_build2_loc (input_location, LT_EXPR,
3324 : logical_type_node, upper, lower);
3325 60 : gfc_trans_runtime_check (false, true, cond, &se->pre,
3326 : &gfc_current_locus, msg);
3327 60 : free (msg);
3328 : }
3329 :
3330 1663635 : gfc_typespec *ts = &sym->ts;
3331 2127945 : while (ref)
3332 : {
3333 838102 : switch (ref->type)
3334 : {
3335 647322 : case REF_ARRAY:
3336 : /* Return the descriptor if that's what we want and this is an array
3337 : section reference. */
3338 647322 : 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 283439 : if (se->want_pointer
3343 25383 : && ref->next == NULL && (se->descriptor_only))
3344 : return;
3345 :
3346 273530 : gfc_conv_array_ref (se, &ref->u.ar, expr, &expr->where);
3347 : /* Return a pointer to an element. */
3348 273530 : break;
3349 :
3350 182535 : case REF_COMPONENT:
3351 182535 : ts = &ref->u.c.component->ts;
3352 182535 : if (first_time && IS_CLASS_ARRAY (sym) && sym->attr.dummy
3353 6419 : && se->descriptor_only && !CLASS_DATA (sym)->attr.allocatable
3354 3394 : && !CLASS_DATA (sym)->attr.class_pointer && CLASS_DATA (sym)->as
3355 3394 : && CLASS_DATA (sym)->as->type != AS_ASSUMED_RANK
3356 2831 : && 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 179704 : if (ref->u.c.sym->attr.extension)
3363 59965 : conv_parent_component_references (se, ref);
3364 :
3365 179704 : gfc_conv_component_ref (se, ref);
3366 :
3367 179704 : if (ref->u.c.component->ts.type == BT_CLASS
3368 13588 : && ref->u.c.component->attr.class_ok
3369 13588 : && ref->u.c.component->ts.u.derived->attr.is_class)
3370 13588 : se->class_container = se->expr;
3371 166116 : else if (!(ref->u.c.sym->attr.flavor == FL_DERIVED
3372 163622 : && ref->u.c.sym->attr.is_class))
3373 90622 : se->class_container = NULL_TREE;
3374 :
3375 179704 : if (!ref->next && ref->u.c.sym->attr.codimension
3376 0 : && se->want_pointer && se->descriptor_only)
3377 : return;
3378 :
3379 : break;
3380 :
3381 7606 : case REF_SUBSTRING:
3382 7606 : gfc_conv_substring (se, ref, expr->ts.kind,
3383 7606 : expr->symtree->name, &expr->where);
3384 7606 : break;
3385 :
3386 639 : case REF_INQUIRY:
3387 639 : conv_inquiry (se, ref, expr, ts);
3388 639 : break;
3389 :
3390 0 : default:
3391 0 : gcc_unreachable ();
3392 464310 : break;
3393 : }
3394 464310 : first_time = false;
3395 464310 : ref = ref->next;
3396 : }
3397 : /* Pointer assignment, allocation or pass by reference. Arrays are handled
3398 : separately. */
3399 1289843 : if (se->want_pointer)
3400 : {
3401 142079 : if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr))
3402 8274 : gfc_conv_string_parameter (se);
3403 : else
3404 133805 : 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 29116 : gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
3413 : {
3414 29116 : gfc_se operand;
3415 29116 : tree type;
3416 :
3417 29116 : gcc_assert (expr->ts.type != BT_CHARACTER);
3418 : /* Initialize the operand. */
3419 29116 : gfc_init_se (&operand, se);
3420 29116 : gfc_conv_expr_val (&operand, expr->value.op.op1);
3421 29116 : gfc_add_block_to_block (&se->pre, &operand.pre);
3422 :
3423 29116 : 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 29116 : if (code == TRUTH_NOT_EXPR)
3429 20459 : se->expr = fold_build2_loc (input_location, EQ_EXPR, type, operand.expr,
3430 : build_int_cst (type, 0));
3431 : else
3432 8657 : se->expr = fold_build1_loc (input_location, code, type, operand.expr);
3433 :
3434 29116 : }
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 178323 : gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
3493 : {
3494 178323 : tree op0;
3495 178323 : tree op1;
3496 178323 : tree tmp;
3497 178323 : int digit;
3498 :
3499 178323 : if (n < POWI_TABLE_SIZE)
3500 : {
3501 137336 : if (tmpvar[n])
3502 : return tmpvar[n];
3503 :
3504 56612 : op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
3505 56612 : op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
3506 : }
3507 40987 : else if (n & 1)
3508 : {
3509 10015 : digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
3510 10015 : op0 = gfc_conv_powi (se, n - digit, tmpvar);
3511 10015 : op1 = gfc_conv_powi (se, digit, tmpvar);
3512 : }
3513 : else
3514 : {
3515 30972 : op0 = gfc_conv_powi (se, n >> 1, tmpvar);
3516 30972 : op1 = op0;
3517 : }
3518 :
3519 97599 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
3520 97599 : tmp = gfc_evaluate_now (tmp, &se->pre);
3521 :
3522 97599 : if (n < POWI_TABLE_SIZE)
3523 56612 : 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 3309 : gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
3534 : {
3535 3309 : tree cond;
3536 3309 : tree tmp;
3537 3309 : tree type;
3538 3309 : tree vartmp[POWI_TABLE_SIZE];
3539 3309 : HOST_WIDE_INT m;
3540 3309 : unsigned HOST_WIDE_INT n;
3541 3309 : int sgn;
3542 3309 : 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 3309 : if (!wi::fits_shwi_p (wrhs))
3547 : return 0;
3548 :
3549 2949 : 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 2949 : n = wi::abs (wrhs).to_shwi ();
3553 :
3554 2949 : type = TREE_TYPE (lhs);
3555 2949 : sgn = tree_int_cst_sgn (rhs);
3556 :
3557 2949 : if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
3558 5898 : || optimize_size) && (m > 2 || m < -1))
3559 : return 0;
3560 :
3561 : /* rhs == 0 */
3562 1639 : if (sgn == 0)
3563 : {
3564 282 : se->expr = gfc_build_const (type, integer_one_node);
3565 282 : return 1;
3566 : }
3567 :
3568 : /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
3569 1357 : if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
3570 : {
3571 220 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3572 220 : lhs, build_int_cst (TREE_TYPE (lhs), -1));
3573 220 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3574 220 : lhs, build_int_cst (TREE_TYPE (lhs), 1));
3575 :
3576 : /* If rhs is even,
3577 : result = (lhs == 1 || lhs == -1) ? 1 : 0. */
3578 220 : if ((n & 1) == 0)
3579 : {
3580 104 : tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
3581 : logical_type_node, tmp, cond);
3582 104 : se->expr = fold_build3_loc (input_location, COND_EXPR, type,
3583 : tmp, build_int_cst (type, 1),
3584 : build_int_cst (type, 0));
3585 104 : return 1;
3586 : }
3587 : /* If rhs is odd,
3588 : result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
3589 116 : tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp,
3590 : build_int_cst (type, -1),
3591 : build_int_cst (type, 0));
3592 116 : se->expr = fold_build3_loc (input_location, COND_EXPR, type,
3593 : cond, build_int_cst (type, 1), tmp);
3594 116 : return 1;
3595 : }
3596 :
3597 1137 : memset (vartmp, 0, sizeof (vartmp));
3598 1137 : vartmp[1] = lhs;
3599 1137 : if (sgn == -1)
3600 : {
3601 141 : tmp = gfc_build_const (type, integer_one_node);
3602 141 : vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
3603 : vartmp[1]);
3604 : }
3605 :
3606 1137 : se->expr = gfc_conv_powi (se, n, vartmp);
3607 :
3608 1137 : 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 15120 : gfc_conv_cst_uint_power (gfc_se * se, tree lhs, tree rhs)
3631 : {
3632 15120 : tree type = TREE_TYPE (lhs);
3633 15120 : tree tmp, is_odd, odd_branch, even_branch;
3634 15120 : unsigned HOST_WIDE_INT lhs_prec, rhs_prec;
3635 15120 : wi::tree_to_wide_ref wrhs = wi::to_wide (rhs);
3636 15120 : unsigned HOST_WIDE_INT n, n_odd;
3637 15120 : tree vartmp_odd[POWI_TABLE_SIZE], vartmp_even[POWI_TABLE_SIZE];
3638 :
3639 : /* Anything ** 0 is one. */
3640 15120 : if (integer_zerop (rhs))
3641 : {
3642 1800 : se->expr = build_int_cst (type, 1);
3643 1800 : return 1;
3644 : }
3645 :
3646 13320 : if (!wi::fits_uhwi_p (wrhs))
3647 : return 0;
3648 :
3649 12960 : n = wrhs.to_uhwi ();
3650 :
3651 : /* tmp = a & 1; . */
3652 12960 : tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type,
3653 : lhs, build_int_cst (type, 1));
3654 12960 : is_odd = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3655 : tmp, build_int_cst (type, 1));
3656 :
3657 12960 : lhs_prec = TYPE_PRECISION (type);
3658 12960 : rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs));
3659 :
3660 12960 : if (rhs_prec >= lhs_prec && lhs_prec <= HOST_BITS_PER_WIDE_INT)
3661 : {
3662 7044 : unsigned HOST_WIDE_INT mask = (HOST_WIDE_INT_1U << (lhs_prec - 1)) - 1;
3663 7044 : n_odd = n & mask;
3664 : }
3665 : else
3666 : n_odd = n;
3667 :
3668 12960 : memset (vartmp_odd, 0, sizeof (vartmp_odd));
3669 12960 : vartmp_odd[0] = build_int_cst (type, 1);
3670 12960 : vartmp_odd[1] = lhs;
3671 12960 : odd_branch = gfc_conv_powi (se, n_odd, vartmp_odd);
3672 12960 : even_branch = NULL_TREE;
3673 :
3674 12960 : if (n > lhs_prec)
3675 4260 : even_branch = build_int_cst (type, 0);
3676 : else
3677 : {
3678 8700 : if (n_odd != n)
3679 : {
3680 0 : memset (vartmp_even, 0, sizeof (vartmp_even));
3681 0 : vartmp_even[0] = build_int_cst (type, 1);
3682 0 : vartmp_even[1] = lhs;
3683 0 : even_branch = gfc_conv_powi (se, n, vartmp_even);
3684 : }
3685 : }
3686 4260 : if (even_branch != NULL_TREE)
3687 4260 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, is_odd,
3688 : odd_branch, even_branch);
3689 : else
3690 8700 : 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 49188 : gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
3700 : {
3701 49188 : tree gfc_int4_type_node;
3702 49188 : int kind;
3703 49188 : int ikind;
3704 49188 : int res_ikind_1, res_ikind_2;
3705 49188 : gfc_se lse;
3706 49188 : gfc_se rse;
3707 49188 : tree fndecl = NULL;
3708 :
3709 49188 : gfc_init_se (&lse, se);
3710 49188 : gfc_conv_expr_val (&lse, expr->value.op.op1);
3711 49188 : lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
3712 49188 : gfc_add_block_to_block (&se->pre, &lse.pre);
3713 :
3714 49188 : gfc_init_se (&rse, se);
3715 49188 : gfc_conv_expr_val (&rse, expr->value.op.op2);
3716 49188 : gfc_add_block_to_block (&se->pre, &rse.pre);
3717 :
3718 49188 : if (expr->value.op.op2->expr_type == EXPR_CONSTANT)
3719 : {
3720 17565 : if (expr->value.op.op2->ts.type == BT_INTEGER)
3721 : {
3722 2294 : if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
3723 20486 : return;
3724 : }
3725 15271 : else if (expr->value.op.op2->ts.type == BT_UNSIGNED)
3726 : {
3727 15120 : if (gfc_conv_cst_uint_power (se, lse.expr, rse.expr))
3728 : return;
3729 : }
3730 : }
3731 :
3732 32789 : if ((expr->value.op.op2->ts.type == BT_INTEGER
3733 31468 : || expr->value.op.op2->ts.type == BT_UNSIGNED)
3734 31921 : && expr->value.op.op2->expr_type == EXPR_CONSTANT)
3735 1015 : if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
3736 : return;
3737 :
3738 32789 : if (INTEGER_CST_P (lse.expr)
3739 15380 : && TREE_CODE (TREE_TYPE (rse.expr)) == INTEGER_TYPE
3740 48169 : && expr->value.op.op2->ts.type == BT_INTEGER)
3741 : {
3742 260 : wi::tree_to_wide_ref wlhs = wi::to_wide (lse.expr);
3743 260 : HOST_WIDE_INT v;
3744 260 : unsigned HOST_WIDE_INT w;
3745 260 : int kind, ikind, bit_size;
3746 :
3747 260 : v = wlhs.to_shwi ();
3748 260 : w = absu_hwi (v);
3749 :
3750 260 : kind = expr->value.op.op1->ts.kind;
3751 260 : ikind = gfc_validate_kind (BT_INTEGER, kind, false);
3752 260 : bit_size = gfc_integer_kinds[ikind].bit_size;
3753 :
3754 260 : if (v == 1)
3755 : {
3756 : /* 1**something is always 1. */
3757 35 : se->expr = build_int_cst (TREE_TYPE (lse.expr), 1);
3758 248 : return;
3759 : }
3760 225 : else if (v == -1)
3761 : {
3762 : /* (-1)**n is 1 - ((n & 1) << 1) */
3763 34 : tree type;
3764 34 : tree tmp;
3765 :
3766 34 : type = TREE_TYPE (lse.expr);
3767 34 : tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type,
3768 : rse.expr, build_int_cst (type, 1));
3769 34 : tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3770 : tmp, build_int_cst (type, 1));
3771 34 : tmp = fold_build2_loc (input_location, MINUS_EXPR, type,
3772 : build_int_cst (type, 1), tmp);
3773 34 : se->expr = tmp;
3774 34 : return;
3775 : }
3776 191 : 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 179 : tree lshift;
3783 179 : tree type;
3784 179 : tree shift;
3785 179 : tree ge;
3786 179 : tree cond;
3787 179 : tree num_bits;
3788 179 : tree cond2;
3789 179 : tree tmp1;
3790 :
3791 179 : type = TREE_TYPE (lse.expr);
3792 :
3793 179 : if (w == 2)
3794 119 : shift = rse.expr;
3795 60 : else if (w == 4)
3796 12 : shift = fold_build2_loc (input_location, PLUS_EXPR,
3797 12 : TREE_TYPE (rse.expr),
3798 : rse.expr, rse.expr);
3799 : else
3800 : {
3801 : /* use popcount for fast log2(w) */
3802 48 : int e = wi::popcount (w-1);
3803 96 : shift = fold_build2_loc (input_location, MULT_EXPR,
3804 48 : TREE_TYPE (rse.expr),
3805 48 : build_int_cst (TREE_TYPE (rse.expr), e),
3806 : rse.expr);
3807 : }
3808 :
3809 179 : lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3810 : build_int_cst (type, 1), shift);
3811 179 : ge = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
3812 : rse.expr, build_int_cst (type, 0));
3813 179 : cond = fold_build3_loc (input_location, COND_EXPR, type, ge, lshift,
3814 : build_int_cst (type, 0));
3815 179 : num_bits = build_int_cst (TREE_TYPE (rse.expr), TYPE_PRECISION (type));
3816 179 : cond2 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
3817 : rse.expr, num_bits);
3818 179 : tmp1 = fold_build3_loc (input_location, COND_EXPR, type, cond2,
3819 : build_int_cst (type, 0), cond);
3820 179 : if (v > 0)
3821 : {
3822 : se->expr = tmp1;
3823 : }
3824 : else
3825 : {
3826 : /* for v < 0, calculate v**n = |v|**n * (-1)**n */
3827 42 : tree tmp2;
3828 42 : tmp2 = fold_build2_loc (input_location, BIT_AND_EXPR, type,
3829 : rse.expr, build_int_cst (type, 1));
3830 42 : tmp2 = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3831 : tmp2, build_int_cst (type, 1));
3832 42 : tmp2 = fold_build2_loc (input_location, MINUS_EXPR, type,
3833 : build_int_cst (type, 1), tmp2);
3834 42 : se->expr = fold_build2_loc (input_location, MULT_EXPR, type,
3835 : tmp1, tmp2);
3836 : }
3837 179 : return;
3838 : }
3839 : }
3840 : /* Handle unsigned separate from signed above, things would be too
3841 : complicated otherwise. */
3842 :
3843 32541 : if (INTEGER_CST_P (lse.expr) && expr->value.op.op1->ts.type == BT_UNSIGNED)
3844 : {
3845 15120 : gfc_expr * op1 = expr->value.op.op1;
3846 15120 : tree type;
3847 :
3848 15120 : type = TREE_TYPE (lse.expr);
3849 :
3850 15120 : if (mpz_cmp_ui (op1->value.integer, 1) == 0)
3851 : {
3852 : /* 1**something is always 1. */
3853 1260 : se->expr = build_int_cst (type, 1);
3854 1260 : return;
3855 : }
3856 :
3857 : /* Simplify 2u**x to a shift, with the value set to zero if it falls
3858 : outside the range. */
3859 26460 : if (mpz_popcount (op1->value.integer) == 1)
3860 : {
3861 2520 : tree prec_m1, lim, shift, lshift, cond, tmp;
3862 2520 : tree rtype = TREE_TYPE (rse.expr);
3863 2520 : int e = mpz_scan1 (op1->value.integer, 0);
3864 :
3865 2520 : shift = fold_build2_loc (input_location, MULT_EXPR,
3866 2520 : rtype, build_int_cst (rtype, e),
3867 : rse.expr);
3868 2520 : lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3869 : build_int_cst (type, 1), shift);
3870 5040 : prec_m1 = fold_build2_loc (input_location, MINUS_EXPR, rtype,
3871 2520 : build_int_cst (rtype, TYPE_PRECISION (type)),
3872 : build_int_cst (rtype, 1));
3873 2520 : lim = fold_build2_loc (input_location, TRUNC_DIV_EXPR, rtype,
3874 2520 : prec_m1, build_int_cst (rtype, e));
3875 2520 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3876 : rse.expr, lim);
3877 2520 : tmp = fold_build3_loc (input_location, COND_EXPR, type, cond,
3878 : build_int_cst (type, 0), lshift);
3879 2520 : se->expr = tmp;
3880 2520 : return;
3881 : }
3882 : }
3883 :
3884 28761 : 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 28761 : res_ikind_1 = -1;
3891 28761 : res_ikind_2 = -1;
3892 :
3893 28761 : kind = expr->value.op.op1->ts.kind;
3894 28761 : switch (expr->value.op.op2->ts.type)
3895 : {
3896 1073 : case BT_INTEGER:
3897 1073 : ikind = expr->value.op.op2->ts.kind;
3898 1073 : switch (ikind)
3899 : {
3900 168 : case 1:
3901 168 : case 2:
3902 168 : rse.expr = convert (gfc_int4_type_node, rse.expr);
3903 168 : res_ikind_2 = ikind;
3904 : /* Fall through. */
3905 :
3906 : case 4:
3907 : ikind = 0;
3908 : break;
3909 :
3910 182 : case 8:
3911 182 : ikind = 1;
3912 182 : break;
3913 :
3914 6 : case 16:
3915 6 : ikind = 2;
3916 6 : break;
3917 :
3918 0 : default:
3919 0 : gcc_unreachable ();
3920 : }
3921 1073 : switch (kind)
3922 : {
3923 0 : case 1:
3924 0 : case 2:
3925 0 : if (expr->value.op.op1->ts.type == BT_INTEGER)
3926 : {
3927 0 : lse.expr = convert (gfc_int4_type_node, lse.expr);
3928 0 : res_ikind_1 = kind;
3929 : }
3930 : else
3931 0 : gcc_unreachable ();
3932 : /* Fall through. */
3933 :
3934 : case 4:
3935 : kind = 0;
3936 : break;
3937 :
3938 212 : case 8:
3939 212 : kind = 1;
3940 212 : break;
3941 :
3942 6 : case 10:
3943 6 : kind = 2;
3944 6 : break;
3945 :
3946 18 : case 16:
3947 18 : kind = 3;
3948 18 : break;
3949 :
3950 0 : default:
3951 0 : gcc_unreachable ();
3952 : }
3953 :
3954 1073 : switch (expr->value.op.op1->ts.type)
3955 : {
3956 131 : case BT_INTEGER:
3957 131 : if (kind == 3) /* Case 16 was not handled properly above. */
3958 : kind = 2;
3959 131 : fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
3960 131 : break;
3961 :
3962 710 : case BT_REAL:
3963 : /* Use builtins for real ** int4. */
3964 :
3965 710 : if (real_minus_onep (lse.expr))
3966 : {
3967 : /* (-1.0)**n is (real) (1 - ((n & 1) << 1)), see the integer case
3968 : above. */
3969 :
3970 59 : tree lhs_type, rhs_type;
3971 59 : tree tmp;
3972 59 : lhs_type = TREE_TYPE (lse.expr);
3973 59 : rhs_type = TREE_TYPE (rse.expr);
3974 59 : tmp = fold_build2_loc (input_location, BIT_AND_EXPR, rhs_type,
3975 : rse.expr, build_int_cst (rhs_type, 1));
3976 59 : tmp = fold_build2_loc (input_location, LSHIFT_EXPR, rhs_type,
3977 : tmp, build_int_cst (rhs_type, 1));
3978 59 : tmp = fold_build2_loc (input_location, MINUS_EXPR, rhs_type,
3979 : build_int_cst (rhs_type, 1), tmp);
3980 59 : se->expr = fold_convert (lhs_type, tmp);
3981 59 : return;
3982 : }
3983 :
3984 651 : if (ikind == 0)
3985 : {
3986 555 : switch (kind)
3987 : {
3988 391 : case 0:
3989 391 : fndecl = builtin_decl_explicit (BUILT_IN_POWIF);
3990 391 : break;
3991 :
3992 146 : case 1:
3993 146 : fndecl = builtin_decl_explicit (BUILT_IN_POWI);
3994 146 : break;
3995 :
3996 6 : case 2:
3997 6 : fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
3998 6 : break;
3999 :
4000 12 : case 3:
4001 : /* Use the __builtin_powil() only if real(kind=16) is
4002 : actually the C long double type. */
4003 12 : if (!gfc_real16_is_float128)
4004 0 : fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
4005 : break;
4006 :
4007 : default:
4008 : gcc_unreachable ();
4009 : }
4010 : }
4011 :
4012 : /* If we don't have a good builtin for this, go for the
4013 : library function. */
4014 543 : if (!fndecl)
4015 108 : fndecl = gfor_fndecl_math_powi[kind][ikind].real;
4016 : break;
4017 :
4018 232 : case BT_COMPLEX:
4019 232 : fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
4020 232 : break;
4021 :
4022 0 : default:
4023 0 : gcc_unreachable ();
4024 : }
4025 : break;
4026 :
4027 139 : case BT_REAL:
4028 139 : fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
4029 139 : break;
4030 :
4031 729 : case BT_COMPLEX:
4032 729 : fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
4033 729 : break;
4034 :
4035 26820 : case BT_UNSIGNED:
4036 26820 : {
4037 : /* Valid kinds for unsigned are 1, 2, 4, 8, 16. Instead of using a
4038 : large switch statement, let's just use __builtin_ctz. */
4039 26820 : int base = __builtin_ctz (expr->value.op.op1->ts.kind);
4040 26820 : int expon = __builtin_ctz (expr->value.op.op2->ts.kind);
4041 26820 : fndecl = gfor_fndecl_unsigned_pow_list[base][expon];
4042 : }
4043 26820 : break;
4044 :
4045 0 : default:
4046 0 : gcc_unreachable ();
4047 28702 : break;
4048 : }
4049 :
4050 28702 : se->expr = build_call_expr_loc (input_location,
4051 : fndecl, 2, lse.expr, rse.expr);
4052 :
4053 : /* Convert the result back if it is of wrong integer kind. */
4054 28702 : if (res_ikind_1 != -1 && res_ikind_2 != -1)
4055 : {
4056 : /* We want the maximum of both operand kinds as result. */
4057 0 : if (res_ikind_1 < res_ikind_2)
4058 0 : res_ikind_1 = res_ikind_2;
4059 0 : se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr);
4060 : }
4061 : }
4062 :
4063 :
4064 : /* Generate code to allocate a string temporary. */
4065 :
4066 : tree
4067 5049 : gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
4068 : {
4069 5049 : tree var;
4070 5049 : tree tmp;
4071 :
4072 5049 : if (gfc_can_put_var_on_stack (len))
4073 : {
4074 : /* Create a temporary variable to hold the result. */
4075 4754 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
4076 2377 : TREE_TYPE (len), len,
4077 2377 : build_int_cst (TREE_TYPE (len), 1));
4078 2377 : tmp = build_range_type (gfc_charlen_type_node, size_zero_node, tmp);
4079 :
4080 2377 : if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
4081 2377 : tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
4082 : else
4083 0 : tmp = build_array_type (TREE_TYPE (type), tmp);
4084 :
4085 2377 : var = gfc_create_var (tmp, "str");
4086 2377 : var = gfc_build_addr_expr (type, var);
4087 : }
4088 : else
4089 : {
4090 : /* Allocate a temporary to hold the result. */
4091 2672 : var = gfc_create_var (type, "pstr");
4092 2672 : gcc_assert (POINTER_TYPE_P (type));
4093 2672 : tmp = TREE_TYPE (type);
4094 2672 : if (TREE_CODE (tmp) == ARRAY_TYPE)
4095 2672 : tmp = TREE_TYPE (tmp);
4096 2672 : tmp = TYPE_SIZE_UNIT (tmp);
4097 2672 : tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
4098 : fold_convert (size_type_node, len),
4099 : fold_convert (size_type_node, tmp));
4100 2672 : tmp = gfc_call_malloc (&se->pre, type, tmp);
4101 2672 : gfc_add_modify (&se->pre, var, tmp);
4102 :
4103 : /* Free the temporary afterwards. */
4104 2672 : tmp = gfc_call_free (var);
4105 2672 : gfc_add_expr_to_block (&se->post, tmp);
4106 : }
4107 :
4108 5049 : return var;
4109 : }
4110 :
4111 :
4112 : /* Handle a string concatenation operation. A temporary will be allocated to
4113 : hold the result. */
4114 :
4115 : static void
4116 1396 : gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
4117 : {
4118 1396 : gfc_se lse, rse;
4119 1396 : tree len, type, var, tmp, fndecl;
4120 :
4121 1396 : gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
4122 : && expr->value.op.op2->ts.type == BT_CHARACTER);
4123 1396 : gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
4124 :
4125 1396 : gfc_init_se (&lse, se);
4126 1396 : gfc_conv_expr (&lse, expr->value.op.op1);
4127 1396 : gfc_conv_string_parameter (&lse);
4128 1396 : gfc_init_se (&rse, se);
4129 1396 : gfc_conv_expr (&rse, expr->value.op.op2);
4130 1396 : gfc_conv_string_parameter (&rse);
4131 :
4132 1396 : gfc_add_block_to_block (&se->pre, &lse.pre);
4133 1396 : gfc_add_block_to_block (&se->pre, &rse.pre);
4134 :
4135 1396 : type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
4136 1396 : len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
4137 1396 : if (len == NULL_TREE)
4138 : {
4139 1177 : len = fold_build2_loc (input_location, PLUS_EXPR,
4140 : gfc_charlen_type_node,
4141 : fold_convert (gfc_charlen_type_node,
4142 : lse.string_length),
4143 : fold_convert (gfc_charlen_type_node,
4144 : rse.string_length));
4145 : }
4146 :
4147 1396 : type = build_pointer_type (type);
4148 :
4149 1396 : var = gfc_conv_string_tmp (se, type, len);
4150 :
4151 : /* Do the actual concatenation. */
4152 1396 : if (expr->ts.kind == 1)
4153 1305 : fndecl = gfor_fndecl_concat_string;
4154 91 : else if (expr->ts.kind == 4)
4155 91 : fndecl = gfor_fndecl_concat_string_char4;
4156 : else
4157 0 : gcc_unreachable ();
4158 :
4159 1396 : tmp = build_call_expr_loc (input_location,
4160 : fndecl, 6, len, var, lse.string_length, lse.expr,
4161 : rse.string_length, rse.expr);
4162 1396 : gfc_add_expr_to_block (&se->pre, tmp);
4163 :
4164 : /* Add the cleanup for the operands. */
4165 1396 : gfc_add_block_to_block (&se->pre, &rse.post);
4166 1396 : gfc_add_block_to_block (&se->pre, &lse.post);
4167 :
4168 1396 : se->expr = var;
4169 1396 : se->string_length = len;
4170 1396 : }
4171 :
4172 :
4173 : /* Return the variable that E puts in parentheses, or NULL. */
4174 :
4175 : static gfc_expr *
4176 1051 : parenthesized_variable (gfc_expr *e)
4177 : {
4178 234 : if (e->expr_type == EXPR_OP
4179 66 : && e->value.op.op == INTRINSIC_PARENTHESES
4180 96 : && e->value.op.op1->expr_type == EXPR_VARIABLE)
4181 0 : return e->value.op.op1;
4182 : return NULL;
4183 : }
4184 :
4185 :
4186 : /* Replace the scalar class container in SE->EXPR, or its address, by a
4187 : container of a copy of its data, which SE->POST finalizes and frees. */
4188 :
4189 : static void
4190 90 : conv_class_value_copy (gfc_se *se)
4191 : {
4192 90 : tree from, copy, data, size, tmp;
4193 90 : stmtblock_t block;
4194 90 : symbol_attribute attr;
4195 90 : gfc_se fse;
4196 90 : bool unlimited;
4197 :
4198 90 : from = se->expr;
4199 90 : if (POINTER_TYPE_P (TREE_TYPE (from)))
4200 48 : from = build_fold_indirect_ref_loc (input_location, from);
4201 90 : if (!DECL_P (from))
4202 0 : from = gfc_evaluate_now (from, &se->pre);
4203 90 : unlimited = !integer_zerop (gfc_class_len_or_zero_get (from));
4204 :
4205 : /* The copy has the _vptr and _len of FROM. */
4206 90 : copy = gfc_create_var (TREE_TYPE (from), "class_copy");
4207 90 : gfc_add_modify (&se->pre, copy, from);
4208 :
4209 : /* Leave a null _data alone; its _vptr may not be valid. */
4210 90 : gfc_init_block (&block);
4211 90 : size = fold_convert (size_type_node, gfc_class_vtab_size_get (from));
4212 90 : size = gfc_resize_class_size_with_len (&block, from, size);
4213 90 : data = gfc_class_data_get (copy);
4214 90 : gfc_add_modify (&block, data,
4215 90 : gfc_call_malloc (&block, TREE_TYPE (data), size));
4216 90 : gfc_add_expr_to_block (&block, gfc_copy_class_to_class (from, copy,
4217 : NULL_TREE,
4218 : unlimited));
4219 90 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
4220 : gfc_class_data_get (from),
4221 90 : fold_convert (TREE_TYPE (data), null_pointer_node));
4222 90 : tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block),
4223 : build_empty_stmt (input_location));
4224 90 : gfc_add_expr_to_block (&se->pre, tmp);
4225 :
4226 90 : gfc_init_se (&fse, NULL);
4227 90 : fse.expr = copy;
4228 90 : gfc_clear_attr (&attr);
4229 90 : attr.allocatable = 1;
4230 90 : gfc_finalize_tree_expr (&fse, NULL, attr, 0);
4231 90 : gfc_add_block_to_block (&se->pre, &fse.pre);
4232 90 : gfc_add_block_to_block (&se->post, &fse.post);
4233 90 : gfc_add_block_to_block (&se->post, &fse.finalblock);
4234 :
4235 90 : se->expr = copy;
4236 90 : }
4237 :
4238 :
4239 : /* Translates an op expression. Common (binary) cases are handled by this
4240 : function, others are passed on. Recursion is used in either case.
4241 : We use the fact that (op1.ts == op2.ts) (except for the power
4242 : operator **).
4243 : Operators need no special handling for scalarized expressions as long as
4244 : they call gfc_conv_simple_val to get their operands.
4245 : Character strings get special handling. */
4246 :
4247 : static void
4248 523570 : gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
4249 : {
4250 523570 : enum tree_code code;
4251 523570 : gfc_se lse;
4252 523570 : gfc_se rse;
4253 523570 : tree tmp, type;
4254 523570 : int lop;
4255 523570 : int checkstring;
4256 :
4257 523570 : checkstring = 0;
4258 523570 : lop = 0;
4259 523570 : switch (expr->value.op.op)
4260 : {
4261 15587 : case INTRINSIC_PARENTHESES:
4262 : /* The value of a parenthesized CLASS variable is a copy. */
4263 66 : if (expr->ts.type == BT_CLASS && expr->rank == 0
4264 15587 : && parenthesized_variable (expr))
4265 : {
4266 30 : gfc_conv_expr (se, expr->value.op.op1);
4267 30 : conv_class_value_copy (se);
4268 91683 : return;
4269 : }
4270 15557 : if ((expr->ts.type == BT_REAL || expr->ts.type == BT_COMPLEX)
4271 3774 : && flag_protect_parens)
4272 : {
4273 3640 : gfc_conv_unary_op (PAREN_EXPR, se, expr);
4274 3640 : gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
4275 : return;
4276 : }
4277 :
4278 : /* Fallthrough. */
4279 11923 : case INTRINSIC_UPLUS:
4280 11923 : gfc_conv_expr (se, expr->value.op.op1);
4281 11923 : return;
4282 :
4283 5017 : case INTRINSIC_UMINUS:
4284 5017 : gfc_conv_unary_op (NEGATE_EXPR, se, expr);
4285 5017 : return;
4286 :
4287 20459 : case INTRINSIC_NOT:
4288 20459 : gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
4289 20459 : return;
4290 :
4291 : case INTRINSIC_PLUS:
4292 : code = PLUS_EXPR;
4293 : break;
4294 :
4295 31690 : case INTRINSIC_MINUS:
4296 31690 : code = MINUS_EXPR;
4297 31690 : break;
4298 :
4299 35091 : case INTRINSIC_TIMES:
4300 35091 : code = MULT_EXPR;
4301 35091 : break;
4302 :
4303 7579 : case INTRINSIC_DIVIDE:
4304 : /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
4305 : an integer or unsigned, we must round towards zero, so we use a
4306 : TRUNC_DIV_EXPR. */
4307 7579 : if (expr->ts.type == BT_INTEGER || expr->ts.type == BT_UNSIGNED)
4308 : code = TRUNC_DIV_EXPR;
4309 : else
4310 431917 : code = RDIV_EXPR;
4311 : break;
4312 :
4313 49188 : case INTRINSIC_POWER:
4314 49188 : gfc_conv_power_op (se, expr);
4315 49188 : return;
4316 :
4317 1396 : case INTRINSIC_CONCAT:
4318 1396 : gfc_conv_concat_op (se, expr);
4319 1396 : return;
4320 :
4321 4949 : case INTRINSIC_AND:
4322 4949 : code = flag_frontend_optimize ? TRUTH_ANDIF_EXPR : TRUTH_AND_EXPR;
4323 : lop = 1;
4324 : break;
4325 :
4326 56586 : case INTRINSIC_OR:
4327 56586 : code = flag_frontend_optimize ? TRUTH_ORIF_EXPR : TRUTH_OR_EXPR;
4328 : lop = 1;
4329 : break;
4330 :
4331 : /* EQV and NEQV only work on logicals, but since we represent them
4332 : as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
4333 12975 : case INTRINSIC_EQ:
4334 12975 : case INTRINSIC_EQ_OS:
4335 12975 : case INTRINSIC_EQV:
4336 12975 : code = EQ_EXPR;
4337 12975 : checkstring = 1;
4338 12975 : lop = 1;
4339 12975 : break;
4340 :
4341 213415 : case INTRINSIC_NE:
4342 213415 : case INTRINSIC_NE_OS:
4343 213415 : case INTRINSIC_NEQV:
4344 213415 : code = NE_EXPR;
4345 213415 : checkstring = 1;
4346 213415 : lop = 1;
4347 213415 : break;
4348 :
4349 12241 : case INTRINSIC_GT:
4350 12241 : case INTRINSIC_GT_OS:
4351 12241 : code = GT_EXPR;
4352 12241 : checkstring = 1;
4353 12241 : lop = 1;
4354 12241 : break;
4355 :
4356 1677 : case INTRINSIC_GE:
4357 1677 : case INTRINSIC_GE_OS:
4358 1677 : code = GE_EXPR;
4359 1677 : checkstring = 1;
4360 1677 : lop = 1;
4361 1677 : break;
4362 :
4363 4435 : case INTRINSIC_LT:
4364 4435 : case INTRINSIC_LT_OS:
4365 4435 : code = LT_EXPR;
4366 4435 : checkstring = 1;
4367 4435 : lop = 1;
4368 4435 : break;
4369 :
4370 2660 : case INTRINSIC_LE:
4371 2660 : case INTRINSIC_LE_OS:
4372 2660 : code = LE_EXPR;
4373 2660 : checkstring = 1;
4374 2660 : lop = 1;
4375 2660 : break;
4376 :
4377 0 : case INTRINSIC_USER:
4378 0 : case INTRINSIC_ASSIGN:
4379 : /* These should be converted into function calls by the frontend. */
4380 0 : gcc_unreachable ();
4381 :
4382 0 : default:
4383 0 : fatal_error (input_location, "Unknown intrinsic op");
4384 431917 : return;
4385 : }
4386 :
4387 : /* The only exception to this is **, which is handled separately anyway. */
4388 431917 : gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
4389 :
4390 431917 : if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
4391 397174 : checkstring = 0;
4392 :
4393 : /* lhs */
4394 431917 : gfc_init_se (&lse, se);
4395 431917 : gfc_conv_expr (&lse, expr->value.op.op1);
4396 431917 : gfc_add_block_to_block (&se->pre, &lse.pre);
4397 :
4398 : /* rhs */
4399 431917 : gfc_init_se (&rse, se);
4400 431917 : gfc_conv_expr (&rse, expr->value.op.op2);
4401 431917 : gfc_add_block_to_block (&se->pre, &rse.pre);
4402 :
4403 431917 : if (checkstring)
4404 : {
4405 34743 : gfc_conv_string_parameter (&lse);
4406 34743 : gfc_conv_string_parameter (&rse);
4407 :
4408 69486 : lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
4409 : rse.string_length, rse.expr,
4410 34743 : expr->value.op.op1->ts.kind,
4411 : code);
4412 34743 : rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
4413 34743 : gfc_add_block_to_block (&lse.post, &rse.post);
4414 : }
4415 :
4416 431917 : type = gfc_typenode_for_spec (&expr->ts);
4417 :
4418 431917 : if (lop)
4419 : {
4420 : // Inhibit overeager optimization of Cray pointer comparisons (PR106692).
4421 308938 : if (expr->value.op.op1->expr_type == EXPR_VARIABLE
4422 174746 : && expr->value.op.op1->ts.type == BT_INTEGER
4423 76969 : && expr->value.op.op1->symtree
4424 76969 : && expr->value.op.op1->symtree->n.sym->attr.cray_pointer)
4425 12 : TREE_THIS_VOLATILE (lse.expr) = 1;
4426 :
4427 308938 : if (expr->value.op.op2->expr_type == EXPR_VARIABLE
4428 73506 : && expr->value.op.op2->ts.type == BT_INTEGER
4429 13947 : && expr->value.op.op2->symtree
4430 13947 : && expr->value.op.op2->symtree->n.sym->attr.cray_pointer)
4431 12 : TREE_THIS_VOLATILE (rse.expr) = 1;
4432 :
4433 : /* The result of logical ops is always logical_type_node. */
4434 308938 : tmp = fold_build2_loc (input_location, code, logical_type_node,
4435 : lse.expr, rse.expr);
4436 308938 : se->expr = convert (type, tmp);
4437 : }
4438 : else
4439 122979 : se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
4440 :
4441 : /* Add the post blocks. */
4442 431917 : gfc_add_block_to_block (&se->post, &rse.post);
4443 431917 : gfc_add_block_to_block (&se->post, &lse.post);
4444 : }
4445 :
4446 : static void
4447 159 : gfc_conv_conditional_expr (gfc_se *se, gfc_expr *expr)
4448 : {
4449 159 : gfc_se cond_se, true_se, false_se;
4450 159 : tree condition, true_val, false_val;
4451 159 : tree type;
4452 :
4453 159 : gfc_init_se (&cond_se, se);
4454 159 : gfc_init_se (&true_se, se);
4455 159 : gfc_init_se (&false_se, se);
4456 :
4457 159 : gfc_conv_expr (&cond_se, expr->value.conditional.condition);
4458 159 : gfc_add_block_to_block (&se->pre, &cond_se.pre);
4459 159 : condition = gfc_evaluate_now (cond_se.expr, &se->pre);
4460 :
4461 159 : true_se.want_pointer = se->want_pointer;
4462 159 : gfc_conv_expr (&true_se, expr->value.conditional.true_expr);
4463 159 : true_val = true_se.expr;
4464 159 : false_se.want_pointer = se->want_pointer;
4465 159 : gfc_conv_expr (&false_se, expr->value.conditional.false_expr);
4466 159 : false_val = false_se.expr;
4467 :
4468 159 : if (true_se.pre.head != NULL_TREE || false_se.pre.head != NULL_TREE)
4469 24 : gfc_add_expr_to_block (
4470 : &se->pre,
4471 : fold_build3_loc (input_location, COND_EXPR, void_type_node, condition,
4472 24 : true_se.pre.head != NULL_TREE
4473 6 : ? gfc_finish_block (&true_se.pre)
4474 18 : : build_empty_stmt (input_location),
4475 24 : false_se.pre.head != NULL_TREE
4476 24 : ? gfc_finish_block (&false_se.pre)
4477 0 : : build_empty_stmt (input_location)));
4478 :
4479 159 : if (true_se.post.head != NULL_TREE || false_se.post.head != NULL_TREE)
4480 6 : gfc_add_expr_to_block (
4481 : &se->post,
4482 : fold_build3_loc (input_location, COND_EXPR, void_type_node, condition,
4483 6 : true_se.post.head != NULL_TREE
4484 0 : ? gfc_finish_block (&true_se.post)
4485 6 : : build_empty_stmt (input_location),
4486 6 : false_se.post.head != NULL_TREE
4487 6 : ? gfc_finish_block (&false_se.post)
4488 0 : : build_empty_stmt (input_location)));
4489 :
4490 159 : type = gfc_typenode_for_spec (&expr->ts);
4491 159 : if (se->want_pointer)
4492 18 : type = build_pointer_type (type);
4493 :
4494 159 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, condition,
4495 : true_val, false_val);
4496 159 : if (expr->ts.type == BT_CHARACTER)
4497 66 : se->string_length
4498 66 : = fold_build3_loc (input_location, COND_EXPR, gfc_charlen_type_node,
4499 : condition, true_se.string_length,
4500 : false_se.string_length);
4501 159 : }
4502 :
4503 : /* If a string's length is one, we convert it to a single character. */
4504 :
4505 : tree
4506 145510 : gfc_string_to_single_character (tree len, tree str, int kind)
4507 : {
4508 :
4509 145510 : if (len == NULL
4510 145510 : || !tree_fits_uhwi_p (len)
4511 267328 : || !POINTER_TYPE_P (TREE_TYPE (str)))
4512 : return NULL_TREE;
4513 :
4514 121766 : if (TREE_INT_CST_LOW (len) == 1)
4515 : {
4516 24398 : str = fold_convert (gfc_get_pchar_type (kind), str);
4517 24398 : return build_fold_indirect_ref_loc (input_location, str);
4518 : }
4519 :
4520 97368 : if (kind == 1
4521 79878 : && TREE_CODE (str) == ADDR_EXPR
4522 68675 : && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
4523 49017 : && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
4524 30400 : && array_ref_low_bound (TREE_OPERAND (str, 0))
4525 30400 : == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
4526 30400 : && TREE_INT_CST_LOW (len) > 1
4527 125907 : && TREE_INT_CST_LOW (len)
4528 : == (unsigned HOST_WIDE_INT)
4529 28539 : TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
4530 : {
4531 28539 : tree ret = fold_convert (gfc_get_pchar_type (kind), str);
4532 28539 : ret = build_fold_indirect_ref_loc (input_location, ret);
4533 28539 : if (TREE_CODE (ret) == INTEGER_CST)
4534 : {
4535 28539 : tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
4536 28539 : int i, length = TREE_STRING_LENGTH (string_cst);
4537 28539 : const char *ptr = TREE_STRING_POINTER (string_cst);
4538 :
4539 42685 : for (i = 1; i < length; i++)
4540 41987 : if (ptr[i] != ' ')
4541 : return NULL_TREE;
4542 :
4543 : return ret;
4544 : }
4545 : }
4546 :
4547 : return NULL_TREE;
4548 : }
4549 :
4550 :
4551 : static void
4552 220 : conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
4553 : {
4554 220 : gcc_assert (expr);
4555 :
4556 : /* We used to modify the tree here. Now it is done earlier in
4557 : the front-end, so we only check it here to avoid regressions. */
4558 220 : if (sym->backend_decl)
4559 : {
4560 67 : gcc_assert (TREE_CODE (TREE_TYPE (sym->backend_decl)) == INTEGER_TYPE);
4561 67 : gcc_assert (TYPE_UNSIGNED (TREE_TYPE (sym->backend_decl)) == 1);
4562 67 : gcc_assert (TYPE_PRECISION (TREE_TYPE (sym->backend_decl)) == CHAR_TYPE_SIZE);
4563 67 : gcc_assert (DECL_BY_REFERENCE (sym->backend_decl) == 0);
4564 : }
4565 :
4566 : /* If we have a constant character expression, make it into an
4567 : integer of type C char. */
4568 220 : if ((*expr)->expr_type == EXPR_CONSTANT)
4569 : {
4570 166 : gfc_typespec ts;
4571 166 : gfc_clear_ts (&ts);
4572 :
4573 332 : gfc_expr *tmp = gfc_get_int_expr (gfc_default_character_kind, NULL,
4574 166 : (*expr)->value.character.string[0]);
4575 166 : gfc_replace_expr (*expr, tmp);
4576 : }
4577 54 : else if (se != NULL)
4578 : {
4579 54 : gfc_conv_expr (se, *expr);
4580 54 : gfc_conv_string_parameter (se);
4581 108 : se->expr = gfc_string_to_single_character (integer_one_node,
4582 54 : se->expr, (*expr)->ts.kind);
4583 54 : se->string_length = integer_one_node;
4584 : }
4585 220 : }
4586 :
4587 : /* Helper function for gfc_build_compare_string. Return LEN_TRIM value
4588 : if STR is a string literal, otherwise return -1. */
4589 :
4590 : static int
4591 32916 : gfc_optimize_len_trim (tree len, tree str, int kind)
4592 : {
4593 32916 : if (kind == 1
4594 27824 : && TREE_CODE (str) == ADDR_EXPR
4595 24396 : && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
4596 15577 : && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
4597 10084 : && array_ref_low_bound (TREE_OPERAND (str, 0))
4598 10084 : == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
4599 10084 : && tree_fits_uhwi_p (len)
4600 10084 : && tree_to_uhwi (len) >= 1
4601 32916 : && tree_to_uhwi (len)
4602 10038 : == (unsigned HOST_WIDE_INT)
4603 10038 : TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
4604 : {
4605 10038 : tree folded = fold_convert (gfc_get_pchar_type (kind), str);
4606 10038 : folded = build_fold_indirect_ref_loc (input_location, folded);
4607 10038 : if (TREE_CODE (folded) == INTEGER_CST)
4608 : {
4609 10038 : tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
4610 10038 : int length = TREE_STRING_LENGTH (string_cst);
4611 10038 : const char *ptr = TREE_STRING_POINTER (string_cst);
4612 :
4613 14962 : for (; length > 0; length--)
4614 14962 : if (ptr[length - 1] != ' ')
4615 : break;
4616 :
4617 : return length;
4618 : }
4619 : }
4620 : return -1;
4621 : }
4622 :
4623 : /* Helper to build a call to memcmp. */
4624 :
4625 : static tree
4626 13417 : build_memcmp_call (tree s1, tree s2, tree n)
4627 : {
4628 13417 : tree tmp;
4629 :
4630 13417 : if (!POINTER_TYPE_P (TREE_TYPE (s1)))
4631 0 : s1 = gfc_build_addr_expr (pvoid_type_node, s1);
4632 : else
4633 13417 : s1 = fold_convert (pvoid_type_node, s1);
4634 :
4635 13417 : if (!POINTER_TYPE_P (TREE_TYPE (s2)))
4636 0 : s2 = gfc_build_addr_expr (pvoid_type_node, s2);
4637 : else
4638 13417 : s2 = fold_convert (pvoid_type_node, s2);
4639 :
4640 13417 : n = fold_convert (size_type_node, n);
4641 :
4642 13417 : tmp = build_call_expr_loc (input_location,
4643 : builtin_decl_explicit (BUILT_IN_MEMCMP),
4644 : 3, s1, s2, n);
4645 :
4646 13417 : return fold_convert (integer_type_node, tmp);
4647 : }
4648 :
4649 : /* Compare two strings. If they are all single characters, the result is the
4650 : subtraction of them. Otherwise, we build a library call. */
4651 :
4652 : tree
4653 34842 : gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
4654 : enum tree_code code)
4655 : {
4656 34842 : tree sc1;
4657 34842 : tree sc2;
4658 34842 : tree fndecl;
4659 :
4660 34842 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
4661 34842 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
4662 :
4663 34842 : sc1 = gfc_string_to_single_character (len1, str1, kind);
4664 34842 : sc2 = gfc_string_to_single_character (len2, str2, kind);
4665 :
4666 34842 : if (sc1 != NULL_TREE && sc2 != NULL_TREE)
4667 : {
4668 : /* Deal with single character specially. */
4669 4911 : sc1 = fold_convert (integer_type_node, sc1);
4670 4911 : sc2 = fold_convert (integer_type_node, sc2);
4671 4911 : return fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
4672 4911 : sc1, sc2);
4673 : }
4674 :
4675 29931 : if ((code == EQ_EXPR || code == NE_EXPR)
4676 29369 : && optimize
4677 24600 : && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
4678 : {
4679 : /* If one string is a string literal with LEN_TRIM longer
4680 : than the length of the second string, the strings
4681 : compare unequal. */
4682 16458 : int len = gfc_optimize_len_trim (len1, str1, kind);
4683 16458 : if (len > 0 && compare_tree_int (len2, len) < 0)
4684 0 : return integer_one_node;
4685 16458 : len = gfc_optimize_len_trim (len2, str2, kind);
4686 16458 : if (len > 0 && compare_tree_int (len1, len) < 0)
4687 0 : return integer_one_node;
4688 : }
4689 :
4690 : /* We can compare via memcmp if the strings are known to be equal
4691 : in length and they are
4692 : - kind=1
4693 : - kind=4 and the comparison is for (in)equality. */
4694 :
4695 20052 : if (INTEGER_CST_P (len1) && INTEGER_CST_P (len2)
4696 19714 : && tree_int_cst_equal (len1, len2)
4697 43408 : && (kind == 1 || code == EQ_EXPR || code == NE_EXPR))
4698 : {
4699 13417 : tree tmp;
4700 13417 : tree chartype;
4701 :
4702 13417 : chartype = gfc_get_char_type (kind);
4703 13417 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE(len1),
4704 13417 : fold_convert (TREE_TYPE(len1),
4705 : TYPE_SIZE_UNIT(chartype)),
4706 : len1);
4707 13417 : return build_memcmp_call (str1, str2, tmp);
4708 : }
4709 :
4710 : /* Build a call for the comparison. */
4711 16514 : if (kind == 1)
4712 13659 : fndecl = gfor_fndecl_compare_string;
4713 2855 : else if (kind == 4)
4714 2855 : fndecl = gfor_fndecl_compare_string_char4;
4715 : else
4716 0 : gcc_unreachable ();
4717 :
4718 16514 : return build_call_expr_loc (input_location, fndecl, 4,
4719 16514 : len1, str1, len2, str2);
4720 : }
4721 :
4722 :
4723 : /* Return the backend_decl for a procedure pointer component. */
4724 :
4725 : static tree
4726 1986 : get_proc_ptr_comp (gfc_expr *e)
4727 : {
4728 1986 : gfc_se comp_se;
4729 1986 : gfc_expr *e2;
4730 1986 : expr_t old_type;
4731 :
4732 1986 : gfc_init_se (&comp_se, NULL);
4733 1986 : e2 = gfc_copy_expr (e);
4734 : /* We have to restore the expr type later so that gfc_free_expr frees
4735 : the exact same thing that was allocated.
4736 : TODO: This is ugly. */
4737 1986 : old_type = e2->expr_type;
4738 1986 : e2->expr_type = EXPR_VARIABLE;
4739 1986 : gfc_conv_expr (&comp_se, e2);
4740 1986 : e2->expr_type = old_type;
4741 1986 : gfc_free_expr (e2);
4742 1986 : return build_fold_addr_expr_loc (input_location, comp_se.expr);
4743 : }
4744 :
4745 :
4746 : /* Convert a typebound function reference from a class object. */
4747 : static void
4748 80 : conv_base_obj_fcn_val (gfc_se * se, tree base_object, gfc_expr * expr)
4749 : {
4750 80 : gfc_ref *ref;
4751 80 : tree var;
4752 :
4753 80 : if (!VAR_P (base_object))
4754 : {
4755 0 : var = gfc_create_var (TREE_TYPE (base_object), NULL);
4756 0 : gfc_add_modify (&se->pre, var, base_object);
4757 : }
4758 80 : se->expr = gfc_class_vptr_get (base_object);
4759 80 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
4760 80 : ref = expr->ref;
4761 308 : while (ref && ref->next)
4762 : ref = ref->next;
4763 80 : gcc_assert (ref && ref->type == REF_COMPONENT);
4764 80 : if (ref->u.c.sym->attr.extension)
4765 0 : conv_parent_component_references (se, ref);
4766 80 : gfc_conv_component_ref (se, ref);
4767 80 : se->expr = build_fold_addr_expr_loc (input_location, se->expr);
4768 80 : }
4769 :
4770 : static tree
4771 132385 : get_builtin_fn (gfc_symbol * sym)
4772 : {
4773 132385 : if (!gfc_option.disable_omp_is_initial_device
4774 132381 : && flag_openmp && sym->attr.function && sym->ts.type == BT_LOGICAL
4775 632 : && !strcmp (sym->name, "omp_is_initial_device"))
4776 42 : return builtin_decl_explicit (BUILT_IN_OMP_IS_INITIAL_DEVICE);
4777 :
4778 132343 : if (!gfc_option.disable_omp_get_initial_device
4779 132336 : && flag_openmp && sym->attr.function && sym->ts.type == BT_INTEGER
4780 4350 : && !strcmp (sym->name, "omp_get_initial_device"))
4781 29 : return builtin_decl_explicit (BUILT_IN_OMP_GET_INITIAL_DEVICE);
4782 :
4783 132314 : if (!gfc_option.disable_omp_get_num_devices
4784 132307 : && flag_openmp && sym->attr.function && sym->ts.type == BT_INTEGER
4785 4321 : && !strcmp (sym->name, "omp_get_num_devices"))
4786 109 : return builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_DEVICES);
4787 :
4788 132205 : if (!gfc_option.disable_acc_on_device
4789 132025 : && flag_openacc && sym->attr.function && sym->ts.type == BT_LOGICAL
4790 1169 : && !strcmp (sym->name, "acc_on_device_h"))
4791 390 : return builtin_decl_explicit (BUILT_IN_ACC_ON_DEVICE);
4792 :
4793 : return NULL_TREE;
4794 : }
4795 :
4796 : static tree
4797 570 : update_builtin_function (tree fn_call, gfc_symbol *sym)
4798 : {
4799 570 : tree fn = TREE_OPERAND (CALL_EXPR_FN (fn_call), 0);
4800 :
4801 570 : if (DECL_FUNCTION_CODE (fn) == BUILT_IN_OMP_IS_INITIAL_DEVICE)
4802 : /* In Fortran omp_is_initial_device returns logical(4)
4803 : but the builtin uses 'int'. */
4804 42 : return fold_convert (TREE_TYPE (TREE_TYPE (sym->backend_decl)), fn_call);
4805 :
4806 528 : else if (DECL_FUNCTION_CODE (fn) == BUILT_IN_ACC_ON_DEVICE)
4807 : {
4808 : /* Likewise for the return type; additionally, the argument it a
4809 : call-by-value int, Fortran has a by-reference 'integer(4)'. */
4810 390 : tree arg = build_fold_indirect_ref_loc (input_location,
4811 390 : CALL_EXPR_ARG (fn_call, 0));
4812 390 : CALL_EXPR_ARG (fn_call, 0) = fold_convert (integer_type_node, arg);
4813 390 : return fold_convert (TREE_TYPE (TREE_TYPE (sym->backend_decl)), fn_call);
4814 : }
4815 : return fn_call;
4816 : }
4817 :
4818 : static void
4819 135198 : conv_function_val (gfc_se * se, bool *is_builtin, gfc_symbol * sym,
4820 : gfc_expr * expr, gfc_actual_arglist *actual_args)
4821 : {
4822 135198 : tree tmp;
4823 :
4824 135198 : if (gfc_is_proc_ptr_comp (expr))
4825 1986 : tmp = get_proc_ptr_comp (expr);
4826 133212 : else if (sym->attr.dummy)
4827 : {
4828 827 : tmp = gfc_get_symbol_decl (sym);
4829 827 : if (sym->attr.proc_pointer)
4830 89 : tmp = build_fold_indirect_ref_loc (input_location,
4831 : tmp);
4832 827 : gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
4833 : && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
4834 : }
4835 : else
4836 : {
4837 132385 : if (!sym->backend_decl)
4838 33454 : sym->backend_decl = gfc_get_extern_function_decl (sym, actual_args);
4839 :
4840 132385 : if ((tmp = get_builtin_fn (sym)) != NULL_TREE)
4841 570 : *is_builtin = true;
4842 : else
4843 : {
4844 131815 : TREE_USED (sym->backend_decl) = 1;
4845 131815 : tmp = sym->backend_decl;
4846 : }
4847 :
4848 132385 : if (sym->attr.cray_pointee)
4849 : {
4850 : /* TODO - make the cray pointee a pointer to a procedure,
4851 : assign the pointer to it and use it for the call. This
4852 : will do for now! */
4853 19 : tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
4854 19 : gfc_get_symbol_decl (sym->cp_pointer));
4855 19 : tmp = gfc_evaluate_now (tmp, &se->pre);
4856 : }
4857 :
4858 132385 : if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
4859 : {
4860 131757 : gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
4861 131757 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
4862 : }
4863 : }
4864 135198 : se->expr = tmp;
4865 135198 : }
4866 :
4867 :
4868 : /* Initialize MAPPING. */
4869 :
4870 : void
4871 135315 : gfc_init_interface_mapping (gfc_interface_mapping * mapping)
4872 : {
4873 135315 : mapping->syms = NULL;
4874 135315 : mapping->charlens = NULL;
4875 135315 : }
4876 :
4877 :
4878 : /* Free all memory held by MAPPING (but not MAPPING itself). */
4879 :
4880 : void
4881 135315 : gfc_free_interface_mapping (gfc_interface_mapping * mapping)
4882 : {
4883 135315 : gfc_interface_sym_mapping *sym;
4884 135315 : gfc_interface_sym_mapping *nextsym;
4885 135315 : gfc_charlen *cl;
4886 135315 : gfc_charlen *nextcl;
4887 :
4888 176432 : for (sym = mapping->syms; sym; sym = nextsym)
4889 : {
4890 41117 : nextsym = sym->next;
4891 41117 : sym->new_sym->n.sym->formal = NULL;
4892 41117 : gfc_free_symbol (sym->new_sym->n.sym);
4893 41117 : gfc_free_expr (sym->expr);
4894 41117 : free (sym->new_sym);
4895 41117 : free (sym);
4896 : }
4897 140027 : for (cl = mapping->charlens; cl; cl = nextcl)
4898 : {
4899 4712 : nextcl = cl->next;
4900 4712 : gfc_free_expr (cl->length);
4901 4712 : free (cl);
4902 : }
4903 135315 : }
4904 :
4905 :
4906 : /* Return a copy of gfc_charlen CL. Add the returned structure to
4907 : MAPPING so that it will be freed by gfc_free_interface_mapping. */
4908 :
4909 : static gfc_charlen *
4910 4712 : gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
4911 : gfc_charlen * cl)
4912 : {
4913 4712 : gfc_charlen *new_charlen;
4914 :
4915 4712 : new_charlen = gfc_get_charlen ();
4916 4712 : new_charlen->next = mapping->charlens;
4917 4712 : new_charlen->length = gfc_copy_expr (cl->length);
4918 :
4919 4712 : mapping->charlens = new_charlen;
4920 4712 : return new_charlen;
4921 : }
4922 :
4923 :
4924 : /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
4925 : array variable that can be used as the actual argument for dummy
4926 : argument SYM, except in the case of assumed rank dummies of
4927 : non-intrinsic functions where the descriptor must be passed. Add any
4928 : initialization code to BLOCK. PACKED is as for gfc_get_nodesc_array_type
4929 : and DATA points to the first element in the passed array. */
4930 :
4931 : static tree
4932 8496 : gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
4933 : gfc_packed packed, tree data, tree len,
4934 : bool assumed_rank_formal)
4935 : {
4936 8496 : tree type;
4937 8496 : tree var;
4938 :
4939 8496 : if (len != NULL_TREE && (TREE_CONSTANT (len) || VAR_P (len)))
4940 70 : type = gfc_get_character_type_len (sym->ts.kind, len);
4941 : else
4942 8426 : type = gfc_typenode_for_spec (&sym->ts);
4943 :
4944 8496 : if (assumed_rank_formal)
4945 13 : type = TREE_TYPE (data);
4946 : else
4947 8483 : type = gfc_get_nodesc_array_type (type, sym->as, packed,
4948 8483 : !sym->attr.target && !sym->attr.pointer
4949 8459 : && !sym->attr.proc_pointer);
4950 :
4951 8496 : var = gfc_create_var (type, "ifm");
4952 8496 : gfc_add_modify (block, var, fold_convert (type, data));
4953 :
4954 8496 : return var;
4955 : }
4956 :
4957 :
4958 : /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
4959 : and offset of descriptorless array type TYPE given that it has the same
4960 : size as DESC. Add any set-up code to BLOCK. */
4961 :
4962 : static void
4963 8166 : gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
4964 : {
4965 8166 : int n;
4966 8166 : tree dim;
4967 8166 : tree offset;
4968 8166 : tree tmp;
4969 :
4970 8166 : offset = gfc_index_zero_node;
4971 9292 : for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
4972 : {
4973 1126 : dim = gfc_rank_cst[n];
4974 1126 : GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
4975 1126 : if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
4976 : {
4977 1 : GFC_TYPE_ARRAY_LBOUND (type, n)
4978 1 : = gfc_conv_descriptor_lbound_get (desc, dim);
4979 1 : GFC_TYPE_ARRAY_UBOUND (type, n)
4980 2 : = gfc_conv_descriptor_ubound_get (desc, dim);
4981 : }
4982 1125 : else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
4983 : {
4984 1099 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
4985 : gfc_array_index_type,
4986 : gfc_conv_descriptor_ubound_get (desc, dim),
4987 : gfc_conv_descriptor_lbound_get (desc, dim));
4988 3297 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
4989 : gfc_array_index_type,
4990 1099 : GFC_TYPE_ARRAY_LBOUND (type, n), tmp);
4991 1099 : tmp = gfc_evaluate_now (tmp, block);
4992 1099 : GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
4993 : }
4994 4504 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
4995 1126 : GFC_TYPE_ARRAY_LBOUND (type, n),
4996 1126 : GFC_TYPE_ARRAY_STRIDE (type, n));
4997 1126 : offset = fold_build2_loc (input_location, MINUS_EXPR,
4998 : gfc_array_index_type, offset, tmp);
4999 : }
5000 8166 : offset = gfc_evaluate_now (offset, block);
5001 8166 : GFC_TYPE_ARRAY_OFFSET (type) = offset;
5002 8166 : }
5003 :
5004 :
5005 : /* Extend MAPPING so that it maps dummy argument SYM to the value stored
5006 : in SE. The caller may still use se->expr and se->string_length after
5007 : calling this function. */
5008 :
5009 : void
5010 41117 : gfc_add_interface_mapping (gfc_interface_mapping * mapping,
5011 : gfc_symbol * sym, gfc_se * se,
5012 : gfc_expr *expr)
5013 : {
5014 41117 : gfc_interface_sym_mapping *sm;
5015 41117 : tree desc;
5016 41117 : tree tmp;
5017 41117 : tree value;
5018 41117 : gfc_symbol *new_sym;
5019 41117 : gfc_symtree *root;
5020 41117 : gfc_symtree *new_symtree;
5021 :
5022 : /* Create a new symbol to represent the actual argument. */
5023 41117 : new_sym = gfc_new_symbol (sym->name, NULL);
5024 41117 : new_sym->ts = sym->ts;
5025 41117 : new_sym->as = gfc_copy_array_spec (sym->as);
5026 41117 : new_sym->attr.referenced = 1;
5027 41117 : new_sym->attr.dimension = sym->attr.dimension;
5028 41117 : new_sym->attr.contiguous = sym->attr.contiguous;
5029 41117 : new_sym->attr.codimension = sym->attr.codimension;
5030 41117 : new_sym->attr.pointer = sym->attr.pointer;
5031 41117 : new_sym->attr.allocatable = sym->attr.allocatable;
5032 41117 : new_sym->attr.flavor = sym->attr.flavor;
5033 41117 : new_sym->attr.function = sym->attr.function;
5034 41117 : new_sym->attr.dummy = 0;
5035 :
5036 : /* Ensure that the interface is available and that
5037 : descriptors are passed for array actual arguments. */
5038 41117 : if (sym->attr.flavor == FL_PROCEDURE)
5039 : {
5040 36 : new_sym->formal = expr->symtree->n.sym->formal;
5041 36 : new_sym->attr.always_explicit
5042 36 : = expr->symtree->n.sym->attr.always_explicit;
5043 : }
5044 :
5045 : /* Create a fake symtree for it. */
5046 41117 : root = NULL;
5047 41117 : new_symtree = gfc_new_symtree (&root, sym->name);
5048 41117 : new_symtree->n.sym = new_sym;
5049 41117 : gcc_assert (new_symtree == root);
5050 :
5051 : /* Create a dummy->actual mapping. */
5052 41117 : sm = XCNEW (gfc_interface_sym_mapping);
5053 41117 : sm->next = mapping->syms;
5054 41117 : sm->old = sym;
5055 41117 : sm->new_sym = new_symtree;
5056 41117 : sm->expr = gfc_copy_expr (expr);
5057 41117 : mapping->syms = sm;
5058 :
5059 : /* Stabilize the argument's value. */
5060 41117 : if (!sym->attr.function && se)
5061 41019 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
5062 :
5063 41117 : if (sym->ts.type == BT_CHARACTER)
5064 : {
5065 : /* Create a copy of the dummy argument's length. */
5066 2898 : new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
5067 2898 : sm->expr->ts.u.cl = new_sym->ts.u.cl;
5068 :
5069 : /* If the length is specified as "*", record the length that
5070 : the caller is passing. We should use the callee's length
5071 : in all other cases. */
5072 2898 : if (!new_sym->ts.u.cl->length && se)
5073 : {
5074 2658 : se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
5075 2658 : new_sym->ts.u.cl->backend_decl = se->string_length;
5076 : }
5077 : }
5078 :
5079 41103 : if (!se)
5080 62 : return;
5081 :
5082 : /* Use the passed value as-is if the argument is a function. */
5083 41055 : if (sym->attr.flavor == FL_PROCEDURE)
5084 36 : value = se->expr;
5085 :
5086 : /* If the argument is a pass-by-value scalar, use the value as is. */
5087 41019 : else if (!sym->attr.dimension && sym->attr.value)
5088 78 : value = se->expr;
5089 :
5090 : /* If the argument is either a string or a pointer to a string,
5091 : convert it to a boundless character type. */
5092 40941 : else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
5093 : {
5094 1305 : se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
5095 1305 : tmp = gfc_get_character_type_len (sym->ts.kind, se->string_length);
5096 1305 : tmp = build_pointer_type (tmp);
5097 1305 : if (sym->attr.pointer)
5098 126 : value = build_fold_indirect_ref_loc (input_location,
5099 : se->expr);
5100 : else
5101 1179 : value = se->expr;
5102 1305 : value = fold_convert (tmp, value);
5103 : }
5104 :
5105 : /* If the argument is a scalar, a pointer to an array or an allocatable,
5106 : dereference it. */
5107 39636 : else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
5108 29631 : value = build_fold_indirect_ref_loc (input_location,
5109 : se->expr);
5110 :
5111 : /* For character(*), use the actual argument's descriptor. */
5112 10005 : else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
5113 1509 : value = build_fold_indirect_ref_loc (input_location,
5114 : se->expr);
5115 :
5116 : /* If the argument is an array descriptor, use it to determine
5117 : information about the actual argument's shape. */
5118 8496 : else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
5119 8496 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
5120 : {
5121 8166 : bool assumed_rank_formal = false;
5122 :
5123 : /* Get the actual argument's descriptor. */
5124 8166 : desc = build_fold_indirect_ref_loc (input_location,
5125 : se->expr);
5126 :
5127 : /* Create the replacement variable. */
5128 8166 : if (sym->as && sym->as->type == AS_ASSUMED_RANK
5129 7364 : && !(sym->ns && sym->ns->proc_name
5130 7364 : && sym->ns->proc_name->attr.proc == PROC_INTRINSIC))
5131 : {
5132 : assumed_rank_formal = true;
5133 : tmp = desc;
5134 : }
5135 : else
5136 8153 : tmp = gfc_conv_descriptor_data_get (desc);
5137 :
5138 8166 : value = gfc_get_interface_mapping_array (&se->pre, sym,
5139 : PACKED_NO, tmp,
5140 : se->string_length,
5141 : assumed_rank_formal);
5142 :
5143 : /* Use DESC to work out the upper bounds, strides and offset. */
5144 8166 : gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
5145 : }
5146 : else
5147 : /* Otherwise we have a packed array. */
5148 330 : value = gfc_get_interface_mapping_array (&se->pre, sym,
5149 : PACKED_FULL, se->expr,
5150 : se->string_length,
5151 : false);
5152 :
5153 41055 : new_sym->backend_decl = value;
5154 : }
5155 :
5156 :
5157 : /* Called once all dummy argument mappings have been added to MAPPING,
5158 : but before the mapping is used to evaluate expressions. Pre-evaluate
5159 : the length of each argument, adding any initialization code to PRE and
5160 : any finalization code to POST. */
5161 :
5162 : static void
5163 135278 : gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
5164 : stmtblock_t * pre, stmtblock_t * post)
5165 : {
5166 135278 : gfc_interface_sym_mapping *sym;
5167 135278 : gfc_expr *expr;
5168 135278 : gfc_se se;
5169 :
5170 176333 : for (sym = mapping->syms; sym; sym = sym->next)
5171 41055 : if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
5172 2884 : && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
5173 : {
5174 226 : expr = sym->new_sym->n.sym->ts.u.cl->length;
5175 226 : gfc_apply_interface_mapping_to_expr (mapping, expr);
5176 226 : gfc_init_se (&se, NULL);
5177 226 : gfc_conv_expr (&se, expr);
5178 226 : se.expr = fold_convert (gfc_charlen_type_node, se.expr);
5179 226 : se.expr = gfc_evaluate_now (se.expr, &se.pre);
5180 226 : gfc_add_block_to_block (pre, &se.pre);
5181 226 : gfc_add_block_to_block (post, &se.post);
5182 :
5183 226 : sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
5184 : }
5185 135278 : }
5186 :
5187 :
5188 : /* Like gfc_apply_interface_mapping_to_expr, but applied to
5189 : constructor C. */
5190 :
5191 : static void
5192 47 : gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
5193 : gfc_constructor_base base)
5194 : {
5195 47 : gfc_constructor *c;
5196 428 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
5197 : {
5198 381 : gfc_apply_interface_mapping_to_expr (mapping, c->expr);
5199 381 : if (c->iterator)
5200 : {
5201 6 : gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
5202 6 : gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
5203 6 : gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
5204 : }
5205 : }
5206 47 : }
5207 :
5208 :
5209 : /* Like gfc_apply_interface_mapping_to_expr, but applied to
5210 : reference REF. */
5211 :
5212 : static void
5213 12952 : gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
5214 : gfc_ref * ref)
5215 : {
5216 12952 : int n;
5217 :
5218 14473 : for (; ref; ref = ref->next)
5219 1521 : switch (ref->type)
5220 : {
5221 : case REF_ARRAY:
5222 2987 : for (n = 0; n < ref->u.ar.dimen; n++)
5223 : {
5224 1686 : gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
5225 1686 : gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
5226 1686 : gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
5227 : }
5228 : break;
5229 :
5230 : case REF_COMPONENT:
5231 : case REF_INQUIRY:
5232 : break;
5233 :
5234 43 : case REF_SUBSTRING:
5235 43 : gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
5236 43 : gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
5237 43 : break;
5238 : }
5239 12952 : }
5240 :
5241 :
5242 : /* Convert intrinsic function calls into result expressions. */
5243 :
5244 : static bool
5245 2244 : gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
5246 : {
5247 2244 : gfc_symbol *sym;
5248 2244 : gfc_expr *new_expr;
5249 2244 : gfc_expr *arg1;
5250 2244 : gfc_expr *arg2;
5251 2244 : int d, dup;
5252 :
5253 2244 : arg1 = expr->value.function.actual->expr;
5254 2244 : if (expr->value.function.actual->next)
5255 2123 : arg2 = expr->value.function.actual->next->expr;
5256 : else
5257 : arg2 = NULL;
5258 :
5259 2244 : sym = arg1->symtree->n.sym;
5260 :
5261 2244 : if (sym->attr.dummy)
5262 : return false;
5263 :
5264 2220 : new_expr = NULL;
5265 :
5266 2220 : switch (expr->value.function.isym->id)
5267 : {
5268 959 : case GFC_ISYM_LEN:
5269 : /* TODO figure out why this condition is necessary. */
5270 959 : if (sym->attr.function
5271 43 : && (arg1->ts.u.cl->length == NULL
5272 42 : || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
5273 42 : && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
5274 : return false;
5275 :
5276 916 : new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
5277 916 : break;
5278 :
5279 228 : case GFC_ISYM_LEN_TRIM:
5280 228 : new_expr = gfc_copy_expr (arg1);
5281 228 : gfc_apply_interface_mapping_to_expr (mapping, new_expr);
5282 :
5283 228 : if (!new_expr)
5284 : return false;
5285 :
5286 228 : gfc_replace_expr (arg1, new_expr);
5287 228 : return true;
5288 :
5289 606 : case GFC_ISYM_SIZE:
5290 606 : if (!sym->as || sym->as->rank == 0)
5291 : return false;
5292 :
5293 530 : if (arg2 && arg2->expr_type == EXPR_CONSTANT)
5294 : {
5295 360 : dup = mpz_get_si (arg2->value.integer);
5296 360 : d = dup - 1;
5297 : }
5298 : else
5299 : {
5300 530 : dup = sym->as->rank;
5301 530 : d = 0;
5302 : }
5303 :
5304 542 : for (; d < dup; d++)
5305 : {
5306 530 : gfc_expr *tmp;
5307 :
5308 530 : if (!sym->as->upper[d] || !sym->as->lower[d])
5309 : {
5310 518 : gfc_free_expr (new_expr);
5311 518 : return false;
5312 : }
5313 :
5314 12 : tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
5315 : gfc_get_int_expr (gfc_default_integer_kind,
5316 : NULL, 1));
5317 12 : tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
5318 12 : if (new_expr)
5319 0 : new_expr = gfc_multiply (new_expr, tmp);
5320 : else
5321 : new_expr = tmp;
5322 : }
5323 : break;
5324 :
5325 44 : case GFC_ISYM_LBOUND:
5326 44 : case GFC_ISYM_UBOUND:
5327 : /* TODO These implementations of lbound and ubound do not limit if
5328 : the size < 0, according to F95's 13.14.53 and 13.14.113. */
5329 :
5330 44 : if (!sym->as || sym->as->rank == 0)
5331 : return false;
5332 :
5333 44 : if (arg2 && arg2->expr_type == EXPR_CONSTANT)
5334 38 : d = mpz_get_si (arg2->value.integer) - 1;
5335 : else
5336 : return false;
5337 :
5338 38 : if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
5339 : {
5340 23 : if (sym->as->lower[d])
5341 23 : new_expr = gfc_copy_expr (sym->as->lower[d]);
5342 : }
5343 : else
5344 : {
5345 15 : if (sym->as->upper[d])
5346 9 : new_expr = gfc_copy_expr (sym->as->upper[d]);
5347 : }
5348 : break;
5349 :
5350 : default:
5351 : break;
5352 : }
5353 :
5354 1349 : gfc_apply_interface_mapping_to_expr (mapping, new_expr);
5355 1349 : if (!new_expr)
5356 : return false;
5357 :
5358 113 : gfc_replace_expr (expr, new_expr);
5359 113 : return true;
5360 : }
5361 :
5362 :
5363 : static void
5364 24 : gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
5365 : gfc_interface_mapping * mapping)
5366 : {
5367 24 : gfc_formal_arglist *f;
5368 24 : gfc_actual_arglist *actual;
5369 :
5370 24 : actual = expr->value.function.actual;
5371 24 : f = gfc_sym_get_dummy_args (map_expr->symtree->n.sym);
5372 :
5373 72 : for (; f && actual; f = f->next, actual = actual->next)
5374 : {
5375 24 : if (!actual->expr)
5376 0 : continue;
5377 :
5378 24 : gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
5379 : }
5380 :
5381 24 : if (map_expr->symtree->n.sym->attr.dimension)
5382 : {
5383 6 : int d;
5384 6 : gfc_array_spec *as;
5385 :
5386 6 : as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
5387 :
5388 18 : for (d = 0; d < as->rank; d++)
5389 : {
5390 6 : gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
5391 6 : gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
5392 : }
5393 :
5394 6 : expr->value.function.esym->as = as;
5395 : }
5396 :
5397 24 : if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
5398 : {
5399 0 : expr->value.function.esym->ts.u.cl->length
5400 0 : = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
5401 :
5402 0 : gfc_apply_interface_mapping_to_expr (mapping,
5403 0 : expr->value.function.esym->ts.u.cl->length);
5404 : }
5405 24 : }
5406 :
5407 :
5408 : /* EXPR is a copy of an expression that appeared in the interface
5409 : associated with MAPPING. Walk it recursively looking for references to
5410 : dummy arguments that MAPPING maps to actual arguments. Replace each such
5411 : reference with a reference to the associated actual argument. */
5412 :
5413 : static void
5414 21656 : gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
5415 : gfc_expr * expr)
5416 : {
5417 23223 : gfc_interface_sym_mapping *sym;
5418 23223 : gfc_actual_arglist *actual;
5419 :
5420 23223 : if (!expr)
5421 : return;
5422 :
5423 : /* Copying an expression does not copy its length, so do that here. */
5424 12952 : if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
5425 : {
5426 1814 : expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
5427 1814 : gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
5428 : }
5429 :
5430 : /* Apply the mapping to any references. */
5431 12952 : gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
5432 :
5433 : /* ...and to the expression's symbol, if it has one. */
5434 : /* TODO Find out why the condition on expr->symtree had to be moved into
5435 : the loop rather than being outside it, as originally. */
5436 30634 : for (sym = mapping->syms; sym; sym = sym->next)
5437 17682 : if (expr->symtree && !strcmp (sym->old->name, expr->symtree->n.sym->name))
5438 : {
5439 3461 : if (sym->new_sym->n.sym->backend_decl)
5440 3417 : expr->symtree = sym->new_sym;
5441 44 : else if (sym->expr)
5442 44 : gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
5443 : }
5444 :
5445 : /* ...and to subexpressions in expr->value. */
5446 12952 : switch (expr->expr_type)
5447 : {
5448 : case EXPR_VARIABLE:
5449 : case EXPR_CONSTANT:
5450 : case EXPR_NULL:
5451 : case EXPR_SUBSTRING:
5452 : break;
5453 :
5454 1567 : case EXPR_OP:
5455 1567 : gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
5456 1567 : gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
5457 1567 : break;
5458 :
5459 0 : case EXPR_CONDITIONAL:
5460 0 : gfc_apply_interface_mapping_to_expr (mapping,
5461 0 : expr->value.conditional.true_expr);
5462 0 : gfc_apply_interface_mapping_to_expr (mapping,
5463 0 : expr->value.conditional.false_expr);
5464 0 : break;
5465 :
5466 2988 : case EXPR_FUNCTION:
5467 9594 : for (actual = expr->value.function.actual; actual; actual = actual->next)
5468 6606 : gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
5469 :
5470 2988 : if (expr->value.function.esym == NULL
5471 2675 : && expr->value.function.isym != NULL
5472 2663 : && expr->value.function.actual
5473 2662 : && expr->value.function.actual->expr
5474 2662 : && expr->value.function.actual->expr->symtree
5475 5232 : && gfc_map_intrinsic_function (expr, mapping))
5476 : break;
5477 :
5478 6228 : for (sym = mapping->syms; sym; sym = sym->next)
5479 3581 : if (sym->old == expr->value.function.esym)
5480 : {
5481 24 : expr->value.function.esym = sym->new_sym->n.sym;
5482 24 : gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
5483 24 : expr->value.function.esym->result = sym->new_sym->n.sym;
5484 : }
5485 : break;
5486 :
5487 47 : case EXPR_ARRAY:
5488 47 : case EXPR_STRUCTURE:
5489 47 : gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
5490 47 : break;
5491 :
5492 0 : case EXPR_COMPCALL:
5493 0 : case EXPR_PPC:
5494 0 : case EXPR_UNKNOWN:
5495 0 : gcc_unreachable ();
5496 : break;
5497 : }
5498 :
5499 : return;
5500 : }
5501 :
5502 :
5503 : /* Evaluate interface expression EXPR using MAPPING. Store the result
5504 : in SE. */
5505 :
5506 : void
5507 4311 : gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
5508 : gfc_se * se, gfc_expr * expr)
5509 : {
5510 4311 : expr = gfc_copy_expr (expr);
5511 4311 : gfc_apply_interface_mapping_to_expr (mapping, expr);
5512 4311 : gfc_conv_expr (se, expr);
5513 4311 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
5514 4311 : gfc_free_expr (expr);
5515 4311 : }
5516 :
5517 :
5518 : /* Returns a reference to a temporary array into which a component of
5519 : an actual argument derived type array is copied and then returned
5520 : after the function call. */
5521 : void
5522 2990 : gfc_conv_subref_array_arg (gfc_se *se, gfc_expr * expr, int g77,
5523 : sym_intent intent, const gfc_symbol *fsym,
5524 : const char *proc_name, gfc_symbol *sym,
5525 : bool check_contiguous, bool deep_copy,
5526 : bool span_only)
5527 : {
5528 2990 : gfc_se lse;
5529 2990 : gfc_se rse;
5530 2990 : gfc_ss *lss;
5531 2990 : gfc_ss *rss;
5532 2990 : gfc_loopinfo loop;
5533 2990 : gfc_loopinfo loop2;
5534 2990 : gfc_array_info *info;
5535 2990 : tree offset;
5536 2990 : tree tmp_index;
5537 2990 : tree tmp;
5538 2990 : tree base_type;
5539 2990 : stmtblock_t body;
5540 2990 : int n;
5541 2990 : int dimen;
5542 2990 : gfc_se work_se;
5543 2990 : gfc_se *parmse;
5544 2990 : bool pass_optional;
5545 2990 : bool readonly;
5546 :
5547 2990 : pass_optional = fsym && fsym->attr.optional && sym && sym->attr.optional;
5548 :
5549 2949 : if (pass_optional || check_contiguous)
5550 : {
5551 1543 : gfc_init_se (&work_se, NULL);
5552 1543 : parmse = &work_se;
5553 : }
5554 : else
5555 : parmse = se;
5556 :
5557 2990 : if (gfc_option.rtcheck & GFC_RTCHECK_ARRAY_TEMPS)
5558 : {
5559 : /* We will create a temporary array, so let us warn. */
5560 868 : char * msg;
5561 :
5562 868 : if (fsym && proc_name)
5563 868 : msg = xasprintf ("An array temporary was created for argument "
5564 868 : "'%s' of procedure '%s'", fsym->name, proc_name);
5565 : else
5566 0 : msg = xasprintf ("An array temporary was created");
5567 :
5568 868 : tmp = build_int_cst (logical_type_node, 1);
5569 868 : gfc_trans_runtime_check (false, true, tmp, &parmse->pre,
5570 : &expr->where, msg);
5571 868 : free (msg);
5572 : }
5573 :
5574 2990 : gfc_init_se (&lse, NULL);
5575 2990 : gfc_init_se (&rse, NULL);
5576 :
5577 : /* Walk the argument expression. */
5578 2990 : rss = gfc_walk_expr (expr);
5579 :
5580 2990 : gcc_assert (rss != gfc_ss_terminator);
5581 :
5582 : /* Initialize the scalarizer. */
5583 2990 : gfc_init_loopinfo (&loop);
5584 2990 : gfc_add_ss_to_loop (&loop, rss);
5585 :
5586 : /* Calculate the bounds of the scalarization. */
5587 2990 : gfc_conv_ss_startstride (&loop);
5588 :
5589 : /* Build an ss for the temporary. */
5590 2990 : if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
5591 136 : gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
5592 :
5593 2990 : base_type = gfc_typenode_for_spec (&expr->ts);
5594 2990 : if (GFC_ARRAY_TYPE_P (base_type)
5595 2990 : || GFC_DESCRIPTOR_TYPE_P (base_type))
5596 0 : base_type = gfc_get_element_type (base_type);
5597 :
5598 2990 : if (expr->ts.type == BT_CLASS)
5599 129 : base_type = gfc_typenode_for_spec (&CLASS_DATA (expr)->ts);
5600 :
5601 4214 : loop.temp_ss = gfc_get_temp_ss (base_type, ((expr->ts.type == BT_CHARACTER)
5602 1224 : ? expr->ts.u.cl->backend_decl
5603 : : NULL),
5604 : loop.dimen);
5605 :
5606 2990 : parmse->string_length = loop.temp_ss->info->string_length;
5607 :
5608 : /* Associate the SS with the loop. */
5609 2990 : gfc_add_ss_to_loop (&loop, loop.temp_ss);
5610 :
5611 : /* Setup the scalarizing loops. */
5612 2990 : gfc_conv_loop_setup (&loop, &expr->where);
5613 :
5614 : /* Pass the temporary descriptor back to the caller. */
5615 2990 : info = &loop.temp_ss->info->data.array;
5616 2990 : parmse->expr = info->descriptor;
5617 :
5618 : /* Setup the gfc_se structures. */
5619 2990 : gfc_copy_loopinfo_to_se (&lse, &loop);
5620 2990 : gfc_copy_loopinfo_to_se (&rse, &loop);
5621 :
5622 2990 : rse.ss = rss;
5623 2990 : lse.ss = loop.temp_ss;
5624 2990 : gfc_mark_ss_chain_used (rss, 1);
5625 2990 : gfc_mark_ss_chain_used (loop.temp_ss, 1);
5626 :
5627 : /* Start the scalarized loop body. */
5628 2990 : gfc_start_scalarized_body (&loop, &body);
5629 :
5630 : /* Translate the expression. */
5631 2990 : gfc_conv_expr (&rse, expr);
5632 :
5633 2990 : gfc_conv_tmp_array_ref (&lse);
5634 :
5635 2990 : if (intent != INTENT_OUT)
5636 : {
5637 2946 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, deep_copy, false);
5638 2946 : gfc_add_expr_to_block (&body, tmp);
5639 2946 : gcc_assert (rse.ss == gfc_ss_terminator);
5640 2946 : gfc_trans_scalarizing_loops (&loop, &body);
5641 : }
5642 : else
5643 : {
5644 : /* Make sure that the temporary declaration survives by merging
5645 : all the loop declarations into the current context. */
5646 97 : for (n = 0; n < loop.dimen; n++)
5647 : {
5648 53 : gfc_merge_block_scope (&body);
5649 53 : body = loop.code[loop.order[n]];
5650 : }
5651 44 : gfc_merge_block_scope (&body);
5652 : }
5653 :
5654 : /* Add the post block after the second loop, so that any
5655 : freeing of allocated memory is done at the right time. */
5656 2990 : gfc_add_block_to_block (&parmse->pre, &loop.pre);
5657 :
5658 : /**********Copy the temporary back again.*********/
5659 :
5660 2990 : gfc_init_se (&lse, NULL);
5661 2990 : gfc_init_se (&rse, NULL);
5662 :
5663 : /* Walk the argument expression. */
5664 2990 : lss = gfc_walk_expr (expr);
5665 2990 : rse.ss = loop.temp_ss;
5666 2990 : lse.ss = lss;
5667 :
5668 : /* Initialize the scalarizer. */
5669 2990 : gfc_init_loopinfo (&loop2);
5670 2990 : gfc_add_ss_to_loop (&loop2, lss);
5671 :
5672 2990 : dimen = rse.ss->dimen;
5673 :
5674 : /* Skip the write-out loop for this case. */
5675 2990 : if (gfc_is_class_array_function (expr))
5676 13 : goto class_array_fcn;
5677 :
5678 : /* Calculate the bounds of the scalarization. */
5679 2977 : gfc_conv_ss_startstride (&loop2);
5680 :
5681 : /* Setup the scalarizing loops. */
5682 2977 : gfc_conv_loop_setup (&loop2, &expr->where);
5683 :
5684 2977 : gfc_copy_loopinfo_to_se (&lse, &loop2);
5685 2977 : gfc_copy_loopinfo_to_se (&rse, &loop2);
5686 :
5687 2977 : gfc_mark_ss_chain_used (lss, 1);
5688 2977 : gfc_mark_ss_chain_used (loop.temp_ss, 1);
5689 :
5690 : /* Declare the variable to hold the temporary offset and start the
5691 : scalarized loop body. */
5692 2977 : offset = gfc_create_var (gfc_array_index_type, NULL);
5693 2977 : gfc_start_scalarized_body (&loop2, &body);
5694 :
5695 : /* Build the offsets for the temporary from the loop variables. The
5696 : temporary array has lbounds of zero and strides of one in all
5697 : dimensions, so this is very simple. The offset is only computed
5698 : outside the innermost loop, so the overall transfer could be
5699 : optimized further. */
5700 2977 : info = &rse.ss->info->data.array;
5701 :
5702 2977 : tmp_index = gfc_index_zero_node;
5703 4378 : for (n = dimen - 1; n > 0; n--)
5704 : {
5705 1401 : tree tmp_str;
5706 1401 : tmp = rse.loop->loopvar[n];
5707 1401 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5708 : tmp, rse.loop->from[n]);
5709 1401 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5710 : tmp, tmp_index);
5711 :
5712 2802 : tmp_str = fold_build2_loc (input_location, MINUS_EXPR,
5713 : gfc_array_index_type,
5714 1401 : rse.loop->to[n-1], rse.loop->from[n-1]);
5715 1401 : tmp_str = fold_build2_loc (input_location, PLUS_EXPR,
5716 : gfc_array_index_type,
5717 : tmp_str, gfc_index_one_node);
5718 :
5719 1401 : tmp_index = fold_build2_loc (input_location, MULT_EXPR,
5720 : gfc_array_index_type, tmp, tmp_str);
5721 : }
5722 :
5723 5954 : tmp_index = fold_build2_loc (input_location, MINUS_EXPR,
5724 : gfc_array_index_type,
5725 2977 : tmp_index, rse.loop->from[0]);
5726 2977 : gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
5727 :
5728 5954 : tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
5729 : gfc_array_index_type,
5730 2977 : rse.loop->loopvar[0], offset);
5731 :
5732 : /* Now use the offset for the reference. */
5733 2977 : tmp = build_fold_indirect_ref_loc (input_location,
5734 : info->data);
5735 2977 : rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
5736 :
5737 2977 : if (expr->ts.type == BT_CHARACTER)
5738 1224 : rse.string_length = expr->ts.u.cl->backend_decl;
5739 :
5740 2977 : gfc_conv_expr (&lse, expr);
5741 :
5742 2977 : gcc_assert (lse.ss == gfc_ss_terminator);
5743 :
5744 : /* Do not do deallocations when we are looking at a g77-style argument. */
5745 :
5746 2977 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, !g77);
5747 2977 : gfc_add_expr_to_block (&body, tmp);
5748 :
5749 : /* Generate the copying loops. */
5750 2977 : gfc_trans_scalarizing_loops (&loop2, &body);
5751 :
5752 : /* Wrap the whole thing up by adding the second loop to the post-block
5753 : and following it by the post-block of the first loop. In this way,
5754 : if the temporary needs freeing, it is done after use!
5755 : If input expr is read-only, e.g. a PARAMETER array, copying back
5756 : modified values is undefined behavior. */
5757 5954 : readonly = (expr->expr_type == EXPR_VARIABLE
5758 2923 : && expr->symtree
5759 5900 : && expr->symtree->n.sym->attr.flavor == FL_PARAMETER);
5760 :
5761 2977 : if ((intent != INTENT_IN) && !readonly)
5762 : {
5763 1232 : gfc_add_block_to_block (&parmse->post, &loop2.pre);
5764 1232 : gfc_add_block_to_block (&parmse->post, &loop2.post);
5765 : }
5766 :
5767 1745 : class_array_fcn:
5768 :
5769 : /* A deep copy allocated fresh components for the temporary; free them
5770 : again once the call has returned, before the temporary itself goes.
5771 : Only INTENT_IN is supported, as writing the temporary back would leave
5772 : the actual argument holding the freed component pointers. */
5773 2990 : gcc_assert (!deep_copy || intent == INTENT_IN);
5774 2990 : if (deep_copy && expr->ts.type == BT_DERIVED
5775 24 : && expr->ts.u.derived->attr.alloc_comp)
5776 : {
5777 24 : tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, parmse->expr,
5778 : dimen);
5779 24 : gfc_add_expr_to_block (&parmse->post, tmp);
5780 : }
5781 :
5782 2990 : gfc_add_block_to_block (&parmse->post, &loop.post);
5783 :
5784 2990 : gfc_cleanup_loop (&loop);
5785 2990 : gfc_cleanup_loop (&loop2);
5786 :
5787 : /* Pass the string length to the argument expression. */
5788 2990 : if (expr->ts.type == BT_CHARACTER)
5789 1224 : parmse->string_length = expr->ts.u.cl->backend_decl;
5790 :
5791 : /* We want either the address for the data or the address of the descriptor,
5792 : depending on the mode of passing array arguments. */
5793 2990 : if (g77)
5794 491 : parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
5795 : else
5796 2499 : parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
5797 :
5798 : /* Basically make this into
5799 :
5800 : if (present)
5801 : {
5802 : if (contiguous)
5803 : {
5804 : pointer = a;
5805 : }
5806 : else
5807 : {
5808 : parmse->pre();
5809 : pointer = parmse->expr;
5810 : }
5811 : }
5812 : else
5813 : pointer = NULL;
5814 :
5815 : foo (pointer);
5816 : if (present && !contiguous)
5817 : se->post();
5818 :
5819 : */
5820 :
5821 2990 : if (pass_optional || check_contiguous)
5822 : {
5823 1543 : tree type;
5824 1543 : stmtblock_t else_block;
5825 1543 : tree pre_stmts, post_stmts;
5826 1543 : tree pointer;
5827 1543 : tree else_stmt;
5828 1543 : tree present_var = NULL_TREE;
5829 1543 : tree cont_var = NULL_TREE;
5830 1543 : tree post_cond;
5831 :
5832 1543 : type = TREE_TYPE (parmse->expr);
5833 1543 : if (POINTER_TYPE_P (type) && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
5834 1177 : type = TREE_TYPE (type);
5835 1543 : pointer = gfc_create_var (type, "arg_ptr");
5836 :
5837 1543 : if (check_contiguous)
5838 : {
5839 1513 : gfc_se cont_se, array_se;
5840 1513 : stmtblock_t if_block, else_block;
5841 1513 : tree if_stmt, else_stmt;
5842 1513 : mpz_t size;
5843 1513 : bool size_set;
5844 :
5845 1513 : cont_var = gfc_create_var (boolean_type_node, "contiguous");
5846 :
5847 : /* If the size is known to be one at compile-time, set
5848 : cont_var to true unconditionally. This may look
5849 : inelegant, but we're only doing this during
5850 : optimization, so the statements will be optimized away,
5851 : and this saves complexity here. */
5852 :
5853 1513 : size_set = gfc_array_size (expr, &size);
5854 1513 : if (size_set && mpz_cmp_ui (size, 1) == 0)
5855 : {
5856 6 : gfc_add_modify (&se->pre, cont_var,
5857 : build_one_cst (boolean_type_node));
5858 : }
5859 : else
5860 : {
5861 : /* cont_var = is_contiguous (expr), or just that the span is the
5862 : element length for a dummy that takes any stride. */
5863 1507 : gfc_init_se (&cont_se, parmse);
5864 1507 : if (span_only)
5865 90 : gfc_conv_span_is_elem_len (&cont_se, expr);
5866 : else
5867 1417 : gfc_conv_is_contiguous_expr (&cont_se, expr);
5868 1507 : gfc_add_block_to_block (&se->pre, &(&cont_se)->pre);
5869 1507 : gfc_add_modify (&se->pre, cont_var, cont_se.expr);
5870 1507 : gfc_add_block_to_block (&se->pre, &(&cont_se)->post);
5871 : }
5872 :
5873 1513 : if (size_set)
5874 1155 : mpz_clear (size);
5875 :
5876 : /* arrayse->expr = descriptor of a. */
5877 1513 : gfc_init_se (&array_se, se);
5878 1513 : gfc_conv_expr_descriptor (&array_se, expr);
5879 1513 : gfc_add_block_to_block (&se->pre, &(&array_se)->pre);
5880 1513 : gfc_add_block_to_block (&se->pre, &(&array_se)->post);
5881 :
5882 : /* if_stmt = { descriptor ? pointer = a : pointer = &a[0]; } . */
5883 1513 : gfc_init_block (&if_block);
5884 1513 : if (GFC_DESCRIPTOR_TYPE_P (type))
5885 1153 : gfc_add_modify (&if_block, pointer, array_se.expr);
5886 : else
5887 : {
5888 360 : tmp = gfc_conv_array_data (array_se.expr);
5889 360 : tmp = fold_convert (type, tmp);
5890 360 : gfc_add_modify (&if_block, pointer, tmp);
5891 : }
5892 1513 : if_stmt = gfc_finish_block (&if_block);
5893 :
5894 : /* else_stmt = { parmse->pre(); pointer = parmse->expr; } . */
5895 1513 : gfc_init_block (&else_block);
5896 1513 : gfc_add_block_to_block (&else_block, &parmse->pre);
5897 1873 : tmp = (GFC_DESCRIPTOR_TYPE_P (type)
5898 1513 : ? build_fold_indirect_ref_loc (input_location, parmse->expr)
5899 : : parmse->expr);
5900 1513 : gfc_add_modify (&else_block, pointer, tmp);
5901 1513 : else_stmt = gfc_finish_block (&else_block);
5902 :
5903 : /* And put the above into an if statement. */
5904 1513 : pre_stmts = fold_build3_loc (input_location, COND_EXPR, void_type_node,
5905 : gfc_likely (cont_var,
5906 : PRED_FORTRAN_CONTIGUOUS),
5907 : if_stmt, else_stmt);
5908 : }
5909 : else
5910 : {
5911 : /* pointer = parmse->expr; . */
5912 36 : tmp = (GFC_DESCRIPTOR_TYPE_P (type)
5913 30 : ? build_fold_indirect_ref_loc (input_location, parmse->expr)
5914 : : parmse->expr);
5915 30 : gfc_add_modify (&parmse->pre, pointer, tmp);
5916 30 : pre_stmts = gfc_finish_block (&parmse->pre);
5917 : }
5918 :
5919 1543 : if (pass_optional)
5920 : {
5921 41 : present_var = gfc_create_var (boolean_type_node, "present");
5922 :
5923 : /* present_var = present(sym); . */
5924 41 : tmp = gfc_conv_expr_present (sym);
5925 41 : tmp = fold_convert (boolean_type_node, tmp);
5926 41 : gfc_add_modify (&se->pre, present_var, tmp);
5927 :
5928 : /* else_stmt = { pointer = NULL; } . */
5929 41 : gfc_init_block (&else_block);
5930 41 : if (GFC_DESCRIPTOR_TYPE_P (type))
5931 24 : gfc_conv_descriptor_data_set (&else_block, pointer,
5932 : null_pointer_node);
5933 : else
5934 17 : gfc_add_modify (&else_block, pointer, build_int_cst (type, 0));
5935 41 : else_stmt = gfc_finish_block (&else_block);
5936 :
5937 41 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
5938 : gfc_likely (present_var,
5939 : PRED_FORTRAN_ABSENT_DUMMY),
5940 : pre_stmts, else_stmt);
5941 41 : gfc_add_expr_to_block (&se->pre, tmp);
5942 : }
5943 : else
5944 1502 : gfc_add_expr_to_block (&se->pre, pre_stmts);
5945 :
5946 1543 : post_stmts = gfc_finish_block (&parmse->post);
5947 :
5948 : /* Put together the post stuff, plus the optional
5949 : deallocation. */
5950 1543 : if (check_contiguous)
5951 : {
5952 : /* !cont_var. */
5953 1513 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
5954 : cont_var,
5955 : build_zero_cst (boolean_type_node));
5956 1513 : tmp = gfc_unlikely (tmp, PRED_FORTRAN_CONTIGUOUS);
5957 :
5958 1513 : if (pass_optional)
5959 : {
5960 11 : tree present_likely = gfc_likely (present_var,
5961 : PRED_FORTRAN_ABSENT_DUMMY);
5962 11 : post_cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
5963 : boolean_type_node, present_likely,
5964 : tmp);
5965 : }
5966 : else
5967 : post_cond = tmp;
5968 : }
5969 : else
5970 : {
5971 30 : gcc_assert (pass_optional);
5972 : post_cond = present_var;
5973 : }
5974 :
5975 1543 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, post_cond,
5976 : post_stmts, build_empty_stmt (input_location));
5977 1543 : gfc_add_expr_to_block (&se->post, tmp);
5978 1543 : if (GFC_DESCRIPTOR_TYPE_P (type))
5979 : {
5980 1177 : type = TREE_TYPE (parmse->expr);
5981 1177 : if (POINTER_TYPE_P (type))
5982 : {
5983 1177 : pointer = gfc_build_addr_expr (type, pointer);
5984 1177 : if (pass_optional)
5985 : {
5986 24 : tmp = gfc_likely (present_var, PRED_FORTRAN_ABSENT_DUMMY);
5987 24 : pointer = fold_build3_loc (input_location, COND_EXPR, type,
5988 : tmp, pointer,
5989 : fold_convert (type,
5990 : null_pointer_node));
5991 : }
5992 : }
5993 : else
5994 0 : gcc_assert (!pass_optional);
5995 : }
5996 1543 : se->expr = pointer;
5997 1543 : se->string_length = parmse->string_length;
5998 : }
5999 :
6000 2990 : return;
6001 : }
6002 :
6003 :
6004 : /* Generate the code for argument list functions. */
6005 :
6006 : static void
6007 5846 : conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
6008 : {
6009 : /* Pass by value for g77 %VAL(arg), pass the address
6010 : indirectly for %LOC, else by reference. Thus %REF
6011 : is a "do-nothing" and %LOC is the same as an F95
6012 : pointer. */
6013 5846 : if (strcmp (name, "%VAL") == 0)
6014 5834 : gfc_conv_expr (se, expr);
6015 12 : else if (strcmp (name, "%LOC") == 0)
6016 : {
6017 6 : gfc_conv_expr_reference (se, expr);
6018 6 : se->expr = gfc_build_addr_expr (NULL, se->expr);
6019 : }
6020 6 : else if (strcmp (name, "%REF") == 0)
6021 6 : gfc_conv_expr_reference (se, expr);
6022 : else
6023 0 : gfc_error ("Unknown argument list function at %L", &expr->where);
6024 5846 : }
6025 :
6026 :
6027 : /* This function tells whether the middle-end representation of the expression
6028 : E given as input may point to data otherwise accessible through a variable
6029 : (sub-)reference.
6030 : It is assumed that the only expressions that may alias are variables,
6031 : and array constructors if ARRAY_MAY_ALIAS is true and some of its elements
6032 : may alias.
6033 : This function is used to decide whether freeing an expression's allocatable
6034 : components is safe or should be avoided.
6035 :
6036 : If ARRAY_MAY_ALIAS is true, an array constructor may alias if some of
6037 : its elements are copied from a variable. This ARRAY_MAY_ALIAS trick
6038 : is necessary because for array constructors, aliasing depends on how
6039 : the array is used:
6040 : - If E is an array constructor used as argument to an elemental procedure,
6041 : the array, which is generated through shallow copy by the scalarizer,
6042 : is used directly and can alias the expressions it was copied from.
6043 : - If E is an array constructor used as argument to a non-elemental
6044 : procedure,the scalarizer is used in gfc_conv_expr_descriptor to generate
6045 : the array as in the previous case, but then that array is used
6046 : to initialize a new descriptor through deep copy. There is no alias
6047 : possible in that case.
6048 : Thus, the ARRAY_MAY_ALIAS flag is necessary to distinguish the two cases
6049 : above. */
6050 :
6051 : static bool
6052 8152 : expr_may_alias_variables (gfc_expr *e, bool array_may_alias)
6053 : {
6054 8152 : gfc_constructor *c;
6055 :
6056 8152 : if (e->expr_type == EXPR_OP
6057 60 : && e->value.op.op == INTRINSIC_PARENTHESES
6058 60 : && e->value.op.op1->expr_type == EXPR_ARRAY)
6059 8152 : e = e->value.op.op1;
6060 :
6061 8152 : if (e->expr_type == EXPR_VARIABLE)
6062 : return true;
6063 725 : else if (e->expr_type == EXPR_FUNCTION)
6064 : {
6065 246 : gfc_symbol *proc_ifc = gfc_get_proc_ifc_for_expr (e);
6066 :
6067 246 : if (proc_ifc->result != NULL
6068 246 : && ((proc_ifc->result->ts.type == BT_CLASS
6069 49 : && proc_ifc->result->ts.u.derived->attr.is_class
6070 49 : && CLASS_DATA (proc_ifc->result)->attr.class_pointer)
6071 246 : || proc_ifc->result->attr.pointer))
6072 : return true;
6073 : else
6074 245 : return false;
6075 : }
6076 479 : else if (e->expr_type != EXPR_ARRAY || !array_may_alias)
6077 : return false;
6078 :
6079 85 : for (c = gfc_constructor_first (e->value.constructor);
6080 239 : c; c = gfc_constructor_next (c))
6081 195 : if (c->expr
6082 195 : && expr_may_alias_variables (c->expr, array_may_alias))
6083 : return true;
6084 :
6085 : return false;
6086 : }
6087 :
6088 :
6089 : /* A helper function to set the dtype for unallocated or unassociated
6090 : entities. */
6091 :
6092 : static void
6093 891 : set_dtype_for_unallocated (gfc_se *parmse, gfc_expr *e)
6094 : {
6095 891 : tree tmp;
6096 891 : tree desc;
6097 891 : tree cond;
6098 891 : tree type;
6099 891 : stmtblock_t block;
6100 :
6101 : /* TODO Figure out how to handle optional dummies. */
6102 891 : if (e && e->expr_type == EXPR_VARIABLE
6103 807 : && e->symtree->n.sym->attr.optional)
6104 108 : return;
6105 :
6106 819 : desc = parmse->expr;
6107 819 : if (desc == NULL_TREE)
6108 : return;
6109 :
6110 819 : if (POINTER_TYPE_P (TREE_TYPE (desc)))
6111 819 : desc = build_fold_indirect_ref_loc (input_location, desc);
6112 819 : if (GFC_CLASS_TYPE_P (TREE_TYPE (desc)))
6113 192 : desc = gfc_class_data_get (desc);
6114 819 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
6115 : return;
6116 :
6117 783 : gfc_init_block (&block);
6118 783 : tmp = gfc_conv_descriptor_data_get (desc);
6119 783 : cond = fold_build2_loc (input_location, EQ_EXPR,
6120 : logical_type_node, tmp,
6121 783 : build_int_cst (TREE_TYPE (tmp), 0));
6122 783 : type = gfc_get_element_type (TREE_TYPE (desc));
6123 783 : gfc_conv_descriptor_dtype_set (&block, desc,
6124 : gfc_get_dtype_rank_type (e->rank, type));
6125 783 : cond = build3_v (COND_EXPR, cond,
6126 : gfc_finish_block (&block),
6127 : build_empty_stmt (input_location));
6128 783 : gfc_add_expr_to_block (&parmse->pre, cond);
6129 : }
6130 :
6131 :
6132 :
6133 : /* Provide an interface between gfortran array descriptors and the F2018:18.4
6134 : ISO_Fortran_binding array descriptors. */
6135 :
6136 : static void
6137 6541 : gfc_conv_gfc_desc_to_cfi_desc (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym)
6138 : {
6139 6541 : stmtblock_t block, block2;
6140 6541 : tree cfi, gfc, tmp, tmp2;
6141 6541 : tree present = NULL;
6142 6541 : tree gfc_strlen = NULL;
6143 6541 : tree rank;
6144 6541 : gfc_se se;
6145 :
6146 6541 : if (fsym->attr.optional
6147 1094 : && e->expr_type == EXPR_VARIABLE
6148 1094 : && e->symtree->n.sym->attr.optional)
6149 103 : present = gfc_conv_expr_present (e->symtree->n.sym);
6150 :
6151 6541 : gfc_init_block (&block);
6152 :
6153 : /* Convert original argument to a tree. */
6154 6541 : gfc_init_se (&se, NULL);
6155 6541 : if (e->rank == 0)
6156 : {
6157 691 : se.want_pointer = 1;
6158 691 : gfc_conv_expr (&se, e);
6159 691 : gfc = se.expr;
6160 : }
6161 : else
6162 : {
6163 : /* If the actual argument can be noncontiguous, copy-in/out is required,
6164 : if the dummy has either the CONTIGUOUS attribute or is an assumed-
6165 : length assumed-length/assumed-size CHARACTER array. This only
6166 : applies if the actual argument is a "variable"; if it's some
6167 : non-lvalue expression, we are going to evaluate it to a
6168 : temporary below anyway. */
6169 5850 : se.force_no_tmp = 1;
6170 5850 : if ((fsym->attr.contiguous
6171 4769 : || (fsym->ts.type == BT_CHARACTER && !fsym->ts.u.cl->length
6172 1375 : && (fsym->as->type == AS_ASSUMED_SIZE
6173 937 : || fsym->as->type == AS_EXPLICIT)))
6174 2023 : && !gfc_is_simply_contiguous (e, false, true)
6175 6883 : && gfc_expr_is_variable (e))
6176 : {
6177 1027 : bool optional = fsym->attr.optional;
6178 1027 : fsym->attr.optional = 0;
6179 1027 : gfc_conv_subref_array_arg (&se, e, false, fsym->attr.intent,
6180 1027 : fsym, fsym->ns->proc_name->name,
6181 : NULL, /* check_contiguous= */ true);
6182 1027 : fsym->attr.optional = optional;
6183 : }
6184 : else
6185 4823 : gfc_conv_expr_descriptor (&se, e);
6186 5850 : gfc = se.expr;
6187 : /* For dt(:)%var, the base_addr is that of the subobject and elem_len is
6188 : its size, see below. The descriptor built for a subreference of the
6189 : array provides both. While sm is fine as it uses span*stride and not
6190 : elem_len. */
6191 5850 : if (POINTER_TYPE_P (TREE_TYPE (gfc)))
6192 1027 : gfc = build_fold_indirect_ref_loc (input_location, gfc);
6193 : }
6194 6541 : if (e->ts.type == BT_CHARACTER)
6195 : {
6196 3409 : if (se.string_length)
6197 : gfc_strlen = se.string_length;
6198 1 : else if (e->ts.u.cl->backend_decl)
6199 : gfc_strlen = e->ts.u.cl->backend_decl;
6200 : else
6201 0 : gcc_unreachable ();
6202 : }
6203 6541 : gfc_add_block_to_block (&block, &se.pre);
6204 :
6205 : /* Create array descriptor and set version, rank, attribute, type. */
6206 12777 : cfi = gfc_create_var (gfc_get_cfi_type (e->rank < 0
6207 : ? GFC_MAX_DIMENSIONS : e->rank,
6208 : false), "cfi");
6209 : /* Convert to CFI_cdesc_t, which has dim[] to avoid TBAA issues,*/
6210 6541 : if (fsym->attr.dimension && fsym->as->type == AS_ASSUMED_RANK)
6211 : {
6212 2520 : tmp = gfc_get_cfi_type (-1, !fsym->attr.pointer && !fsym->attr.target);
6213 2342 : tmp = build_pointer_type (tmp);
6214 2342 : parmse->expr = cfi = gfc_build_addr_expr (tmp, cfi);
6215 2342 : cfi = build_fold_indirect_ref_loc (input_location, cfi);
6216 : }
6217 : else
6218 4199 : parmse->expr = gfc_build_addr_expr (NULL, cfi);
6219 :
6220 6541 : tmp = gfc_get_cfi_desc_version (cfi);
6221 6541 : gfc_add_modify (&block, tmp,
6222 6541 : build_int_cst (TREE_TYPE (tmp), CFI_VERSION));
6223 6541 : if (e->rank < 0)
6224 305 : rank = gfc_conv_descriptor_rank_get (gfc);
6225 : else
6226 6236 : rank = gfc_rank_cst[e->rank];
6227 6541 : tmp = gfc_get_cfi_desc_rank (cfi);
6228 6541 : gfc_add_modify (&block, tmp,
6229 6541 : fold_convert (TREE_TYPE (tmp), rank));
6230 6541 : int itype = CFI_type_other;
6231 6541 : if (e->ts.f90_type == BT_VOID)
6232 96 : itype = (e->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
6233 96 : ? CFI_type_cfunptr : CFI_type_cptr);
6234 : else
6235 : {
6236 6445 : if (e->expr_type == EXPR_NULL && e->ts.type == BT_UNKNOWN)
6237 1 : e->ts = fsym->ts;
6238 6445 : switch (e->ts.type)
6239 : {
6240 2296 : case BT_INTEGER:
6241 2296 : case BT_LOGICAL:
6242 2296 : case BT_REAL:
6243 2296 : case BT_COMPLEX:
6244 2296 : itype = CFI_type_from_type_kind (e->ts.type, e->ts.kind);
6245 2296 : break;
6246 3410 : case BT_CHARACTER:
6247 3410 : itype = CFI_type_from_type_kind (CFI_type_Character, e->ts.kind);
6248 3410 : break;
6249 : case BT_DERIVED:
6250 6541 : itype = CFI_type_struct;
6251 : break;
6252 0 : case BT_VOID:
6253 0 : itype = (e->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
6254 0 : ? CFI_type_cfunptr : CFI_type_cptr);
6255 : break;
6256 : case BT_ASSUMED:
6257 : itype = CFI_type_other; // FIXME: Or CFI_type_cptr ?
6258 : break;
6259 1 : case BT_CLASS:
6260 1 : if (fsym->ts.type == BT_ASSUMED)
6261 : {
6262 : // F2017: 7.3.2.2: "An entity that is declared using the TYPE(*)
6263 : // type specifier is assumed-type and is an unlimited polymorphic
6264 : // entity." The actual argument _data component is passed.
6265 : itype = CFI_type_other; // FIXME: Or CFI_type_cptr ?
6266 : break;
6267 : }
6268 : else
6269 0 : gcc_unreachable ();
6270 :
6271 0 : case BT_UNSIGNED:
6272 0 : gfc_internal_error ("Unsigned not yet implemented");
6273 :
6274 0 : case BT_PROCEDURE:
6275 0 : case BT_HOLLERITH:
6276 0 : case BT_UNION:
6277 0 : case BT_BOZ:
6278 0 : case BT_UNKNOWN:
6279 : // FIXME: Really unreachable? Or reachable for type(*) ? If so, CFI_type_other?
6280 0 : gcc_unreachable ();
6281 : }
6282 : }
6283 :
6284 6541 : tmp = gfc_get_cfi_desc_type (cfi);
6285 6541 : gfc_add_modify (&block, tmp,
6286 6541 : build_int_cst (TREE_TYPE (tmp), itype));
6287 :
6288 6541 : int attr = CFI_attribute_other;
6289 6541 : if (fsym->attr.pointer)
6290 : attr = CFI_attribute_pointer;
6291 5778 : else if (fsym->attr.allocatable)
6292 433 : attr = CFI_attribute_allocatable;
6293 6541 : tmp = gfc_get_cfi_desc_attribute (cfi);
6294 6541 : gfc_add_modify (&block, tmp,
6295 6541 : build_int_cst (TREE_TYPE (tmp), attr));
6296 :
6297 : /* The cfi-base_addr assignment could be skipped for 'pointer, intent(out)'.
6298 : That is very sensible for undefined pointers, but the C code might assume
6299 : that the pointer retains the value, in particular, if it was NULL. */
6300 6541 : if (e->rank == 0)
6301 : {
6302 691 : tmp = gfc_get_cfi_desc_base_addr (cfi);
6303 691 : gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), gfc));
6304 : }
6305 : else
6306 : {
6307 5850 : tmp = gfc_get_cfi_desc_base_addr (cfi);
6308 5850 : tmp2 = gfc_conv_descriptor_data_get (gfc);
6309 5850 : gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), tmp2));
6310 : }
6311 :
6312 : /* Set elem_len if known - must be before the next if block.
6313 : Note that allocatable implies 'len=:'. */
6314 6541 : if (e->ts.type != BT_ASSUMED && e->ts.type != BT_CHARACTER )
6315 : {
6316 : /* Length is known at compile time; use 'block' for it. */
6317 3077 : tmp = size_in_bytes (gfc_typenode_for_spec (&e->ts));
6318 3077 : tmp2 = gfc_get_cfi_desc_elem_len (cfi);
6319 3077 : gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
6320 : }
6321 :
6322 6541 : if (fsym->attr.pointer && fsym->attr.intent == INTENT_OUT)
6323 91 : goto done;
6324 :
6325 : /* When allocatable + intent out, free the cfi descriptor. */
6326 6450 : if (fsym->attr.allocatable && fsym->attr.intent == INTENT_OUT)
6327 : {
6328 90 : tmp = gfc_get_cfi_desc_base_addr (cfi);
6329 90 : tree call = builtin_decl_explicit (BUILT_IN_FREE);
6330 90 : call = build_call_expr_loc (input_location, call, 1, tmp);
6331 90 : gfc_add_expr_to_block (&block, fold_convert (void_type_node, call));
6332 90 : gfc_add_modify (&block, tmp,
6333 90 : fold_convert (TREE_TYPE (tmp), null_pointer_node));
6334 90 : goto done;
6335 : }
6336 :
6337 : /* If not unallocated/unassociated. */
6338 6360 : gfc_init_block (&block2);
6339 :
6340 : /* Set elem_len, which may be only known at run time. */
6341 6360 : if (e->ts.type == BT_CHARACTER
6342 3410 : && (e->expr_type != EXPR_NULL || gfc_strlen != NULL_TREE))
6343 : {
6344 3408 : gcc_assert (gfc_strlen);
6345 3409 : tmp = gfc_strlen;
6346 3409 : if (e->ts.kind != 1)
6347 1117 : tmp = fold_build2_loc (input_location, MULT_EXPR,
6348 : gfc_charlen_type_node, tmp,
6349 : build_int_cst (gfc_charlen_type_node,
6350 1117 : e->ts.kind));
6351 3409 : tmp2 = gfc_get_cfi_desc_elem_len (cfi);
6352 3409 : gfc_add_modify (&block2, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
6353 : }
6354 2951 : else if (e->ts.type == BT_ASSUMED)
6355 : {
6356 54 : tmp = gfc_conv_descriptor_elem_len_get (gfc);
6357 54 : tmp2 = gfc_get_cfi_desc_elem_len (cfi);
6358 54 : gfc_add_modify (&block2, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
6359 : }
6360 :
6361 6360 : if (e->ts.type == BT_ASSUMED)
6362 : {
6363 : /* Note: type(*) implies assumed-shape/assumed-rank if fsym requires
6364 : an CFI descriptor. Use the type in the descriptor as it provide
6365 : mode information. (Quality of implementation feature.) */
6366 54 : tree cond;
6367 54 : tree ctype = gfc_get_cfi_desc_type (cfi);
6368 54 : tree type = fold_convert (TREE_TYPE (ctype),
6369 : gfc_conv_descriptor_type_get (gfc));
6370 54 : tree kind = fold_convert (TREE_TYPE (ctype),
6371 : gfc_conv_descriptor_elem_len_get (gfc));
6372 54 : kind = fold_build2_loc (input_location, LSHIFT_EXPR, TREE_TYPE (type),
6373 54 : kind, build_int_cst (TREE_TYPE (type),
6374 : CFI_type_kind_shift));
6375 :
6376 : /* if (BT_VOID) CFI_type_cptr else CFI_type_other */
6377 : /* Note: BT_VOID is could also be CFI_type_funcptr, but assume c_ptr. */
6378 54 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6379 54 : build_int_cst (TREE_TYPE (type), BT_VOID));
6380 54 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, ctype,
6381 54 : build_int_cst (TREE_TYPE (type), CFI_type_cptr));
6382 54 : tmp2 = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
6383 : ctype,
6384 54 : build_int_cst (TREE_TYPE (type), CFI_type_other));
6385 54 : tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6386 : tmp, tmp2);
6387 : /* if (BT_DERIVED) CFI_type_struct else < tmp2 > */
6388 54 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6389 54 : build_int_cst (TREE_TYPE (type), BT_DERIVED));
6390 54 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, ctype,
6391 54 : build_int_cst (TREE_TYPE (type), CFI_type_struct));
6392 54 : tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6393 : tmp, tmp2);
6394 : /* if (BT_CHARACTER) CFI_type_Character + kind=1 else < tmp2 > */
6395 : /* Note: could also be kind=4, with cfi->elem_len = gfc->elem_len*4. */
6396 54 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6397 54 : build_int_cst (TREE_TYPE (type), BT_CHARACTER));
6398 54 : tmp = build_int_cst (TREE_TYPE (type),
6399 : CFI_type_from_type_kind (CFI_type_Character, 1));
6400 54 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
6401 : ctype, tmp);
6402 54 : tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6403 : tmp, tmp2);
6404 : /* if (BT_COMPLEX) CFI_type_Complex + kind/2 else < tmp2 > */
6405 54 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6406 54 : build_int_cst (TREE_TYPE (type), BT_COMPLEX));
6407 54 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR, TREE_TYPE (type),
6408 54 : kind, build_int_cst (TREE_TYPE (type), 2));
6409 54 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (type), tmp,
6410 54 : build_int_cst (TREE_TYPE (type),
6411 : CFI_type_Complex));
6412 54 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
6413 : ctype, tmp);
6414 54 : tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6415 : tmp, tmp2);
6416 : /* if (BT_INTEGER || BT_LOGICAL || BT_REAL) type + kind else <tmp2> */
6417 54 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6418 54 : build_int_cst (TREE_TYPE (type), BT_INTEGER));
6419 54 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6420 54 : build_int_cst (TREE_TYPE (type), BT_LOGICAL));
6421 54 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
6422 : cond, tmp);
6423 54 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6424 54 : build_int_cst (TREE_TYPE (type), BT_REAL));
6425 54 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
6426 : cond, tmp);
6427 54 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (type),
6428 : type, kind);
6429 54 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
6430 : ctype, tmp);
6431 54 : tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6432 : tmp, tmp2);
6433 54 : gfc_add_expr_to_block (&block2, tmp2);
6434 : }
6435 :
6436 6360 : if (e->rank != 0)
6437 : {
6438 : /* Loop: for (i = 0; i < rank; ++i). */
6439 5735 : tree idx = gfc_create_var (gfc_array_dim_rank_type, "idx");
6440 : /* Loop body. */
6441 5735 : stmtblock_t loop_body;
6442 5735 : gfc_init_block (&loop_body);
6443 : /* cfi->dim[i].lower_bound = (allocatable/pointer)
6444 : ? gfc->dim[i].lbound : 0 */
6445 5735 : if (fsym->attr.pointer || fsym->attr.allocatable)
6446 648 : tmp = gfc_conv_descriptor_lbound_get (gfc, idx);
6447 : else
6448 5087 : tmp = gfc_index_zero_node;
6449 5735 : gfc_add_modify (&loop_body, gfc_get_cfi_dim_lbound (cfi, idx), tmp);
6450 : /* cfi->dim[i].extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1. */
6451 5735 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6452 : gfc_conv_descriptor_ubound_get (gfc, idx),
6453 : gfc_conv_descriptor_lbound_get (gfc, idx));
6454 5735 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
6455 : tmp, gfc_index_one_node);
6456 5735 : gfc_add_modify (&loop_body, gfc_get_cfi_dim_extent (cfi, idx), tmp);
6457 : /* d->dim[n].sm = gfc->dim[i].stride * gfc->span); */
6458 5735 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6459 : gfc_conv_descriptor_stride_get (gfc, idx),
6460 : gfc_conv_descriptor_span_get (gfc));
6461 5735 : gfc_add_modify (&loop_body, gfc_get_cfi_dim_sm (cfi, idx), tmp);
6462 :
6463 : /* Generate loop. */
6464 5735 : gfc_simple_for_loop (&block2, idx, gfc_rank_cst[0], rank, LT_EXPR,
6465 : gfc_rank_cst[1], gfc_finish_block (&loop_body));
6466 :
6467 5735 : if (e->expr_type == EXPR_VARIABLE
6468 5573 : && e->ref
6469 5573 : && e->ref->u.ar.type == AR_FULL
6470 2732 : && e->symtree->n.sym->attr.dummy
6471 988 : && e->symtree->n.sym->as
6472 988 : && e->symtree->n.sym->as->type == AS_ASSUMED_SIZE)
6473 : {
6474 138 : tmp = gfc_get_cfi_dim_extent (cfi, gfc_rank_cst[e->rank-1]),
6475 138 : gfc_add_modify (&block2, tmp, build_int_cst (TREE_TYPE (tmp), -1));
6476 : }
6477 : }
6478 :
6479 6360 : if (fsym->attr.allocatable || fsym->attr.pointer)
6480 : {
6481 1015 : tmp = gfc_get_cfi_desc_base_addr (cfi),
6482 1015 : tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
6483 : tmp, null_pointer_node);
6484 1015 : tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
6485 : build_empty_stmt (input_location));
6486 1015 : gfc_add_expr_to_block (&block, tmp);
6487 : }
6488 : else
6489 5345 : gfc_add_block_to_block (&block, &block2);
6490 :
6491 :
6492 6541 : done:
6493 6541 : if (present)
6494 : {
6495 103 : parmse->expr = build3_loc (input_location, COND_EXPR,
6496 103 : TREE_TYPE (parmse->expr),
6497 : present, parmse->expr, null_pointer_node);
6498 103 : tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block),
6499 : build_empty_stmt (input_location));
6500 103 : gfc_add_expr_to_block (&parmse->pre, tmp);
6501 : }
6502 : else
6503 6438 : gfc_add_block_to_block (&parmse->pre, &block);
6504 :
6505 6541 : gfc_init_block (&block);
6506 :
6507 6541 : if ((!fsym->attr.allocatable && !fsym->attr.pointer)
6508 1196 : || fsym->attr.intent == INTENT_IN)
6509 5554 : goto post_call;
6510 :
6511 987 : gfc_init_block (&block2);
6512 987 : if (e->rank == 0)
6513 : {
6514 428 : tmp = gfc_get_cfi_desc_base_addr (cfi);
6515 428 : gfc_add_modify (&block, gfc, fold_convert (TREE_TYPE (gfc), tmp));
6516 : }
6517 : else
6518 : {
6519 559 : tmp = gfc_get_cfi_desc_base_addr (cfi);
6520 559 : gfc_conv_descriptor_data_set (&block, gfc, tmp);
6521 :
6522 559 : if (fsym->attr.allocatable)
6523 : {
6524 : /* gfc->span = cfi->elem_len. */
6525 252 : tmp = fold_convert (gfc_array_index_type,
6526 : gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]));
6527 : }
6528 : else
6529 : {
6530 : /* gfc->span = ((cfi->dim[0].sm % cfi->elem_len)
6531 : ? cfi->dim[0].sm : cfi->elem_len). */
6532 307 : tmp = gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]);
6533 307 : tmp2 = fold_convert (gfc_array_index_type,
6534 : gfc_get_cfi_desc_elem_len (cfi));
6535 307 : tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR,
6536 : gfc_array_index_type, tmp, tmp2);
6537 307 : tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
6538 : tmp, gfc_index_zero_node);
6539 307 : tmp = build3_loc (input_location, COND_EXPR, gfc_array_index_type, tmp,
6540 : gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]), tmp2);
6541 : }
6542 559 : gfc_conv_descriptor_span_set (&block2, gfc, tmp);
6543 :
6544 : /* Calculate offset + set lbound, ubound and stride. */
6545 559 : gfc_conv_descriptor_offset_set (&block2, gfc, gfc_index_zero_node);
6546 : /* Loop: for (i = 0; i < rank; ++i). */
6547 559 : tree idx = gfc_create_var (gfc_array_dim_rank_type, "idx");
6548 : /* Loop body. */
6549 559 : stmtblock_t loop_body;
6550 559 : gfc_init_block (&loop_body);
6551 : /* gfc->dim[i].lbound = ... */
6552 559 : tmp = gfc_get_cfi_dim_lbound (cfi, idx);
6553 559 : gfc_conv_descriptor_lbound_set (&loop_body, gfc, idx, tmp);
6554 :
6555 : /* gfc->dim[i].ubound = gfc->dim[i].lbound + cfi->dim[i].extent - 1. */
6556 559 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6557 : gfc_conv_descriptor_lbound_get (gfc, idx),
6558 : gfc_index_one_node);
6559 559 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
6560 : gfc_get_cfi_dim_extent (cfi, idx), tmp);
6561 559 : gfc_conv_descriptor_ubound_set (&loop_body, gfc, idx, tmp);
6562 :
6563 : /* gfc->dim[i].stride = cfi->dim[i].sm / cfi>elem_len */
6564 559 : tmp = gfc_get_cfi_dim_sm (cfi, idx);
6565 559 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
6566 : gfc_array_index_type, tmp,
6567 : fold_convert (gfc_array_index_type,
6568 : gfc_get_cfi_desc_elem_len (cfi)));
6569 559 : gfc_conv_descriptor_stride_set (&loop_body, gfc, idx, tmp);
6570 :
6571 : /* gfc->offset -= gfc->dim[i].stride * gfc->dim[i].lbound. */
6572 559 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6573 : gfc_conv_descriptor_stride_get (gfc, idx),
6574 : gfc_conv_descriptor_lbound_get (gfc, idx));
6575 559 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6576 : gfc_conv_descriptor_offset_get (gfc), tmp);
6577 559 : gfc_conv_descriptor_offset_set (&loop_body, gfc, tmp);
6578 : /* Generate loop. */
6579 559 : gfc_simple_for_loop (&block2, idx, gfc_rank_cst[0], rank, LT_EXPR,
6580 : gfc_rank_cst[1], gfc_finish_block (&loop_body));
6581 : }
6582 :
6583 987 : if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
6584 : {
6585 60 : tmp = fold_convert (gfc_charlen_type_node,
6586 : gfc_get_cfi_desc_elem_len (cfi));
6587 60 : if (e->ts.kind != 1)
6588 24 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
6589 : gfc_charlen_type_node, tmp,
6590 : build_int_cst (gfc_charlen_type_node,
6591 24 : e->ts.kind));
6592 60 : gfc_add_modify (&block2, gfc_strlen, tmp);
6593 : }
6594 :
6595 987 : tmp = gfc_get_cfi_desc_base_addr (cfi),
6596 987 : tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
6597 : tmp, null_pointer_node);
6598 987 : tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
6599 : build_empty_stmt (input_location));
6600 987 : gfc_add_expr_to_block (&block, tmp);
6601 :
6602 6541 : post_call:
6603 6541 : gfc_add_block_to_block (&block, &se.post);
6604 6541 : if (present && block.head)
6605 : {
6606 6 : tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block),
6607 : build_empty_stmt (input_location));
6608 6 : gfc_add_expr_to_block (&parmse->post, tmp);
6609 : }
6610 6535 : else if (block.head)
6611 1564 : gfc_add_block_to_block (&parmse->post, &block);
6612 6541 : }
6613 :
6614 :
6615 : /* Create "conditional temporary" to handle scalar dummy variables with the
6616 : OPTIONAL+VALUE attribute that shall not be dereferenced. Use null value
6617 : as fallback. Does not handle CLASS. */
6618 :
6619 : static void
6620 252 : conv_cond_temp (gfc_se * parmse, gfc_expr * e, tree cond)
6621 : {
6622 252 : tree temp;
6623 252 : gcc_assert (e && e->ts.type != BT_CLASS);
6624 252 : gcc_assert (e->rank == 0);
6625 252 : temp = gfc_create_var (TREE_TYPE (parmse->expr), "condtemp");
6626 252 : TREE_STATIC (temp) = 1;
6627 252 : TREE_CONSTANT (temp) = 1;
6628 252 : TREE_READONLY (temp) = 1;
6629 252 : DECL_INITIAL (temp) = build_zero_cst (TREE_TYPE (temp));
6630 252 : parmse->expr = fold_build3_loc (input_location, COND_EXPR,
6631 252 : TREE_TYPE (parmse->expr),
6632 : cond, parmse->expr, temp);
6633 252 : parmse->expr = gfc_evaluate_now (parmse->expr, &parmse->pre);
6634 252 : }
6635 :
6636 :
6637 : /* Returns true if the type specified in TS is a character type whose length
6638 : is constant. Otherwise returns false. */
6639 :
6640 : static bool
6641 22437 : gfc_const_length_character_type_p (gfc_typespec *ts)
6642 : {
6643 22437 : return (ts->type == BT_CHARACTER
6644 515 : && ts->u.cl
6645 515 : && ts->u.cl->length
6646 479 : && ts->u.cl->length->expr_type == EXPR_CONSTANT
6647 22904 : && ts->u.cl->length->ts.type == BT_INTEGER);
6648 : }
6649 :
6650 :
6651 : /* Returns true if FORMAL contains an explicit-shape array dummy with the
6652 : VALUE attribute. The bounds of such a dummy may have to be evaluated
6653 : on the caller side, which needs an interface mapping. */
6654 :
6655 : static bool
6656 117716 : has_value_array_dummy (gfc_formal_arglist *formal)
6657 : {
6658 314472 : for (; formal; formal = formal->next)
6659 196822 : if (formal->sym && formal->sym->attr.value && formal->sym->attr.dimension
6660 174 : && formal->sym->as && formal->sym->as->type == AS_EXPLICIT)
6661 : return true;
6662 :
6663 : return false;
6664 : }
6665 :
6666 :
6667 : /* Sequence association (F2023, 15.5.2.12) of a scalar actual argument E with
6668 : an explicit-shape array dummy FSYM that has the VALUE attribute. Copy as
6669 : many elements as the dummy declares into a temporary and pass that.
6670 : MAPPING supplies the caller-side values of any dummy arguments appearing
6671 : in the bounds or the character length of FSYM. */
6672 :
6673 : static void
6674 36 : conv_seq_assoc_value_arg (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym,
6675 : gfc_interface_mapping *mapping)
6676 : {
6677 36 : tree nelems, elem_type, elem_size, tmpvar, src, tmp;
6678 36 : gfc_se se;
6679 36 : int n;
6680 :
6681 36 : gcc_assert (fsym->as && fsym->as->type == AS_EXPLICIT);
6682 :
6683 : /* Address of the first element of the actual argument's sequence. */
6684 36 : gfc_init_se (&se, NULL);
6685 36 : if (e->ts.type == BT_CHARACTER)
6686 : {
6687 12 : gfc_conv_expr (&se, e);
6688 12 : gfc_conv_string_parameter (&se);
6689 : /* The hidden length argument is that of the actual argument, as it
6690 : is for a dummy that does not have the VALUE attribute. */
6691 12 : parmse->string_length = se.string_length;
6692 : }
6693 : else
6694 24 : gfc_conv_expr_reference (&se, e);
6695 36 : gfc_add_block_to_block (&parmse->pre, &se.pre);
6696 36 : gfc_add_block_to_block (&parmse->post, &se.post);
6697 36 : src = se.expr;
6698 :
6699 : /* Number of elements of the dummy. */
6700 36 : nelems = gfc_index_one_node;
6701 78 : for (n = 0; n < fsym->as->rank; n++)
6702 : {
6703 42 : tree lbound, ubound, extent;
6704 :
6705 42 : gfc_init_se (&se, NULL);
6706 42 : gfc_apply_interface_mapping (mapping, &se, fsym->as->upper[n]);
6707 42 : gfc_add_block_to_block (&parmse->pre, &se.pre);
6708 42 : gfc_add_block_to_block (&parmse->post, &se.post);
6709 42 : ubound = fold_convert (gfc_array_index_type, se.expr);
6710 :
6711 42 : if (fsym->as->lower[n])
6712 : {
6713 42 : gfc_init_se (&se, NULL);
6714 42 : gfc_apply_interface_mapping (mapping, &se, fsym->as->lower[n]);
6715 42 : gfc_add_block_to_block (&parmse->pre, &se.pre);
6716 42 : gfc_add_block_to_block (&parmse->post, &se.post);
6717 42 : lbound = fold_convert (gfc_array_index_type, se.expr);
6718 : }
6719 : else
6720 0 : lbound = gfc_index_one_node;
6721 :
6722 42 : extent = fold_build2_loc (input_location, MINUS_EXPR,
6723 : gfc_array_index_type, ubound, lbound);
6724 42 : extent = fold_build2_loc (input_location, PLUS_EXPR,
6725 : gfc_array_index_type, extent,
6726 : gfc_index_one_node);
6727 42 : extent = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
6728 : extent, gfc_index_zero_node);
6729 42 : nelems = fold_build2_loc (input_location, MULT_EXPR,
6730 : gfc_array_index_type, nelems, extent);
6731 : }
6732 36 : nelems = gfc_evaluate_now (nelems, &parmse->pre);
6733 :
6734 : /* Element type and size of the dummy. For characters the element
6735 : sequence is grouped by the character length of the dummy. */
6736 36 : if (fsym->ts.type == BT_CHARACTER)
6737 : {
6738 12 : tree len;
6739 :
6740 12 : if (fsym->ts.u.cl->length)
6741 : {
6742 12 : gfc_init_se (&se, NULL);
6743 12 : gfc_apply_interface_mapping (mapping, &se, fsym->ts.u.cl->length);
6744 12 : gfc_add_block_to_block (&parmse->pre, &se.pre);
6745 12 : gfc_add_block_to_block (&parmse->post, &se.post);
6746 12 : len = fold_convert (gfc_charlen_type_node, se.expr);
6747 : }
6748 : else
6749 0 : len = fold_convert (gfc_charlen_type_node, parmse->string_length);
6750 :
6751 12 : tree char_size = TYPE_SIZE_UNIT (gfc_get_char_type (fsym->ts.kind));
6752 :
6753 12 : elem_type = gfc_get_character_type_len (fsym->ts.kind, len);
6754 12 : elem_size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
6755 : fold_convert (size_type_node, len),
6756 : fold_convert (size_type_node, char_size));
6757 : }
6758 : else
6759 : {
6760 24 : elem_type = gfc_typenode_for_spec (&fsym->ts);
6761 24 : elem_size = fold_convert (size_type_node, TYPE_SIZE_UNIT (elem_type));
6762 : }
6763 :
6764 : /* The temporary holding the copy. Allocate at least one element so that
6765 : a zero-sized dummy does not produce a degenerate array type. */
6766 36 : tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
6767 : nelems, gfc_index_one_node);
6768 36 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6769 : tmp, gfc_index_one_node);
6770 36 : tmp = build_array_type (elem_type,
6771 : build_range_type (gfc_array_index_type,
6772 : gfc_index_zero_node, tmp));
6773 36 : tmpvar = gfc_create_var (tmp, "seq_copy");
6774 36 : gfc_add_expr_to_block (&parmse->pre,
6775 : fold_build1_loc (input_location, DECL_EXPR, tmp,
6776 : tmpvar));
6777 :
6778 36 : tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
6779 : fold_convert (size_type_node, nelems), elem_size);
6780 36 : tmp = gfc_build_memcpy_call (fold_convert (pvoid_type_node,
6781 : gfc_build_addr_expr (NULL_TREE,
6782 : tmpvar)),
6783 : fold_convert (pvoid_type_node, src), tmp);
6784 36 : gfc_add_expr_to_block (&parmse->pre, tmp);
6785 :
6786 : /* The memcpy also copied the component pointers of a derived type, which
6787 : would leave the temporary sharing the actual argument's allocatable
6788 : components. Give the copy components of its own and free them again
6789 : once the call has returned. */
6790 36 : if (fsym->ts.type == BT_DERIVED && fsym->ts.u.derived->attr.alloc_comp)
6791 : {
6792 6 : tree src_ptr = fold_convert (build_pointer_type (elem_type), src);
6793 6 : tree elem_idx = gfc_create_var (gfc_array_index_type, "elem");
6794 6 : tree dest_elem = gfc_build_array_ref (tmpvar, elem_idx, NULL_TREE);
6795 6 : tree src_offset = fold_build2_loc (input_location, MULT_EXPR, sizetype,
6796 : fold_convert (sizetype, elem_idx),
6797 : elem_size);
6798 6 : tree src_elem
6799 6 : = build_fold_indirect_ref_loc (input_location,
6800 : fold_build_pointer_plus_loc
6801 : (input_location, src_ptr, src_offset));
6802 :
6803 6 : tmp = gfc_copy_alloc_comp (fsym->ts.u.derived, src_elem, dest_elem, 0, 0);
6804 6 : gfc_simple_for_loop (&parmse->pre, elem_idx, gfc_index_zero_node, nelems,
6805 : LT_EXPR, gfc_index_one_node, tmp);
6806 :
6807 6 : tmp = gfc_deallocate_alloc_comp (fsym->ts.u.derived, dest_elem, 0);
6808 6 : gfc_simple_for_loop (&parmse->post, elem_idx, gfc_index_zero_node, nelems,
6809 : LT_EXPR, gfc_index_one_node, tmp);
6810 : }
6811 :
6812 36 : if (fsym->ts.type == BT_CHARACTER)
6813 12 : parmse->expr
6814 12 : = gfc_build_addr_expr (build_pointer_type (gfc_get_char_type
6815 : (fsym->ts.kind)), tmpvar);
6816 : else
6817 24 : parmse->expr = gfc_build_addr_expr (build_pointer_type (elem_type), tmpvar);
6818 36 : }
6819 :
6820 :
6821 : /* Whether actual argument E can share storage with a variable, so that a
6822 : VALUE dummy needs a copy; any other value is freed after the call. */
6823 :
6824 : static bool
6825 174 : value_actual_shares_storage (gfc_expr *e)
6826 : {
6827 174 : return (expr_may_alias_variables (e, true)
6828 174 : || (e->ts.type == BT_DERIVED && parenthesized_variable (e)));
6829 : }
6830 :
6831 :
6832 : /* Pass the class container in PARMSE->EXPR, or its address, to a scalar
6833 : CLASS dummy with VALUE: a copy if actual argument E can share storage
6834 : with a variable, otherwise the container itself. */
6835 :
6836 : static void
6837 60 : conv_class_value_arg (gfc_se *parmse, gfc_expr *e)
6838 : {
6839 60 : if (value_actual_shares_storage (e))
6840 36 : conv_class_value_copy (parmse);
6841 24 : else if (POINTER_TYPE_P (TREE_TYPE (parmse->expr)))
6842 6 : parmse->expr = build_fold_indirect_ref_loc (input_location,
6843 : parmse->expr);
6844 60 : }
6845 :
6846 :
6847 : /* Helper function for the handling of (currently) scalar dummy variables
6848 : with the VALUE attribute. Argument parmse should already be set up. */
6849 : static void
6850 22895 : conv_dummy_value (gfc_se * parmse, gfc_expr * e, gfc_symbol * fsym,
6851 : vec<tree, va_gc> *& optionalargs)
6852 : {
6853 22895 : tree tmp;
6854 :
6855 22895 : gcc_assert (fsym && fsym->attr.value && !fsym->attr.dimension);
6856 :
6857 22895 : if (IS_PDT (e))
6858 : {
6859 12 : tmp = gfc_create_var (TREE_TYPE (parmse->expr), "PDT");
6860 12 : gfc_add_modify (&parmse->pre, tmp, parmse->expr);
6861 12 : gfc_add_expr_to_block (&parmse->pre,
6862 12 : gfc_copy_alloc_comp (e->ts.u.derived,
6863 : parmse->expr, tmp,
6864 : e->rank, 0));
6865 12 : parmse->expr = gfc_value_dummy_passed_byref (fsym)
6866 12 : ? gfc_build_addr_expr (NULL_TREE, tmp) : tmp;
6867 12 : tmp = gfc_deallocate_pdt_comp (e->ts.u.derived, tmp, e->rank);
6868 12 : gfc_add_expr_to_block (&parmse->post, tmp);
6869 12 : return;
6870 : }
6871 :
6872 : /* Absent actual argument for optional scalar dummy. */
6873 22883 : if ((e == NULL || e->expr_type == EXPR_NULL) && fsym->attr.optional)
6874 : {
6875 : /* For scalar arguments with VALUE attribute which are passed by
6876 : value, pass "0" and a hidden argument for the optional status. */
6877 446 : if (fsym->ts.type == BT_CHARACTER)
6878 : {
6879 : /* Pass a NULL pointer for an absent CHARACTER arg and a length of
6880 : zero. */
6881 102 : parmse->expr = null_pointer_node;
6882 102 : parmse->string_length = build_int_cst (gfc_charlen_type_node, 0);
6883 : }
6884 344 : else if (gfc_value_dummy_passed_byref (fsym))
6885 6 : parmse->expr = null_pointer_node;
6886 338 : else if (gfc_bt_struct (fsym->ts.type)
6887 30 : && !(fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING))
6888 : {
6889 : /* Pass null struct. Types c_ptr and c_funptr from ISO_C_BINDING
6890 : are pointers and passed as such below. */
6891 24 : tree temp = gfc_create_var (gfc_sym_type (fsym), "absent");
6892 24 : TREE_CONSTANT (temp) = 1;
6893 24 : TREE_READONLY (temp) = 1;
6894 24 : DECL_INITIAL (temp) = build_zero_cst (TREE_TYPE (temp));
6895 24 : parmse->expr = temp;
6896 24 : }
6897 : else
6898 314 : parmse->expr = fold_convert (gfc_sym_type (fsym),
6899 : integer_zero_node);
6900 446 : vec_safe_push (optionalargs, boolean_false_node);
6901 :
6902 446 : return;
6903 : }
6904 :
6905 : /* A derived type with allocatable components is passed by reference to
6906 : a copy of a variable, or to a temporary as it is. */
6907 22437 : if (fsym->ts.type == BT_DERIVED && gfc_value_dummy_passed_byref (fsym))
6908 : {
6909 114 : bool shared = value_actual_shares_storage (e);
6910 114 : tree addr, copy, cond;
6911 114 : stmtblock_t block;
6912 :
6913 114 : if (GFC_CLASS_TYPE_P (TREE_TYPE (parmse->expr)))
6914 : {
6915 : /* Copy the declared type of a CLASS actual. */
6916 6 : parmse->expr = build_fold_indirect_ref_loc (input_location,
6917 : gfc_class_data_get
6918 : (parmse->expr));
6919 6 : shared = true;
6920 : }
6921 108 : else if (!shared)
6922 72 : parmse->expr = gfc_evaluate_now (parmse->expr, &parmse->pre);
6923 :
6924 114 : addr = gfc_evaluate_now (gfc_build_addr_expr (NULL_TREE, parmse->expr),
6925 : &parmse->pre);
6926 114 : parmse->expr = addr;
6927 114 : if (shared)
6928 : {
6929 : /* An absent optional actual has nothing to copy or free. */
6930 18 : cond = fsym->attr.optional
6931 66 : ? fold_build2_loc (input_location, NE_EXPR, logical_type_node,
6932 24 : addr, fold_convert (TREE_TYPE (addr),
6933 : null_pointer_node))
6934 : : logical_true_node;
6935 42 : copy = gfc_create_var (TREE_TYPE (TREE_TYPE (addr)), "val_copy");
6936 42 : tmp = build_fold_indirect_ref_loc (input_location, addr);
6937 42 : gfc_init_block (&block);
6938 42 : gfc_add_modify (&block, copy, tmp);
6939 42 : gfc_add_expr_to_block (&block,
6940 42 : gfc_copy_alloc_comp (fsym->ts.u.derived, tmp,
6941 : copy, 0, 0));
6942 42 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
6943 : cond, gfc_finish_block (&block),
6944 : build_empty_stmt (input_location));
6945 42 : gfc_add_expr_to_block (&parmse->pre, tmp);
6946 42 : tmp = gfc_deallocate_alloc_comp (fsym->ts.u.derived, copy, 0);
6947 42 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
6948 : cond, tmp, build_empty_stmt (input_location));
6949 42 : gfc_add_expr_to_block (&parmse->post, tmp);
6950 42 : parmse->expr = gfc_build_addr_expr (NULL_TREE, copy);
6951 : }
6952 : }
6953 :
6954 : /* Assumed-length or non-constant-length CHARACTER VALUE dummy: copy
6955 : the actual argument and pass the copy. */
6956 22437 : if (fsym->ts.type == BT_CHARACTER
6957 515 : && (!fsym->ts.u.cl || !fsym->ts.u.cl->length
6958 479 : || fsym->ts.u.cl->length->expr_type != EXPR_CONSTANT))
6959 : {
6960 : /* An optional actual argument that is absent has nothing to copy
6961 : from; pass a null pointer and a length of zero instead. */
6962 48 : tree present = NULL_TREE;
6963 48 : if (fsym->attr.optional && e->expr_type == EXPR_VARIABLE
6964 24 : && e->symtree->n.sym->attr.optional)
6965 12 : present = gfc_conv_expr_present (e->symtree->n.sym);
6966 :
6967 48 : gfc_conv_string_parameter (parmse);
6968 48 : tree len = fold_convert (gfc_charlen_type_node, parmse->string_length);
6969 48 : if (present)
6970 : {
6971 12 : len = fold_build3_loc (input_location, COND_EXPR,
6972 : gfc_charlen_type_node, present, len,
6973 : build_zero_cst (gfc_charlen_type_node));
6974 12 : len = gfc_evaluate_now (len, &parmse->pre);
6975 12 : parmse->string_length = len;
6976 : }
6977 48 : tree chartype = gfc_get_character_type_len (fsym->ts.kind, len);
6978 48 : tree val_copy = gfc_create_var (chartype, "val_copy");
6979 48 : tmp = fold_build1_loc (input_location, DECL_EXPR, chartype, val_copy);
6980 48 : gfc_add_expr_to_block (&parmse->pre, tmp);
6981 : /* The copy size is in bytes, not in characters. */
6982 48 : tree bytes
6983 48 : = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
6984 : fold_convert (size_type_node, len),
6985 48 : fold_convert (size_type_node,
6986 : TYPE_SIZE_UNIT (gfc_get_char_type
6987 : (fsym->ts.kind))));
6988 48 : tmp = gfc_build_memcpy_call (
6989 : fold_convert (pvoid_type_node,
6990 : gfc_build_addr_expr (NULL_TREE, val_copy)),
6991 : fold_convert (pvoid_type_node, parmse->expr), bytes);
6992 48 : if (present)
6993 12 : tmp = build3_v (COND_EXPR, present, tmp,
6994 : build_empty_stmt (input_location));
6995 48 : gfc_add_expr_to_block (&parmse->pre, tmp);
6996 48 : parmse->expr = fold_convert (
6997 : build_pointer_type (gfc_get_char_type (fsym->ts.kind)),
6998 : gfc_build_addr_expr (NULL_TREE, val_copy));
6999 48 : if (present)
7000 24 : parmse->expr = fold_build3_loc (input_location, COND_EXPR,
7001 12 : TREE_TYPE (parmse->expr), present,
7002 : parmse->expr,
7003 12 : fold_convert (TREE_TYPE (parmse->expr),
7004 : null_pointer_node));
7005 : }
7006 :
7007 : /* Truncate a too long constant character actual argument. */
7008 22437 : if (gfc_const_length_character_type_p (&fsym->ts)
7009 467 : && e->expr_type == EXPR_CONSTANT
7010 22520 : && mpz_cmp_ui (fsym->ts.u.cl->length->value.integer,
7011 : e->value.character.length) < 0)
7012 : {
7013 17 : gfc_charlen_t flen = mpz_get_ui (fsym->ts.u.cl->length->value.integer);
7014 :
7015 : /* Truncate actual string argument. */
7016 17 : gfc_conv_expr (parmse, e);
7017 34 : parmse->expr = gfc_build_wide_string_const (e->ts.kind, flen,
7018 17 : e->value.character.string);
7019 17 : parmse->string_length = build_int_cst (gfc_charlen_type_node, flen);
7020 :
7021 17 : if (flen == 1)
7022 : {
7023 14 : tree slen1 = build_int_cst (gfc_charlen_type_node, 1);
7024 14 : gfc_conv_string_parameter (parmse);
7025 14 : parmse->expr = gfc_string_to_single_character (slen1, parmse->expr,
7026 : e->ts.kind);
7027 : }
7028 :
7029 : /* Indicate value,optional scalar dummy argument as present. */
7030 17 : if (fsym->attr.optional)
7031 1 : vec_safe_push (optionalargs, boolean_true_node);
7032 : return;
7033 : }
7034 :
7035 : /* gfortran argument passing conventions:
7036 : actual arguments to CHARACTER(len=1),VALUE
7037 : dummy arguments are actually passed by value.
7038 : Strings are truncated to length 1. */
7039 22420 : if (gfc_length_one_character_type_p (&fsym->ts))
7040 : {
7041 378 : if (e->expr_type == EXPR_CONSTANT
7042 54 : && e->value.character.length > 1)
7043 : {
7044 0 : e->value.character.length = 1;
7045 0 : gfc_conv_expr (parmse, e);
7046 : }
7047 :
7048 378 : tree slen1 = build_int_cst (gfc_charlen_type_node, 1);
7049 378 : gfc_conv_string_parameter (parmse);
7050 378 : parmse->expr = gfc_string_to_single_character (slen1, parmse->expr,
7051 : e->ts.kind);
7052 : /* Truncate resulting string to length 1. */
7053 378 : parmse->string_length = slen1;
7054 : }
7055 :
7056 22420 : if (fsym->attr.optional && fsym->ts.type != BT_CLASS)
7057 : {
7058 : /* F2018:15.5.2.12 Argument presence and
7059 : restrictions on arguments not present. */
7060 877 : if (e->expr_type == EXPR_VARIABLE
7061 698 : && e->rank == 0
7062 1521 : && (gfc_expr_attr (e).allocatable
7063 644 : || gfc_expr_attr (e).pointer))
7064 : {
7065 204 : gfc_se argse;
7066 204 : tree cond;
7067 204 : gfc_init_se (&argse, NULL);
7068 204 : argse.want_pointer = 1;
7069 204 : gfc_conv_expr (&argse, e);
7070 204 : cond = fold_convert (TREE_TYPE (argse.expr), null_pointer_node);
7071 204 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
7072 : argse.expr, cond);
7073 204 : if (e->symtree->n.sym->attr.dummy)
7074 24 : cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
7075 : logical_type_node,
7076 : gfc_conv_expr_present (e->symtree->n.sym),
7077 : cond);
7078 204 : vec_safe_push (optionalargs, fold_convert (boolean_type_node, cond));
7079 : /* Create "conditional temporary". */
7080 204 : conv_cond_temp (parmse, e, cond);
7081 : }
7082 673 : else if (e->expr_type != EXPR_VARIABLE
7083 494 : || !e->symtree->n.sym->attr.optional
7084 284 : || (e->ref != NULL && e->ref->type != REF_ARRAY))
7085 389 : vec_safe_push (optionalargs, boolean_true_node);
7086 : else
7087 : {
7088 284 : tmp = gfc_conv_expr_present (e->symtree->n.sym);
7089 284 : if (gfc_bt_struct (fsym->ts.type)
7090 48 : && !(fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING))
7091 48 : conv_cond_temp (parmse, e, tmp);
7092 236 : else if (e->ts.type != BT_CHARACTER && !e->symtree->n.sym->attr.value)
7093 84 : parmse->expr
7094 168 : = fold_build3_loc (input_location, COND_EXPR,
7095 84 : TREE_TYPE (parmse->expr),
7096 : tmp, parmse->expr,
7097 84 : fold_convert (TREE_TYPE (parmse->expr),
7098 : integer_zero_node));
7099 :
7100 568 : vec_safe_push (optionalargs,
7101 284 : fold_convert (boolean_type_node, tmp));
7102 : }
7103 : }
7104 : }
7105 :
7106 :
7107 : /* Helper function for the handling of NULL() actual arguments associated with
7108 : non-optional dummy variables. Argument parmse should already be set up. */
7109 : static void
7110 444 : conv_null_actual (gfc_se * parmse, gfc_expr * e, gfc_symbol * fsym)
7111 : {
7112 444 : gcc_assert (fsym && e->expr_type == EXPR_NULL);
7113 :
7114 : /* Obtain the character length for a NULL() actual with a character
7115 : MOLD argument. Otherwise substitute a suitable dummy length.
7116 : Here we handle only non-optional dummies of non-bind(c) procedures. */
7117 444 : if (fsym->ts.type == BT_CHARACTER)
7118 : {
7119 216 : if (e->ts.type == BT_CHARACTER
7120 162 : && e->symtree->n.sym->ts.type == BT_CHARACTER)
7121 : {
7122 : /* MOLD is present. Substitute a temporary character NULL pointer.
7123 : For an assumed-rank dummy we need a descriptor that passes the
7124 : correct rank. */
7125 162 : if (fsym->as && fsym->as->type == AS_ASSUMED_RANK)
7126 : {
7127 54 : tree tmp;
7128 54 : tmp = gfc_create_null_actual_descriptor (&parmse->pre, &e->ts,
7129 : fsym->attr, e->rank);
7130 54 : parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
7131 54 : }
7132 : else
7133 : {
7134 108 : tree tmp = gfc_create_var (TREE_TYPE (parmse->expr), "null");
7135 108 : gfc_add_modify (&parmse->pre, tmp,
7136 108 : build_zero_cst (TREE_TYPE (tmp)));
7137 108 : parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
7138 : }
7139 :
7140 : /* Ensure that a usable length is available. */
7141 162 : if (parmse->string_length == NULL_TREE)
7142 : {
7143 162 : gfc_typespec *ts = &e->symtree->n.sym->ts;
7144 :
7145 162 : if (ts->u.cl->length != NULL
7146 108 : && ts->u.cl->length->expr_type == EXPR_CONSTANT)
7147 108 : gfc_conv_const_charlen (ts->u.cl);
7148 :
7149 162 : if (ts->u.cl->backend_decl)
7150 162 : parmse->string_length = ts->u.cl->backend_decl;
7151 : }
7152 : }
7153 54 : else if (e->ts.type == BT_UNKNOWN && parmse->string_length == NULL_TREE)
7154 : {
7155 : /* MOLD is not present. Pass length of associated dummy character
7156 : argument if constant, or zero. */
7157 54 : if (fsym->ts.u.cl->length != NULL
7158 18 : && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7159 : {
7160 18 : gfc_conv_const_charlen (fsym->ts.u.cl);
7161 18 : parmse->string_length = fsym->ts.u.cl->backend_decl;
7162 : }
7163 : else
7164 : {
7165 36 : parmse->string_length = gfc_create_var (gfc_charlen_type_node,
7166 : "slen");
7167 36 : gfc_add_modify (&parmse->pre, parmse->string_length,
7168 : build_zero_cst (gfc_charlen_type_node));
7169 : }
7170 : }
7171 : }
7172 228 : else if (fsym->ts.type == BT_DERIVED
7173 18 : || ((fsym->attr.allocatable || fsym->attr.pointer)
7174 18 : && fsym->ts.type != BT_CLASS))
7175 : {
7176 228 : if (e->ts.type != BT_UNKNOWN)
7177 : /* MOLD is present. Pass a corresponding temporary NULL pointer.
7178 : For an assumed-rank dummy we provide a descriptor that passes
7179 : the correct rank. */
7180 : {
7181 138 : tree tmp = gfc_create_null_actual_descriptor (&parmse->pre, &e->ts,
7182 : fsym->attr, e->rank);
7183 138 : parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
7184 : }
7185 : else
7186 : /* MOLD is not present. Use attributes from dummy argument, which is
7187 : not allowed to be assumed-rank. */
7188 : {
7189 90 : int dummy_rank = fsym->as ? fsym->as->rank : 0;
7190 90 : tree tmp = gfc_create_null_actual_descriptor (&parmse->pre, &fsym->ts,
7191 : fsym->attr, dummy_rank);
7192 90 : parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
7193 : }
7194 : }
7195 444 : }
7196 :
7197 :
7198 : /* Return true if a subobject of the elements of an array is referenced. */
7199 :
7200 : static bool
7201 60 : is_subobject_ref (gfc_expr *e)
7202 : {
7203 60 : bool seen_array = false;
7204 :
7205 132 : for (gfc_ref *ref = e->ref; ref; ref = ref->next)
7206 : {
7207 120 : if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
7208 : seen_array = true;
7209 60 : else if (seen_array)
7210 : return true;
7211 : }
7212 :
7213 : return false;
7214 : }
7215 :
7216 :
7217 : /* Return true if expr is a span addressed dummy that is passed on as a whole,
7218 : rather than a reference to a subobject of the elements of an array. */
7219 :
7220 : static bool
7221 831 : is_whole_span_addressed_dummy (gfc_expr *e)
7222 : {
7223 831 : return e->expr_type == EXPR_VARIABLE
7224 831 : && e->symtree && e->symtree->n.sym
7225 831 : && gfc_is_span_addressed_dummy (e->symtree->n.sym)
7226 891 : && !is_subobject_ref (e);
7227 : }
7228 :
7229 :
7230 : /* Return true if the dummy fsym has an array descriptor and so addresses its
7231 : elements by the strides held in it. Such a dummy accepts an actual
7232 : argument of any stride; only a dummy without a descriptor, or one declared
7233 : CONTIGUOUS, needs it packed into contiguous storage. */
7234 :
7235 : static bool
7236 157 : dummy_accepts_strided_arg (gfc_symbol *fsym, bool nodesc_arg)
7237 : {
7238 126 : return fsym && !nodesc_arg && !fsym->attr.contiguous && fsym->as
7239 247 : && (fsym->as->type == AS_ASSUMED_SHAPE
7240 0 : || fsym->as->type == AS_ASSUMED_RANK
7241 0 : || fsym->as->type == AS_DEFERRED);
7242 : }
7243 :
7244 :
7245 : /* Return true if the actual argument expr for the dummy fsym may be passed as
7246 : a copy-in/copy-out temporary. A pointer associated with a TARGET or POINTER
7247 : dummy must remain valid after the call, so the actual argument is passed
7248 : directly, with a descriptor whose span provides the element spacing. An
7249 : actual argument with a vector subscript is not definable and its pointer
7250 : association is undefined on return, so it is still copied. */
7251 :
7252 : static bool
7253 1397 : copy_in_out_allowed (gfc_symbol *fsym, gfc_expr *e, bool nodesc_arg)
7254 : {
7255 1397 : if (fsym == NULL || nodesc_arg || gfc_has_vector_subscript (e))
7256 : return true;
7257 :
7258 1259 : if (gfc_dummy_requires_direct_arg (fsym))
7259 : return false;
7260 :
7261 971 : return !(fsym->attr.pointer && !fsym->attr.contiguous && fsym->as
7262 6 : && (fsym->as->type == AS_ASSUMED_SHAPE
7263 : || fsym->as->type == AS_ASSUMED_RANK
7264 : || fsym->as->type == AS_DEFERRED));
7265 : }
7266 :
7267 :
7268 : /* Return true if E is a reference to a function returning a pointer array,
7269 : whose elements may be spaced by more than the element size. */
7270 :
7271 : static bool
7272 53901 : pointer_array_result_p (gfc_expr *e)
7273 : {
7274 53901 : return e->expr_type == EXPR_FUNCTION
7275 4143 : && e->rank > 0
7276 4143 : && e->ts.type != BT_CLASS
7277 58031 : && gfc_expr_attr (e).pointer;
7278 : }
7279 :
7280 :
7281 : /* Evaluate the pointer array function reference E into a temporary pointer
7282 : in BLOCK and return a variable expression for that pointer. */
7283 :
7284 : static gfc_expr *
7285 145 : pointer_result_var (stmtblock_t *block, gfc_expr *e)
7286 : {
7287 145 : gfc_symtree *st = gfc_get_unique_symtree (NULL);
7288 145 : gfc_symbol *sym = gfc_new_symbol (st->name, NULL);
7289 145 : st->n.sym = sym;
7290 145 : sym->ts = e->ts;
7291 145 : sym->attr.flavor = FL_VARIABLE;
7292 145 : sym->attr.referenced = 1;
7293 145 : sym->attr.dimension = 1;
7294 145 : sym->attr.pointer = 1;
7295 145 : sym->as = gfc_get_array_spec ();
7296 145 : sym->as->rank = e->rank;
7297 145 : sym->as->type = AS_DEFERRED;
7298 :
7299 145 : gfc_se se;
7300 145 : gfc_init_se (&se, NULL);
7301 145 : gfc_conv_expr_descriptor (&se, e);
7302 145 : gfc_add_block_to_block (block, &se.pre);
7303 145 : gfc_add_block_to_block (block, &se.post);
7304 :
7305 145 : gcc_assert (VAR_P (se.expr) && GFC_DECL_PTR_ARRAY_P (se.expr));
7306 145 : sym->backend_decl = se.expr;
7307 :
7308 145 : if (sym->ts.type == BT_CHARACTER)
7309 : {
7310 36 : sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
7311 36 : sym->ts.u.cl->backend_decl = gfc_evaluate_now (se.string_length, block);
7312 : }
7313 :
7314 145 : gfc_expr *var = gfc_get_variable_expr (st);
7315 145 : var->where = e->where;
7316 145 : return var;
7317 : }
7318 :
7319 :
7320 : /* Generate code for a procedure call. Note can return se->post != NULL.
7321 : If se->direct_byref is set then se->expr contains the return parameter.
7322 : Return nonzero, if the call has alternate specifiers.
7323 : 'expr' is only needed for procedure pointer components. */
7324 :
7325 : int
7326 141180 : gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
7327 : gfc_actual_arglist * args, gfc_expr * expr,
7328 : vec<tree, va_gc> *append_args)
7329 : {
7330 141180 : gfc_interface_mapping mapping;
7331 141180 : vec<tree, va_gc> *arglist;
7332 141180 : vec<tree, va_gc> *retargs;
7333 141180 : tree tmp;
7334 141180 : tree fntype;
7335 141180 : gfc_se parmse;
7336 141180 : gfc_array_info *info;
7337 141180 : int byref;
7338 141180 : int parm_kind;
7339 141180 : tree type;
7340 141180 : tree var;
7341 141180 : tree len;
7342 141180 : tree base_object;
7343 141180 : vec<tree, va_gc> *stringargs;
7344 141180 : vec<tree, va_gc> *optionalargs;
7345 141180 : tree result = NULL;
7346 141180 : gfc_formal_arglist *formal;
7347 141180 : gfc_actual_arglist *arg;
7348 141180 : int has_alternate_specifier = 0;
7349 141180 : bool need_interface_mapping;
7350 141180 : bool is_builtin;
7351 141180 : bool callee_alloc;
7352 141180 : bool ulim_copy;
7353 141180 : gfc_typespec ts;
7354 141180 : gfc_charlen cl;
7355 141180 : gfc_expr *e;
7356 141180 : gfc_symbol *fsym;
7357 141180 : enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
7358 141180 : gfc_component *comp = NULL;
7359 141180 : int arglen;
7360 141180 : unsigned int argc;
7361 141180 : tree arg1_cntnr = NULL_TREE;
7362 141180 : bool call_needed_for_length = true;
7363 141180 : arglist = NULL;
7364 141180 : retargs = NULL;
7365 141180 : stringargs = NULL;
7366 141180 : optionalargs = NULL;
7367 141180 : var = NULL_TREE;
7368 141180 : len = NULL_TREE;
7369 141180 : gfc_clear_ts (&ts);
7370 141180 : gfc_intrinsic_sym *isym = expr && expr->rank ?
7371 : expr->value.function.isym : NULL;
7372 :
7373 141180 : comp = gfc_get_proc_ptr_comp (expr);
7374 :
7375 282360 : bool elemental_proc = (comp
7376 2115 : && comp->ts.interface
7377 2061 : && comp->ts.interface->attr.elemental)
7378 1904 : || (comp && comp->attr.elemental)
7379 143084 : || sym->attr.elemental;
7380 :
7381 141180 : if (se->ss != NULL)
7382 : {
7383 25620 : if (!elemental_proc)
7384 : {
7385 22036 : gcc_assert (se->ss->info->type == GFC_SS_FUNCTION);
7386 22036 : if (se->ss->info->useflags)
7387 : {
7388 5902 : gcc_assert ((!comp && gfc_return_by_reference (sym)
7389 : && sym->result->attr.dimension)
7390 : || (comp && comp->attr.dimension)
7391 : || gfc_is_class_array_function (expr));
7392 5902 : gcc_assert (se->loop != NULL);
7393 : /* Access the previously obtained result. */
7394 5902 : gfc_conv_tmp_array_ref (se);
7395 5902 : return 0;
7396 : }
7397 : }
7398 19718 : info = &se->ss->info->data.array;
7399 : }
7400 : else
7401 : info = NULL;
7402 :
7403 135278 : stmtblock_t post, clobbers, dealloc_blk;
7404 135278 : gfc_init_block (&post);
7405 135278 : gfc_init_block (&clobbers);
7406 135278 : gfc_init_block (&dealloc_blk);
7407 135278 : gfc_init_interface_mapping (&mapping);
7408 135278 : if (!comp)
7409 : {
7410 133212 : formal = gfc_sym_get_dummy_args (sym);
7411 250548 : need_interface_mapping = sym->attr.dimension ||
7412 117336 : (sym->ts.type == BT_CHARACTER
7413 3235 : && sym->ts.u.cl->length
7414 2483 : && sym->ts.u.cl->length->expr_type
7415 : != EXPR_CONSTANT)
7416 248950 : || has_value_array_dummy (formal);
7417 : }
7418 : else
7419 : {
7420 2066 : formal = comp->ts.interface ? comp->ts.interface->formal : NULL;
7421 4063 : need_interface_mapping = comp->attr.dimension ||
7422 1997 : (comp->ts.type == BT_CHARACTER
7423 229 : && comp->ts.u.cl->length
7424 220 : && comp->ts.u.cl->length->expr_type
7425 : != EXPR_CONSTANT)
7426 4044 : || has_value_array_dummy (formal);
7427 : }
7428 :
7429 135278 : base_object = NULL_TREE;
7430 : /* For _vprt->_copy () routines no formal symbol is present. Nevertheless
7431 : is the third and fourth argument to such a function call a value
7432 : denoting the number of elements to copy (i.e., most of the time the
7433 : length of a deferred length string). */
7434 270556 : ulim_copy = (formal == NULL)
7435 32831 : && UNLIMITED_POLY (sym)
7436 135370 : && comp && (strcmp ("_copy", comp->name) == 0);
7437 :
7438 : /* Scan for allocatable actual arguments passed to allocatable dummy
7439 : arguments with INTENT(OUT). As the corresponding actual arguments are
7440 : deallocated before execution of the procedure, we evaluate actual
7441 : argument expressions to avoid problems with possible dependencies. */
7442 135278 : bool force_eval_args = false;
7443 135278 : gfc_formal_arglist *tmp_formal;
7444 412146 : for (arg = args, tmp_formal = formal; arg != NULL;
7445 243513 : arg = arg->next, tmp_formal = tmp_formal ? tmp_formal->next : NULL)
7446 : {
7447 277427 : e = arg->expr;
7448 277427 : fsym = tmp_formal ? tmp_formal->sym : NULL;
7449 263586 : if (e && fsym
7450 231658 : && e->expr_type == EXPR_VARIABLE
7451 102467 : && fsym->attr.intent == INTENT_OUT
7452 6708 : && (fsym->ts.type == BT_CLASS && fsym->attr.class_ok
7453 6708 : ? CLASS_DATA (fsym)->attr.allocatable
7454 4964 : : fsym->attr.allocatable)
7455 559 : && e->symtree
7456 559 : && e->symtree->n.sym
7457 541013 : && gfc_variable_attr (e).allocatable)
7458 : {
7459 : force_eval_args = true;
7460 : break;
7461 : }
7462 : }
7463 :
7464 : /* Evaluate the arguments. */
7465 135278 : actual_arg_level++;
7466 413119 : for (arg = args, argc = 0; arg != NULL;
7467 277841 : arg = arg->next, formal = formal ? formal->next : NULL, ++argc)
7468 : {
7469 277841 : bool finalized = false;
7470 277841 : tree derived_array = NULL_TREE;
7471 277841 : symbol_attribute *attr;
7472 :
7473 277841 : e = arg->expr;
7474 277841 : fsym = formal ? formal->sym : NULL;
7475 522327 : parm_kind = MISSING;
7476 :
7477 244486 : attr = fsym ? &(fsym->ts.type == BT_CLASS ? CLASS_DATA (fsym)->attr
7478 : : fsym->attr)
7479 : : nullptr;
7480 : /* If the procedure requires an explicit interface, the actual
7481 : argument is passed according to the corresponding formal
7482 : argument. If the corresponding formal argument is a POINTER,
7483 : ALLOCATABLE or assumed shape, we do not use g77's calling
7484 : convention, and pass the address of the array descriptor
7485 : instead. Otherwise we use g77's calling convention, in other words
7486 : pass the array data pointer without descriptor. */
7487 244433 : bool nodesc_arg = fsym != NULL
7488 244433 : && !(fsym->attr.pointer || fsym->attr.allocatable)
7489 234982 : && fsym->as
7490 42177 : && fsym->as->type != AS_ASSUMED_SHAPE
7491 25123 : && fsym->as->type != AS_ASSUMED_RANK;
7492 277841 : if (comp)
7493 2839 : nodesc_arg = nodesc_arg || !comp->attr.always_explicit;
7494 : else
7495 275002 : nodesc_arg
7496 : = nodesc_arg
7497 275002 : || !(sym->attr.always_explicit || (attr && attr->codimension));
7498 :
7499 : /* Class array expressions are sometimes coming completely unadorned
7500 : with either arrayspec or _data component. Correct that here.
7501 : OOP-TODO: Move this to the frontend. */
7502 277841 : if (e && e->expr_type == EXPR_VARIABLE
7503 116594 : && !e->ref
7504 53102 : && e->ts.type == BT_CLASS
7505 2825 : && (CLASS_DATA (e)->attr.codimension
7506 2825 : || CLASS_DATA (e)->attr.dimension))
7507 : {
7508 0 : gfc_typespec temp_ts = e->ts;
7509 0 : gfc_add_class_array_ref (e);
7510 0 : e->ts = temp_ts;
7511 : }
7512 :
7513 277841 : if (e == NULL
7514 263994 : || (e->expr_type == EXPR_NULL
7515 763 : && fsym
7516 763 : && fsym->attr.value
7517 72 : && fsym->attr.optional
7518 72 : && !fsym->attr.dimension
7519 72 : && fsym->ts.type != BT_CLASS))
7520 : {
7521 13919 : if (se->ignore_optional)
7522 : {
7523 : /* Some intrinsics have already been resolved to the correct
7524 : parameters. */
7525 446 : continue;
7526 : }
7527 13721 : else if (arg->label)
7528 : {
7529 224 : has_alternate_specifier = 1;
7530 224 : continue;
7531 : }
7532 : else
7533 : {
7534 13497 : gfc_init_se (&parmse, NULL);
7535 :
7536 : /* For scalar arguments with VALUE attribute which are passed by
7537 : value, pass "0" and a hidden argument gives the optional
7538 : status. */
7539 13497 : if (fsym && fsym->attr.optional && fsym->attr.value
7540 482 : && !fsym->attr.dimension && fsym->ts.type != BT_CLASS)
7541 : {
7542 446 : conv_dummy_value (&parmse, e, fsym, optionalargs);
7543 : }
7544 : else
7545 : {
7546 : /* Pass a NULL pointer for an absent arg. */
7547 13051 : parmse.expr = null_pointer_node;
7548 :
7549 : /* Is it an absent character dummy? */
7550 13051 : bool absent_char = false;
7551 13051 : gfc_dummy_arg * const dummy_arg = arg->associated_dummy;
7552 :
7553 : /* Fall back to inferred type only if no formal. */
7554 13051 : if (fsym)
7555 11993 : absent_char = (fsym->ts.type == BT_CHARACTER);
7556 1058 : else if (dummy_arg)
7557 1058 : absent_char = (gfc_dummy_arg_get_typespec (*dummy_arg).type
7558 : == BT_CHARACTER);
7559 13051 : if (absent_char)
7560 1133 : parmse.string_length = build_int_cst (gfc_charlen_type_node,
7561 : 0);
7562 : }
7563 : }
7564 : }
7565 263922 : else if (e->expr_type == EXPR_NULL
7566 691 : && fsym && attr && (attr->pointer || attr->allocatable)
7567 534 : && ((fsym->ts.type == BT_DERIVED
7568 210 : && (e->ts.type == BT_UNKNOWN || e->ts.type == BT_DERIVED))
7569 324 : || (fsym->attr.allocatable && fsym->ts.type != BT_CLASS
7570 18 : && fsym->ts.type != BT_CHARACTER)))
7571 : {
7572 228 : gfc_init_se (&parmse, NULL);
7573 228 : gfc_conv_expr_reference (&parmse, e);
7574 228 : conv_null_actual (&parmse, e, fsym);
7575 : }
7576 263694 : else if (arg->expr->expr_type == EXPR_NULL
7577 463 : && fsym && !fsym->attr.pointer
7578 163 : && (fsym->ts.type != BT_CLASS
7579 6 : || !CLASS_DATA (fsym)->attr.class_pointer))
7580 : {
7581 : /* Pass a NULL pointer to denote an absent arg. */
7582 163 : gcc_assert (fsym->attr.optional && !fsym->attr.allocatable
7583 : && (fsym->ts.type != BT_CLASS
7584 : || !CLASS_DATA (fsym)->attr.allocatable));
7585 163 : gfc_init_se (&parmse, NULL);
7586 163 : parmse.expr = null_pointer_node;
7587 163 : if (fsym->ts.type == BT_CHARACTER)
7588 42 : parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
7589 : }
7590 263531 : else if (fsym && fsym->ts.type == BT_CLASS
7591 11960 : && e->ts.type == BT_DERIVED)
7592 : {
7593 : /* The derived type needs to be converted to a temporary
7594 : CLASS object. */
7595 4912 : gfc_init_se (&parmse, se);
7596 4912 : gfc_conv_derived_to_class (&parmse, e, fsym, NULL_TREE,
7597 4912 : fsym->attr.optional
7598 1008 : && e->expr_type == EXPR_VARIABLE
7599 1008 : && e->symtree->n.sym->attr.optional,
7600 4912 : CLASS_DATA (fsym)->attr.class_pointer
7601 4719 : || CLASS_DATA (fsym)->attr.allocatable,
7602 : sym->name, &derived_array);
7603 :
7604 4912 : if (fsym->attr.value && e->rank == 0)
7605 24 : conv_class_value_arg (&parmse, e);
7606 : }
7607 226691 : else if (UNLIMITED_POLY (fsym) && e->ts.type != BT_CLASS
7608 978 : && e->ts.type != BT_PROCEDURE
7609 954 : && (gfc_expr_attr (e).flavor != FL_PROCEDURE
7610 954 : || gfc_expr_attr (e).proc != PROC_UNKNOWN))
7611 : {
7612 : /* The intrinsic type needs to be converted to a temporary
7613 : CLASS object for the unlimited polymorphic formal. */
7614 954 : gfc_find_vtab (&e->ts);
7615 954 : gfc_init_se (&parmse, se);
7616 954 : gfc_conv_intrinsic_to_class (&parmse, e, fsym->ts);
7617 954 : if (fsym->attr.value)
7618 24 : conv_class_value_copy (&parmse);
7619 : }
7620 257665 : else if (se->ss && se->ss->info->useflags)
7621 : {
7622 5875 : gfc_ss *ss;
7623 :
7624 5875 : ss = se->ss;
7625 :
7626 : /* An elemental function inside a scalarized loop. */
7627 5875 : gfc_init_se (&parmse, se);
7628 5875 : parm_kind = ELEMENTAL;
7629 :
7630 : /* When no fsym is present, ulim_copy is set and this is a third or
7631 : fourth argument, use call-by-value instead of by reference to
7632 : hand the length properties to the copy routine (i.e., most of the
7633 : time this will be a call to a __copy_character_* routine where the
7634 : third and fourth arguments are the lengths of a deferred length
7635 : char array). */
7636 5875 : if ((fsym && fsym->attr.value)
7637 5641 : || (ulim_copy && (argc == 2 || argc == 3)))
7638 234 : gfc_conv_expr (&parmse, e);
7639 5641 : else if (e->expr_type == EXPR_ARRAY)
7640 : {
7641 306 : gfc_conv_expr (&parmse, e);
7642 306 : if (e->ts.type != BT_CHARACTER)
7643 263 : parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
7644 : }
7645 : else
7646 5335 : gfc_conv_expr_reference (&parmse, e);
7647 :
7648 5875 : if (e->ts.type == BT_CHARACTER && !e->rank
7649 174 : && e->expr_type == EXPR_FUNCTION)
7650 12 : parmse.expr = build_fold_indirect_ref_loc (input_location,
7651 : parmse.expr);
7652 :
7653 5825 : if (fsym && fsym->ts.type == BT_DERIVED
7654 7523 : && gfc_is_class_container_ref (e))
7655 : {
7656 24 : parmse.expr = gfc_class_data_get (parmse.expr);
7657 :
7658 24 : if (fsym->attr.optional && e->expr_type == EXPR_VARIABLE
7659 24 : && e->symtree->n.sym->attr.optional)
7660 : {
7661 0 : tree cond = gfc_conv_expr_present (e->symtree->n.sym);
7662 0 : parmse.expr = build3_loc (input_location, COND_EXPR,
7663 0 : TREE_TYPE (parmse.expr),
7664 : cond, parmse.expr,
7665 0 : fold_convert (TREE_TYPE (parmse.expr),
7666 : null_pointer_node));
7667 : }
7668 : }
7669 :
7670 : /* Scalar dummy arguments of intrinsic type or derived type with
7671 : VALUE attribute. */
7672 5875 : if (fsym
7673 5825 : && fsym->attr.value
7674 234 : && fsym->ts.type != BT_CLASS)
7675 234 : conv_dummy_value (&parmse, e, fsym, optionalargs);
7676 :
7677 : /* If we are passing an absent array as optional dummy to an
7678 : elemental procedure, make sure that we pass NULL when the data
7679 : pointer is NULL. We need this extra conditional because of
7680 : scalarization which passes arrays elements to the procedure,
7681 : ignoring the fact that the array can be absent/unallocated/... */
7682 5641 : else if (ss->info->can_be_null_ref
7683 421 : && ss->info->type != GFC_SS_REFERENCE)
7684 : {
7685 199 : tree descriptor_data;
7686 :
7687 199 : descriptor_data = ss->info->data.array.data;
7688 199 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7689 : descriptor_data,
7690 199 : fold_convert (TREE_TYPE (descriptor_data),
7691 : null_pointer_node));
7692 199 : parmse.expr
7693 398 : = fold_build3_loc (input_location, COND_EXPR,
7694 199 : TREE_TYPE (parmse.expr),
7695 : gfc_unlikely (tmp, PRED_FORTRAN_ABSENT_DUMMY),
7696 199 : fold_convert (TREE_TYPE (parmse.expr),
7697 : null_pointer_node),
7698 : parmse.expr);
7699 : }
7700 :
7701 : /* The scalarizer does not repackage the reference to a class
7702 : array - instead it returns a pointer to the data element. */
7703 5875 : if (fsym && fsym->ts.type == BT_CLASS && e->ts.type == BT_CLASS)
7704 216 : gfc_conv_class_to_class (&parmse, e, fsym->ts, true,
7705 192 : fsym->attr.intent != INTENT_IN
7706 : && (CLASS_DATA (fsym)->attr.class_pointer
7707 24 : || CLASS_DATA (fsym)->attr.allocatable),
7708 192 : fsym->attr.optional
7709 0 : && e->expr_type == EXPR_VARIABLE
7710 0 : && e->symtree->n.sym->attr.optional,
7711 192 : CLASS_DATA (fsym)->attr.class_pointer
7712 192 : || CLASS_DATA (fsym)->attr.allocatable);
7713 : }
7714 : else
7715 : {
7716 251790 : bool scalar;
7717 251790 : gfc_ss *argss;
7718 :
7719 251790 : gfc_init_se (&parmse, NULL);
7720 :
7721 : /* Check whether the expression is a scalar or not; we cannot use
7722 : e->rank as it can be nonzero for functions arguments. */
7723 251790 : argss = gfc_walk_expr (e);
7724 251790 : scalar = argss == gfc_ss_terminator;
7725 251790 : if (!scalar)
7726 62301 : gfc_free_ss_chain (argss);
7727 :
7728 : /* Special handling for passing scalar polymorphic coarrays;
7729 : otherwise one passes "class->_data.data" instead of "&class". */
7730 251790 : if (e->rank == 0 && e->ts.type == BT_CLASS
7731 3870 : && fsym && fsym->ts.type == BT_CLASS
7732 3430 : && CLASS_DATA (fsym)->attr.codimension
7733 55 : && !CLASS_DATA (fsym)->attr.dimension)
7734 : {
7735 55 : gfc_add_class_array_ref (e);
7736 55 : parmse.want_coarray = 1;
7737 55 : scalar = false;
7738 : }
7739 :
7740 : /* A scalar or transformational function. */
7741 251790 : if (scalar)
7742 : {
7743 189434 : if (e->expr_type == EXPR_VARIABLE
7744 56830 : && e->symtree->n.sym->attr.cray_pointee
7745 390 : && fsym && fsym->attr.flavor == FL_PROCEDURE)
7746 : {
7747 : /* The Cray pointer needs to be converted to a pointer to
7748 : a type given by the expression. */
7749 6 : gfc_conv_expr (&parmse, e);
7750 6 : type = build_pointer_type (TREE_TYPE (parmse.expr));
7751 6 : tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
7752 6 : parmse.expr = convert (type, tmp);
7753 : }
7754 :
7755 189428 : else if (sym->attr.is_bind_c && e && is_CFI_desc (fsym, NULL))
7756 : /* Implement F2018, 18.3.6, list item (5), bullet point 2. */
7757 691 : gfc_conv_gfc_desc_to_cfi_desc (&parmse, e, fsym);
7758 :
7759 188737 : else if (fsym && fsym->attr.value && fsym->attr.dimension)
7760 : /* Scalar actual argument sequence associated with a VALUE
7761 : array dummy. */
7762 36 : conv_seq_assoc_value_arg (&parmse, e, fsym, &mapping);
7763 :
7764 160446 : else if (fsym && fsym->attr.value)
7765 : {
7766 22471 : if (fsym->ts.type == BT_CHARACTER
7767 639 : && fsym->ts.is_c_interop
7768 229 : && fsym->ns->proc_name != NULL
7769 229 : && fsym->ns->proc_name->attr.is_bind_c)
7770 : {
7771 220 : parmse.expr = NULL;
7772 220 : conv_scalar_char_value (fsym, &parmse, &e);
7773 220 : if (parmse.expr == NULL)
7774 166 : gfc_conv_expr (&parmse, e);
7775 : }
7776 22251 : else if (fsym->ts.type == BT_CLASS)
7777 : {
7778 36 : if (e->ts.type == BT_CLASS
7779 36 : && CLASS_DATA (e)->attr.dimension)
7780 : {
7781 : /* An element of a class array is not a class
7782 : container; repackage it as one. */
7783 6 : gfc_conv_expr_reference (&parmse, e);
7784 6 : gfc_conv_class_to_class (&parmse, e, fsym->ts, false,
7785 : false, false, false);
7786 : }
7787 : else
7788 30 : gfc_conv_expr (&parmse, e);
7789 36 : conv_class_value_arg (&parmse, e);
7790 : }
7791 : else
7792 : {
7793 22215 : gfc_conv_expr (&parmse, e);
7794 22215 : conv_dummy_value (&parmse, e, fsym, optionalargs);
7795 : }
7796 : }
7797 :
7798 166230 : else if (arg->name && arg->name[0] == '%')
7799 : /* Argument list functions %VAL, %LOC and %REF are signalled
7800 : through arg->name. */
7801 5846 : conv_arglist_function (&parmse, arg->expr, arg->name);
7802 160384 : else if ((e->expr_type == EXPR_FUNCTION)
7803 8447 : && ((e->value.function.esym
7804 2226 : && e->value.function.esym->result->attr.pointer)
7805 8352 : || (!e->value.function.esym
7806 6221 : && e->symtree->n.sym->attr.pointer))
7807 95 : && fsym && fsym->attr.target)
7808 : /* Make sure the function only gets called once. */
7809 8 : gfc_conv_expr_reference (&parmse, e);
7810 160376 : else if (e->expr_type == EXPR_FUNCTION
7811 8439 : && e->symtree->n.sym->result
7812 7404 : && e->symtree->n.sym->result != e->symtree->n.sym
7813 210 : && e->symtree->n.sym->result->attr.proc_pointer)
7814 : {
7815 : /* Functions returning procedure pointers. */
7816 18 : gfc_conv_expr (&parmse, e);
7817 18 : if (fsym && fsym->attr.proc_pointer)
7818 6 : parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
7819 : }
7820 :
7821 : else
7822 : {
7823 160358 : bool defer_to_dealloc_blk = false;
7824 160358 : if (e->ts.type == BT_CLASS && fsym
7825 3767 : && fsym->ts.type == BT_CLASS
7826 3339 : && (!CLASS_DATA (fsym)->as
7827 417 : || CLASS_DATA (fsym)->as->type != AS_ASSUMED_RANK)
7828 2922 : && CLASS_DATA (e)->attr.codimension)
7829 : {
7830 48 : gcc_assert (!CLASS_DATA (fsym)->attr.codimension);
7831 48 : gcc_assert (!CLASS_DATA (fsym)->as);
7832 48 : gfc_add_class_array_ref (e);
7833 48 : parmse.want_coarray = 1;
7834 48 : gfc_conv_expr_reference (&parmse, e);
7835 48 : class_scalar_coarray_to_class (&parmse, e, fsym->ts,
7836 48 : fsym->attr.optional
7837 48 : && e->expr_type == EXPR_VARIABLE);
7838 : }
7839 160310 : else if (e->ts.type == BT_CLASS && fsym
7840 3719 : && fsym->ts.type == BT_CLASS
7841 3291 : && !CLASS_DATA (fsym)->as
7842 2874 : && !CLASS_DATA (e)->as
7843 2746 : && strcmp (fsym->ts.u.derived->name,
7844 : e->ts.u.derived->name))
7845 : {
7846 1715 : type = gfc_typenode_for_spec (&fsym->ts);
7847 1715 : var = gfc_create_var (type, fsym->name);
7848 1715 : gfc_conv_expr (&parmse, e);
7849 1715 : if (fsym->attr.optional
7850 153 : && e->expr_type == EXPR_VARIABLE
7851 153 : && e->symtree->n.sym->attr.optional)
7852 : {
7853 66 : stmtblock_t block;
7854 66 : tree cond;
7855 66 : tmp = gfc_build_addr_expr (NULL_TREE, parmse.expr);
7856 66 : cond = fold_build2_loc (input_location, NE_EXPR,
7857 : logical_type_node, tmp,
7858 66 : fold_convert (TREE_TYPE (tmp),
7859 : null_pointer_node));
7860 66 : gfc_start_block (&block);
7861 66 : gfc_add_modify (&block, var,
7862 : fold_build1_loc (input_location,
7863 : VIEW_CONVERT_EXPR,
7864 : type, parmse.expr));
7865 66 : gfc_add_expr_to_block (&parmse.pre,
7866 : fold_build3_loc (input_location,
7867 : COND_EXPR, void_type_node,
7868 : cond, gfc_finish_block (&block),
7869 : build_empty_stmt (input_location)));
7870 66 : parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
7871 132 : parmse.expr = build3_loc (input_location, COND_EXPR,
7872 66 : TREE_TYPE (parmse.expr),
7873 : cond, parmse.expr,
7874 66 : fold_convert (TREE_TYPE (parmse.expr),
7875 : null_pointer_node));
7876 66 : }
7877 : else
7878 : {
7879 : /* Since the internal representation of unlimited
7880 : polymorphic expressions includes an extra field
7881 : that other class objects do not, a cast to the
7882 : formal type does not work. */
7883 1649 : if (!UNLIMITED_POLY (e) && UNLIMITED_POLY (fsym))
7884 : {
7885 91 : tree efield;
7886 :
7887 : /* Evaluate arguments just once, when they have
7888 : side effects. */
7889 91 : if (TREE_SIDE_EFFECTS (parmse.expr))
7890 : {
7891 25 : tree cldata, zero;
7892 :
7893 25 : parmse.expr = gfc_evaluate_now (parmse.expr,
7894 : &parmse.pre);
7895 :
7896 : /* Prevent memory leak, when old component
7897 : was allocated already. */
7898 25 : cldata = gfc_class_data_get (parmse.expr);
7899 25 : zero = build_int_cst (TREE_TYPE (cldata),
7900 : 0);
7901 25 : tmp = fold_build2_loc (input_location, NE_EXPR,
7902 : logical_type_node,
7903 : cldata, zero);
7904 25 : tmp = build3_v (COND_EXPR, tmp,
7905 : gfc_call_free (cldata),
7906 : build_empty_stmt (
7907 : input_location));
7908 25 : gfc_add_expr_to_block (&parmse.finalblock,
7909 : tmp);
7910 25 : gfc_add_modify (&parmse.finalblock,
7911 : cldata, zero);
7912 : }
7913 :
7914 : /* Set the _data field. */
7915 91 : tmp = gfc_class_data_get (var);
7916 91 : efield = fold_convert (TREE_TYPE (tmp),
7917 : gfc_class_data_get (parmse.expr));
7918 91 : gfc_add_modify (&parmse.pre, tmp, efield);
7919 :
7920 : /* Set the _vptr field. */
7921 91 : tmp = gfc_class_vptr_get (var);
7922 91 : efield = fold_convert (TREE_TYPE (tmp),
7923 : gfc_class_vptr_get (parmse.expr));
7924 91 : gfc_add_modify (&parmse.pre, tmp, efield);
7925 :
7926 : /* Set the _len field. */
7927 91 : tmp = gfc_class_len_get (var);
7928 91 : gfc_add_modify (&parmse.pre, tmp,
7929 91 : build_int_cst (TREE_TYPE (tmp), 0));
7930 91 : }
7931 : else
7932 : {
7933 1558 : tmp = fold_build1_loc (input_location,
7934 : VIEW_CONVERT_EXPR,
7935 : type, parmse.expr);
7936 1558 : gfc_add_modify (&parmse.pre, var, tmp);
7937 1649 : ;
7938 : }
7939 1649 : parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
7940 : }
7941 : }
7942 : else
7943 : {
7944 158595 : gfc_conv_expr_reference (&parmse, e);
7945 :
7946 158595 : gfc_symbol *dsym = fsym;
7947 158595 : gfc_dummy_arg *dummy;
7948 :
7949 : /* Use associated dummy as fallback for formal
7950 : argument if there is no explicit interface. */
7951 158595 : if (dsym == NULL
7952 27443 : && (dummy = arg->associated_dummy)
7953 24901 : && dummy->intrinsicness == GFC_NON_INTRINSIC_DUMMY_ARG
7954 182089 : && dummy->u.non_intrinsic->sym)
7955 158595 : dsym = dummy->u.non_intrinsic->sym;
7956 :
7957 158595 : if (dsym
7958 154646 : && dsym->attr.intent == INTENT_OUT
7959 3411 : && !dsym->attr.allocatable
7960 3262 : && !dsym->attr.pointer
7961 3244 : && e->expr_type == EXPR_VARIABLE
7962 3243 : && e->ref == NULL
7963 3128 : && e->symtree
7964 3128 : && e->symtree->n.sym
7965 3128 : && !e->symtree->n.sym->attr.dimension
7966 3128 : && e->ts.type != BT_CHARACTER
7967 3026 : && e->ts.type != BT_CLASS
7968 2778 : && (e->ts.type != BT_DERIVED
7969 528 : || (dsym->ts.type == BT_DERIVED
7970 528 : && e->ts.u.derived == dsym->ts.u.derived
7971 : /* Types with allocatable components are
7972 : excluded from clobbering because we need
7973 : the unclobbered pointers to free the
7974 : allocatable components in the callee.
7975 : Same goes for finalizable types or types
7976 : with finalizable components, we need to
7977 : pass the unclobbered values to the
7978 : finalization routines.
7979 : For parameterized types, it's less clear
7980 : but they may not have a constant size
7981 : so better exclude them in any case. */
7982 513 : && !e->ts.u.derived->attr.alloc_comp
7983 387 : && !e->ts.u.derived->attr.pdt_type
7984 351 : && !gfc_is_finalizable (e->ts.u.derived, NULL)))
7985 2559 : && e->ts.type != BT_PROCEDURE
7986 161118 : && !sym->attr.elemental)
7987 : {
7988 1190 : tree var;
7989 1190 : var = build_fold_indirect_ref_loc (input_location,
7990 : parmse.expr);
7991 1190 : tree clobber = build_clobber (TREE_TYPE (var));
7992 1190 : gfc_add_modify (&clobbers, var, clobber);
7993 : }
7994 : }
7995 : /* Catch base objects that are not variables. */
7996 160358 : if (e->ts.type == BT_CLASS
7997 3767 : && e->expr_type != EXPR_VARIABLE
7998 330 : && expr && e == expr->base_expr)
7999 80 : base_object = build_fold_indirect_ref_loc (input_location,
8000 : parmse.expr);
8001 :
8002 : /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
8003 : allocated on entry, it must be deallocated. */
8004 132915 : if (fsym && fsym->attr.intent == INTENT_OUT
8005 3340 : && (fsym->attr.allocatable
8006 3191 : || (fsym->ts.type == BT_CLASS
8007 277 : && CLASS_DATA (fsym)->attr.allocatable))
8008 160674 : && !is_CFI_desc (fsym, NULL))
8009 : {
8010 316 : stmtblock_t block;
8011 316 : tree ptr;
8012 :
8013 316 : defer_to_dealloc_blk = true;
8014 :
8015 316 : parmse.expr = gfc_evaluate_data_ref_now (parmse.expr,
8016 : &parmse.pre);
8017 :
8018 316 : if (parmse.class_container != NULL_TREE)
8019 174 : parmse.class_container
8020 174 : = gfc_evaluate_data_ref_now (parmse.class_container,
8021 : &parmse.pre);
8022 :
8023 316 : gfc_init_block (&block);
8024 316 : ptr = parmse.expr;
8025 316 : if (e->ts.type == BT_CLASS)
8026 174 : ptr = gfc_class_data_get (ptr);
8027 :
8028 316 : tree cls = parmse.class_container;
8029 316 : tmp = gfc_deallocate_scalar_with_status (ptr, NULL_TREE,
8030 : NULL_TREE, true,
8031 : e, e->ts, cls);
8032 316 : gfc_add_expr_to_block (&block, tmp);
8033 316 : gfc_add_modify (&block, ptr,
8034 316 : fold_convert (TREE_TYPE (ptr),
8035 : null_pointer_node));
8036 :
8037 316 : if (fsym->ts.type == BT_CLASS)
8038 167 : gfc_reset_vptr (&block, nullptr,
8039 : build_fold_indirect_ref (parmse.expr),
8040 167 : fsym->ts.u.derived);
8041 :
8042 316 : if (fsym->attr.optional
8043 42 : && e->expr_type == EXPR_VARIABLE
8044 42 : && e->symtree->n.sym->attr.optional)
8045 : {
8046 36 : tmp = fold_build3_loc (input_location, COND_EXPR,
8047 : void_type_node,
8048 18 : gfc_conv_expr_present (e->symtree->n.sym),
8049 : gfc_finish_block (&block),
8050 : build_empty_stmt (input_location));
8051 : }
8052 : else
8053 298 : tmp = gfc_finish_block (&block);
8054 :
8055 316 : gfc_add_expr_to_block (&dealloc_blk, tmp);
8056 : }
8057 :
8058 : /* A class array element needs converting back to be a
8059 : class object, if the formal argument is a class object. */
8060 160358 : if (fsym && fsym->ts.type == BT_CLASS
8061 3363 : && e->ts.type == BT_CLASS
8062 3339 : && ((CLASS_DATA (fsym)->as
8063 417 : && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
8064 2922 : || CLASS_DATA (e)->attr.dimension))
8065 : {
8066 545 : gfc_se class_se = parmse;
8067 545 : gfc_init_block (&class_se.pre);
8068 545 : gfc_init_block (&class_se.post);
8069 :
8070 818 : gfc_conv_class_to_class (&class_se, e, fsym->ts, false,
8071 545 : fsym->attr.intent != INTENT_IN
8072 : && (CLASS_DATA (fsym)->attr.class_pointer
8073 273 : || CLASS_DATA (fsym)->attr.allocatable),
8074 545 : fsym->attr.optional
8075 198 : && e->expr_type == EXPR_VARIABLE
8076 198 : && e->symtree->n.sym->attr.optional,
8077 545 : CLASS_DATA (fsym)->attr.class_pointer
8078 509 : || CLASS_DATA (fsym)->attr.allocatable);
8079 :
8080 545 : parmse.expr = class_se.expr;
8081 521 : stmtblock_t *class_pre_block = defer_to_dealloc_blk
8082 545 : ? &dealloc_blk
8083 : : &parmse.pre;
8084 545 : gfc_add_block_to_block (class_pre_block, &class_se.pre);
8085 545 : gfc_add_block_to_block (&parmse.post, &class_se.post);
8086 : }
8087 :
8088 132915 : if (fsym && (fsym->ts.type == BT_DERIVED
8089 120645 : || fsym->ts.type == BT_ASSUMED)
8090 13137 : && e->ts.type == BT_CLASS
8091 416 : && !CLASS_DATA (e)->attr.dimension
8092 380 : && !CLASS_DATA (e)->attr.codimension)
8093 : {
8094 380 : parmse.expr = gfc_class_data_get (parmse.expr);
8095 : /* The result is a class temporary, whose _data component
8096 : must be freed to avoid a memory leak. */
8097 380 : if (e->expr_type == EXPR_FUNCTION
8098 23 : && CLASS_DATA (e)->attr.allocatable)
8099 : {
8100 19 : tree zero;
8101 :
8102 : /* Finalize the expression. */
8103 19 : gfc_finalize_tree_expr (&parmse, NULL,
8104 19 : gfc_expr_attr (e), e->rank);
8105 19 : gfc_add_block_to_block (&parmse.post,
8106 : &parmse.finalblock);
8107 :
8108 : /* Then free the class _data. */
8109 19 : zero = build_int_cst (TREE_TYPE (parmse.expr), 0);
8110 19 : tmp = fold_build2_loc (input_location, NE_EXPR,
8111 : logical_type_node,
8112 : parmse.expr, zero);
8113 19 : tmp = build3_v (COND_EXPR, tmp,
8114 : gfc_call_free (parmse.expr),
8115 : build_empty_stmt (input_location));
8116 19 : gfc_add_expr_to_block (&parmse.post, tmp);
8117 19 : gfc_add_modify (&parmse.post, parmse.expr, zero);
8118 : }
8119 : }
8120 :
8121 : /* Wrap scalar variable in a descriptor. We need to convert
8122 : the address of a pointer back to the pointer itself before,
8123 : we can assign it to the data field. */
8124 :
8125 132915 : if (fsym && fsym->as && fsym->as->type == AS_ASSUMED_RANK
8126 1369 : && fsym->ts.type != BT_CLASS && e->expr_type != EXPR_NULL)
8127 : {
8128 1297 : tmp = parmse.expr;
8129 1297 : if (TREE_CODE (tmp) == ADDR_EXPR)
8130 773 : tmp = TREE_OPERAND (tmp, 0);
8131 1297 : parmse.expr = gfc_conv_scalar_to_descriptor (&parmse, tmp,
8132 : fsym->attr);
8133 1297 : parmse.expr = gfc_build_addr_expr (NULL_TREE,
8134 : parmse.expr);
8135 : }
8136 131618 : else if (fsym && e->expr_type != EXPR_NULL
8137 131320 : && ((fsym->attr.pointer
8138 1740 : && fsym->attr.flavor != FL_PROCEDURE)
8139 129586 : || (fsym->attr.proc_pointer
8140 199 : && !(e->expr_type == EXPR_VARIABLE
8141 199 : && e->symtree->n.sym->attr.dummy))
8142 129399 : || (fsym->attr.proc_pointer
8143 12 : && e->expr_type == EXPR_VARIABLE
8144 12 : && gfc_is_proc_ptr_comp (e))
8145 129393 : || (fsym->attr.allocatable
8146 1329 : && fsym->attr.flavor != FL_PROCEDURE)))
8147 : {
8148 : /* Scalar pointer dummy args require an extra level of
8149 : indirection. The null pointer already contains
8150 : this level of indirection. */
8151 3250 : parm_kind = SCALAR_POINTER;
8152 3250 : parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
8153 : }
8154 : }
8155 : }
8156 62356 : else if (e->ts.type == BT_CLASS
8157 2945 : && fsym && fsym->ts.type == BT_CLASS
8158 2503 : && (CLASS_DATA (fsym)->attr.dimension
8159 55 : || CLASS_DATA (fsym)->attr.codimension))
8160 : {
8161 : /* Pass a class array. */
8162 2503 : gfc_conv_expr_descriptor (&parmse, e);
8163 2503 : bool defer_to_dealloc_blk = false;
8164 :
8165 2503 : if (fsym->attr.optional
8166 798 : && e->expr_type == EXPR_VARIABLE
8167 798 : && e->symtree->n.sym->attr.optional)
8168 : {
8169 438 : stmtblock_t block;
8170 :
8171 438 : gfc_init_block (&block);
8172 438 : gfc_add_block_to_block (&block, &parmse.pre);
8173 :
8174 876 : tree t = fold_build3_loc (input_location, COND_EXPR,
8175 : void_type_node,
8176 438 : gfc_conv_expr_present (e->symtree->n.sym),
8177 : gfc_finish_block (&block),
8178 : build_empty_stmt (input_location));
8179 :
8180 438 : gfc_add_expr_to_block (&parmse.pre, t);
8181 : }
8182 :
8183 : /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
8184 : allocated on entry, it must be deallocated. */
8185 2503 : if (fsym->attr.intent == INTENT_OUT
8186 171 : && CLASS_DATA (fsym)->attr.allocatable)
8187 : {
8188 134 : stmtblock_t block;
8189 134 : tree ptr;
8190 :
8191 : /* In case the data reference to deallocate is dependent on
8192 : its own content, save the resulting pointer to a variable
8193 : and only use that variable from now on, before the
8194 : expression becomes invalid. */
8195 134 : parmse.expr = gfc_evaluate_data_ref_now (parmse.expr,
8196 : &parmse.pre);
8197 :
8198 134 : if (parmse.class_container != NULL_TREE)
8199 134 : parmse.class_container
8200 134 : = gfc_evaluate_data_ref_now (parmse.class_container,
8201 : &parmse.pre);
8202 :
8203 134 : gfc_init_block (&block);
8204 134 : ptr = parmse.expr;
8205 134 : ptr = gfc_class_data_get (ptr);
8206 :
8207 134 : tree cls = parmse.class_container;
8208 134 : tmp = gfc_deallocate_with_status (ptr, NULL_TREE,
8209 : NULL_TREE, NULL_TREE,
8210 : NULL_TREE, true, e,
8211 : GFC_CAF_COARRAY_NOCOARRAY,
8212 : cls);
8213 134 : gfc_add_expr_to_block (&block, tmp);
8214 134 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
8215 : void_type_node, ptr,
8216 : null_pointer_node);
8217 134 : gfc_add_expr_to_block (&block, tmp);
8218 134 : gfc_reset_vptr (&block, e, parmse.class_container);
8219 :
8220 134 : if (fsym->attr.optional
8221 30 : && e->expr_type == EXPR_VARIABLE
8222 30 : && (!e->ref
8223 30 : || (e->ref->type == REF_ARRAY
8224 0 : && e->ref->u.ar.type != AR_FULL))
8225 0 : && e->symtree->n.sym->attr.optional)
8226 : {
8227 0 : tmp = fold_build3_loc (input_location, COND_EXPR,
8228 : void_type_node,
8229 0 : gfc_conv_expr_present (e->symtree->n.sym),
8230 : gfc_finish_block (&block),
8231 : build_empty_stmt (input_location));
8232 : }
8233 : else
8234 134 : tmp = gfc_finish_block (&block);
8235 :
8236 134 : gfc_add_expr_to_block (&dealloc_blk, tmp);
8237 134 : defer_to_dealloc_blk = true;
8238 : }
8239 :
8240 2503 : gfc_se class_se = parmse;
8241 2503 : gfc_init_block (&class_se.pre);
8242 2503 : gfc_init_block (&class_se.post);
8243 :
8244 2503 : if (e->expr_type != EXPR_VARIABLE)
8245 : {
8246 : int n;
8247 : /* Set the bounds and offset correctly. */
8248 84 : for (n = 0; n < e->rank; n++)
8249 42 : gfc_conv_shift_descriptor_lbound (&class_se.pre,
8250 : class_se.expr,
8251 : n, gfc_index_one_node);
8252 : }
8253 :
8254 : /* The conversion does not repackage the reference to a class
8255 : array - _data descriptor. */
8256 3954 : gfc_conv_class_to_class (&class_se, e, fsym->ts, false,
8257 2503 : fsym->attr.intent != INTENT_IN
8258 : && (CLASS_DATA (fsym)->attr.class_pointer
8259 1265 : || CLASS_DATA (fsym)->attr.allocatable),
8260 2503 : fsym->attr.optional
8261 798 : && e->expr_type == EXPR_VARIABLE
8262 798 : && e->symtree->n.sym->attr.optional,
8263 2503 : CLASS_DATA (fsym)->attr.class_pointer
8264 2077 : || CLASS_DATA (fsym)->attr.allocatable);
8265 :
8266 2503 : parmse.expr = class_se.expr;
8267 2369 : stmtblock_t *class_pre_block = defer_to_dealloc_blk
8268 2503 : ? &dealloc_blk
8269 : : &parmse.pre;
8270 2503 : gfc_add_block_to_block (class_pre_block, &class_se.pre);
8271 2503 : gfc_add_block_to_block (&parmse.post, &class_se.post);
8272 :
8273 2503 : if (e->expr_type == EXPR_OP
8274 24 : && POINTER_TYPE_P (TREE_TYPE (parmse.expr))
8275 2527 : && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (parmse.expr, 0))))
8276 : {
8277 24 : tree cond;
8278 24 : tree dealloc_expr = gfc_finish_block (&parmse.post);
8279 24 : tmp = TREE_OPERAND (parmse.expr, 0);
8280 24 : gfc_init_block (&parmse.post);
8281 24 : cond = gfc_class_data_get (tmp);
8282 24 : tmp = gfc_deallocate_alloc_comp_no_caf (e->ts.u.derived,
8283 : tmp, e->rank, true);
8284 24 : gfc_add_expr_to_block (&parmse.post, tmp);
8285 24 : cond = gfc_class_data_get (TREE_OPERAND (parmse.expr, 0));
8286 24 : cond = gfc_conv_descriptor_data_get (cond);
8287 24 : cond = fold_build2_loc (input_location, NE_EXPR,
8288 : logical_type_node, cond,
8289 24 : build_int_cst (TREE_TYPE (cond), 0));
8290 24 : tmp = build3_v (COND_EXPR, cond, dealloc_expr,
8291 : build_empty_stmt (input_location));
8292 :
8293 : /* This specific case should not be processed further and so
8294 : bundle everything up and proceed to the next argument. */
8295 24 : if (fsym && need_interface_mapping && e)
8296 12 : gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
8297 24 : gfc_add_expr_to_block (&parmse.post, tmp);
8298 24 : gfc_add_block_to_block (&se->pre, &parmse.pre);
8299 24 : gfc_add_block_to_block (&post, &parmse.post);
8300 24 : gfc_add_block_to_block (&se->finalblock, &parmse.finalblock);
8301 24 : vec_safe_push (arglist, parmse.expr);
8302 24 : continue;
8303 24 : }
8304 2479 : }
8305 : else
8306 : {
8307 : /* If the argument is a function call that may not create
8308 : a temporary for the result, we have to check that we
8309 : can do it, i.e. that there is no alias between this
8310 : argument and another one. */
8311 59853 : if (gfc_get_noncopying_intrinsic_argument (e) != NULL)
8312 : {
8313 472 : gfc_expr *iarg;
8314 472 : sym_intent intent;
8315 :
8316 472 : if (fsym != NULL)
8317 463 : intent = fsym->attr.intent;
8318 : else
8319 : intent = INTENT_UNKNOWN;
8320 :
8321 472 : if (gfc_check_fncall_dependency (e, intent, sym, args,
8322 : NOT_ELEMENTAL))
8323 21 : parmse.force_tmp = 1;
8324 :
8325 472 : iarg = e->value.function.actual->expr;
8326 :
8327 : /* Temporary needed if aliasing due to host association. */
8328 472 : if (sym->attr.contained
8329 234 : && !sym->attr.pure
8330 234 : && !sym->attr.implicit_pure
8331 150 : && !sym->attr.use_assoc
8332 150 : && iarg->expr_type == EXPR_VARIABLE
8333 84 : && sym->ns == iarg->symtree->n.sym->ns)
8334 36 : parmse.force_tmp = 1;
8335 :
8336 : /* Ditto within module. */
8337 472 : if (sym->attr.use_assoc
8338 6 : && !sym->attr.pure
8339 6 : && !sym->attr.implicit_pure
8340 0 : && iarg->expr_type == EXPR_VARIABLE
8341 0 : && sym->module == iarg->symtree->n.sym->module)
8342 0 : parmse.force_tmp = 1;
8343 : }
8344 :
8345 : /* Special case for assumed-rank arrays: when passing an
8346 : argument to a nonallocatable/nonpointer dummy, the bounds have
8347 : to be reset as otherwise a last-dim ubound of -1 is
8348 : indistinguishable from an assumed-size array in the callee. */
8349 59853 : if (!sym->attr.is_bind_c && e && fsym && fsym->as
8350 36492 : && fsym->as->type == AS_ASSUMED_RANK
8351 12057 : && e->rank != -1
8352 11694 : && e->expr_type == EXPR_VARIABLE
8353 11229 : && ((fsym->ts.type == BT_CLASS
8354 0 : && !CLASS_DATA (fsym)->attr.class_pointer
8355 0 : && !CLASS_DATA (fsym)->attr.allocatable)
8356 11229 : || (fsym->ts.type != BT_CLASS
8357 11229 : && !fsym->attr.pointer && !fsym->attr.allocatable)))
8358 : {
8359 : /* Change AR_FULL to a (:,:,:) ref to force bounds update. */
8360 10686 : gfc_ref *ref;
8361 10950 : for (ref = e->ref; ref->next; ref = ref->next)
8362 : {
8363 342 : if (ref->next->type == REF_INQUIRY)
8364 : break;
8365 294 : if (ref->type == REF_ARRAY
8366 30 : && ref->u.ar.type != AR_ELEMENT)
8367 : break;
8368 10686 : };
8369 10686 : if (ref->u.ar.type == AR_FULL
8370 9934 : && ref->u.ar.as->type != AS_ASSUMED_SIZE)
8371 9814 : ref->u.ar.type = AR_SECTION;
8372 : }
8373 :
8374 59853 : if (sym->attr.is_bind_c && e && is_CFI_desc (fsym, NULL))
8375 : /* Implement F2018, 18.3.6, list item (5), bullet point 2. */
8376 5850 : gfc_conv_gfc_desc_to_cfi_desc (&parmse, e, fsym);
8377 :
8378 54003 : else if (fsym && fsym->attr.value && fsym->attr.dimension
8379 102 : && e->rank != -1)
8380 : /* VALUE array dummy: pass a private copy of the actual
8381 : argument. Allocatable components are copied deeply, so
8382 : that the callee cannot reach the actual argument's data.
8383 : The symbol passed is that of the actual argument, so that
8384 : the copy is suppressed and a null pointer passed when an
8385 : optional actual argument is absent. */
8386 102 : gfc_conv_subref_array_arg (&parmse, e, nodesc_arg, INTENT_IN,
8387 : fsym, sym->name,
8388 102 : e->expr_type == EXPR_VARIABLE
8389 102 : ? e->symtree->n.sym : NULL,
8390 : false, true);
8391 :
8392 53901 : else if (pointer_array_result_p (e)
8393 157 : && !(fsym && fsym->attr.pointer)
8394 54052 : && copy_in_out_allowed (fsym, e, nodesc_arg))
8395 : /* A pointer function result is copied into a temporary when
8396 : its elements are not spaced by the element size. */
8397 : {
8398 145 : gfc_expr *var = pointer_result_var (&parmse.pre, e);
8399 283 : gfc_conv_subref_array_arg (&parmse, var, nodesc_arg,
8400 138 : fsym ? fsym->attr.intent : INTENT_INOUT,
8401 : fsym, sym->name, NULL, true, false,
8402 : dummy_accepts_strided_arg (fsym, nodesc_arg));
8403 145 : gfc_symtree *st = var->symtree;
8404 145 : gfc_free_expr (var);
8405 145 : gfc_free_symbol (st->n.sym);
8406 145 : XDELETE (st);
8407 : }
8408 :
8409 53756 : else if (e->expr_type == EXPR_VARIABLE
8410 41806 : && is_subref_array (e)
8411 1327 : && !(fsym && fsym->attr.pointer)
8412 54797 : && copy_in_out_allowed (fsym, e, nodesc_arg))
8413 : /* The actual argument is a component reference to an
8414 : array of derived types. In this case, the argument
8415 : is converted to a temporary, which is passed and then
8416 : written back after the procedure call. The elements of
8417 : a span addressed dummy passed on as a whole are usually
8418 : contiguous, so the copy is made conditional. A dummy that
8419 : has a descriptor takes any stride, so for it the condition
8420 : is only that the span be the element length. */
8421 : {
8422 831 : bool whole_span = is_whole_span_addressed_dummy (e);
8423 1632 : gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
8424 789 : fsym ? fsym->attr.intent : INTENT_INOUT,
8425 : fsym, sym->name, NULL, whole_span, false,
8426 : whole_span
8427 12 : && dummy_accepts_strided_arg (fsym,
8428 : nodesc_arg));
8429 : }
8430 :
8431 52925 : else if (e->ts.type == BT_CLASS && CLASS_DATA (e)->as
8432 439 : && CLASS_DATA (e)->as->type == AS_ASSUMED_SIZE
8433 18 : && nodesc_arg && fsym->ts.type == BT_DERIVED)
8434 : /* An assumed size class actual argument being passed to
8435 : a 'no descriptor' formal argument just requires the
8436 : data pointer to be passed. For class dummy arguments
8437 : this is stored in the symbol backend decl.. */
8438 6 : parmse.expr = e->symtree->n.sym->backend_decl;
8439 :
8440 52919 : else if (gfc_is_class_array_ref (e, NULL)
8441 432 : && fsym && fsym->ts.type == BT_DERIVED
8442 53111 : && copy_in_out_allowed (fsym, e, nodesc_arg))
8443 : /* The actual argument is a component reference to an
8444 : array of derived types. In this case, the argument
8445 : is converted to a temporary, which is passed and then
8446 : written back after the procedure call. */
8447 114 : gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
8448 114 : fsym->attr.intent);
8449 :
8450 52805 : else if (gfc_is_class_array_function (e)
8451 13 : && fsym && fsym->ts.type == BT_DERIVED
8452 52818 : && copy_in_out_allowed (fsym, e, nodesc_arg))
8453 : /* See previous comment. For function actual argument,
8454 : the write out is not needed so the intent is set as
8455 : intent in. */
8456 : {
8457 13 : e->must_finalize = 1;
8458 13 : gfc_conv_subref_array_arg (&parmse, e, nodesc_arg, INTENT_IN);
8459 : }
8460 49218 : else if (fsym && fsym->attr.contiguous
8461 90 : && (fsym->attr.target
8462 1775 : ? gfc_is_not_contiguous (e)
8463 1685 : : !gfc_is_simply_contiguous (e, false, true))
8464 351 : && gfc_expr_is_variable (e)
8465 54906 : && e->rank != -1)
8466 : {
8467 327 : gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
8468 327 : fsym->attr.intent);
8469 : }
8470 : else
8471 : {
8472 : /* Having declined copy-in/copy-out above, a subobject of an
8473 : array is described by a spanned descriptor. */
8474 52465 : if (e->expr_type == EXPR_VARIABLE && is_subref_array (e))
8475 496 : parmse.force_no_tmp = 1;
8476 :
8477 : /* This is where we introduce a temporary to store the
8478 : result of a non-lvalue array expression. */
8479 52465 : gfc_conv_array_parameter (&parmse, e, nodesc_arg, fsym,
8480 : sym->name, NULL);
8481 : }
8482 :
8483 : /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
8484 : allocated on entry, it must be deallocated.
8485 : CFI descriptors are handled elsewhere. */
8486 56230 : if (fsym && fsym->attr.allocatable
8487 1799 : && fsym->attr.intent == INTENT_OUT
8488 59621 : && !is_CFI_desc (fsym, NULL))
8489 : {
8490 167 : if (fsym->ts.type == BT_DERIVED
8491 53 : && fsym->ts.u.derived->attr.alloc_comp)
8492 : {
8493 : // deallocate the components first
8494 11 : tmp = gfc_deallocate_alloc_comp (fsym->ts.u.derived,
8495 : parmse.expr, e->rank);
8496 : /* But check whether dummy argument is optional. */
8497 11 : if (tmp != NULL_TREE
8498 11 : && fsym->attr.optional
8499 6 : && e->expr_type == EXPR_VARIABLE
8500 6 : && e->symtree->n.sym->attr.optional)
8501 : {
8502 6 : tree present;
8503 6 : present = gfc_conv_expr_present (e->symtree->n.sym);
8504 6 : tmp = build3_v (COND_EXPR, present, tmp,
8505 : build_empty_stmt (input_location));
8506 : }
8507 11 : if (tmp != NULL_TREE)
8508 11 : gfc_add_expr_to_block (&dealloc_blk, tmp);
8509 : }
8510 :
8511 167 : tmp = parmse.expr;
8512 : /* With bind(C), the actual argument is replaced by a bind-C
8513 : descriptor; in this case, the data component arrives here,
8514 : which shall not be dereferenced, but still freed and
8515 : nullified. */
8516 167 : if (TREE_TYPE(tmp) != pvoid_type_node)
8517 167 : tmp = build_fold_indirect_ref_loc (input_location,
8518 : parmse.expr);
8519 167 : tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
8520 : NULL_TREE, NULL_TREE, true,
8521 : e,
8522 : GFC_CAF_COARRAY_NOCOARRAY);
8523 167 : if (fsym->attr.optional
8524 48 : && e->expr_type == EXPR_VARIABLE
8525 48 : && e->symtree->n.sym->attr.optional)
8526 48 : tmp = fold_build3_loc (input_location, COND_EXPR,
8527 : void_type_node,
8528 24 : gfc_conv_expr_present (e->symtree->n.sym),
8529 : tmp, build_empty_stmt (input_location));
8530 167 : gfc_add_expr_to_block (&dealloc_blk, tmp);
8531 : }
8532 : }
8533 : }
8534 : /* Special case for an assumed-rank dummy argument. */
8535 277395 : if (!sym->attr.is_bind_c && e && fsym && e->rank > 0
8536 58666 : && (fsym->ts.type == BT_CLASS
8537 58666 : ? (CLASS_DATA (fsym)->as
8538 4799 : && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
8539 53867 : : (fsym->as && fsym->as->type == AS_ASSUMED_RANK)))
8540 : {
8541 12857 : if (fsym->ts.type == BT_CLASS
8542 12857 : ? (CLASS_DATA (fsym)->attr.class_pointer
8543 1079 : || CLASS_DATA (fsym)->attr.allocatable)
8544 11778 : : (fsym->attr.pointer || fsym->attr.allocatable))
8545 : {
8546 : /* Unallocated allocatable arrays and unassociated pointer
8547 : arrays need their dtype setting if they are argument
8548 : associated with assumed rank dummies to set the rank. */
8549 891 : set_dtype_for_unallocated (&parmse, e);
8550 : }
8551 11966 : else if (e->expr_type == EXPR_VARIABLE
8552 11463 : && e->symtree->n.sym->attr.dummy
8553 722 : && (e->ts.type == BT_CLASS
8554 915 : ? (e->ref && e->ref->next
8555 193 : && e->ref->next->type == REF_ARRAY
8556 193 : && e->ref->next->u.ar.type == AR_FULL
8557 386 : && e->ref->next->u.ar.as->type == AS_ASSUMED_SIZE)
8558 529 : : (e->ref && e->ref->type == REF_ARRAY
8559 529 : && e->ref->u.ar.type == AR_FULL
8560 757 : && e->ref->u.ar.as->type == AS_ASSUMED_SIZE)))
8561 : {
8562 : /* Assumed-size actual to assumed-rank dummy requires
8563 : dim[rank-1].ubound = -1. */
8564 180 : tree minus_one;
8565 180 : tmp = build_fold_indirect_ref_loc (input_location, parmse.expr);
8566 180 : if (fsym->ts.type == BT_CLASS)
8567 60 : tmp = gfc_class_data_get (tmp);
8568 180 : minus_one = build_int_cst (gfc_array_index_type, -1);
8569 180 : gfc_conv_descriptor_ubound_set (&parmse.pre, tmp,
8570 180 : gfc_rank_cst[e->rank - 1],
8571 : minus_one);
8572 : }
8573 : }
8574 :
8575 : /* The case with fsym->attr.optional is that of a user subroutine
8576 : with an interface indicating an optional argument. When we call
8577 : an intrinsic subroutine, however, fsym is NULL, but we might still
8578 : have an optional argument, so we proceed to the substitution
8579 : just in case. Arguments passed to bind(c) procedures via CFI
8580 : descriptors are handled elsewhere. */
8581 263970 : if (e && (fsym == NULL || fsym->attr.optional)
8582 338144 : && !(sym->attr.is_bind_c && is_CFI_desc (fsym, NULL)))
8583 : {
8584 : /* If an optional argument is itself an optional dummy argument,
8585 : check its presence and substitute a null if absent. This is
8586 : only needed when passing an array to an elemental procedure
8587 : as then array elements are accessed - or no NULL pointer is
8588 : allowed and a "1" or "0" should be passed if not present.
8589 : When passing a non-array-descriptor full array to a
8590 : non-array-descriptor dummy, no check is needed. For
8591 : array-descriptor actual to array-descriptor dummy, see
8592 : PR 41911 for why a check has to be inserted.
8593 : fsym == NULL is checked as intrinsics required the descriptor
8594 : but do not always set fsym.
8595 : Also, it is necessary to pass a NULL pointer to library routines
8596 : which usually ignore optional arguments, so they can handle
8597 : these themselves. */
8598 59655 : if (e->expr_type == EXPR_VARIABLE
8599 26666 : && e->symtree->n.sym->attr.optional
8600 2481 : && (((e->rank != 0 && elemental_proc)
8601 2300 : || e->representation.length || e->ts.type == BT_CHARACTER
8602 2056 : || (e->rank == 0 && e->symtree->n.sym->attr.value)
8603 1940 : || (e->rank != 0
8604 1094 : && (fsym == NULL
8605 1058 : || (fsym->as
8606 296 : && (fsym->as->type == AS_ASSUMED_SHAPE
8607 241 : || fsym->as->type == AS_ASSUMED_RANK
8608 123 : || fsym->as->type == AS_DEFERRED)))))
8609 1697 : || se->ignore_optional))
8610 812 : gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
8611 812 : e->representation.length);
8612 : }
8613 :
8614 : /* Make the class container for the first argument available with class
8615 : valued transformational functions. */
8616 277395 : if (argc == 0 && e && e->ts.type == BT_CLASS
8617 5496 : && isym && isym->transformational
8618 84 : && se->ss && se->ss->info)
8619 : {
8620 84 : arg1_cntnr = parmse.expr;
8621 84 : if (POINTER_TYPE_P (TREE_TYPE (arg1_cntnr)))
8622 84 : arg1_cntnr = build_fold_indirect_ref_loc (input_location, arg1_cntnr);
8623 84 : arg1_cntnr = gfc_get_class_from_expr (arg1_cntnr);
8624 84 : se->ss->info->class_container = arg1_cntnr;
8625 : }
8626 :
8627 : /* Obtain the character length of an assumed character length procedure
8628 : from the typespec of the actual argument. */
8629 277395 : if (e
8630 263970 : && parmse.string_length == NULL_TREE
8631 227914 : && e->ts.type == BT_PROCEDURE
8632 1941 : && e->symtree->n.sym->ts.type == BT_CHARACTER
8633 21 : && e->symtree->n.sym->ts.u.cl->length != NULL
8634 21 : && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8635 : {
8636 13 : gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
8637 13 : parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
8638 : }
8639 :
8640 277395 : if (fsym && e)
8641 : {
8642 : /* Obtain the character length for a NULL() actual with a character
8643 : MOLD argument. Otherwise substitute a suitable dummy length.
8644 : Here we handle non-optional dummies of non-bind(c) procedures. */
8645 232042 : if (e->expr_type == EXPR_NULL
8646 763 : && fsym->ts.type == BT_CHARACTER
8647 296 : && !fsym->attr.optional
8648 232260 : && !(sym->attr.is_bind_c && is_CFI_desc (fsym, NULL)))
8649 216 : conv_null_actual (&parmse, e, fsym);
8650 : }
8651 :
8652 : /* If any actual argument of the procedure is allocatable and passed
8653 : to an allocatable dummy with INTENT(OUT), we conservatively
8654 : evaluate actual argument expressions before deallocations are
8655 : performed and the procedure is executed. May create temporaries.
8656 : This ensures we conform to F2023:15.5.3, 15.5.4. */
8657 263970 : if (e && fsym && force_eval_args
8658 1180 : && fsym->attr.intent != INTENT_OUT
8659 277822 : && !gfc_is_constant_expr (e))
8660 274 : parmse.expr = gfc_evaluate_now (parmse.expr, &parmse.pre);
8661 :
8662 277395 : if (fsym && need_interface_mapping && e)
8663 41043 : gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
8664 :
8665 277395 : gfc_add_block_to_block (&se->pre, &parmse.pre);
8666 277395 : gfc_add_block_to_block (&post, &parmse.post);
8667 277395 : gfc_add_block_to_block (&se->finalblock, &parmse.finalblock);
8668 :
8669 : /* Allocated allocatable components of derived types must be
8670 : deallocated for non-variable scalars, array arguments to elemental
8671 : procedures, and array arguments with descriptor to non-elemental
8672 : procedures. As bounds information for descriptorless arrays is no
8673 : longer available here, they are dealt with in trans-array.cc
8674 : (gfc_conv_array_parameter). A derived-type temporary for a
8675 : parenthesized variable shares the components of the variable, and
8676 : the copy of a parenthesized scalar CLASS variable frees its own. */
8677 263970 : if (e && (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS)
8678 29914 : && e->ts.u.derived->attr.alloc_comp
8679 7927 : && (e->rank == 0 || elemental_proc || !nodesc_arg)
8680 7783 : && !expr_may_alias_variables (e, elemental_proc)
8681 277828 : && !((e->ts.type == BT_DERIVED || e->rank == 0)
8682 432 : && parenthesized_variable (e)))
8683 : {
8684 385 : int parm_rank;
8685 : /* It is known the e returns a structure type with at least one
8686 : allocatable component. When e is a function, ensure that the
8687 : function is called once only by using a temporary variable. */
8688 385 : if (!DECL_P (parmse.expr) && e->expr_type == EXPR_FUNCTION)
8689 123 : parmse.expr = gfc_evaluate_now_loc (input_location,
8690 : parmse.expr, &se->pre);
8691 :
8692 385 : if ((fsym && fsym->attr.value) || e->expr_type == EXPR_ARRAY)
8693 182 : tmp = parmse.expr;
8694 : else
8695 203 : tmp = build_fold_indirect_ref_loc (input_location,
8696 : parmse.expr);
8697 :
8698 385 : parm_rank = e->rank;
8699 385 : switch (parm_kind)
8700 : {
8701 : case (ELEMENTAL):
8702 : case (SCALAR):
8703 385 : parm_rank = 0;
8704 : break;
8705 :
8706 0 : case (SCALAR_POINTER):
8707 0 : tmp = build_fold_indirect_ref_loc (input_location,
8708 : tmp);
8709 0 : break;
8710 : }
8711 :
8712 385 : if (e->ts.type == BT_DERIVED && fsym && fsym->ts.type == BT_CLASS)
8713 : {
8714 : /* The derived type is passed to gfc_deallocate_alloc_comp.
8715 : Therefore, class actuals can be handled correctly but derived
8716 : types passed to class formals need the _data component. */
8717 76 : tmp = gfc_class_data_get (tmp);
8718 76 : if (!CLASS_DATA (fsym)->attr.dimension)
8719 : {
8720 50 : if (UNLIMITED_POLY (fsym))
8721 : {
8722 6 : tree type = gfc_typenode_for_spec (&e->ts);
8723 6 : type = build_pointer_type (type);
8724 6 : tmp = fold_convert (type, tmp);
8725 : }
8726 50 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
8727 : }
8728 : }
8729 :
8730 385 : if (parenthesized_variable (e))
8731 : {
8732 0 : tree local_tmp;
8733 0 : local_tmp = gfc_evaluate_now (tmp, &se->pre);
8734 0 : local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp,
8735 : parm_rank, 0);
8736 0 : gfc_add_expr_to_block (&se->post, local_tmp);
8737 : }
8738 :
8739 : /* Items of array expressions passed to a polymorphic formal arguments
8740 : create their own clean up, so prevent double free. */
8741 385 : if (!finalized && !e->must_finalize
8742 384 : && !(e->expr_type == EXPR_ARRAY && fsym
8743 86 : && fsym->ts.type == BT_CLASS))
8744 : {
8745 364 : bool scalar_res_outside_loop;
8746 1079 : scalar_res_outside_loop = e->expr_type == EXPR_FUNCTION
8747 176 : && parm_rank == 0
8748 527 : && parmse.loop;
8749 :
8750 : /* Scalars passed to an assumed rank argument are converted to
8751 : a descriptor. Obtain the data field before deallocating any
8752 : allocatable components. */
8753 304 : if (parm_rank == 0 && e->expr_type != EXPR_ARRAY
8754 630 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
8755 19 : tmp = gfc_conv_descriptor_data_get (tmp);
8756 :
8757 364 : if (scalar_res_outside_loop)
8758 : {
8759 : /* Go through the ss chain to find the argument and use
8760 : the stored value. */
8761 30 : gfc_ss *tmp_ss = parmse.loop->ss;
8762 72 : for (; tmp_ss; tmp_ss = tmp_ss->next)
8763 60 : if (tmp_ss->info
8764 48 : && tmp_ss->info->expr == e
8765 18 : && tmp_ss->info->data.scalar.value != NULL_TREE)
8766 : {
8767 18 : tmp = tmp_ss->info->data.scalar.value;
8768 18 : break;
8769 : }
8770 : }
8771 :
8772 364 : STRIP_NOPS (tmp);
8773 :
8774 364 : if (derived_array != NULL_TREE)
8775 0 : tmp = gfc_deallocate_alloc_comp (e->ts.u.derived,
8776 : derived_array,
8777 : parm_rank);
8778 364 : else if ((e->ts.type == BT_CLASS
8779 30 : && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
8780 364 : || e->ts.type == BT_DERIVED)
8781 364 : tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp,
8782 : parm_rank, 0, true);
8783 0 : else if (e->ts.type == BT_CLASS)
8784 0 : tmp = gfc_deallocate_alloc_comp (CLASS_DATA (e)->ts.u.derived,
8785 : tmp, parm_rank);
8786 :
8787 364 : if (scalar_res_outside_loop)
8788 30 : gfc_add_expr_to_block (&parmse.loop->post, tmp);
8789 : else
8790 334 : gfc_prepend_expr_to_block (&post, tmp);
8791 : }
8792 : }
8793 :
8794 : /* Add argument checking of passing an unallocated/NULL actual to
8795 : a nonallocatable/nonpointer dummy. */
8796 :
8797 277395 : if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
8798 : {
8799 6546 : symbol_attribute attr;
8800 6546 : char *msg;
8801 6546 : tree cond;
8802 6546 : tree tmp;
8803 6546 : symbol_attribute fsym_attr;
8804 :
8805 6546 : if (fsym)
8806 : {
8807 6385 : if (fsym->ts.type == BT_CLASS)
8808 : {
8809 321 : fsym_attr = CLASS_DATA (fsym)->attr;
8810 321 : fsym_attr.pointer = fsym_attr.class_pointer;
8811 : }
8812 : else
8813 6064 : fsym_attr = fsym->attr;
8814 : }
8815 :
8816 6546 : if (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
8817 4094 : attr = gfc_expr_attr (e);
8818 : else
8819 6081 : goto end_pointer_check;
8820 :
8821 : /* In Fortran 2008 it's allowed to pass a NULL pointer/nonallocated
8822 : allocatable to an optional dummy, cf. 12.5.2.12. */
8823 4094 : if (fsym != NULL && fsym->attr.optional && !attr.proc_pointer
8824 1038 : && (gfc_option.allow_std & GFC_STD_F2008) != 0)
8825 1032 : goto end_pointer_check;
8826 :
8827 3062 : if (attr.optional)
8828 : {
8829 : /* If the actual argument is an optional pointer/allocatable and
8830 : the formal argument takes an nonpointer optional value,
8831 : it is invalid to pass a non-present argument on, even
8832 : though there is no technical reason for this in gfortran.
8833 : See Fortran 2003, Section 12.4.1.6 item (7)+(8). */
8834 96 : tree present, null_ptr, type;
8835 :
8836 96 : if (attr.allocatable
8837 12 : && (fsym == NULL || !fsym_attr.allocatable))
8838 0 : msg = xasprintf ("Allocatable actual argument '%s' is not "
8839 : "allocated or not present",
8840 0 : e->symtree->n.sym->name);
8841 96 : else if (attr.pointer
8842 24 : && (fsym == NULL || !fsym_attr.pointer))
8843 12 : msg = xasprintf ("Pointer actual argument '%s' is not "
8844 : "associated or not present",
8845 12 : e->symtree->n.sym->name);
8846 84 : else if (attr.proc_pointer && !e->value.function.actual
8847 0 : && (fsym == NULL || !fsym_attr.proc_pointer))
8848 0 : msg = xasprintf ("Proc-pointer actual argument '%s' is not "
8849 : "associated or not present",
8850 0 : e->symtree->n.sym->name);
8851 : else
8852 84 : goto end_pointer_check;
8853 :
8854 12 : present = gfc_conv_expr_present (e->symtree->n.sym);
8855 12 : type = TREE_TYPE (present);
8856 12 : present = fold_build2_loc (input_location, EQ_EXPR,
8857 : logical_type_node, present,
8858 : fold_convert (type,
8859 : null_pointer_node));
8860 12 : type = TREE_TYPE (parmse.expr);
8861 12 : null_ptr = fold_build2_loc (input_location, EQ_EXPR,
8862 : logical_type_node, parmse.expr,
8863 : fold_convert (type,
8864 : null_pointer_node));
8865 12 : cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
8866 : logical_type_node, present, null_ptr);
8867 : }
8868 : else
8869 : {
8870 2966 : if (attr.allocatable
8871 244 : && (fsym == NULL || !fsym_attr.allocatable))
8872 190 : msg = xasprintf ("Allocatable actual argument '%s' is not "
8873 190 : "allocated", e->symtree->n.sym->name);
8874 2776 : else if (attr.pointer
8875 260 : && (fsym == NULL || !fsym_attr.pointer))
8876 184 : msg = xasprintf ("Pointer actual argument '%s' is not "
8877 184 : "associated", e->symtree->n.sym->name);
8878 2592 : else if (attr.proc_pointer && !e->value.function.actual
8879 80 : && (fsym == NULL
8880 50 : || (!fsym_attr.proc_pointer && !fsym_attr.optional)))
8881 79 : msg = xasprintf ("Proc-pointer actual argument '%s' is not "
8882 79 : "associated", e->symtree->n.sym->name);
8883 : else
8884 2513 : goto end_pointer_check;
8885 :
8886 453 : tmp = parmse.expr;
8887 453 : if (fsym && fsym->ts.type == BT_CLASS && !attr.proc_pointer)
8888 : {
8889 76 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
8890 70 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
8891 76 : tmp = gfc_class_data_get (tmp);
8892 76 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
8893 3 : tmp = gfc_conv_descriptor_data_get (tmp);
8894 : }
8895 :
8896 : /* If the argument is passed by value, we need to strip the
8897 : INDIRECT_REF. */
8898 453 : if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
8899 12 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
8900 :
8901 453 : cond = fold_build2_loc (input_location, EQ_EXPR,
8902 : logical_type_node, tmp,
8903 453 : fold_convert (TREE_TYPE (tmp),
8904 : null_pointer_node));
8905 : }
8906 :
8907 465 : gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
8908 : msg);
8909 465 : free (msg);
8910 : }
8911 270849 : end_pointer_check:
8912 :
8913 : /* Deferred length dummies pass the character length by reference
8914 : so that the value can be returned. */
8915 277395 : if (parmse.string_length && fsym && fsym->ts.deferred)
8916 : {
8917 795 : if (INDIRECT_REF_P (parmse.string_length))
8918 : {
8919 : /* In chains of functions/procedure calls the string_length already
8920 : is a pointer to the variable holding the length. Therefore
8921 : remove the deref on call. */
8922 90 : tmp = parmse.string_length;
8923 90 : parmse.string_length = TREE_OPERAND (parmse.string_length, 0);
8924 : }
8925 : else
8926 : {
8927 705 : tmp = parmse.string_length;
8928 705 : if (!VAR_P (tmp) && TREE_CODE (tmp) != COMPONENT_REF)
8929 61 : tmp = gfc_evaluate_now (parmse.string_length, &se->pre);
8930 705 : parmse.string_length = gfc_build_addr_expr (NULL_TREE, tmp);
8931 : }
8932 :
8933 795 : if (e && e->expr_type == EXPR_VARIABLE
8934 638 : && fsym->attr.allocatable
8935 368 : && e->ts.u.cl->backend_decl
8936 368 : && VAR_P (e->ts.u.cl->backend_decl))
8937 : {
8938 284 : if (INDIRECT_REF_P (tmp))
8939 0 : tmp = TREE_OPERAND (tmp, 0);
8940 284 : gfc_add_modify (&se->post, e->ts.u.cl->backend_decl,
8941 : fold_convert (gfc_charlen_type_node, tmp));
8942 : }
8943 : }
8944 :
8945 : /* Character strings are passed as two parameters, a length and a
8946 : pointer - except for Bind(c) and c_ptrs which only pass the pointer.
8947 : An unlimited polymorphic formal argument likewise does not
8948 : need the length. */
8949 277395 : if (parmse.string_length != NULL_TREE
8950 37484 : && !sym->attr.is_bind_c
8951 36734 : && !(fsym && fsym->ts.type == BT_DERIVED && fsym->ts.u.derived
8952 6 : && fsym->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
8953 6 : && fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING )
8954 30849 : && !(fsym && fsym->ts.type == BT_ASSUMED)
8955 30740 : && !(fsym && UNLIMITED_POLY (fsym)))
8956 36432 : vec_safe_push (stringargs, parmse.string_length);
8957 :
8958 : /* When calling __copy for character expressions to unlimited
8959 : polymorphic entities, the dst argument needs a string length. */
8960 52819 : if (sym->name[0] == '_' && e && e->ts.type == BT_CHARACTER
8961 5410 : && startswith (sym->name, "__vtab_CHARACTER")
8962 0 : && arg->next && arg->next->expr
8963 0 : && (arg->next->expr->ts.type == BT_DERIVED
8964 0 : || arg->next->expr->ts.type == BT_CLASS)
8965 277395 : && arg->next->expr->ts.u.derived->attr.unlimited_polymorphic)
8966 0 : vec_safe_push (stringargs, parmse.string_length);
8967 :
8968 : /* For descriptorless coarrays and assumed-shape coarray dummies, we
8969 : pass the token and the offset as additional arguments. */
8970 277395 : if (fsym && e == NULL && flag_coarray == GFC_FCOARRAY_LIB
8971 144 : && attr->codimension && !attr->allocatable)
8972 : {
8973 : /* Token and offset. */
8974 5 : vec_safe_push (stringargs, null_pointer_node);
8975 5 : vec_safe_push (stringargs, build_int_cst (gfc_array_index_type, 0));
8976 5 : gcc_assert (fsym->attr.optional);
8977 : }
8978 244404 : else if (fsym && flag_coarray == GFC_FCOARRAY_LIB && attr->codimension
8979 305 : && !attr->allocatable)
8980 : {
8981 123 : tree caf_decl, caf_type, caf_desc = NULL_TREE;
8982 123 : tree offset, tmp2;
8983 :
8984 123 : caf_decl = gfc_get_tree_for_caf_expr (e);
8985 123 : caf_type = TREE_TYPE (caf_decl);
8986 123 : if (POINTER_TYPE_P (caf_type)
8987 123 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_type)))
8988 3 : caf_desc = TREE_TYPE (caf_type);
8989 120 : else if (GFC_DESCRIPTOR_TYPE_P (caf_type))
8990 : caf_desc = caf_type;
8991 :
8992 51 : if (caf_desc
8993 51 : && (GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_ALLOCATABLE
8994 0 : || GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_POINTER))
8995 : {
8996 102 : tmp = POINTER_TYPE_P (TREE_TYPE (caf_decl))
8997 54 : ? build_fold_indirect_ref (caf_decl)
8998 : : caf_decl;
8999 51 : tmp = gfc_conv_descriptor_token (tmp);
9000 : }
9001 72 : else if (DECL_LANG_SPECIFIC (caf_decl)
9002 72 : && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
9003 12 : tmp = GFC_DECL_TOKEN (caf_decl);
9004 : else
9005 : {
9006 60 : gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
9007 : && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
9008 60 : tmp = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
9009 : }
9010 :
9011 123 : vec_safe_push (stringargs, tmp);
9012 :
9013 123 : if (caf_desc
9014 123 : && GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_ALLOCATABLE)
9015 51 : offset = build_int_cst (gfc_array_index_type, 0);
9016 72 : else if (DECL_LANG_SPECIFIC (caf_decl)
9017 72 : && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
9018 12 : offset = GFC_DECL_CAF_OFFSET (caf_decl);
9019 60 : else if (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) != NULL_TREE)
9020 0 : offset = GFC_TYPE_ARRAY_CAF_OFFSET (caf_type);
9021 : else
9022 60 : offset = build_int_cst (gfc_array_index_type, 0);
9023 :
9024 123 : if (caf_desc)
9025 : {
9026 102 : tmp = POINTER_TYPE_P (TREE_TYPE (caf_decl))
9027 54 : ? build_fold_indirect_ref (caf_decl)
9028 : : caf_decl;
9029 51 : tmp = gfc_conv_descriptor_data_get (tmp);
9030 : }
9031 : else
9032 : {
9033 72 : gcc_assert (POINTER_TYPE_P (caf_type));
9034 72 : tmp = caf_decl;
9035 : }
9036 :
9037 108 : tmp2 = fsym->ts.type == BT_CLASS
9038 123 : ? gfc_class_data_get (parmse.expr) : parmse.expr;
9039 123 : if ((fsym->ts.type != BT_CLASS
9040 108 : && (fsym->as->type == AS_ASSUMED_SHAPE
9041 59 : || fsym->as->type == AS_ASSUMED_RANK))
9042 74 : || (fsym->ts.type == BT_CLASS
9043 15 : && (CLASS_DATA (fsym)->as->type == AS_ASSUMED_SHAPE
9044 10 : || CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)))
9045 : {
9046 54 : if (fsym->ts.type == BT_CLASS)
9047 5 : gcc_assert (!POINTER_TYPE_P (TREE_TYPE (tmp2)));
9048 : else
9049 : {
9050 49 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
9051 49 : tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
9052 : }
9053 54 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)));
9054 54 : tmp2 = gfc_conv_descriptor_data_get (tmp2);
9055 : }
9056 69 : else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
9057 10 : tmp2 = gfc_conv_descriptor_data_get (tmp2);
9058 : else
9059 : {
9060 59 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
9061 : }
9062 :
9063 123 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
9064 : gfc_array_index_type,
9065 : fold_convert (gfc_array_index_type, tmp2),
9066 : fold_convert (gfc_array_index_type, tmp));
9067 123 : offset = fold_build2_loc (input_location, PLUS_EXPR,
9068 : gfc_array_index_type, offset, tmp);
9069 :
9070 123 : vec_safe_push (stringargs, offset);
9071 123 : }
9072 277267 : else if (e && gfc_pdt_requires_hidden_arg (fsym))
9073 : {
9074 208 : gfc_actual_arglist *parms;
9075 208 : tree hidden_pdt;
9076 208 : gfc_symbol *derived;
9077 :
9078 : /* Build a temporary of the declared type. */
9079 208 : if (IS_CLASS_PDT (e))
9080 : {
9081 54 : tmp = gfc_typenode_for_spec (&CLASS_DATA (e)->ts);
9082 54 : hidden_pdt = gfc_create_var (tmp, "pdt");
9083 54 : derived = CLASS_DATA (e)->ts.u.derived;
9084 : }
9085 : else
9086 : {
9087 154 : tmp = gfc_typenode_for_spec (&e->ts);
9088 154 : hidden_pdt = gfc_create_var (tmp, "pdt");
9089 154 : derived = e->ts.u.derived;
9090 : }
9091 :
9092 : /* Obtain the PDT parameter list. */
9093 208 : parms = e->param_list ? e->param_list :
9094 208 : e->symtree->n.sym->param_list;
9095 :
9096 208 : if (gfc_spec_list_type (parms, derived) == SPEC_EXPLICIT)
9097 : {
9098 : /* If all the parameters are explcit, build the hidden PDT... */
9099 66 : tmp = gfc_allocate_pdt_comp (derived, hidden_pdt,
9100 : 0, parms);
9101 66 : gfc_add_expr_to_block (&se->pre, tmp);
9102 66 : tmp = gfc_build_addr_expr (NULL_TREE, hidden_pdt);
9103 66 : vec_safe_push (stringargs, tmp);
9104 : /* ...and deallocate it after the calls. */
9105 66 : tmp = gfc_deallocate_pdt_comp (derived,
9106 : hidden_pdt, 0);
9107 66 : gfc_add_expr_to_block (&se->post, tmp);
9108 : }
9109 142 : else if (e->expr_type == EXPR_VARIABLE
9110 142 : && e->symtree->n.sym->attr.dummy
9111 6 : && e->symtree->n.sym->hidden_pdt_dummy_decl != NULL_TREE)
9112 : /* Otherwise hope that this was a dummy with a hidden arg. */
9113 6 : vec_safe_push (stringargs,
9114 6 : e->symtree->n.sym->hidden_pdt_dummy_decl);
9115 : else
9116 : {
9117 : /* Or else just add it without the parameter set here. */
9118 136 : tmp = gfc_build_addr_expr (NULL_TREE, hidden_pdt);
9119 136 : vec_safe_push (stringargs, tmp);
9120 : }
9121 : }
9122 277395 : vec_safe_push (arglist, parmse.expr);
9123 : }
9124 135278 : actual_arg_level--;
9125 :
9126 135278 : gfc_add_block_to_block (&se->pre, &dealloc_blk);
9127 135278 : gfc_add_block_to_block (&se->pre, &clobbers);
9128 135278 : gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
9129 :
9130 135278 : if (comp)
9131 2066 : ts = comp->ts;
9132 133212 : else if (sym->ts.type == BT_CLASS)
9133 912 : ts = CLASS_DATA (sym)->ts;
9134 : else
9135 132300 : ts = sym->ts;
9136 :
9137 135278 : if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
9138 210 : se->string_length = build_int_cst (gfc_charlen_type_node, 1);
9139 135068 : else if (ts.type == BT_CHARACTER)
9140 : {
9141 5245 : if (ts.u.cl->length == NULL)
9142 : {
9143 : /* Assumed character length results are not allowed by C418 of the 2003
9144 : standard and are trapped in resolve.cc; except in the case of SPREAD
9145 : (and other intrinsics?) and dummy functions. In the case of SPREAD,
9146 : we take the character length of the first argument for the result.
9147 : For dummies, we have to look through the formal argument list for
9148 : this function and use the character length found there.
9149 : Likewise, we handle the case of deferred-length character dummy
9150 : arguments to intrinsics that determine the characteristics of
9151 : the result, which cannot be deferred-length. */
9152 2423 : if (expr->value.function.isym)
9153 1787 : ts.deferred = false;
9154 2423 : if (ts.deferred)
9155 629 : cl.backend_decl = gfc_create_var (gfc_charlen_type_node, "slen");
9156 1794 : else if (!sym->attr.dummy)
9157 1787 : cl.backend_decl = (*stringargs)[0];
9158 : else
9159 : {
9160 7 : formal = gfc_sym_get_dummy_args (sym->ns->proc_name);
9161 26 : for (; formal; formal = formal->next)
9162 12 : if (strcmp (formal->sym->name, sym->name) == 0)
9163 7 : cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
9164 : }
9165 : len = cl.backend_decl;
9166 : }
9167 : else
9168 : {
9169 2822 : tree tmp;
9170 :
9171 : /* Calculate the length of the returned string. */
9172 2822 : gfc_init_se (&parmse, NULL);
9173 2822 : if (need_interface_mapping)
9174 1946 : gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
9175 : else
9176 876 : gfc_conv_expr (&parmse, ts.u.cl->length);
9177 2822 : gfc_add_block_to_block (&se->pre, &parmse.pre);
9178 2822 : gfc_add_block_to_block (&se->post, &parmse.post);
9179 2822 : tmp = parmse.expr;
9180 : /* TODO: It would be better to have the charlens as
9181 : gfc_charlen_type_node already when the interface is
9182 : created instead of converting it here (see PR 84615). */
9183 2822 : tmp = fold_build2_loc (input_location, MAX_EXPR,
9184 : gfc_charlen_type_node,
9185 : fold_convert (gfc_charlen_type_node, tmp),
9186 : build_zero_cst (gfc_charlen_type_node));
9187 2822 : cl.backend_decl = tmp;
9188 :
9189 : /* The length was fully computed above from the specification
9190 : expression, without needing the callee to actually run. */
9191 2822 : call_needed_for_length = false;
9192 : }
9193 :
9194 : /* Set up a charlen structure for it. */
9195 5245 : cl.next = NULL;
9196 5245 : cl.length = NULL;
9197 5245 : ts.u.cl = &cl;
9198 :
9199 5245 : len = cl.backend_decl;
9200 : }
9201 :
9202 2066 : byref = (comp && (comp->attr.dimension
9203 1997 : || (comp->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)))
9204 135278 : || (!comp && gfc_return_by_reference (sym));
9205 :
9206 : if (byref)
9207 : {
9208 19260 : if (se->direct_byref)
9209 : {
9210 : /* Sometimes, too much indirection can be applied; e.g. for
9211 : function_result = array_valued_recursive_function. */
9212 7059 : if (TREE_TYPE (TREE_TYPE (se->expr))
9213 7059 : && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
9214 7077 : && GFC_DESCRIPTOR_TYPE_P
9215 : (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
9216 18 : se->expr = build_fold_indirect_ref_loc (input_location,
9217 : se->expr);
9218 :
9219 : /* If the lhs of an assignment x = f(..) is allocatable and
9220 : f2003 is allowed, we must do the automatic reallocation.
9221 : TODO - deal with intrinsics, without using a temporary. */
9222 7059 : if (flag_realloc_lhs
9223 6984 : && se->ss && se->ss->loop_chain
9224 203 : && se->ss->loop_chain->is_alloc_lhs
9225 203 : && !expr->value.function.isym
9226 203 : && sym->result->as != NULL)
9227 : {
9228 : /* Evaluate the bounds of the result, if known. */
9229 203 : gfc_set_loop_bounds_from_array_spec (&mapping, se,
9230 : sym->result->as);
9231 :
9232 : /* Perform the automatic reallocation. */
9233 203 : tmp = gfc_alloc_allocatable_for_assignment (se->loop,
9234 : expr, NULL);
9235 203 : gfc_add_expr_to_block (&se->pre, tmp);
9236 :
9237 : /* Pass the temporary as the first argument. */
9238 203 : result = info->descriptor;
9239 : }
9240 : else
9241 6856 : result = build_fold_indirect_ref_loc (input_location,
9242 : se->expr);
9243 7059 : vec_safe_push (retargs, se->expr);
9244 : }
9245 12201 : else if (comp && comp->attr.dimension)
9246 : {
9247 66 : gcc_assert (se->loop && info);
9248 :
9249 : /* Set the type of the array. vtable charlens are not always reliable.
9250 : Use the interface, if possible. */
9251 66 : if (comp->ts.type == BT_CHARACTER
9252 1 : && expr->symtree->n.sym->ts.type == BT_CLASS
9253 1 : && comp->ts.interface && comp->ts.interface->result)
9254 1 : tmp = gfc_typenode_for_spec (&comp->ts.interface->result->ts);
9255 : else
9256 65 : tmp = gfc_typenode_for_spec (&comp->ts);
9257 66 : gcc_assert (se->ss->dimen == se->loop->dimen);
9258 :
9259 : /* Evaluate the bounds of the result, if known. */
9260 66 : gfc_set_loop_bounds_from_array_spec (&mapping, se, comp->as);
9261 :
9262 : /* If the lhs of an assignment x = f(..) is allocatable and
9263 : f2003 is allowed, we must not generate the function call
9264 : here but should just send back the results of the mapping.
9265 : This is signalled by the function ss being flagged. */
9266 66 : if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
9267 : {
9268 0 : gfc_free_interface_mapping (&mapping);
9269 0 : return has_alternate_specifier;
9270 : }
9271 :
9272 : /* Create a temporary to store the result. In case the function
9273 : returns a pointer, the temporary will be a shallow copy and
9274 : mustn't be deallocated. */
9275 66 : callee_alloc = comp->attr.allocatable || comp->attr.pointer;
9276 66 : gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
9277 : tmp, NULL_TREE, false,
9278 : !comp->attr.pointer, callee_alloc,
9279 66 : &se->ss->info->expr->where);
9280 :
9281 : /* Index a pointer result by the span the callee returns. */
9282 66 : if (comp->attr.pointer && VAR_P (info->descriptor))
9283 0 : GFC_DECL_PTR_ARRAY_P (info->descriptor) = 1;
9284 :
9285 : /* Pass the temporary as the first argument. */
9286 66 : result = info->descriptor;
9287 66 : tmp = gfc_build_addr_expr (NULL_TREE, result);
9288 66 : vec_safe_push (retargs, tmp);
9289 : }
9290 11906 : else if (!comp && sym->result->attr.dimension)
9291 : {
9292 8820 : gcc_assert (se->loop && info);
9293 :
9294 : /* Set the type of the array. */
9295 8820 : tmp = gfc_typenode_for_spec (&ts);
9296 8820 : tmp = arg1_cntnr ? TREE_TYPE (arg1_cntnr) : tmp;
9297 8820 : gcc_assert (se->ss->dimen == se->loop->dimen);
9298 :
9299 : /* Evaluate the bounds of the result, if known. */
9300 8820 : gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
9301 :
9302 : /* If the lhs of an assignment x = f(..) is allocatable and
9303 : f2003 is allowed, we must not generate the function call
9304 : here but should just send back the results of the mapping.
9305 : This is signalled by the function ss being flagged. */
9306 8820 : if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
9307 : {
9308 0 : gfc_free_interface_mapping (&mapping);
9309 0 : return has_alternate_specifier;
9310 : }
9311 :
9312 : /* Create a temporary to store the result. In case the function
9313 : returns a pointer, the temporary will be a shallow copy and
9314 : mustn't be deallocated. */
9315 8820 : callee_alloc = sym->attr.allocatable || sym->attr.pointer;
9316 8820 : gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
9317 : tmp, NULL_TREE, false,
9318 : !sym->attr.pointer, callee_alloc,
9319 8820 : &se->ss->info->expr->where);
9320 :
9321 : /* Index a pointer result by the span the callee returns. */
9322 8820 : if (sym->attr.pointer && VAR_P (info->descriptor))
9323 356 : GFC_DECL_PTR_ARRAY_P (info->descriptor) = 1;
9324 :
9325 : /* Pass the temporary as the first argument. */
9326 8820 : result = info->descriptor;
9327 8820 : tmp = gfc_build_addr_expr (NULL_TREE, result);
9328 8820 : vec_safe_push (retargs, tmp);
9329 : }
9330 3315 : else if (ts.type == BT_CHARACTER)
9331 : {
9332 : /* Pass the string length. */
9333 3254 : type = gfc_get_character_type (ts.kind, ts.u.cl);
9334 3254 : type = build_pointer_type (type);
9335 :
9336 : /* Emit a DECL_EXPR for the VLA type. */
9337 3254 : tmp = TREE_TYPE (type);
9338 3254 : if (TYPE_SIZE (tmp)
9339 3254 : && TREE_CODE (TYPE_SIZE (tmp)) != INTEGER_CST)
9340 : {
9341 1936 : tmp = build_decl (input_location, TYPE_DECL, NULL_TREE, tmp);
9342 1936 : DECL_ARTIFICIAL (tmp) = 1;
9343 1936 : DECL_IGNORED_P (tmp) = 1;
9344 1936 : tmp = fold_build1_loc (input_location, DECL_EXPR,
9345 1936 : TREE_TYPE (tmp), tmp);
9346 1936 : gfc_add_expr_to_block (&se->pre, tmp);
9347 : }
9348 :
9349 : /* Return an address to a char[0:len-1]* temporary for
9350 : character pointers. */
9351 3254 : if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
9352 229 : || (comp && (comp->attr.pointer || comp->attr.allocatable)))
9353 : {
9354 648 : var = gfc_create_var (type, "pstr");
9355 :
9356 648 : if ((!comp && sym->attr.allocatable)
9357 21 : || (comp && comp->attr.allocatable))
9358 : {
9359 361 : gfc_add_modify (&se->pre, var,
9360 361 : fold_convert (TREE_TYPE (var),
9361 : null_pointer_node));
9362 361 : tmp = gfc_call_free (var);
9363 361 : gfc_add_expr_to_block (&se->post, tmp);
9364 : }
9365 :
9366 : /* Provide an address expression for the function arguments. */
9367 648 : var = gfc_build_addr_expr (NULL_TREE, var);
9368 : }
9369 : else
9370 2606 : var = gfc_conv_string_tmp (se, type, len);
9371 :
9372 3254 : vec_safe_push (retargs, var);
9373 : }
9374 : else
9375 : {
9376 61 : gcc_assert (flag_f2c && ts.type == BT_COMPLEX);
9377 :
9378 61 : type = gfc_get_complex_type (ts.kind);
9379 61 : var = gfc_build_addr_expr (NULL_TREE, gfc_create_var (type, "cmplx"));
9380 61 : vec_safe_push (retargs, var);
9381 : }
9382 :
9383 : /* Add the string length to the argument list. */
9384 19260 : if (ts.type == BT_CHARACTER && ts.deferred)
9385 : {
9386 629 : tmp = len;
9387 629 : if (!VAR_P (tmp))
9388 0 : tmp = gfc_evaluate_now (len, &se->pre);
9389 629 : TREE_STATIC (tmp) = 1;
9390 629 : gfc_add_modify (&se->pre, tmp,
9391 629 : build_int_cst (TREE_TYPE (tmp), 0));
9392 629 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
9393 629 : vec_safe_push (retargs, tmp);
9394 : }
9395 18631 : else if (ts.type == BT_CHARACTER)
9396 4616 : vec_safe_push (retargs, len);
9397 : }
9398 :
9399 135278 : gfc_free_interface_mapping (&mapping);
9400 :
9401 : /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS. */
9402 251617 : arglen = (vec_safe_length (arglist) + vec_safe_length (optionalargs)
9403 161318 : + vec_safe_length (stringargs) + vec_safe_length (append_args));
9404 135278 : vec_safe_reserve (retargs, arglen);
9405 :
9406 : /* Add the return arguments. */
9407 135278 : vec_safe_splice (retargs, arglist);
9408 :
9409 : /* Add the hidden present status for optional+value to the arguments. */
9410 135278 : vec_safe_splice (retargs, optionalargs);
9411 :
9412 : /* Add the hidden string length parameters to the arguments. */
9413 135278 : vec_safe_splice (retargs, stringargs);
9414 :
9415 : /* We may want to append extra arguments here. This is used e.g. for
9416 : calls to libgfortran_matmul_??, which need extra information. */
9417 135278 : vec_safe_splice (retargs, append_args);
9418 :
9419 135278 : arglist = retargs;
9420 :
9421 : /* Generate the actual call. */
9422 135278 : is_builtin = false;
9423 135278 : if (base_object == NULL_TREE)
9424 135198 : conv_function_val (se, &is_builtin, sym, expr, args);
9425 : else
9426 80 : conv_base_obj_fcn_val (se, base_object, expr);
9427 :
9428 : /* If there are alternate return labels, function type should be
9429 : integer. Can't modify the type in place though, since it can be shared
9430 : with other functions. For dummy arguments, the typing is done to
9431 : this result, even if it has to be repeated for each call. */
9432 135278 : if (has_alternate_specifier
9433 135278 : && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
9434 : {
9435 7 : if (!sym->attr.dummy)
9436 : {
9437 0 : TREE_TYPE (sym->backend_decl)
9438 0 : = build_function_type (integer_type_node,
9439 0 : TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
9440 0 : se->expr = gfc_build_addr_expr (NULL_TREE, sym->backend_decl);
9441 : }
9442 : else
9443 7 : TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
9444 : }
9445 :
9446 135278 : fntype = TREE_TYPE (TREE_TYPE (se->expr));
9447 135278 : se->expr = build_call_vec (TREE_TYPE (fntype), se->expr, arglist);
9448 :
9449 135278 : if (is_builtin)
9450 570 : se->expr = update_builtin_function (se->expr, sym);
9451 :
9452 : /* Allocatable scalar function results must be freed and nullified
9453 : after use. This necessitates the creation of a temporary to
9454 : hold the result to prevent duplicate calls. */
9455 135278 : symbol_attribute attr = comp ? comp->attr : sym->attr;
9456 135278 : bool allocatable = attr.allocatable && !attr.dimension;
9457 138900 : gfc_symbol *der = comp ?
9458 2066 : comp->ts.type == BT_DERIVED ? comp->ts.u.derived : NULL
9459 : :
9460 133212 : sym->ts.type == BT_DERIVED ? sym->ts.u.derived : NULL;
9461 3622 : bool finalizable = der != NULL && der->ns->proc_name
9462 7241 : && gfc_is_finalizable (der, NULL);
9463 :
9464 : /* Finalize the function results passed as arguments with the post code
9465 : of the arguments, so that any enclosing expression keeps them. */
9466 135278 : if (!byref && (comp ? comp->attr.function : sym->attr.function))
9467 : {
9468 34432 : gfc_add_block_to_block (&se->finalblock, &post);
9469 34432 : gfc_add_block_to_block (&post, &se->finalblock);
9470 : }
9471 :
9472 135278 : if (!byref && finalizable)
9473 266 : gfc_finalize_tree_expr (se, der, attr, expr->rank);
9474 :
9475 135278 : if (!byref && sym->ts.type != BT_CHARACTER
9476 115808 : && allocatable && !finalizable)
9477 : {
9478 236 : tmp = gfc_create_var (TREE_TYPE (se->expr), NULL);
9479 236 : gfc_add_modify (&se->pre, tmp, se->expr);
9480 236 : se->expr = tmp;
9481 236 : tmp = gfc_call_free (tmp);
9482 236 : gfc_add_expr_to_block (&post, tmp);
9483 236 : gfc_add_modify (&post, se->expr, build_int_cst (TREE_TYPE (se->expr), 0));
9484 : }
9485 :
9486 : /* If we have a pointer function, but we don't want a pointer, e.g.
9487 : something like
9488 : x = f()
9489 : where f is pointer valued, we have to dereference the result. */
9490 135278 : if (!se->want_pointer && !byref
9491 115410 : && ((!comp && (sym->attr.pointer || sym->attr.allocatable))
9492 1724 : || (comp && (comp->attr.pointer || comp->attr.allocatable))))
9493 462 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
9494 :
9495 : /* f2c calling conventions require a scalar default real function to
9496 : return a double precision result. Convert this back to default
9497 : real. We only care about the cases that can happen in Fortran 77.
9498 : */
9499 135278 : if (flag_f2c && sym->ts.type == BT_REAL
9500 98 : && sym->ts.kind == gfc_default_real_kind
9501 74 : && !sym->attr.pointer
9502 55 : && !sym->attr.allocatable
9503 43 : && !sym->attr.always_explicit)
9504 43 : se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
9505 :
9506 : /* A pure function may still have side-effects - it may modify its
9507 : parameters. */
9508 135278 : TREE_SIDE_EFFECTS (se->expr) = 1;
9509 : #if 0
9510 : if (!sym->attr.pure)
9511 : TREE_SIDE_EFFECTS (se->expr) = 1;
9512 : #endif
9513 :
9514 135278 : if (byref)
9515 : {
9516 : /* Add the function call to the pre chain. There is no expression. */
9517 19260 : if (!se->no_function_call || call_needed_for_length)
9518 19228 : gfc_add_expr_to_block (&se->pre, se->expr);
9519 :
9520 19260 : se->expr = NULL_TREE;
9521 :
9522 19260 : if (!se->direct_byref)
9523 : {
9524 12201 : if ((sym->attr.dimension && !comp) || (comp && comp->attr.dimension))
9525 : {
9526 8886 : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
9527 : {
9528 : /* Check the data pointer hasn't been modified. This would
9529 : happen in a function returning a pointer. */
9530 251 : tmp = gfc_conv_descriptor_data_get (info->descriptor);
9531 251 : tmp = fold_build2_loc (input_location, NE_EXPR,
9532 : logical_type_node,
9533 : tmp, info->data);
9534 251 : gfc_trans_runtime_check (true, false, tmp, &se->pre, NULL,
9535 : gfc_msg_fault);
9536 : }
9537 8886 : se->expr = info->descriptor;
9538 : /* Bundle in the string length. */
9539 8886 : se->string_length = len;
9540 :
9541 8886 : if (finalizable)
9542 6 : gfc_finalize_tree_expr (se, der, attr, expr->rank);
9543 : }
9544 3315 : else if (ts.type == BT_CHARACTER)
9545 : {
9546 : /* Dereference for character pointer results. */
9547 3254 : if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
9548 229 : || (comp && (comp->attr.pointer || comp->attr.allocatable)))
9549 648 : se->expr = build_fold_indirect_ref_loc (input_location, var);
9550 : else
9551 2606 : se->expr = var;
9552 :
9553 3254 : se->string_length = len;
9554 : }
9555 : else
9556 : {
9557 61 : gcc_assert (ts.type == BT_COMPLEX && flag_f2c);
9558 61 : se->expr = build_fold_indirect_ref_loc (input_location, var);
9559 : }
9560 : }
9561 : }
9562 :
9563 : /* Associate the rhs class object's meta-data with the result, when the
9564 : result is a temporary. */
9565 116344 : if (args && args->expr && args->expr->ts.type == BT_CLASS
9566 5520 : && sym->ts.type == BT_CLASS && result != NULL_TREE && DECL_P (result)
9567 135310 : && !GFC_CLASS_TYPE_P (TREE_TYPE (result)))
9568 : {
9569 32 : gfc_se parmse;
9570 32 : gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (args->expr);
9571 :
9572 32 : gfc_init_se (&parmse, NULL);
9573 32 : parmse.data_not_needed = 1;
9574 32 : gfc_conv_expr (&parmse, class_expr);
9575 32 : if (!DECL_LANG_SPECIFIC (result))
9576 32 : gfc_allocate_lang_decl (result);
9577 32 : GFC_DECL_SAVED_DESCRIPTOR (result) = parmse.expr;
9578 32 : gfc_free_expr (class_expr);
9579 : /* -fcheck= can add diagnostic code, which has to be placed before
9580 : the call. */
9581 32 : if (parmse.pre.head != NULL)
9582 12 : gfc_add_expr_to_block (&se->pre, parmse.pre.head);
9583 32 : gcc_assert (parmse.post.head == NULL_TREE);
9584 : }
9585 :
9586 : /* Follow the function call with the argument post block. */
9587 135278 : if (byref)
9588 : {
9589 : /* Transformational functions of derived types with allocatable
9590 : components must have the result allocatable components copied
9591 : BEFORE the argument post block is appended. Copying the result
9592 : first, then freeing the argument, gives the correct order. */
9593 19260 : arg = expr->value.function.actual;
9594 19260 : if (result && arg && expr->rank
9595 14876 : && isym && isym->transformational
9596 13265 : && isym->id != GFC_ISYM_REDUCE
9597 13139 : && arg->expr
9598 13079 : && arg->expr->ts.type == BT_DERIVED
9599 271 : && (arg->expr->ts.u.derived->attr.alloc_comp
9600 223 : || arg->expr->ts.u.derived->attr.pdt_type))
9601 : {
9602 78 : tree tmp2;
9603 : /* Copy the allocatable components. We have to use a
9604 : temporary here to prevent source allocatable components
9605 : from being corrupted. */
9606 78 : tmp2 = gfc_evaluate_now (result, &se->pre);
9607 :
9608 78 : tmp = gfc_copy_alloc_comp (arg->expr->ts.u.derived,
9609 : result, tmp2, expr->rank, 0);
9610 78 : gfc_add_expr_to_block (&se->pre, tmp);
9611 78 : tmp = gfc_copy_allocatable_data (result, tmp2, TREE_TYPE(tmp2),
9612 : expr->rank);
9613 78 : gfc_add_expr_to_block (&se->pre, tmp);
9614 :
9615 : /* Free the result PST components if not direct_byref. */
9616 78 : if (!se->direct_byref && arg->expr->ts.u.derived->attr.pdt_type)
9617 : {
9618 6 : tmp = gfc_deallocate_pdt_comp (arg->expr->ts.u.derived, result,
9619 : expr->rank);
9620 6 : gfc_add_expr_to_block (&se->pre, tmp);
9621 : }
9622 : /* Finally free the temporary's data field. */
9623 78 : tmp = gfc_conv_descriptor_data_get (tmp2);
9624 78 : tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
9625 : NULL_TREE, NULL_TREE, true,
9626 : NULL, GFC_CAF_COARRAY_NOCOARRAY);
9627 78 : gfc_add_expr_to_block (&se->pre, tmp);
9628 : }
9629 :
9630 19260 : gfc_add_block_to_block (&se->pre, &post);
9631 : }
9632 : else
9633 : {
9634 : /* For a function with a class array result, save the result as
9635 : a temporary, set the info fields needed by the scalarizer and
9636 : call the finalization function of the temporary. Note that the
9637 : nullification of allocatable components needed by the result
9638 : is done in gfc_trans_assignment_1. */
9639 36149 : if (expr && (gfc_is_class_array_function (expr)
9640 35809 : || gfc_is_alloc_class_scalar_function (expr))
9641 902 : && se->expr && GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
9642 116908 : && expr->must_finalize)
9643 : {
9644 : /* TODO Eliminate the doubling of temporaries. This
9645 : one is necessary to ensure no memory leakage. */
9646 339 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
9647 :
9648 : /* Finalize the result, if necessary. */
9649 678 : attr = expr->value.function.esym
9650 339 : ? CLASS_DATA (expr->value.function.esym->result)->attr
9651 14 : : CLASS_DATA (expr)->attr;
9652 339 : if (!((gfc_is_class_array_function (expr)
9653 120 : || gfc_is_alloc_class_scalar_function (expr))
9654 339 : && attr.pointer))
9655 294 : gfc_finalize_tree_expr (se, NULL, attr, expr->rank);
9656 : }
9657 116018 : gfc_add_block_to_block (&se->post, &post);
9658 : }
9659 :
9660 : return has_alternate_specifier;
9661 : }
9662 :
9663 :
9664 : /* Fill a character string with spaces. */
9665 :
9666 : static tree
9667 31726 : fill_with_spaces (tree start, tree type, tree size)
9668 : {
9669 31726 : stmtblock_t block, loop;
9670 31726 : tree i, el, exit_label, cond, tmp;
9671 :
9672 : /* For a simple char type, we can call memset(). */
9673 31726 : if (compare_tree_int (TYPE_SIZE_UNIT (type), 1) == 0)
9674 52816 : return build_call_expr_loc (input_location,
9675 : builtin_decl_explicit (BUILT_IN_MEMSET),
9676 : 3, start,
9677 : build_int_cst (gfc_get_int_type (gfc_c_int_kind),
9678 26408 : lang_hooks.to_target_charset (' ')),
9679 : fold_convert (size_type_node, size));
9680 :
9681 : /* Otherwise, we use a loop:
9682 : for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
9683 : *el = (type) ' ';
9684 : */
9685 :
9686 : /* Initialize variables. */
9687 5318 : gfc_init_block (&block);
9688 5318 : i = gfc_create_var (sizetype, "i");
9689 5318 : gfc_add_modify (&block, i, fold_convert (sizetype, size));
9690 5318 : el = gfc_create_var (build_pointer_type (type), "el");
9691 5318 : gfc_add_modify (&block, el, fold_convert (TREE_TYPE (el), start));
9692 5318 : exit_label = gfc_build_label_decl (NULL_TREE);
9693 5318 : TREE_USED (exit_label) = 1;
9694 :
9695 :
9696 : /* Loop body. */
9697 5318 : gfc_init_block (&loop);
9698 :
9699 : /* Exit condition. */
9700 5318 : cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, i,
9701 : build_zero_cst (sizetype));
9702 5318 : tmp = build1_v (GOTO_EXPR, exit_label);
9703 5318 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
9704 : build_empty_stmt (input_location));
9705 5318 : gfc_add_expr_to_block (&loop, tmp);
9706 :
9707 : /* Assignment. */
9708 5318 : gfc_add_modify (&loop,
9709 : fold_build1_loc (input_location, INDIRECT_REF, type, el),
9710 5318 : build_int_cst (type, lang_hooks.to_target_charset (' ')));
9711 :
9712 : /* Increment loop variables. */
9713 5318 : gfc_add_modify (&loop, i,
9714 : fold_build2_loc (input_location, MINUS_EXPR, sizetype, i,
9715 5318 : TYPE_SIZE_UNIT (type)));
9716 5318 : gfc_add_modify (&loop, el,
9717 : fold_build_pointer_plus_loc (input_location,
9718 5318 : el, TYPE_SIZE_UNIT (type)));
9719 :
9720 : /* Making the loop... actually loop! */
9721 5318 : tmp = gfc_finish_block (&loop);
9722 5318 : tmp = build1_v (LOOP_EXPR, tmp);
9723 5318 : gfc_add_expr_to_block (&block, tmp);
9724 :
9725 : /* The exit label. */
9726 5318 : tmp = build1_v (LABEL_EXPR, exit_label);
9727 5318 : gfc_add_expr_to_block (&block, tmp);
9728 :
9729 :
9730 5318 : return gfc_finish_block (&block);
9731 : }
9732 :
9733 :
9734 : /* Generate code to copy a string. */
9735 :
9736 : void
9737 37682 : gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
9738 : int dkind, tree slength, tree src, int skind)
9739 : {
9740 37682 : tree tmp, dlen, slen;
9741 37682 : tree dsc;
9742 37682 : tree ssc;
9743 37682 : tree cond;
9744 37682 : tree cond2;
9745 37682 : tree tmp2;
9746 37682 : tree tmp3;
9747 37682 : tree tmp4;
9748 37682 : tree chartype;
9749 37682 : stmtblock_t tempblock;
9750 :
9751 37682 : gcc_assert (dkind == skind);
9752 :
9753 37682 : if (slength != NULL_TREE)
9754 : {
9755 37682 : slen = gfc_evaluate_now (fold_convert (gfc_charlen_type_node, slength), block);
9756 37682 : ssc = gfc_string_to_single_character (slen, src, skind);
9757 : }
9758 : else
9759 : {
9760 0 : slen = build_one_cst (gfc_charlen_type_node);
9761 0 : ssc = src;
9762 : }
9763 :
9764 37682 : if (dlength != NULL_TREE)
9765 : {
9766 37682 : dlen = gfc_evaluate_now (fold_convert (gfc_charlen_type_node, dlength), block);
9767 37682 : dsc = gfc_string_to_single_character (dlen, dest, dkind);
9768 : }
9769 : else
9770 : {
9771 0 : dlen = build_one_cst (gfc_charlen_type_node);
9772 0 : dsc = dest;
9773 : }
9774 :
9775 : /* Assign directly if the types are compatible. */
9776 37682 : if (dsc != NULL_TREE && ssc != NULL_TREE
9777 37682 : && TREE_TYPE (dsc) == TREE_TYPE (ssc))
9778 : {
9779 5956 : gfc_add_modify (block, dsc, ssc);
9780 5956 : return;
9781 : }
9782 :
9783 : /* The string copy algorithm below generates code like
9784 :
9785 : if (destlen > 0)
9786 : {
9787 : if (srclen < destlen)
9788 : {
9789 : memmove (dest, src, srclen);
9790 : // Pad with spaces.
9791 : memset (&dest[srclen], ' ', destlen - srclen);
9792 : }
9793 : else
9794 : {
9795 : // Truncate if too long.
9796 : memmove (dest, src, destlen);
9797 : }
9798 : }
9799 : */
9800 :
9801 : /* Do nothing if the destination length is zero. */
9802 31726 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node, dlen,
9803 31726 : build_zero_cst (TREE_TYPE (dlen)));
9804 :
9805 : /* For non-default character kinds, we have to multiply the string
9806 : length by the base type size. */
9807 31726 : chartype = gfc_get_char_type (dkind);
9808 31726 : slen = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (slen),
9809 : slen,
9810 31726 : fold_convert (TREE_TYPE (slen),
9811 : TYPE_SIZE_UNIT (chartype)));
9812 31726 : dlen = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (dlen),
9813 : dlen,
9814 31726 : fold_convert (TREE_TYPE (dlen),
9815 : TYPE_SIZE_UNIT (chartype)));
9816 :
9817 31726 : if (dlength && POINTER_TYPE_P (TREE_TYPE (dest)))
9818 31678 : dest = fold_convert (pvoid_type_node, dest);
9819 : else
9820 48 : dest = gfc_build_addr_expr (pvoid_type_node, dest);
9821 :
9822 31726 : if (slength && POINTER_TYPE_P (TREE_TYPE (src)))
9823 31722 : src = fold_convert (pvoid_type_node, src);
9824 : else
9825 4 : src = gfc_build_addr_expr (pvoid_type_node, src);
9826 :
9827 : /* Truncate string if source is too long. */
9828 31726 : cond2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node, slen,
9829 : dlen);
9830 :
9831 : /* Pre-evaluate pointers unless one of the IF arms will be optimized away. */
9832 31726 : if (!CONSTANT_CLASS_P (cond2))
9833 : {
9834 9898 : dest = gfc_evaluate_now (dest, block);
9835 9898 : src = gfc_evaluate_now (src, block);
9836 : }
9837 :
9838 : /* Copy and pad with spaces. */
9839 31726 : tmp3 = build_call_expr_loc (input_location,
9840 : builtin_decl_explicit (BUILT_IN_MEMMOVE),
9841 : 3, dest, src,
9842 : fold_convert (size_type_node, slen));
9843 :
9844 : /* Wstringop-overflow appears at -O3 even though this warning is not
9845 : explicitly available in fortran nor can it be switched off. If the
9846 : source length is a constant, its negative appears as a very large
9847 : positive number and triggers the warning in BUILTIN_MEMSET. Fixing
9848 : the result of the MINUS_EXPR suppresses this spurious warning. */
9849 31726 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
9850 31726 : TREE_TYPE(dlen), dlen, slen);
9851 31726 : if (slength && TREE_CONSTANT (slength))
9852 27978 : tmp = gfc_evaluate_now (tmp, block);
9853 :
9854 31726 : tmp4 = fold_build_pointer_plus_loc (input_location, dest, slen);
9855 31726 : tmp4 = fill_with_spaces (tmp4, chartype, tmp);
9856 :
9857 31726 : gfc_init_block (&tempblock);
9858 31726 : gfc_add_expr_to_block (&tempblock, tmp3);
9859 31726 : gfc_add_expr_to_block (&tempblock, tmp4);
9860 31726 : tmp3 = gfc_finish_block (&tempblock);
9861 :
9862 : /* The truncated memmove if the slen >= dlen. */
9863 31726 : tmp2 = build_call_expr_loc (input_location,
9864 : builtin_decl_explicit (BUILT_IN_MEMMOVE),
9865 : 3, dest, src,
9866 : fold_convert (size_type_node, dlen));
9867 :
9868 : /* The whole copy_string function is there. */
9869 31726 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
9870 : tmp3, tmp2);
9871 31726 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
9872 : build_empty_stmt (input_location));
9873 31726 : gfc_add_expr_to_block (block, tmp);
9874 : }
9875 :
9876 :
9877 : /* Translate a statement function.
9878 : The value of a statement function reference is obtained by evaluating the
9879 : expression using the values of the actual arguments for the values of the
9880 : corresponding dummy arguments. */
9881 :
9882 : static void
9883 269 : gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
9884 : {
9885 269 : gfc_symbol *sym;
9886 269 : gfc_symbol *fsym;
9887 269 : gfc_formal_arglist *fargs;
9888 269 : gfc_actual_arglist *args;
9889 269 : gfc_se lse;
9890 269 : gfc_se rse;
9891 269 : gfc_saved_var *saved_vars;
9892 269 : tree *temp_vars;
9893 269 : tree type;
9894 269 : tree tmp;
9895 269 : int n;
9896 :
9897 269 : sym = expr->symtree->n.sym;
9898 269 : args = expr->value.function.actual;
9899 269 : gfc_init_se (&lse, NULL);
9900 269 : gfc_init_se (&rse, NULL);
9901 :
9902 269 : n = 0;
9903 727 : for (fargs = gfc_sym_get_dummy_args (sym); fargs; fargs = fargs->next)
9904 458 : n++;
9905 269 : saved_vars = XCNEWVEC (gfc_saved_var, n);
9906 269 : temp_vars = XCNEWVEC (tree, n);
9907 :
9908 727 : for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
9909 458 : fargs = fargs->next, n++)
9910 : {
9911 : /* Each dummy shall be specified, explicitly or implicitly, to be
9912 : scalar. */
9913 458 : gcc_assert (fargs->sym->attr.dimension == 0);
9914 458 : fsym = fargs->sym;
9915 :
9916 458 : if (fsym->ts.type == BT_CHARACTER)
9917 : {
9918 : /* Copy string arguments. */
9919 48 : tree arglen;
9920 :
9921 48 : gcc_assert (fsym->ts.u.cl && fsym->ts.u.cl->length
9922 : && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT);
9923 :
9924 : /* Create a temporary to hold the value. */
9925 48 : if (fsym->ts.u.cl->backend_decl == NULL_TREE)
9926 1 : fsym->ts.u.cl->backend_decl
9927 1 : = gfc_conv_constant_to_tree (fsym->ts.u.cl->length);
9928 :
9929 48 : type = gfc_get_character_type (fsym->ts.kind, fsym->ts.u.cl);
9930 48 : temp_vars[n] = gfc_create_var (type, fsym->name);
9931 :
9932 48 : arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
9933 :
9934 48 : gfc_conv_expr (&rse, args->expr);
9935 48 : gfc_conv_string_parameter (&rse);
9936 48 : gfc_add_block_to_block (&se->pre, &lse.pre);
9937 48 : gfc_add_block_to_block (&se->pre, &rse.pre);
9938 :
9939 48 : gfc_trans_string_copy (&se->pre, arglen, temp_vars[n], fsym->ts.kind,
9940 : rse.string_length, rse.expr, fsym->ts.kind);
9941 48 : gfc_add_block_to_block (&se->pre, &lse.post);
9942 48 : gfc_add_block_to_block (&se->pre, &rse.post);
9943 : }
9944 : else
9945 : {
9946 : /* For everything else, just evaluate the expression. */
9947 :
9948 : /* Create a temporary to hold the value. */
9949 410 : type = gfc_typenode_for_spec (&fsym->ts);
9950 410 : temp_vars[n] = gfc_create_var (type, fsym->name);
9951 :
9952 410 : gfc_conv_expr (&lse, args->expr);
9953 :
9954 410 : gfc_add_block_to_block (&se->pre, &lse.pre);
9955 410 : gfc_add_modify (&se->pre, temp_vars[n], lse.expr);
9956 410 : gfc_add_block_to_block (&se->pre, &lse.post);
9957 : }
9958 :
9959 458 : args = args->next;
9960 : }
9961 :
9962 : /* Use the temporary variables in place of the real ones. */
9963 727 : for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
9964 458 : fargs = fargs->next, n++)
9965 458 : gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
9966 :
9967 269 : gfc_conv_expr (se, sym->value);
9968 :
9969 269 : if (sym->ts.type == BT_CHARACTER)
9970 : {
9971 55 : gfc_conv_const_charlen (sym->ts.u.cl);
9972 :
9973 : /* Force the expression to the correct length. */
9974 55 : if (!INTEGER_CST_P (se->string_length)
9975 101 : || tree_int_cst_lt (se->string_length,
9976 46 : sym->ts.u.cl->backend_decl))
9977 : {
9978 31 : type = gfc_get_character_type (sym->ts.kind, sym->ts.u.cl);
9979 31 : tmp = gfc_create_var (type, sym->name);
9980 31 : tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
9981 31 : gfc_trans_string_copy (&se->pre, sym->ts.u.cl->backend_decl, tmp,
9982 : sym->ts.kind, se->string_length, se->expr,
9983 : sym->ts.kind);
9984 31 : se->expr = tmp;
9985 : }
9986 55 : se->string_length = sym->ts.u.cl->backend_decl;
9987 : }
9988 :
9989 : /* Restore the original variables. */
9990 727 : for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
9991 458 : fargs = fargs->next, n++)
9992 458 : gfc_restore_sym (fargs->sym, &saved_vars[n]);
9993 269 : free (temp_vars);
9994 269 : free (saved_vars);
9995 269 : }
9996 :
9997 :
9998 : /* Translate a function expression. */
9999 :
10000 : static void
10001 329867 : gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
10002 : {
10003 329867 : gfc_symbol *sym;
10004 :
10005 329867 : if (expr->value.function.isym)
10006 : {
10007 277695 : gfc_conv_intrinsic_function (se, expr);
10008 277695 : return;
10009 : }
10010 :
10011 : /* expr.value.function.esym is the resolved (specific) function symbol for
10012 : most functions. However this isn't set for dummy procedures. */
10013 52172 : sym = expr->value.function.esym;
10014 52172 : if (!sym)
10015 1688 : sym = expr->symtree->n.sym;
10016 :
10017 : /* The IEEE_ARITHMETIC functions are caught here. */
10018 52172 : if (sym->from_intmod == INTMOD_IEEE_ARITHMETIC)
10019 13939 : if (gfc_conv_ieee_arithmetic_function (se, expr))
10020 : return;
10021 :
10022 : /* We distinguish statement functions from general functions to improve
10023 : runtime performance. */
10024 39715 : if (sym->attr.proc == PROC_ST_FUNCTION)
10025 : {
10026 269 : gfc_conv_statement_function (se, expr);
10027 269 : return;
10028 : }
10029 :
10030 39446 : gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
10031 : NULL);
10032 : }
10033 :
10034 :
10035 : /* Determine whether the given EXPR_CONSTANT is a zero initializer. */
10036 :
10037 : static bool
10038 48938 : is_zero_initializer_p (gfc_expr * expr)
10039 : {
10040 48938 : if (expr->expr_type != EXPR_CONSTANT)
10041 : return false;
10042 :
10043 : /* We ignore constants with prescribed memory representations for now. */
10044 14160 : if (expr->representation.string)
10045 : return false;
10046 :
10047 14142 : switch (expr->ts.type)
10048 : {
10049 8297 : case BT_INTEGER:
10050 8297 : return mpz_cmp_si (expr->value.integer, 0) == 0;
10051 :
10052 4408 : case BT_REAL:
10053 4408 : return mpfr_zero_p (expr->value.real)
10054 4408 : && MPFR_SIGN (expr->value.real) >= 0;
10055 :
10056 932 : case BT_LOGICAL:
10057 932 : return expr->value.logical == 0;
10058 :
10059 286 : case BT_COMPLEX:
10060 286 : return mpfr_zero_p (mpc_realref (expr->value.complex))
10061 186 : && MPFR_SIGN (mpc_realref (expr->value.complex)) >= 0
10062 186 : && mpfr_zero_p (mpc_imagref (expr->value.complex))
10063 454 : && MPFR_SIGN (mpc_imagref (expr->value.complex)) >= 0;
10064 :
10065 : default:
10066 : break;
10067 : }
10068 : return false;
10069 : }
10070 :
10071 :
10072 : static void
10073 37447 : gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
10074 : {
10075 37447 : gfc_ss *ss;
10076 :
10077 37447 : ss = se->ss;
10078 37447 : gcc_assert (ss != NULL && ss != gfc_ss_terminator);
10079 37447 : gcc_assert (ss->info->expr == expr && ss->info->type == GFC_SS_CONSTRUCTOR);
10080 :
10081 37447 : gfc_conv_tmp_array_ref (se);
10082 37447 : }
10083 :
10084 :
10085 : /* Build a static initializer. EXPR is the expression for the initial value.
10086 : The other parameters describe the variable of the component being
10087 : initialized. EXPR may be null. */
10088 :
10089 : tree
10090 147526 : gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
10091 : bool array, bool pointer, bool procptr)
10092 : {
10093 147526 : gfc_se se;
10094 :
10095 147526 : if (flag_coarray != GFC_FCOARRAY_LIB && ts->type == BT_DERIVED
10096 46814 : && ts->u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
10097 171 : && ts->u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
10098 59 : return build_constructor (type, NULL);
10099 :
10100 147467 : if (!(expr || pointer || procptr))
10101 : return NULL_TREE;
10102 :
10103 : /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
10104 : (these are the only two iso_c_binding derived types that can be
10105 : used as initialization expressions). If so, we need to modify
10106 : the 'expr' to be that for a (void *). */
10107 138786 : if (expr != NULL && expr->ts.type == BT_DERIVED
10108 42343 : && expr->ts.is_iso_c && expr->ts.u.derived)
10109 : {
10110 186 : if (TREE_CODE (type) == ARRAY_TYPE)
10111 4 : return build_constructor (type, NULL);
10112 182 : else if (POINTER_TYPE_P (type))
10113 182 : return build_int_cst (type, 0);
10114 : else
10115 0 : gcc_unreachable ();
10116 : }
10117 :
10118 138600 : if (array && !procptr)
10119 : {
10120 9390 : tree ctor;
10121 : /* Arrays need special handling. */
10122 9390 : if (pointer)
10123 845 : ctor = gfc_build_null_descriptor (type);
10124 : /* Special case assigning an array to zero. */
10125 8545 : else if (is_zero_initializer_p (expr))
10126 226 : ctor = build_constructor (type, NULL);
10127 : else
10128 8319 : ctor = gfc_conv_array_initializer (type, expr);
10129 9390 : TREE_STATIC (ctor) = 1;
10130 9390 : return ctor;
10131 : }
10132 129210 : else if (pointer || procptr)
10133 : {
10134 60715 : if (ts->type == BT_CLASS && !procptr)
10135 : {
10136 1879 : gfc_init_se (&se, NULL);
10137 1879 : gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
10138 1879 : gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
10139 1879 : TREE_STATIC (se.expr) = 1;
10140 1879 : return se.expr;
10141 : }
10142 58836 : else if (!expr || expr->expr_type == EXPR_NULL)
10143 31208 : return fold_convert (type, null_pointer_node);
10144 : else
10145 : {
10146 27628 : gfc_init_se (&se, NULL);
10147 27628 : se.want_pointer = 1;
10148 27628 : gfc_conv_expr (&se, expr);
10149 27628 : gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
10150 : return se.expr;
10151 : }
10152 : }
10153 : else
10154 : {
10155 68495 : switch (ts->type)
10156 : {
10157 20650 : case_bt_struct:
10158 20650 : case BT_CLASS:
10159 20650 : gfc_init_se (&se, NULL);
10160 20650 : if (ts->type == BT_CLASS && expr->expr_type == EXPR_NULL)
10161 901 : gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
10162 : else
10163 19749 : gfc_conv_structure (&se, expr, 1);
10164 20650 : gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
10165 20650 : TREE_STATIC (se.expr) = 1;
10166 20650 : return se.expr;
10167 :
10168 2909 : case BT_CHARACTER:
10169 2909 : if (expr->expr_type == EXPR_CONSTANT)
10170 : {
10171 2908 : tree ctor = gfc_conv_string_init (ts->u.cl->backend_decl, expr);
10172 2908 : TREE_STATIC (ctor) = 1;
10173 2908 : return ctor;
10174 : }
10175 :
10176 : /* Fallthrough. */
10177 44937 : default:
10178 44937 : gfc_init_se (&se, NULL);
10179 44937 : gfc_conv_constant (&se, expr);
10180 44937 : gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
10181 : return se.expr;
10182 : }
10183 : }
10184 : }
10185 :
10186 : static tree
10187 1113 : gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
10188 : {
10189 1113 : gfc_se rse;
10190 1113 : gfc_se lse;
10191 1113 : gfc_ss *rss;
10192 1113 : gfc_ss *lss;
10193 1113 : gfc_array_info *lss_array;
10194 1113 : stmtblock_t body;
10195 1113 : stmtblock_t block;
10196 1113 : gfc_loopinfo loop;
10197 1113 : int n;
10198 1113 : tree tmp;
10199 :
10200 1113 : gfc_start_block (&block);
10201 :
10202 : /* Initialize the scalarizer. */
10203 1113 : gfc_init_loopinfo (&loop);
10204 :
10205 1113 : gfc_init_se (&lse, NULL);
10206 1113 : gfc_init_se (&rse, NULL);
10207 :
10208 : /* Walk the rhs. */
10209 1113 : rss = gfc_walk_expr (expr);
10210 1113 : if (rss == gfc_ss_terminator)
10211 : /* The rhs is scalar. Add a ss for the expression. */
10212 323 : rss = gfc_get_scalar_ss (gfc_ss_terminator, expr);
10213 :
10214 : /* Create a SS for the destination. */
10215 1113 : lss = gfc_get_array_ss (gfc_ss_terminator, NULL, cm->as->rank,
10216 : GFC_SS_COMPONENT);
10217 1113 : lss_array = &lss->info->data.array;
10218 1113 : lss_array->shape = gfc_get_shape (cm->as->rank);
10219 1113 : lss_array->descriptor = dest;
10220 1113 : lss_array->data = gfc_conv_array_data (dest);
10221 1113 : lss_array->offset = gfc_conv_array_offset (dest);
10222 2301 : for (n = 0; n < cm->as->rank; n++)
10223 : {
10224 1188 : lss_array->start[n] = gfc_conv_array_lbound (dest, n);
10225 1188 : lss_array->stride[n] = gfc_index_one_node;
10226 :
10227 1188 : mpz_init (lss_array->shape[n]);
10228 1188 : mpz_sub (lss_array->shape[n], cm->as->upper[n]->value.integer,
10229 1188 : cm->as->lower[n]->value.integer);
10230 1188 : mpz_add_ui (lss_array->shape[n], lss_array->shape[n], 1);
10231 : }
10232 :
10233 : /* Associate the SS with the loop. */
10234 1113 : gfc_add_ss_to_loop (&loop, lss);
10235 1113 : gfc_add_ss_to_loop (&loop, rss);
10236 :
10237 : /* Calculate the bounds of the scalarization. */
10238 1113 : gfc_conv_ss_startstride (&loop);
10239 :
10240 : /* Setup the scalarizing loops. */
10241 1113 : gfc_conv_loop_setup (&loop, &expr->where);
10242 :
10243 : /* Setup the gfc_se structures. */
10244 1113 : gfc_copy_loopinfo_to_se (&lse, &loop);
10245 1113 : gfc_copy_loopinfo_to_se (&rse, &loop);
10246 :
10247 1113 : rse.ss = rss;
10248 1113 : gfc_mark_ss_chain_used (rss, 1);
10249 1113 : lse.ss = lss;
10250 1113 : gfc_mark_ss_chain_used (lss, 1);
10251 :
10252 : /* Start the scalarized loop body. */
10253 1113 : gfc_start_scalarized_body (&loop, &body);
10254 :
10255 1113 : gfc_conv_tmp_array_ref (&lse);
10256 1113 : if (cm->ts.type == BT_CHARACTER)
10257 207 : lse.string_length = cm->ts.u.cl->backend_decl;
10258 :
10259 1113 : gfc_conv_expr (&rse, expr);
10260 :
10261 1113 : tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false);
10262 1113 : gfc_add_expr_to_block (&body, tmp);
10263 :
10264 1113 : gcc_assert (rse.ss == gfc_ss_terminator);
10265 :
10266 : /* Generate the copying loops. */
10267 1113 : gfc_trans_scalarizing_loops (&loop, &body);
10268 :
10269 : /* Wrap the whole thing up. */
10270 1113 : gfc_add_block_to_block (&block, &loop.pre);
10271 1113 : gfc_add_block_to_block (&block, &loop.post);
10272 :
10273 1113 : gcc_assert (lss_array->shape != NULL);
10274 1113 : gfc_free_shape (&lss_array->shape, cm->as->rank);
10275 1113 : gfc_cleanup_loop (&loop);
10276 :
10277 1113 : return gfc_finish_block (&block);
10278 : }
10279 :
10280 :
10281 : static stmtblock_t *final_block;
10282 :
10283 :
10284 : /* Get the address of element index of contiguous character array data whose elements
10285 : are len characters of ksize bytes each. */
10286 :
10287 : static tree
10288 196 : gfc_char_elem_addr (tree char_ptr, tree data, tree idx, tree len, tree ksize)
10289 : {
10290 196 : tree offset = fold_build2_loc (input_location, MULT_EXPR,
10291 : gfc_array_index_type, len, ksize);
10292 196 : offset = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
10293 : idx, offset);
10294 196 : return fold_build_pointer_plus_loc (input_location,
10295 196 : fold_convert (char_ptr, data), offset);
10296 : }
10297 :
10298 :
10299 : /* Copy a deferred-shape allocatable character array component in a structure
10300 : constructor when the source element length (SRC_LEN) may differ from the
10301 : component's declared length. Like gfc_duplicate_allocatable, a
10302 : contiguous source layout is assumed. DEST and SRC are array descriptors;
10303 : DEST already carries the source's bounds. */
10304 :
10305 : static tree
10306 98 : gfc_trans_alloc_char_subarray_assign (tree dest, gfc_component *cm, tree src,
10307 : tree src_len, int rank)
10308 : {
10309 98 : stmtblock_t block, body;
10310 98 : tree dlen, slen, ksize, nelems, idx, size, tmp, pchar, cond;
10311 :
10312 98 : gfc_init_block (&block);
10313 :
10314 98 : pchar = gfc_get_pchar_type (cm->ts.kind);
10315 98 : ksize = fold_convert (gfc_array_index_type,
10316 : TYPE_SIZE_UNIT (gfc_get_char_type (cm->ts.kind)));
10317 98 : dlen = fold_convert (gfc_array_index_type, cm->ts.u.cl->backend_decl);
10318 98 : slen = fold_convert (gfc_array_index_type, src_len);
10319 98 : nelems = gfc_full_array_size (&block, src, rank);
10320 98 : nelems = gfc_evaluate_now (nelems, &block);
10321 :
10322 : /* Allocate the destination data: nelems elements of the component length. */
10323 98 : size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
10324 : nelems, dlen);
10325 98 : size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
10326 : size, ksize);
10327 98 : tmp = GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (dest));
10328 98 : gfc_conv_descriptor_data_set (&block, dest,
10329 : gfc_call_malloc (&block, tmp, size));
10330 :
10331 : /* Copy element IDX, padding or truncating to the component length. */
10332 98 : idx = gfc_create_var (gfc_array_index_type, "idx");
10333 98 : gfc_init_block (&body);
10334 98 : gfc_trans_string_copy (&body, cm->ts.u.cl->backend_decl,
10335 : gfc_char_elem_addr (pchar,
10336 : gfc_conv_descriptor_data_get (dest),
10337 : idx, dlen, ksize),
10338 : cm->ts.kind, src_len,
10339 : gfc_char_elem_addr (pchar,
10340 : gfc_conv_descriptor_data_get (src),
10341 : idx, slen, ksize),
10342 : cm->ts.kind);
10343 98 : gfc_simple_for_loop (&block, idx, gfc_index_zero_node, nelems, LT_EXPR,
10344 : gfc_index_one_node, gfc_finish_block (&body));
10345 :
10346 98 : tmp = gfc_finish_block (&block);
10347 :
10348 : /* Null the destination if the source is unallocated. */
10349 98 : gfc_init_block (&body);
10350 98 : gfc_conv_descriptor_data_set (&body, dest, null_pointer_node);
10351 98 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10352 : fold_convert (pvoid_type_node,
10353 : gfc_conv_descriptor_data_get (src)),
10354 : null_pointer_node);
10355 98 : return build3_v (COND_EXPR, cond, tmp, gfc_finish_block (&body));
10356 : }
10357 :
10358 :
10359 : static tree
10360 1480 : gfc_trans_alloc_subarray_assign (tree dest, gfc_component * cm,
10361 : gfc_expr * expr)
10362 : {
10363 1480 : gfc_se se;
10364 1480 : stmtblock_t block;
10365 1480 : tree offset;
10366 1480 : int n;
10367 1480 : tree tmp;
10368 1480 : tree tmp2;
10369 1480 : gfc_array_spec *as;
10370 1480 : gfc_expr *arg = NULL;
10371 :
10372 1480 : gfc_start_block (&block);
10373 1480 : gfc_init_se (&se, NULL);
10374 :
10375 : /* Get the descriptor for the expressions. */
10376 1480 : se.want_pointer = 0;
10377 1480 : gfc_conv_expr_descriptor (&se, expr);
10378 1480 : gfc_add_block_to_block (&block, &se.pre);
10379 1480 : gfc_add_modify (&block, dest, se.expr);
10380 1480 : if (cm->ts.type == BT_CHARACTER
10381 1480 : && gfc_deferred_strlen (cm, &tmp))
10382 : {
10383 30 : tmp = fold_build3_loc (input_location, COMPONENT_REF,
10384 30 : TREE_TYPE (tmp),
10385 30 : TREE_OPERAND (dest, 0),
10386 : tmp, NULL_TREE);
10387 30 : gfc_add_modify (&block, tmp,
10388 30 : fold_convert (TREE_TYPE (tmp),
10389 : se.string_length));
10390 30 : cm->ts.u.cl->backend_decl = gfc_create_var (gfc_charlen_type_node,
10391 : "slen");
10392 30 : gfc_add_modify (&block, cm->ts.u.cl->backend_decl, se.string_length);
10393 : }
10394 :
10395 : /* Deal with arrays of derived types with allocatable components. */
10396 1480 : if (gfc_bt_struct (cm->ts.type)
10397 199 : && cm->ts.u.derived->attr.alloc_comp)
10398 : // TODO: Fix caf_mode
10399 113 : tmp = gfc_copy_alloc_comp (cm->ts.u.derived,
10400 : se.expr, dest,
10401 113 : cm->as->rank, 0);
10402 1367 : else if (cm->ts.type == BT_CLASS && expr->ts.type == BT_DERIVED
10403 36 : && CLASS_DATA(cm)->attr.allocatable)
10404 : {
10405 36 : if (cm->ts.u.derived->attr.alloc_comp)
10406 : // TODO: Fix caf_mode
10407 0 : tmp = gfc_copy_alloc_comp (expr->ts.u.derived,
10408 : se.expr, dest,
10409 : expr->rank, 0);
10410 : else
10411 : {
10412 36 : tmp = TREE_TYPE (dest);
10413 36 : tmp = gfc_duplicate_allocatable (dest, se.expr,
10414 : tmp, expr->rank, NULL_TREE);
10415 : }
10416 : }
10417 1331 : else if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
10418 30 : tmp = gfc_duplicate_allocatable (dest, se.expr,
10419 : gfc_typenode_for_spec (&cm->ts),
10420 30 : cm->as->rank, NULL_TREE);
10421 1301 : else if (cm->ts.type == BT_CHARACTER)
10422 : /* Explicit-length character: the source element length may differ from
10423 : the component length, so a bitwise duplicate would copy the wrong
10424 : bytes. Copy element by element with padding/truncation. */
10425 98 : tmp = gfc_trans_alloc_char_subarray_assign (dest, cm, se.expr,
10426 : se.string_length,
10427 98 : cm->as->rank);
10428 : else
10429 1203 : tmp = gfc_duplicate_allocatable (dest, se.expr,
10430 1203 : TREE_TYPE(cm->backend_decl),
10431 1203 : cm->as->rank, NULL_TREE);
10432 :
10433 :
10434 1480 : gfc_add_expr_to_block (&block, tmp);
10435 1480 : gfc_add_block_to_block (&block, &se.post);
10436 :
10437 1480 : if (final_block && !cm->attr.allocatable
10438 108 : && expr->expr_type == EXPR_ARRAY)
10439 : {
10440 108 : tree data_ptr;
10441 108 : data_ptr = gfc_conv_descriptor_data_get (dest);
10442 108 : gfc_add_expr_to_block (final_block, gfc_call_free (data_ptr));
10443 108 : }
10444 1372 : else if (final_block && cm->attr.allocatable)
10445 162 : gfc_add_block_to_block (final_block, &se.finalblock);
10446 :
10447 1480 : if (expr->expr_type != EXPR_VARIABLE)
10448 : {
10449 1341 : if (gfc_bt_struct (cm->ts.type) && cm->ts.u.derived->attr.alloc_comp)
10450 : {
10451 214 : tmp = gfc_deallocate_alloc_comp_no_caf (cm->ts.u.derived,
10452 107 : se.expr, cm->as->rank, true);
10453 107 : gfc_add_expr_to_block (&block, tmp);
10454 : }
10455 1341 : gfc_conv_descriptor_data_set (&block, se.expr, null_pointer_node);
10456 : }
10457 :
10458 : /* We need to know if the argument of a conversion function is a
10459 : variable, so that the correct lower bound can be used. */
10460 1480 : if (expr->expr_type == EXPR_FUNCTION
10461 68 : && expr->value.function.isym
10462 56 : && expr->value.function.isym->conversion
10463 56 : && expr->value.function.actual->expr
10464 56 : && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE)
10465 56 : arg = expr->value.function.actual->expr;
10466 :
10467 : /* Obtain the array spec of full array references. */
10468 56 : if (arg)
10469 56 : as = gfc_get_full_arrayspec_from_expr (arg);
10470 : else
10471 1424 : as = gfc_get_full_arrayspec_from_expr (expr);
10472 :
10473 : /* Shift the lbound and ubound of temporaries to being unity,
10474 : rather than zero, based. Always calculate the offset. */
10475 1480 : gfc_conv_descriptor_offset_set (&block, dest, gfc_index_zero_node);
10476 1480 : offset = gfc_conv_descriptor_offset_get (dest);
10477 1480 : tmp2 =gfc_create_var (gfc_array_index_type, NULL);
10478 :
10479 4496 : for (n = 0; n < expr->rank; n++)
10480 : {
10481 1536 : tree span;
10482 1536 : tree lbound;
10483 :
10484 : /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
10485 : TODO It looks as if gfc_conv_expr_descriptor should return
10486 : the correct bounds and that the following should not be
10487 : necessary. This would simplify gfc_conv_intrinsic_bound
10488 : as well. */
10489 1536 : if (as && as->lower[n])
10490 : {
10491 92 : gfc_se lbse;
10492 92 : gfc_init_se (&lbse, NULL);
10493 92 : gfc_conv_expr (&lbse, as->lower[n]);
10494 92 : gfc_add_block_to_block (&block, &lbse.pre);
10495 92 : lbound = gfc_evaluate_now (lbse.expr, &block);
10496 92 : }
10497 1444 : else if (as && arg)
10498 : {
10499 34 : tmp = gfc_get_symbol_decl (arg->symtree->n.sym);
10500 34 : lbound = gfc_conv_descriptor_lbound_get (tmp,
10501 : gfc_rank_cst[n]);
10502 : }
10503 1410 : else if (as)
10504 82 : lbound = gfc_conv_descriptor_lbound_get (dest,
10505 : gfc_rank_cst[n]);
10506 : else
10507 1328 : lbound = gfc_index_one_node;
10508 :
10509 1536 : lbound = fold_convert (gfc_array_index_type, lbound);
10510 :
10511 : /* Shift the bounds and set the offset accordingly. */
10512 1536 : tmp = gfc_conv_descriptor_ubound_get (dest, gfc_rank_cst[n]);
10513 1536 : span = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
10514 : tmp, gfc_conv_descriptor_lbound_get (dest, gfc_rank_cst[n]));
10515 1536 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
10516 : span, lbound);
10517 1536 : gfc_conv_descriptor_ubound_set (&block, dest,
10518 : gfc_rank_cst[n], tmp);
10519 1536 : gfc_conv_descriptor_lbound_set (&block, dest,
10520 : gfc_rank_cst[n], lbound);
10521 :
10522 1536 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
10523 : gfc_conv_descriptor_lbound_get (dest,
10524 : gfc_rank_cst[n]),
10525 : gfc_conv_descriptor_stride_get (dest,
10526 : gfc_rank_cst[n]));
10527 1536 : gfc_add_modify (&block, tmp2, tmp);
10528 1536 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
10529 : offset, tmp2);
10530 1536 : gfc_conv_descriptor_offset_set (&block, dest, tmp);
10531 : }
10532 :
10533 1480 : if (arg)
10534 : {
10535 : /* If a conversion expression has a null data pointer
10536 : argument, nullify the allocatable component. */
10537 56 : tree non_null_expr;
10538 56 : tree null_expr;
10539 :
10540 56 : if (arg->symtree->n.sym->attr.allocatable
10541 24 : || arg->symtree->n.sym->attr.pointer)
10542 : {
10543 32 : non_null_expr = gfc_finish_block (&block);
10544 32 : gfc_start_block (&block);
10545 32 : gfc_conv_descriptor_data_set (&block, dest,
10546 : null_pointer_node);
10547 32 : null_expr = gfc_finish_block (&block);
10548 32 : tmp = gfc_conv_descriptor_data_get (arg->symtree->n.sym->backend_decl);
10549 32 : tmp = build2_loc (input_location, EQ_EXPR, logical_type_node, tmp,
10550 32 : fold_convert (TREE_TYPE (tmp), null_pointer_node));
10551 32 : return build3_v (COND_EXPR, tmp,
10552 : null_expr, non_null_expr);
10553 : }
10554 : }
10555 :
10556 1448 : return gfc_finish_block (&block);
10557 : }
10558 :
10559 :
10560 : /* Allocate or reallocate scalar component, as necessary. */
10561 :
10562 : static void
10563 464 : alloc_scalar_allocatable_subcomponent (stmtblock_t *block, tree comp,
10564 : gfc_component *cm, gfc_expr *expr2,
10565 : tree slen)
10566 : {
10567 464 : tree tmp;
10568 464 : tree ptr;
10569 464 : tree size;
10570 464 : tree size_in_bytes;
10571 464 : tree lhs_cl_size = NULL_TREE;
10572 464 : gfc_se se;
10573 :
10574 464 : if (!comp)
10575 0 : return;
10576 :
10577 464 : if (!expr2 || expr2->rank)
10578 : return;
10579 :
10580 464 : realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
10581 :
10582 464 : if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
10583 : {
10584 145 : gcc_assert (expr2->ts.type == BT_CHARACTER);
10585 145 : size = expr2->ts.u.cl->backend_decl;
10586 145 : if (!size || !VAR_P (size))
10587 145 : size = gfc_create_var (TREE_TYPE (slen), "slen");
10588 145 : gfc_add_modify (block, size, slen);
10589 :
10590 145 : gfc_deferred_strlen (cm, &tmp);
10591 145 : lhs_cl_size = fold_build3_loc (input_location, COMPONENT_REF,
10592 : gfc_charlen_type_node,
10593 145 : TREE_OPERAND (comp, 0),
10594 : tmp, NULL_TREE);
10595 :
10596 145 : tmp = TREE_TYPE (gfc_typenode_for_spec (&cm->ts));
10597 145 : tmp = TYPE_SIZE_UNIT (tmp);
10598 290 : size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
10599 145 : TREE_TYPE (tmp), tmp,
10600 145 : fold_convert (TREE_TYPE (tmp), size));
10601 : }
10602 319 : else if (cm->ts.type == BT_CLASS)
10603 : {
10604 121 : if (expr2->ts.type != BT_CLASS)
10605 : {
10606 121 : if (expr2->ts.type == BT_CHARACTER)
10607 : {
10608 24 : gfc_init_se (&se, NULL);
10609 24 : gfc_conv_expr (&se, expr2);
10610 24 : size = build_int_cst (gfc_charlen_type_node, expr2->ts.kind);
10611 24 : size = fold_build2_loc (input_location, MULT_EXPR,
10612 : gfc_charlen_type_node,
10613 : se.string_length, size);
10614 24 : size = fold_convert (size_type_node, size);
10615 : }
10616 : else
10617 : {
10618 97 : tmp = gfc_typenode_for_spec (&expr2->ts);
10619 97 : size = TYPE_SIZE_UNIT (tmp);
10620 : }
10621 : }
10622 : else
10623 : {
10624 0 : gfc_expr *e2vtab;
10625 0 : e2vtab = gfc_find_and_cut_at_last_class_ref (expr2);
10626 0 : gfc_add_vptr_component (e2vtab);
10627 0 : gfc_add_size_component (e2vtab);
10628 0 : gfc_init_se (&se, NULL);
10629 0 : gfc_conv_expr (&se, e2vtab);
10630 0 : gfc_add_block_to_block (block, &se.pre);
10631 0 : size = fold_convert (size_type_node, se.expr);
10632 0 : gfc_free_expr (e2vtab);
10633 : }
10634 : size_in_bytes = size;
10635 : }
10636 : else
10637 : {
10638 : /* Otherwise use the length in bytes of the rhs. */
10639 198 : size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&cm->ts));
10640 198 : size_in_bytes = size;
10641 : }
10642 :
10643 464 : size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
10644 : size_in_bytes, size_one_node);
10645 :
10646 464 : if (cm->ts.type == BT_DERIVED && cm->ts.u.derived->attr.alloc_comp)
10647 : {
10648 6 : tmp = build_call_expr_loc (input_location,
10649 : builtin_decl_explicit (BUILT_IN_CALLOC),
10650 : 2, build_one_cst (size_type_node),
10651 : size_in_bytes);
10652 6 : tmp = fold_convert (TREE_TYPE (comp), tmp);
10653 6 : gfc_add_modify (block, comp, tmp);
10654 : }
10655 : else
10656 : {
10657 458 : tmp = build_call_expr_loc (input_location,
10658 : builtin_decl_explicit (BUILT_IN_MALLOC),
10659 : 1, size_in_bytes);
10660 458 : if (GFC_CLASS_TYPE_P (TREE_TYPE (comp)))
10661 121 : ptr = gfc_class_data_get (comp);
10662 : else
10663 : ptr = comp;
10664 458 : tmp = fold_convert (TREE_TYPE (ptr), tmp);
10665 458 : gfc_add_modify (block, ptr, tmp);
10666 : }
10667 :
10668 464 : if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
10669 : /* Update the lhs character length. */
10670 145 : gfc_add_modify (block, lhs_cl_size,
10671 145 : fold_convert (TREE_TYPE (lhs_cl_size), size));
10672 : }
10673 :
10674 :
10675 : /* Assign a single component of a derived type constructor. */
10676 :
10677 : static tree
10678 33438 : gfc_trans_subcomponent_assign (tree dest, gfc_component * cm,
10679 : gfc_expr * expr, bool init)
10680 : {
10681 33438 : gfc_se se;
10682 33438 : gfc_se lse;
10683 33438 : stmtblock_t block;
10684 33438 : tree tmp;
10685 33438 : tree vtab;
10686 :
10687 33438 : gfc_start_block (&block);
10688 :
10689 33438 : if (cm->attr.pointer || cm->attr.proc_pointer)
10690 : {
10691 : /* Only care about pointers here, not about allocatables. */
10692 2825 : gfc_init_se (&se, NULL);
10693 : /* Pointer component. */
10694 2825 : if ((cm->attr.dimension || cm->attr.codimension)
10695 688 : && !cm->attr.proc_pointer)
10696 : {
10697 : /* Array pointer. */
10698 672 : if (expr->expr_type == EXPR_NULL)
10699 666 : gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
10700 : else
10701 : {
10702 6 : se.direct_byref = 1;
10703 6 : se.expr = dest;
10704 6 : gfc_conv_expr_descriptor (&se, expr);
10705 6 : gfc_add_block_to_block (&block, &se.pre);
10706 6 : gfc_add_block_to_block (&block, &se.post);
10707 : }
10708 : }
10709 : else
10710 : {
10711 : /* Scalar pointers. */
10712 2153 : se.want_pointer = 1;
10713 2153 : gfc_conv_expr (&se, expr);
10714 2153 : gfc_add_block_to_block (&block, &se.pre);
10715 :
10716 2153 : if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
10717 12 : && expr->symtree->n.sym->attr.dummy)
10718 12 : se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
10719 :
10720 2153 : gfc_add_modify (&block, dest,
10721 2153 : fold_convert (TREE_TYPE (dest), se.expr));
10722 2153 : gfc_add_block_to_block (&block, &se.post);
10723 : }
10724 : }
10725 30613 : else if (cm->ts.type == BT_CLASS && expr->expr_type == EXPR_NULL)
10726 : {
10727 : /* NULL initialization for CLASS components. */
10728 1055 : tmp = gfc_trans_structure_assign (dest,
10729 : gfc_class_initializer (&cm->ts, expr),
10730 : false);
10731 1055 : gfc_add_expr_to_block (&block, tmp);
10732 : }
10733 29558 : else if (cm->ts.type == BT_CLASS && CLASS_DATA (cm)->attr.class_pointer
10734 30 : && expr->ts.type != BT_CLASS)
10735 : {
10736 : /* Associate a CLASS pointer component with a non-class target. */
10737 30 : gfc_init_se (&se, NULL);
10738 30 : if (CLASS_DATA (cm)->attr.dimension)
10739 : {
10740 12 : se.direct_byref = 1;
10741 12 : se.expr = gfc_class_data_get (dest);
10742 12 : gfc_conv_expr_descriptor (&se, expr);
10743 12 : gfc_add_block_to_block (&block, &se.pre);
10744 : }
10745 : else
10746 : {
10747 18 : gfc_conv_expr_reference (&se, expr);
10748 18 : gfc_add_block_to_block (&block, &se.pre);
10749 18 : tmp = gfc_class_data_get (dest);
10750 18 : gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), se.expr));
10751 : }
10752 30 : vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
10753 30 : vtab = gfc_build_addr_expr (NULL_TREE, vtab);
10754 30 : tmp = gfc_class_vptr_get (dest);
10755 30 : gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), vtab));
10756 30 : if (UNLIMITED_POLY (cm))
10757 : {
10758 18 : tmp = gfc_class_len_get (dest);
10759 18 : gfc_add_modify (&block, tmp,
10760 18 : expr->ts.type == BT_CHARACTER
10761 12 : ? fold_convert (TREE_TYPE (tmp), se.string_length)
10762 6 : : build_zero_cst (TREE_TYPE (tmp)));
10763 : }
10764 30 : gfc_add_block_to_block (&block, &se.post);
10765 : }
10766 29528 : else if ((cm->attr.dimension || cm->attr.codimension)
10767 : && !cm->attr.proc_pointer)
10768 : {
10769 5572 : if (cm->attr.allocatable && expr->expr_type == EXPR_NULL)
10770 : {
10771 3015 : gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
10772 3015 : if (cm->attr.codimension && flag_coarray == GFC_FCOARRAY_LIB)
10773 2 : gfc_conv_descriptor_token_set (&block, dest, null_pointer_node);
10774 : }
10775 2557 : else if (cm->attr.allocatable || cm->attr.pdt_array)
10776 : {
10777 1444 : tmp = gfc_trans_alloc_subarray_assign (dest, cm, expr);
10778 1444 : gfc_add_expr_to_block (&block, tmp);
10779 : }
10780 : else
10781 : {
10782 1113 : tmp = gfc_trans_subarray_assign (dest, cm, expr);
10783 1113 : gfc_add_expr_to_block (&block, tmp);
10784 : }
10785 : }
10786 23956 : else if (cm->ts.type == BT_CLASS
10787 169 : && CLASS_DATA (cm)->attr.dimension
10788 36 : && CLASS_DATA (cm)->attr.allocatable
10789 36 : && expr->ts.type == BT_DERIVED)
10790 : {
10791 36 : vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
10792 36 : vtab = gfc_build_addr_expr (NULL_TREE, vtab);
10793 36 : tmp = gfc_class_vptr_get (dest);
10794 36 : gfc_add_modify (&block, tmp,
10795 36 : fold_convert (TREE_TYPE (tmp), vtab));
10796 36 : tmp = gfc_class_data_get (dest);
10797 36 : tmp = gfc_trans_alloc_subarray_assign (tmp, cm, expr);
10798 36 : gfc_add_expr_to_block (&block, tmp);
10799 : }
10800 23920 : else if (cm->attr.allocatable && expr->expr_type == EXPR_NULL
10801 1878 : && (init
10802 1751 : || (cm->ts.type == BT_CHARACTER
10803 137 : && !(cm->ts.deferred || cm->attr.pdt_string))))
10804 : {
10805 : /* NULL initialization for allocatable components.
10806 : Deferred-length character is dealt with later. */
10807 151 : gfc_add_modify (&block, dest, fold_convert (TREE_TYPE (dest),
10808 : null_pointer_node));
10809 : }
10810 23769 : else if (init && (cm->attr.allocatable
10811 14765 : || (cm->ts.type == BT_CLASS && CLASS_DATA (cm)->attr.allocatable
10812 133 : && expr->ts.type != BT_CLASS)))
10813 : {
10814 464 : tree size;
10815 464 : tree tmp2;
10816 :
10817 464 : gfc_init_se (&se, NULL);
10818 464 : gfc_conv_expr (&se, expr);
10819 :
10820 : /* The remainder of these instructions follow the if (cm->attr.pointer)
10821 : if (!cm->attr.dimension) part above. */
10822 464 : gfc_add_block_to_block (&block, &se.pre);
10823 : /* Take care about non-array allocatable components here. The alloc_*
10824 : routine below is motivated by the alloc_scalar_allocatable_for_
10825 : assignment() routine, but with the realloc portions removed and
10826 : different input. */
10827 464 : alloc_scalar_allocatable_subcomponent (&block, dest, cm, expr,
10828 : se.string_length);
10829 :
10830 464 : if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
10831 0 : && expr->symtree->n.sym->attr.dummy)
10832 0 : se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
10833 :
10834 464 : if (cm->ts.type == BT_CLASS)
10835 : {
10836 121 : tmp = gfc_class_data_get (dest);
10837 121 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
10838 121 : vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
10839 121 : vtab = gfc_build_addr_expr (NULL_TREE, vtab);
10840 121 : gfc_add_modify (&block, gfc_class_vptr_get (dest),
10841 121 : fold_convert (TREE_TYPE (gfc_class_vptr_get (dest)), vtab));
10842 : }
10843 : else
10844 343 : tmp = build_fold_indirect_ref_loc (input_location, dest);
10845 :
10846 : /* For deferred strings insert a memcpy. */
10847 464 : if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
10848 : {
10849 145 : gcc_assert (se.string_length || expr->ts.u.cl->backend_decl);
10850 145 : size = size_of_string_in_bytes (cm->ts.kind, se.string_length
10851 : ? se.string_length
10852 0 : : expr->ts.u.cl->backend_decl);
10853 145 : tmp = gfc_build_memcpy_call (tmp, se.expr, size);
10854 145 : gfc_add_expr_to_block (&block, tmp);
10855 : }
10856 319 : else if (cm->ts.type == BT_CLASS)
10857 : {
10858 : /* Fix the expression for memcpy. */
10859 121 : if (expr->expr_type != EXPR_VARIABLE)
10860 85 : se.expr = gfc_evaluate_now (se.expr, &block);
10861 :
10862 121 : if (expr->ts.type == BT_CHARACTER)
10863 : {
10864 24 : size = build_int_cst (gfc_charlen_type_node, expr->ts.kind);
10865 24 : size = fold_build2_loc (input_location, MULT_EXPR,
10866 : gfc_charlen_type_node,
10867 : se.string_length, size);
10868 24 : size = fold_convert (size_type_node, size);
10869 : }
10870 : else
10871 97 : size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr->ts));
10872 :
10873 : /* Now copy the expression to the constructor component _data. */
10874 121 : gfc_add_expr_to_block (&block,
10875 : gfc_build_memcpy_call (tmp, se.expr, size));
10876 :
10877 121 : if (expr->ts.type == BT_DERIVED
10878 66 : && expr->ts.u.derived->attr.alloc_comp
10879 6 : && expr->expr_type != EXPR_NULL)
10880 : {
10881 6 : tmp2 = gfc_class_data_get (dest);
10882 6 : tmp2 = gfc_copy_alloc_comp (expr->ts.u.derived, tmp2,
10883 : gfc_class_data_get (dest),
10884 : expr->rank, 0);
10885 6 : gfc_add_expr_to_block (&block, tmp2);
10886 : }
10887 :
10888 : /* Fill the unlimited polymorphic _len field. */
10889 121 : if (UNLIMITED_POLY (cm) && expr->ts.type == BT_CHARACTER)
10890 : {
10891 24 : tmp = gfc_class_len_get (gfc_get_class_from_expr (tmp));
10892 24 : gfc_add_modify (&block, tmp,
10893 24 : fold_convert (TREE_TYPE (tmp),
10894 : se.string_length));
10895 : }
10896 : }
10897 198 : else if (cm->ts.type == BT_CHARACTER)
10898 : {
10899 : /* Copy into the explicit-length allocatable character component. */
10900 24 : gfc_conv_string_parameter (&se);
10901 24 : gfc_trans_string_copy (&block, cm->ts.u.cl->backend_decl, dest,
10902 : cm->ts.kind, se.string_length, se.expr,
10903 : expr->ts.kind);
10904 : }
10905 : else
10906 : {
10907 174 : gfc_add_modify (&block, tmp,
10908 174 : fold_convert (TREE_TYPE (tmp), se.expr));
10909 174 : if (expr->ts.type == BT_DERIVED
10910 32 : && expr->ts.u.derived->attr.alloc_comp
10911 6 : && expr->expr_type != EXPR_NULL)
10912 : {
10913 6 : tmp2 = build_fold_indirect_ref_loc (input_location, dest);
10914 6 : tmp2 = gfc_copy_alloc_comp (cm->ts.u.derived, tmp2,
10915 : se.expr, expr->rank, 0);
10916 6 : gfc_add_expr_to_block (&block, tmp2);
10917 : }
10918 : }
10919 :
10920 464 : gfc_add_block_to_block (&block, &se.post);
10921 464 : }
10922 23305 : else if (expr->ts.type == BT_UNION)
10923 : {
10924 13 : tree tmp;
10925 13 : gfc_constructor *c = gfc_constructor_first (expr->value.constructor);
10926 : /* We mark that the entire union should be initialized with a contrived
10927 : EXPR_NULL expression at the beginning. */
10928 13 : if (c != NULL && c->n.component == NULL
10929 7 : && c->expr != NULL && c->expr->expr_type == EXPR_NULL)
10930 : {
10931 6 : tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
10932 6 : dest, build_constructor (TREE_TYPE (dest), NULL));
10933 6 : gfc_add_expr_to_block (&block, tmp);
10934 6 : c = gfc_constructor_next (c);
10935 : }
10936 : /* The following constructor expression, if any, represents a specific
10937 : map initializer, as given by the user. */
10938 13 : if (c != NULL && c->expr != NULL)
10939 : {
10940 6 : gcc_assert (expr->expr_type == EXPR_STRUCTURE);
10941 6 : tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
10942 6 : gfc_add_expr_to_block (&block, tmp);
10943 : }
10944 : }
10945 23292 : else if (expr->ts.type == BT_DERIVED && expr->ts.f90_type != BT_VOID)
10946 : {
10947 3821 : if (expr->expr_type != EXPR_STRUCTURE)
10948 : {
10949 494 : tree dealloc = NULL_TREE;
10950 494 : gfc_init_se (&se, NULL);
10951 494 : gfc_conv_expr (&se, expr);
10952 494 : gfc_add_block_to_block (&block, &se.pre);
10953 : /* Prevent repeat evaluations in gfc_copy_alloc_comp by fixing the
10954 : expression in a temporary variable and deallocate the allocatable
10955 : components. Then we can the copy the expression to the result. */
10956 494 : if (cm->ts.u.derived->attr.alloc_comp
10957 372 : && expr->expr_type != EXPR_VARIABLE)
10958 : {
10959 336 : se.expr = gfc_evaluate_now (se.expr, &block);
10960 336 : dealloc = gfc_deallocate_alloc_comp (cm->ts.u.derived, se.expr,
10961 : expr->rank);
10962 : }
10963 494 : gfc_add_modify (&block, dest,
10964 494 : fold_convert (TREE_TYPE (dest), se.expr));
10965 494 : if (cm->ts.u.derived->attr.alloc_comp
10966 372 : && expr->expr_type != EXPR_NULL)
10967 : {
10968 : // TODO: Fix caf_mode
10969 54 : tmp = gfc_copy_alloc_comp (cm->ts.u.derived, se.expr,
10970 : dest, expr->rank, 0);
10971 54 : gfc_add_expr_to_block (&block, tmp);
10972 54 : if (dealloc != NULL_TREE)
10973 18 : gfc_add_expr_to_block (&block, dealloc);
10974 : }
10975 494 : gfc_add_block_to_block (&block, &se.post);
10976 : }
10977 : else
10978 : {
10979 : /* Nested constructors. */
10980 3327 : tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
10981 3327 : gfc_add_expr_to_block (&block, tmp);
10982 : }
10983 : }
10984 19471 : else if (gfc_deferred_strlen (cm, &tmp))
10985 : {
10986 137 : tree strlen;
10987 137 : strlen = tmp;
10988 137 : gcc_assert (strlen);
10989 137 : strlen = fold_build3_loc (input_location, COMPONENT_REF,
10990 137 : TREE_TYPE (strlen),
10991 137 : TREE_OPERAND (dest, 0),
10992 : strlen, NULL_TREE);
10993 :
10994 137 : if (expr->expr_type == EXPR_NULL)
10995 : {
10996 113 : tmp = build_int_cst (TREE_TYPE (cm->backend_decl), 0);
10997 113 : gfc_add_modify (&block, dest, tmp);
10998 113 : tmp = build_int_cst (TREE_TYPE (strlen), 0);
10999 113 : gfc_add_modify (&block, strlen, tmp);
11000 : }
11001 : else
11002 : {
11003 24 : tree size;
11004 24 : gfc_init_se (&se, NULL);
11005 24 : gfc_conv_expr (&se, expr);
11006 24 : size = size_of_string_in_bytes (cm->ts.kind, se.string_length);
11007 24 : size = fold_convert (size_type_node, size);
11008 24 : tmp = build_call_expr_loc (input_location,
11009 : builtin_decl_explicit (BUILT_IN_MALLOC),
11010 : 1, size);
11011 24 : gfc_add_modify (&block, dest,
11012 24 : fold_convert (TREE_TYPE (dest), tmp));
11013 24 : gfc_add_modify (&block, strlen,
11014 24 : fold_convert (TREE_TYPE (strlen), se.string_length));
11015 24 : tmp = gfc_build_memcpy_call (dest, se.expr, size);
11016 24 : gfc_add_expr_to_block (&block, tmp);
11017 : }
11018 : }
11019 19334 : else if (cm->ts.type == BT_CLASS
11020 12 : && !CLASS_DATA (cm)->as
11021 12 : && expr->ts.type == BT_CLASS)
11022 : {
11023 12 : tree vptr1, vptr2;
11024 12 : tree data1, data2;
11025 12 : tree size, fcn;
11026 :
11027 12 : gfc_init_se (&se, NULL);
11028 :
11029 12 : gfc_conv_expr (&se, expr);
11030 :
11031 : /* Copy the _vptr to the destination.... */
11032 12 : vptr1 = gfc_class_vptr_get (dest);
11033 12 : vptr2 = gfc_class_vptr_get (se.expr);
11034 12 : gfc_add_modify (&block, vptr1,
11035 12 : fold_convert (TREE_TYPE (vptr1), vptr2));
11036 :
11037 : /* ....and the _len field if necessary. */
11038 12 : size = gfc_vptr_size_get (vptr2);
11039 12 : if (UNLIMITED_POLY (cm) && UNLIMITED_POLY (expr))
11040 : {
11041 0 : gfc_add_modify (&block, gfc_class_len_get (dest),
11042 : gfc_class_len_get (se.expr));
11043 0 : size = gfc_resize_class_size_with_len (&block, se.expr, size);
11044 : }
11045 :
11046 : /* Allocate the destination data. */
11047 12 : data1 = gfc_class_data_get (dest);
11048 12 : data2 = gfc_class_data_get (se.expr);
11049 12 : tmp = gfc_call_malloc (&block, TREE_TYPE (data1), size);
11050 12 : gfc_add_modify (&block, data1, tmp);
11051 :
11052 : /* Now call the copy function. */
11053 12 : fcn = gfc_vptr_copy_get (vptr2);
11054 12 : if (POINTER_TYPE_P (TREE_TYPE (fcn)))
11055 12 : fcn = build_fold_indirect_ref_loc (input_location, fcn);
11056 12 : tmp = build_call_expr_loc (input_location, fcn, 2,
11057 : data2, data1);
11058 12 : gfc_add_expr_to_block (&block, tmp);
11059 12 : }
11060 19322 : else if (!cm->attr.artificial)
11061 : {
11062 : /* Scalar component (excluding deferred parameters). */
11063 19201 : gfc_init_se (&se, NULL);
11064 19201 : gfc_init_se (&lse, NULL);
11065 :
11066 19201 : gfc_conv_expr (&se, expr);
11067 19201 : if (cm->ts.type == BT_CHARACTER)
11068 1105 : lse.string_length = cm->ts.u.cl->backend_decl;
11069 19201 : lse.expr = dest;
11070 19201 : tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, false, false);
11071 19201 : gfc_add_expr_to_block (&block, tmp);
11072 : }
11073 33438 : return gfc_finish_block (&block);
11074 : }
11075 :
11076 : /* Assign a derived type constructor to a variable. */
11077 :
11078 : tree
11079 22793 : gfc_trans_structure_assign (tree dest, gfc_expr * expr, bool init, bool coarray)
11080 : {
11081 22793 : gfc_constructor *c;
11082 22793 : gfc_component *cm;
11083 22793 : stmtblock_t block;
11084 22793 : tree field;
11085 22793 : tree tmp;
11086 22793 : gfc_se se;
11087 :
11088 22793 : gfc_start_block (&block);
11089 :
11090 22793 : if (expr->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
11091 182 : && (expr->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
11092 13 : || expr->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
11093 : {
11094 182 : gfc_se lse;
11095 :
11096 182 : gfc_init_se (&se, NULL);
11097 182 : gfc_init_se (&lse, NULL);
11098 182 : gfc_conv_expr (&se, gfc_constructor_first (expr->value.constructor)->expr);
11099 182 : lse.expr = dest;
11100 182 : gfc_add_modify (&block, lse.expr,
11101 182 : fold_convert (TREE_TYPE (lse.expr), se.expr));
11102 :
11103 182 : return gfc_finish_block (&block);
11104 : }
11105 :
11106 : /* Make sure that the derived type has been completely built. */
11107 22611 : if (!expr->ts.u.derived->backend_decl
11108 22611 : || !TYPE_FIELDS (expr->ts.u.derived->backend_decl))
11109 : {
11110 231 : tmp = gfc_typenode_for_spec (&expr->ts);
11111 231 : gcc_assert (tmp);
11112 : }
11113 :
11114 22611 : cm = expr->ts.u.derived->components;
11115 :
11116 :
11117 22611 : if (coarray)
11118 225 : gfc_init_se (&se, NULL);
11119 :
11120 22611 : for (c = gfc_constructor_first (expr->value.constructor);
11121 59397 : c; c = gfc_constructor_next (c), cm = cm->next)
11122 : {
11123 : /* Skip absent members in default initializers. */
11124 36786 : if (!c->expr && !cm->attr.allocatable)
11125 3348 : continue;
11126 :
11127 : /* Register the component with the caf-lib before it is initialized.
11128 : Register only allocatable components, that are not coarray'ed
11129 : components (%comp[*]). Only register when the constructor is the
11130 : null-expression. */
11131 33438 : if (coarray && !cm->attr.codimension
11132 515 : && (cm->attr.allocatable || cm->attr.pointer)
11133 179 : && (!c->expr || c->expr->expr_type == EXPR_NULL))
11134 : {
11135 177 : tree token, desc, size;
11136 354 : bool is_array = cm->ts.type == BT_CLASS
11137 177 : ? CLASS_DATA (cm)->attr.dimension : cm->attr.dimension;
11138 :
11139 177 : field = cm->backend_decl;
11140 177 : field = fold_build3_loc (input_location, COMPONENT_REF,
11141 177 : TREE_TYPE (field), dest, field, NULL_TREE);
11142 177 : if (cm->ts.type == BT_CLASS)
11143 0 : field = gfc_class_data_get (field);
11144 :
11145 177 : token
11146 : = is_array
11147 177 : ? gfc_conv_descriptor_token (field)
11148 52 : : fold_build3_loc (input_location, COMPONENT_REF,
11149 52 : TREE_TYPE (gfc_comp_caf_token (cm)), dest,
11150 52 : gfc_comp_caf_token (cm), NULL_TREE);
11151 :
11152 177 : if (is_array)
11153 : {
11154 : /* The _caf_register routine looks at the rank of the array
11155 : descriptor to decide whether the data registered is an array
11156 : or not. */
11157 125 : int rank = cm->ts.type == BT_CLASS ? CLASS_DATA (cm)->as->rank
11158 125 : : cm->as->rank;
11159 : /* When the rank is not known just set a positive rank, which
11160 : suffices to recognize the data as array. */
11161 125 : if (rank < 0)
11162 0 : rank = 1;
11163 125 : size = build_zero_cst (size_type_node);
11164 125 : desc = field;
11165 125 : gfc_conv_descriptor_rank_set (&block, desc, rank);
11166 : }
11167 : else
11168 : {
11169 52 : desc = gfc_conv_scalar_to_descriptor (&se, field,
11170 52 : cm->ts.type == BT_CLASS
11171 52 : ? CLASS_DATA (cm)->attr
11172 : : cm->attr);
11173 52 : size = TYPE_SIZE_UNIT (TREE_TYPE (field));
11174 : }
11175 177 : gfc_add_block_to_block (&block, &se.pre);
11176 177 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_register,
11177 : 7, size, build_int_cst (
11178 : integer_type_node,
11179 : GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY),
11180 : gfc_build_addr_expr (pvoid_type_node,
11181 : token),
11182 : gfc_build_addr_expr (NULL_TREE, desc),
11183 : null_pointer_node, null_pointer_node,
11184 : integer_zero_node);
11185 177 : gfc_add_expr_to_block (&block, tmp);
11186 : }
11187 33438 : field = cm->backend_decl;
11188 33438 : gcc_assert(field);
11189 33438 : tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
11190 : dest, field, NULL_TREE);
11191 33438 : if (!c->expr)
11192 : {
11193 0 : gfc_expr *e = gfc_get_null_expr (NULL);
11194 0 : tmp = gfc_trans_subcomponent_assign (tmp, cm, e, init);
11195 0 : gfc_free_expr (e);
11196 : }
11197 : else
11198 33438 : tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr, init);
11199 33438 : gfc_add_expr_to_block (&block, tmp);
11200 : }
11201 22611 : return gfc_finish_block (&block);
11202 : }
11203 :
11204 : static void
11205 21 : gfc_conv_union_initializer (vec<constructor_elt, va_gc> *&v,
11206 : gfc_component *un, gfc_expr *init)
11207 : {
11208 21 : gfc_constructor *ctor;
11209 :
11210 21 : if (un->ts.type != BT_UNION || un == NULL || init == NULL)
11211 : return;
11212 :
11213 21 : ctor = gfc_constructor_first (init->value.constructor);
11214 :
11215 21 : if (ctor == NULL || ctor->expr == NULL)
11216 : return;
11217 :
11218 21 : gcc_assert (init->expr_type == EXPR_STRUCTURE);
11219 :
11220 : /* If we have an 'initialize all' constructor, do it first. */
11221 21 : if (ctor->expr->expr_type == EXPR_NULL)
11222 : {
11223 9 : tree union_type = TREE_TYPE (un->backend_decl);
11224 9 : tree val = build_constructor (union_type, NULL);
11225 9 : CONSTRUCTOR_APPEND_ELT (v, un->backend_decl, val);
11226 9 : ctor = gfc_constructor_next (ctor);
11227 : }
11228 :
11229 : /* Add the map initializer on top. */
11230 21 : if (ctor != NULL && ctor->expr != NULL)
11231 : {
11232 12 : gcc_assert (ctor->expr->expr_type == EXPR_STRUCTURE);
11233 12 : tree val = gfc_conv_initializer (ctor->expr, &un->ts,
11234 12 : TREE_TYPE (un->backend_decl),
11235 12 : un->attr.dimension, un->attr.pointer,
11236 12 : un->attr.proc_pointer);
11237 12 : CONSTRUCTOR_APPEND_ELT (v, un->backend_decl, val);
11238 : }
11239 : }
11240 :
11241 : /* Build an expression for a constructor. If init is nonzero then
11242 : this is part of a static variable initializer. */
11243 :
11244 : void
11245 42422 : gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
11246 : {
11247 42422 : gfc_constructor *c;
11248 42422 : gfc_component *cm;
11249 42422 : tree val;
11250 42422 : tree type;
11251 42422 : tree tmp;
11252 42422 : vec<constructor_elt, va_gc> *v = NULL;
11253 :
11254 42422 : gcc_assert (se->ss == NULL);
11255 42422 : gcc_assert (expr->expr_type == EXPR_STRUCTURE);
11256 42422 : type = gfc_typenode_for_spec (&expr->ts);
11257 :
11258 42422 : if (!init)
11259 : {
11260 17456 : if (IS_PDT (expr) && expr->must_finalize)
11261 288 : final_block = &se->finalblock;
11262 :
11263 : /* Create a temporary variable and fill it in. */
11264 17456 : se->expr = gfc_create_var (type, expr->ts.u.derived->name);
11265 : /* The symtree in expr is NULL, if the code to generate is for
11266 : initializing the static members only. */
11267 34912 : tmp = gfc_trans_structure_assign (se->expr, expr, expr->symtree != NULL,
11268 17456 : se->want_coarray);
11269 17456 : gfc_add_expr_to_block (&se->pre, tmp);
11270 17456 : final_block = NULL;
11271 :
11272 : /* Did we ask for a pointer to the structure temporary? */
11273 17456 : if (se->want_pointer && !POINTER_TYPE_P (TREE_TYPE (se->expr)))
11274 2 : se->expr = gfc_build_addr_expr (NULL, se->expr);
11275 :
11276 17456 : return;
11277 : }
11278 :
11279 24966 : cm = expr->ts.u.derived->components;
11280 :
11281 24966 : for (c = gfc_constructor_first (expr->value.constructor);
11282 127269 : c && cm; c = gfc_constructor_next (c), cm = cm->next)
11283 : {
11284 : /* Skip absent members in default initializers and allocatable
11285 : components. Although the latter have a default initializer
11286 : of EXPR_NULL,... by default, the static nullify is not needed
11287 : since this is done every time we come into scope. */
11288 111950 : if (!c->expr
11289 99668 : || (cm->attr.allocatable && cm->attr.flavor != FL_PROCEDURE)
11290 195194 : || (IS_PDT (cm) && has_parameterized_comps (cm->ts.u.derived)))
11291 9647 : continue;
11292 :
11293 92656 : if (cm->initializer && cm->initializer->expr_type != EXPR_NULL
11294 54397 : && strcmp (cm->name, "_extends") == 0
11295 1479 : && cm->initializer->symtree)
11296 : {
11297 1479 : tree vtab;
11298 1479 : gfc_symbol *vtabs;
11299 1479 : vtabs = cm->initializer->symtree->n.sym;
11300 1479 : vtab = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtabs));
11301 1479 : vtab = unshare_expr_without_location (vtab);
11302 1479 : CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, vtab);
11303 1479 : }
11304 91177 : else if (cm->ts.u.derived && strcmp (cm->name, "_size") == 0)
11305 : {
11306 9811 : val = TYPE_SIZE_UNIT (gfc_get_derived_type (cm->ts.u.derived));
11307 9811 : CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
11308 : fold_convert (TREE_TYPE (cm->backend_decl),
11309 : val));
11310 9811 : }
11311 81366 : else if (cm->ts.type == BT_INTEGER && strcmp (cm->name, "_len") == 0)
11312 479 : CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
11313 : fold_convert (TREE_TYPE (cm->backend_decl),
11314 479 : integer_zero_node));
11315 80887 : else if (cm->ts.type == BT_UNION)
11316 21 : gfc_conv_union_initializer (v, cm, c->expr);
11317 : else
11318 : {
11319 80866 : val = gfc_conv_initializer (c->expr, &cm->ts,
11320 80866 : TREE_TYPE (cm->backend_decl),
11321 80866 : cm->attr.dimension, cm->attr.pointer,
11322 80866 : cm->attr.proc_pointer);
11323 80866 : val = unshare_expr_without_location (val);
11324 :
11325 : /* Append it to the constructor list. */
11326 183169 : CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
11327 : }
11328 : }
11329 :
11330 24966 : se->expr = build_constructor (type, v);
11331 24966 : if (init)
11332 24966 : TREE_CONSTANT (se->expr) = 1;
11333 : }
11334 :
11335 :
11336 : /* Translate a substring expression. */
11337 :
11338 : static void
11339 258 : gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
11340 : {
11341 258 : gfc_ref *ref;
11342 :
11343 258 : ref = expr->ref;
11344 :
11345 258 : gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
11346 :
11347 516 : se->expr = gfc_build_wide_string_const (expr->ts.kind,
11348 258 : expr->value.character.length,
11349 258 : expr->value.character.string);
11350 :
11351 258 : se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
11352 258 : TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
11353 :
11354 258 : if (ref)
11355 258 : gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
11356 258 : }
11357 :
11358 :
11359 : /* Entry point for expression translation. Evaluates a scalar quantity.
11360 : EXPR is the expression to be translated, and SE is the state structure if
11361 : called from within the scalarized. */
11362 :
11363 : void
11364 3817262 : gfc_conv_expr (gfc_se * se, gfc_expr * expr)
11365 : {
11366 3817262 : gfc_ss *ss;
11367 :
11368 3817262 : ss = se->ss;
11369 3817262 : if (ss && ss->info->expr == expr
11370 245312 : && (ss->info->type == GFC_SS_SCALAR
11371 : || ss->info->type == GFC_SS_REFERENCE))
11372 : {
11373 41524 : gfc_ss_info *ss_info;
11374 :
11375 41524 : ss_info = ss->info;
11376 : /* Substitute a scalar expression evaluated outside the scalarization
11377 : loop. */
11378 41524 : se->expr = ss_info->data.scalar.value;
11379 41524 : if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
11380 910 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
11381 :
11382 41524 : se->string_length = ss_info->string_length;
11383 41524 : gfc_advance_se_ss_chain (se);
11384 41524 : return;
11385 : }
11386 :
11387 : /* We need to convert the expressions for the iso_c_binding derived types.
11388 : C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
11389 : null_pointer_node. C_PTR and C_FUNPTR are converted to match the
11390 : typespec for the C_PTR and C_FUNPTR symbols, which has already been
11391 : updated to be an integer with a kind equal to the size of a (void *). */
11392 3775738 : if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->ts.f90_type == BT_VOID
11393 15037 : && expr->ts.u.derived->attr.is_bind_c)
11394 : {
11395 14086 : if (expr->expr_type == EXPR_VARIABLE
11396 9610 : && (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
11397 9610 : || expr->symtree->n.sym->intmod_sym_id
11398 : == ISOCBINDING_NULL_FUNPTR))
11399 : {
11400 : /* Set expr_type to EXPR_NULL, which will result in
11401 : null_pointer_node being used below. */
11402 0 : expr->expr_type = EXPR_NULL;
11403 : }
11404 : else
11405 : {
11406 : /* Update the type/kind of the expression to be what the new
11407 : type/kind are for the updated symbols of C_PTR/C_FUNPTR. */
11408 14086 : expr->ts.type = BT_INTEGER;
11409 14086 : expr->ts.f90_type = BT_VOID;
11410 14086 : expr->ts.kind = gfc_index_integer_kind;
11411 : }
11412 : }
11413 :
11414 3775738 : gfc_fix_class_refs (expr);
11415 :
11416 3775738 : switch (expr->expr_type)
11417 : {
11418 523570 : case EXPR_OP:
11419 523570 : gfc_conv_expr_op (se, expr);
11420 523570 : break;
11421 :
11422 159 : case EXPR_CONDITIONAL:
11423 159 : gfc_conv_conditional_expr (se, expr);
11424 159 : break;
11425 :
11426 322915 : case EXPR_FUNCTION:
11427 322915 : gfc_conv_function_expr (se, expr);
11428 322915 : break;
11429 :
11430 1182287 : case EXPR_CONSTANT:
11431 1182287 : gfc_conv_constant (se, expr);
11432 1182287 : break;
11433 :
11434 1687262 : case EXPR_VARIABLE:
11435 1687262 : gfc_conv_variable (se, expr);
11436 1687262 : break;
11437 :
11438 4384 : case EXPR_NULL:
11439 4384 : se->expr = null_pointer_node;
11440 4384 : break;
11441 :
11442 258 : case EXPR_SUBSTRING:
11443 258 : gfc_conv_substring_expr (se, expr);
11444 258 : break;
11445 :
11446 17456 : case EXPR_STRUCTURE:
11447 17456 : gfc_conv_structure (se, expr, 0);
11448 : /* F2008 4.5.6.3 para 5: If an executable construct references a
11449 : structure constructor or array constructor, the entity created by
11450 : the constructor is finalized after execution of the innermost
11451 : executable construct containing the reference. This, in fact,
11452 : was later deleted by the Combined Technical Corrigenda 1 TO 4 for
11453 : fortran 2008 (f08/0011). */
11454 17456 : if ((gfc_option.allow_std & (GFC_STD_F2008 | GFC_STD_F2003))
11455 17456 : && !(gfc_option.allow_std & GFC_STD_GNU)
11456 143 : && expr->must_finalize
11457 17468 : && gfc_may_be_finalized (expr->ts))
11458 : {
11459 12 : locus loc;
11460 12 : gfc_locus_from_location (&loc, input_location);
11461 12 : gfc_warning (0, "The structure constructor at %L has been"
11462 : " finalized. This feature was removed by f08/0011."
11463 : " Use -std=f2018 or -std=gnu to eliminate the"
11464 : " finalization.", &loc);
11465 12 : symbol_attribute attr;
11466 12 : attr.allocatable = attr.pointer = 0;
11467 12 : gfc_finalize_tree_expr (se, expr->ts.u.derived, attr, 0);
11468 12 : gfc_add_block_to_block (&se->post, &se->finalblock);
11469 : }
11470 : break;
11471 :
11472 37447 : case EXPR_ARRAY:
11473 37447 : gfc_conv_array_constructor_expr (se, expr);
11474 37447 : gfc_add_block_to_block (&se->post, &se->finalblock);
11475 37447 : break;
11476 :
11477 0 : default:
11478 0 : gcc_unreachable ();
11479 3817262 : break;
11480 : }
11481 : }
11482 :
11483 : /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
11484 : of an assignment. */
11485 : void
11486 399432 : gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
11487 : {
11488 399432 : gfc_conv_expr (se, expr);
11489 : /* All numeric lvalues should have empty post chains. If not we need to
11490 : figure out a way of rewriting an lvalue so that it has no post chain. */
11491 399432 : gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
11492 399432 : }
11493 :
11494 : /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
11495 : numeric expressions. Used for scalar values where inserting cleanup code
11496 : is inconvenient. */
11497 : void
11498 1080845 : gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
11499 : {
11500 1080845 : tree val;
11501 :
11502 1080845 : gcc_assert (expr->ts.type != BT_CHARACTER);
11503 1080845 : gfc_conv_expr (se, expr);
11504 1080845 : if (se->post.head)
11505 : {
11506 2677 : val = gfc_create_var (TREE_TYPE (se->expr), NULL);
11507 2677 : gfc_add_modify (&se->pre, val, se->expr);
11508 2677 : se->expr = val;
11509 2677 : gfc_add_block_to_block (&se->pre, &se->post);
11510 : }
11511 1080845 : }
11512 :
11513 : /* Helper to translate an expression and convert it to a particular type. */
11514 : void
11515 310659 : gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
11516 : {
11517 310659 : gfc_conv_expr_val (se, expr);
11518 310659 : se->expr = convert (type, se->expr);
11519 310659 : }
11520 :
11521 :
11522 : /* Converts an expression so that it can be passed by reference. Scalar
11523 : values only. */
11524 :
11525 : void
11526 234639 : gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
11527 : {
11528 234639 : gfc_ss *ss;
11529 234639 : tree var;
11530 :
11531 234639 : ss = se->ss;
11532 234639 : if (ss && ss->info->expr == expr
11533 8090 : && ss->info->type == GFC_SS_REFERENCE)
11534 : {
11535 : /* Returns a reference to the scalar evaluated outside the loop
11536 : for this case. */
11537 913 : gfc_conv_expr (se, expr);
11538 :
11539 913 : if (expr->ts.type == BT_CHARACTER
11540 114 : && expr->expr_type != EXPR_FUNCTION)
11541 102 : gfc_conv_string_parameter (se);
11542 : else
11543 811 : se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
11544 :
11545 : return;
11546 : }
11547 :
11548 233726 : if (expr->ts.type == BT_CHARACTER)
11549 : {
11550 50266 : gfc_conv_expr (se, expr);
11551 50266 : gfc_conv_string_parameter (se);
11552 50266 : return;
11553 : }
11554 :
11555 183460 : if (expr->expr_type == EXPR_VARIABLE)
11556 : {
11557 73490 : se->want_pointer = 1;
11558 73490 : gfc_conv_expr (se, expr);
11559 73490 : if (se->post.head)
11560 : {
11561 0 : var = gfc_create_var (TREE_TYPE (se->expr), NULL);
11562 0 : gfc_add_modify (&se->pre, var, se->expr);
11563 0 : gfc_add_block_to_block (&se->pre, &se->post);
11564 0 : se->expr = var;
11565 : }
11566 : return;
11567 : }
11568 :
11569 109970 : if (expr->expr_type == EXPR_CONDITIONAL)
11570 : {
11571 18 : se->want_pointer = 1;
11572 18 : gfc_conv_expr (se, expr);
11573 18 : return;
11574 : }
11575 :
11576 : /* The copy of a parenthesized CLASS variable is freed in se->post, so it
11577 : must stay there until after the copy's use. */
11578 186 : if (expr->ts.type == BT_CLASS && expr->rank == 0
11579 109952 : && parenthesized_variable (expr))
11580 : {
11581 6 : gfc_conv_expr (se, expr);
11582 6 : se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
11583 6 : return;
11584 : }
11585 :
11586 109946 : if (expr->expr_type == EXPR_FUNCTION
11587 14506 : && ((expr->value.function.esym
11588 2191 : && expr->value.function.esym->result
11589 2190 : && expr->value.function.esym->result->attr.pointer
11590 83 : && !expr->value.function.esym->result->attr.dimension)
11591 14429 : || (!expr->value.function.esym && !expr->ref
11592 12209 : && expr->symtree->n.sym->attr.pointer
11593 0 : && !expr->symtree->n.sym->attr.dimension)))
11594 : {
11595 77 : se->want_pointer = 1;
11596 77 : gfc_conv_expr (se, expr);
11597 77 : var = gfc_create_var (TREE_TYPE (se->expr), NULL);
11598 77 : gfc_add_modify (&se->pre, var, se->expr);
11599 77 : se->expr = var;
11600 77 : return;
11601 : }
11602 :
11603 109869 : gfc_conv_expr (se, expr);
11604 :
11605 : /* Create a temporary var to hold the value. */
11606 109869 : if (TREE_CONSTANT (se->expr))
11607 : {
11608 : tree tmp = se->expr;
11609 86505 : STRIP_TYPE_NOPS (tmp);
11610 86505 : var = build_decl (input_location,
11611 86505 : CONST_DECL, NULL, TREE_TYPE (tmp));
11612 86505 : DECL_INITIAL (var) = tmp;
11613 86505 : TREE_STATIC (var) = 1;
11614 86505 : pushdecl (var);
11615 : }
11616 : else
11617 : {
11618 23364 : var = gfc_create_var (TREE_TYPE (se->expr), NULL);
11619 23364 : gfc_add_modify (&se->pre, var, se->expr);
11620 : }
11621 :
11622 109869 : if (!expr->must_finalize)
11623 109773 : gfc_add_block_to_block (&se->pre, &se->post);
11624 :
11625 : /* Take the address of that value. */
11626 109869 : se->expr = gfc_build_addr_expr (NULL_TREE, var);
11627 : }
11628 :
11629 :
11630 : /* Get the _len component for an unlimited polymorphic expression. */
11631 :
11632 : static tree
11633 2082 : trans_get_upoly_len (stmtblock_t *block, gfc_expr *expr)
11634 : {
11635 2082 : gfc_se se;
11636 2082 : gfc_ref *ref = expr->ref;
11637 :
11638 2082 : gfc_init_se (&se, NULL);
11639 4278 : while (ref && ref->next)
11640 : ref = ref->next;
11641 2082 : gfc_add_len_component (expr);
11642 2082 : gfc_conv_expr (&se, expr);
11643 2082 : gfc_add_block_to_block (block, &se.pre);
11644 2082 : gcc_assert (se.post.head == NULL_TREE);
11645 2082 : if (ref)
11646 : {
11647 316 : gfc_free_ref_list (ref->next);
11648 316 : ref->next = NULL;
11649 : }
11650 : else
11651 : {
11652 1766 : gfc_free_ref_list (expr->ref);
11653 1766 : expr->ref = NULL;
11654 : }
11655 2082 : return se.expr;
11656 : }
11657 :
11658 :
11659 : /* Assign _vptr and _len components as appropriate. BLOCK should be a
11660 : statement-list outside of the scalarizer-loop. When code is generated, that
11661 : depends on the scalarized expression, it is added to RSE.PRE.
11662 : Returns le's _vptr tree and when set the len expressions in to_lenp and
11663 : from_lenp to form a le%_vptr%_copy (re, le, [from_lenp, to_lenp])
11664 : expression. */
11665 :
11666 : static tree
11667 5132 : trans_class_vptr_len_assignment (stmtblock_t *block, gfc_expr * le,
11668 : gfc_expr * re, gfc_se *rse,
11669 : tree * to_lenp, tree * from_lenp,
11670 : tree * from_vptrp)
11671 : {
11672 5132 : gfc_se se;
11673 5132 : gfc_expr * vptr_expr;
11674 5132 : tree tmp, to_len = NULL_TREE, from_len = NULL_TREE, lhs_vptr;
11675 5132 : bool set_vptr = false, temp_rhs = false;
11676 5132 : stmtblock_t *pre = block;
11677 5132 : tree class_expr = NULL_TREE;
11678 5132 : tree from_vptr = NULL_TREE;
11679 :
11680 : /* Create a temporary for complicated expressions. */
11681 5132 : if (re->expr_type != EXPR_VARIABLE && re->expr_type != EXPR_NULL
11682 1432 : && rse->expr != NULL_TREE)
11683 : {
11684 1432 : if (!DECL_P (rse->expr))
11685 : {
11686 416 : if (re->ts.type == BT_CLASS && !GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
11687 37 : class_expr = gfc_get_class_from_expr (rse->expr);
11688 :
11689 416 : if (rse->loop)
11690 159 : pre = &rse->loop->pre;
11691 : else
11692 257 : pre = &rse->pre;
11693 :
11694 416 : if (class_expr != NULL_TREE && UNLIMITED_POLY (re))
11695 37 : tmp = gfc_evaluate_now (TREE_OPERAND (rse->expr, 0), &rse->pre);
11696 : else
11697 379 : tmp = gfc_evaluate_now (rse->expr, &rse->pre);
11698 :
11699 416 : rse->expr = tmp;
11700 : }
11701 : else
11702 1016 : pre = &rse->pre;
11703 :
11704 : temp_rhs = true;
11705 : }
11706 :
11707 : /* Get the _vptr for the left-hand side expression. */
11708 5132 : gfc_init_se (&se, NULL);
11709 5132 : vptr_expr = gfc_find_and_cut_at_last_class_ref (le);
11710 5132 : if (vptr_expr != NULL && gfc_expr_attr (vptr_expr).class_ok)
11711 : {
11712 : /* Care about _len for unlimited polymorphic entities. */
11713 5132 : if (UNLIMITED_POLY (vptr_expr)
11714 3956 : || (vptr_expr->ts.type == BT_DERIVED
11715 2788 : && vptr_expr->ts.u.derived->attr.unlimited_polymorphic))
11716 1750 : to_len = trans_get_upoly_len (block, vptr_expr);
11717 5132 : gfc_add_vptr_component (vptr_expr);
11718 5132 : set_vptr = true;
11719 : }
11720 : else
11721 0 : vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&le->ts));
11722 5132 : se.want_pointer = 1;
11723 5132 : gfc_conv_expr (&se, vptr_expr);
11724 5132 : gfc_free_expr (vptr_expr);
11725 5132 : gfc_add_block_to_block (block, &se.pre);
11726 5132 : gcc_assert (se.post.head == NULL_TREE);
11727 5132 : lhs_vptr = se.expr;
11728 5132 : STRIP_NOPS (lhs_vptr);
11729 :
11730 : /* Set the _vptr only when the left-hand side of the assignment is a
11731 : class-object. */
11732 5132 : if (set_vptr)
11733 : {
11734 : /* Get the vptr from the rhs expression only, when it is variable.
11735 : Functions are expected to be assigned to a temporary beforehand. */
11736 3571 : vptr_expr = (re->expr_type == EXPR_VARIABLE && re->ts.type == BT_CLASS)
11737 6033 : ? gfc_find_and_cut_at_last_class_ref (re)
11738 : : NULL;
11739 901 : if (vptr_expr != NULL && vptr_expr->ts.type == BT_CLASS)
11740 : {
11741 901 : if (to_len != NULL_TREE)
11742 : {
11743 : /* Get the _len information from the rhs. */
11744 347 : if (UNLIMITED_POLY (vptr_expr)
11745 : || (vptr_expr->ts.type == BT_DERIVED
11746 : && vptr_expr->ts.u.derived->attr.unlimited_polymorphic))
11747 320 : from_len = trans_get_upoly_len (block, vptr_expr);
11748 : }
11749 901 : gfc_add_vptr_component (vptr_expr);
11750 : }
11751 : else
11752 : {
11753 4231 : if (re->expr_type == EXPR_VARIABLE
11754 2670 : && DECL_P (re->symtree->n.sym->backend_decl)
11755 2670 : && DECL_LANG_SPECIFIC (re->symtree->n.sym->backend_decl)
11756 864 : && GFC_DECL_SAVED_DESCRIPTOR (re->symtree->n.sym->backend_decl)
11757 4286 : && GFC_CLASS_TYPE_P (TREE_TYPE (GFC_DECL_SAVED_DESCRIPTOR (
11758 : re->symtree->n.sym->backend_decl))))
11759 : {
11760 31 : vptr_expr = NULL;
11761 31 : se.expr = gfc_class_vptr_get (GFC_DECL_SAVED_DESCRIPTOR (
11762 : re->symtree->n.sym->backend_decl));
11763 31 : if (to_len && UNLIMITED_POLY (re))
11764 0 : from_len = gfc_class_len_get (GFC_DECL_SAVED_DESCRIPTOR (
11765 : re->symtree->n.sym->backend_decl));
11766 : }
11767 4200 : else if (temp_rhs && re->ts.type == BT_CLASS)
11768 : {
11769 245 : vptr_expr = NULL;
11770 245 : if (class_expr)
11771 : tmp = class_expr;
11772 208 : else if (!GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
11773 0 : tmp = gfc_get_class_from_expr (rse->expr);
11774 : else
11775 : tmp = rse->expr;
11776 :
11777 245 : se.expr = gfc_class_vptr_get (tmp);
11778 245 : from_vptr = se.expr;
11779 245 : if (UNLIMITED_POLY (re))
11780 86 : from_len = gfc_class_len_get (tmp);
11781 :
11782 : }
11783 3955 : else if (re->expr_type != EXPR_NULL)
11784 : /* Only when rhs is non-NULL use its declared type for vptr
11785 : initialisation. */
11786 3826 : vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&re->ts));
11787 : else
11788 : /* When the rhs is NULL use the vtab of lhs' declared type. */
11789 129 : vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&le->ts));
11790 : }
11791 :
11792 4942 : if (vptr_expr)
11793 : {
11794 4856 : gfc_init_se (&se, NULL);
11795 4856 : se.want_pointer = 1;
11796 4856 : gfc_conv_expr (&se, vptr_expr);
11797 4856 : gfc_free_expr (vptr_expr);
11798 4856 : gfc_add_block_to_block (block, &se.pre);
11799 4856 : gcc_assert (se.post.head == NULL_TREE);
11800 4856 : from_vptr = se.expr;
11801 : }
11802 5132 : gfc_add_modify (pre, lhs_vptr, fold_convert (TREE_TYPE (lhs_vptr),
11803 : se.expr));
11804 :
11805 5132 : if (to_len != NULL_TREE)
11806 : {
11807 : /* The _len component needs to be set. Figure how to get the
11808 : value of the right-hand side. */
11809 1750 : if (from_len == NULL_TREE)
11810 : {
11811 1344 : if (rse->string_length != NULL_TREE)
11812 : from_len = rse->string_length;
11813 856 : else if (re->ts.type == BT_CHARACTER && re->ts.u.cl->length)
11814 : {
11815 0 : gfc_init_se (&se, NULL);
11816 0 : gfc_conv_expr (&se, re->ts.u.cl->length);
11817 0 : gfc_add_block_to_block (block, &se.pre);
11818 0 : gcc_assert (se.post.head == NULL_TREE);
11819 0 : from_len = gfc_evaluate_now (se.expr, block);
11820 : }
11821 : else
11822 856 : from_len = build_zero_cst (gfc_charlen_type_node);
11823 : }
11824 1750 : gfc_add_modify (pre, to_len, fold_convert (TREE_TYPE (to_len),
11825 : from_len));
11826 : }
11827 : }
11828 :
11829 : /* Return the _len and _vptr trees only, when requested. */
11830 5132 : if (to_lenp)
11831 3779 : *to_lenp = to_len;
11832 5132 : if (from_lenp)
11833 3779 : *from_lenp = from_len;
11834 5132 : if (from_vptrp)
11835 3779 : *from_vptrp = from_vptr;
11836 5132 : return lhs_vptr;
11837 : }
11838 :
11839 :
11840 : /* Assign tokens for pointer components. */
11841 :
11842 : static void
11843 12 : trans_caf_token_assign (gfc_se *lse, gfc_se *rse, gfc_expr *expr1,
11844 : gfc_expr *expr2)
11845 : {
11846 12 : symbol_attribute lhs_attr, rhs_attr;
11847 12 : tree tmp, lhs_tok, rhs_tok;
11848 : /* Flag to indicated component refs on the rhs. */
11849 12 : bool rhs_cr;
11850 :
11851 12 : lhs_attr = gfc_caf_attr (expr1);
11852 12 : if (expr2->expr_type != EXPR_NULL)
11853 : {
11854 8 : rhs_attr = gfc_caf_attr (expr2, false, &rhs_cr);
11855 8 : if (lhs_attr.codimension && rhs_attr.codimension)
11856 : {
11857 4 : lhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (lse, expr1);
11858 4 : lhs_tok = build_fold_indirect_ref (lhs_tok);
11859 :
11860 4 : if (rhs_cr)
11861 0 : rhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (rse, expr2);
11862 : else
11863 : {
11864 4 : tree caf_decl;
11865 4 : caf_decl = gfc_get_tree_for_caf_expr (expr2);
11866 4 : gfc_get_caf_token_offset (rse, &rhs_tok, NULL, caf_decl,
11867 : NULL_TREE, NULL);
11868 : }
11869 4 : tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
11870 : lhs_tok,
11871 4 : fold_convert (TREE_TYPE (lhs_tok), rhs_tok));
11872 4 : gfc_prepend_expr_to_block (&lse->post, tmp);
11873 : }
11874 : }
11875 4 : else if (lhs_attr.codimension)
11876 : {
11877 4 : lhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (lse, expr1);
11878 4 : if (!lhs_tok)
11879 : {
11880 2 : lhs_tok = gfc_get_tree_for_caf_expr (expr1);
11881 2 : lhs_tok = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (lhs_tok));
11882 : }
11883 : else
11884 2 : lhs_tok = build_fold_indirect_ref (lhs_tok);
11885 4 : tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
11886 : lhs_tok, null_pointer_node);
11887 4 : gfc_prepend_expr_to_block (&lse->post, tmp);
11888 : }
11889 12 : }
11890 :
11891 :
11892 : /* Do everything that is needed for a CLASS function expr2. */
11893 :
11894 : static tree
11895 18 : trans_class_pointer_fcn (stmtblock_t *block, gfc_se *lse, gfc_se *rse,
11896 : gfc_expr *expr1, gfc_expr *expr2)
11897 : {
11898 18 : tree expr1_vptr = NULL_TREE;
11899 18 : tree tmp;
11900 :
11901 18 : gfc_conv_function_expr (rse, expr2);
11902 18 : rse->expr = gfc_evaluate_now (rse->expr, &rse->pre);
11903 :
11904 18 : if (expr1->ts.type != BT_CLASS)
11905 12 : rse->expr = gfc_class_data_get (rse->expr);
11906 : else
11907 : {
11908 6 : expr1_vptr = trans_class_vptr_len_assignment (block, expr1,
11909 : expr2, rse,
11910 : NULL, NULL, NULL);
11911 6 : gfc_add_block_to_block (block, &rse->pre);
11912 6 : tmp = gfc_create_var (TREE_TYPE (rse->expr), "ptrtemp");
11913 6 : gfc_add_modify (&lse->pre, tmp, rse->expr);
11914 :
11915 12 : gfc_add_modify (&lse->pre, expr1_vptr,
11916 6 : fold_convert (TREE_TYPE (expr1_vptr),
11917 : gfc_class_vptr_get (tmp)));
11918 6 : rse->expr = gfc_class_data_get (tmp);
11919 : }
11920 :
11921 18 : return expr1_vptr;
11922 : }
11923 :
11924 :
11925 : tree
11926 10526 : gfc_trans_pointer_assign (gfc_code * code)
11927 : {
11928 10526 : return gfc_trans_pointer_assignment (code->expr1, code->expr2);
11929 : }
11930 :
11931 :
11932 : /* Generate code for a pointer assignment. */
11933 :
11934 : tree
11935 10581 : gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
11936 : {
11937 10581 : gfc_se lse;
11938 10581 : gfc_se rse;
11939 10581 : stmtblock_t block;
11940 10581 : tree desc;
11941 10581 : tree tmp;
11942 10581 : tree expr1_vptr = NULL_TREE;
11943 10581 : bool scalar, non_proc_ptr_assign;
11944 10581 : gfc_ss *ss;
11945 :
11946 10581 : gfc_start_block (&block);
11947 :
11948 10581 : gfc_init_se (&lse, NULL);
11949 :
11950 : /* Usually testing whether this is not a proc pointer assignment. */
11951 10581 : non_proc_ptr_assign
11952 10581 : = !(gfc_expr_attr (expr1).proc_pointer
11953 1231 : && ((expr2->expr_type == EXPR_VARIABLE
11954 993 : && expr2->symtree->n.sym->attr.flavor == FL_PROCEDURE)
11955 288 : || expr2->expr_type == EXPR_NULL));
11956 :
11957 : /* Check whether the expression is a scalar or not; we cannot use
11958 : expr1->rank as it can be nonzero for proc pointers. */
11959 10581 : ss = gfc_walk_expr (expr1);
11960 10581 : scalar = ss == gfc_ss_terminator;
11961 10581 : if (!scalar)
11962 4659 : gfc_free_ss_chain (ss);
11963 :
11964 10581 : if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS
11965 96 : && expr2->expr_type != EXPR_FUNCTION && non_proc_ptr_assign)
11966 : {
11967 72 : gfc_add_data_component (expr2);
11968 : /* The following is required as gfc_add_data_component doesn't
11969 : update ts.type if there is a trailing REF_ARRAY. */
11970 72 : expr2->ts.type = BT_DERIVED;
11971 : }
11972 :
11973 10581 : if (scalar)
11974 : {
11975 : /* Scalar pointers. */
11976 5922 : lse.want_pointer = 1;
11977 5922 : gfc_conv_expr (&lse, expr1);
11978 5922 : gfc_init_se (&rse, NULL);
11979 5922 : rse.want_pointer = 1;
11980 5922 : if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
11981 6 : trans_class_pointer_fcn (&block, &lse, &rse, expr1, expr2);
11982 : else
11983 5916 : gfc_conv_expr (&rse, expr2);
11984 :
11985 5922 : if (non_proc_ptr_assign && expr1->ts.type == BT_CLASS)
11986 : {
11987 787 : trans_class_vptr_len_assignment (&block, expr1, expr2, &rse, NULL,
11988 : NULL, NULL);
11989 787 : lse.expr = gfc_class_data_get (lse.expr);
11990 : }
11991 :
11992 5922 : if (expr1->symtree->n.sym->attr.proc_pointer
11993 863 : && expr1->symtree->n.sym->attr.dummy)
11994 49 : lse.expr = build_fold_indirect_ref_loc (input_location,
11995 : lse.expr);
11996 :
11997 5922 : if (expr2->symtree && expr2->symtree->n.sym->attr.proc_pointer
11998 47 : && expr2->symtree->n.sym->attr.dummy)
11999 20 : rse.expr = build_fold_indirect_ref_loc (input_location,
12000 : rse.expr);
12001 :
12002 5922 : gfc_add_block_to_block (&block, &lse.pre);
12003 5922 : gfc_add_block_to_block (&block, &rse.pre);
12004 :
12005 : /* Check character lengths if character expression. The test is only
12006 : really added if -fbounds-check is enabled. Exclude deferred
12007 : character length lefthand sides. */
12008 960 : if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL
12009 786 : && !expr1->ts.deferred
12010 371 : && !expr1->symtree->n.sym->attr.proc_pointer
12011 6286 : && !gfc_is_proc_ptr_comp (expr1))
12012 : {
12013 345 : gcc_assert (expr2->ts.type == BT_CHARACTER);
12014 345 : gcc_assert (lse.string_length && rse.string_length);
12015 345 : gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
12016 : lse.string_length, rse.string_length,
12017 : &block);
12018 : }
12019 :
12020 : /* The assignment to an deferred character length sets the string
12021 : length to that of the rhs. */
12022 5922 : if (expr1->ts.deferred)
12023 : {
12024 530 : if (expr2->expr_type != EXPR_NULL && lse.string_length != NULL)
12025 413 : gfc_add_modify (&block, lse.string_length,
12026 413 : fold_convert (TREE_TYPE (lse.string_length),
12027 : rse.string_length));
12028 117 : else if (lse.string_length != NULL)
12029 115 : gfc_add_modify (&block, lse.string_length,
12030 115 : build_zero_cst (TREE_TYPE (lse.string_length)));
12031 : }
12032 :
12033 5922 : gfc_add_modify (&block, lse.expr,
12034 5922 : fold_convert (TREE_TYPE (lse.expr), rse.expr));
12035 :
12036 5922 : if (flag_coarray == GFC_FCOARRAY_LIB)
12037 : {
12038 354 : if (expr1->ref)
12039 : /* Also set the tokens for pointer components in derived typed
12040 : coarrays. */
12041 12 : trans_caf_token_assign (&lse, &rse, expr1, expr2);
12042 342 : else if (gfc_caf_attr (expr1).codimension)
12043 : {
12044 0 : tree lhs_caf_decl, rhs_caf_decl, lhs_tok, rhs_tok;
12045 :
12046 0 : lhs_caf_decl = gfc_get_tree_for_caf_expr (expr1);
12047 0 : rhs_caf_decl = gfc_get_tree_for_caf_expr (expr2);
12048 0 : gfc_get_caf_token_offset (&lse, &lhs_tok, nullptr, lhs_caf_decl,
12049 : NULL_TREE, expr1);
12050 0 : gfc_get_caf_token_offset (&rse, &rhs_tok, nullptr, rhs_caf_decl,
12051 : NULL_TREE, expr2);
12052 0 : gfc_add_modify (&block, lhs_tok, rhs_tok);
12053 : }
12054 : }
12055 :
12056 5922 : gfc_add_block_to_block (&block, &rse.post);
12057 5922 : gfc_add_block_to_block (&block, &lse.post);
12058 : }
12059 : else
12060 : {
12061 4659 : gfc_ref* remap;
12062 4659 : bool rank_remap;
12063 4659 : tree strlen_lhs;
12064 4659 : tree strlen_rhs = NULL_TREE;
12065 :
12066 : /* Array pointer. Find the last reference on the LHS and if it is an
12067 : array section ref, we're dealing with bounds remapping. In this case,
12068 : set it to AR_FULL so that gfc_conv_expr_descriptor does
12069 : not see it and process the bounds remapping afterwards explicitly. */
12070 10429 : for (remap = expr1->ref; remap; remap = remap->next)
12071 6153 : if (!remap->next && remap->type == REF_ARRAY
12072 4659 : && remap->u.ar.type == AR_SECTION)
12073 : break;
12074 4659 : rank_remap = (remap && remap->u.ar.end[0]);
12075 :
12076 383 : if (remap && expr2->expr_type == EXPR_NULL)
12077 : {
12078 2 : gfc_error ("If bounds remapping is specified at %L, "
12079 : "the pointer target shall not be NULL", &expr1->where);
12080 2 : return NULL_TREE;
12081 : }
12082 :
12083 4657 : gfc_init_se (&lse, NULL);
12084 4657 : if (remap)
12085 381 : lse.descriptor_only = 1;
12086 4657 : gfc_conv_expr_descriptor (&lse, expr1);
12087 4657 : strlen_lhs = lse.string_length;
12088 4657 : desc = lse.expr;
12089 :
12090 4657 : if (expr2->expr_type == EXPR_NULL)
12091 : {
12092 : /* Just set the data pointer to null. */
12093 692 : gfc_nullify_descriptor (&lse.pre, lse.expr);
12094 : }
12095 3965 : else if (rank_remap)
12096 : {
12097 : /* If we are rank-remapping, just get the RHS's descriptor and
12098 : process this later on. */
12099 258 : gfc_init_se (&rse, NULL);
12100 258 : rse.direct_byref = 1;
12101 258 : rse.byref_noassign = 1;
12102 :
12103 258 : if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
12104 12 : expr1_vptr = trans_class_pointer_fcn (&block, &lse, &rse,
12105 : expr1, expr2);
12106 246 : else if (expr2->expr_type == EXPR_FUNCTION)
12107 : {
12108 : tree bound[GFC_MAX_DIMENSIONS];
12109 : int i;
12110 :
12111 26 : for (i = 0; i < expr2->rank; i++)
12112 13 : bound[i] = NULL_TREE;
12113 13 : tmp = gfc_typenode_for_spec (&expr2->ts);
12114 13 : tmp = gfc_get_array_type_bounds (tmp, expr2->rank, 0,
12115 : bound, bound, 0,
12116 : GFC_ARRAY_POINTER_CONT, false);
12117 13 : tmp = gfc_create_var (tmp, "ptrtemp");
12118 13 : rse.descriptor_only = 0;
12119 13 : rse.expr = tmp;
12120 13 : rse.direct_byref = 1;
12121 13 : gfc_conv_expr_descriptor (&rse, expr2);
12122 13 : strlen_rhs = rse.string_length;
12123 13 : rse.expr = tmp;
12124 : }
12125 : else
12126 : {
12127 233 : gfc_conv_expr_descriptor (&rse, expr2);
12128 233 : strlen_rhs = rse.string_length;
12129 233 : if (expr1->ts.type == BT_CLASS)
12130 64 : expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
12131 : expr2, &rse,
12132 : NULL, NULL,
12133 : NULL);
12134 : }
12135 : }
12136 3707 : else if (expr2->expr_type == EXPR_VARIABLE)
12137 : {
12138 : /* Assign directly to the LHS's descriptor. */
12139 3557 : lse.descriptor_only = 0;
12140 3557 : lse.direct_byref = 1;
12141 3557 : gfc_conv_expr_descriptor (&lse, expr2);
12142 3557 : strlen_rhs = lse.string_length;
12143 3557 : gfc_init_se (&rse, NULL);
12144 :
12145 3557 : if (expr1->ts.type == BT_CLASS)
12146 : {
12147 477 : rse.expr = NULL_TREE;
12148 477 : rse.string_length = strlen_rhs;
12149 477 : trans_class_vptr_len_assignment (&block, expr1, expr2, &rse,
12150 : NULL, NULL, NULL);
12151 : }
12152 :
12153 3557 : if (remap == NULL)
12154 : {
12155 : /* If the target is not a whole array, use the target array
12156 : reference for remap. */
12157 7216 : for (remap = expr2->ref; remap; remap = remap->next)
12158 4040 : if (remap->type == REF_ARRAY
12159 3494 : && remap->u.ar.type == AR_FULL
12160 2795 : && remap->next)
12161 : break;
12162 : }
12163 : }
12164 150 : else if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
12165 : {
12166 31 : gfc_init_se (&rse, NULL);
12167 31 : rse.want_pointer = 1;
12168 31 : gfc_conv_function_expr (&rse, expr2);
12169 31 : if (expr1->ts.type != BT_CLASS)
12170 : {
12171 12 : rse.expr = gfc_class_data_get (rse.expr);
12172 12 : gfc_add_modify (&lse.pre, desc, rse.expr);
12173 : }
12174 : else
12175 : {
12176 19 : expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
12177 : expr2, &rse, NULL,
12178 : NULL, NULL);
12179 19 : gfc_add_block_to_block (&block, &rse.pre);
12180 19 : tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
12181 19 : gfc_add_modify (&lse.pre, tmp, rse.expr);
12182 :
12183 38 : gfc_add_modify (&lse.pre, expr1_vptr,
12184 19 : fold_convert (TREE_TYPE (expr1_vptr),
12185 : gfc_class_vptr_get (tmp)));
12186 19 : rse.expr = gfc_class_data_get (tmp);
12187 19 : gfc_add_modify (&lse.pre, desc, rse.expr);
12188 : }
12189 : }
12190 : else
12191 : {
12192 : /* Assign to a temporary descriptor and then copy that
12193 : temporary to the pointer. */
12194 119 : tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
12195 119 : lse.descriptor_only = 0;
12196 119 : lse.expr = tmp;
12197 119 : lse.direct_byref = 1;
12198 119 : gfc_conv_expr_descriptor (&lse, expr2);
12199 119 : strlen_rhs = lse.string_length;
12200 119 : gfc_add_modify (&lse.pre, desc, tmp);
12201 : }
12202 :
12203 4657 : if (expr1->ts.type == BT_CHARACTER
12204 620 : && expr1->ts.deferred)
12205 : {
12206 344 : gfc_symbol *psym = expr1->symtree->n.sym;
12207 344 : tmp = NULL_TREE;
12208 344 : if (psym->ts.type == BT_CHARACTER
12209 343 : && psym->ts.u.cl->backend_decl)
12210 343 : tmp = psym->ts.u.cl->backend_decl;
12211 1 : else if (expr1->ts.u.cl->backend_decl
12212 1 : && VAR_P (expr1->ts.u.cl->backend_decl))
12213 0 : tmp = expr1->ts.u.cl->backend_decl;
12214 1 : else if (TREE_CODE (lse.expr) == COMPONENT_REF)
12215 : {
12216 1 : gfc_ref *ref = expr1->ref;
12217 3 : for (;ref; ref = ref->next)
12218 : {
12219 2 : if (ref->type == REF_COMPONENT
12220 1 : && ref->u.c.component->ts.type == BT_CHARACTER
12221 3 : && gfc_deferred_strlen (ref->u.c.component, &tmp))
12222 1 : tmp = fold_build3_loc (input_location, COMPONENT_REF,
12223 1 : TREE_TYPE (tmp),
12224 1 : TREE_OPERAND (lse.expr, 0),
12225 : tmp, NULL_TREE);
12226 : }
12227 : }
12228 :
12229 344 : gcc_assert (tmp);
12230 :
12231 344 : if (expr2->expr_type != EXPR_NULL)
12232 332 : gfc_add_modify (&block, tmp,
12233 332 : fold_convert (TREE_TYPE (tmp), strlen_rhs));
12234 : else
12235 12 : gfc_add_modify (&block, tmp, build_zero_cst (TREE_TYPE (tmp)));
12236 : }
12237 :
12238 4657 : gfc_add_block_to_block (&block, &lse.pre);
12239 4657 : if (rank_remap)
12240 258 : gfc_add_block_to_block (&block, &rse.pre);
12241 :
12242 : /* If we do bounds remapping, update LHS descriptor accordingly. */
12243 4657 : if (remap)
12244 : {
12245 639 : int dim;
12246 639 : gcc_assert (remap->u.ar.dimen == expr1->rank);
12247 :
12248 : /* Always set dtype. */
12249 639 : gfc_conv_descriptor_dtype_set (&block, desc,
12250 639 : gfc_get_dtype (TREE_TYPE (desc)));
12251 :
12252 : /* For unlimited polymorphic LHS use elem_len from RHS. */
12253 639 : if (UNLIMITED_POLY (expr1) && expr2->ts.type != BT_CLASS)
12254 : {
12255 66 : tree elem_len;
12256 66 : tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
12257 66 : elem_len = fold_convert (gfc_array_index_type, tmp);
12258 66 : elem_len = gfc_evaluate_now (elem_len, &block);
12259 66 : gfc_conv_descriptor_elem_len_set (&block, desc, elem_len);
12260 : }
12261 :
12262 639 : if (rank_remap)
12263 : {
12264 : /* Do rank remapping. We already have the RHS's descriptor
12265 : converted in rse and now have to build the correct LHS
12266 : descriptor for it. */
12267 :
12268 258 : tree data, span;
12269 258 : tree offs, stride;
12270 258 : tree lbound, ubound;
12271 :
12272 : /* Copy data pointer. */
12273 258 : data = gfc_conv_descriptor_data_get (rse.expr);
12274 258 : gfc_conv_descriptor_data_set (&block, desc, data);
12275 :
12276 : /* Copy the span. */
12277 258 : if (VAR_P (rse.expr)
12278 258 : && GFC_DECL_PTR_ARRAY_P (rse.expr))
12279 12 : span = gfc_conv_descriptor_span_get (rse.expr);
12280 : else
12281 : {
12282 246 : tmp = TREE_TYPE (rse.expr);
12283 246 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (tmp));
12284 246 : span = fold_convert (gfc_array_index_type, tmp);
12285 : }
12286 258 : gfc_conv_descriptor_span_set (&block, desc, span);
12287 :
12288 : /* Copy offset but adjust it such that it would correspond
12289 : to a lbound of zero. */
12290 258 : if (expr2->rank == -1)
12291 42 : gfc_conv_descriptor_offset_set (&block, desc,
12292 : gfc_index_zero_node);
12293 : else
12294 : {
12295 216 : offs = gfc_conv_descriptor_offset_get (rse.expr);
12296 670 : for (dim = 0; dim < expr2->rank; ++dim)
12297 : {
12298 238 : stride = gfc_conv_descriptor_stride_get (rse.expr,
12299 : gfc_rank_cst[dim]);
12300 238 : lbound = gfc_conv_descriptor_lbound_get (rse.expr,
12301 : gfc_rank_cst[dim]);
12302 238 : tmp = fold_build2_loc (input_location, MULT_EXPR,
12303 : gfc_array_index_type, stride,
12304 : lbound);
12305 238 : offs = fold_build2_loc (input_location, PLUS_EXPR,
12306 : gfc_array_index_type, offs, tmp);
12307 : }
12308 216 : gfc_conv_descriptor_offset_set (&block, desc, offs);
12309 : }
12310 : /* Set the bounds as declared for the LHS and calculate strides as
12311 : well as another offset update accordingly. */
12312 258 : stride = gfc_conv_descriptor_stride_get (rse.expr,
12313 : gfc_rank_cst[0]);
12314 907 : for (dim = 0; dim < expr1->rank; ++dim)
12315 : {
12316 391 : gfc_se lower_se;
12317 391 : gfc_se upper_se;
12318 :
12319 391 : gcc_assert (remap->u.ar.start[dim] && remap->u.ar.end[dim]);
12320 :
12321 391 : if (remap->u.ar.start[dim]->expr_type != EXPR_CONSTANT
12322 391 : && remap->u.ar.start[dim]->expr_type != EXPR_VARIABLE)
12323 0 : gfc_resolve_expr (remap->u.ar.start[dim]);
12324 391 : if (remap->u.ar.end[dim]->expr_type != EXPR_CONSTANT
12325 391 : && remap->u.ar.end[dim]->expr_type != EXPR_VARIABLE)
12326 96 : gfc_resolve_expr (remap->u.ar.end[dim]);
12327 :
12328 : /* Convert declared bounds. */
12329 391 : gfc_init_se (&lower_se, NULL);
12330 391 : gfc_init_se (&upper_se, NULL);
12331 391 : gfc_conv_expr (&lower_se, remap->u.ar.start[dim]);
12332 391 : gfc_conv_expr (&upper_se, remap->u.ar.end[dim]);
12333 :
12334 391 : gfc_add_block_to_block (&block, &lower_se.pre);
12335 391 : gfc_add_block_to_block (&block, &upper_se.pre);
12336 :
12337 391 : lbound = fold_convert (gfc_array_index_type, lower_se.expr);
12338 391 : ubound = fold_convert (gfc_array_index_type, upper_se.expr);
12339 :
12340 391 : lbound = gfc_evaluate_now (lbound, &block);
12341 391 : ubound = gfc_evaluate_now (ubound, &block);
12342 :
12343 391 : gfc_add_block_to_block (&block, &lower_se.post);
12344 391 : gfc_add_block_to_block (&block, &upper_se.post);
12345 :
12346 : /* Set bounds in descriptor. */
12347 391 : gfc_conv_descriptor_lbound_set (&block, desc,
12348 : gfc_rank_cst[dim], lbound);
12349 391 : gfc_conv_descriptor_ubound_set (&block, desc,
12350 : gfc_rank_cst[dim], ubound);
12351 :
12352 : /* Set stride. */
12353 391 : stride = gfc_evaluate_now (stride, &block);
12354 391 : gfc_conv_descriptor_stride_set (&block, desc,
12355 : gfc_rank_cst[dim], stride);
12356 :
12357 : /* Update offset. */
12358 391 : offs = gfc_conv_descriptor_offset_get (desc);
12359 391 : tmp = fold_build2_loc (input_location, MULT_EXPR,
12360 : gfc_array_index_type, lbound, stride);
12361 391 : offs = fold_build2_loc (input_location, MINUS_EXPR,
12362 : gfc_array_index_type, offs, tmp);
12363 391 : offs = gfc_evaluate_now (offs, &block);
12364 391 : gfc_conv_descriptor_offset_set (&block, desc, offs);
12365 :
12366 : /* Update stride. */
12367 391 : tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
12368 391 : stride = fold_build2_loc (input_location, MULT_EXPR,
12369 : gfc_array_index_type, stride, tmp);
12370 : }
12371 : }
12372 : else
12373 : {
12374 : /* Bounds remapping. Just shift the lower bounds. */
12375 :
12376 381 : gcc_assert (expr1->rank == expr2->rank);
12377 :
12378 876 : for (dim = 0; dim < remap->u.ar.dimen; ++dim)
12379 : {
12380 495 : gfc_se lbound_se;
12381 :
12382 495 : gcc_assert (!remap->u.ar.end[dim]);
12383 495 : gfc_init_se (&lbound_se, NULL);
12384 495 : if (remap->u.ar.start[dim])
12385 : {
12386 225 : gfc_conv_expr (&lbound_se, remap->u.ar.start[dim]);
12387 225 : gfc_add_block_to_block (&block, &lbound_se.pre);
12388 : }
12389 : else
12390 : /* This remap arises from a target that is not a whole
12391 : array. The start expressions will be NULL but we need
12392 : the lbounds to be one. */
12393 270 : lbound_se.expr = gfc_index_one_node;
12394 495 : gfc_conv_shift_descriptor_lbound (&block, desc,
12395 : dim, lbound_se.expr);
12396 495 : gfc_add_block_to_block (&block, &lbound_se.post);
12397 : }
12398 : }
12399 : }
12400 :
12401 : /* If rank remapping was done, check with -fcheck=bounds that
12402 : the target is at least as large as the pointer. */
12403 4657 : if (rank_remap && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
12404 72 : && expr2->rank != -1)
12405 : {
12406 54 : tree lsize, rsize;
12407 54 : tree fault;
12408 54 : const char* msg;
12409 :
12410 54 : lsize = gfc_conv_descriptor_size (lse.expr, expr1->rank);
12411 54 : rsize = gfc_conv_descriptor_size (rse.expr, expr2->rank);
12412 :
12413 54 : lsize = gfc_evaluate_now (lsize, &block);
12414 54 : rsize = gfc_evaluate_now (rsize, &block);
12415 54 : fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
12416 : rsize, lsize);
12417 :
12418 54 : msg = _("Target of rank remapping is too small (%ld < %ld)");
12419 54 : gfc_trans_runtime_check (true, false, fault, &block, &expr2->where,
12420 : msg, rsize, lsize);
12421 : }
12422 :
12423 : /* Check string lengths if applicable. The check is only really added
12424 : to the output code if -fbounds-check is enabled. */
12425 4657 : if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
12426 : {
12427 554 : gcc_assert (expr2->ts.type == BT_CHARACTER);
12428 554 : gcc_assert (strlen_lhs && strlen_rhs);
12429 554 : gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
12430 : strlen_lhs, strlen_rhs, &block);
12431 : }
12432 :
12433 4657 : gfc_add_block_to_block (&block, &lse.post);
12434 4657 : if (rank_remap)
12435 258 : gfc_add_block_to_block (&block, &rse.post);
12436 : }
12437 :
12438 10579 : return gfc_finish_block (&block);
12439 : }
12440 :
12441 :
12442 : /* Makes sure se is suitable for passing as a function string parameter. */
12443 : /* TODO: Need to check all callers of this function. It may be abused. */
12444 :
12445 : void
12446 252132 : gfc_conv_string_parameter (gfc_se * se)
12447 : {
12448 252132 : tree type;
12449 :
12450 252132 : if (TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE
12451 252132 : && integer_onep (se->string_length))
12452 : {
12453 697 : se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
12454 697 : return;
12455 : }
12456 :
12457 251435 : if (TREE_CODE (se->expr) == STRING_CST)
12458 : {
12459 104850 : type = TREE_TYPE (TREE_TYPE (se->expr));
12460 104850 : se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
12461 104850 : return;
12462 : }
12463 :
12464 146585 : if (TREE_CODE (se->expr) == COND_EXPR)
12465 : {
12466 62 : tree cond = TREE_OPERAND (se->expr, 0);
12467 62 : tree lhs = TREE_OPERAND (se->expr, 1);
12468 62 : tree rhs = TREE_OPERAND (se->expr, 2);
12469 :
12470 62 : gfc_se lse, rse;
12471 62 : gfc_init_se (&lse, NULL);
12472 62 : gfc_init_se (&rse, NULL);
12473 :
12474 62 : lse.expr = lhs;
12475 62 : lse.string_length = se->string_length;
12476 62 : gfc_conv_string_parameter (&lse);
12477 :
12478 62 : rse.expr = rhs;
12479 62 : rse.string_length = se->string_length;
12480 62 : gfc_conv_string_parameter (&rse);
12481 :
12482 62 : se->expr
12483 62 : = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (lse.expr),
12484 : cond, lse.expr, rse.expr);
12485 : }
12486 :
12487 146585 : if ((TREE_CODE (TREE_TYPE (se->expr)) == ARRAY_TYPE
12488 47545 : || TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE)
12489 146960 : && TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
12490 : {
12491 99415 : type = TREE_TYPE (se->expr);
12492 99415 : if (TREE_CODE (se->expr) != INDIRECT_REF)
12493 84393 : se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
12494 : else
12495 : {
12496 15022 : if (TREE_CODE (type) == ARRAY_TYPE)
12497 14743 : type = TREE_TYPE (type);
12498 15022 : type = gfc_get_character_type_len_for_eltype (type,
12499 : se->string_length);
12500 15022 : type = build_pointer_type (type);
12501 15022 : se->expr = gfc_build_addr_expr (type, se->expr);
12502 : }
12503 : }
12504 :
12505 146585 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
12506 : }
12507 :
12508 :
12509 : /* Generate code for assignment of scalar variables. Includes character
12510 : strings and derived types with allocatable components.
12511 : If you know that the LHS has no allocations, set dealloc to false.
12512 :
12513 : DEEP_COPY has no effect if the typespec TS is not a derived type with
12514 : allocatable components. Otherwise, if it is set, an explicit copy of each
12515 : allocatable component is made. This is necessary as a simple copy of the
12516 : whole object would copy array descriptors as is, so that the lhs's
12517 : allocatable components would point to the rhs's after the assignment.
12518 : Typically, setting DEEP_COPY is necessary if the rhs is a variable, and not
12519 : necessary if the rhs is a non-pointer function, as the allocatable components
12520 : are not accessible by other means than the function's result after the
12521 : function has returned. It is even more subtle when temporaries are involved,
12522 : as the two following examples show:
12523 : 1. When we evaluate an array constructor, a temporary is created. Thus
12524 : there is theoretically no alias possible. However, no deep copy is
12525 : made for this temporary, so that if the constructor is made of one or
12526 : more variable with allocatable components, those components still point
12527 : to the variable's: DEEP_COPY should be set for the assignment from the
12528 : temporary to the lhs in that case.
12529 : 2. When assigning a scalar to an array, we evaluate the scalar value out
12530 : of the loop, store it into a temporary variable, and assign from that.
12531 : In that case, deep copying when assigning to the temporary would be a
12532 : waste of resources; however deep copies should happen when assigning from
12533 : the temporary to each array element: again DEEP_COPY should be set for
12534 : the assignment from the temporary to the lhs. */
12535 :
12536 : tree
12537 353925 : gfc_trans_scalar_assign (gfc_se *lse, gfc_se *rse, gfc_typespec ts,
12538 : bool deep_copy, bool dealloc, bool in_coarray,
12539 : bool assoc_assign)
12540 : {
12541 353925 : stmtblock_t block;
12542 353925 : tree tmp;
12543 353925 : tree cond;
12544 353925 : int caf_mode;
12545 :
12546 353925 : gfc_init_block (&block);
12547 :
12548 353925 : if (ts.type == BT_CHARACTER)
12549 : {
12550 34433 : tree rlen = NULL;
12551 34433 : tree llen = NULL;
12552 :
12553 34433 : if (lse->string_length != NULL_TREE)
12554 : {
12555 34433 : gfc_conv_string_parameter (lse);
12556 34433 : gfc_add_block_to_block (&block, &lse->pre);
12557 34433 : llen = lse->string_length;
12558 : }
12559 :
12560 34433 : if (rse->string_length != NULL_TREE)
12561 : {
12562 34433 : gfc_conv_string_parameter (rse);
12563 34433 : gfc_add_block_to_block (&block, &rse->pre);
12564 34433 : rlen = rse->string_length;
12565 : }
12566 :
12567 34433 : gfc_trans_string_copy (&block, llen, lse->expr, ts.kind, rlen,
12568 : rse->expr, ts.kind);
12569 : }
12570 298273 : else if (gfc_bt_struct (ts.type)
12571 319492 : && (ts.u.derived->attr.alloc_comp
12572 13819 : || (deep_copy && has_parameterized_comps (ts.u.derived))))
12573 : {
12574 7773 : tree tmp_var = NULL_TREE;
12575 7773 : cond = NULL_TREE;
12576 :
12577 : /* Are the rhs and the lhs the same? */
12578 7773 : if (deep_copy)
12579 : {
12580 4750 : if (!TREE_CONSTANT (rse->expr) && !VAR_P (rse->expr))
12581 3485 : rse->expr = gfc_evaluate_now (rse->expr, &rse->pre);
12582 4750 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
12583 : gfc_build_addr_expr (NULL_TREE, lse->expr),
12584 : gfc_build_addr_expr (NULL_TREE, rse->expr));
12585 4750 : cond = gfc_evaluate_now (cond, &lse->pre);
12586 : }
12587 :
12588 : /* Deallocate the lhs allocated components as long as it is not
12589 : the same as the rhs. This must be done following the assignment
12590 : to prevent deallocating data that could be used in the rhs
12591 : expression. */
12592 7773 : if (dealloc)
12593 : {
12594 2190 : tmp_var = gfc_evaluate_now (lse->expr, &lse->pre);
12595 2190 : tmp = gfc_deallocate_alloc_comp_no_caf (ts.u.derived, tmp_var,
12596 : 0, gfc_may_be_finalized (ts));
12597 2190 : if (deep_copy)
12598 914 : tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
12599 : tmp);
12600 2190 : gfc_add_expr_to_block (&lse->post, tmp);
12601 : }
12602 :
12603 7773 : gfc_add_block_to_block (&block, &rse->pre);
12604 :
12605 : /* Skip finalization for self-assignment. */
12606 7773 : if (deep_copy && lse->finalblock.head)
12607 : {
12608 24 : tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
12609 : gfc_finish_block (&lse->finalblock));
12610 24 : gfc_add_expr_to_block (&block, tmp);
12611 : }
12612 : else
12613 7749 : gfc_add_block_to_block (&block, &lse->finalblock);
12614 :
12615 7773 : gfc_add_block_to_block (&block, &lse->pre);
12616 :
12617 7773 : if (TYPE_MAIN_VARIANT (TREE_TYPE (lse->expr))
12618 7773 : == TYPE_MAIN_VARIANT (TREE_TYPE (rse->expr)))
12619 7425 : gfc_add_modify (&block, lse->expr,
12620 7425 : fold_convert (TREE_TYPE (lse->expr), rse->expr));
12621 : else
12622 : {
12623 348 : tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
12624 348 : TREE_TYPE (lse->expr), rse->expr);
12625 348 : gfc_add_modify (&block, lse->expr, tmp);
12626 : }
12627 :
12628 : /* Restore pointer address of coarray components. */
12629 7773 : if (ts.u.derived->attr.coarray_comp && deep_copy && tmp_var != NULL_TREE)
12630 : {
12631 5 : tmp = gfc_reassign_alloc_comp_caf (ts.u.derived, tmp_var, lse->expr);
12632 5 : tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
12633 : tmp);
12634 5 : gfc_add_expr_to_block (&block, tmp);
12635 : }
12636 :
12637 : /* Do a deep copy if the rhs is a variable, if it is not the
12638 : same as the lhs. */
12639 7773 : if (deep_copy)
12640 : {
12641 4750 : caf_mode = in_coarray ? (GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
12642 : | GFC_STRUCTURE_CAF_MODE_IN_COARRAY) : 0;
12643 4750 : tmp = gfc_copy_alloc_comp (ts.u.derived, rse->expr, lse->expr, 0,
12644 : caf_mode);
12645 4750 : tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
12646 : tmp);
12647 4750 : gfc_add_expr_to_block (&block, tmp);
12648 : }
12649 : }
12650 311719 : else if (gfc_bt_struct (ts.type))
12651 : {
12652 13446 : gfc_add_block_to_block (&block, &rse->pre);
12653 13446 : gfc_add_block_to_block (&block, &lse->finalblock);
12654 13446 : gfc_add_block_to_block (&block, &lse->pre);
12655 13446 : tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
12656 13446 : TREE_TYPE (lse->expr), rse->expr);
12657 13446 : gfc_add_modify (&block, lse->expr, tmp);
12658 : }
12659 : /* If possible use the rhs vptr copy with trans_scalar_class_assign.... */
12660 298273 : else if (ts.type == BT_CLASS)
12661 : {
12662 779 : gfc_add_block_to_block (&block, &lse->pre);
12663 779 : gfc_add_block_to_block (&block, &rse->pre);
12664 779 : gfc_add_block_to_block (&block, &lse->finalblock);
12665 :
12666 779 : if (!trans_scalar_class_assign (&block, lse, rse))
12667 : {
12668 : /* ..otherwise assignment suffices. Note the use of VIEW_CONVERT_EXPR
12669 : for the lhs which ensures that class data rhs cast as a string
12670 : assigns correctly. */
12671 597 : tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
12672 597 : TREE_TYPE (rse->expr), lse->expr);
12673 597 : gfc_add_modify (&block, tmp, rse->expr);
12674 :
12675 : /* Copy allocatable components but guard against class pointer
12676 : assign, which arrives here. */
12677 : #define DATA_DT ts.u.derived->components->ts.u.derived
12678 597 : if (deep_copy
12679 152 : && !(GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
12680 0 : && GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
12681 152 : && ts.u.derived->components
12682 749 : && DATA_DT && DATA_DT->attr.alloc_comp)
12683 : {
12684 6 : caf_mode = in_coarray ? (GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
12685 : | GFC_STRUCTURE_CAF_MODE_IN_COARRAY)
12686 : : 0;
12687 6 : tmp = gfc_copy_alloc_comp (DATA_DT, rse->expr, lse->expr, 0,
12688 : caf_mode);
12689 6 : gfc_add_expr_to_block (&block, tmp);
12690 : }
12691 : #undef DATA_DT
12692 : }
12693 : }
12694 297494 : else if (ts.type != BT_CLASS)
12695 : {
12696 297494 : gfc_add_block_to_block (&block, &lse->pre);
12697 297494 : gfc_add_block_to_block (&block, &rse->pre);
12698 :
12699 297494 : if (in_coarray)
12700 : {
12701 956 : if (flag_coarray == GFC_FCOARRAY_LIB && assoc_assign)
12702 : {
12703 0 : tree rtype = TREE_TYPE (TREE_TYPE (rse->expr));
12704 0 : tree rtoken = TYPE_LANG_SPECIFIC (rtype)->caf_token;
12705 0 : gfc_conv_descriptor_token_set (&block, lse->expr, rtoken);
12706 : }
12707 956 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (lse->expr)))
12708 0 : lse->expr = gfc_conv_array_data (lse->expr);
12709 314 : if (flag_coarray == GFC_FCOARRAY_SINGLE && assoc_assign
12710 956 : && !POINTER_TYPE_P (TREE_TYPE (rse->expr)))
12711 0 : rse->expr = gfc_build_addr_expr (NULL_TREE, rse->expr);
12712 : }
12713 297494 : gfc_add_modify (&block, lse->expr,
12714 297494 : fold_convert (TREE_TYPE (lse->expr), rse->expr));
12715 : }
12716 :
12717 353925 : gfc_add_block_to_block (&block, &lse->post);
12718 353925 : gfc_add_block_to_block (&block, &rse->post);
12719 :
12720 353925 : return gfc_finish_block (&block);
12721 : }
12722 :
12723 :
12724 : /* There are quite a lot of restrictions on the optimisation in using an
12725 : array function assign without a temporary. */
12726 :
12727 : static bool
12728 14628 : arrayfunc_assign_needs_temporary (gfc_expr * expr1, gfc_expr * expr2)
12729 : {
12730 14628 : gfc_ref * ref;
12731 14628 : bool seen_array_ref;
12732 14628 : bool c = false;
12733 14628 : gfc_symbol *sym = expr1->symtree->n.sym;
12734 :
12735 : /* Play it safe with class functions assigned to a derived type. */
12736 14628 : if (gfc_is_class_array_function (expr2)
12737 14628 : && expr1->ts.type == BT_DERIVED)
12738 : return true;
12739 :
12740 : /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
12741 14598 : if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
12742 : return true;
12743 :
12744 : /* Elemental functions are scalarized so that they don't need a
12745 : temporary in gfc_trans_assignment_1, so return a true. Otherwise,
12746 : they would need special treatment in gfc_trans_arrayfunc_assign. */
12747 8597 : if (expr2->value.function.esym != NULL
12748 1595 : && expr2->value.function.esym->attr.elemental)
12749 : return true;
12750 :
12751 : /* Need a temporary if rhs is not FULL or a contiguous section. */
12752 8232 : if (expr1->ref && !(gfc_full_array_ref_p (expr1->ref, &c) || c))
12753 : return true;
12754 :
12755 : /* Need a temporary if EXPR1 can't be expressed as a descriptor. */
12756 7982 : if (gfc_ref_needs_temporary_p (expr1->ref))
12757 : return true;
12758 :
12759 : /* Functions returning pointers or allocatables need temporaries. */
12760 7970 : if (gfc_expr_attr (expr2).pointer
12761 7970 : || gfc_expr_attr (expr2).allocatable)
12762 : return true;
12763 :
12764 : /* Character array functions need temporaries unless the
12765 : character lengths are the same. */
12766 7594 : if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
12767 : {
12768 598 : if (UNLIMITED_POLY (expr1))
12769 : return true;
12770 :
12771 592 : if (expr1->ts.u.cl->length == NULL
12772 537 : || expr1->ts.u.cl->length->expr_type != EXPR_CONSTANT)
12773 : return true;
12774 :
12775 505 : if (expr2->ts.u.cl->length == NULL
12776 499 : || expr2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
12777 : return true;
12778 :
12779 487 : if (mpz_cmp (expr1->ts.u.cl->length->value.integer,
12780 487 : expr2->ts.u.cl->length->value.integer) != 0)
12781 : return true;
12782 : }
12783 :
12784 : /* Check that no LHS component references appear during an array
12785 : reference. This is needed because we do not have the means to
12786 : span any arbitrary stride with an array descriptor. This check
12787 : is not needed for the rhs because the function result has to be
12788 : a complete type. */
12789 7477 : seen_array_ref = false;
12790 14954 : for (ref = expr1->ref; ref; ref = ref->next)
12791 : {
12792 7490 : if (ref->type == REF_ARRAY)
12793 : seen_array_ref= true;
12794 13 : else if (ref->type == REF_COMPONENT && seen_array_ref)
12795 : return true;
12796 : }
12797 :
12798 : /* Check for a dependency. */
12799 7464 : if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
12800 : expr2->value.function.esym,
12801 : expr2->value.function.actual,
12802 : NOT_ELEMENTAL))
12803 : return true;
12804 :
12805 : /* If we have reached here with an intrinsic function, we do not
12806 : need a temporary except in the particular case that reallocation
12807 : on assignment is active and the lhs is allocatable and a target,
12808 : or a pointer which may be a subref pointer. FIXME: The last
12809 : condition can go away when we use span in the intrinsics
12810 : directly.*/
12811 7027 : if (expr2->value.function.isym)
12812 6143 : return (flag_realloc_lhs && sym->attr.allocatable && sym->attr.target)
12813 12340 : || (sym->attr.pointer && sym->attr.subref_array_pointer);
12814 :
12815 : /* If the LHS is a dummy, we need a temporary if it is not
12816 : INTENT(OUT). */
12817 809 : if (sym->attr.dummy && sym->attr.intent != INTENT_OUT)
12818 : return true;
12819 :
12820 : /* If the lhs has been host_associated, is in common, a pointer or is
12821 : a target and the function is not using a RESULT variable, aliasing
12822 : can occur and a temporary is needed. */
12823 803 : if ((sym->attr.host_assoc
12824 749 : || sym->attr.in_common
12825 743 : || sym->attr.pointer
12826 737 : || sym->attr.cray_pointee
12827 737 : || sym->attr.target)
12828 66 : && expr2->symtree != NULL
12829 66 : && expr2->symtree->n.sym == expr2->symtree->n.sym->result)
12830 : return true;
12831 :
12832 : /* A PURE function can unconditionally be called without a temporary. */
12833 761 : if (expr2->value.function.esym != NULL
12834 730 : && expr2->value.function.esym->attr.pure)
12835 : return false;
12836 :
12837 : /* Implicit_pure functions are those which could legally be declared
12838 : to be PURE. */
12839 733 : if (expr2->value.function.esym != NULL
12840 702 : && expr2->value.function.esym->attr.implicit_pure)
12841 : return false;
12842 :
12843 450 : if (!sym->attr.use_assoc
12844 450 : && !sym->attr.in_common
12845 450 : && !sym->attr.pointer
12846 444 : && !sym->attr.target
12847 444 : && !sym->attr.cray_pointee
12848 444 : && expr2->value.function.esym)
12849 : {
12850 : /* A temporary is not needed if the function is not contained and
12851 : the variable is local or host associated and not a pointer or
12852 : a target. */
12853 413 : if (!expr2->value.function.esym->attr.contained)
12854 : return false;
12855 :
12856 : /* A temporary is not needed if the lhs has never been host
12857 : associated and the procedure is contained. */
12858 164 : else if (!sym->attr.host_assoc)
12859 : return false;
12860 :
12861 : /* A temporary is not needed if the variable is local and not
12862 : a pointer, a target or a result. */
12863 6 : if (sym->ns->parent
12864 0 : && expr2->value.function.esym->ns == sym->ns->parent)
12865 0 : return false;
12866 : }
12867 :
12868 : /* Default to temporary use. */
12869 : return true;
12870 : }
12871 :
12872 :
12873 : /* Provide the loop info so that the lhs descriptor can be built for
12874 : reallocatable assignments from extrinsic function calls. */
12875 :
12876 : static void
12877 203 : realloc_lhs_loop_for_fcn_call (gfc_se *se, locus *where, gfc_ss **ss,
12878 : gfc_loopinfo *loop)
12879 : {
12880 : /* Signal that the function call should not be made by
12881 : gfc_conv_loop_setup. */
12882 203 : se->ss->is_alloc_lhs = 1;
12883 203 : gfc_init_loopinfo (loop);
12884 203 : gfc_add_ss_to_loop (loop, *ss);
12885 203 : gfc_add_ss_to_loop (loop, se->ss);
12886 203 : gfc_conv_ss_startstride (loop);
12887 203 : gfc_conv_loop_setup (loop, where);
12888 203 : gfc_copy_loopinfo_to_se (se, loop);
12889 203 : gfc_add_block_to_block (&se->pre, &loop->pre);
12890 203 : gfc_add_block_to_block (&se->pre, &loop->post);
12891 203 : se->ss->is_alloc_lhs = 0;
12892 203 : }
12893 :
12894 :
12895 : /* For assignment to a reallocatable lhs from intrinsic functions,
12896 : replace the se.expr (ie. the result) with a temporary descriptor.
12897 : Null the data field so that the library allocates space for the
12898 : result. Free the data of the original descriptor after the function,
12899 : in case it appears in an argument expression and transfer the
12900 : result to the original descriptor. */
12901 :
12902 : static void
12903 2143 : fcncall_realloc_result (gfc_se *se, int rank, tree dtype)
12904 : {
12905 2143 : tree desc;
12906 2143 : tree res_desc;
12907 2143 : tree tmp;
12908 2143 : tree offset;
12909 2143 : tree zero_cond;
12910 2143 : tree not_same_shape;
12911 2143 : stmtblock_t shape_block;
12912 2143 : int n;
12913 :
12914 : /* Use the allocation done by the library. Substitute the lhs
12915 : descriptor with a copy, whose data field is nulled.*/
12916 2143 : desc = build_fold_indirect_ref_loc (input_location, se->expr);
12917 2143 : if (POINTER_TYPE_P (TREE_TYPE (desc)))
12918 9 : desc = build_fold_indirect_ref_loc (input_location, desc);
12919 :
12920 2143 : res_desc = gfc_create_unallocated_library_result_descriptor (&se->pre, desc,
12921 : dtype);
12922 2143 : se->expr = gfc_build_addr_expr (NULL_TREE, res_desc);
12923 :
12924 : /* Free the lhs after the function call and copy the result data to
12925 : the lhs descriptor. */
12926 2143 : tmp = gfc_conv_descriptor_data_get (desc);
12927 2143 : zero_cond = fold_build2_loc (input_location, EQ_EXPR,
12928 : logical_type_node, tmp,
12929 2143 : build_int_cst (TREE_TYPE (tmp), 0));
12930 2143 : zero_cond = gfc_evaluate_now (zero_cond, &se->post);
12931 2143 : tmp = gfc_call_free (tmp);
12932 2143 : gfc_add_expr_to_block (&se->post, tmp);
12933 :
12934 2143 : tmp = gfc_conv_descriptor_data_get (res_desc);
12935 2143 : gfc_conv_descriptor_data_set (&se->post, desc, tmp);
12936 :
12937 : /* Check that the shapes are the same between lhs and expression.
12938 : The evaluation of the shape is done in 'shape_block' to avoid
12939 : uninitialized warnings from the lhs bounds. */
12940 2143 : not_same_shape = boolean_false_node;
12941 2143 : gfc_start_block (&shape_block);
12942 9039 : for (n = 0 ; n < rank; n++)
12943 : {
12944 4753 : tree tmp1;
12945 4753 : tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
12946 4753 : tmp1 = gfc_conv_descriptor_lbound_get (res_desc, gfc_rank_cst[n]);
12947 4753 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
12948 : gfc_array_index_type, tmp, tmp1);
12949 4753 : tmp1 = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
12950 4753 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
12951 : gfc_array_index_type, tmp, tmp1);
12952 4753 : tmp1 = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
12953 4753 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
12954 : gfc_array_index_type, tmp, tmp1);
12955 4753 : tmp = fold_build2_loc (input_location, NE_EXPR,
12956 : logical_type_node, tmp,
12957 : gfc_index_zero_node);
12958 4753 : tmp = gfc_evaluate_now (tmp, &shape_block);
12959 4753 : if (n == 0)
12960 : not_same_shape = tmp;
12961 : else
12962 2610 : not_same_shape = fold_build2_loc (input_location, TRUTH_OR_EXPR,
12963 : logical_type_node, tmp,
12964 : not_same_shape);
12965 : }
12966 :
12967 : /* 'zero_cond' being true is equal to lhs not being allocated or the
12968 : shapes being different. */
12969 2143 : tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR, logical_type_node,
12970 : zero_cond, not_same_shape);
12971 2143 : gfc_add_modify (&shape_block, zero_cond, tmp);
12972 2143 : tmp = gfc_finish_block (&shape_block);
12973 2143 : tmp = build3_v (COND_EXPR, zero_cond,
12974 : build_empty_stmt (input_location), tmp);
12975 2143 : gfc_add_expr_to_block (&se->post, tmp);
12976 :
12977 : /* Now reset the bounds returned from the function call to bounds based
12978 : on the lhs lbounds, except where the lhs is not allocated or the shapes
12979 : of 'variable and 'expr' are different. Set the offset accordingly. */
12980 2143 : offset = gfc_index_zero_node;
12981 6896 : for (n = 0 ; n < rank; n++)
12982 : {
12983 4753 : tree lbound;
12984 :
12985 4753 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
12986 4753 : lbound = fold_build3_loc (input_location, COND_EXPR,
12987 : gfc_array_index_type, zero_cond,
12988 : gfc_index_one_node, lbound);
12989 4753 : lbound = gfc_evaluate_now (lbound, &se->post);
12990 :
12991 4753 : tmp = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
12992 4753 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
12993 : gfc_array_index_type, tmp, lbound);
12994 4753 : gfc_conv_descriptor_lbound_set (&se->post, desc,
12995 : gfc_rank_cst[n], lbound);
12996 4753 : gfc_conv_descriptor_ubound_set (&se->post, desc,
12997 : gfc_rank_cst[n], tmp);
12998 :
12999 : /* Set stride and accumulate the offset. */
13000 4753 : tmp = gfc_conv_descriptor_stride_get (res_desc, gfc_rank_cst[n]);
13001 4753 : gfc_conv_descriptor_stride_set (&se->post, desc,
13002 : gfc_rank_cst[n], tmp);
13003 4753 : tmp = fold_build2_loc (input_location, MULT_EXPR,
13004 : gfc_array_index_type, lbound, tmp);
13005 4753 : offset = fold_build2_loc (input_location, MINUS_EXPR,
13006 : gfc_array_index_type, offset, tmp);
13007 4753 : offset = gfc_evaluate_now (offset, &se->post);
13008 : }
13009 :
13010 2143 : gfc_conv_descriptor_offset_set (&se->post, desc, offset);
13011 2143 : }
13012 :
13013 :
13014 :
13015 : /* Try to translate array(:) = func (...), where func is a transformational
13016 : array function, without using a temporary. Returns NULL if this isn't the
13017 : case. */
13018 :
13019 : static tree
13020 14668 : gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
13021 : {
13022 14668 : gfc_se se;
13023 14668 : gfc_ss *ss = NULL;
13024 14668 : gfc_component *comp = NULL;
13025 14668 : gfc_loopinfo loop;
13026 14668 : tree tmp;
13027 14668 : tree lhs;
13028 14668 : gfc_se final_se;
13029 14668 : gfc_symbol *sym = expr1->symtree->n.sym;
13030 14668 : bool finalizable = gfc_may_be_finalized (expr1->ts);
13031 :
13032 : /* If the symbol is host associated and has not been referenced in its name
13033 : space, it might be lacking a backend_decl and vtable. */
13034 14668 : if (sym->backend_decl == NULL_TREE)
13035 : return NULL_TREE;
13036 :
13037 14628 : if (arrayfunc_assign_needs_temporary (expr1, expr2))
13038 : return NULL_TREE;
13039 :
13040 : /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
13041 : functions. */
13042 6903 : comp = gfc_get_proc_ptr_comp (expr2);
13043 :
13044 6903 : if (!(expr2->value.function.isym
13045 718 : || (comp && comp->attr.dimension)
13046 718 : || (!comp && gfc_return_by_reference (expr2->value.function.esym)
13047 718 : && expr2->value.function.esym->result->attr.dimension)))
13048 : return NULL_TREE;
13049 :
13050 6903 : gfc_init_se (&se, NULL);
13051 6903 : gfc_start_block (&se.pre);
13052 6903 : se.want_pointer = 1;
13053 :
13054 : /* First the lhs must be finalized, if necessary. We use a copy of the symbol
13055 : backend decl, stash the original away for the finalization so that the
13056 : value used is that before the assignment. This is necessary because
13057 : evaluation of the rhs expression using direct by reference can change
13058 : the value. However, the standard mandates that the finalization must occur
13059 : after evaluation of the rhs. */
13060 6903 : gfc_init_se (&final_se, NULL);
13061 :
13062 6903 : if (finalizable)
13063 : {
13064 45 : tmp = sym->backend_decl;
13065 45 : lhs = sym->backend_decl;
13066 45 : if (INDIRECT_REF_P (tmp))
13067 0 : tmp = TREE_OPERAND (tmp, 0);
13068 45 : sym->backend_decl = gfc_create_var (TREE_TYPE (tmp), "lhs");
13069 45 : gfc_add_modify (&se.pre, sym->backend_decl, tmp);
13070 45 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
13071 : {
13072 0 : tmp = gfc_copy_alloc_comp (expr1->ts.u.derived, tmp, sym->backend_decl,
13073 : expr1->rank, 0);
13074 0 : gfc_add_expr_to_block (&final_se.pre, tmp);
13075 : }
13076 : }
13077 :
13078 45 : if (finalizable && gfc_assignment_finalizer_call (&final_se, expr1, false))
13079 : {
13080 45 : gfc_add_block_to_block (&se.pre, &final_se.pre);
13081 45 : gfc_add_block_to_block (&se.post, &final_se.finalblock);
13082 : }
13083 :
13084 6903 : if (finalizable)
13085 45 : sym->backend_decl = lhs;
13086 :
13087 6903 : gfc_conv_array_parameter (&se, expr1, false, NULL, NULL, NULL);
13088 :
13089 6903 : if (expr1->ts.type == BT_DERIVED
13090 288 : && expr1->ts.u.derived->attr.alloc_comp)
13091 : {
13092 110 : tmp = build_fold_indirect_ref_loc (input_location, se.expr);
13093 110 : tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, tmp,
13094 : expr1->rank);
13095 110 : gfc_add_expr_to_block (&se.pre, tmp);
13096 : }
13097 6793 : else if (expr1->ts.type == BT_DERIVED
13098 178 : && has_parameterized_comps (expr1->ts.u.derived) && expr1->rank > 0
13099 24 : && expr2->value.function.isym
13100 24 : && expr2->value.function.isym->transformational
13101 6817 : && expr2->value.function.isym->id != GFC_ISYM_REDUCE)
13102 : {
13103 24 : tmp = build_fold_indirect_ref_loc (input_location, se.expr);
13104 24 : tmp = gfc_deallocate_pdt_comp (expr1->ts.u.derived, tmp, expr1->rank);
13105 24 : gfc_add_expr_to_block (&se.pre, tmp);
13106 : }
13107 :
13108 6903 : se.direct_byref = 1;
13109 6903 : se.ss = gfc_walk_expr (expr2);
13110 6903 : gcc_assert (se.ss != gfc_ss_terminator);
13111 :
13112 : /* Since this is a direct by reference call, references to the lhs can be
13113 : used for finalization of the function result just as long as the blocks
13114 : from final_se are added at the right time. */
13115 6903 : gfc_init_se (&final_se, NULL);
13116 6903 : if (finalizable && expr2->value.function.esym)
13117 : {
13118 32 : final_se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
13119 32 : gfc_finalize_tree_expr (&final_se, expr2->ts.u.derived,
13120 32 : expr2->value.function.esym->attr,
13121 : expr2->rank);
13122 : }
13123 :
13124 : /* Reallocate on assignment needs the loopinfo for extrinsic functions.
13125 : This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
13126 : Clearly, this cannot be done for an allocatable function result, since
13127 : the shape of the result is unknown and, in any case, the function must
13128 : correctly take care of the reallocation internally. For intrinsic
13129 : calls, the array data is freed and the library takes care of allocation.
13130 : TODO: Add logic of trans-array.cc: gfc_alloc_allocatable_for_assignment
13131 : to the library. */
13132 6903 : if (flag_realloc_lhs
13133 6828 : && gfc_is_reallocatable_lhs (expr1)
13134 9249 : && !gfc_expr_attr (expr1).codimension
13135 2346 : && !gfc_is_coindexed (expr1)
13136 9249 : && !(expr2->value.function.esym
13137 203 : && expr2->value.function.esym->result->attr.allocatable))
13138 : {
13139 2346 : realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
13140 :
13141 2346 : if (!expr2->value.function.isym)
13142 : {
13143 203 : ss = gfc_walk_expr (expr1);
13144 203 : gcc_assert (ss != gfc_ss_terminator);
13145 :
13146 203 : realloc_lhs_loop_for_fcn_call (&se, &expr1->where, &ss, &loop);
13147 203 : ss->is_alloc_lhs = 1;
13148 : }
13149 : else
13150 : {
13151 2143 : tree dtype = NULL_TREE;
13152 2143 : tree type = gfc_typenode_for_spec (&expr2->ts);
13153 2143 : if (expr1->ts.type == BT_CLASS)
13154 : {
13155 13 : tmp = gfc_class_vptr_get (sym->backend_decl);
13156 13 : tree tmp2 = gfc_get_symbol_decl (gfc_find_vtab (&expr2->ts));
13157 13 : tmp2 = gfc_build_addr_expr (TREE_TYPE (tmp), tmp2);
13158 13 : gfc_add_modify (&se.pre, tmp, tmp2);
13159 13 : dtype = gfc_get_dtype_rank_type (expr1->rank,type);
13160 : }
13161 2143 : fcncall_realloc_result (&se, expr1->rank, dtype);
13162 : }
13163 : }
13164 :
13165 6903 : gfc_conv_function_expr (&se, expr2);
13166 :
13167 : /* Fix the result. */
13168 6903 : gfc_add_block_to_block (&se.pre, &se.post);
13169 6903 : if (finalizable)
13170 45 : gfc_add_block_to_block (&se.pre, &final_se.pre);
13171 :
13172 : /* Do the finalization, including final calls from function arguments. */
13173 45 : if (finalizable)
13174 : {
13175 45 : gfc_add_block_to_block (&se.pre, &final_se.post);
13176 45 : gfc_add_block_to_block (&se.pre, &se.finalblock);
13177 45 : gfc_add_block_to_block (&se.pre, &final_se.finalblock);
13178 : }
13179 :
13180 6903 : if (ss)
13181 203 : gfc_cleanup_loop (&loop);
13182 : else
13183 6700 : gfc_free_ss_chain (se.ss);
13184 :
13185 6903 : return gfc_finish_block (&se.pre);
13186 : }
13187 :
13188 : /* Try to efficiently translate array(:,1) or similar to zero. */
13189 :
13190 : static tree
13191 4176 : gfc_trans_zero_assign (gfc_expr * expr)
13192 : {
13193 4176 : tree dest_decl, len, type, dest;
13194 4176 : tree tmp;
13195 4176 : gfc_symbol *sym;
13196 4176 : stmtblock_t block;
13197 4176 : gfc_se dest_se;
13198 :
13199 4176 : sym = expr->symtree->n.sym;
13200 4176 : dest_decl = gfc_get_symbol_decl (sym);
13201 :
13202 4176 : type = TREE_TYPE (dest_decl);
13203 4176 : if (POINTER_TYPE_P (dest_decl))
13204 0 : type = TREE_TYPE (dest_decl);
13205 :
13206 : /* If we are zeroing a full local array avoid taking its address by emitting
13207 : a = {} instead. */
13208 :
13209 4176 : if (gfc_full_array_ref_p (expr->ref, NULL) && GFC_ARRAY_TYPE_P (type))
13210 : {
13211 2924 : len = GFC_TYPE_ARRAY_SIZE (type);
13212 2924 : if (len != NULL_TREE && TREE_CODE (len) == INTEGER_CST
13213 5732 : && !POINTER_TYPE_P (TREE_TYPE (dest_decl)))
13214 2766 : return build2_loc (input_location, MODIFY_EXPR, void_type_node,
13215 2766 : dest_decl, build_constructor (TREE_TYPE (dest_decl),
13216 2766 : NULL));
13217 : }
13218 :
13219 1410 : gfc_init_block (&block);
13220 1410 : gfc_init_se (&dest_se, NULL);
13221 1410 : gfc_conv_expr_descriptor (&dest_se, expr);
13222 1410 : gfc_add_block_to_block (&block, &dest_se.pre);
13223 :
13224 1410 : len = gfc_conv_descriptor_size (dest_se.expr, expr->rank);
13225 :
13226 1410 : if (gfc_full_array_ref_p (expr->ref, NULL) && GFC_ARRAY_TYPE_P (type))
13227 : dest = dest_decl;
13228 : else
13229 1252 : dest = gfc_conv_descriptor_data_get (dest_se.expr);
13230 :
13231 : /* Multiply len by element size. */
13232 1410 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (dest_se.expr)));
13233 1410 : len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
13234 : len, fold_convert (gfc_array_index_type, tmp));
13235 :
13236 : /* Convert arguments to the correct types. */
13237 1410 : dest = fold_convert (pvoid_type_node, dest);
13238 1410 : len = fold_convert (size_type_node, len);
13239 :
13240 : /* Construct call to __builtin_memset. */
13241 :
13242 1410 : tmp = build_call_expr_loc (input_location,
13243 : builtin_decl_explicit (BUILT_IN_MEMSET),
13244 : 3, dest, integer_zero_node, len);
13245 1410 : tmp = fold_convert (void_type_node, tmp);
13246 :
13247 1410 : gfc_add_expr_to_block (&block, tmp);
13248 1410 : gfc_add_block_to_block (&block, &dest_se.post);
13249 :
13250 1410 : return gfc_finish_block (&block);
13251 : }
13252 :
13253 :
13254 : /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
13255 : that constructs the call to __builtin_memcpy. */
13256 :
13257 : tree
13258 8860 : gfc_build_memcpy_call (tree dst, tree src, tree len, bool use_memmove)
13259 : {
13260 8860 : tree tmp;
13261 :
13262 8860 : enum built_in_function func = use_memmove ? BUILT_IN_MEMMOVE : BUILT_IN_MEMCPY;
13263 :
13264 : /* Convert arguments to the correct types. */
13265 8860 : if (!POINTER_TYPE_P (TREE_TYPE (dst)))
13266 7860 : dst = gfc_build_addr_expr (pvoid_type_node, dst);
13267 : else
13268 1000 : dst = fold_convert (pvoid_type_node, dst);
13269 :
13270 8860 : if (!POINTER_TYPE_P (TREE_TYPE (src)))
13271 8065 : src = gfc_build_addr_expr (pvoid_type_node, src);
13272 : else
13273 795 : src = fold_convert (pvoid_type_node, src);
13274 :
13275 8860 : len = fold_convert (size_type_node, len);
13276 :
13277 : /* Construct call to __builtin_memcpy. */
13278 8860 : tmp = build_call_expr_loc (input_location,
13279 : builtin_decl_explicit (func),
13280 : 3, dst, src, len);
13281 8860 : return fold_convert (void_type_node, tmp);
13282 : }
13283 :
13284 : /* Try to efficiently translate array or slice copies. EXPR1 is the
13285 : destination/lhs and EXPR2 is the source/rhs, both are contiguous slices or
13286 : full arrays which have been checked for dependencies. If there is a
13287 : dependency, invoke memmove instad of memcpy. */
13288 :
13289 : static tree
13290 2114 : gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
13291 : {
13292 2114 : gfc_se dst_se, src_se;
13293 2114 : stmtblock_t block;
13294 2114 : tree dst, src, dlen;
13295 2114 : tree call;
13296 :
13297 2114 : gfc_init_block (&block);
13298 :
13299 2114 : gfc_init_se (&dst_se, NULL);
13300 2114 : gfc_init_se (&src_se, NULL);
13301 :
13302 2114 : gfc_conv_expr_descriptor (&dst_se, expr1);
13303 2114 : gfc_conv_expr_descriptor (&src_se, expr2);
13304 :
13305 2114 : gfc_add_block_to_block (&block, &dst_se.pre);
13306 2114 : gfc_add_block_to_block (&block, &src_se.pre);
13307 :
13308 2114 : dlen = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (dst_se.expr)));
13309 2114 : dlen = fold_convert (gfc_array_index_type, dlen);
13310 :
13311 7428 : for (int dim = 0; dim < expr1->rank; dim++)
13312 : {
13313 3200 : tree lb_src, ub_src, ex_src, lb_dst, ub_dst, ex_dst;
13314 :
13315 3200 : lb_src = gfc_conv_array_lbound (src_se.expr, dim);
13316 3200 : ub_src = gfc_conv_array_ubound (src_se.expr, dim);
13317 3200 : ex_src = gfc_conv_array_extent_dim (lb_src, ub_src, NULL);
13318 :
13319 3200 : lb_dst = gfc_conv_array_lbound (dst_se.expr, dim);
13320 3200 : ub_dst = gfc_conv_array_ubound (dst_se.expr, dim);
13321 3200 : ex_dst = gfc_conv_array_extent_dim (lb_dst, ub_dst, NULL);
13322 :
13323 3200 : dlen = fold_build2_loc (input_location, MULT_EXPR,
13324 : gfc_array_index_type, dlen,
13325 : fold_convert (gfc_array_index_type, ex_dst));
13326 :
13327 3200 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) != 0)
13328 : {
13329 115 : tree cond;
13330 115 : char *msg;
13331 :
13332 115 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
13333 : ex_dst, ex_src);
13334 230 : msg = xasprintf ("Array bound mismatch for dimension %d "
13335 : "of array '%s' (%%ld/%%ld)", dim + 1,
13336 115 : expr1->symtree->name);
13337 115 : gfc_trans_runtime_check (true, false, cond, &block, &expr1->where, msg,
13338 : fold_convert (long_integer_type_node, ex_src),
13339 : fold_convert (long_integer_type_node, ex_dst));
13340 : }
13341 : }
13342 :
13343 : /* It is easier on the optimizers to pass a pointer to the full array if
13344 : possible than the data pointer from the descriptors. Otherwise,
13345 : pr56049.f90 would regress. */
13346 :
13347 2114 : tree dst_decl = gfc_get_symbol_decl (expr1->symtree->n.sym);
13348 2114 : tree src_decl = gfc_get_symbol_decl (expr2->symtree->n.sym);
13349 :
13350 2114 : tree dtype = TREE_TYPE (dst_decl);
13351 2114 : if (POINTER_TYPE_P (dtype))
13352 90 : dtype = TREE_TYPE (dtype);
13353 :
13354 2114 : tree stype = TREE_TYPE (src_decl);
13355 2114 : if (POINTER_TYPE_P (stype))
13356 105 : stype = TREE_TYPE (stype);
13357 :
13358 2114 : if (gfc_full_array_ref_p (expr1->ref, NULL) && GFC_ARRAY_TYPE_P (dtype))
13359 : dst = dst_decl;
13360 : else
13361 558 : dst = gfc_conv_descriptor_data_get (dst_se.expr);
13362 :
13363 2114 : if (gfc_full_array_ref_p (expr2->ref, NULL) && GFC_ARRAY_TYPE_P (stype))
13364 : src = src_decl;
13365 : else
13366 233 : src = gfc_conv_descriptor_data_get (src_se.expr);
13367 :
13368 2114 : bool use_memmove = gfc_check_dependency (expr1, expr2, 0);
13369 2114 : call = gfc_build_memcpy_call (dst, src, dlen, use_memmove);
13370 :
13371 2114 : gfc_add_expr_to_block (&block, call);
13372 2114 : gfc_add_block_to_block (&block, &dst_se.post);
13373 2114 : gfc_add_block_to_block (&block, &src_se.post);
13374 :
13375 2114 : return gfc_finish_block (&block);
13376 : }
13377 :
13378 :
13379 : /* Try to efficiently translate array(:) = (/ ... /). Return NULL if
13380 : this can't be done. EXPR1 is the destination/lhs for which
13381 : gfc_full_array_ref_p is true, and EXPR2 is the source/rhs. */
13382 :
13383 : static tree
13384 8386 : gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
13385 : {
13386 8386 : unsigned HOST_WIDE_INT nelem;
13387 8386 : tree dst, dtype;
13388 8386 : tree src, stype;
13389 8386 : tree len;
13390 8386 : tree tmp;
13391 :
13392 8386 : nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
13393 8386 : if (nelem == 0)
13394 : return NULL_TREE;
13395 :
13396 6923 : dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
13397 6923 : dtype = TREE_TYPE (dst);
13398 6923 : if (POINTER_TYPE_P (dtype))
13399 271 : dtype = TREE_TYPE (dtype);
13400 6923 : if (!GFC_ARRAY_TYPE_P (dtype))
13401 : return NULL_TREE;
13402 :
13403 : /* Determine the lengths of the array. */
13404 6063 : len = GFC_TYPE_ARRAY_SIZE (dtype);
13405 6063 : if (!len || TREE_CODE (len) != INTEGER_CST)
13406 : return NULL_TREE;
13407 :
13408 : /* Confirm that the constructor is the same size. */
13409 5959 : if (compare_tree_int (len, nelem) != 0)
13410 : return NULL_TREE;
13411 :
13412 5959 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
13413 5959 : len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
13414 : fold_convert (gfc_array_index_type, tmp));
13415 :
13416 5959 : stype = gfc_typenode_for_spec (&expr2->ts);
13417 5959 : src = gfc_build_constant_array_constructor (expr2, stype);
13418 :
13419 5959 : return gfc_build_memcpy_call (dst, src, len);
13420 : }
13421 :
13422 :
13423 : /* Tells whether the expression is to be treated as a variable reference. */
13424 :
13425 : bool
13426 327648 : gfc_expr_is_variable (gfc_expr *expr)
13427 : {
13428 327938 : gfc_expr *arg;
13429 327938 : gfc_component *comp;
13430 327938 : gfc_symbol *func_ifc;
13431 :
13432 327938 : if (expr->expr_type == EXPR_VARIABLE)
13433 : return true;
13434 :
13435 290933 : arg = gfc_get_noncopying_intrinsic_argument (expr);
13436 290933 : if (arg)
13437 : {
13438 290 : gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
13439 : return gfc_expr_is_variable (arg);
13440 : }
13441 :
13442 : /* A data-pointer-returning function should be considered as a variable
13443 : too. */
13444 290643 : if (expr->expr_type == EXPR_FUNCTION
13445 38759 : && expr->ref == NULL)
13446 : {
13447 38322 : if (expr->value.function.isym != NULL)
13448 : return false;
13449 :
13450 9964 : if (expr->value.function.esym != NULL)
13451 : {
13452 9955 : func_ifc = expr->value.function.esym;
13453 9955 : goto found_ifc;
13454 : }
13455 9 : gcc_assert (expr->symtree);
13456 9 : func_ifc = expr->symtree->n.sym;
13457 9 : goto found_ifc;
13458 : }
13459 :
13460 252321 : comp = gfc_get_proc_ptr_comp (expr);
13461 252321 : if ((expr->expr_type == EXPR_PPC || expr->expr_type == EXPR_FUNCTION)
13462 437 : && comp)
13463 : {
13464 317 : func_ifc = comp->ts.interface;
13465 317 : goto found_ifc;
13466 : }
13467 :
13468 252004 : if (expr->expr_type == EXPR_COMPCALL)
13469 : {
13470 0 : gcc_assert (!expr->value.compcall.tbp->is_generic);
13471 0 : func_ifc = expr->value.compcall.tbp->u.specific->n.sym;
13472 0 : goto found_ifc;
13473 : }
13474 :
13475 : return false;
13476 :
13477 10281 : found_ifc:
13478 10281 : gcc_assert (func_ifc->attr.function
13479 : && func_ifc->result != NULL);
13480 10281 : return func_ifc->result->attr.pointer;
13481 : }
13482 :
13483 :
13484 : /* Is the lhs OK for automatic reallocation? */
13485 :
13486 : static bool
13487 277758 : is_scalar_reallocatable_lhs (gfc_expr *expr)
13488 : {
13489 277758 : gfc_ref * ref;
13490 :
13491 : /* An allocatable variable with no reference. */
13492 277758 : if (expr->symtree->n.sym->attr.allocatable
13493 6955 : && !expr->ref)
13494 : return true;
13495 :
13496 : /* All that can be left are allocatable components. However, we do
13497 : not check for allocatable components here because the expression
13498 : could be an allocatable component of a pointer component. */
13499 274867 : if (expr->symtree->n.sym->ts.type != BT_DERIVED
13500 250118 : && expr->symtree->n.sym->ts.type != BT_CLASS)
13501 : return false;
13502 :
13503 : /* Find an allocatable component ref last. */
13504 43583 : for (ref = expr->ref; ref; ref = ref->next)
13505 17686 : if (ref->type == REF_COMPONENT
13506 13082 : && !ref->next
13507 10016 : && ref->u.c.component->attr.allocatable)
13508 : return true;
13509 :
13510 : return false;
13511 : }
13512 :
13513 :
13514 : /* Allocate or reallocate scalar lhs, as necessary. */
13515 :
13516 : static void
13517 3785 : alloc_scalar_allocatable_for_assignment (stmtblock_t *block,
13518 : tree string_length,
13519 : gfc_expr *expr1,
13520 : gfc_expr *expr2)
13521 :
13522 : {
13523 3785 : tree cond;
13524 3785 : tree tmp;
13525 3785 : tree size;
13526 3785 : tree size_in_bytes;
13527 3785 : tree jump_label1;
13528 3785 : tree jump_label2;
13529 3785 : gfc_se lse;
13530 3785 : gfc_ref *ref;
13531 :
13532 3785 : if (!expr1 || expr1->rank)
13533 0 : return;
13534 :
13535 3785 : if (!expr2 || expr2->rank)
13536 : return;
13537 :
13538 5305 : for (ref = expr1->ref; ref; ref = ref->next)
13539 1520 : if (ref->type == REF_SUBSTRING)
13540 : return;
13541 :
13542 3785 : realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
13543 :
13544 : /* Since this is a scalar lhs, we can afford to do this. That is,
13545 : there is no risk of side effects being repeated. */
13546 3785 : gfc_init_se (&lse, NULL);
13547 3785 : lse.want_pointer = 1;
13548 3785 : gfc_conv_expr (&lse, expr1);
13549 :
13550 3785 : jump_label1 = gfc_build_label_decl (NULL_TREE);
13551 3785 : jump_label2 = gfc_build_label_decl (NULL_TREE);
13552 :
13553 : /* Do the allocation if the lhs is NULL. Otherwise go to label 1. */
13554 3785 : tmp = build_int_cst (TREE_TYPE (lse.expr), 0);
13555 3785 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
13556 : lse.expr, tmp);
13557 3785 : tmp = build3_v (COND_EXPR, cond,
13558 : build1_v (GOTO_EXPR, jump_label1),
13559 : build_empty_stmt (input_location));
13560 3785 : gfc_add_expr_to_block (block, tmp);
13561 :
13562 3785 : if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
13563 : {
13564 : /* Use the rhs string length and the lhs element size. Note that 'size' is
13565 : used below for the string-length comparison, only. */
13566 1590 : size = string_length;
13567 1590 : tmp = TYPE_SIZE_UNIT (gfc_get_char_type (expr1->ts.kind));
13568 3180 : size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
13569 1590 : TREE_TYPE (tmp), tmp,
13570 1590 : fold_convert (TREE_TYPE (tmp), size));
13571 : }
13572 : else
13573 : {
13574 : /* Otherwise use the length in bytes of the rhs. */
13575 2195 : size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr1->ts));
13576 2195 : size_in_bytes = size;
13577 : }
13578 :
13579 3785 : size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
13580 : size_in_bytes, size_one_node);
13581 :
13582 3785 : if (gfc_caf_attr (expr1).codimension && flag_coarray == GFC_FCOARRAY_LIB)
13583 : {
13584 32 : tree caf_decl, token;
13585 32 : gfc_se caf_se;
13586 32 : symbol_attribute attr;
13587 :
13588 32 : gfc_clear_attr (&attr);
13589 32 : gfc_init_se (&caf_se, NULL);
13590 :
13591 32 : caf_decl = gfc_get_tree_for_caf_expr (expr1);
13592 32 : gfc_get_caf_token_offset (&caf_se, &token, NULL, caf_decl, NULL_TREE,
13593 : NULL);
13594 32 : gfc_add_block_to_block (block, &caf_se.pre);
13595 32 : gfc_allocate_allocatable (block, lse.expr, size_in_bytes,
13596 : gfc_build_addr_expr (NULL_TREE, token),
13597 : NULL_TREE, NULL_TREE, NULL_TREE, jump_label1,
13598 : expr1, 1);
13599 : }
13600 3753 : else if (expr1->ts.type == BT_DERIVED
13601 3753 : && (expr1->ts.u.derived->attr.alloc_comp
13602 250 : || has_parameterized_comps (expr1->ts.u.derived)))
13603 : {
13604 152 : tmp = build_call_expr_loc (input_location,
13605 : builtin_decl_explicit (BUILT_IN_CALLOC),
13606 : 2, build_one_cst (size_type_node),
13607 : size_in_bytes);
13608 152 : tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
13609 152 : gfc_add_modify (block, lse.expr, tmp);
13610 : }
13611 : else
13612 : {
13613 3601 : tmp = build_call_expr_loc (input_location,
13614 : builtin_decl_explicit (BUILT_IN_MALLOC),
13615 : 1, size_in_bytes);
13616 3601 : tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
13617 3601 : gfc_add_modify (block, lse.expr, tmp);
13618 : }
13619 :
13620 3785 : if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
13621 : {
13622 : /* Deferred characters need checking for lhs and rhs string
13623 : length. Other deferred parameter variables will have to
13624 : come here too. */
13625 1590 : tmp = build1_v (GOTO_EXPR, jump_label2);
13626 1590 : gfc_add_expr_to_block (block, tmp);
13627 : }
13628 3785 : tmp = build1_v (LABEL_EXPR, jump_label1);
13629 3785 : gfc_add_expr_to_block (block, tmp);
13630 :
13631 : /* For a deferred length character, reallocate if lengths of lhs and
13632 : rhs are different. */
13633 3785 : if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
13634 : {
13635 1590 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
13636 : lse.string_length,
13637 1590 : fold_convert (TREE_TYPE (lse.string_length),
13638 : size));
13639 : /* Jump past the realloc if the lengths are the same. */
13640 1590 : tmp = build3_v (COND_EXPR, cond,
13641 : build1_v (GOTO_EXPR, jump_label2),
13642 : build_empty_stmt (input_location));
13643 1590 : gfc_add_expr_to_block (block, tmp);
13644 1590 : tmp = build_call_expr_loc (input_location,
13645 : builtin_decl_explicit (BUILT_IN_REALLOC),
13646 : 2, fold_convert (pvoid_type_node, lse.expr),
13647 : size_in_bytes);
13648 1590 : tree omp_cond = NULL_TREE;
13649 1590 : if (flag_openmp_allocators)
13650 : {
13651 1 : tree omp_tmp;
13652 1 : omp_cond = gfc_omp_call_is_alloc (lse.expr);
13653 1 : omp_cond = gfc_evaluate_now (omp_cond, block);
13654 :
13655 1 : omp_tmp = builtin_decl_explicit (BUILT_IN_GOMP_REALLOC);
13656 1 : omp_tmp = build_call_expr_loc (input_location, omp_tmp, 4,
13657 : fold_convert (pvoid_type_node,
13658 : lse.expr), size_in_bytes,
13659 : build_zero_cst (ptr_type_node),
13660 : build_zero_cst (ptr_type_node));
13661 1 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
13662 : omp_cond, omp_tmp, tmp);
13663 : }
13664 1590 : tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
13665 1590 : gfc_add_modify (block, lse.expr, tmp);
13666 1590 : if (omp_cond)
13667 1 : gfc_add_expr_to_block (block,
13668 : build3_loc (input_location, COND_EXPR,
13669 : void_type_node, omp_cond,
13670 : gfc_omp_call_add_alloc (lse.expr),
13671 : build_empty_stmt (input_location)));
13672 1590 : tmp = build1_v (LABEL_EXPR, jump_label2);
13673 1590 : gfc_add_expr_to_block (block, tmp);
13674 :
13675 : /* Update the lhs character length. */
13676 1590 : size = string_length;
13677 1590 : gfc_add_modify (block, lse.string_length,
13678 1590 : fold_convert (TREE_TYPE (lse.string_length), size));
13679 : }
13680 : }
13681 :
13682 : /* Check for assignments of the type
13683 :
13684 : a = a + 4
13685 :
13686 : to make sure we do not check for reallocation unnecessarily. */
13687 :
13688 :
13689 : /* Strip parentheses from an expression to get the underlying variable.
13690 : This is needed for self-assignment detection since (a) creates a
13691 : parentheses operator node. */
13692 :
13693 : static gfc_expr *
13694 8217 : strip_parentheses (gfc_expr *expr)
13695 : {
13696 0 : while (expr->expr_type == EXPR_OP
13697 328808 : && expr->value.op.op == INTRINSIC_PARENTHESES)
13698 605 : expr = expr->value.op.op1;
13699 327532 : return expr;
13700 : }
13701 :
13702 :
13703 : static bool
13704 7740 : is_runtime_conformable (gfc_expr *expr1, gfc_expr *expr2)
13705 : {
13706 8217 : gfc_actual_arglist *a;
13707 8217 : gfc_expr *e1, *e2;
13708 :
13709 : /* Strip parentheses to handle cases like a = (a). */
13710 16485 : expr1 = strip_parentheses (expr1);
13711 8217 : expr2 = strip_parentheses (expr2);
13712 :
13713 8217 : switch (expr2->expr_type)
13714 : {
13715 2244 : case EXPR_VARIABLE:
13716 2244 : return gfc_dep_compare_expr (expr1, expr2) == 0;
13717 :
13718 2875 : case EXPR_FUNCTION:
13719 2875 : if (expr2->value.function.esym
13720 311 : && expr2->value.function.esym->attr.elemental)
13721 : {
13722 75 : for (a = expr2->value.function.actual; a != NULL; a = a->next)
13723 : {
13724 74 : e1 = a->expr;
13725 74 : if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
13726 : return false;
13727 : }
13728 : return true;
13729 : }
13730 2813 : else if (expr2->value.function.isym
13731 2550 : && expr2->value.function.isym->elemental)
13732 : {
13733 332 : for (a = expr2->value.function.actual; a != NULL; a = a->next)
13734 : {
13735 322 : e1 = a->expr;
13736 322 : if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
13737 : return false;
13738 : }
13739 : return true;
13740 : }
13741 :
13742 : break;
13743 :
13744 671 : case EXPR_OP:
13745 671 : switch (expr2->value.op.op)
13746 : {
13747 19 : case INTRINSIC_NOT:
13748 19 : case INTRINSIC_UPLUS:
13749 19 : case INTRINSIC_UMINUS:
13750 19 : case INTRINSIC_PARENTHESES:
13751 19 : return is_runtime_conformable (expr1, expr2->value.op.op1);
13752 :
13753 627 : case INTRINSIC_PLUS:
13754 627 : case INTRINSIC_MINUS:
13755 627 : case INTRINSIC_TIMES:
13756 627 : case INTRINSIC_DIVIDE:
13757 627 : case INTRINSIC_POWER:
13758 627 : case INTRINSIC_AND:
13759 627 : case INTRINSIC_OR:
13760 627 : case INTRINSIC_EQV:
13761 627 : case INTRINSIC_NEQV:
13762 627 : case INTRINSIC_EQ:
13763 627 : case INTRINSIC_NE:
13764 627 : case INTRINSIC_GT:
13765 627 : case INTRINSIC_GE:
13766 627 : case INTRINSIC_LT:
13767 627 : case INTRINSIC_LE:
13768 627 : case INTRINSIC_EQ_OS:
13769 627 : case INTRINSIC_NE_OS:
13770 627 : case INTRINSIC_GT_OS:
13771 627 : case INTRINSIC_GE_OS:
13772 627 : case INTRINSIC_LT_OS:
13773 627 : case INTRINSIC_LE_OS:
13774 :
13775 627 : e1 = expr2->value.op.op1;
13776 627 : e2 = expr2->value.op.op2;
13777 :
13778 627 : if (e1->rank == 0 && e2->rank > 0)
13779 : return is_runtime_conformable (expr1, e2);
13780 569 : else if (e1->rank > 0 && e2->rank == 0)
13781 : return is_runtime_conformable (expr1, e1);
13782 169 : else if (e1->rank > 0 && e2->rank > 0)
13783 169 : return is_runtime_conformable (expr1, e1)
13784 169 : && is_runtime_conformable (expr1, e2);
13785 : break;
13786 :
13787 : default:
13788 : break;
13789 :
13790 : }
13791 :
13792 : break;
13793 :
13794 : default:
13795 : break;
13796 : }
13797 : return false;
13798 : }
13799 :
13800 :
13801 : static tree
13802 3779 : trans_class_assignment (stmtblock_t *block, gfc_expr *lhs, gfc_expr *rhs,
13803 : gfc_se *lse, gfc_se *rse, bool use_vptr_copy,
13804 : bool class_realloc)
13805 : {
13806 3779 : tree tmp, fcn, stdcopy, to_len, from_len, vptr, old_vptr, rhs_vptr;
13807 3779 : vec<tree, va_gc> *args = NULL;
13808 3779 : bool final_expr;
13809 :
13810 3779 : final_expr = gfc_assignment_finalizer_call (lse, lhs, false);
13811 3779 : if (final_expr)
13812 : {
13813 569 : if (rse->loop)
13814 244 : gfc_prepend_expr_to_block (&rse->loop->pre,
13815 : gfc_finish_block (&lse->finalblock));
13816 : else
13817 325 : gfc_add_block_to_block (block, &lse->finalblock);
13818 : }
13819 :
13820 : /* Store the old vptr so that dynamic types can be compared for
13821 : reallocation to occur or not. */
13822 3779 : if (class_realloc)
13823 : {
13824 361 : tmp = lse->expr;
13825 361 : if (!GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
13826 0 : tmp = gfc_get_class_from_expr (tmp);
13827 : }
13828 :
13829 3779 : vptr = trans_class_vptr_len_assignment (block, lhs, rhs, rse, &to_len,
13830 : &from_len, &rhs_vptr);
13831 3779 : if (rhs_vptr == NULL_TREE)
13832 31 : rhs_vptr = vptr;
13833 :
13834 : /* Generate (re)allocation of the lhs. */
13835 3779 : if (class_realloc)
13836 : {
13837 361 : stmtblock_t alloc, re_alloc;
13838 361 : tree class_han, re, size;
13839 :
13840 361 : if (tmp && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
13841 361 : old_vptr = gfc_evaluate_now (gfc_class_vptr_get (tmp), block);
13842 : else
13843 0 : old_vptr = build_int_cst (TREE_TYPE (vptr), 0);
13844 :
13845 361 : size = gfc_vptr_size_get (rhs_vptr);
13846 :
13847 : /* Take into account _len of unlimited polymorphic entities.
13848 : TODO: handle class(*) allocatable function results on rhs. */
13849 361 : if (UNLIMITED_POLY (rhs))
13850 : {
13851 18 : tree len;
13852 18 : if (rhs->expr_type == EXPR_VARIABLE)
13853 12 : len = trans_get_upoly_len (block, rhs);
13854 : else
13855 6 : len = gfc_class_len_get (tmp);
13856 18 : len = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
13857 : fold_convert (size_type_node, len),
13858 : size_one_node);
13859 18 : size = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (size),
13860 18 : size, fold_convert (TREE_TYPE (size), len));
13861 18 : }
13862 343 : else if (rhs->ts.type == BT_CHARACTER && rse->string_length)
13863 39 : size = fold_build2_loc (input_location, MULT_EXPR,
13864 : gfc_charlen_type_node, size,
13865 : rse->string_length);
13866 :
13867 :
13868 361 : tmp = lse->expr;
13869 361 : class_han = GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
13870 361 : ? gfc_class_data_get (tmp) : tmp;
13871 :
13872 361 : if (!POINTER_TYPE_P (TREE_TYPE (class_han)))
13873 0 : class_han = gfc_build_addr_expr (NULL_TREE, class_han);
13874 :
13875 : /* Allocate block. */
13876 361 : gfc_init_block (&alloc);
13877 361 : gfc_allocate_using_malloc (&alloc, class_han, size, NULL_TREE);
13878 :
13879 : /* Reallocate if dynamic types are different. */
13880 361 : gfc_init_block (&re_alloc);
13881 361 : if (UNLIMITED_POLY (lhs) && rhs->ts.type == BT_CHARACTER)
13882 : {
13883 39 : gfc_add_expr_to_block (&re_alloc, gfc_call_free (class_han));
13884 39 : gfc_allocate_using_malloc (&re_alloc, class_han, size, NULL_TREE);
13885 : }
13886 : else
13887 : {
13888 322 : tmp = fold_convert (pvoid_type_node, class_han);
13889 322 : re = build_call_expr_loc (input_location,
13890 : builtin_decl_explicit (BUILT_IN_REALLOC),
13891 : 2, tmp, size);
13892 322 : re = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (tmp),
13893 : tmp, re);
13894 322 : tmp = fold_build2_loc (input_location, NE_EXPR,
13895 : logical_type_node, rhs_vptr, old_vptr);
13896 322 : re = fold_build3_loc (input_location, COND_EXPR, void_type_node,
13897 : tmp, re, build_empty_stmt (input_location));
13898 322 : gfc_add_expr_to_block (&re_alloc, re);
13899 : }
13900 361 : tree realloc_expr = lhs->ts.type == BT_CLASS ?
13901 361 : gfc_finish_block (&re_alloc) :
13902 0 : build_empty_stmt (input_location);
13903 :
13904 : /* Allocate if _data is NULL, reallocate otherwise. */
13905 361 : tmp = fold_build2_loc (input_location, EQ_EXPR,
13906 : logical_type_node, class_han,
13907 : build_int_cst (prvoid_type_node, 0));
13908 361 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
13909 : gfc_unlikely (tmp,
13910 : PRED_FORTRAN_FAIL_ALLOC),
13911 : gfc_finish_block (&alloc),
13912 : realloc_expr);
13913 361 : gfc_add_expr_to_block (&lse->pre, tmp);
13914 : }
13915 :
13916 3779 : fcn = gfc_vptr_copy_get (vptr);
13917 :
13918 3779 : tmp = GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr))
13919 3779 : ? gfc_class_data_get (rse->expr) : rse->expr;
13920 3779 : if (use_vptr_copy)
13921 : {
13922 6340 : if (!POINTER_TYPE_P (TREE_TYPE (tmp))
13923 578 : || INDIRECT_REF_P (tmp)
13924 421 : || (rhs->ts.type == BT_DERIVED
13925 0 : && rhs->ts.u.derived->attr.unlimited_polymorphic
13926 0 : && !rhs->ts.u.derived->attr.pointer
13927 0 : && !rhs->ts.u.derived->attr.allocatable)
13928 3877 : || (UNLIMITED_POLY (rhs)
13929 134 : && !CLASS_DATA (rhs)->attr.pointer
13930 43 : && !CLASS_DATA (rhs)->attr.allocatable))
13931 3035 : vec_safe_push (args, gfc_build_addr_expr (NULL_TREE, tmp));
13932 : else
13933 421 : vec_safe_push (args, tmp);
13934 3456 : tmp = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
13935 3456 : ? gfc_class_data_get (lse->expr) : lse->expr;
13936 6012 : if (!POINTER_TYPE_P (TREE_TYPE (tmp))
13937 900 : || INDIRECT_REF_P (tmp)
13938 361 : || (lhs->ts.type == BT_DERIVED
13939 0 : && lhs->ts.u.derived->attr.unlimited_polymorphic
13940 0 : && !lhs->ts.u.derived->attr.pointer
13941 0 : && !lhs->ts.u.derived->attr.allocatable)
13942 3817 : || (UNLIMITED_POLY (lhs)
13943 161 : && !CLASS_DATA (lhs)->attr.pointer
13944 161 : && !CLASS_DATA (lhs)->attr.allocatable))
13945 3095 : vec_safe_push (args, gfc_build_addr_expr (NULL_TREE, tmp));
13946 : else
13947 361 : vec_safe_push (args, tmp);
13948 :
13949 3456 : stdcopy = build_call_vec (TREE_TYPE (TREE_TYPE (fcn)), fcn, args);
13950 :
13951 3456 : if (to_len != NULL_TREE && !integer_zerop (from_len))
13952 : {
13953 454 : tree extcopy;
13954 454 : vec_safe_push (args, from_len);
13955 454 : vec_safe_push (args, to_len);
13956 454 : extcopy = build_call_vec (TREE_TYPE (TREE_TYPE (fcn)), fcn, args);
13957 :
13958 454 : tmp = fold_build2_loc (input_location, GT_EXPR,
13959 : logical_type_node, from_len,
13960 454 : build_zero_cst (TREE_TYPE (from_len)));
13961 454 : return fold_build3_loc (input_location, COND_EXPR,
13962 : void_type_node, tmp,
13963 454 : extcopy, stdcopy);
13964 : }
13965 : else
13966 : return stdcopy;
13967 : }
13968 : else
13969 : {
13970 323 : tree rhst = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
13971 323 : ? gfc_class_data_get (lse->expr) : lse->expr;
13972 323 : stmtblock_t tblock;
13973 323 : gfc_init_block (&tblock);
13974 323 : if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
13975 0 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
13976 323 : if (!POINTER_TYPE_P (TREE_TYPE (rhst)))
13977 0 : rhst = gfc_build_addr_expr (NULL_TREE, rhst);
13978 : /* When coming from a ptr_copy lhs and rhs are swapped. */
13979 323 : gfc_add_modify_loc (input_location, &tblock, rhst,
13980 323 : fold_convert (TREE_TYPE (rhst), tmp));
13981 323 : return gfc_finish_block (&tblock);
13982 : }
13983 : }
13984 :
13985 : bool
13986 321563 : is_assoc_assign (gfc_expr *lhs, gfc_expr *rhs)
13987 : {
13988 321563 : if (lhs->expr_type != EXPR_VARIABLE || rhs->expr_type != EXPR_VARIABLE)
13989 : return false;
13990 :
13991 33329 : return lhs->symtree->n.sym->assoc
13992 33329 : && lhs->symtree->n.sym->assoc->target == rhs;
13993 : }
13994 :
13995 : /* Return true if RHS is a function reference with an actual argument that
13996 : overlaps LHS and whose dummy may be redefined. */
13997 :
13998 : static bool
13999 24872 : rhs_may_redefine_lhs (gfc_expr *lhs, gfc_expr *rhs)
14000 : {
14001 24872 : if (rhs->expr_type != EXPR_FUNCTION || rhs->value.function.isym)
14002 : return false;
14003 :
14004 2574 : gfc_symbol *esym = rhs->value.function.esym;
14005 2574 : gfc_formal_arglist *f = esym ? esym->formal : NULL;
14006 :
14007 9470 : for (gfc_actual_arglist *a = rhs->value.function.actual; a; a = a->next)
14008 : {
14009 6947 : gfc_symbol *dummy = f ? f->sym : NULL;
14010 :
14011 : /* Without a dummy to inspect, assume the worst. */
14012 6864 : if ((!dummy
14013 6864 : || (dummy->attr.intent != INTENT_IN && !dummy->attr.value))
14014 1878 : && a->expr && a->expr->expr_type == EXPR_VARIABLE
14015 8408 : && gfc_check_dependency (lhs, a->expr, false))
14016 : return true;
14017 :
14018 6896 : if (f)
14019 6844 : f = f->next;
14020 : }
14021 :
14022 : return false;
14023 : }
14024 :
14025 :
14026 : /* Subroutine of gfc_trans_assignment that actually scalarizes the
14027 : assignment. EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
14028 : init_flag indicates initialization expressions and dealloc that no
14029 : deallocate prior assignment is needed (if in doubt, set true).
14030 : When PTR_COPY is set and expr1 is a class type, then use the _vptr-copy
14031 : routine instead of a pointer assignment. Alias resolution is only done,
14032 : when MAY_ALIAS is set (the default). This flag is used by ALLOCATE()
14033 : where it is known, that newly allocated memory on the lhs can never be
14034 : an alias of the rhs. */
14035 :
14036 : static tree
14037 321563 : gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
14038 : bool dealloc, bool use_vptr_copy, bool may_alias)
14039 : {
14040 321563 : gfc_se lse;
14041 321563 : gfc_se rse;
14042 321563 : gfc_ss *lss;
14043 321563 : gfc_ss *lss_section;
14044 321563 : gfc_ss *rss;
14045 321563 : gfc_loopinfo loop;
14046 321563 : tree tmp;
14047 321563 : stmtblock_t block;
14048 321563 : stmtblock_t body;
14049 321563 : bool final_expr;
14050 321563 : bool l_is_temp;
14051 321563 : bool scalar_to_array;
14052 321563 : tree string_length;
14053 321563 : int n;
14054 321563 : bool maybe_workshare = false, lhs_refs_comp = false, rhs_refs_comp = false;
14055 321563 : symbol_attribute lhs_caf_attr, rhs_caf_attr, lhs_attr, rhs_attr;
14056 321563 : bool is_poly_assign;
14057 321563 : bool realloc_flag;
14058 321563 : bool assoc_assign = false;
14059 321563 : bool dummy_class_array_copy;
14060 :
14061 : /* Assignment of the form lhs = rhs. */
14062 321563 : gfc_start_block (&block);
14063 :
14064 321563 : gfc_init_se (&lse, NULL);
14065 321563 : gfc_init_se (&rse, NULL);
14066 :
14067 321563 : gfc_fix_class_refs (expr1);
14068 :
14069 643126 : realloc_flag = flag_realloc_lhs
14070 315170 : && gfc_is_reallocatable_lhs (expr1)
14071 8602 : && expr2->rank
14072 328643 : && !is_runtime_conformable (expr1, expr2);
14073 :
14074 : /* Walk the lhs. */
14075 321563 : lss = gfc_walk_expr (expr1);
14076 321563 : if (realloc_flag)
14077 : {
14078 6697 : lss->no_bounds_check = 1;
14079 6697 : lss->is_alloc_lhs = 1;
14080 : }
14081 : else
14082 314866 : lss->no_bounds_check = expr1->no_bounds_check;
14083 :
14084 321563 : rss = NULL;
14085 :
14086 321563 : if (expr2->expr_type != EXPR_VARIABLE
14087 321563 : && expr2->expr_type != EXPR_CONSTANT
14088 321563 : && (expr2->ts.type == BT_CLASS || gfc_may_be_finalized (expr2->ts)))
14089 : {
14090 953 : expr2->must_finalize = 1;
14091 : /* F2023 7.5.6.3: If an executable construct references a nonpointer
14092 : function, the result is finalized after execution of the innermost
14093 : executable construct containing the reference. */
14094 953 : if (expr2->expr_type == EXPR_FUNCTION
14095 953 : && (gfc_expr_attr (expr2).pointer
14096 316 : || (expr2->ts.type == BT_CLASS && CLASS_DATA (expr2)->attr.class_pointer)))
14097 147 : expr2->must_finalize = 0;
14098 : /* F2008 4.5.6.3 para 5: If an executable construct references a
14099 : structure constructor or array constructor, the entity created by
14100 : the constructor is finalized after execution of the innermost
14101 : executable construct containing the reference.
14102 : These finalizations were later deleted by the Combined Technical
14103 : Corrigenda 1 TO 4 for fortran 2008 (f08/0011). */
14104 806 : else if (gfc_notification_std (GFC_STD_F2018_DEL)
14105 806 : && (expr2->expr_type == EXPR_STRUCTURE
14106 763 : || expr2->expr_type == EXPR_ARRAY))
14107 428 : expr2->must_finalize = 0;
14108 : }
14109 :
14110 :
14111 : /* Checking whether a class assignment is desired is quite complicated and
14112 : needed at two locations, so do it once only before the information is
14113 : needed. */
14114 321563 : lhs_attr = gfc_expr_attr (expr1);
14115 321563 : rhs_attr = gfc_expr_attr (expr2);
14116 321563 : dummy_class_array_copy
14117 643126 : = (expr2->expr_type == EXPR_VARIABLE
14118 33329 : && expr2->rank > 0
14119 7958 : && expr2->symtree != NULL
14120 7958 : && expr2->symtree->n.sym->attr.dummy
14121 1439 : && expr2->ts.type == BT_CLASS
14122 169 : && !rhs_attr.pointer
14123 163 : && !rhs_attr.allocatable
14124 150 : && !CLASS_DATA (expr2)->attr.class_pointer
14125 321713 : && !CLASS_DATA (expr2)->attr.allocatable);
14126 :
14127 : /* What can be sent to trans_class_assignment includes all the obvious
14128 : candidates but scalar assignment of a class expression to a derived type
14129 : must be done using gfc_trans_scalar_assign; partly because it is simpler
14130 : and partly because some cases fail, eg. class assignment to derived_type
14131 : select type temporaries. */
14132 321563 : is_poly_assign
14133 321563 : = (use_vptr_copy
14134 303081 : || ((lhs_attr.pointer || lhs_attr.allocatable) && !lhs_attr.dimension))
14135 24632 : && (expr1->ts.type == BT_CLASS || gfc_is_class_array_ref (expr1, NULL)
14136 22345 : || gfc_is_class_scalar_expr (expr1)
14137 20841 : || gfc_is_class_array_ref (expr2, NULL)
14138 20841 : || (gfc_is_class_scalar_expr (expr2)
14139 30 : && !(expr1->ts.type == BT_DERIVED && !lhs_attr.dimension)))
14140 325354 : && lhs_attr.flavor != FL_PROCEDURE;
14141 :
14142 321563 : assoc_assign = is_assoc_assign (expr1, expr2);
14143 :
14144 : /* Only analyze the expressions for coarray properties, when in coarray-lib
14145 : mode. Avoid false-positive uninitialized diagnostics with initializing
14146 : the codimension flag unconditionally. */
14147 321563 : lhs_caf_attr.codimension = false;
14148 321563 : rhs_caf_attr.codimension = false;
14149 321563 : if (flag_coarray == GFC_FCOARRAY_LIB)
14150 : {
14151 7061 : lhs_caf_attr = gfc_caf_attr (expr1, false, &lhs_refs_comp);
14152 7061 : rhs_caf_attr = gfc_caf_attr (expr2, false, &rhs_refs_comp);
14153 : }
14154 :
14155 321563 : tree reallocation = NULL_TREE;
14156 321563 : if (lss != gfc_ss_terminator)
14157 : {
14158 : /* The assignment needs scalarization. */
14159 : lss_section = lss;
14160 :
14161 : /* Find a non-scalar SS from the lhs. */
14162 : while (lss_section != gfc_ss_terminator
14163 40771 : && lss_section->info->type != GFC_SS_SECTION)
14164 0 : lss_section = lss_section->next;
14165 :
14166 40771 : gcc_assert (lss_section != gfc_ss_terminator);
14167 :
14168 : /* Initialize the scalarizer. */
14169 40771 : gfc_init_loopinfo (&loop);
14170 :
14171 : /* Walk the rhs. */
14172 40771 : rss = gfc_walk_expr (expr2);
14173 40771 : if (rss == gfc_ss_terminator)
14174 : {
14175 : /* The rhs is scalar. Add a ss for the expression. */
14176 15452 : rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
14177 15452 : lss->is_alloc_lhs = 0;
14178 : }
14179 :
14180 : /* When doing a class assign, then the handle to the rhs needs to be a
14181 : pointer to allow for polymorphism. */
14182 40771 : if (is_poly_assign && expr2->rank == 0 && !UNLIMITED_POLY (expr2))
14183 587 : rss->info->type = GFC_SS_REFERENCE;
14184 :
14185 40771 : rss->no_bounds_check = expr2->no_bounds_check;
14186 : /* Associate the SS with the loop. */
14187 40771 : gfc_add_ss_to_loop (&loop, lss);
14188 40771 : gfc_add_ss_to_loop (&loop, rss);
14189 :
14190 : /* Calculate the bounds of the scalarization. */
14191 40771 : gfc_conv_ss_startstride (&loop);
14192 : /* Enable loop reversal. */
14193 693107 : for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
14194 611565 : loop.reverse[n] = GFC_ENABLE_REVERSE;
14195 : /* Resolve any data dependencies in the statement. */
14196 40771 : if (may_alias)
14197 38515 : gfc_conv_resolve_dependencies (&loop, lss, rss);
14198 : /* Setup the scalarizing loops. */
14199 40771 : gfc_conv_loop_setup (&loop, &expr2->where);
14200 :
14201 : /* Setup the gfc_se structures. */
14202 40771 : gfc_copy_loopinfo_to_se (&lse, &loop);
14203 40771 : gfc_copy_loopinfo_to_se (&rse, &loop);
14204 :
14205 40771 : rse.ss = rss;
14206 40771 : gfc_mark_ss_chain_used (rss, 1);
14207 40771 : if (loop.temp_ss == NULL)
14208 : {
14209 39665 : lse.ss = lss;
14210 39665 : gfc_mark_ss_chain_used (lss, 1);
14211 : }
14212 : else
14213 : {
14214 1106 : lse.ss = loop.temp_ss;
14215 1106 : gfc_mark_ss_chain_used (lss, 3);
14216 1106 : gfc_mark_ss_chain_used (loop.temp_ss, 3);
14217 : }
14218 :
14219 : /* Allow the scalarizer to workshare array assignments. */
14220 40771 : if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_BODY))
14221 : == OMPWS_WORKSHARE_FLAG
14222 70 : && loop.temp_ss == NULL)
14223 : {
14224 58 : maybe_workshare = true;
14225 58 : ompws_flags |= OMPWS_SCALARIZER_WS | OMPWS_SCALARIZER_BODY;
14226 : }
14227 :
14228 : /* F2003: Allocate or reallocate lhs of allocatable array. */
14229 40771 : if (realloc_flag)
14230 : {
14231 6697 : realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
14232 6697 : ompws_flags &= ~OMPWS_SCALARIZER_WS;
14233 6697 : reallocation = gfc_alloc_allocatable_for_assignment (&loop, expr1,
14234 : expr2);
14235 : }
14236 :
14237 : /* Start the scalarized loop body. */
14238 40771 : gfc_start_scalarized_body (&loop, &body);
14239 : }
14240 : else
14241 280792 : gfc_init_block (&body);
14242 :
14243 321563 : l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
14244 :
14245 : /* Translate the expression. */
14246 643126 : rse.want_coarray = flag_coarray == GFC_FCOARRAY_LIB
14247 321563 : && (init_flag || assoc_assign) && lhs_caf_attr.codimension;
14248 321563 : rse.want_pointer = rse.want_coarray && !init_flag && !lhs_caf_attr.dimension;
14249 321563 : gfc_conv_expr (&rse, expr2);
14250 :
14251 : /* Deal with the case of a scalar class function assigned to a derived type.
14252 : */
14253 321563 : if (gfc_is_alloc_class_scalar_function (expr2)
14254 321563 : && expr1->ts.type == BT_DERIVED)
14255 : {
14256 60 : rse.expr = gfc_class_data_get (rse.expr);
14257 60 : rse.expr = build_fold_indirect_ref_loc (input_location, rse.expr);
14258 : }
14259 :
14260 : /* The lhs components to free are read after the rhs call. */
14261 296691 : if (gfc_bt_struct (expr1->ts.type)
14262 24872 : && rhs_may_redefine_lhs (expr1, expr2)
14263 321614 : && !TREE_CONSTANT (rse.expr) && !VAR_P (rse.expr))
14264 51 : rse.expr = gfc_evaluate_now (rse.expr, &rse.pre);
14265 :
14266 : /* Stabilize a string length for temporaries. */
14267 321563 : if (expr2->ts.type == BT_CHARACTER && !expr1->ts.deferred
14268 25264 : && !(VAR_P (rse.string_length)
14269 : || TREE_CODE (rse.string_length) == PARM_DECL
14270 : || INDIRECT_REF_P (rse.string_length)))
14271 24340 : string_length = gfc_evaluate_now (rse.string_length, &rse.pre);
14272 297223 : else if (expr2->ts.type == BT_CHARACTER)
14273 : {
14274 4556 : if (expr1->ts.deferred
14275 7061 : && gfc_expr_attr (expr1).allocatable
14276 7181 : && gfc_check_dependency (expr1, expr2, true))
14277 120 : rse.string_length =
14278 120 : gfc_evaluate_now_function_scope (rse.string_length, &rse.pre);
14279 4556 : string_length = rse.string_length;
14280 : }
14281 : else
14282 : string_length = NULL_TREE;
14283 :
14284 321563 : if (l_is_temp)
14285 : {
14286 1106 : gfc_conv_tmp_array_ref (&lse);
14287 1106 : if (expr2->ts.type == BT_CHARACTER)
14288 123 : lse.string_length = string_length;
14289 : }
14290 : else
14291 : {
14292 320457 : gfc_conv_expr (&lse, expr1);
14293 : /* For some expression (e.g. complex numbers) fold_convert uses a
14294 : SAVE_EXPR, which is hazardous on the lhs, because the value is
14295 : not updated when assigned to. */
14296 320457 : if (TREE_CODE (lse.expr) == SAVE_EXPR)
14297 8 : lse.expr = TREE_OPERAND (lse.expr, 0);
14298 :
14299 6183 : if (gfc_option.rtcheck & GFC_RTCHECK_MEM && !init_flag
14300 326640 : && gfc_expr_attr (expr1).allocatable && expr1->rank && !expr2->rank)
14301 : {
14302 36 : tree cond;
14303 36 : const char* msg;
14304 :
14305 36 : tmp = INDIRECT_REF_P (lse.expr)
14306 36 : ? gfc_build_addr_expr (NULL_TREE, lse.expr) : lse.expr;
14307 36 : STRIP_NOPS (tmp);
14308 :
14309 : /* We should only get array references here. */
14310 36 : gcc_assert (TREE_CODE (tmp) == POINTER_PLUS_EXPR
14311 : || TREE_CODE (tmp) == ARRAY_REF);
14312 :
14313 : /* 'tmp' is either the pointer to the array(POINTER_PLUS_EXPR)
14314 : or the array itself(ARRAY_REF). */
14315 36 : tmp = TREE_OPERAND (tmp, 0);
14316 :
14317 : /* Provide the address of the array. */
14318 36 : if (TREE_CODE (lse.expr) == ARRAY_REF)
14319 18 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
14320 :
14321 36 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
14322 36 : tmp, build_int_cst (TREE_TYPE (tmp), 0));
14323 36 : msg = _("Assignment of scalar to unallocated array");
14324 36 : gfc_trans_runtime_check (true, false, cond, &loop.pre,
14325 : &expr1->where, msg);
14326 : }
14327 :
14328 : /* Deallocate the lhs parameterized components if required. */
14329 320457 : if (dealloc
14330 300366 : && !expr1->symtree->n.sym->attr.associate_var
14331 298222 : && expr2->expr_type != EXPR_ARRAY
14332 291771 : && (IS_PDT (expr1) || IS_CLASS_PDT (expr1)))
14333 : {
14334 528 : bool pdt_dep = gfc_check_dependency (expr1, expr2, true);
14335 :
14336 528 : tmp = lse.expr;
14337 528 : if (pdt_dep)
14338 : {
14339 : /* Create a temporary for deallocation after assignment. */
14340 263 : tmp = gfc_create_var (TREE_TYPE (lse.expr), "pdt_tmp");
14341 263 : gfc_add_modify (&lse.pre, tmp, lse.expr);
14342 : }
14343 :
14344 528 : if (expr1->ts.type == BT_DERIVED)
14345 528 : tmp = gfc_deallocate_pdt_comp (expr1->ts.u.derived, tmp,
14346 : expr1->rank);
14347 0 : else if (expr1->ts.type == BT_CLASS)
14348 : {
14349 0 : tmp = gfc_class_data_get (tmp);
14350 0 : tmp = gfc_deallocate_pdt_comp (CLASS_DATA (expr1)->ts.u.derived,
14351 : tmp, expr1->rank);
14352 : }
14353 :
14354 528 : if (tmp && pdt_dep)
14355 134 : gfc_add_expr_to_block (&rse.post, tmp);
14356 394 : else if (tmp)
14357 133 : gfc_add_expr_to_block (&lse.pre, tmp);
14358 : }
14359 : }
14360 :
14361 : /* Assignments of scalar derived types with allocatable components
14362 : to arrays must be done with a deep copy and the rhs temporary
14363 : must have its components deallocated afterwards. */
14364 643126 : scalar_to_array = (expr2->ts.type == BT_DERIVED
14365 21646 : && expr2->ts.u.derived->attr.alloc_comp
14366 7474 : && !gfc_expr_is_variable (expr2)
14367 325596 : && expr1->rank && !expr2->rank);
14368 643126 : scalar_to_array |= (expr1->ts.type == BT_DERIVED
14369 21906 : && expr1->rank
14370 4086 : && expr1->ts.u.derived->attr.alloc_comp
14371 323056 : && gfc_is_alloc_class_scalar_function (expr2));
14372 321563 : if (scalar_to_array && dealloc)
14373 : {
14374 59 : tmp = gfc_deallocate_alloc_comp_no_caf (expr2->ts.u.derived, rse.expr, 0);
14375 59 : gfc_prepend_expr_to_block (&loop.post, tmp);
14376 : }
14377 :
14378 : /* When assigning a character function result to a deferred-length variable,
14379 : the function call must happen before the (re)allocation of the lhs -
14380 : otherwise the character length of the result is not known.
14381 : NOTE 1: This relies on having the exact dependence of the length type
14382 : parameter available to the caller; gfortran saves it in the .mod files.
14383 : NOTE 2: Vector array references generate an index temporary that must
14384 : not go outside the loop. Otherwise, variables should not generate
14385 : a pre block.
14386 : NOTE 3: The concatenation operation generates a temporary pointer,
14387 : whose allocation must go to the innermost loop.
14388 : NOTE 4: Elemental functions may generate a temporary, too. */
14389 321563 : if (flag_realloc_lhs
14390 315170 : && expr2->ts.type == BT_CHARACTER && expr1->ts.deferred
14391 3104 : && !(lss != gfc_ss_terminator
14392 958 : && rss != gfc_ss_terminator
14393 958 : && ((expr2->expr_type == EXPR_VARIABLE && expr2->rank)
14394 765 : || (expr2->expr_type == EXPR_FUNCTION
14395 166 : && expr2->value.function.esym != NULL
14396 26 : && expr2->value.function.esym->attr.elemental)
14397 752 : || (expr2->expr_type == EXPR_FUNCTION
14398 153 : && expr2->value.function.isym != NULL
14399 140 : && expr2->value.function.isym->elemental)
14400 696 : || (expr2->expr_type == EXPR_OP
14401 31 : && expr2->value.op.op == INTRINSIC_CONCAT))))
14402 2817 : gfc_add_block_to_block (&block, &rse.pre);
14403 :
14404 : /* Nullify the allocatable components corresponding to those of the lhs
14405 : derived type, so that the finalization of the function result does not
14406 : affect the lhs of the assignment. Prepend is used to ensure that the
14407 : nullification occurs before the call to the finalizer. In the case of
14408 : a scalar to array assignment, this is done in gfc_trans_scalar_assign
14409 : as part of the deep copy. */
14410 320690 : if (!scalar_to_array && expr1->ts.type == BT_DERIVED
14411 342596 : && (gfc_is_class_array_function (expr2)
14412 21003 : || gfc_is_alloc_class_scalar_function (expr2)))
14413 : {
14414 84 : tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, rse.expr, 0);
14415 84 : gfc_prepend_expr_to_block (&rse.post, tmp);
14416 84 : if (lss != gfc_ss_terminator && rss == gfc_ss_terminator)
14417 0 : gfc_add_block_to_block (&loop.post, &rse.post);
14418 : }
14419 :
14420 321563 : tmp = NULL_TREE;
14421 :
14422 321563 : if (is_poly_assign)
14423 : {
14424 11337 : tmp = trans_class_assignment (&body, expr1, expr2, &lse, &rse,
14425 684 : use_vptr_copy || (lhs_attr.allocatable
14426 361 : && !lhs_attr.dimension),
14427 3505 : !realloc_flag && flag_realloc_lhs
14428 684 : && !lhs_attr.pointer);
14429 : /* Free the components of an rhs temporary after its deep copy. */
14430 3779 : if ((expr2->expr_type == EXPR_FUNCTION
14431 3535 : || (expr2->expr_type == EXPR_STRUCTURE && !expr2->rank
14432 950 : && (use_vptr_copy
14433 115 : || (lhs_attr.allocatable && !lhs_attr.dimension))))
14434 1194 : && expr2->ts.type == BT_DERIVED
14435 968 : && (expr2->ts.u.derived->attr.alloc_comp
14436 426 : || (expr2->expr_type == EXPR_STRUCTURE && IS_PDT (expr2))))
14437 : {
14438 554 : gfc_symbol *rhs_type = expr2->ts.u.derived;
14439 554 : stmtblock_t free_rhs;
14440 554 : gfc_init_block (&free_rhs);
14441 554 : tree tmp2;
14442 554 : if (rhs_type->attr.alloc_comp)
14443 : {
14444 542 : tmp2 = gfc_deallocate_alloc_comp (rhs_type, rse.expr,
14445 : expr2->rank);
14446 542 : gfc_add_expr_to_block (&free_rhs, tmp2);
14447 : }
14448 554 : if (expr2->expr_type == EXPR_STRUCTURE && IS_PDT (expr2))
14449 : {
14450 12 : tmp2 = gfc_deallocate_pdt_comp (rhs_type, rse.expr, 0);
14451 12 : gfc_add_expr_to_block (&free_rhs, tmp2);
14452 : }
14453 650 : gfc_add_expr_to_block (lss == gfc_ss_terminator ? &rse.post
14454 : : &loop.post,
14455 : gfc_finish_block (&free_rhs));
14456 : }
14457 :
14458 3779 : expr1->must_finalize = 0;
14459 : }
14460 317784 : else if (!is_poly_assign
14461 317784 : && expr1->ts.type == BT_CLASS
14462 399 : && expr2->ts.type == BT_CLASS
14463 193 : && (expr2->must_finalize || dummy_class_array_copy))
14464 : {
14465 : /* This case comes about when the scalarizer provides array element
14466 : references to class temporaries or nonpointer dummy arrays. Use the
14467 : vptr copy function, since this does a deep copy of allocatable
14468 : components. */
14469 132 : tmp = gfc_get_vptr_from_expr (rse.expr);
14470 132 : if (tmp == NULL_TREE && dummy_class_array_copy)
14471 12 : tmp = gfc_get_vptr_from_expr (gfc_get_class_from_gfc_expr (expr2));
14472 132 : if (tmp != NULL_TREE)
14473 : {
14474 132 : tree fcn = gfc_vptr_copy_get (tmp);
14475 132 : if (POINTER_TYPE_P (TREE_TYPE (fcn)))
14476 132 : fcn = build_fold_indirect_ref_loc (input_location, fcn);
14477 132 : tmp = build_call_expr_loc (input_location,
14478 : fcn, 2,
14479 : gfc_build_addr_expr (NULL, rse.expr),
14480 : gfc_build_addr_expr (NULL, lse.expr));
14481 : }
14482 : }
14483 :
14484 : /* Comply with F2018 (7.5.6.3). Make sure that any finalization code is added
14485 : after evaluation of the rhs and before reallocation.
14486 : Skip finalization for self-assignment to avoid use-after-free.
14487 : Strip parentheses from both sides to handle cases like a = (a). */
14488 321563 : final_expr = gfc_assignment_finalizer_call (&lse, expr1, init_flag);
14489 321563 : if (final_expr
14490 732 : && gfc_dep_compare_expr (strip_parentheses (expr1),
14491 : strip_parentheses (expr2)) != 0
14492 322271 : && !(strip_parentheses (expr2)->expr_type == EXPR_VARIABLE
14493 253 : && strip_parentheses (expr2)->symtree->n.sym->attr.artificial))
14494 : {
14495 708 : if (lss == gfc_ss_terminator)
14496 : {
14497 225 : gfc_add_block_to_block (&block, &rse.pre);
14498 225 : gfc_add_block_to_block (&block, &lse.finalblock);
14499 : }
14500 : else
14501 : {
14502 483 : gfc_add_block_to_block (&body, &rse.pre);
14503 483 : gfc_add_block_to_block (&loop.code[expr1->rank - 1],
14504 : &lse.finalblock);
14505 : }
14506 : }
14507 : else
14508 320855 : gfc_add_block_to_block (&body, &rse.pre);
14509 :
14510 321563 : if (flag_coarray != GFC_FCOARRAY_NONE && expr1->ts.type == BT_CHARACTER
14511 3004 : && assoc_assign)
14512 0 : tmp = gfc_trans_pointer_assignment (expr1, expr2);
14513 :
14514 : /* The finalization above is all that is wanted: the structure copy is done
14515 : component by component in generate_component_assignments. */
14516 321563 : if (expr1->finalize_only)
14517 30 : tmp = build_empty_stmt (input_location);
14518 :
14519 : /* If nothing else works, do it the old fashioned way! */
14520 321563 : if (tmp == NULL_TREE)
14521 : {
14522 : /* Strip parentheses to detect cases like a = (a) which need deep_copy. */
14523 317622 : gfc_expr *expr2_stripped = strip_parentheses (expr2);
14524 317622 : tmp
14525 635244 : = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
14526 317622 : gfc_expr_is_variable (expr2_stripped)
14527 286349 : || scalar_to_array
14528 285572 : || expr2->expr_type == EXPR_ARRAY,
14529 : !(l_is_temp || init_flag) && dealloc,
14530 317622 : expr1->symtree->n.sym->attr.codimension,
14531 : assoc_assign);
14532 : }
14533 :
14534 : /* Add the lse pre block to the body */
14535 321563 : gfc_add_block_to_block (&body, &lse.pre);
14536 321563 : gfc_add_expr_to_block (&body, tmp);
14537 :
14538 : /* Add the post blocks to the body. Scalar finalization must appear before
14539 : the post block in case any dellocations are done. */
14540 321563 : if (rse.finalblock.head
14541 321563 : && (!l_is_temp || (expr2->expr_type == EXPR_FUNCTION
14542 153 : && gfc_expr_attr (expr2).elemental)))
14543 : {
14544 153 : gfc_add_block_to_block (&body, &rse.finalblock);
14545 153 : gfc_add_block_to_block (&body, &rse.post);
14546 : }
14547 : else
14548 321410 : gfc_add_block_to_block (&body, &rse.post);
14549 :
14550 321563 : gfc_add_block_to_block (&body, &lse.post);
14551 :
14552 321563 : if (lss == gfc_ss_terminator)
14553 : {
14554 : /* F2003: Add the code for reallocation on assignment. */
14555 277758 : if (flag_realloc_lhs && is_scalar_reallocatable_lhs (expr1)
14556 284577 : && !is_poly_assign)
14557 3785 : alloc_scalar_allocatable_for_assignment (&block, string_length,
14558 : expr1, expr2);
14559 :
14560 : /* Use the scalar assignment as is. */
14561 280792 : gfc_add_block_to_block (&block, &body);
14562 : }
14563 : else
14564 : {
14565 40771 : gcc_assert (lse.ss == gfc_ss_terminator
14566 : && rse.ss == gfc_ss_terminator);
14567 :
14568 40771 : if (l_is_temp)
14569 : {
14570 1106 : gfc_trans_scalarized_loop_boundary (&loop, &body);
14571 :
14572 : /* We need to copy the temporary to the actual lhs. */
14573 1106 : gfc_init_se (&lse, NULL);
14574 1106 : gfc_init_se (&rse, NULL);
14575 1106 : gfc_copy_loopinfo_to_se (&lse, &loop);
14576 1106 : gfc_copy_loopinfo_to_se (&rse, &loop);
14577 :
14578 1106 : rse.ss = loop.temp_ss;
14579 1106 : lse.ss = lss;
14580 :
14581 1106 : gfc_conv_tmp_array_ref (&rse);
14582 1106 : gfc_conv_expr (&lse, expr1);
14583 :
14584 1106 : gcc_assert (lse.ss == gfc_ss_terminator
14585 : && rse.ss == gfc_ss_terminator);
14586 :
14587 1106 : if (expr2->ts.type == BT_CHARACTER)
14588 123 : rse.string_length = string_length;
14589 :
14590 1106 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
14591 : false, dealloc);
14592 1106 : gfc_add_expr_to_block (&body, tmp);
14593 : }
14594 :
14595 40771 : if (reallocation != NULL_TREE)
14596 6697 : gfc_add_expr_to_block (&loop.code[loop.dimen - 1], reallocation);
14597 :
14598 40771 : if (maybe_workshare)
14599 58 : ompws_flags &= ~OMPWS_SCALARIZER_BODY;
14600 :
14601 : /* Generate the copying loops. */
14602 40771 : gfc_trans_scalarizing_loops (&loop, &body);
14603 :
14604 : /* Wrap the whole thing up. */
14605 40771 : gfc_add_block_to_block (&block, &loop.pre);
14606 40771 : gfc_add_block_to_block (&block, &loop.post);
14607 :
14608 40771 : gfc_cleanup_loop (&loop);
14609 : }
14610 :
14611 : /* Since parameterized components cannot have default initializers,
14612 : the default PDT constructor leaves them unallocated. Do the
14613 : allocation now. */
14614 321563 : if (init_flag && IS_PDT (expr1)
14615 619 : && !expr1->symtree->n.sym->attr.allocatable
14616 601 : && !expr1->symtree->n.sym->attr.dummy)
14617 : {
14618 79 : gfc_symbol *sym = expr1->symtree->n.sym;
14619 79 : tmp = gfc_allocate_pdt_comp (sym->ts.u.derived,
14620 : sym->backend_decl,
14621 79 : sym->as ? sym->as->rank : 0,
14622 79 : sym->param_list);
14623 79 : gfc_add_expr_to_block (&block, tmp);
14624 : }
14625 :
14626 321563 : return gfc_finish_block (&block);
14627 : }
14628 :
14629 :
14630 : /* Check whether EXPR is a copyable array. */
14631 :
14632 : static bool
14633 327522 : copyable_array_p (gfc_expr * expr)
14634 : {
14635 327522 : if (expr->expr_type != EXPR_VARIABLE)
14636 : return false;
14637 :
14638 : /* First check it's an array. */
14639 327522 : if (expr->rank < 1 || !expr->ref || expr->ref->next)
14640 : return false;
14641 :
14642 47534 : if (!gfc_full_array_ref_p (expr->ref, NULL))
14643 : return false;
14644 :
14645 : /* Next check that it's of a simple enough type. */
14646 35810 : switch (expr->ts.type)
14647 : {
14648 : case BT_INTEGER:
14649 : case BT_REAL:
14650 : case BT_COMPLEX:
14651 : case BT_LOGICAL:
14652 : return true;
14653 :
14654 : case BT_CHARACTER:
14655 : return false;
14656 :
14657 2216 : case_bt_struct:
14658 2216 : return (!expr->ts.u.derived->attr.alloc_comp
14659 2216 : && !expr->ts.u.derived->attr.pdt_type);
14660 :
14661 : default:
14662 : break;
14663 : }
14664 :
14665 : return false;
14666 : }
14667 :
14668 : /* Check whether EXPR is a copyable slice. */
14669 :
14670 : static bool
14671 694533 : copyable_slice_p (gfc_expr * expr)
14672 : {
14673 694533 : if (expr->expr_type != EXPR_VARIABLE)
14674 : return false;
14675 :
14676 : /* First check it's a slice. */
14677 667117 : if (expr->rank < 1 || !expr->ref || expr->ref->next)
14678 : return false;
14679 :
14680 : /* Don't touch anything with coarrays. */
14681 211859 : if (gfc_is_coindexed (expr) || expr->symtree->n.sym->attr.codimension
14682 209283 : || expr->corank > 0)
14683 : return false;
14684 :
14685 102998 : if (!gfc_is_simply_contiguous (expr, false, false))
14686 : return false;
14687 :
14688 94636 : if (gfc_may_be_finalized (expr->ts))
14689 : return false;
14690 :
14691 : /* Next check that it's of a simple enough type. */
14692 93166 : switch (expr->ts.type)
14693 : {
14694 : case BT_INTEGER:
14695 : case BT_REAL:
14696 : case BT_COMPLEX:
14697 : case BT_LOGICAL:
14698 : return true;
14699 :
14700 : case BT_CHARACTER:
14701 : return false;
14702 :
14703 4143 : case_bt_struct:
14704 4143 : return (!expr->ts.u.derived->attr.alloc_comp
14705 4143 : && !expr->ts.u.derived->attr.pdt_type);
14706 :
14707 : default:
14708 : break;
14709 : }
14710 :
14711 : return false;
14712 : }
14713 :
14714 : /* Translate an assignment. */
14715 :
14716 : tree
14717 340715 : gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
14718 : bool dealloc, bool use_vptr_copy, bool may_alias)
14719 : {
14720 340715 : tree tmp;
14721 :
14722 : /* Special case a single function returning an array. */
14723 340715 : if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
14724 : {
14725 14668 : tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
14726 14668 : if (tmp)
14727 : return tmp;
14728 : }
14729 :
14730 : /* Special case assigning an array or array slice to zero. */
14731 333812 : if (copyable_slice_p (expr1) && is_zero_initializer_p (expr2))
14732 : {
14733 4176 : tmp = gfc_trans_zero_assign (expr1);
14734 4176 : if (tmp)
14735 : return tmp;
14736 : }
14737 :
14738 : /* Special case copying or moving one array slice to another. */
14739 329636 : if (copyable_slice_p (expr1)
14740 36217 : && !gfc_is_reallocatable_lhs (expr1)
14741 31085 : && copyable_slice_p (expr2)
14742 331750 : && gfc_compare_types (&expr1->ts, &expr2->ts))
14743 : {
14744 2114 : tmp = gfc_trans_array_copy (expr1, expr2);
14745 2114 : if (tmp)
14746 : return tmp;
14747 : }
14748 :
14749 : /* Special case initializing an array from a constant array constructor. */
14750 327522 : if (copyable_array_p (expr1)
14751 26564 : && expr2->expr_type == EXPR_ARRAY
14752 335908 : && gfc_compare_types (&expr1->ts, &expr2->ts))
14753 : {
14754 8386 : tmp = gfc_trans_array_constructor_copy (expr1, expr2);
14755 8386 : if (tmp)
14756 : return tmp;
14757 : }
14758 :
14759 321563 : if (UNLIMITED_POLY (expr1) && expr1->rank)
14760 321563 : use_vptr_copy = true;
14761 :
14762 : /* Fallback to the scalarizer to generate explicit loops. */
14763 321563 : return gfc_trans_assignment_1 (expr1, expr2, init_flag, dealloc,
14764 321563 : use_vptr_copy, may_alias);
14765 : }
14766 :
14767 : tree
14768 14539 : gfc_trans_init_assign (gfc_code * code)
14769 : {
14770 14539 : return gfc_trans_assignment (code->expr1, code->expr2, true, false, true);
14771 : }
14772 :
14773 : tree
14774 317334 : gfc_trans_assign (gfc_code * code)
14775 : {
14776 317334 : return gfc_trans_assignment (code->expr1, code->expr2, false, true);
14777 : }
14778 :
14779 : /* Generate a simple loop for internal use of the form
14780 : for (var = begin; var <cond> end; var += step)
14781 : body; */
14782 : void
14783 12282 : gfc_simple_for_loop (stmtblock_t *block, tree var, tree begin, tree end,
14784 : enum tree_code cond, tree step, tree body)
14785 : {
14786 12282 : tree tmp;
14787 :
14788 : /* var = begin. */
14789 12282 : gfc_add_modify (block, var, begin);
14790 :
14791 : /* Loop: for (var = begin; var <cond> end; var += step). */
14792 12282 : tree label_loop = gfc_build_label_decl (NULL_TREE);
14793 12282 : tree label_cond = gfc_build_label_decl (NULL_TREE);
14794 12282 : TREE_USED (label_loop) = 1;
14795 12282 : TREE_USED (label_cond) = 1;
14796 :
14797 12282 : gfc_add_expr_to_block (block, build1_v (GOTO_EXPR, label_cond));
14798 12282 : gfc_add_expr_to_block (block, build1_v (LABEL_EXPR, label_loop));
14799 :
14800 : /* Loop body. */
14801 12282 : gfc_add_expr_to_block (block, body);
14802 :
14803 : /* End of loop body. */
14804 12282 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (var), var, step);
14805 12282 : gfc_add_modify (block, var, tmp);
14806 12282 : gfc_add_expr_to_block (block, build1_v (LABEL_EXPR, label_cond));
14807 12282 : tmp = fold_build2_loc (input_location, cond, boolean_type_node, var, end);
14808 12282 : tmp = build3_v (COND_EXPR, tmp, build1_v (GOTO_EXPR, label_loop),
14809 : build_empty_stmt (input_location));
14810 12282 : gfc_add_expr_to_block (block, tmp);
14811 12282 : }
|