Branch data Line data Source code
1 : : /* Expression translation
2 : : Copyright (C) 2002-2025 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 : : /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
42 : : #include "trans-stmt.h"
43 : : #include "dependency.h"
44 : : #include "gimplify.h"
45 : : #include "tm.h" /* For CHAR_TYPE_SIZE. */
46 : :
47 : :
48 : : /* Calculate the number of characters in a string. */
49 : :
50 : : static tree
51 : 35461 : gfc_get_character_len (tree type)
52 : : {
53 : 35461 : tree len;
54 : :
55 : 35461 : gcc_assert (type && TREE_CODE (type) == ARRAY_TYPE
56 : : && TYPE_STRING_FLAG (type));
57 : :
58 : 35461 : len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
59 : 35461 : len = (len) ? (len) : (integer_zero_node);
60 : 35461 : return fold_convert (gfc_charlen_type_node, len);
61 : : }
62 : :
63 : :
64 : :
65 : : /* Calculate the number of bytes in a string. */
66 : :
67 : : tree
68 : 35461 : gfc_get_character_len_in_bytes (tree type)
69 : : {
70 : 35461 : tree tmp, len;
71 : :
72 : 35461 : gcc_assert (type && TREE_CODE (type) == ARRAY_TYPE
73 : : && TYPE_STRING_FLAG (type));
74 : :
75 : 35461 : tmp = TYPE_SIZE_UNIT (TREE_TYPE (type));
76 : 70922 : tmp = (tmp && !integer_zerop (tmp))
77 : 70922 : ? (fold_convert (gfc_charlen_type_node, tmp)) : (NULL_TREE);
78 : 35461 : len = gfc_get_character_len (type);
79 : 35461 : if (tmp && len && !integer_zerop (len))
80 : 34707 : len = fold_build2_loc (input_location, MULT_EXPR,
81 : : gfc_charlen_type_node, len, tmp);
82 : 35461 : return len;
83 : : }
84 : :
85 : :
86 : : /* Convert a scalar to an array descriptor. To be used for assumed-rank
87 : : arrays. */
88 : :
89 : : static tree
90 : 5859 : get_scalar_to_descriptor_type (tree scalar, symbol_attribute attr)
91 : : {
92 : 5859 : enum gfc_array_kind akind;
93 : :
94 : 5859 : if (attr.pointer)
95 : : akind = GFC_ARRAY_POINTER_CONT;
96 : 5509 : else if (attr.allocatable)
97 : : akind = GFC_ARRAY_ALLOCATABLE;
98 : : else
99 : 4746 : akind = GFC_ARRAY_ASSUMED_SHAPE_CONT;
100 : :
101 : 5859 : if (POINTER_TYPE_P (TREE_TYPE (scalar)))
102 : 4972 : scalar = TREE_TYPE (scalar);
103 : 5859 : return gfc_get_array_type_bounds (TREE_TYPE (scalar), 0, 0, NULL, NULL, 1,
104 : 5859 : akind, !(attr.pointer || attr.target));
105 : : }
106 : :
107 : : tree
108 : 5182 : gfc_conv_scalar_to_descriptor (gfc_se *se, tree scalar, symbol_attribute attr)
109 : : {
110 : 5182 : tree desc, type, etype;
111 : :
112 : 5182 : type = get_scalar_to_descriptor_type (scalar, attr);
113 : 5182 : etype = TREE_TYPE (scalar);
114 : 5182 : desc = gfc_create_var (type, "desc");
115 : 5182 : DECL_ARTIFICIAL (desc) = 1;
116 : :
117 : 5182 : if (CONSTANT_CLASS_P (scalar))
118 : : {
119 : 54 : tree tmp;
120 : 54 : tmp = gfc_create_var (TREE_TYPE (scalar), "scalar");
121 : 54 : gfc_add_modify (&se->pre, tmp, scalar);
122 : 54 : scalar = tmp;
123 : : }
124 : 5182 : if (!POINTER_TYPE_P (TREE_TYPE (scalar)))
125 : 887 : scalar = gfc_build_addr_expr (NULL_TREE, scalar);
126 : 4295 : else if (TREE_TYPE (etype) && TREE_CODE (TREE_TYPE (etype)) == ARRAY_TYPE)
127 : 113 : etype = TREE_TYPE (etype);
128 : 5182 : gfc_add_modify (&se->pre, gfc_conv_descriptor_dtype (desc),
129 : : gfc_get_dtype_rank_type (0, etype));
130 : 5182 : gfc_conv_descriptor_data_set (&se->pre, desc, scalar);
131 : 5182 : gfc_conv_descriptor_span_set (&se->pre, desc,
132 : : gfc_conv_descriptor_elem_len (desc));
133 : :
134 : : /* Copy pointer address back - but only if it could have changed and
135 : : if the actual argument is a pointer and not, e.g., NULL(). */
136 : 5182 : if ((attr.pointer || attr.allocatable) && attr.intent != INTENT_IN)
137 : 830 : gfc_add_modify (&se->post, scalar,
138 : 415 : fold_convert (TREE_TYPE (scalar),
139 : : gfc_conv_descriptor_data_get (desc)));
140 : 5182 : return desc;
141 : : }
142 : :
143 : :
144 : : /* Get the coarray token from the ultimate array or component ref.
145 : : Returns a NULL_TREE, when the ref object is not allocatable or pointer. */
146 : :
147 : : tree
148 : 408 : gfc_get_ultimate_alloc_ptr_comps_caf_token (gfc_se *outerse, gfc_expr *expr)
149 : : {
150 : 408 : gfc_symbol *sym = expr->symtree->n.sym;
151 : 816 : bool is_coarray = sym->ts.type == BT_CLASS
152 : 408 : ? CLASS_DATA (sym)->attr.codimension
153 : 373 : : sym->attr.codimension;
154 : 408 : gfc_expr *caf_expr = gfc_copy_expr (expr);
155 : 408 : gfc_ref *ref = caf_expr->ref, *last_caf_ref = NULL;
156 : :
157 : 1363 : while (ref)
158 : : {
159 : 955 : if (ref->type == REF_COMPONENT
160 : 374 : && (ref->u.c.component->attr.allocatable
161 : 98 : || ref->u.c.component->attr.pointer)
162 : 373 : && (is_coarray || ref->u.c.component->attr.codimension))
163 : 955 : last_caf_ref = ref;
164 : 955 : ref = ref->next;
165 : : }
166 : :
167 : 408 : if (last_caf_ref == NULL)
168 : : {
169 : 115 : gfc_free_expr (caf_expr);
170 : 115 : return NULL_TREE;
171 : : }
172 : :
173 : 133 : tree comp = last_caf_ref->u.c.component->caf_token
174 : 293 : ? gfc_comp_caf_token (last_caf_ref->u.c.component)
175 : : : NULL_TREE,
176 : : caf;
177 : 293 : gfc_se se;
178 : 293 : bool comp_ref = !last_caf_ref->u.c.component->attr.dimension;
179 : 293 : if (comp == NULL_TREE && comp_ref)
180 : : {
181 : 34 : gfc_free_expr (caf_expr);
182 : 34 : return NULL_TREE;
183 : : }
184 : 259 : gfc_init_se (&se, outerse);
185 : 259 : gfc_free_ref_list (last_caf_ref->next);
186 : 259 : last_caf_ref->next = NULL;
187 : 259 : caf_expr->rank = comp_ref ? 0 : last_caf_ref->u.c.component->as->rank;
188 : 518 : caf_expr->corank = last_caf_ref->u.c.component->as
189 : 259 : ? last_caf_ref->u.c.component->as->corank
190 : : : expr->corank;
191 : 259 : se.want_pointer = comp_ref;
192 : 259 : gfc_conv_expr (&se, caf_expr);
193 : 259 : gfc_add_block_to_block (&outerse->pre, &se.pre);
194 : :
195 : 259 : if (TREE_CODE (se.expr) == COMPONENT_REF && comp_ref)
196 : 133 : se.expr = TREE_OPERAND (se.expr, 0);
197 : 259 : gfc_free_expr (caf_expr);
198 : :
199 : 259 : if (comp_ref)
200 : 133 : caf = fold_build3_loc (input_location, COMPONENT_REF,
201 : 133 : TREE_TYPE (comp), se.expr, comp, NULL_TREE);
202 : : else
203 : 126 : caf = gfc_conv_descriptor_token (se.expr);
204 : 259 : return gfc_build_addr_expr (NULL_TREE, caf);
205 : : }
206 : :
207 : :
208 : : /* This is the seed for an eventual trans-class.c
209 : :
210 : : The following parameters should not be used directly since they might
211 : : in future implementations. Use the corresponding APIs. */
212 : : #define CLASS_DATA_FIELD 0
213 : : #define CLASS_VPTR_FIELD 1
214 : : #define CLASS_LEN_FIELD 2
215 : : #define VTABLE_HASH_FIELD 0
216 : : #define VTABLE_SIZE_FIELD 1
217 : : #define VTABLE_EXTENDS_FIELD 2
218 : : #define VTABLE_DEF_INIT_FIELD 3
219 : : #define VTABLE_COPY_FIELD 4
220 : : #define VTABLE_FINAL_FIELD 5
221 : : #define VTABLE_DEALLOCATE_FIELD 6
222 : :
223 : :
224 : : tree
225 : 40 : gfc_class_set_static_fields (tree decl, tree vptr, tree data)
226 : : {
227 : 40 : tree tmp;
228 : 40 : tree field;
229 : 40 : vec<constructor_elt, va_gc> *init = NULL;
230 : :
231 : 40 : field = TYPE_FIELDS (TREE_TYPE (decl));
232 : 40 : tmp = gfc_advance_chain (field, CLASS_DATA_FIELD);
233 : 40 : CONSTRUCTOR_APPEND_ELT (init, tmp, data);
234 : :
235 : 40 : tmp = gfc_advance_chain (field, CLASS_VPTR_FIELD);
236 : 40 : CONSTRUCTOR_APPEND_ELT (init, tmp, vptr);
237 : :
238 : 40 : return build_constructor (TREE_TYPE (decl), init);
239 : : }
240 : :
241 : :
242 : : tree
243 : 31038 : gfc_class_data_get (tree decl)
244 : : {
245 : 31038 : tree data;
246 : 31038 : if (POINTER_TYPE_P (TREE_TYPE (decl)))
247 : 5152 : decl = build_fold_indirect_ref_loc (input_location, decl);
248 : 31038 : data = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
249 : : CLASS_DATA_FIELD);
250 : 31038 : return fold_build3_loc (input_location, COMPONENT_REF,
251 : 31038 : TREE_TYPE (data), decl, data,
252 : 31038 : NULL_TREE);
253 : : }
254 : :
255 : :
256 : : tree
257 : 43604 : gfc_class_vptr_get (tree decl)
258 : : {
259 : 43604 : tree vptr;
260 : : /* For class arrays decl may be a temporary descriptor handle, the vptr is
261 : : then available through the saved descriptor. */
262 : 26569 : if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
263 : 45291 : && GFC_DECL_SAVED_DESCRIPTOR (decl))
264 : 1220 : decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
265 : 43604 : if (POINTER_TYPE_P (TREE_TYPE (decl)))
266 : 2273 : decl = build_fold_indirect_ref_loc (input_location, decl);
267 : 43604 : vptr = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
268 : : CLASS_VPTR_FIELD);
269 : 43604 : return fold_build3_loc (input_location, COMPONENT_REF,
270 : 43604 : TREE_TYPE (vptr), decl, vptr,
271 : 43604 : NULL_TREE);
272 : : }
273 : :
274 : :
275 : : tree
276 : 6332 : gfc_class_len_get (tree decl)
277 : : {
278 : 6332 : tree len;
279 : : /* For class arrays decl may be a temporary descriptor handle, the len is
280 : : then available through the saved descriptor. */
281 : 4508 : if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
282 : 6581 : && GFC_DECL_SAVED_DESCRIPTOR (decl))
283 : 85 : decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
284 : 6332 : if (POINTER_TYPE_P (TREE_TYPE (decl)))
285 : 656 : decl = build_fold_indirect_ref_loc (input_location, decl);
286 : 6332 : len = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
287 : : CLASS_LEN_FIELD);
288 : 6332 : return fold_build3_loc (input_location, COMPONENT_REF,
289 : 6332 : TREE_TYPE (len), decl, len,
290 : 6332 : NULL_TREE);
291 : : }
292 : :
293 : :
294 : : /* Try to get the _len component of a class. When the class is not unlimited
295 : : poly, i.e. no _len field exists, then return a zero node. */
296 : :
297 : : static tree
298 : 4700 : gfc_class_len_or_zero_get (tree decl)
299 : : {
300 : 4700 : tree len;
301 : : /* For class arrays decl may be a temporary descriptor handle, the vptr is
302 : : then available through the saved descriptor. */
303 : 2795 : if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
304 : 4742 : && GFC_DECL_SAVED_DESCRIPTOR (decl))
305 : 0 : decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
306 : 4700 : if (POINTER_TYPE_P (TREE_TYPE (decl)))
307 : 0 : decl = build_fold_indirect_ref_loc (input_location, decl);
308 : 4700 : len = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
309 : : CLASS_LEN_FIELD);
310 : 6437 : return len != NULL_TREE ? fold_build3_loc (input_location, COMPONENT_REF,
311 : 1737 : TREE_TYPE (len), decl, len,
312 : : NULL_TREE)
313 : 2963 : : build_zero_cst (gfc_charlen_type_node);
314 : : }
315 : :
316 : :
317 : : tree
318 : 4541 : gfc_resize_class_size_with_len (stmtblock_t * block, tree class_expr, tree size)
319 : : {
320 : 4541 : tree tmp;
321 : 4541 : tree tmp2;
322 : 4541 : tree type;
323 : :
324 : 4541 : tmp = gfc_class_len_or_zero_get (class_expr);
325 : :
326 : : /* Include the len value in the element size if present. */
327 : 4541 : if (!integer_zerop (tmp))
328 : : {
329 : 1578 : type = TREE_TYPE (size);
330 : 1578 : if (block)
331 : : {
332 : 913 : size = gfc_evaluate_now (size, block);
333 : 913 : tmp = gfc_evaluate_now (fold_convert (type , tmp), block);
334 : : }
335 : : else
336 : 665 : tmp = fold_convert (type , tmp);
337 : 1578 : tmp2 = fold_build2_loc (input_location, MULT_EXPR,
338 : : type, size, tmp);
339 : 1578 : tmp = fold_build2_loc (input_location, GT_EXPR,
340 : : logical_type_node, tmp,
341 : : build_zero_cst (type));
342 : 1578 : size = fold_build3_loc (input_location, COND_EXPR,
343 : : type, tmp, tmp2, size);
344 : : }
345 : : else
346 : : return size;
347 : :
348 : 1578 : if (block)
349 : 913 : size = gfc_evaluate_now (size, block);
350 : :
351 : : return size;
352 : : }
353 : :
354 : :
355 : : /* Get the specified FIELD from the VPTR. */
356 : :
357 : : static tree
358 : 20118 : vptr_field_get (tree vptr, int fieldno)
359 : : {
360 : 20118 : tree field;
361 : 20118 : vptr = build_fold_indirect_ref_loc (input_location, vptr);
362 : 20118 : field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (vptr)),
363 : : fieldno);
364 : 20118 : field = fold_build3_loc (input_location, COMPONENT_REF,
365 : 20118 : TREE_TYPE (field), vptr, field,
366 : : NULL_TREE);
367 : 20118 : gcc_assert (field);
368 : 20118 : return field;
369 : : }
370 : :
371 : :
372 : : /* Get the field from the class' vptr. */
373 : :
374 : : static tree
375 : 9682 : class_vtab_field_get (tree decl, int fieldno)
376 : : {
377 : 9682 : tree vptr;
378 : 9682 : vptr = gfc_class_vptr_get (decl);
379 : 9682 : return vptr_field_get (vptr, fieldno);
380 : : }
381 : :
382 : :
383 : : /* Define a macro for creating the class_vtab_* and vptr_* accessors in
384 : : unison. */
385 : : #define VTAB_GET_FIELD_GEN(name, field) tree \
386 : : gfc_class_vtab_## name ##_get (tree cl) \
387 : : { \
388 : : return class_vtab_field_get (cl, field); \
389 : : } \
390 : : \
391 : : tree \
392 : : gfc_vptr_## name ##_get (tree vptr) \
393 : : { \
394 : : return vptr_field_get (vptr, field); \
395 : : }
396 : :
397 : 171 : VTAB_GET_FIELD_GEN (hash, VTABLE_HASH_FIELD)
398 : 0 : VTAB_GET_FIELD_GEN (extends, VTABLE_EXTENDS_FIELD)
399 : 0 : VTAB_GET_FIELD_GEN (def_init, VTABLE_DEF_INIT_FIELD)
400 : 4189 : VTAB_GET_FIELD_GEN (copy, VTABLE_COPY_FIELD)
401 : 1745 : VTAB_GET_FIELD_GEN (final, VTABLE_FINAL_FIELD)
402 : 484 : VTAB_GET_FIELD_GEN (deallocate, VTABLE_DEALLOCATE_FIELD)
403 : : #undef VTAB_GET_FIELD_GEN
404 : :
405 : : /* The size field is returned as an array index type. Therefore treat
406 : : it and only it specially. */
407 : :
408 : : tree
409 : 7730 : gfc_class_vtab_size_get (tree cl)
410 : : {
411 : 7730 : tree size;
412 : 7730 : size = class_vtab_field_get (cl, VTABLE_SIZE_FIELD);
413 : : /* Always return size as an array index type. */
414 : 7730 : size = fold_convert (gfc_array_index_type, size);
415 : 7730 : gcc_assert (size);
416 : 7730 : return size;
417 : : }
418 : :
419 : : tree
420 : 5799 : gfc_vptr_size_get (tree vptr)
421 : : {
422 : 5799 : tree size;
423 : 5799 : size = vptr_field_get (vptr, VTABLE_SIZE_FIELD);
424 : : /* Always return size as an array index type. */
425 : 5799 : size = fold_convert (gfc_array_index_type, size);
426 : 5799 : gcc_assert (size);
427 : 5799 : return size;
428 : : }
429 : :
430 : :
431 : : #undef CLASS_DATA_FIELD
432 : : #undef CLASS_VPTR_FIELD
433 : : #undef CLASS_LEN_FIELD
434 : : #undef VTABLE_HASH_FIELD
435 : : #undef VTABLE_SIZE_FIELD
436 : : #undef VTABLE_EXTENDS_FIELD
437 : : #undef VTABLE_DEF_INIT_FIELD
438 : : #undef VTABLE_COPY_FIELD
439 : : #undef VTABLE_FINAL_FIELD
440 : :
441 : :
442 : : /* IF ts is null (default), search for the last _class ref in the chain
443 : : of references of the expression and cut the chain there. Although
444 : : this routine is similiar to class.cc:gfc_add_component_ref (), there
445 : : is a significant difference: gfc_add_component_ref () concentrates
446 : : on an array ref that is the last ref in the chain and is oblivious
447 : : to the kind of refs following.
448 : : ELSE IF ts is non-null the cut is at the class entity or component
449 : : that is followed by an array reference, which is not an element.
450 : : These calls come from trans-array.cc:build_class_array_ref, which
451 : : handles scalarized class array references.*/
452 : :
453 : : gfc_expr *
454 : 8839 : gfc_find_and_cut_at_last_class_ref (gfc_expr *e, bool is_mold,
455 : : gfc_typespec **ts)
456 : : {
457 : 8839 : gfc_expr *base_expr;
458 : 8839 : gfc_ref *ref, *class_ref, *tail = NULL, *array_ref;
459 : :
460 : : /* Find the last class reference. */
461 : 8839 : class_ref = NULL;
462 : 8839 : array_ref = NULL;
463 : :
464 : 8839 : if (ts)
465 : : {
466 : 387 : if (e->symtree
467 : 362 : && e->symtree->n.sym->ts.type == BT_CLASS)
468 : 362 : *ts = &e->symtree->n.sym->ts;
469 : : else
470 : 25 : *ts = NULL;
471 : : }
472 : :
473 : 22091 : for (ref = e->ref; ref; ref = ref->next)
474 : : {
475 : 13624 : if (ts)
476 : : {
477 : 942 : if (ref->type == REF_COMPONENT
478 : 442 : && ref->u.c.component->ts.type == BT_CLASS
479 : 0 : && ref->next && ref->next->type == REF_COMPONENT
480 : 0 : && !strcmp (ref->next->u.c.component->name, "_data")
481 : 0 : && ref->next->next
482 : 0 : && ref->next->next->type == REF_ARRAY
483 : 0 : && ref->next->next->u.ar.type != AR_ELEMENT)
484 : : {
485 : 0 : *ts = &ref->u.c.component->ts;
486 : 0 : class_ref = ref;
487 : 0 : break;
488 : : }
489 : :
490 : 942 : if (ref->next == NULL)
491 : : break;
492 : : }
493 : : else
494 : : {
495 : 12682 : if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
496 : 12682 : array_ref = ref;
497 : :
498 : 12682 : if (ref->type == REF_COMPONENT
499 : 7715 : && ref->u.c.component->ts.type == BT_CLASS)
500 : : {
501 : : /* Component to the right of a part reference with nonzero
502 : : rank must not have the ALLOCATABLE attribute. If attempts
503 : : are made to reference such a component reference, an error
504 : : results followed by an ICE. */
505 : 1537 : if (array_ref
506 : 10 : && CLASS_DATA (ref->u.c.component)->attr.allocatable)
507 : : return NULL;
508 : : class_ref = ref;
509 : : }
510 : : }
511 : : }
512 : :
513 : 8829 : if (ts && *ts == NULL)
514 : : return NULL;
515 : :
516 : : /* Remove and store all subsequent references after the
517 : : CLASS reference. */
518 : 8804 : if (class_ref)
519 : : {
520 : 1347 : tail = class_ref->next;
521 : 1347 : class_ref->next = NULL;
522 : : }
523 : 7457 : else if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
524 : : {
525 : 7439 : tail = e->ref;
526 : 7439 : e->ref = NULL;
527 : : }
528 : :
529 : 8804 : if (is_mold)
530 : 61 : base_expr = gfc_expr_to_initialize (e);
531 : : else
532 : 8743 : base_expr = gfc_copy_expr (e);
533 : :
534 : : /* Restore the original tail expression. */
535 : 8804 : if (class_ref)
536 : : {
537 : 1347 : gfc_free_ref_list (class_ref->next);
538 : 1347 : class_ref->next = tail;
539 : : }
540 : 7457 : else if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
541 : : {
542 : 7439 : gfc_free_ref_list (e->ref);
543 : 7439 : e->ref = tail;
544 : : }
545 : : return base_expr;
546 : : }
547 : :
548 : : /* Reset the vptr to the declared type, e.g. after deallocation.
549 : : Use the variable in CLASS_CONTAINER if available. Otherwise, recreate
550 : : one with e or class_type. At least one of the two has to be set. The
551 : : generated assignment code is added at the end of BLOCK. */
552 : :
553 : : void
554 : 10380 : gfc_reset_vptr (stmtblock_t *block, gfc_expr *e, tree class_container,
555 : : gfc_symbol *class_type)
556 : : {
557 : 10380 : tree vptr = NULL_TREE;
558 : :
559 : 10380 : if (class_container != NULL_TREE)
560 : 6113 : vptr = gfc_get_vptr_from_expr (class_container);
561 : :
562 : 6113 : if (vptr == NULL_TREE)
563 : : {
564 : 4274 : gfc_se se;
565 : 4274 : gcc_assert (e);
566 : :
567 : : /* Evaluate the expression and obtain the vptr from it. */
568 : 4274 : gfc_init_se (&se, NULL);
569 : 4274 : if (e->rank)
570 : 2101 : gfc_conv_expr_descriptor (&se, e);
571 : : else
572 : 2173 : gfc_conv_expr (&se, e);
573 : 4274 : gfc_add_block_to_block (block, &se.pre);
574 : :
575 : 4274 : vptr = gfc_get_vptr_from_expr (se.expr);
576 : : }
577 : :
578 : : /* If a vptr is not found, we can do nothing more. */
579 : 4274 : if (vptr == NULL_TREE)
580 : : return;
581 : :
582 : 10370 : if (UNLIMITED_POLY (e)
583 : 9424 : || UNLIMITED_POLY (class_type)
584 : : /* When the class_type's source is not a symbol (e.g. a component's ts),
585 : : then look at the _data-components type. */
586 : 1441 : || (class_type != NULL && class_type->ts.type == BT_UNKNOWN
587 : 1441 : && class_type->components && class_type->components->ts.u.derived
588 : 1435 : && class_type->components->ts.u.derived->attr.unlimited_polymorphic))
589 : 1080 : gfc_add_modify (block, vptr, build_int_cst (TREE_TYPE (vptr), 0));
590 : : else
591 : : {
592 : 9290 : gfc_symbol *vtab, *type = nullptr;
593 : 9290 : tree vtable;
594 : :
595 : 9290 : if (e)
596 : 7983 : type = e->ts.u.derived;
597 : 1307 : else if (class_type)
598 : : {
599 : 1307 : if (class_type->ts.type == BT_CLASS)
600 : 0 : type = CLASS_DATA (class_type)->ts.u.derived;
601 : : else
602 : : type = class_type;
603 : : }
604 : 7983 : gcc_assert (type);
605 : : /* Return the vptr to the address of the declared type. */
606 : 9290 : vtab = gfc_find_derived_vtab (type);
607 : 9290 : vtable = vtab->backend_decl;
608 : 9290 : if (vtable == NULL_TREE)
609 : 70 : vtable = gfc_get_symbol_decl (vtab);
610 : 9290 : vtable = gfc_build_addr_expr (NULL, vtable);
611 : 9290 : vtable = fold_convert (TREE_TYPE (vptr), vtable);
612 : 9290 : gfc_add_modify (block, vptr, vtable);
613 : : }
614 : : }
615 : :
616 : : /* Set the vptr of a class in to from the type given in from. If from is NULL,
617 : : then reset the vptr to the default or to. */
618 : :
619 : : void
620 : 216 : gfc_class_set_vptr (stmtblock_t *block, tree to, tree from)
621 : : {
622 : 216 : tree tmp, vptr_ref;
623 : 216 : gfc_symbol *type;
624 : :
625 : 216 : vptr_ref = gfc_get_vptr_from_expr (to);
626 : 252 : if (POINTER_TYPE_P (TREE_TYPE (from))
627 : 216 : && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (from))))
628 : : {
629 : 44 : gfc_add_modify (block, vptr_ref,
630 : 22 : fold_convert (TREE_TYPE (vptr_ref),
631 : : gfc_get_vptr_from_expr (from)));
632 : 238 : return;
633 : : }
634 : 194 : tmp = gfc_get_vptr_from_expr (from);
635 : 194 : if (tmp)
636 : : {
637 : 158 : gfc_add_modify (block, vptr_ref,
638 : 158 : fold_convert (TREE_TYPE (vptr_ref), tmp));
639 : 158 : return;
640 : : }
641 : 36 : if (VAR_P (from)
642 : 36 : && strncmp (IDENTIFIER_POINTER (DECL_NAME (from)), "__vtab", 6) == 0)
643 : : {
644 : 36 : gfc_add_modify (block, vptr_ref,
645 : 36 : gfc_build_addr_expr (TREE_TYPE (vptr_ref), from));
646 : 36 : return;
647 : : }
648 : 0 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (from)))
649 : 0 : && GFC_CLASS_TYPE_P (
650 : : TREE_TYPE (TREE_OPERAND (TREE_OPERAND (from, 0), 0))))
651 : : {
652 : 0 : gfc_add_modify (block, vptr_ref,
653 : 0 : fold_convert (TREE_TYPE (vptr_ref),
654 : : gfc_get_vptr_from_expr (TREE_OPERAND (
655 : : TREE_OPERAND (from, 0), 0))));
656 : 0 : return;
657 : : }
658 : :
659 : : /* If nothing of the above matches, set the vtype according to the type. */
660 : 0 : tmp = TREE_TYPE (from);
661 : 0 : if (POINTER_TYPE_P (tmp))
662 : 0 : tmp = TREE_TYPE (tmp);
663 : 0 : gfc_find_symbol (IDENTIFIER_POINTER (TYPE_NAME (tmp)), gfc_current_ns, 1,
664 : : &type);
665 : 0 : tmp = gfc_find_derived_vtab (type)->backend_decl;
666 : 0 : gcc_assert (tmp);
667 : 0 : gfc_add_modify (block, vptr_ref,
668 : 0 : gfc_build_addr_expr (TREE_TYPE (vptr_ref), tmp));
669 : : }
670 : :
671 : : /* Reset the len for unlimited polymorphic objects. */
672 : :
673 : : void
674 : 563 : gfc_reset_len (stmtblock_t *block, gfc_expr *expr)
675 : : {
676 : 563 : gfc_expr *e;
677 : 563 : gfc_se se_len;
678 : 563 : e = gfc_find_and_cut_at_last_class_ref (expr);
679 : 563 : if (e == NULL)
680 : 0 : return;
681 : 563 : gfc_add_len_component (e);
682 : 563 : gfc_init_se (&se_len, NULL);
683 : 563 : gfc_conv_expr (&se_len, e);
684 : 563 : gfc_add_modify (block, se_len.expr,
685 : 563 : fold_convert (TREE_TYPE (se_len.expr), integer_zero_node));
686 : 563 : gfc_free_expr (e);
687 : : }
688 : :
689 : :
690 : : /* Obtain the last class reference in a gfc_expr. Return NULL_TREE if no class
691 : : reference is found. Note that it is up to the caller to avoid using this
692 : : for expressions other than variables. */
693 : :
694 : : tree
695 : 1272 : gfc_get_class_from_gfc_expr (gfc_expr *e)
696 : : {
697 : 1272 : gfc_expr *class_expr;
698 : 1272 : gfc_se cse;
699 : 1272 : class_expr = gfc_find_and_cut_at_last_class_ref (e);
700 : 1272 : if (class_expr == NULL)
701 : : return NULL_TREE;
702 : 1272 : gfc_init_se (&cse, NULL);
703 : 1272 : gfc_conv_expr (&cse, class_expr);
704 : 1272 : gfc_free_expr (class_expr);
705 : 1272 : return cse.expr;
706 : : }
707 : :
708 : :
709 : : /* Obtain the last class reference in an expression.
710 : : Return NULL_TREE if no class reference is found. */
711 : :
712 : : tree
713 : 102645 : gfc_get_class_from_expr (tree expr)
714 : : {
715 : 102645 : tree tmp;
716 : 102645 : tree type;
717 : :
718 : 267466 : for (tmp = expr; tmp; tmp = TREE_OPERAND (tmp, 0))
719 : : {
720 : 267466 : if (CONSTANT_CLASS_P (tmp))
721 : : return NULL_TREE;
722 : :
723 : 267429 : type = TREE_TYPE (tmp);
724 : 311095 : while (type)
725 : : {
726 : 303489 : if (GFC_CLASS_TYPE_P (type))
727 : : return tmp;
728 : 284803 : if (type != TYPE_CANONICAL (type))
729 : 43666 : type = TYPE_CANONICAL (type);
730 : : else
731 : : type = NULL_TREE;
732 : : }
733 : 248743 : if (VAR_P (tmp) || TREE_CODE (tmp) == PARM_DECL)
734 : : break;
735 : : }
736 : :
737 : 83922 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
738 : 55038 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
739 : :
740 : 83922 : if (GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
741 : : return tmp;
742 : :
743 : : return NULL_TREE;
744 : : }
745 : :
746 : :
747 : : /* Obtain the vptr of the last class reference in an expression.
748 : : Return NULL_TREE if no class reference is found. */
749 : :
750 : : tree
751 : 10987 : gfc_get_vptr_from_expr (tree expr)
752 : : {
753 : 10987 : tree tmp;
754 : :
755 : 10987 : tmp = gfc_get_class_from_expr (expr);
756 : :
757 : 10987 : if (tmp != NULL_TREE)
758 : 10934 : return gfc_class_vptr_get (tmp);
759 : :
760 : : return NULL_TREE;
761 : : }
762 : :
763 : : void
764 : 1988 : gfc_class_array_data_assign (stmtblock_t *block, tree lhs_desc, tree rhs_desc,
765 : : bool lhs_type)
766 : : {
767 : 1988 : tree tmp, tmp2, type;
768 : :
769 : 1988 : gfc_conv_descriptor_data_set (block, lhs_desc,
770 : : gfc_conv_descriptor_data_get (rhs_desc));
771 : 1988 : gfc_conv_descriptor_offset_set (block, lhs_desc,
772 : : gfc_conv_descriptor_offset_get (rhs_desc));
773 : :
774 : 1988 : gfc_add_modify (block, gfc_conv_descriptor_dtype (lhs_desc),
775 : : gfc_conv_descriptor_dtype (rhs_desc));
776 : :
777 : : /* Assign the dimension as range-ref. */
778 : 1988 : tmp = gfc_get_descriptor_dimension (lhs_desc);
779 : 1988 : tmp2 = gfc_get_descriptor_dimension (rhs_desc);
780 : :
781 : 1988 : type = lhs_type ? TREE_TYPE (tmp) : TREE_TYPE (tmp2);
782 : 1988 : tmp = build4_loc (input_location, ARRAY_RANGE_REF, type, tmp,
783 : : gfc_index_zero_node, NULL_TREE, NULL_TREE);
784 : 1988 : tmp2 = build4_loc (input_location, ARRAY_RANGE_REF, type, tmp2,
785 : : gfc_index_zero_node, NULL_TREE, NULL_TREE);
786 : 1988 : gfc_add_modify (block, tmp, tmp2);
787 : 1988 : }
788 : :
789 : : /* Takes a derived type expression and returns the address of a temporary
790 : : class object of the 'declared' type. If opt_vptr_src is not NULL, this is
791 : : used for the temporary class object.
792 : : optional_alloc_ptr is false when the dummy is neither allocatable
793 : : nor a pointer; that's only relevant for the optional handling.
794 : : The optional argument 'derived_array' is used to preserve the parmse
795 : : expression for deallocation of allocatable components. Assumed rank
796 : : formal arguments made this necessary. */
797 : : void
798 : 4678 : gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym,
799 : : tree opt_vptr_src, bool optional,
800 : : bool optional_alloc_ptr, const char *proc_name,
801 : : tree *derived_array)
802 : : {
803 : 4678 : tree cond_optional = NULL_TREE;
804 : 4678 : gfc_ss *ss;
805 : 4678 : tree ctree;
806 : 4678 : tree var;
807 : 4678 : tree tmp;
808 : 4678 : tree packed = NULL_TREE;
809 : :
810 : : /* The derived type needs to be converted to a temporary CLASS object. */
811 : 4678 : tmp = gfc_typenode_for_spec (&fsym->ts);
812 : 4678 : var = gfc_create_var (tmp, "class");
813 : :
814 : : /* Set the vptr. */
815 : 4678 : if (opt_vptr_src)
816 : 116 : gfc_class_set_vptr (&parmse->pre, var, opt_vptr_src);
817 : : else
818 : 4562 : gfc_reset_vptr (&parmse->pre, e, var);
819 : :
820 : : /* Now set the data field. */
821 : 4678 : ctree = gfc_class_data_get (var);
822 : :
823 : 4678 : if (flag_coarray == GFC_FCOARRAY_LIB && CLASS_DATA (fsym)->attr.codimension)
824 : : {
825 : 2 : tree token;
826 : 2 : tmp = gfc_get_tree_for_caf_expr (e);
827 : 2 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
828 : 1 : tmp = build_fold_indirect_ref (tmp);
829 : 2 : gfc_get_caf_token_offset (parmse, &token, nullptr, tmp, NULL_TREE, e);
830 : 2 : gfc_add_modify (&parmse->pre, gfc_conv_descriptor_token (ctree), token);
831 : : }
832 : :
833 : 4678 : if (optional)
834 : 576 : cond_optional = gfc_conv_expr_present (e->symtree->n.sym);
835 : :
836 : : /* Set the _len as early as possible. */
837 : 4678 : if (fsym->ts.u.derived->components->ts.type == BT_DERIVED
838 : 4678 : && fsym->ts.u.derived->components->ts.u.derived->attr
839 : 4678 : .unlimited_polymorphic)
840 : : {
841 : : /* Take care about initializing the _len component correctly. */
842 : 374 : tree len_tree = gfc_class_len_get (var);
843 : 374 : if (UNLIMITED_POLY (e))
844 : : {
845 : 12 : gfc_expr *len;
846 : 12 : gfc_se se;
847 : :
848 : 12 : len = gfc_find_and_cut_at_last_class_ref (e);
849 : 12 : gfc_add_len_component (len);
850 : 12 : gfc_init_se (&se, NULL);
851 : 12 : gfc_conv_expr (&se, len);
852 : 12 : if (optional)
853 : 0 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se.expr),
854 : : cond_optional, se.expr,
855 : 0 : fold_convert (TREE_TYPE (se.expr),
856 : : integer_zero_node));
857 : : else
858 : 12 : tmp = se.expr;
859 : 12 : gfc_free_expr (len);
860 : 12 : }
861 : : else
862 : 362 : tmp = integer_zero_node;
863 : 374 : gfc_add_modify (&parmse->pre, len_tree,
864 : 374 : fold_convert (TREE_TYPE (len_tree), tmp));
865 : : }
866 : :
867 : 4678 : if (parmse->expr && POINTER_TYPE_P (TREE_TYPE (parmse->expr)))
868 : : {
869 : : /* If there is a ready made pointer to a derived type, use it
870 : : rather than evaluating the expression again. */
871 : 498 : tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
872 : 498 : gfc_add_modify (&parmse->pre, ctree, tmp);
873 : : }
874 : 4180 : else if (parmse->ss && parmse->ss->info && parmse->ss->info->useflags)
875 : : {
876 : : /* For an array reference in an elemental procedure call we need
877 : : to retain the ss to provide the scalarized array reference. */
878 : 252 : gfc_conv_expr_reference (parmse, e);
879 : 252 : tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
880 : 252 : if (optional)
881 : 0 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
882 : : cond_optional, tmp,
883 : 0 : fold_convert (TREE_TYPE (tmp), null_pointer_node));
884 : 252 : gfc_add_modify (&parmse->pre, ctree, tmp);
885 : : }
886 : : else
887 : : {
888 : 3928 : ss = gfc_walk_expr (e);
889 : 3928 : if (ss == gfc_ss_terminator)
890 : : {
891 : 2717 : parmse->ss = NULL;
892 : 2717 : gfc_conv_expr_reference (parmse, e);
893 : :
894 : : /* Scalar to an assumed-rank array. */
895 : 2717 : if (fsym->ts.u.derived->components->as)
896 : : {
897 : 321 : tree type;
898 : 321 : type = get_scalar_to_descriptor_type (parmse->expr,
899 : : gfc_expr_attr (e));
900 : 321 : gfc_add_modify (&parmse->pre, gfc_conv_descriptor_dtype (ctree),
901 : : gfc_get_dtype (type));
902 : 321 : if (optional)
903 : 192 : parmse->expr = build3_loc (input_location, COND_EXPR,
904 : 96 : TREE_TYPE (parmse->expr),
905 : : cond_optional, parmse->expr,
906 : 96 : fold_convert (TREE_TYPE (parmse->expr),
907 : : null_pointer_node));
908 : 321 : gfc_conv_descriptor_data_set (&parmse->pre, ctree, parmse->expr);
909 : : }
910 : : else
911 : : {
912 : 2396 : tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
913 : 2396 : if (optional)
914 : 132 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
915 : : cond_optional, tmp,
916 : 132 : fold_convert (TREE_TYPE (tmp),
917 : : null_pointer_node));
918 : 2396 : gfc_add_modify (&parmse->pre, ctree, tmp);
919 : : }
920 : : }
921 : : else
922 : : {
923 : 1211 : stmtblock_t block;
924 : 1211 : gfc_init_block (&block);
925 : 1211 : gfc_ref *ref;
926 : 1211 : int dim;
927 : 1211 : tree lbshift = NULL_TREE;
928 : :
929 : : /* Array refs with sections indicate, that a for a formal argument
930 : : expecting contiguous repacking needs to be done. */
931 : 2271 : for (ref = e->ref; ref; ref = ref->next)
932 : 1210 : if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
933 : : break;
934 : 1211 : if (IS_CLASS_ARRAY (fsym)
935 : 1103 : && (CLASS_DATA (fsym)->as->type == AS_EXPLICIT
936 : 845 : || CLASS_DATA (fsym)->as->type == AS_ASSUMED_SIZE)
937 : 354 : && (ref || e->rank != fsym->ts.u.derived->components->as->rank))
938 : 144 : fsym->attr.contiguous = 1;
939 : :
940 : : /* Detect any array references with vector subscripts. */
941 : 2415 : for (ref = e->ref; ref; ref = ref->next)
942 : 1210 : if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT
943 : 1168 : && ref->u.ar.type != AR_FULL)
944 : : {
945 : 336 : for (dim = 0; dim < ref->u.ar.dimen; dim++)
946 : 192 : if (ref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
947 : : break;
948 : 150 : if (dim < ref->u.ar.dimen)
949 : : break;
950 : : }
951 : : /* Array references with vector subscripts and non-variable
952 : : expressions need be converted to a one-based descriptor. */
953 : 1211 : if (ref || e->expr_type != EXPR_VARIABLE)
954 : 49 : lbshift = gfc_index_one_node;
955 : :
956 : 1211 : parmse->expr = var;
957 : 1211 : gfc_conv_array_parameter (parmse, e, false, fsym, proc_name, nullptr,
958 : : &lbshift, &packed);
959 : :
960 : 1211 : if (derived_array && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (parmse->expr)))
961 : : {
962 : 1115 : *derived_array
963 : 1115 : = gfc_create_var (TREE_TYPE (parmse->expr), "array");
964 : 1115 : gfc_add_modify (&block, *derived_array, parmse->expr);
965 : : }
966 : :
967 : 1211 : if (optional)
968 : : {
969 : 348 : tmp = gfc_finish_block (&block);
970 : :
971 : 348 : gfc_init_block (&block);
972 : 348 : gfc_conv_descriptor_data_set (&block, ctree, null_pointer_node);
973 : 348 : if (derived_array && *derived_array != NULL_TREE)
974 : 348 : gfc_conv_descriptor_data_set (&block, *derived_array,
975 : : null_pointer_node);
976 : :
977 : 348 : tmp = build3_v (COND_EXPR, cond_optional, tmp,
978 : : gfc_finish_block (&block));
979 : 348 : gfc_add_expr_to_block (&parmse->pre, tmp);
980 : : }
981 : : else
982 : 863 : gfc_add_block_to_block (&parmse->pre, &block);
983 : : }
984 : : }
985 : :
986 : : /* Pass the address of the class object. */
987 : 4678 : if (packed)
988 : 96 : parmse->expr = packed;
989 : : else
990 : 4582 : parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
991 : :
992 : 4678 : if (optional && optional_alloc_ptr)
993 : 84 : parmse->expr
994 : 84 : = build3_loc (input_location, COND_EXPR, TREE_TYPE (parmse->expr),
995 : : cond_optional, parmse->expr,
996 : 84 : fold_convert (TREE_TYPE (parmse->expr), null_pointer_node));
997 : 4678 : }
998 : :
999 : : /* Create a new class container, which is required as scalar coarrays
1000 : : have an array descriptor while normal scalars haven't. Optionally,
1001 : : NULL pointer checks are added if the argument is OPTIONAL. */
1002 : :
1003 : : static void
1004 : 48 : class_scalar_coarray_to_class (gfc_se *parmse, gfc_expr *e,
1005 : : gfc_typespec class_ts, bool optional)
1006 : : {
1007 : 48 : tree var, ctree, tmp;
1008 : 48 : stmtblock_t block;
1009 : 48 : gfc_ref *ref;
1010 : 48 : gfc_ref *class_ref;
1011 : :
1012 : 48 : gfc_init_block (&block);
1013 : :
1014 : 48 : class_ref = NULL;
1015 : 144 : for (ref = e->ref; ref; ref = ref->next)
1016 : : {
1017 : 96 : if (ref->type == REF_COMPONENT
1018 : 48 : && ref->u.c.component->ts.type == BT_CLASS)
1019 : 96 : class_ref = ref;
1020 : : }
1021 : :
1022 : 48 : if (class_ref == NULL
1023 : 48 : && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
1024 : 48 : tmp = e->symtree->n.sym->backend_decl;
1025 : : else
1026 : : {
1027 : : /* Remove everything after the last class reference, convert the
1028 : : expression and then recover its tailend once more. */
1029 : 0 : gfc_se tmpse;
1030 : 0 : ref = class_ref->next;
1031 : 0 : class_ref->next = NULL;
1032 : 0 : gfc_init_se (&tmpse, NULL);
1033 : 0 : gfc_conv_expr (&tmpse, e);
1034 : 0 : class_ref->next = ref;
1035 : 0 : tmp = tmpse.expr;
1036 : : }
1037 : :
1038 : 48 : var = gfc_typenode_for_spec (&class_ts);
1039 : 48 : var = gfc_create_var (var, "class");
1040 : :
1041 : 48 : ctree = gfc_class_vptr_get (var);
1042 : 96 : gfc_add_modify (&block, ctree,
1043 : 48 : fold_convert (TREE_TYPE (ctree), gfc_class_vptr_get (tmp)));
1044 : :
1045 : 48 : ctree = gfc_class_data_get (var);
1046 : 48 : tmp = gfc_conv_descriptor_data_get (
1047 : 48 : gfc_class_data_get (GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (tmp)))
1048 : : ? tmp
1049 : 24 : : GFC_DECL_SAVED_DESCRIPTOR (tmp)));
1050 : 48 : gfc_add_modify (&block, ctree, fold_convert (TREE_TYPE (ctree), tmp));
1051 : :
1052 : : /* Pass the address of the class object. */
1053 : 48 : parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
1054 : :
1055 : 48 : if (optional)
1056 : : {
1057 : 48 : tree cond = gfc_conv_expr_present (e->symtree->n.sym);
1058 : 48 : tree tmp2;
1059 : :
1060 : 48 : tmp = gfc_finish_block (&block);
1061 : :
1062 : 48 : gfc_init_block (&block);
1063 : 48 : tmp2 = gfc_class_data_get (var);
1064 : 48 : gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
1065 : : null_pointer_node));
1066 : 48 : tmp2 = gfc_finish_block (&block);
1067 : :
1068 : 48 : tmp = build3_loc (input_location, COND_EXPR, void_type_node,
1069 : : cond, tmp, tmp2);
1070 : 48 : gfc_add_expr_to_block (&parmse->pre, tmp);
1071 : : }
1072 : : else
1073 : 0 : gfc_add_block_to_block (&parmse->pre, &block);
1074 : 48 : }
1075 : :
1076 : :
1077 : : /* Takes an intrinsic type expression and returns the address of a temporary
1078 : : class object of the 'declared' type. */
1079 : : void
1080 : 864 : gfc_conv_intrinsic_to_class (gfc_se *parmse, gfc_expr *e,
1081 : : gfc_typespec class_ts)
1082 : : {
1083 : 864 : gfc_symbol *vtab;
1084 : 864 : gfc_ss *ss;
1085 : 864 : tree ctree;
1086 : 864 : tree var;
1087 : 864 : tree tmp;
1088 : 864 : int dim;
1089 : 864 : bool unlimited_poly;
1090 : :
1091 : 1728 : unlimited_poly = class_ts.type == BT_CLASS
1092 : 864 : && class_ts.u.derived->components->ts.type == BT_DERIVED
1093 : 864 : && class_ts.u.derived->components->ts.u.derived
1094 : 864 : ->attr.unlimited_polymorphic;
1095 : :
1096 : : /* The intrinsic type needs to be converted to a temporary
1097 : : CLASS object. */
1098 : 864 : tmp = gfc_typenode_for_spec (&class_ts);
1099 : 864 : var = gfc_create_var (tmp, "class");
1100 : :
1101 : : /* Force a temporary for component or substring references. */
1102 : 864 : if (unlimited_poly
1103 : 864 : && class_ts.u.derived->components->attr.dimension
1104 : 605 : && !class_ts.u.derived->components->attr.allocatable
1105 : 605 : && !class_ts.u.derived->components->attr.class_pointer
1106 : 1469 : && is_subref_array (e))
1107 : 17 : parmse->force_tmp = 1;
1108 : :
1109 : : /* Set the vptr. */
1110 : 864 : ctree = gfc_class_vptr_get (var);
1111 : :
1112 : 864 : vtab = gfc_find_vtab (&e->ts);
1113 : 864 : gcc_assert (vtab);
1114 : 864 : tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
1115 : 864 : gfc_add_modify (&parmse->pre, ctree,
1116 : 864 : fold_convert (TREE_TYPE (ctree), tmp));
1117 : :
1118 : : /* Now set the data field. */
1119 : 864 : ctree = gfc_class_data_get (var);
1120 : 864 : if (parmse->ss && parmse->ss->info->useflags)
1121 : : {
1122 : : /* For an array reference in an elemental procedure call we need
1123 : : to retain the ss to provide the scalarized array reference. */
1124 : 36 : gfc_conv_expr_reference (parmse, e);
1125 : 36 : tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
1126 : 36 : gfc_add_modify (&parmse->pre, ctree, tmp);
1127 : : }
1128 : : else
1129 : : {
1130 : 828 : ss = gfc_walk_expr (e);
1131 : 828 : if (ss == gfc_ss_terminator)
1132 : : {
1133 : 247 : parmse->ss = NULL;
1134 : 247 : gfc_conv_expr_reference (parmse, e);
1135 : 247 : if (class_ts.u.derived->components->as
1136 : 24 : && class_ts.u.derived->components->as->type == AS_ASSUMED_RANK)
1137 : : {
1138 : 24 : tmp = gfc_conv_scalar_to_descriptor (parmse, parmse->expr,
1139 : : gfc_expr_attr (e));
1140 : 24 : tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
1141 : 24 : TREE_TYPE (ctree), tmp);
1142 : : }
1143 : : else
1144 : 223 : tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
1145 : 247 : gfc_add_modify (&parmse->pre, ctree, tmp);
1146 : : }
1147 : : else
1148 : : {
1149 : 581 : parmse->ss = ss;
1150 : 581 : parmse->use_offset = 1;
1151 : 581 : gfc_conv_expr_descriptor (parmse, e);
1152 : :
1153 : : /* Array references with vector subscripts and non-variable expressions
1154 : : need be converted to a one-based descriptor. */
1155 : 581 : if (e->expr_type != EXPR_VARIABLE)
1156 : : {
1157 : 368 : for (dim = 0; dim < e->rank; ++dim)
1158 : 193 : gfc_conv_shift_descriptor_lbound (&parmse->pre, parmse->expr,
1159 : : dim, gfc_index_one_node);
1160 : : }
1161 : :
1162 : 581 : if (class_ts.u.derived->components->as->rank != e->rank)
1163 : : {
1164 : 49 : tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
1165 : 49 : TREE_TYPE (ctree), parmse->expr);
1166 : 49 : gfc_add_modify (&parmse->pre, ctree, tmp);
1167 : : }
1168 : : else
1169 : 532 : gfc_add_modify (&parmse->pre, ctree, parmse->expr);
1170 : : }
1171 : : }
1172 : :
1173 : 864 : gcc_assert (class_ts.type == BT_CLASS);
1174 : 864 : if (unlimited_poly)
1175 : : {
1176 : 864 : ctree = gfc_class_len_get (var);
1177 : : /* When the actual arg is a char array, then set the _len component of the
1178 : : unlimited polymorphic entity to the length of the string. */
1179 : 864 : if (e->ts.type == BT_CHARACTER)
1180 : : {
1181 : : /* Start with parmse->string_length because this seems to be set to a
1182 : : correct value more often. */
1183 : 175 : if (parmse->string_length)
1184 : : tmp = parmse->string_length;
1185 : : /* When the string_length is not yet set, then try the backend_decl of
1186 : : the cl. */
1187 : 0 : else if (e->ts.u.cl->backend_decl)
1188 : : tmp = e->ts.u.cl->backend_decl;
1189 : : /* If both of the above approaches fail, then try to generate an
1190 : : expression from the input, which is only feasible currently, when the
1191 : : expression can be evaluated to a constant one. */
1192 : : else
1193 : : {
1194 : : /* Try to simplify the expression. */
1195 : 0 : gfc_simplify_expr (e, 0);
1196 : 0 : if (e->expr_type == EXPR_CONSTANT && !e->ts.u.cl->resolved)
1197 : : {
1198 : : /* Amazingly all data is present to compute the length of a
1199 : : constant string, but the expression is not yet there. */
1200 : 0 : e->ts.u.cl->length = gfc_get_constant_expr (BT_INTEGER,
1201 : : gfc_charlen_int_kind,
1202 : : &e->where);
1203 : 0 : mpz_set_ui (e->ts.u.cl->length->value.integer,
1204 : 0 : e->value.character.length);
1205 : 0 : gfc_conv_const_charlen (e->ts.u.cl);
1206 : 0 : e->ts.u.cl->resolved = 1;
1207 : 0 : tmp = e->ts.u.cl->backend_decl;
1208 : : }
1209 : : else
1210 : : {
1211 : 0 : gfc_error ("Cannot compute the length of the char array "
1212 : : "at %L.", &e->where);
1213 : : }
1214 : : }
1215 : : }
1216 : : else
1217 : 689 : tmp = integer_zero_node;
1218 : :
1219 : 864 : gfc_add_modify (&parmse->pre, ctree, fold_convert (TREE_TYPE (ctree), tmp));
1220 : : }
1221 : :
1222 : : /* Pass the address of the class object. */
1223 : 864 : parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
1224 : 864 : }
1225 : :
1226 : :
1227 : : /* Takes a scalarized class array expression and returns the
1228 : : address of a temporary scalar class object of the 'declared'
1229 : : type.
1230 : : OOP-TODO: This could be improved by adding code that branched on
1231 : : the dynamic type being the same as the declared type. In this case
1232 : : the original class expression can be passed directly.
1233 : : optional_alloc_ptr is false when the dummy is neither allocatable
1234 : : nor a pointer; that's relevant for the optional handling.
1235 : : Set copyback to true if class container's _data and _vtab pointers
1236 : : might get modified. */
1237 : :
1238 : : void
1239 : 3433 : gfc_conv_class_to_class (gfc_se *parmse, gfc_expr *e, gfc_typespec class_ts,
1240 : : bool elemental, bool copyback, bool optional,
1241 : : bool optional_alloc_ptr)
1242 : : {
1243 : 3433 : tree ctree;
1244 : 3433 : tree var;
1245 : 3433 : tree tmp;
1246 : 3433 : tree vptr;
1247 : 3433 : tree cond = NULL_TREE;
1248 : 3433 : tree slen = NULL_TREE;
1249 : 3433 : gfc_ref *ref;
1250 : 3433 : gfc_ref *class_ref;
1251 : 3433 : stmtblock_t block;
1252 : 3433 : bool full_array = false;
1253 : :
1254 : : /* Class transformational function results are the data field of a class
1255 : : temporary and so the class expression can be obtained directly. */
1256 : 3433 : if (e->expr_type == EXPR_FUNCTION
1257 : 162 : && e->value.function.isym
1258 : 30 : && e->value.function.isym->transformational
1259 : 30 : && TREE_CODE (parmse->expr) == COMPONENT_REF
1260 : 3457 : && !GFC_CLASS_TYPE_P (TREE_TYPE (parmse->expr)))
1261 : : {
1262 : 24 : parmse->expr = TREE_OPERAND (parmse->expr, 0);
1263 : 24 : if (!VAR_P (parmse->expr))
1264 : 0 : parmse->expr = gfc_evaluate_now (parmse->expr, &parmse->pre);
1265 : 24 : parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
1266 : 162 : return;
1267 : : }
1268 : :
1269 : 3409 : gfc_init_block (&block);
1270 : :
1271 : 3409 : class_ref = NULL;
1272 : 6839 : for (ref = e->ref; ref; ref = ref->next)
1273 : : {
1274 : 6469 : if (ref->type == REF_COMPONENT
1275 : 3464 : && ref->u.c.component->ts.type == BT_CLASS)
1276 : 6469 : class_ref = ref;
1277 : :
1278 : 6469 : if (ref->next == NULL)
1279 : : break;
1280 : : }
1281 : :
1282 : 3409 : if ((ref == NULL || class_ref == ref)
1283 : 482 : && !(gfc_is_class_array_function (e) && parmse->class_vptr != NULL_TREE)
1284 : 3879 : && (!class_ts.u.derived->components->as
1285 : 379 : || class_ts.u.derived->components->as->rank != -1))
1286 : : return;
1287 : :
1288 : : /* Test for FULL_ARRAY. */
1289 : 3271 : if (e->rank == 0
1290 : 3271 : && ((gfc_expr_attr (e).codimension && gfc_expr_attr (e).dimension)
1291 : 493 : || (class_ts.u.derived->components->as
1292 : 365 : && class_ts.u.derived->components->as->type == AS_ASSUMED_RANK)))
1293 : 407 : full_array = true;
1294 : : else
1295 : 2864 : gfc_is_class_array_ref (e, &full_array);
1296 : :
1297 : : /* The derived type needs to be converted to a temporary
1298 : : CLASS object. */
1299 : 3271 : tmp = gfc_typenode_for_spec (&class_ts);
1300 : 3271 : var = gfc_create_var (tmp, "class");
1301 : :
1302 : : /* Set the data. */
1303 : 3271 : ctree = gfc_class_data_get (var);
1304 : 3271 : if (class_ts.u.derived->components->as
1305 : 3017 : && e->rank != class_ts.u.derived->components->as->rank)
1306 : : {
1307 : 965 : if (e->rank == 0)
1308 : : {
1309 : 356 : tree type = get_scalar_to_descriptor_type (parmse->expr,
1310 : : gfc_expr_attr (e));
1311 : 356 : gfc_add_modify (&block, gfc_conv_descriptor_dtype (ctree),
1312 : : gfc_get_dtype (type));
1313 : :
1314 : 356 : tmp = gfc_class_data_get (parmse->expr);
1315 : 356 : if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
1316 : 12 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
1317 : :
1318 : 356 : gfc_conv_descriptor_data_set (&block, ctree, tmp);
1319 : : }
1320 : : else
1321 : 609 : gfc_class_array_data_assign (&block, ctree, parmse->expr, false);
1322 : : }
1323 : : else
1324 : : {
1325 : 2306 : if (TREE_TYPE (parmse->expr) != TREE_TYPE (ctree))
1326 : 1335 : parmse->expr = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
1327 : 1335 : TREE_TYPE (ctree), parmse->expr);
1328 : 2306 : gfc_add_modify (&block, ctree, parmse->expr);
1329 : : }
1330 : :
1331 : : /* Return the data component, except in the case of scalarized array
1332 : : references, where nullification of the cannot occur and so there
1333 : : is no need. */
1334 : 3271 : if (!elemental && full_array && copyback)
1335 : : {
1336 : 1128 : if (class_ts.u.derived->components->as
1337 : 1128 : && e->rank != class_ts.u.derived->components->as->rank)
1338 : : {
1339 : 270 : if (e->rank == 0)
1340 : : {
1341 : 102 : tmp = gfc_class_data_get (parmse->expr);
1342 : 204 : gfc_add_modify (&parmse->post, tmp,
1343 : 102 : fold_convert (TREE_TYPE (tmp),
1344 : : gfc_conv_descriptor_data_get (ctree)));
1345 : : }
1346 : : else
1347 : 168 : gfc_class_array_data_assign (&parmse->post, parmse->expr, ctree,
1348 : : true);
1349 : : }
1350 : : else
1351 : 858 : gfc_add_modify (&parmse->post, parmse->expr, ctree);
1352 : : }
1353 : :
1354 : : /* Set the vptr. */
1355 : 3271 : ctree = gfc_class_vptr_get (var);
1356 : :
1357 : : /* The vptr is the second field of the actual argument.
1358 : : First we have to find the corresponding class reference. */
1359 : :
1360 : 3271 : tmp = NULL_TREE;
1361 : 3271 : if (gfc_is_class_array_function (e)
1362 : 3271 : && parmse->class_vptr != NULL_TREE)
1363 : : tmp = parmse->class_vptr;
1364 : 3259 : else if (class_ref == NULL
1365 : 2816 : && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
1366 : : {
1367 : 2816 : tmp = e->symtree->n.sym->backend_decl;
1368 : :
1369 : 2816 : if (TREE_CODE (tmp) == FUNCTION_DECL)
1370 : 6 : tmp = gfc_get_fake_result_decl (e->symtree->n.sym, 0);
1371 : :
1372 : 2816 : if (DECL_LANG_SPECIFIC (tmp) && GFC_DECL_SAVED_DESCRIPTOR (tmp))
1373 : 372 : tmp = GFC_DECL_SAVED_DESCRIPTOR (tmp);
1374 : :
1375 : 2816 : slen = build_zero_cst (size_type_node);
1376 : : }
1377 : 443 : else if (parmse->class_container != NULL_TREE)
1378 : : /* Don't redundantly evaluate the expression if the required information
1379 : : is already available. */
1380 : : tmp = parmse->class_container;
1381 : : else
1382 : : {
1383 : : /* Remove everything after the last class reference, convert the
1384 : : expression and then recover its tailend once more. */
1385 : 18 : gfc_se tmpse;
1386 : 18 : ref = class_ref->next;
1387 : 18 : class_ref->next = NULL;
1388 : 18 : gfc_init_se (&tmpse, NULL);
1389 : 18 : gfc_conv_expr (&tmpse, e);
1390 : 18 : class_ref->next = ref;
1391 : 18 : tmp = tmpse.expr;
1392 : 18 : slen = tmpse.string_length;
1393 : : }
1394 : :
1395 : 3271 : gcc_assert (tmp != NULL_TREE);
1396 : :
1397 : : /* Dereference if needs be. */
1398 : 3271 : if (TREE_CODE (TREE_TYPE (tmp)) == REFERENCE_TYPE)
1399 : 320 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
1400 : :
1401 : 3271 : if (!(gfc_is_class_array_function (e) && parmse->class_vptr))
1402 : 3259 : vptr = gfc_class_vptr_get (tmp);
1403 : : else
1404 : : vptr = tmp;
1405 : :
1406 : 3271 : gfc_add_modify (&block, ctree,
1407 : 3271 : fold_convert (TREE_TYPE (ctree), vptr));
1408 : :
1409 : : /* Return the vptr component, except in the case of scalarized array
1410 : : references, where the dynamic type cannot change. */
1411 : 3271 : if (!elemental && full_array && copyback)
1412 : 1128 : gfc_add_modify (&parmse->post, vptr,
1413 : 1128 : fold_convert (TREE_TYPE (vptr), ctree));
1414 : :
1415 : : /* For unlimited polymorphic objects also set the _len component. */
1416 : 3271 : if (class_ts.type == BT_CLASS
1417 : 3271 : && class_ts.u.derived->components
1418 : 3271 : && class_ts.u.derived->components->ts.u
1419 : 3271 : .derived->attr.unlimited_polymorphic)
1420 : : {
1421 : 1013 : ctree = gfc_class_len_get (var);
1422 : 1013 : if (UNLIMITED_POLY (e))
1423 : 817 : tmp = gfc_class_len_get (tmp);
1424 : 196 : else if (e->ts.type == BT_CHARACTER)
1425 : : {
1426 : 0 : gcc_assert (slen != NULL_TREE);
1427 : : tmp = slen;
1428 : : }
1429 : : else
1430 : 196 : tmp = build_zero_cst (size_type_node);
1431 : 1013 : gfc_add_modify (&parmse->pre, ctree,
1432 : 1013 : fold_convert (TREE_TYPE (ctree), tmp));
1433 : :
1434 : : /* Return the len component, except in the case of scalarized array
1435 : : references, where the dynamic type cannot change. */
1436 : 1013 : if (!elemental && full_array && copyback
1437 : 440 : && (UNLIMITED_POLY (e) || VAR_P (tmp)))
1438 : 428 : gfc_add_modify (&parmse->post, tmp,
1439 : 428 : fold_convert (TREE_TYPE (tmp), ctree));
1440 : : }
1441 : :
1442 : 3271 : if (optional)
1443 : : {
1444 : 510 : tree tmp2;
1445 : :
1446 : 510 : cond = gfc_conv_expr_present (e->symtree->n.sym);
1447 : : /* parmse->pre may contain some preparatory instructions for the
1448 : : temporary array descriptor. Those may only be executed when the
1449 : : optional argument is set, therefore add parmse->pre's instructions
1450 : : to block, which is later guarded by an if (optional_arg_given). */
1451 : 510 : gfc_add_block_to_block (&parmse->pre, &block);
1452 : 510 : block.head = parmse->pre.head;
1453 : 510 : parmse->pre.head = NULL_TREE;
1454 : 510 : tmp = gfc_finish_block (&block);
1455 : :
1456 : 510 : if (optional_alloc_ptr)
1457 : 102 : tmp2 = build_empty_stmt (input_location);
1458 : : else
1459 : : {
1460 : 408 : gfc_init_block (&block);
1461 : :
1462 : 408 : tmp2 = gfc_conv_descriptor_data_get (gfc_class_data_get (var));
1463 : 408 : gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
1464 : : null_pointer_node));
1465 : 408 : tmp2 = gfc_finish_block (&block);
1466 : : }
1467 : :
1468 : 510 : tmp = build3_loc (input_location, COND_EXPR, void_type_node,
1469 : : cond, tmp, tmp2);
1470 : 510 : gfc_add_expr_to_block (&parmse->pre, tmp);
1471 : :
1472 : 510 : if (!elemental && full_array && copyback)
1473 : : {
1474 : 30 : tmp2 = build_empty_stmt (input_location);
1475 : 30 : tmp = gfc_finish_block (&parmse->post);
1476 : 30 : tmp = build3_loc (input_location, COND_EXPR, void_type_node,
1477 : : cond, tmp, tmp2);
1478 : 30 : gfc_add_expr_to_block (&parmse->post, tmp);
1479 : : }
1480 : : }
1481 : : else
1482 : 2761 : gfc_add_block_to_block (&parmse->pre, &block);
1483 : :
1484 : : /* Pass the address of the class object. */
1485 : 3271 : parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
1486 : :
1487 : 3271 : if (optional && optional_alloc_ptr)
1488 : 204 : parmse->expr = build3_loc (input_location, COND_EXPR,
1489 : 102 : TREE_TYPE (parmse->expr),
1490 : : cond, parmse->expr,
1491 : 102 : fold_convert (TREE_TYPE (parmse->expr),
1492 : : null_pointer_node));
1493 : : }
1494 : :
1495 : :
1496 : : /* Given a class array declaration and an index, returns the address
1497 : : of the referenced element. */
1498 : :
1499 : : static tree
1500 : 720 : gfc_get_class_array_ref (tree index, tree class_decl, tree data_comp,
1501 : : bool unlimited)
1502 : : {
1503 : 720 : tree data, size, tmp, ctmp, offset, ptr;
1504 : :
1505 : 720 : data = data_comp != NULL_TREE ? data_comp :
1506 : 0 : gfc_class_data_get (class_decl);
1507 : 720 : size = gfc_class_vtab_size_get (class_decl);
1508 : :
1509 : 720 : if (unlimited)
1510 : : {
1511 : 200 : tmp = fold_convert (gfc_array_index_type,
1512 : : gfc_class_len_get (class_decl));
1513 : 200 : ctmp = fold_build2_loc (input_location, MULT_EXPR,
1514 : : gfc_array_index_type, size, tmp);
1515 : 200 : tmp = fold_build2_loc (input_location, GT_EXPR,
1516 : : logical_type_node, tmp,
1517 : 200 : build_zero_cst (TREE_TYPE (tmp)));
1518 : 200 : size = fold_build3_loc (input_location, COND_EXPR,
1519 : : gfc_array_index_type, tmp, ctmp, size);
1520 : : }
1521 : :
1522 : 720 : offset = fold_build2_loc (input_location, MULT_EXPR,
1523 : : gfc_array_index_type,
1524 : : index, size);
1525 : :
1526 : 720 : data = gfc_conv_descriptor_data_get (data);
1527 : 720 : ptr = fold_convert (pvoid_type_node, data);
1528 : 720 : ptr = fold_build_pointer_plus_loc (input_location, ptr, offset);
1529 : 720 : return fold_convert (TREE_TYPE (data), ptr);
1530 : : }
1531 : :
1532 : :
1533 : : /* Copies one class expression to another, assuming that if either
1534 : : 'to' or 'from' are arrays they are packed. Should 'from' be
1535 : : NULL_TREE, the initialization expression for 'to' is used, assuming
1536 : : that the _vptr is set. */
1537 : :
1538 : : tree
1539 : 752 : gfc_copy_class_to_class (tree from, tree to, tree nelems, bool unlimited)
1540 : : {
1541 : 752 : tree fcn;
1542 : 752 : tree fcn_type;
1543 : 752 : tree from_data;
1544 : 752 : tree from_len;
1545 : 752 : tree to_data;
1546 : 752 : tree to_len;
1547 : 752 : tree to_ref;
1548 : 752 : tree from_ref;
1549 : 752 : vec<tree, va_gc> *args;
1550 : 752 : tree tmp;
1551 : 752 : tree stdcopy;
1552 : 752 : tree extcopy;
1553 : 752 : tree index;
1554 : 752 : bool is_from_desc = false, is_to_class = false;
1555 : :
1556 : 752 : args = NULL;
1557 : : /* To prevent warnings on uninitialized variables. */
1558 : 752 : from_len = to_len = NULL_TREE;
1559 : :
1560 : 752 : if (from != NULL_TREE)
1561 : 752 : fcn = gfc_class_vtab_copy_get (from);
1562 : : else
1563 : 0 : fcn = gfc_class_vtab_copy_get (to);
1564 : :
1565 : 752 : fcn_type = TREE_TYPE (TREE_TYPE (fcn));
1566 : :
1567 : 752 : if (from != NULL_TREE)
1568 : : {
1569 : 752 : is_from_desc = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from));
1570 : 752 : if (is_from_desc)
1571 : : {
1572 : 0 : from_data = from;
1573 : 0 : from = GFC_DECL_SAVED_DESCRIPTOR (from);
1574 : : }
1575 : : else
1576 : : {
1577 : : /* Check that from is a class. When the class is part of a coarray,
1578 : : then from is a common pointer and is to be used as is. */
1579 : 1504 : tmp = POINTER_TYPE_P (TREE_TYPE (from))
1580 : 752 : ? build_fold_indirect_ref (from) : from;
1581 : 1504 : from_data =
1582 : 752 : (GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
1583 : 0 : || (DECL_P (tmp) && GFC_DECL_CLASS (tmp)))
1584 : 752 : ? gfc_class_data_get (from) : from;
1585 : 752 : is_from_desc = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data));
1586 : : }
1587 : : }
1588 : : else
1589 : 0 : from_data = gfc_class_vtab_def_init_get (to);
1590 : :
1591 : 752 : if (unlimited)
1592 : : {
1593 : 159 : if (from != NULL_TREE && unlimited)
1594 : 159 : from_len = gfc_class_len_or_zero_get (from);
1595 : : else
1596 : 0 : from_len = build_zero_cst (size_type_node);
1597 : : }
1598 : :
1599 : 752 : if (GFC_CLASS_TYPE_P (TREE_TYPE (to)))
1600 : : {
1601 : 752 : is_to_class = true;
1602 : 752 : to_data = gfc_class_data_get (to);
1603 : 752 : if (unlimited)
1604 : 159 : to_len = gfc_class_len_get (to);
1605 : : }
1606 : : else
1607 : : /* When to is a BT_DERIVED and not a BT_CLASS, then to_data == to. */
1608 : 0 : to_data = to;
1609 : :
1610 : 752 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (to_data)))
1611 : : {
1612 : 360 : stmtblock_t loopbody;
1613 : 360 : stmtblock_t body;
1614 : 360 : stmtblock_t ifbody;
1615 : 360 : gfc_loopinfo loop;
1616 : :
1617 : 360 : gfc_init_block (&body);
1618 : 360 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
1619 : : gfc_array_index_type, nelems,
1620 : : gfc_index_one_node);
1621 : 360 : nelems = gfc_evaluate_now (tmp, &body);
1622 : 360 : index = gfc_create_var (gfc_array_index_type, "S");
1623 : :
1624 : 360 : if (is_from_desc)
1625 : : {
1626 : 360 : from_ref = gfc_get_class_array_ref (index, from, from_data,
1627 : : unlimited);
1628 : 360 : vec_safe_push (args, from_ref);
1629 : : }
1630 : : else
1631 : 0 : vec_safe_push (args, from_data);
1632 : :
1633 : 360 : if (is_to_class)
1634 : 360 : to_ref = gfc_get_class_array_ref (index, to, to_data, unlimited);
1635 : : else
1636 : : {
1637 : 0 : tmp = gfc_conv_array_data (to);
1638 : 0 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
1639 : 0 : to_ref = gfc_build_addr_expr (NULL_TREE,
1640 : : gfc_build_array_ref (tmp, index, to));
1641 : : }
1642 : 360 : vec_safe_push (args, to_ref);
1643 : :
1644 : : /* Add bounds check. */
1645 : 360 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) > 0 && is_from_desc)
1646 : : {
1647 : 25 : const char *name = "<<unknown>>";
1648 : 25 : int dim, rank;
1649 : :
1650 : 25 : if (DECL_P (to))
1651 : 0 : name = IDENTIFIER_POINTER (DECL_NAME (to));
1652 : :
1653 : 25 : rank = GFC_TYPE_ARRAY_RANK (TREE_TYPE (from_data));
1654 : 55 : for (dim = 1; dim <= rank; dim++)
1655 : : {
1656 : 30 : tree from_len, to_len, cond;
1657 : 30 : char *msg;
1658 : :
1659 : 30 : from_len = gfc_conv_descriptor_size (from_data, dim);
1660 : 30 : from_len = fold_convert (long_integer_type_node, from_len);
1661 : 30 : to_len = gfc_conv_descriptor_size (to_data, dim);
1662 : 30 : to_len = fold_convert (long_integer_type_node, to_len);
1663 : 30 : msg = xasprintf ("Array bound mismatch for dimension %d "
1664 : : "of array '%s' (%%ld/%%ld)",
1665 : : dim, name);
1666 : 30 : cond = fold_build2_loc (input_location, NE_EXPR,
1667 : : logical_type_node, from_len, to_len);
1668 : 30 : gfc_trans_runtime_check (true, false, cond, &body,
1669 : : NULL, msg, to_len, from_len);
1670 : 30 : free (msg);
1671 : : }
1672 : : }
1673 : :
1674 : 360 : tmp = build_call_vec (fcn_type, fcn, args);
1675 : :
1676 : : /* Build the body of the loop. */
1677 : 360 : gfc_init_block (&loopbody);
1678 : 360 : gfc_add_expr_to_block (&loopbody, tmp);
1679 : :
1680 : : /* Build the loop and return. */
1681 : 360 : gfc_init_loopinfo (&loop);
1682 : 360 : loop.dimen = 1;
1683 : 360 : loop.from[0] = gfc_index_zero_node;
1684 : 360 : loop.loopvar[0] = index;
1685 : 360 : loop.to[0] = nelems;
1686 : 360 : gfc_trans_scalarizing_loops (&loop, &loopbody);
1687 : 360 : gfc_init_block (&ifbody);
1688 : 360 : gfc_add_block_to_block (&ifbody, &loop.pre);
1689 : 360 : stdcopy = gfc_finish_block (&ifbody);
1690 : : /* In initialization mode from_len is a constant zero. */
1691 : 360 : if (unlimited && !integer_zerop (from_len))
1692 : : {
1693 : 100 : vec_safe_push (args, from_len);
1694 : 100 : vec_safe_push (args, to_len);
1695 : 100 : tmp = build_call_vec (fcn_type, fcn, args);
1696 : : /* Build the body of the loop. */
1697 : 100 : gfc_init_block (&loopbody);
1698 : 100 : gfc_add_expr_to_block (&loopbody, tmp);
1699 : :
1700 : : /* Build the loop and return. */
1701 : 100 : gfc_init_loopinfo (&loop);
1702 : 100 : loop.dimen = 1;
1703 : 100 : loop.from[0] = gfc_index_zero_node;
1704 : 100 : loop.loopvar[0] = index;
1705 : 100 : loop.to[0] = nelems;
1706 : 100 : gfc_trans_scalarizing_loops (&loop, &loopbody);
1707 : 100 : gfc_init_block (&ifbody);
1708 : 100 : gfc_add_block_to_block (&ifbody, &loop.pre);
1709 : 100 : extcopy = gfc_finish_block (&ifbody);
1710 : :
1711 : 100 : tmp = fold_build2_loc (input_location, GT_EXPR,
1712 : : logical_type_node, from_len,
1713 : 100 : build_zero_cst (TREE_TYPE (from_len)));
1714 : 100 : tmp = fold_build3_loc (input_location, COND_EXPR,
1715 : : void_type_node, tmp, extcopy, stdcopy);
1716 : 100 : gfc_add_expr_to_block (&body, tmp);
1717 : 100 : tmp = gfc_finish_block (&body);
1718 : : }
1719 : : else
1720 : : {
1721 : 260 : gfc_add_expr_to_block (&body, stdcopy);
1722 : 260 : tmp = gfc_finish_block (&body);
1723 : : }
1724 : 360 : gfc_cleanup_loop (&loop);
1725 : : }
1726 : : else
1727 : : {
1728 : 392 : gcc_assert (!is_from_desc);
1729 : 392 : vec_safe_push (args, from_data);
1730 : 392 : vec_safe_push (args, to_data);
1731 : 392 : stdcopy = build_call_vec (fcn_type, fcn, args);
1732 : :
1733 : : /* In initialization mode from_len is a constant zero. */
1734 : 392 : if (unlimited && !integer_zerop (from_len))
1735 : : {
1736 : 59 : vec_safe_push (args, from_len);
1737 : 59 : vec_safe_push (args, to_len);
1738 : 59 : extcopy = build_call_vec (fcn_type, unshare_expr (fcn), args);
1739 : 59 : tmp = fold_build2_loc (input_location, GT_EXPR,
1740 : : logical_type_node, from_len,
1741 : 59 : build_zero_cst (TREE_TYPE (from_len)));
1742 : 59 : tmp = fold_build3_loc (input_location, COND_EXPR,
1743 : : void_type_node, tmp, extcopy, stdcopy);
1744 : : }
1745 : : else
1746 : : tmp = stdcopy;
1747 : : }
1748 : :
1749 : : /* Only copy _def_init to to_data, when it is not a NULL-pointer. */
1750 : 752 : if (from == NULL_TREE)
1751 : : {
1752 : 0 : tree cond;
1753 : 0 : cond = fold_build2_loc (input_location, NE_EXPR,
1754 : : logical_type_node,
1755 : : from_data, null_pointer_node);
1756 : 0 : tmp = fold_build3_loc (input_location, COND_EXPR,
1757 : : void_type_node, cond,
1758 : : tmp, build_empty_stmt (input_location));
1759 : : }
1760 : :
1761 : 752 : return tmp;
1762 : : }
1763 : :
1764 : :
1765 : : static tree
1766 : 106 : gfc_trans_class_array_init_assign (gfc_expr *rhs, gfc_expr *lhs, gfc_expr *obj)
1767 : : {
1768 : 106 : gfc_actual_arglist *actual;
1769 : 106 : gfc_expr *ppc;
1770 : 106 : gfc_code *ppc_code;
1771 : 106 : tree res;
1772 : :
1773 : 106 : actual = gfc_get_actual_arglist ();
1774 : 106 : actual->expr = gfc_copy_expr (rhs);
1775 : 106 : actual->next = gfc_get_actual_arglist ();
1776 : 106 : actual->next->expr = gfc_copy_expr (lhs);
1777 : 106 : ppc = gfc_copy_expr (obj);
1778 : 106 : gfc_add_vptr_component (ppc);
1779 : 106 : gfc_add_component_ref (ppc, "_copy");
1780 : 106 : ppc_code = gfc_get_code (EXEC_CALL);
1781 : 106 : ppc_code->resolved_sym = ppc->symtree->n.sym;
1782 : : /* Although '_copy' is set to be elemental in class.cc, it is
1783 : : not staying that way. Find out why, sometime.... */
1784 : 106 : ppc_code->resolved_sym->attr.elemental = 1;
1785 : 106 : ppc_code->ext.actual = actual;
1786 : 106 : ppc_code->expr1 = ppc;
1787 : : /* Since '_copy' is elemental, the scalarizer will take care
1788 : : of arrays in gfc_trans_call. */
1789 : 106 : res = gfc_trans_call (ppc_code, false, NULL, NULL, false);
1790 : 106 : gfc_free_statements (ppc_code);
1791 : :
1792 : 106 : if (UNLIMITED_POLY(obj))
1793 : : {
1794 : : /* Check if rhs is non-NULL. */
1795 : 24 : gfc_se src;
1796 : 24 : gfc_init_se (&src, NULL);
1797 : 24 : gfc_conv_expr (&src, rhs);
1798 : 24 : src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
1799 : 24 : tree cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1800 : 24 : src.expr, fold_convert (TREE_TYPE (src.expr),
1801 : : null_pointer_node));
1802 : 24 : res = build3_loc (input_location, COND_EXPR, TREE_TYPE (res), cond, res,
1803 : : build_empty_stmt (input_location));
1804 : : }
1805 : :
1806 : 106 : return res;
1807 : : }
1808 : :
1809 : : /* Special case for initializing a polymorphic dummy with INTENT(OUT).
1810 : : A MEMCPY is needed to copy the full data from the default initializer
1811 : : of the dynamic type. */
1812 : :
1813 : : tree
1814 : 441 : gfc_trans_class_init_assign (gfc_code *code)
1815 : : {
1816 : 441 : stmtblock_t block;
1817 : 441 : tree tmp;
1818 : 441 : bool cmp_flag = true;
1819 : 441 : gfc_se dst,src,memsz;
1820 : 441 : gfc_expr *lhs, *rhs, *sz;
1821 : 441 : gfc_component *cmp;
1822 : 441 : gfc_symbol *sym;
1823 : 441 : gfc_ref *ref;
1824 : :
1825 : 441 : gfc_start_block (&block);
1826 : :
1827 : 441 : lhs = gfc_copy_expr (code->expr1);
1828 : :
1829 : 441 : rhs = gfc_copy_expr (code->expr1);
1830 : 441 : gfc_add_vptr_component (rhs);
1831 : :
1832 : : /* Make sure that the component backend_decls have been built, which
1833 : : will not have happened if the derived types concerned have not
1834 : : been referenced. */
1835 : 441 : gfc_get_derived_type (rhs->ts.u.derived);
1836 : 441 : gfc_add_def_init_component (rhs);
1837 : : /* The _def_init is always scalar. */
1838 : 441 : rhs->rank = 0;
1839 : :
1840 : : /* Check def_init for initializers. If this is an INTENT(OUT) dummy with all
1841 : : default initializer components NULL, use the passed value even though
1842 : : F2018(8.5.10) asserts that it should considered to be undefined. This is
1843 : : needed for consistency with other brands. */
1844 : 441 : sym = code->expr1->expr_type == EXPR_VARIABLE ? code->expr1->symtree->n.sym
1845 : : : NULL;
1846 : 441 : if (code->op != EXEC_ALLOCATE
1847 : 380 : && sym && sym->attr.dummy
1848 : 380 : && sym->attr.intent == INTENT_OUT)
1849 : : {
1850 : 380 : ref = rhs->ref;
1851 : 760 : while (ref && ref->next)
1852 : : ref = ref->next;
1853 : 380 : cmp = ref->u.c.component->ts.u.derived->components;
1854 : 603 : for (; cmp; cmp = cmp->next)
1855 : : {
1856 : 427 : if (cmp->initializer)
1857 : : break;
1858 : 223 : else if (!cmp->next)
1859 : 146 : cmp_flag = false;
1860 : : }
1861 : : }
1862 : :
1863 : 441 : if (code->expr1->ts.type == BT_CLASS
1864 : 418 : && CLASS_DATA (code->expr1)->attr.dimension)
1865 : : {
1866 : 106 : gfc_array_spec *tmparr = gfc_get_array_spec ();
1867 : 106 : *tmparr = *CLASS_DATA (code->expr1)->as;
1868 : : /* Adding the array ref to the class expression results in correct
1869 : : indexing to the dynamic type. */
1870 : 106 : gfc_add_full_array_ref (lhs, tmparr);
1871 : 106 : tmp = gfc_trans_class_array_init_assign (rhs, lhs, code->expr1);
1872 : 106 : }
1873 : 335 : else if (cmp_flag)
1874 : : {
1875 : : /* Scalar initialization needs the _data component. */
1876 : 202 : gfc_add_data_component (lhs);
1877 : 202 : sz = gfc_copy_expr (code->expr1);
1878 : 202 : gfc_add_vptr_component (sz);
1879 : 202 : gfc_add_size_component (sz);
1880 : :
1881 : 202 : gfc_init_se (&dst, NULL);
1882 : 202 : gfc_init_se (&src, NULL);
1883 : 202 : gfc_init_se (&memsz, NULL);
1884 : 202 : gfc_conv_expr (&dst, lhs);
1885 : 202 : gfc_conv_expr (&src, rhs);
1886 : 202 : gfc_conv_expr (&memsz, sz);
1887 : 202 : gfc_add_block_to_block (&block, &src.pre);
1888 : 202 : src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
1889 : :
1890 : 202 : tmp = gfc_build_memcpy_call (dst.expr, src.expr, memsz.expr);
1891 : :
1892 : 202 : if (UNLIMITED_POLY(code->expr1))
1893 : : {
1894 : : /* Check if _def_init is non-NULL. */
1895 : 7 : tree cond = fold_build2_loc (input_location, NE_EXPR,
1896 : : logical_type_node, src.expr,
1897 : 7 : fold_convert (TREE_TYPE (src.expr),
1898 : : null_pointer_node));
1899 : 7 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
1900 : : tmp, build_empty_stmt (input_location));
1901 : : }
1902 : : }
1903 : : else
1904 : 133 : tmp = build_empty_stmt (input_location);
1905 : :
1906 : 441 : if (code->expr1->symtree->n.sym->attr.dummy
1907 : 390 : && (code->expr1->symtree->n.sym->attr.optional
1908 : 384 : || code->expr1->symtree->n.sym->ns->proc_name->attr.entry_master))
1909 : : {
1910 : 6 : tree present = gfc_conv_expr_present (code->expr1->symtree->n.sym);
1911 : 6 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
1912 : : present, tmp,
1913 : : build_empty_stmt (input_location));
1914 : : }
1915 : :
1916 : 441 : gfc_add_expr_to_block (&block, tmp);
1917 : 441 : gfc_free_expr (lhs);
1918 : 441 : gfc_free_expr (rhs);
1919 : :
1920 : 441 : return gfc_finish_block (&block);
1921 : : }
1922 : :
1923 : :
1924 : : /* Class valued elemental function calls or class array elements arriving
1925 : : in gfc_trans_scalar_assign come here. Wherever possible the vptr copy
1926 : : is used to ensure that the rhs dynamic type is assigned to the lhs. */
1927 : :
1928 : : static bool
1929 : 746 : trans_scalar_class_assign (stmtblock_t *block, gfc_se *lse, gfc_se *rse)
1930 : : {
1931 : 746 : tree fcn;
1932 : 746 : tree rse_expr;
1933 : 746 : tree class_data;
1934 : 746 : tree tmp;
1935 : 746 : tree zero;
1936 : 746 : tree cond;
1937 : 746 : tree final_cond;
1938 : 746 : stmtblock_t inner_block;
1939 : 746 : bool is_descriptor;
1940 : 746 : bool not_call_expr = TREE_CODE (rse->expr) != CALL_EXPR;
1941 : 746 : bool not_lhs_array_type;
1942 : :
1943 : : /* Temporaries arising from dependencies in assignment get cast as a
1944 : : character type of the dynamic size of the rhs. Use the vptr copy
1945 : : for this case. */
1946 : 746 : tmp = TREE_TYPE (lse->expr);
1947 : 746 : not_lhs_array_type = !(tmp && TREE_CODE (tmp) == ARRAY_TYPE
1948 : 0 : && TYPE_MAX_VALUE (TYPE_DOMAIN (tmp)) != NULL_TREE);
1949 : :
1950 : : /* Use ordinary assignment if the rhs is not a call expression or
1951 : : the lhs is not a class entity or an array(ie. character) type. */
1952 : 698 : if ((not_call_expr && gfc_get_class_from_expr (lse->expr) == NULL_TREE)
1953 : 1007 : && not_lhs_array_type)
1954 : : return false;
1955 : :
1956 : : /* Ordinary assignment can be used if both sides are class expressions
1957 : : since the dynamic type is preserved by copying the vptr. This
1958 : : should only occur, where temporaries are involved. */
1959 : 485 : if (GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
1960 : 485 : && GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
1961 : : return false;
1962 : :
1963 : : /* Fix the class expression and the class data of the rhs. */
1964 : 424 : if (!GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr))
1965 : 424 : || not_call_expr)
1966 : : {
1967 : 424 : tmp = gfc_get_class_from_expr (rse->expr);
1968 : 424 : if (tmp == NULL_TREE)
1969 : : return false;
1970 : 134 : rse_expr = gfc_evaluate_now (tmp, block);
1971 : : }
1972 : : else
1973 : 0 : rse_expr = gfc_evaluate_now (rse->expr, block);
1974 : :
1975 : 134 : class_data = gfc_class_data_get (rse_expr);
1976 : :
1977 : : /* Check that the rhs data is not null. */
1978 : 134 : is_descriptor = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (class_data));
1979 : 134 : if (is_descriptor)
1980 : 134 : class_data = gfc_conv_descriptor_data_get (class_data);
1981 : 134 : class_data = gfc_evaluate_now (class_data, block);
1982 : :
1983 : 134 : zero = build_int_cst (TREE_TYPE (class_data), 0);
1984 : 134 : cond = fold_build2_loc (input_location, NE_EXPR,
1985 : : logical_type_node,
1986 : : class_data, zero);
1987 : :
1988 : : /* Copy the rhs to the lhs. */
1989 : 134 : fcn = gfc_vptr_copy_get (gfc_class_vptr_get (rse_expr));
1990 : 134 : fcn = build_fold_indirect_ref_loc (input_location, fcn);
1991 : 134 : tmp = gfc_evaluate_now (gfc_build_addr_expr (NULL, rse->expr), block);
1992 : 134 : tmp = is_descriptor ? tmp : class_data;
1993 : 134 : tmp = build_call_expr_loc (input_location, fcn, 2, tmp,
1994 : : gfc_build_addr_expr (NULL, lse->expr));
1995 : 134 : gfc_add_expr_to_block (block, tmp);
1996 : :
1997 : : /* Only elemental function results need to be finalised and freed. */
1998 : 134 : if (not_call_expr)
1999 : : return true;
2000 : :
2001 : : /* Finalize the class data if needed. */
2002 : 0 : gfc_init_block (&inner_block);
2003 : 0 : fcn = gfc_vptr_final_get (gfc_class_vptr_get (rse_expr));
2004 : 0 : zero = build_int_cst (TREE_TYPE (fcn), 0);
2005 : 0 : final_cond = fold_build2_loc (input_location, NE_EXPR,
2006 : : logical_type_node, fcn, zero);
2007 : 0 : fcn = build_fold_indirect_ref_loc (input_location, fcn);
2008 : 0 : tmp = build_call_expr_loc (input_location, fcn, 1, class_data);
2009 : 0 : tmp = build3_v (COND_EXPR, final_cond,
2010 : : tmp, build_empty_stmt (input_location));
2011 : 0 : gfc_add_expr_to_block (&inner_block, tmp);
2012 : :
2013 : : /* Free the class data. */
2014 : 0 : tmp = gfc_call_free (class_data);
2015 : 0 : tmp = build3_v (COND_EXPR, cond, tmp,
2016 : : build_empty_stmt (input_location));
2017 : 0 : gfc_add_expr_to_block (&inner_block, tmp);
2018 : :
2019 : : /* Finish the inner block and subject it to the condition on the
2020 : : class data being non-zero. */
2021 : 0 : tmp = gfc_finish_block (&inner_block);
2022 : 0 : tmp = build3_v (COND_EXPR, cond, tmp,
2023 : : build_empty_stmt (input_location));
2024 : 0 : gfc_add_expr_to_block (block, tmp);
2025 : :
2026 : 0 : return true;
2027 : : }
2028 : :
2029 : : /* End of prototype trans-class.c */
2030 : :
2031 : :
2032 : : static void
2033 : 11591 : realloc_lhs_warning (bt type, bool array, locus *where)
2034 : : {
2035 : 11591 : if (array && type != BT_CLASS && type != BT_DERIVED && warn_realloc_lhs)
2036 : 25 : gfc_warning (OPT_Wrealloc_lhs,
2037 : : "Code for reallocating the allocatable array at %L will "
2038 : : "be added", where);
2039 : 11566 : else if (warn_realloc_lhs_all)
2040 : 4 : gfc_warning (OPT_Wrealloc_lhs_all,
2041 : : "Code for reallocating the allocatable variable at %L "
2042 : : "will be added", where);
2043 : 11591 : }
2044 : :
2045 : :
2046 : : static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
2047 : : gfc_expr *);
2048 : :
2049 : : /* Copy the scalarization loop variables. */
2050 : :
2051 : : static void
2052 : 1233408 : gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
2053 : : {
2054 : 1233408 : dest->ss = src->ss;
2055 : 1233408 : dest->loop = src->loop;
2056 : 1233408 : }
2057 : :
2058 : :
2059 : : /* Initialize a simple expression holder.
2060 : :
2061 : : Care must be taken when multiple se are created with the same parent.
2062 : : The child se must be kept in sync. The easiest way is to delay creation
2063 : : of a child se until after the previous se has been translated. */
2064 : :
2065 : : void
2066 : 4417277 : gfc_init_se (gfc_se * se, gfc_se * parent)
2067 : : {
2068 : 4417277 : memset (se, 0, sizeof (gfc_se));
2069 : 4417277 : gfc_init_block (&se->pre);
2070 : 4417277 : gfc_init_block (&se->finalblock);
2071 : 4417277 : gfc_init_block (&se->post);
2072 : :
2073 : 4417277 : se->parent = parent;
2074 : :
2075 : 4417277 : if (parent)
2076 : 1233408 : gfc_copy_se_loopvars (se, parent);
2077 : 4417277 : }
2078 : :
2079 : :
2080 : : /* Advances to the next SS in the chain. Use this rather than setting
2081 : : se->ss = se->ss->next because all the parents needs to be kept in sync.
2082 : : See gfc_init_se. */
2083 : :
2084 : : void
2085 : 233723 : gfc_advance_se_ss_chain (gfc_se * se)
2086 : : {
2087 : 233723 : gfc_se *p;
2088 : :
2089 : 233723 : gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
2090 : :
2091 : : p = se;
2092 : : /* Walk down the parent chain. */
2093 : 615265 : while (p != NULL)
2094 : : {
2095 : : /* Simple consistency check. */
2096 : 381542 : gcc_assert (p->parent == NULL || p->parent->ss == p->ss
2097 : : || p->parent->ss->nested_ss == p->ss);
2098 : :
2099 : 381542 : p->ss = p->ss->next;
2100 : :
2101 : 381542 : p = p->parent;
2102 : : }
2103 : 233723 : }
2104 : :
2105 : :
2106 : : /* Ensures the result of the expression as either a temporary variable
2107 : : or a constant so that it can be used repeatedly. */
2108 : :
2109 : : void
2110 : 7958 : gfc_make_safe_expr (gfc_se * se)
2111 : : {
2112 : 7958 : tree var;
2113 : :
2114 : 7958 : if (CONSTANT_CLASS_P (se->expr))
2115 : : return;
2116 : :
2117 : : /* We need a temporary for this result. */
2118 : 206 : var = gfc_create_var (TREE_TYPE (se->expr), NULL);
2119 : 206 : gfc_add_modify (&se->pre, var, se->expr);
2120 : 206 : se->expr = var;
2121 : : }
2122 : :
2123 : :
2124 : : /* Return an expression which determines if a dummy parameter is present.
2125 : : Also used for arguments to procedures with multiple entry points. */
2126 : :
2127 : : tree
2128 : 11312 : gfc_conv_expr_present (gfc_symbol * sym, bool use_saved_desc)
2129 : : {
2130 : 11312 : tree decl, orig_decl, cond;
2131 : :
2132 : 11312 : gcc_assert (sym->attr.dummy);
2133 : 11312 : orig_decl = decl = gfc_get_symbol_decl (sym);
2134 : :
2135 : : /* Intrinsic scalars and derived types with VALUE attribute which are passed
2136 : : by value use a hidden argument to denote the presence status. */
2137 : 11312 : if (sym->attr.value && !sym->attr.dimension && sym->ts.type != BT_CLASS)
2138 : : {
2139 : 1040 : char name[GFC_MAX_SYMBOL_LEN + 2];
2140 : 1040 : tree tree_name;
2141 : :
2142 : 1040 : gcc_assert (TREE_CODE (decl) == PARM_DECL);
2143 : 1040 : name[0] = '.';
2144 : 1040 : strcpy (&name[1], sym->name);
2145 : 1040 : tree_name = get_identifier (name);
2146 : :
2147 : : /* Walk function argument list to find hidden arg. */
2148 : 1040 : cond = DECL_ARGUMENTS (DECL_CONTEXT (decl));
2149 : 5296 : for ( ; cond != NULL_TREE; cond = TREE_CHAIN (cond))
2150 : 5296 : if (DECL_NAME (cond) == tree_name
2151 : 5296 : && DECL_ARTIFICIAL (cond))
2152 : : break;
2153 : :
2154 : 1040 : gcc_assert (cond);
2155 : 1040 : return cond;
2156 : : }
2157 : :
2158 : : /* Assumed-shape arrays use a local variable for the array data;
2159 : : the actual PARAM_DECL is in a saved decl. As the local variable
2160 : : is NULL, it can be checked instead, unless use_saved_desc is
2161 : : requested. */
2162 : :
2163 : 10272 : if (use_saved_desc && TREE_CODE (decl) != PARM_DECL)
2164 : : {
2165 : 821 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
2166 : : || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
2167 : 821 : decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
2168 : : }
2169 : :
2170 : 10272 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, decl,
2171 : 10272 : fold_convert (TREE_TYPE (decl), null_pointer_node));
2172 : :
2173 : : /* Fortran 2008 allows to pass null pointers and non-associated pointers
2174 : : as actual argument to denote absent dummies. For array descriptors,
2175 : : we thus also need to check the array descriptor. For BT_CLASS, it
2176 : : can also occur for scalars and F2003 due to type->class wrapping and
2177 : : class->class wrapping. Note further that BT_CLASS always uses an
2178 : : array descriptor for arrays, also for explicit-shape/assumed-size.
2179 : : For assumed-rank arrays, no local variable is generated, hence,
2180 : : the following also applies with !use_saved_desc. */
2181 : :
2182 : 10272 : if ((use_saved_desc || TREE_CODE (orig_decl) == PARM_DECL)
2183 : 7351 : && !sym->attr.allocatable
2184 : 6185 : && ((sym->ts.type != BT_CLASS && !sym->attr.pointer)
2185 : 2266 : || (sym->ts.type == BT_CLASS
2186 : 1041 : && !CLASS_DATA (sym)->attr.allocatable
2187 : 567 : && !CLASS_DATA (sym)->attr.class_pointer))
2188 : 4126 : && ((gfc_option.allow_std & GFC_STD_F2008) != 0
2189 : 6 : || sym->ts.type == BT_CLASS))
2190 : : {
2191 : 4120 : tree tmp;
2192 : :
2193 : 4120 : if ((sym->as && (sym->as->type == AS_ASSUMED_SHAPE
2194 : 1455 : || sym->as->type == AS_ASSUMED_RANK
2195 : 1373 : || sym->attr.codimension))
2196 : 3259 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
2197 : : {
2198 : 1032 : tmp = build_fold_indirect_ref_loc (input_location, decl);
2199 : 1032 : if (sym->ts.type == BT_CLASS)
2200 : 171 : tmp = gfc_class_data_get (tmp);
2201 : 1032 : tmp = gfc_conv_array_data (tmp);
2202 : : }
2203 : 3088 : else if (sym->ts.type == BT_CLASS)
2204 : 36 : tmp = gfc_class_data_get (decl);
2205 : : else
2206 : : tmp = NULL_TREE;
2207 : :
2208 : 1068 : if (tmp != NULL_TREE)
2209 : : {
2210 : 1068 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
2211 : 1068 : fold_convert (TREE_TYPE (tmp), null_pointer_node));
2212 : 1068 : cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2213 : : logical_type_node, cond, tmp);
2214 : : }
2215 : : }
2216 : :
2217 : : return cond;
2218 : : }
2219 : :
2220 : :
2221 : : /* Converts a missing, dummy argument into a null or zero. */
2222 : :
2223 : : void
2224 : 843 : gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
2225 : : {
2226 : 843 : tree present;
2227 : 843 : tree tmp;
2228 : :
2229 : 843 : present = gfc_conv_expr_present (arg->symtree->n.sym);
2230 : :
2231 : 843 : if (kind > 0)
2232 : : {
2233 : : /* Create a temporary and convert it to the correct type. */
2234 : 54 : tmp = gfc_get_int_type (kind);
2235 : 54 : tmp = fold_convert (tmp, build_fold_indirect_ref_loc (input_location,
2236 : : se->expr));
2237 : :
2238 : : /* Test for a NULL value. */
2239 : 54 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
2240 : 54 : tmp, fold_convert (TREE_TYPE (tmp), integer_one_node));
2241 : 54 : tmp = gfc_evaluate_now (tmp, &se->pre);
2242 : 54 : se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
2243 : : }
2244 : : else
2245 : : {
2246 : 789 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
2247 : : present, se->expr,
2248 : 789 : build_zero_cst (TREE_TYPE (se->expr)));
2249 : 789 : tmp = gfc_evaluate_now (tmp, &se->pre);
2250 : 789 : se->expr = tmp;
2251 : : }
2252 : :
2253 : 843 : if (ts.type == BT_CHARACTER)
2254 : : {
2255 : : /* Handle deferred-length dummies that pass the character length by
2256 : : reference so that the value can be returned. */
2257 : 244 : if (ts.deferred && INDIRECT_REF_P (se->string_length))
2258 : : {
2259 : 18 : tmp = gfc_build_addr_expr (NULL_TREE, se->string_length);
2260 : 18 : tmp = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
2261 : : present, tmp, null_pointer_node);
2262 : 18 : tmp = gfc_evaluate_now (tmp, &se->pre);
2263 : 18 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
2264 : : }
2265 : : else
2266 : : {
2267 : 226 : tmp = build_int_cst (gfc_charlen_type_node, 0);
2268 : 226 : tmp = fold_build3_loc (input_location, COND_EXPR,
2269 : : gfc_charlen_type_node,
2270 : : present, se->string_length, tmp);
2271 : 226 : tmp = gfc_evaluate_now (tmp, &se->pre);
2272 : : }
2273 : 244 : se->string_length = tmp;
2274 : : }
2275 : 843 : return;
2276 : : }
2277 : :
2278 : :
2279 : : /* Get the character length of an expression, looking through gfc_refs
2280 : : if necessary. */
2281 : :
2282 : : tree
2283 : 19860 : gfc_get_expr_charlen (gfc_expr *e)
2284 : : {
2285 : 19860 : gfc_ref *r;
2286 : 19860 : tree length;
2287 : 19860 : tree previous = NULL_TREE;
2288 : 19860 : gfc_se se;
2289 : :
2290 : 19860 : gcc_assert (e->expr_type == EXPR_VARIABLE
2291 : : && e->ts.type == BT_CHARACTER);
2292 : :
2293 : 19860 : length = NULL; /* To silence compiler warning. */
2294 : :
2295 : 19860 : if (is_subref_array (e) && e->ts.u.cl->length)
2296 : : {
2297 : 767 : gfc_se tmpse;
2298 : 767 : gfc_init_se (&tmpse, NULL);
2299 : 767 : gfc_conv_expr_type (&tmpse, e->ts.u.cl->length, gfc_charlen_type_node);
2300 : 767 : e->ts.u.cl->backend_decl = tmpse.expr;
2301 : 767 : return tmpse.expr;
2302 : : }
2303 : :
2304 : : /* First candidate: if the variable is of type CHARACTER, the
2305 : : expression's length could be the length of the character
2306 : : variable. */
2307 : 19093 : if (e->symtree->n.sym->ts.type == BT_CHARACTER)
2308 : 18805 : length = e->symtree->n.sym->ts.u.cl->backend_decl;
2309 : :
2310 : : /* Look through the reference chain for component references. */
2311 : 38325 : for (r = e->ref; r; r = r->next)
2312 : : {
2313 : 19232 : previous = length;
2314 : 19232 : switch (r->type)
2315 : : {
2316 : 288 : case REF_COMPONENT:
2317 : 288 : if (r->u.c.component->ts.type == BT_CHARACTER)
2318 : 288 : length = r->u.c.component->ts.u.cl->backend_decl;
2319 : : break;
2320 : :
2321 : : case REF_ARRAY:
2322 : : /* Do nothing. */
2323 : : break;
2324 : :
2325 : 20 : case REF_SUBSTRING:
2326 : 20 : gfc_init_se (&se, NULL);
2327 : 20 : gfc_conv_expr_type (&se, r->u.ss.start, gfc_charlen_type_node);
2328 : 20 : length = se.expr;
2329 : 20 : if (r->u.ss.end)
2330 : 0 : gfc_conv_expr_type (&se, r->u.ss.end, gfc_charlen_type_node);
2331 : : else
2332 : 20 : se.expr = previous;
2333 : 20 : length = fold_build2_loc (input_location, MINUS_EXPR,
2334 : : gfc_charlen_type_node,
2335 : : se.expr, length);
2336 : 20 : length = fold_build2_loc (input_location, PLUS_EXPR,
2337 : : gfc_charlen_type_node, length,
2338 : : gfc_index_one_node);
2339 : 20 : break;
2340 : :
2341 : 0 : default:
2342 : 0 : gcc_unreachable ();
2343 : 19232 : break;
2344 : : }
2345 : : }
2346 : :
2347 : 19093 : gcc_assert (length != NULL);
2348 : : return length;
2349 : : }
2350 : :
2351 : :
2352 : : /* Return for an expression the backend decl of the coarray. */
2353 : :
2354 : : tree
2355 : 1542 : gfc_get_tree_for_caf_expr (gfc_expr *expr)
2356 : : {
2357 : 1542 : tree caf_decl;
2358 : 1542 : bool found = false;
2359 : 1542 : gfc_ref *ref;
2360 : :
2361 : 1542 : gcc_assert (expr && expr->expr_type == EXPR_VARIABLE);
2362 : :
2363 : : /* Not-implemented diagnostic. */
2364 : 1542 : if (expr->symtree->n.sym->ts.type == BT_CLASS
2365 : 30 : && UNLIMITED_POLY (expr->symtree->n.sym)
2366 : 0 : && CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
2367 : 0 : gfc_error ("Sorry, coindexed access to an unlimited polymorphic object at "
2368 : : "%L is not supported", &expr->where);
2369 : :
2370 : 3294 : for (ref = expr->ref; ref; ref = ref->next)
2371 : 1752 : if (ref->type == REF_COMPONENT)
2372 : : {
2373 : 174 : if (ref->u.c.component->ts.type == BT_CLASS
2374 : 0 : && UNLIMITED_POLY (ref->u.c.component)
2375 : 0 : && CLASS_DATA (ref->u.c.component)->attr.codimension)
2376 : 0 : gfc_error ("Sorry, coindexed access to an unlimited polymorphic "
2377 : : "component at %L is not supported", &expr->where);
2378 : : }
2379 : :
2380 : : /* Make sure the backend_decl is present before accessing it. */
2381 : 1542 : caf_decl = expr->symtree->n.sym->backend_decl == NULL_TREE
2382 : 1542 : ? gfc_get_symbol_decl (expr->symtree->n.sym)
2383 : : : expr->symtree->n.sym->backend_decl;
2384 : :
2385 : 1542 : if (expr->symtree->n.sym->ts.type == BT_CLASS)
2386 : : {
2387 : 30 : if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
2388 : 33 : && GFC_DECL_SAVED_DESCRIPTOR (caf_decl))
2389 : 3 : caf_decl = GFC_DECL_SAVED_DESCRIPTOR (caf_decl);
2390 : :
2391 : 30 : if (expr->ref && expr->ref->type == REF_ARRAY)
2392 : : {
2393 : 23 : caf_decl = gfc_class_data_get (caf_decl);
2394 : 23 : if (CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
2395 : : return caf_decl;
2396 : : }
2397 : 7 : else if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
2398 : 1 : && GFC_DECL_TOKEN (caf_decl)
2399 : 8 : && CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
2400 : : return caf_decl;
2401 : :
2402 : 15 : for (ref = expr->ref; ref; ref = ref->next)
2403 : : {
2404 : 12 : if (ref->type == REF_COMPONENT
2405 : 6 : && strcmp (ref->u.c.component->name, "_data") != 0)
2406 : : {
2407 : 0 : caf_decl = gfc_class_data_get (caf_decl);
2408 : 0 : if (CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
2409 : : return caf_decl;
2410 : : break;
2411 : : }
2412 : 12 : else if (ref->type == REF_ARRAY && ref->u.ar.dimen)
2413 : : break;
2414 : : }
2415 : : }
2416 : 1518 : if (expr->symtree->n.sym->attr.codimension)
2417 : : return caf_decl;
2418 : :
2419 : : /* The following code assumes that the coarray is a component reachable via
2420 : : only scalar components/variables; the Fortran standard guarantees this. */
2421 : :
2422 : 33 : for (ref = expr->ref; ref; ref = ref->next)
2423 : 33 : if (ref->type == REF_COMPONENT)
2424 : : {
2425 : 33 : gfc_component *comp = ref->u.c.component;
2426 : :
2427 : 33 : if (POINTER_TYPE_P (TREE_TYPE (caf_decl)))
2428 : 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
2429 : 33 : caf_decl = fold_build3_loc (input_location, COMPONENT_REF,
2430 : 33 : TREE_TYPE (comp->backend_decl), caf_decl,
2431 : : comp->backend_decl, NULL_TREE);
2432 : 33 : if (comp->ts.type == BT_CLASS)
2433 : : {
2434 : 0 : caf_decl = gfc_class_data_get (caf_decl);
2435 : 0 : if (CLASS_DATA (comp)->attr.codimension)
2436 : : {
2437 : : found = true;
2438 : : break;
2439 : : }
2440 : : }
2441 : 33 : if (comp->attr.codimension)
2442 : : {
2443 : : found = true;
2444 : : break;
2445 : : }
2446 : : }
2447 : 33 : gcc_assert (found && caf_decl);
2448 : : return caf_decl;
2449 : : }
2450 : :
2451 : :
2452 : : /* Obtain the Coarray token - and optionally also the offset. */
2453 : :
2454 : : void
2455 : 1459 : gfc_get_caf_token_offset (gfc_se *se, tree *token, tree *offset, tree caf_decl,
2456 : : tree se_expr, gfc_expr *expr)
2457 : : {
2458 : 1459 : tree tmp;
2459 : :
2460 : 1459 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
2461 : :
2462 : : /* Coarray token. */
2463 : 1459 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
2464 : 356 : *token = gfc_conv_descriptor_token (caf_decl);
2465 : 1102 : else if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
2466 : 1204 : && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
2467 : 4 : *token = GFC_DECL_TOKEN (caf_decl);
2468 : : else
2469 : : {
2470 : 1099 : gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (caf_decl))
2471 : : && GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl)) != NULL_TREE);
2472 : 1099 : *token = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl));
2473 : : }
2474 : :
2475 : 1459 : if (offset == NULL)
2476 : : return;
2477 : :
2478 : : /* Offset between the coarray base address and the address wanted. */
2479 : 91 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl))
2480 : 91 : && (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_ALLOCATABLE
2481 : 0 : || GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_POINTER))
2482 : 0 : *offset = build_int_cst (gfc_array_index_type, 0);
2483 : 91 : else if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
2484 : 91 : && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
2485 : 0 : *offset = GFC_DECL_CAF_OFFSET (caf_decl);
2486 : 91 : else if (GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl)) != NULL_TREE)
2487 : 0 : *offset = GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl));
2488 : : else
2489 : 91 : *offset = build_int_cst (gfc_array_index_type, 0);
2490 : :
2491 : 91 : if (POINTER_TYPE_P (TREE_TYPE (se_expr))
2492 : 91 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se_expr))))
2493 : : {
2494 : 0 : tmp = build_fold_indirect_ref_loc (input_location, se_expr);
2495 : 0 : tmp = gfc_conv_descriptor_data_get (tmp);
2496 : : }
2497 : 91 : else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se_expr)))
2498 : 0 : tmp = gfc_conv_descriptor_data_get (se_expr);
2499 : : else
2500 : : {
2501 : 91 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (se_expr)));
2502 : : tmp = se_expr;
2503 : : }
2504 : :
2505 : 91 : *offset = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
2506 : : *offset, fold_convert (gfc_array_index_type, tmp));
2507 : :
2508 : 91 : if (expr->symtree->n.sym->ts.type == BT_DERIVED
2509 : 0 : && expr->symtree->n.sym->attr.codimension
2510 : 0 : && expr->symtree->n.sym->ts.u.derived->attr.alloc_comp)
2511 : : {
2512 : 0 : gfc_expr *base_expr = gfc_copy_expr (expr);
2513 : 0 : gfc_ref *ref = base_expr->ref;
2514 : 0 : gfc_se base_se;
2515 : :
2516 : : // Iterate through the refs until the last one.
2517 : 0 : while (ref->next)
2518 : : ref = ref->next;
2519 : :
2520 : 0 : if (ref->type == REF_ARRAY
2521 : 0 : && ref->u.ar.type != AR_FULL)
2522 : : {
2523 : 0 : const int ranksum = ref->u.ar.dimen + ref->u.ar.codimen;
2524 : 0 : int i;
2525 : 0 : for (i = 0; i < ranksum; ++i)
2526 : : {
2527 : 0 : ref->u.ar.start[i] = NULL;
2528 : 0 : ref->u.ar.end[i] = NULL;
2529 : : }
2530 : 0 : ref->u.ar.type = AR_FULL;
2531 : : }
2532 : 0 : gfc_init_se (&base_se, NULL);
2533 : 0 : if (gfc_caf_attr (base_expr).dimension)
2534 : : {
2535 : 0 : gfc_conv_expr_descriptor (&base_se, base_expr);
2536 : 0 : tmp = gfc_conv_descriptor_data_get (base_se.expr);
2537 : : }
2538 : : else
2539 : : {
2540 : 0 : gfc_conv_expr (&base_se, base_expr);
2541 : 0 : tmp = base_se.expr;
2542 : : }
2543 : :
2544 : 0 : gfc_free_expr (base_expr);
2545 : 0 : gfc_add_block_to_block (&se->pre, &base_se.pre);
2546 : 0 : gfc_add_block_to_block (&se->post, &base_se.post);
2547 : 0 : }
2548 : 91 : else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
2549 : 0 : tmp = gfc_conv_descriptor_data_get (caf_decl);
2550 : 91 : else if (INDIRECT_REF_P (caf_decl))
2551 : 0 : tmp = TREE_OPERAND (caf_decl, 0);
2552 : : else
2553 : : {
2554 : 91 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (caf_decl)));
2555 : : tmp = caf_decl;
2556 : : }
2557 : :
2558 : 91 : *offset = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2559 : : fold_convert (gfc_array_index_type, *offset),
2560 : : fold_convert (gfc_array_index_type, tmp));
2561 : : }
2562 : :
2563 : :
2564 : : /* Convert the coindex of a coarray into an image index; the result is
2565 : : image_num = (idx(1)-lcobound(1)+1) + (idx(2)-lcobound(2))*extent(1)
2566 : : + (idx(3)-lcobound(3))*extend(1)*extent(2) + ... */
2567 : :
2568 : : tree
2569 : 1264 : gfc_caf_get_image_index (stmtblock_t *block, gfc_expr *e, tree desc)
2570 : : {
2571 : 1264 : gfc_ref *ref;
2572 : 1264 : tree lbound, ubound, extent, tmp, img_idx;
2573 : 1264 : gfc_se se;
2574 : 1264 : int i;
2575 : :
2576 : 1285 : for (ref = e->ref; ref; ref = ref->next)
2577 : 1285 : if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
2578 : : break;
2579 : 1264 : gcc_assert (ref != NULL);
2580 : :
2581 : 1264 : if (ref->u.ar.dimen_type[ref->u.ar.dimen] == DIMEN_THIS_IMAGE)
2582 : 66 : return build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
2583 : 66 : null_pointer_node);
2584 : :
2585 : 1198 : img_idx = build_zero_cst (gfc_array_index_type);
2586 : 1198 : extent = build_one_cst (gfc_array_index_type);
2587 : 1198 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
2588 : 454 : for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
2589 : : {
2590 : 230 : gfc_init_se (&se, NULL);
2591 : 230 : gfc_conv_expr_type (&se, ref->u.ar.start[i], gfc_array_index_type);
2592 : 230 : gfc_add_block_to_block (block, &se.pre);
2593 : 230 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
2594 : 230 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2595 : 230 : TREE_TYPE (lbound), se.expr, lbound);
2596 : 230 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
2597 : : extent, tmp);
2598 : 230 : img_idx = fold_build2_loc (input_location, PLUS_EXPR,
2599 : 230 : TREE_TYPE (tmp), img_idx, tmp);
2600 : 230 : if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
2601 : : {
2602 : 6 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
2603 : 6 : tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
2604 : 6 : extent = fold_build2_loc (input_location, MULT_EXPR,
2605 : 6 : TREE_TYPE (tmp), extent, tmp);
2606 : : }
2607 : : }
2608 : : else
2609 : 1956 : for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
2610 : : {
2611 : 982 : gfc_init_se (&se, NULL);
2612 : 982 : gfc_conv_expr_type (&se, ref->u.ar.start[i], gfc_array_index_type);
2613 : 982 : gfc_add_block_to_block (block, &se.pre);
2614 : 982 : lbound = GFC_TYPE_ARRAY_LBOUND (TREE_TYPE (desc), i);
2615 : 982 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2616 : 982 : TREE_TYPE (lbound), se.expr, lbound);
2617 : 982 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
2618 : : extent, tmp);
2619 : 982 : img_idx = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
2620 : : img_idx, tmp);
2621 : 982 : if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
2622 : : {
2623 : 8 : ubound = GFC_TYPE_ARRAY_UBOUND (TREE_TYPE (desc), i);
2624 : 8 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2625 : 8 : TREE_TYPE (ubound), ubound, lbound);
2626 : 8 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
2627 : 8 : tmp, build_one_cst (TREE_TYPE (tmp)));
2628 : 8 : extent = fold_build2_loc (input_location, MULT_EXPR,
2629 : 8 : TREE_TYPE (tmp), extent, tmp);
2630 : : }
2631 : : }
2632 : 1198 : img_idx = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (img_idx),
2633 : 1198 : img_idx, build_one_cst (TREE_TYPE (img_idx)));
2634 : 1198 : return fold_convert (integer_type_node, img_idx);
2635 : : }
2636 : :
2637 : :
2638 : : /* For each character array constructor subexpression without a ts.u.cl->length,
2639 : : replace it by its first element (if there aren't any elements, the length
2640 : : should already be set to zero). */
2641 : :
2642 : : static void
2643 : 105 : flatten_array_ctors_without_strlen (gfc_expr* e)
2644 : : {
2645 : 105 : gfc_actual_arglist* arg;
2646 : 105 : gfc_constructor* c;
2647 : :
2648 : 105 : if (!e)
2649 : : return;
2650 : :
2651 : 105 : switch (e->expr_type)
2652 : : {
2653 : :
2654 : 0 : case EXPR_OP:
2655 : 0 : flatten_array_ctors_without_strlen (e->value.op.op1);
2656 : 0 : flatten_array_ctors_without_strlen (e->value.op.op2);
2657 : 0 : break;
2658 : :
2659 : 0 : case EXPR_COMPCALL:
2660 : : /* TODO: Implement as with EXPR_FUNCTION when needed. */
2661 : 0 : gcc_unreachable ();
2662 : :
2663 : 12 : case EXPR_FUNCTION:
2664 : 36 : for (arg = e->value.function.actual; arg; arg = arg->next)
2665 : 24 : flatten_array_ctors_without_strlen (arg->expr);
2666 : : break;
2667 : :
2668 : 0 : case EXPR_ARRAY:
2669 : :
2670 : : /* We've found what we're looking for. */
2671 : 0 : if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
2672 : : {
2673 : 0 : gfc_constructor *c;
2674 : 0 : gfc_expr* new_expr;
2675 : :
2676 : 0 : gcc_assert (e->value.constructor);
2677 : :
2678 : 0 : c = gfc_constructor_first (e->value.constructor);
2679 : 0 : new_expr = c->expr;
2680 : 0 : c->expr = NULL;
2681 : :
2682 : 0 : flatten_array_ctors_without_strlen (new_expr);
2683 : 0 : gfc_replace_expr (e, new_expr);
2684 : 0 : break;
2685 : : }
2686 : :
2687 : : /* Otherwise, fall through to handle constructor elements. */
2688 : 0 : gcc_fallthrough ();
2689 : 0 : case EXPR_STRUCTURE:
2690 : 0 : for (c = gfc_constructor_first (e->value.constructor);
2691 : 0 : c; c = gfc_constructor_next (c))
2692 : 0 : flatten_array_ctors_without_strlen (c->expr);
2693 : : break;
2694 : :
2695 : : default:
2696 : : break;
2697 : :
2698 : : }
2699 : : }
2700 : :
2701 : :
2702 : : /* Generate code to initialize a string length variable. Returns the
2703 : : value. For array constructors, cl->length might be NULL and in this case,
2704 : : the first element of the constructor is needed. expr is the original
2705 : : expression so we can access it but can be NULL if this is not needed. */
2706 : :
2707 : : void
2708 : 3692 : gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
2709 : : {
2710 : 3692 : gfc_se se;
2711 : :
2712 : 3692 : gfc_init_se (&se, NULL);
2713 : :
2714 : 3692 : if (!cl->length && cl->backend_decl && VAR_P (cl->backend_decl))
2715 : 1293 : return;
2716 : :
2717 : : /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
2718 : : "flatten" array constructors by taking their first element; all elements
2719 : : should be the same length or a cl->length should be present. */
2720 : 2479 : if (!cl->length)
2721 : : {
2722 : 161 : gfc_expr* expr_flat;
2723 : 161 : if (!expr)
2724 : : return;
2725 : 81 : expr_flat = gfc_copy_expr (expr);
2726 : 81 : flatten_array_ctors_without_strlen (expr_flat);
2727 : 81 : gfc_resolve_expr (expr_flat);
2728 : 81 : if (expr_flat->rank)
2729 : 12 : gfc_conv_expr_descriptor (&se, expr_flat);
2730 : : else
2731 : 69 : gfc_conv_expr (&se, expr_flat);
2732 : 81 : if (expr_flat->expr_type != EXPR_VARIABLE)
2733 : 75 : gfc_add_block_to_block (pblock, &se.pre);
2734 : 81 : se.expr = convert (gfc_charlen_type_node, se.string_length);
2735 : 81 : gfc_add_block_to_block (pblock, &se.post);
2736 : 81 : gfc_free_expr (expr_flat);
2737 : : }
2738 : : else
2739 : : {
2740 : : /* Convert cl->length. */
2741 : 2318 : gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
2742 : 2318 : se.expr = fold_build2_loc (input_location, MAX_EXPR,
2743 : : gfc_charlen_type_node, se.expr,
2744 : 2318 : build_zero_cst (TREE_TYPE (se.expr)));
2745 : 2318 : gfc_add_block_to_block (pblock, &se.pre);
2746 : : }
2747 : :
2748 : 2399 : if (cl->backend_decl && VAR_P (cl->backend_decl))
2749 : 1539 : gfc_add_modify (pblock, cl->backend_decl, se.expr);
2750 : : else
2751 : 860 : cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
2752 : : }
2753 : :
2754 : :
2755 : : static void
2756 : 6788 : gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
2757 : : const char *name, locus *where)
2758 : : {
2759 : 6788 : tree tmp;
2760 : 6788 : tree type;
2761 : 6788 : tree fault;
2762 : 6788 : gfc_se start;
2763 : 6788 : gfc_se end;
2764 : 6788 : char *msg;
2765 : 6788 : mpz_t length;
2766 : :
2767 : 6788 : type = gfc_get_character_type (kind, ref->u.ss.length);
2768 : 6788 : type = build_pointer_type (type);
2769 : :
2770 : 6788 : gfc_init_se (&start, se);
2771 : 6788 : gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
2772 : 6788 : gfc_add_block_to_block (&se->pre, &start.pre);
2773 : :
2774 : 6788 : if (integer_onep (start.expr))
2775 : 2308 : gfc_conv_string_parameter (se);
2776 : : else
2777 : : {
2778 : 4480 : tmp = start.expr;
2779 : 4480 : STRIP_NOPS (tmp);
2780 : : /* Avoid multiple evaluation of substring start. */
2781 : 4480 : if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
2782 : 1664 : start.expr = gfc_evaluate_now (start.expr, &se->pre);
2783 : :
2784 : : /* Change the start of the string. */
2785 : 4480 : if (((TREE_CODE (TREE_TYPE (se->expr)) == ARRAY_TYPE
2786 : 1150 : || TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE)
2787 : 3450 : && TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
2788 : 5510 : || (POINTER_TYPE_P (TREE_TYPE (se->expr))
2789 : 1030 : && TREE_CODE (TREE_TYPE (TREE_TYPE (se->expr))) != ARRAY_TYPE))
2790 : : tmp = se->expr;
2791 : : else
2792 : 1024 : tmp = build_fold_indirect_ref_loc (input_location,
2793 : : se->expr);
2794 : : /* For BIND(C), a BT_CHARACTER is not an ARRAY_TYPE. */
2795 : 4480 : if (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE)
2796 : : {
2797 : 4354 : tmp = gfc_build_array_ref (tmp, start.expr, NULL_TREE, true);
2798 : 4354 : se->expr = gfc_build_addr_expr (type, tmp);
2799 : : }
2800 : 126 : else if (POINTER_TYPE_P (TREE_TYPE (tmp)))
2801 : : {
2802 : 6 : tree diff;
2803 : 6 : diff = fold_build2 (MINUS_EXPR, size_type_node, start.expr,
2804 : : build_one_cst (size_type_node));
2805 : 6 : se->expr
2806 : 6 : = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (tmp), tmp, diff);
2807 : : }
2808 : : }
2809 : :
2810 : : /* Length = end + 1 - start. */
2811 : 6788 : gfc_init_se (&end, se);
2812 : 6788 : if (ref->u.ss.end == NULL)
2813 : 177 : end.expr = se->string_length;
2814 : : else
2815 : : {
2816 : 6611 : gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
2817 : 6611 : gfc_add_block_to_block (&se->pre, &end.pre);
2818 : : }
2819 : 6788 : tmp = end.expr;
2820 : 6788 : STRIP_NOPS (tmp);
2821 : 6788 : if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
2822 : 2289 : end.expr = gfc_evaluate_now (end.expr, &se->pre);
2823 : :
2824 : 6788 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2825 : 474 : && !gfc_contains_implied_index_p (ref->u.ss.start)
2826 : 7243 : && !gfc_contains_implied_index_p (ref->u.ss.end))
2827 : : {
2828 : 455 : tree nonempty = fold_build2_loc (input_location, LE_EXPR,
2829 : : logical_type_node, start.expr,
2830 : : end.expr);
2831 : :
2832 : : /* Check lower bound. */
2833 : 455 : fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2834 : : start.expr,
2835 : 455 : build_one_cst (TREE_TYPE (start.expr)));
2836 : 455 : fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2837 : : logical_type_node, nonempty, fault);
2838 : 455 : if (name)
2839 : 454 : msg = xasprintf ("Substring out of bounds: lower bound (%%ld) of '%s' "
2840 : : "is less than one", name);
2841 : : else
2842 : 1 : msg = xasprintf ("Substring out of bounds: lower bound (%%ld) "
2843 : : "is less than one");
2844 : 455 : gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
2845 : : fold_convert (long_integer_type_node,
2846 : : start.expr));
2847 : 455 : free (msg);
2848 : :
2849 : : /* Check upper bound. */
2850 : 455 : fault = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2851 : : end.expr, se->string_length);
2852 : 455 : fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2853 : : logical_type_node, nonempty, fault);
2854 : 455 : if (name)
2855 : 454 : msg = xasprintf ("Substring out of bounds: upper bound (%%ld) of '%s' "
2856 : : "exceeds string length (%%ld)", name);
2857 : : else
2858 : 1 : msg = xasprintf ("Substring out of bounds: upper bound (%%ld) "
2859 : : "exceeds string length (%%ld)");
2860 : 455 : gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
2861 : : fold_convert (long_integer_type_node, end.expr),
2862 : : fold_convert (long_integer_type_node,
2863 : : se->string_length));
2864 : 455 : free (msg);
2865 : : }
2866 : :
2867 : : /* Try to calculate the length from the start and end expressions. */
2868 : 6788 : if (ref->u.ss.end
2869 : 6788 : && gfc_dep_difference (ref->u.ss.end, ref->u.ss.start, &length))
2870 : : {
2871 : 5604 : HOST_WIDE_INT i_len;
2872 : :
2873 : 5604 : i_len = gfc_mpz_get_hwi (length) + 1;
2874 : 5604 : if (i_len < 0)
2875 : : i_len = 0;
2876 : :
2877 : 5604 : tmp = build_int_cst (gfc_charlen_type_node, i_len);
2878 : 5604 : mpz_clear (length); /* Was initialized by gfc_dep_difference. */
2879 : : }
2880 : : else
2881 : : {
2882 : 1184 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
2883 : : fold_convert (gfc_charlen_type_node, end.expr),
2884 : : fold_convert (gfc_charlen_type_node, start.expr));
2885 : 1184 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
2886 : : build_int_cst (gfc_charlen_type_node, 1), tmp);
2887 : 1184 : tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
2888 : : tmp, build_int_cst (gfc_charlen_type_node, 0));
2889 : : }
2890 : :
2891 : 6788 : se->string_length = tmp;
2892 : 6788 : }
2893 : :
2894 : :
2895 : : /* Convert a derived type component reference. */
2896 : :
2897 : : void
2898 : 163410 : gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
2899 : : {
2900 : 163410 : gfc_component *c;
2901 : 163410 : tree tmp;
2902 : 163410 : tree decl;
2903 : 163410 : tree field;
2904 : 163410 : tree context;
2905 : :
2906 : 163410 : c = ref->u.c.component;
2907 : :
2908 : 163410 : if (c->backend_decl == NULL_TREE
2909 : 6 : && ref->u.c.sym != NULL)
2910 : 6 : gfc_get_derived_type (ref->u.c.sym);
2911 : :
2912 : 163410 : field = c->backend_decl;
2913 : 163410 : gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
2914 : 163410 : decl = se->expr;
2915 : 163410 : context = DECL_FIELD_CONTEXT (field);
2916 : :
2917 : : /* Components can correspond to fields of different containing
2918 : : types, as components are created without context, whereas
2919 : : a concrete use of a component has the type of decl as context.
2920 : : So, if the type doesn't match, we search the corresponding
2921 : : FIELD_DECL in the parent type. To not waste too much time
2922 : : we cache this result in norestrict_decl.
2923 : : On the other hand, if the context is a UNION or a MAP (a
2924 : : RECORD_TYPE within a UNION_TYPE) always use the given FIELD_DECL. */
2925 : :
2926 : 163410 : if (context != TREE_TYPE (decl)
2927 : 163410 : && !( TREE_CODE (TREE_TYPE (field)) == UNION_TYPE /* Field is union */
2928 : 10793 : || TREE_CODE (context) == UNION_TYPE)) /* Field is map */
2929 : : {
2930 : 10793 : tree f2 = c->norestrict_decl;
2931 : 18544 : if (!f2 || DECL_FIELD_CONTEXT (f2) != TREE_TYPE (decl))
2932 : 6174 : for (f2 = TYPE_FIELDS (TREE_TYPE (decl)); f2; f2 = DECL_CHAIN (f2))
2933 : 6174 : if (TREE_CODE (f2) == FIELD_DECL
2934 : 6174 : && DECL_NAME (f2) == DECL_NAME (field))
2935 : : break;
2936 : 10793 : gcc_assert (f2);
2937 : 10793 : c->norestrict_decl = f2;
2938 : 10793 : field = f2;
2939 : : }
2940 : :
2941 : 163410 : if (ref->u.c.sym && ref->u.c.sym->ts.type == BT_CLASS
2942 : 0 : && strcmp ("_data", c->name) == 0)
2943 : : {
2944 : : /* Found a ref to the _data component. Store the associated ref to
2945 : : the vptr in se->class_vptr. */
2946 : 0 : se->class_vptr = gfc_class_vptr_get (decl);
2947 : : }
2948 : : else
2949 : 163410 : se->class_vptr = NULL_TREE;
2950 : :
2951 : 163410 : tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
2952 : : decl, field, NULL_TREE);
2953 : :
2954 : 163410 : se->expr = tmp;
2955 : :
2956 : : /* Allocatable deferred char arrays are to be handled by the gfc_deferred_
2957 : : strlen () conditional below. */
2958 : 163410 : if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
2959 : 8460 : && !c->ts.deferred
2960 : 5459 : && !c->attr.pdt_string)
2961 : : {
2962 : 5333 : tmp = c->ts.u.cl->backend_decl;
2963 : : /* Components must always be constant length. */
2964 : 5333 : gcc_assert (tmp && INTEGER_CST_P (tmp));
2965 : 5333 : se->string_length = tmp;
2966 : : }
2967 : :
2968 : 163410 : if (gfc_deferred_strlen (c, &field))
2969 : : {
2970 : 3127 : tmp = fold_build3_loc (input_location, COMPONENT_REF,
2971 : 3127 : TREE_TYPE (field),
2972 : : decl, field, NULL_TREE);
2973 : 3127 : se->string_length = tmp;
2974 : : }
2975 : :
2976 : 163410 : if (((c->attr.pointer || c->attr.allocatable)
2977 : 94500 : && (!c->attr.dimension && !c->attr.codimension)
2978 : 52828 : && c->ts.type != BT_CHARACTER)
2979 : 112612 : || c->attr.proc_pointer)
2980 : 56602 : se->expr = build_fold_indirect_ref_loc (input_location,
2981 : : se->expr);
2982 : 163410 : }
2983 : :
2984 : :
2985 : : /* This function deals with component references to components of the
2986 : : parent type for derived type extensions. */
2987 : : void
2988 : 61451 : conv_parent_component_references (gfc_se * se, gfc_ref * ref)
2989 : : {
2990 : 61451 : gfc_component *c;
2991 : 61451 : gfc_component *cmp;
2992 : 61451 : gfc_symbol *dt;
2993 : 61451 : gfc_ref parent;
2994 : :
2995 : 61451 : dt = ref->u.c.sym;
2996 : 61451 : c = ref->u.c.component;
2997 : :
2998 : : /* Return if the component is in this type, i.e. not in the parent type. */
2999 : 105710 : for (cmp = dt->components; cmp; cmp = cmp->next)
3000 : 95771 : if (c == cmp)
3001 : 51512 : return;
3002 : :
3003 : : /* Build a gfc_ref to recursively call gfc_conv_component_ref. */
3004 : 9939 : parent.type = REF_COMPONENT;
3005 : 9939 : parent.next = NULL;
3006 : 9939 : parent.u.c.sym = dt;
3007 : 9939 : parent.u.c.component = dt->components;
3008 : :
3009 : 9939 : if (dt->backend_decl == NULL)
3010 : 0 : gfc_get_derived_type (dt);
3011 : :
3012 : : /* Build the reference and call self. */
3013 : 9939 : gfc_conv_component_ref (se, &parent);
3014 : 9939 : parent.u.c.sym = dt->components->ts.u.derived;
3015 : 9939 : parent.u.c.component = c;
3016 : 9939 : conv_parent_component_references (se, &parent);
3017 : : }
3018 : :
3019 : :
3020 : : static void
3021 : 537 : conv_inquiry (gfc_se * se, gfc_ref * ref, gfc_expr *expr, gfc_typespec *ts)
3022 : : {
3023 : 537 : tree res = se->expr;
3024 : :
3025 : 537 : switch (ref->u.i)
3026 : : {
3027 : 259 : case INQUIRY_RE:
3028 : 518 : res = fold_build1_loc (input_location, REALPART_EXPR,
3029 : 259 : TREE_TYPE (TREE_TYPE (res)), res);
3030 : 259 : break;
3031 : :
3032 : 233 : case INQUIRY_IM:
3033 : 466 : res = fold_build1_loc (input_location, IMAGPART_EXPR,
3034 : 233 : TREE_TYPE (TREE_TYPE (res)), res);
3035 : 233 : break;
3036 : :
3037 : 7 : case INQUIRY_KIND:
3038 : 7 : res = build_int_cst (gfc_typenode_for_spec (&expr->ts),
3039 : 7 : ts->kind);
3040 : 7 : se->string_length = NULL_TREE;
3041 : 7 : break;
3042 : :
3043 : 38 : case INQUIRY_LEN:
3044 : 38 : res = fold_convert (gfc_typenode_for_spec (&expr->ts),
3045 : : se->string_length);
3046 : 38 : se->string_length = NULL_TREE;
3047 : 38 : break;
3048 : :
3049 : 0 : default:
3050 : 0 : gcc_unreachable ();
3051 : : }
3052 : 537 : se->expr = res;
3053 : 537 : }
3054 : :
3055 : : /* Dereference VAR where needed if it is a pointer, reference, etc.
3056 : : according to Fortran semantics. */
3057 : :
3058 : : tree
3059 : 1389071 : gfc_maybe_dereference_var (gfc_symbol *sym, tree var, bool descriptor_only_p,
3060 : : bool is_classarray)
3061 : : {
3062 : 1389071 : if (!POINTER_TYPE_P (TREE_TYPE (var)))
3063 : : return var;
3064 : 273051 : if (is_CFI_desc (sym, NULL))
3065 : 11890 : return build_fold_indirect_ref_loc (input_location, var);
3066 : :
3067 : : /* Characters are entirely different from other types, they are treated
3068 : : separately. */
3069 : 261161 : if (sym->ts.type == BT_CHARACTER)
3070 : : {
3071 : : /* Dereference character pointer dummy arguments
3072 : : or results. */
3073 : 30223 : if ((sym->attr.pointer || sym->attr.allocatable
3074 : 17172 : || (sym->as && sym->as->type == AS_ASSUMED_RANK))
3075 : 13387 : && (sym->attr.dummy
3076 : 10248 : || sym->attr.function
3077 : 9898 : || sym->attr.result))
3078 : 4144 : var = build_fold_indirect_ref_loc (input_location, var);
3079 : : }
3080 : 230938 : else if (!sym->attr.value)
3081 : : {
3082 : : /* Dereference temporaries for class array dummy arguments. */
3083 : 161545 : if (sym->attr.dummy && is_classarray
3084 : 237322 : && GFC_ARRAY_TYPE_P (TREE_TYPE (var)))
3085 : : {
3086 : 4896 : if (!descriptor_only_p)
3087 : 2477 : var = GFC_DECL_SAVED_DESCRIPTOR (var);
3088 : :
3089 : 4896 : var = build_fold_indirect_ref_loc (input_location, var);
3090 : : }
3091 : :
3092 : : /* Dereference non-character scalar dummy arguments. */
3093 : 230134 : if (sym->attr.dummy && !sym->attr.dimension
3094 : 99307 : && !(sym->attr.codimension && sym->attr.allocatable)
3095 : 99246 : && (sym->ts.type != BT_CLASS
3096 : 18314 : || (!CLASS_DATA (sym)->attr.dimension
3097 : 10667 : && !(CLASS_DATA (sym)->attr.codimension
3098 : 270 : && CLASS_DATA (sym)->attr.allocatable))))
3099 : 91461 : var = build_fold_indirect_ref_loc (input_location, var);
3100 : :
3101 : : /* Dereference scalar hidden result. */
3102 : 230134 : if (flag_f2c && sym->ts.type == BT_COMPLEX
3103 : 286 : && (sym->attr.function || sym->attr.result)
3104 : 108 : && !sym->attr.dimension && !sym->attr.pointer
3105 : 60 : && !sym->attr.always_explicit)
3106 : 36 : var = build_fold_indirect_ref_loc (input_location, var);
3107 : :
3108 : : /* Dereference non-character, non-class pointer variables.
3109 : : These must be dummies, results, or scalars. */
3110 : 230134 : if (!is_classarray
3111 : 222522 : && (sym->attr.pointer || sym->attr.allocatable
3112 : 176144 : || gfc_is_associate_pointer (sym)
3113 : 171644 : || (sym->as && sym->as->type == AS_ASSUMED_RANK))
3114 : 299739 : && (sym->attr.dummy
3115 : 33373 : || sym->attr.function
3116 : 32461 : || sym->attr.result
3117 : 31389 : || (!sym->attr.dimension
3118 : 31386 : && (!sym->attr.codimension || !sym->attr.allocatable))))
3119 : 69602 : var = build_fold_indirect_ref_loc (input_location, var);
3120 : : /* Now treat the class array pointer variables accordingly. */
3121 : 160532 : else if (sym->ts.type == BT_CLASS
3122 : 18738 : && sym->attr.dummy
3123 : 18314 : && (CLASS_DATA (sym)->attr.dimension
3124 : 10667 : || CLASS_DATA (sym)->attr.codimension)
3125 : 7917 : && ((CLASS_DATA (sym)->as
3126 : 7917 : && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
3127 : 6920 : || CLASS_DATA (sym)->attr.allocatable
3128 : 5595 : || CLASS_DATA (sym)->attr.class_pointer))
3129 : 2889 : var = build_fold_indirect_ref_loc (input_location, var);
3130 : : /* And the case where a non-dummy, non-result, non-function,
3131 : : non-allocable and non-pointer classarray is present. This case was
3132 : : previously covered by the first if, but with introducing the
3133 : : condition !is_classarray there, that case has to be covered
3134 : : explicitly. */
3135 : 157643 : else if (sym->ts.type == BT_CLASS
3136 : 15849 : && !sym->attr.dummy
3137 : 424 : && !sym->attr.function
3138 : 424 : && !sym->attr.result
3139 : 424 : && (CLASS_DATA (sym)->attr.dimension
3140 : 3 : || CLASS_DATA (sym)->attr.codimension)
3141 : 424 : && (sym->assoc
3142 : 0 : || !CLASS_DATA (sym)->attr.allocatable)
3143 : 424 : && !CLASS_DATA (sym)->attr.class_pointer)
3144 : 424 : var = build_fold_indirect_ref_loc (input_location, var);
3145 : : }
3146 : :
3147 : : return var;
3148 : : }
3149 : :
3150 : : /* Return the contents of a variable. Also handles reference/pointer
3151 : : variables (all Fortran pointer references are implicit). */
3152 : :
3153 : : static void
3154 : 1535481 : gfc_conv_variable (gfc_se * se, gfc_expr * expr)
3155 : : {
3156 : 1535481 : gfc_ss *ss;
3157 : 1535481 : gfc_ref *ref;
3158 : 1535481 : gfc_symbol *sym;
3159 : 1535481 : tree parent_decl = NULL_TREE;
3160 : 1535481 : int parent_flag;
3161 : 1535481 : bool return_value;
3162 : 1535481 : bool alternate_entry;
3163 : 1535481 : bool entry_master;
3164 : 1535481 : bool is_classarray;
3165 : 1535481 : bool first_time = true;
3166 : :
3167 : 1535481 : sym = expr->symtree->n.sym;
3168 : 1535481 : is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
3169 : 1535481 : ss = se->ss;
3170 : 1535481 : if (ss != NULL)
3171 : : {
3172 : 127697 : gfc_ss_info *ss_info = ss->info;
3173 : :
3174 : : /* Check that something hasn't gone horribly wrong. */
3175 : 127697 : gcc_assert (ss != gfc_ss_terminator);
3176 : 127697 : gcc_assert (ss_info->expr == expr);
3177 : :
3178 : : /* A scalarized term. We already know the descriptor. */
3179 : 127697 : se->expr = ss_info->data.array.descriptor;
3180 : 127697 : se->string_length = ss_info->string_length;
3181 : 127697 : ref = ss_info->data.array.ref;
3182 : 127697 : if (ref)
3183 : 127379 : gcc_assert (ref->type == REF_ARRAY
3184 : : && ref->u.ar.type != AR_ELEMENT);
3185 : : else
3186 : 318 : gfc_conv_tmp_array_ref (se);
3187 : : }
3188 : : else
3189 : : {
3190 : 1407784 : tree se_expr = NULL_TREE;
3191 : :
3192 : 1407784 : se->expr = gfc_get_symbol_decl (sym);
3193 : :
3194 : : /* Deal with references to a parent results or entries by storing
3195 : : the current_function_decl and moving to the parent_decl. */
3196 : 1407784 : return_value = sym->attr.function && sym->result == sym;
3197 : 19350 : alternate_entry = sym->attr.function && sym->attr.entry
3198 : 1408859 : && sym->result == sym;
3199 : 2815568 : entry_master = sym->attr.result
3200 : 11590 : && sym->ns->proc_name->attr.entry_master
3201 : 1408165 : && !gfc_return_by_reference (sym->ns->proc_name);
3202 : 1407784 : if (current_function_decl)
3203 : 1389243 : parent_decl = DECL_CONTEXT (current_function_decl);
3204 : :
3205 : 1407784 : if ((se->expr == parent_decl && return_value)
3206 : 1407673 : || (sym->ns && sym->ns->proc_name
3207 : 1402849 : && parent_decl
3208 : 1384308 : && sym->ns->proc_name->backend_decl == parent_decl
3209 : 37333 : && (alternate_entry || entry_master)))
3210 : : parent_flag = 1;
3211 : : else
3212 : 1407640 : parent_flag = 0;
3213 : :
3214 : : /* Special case for assigning the return value of a function.
3215 : : Self recursive functions must have an explicit return value. */
3216 : 1407784 : if (return_value && (se->expr == current_function_decl || parent_flag))
3217 : 11785 : se_expr = gfc_get_fake_result_decl (sym, parent_flag);
3218 : :
3219 : : /* Similarly for alternate entry points. */
3220 : 1395999 : else if (alternate_entry
3221 : 1042 : && (sym->ns->proc_name->backend_decl == current_function_decl
3222 : 0 : || parent_flag))
3223 : : {
3224 : 1042 : gfc_entry_list *el = NULL;
3225 : :
3226 : 1609 : for (el = sym->ns->entries; el; el = el->next)
3227 : 1609 : if (sym == el->sym)
3228 : : {
3229 : 1042 : se_expr = gfc_get_fake_result_decl (sym, parent_flag);
3230 : 1042 : break;
3231 : : }
3232 : : }
3233 : :
3234 : 1394957 : else if (entry_master
3235 : 295 : && (sym->ns->proc_name->backend_decl == current_function_decl
3236 : 0 : || parent_flag))
3237 : 295 : se_expr = gfc_get_fake_result_decl (sym, parent_flag);
3238 : :
3239 : 13122 : if (se_expr)
3240 : 13122 : se->expr = se_expr;
3241 : :
3242 : : /* Procedure actual arguments. Look out for temporary variables
3243 : : with the same attributes as function values. */
3244 : 1394662 : else if (!sym->attr.temporary
3245 : 1394594 : && sym->attr.flavor == FL_PROCEDURE
3246 : 20429 : && se->expr != current_function_decl)
3247 : : {
3248 : 20390 : if (!sym->attr.dummy && !sym->attr.proc_pointer)
3249 : : {
3250 : 18855 : gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
3251 : 18855 : se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
3252 : : }
3253 : 20390 : return;
3254 : : }
3255 : :
3256 : 1387394 : if (sym->ts.type == BT_CLASS
3257 : 68481 : && sym->attr.class_ok
3258 : 68239 : && sym->ts.u.derived->attr.is_class)
3259 : : {
3260 : 25695 : if (is_classarray && DECL_LANG_SPECIFIC (se->expr)
3261 : 75226 : && GFC_DECL_SAVED_DESCRIPTOR (se->expr))
3262 : 5028 : se->class_container = GFC_DECL_SAVED_DESCRIPTOR (se->expr);
3263 : : else
3264 : 63211 : se->class_container = se->expr;
3265 : : }
3266 : :
3267 : : /* Dereference the expression, where needed. */
3268 : 1387394 : if (se->class_container && CLASS_DATA (sym)->attr.codimension
3269 : 1890 : && !CLASS_DATA (sym)->attr.dimension)
3270 : 779 : se->expr
3271 : 779 : = gfc_maybe_dereference_var (sym, se->class_container,
3272 : 779 : se->descriptor_only, is_classarray);
3273 : : else
3274 : 1386615 : se->expr
3275 : 1386615 : = gfc_maybe_dereference_var (sym, se->expr, se->descriptor_only,
3276 : : is_classarray);
3277 : :
3278 : 1387394 : ref = expr->ref;
3279 : : }
3280 : :
3281 : : /* For character variables, also get the length. */
3282 : 1515091 : if (sym->ts.type == BT_CHARACTER)
3283 : : {
3284 : : /* If the character length of an entry isn't set, get the length from
3285 : : the master function instead. */
3286 : 157456 : if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
3287 : 0 : se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
3288 : : else
3289 : 157456 : se->string_length = sym->ts.u.cl->backend_decl;
3290 : 157456 : gcc_assert (se->string_length);
3291 : :
3292 : : /* For coarray strings return the pointer to the data and not the
3293 : : descriptor. */
3294 : 3459 : if (sym->attr.codimension && sym->attr.associate_var
3295 : 6 : && !se->descriptor_only
3296 : 157462 : && TREE_CODE (TREE_TYPE (se->expr)) != ARRAY_TYPE)
3297 : 6 : se->expr = gfc_conv_descriptor_data_get (se->expr);
3298 : : }
3299 : :
3300 : : /* F202Y: Runtime warning that an assumed rank object is associated
3301 : : with an assumed size object. */
3302 : 1515091 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
3303 : 88984 : && (gfc_option.allow_std & GFC_STD_F202Y)
3304 : 1515325 : && expr->rank == -1 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr)))
3305 : : {
3306 : 60 : tree dim, lower, upper, cond;
3307 : 60 : char *msg;
3308 : :
3309 : 60 : dim = fold_convert (signed_char_type_node,
3310 : : gfc_conv_descriptor_rank (se->expr));
3311 : 60 : dim = fold_build2_loc (input_location, MINUS_EXPR, signed_char_type_node,
3312 : : dim, build_int_cst (signed_char_type_node, 1));
3313 : 60 : lower = gfc_conv_descriptor_lbound_get (se->expr, dim);
3314 : 60 : upper = gfc_conv_descriptor_ubound_get (se->expr, dim);
3315 : :
3316 : 60 : msg = xasprintf ("Assumed rank object %s is associated with an "
3317 : : "assumed size object", sym->name);
3318 : 60 : cond = fold_build2_loc (input_location, LT_EXPR,
3319 : : logical_type_node, upper, lower);
3320 : 60 : gfc_trans_runtime_check (false, true, cond, &se->pre,
3321 : : &gfc_current_locus, msg);
3322 : 60 : free (msg);
3323 : : }
3324 : :
3325 : : /* Some expressions leak through that haven't been fixed up. */
3326 : 1515091 : if (IS_INFERRED_TYPE (expr) && expr->ref)
3327 : 380 : gfc_fixup_inferred_type_refs (expr);
3328 : :
3329 : 1515091 : gfc_typespec *ts = &sym->ts;
3330 : 1921937 : while (ref)
3331 : : {
3332 : 740815 : switch (ref->type)
3333 : : {
3334 : 579872 : case REF_ARRAY:
3335 : : /* Return the descriptor if that's what we want and this is an array
3336 : : section reference. */
3337 : 579872 : if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
3338 : : return;
3339 : : /* TODO: Pointers to single elements of array sections, eg elemental subs. */
3340 : : /* Return the descriptor for array pointers and allocations. */
3341 : 254802 : if (se->want_pointer
3342 : 22384 : && ref->next == NULL && (se->descriptor_only))
3343 : : return;
3344 : :
3345 : 245903 : gfc_conv_array_ref (se, &ref->u.ar, expr, &expr->where);
3346 : : /* Return a pointer to an element. */
3347 : 245903 : break;
3348 : :
3349 : 153876 : case REF_COMPONENT:
3350 : 153876 : ts = &ref->u.c.component->ts;
3351 : 153876 : if (first_time && IS_CLASS_ARRAY (sym) && sym->attr.dummy
3352 : 5498 : && se->descriptor_only && !CLASS_DATA (sym)->attr.allocatable
3353 : 2886 : && !CLASS_DATA (sym)->attr.class_pointer && CLASS_DATA (sym)->as
3354 : 2886 : && CLASS_DATA (sym)->as->type != AS_ASSUMED_RANK
3355 : 2419 : && strcmp ("_data", ref->u.c.component->name) == 0)
3356 : : /* Skip the first ref of a _data component, because for class
3357 : : arrays that one is already done by introducing a temporary
3358 : : array descriptor. */
3359 : : break;
3360 : :
3361 : 151457 : if (ref->u.c.sym->attr.extension)
3362 : 51421 : conv_parent_component_references (se, ref);
3363 : :
3364 : 151457 : gfc_conv_component_ref (se, ref);
3365 : :
3366 : 151457 : if (ref->u.c.component->ts.type == BT_CLASS
3367 : 11599 : && ref->u.c.component->attr.class_ok
3368 : 11599 : && ref->u.c.component->ts.u.derived->attr.is_class)
3369 : 11599 : se->class_container = se->expr;
3370 : 139858 : else if (!(ref->u.c.sym->attr.flavor == FL_DERIVED
3371 : 137364 : && ref->u.c.sym->attr.is_class))
3372 : 75492 : se->class_container = NULL_TREE;
3373 : :
3374 : 151457 : if (!ref->next && ref->u.c.sym->attr.codimension
3375 : 0 : && se->want_pointer && se->descriptor_only)
3376 : : return;
3377 : :
3378 : : break;
3379 : :
3380 : 6530 : case REF_SUBSTRING:
3381 : 6530 : gfc_conv_substring (se, ref, expr->ts.kind,
3382 : 6530 : expr->symtree->name, &expr->where);
3383 : 6530 : break;
3384 : :
3385 : 537 : case REF_INQUIRY:
3386 : 537 : conv_inquiry (se, ref, expr, ts);
3387 : 537 : break;
3388 : :
3389 : 0 : default:
3390 : 0 : gcc_unreachable ();
3391 : 406846 : break;
3392 : : }
3393 : 406846 : first_time = false;
3394 : 406846 : ref = ref->next;
3395 : : }
3396 : : /* Pointer assignment, allocation or pass by reference. Arrays are handled
3397 : : separately. */
3398 : 1181122 : if (se->want_pointer)
3399 : : {
3400 : 127273 : if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr))
3401 : 7637 : gfc_conv_string_parameter (se);
3402 : : else
3403 : 119636 : se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
3404 : : }
3405 : : }
3406 : :
3407 : :
3408 : : /* Unary ops are easy... Or they would be if ! was a valid op. */
3409 : :
3410 : : static void
3411 : 28052 : gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
3412 : : {
3413 : 28052 : gfc_se operand;
3414 : 28052 : tree type;
3415 : :
3416 : 28052 : gcc_assert (expr->ts.type != BT_CHARACTER);
3417 : : /* Initialize the operand. */
3418 : 28052 : gfc_init_se (&operand, se);
3419 : 28052 : gfc_conv_expr_val (&operand, expr->value.op.op1);
3420 : 28052 : gfc_add_block_to_block (&se->pre, &operand.pre);
3421 : :
3422 : 28052 : type = gfc_typenode_for_spec (&expr->ts);
3423 : :
3424 : : /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
3425 : : We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
3426 : : All other unary operators have an equivalent GIMPLE unary operator. */
3427 : 28052 : if (code == TRUTH_NOT_EXPR)
3428 : 19842 : se->expr = fold_build2_loc (input_location, EQ_EXPR, type, operand.expr,
3429 : : build_int_cst (type, 0));
3430 : : else
3431 : 8210 : se->expr = fold_build1_loc (input_location, code, type, operand.expr);
3432 : :
3433 : 28052 : }
3434 : :
3435 : : /* Expand power operator to optimal multiplications when a value is raised
3436 : : to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
3437 : : Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
3438 : : Programming", 3rd Edition, 1998. */
3439 : :
3440 : : /* This code is mostly duplicated from expand_powi in the backend.
3441 : : We establish the "optimal power tree" lookup table with the defined size.
3442 : : The items in the table are the exponents used to calculate the index
3443 : : exponents. Any integer n less than the value can get an "addition chain",
3444 : : with the first node being one. */
3445 : : #define POWI_TABLE_SIZE 256
3446 : :
3447 : : /* The table is from builtins.cc. */
3448 : : static const unsigned char powi_table[POWI_TABLE_SIZE] =
3449 : : {
3450 : : 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
3451 : : 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
3452 : : 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
3453 : : 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
3454 : : 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
3455 : : 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
3456 : : 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
3457 : : 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
3458 : : 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
3459 : : 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
3460 : : 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
3461 : : 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
3462 : : 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
3463 : : 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
3464 : : 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
3465 : : 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
3466 : : 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
3467 : : 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
3468 : : 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
3469 : : 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
3470 : : 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
3471 : : 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
3472 : : 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
3473 : : 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
3474 : : 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
3475 : : 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
3476 : : 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
3477 : : 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
3478 : : 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
3479 : : 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
3480 : : 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
3481 : : 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
3482 : : };
3483 : :
3484 : : /* If n is larger than lookup table's max index, we use the "window
3485 : : method". */
3486 : : #define POWI_WINDOW_SIZE 3
3487 : :
3488 : : /* Recursive function to expand the power operator. The temporary
3489 : : values are put in tmpvar. The function returns tmpvar[1] ** n. */
3490 : : static tree
3491 : 176241 : gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
3492 : : {
3493 : 176241 : tree op0;
3494 : 176241 : tree op1;
3495 : 176241 : tree tmp;
3496 : 176241 : int digit;
3497 : :
3498 : 176241 : if (n < POWI_TABLE_SIZE)
3499 : : {
3500 : 136094 : if (tmpvar[n])
3501 : : return tmpvar[n];
3502 : :
3503 : 56205 : op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
3504 : 56205 : op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
3505 : : }
3506 : 40147 : else if (n & 1)
3507 : : {
3508 : 9799 : digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
3509 : 9799 : op0 = gfc_conv_powi (se, n - digit, tmpvar);
3510 : 9799 : op1 = gfc_conv_powi (se, digit, tmpvar);
3511 : : }
3512 : : else
3513 : : {
3514 : 30348 : op0 = gfc_conv_powi (se, n >> 1, tmpvar);
3515 : 30348 : op1 = op0;
3516 : : }
3517 : :
3518 : 96352 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
3519 : 96352 : tmp = gfc_evaluate_now (tmp, &se->pre);
3520 : :
3521 : 96352 : if (n < POWI_TABLE_SIZE)
3522 : 56205 : tmpvar[n] = tmp;
3523 : :
3524 : : return tmp;
3525 : : }
3526 : :
3527 : :
3528 : : /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
3529 : : return 1. Else return 0 and a call to runtime library functions
3530 : : will have to be built. */
3531 : : static int
3532 : 2562 : gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
3533 : : {
3534 : 2562 : tree cond;
3535 : 2562 : tree tmp;
3536 : 2562 : tree type;
3537 : 2562 : tree vartmp[POWI_TABLE_SIZE];
3538 : 2562 : HOST_WIDE_INT m;
3539 : 2562 : unsigned HOST_WIDE_INT n;
3540 : 2562 : int sgn;
3541 : 2562 : wi::tree_to_wide_ref wrhs = wi::to_wide (rhs);
3542 : :
3543 : : /* If exponent is too large, we won't expand it anyway, so don't bother
3544 : : with large integer values. */
3545 : 2562 : if (!wi::fits_shwi_p (wrhs))
3546 : : return 0;
3547 : :
3548 : 2202 : m = wrhs.to_shwi ();
3549 : : /* Use the wide_int's routine to reliably get the absolute value on all
3550 : : platforms. Then convert it to a HOST_WIDE_INT like above. */
3551 : 2202 : n = wi::abs (wrhs).to_shwi ();
3552 : :
3553 : 2202 : type = TREE_TYPE (lhs);
3554 : 2202 : sgn = tree_int_cst_sgn (rhs);
3555 : :
3556 : 2202 : if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
3557 : 4404 : || optimize_size) && (m > 2 || m < -1))
3558 : : return 0;
3559 : :
3560 : : /* rhs == 0 */
3561 : 1244 : if (sgn == 0)
3562 : : {
3563 : 167 : se->expr = gfc_build_const (type, integer_one_node);
3564 : 167 : return 1;
3565 : : }
3566 : :
3567 : : /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
3568 : 1077 : if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
3569 : : {
3570 : 152 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3571 : 152 : lhs, build_int_cst (TREE_TYPE (lhs), -1));
3572 : 152 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3573 : 152 : lhs, build_int_cst (TREE_TYPE (lhs), 1));
3574 : :
3575 : : /* If rhs is even,
3576 : : result = (lhs == 1 || lhs == -1) ? 1 : 0. */
3577 : 152 : if ((n & 1) == 0)
3578 : : {
3579 : 72 : tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
3580 : : logical_type_node, tmp, cond);
3581 : 72 : se->expr = fold_build3_loc (input_location, COND_EXPR, type,
3582 : : tmp, build_int_cst (type, 1),
3583 : : build_int_cst (type, 0));
3584 : 72 : return 1;
3585 : : }
3586 : : /* If rhs is odd,
3587 : : result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
3588 : 80 : tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp,
3589 : : build_int_cst (type, -1),
3590 : : build_int_cst (type, 0));
3591 : 80 : se->expr = fold_build3_loc (input_location, COND_EXPR, type,
3592 : : cond, build_int_cst (type, 1), tmp);
3593 : 80 : return 1;
3594 : : }
3595 : :
3596 : 925 : memset (vartmp, 0, sizeof (vartmp));
3597 : 925 : vartmp[1] = lhs;
3598 : 925 : if (sgn == -1)
3599 : : {
3600 : 91 : tmp = gfc_build_const (type, integer_one_node);
3601 : 91 : vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
3602 : : vartmp[1]);
3603 : : }
3604 : :
3605 : 925 : se->expr = gfc_conv_powi (se, n, vartmp);
3606 : :
3607 : 925 : return 1;
3608 : : }
3609 : :
3610 : : /* Convert lhs**rhs, for constant rhs, when both are unsigned.
3611 : : Method:
3612 : : if (rhs == 0) ! Checked here.
3613 : : return 1;
3614 : : if (lhs & 1 == 1) ! odd_cnd
3615 : : {
3616 : : if (bit_size(rhs) < bit_size(lhs)) ! Checked here.
3617 : : return lhs ** rhs;
3618 : :
3619 : : mask = 1 << (bit_size(a) - 1) / 2;
3620 : : return lhs ** (n & rhs);
3621 : : }
3622 : : if (rhs > bit_size(lhs)) ! Checked here.
3623 : : return 0;
3624 : :
3625 : : return lhs ** rhs;
3626 : : */
3627 : :
3628 : : static int
3629 : 15120 : gfc_conv_cst_uint_power (gfc_se * se, tree lhs, tree rhs)
3630 : : {
3631 : 15120 : tree type = TREE_TYPE (lhs);
3632 : 15120 : tree tmp, is_odd, odd_branch, even_branch;
3633 : 15120 : unsigned HOST_WIDE_INT lhs_prec, rhs_prec;
3634 : 15120 : wi::tree_to_wide_ref wrhs = wi::to_wide (rhs);
3635 : 15120 : unsigned HOST_WIDE_INT n, n_odd;
3636 : 15120 : tree vartmp_odd[POWI_TABLE_SIZE], vartmp_even[POWI_TABLE_SIZE];
3637 : :
3638 : : /* Anything ** 0 is one. */
3639 : 15120 : if (integer_zerop (rhs))
3640 : : {
3641 : 1800 : se->expr = build_int_cst (type, 1);
3642 : 1800 : return 1;
3643 : : }
3644 : :
3645 : 13320 : if (!wi::fits_uhwi_p (wrhs))
3646 : : return 0;
3647 : :
3648 : 12960 : n = wrhs.to_uhwi ();
3649 : :
3650 : : /* tmp = a & 1; . */
3651 : 12960 : tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type,
3652 : : lhs, build_int_cst (type, 1));
3653 : 12960 : is_odd = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3654 : : tmp, build_int_cst (type, 1));
3655 : :
3656 : 12960 : lhs_prec = TYPE_PRECISION (type);
3657 : 12960 : rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs));
3658 : :
3659 : 12960 : if (rhs_prec >= lhs_prec && lhs_prec <= HOST_BITS_PER_WIDE_INT)
3660 : : {
3661 : 7044 : unsigned HOST_WIDE_INT mask = (HOST_WIDE_INT_1U << (lhs_prec - 1)) - 1;
3662 : 7044 : n_odd = n & mask;
3663 : : }
3664 : : else
3665 : : n_odd = n;
3666 : :
3667 : 12960 : memset (vartmp_odd, 0, sizeof (vartmp_odd));
3668 : 12960 : vartmp_odd[0] = build_int_cst (type, 1);
3669 : 12960 : vartmp_odd[1] = lhs;
3670 : 12960 : odd_branch = gfc_conv_powi (se, n_odd, vartmp_odd);
3671 : 12960 : even_branch = NULL_TREE;
3672 : :
3673 : 12960 : if (n > lhs_prec)
3674 : 4260 : even_branch = build_int_cst (type, 0);
3675 : : else
3676 : : {
3677 : 8700 : if (n_odd != n)
3678 : : {
3679 : 0 : memset (vartmp_even, 0, sizeof (vartmp_even));
3680 : 0 : vartmp_even[0] = build_int_cst (type, 1);
3681 : 0 : vartmp_even[1] = lhs;
3682 : 0 : even_branch = gfc_conv_powi (se, n, vartmp_even);
3683 : : }
3684 : : }
3685 : 4260 : if (even_branch != NULL_TREE)
3686 : 4260 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, is_odd,
3687 : : odd_branch, even_branch);
3688 : : else
3689 : 8700 : se->expr = odd_branch;
3690 : :
3691 : : return 1;
3692 : : }
3693 : :
3694 : : /* Power op (**). Constant integer exponent and powers of 2 have special
3695 : : handling. */
3696 : :
3697 : : static void
3698 : 48547 : gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
3699 : : {
3700 : 48547 : tree gfc_int4_type_node;
3701 : 48547 : int kind;
3702 : 48547 : int ikind;
3703 : 48547 : int res_ikind_1, res_ikind_2;
3704 : 48547 : gfc_se lse;
3705 : 48547 : gfc_se rse;
3706 : 48547 : tree fndecl = NULL;
3707 : :
3708 : 48547 : gfc_init_se (&lse, se);
3709 : 48547 : gfc_conv_expr_val (&lse, expr->value.op.op1);
3710 : 48547 : lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
3711 : 48547 : gfc_add_block_to_block (&se->pre, &lse.pre);
3712 : :
3713 : 48547 : gfc_init_se (&rse, se);
3714 : 48547 : gfc_conv_expr_val (&rse, expr->value.op.op2);
3715 : 48547 : gfc_add_block_to_block (&se->pre, &rse.pre);
3716 : :
3717 : 48547 : if (expr->value.op.op2->expr_type == EXPR_CONSTANT)
3718 : : {
3719 : 16994 : if (expr->value.op.op2->ts.type == BT_INTEGER)
3720 : : {
3721 : 1723 : if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
3722 : 20023 : return;
3723 : : }
3724 : 15271 : else if (expr->value.op.op2->ts.type == BT_UNSIGNED)
3725 : : {
3726 : 15120 : if (gfc_conv_cst_uint_power (se, lse.expr, rse.expr))
3727 : : return;
3728 : : }
3729 : : }
3730 : :
3731 : 32543 : if ((expr->value.op.op2->ts.type == BT_INTEGER
3732 : 31461 : || expr->value.op.op2->ts.type == BT_UNSIGNED)
3733 : 31682 : && expr->value.op.op2->expr_type == EXPR_CONSTANT)
3734 : 839 : if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
3735 : : return;
3736 : :
3737 : 32543 : if (INTEGER_CST_P (lse.expr)
3738 : 15365 : && TREE_CODE (TREE_TYPE (rse.expr)) == INTEGER_TYPE
3739 : 47908 : && expr->value.op.op2->ts.type == BT_INTEGER)
3740 : : {
3741 : 245 : wi::tree_to_wide_ref wlhs = wi::to_wide (lse.expr);
3742 : 245 : HOST_WIDE_INT v;
3743 : 245 : unsigned HOST_WIDE_INT w;
3744 : 245 : int kind, ikind, bit_size;
3745 : :
3746 : 245 : v = wlhs.to_shwi ();
3747 : 245 : w = absu_hwi (v);
3748 : :
3749 : 245 : kind = expr->value.op.op1->ts.kind;
3750 : 245 : ikind = gfc_validate_kind (BT_INTEGER, kind, false);
3751 : 245 : bit_size = gfc_integer_kinds[ikind].bit_size;
3752 : :
3753 : 245 : if (v == 1)
3754 : : {
3755 : : /* 1**something is always 1. */
3756 : 35 : se->expr = build_int_cst (TREE_TYPE (lse.expr), 1);
3757 : 239 : return;
3758 : : }
3759 : 210 : else if (v == -1)
3760 : : {
3761 : : /* (-1)**n is 1 - ((n & 1) << 1) */
3762 : 34 : tree type;
3763 : 34 : tree tmp;
3764 : :
3765 : 34 : type = TREE_TYPE (lse.expr);
3766 : 34 : tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type,
3767 : : rse.expr, build_int_cst (type, 1));
3768 : 34 : tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3769 : : tmp, build_int_cst (type, 1));
3770 : 34 : tmp = fold_build2_loc (input_location, MINUS_EXPR, type,
3771 : : build_int_cst (type, 1), tmp);
3772 : 34 : se->expr = tmp;
3773 : 34 : return;
3774 : : }
3775 : 176 : else if (w > 0 && ((w & (w-1)) == 0) && ((w >> (bit_size-1)) == 0))
3776 : : {
3777 : : /* Here v is +/- 2**e. The further simplification uses
3778 : : 2**n = 1<<n, 4**n = 1<<(n+n), 8**n = 1 <<(3*n), 16**n =
3779 : : 1<<(4*n), etc., but we have to make sure to return zero
3780 : : if the number of bits is too large. */
3781 : 170 : tree lshift;
3782 : 170 : tree type;
3783 : 170 : tree shift;
3784 : 170 : tree ge;
3785 : 170 : tree cond;
3786 : 170 : tree num_bits;
3787 : 170 : tree cond2;
3788 : 170 : tree tmp1;
3789 : :
3790 : 170 : type = TREE_TYPE (lse.expr);
3791 : :
3792 : 170 : if (w == 2)
3793 : 110 : shift = rse.expr;
3794 : 60 : else if (w == 4)
3795 : 12 : shift = fold_build2_loc (input_location, PLUS_EXPR,
3796 : 12 : TREE_TYPE (rse.expr),
3797 : : rse.expr, rse.expr);
3798 : : else
3799 : : {
3800 : : /* use popcount for fast log2(w) */
3801 : 48 : int e = wi::popcount (w-1);
3802 : 96 : shift = fold_build2_loc (input_location, MULT_EXPR,
3803 : 48 : TREE_TYPE (rse.expr),
3804 : 48 : build_int_cst (TREE_TYPE (rse.expr), e),
3805 : : rse.expr);
3806 : : }
3807 : :
3808 : 170 : lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3809 : : build_int_cst (type, 1), shift);
3810 : 170 : ge = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
3811 : : rse.expr, build_int_cst (type, 0));
3812 : 170 : cond = fold_build3_loc (input_location, COND_EXPR, type, ge, lshift,
3813 : : build_int_cst (type, 0));
3814 : 170 : num_bits = build_int_cst (TREE_TYPE (rse.expr), TYPE_PRECISION (type));
3815 : 170 : cond2 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
3816 : : rse.expr, num_bits);
3817 : 170 : tmp1 = fold_build3_loc (input_location, COND_EXPR, type, cond2,
3818 : : build_int_cst (type, 0), cond);
3819 : 170 : if (v > 0)
3820 : : {
3821 : 128 : se->expr = tmp1;
3822 : : }
3823 : : else
3824 : : {
3825 : : /* for v < 0, calculate v**n = |v|**n * (-1)**n */
3826 : 42 : tree tmp2;
3827 : 42 : tmp2 = fold_build2_loc (input_location, BIT_AND_EXPR, type,
3828 : : rse.expr, build_int_cst (type, 1));
3829 : 42 : tmp2 = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3830 : : tmp2, build_int_cst (type, 1));
3831 : 42 : tmp2 = fold_build2_loc (input_location, MINUS_EXPR, type,
3832 : : build_int_cst (type, 1), tmp2);
3833 : 42 : se->expr = fold_build2_loc (input_location, MULT_EXPR, type,
3834 : : tmp1, tmp2);
3835 : : }
3836 : 170 : return;
3837 : : }
3838 : : }
3839 : : /* Handle unsigned separate from signed above, things would be too
3840 : : complicated otherwise. */
3841 : :
3842 : 32304 : if (INTEGER_CST_P (lse.expr) && expr->value.op.op1->ts.type == BT_UNSIGNED)
3843 : : {
3844 : 15120 : gfc_expr * op1 = expr->value.op.op1;
3845 : 15120 : tree type;
3846 : :
3847 : 15120 : type = TREE_TYPE (lse.expr);
3848 : :
3849 : 15120 : if (mpz_cmp_ui (op1->value.integer, 1) == 0)
3850 : : {
3851 : : /* 1**something is always 1. */
3852 : 1260 : se->expr = build_int_cst (type, 1);
3853 : 1260 : return;
3854 : : }
3855 : :
3856 : : /* Simplify 2u**x to a shift, with the value set to zero if it falls
3857 : : outside the range. */
3858 : 26460 : if (mpz_popcount (op1->value.integer) == 1)
3859 : : {
3860 : 2520 : tree prec_m1, lim, shift, lshift, cond, tmp;
3861 : 2520 : tree rtype = TREE_TYPE (rse.expr);
3862 : 2520 : int e = mpz_scan1 (op1->value.integer, 0);
3863 : :
3864 : 2520 : shift = fold_build2_loc (input_location, MULT_EXPR,
3865 : 2520 : rtype, build_int_cst (rtype, e),
3866 : : rse.expr);
3867 : 2520 : lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3868 : : build_int_cst (type, 1), shift);
3869 : 5040 : prec_m1 = fold_build2_loc (input_location, MINUS_EXPR, rtype,
3870 : 2520 : build_int_cst (rtype, TYPE_PRECISION (type)),
3871 : : build_int_cst (rtype, 1));
3872 : 2520 : lim = fold_build2_loc (input_location, TRUNC_DIV_EXPR, rtype,
3873 : 2520 : prec_m1, build_int_cst (rtype, e));
3874 : 2520 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3875 : : rse.expr, lim);
3876 : 2520 : tmp = fold_build3_loc (input_location, COND_EXPR, type, cond,
3877 : : build_int_cst (type, 0), lshift);
3878 : 2520 : se->expr = tmp;
3879 : 2520 : return;
3880 : : }
3881 : : }
3882 : :
3883 : 28524 : gfc_int4_type_node = gfc_get_int_type (4);
3884 : :
3885 : : /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
3886 : : library routine. But in the end, we have to convert the result back
3887 : : if this case applies -- with res_ikind_K, we keep track whether operand K
3888 : : falls into this case. */
3889 : 28524 : res_ikind_1 = -1;
3890 : 28524 : res_ikind_2 = -1;
3891 : :
3892 : 28524 : kind = expr->value.op.op1->ts.kind;
3893 : 28524 : switch (expr->value.op.op2->ts.type)
3894 : : {
3895 : 843 : case BT_INTEGER:
3896 : 843 : ikind = expr->value.op.op2->ts.kind;
3897 : 843 : switch (ikind)
3898 : : {
3899 : 144 : case 1:
3900 : 144 : case 2:
3901 : 144 : rse.expr = convert (gfc_int4_type_node, rse.expr);
3902 : 144 : res_ikind_2 = ikind;
3903 : : /* Fall through. */
3904 : :
3905 : : case 4:
3906 : : ikind = 0;
3907 : : break;
3908 : :
3909 : : case 8:
3910 : : ikind = 1;
3911 : : break;
3912 : :
3913 : 6 : case 16:
3914 : 6 : ikind = 2;
3915 : 6 : break;
3916 : :
3917 : 0 : default:
3918 : 0 : gcc_unreachable ();
3919 : : }
3920 : 843 : switch (kind)
3921 : : {
3922 : 0 : case 1:
3923 : 0 : case 2:
3924 : 0 : if (expr->value.op.op1->ts.type == BT_INTEGER)
3925 : : {
3926 : 0 : lse.expr = convert (gfc_int4_type_node, lse.expr);
3927 : 0 : res_ikind_1 = kind;
3928 : : }
3929 : : else
3930 : 0 : gcc_unreachable ();
3931 : : /* Fall through. */
3932 : :
3933 : : case 4:
3934 : : kind = 0;
3935 : : break;
3936 : :
3937 : : case 8:
3938 : : kind = 1;
3939 : : break;
3940 : :
3941 : 6 : case 10:
3942 : 6 : kind = 2;
3943 : 6 : break;
3944 : :
3945 : 18 : case 16:
3946 : 18 : kind = 3;
3947 : 18 : break;
3948 : :
3949 : 0 : default:
3950 : 0 : gcc_unreachable ();
3951 : : }
3952 : :
3953 : 843 : switch (expr->value.op.op1->ts.type)
3954 : : {
3955 : 99 : case BT_INTEGER:
3956 : 99 : if (kind == 3) /* Case 16 was not handled properly above. */
3957 : : kind = 2;
3958 : 99 : fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
3959 : 99 : break;
3960 : :
3961 : 557 : case BT_REAL:
3962 : : /* Use builtins for real ** int4. */
3963 : 557 : if (ikind == 0)
3964 : : {
3965 : 500 : switch (kind)
3966 : : {
3967 : 327 : case 0:
3968 : 327 : fndecl = builtin_decl_explicit (BUILT_IN_POWIF);
3969 : 327 : break;
3970 : :
3971 : 155 : case 1:
3972 : 155 : fndecl = builtin_decl_explicit (BUILT_IN_POWI);
3973 : 155 : break;
3974 : :
3975 : 6 : case 2:
3976 : 6 : fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
3977 : 6 : break;
3978 : :
3979 : 12 : case 3:
3980 : : /* Use the __builtin_powil() only if real(kind=16) is
3981 : : actually the C long double type. */
3982 : 12 : if (!gfc_real16_is_float128)
3983 : 0 : fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
3984 : : break;
3985 : :
3986 : : default:
3987 : : gcc_unreachable ();
3988 : : }
3989 : : }
3990 : :
3991 : : /* If we don't have a good builtin for this, go for the
3992 : : library function. */
3993 : 488 : if (!fndecl)
3994 : 69 : fndecl = gfor_fndecl_math_powi[kind][ikind].real;
3995 : : break;
3996 : :
3997 : 187 : case BT_COMPLEX:
3998 : 187 : fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
3999 : 187 : break;
4000 : :
4001 : 0 : default:
4002 : 0 : gcc_unreachable ();
4003 : : }
4004 : : break;
4005 : :
4006 : 132 : case BT_REAL:
4007 : 132 : fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
4008 : 132 : break;
4009 : :
4010 : 729 : case BT_COMPLEX:
4011 : 729 : fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
4012 : 729 : break;
4013 : :
4014 : 26820 : case BT_UNSIGNED:
4015 : 26820 : {
4016 : : /* Valid kinds for unsigned are 1, 2, 4, 8, 16. Instead of using a
4017 : : large switch statement, let's just use __builtin_ctz. */
4018 : 26820 : int base = __builtin_ctz (expr->value.op.op1->ts.kind);
4019 : 26820 : int expon = __builtin_ctz (expr->value.op.op2->ts.kind);
4020 : 26820 : fndecl = gfor_fndecl_unsigned_pow_list[base][expon];
4021 : : }
4022 : 26820 : break;
4023 : :
4024 : 0 : default:
4025 : 0 : gcc_unreachable ();
4026 : 28524 : break;
4027 : : }
4028 : :
4029 : 28524 : se->expr = build_call_expr_loc (input_location,
4030 : : fndecl, 2, lse.expr, rse.expr);
4031 : :
4032 : : /* Convert the result back if it is of wrong integer kind. */
4033 : 28524 : if (res_ikind_1 != -1 && res_ikind_2 != -1)
4034 : : {
4035 : : /* We want the maximum of both operand kinds as result. */
4036 : 0 : if (res_ikind_1 < res_ikind_2)
4037 : 0 : res_ikind_1 = res_ikind_2;
4038 : 0 : se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr);
4039 : : }
4040 : : }
4041 : :
4042 : :
4043 : : /* Generate code to allocate a string temporary. */
4044 : :
4045 : : tree
4046 : 4625 : gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
4047 : : {
4048 : 4625 : tree var;
4049 : 4625 : tree tmp;
4050 : :
4051 : 4625 : if (gfc_can_put_var_on_stack (len))
4052 : : {
4053 : : /* Create a temporary variable to hold the result. */
4054 : 4044 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
4055 : 2022 : TREE_TYPE (len), len,
4056 : 2022 : build_int_cst (TREE_TYPE (len), 1));
4057 : 2022 : tmp = build_range_type (gfc_charlen_type_node, size_zero_node, tmp);
4058 : :
4059 : 2022 : if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
4060 : 1992 : tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
4061 : : else
4062 : 30 : tmp = build_array_type (TREE_TYPE (type), tmp);
4063 : :
4064 : 2022 : var = gfc_create_var (tmp, "str");
4065 : 2022 : var = gfc_build_addr_expr (type, var);
4066 : : }
4067 : : else
4068 : : {
4069 : : /* Allocate a temporary to hold the result. */
4070 : 2603 : var = gfc_create_var (type, "pstr");
4071 : 2603 : gcc_assert (POINTER_TYPE_P (type));
4072 : 2603 : tmp = TREE_TYPE (type);
4073 : 2603 : if (TREE_CODE (tmp) == ARRAY_TYPE)
4074 : 2561 : tmp = TREE_TYPE (tmp);
4075 : 2603 : tmp = TYPE_SIZE_UNIT (tmp);
4076 : 2603 : tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
4077 : : fold_convert (size_type_node, len),
4078 : : fold_convert (size_type_node, tmp));
4079 : 2603 : tmp = gfc_call_malloc (&se->pre, type, tmp);
4080 : 2603 : gfc_add_modify (&se->pre, var, tmp);
4081 : :
4082 : : /* Free the temporary afterwards. */
4083 : 2603 : tmp = gfc_call_free (var);
4084 : 2603 : gfc_add_expr_to_block (&se->post, tmp);
4085 : : }
4086 : :
4087 : 4625 : return var;
4088 : : }
4089 : :
4090 : :
4091 : : /* Handle a string concatenation operation. A temporary will be allocated to
4092 : : hold the result. */
4093 : :
4094 : : static void
4095 : 1168 : gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
4096 : : {
4097 : 1168 : gfc_se lse, rse;
4098 : 1168 : tree len, type, var, tmp, fndecl;
4099 : :
4100 : 1168 : gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
4101 : : && expr->value.op.op2->ts.type == BT_CHARACTER);
4102 : 1168 : gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
4103 : :
4104 : 1168 : gfc_init_se (&lse, se);
4105 : 1168 : gfc_conv_expr (&lse, expr->value.op.op1);
4106 : 1168 : gfc_conv_string_parameter (&lse);
4107 : 1168 : gfc_init_se (&rse, se);
4108 : 1168 : gfc_conv_expr (&rse, expr->value.op.op2);
4109 : 1168 : gfc_conv_string_parameter (&rse);
4110 : :
4111 : 1168 : gfc_add_block_to_block (&se->pre, &lse.pre);
4112 : 1168 : gfc_add_block_to_block (&se->pre, &rse.pre);
4113 : :
4114 : 1168 : type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
4115 : 1168 : len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
4116 : 1168 : if (len == NULL_TREE)
4117 : : {
4118 : 1029 : len = fold_build2_loc (input_location, PLUS_EXPR,
4119 : : gfc_charlen_type_node,
4120 : : fold_convert (gfc_charlen_type_node,
4121 : : lse.string_length),
4122 : : fold_convert (gfc_charlen_type_node,
4123 : : rse.string_length));
4124 : : }
4125 : :
4126 : 1168 : type = build_pointer_type (type);
4127 : :
4128 : 1168 : var = gfc_conv_string_tmp (se, type, len);
4129 : :
4130 : : /* Do the actual concatenation. */
4131 : 1168 : if (expr->ts.kind == 1)
4132 : 1079 : fndecl = gfor_fndecl_concat_string;
4133 : 89 : else if (expr->ts.kind == 4)
4134 : 89 : fndecl = gfor_fndecl_concat_string_char4;
4135 : : else
4136 : 0 : gcc_unreachable ();
4137 : :
4138 : 1168 : tmp = build_call_expr_loc (input_location,
4139 : : fndecl, 6, len, var, lse.string_length, lse.expr,
4140 : : rse.string_length, rse.expr);
4141 : 1168 : gfc_add_expr_to_block (&se->pre, tmp);
4142 : :
4143 : : /* Add the cleanup for the operands. */
4144 : 1168 : gfc_add_block_to_block (&se->pre, &rse.post);
4145 : 1168 : gfc_add_block_to_block (&se->pre, &lse.post);
4146 : :
4147 : 1168 : se->expr = var;
4148 : 1168 : se->string_length = len;
4149 : 1168 : }
4150 : :
4151 : : /* Translates an op expression. Common (binary) cases are handled by this
4152 : : function, others are passed on. Recursion is used in either case.
4153 : : We use the fact that (op1.ts == op2.ts) (except for the power
4154 : : operator **).
4155 : : Operators need no special handling for scalarized expressions as long as
4156 : : they call gfc_conv_simple_val to get their operands.
4157 : : Character strings get special handling. */
4158 : :
4159 : : static void
4160 : 489976 : gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
4161 : : {
4162 : 489976 : enum tree_code code;
4163 : 489976 : gfc_se lse;
4164 : 489976 : gfc_se rse;
4165 : 489976 : tree tmp, type;
4166 : 489976 : int lop;
4167 : 489976 : int checkstring;
4168 : :
4169 : 489976 : checkstring = 0;
4170 : 489976 : lop = 0;
4171 : 489976 : switch (expr->value.op.op)
4172 : : {
4173 : 14571 : case INTRINSIC_PARENTHESES:
4174 : 14571 : if ((expr->ts.type == BT_REAL || expr->ts.type == BT_COMPLEX)
4175 : 3446 : && flag_protect_parens)
4176 : : {
4177 : 3313 : gfc_conv_unary_op (PAREN_EXPR, se, expr);
4178 : 3313 : gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
4179 : 89031 : return;
4180 : : }
4181 : :
4182 : : /* Fallthrough. */
4183 : 11264 : case INTRINSIC_UPLUS:
4184 : 11264 : gfc_conv_expr (se, expr->value.op.op1);
4185 : 11264 : return;
4186 : :
4187 : 4897 : case INTRINSIC_UMINUS:
4188 : 4897 : gfc_conv_unary_op (NEGATE_EXPR, se, expr);
4189 : 4897 : return;
4190 : :
4191 : 19842 : case INTRINSIC_NOT:
4192 : 19842 : gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
4193 : 19842 : return;
4194 : :
4195 : : case INTRINSIC_PLUS:
4196 : : code = PLUS_EXPR;
4197 : : break;
4198 : :
4199 : 26895 : case INTRINSIC_MINUS:
4200 : 26895 : code = MINUS_EXPR;
4201 : 26895 : break;
4202 : :
4203 : 30520 : case INTRINSIC_TIMES:
4204 : 30520 : code = MULT_EXPR;
4205 : 30520 : break;
4206 : :
4207 : 6318 : case INTRINSIC_DIVIDE:
4208 : : /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
4209 : : an integer or unsigned, we must round towards zero, so we use a
4210 : : TRUNC_DIV_EXPR. */
4211 : 6318 : if (expr->ts.type == BT_INTEGER || expr->ts.type == BT_UNSIGNED)
4212 : : code = TRUNC_DIV_EXPR;
4213 : : else
4214 : 400945 : code = RDIV_EXPR;
4215 : : break;
4216 : :
4217 : 48547 : case INTRINSIC_POWER:
4218 : 48547 : gfc_conv_power_op (se, expr);
4219 : 48547 : return;
4220 : :
4221 : 1168 : case INTRINSIC_CONCAT:
4222 : 1168 : gfc_conv_concat_op (se, expr);
4223 : 1168 : return;
4224 : :
4225 : 4700 : case INTRINSIC_AND:
4226 : 4700 : code = flag_frontend_optimize ? TRUTH_ANDIF_EXPR : TRUTH_AND_EXPR;
4227 : : lop = 1;
4228 : : break;
4229 : :
4230 : 55430 : case INTRINSIC_OR:
4231 : 55430 : code = flag_frontend_optimize ? TRUTH_ORIF_EXPR : TRUTH_OR_EXPR;
4232 : : lop = 1;
4233 : : break;
4234 : :
4235 : : /* EQV and NEQV only work on logicals, but since we represent them
4236 : : as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
4237 : 12144 : case INTRINSIC_EQ:
4238 : 12144 : case INTRINSIC_EQ_OS:
4239 : 12144 : case INTRINSIC_EQV:
4240 : 12144 : code = EQ_EXPR;
4241 : 12144 : checkstring = 1;
4242 : 12144 : lop = 1;
4243 : 12144 : break;
4244 : :
4245 : 200401 : case INTRINSIC_NE:
4246 : 200401 : case INTRINSIC_NE_OS:
4247 : 200401 : case INTRINSIC_NEQV:
4248 : 200401 : code = NE_EXPR;
4249 : 200401 : checkstring = 1;
4250 : 200401 : lop = 1;
4251 : 200401 : break;
4252 : :
4253 : 11701 : case INTRINSIC_GT:
4254 : 11701 : case INTRINSIC_GT_OS:
4255 : 11701 : code = GT_EXPR;
4256 : 11701 : checkstring = 1;
4257 : 11701 : lop = 1;
4258 : 11701 : break;
4259 : :
4260 : 1650 : case INTRINSIC_GE:
4261 : 1650 : case INTRINSIC_GE_OS:
4262 : 1650 : code = GE_EXPR;
4263 : 1650 : checkstring = 1;
4264 : 1650 : lop = 1;
4265 : 1650 : break;
4266 : :
4267 : 4264 : case INTRINSIC_LT:
4268 : 4264 : case INTRINSIC_LT_OS:
4269 : 4264 : code = LT_EXPR;
4270 : 4264 : checkstring = 1;
4271 : 4264 : lop = 1;
4272 : 4264 : break;
4273 : :
4274 : 2582 : case INTRINSIC_LE:
4275 : 2582 : case INTRINSIC_LE_OS:
4276 : 2582 : code = LE_EXPR;
4277 : 2582 : checkstring = 1;
4278 : 2582 : lop = 1;
4279 : 2582 : break;
4280 : :
4281 : 0 : case INTRINSIC_USER:
4282 : 0 : case INTRINSIC_ASSIGN:
4283 : : /* These should be converted into function calls by the frontend. */
4284 : 0 : gcc_unreachable ();
4285 : :
4286 : 0 : default:
4287 : 0 : fatal_error (input_location, "Unknown intrinsic op");
4288 : 400945 : return;
4289 : : }
4290 : :
4291 : : /* The only exception to this is **, which is handled separately anyway. */
4292 : 400945 : gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
4293 : :
4294 : 400945 : if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
4295 : 369046 : checkstring = 0;
4296 : :
4297 : : /* lhs */
4298 : 400945 : gfc_init_se (&lse, se);
4299 : 400945 : gfc_conv_expr (&lse, expr->value.op.op1);
4300 : 400945 : gfc_add_block_to_block (&se->pre, &lse.pre);
4301 : :
4302 : : /* rhs */
4303 : 400945 : gfc_init_se (&rse, se);
4304 : 400945 : gfc_conv_expr (&rse, expr->value.op.op2);
4305 : 400945 : gfc_add_block_to_block (&se->pre, &rse.pre);
4306 : :
4307 : 400945 : if (checkstring)
4308 : : {
4309 : 31899 : gfc_conv_string_parameter (&lse);
4310 : 31899 : gfc_conv_string_parameter (&rse);
4311 : :
4312 : 63798 : lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
4313 : : rse.string_length, rse.expr,
4314 : 31899 : expr->value.op.op1->ts.kind,
4315 : : code);
4316 : 31899 : rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
4317 : 31899 : gfc_add_block_to_block (&lse.post, &rse.post);
4318 : : }
4319 : :
4320 : 400945 : type = gfc_typenode_for_spec (&expr->ts);
4321 : :
4322 : 400945 : if (lop)
4323 : : {
4324 : : // Inhibit overeager optimization of Cray pointer comparisons (PR106692).
4325 : 292872 : if (expr->value.op.op1->expr_type == EXPR_VARIABLE
4326 : 164482 : && expr->value.op.op1->ts.type == BT_INTEGER
4327 : 70574 : && expr->value.op.op1->symtree
4328 : 70574 : && expr->value.op.op1->symtree->n.sym->attr.cray_pointer)
4329 : 12 : TREE_THIS_VOLATILE (lse.expr) = 1;
4330 : :
4331 : 292872 : if (expr->value.op.op2->expr_type == EXPR_VARIABLE
4332 : 70981 : && expr->value.op.op2->ts.type == BT_INTEGER
4333 : 12185 : && expr->value.op.op2->symtree
4334 : 12185 : && expr->value.op.op2->symtree->n.sym->attr.cray_pointer)
4335 : 12 : TREE_THIS_VOLATILE (rse.expr) = 1;
4336 : :
4337 : : /* The result of logical ops is always logical_type_node. */
4338 : 292872 : tmp = fold_build2_loc (input_location, code, logical_type_node,
4339 : : lse.expr, rse.expr);
4340 : 292872 : se->expr = convert (type, tmp);
4341 : : }
4342 : : else
4343 : 108073 : se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
4344 : :
4345 : : /* Add the post blocks. */
4346 : 400945 : gfc_add_block_to_block (&se->post, &rse.post);
4347 : 400945 : gfc_add_block_to_block (&se->post, &lse.post);
4348 : : }
4349 : :
4350 : : /* If a string's length is one, we convert it to a single character. */
4351 : :
4352 : : tree
4353 : 131444 : gfc_string_to_single_character (tree len, tree str, int kind)
4354 : : {
4355 : :
4356 : 131444 : if (len == NULL
4357 : 131444 : || !tree_fits_uhwi_p (len)
4358 : 241007 : || !POINTER_TYPE_P (TREE_TYPE (str)))
4359 : : return NULL_TREE;
4360 : :
4361 : 109511 : if (TREE_INT_CST_LOW (len) == 1)
4362 : : {
4363 : 21715 : str = fold_convert (gfc_get_pchar_type (kind), str);
4364 : 21715 : return build_fold_indirect_ref_loc (input_location, str);
4365 : : }
4366 : :
4367 : 87796 : if (kind == 1
4368 : 71794 : && TREE_CODE (str) == ADDR_EXPR
4369 : 61622 : && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
4370 : 44617 : && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
4371 : 27126 : && array_ref_low_bound (TREE_OPERAND (str, 0))
4372 : 27126 : == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
4373 : 27126 : && TREE_INT_CST_LOW (len) > 1
4374 : 113169 : && TREE_INT_CST_LOW (len)
4375 : : == (unsigned HOST_WIDE_INT)
4376 : 25373 : TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
4377 : : {
4378 : 25373 : tree ret = fold_convert (gfc_get_pchar_type (kind), str);
4379 : 25373 : ret = build_fold_indirect_ref_loc (input_location, ret);
4380 : 25373 : if (TREE_CODE (ret) == INTEGER_CST)
4381 : : {
4382 : 25373 : tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
4383 : 25373 : int i, length = TREE_STRING_LENGTH (string_cst);
4384 : 25373 : const char *ptr = TREE_STRING_POINTER (string_cst);
4385 : :
4386 : 37109 : for (i = 1; i < length; i++)
4387 : 36545 : if (ptr[i] != ' ')
4388 : : return NULL_TREE;
4389 : :
4390 : : return ret;
4391 : : }
4392 : : }
4393 : :
4394 : : return NULL_TREE;
4395 : : }
4396 : :
4397 : :
4398 : : static void
4399 : 171 : conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
4400 : : {
4401 : 171 : gcc_assert (expr);
4402 : :
4403 : : /* We used to modify the tree here. Now it is done earlier in
4404 : : the front-end, so we only check it here to avoid regressions. */
4405 : 171 : if (sym->backend_decl)
4406 : : {
4407 : 66 : gcc_assert (TREE_CODE (TREE_TYPE (sym->backend_decl)) == INTEGER_TYPE);
4408 : 66 : gcc_assert (TYPE_UNSIGNED (TREE_TYPE (sym->backend_decl)) == 1);
4409 : 66 : gcc_assert (TYPE_PRECISION (TREE_TYPE (sym->backend_decl)) == CHAR_TYPE_SIZE);
4410 : 66 : gcc_assert (DECL_BY_REFERENCE (sym->backend_decl) == 0);
4411 : : }
4412 : :
4413 : : /* If we have a constant character expression, make it into an
4414 : : integer of type C char. */
4415 : 171 : if ((*expr)->expr_type == EXPR_CONSTANT)
4416 : : {
4417 : 165 : gfc_typespec ts;
4418 : 165 : gfc_clear_ts (&ts);
4419 : :
4420 : 330 : gfc_expr *tmp = gfc_get_int_expr (gfc_default_character_kind, NULL,
4421 : 165 : (*expr)->value.character.string[0]);
4422 : 165 : gfc_replace_expr (*expr, tmp);
4423 : : }
4424 : 6 : else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
4425 : : {
4426 : 6 : if ((*expr)->ref == NULL)
4427 : : {
4428 : 6 : se->expr = gfc_string_to_single_character
4429 : 6 : (integer_one_node,
4430 : 6 : gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
4431 : : gfc_get_symbol_decl
4432 : 6 : ((*expr)->symtree->n.sym)),
4433 : : (*expr)->ts.kind);
4434 : : }
4435 : : else
4436 : : {
4437 : 0 : gfc_conv_variable (se, *expr);
4438 : 0 : se->expr = gfc_string_to_single_character
4439 : 0 : (integer_one_node,
4440 : : gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
4441 : : se->expr),
4442 : 0 : (*expr)->ts.kind);
4443 : : }
4444 : : }
4445 : 171 : }
4446 : :
4447 : : /* Helper function for gfc_build_compare_string. Return LEN_TRIM value
4448 : : if STR is a string literal, otherwise return -1. */
4449 : :
4450 : : static int
4451 : 29552 : gfc_optimize_len_trim (tree len, tree str, int kind)
4452 : : {
4453 : 29552 : if (kind == 1
4454 : 24854 : && TREE_CODE (str) == ADDR_EXPR
4455 : 21541 : && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
4456 : 13972 : && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
4457 : 8904 : && array_ref_low_bound (TREE_OPERAND (str, 0))
4458 : 8904 : == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
4459 : 8904 : && tree_fits_uhwi_p (len)
4460 : 8904 : && tree_to_uhwi (len) >= 1
4461 : 29552 : && tree_to_uhwi (len)
4462 : 8860 : == (unsigned HOST_WIDE_INT)
4463 : 8860 : TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
4464 : : {
4465 : 8860 : tree folded = fold_convert (gfc_get_pchar_type (kind), str);
4466 : 8860 : folded = build_fold_indirect_ref_loc (input_location, folded);
4467 : 8860 : if (TREE_CODE (folded) == INTEGER_CST)
4468 : : {
4469 : 8860 : tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
4470 : 8860 : int length = TREE_STRING_LENGTH (string_cst);
4471 : 8860 : const char *ptr = TREE_STRING_POINTER (string_cst);
4472 : :
4473 : 12001 : for (; length > 0; length--)
4474 : 12001 : if (ptr[length - 1] != ' ')
4475 : : break;
4476 : :
4477 : : return length;
4478 : : }
4479 : : }
4480 : : return -1;
4481 : : }
4482 : :
4483 : : /* Helper to build a call to memcmp. */
4484 : :
4485 : : static tree
4486 : 11791 : build_memcmp_call (tree s1, tree s2, tree n)
4487 : : {
4488 : 11791 : tree tmp;
4489 : :
4490 : 11791 : if (!POINTER_TYPE_P (TREE_TYPE (s1)))
4491 : 0 : s1 = gfc_build_addr_expr (pvoid_type_node, s1);
4492 : : else
4493 : 11791 : s1 = fold_convert (pvoid_type_node, s1);
4494 : :
4495 : 11791 : if (!POINTER_TYPE_P (TREE_TYPE (s2)))
4496 : 0 : s2 = gfc_build_addr_expr (pvoid_type_node, s2);
4497 : : else
4498 : 11791 : s2 = fold_convert (pvoid_type_node, s2);
4499 : :
4500 : 11791 : n = fold_convert (size_type_node, n);
4501 : :
4502 : 11791 : tmp = build_call_expr_loc (input_location,
4503 : : builtin_decl_explicit (BUILT_IN_MEMCMP),
4504 : : 3, s1, s2, n);
4505 : :
4506 : 11791 : return fold_convert (integer_type_node, tmp);
4507 : : }
4508 : :
4509 : : /* Compare two strings. If they are all single characters, the result is the
4510 : : subtraction of them. Otherwise, we build a library call. */
4511 : :
4512 : : tree
4513 : 31998 : gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
4514 : : enum tree_code code)
4515 : : {
4516 : 31998 : tree sc1;
4517 : 31998 : tree sc2;
4518 : 31998 : tree fndecl;
4519 : :
4520 : 31998 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
4521 : 31998 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
4522 : :
4523 : 31998 : sc1 = gfc_string_to_single_character (len1, str1, kind);
4524 : 31998 : sc2 = gfc_string_to_single_character (len2, str2, kind);
4525 : :
4526 : 31998 : if (sc1 != NULL_TREE && sc2 != NULL_TREE)
4527 : : {
4528 : : /* Deal with single character specially. */
4529 : 4718 : sc1 = fold_convert (integer_type_node, sc1);
4530 : 4718 : sc2 = fold_convert (integer_type_node, sc2);
4531 : 4718 : return fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
4532 : 4718 : sc1, sc2);
4533 : : }
4534 : :
4535 : 27280 : if ((code == EQ_EXPR || code == NE_EXPR)
4536 : 26724 : && optimize
4537 : 22461 : && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
4538 : : {
4539 : : /* If one string is a string literal with LEN_TRIM longer
4540 : : than the length of the second string, the strings
4541 : : compare unequal. */
4542 : 14776 : int len = gfc_optimize_len_trim (len1, str1, kind);
4543 : 14776 : if (len > 0 && compare_tree_int (len2, len) < 0)
4544 : 0 : return integer_one_node;
4545 : 14776 : len = gfc_optimize_len_trim (len2, str2, kind);
4546 : 14776 : if (len > 0 && compare_tree_int (len1, len) < 0)
4547 : 0 : return integer_one_node;
4548 : : }
4549 : :
4550 : : /* We can compare via memcpy if the strings are known to be equal
4551 : : in length and they are
4552 : : - kind=1
4553 : : - kind=4 and the comparison is for (in)equality. */
4554 : :
4555 : 17952 : if (INTEGER_CST_P (len1) && INTEGER_CST_P (len2)
4556 : 17614 : && tree_int_cst_equal (len1, len2)
4557 : 39131 : && (kind == 1 || code == EQ_EXPR || code == NE_EXPR))
4558 : : {
4559 : 11791 : tree tmp;
4560 : 11791 : tree chartype;
4561 : :
4562 : 11791 : chartype = gfc_get_char_type (kind);
4563 : 11791 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE(len1),
4564 : 11791 : fold_convert (TREE_TYPE(len1),
4565 : : TYPE_SIZE_UNIT(chartype)),
4566 : : len1);
4567 : 11791 : return build_memcmp_call (str1, str2, tmp);
4568 : : }
4569 : :
4570 : : /* Build a call for the comparison. */
4571 : 15489 : if (kind == 1)
4572 : 12653 : fndecl = gfor_fndecl_compare_string;
4573 : 2836 : else if (kind == 4)
4574 : 2836 : fndecl = gfor_fndecl_compare_string_char4;
4575 : : else
4576 : 0 : gcc_unreachable ();
4577 : :
4578 : 15489 : return build_call_expr_loc (input_location, fndecl, 4,
4579 : 15489 : len1, str1, len2, str2);
4580 : : }
4581 : :
4582 : :
4583 : : /* Return the backend_decl for a procedure pointer component. */
4584 : :
4585 : : static tree
4586 : 1713 : get_proc_ptr_comp (gfc_expr *e)
4587 : : {
4588 : 1713 : gfc_se comp_se;
4589 : 1713 : gfc_expr *e2;
4590 : 1713 : expr_t old_type;
4591 : :
4592 : 1713 : gfc_init_se (&comp_se, NULL);
4593 : 1713 : e2 = gfc_copy_expr (e);
4594 : : /* We have to restore the expr type later so that gfc_free_expr frees
4595 : : the exact same thing that was allocated.
4596 : : TODO: This is ugly. */
4597 : 1713 : old_type = e2->expr_type;
4598 : 1713 : e2->expr_type = EXPR_VARIABLE;
4599 : 1713 : gfc_conv_expr (&comp_se, e2);
4600 : 1713 : e2->expr_type = old_type;
4601 : 1713 : gfc_free_expr (e2);
4602 : 1713 : return build_fold_addr_expr_loc (input_location, comp_se.expr);
4603 : : }
4604 : :
4605 : :
4606 : : /* Convert a typebound function reference from a class object. */
4607 : : static void
4608 : 80 : conv_base_obj_fcn_val (gfc_se * se, tree base_object, gfc_expr * expr)
4609 : : {
4610 : 80 : gfc_ref *ref;
4611 : 80 : tree var;
4612 : :
4613 : 80 : if (!VAR_P (base_object))
4614 : : {
4615 : 0 : var = gfc_create_var (TREE_TYPE (base_object), NULL);
4616 : 0 : gfc_add_modify (&se->pre, var, base_object);
4617 : : }
4618 : 80 : se->expr = gfc_class_vptr_get (base_object);
4619 : 80 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
4620 : 80 : ref = expr->ref;
4621 : 308 : while (ref && ref->next)
4622 : : ref = ref->next;
4623 : 80 : gcc_assert (ref && ref->type == REF_COMPONENT);
4624 : 80 : if (ref->u.c.sym->attr.extension)
4625 : 0 : conv_parent_component_references (se, ref);
4626 : 80 : gfc_conv_component_ref (se, ref);
4627 : 80 : se->expr = build_fold_addr_expr_loc (input_location, se->expr);
4628 : 80 : }
4629 : :
4630 : : static tree
4631 : 124326 : get_builtin_fn (gfc_symbol * sym)
4632 : : {
4633 : 124326 : if (!gfc_option.disable_omp_is_initial_device
4634 : 124322 : && flag_openmp && sym->attr.function && sym->ts.type == BT_LOGICAL
4635 : 610 : && !strcmp (sym->name, "omp_is_initial_device"))
4636 : 23 : return builtin_decl_explicit (BUILT_IN_OMP_IS_INITIAL_DEVICE);
4637 : :
4638 : 124303 : if (!gfc_option.disable_omp_get_initial_device
4639 : 124296 : && flag_openmp && sym->attr.function && sym->ts.type == BT_INTEGER
4640 : 4053 : && !strcmp (sym->name, "omp_get_initial_device"))
4641 : 24 : return builtin_decl_explicit (BUILT_IN_OMP_GET_INITIAL_DEVICE);
4642 : :
4643 : 124279 : if (!gfc_option.disable_omp_get_num_devices
4644 : 124272 : && flag_openmp && sym->attr.function && sym->ts.type == BT_INTEGER
4645 : 4029 : && !strcmp (sym->name, "omp_get_num_devices"))
4646 : 70 : return builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_DEVICES);
4647 : :
4648 : 124209 : if (!gfc_option.disable_acc_on_device
4649 : 124029 : && flag_openacc && sym->attr.function && sym->ts.type == BT_LOGICAL
4650 : 1163 : && !strcmp (sym->name, "acc_on_device_h"))
4651 : 390 : return builtin_decl_explicit (BUILT_IN_ACC_ON_DEVICE);
4652 : :
4653 : : return NULL_TREE;
4654 : : }
4655 : :
4656 : : static tree
4657 : 507 : update_builtin_function (tree fn_call, gfc_symbol *sym)
4658 : : {
4659 : 507 : tree fn = TREE_OPERAND (CALL_EXPR_FN (fn_call), 0);
4660 : :
4661 : 507 : if (DECL_FUNCTION_CODE (fn) == BUILT_IN_OMP_IS_INITIAL_DEVICE)
4662 : : /* In Fortran omp_is_initial_device returns logical(4)
4663 : : but the builtin uses 'int'. */
4664 : 23 : return fold_convert (TREE_TYPE (TREE_TYPE (sym->backend_decl)), fn_call);
4665 : :
4666 : 484 : else if (DECL_FUNCTION_CODE (fn) == BUILT_IN_ACC_ON_DEVICE)
4667 : : {
4668 : : /* Likewise for the return type; additionally, the argument it a
4669 : : call-by-value int, Fortran has a by-reference 'integer(4)'. */
4670 : 390 : tree arg = build_fold_indirect_ref_loc (input_location,
4671 : 390 : CALL_EXPR_ARG (fn_call, 0));
4672 : 390 : CALL_EXPR_ARG (fn_call, 0) = fold_convert (integer_type_node, arg);
4673 : 390 : return fold_convert (TREE_TYPE (TREE_TYPE (sym->backend_decl)), fn_call);
4674 : : }
4675 : : return fn_call;
4676 : : }
4677 : :
4678 : : static void
4679 : 126840 : conv_function_val (gfc_se * se, bool *is_builtin, gfc_symbol * sym,
4680 : : gfc_expr * expr, gfc_actual_arglist *actual_args)
4681 : : {
4682 : 126840 : tree tmp;
4683 : :
4684 : 126840 : if (gfc_is_proc_ptr_comp (expr))
4685 : 1713 : tmp = get_proc_ptr_comp (expr);
4686 : 125127 : else if (sym->attr.dummy)
4687 : : {
4688 : 801 : tmp = gfc_get_symbol_decl (sym);
4689 : 801 : if (sym->attr.proc_pointer)
4690 : 83 : tmp = build_fold_indirect_ref_loc (input_location,
4691 : : tmp);
4692 : 801 : gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
4693 : : && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
4694 : : }
4695 : : else
4696 : : {
4697 : 124326 : if (!sym->backend_decl)
4698 : 31165 : sym->backend_decl = gfc_get_extern_function_decl (sym, actual_args);
4699 : :
4700 : 124326 : if ((tmp = get_builtin_fn (sym)) != NULL_TREE)
4701 : 507 : *is_builtin = true;
4702 : : else
4703 : : {
4704 : 123819 : TREE_USED (sym->backend_decl) = 1;
4705 : 123819 : tmp = sym->backend_decl;
4706 : : }
4707 : :
4708 : 124326 : if (sym->attr.cray_pointee)
4709 : : {
4710 : : /* TODO - make the cray pointee a pointer to a procedure,
4711 : : assign the pointer to it and use it for the call. This
4712 : : will do for now! */
4713 : 19 : tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
4714 : 19 : gfc_get_symbol_decl (sym->cp_pointer));
4715 : 19 : tmp = gfc_evaluate_now (tmp, &se->pre);
4716 : : }
4717 : :
4718 : 124326 : if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
4719 : : {
4720 : 123747 : gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
4721 : 123747 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
4722 : : }
4723 : : }
4724 : 126840 : se->expr = tmp;
4725 : 126840 : }
4726 : :
4727 : :
4728 : : /* Initialize MAPPING. */
4729 : :
4730 : : void
4731 : 126957 : gfc_init_interface_mapping (gfc_interface_mapping * mapping)
4732 : : {
4733 : 126957 : mapping->syms = NULL;
4734 : 126957 : mapping->charlens = NULL;
4735 : 126957 : }
4736 : :
4737 : :
4738 : : /* Free all memory held by MAPPING (but not MAPPING itself). */
4739 : :
4740 : : void
4741 : 126957 : gfc_free_interface_mapping (gfc_interface_mapping * mapping)
4742 : : {
4743 : 126957 : gfc_interface_sym_mapping *sym;
4744 : 126957 : gfc_interface_sym_mapping *nextsym;
4745 : 126957 : gfc_charlen *cl;
4746 : 126957 : gfc_charlen *nextcl;
4747 : :
4748 : 167602 : for (sym = mapping->syms; sym; sym = nextsym)
4749 : : {
4750 : 40645 : nextsym = sym->next;
4751 : 40645 : sym->new_sym->n.sym->formal = NULL;
4752 : 40645 : gfc_free_symbol (sym->new_sym->n.sym);
4753 : 40645 : gfc_free_expr (sym->expr);
4754 : 40645 : free (sym->new_sym);
4755 : 40645 : free (sym);
4756 : : }
4757 : 131526 : for (cl = mapping->charlens; cl; cl = nextcl)
4758 : : {
4759 : 4569 : nextcl = cl->next;
4760 : 4569 : gfc_free_expr (cl->length);
4761 : 4569 : free (cl);
4762 : : }
4763 : 126957 : }
4764 : :
4765 : :
4766 : : /* Return a copy of gfc_charlen CL. Add the returned structure to
4767 : : MAPPING so that it will be freed by gfc_free_interface_mapping. */
4768 : :
4769 : : static gfc_charlen *
4770 : 4569 : gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
4771 : : gfc_charlen * cl)
4772 : : {
4773 : 4569 : gfc_charlen *new_charlen;
4774 : :
4775 : 4569 : new_charlen = gfc_get_charlen ();
4776 : 4569 : new_charlen->next = mapping->charlens;
4777 : 4569 : new_charlen->length = gfc_copy_expr (cl->length);
4778 : :
4779 : 4569 : mapping->charlens = new_charlen;
4780 : 4569 : return new_charlen;
4781 : : }
4782 : :
4783 : :
4784 : : /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
4785 : : array variable that can be used as the actual argument for dummy
4786 : : argument SYM. Add any initialization code to BLOCK. PACKED is as
4787 : : for gfc_get_nodesc_array_type and DATA points to the first element
4788 : : in the passed array. */
4789 : :
4790 : : static tree
4791 : 8375 : gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
4792 : : gfc_packed packed, tree data, tree len)
4793 : : {
4794 : 8375 : tree type;
4795 : 8375 : tree var;
4796 : :
4797 : 8375 : if (len != NULL_TREE && (TREE_CONSTANT (len) || VAR_P (len)))
4798 : 58 : type = gfc_get_character_type_len (sym->ts.kind, len);
4799 : : else
4800 : 8317 : type = gfc_typenode_for_spec (&sym->ts);
4801 : 8375 : type = gfc_get_nodesc_array_type (type, sym->as, packed,
4802 : 8351 : !sym->attr.target && !sym->attr.pointer
4803 : 16726 : && !sym->attr.proc_pointer);
4804 : :
4805 : 8375 : var = gfc_create_var (type, "ifm");
4806 : 8375 : gfc_add_modify (block, var, fold_convert (type, data));
4807 : :
4808 : 8375 : return var;
4809 : : }
4810 : :
4811 : :
4812 : : /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
4813 : : and offset of descriptorless array type TYPE given that it has the same
4814 : : size as DESC. Add any set-up code to BLOCK. */
4815 : :
4816 : : static void
4817 : 8105 : gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
4818 : : {
4819 : 8105 : int n;
4820 : 8105 : tree dim;
4821 : 8105 : tree offset;
4822 : 8105 : tree tmp;
4823 : :
4824 : 8105 : offset = gfc_index_zero_node;
4825 : 9180 : for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
4826 : : {
4827 : 1075 : dim = gfc_rank_cst[n];
4828 : 1075 : GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
4829 : 1075 : if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
4830 : : {
4831 : 1 : GFC_TYPE_ARRAY_LBOUND (type, n)
4832 : 1 : = gfc_conv_descriptor_lbound_get (desc, dim);
4833 : 1 : GFC_TYPE_ARRAY_UBOUND (type, n)
4834 : 2 : = gfc_conv_descriptor_ubound_get (desc, dim);
4835 : : }
4836 : 1074 : else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
4837 : : {
4838 : 1074 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
4839 : : gfc_array_index_type,
4840 : : gfc_conv_descriptor_ubound_get (desc, dim),
4841 : : gfc_conv_descriptor_lbound_get (desc, dim));
4842 : 3222 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
4843 : : gfc_array_index_type,
4844 : 1074 : GFC_TYPE_ARRAY_LBOUND (type, n), tmp);
4845 : 1074 : tmp = gfc_evaluate_now (tmp, block);
4846 : 1074 : GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
4847 : : }
4848 : 4300 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
4849 : 1075 : GFC_TYPE_ARRAY_LBOUND (type, n),
4850 : 1075 : GFC_TYPE_ARRAY_STRIDE (type, n));
4851 : 1075 : offset = fold_build2_loc (input_location, MINUS_EXPR,
4852 : : gfc_array_index_type, offset, tmp);
4853 : : }
4854 : 8105 : offset = gfc_evaluate_now (offset, block);
4855 : 8105 : GFC_TYPE_ARRAY_OFFSET (type) = offset;
4856 : 8105 : }
4857 : :
4858 : :
4859 : : /* Extend MAPPING so that it maps dummy argument SYM to the value stored
4860 : : in SE. The caller may still use se->expr and se->string_length after
4861 : : calling this function. */
4862 : :
4863 : : void
4864 : 40645 : gfc_add_interface_mapping (gfc_interface_mapping * mapping,
4865 : : gfc_symbol * sym, gfc_se * se,
4866 : : gfc_expr *expr)
4867 : : {
4868 : 40645 : gfc_interface_sym_mapping *sm;
4869 : 40645 : tree desc;
4870 : 40645 : tree tmp;
4871 : 40645 : tree value;
4872 : 40645 : gfc_symbol *new_sym;
4873 : 40645 : gfc_symtree *root;
4874 : 40645 : gfc_symtree *new_symtree;
4875 : :
4876 : : /* Create a new symbol to represent the actual argument. */
4877 : 40645 : new_sym = gfc_new_symbol (sym->name, NULL);
4878 : 40645 : new_sym->ts = sym->ts;
4879 : 40645 : new_sym->as = gfc_copy_array_spec (sym->as);
4880 : 40645 : new_sym->attr.referenced = 1;
4881 : 40645 : new_sym->attr.dimension = sym->attr.dimension;
4882 : 40645 : new_sym->attr.contiguous = sym->attr.contiguous;
4883 : 40645 : new_sym->attr.codimension = sym->attr.codimension;
4884 : 40645 : new_sym->attr.pointer = sym->attr.pointer;
4885 : 40645 : new_sym->attr.allocatable = sym->attr.allocatable;
4886 : 40645 : new_sym->attr.flavor = sym->attr.flavor;
4887 : 40645 : new_sym->attr.function = sym->attr.function;
4888 : :
4889 : : /* Ensure that the interface is available and that
4890 : : descriptors are passed for array actual arguments. */
4891 : 40645 : if (sym->attr.flavor == FL_PROCEDURE)
4892 : : {
4893 : 36 : new_sym->formal = expr->symtree->n.sym->formal;
4894 : 36 : new_sym->attr.always_explicit
4895 : 36 : = expr->symtree->n.sym->attr.always_explicit;
4896 : : }
4897 : :
4898 : : /* Create a fake symtree for it. */
4899 : 40645 : root = NULL;
4900 : 40645 : new_symtree = gfc_new_symtree (&root, sym->name);
4901 : 40645 : new_symtree->n.sym = new_sym;
4902 : 40645 : gcc_assert (new_symtree == root);
4903 : :
4904 : : /* Create a dummy->actual mapping. */
4905 : 40645 : sm = XCNEW (gfc_interface_sym_mapping);
4906 : 40645 : sm->next = mapping->syms;
4907 : 40645 : sm->old = sym;
4908 : 40645 : sm->new_sym = new_symtree;
4909 : 40645 : sm->expr = gfc_copy_expr (expr);
4910 : 40645 : mapping->syms = sm;
4911 : :
4912 : : /* Stabilize the argument's value. */
4913 : 40645 : if (!sym->attr.function && se)
4914 : 40547 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
4915 : :
4916 : 40645 : if (sym->ts.type == BT_CHARACTER)
4917 : : {
4918 : : /* Create a copy of the dummy argument's length. */
4919 : 2785 : new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
4920 : 2785 : sm->expr->ts.u.cl = new_sym->ts.u.cl;
4921 : :
4922 : : /* If the length is specified as "*", record the length that
4923 : : the caller is passing. We should use the callee's length
4924 : : in all other cases. */
4925 : 2785 : if (!new_sym->ts.u.cl->length && se)
4926 : : {
4927 : 2557 : se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
4928 : 2557 : new_sym->ts.u.cl->backend_decl = se->string_length;
4929 : : }
4930 : : }
4931 : :
4932 : 40631 : if (!se)
4933 : 62 : return;
4934 : :
4935 : : /* Use the passed value as-is if the argument is a function. */
4936 : 40583 : if (sym->attr.flavor == FL_PROCEDURE)
4937 : 36 : value = se->expr;
4938 : :
4939 : : /* If the argument is a pass-by-value scalar, use the value as is. */
4940 : 40547 : else if (!sym->attr.dimension && sym->attr.value)
4941 : 63 : value = se->expr;
4942 : :
4943 : : /* If the argument is either a string or a pointer to a string,
4944 : : convert it to a boundless character type. */
4945 : 40484 : else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
4946 : : {
4947 : 1216 : se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
4948 : 1216 : tmp = gfc_get_character_type_len (sym->ts.kind, se->string_length);
4949 : 1216 : tmp = build_pointer_type (tmp);
4950 : 1216 : if (sym->attr.pointer)
4951 : 126 : value = build_fold_indirect_ref_loc (input_location,
4952 : : se->expr);
4953 : : else
4954 : 1090 : value = se->expr;
4955 : 1216 : value = fold_convert (tmp, value);
4956 : : }
4957 : :
4958 : : /* If the argument is a scalar, a pointer to an array or an allocatable,
4959 : : dereference it. */
4960 : 39268 : else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
4961 : 29396 : value = build_fold_indirect_ref_loc (input_location,
4962 : : se->expr);
4963 : :
4964 : : /* For character(*), use the actual argument's descriptor. */
4965 : 9872 : else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
4966 : 1497 : value = build_fold_indirect_ref_loc (input_location,
4967 : : se->expr);
4968 : :
4969 : : /* If the argument is an array descriptor, use it to determine
4970 : : information about the actual argument's shape. */
4971 : 8375 : else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
4972 : 8375 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
4973 : : {
4974 : : /* Get the actual argument's descriptor. */
4975 : 8105 : desc = build_fold_indirect_ref_loc (input_location,
4976 : : se->expr);
4977 : :
4978 : : /* Create the replacement variable. */
4979 : 8105 : tmp = gfc_conv_descriptor_data_get (desc);
4980 : 8105 : value = gfc_get_interface_mapping_array (&se->pre, sym,
4981 : : PACKED_NO, tmp,
4982 : : se->string_length);
4983 : :
4984 : : /* Use DESC to work out the upper bounds, strides and offset. */
4985 : 8105 : gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
4986 : : }
4987 : : else
4988 : : /* Otherwise we have a packed array. */
4989 : 270 : value = gfc_get_interface_mapping_array (&se->pre, sym,
4990 : : PACKED_FULL, se->expr,
4991 : : se->string_length);
4992 : :
4993 : 40583 : new_sym->backend_decl = value;
4994 : : }
4995 : :
4996 : :
4997 : : /* Called once all dummy argument mappings have been added to MAPPING,
4998 : : but before the mapping is used to evaluate expressions. Pre-evaluate
4999 : : the length of each argument, adding any initialization code to PRE and
5000 : : any finalization code to POST. */
5001 : :
5002 : : static void
5003 : 126920 : gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
5004 : : stmtblock_t * pre, stmtblock_t * post)
5005 : : {
5006 : 126920 : gfc_interface_sym_mapping *sym;
5007 : 126920 : gfc_expr *expr;
5008 : 126920 : gfc_se se;
5009 : :
5010 : 167503 : for (sym = mapping->syms; sym; sym = sym->next)
5011 : 40583 : if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
5012 : 2771 : && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
5013 : : {
5014 : 214 : expr = sym->new_sym->n.sym->ts.u.cl->length;
5015 : 214 : gfc_apply_interface_mapping_to_expr (mapping, expr);
5016 : 214 : gfc_init_se (&se, NULL);
5017 : 214 : gfc_conv_expr (&se, expr);
5018 : 214 : se.expr = fold_convert (gfc_charlen_type_node, se.expr);
5019 : 214 : se.expr = gfc_evaluate_now (se.expr, &se.pre);
5020 : 214 : gfc_add_block_to_block (pre, &se.pre);
5021 : 214 : gfc_add_block_to_block (post, &se.post);
5022 : :
5023 : 214 : sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
5024 : : }
5025 : 126920 : }
5026 : :
5027 : :
5028 : : /* Like gfc_apply_interface_mapping_to_expr, but applied to
5029 : : constructor C. */
5030 : :
5031 : : static void
5032 : 47 : gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
5033 : : gfc_constructor_base base)
5034 : : {
5035 : 47 : gfc_constructor *c;
5036 : 428 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
5037 : : {
5038 : 381 : gfc_apply_interface_mapping_to_expr (mapping, c->expr);
5039 : 381 : if (c->iterator)
5040 : : {
5041 : 6 : gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
5042 : 6 : gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
5043 : 6 : gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
5044 : : }
5045 : : }
5046 : 47 : }
5047 : :
5048 : :
5049 : : /* Like gfc_apply_interface_mapping_to_expr, but applied to
5050 : : reference REF. */
5051 : :
5052 : : static void
5053 : 12445 : gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
5054 : : gfc_ref * ref)
5055 : : {
5056 : 12445 : int n;
5057 : :
5058 : 13888 : for (; ref; ref = ref->next)
5059 : 1443 : switch (ref->type)
5060 : : {
5061 : : case REF_ARRAY:
5062 : 2873 : for (n = 0; n < ref->u.ar.dimen; n++)
5063 : : {
5064 : 1632 : gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
5065 : 1632 : gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
5066 : 1632 : gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
5067 : : }
5068 : : break;
5069 : :
5070 : : case REF_COMPONENT:
5071 : : case REF_INQUIRY:
5072 : : break;
5073 : :
5074 : 43 : case REF_SUBSTRING:
5075 : 43 : gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
5076 : 43 : gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
5077 : 43 : break;
5078 : : }
5079 : 12445 : }
5080 : :
5081 : :
5082 : : /* Convert intrinsic function calls into result expressions. */
5083 : :
5084 : : static bool
5085 : 2184 : gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
5086 : : {
5087 : 2184 : gfc_symbol *sym;
5088 : 2184 : gfc_expr *new_expr;
5089 : 2184 : gfc_expr *arg1;
5090 : 2184 : gfc_expr *arg2;
5091 : 2184 : int d, dup;
5092 : :
5093 : 2184 : arg1 = expr->value.function.actual->expr;
5094 : 2184 : if (expr->value.function.actual->next)
5095 : 2063 : arg2 = expr->value.function.actual->next->expr;
5096 : : else
5097 : : arg2 = NULL;
5098 : :
5099 : 2184 : sym = arg1->symtree->n.sym;
5100 : :
5101 : 2184 : if (sym->attr.dummy)
5102 : : return false;
5103 : :
5104 : 2160 : new_expr = NULL;
5105 : :
5106 : 2160 : switch (expr->value.function.isym->id)
5107 : : {
5108 : 929 : case GFC_ISYM_LEN:
5109 : : /* TODO figure out why this condition is necessary. */
5110 : 929 : if (sym->attr.function
5111 : 43 : && (arg1->ts.u.cl->length == NULL
5112 : 42 : || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
5113 : 42 : && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
5114 : : return false;
5115 : :
5116 : 886 : new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
5117 : 886 : break;
5118 : :
5119 : 228 : case GFC_ISYM_LEN_TRIM:
5120 : 228 : new_expr = gfc_copy_expr (arg1);
5121 : 228 : gfc_apply_interface_mapping_to_expr (mapping, new_expr);
5122 : :
5123 : 228 : if (!new_expr)
5124 : : return false;
5125 : :
5126 : 228 : gfc_replace_expr (arg1, new_expr);
5127 : 228 : return true;
5128 : :
5129 : 588 : case GFC_ISYM_SIZE:
5130 : 588 : if (!sym->as || sym->as->rank == 0)
5131 : : return false;
5132 : :
5133 : 530 : if (arg2 && arg2->expr_type == EXPR_CONSTANT)
5134 : : {
5135 : 360 : dup = mpz_get_si (arg2->value.integer);
5136 : 360 : d = dup - 1;
5137 : : }
5138 : : else
5139 : : {
5140 : 530 : dup = sym->as->rank;
5141 : 530 : d = 0;
5142 : : }
5143 : :
5144 : 542 : for (; d < dup; d++)
5145 : : {
5146 : 530 : gfc_expr *tmp;
5147 : :
5148 : 530 : if (!sym->as->upper[d] || !sym->as->lower[d])
5149 : : {
5150 : 518 : gfc_free_expr (new_expr);
5151 : 518 : return false;
5152 : : }
5153 : :
5154 : 12 : tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
5155 : : gfc_get_int_expr (gfc_default_integer_kind,
5156 : : NULL, 1));
5157 : 12 : tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
5158 : 12 : if (new_expr)
5159 : 0 : new_expr = gfc_multiply (new_expr, tmp);
5160 : : else
5161 : : new_expr = tmp;
5162 : : }
5163 : : break;
5164 : :
5165 : 44 : case GFC_ISYM_LBOUND:
5166 : 44 : case GFC_ISYM_UBOUND:
5167 : : /* TODO These implementations of lbound and ubound do not limit if
5168 : : the size < 0, according to F95's 13.14.53 and 13.14.113. */
5169 : :
5170 : 44 : if (!sym->as || sym->as->rank == 0)
5171 : : return false;
5172 : :
5173 : 44 : if (arg2 && arg2->expr_type == EXPR_CONSTANT)
5174 : 38 : d = mpz_get_si (arg2->value.integer) - 1;
5175 : : else
5176 : : return false;
5177 : :
5178 : 38 : if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
5179 : : {
5180 : 23 : if (sym->as->lower[d])
5181 : 23 : new_expr = gfc_copy_expr (sym->as->lower[d]);
5182 : : }
5183 : : else
5184 : : {
5185 : 15 : if (sym->as->upper[d])
5186 : 9 : new_expr = gfc_copy_expr (sym->as->upper[d]);
5187 : : }
5188 : : break;
5189 : :
5190 : : default:
5191 : : break;
5192 : : }
5193 : :
5194 : 1307 : gfc_apply_interface_mapping_to_expr (mapping, new_expr);
5195 : 1307 : if (!new_expr)
5196 : : return false;
5197 : :
5198 : 113 : gfc_replace_expr (expr, new_expr);
5199 : 113 : return true;
5200 : : }
5201 : :
5202 : :
5203 : : static void
5204 : 24 : gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
5205 : : gfc_interface_mapping * mapping)
5206 : : {
5207 : 24 : gfc_formal_arglist *f;
5208 : 24 : gfc_actual_arglist *actual;
5209 : :
5210 : 24 : actual = expr->value.function.actual;
5211 : 24 : f = gfc_sym_get_dummy_args (map_expr->symtree->n.sym);
5212 : :
5213 : 72 : for (; f && actual; f = f->next, actual = actual->next)
5214 : : {
5215 : 24 : if (!actual->expr)
5216 : 0 : continue;
5217 : :
5218 : 24 : gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
5219 : : }
5220 : :
5221 : 24 : if (map_expr->symtree->n.sym->attr.dimension)
5222 : : {
5223 : 6 : int d;
5224 : 6 : gfc_array_spec *as;
5225 : :
5226 : 6 : as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
5227 : :
5228 : 18 : for (d = 0; d < as->rank; d++)
5229 : : {
5230 : 6 : gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
5231 : 6 : gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
5232 : : }
5233 : :
5234 : 6 : expr->value.function.esym->as = as;
5235 : : }
5236 : :
5237 : 24 : if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
5238 : : {
5239 : 0 : expr->value.function.esym->ts.u.cl->length
5240 : 0 : = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
5241 : :
5242 : 0 : gfc_apply_interface_mapping_to_expr (mapping,
5243 : 0 : expr->value.function.esym->ts.u.cl->length);
5244 : : }
5245 : 24 : }
5246 : :
5247 : :
5248 : : /* EXPR is a copy of an expression that appeared in the interface
5249 : : associated with MAPPING. Walk it recursively looking for references to
5250 : : dummy arguments that MAPPING maps to actual arguments. Replace each such
5251 : : reference with a reference to the associated actual argument. */
5252 : :
5253 : : static void
5254 : 20870 : gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
5255 : : gfc_expr * expr)
5256 : : {
5257 : 22423 : gfc_interface_sym_mapping *sym;
5258 : 22423 : gfc_actual_arglist *actual;
5259 : :
5260 : 22423 : if (!expr)
5261 : : return;
5262 : :
5263 : : /* Copying an expression does not copy its length, so do that here. */
5264 : 12445 : if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
5265 : : {
5266 : 1784 : expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
5267 : 1784 : gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
5268 : : }
5269 : :
5270 : : /* Apply the mapping to any references. */
5271 : 12445 : gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
5272 : :
5273 : : /* ...and to the expression's symbol, if it has one. */
5274 : : /* TODO Find out why the condition on expr->symtree had to be moved into
5275 : : the loop rather than being outside it, as originally. */
5276 : 29650 : for (sym = mapping->syms; sym; sym = sym->next)
5277 : 17205 : if (expr->symtree && !strcmp (sym->old->name, expr->symtree->n.sym->name))
5278 : : {
5279 : 3345 : if (sym->new_sym->n.sym->backend_decl)
5280 : 3301 : expr->symtree = sym->new_sym;
5281 : 44 : else if (sym->expr)
5282 : 44 : gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
5283 : : }
5284 : :
5285 : : /* ...and to subexpressions in expr->value. */
5286 : 12445 : switch (expr->expr_type)
5287 : : {
5288 : : case EXPR_VARIABLE:
5289 : : case EXPR_CONSTANT:
5290 : : case EXPR_NULL:
5291 : : case EXPR_SUBSTRING:
5292 : : break;
5293 : :
5294 : 1553 : case EXPR_OP:
5295 : 1553 : gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
5296 : 1553 : gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
5297 : 1553 : break;
5298 : :
5299 : 2927 : case EXPR_FUNCTION:
5300 : 9388 : for (actual = expr->value.function.actual; actual; actual = actual->next)
5301 : 6461 : gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
5302 : :
5303 : 2927 : if (expr->value.function.esym == NULL
5304 : 2614 : && expr->value.function.isym != NULL
5305 : 2602 : && expr->value.function.actual
5306 : 2601 : && expr->value.function.actual->expr
5307 : 2601 : && expr->value.function.actual->expr->symtree
5308 : 5111 : && gfc_map_intrinsic_function (expr, mapping))
5309 : : break;
5310 : :
5311 : 6094 : for (sym = mapping->syms; sym; sym = sym->next)
5312 : 3508 : if (sym->old == expr->value.function.esym)
5313 : : {
5314 : 24 : expr->value.function.esym = sym->new_sym->n.sym;
5315 : 24 : gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
5316 : 24 : expr->value.function.esym->result = sym->new_sym->n.sym;
5317 : : }
5318 : : break;
5319 : :
5320 : 47 : case EXPR_ARRAY:
5321 : 47 : case EXPR_STRUCTURE:
5322 : 47 : gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
5323 : 47 : break;
5324 : :
5325 : 0 : case EXPR_COMPCALL:
5326 : 0 : case EXPR_PPC:
5327 : 0 : case EXPR_UNKNOWN:
5328 : 0 : gcc_unreachable ();
5329 : : break;
5330 : : }
5331 : :
5332 : : return;
5333 : : }
5334 : :
5335 : :
5336 : : /* Evaluate interface expression EXPR using MAPPING. Store the result
5337 : : in SE. */
5338 : :
5339 : : void
5340 : 3930 : gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
5341 : : gfc_se * se, gfc_expr * expr)
5342 : : {
5343 : 3930 : expr = gfc_copy_expr (expr);
5344 : 3930 : gfc_apply_interface_mapping_to_expr (mapping, expr);
5345 : 3930 : gfc_conv_expr (se, expr);
5346 : 3930 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
5347 : 3930 : gfc_free_expr (expr);
5348 : 3930 : }
5349 : :
5350 : :
5351 : : /* Returns a reference to a temporary array into which a component of
5352 : : an actual argument derived type array is copied and then returned
5353 : : after the function call. */
5354 : : void
5355 : 2380 : gfc_conv_subref_array_arg (gfc_se *se, gfc_expr * expr, int g77,
5356 : : sym_intent intent, bool formal_ptr,
5357 : : const gfc_symbol *fsym, const char *proc_name,
5358 : : gfc_symbol *sym, bool check_contiguous)
5359 : : {
5360 : 2380 : gfc_se lse;
5361 : 2380 : gfc_se rse;
5362 : 2380 : gfc_ss *lss;
5363 : 2380 : gfc_ss *rss;
5364 : 2380 : gfc_loopinfo loop;
5365 : 2380 : gfc_loopinfo loop2;
5366 : 2380 : gfc_array_info *info;
5367 : 2380 : tree offset;
5368 : 2380 : tree tmp_index;
5369 : 2380 : tree tmp;
5370 : 2380 : tree base_type;
5371 : 2380 : tree size;
5372 : 2380 : stmtblock_t body;
5373 : 2380 : int n;
5374 : 2380 : int dimen;
5375 : 2380 : gfc_se work_se;
5376 : 2380 : gfc_se *parmse;
5377 : 2380 : bool pass_optional;
5378 : 2380 : bool readonly;
5379 : :
5380 : 2380 : pass_optional = fsym && fsym->attr.optional && sym && sym->attr.optional;
5381 : :
5382 : 2369 : if (pass_optional || check_contiguous)
5383 : : {
5384 : 1349 : gfc_init_se (&work_se, NULL);
5385 : 1349 : parmse = &work_se;
5386 : : }
5387 : : else
5388 : : parmse = se;
5389 : :
5390 : 2380 : if (gfc_option.rtcheck & GFC_RTCHECK_ARRAY_TEMPS)
5391 : : {
5392 : : /* We will create a temporary array, so let us warn. */
5393 : 868 : char * msg;
5394 : :
5395 : 868 : if (fsym && proc_name)
5396 : 868 : msg = xasprintf ("An array temporary was created for argument "
5397 : 868 : "'%s' of procedure '%s'", fsym->name, proc_name);
5398 : : else
5399 : 0 : msg = xasprintf ("An array temporary was created");
5400 : :
5401 : 868 : tmp = build_int_cst (logical_type_node, 1);
5402 : 868 : gfc_trans_runtime_check (false, true, tmp, &parmse->pre,
5403 : : &expr->where, msg);
5404 : 868 : free (msg);
5405 : : }
5406 : :
5407 : 2380 : gfc_init_se (&lse, NULL);
5408 : 2380 : gfc_init_se (&rse, NULL);
5409 : :
5410 : : /* Walk the argument expression. */
5411 : 2380 : rss = gfc_walk_expr (expr);
5412 : :
5413 : 2380 : gcc_assert (rss != gfc_ss_terminator);
5414 : :
5415 : : /* Initialize the scalarizer. */
5416 : 2380 : gfc_init_loopinfo (&loop);
5417 : 2380 : gfc_add_ss_to_loop (&loop, rss);
5418 : :
5419 : : /* Calculate the bounds of the scalarization. */
5420 : 2380 : gfc_conv_ss_startstride (&loop);
5421 : :
5422 : : /* Build an ss for the temporary. */
5423 : 2380 : if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
5424 : 136 : gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
5425 : :
5426 : 2380 : base_type = gfc_typenode_for_spec (&expr->ts);
5427 : 2380 : if (GFC_ARRAY_TYPE_P (base_type)
5428 : 2380 : || GFC_DESCRIPTOR_TYPE_P (base_type))
5429 : 0 : base_type = gfc_get_element_type (base_type);
5430 : :
5431 : 2380 : if (expr->ts.type == BT_CLASS)
5432 : 121 : base_type = gfc_typenode_for_spec (&CLASS_DATA (expr)->ts);
5433 : :
5434 : 3538 : loop.temp_ss = gfc_get_temp_ss (base_type, ((expr->ts.type == BT_CHARACTER)
5435 : 1158 : ? expr->ts.u.cl->backend_decl
5436 : : : NULL),
5437 : : loop.dimen);
5438 : :
5439 : 2380 : parmse->string_length = loop.temp_ss->info->string_length;
5440 : :
5441 : : /* Associate the SS with the loop. */
5442 : 2380 : gfc_add_ss_to_loop (&loop, loop.temp_ss);
5443 : :
5444 : : /* Setup the scalarizing loops. */
5445 : 2380 : gfc_conv_loop_setup (&loop, &expr->where);
5446 : :
5447 : : /* Pass the temporary descriptor back to the caller. */
5448 : 2380 : info = &loop.temp_ss->info->data.array;
5449 : 2380 : parmse->expr = info->descriptor;
5450 : :
5451 : : /* Setup the gfc_se structures. */
5452 : 2380 : gfc_copy_loopinfo_to_se (&lse, &loop);
5453 : 2380 : gfc_copy_loopinfo_to_se (&rse, &loop);
5454 : :
5455 : 2380 : rse.ss = rss;
5456 : 2380 : lse.ss = loop.temp_ss;
5457 : 2380 : gfc_mark_ss_chain_used (rss, 1);
5458 : 2380 : gfc_mark_ss_chain_used (loop.temp_ss, 1);
5459 : :
5460 : : /* Start the scalarized loop body. */
5461 : 2380 : gfc_start_scalarized_body (&loop, &body);
5462 : :
5463 : : /* Translate the expression. */
5464 : 2380 : gfc_conv_expr (&rse, expr);
5465 : :
5466 : : /* Reset the offset for the function call since the loop
5467 : : is zero based on the data pointer. Note that the temp
5468 : : comes first in the loop chain since it is added second. */
5469 : 2380 : if (gfc_is_class_array_function (expr))
5470 : : {
5471 : 13 : tmp = loop.ss->loop_chain->info->data.array.descriptor;
5472 : 13 : gfc_conv_descriptor_offset_set (&loop.pre, tmp,
5473 : : gfc_index_zero_node);
5474 : : }
5475 : :
5476 : 2380 : gfc_conv_tmp_array_ref (&lse);
5477 : :
5478 : 2380 : if (intent != INTENT_OUT)
5479 : : {
5480 : 2342 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false);
5481 : 2342 : gfc_add_expr_to_block (&body, tmp);
5482 : 2342 : gcc_assert (rse.ss == gfc_ss_terminator);
5483 : 2342 : gfc_trans_scalarizing_loops (&loop, &body);
5484 : : }
5485 : : else
5486 : : {
5487 : : /* Make sure that the temporary declaration survives by merging
5488 : : all the loop declarations into the current context. */
5489 : 85 : for (n = 0; n < loop.dimen; n++)
5490 : : {
5491 : 47 : gfc_merge_block_scope (&body);
5492 : 47 : body = loop.code[loop.order[n]];
5493 : : }
5494 : 38 : gfc_merge_block_scope (&body);
5495 : : }
5496 : :
5497 : : /* Add the post block after the second loop, so that any
5498 : : freeing of allocated memory is done at the right time. */
5499 : 2380 : gfc_add_block_to_block (&parmse->pre, &loop.pre);
5500 : :
5501 : : /**********Copy the temporary back again.*********/
5502 : :
5503 : 2380 : gfc_init_se (&lse, NULL);
5504 : 2380 : gfc_init_se (&rse, NULL);
5505 : :
5506 : : /* Walk the argument expression. */
5507 : 2380 : lss = gfc_walk_expr (expr);
5508 : 2380 : rse.ss = loop.temp_ss;
5509 : 2380 : lse.ss = lss;
5510 : :
5511 : : /* Initialize the scalarizer. */
5512 : 2380 : gfc_init_loopinfo (&loop2);
5513 : 2380 : gfc_add_ss_to_loop (&loop2, lss);
5514 : :
5515 : 2380 : dimen = rse.ss->dimen;
5516 : :
5517 : : /* Skip the write-out loop for this case. */
5518 : 2380 : if (gfc_is_class_array_function (expr))
5519 : 13 : goto class_array_fcn;
5520 : :
5521 : : /* Calculate the bounds of the scalarization. */
5522 : 2367 : gfc_conv_ss_startstride (&loop2);
5523 : :
5524 : : /* Setup the scalarizing loops. */
5525 : 2367 : gfc_conv_loop_setup (&loop2, &expr->where);
5526 : :
5527 : 2367 : gfc_copy_loopinfo_to_se (&lse, &loop2);
5528 : 2367 : gfc_copy_loopinfo_to_se (&rse, &loop2);
5529 : :
5530 : 2367 : gfc_mark_ss_chain_used (lss, 1);
5531 : 2367 : gfc_mark_ss_chain_used (loop.temp_ss, 1);
5532 : :
5533 : : /* Declare the variable to hold the temporary offset and start the
5534 : : scalarized loop body. */
5535 : 2367 : offset = gfc_create_var (gfc_array_index_type, NULL);
5536 : 2367 : gfc_start_scalarized_body (&loop2, &body);
5537 : :
5538 : : /* Build the offsets for the temporary from the loop variables. The
5539 : : temporary array has lbounds of zero and strides of one in all
5540 : : dimensions, so this is very simple. The offset is only computed
5541 : : outside the innermost loop, so the overall transfer could be
5542 : : optimized further. */
5543 : 2367 : info = &rse.ss->info->data.array;
5544 : :
5545 : 2367 : tmp_index = gfc_index_zero_node;
5546 : 3711 : for (n = dimen - 1; n > 0; n--)
5547 : : {
5548 : 1344 : tree tmp_str;
5549 : 1344 : tmp = rse.loop->loopvar[n];
5550 : 1344 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5551 : : tmp, rse.loop->from[n]);
5552 : 1344 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5553 : : tmp, tmp_index);
5554 : :
5555 : 2688 : tmp_str = fold_build2_loc (input_location, MINUS_EXPR,
5556 : : gfc_array_index_type,
5557 : 1344 : rse.loop->to[n-1], rse.loop->from[n-1]);
5558 : 1344 : tmp_str = fold_build2_loc (input_location, PLUS_EXPR,
5559 : : gfc_array_index_type,
5560 : : tmp_str, gfc_index_one_node);
5561 : :
5562 : 1344 : tmp_index = fold_build2_loc (input_location, MULT_EXPR,
5563 : : gfc_array_index_type, tmp, tmp_str);
5564 : : }
5565 : :
5566 : 4734 : tmp_index = fold_build2_loc (input_location, MINUS_EXPR,
5567 : : gfc_array_index_type,
5568 : 2367 : tmp_index, rse.loop->from[0]);
5569 : 2367 : gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
5570 : :
5571 : 4734 : tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
5572 : : gfc_array_index_type,
5573 : 2367 : rse.loop->loopvar[0], offset);
5574 : :
5575 : : /* Now use the offset for the reference. */
5576 : 2367 : tmp = build_fold_indirect_ref_loc (input_location,
5577 : : info->data);
5578 : 2367 : rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
5579 : :
5580 : 2367 : if (expr->ts.type == BT_CHARACTER)
5581 : 1158 : rse.string_length = expr->ts.u.cl->backend_decl;
5582 : :
5583 : 2367 : gfc_conv_expr (&lse, expr);
5584 : :
5585 : 2367 : gcc_assert (lse.ss == gfc_ss_terminator);
5586 : :
5587 : 2367 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, true);
5588 : 2367 : gfc_add_expr_to_block (&body, tmp);
5589 : :
5590 : : /* Generate the copying loops. */
5591 : 2367 : gfc_trans_scalarizing_loops (&loop2, &body);
5592 : :
5593 : : /* Wrap the whole thing up by adding the second loop to the post-block
5594 : : and following it by the post-block of the first loop. In this way,
5595 : : if the temporary needs freeing, it is done after use!
5596 : : If input expr is read-only, e.g. a PARAMETER array, copying back
5597 : : modified values is undefined behavior. */
5598 : 4734 : readonly = (expr->expr_type == EXPR_VARIABLE
5599 : 2313 : && expr->symtree
5600 : 4680 : && expr->symtree->n.sym->attr.flavor == FL_PARAMETER);
5601 : :
5602 : 2367 : if ((intent != INTENT_IN) && !readonly)
5603 : : {
5604 : 1138 : gfc_add_block_to_block (&parmse->post, &loop2.pre);
5605 : 1138 : gfc_add_block_to_block (&parmse->post, &loop2.post);
5606 : : }
5607 : :
5608 : 1229 : class_array_fcn:
5609 : :
5610 : 2380 : gfc_add_block_to_block (&parmse->post, &loop.post);
5611 : :
5612 : 2380 : gfc_cleanup_loop (&loop);
5613 : 2380 : gfc_cleanup_loop (&loop2);
5614 : :
5615 : : /* Pass the string length to the argument expression. */
5616 : 2380 : if (expr->ts.type == BT_CHARACTER)
5617 : 1158 : parmse->string_length = expr->ts.u.cl->backend_decl;
5618 : :
5619 : : /* Determine the offset for pointer formal arguments and set the
5620 : : lbounds to one. */
5621 : 2380 : if (formal_ptr)
5622 : : {
5623 : 0 : size = gfc_index_one_node;
5624 : 0 : offset = gfc_index_zero_node;
5625 : 0 : for (n = 0; n < dimen; n++)
5626 : : {
5627 : 0 : tmp = gfc_conv_descriptor_ubound_get (parmse->expr,
5628 : : gfc_rank_cst[n]);
5629 : 0 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
5630 : : gfc_array_index_type, tmp,
5631 : : gfc_index_one_node);
5632 : 0 : gfc_conv_descriptor_ubound_set (&parmse->pre,
5633 : : parmse->expr,
5634 : : gfc_rank_cst[n],
5635 : : tmp);
5636 : 0 : gfc_conv_descriptor_lbound_set (&parmse->pre,
5637 : : parmse->expr,
5638 : : gfc_rank_cst[n],
5639 : : gfc_index_one_node);
5640 : 0 : size = gfc_evaluate_now (size, &parmse->pre);
5641 : 0 : offset = fold_build2_loc (input_location, MINUS_EXPR,
5642 : : gfc_array_index_type,
5643 : : offset, size);
5644 : 0 : offset = gfc_evaluate_now (offset, &parmse->pre);
5645 : 0 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
5646 : : gfc_array_index_type,
5647 : 0 : rse.loop->to[n], rse.loop->from[n]);
5648 : 0 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
5649 : : gfc_array_index_type,
5650 : : tmp, gfc_index_one_node);
5651 : 0 : size = fold_build2_loc (input_location, MULT_EXPR,
5652 : : gfc_array_index_type, size, tmp);
5653 : : }
5654 : :
5655 : 0 : gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
5656 : : offset);
5657 : : }
5658 : :
5659 : : /* We want either the address for the data or the address of the descriptor,
5660 : : depending on the mode of passing array arguments. */
5661 : 2380 : if (g77)
5662 : 433 : parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
5663 : : else
5664 : 1947 : parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
5665 : :
5666 : : /* Basically make this into
5667 : :
5668 : : if (present)
5669 : : {
5670 : : if (contiguous)
5671 : : {
5672 : : pointer = a;
5673 : : }
5674 : : else
5675 : : {
5676 : : parmse->pre();
5677 : : pointer = parmse->expr;
5678 : : }
5679 : : }
5680 : : else
5681 : : pointer = NULL;
5682 : :
5683 : : foo (pointer);
5684 : : if (present && !contiguous)
5685 : : se->post();
5686 : :
5687 : : */
5688 : :
5689 : 2380 : if (pass_optional || check_contiguous)
5690 : : {
5691 : 1349 : tree type;
5692 : 1349 : stmtblock_t else_block;
5693 : 1349 : tree pre_stmts, post_stmts;
5694 : 1349 : tree pointer;
5695 : 1349 : tree else_stmt;
5696 : 1349 : tree present_var = NULL_TREE;
5697 : 1349 : tree cont_var = NULL_TREE;
5698 : 1349 : tree post_cond;
5699 : :
5700 : 1349 : type = TREE_TYPE (parmse->expr);
5701 : 1349 : if (POINTER_TYPE_P (type) && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
5702 : 1021 : type = TREE_TYPE (type);
5703 : 1349 : pointer = gfc_create_var (type, "arg_ptr");
5704 : :
5705 : 1349 : if (check_contiguous)
5706 : : {
5707 : 1349 : gfc_se cont_se, array_se;
5708 : 1349 : stmtblock_t if_block, else_block;
5709 : 1349 : tree if_stmt, else_stmt;
5710 : 1349 : mpz_t size;
5711 : 1349 : bool size_set;
5712 : :
5713 : 1349 : cont_var = gfc_create_var (boolean_type_node, "contiguous");
5714 : :
5715 : : /* If the size is known to be one at compile-time, set
5716 : : cont_var to true unconditionally. This may look
5717 : : inelegant, but we're only doing this during
5718 : : optimization, so the statements will be optimized away,
5719 : : and this saves complexity here. */
5720 : :
5721 : 1349 : size_set = gfc_array_size (expr, &size);
5722 : 1349 : if (size_set && mpz_cmp_ui (size, 1) == 0)
5723 : : {
5724 : 6 : gfc_add_modify (&se->pre, cont_var,
5725 : : build_one_cst (boolean_type_node));
5726 : : }
5727 : : else
5728 : : {
5729 : : /* cont_var = is_contiguous (expr); . */
5730 : 1343 : gfc_init_se (&cont_se, parmse);
5731 : 1343 : gfc_conv_is_contiguous_expr (&cont_se, expr);
5732 : 1343 : gfc_add_block_to_block (&se->pre, &(&cont_se)->pre);
5733 : 1343 : gfc_add_modify (&se->pre, cont_var, cont_se.expr);
5734 : 1343 : gfc_add_block_to_block (&se->pre, &(&cont_se)->post);
5735 : : }
5736 : :
5737 : 1349 : if (size_set)
5738 : 1145 : mpz_clear (size);
5739 : :
5740 : : /* arrayse->expr = descriptor of a. */
5741 : 1349 : gfc_init_se (&array_se, se);
5742 : 1349 : gfc_conv_expr_descriptor (&array_se, expr);
5743 : 1349 : gfc_add_block_to_block (&se->pre, &(&array_se)->pre);
5744 : 1349 : gfc_add_block_to_block (&se->pre, &(&array_se)->post);
5745 : :
5746 : : /* if_stmt = { descriptor ? pointer = a : pointer = &a[0]; } . */
5747 : 1349 : gfc_init_block (&if_block);
5748 : 1349 : if (GFC_DESCRIPTOR_TYPE_P (type))
5749 : 1021 : gfc_add_modify (&if_block, pointer, array_se.expr);
5750 : : else
5751 : : {
5752 : 328 : tmp = gfc_conv_array_data (array_se.expr);
5753 : 328 : tmp = fold_convert (type, tmp);
5754 : 328 : gfc_add_modify (&if_block, pointer, tmp);
5755 : : }
5756 : 1349 : if_stmt = gfc_finish_block (&if_block);
5757 : :
5758 : : /* else_stmt = { parmse->pre(); pointer = parmse->expr; } . */
5759 : 1349 : gfc_init_block (&else_block);
5760 : 1349 : gfc_add_block_to_block (&else_block, &parmse->pre);
5761 : 1677 : tmp = (GFC_DESCRIPTOR_TYPE_P (type)
5762 : 1349 : ? build_fold_indirect_ref_loc (input_location, parmse->expr)
5763 : : : parmse->expr);
5764 : 1349 : gfc_add_modify (&else_block, pointer, tmp);
5765 : 1349 : else_stmt = gfc_finish_block (&else_block);
5766 : :
5767 : : /* And put the above into an if statement. */
5768 : 1349 : pre_stmts = fold_build3_loc (input_location, COND_EXPR, void_type_node,
5769 : : gfc_likely (cont_var,
5770 : : PRED_FORTRAN_CONTIGUOUS),
5771 : : if_stmt, else_stmt);
5772 : : }
5773 : : else
5774 : : {
5775 : : /* pointer = pramse->expr; . */
5776 : 0 : gfc_add_modify (&parmse->pre, pointer, parmse->expr);
5777 : 0 : pre_stmts = gfc_finish_block (&parmse->pre);
5778 : : }
5779 : :
5780 : 1349 : if (pass_optional)
5781 : : {
5782 : 11 : present_var = gfc_create_var (boolean_type_node, "present");
5783 : :
5784 : : /* present_var = present(sym); . */
5785 : 11 : tmp = gfc_conv_expr_present (sym);
5786 : 11 : tmp = fold_convert (boolean_type_node, tmp);
5787 : 11 : gfc_add_modify (&se->pre, present_var, tmp);
5788 : :
5789 : : /* else_stmt = { pointer = NULL; } . */
5790 : 11 : gfc_init_block (&else_block);
5791 : 11 : if (GFC_DESCRIPTOR_TYPE_P (type))
5792 : 0 : gfc_conv_descriptor_data_set (&else_block, pointer,
5793 : : null_pointer_node);
5794 : : else
5795 : 11 : gfc_add_modify (&else_block, pointer, build_int_cst (type, 0));
5796 : 11 : else_stmt = gfc_finish_block (&else_block);
5797 : :
5798 : 11 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
5799 : : gfc_likely (present_var,
5800 : : PRED_FORTRAN_ABSENT_DUMMY),
5801 : : pre_stmts, else_stmt);
5802 : 11 : gfc_add_expr_to_block (&se->pre, tmp);
5803 : : }
5804 : : else
5805 : 1338 : gfc_add_expr_to_block (&se->pre, pre_stmts);
5806 : :
5807 : 1349 : post_stmts = gfc_finish_block (&parmse->post);
5808 : :
5809 : : /* Put together the post stuff, plus the optional
5810 : : deallocation. */
5811 : 1349 : if (check_contiguous)
5812 : : {
5813 : : /* !cont_var. */
5814 : 1349 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
5815 : : cont_var,
5816 : : build_zero_cst (boolean_type_node));
5817 : 1349 : tmp = gfc_unlikely (tmp, PRED_FORTRAN_CONTIGUOUS);
5818 : :
5819 : 1349 : if (pass_optional)
5820 : : {
5821 : 11 : tree present_likely = gfc_likely (present_var,
5822 : : PRED_FORTRAN_ABSENT_DUMMY);
5823 : 11 : post_cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
5824 : : boolean_type_node, present_likely,
5825 : : tmp);
5826 : : }
5827 : : else
5828 : : post_cond = tmp;
5829 : : }
5830 : : else
5831 : : {
5832 : 0 : gcc_assert (pass_optional);
5833 : : post_cond = present_var;
5834 : : }
5835 : :
5836 : 1349 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, post_cond,
5837 : : post_stmts, build_empty_stmt (input_location));
5838 : 1349 : gfc_add_expr_to_block (&se->post, tmp);
5839 : 1349 : if (GFC_DESCRIPTOR_TYPE_P (type))
5840 : : {
5841 : 1021 : type = TREE_TYPE (parmse->expr);
5842 : 1021 : if (POINTER_TYPE_P (type))
5843 : : {
5844 : 1021 : pointer = gfc_build_addr_expr (type, pointer);
5845 : 1021 : if (pass_optional)
5846 : : {
5847 : 0 : tmp = gfc_likely (present_var, PRED_FORTRAN_ABSENT_DUMMY);
5848 : 0 : pointer = fold_build3_loc (input_location, COND_EXPR, type,
5849 : : tmp, pointer,
5850 : : fold_convert (type,
5851 : : null_pointer_node));
5852 : : }
5853 : : }
5854 : : else
5855 : 0 : gcc_assert (!pass_optional);
5856 : : }
5857 : 1349 : se->expr = pointer;
5858 : : }
5859 : :
5860 : 2380 : return;
5861 : : }
5862 : :
5863 : :
5864 : : /* Generate the code for argument list functions. */
5865 : :
5866 : : static void
5867 : 5822 : conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
5868 : : {
5869 : : /* Pass by value for g77 %VAL(arg), pass the address
5870 : : indirectly for %LOC, else by reference. Thus %REF
5871 : : is a "do-nothing" and %LOC is the same as an F95
5872 : : pointer. */
5873 : 5822 : if (strcmp (name, "%VAL") == 0)
5874 : 5810 : gfc_conv_expr (se, expr);
5875 : 12 : else if (strcmp (name, "%LOC") == 0)
5876 : : {
5877 : 6 : gfc_conv_expr_reference (se, expr);
5878 : 6 : se->expr = gfc_build_addr_expr (NULL, se->expr);
5879 : : }
5880 : 6 : else if (strcmp (name, "%REF") == 0)
5881 : 6 : gfc_conv_expr_reference (se, expr);
5882 : : else
5883 : 0 : gfc_error ("Unknown argument list function at %L", &expr->where);
5884 : 5822 : }
5885 : :
5886 : :
5887 : : /* This function tells whether the middle-end representation of the expression
5888 : : E given as input may point to data otherwise accessible through a variable
5889 : : (sub-)reference.
5890 : : It is assumed that the only expressions that may alias are variables,
5891 : : and array constructors if ARRAY_MAY_ALIAS is true and some of its elements
5892 : : may alias.
5893 : : This function is used to decide whether freeing an expression's allocatable
5894 : : components is safe or should be avoided.
5895 : :
5896 : : If ARRAY_MAY_ALIAS is true, an array constructor may alias if some of
5897 : : its elements are copied from a variable. This ARRAY_MAY_ALIAS trick
5898 : : is necessary because for array constructors, aliasing depends on how
5899 : : the array is used:
5900 : : - If E is an array constructor used as argument to an elemental procedure,
5901 : : the array, which is generated through shallow copy by the scalarizer,
5902 : : is used directly and can alias the expressions it was copied from.
5903 : : - If E is an array constructor used as argument to a non-elemental
5904 : : procedure,the scalarizer is used in gfc_conv_expr_descriptor to generate
5905 : : the array as in the previous case, but then that array is used
5906 : : to initialize a new descriptor through deep copy. There is no alias
5907 : : possible in that case.
5908 : : Thus, the ARRAY_MAY_ALIAS flag is necessary to distinguish the two cases
5909 : : above. */
5910 : :
5911 : : static bool
5912 : 7350 : expr_may_alias_variables (gfc_expr *e, bool array_may_alias)
5913 : : {
5914 : 7350 : gfc_constructor *c;
5915 : :
5916 : 7350 : if (e->expr_type == EXPR_VARIABLE)
5917 : : return true;
5918 : 398 : else if (e->expr_type == EXPR_FUNCTION)
5919 : : {
5920 : 160 : gfc_symbol *proc_ifc = gfc_get_proc_ifc_for_expr (e);
5921 : :
5922 : 160 : if (proc_ifc->result != NULL
5923 : 160 : && ((proc_ifc->result->ts.type == BT_CLASS
5924 : 25 : && proc_ifc->result->ts.u.derived->attr.is_class
5925 : 25 : && CLASS_DATA (proc_ifc->result)->attr.class_pointer)
5926 : 160 : || proc_ifc->result->attr.pointer))
5927 : : return true;
5928 : : else
5929 : : return false;
5930 : : }
5931 : 238 : else if (e->expr_type != EXPR_ARRAY || !array_may_alias)
5932 : : return false;
5933 : :
5934 : 67 : for (c = gfc_constructor_first (e->value.constructor);
5935 : 101 : c; c = gfc_constructor_next (c))
5936 : 69 : if (c->expr
5937 : 69 : && expr_may_alias_variables (c->expr, array_may_alias))
5938 : : return true;
5939 : :
5940 : : return false;
5941 : : }
5942 : :
5943 : :
5944 : : /* A helper function to set the dtype for unallocated or unassociated
5945 : : entities. */
5946 : :
5947 : : static void
5948 : 891 : set_dtype_for_unallocated (gfc_se *parmse, gfc_expr *e)
5949 : : {
5950 : 891 : tree tmp;
5951 : 891 : tree desc;
5952 : 891 : tree cond;
5953 : 891 : tree type;
5954 : 891 : stmtblock_t block;
5955 : :
5956 : : /* TODO Figure out how to handle optional dummies. */
5957 : 891 : if (e && e->expr_type == EXPR_VARIABLE
5958 : 807 : && e->symtree->n.sym->attr.optional)
5959 : 108 : return;
5960 : :
5961 : 819 : desc = parmse->expr;
5962 : 819 : if (desc == NULL_TREE)
5963 : : return;
5964 : :
5965 : 819 : if (POINTER_TYPE_P (TREE_TYPE (desc)))
5966 : 819 : desc = build_fold_indirect_ref_loc (input_location, desc);
5967 : 819 : if (GFC_CLASS_TYPE_P (TREE_TYPE (desc)))
5968 : 192 : desc = gfc_class_data_get (desc);
5969 : 819 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
5970 : : return;
5971 : :
5972 : 783 : gfc_init_block (&block);
5973 : 783 : tmp = gfc_conv_descriptor_data_get (desc);
5974 : 783 : cond = fold_build2_loc (input_location, EQ_EXPR,
5975 : : logical_type_node, tmp,
5976 : 783 : build_int_cst (TREE_TYPE (tmp), 0));
5977 : 783 : tmp = gfc_conv_descriptor_dtype (desc);
5978 : 783 : type = gfc_get_element_type (TREE_TYPE (desc));
5979 : 1566 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
5980 : 783 : TREE_TYPE (tmp), tmp,
5981 : : gfc_get_dtype_rank_type (e->rank, type));
5982 : 783 : gfc_add_expr_to_block (&block, tmp);
5983 : 783 : cond = build3_v (COND_EXPR, cond,
5984 : : gfc_finish_block (&block),
5985 : : build_empty_stmt (input_location));
5986 : 783 : gfc_add_expr_to_block (&parmse->pre, cond);
5987 : : }
5988 : :
5989 : :
5990 : :
5991 : : /* Provide an interface between gfortran array descriptors and the F2018:18.4
5992 : : ISO_Fortran_binding array descriptors. */
5993 : :
5994 : : static void
5995 : 6536 : gfc_conv_gfc_desc_to_cfi_desc (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym)
5996 : : {
5997 : 6536 : stmtblock_t block, block2;
5998 : 6536 : tree cfi, gfc, tmp, tmp2;
5999 : 6536 : tree present = NULL;
6000 : 6536 : tree gfc_strlen = NULL;
6001 : 6536 : tree rank;
6002 : 6536 : gfc_se se;
6003 : :
6004 : 6536 : if (fsym->attr.optional
6005 : 1094 : && e->expr_type == EXPR_VARIABLE
6006 : 1094 : && e->symtree->n.sym->attr.optional)
6007 : 103 : present = gfc_conv_expr_present (e->symtree->n.sym);
6008 : :
6009 : 6536 : gfc_init_block (&block);
6010 : :
6011 : : /* Convert original argument to a tree. */
6012 : 6536 : gfc_init_se (&se, NULL);
6013 : 6536 : if (e->rank == 0)
6014 : : {
6015 : 686 : se.want_pointer = 1;
6016 : 686 : gfc_conv_expr (&se, e);
6017 : 686 : gfc = se.expr;
6018 : : /* gfc_conv_constant ignores se.want_poiner, e.g. for string_cst. */
6019 : 686 : if (!POINTER_TYPE_P (TREE_TYPE (gfc)))
6020 : 20 : gfc = gfc_build_addr_expr (NULL, gfc);
6021 : : }
6022 : : else
6023 : : {
6024 : : /* If the actual argument can be noncontiguous, copy-in/out is required,
6025 : : if the dummy has either the CONTIGUOUS attribute or is an assumed-
6026 : : length assumed-length/assumed-size CHARACTER array. This only
6027 : : applies if the actual argument is a "variable"; if it's some
6028 : : non-lvalue expression, we are going to evaluate it to a
6029 : : temporary below anyway. */
6030 : 5850 : se.force_no_tmp = 1;
6031 : 5850 : if ((fsym->attr.contiguous
6032 : 4769 : || (fsym->ts.type == BT_CHARACTER && !fsym->ts.u.cl->length
6033 : 1375 : && (fsym->as->type == AS_ASSUMED_SIZE
6034 : 937 : || fsym->as->type == AS_EXPLICIT)))
6035 : 2023 : && !gfc_is_simply_contiguous (e, false, true)
6036 : 6877 : && gfc_expr_is_variable (e))
6037 : : {
6038 : 1021 : bool optional = fsym->attr.optional;
6039 : 1021 : fsym->attr.optional = 0;
6040 : 1021 : gfc_conv_subref_array_arg (&se, e, false, fsym->attr.intent,
6041 : 1021 : fsym->attr.pointer, fsym,
6042 : 1021 : fsym->ns->proc_name->name, NULL,
6043 : : /* check_contiguous= */ true);
6044 : 1021 : fsym->attr.optional = optional;
6045 : : }
6046 : : else
6047 : 4829 : gfc_conv_expr_descriptor (&se, e);
6048 : 5850 : gfc = se.expr;
6049 : : /* For dt(:)%var the elem_len*stride != sm, hence, GFC uses
6050 : : elem_len = sizeof(dt) and base_addr = dt(lb) instead.
6051 : : gfc_get_dataptr_offset fixes the base_addr; for elem_len, see below.
6052 : : While sm is fine as it uses span*stride and not elem_len. */
6053 : 5850 : if (POINTER_TYPE_P (TREE_TYPE (gfc)))
6054 : 1021 : gfc = build_fold_indirect_ref_loc (input_location, gfc);
6055 : 4829 : else if (is_subref_array (e) && e->ts.type != BT_CHARACTER)
6056 : 12 : gfc_get_dataptr_offset (&se.pre, gfc, gfc, NULL, true, e);
6057 : : }
6058 : 6536 : if (e->ts.type == BT_CHARACTER)
6059 : : {
6060 : 3409 : if (se.string_length)
6061 : : gfc_strlen = se.string_length;
6062 : 883 : else if (e->ts.u.cl->backend_decl)
6063 : : gfc_strlen = e->ts.u.cl->backend_decl;
6064 : : else
6065 : 0 : gcc_unreachable ();
6066 : : }
6067 : 6536 : gfc_add_block_to_block (&block, &se.pre);
6068 : :
6069 : : /* Create array descriptor and set version, rank, attribute, type. */
6070 : 12767 : cfi = gfc_create_var (gfc_get_cfi_type (e->rank < 0
6071 : : ? GFC_MAX_DIMENSIONS : e->rank,
6072 : : false), "cfi");
6073 : : /* Convert to CFI_cdesc_t, which has dim[] to avoid TBAA issues,*/
6074 : 6536 : if (fsym->attr.dimension && fsym->as->type == AS_ASSUMED_RANK)
6075 : : {
6076 : 2516 : tmp = gfc_get_cfi_type (-1, !fsym->attr.pointer && !fsym->attr.target);
6077 : 2338 : tmp = build_pointer_type (tmp);
6078 : 2338 : parmse->expr = cfi = gfc_build_addr_expr (tmp, cfi);
6079 : 2338 : cfi = build_fold_indirect_ref_loc (input_location, cfi);
6080 : : }
6081 : : else
6082 : 4198 : parmse->expr = gfc_build_addr_expr (NULL, cfi);
6083 : :
6084 : 6536 : tmp = gfc_get_cfi_desc_version (cfi);
6085 : 6536 : gfc_add_modify (&block, tmp,
6086 : 6536 : build_int_cst (TREE_TYPE (tmp), CFI_VERSION));
6087 : 6536 : if (e->rank < 0)
6088 : 305 : rank = fold_convert (signed_char_type_node, gfc_conv_descriptor_rank (gfc));
6089 : : else
6090 : 6231 : rank = build_int_cst (signed_char_type_node, e->rank);
6091 : 6536 : tmp = gfc_get_cfi_desc_rank (cfi);
6092 : 6536 : gfc_add_modify (&block, tmp, rank);
6093 : 6536 : int itype = CFI_type_other;
6094 : 6536 : if (e->ts.f90_type == BT_VOID)
6095 : 96 : itype = (e->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
6096 : 96 : ? CFI_type_cfunptr : CFI_type_cptr);
6097 : : else
6098 : : {
6099 : 6440 : if (e->expr_type == EXPR_NULL && e->ts.type == BT_UNKNOWN)
6100 : 1 : e->ts = fsym->ts;
6101 : 6440 : switch (e->ts.type)
6102 : : {
6103 : 2296 : case BT_INTEGER:
6104 : 2296 : case BT_LOGICAL:
6105 : 2296 : case BT_REAL:
6106 : 2296 : case BT_COMPLEX:
6107 : 2296 : itype = CFI_type_from_type_kind (e->ts.type, e->ts.kind);
6108 : 2296 : break;
6109 : 3410 : case BT_CHARACTER:
6110 : 3410 : itype = CFI_type_from_type_kind (CFI_type_Character, e->ts.kind);
6111 : 3410 : break;
6112 : : case BT_DERIVED:
6113 : 6536 : itype = CFI_type_struct;
6114 : : break;
6115 : 0 : case BT_VOID:
6116 : 0 : itype = (e->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
6117 : 0 : ? CFI_type_cfunptr : CFI_type_cptr);
6118 : : break;
6119 : : case BT_ASSUMED:
6120 : : itype = CFI_type_other; // FIXME: Or CFI_type_cptr ?
6121 : : break;
6122 : 1 : case BT_CLASS:
6123 : 1 : if (fsym->ts.type == BT_ASSUMED)
6124 : : {
6125 : : // F2017: 7.3.2.2: "An entity that is declared using the TYPE(*)
6126 : : // type specifier is assumed-type and is an unlimited polymorphic
6127 : : // entity." The actual argument _data component is passed.
6128 : : itype = CFI_type_other; // FIXME: Or CFI_type_cptr ?
6129 : : break;
6130 : : }
6131 : : else
6132 : 0 : gcc_unreachable ();
6133 : :
6134 : 0 : case BT_UNSIGNED:
6135 : 0 : gfc_internal_error ("Unsigned not yet implemented");
6136 : :
6137 : 0 : case BT_PROCEDURE:
6138 : 0 : case BT_HOLLERITH:
6139 : 0 : case BT_UNION:
6140 : 0 : case BT_BOZ:
6141 : 0 : case BT_UNKNOWN:
6142 : : // FIXME: Really unreachable? Or reachable for type(*) ? If so, CFI_type_other?
6143 : 0 : gcc_unreachable ();
6144 : : }
6145 : : }
6146 : :
6147 : 6536 : tmp = gfc_get_cfi_desc_type (cfi);
6148 : 6536 : gfc_add_modify (&block, tmp,
6149 : 6536 : build_int_cst (TREE_TYPE (tmp), itype));
6150 : :
6151 : 6536 : int attr = CFI_attribute_other;
6152 : 6536 : if (fsym->attr.pointer)
6153 : : attr = CFI_attribute_pointer;
6154 : 5774 : else if (fsym->attr.allocatable)
6155 : 433 : attr = CFI_attribute_allocatable;
6156 : 6536 : tmp = gfc_get_cfi_desc_attribute (cfi);
6157 : 6536 : gfc_add_modify (&block, tmp,
6158 : 6536 : build_int_cst (TREE_TYPE (tmp), attr));
6159 : :
6160 : : /* The cfi-base_addr assignment could be skipped for 'pointer, intent(out)'.
6161 : : That is very sensible for undefined pointers, but the C code might assume
6162 : : that the pointer retains the value, in particular, if it was NULL. */
6163 : 6536 : if (e->rank == 0)
6164 : : {
6165 : 686 : tmp = gfc_get_cfi_desc_base_addr (cfi);
6166 : 686 : gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), gfc));
6167 : : }
6168 : : else
6169 : : {
6170 : 5850 : tmp = gfc_get_cfi_desc_base_addr (cfi);
6171 : 5850 : tmp2 = gfc_conv_descriptor_data_get (gfc);
6172 : 5850 : gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), tmp2));
6173 : : }
6174 : :
6175 : : /* Set elem_len if known - must be before the next if block.
6176 : : Note that allocatable implies 'len=:'. */
6177 : 6536 : if (e->ts.type != BT_ASSUMED && e->ts.type != BT_CHARACTER )
6178 : : {
6179 : : /* Length is known at compile time; use 'block' for it. */
6180 : 3072 : tmp = size_in_bytes (gfc_typenode_for_spec (&e->ts));
6181 : 3072 : tmp2 = gfc_get_cfi_desc_elem_len (cfi);
6182 : 3072 : gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
6183 : : }
6184 : :
6185 : 6536 : if (fsym->attr.pointer && fsym->attr.intent == INTENT_OUT)
6186 : 91 : goto done;
6187 : :
6188 : : /* When allocatable + intent out, free the cfi descriptor. */
6189 : 6445 : if (fsym->attr.allocatable && fsym->attr.intent == INTENT_OUT)
6190 : : {
6191 : 90 : tmp = gfc_get_cfi_desc_base_addr (cfi);
6192 : 90 : tree call = builtin_decl_explicit (BUILT_IN_FREE);
6193 : 90 : call = build_call_expr_loc (input_location, call, 1, tmp);
6194 : 90 : gfc_add_expr_to_block (&block, fold_convert (void_type_node, call));
6195 : 90 : gfc_add_modify (&block, tmp,
6196 : 90 : fold_convert (TREE_TYPE (tmp), null_pointer_node));
6197 : 90 : goto done;
6198 : : }
6199 : :
6200 : : /* If not unallocated/unassociated. */
6201 : 6355 : gfc_init_block (&block2);
6202 : :
6203 : : /* Set elem_len, which may be only known at run time. */
6204 : 6355 : if (e->ts.type == BT_CHARACTER
6205 : 3410 : && (e->expr_type != EXPR_NULL || gfc_strlen != NULL_TREE))
6206 : : {
6207 : 3408 : gcc_assert (gfc_strlen);
6208 : 3409 : tmp = gfc_strlen;
6209 : 3409 : if (e->ts.kind != 1)
6210 : 1117 : tmp = fold_build2_loc (input_location, MULT_EXPR,
6211 : : gfc_charlen_type_node, tmp,
6212 : : build_int_cst (gfc_charlen_type_node,
6213 : 1117 : e->ts.kind));
6214 : 3409 : tmp2 = gfc_get_cfi_desc_elem_len (cfi);
6215 : 3409 : gfc_add_modify (&block2, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
6216 : : }
6217 : 2946 : else if (e->ts.type == BT_ASSUMED)
6218 : : {
6219 : 54 : tmp = gfc_conv_descriptor_elem_len (gfc);
6220 : 54 : tmp2 = gfc_get_cfi_desc_elem_len (cfi);
6221 : 54 : gfc_add_modify (&block2, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
6222 : : }
6223 : :
6224 : 6355 : if (e->ts.type == BT_ASSUMED)
6225 : : {
6226 : : /* Note: type(*) implies assumed-shape/assumed-rank if fsym requires
6227 : : an CFI descriptor. Use the type in the descriptor as it provide
6228 : : mode information. (Quality of implementation feature.) */
6229 : 54 : tree cond;
6230 : 54 : tree ctype = gfc_get_cfi_desc_type (cfi);
6231 : 54 : tree type = fold_convert (TREE_TYPE (ctype),
6232 : : gfc_conv_descriptor_type (gfc));
6233 : 54 : tree kind = fold_convert (TREE_TYPE (ctype),
6234 : : gfc_conv_descriptor_elem_len (gfc));
6235 : 54 : kind = fold_build2_loc (input_location, LSHIFT_EXPR, TREE_TYPE (type),
6236 : 54 : kind, build_int_cst (TREE_TYPE (type),
6237 : : CFI_type_kind_shift));
6238 : :
6239 : : /* if (BT_VOID) CFI_type_cptr else CFI_type_other */
6240 : : /* Note: BT_VOID is could also be CFI_type_funcptr, but assume c_ptr. */
6241 : 54 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6242 : 54 : build_int_cst (TREE_TYPE (type), BT_VOID));
6243 : 54 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, ctype,
6244 : 54 : build_int_cst (TREE_TYPE (type), CFI_type_cptr));
6245 : 54 : tmp2 = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
6246 : : ctype,
6247 : 54 : build_int_cst (TREE_TYPE (type), CFI_type_other));
6248 : 54 : tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6249 : : tmp, tmp2);
6250 : : /* if (BT_DERIVED) CFI_type_struct else < tmp2 > */
6251 : 54 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6252 : 54 : build_int_cst (TREE_TYPE (type), BT_DERIVED));
6253 : 54 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, ctype,
6254 : 54 : build_int_cst (TREE_TYPE (type), CFI_type_struct));
6255 : 54 : tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6256 : : tmp, tmp2);
6257 : : /* if (BT_CHARACTER) CFI_type_Character + kind=1 else < tmp2 > */
6258 : : /* Note: could also be kind=4, with cfi->elem_len = gfc->elem_len*4. */
6259 : 54 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6260 : 54 : build_int_cst (TREE_TYPE (type), BT_CHARACTER));
6261 : 54 : tmp = build_int_cst (TREE_TYPE (type),
6262 : : CFI_type_from_type_kind (CFI_type_Character, 1));
6263 : 54 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
6264 : : ctype, tmp);
6265 : 54 : tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6266 : : tmp, tmp2);
6267 : : /* if (BT_COMPLEX) CFI_type_Complex + kind/2 else < tmp2 > */
6268 : 54 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6269 : 54 : build_int_cst (TREE_TYPE (type), BT_COMPLEX));
6270 : 54 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR, TREE_TYPE (type),
6271 : 54 : kind, build_int_cst (TREE_TYPE (type), 2));
6272 : 54 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (type), tmp,
6273 : 54 : build_int_cst (TREE_TYPE (type),
6274 : : CFI_type_Complex));
6275 : 54 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
6276 : : ctype, tmp);
6277 : 54 : tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6278 : : tmp, tmp2);
6279 : : /* if (BT_INTEGER || BT_LOGICAL || BT_REAL) type + kind else <tmp2> */
6280 : 54 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6281 : 54 : build_int_cst (TREE_TYPE (type), BT_INTEGER));
6282 : 54 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6283 : 54 : build_int_cst (TREE_TYPE (type), BT_LOGICAL));
6284 : 54 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
6285 : : cond, tmp);
6286 : 54 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6287 : 54 : build_int_cst (TREE_TYPE (type), BT_REAL));
6288 : 54 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
6289 : : cond, tmp);
6290 : 54 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (type),
6291 : : type, kind);
6292 : 54 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
6293 : : ctype, tmp);
6294 : 54 : tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6295 : : tmp, tmp2);
6296 : 54 : gfc_add_expr_to_block (&block2, tmp2);
6297 : : }
6298 : :
6299 : 6355 : if (e->rank != 0)
6300 : : {
6301 : : /* Loop: for (i = 0; i < rank; ++i). */
6302 : 5735 : tree idx = gfc_create_var (TREE_TYPE (rank), "idx");
6303 : : /* Loop body. */
6304 : 5735 : stmtblock_t loop_body;
6305 : 5735 : gfc_init_block (&loop_body);
6306 : : /* cfi->dim[i].lower_bound = (allocatable/pointer)
6307 : : ? gfc->dim[i].lbound : 0 */
6308 : 5735 : if (fsym->attr.pointer || fsym->attr.allocatable)
6309 : 648 : tmp = gfc_conv_descriptor_lbound_get (gfc, idx);
6310 : : else
6311 : 5087 : tmp = gfc_index_zero_node;
6312 : 5735 : gfc_add_modify (&loop_body, gfc_get_cfi_dim_lbound (cfi, idx), tmp);
6313 : : /* cfi->dim[i].extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1. */
6314 : 5735 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6315 : : gfc_conv_descriptor_ubound_get (gfc, idx),
6316 : : gfc_conv_descriptor_lbound_get (gfc, idx));
6317 : 5735 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
6318 : : tmp, gfc_index_one_node);
6319 : 5735 : gfc_add_modify (&loop_body, gfc_get_cfi_dim_extent (cfi, idx), tmp);
6320 : : /* d->dim[n].sm = gfc->dim[i].stride * gfc->span); */
6321 : 5735 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6322 : : gfc_conv_descriptor_stride_get (gfc, idx),
6323 : : gfc_conv_descriptor_span_get (gfc));
6324 : 5735 : gfc_add_modify (&loop_body, gfc_get_cfi_dim_sm (cfi, idx), tmp);
6325 : :
6326 : : /* Generate loop. */
6327 : 11470 : gfc_simple_for_loop (&block2, idx, build_int_cst (TREE_TYPE (idx), 0),
6328 : 5735 : rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
6329 : : gfc_finish_block (&loop_body));
6330 : :
6331 : 5735 : if (e->expr_type == EXPR_VARIABLE
6332 : 5573 : && e->ref
6333 : 5573 : && e->ref->u.ar.type == AR_FULL
6334 : 2732 : && e->symtree->n.sym->attr.dummy
6335 : 988 : && e->symtree->n.sym->as
6336 : 988 : && e->symtree->n.sym->as->type == AS_ASSUMED_SIZE)
6337 : : {
6338 : 138 : tmp = gfc_get_cfi_dim_extent (cfi, gfc_rank_cst[e->rank-1]),
6339 : 138 : gfc_add_modify (&block2, tmp, build_int_cst (TREE_TYPE (tmp), -1));
6340 : : }
6341 : : }
6342 : :
6343 : 6355 : if (fsym->attr.allocatable || fsym->attr.pointer)
6344 : : {
6345 : 1014 : tmp = gfc_get_cfi_desc_base_addr (cfi),
6346 : 1014 : tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
6347 : : tmp, null_pointer_node);
6348 : 1014 : tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
6349 : : build_empty_stmt (input_location));
6350 : 1014 : gfc_add_expr_to_block (&block, tmp);
6351 : : }
6352 : : else
6353 : 5341 : gfc_add_block_to_block (&block, &block2);
6354 : :
6355 : :
6356 : 6536 : done:
6357 : 6536 : if (present)
6358 : : {
6359 : 103 : parmse->expr = build3_loc (input_location, COND_EXPR,
6360 : 103 : TREE_TYPE (parmse->expr),
6361 : : present, parmse->expr, null_pointer_node);
6362 : 103 : tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block),
6363 : : build_empty_stmt (input_location));
6364 : 103 : gfc_add_expr_to_block (&parmse->pre, tmp);
6365 : : }
6366 : : else
6367 : 6433 : gfc_add_block_to_block (&parmse->pre, &block);
6368 : :
6369 : 6536 : gfc_init_block (&block);
6370 : :
6371 : 6536 : if ((!fsym->attr.allocatable && !fsym->attr.pointer)
6372 : 1195 : || fsym->attr.intent == INTENT_IN)
6373 : 5549 : goto post_call;
6374 : :
6375 : 987 : gfc_init_block (&block2);
6376 : 987 : if (e->rank == 0)
6377 : : {
6378 : 428 : tmp = gfc_get_cfi_desc_base_addr (cfi);
6379 : 428 : gfc_add_modify (&block, gfc, fold_convert (TREE_TYPE (gfc), tmp));
6380 : : }
6381 : : else
6382 : : {
6383 : 559 : tmp = gfc_get_cfi_desc_base_addr (cfi);
6384 : 559 : gfc_conv_descriptor_data_set (&block, gfc, tmp);
6385 : :
6386 : 559 : if (fsym->attr.allocatable)
6387 : : {
6388 : : /* gfc->span = cfi->elem_len. */
6389 : 252 : tmp = fold_convert (gfc_array_index_type,
6390 : : gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]));
6391 : : }
6392 : : else
6393 : : {
6394 : : /* gfc->span = ((cfi->dim[0].sm % cfi->elem_len)
6395 : : ? cfi->dim[0].sm : cfi->elem_len). */
6396 : 307 : tmp = gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]);
6397 : 307 : tmp2 = fold_convert (gfc_array_index_type,
6398 : : gfc_get_cfi_desc_elem_len (cfi));
6399 : 307 : tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR,
6400 : : gfc_array_index_type, tmp, tmp2);
6401 : 307 : tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
6402 : : tmp, gfc_index_zero_node);
6403 : 307 : tmp = build3_loc (input_location, COND_EXPR, gfc_array_index_type, tmp,
6404 : : gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]), tmp2);
6405 : : }
6406 : 559 : gfc_conv_descriptor_span_set (&block2, gfc, tmp);
6407 : :
6408 : : /* Calculate offset + set lbound, ubound and stride. */
6409 : 559 : gfc_conv_descriptor_offset_set (&block2, gfc, gfc_index_zero_node);
6410 : : /* Loop: for (i = 0; i < rank; ++i). */
6411 : 559 : tree idx = gfc_create_var (TREE_TYPE (rank), "idx");
6412 : : /* Loop body. */
6413 : 559 : stmtblock_t loop_body;
6414 : 559 : gfc_init_block (&loop_body);
6415 : : /* gfc->dim[i].lbound = ... */
6416 : 559 : tmp = gfc_get_cfi_dim_lbound (cfi, idx);
6417 : 559 : gfc_conv_descriptor_lbound_set (&loop_body, gfc, idx, tmp);
6418 : :
6419 : : /* gfc->dim[i].ubound = gfc->dim[i].lbound + cfi->dim[i].extent - 1. */
6420 : 559 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6421 : : gfc_conv_descriptor_lbound_get (gfc, idx),
6422 : : gfc_index_one_node);
6423 : 559 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
6424 : : gfc_get_cfi_dim_extent (cfi, idx), tmp);
6425 : 559 : gfc_conv_descriptor_ubound_set (&loop_body, gfc, idx, tmp);
6426 : :
6427 : : /* gfc->dim[i].stride = cfi->dim[i].sm / cfi>elem_len */
6428 : 559 : tmp = gfc_get_cfi_dim_sm (cfi, idx);
6429 : 559 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
6430 : : gfc_array_index_type, tmp,
6431 : : fold_convert (gfc_array_index_type,
6432 : : gfc_get_cfi_desc_elem_len (cfi)));
6433 : 559 : gfc_conv_descriptor_stride_set (&loop_body, gfc, idx, tmp);
6434 : :
6435 : : /* gfc->offset -= gfc->dim[i].stride * gfc->dim[i].lbound. */
6436 : 559 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6437 : : gfc_conv_descriptor_stride_get (gfc, idx),
6438 : : gfc_conv_descriptor_lbound_get (gfc, idx));
6439 : 559 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6440 : : gfc_conv_descriptor_offset_get (gfc), tmp);
6441 : 559 : gfc_conv_descriptor_offset_set (&loop_body, gfc, tmp);
6442 : : /* Generate loop. */
6443 : 1118 : gfc_simple_for_loop (&block2, idx, build_int_cst (TREE_TYPE (idx), 0),
6444 : 559 : rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
6445 : : gfc_finish_block (&loop_body));
6446 : : }
6447 : :
6448 : 987 : if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
6449 : : {
6450 : 60 : tmp = fold_convert (gfc_charlen_type_node,
6451 : : gfc_get_cfi_desc_elem_len (cfi));
6452 : 60 : if (e->ts.kind != 1)
6453 : 24 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
6454 : : gfc_charlen_type_node, tmp,
6455 : : build_int_cst (gfc_charlen_type_node,
6456 : 24 : e->ts.kind));
6457 : 60 : gfc_add_modify (&block2, gfc_strlen, tmp);
6458 : : }
6459 : :
6460 : 987 : tmp = gfc_get_cfi_desc_base_addr (cfi),
6461 : 987 : tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
6462 : : tmp, null_pointer_node);
6463 : 987 : tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
6464 : : build_empty_stmt (input_location));
6465 : 987 : gfc_add_expr_to_block (&block, tmp);
6466 : :
6467 : 6536 : post_call:
6468 : 6536 : gfc_add_block_to_block (&block, &se.post);
6469 : 6536 : if (present && block.head)
6470 : : {
6471 : 6 : tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block),
6472 : : build_empty_stmt (input_location));
6473 : 6 : gfc_add_expr_to_block (&parmse->post, tmp);
6474 : : }
6475 : 6530 : else if (block.head)
6476 : 1558 : gfc_add_block_to_block (&parmse->post, &block);
6477 : 6536 : }
6478 : :
6479 : :
6480 : : /* Create "conditional temporary" to handle scalar dummy variables with the
6481 : : OPTIONAL+VALUE attribute that shall not be dereferenced. Use null value
6482 : : as fallback. Does not handle CLASS. */
6483 : :
6484 : : static void
6485 : 222 : conv_cond_temp (gfc_se * parmse, gfc_expr * e, tree cond)
6486 : : {
6487 : 222 : tree temp;
6488 : 222 : gcc_assert (e && e->ts.type != BT_CLASS);
6489 : 222 : gcc_assert (e->rank == 0);
6490 : 222 : temp = gfc_create_var (TREE_TYPE (parmse->expr), "condtemp");
6491 : 222 : TREE_STATIC (temp) = 1;
6492 : 222 : TREE_CONSTANT (temp) = 1;
6493 : 222 : TREE_READONLY (temp) = 1;
6494 : 222 : DECL_INITIAL (temp) = build_zero_cst (TREE_TYPE (temp));
6495 : 222 : parmse->expr = fold_build3_loc (input_location, COND_EXPR,
6496 : 222 : TREE_TYPE (parmse->expr),
6497 : : cond, parmse->expr, temp);
6498 : 222 : parmse->expr = gfc_evaluate_now (parmse->expr, &parmse->pre);
6499 : 222 : }
6500 : :
6501 : :
6502 : : /* Helper function for the handling of (currently) scalar dummy variables
6503 : : with the VALUE attribute. Argument parmse should already be set up. */
6504 : : static void
6505 : 22140 : conv_dummy_value (gfc_se * parmse, gfc_expr * e, gfc_symbol * fsym,
6506 : : vec<tree, va_gc> *& optionalargs)
6507 : : {
6508 : 22140 : tree tmp;
6509 : :
6510 : 22140 : gcc_assert (fsym && fsym->attr.value && !fsym->attr.dimension);
6511 : :
6512 : : /* Absent actual argument for optional scalar dummy. */
6513 : 22140 : if ((e == NULL || e->expr_type == EXPR_NULL) && fsym->attr.optional)
6514 : : {
6515 : : /* For scalar arguments with VALUE attribute which are passed by
6516 : : value, pass "0" and a hidden argument for the optional status. */
6517 : 427 : if (fsym->ts.type == BT_CHARACTER)
6518 : : {
6519 : : /* Pass a NULL pointer for an absent CHARACTER arg and a length of
6520 : : zero. */
6521 : 90 : parmse->expr = null_pointer_node;
6522 : 90 : parmse->string_length = build_int_cst (gfc_charlen_type_node, 0);
6523 : : }
6524 : 337 : else if (gfc_bt_struct (fsym->ts.type)
6525 : 30 : && !(fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING))
6526 : : {
6527 : : /* Pass null struct. Types c_ptr and c_funptr from ISO_C_BINDING
6528 : : are pointers and passed as such below. */
6529 : 24 : tree temp = gfc_create_var (gfc_sym_type (fsym), "absent");
6530 : 24 : TREE_CONSTANT (temp) = 1;
6531 : 24 : TREE_READONLY (temp) = 1;
6532 : 24 : DECL_INITIAL (temp) = build_zero_cst (TREE_TYPE (temp));
6533 : 24 : parmse->expr = temp;
6534 : 24 : }
6535 : : else
6536 : 313 : parmse->expr = fold_convert (gfc_sym_type (fsym),
6537 : : integer_zero_node);
6538 : 427 : vec_safe_push (optionalargs, boolean_false_node);
6539 : :
6540 : 427 : return;
6541 : : }
6542 : :
6543 : : /* gfortran argument passing conventions:
6544 : : actual arguments to CHARACTER(len=1),VALUE
6545 : : dummy arguments are actually passed by value.
6546 : : Strings are truncated to length 1. */
6547 : 21713 : if (gfc_length_one_character_type_p (&fsym->ts))
6548 : : {
6549 : 390 : if (e->expr_type == EXPR_CONSTANT
6550 : 66 : && e->value.character.length > 1)
6551 : : {
6552 : 12 : e->value.character.length = 1;
6553 : 12 : gfc_conv_expr (parmse, e);
6554 : : }
6555 : :
6556 : 390 : tree slen1 = build_int_cst (gfc_charlen_type_node, 1);
6557 : 390 : gfc_conv_string_parameter (parmse);
6558 : 390 : parmse->expr = gfc_string_to_single_character (slen1, parmse->expr,
6559 : : e->ts.kind);
6560 : : /* Truncate resulting string to length 1. */
6561 : 390 : parmse->string_length = slen1;
6562 : : }
6563 : :
6564 : 21713 : if (fsym->attr.optional && fsym->ts.type != BT_CLASS)
6565 : : {
6566 : : /* F2018:15.5.2.12 Argument presence and
6567 : : restrictions on arguments not present. */
6568 : 811 : if (e->expr_type == EXPR_VARIABLE
6569 : 638 : && e->rank == 0
6570 : 1395 : && (gfc_expr_attr (e).allocatable
6571 : 482 : || gfc_expr_attr (e).pointer))
6572 : : {
6573 : 186 : gfc_se argse;
6574 : 186 : tree cond;
6575 : 186 : gfc_init_se (&argse, NULL);
6576 : 186 : argse.want_pointer = 1;
6577 : 186 : gfc_conv_expr (&argse, e);
6578 : 186 : cond = fold_convert (TREE_TYPE (argse.expr), null_pointer_node);
6579 : 186 : cond = fold_build2_loc (input_location, NE_EXPR,
6580 : : logical_type_node,
6581 : : argse.expr, cond);
6582 : 372 : vec_safe_push (optionalargs,
6583 : 186 : fold_convert (boolean_type_node, cond));
6584 : : /* Create "conditional temporary". */
6585 : 186 : conv_cond_temp (parmse, e, cond);
6586 : : }
6587 : 625 : else if (e->expr_type != EXPR_VARIABLE
6588 : 452 : || !e->symtree->n.sym->attr.optional
6589 : 260 : || (e->ref != NULL && e->ref->type != REF_ARRAY))
6590 : 365 : vec_safe_push (optionalargs, boolean_true_node);
6591 : : else
6592 : : {
6593 : 260 : tmp = gfc_conv_expr_present (e->symtree->n.sym);
6594 : 260 : if (gfc_bt_struct (fsym->ts.type)
6595 : 36 : && !(fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING))
6596 : 36 : conv_cond_temp (parmse, e, tmp);
6597 : 224 : else if (e->ts.type != BT_CHARACTER && !e->symtree->n.sym->attr.value)
6598 : 84 : parmse->expr
6599 : 168 : = fold_build3_loc (input_location, COND_EXPR,
6600 : 84 : TREE_TYPE (parmse->expr),
6601 : : tmp, parmse->expr,
6602 : 84 : fold_convert (TREE_TYPE (parmse->expr),
6603 : : integer_zero_node));
6604 : :
6605 : 520 : vec_safe_push (optionalargs,
6606 : 260 : fold_convert (boolean_type_node, tmp));
6607 : : }
6608 : : }
6609 : : }
6610 : :
6611 : :
6612 : : /* Helper function for the handling of NULL() actual arguments associated with
6613 : : non-optional dummy variables. Argument parmse should already be set up. */
6614 : : static void
6615 : 426 : conv_null_actual (gfc_se * parmse, gfc_expr * e, gfc_symbol * fsym)
6616 : : {
6617 : 426 : gcc_assert (fsym && e->expr_type == EXPR_NULL);
6618 : :
6619 : : /* Obtain the character length for a NULL() actual with a character
6620 : : MOLD argument. Otherwise substitute a suitable dummy length.
6621 : : Here we handle only non-optional dummies of non-bind(c) procedures. */
6622 : 426 : if (fsym->ts.type == BT_CHARACTER)
6623 : : {
6624 : 216 : if (e->ts.type == BT_CHARACTER
6625 : 162 : && e->symtree->n.sym->ts.type == BT_CHARACTER)
6626 : : {
6627 : : /* MOLD is present. Substitute a temporary character NULL pointer.
6628 : : For an assumed-rank dummy we need a descriptor that passes the
6629 : : correct rank. */
6630 : 162 : if (fsym->as && fsym->as->type == AS_ASSUMED_RANK)
6631 : : {
6632 : 54 : tree rank;
6633 : 54 : tree tmp = parmse->expr;
6634 : 54 : tmp = gfc_conv_scalar_to_descriptor (parmse, tmp, fsym->attr);
6635 : 54 : rank = gfc_conv_descriptor_rank (tmp);
6636 : 54 : gfc_add_modify (&parmse->pre, rank,
6637 : 54 : build_int_cst (TREE_TYPE (rank), e->rank));
6638 : 54 : parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
6639 : 54 : }
6640 : : else
6641 : : {
6642 : 108 : tree tmp = gfc_create_var (TREE_TYPE (parmse->expr), "null");
6643 : 108 : gfc_add_modify (&parmse->pre, tmp,
6644 : 108 : build_zero_cst (TREE_TYPE (tmp)));
6645 : 108 : parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
6646 : : }
6647 : :
6648 : : /* Ensure that a usable length is available. */
6649 : 162 : if (parmse->string_length == NULL_TREE)
6650 : : {
6651 : 162 : gfc_typespec *ts = &e->symtree->n.sym->ts;
6652 : :
6653 : 162 : if (ts->u.cl->length != NULL
6654 : 108 : && ts->u.cl->length->expr_type == EXPR_CONSTANT)
6655 : 108 : gfc_conv_const_charlen (ts->u.cl);
6656 : :
6657 : 162 : if (ts->u.cl->backend_decl)
6658 : 162 : parmse->string_length = ts->u.cl->backend_decl;
6659 : : }
6660 : : }
6661 : 54 : else if (e->ts.type == BT_UNKNOWN && parmse->string_length == NULL_TREE)
6662 : : {
6663 : : /* MOLD is not present. Pass length of associated dummy character
6664 : : argument if constant, or zero. */
6665 : 54 : if (fsym->ts.u.cl->length != NULL
6666 : 18 : && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
6667 : : {
6668 : 18 : gfc_conv_const_charlen (fsym->ts.u.cl);
6669 : 18 : parmse->string_length = fsym->ts.u.cl->backend_decl;
6670 : : }
6671 : : else
6672 : : {
6673 : 36 : parmse->string_length = gfc_create_var (gfc_charlen_type_node,
6674 : : "slen");
6675 : 36 : gfc_add_modify (&parmse->pre, parmse->string_length,
6676 : : build_zero_cst (gfc_charlen_type_node));
6677 : : }
6678 : : }
6679 : : }
6680 : 210 : else if (fsym->ts.type == BT_DERIVED)
6681 : : {
6682 : 210 : if (e->ts.type != BT_UNKNOWN)
6683 : : /* MOLD is present. Pass a corresponding temporary NULL pointer.
6684 : : For an assumed-rank dummy we provide a descriptor that passes
6685 : : the correct rank. */
6686 : : {
6687 : 138 : tree rank;
6688 : 138 : tree tmp = parmse->expr;
6689 : :
6690 : 138 : tmp = gfc_conv_scalar_to_descriptor (parmse, tmp, gfc_expr_attr (e));
6691 : 138 : rank = gfc_conv_descriptor_rank (tmp);
6692 : 138 : gfc_add_modify (&parmse->pre, rank,
6693 : 138 : build_int_cst (TREE_TYPE (rank), e->rank));
6694 : 138 : gfc_conv_descriptor_data_set (&parmse->pre, tmp, null_pointer_node);
6695 : 138 : parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
6696 : : }
6697 : : else
6698 : : /* MOLD is not present. Use attributes from dummy argument, which is
6699 : : not allowed to be assumed-rank. */
6700 : : {
6701 : 72 : int dummy_rank;
6702 : 72 : tree tmp = parmse->expr;
6703 : :
6704 : 72 : if ((fsym->attr.allocatable || fsym->attr.pointer)
6705 : 72 : && fsym->attr.intent == INTENT_UNKNOWN)
6706 : 36 : fsym->attr.intent = INTENT_IN;
6707 : 72 : tmp = gfc_conv_scalar_to_descriptor (parmse, tmp, fsym->attr);
6708 : 72 : dummy_rank = fsym->as ? fsym->as->rank : 0;
6709 : 24 : if (dummy_rank > 0)
6710 : : {
6711 : 24 : tree rank = gfc_conv_descriptor_rank (tmp);
6712 : 24 : gfc_add_modify (&parmse->pre, rank,
6713 : 24 : build_int_cst (TREE_TYPE (rank), dummy_rank));
6714 : : }
6715 : 72 : gfc_conv_descriptor_data_set (&parmse->pre, tmp, null_pointer_node);
6716 : 72 : parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
6717 : : }
6718 : : }
6719 : 426 : }
6720 : :
6721 : :
6722 : : /* Generate code for a procedure call. Note can return se->post != NULL.
6723 : : If se->direct_byref is set then se->expr contains the return parameter.
6724 : : Return nonzero, if the call has alternate specifiers.
6725 : : 'expr' is only needed for procedure pointer components. */
6726 : :
6727 : : int
6728 : 132633 : gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
6729 : : gfc_actual_arglist * args, gfc_expr * expr,
6730 : : vec<tree, va_gc> *append_args)
6731 : : {
6732 : 132633 : gfc_interface_mapping mapping;
6733 : 132633 : vec<tree, va_gc> *arglist;
6734 : 132633 : vec<tree, va_gc> *retargs;
6735 : 132633 : tree tmp;
6736 : 132633 : tree fntype;
6737 : 132633 : gfc_se parmse;
6738 : 132633 : gfc_array_info *info;
6739 : 132633 : int byref;
6740 : 132633 : int parm_kind;
6741 : 132633 : tree type;
6742 : 132633 : tree var;
6743 : 132633 : tree len;
6744 : 132633 : tree base_object;
6745 : 132633 : vec<tree, va_gc> *stringargs;
6746 : 132633 : vec<tree, va_gc> *optionalargs;
6747 : 132633 : tree result = NULL;
6748 : 132633 : gfc_formal_arglist *formal;
6749 : 132633 : gfc_actual_arglist *arg;
6750 : 132633 : int has_alternate_specifier = 0;
6751 : 132633 : bool need_interface_mapping;
6752 : 132633 : bool is_builtin;
6753 : 132633 : bool callee_alloc;
6754 : 132633 : bool ulim_copy;
6755 : 132633 : gfc_typespec ts;
6756 : 132633 : gfc_charlen cl;
6757 : 132633 : gfc_expr *e;
6758 : 132633 : gfc_symbol *fsym;
6759 : 132633 : enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
6760 : 132633 : gfc_component *comp = NULL;
6761 : 132633 : int arglen;
6762 : 132633 : unsigned int argc;
6763 : 132633 : tree arg1_cntnr = NULL_TREE;
6764 : 132633 : arglist = NULL;
6765 : 132633 : retargs = NULL;
6766 : 132633 : stringargs = NULL;
6767 : 132633 : optionalargs = NULL;
6768 : 132633 : var = NULL_TREE;
6769 : 132633 : len = NULL_TREE;
6770 : 132633 : gfc_clear_ts (&ts);
6771 : 132633 : gfc_intrinsic_sym *isym = expr && expr->rank ?
6772 : : expr->value.function.isym : NULL;
6773 : :
6774 : 132633 : comp = gfc_get_proc_ptr_comp (expr);
6775 : :
6776 : 265266 : bool elemental_proc = (comp
6777 : 1842 : && comp->ts.interface
6778 : 1789 : && comp->ts.interface->attr.elemental)
6779 : 1649 : || (comp && comp->attr.elemental)
6780 : 134282 : || sym->attr.elemental;
6781 : :
6782 : 132633 : if (se->ss != NULL)
6783 : : {
6784 : 24738 : if (!elemental_proc)
6785 : : {
6786 : 21450 : gcc_assert (se->ss->info->type == GFC_SS_FUNCTION);
6787 : 21450 : if (se->ss->info->useflags)
6788 : : {
6789 : 5713 : gcc_assert ((!comp && gfc_return_by_reference (sym)
6790 : : && sym->result->attr.dimension)
6791 : : || (comp && comp->attr.dimension)
6792 : : || gfc_is_class_array_function (expr));
6793 : 5713 : gcc_assert (se->loop != NULL);
6794 : : /* Access the previously obtained result. */
6795 : 5713 : gfc_conv_tmp_array_ref (se);
6796 : 5713 : return 0;
6797 : : }
6798 : : }
6799 : 19025 : info = &se->ss->info->data.array;
6800 : : }
6801 : : else
6802 : : info = NULL;
6803 : :
6804 : 126920 : stmtblock_t post, clobbers, dealloc_blk;
6805 : 126920 : gfc_init_block (&post);
6806 : 126920 : gfc_init_block (&clobbers);
6807 : 126920 : gfc_init_block (&dealloc_blk);
6808 : 126920 : gfc_init_interface_mapping (&mapping);
6809 : 126920 : if (!comp)
6810 : : {
6811 : 125127 : formal = gfc_sym_get_dummy_args (sym);
6812 : 125127 : need_interface_mapping = sym->attr.dimension ||
6813 : 109636 : (sym->ts.type == BT_CHARACTER
6814 : 3031 : && sym->ts.u.cl->length
6815 : 2379 : && sym->ts.u.cl->length->expr_type
6816 : : != EXPR_CONSTANT);
6817 : : }
6818 : : else
6819 : : {
6820 : 1793 : formal = comp->ts.interface ? comp->ts.interface->formal : NULL;
6821 : 1793 : need_interface_mapping = comp->attr.dimension ||
6822 : 1724 : (comp->ts.type == BT_CHARACTER
6823 : 67 : && comp->ts.u.cl->length
6824 : 58 : && comp->ts.u.cl->length->expr_type
6825 : : != EXPR_CONSTANT);
6826 : : }
6827 : :
6828 : 126920 : base_object = NULL_TREE;
6829 : : /* For _vprt->_copy () routines no formal symbol is present. Nevertheless
6830 : : is the third and fourth argument to such a function call a value
6831 : : denoting the number of elements to copy (i.e., most of the time the
6832 : : length of a deferred length string). */
6833 : 253840 : ulim_copy = (formal == NULL)
6834 : 31046 : && UNLIMITED_POLY (sym)
6835 : 126999 : && comp && (strcmp ("_copy", comp->name) == 0);
6836 : :
6837 : : /* Scan for allocatable actual arguments passed to allocatable dummy
6838 : : arguments with INTENT(OUT). As the corresponding actual arguments are
6839 : : deallocated before execution of the procedure, we evaluate actual
6840 : : argument expressions to avoid problems with possible dependencies. */
6841 : 126920 : bool force_eval_args = false;
6842 : 126920 : gfc_formal_arglist *tmp_formal;
6843 : 392759 : for (arg = args, tmp_formal = formal; arg != NULL;
6844 : 232521 : arg = arg->next, tmp_formal = tmp_formal ? tmp_formal->next : NULL)
6845 : : {
6846 : 266326 : e = arg->expr;
6847 : 266326 : fsym = tmp_formal ? tmp_formal->sym : NULL;
6848 : 252495 : if (e && fsym
6849 : 220604 : && e->expr_type == EXPR_VARIABLE
6850 : 96454 : && fsym->attr.intent == INTENT_OUT
6851 : 6192 : && (fsym->ts.type == BT_CLASS && fsym->attr.class_ok
6852 : 6192 : ? CLASS_DATA (fsym)->attr.allocatable
6853 : 4718 : : fsym->attr.allocatable)
6854 : 487 : && e->symtree
6855 : 487 : && e->symtree->n.sym
6856 : 518821 : && gfc_variable_attr (e, NULL).allocatable)
6857 : : {
6858 : : force_eval_args = true;
6859 : : break;
6860 : : }
6861 : : }
6862 : :
6863 : : /* Evaluate the arguments. */
6864 : 393636 : for (arg = args, argc = 0; arg != NULL;
6865 : 266716 : arg = arg->next, formal = formal ? formal->next : NULL, ++argc)
6866 : : {
6867 : 266716 : bool finalized = false;
6868 : 266716 : tree derived_array = NULL_TREE;
6869 : 266716 : symbol_attribute *attr;
6870 : :
6871 : 266716 : e = arg->expr;
6872 : 266716 : fsym = formal ? formal->sym : NULL;
6873 : 500114 : parm_kind = MISSING;
6874 : :
6875 : 233398 : attr = fsym ? &(fsym->ts.type == BT_CLASS ? CLASS_DATA (fsym)->attr
6876 : : : fsym->attr)
6877 : : : nullptr;
6878 : : /* If the procedure requires an explicit interface, the actual
6879 : : argument is passed according to the corresponding formal
6880 : : argument. If the corresponding formal argument is a POINTER,
6881 : : ALLOCATABLE or assumed shape, we do not use g77's calling
6882 : : convention, and pass the address of the array descriptor
6883 : : instead. Otherwise we use g77's calling convention, in other words
6884 : : pass the array data pointer without descriptor. */
6885 : 233345 : bool nodesc_arg = fsym != NULL
6886 : 233345 : && !(fsym->attr.pointer || fsym->attr.allocatable)
6887 : 224427 : && fsym->as
6888 : 39666 : && fsym->as->type != AS_ASSUMED_SHAPE
6889 : 24546 : && fsym->as->type != AS_ASSUMED_RANK;
6890 : 266716 : if (comp)
6891 : 2697 : nodesc_arg = nodesc_arg || !comp->attr.always_explicit;
6892 : : else
6893 : 264019 : nodesc_arg
6894 : : = nodesc_arg
6895 : 264019 : || !(sym->attr.always_explicit || (attr && attr->codimension));
6896 : :
6897 : : /* Class array expressions are sometimes coming completely unadorned
6898 : : with either arrayspec or _data component. Correct that here.
6899 : : OOP-TODO: Move this to the frontend. */
6900 : 266716 : if (e && e->expr_type == EXPR_VARIABLE
6901 : 110568 : && !e->ref
6902 : 50604 : && e->ts.type == BT_CLASS
6903 : 2593 : && (CLASS_DATA (e)->attr.codimension
6904 : 2593 : || CLASS_DATA (e)->attr.dimension))
6905 : : {
6906 : 0 : gfc_typespec temp_ts = e->ts;
6907 : 0 : gfc_add_class_array_ref (e);
6908 : 0 : e->ts = temp_ts;
6909 : : }
6910 : :
6911 : 266716 : if (e == NULL
6912 : 252879 : || (e->expr_type == EXPR_NULL
6913 : 745 : && fsym
6914 : 745 : && fsym->attr.value
6915 : 72 : && fsym->attr.optional
6916 : 72 : && !fsym->attr.dimension
6917 : 72 : && fsym->ts.type != BT_CLASS))
6918 : : {
6919 : 13909 : if (se->ignore_optional)
6920 : : {
6921 : : /* Some intrinsics have already been resolved to the correct
6922 : : parameters. */
6923 : 422 : continue;
6924 : : }
6925 : 13711 : else if (arg->label)
6926 : : {
6927 : 224 : has_alternate_specifier = 1;
6928 : 224 : continue;
6929 : : }
6930 : : else
6931 : : {
6932 : 13487 : gfc_init_se (&parmse, NULL);
6933 : :
6934 : : /* For scalar arguments with VALUE attribute which are passed by
6935 : : value, pass "0" and a hidden argument gives the optional
6936 : : status. */
6937 : 13487 : if (fsym && fsym->attr.optional && fsym->attr.value
6938 : 427 : && !fsym->attr.dimension && fsym->ts.type != BT_CLASS)
6939 : : {
6940 : 427 : conv_dummy_value (&parmse, e, fsym, optionalargs);
6941 : : }
6942 : : else
6943 : : {
6944 : : /* Pass a NULL pointer for an absent arg. */
6945 : 13060 : parmse.expr = null_pointer_node;
6946 : :
6947 : : /* Is it an absent character dummy? */
6948 : 13060 : bool absent_char = false;
6949 : 13060 : gfc_dummy_arg * const dummy_arg = arg->associated_dummy;
6950 : :
6951 : : /* Fall back to inferred type only if no formal. */
6952 : 13060 : if (fsym)
6953 : 12002 : absent_char = (fsym->ts.type == BT_CHARACTER);
6954 : 1058 : else if (dummy_arg)
6955 : 1058 : absent_char = (gfc_dummy_arg_get_typespec (*dummy_arg).type
6956 : : == BT_CHARACTER);
6957 : 13060 : if (absent_char)
6958 : 1115 : parmse.string_length = build_int_cst (gfc_charlen_type_node,
6959 : : 0);
6960 : : }
6961 : : }
6962 : : }
6963 : 252807 : else if (e->expr_type == EXPR_NULL
6964 : 673 : && (e->ts.type == BT_UNKNOWN || e->ts.type == BT_DERIVED)
6965 : 371 : && fsym && attr && (attr->pointer || attr->allocatable)
6966 : 293 : && fsym->ts.type == BT_DERIVED)
6967 : : {
6968 : 210 : gfc_init_se (&parmse, NULL);
6969 : 210 : gfc_conv_expr_reference (&parmse, e);
6970 : 210 : conv_null_actual (&parmse, e, fsym);
6971 : : }
6972 : 252597 : else if (arg->expr->expr_type == EXPR_NULL
6973 : 463 : && fsym && !fsym->attr.pointer
6974 : 163 : && (fsym->ts.type != BT_CLASS
6975 : 6 : || !CLASS_DATA (fsym)->attr.class_pointer))
6976 : : {
6977 : : /* Pass a NULL pointer to denote an absent arg. */
6978 : 163 : gcc_assert (fsym->attr.optional && !fsym->attr.allocatable
6979 : : && (fsym->ts.type != BT_CLASS
6980 : : || !CLASS_DATA (fsym)->attr.allocatable));
6981 : 163 : gfc_init_se (&parmse, NULL);
6982 : 163 : parmse.expr = null_pointer_node;
6983 : 163 : if (fsym->ts.type == BT_CHARACTER)
6984 : 42 : parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
6985 : : }
6986 : 252434 : else if (fsym && fsym->ts.type == BT_CLASS
6987 : 10490 : && e->ts.type == BT_DERIVED)
6988 : : {
6989 : : /* The derived type needs to be converted to a temporary
6990 : : CLASS object. */
6991 : 4180 : gfc_init_se (&parmse, se);
6992 : 4180 : gfc_conv_derived_to_class (&parmse, e, fsym, NULL_TREE,
6993 : 4180 : fsym->attr.optional
6994 : 1008 : && e->expr_type == EXPR_VARIABLE
6995 : 5188 : && e->symtree->n.sym->attr.optional,
6996 : 4180 : CLASS_DATA (fsym)->attr.class_pointer
6997 : 4180 : || CLASS_DATA (fsym)->attr.allocatable,
6998 : : sym->name, &derived_array);
6999 : : }
7000 : 216363 : else if (UNLIMITED_POLY (fsym) && e->ts.type != BT_CLASS
7001 : 888 : && e->ts.type != BT_PROCEDURE
7002 : 864 : && (gfc_expr_attr (e).flavor != FL_PROCEDURE
7003 : 12 : || gfc_expr_attr (e).proc != PROC_UNKNOWN))
7004 : : {
7005 : : /* The intrinsic type needs to be converted to a temporary
7006 : : CLASS object for the unlimited polymorphic formal. */
7007 : 864 : gfc_find_vtab (&e->ts);
7008 : 864 : gfc_init_se (&parmse, se);
7009 : 864 : gfc_conv_intrinsic_to_class (&parmse, e, fsym->ts);
7010 : :
7011 : : }
7012 : 247390 : else if (se->ss && se->ss->info->useflags)
7013 : : {
7014 : 5518 : gfc_ss *ss;
7015 : :
7016 : 5518 : ss = se->ss;
7017 : :
7018 : : /* An elemental function inside a scalarized loop. */
7019 : 5518 : gfc_init_se (&parmse, se);
7020 : 5518 : parm_kind = ELEMENTAL;
7021 : :
7022 : : /* When no fsym is present, ulim_copy is set and this is a third or
7023 : : fourth argument, use call-by-value instead of by reference to
7024 : : hand the length properties to the copy routine (i.e., most of the
7025 : : time this will be a call to a __copy_character_* routine where the
7026 : : third and fourth arguments are the lengths of a deferred length
7027 : : char array). */
7028 : 5518 : if ((fsym && fsym->attr.value)
7029 : 5284 : || (ulim_copy && (argc == 2 || argc == 3)))
7030 : 234 : gfc_conv_expr (&parmse, e);
7031 : 5284 : else if (e->expr_type == EXPR_ARRAY)
7032 : : {
7033 : 294 : gfc_conv_expr (&parmse, e);
7034 : 294 : if (e->ts.type != BT_CHARACTER)
7035 : 251 : parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
7036 : : }
7037 : : else
7038 : 4990 : gfc_conv_expr_reference (&parmse, e);
7039 : :
7040 : 5518 : if (e->ts.type == BT_CHARACTER && !e->rank
7041 : 174 : && e->expr_type == EXPR_FUNCTION)
7042 : 12 : parmse.expr = build_fold_indirect_ref_loc (input_location,
7043 : : parmse.expr);
7044 : :
7045 : 5468 : if (fsym && fsym->ts.type == BT_DERIVED
7046 : 6858 : && gfc_is_class_container_ref (e))
7047 : : {
7048 : 24 : parmse.expr = gfc_class_data_get (parmse.expr);
7049 : :
7050 : 24 : if (fsym->attr.optional && e->expr_type == EXPR_VARIABLE
7051 : 24 : && e->symtree->n.sym->attr.optional)
7052 : : {
7053 : 0 : tree cond = gfc_conv_expr_present (e->symtree->n.sym);
7054 : 0 : parmse.expr = build3_loc (input_location, COND_EXPR,
7055 : 0 : TREE_TYPE (parmse.expr),
7056 : : cond, parmse.expr,
7057 : 0 : fold_convert (TREE_TYPE (parmse.expr),
7058 : : null_pointer_node));
7059 : : }
7060 : : }
7061 : :
7062 : : /* Scalar dummy arguments of intrinsic type or derived type with
7063 : : VALUE attribute. */
7064 : 5518 : if (fsym
7065 : 5468 : && fsym->attr.value
7066 : 234 : && fsym->ts.type != BT_CLASS)
7067 : 234 : conv_dummy_value (&parmse, e, fsym, optionalargs);
7068 : :
7069 : : /* If we are passing an absent array as optional dummy to an
7070 : : elemental procedure, make sure that we pass NULL when the data
7071 : : pointer is NULL. We need this extra conditional because of
7072 : : scalarization which passes arrays elements to the procedure,
7073 : : ignoring the fact that the array can be absent/unallocated/... */
7074 : 5284 : else if (ss->info->can_be_null_ref
7075 : 415 : && ss->info->type != GFC_SS_REFERENCE)
7076 : : {
7077 : 193 : tree descriptor_data;
7078 : :
7079 : 193 : descriptor_data = ss->info->data.array.data;
7080 : 193 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7081 : : descriptor_data,
7082 : 193 : fold_convert (TREE_TYPE (descriptor_data),
7083 : : null_pointer_node));
7084 : 193 : parmse.expr
7085 : 386 : = fold_build3_loc (input_location, COND_EXPR,
7086 : 193 : TREE_TYPE (parmse.expr),
7087 : : gfc_unlikely (tmp, PRED_FORTRAN_ABSENT_DUMMY),
7088 : 193 : fold_convert (TREE_TYPE (parmse.expr),
7089 : : null_pointer_node),
7090 : : parmse.expr);
7091 : : }
7092 : :
7093 : : /* The scalarizer does not repackage the reference to a class
7094 : : array - instead it returns a pointer to the data element. */
7095 : 5518 : if (fsym && fsym->ts.type == BT_CLASS && e->ts.type == BT_CLASS)
7096 : 156 : gfc_conv_class_to_class (&parmse, e, fsym->ts, true,
7097 : 156 : fsym->attr.intent != INTENT_IN
7098 : 156 : && (CLASS_DATA (fsym)->attr.class_pointer
7099 : 12 : || CLASS_DATA (fsym)->attr.allocatable),
7100 : 156 : fsym->attr.optional
7101 : 0 : && e->expr_type == EXPR_VARIABLE
7102 : 156 : && e->symtree->n.sym->attr.optional,
7103 : 156 : CLASS_DATA (fsym)->attr.class_pointer
7104 : 156 : || CLASS_DATA (fsym)->attr.allocatable);
7105 : : }
7106 : : else
7107 : : {
7108 : 241872 : bool scalar;
7109 : 241872 : gfc_ss *argss;
7110 : :
7111 : 241872 : gfc_init_se (&parmse, NULL);
7112 : :
7113 : : /* Check whether the expression is a scalar or not; we cannot use
7114 : : e->rank as it can be nonzero for functions arguments. */
7115 : 241872 : argss = gfc_walk_expr (e);
7116 : 241872 : scalar = argss == gfc_ss_terminator;
7117 : 241872 : if (!scalar)
7118 : 59638 : gfc_free_ss_chain (argss);
7119 : :
7120 : : /* Special handling for passing scalar polymorphic coarrays;
7121 : : otherwise one passes "class->_data.data" instead of "&class". */
7122 : 241872 : if (e->rank == 0 && e->ts.type == BT_CLASS
7123 : 3518 : && fsym && fsym->ts.type == BT_CLASS
7124 : 3096 : && CLASS_DATA (fsym)->attr.codimension
7125 : 51 : && !CLASS_DATA (fsym)->attr.dimension)
7126 : : {
7127 : 51 : gfc_add_class_array_ref (e);
7128 : 51 : parmse.want_coarray = 1;
7129 : 51 : scalar = false;
7130 : : }
7131 : :
7132 : : /* A scalar or transformational function. */
7133 : 241872 : if (scalar)
7134 : : {
7135 : 182183 : if (e->expr_type == EXPR_VARIABLE
7136 : 54089 : && e->symtree->n.sym->attr.cray_pointee
7137 : 390 : && fsym && fsym->attr.flavor == FL_PROCEDURE)
7138 : : {
7139 : : /* The Cray pointer needs to be converted to a pointer to
7140 : : a type given by the expression. */
7141 : 6 : gfc_conv_expr (&parmse, e);
7142 : 6 : type = build_pointer_type (TREE_TYPE (parmse.expr));
7143 : 6 : tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
7144 : 6 : parmse.expr = convert (type, tmp);
7145 : : }
7146 : :
7147 : 182177 : else if (sym->attr.is_bind_c && e && is_CFI_desc (fsym, NULL))
7148 : : /* Implement F2018, 18.3.6, list item (5), bullet point 2. */
7149 : 686 : gfc_conv_gfc_desc_to_cfi_desc (&parmse, e, fsym);
7150 : :
7151 : 181491 : else if (fsym && fsym->attr.value)
7152 : : {
7153 : 21650 : if (fsym->ts.type == BT_CHARACTER
7154 : 537 : && fsym->ts.is_c_interop
7155 : 180 : && fsym->ns->proc_name != NULL
7156 : 180 : && fsym->ns->proc_name->attr.is_bind_c)
7157 : : {
7158 : 171 : parmse.expr = NULL;
7159 : 171 : conv_scalar_char_value (fsym, &parmse, &e);
7160 : 171 : if (parmse.expr == NULL)
7161 : 165 : gfc_conv_expr (&parmse, e);
7162 : : }
7163 : : else
7164 : : {
7165 : 21479 : gfc_conv_expr (&parmse, e);
7166 : 21479 : conv_dummy_value (&parmse, e, fsym, optionalargs);
7167 : : }
7168 : : }
7169 : :
7170 : 159841 : else if (arg->name && arg->name[0] == '%')
7171 : : /* Argument list functions %VAL, %LOC and %REF are signalled
7172 : : through arg->name. */
7173 : 5822 : conv_arglist_function (&parmse, arg->expr, arg->name);
7174 : 154019 : else if ((e->expr_type == EXPR_FUNCTION)
7175 : 8159 : && ((e->value.function.esym
7176 : 2141 : && e->value.function.esym->result->attr.pointer)
7177 : 8064 : || (!e->value.function.esym
7178 : 6018 : && e->symtree->n.sym->attr.pointer))
7179 : 95 : && fsym && fsym->attr.target)
7180 : : /* Make sure the function only gets called once. */
7181 : 8 : gfc_conv_expr_reference (&parmse, e);
7182 : 154011 : else if (e->expr_type == EXPR_FUNCTION
7183 : 8151 : && e->symtree->n.sym->result
7184 : 7122 : && e->symtree->n.sym->result != e->symtree->n.sym
7185 : 136 : && e->symtree->n.sym->result->attr.proc_pointer)
7186 : : {
7187 : : /* Functions returning procedure pointers. */
7188 : 18 : gfc_conv_expr (&parmse, e);
7189 : 18 : if (fsym && fsym->attr.proc_pointer)
7190 : 6 : parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
7191 : : }
7192 : :
7193 : : else
7194 : : {
7195 : 153993 : bool defer_to_dealloc_blk = false;
7196 : 153993 : if (e->ts.type == BT_CLASS && fsym
7197 : 3455 : && fsym->ts.type == BT_CLASS
7198 : 3033 : && (!CLASS_DATA (fsym)->as
7199 : 356 : || CLASS_DATA (fsym)->as->type != AS_ASSUMED_RANK)
7200 : 2677 : && CLASS_DATA (e)->attr.codimension)
7201 : : {
7202 : 48 : gcc_assert (!CLASS_DATA (fsym)->attr.codimension);
7203 : 48 : gcc_assert (!CLASS_DATA (fsym)->as);
7204 : 48 : gfc_add_class_array_ref (e);
7205 : 48 : parmse.want_coarray = 1;
7206 : 48 : gfc_conv_expr_reference (&parmse, e);
7207 : 48 : class_scalar_coarray_to_class (&parmse, e, fsym->ts,
7208 : 48 : fsym->attr.optional
7209 : 48 : && e->expr_type == EXPR_VARIABLE);
7210 : : }
7211 : 153945 : else if (e->ts.type == BT_CLASS && fsym
7212 : 3407 : && fsym->ts.type == BT_CLASS
7213 : 2985 : && !CLASS_DATA (fsym)->as
7214 : 2629 : && !CLASS_DATA (e)->as
7215 : 2519 : && strcmp (fsym->ts.u.derived->name,
7216 : : e->ts.u.derived->name))
7217 : : {
7218 : 1602 : type = gfc_typenode_for_spec (&fsym->ts);
7219 : 1602 : var = gfc_create_var (type, fsym->name);
7220 : 1602 : gfc_conv_expr (&parmse, e);
7221 : 1602 : if (fsym->attr.optional
7222 : 153 : && e->expr_type == EXPR_VARIABLE
7223 : 153 : && e->symtree->n.sym->attr.optional)
7224 : : {
7225 : 66 : stmtblock_t block;
7226 : 66 : tree cond;
7227 : 66 : tmp = gfc_build_addr_expr (NULL_TREE, parmse.expr);
7228 : 66 : cond = fold_build2_loc (input_location, NE_EXPR,
7229 : : logical_type_node, tmp,
7230 : 66 : fold_convert (TREE_TYPE (tmp),
7231 : : null_pointer_node));
7232 : 66 : gfc_start_block (&block);
7233 : 66 : gfc_add_modify (&block, var,
7234 : : fold_build1_loc (input_location,
7235 : : VIEW_CONVERT_EXPR,
7236 : : type, parmse.expr));
7237 : 66 : gfc_add_expr_to_block (&parmse.pre,
7238 : : fold_build3_loc (input_location,
7239 : : COND_EXPR, void_type_node,
7240 : : cond, gfc_finish_block (&block),
7241 : : build_empty_stmt (input_location)));
7242 : 66 : parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
7243 : 132 : parmse.expr = build3_loc (input_location, COND_EXPR,
7244 : 66 : TREE_TYPE (parmse.expr),
7245 : : cond, parmse.expr,
7246 : 66 : fold_convert (TREE_TYPE (parmse.expr),
7247 : : null_pointer_node));
7248 : 66 : }
7249 : : else
7250 : : {
7251 : : /* Since the internal representation of unlimited
7252 : : polymorphic expressions includes an extra field
7253 : : that other class objects do not, a cast to the
7254 : : formal type does not work. */
7255 : 1536 : if (!UNLIMITED_POLY (e) && UNLIMITED_POLY (fsym))
7256 : : {
7257 : 91 : tree efield;
7258 : :
7259 : : /* Evaluate arguments just once, when they have
7260 : : side effects. */
7261 : 91 : if (TREE_SIDE_EFFECTS (parmse.expr))
7262 : : {
7263 : 25 : tree cldata, zero;
7264 : :
7265 : 25 : parmse.expr = gfc_evaluate_now (parmse.expr,
7266 : : &parmse.pre);
7267 : :
7268 : : /* Prevent memory leak, when old component
7269 : : was allocated already. */
7270 : 25 : cldata = gfc_class_data_get (parmse.expr);
7271 : 25 : zero = build_int_cst (TREE_TYPE (cldata),
7272 : : 0);
7273 : 25 : tmp = fold_build2_loc (input_location, NE_EXPR,
7274 : : logical_type_node,
7275 : : cldata, zero);
7276 : 25 : tmp = build3_v (COND_EXPR, tmp,
7277 : : gfc_call_free (cldata),
7278 : : build_empty_stmt (
7279 : : input_location));
7280 : 25 : gfc_add_expr_to_block (&parmse.finalblock,
7281 : : tmp);
7282 : 25 : gfc_add_modify (&parmse.finalblock,
7283 : : cldata, zero);
7284 : : }
7285 : :
7286 : : /* Set the _data field. */
7287 : 91 : tmp = gfc_class_data_get (var);
7288 : 91 : efield = fold_convert (TREE_TYPE (tmp),
7289 : : gfc_class_data_get (parmse.expr));
7290 : 91 : gfc_add_modify (&parmse.pre, tmp, efield);
7291 : :
7292 : : /* Set the _vptr field. */
7293 : 91 : tmp = gfc_class_vptr_get (var);
7294 : 91 : efield = fold_convert (TREE_TYPE (tmp),
7295 : : gfc_class_vptr_get (parmse.expr));
7296 : 91 : gfc_add_modify (&parmse.pre, tmp, efield);
7297 : :
7298 : : /* Set the _len field. */
7299 : 91 : tmp = gfc_class_len_get (var);
7300 : 91 : gfc_add_modify (&parmse.pre, tmp,
7301 : 91 : build_int_cst (TREE_TYPE (tmp), 0));
7302 : 91 : }
7303 : : else
7304 : : {
7305 : 1445 : tmp = fold_build1_loc (input_location,
7306 : : VIEW_CONVERT_EXPR,
7307 : : type, parmse.expr);
7308 : 1445 : gfc_add_modify (&parmse.pre, var, tmp);
7309 : 1536 : ;
7310 : : }
7311 : 1536 : parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
7312 : : }
7313 : : }
7314 : : else
7315 : : {
7316 : 152343 : gfc_conv_expr_reference (&parmse, e);
7317 : :
7318 : 152343 : gfc_symbol *dsym = fsym;
7319 : 152343 : gfc_dummy_arg *dummy;
7320 : :
7321 : : /* Use associated dummy as fallback for formal
7322 : : argument if there is no explicit interface. */
7323 : 152343 : if (dsym == NULL
7324 : 27410 : && (dummy = arg->associated_dummy)
7325 : 24916 : && dummy->intrinsicness == GFC_NON_INTRINSIC_DUMMY_ARG
7326 : 175817 : && dummy->u.non_intrinsic->sym)
7327 : : dsym = dummy->u.non_intrinsic->sym;
7328 : :
7329 : 152343 : if (dsym
7330 : 148407 : && dsym->attr.intent == INTENT_OUT
7331 : 3215 : && !dsym->attr.allocatable
7332 : 3074 : && !dsym->attr.pointer
7333 : 3056 : && e->expr_type == EXPR_VARIABLE
7334 : 3055 : && e->ref == NULL
7335 : 2948 : && e->symtree
7336 : 2948 : && e->symtree->n.sym
7337 : 2948 : && !e->symtree->n.sym->attr.dimension
7338 : 2948 : && e->ts.type != BT_CHARACTER
7339 : 2846 : && e->ts.type != BT_CLASS
7340 : 2616 : && (e->ts.type != BT_DERIVED
7341 : 492 : || (dsym->ts.type == BT_DERIVED
7342 : 492 : && e->ts.u.derived == dsym->ts.u.derived
7343 : : /* Types with allocatable components are
7344 : : excluded from clobbering because we need
7345 : : the unclobbered pointers to free the
7346 : : allocatable components in the callee.
7347 : : Same goes for finalizable types or types
7348 : : with finalizable components, we need to
7349 : : pass the unclobbered values to the
7350 : : finalization routines.
7351 : : For parameterized types, it's less clear
7352 : : but they may not have a constant size
7353 : : so better exclude them in any case. */
7354 : 477 : && !e->ts.u.derived->attr.alloc_comp
7355 : 351 : && !e->ts.u.derived->attr.pdt_type
7356 : 351 : && !gfc_is_finalizable (e->ts.u.derived, NULL)))
7357 : 154776 : && !sym->attr.elemental)
7358 : : {
7359 : 1100 : tree var;
7360 : 1100 : var = build_fold_indirect_ref_loc (input_location,
7361 : : parmse.expr);
7362 : 1100 : tree clobber = build_clobber (TREE_TYPE (var));
7363 : 1100 : gfc_add_modify (&clobbers, var, clobber);
7364 : : }
7365 : : }
7366 : : /* Catch base objects that are not variables. */
7367 : 153993 : if (e->ts.type == BT_CLASS
7368 : 3455 : && e->expr_type != EXPR_VARIABLE
7369 : 306 : && expr && e == expr->base_expr)
7370 : 80 : base_object = build_fold_indirect_ref_loc (input_location,
7371 : : parmse.expr);
7372 : :
7373 : : /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
7374 : : allocated on entry, it must be deallocated. */
7375 : 126583 : if (fsym && fsym->attr.intent == INTENT_OUT
7376 : 3144 : && (fsym->attr.allocatable
7377 : 3003 : || (fsym->ts.type == BT_CLASS
7378 : 259 : && CLASS_DATA (fsym)->attr.allocatable))
7379 : 154283 : && !is_CFI_desc (fsym, NULL))
7380 : : {
7381 : 290 : stmtblock_t block;
7382 : 290 : tree ptr;
7383 : :
7384 : 290 : defer_to_dealloc_blk = true;
7385 : :
7386 : 290 : parmse.expr = gfc_evaluate_data_ref_now (parmse.expr,
7387 : : &parmse.pre);
7388 : :
7389 : 290 : if (parmse.class_container != NULL_TREE)
7390 : 156 : parmse.class_container
7391 : 156 : = gfc_evaluate_data_ref_now (parmse.class_container,
7392 : : &parmse.pre);
7393 : :
7394 : 290 : gfc_init_block (&block);
7395 : 290 : ptr = parmse.expr;
7396 : 290 : if (e->ts.type == BT_CLASS)
7397 : 156 : ptr = gfc_class_data_get (ptr);
7398 : :
7399 : 290 : tree cls = parmse.class_container;
7400 : 290 : tmp = gfc_deallocate_scalar_with_status (ptr, NULL_TREE,
7401 : : NULL_TREE, true,
7402 : : e, e->ts, cls);
7403 : 290 : gfc_add_expr_to_block (&block, tmp);
7404 : 290 : gfc_add_modify (&block, ptr,
7405 : 290 : fold_convert (TREE_TYPE (ptr),
7406 : : null_pointer_node));
7407 : :
7408 : 290 : if (fsym->ts.type == BT_CLASS)
7409 : 149 : gfc_reset_vptr (&block, nullptr,
7410 : : build_fold_indirect_ref (parmse.expr),
7411 : 149 : fsym->ts.u.derived);
7412 : :
7413 : 290 : if (fsym->attr.optional
7414 : 42 : && e->expr_type == EXPR_VARIABLE
7415 : 42 : && e->symtree->n.sym->attr.optional)
7416 : : {
7417 : 36 : tmp = fold_build3_loc (input_location, COND_EXPR,
7418 : : void_type_node,
7419 : 18 : gfc_conv_expr_present (e->symtree->n.sym),
7420 : : gfc_finish_block (&block),
7421 : : build_empty_stmt (input_location));
7422 : : }
7423 : : else
7424 : 272 : tmp = gfc_finish_block (&block);
7425 : :
7426 : 290 : gfc_add_expr_to_block (&dealloc_blk, tmp);
7427 : : }
7428 : :
7429 : : /* A class array element needs converting back to be a
7430 : : class object, if the formal argument is a class object. */
7431 : 153993 : if (fsym && fsym->ts.type == BT_CLASS
7432 : 3057 : && e->ts.type == BT_CLASS
7433 : 3033 : && ((CLASS_DATA (fsym)->as
7434 : 356 : && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
7435 : 2677 : || CLASS_DATA (e)->attr.dimension))
7436 : : {
7437 : 466 : gfc_se class_se = parmse;
7438 : 466 : gfc_init_block (&class_se.pre);
7439 : 466 : gfc_init_block (&class_se.post);
7440 : :
7441 : 466 : gfc_conv_class_to_class (&class_se, e, fsym->ts, false,
7442 : 466 : fsym->attr.intent != INTENT_IN
7443 : 466 : && (CLASS_DATA (fsym)->attr.class_pointer
7444 : 267 : || CLASS_DATA (fsym)->attr.allocatable),
7445 : 466 : fsym->attr.optional
7446 : 198 : && e->expr_type == EXPR_VARIABLE
7447 : 664 : && e->symtree->n.sym->attr.optional,
7448 : 466 : CLASS_DATA (fsym)->attr.class_pointer
7449 : 466 : || CLASS_DATA (fsym)->attr.allocatable);
7450 : :
7451 : 466 : parmse.expr = class_se.expr;
7452 : 442 : stmtblock_t *class_pre_block = defer_to_dealloc_blk
7453 : 466 : ? &dealloc_blk
7454 : : : &parmse.pre;
7455 : 466 : gfc_add_block_to_block (class_pre_block, &class_se.pre);
7456 : 466 : gfc_add_block_to_block (&parmse.post, &class_se.post);
7457 : : }
7458 : :
7459 : 126583 : if (fsym && (fsym->ts.type == BT_DERIVED
7460 : 114978 : || fsym->ts.type == BT_ASSUMED)
7461 : 12456 : && e->ts.type == BT_CLASS
7462 : 410 : && !CLASS_DATA (e)->attr.dimension
7463 : 374 : && !CLASS_DATA (e)->attr.codimension)
7464 : : {
7465 : 374 : parmse.expr = gfc_class_data_get (parmse.expr);
7466 : : /* The result is a class temporary, whose _data component
7467 : : must be freed to avoid a memory leak. */
7468 : 374 : if (e->expr_type == EXPR_FUNCTION
7469 : 23 : && CLASS_DATA (e)->attr.allocatable)
7470 : : {
7471 : 19 : tree zero;
7472 : :
7473 : : /* Finalize the expression. */
7474 : 19 : gfc_finalize_tree_expr (&parmse, NULL,
7475 : : gfc_expr_attr (e), e->rank);
7476 : 19 : gfc_add_block_to_block (&parmse.post,
7477 : : &parmse.finalblock);
7478 : :
7479 : : /* Then free the class _data. */
7480 : 19 : zero = build_int_cst (TREE_TYPE (parmse.expr), 0);
7481 : 19 : tmp = fold_build2_loc (input_location, NE_EXPR,
7482 : : logical_type_node,
7483 : : parmse.expr, zero);
7484 : 19 : tmp = build3_v (COND_EXPR, tmp,
7485 : : gfc_call_free (parmse.expr),
7486 : : build_empty_stmt (input_location));
7487 : 19 : gfc_add_expr_to_block (&parmse.post, tmp);
7488 : 19 : gfc_add_modify (&parmse.post, parmse.expr, zero);
7489 : : }
7490 : : }
7491 : :
7492 : : /* Wrap scalar variable in a descriptor. We need to convert
7493 : : the address of a pointer back to the pointer itself before,
7494 : : we can assign it to the data field. */
7495 : :
7496 : 126583 : if (fsym && fsym->as && fsym->as->type == AS_ASSUMED_RANK
7497 : 1293 : && fsym->ts.type != BT_CLASS && e->expr_type != EXPR_NULL)
7498 : : {
7499 : 1221 : tmp = parmse.expr;
7500 : 1221 : if (TREE_CODE (tmp) == ADDR_EXPR)
7501 : 715 : tmp = TREE_OPERAND (tmp, 0);
7502 : 1221 : parmse.expr = gfc_conv_scalar_to_descriptor (&parmse, tmp,
7503 : : fsym->attr);
7504 : 1221 : parmse.expr = gfc_build_addr_expr (NULL_TREE,
7505 : : parmse.expr);
7506 : : }
7507 : 125362 : else if (fsym && e->expr_type != EXPR_NULL
7508 : 125064 : && ((fsym->attr.pointer
7509 : 1734 : && fsym->attr.flavor != FL_PROCEDURE)
7510 : 123336 : || (fsym->attr.proc_pointer
7511 : 157 : && !(e->expr_type == EXPR_VARIABLE
7512 : 157 : && e->symtree->n.sym->attr.dummy))
7513 : 123191 : || (fsym->attr.proc_pointer
7514 : 12 : && e->expr_type == EXPR_VARIABLE
7515 : 12 : && gfc_is_proc_ptr_comp (e))
7516 : 123185 : || (fsym->attr.allocatable
7517 : 1007 : && fsym->attr.flavor != FL_PROCEDURE)))
7518 : : {
7519 : : /* Scalar pointer dummy args require an extra level of
7520 : : indirection. The null pointer already contains
7521 : : this level of indirection. */
7522 : 2880 : parm_kind = SCALAR_POINTER;
7523 : 2880 : parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
7524 : : }
7525 : : }
7526 : : }
7527 : 59689 : else if (e->ts.type == BT_CLASS
7528 : 2567 : && fsym && fsym->ts.type == BT_CLASS
7529 : 2221 : && (CLASS_DATA (fsym)->attr.dimension
7530 : 51 : || CLASS_DATA (fsym)->attr.codimension))
7531 : : {
7532 : : /* Pass a class array. */
7533 : 2221 : parmse.use_offset = 1;
7534 : 2221 : gfc_conv_expr_descriptor (&parmse, e);
7535 : 2221 : bool defer_to_dealloc_blk = false;
7536 : :
7537 : 2221 : if (fsym->attr.optional
7538 : 798 : && e->expr_type == EXPR_VARIABLE
7539 : 798 : && e->symtree->n.sym->attr.optional)
7540 : : {
7541 : 438 : stmtblock_t block;
7542 : :
7543 : 438 : gfc_init_block (&block);
7544 : 438 : gfc_add_block_to_block (&block, &parmse.pre);
7545 : :
7546 : 876 : tree t = fold_build3_loc (input_location, COND_EXPR,
7547 : : void_type_node,
7548 : 438 : gfc_conv_expr_present (e->symtree->n.sym),
7549 : : gfc_finish_block (&block),
7550 : : build_empty_stmt (input_location));
7551 : :
7552 : 438 : gfc_add_expr_to_block (&parmse.pre, t);
7553 : : }
7554 : :
7555 : : /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
7556 : : allocated on entry, it must be deallocated. */
7557 : 2221 : if (fsym->attr.intent == INTENT_OUT
7558 : 141 : && CLASS_DATA (fsym)->attr.allocatable)
7559 : : {
7560 : 110 : stmtblock_t block;
7561 : 110 : tree ptr;
7562 : :
7563 : : /* In case the data reference to deallocate is dependent on
7564 : : its own content, save the resulting pointer to a variable
7565 : : and only use that variable from now on, before the
7566 : : expression becomes invalid. */
7567 : 110 : parmse.expr = gfc_evaluate_data_ref_now (parmse.expr,
7568 : : &parmse.pre);
7569 : :
7570 : 110 : if (parmse.class_container != NULL_TREE)
7571 : 110 : parmse.class_container
7572 : 110 : = gfc_evaluate_data_ref_now (parmse.class_container,
7573 : : &parmse.pre);
7574 : :
7575 : 110 : gfc_init_block (&block);
7576 : 110 : ptr = parmse.expr;
7577 : 110 : ptr = gfc_class_data_get (ptr);
7578 : :
7579 : 110 : tree cls = parmse.class_container;
7580 : 110 : tmp = gfc_deallocate_with_status (ptr, NULL_TREE,
7581 : : NULL_TREE, NULL_TREE,
7582 : : NULL_TREE, true, e,
7583 : : GFC_CAF_COARRAY_NOCOARRAY,
7584 : : cls);
7585 : 110 : gfc_add_expr_to_block (&block, tmp);
7586 : 110 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
7587 : : void_type_node, ptr,
7588 : : null_pointer_node);
7589 : 110 : gfc_add_expr_to_block (&block, tmp);
7590 : 110 : gfc_reset_vptr (&block, e, parmse.class_container);
7591 : :
7592 : 110 : if (fsym->attr.optional
7593 : 30 : && e->expr_type == EXPR_VARIABLE
7594 : 30 : && (!e->ref
7595 : 30 : || (e->ref->type == REF_ARRAY
7596 : 0 : && e->ref->u.ar.type != AR_FULL))
7597 : 0 : && e->symtree->n.sym->attr.optional)
7598 : : {
7599 : 0 : tmp = fold_build3_loc (input_location, COND_EXPR,
7600 : : void_type_node,
7601 : 0 : gfc_conv_expr_present (e->symtree->n.sym),
7602 : : gfc_finish_block (&block),
7603 : : build_empty_stmt (input_location));
7604 : : }
7605 : : else
7606 : 110 : tmp = gfc_finish_block (&block);
7607 : :
7608 : 110 : gfc_add_expr_to_block (&dealloc_blk, tmp);
7609 : 110 : defer_to_dealloc_blk = true;
7610 : : }
7611 : :
7612 : 2221 : gfc_se class_se = parmse;
7613 : 2221 : gfc_init_block (&class_se.pre);
7614 : 2221 : gfc_init_block (&class_se.post);
7615 : :
7616 : : /* The conversion does not repackage the reference to a class
7617 : : array - _data descriptor. */
7618 : 2221 : gfc_conv_class_to_class (&class_se, e, fsym->ts, false,
7619 : 2221 : fsym->attr.intent != INTENT_IN
7620 : 2221 : && (CLASS_DATA (fsym)->attr.class_pointer
7621 : 1187 : || CLASS_DATA (fsym)->attr.allocatable),
7622 : 2221 : fsym->attr.optional
7623 : 798 : && e->expr_type == EXPR_VARIABLE
7624 : 3019 : && e->symtree->n.sym->attr.optional,
7625 : 2221 : CLASS_DATA (fsym)->attr.class_pointer
7626 : 2221 : || CLASS_DATA (fsym)->attr.allocatable);
7627 : :
7628 : 2221 : parmse.expr = class_se.expr;
7629 : 2111 : stmtblock_t *class_pre_block = defer_to_dealloc_blk
7630 : 2221 : ? &dealloc_blk
7631 : : : &parmse.pre;
7632 : 2221 : gfc_add_block_to_block (class_pre_block, &class_se.pre);
7633 : 2221 : gfc_add_block_to_block (&parmse.post, &class_se.post);
7634 : 2221 : }
7635 : : else
7636 : : {
7637 : : /* If the argument is a function call that may not create
7638 : : a temporary for the result, we have to check that we
7639 : : can do it, i.e. that there is no alias between this
7640 : : argument and another one. */
7641 : 57468 : if (gfc_get_noncopying_intrinsic_argument (e) != NULL)
7642 : : {
7643 : 358 : gfc_expr *iarg;
7644 : 358 : sym_intent intent;
7645 : :
7646 : 358 : if (fsym != NULL)
7647 : 349 : intent = fsym->attr.intent;
7648 : : else
7649 : : intent = INTENT_UNKNOWN;
7650 : :
7651 : 358 : if (gfc_check_fncall_dependency (e, intent, sym, args,
7652 : : NOT_ELEMENTAL))
7653 : 21 : parmse.force_tmp = 1;
7654 : :
7655 : 358 : iarg = e->value.function.actual->expr;
7656 : :
7657 : : /* Temporary needed if aliasing due to host association. */
7658 : 358 : if (sym->attr.contained
7659 : 114 : && !sym->attr.pure
7660 : 114 : && !sym->attr.implicit_pure
7661 : 36 : && !sym->attr.use_assoc
7662 : 36 : && iarg->expr_type == EXPR_VARIABLE
7663 : 36 : && sym->ns == iarg->symtree->n.sym->ns)
7664 : 36 : parmse.force_tmp = 1;
7665 : :
7666 : : /* Ditto within module. */
7667 : 358 : if (sym->attr.use_assoc
7668 : 6 : && !sym->attr.pure
7669 : 6 : && !sym->attr.implicit_pure
7670 : 0 : && iarg->expr_type == EXPR_VARIABLE
7671 : 0 : && sym->module == iarg->symtree->n.sym->module)
7672 : 0 : parmse.force_tmp = 1;
7673 : : }
7674 : :
7675 : : /* Special case for assumed-rank arrays: when passing an
7676 : : argument to a nonallocatable/nonpointer dummy, the bounds have
7677 : : to be reset as otherwise a last-dim ubound of -1 is
7678 : : indistinguishable from an assumed-size array in the callee. */
7679 : 57468 : if (!sym->attr.is_bind_c && e && fsym && fsym->as
7680 : 34060 : && fsym->as->type == AS_ASSUMED_RANK
7681 : 11839 : && e->rank != -1
7682 : 11550 : && e->expr_type == EXPR_VARIABLE
7683 : 11109 : && ((fsym->ts.type == BT_CLASS
7684 : 0 : && !CLASS_DATA (fsym)->attr.class_pointer
7685 : 0 : && !CLASS_DATA (fsym)->attr.allocatable)
7686 : 11109 : || (fsym->ts.type != BT_CLASS
7687 : 11109 : && !fsym->attr.pointer && !fsym->attr.allocatable)))
7688 : : {
7689 : : /* Change AR_FULL to a (:,:,:) ref to force bounds update. */
7690 : 10566 : gfc_ref *ref;
7691 : 10812 : for (ref = e->ref; ref->next; ref = ref->next)
7692 : : {
7693 : 318 : if (ref->next->type == REF_INQUIRY)
7694 : : break;
7695 : 270 : if (ref->type == REF_ARRAY
7696 : 24 : && ref->u.ar.type != AR_ELEMENT)
7697 : : break;
7698 : 10566 : };
7699 : 10566 : if (ref->u.ar.type == AR_FULL
7700 : 9840 : && ref->u.ar.as->type != AS_ASSUMED_SIZE)
7701 : 9720 : ref->u.ar.type = AR_SECTION;
7702 : : }
7703 : :
7704 : 57468 : if (sym->attr.is_bind_c && e && is_CFI_desc (fsym, NULL))
7705 : : /* Implement F2018, 18.3.6, list item (5), bullet point 2. */
7706 : 5850 : gfc_conv_gfc_desc_to_cfi_desc (&parmse, e, fsym);
7707 : :
7708 : 51618 : else if (e->expr_type == EXPR_VARIABLE
7709 : 39920 : && is_subref_array (e)
7710 : 52346 : && !(fsym && fsym->attr.pointer))
7711 : : /* The actual argument is a component reference to an
7712 : : array of derived types. In this case, the argument
7713 : : is converted to a temporary, which is passed and then
7714 : : written back after the procedure call. */
7715 : 523 : gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
7716 : 481 : fsym ? fsym->attr.intent : INTENT_INOUT,
7717 : 523 : fsym && fsym->attr.pointer);
7718 : :
7719 : 51095 : else if (e->ts.type == BT_CLASS && CLASS_DATA (e)->as
7720 : 345 : && CLASS_DATA (e)->as->type == AS_ASSUMED_SIZE
7721 : 18 : && nodesc_arg && fsym->ts.type == BT_DERIVED)
7722 : : /* An assumed size class actual argument being passed to
7723 : : a 'no descriptor' formal argument just requires the
7724 : : data pointer to be passed. For class dummy arguments
7725 : : this is stored in the symbol backend decl.. */
7726 : 6 : parmse.expr = e->symtree->n.sym->backend_decl;
7727 : :
7728 : 51089 : else if (gfc_is_class_array_ref (e, NULL)
7729 : 51089 : && fsym && fsym->ts.type == BT_DERIVED)
7730 : : /* The actual argument is a component reference to an
7731 : : array of derived types. In this case, the argument
7732 : : is converted to a temporary, which is passed and then
7733 : : written back after the procedure call.
7734 : : OOP-TODO: Insert code so that if the dynamic type is
7735 : : the same as the declared type, copy-in/copy-out does
7736 : : not occur. */
7737 : 108 : gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
7738 : 108 : fsym->attr.intent,
7739 : 108 : fsym->attr.pointer);
7740 : :
7741 : 50981 : else if (gfc_is_class_array_function (e)
7742 : 50981 : && fsym && fsym->ts.type == BT_DERIVED)
7743 : : /* See previous comment. For function actual argument,
7744 : : the write out is not needed so the intent is set as
7745 : : intent in. */
7746 : : {
7747 : 13 : e->must_finalize = 1;
7748 : 13 : gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
7749 : 13 : INTENT_IN, fsym->attr.pointer);
7750 : : }
7751 : 47387 : else if (fsym && fsym->attr.contiguous
7752 : 60 : && (fsym->attr.target
7753 : 1674 : ? gfc_is_not_contiguous (e)
7754 : 1614 : : !gfc_is_simply_contiguous (e, false, true))
7755 : 52957 : && gfc_expr_is_variable (e))
7756 : : {
7757 : 303 : gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
7758 : 303 : fsym->attr.intent,
7759 : 303 : fsym->attr.pointer);
7760 : : }
7761 : : else
7762 : : /* This is where we introduce a temporary to store the
7763 : : result of a non-lvalue array expression. */
7764 : 50665 : gfc_conv_array_parameter (&parmse, e, nodesc_arg, fsym,
7765 : : sym->name, NULL);
7766 : :
7767 : : /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
7768 : : allocated on entry, it must be deallocated.
7769 : : CFI descriptors are handled elsewhere. */
7770 : 53845 : if (fsym && fsym->attr.allocatable
7771 : 1717 : && fsym->attr.intent == INTENT_OUT
7772 : 57231 : && !is_CFI_desc (fsym, NULL))
7773 : : {
7774 : 145 : if (fsym->ts.type == BT_DERIVED
7775 : 45 : && fsym->ts.u.derived->attr.alloc_comp)
7776 : : {
7777 : : // deallocate the components first
7778 : 9 : tmp = gfc_deallocate_alloc_comp (fsym->ts.u.derived,
7779 : : parmse.expr, e->rank);
7780 : : /* But check whether dummy argument is optional. */
7781 : 9 : if (tmp != NULL_TREE
7782 : 9 : && fsym->attr.optional
7783 : 6 : && e->expr_type == EXPR_VARIABLE
7784 : 6 : && e->symtree->n.sym->attr.optional)
7785 : : {
7786 : 6 : tree present;
7787 : 6 : present = gfc_conv_expr_present (e->symtree->n.sym);
7788 : 6 : tmp = build3_v (COND_EXPR, present, tmp,
7789 : : build_empty_stmt (input_location));
7790 : : }
7791 : 9 : if (tmp != NULL_TREE)
7792 : 9 : gfc_add_expr_to_block (&dealloc_blk, tmp);
7793 : : }
7794 : :
7795 : 145 : tmp = parmse.expr;
7796 : : /* With bind(C), the actual argument is replaced by a bind-C
7797 : : descriptor; in this case, the data component arrives here,
7798 : : which shall not be dereferenced, but still freed and
7799 : : nullified. */
7800 : 145 : if (TREE_TYPE(tmp) != pvoid_type_node)
7801 : 145 : tmp = build_fold_indirect_ref_loc (input_location,
7802 : : parmse.expr);
7803 : 145 : tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
7804 : : NULL_TREE, NULL_TREE, true,
7805 : : e,
7806 : : GFC_CAF_COARRAY_NOCOARRAY);
7807 : 145 : if (fsym->attr.optional
7808 : 48 : && e->expr_type == EXPR_VARIABLE
7809 : 48 : && e->symtree->n.sym->attr.optional)
7810 : 48 : tmp = fold_build3_loc (input_location, COND_EXPR,
7811 : : void_type_node,
7812 : 24 : gfc_conv_expr_present (e->symtree->n.sym),
7813 : : tmp, build_empty_stmt (input_location));
7814 : 145 : gfc_add_expr_to_block (&dealloc_blk, tmp);
7815 : : }
7816 : : }
7817 : : }
7818 : : /* Special case for an assumed-rank dummy argument. */
7819 : 266294 : if (!sym->attr.is_bind_c && e && fsym && e->rank > 0
7820 : 55557 : && (fsym->ts.type == BT_CLASS
7821 : 55557 : ? (CLASS_DATA (fsym)->as
7822 : 4177 : && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
7823 : 51380 : : (fsym->as && fsym->as->type == AS_ASSUMED_RANK)))
7824 : : {
7825 : 12689 : if (fsym->ts.type == BT_CLASS
7826 : 12689 : ? (CLASS_DATA (fsym)->attr.class_pointer
7827 : 1055 : || CLASS_DATA (fsym)->attr.allocatable)
7828 : 11634 : : (fsym->attr.pointer || fsym->attr.allocatable))
7829 : : {
7830 : : /* Unallocated allocatable arrays and unassociated pointer
7831 : : arrays need their dtype setting if they are argument
7832 : : associated with assumed rank dummies to set the rank. */
7833 : 891 : set_dtype_for_unallocated (&parmse, e);
7834 : : }
7835 : 11798 : else if (e->expr_type == EXPR_VARIABLE
7836 : 11319 : && e->symtree->n.sym->attr.dummy
7837 : 698 : && (e->ts.type == BT_CLASS
7838 : 891 : ? (e->ref && e->ref->next
7839 : 193 : && e->ref->next->type == REF_ARRAY
7840 : 193 : && e->ref->next->u.ar.type == AR_FULL
7841 : 386 : && e->ref->next->u.ar.as->type == AS_ASSUMED_SIZE)
7842 : 505 : : (e->ref && e->ref->type == REF_ARRAY
7843 : 505 : && e->ref->u.ar.type == AR_FULL
7844 : 733 : && e->ref->u.ar.as->type == AS_ASSUMED_SIZE)))
7845 : : {
7846 : : /* Assumed-size actual to assumed-rank dummy requires
7847 : : dim[rank-1].ubound = -1. */
7848 : 180 : tree minus_one;
7849 : 180 : tmp = build_fold_indirect_ref_loc (input_location, parmse.expr);
7850 : 180 : if (fsym->ts.type == BT_CLASS)
7851 : 60 : tmp = gfc_class_data_get (tmp);
7852 : 180 : minus_one = build_int_cst (gfc_array_index_type, -1);
7853 : 180 : gfc_conv_descriptor_ubound_set (&parmse.pre, tmp,
7854 : 180 : gfc_rank_cst[e->rank - 1],
7855 : : minus_one);
7856 : : }
7857 : : }
7858 : :
7859 : : /* The case with fsym->attr.optional is that of a user subroutine
7860 : : with an interface indicating an optional argument. When we call
7861 : : an intrinsic subroutine, however, fsym is NULL, but we might still
7862 : : have an optional argument, so we proceed to the substitution
7863 : : just in case. Arguments passed to bind(c) procedures via CFI
7864 : : descriptors are handled elsewhere. */
7865 : 252879 : if (e && (fsym == NULL || fsym->attr.optional)
7866 : 326649 : && !(sym->attr.is_bind_c && is_CFI_desc (fsym, NULL)))
7867 : : {
7868 : : /* If an optional argument is itself an optional dummy argument,
7869 : : check its presence and substitute a null if absent. This is
7870 : : only needed when passing an array to an elemental procedure
7871 : : as then array elements are accessed - or no NULL pointer is
7872 : : allowed and a "1" or "0" should be passed if not present.
7873 : : When passing a non-array-descriptor full array to a
7874 : : non-array-descriptor dummy, no check is needed. For
7875 : : array-descriptor actual to array-descriptor dummy, see
7876 : : PR 41911 for why a check has to be inserted.
7877 : : fsym == NULL is checked as intrinsics required the descriptor
7878 : : but do not always set fsym.
7879 : : Also, it is necessary to pass a NULL pointer to library routines
7880 : : which usually ignore optional arguments, so they can handle
7881 : : these themselves. */
7882 : 59261 : if (e->expr_type == EXPR_VARIABLE
7883 : 26419 : && e->symtree->n.sym->attr.optional
7884 : 2414 : && (((e->rank != 0 && elemental_proc)
7885 : 2239 : || e->representation.length || e->ts.type == BT_CHARACTER
7886 : 2013 : || (e->rank == 0 && e->symtree->n.sym->attr.value)
7887 : 1903 : || (e->rank != 0
7888 : 1069 : && (fsym == NULL
7889 : 1033 : || (fsym->as
7890 : 271 : && (fsym->as->type == AS_ASSUMED_SHAPE
7891 : 235 : || fsym->as->type == AS_ASSUMED_RANK
7892 : 117 : || fsym->as->type == AS_DEFERRED)))))
7893 : 1679 : || se->ignore_optional))
7894 : 763 : gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
7895 : 763 : e->representation.length);
7896 : : }
7897 : :
7898 : : /* Make the class container for the first argument available with class
7899 : : valued transformational functions. */
7900 : 266294 : if (argc == 0 && e && e->ts.type == BT_CLASS
7901 : 4800 : && isym && isym->transformational
7902 : 84 : && se->ss && se->ss->info)
7903 : : {
7904 : 84 : arg1_cntnr = parmse.expr;
7905 : 84 : if (POINTER_TYPE_P (TREE_TYPE (arg1_cntnr)))
7906 : 84 : arg1_cntnr = build_fold_indirect_ref_loc (input_location, arg1_cntnr);
7907 : 84 : arg1_cntnr = gfc_get_class_from_expr (arg1_cntnr);
7908 : 84 : se->ss->info->class_container = arg1_cntnr;
7909 : : }
7910 : :
7911 : 266294 : if (fsym && e)
7912 : : {
7913 : : /* Obtain the character length of an assumed character length
7914 : : length procedure from the typespec. */
7915 : 220988 : if (fsym->ts.type == BT_CHARACTER
7916 : 31476 : && parmse.string_length == NULL_TREE
7917 : 3508 : && e->ts.type == BT_PROCEDURE
7918 : 15 : && e->symtree->n.sym->ts.type == BT_CHARACTER
7919 : 15 : && e->symtree->n.sym->ts.u.cl->length != NULL
7920 : 15 : && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7921 : : {
7922 : 8 : gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
7923 : 8 : parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
7924 : : }
7925 : :
7926 : : /* Obtain the character length for a NULL() actual with a character
7927 : : MOLD argument. Otherwise substitute a suitable dummy length.
7928 : : Here we handle non-optional dummies of non-bind(c) procedures. */
7929 : 220988 : if (e->expr_type == EXPR_NULL
7930 : 745 : && fsym->ts.type == BT_CHARACTER
7931 : 296 : && !fsym->attr.optional
7932 : 221206 : && !(sym->attr.is_bind_c && is_CFI_desc (fsym, NULL)))
7933 : 216 : conv_null_actual (&parmse, e, fsym);
7934 : : }
7935 : :
7936 : : /* If any actual argument of the procedure is allocatable and passed
7937 : : to an allocatable dummy with INTENT(OUT), we conservatively
7938 : : evaluate actual argument expressions before deallocations are
7939 : : performed and the procedure is executed. May create temporaries.
7940 : : This ensures we conform to F2023:15.5.3, 15.5.4. */
7941 : 252879 : if (e && fsym && force_eval_args
7942 : 1078 : && fsym->attr.intent != INTENT_OUT
7943 : 266691 : && !gfc_is_constant_expr (e))
7944 : 256 : parmse.expr = gfc_evaluate_now (parmse.expr, &parmse.pre);
7945 : :
7946 : 266294 : if (fsym && need_interface_mapping && e)
7947 : 40583 : gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
7948 : :
7949 : 266294 : gfc_add_block_to_block (&se->pre, &parmse.pre);
7950 : 266294 : gfc_add_block_to_block (&post, &parmse.post);
7951 : 266294 : gfc_add_block_to_block (&se->finalblock, &parmse.finalblock);
7952 : :
7953 : : /* Allocated allocatable components of derived types must be
7954 : : deallocated for non-variable scalars, array arguments to elemental
7955 : : procedures, and array arguments with descriptor to non-elemental
7956 : : procedures. As bounds information for descriptorless arrays is no
7957 : : longer available here, they are dealt with in trans-array.cc
7958 : : (gfc_conv_array_parameter). */
7959 : 252879 : if (e && (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS)
7960 : 26930 : && e->ts.u.derived->attr.alloc_comp
7961 : 7399 : && (e->rank == 0 || elemental_proc || !nodesc_arg)
7962 : 273575 : && !expr_may_alias_variables (e, elemental_proc))
7963 : : {
7964 : 328 : int parm_rank;
7965 : : /* It is known the e returns a structure type with at least one
7966 : : allocatable component. When e is a function, ensure that the
7967 : : function is called once only by using a temporary variable. */
7968 : 328 : if (!DECL_P (parmse.expr) && e->expr_type == EXPR_FUNCTION)
7969 : 139 : parmse.expr = gfc_evaluate_now_loc (input_location,
7970 : : parmse.expr, &se->pre);
7971 : :
7972 : 328 : if ((fsym && fsym->attr.value) || e->expr_type == EXPR_ARRAY)
7973 : 116 : tmp = parmse.expr;
7974 : : else
7975 : 212 : tmp = build_fold_indirect_ref_loc (input_location,
7976 : : parmse.expr);
7977 : :
7978 : 328 : parm_rank = e->rank;
7979 : 328 : switch (parm_kind)
7980 : : {
7981 : : case (ELEMENTAL):
7982 : : case (SCALAR):
7983 : 328 : parm_rank = 0;
7984 : : break;
7985 : :
7986 : 0 : case (SCALAR_POINTER):
7987 : 0 : tmp = build_fold_indirect_ref_loc (input_location,
7988 : : tmp);
7989 : 0 : break;
7990 : : }
7991 : :
7992 : 328 : if (e->ts.type == BT_DERIVED && fsym && fsym->ts.type == BT_CLASS)
7993 : : {
7994 : : /* The derived type is passed to gfc_deallocate_alloc_comp.
7995 : : Therefore, class actuals can be handled correctly but derived
7996 : : types passed to class formals need the _data component. */
7997 : 81 : tmp = gfc_class_data_get (tmp);
7998 : 81 : if (!CLASS_DATA (fsym)->attr.dimension)
7999 : : {
8000 : 55 : if (UNLIMITED_POLY (fsym))
8001 : : {
8002 : 12 : tree type = gfc_typenode_for_spec (&e->ts);
8003 : 12 : type = build_pointer_type (type);
8004 : 12 : tmp = fold_convert (type, tmp);
8005 : : }
8006 : 55 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
8007 : : }
8008 : : }
8009 : :
8010 : 328 : if (e->expr_type == EXPR_OP
8011 : 24 : && e->value.op.op == INTRINSIC_PARENTHESES
8012 : 24 : && e->value.op.op1->expr_type == EXPR_VARIABLE)
8013 : : {
8014 : 24 : tree local_tmp;
8015 : 24 : local_tmp = gfc_evaluate_now (tmp, &se->pre);
8016 : 24 : local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp,
8017 : : parm_rank, 0);
8018 : 24 : gfc_add_expr_to_block (&se->post, local_tmp);
8019 : : }
8020 : :
8021 : : /* Items of array expressions passed to a polymorphic formal arguments
8022 : : create their own clean up, so prevent double free. */
8023 : 328 : if (!finalized && !e->must_finalize
8024 : 327 : && !(e->expr_type == EXPR_ARRAY && fsym
8025 : 50 : && fsym->ts.type == BT_CLASS))
8026 : : {
8027 : 307 : bool scalar_res_outside_loop;
8028 : 909 : scalar_res_outside_loop = e->expr_type == EXPR_FUNCTION
8029 : 150 : && parm_rank == 0
8030 : 445 : && parmse.loop;
8031 : :
8032 : : /* Scalars passed to an assumed rank argument are converted to
8033 : : a descriptor. Obtain the data field before deallocating any
8034 : : allocatable components. */
8035 : 278 : if (parm_rank == 0 && e->expr_type != EXPR_ARRAY
8036 : 560 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
8037 : 19 : tmp = gfc_conv_descriptor_data_get (tmp);
8038 : :
8039 : 307 : if (scalar_res_outside_loop)
8040 : : {
8041 : : /* Go through the ss chain to find the argument and use
8042 : : the stored value. */
8043 : 30 : gfc_ss *tmp_ss = parmse.loop->ss;
8044 : 72 : for (; tmp_ss; tmp_ss = tmp_ss->next)
8045 : 60 : if (tmp_ss->info
8046 : 48 : && tmp_ss->info->expr == e
8047 : 18 : && tmp_ss->info->data.scalar.value != NULL_TREE)
8048 : : {
8049 : 18 : tmp = tmp_ss->info->data.scalar.value;
8050 : 18 : break;
8051 : : }
8052 : : }
8053 : :
8054 : 307 : STRIP_NOPS (tmp);
8055 : :
8056 : 307 : if (derived_array != NULL_TREE)
8057 : 0 : tmp = gfc_deallocate_alloc_comp (e->ts.u.derived,
8058 : : derived_array,
8059 : : parm_rank);
8060 : 307 : else if ((e->ts.type == BT_CLASS
8061 : 24 : && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
8062 : 307 : || e->ts.type == BT_DERIVED)
8063 : 307 : tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp,
8064 : : parm_rank);
8065 : 0 : else if (e->ts.type == BT_CLASS)
8066 : 0 : tmp = gfc_deallocate_alloc_comp (CLASS_DATA (e)->ts.u.derived,
8067 : : tmp, parm_rank);
8068 : :
8069 : 307 : if (scalar_res_outside_loop)
8070 : 30 : gfc_add_expr_to_block (&parmse.loop->post, tmp);
8071 : : else
8072 : 277 : gfc_prepend_expr_to_block (&post, tmp);
8073 : : }
8074 : : }
8075 : :
8076 : : /* Add argument checking of passing an unallocated/NULL actual to
8077 : : a nonallocatable/nonpointer dummy. */
8078 : :
8079 : 266294 : if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
8080 : : {
8081 : 6534 : symbol_attribute attr;
8082 : 6534 : char *msg;
8083 : 6534 : tree cond;
8084 : 6534 : tree tmp;
8085 : 6534 : symbol_attribute fsym_attr;
8086 : :
8087 : 6534 : if (fsym)
8088 : : {
8089 : 6373 : if (fsym->ts.type == BT_CLASS)
8090 : : {
8091 : 321 : fsym_attr = CLASS_DATA (fsym)->attr;
8092 : 321 : fsym_attr.pointer = fsym_attr.class_pointer;
8093 : : }
8094 : : else
8095 : 6052 : fsym_attr = fsym->attr;
8096 : : }
8097 : :
8098 : 6534 : if (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
8099 : 4085 : attr = gfc_expr_attr (e);
8100 : : else
8101 : 6070 : goto end_pointer_check;
8102 : :
8103 : : /* In Fortran 2008 it's allowed to pass a NULL pointer/nonallocated
8104 : : allocatable to an optional dummy, cf. 12.5.2.12. */
8105 : 4085 : if (fsym != NULL && fsym->attr.optional && !attr.proc_pointer
8106 : 1038 : && (gfc_option.allow_std & GFC_STD_F2008) != 0)
8107 : 1032 : goto end_pointer_check;
8108 : :
8109 : 3053 : if (attr.optional)
8110 : : {
8111 : : /* If the actual argument is an optional pointer/allocatable and
8112 : : the formal argument takes an nonpointer optional value,
8113 : : it is invalid to pass a non-present argument on, even
8114 : : though there is no technical reason for this in gfortran.
8115 : : See Fortran 2003, Section 12.4.1.6 item (7)+(8). */
8116 : 60 : tree present, null_ptr, type;
8117 : :
8118 : 60 : if (attr.allocatable
8119 : 0 : && (fsym == NULL || !fsym_attr.allocatable))
8120 : 0 : msg = xasprintf ("Allocatable actual argument '%s' is not "
8121 : : "allocated or not present",
8122 : 0 : e->symtree->n.sym->name);
8123 : 60 : else if (attr.pointer
8124 : 12 : && (fsym == NULL || !fsym_attr.pointer))
8125 : 12 : msg = xasprintf ("Pointer actual argument '%s' is not "
8126 : : "associated or not present",
8127 : 12 : e->symtree->n.sym->name);
8128 : 48 : else if (attr.proc_pointer && !e->value.function.actual
8129 : 0 : && (fsym == NULL || !fsym_attr.proc_pointer))
8130 : 0 : msg = xasprintf ("Proc-pointer actual argument '%s' is not "
8131 : : "associated or not present",
8132 : 0 : e->symtree->n.sym->name);
8133 : : else
8134 : 48 : goto end_pointer_check;
8135 : :
8136 : 12 : present = gfc_conv_expr_present (e->symtree->n.sym);
8137 : 12 : type = TREE_TYPE (present);
8138 : 12 : present = fold_build2_loc (input_location, EQ_EXPR,
8139 : : logical_type_node, present,
8140 : : fold_convert (type,
8141 : : null_pointer_node));
8142 : 12 : type = TREE_TYPE (parmse.expr);
8143 : 12 : null_ptr = fold_build2_loc (input_location, EQ_EXPR,
8144 : : logical_type_node, parmse.expr,
8145 : : fold_convert (type,
8146 : : null_pointer_node));
8147 : 12 : cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
8148 : : logical_type_node, present, null_ptr);
8149 : : }
8150 : : else
8151 : : {
8152 : 2993 : if (attr.allocatable
8153 : 256 : && (fsym == NULL || !fsym_attr.allocatable))
8154 : 190 : msg = xasprintf ("Allocatable actual argument '%s' is not "
8155 : 190 : "allocated", e->symtree->n.sym->name);
8156 : 2803 : else if (attr.pointer
8157 : 265 : && (fsym == NULL || !fsym_attr.pointer))
8158 : 184 : msg = xasprintf ("Pointer actual argument '%s' is not "
8159 : 184 : "associated", e->symtree->n.sym->name);
8160 : 2619 : else if (attr.proc_pointer && !e->value.function.actual
8161 : 78 : && (fsym == NULL || !fsym_attr.proc_pointer))
8162 : 78 : msg = xasprintf ("Proc-pointer actual argument '%s' is not "
8163 : 78 : "associated", e->symtree->n.sym->name);
8164 : : else
8165 : 2541 : goto end_pointer_check;
8166 : :
8167 : 452 : tmp = parmse.expr;
8168 : 452 : if (fsym && fsym->ts.type == BT_CLASS && !attr.proc_pointer)
8169 : : {
8170 : 76 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
8171 : 70 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
8172 : 76 : tmp = gfc_class_data_get (tmp);
8173 : 76 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
8174 : 3 : tmp = gfc_conv_descriptor_data_get (tmp);
8175 : : }
8176 : :
8177 : : /* If the argument is passed by value, we need to strip the
8178 : : INDIRECT_REF. */
8179 : 452 : if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
8180 : 12 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
8181 : :
8182 : 452 : cond = fold_build2_loc (input_location, EQ_EXPR,
8183 : : logical_type_node, tmp,
8184 : 452 : fold_convert (TREE_TYPE (tmp),
8185 : : null_pointer_node));
8186 : : }
8187 : :
8188 : 464 : gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
8189 : : msg);
8190 : 464 : free (msg);
8191 : : }
8192 : 259760 : end_pointer_check:
8193 : :
8194 : : /* Deferred length dummies pass the character length by reference
8195 : : so that the value can be returned. */
8196 : 266294 : if (parmse.string_length && fsym && fsym->ts.deferred)
8197 : : {
8198 : 781 : if (INDIRECT_REF_P (parmse.string_length))
8199 : : {
8200 : : /* In chains of functions/procedure calls the string_length already
8201 : : is a pointer to the variable holding the length. Therefore
8202 : : remove the deref on call. */
8203 : 90 : tmp = parmse.string_length;
8204 : 90 : parmse.string_length = TREE_OPERAND (parmse.string_length, 0);
8205 : : }
8206 : : else
8207 : : {
8208 : 691 : tmp = parmse.string_length;
8209 : 691 : if (!VAR_P (tmp) && TREE_CODE (tmp) != COMPONENT_REF)
8210 : 61 : tmp = gfc_evaluate_now (parmse.string_length, &se->pre);
8211 : 691 : parmse.string_length = gfc_build_addr_expr (NULL_TREE, tmp);
8212 : : }
8213 : :
8214 : 781 : if (e && e->expr_type == EXPR_VARIABLE
8215 : 624 : && fsym->attr.allocatable
8216 : 360 : && e->ts.u.cl->backend_decl
8217 : 360 : && VAR_P (e->ts.u.cl->backend_decl))
8218 : : {
8219 : 276 : if (INDIRECT_REF_P (tmp))
8220 : 0 : tmp = TREE_OPERAND (tmp, 0);
8221 : 276 : gfc_add_modify (&se->post, e->ts.u.cl->backend_decl,
8222 : : fold_convert (gfc_charlen_type_node, tmp));
8223 : : }
8224 : : }
8225 : :
8226 : : /* Character strings are passed as two parameters, a length and a
8227 : : pointer - except for Bind(c) and c_ptrs which only passe the pointer.
8228 : : An unlimited polymorphic formal argument likewise does not
8229 : : need the length. */
8230 : 266294 : if (parmse.string_length != NULL_TREE
8231 : 36672 : && !sym->attr.is_bind_c
8232 : 35976 : && !(fsym && fsym->ts.type == BT_DERIVED && fsym->ts.u.derived
8233 : 6 : && fsym->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
8234 : 6 : && fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING )
8235 : 30087 : && !(fsym && fsym->ts.type == BT_ASSUMED)
8236 : 29978 : && !(fsym && UNLIMITED_POLY (fsym)))
8237 : 35686 : vec_safe_push (stringargs, parmse.string_length);
8238 : :
8239 : : /* When calling __copy for character expressions to unlimited
8240 : : polymorphic entities, the dst argument needs a string length. */
8241 : 52359 : if (sym->name[0] == '_' && e && e->ts.type == BT_CHARACTER
8242 : 5335 : && startswith (sym->name, "__vtab_CHARACTER")
8243 : 0 : && arg->next && arg->next->expr
8244 : 0 : && (arg->next->expr->ts.type == BT_DERIVED
8245 : 0 : || arg->next->expr->ts.type == BT_CLASS)
8246 : 266294 : && arg->next->expr->ts.u.derived->attr.unlimited_polymorphic)
8247 : 0 : vec_safe_push (stringargs, parmse.string_length);
8248 : :
8249 : : /* For descriptorless coarrays and assumed-shape coarray dummies, we
8250 : : pass the token and the offset as additional arguments. */
8251 : 266294 : if (fsym && e == NULL && flag_coarray == GFC_FCOARRAY_LIB
8252 : 74 : && attr->codimension && !attr->allocatable)
8253 : : {
8254 : : /* Token and offset. */
8255 : 5 : vec_safe_push (stringargs, null_pointer_node);
8256 : 5 : vec_safe_push (stringargs, build_int_cst (gfc_array_index_type, 0));
8257 : 5 : gcc_assert (fsym->attr.optional);
8258 : : }
8259 : 233340 : else if (fsym && flag_coarray == GFC_FCOARRAY_LIB && attr->codimension
8260 : 93 : && !attr->allocatable)
8261 : : {
8262 : 78 : tree caf_decl, caf_type, caf_desc = NULL_TREE;
8263 : 78 : tree offset, tmp2;
8264 : :
8265 : 78 : caf_decl = gfc_get_tree_for_caf_expr (e);
8266 : 78 : caf_type = TREE_TYPE (caf_decl);
8267 : 78 : if (POINTER_TYPE_P (caf_type)
8268 : 78 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_type)))
8269 : 2 : caf_desc = TREE_TYPE (caf_type);
8270 : 76 : else if (GFC_DESCRIPTOR_TYPE_P (caf_type))
8271 : : caf_desc = caf_type;
8272 : :
8273 : 32 : if (caf_desc
8274 : 32 : && (GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_ALLOCATABLE
8275 : 0 : || GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_POINTER))
8276 : : {
8277 : 64 : tmp = POINTER_TYPE_P (TREE_TYPE (caf_decl))
8278 : 34 : ? build_fold_indirect_ref (caf_decl)
8279 : : : caf_decl;
8280 : 32 : tmp = gfc_conv_descriptor_token (tmp);
8281 : : }
8282 : 46 : else if (DECL_LANG_SPECIFIC (caf_decl)
8283 : 46 : && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
8284 : 9 : tmp = GFC_DECL_TOKEN (caf_decl);
8285 : : else
8286 : : {
8287 : 37 : gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
8288 : : && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
8289 : 37 : tmp = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
8290 : : }
8291 : :
8292 : 78 : vec_safe_push (stringargs, tmp);
8293 : :
8294 : 78 : if (caf_desc
8295 : 78 : && GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_ALLOCATABLE)
8296 : 32 : offset = build_int_cst (gfc_array_index_type, 0);
8297 : 46 : else if (DECL_LANG_SPECIFIC (caf_decl)
8298 : 46 : && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
8299 : 9 : offset = GFC_DECL_CAF_OFFSET (caf_decl);
8300 : 37 : else if (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) != NULL_TREE)
8301 : 0 : offset = GFC_TYPE_ARRAY_CAF_OFFSET (caf_type);
8302 : : else
8303 : 37 : offset = build_int_cst (gfc_array_index_type, 0);
8304 : :
8305 : 78 : if (caf_desc)
8306 : : {
8307 : 64 : tmp = POINTER_TYPE_P (TREE_TYPE (caf_decl))
8308 : 34 : ? build_fold_indirect_ref (caf_decl)
8309 : : : caf_decl;
8310 : 32 : tmp = gfc_conv_descriptor_data_get (tmp);
8311 : : }
8312 : : else
8313 : : {
8314 : 46 : gcc_assert (POINTER_TYPE_P (caf_type));
8315 : 46 : tmp = caf_decl;
8316 : : }
8317 : :
8318 : 69 : tmp2 = fsym->ts.type == BT_CLASS
8319 : 78 : ? gfc_class_data_get (parmse.expr) : parmse.expr;
8320 : 78 : if ((fsym->ts.type != BT_CLASS
8321 : 69 : && (fsym->as->type == AS_ASSUMED_SHAPE
8322 : 40 : || fsym->as->type == AS_ASSUMED_RANK))
8323 : 49 : || (fsym->ts.type == BT_CLASS
8324 : 9 : && (CLASS_DATA (fsym)->as->type == AS_ASSUMED_SHAPE
8325 : 6 : || CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)))
8326 : : {
8327 : 32 : if (fsym->ts.type == BT_CLASS)
8328 : 3 : gcc_assert (!POINTER_TYPE_P (TREE_TYPE (tmp2)));
8329 : : else
8330 : : {
8331 : 29 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
8332 : 29 : tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
8333 : : }
8334 : 32 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)));
8335 : 32 : tmp2 = gfc_conv_descriptor_data_get (tmp2);
8336 : : }
8337 : 46 : else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
8338 : 6 : tmp2 = gfc_conv_descriptor_data_get (tmp2);
8339 : : else
8340 : : {
8341 : 40 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
8342 : : }
8343 : :
8344 : 78 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
8345 : : gfc_array_index_type,
8346 : : fold_convert (gfc_array_index_type, tmp2),
8347 : : fold_convert (gfc_array_index_type, tmp));
8348 : 78 : offset = fold_build2_loc (input_location, PLUS_EXPR,
8349 : : gfc_array_index_type, offset, tmp);
8350 : :
8351 : 78 : vec_safe_push (stringargs, offset);
8352 : : }
8353 : :
8354 : 266294 : vec_safe_push (arglist, parmse.expr);
8355 : : }
8356 : :
8357 : 126920 : gfc_add_block_to_block (&se->pre, &dealloc_blk);
8358 : 126920 : gfc_add_block_to_block (&se->pre, &clobbers);
8359 : 126920 : gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
8360 : :
8361 : 126920 : if (comp)
8362 : 1793 : ts = comp->ts;
8363 : 125127 : else if (sym->ts.type == BT_CLASS)
8364 : 836 : ts = CLASS_DATA (sym)->ts;
8365 : : else
8366 : 124291 : ts = sym->ts;
8367 : :
8368 : 126920 : if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
8369 : 186 : se->string_length = build_int_cst (gfc_charlen_type_node, 1);
8370 : 126734 : else if (ts.type == BT_CHARACTER)
8371 : : {
8372 : 4733 : if (ts.u.cl->length == NULL)
8373 : : {
8374 : : /* Assumed character length results are not allowed by C418 of the 2003
8375 : : standard and are trapped in resolve.cc; except in the case of SPREAD
8376 : : (and other intrinsics?) and dummy functions. In the case of SPREAD,
8377 : : we take the character length of the first argument for the result.
8378 : : For dummies, we have to look through the formal argument list for
8379 : : this function and use the character length found there.
8380 : : Likewise, we handle the case of deferred-length character dummy
8381 : : arguments to intrinsics that determine the characteristics of
8382 : : the result, which cannot be deferred-length. */
8383 : 2213 : if (expr->value.function.isym)
8384 : 1701 : ts.deferred = false;
8385 : 2213 : if (ts.deferred)
8386 : 507 : cl.backend_decl = gfc_create_var (gfc_charlen_type_node, "slen");
8387 : 1706 : else if (!sym->attr.dummy)
8388 : 1701 : cl.backend_decl = (*stringargs)[0];
8389 : : else
8390 : : {
8391 : 5 : formal = gfc_sym_get_dummy_args (sym->ns->proc_name);
8392 : 18 : for (; formal; formal = formal->next)
8393 : 8 : if (strcmp (formal->sym->name, sym->name) == 0)
8394 : 5 : cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
8395 : : }
8396 : 2213 : len = cl.backend_decl;
8397 : : }
8398 : : else
8399 : : {
8400 : 2520 : tree tmp;
8401 : :
8402 : : /* Calculate the length of the returned string. */
8403 : 2520 : gfc_init_se (&parmse, NULL);
8404 : 2520 : if (need_interface_mapping)
8405 : 1867 : gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
8406 : : else
8407 : 653 : gfc_conv_expr (&parmse, ts.u.cl->length);
8408 : 2520 : gfc_add_block_to_block (&se->pre, &parmse.pre);
8409 : 2520 : gfc_add_block_to_block (&se->post, &parmse.post);
8410 : 2520 : tmp = parmse.expr;
8411 : : /* TODO: It would be better to have the charlens as
8412 : : gfc_charlen_type_node already when the interface is
8413 : : created instead of converting it here (see PR 84615). */
8414 : 2520 : tmp = fold_build2_loc (input_location, MAX_EXPR,
8415 : : gfc_charlen_type_node,
8416 : : fold_convert (gfc_charlen_type_node, tmp),
8417 : : build_zero_cst (gfc_charlen_type_node));
8418 : 2520 : cl.backend_decl = tmp;
8419 : : }
8420 : :
8421 : : /* Set up a charlen structure for it. */
8422 : 4733 : cl.next = NULL;
8423 : 4733 : cl.length = NULL;
8424 : 4733 : ts.u.cl = &cl;
8425 : :
8426 : 4733 : len = cl.backend_decl;
8427 : : }
8428 : :
8429 : 1793 : byref = (comp && (comp->attr.dimension
8430 : 1724 : || (comp->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)))
8431 : 126920 : || (!comp && gfc_return_by_reference (sym));
8432 : :
8433 : 18533 : if (byref)
8434 : : {
8435 : 18533 : if (se->direct_byref)
8436 : : {
8437 : : /* Sometimes, too much indirection can be applied; e.g. for
8438 : : function_result = array_valued_recursive_function. */
8439 : 7206 : if (TREE_TYPE (TREE_TYPE (se->expr))
8440 : 7206 : && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
8441 : 7224 : && GFC_DESCRIPTOR_TYPE_P
8442 : : (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
8443 : 18 : se->expr = build_fold_indirect_ref_loc (input_location,
8444 : : se->expr);
8445 : :
8446 : : /* If the lhs of an assignment x = f(..) is allocatable and
8447 : : f2003 is allowed, we must do the automatic reallocation.
8448 : : TODO - deal with intrinsics, without using a temporary. */
8449 : 7206 : if (flag_realloc_lhs
8450 : 7131 : && se->ss && se->ss->loop_chain
8451 : 166 : && se->ss->loop_chain->is_alloc_lhs
8452 : 166 : && !expr->value.function.isym
8453 : 166 : && sym->result->as != NULL)
8454 : : {
8455 : : /* Evaluate the bounds of the result, if known. */
8456 : 166 : gfc_set_loop_bounds_from_array_spec (&mapping, se,
8457 : : sym->result->as);
8458 : :
8459 : : /* Perform the automatic reallocation. */
8460 : 166 : tmp = gfc_alloc_allocatable_for_assignment (se->loop,
8461 : : expr, NULL);
8462 : 166 : gfc_add_expr_to_block (&se->pre, tmp);
8463 : :
8464 : : /* Pass the temporary as the first argument. */
8465 : 166 : result = info->descriptor;
8466 : : }
8467 : : else
8468 : 7040 : result = build_fold_indirect_ref_loc (input_location,
8469 : : se->expr);
8470 : 7206 : vec_safe_push (retargs, se->expr);
8471 : : }
8472 : 11327 : else if (comp && comp->attr.dimension)
8473 : : {
8474 : 66 : gcc_assert (se->loop && info);
8475 : :
8476 : : /* Set the type of the array. vtable charlens are not always reliable.
8477 : : Use the interface, if possible. */
8478 : 66 : if (comp->ts.type == BT_CHARACTER
8479 : 1 : && expr->symtree->n.sym->ts.type == BT_CLASS
8480 : 1 : && comp->ts.interface && comp->ts.interface->result)
8481 : 1 : tmp = gfc_typenode_for_spec (&comp->ts.interface->result->ts);
8482 : : else
8483 : 65 : tmp = gfc_typenode_for_spec (&comp->ts);
8484 : 66 : gcc_assert (se->ss->dimen == se->loop->dimen);
8485 : :
8486 : : /* Evaluate the bounds of the result, if known. */
8487 : 66 : gfc_set_loop_bounds_from_array_spec (&mapping, se, comp->as);
8488 : :
8489 : : /* If the lhs of an assignment x = f(..) is allocatable and
8490 : : f2003 is allowed, we must not generate the function call
8491 : : here but should just send back the results of the mapping.
8492 : : This is signalled by the function ss being flagged. */
8493 : 66 : if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
8494 : : {
8495 : 0 : gfc_free_interface_mapping (&mapping);
8496 : 0 : return has_alternate_specifier;
8497 : : }
8498 : :
8499 : : /* Create a temporary to store the result. In case the function
8500 : : returns a pointer, the temporary will be a shallow copy and
8501 : : mustn't be deallocated. */
8502 : 66 : callee_alloc = comp->attr.allocatable || comp->attr.pointer;
8503 : 66 : gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
8504 : : tmp, NULL_TREE, false,
8505 : : !comp->attr.pointer, callee_alloc,
8506 : 66 : &se->ss->info->expr->where);
8507 : :
8508 : : /* Pass the temporary as the first argument. */
8509 : 66 : result = info->descriptor;
8510 : 66 : tmp = gfc_build_addr_expr (NULL_TREE, result);
8511 : 66 : vec_safe_push (retargs, tmp);
8512 : : }
8513 : 11194 : else if (!comp && sym->result->attr.dimension)
8514 : : {
8515 : 8288 : gcc_assert (se->loop && info);
8516 : :
8517 : : /* Set the type of the array. */
8518 : 8288 : tmp = gfc_typenode_for_spec (&ts);
8519 : 8288 : tmp = arg1_cntnr ? TREE_TYPE (arg1_cntnr) : tmp;
8520 : 8288 : gcc_assert (se->ss->dimen == se->loop->dimen);
8521 : :
8522 : : /* Evaluate the bounds of the result, if known. */
8523 : 8288 : gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
8524 : :
8525 : : /* If the lhs of an assignment x = f(..) is allocatable and
8526 : : f2003 is allowed, we must not generate the function call
8527 : : here but should just send back the results of the mapping.
8528 : : This is signalled by the function ss being flagged. */
8529 : 8288 : if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
8530 : : {
8531 : 0 : gfc_free_interface_mapping (&mapping);
8532 : 0 : return has_alternate_specifier;
8533 : : }
8534 : :
8535 : : /* Create a temporary to store the result. In case the function
8536 : : returns a pointer, the temporary will be a shallow copy and
8537 : : mustn't be deallocated. */
8538 : 8288 : callee_alloc = sym->attr.allocatable || sym->attr.pointer;
8539 : 8288 : gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
8540 : : tmp, NULL_TREE, false,
8541 : : !sym->attr.pointer, callee_alloc,
8542 : 8288 : &se->ss->info->expr->where);
8543 : :
8544 : : /* Pass the temporary as the first argument. */
8545 : 8288 : result = info->descriptor;
8546 : 8288 : tmp = gfc_build_addr_expr (NULL_TREE, result);
8547 : 8288 : vec_safe_push (retargs, tmp);
8548 : : }
8549 : 2973 : else if (ts.type == BT_CHARACTER)
8550 : : {
8551 : : /* Pass the string length. */
8552 : 2912 : type = gfc_get_character_type (ts.kind, ts.u.cl);
8553 : 2912 : type = build_pointer_type (type);
8554 : :
8555 : : /* Emit a DECL_EXPR for the VLA type. */
8556 : 2912 : tmp = TREE_TYPE (type);
8557 : 2912 : if (TYPE_SIZE (tmp)
8558 : 2912 : && TREE_CODE (TYPE_SIZE (tmp)) != INTEGER_CST)
8559 : : {
8560 : 1835 : tmp = build_decl (input_location, TYPE_DECL, NULL_TREE, tmp);
8561 : 1835 : DECL_ARTIFICIAL (tmp) = 1;
8562 : 1835 : DECL_IGNORED_P (tmp) = 1;
8563 : 1835 : tmp = fold_build1_loc (input_location, DECL_EXPR,
8564 : 1835 : TREE_TYPE (tmp), tmp);
8565 : 1835 : gfc_add_expr_to_block (&se->pre, tmp);
8566 : : }
8567 : :
8568 : : /* Return an address to a char[0:len-1]* temporary for
8569 : : character pointers. */
8570 : 2912 : if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
8571 : 67 : || (comp && (comp->attr.pointer || comp->attr.allocatable)))
8572 : : {
8573 : 550 : var = gfc_create_var (type, "pstr");
8574 : :
8575 : 550 : if ((!comp && sym->attr.allocatable)
8576 : 21 : || (comp && comp->attr.allocatable))
8577 : : {
8578 : 265 : gfc_add_modify (&se->pre, var,
8579 : 265 : fold_convert (TREE_TYPE (var),
8580 : : null_pointer_node));
8581 : 265 : tmp = gfc_call_free (var);
8582 : 265 : gfc_add_expr_to_block (&se->post, tmp);
8583 : : }
8584 : :
8585 : : /* Provide an address expression for the function arguments. */
8586 : 550 : var = gfc_build_addr_expr (NULL_TREE, var);
8587 : : }
8588 : : else
8589 : 2362 : var = gfc_conv_string_tmp (se, type, len);
8590 : :
8591 : 2912 : vec_safe_push (retargs, var);
8592 : : }
8593 : : else
8594 : : {
8595 : 61 : gcc_assert (flag_f2c && ts.type == BT_COMPLEX);
8596 : :
8597 : 61 : type = gfc_get_complex_type (ts.kind);
8598 : 61 : var = gfc_build_addr_expr (NULL_TREE, gfc_create_var (type, "cmplx"));
8599 : 61 : vec_safe_push (retargs, var);
8600 : : }
8601 : :
8602 : : /* Add the string length to the argument list. */
8603 : 18533 : if (ts.type == BT_CHARACTER && ts.deferred)
8604 : : {
8605 : 507 : tmp = len;
8606 : 507 : if (!VAR_P (tmp))
8607 : 0 : tmp = gfc_evaluate_now (len, &se->pre);
8608 : 507 : TREE_STATIC (tmp) = 1;
8609 : 507 : gfc_add_modify (&se->pre, tmp,
8610 : 507 : build_int_cst (TREE_TYPE (tmp), 0));
8611 : 507 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
8612 : 507 : vec_safe_push (retargs, tmp);
8613 : : }
8614 : 18026 : else if (ts.type == BT_CHARACTER)
8615 : 4226 : vec_safe_push (retargs, len);
8616 : : }
8617 : :
8618 : 126920 : gfc_free_interface_mapping (&mapping);
8619 : :
8620 : : /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS. */
8621 : 236638 : arglen = (vec_safe_length (arglist) + vec_safe_length (optionalargs)
8622 : 152030 : + vec_safe_length (stringargs) + vec_safe_length (append_args));
8623 : 126920 : vec_safe_reserve (retargs, arglen);
8624 : :
8625 : : /* Add the return arguments. */
8626 : 126920 : vec_safe_splice (retargs, arglist);
8627 : :
8628 : : /* Add the hidden present status for optional+value to the arguments. */
8629 : 126920 : vec_safe_splice (retargs, optionalargs);
8630 : :
8631 : : /* Add the hidden string length parameters to the arguments. */
8632 : 126920 : vec_safe_splice (retargs, stringargs);
8633 : :
8634 : : /* We may want to append extra arguments here. This is used e.g. for
8635 : : calls to libgfortran_matmul_??, which need extra information. */
8636 : 126920 : vec_safe_splice (retargs, append_args);
8637 : :
8638 : 126920 : arglist = retargs;
8639 : :
8640 : : /* Generate the actual call. */
8641 : 126920 : is_builtin = false;
8642 : 126920 : if (base_object == NULL_TREE)
8643 : 126840 : conv_function_val (se, &is_builtin, sym, expr, args);
8644 : : else
8645 : 80 : conv_base_obj_fcn_val (se, base_object, expr);
8646 : :
8647 : : /* If there are alternate return labels, function type should be
8648 : : integer. Can't modify the type in place though, since it can be shared
8649 : : with other functions. For dummy arguments, the typing is done to
8650 : : this result, even if it has to be repeated for each call. */
8651 : 126920 : if (has_alternate_specifier
8652 : 126920 : && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
8653 : : {
8654 : 7 : if (!sym->attr.dummy)
8655 : : {
8656 : 0 : TREE_TYPE (sym->backend_decl)
8657 : 0 : = build_function_type (integer_type_node,
8658 : 0 : TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
8659 : 0 : se->expr = gfc_build_addr_expr (NULL_TREE, sym->backend_decl);
8660 : : }
8661 : : else
8662 : 7 : TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
8663 : : }
8664 : :
8665 : 126920 : fntype = TREE_TYPE (TREE_TYPE (se->expr));
8666 : 126920 : se->expr = build_call_vec (TREE_TYPE (fntype), se->expr, arglist);
8667 : :
8668 : 126920 : if (is_builtin)
8669 : 507 : se->expr = update_builtin_function (se->expr, sym);
8670 : :
8671 : : /* Allocatable scalar function results must be freed and nullified
8672 : : after use. This necessitates the creation of a temporary to
8673 : : hold the result to prevent duplicate calls. */
8674 : 126920 : symbol_attribute attr = comp ? comp->attr : sym->attr;
8675 : 126920 : bool allocatable = attr.allocatable && !attr.dimension;
8676 : 129839 : gfc_symbol *der = comp ?
8677 : 1793 : comp->ts.type == BT_DERIVED ? comp->ts.u.derived : NULL
8678 : : :
8679 : 125127 : sym->ts.type == BT_DERIVED ? sym->ts.u.derived : NULL;
8680 : 2919 : bool finalizable = der != NULL && der->ns->proc_name
8681 : 5835 : && gfc_is_finalizable (der, NULL);
8682 : :
8683 : 126920 : if (!byref && finalizable)
8684 : 149 : gfc_finalize_tree_expr (se, der, attr, expr->rank);
8685 : :
8686 : 126920 : if (!byref && sym->ts.type != BT_CHARACTER
8687 : 108201 : && allocatable && !finalizable)
8688 : : {
8689 : 224 : tmp = gfc_create_var (TREE_TYPE (se->expr), NULL);
8690 : 224 : gfc_add_modify (&se->pre, tmp, se->expr);
8691 : 224 : se->expr = tmp;
8692 : 224 : tmp = gfc_call_free (tmp);
8693 : 224 : gfc_add_expr_to_block (&post, tmp);
8694 : 224 : gfc_add_modify (&post, se->expr, build_int_cst (TREE_TYPE (se->expr), 0));
8695 : : }
8696 : :
8697 : : /* If we have a pointer function, but we don't want a pointer, e.g.
8698 : : something like
8699 : : x = f()
8700 : : where f is pointer valued, we have to dereference the result. */
8701 : 126920 : if (!se->want_pointer && !byref
8702 : 107812 : && ((!comp && (sym->attr.pointer || sym->attr.allocatable))
8703 : 1613 : || (comp && (comp->attr.pointer || comp->attr.allocatable))))
8704 : 450 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
8705 : :
8706 : : /* f2c calling conventions require a scalar default real function to
8707 : : return a double precision result. Convert this back to default
8708 : : real. We only care about the cases that can happen in Fortran 77.
8709 : : */
8710 : 126920 : if (flag_f2c && sym->ts.type == BT_REAL
8711 : 97 : && sym->ts.kind == gfc_default_real_kind
8712 : 73 : && !sym->attr.pointer
8713 : 54 : && !sym->attr.allocatable
8714 : 42 : && !sym->attr.always_explicit)
8715 : 42 : se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
8716 : :
8717 : : /* A pure function may still have side-effects - it may modify its
8718 : : parameters. */
8719 : 126920 : TREE_SIDE_EFFECTS (se->expr) = 1;
8720 : : #if 0
8721 : : if (!sym->attr.pure)
8722 : : TREE_SIDE_EFFECTS (se->expr) = 1;
8723 : : #endif
8724 : :
8725 : 126920 : if (byref)
8726 : : {
8727 : : /* Add the function call to the pre chain. There is no expression. */
8728 : 18533 : gfc_add_expr_to_block (&se->pre, se->expr);
8729 : 18533 : se->expr = NULL_TREE;
8730 : :
8731 : 18533 : if (!se->direct_byref)
8732 : : {
8733 : 11327 : if ((sym->attr.dimension && !comp) || (comp && comp->attr.dimension))
8734 : : {
8735 : 8354 : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
8736 : : {
8737 : : /* Check the data pointer hasn't been modified. This would
8738 : : happen in a function returning a pointer. */
8739 : 251 : tmp = gfc_conv_descriptor_data_get (info->descriptor);
8740 : 251 : tmp = fold_build2_loc (input_location, NE_EXPR,
8741 : : logical_type_node,
8742 : : tmp, info->data);
8743 : 251 : gfc_trans_runtime_check (true, false, tmp, &se->pre, NULL,
8744 : : gfc_msg_fault);
8745 : : }
8746 : 8354 : se->expr = info->descriptor;
8747 : : /* Bundle in the string length. */
8748 : 8354 : se->string_length = len;
8749 : :
8750 : 8354 : if (finalizable)
8751 : 6 : gfc_finalize_tree_expr (se, der, attr, expr->rank);
8752 : : }
8753 : 2973 : else if (ts.type == BT_CHARACTER)
8754 : : {
8755 : : /* Dereference for character pointer results. */
8756 : 2912 : if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
8757 : 67 : || (comp && (comp->attr.pointer || comp->attr.allocatable)))
8758 : 550 : se->expr = build_fold_indirect_ref_loc (input_location, var);
8759 : : else
8760 : 2362 : se->expr = var;
8761 : :
8762 : 2912 : se->string_length = len;
8763 : : }
8764 : : else
8765 : : {
8766 : 61 : gcc_assert (ts.type == BT_COMPLEX && flag_f2c);
8767 : 61 : se->expr = build_fold_indirect_ref_loc (input_location, var);
8768 : : }
8769 : : }
8770 : : }
8771 : :
8772 : : /* Associate the rhs class object's meta-data with the result, when the
8773 : : result is a temporary. */
8774 : 109723 : if (args && args->expr && args->expr->ts.type == BT_CLASS
8775 : 4800 : && sym->ts.type == BT_CLASS && result != NULL_TREE && DECL_P (result)
8776 : 126952 : && !GFC_CLASS_TYPE_P (TREE_TYPE (result)))
8777 : : {
8778 : 32 : gfc_se parmse;
8779 : 32 : gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (args->expr);
8780 : :
8781 : 32 : gfc_init_se (&parmse, NULL);
8782 : 32 : parmse.data_not_needed = 1;
8783 : 32 : gfc_conv_expr (&parmse, class_expr);
8784 : 32 : if (!DECL_LANG_SPECIFIC (result))
8785 : 32 : gfc_allocate_lang_decl (result);
8786 : 32 : GFC_DECL_SAVED_DESCRIPTOR (result) = parmse.expr;
8787 : 32 : gfc_free_expr (class_expr);
8788 : : /* -fcheck= can add diagnostic code, which has to be placed before
8789 : : the call. */
8790 : 32 : if (parmse.pre.head != NULL)
8791 : 12 : gfc_add_expr_to_block (&se->pre, parmse.pre.head);
8792 : 32 : gcc_assert (parmse.post.head == NULL_TREE);
8793 : : }
8794 : :
8795 : : /* Follow the function call with the argument post block. */
8796 : 126920 : if (byref)
8797 : : {
8798 : 18533 : gfc_add_block_to_block (&se->pre, &post);
8799 : :
8800 : : /* Transformational functions of derived types with allocatable
8801 : : components must have the result allocatable components copied when the
8802 : : argument is actually given. This is unnecessry for REDUCE because the
8803 : : wrapper for the OPERATION function takes care of this. */
8804 : 18533 : arg = expr->value.function.actual;
8805 : 18533 : if (result && arg && expr->rank
8806 : 14762 : && isym && isym->transformational
8807 : 13206 : && isym->id != GFC_ISYM_REDUCE
8808 : 13080 : && arg->expr
8809 : 13038 : && arg->expr->ts.type == BT_DERIVED
8810 : 221 : && arg->expr->ts.u.derived->attr.alloc_comp)
8811 : : {
8812 : 36 : tree tmp2;
8813 : : /* Copy the allocatable components. We have to use a
8814 : : temporary here to prevent source allocatable components
8815 : : from being corrupted. */
8816 : 36 : tmp2 = gfc_evaluate_now (result, &se->pre);
8817 : 36 : tmp = gfc_copy_alloc_comp (arg->expr->ts.u.derived,
8818 : : result, tmp2, expr->rank, 0);
8819 : 36 : gfc_add_expr_to_block (&se->pre, tmp);
8820 : 36 : tmp = gfc_copy_allocatable_data (result, tmp2, TREE_TYPE(tmp2),
8821 : : expr->rank);
8822 : 36 : gfc_add_expr_to_block (&se->pre, tmp);
8823 : :
8824 : : /* Finally free the temporary's data field. */
8825 : 36 : tmp = gfc_conv_descriptor_data_get (tmp2);
8826 : 36 : tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
8827 : : NULL_TREE, NULL_TREE, true,
8828 : : NULL, GFC_CAF_COARRAY_NOCOARRAY);
8829 : 36 : gfc_add_expr_to_block (&se->pre, tmp);
8830 : : }
8831 : : }
8832 : : else
8833 : : {
8834 : : /* For a function with a class array result, save the result as
8835 : : a temporary, set the info fields needed by the scalarizer and
8836 : : call the finalization function of the temporary. Note that the
8837 : : nullification of allocatable components needed by the result
8838 : : is done in gfc_trans_assignment_1. */
8839 : 33629 : if (expr && (gfc_is_class_array_function (expr)
8840 : 33320 : || gfc_is_alloc_class_scalar_function (expr))
8841 : 828 : && se->expr && GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
8842 : 109203 : && expr->must_finalize)
8843 : : {
8844 : 315 : int n;
8845 : 315 : if (se->ss && se->ss->loop)
8846 : : {
8847 : 177 : gfc_add_block_to_block (&se->ss->loop->pre, &se->pre);
8848 : 177 : se->expr = gfc_evaluate_now (se->expr, &se->ss->loop->pre);
8849 : 177 : tmp = gfc_class_data_get (se->expr);
8850 : 177 : info->descriptor = tmp;
8851 : 177 : info->data = gfc_conv_descriptor_data_get (tmp);
8852 : 177 : info->offset = gfc_conv_descriptor_offset_get (tmp);
8853 : 366 : for (n = 0; n < se->ss->loop->dimen; n++)
8854 : : {
8855 : 189 : tree dim = gfc_rank_cst[n];
8856 : 189 : se->ss->loop->to[n] = gfc_conv_descriptor_ubound_get (tmp, dim);
8857 : 189 : se->ss->loop->from[n] = gfc_conv_descriptor_lbound_get (tmp, dim);
8858 : : }
8859 : : }
8860 : : else
8861 : : {
8862 : : /* TODO Eliminate the doubling of temporaries. This
8863 : : one is necessary to ensure no memory leakage. */
8864 : 138 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
8865 : : }
8866 : :
8867 : : /* Finalize the result, if necessary. */
8868 : 630 : attr = expr->value.function.esym
8869 : 315 : ? CLASS_DATA (expr->value.function.esym->result)->attr
8870 : 14 : : CLASS_DATA (expr)->attr;
8871 : 315 : if (!((gfc_is_class_array_function (expr)
8872 : 108 : || gfc_is_alloc_class_scalar_function (expr))
8873 : 315 : && attr.pointer))
8874 : 270 : gfc_finalize_tree_expr (se, NULL, attr, expr->rank);
8875 : : }
8876 : 108387 : gfc_add_block_to_block (&se->post, &post);
8877 : : }
8878 : :
8879 : : return has_alternate_specifier;
8880 : : }
8881 : :
8882 : :
8883 : : /* Fill a character string with spaces. */
8884 : :
8885 : : static tree
8886 : 28597 : fill_with_spaces (tree start, tree type, tree size)
8887 : : {
8888 : 28597 : stmtblock_t block, loop;
8889 : 28597 : tree i, el, exit_label, cond, tmp;
8890 : :
8891 : : /* For a simple char type, we can call memset(). */
8892 : 28597 : if (compare_tree_int (TYPE_SIZE_UNIT (type), 1) == 0)
8893 : 47698 : return build_call_expr_loc (input_location,
8894 : : builtin_decl_explicit (BUILT_IN_MEMSET),
8895 : : 3, start,
8896 : : build_int_cst (gfc_get_int_type (gfc_c_int_kind),
8897 : 23849 : lang_hooks.to_target_charset (' ')),
8898 : : fold_convert (size_type_node, size));
8899 : :
8900 : : /* Otherwise, we use a loop:
8901 : : for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
8902 : : *el = (type) ' ';
8903 : : */
8904 : :
8905 : : /* Initialize variables. */
8906 : 4748 : gfc_init_block (&block);
8907 : 4748 : i = gfc_create_var (sizetype, "i");
8908 : 4748 : gfc_add_modify (&block, i, fold_convert (sizetype, size));
8909 : 4748 : el = gfc_create_var (build_pointer_type (type), "el");
8910 : 4748 : gfc_add_modify (&block, el, fold_convert (TREE_TYPE (el), start));
8911 : 4748 : exit_label = gfc_build_label_decl (NULL_TREE);
8912 : 4748 : TREE_USED (exit_label) = 1;
8913 : :
8914 : :
8915 : : /* Loop body. */
8916 : 4748 : gfc_init_block (&loop);
8917 : :
8918 : : /* Exit condition. */
8919 : 4748 : cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, i,
8920 : : build_zero_cst (sizetype));
8921 : 4748 : tmp = build1_v (GOTO_EXPR, exit_label);
8922 : 4748 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
8923 : : build_empty_stmt (input_location));
8924 : 4748 : gfc_add_expr_to_block (&loop, tmp);
8925 : :
8926 : : /* Assignment. */
8927 : 4748 : gfc_add_modify (&loop,
8928 : : fold_build1_loc (input_location, INDIRECT_REF, type, el),
8929 : 4748 : build_int_cst (type, lang_hooks.to_target_charset (' ')));
8930 : :
8931 : : /* Increment loop variables. */
8932 : 4748 : gfc_add_modify (&loop, i,
8933 : : fold_build2_loc (input_location, MINUS_EXPR, sizetype, i,
8934 : 4748 : TYPE_SIZE_UNIT (type)));
8935 : 4748 : gfc_add_modify (&loop, el,
8936 : : fold_build_pointer_plus_loc (input_location,
8937 : 4748 : el, TYPE_SIZE_UNIT (type)));
8938 : :
8939 : : /* Making the loop... actually loop! */
8940 : 4748 : tmp = gfc_finish_block (&loop);
8941 : 4748 : tmp = build1_v (LOOP_EXPR, tmp);
8942 : 4748 : gfc_add_expr_to_block (&block, tmp);
8943 : :
8944 : : /* The exit label. */
8945 : 4748 : tmp = build1_v (LABEL_EXPR, exit_label);
8946 : 4748 : gfc_add_expr_to_block (&block, tmp);
8947 : :
8948 : :
8949 : 4748 : return gfc_finish_block (&block);
8950 : : }
8951 : :
8952 : :
8953 : : /* Generate code to copy a string. */
8954 : :
8955 : : void
8956 : 33518 : gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
8957 : : int dkind, tree slength, tree src, int skind)
8958 : : {
8959 : 33518 : tree tmp, dlen, slen;
8960 : 33518 : tree dsc;
8961 : 33518 : tree ssc;
8962 : 33518 : tree cond;
8963 : 33518 : tree cond2;
8964 : 33518 : tree tmp2;
8965 : 33518 : tree tmp3;
8966 : 33518 : tree tmp4;
8967 : 33518 : tree chartype;
8968 : 33518 : stmtblock_t tempblock;
8969 : :
8970 : 33518 : gcc_assert (dkind == skind);
8971 : :
8972 : 33518 : if (slength != NULL_TREE)
8973 : : {
8974 : 33518 : slen = gfc_evaluate_now (fold_convert (gfc_charlen_type_node, slength), block);
8975 : 33518 : ssc = gfc_string_to_single_character (slen, src, skind);
8976 : : }
8977 : : else
8978 : : {
8979 : 0 : slen = build_one_cst (gfc_charlen_type_node);
8980 : 0 : ssc = src;
8981 : : }
8982 : :
8983 : 33518 : if (dlength != NULL_TREE)
8984 : : {
8985 : 33518 : dlen = gfc_evaluate_now (fold_convert (gfc_charlen_type_node, dlength), block);
8986 : 33518 : dsc = gfc_string_to_single_character (dlen, dest, dkind);
8987 : : }
8988 : : else
8989 : : {
8990 : 0 : dlen = build_one_cst (gfc_charlen_type_node);
8991 : 0 : dsc = dest;
8992 : : }
8993 : :
8994 : : /* Assign directly if the types are compatible. */
8995 : 33518 : if (dsc != NULL_TREE && ssc != NULL_TREE
8996 : 33518 : && TREE_TYPE (dsc) == TREE_TYPE (ssc))
8997 : : {
8998 : 4921 : gfc_add_modify (block, dsc, ssc);
8999 : 4921 : return;
9000 : : }
9001 : :
9002 : : /* The string copy algorithm below generates code like
9003 : :
9004 : : if (destlen > 0)
9005 : : {
9006 : : if (srclen < destlen)
9007 : : {
9008 : : memmove (dest, src, srclen);
9009 : : // Pad with spaces.
9010 : : memset (&dest[srclen], ' ', destlen - srclen);
9011 : : }
9012 : : else
9013 : : {
9014 : : // Truncate if too long.
9015 : : memmove (dest, src, destlen);
9016 : : }
9017 : : }
9018 : : */
9019 : :
9020 : : /* Do nothing if the destination length is zero. */
9021 : 28597 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node, dlen,
9022 : 28597 : build_zero_cst (TREE_TYPE (dlen)));
9023 : :
9024 : : /* For non-default character kinds, we have to multiply the string
9025 : : length by the base type size. */
9026 : 28597 : chartype = gfc_get_char_type (dkind);
9027 : 28597 : slen = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (slen),
9028 : : slen,
9029 : 28597 : fold_convert (TREE_TYPE (slen),
9030 : : TYPE_SIZE_UNIT (chartype)));
9031 : 28597 : dlen = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (dlen),
9032 : : dlen,
9033 : 28597 : fold_convert (TREE_TYPE (dlen),
9034 : : TYPE_SIZE_UNIT (chartype)));
9035 : :
9036 : 28597 : if (dlength && POINTER_TYPE_P (TREE_TYPE (dest)))
9037 : 28549 : dest = fold_convert (pvoid_type_node, dest);
9038 : : else
9039 : 48 : dest = gfc_build_addr_expr (pvoid_type_node, dest);
9040 : :
9041 : 28597 : if (slength && POINTER_TYPE_P (TREE_TYPE (src)))
9042 : 28593 : src = fold_convert (pvoid_type_node, src);
9043 : : else
9044 : 4 : src = gfc_build_addr_expr (pvoid_type_node, src);
9045 : :
9046 : : /* Truncate string if source is too long. */
9047 : 28597 : cond2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node, slen,
9048 : : dlen);
9049 : :
9050 : : /* Pre-evaluate pointers unless one of the IF arms will be optimized away. */
9051 : 28597 : if (!CONSTANT_CLASS_P (cond2))
9052 : : {
9053 : 8870 : dest = gfc_evaluate_now (dest, block);
9054 : 8870 : src = gfc_evaluate_now (src, block);
9055 : : }
9056 : :
9057 : : /* Copy and pad with spaces. */
9058 : 28597 : tmp3 = build_call_expr_loc (input_location,
9059 : : builtin_decl_explicit (BUILT_IN_MEMMOVE),
9060 : : 3, dest, src,
9061 : : fold_convert (size_type_node, slen));
9062 : :
9063 : : /* Wstringop-overflow appears at -O3 even though this warning is not
9064 : : explicitly available in fortran nor can it be switched off. If the
9065 : : source length is a constant, its negative appears as a very large
9066 : : positive number and triggers the warning in BUILTIN_MEMSET. Fixing
9067 : : the result of the MINUS_EXPR suppresses this spurious warning. */
9068 : 28597 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
9069 : 28597 : TREE_TYPE(dlen), dlen, slen);
9070 : 28597 : if (slength && TREE_CONSTANT (slength))
9071 : 25237 : tmp = gfc_evaluate_now (tmp, block);
9072 : :
9073 : 28597 : tmp4 = fold_build_pointer_plus_loc (input_location, dest, slen);
9074 : 28597 : tmp4 = fill_with_spaces (tmp4, chartype, tmp);
9075 : :
9076 : 28597 : gfc_init_block (&tempblock);
9077 : 28597 : gfc_add_expr_to_block (&tempblock, tmp3);
9078 : 28597 : gfc_add_expr_to_block (&tempblock, tmp4);
9079 : 28597 : tmp3 = gfc_finish_block (&tempblock);
9080 : :
9081 : : /* The truncated memmove if the slen >= dlen. */
9082 : 28597 : tmp2 = build_call_expr_loc (input_location,
9083 : : builtin_decl_explicit (BUILT_IN_MEMMOVE),
9084 : : 3, dest, src,
9085 : : fold_convert (size_type_node, dlen));
9086 : :
9087 : : /* The whole copy_string function is there. */
9088 : 28597 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
9089 : : tmp3, tmp2);
9090 : 28597 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
9091 : : build_empty_stmt (input_location));
9092 : 28597 : gfc_add_expr_to_block (block, tmp);
9093 : : }
9094 : :
9095 : :
9096 : : /* Translate a statement function.
9097 : : The value of a statement function reference is obtained by evaluating the
9098 : : expression using the values of the actual arguments for the values of the
9099 : : corresponding dummy arguments. */
9100 : :
9101 : : static void
9102 : 269 : gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
9103 : : {
9104 : 269 : gfc_symbol *sym;
9105 : 269 : gfc_symbol *fsym;
9106 : 269 : gfc_formal_arglist *fargs;
9107 : 269 : gfc_actual_arglist *args;
9108 : 269 : gfc_se lse;
9109 : 269 : gfc_se rse;
9110 : 269 : gfc_saved_var *saved_vars;
9111 : 269 : tree *temp_vars;
9112 : 269 : tree type;
9113 : 269 : tree tmp;
9114 : 269 : int n;
9115 : :
9116 : 269 : sym = expr->symtree->n.sym;
9117 : 269 : args = expr->value.function.actual;
9118 : 269 : gfc_init_se (&lse, NULL);
9119 : 269 : gfc_init_se (&rse, NULL);
9120 : :
9121 : 269 : n = 0;
9122 : 727 : for (fargs = gfc_sym_get_dummy_args (sym); fargs; fargs = fargs->next)
9123 : 458 : n++;
9124 : 269 : saved_vars = XCNEWVEC (gfc_saved_var, n);
9125 : 269 : temp_vars = XCNEWVEC (tree, n);
9126 : :
9127 : 727 : for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
9128 : 458 : fargs = fargs->next, n++)
9129 : : {
9130 : : /* Each dummy shall be specified, explicitly or implicitly, to be
9131 : : scalar. */
9132 : 458 : gcc_assert (fargs->sym->attr.dimension == 0);
9133 : 458 : fsym = fargs->sym;
9134 : :
9135 : 458 : if (fsym->ts.type == BT_CHARACTER)
9136 : : {
9137 : : /* Copy string arguments. */
9138 : 48 : tree arglen;
9139 : :
9140 : 48 : gcc_assert (fsym->ts.u.cl && fsym->ts.u.cl->length
9141 : : && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT);
9142 : :
9143 : : /* Create a temporary to hold the value. */
9144 : 48 : if (fsym->ts.u.cl->backend_decl == NULL_TREE)
9145 : 1 : fsym->ts.u.cl->backend_decl
9146 : 1 : = gfc_conv_constant_to_tree (fsym->ts.u.cl->length);
9147 : :
9148 : 48 : type = gfc_get_character_type (fsym->ts.kind, fsym->ts.u.cl);
9149 : 48 : temp_vars[n] = gfc_create_var (type, fsym->name);
9150 : :
9151 : 48 : arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
9152 : :
9153 : 48 : gfc_conv_expr (&rse, args->expr);
9154 : 48 : gfc_conv_string_parameter (&rse);
9155 : 48 : gfc_add_block_to_block (&se->pre, &lse.pre);
9156 : 48 : gfc_add_block_to_block (&se->pre, &rse.pre);
9157 : :
9158 : 48 : gfc_trans_string_copy (&se->pre, arglen, temp_vars[n], fsym->ts.kind,
9159 : : rse.string_length, rse.expr, fsym->ts.kind);
9160 : 48 : gfc_add_block_to_block (&se->pre, &lse.post);
9161 : 48 : gfc_add_block_to_block (&se->pre, &rse.post);
9162 : : }
9163 : : else
9164 : : {
9165 : : /* For everything else, just evaluate the expression. */
9166 : :
9167 : : /* Create a temporary to hold the value. */
9168 : 410 : type = gfc_typenode_for_spec (&fsym->ts);
9169 : 410 : temp_vars[n] = gfc_create_var (type, fsym->name);
9170 : :
9171 : 410 : gfc_conv_expr (&lse, args->expr);
9172 : :
9173 : 410 : gfc_add_block_to_block (&se->pre, &lse.pre);
9174 : 410 : gfc_add_modify (&se->pre, temp_vars[n], lse.expr);
9175 : 410 : gfc_add_block_to_block (&se->pre, &lse.post);
9176 : : }
9177 : :
9178 : 458 : args = args->next;
9179 : : }
9180 : :
9181 : : /* Use the temporary variables in place of the real ones. */
9182 : 727 : for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
9183 : 458 : fargs = fargs->next, n++)
9184 : 458 : gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
9185 : :
9186 : 269 : gfc_conv_expr (se, sym->value);
9187 : :
9188 : 269 : if (sym->ts.type == BT_CHARACTER)
9189 : : {
9190 : 55 : gfc_conv_const_charlen (sym->ts.u.cl);
9191 : :
9192 : : /* Force the expression to the correct length. */
9193 : 55 : if (!INTEGER_CST_P (se->string_length)
9194 : 101 : || tree_int_cst_lt (se->string_length,
9195 : 46 : sym->ts.u.cl->backend_decl))
9196 : : {
9197 : 31 : type = gfc_get_character_type (sym->ts.kind, sym->ts.u.cl);
9198 : 31 : tmp = gfc_create_var (type, sym->name);
9199 : 31 : tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
9200 : 31 : gfc_trans_string_copy (&se->pre, sym->ts.u.cl->backend_decl, tmp,
9201 : : sym->ts.kind, se->string_length, se->expr,
9202 : : sym->ts.kind);
9203 : 31 : se->expr = tmp;
9204 : : }
9205 : 55 : se->string_length = sym->ts.u.cl->backend_decl;
9206 : : }
9207 : :
9208 : : /* Restore the original variables. */
9209 : 727 : for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
9210 : 458 : fargs = fargs->next, n++)
9211 : 458 : gfc_restore_sym (fargs->sym, &saved_vars[n]);
9212 : 269 : free (temp_vars);
9213 : 269 : free (saved_vars);
9214 : 269 : }
9215 : :
9216 : :
9217 : : /* Translate a function expression. */
9218 : :
9219 : : static void
9220 : 297469 : gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
9221 : : {
9222 : 297469 : gfc_symbol *sym;
9223 : :
9224 : 297469 : if (expr->value.function.isym)
9225 : : {
9226 : 248313 : gfc_conv_intrinsic_function (se, expr);
9227 : 248313 : return;
9228 : : }
9229 : :
9230 : : /* expr.value.function.esym is the resolved (specific) function symbol for
9231 : : most functions. However this isn't set for dummy procedures. */
9232 : 49156 : sym = expr->value.function.esym;
9233 : 49156 : if (!sym)
9234 : 1437 : sym = expr->symtree->n.sym;
9235 : :
9236 : : /* The IEEE_ARITHMETIC functions are caught here. */
9237 : 49156 : if (sym->from_intmod == INTMOD_IEEE_ARITHMETIC)
9238 : 13939 : if (gfc_conv_ieee_arithmetic_function (se, expr))
9239 : : return;
9240 : :
9241 : : /* We distinguish statement functions from general functions to improve
9242 : : runtime performance. */
9243 : 36699 : if (sym->attr.proc == PROC_ST_FUNCTION)
9244 : : {
9245 : 269 : gfc_conv_statement_function (se, expr);
9246 : 269 : return;
9247 : : }
9248 : :
9249 : 36430 : gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
9250 : : NULL);
9251 : : }
9252 : :
9253 : :
9254 : : /* Determine whether the given EXPR_CONSTANT is a zero initializer. */
9255 : :
9256 : : static bool
9257 : 38088 : is_zero_initializer_p (gfc_expr * expr)
9258 : : {
9259 : 38088 : if (expr->expr_type != EXPR_CONSTANT)
9260 : : return false;
9261 : :
9262 : : /* We ignore constants with prescribed memory representations for now. */
9263 : 11237 : if (expr->representation.string)
9264 : : return false;
9265 : :
9266 : 11219 : switch (expr->ts.type)
9267 : : {
9268 : 5107 : case BT_INTEGER:
9269 : 5107 : return mpz_cmp_si (expr->value.integer, 0) == 0;
9270 : :
9271 : 4813 : case BT_REAL:
9272 : 4813 : return mpfr_zero_p (expr->value.real)
9273 : 4813 : && MPFR_SIGN (expr->value.real) >= 0;
9274 : :
9275 : 925 : case BT_LOGICAL:
9276 : 925 : return expr->value.logical == 0;
9277 : :
9278 : 240 : case BT_COMPLEX:
9279 : 240 : return mpfr_zero_p (mpc_realref (expr->value.complex))
9280 : 154 : && MPFR_SIGN (mpc_realref (expr->value.complex)) >= 0
9281 : 154 : && mpfr_zero_p (mpc_imagref (expr->value.complex))
9282 : 382 : && MPFR_SIGN (mpc_imagref (expr->value.complex)) >= 0;
9283 : :
9284 : : default:
9285 : : break;
9286 : : }
9287 : : return false;
9288 : : }
9289 : :
9290 : :
9291 : : static void
9292 : 34435 : gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
9293 : : {
9294 : 34435 : gfc_ss *ss;
9295 : :
9296 : 34435 : ss = se->ss;
9297 : 34435 : gcc_assert (ss != NULL && ss != gfc_ss_terminator);
9298 : 34435 : gcc_assert (ss->info->expr == expr && ss->info->type == GFC_SS_CONSTRUCTOR);
9299 : :
9300 : 34435 : gfc_conv_tmp_array_ref (se);
9301 : 34435 : }
9302 : :
9303 : :
9304 : : /* Build a static initializer. EXPR is the expression for the initial value.
9305 : : The other parameters describe the variable of the component being
9306 : : initialized. EXPR may be null. */
9307 : :
9308 : : tree
9309 : 132327 : gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
9310 : : bool array, bool pointer, bool procptr)
9311 : : {
9312 : 132327 : gfc_se se;
9313 : :
9314 : 132327 : if (flag_coarray != GFC_FCOARRAY_LIB && ts->type == BT_DERIVED
9315 : 42103 : && ts->u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
9316 : 165 : && ts->u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
9317 : 57 : return build_constructor (type, NULL);
9318 : :
9319 : 132270 : if (!(expr || pointer || procptr))
9320 : : return NULL_TREE;
9321 : :
9322 : : /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
9323 : : (these are the only two iso_c_binding derived types that can be
9324 : : used as initialization expressions). If so, we need to modify
9325 : : the 'expr' to be that for a (void *). */
9326 : 124142 : if (expr != NULL && expr->ts.type == BT_DERIVED
9327 : 37933 : && expr->ts.is_iso_c && expr->ts.u.derived)
9328 : : {
9329 : 186 : if (TREE_CODE (type) == ARRAY_TYPE)
9330 : 4 : return build_constructor (type, NULL);
9331 : 182 : else if (POINTER_TYPE_P (type))
9332 : 182 : return build_int_cst (type, 0);
9333 : : else
9334 : 0 : gcc_unreachable ();
9335 : : }
9336 : :
9337 : 123956 : if (array && !procptr)
9338 : : {
9339 : 8308 : tree ctor;
9340 : : /* Arrays need special handling. */
9341 : 8308 : if (pointer)
9342 : 705 : ctor = gfc_build_null_descriptor (type);
9343 : : /* Special case assigning an array to zero. */
9344 : 7603 : else if (is_zero_initializer_p (expr))
9345 : 215 : ctor = build_constructor (type, NULL);
9346 : : else
9347 : 7388 : ctor = gfc_conv_array_initializer (type, expr);
9348 : 8308 : TREE_STATIC (ctor) = 1;
9349 : 8308 : return ctor;
9350 : : }
9351 : 115648 : else if (pointer || procptr)
9352 : : {
9353 : 55775 : if (ts->type == BT_CLASS && !procptr)
9354 : : {
9355 : 1694 : gfc_init_se (&se, NULL);
9356 : 1694 : gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
9357 : 1694 : gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
9358 : 1694 : TREE_STATIC (se.expr) = 1;
9359 : 1694 : return se.expr;
9360 : : }
9361 : 54081 : else if (!expr || expr->expr_type == EXPR_NULL)
9362 : 29393 : return fold_convert (type, null_pointer_node);
9363 : : else
9364 : : {
9365 : 24688 : gfc_init_se (&se, NULL);
9366 : 24688 : se.want_pointer = 1;
9367 : 24688 : gfc_conv_expr (&se, expr);
9368 : 24688 : gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
9369 : : return se.expr;
9370 : : }
9371 : : }
9372 : : else
9373 : : {
9374 : 59873 : switch (ts->type)
9375 : : {
9376 : 17681 : case_bt_struct:
9377 : 17681 : case BT_CLASS:
9378 : 17681 : gfc_init_se (&se, NULL);
9379 : 17681 : if (ts->type == BT_CLASS && expr->expr_type == EXPR_NULL)
9380 : 739 : gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
9381 : : else
9382 : 16942 : gfc_conv_structure (&se, expr, 1);
9383 : 17681 : gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
9384 : 17681 : TREE_STATIC (se.expr) = 1;
9385 : 17681 : return se.expr;
9386 : :
9387 : 2575 : case BT_CHARACTER:
9388 : 2575 : if (expr->expr_type == EXPR_CONSTANT)
9389 : : {
9390 : 2574 : tree ctor = gfc_conv_string_init (ts->u.cl->backend_decl, expr);
9391 : 2574 : TREE_STATIC (ctor) = 1;
9392 : 2574 : return ctor;
9393 : : }
9394 : :
9395 : : /* Fallthrough. */
9396 : 39618 : default:
9397 : 39618 : gfc_init_se (&se, NULL);
9398 : 39618 : gfc_conv_constant (&se, expr);
9399 : 39618 : gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
9400 : : return se.expr;
9401 : : }
9402 : : }
9403 : : }
9404 : :
9405 : : static tree
9406 : 933 : gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
9407 : : {
9408 : 933 : gfc_se rse;
9409 : 933 : gfc_se lse;
9410 : 933 : gfc_ss *rss;
9411 : 933 : gfc_ss *lss;
9412 : 933 : gfc_array_info *lss_array;
9413 : 933 : stmtblock_t body;
9414 : 933 : stmtblock_t block;
9415 : 933 : gfc_loopinfo loop;
9416 : 933 : int n;
9417 : 933 : tree tmp;
9418 : :
9419 : 933 : gfc_start_block (&block);
9420 : :
9421 : : /* Initialize the scalarizer. */
9422 : 933 : gfc_init_loopinfo (&loop);
9423 : :
9424 : 933 : gfc_init_se (&lse, NULL);
9425 : 933 : gfc_init_se (&rse, NULL);
9426 : :
9427 : : /* Walk the rhs. */
9428 : 933 : rss = gfc_walk_expr (expr);
9429 : 933 : if (rss == gfc_ss_terminator)
9430 : : /* The rhs is scalar. Add a ss for the expression. */
9431 : 201 : rss = gfc_get_scalar_ss (gfc_ss_terminator, expr);
9432 : :
9433 : : /* Create a SS for the destination. */
9434 : 933 : lss = gfc_get_array_ss (gfc_ss_terminator, NULL, cm->as->rank,
9435 : : GFC_SS_COMPONENT);
9436 : 933 : lss_array = &lss->info->data.array;
9437 : 933 : lss_array->shape = gfc_get_shape (cm->as->rank);
9438 : 933 : lss_array->descriptor = dest;
9439 : 933 : lss_array->data = gfc_conv_array_data (dest);
9440 : 933 : lss_array->offset = gfc_conv_array_offset (dest);
9441 : 1923 : for (n = 0; n < cm->as->rank; n++)
9442 : : {
9443 : 990 : lss_array->start[n] = gfc_conv_array_lbound (dest, n);
9444 : 990 : lss_array->stride[n] = gfc_index_one_node;
9445 : :
9446 : 990 : mpz_init (lss_array->shape[n]);
9447 : 990 : mpz_sub (lss_array->shape[n], cm->as->upper[n]->value.integer,
9448 : 990 : cm->as->lower[n]->value.integer);
9449 : 990 : mpz_add_ui (lss_array->shape[n], lss_array->shape[n], 1);
9450 : : }
9451 : :
9452 : : /* Associate the SS with the loop. */
9453 : 933 : gfc_add_ss_to_loop (&loop, lss);
9454 : 933 : gfc_add_ss_to_loop (&loop, rss);
9455 : :
9456 : : /* Calculate the bounds of the scalarization. */
9457 : 933 : gfc_conv_ss_startstride (&loop);
9458 : :
9459 : : /* Setup the scalarizing loops. */
9460 : 933 : gfc_conv_loop_setup (&loop, &expr->where);
9461 : :
9462 : : /* Setup the gfc_se structures. */
9463 : 933 : gfc_copy_loopinfo_to_se (&lse, &loop);
9464 : 933 : gfc_copy_loopinfo_to_se (&rse, &loop);
9465 : :
9466 : 933 : rse.ss = rss;
9467 : 933 : gfc_mark_ss_chain_used (rss, 1);
9468 : 933 : lse.ss = lss;
9469 : 933 : gfc_mark_ss_chain_used (lss, 1);
9470 : :
9471 : : /* Start the scalarized loop body. */
9472 : 933 : gfc_start_scalarized_body (&loop, &body);
9473 : :
9474 : 933 : gfc_conv_tmp_array_ref (&lse);
9475 : 933 : if (cm->ts.type == BT_CHARACTER)
9476 : 176 : lse.string_length = cm->ts.u.cl->backend_decl;
9477 : :
9478 : 933 : gfc_conv_expr (&rse, expr);
9479 : :
9480 : 933 : tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false);
9481 : 933 : gfc_add_expr_to_block (&body, tmp);
9482 : :
9483 : 933 : gcc_assert (rse.ss == gfc_ss_terminator);
9484 : :
9485 : : /* Generate the copying loops. */
9486 : 933 : gfc_trans_scalarizing_loops (&loop, &body);
9487 : :
9488 : : /* Wrap the whole thing up. */
9489 : 933 : gfc_add_block_to_block (&block, &loop.pre);
9490 : 933 : gfc_add_block_to_block (&block, &loop.post);
9491 : :
9492 : 933 : gcc_assert (lss_array->shape != NULL);
9493 : 933 : gfc_free_shape (&lss_array->shape, cm->as->rank);
9494 : 933 : gfc_cleanup_loop (&loop);
9495 : :
9496 : 933 : return gfc_finish_block (&block);
9497 : : }
9498 : :
9499 : :
9500 : : static tree
9501 : 968 : gfc_trans_alloc_subarray_assign (tree dest, gfc_component * cm,
9502 : : gfc_expr * expr)
9503 : : {
9504 : 968 : gfc_se se;
9505 : 968 : stmtblock_t block;
9506 : 968 : tree offset;
9507 : 968 : int n;
9508 : 968 : tree tmp;
9509 : 968 : tree tmp2;
9510 : 968 : gfc_array_spec *as;
9511 : 968 : gfc_expr *arg = NULL;
9512 : :
9513 : 968 : gfc_start_block (&block);
9514 : 968 : gfc_init_se (&se, NULL);
9515 : :
9516 : : /* Get the descriptor for the expressions. */
9517 : 968 : se.want_pointer = 0;
9518 : 968 : gfc_conv_expr_descriptor (&se, expr);
9519 : 968 : gfc_add_block_to_block (&block, &se.pre);
9520 : 968 : gfc_add_modify (&block, dest, se.expr);
9521 : 968 : if (cm->ts.type == BT_CHARACTER
9522 : 968 : && gfc_deferred_strlen (cm, &tmp))
9523 : : {
9524 : 30 : tmp = fold_build3_loc (input_location, COMPONENT_REF,
9525 : 30 : TREE_TYPE (tmp),
9526 : 30 : TREE_OPERAND (dest, 0),
9527 : : tmp, NULL_TREE);
9528 : 30 : gfc_add_modify (&block, tmp,
9529 : 30 : fold_convert (TREE_TYPE (tmp),
9530 : : se.string_length));
9531 : 30 : cm->ts.u.cl->backend_decl = gfc_create_var (gfc_charlen_type_node,
9532 : : "slen");
9533 : 30 : gfc_add_modify (&block, cm->ts.u.cl->backend_decl, se.string_length);
9534 : : }
9535 : :
9536 : : /* Deal with arrays of derived types with allocatable components. */
9537 : 968 : if (gfc_bt_struct (cm->ts.type)
9538 : 150 : && cm->ts.u.derived->attr.alloc_comp)
9539 : : // TODO: Fix caf_mode
9540 : 106 : tmp = gfc_copy_alloc_comp (cm->ts.u.derived,
9541 : : se.expr, dest,
9542 : 106 : cm->as->rank, 0);
9543 : 862 : else if (cm->ts.type == BT_CLASS && expr->ts.type == BT_DERIVED
9544 : 36 : && CLASS_DATA(cm)->attr.allocatable)
9545 : : {
9546 : 36 : if (cm->ts.u.derived->attr.alloc_comp)
9547 : : // TODO: Fix caf_mode
9548 : 0 : tmp = gfc_copy_alloc_comp (expr->ts.u.derived,
9549 : : se.expr, dest,
9550 : : expr->rank, 0);
9551 : : else
9552 : : {
9553 : 36 : tmp = TREE_TYPE (dest);
9554 : 36 : tmp = gfc_duplicate_allocatable (dest, se.expr,
9555 : : tmp, expr->rank, NULL_TREE);
9556 : : }
9557 : : }
9558 : 826 : else if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
9559 : 30 : tmp = gfc_duplicate_allocatable (dest, se.expr,
9560 : : gfc_typenode_for_spec (&cm->ts),
9561 : 30 : cm->as->rank, NULL_TREE);
9562 : : else
9563 : 796 : tmp = gfc_duplicate_allocatable (dest, se.expr,
9564 : 796 : TREE_TYPE(cm->backend_decl),
9565 : 796 : cm->as->rank, NULL_TREE);
9566 : :
9567 : :
9568 : 968 : gfc_add_expr_to_block (&block, tmp);
9569 : 968 : gfc_add_block_to_block (&block, &se.post);
9570 : :
9571 : 968 : if (expr->expr_type != EXPR_VARIABLE)
9572 : 857 : gfc_conv_descriptor_data_set (&block, se.expr,
9573 : : null_pointer_node);
9574 : :
9575 : : /* We need to know if the argument of a conversion function is a
9576 : : variable, so that the correct lower bound can be used. */
9577 : 968 : if (expr->expr_type == EXPR_FUNCTION
9578 : 50 : && expr->value.function.isym
9579 : 38 : && expr->value.function.isym->conversion
9580 : 38 : && expr->value.function.actual->expr
9581 : 38 : && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE)
9582 : 38 : arg = expr->value.function.actual->expr;
9583 : :
9584 : : /* Obtain the array spec of full array references. */
9585 : 38 : if (arg)
9586 : 38 : as = gfc_get_full_arrayspec_from_expr (arg);
9587 : : else
9588 : 930 : as = gfc_get_full_arrayspec_from_expr (expr);
9589 : :
9590 : : /* Shift the lbound and ubound of temporaries to being unity,
9591 : : rather than zero, based. Always calculate the offset. */
9592 : 968 : offset = gfc_conv_descriptor_offset_get (dest);
9593 : 968 : gfc_add_modify (&block, offset, gfc_index_zero_node);
9594 : 968 : tmp2 =gfc_create_var (gfc_array_index_type, NULL);
9595 : :
9596 : 1992 : for (n = 0; n < expr->rank; n++)
9597 : : {
9598 : 1024 : tree span;
9599 : 1024 : tree lbound;
9600 : :
9601 : : /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
9602 : : TODO It looks as if gfc_conv_expr_descriptor should return
9603 : : the correct bounds and that the following should not be
9604 : : necessary. This would simplify gfc_conv_intrinsic_bound
9605 : : as well. */
9606 : 1024 : if (as && as->lower[n])
9607 : : {
9608 : 54 : gfc_se lbse;
9609 : 54 : gfc_init_se (&lbse, NULL);
9610 : 54 : gfc_conv_expr (&lbse, as->lower[n]);
9611 : 54 : gfc_add_block_to_block (&block, &lbse.pre);
9612 : 54 : lbound = gfc_evaluate_now (lbse.expr, &block);
9613 : 54 : }
9614 : 970 : else if (as && arg)
9615 : : {
9616 : 46 : tmp = gfc_get_symbol_decl (arg->symtree->n.sym);
9617 : 46 : lbound = gfc_conv_descriptor_lbound_get (tmp,
9618 : : gfc_rank_cst[n]);
9619 : : }
9620 : 924 : else if (as)
9621 : 63 : lbound = gfc_conv_descriptor_lbound_get (dest,
9622 : : gfc_rank_cst[n]);
9623 : : else
9624 : 861 : lbound = gfc_index_one_node;
9625 : :
9626 : 1024 : lbound = fold_convert (gfc_array_index_type, lbound);
9627 : :
9628 : : /* Shift the bounds and set the offset accordingly. */
9629 : 1024 : tmp = gfc_conv_descriptor_ubound_get (dest, gfc_rank_cst[n]);
9630 : 1024 : span = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
9631 : : tmp, gfc_conv_descriptor_lbound_get (dest, gfc_rank_cst[n]));
9632 : 1024 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
9633 : : span, lbound);
9634 : 1024 : gfc_conv_descriptor_ubound_set (&block, dest,
9635 : : gfc_rank_cst[n], tmp);
9636 : 1024 : gfc_conv_descriptor_lbound_set (&block, dest,
9637 : : gfc_rank_cst[n], lbound);
9638 : :
9639 : 1024 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
9640 : : gfc_conv_descriptor_lbound_get (dest,
9641 : : gfc_rank_cst[n]),
9642 : : gfc_conv_descriptor_stride_get (dest,
9643 : : gfc_rank_cst[n]));
9644 : 1024 : gfc_add_modify (&block, tmp2, tmp);
9645 : 1024 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
9646 : : offset, tmp2);
9647 : 1024 : gfc_conv_descriptor_offset_set (&block, dest, tmp);
9648 : : }
9649 : :
9650 : 968 : if (arg)
9651 : : {
9652 : : /* If a conversion expression has a null data pointer
9653 : : argument, nullify the allocatable component. */
9654 : 38 : tree non_null_expr;
9655 : 38 : tree null_expr;
9656 : :
9657 : 38 : if (arg->symtree->n.sym->attr.allocatable
9658 : 0 : || arg->symtree->n.sym->attr.pointer)
9659 : : {
9660 : 38 : non_null_expr = gfc_finish_block (&block);
9661 : 38 : gfc_start_block (&block);
9662 : 38 : gfc_conv_descriptor_data_set (&block, dest,
9663 : : null_pointer_node);
9664 : 38 : null_expr = gfc_finish_block (&block);
9665 : 38 : tmp = gfc_conv_descriptor_data_get (arg->symtree->n.sym->backend_decl);
9666 : 38 : tmp = build2_loc (input_location, EQ_EXPR, logical_type_node, tmp,
9667 : 38 : fold_convert (TREE_TYPE (tmp), null_pointer_node));
9668 : 38 : return build3_v (COND_EXPR, tmp,
9669 : : null_expr, non_null_expr);
9670 : : }
9671 : : }
9672 : :
9673 : 930 : return gfc_finish_block (&block);
9674 : : }
9675 : :
9676 : :
9677 : : /* Allocate or reallocate scalar component, as necessary. */
9678 : :
9679 : : static void
9680 : 351 : alloc_scalar_allocatable_subcomponent (stmtblock_t *block, tree comp,
9681 : : gfc_component *cm, gfc_expr *expr2,
9682 : : tree slen)
9683 : : {
9684 : 351 : tree tmp;
9685 : 351 : tree ptr;
9686 : 351 : tree size;
9687 : 351 : tree size_in_bytes;
9688 : 351 : tree lhs_cl_size = NULL_TREE;
9689 : 351 : gfc_se se;
9690 : :
9691 : 351 : if (!comp)
9692 : 0 : return;
9693 : :
9694 : 351 : if (!expr2 || expr2->rank)
9695 : : return;
9696 : :
9697 : 351 : realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
9698 : :
9699 : 351 : if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
9700 : : {
9701 : 127 : gcc_assert (expr2->ts.type == BT_CHARACTER);
9702 : 127 : size = expr2->ts.u.cl->backend_decl;
9703 : 127 : if (!size || !VAR_P (size))
9704 : 127 : size = gfc_create_var (TREE_TYPE (slen), "slen");
9705 : 127 : gfc_add_modify (block, size, slen);
9706 : :
9707 : 127 : gfc_deferred_strlen (cm, &tmp);
9708 : 127 : lhs_cl_size = fold_build3_loc (input_location, COMPONENT_REF,
9709 : : gfc_charlen_type_node,
9710 : 127 : TREE_OPERAND (comp, 0),
9711 : : tmp, NULL_TREE);
9712 : :
9713 : 127 : tmp = TREE_TYPE (gfc_typenode_for_spec (&cm->ts));
9714 : 127 : tmp = TYPE_SIZE_UNIT (tmp);
9715 : 254 : size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
9716 : 127 : TREE_TYPE (tmp), tmp,
9717 : 127 : fold_convert (TREE_TYPE (tmp), size));
9718 : : }
9719 : 224 : else if (cm->ts.type == BT_CLASS)
9720 : : {
9721 : 78 : if (expr2->ts.type != BT_CLASS)
9722 : : {
9723 : 78 : if (expr2->ts.type == BT_CHARACTER)
9724 : : {
9725 : 18 : gfc_init_se (&se, NULL);
9726 : 18 : gfc_conv_expr (&se, expr2);
9727 : 18 : size = build_int_cst (gfc_charlen_type_node, expr2->ts.kind);
9728 : 18 : size = fold_build2_loc (input_location, MULT_EXPR,
9729 : : gfc_charlen_type_node,
9730 : : se.string_length, size);
9731 : 18 : size = fold_convert (size_type_node, size);
9732 : : }
9733 : : else
9734 : : {
9735 : 60 : if (expr2->ts.type == BT_DERIVED)
9736 : 48 : tmp = gfc_get_symbol_decl (expr2->ts.u.derived);
9737 : : else
9738 : 12 : tmp = gfc_typenode_for_spec (&expr2->ts);
9739 : 60 : size = TYPE_SIZE_UNIT (tmp);
9740 : : }
9741 : : }
9742 : : else
9743 : : {
9744 : 0 : gfc_expr *e2vtab;
9745 : 0 : e2vtab = gfc_find_and_cut_at_last_class_ref (expr2);
9746 : 0 : gfc_add_vptr_component (e2vtab);
9747 : 0 : gfc_add_size_component (e2vtab);
9748 : 0 : gfc_init_se (&se, NULL);
9749 : 0 : gfc_conv_expr (&se, e2vtab);
9750 : 0 : gfc_add_block_to_block (block, &se.pre);
9751 : 0 : size = fold_convert (size_type_node, se.expr);
9752 : 0 : gfc_free_expr (e2vtab);
9753 : : }
9754 : : size_in_bytes = size;
9755 : : }
9756 : : else
9757 : : {
9758 : : /* Otherwise use the length in bytes of the rhs. */
9759 : 146 : size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&cm->ts));
9760 : 146 : size_in_bytes = size;
9761 : : }
9762 : :
9763 : 351 : size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
9764 : : size_in_bytes, size_one_node);
9765 : :
9766 : 351 : if (cm->ts.type == BT_DERIVED && cm->ts.u.derived->attr.alloc_comp)
9767 : : {
9768 : 0 : tmp = build_call_expr_loc (input_location,
9769 : : builtin_decl_explicit (BUILT_IN_CALLOC),
9770 : : 2, build_one_cst (size_type_node),
9771 : : size_in_bytes);
9772 : 0 : tmp = fold_convert (TREE_TYPE (comp), tmp);
9773 : 0 : gfc_add_modify (block, comp, tmp);
9774 : : }
9775 : : else
9776 : : {
9777 : 351 : tmp = build_call_expr_loc (input_location,
9778 : : builtin_decl_explicit (BUILT_IN_MALLOC),
9779 : : 1, size_in_bytes);
9780 : 351 : if (GFC_CLASS_TYPE_P (TREE_TYPE (comp)))
9781 : 78 : ptr = gfc_class_data_get (comp);
9782 : : else
9783 : : ptr = comp;
9784 : 351 : tmp = fold_convert (TREE_TYPE (ptr), tmp);
9785 : 351 : gfc_add_modify (block, ptr, tmp);
9786 : : }
9787 : :
9788 : 351 : if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
9789 : : /* Update the lhs character length. */
9790 : 127 : gfc_add_modify (block, lhs_cl_size,
9791 : 127 : fold_convert (TREE_TYPE (lhs_cl_size), size));
9792 : : }
9793 : :
9794 : :
9795 : : /* Assign a single component of a derived type constructor. */
9796 : :
9797 : : static tree
9798 : 27007 : gfc_trans_subcomponent_assign (tree dest, gfc_component * cm,
9799 : : gfc_expr * expr, bool init)
9800 : : {
9801 : 27007 : gfc_se se;
9802 : 27007 : gfc_se lse;
9803 : 27007 : stmtblock_t block;
9804 : 27007 : tree tmp;
9805 : 27007 : tree vtab;
9806 : :
9807 : 27007 : gfc_start_block (&block);
9808 : :
9809 : 27007 : if (cm->attr.pointer || cm->attr.proc_pointer)
9810 : : {
9811 : : /* Only care about pointers here, not about allocatables. */
9812 : 2523 : gfc_init_se (&se, NULL);
9813 : : /* Pointer component. */
9814 : 2523 : if ((cm->attr.dimension || cm->attr.codimension)
9815 : 640 : && !cm->attr.proc_pointer)
9816 : : {
9817 : : /* Array pointer. */
9818 : 624 : if (expr->expr_type == EXPR_NULL)
9819 : 618 : gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
9820 : : else
9821 : : {
9822 : 6 : se.direct_byref = 1;
9823 : 6 : se.expr = dest;
9824 : 6 : gfc_conv_expr_descriptor (&se, expr);
9825 : 6 : gfc_add_block_to_block (&block, &se.pre);
9826 : 6 : gfc_add_block_to_block (&block, &se.post);
9827 : : }
9828 : : }
9829 : : else
9830 : : {
9831 : : /* Scalar pointers. */
9832 : 1899 : se.want_pointer = 1;
9833 : 1899 : gfc_conv_expr (&se, expr);
9834 : 1899 : gfc_add_block_to_block (&block, &se.pre);
9835 : :
9836 : 1899 : if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
9837 : 12 : && expr->symtree->n.sym->attr.dummy)
9838 : 12 : se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
9839 : :
9840 : 1899 : gfc_add_modify (&block, dest,
9841 : 1899 : fold_convert (TREE_TYPE (dest), se.expr));
9842 : 1899 : gfc_add_block_to_block (&block, &se.post);
9843 : : }
9844 : : }
9845 : 24484 : else if (cm->ts.type == BT_CLASS && expr->expr_type == EXPR_NULL)
9846 : : {
9847 : : /* NULL initialization for CLASS components. */
9848 : 916 : tmp = gfc_trans_structure_assign (dest,
9849 : : gfc_class_initializer (&cm->ts, expr),
9850 : : false);
9851 : 916 : gfc_add_expr_to_block (&block, tmp);
9852 : : }
9853 : 23568 : else if ((cm->attr.dimension || cm->attr.codimension)
9854 : : && !cm->attr.proc_pointer)
9855 : : {
9856 : 4168 : if (cm->attr.allocatable && expr->expr_type == EXPR_NULL)
9857 : : {
9858 : 2303 : gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
9859 : 2303 : if (cm->attr.codimension && flag_coarray == GFC_FCOARRAY_LIB)
9860 : 1 : gfc_add_modify (&block, gfc_conv_descriptor_token (dest),
9861 : : null_pointer_node);
9862 : : }
9863 : 1865 : else if (cm->attr.allocatable || cm->attr.pdt_array)
9864 : : {
9865 : 932 : tmp = gfc_trans_alloc_subarray_assign (dest, cm, expr);
9866 : 932 : gfc_add_expr_to_block (&block, tmp);
9867 : : }
9868 : : else
9869 : : {
9870 : 933 : tmp = gfc_trans_subarray_assign (dest, cm, expr);
9871 : 933 : gfc_add_expr_to_block (&block, tmp);
9872 : : }
9873 : : }
9874 : 19400 : else if (cm->ts.type == BT_CLASS
9875 : 120 : && CLASS_DATA (cm)->attr.dimension
9876 : 36 : && CLASS_DATA (cm)->attr.allocatable
9877 : 36 : && expr->ts.type == BT_DERIVED)
9878 : : {
9879 : 36 : vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
9880 : 36 : vtab = gfc_build_addr_expr (NULL_TREE, vtab);
9881 : 36 : tmp = gfc_class_vptr_get (dest);
9882 : 36 : gfc_add_modify (&block, tmp,
9883 : 36 : fold_convert (TREE_TYPE (tmp), vtab));
9884 : 36 : tmp = gfc_class_data_get (dest);
9885 : 36 : tmp = gfc_trans_alloc_subarray_assign (tmp, cm, expr);
9886 : 36 : gfc_add_expr_to_block (&block, tmp);
9887 : : }
9888 : 19364 : else if (cm->attr.allocatable && expr->expr_type == EXPR_NULL
9889 : 1636 : && (init
9890 : 1510 : || (cm->ts.type == BT_CHARACTER
9891 : 125 : && !(cm->ts.deferred || cm->attr.pdt_string))))
9892 : : {
9893 : : /* NULL initialization for allocatable components.
9894 : : Deferred-length character is dealt with later. */
9895 : 144 : gfc_add_modify (&block, dest, fold_convert (TREE_TYPE (dest),
9896 : : null_pointer_node));
9897 : : }
9898 : 19220 : else if (init && (cm->attr.allocatable
9899 : 12774 : || (cm->ts.type == BT_CLASS && CLASS_DATA (cm)->attr.allocatable
9900 : 84 : && expr->ts.type != BT_CLASS)))
9901 : : {
9902 : 351 : tree size;
9903 : :
9904 : 351 : gfc_init_se (&se, NULL);
9905 : 351 : gfc_conv_expr (&se, expr);
9906 : :
9907 : : /* The remainder of these instructions follow the if (cm->attr.pointer)
9908 : : if (!cm->attr.dimension) part above. */
9909 : 351 : gfc_add_block_to_block (&block, &se.pre);
9910 : : /* Take care about non-array allocatable components here. The alloc_*
9911 : : routine below is motivated by the alloc_scalar_allocatable_for_
9912 : : assignment() routine, but with the realloc portions removed and
9913 : : different input. */
9914 : 351 : alloc_scalar_allocatable_subcomponent (&block, dest, cm, expr,
9915 : : se.string_length);
9916 : :
9917 : 351 : if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
9918 : 0 : && expr->symtree->n.sym->attr.dummy)
9919 : 0 : se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
9920 : :
9921 : 351 : if (cm->ts.type == BT_CLASS)
9922 : : {
9923 : 78 : tmp = gfc_class_data_get (dest);
9924 : 78 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
9925 : 78 : vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
9926 : 78 : vtab = gfc_build_addr_expr (NULL_TREE, vtab);
9927 : 78 : gfc_add_modify (&block, gfc_class_vptr_get (dest),
9928 : 78 : fold_convert (TREE_TYPE (gfc_class_vptr_get (dest)), vtab));
9929 : : }
9930 : : else
9931 : 273 : tmp = build_fold_indirect_ref_loc (input_location, dest);
9932 : :
9933 : : /* For deferred strings insert a memcpy. */
9934 : 351 : if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
9935 : : {
9936 : 127 : gcc_assert (se.string_length || expr->ts.u.cl->backend_decl);
9937 : 127 : size = size_of_string_in_bytes (cm->ts.kind, se.string_length
9938 : : ? se.string_length
9939 : 0 : : expr->ts.u.cl->backend_decl);
9940 : 127 : tmp = gfc_build_memcpy_call (tmp, se.expr, size);
9941 : 127 : gfc_add_expr_to_block (&block, tmp);
9942 : : }
9943 : 224 : else if (cm->ts.type == BT_CLASS)
9944 : : {
9945 : : /* Fix the expression for memcpy. */
9946 : 78 : if (expr->expr_type != EXPR_VARIABLE)
9947 : 48 : se.expr = gfc_evaluate_now (se.expr, &block);
9948 : :
9949 : 78 : if (expr->ts.type == BT_CHARACTER)
9950 : : {
9951 : 18 : size = build_int_cst (gfc_charlen_type_node, expr->ts.kind);
9952 : 18 : size = fold_build2_loc (input_location, MULT_EXPR,
9953 : : gfc_charlen_type_node,
9954 : : se.string_length, size);
9955 : 18 : size = fold_convert (size_type_node, size);
9956 : : }
9957 : : else
9958 : 60 : size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr->ts));
9959 : :
9960 : : /* Now copy the expression to the constructor component _data. */
9961 : 78 : gfc_add_expr_to_block (&block,
9962 : : gfc_build_memcpy_call (tmp, se.expr, size));
9963 : :
9964 : : /* Fill the unlimited polymorphic _len field. */
9965 : 78 : if (UNLIMITED_POLY (cm) && expr->ts.type == BT_CHARACTER)
9966 : : {
9967 : 18 : tmp = gfc_class_len_get (gfc_get_class_from_expr (tmp));
9968 : 18 : gfc_add_modify (&block, tmp,
9969 : 18 : fold_convert (TREE_TYPE (tmp),
9970 : : se.string_length));
9971 : : }
9972 : : }
9973 : : else
9974 : 146 : gfc_add_modify (&block, tmp,
9975 : 146 : fold_convert (TREE_TYPE (tmp), se.expr));
9976 : 351 : gfc_add_block_to_block (&block, &se.post);
9977 : 351 : }
9978 : 18869 : else if (expr->ts.type == BT_UNION)
9979 : : {
9980 : 13 : tree tmp;
9981 : 13 : gfc_constructor *c = gfc_constructor_first (expr->value.constructor);
9982 : : /* We mark that the entire union should be initialized with a contrived
9983 : : EXPR_NULL expression at the beginning. */
9984 : 13 : if (c != NULL && c->n.component == NULL
9985 : 7 : && c->expr != NULL && c->expr->expr_type == EXPR_NULL)
9986 : : {
9987 : 6 : tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
9988 : 6 : dest, build_constructor (TREE_TYPE (dest), NULL));
9989 : 6 : gfc_add_expr_to_block (&block, tmp);
9990 : 6 : c = gfc_constructor_next (c);
9991 : : }
9992 : : /* The following constructor expression, if any, represents a specific
9993 : : map intializer, as given by the user. */
9994 : 13 : if (c != NULL && c->expr != NULL)
9995 : : {
9996 : 6 : gcc_assert (expr->expr_type == EXPR_STRUCTURE);
9997 : 6 : tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
9998 : 6 : gfc_add_expr_to_block (&block, tmp);
9999 : : }
10000 : : }
10001 : 18856 : else if (expr->ts.type == BT_DERIVED && expr->ts.f90_type != BT_VOID)
10002 : : {
10003 : 2907 : if (expr->expr_type != EXPR_STRUCTURE)
10004 : : {
10005 : 373 : tree dealloc = NULL_TREE;
10006 : 373 : gfc_init_se (&se, NULL);
10007 : 373 : gfc_conv_expr (&se, expr);
10008 : 373 : gfc_add_block_to_block (&block, &se.pre);
10009 : : /* Prevent repeat evaluations in gfc_copy_alloc_comp by fixing the
10010 : : expression in a temporary variable and deallocate the allocatable
10011 : : components. Then we can the copy the expression to the result. */
10012 : 373 : if (cm->ts.u.derived->attr.alloc_comp
10013 : 251 : && expr->expr_type != EXPR_VARIABLE)
10014 : : {
10015 : 221 : se.expr = gfc_evaluate_now (se.expr, &block);
10016 : 221 : dealloc = gfc_deallocate_alloc_comp (cm->ts.u.derived, se.expr,
10017 : : expr->rank);
10018 : : }
10019 : 373 : gfc_add_modify (&block, dest,
10020 : 373 : fold_convert (TREE_TYPE (dest), se.expr));
10021 : 373 : if (cm->ts.u.derived->attr.alloc_comp
10022 : 251 : && expr->expr_type != EXPR_NULL)
10023 : : {
10024 : : // TODO: Fix caf_mode
10025 : 48 : tmp = gfc_copy_alloc_comp (cm->ts.u.derived, se.expr,
10026 : : dest, expr->rank, 0);
10027 : 48 : gfc_add_expr_to_block (&block, tmp);
10028 : 48 : if (dealloc != NULL_TREE)
10029 : 18 : gfc_add_expr_to_block (&block, dealloc);
10030 : : }
10031 : 373 : gfc_add_block_to_block (&block, &se.post);
10032 : : }
10033 : : else
10034 : : {
10035 : : /* Nested constructors. */
10036 : 2534 : tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
10037 : 2534 : gfc_add_expr_to_block (&block, tmp);
10038 : : }
10039 : : }
10040 : 15949 : else if (gfc_deferred_strlen (cm, &tmp))
10041 : : {
10042 : 119 : tree strlen;
10043 : 119 : strlen = tmp;
10044 : 119 : gcc_assert (strlen);
10045 : 119 : strlen = fold_build3_loc (input_location, COMPONENT_REF,
10046 : 119 : TREE_TYPE (strlen),
10047 : 119 : TREE_OPERAND (dest, 0),
10048 : : strlen, NULL_TREE);
10049 : :
10050 : 119 : if (expr->expr_type == EXPR_NULL)
10051 : : {
10052 : 107 : tmp = build_int_cst (TREE_TYPE (cm->backend_decl), 0);
10053 : 107 : gfc_add_modify (&block, dest, tmp);
10054 : 107 : tmp = build_int_cst (TREE_TYPE (strlen), 0);
10055 : 107 : gfc_add_modify (&block, strlen, tmp);
10056 : : }
10057 : : else
10058 : : {
10059 : 12 : tree size;
10060 : 12 : gfc_init_se (&se, NULL);
10061 : 12 : gfc_conv_expr (&se, expr);
10062 : 12 : size = size_of_string_in_bytes (cm->ts.kind, se.string_length);
10063 : 12 : size = fold_convert (size_type_node, size);
10064 : 12 : tmp = build_call_expr_loc (input_location,
10065 : : builtin_decl_explicit (BUILT_IN_MALLOC),
10066 : : 1, size);
10067 : 12 : gfc_add_modify (&block, dest,
10068 : 12 : fold_convert (TREE_TYPE (dest), tmp));
10069 : 12 : gfc_add_modify (&block, strlen,
10070 : 12 : fold_convert (TREE_TYPE (strlen), se.string_length));
10071 : 12 : tmp = gfc_build_memcpy_call (dest, se.expr, size);
10072 : 12 : gfc_add_expr_to_block (&block, tmp);
10073 : : }
10074 : : }
10075 : 15830 : else if (!cm->attr.artificial)
10076 : : {
10077 : : /* Scalar component (excluding deferred parameters). */
10078 : 15715 : gfc_init_se (&se, NULL);
10079 : 15715 : gfc_init_se (&lse, NULL);
10080 : :
10081 : 15715 : gfc_conv_expr (&se, expr);
10082 : 15715 : if (cm->ts.type == BT_CHARACTER)
10083 : 1047 : lse.string_length = cm->ts.u.cl->backend_decl;
10084 : 15715 : lse.expr = dest;
10085 : 15715 : tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, false, false);
10086 : 15715 : gfc_add_expr_to_block (&block, tmp);
10087 : : }
10088 : 27007 : return gfc_finish_block (&block);
10089 : : }
10090 : :
10091 : : /* Assign a derived type constructor to a variable. */
10092 : :
10093 : : tree
10094 : 19070 : gfc_trans_structure_assign (tree dest, gfc_expr * expr, bool init, bool coarray)
10095 : : {
10096 : 19070 : gfc_constructor *c;
10097 : 19070 : gfc_component *cm;
10098 : 19070 : stmtblock_t block;
10099 : 19070 : tree field;
10100 : 19070 : tree tmp;
10101 : 19070 : gfc_se se;
10102 : :
10103 : 19070 : gfc_start_block (&block);
10104 : :
10105 : 19070 : if (expr->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
10106 : 172 : && (expr->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
10107 : 9 : || expr->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
10108 : : {
10109 : 172 : gfc_se lse;
10110 : :
10111 : 172 : gfc_init_se (&se, NULL);
10112 : 172 : gfc_init_se (&lse, NULL);
10113 : 172 : gfc_conv_expr (&se, gfc_constructor_first (expr->value.constructor)->expr);
10114 : 172 : lse.expr = dest;
10115 : 172 : gfc_add_modify (&block, lse.expr,
10116 : 172 : fold_convert (TREE_TYPE (lse.expr), se.expr));
10117 : :
10118 : 172 : return gfc_finish_block (&block);
10119 : : }
10120 : :
10121 : : /* Make sure that the derived type has been completely built. */
10122 : 18898 : if (!expr->ts.u.derived->backend_decl
10123 : 18898 : || !TYPE_FIELDS (expr->ts.u.derived->backend_decl))
10124 : : {
10125 : 223 : tmp = gfc_typenode_for_spec (&expr->ts);
10126 : 223 : gcc_assert (tmp);
10127 : : }
10128 : :
10129 : 18898 : cm = expr->ts.u.derived->components;
10130 : :
10131 : :
10132 : 18898 : if (coarray)
10133 : 168 : gfc_init_se (&se, NULL);
10134 : :
10135 : 18898 : for (c = gfc_constructor_first (expr->value.constructor);
10136 : 48908 : c; c = gfc_constructor_next (c), cm = cm->next)
10137 : : {
10138 : : /* Skip absent members in default initializers. */
10139 : 30010 : if (!c->expr && !cm->attr.allocatable)
10140 : 3003 : continue;
10141 : :
10142 : : /* Register the component with the caf-lib before it is initialized.
10143 : : Register only allocatable components, that are not coarray'ed
10144 : : components (%comp[*]). Only register when the constructor is the
10145 : : null-expression. */
10146 : 27007 : if (coarray && !cm->attr.codimension
10147 : 380 : && (cm->attr.allocatable || cm->attr.pointer)
10148 : 169 : && (!c->expr || c->expr->expr_type == EXPR_NULL))
10149 : : {
10150 : 168 : tree token, desc, size;
10151 : 336 : bool is_array = cm->ts.type == BT_CLASS
10152 : 168 : ? CLASS_DATA (cm)->attr.dimension : cm->attr.dimension;
10153 : :
10154 : 168 : field = cm->backend_decl;
10155 : 168 : field = fold_build3_loc (input_location, COMPONENT_REF,
10156 : 168 : TREE_TYPE (field), dest, field, NULL_TREE);
10157 : 168 : if (cm->ts.type == BT_CLASS)
10158 : 0 : field = gfc_class_data_get (field);
10159 : :
10160 : 168 : token
10161 : : = is_array
10162 : 168 : ? gfc_conv_descriptor_token (field)
10163 : 49 : : fold_build3_loc (input_location, COMPONENT_REF,
10164 : 49 : TREE_TYPE (gfc_comp_caf_token (cm)), dest,
10165 : 49 : gfc_comp_caf_token (cm), NULL_TREE);
10166 : :
10167 : 168 : if (is_array)
10168 : : {
10169 : : /* The _caf_register routine looks at the rank of the array
10170 : : descriptor to decide whether the data registered is an array
10171 : : or not. */
10172 : 119 : int rank = cm->ts.type == BT_CLASS ? CLASS_DATA (cm)->as->rank
10173 : 119 : : cm->as->rank;
10174 : : /* When the rank is not known just set a positive rank, which
10175 : : suffices to recognize the data as array. */
10176 : 119 : if (rank < 0)
10177 : 0 : rank = 1;
10178 : 119 : size = build_zero_cst (size_type_node);
10179 : 119 : desc = field;
10180 : 119 : gfc_add_modify (&block, gfc_conv_descriptor_rank (desc),
10181 : 119 : build_int_cst (signed_char_type_node, rank));
10182 : : }
10183 : : else
10184 : : {
10185 : 49 : desc = gfc_conv_scalar_to_descriptor (&se, field,
10186 : 49 : cm->ts.type == BT_CLASS
10187 : 49 : ? CLASS_DATA (cm)->attr
10188 : : : cm->attr);
10189 : 49 : size = TYPE_SIZE_UNIT (TREE_TYPE (field));
10190 : : }
10191 : 168 : gfc_add_block_to_block (&block, &se.pre);
10192 : 168 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_register,
10193 : : 7, size, build_int_cst (
10194 : : integer_type_node,
10195 : : GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY),
10196 : : gfc_build_addr_expr (pvoid_type_node,
10197 : : token),
10198 : : gfc_build_addr_expr (NULL_TREE, desc),
10199 : : null_pointer_node, null_pointer_node,
10200 : : integer_zero_node);
10201 : 168 : gfc_add_expr_to_block (&block, tmp);
10202 : : }
10203 : 27007 : field = cm->backend_decl;
10204 : 27007 : gcc_assert(field);
10205 : 27007 : tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
10206 : : dest, field, NULL_TREE);
10207 : 27007 : if (!c->expr)
10208 : : {
10209 : 0 : gfc_expr *e = gfc_get_null_expr (NULL);
10210 : 0 : tmp = gfc_trans_subcomponent_assign (tmp, cm, e, init);
10211 : 0 : gfc_free_expr (e);
10212 : : }
10213 : : else
10214 : 27007 : tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr, init);
10215 : 27007 : gfc_add_expr_to_block (&block, tmp);
10216 : : }
10217 : 18898 : return gfc_finish_block (&block);
10218 : : }
10219 : :
10220 : : static void
10221 : 21 : gfc_conv_union_initializer (vec<constructor_elt, va_gc> *&v,
10222 : : gfc_component *un, gfc_expr *init)
10223 : : {
10224 : 21 : gfc_constructor *ctor;
10225 : :
10226 : 21 : if (un->ts.type != BT_UNION || un == NULL || init == NULL)
10227 : : return;
10228 : :
10229 : 21 : ctor = gfc_constructor_first (init->value.constructor);
10230 : :
10231 : 21 : if (ctor == NULL || ctor->expr == NULL)
10232 : : return;
10233 : :
10234 : 21 : gcc_assert (init->expr_type == EXPR_STRUCTURE);
10235 : :
10236 : : /* If we have an 'initialize all' constructor, do it first. */
10237 : 21 : if (ctor->expr->expr_type == EXPR_NULL)
10238 : : {
10239 : 9 : tree union_type = TREE_TYPE (un->backend_decl);
10240 : 9 : tree val = build_constructor (union_type, NULL);
10241 : 9 : CONSTRUCTOR_APPEND_ELT (v, un->backend_decl, val);
10242 : 9 : ctor = gfc_constructor_next (ctor);
10243 : : }
10244 : :
10245 : : /* Add the map initializer on top. */
10246 : 21 : if (ctor != NULL && ctor->expr != NULL)
10247 : : {
10248 : 12 : gcc_assert (ctor->expr->expr_type == EXPR_STRUCTURE);
10249 : 12 : tree val = gfc_conv_initializer (ctor->expr, &un->ts,
10250 : 12 : TREE_TYPE (un->backend_decl),
10251 : 12 : un->attr.dimension, un->attr.pointer,
10252 : 12 : un->attr.proc_pointer);
10253 : 12 : CONSTRUCTOR_APPEND_ELT (v, un->backend_decl, val);
10254 : : }
10255 : : }
10256 : :
10257 : : /* Build an expression for a constructor. If init is nonzero then
10258 : : this is part of a static variable initializer. */
10259 : :
10260 : : void
10261 : 36363 : gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
10262 : : {
10263 : 36363 : gfc_constructor *c;
10264 : 36363 : gfc_component *cm;
10265 : 36363 : tree val;
10266 : 36363 : tree type;
10267 : 36363 : tree tmp;
10268 : 36363 : vec<constructor_elt, va_gc> *v = NULL;
10269 : :
10270 : 36363 : gcc_assert (se->ss == NULL);
10271 : 36363 : gcc_assert (expr->expr_type == EXPR_STRUCTURE);
10272 : 36363 : type = gfc_typenode_for_spec (&expr->ts);
10273 : :
10274 : 36363 : if (!init)
10275 : : {
10276 : : /* Create a temporary variable and fill it in. */
10277 : 15026 : se->expr = gfc_create_var (type, expr->ts.u.derived->name);
10278 : : /* The symtree in expr is NULL, if the code to generate is for
10279 : : initializing the static members only. */
10280 : 30052 : tmp = gfc_trans_structure_assign (se->expr, expr, expr->symtree != NULL,
10281 : 15026 : se->want_coarray);
10282 : 15026 : gfc_add_expr_to_block (&se->pre, tmp);
10283 : 15026 : return;
10284 : : }
10285 : :
10286 : 21337 : cm = expr->ts.u.derived->components;
10287 : :
10288 : 21337 : for (c = gfc_constructor_first (expr->value.constructor);
10289 : 112673 : c && cm; c = gfc_constructor_next (c), cm = cm->next)
10290 : : {
10291 : : /* Skip absent members in default initializers and allocatable
10292 : : components. Although the latter have a default initializer
10293 : : of EXPR_NULL,... by default, the static nullify is not needed
10294 : : since this is done every time we come into scope. */
10295 : 91336 : if (!c->expr || (cm->attr.allocatable && cm->attr.flavor != FL_PROCEDURE))
10296 : 7722 : continue;
10297 : :
10298 : 83614 : if (cm->initializer && cm->initializer->expr_type != EXPR_NULL
10299 : 48235 : && strcmp (cm->name, "_extends") == 0
10300 : 1254 : && cm->initializer->symtree)
10301 : : {
10302 : 1254 : tree vtab;
10303 : 1254 : gfc_symbol *vtabs;
10304 : 1254 : vtabs = cm->initializer->symtree->n.sym;
10305 : 1254 : vtab = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtabs));
10306 : 1254 : vtab = unshare_expr_without_location (vtab);
10307 : 1254 : CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, vtab);
10308 : 1254 : }
10309 : 82360 : else if (cm->ts.u.derived && strcmp (cm->name, "_size") == 0)
10310 : : {
10311 : 9121 : val = TYPE_SIZE_UNIT (gfc_get_derived_type (cm->ts.u.derived));
10312 : 9121 : CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
10313 : : fold_convert (TREE_TYPE (cm->backend_decl),
10314 : : val));
10315 : 9121 : }
10316 : 73239 : else if (cm->ts.type == BT_INTEGER && strcmp (cm->name, "_len") == 0)
10317 : 387 : CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
10318 : : fold_convert (TREE_TYPE (cm->backend_decl),
10319 : 387 : integer_zero_node));
10320 : 72852 : else if (cm->ts.type == BT_UNION)
10321 : 21 : gfc_conv_union_initializer (v, cm, c->expr);
10322 : : else
10323 : : {
10324 : 72831 : val = gfc_conv_initializer (c->expr, &cm->ts,
10325 : 72831 : TREE_TYPE (cm->backend_decl),
10326 : : cm->attr.dimension, cm->attr.pointer,
10327 : 72831 : cm->attr.proc_pointer);
10328 : 72831 : val = unshare_expr_without_location (val);
10329 : :
10330 : : /* Append it to the constructor list. */
10331 : 164167 : CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
10332 : : }
10333 : : }
10334 : :
10335 : 21337 : se->expr = build_constructor (type, v);
10336 : 21337 : if (init)
10337 : 21337 : TREE_CONSTANT (se->expr) = 1;
10338 : : }
10339 : :
10340 : :
10341 : : /* Translate a substring expression. */
10342 : :
10343 : : static void
10344 : 258 : gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
10345 : : {
10346 : 258 : gfc_ref *ref;
10347 : :
10348 : 258 : ref = expr->ref;
10349 : :
10350 : 258 : gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
10351 : :
10352 : 516 : se->expr = gfc_build_wide_string_const (expr->ts.kind,
10353 : 258 : expr->value.character.length,
10354 : 258 : expr->value.character.string);
10355 : :
10356 : 258 : se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
10357 : 258 : TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
10358 : :
10359 : 258 : if (ref)
10360 : 258 : gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
10361 : 258 : }
10362 : :
10363 : :
10364 : : /* Entry point for expression translation. Evaluates a scalar quantity.
10365 : : EXPR is the expression to be translated, and SE is the state structure if
10366 : : called from within the scalarized. */
10367 : :
10368 : : void
10369 : 3502985 : gfc_conv_expr (gfc_se * se, gfc_expr * expr)
10370 : : {
10371 : 3502985 : gfc_ss *ss;
10372 : :
10373 : 3502985 : ss = se->ss;
10374 : 3502985 : if (ss && ss->info->expr == expr
10375 : 229521 : && (ss->info->type == GFC_SS_SCALAR
10376 : : || ss->info->type == GFC_SS_REFERENCE))
10377 : : {
10378 : 38762 : gfc_ss_info *ss_info;
10379 : :
10380 : 38762 : ss_info = ss->info;
10381 : : /* Substitute a scalar expression evaluated outside the scalarization
10382 : : loop. */
10383 : 38762 : se->expr = ss_info->data.scalar.value;
10384 : 38762 : if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
10385 : 824 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
10386 : :
10387 : 38762 : se->string_length = ss_info->string_length;
10388 : 38762 : gfc_advance_se_ss_chain (se);
10389 : 38762 : return;
10390 : : }
10391 : :
10392 : : /* We need to convert the expressions for the iso_c_binding derived types.
10393 : : C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
10394 : : null_pointer_node. C_PTR and C_FUNPTR are converted to match the
10395 : : typespec for the C_PTR and C_FUNPTR symbols, which has already been
10396 : : updated to be an integer with a kind equal to the size of a (void *). */
10397 : 3464223 : if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->ts.f90_type == BT_VOID
10398 : 15689 : && expr->ts.u.derived->attr.is_bind_c)
10399 : : {
10400 : 14860 : if (expr->expr_type == EXPR_VARIABLE
10401 : 10644 : && (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
10402 : 10644 : || expr->symtree->n.sym->intmod_sym_id
10403 : : == ISOCBINDING_NULL_FUNPTR))
10404 : : {
10405 : : /* Set expr_type to EXPR_NULL, which will result in
10406 : : null_pointer_node being used below. */
10407 : 0 : expr->expr_type = EXPR_NULL;
10408 : : }
10409 : : else
10410 : : {
10411 : : /* Update the type/kind of the expression to be what the new
10412 : : type/kind are for the updated symbols of C_PTR/C_FUNPTR. */
10413 : 14860 : expr->ts.type = BT_INTEGER;
10414 : 14860 : expr->ts.f90_type = BT_VOID;
10415 : 14860 : expr->ts.kind = gfc_index_integer_kind;
10416 : : }
10417 : : }
10418 : :
10419 : 3464223 : gfc_fix_class_refs (expr);
10420 : :
10421 : 3464223 : switch (expr->expr_type)
10422 : : {
10423 : 489976 : case EXPR_OP:
10424 : 489976 : gfc_conv_expr_op (se, expr);
10425 : 489976 : break;
10426 : :
10427 : 290352 : case EXPR_FUNCTION:
10428 : 290352 : gfc_conv_function_expr (se, expr);
10429 : 290352 : break;
10430 : :
10431 : 1094694 : case EXPR_CONSTANT:
10432 : 1094694 : gfc_conv_constant (se, expr);
10433 : 1094694 : break;
10434 : :
10435 : 1535481 : case EXPR_VARIABLE:
10436 : 1535481 : gfc_conv_variable (se, expr);
10437 : 1535481 : break;
10438 : :
10439 : 4001 : case EXPR_NULL:
10440 : 4001 : se->expr = null_pointer_node;
10441 : 4001 : break;
10442 : :
10443 : 258 : case EXPR_SUBSTRING:
10444 : 258 : gfc_conv_substring_expr (se, expr);
10445 : 258 : break;
10446 : :
10447 : 15026 : case EXPR_STRUCTURE:
10448 : 15026 : gfc_conv_structure (se, expr, 0);
10449 : : /* F2008 4.5.6.3 para 5: If an executable construct references a
10450 : : structure constructor or array constructor, the entity created by
10451 : : the constructor is finalized after execution of the innermost
10452 : : executable construct containing the reference. This, in fact,
10453 : : was later deleted by the Combined Techical Corrigenda 1 TO 4 for
10454 : : fortran 2008 (f08/0011). */
10455 : 15026 : if ((gfc_option.allow_std & (GFC_STD_F2008 | GFC_STD_F2003))
10456 : 15026 : && !(gfc_option.allow_std & GFC_STD_GNU)
10457 : 139 : && expr->must_finalize
10458 : 15038 : && gfc_may_be_finalized (expr->ts))
10459 : : {
10460 : 12 : locus loc;
10461 : 12 : gfc_locus_from_location (&loc, input_location);
10462 : 12 : gfc_warning (0, "The structure constructor at %L has been"
10463 : : " finalized. This feature was removed by f08/0011."
10464 : : " Use -std=f2018 or -std=gnu to eliminate the"
10465 : : " finalization.", &loc);
10466 : 12 : symbol_attribute attr;
10467 : 12 : attr.allocatable = attr.pointer = 0;
10468 : 12 : gfc_finalize_tree_expr (se, expr->ts.u.derived, attr, 0);
10469 : 12 : gfc_add_block_to_block (&se->post, &se->finalblock);
10470 : : }
10471 : : break;
10472 : :
10473 : 34435 : case EXPR_ARRAY:
10474 : 34435 : gfc_conv_array_constructor_expr (se, expr);
10475 : 34435 : gfc_add_block_to_block (&se->post, &se->finalblock);
10476 : 34435 : break;
10477 : :
10478 : 0 : default:
10479 : 0 : gcc_unreachable ();
10480 : 3502985 : break;
10481 : : }
10482 : : }
10483 : :
10484 : : /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
10485 : : of an assignment. */
10486 : : void
10487 : 356564 : gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
10488 : : {
10489 : 356564 : gfc_conv_expr (se, expr);
10490 : : /* All numeric lvalues should have empty post chains. If not we need to
10491 : : figure out a way of rewriting an lvalue so that it has no post chain. */
10492 : 356564 : gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
10493 : 356564 : }
10494 : :
10495 : : /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
10496 : : numeric expressions. Used for scalar values where inserting cleanup code
10497 : : is inconvenient. */
10498 : : void
10499 : 992519 : gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
10500 : : {
10501 : 992519 : tree val;
10502 : :
10503 : 992519 : gcc_assert (expr->ts.type != BT_CHARACTER);
10504 : 992519 : gfc_conv_expr (se, expr);
10505 : 992519 : if (se->post.head)
10506 : : {
10507 : 2435 : val = gfc_create_var (TREE_TYPE (se->expr), NULL);
10508 : 2435 : gfc_add_modify (&se->pre, val, se->expr);
10509 : 2435 : se->expr = val;
10510 : 2435 : gfc_add_block_to_block (&se->pre, &se->post);
10511 : : }
10512 : 992519 : }
10513 : :
10514 : : /* Helper to translate an expression and convert it to a particular type. */
10515 : : void
10516 : 274580 : gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
10517 : : {
10518 : 274580 : gfc_conv_expr_val (se, expr);
10519 : 274580 : se->expr = convert (type, se->expr);
10520 : 274580 : }
10521 : :
10522 : :
10523 : : /* Converts an expression so that it can be passed by reference. Scalar
10524 : : values only. */
10525 : :
10526 : : void
10527 : 220552 : gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
10528 : : {
10529 : 220552 : gfc_ss *ss;
10530 : 220552 : tree var;
10531 : :
10532 : 220552 : ss = se->ss;
10533 : 220552 : if (ss && ss->info->expr == expr
10534 : 7154 : && ss->info->type == GFC_SS_REFERENCE)
10535 : : {
10536 : : /* Returns a reference to the scalar evaluated outside the loop
10537 : : for this case. */
10538 : 906 : gfc_conv_expr (se, expr);
10539 : :
10540 : 906 : if (expr->ts.type == BT_CHARACTER
10541 : 114 : && expr->expr_type != EXPR_FUNCTION)
10542 : 102 : gfc_conv_string_parameter (se);
10543 : : else
10544 : 804 : se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
10545 : :
10546 : 906 : return;
10547 : : }
10548 : :
10549 : 219646 : if (expr->ts.type == BT_CHARACTER)
10550 : : {
10551 : 48649 : gfc_conv_expr (se, expr);
10552 : 48649 : gfc_conv_string_parameter (se);
10553 : 48649 : return;
10554 : : }
10555 : :
10556 : 170997 : if (expr->expr_type == EXPR_VARIABLE)
10557 : : {
10558 : 67762 : se->want_pointer = 1;
10559 : 67762 : gfc_conv_expr (se, expr);
10560 : 67762 : if (se->post.head)
10561 : : {
10562 : 0 : var = gfc_create_var (TREE_TYPE (se->expr), NULL);
10563 : 0 : gfc_add_modify (&se->pre, var, se->expr);
10564 : 0 : gfc_add_block_to_block (&se->pre, &se->post);
10565 : 0 : se->expr = var;
10566 : : }
10567 : 67762 : return;
10568 : : }
10569 : :
10570 : 103235 : if (expr->expr_type == EXPR_FUNCTION
10571 : 12980 : && ((expr->value.function.esym
10572 : 2026 : && expr->value.function.esym->result
10573 : 2025 : && expr->value.function.esym->result->attr.pointer
10574 : 71 : && !expr->value.function.esym->result->attr.dimension)
10575 : 12915 : || (!expr->value.function.esym && !expr->ref
10576 : 10848 : && expr->symtree->n.sym->attr.pointer
10577 : 0 : && !expr->symtree->n.sym->attr.dimension)))
10578 : : {
10579 : 65 : se->want_pointer = 1;
10580 : 65 : gfc_conv_expr (se, expr);
10581 : 65 : var = gfc_create_var (TREE_TYPE (se->expr), NULL);
10582 : 65 : gfc_add_modify (&se->pre, var, se->expr);
10583 : 65 : se->expr = var;
10584 : 65 : return;
10585 : : }
10586 : :
10587 : 103170 : gfc_conv_expr (se, expr);
10588 : :
10589 : : /* Create a temporary var to hold the value. */
10590 : 103170 : if (TREE_CONSTANT (se->expr))
10591 : : {
10592 : : tree tmp = se->expr;
10593 : 82360 : STRIP_TYPE_NOPS (tmp);
10594 : 82360 : var = build_decl (input_location,
10595 : 82360 : CONST_DECL, NULL, TREE_TYPE (tmp));
10596 : 82360 : DECL_INITIAL (var) = tmp;
10597 : 82360 : TREE_STATIC (var) = 1;
10598 : 82360 : pushdecl (var);
10599 : : }
10600 : : else
10601 : : {
10602 : 20810 : var = gfc_create_var (TREE_TYPE (se->expr), NULL);
10603 : 20810 : gfc_add_modify (&se->pre, var, se->expr);
10604 : : }
10605 : :
10606 : 103170 : if (!expr->must_finalize)
10607 : 103080 : gfc_add_block_to_block (&se->pre, &se->post);
10608 : :
10609 : : /* Take the address of that value. */
10610 : 103170 : se->expr = gfc_build_addr_expr (NULL_TREE, var);
10611 : : }
10612 : :
10613 : :
10614 : : /* Get the _len component for an unlimited polymorphic expression. */
10615 : :
10616 : : static tree
10617 : 1660 : trans_get_upoly_len (stmtblock_t *block, gfc_expr *expr)
10618 : : {
10619 : 1660 : gfc_se se;
10620 : 1660 : gfc_ref *ref = expr->ref;
10621 : :
10622 : 1660 : gfc_init_se (&se, NULL);
10623 : 3410 : while (ref && ref->next)
10624 : : ref = ref->next;
10625 : 1660 : gfc_add_len_component (expr);
10626 : 1660 : gfc_conv_expr (&se, expr);
10627 : 1660 : gfc_add_block_to_block (block, &se.pre);
10628 : 1660 : gcc_assert (se.post.head == NULL_TREE);
10629 : 1660 : if (ref)
10630 : : {
10631 : 238 : gfc_free_ref_list (ref->next);
10632 : 238 : ref->next = NULL;
10633 : : }
10634 : : else
10635 : : {
10636 : 1422 : gfc_free_ref_list (expr->ref);
10637 : 1422 : expr->ref = NULL;
10638 : : }
10639 : 1660 : return se.expr;
10640 : : }
10641 : :
10642 : :
10643 : : /* Assign _vptr and _len components as appropriate. BLOCK should be a
10644 : : statement-list outside of the scalarizer-loop. When code is generated, that
10645 : : depends on the scalarized expression, it is added to RSE.PRE.
10646 : : Returns le's _vptr tree and when set the len expressions in to_lenp and
10647 : : from_lenp to form a le%_vptr%_copy (re, le, [from_lenp, to_lenp])
10648 : : expression. */
10649 : :
10650 : : static tree
10651 : 4327 : trans_class_vptr_len_assignment (stmtblock_t *block, gfc_expr * le,
10652 : : gfc_expr * re, gfc_se *rse,
10653 : : tree * to_lenp, tree * from_lenp,
10654 : : tree * from_vptrp)
10655 : : {
10656 : 4327 : gfc_se se;
10657 : 4327 : gfc_expr * vptr_expr;
10658 : 4327 : tree tmp, to_len = NULL_TREE, from_len = NULL_TREE, lhs_vptr;
10659 : 4327 : bool set_vptr = false, temp_rhs = false;
10660 : 4327 : stmtblock_t *pre = block;
10661 : 4327 : tree class_expr = NULL_TREE;
10662 : 4327 : tree from_vptr = NULL_TREE;
10663 : :
10664 : : /* Create a temporary for complicated expressions. */
10665 : 4327 : if (re->expr_type != EXPR_VARIABLE && re->expr_type != EXPR_NULL
10666 : 1172 : && rse->expr != NULL_TREE)
10667 : : {
10668 : 1172 : if (!DECL_P (rse->expr))
10669 : : {
10670 : 330 : if (re->ts.type == BT_CLASS && !GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
10671 : 37 : class_expr = gfc_get_class_from_expr (rse->expr);
10672 : :
10673 : 330 : if (rse->loop)
10674 : 111 : pre = &rse->loop->pre;
10675 : : else
10676 : 219 : pre = &rse->pre;
10677 : :
10678 : 330 : if (class_expr != NULL_TREE && UNLIMITED_POLY (re))
10679 : 37 : tmp = gfc_evaluate_now (TREE_OPERAND (rse->expr, 0), &rse->pre);
10680 : : else
10681 : 293 : tmp = gfc_evaluate_now (rse->expr, &rse->pre);
10682 : :
10683 : 330 : rse->expr = tmp;
10684 : : }
10685 : : else
10686 : 842 : pre = &rse->pre;
10687 : :
10688 : : temp_rhs = true;
10689 : : }
10690 : :
10691 : : /* Get the _vptr for the left-hand side expression. */
10692 : 4327 : gfc_init_se (&se, NULL);
10693 : 4327 : vptr_expr = gfc_find_and_cut_at_last_class_ref (le);
10694 : 4327 : if (vptr_expr != NULL && gfc_expr_attr (vptr_expr).class_ok)
10695 : : {
10696 : : /* Care about _len for unlimited polymorphic entities. */
10697 : 4309 : if (UNLIMITED_POLY (vptr_expr)
10698 : 3417 : || (vptr_expr->ts.type == BT_DERIVED
10699 : 2427 : && vptr_expr->ts.u.derived->attr.unlimited_polymorphic))
10700 : 1376 : to_len = trans_get_upoly_len (block, vptr_expr);
10701 : 4309 : gfc_add_vptr_component (vptr_expr);
10702 : 4309 : set_vptr = true;
10703 : : }
10704 : : else
10705 : 18 : vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&le->ts));
10706 : 4327 : se.want_pointer = 1;
10707 : 4327 : gfc_conv_expr (&se, vptr_expr);
10708 : 4327 : gfc_free_expr (vptr_expr);
10709 : 4327 : gfc_add_block_to_block (block, &se.pre);
10710 : 4327 : gcc_assert (se.post.head == NULL_TREE);
10711 : 4327 : lhs_vptr = se.expr;
10712 : 4327 : STRIP_NOPS (lhs_vptr);
10713 : :
10714 : : /* Set the _vptr only when the left-hand side of the assignment is a
10715 : : class-object. */
10716 : 4327 : if (set_vptr)
10717 : : {
10718 : : /* Get the vptr from the rhs expression only, when it is variable.
10719 : : Functions are expected to be assigned to a temporary beforehand. */
10720 : 3023 : vptr_expr = (re->expr_type == EXPR_VARIABLE && re->ts.type == BT_CLASS)
10721 : 5058 : ? gfc_find_and_cut_at_last_class_ref (re)
10722 : : : NULL;
10723 : 749 : if (vptr_expr != NULL && vptr_expr->ts.type == BT_CLASS)
10724 : : {
10725 : 749 : if (to_len != NULL_TREE)
10726 : : {
10727 : : /* Get the _len information from the rhs. */
10728 : 299 : if (UNLIMITED_POLY (vptr_expr)
10729 : : || (vptr_expr->ts.type == BT_DERIVED
10730 : : && vptr_expr->ts.u.derived->attr.unlimited_polymorphic))
10731 : 272 : from_len = trans_get_upoly_len (block, vptr_expr);
10732 : : }
10733 : 749 : gfc_add_vptr_component (vptr_expr);
10734 : : }
10735 : : else
10736 : : {
10737 : 3560 : if (re->expr_type == EXPR_VARIABLE
10738 : 2274 : && DECL_P (re->symtree->n.sym->backend_decl)
10739 : 2274 : && DECL_LANG_SPECIFIC (re->symtree->n.sym->backend_decl)
10740 : 819 : && GFC_DECL_SAVED_DESCRIPTOR (re->symtree->n.sym->backend_decl)
10741 : 3627 : && GFC_CLASS_TYPE_P (TREE_TYPE (GFC_DECL_SAVED_DESCRIPTOR (
10742 : : re->symtree->n.sym->backend_decl))))
10743 : : {
10744 : 43 : vptr_expr = NULL;
10745 : 43 : se.expr = gfc_class_vptr_get (GFC_DECL_SAVED_DESCRIPTOR (
10746 : : re->symtree->n.sym->backend_decl));
10747 : 43 : if (to_len && UNLIMITED_POLY (re))
10748 : 0 : from_len = gfc_class_len_get (GFC_DECL_SAVED_DESCRIPTOR (
10749 : : re->symtree->n.sym->backend_decl));
10750 : : }
10751 : 3517 : else if (temp_rhs && re->ts.type == BT_CLASS)
10752 : : {
10753 : 213 : vptr_expr = NULL;
10754 : 213 : if (class_expr)
10755 : : tmp = class_expr;
10756 : 176 : else if (!GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
10757 : 0 : tmp = gfc_get_class_from_expr (rse->expr);
10758 : : else
10759 : : tmp = rse->expr;
10760 : :
10761 : 213 : se.expr = gfc_class_vptr_get (tmp);
10762 : 213 : from_vptr = se.expr;
10763 : 213 : if (UNLIMITED_POLY (re))
10764 : 73 : from_len = gfc_class_len_get (tmp);
10765 : :
10766 : : }
10767 : 3304 : else if (re->expr_type != EXPR_NULL)
10768 : : /* Only when rhs is non-NULL use its declared type for vptr
10769 : : initialisation. */
10770 : 3178 : vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&re->ts));
10771 : : else
10772 : : /* When the rhs is NULL use the vtab of lhs' declared type. */
10773 : 126 : vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&le->ts));
10774 : : }
10775 : :
10776 : 4126 : if (vptr_expr)
10777 : : {
10778 : 4053 : gfc_init_se (&se, NULL);
10779 : 4053 : se.want_pointer = 1;
10780 : 4053 : gfc_conv_expr (&se, vptr_expr);
10781 : 4053 : gfc_free_expr (vptr_expr);
10782 : 4053 : gfc_add_block_to_block (block, &se.pre);
10783 : 4053 : gcc_assert (se.post.head == NULL_TREE);
10784 : 4053 : from_vptr = se.expr;
10785 : : }
10786 : 4309 : gfc_add_modify (pre, lhs_vptr, fold_convert (TREE_TYPE (lhs_vptr),
10787 : : se.expr));
10788 : :
10789 : 4309 : if (to_len != NULL_TREE)
10790 : : {
10791 : : /* The _len component needs to be set. Figure how to get the
10792 : : value of the right-hand side. */
10793 : 1376 : if (from_len == NULL_TREE)
10794 : : {
10795 : 1031 : if (rse->string_length != NULL_TREE)
10796 : : from_len = rse->string_length;
10797 : 591 : else if (re->ts.type == BT_CHARACTER && re->ts.u.cl->length)
10798 : : {
10799 : 0 : gfc_init_se (&se, NULL);
10800 : 0 : gfc_conv_expr (&se, re->ts.u.cl->length);
10801 : 0 : gfc_add_block_to_block (block, &se.pre);
10802 : 0 : gcc_assert (se.post.head == NULL_TREE);
10803 : 0 : from_len = gfc_evaluate_now (se.expr, block);
10804 : : }
10805 : : else
10806 : 591 : from_len = build_zero_cst (gfc_charlen_type_node);
10807 : : }
10808 : 1376 : gfc_add_modify (pre, to_len, fold_convert (TREE_TYPE (to_len),
10809 : : from_len));
10810 : : }
10811 : : }
10812 : :
10813 : : /* Return the _len and _vptr trees only, when requested. */
10814 : 4327 : if (to_lenp)
10815 : 3183 : *to_lenp = to_len;
10816 : 4327 : if (from_lenp)
10817 : 3183 : *from_lenp = from_len;
10818 : 4327 : if (from_vptrp)
10819 : 3183 : *from_vptrp = from_vptr;
10820 : 4327 : return lhs_vptr;
10821 : : }
10822 : :
10823 : :
10824 : : /* Assign tokens for pointer components. */
10825 : :
10826 : : static void
10827 : 6 : trans_caf_token_assign (gfc_se *lse, gfc_se *rse, gfc_expr *expr1,
10828 : : gfc_expr *expr2)
10829 : : {
10830 : 6 : symbol_attribute lhs_attr, rhs_attr;
10831 : 6 : tree tmp, lhs_tok, rhs_tok;
10832 : : /* Flag to indicated component refs on the rhs. */
10833 : 6 : bool rhs_cr;
10834 : :
10835 : 6 : lhs_attr = gfc_caf_attr (expr1);
10836 : 6 : if (expr2->expr_type != EXPR_NULL)
10837 : : {
10838 : 4 : rhs_attr = gfc_caf_attr (expr2, false, &rhs_cr);
10839 : 4 : if (lhs_attr.codimension && rhs_attr.codimension)
10840 : : {
10841 : 2 : lhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (lse, expr1);
10842 : 2 : lhs_tok = build_fold_indirect_ref (lhs_tok);
10843 : :
10844 : 2 : if (rhs_cr)
10845 : 0 : rhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (rse, expr2);
10846 : : else
10847 : : {
10848 : 2 : tree caf_decl;
10849 : 2 : caf_decl = gfc_get_tree_for_caf_expr (expr2);
10850 : 2 : gfc_get_caf_token_offset (rse, &rhs_tok, NULL, caf_decl,
10851 : : NULL_TREE, NULL);
10852 : : }
10853 : 2 : tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
10854 : : lhs_tok,
10855 : 2 : fold_convert (TREE_TYPE (lhs_tok), rhs_tok));
10856 : 2 : gfc_prepend_expr_to_block (&lse->post, tmp);
10857 : : }
10858 : : }
10859 : 2 : else if (lhs_attr.codimension)
10860 : : {
10861 : 2 : lhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (lse, expr1);
10862 : 2 : if (!lhs_tok)
10863 : : {
10864 : 1 : lhs_tok = gfc_get_tree_for_caf_expr (expr1);
10865 : 1 : lhs_tok = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (lhs_tok));
10866 : : }
10867 : : else
10868 : 1 : lhs_tok = build_fold_indirect_ref (lhs_tok);
10869 : 2 : tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
10870 : : lhs_tok, null_pointer_node);
10871 : 2 : gfc_prepend_expr_to_block (&lse->post, tmp);
10872 : : }
10873 : 6 : }
10874 : :
10875 : :
10876 : : /* Do everything that is needed for a CLASS function expr2. */
10877 : :
10878 : : static tree
10879 : 18 : trans_class_pointer_fcn (stmtblock_t *block, gfc_se *lse, gfc_se *rse,
10880 : : gfc_expr *expr1, gfc_expr *expr2)
10881 : : {
10882 : 18 : tree expr1_vptr = NULL_TREE;
10883 : 18 : tree tmp;
10884 : :
10885 : 18 : gfc_conv_function_expr (rse, expr2);
10886 : 18 : rse->expr = gfc_evaluate_now (rse->expr, &rse->pre);
10887 : :
10888 : 18 : if (expr1->ts.type != BT_CLASS)
10889 : 12 : rse->expr = gfc_class_data_get (rse->expr);
10890 : : else
10891 : : {
10892 : 6 : expr1_vptr = trans_class_vptr_len_assignment (block, expr1,
10893 : : expr2, rse,
10894 : : NULL, NULL, NULL);
10895 : 6 : gfc_add_block_to_block (block, &rse->pre);
10896 : 6 : tmp = gfc_create_var (TREE_TYPE (rse->expr), "ptrtemp");
10897 : 6 : gfc_add_modify (&lse->pre, tmp, rse->expr);
10898 : :
10899 : 12 : gfc_add_modify (&lse->pre, expr1_vptr,
10900 : 6 : fold_convert (TREE_TYPE (expr1_vptr),
10901 : : gfc_class_vptr_get (tmp)));
10902 : 6 : rse->expr = gfc_class_data_get (tmp);
10903 : : }
10904 : :
10905 : 18 : return expr1_vptr;
10906 : : }
10907 : :
10908 : :
10909 : : tree
10910 : 9787 : gfc_trans_pointer_assign (gfc_code * code)
10911 : : {
10912 : 9787 : return gfc_trans_pointer_assignment (code->expr1, code->expr2);
10913 : : }
10914 : :
10915 : :
10916 : : /* Generate code for a pointer assignment. */
10917 : :
10918 : : tree
10919 : 9842 : gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
10920 : : {
10921 : 9842 : gfc_se lse;
10922 : 9842 : gfc_se rse;
10923 : 9842 : stmtblock_t block;
10924 : 9842 : tree desc;
10925 : 9842 : tree tmp;
10926 : 9842 : tree expr1_vptr = NULL_TREE;
10927 : 9842 : bool scalar, non_proc_ptr_assign;
10928 : 9842 : gfc_ss *ss;
10929 : :
10930 : 9842 : gfc_start_block (&block);
10931 : :
10932 : 9842 : gfc_init_se (&lse, NULL);
10933 : :
10934 : : /* Usually testing whether this is not a proc pointer assignment. */
10935 : 9842 : non_proc_ptr_assign
10936 : 9842 : = !(gfc_expr_attr (expr1).proc_pointer
10937 : 1165 : && ((expr2->expr_type == EXPR_VARIABLE
10938 : 934 : && expr2->symtree->n.sym->attr.flavor == FL_PROCEDURE)
10939 : 281 : || expr2->expr_type == EXPR_NULL));
10940 : :
10941 : : /* Check whether the expression is a scalar or not; we cannot use
10942 : : expr1->rank as it can be nonzero for proc pointers. */
10943 : 9842 : ss = gfc_walk_expr (expr1);
10944 : 9842 : scalar = ss == gfc_ss_terminator;
10945 : 9842 : if (!scalar)
10946 : 4209 : gfc_free_ss_chain (ss);
10947 : :
10948 : 9842 : if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS
10949 : 84 : && expr2->expr_type != EXPR_FUNCTION && non_proc_ptr_assign)
10950 : : {
10951 : 66 : gfc_add_data_component (expr2);
10952 : : /* The following is required as gfc_add_data_component doesn't
10953 : : update ts.type if there is a trailing REF_ARRAY. */
10954 : 66 : expr2->ts.type = BT_DERIVED;
10955 : : }
10956 : :
10957 : 9842 : if (scalar)
10958 : : {
10959 : : /* Scalar pointers. */
10960 : 5633 : lse.want_pointer = 1;
10961 : 5633 : gfc_conv_expr (&lse, expr1);
10962 : 5633 : gfc_init_se (&rse, NULL);
10963 : 5633 : rse.want_pointer = 1;
10964 : 5633 : if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
10965 : 6 : trans_class_pointer_fcn (&block, &lse, &rse, expr1, expr2);
10966 : : else
10967 : 5627 : gfc_conv_expr (&rse, expr2);
10968 : :
10969 : 5633 : if (non_proc_ptr_assign && expr1->ts.type == BT_CLASS)
10970 : : {
10971 : 763 : trans_class_vptr_len_assignment (&block, expr1, expr2, &rse, NULL,
10972 : : NULL, NULL);
10973 : 763 : lse.expr = gfc_class_data_get (lse.expr);
10974 : : }
10975 : :
10976 : 5633 : if (expr1->symtree->n.sym->attr.proc_pointer
10977 : 850 : && expr1->symtree->n.sym->attr.dummy)
10978 : 49 : lse.expr = build_fold_indirect_ref_loc (input_location,
10979 : : lse.expr);
10980 : :
10981 : 5633 : if (expr2->symtree && expr2->symtree->n.sym->attr.proc_pointer
10982 : 47 : && expr2->symtree->n.sym->attr.dummy)
10983 : 20 : rse.expr = build_fold_indirect_ref_loc (input_location,
10984 : : rse.expr);
10985 : :
10986 : 5633 : gfc_add_block_to_block (&block, &lse.pre);
10987 : 5633 : gfc_add_block_to_block (&block, &rse.pre);
10988 : :
10989 : : /* Check character lengths if character expression. The test is only
10990 : : really added if -fbounds-check is enabled. Exclude deferred
10991 : : character length lefthand sides. */
10992 : 921 : if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL
10993 : 747 : && !expr1->ts.deferred
10994 : 333 : && !expr1->symtree->n.sym->attr.proc_pointer
10995 : 5959 : && !gfc_is_proc_ptr_comp (expr1))
10996 : : {
10997 : 307 : gcc_assert (expr2->ts.type == BT_CHARACTER);
10998 : 307 : gcc_assert (lse.string_length && rse.string_length);
10999 : 307 : gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
11000 : : lse.string_length, rse.string_length,
11001 : : &block);
11002 : : }
11003 : :
11004 : : /* The assignment to an deferred character length sets the string
11005 : : length to that of the rhs. */
11006 : 5633 : if (expr1->ts.deferred)
11007 : : {
11008 : 529 : if (expr2->expr_type != EXPR_NULL && lse.string_length != NULL)
11009 : 412 : gfc_add_modify (&block, lse.string_length,
11010 : 412 : fold_convert (TREE_TYPE (lse.string_length),
11011 : : rse.string_length));
11012 : 117 : else if (lse.string_length != NULL)
11013 : 115 : gfc_add_modify (&block, lse.string_length,
11014 : 115 : build_zero_cst (TREE_TYPE (lse.string_length)));
11015 : : }
11016 : :
11017 : 5633 : gfc_add_modify (&block, lse.expr,
11018 : 5633 : fold_convert (TREE_TYPE (lse.expr), rse.expr));
11019 : :
11020 : 5633 : if (flag_coarray == GFC_FCOARRAY_LIB)
11021 : : {
11022 : 249 : if (expr1->ref)
11023 : : /* Also set the tokens for pointer components in derived typed
11024 : : coarrays. */
11025 : 6 : trans_caf_token_assign (&lse, &rse, expr1, expr2);
11026 : 243 : else if (gfc_caf_attr (expr1).codimension)
11027 : : {
11028 : 0 : tree lhs_caf_decl, rhs_caf_decl, lhs_tok, rhs_tok;
11029 : :
11030 : 0 : lhs_caf_decl = gfc_get_tree_for_caf_expr (expr1);
11031 : 0 : rhs_caf_decl = gfc_get_tree_for_caf_expr (expr2);
11032 : 0 : gfc_get_caf_token_offset (&lse, &lhs_tok, nullptr, lhs_caf_decl,
11033 : : NULL_TREE, expr1);
11034 : 0 : gfc_get_caf_token_offset (&rse, &rhs_tok, nullptr, rhs_caf_decl,
11035 : : NULL_TREE, expr2);
11036 : 0 : gfc_add_modify (&block, lhs_tok, rhs_tok);
11037 : : }
11038 : : }
11039 : :
11040 : 5633 : gfc_add_block_to_block (&block, &rse.post);
11041 : 5633 : gfc_add_block_to_block (&block, &lse.post);
11042 : : }
11043 : : else
11044 : : {
11045 : 4209 : gfc_ref* remap;
11046 : 4209 : bool rank_remap;
11047 : 4209 : tree strlen_lhs;
11048 : 4209 : tree strlen_rhs = NULL_TREE;
11049 : :
11050 : : /* Array pointer. Find the last reference on the LHS and if it is an
11051 : : array section ref, we're dealing with bounds remapping. In this case,
11052 : : set it to AR_FULL so that gfc_conv_expr_descriptor does
11053 : : not see it and process the bounds remapping afterwards explicitly. */
11054 : 13572 : for (remap = expr1->ref; remap; remap = remap->next)
11055 : 5485 : if (!remap->next && remap->type == REF_ARRAY
11056 : 4209 : && remap->u.ar.type == AR_SECTION)
11057 : : break;
11058 : 4209 : rank_remap = (remap && remap->u.ar.end[0]);
11059 : :
11060 : 331 : if (remap && expr2->expr_type == EXPR_NULL)
11061 : : {
11062 : 2 : gfc_error ("If bounds remapping is specified at %L, "
11063 : : "the pointer target shall not be NULL", &expr1->where);
11064 : 2 : return NULL_TREE;
11065 : : }
11066 : :
11067 : 4207 : gfc_init_se (&lse, NULL);
11068 : 4207 : if (remap)
11069 : 329 : lse.descriptor_only = 1;
11070 : 4207 : gfc_conv_expr_descriptor (&lse, expr1);
11071 : 4207 : strlen_lhs = lse.string_length;
11072 : 4207 : desc = lse.expr;
11073 : :
11074 : 4207 : if (expr2->expr_type == EXPR_NULL)
11075 : : {
11076 : : /* Just set the data pointer to null. */
11077 : 679 : gfc_conv_descriptor_data_set (&lse.pre, lse.expr, null_pointer_node);
11078 : : }
11079 : 3528 : else if (rank_remap)
11080 : : {
11081 : : /* If we are rank-remapping, just get the RHS's descriptor and
11082 : : process this later on. */
11083 : 206 : gfc_init_se (&rse, NULL);
11084 : 206 : rse.direct_byref = 1;
11085 : 206 : rse.byref_noassign = 1;
11086 : :
11087 : 206 : if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
11088 : 12 : expr1_vptr = trans_class_pointer_fcn (&block, &lse, &rse,
11089 : : expr1, expr2);
11090 : 194 : else if (expr2->expr_type == EXPR_FUNCTION)
11091 : : {
11092 : : tree bound[GFC_MAX_DIMENSIONS];
11093 : : int i;
11094 : :
11095 : 26 : for (i = 0; i < expr2->rank; i++)
11096 : 13 : bound[i] = NULL_TREE;
11097 : 13 : tmp = gfc_typenode_for_spec (&expr2->ts);
11098 : 13 : tmp = gfc_get_array_type_bounds (tmp, expr2->rank, 0,
11099 : : bound, bound, 0,
11100 : : GFC_ARRAY_POINTER_CONT, false);
11101 : 13 : tmp = gfc_create_var (tmp, "ptrtemp");
11102 : 13 : rse.descriptor_only = 0;
11103 : 13 : rse.expr = tmp;
11104 : 13 : rse.direct_byref = 1;
11105 : 13 : gfc_conv_expr_descriptor (&rse, expr2);
11106 : 13 : strlen_rhs = rse.string_length;
11107 : 13 : rse.expr = tmp;
11108 : : }
11109 : : else
11110 : : {
11111 : 181 : gfc_conv_expr_descriptor (&rse, expr2);
11112 : 181 : strlen_rhs = rse.string_length;
11113 : 181 : if (expr1->ts.type == BT_CLASS)
11114 : 36 : expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
11115 : : expr2, &rse,
11116 : : NULL, NULL,
11117 : : NULL);
11118 : : }
11119 : : }
11120 : 3322 : else if (expr2->expr_type == EXPR_VARIABLE)
11121 : : {
11122 : : /* Assign directly to the LHS's descriptor. */
11123 : 3196 : lse.descriptor_only = 0;
11124 : 3196 : lse.direct_byref = 1;
11125 : 3196 : gfc_conv_expr_descriptor (&lse, expr2);
11126 : 3196 : strlen_rhs = lse.string_length;
11127 : 3196 : gfc_init_se (&rse, NULL);
11128 : :
11129 : 3196 : if (expr1->ts.type == BT_CLASS)
11130 : : {
11131 : 326 : rse.expr = NULL_TREE;
11132 : 326 : rse.string_length = strlen_rhs;
11133 : 326 : trans_class_vptr_len_assignment (&block, expr1, expr2, &rse,
11134 : : NULL, NULL, NULL);
11135 : : }
11136 : :
11137 : 3196 : if (remap == NULL)
11138 : : {
11139 : : /* If the target is not a whole array, use the target array
11140 : : reference for remap. */
11141 : 6601 : for (remap = expr2->ref; remap; remap = remap->next)
11142 : 3629 : if (remap->type == REF_ARRAY
11143 : 3133 : && remap->u.ar.type == AR_FULL
11144 : 2465 : && remap->next)
11145 : : break;
11146 : : }
11147 : : }
11148 : 126 : else if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
11149 : : {
11150 : 19 : gfc_init_se (&rse, NULL);
11151 : 19 : rse.want_pointer = 1;
11152 : 19 : gfc_conv_function_expr (&rse, expr2);
11153 : 19 : if (expr1->ts.type != BT_CLASS)
11154 : : {
11155 : 6 : rse.expr = gfc_class_data_get (rse.expr);
11156 : 6 : gfc_add_modify (&lse.pre, desc, rse.expr);
11157 : : /* Set the lhs span. */
11158 : 6 : tmp = TREE_TYPE (rse.expr);
11159 : 6 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (tmp));
11160 : 6 : tmp = fold_convert (gfc_array_index_type, tmp);
11161 : 6 : gfc_conv_descriptor_span_set (&lse.pre, desc, tmp);
11162 : : }
11163 : : else
11164 : : {
11165 : 13 : expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
11166 : : expr2, &rse, NULL,
11167 : : NULL, NULL);
11168 : 13 : gfc_add_block_to_block (&block, &rse.pre);
11169 : 13 : tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
11170 : 13 : gfc_add_modify (&lse.pre, tmp, rse.expr);
11171 : :
11172 : 26 : gfc_add_modify (&lse.pre, expr1_vptr,
11173 : 13 : fold_convert (TREE_TYPE (expr1_vptr),
11174 : : gfc_class_vptr_get (tmp)));
11175 : 13 : rse.expr = gfc_class_data_get (tmp);
11176 : 13 : gfc_add_modify (&lse.pre, desc, rse.expr);
11177 : : }
11178 : : }
11179 : : else
11180 : : {
11181 : : /* Assign to a temporary descriptor and then copy that
11182 : : temporary to the pointer. */
11183 : 107 : tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
11184 : 107 : lse.descriptor_only = 0;
11185 : 107 : lse.expr = tmp;
11186 : 107 : lse.direct_byref = 1;
11187 : 107 : gfc_conv_expr_descriptor (&lse, expr2);
11188 : 107 : strlen_rhs = lse.string_length;
11189 : 107 : gfc_add_modify (&lse.pre, desc, tmp);
11190 : : }
11191 : :
11192 : 4207 : if (expr1->ts.type == BT_CHARACTER
11193 : 572 : && expr1->ts.deferred)
11194 : : {
11195 : 326 : gfc_symbol *psym = expr1->symtree->n.sym;
11196 : 326 : tmp = NULL_TREE;
11197 : 326 : if (psym->ts.type == BT_CHARACTER
11198 : 325 : && psym->ts.u.cl->backend_decl)
11199 : 325 : tmp = psym->ts.u.cl->backend_decl;
11200 : 1 : else if (expr1->ts.u.cl->backend_decl
11201 : 1 : && VAR_P (expr1->ts.u.cl->backend_decl))
11202 : 0 : tmp = expr1->ts.u.cl->backend_decl;
11203 : 1 : else if (TREE_CODE (lse.expr) == COMPONENT_REF)
11204 : : {
11205 : 1 : gfc_ref *ref = expr1->ref;
11206 : 3 : for (;ref; ref = ref->next)
11207 : : {
11208 : 2 : if (ref->type == REF_COMPONENT
11209 : 1 : && ref->u.c.component->ts.type == BT_CHARACTER
11210 : 3 : && gfc_deferred_strlen (ref->u.c.component, &tmp))
11211 : 1 : tmp = fold_build3_loc (input_location, COMPONENT_REF,
11212 : 1 : TREE_TYPE (tmp),
11213 : 1 : TREE_OPERAND (lse.expr, 0),
11214 : : tmp, NULL_TREE);
11215 : : }
11216 : : }
11217 : :
11218 : 326 : gcc_assert (tmp);
11219 : :
11220 : 326 : if (expr2->expr_type != EXPR_NULL)
11221 : 314 : gfc_add_modify (&block, tmp,
11222 : 314 : fold_convert (TREE_TYPE (tmp), strlen_rhs));
11223 : : else
11224 : 12 : gfc_add_modify (&block, tmp, build_zero_cst (TREE_TYPE (tmp)));
11225 : : }
11226 : :
11227 : 4207 : gfc_add_block_to_block (&block, &lse.pre);
11228 : 4207 : if (rank_remap)
11229 : 206 : gfc_add_block_to_block (&block, &rse.pre);
11230 : :
11231 : : /* If we do bounds remapping, update LHS descriptor accordingly. */
11232 : 4207 : if (remap)
11233 : : {
11234 : 430 : int dim;
11235 : 430 : gcc_assert (remap->u.ar.dimen == expr1->rank);
11236 : :
11237 : 430 : if (rank_remap)
11238 : : {
11239 : : /* Do rank remapping. We already have the RHS's descriptor
11240 : : converted in rse and now have to build the correct LHS
11241 : : descriptor for it. */
11242 : :
11243 : 206 : tree dtype, data, span;
11244 : 206 : tree offs, stride;
11245 : 206 : tree lbound, ubound;
11246 : :
11247 : : /* Set dtype. */
11248 : 206 : dtype = gfc_conv_descriptor_dtype (desc);
11249 : 206 : tmp = gfc_get_dtype (TREE_TYPE (desc));
11250 : 206 : gfc_add_modify (&block, dtype, tmp);
11251 : :
11252 : : /* Copy data pointer. */
11253 : 206 : data = gfc_conv_descriptor_data_get (rse.expr);
11254 : 206 : gfc_conv_descriptor_data_set (&block, desc, data);
11255 : :
11256 : : /* Copy the span. */
11257 : 206 : if (VAR_P (rse.expr)
11258 : 206 : && GFC_DECL_PTR_ARRAY_P (rse.expr))
11259 : 12 : span = gfc_conv_descriptor_span_get (rse.expr);
11260 : : else
11261 : : {
11262 : 194 : tmp = TREE_TYPE (rse.expr);
11263 : 194 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (tmp));
11264 : 194 : span = fold_convert (gfc_array_index_type, tmp);
11265 : : }
11266 : 206 : gfc_conv_descriptor_span_set (&block, desc, span);
11267 : :
11268 : : /* Copy offset but adjust it such that it would correspond
11269 : : to a lbound of zero. */
11270 : 206 : if (expr2->rank == -1)
11271 : 42 : gfc_conv_descriptor_offset_set (&block, desc,
11272 : : gfc_index_zero_node);
11273 : : else
11274 : : {
11275 : 164 : offs = gfc_conv_descriptor_offset_get (rse.expr);
11276 : 510 : for (dim = 0; dim < expr2->rank; ++dim)
11277 : : {
11278 : 182 : stride = gfc_conv_descriptor_stride_get (rse.expr,
11279 : : gfc_rank_cst[dim]);
11280 : 182 : lbound = gfc_conv_descriptor_lbound_get (rse.expr,
11281 : : gfc_rank_cst[dim]);
11282 : 182 : tmp = fold_build2_loc (input_location, MULT_EXPR,
11283 : : gfc_array_index_type, stride,
11284 : : lbound);
11285 : 182 : offs = fold_build2_loc (input_location, PLUS_EXPR,
11286 : : gfc_array_index_type, offs, tmp);
11287 : : }
11288 : 164 : gfc_conv_descriptor_offset_set (&block, desc, offs);
11289 : : }
11290 : : /* Set the bounds as declared for the LHS and calculate strides as
11291 : : well as another offset update accordingly. */
11292 : 206 : stride = gfc_conv_descriptor_stride_get (rse.expr,
11293 : : gfc_rank_cst[0]);
11294 : 545 : for (dim = 0; dim < expr1->rank; ++dim)
11295 : : {
11296 : 339 : gfc_se lower_se;
11297 : 339 : gfc_se upper_se;
11298 : :
11299 : 339 : gcc_assert (remap->u.ar.start[dim] && remap->u.ar.end[dim]);
11300 : :
11301 : 339 : if (remap->u.ar.start[dim]->expr_type != EXPR_CONSTANT
11302 : : || remap->u.ar.start[dim]->expr_type != EXPR_VARIABLE)
11303 : 339 : gfc_resolve_expr (remap->u.ar.start[dim]);
11304 : 339 : if (remap->u.ar.end[dim]->expr_type != EXPR_CONSTANT
11305 : : || remap->u.ar.end[dim]->expr_type != EXPR_VARIABLE)
11306 : 339 : gfc_resolve_expr (remap->u.ar.end[dim]);
11307 : :
11308 : : /* Convert declared bounds. */
11309 : 339 : gfc_init_se (&lower_se, NULL);
11310 : 339 : gfc_init_se (&upper_se, NULL);
11311 : 339 : gfc_conv_expr (&lower_se, remap->u.ar.start[dim]);
11312 : 339 : gfc_conv_expr (&upper_se, remap->u.ar.end[dim]);
11313 : :
11314 : 339 : gfc_add_block_to_block (&block, &lower_se.pre);
11315 : 339 : gfc_add_block_to_block (&block, &upper_se.pre);
11316 : :
11317 : 339 : lbound = fold_convert (gfc_array_index_type, lower_se.expr);
11318 : 339 : ubound = fold_convert (gfc_array_index_type, upper_se.expr);
11319 : :
11320 : 339 : lbound = gfc_evaluate_now (lbound, &block);
11321 : 339 : ubound = gfc_evaluate_now (ubound, &block);
11322 : :
11323 : 339 : gfc_add_block_to_block (&block, &lower_se.post);
11324 : 339 : gfc_add_block_to_block (&block, &upper_se.post);
11325 : :
11326 : : /* Set bounds in descriptor. */
11327 : 339 : gfc_conv_descriptor_lbound_set (&block, desc,
11328 : : gfc_rank_cst[dim], lbound);
11329 : 339 : gfc_conv_descriptor_ubound_set (&block, desc,
11330 : : gfc_rank_cst[dim], ubound);
11331 : :
11332 : : /* Set stride. */
11333 : 339 : stride = gfc_evaluate_now (stride, &block);
11334 : 339 : gfc_conv_descriptor_stride_set (&block, desc,
11335 : : gfc_rank_cst[dim], stride);
11336 : :
11337 : : /* Update offset. */
11338 : 339 : offs = gfc_conv_descriptor_offset_get (desc);
11339 : 339 : tmp = fold_build2_loc (input_location, MULT_EXPR,
11340 : : gfc_array_index_type, lbound, stride);
11341 : 339 : offs = fold_build2_loc (input_location, MINUS_EXPR,
11342 : : gfc_array_index_type, offs, tmp);
11343 : 339 : offs = gfc_evaluate_now (offs, &block);
11344 : 339 : gfc_conv_descriptor_offset_set (&block, desc, offs);
11345 : :
11346 : : /* Update stride. */
11347 : 339 : tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
11348 : 339 : stride = fold_build2_loc (input_location, MULT_EXPR,
11349 : : gfc_array_index_type, stride, tmp);
11350 : : }
11351 : : }
11352 : : else
11353 : : {
11354 : : /* Bounds remapping. Just shift the lower bounds. */
11355 : :
11356 : 224 : gcc_assert (expr1->rank == expr2->rank);
11357 : :
11358 : 556 : for (dim = 0; dim < remap->u.ar.dimen; ++dim)
11359 : : {
11360 : 332 : gfc_se lbound_se;
11361 : :
11362 : 332 : gcc_assert (!remap->u.ar.end[dim]);
11363 : 332 : gfc_init_se (&lbound_se, NULL);
11364 : 332 : if (remap->u.ar.start[dim])
11365 : : {
11366 : 225 : gfc_conv_expr (&lbound_se, remap->u.ar.start[dim]);
11367 : 225 : gfc_add_block_to_block (&block, &lbound_se.pre);
11368 : : }
11369 : : else
11370 : : /* This remap arises from a target that is not a whole
11371 : : array. The start expressions will be NULL but we need
11372 : : the lbounds to be one. */
11373 : 107 : lbound_se.expr = gfc_index_one_node;
11374 : 332 : gfc_conv_shift_descriptor_lbound (&block, desc,
11375 : : dim, lbound_se.expr);
11376 : 332 : gfc_add_block_to_block (&block, &lbound_se.post);
11377 : : }
11378 : : }
11379 : : }
11380 : :
11381 : : /* If rank remapping was done, check with -fcheck=bounds that
11382 : : the target is at least as large as the pointer. */
11383 : 4207 : if (rank_remap && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
11384 : 72 : && expr2->rank != -1)
11385 : : {
11386 : 54 : tree lsize, rsize;
11387 : 54 : tree fault;
11388 : 54 : const char* msg;
11389 : :
11390 : 54 : lsize = gfc_conv_descriptor_size (lse.expr, expr1->rank);
11391 : 54 : rsize = gfc_conv_descriptor_size (rse.expr, expr2->rank);
11392 : :
11393 : 54 : lsize = gfc_evaluate_now (lsize, &block);
11394 : 54 : rsize = gfc_evaluate_now (rsize, &block);
11395 : 54 : fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
11396 : : rsize, lsize);
11397 : :
11398 : 54 : msg = _("Target of rank remapping is too small (%ld < %ld)");
11399 : 54 : gfc_trans_runtime_check (true, false, fault, &block, &expr2->where,
11400 : : msg, rsize, lsize);
11401 : : }
11402 : :
11403 : : /* Check string lengths if applicable. The check is only really added
11404 : : to the output code if -fbounds-check is enabled. */
11405 : 4207 : if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
11406 : : {
11407 : 506 : gcc_assert (expr2->ts.type == BT_CHARACTER);
11408 : 506 : gcc_assert (strlen_lhs && strlen_rhs);
11409 : 506 : gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
11410 : : strlen_lhs, strlen_rhs, &block);
11411 : : }
11412 : :
11413 : 4207 : gfc_add_block_to_block (&block, &lse.post);
11414 : 4207 : if (rank_remap)
11415 : 206 : gfc_add_block_to_block (&block, &rse.post);
11416 : : }
11417 : :
11418 : 9840 : return gfc_finish_block (&block);
11419 : : }
11420 : :
11421 : :
11422 : : /* Makes sure se is suitable for passing as a function string parameter. */
11423 : : /* TODO: Need to check all callers of this function. It may be abused. */
11424 : :
11425 : : void
11426 : 231525 : gfc_conv_string_parameter (gfc_se * se)
11427 : : {
11428 : 231525 : tree type;
11429 : :
11430 : 231525 : if (TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE
11431 : 231525 : && integer_onep (se->string_length))
11432 : : {
11433 : 667 : se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
11434 : 667 : return;
11435 : : }
11436 : :
11437 : 230858 : if (TREE_CODE (se->expr) == STRING_CST)
11438 : : {
11439 : 97241 : type = TREE_TYPE (TREE_TYPE (se->expr));
11440 : 97241 : se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
11441 : 97241 : return;
11442 : : }
11443 : :
11444 : 133617 : if ((TREE_CODE (TREE_TYPE (se->expr)) == ARRAY_TYPE
11445 : 51953 : || TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE)
11446 : 133713 : && TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
11447 : : {
11448 : 81760 : type = TREE_TYPE (se->expr);
11449 : 81760 : if (TREE_CODE (se->expr) != INDIRECT_REF)
11450 : 76885 : se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
11451 : : else
11452 : : {
11453 : 4875 : if (TREE_CODE (type) == ARRAY_TYPE)
11454 : 4875 : type = TREE_TYPE (type);
11455 : 4875 : type = gfc_get_character_type_len_for_eltype (type,
11456 : : se->string_length);
11457 : 4875 : type = build_pointer_type (type);
11458 : 4875 : se->expr = gfc_build_addr_expr (type, se->expr);
11459 : : }
11460 : : }
11461 : :
11462 : 133617 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
11463 : : }
11464 : :
11465 : :
11466 : : /* Generate code for assignment of scalar variables. Includes character
11467 : : strings and derived types with allocatable components.
11468 : : If you know that the LHS has no allocations, set dealloc to false.
11469 : :
11470 : : DEEP_COPY has no effect if the typespec TS is not a derived type with
11471 : : allocatable components. Otherwise, if it is set, an explicit copy of each
11472 : : allocatable component is made. This is necessary as a simple copy of the
11473 : : whole object would copy array descriptors as is, so that the lhs's
11474 : : allocatable components would point to the rhs's after the assignment.
11475 : : Typically, setting DEEP_COPY is necessary if the rhs is a variable, and not
11476 : : necessary if the rhs is a non-pointer function, as the allocatable components
11477 : : are not accessible by other means than the function's result after the
11478 : : function has returned. It is even more subtle when temporaries are involved,
11479 : : as the two following examples show:
11480 : : 1. When we evaluate an array constructor, a temporary is created. Thus
11481 : : there is theoretically no alias possible. However, no deep copy is
11482 : : made for this temporary, so that if the constructor is made of one or
11483 : : more variable with allocatable components, those components still point
11484 : : to the variable's: DEEP_COPY should be set for the assignment from the
11485 : : temporary to the lhs in that case.
11486 : : 2. When assigning a scalar to an array, we evaluate the scalar value out
11487 : : of the loop, store it into a temporary variable, and assign from that.
11488 : : In that case, deep copying when assigning to the temporary would be a
11489 : : waste of resources; however deep copies should happen when assigning from
11490 : : the temporary to each array element: again DEEP_COPY should be set for
11491 : : the assignment from the temporary to the lhs. */
11492 : :
11493 : : tree
11494 : 323567 : gfc_trans_scalar_assign (gfc_se *lse, gfc_se *rse, gfc_typespec ts,
11495 : : bool deep_copy, bool dealloc, bool in_coarray,
11496 : : bool assoc_assign)
11497 : : {
11498 : 323567 : stmtblock_t block;
11499 : 323567 : tree tmp;
11500 : 323567 : tree cond;
11501 : :
11502 : 323567 : gfc_init_block (&block);
11503 : :
11504 : 323567 : if (ts.type == BT_CHARACTER)
11505 : : {
11506 : 31262 : tree rlen = NULL;
11507 : 31262 : tree llen = NULL;
11508 : :
11509 : 31262 : if (lse->string_length != NULL_TREE)
11510 : : {
11511 : 31262 : gfc_conv_string_parameter (lse);
11512 : 31262 : gfc_add_block_to_block (&block, &lse->pre);
11513 : 31262 : llen = lse->string_length;
11514 : : }
11515 : :
11516 : 31262 : if (rse->string_length != NULL_TREE)
11517 : : {
11518 : 31262 : gfc_conv_string_parameter (rse);
11519 : 31262 : gfc_add_block_to_block (&block, &rse->pre);
11520 : 31262 : rlen = rse->string_length;
11521 : : }
11522 : :
11523 : 31262 : gfc_trans_string_copy (&block, llen, lse->expr, ts.kind, rlen,
11524 : : rse->expr, ts.kind);
11525 : : }
11526 : 292305 : else if (gfc_bt_struct (ts.type)
11527 : 17044 : && (ts.u.derived->attr.alloc_comp
11528 : 11523 : || (deep_copy && ts.u.derived->attr.pdt_type)))
11529 : : {
11530 : 5592 : tree tmp_var = NULL_TREE;
11531 : 5592 : cond = NULL_TREE;
11532 : :
11533 : : /* Are the rhs and the lhs the same? */
11534 : 5592 : if (deep_copy)
11535 : : {
11536 : 3222 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
11537 : : gfc_build_addr_expr (NULL_TREE, lse->expr),
11538 : : gfc_build_addr_expr (NULL_TREE, rse->expr));
11539 : 3222 : cond = gfc_evaluate_now (cond, &lse->pre);
11540 : : }
11541 : :
11542 : : /* Deallocate the lhs allocated components as long as it is not
11543 : : the same as the rhs. This must be done following the assignment
11544 : : to prevent deallocating data that could be used in the rhs
11545 : : expression. */
11546 : 5592 : if (dealloc)
11547 : : {
11548 : 1564 : tmp_var = gfc_evaluate_now (lse->expr, &lse->pre);
11549 : 1564 : tmp = gfc_deallocate_alloc_comp_no_caf (ts.u.derived, tmp_var,
11550 : 1564 : 0, gfc_may_be_finalized (ts));
11551 : 1564 : if (deep_copy)
11552 : 635 : tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
11553 : : tmp);
11554 : 1564 : gfc_add_expr_to_block (&lse->post, tmp);
11555 : : }
11556 : :
11557 : 5592 : gfc_add_block_to_block (&block, &rse->pre);
11558 : 5592 : gfc_add_block_to_block (&block, &lse->finalblock);
11559 : 5592 : gfc_add_block_to_block (&block, &lse->pre);
11560 : :
11561 : 5592 : gfc_add_modify (&block, lse->expr,
11562 : 5592 : fold_convert (TREE_TYPE (lse->expr), rse->expr));
11563 : :
11564 : : /* Restore pointer address of coarray components. */
11565 : 5592 : if (ts.u.derived->attr.coarray_comp && deep_copy && tmp_var != NULL_TREE)
11566 : : {
11567 : 4 : tmp = gfc_reassign_alloc_comp_caf (ts.u.derived, tmp_var, lse->expr);
11568 : 4 : tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
11569 : : tmp);
11570 : 4 : gfc_add_expr_to_block (&block, tmp);
11571 : : }
11572 : :
11573 : : /* Do a deep copy if the rhs is a variable, if it is not the
11574 : : same as the lhs. */
11575 : 5592 : if (deep_copy)
11576 : : {
11577 : 3222 : int caf_mode = in_coarray ? (GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
11578 : : | GFC_STRUCTURE_CAF_MODE_IN_COARRAY) : 0;
11579 : 3222 : tmp = gfc_copy_alloc_comp (ts.u.derived, rse->expr, lse->expr, 0,
11580 : : caf_mode);
11581 : 3222 : tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
11582 : : tmp);
11583 : 3222 : gfc_add_expr_to_block (&block, tmp);
11584 : : }
11585 : : }
11586 : 286713 : else if (gfc_bt_struct (ts.type))
11587 : : {
11588 : 11452 : gfc_add_block_to_block (&block, &rse->pre);
11589 : 11452 : gfc_add_block_to_block (&block, &lse->finalblock);
11590 : 11452 : gfc_add_block_to_block (&block, &lse->pre);
11591 : 11452 : tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
11592 : 11452 : TREE_TYPE (lse->expr), rse->expr);
11593 : 11452 : gfc_add_modify (&block, lse->expr, tmp);
11594 : : }
11595 : : /* If possible use the rhs vptr copy with trans_scalar_class_assign.... */
11596 : 275261 : else if (ts.type == BT_CLASS)
11597 : : {
11598 : 746 : gfc_add_block_to_block (&block, &lse->pre);
11599 : 746 : gfc_add_block_to_block (&block, &rse->pre);
11600 : 746 : gfc_add_block_to_block (&block, &lse->finalblock);
11601 : :
11602 : 746 : if (!trans_scalar_class_assign (&block, lse, rse))
11603 : : {
11604 : : /* ...otherwise assignment suffices. Note the use of VIEW_CONVERT_EXPR
11605 : : for the lhs which ensures that class data rhs cast as a string assigns
11606 : : correctly. */
11607 : 612 : tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
11608 : 612 : TREE_TYPE (rse->expr), lse->expr);
11609 : 612 : gfc_add_modify (&block, tmp, rse->expr);
11610 : : }
11611 : : }
11612 : 274515 : else if (ts.type != BT_CLASS)
11613 : : {
11614 : 274515 : gfc_add_block_to_block (&block, &lse->pre);
11615 : 274515 : gfc_add_block_to_block (&block, &rse->pre);
11616 : :
11617 : 274515 : if (in_coarray)
11618 : : {
11619 : 675 : if (flag_coarray == GFC_FCOARRAY_LIB && assoc_assign)
11620 : : {
11621 : 0 : gfc_add_modify (&block, gfc_conv_descriptor_token (lse->expr),
11622 : 0 : TYPE_LANG_SPECIFIC (
11623 : : TREE_TYPE (TREE_TYPE (rse->expr)))
11624 : : ->caf_token);
11625 : : }
11626 : 675 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (lse->expr)))
11627 : 0 : lse->expr = gfc_conv_array_data (lse->expr);
11628 : 266 : if (flag_coarray == GFC_FCOARRAY_SINGLE && assoc_assign
11629 : 675 : && !POINTER_TYPE_P (TREE_TYPE (rse->expr)))
11630 : 0 : rse->expr = gfc_build_addr_expr (NULL_TREE, rse->expr);
11631 : : }
11632 : 274515 : gfc_add_modify (&block, lse->expr,
11633 : 274515 : fold_convert (TREE_TYPE (lse->expr), rse->expr));
11634 : : }
11635 : :
11636 : 323567 : gfc_add_block_to_block (&block, &lse->post);
11637 : 323567 : gfc_add_block_to_block (&block, &rse->post);
11638 : :
11639 : 323567 : return gfc_finish_block (&block);
11640 : : }
11641 : :
11642 : :
11643 : : /* There are quite a lot of restrictions on the optimisation in using an
11644 : : array function assign without a temporary. */
11645 : :
11646 : : static bool
11647 : 14577 : arrayfunc_assign_needs_temporary (gfc_expr * expr1, gfc_expr * expr2)
11648 : : {
11649 : 14577 : gfc_ref * ref;
11650 : 14577 : bool seen_array_ref;
11651 : 14577 : bool c = false;
11652 : 14577 : gfc_symbol *sym = expr1->symtree->n.sym;
11653 : :
11654 : : /* Play it safe with class functions assigned to a derived type. */
11655 : 14577 : if (gfc_is_class_array_function (expr2)
11656 : 14577 : && expr1->ts.type == BT_DERIVED)
11657 : : return true;
11658 : :
11659 : : /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
11660 : 14553 : if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
11661 : : return true;
11662 : :
11663 : : /* Elemental functions are scalarized so that they don't need a
11664 : : temporary in gfc_trans_assignment_1, so return a true. Otherwise,
11665 : : they would need special treatment in gfc_trans_arrayfunc_assign. */
11666 : 8711 : if (expr2->value.function.esym != NULL
11667 : 1514 : && expr2->value.function.esym->attr.elemental)
11668 : : return true;
11669 : :
11670 : : /* Need a temporary if rhs is not FULL or a contiguous section. */
11671 : 8371 : if (expr1->ref && !(gfc_full_array_ref_p (expr1->ref, &c) || c))
11672 : : return true;
11673 : :
11674 : : /* Need a temporary if EXPR1 can't be expressed as a descriptor. */
11675 : 8134 : if (gfc_ref_needs_temporary_p (expr1->ref))
11676 : : return true;
11677 : :
11678 : : /* Functions returning pointers or allocatables need temporaries. */
11679 : 8122 : if (gfc_expr_attr (expr2).pointer
11680 : 8122 : || gfc_expr_attr (expr2).allocatable)
11681 : 381 : return true;
11682 : :
11683 : : /* Character array functions need temporaries unless the
11684 : : character lengths are the same. */
11685 : 7741 : if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
11686 : : {
11687 : 562 : if (UNLIMITED_POLY (expr1))
11688 : : return true;
11689 : :
11690 : 556 : if (expr1->ts.u.cl->length == NULL
11691 : 507 : || expr1->ts.u.cl->length->expr_type != EXPR_CONSTANT)
11692 : : return true;
11693 : :
11694 : 493 : if (expr2->ts.u.cl->length == NULL
11695 : 487 : || expr2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
11696 : : return true;
11697 : :
11698 : 475 : if (mpz_cmp (expr1->ts.u.cl->length->value.integer,
11699 : 475 : expr2->ts.u.cl->length->value.integer) != 0)
11700 : : return true;
11701 : : }
11702 : :
11703 : : /* Check that no LHS component references appear during an array
11704 : : reference. This is needed because we do not have the means to
11705 : : span any arbitrary stride with an array descriptor. This check
11706 : : is not needed for the rhs because the function result has to be
11707 : : a complete type. */
11708 : 7648 : seen_array_ref = false;
11709 : 15296 : for (ref = expr1->ref; ref; ref = ref->next)
11710 : : {
11711 : 7661 : if (ref->type == REF_ARRAY)
11712 : : seen_array_ref= true;
11713 : 13 : else if (ref->type == REF_COMPONENT && seen_array_ref)
11714 : : return true;
11715 : : }
11716 : :
11717 : : /* Check for a dependency. */
11718 : 7635 : if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
11719 : : expr2->value.function.esym,
11720 : : expr2->value.function.actual,
11721 : : NOT_ELEMENTAL))
11722 : : return true;
11723 : :
11724 : : /* If we have reached here with an intrinsic function, we do not
11725 : : need a temporary except in the particular case that reallocation
11726 : : on assignment is active and the lhs is allocatable and a target,
11727 : : or a pointer which may be a subref pointer. FIXME: The last
11728 : : condition can go away when we use span in the intrinsics
11729 : : directly.*/
11730 : 7198 : if (expr2->value.function.isym)
11731 : 6363 : return (flag_realloc_lhs && sym->attr.allocatable && sym->attr.target)
11732 : 12813 : || (sym->attr.pointer && sym->attr.subref_array_pointer);
11733 : :
11734 : : /* If the LHS is a dummy, we need a temporary if it is not
11735 : : INTENT(OUT). */
11736 : 760 : if (sym->attr.dummy && sym->attr.intent != INTENT_OUT)
11737 : : return true;
11738 : :
11739 : : /* If the lhs has been host_associated, is in common, a pointer or is
11740 : : a target and the function is not using a RESULT variable, aliasing
11741 : : can occur and a temporary is needed. */
11742 : 754 : if ((sym->attr.host_assoc
11743 : 700 : || sym->attr.in_common
11744 : 694 : || sym->attr.pointer
11745 : 688 : || sym->attr.cray_pointee
11746 : 688 : || sym->attr.target)
11747 : 66 : && expr2->symtree != NULL
11748 : 66 : && expr2->symtree->n.sym == expr2->symtree->n.sym->result)
11749 : : return true;
11750 : :
11751 : : /* A PURE function can unconditionally be called without a temporary. */
11752 : 712 : if (expr2->value.function.esym != NULL
11753 : 687 : && expr2->value.function.esym->attr.pure)
11754 : : return false;
11755 : :
11756 : : /* Implicit_pure functions are those which could legally be declared
11757 : : to be PURE. */
11758 : 684 : if (expr2->value.function.esym != NULL
11759 : 659 : && expr2->value.function.esym->attr.implicit_pure)
11760 : : return false;
11761 : :
11762 : 408 : if (!sym->attr.use_assoc
11763 : 408 : && !sym->attr.in_common
11764 : 408 : && !sym->attr.pointer
11765 : 402 : && !sym->attr.target
11766 : 402 : && !sym->attr.cray_pointee
11767 : 402 : && expr2->value.function.esym)
11768 : : {
11769 : : /* A temporary is not needed if the function is not contained and
11770 : : the variable is local or host associated and not a pointer or
11771 : : a target. */
11772 : 377 : if (!expr2->value.function.esym->attr.contained)
11773 : : return false;
11774 : :
11775 : : /* A temporary is not needed if the lhs has never been host
11776 : : associated and the procedure is contained. */
11777 : 146 : else if (!sym->attr.host_assoc)
11778 : : return false;
11779 : :
11780 : : /* A temporary is not needed if the variable is local and not
11781 : : a pointer, a target or a result. */
11782 : 6 : if (sym->ns->parent
11783 : 0 : && expr2->value.function.esym->ns == sym->ns->parent)
11784 : : return false;
11785 : : }
11786 : :
11787 : : /* Default to temporary use. */
11788 : : return true;
11789 : : }
11790 : :
11791 : :
11792 : : /* Provide the loop info so that the lhs descriptor can be built for
11793 : : reallocatable assignments from extrinsic function calls. */
11794 : :
11795 : : static void
11796 : 166 : realloc_lhs_loop_for_fcn_call (gfc_se *se, locus *where, gfc_ss **ss,
11797 : : gfc_loopinfo *loop)
11798 : : {
11799 : : /* Signal that the function call should not be made by
11800 : : gfc_conv_loop_setup. */
11801 : 166 : se->ss->is_alloc_lhs = 1;
11802 : 166 : gfc_init_loopinfo (loop);
11803 : 166 : gfc_add_ss_to_loop (loop, *ss);
11804 : 166 : gfc_add_ss_to_loop (loop, se->ss);
11805 : 166 : gfc_conv_ss_startstride (loop);
11806 : 166 : gfc_conv_loop_setup (loop, where);
11807 : 166 : gfc_copy_loopinfo_to_se (se, loop);
11808 : 166 : gfc_add_block_to_block (&se->pre, &loop->pre);
11809 : 166 : gfc_add_block_to_block (&se->pre, &loop->post);
11810 : 166 : se->ss->is_alloc_lhs = 0;
11811 : 166 : }
11812 : :
11813 : :
11814 : : /* For assignment to a reallocatable lhs from intrinsic functions,
11815 : : replace the se.expr (ie. the result) with a temporary descriptor.
11816 : : Null the data field so that the library allocates space for the
11817 : : result. Free the data of the original descriptor after the function,
11818 : : in case it appears in an argument expression and transfer the
11819 : : result to the original descriptor. */
11820 : :
11821 : : static void
11822 : 2109 : fcncall_realloc_result (gfc_se *se, int rank, tree dtype)
11823 : : {
11824 : 2109 : tree desc;
11825 : 2109 : tree res_desc;
11826 : 2109 : tree tmp;
11827 : 2109 : tree offset;
11828 : 2109 : tree zero_cond;
11829 : 2109 : tree not_same_shape;
11830 : 2109 : stmtblock_t shape_block;
11831 : 2109 : int n;
11832 : :
11833 : : /* Use the allocation done by the library. Substitute the lhs
11834 : : descriptor with a copy, whose data field is nulled.*/
11835 : 2109 : desc = build_fold_indirect_ref_loc (input_location, se->expr);
11836 : 2109 : if (POINTER_TYPE_P (TREE_TYPE (desc)))
11837 : 9 : desc = build_fold_indirect_ref_loc (input_location, desc);
11838 : :
11839 : : /* Unallocated, the descriptor does not have a dtype. */
11840 : 2109 : tmp = gfc_conv_descriptor_dtype (desc);
11841 : 2109 : if (dtype != NULL_TREE)
11842 : 13 : gfc_add_modify (&se->pre, tmp, dtype);
11843 : : else
11844 : 2096 : gfc_add_modify (&se->pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
11845 : :
11846 : 2109 : res_desc = gfc_evaluate_now (desc, &se->pre);
11847 : 2109 : gfc_conv_descriptor_data_set (&se->pre, res_desc, null_pointer_node);
11848 : 2109 : se->expr = gfc_build_addr_expr (NULL_TREE, res_desc);
11849 : :
11850 : : /* Free the lhs after the function call and copy the result data to
11851 : : the lhs descriptor. */
11852 : 2109 : tmp = gfc_conv_descriptor_data_get (desc);
11853 : 2109 : zero_cond = fold_build2_loc (input_location, EQ_EXPR,
11854 : : logical_type_node, tmp,
11855 : 2109 : build_int_cst (TREE_TYPE (tmp), 0));
11856 : 2109 : zero_cond = gfc_evaluate_now (zero_cond, &se->post);
11857 : 2109 : tmp = gfc_call_free (tmp);
11858 : 2109 : gfc_add_expr_to_block (&se->post, tmp);
11859 : :
11860 : 2109 : tmp = gfc_conv_descriptor_data_get (res_desc);
11861 : 2109 : gfc_conv_descriptor_data_set (&se->post, desc, tmp);
11862 : :
11863 : : /* Check that the shapes are the same between lhs and expression.
11864 : : The evaluation of the shape is done in 'shape_block' to avoid
11865 : : unitialized warnings from the lhs bounds. */
11866 : 2109 : not_same_shape = boolean_false_node;
11867 : 2109 : gfc_start_block (&shape_block);
11868 : 6801 : for (n = 0 ; n < rank; n++)
11869 : : {
11870 : 4692 : tree tmp1;
11871 : 4692 : tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
11872 : 4692 : tmp1 = gfc_conv_descriptor_lbound_get (res_desc, gfc_rank_cst[n]);
11873 : 4692 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
11874 : : gfc_array_index_type, tmp, tmp1);
11875 : 4692 : tmp1 = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
11876 : 4692 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
11877 : : gfc_array_index_type, tmp, tmp1);
11878 : 4692 : tmp1 = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
11879 : 4692 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
11880 : : gfc_array_index_type, tmp, tmp1);
11881 : 4692 : tmp = fold_build2_loc (input_location, NE_EXPR,
11882 : : logical_type_node, tmp,
11883 : : gfc_index_zero_node);
11884 : 4692 : tmp = gfc_evaluate_now (tmp, &shape_block);
11885 : 4692 : if (n == 0)
11886 : : not_same_shape = tmp;
11887 : : else
11888 : 2583 : not_same_shape = fold_build2_loc (input_location, TRUTH_OR_EXPR,
11889 : : logical_type_node, tmp,
11890 : : not_same_shape);
11891 : : }
11892 : :
11893 : : /* 'zero_cond' being true is equal to lhs not being allocated or the
11894 : : shapes being different. */
11895 : 2109 : tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR, logical_type_node,
11896 : : zero_cond, not_same_shape);
11897 : 2109 : gfc_add_modify (&shape_block, zero_cond, tmp);
11898 : 2109 : tmp = gfc_finish_block (&shape_block);
11899 : 2109 : tmp = build3_v (COND_EXPR, zero_cond,
11900 : : build_empty_stmt (input_location), tmp);
11901 : 2109 : gfc_add_expr_to_block (&se->post, tmp);
11902 : :
11903 : : /* Now reset the bounds returned from the function call to bounds based
11904 : : on the lhs lbounds, except where the lhs is not allocated or the shapes
11905 : : of 'variable and 'expr' are different. Set the offset accordingly. */
11906 : 2109 : offset = gfc_index_zero_node;
11907 : 6801 : for (n = 0 ; n < rank; n++)
11908 : : {
11909 : 4692 : tree lbound;
11910 : :
11911 : 4692 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
11912 : 4692 : lbound = fold_build3_loc (input_location, COND_EXPR,
11913 : : gfc_array_index_type, zero_cond,
11914 : : gfc_index_one_node, lbound);
11915 : 4692 : lbound = gfc_evaluate_now (lbound, &se->post);
11916 : :
11917 : 4692 : tmp = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
11918 : 4692 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
11919 : : gfc_array_index_type, tmp, lbound);
11920 : 4692 : gfc_conv_descriptor_lbound_set (&se->post, desc,
11921 : : gfc_rank_cst[n], lbound);
11922 : 4692 : gfc_conv_descriptor_ubound_set (&se->post, desc,
11923 : : gfc_rank_cst[n], tmp);
11924 : :
11925 : : /* Set stride and accumulate the offset. */
11926 : 4692 : tmp = gfc_conv_descriptor_stride_get (res_desc, gfc_rank_cst[n]);
11927 : 4692 : gfc_conv_descriptor_stride_set (&se->post, desc,
11928 : : gfc_rank_cst[n], tmp);
11929 : 4692 : tmp = fold_build2_loc (input_location, MULT_EXPR,
11930 : : gfc_array_index_type, lbound, tmp);
11931 : 4692 : offset = fold_build2_loc (input_location, MINUS_EXPR,
11932 : : gfc_array_index_type, offset, tmp);
11933 : 4692 : offset = gfc_evaluate_now (offset, &se->post);
11934 : : }
11935 : :
11936 : 2109 : gfc_conv_descriptor_offset_set (&se->post, desc, offset);
11937 : 2109 : }
11938 : :
11939 : :
11940 : :
11941 : : /* Try to translate array(:) = func (...), where func is a transformational
11942 : : array function, without using a temporary. Returns NULL if this isn't the
11943 : : case. */
11944 : :
11945 : : static tree
11946 : 14577 : gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
11947 : : {
11948 : 14577 : gfc_se se;
11949 : 14577 : gfc_ss *ss = NULL;
11950 : 14577 : gfc_component *comp = NULL;
11951 : 14577 : gfc_loopinfo loop;
11952 : 14577 : tree tmp;
11953 : 14577 : tree lhs;
11954 : 14577 : gfc_se final_se;
11955 : 14577 : gfc_symbol *sym = expr1->symtree->n.sym;
11956 : 14577 : bool finalizable = gfc_may_be_finalized (expr1->ts);
11957 : :
11958 : 14577 : if (arrayfunc_assign_needs_temporary (expr1, expr2))
11959 : : return NULL;
11960 : :
11961 : : /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
11962 : : functions. */
11963 : 7080 : comp = gfc_get_proc_ptr_comp (expr2);
11964 : :
11965 : 7080 : if (!(expr2->value.function.isym
11966 : 675 : || (comp && comp->attr.dimension)
11967 : 675 : || (!comp && gfc_return_by_reference (expr2->value.function.esym)
11968 : 675 : && expr2->value.function.esym->result->attr.dimension)))
11969 : 0 : return NULL;
11970 : :
11971 : 7080 : gfc_init_se (&se, NULL);
11972 : 7080 : gfc_start_block (&se.pre);
11973 : 7080 : se.want_pointer = 1;
11974 : :
11975 : : /* First the lhs must be finalized, if necessary. We use a copy of the symbol
11976 : : backend decl, stash the original away for the finalization so that the
11977 : : value used is that before the assignment. This is necessary because
11978 : : evaluation of the rhs expression using direct by reference can change
11979 : : the value. However, the standard mandates that the finalization must occur
11980 : : after evaluation of the rhs. */
11981 : 7080 : gfc_init_se (&final_se, NULL);
11982 : :
11983 : 7080 : if (finalizable)
11984 : : {
11985 : 33 : tmp = sym->backend_decl;
11986 : 33 : lhs = sym->backend_decl;
11987 : 33 : if (INDIRECT_REF_P (tmp))
11988 : 0 : tmp = TREE_OPERAND (tmp, 0);
11989 : 33 : sym->backend_decl = gfc_create_var (TREE_TYPE (tmp), "lhs");
11990 : 33 : gfc_add_modify (&se.pre, sym->backend_decl, tmp);
11991 : 33 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
11992 : : {
11993 : 0 : tmp = gfc_copy_alloc_comp (expr1->ts.u.derived, tmp, sym->backend_decl,
11994 : : expr1->rank, 0);
11995 : 0 : gfc_add_expr_to_block (&final_se.pre, tmp);
11996 : : }
11997 : : }
11998 : :
11999 : 33 : if (finalizable && gfc_assignment_finalizer_call (&final_se, expr1, false))
12000 : : {
12001 : 33 : gfc_add_block_to_block (&se.pre, &final_se.pre);
12002 : 33 : gfc_add_block_to_block (&se.post, &final_se.finalblock);
12003 : : }
12004 : :
12005 : 7080 : if (finalizable)
12006 : 33 : sym->backend_decl = lhs;
12007 : :
12008 : 7080 : gfc_conv_array_parameter (&se, expr1, false, NULL, NULL, NULL);
12009 : :
12010 : 7080 : if (expr1->ts.type == BT_DERIVED
12011 : 228 : && expr1->ts.u.derived->attr.alloc_comp)
12012 : : {
12013 : 80 : tmp = build_fold_indirect_ref_loc (input_location, se.expr);
12014 : 80 : tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, tmp,
12015 : : expr1->rank);
12016 : 80 : gfc_add_expr_to_block (&se.pre, tmp);
12017 : : }
12018 : :
12019 : 7080 : se.direct_byref = 1;
12020 : 7080 : se.ss = gfc_walk_expr (expr2);
12021 : 7080 : gcc_assert (se.ss != gfc_ss_terminator);
12022 : :
12023 : : /* Since this is a direct by reference call, references to the lhs can be
12024 : : used for finalization of the function result just as long as the blocks
12025 : : from final_se are added at the right time. */
12026 : 7080 : gfc_init_se (&final_se, NULL);
12027 : 7080 : if (finalizable && expr2->value.function.esym)
12028 : : {
12029 : 20 : final_se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
12030 : 20 : gfc_finalize_tree_expr (&final_se, expr2->ts.u.derived,
12031 : 20 : expr2->value.function.esym->attr,
12032 : : expr2->rank);
12033 : : }
12034 : :
12035 : : /* Reallocate on assignment needs the loopinfo for extrinsic functions.
12036 : : This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
12037 : : Clearly, this cannot be done for an allocatable function result, since
12038 : : the shape of the result is unknown and, in any case, the function must
12039 : : correctly take care of the reallocation internally. For intrinsic
12040 : : calls, the array data is freed and the library takes care of allocation.
12041 : : TODO: Add logic of trans-array.cc: gfc_alloc_allocatable_for_assignment
12042 : : to the library. */
12043 : 7080 : if (flag_realloc_lhs
12044 : 7005 : && gfc_is_reallocatable_lhs (expr1)
12045 : 9355 : && !gfc_expr_attr (expr1).codimension
12046 : 2275 : && !gfc_is_coindexed (expr1)
12047 : 9355 : && !(expr2->value.function.esym
12048 : 166 : && expr2->value.function.esym->result->attr.allocatable))
12049 : : {
12050 : 2275 : realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
12051 : :
12052 : 2275 : if (!expr2->value.function.isym)
12053 : : {
12054 : 166 : ss = gfc_walk_expr (expr1);
12055 : 166 : gcc_assert (ss != gfc_ss_terminator);
12056 : :
12057 : 166 : realloc_lhs_loop_for_fcn_call (&se, &expr1->where, &ss, &loop);
12058 : 166 : ss->is_alloc_lhs = 1;
12059 : : }
12060 : : else
12061 : : {
12062 : 2109 : tree dtype = NULL_TREE;
12063 : 2109 : tree type = gfc_typenode_for_spec (&expr2->ts);
12064 : 2109 : if (expr1->ts.type == BT_CLASS)
12065 : : {
12066 : 13 : tmp = gfc_class_vptr_get (sym->backend_decl);
12067 : 13 : tree tmp2 = gfc_get_symbol_decl (gfc_find_vtab (&expr2->ts));
12068 : 13 : tmp2 = gfc_build_addr_expr (TREE_TYPE (tmp), tmp2);
12069 : 13 : gfc_add_modify (&se.pre, tmp, tmp2);
12070 : 13 : dtype = gfc_get_dtype_rank_type (expr1->rank,type);
12071 : : }
12072 : 2109 : fcncall_realloc_result (&se, expr1->rank, dtype);
12073 : : }
12074 : : }
12075 : :
12076 : 7080 : gfc_conv_function_expr (&se, expr2);
12077 : :
12078 : : /* Fix the result. */
12079 : 7080 : gfc_add_block_to_block (&se.pre, &se.post);
12080 : 7080 : if (finalizable)
12081 : 33 : gfc_add_block_to_block (&se.pre, &final_se.pre);
12082 : :
12083 : : /* Do the finalization, including final calls from function arguments. */
12084 : 33 : if (finalizable)
12085 : : {
12086 : 33 : gfc_add_block_to_block (&se.pre, &final_se.post);
12087 : 33 : gfc_add_block_to_block (&se.pre, &se.finalblock);
12088 : 33 : gfc_add_block_to_block (&se.pre, &final_se.finalblock);
12089 : : }
12090 : :
12091 : 7080 : if (ss)
12092 : 166 : gfc_cleanup_loop (&loop);
12093 : : else
12094 : 6914 : gfc_free_ss_chain (se.ss);
12095 : :
12096 : 7080 : return gfc_finish_block (&se.pre);
12097 : : }
12098 : :
12099 : :
12100 : : /* Try to efficiently translate array(:) = 0. Return NULL if this
12101 : : can't be done. */
12102 : :
12103 : : static tree
12104 : 3924 : gfc_trans_zero_assign (gfc_expr * expr)
12105 : : {
12106 : 3924 : tree dest, len, type;
12107 : 3924 : tree tmp;
12108 : 3924 : gfc_symbol *sym;
12109 : :
12110 : 3924 : sym = expr->symtree->n.sym;
12111 : 3924 : dest = gfc_get_symbol_decl (sym);
12112 : :
12113 : 3924 : type = TREE_TYPE (dest);
12114 : 3924 : if (POINTER_TYPE_P (type))
12115 : 247 : type = TREE_TYPE (type);
12116 : 3924 : if (GFC_ARRAY_TYPE_P (type))
12117 : : {
12118 : : /* Determine the length of the array. */
12119 : 2749 : len = GFC_TYPE_ARRAY_SIZE (type);
12120 : 2749 : if (!len || TREE_CODE (len) != INTEGER_CST)
12121 : : return NULL_TREE;
12122 : : }
12123 : 1175 : else if (GFC_DESCRIPTOR_TYPE_P (type)
12124 : 1175 : && gfc_is_simply_contiguous (expr, false, false))
12125 : : {
12126 : 1075 : if (POINTER_TYPE_P (TREE_TYPE (dest)))
12127 : 4 : dest = build_fold_indirect_ref_loc (input_location, dest);
12128 : 1075 : len = gfc_conv_descriptor_size (dest, GFC_TYPE_ARRAY_RANK (type));
12129 : 1075 : dest = gfc_conv_descriptor_data_get (dest);
12130 : : }
12131 : : else
12132 : 100 : return NULL_TREE;
12133 : :
12134 : : /* If we are zeroing a local array avoid taking its address by emitting
12135 : : a = {} instead. */
12136 : 3646 : if (!POINTER_TYPE_P (TREE_TYPE (dest)))
12137 : 2529 : return build2_loc (input_location, MODIFY_EXPR, void_type_node,
12138 : 2529 : dest, build_constructor (TREE_TYPE (dest),
12139 : 2529 : NULL));
12140 : :
12141 : : /* Multiply len by element size. */
12142 : 1117 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
12143 : 1117 : len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
12144 : : len, fold_convert (gfc_array_index_type, tmp));
12145 : :
12146 : : /* Convert arguments to the correct types. */
12147 : 1117 : dest = fold_convert (pvoid_type_node, dest);
12148 : 1117 : len = fold_convert (size_type_node, len);
12149 : :
12150 : : /* Construct call to __builtin_memset. */
12151 : 1117 : tmp = build_call_expr_loc (input_location,
12152 : : builtin_decl_explicit (BUILT_IN_MEMSET),
12153 : : 3, dest, integer_zero_node, len);
12154 : 1117 : return fold_convert (void_type_node, tmp);
12155 : : }
12156 : :
12157 : :
12158 : : /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
12159 : : that constructs the call to __builtin_memcpy. */
12160 : :
12161 : : tree
12162 : 7008 : gfc_build_memcpy_call (tree dst, tree src, tree len)
12163 : : {
12164 : 7008 : tree tmp;
12165 : :
12166 : : /* Convert arguments to the correct types. */
12167 : 7008 : if (!POINTER_TYPE_P (TREE_TYPE (dst)))
12168 : 6775 : dst = gfc_build_addr_expr (pvoid_type_node, dst);
12169 : : else
12170 : 233 : dst = fold_convert (pvoid_type_node, dst);
12171 : :
12172 : 7008 : if (!POINTER_TYPE_P (TREE_TYPE (src)))
12173 : 6674 : src = gfc_build_addr_expr (pvoid_type_node, src);
12174 : : else
12175 : 334 : src = fold_convert (pvoid_type_node, src);
12176 : :
12177 : 7008 : len = fold_convert (size_type_node, len);
12178 : :
12179 : : /* Construct call to __builtin_memcpy. */
12180 : 7008 : tmp = build_call_expr_loc (input_location,
12181 : : builtin_decl_explicit (BUILT_IN_MEMCPY),
12182 : : 3, dst, src, len);
12183 : 7008 : return fold_convert (void_type_node, tmp);
12184 : : }
12185 : :
12186 : :
12187 : : /* Try to efficiently translate dst(:) = src(:). Return NULL if this
12188 : : can't be done. EXPR1 is the destination/lhs and EXPR2 is the
12189 : : source/rhs, both are gfc_full_array_ref_p which have been checked for
12190 : : dependencies. */
12191 : :
12192 : : static tree
12193 : 2503 : gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
12194 : : {
12195 : 2503 : tree dst, dlen, dtype;
12196 : 2503 : tree src, slen, stype;
12197 : 2503 : tree tmp;
12198 : :
12199 : 2503 : dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
12200 : 2503 : src = gfc_get_symbol_decl (expr2->symtree->n.sym);
12201 : :
12202 : 2503 : dtype = TREE_TYPE (dst);
12203 : 2503 : if (POINTER_TYPE_P (dtype))
12204 : 223 : dtype = TREE_TYPE (dtype);
12205 : 2503 : stype = TREE_TYPE (src);
12206 : 2503 : if (POINTER_TYPE_P (stype))
12207 : 265 : stype = TREE_TYPE (stype);
12208 : :
12209 : 2503 : if (!GFC_ARRAY_TYPE_P (dtype) || !GFC_ARRAY_TYPE_P (stype))
12210 : : return NULL_TREE;
12211 : :
12212 : : /* Determine the lengths of the arrays. */
12213 : 1533 : dlen = GFC_TYPE_ARRAY_SIZE (dtype);
12214 : 1533 : if (!dlen || TREE_CODE (dlen) != INTEGER_CST)
12215 : : return NULL_TREE;
12216 : 1444 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
12217 : 1444 : dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
12218 : : dlen, fold_convert (gfc_array_index_type, tmp));
12219 : :
12220 : 1444 : slen = GFC_TYPE_ARRAY_SIZE (stype);
12221 : 1444 : if (!slen || TREE_CODE (slen) != INTEGER_CST)
12222 : : return NULL_TREE;
12223 : 1438 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (stype));
12224 : 1438 : slen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
12225 : : slen, fold_convert (gfc_array_index_type, tmp));
12226 : :
12227 : : /* Sanity check that they are the same. This should always be
12228 : : the case, as we should already have checked for conformance. */
12229 : 1438 : if (!tree_int_cst_equal (slen, dlen))
12230 : : return NULL_TREE;
12231 : :
12232 : 1438 : return gfc_build_memcpy_call (dst, src, dlen);
12233 : : }
12234 : :
12235 : :
12236 : : /* Try to efficiently translate array(:) = (/ ... /). Return NULL if
12237 : : this can't be done. EXPR1 is the destination/lhs for which
12238 : : gfc_full_array_ref_p is true, and EXPR2 is the source/rhs. */
12239 : :
12240 : : static tree
12241 : 7448 : gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
12242 : : {
12243 : 7448 : unsigned HOST_WIDE_INT nelem;
12244 : 7448 : tree dst, dtype;
12245 : 7448 : tree src, stype;
12246 : 7448 : tree len;
12247 : 7448 : tree tmp;
12248 : :
12249 : 7448 : nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
12250 : 7448 : if (nelem == 0)
12251 : : return NULL_TREE;
12252 : :
12253 : 5818 : dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
12254 : 5818 : dtype = TREE_TYPE (dst);
12255 : 5818 : if (POINTER_TYPE_P (dtype))
12256 : 252 : dtype = TREE_TYPE (dtype);
12257 : 5818 : if (!GFC_ARRAY_TYPE_P (dtype))
12258 : : return NULL_TREE;
12259 : :
12260 : : /* Determine the lengths of the array. */
12261 : 5148 : len = GFC_TYPE_ARRAY_SIZE (dtype);
12262 : 5148 : if (!len || TREE_CODE (len) != INTEGER_CST)
12263 : : return NULL_TREE;
12264 : :
12265 : : /* Confirm that the constructor is the same size. */
12266 : 5050 : if (compare_tree_int (len, nelem) != 0)
12267 : : return NULL_TREE;
12268 : :
12269 : 5050 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
12270 : 5050 : len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
12271 : : fold_convert (gfc_array_index_type, tmp));
12272 : :
12273 : 5050 : stype = gfc_typenode_for_spec (&expr2->ts);
12274 : 5050 : src = gfc_build_constant_array_constructor (expr2, stype);
12275 : :
12276 : 5050 : return gfc_build_memcpy_call (dst, src, len);
12277 : : }
12278 : :
12279 : :
12280 : : /* Tells whether the expression is to be treated as a variable reference. */
12281 : :
12282 : : bool
12283 : 300344 : gfc_expr_is_variable (gfc_expr *expr)
12284 : : {
12285 : 300604 : gfc_expr *arg;
12286 : 300604 : gfc_component *comp;
12287 : 300604 : gfc_symbol *func_ifc;
12288 : :
12289 : 300604 : if (expr->expr_type == EXPR_VARIABLE)
12290 : : return true;
12291 : :
12292 : 267674 : arg = gfc_get_noncopying_intrinsic_argument (expr);
12293 : 267674 : if (arg)
12294 : : {
12295 : 260 : gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
12296 : : return gfc_expr_is_variable (arg);
12297 : : }
12298 : :
12299 : : /* A data-pointer-returning function should be considered as a variable
12300 : : too. */
12301 : 267414 : if (expr->expr_type == EXPR_FUNCTION
12302 : 35315 : && expr->ref == NULL)
12303 : : {
12304 : 34948 : if (expr->value.function.isym != NULL)
12305 : : return false;
12306 : :
12307 : 8980 : if (expr->value.function.esym != NULL)
12308 : : {
12309 : 8971 : func_ifc = expr->value.function.esym;
12310 : 8971 : goto found_ifc;
12311 : : }
12312 : 9 : gcc_assert (expr->symtree);
12313 : 9 : func_ifc = expr->symtree->n.sym;
12314 : 9 : goto found_ifc;
12315 : : }
12316 : :
12317 : 232466 : comp = gfc_get_proc_ptr_comp (expr);
12318 : 232466 : if ((expr->expr_type == EXPR_PPC || expr->expr_type == EXPR_FUNCTION)
12319 : 367 : && comp)
12320 : : {
12321 : 265 : func_ifc = comp->ts.interface;
12322 : 265 : goto found_ifc;
12323 : : }
12324 : :
12325 : 232201 : if (expr->expr_type == EXPR_COMPCALL)
12326 : : {
12327 : 0 : gcc_assert (!expr->value.compcall.tbp->is_generic);
12328 : 0 : func_ifc = expr->value.compcall.tbp->u.specific->n.sym;
12329 : 0 : goto found_ifc;
12330 : : }
12331 : :
12332 : : return false;
12333 : :
12334 : 9245 : found_ifc:
12335 : 9245 : gcc_assert (func_ifc->attr.function
12336 : : && func_ifc->result != NULL);
12337 : 9245 : return func_ifc->result->attr.pointer;
12338 : : }
12339 : :
12340 : :
12341 : : /* Is the lhs OK for automatic reallocation? */
12342 : :
12343 : : static bool
12344 : 255383 : is_scalar_reallocatable_lhs (gfc_expr *expr)
12345 : : {
12346 : 255383 : gfc_ref * ref;
12347 : :
12348 : : /* An allocatable variable with no reference. */
12349 : 255383 : if (expr->symtree->n.sym->attr.allocatable
12350 : 6539 : && !expr->ref)
12351 : : return true;
12352 : :
12353 : : /* All that can be left are allocatable components. However, we do
12354 : : not check for allocatable components here because the expression
12355 : : could be an allocatable component of a pointer component. */
12356 : 252755 : if (expr->symtree->n.sym->ts.type != BT_DERIVED
12357 : 232089 : && expr->symtree->n.sym->ts.type != BT_CLASS)
12358 : : return false;
12359 : :
12360 : : /* Find an allocatable component ref last. */
12361 : 36848 : for (ref = expr->ref; ref; ref = ref->next)
12362 : 15211 : if (ref->type == REF_COMPONENT
12363 : 11330 : && !ref->next
12364 : 8828 : && ref->u.c.component->attr.allocatable)
12365 : : return true;
12366 : :
12367 : : return false;
12368 : : }
12369 : :
12370 : :
12371 : : /* Allocate or reallocate scalar lhs, as necessary. */
12372 : :
12373 : : static void
12374 : 3359 : alloc_scalar_allocatable_for_assignment (stmtblock_t *block,
12375 : : tree string_length,
12376 : : gfc_expr *expr1,
12377 : : gfc_expr *expr2)
12378 : :
12379 : : {
12380 : 3359 : tree cond;
12381 : 3359 : tree tmp;
12382 : 3359 : tree size;
12383 : 3359 : tree size_in_bytes;
12384 : 3359 : tree jump_label1;
12385 : 3359 : tree jump_label2;
12386 : 3359 : gfc_se lse;
12387 : 3359 : gfc_ref *ref;
12388 : :
12389 : 3359 : if (!expr1 || expr1->rank)
12390 : 0 : return;
12391 : :
12392 : 3359 : if (!expr2 || expr2->rank)
12393 : : return;
12394 : :
12395 : 4577 : for (ref = expr1->ref; ref; ref = ref->next)
12396 : 1218 : if (ref->type == REF_SUBSTRING)
12397 : : return;
12398 : :
12399 : 3359 : realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
12400 : :
12401 : : /* Since this is a scalar lhs, we can afford to do this. That is,
12402 : : there is no risk of side effects being repeated. */
12403 : 3359 : gfc_init_se (&lse, NULL);
12404 : 3359 : lse.want_pointer = 1;
12405 : 3359 : gfc_conv_expr (&lse, expr1);
12406 : :
12407 : 3359 : jump_label1 = gfc_build_label_decl (NULL_TREE);
12408 : 3359 : jump_label2 = gfc_build_label_decl (NULL_TREE);
12409 : :
12410 : : /* Do the allocation if the lhs is NULL. Otherwise go to label 1. */
12411 : 3359 : tmp = build_int_cst (TREE_TYPE (lse.expr), 0);
12412 : 3359 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
12413 : : lse.expr, tmp);
12414 : 3359 : tmp = build3_v (COND_EXPR, cond,
12415 : : build1_v (GOTO_EXPR, jump_label1),
12416 : : build_empty_stmt (input_location));
12417 : 3359 : gfc_add_expr_to_block (block, tmp);
12418 : :
12419 : 3359 : if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
12420 : : {
12421 : : /* Use the rhs string length and the lhs element size. Note that 'size' is
12422 : : used below for the string-length comparison, only. */
12423 : 1365 : size = string_length;
12424 : 1365 : tmp = TYPE_SIZE_UNIT (gfc_get_char_type (expr1->ts.kind));
12425 : 2730 : size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
12426 : 1365 : TREE_TYPE (tmp), tmp,
12427 : 1365 : fold_convert (TREE_TYPE (tmp), size));
12428 : : }
12429 : : else
12430 : : {
12431 : : /* Otherwise use the length in bytes of the rhs. */
12432 : 1994 : size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr1->ts));
12433 : 1994 : size_in_bytes = size;
12434 : : }
12435 : :
12436 : 3359 : size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
12437 : : size_in_bytes, size_one_node);
12438 : :
12439 : 3359 : if (gfc_caf_attr (expr1).codimension && flag_coarray == GFC_FCOARRAY_LIB)
12440 : : {
12441 : 31 : tree caf_decl, token;
12442 : 31 : gfc_se caf_se;
12443 : 31 : symbol_attribute attr;
12444 : :
12445 : 31 : gfc_clear_attr (&attr);
12446 : 31 : gfc_init_se (&caf_se, NULL);
12447 : :
12448 : 31 : caf_decl = gfc_get_tree_for_caf_expr (expr1);
12449 : 31 : gfc_get_caf_token_offset (&caf_se, &token, NULL, caf_decl, NULL_TREE,
12450 : : NULL);
12451 : 31 : gfc_add_block_to_block (block, &caf_se.pre);
12452 : 31 : gfc_allocate_allocatable (block, lse.expr, size_in_bytes,
12453 : : gfc_build_addr_expr (NULL_TREE, token),
12454 : : NULL_TREE, NULL_TREE, NULL_TREE, jump_label1,
12455 : : expr1, 1);
12456 : : }
12457 : 3328 : else if (expr1->ts.type == BT_DERIVED && expr1->ts.u.derived->attr.alloc_comp)
12458 : : {
12459 : 34 : tmp = build_call_expr_loc (input_location,
12460 : : builtin_decl_explicit (BUILT_IN_CALLOC),
12461 : : 2, build_one_cst (size_type_node),
12462 : : size_in_bytes);
12463 : 34 : tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
12464 : 34 : gfc_add_modify (block, lse.expr, tmp);
12465 : : }
12466 : : else
12467 : : {
12468 : 3294 : tmp = build_call_expr_loc (input_location,
12469 : : builtin_decl_explicit (BUILT_IN_MALLOC),
12470 : : 1, size_in_bytes);
12471 : 3294 : tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
12472 : 3294 : gfc_add_modify (block, lse.expr, tmp);
12473 : : }
12474 : :
12475 : 3359 : if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
12476 : : {
12477 : : /* Deferred characters need checking for lhs and rhs string
12478 : : length. Other deferred parameter variables will have to
12479 : : come here too. */
12480 : 1365 : tmp = build1_v (GOTO_EXPR, jump_label2);
12481 : 1365 : gfc_add_expr_to_block (block, tmp);
12482 : : }
12483 : 3359 : tmp = build1_v (LABEL_EXPR, jump_label1);
12484 : 3359 : gfc_add_expr_to_block (block, tmp);
12485 : :
12486 : : /* For a deferred length character, reallocate if lengths of lhs and
12487 : : rhs are different. */
12488 : 3359 : if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
12489 : : {
12490 : 1365 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
12491 : : lse.string_length,
12492 : 1365 : fold_convert (TREE_TYPE (lse.string_length),
12493 : : size));
12494 : : /* Jump past the realloc if the lengths are the same. */
12495 : 1365 : tmp = build3_v (COND_EXPR, cond,
12496 : : build1_v (GOTO_EXPR, jump_label2),
12497 : : build_empty_stmt (input_location));
12498 : 1365 : gfc_add_expr_to_block (block, tmp);
12499 : 1365 : tmp = build_call_expr_loc (input_location,
12500 : : builtin_decl_explicit (BUILT_IN_REALLOC),
12501 : : 2, fold_convert (pvoid_type_node, lse.expr),
12502 : : size_in_bytes);
12503 : 1365 : tree omp_cond = NULL_TREE;
12504 : 1365 : if (flag_openmp_allocators)
12505 : : {
12506 : 1 : tree omp_tmp;
12507 : 1 : omp_cond = gfc_omp_call_is_alloc (lse.expr);
12508 : 1 : omp_cond = gfc_evaluate_now (omp_cond, block);
12509 : :
12510 : 1 : omp_tmp = builtin_decl_explicit (BUILT_IN_GOMP_REALLOC);
12511 : 1 : omp_tmp = build_call_expr_loc (input_location, omp_tmp, 4,
12512 : : fold_convert (pvoid_type_node,
12513 : : lse.expr), size_in_bytes,
12514 : : build_zero_cst (ptr_type_node),
12515 : : build_zero_cst (ptr_type_node));
12516 : 1 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
12517 : : omp_cond, omp_tmp, tmp);
12518 : : }
12519 : 1365 : tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
12520 : 1365 : gfc_add_modify (block, lse.expr, tmp);
12521 : 1365 : if (omp_cond)
12522 : 1 : gfc_add_expr_to_block (block,
12523 : : build3_loc (input_location, COND_EXPR,
12524 : : void_type_node, omp_cond,
12525 : : gfc_omp_call_add_alloc (lse.expr),
12526 : : build_empty_stmt (input_location)));
12527 : 1365 : tmp = build1_v (LABEL_EXPR, jump_label2);
12528 : 1365 : gfc_add_expr_to_block (block, tmp);
12529 : :
12530 : : /* Update the lhs character length. */
12531 : 1365 : size = string_length;
12532 : 1365 : gfc_add_modify (block, lse.string_length,
12533 : 1365 : fold_convert (TREE_TYPE (lse.string_length), size));
12534 : : }
12535 : : }
12536 : :
12537 : : /* Check for assignments of the type
12538 : :
12539 : : a = a + 4
12540 : :
12541 : : to make sure we do not check for reallocation unneccessarily. */
12542 : :
12543 : :
12544 : : static bool
12545 : 6431 : is_runtime_conformable (gfc_expr *expr1, gfc_expr *expr2)
12546 : : {
12547 : 6736 : gfc_actual_arglist *a;
12548 : 6736 : gfc_expr *e1, *e2;
12549 : :
12550 : 6736 : switch (expr2->expr_type)
12551 : : {
12552 : 1776 : case EXPR_VARIABLE:
12553 : 1776 : return gfc_dep_compare_expr (expr1, expr2) == 0;
12554 : :
12555 : 2772 : case EXPR_FUNCTION:
12556 : 2772 : if (expr2->value.function.esym
12557 : 269 : && expr2->value.function.esym->attr.elemental)
12558 : : {
12559 : 51 : for (a = expr2->value.function.actual; a != NULL; a = a->next)
12560 : : {
12561 : 50 : e1 = a->expr;
12562 : 50 : if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
12563 : : return false;
12564 : : }
12565 : : return true;
12566 : : }
12567 : 2734 : else if (expr2->value.function.isym
12568 : 2489 : && expr2->value.function.isym->elemental)
12569 : : {
12570 : 324 : for (a = expr2->value.function.actual; a != NULL; a = a->next)
12571 : : {
12572 : 314 : e1 = a->expr;
12573 : 314 : if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
12574 : : return false;
12575 : : }
12576 : : return true;
12577 : : }
12578 : :
12579 : : break;
12580 : :
12581 : 499 : case EXPR_OP:
12582 : 499 : switch (expr2->value.op.op)
12583 : : {
12584 : 34 : case INTRINSIC_NOT:
12585 : 34 : case INTRINSIC_UPLUS:
12586 : 34 : case INTRINSIC_UMINUS:
12587 : 34 : case INTRINSIC_PARENTHESES:
12588 : 34 : return is_runtime_conformable (expr1, expr2->value.op.op1);
12589 : :
12590 : 440 : case INTRINSIC_PLUS:
12591 : 440 : case INTRINSIC_MINUS:
12592 : 440 : case INTRINSIC_TIMES:
12593 : 440 : case INTRINSIC_DIVIDE:
12594 : 440 : case INTRINSIC_POWER:
12595 : 440 : case INTRINSIC_AND:
12596 : 440 : case INTRINSIC_OR:
12597 : 440 : case INTRINSIC_EQV:
12598 : 440 : case INTRINSIC_NEQV:
12599 : 440 : case INTRINSIC_EQ:
12600 : 440 : case INTRINSIC_NE:
12601 : 440 : case INTRINSIC_GT:
12602 : 440 : case INTRINSIC_GE:
12603 : 440 : case INTRINSIC_LT:
12604 : 440 : case INTRINSIC_LE:
12605 : 440 : case INTRINSIC_EQ_OS:
12606 : 440 : case INTRINSIC_NE_OS:
12607 : 440 : case INTRINSIC_GT_OS:
12608 : 440 : case INTRINSIC_GE_OS:
12609 : 440 : case INTRINSIC_LT_OS:
12610 : 440 : case INTRINSIC_LE_OS:
12611 : :
12612 : 440 : e1 = expr2->value.op.op1;
12613 : 440 : e2 = expr2->value.op.op2;
12614 : :
12615 : 440 : if (e1->rank == 0 && e2->rank > 0)
12616 : : return is_runtime_conformable (expr1, e2);
12617 : 382 : else if (e1->rank > 0 && e2->rank == 0)
12618 : : return is_runtime_conformable (expr1, e1);
12619 : 169 : else if (e1->rank > 0 && e2->rank > 0)
12620 : 169 : return is_runtime_conformable (expr1, e1)
12621 : 169 : && is_runtime_conformable (expr1, e2);
12622 : : break;
12623 : :
12624 : : default:
12625 : : break;
12626 : :
12627 : : }
12628 : :
12629 : : break;
12630 : :
12631 : : default:
12632 : : break;
12633 : : }
12634 : : return false;
12635 : : }
12636 : :
12637 : :
12638 : : static tree
12639 : 3183 : trans_class_assignment (stmtblock_t *block, gfc_expr *lhs, gfc_expr *rhs,
12640 : : gfc_se *lse, gfc_se *rse, bool use_vptr_copy,
12641 : : bool class_realloc)
12642 : : {
12643 : 3183 : tree tmp, fcn, stdcopy, to_len, from_len, vptr, old_vptr, rhs_vptr;
12644 : 3183 : vec<tree, va_gc> *args = NULL;
12645 : 3183 : bool final_expr;
12646 : :
12647 : 3183 : final_expr = gfc_assignment_finalizer_call (lse, lhs, false);
12648 : 3183 : if (final_expr)
12649 : : {
12650 : 432 : if (rse->loop)
12651 : 172 : gfc_prepend_expr_to_block (&rse->loop->pre,
12652 : : gfc_finish_block (&lse->finalblock));
12653 : : else
12654 : 260 : gfc_add_block_to_block (block, &lse->finalblock);
12655 : : }
12656 : :
12657 : : /* Store the old vptr so that dynamic types can be compared for
12658 : : reallocation to occur or not. */
12659 : 3183 : if (class_realloc)
12660 : : {
12661 : 272 : tmp = lse->expr;
12662 : 272 : if (!GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
12663 : 18 : tmp = gfc_get_class_from_expr (tmp);
12664 : : }
12665 : :
12666 : 3183 : vptr = trans_class_vptr_len_assignment (block, lhs, rhs, rse, &to_len,
12667 : : &from_len, &rhs_vptr);
12668 : 3183 : if (rhs_vptr == NULL_TREE)
12669 : 61 : rhs_vptr = vptr;
12670 : :
12671 : : /* Generate (re)allocation of the lhs. */
12672 : 3183 : if (class_realloc)
12673 : : {
12674 : 272 : stmtblock_t alloc, re_alloc;
12675 : 272 : tree class_han, re, size;
12676 : :
12677 : 272 : if (tmp && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
12678 : 254 : old_vptr = gfc_evaluate_now (gfc_class_vptr_get (tmp), block);
12679 : : else
12680 : 18 : old_vptr = build_int_cst (TREE_TYPE (vptr), 0);
12681 : :
12682 : 272 : size = gfc_vptr_size_get (rhs_vptr);
12683 : :
12684 : : /* Take into account _len of unlimited polymorphic entities.
12685 : : TODO: handle class(*) allocatable function results on rhs. */
12686 : 272 : if (UNLIMITED_POLY (rhs))
12687 : : {
12688 : 18 : tree len;
12689 : 18 : if (rhs->expr_type == EXPR_VARIABLE)
12690 : 12 : len = trans_get_upoly_len (block, rhs);
12691 : : else
12692 : 6 : len = gfc_class_len_get (tmp);
12693 : 18 : len = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
12694 : : fold_convert (size_type_node, len),
12695 : : size_one_node);
12696 : 18 : size = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (size),
12697 : 18 : size, fold_convert (TREE_TYPE (size), len));
12698 : 18 : }
12699 : 254 : else if (rhs->ts.type == BT_CHARACTER && rse->string_length)
12700 : 21 : size = fold_build2_loc (input_location, MULT_EXPR,
12701 : : gfc_charlen_type_node, size,
12702 : : rse->string_length);
12703 : :
12704 : :
12705 : 272 : tmp = lse->expr;
12706 : 272 : class_han = GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
12707 : 272 : ? gfc_class_data_get (tmp) : tmp;
12708 : :
12709 : 272 : if (!POINTER_TYPE_P (TREE_TYPE (class_han)))
12710 : 18 : class_han = gfc_build_addr_expr (NULL_TREE, class_han);
12711 : :
12712 : : /* Allocate block. */
12713 : 272 : gfc_init_block (&alloc);
12714 : 272 : gfc_allocate_using_malloc (&alloc, class_han, size, NULL_TREE);
12715 : :
12716 : : /* Reallocate if dynamic types are different. */
12717 : 272 : gfc_init_block (&re_alloc);
12718 : 272 : if (UNLIMITED_POLY (lhs) && rhs->ts.type == BT_CHARACTER)
12719 : : {
12720 : 21 : gfc_add_expr_to_block (&re_alloc, gfc_call_free (class_han));
12721 : 21 : gfc_allocate_using_malloc (&re_alloc, class_han, size, NULL_TREE);
12722 : : }
12723 : : else
12724 : : {
12725 : 251 : tmp = fold_convert (pvoid_type_node, class_han);
12726 : 251 : re = build_call_expr_loc (input_location,
12727 : : builtin_decl_explicit (BUILT_IN_REALLOC),
12728 : : 2, tmp, size);
12729 : 251 : re = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (tmp),
12730 : : tmp, re);
12731 : 251 : tmp = fold_build2_loc (input_location, NE_EXPR,
12732 : : logical_type_node, rhs_vptr, old_vptr);
12733 : 251 : re = fold_build3_loc (input_location, COND_EXPR, void_type_node,
12734 : : tmp, re, build_empty_stmt (input_location));
12735 : 251 : gfc_add_expr_to_block (&re_alloc, re);
12736 : : }
12737 : 272 : tree realloc_expr = lhs->ts.type == BT_CLASS ?
12738 : 254 : gfc_finish_block (&re_alloc) :
12739 : 18 : build_empty_stmt (input_location);
12740 : :
12741 : : /* Allocate if _data is NULL, reallocate otherwise. */
12742 : 272 : tmp = fold_build2_loc (input_location, EQ_EXPR,
12743 : : logical_type_node, class_han,
12744 : : build_int_cst (prvoid_type_node, 0));
12745 : 272 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
12746 : : gfc_unlikely (tmp,
12747 : : PRED_FORTRAN_FAIL_ALLOC),
12748 : : gfc_finish_block (&alloc),
12749 : : realloc_expr);
12750 : 272 : gfc_add_expr_to_block (&lse->pre, tmp);
12751 : : }
12752 : :
12753 : 3183 : fcn = gfc_vptr_copy_get (vptr);
12754 : :
12755 : 3183 : tmp = GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr))
12756 : 3183 : ? gfc_class_data_get (rse->expr) : rse->expr;
12757 : 3183 : if (use_vptr_copy)
12758 : : {
12759 : 5350 : if (!POINTER_TYPE_P (TREE_TYPE (tmp))
12760 : 518 : || INDIRECT_REF_P (tmp)
12761 : 397 : || (rhs->ts.type == BT_DERIVED
12762 : 0 : && rhs->ts.u.derived->attr.unlimited_polymorphic
12763 : 0 : && !rhs->ts.u.derived->attr.pointer
12764 : 0 : && !rhs->ts.u.derived->attr.allocatable)
12765 : 3328 : || (UNLIMITED_POLY (rhs)
12766 : 134 : && !CLASS_DATA (rhs)->attr.pointer
12767 : 43 : && !CLASS_DATA (rhs)->attr.allocatable))
12768 : 2534 : vec_safe_push (args, gfc_build_addr_expr (NULL_TREE, tmp));
12769 : : else
12770 : 397 : vec_safe_push (args, tmp);
12771 : 2931 : tmp = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
12772 : 2931 : ? gfc_class_data_get (lse->expr) : lse->expr;
12773 : 5165 : if (!POINTER_TYPE_P (TREE_TYPE (tmp))
12774 : 697 : || INDIRECT_REF_P (tmp)
12775 : 254 : || (lhs->ts.type == BT_DERIVED
12776 : 0 : && lhs->ts.u.derived->attr.unlimited_polymorphic
12777 : 0 : && !lhs->ts.u.derived->attr.pointer
12778 : 0 : && !lhs->ts.u.derived->attr.allocatable)
12779 : 3185 : || (UNLIMITED_POLY (lhs)
12780 : 95 : && !CLASS_DATA (lhs)->attr.pointer
12781 : 95 : && !CLASS_DATA (lhs)->attr.allocatable))
12782 : 2677 : vec_safe_push (args, gfc_build_addr_expr (NULL_TREE, tmp));
12783 : : else
12784 : 254 : vec_safe_push (args, tmp);
12785 : :
12786 : 2931 : stdcopy = build_call_vec (TREE_TYPE (TREE_TYPE (fcn)), fcn, args);
12787 : :
12788 : 2931 : if (to_len != NULL_TREE && !integer_zerop (from_len))
12789 : : {
12790 : 400 : tree extcopy;
12791 : 400 : vec_safe_push (args, from_len);
12792 : 400 : vec_safe_push (args, to_len);
12793 : 400 : extcopy = build_call_vec (TREE_TYPE (TREE_TYPE (fcn)), fcn, args);
12794 : :
12795 : 400 : tmp = fold_build2_loc (input_location, GT_EXPR,
12796 : : logical_type_node, from_len,
12797 : 400 : build_zero_cst (TREE_TYPE (from_len)));
12798 : 400 : return fold_build3_loc (input_location, COND_EXPR,
12799 : : void_type_node, tmp,
12800 : 400 : extcopy, stdcopy);
12801 : : }
12802 : : else
12803 : 2531 : return stdcopy;
12804 : : }
12805 : : else
12806 : : {
12807 : 252 : tree rhst = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
12808 : 252 : ? gfc_class_data_get (lse->expr) : lse->expr;
12809 : 252 : stmtblock_t tblock;
12810 : 252 : gfc_init_block (&tblock);
12811 : 252 : if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
12812 : 0 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
12813 : 252 : if (!POINTER_TYPE_P (TREE_TYPE (rhst)))
12814 : 0 : rhst = gfc_build_addr_expr (NULL_TREE, rhst);
12815 : : /* When coming from a ptr_copy lhs and rhs are swapped. */
12816 : 252 : gfc_add_modify_loc (input_location, &tblock, rhst,
12817 : 252 : fold_convert (TREE_TYPE (rhst), tmp));
12818 : 252 : return gfc_finish_block (&tblock);
12819 : : }
12820 : : }
12821 : :
12822 : : bool
12823 : 295679 : is_assoc_assign (gfc_expr *lhs, gfc_expr *rhs)
12824 : : {
12825 : 295679 : if (lhs->expr_type != EXPR_VARIABLE || rhs->expr_type != EXPR_VARIABLE)
12826 : : return false;
12827 : :
12828 : 30099 : return lhs->symtree->n.sym->assoc
12829 : 30099 : && lhs->symtree->n.sym->assoc->target == rhs;
12830 : : }
12831 : :
12832 : : /* Subroutine of gfc_trans_assignment that actually scalarizes the
12833 : : assignment. EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
12834 : : init_flag indicates initialization expressions and dealloc that no
12835 : : deallocate prior assignment is needed (if in doubt, set true).
12836 : : When PTR_COPY is set and expr1 is a class type, then use the _vptr-copy
12837 : : routine instead of a pointer assignment. Alias resolution is only done,
12838 : : when MAY_ALIAS is set (the default). This flag is used by ALLOCATE()
12839 : : where it is known, that newly allocated memory on the lhs can never be
12840 : : an alias of the rhs. */
12841 : :
12842 : : static tree
12843 : 295679 : gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
12844 : : bool dealloc, bool use_vptr_copy, bool may_alias)
12845 : : {
12846 : 295679 : gfc_se lse;
12847 : 295679 : gfc_se rse;
12848 : 295679 : gfc_ss *lss;
12849 : 295679 : gfc_ss *lss_section;
12850 : 295679 : gfc_ss *rss;
12851 : 295679 : gfc_loopinfo loop;
12852 : 295679 : tree tmp;
12853 : 295679 : stmtblock_t block;
12854 : 295679 : stmtblock_t body;
12855 : 295679 : bool final_expr;
12856 : 295679 : bool l_is_temp;
12857 : 295679 : bool scalar_to_array;
12858 : 295679 : tree string_length;
12859 : 295679 : int n;
12860 : 295679 : bool maybe_workshare = false, lhs_refs_comp = false, rhs_refs_comp = false;
12861 : 295679 : symbol_attribute lhs_caf_attr, rhs_caf_attr, lhs_attr;
12862 : 295679 : bool is_poly_assign;
12863 : 295679 : bool realloc_flag;
12864 : 295679 : bool assoc_assign = false;
12865 : :
12866 : : /* Assignment of the form lhs = rhs. */
12867 : 295679 : gfc_start_block (&block);
12868 : :
12869 : 295679 : gfc_init_se (&lse, NULL);
12870 : 295679 : gfc_init_se (&rse, NULL);
12871 : :
12872 : : /* Walk the lhs. */
12873 : 295679 : lss = gfc_walk_expr (expr1);
12874 : 295679 : if (gfc_is_reallocatable_lhs (expr1))
12875 : : {
12876 : 10384 : lss->no_bounds_check = 1;
12877 : 10384 : if (!(expr2->expr_type == EXPR_FUNCTION
12878 : 2587 : && expr2->value.function.isym != NULL
12879 : 2300 : && !(expr2->value.function.isym->elemental
12880 : 2015 : || expr2->value.function.isym->conversion)))
12881 : 8369 : lss->is_alloc_lhs = 1;
12882 : : }
12883 : : else
12884 : 285295 : lss->no_bounds_check = expr1->no_bounds_check;
12885 : :
12886 : 295679 : rss = NULL;
12887 : :
12888 : 295679 : if (expr2->expr_type != EXPR_VARIABLE
12889 : 295679 : && expr2->expr_type != EXPR_CONSTANT
12890 : 295679 : && (expr2->ts.type == BT_CLASS || gfc_may_be_finalized (expr2->ts)))
12891 : : {
12892 : 719 : expr2->must_finalize = 1;
12893 : : /* F2023 7.5.6.3: If an executable construct references a nonpointer
12894 : : function, the result is finalized after execution of the innermost
12895 : : executable construct containing the reference. */
12896 : 719 : if (expr2->expr_type == EXPR_FUNCTION
12897 : 719 : && (gfc_expr_attr (expr2).pointer
12898 : 291 : || (expr2->ts.type == BT_CLASS && CLASS_DATA (expr2)->attr.class_pointer)))
12899 : 145 : expr2->must_finalize = 0;
12900 : : /* F2008 4.5.6.3 para 5: If an executable construct references a
12901 : : structure constructor or array constructor, the entity created by
12902 : : the constructor is finalized after execution of the innermost
12903 : : executable construct containing the reference.
12904 : : These finalizations were later deleted by the Combined Techical
12905 : : Corrigenda 1 TO 4 for fortran 2008 (f08/0011). */
12906 : 574 : else if (gfc_notification_std (GFC_STD_F2018_DEL)
12907 : 574 : && (expr2->expr_type == EXPR_STRUCTURE
12908 : 531 : || expr2->expr_type == EXPR_ARRAY))
12909 : 251 : expr2->must_finalize = 0;
12910 : : }
12911 : :
12912 : :
12913 : : /* Checking whether a class assignment is desired is quite complicated and
12914 : : needed at two locations, so do it once only before the information is
12915 : : needed. */
12916 : 295679 : lhs_attr = gfc_expr_attr (expr1);
12917 : :
12918 : 295679 : is_poly_assign
12919 : 295679 : = (use_vptr_copy
12920 : 280210 : || ((lhs_attr.pointer || lhs_attr.allocatable) && !lhs_attr.dimension))
12921 : 20876 : && (expr1->ts.type == BT_CLASS || gfc_is_class_array_ref (expr1, NULL)
12922 : 18934 : || gfc_is_class_scalar_expr (expr1)
12923 : 17644 : || gfc_is_class_array_ref (expr2, NULL)
12924 : 17644 : || gfc_is_class_scalar_expr (expr2))
12925 : 298929 : && lhs_attr.flavor != FL_PROCEDURE;
12926 : :
12927 : 295679 : assoc_assign = is_assoc_assign (expr1, expr2);
12928 : :
12929 : 591358 : realloc_flag = flag_realloc_lhs
12930 : 289941 : && gfc_is_reallocatable_lhs (expr1)
12931 : 7219 : && expr2->rank
12932 : 301482 : && !is_runtime_conformable (expr1, expr2);
12933 : :
12934 : : /* Only analyze the expressions for coarray properties, when in coarray-lib
12935 : : mode. Avoid false-positive uninitialized diagnostics with initializing
12936 : : the codimension flag unconditionally. */
12937 : 295679 : lhs_caf_attr.codimension = false;
12938 : 295679 : rhs_caf_attr.codimension = false;
12939 : 295679 : if (flag_coarray == GFC_FCOARRAY_LIB)
12940 : : {
12941 : 4505 : lhs_caf_attr = gfc_caf_attr (expr1, false, &lhs_refs_comp);
12942 : 4505 : rhs_caf_attr = gfc_caf_attr (expr2, false, &rhs_refs_comp);
12943 : : }
12944 : :
12945 : 295679 : if (lss != gfc_ss_terminator)
12946 : : {
12947 : : /* The assignment needs scalarization. */
12948 : : lss_section = lss;
12949 : :
12950 : : /* Find a non-scalar SS from the lhs. */
12951 : : while (lss_section != gfc_ss_terminator
12952 : 37644 : && lss_section->info->type != GFC_SS_SECTION)
12953 : 0 : lss_section = lss_section->next;
12954 : :
12955 : 37644 : gcc_assert (lss_section != gfc_ss_terminator);
12956 : :
12957 : : /* Initialize the scalarizer. */
12958 : 37644 : gfc_init_loopinfo (&loop);
12959 : :
12960 : : /* Walk the rhs. */
12961 : 37644 : rss = gfc_walk_expr (expr2);
12962 : 37644 : if (rss == gfc_ss_terminator)
12963 : : /* The rhs is scalar. Add a ss for the expression. */
12964 : 14067 : rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
12965 : : /* When doing a class assign, then the handle to the rhs needs to be a
12966 : : pointer to allow for polymorphism. */
12967 : 37644 : if (is_poly_assign && expr2->rank == 0 && !UNLIMITED_POLY (expr2))
12968 : 484 : rss->info->type = GFC_SS_REFERENCE;
12969 : :
12970 : 37644 : rss->no_bounds_check = expr2->no_bounds_check;
12971 : : /* Associate the SS with the loop. */
12972 : 37644 : gfc_add_ss_to_loop (&loop, lss);
12973 : 37644 : gfc_add_ss_to_loop (&loop, rss);
12974 : :
12975 : : /* Calculate the bounds of the scalarization. */
12976 : 37644 : gfc_conv_ss_startstride (&loop);
12977 : : /* Enable loop reversal. */
12978 : 639948 : for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
12979 : 564660 : loop.reverse[n] = GFC_ENABLE_REVERSE;
12980 : : /* Resolve any data dependencies in the statement. */
12981 : 37644 : if (may_alias)
12982 : 35470 : gfc_conv_resolve_dependencies (&loop, lss, rss);
12983 : : /* Setup the scalarizing loops. */
12984 : 37644 : gfc_conv_loop_setup (&loop, &expr2->where);
12985 : :
12986 : : /* Setup the gfc_se structures. */
12987 : 37644 : gfc_copy_loopinfo_to_se (&lse, &loop);
12988 : 37644 : gfc_copy_loopinfo_to_se (&rse, &loop);
12989 : :
12990 : 37644 : rse.ss = rss;
12991 : 37644 : gfc_mark_ss_chain_used (rss, 1);
12992 : 37644 : if (loop.temp_ss == NULL)
12993 : : {
12994 : 36660 : lse.ss = lss;
12995 : 36660 : gfc_mark_ss_chain_used (lss, 1);
12996 : : }
12997 : : else
12998 : : {
12999 : 984 : lse.ss = loop.temp_ss;
13000 : 984 : gfc_mark_ss_chain_used (lss, 3);
13001 : 984 : gfc_mark_ss_chain_used (loop.temp_ss, 3);
13002 : : }
13003 : :
13004 : : /* Allow the scalarizer to workshare array assignments. */
13005 : 37644 : if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_BODY))
13006 : : == OMPWS_WORKSHARE_FLAG
13007 : 85 : && loop.temp_ss == NULL)
13008 : : {
13009 : 73 : maybe_workshare = true;
13010 : 73 : ompws_flags |= OMPWS_SCALARIZER_WS | OMPWS_SCALARIZER_BODY;
13011 : : }
13012 : :
13013 : : /* Start the scalarized loop body. */
13014 : 37644 : gfc_start_scalarized_body (&loop, &body);
13015 : : }
13016 : : else
13017 : 258035 : gfc_init_block (&body);
13018 : :
13019 : 295679 : l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
13020 : :
13021 : : /* Translate the expression. */
13022 : 591358 : rse.want_coarray = flag_coarray == GFC_FCOARRAY_LIB
13023 : 295679 : && (init_flag || assoc_assign) && lhs_caf_attr.codimension;
13024 : 295679 : rse.want_pointer = rse.want_coarray && !init_flag && !lhs_caf_attr.dimension;
13025 : 295679 : gfc_conv_expr (&rse, expr2);
13026 : :
13027 : : /* Deal with the case of a scalar class function assigned to a derived type.
13028 : : */
13029 : 295679 : if (gfc_is_alloc_class_scalar_function (expr2)
13030 : 295679 : && expr1->ts.type == BT_DERIVED)
13031 : : {
13032 : 60 : rse.expr = gfc_class_data_get (rse.expr);
13033 : 60 : rse.expr = build_fold_indirect_ref_loc (input_location, rse.expr);
13034 : : }
13035 : :
13036 : : /* Stabilize a string length for temporaries. */
13037 : 295679 : if (expr2->ts.type == BT_CHARACTER && !expr1->ts.deferred
13038 : 22949 : && !(VAR_P (rse.string_length)
13039 : : || TREE_CODE (rse.string_length) == PARM_DECL
13040 : : || INDIRECT_REF_P (rse.string_length)))
13041 : 22180 : string_length = gfc_evaluate_now (rse.string_length, &rse.pre);
13042 : 273499 : else if (expr2->ts.type == BT_CHARACTER)
13043 : : {
13044 : 3940 : if (expr1->ts.deferred
13045 : 6126 : && gfc_expr_attr (expr1).allocatable
13046 : 6240 : && gfc_check_dependency (expr1, expr2, true))
13047 : 114 : rse.string_length =
13048 : 114 : gfc_evaluate_now_function_scope (rse.string_length, &rse.pre);
13049 : 3940 : string_length = rse.string_length;
13050 : : }
13051 : : else
13052 : : string_length = NULL_TREE;
13053 : :
13054 : 295679 : if (l_is_temp)
13055 : : {
13056 : 984 : gfc_conv_tmp_array_ref (&lse);
13057 : 984 : if (expr2->ts.type == BT_CHARACTER)
13058 : 123 : lse.string_length = string_length;
13059 : : }
13060 : : else
13061 : : {
13062 : 294695 : gfc_conv_expr (&lse, expr1);
13063 : : /* For some expression (e.g. complex numbers) fold_convert uses a
13064 : : SAVE_EXPR, which is hazardous on the lhs, because the value is
13065 : : not updated when assigned to. */
13066 : 294695 : if (TREE_CODE (lse.expr) == SAVE_EXPR)
13067 : 5 : lse.expr = TREE_OPERAND (lse.expr, 0);
13068 : :
13069 : 6138 : if (gfc_option.rtcheck & GFC_RTCHECK_MEM && !init_flag
13070 : 300833 : && gfc_expr_attr (expr1).allocatable && expr1->rank && !expr2->rank)
13071 : : {
13072 : 36 : tree cond;
13073 : 36 : const char* msg;
13074 : :
13075 : 36 : tmp = INDIRECT_REF_P (lse.expr)
13076 : 36 : ? gfc_build_addr_expr (NULL_TREE, lse.expr) : lse.expr;
13077 : 36 : STRIP_NOPS (tmp);
13078 : :
13079 : : /* We should only get array references here. */
13080 : 36 : gcc_assert (TREE_CODE (tmp) == POINTER_PLUS_EXPR
13081 : : || TREE_CODE (tmp) == ARRAY_REF);
13082 : :
13083 : : /* 'tmp' is either the pointer to the array(POINTER_PLUS_EXPR)
13084 : : or the array itself(ARRAY_REF). */
13085 : 36 : tmp = TREE_OPERAND (tmp, 0);
13086 : :
13087 : : /* Provide the address of the array. */
13088 : 36 : if (TREE_CODE (lse.expr) == ARRAY_REF)
13089 : 18 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
13090 : :
13091 : 36 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
13092 : 36 : tmp, build_int_cst (TREE_TYPE (tmp), 0));
13093 : 36 : msg = _("Assignment of scalar to unallocated array");
13094 : 36 : gfc_trans_runtime_check (true, false, cond, &loop.pre,
13095 : : &expr1->where, msg);
13096 : : }
13097 : :
13098 : : /* Deallocate the lhs parameterized components if required. */
13099 : 294695 : if (dealloc && expr2->expr_type == EXPR_FUNCTION
13100 : 34971 : && !expr1->symtree->n.sym->attr.associate_var)
13101 : : {
13102 : 34882 : if (expr1->ts.type == BT_DERIVED
13103 : 1949 : && expr1->ts.u.derived
13104 : 1949 : && expr1->ts.u.derived->attr.pdt_type)
13105 : : {
13106 : 12 : tmp = gfc_deallocate_pdt_comp (expr1->ts.u.derived, lse.expr,
13107 : : expr1->rank);
13108 : 12 : gfc_add_expr_to_block (&lse.pre, tmp);
13109 : : }
13110 : 34870 : else if (expr1->ts.type == BT_CLASS
13111 : 349 : && CLASS_DATA (expr1)->ts.u.derived
13112 : 349 : && CLASS_DATA (expr1)->ts.u.derived->attr.pdt_type)
13113 : : {
13114 : 0 : tmp = gfc_class_data_get (lse.expr);
13115 : 0 : tmp = gfc_deallocate_pdt_comp (CLASS_DATA (expr1)->ts.u.derived,
13116 : : tmp, expr1->rank);
13117 : 0 : gfc_add_expr_to_block (&lse.pre, tmp);
13118 : : }
13119 : : }
13120 : : }
13121 : :
13122 : : /* Assignments of scalar derived types with allocatable components
13123 : : to arrays must be done with a deep copy and the rhs temporary
13124 : : must have its components deallocated afterwards. */
13125 : 591358 : scalar_to_array = (expr2->ts.type == BT_DERIVED
13126 : 17469 : && expr2->ts.u.derived->attr.alloc_comp
13127 : 5630 : && !gfc_expr_is_variable (expr2)
13128 : 298802 : && expr1->rank && !expr2->rank);
13129 : 591358 : scalar_to_array |= (expr1->ts.type == BT_DERIVED
13130 : 17754 : && expr1->rank
13131 : 3352 : && expr1->ts.u.derived->attr.alloc_comp
13132 : 296826 : && gfc_is_alloc_class_scalar_function (expr2));
13133 : 295679 : if (scalar_to_array && dealloc)
13134 : : {
13135 : 52 : tmp = gfc_deallocate_alloc_comp_no_caf (expr2->ts.u.derived, rse.expr, 0);
13136 : 52 : gfc_prepend_expr_to_block (&loop.post, tmp);
13137 : : }
13138 : :
13139 : : /* When assigning a character function result to a deferred-length variable,
13140 : : the function call must happen before the (re)allocation of the lhs -
13141 : : otherwise the character length of the result is not known.
13142 : : NOTE 1: This relies on having the exact dependence of the length type
13143 : : parameter available to the caller; gfortran saves it in the .mod files.
13144 : : NOTE 2: Vector array references generate an index temporary that must
13145 : : not go outside the loop. Otherwise, variables should not generate
13146 : : a pre block.
13147 : : NOTE 3: The concatenation operation generates a temporary pointer,
13148 : : whose allocation must go to the innermost loop.
13149 : : NOTE 4: Elemental functions may generate a temporary, too. */
13150 : 295679 : if (flag_realloc_lhs
13151 : 289941 : && expr2->ts.type == BT_CHARACTER && expr1->ts.deferred
13152 : 2671 : && !(lss != gfc_ss_terminator
13153 : 794 : && rss != gfc_ss_terminator
13154 : 794 : && ((expr2->expr_type == EXPR_VARIABLE && expr2->rank)
13155 : 619 : || (expr2->expr_type == EXPR_FUNCTION
13156 : 142 : && expr2->value.function.esym != NULL
13157 : 26 : && expr2->value.function.esym->attr.elemental)
13158 : 606 : || (expr2->expr_type == EXPR_FUNCTION
13159 : 129 : && expr2->value.function.isym != NULL
13160 : 116 : && expr2->value.function.isym->elemental)
13161 : 568 : || (expr2->expr_type == EXPR_OP
13162 : 31 : && expr2->value.op.op == INTRINSIC_CONCAT))))
13163 : 2420 : gfc_add_block_to_block (&block, &rse.pre);
13164 : :
13165 : : /* Nullify the allocatable components corresponding to those of the lhs
13166 : : derived type, so that the finalization of the function result does not
13167 : : affect the lhs of the assignment. Prepend is used to ensure that the
13168 : : nullification occurs before the call to the finalizer. In the case of
13169 : : a scalar to array assignment, this is done in gfc_trans_scalar_assign
13170 : : as part of the deep copy. */
13171 : 295016 : if (!scalar_to_array && expr1->ts.type == BT_DERIVED
13172 : 312770 : && (gfc_is_class_array_function (expr2)
13173 : 17067 : || gfc_is_alloc_class_scalar_function (expr2)))
13174 : : {
13175 : 78 : tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, rse.expr, 0);
13176 : 78 : gfc_prepend_expr_to_block (&rse.post, tmp);
13177 : 78 : if (lss != gfc_ss_terminator && rss == gfc_ss_terminator)
13178 : 0 : gfc_add_block_to_block (&loop.post, &rse.post);
13179 : : }
13180 : :
13181 : 295679 : tmp = NULL_TREE;
13182 : :
13183 : 295679 : if (is_poly_assign)
13184 : : {
13185 : 3183 : tmp = trans_class_assignment (&body, expr1, expr2, &lse, &rse,
13186 : 3183 : use_vptr_copy || (lhs_attr.allocatable
13187 : 272 : && !lhs_attr.dimension),
13188 : 2981 : !realloc_flag && flag_realloc_lhs
13189 : 3707 : && !lhs_attr.pointer);
13190 : 3183 : if (expr2->expr_type == EXPR_FUNCTION
13191 : 230 : && expr2->ts.type == BT_DERIVED
13192 : 30 : && expr2->ts.u.derived->attr.alloc_comp)
13193 : : {
13194 : 18 : tree tmp2 = gfc_deallocate_alloc_comp (expr2->ts.u.derived,
13195 : : rse.expr, expr2->rank);
13196 : 18 : if (lss == gfc_ss_terminator)
13197 : 18 : gfc_add_expr_to_block (&rse.post, tmp2);
13198 : : else
13199 : 0 : gfc_add_expr_to_block (&loop.post, tmp2);
13200 : : }
13201 : :
13202 : 3183 : expr1->must_finalize = 0;
13203 : : }
13204 : 292496 : else if (!is_poly_assign && expr2->must_finalize
13205 : 348 : && expr1->ts.type == BT_CLASS
13206 : 126 : && expr2->ts.type == BT_CLASS)
13207 : : {
13208 : : /* This case comes about when the scalarizer provides array element
13209 : : references. Use the vptr copy function, since this does a deep
13210 : : copy of allocatable components, without which the finalizer call
13211 : : will deallocate the components. */
13212 : 120 : tmp = gfc_get_vptr_from_expr (rse.expr);
13213 : 120 : if (tmp != NULL_TREE)
13214 : : {
13215 : 120 : tree fcn = gfc_vptr_copy_get (tmp);
13216 : 120 : if (POINTER_TYPE_P (TREE_TYPE (fcn)))
13217 : 120 : fcn = build_fold_indirect_ref_loc (input_location, fcn);
13218 : 120 : tmp = build_call_expr_loc (input_location,
13219 : : fcn, 2,
13220 : : gfc_build_addr_expr (NULL, rse.expr),
13221 : : gfc_build_addr_expr (NULL, lse.expr));
13222 : : }
13223 : : }
13224 : :
13225 : : /* Comply with F2018 (7.5.6.3). Make sure that any finalization code is added
13226 : : after evaluation of the rhs and before reallocation. */
13227 : 295679 : final_expr = gfc_assignment_finalizer_call (&lse, expr1, init_flag);
13228 : 295679 : if (final_expr && !(expr2->expr_type == EXPR_VARIABLE
13229 : 163 : && expr2->symtree->n.sym->attr.artificial))
13230 : : {
13231 : 545 : if (lss == gfc_ss_terminator)
13232 : : {
13233 : 158 : gfc_add_block_to_block (&block, &rse.pre);
13234 : 158 : gfc_add_block_to_block (&block, &lse.finalblock);
13235 : : }
13236 : : else
13237 : : {
13238 : 387 : gfc_add_block_to_block (&body, &rse.pre);
13239 : 387 : gfc_add_block_to_block (&loop.code[expr1->rank - 1],
13240 : : &lse.finalblock);
13241 : : }
13242 : : }
13243 : : else
13244 : 295134 : gfc_add_block_to_block (&body, &rse.pre);
13245 : :
13246 : 295679 : if (flag_coarray != GFC_FCOARRAY_NONE && expr1->ts.type == BT_CHARACTER
13247 : 2001 : && assoc_assign)
13248 : 0 : tmp = gfc_trans_pointer_assignment (expr1, expr2);
13249 : :
13250 : : /* If nothing else works, do it the old fashioned way! */
13251 : 295679 : if (tmp == NULL_TREE)
13252 : 292376 : tmp
13253 : 292376 : = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
13254 : 556406 : gfc_expr_is_variable (expr2) || scalar_to_array
13255 : 555827 : || expr2->expr_type == EXPR_ARRAY,
13256 : 292376 : !(l_is_temp || init_flag) && dealloc,
13257 : 292376 : expr1->symtree->n.sym->attr.codimension,
13258 : : assoc_assign);
13259 : :
13260 : : /* Add the lse pre block to the body */
13261 : 295679 : gfc_add_block_to_block (&body, &lse.pre);
13262 : 295679 : gfc_add_expr_to_block (&body, tmp);
13263 : :
13264 : : /* Add the post blocks to the body. Scalar finalization must appear before
13265 : : the post block in case any dellocations are done. */
13266 : 295679 : if (rse.finalblock.head
13267 : 295679 : && (!l_is_temp || (expr2->expr_type == EXPR_FUNCTION
13268 : 14 : && gfc_expr_attr (expr2).elemental)))
13269 : : {
13270 : 135 : gfc_add_block_to_block (&body, &rse.finalblock);
13271 : 135 : gfc_add_block_to_block (&body, &rse.post);
13272 : : }
13273 : : else
13274 : 295544 : gfc_add_block_to_block (&body, &rse.post);
13275 : :
13276 : 295679 : gfc_add_block_to_block (&body, &lse.post);
13277 : :
13278 : 295679 : if (lss == gfc_ss_terminator)
13279 : : {
13280 : : /* F2003: Add the code for reallocation on assignment. */
13281 : 255383 : if (flag_realloc_lhs && is_scalar_reallocatable_lhs (expr1)
13282 : 261412 : && !is_poly_assign)
13283 : 3359 : alloc_scalar_allocatable_for_assignment (&block, string_length,
13284 : : expr1, expr2);
13285 : :
13286 : : /* Use the scalar assignment as is. */
13287 : 258035 : gfc_add_block_to_block (&block, &body);
13288 : : }
13289 : : else
13290 : : {
13291 : 37644 : gcc_assert (lse.ss == gfc_ss_terminator
13292 : : && rse.ss == gfc_ss_terminator);
13293 : :
13294 : 37644 : if (l_is_temp)
13295 : : {
13296 : 984 : gfc_trans_scalarized_loop_boundary (&loop, &body);
13297 : :
13298 : : /* We need to copy the temporary to the actual lhs. */
13299 : 984 : gfc_init_se (&lse, NULL);
13300 : 984 : gfc_init_se (&rse, NULL);
13301 : 984 : gfc_copy_loopinfo_to_se (&lse, &loop);
13302 : 984 : gfc_copy_loopinfo_to_se (&rse, &loop);
13303 : :
13304 : 984 : rse.ss = loop.temp_ss;
13305 : 984 : lse.ss = lss;
13306 : :
13307 : 984 : gfc_conv_tmp_array_ref (&rse);
13308 : 984 : gfc_conv_expr (&lse, expr1);
13309 : :
13310 : 984 : gcc_assert (lse.ss == gfc_ss_terminator
13311 : : && rse.ss == gfc_ss_terminator);
13312 : :
13313 : 984 : if (expr2->ts.type == BT_CHARACTER)
13314 : 123 : rse.string_length = string_length;
13315 : :
13316 : 984 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
13317 : : false, dealloc);
13318 : 984 : gfc_add_expr_to_block (&body, tmp);
13319 : : }
13320 : :
13321 : : /* F2003: Allocate or reallocate lhs of allocatable array. */
13322 : 37644 : if (realloc_flag)
13323 : : {
13324 : 5606 : realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
13325 : 5606 : ompws_flags &= ~OMPWS_SCALARIZER_WS;
13326 : 5606 : tmp = gfc_alloc_allocatable_for_assignment (&loop, expr1, expr2);
13327 : 5606 : if (tmp != NULL_TREE)
13328 : 5606 : gfc_add_expr_to_block (&loop.code[expr1->rank - 1], tmp);
13329 : : }
13330 : :
13331 : 37644 : if (maybe_workshare)
13332 : 73 : ompws_flags &= ~OMPWS_SCALARIZER_BODY;
13333 : :
13334 : : /* Generate the copying loops. */
13335 : 37644 : gfc_trans_scalarizing_loops (&loop, &body);
13336 : :
13337 : : /* Wrap the whole thing up. */
13338 : 37644 : gfc_add_block_to_block (&block, &loop.pre);
13339 : 37644 : gfc_add_block_to_block (&block, &loop.post);
13340 : :
13341 : 37644 : gfc_cleanup_loop (&loop);
13342 : : }
13343 : :
13344 : 295679 : return gfc_finish_block (&block);
13345 : : }
13346 : :
13347 : :
13348 : : /* Check whether EXPR is a copyable array. */
13349 : :
13350 : : static bool
13351 : 935548 : copyable_array_p (gfc_expr * expr)
13352 : : {
13353 : 935548 : if (expr->expr_type != EXPR_VARIABLE)
13354 : : return false;
13355 : :
13356 : : /* First check it's an array. */
13357 : 912919 : if (expr->rank < 1 || !expr->ref || expr->ref->next)
13358 : : return false;
13359 : :
13360 : 136630 : if (!gfc_full_array_ref_p (expr->ref, NULL))
13361 : : return false;
13362 : :
13363 : : /* Next check that it's of a simple enough type. */
13364 : 109645 : switch (expr->ts.type)
13365 : : {
13366 : : case BT_INTEGER:
13367 : : case BT_REAL:
13368 : : case BT_COMPLEX:
13369 : : case BT_LOGICAL:
13370 : : return true;
13371 : :
13372 : : case BT_CHARACTER:
13373 : : return false;
13374 : :
13375 : 5818 : case_bt_struct:
13376 : 5818 : return !expr->ts.u.derived->attr.alloc_comp;
13377 : :
13378 : : default:
13379 : : break;
13380 : : }
13381 : :
13382 : : return false;
13383 : : }
13384 : :
13385 : : /* Translate an assignment. */
13386 : :
13387 : : tree
13388 : 312893 : gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
13389 : : bool dealloc, bool use_vptr_copy, bool may_alias)
13390 : : {
13391 : 312893 : tree tmp;
13392 : :
13393 : : /* Special case a single function returning an array. */
13394 : 312893 : if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
13395 : : {
13396 : 14577 : tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
13397 : 14577 : if (tmp)
13398 : : return tmp;
13399 : : }
13400 : :
13401 : : /* Special case assigning an array to zero. */
13402 : 305813 : if (copyable_array_p (expr1)
13403 : 305813 : && is_zero_initializer_p (expr2))
13404 : : {
13405 : 3924 : tmp = gfc_trans_zero_assign (expr1);
13406 : 3924 : if (tmp)
13407 : : return tmp;
13408 : : }
13409 : :
13410 : : /* Special case copying one array to another. */
13411 : 302167 : if (copyable_array_p (expr1)
13412 : 26839 : && copyable_array_p (expr2)
13413 : 2599 : && gfc_compare_types (&expr1->ts, &expr2->ts)
13414 : 304766 : && !gfc_check_dependency (expr1, expr2, 0))
13415 : : {
13416 : 2503 : tmp = gfc_trans_array_copy (expr1, expr2);
13417 : 2503 : if (tmp)
13418 : : return tmp;
13419 : : }
13420 : :
13421 : : /* Special case initializing an array from a constant array constructor. */
13422 : 300729 : if (copyable_array_p (expr1)
13423 : 25401 : && expr2->expr_type == EXPR_ARRAY
13424 : 308177 : && gfc_compare_types (&expr1->ts, &expr2->ts))
13425 : : {
13426 : 7448 : tmp = gfc_trans_array_constructor_copy (expr1, expr2);
13427 : 7448 : if (tmp)
13428 : : return tmp;
13429 : : }
13430 : :
13431 : 295679 : if (UNLIMITED_POLY (expr1) && expr1->rank)
13432 : 295679 : use_vptr_copy = true;
13433 : :
13434 : : /* Fallback to the scalarizer to generate explicit loops. */
13435 : 295679 : return gfc_trans_assignment_1 (expr1, expr2, init_flag, dealloc,
13436 : 295679 : use_vptr_copy, may_alias);
13437 : : }
13438 : :
13439 : : tree
13440 : 11838 : gfc_trans_init_assign (gfc_code * code)
13441 : : {
13442 : 11838 : return gfc_trans_assignment (code->expr1, code->expr2, true, false, true);
13443 : : }
13444 : :
13445 : : tree
13446 : 293126 : gfc_trans_assign (gfc_code * code)
13447 : : {
13448 : 293126 : return gfc_trans_assignment (code->expr1, code->expr2, false, true);
13449 : : }
13450 : :
13451 : : /* Generate a simple loop for internal use of the form
13452 : : for (var = begin; var <cond> end; var += step)
13453 : : body; */
13454 : : void
13455 : 12144 : gfc_simple_for_loop (stmtblock_t *block, tree var, tree begin, tree end,
13456 : : enum tree_code cond, tree step, tree body)
13457 : : {
13458 : 12144 : tree tmp;
13459 : :
13460 : : /* var = begin. */
13461 : 12144 : gfc_add_modify (block, var, begin);
13462 : :
13463 : : /* Loop: for (var = begin; var <cond> end; var += step). */
13464 : 12144 : tree label_loop = gfc_build_label_decl (NULL_TREE);
13465 : 12144 : tree label_cond = gfc_build_label_decl (NULL_TREE);
13466 : 12144 : TREE_USED (label_loop) = 1;
13467 : 12144 : TREE_USED (label_cond) = 1;
13468 : :
13469 : 12144 : gfc_add_expr_to_block (block, build1_v (GOTO_EXPR, label_cond));
13470 : 12144 : gfc_add_expr_to_block (block, build1_v (LABEL_EXPR, label_loop));
13471 : :
13472 : : /* Loop body. */
13473 : 12144 : gfc_add_expr_to_block (block, body);
13474 : :
13475 : : /* End of loop body. */
13476 : 12144 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (var), var, step);
13477 : 12144 : gfc_add_modify (block, var, tmp);
13478 : 12144 : gfc_add_expr_to_block (block, build1_v (LABEL_EXPR, label_cond));
13479 : 12144 : tmp = fold_build2_loc (input_location, cond, boolean_type_node, var, end);
13480 : 12144 : tmp = build3_v (COND_EXPR, tmp, build1_v (GOTO_EXPR, label_loop),
13481 : : build_empty_stmt (input_location));
13482 : 12144 : gfc_add_expr_to_block (block, tmp);
13483 : 12144 : }
|