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 : 5890 : get_scalar_to_descriptor_type (tree scalar, symbol_attribute attr)
91 : : {
92 : 5890 : enum gfc_array_kind akind;
93 : :
94 : 5890 : if (attr.pointer)
95 : : akind = GFC_ARRAY_POINTER_CONT;
96 : 5540 : else if (attr.allocatable)
97 : : akind = GFC_ARRAY_ALLOCATABLE;
98 : : else
99 : 4777 : akind = GFC_ARRAY_ASSUMED_SHAPE_CONT;
100 : :
101 : 5890 : if (POINTER_TYPE_P (TREE_TYPE (scalar)))
102 : 4991 : scalar = TREE_TYPE (scalar);
103 : 5890 : return gfc_get_array_type_bounds (TREE_TYPE (scalar), 0, 0, NULL, NULL, 1,
104 : 5890 : akind, !(attr.pointer || attr.target));
105 : : }
106 : :
107 : : tree
108 : 5213 : gfc_conv_scalar_to_descriptor (gfc_se *se, tree scalar, symbol_attribute attr)
109 : : {
110 : 5213 : tree desc, type, etype;
111 : :
112 : 5213 : type = get_scalar_to_descriptor_type (scalar, attr);
113 : 5213 : etype = TREE_TYPE (scalar);
114 : 5213 : desc = gfc_create_var (type, "desc");
115 : 5213 : DECL_ARTIFICIAL (desc) = 1;
116 : :
117 : 5213 : 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 : 5213 : if (!POINTER_TYPE_P (TREE_TYPE (scalar)))
125 : 899 : scalar = gfc_build_addr_expr (NULL_TREE, scalar);
126 : 4314 : else if (TREE_TYPE (etype) && TREE_CODE (TREE_TYPE (etype)) == ARRAY_TYPE)
127 : 113 : etype = TREE_TYPE (etype);
128 : 5213 : gfc_add_modify (&se->pre, gfc_conv_descriptor_dtype (desc),
129 : : gfc_get_dtype_rank_type (0, etype));
130 : 5213 : gfc_conv_descriptor_data_set (&se->pre, desc, scalar);
131 : 5213 : 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 : 5213 : 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 : 5213 : 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 : 411 : gfc_get_ultimate_alloc_ptr_comps_caf_token (gfc_se *outerse, gfc_expr *expr)
149 : : {
150 : 411 : gfc_symbol *sym = expr->symtree->n.sym;
151 : 822 : bool is_coarray = sym->ts.type == BT_CLASS
152 : 411 : ? CLASS_DATA (sym)->attr.codimension
153 : 376 : : sym->attr.codimension;
154 : 411 : gfc_expr *caf_expr = gfc_copy_expr (expr);
155 : 411 : gfc_ref *ref = caf_expr->ref, *last_caf_ref = NULL;
156 : :
157 : 1371 : while (ref)
158 : : {
159 : 960 : if (ref->type == REF_COMPONENT
160 : 376 : && (ref->u.c.component->attr.allocatable
161 : 98 : || ref->u.c.component->attr.pointer)
162 : 375 : && (is_coarray || ref->u.c.component->attr.codimension))
163 : 960 : last_caf_ref = ref;
164 : 960 : ref = ref->next;
165 : : }
166 : :
167 : 411 : if (last_caf_ref == NULL)
168 : : {
169 : 116 : gfc_free_expr (caf_expr);
170 : 116 : return NULL_TREE;
171 : : }
172 : :
173 : 133 : tree comp = last_caf_ref->u.c.component->caf_token
174 : 295 : ? gfc_comp_caf_token (last_caf_ref->u.c.component)
175 : : : NULL_TREE,
176 : : caf;
177 : 295 : gfc_se se;
178 : 295 : bool comp_ref = !last_caf_ref->u.c.component->attr.dimension;
179 : 295 : if (comp == NULL_TREE && comp_ref)
180 : : {
181 : 34 : gfc_free_expr (caf_expr);
182 : 34 : return NULL_TREE;
183 : : }
184 : 261 : gfc_init_se (&se, outerse);
185 : 261 : gfc_free_ref_list (last_caf_ref->next);
186 : 261 : last_caf_ref->next = NULL;
187 : 261 : caf_expr->rank = comp_ref ? 0 : last_caf_ref->u.c.component->as->rank;
188 : 522 : caf_expr->corank = last_caf_ref->u.c.component->as
189 : 261 : ? last_caf_ref->u.c.component->as->corank
190 : : : expr->corank;
191 : 261 : se.want_pointer = comp_ref;
192 : 261 : gfc_conv_expr (&se, caf_expr);
193 : 261 : gfc_add_block_to_block (&outerse->pre, &se.pre);
194 : :
195 : 261 : if (TREE_CODE (se.expr) == COMPONENT_REF && comp_ref)
196 : 133 : se.expr = TREE_OPERAND (se.expr, 0);
197 : 261 : gfc_free_expr (caf_expr);
198 : :
199 : 261 : 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 : 128 : caf = gfc_conv_descriptor_token (se.expr);
204 : 261 : 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 : 31040 : gfc_class_data_get (tree decl)
244 : : {
245 : 31040 : tree data;
246 : 31040 : if (POINTER_TYPE_P (TREE_TYPE (decl)))
247 : 5152 : decl = build_fold_indirect_ref_loc (input_location, decl);
248 : 31040 : data = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
249 : : CLASS_DATA_FIELD);
250 : 31040 : return fold_build3_loc (input_location, COMPONENT_REF,
251 : 31040 : TREE_TYPE (data), decl, data,
252 : 31040 : NULL_TREE);
253 : : }
254 : :
255 : :
256 : : tree
257 : 43612 : gfc_class_vptr_get (tree decl)
258 : : {
259 : 43612 : tree vptr;
260 : : /* For class arrays decl may be a temporary descriptor handle, the vptr is
261 : : then available through the saved descriptor. */
262 : 26576 : if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
263 : 45299 : && GFC_DECL_SAVED_DESCRIPTOR (decl))
264 : 1220 : decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
265 : 43612 : if (POINTER_TYPE_P (TREE_TYPE (decl)))
266 : 2273 : decl = build_fold_indirect_ref_loc (input_location, decl);
267 : 43612 : vptr = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
268 : : CLASS_VPTR_FIELD);
269 : 43612 : return fold_build3_loc (input_location, COMPONENT_REF,
270 : 43612 : TREE_TYPE (vptr), decl, vptr,
271 : 43612 : 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 : 20148 : vptr_field_get (tree vptr, int fieldno)
359 : : {
360 : 20148 : tree field;
361 : 20148 : vptr = build_fold_indirect_ref_loc (input_location, vptr);
362 : 20148 : field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (vptr)),
363 : : fieldno);
364 : 20148 : field = fold_build3_loc (input_location, COMPONENT_REF,
365 : 20148 : TREE_TYPE (field), vptr, field,
366 : : NULL_TREE);
367 : 20148 : gcc_assert (field);
368 : 20148 : 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 : 4195 : VTAB_GET_FIELD_GEN (copy, VTABLE_COPY_FIELD)
401 : 1757 : 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 : 5811 : gfc_vptr_size_get (tree vptr)
421 : : {
422 : 5811 : tree size;
423 : 5811 : size = vptr_field_get (vptr, VTABLE_SIZE_FIELD);
424 : : /* Always return size as an array index type. */
425 : 5811 : size = fold_convert (gfc_array_index_type, size);
426 : 5811 : gcc_assert (size);
427 : 5811 : 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 : 8845 : gfc_find_and_cut_at_last_class_ref (gfc_expr *e, bool is_mold,
455 : : gfc_typespec **ts)
456 : : {
457 : 8845 : gfc_expr *base_expr;
458 : 8845 : gfc_ref *ref, *class_ref, *tail = NULL, *array_ref;
459 : :
460 : : /* Find the last class reference. */
461 : 8845 : class_ref = NULL;
462 : 8845 : array_ref = NULL;
463 : :
464 : 8845 : 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 : 22103 : for (ref = e->ref; ref; ref = ref->next)
474 : : {
475 : 13630 : 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 : 12688 : if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
496 : 12688 : array_ref = ref;
497 : :
498 : 12688 : if (ref->type == REF_COMPONENT
499 : 7721 : && 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 : 8835 : if (ts && *ts == NULL)
514 : : return NULL;
515 : :
516 : : /* Remove and store all subsequent references after the
517 : : CLASS reference. */
518 : 8810 : if (class_ref)
519 : : {
520 : 1347 : tail = class_ref->next;
521 : 1347 : class_ref->next = NULL;
522 : : }
523 : 7463 : else if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
524 : : {
525 : 7445 : tail = e->ref;
526 : 7445 : e->ref = NULL;
527 : : }
528 : :
529 : 8810 : if (is_mold)
530 : 61 : base_expr = gfc_expr_to_initialize (e);
531 : : else
532 : 8749 : base_expr = gfc_copy_expr (e);
533 : :
534 : : /* Restore the original tail expression. */
535 : 8810 : if (class_ref)
536 : : {
537 : 1347 : gfc_free_ref_list (class_ref->next);
538 : 1347 : class_ref->next = tail;
539 : : }
540 : 7463 : else if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
541 : : {
542 : 7445 : gfc_free_ref_list (e->ref);
543 : 7445 : 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 : 10386 : gfc_reset_vptr (stmtblock_t *block, gfc_expr *e, tree class_container,
555 : : gfc_symbol *class_type)
556 : : {
557 : 10386 : tree vptr = NULL_TREE;
558 : :
559 : 10386 : if (class_container != NULL_TREE)
560 : 6113 : vptr = gfc_get_vptr_from_expr (class_container);
561 : :
562 : 6113 : if (vptr == NULL_TREE)
563 : : {
564 : 4280 : gfc_se se;
565 : 4280 : gcc_assert (e);
566 : :
567 : : /* Evaluate the expression and obtain the vptr from it. */
568 : 4280 : gfc_init_se (&se, NULL);
569 : 4280 : if (e->rank)
570 : 2101 : gfc_conv_expr_descriptor (&se, e);
571 : : else
572 : 2179 : gfc_conv_expr (&se, e);
573 : 4280 : gfc_add_block_to_block (block, &se.pre);
574 : :
575 : 4280 : vptr = gfc_get_vptr_from_expr (se.expr);
576 : : }
577 : :
578 : : /* If a vptr is not found, we can do nothing more. */
579 : 4280 : if (vptr == NULL_TREE)
580 : : return;
581 : :
582 : 10376 : if (UNLIMITED_POLY (e)
583 : 9430 : || 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 : 9296 : gfc_symbol *vtab, *type = nullptr;
593 : 9296 : tree vtable;
594 : :
595 : 9296 : if (e)
596 : 7989 : 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 : 7989 : gcc_assert (type);
605 : : /* Return the vptr to the address of the declared type. */
606 : 9296 : vtab = gfc_find_derived_vtab (type);
607 : 9296 : vtable = vtab->backend_decl;
608 : 9296 : if (vtable == NULL_TREE)
609 : 70 : vtable = gfc_get_symbol_decl (vtab);
610 : 9296 : vtable = gfc_build_addr_expr (NULL, vtable);
611 : 9296 : vtable = fold_convert (TREE_TYPE (vptr), vtable);
612 : 9296 : 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 : 102771 : gfc_get_class_from_expr (tree expr)
714 : : {
715 : 102771 : tree tmp;
716 : 102771 : tree type;
717 : :
718 : 267752 : for (tmp = expr; tmp; tmp = TREE_OPERAND (tmp, 0))
719 : : {
720 : 267752 : if (CONSTANT_CLASS_P (tmp))
721 : : return NULL_TREE;
722 : :
723 : 267715 : type = TREE_TYPE (tmp);
724 : 311399 : while (type)
725 : : {
726 : 303793 : if (GFC_CLASS_TYPE_P (type))
727 : : return tmp;
728 : 285089 : if (type != TYPE_CANONICAL (type))
729 : 43684 : type = TYPE_CANONICAL (type);
730 : : else
731 : : type = NULL_TREE;
732 : : }
733 : 249011 : if (VAR_P (tmp) || TREE_CODE (tmp) == PARM_DECL)
734 : : break;
735 : : }
736 : :
737 : 84030 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
738 : 55128 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
739 : :
740 : 84030 : 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 : 10993 : gfc_get_vptr_from_expr (tree expr)
752 : : {
753 : 10993 : tree tmp;
754 : :
755 : 10993 : tmp = gfc_get_class_from_expr (expr);
756 : :
757 : 10993 : if (tmp != NULL_TREE)
758 : 10940 : 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 : 591 : for (; cmp; cmp = cmp->next)
1855 : : {
1856 : 415 : if (cmp->initializer)
1857 : : break;
1858 : 211 : 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 : 11621 : realloc_lhs_warning (bt type, bool array, locus *where)
2034 : : {
2035 : 11621 : 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 : 11596 : 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 : 11621 : }
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 : 1234950 : gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
2053 : : {
2054 : 1234950 : dest->ss = src->ss;
2055 : 1234950 : dest->loop = src->loop;
2056 : 1234950 : }
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 : 4423397 : gfc_init_se (gfc_se * se, gfc_se * parent)
2067 : : {
2068 : 4423397 : memset (se, 0, sizeof (gfc_se));
2069 : 4423397 : gfc_init_block (&se->pre);
2070 : 4423397 : gfc_init_block (&se->finalblock);
2071 : 4423397 : gfc_init_block (&se->post);
2072 : :
2073 : 4423397 : se->parent = parent;
2074 : :
2075 : 4423397 : if (parent)
2076 : 1234950 : gfc_copy_se_loopvars (se, parent);
2077 : 4423397 : }
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 : 233993 : gfc_advance_se_ss_chain (gfc_se * se)
2086 : : {
2087 : 233993 : gfc_se *p;
2088 : :
2089 : 233993 : gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
2090 : :
2091 : : p = se;
2092 : : /* Walk down the parent chain. */
2093 : 615973 : while (p != NULL)
2094 : : {
2095 : : /* Simple consistency check. */
2096 : 381980 : gcc_assert (p->parent == NULL || p->parent->ss == p->ss
2097 : : || p->parent->ss->nested_ss == p->ss);
2098 : :
2099 : 381980 : p->ss = p->ss->next;
2100 : :
2101 : 381980 : p = p->parent;
2102 : : }
2103 : 233993 : }
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 : 1543 : gfc_get_tree_for_caf_expr (gfc_expr *expr)
2356 : : {
2357 : 1543 : tree caf_decl;
2358 : 1543 : bool found = false;
2359 : 1543 : gfc_ref *ref;
2360 : :
2361 : 1543 : gcc_assert (expr && expr->expr_type == EXPR_VARIABLE);
2362 : :
2363 : : /* Not-implemented diagnostic. */
2364 : 1543 : 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 : 3297 : for (ref = expr->ref; ref; ref = ref->next)
2371 : 1754 : if (ref->type == REF_COMPONENT)
2372 : : {
2373 : 175 : 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 : 1543 : caf_decl = expr->symtree->n.sym->backend_decl == NULL_TREE
2382 : 1543 : ? gfc_get_symbol_decl (expr->symtree->n.sym)
2383 : : : expr->symtree->n.sym->backend_decl;
2384 : :
2385 : 1543 : 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 : 1519 : 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 : 34 : for (ref = expr->ref; ref; ref = ref->next)
2423 : 34 : if (ref->type == REF_COMPONENT)
2424 : : {
2425 : 34 : gfc_component *comp = ref->u.c.component;
2426 : :
2427 : 34 : if (POINTER_TYPE_P (TREE_TYPE (caf_decl)))
2428 : 0 : caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
2429 : 34 : caf_decl = fold_build3_loc (input_location, COMPONENT_REF,
2430 : 34 : TREE_TYPE (comp->backend_decl), caf_decl,
2431 : : comp->backend_decl, NULL_TREE);
2432 : 34 : 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 : 34 : if (comp->attr.codimension)
2442 : : {
2443 : : found = true;
2444 : : break;
2445 : : }
2446 : : }
2447 : 34 : 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 : 1460 : gfc_get_caf_token_offset (gfc_se *se, tree *token, tree *offset, tree caf_decl,
2456 : : tree se_expr, gfc_expr *expr)
2457 : : {
2458 : 1460 : tree tmp;
2459 : :
2460 : 1460 : gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
2461 : :
2462 : : /* Coarray token. */
2463 : 1460 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
2464 : 357 : *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 : 1460 : 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 : 1265 : gfc_caf_get_image_index (stmtblock_t *block, gfc_expr *e, tree desc)
2570 : : {
2571 : 1265 : gfc_ref *ref;
2572 : 1265 : tree lbound, ubound, extent, tmp, img_idx;
2573 : 1265 : gfc_se se;
2574 : 1265 : int i;
2575 : :
2576 : 1287 : for (ref = e->ref; ref; ref = ref->next)
2577 : 1287 : if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
2578 : : break;
2579 : 1265 : gcc_assert (ref != NULL);
2580 : :
2581 : 1265 : 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 : 1199 : img_idx = build_zero_cst (gfc_array_index_type);
2586 : 1199 : extent = build_one_cst (gfc_array_index_type);
2587 : 1199 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
2588 : 456 : for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
2589 : : {
2590 : 231 : gfc_init_se (&se, NULL);
2591 : 231 : gfc_conv_expr_type (&se, ref->u.ar.start[i], gfc_array_index_type);
2592 : 231 : gfc_add_block_to_block (block, &se.pre);
2593 : 231 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
2594 : 231 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2595 : 231 : TREE_TYPE (lbound), se.expr, lbound);
2596 : 231 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
2597 : : extent, tmp);
2598 : 231 : img_idx = fold_build2_loc (input_location, PLUS_EXPR,
2599 : 231 : TREE_TYPE (tmp), img_idx, tmp);
2600 : 231 : 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 : 1199 : img_idx = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (img_idx),
2633 : 1199 : img_idx, build_one_cst (TREE_TYPE (img_idx)));
2634 : 1199 : 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 : 6790 : gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
2757 : : const char *name, locus *where)
2758 : : {
2759 : 6790 : tree tmp;
2760 : 6790 : tree type;
2761 : 6790 : tree fault;
2762 : 6790 : gfc_se start;
2763 : 6790 : gfc_se end;
2764 : 6790 : char *msg;
2765 : 6790 : mpz_t length;
2766 : :
2767 : 6790 : type = gfc_get_character_type (kind, ref->u.ss.length);
2768 : 6790 : type = build_pointer_type (type);
2769 : :
2770 : 6790 : gfc_init_se (&start, se);
2771 : 6790 : gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
2772 : 6790 : gfc_add_block_to_block (&se->pre, &start.pre);
2773 : :
2774 : 6790 : if (integer_onep (start.expr))
2775 : 2308 : gfc_conv_string_parameter (se);
2776 : : else
2777 : : {
2778 : 4482 : tmp = start.expr;
2779 : 4482 : STRIP_NOPS (tmp);
2780 : : /* Avoid multiple evaluation of substring start. */
2781 : 4482 : if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
2782 : 1666 : start.expr = gfc_evaluate_now (start.expr, &se->pre);
2783 : :
2784 : : /* Change the start of the string. */
2785 : 4482 : if (((TREE_CODE (TREE_TYPE (se->expr)) == ARRAY_TYPE
2786 : 1152 : || TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE)
2787 : 3450 : && TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
2788 : 5514 : || (POINTER_TYPE_P (TREE_TYPE (se->expr))
2789 : 1032 : && 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 : 4482 : 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 : 128 : else if (POINTER_TYPE_P (TREE_TYPE (tmp)))
2801 : : {
2802 : 8 : tree diff;
2803 : 8 : diff = fold_build2 (MINUS_EXPR, gfc_charlen_type_node, start.expr,
2804 : : build_one_cst (gfc_charlen_type_node));
2805 : 8 : diff = fold_convert (size_type_node, diff);
2806 : 8 : se->expr
2807 : 8 : = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (tmp), tmp, diff);
2808 : : }
2809 : : }
2810 : :
2811 : : /* Length = end + 1 - start. */
2812 : 6790 : gfc_init_se (&end, se);
2813 : 6790 : if (ref->u.ss.end == NULL)
2814 : 178 : end.expr = se->string_length;
2815 : : else
2816 : : {
2817 : 6612 : gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
2818 : 6612 : gfc_add_block_to_block (&se->pre, &end.pre);
2819 : : }
2820 : 6790 : tmp = end.expr;
2821 : 6790 : STRIP_NOPS (tmp);
2822 : 6790 : if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
2823 : 2290 : end.expr = gfc_evaluate_now (end.expr, &se->pre);
2824 : :
2825 : 6790 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2826 : 474 : && !gfc_contains_implied_index_p (ref->u.ss.start)
2827 : 7245 : && !gfc_contains_implied_index_p (ref->u.ss.end))
2828 : : {
2829 : 455 : tree nonempty = fold_build2_loc (input_location, LE_EXPR,
2830 : : logical_type_node, start.expr,
2831 : : end.expr);
2832 : :
2833 : : /* Check lower bound. */
2834 : 455 : fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2835 : : start.expr,
2836 : 455 : build_one_cst (TREE_TYPE (start.expr)));
2837 : 455 : fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2838 : : logical_type_node, nonempty, fault);
2839 : 455 : if (name)
2840 : 454 : msg = xasprintf ("Substring out of bounds: lower bound (%%ld) of '%s' "
2841 : : "is less than one", name);
2842 : : else
2843 : 1 : msg = xasprintf ("Substring out of bounds: lower bound (%%ld) "
2844 : : "is less than one");
2845 : 455 : gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
2846 : : fold_convert (long_integer_type_node,
2847 : : start.expr));
2848 : 455 : free (msg);
2849 : :
2850 : : /* Check upper bound. */
2851 : 455 : fault = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2852 : : end.expr, se->string_length);
2853 : 455 : fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2854 : : logical_type_node, nonempty, fault);
2855 : 455 : if (name)
2856 : 454 : msg = xasprintf ("Substring out of bounds: upper bound (%%ld) of '%s' "
2857 : : "exceeds string length (%%ld)", name);
2858 : : else
2859 : 1 : msg = xasprintf ("Substring out of bounds: upper bound (%%ld) "
2860 : : "exceeds string length (%%ld)");
2861 : 455 : gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
2862 : : fold_convert (long_integer_type_node, end.expr),
2863 : : fold_convert (long_integer_type_node,
2864 : : se->string_length));
2865 : 455 : free (msg);
2866 : : }
2867 : :
2868 : : /* Try to calculate the length from the start and end expressions. */
2869 : 6790 : if (ref->u.ss.end
2870 : 6790 : && gfc_dep_difference (ref->u.ss.end, ref->u.ss.start, &length))
2871 : : {
2872 : 5605 : HOST_WIDE_INT i_len;
2873 : :
2874 : 5605 : i_len = gfc_mpz_get_hwi (length) + 1;
2875 : 5605 : if (i_len < 0)
2876 : : i_len = 0;
2877 : :
2878 : 5605 : tmp = build_int_cst (gfc_charlen_type_node, i_len);
2879 : 5605 : mpz_clear (length); /* Was initialized by gfc_dep_difference. */
2880 : : }
2881 : : else
2882 : : {
2883 : 1185 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
2884 : : fold_convert (gfc_charlen_type_node, end.expr),
2885 : : fold_convert (gfc_charlen_type_node, start.expr));
2886 : 1185 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
2887 : : build_int_cst (gfc_charlen_type_node, 1), tmp);
2888 : 1185 : tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
2889 : : tmp, build_int_cst (gfc_charlen_type_node, 0));
2890 : : }
2891 : :
2892 : 6790 : se->string_length = tmp;
2893 : 6790 : }
2894 : :
2895 : :
2896 : : /* Convert a derived type component reference. */
2897 : :
2898 : : void
2899 : 163958 : gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
2900 : : {
2901 : 163958 : gfc_component *c;
2902 : 163958 : tree tmp;
2903 : 163958 : tree decl;
2904 : 163958 : tree field;
2905 : 163958 : tree context;
2906 : :
2907 : 163958 : c = ref->u.c.component;
2908 : :
2909 : 163958 : if (c->backend_decl == NULL_TREE
2910 : 6 : && ref->u.c.sym != NULL)
2911 : 6 : gfc_get_derived_type (ref->u.c.sym);
2912 : :
2913 : 163958 : field = c->backend_decl;
2914 : 163958 : gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
2915 : 163958 : decl = se->expr;
2916 : 163958 : context = DECL_FIELD_CONTEXT (field);
2917 : :
2918 : : /* Components can correspond to fields of different containing
2919 : : types, as components are created without context, whereas
2920 : : a concrete use of a component has the type of decl as context.
2921 : : So, if the type doesn't match, we search the corresponding
2922 : : FIELD_DECL in the parent type. To not waste too much time
2923 : : we cache this result in norestrict_decl.
2924 : : On the other hand, if the context is a UNION or a MAP (a
2925 : : RECORD_TYPE within a UNION_TYPE) always use the given FIELD_DECL. */
2926 : :
2927 : 163958 : if (context != TREE_TYPE (decl)
2928 : 163958 : && !( TREE_CODE (TREE_TYPE (field)) == UNION_TYPE /* Field is union */
2929 : 11155 : || TREE_CODE (context) == UNION_TYPE)) /* Field is map */
2930 : : {
2931 : 11155 : tree f2 = c->norestrict_decl;
2932 : 19172 : if (!f2 || DECL_FIELD_CONTEXT (f2) != TREE_TYPE (decl))
2933 : 6341 : for (f2 = TYPE_FIELDS (TREE_TYPE (decl)); f2; f2 = DECL_CHAIN (f2))
2934 : 6341 : if (TREE_CODE (f2) == FIELD_DECL
2935 : 6341 : && DECL_NAME (f2) == DECL_NAME (field))
2936 : : break;
2937 : 11155 : gcc_assert (f2);
2938 : 11155 : c->norestrict_decl = f2;
2939 : 11155 : field = f2;
2940 : : }
2941 : :
2942 : 163958 : if (ref->u.c.sym && ref->u.c.sym->ts.type == BT_CLASS
2943 : 0 : && strcmp ("_data", c->name) == 0)
2944 : : {
2945 : : /* Found a ref to the _data component. Store the associated ref to
2946 : : the vptr in se->class_vptr. */
2947 : 0 : se->class_vptr = gfc_class_vptr_get (decl);
2948 : : }
2949 : : else
2950 : 163958 : se->class_vptr = NULL_TREE;
2951 : :
2952 : 163958 : tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
2953 : : decl, field, NULL_TREE);
2954 : :
2955 : 163958 : se->expr = tmp;
2956 : :
2957 : : /* Allocatable deferred char arrays are to be handled by the gfc_deferred_
2958 : : strlen () conditional below. */
2959 : 163958 : if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
2960 : 8460 : && !c->ts.deferred
2961 : 5459 : && !c->attr.pdt_string)
2962 : : {
2963 : 5333 : tmp = c->ts.u.cl->backend_decl;
2964 : : /* Components must always be constant length. */
2965 : 5333 : gcc_assert (tmp && INTEGER_CST_P (tmp));
2966 : 5333 : se->string_length = tmp;
2967 : : }
2968 : :
2969 : 163958 : if (gfc_deferred_strlen (c, &field))
2970 : : {
2971 : 3127 : tmp = fold_build3_loc (input_location, COMPONENT_REF,
2972 : 3127 : TREE_TYPE (field),
2973 : : decl, field, NULL_TREE);
2974 : 3127 : se->string_length = tmp;
2975 : : }
2976 : :
2977 : 163958 : if (((c->attr.pointer || c->attr.allocatable)
2978 : 94881 : && (!c->attr.dimension && !c->attr.codimension)
2979 : 52974 : && c->ts.type != BT_CHARACTER)
2980 : 113014 : || c->attr.proc_pointer)
2981 : 56754 : se->expr = build_fold_indirect_ref_loc (input_location,
2982 : : se->expr);
2983 : 163958 : }
2984 : :
2985 : :
2986 : : /* This function deals with component references to components of the
2987 : : parent type for derived type extensions. */
2988 : : void
2989 : 61525 : conv_parent_component_references (gfc_se * se, gfc_ref * ref)
2990 : : {
2991 : 61525 : gfc_component *c;
2992 : 61525 : gfc_component *cmp;
2993 : 61525 : gfc_symbol *dt;
2994 : 61525 : gfc_ref parent;
2995 : :
2996 : 61525 : dt = ref->u.c.sym;
2997 : 61525 : c = ref->u.c.component;
2998 : :
2999 : : /* Return if the component is in this type, i.e. not in the parent type. */
3000 : 105834 : for (cmp = dt->components; cmp; cmp = cmp->next)
3001 : 95882 : if (c == cmp)
3002 : 51573 : return;
3003 : :
3004 : : /* Build a gfc_ref to recursively call gfc_conv_component_ref. */
3005 : 9952 : parent.type = REF_COMPONENT;
3006 : 9952 : parent.next = NULL;
3007 : 9952 : parent.u.c.sym = dt;
3008 : 9952 : parent.u.c.component = dt->components;
3009 : :
3010 : 9952 : if (dt->backend_decl == NULL)
3011 : 0 : gfc_get_derived_type (dt);
3012 : :
3013 : : /* Build the reference and call self. */
3014 : 9952 : gfc_conv_component_ref (se, &parent);
3015 : 9952 : parent.u.c.sym = dt->components->ts.u.derived;
3016 : 9952 : parent.u.c.component = c;
3017 : 9952 : conv_parent_component_references (se, &parent);
3018 : : }
3019 : :
3020 : :
3021 : : static void
3022 : 537 : conv_inquiry (gfc_se * se, gfc_ref * ref, gfc_expr *expr, gfc_typespec *ts)
3023 : : {
3024 : 537 : tree res = se->expr;
3025 : :
3026 : 537 : switch (ref->u.i)
3027 : : {
3028 : 259 : case INQUIRY_RE:
3029 : 518 : res = fold_build1_loc (input_location, REALPART_EXPR,
3030 : 259 : TREE_TYPE (TREE_TYPE (res)), res);
3031 : 259 : break;
3032 : :
3033 : 233 : case INQUIRY_IM:
3034 : 466 : res = fold_build1_loc (input_location, IMAGPART_EXPR,
3035 : 233 : TREE_TYPE (TREE_TYPE (res)), res);
3036 : 233 : break;
3037 : :
3038 : 7 : case INQUIRY_KIND:
3039 : 7 : res = build_int_cst (gfc_typenode_for_spec (&expr->ts),
3040 : 7 : ts->kind);
3041 : 7 : se->string_length = NULL_TREE;
3042 : 7 : break;
3043 : :
3044 : 38 : case INQUIRY_LEN:
3045 : 38 : res = fold_convert (gfc_typenode_for_spec (&expr->ts),
3046 : : se->string_length);
3047 : 38 : se->string_length = NULL_TREE;
3048 : 38 : break;
3049 : :
3050 : 0 : default:
3051 : 0 : gcc_unreachable ();
3052 : : }
3053 : 537 : se->expr = res;
3054 : 537 : }
3055 : :
3056 : : /* Dereference VAR where needed if it is a pointer, reference, etc.
3057 : : according to Fortran semantics. */
3058 : :
3059 : : tree
3060 : 1391405 : gfc_maybe_dereference_var (gfc_symbol *sym, tree var, bool descriptor_only_p,
3061 : : bool is_classarray)
3062 : : {
3063 : 1391405 : if (!POINTER_TYPE_P (TREE_TYPE (var)))
3064 : : return var;
3065 : 273846 : if (is_CFI_desc (sym, NULL))
3066 : 11890 : return build_fold_indirect_ref_loc (input_location, var);
3067 : :
3068 : : /* Characters are entirely different from other types, they are treated
3069 : : separately. */
3070 : 261956 : if (sym->ts.type == BT_CHARACTER)
3071 : : {
3072 : : /* Dereference character pointer dummy arguments
3073 : : or results. */
3074 : 30230 : if ((sym->attr.pointer || sym->attr.allocatable
3075 : 17174 : || (sym->as && sym->as->type == AS_ASSUMED_RANK))
3076 : 13392 : && (sym->attr.dummy
3077 : 10253 : || sym->attr.function
3078 : 9903 : || sym->attr.result))
3079 : 4144 : var = build_fold_indirect_ref_loc (input_location, var);
3080 : : }
3081 : 231726 : else if (!sym->attr.value)
3082 : : {
3083 : : /* Dereference temporaries for class array dummy arguments. */
3084 : 161904 : if (sym->attr.dummy && is_classarray
3085 : 238110 : && GFC_ARRAY_TYPE_P (TREE_TYPE (var)))
3086 : : {
3087 : 4896 : if (!descriptor_only_p)
3088 : 2477 : var = GFC_DECL_SAVED_DESCRIPTOR (var);
3089 : :
3090 : 4896 : var = build_fold_indirect_ref_loc (input_location, var);
3091 : : }
3092 : :
3093 : : /* Dereference non-character scalar dummy arguments. */
3094 : 230922 : if (sym->attr.dummy && !sym->attr.dimension
3095 : 99425 : && !(sym->attr.codimension && sym->attr.allocatable)
3096 : 99364 : && (sym->ts.type != BT_CLASS
3097 : 18332 : || (!CLASS_DATA (sym)->attr.dimension
3098 : 10685 : && !(CLASS_DATA (sym)->attr.codimension
3099 : 270 : && CLASS_DATA (sym)->attr.allocatable))))
3100 : 91579 : var = build_fold_indirect_ref_loc (input_location, var);
3101 : :
3102 : : /* Dereference scalar hidden result. */
3103 : 230922 : if (flag_f2c && sym->ts.type == BT_COMPLEX
3104 : 286 : && (sym->attr.function || sym->attr.result)
3105 : 108 : && !sym->attr.dimension && !sym->attr.pointer
3106 : 60 : && !sym->attr.always_explicit)
3107 : 36 : var = build_fold_indirect_ref_loc (input_location, var);
3108 : :
3109 : : /* Dereference non-character, non-class pointer variables.
3110 : : These must be dummies, results, or scalars. */
3111 : 230922 : if (!is_classarray
3112 : 223310 : && (sym->attr.pointer || sym->attr.allocatable
3113 : 176798 : || gfc_is_associate_pointer (sym)
3114 : 172288 : || (sym->as && sym->as->type == AS_ASSUMED_RANK))
3115 : 300911 : && (sym->attr.dummy
3116 : 33515 : || sym->attr.function
3117 : 32603 : || sym->attr.result
3118 : 31531 : || (!sym->attr.dimension
3119 : 31528 : && (!sym->attr.codimension || !sym->attr.allocatable))))
3120 : 69986 : var = build_fold_indirect_ref_loc (input_location, var);
3121 : : /* Now treat the class array pointer variables accordingly. */
3122 : 160936 : else if (sym->ts.type == BT_CLASS
3123 : 18756 : && sym->attr.dummy
3124 : 18332 : && (CLASS_DATA (sym)->attr.dimension
3125 : 10685 : || CLASS_DATA (sym)->attr.codimension)
3126 : 7917 : && ((CLASS_DATA (sym)->as
3127 : 7917 : && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
3128 : 6920 : || CLASS_DATA (sym)->attr.allocatable
3129 : 5595 : || CLASS_DATA (sym)->attr.class_pointer))
3130 : 2889 : var = build_fold_indirect_ref_loc (input_location, var);
3131 : : /* And the case where a non-dummy, non-result, non-function,
3132 : : non-allocable and non-pointer classarray is present. This case was
3133 : : previously covered by the first if, but with introducing the
3134 : : condition !is_classarray there, that case has to be covered
3135 : : explicitly. */
3136 : 158047 : else if (sym->ts.type == BT_CLASS
3137 : 15867 : && !sym->attr.dummy
3138 : 424 : && !sym->attr.function
3139 : 424 : && !sym->attr.result
3140 : 424 : && (CLASS_DATA (sym)->attr.dimension
3141 : 3 : || CLASS_DATA (sym)->attr.codimension)
3142 : 424 : && (sym->assoc
3143 : 0 : || !CLASS_DATA (sym)->attr.allocatable)
3144 : 424 : && !CLASS_DATA (sym)->attr.class_pointer)
3145 : 424 : var = build_fold_indirect_ref_loc (input_location, var);
3146 : : }
3147 : :
3148 : : return var;
3149 : : }
3150 : :
3151 : : /* Return the contents of a variable. Also handles reference/pointer
3152 : : variables (all Fortran pointer references are implicit). */
3153 : :
3154 : : static void
3155 : 1537998 : gfc_conv_variable (gfc_se * se, gfc_expr * expr)
3156 : : {
3157 : 1537998 : gfc_ss *ss;
3158 : 1537998 : gfc_ref *ref;
3159 : 1537998 : gfc_symbol *sym;
3160 : 1537998 : tree parent_decl = NULL_TREE;
3161 : 1537998 : int parent_flag;
3162 : 1537998 : bool return_value;
3163 : 1537998 : bool alternate_entry;
3164 : 1537998 : bool entry_master;
3165 : 1537998 : bool is_classarray;
3166 : 1537998 : bool first_time = true;
3167 : :
3168 : 1537998 : sym = expr->symtree->n.sym;
3169 : 1537998 : is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
3170 : 1537998 : ss = se->ss;
3171 : 1537998 : if (ss != NULL)
3172 : : {
3173 : 127799 : gfc_ss_info *ss_info = ss->info;
3174 : :
3175 : : /* Check that something hasn't gone horribly wrong. */
3176 : 127799 : gcc_assert (ss != gfc_ss_terminator);
3177 : 127799 : gcc_assert (ss_info->expr == expr);
3178 : :
3179 : : /* A scalarized term. We already know the descriptor. */
3180 : 127799 : se->expr = ss_info->data.array.descriptor;
3181 : 127799 : se->string_length = ss_info->string_length;
3182 : 127799 : ref = ss_info->data.array.ref;
3183 : 127799 : if (ref)
3184 : 127481 : gcc_assert (ref->type == REF_ARRAY
3185 : : && ref->u.ar.type != AR_ELEMENT);
3186 : : else
3187 : 318 : gfc_conv_tmp_array_ref (se);
3188 : : }
3189 : : else
3190 : : {
3191 : 1410199 : tree se_expr = NULL_TREE;
3192 : :
3193 : 1410199 : se->expr = gfc_get_symbol_decl (sym);
3194 : :
3195 : : /* Deal with references to a parent results or entries by storing
3196 : : the current_function_decl and moving to the parent_decl. */
3197 : 1410199 : return_value = sym->attr.function && sym->result == sym;
3198 : 19449 : alternate_entry = sym->attr.function && sym->attr.entry
3199 : 1411274 : && sym->result == sym;
3200 : 2820398 : entry_master = sym->attr.result
3201 : 11604 : && sym->ns->proc_name->attr.entry_master
3202 : 1410580 : && !gfc_return_by_reference (sym->ns->proc_name);
3203 : 1410199 : if (current_function_decl)
3204 : 1391557 : parent_decl = DECL_CONTEXT (current_function_decl);
3205 : :
3206 : 1410199 : if ((se->expr == parent_decl && return_value)
3207 : 1410088 : || (sym->ns && sym->ns->proc_name
3208 : 1405264 : && parent_decl
3209 : 1386622 : && sym->ns->proc_name->backend_decl == parent_decl
3210 : 37445 : && (alternate_entry || entry_master)))
3211 : : parent_flag = 1;
3212 : : else
3213 : 1410055 : parent_flag = 0;
3214 : :
3215 : : /* Special case for assigning the return value of a function.
3216 : : Self recursive functions must have an explicit return value. */
3217 : 1410199 : if (return_value && (se->expr == current_function_decl || parent_flag))
3218 : 11842 : se_expr = gfc_get_fake_result_decl (sym, parent_flag);
3219 : :
3220 : : /* Similarly for alternate entry points. */
3221 : 1398357 : else if (alternate_entry
3222 : 1042 : && (sym->ns->proc_name->backend_decl == current_function_decl
3223 : 0 : || parent_flag))
3224 : : {
3225 : 1042 : gfc_entry_list *el = NULL;
3226 : :
3227 : 1609 : for (el = sym->ns->entries; el; el = el->next)
3228 : 1609 : if (sym == el->sym)
3229 : : {
3230 : 1042 : se_expr = gfc_get_fake_result_decl (sym, parent_flag);
3231 : 1042 : break;
3232 : : }
3233 : : }
3234 : :
3235 : 1397315 : else if (entry_master
3236 : 295 : && (sym->ns->proc_name->backend_decl == current_function_decl
3237 : 0 : || parent_flag))
3238 : 295 : se_expr = gfc_get_fake_result_decl (sym, parent_flag);
3239 : :
3240 : 13179 : if (se_expr)
3241 : 13179 : se->expr = se_expr;
3242 : :
3243 : : /* Procedure actual arguments. Look out for temporary variables
3244 : : with the same attributes as function values. */
3245 : 1397020 : else if (!sym->attr.temporary
3246 : 1396952 : && sym->attr.flavor == FL_PROCEDURE
3247 : 20510 : && se->expr != current_function_decl)
3248 : : {
3249 : 20471 : if (!sym->attr.dummy && !sym->attr.proc_pointer)
3250 : : {
3251 : 18936 : gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
3252 : 18936 : se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
3253 : : }
3254 : 20471 : return;
3255 : : }
3256 : :
3257 : 1389728 : if (sym->ts.type == BT_CLASS
3258 : 68577 : && sym->attr.class_ok
3259 : 68335 : && sym->ts.u.derived->attr.is_class)
3260 : : {
3261 : 25695 : if (is_classarray && DECL_LANG_SPECIFIC (se->expr)
3262 : 75322 : && GFC_DECL_SAVED_DESCRIPTOR (se->expr))
3263 : 5028 : se->class_container = GFC_DECL_SAVED_DESCRIPTOR (se->expr);
3264 : : else
3265 : 63307 : se->class_container = se->expr;
3266 : : }
3267 : :
3268 : : /* Dereference the expression, where needed. */
3269 : 1389728 : if (se->class_container && CLASS_DATA (sym)->attr.codimension
3270 : 1890 : && !CLASS_DATA (sym)->attr.dimension)
3271 : 779 : se->expr
3272 : 779 : = gfc_maybe_dereference_var (sym, se->class_container,
3273 : 779 : se->descriptor_only, is_classarray);
3274 : : else
3275 : 1388949 : se->expr
3276 : 1388949 : = gfc_maybe_dereference_var (sym, se->expr, se->descriptor_only,
3277 : : is_classarray);
3278 : :
3279 : 1389728 : ref = expr->ref;
3280 : : }
3281 : :
3282 : : /* For character variables, also get the length. */
3283 : 1517527 : if (sym->ts.type == BT_CHARACTER)
3284 : : {
3285 : : /* If the character length of an entry isn't set, get the length from
3286 : : the master function instead. */
3287 : 157468 : if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
3288 : 0 : se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
3289 : : else
3290 : 157468 : se->string_length = sym->ts.u.cl->backend_decl;
3291 : 157468 : gcc_assert (se->string_length);
3292 : :
3293 : : /* For coarray strings return the pointer to the data and not the
3294 : : descriptor. */
3295 : 3459 : if (sym->attr.codimension && sym->attr.associate_var
3296 : 6 : && !se->descriptor_only
3297 : 157474 : && TREE_CODE (TREE_TYPE (se->expr)) != ARRAY_TYPE)
3298 : 6 : se->expr = gfc_conv_descriptor_data_get (se->expr);
3299 : : }
3300 : :
3301 : : /* F202Y: Runtime warning that an assumed rank object is associated
3302 : : with an assumed size object. */
3303 : 1517527 : if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
3304 : 88984 : && (gfc_option.allow_std & GFC_STD_F202Y)
3305 : 1517761 : && expr->rank == -1 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr)))
3306 : : {
3307 : 60 : tree dim, lower, upper, cond;
3308 : 60 : char *msg;
3309 : :
3310 : 60 : dim = fold_convert (signed_char_type_node,
3311 : : gfc_conv_descriptor_rank (se->expr));
3312 : 60 : dim = fold_build2_loc (input_location, MINUS_EXPR, signed_char_type_node,
3313 : : dim, build_int_cst (signed_char_type_node, 1));
3314 : 60 : lower = gfc_conv_descriptor_lbound_get (se->expr, dim);
3315 : 60 : upper = gfc_conv_descriptor_ubound_get (se->expr, dim);
3316 : :
3317 : 60 : msg = xasprintf ("Assumed rank object %s is associated with an "
3318 : : "assumed size object", sym->name);
3319 : 60 : cond = fold_build2_loc (input_location, LT_EXPR,
3320 : : logical_type_node, upper, lower);
3321 : 60 : gfc_trans_runtime_check (false, true, cond, &se->pre,
3322 : : &gfc_current_locus, msg);
3323 : 60 : free (msg);
3324 : : }
3325 : :
3326 : : /* Some expressions leak through that haven't been fixed up. */
3327 : 1517527 : if (IS_INFERRED_TYPE (expr) && expr->ref)
3328 : 380 : gfc_fixup_inferred_type_refs (expr);
3329 : :
3330 : 1517527 : gfc_typespec *ts = &sym->ts;
3331 : 1925408 : while (ref)
3332 : : {
3333 : 742519 : switch (ref->type)
3334 : : {
3335 : 581039 : case REF_ARRAY:
3336 : : /* Return the descriptor if that's what we want and this is an array
3337 : : section reference. */
3338 : 581039 : if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
3339 : : return;
3340 : : /* TODO: Pointers to single elements of array sections, eg elemental subs. */
3341 : : /* Return the descriptor for array pointers and allocations. */
3342 : 255330 : if (se->want_pointer
3343 : 22415 : && ref->next == NULL && (se->descriptor_only))
3344 : : return;
3345 : :
3346 : 246401 : gfc_conv_array_ref (se, &ref->u.ar, expr, &expr->where);
3347 : : /* Return a pointer to an element. */
3348 : 246401 : break;
3349 : :
3350 : 154411 : case REF_COMPONENT:
3351 : 154411 : ts = &ref->u.c.component->ts;
3352 : 154411 : if (first_time && IS_CLASS_ARRAY (sym) && sym->attr.dummy
3353 : 5498 : && se->descriptor_only && !CLASS_DATA (sym)->attr.allocatable
3354 : 2886 : && !CLASS_DATA (sym)->attr.class_pointer && CLASS_DATA (sym)->as
3355 : 2886 : && CLASS_DATA (sym)->as->type != AS_ASSUMED_RANK
3356 : 2419 : && strcmp ("_data", ref->u.c.component->name) == 0)
3357 : : /* Skip the first ref of a _data component, because for class
3358 : : arrays that one is already done by introducing a temporary
3359 : : array descriptor. */
3360 : : break;
3361 : :
3362 : 151992 : if (ref->u.c.sym->attr.extension)
3363 : 51482 : conv_parent_component_references (se, ref);
3364 : :
3365 : 151992 : gfc_conv_component_ref (se, ref);
3366 : :
3367 : 151992 : if (ref->u.c.component->ts.type == BT_CLASS
3368 : 11599 : && ref->u.c.component->attr.class_ok
3369 : 11599 : && ref->u.c.component->ts.u.derived->attr.is_class)
3370 : 11599 : se->class_container = se->expr;
3371 : 140393 : else if (!(ref->u.c.sym->attr.flavor == FL_DERIVED
3372 : 137899 : && ref->u.c.sym->attr.is_class))
3373 : 75941 : se->class_container = NULL_TREE;
3374 : :
3375 : 151992 : if (!ref->next && ref->u.c.sym->attr.codimension
3376 : 0 : && se->want_pointer && se->descriptor_only)
3377 : : return;
3378 : :
3379 : : break;
3380 : :
3381 : 6532 : case REF_SUBSTRING:
3382 : 6532 : gfc_conv_substring (se, ref, expr->ts.kind,
3383 : 6532 : expr->symtree->name, &expr->where);
3384 : 6532 : break;
3385 : :
3386 : 537 : case REF_INQUIRY:
3387 : 537 : conv_inquiry (se, ref, expr, ts);
3388 : 537 : break;
3389 : :
3390 : 0 : default:
3391 : 0 : gcc_unreachable ();
3392 : 407881 : break;
3393 : : }
3394 : 407881 : first_time = false;
3395 : 407881 : ref = ref->next;
3396 : : }
3397 : : /* Pointer assignment, allocation or pass by reference. Arrays are handled
3398 : : separately. */
3399 : 1182889 : if (se->want_pointer)
3400 : : {
3401 : 127519 : if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr))
3402 : 7638 : gfc_conv_string_parameter (se);
3403 : : else
3404 : 119881 : se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
3405 : : }
3406 : : }
3407 : :
3408 : :
3409 : : /* Unary ops are easy... Or they would be if ! was a valid op. */
3410 : :
3411 : : static void
3412 : 28092 : gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
3413 : : {
3414 : 28092 : gfc_se operand;
3415 : 28092 : tree type;
3416 : :
3417 : 28092 : gcc_assert (expr->ts.type != BT_CHARACTER);
3418 : : /* Initialize the operand. */
3419 : 28092 : gfc_init_se (&operand, se);
3420 : 28092 : gfc_conv_expr_val (&operand, expr->value.op.op1);
3421 : 28092 : gfc_add_block_to_block (&se->pre, &operand.pre);
3422 : :
3423 : 28092 : type = gfc_typenode_for_spec (&expr->ts);
3424 : :
3425 : : /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
3426 : : We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
3427 : : All other unary operators have an equivalent GIMPLE unary operator. */
3428 : 28092 : if (code == TRUTH_NOT_EXPR)
3429 : 19880 : se->expr = fold_build2_loc (input_location, EQ_EXPR, type, operand.expr,
3430 : : build_int_cst (type, 0));
3431 : : else
3432 : 8212 : se->expr = fold_build1_loc (input_location, code, type, operand.expr);
3433 : :
3434 : 28092 : }
3435 : :
3436 : : /* Expand power operator to optimal multiplications when a value is raised
3437 : : to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
3438 : : Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
3439 : : Programming", 3rd Edition, 1998. */
3440 : :
3441 : : /* This code is mostly duplicated from expand_powi in the backend.
3442 : : We establish the "optimal power tree" lookup table with the defined size.
3443 : : The items in the table are the exponents used to calculate the index
3444 : : exponents. Any integer n less than the value can get an "addition chain",
3445 : : with the first node being one. */
3446 : : #define POWI_TABLE_SIZE 256
3447 : :
3448 : : /* The table is from builtins.cc. */
3449 : : static const unsigned char powi_table[POWI_TABLE_SIZE] =
3450 : : {
3451 : : 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
3452 : : 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
3453 : : 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
3454 : : 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
3455 : : 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
3456 : : 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
3457 : : 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
3458 : : 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
3459 : : 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
3460 : : 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
3461 : : 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
3462 : : 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
3463 : : 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
3464 : : 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
3465 : : 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
3466 : : 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
3467 : : 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
3468 : : 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
3469 : : 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
3470 : : 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
3471 : : 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
3472 : : 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
3473 : : 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
3474 : : 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
3475 : : 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
3476 : : 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
3477 : : 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
3478 : : 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
3479 : : 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
3480 : : 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
3481 : : 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
3482 : : 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
3483 : : };
3484 : :
3485 : : /* If n is larger than lookup table's max index, we use the "window
3486 : : method". */
3487 : : #define POWI_WINDOW_SIZE 3
3488 : :
3489 : : /* Recursive function to expand the power operator. The temporary
3490 : : values are put in tmpvar. The function returns tmpvar[1] ** n. */
3491 : : static tree
3492 : 176241 : gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
3493 : : {
3494 : 176241 : tree op0;
3495 : 176241 : tree op1;
3496 : 176241 : tree tmp;
3497 : 176241 : int digit;
3498 : :
3499 : 176241 : if (n < POWI_TABLE_SIZE)
3500 : : {
3501 : 136094 : if (tmpvar[n])
3502 : : return tmpvar[n];
3503 : :
3504 : 56205 : op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
3505 : 56205 : op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
3506 : : }
3507 : 40147 : else if (n & 1)
3508 : : {
3509 : 9799 : digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
3510 : 9799 : op0 = gfc_conv_powi (se, n - digit, tmpvar);
3511 : 9799 : op1 = gfc_conv_powi (se, digit, tmpvar);
3512 : : }
3513 : : else
3514 : : {
3515 : 30348 : op0 = gfc_conv_powi (se, n >> 1, tmpvar);
3516 : 30348 : op1 = op0;
3517 : : }
3518 : :
3519 : 96352 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
3520 : 96352 : tmp = gfc_evaluate_now (tmp, &se->pre);
3521 : :
3522 : 96352 : if (n < POWI_TABLE_SIZE)
3523 : 56205 : tmpvar[n] = tmp;
3524 : :
3525 : : return tmp;
3526 : : }
3527 : :
3528 : :
3529 : : /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
3530 : : return 1. Else return 0 and a call to runtime library functions
3531 : : will have to be built. */
3532 : : static int
3533 : 2562 : gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
3534 : : {
3535 : 2562 : tree cond;
3536 : 2562 : tree tmp;
3537 : 2562 : tree type;
3538 : 2562 : tree vartmp[POWI_TABLE_SIZE];
3539 : 2562 : HOST_WIDE_INT m;
3540 : 2562 : unsigned HOST_WIDE_INT n;
3541 : 2562 : int sgn;
3542 : 2562 : wi::tree_to_wide_ref wrhs = wi::to_wide (rhs);
3543 : :
3544 : : /* If exponent is too large, we won't expand it anyway, so don't bother
3545 : : with large integer values. */
3546 : 2562 : if (!wi::fits_shwi_p (wrhs))
3547 : : return 0;
3548 : :
3549 : 2202 : m = wrhs.to_shwi ();
3550 : : /* Use the wide_int's routine to reliably get the absolute value on all
3551 : : platforms. Then convert it to a HOST_WIDE_INT like above. */
3552 : 2202 : n = wi::abs (wrhs).to_shwi ();
3553 : :
3554 : 2202 : type = TREE_TYPE (lhs);
3555 : 2202 : sgn = tree_int_cst_sgn (rhs);
3556 : :
3557 : 2202 : if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
3558 : 4404 : || optimize_size) && (m > 2 || m < -1))
3559 : : return 0;
3560 : :
3561 : : /* rhs == 0 */
3562 : 1244 : if (sgn == 0)
3563 : : {
3564 : 167 : se->expr = gfc_build_const (type, integer_one_node);
3565 : 167 : return 1;
3566 : : }
3567 : :
3568 : : /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
3569 : 1077 : if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
3570 : : {
3571 : 152 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3572 : 152 : lhs, build_int_cst (TREE_TYPE (lhs), -1));
3573 : 152 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3574 : 152 : lhs, build_int_cst (TREE_TYPE (lhs), 1));
3575 : :
3576 : : /* If rhs is even,
3577 : : result = (lhs == 1 || lhs == -1) ? 1 : 0. */
3578 : 152 : if ((n & 1) == 0)
3579 : : {
3580 : 72 : tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
3581 : : logical_type_node, tmp, cond);
3582 : 72 : se->expr = fold_build3_loc (input_location, COND_EXPR, type,
3583 : : tmp, build_int_cst (type, 1),
3584 : : build_int_cst (type, 0));
3585 : 72 : return 1;
3586 : : }
3587 : : /* If rhs is odd,
3588 : : result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
3589 : 80 : tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp,
3590 : : build_int_cst (type, -1),
3591 : : build_int_cst (type, 0));
3592 : 80 : se->expr = fold_build3_loc (input_location, COND_EXPR, type,
3593 : : cond, build_int_cst (type, 1), tmp);
3594 : 80 : return 1;
3595 : : }
3596 : :
3597 : 925 : memset (vartmp, 0, sizeof (vartmp));
3598 : 925 : vartmp[1] = lhs;
3599 : 925 : if (sgn == -1)
3600 : : {
3601 : 91 : tmp = gfc_build_const (type, integer_one_node);
3602 : 91 : vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
3603 : : vartmp[1]);
3604 : : }
3605 : :
3606 : 925 : se->expr = gfc_conv_powi (se, n, vartmp);
3607 : :
3608 : 925 : return 1;
3609 : : }
3610 : :
3611 : : /* Convert lhs**rhs, for constant rhs, when both are unsigned.
3612 : : Method:
3613 : : if (rhs == 0) ! Checked here.
3614 : : return 1;
3615 : : if (lhs & 1 == 1) ! odd_cnd
3616 : : {
3617 : : if (bit_size(rhs) < bit_size(lhs)) ! Checked here.
3618 : : return lhs ** rhs;
3619 : :
3620 : : mask = 1 << (bit_size(a) - 1) / 2;
3621 : : return lhs ** (n & rhs);
3622 : : }
3623 : : if (rhs > bit_size(lhs)) ! Checked here.
3624 : : return 0;
3625 : :
3626 : : return lhs ** rhs;
3627 : : */
3628 : :
3629 : : static int
3630 : 15120 : gfc_conv_cst_uint_power (gfc_se * se, tree lhs, tree rhs)
3631 : : {
3632 : 15120 : tree type = TREE_TYPE (lhs);
3633 : 15120 : tree tmp, is_odd, odd_branch, even_branch;
3634 : 15120 : unsigned HOST_WIDE_INT lhs_prec, rhs_prec;
3635 : 15120 : wi::tree_to_wide_ref wrhs = wi::to_wide (rhs);
3636 : 15120 : unsigned HOST_WIDE_INT n, n_odd;
3637 : 15120 : tree vartmp_odd[POWI_TABLE_SIZE], vartmp_even[POWI_TABLE_SIZE];
3638 : :
3639 : : /* Anything ** 0 is one. */
3640 : 15120 : if (integer_zerop (rhs))
3641 : : {
3642 : 1800 : se->expr = build_int_cst (type, 1);
3643 : 1800 : return 1;
3644 : : }
3645 : :
3646 : 13320 : if (!wi::fits_uhwi_p (wrhs))
3647 : : return 0;
3648 : :
3649 : 12960 : n = wrhs.to_uhwi ();
3650 : :
3651 : : /* tmp = a & 1; . */
3652 : 12960 : tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type,
3653 : : lhs, build_int_cst (type, 1));
3654 : 12960 : is_odd = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3655 : : tmp, build_int_cst (type, 1));
3656 : :
3657 : 12960 : lhs_prec = TYPE_PRECISION (type);
3658 : 12960 : rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs));
3659 : :
3660 : 12960 : if (rhs_prec >= lhs_prec && lhs_prec <= HOST_BITS_PER_WIDE_INT)
3661 : : {
3662 : 7044 : unsigned HOST_WIDE_INT mask = (HOST_WIDE_INT_1U << (lhs_prec - 1)) - 1;
3663 : 7044 : n_odd = n & mask;
3664 : : }
3665 : : else
3666 : : n_odd = n;
3667 : :
3668 : 12960 : memset (vartmp_odd, 0, sizeof (vartmp_odd));
3669 : 12960 : vartmp_odd[0] = build_int_cst (type, 1);
3670 : 12960 : vartmp_odd[1] = lhs;
3671 : 12960 : odd_branch = gfc_conv_powi (se, n_odd, vartmp_odd);
3672 : 12960 : even_branch = NULL_TREE;
3673 : :
3674 : 12960 : if (n > lhs_prec)
3675 : 4260 : even_branch = build_int_cst (type, 0);
3676 : : else
3677 : : {
3678 : 8700 : if (n_odd != n)
3679 : : {
3680 : 0 : memset (vartmp_even, 0, sizeof (vartmp_even));
3681 : 0 : vartmp_even[0] = build_int_cst (type, 1);
3682 : 0 : vartmp_even[1] = lhs;
3683 : 0 : even_branch = gfc_conv_powi (se, n, vartmp_even);
3684 : : }
3685 : : }
3686 : 4260 : if (even_branch != NULL_TREE)
3687 : 4260 : se->expr = fold_build3_loc (input_location, COND_EXPR, type, is_odd,
3688 : : odd_branch, even_branch);
3689 : : else
3690 : 8700 : se->expr = odd_branch;
3691 : :
3692 : : return 1;
3693 : : }
3694 : :
3695 : : /* Power op (**). Constant integer exponent and powers of 2 have special
3696 : : handling. */
3697 : :
3698 : : static void
3699 : 48547 : gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
3700 : : {
3701 : 48547 : tree gfc_int4_type_node;
3702 : 48547 : int kind;
3703 : 48547 : int ikind;
3704 : 48547 : int res_ikind_1, res_ikind_2;
3705 : 48547 : gfc_se lse;
3706 : 48547 : gfc_se rse;
3707 : 48547 : tree fndecl = NULL;
3708 : :
3709 : 48547 : gfc_init_se (&lse, se);
3710 : 48547 : gfc_conv_expr_val (&lse, expr->value.op.op1);
3711 : 48547 : lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
3712 : 48547 : gfc_add_block_to_block (&se->pre, &lse.pre);
3713 : :
3714 : 48547 : gfc_init_se (&rse, se);
3715 : 48547 : gfc_conv_expr_val (&rse, expr->value.op.op2);
3716 : 48547 : gfc_add_block_to_block (&se->pre, &rse.pre);
3717 : :
3718 : 48547 : if (expr->value.op.op2->expr_type == EXPR_CONSTANT)
3719 : : {
3720 : 16994 : if (expr->value.op.op2->ts.type == BT_INTEGER)
3721 : : {
3722 : 1723 : if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
3723 : 20023 : return;
3724 : : }
3725 : 15271 : else if (expr->value.op.op2->ts.type == BT_UNSIGNED)
3726 : : {
3727 : 15120 : if (gfc_conv_cst_uint_power (se, lse.expr, rse.expr))
3728 : : return;
3729 : : }
3730 : : }
3731 : :
3732 : 32543 : if ((expr->value.op.op2->ts.type == BT_INTEGER
3733 : 31461 : || expr->value.op.op2->ts.type == BT_UNSIGNED)
3734 : 31682 : && expr->value.op.op2->expr_type == EXPR_CONSTANT)
3735 : 839 : if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
3736 : : return;
3737 : :
3738 : 32543 : if (INTEGER_CST_P (lse.expr)
3739 : 15365 : && TREE_CODE (TREE_TYPE (rse.expr)) == INTEGER_TYPE
3740 : 47908 : && expr->value.op.op2->ts.type == BT_INTEGER)
3741 : : {
3742 : 245 : wi::tree_to_wide_ref wlhs = wi::to_wide (lse.expr);
3743 : 245 : HOST_WIDE_INT v;
3744 : 245 : unsigned HOST_WIDE_INT w;
3745 : 245 : int kind, ikind, bit_size;
3746 : :
3747 : 245 : v = wlhs.to_shwi ();
3748 : 245 : w = absu_hwi (v);
3749 : :
3750 : 245 : kind = expr->value.op.op1->ts.kind;
3751 : 245 : ikind = gfc_validate_kind (BT_INTEGER, kind, false);
3752 : 245 : bit_size = gfc_integer_kinds[ikind].bit_size;
3753 : :
3754 : 245 : if (v == 1)
3755 : : {
3756 : : /* 1**something is always 1. */
3757 : 35 : se->expr = build_int_cst (TREE_TYPE (lse.expr), 1);
3758 : 239 : return;
3759 : : }
3760 : 210 : else if (v == -1)
3761 : : {
3762 : : /* (-1)**n is 1 - ((n & 1) << 1) */
3763 : 34 : tree type;
3764 : 34 : tree tmp;
3765 : :
3766 : 34 : type = TREE_TYPE (lse.expr);
3767 : 34 : tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type,
3768 : : rse.expr, build_int_cst (type, 1));
3769 : 34 : tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3770 : : tmp, build_int_cst (type, 1));
3771 : 34 : tmp = fold_build2_loc (input_location, MINUS_EXPR, type,
3772 : : build_int_cst (type, 1), tmp);
3773 : 34 : se->expr = tmp;
3774 : 34 : return;
3775 : : }
3776 : 176 : else if (w > 0 && ((w & (w-1)) == 0) && ((w >> (bit_size-1)) == 0))
3777 : : {
3778 : : /* Here v is +/- 2**e. The further simplification uses
3779 : : 2**n = 1<<n, 4**n = 1<<(n+n), 8**n = 1 <<(3*n), 16**n =
3780 : : 1<<(4*n), etc., but we have to make sure to return zero
3781 : : if the number of bits is too large. */
3782 : 170 : tree lshift;
3783 : 170 : tree type;
3784 : 170 : tree shift;
3785 : 170 : tree ge;
3786 : 170 : tree cond;
3787 : 170 : tree num_bits;
3788 : 170 : tree cond2;
3789 : 170 : tree tmp1;
3790 : :
3791 : 170 : type = TREE_TYPE (lse.expr);
3792 : :
3793 : 170 : if (w == 2)
3794 : 110 : shift = rse.expr;
3795 : 60 : else if (w == 4)
3796 : 12 : shift = fold_build2_loc (input_location, PLUS_EXPR,
3797 : 12 : TREE_TYPE (rse.expr),
3798 : : rse.expr, rse.expr);
3799 : : else
3800 : : {
3801 : : /* use popcount for fast log2(w) */
3802 : 48 : int e = wi::popcount (w-1);
3803 : 96 : shift = fold_build2_loc (input_location, MULT_EXPR,
3804 : 48 : TREE_TYPE (rse.expr),
3805 : 48 : build_int_cst (TREE_TYPE (rse.expr), e),
3806 : : rse.expr);
3807 : : }
3808 : :
3809 : 170 : lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3810 : : build_int_cst (type, 1), shift);
3811 : 170 : ge = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
3812 : : rse.expr, build_int_cst (type, 0));
3813 : 170 : cond = fold_build3_loc (input_location, COND_EXPR, type, ge, lshift,
3814 : : build_int_cst (type, 0));
3815 : 170 : num_bits = build_int_cst (TREE_TYPE (rse.expr), TYPE_PRECISION (type));
3816 : 170 : cond2 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
3817 : : rse.expr, num_bits);
3818 : 170 : tmp1 = fold_build3_loc (input_location, COND_EXPR, type, cond2,
3819 : : build_int_cst (type, 0), cond);
3820 : 170 : if (v > 0)
3821 : : {
3822 : 128 : se->expr = tmp1;
3823 : : }
3824 : : else
3825 : : {
3826 : : /* for v < 0, calculate v**n = |v|**n * (-1)**n */
3827 : 42 : tree tmp2;
3828 : 42 : tmp2 = fold_build2_loc (input_location, BIT_AND_EXPR, type,
3829 : : rse.expr, build_int_cst (type, 1));
3830 : 42 : tmp2 = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3831 : : tmp2, build_int_cst (type, 1));
3832 : 42 : tmp2 = fold_build2_loc (input_location, MINUS_EXPR, type,
3833 : : build_int_cst (type, 1), tmp2);
3834 : 42 : se->expr = fold_build2_loc (input_location, MULT_EXPR, type,
3835 : : tmp1, tmp2);
3836 : : }
3837 : 170 : return;
3838 : : }
3839 : : }
3840 : : /* Handle unsigned separate from signed above, things would be too
3841 : : complicated otherwise. */
3842 : :
3843 : 32304 : if (INTEGER_CST_P (lse.expr) && expr->value.op.op1->ts.type == BT_UNSIGNED)
3844 : : {
3845 : 15120 : gfc_expr * op1 = expr->value.op.op1;
3846 : 15120 : tree type;
3847 : :
3848 : 15120 : type = TREE_TYPE (lse.expr);
3849 : :
3850 : 15120 : if (mpz_cmp_ui (op1->value.integer, 1) == 0)
3851 : : {
3852 : : /* 1**something is always 1. */
3853 : 1260 : se->expr = build_int_cst (type, 1);
3854 : 1260 : return;
3855 : : }
3856 : :
3857 : : /* Simplify 2u**x to a shift, with the value set to zero if it falls
3858 : : outside the range. */
3859 : 26460 : if (mpz_popcount (op1->value.integer) == 1)
3860 : : {
3861 : 2520 : tree prec_m1, lim, shift, lshift, cond, tmp;
3862 : 2520 : tree rtype = TREE_TYPE (rse.expr);
3863 : 2520 : int e = mpz_scan1 (op1->value.integer, 0);
3864 : :
3865 : 2520 : shift = fold_build2_loc (input_location, MULT_EXPR,
3866 : 2520 : rtype, build_int_cst (rtype, e),
3867 : : rse.expr);
3868 : 2520 : lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3869 : : build_int_cst (type, 1), shift);
3870 : 5040 : prec_m1 = fold_build2_loc (input_location, MINUS_EXPR, rtype,
3871 : 2520 : build_int_cst (rtype, TYPE_PRECISION (type)),
3872 : : build_int_cst (rtype, 1));
3873 : 2520 : lim = fold_build2_loc (input_location, TRUNC_DIV_EXPR, rtype,
3874 : 2520 : prec_m1, build_int_cst (rtype, e));
3875 : 2520 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
3876 : : rse.expr, lim);
3877 : 2520 : tmp = fold_build3_loc (input_location, COND_EXPR, type, cond,
3878 : : build_int_cst (type, 0), lshift);
3879 : 2520 : se->expr = tmp;
3880 : 2520 : return;
3881 : : }
3882 : : }
3883 : :
3884 : 28524 : gfc_int4_type_node = gfc_get_int_type (4);
3885 : :
3886 : : /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
3887 : : library routine. But in the end, we have to convert the result back
3888 : : if this case applies -- with res_ikind_K, we keep track whether operand K
3889 : : falls into this case. */
3890 : 28524 : res_ikind_1 = -1;
3891 : 28524 : res_ikind_2 = -1;
3892 : :
3893 : 28524 : kind = expr->value.op.op1->ts.kind;
3894 : 28524 : switch (expr->value.op.op2->ts.type)
3895 : : {
3896 : 843 : case BT_INTEGER:
3897 : 843 : ikind = expr->value.op.op2->ts.kind;
3898 : 843 : switch (ikind)
3899 : : {
3900 : 144 : case 1:
3901 : 144 : case 2:
3902 : 144 : rse.expr = convert (gfc_int4_type_node, rse.expr);
3903 : 144 : res_ikind_2 = ikind;
3904 : : /* Fall through. */
3905 : :
3906 : : case 4:
3907 : : ikind = 0;
3908 : : break;
3909 : :
3910 : : case 8:
3911 : : ikind = 1;
3912 : : break;
3913 : :
3914 : 6 : case 16:
3915 : 6 : ikind = 2;
3916 : 6 : break;
3917 : :
3918 : 0 : default:
3919 : 0 : gcc_unreachable ();
3920 : : }
3921 : 843 : switch (kind)
3922 : : {
3923 : 0 : case 1:
3924 : 0 : case 2:
3925 : 0 : if (expr->value.op.op1->ts.type == BT_INTEGER)
3926 : : {
3927 : 0 : lse.expr = convert (gfc_int4_type_node, lse.expr);
3928 : 0 : res_ikind_1 = kind;
3929 : : }
3930 : : else
3931 : 0 : gcc_unreachable ();
3932 : : /* Fall through. */
3933 : :
3934 : : case 4:
3935 : : kind = 0;
3936 : : break;
3937 : :
3938 : : case 8:
3939 : : kind = 1;
3940 : : break;
3941 : :
3942 : 6 : case 10:
3943 : 6 : kind = 2;
3944 : 6 : break;
3945 : :
3946 : 18 : case 16:
3947 : 18 : kind = 3;
3948 : 18 : break;
3949 : :
3950 : 0 : default:
3951 : 0 : gcc_unreachable ();
3952 : : }
3953 : :
3954 : 843 : switch (expr->value.op.op1->ts.type)
3955 : : {
3956 : 99 : case BT_INTEGER:
3957 : 99 : if (kind == 3) /* Case 16 was not handled properly above. */
3958 : : kind = 2;
3959 : 99 : fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
3960 : 99 : break;
3961 : :
3962 : 557 : case BT_REAL:
3963 : : /* Use builtins for real ** int4. */
3964 : 557 : if (ikind == 0)
3965 : : {
3966 : 500 : switch (kind)
3967 : : {
3968 : 327 : case 0:
3969 : 327 : fndecl = builtin_decl_explicit (BUILT_IN_POWIF);
3970 : 327 : break;
3971 : :
3972 : 155 : case 1:
3973 : 155 : fndecl = builtin_decl_explicit (BUILT_IN_POWI);
3974 : 155 : break;
3975 : :
3976 : 6 : case 2:
3977 : 6 : fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
3978 : 6 : break;
3979 : :
3980 : 12 : case 3:
3981 : : /* Use the __builtin_powil() only if real(kind=16) is
3982 : : actually the C long double type. */
3983 : 12 : if (!gfc_real16_is_float128)
3984 : 0 : fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
3985 : : break;
3986 : :
3987 : : default:
3988 : : gcc_unreachable ();
3989 : : }
3990 : : }
3991 : :
3992 : : /* If we don't have a good builtin for this, go for the
3993 : : library function. */
3994 : 488 : if (!fndecl)
3995 : 69 : fndecl = gfor_fndecl_math_powi[kind][ikind].real;
3996 : : break;
3997 : :
3998 : 187 : case BT_COMPLEX:
3999 : 187 : fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
4000 : 187 : break;
4001 : :
4002 : 0 : default:
4003 : 0 : gcc_unreachable ();
4004 : : }
4005 : : break;
4006 : :
4007 : 132 : case BT_REAL:
4008 : 132 : fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
4009 : 132 : break;
4010 : :
4011 : 729 : case BT_COMPLEX:
4012 : 729 : fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
4013 : 729 : break;
4014 : :
4015 : 26820 : case BT_UNSIGNED:
4016 : 26820 : {
4017 : : /* Valid kinds for unsigned are 1, 2, 4, 8, 16. Instead of using a
4018 : : large switch statement, let's just use __builtin_ctz. */
4019 : 26820 : int base = __builtin_ctz (expr->value.op.op1->ts.kind);
4020 : 26820 : int expon = __builtin_ctz (expr->value.op.op2->ts.kind);
4021 : 26820 : fndecl = gfor_fndecl_unsigned_pow_list[base][expon];
4022 : : }
4023 : 26820 : break;
4024 : :
4025 : 0 : default:
4026 : 0 : gcc_unreachable ();
4027 : 28524 : break;
4028 : : }
4029 : :
4030 : 28524 : se->expr = build_call_expr_loc (input_location,
4031 : : fndecl, 2, lse.expr, rse.expr);
4032 : :
4033 : : /* Convert the result back if it is of wrong integer kind. */
4034 : 28524 : if (res_ikind_1 != -1 && res_ikind_2 != -1)
4035 : : {
4036 : : /* We want the maximum of both operand kinds as result. */
4037 : 0 : if (res_ikind_1 < res_ikind_2)
4038 : 0 : res_ikind_1 = res_ikind_2;
4039 : 0 : se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr);
4040 : : }
4041 : : }
4042 : :
4043 : :
4044 : : /* Generate code to allocate a string temporary. */
4045 : :
4046 : : tree
4047 : 4625 : gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
4048 : : {
4049 : 4625 : tree var;
4050 : 4625 : tree tmp;
4051 : :
4052 : 4625 : if (gfc_can_put_var_on_stack (len))
4053 : : {
4054 : : /* Create a temporary variable to hold the result. */
4055 : 4044 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
4056 : 2022 : TREE_TYPE (len), len,
4057 : 2022 : build_int_cst (TREE_TYPE (len), 1));
4058 : 2022 : tmp = build_range_type (gfc_charlen_type_node, size_zero_node, tmp);
4059 : :
4060 : 2022 : if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
4061 : 1992 : tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
4062 : : else
4063 : 30 : tmp = build_array_type (TREE_TYPE (type), tmp);
4064 : :
4065 : 2022 : var = gfc_create_var (tmp, "str");
4066 : 2022 : var = gfc_build_addr_expr (type, var);
4067 : : }
4068 : : else
4069 : : {
4070 : : /* Allocate a temporary to hold the result. */
4071 : 2603 : var = gfc_create_var (type, "pstr");
4072 : 2603 : gcc_assert (POINTER_TYPE_P (type));
4073 : 2603 : tmp = TREE_TYPE (type);
4074 : 2603 : if (TREE_CODE (tmp) == ARRAY_TYPE)
4075 : 2561 : tmp = TREE_TYPE (tmp);
4076 : 2603 : tmp = TYPE_SIZE_UNIT (tmp);
4077 : 2603 : tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
4078 : : fold_convert (size_type_node, len),
4079 : : fold_convert (size_type_node, tmp));
4080 : 2603 : tmp = gfc_call_malloc (&se->pre, type, tmp);
4081 : 2603 : gfc_add_modify (&se->pre, var, tmp);
4082 : :
4083 : : /* Free the temporary afterwards. */
4084 : 2603 : tmp = gfc_call_free (var);
4085 : 2603 : gfc_add_expr_to_block (&se->post, tmp);
4086 : : }
4087 : :
4088 : 4625 : return var;
4089 : : }
4090 : :
4091 : :
4092 : : /* Handle a string concatenation operation. A temporary will be allocated to
4093 : : hold the result. */
4094 : :
4095 : : static void
4096 : 1168 : gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
4097 : : {
4098 : 1168 : gfc_se lse, rse;
4099 : 1168 : tree len, type, var, tmp, fndecl;
4100 : :
4101 : 1168 : gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
4102 : : && expr->value.op.op2->ts.type == BT_CHARACTER);
4103 : 1168 : gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
4104 : :
4105 : 1168 : gfc_init_se (&lse, se);
4106 : 1168 : gfc_conv_expr (&lse, expr->value.op.op1);
4107 : 1168 : gfc_conv_string_parameter (&lse);
4108 : 1168 : gfc_init_se (&rse, se);
4109 : 1168 : gfc_conv_expr (&rse, expr->value.op.op2);
4110 : 1168 : gfc_conv_string_parameter (&rse);
4111 : :
4112 : 1168 : gfc_add_block_to_block (&se->pre, &lse.pre);
4113 : 1168 : gfc_add_block_to_block (&se->pre, &rse.pre);
4114 : :
4115 : 1168 : type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
4116 : 1168 : len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
4117 : 1168 : if (len == NULL_TREE)
4118 : : {
4119 : 1029 : len = fold_build2_loc (input_location, PLUS_EXPR,
4120 : : gfc_charlen_type_node,
4121 : : fold_convert (gfc_charlen_type_node,
4122 : : lse.string_length),
4123 : : fold_convert (gfc_charlen_type_node,
4124 : : rse.string_length));
4125 : : }
4126 : :
4127 : 1168 : type = build_pointer_type (type);
4128 : :
4129 : 1168 : var = gfc_conv_string_tmp (se, type, len);
4130 : :
4131 : : /* Do the actual concatenation. */
4132 : 1168 : if (expr->ts.kind == 1)
4133 : 1079 : fndecl = gfor_fndecl_concat_string;
4134 : 89 : else if (expr->ts.kind == 4)
4135 : 89 : fndecl = gfor_fndecl_concat_string_char4;
4136 : : else
4137 : 0 : gcc_unreachable ();
4138 : :
4139 : 1168 : tmp = build_call_expr_loc (input_location,
4140 : : fndecl, 6, len, var, lse.string_length, lse.expr,
4141 : : rse.string_length, rse.expr);
4142 : 1168 : gfc_add_expr_to_block (&se->pre, tmp);
4143 : :
4144 : : /* Add the cleanup for the operands. */
4145 : 1168 : gfc_add_block_to_block (&se->pre, &rse.post);
4146 : 1168 : gfc_add_block_to_block (&se->pre, &lse.post);
4147 : :
4148 : 1168 : se->expr = var;
4149 : 1168 : se->string_length = len;
4150 : 1168 : }
4151 : :
4152 : : /* Translates an op expression. Common (binary) cases are handled by this
4153 : : function, others are passed on. Recursion is used in either case.
4154 : : We use the fact that (op1.ts == op2.ts) (except for the power
4155 : : operator **).
4156 : : Operators need no special handling for scalarized expressions as long as
4157 : : they call gfc_conv_simple_val to get their operands.
4158 : : Character strings get special handling. */
4159 : :
4160 : : static void
4161 : 490458 : gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
4162 : : {
4163 : 490458 : enum tree_code code;
4164 : 490458 : gfc_se lse;
4165 : 490458 : gfc_se rse;
4166 : 490458 : tree tmp, type;
4167 : 490458 : int lop;
4168 : 490458 : int checkstring;
4169 : :
4170 : 490458 : checkstring = 0;
4171 : 490458 : lop = 0;
4172 : 490458 : switch (expr->value.op.op)
4173 : : {
4174 : 14572 : case INTRINSIC_PARENTHESES:
4175 : 14572 : if ((expr->ts.type == BT_REAL || expr->ts.type == BT_COMPLEX)
4176 : 3446 : && flag_protect_parens)
4177 : : {
4178 : 3313 : gfc_conv_unary_op (PAREN_EXPR, se, expr);
4179 : 3313 : gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
4180 : 89072 : return;
4181 : : }
4182 : :
4183 : : /* Fallthrough. */
4184 : 11265 : case INTRINSIC_UPLUS:
4185 : 11265 : gfc_conv_expr (se, expr->value.op.op1);
4186 : 11265 : return;
4187 : :
4188 : 4899 : case INTRINSIC_UMINUS:
4189 : 4899 : gfc_conv_unary_op (NEGATE_EXPR, se, expr);
4190 : 4899 : return;
4191 : :
4192 : 19880 : case INTRINSIC_NOT:
4193 : 19880 : gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
4194 : 19880 : return;
4195 : :
4196 : : case INTRINSIC_PLUS:
4197 : : code = PLUS_EXPR;
4198 : : break;
4199 : :
4200 : 27009 : case INTRINSIC_MINUS:
4201 : 27009 : code = MINUS_EXPR;
4202 : 27009 : break;
4203 : :
4204 : 30602 : case INTRINSIC_TIMES:
4205 : 30602 : code = MULT_EXPR;
4206 : 30602 : break;
4207 : :
4208 : 6342 : case INTRINSIC_DIVIDE:
4209 : : /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
4210 : : an integer or unsigned, we must round towards zero, so we use a
4211 : : TRUNC_DIV_EXPR. */
4212 : 6342 : if (expr->ts.type == BT_INTEGER || expr->ts.type == BT_UNSIGNED)
4213 : : code = TRUNC_DIV_EXPR;
4214 : : else
4215 : 401386 : code = RDIV_EXPR;
4216 : : break;
4217 : :
4218 : 48547 : case INTRINSIC_POWER:
4219 : 48547 : gfc_conv_power_op (se, expr);
4220 : 48547 : return;
4221 : :
4222 : 1168 : case INTRINSIC_CONCAT:
4223 : 1168 : gfc_conv_concat_op (se, expr);
4224 : 1168 : return;
4225 : :
4226 : 4701 : case INTRINSIC_AND:
4227 : 4701 : code = flag_frontend_optimize ? TRUTH_ANDIF_EXPR : TRUTH_AND_EXPR;
4228 : : lop = 1;
4229 : : break;
4230 : :
4231 : 55430 : case INTRINSIC_OR:
4232 : 55430 : code = flag_frontend_optimize ? TRUTH_ORIF_EXPR : TRUTH_OR_EXPR;
4233 : : lop = 1;
4234 : : break;
4235 : :
4236 : : /* EQV and NEQV only work on logicals, but since we represent them
4237 : : as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
4238 : 12156 : case INTRINSIC_EQ:
4239 : 12156 : case INTRINSIC_EQ_OS:
4240 : 12156 : case INTRINSIC_EQV:
4241 : 12156 : code = EQ_EXPR;
4242 : 12156 : checkstring = 1;
4243 : 12156 : lop = 1;
4244 : 12156 : break;
4245 : :
4246 : 200551 : case INTRINSIC_NE:
4247 : 200551 : case INTRINSIC_NE_OS:
4248 : 200551 : case INTRINSIC_NEQV:
4249 : 200551 : code = NE_EXPR;
4250 : 200551 : checkstring = 1;
4251 : 200551 : lop = 1;
4252 : 200551 : break;
4253 : :
4254 : 11701 : case INTRINSIC_GT:
4255 : 11701 : case INTRINSIC_GT_OS:
4256 : 11701 : code = GT_EXPR;
4257 : 11701 : checkstring = 1;
4258 : 11701 : lop = 1;
4259 : 11701 : break;
4260 : :
4261 : 1650 : case INTRINSIC_GE:
4262 : 1650 : case INTRINSIC_GE_OS:
4263 : 1650 : code = GE_EXPR;
4264 : 1650 : checkstring = 1;
4265 : 1650 : lop = 1;
4266 : 1650 : break;
4267 : :
4268 : 4266 : case INTRINSIC_LT:
4269 : 4266 : case INTRINSIC_LT_OS:
4270 : 4266 : code = LT_EXPR;
4271 : 4266 : checkstring = 1;
4272 : 4266 : lop = 1;
4273 : 4266 : break;
4274 : :
4275 : 2582 : case INTRINSIC_LE:
4276 : 2582 : case INTRINSIC_LE_OS:
4277 : 2582 : code = LE_EXPR;
4278 : 2582 : checkstring = 1;
4279 : 2582 : lop = 1;
4280 : 2582 : break;
4281 : :
4282 : 0 : case INTRINSIC_USER:
4283 : 0 : case INTRINSIC_ASSIGN:
4284 : : /* These should be converted into function calls by the frontend. */
4285 : 0 : gcc_unreachable ();
4286 : :
4287 : 0 : default:
4288 : 0 : fatal_error (input_location, "Unknown intrinsic op");
4289 : 401386 : return;
4290 : : }
4291 : :
4292 : : /* The only exception to this is **, which is handled separately anyway. */
4293 : 401386 : gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
4294 : :
4295 : 401386 : if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
4296 : 369485 : checkstring = 0;
4297 : :
4298 : : /* lhs */
4299 : 401386 : gfc_init_se (&lse, se);
4300 : 401386 : gfc_conv_expr (&lse, expr->value.op.op1);
4301 : 401386 : gfc_add_block_to_block (&se->pre, &lse.pre);
4302 : :
4303 : : /* rhs */
4304 : 401386 : gfc_init_se (&rse, se);
4305 : 401386 : gfc_conv_expr (&rse, expr->value.op.op2);
4306 : 401386 : gfc_add_block_to_block (&se->pre, &rse.pre);
4307 : :
4308 : 401386 : if (checkstring)
4309 : : {
4310 : 31901 : gfc_conv_string_parameter (&lse);
4311 : 31901 : gfc_conv_string_parameter (&rse);
4312 : :
4313 : 63802 : lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
4314 : : rse.string_length, rse.expr,
4315 : 31901 : expr->value.op.op1->ts.kind,
4316 : : code);
4317 : 31901 : rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
4318 : 31901 : gfc_add_block_to_block (&lse.post, &rse.post);
4319 : : }
4320 : :
4321 : 401386 : type = gfc_typenode_for_spec (&expr->ts);
4322 : :
4323 : 401386 : if (lop)
4324 : : {
4325 : : // Inhibit overeager optimization of Cray pointer comparisons (PR106692).
4326 : 293037 : if (expr->value.op.op1->expr_type == EXPR_VARIABLE
4327 : 164605 : && expr->value.op.op1->ts.type == BT_INTEGER
4328 : 70695 : && expr->value.op.op1->symtree
4329 : 70695 : && expr->value.op.op1->symtree->n.sym->attr.cray_pointer)
4330 : 12 : TREE_THIS_VOLATILE (lse.expr) = 1;
4331 : :
4332 : 293037 : if (expr->value.op.op2->expr_type == EXPR_VARIABLE
4333 : 71011 : && expr->value.op.op2->ts.type == BT_INTEGER
4334 : 12215 : && expr->value.op.op2->symtree
4335 : 12215 : && expr->value.op.op2->symtree->n.sym->attr.cray_pointer)
4336 : 12 : TREE_THIS_VOLATILE (rse.expr) = 1;
4337 : :
4338 : : /* The result of logical ops is always logical_type_node. */
4339 : 293037 : tmp = fold_build2_loc (input_location, code, logical_type_node,
4340 : : lse.expr, rse.expr);
4341 : 293037 : se->expr = convert (type, tmp);
4342 : : }
4343 : : else
4344 : 108349 : se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
4345 : :
4346 : : /* Add the post blocks. */
4347 : 401386 : gfc_add_block_to_block (&se->post, &rse.post);
4348 : 401386 : gfc_add_block_to_block (&se->post, &lse.post);
4349 : : }
4350 : :
4351 : : /* If a string's length is one, we convert it to a single character. */
4352 : :
4353 : : tree
4354 : 131454 : gfc_string_to_single_character (tree len, tree str, int kind)
4355 : : {
4356 : :
4357 : 131454 : if (len == NULL
4358 : 131454 : || !tree_fits_uhwi_p (len)
4359 : 241026 : || !POINTER_TYPE_P (TREE_TYPE (str)))
4360 : : return NULL_TREE;
4361 : :
4362 : 109520 : if (TREE_INT_CST_LOW (len) == 1)
4363 : : {
4364 : 21723 : str = fold_convert (gfc_get_pchar_type (kind), str);
4365 : 21723 : return build_fold_indirect_ref_loc (input_location, str);
4366 : : }
4367 : :
4368 : 87797 : if (kind == 1
4369 : 71795 : && TREE_CODE (str) == ADDR_EXPR
4370 : 61623 : && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
4371 : 44618 : && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
4372 : 27127 : && array_ref_low_bound (TREE_OPERAND (str, 0))
4373 : 27127 : == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
4374 : 27127 : && TREE_INT_CST_LOW (len) > 1
4375 : 113170 : && TREE_INT_CST_LOW (len)
4376 : : == (unsigned HOST_WIDE_INT)
4377 : 25373 : TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
4378 : : {
4379 : 25373 : tree ret = fold_convert (gfc_get_pchar_type (kind), str);
4380 : 25373 : ret = build_fold_indirect_ref_loc (input_location, ret);
4381 : 25373 : if (TREE_CODE (ret) == INTEGER_CST)
4382 : : {
4383 : 25373 : tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
4384 : 25373 : int i, length = TREE_STRING_LENGTH (string_cst);
4385 : 25373 : const char *ptr = TREE_STRING_POINTER (string_cst);
4386 : :
4387 : 37109 : for (i = 1; i < length; i++)
4388 : 36545 : if (ptr[i] != ' ')
4389 : : return NULL_TREE;
4390 : :
4391 : : return ret;
4392 : : }
4393 : : }
4394 : :
4395 : : return NULL_TREE;
4396 : : }
4397 : :
4398 : :
4399 : : static void
4400 : 171 : conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
4401 : : {
4402 : 171 : gcc_assert (expr);
4403 : :
4404 : : /* We used to modify the tree here. Now it is done earlier in
4405 : : the front-end, so we only check it here to avoid regressions. */
4406 : 171 : if (sym->backend_decl)
4407 : : {
4408 : 66 : gcc_assert (TREE_CODE (TREE_TYPE (sym->backend_decl)) == INTEGER_TYPE);
4409 : 66 : gcc_assert (TYPE_UNSIGNED (TREE_TYPE (sym->backend_decl)) == 1);
4410 : 66 : gcc_assert (TYPE_PRECISION (TREE_TYPE (sym->backend_decl)) == CHAR_TYPE_SIZE);
4411 : 66 : gcc_assert (DECL_BY_REFERENCE (sym->backend_decl) == 0);
4412 : : }
4413 : :
4414 : : /* If we have a constant character expression, make it into an
4415 : : integer of type C char. */
4416 : 171 : if ((*expr)->expr_type == EXPR_CONSTANT)
4417 : : {
4418 : 165 : gfc_typespec ts;
4419 : 165 : gfc_clear_ts (&ts);
4420 : :
4421 : 330 : gfc_expr *tmp = gfc_get_int_expr (gfc_default_character_kind, NULL,
4422 : 165 : (*expr)->value.character.string[0]);
4423 : 165 : gfc_replace_expr (*expr, tmp);
4424 : : }
4425 : 6 : else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
4426 : : {
4427 : 6 : if ((*expr)->ref == NULL)
4428 : : {
4429 : 6 : se->expr = gfc_string_to_single_character
4430 : 6 : (integer_one_node,
4431 : 6 : gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
4432 : : gfc_get_symbol_decl
4433 : 6 : ((*expr)->symtree->n.sym)),
4434 : : (*expr)->ts.kind);
4435 : : }
4436 : : else
4437 : : {
4438 : 0 : gfc_conv_variable (se, *expr);
4439 : 0 : se->expr = gfc_string_to_single_character
4440 : 0 : (integer_one_node,
4441 : : gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
4442 : : se->expr),
4443 : 0 : (*expr)->ts.kind);
4444 : : }
4445 : : }
4446 : 171 : }
4447 : :
4448 : : /* Helper function for gfc_build_compare_string. Return LEN_TRIM value
4449 : : if STR is a string literal, otherwise return -1. */
4450 : :
4451 : : static int
4452 : 29552 : gfc_optimize_len_trim (tree len, tree str, int kind)
4453 : : {
4454 : 29552 : if (kind == 1
4455 : 24854 : && TREE_CODE (str) == ADDR_EXPR
4456 : 21541 : && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
4457 : 13972 : && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
4458 : 8904 : && array_ref_low_bound (TREE_OPERAND (str, 0))
4459 : 8904 : == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
4460 : 8904 : && tree_fits_uhwi_p (len)
4461 : 8904 : && tree_to_uhwi (len) >= 1
4462 : 29552 : && tree_to_uhwi (len)
4463 : 8860 : == (unsigned HOST_WIDE_INT)
4464 : 8860 : TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
4465 : : {
4466 : 8860 : tree folded = fold_convert (gfc_get_pchar_type (kind), str);
4467 : 8860 : folded = build_fold_indirect_ref_loc (input_location, folded);
4468 : 8860 : if (TREE_CODE (folded) == INTEGER_CST)
4469 : : {
4470 : 8860 : tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
4471 : 8860 : int length = TREE_STRING_LENGTH (string_cst);
4472 : 8860 : const char *ptr = TREE_STRING_POINTER (string_cst);
4473 : :
4474 : 12001 : for (; length > 0; length--)
4475 : 12001 : if (ptr[length - 1] != ' ')
4476 : : break;
4477 : :
4478 : : return length;
4479 : : }
4480 : : }
4481 : : return -1;
4482 : : }
4483 : :
4484 : : /* Helper to build a call to memcmp. */
4485 : :
4486 : : static tree
4487 : 11791 : build_memcmp_call (tree s1, tree s2, tree n)
4488 : : {
4489 : 11791 : tree tmp;
4490 : :
4491 : 11791 : if (!POINTER_TYPE_P (TREE_TYPE (s1)))
4492 : 0 : s1 = gfc_build_addr_expr (pvoid_type_node, s1);
4493 : : else
4494 : 11791 : s1 = fold_convert (pvoid_type_node, s1);
4495 : :
4496 : 11791 : if (!POINTER_TYPE_P (TREE_TYPE (s2)))
4497 : 0 : s2 = gfc_build_addr_expr (pvoid_type_node, s2);
4498 : : else
4499 : 11791 : s2 = fold_convert (pvoid_type_node, s2);
4500 : :
4501 : 11791 : n = fold_convert (size_type_node, n);
4502 : :
4503 : 11791 : tmp = build_call_expr_loc (input_location,
4504 : : builtin_decl_explicit (BUILT_IN_MEMCMP),
4505 : : 3, s1, s2, n);
4506 : :
4507 : 11791 : return fold_convert (integer_type_node, tmp);
4508 : : }
4509 : :
4510 : : /* Compare two strings. If they are all single characters, the result is the
4511 : : subtraction of them. Otherwise, we build a library call. */
4512 : :
4513 : : tree
4514 : 32000 : gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
4515 : : enum tree_code code)
4516 : : {
4517 : 32000 : tree sc1;
4518 : 32000 : tree sc2;
4519 : 32000 : tree fndecl;
4520 : :
4521 : 32000 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
4522 : 32000 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
4523 : :
4524 : 32000 : sc1 = gfc_string_to_single_character (len1, str1, kind);
4525 : 32000 : sc2 = gfc_string_to_single_character (len2, str2, kind);
4526 : :
4527 : 32000 : if (sc1 != NULL_TREE && sc2 != NULL_TREE)
4528 : : {
4529 : : /* Deal with single character specially. */
4530 : 4720 : sc1 = fold_convert (integer_type_node, sc1);
4531 : 4720 : sc2 = fold_convert (integer_type_node, sc2);
4532 : 4720 : return fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
4533 : 4720 : sc1, sc2);
4534 : : }
4535 : :
4536 : 27280 : if ((code == EQ_EXPR || code == NE_EXPR)
4537 : 26724 : && optimize
4538 : 22461 : && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
4539 : : {
4540 : : /* If one string is a string literal with LEN_TRIM longer
4541 : : than the length of the second string, the strings
4542 : : compare unequal. */
4543 : 14776 : int len = gfc_optimize_len_trim (len1, str1, kind);
4544 : 14776 : if (len > 0 && compare_tree_int (len2, len) < 0)
4545 : 0 : return integer_one_node;
4546 : 14776 : len = gfc_optimize_len_trim (len2, str2, kind);
4547 : 14776 : if (len > 0 && compare_tree_int (len1, len) < 0)
4548 : 0 : return integer_one_node;
4549 : : }
4550 : :
4551 : : /* We can compare via memcpy if the strings are known to be equal
4552 : : in length and they are
4553 : : - kind=1
4554 : : - kind=4 and the comparison is for (in)equality. */
4555 : :
4556 : 17952 : if (INTEGER_CST_P (len1) && INTEGER_CST_P (len2)
4557 : 17614 : && tree_int_cst_equal (len1, len2)
4558 : 39131 : && (kind == 1 || code == EQ_EXPR || code == NE_EXPR))
4559 : : {
4560 : 11791 : tree tmp;
4561 : 11791 : tree chartype;
4562 : :
4563 : 11791 : chartype = gfc_get_char_type (kind);
4564 : 11791 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE(len1),
4565 : 11791 : fold_convert (TREE_TYPE(len1),
4566 : : TYPE_SIZE_UNIT(chartype)),
4567 : : len1);
4568 : 11791 : return build_memcmp_call (str1, str2, tmp);
4569 : : }
4570 : :
4571 : : /* Build a call for the comparison. */
4572 : 15489 : if (kind == 1)
4573 : 12653 : fndecl = gfor_fndecl_compare_string;
4574 : 2836 : else if (kind == 4)
4575 : 2836 : fndecl = gfor_fndecl_compare_string_char4;
4576 : : else
4577 : 0 : gcc_unreachable ();
4578 : :
4579 : 15489 : return build_call_expr_loc (input_location, fndecl, 4,
4580 : 15489 : len1, str1, len2, str2);
4581 : : }
4582 : :
4583 : :
4584 : : /* Return the backend_decl for a procedure pointer component. */
4585 : :
4586 : : static tree
4587 : 1713 : get_proc_ptr_comp (gfc_expr *e)
4588 : : {
4589 : 1713 : gfc_se comp_se;
4590 : 1713 : gfc_expr *e2;
4591 : 1713 : expr_t old_type;
4592 : :
4593 : 1713 : gfc_init_se (&comp_se, NULL);
4594 : 1713 : e2 = gfc_copy_expr (e);
4595 : : /* We have to restore the expr type later so that gfc_free_expr frees
4596 : : the exact same thing that was allocated.
4597 : : TODO: This is ugly. */
4598 : 1713 : old_type = e2->expr_type;
4599 : 1713 : e2->expr_type = EXPR_VARIABLE;
4600 : 1713 : gfc_conv_expr (&comp_se, e2);
4601 : 1713 : e2->expr_type = old_type;
4602 : 1713 : gfc_free_expr (e2);
4603 : 1713 : return build_fold_addr_expr_loc (input_location, comp_se.expr);
4604 : : }
4605 : :
4606 : :
4607 : : /* Convert a typebound function reference from a class object. */
4608 : : static void
4609 : 80 : conv_base_obj_fcn_val (gfc_se * se, tree base_object, gfc_expr * expr)
4610 : : {
4611 : 80 : gfc_ref *ref;
4612 : 80 : tree var;
4613 : :
4614 : 80 : if (!VAR_P (base_object))
4615 : : {
4616 : 0 : var = gfc_create_var (TREE_TYPE (base_object), NULL);
4617 : 0 : gfc_add_modify (&se->pre, var, base_object);
4618 : : }
4619 : 80 : se->expr = gfc_class_vptr_get (base_object);
4620 : 80 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
4621 : 80 : ref = expr->ref;
4622 : 308 : while (ref && ref->next)
4623 : : ref = ref->next;
4624 : 80 : gcc_assert (ref && ref->type == REF_COMPONENT);
4625 : 80 : if (ref->u.c.sym->attr.extension)
4626 : 0 : conv_parent_component_references (se, ref);
4627 : 80 : gfc_conv_component_ref (se, ref);
4628 : 80 : se->expr = build_fold_addr_expr_loc (input_location, se->expr);
4629 : 80 : }
4630 : :
4631 : : static tree
4632 : 124387 : get_builtin_fn (gfc_symbol * sym)
4633 : : {
4634 : 124387 : if (!gfc_option.disable_omp_is_initial_device
4635 : 124383 : && flag_openmp && sym->attr.function && sym->ts.type == BT_LOGICAL
4636 : 610 : && !strcmp (sym->name, "omp_is_initial_device"))
4637 : 23 : return builtin_decl_explicit (BUILT_IN_OMP_IS_INITIAL_DEVICE);
4638 : :
4639 : 124364 : if (!gfc_option.disable_omp_get_initial_device
4640 : 124357 : && flag_openmp && sym->attr.function && sym->ts.type == BT_INTEGER
4641 : 4053 : && !strcmp (sym->name, "omp_get_initial_device"))
4642 : 24 : return builtin_decl_explicit (BUILT_IN_OMP_GET_INITIAL_DEVICE);
4643 : :
4644 : 124340 : if (!gfc_option.disable_omp_get_num_devices
4645 : 124333 : && flag_openmp && sym->attr.function && sym->ts.type == BT_INTEGER
4646 : 4029 : && !strcmp (sym->name, "omp_get_num_devices"))
4647 : 70 : return builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_DEVICES);
4648 : :
4649 : 124270 : if (!gfc_option.disable_acc_on_device
4650 : 124090 : && flag_openacc && sym->attr.function && sym->ts.type == BT_LOGICAL
4651 : 1163 : && !strcmp (sym->name, "acc_on_device_h"))
4652 : 390 : return builtin_decl_explicit (BUILT_IN_ACC_ON_DEVICE);
4653 : :
4654 : : return NULL_TREE;
4655 : : }
4656 : :
4657 : : static tree
4658 : 507 : update_builtin_function (tree fn_call, gfc_symbol *sym)
4659 : : {
4660 : 507 : tree fn = TREE_OPERAND (CALL_EXPR_FN (fn_call), 0);
4661 : :
4662 : 507 : if (DECL_FUNCTION_CODE (fn) == BUILT_IN_OMP_IS_INITIAL_DEVICE)
4663 : : /* In Fortran omp_is_initial_device returns logical(4)
4664 : : but the builtin uses 'int'. */
4665 : 23 : return fold_convert (TREE_TYPE (TREE_TYPE (sym->backend_decl)), fn_call);
4666 : :
4667 : 484 : else if (DECL_FUNCTION_CODE (fn) == BUILT_IN_ACC_ON_DEVICE)
4668 : : {
4669 : : /* Likewise for the return type; additionally, the argument it a
4670 : : call-by-value int, Fortran has a by-reference 'integer(4)'. */
4671 : 390 : tree arg = build_fold_indirect_ref_loc (input_location,
4672 : 390 : CALL_EXPR_ARG (fn_call, 0));
4673 : 390 : CALL_EXPR_ARG (fn_call, 0) = fold_convert (integer_type_node, arg);
4674 : 390 : return fold_convert (TREE_TYPE (TREE_TYPE (sym->backend_decl)), fn_call);
4675 : : }
4676 : : return fn_call;
4677 : : }
4678 : :
4679 : : static void
4680 : 126901 : conv_function_val (gfc_se * se, bool *is_builtin, gfc_symbol * sym,
4681 : : gfc_expr * expr, gfc_actual_arglist *actual_args)
4682 : : {
4683 : 126901 : tree tmp;
4684 : :
4685 : 126901 : if (gfc_is_proc_ptr_comp (expr))
4686 : 1713 : tmp = get_proc_ptr_comp (expr);
4687 : 125188 : else if (sym->attr.dummy)
4688 : : {
4689 : 801 : tmp = gfc_get_symbol_decl (sym);
4690 : 801 : if (sym->attr.proc_pointer)
4691 : 83 : tmp = build_fold_indirect_ref_loc (input_location,
4692 : : tmp);
4693 : 801 : gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
4694 : : && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
4695 : : }
4696 : : else
4697 : : {
4698 : 124387 : if (!sym->backend_decl)
4699 : 31183 : sym->backend_decl = gfc_get_extern_function_decl (sym, actual_args);
4700 : :
4701 : 124387 : if ((tmp = get_builtin_fn (sym)) != NULL_TREE)
4702 : 507 : *is_builtin = true;
4703 : : else
4704 : : {
4705 : 123880 : TREE_USED (sym->backend_decl) = 1;
4706 : 123880 : tmp = sym->backend_decl;
4707 : : }
4708 : :
4709 : 124387 : if (sym->attr.cray_pointee)
4710 : : {
4711 : : /* TODO - make the cray pointee a pointer to a procedure,
4712 : : assign the pointer to it and use it for the call. This
4713 : : will do for now! */
4714 : 19 : tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
4715 : 19 : gfc_get_symbol_decl (sym->cp_pointer));
4716 : 19 : tmp = gfc_evaluate_now (tmp, &se->pre);
4717 : : }
4718 : :
4719 : 124387 : if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
4720 : : {
4721 : 123808 : gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
4722 : 123808 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
4723 : : }
4724 : : }
4725 : 126901 : se->expr = tmp;
4726 : 126901 : }
4727 : :
4728 : :
4729 : : /* Initialize MAPPING. */
4730 : :
4731 : : void
4732 : 127018 : gfc_init_interface_mapping (gfc_interface_mapping * mapping)
4733 : : {
4734 : 127018 : mapping->syms = NULL;
4735 : 127018 : mapping->charlens = NULL;
4736 : 127018 : }
4737 : :
4738 : :
4739 : : /* Free all memory held by MAPPING (but not MAPPING itself). */
4740 : :
4741 : : void
4742 : 127018 : gfc_free_interface_mapping (gfc_interface_mapping * mapping)
4743 : : {
4744 : 127018 : gfc_interface_sym_mapping *sym;
4745 : 127018 : gfc_interface_sym_mapping *nextsym;
4746 : 127018 : gfc_charlen *cl;
4747 : 127018 : gfc_charlen *nextcl;
4748 : :
4749 : 167663 : for (sym = mapping->syms; sym; sym = nextsym)
4750 : : {
4751 : 40645 : nextsym = sym->next;
4752 : 40645 : sym->new_sym->n.sym->formal = NULL;
4753 : 40645 : gfc_free_symbol (sym->new_sym->n.sym);
4754 : 40645 : gfc_free_expr (sym->expr);
4755 : 40645 : free (sym->new_sym);
4756 : 40645 : free (sym);
4757 : : }
4758 : 131587 : for (cl = mapping->charlens; cl; cl = nextcl)
4759 : : {
4760 : 4569 : nextcl = cl->next;
4761 : 4569 : gfc_free_expr (cl->length);
4762 : 4569 : free (cl);
4763 : : }
4764 : 127018 : }
4765 : :
4766 : :
4767 : : /* Return a copy of gfc_charlen CL. Add the returned structure to
4768 : : MAPPING so that it will be freed by gfc_free_interface_mapping. */
4769 : :
4770 : : static gfc_charlen *
4771 : 4569 : gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
4772 : : gfc_charlen * cl)
4773 : : {
4774 : 4569 : gfc_charlen *new_charlen;
4775 : :
4776 : 4569 : new_charlen = gfc_get_charlen ();
4777 : 4569 : new_charlen->next = mapping->charlens;
4778 : 4569 : new_charlen->length = gfc_copy_expr (cl->length);
4779 : :
4780 : 4569 : mapping->charlens = new_charlen;
4781 : 4569 : return new_charlen;
4782 : : }
4783 : :
4784 : :
4785 : : /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
4786 : : array variable that can be used as the actual argument for dummy
4787 : : argument SYM. Add any initialization code to BLOCK. PACKED is as
4788 : : for gfc_get_nodesc_array_type and DATA points to the first element
4789 : : in the passed array. */
4790 : :
4791 : : static tree
4792 : 8375 : gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
4793 : : gfc_packed packed, tree data, tree len)
4794 : : {
4795 : 8375 : tree type;
4796 : 8375 : tree var;
4797 : :
4798 : 8375 : if (len != NULL_TREE && (TREE_CONSTANT (len) || VAR_P (len)))
4799 : 58 : type = gfc_get_character_type_len (sym->ts.kind, len);
4800 : : else
4801 : 8317 : type = gfc_typenode_for_spec (&sym->ts);
4802 : 8375 : type = gfc_get_nodesc_array_type (type, sym->as, packed,
4803 : 8351 : !sym->attr.target && !sym->attr.pointer
4804 : 16726 : && !sym->attr.proc_pointer);
4805 : :
4806 : 8375 : var = gfc_create_var (type, "ifm");
4807 : 8375 : gfc_add_modify (block, var, fold_convert (type, data));
4808 : :
4809 : 8375 : return var;
4810 : : }
4811 : :
4812 : :
4813 : : /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
4814 : : and offset of descriptorless array type TYPE given that it has the same
4815 : : size as DESC. Add any set-up code to BLOCK. */
4816 : :
4817 : : static void
4818 : 8105 : gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
4819 : : {
4820 : 8105 : int n;
4821 : 8105 : tree dim;
4822 : 8105 : tree offset;
4823 : 8105 : tree tmp;
4824 : :
4825 : 8105 : offset = gfc_index_zero_node;
4826 : 9180 : for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
4827 : : {
4828 : 1075 : dim = gfc_rank_cst[n];
4829 : 1075 : GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
4830 : 1075 : if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
4831 : : {
4832 : 1 : GFC_TYPE_ARRAY_LBOUND (type, n)
4833 : 1 : = gfc_conv_descriptor_lbound_get (desc, dim);
4834 : 1 : GFC_TYPE_ARRAY_UBOUND (type, n)
4835 : 2 : = gfc_conv_descriptor_ubound_get (desc, dim);
4836 : : }
4837 : 1074 : else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
4838 : : {
4839 : 1074 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
4840 : : gfc_array_index_type,
4841 : : gfc_conv_descriptor_ubound_get (desc, dim),
4842 : : gfc_conv_descriptor_lbound_get (desc, dim));
4843 : 3222 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
4844 : : gfc_array_index_type,
4845 : 1074 : GFC_TYPE_ARRAY_LBOUND (type, n), tmp);
4846 : 1074 : tmp = gfc_evaluate_now (tmp, block);
4847 : 1074 : GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
4848 : : }
4849 : 4300 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
4850 : 1075 : GFC_TYPE_ARRAY_LBOUND (type, n),
4851 : 1075 : GFC_TYPE_ARRAY_STRIDE (type, n));
4852 : 1075 : offset = fold_build2_loc (input_location, MINUS_EXPR,
4853 : : gfc_array_index_type, offset, tmp);
4854 : : }
4855 : 8105 : offset = gfc_evaluate_now (offset, block);
4856 : 8105 : GFC_TYPE_ARRAY_OFFSET (type) = offset;
4857 : 8105 : }
4858 : :
4859 : :
4860 : : /* Extend MAPPING so that it maps dummy argument SYM to the value stored
4861 : : in SE. The caller may still use se->expr and se->string_length after
4862 : : calling this function. */
4863 : :
4864 : : void
4865 : 40645 : gfc_add_interface_mapping (gfc_interface_mapping * mapping,
4866 : : gfc_symbol * sym, gfc_se * se,
4867 : : gfc_expr *expr)
4868 : : {
4869 : 40645 : gfc_interface_sym_mapping *sm;
4870 : 40645 : tree desc;
4871 : 40645 : tree tmp;
4872 : 40645 : tree value;
4873 : 40645 : gfc_symbol *new_sym;
4874 : 40645 : gfc_symtree *root;
4875 : 40645 : gfc_symtree *new_symtree;
4876 : :
4877 : : /* Create a new symbol to represent the actual argument. */
4878 : 40645 : new_sym = gfc_new_symbol (sym->name, NULL);
4879 : 40645 : new_sym->ts = sym->ts;
4880 : 40645 : new_sym->as = gfc_copy_array_spec (sym->as);
4881 : 40645 : new_sym->attr.referenced = 1;
4882 : 40645 : new_sym->attr.dimension = sym->attr.dimension;
4883 : 40645 : new_sym->attr.contiguous = sym->attr.contiguous;
4884 : 40645 : new_sym->attr.codimension = sym->attr.codimension;
4885 : 40645 : new_sym->attr.pointer = sym->attr.pointer;
4886 : 40645 : new_sym->attr.allocatable = sym->attr.allocatable;
4887 : 40645 : new_sym->attr.flavor = sym->attr.flavor;
4888 : 40645 : new_sym->attr.function = sym->attr.function;
4889 : :
4890 : : /* Ensure that the interface is available and that
4891 : : descriptors are passed for array actual arguments. */
4892 : 40645 : if (sym->attr.flavor == FL_PROCEDURE)
4893 : : {
4894 : 36 : new_sym->formal = expr->symtree->n.sym->formal;
4895 : 36 : new_sym->attr.always_explicit
4896 : 36 : = expr->symtree->n.sym->attr.always_explicit;
4897 : : }
4898 : :
4899 : : /* Create a fake symtree for it. */
4900 : 40645 : root = NULL;
4901 : 40645 : new_symtree = gfc_new_symtree (&root, sym->name);
4902 : 40645 : new_symtree->n.sym = new_sym;
4903 : 40645 : gcc_assert (new_symtree == root);
4904 : :
4905 : : /* Create a dummy->actual mapping. */
4906 : 40645 : sm = XCNEW (gfc_interface_sym_mapping);
4907 : 40645 : sm->next = mapping->syms;
4908 : 40645 : sm->old = sym;
4909 : 40645 : sm->new_sym = new_symtree;
4910 : 40645 : sm->expr = gfc_copy_expr (expr);
4911 : 40645 : mapping->syms = sm;
4912 : :
4913 : : /* Stabilize the argument's value. */
4914 : 40645 : if (!sym->attr.function && se)
4915 : 40547 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
4916 : :
4917 : 40645 : if (sym->ts.type == BT_CHARACTER)
4918 : : {
4919 : : /* Create a copy of the dummy argument's length. */
4920 : 2785 : new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
4921 : 2785 : sm->expr->ts.u.cl = new_sym->ts.u.cl;
4922 : :
4923 : : /* If the length is specified as "*", record the length that
4924 : : the caller is passing. We should use the callee's length
4925 : : in all other cases. */
4926 : 2785 : if (!new_sym->ts.u.cl->length && se)
4927 : : {
4928 : 2557 : se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
4929 : 2557 : new_sym->ts.u.cl->backend_decl = se->string_length;
4930 : : }
4931 : : }
4932 : :
4933 : 40631 : if (!se)
4934 : 62 : return;
4935 : :
4936 : : /* Use the passed value as-is if the argument is a function. */
4937 : 40583 : if (sym->attr.flavor == FL_PROCEDURE)
4938 : 36 : value = se->expr;
4939 : :
4940 : : /* If the argument is a pass-by-value scalar, use the value as is. */
4941 : 40547 : else if (!sym->attr.dimension && sym->attr.value)
4942 : 63 : value = se->expr;
4943 : :
4944 : : /* If the argument is either a string or a pointer to a string,
4945 : : convert it to a boundless character type. */
4946 : 40484 : else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
4947 : : {
4948 : 1216 : se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
4949 : 1216 : tmp = gfc_get_character_type_len (sym->ts.kind, se->string_length);
4950 : 1216 : tmp = build_pointer_type (tmp);
4951 : 1216 : if (sym->attr.pointer)
4952 : 126 : value = build_fold_indirect_ref_loc (input_location,
4953 : : se->expr);
4954 : : else
4955 : 1090 : value = se->expr;
4956 : 1216 : value = fold_convert (tmp, value);
4957 : : }
4958 : :
4959 : : /* If the argument is a scalar, a pointer to an array or an allocatable,
4960 : : dereference it. */
4961 : 39268 : else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
4962 : 29396 : value = build_fold_indirect_ref_loc (input_location,
4963 : : se->expr);
4964 : :
4965 : : /* For character(*), use the actual argument's descriptor. */
4966 : 9872 : else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
4967 : 1497 : value = build_fold_indirect_ref_loc (input_location,
4968 : : se->expr);
4969 : :
4970 : : /* If the argument is an array descriptor, use it to determine
4971 : : information about the actual argument's shape. */
4972 : 8375 : else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
4973 : 8375 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
4974 : : {
4975 : : /* Get the actual argument's descriptor. */
4976 : 8105 : desc = build_fold_indirect_ref_loc (input_location,
4977 : : se->expr);
4978 : :
4979 : : /* Create the replacement variable. */
4980 : 8105 : tmp = gfc_conv_descriptor_data_get (desc);
4981 : 8105 : value = gfc_get_interface_mapping_array (&se->pre, sym,
4982 : : PACKED_NO, tmp,
4983 : : se->string_length);
4984 : :
4985 : : /* Use DESC to work out the upper bounds, strides and offset. */
4986 : 8105 : gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
4987 : : }
4988 : : else
4989 : : /* Otherwise we have a packed array. */
4990 : 270 : value = gfc_get_interface_mapping_array (&se->pre, sym,
4991 : : PACKED_FULL, se->expr,
4992 : : se->string_length);
4993 : :
4994 : 40583 : new_sym->backend_decl = value;
4995 : : }
4996 : :
4997 : :
4998 : : /* Called once all dummy argument mappings have been added to MAPPING,
4999 : : but before the mapping is used to evaluate expressions. Pre-evaluate
5000 : : the length of each argument, adding any initialization code to PRE and
5001 : : any finalization code to POST. */
5002 : :
5003 : : static void
5004 : 126981 : gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
5005 : : stmtblock_t * pre, stmtblock_t * post)
5006 : : {
5007 : 126981 : gfc_interface_sym_mapping *sym;
5008 : 126981 : gfc_expr *expr;
5009 : 126981 : gfc_se se;
5010 : :
5011 : 167564 : for (sym = mapping->syms; sym; sym = sym->next)
5012 : 40583 : if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
5013 : 2771 : && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
5014 : : {
5015 : 214 : expr = sym->new_sym->n.sym->ts.u.cl->length;
5016 : 214 : gfc_apply_interface_mapping_to_expr (mapping, expr);
5017 : 214 : gfc_init_se (&se, NULL);
5018 : 214 : gfc_conv_expr (&se, expr);
5019 : 214 : se.expr = fold_convert (gfc_charlen_type_node, se.expr);
5020 : 214 : se.expr = gfc_evaluate_now (se.expr, &se.pre);
5021 : 214 : gfc_add_block_to_block (pre, &se.pre);
5022 : 214 : gfc_add_block_to_block (post, &se.post);
5023 : :
5024 : 214 : sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
5025 : : }
5026 : 126981 : }
5027 : :
5028 : :
5029 : : /* Like gfc_apply_interface_mapping_to_expr, but applied to
5030 : : constructor C. */
5031 : :
5032 : : static void
5033 : 47 : gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
5034 : : gfc_constructor_base base)
5035 : : {
5036 : 47 : gfc_constructor *c;
5037 : 428 : for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
5038 : : {
5039 : 381 : gfc_apply_interface_mapping_to_expr (mapping, c->expr);
5040 : 381 : if (c->iterator)
5041 : : {
5042 : 6 : gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
5043 : 6 : gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
5044 : 6 : gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
5045 : : }
5046 : : }
5047 : 47 : }
5048 : :
5049 : :
5050 : : /* Like gfc_apply_interface_mapping_to_expr, but applied to
5051 : : reference REF. */
5052 : :
5053 : : static void
5054 : 12445 : gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
5055 : : gfc_ref * ref)
5056 : : {
5057 : 12445 : int n;
5058 : :
5059 : 13888 : for (; ref; ref = ref->next)
5060 : 1443 : switch (ref->type)
5061 : : {
5062 : : case REF_ARRAY:
5063 : 2873 : for (n = 0; n < ref->u.ar.dimen; n++)
5064 : : {
5065 : 1632 : gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
5066 : 1632 : gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
5067 : 1632 : gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
5068 : : }
5069 : : break;
5070 : :
5071 : : case REF_COMPONENT:
5072 : : case REF_INQUIRY:
5073 : : break;
5074 : :
5075 : 43 : case REF_SUBSTRING:
5076 : 43 : gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
5077 : 43 : gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
5078 : 43 : break;
5079 : : }
5080 : 12445 : }
5081 : :
5082 : :
5083 : : /* Convert intrinsic function calls into result expressions. */
5084 : :
5085 : : static bool
5086 : 2184 : gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
5087 : : {
5088 : 2184 : gfc_symbol *sym;
5089 : 2184 : gfc_expr *new_expr;
5090 : 2184 : gfc_expr *arg1;
5091 : 2184 : gfc_expr *arg2;
5092 : 2184 : int d, dup;
5093 : :
5094 : 2184 : arg1 = expr->value.function.actual->expr;
5095 : 2184 : if (expr->value.function.actual->next)
5096 : 2063 : arg2 = expr->value.function.actual->next->expr;
5097 : : else
5098 : : arg2 = NULL;
5099 : :
5100 : 2184 : sym = arg1->symtree->n.sym;
5101 : :
5102 : 2184 : if (sym->attr.dummy)
5103 : : return false;
5104 : :
5105 : 2160 : new_expr = NULL;
5106 : :
5107 : 2160 : switch (expr->value.function.isym->id)
5108 : : {
5109 : 929 : case GFC_ISYM_LEN:
5110 : : /* TODO figure out why this condition is necessary. */
5111 : 929 : if (sym->attr.function
5112 : 43 : && (arg1->ts.u.cl->length == NULL
5113 : 42 : || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
5114 : 42 : && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
5115 : : return false;
5116 : :
5117 : 886 : new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
5118 : 886 : break;
5119 : :
5120 : 228 : case GFC_ISYM_LEN_TRIM:
5121 : 228 : new_expr = gfc_copy_expr (arg1);
5122 : 228 : gfc_apply_interface_mapping_to_expr (mapping, new_expr);
5123 : :
5124 : 228 : if (!new_expr)
5125 : : return false;
5126 : :
5127 : 228 : gfc_replace_expr (arg1, new_expr);
5128 : 228 : return true;
5129 : :
5130 : 588 : case GFC_ISYM_SIZE:
5131 : 588 : if (!sym->as || sym->as->rank == 0)
5132 : : return false;
5133 : :
5134 : 530 : if (arg2 && arg2->expr_type == EXPR_CONSTANT)
5135 : : {
5136 : 360 : dup = mpz_get_si (arg2->value.integer);
5137 : 360 : d = dup - 1;
5138 : : }
5139 : : else
5140 : : {
5141 : 530 : dup = sym->as->rank;
5142 : 530 : d = 0;
5143 : : }
5144 : :
5145 : 542 : for (; d < dup; d++)
5146 : : {
5147 : 530 : gfc_expr *tmp;
5148 : :
5149 : 530 : if (!sym->as->upper[d] || !sym->as->lower[d])
5150 : : {
5151 : 518 : gfc_free_expr (new_expr);
5152 : 518 : return false;
5153 : : }
5154 : :
5155 : 12 : tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
5156 : : gfc_get_int_expr (gfc_default_integer_kind,
5157 : : NULL, 1));
5158 : 12 : tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
5159 : 12 : if (new_expr)
5160 : 0 : new_expr = gfc_multiply (new_expr, tmp);
5161 : : else
5162 : : new_expr = tmp;
5163 : : }
5164 : : break;
5165 : :
5166 : 44 : case GFC_ISYM_LBOUND:
5167 : 44 : case GFC_ISYM_UBOUND:
5168 : : /* TODO These implementations of lbound and ubound do not limit if
5169 : : the size < 0, according to F95's 13.14.53 and 13.14.113. */
5170 : :
5171 : 44 : if (!sym->as || sym->as->rank == 0)
5172 : : return false;
5173 : :
5174 : 44 : if (arg2 && arg2->expr_type == EXPR_CONSTANT)
5175 : 38 : d = mpz_get_si (arg2->value.integer) - 1;
5176 : : else
5177 : : return false;
5178 : :
5179 : 38 : if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
5180 : : {
5181 : 23 : if (sym->as->lower[d])
5182 : 23 : new_expr = gfc_copy_expr (sym->as->lower[d]);
5183 : : }
5184 : : else
5185 : : {
5186 : 15 : if (sym->as->upper[d])
5187 : 9 : new_expr = gfc_copy_expr (sym->as->upper[d]);
5188 : : }
5189 : : break;
5190 : :
5191 : : default:
5192 : : break;
5193 : : }
5194 : :
5195 : 1307 : gfc_apply_interface_mapping_to_expr (mapping, new_expr);
5196 : 1307 : if (!new_expr)
5197 : : return false;
5198 : :
5199 : 113 : gfc_replace_expr (expr, new_expr);
5200 : 113 : return true;
5201 : : }
5202 : :
5203 : :
5204 : : static void
5205 : 24 : gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
5206 : : gfc_interface_mapping * mapping)
5207 : : {
5208 : 24 : gfc_formal_arglist *f;
5209 : 24 : gfc_actual_arglist *actual;
5210 : :
5211 : 24 : actual = expr->value.function.actual;
5212 : 24 : f = gfc_sym_get_dummy_args (map_expr->symtree->n.sym);
5213 : :
5214 : 72 : for (; f && actual; f = f->next, actual = actual->next)
5215 : : {
5216 : 24 : if (!actual->expr)
5217 : 0 : continue;
5218 : :
5219 : 24 : gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
5220 : : }
5221 : :
5222 : 24 : if (map_expr->symtree->n.sym->attr.dimension)
5223 : : {
5224 : 6 : int d;
5225 : 6 : gfc_array_spec *as;
5226 : :
5227 : 6 : as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
5228 : :
5229 : 18 : for (d = 0; d < as->rank; d++)
5230 : : {
5231 : 6 : gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
5232 : 6 : gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
5233 : : }
5234 : :
5235 : 6 : expr->value.function.esym->as = as;
5236 : : }
5237 : :
5238 : 24 : if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
5239 : : {
5240 : 0 : expr->value.function.esym->ts.u.cl->length
5241 : 0 : = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
5242 : :
5243 : 0 : gfc_apply_interface_mapping_to_expr (mapping,
5244 : 0 : expr->value.function.esym->ts.u.cl->length);
5245 : : }
5246 : 24 : }
5247 : :
5248 : :
5249 : : /* EXPR is a copy of an expression that appeared in the interface
5250 : : associated with MAPPING. Walk it recursively looking for references to
5251 : : dummy arguments that MAPPING maps to actual arguments. Replace each such
5252 : : reference with a reference to the associated actual argument. */
5253 : :
5254 : : static void
5255 : 20870 : gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
5256 : : gfc_expr * expr)
5257 : : {
5258 : 22423 : gfc_interface_sym_mapping *sym;
5259 : 22423 : gfc_actual_arglist *actual;
5260 : :
5261 : 22423 : if (!expr)
5262 : : return;
5263 : :
5264 : : /* Copying an expression does not copy its length, so do that here. */
5265 : 12445 : if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
5266 : : {
5267 : 1784 : expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
5268 : 1784 : gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
5269 : : }
5270 : :
5271 : : /* Apply the mapping to any references. */
5272 : 12445 : gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
5273 : :
5274 : : /* ...and to the expression's symbol, if it has one. */
5275 : : /* TODO Find out why the condition on expr->symtree had to be moved into
5276 : : the loop rather than being outside it, as originally. */
5277 : 29650 : for (sym = mapping->syms; sym; sym = sym->next)
5278 : 17205 : if (expr->symtree && !strcmp (sym->old->name, expr->symtree->n.sym->name))
5279 : : {
5280 : 3345 : if (sym->new_sym->n.sym->backend_decl)
5281 : 3301 : expr->symtree = sym->new_sym;
5282 : 44 : else if (sym->expr)
5283 : 44 : gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
5284 : : }
5285 : :
5286 : : /* ...and to subexpressions in expr->value. */
5287 : 12445 : switch (expr->expr_type)
5288 : : {
5289 : : case EXPR_VARIABLE:
5290 : : case EXPR_CONSTANT:
5291 : : case EXPR_NULL:
5292 : : case EXPR_SUBSTRING:
5293 : : break;
5294 : :
5295 : 1553 : case EXPR_OP:
5296 : 1553 : gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
5297 : 1553 : gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
5298 : 1553 : break;
5299 : :
5300 : 2927 : case EXPR_FUNCTION:
5301 : 9388 : for (actual = expr->value.function.actual; actual; actual = actual->next)
5302 : 6461 : gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
5303 : :
5304 : 2927 : if (expr->value.function.esym == NULL
5305 : 2614 : && expr->value.function.isym != NULL
5306 : 2602 : && expr->value.function.actual
5307 : 2601 : && expr->value.function.actual->expr
5308 : 2601 : && expr->value.function.actual->expr->symtree
5309 : 5111 : && gfc_map_intrinsic_function (expr, mapping))
5310 : : break;
5311 : :
5312 : 6094 : for (sym = mapping->syms; sym; sym = sym->next)
5313 : 3508 : if (sym->old == expr->value.function.esym)
5314 : : {
5315 : 24 : expr->value.function.esym = sym->new_sym->n.sym;
5316 : 24 : gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
5317 : 24 : expr->value.function.esym->result = sym->new_sym->n.sym;
5318 : : }
5319 : : break;
5320 : :
5321 : 47 : case EXPR_ARRAY:
5322 : 47 : case EXPR_STRUCTURE:
5323 : 47 : gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
5324 : 47 : break;
5325 : :
5326 : 0 : case EXPR_COMPCALL:
5327 : 0 : case EXPR_PPC:
5328 : 0 : case EXPR_UNKNOWN:
5329 : 0 : gcc_unreachable ();
5330 : : break;
5331 : : }
5332 : :
5333 : : return;
5334 : : }
5335 : :
5336 : :
5337 : : /* Evaluate interface expression EXPR using MAPPING. Store the result
5338 : : in SE. */
5339 : :
5340 : : void
5341 : 3930 : gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
5342 : : gfc_se * se, gfc_expr * expr)
5343 : : {
5344 : 3930 : expr = gfc_copy_expr (expr);
5345 : 3930 : gfc_apply_interface_mapping_to_expr (mapping, expr);
5346 : 3930 : gfc_conv_expr (se, expr);
5347 : 3930 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
5348 : 3930 : gfc_free_expr (expr);
5349 : 3930 : }
5350 : :
5351 : :
5352 : : /* Returns a reference to a temporary array into which a component of
5353 : : an actual argument derived type array is copied and then returned
5354 : : after the function call. */
5355 : : void
5356 : 2380 : gfc_conv_subref_array_arg (gfc_se *se, gfc_expr * expr, int g77,
5357 : : sym_intent intent, bool formal_ptr,
5358 : : const gfc_symbol *fsym, const char *proc_name,
5359 : : gfc_symbol *sym, bool check_contiguous)
5360 : : {
5361 : 2380 : gfc_se lse;
5362 : 2380 : gfc_se rse;
5363 : 2380 : gfc_ss *lss;
5364 : 2380 : gfc_ss *rss;
5365 : 2380 : gfc_loopinfo loop;
5366 : 2380 : gfc_loopinfo loop2;
5367 : 2380 : gfc_array_info *info;
5368 : 2380 : tree offset;
5369 : 2380 : tree tmp_index;
5370 : 2380 : tree tmp;
5371 : 2380 : tree base_type;
5372 : 2380 : tree size;
5373 : 2380 : stmtblock_t body;
5374 : 2380 : int n;
5375 : 2380 : int dimen;
5376 : 2380 : gfc_se work_se;
5377 : 2380 : gfc_se *parmse;
5378 : 2380 : bool pass_optional;
5379 : 2380 : bool readonly;
5380 : :
5381 : 2380 : pass_optional = fsym && fsym->attr.optional && sym && sym->attr.optional;
5382 : :
5383 : 2369 : if (pass_optional || check_contiguous)
5384 : : {
5385 : 1349 : gfc_init_se (&work_se, NULL);
5386 : 1349 : parmse = &work_se;
5387 : : }
5388 : : else
5389 : : parmse = se;
5390 : :
5391 : 2380 : if (gfc_option.rtcheck & GFC_RTCHECK_ARRAY_TEMPS)
5392 : : {
5393 : : /* We will create a temporary array, so let us warn. */
5394 : 868 : char * msg;
5395 : :
5396 : 868 : if (fsym && proc_name)
5397 : 868 : msg = xasprintf ("An array temporary was created for argument "
5398 : 868 : "'%s' of procedure '%s'", fsym->name, proc_name);
5399 : : else
5400 : 0 : msg = xasprintf ("An array temporary was created");
5401 : :
5402 : 868 : tmp = build_int_cst (logical_type_node, 1);
5403 : 868 : gfc_trans_runtime_check (false, true, tmp, &parmse->pre,
5404 : : &expr->where, msg);
5405 : 868 : free (msg);
5406 : : }
5407 : :
5408 : 2380 : gfc_init_se (&lse, NULL);
5409 : 2380 : gfc_init_se (&rse, NULL);
5410 : :
5411 : : /* Walk the argument expression. */
5412 : 2380 : rss = gfc_walk_expr (expr);
5413 : :
5414 : 2380 : gcc_assert (rss != gfc_ss_terminator);
5415 : :
5416 : : /* Initialize the scalarizer. */
5417 : 2380 : gfc_init_loopinfo (&loop);
5418 : 2380 : gfc_add_ss_to_loop (&loop, rss);
5419 : :
5420 : : /* Calculate the bounds of the scalarization. */
5421 : 2380 : gfc_conv_ss_startstride (&loop);
5422 : :
5423 : : /* Build an ss for the temporary. */
5424 : 2380 : if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
5425 : 136 : gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
5426 : :
5427 : 2380 : base_type = gfc_typenode_for_spec (&expr->ts);
5428 : 2380 : if (GFC_ARRAY_TYPE_P (base_type)
5429 : 2380 : || GFC_DESCRIPTOR_TYPE_P (base_type))
5430 : 0 : base_type = gfc_get_element_type (base_type);
5431 : :
5432 : 2380 : if (expr->ts.type == BT_CLASS)
5433 : 121 : base_type = gfc_typenode_for_spec (&CLASS_DATA (expr)->ts);
5434 : :
5435 : 3538 : loop.temp_ss = gfc_get_temp_ss (base_type, ((expr->ts.type == BT_CHARACTER)
5436 : 1158 : ? expr->ts.u.cl->backend_decl
5437 : : : NULL),
5438 : : loop.dimen);
5439 : :
5440 : 2380 : parmse->string_length = loop.temp_ss->info->string_length;
5441 : :
5442 : : /* Associate the SS with the loop. */
5443 : 2380 : gfc_add_ss_to_loop (&loop, loop.temp_ss);
5444 : :
5445 : : /* Setup the scalarizing loops. */
5446 : 2380 : gfc_conv_loop_setup (&loop, &expr->where);
5447 : :
5448 : : /* Pass the temporary descriptor back to the caller. */
5449 : 2380 : info = &loop.temp_ss->info->data.array;
5450 : 2380 : parmse->expr = info->descriptor;
5451 : :
5452 : : /* Setup the gfc_se structures. */
5453 : 2380 : gfc_copy_loopinfo_to_se (&lse, &loop);
5454 : 2380 : gfc_copy_loopinfo_to_se (&rse, &loop);
5455 : :
5456 : 2380 : rse.ss = rss;
5457 : 2380 : lse.ss = loop.temp_ss;
5458 : 2380 : gfc_mark_ss_chain_used (rss, 1);
5459 : 2380 : gfc_mark_ss_chain_used (loop.temp_ss, 1);
5460 : :
5461 : : /* Start the scalarized loop body. */
5462 : 2380 : gfc_start_scalarized_body (&loop, &body);
5463 : :
5464 : : /* Translate the expression. */
5465 : 2380 : gfc_conv_expr (&rse, expr);
5466 : :
5467 : : /* Reset the offset for the function call since the loop
5468 : : is zero based on the data pointer. Note that the temp
5469 : : comes first in the loop chain since it is added second. */
5470 : 2380 : if (gfc_is_class_array_function (expr))
5471 : : {
5472 : 13 : tmp = loop.ss->loop_chain->info->data.array.descriptor;
5473 : 13 : gfc_conv_descriptor_offset_set (&loop.pre, tmp,
5474 : : gfc_index_zero_node);
5475 : : }
5476 : :
5477 : 2380 : gfc_conv_tmp_array_ref (&lse);
5478 : :
5479 : 2380 : if (intent != INTENT_OUT)
5480 : : {
5481 : 2342 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false);
5482 : 2342 : gfc_add_expr_to_block (&body, tmp);
5483 : 2342 : gcc_assert (rse.ss == gfc_ss_terminator);
5484 : 2342 : gfc_trans_scalarizing_loops (&loop, &body);
5485 : : }
5486 : : else
5487 : : {
5488 : : /* Make sure that the temporary declaration survives by merging
5489 : : all the loop declarations into the current context. */
5490 : 85 : for (n = 0; n < loop.dimen; n++)
5491 : : {
5492 : 47 : gfc_merge_block_scope (&body);
5493 : 47 : body = loop.code[loop.order[n]];
5494 : : }
5495 : 38 : gfc_merge_block_scope (&body);
5496 : : }
5497 : :
5498 : : /* Add the post block after the second loop, so that any
5499 : : freeing of allocated memory is done at the right time. */
5500 : 2380 : gfc_add_block_to_block (&parmse->pre, &loop.pre);
5501 : :
5502 : : /**********Copy the temporary back again.*********/
5503 : :
5504 : 2380 : gfc_init_se (&lse, NULL);
5505 : 2380 : gfc_init_se (&rse, NULL);
5506 : :
5507 : : /* Walk the argument expression. */
5508 : 2380 : lss = gfc_walk_expr (expr);
5509 : 2380 : rse.ss = loop.temp_ss;
5510 : 2380 : lse.ss = lss;
5511 : :
5512 : : /* Initialize the scalarizer. */
5513 : 2380 : gfc_init_loopinfo (&loop2);
5514 : 2380 : gfc_add_ss_to_loop (&loop2, lss);
5515 : :
5516 : 2380 : dimen = rse.ss->dimen;
5517 : :
5518 : : /* Skip the write-out loop for this case. */
5519 : 2380 : if (gfc_is_class_array_function (expr))
5520 : 13 : goto class_array_fcn;
5521 : :
5522 : : /* Calculate the bounds of the scalarization. */
5523 : 2367 : gfc_conv_ss_startstride (&loop2);
5524 : :
5525 : : /* Setup the scalarizing loops. */
5526 : 2367 : gfc_conv_loop_setup (&loop2, &expr->where);
5527 : :
5528 : 2367 : gfc_copy_loopinfo_to_se (&lse, &loop2);
5529 : 2367 : gfc_copy_loopinfo_to_se (&rse, &loop2);
5530 : :
5531 : 2367 : gfc_mark_ss_chain_used (lss, 1);
5532 : 2367 : gfc_mark_ss_chain_used (loop.temp_ss, 1);
5533 : :
5534 : : /* Declare the variable to hold the temporary offset and start the
5535 : : scalarized loop body. */
5536 : 2367 : offset = gfc_create_var (gfc_array_index_type, NULL);
5537 : 2367 : gfc_start_scalarized_body (&loop2, &body);
5538 : :
5539 : : /* Build the offsets for the temporary from the loop variables. The
5540 : : temporary array has lbounds of zero and strides of one in all
5541 : : dimensions, so this is very simple. The offset is only computed
5542 : : outside the innermost loop, so the overall transfer could be
5543 : : optimized further. */
5544 : 2367 : info = &rse.ss->info->data.array;
5545 : :
5546 : 2367 : tmp_index = gfc_index_zero_node;
5547 : 3711 : for (n = dimen - 1; n > 0; n--)
5548 : : {
5549 : 1344 : tree tmp_str;
5550 : 1344 : tmp = rse.loop->loopvar[n];
5551 : 1344 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
5552 : : tmp, rse.loop->from[n]);
5553 : 1344 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5554 : : tmp, tmp_index);
5555 : :
5556 : 2688 : tmp_str = fold_build2_loc (input_location, MINUS_EXPR,
5557 : : gfc_array_index_type,
5558 : 1344 : rse.loop->to[n-1], rse.loop->from[n-1]);
5559 : 1344 : tmp_str = fold_build2_loc (input_location, PLUS_EXPR,
5560 : : gfc_array_index_type,
5561 : : tmp_str, gfc_index_one_node);
5562 : :
5563 : 1344 : tmp_index = fold_build2_loc (input_location, MULT_EXPR,
5564 : : gfc_array_index_type, tmp, tmp_str);
5565 : : }
5566 : :
5567 : 4734 : tmp_index = fold_build2_loc (input_location, MINUS_EXPR,
5568 : : gfc_array_index_type,
5569 : 2367 : tmp_index, rse.loop->from[0]);
5570 : 2367 : gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
5571 : :
5572 : 4734 : tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
5573 : : gfc_array_index_type,
5574 : 2367 : rse.loop->loopvar[0], offset);
5575 : :
5576 : : /* Now use the offset for the reference. */
5577 : 2367 : tmp = build_fold_indirect_ref_loc (input_location,
5578 : : info->data);
5579 : 2367 : rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
5580 : :
5581 : 2367 : if (expr->ts.type == BT_CHARACTER)
5582 : 1158 : rse.string_length = expr->ts.u.cl->backend_decl;
5583 : :
5584 : 2367 : gfc_conv_expr (&lse, expr);
5585 : :
5586 : 2367 : gcc_assert (lse.ss == gfc_ss_terminator);
5587 : :
5588 : 2367 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, true);
5589 : 2367 : gfc_add_expr_to_block (&body, tmp);
5590 : :
5591 : : /* Generate the copying loops. */
5592 : 2367 : gfc_trans_scalarizing_loops (&loop2, &body);
5593 : :
5594 : : /* Wrap the whole thing up by adding the second loop to the post-block
5595 : : and following it by the post-block of the first loop. In this way,
5596 : : if the temporary needs freeing, it is done after use!
5597 : : If input expr is read-only, e.g. a PARAMETER array, copying back
5598 : : modified values is undefined behavior. */
5599 : 4734 : readonly = (expr->expr_type == EXPR_VARIABLE
5600 : 2313 : && expr->symtree
5601 : 4680 : && expr->symtree->n.sym->attr.flavor == FL_PARAMETER);
5602 : :
5603 : 2367 : if ((intent != INTENT_IN) && !readonly)
5604 : : {
5605 : 1138 : gfc_add_block_to_block (&parmse->post, &loop2.pre);
5606 : 1138 : gfc_add_block_to_block (&parmse->post, &loop2.post);
5607 : : }
5608 : :
5609 : 1229 : class_array_fcn:
5610 : :
5611 : 2380 : gfc_add_block_to_block (&parmse->post, &loop.post);
5612 : :
5613 : 2380 : gfc_cleanup_loop (&loop);
5614 : 2380 : gfc_cleanup_loop (&loop2);
5615 : :
5616 : : /* Pass the string length to the argument expression. */
5617 : 2380 : if (expr->ts.type == BT_CHARACTER)
5618 : 1158 : parmse->string_length = expr->ts.u.cl->backend_decl;
5619 : :
5620 : : /* Determine the offset for pointer formal arguments and set the
5621 : : lbounds to one. */
5622 : 2380 : if (formal_ptr)
5623 : : {
5624 : 0 : size = gfc_index_one_node;
5625 : 0 : offset = gfc_index_zero_node;
5626 : 0 : for (n = 0; n < dimen; n++)
5627 : : {
5628 : 0 : tmp = gfc_conv_descriptor_ubound_get (parmse->expr,
5629 : : gfc_rank_cst[n]);
5630 : 0 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
5631 : : gfc_array_index_type, tmp,
5632 : : gfc_index_one_node);
5633 : 0 : gfc_conv_descriptor_ubound_set (&parmse->pre,
5634 : : parmse->expr,
5635 : : gfc_rank_cst[n],
5636 : : tmp);
5637 : 0 : gfc_conv_descriptor_lbound_set (&parmse->pre,
5638 : : parmse->expr,
5639 : : gfc_rank_cst[n],
5640 : : gfc_index_one_node);
5641 : 0 : size = gfc_evaluate_now (size, &parmse->pre);
5642 : 0 : offset = fold_build2_loc (input_location, MINUS_EXPR,
5643 : : gfc_array_index_type,
5644 : : offset, size);
5645 : 0 : offset = gfc_evaluate_now (offset, &parmse->pre);
5646 : 0 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
5647 : : gfc_array_index_type,
5648 : 0 : rse.loop->to[n], rse.loop->from[n]);
5649 : 0 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
5650 : : gfc_array_index_type,
5651 : : tmp, gfc_index_one_node);
5652 : 0 : size = fold_build2_loc (input_location, MULT_EXPR,
5653 : : gfc_array_index_type, size, tmp);
5654 : : }
5655 : :
5656 : 0 : gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
5657 : : offset);
5658 : : }
5659 : :
5660 : : /* We want either the address for the data or the address of the descriptor,
5661 : : depending on the mode of passing array arguments. */
5662 : 2380 : if (g77)
5663 : 433 : parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
5664 : : else
5665 : 1947 : parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
5666 : :
5667 : : /* Basically make this into
5668 : :
5669 : : if (present)
5670 : : {
5671 : : if (contiguous)
5672 : : {
5673 : : pointer = a;
5674 : : }
5675 : : else
5676 : : {
5677 : : parmse->pre();
5678 : : pointer = parmse->expr;
5679 : : }
5680 : : }
5681 : : else
5682 : : pointer = NULL;
5683 : :
5684 : : foo (pointer);
5685 : : if (present && !contiguous)
5686 : : se->post();
5687 : :
5688 : : */
5689 : :
5690 : 2380 : if (pass_optional || check_contiguous)
5691 : : {
5692 : 1349 : tree type;
5693 : 1349 : stmtblock_t else_block;
5694 : 1349 : tree pre_stmts, post_stmts;
5695 : 1349 : tree pointer;
5696 : 1349 : tree else_stmt;
5697 : 1349 : tree present_var = NULL_TREE;
5698 : 1349 : tree cont_var = NULL_TREE;
5699 : 1349 : tree post_cond;
5700 : :
5701 : 1349 : type = TREE_TYPE (parmse->expr);
5702 : 1349 : if (POINTER_TYPE_P (type) && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
5703 : 1021 : type = TREE_TYPE (type);
5704 : 1349 : pointer = gfc_create_var (type, "arg_ptr");
5705 : :
5706 : 1349 : if (check_contiguous)
5707 : : {
5708 : 1349 : gfc_se cont_se, array_se;
5709 : 1349 : stmtblock_t if_block, else_block;
5710 : 1349 : tree if_stmt, else_stmt;
5711 : 1349 : mpz_t size;
5712 : 1349 : bool size_set;
5713 : :
5714 : 1349 : cont_var = gfc_create_var (boolean_type_node, "contiguous");
5715 : :
5716 : : /* If the size is known to be one at compile-time, set
5717 : : cont_var to true unconditionally. This may look
5718 : : inelegant, but we're only doing this during
5719 : : optimization, so the statements will be optimized away,
5720 : : and this saves complexity here. */
5721 : :
5722 : 1349 : size_set = gfc_array_size (expr, &size);
5723 : 1349 : if (size_set && mpz_cmp_ui (size, 1) == 0)
5724 : : {
5725 : 6 : gfc_add_modify (&se->pre, cont_var,
5726 : : build_one_cst (boolean_type_node));
5727 : : }
5728 : : else
5729 : : {
5730 : : /* cont_var = is_contiguous (expr); . */
5731 : 1343 : gfc_init_se (&cont_se, parmse);
5732 : 1343 : gfc_conv_is_contiguous_expr (&cont_se, expr);
5733 : 1343 : gfc_add_block_to_block (&se->pre, &(&cont_se)->pre);
5734 : 1343 : gfc_add_modify (&se->pre, cont_var, cont_se.expr);
5735 : 1343 : gfc_add_block_to_block (&se->pre, &(&cont_se)->post);
5736 : : }
5737 : :
5738 : 1349 : if (size_set)
5739 : 1145 : mpz_clear (size);
5740 : :
5741 : : /* arrayse->expr = descriptor of a. */
5742 : 1349 : gfc_init_se (&array_se, se);
5743 : 1349 : gfc_conv_expr_descriptor (&array_se, expr);
5744 : 1349 : gfc_add_block_to_block (&se->pre, &(&array_se)->pre);
5745 : 1349 : gfc_add_block_to_block (&se->pre, &(&array_se)->post);
5746 : :
5747 : : /* if_stmt = { descriptor ? pointer = a : pointer = &a[0]; } . */
5748 : 1349 : gfc_init_block (&if_block);
5749 : 1349 : if (GFC_DESCRIPTOR_TYPE_P (type))
5750 : 1021 : gfc_add_modify (&if_block, pointer, array_se.expr);
5751 : : else
5752 : : {
5753 : 328 : tmp = gfc_conv_array_data (array_se.expr);
5754 : 328 : tmp = fold_convert (type, tmp);
5755 : 328 : gfc_add_modify (&if_block, pointer, tmp);
5756 : : }
5757 : 1349 : if_stmt = gfc_finish_block (&if_block);
5758 : :
5759 : : /* else_stmt = { parmse->pre(); pointer = parmse->expr; } . */
5760 : 1349 : gfc_init_block (&else_block);
5761 : 1349 : gfc_add_block_to_block (&else_block, &parmse->pre);
5762 : 1677 : tmp = (GFC_DESCRIPTOR_TYPE_P (type)
5763 : 1349 : ? build_fold_indirect_ref_loc (input_location, parmse->expr)
5764 : : : parmse->expr);
5765 : 1349 : gfc_add_modify (&else_block, pointer, tmp);
5766 : 1349 : else_stmt = gfc_finish_block (&else_block);
5767 : :
5768 : : /* And put the above into an if statement. */
5769 : 1349 : pre_stmts = fold_build3_loc (input_location, COND_EXPR, void_type_node,
5770 : : gfc_likely (cont_var,
5771 : : PRED_FORTRAN_CONTIGUOUS),
5772 : : if_stmt, else_stmt);
5773 : : }
5774 : : else
5775 : : {
5776 : : /* pointer = pramse->expr; . */
5777 : 0 : gfc_add_modify (&parmse->pre, pointer, parmse->expr);
5778 : 0 : pre_stmts = gfc_finish_block (&parmse->pre);
5779 : : }
5780 : :
5781 : 1349 : if (pass_optional)
5782 : : {
5783 : 11 : present_var = gfc_create_var (boolean_type_node, "present");
5784 : :
5785 : : /* present_var = present(sym); . */
5786 : 11 : tmp = gfc_conv_expr_present (sym);
5787 : 11 : tmp = fold_convert (boolean_type_node, tmp);
5788 : 11 : gfc_add_modify (&se->pre, present_var, tmp);
5789 : :
5790 : : /* else_stmt = { pointer = NULL; } . */
5791 : 11 : gfc_init_block (&else_block);
5792 : 11 : if (GFC_DESCRIPTOR_TYPE_P (type))
5793 : 0 : gfc_conv_descriptor_data_set (&else_block, pointer,
5794 : : null_pointer_node);
5795 : : else
5796 : 11 : gfc_add_modify (&else_block, pointer, build_int_cst (type, 0));
5797 : 11 : else_stmt = gfc_finish_block (&else_block);
5798 : :
5799 : 11 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
5800 : : gfc_likely (present_var,
5801 : : PRED_FORTRAN_ABSENT_DUMMY),
5802 : : pre_stmts, else_stmt);
5803 : 11 : gfc_add_expr_to_block (&se->pre, tmp);
5804 : : }
5805 : : else
5806 : 1338 : gfc_add_expr_to_block (&se->pre, pre_stmts);
5807 : :
5808 : 1349 : post_stmts = gfc_finish_block (&parmse->post);
5809 : :
5810 : : /* Put together the post stuff, plus the optional
5811 : : deallocation. */
5812 : 1349 : if (check_contiguous)
5813 : : {
5814 : : /* !cont_var. */
5815 : 1349 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
5816 : : cont_var,
5817 : : build_zero_cst (boolean_type_node));
5818 : 1349 : tmp = gfc_unlikely (tmp, PRED_FORTRAN_CONTIGUOUS);
5819 : :
5820 : 1349 : if (pass_optional)
5821 : : {
5822 : 11 : tree present_likely = gfc_likely (present_var,
5823 : : PRED_FORTRAN_ABSENT_DUMMY);
5824 : 11 : post_cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
5825 : : boolean_type_node, present_likely,
5826 : : tmp);
5827 : : }
5828 : : else
5829 : : post_cond = tmp;
5830 : : }
5831 : : else
5832 : : {
5833 : 0 : gcc_assert (pass_optional);
5834 : : post_cond = present_var;
5835 : : }
5836 : :
5837 : 1349 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, post_cond,
5838 : : post_stmts, build_empty_stmt (input_location));
5839 : 1349 : gfc_add_expr_to_block (&se->post, tmp);
5840 : 1349 : if (GFC_DESCRIPTOR_TYPE_P (type))
5841 : : {
5842 : 1021 : type = TREE_TYPE (parmse->expr);
5843 : 1021 : if (POINTER_TYPE_P (type))
5844 : : {
5845 : 1021 : pointer = gfc_build_addr_expr (type, pointer);
5846 : 1021 : if (pass_optional)
5847 : : {
5848 : 0 : tmp = gfc_likely (present_var, PRED_FORTRAN_ABSENT_DUMMY);
5849 : 0 : pointer = fold_build3_loc (input_location, COND_EXPR, type,
5850 : : tmp, pointer,
5851 : : fold_convert (type,
5852 : : null_pointer_node));
5853 : : }
5854 : : }
5855 : : else
5856 : 0 : gcc_assert (!pass_optional);
5857 : : }
5858 : 1349 : se->expr = pointer;
5859 : : }
5860 : :
5861 : 2380 : return;
5862 : : }
5863 : :
5864 : :
5865 : : /* Generate the code for argument list functions. */
5866 : :
5867 : : static void
5868 : 5822 : conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
5869 : : {
5870 : : /* Pass by value for g77 %VAL(arg), pass the address
5871 : : indirectly for %LOC, else by reference. Thus %REF
5872 : : is a "do-nothing" and %LOC is the same as an F95
5873 : : pointer. */
5874 : 5822 : if (strcmp (name, "%VAL") == 0)
5875 : 5810 : gfc_conv_expr (se, expr);
5876 : 12 : else if (strcmp (name, "%LOC") == 0)
5877 : : {
5878 : 6 : gfc_conv_expr_reference (se, expr);
5879 : 6 : se->expr = gfc_build_addr_expr (NULL, se->expr);
5880 : : }
5881 : 6 : else if (strcmp (name, "%REF") == 0)
5882 : 6 : gfc_conv_expr_reference (se, expr);
5883 : : else
5884 : 0 : gfc_error ("Unknown argument list function at %L", &expr->where);
5885 : 5822 : }
5886 : :
5887 : :
5888 : : /* This function tells whether the middle-end representation of the expression
5889 : : E given as input may point to data otherwise accessible through a variable
5890 : : (sub-)reference.
5891 : : It is assumed that the only expressions that may alias are variables,
5892 : : and array constructors if ARRAY_MAY_ALIAS is true and some of its elements
5893 : : may alias.
5894 : : This function is used to decide whether freeing an expression's allocatable
5895 : : components is safe or should be avoided.
5896 : :
5897 : : If ARRAY_MAY_ALIAS is true, an array constructor may alias if some of
5898 : : its elements are copied from a variable. This ARRAY_MAY_ALIAS trick
5899 : : is necessary because for array constructors, aliasing depends on how
5900 : : the array is used:
5901 : : - If E is an array constructor used as argument to an elemental procedure,
5902 : : the array, which is generated through shallow copy by the scalarizer,
5903 : : is used directly and can alias the expressions it was copied from.
5904 : : - If E is an array constructor used as argument to a non-elemental
5905 : : procedure,the scalarizer is used in gfc_conv_expr_descriptor to generate
5906 : : the array as in the previous case, but then that array is used
5907 : : to initialize a new descriptor through deep copy. There is no alias
5908 : : possible in that case.
5909 : : Thus, the ARRAY_MAY_ALIAS flag is necessary to distinguish the two cases
5910 : : above. */
5911 : :
5912 : : static bool
5913 : 7350 : expr_may_alias_variables (gfc_expr *e, bool array_may_alias)
5914 : : {
5915 : 7350 : gfc_constructor *c;
5916 : :
5917 : 7350 : if (e->expr_type == EXPR_VARIABLE)
5918 : : return true;
5919 : 398 : else if (e->expr_type == EXPR_FUNCTION)
5920 : : {
5921 : 160 : gfc_symbol *proc_ifc = gfc_get_proc_ifc_for_expr (e);
5922 : :
5923 : 160 : if (proc_ifc->result != NULL
5924 : 160 : && ((proc_ifc->result->ts.type == BT_CLASS
5925 : 25 : && proc_ifc->result->ts.u.derived->attr.is_class
5926 : 25 : && CLASS_DATA (proc_ifc->result)->attr.class_pointer)
5927 : 160 : || proc_ifc->result->attr.pointer))
5928 : : return true;
5929 : : else
5930 : : return false;
5931 : : }
5932 : 238 : else if (e->expr_type != EXPR_ARRAY || !array_may_alias)
5933 : : return false;
5934 : :
5935 : 67 : for (c = gfc_constructor_first (e->value.constructor);
5936 : 101 : c; c = gfc_constructor_next (c))
5937 : 69 : if (c->expr
5938 : 69 : && expr_may_alias_variables (c->expr, array_may_alias))
5939 : : return true;
5940 : :
5941 : : return false;
5942 : : }
5943 : :
5944 : :
5945 : : /* A helper function to set the dtype for unallocated or unassociated
5946 : : entities. */
5947 : :
5948 : : static void
5949 : 891 : set_dtype_for_unallocated (gfc_se *parmse, gfc_expr *e)
5950 : : {
5951 : 891 : tree tmp;
5952 : 891 : tree desc;
5953 : 891 : tree cond;
5954 : 891 : tree type;
5955 : 891 : stmtblock_t block;
5956 : :
5957 : : /* TODO Figure out how to handle optional dummies. */
5958 : 891 : if (e && e->expr_type == EXPR_VARIABLE
5959 : 807 : && e->symtree->n.sym->attr.optional)
5960 : 108 : return;
5961 : :
5962 : 819 : desc = parmse->expr;
5963 : 819 : if (desc == NULL_TREE)
5964 : : return;
5965 : :
5966 : 819 : if (POINTER_TYPE_P (TREE_TYPE (desc)))
5967 : 819 : desc = build_fold_indirect_ref_loc (input_location, desc);
5968 : 819 : if (GFC_CLASS_TYPE_P (TREE_TYPE (desc)))
5969 : 192 : desc = gfc_class_data_get (desc);
5970 : 819 : if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
5971 : : return;
5972 : :
5973 : 783 : gfc_init_block (&block);
5974 : 783 : tmp = gfc_conv_descriptor_data_get (desc);
5975 : 783 : cond = fold_build2_loc (input_location, EQ_EXPR,
5976 : : logical_type_node, tmp,
5977 : 783 : build_int_cst (TREE_TYPE (tmp), 0));
5978 : 783 : tmp = gfc_conv_descriptor_dtype (desc);
5979 : 783 : type = gfc_get_element_type (TREE_TYPE (desc));
5980 : 1566 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
5981 : 783 : TREE_TYPE (tmp), tmp,
5982 : : gfc_get_dtype_rank_type (e->rank, type));
5983 : 783 : gfc_add_expr_to_block (&block, tmp);
5984 : 783 : cond = build3_v (COND_EXPR, cond,
5985 : : gfc_finish_block (&block),
5986 : : build_empty_stmt (input_location));
5987 : 783 : gfc_add_expr_to_block (&parmse->pre, cond);
5988 : : }
5989 : :
5990 : :
5991 : :
5992 : : /* Provide an interface between gfortran array descriptors and the F2018:18.4
5993 : : ISO_Fortran_binding array descriptors. */
5994 : :
5995 : : static void
5996 : 6536 : gfc_conv_gfc_desc_to_cfi_desc (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym)
5997 : : {
5998 : 6536 : stmtblock_t block, block2;
5999 : 6536 : tree cfi, gfc, tmp, tmp2;
6000 : 6536 : tree present = NULL;
6001 : 6536 : tree gfc_strlen = NULL;
6002 : 6536 : tree rank;
6003 : 6536 : gfc_se se;
6004 : :
6005 : 6536 : if (fsym->attr.optional
6006 : 1094 : && e->expr_type == EXPR_VARIABLE
6007 : 1094 : && e->symtree->n.sym->attr.optional)
6008 : 103 : present = gfc_conv_expr_present (e->symtree->n.sym);
6009 : :
6010 : 6536 : gfc_init_block (&block);
6011 : :
6012 : : /* Convert original argument to a tree. */
6013 : 6536 : gfc_init_se (&se, NULL);
6014 : 6536 : if (e->rank == 0)
6015 : : {
6016 : 686 : se.want_pointer = 1;
6017 : 686 : gfc_conv_expr (&se, e);
6018 : 686 : gfc = se.expr;
6019 : : /* gfc_conv_constant ignores se.want_poiner, e.g. for string_cst. */
6020 : 686 : if (!POINTER_TYPE_P (TREE_TYPE (gfc)))
6021 : 20 : gfc = gfc_build_addr_expr (NULL, gfc);
6022 : : }
6023 : : else
6024 : : {
6025 : : /* If the actual argument can be noncontiguous, copy-in/out is required,
6026 : : if the dummy has either the CONTIGUOUS attribute or is an assumed-
6027 : : length assumed-length/assumed-size CHARACTER array. This only
6028 : : applies if the actual argument is a "variable"; if it's some
6029 : : non-lvalue expression, we are going to evaluate it to a
6030 : : temporary below anyway. */
6031 : 5850 : se.force_no_tmp = 1;
6032 : 5850 : if ((fsym->attr.contiguous
6033 : 4769 : || (fsym->ts.type == BT_CHARACTER && !fsym->ts.u.cl->length
6034 : 1375 : && (fsym->as->type == AS_ASSUMED_SIZE
6035 : 937 : || fsym->as->type == AS_EXPLICIT)))
6036 : 2023 : && !gfc_is_simply_contiguous (e, false, true)
6037 : 6877 : && gfc_expr_is_variable (e))
6038 : : {
6039 : 1021 : bool optional = fsym->attr.optional;
6040 : 1021 : fsym->attr.optional = 0;
6041 : 1021 : gfc_conv_subref_array_arg (&se, e, false, fsym->attr.intent,
6042 : 1021 : fsym->attr.pointer, fsym,
6043 : 1021 : fsym->ns->proc_name->name, NULL,
6044 : : /* check_contiguous= */ true);
6045 : 1021 : fsym->attr.optional = optional;
6046 : : }
6047 : : else
6048 : 4829 : gfc_conv_expr_descriptor (&se, e);
6049 : 5850 : gfc = se.expr;
6050 : : /* For dt(:)%var the elem_len*stride != sm, hence, GFC uses
6051 : : elem_len = sizeof(dt) and base_addr = dt(lb) instead.
6052 : : gfc_get_dataptr_offset fixes the base_addr; for elem_len, see below.
6053 : : While sm is fine as it uses span*stride and not elem_len. */
6054 : 5850 : if (POINTER_TYPE_P (TREE_TYPE (gfc)))
6055 : 1021 : gfc = build_fold_indirect_ref_loc (input_location, gfc);
6056 : 4829 : else if (is_subref_array (e) && e->ts.type != BT_CHARACTER)
6057 : 12 : gfc_get_dataptr_offset (&se.pre, gfc, gfc, NULL, true, e);
6058 : : }
6059 : 6536 : if (e->ts.type == BT_CHARACTER)
6060 : : {
6061 : 3409 : if (se.string_length)
6062 : : gfc_strlen = se.string_length;
6063 : 883 : else if (e->ts.u.cl->backend_decl)
6064 : : gfc_strlen = e->ts.u.cl->backend_decl;
6065 : : else
6066 : 0 : gcc_unreachable ();
6067 : : }
6068 : 6536 : gfc_add_block_to_block (&block, &se.pre);
6069 : :
6070 : : /* Create array descriptor and set version, rank, attribute, type. */
6071 : 12767 : cfi = gfc_create_var (gfc_get_cfi_type (e->rank < 0
6072 : : ? GFC_MAX_DIMENSIONS : e->rank,
6073 : : false), "cfi");
6074 : : /* Convert to CFI_cdesc_t, which has dim[] to avoid TBAA issues,*/
6075 : 6536 : if (fsym->attr.dimension && fsym->as->type == AS_ASSUMED_RANK)
6076 : : {
6077 : 2516 : tmp = gfc_get_cfi_type (-1, !fsym->attr.pointer && !fsym->attr.target);
6078 : 2338 : tmp = build_pointer_type (tmp);
6079 : 2338 : parmse->expr = cfi = gfc_build_addr_expr (tmp, cfi);
6080 : 2338 : cfi = build_fold_indirect_ref_loc (input_location, cfi);
6081 : : }
6082 : : else
6083 : 4198 : parmse->expr = gfc_build_addr_expr (NULL, cfi);
6084 : :
6085 : 6536 : tmp = gfc_get_cfi_desc_version (cfi);
6086 : 6536 : gfc_add_modify (&block, tmp,
6087 : 6536 : build_int_cst (TREE_TYPE (tmp), CFI_VERSION));
6088 : 6536 : if (e->rank < 0)
6089 : 305 : rank = fold_convert (signed_char_type_node, gfc_conv_descriptor_rank (gfc));
6090 : : else
6091 : 6231 : rank = build_int_cst (signed_char_type_node, e->rank);
6092 : 6536 : tmp = gfc_get_cfi_desc_rank (cfi);
6093 : 6536 : gfc_add_modify (&block, tmp, rank);
6094 : 6536 : int itype = CFI_type_other;
6095 : 6536 : if (e->ts.f90_type == BT_VOID)
6096 : 96 : itype = (e->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
6097 : 96 : ? CFI_type_cfunptr : CFI_type_cptr);
6098 : : else
6099 : : {
6100 : 6440 : if (e->expr_type == EXPR_NULL && e->ts.type == BT_UNKNOWN)
6101 : 1 : e->ts = fsym->ts;
6102 : 6440 : switch (e->ts.type)
6103 : : {
6104 : 2296 : case BT_INTEGER:
6105 : 2296 : case BT_LOGICAL:
6106 : 2296 : case BT_REAL:
6107 : 2296 : case BT_COMPLEX:
6108 : 2296 : itype = CFI_type_from_type_kind (e->ts.type, e->ts.kind);
6109 : 2296 : break;
6110 : 3410 : case BT_CHARACTER:
6111 : 3410 : itype = CFI_type_from_type_kind (CFI_type_Character, e->ts.kind);
6112 : 3410 : break;
6113 : : case BT_DERIVED:
6114 : 6536 : itype = CFI_type_struct;
6115 : : break;
6116 : 0 : case BT_VOID:
6117 : 0 : itype = (e->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
6118 : 0 : ? CFI_type_cfunptr : CFI_type_cptr);
6119 : : break;
6120 : : case BT_ASSUMED:
6121 : : itype = CFI_type_other; // FIXME: Or CFI_type_cptr ?
6122 : : break;
6123 : 1 : case BT_CLASS:
6124 : 1 : if (fsym->ts.type == BT_ASSUMED)
6125 : : {
6126 : : // F2017: 7.3.2.2: "An entity that is declared using the TYPE(*)
6127 : : // type specifier is assumed-type and is an unlimited polymorphic
6128 : : // entity." The actual argument _data component is passed.
6129 : : itype = CFI_type_other; // FIXME: Or CFI_type_cptr ?
6130 : : break;
6131 : : }
6132 : : else
6133 : 0 : gcc_unreachable ();
6134 : :
6135 : 0 : case BT_UNSIGNED:
6136 : 0 : gfc_internal_error ("Unsigned not yet implemented");
6137 : :
6138 : 0 : case BT_PROCEDURE:
6139 : 0 : case BT_HOLLERITH:
6140 : 0 : case BT_UNION:
6141 : 0 : case BT_BOZ:
6142 : 0 : case BT_UNKNOWN:
6143 : : // FIXME: Really unreachable? Or reachable for type(*) ? If so, CFI_type_other?
6144 : 0 : gcc_unreachable ();
6145 : : }
6146 : : }
6147 : :
6148 : 6536 : tmp = gfc_get_cfi_desc_type (cfi);
6149 : 6536 : gfc_add_modify (&block, tmp,
6150 : 6536 : build_int_cst (TREE_TYPE (tmp), itype));
6151 : :
6152 : 6536 : int attr = CFI_attribute_other;
6153 : 6536 : if (fsym->attr.pointer)
6154 : : attr = CFI_attribute_pointer;
6155 : 5774 : else if (fsym->attr.allocatable)
6156 : 433 : attr = CFI_attribute_allocatable;
6157 : 6536 : tmp = gfc_get_cfi_desc_attribute (cfi);
6158 : 6536 : gfc_add_modify (&block, tmp,
6159 : 6536 : build_int_cst (TREE_TYPE (tmp), attr));
6160 : :
6161 : : /* The cfi-base_addr assignment could be skipped for 'pointer, intent(out)'.
6162 : : That is very sensible for undefined pointers, but the C code might assume
6163 : : that the pointer retains the value, in particular, if it was NULL. */
6164 : 6536 : if (e->rank == 0)
6165 : : {
6166 : 686 : tmp = gfc_get_cfi_desc_base_addr (cfi);
6167 : 686 : gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), gfc));
6168 : : }
6169 : : else
6170 : : {
6171 : 5850 : tmp = gfc_get_cfi_desc_base_addr (cfi);
6172 : 5850 : tmp2 = gfc_conv_descriptor_data_get (gfc);
6173 : 5850 : gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), tmp2));
6174 : : }
6175 : :
6176 : : /* Set elem_len if known - must be before the next if block.
6177 : : Note that allocatable implies 'len=:'. */
6178 : 6536 : if (e->ts.type != BT_ASSUMED && e->ts.type != BT_CHARACTER )
6179 : : {
6180 : : /* Length is known at compile time; use 'block' for it. */
6181 : 3072 : tmp = size_in_bytes (gfc_typenode_for_spec (&e->ts));
6182 : 3072 : tmp2 = gfc_get_cfi_desc_elem_len (cfi);
6183 : 3072 : gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
6184 : : }
6185 : :
6186 : 6536 : if (fsym->attr.pointer && fsym->attr.intent == INTENT_OUT)
6187 : 91 : goto done;
6188 : :
6189 : : /* When allocatable + intent out, free the cfi descriptor. */
6190 : 6445 : if (fsym->attr.allocatable && fsym->attr.intent == INTENT_OUT)
6191 : : {
6192 : 90 : tmp = gfc_get_cfi_desc_base_addr (cfi);
6193 : 90 : tree call = builtin_decl_explicit (BUILT_IN_FREE);
6194 : 90 : call = build_call_expr_loc (input_location, call, 1, tmp);
6195 : 90 : gfc_add_expr_to_block (&block, fold_convert (void_type_node, call));
6196 : 90 : gfc_add_modify (&block, tmp,
6197 : 90 : fold_convert (TREE_TYPE (tmp), null_pointer_node));
6198 : 90 : goto done;
6199 : : }
6200 : :
6201 : : /* If not unallocated/unassociated. */
6202 : 6355 : gfc_init_block (&block2);
6203 : :
6204 : : /* Set elem_len, which may be only known at run time. */
6205 : 6355 : if (e->ts.type == BT_CHARACTER
6206 : 3410 : && (e->expr_type != EXPR_NULL || gfc_strlen != NULL_TREE))
6207 : : {
6208 : 3408 : gcc_assert (gfc_strlen);
6209 : 3409 : tmp = gfc_strlen;
6210 : 3409 : if (e->ts.kind != 1)
6211 : 1117 : tmp = fold_build2_loc (input_location, MULT_EXPR,
6212 : : gfc_charlen_type_node, tmp,
6213 : : build_int_cst (gfc_charlen_type_node,
6214 : 1117 : e->ts.kind));
6215 : 3409 : tmp2 = gfc_get_cfi_desc_elem_len (cfi);
6216 : 3409 : gfc_add_modify (&block2, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
6217 : : }
6218 : 2946 : else if (e->ts.type == BT_ASSUMED)
6219 : : {
6220 : 54 : tmp = gfc_conv_descriptor_elem_len (gfc);
6221 : 54 : tmp2 = gfc_get_cfi_desc_elem_len (cfi);
6222 : 54 : gfc_add_modify (&block2, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
6223 : : }
6224 : :
6225 : 6355 : if (e->ts.type == BT_ASSUMED)
6226 : : {
6227 : : /* Note: type(*) implies assumed-shape/assumed-rank if fsym requires
6228 : : an CFI descriptor. Use the type in the descriptor as it provide
6229 : : mode information. (Quality of implementation feature.) */
6230 : 54 : tree cond;
6231 : 54 : tree ctype = gfc_get_cfi_desc_type (cfi);
6232 : 54 : tree type = fold_convert (TREE_TYPE (ctype),
6233 : : gfc_conv_descriptor_type (gfc));
6234 : 54 : tree kind = fold_convert (TREE_TYPE (ctype),
6235 : : gfc_conv_descriptor_elem_len (gfc));
6236 : 54 : kind = fold_build2_loc (input_location, LSHIFT_EXPR, TREE_TYPE (type),
6237 : 54 : kind, build_int_cst (TREE_TYPE (type),
6238 : : CFI_type_kind_shift));
6239 : :
6240 : : /* if (BT_VOID) CFI_type_cptr else CFI_type_other */
6241 : : /* Note: BT_VOID is could also be CFI_type_funcptr, but assume c_ptr. */
6242 : 54 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6243 : 54 : build_int_cst (TREE_TYPE (type), BT_VOID));
6244 : 54 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, ctype,
6245 : 54 : build_int_cst (TREE_TYPE (type), CFI_type_cptr));
6246 : 54 : tmp2 = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
6247 : : ctype,
6248 : 54 : build_int_cst (TREE_TYPE (type), CFI_type_other));
6249 : 54 : tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6250 : : tmp, tmp2);
6251 : : /* if (BT_DERIVED) CFI_type_struct else < tmp2 > */
6252 : 54 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6253 : 54 : build_int_cst (TREE_TYPE (type), BT_DERIVED));
6254 : 54 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, ctype,
6255 : 54 : build_int_cst (TREE_TYPE (type), CFI_type_struct));
6256 : 54 : tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6257 : : tmp, tmp2);
6258 : : /* if (BT_CHARACTER) CFI_type_Character + kind=1 else < tmp2 > */
6259 : : /* Note: could also be kind=4, with cfi->elem_len = gfc->elem_len*4. */
6260 : 54 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6261 : 54 : build_int_cst (TREE_TYPE (type), BT_CHARACTER));
6262 : 54 : tmp = build_int_cst (TREE_TYPE (type),
6263 : : CFI_type_from_type_kind (CFI_type_Character, 1));
6264 : 54 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
6265 : : ctype, tmp);
6266 : 54 : tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6267 : : tmp, tmp2);
6268 : : /* if (BT_COMPLEX) CFI_type_Complex + kind/2 else < tmp2 > */
6269 : 54 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6270 : 54 : build_int_cst (TREE_TYPE (type), BT_COMPLEX));
6271 : 54 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR, TREE_TYPE (type),
6272 : 54 : kind, build_int_cst (TREE_TYPE (type), 2));
6273 : 54 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (type), tmp,
6274 : 54 : build_int_cst (TREE_TYPE (type),
6275 : : CFI_type_Complex));
6276 : 54 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
6277 : : ctype, tmp);
6278 : 54 : tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6279 : : tmp, tmp2);
6280 : : /* if (BT_INTEGER || BT_LOGICAL || BT_REAL) type + kind else <tmp2> */
6281 : 54 : cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6282 : 54 : build_int_cst (TREE_TYPE (type), BT_INTEGER));
6283 : 54 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6284 : 54 : build_int_cst (TREE_TYPE (type), BT_LOGICAL));
6285 : 54 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
6286 : : cond, tmp);
6287 : 54 : tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
6288 : 54 : build_int_cst (TREE_TYPE (type), BT_REAL));
6289 : 54 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
6290 : : cond, tmp);
6291 : 54 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (type),
6292 : : type, kind);
6293 : 54 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
6294 : : ctype, tmp);
6295 : 54 : tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
6296 : : tmp, tmp2);
6297 : 54 : gfc_add_expr_to_block (&block2, tmp2);
6298 : : }
6299 : :
6300 : 6355 : if (e->rank != 0)
6301 : : {
6302 : : /* Loop: for (i = 0; i < rank; ++i). */
6303 : 5735 : tree idx = gfc_create_var (TREE_TYPE (rank), "idx");
6304 : : /* Loop body. */
6305 : 5735 : stmtblock_t loop_body;
6306 : 5735 : gfc_init_block (&loop_body);
6307 : : /* cfi->dim[i].lower_bound = (allocatable/pointer)
6308 : : ? gfc->dim[i].lbound : 0 */
6309 : 5735 : if (fsym->attr.pointer || fsym->attr.allocatable)
6310 : 648 : tmp = gfc_conv_descriptor_lbound_get (gfc, idx);
6311 : : else
6312 : 5087 : tmp = gfc_index_zero_node;
6313 : 5735 : gfc_add_modify (&loop_body, gfc_get_cfi_dim_lbound (cfi, idx), tmp);
6314 : : /* cfi->dim[i].extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1. */
6315 : 5735 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6316 : : gfc_conv_descriptor_ubound_get (gfc, idx),
6317 : : gfc_conv_descriptor_lbound_get (gfc, idx));
6318 : 5735 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
6319 : : tmp, gfc_index_one_node);
6320 : 5735 : gfc_add_modify (&loop_body, gfc_get_cfi_dim_extent (cfi, idx), tmp);
6321 : : /* d->dim[n].sm = gfc->dim[i].stride * gfc->span); */
6322 : 5735 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6323 : : gfc_conv_descriptor_stride_get (gfc, idx),
6324 : : gfc_conv_descriptor_span_get (gfc));
6325 : 5735 : gfc_add_modify (&loop_body, gfc_get_cfi_dim_sm (cfi, idx), tmp);
6326 : :
6327 : : /* Generate loop. */
6328 : 11470 : gfc_simple_for_loop (&block2, idx, build_int_cst (TREE_TYPE (idx), 0),
6329 : 5735 : rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
6330 : : gfc_finish_block (&loop_body));
6331 : :
6332 : 5735 : if (e->expr_type == EXPR_VARIABLE
6333 : 5573 : && e->ref
6334 : 5573 : && e->ref->u.ar.type == AR_FULL
6335 : 2732 : && e->symtree->n.sym->attr.dummy
6336 : 988 : && e->symtree->n.sym->as
6337 : 988 : && e->symtree->n.sym->as->type == AS_ASSUMED_SIZE)
6338 : : {
6339 : 138 : tmp = gfc_get_cfi_dim_extent (cfi, gfc_rank_cst[e->rank-1]),
6340 : 138 : gfc_add_modify (&block2, tmp, build_int_cst (TREE_TYPE (tmp), -1));
6341 : : }
6342 : : }
6343 : :
6344 : 6355 : if (fsym->attr.allocatable || fsym->attr.pointer)
6345 : : {
6346 : 1014 : tmp = gfc_get_cfi_desc_base_addr (cfi),
6347 : 1014 : tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
6348 : : tmp, null_pointer_node);
6349 : 1014 : tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
6350 : : build_empty_stmt (input_location));
6351 : 1014 : gfc_add_expr_to_block (&block, tmp);
6352 : : }
6353 : : else
6354 : 5341 : gfc_add_block_to_block (&block, &block2);
6355 : :
6356 : :
6357 : 6536 : done:
6358 : 6536 : if (present)
6359 : : {
6360 : 103 : parmse->expr = build3_loc (input_location, COND_EXPR,
6361 : 103 : TREE_TYPE (parmse->expr),
6362 : : present, parmse->expr, null_pointer_node);
6363 : 103 : tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block),
6364 : : build_empty_stmt (input_location));
6365 : 103 : gfc_add_expr_to_block (&parmse->pre, tmp);
6366 : : }
6367 : : else
6368 : 6433 : gfc_add_block_to_block (&parmse->pre, &block);
6369 : :
6370 : 6536 : gfc_init_block (&block);
6371 : :
6372 : 6536 : if ((!fsym->attr.allocatable && !fsym->attr.pointer)
6373 : 1195 : || fsym->attr.intent == INTENT_IN)
6374 : 5549 : goto post_call;
6375 : :
6376 : 987 : gfc_init_block (&block2);
6377 : 987 : if (e->rank == 0)
6378 : : {
6379 : 428 : tmp = gfc_get_cfi_desc_base_addr (cfi);
6380 : 428 : gfc_add_modify (&block, gfc, fold_convert (TREE_TYPE (gfc), tmp));
6381 : : }
6382 : : else
6383 : : {
6384 : 559 : tmp = gfc_get_cfi_desc_base_addr (cfi);
6385 : 559 : gfc_conv_descriptor_data_set (&block, gfc, tmp);
6386 : :
6387 : 559 : if (fsym->attr.allocatable)
6388 : : {
6389 : : /* gfc->span = cfi->elem_len. */
6390 : 252 : tmp = fold_convert (gfc_array_index_type,
6391 : : gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]));
6392 : : }
6393 : : else
6394 : : {
6395 : : /* gfc->span = ((cfi->dim[0].sm % cfi->elem_len)
6396 : : ? cfi->dim[0].sm : cfi->elem_len). */
6397 : 307 : tmp = gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]);
6398 : 307 : tmp2 = fold_convert (gfc_array_index_type,
6399 : : gfc_get_cfi_desc_elem_len (cfi));
6400 : 307 : tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR,
6401 : : gfc_array_index_type, tmp, tmp2);
6402 : 307 : tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
6403 : : tmp, gfc_index_zero_node);
6404 : 307 : tmp = build3_loc (input_location, COND_EXPR, gfc_array_index_type, tmp,
6405 : : gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]), tmp2);
6406 : : }
6407 : 559 : gfc_conv_descriptor_span_set (&block2, gfc, tmp);
6408 : :
6409 : : /* Calculate offset + set lbound, ubound and stride. */
6410 : 559 : gfc_conv_descriptor_offset_set (&block2, gfc, gfc_index_zero_node);
6411 : : /* Loop: for (i = 0; i < rank; ++i). */
6412 : 559 : tree idx = gfc_create_var (TREE_TYPE (rank), "idx");
6413 : : /* Loop body. */
6414 : 559 : stmtblock_t loop_body;
6415 : 559 : gfc_init_block (&loop_body);
6416 : : /* gfc->dim[i].lbound = ... */
6417 : 559 : tmp = gfc_get_cfi_dim_lbound (cfi, idx);
6418 : 559 : gfc_conv_descriptor_lbound_set (&loop_body, gfc, idx, tmp);
6419 : :
6420 : : /* gfc->dim[i].ubound = gfc->dim[i].lbound + cfi->dim[i].extent - 1. */
6421 : 559 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6422 : : gfc_conv_descriptor_lbound_get (gfc, idx),
6423 : : gfc_index_one_node);
6424 : 559 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
6425 : : gfc_get_cfi_dim_extent (cfi, idx), tmp);
6426 : 559 : gfc_conv_descriptor_ubound_set (&loop_body, gfc, idx, tmp);
6427 : :
6428 : : /* gfc->dim[i].stride = cfi->dim[i].sm / cfi>elem_len */
6429 : 559 : tmp = gfc_get_cfi_dim_sm (cfi, idx);
6430 : 559 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
6431 : : gfc_array_index_type, tmp,
6432 : : fold_convert (gfc_array_index_type,
6433 : : gfc_get_cfi_desc_elem_len (cfi)));
6434 : 559 : gfc_conv_descriptor_stride_set (&loop_body, gfc, idx, tmp);
6435 : :
6436 : : /* gfc->offset -= gfc->dim[i].stride * gfc->dim[i].lbound. */
6437 : 559 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6438 : : gfc_conv_descriptor_stride_get (gfc, idx),
6439 : : gfc_conv_descriptor_lbound_get (gfc, idx));
6440 : 559 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6441 : : gfc_conv_descriptor_offset_get (gfc), tmp);
6442 : 559 : gfc_conv_descriptor_offset_set (&loop_body, gfc, tmp);
6443 : : /* Generate loop. */
6444 : 1118 : gfc_simple_for_loop (&block2, idx, build_int_cst (TREE_TYPE (idx), 0),
6445 : 559 : rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
6446 : : gfc_finish_block (&loop_body));
6447 : : }
6448 : :
6449 : 987 : if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
6450 : : {
6451 : 60 : tmp = fold_convert (gfc_charlen_type_node,
6452 : : gfc_get_cfi_desc_elem_len (cfi));
6453 : 60 : if (e->ts.kind != 1)
6454 : 24 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
6455 : : gfc_charlen_type_node, tmp,
6456 : : build_int_cst (gfc_charlen_type_node,
6457 : 24 : e->ts.kind));
6458 : 60 : gfc_add_modify (&block2, gfc_strlen, tmp);
6459 : : }
6460 : :
6461 : 987 : tmp = gfc_get_cfi_desc_base_addr (cfi),
6462 : 987 : tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
6463 : : tmp, null_pointer_node);
6464 : 987 : tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
6465 : : build_empty_stmt (input_location));
6466 : 987 : gfc_add_expr_to_block (&block, tmp);
6467 : :
6468 : 6536 : post_call:
6469 : 6536 : gfc_add_block_to_block (&block, &se.post);
6470 : 6536 : if (present && block.head)
6471 : : {
6472 : 6 : tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block),
6473 : : build_empty_stmt (input_location));
6474 : 6 : gfc_add_expr_to_block (&parmse->post, tmp);
6475 : : }
6476 : 6530 : else if (block.head)
6477 : 1558 : gfc_add_block_to_block (&parmse->post, &block);
6478 : 6536 : }
6479 : :
6480 : :
6481 : : /* Create "conditional temporary" to handle scalar dummy variables with the
6482 : : OPTIONAL+VALUE attribute that shall not be dereferenced. Use null value
6483 : : as fallback. Does not handle CLASS. */
6484 : :
6485 : : static void
6486 : 222 : conv_cond_temp (gfc_se * parmse, gfc_expr * e, tree cond)
6487 : : {
6488 : 222 : tree temp;
6489 : 222 : gcc_assert (e && e->ts.type != BT_CLASS);
6490 : 222 : gcc_assert (e->rank == 0);
6491 : 222 : temp = gfc_create_var (TREE_TYPE (parmse->expr), "condtemp");
6492 : 222 : TREE_STATIC (temp) = 1;
6493 : 222 : TREE_CONSTANT (temp) = 1;
6494 : 222 : TREE_READONLY (temp) = 1;
6495 : 222 : DECL_INITIAL (temp) = build_zero_cst (TREE_TYPE (temp));
6496 : 222 : parmse->expr = fold_build3_loc (input_location, COND_EXPR,
6497 : 222 : TREE_TYPE (parmse->expr),
6498 : : cond, parmse->expr, temp);
6499 : 222 : parmse->expr = gfc_evaluate_now (parmse->expr, &parmse->pre);
6500 : 222 : }
6501 : :
6502 : :
6503 : : /* Helper function for the handling of (currently) scalar dummy variables
6504 : : with the VALUE attribute. Argument parmse should already be set up. */
6505 : : static void
6506 : 22140 : conv_dummy_value (gfc_se * parmse, gfc_expr * e, gfc_symbol * fsym,
6507 : : vec<tree, va_gc> *& optionalargs)
6508 : : {
6509 : 22140 : tree tmp;
6510 : :
6511 : 22140 : gcc_assert (fsym && fsym->attr.value && !fsym->attr.dimension);
6512 : :
6513 : : /* Absent actual argument for optional scalar dummy. */
6514 : 22140 : if ((e == NULL || e->expr_type == EXPR_NULL) && fsym->attr.optional)
6515 : : {
6516 : : /* For scalar arguments with VALUE attribute which are passed by
6517 : : value, pass "0" and a hidden argument for the optional status. */
6518 : 427 : if (fsym->ts.type == BT_CHARACTER)
6519 : : {
6520 : : /* Pass a NULL pointer for an absent CHARACTER arg and a length of
6521 : : zero. */
6522 : 90 : parmse->expr = null_pointer_node;
6523 : 90 : parmse->string_length = build_int_cst (gfc_charlen_type_node, 0);
6524 : : }
6525 : 337 : else if (gfc_bt_struct (fsym->ts.type)
6526 : 30 : && !(fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING))
6527 : : {
6528 : : /* Pass null struct. Types c_ptr and c_funptr from ISO_C_BINDING
6529 : : are pointers and passed as such below. */
6530 : 24 : tree temp = gfc_create_var (gfc_sym_type (fsym), "absent");
6531 : 24 : TREE_CONSTANT (temp) = 1;
6532 : 24 : TREE_READONLY (temp) = 1;
6533 : 24 : DECL_INITIAL (temp) = build_zero_cst (TREE_TYPE (temp));
6534 : 24 : parmse->expr = temp;
6535 : 24 : }
6536 : : else
6537 : 313 : parmse->expr = fold_convert (gfc_sym_type (fsym),
6538 : : integer_zero_node);
6539 : 427 : vec_safe_push (optionalargs, boolean_false_node);
6540 : :
6541 : 427 : return;
6542 : : }
6543 : :
6544 : : /* gfortran argument passing conventions:
6545 : : actual arguments to CHARACTER(len=1),VALUE
6546 : : dummy arguments are actually passed by value.
6547 : : Strings are truncated to length 1. */
6548 : 21713 : if (gfc_length_one_character_type_p (&fsym->ts))
6549 : : {
6550 : 390 : if (e->expr_type == EXPR_CONSTANT
6551 : 66 : && e->value.character.length > 1)
6552 : : {
6553 : 12 : e->value.character.length = 1;
6554 : 12 : gfc_conv_expr (parmse, e);
6555 : : }
6556 : :
6557 : 390 : tree slen1 = build_int_cst (gfc_charlen_type_node, 1);
6558 : 390 : gfc_conv_string_parameter (parmse);
6559 : 390 : parmse->expr = gfc_string_to_single_character (slen1, parmse->expr,
6560 : : e->ts.kind);
6561 : : /* Truncate resulting string to length 1. */
6562 : 390 : parmse->string_length = slen1;
6563 : : }
6564 : :
6565 : 21713 : if (fsym->attr.optional && fsym->ts.type != BT_CLASS)
6566 : : {
6567 : : /* F2018:15.5.2.12 Argument presence and
6568 : : restrictions on arguments not present. */
6569 : 811 : if (e->expr_type == EXPR_VARIABLE
6570 : 638 : && e->rank == 0
6571 : 1395 : && (gfc_expr_attr (e).allocatable
6572 : 482 : || gfc_expr_attr (e).pointer))
6573 : : {
6574 : 186 : gfc_se argse;
6575 : 186 : tree cond;
6576 : 186 : gfc_init_se (&argse, NULL);
6577 : 186 : argse.want_pointer = 1;
6578 : 186 : gfc_conv_expr (&argse, e);
6579 : 186 : cond = fold_convert (TREE_TYPE (argse.expr), null_pointer_node);
6580 : 186 : cond = fold_build2_loc (input_location, NE_EXPR,
6581 : : logical_type_node,
6582 : : argse.expr, cond);
6583 : 372 : vec_safe_push (optionalargs,
6584 : 186 : fold_convert (boolean_type_node, cond));
6585 : : /* Create "conditional temporary". */
6586 : 186 : conv_cond_temp (parmse, e, cond);
6587 : : }
6588 : 625 : else if (e->expr_type != EXPR_VARIABLE
6589 : 452 : || !e->symtree->n.sym->attr.optional
6590 : 260 : || (e->ref != NULL && e->ref->type != REF_ARRAY))
6591 : 365 : vec_safe_push (optionalargs, boolean_true_node);
6592 : : else
6593 : : {
6594 : 260 : tmp = gfc_conv_expr_present (e->symtree->n.sym);
6595 : 260 : if (gfc_bt_struct (fsym->ts.type)
6596 : 36 : && !(fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING))
6597 : 36 : conv_cond_temp (parmse, e, tmp);
6598 : 224 : else if (e->ts.type != BT_CHARACTER && !e->symtree->n.sym->attr.value)
6599 : 84 : parmse->expr
6600 : 168 : = fold_build3_loc (input_location, COND_EXPR,
6601 : 84 : TREE_TYPE (parmse->expr),
6602 : : tmp, parmse->expr,
6603 : 84 : fold_convert (TREE_TYPE (parmse->expr),
6604 : : integer_zero_node));
6605 : :
6606 : 520 : vec_safe_push (optionalargs,
6607 : 260 : fold_convert (boolean_type_node, tmp));
6608 : : }
6609 : : }
6610 : : }
6611 : :
6612 : :
6613 : : /* Helper function for the handling of NULL() actual arguments associated with
6614 : : non-optional dummy variables. Argument parmse should already be set up. */
6615 : : static void
6616 : 426 : conv_null_actual (gfc_se * parmse, gfc_expr * e, gfc_symbol * fsym)
6617 : : {
6618 : 426 : gcc_assert (fsym && e->expr_type == EXPR_NULL);
6619 : :
6620 : : /* Obtain the character length for a NULL() actual with a character
6621 : : MOLD argument. Otherwise substitute a suitable dummy length.
6622 : : Here we handle only non-optional dummies of non-bind(c) procedures. */
6623 : 426 : if (fsym->ts.type == BT_CHARACTER)
6624 : : {
6625 : 216 : if (e->ts.type == BT_CHARACTER
6626 : 162 : && e->symtree->n.sym->ts.type == BT_CHARACTER)
6627 : : {
6628 : : /* MOLD is present. Substitute a temporary character NULL pointer.
6629 : : For an assumed-rank dummy we need a descriptor that passes the
6630 : : correct rank. */
6631 : 162 : if (fsym->as && fsym->as->type == AS_ASSUMED_RANK)
6632 : : {
6633 : 54 : tree rank;
6634 : 54 : tree tmp = parmse->expr;
6635 : 54 : tmp = gfc_conv_scalar_to_descriptor (parmse, tmp, fsym->attr);
6636 : 54 : rank = gfc_conv_descriptor_rank (tmp);
6637 : 54 : gfc_add_modify (&parmse->pre, rank,
6638 : 54 : build_int_cst (TREE_TYPE (rank), e->rank));
6639 : 54 : parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
6640 : 54 : }
6641 : : else
6642 : : {
6643 : 108 : tree tmp = gfc_create_var (TREE_TYPE (parmse->expr), "null");
6644 : 108 : gfc_add_modify (&parmse->pre, tmp,
6645 : 108 : build_zero_cst (TREE_TYPE (tmp)));
6646 : 108 : parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
6647 : : }
6648 : :
6649 : : /* Ensure that a usable length is available. */
6650 : 162 : if (parmse->string_length == NULL_TREE)
6651 : : {
6652 : 162 : gfc_typespec *ts = &e->symtree->n.sym->ts;
6653 : :
6654 : 162 : if (ts->u.cl->length != NULL
6655 : 108 : && ts->u.cl->length->expr_type == EXPR_CONSTANT)
6656 : 108 : gfc_conv_const_charlen (ts->u.cl);
6657 : :
6658 : 162 : if (ts->u.cl->backend_decl)
6659 : 162 : parmse->string_length = ts->u.cl->backend_decl;
6660 : : }
6661 : : }
6662 : 54 : else if (e->ts.type == BT_UNKNOWN && parmse->string_length == NULL_TREE)
6663 : : {
6664 : : /* MOLD is not present. Pass length of associated dummy character
6665 : : argument if constant, or zero. */
6666 : 54 : if (fsym->ts.u.cl->length != NULL
6667 : 18 : && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
6668 : : {
6669 : 18 : gfc_conv_const_charlen (fsym->ts.u.cl);
6670 : 18 : parmse->string_length = fsym->ts.u.cl->backend_decl;
6671 : : }
6672 : : else
6673 : : {
6674 : 36 : parmse->string_length = gfc_create_var (gfc_charlen_type_node,
6675 : : "slen");
6676 : 36 : gfc_add_modify (&parmse->pre, parmse->string_length,
6677 : : build_zero_cst (gfc_charlen_type_node));
6678 : : }
6679 : : }
6680 : : }
6681 : 210 : else if (fsym->ts.type == BT_DERIVED)
6682 : : {
6683 : 210 : if (e->ts.type != BT_UNKNOWN)
6684 : : /* MOLD is present. Pass a corresponding temporary NULL pointer.
6685 : : For an assumed-rank dummy we provide a descriptor that passes
6686 : : the correct rank. */
6687 : : {
6688 : 138 : tree rank;
6689 : 138 : tree tmp = parmse->expr;
6690 : :
6691 : 138 : tmp = gfc_conv_scalar_to_descriptor (parmse, tmp, gfc_expr_attr (e));
6692 : 138 : rank = gfc_conv_descriptor_rank (tmp);
6693 : 138 : gfc_add_modify (&parmse->pre, rank,
6694 : 138 : build_int_cst (TREE_TYPE (rank), e->rank));
6695 : 138 : gfc_conv_descriptor_data_set (&parmse->pre, tmp, null_pointer_node);
6696 : 138 : parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
6697 : : }
6698 : : else
6699 : : /* MOLD is not present. Use attributes from dummy argument, which is
6700 : : not allowed to be assumed-rank. */
6701 : : {
6702 : 72 : int dummy_rank;
6703 : 72 : tree tmp = parmse->expr;
6704 : :
6705 : 72 : if ((fsym->attr.allocatable || fsym->attr.pointer)
6706 : 72 : && fsym->attr.intent == INTENT_UNKNOWN)
6707 : 36 : fsym->attr.intent = INTENT_IN;
6708 : 72 : tmp = gfc_conv_scalar_to_descriptor (parmse, tmp, fsym->attr);
6709 : 72 : dummy_rank = fsym->as ? fsym->as->rank : 0;
6710 : 24 : if (dummy_rank > 0)
6711 : : {
6712 : 24 : tree rank = gfc_conv_descriptor_rank (tmp);
6713 : 24 : gfc_add_modify (&parmse->pre, rank,
6714 : 24 : build_int_cst (TREE_TYPE (rank), dummy_rank));
6715 : : }
6716 : 72 : gfc_conv_descriptor_data_set (&parmse->pre, tmp, null_pointer_node);
6717 : 72 : parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
6718 : : }
6719 : : }
6720 : 426 : }
6721 : :
6722 : :
6723 : : /* Generate code for a procedure call. Note can return se->post != NULL.
6724 : : If se->direct_byref is set then se->expr contains the return parameter.
6725 : : Return nonzero, if the call has alternate specifiers.
6726 : : 'expr' is only needed for procedure pointer components. */
6727 : :
6728 : : int
6729 : 132694 : gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
6730 : : gfc_actual_arglist * args, gfc_expr * expr,
6731 : : vec<tree, va_gc> *append_args)
6732 : : {
6733 : 132694 : gfc_interface_mapping mapping;
6734 : 132694 : vec<tree, va_gc> *arglist;
6735 : 132694 : vec<tree, va_gc> *retargs;
6736 : 132694 : tree tmp;
6737 : 132694 : tree fntype;
6738 : 132694 : gfc_se parmse;
6739 : 132694 : gfc_array_info *info;
6740 : 132694 : int byref;
6741 : 132694 : int parm_kind;
6742 : 132694 : tree type;
6743 : 132694 : tree var;
6744 : 132694 : tree len;
6745 : 132694 : tree base_object;
6746 : 132694 : vec<tree, va_gc> *stringargs;
6747 : 132694 : vec<tree, va_gc> *optionalargs;
6748 : 132694 : tree result = NULL;
6749 : 132694 : gfc_formal_arglist *formal;
6750 : 132694 : gfc_actual_arglist *arg;
6751 : 132694 : int has_alternate_specifier = 0;
6752 : 132694 : bool need_interface_mapping;
6753 : 132694 : bool is_builtin;
6754 : 132694 : bool callee_alloc;
6755 : 132694 : bool ulim_copy;
6756 : 132694 : gfc_typespec ts;
6757 : 132694 : gfc_charlen cl;
6758 : 132694 : gfc_expr *e;
6759 : 132694 : gfc_symbol *fsym;
6760 : 132694 : enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
6761 : 132694 : gfc_component *comp = NULL;
6762 : 132694 : int arglen;
6763 : 132694 : unsigned int argc;
6764 : 132694 : tree arg1_cntnr = NULL_TREE;
6765 : 132694 : arglist = NULL;
6766 : 132694 : retargs = NULL;
6767 : 132694 : stringargs = NULL;
6768 : 132694 : optionalargs = NULL;
6769 : 132694 : var = NULL_TREE;
6770 : 132694 : len = NULL_TREE;
6771 : 132694 : gfc_clear_ts (&ts);
6772 : 132694 : gfc_intrinsic_sym *isym = expr && expr->rank ?
6773 : : expr->value.function.isym : NULL;
6774 : :
6775 : 132694 : comp = gfc_get_proc_ptr_comp (expr);
6776 : :
6777 : 265388 : bool elemental_proc = (comp
6778 : 1842 : && comp->ts.interface
6779 : 1789 : && comp->ts.interface->attr.elemental)
6780 : 1649 : || (comp && comp->attr.elemental)
6781 : 134343 : || sym->attr.elemental;
6782 : :
6783 : 132694 : if (se->ss != NULL)
6784 : : {
6785 : 24738 : if (!elemental_proc)
6786 : : {
6787 : 21450 : gcc_assert (se->ss->info->type == GFC_SS_FUNCTION);
6788 : 21450 : if (se->ss->info->useflags)
6789 : : {
6790 : 5713 : gcc_assert ((!comp && gfc_return_by_reference (sym)
6791 : : && sym->result->attr.dimension)
6792 : : || (comp && comp->attr.dimension)
6793 : : || gfc_is_class_array_function (expr));
6794 : 5713 : gcc_assert (se->loop != NULL);
6795 : : /* Access the previously obtained result. */
6796 : 5713 : gfc_conv_tmp_array_ref (se);
6797 : 5713 : return 0;
6798 : : }
6799 : : }
6800 : 19025 : info = &se->ss->info->data.array;
6801 : : }
6802 : : else
6803 : : info = NULL;
6804 : :
6805 : 126981 : stmtblock_t post, clobbers, dealloc_blk;
6806 : 126981 : gfc_init_block (&post);
6807 : 126981 : gfc_init_block (&clobbers);
6808 : 126981 : gfc_init_block (&dealloc_blk);
6809 : 126981 : gfc_init_interface_mapping (&mapping);
6810 : 126981 : if (!comp)
6811 : : {
6812 : 125188 : formal = gfc_sym_get_dummy_args (sym);
6813 : 125188 : need_interface_mapping = sym->attr.dimension ||
6814 : 109697 : (sym->ts.type == BT_CHARACTER
6815 : 3031 : && sym->ts.u.cl->length
6816 : 2379 : && sym->ts.u.cl->length->expr_type
6817 : : != EXPR_CONSTANT);
6818 : : }
6819 : : else
6820 : : {
6821 : 1793 : formal = comp->ts.interface ? comp->ts.interface->formal : NULL;
6822 : 1793 : need_interface_mapping = comp->attr.dimension ||
6823 : 1724 : (comp->ts.type == BT_CHARACTER
6824 : 67 : && comp->ts.u.cl->length
6825 : 58 : && comp->ts.u.cl->length->expr_type
6826 : : != EXPR_CONSTANT);
6827 : : }
6828 : :
6829 : 126981 : base_object = NULL_TREE;
6830 : : /* For _vprt->_copy () routines no formal symbol is present. Nevertheless
6831 : : is the third and fourth argument to such a function call a value
6832 : : denoting the number of elements to copy (i.e., most of the time the
6833 : : length of a deferred length string). */
6834 : 253962 : ulim_copy = (formal == NULL)
6835 : 31064 : && UNLIMITED_POLY (sym)
6836 : 127060 : && comp && (strcmp ("_copy", comp->name) == 0);
6837 : :
6838 : : /* Scan for allocatable actual arguments passed to allocatable dummy
6839 : : arguments with INTENT(OUT). As the corresponding actual arguments are
6840 : : deallocated before execution of the procedure, we evaluate actual
6841 : : argument expressions to avoid problems with possible dependencies. */
6842 : 126981 : bool force_eval_args = false;
6843 : 126981 : gfc_formal_arglist *tmp_formal;
6844 : 392864 : for (arg = args, tmp_formal = formal; arg != NULL;
6845 : 232565 : arg = arg->next, tmp_formal = tmp_formal ? tmp_formal->next : NULL)
6846 : : {
6847 : 266370 : e = arg->expr;
6848 : 266370 : fsym = tmp_formal ? tmp_formal->sym : NULL;
6849 : 252539 : if (e && fsym
6850 : 220648 : && e->expr_type == EXPR_VARIABLE
6851 : 96480 : && fsym->attr.intent == INTENT_OUT
6852 : 6192 : && (fsym->ts.type == BT_CLASS && fsym->attr.class_ok
6853 : 6192 : ? CLASS_DATA (fsym)->attr.allocatable
6854 : 4718 : : fsym->attr.allocatable)
6855 : 487 : && e->symtree
6856 : 487 : && e->symtree->n.sym
6857 : 518909 : && gfc_variable_attr (e, NULL).allocatable)
6858 : : {
6859 : : force_eval_args = true;
6860 : : break;
6861 : : }
6862 : : }
6863 : :
6864 : : /* Evaluate the arguments. */
6865 : 393741 : for (arg = args, argc = 0; arg != NULL;
6866 : 266760 : arg = arg->next, formal = formal ? formal->next : NULL, ++argc)
6867 : : {
6868 : 266760 : bool finalized = false;
6869 : 266760 : tree derived_array = NULL_TREE;
6870 : 266760 : symbol_attribute *attr;
6871 : :
6872 : 266760 : e = arg->expr;
6873 : 266760 : fsym = formal ? formal->sym : NULL;
6874 : 500202 : parm_kind = MISSING;
6875 : :
6876 : 233442 : attr = fsym ? &(fsym->ts.type == BT_CLASS ? CLASS_DATA (fsym)->attr
6877 : : : fsym->attr)
6878 : : : nullptr;
6879 : : /* If the procedure requires an explicit interface, the actual
6880 : : argument is passed according to the corresponding formal
6881 : : argument. If the corresponding formal argument is a POINTER,
6882 : : ALLOCATABLE or assumed shape, we do not use g77's calling
6883 : : convention, and pass the address of the array descriptor
6884 : : instead. Otherwise we use g77's calling convention, in other words
6885 : : pass the array data pointer without descriptor. */
6886 : 233389 : bool nodesc_arg = fsym != NULL
6887 : 233389 : && !(fsym->attr.pointer || fsym->attr.allocatable)
6888 : 224471 : && fsym->as
6889 : 39666 : && fsym->as->type != AS_ASSUMED_SHAPE
6890 : 24546 : && fsym->as->type != AS_ASSUMED_RANK;
6891 : 266760 : if (comp)
6892 : 2697 : nodesc_arg = nodesc_arg || !comp->attr.always_explicit;
6893 : : else
6894 : 264063 : nodesc_arg
6895 : : = nodesc_arg
6896 : 264063 : || !(sym->attr.always_explicit || (attr && attr->codimension));
6897 : :
6898 : : /* Class array expressions are sometimes coming completely unadorned
6899 : : with either arrayspec or _data component. Correct that here.
6900 : : OOP-TODO: Move this to the frontend. */
6901 : 266760 : if (e && e->expr_type == EXPR_VARIABLE
6902 : 110594 : && !e->ref
6903 : 50623 : && e->ts.type == BT_CLASS
6904 : 2593 : && (CLASS_DATA (e)->attr.codimension
6905 : 2593 : || CLASS_DATA (e)->attr.dimension))
6906 : : {
6907 : 0 : gfc_typespec temp_ts = e->ts;
6908 : 0 : gfc_add_class_array_ref (e);
6909 : 0 : e->ts = temp_ts;
6910 : : }
6911 : :
6912 : 266760 : if (e == NULL
6913 : 252923 : || (e->expr_type == EXPR_NULL
6914 : 745 : && fsym
6915 : 745 : && fsym->attr.value
6916 : 72 : && fsym->attr.optional
6917 : 72 : && !fsym->attr.dimension
6918 : 72 : && fsym->ts.type != BT_CLASS))
6919 : : {
6920 : 13909 : if (se->ignore_optional)
6921 : : {
6922 : : /* Some intrinsics have already been resolved to the correct
6923 : : parameters. */
6924 : 422 : continue;
6925 : : }
6926 : 13711 : else if (arg->label)
6927 : : {
6928 : 224 : has_alternate_specifier = 1;
6929 : 224 : continue;
6930 : : }
6931 : : else
6932 : : {
6933 : 13487 : gfc_init_se (&parmse, NULL);
6934 : :
6935 : : /* For scalar arguments with VALUE attribute which are passed by
6936 : : value, pass "0" and a hidden argument gives the optional
6937 : : status. */
6938 : 13487 : if (fsym && fsym->attr.optional && fsym->attr.value
6939 : 427 : && !fsym->attr.dimension && fsym->ts.type != BT_CLASS)
6940 : : {
6941 : 427 : conv_dummy_value (&parmse, e, fsym, optionalargs);
6942 : : }
6943 : : else
6944 : : {
6945 : : /* Pass a NULL pointer for an absent arg. */
6946 : 13060 : parmse.expr = null_pointer_node;
6947 : :
6948 : : /* Is it an absent character dummy? */
6949 : 13060 : bool absent_char = false;
6950 : 13060 : gfc_dummy_arg * const dummy_arg = arg->associated_dummy;
6951 : :
6952 : : /* Fall back to inferred type only if no formal. */
6953 : 13060 : if (fsym)
6954 : 12002 : absent_char = (fsym->ts.type == BT_CHARACTER);
6955 : 1058 : else if (dummy_arg)
6956 : 1058 : absent_char = (gfc_dummy_arg_get_typespec (*dummy_arg).type
6957 : : == BT_CHARACTER);
6958 : 13060 : if (absent_char)
6959 : 1115 : parmse.string_length = build_int_cst (gfc_charlen_type_node,
6960 : : 0);
6961 : : }
6962 : : }
6963 : : }
6964 : 252851 : else if (e->expr_type == EXPR_NULL
6965 : 673 : && (e->ts.type == BT_UNKNOWN || e->ts.type == BT_DERIVED)
6966 : 371 : && fsym && attr && (attr->pointer || attr->allocatable)
6967 : 293 : && fsym->ts.type == BT_DERIVED)
6968 : : {
6969 : 210 : gfc_init_se (&parmse, NULL);
6970 : 210 : gfc_conv_expr_reference (&parmse, e);
6971 : 210 : conv_null_actual (&parmse, e, fsym);
6972 : : }
6973 : 252641 : else if (arg->expr->expr_type == EXPR_NULL
6974 : 463 : && fsym && !fsym->attr.pointer
6975 : 163 : && (fsym->ts.type != BT_CLASS
6976 : 6 : || !CLASS_DATA (fsym)->attr.class_pointer))
6977 : : {
6978 : : /* Pass a NULL pointer to denote an absent arg. */
6979 : 163 : gcc_assert (fsym->attr.optional && !fsym->attr.allocatable
6980 : : && (fsym->ts.type != BT_CLASS
6981 : : || !CLASS_DATA (fsym)->attr.allocatable));
6982 : 163 : gfc_init_se (&parmse, NULL);
6983 : 163 : parmse.expr = null_pointer_node;
6984 : 163 : if (fsym->ts.type == BT_CHARACTER)
6985 : 42 : parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
6986 : : }
6987 : 252478 : else if (fsym && fsym->ts.type == BT_CLASS
6988 : 10490 : && e->ts.type == BT_DERIVED)
6989 : : {
6990 : : /* The derived type needs to be converted to a temporary
6991 : : CLASS object. */
6992 : 4180 : gfc_init_se (&parmse, se);
6993 : 4180 : gfc_conv_derived_to_class (&parmse, e, fsym, NULL_TREE,
6994 : 4180 : fsym->attr.optional
6995 : 1008 : && e->expr_type == EXPR_VARIABLE
6996 : 5188 : && e->symtree->n.sym->attr.optional,
6997 : 4180 : CLASS_DATA (fsym)->attr.class_pointer
6998 : 4180 : || CLASS_DATA (fsym)->attr.allocatable,
6999 : : sym->name, &derived_array);
7000 : : }
7001 : 216407 : else if (UNLIMITED_POLY (fsym) && e->ts.type != BT_CLASS
7002 : 888 : && e->ts.type != BT_PROCEDURE
7003 : 864 : && (gfc_expr_attr (e).flavor != FL_PROCEDURE
7004 : 12 : || gfc_expr_attr (e).proc != PROC_UNKNOWN))
7005 : : {
7006 : : /* The intrinsic type needs to be converted to a temporary
7007 : : CLASS object for the unlimited polymorphic formal. */
7008 : 864 : gfc_find_vtab (&e->ts);
7009 : 864 : gfc_init_se (&parmse, se);
7010 : 864 : gfc_conv_intrinsic_to_class (&parmse, e, fsym->ts);
7011 : :
7012 : : }
7013 : 247434 : else if (se->ss && se->ss->info->useflags)
7014 : : {
7015 : 5518 : gfc_ss *ss;
7016 : :
7017 : 5518 : ss = se->ss;
7018 : :
7019 : : /* An elemental function inside a scalarized loop. */
7020 : 5518 : gfc_init_se (&parmse, se);
7021 : 5518 : parm_kind = ELEMENTAL;
7022 : :
7023 : : /* When no fsym is present, ulim_copy is set and this is a third or
7024 : : fourth argument, use call-by-value instead of by reference to
7025 : : hand the length properties to the copy routine (i.e., most of the
7026 : : time this will be a call to a __copy_character_* routine where the
7027 : : third and fourth arguments are the lengths of a deferred length
7028 : : char array). */
7029 : 5518 : if ((fsym && fsym->attr.value)
7030 : 5284 : || (ulim_copy && (argc == 2 || argc == 3)))
7031 : 234 : gfc_conv_expr (&parmse, e);
7032 : 5284 : else if (e->expr_type == EXPR_ARRAY)
7033 : : {
7034 : 294 : gfc_conv_expr (&parmse, e);
7035 : 294 : if (e->ts.type != BT_CHARACTER)
7036 : 251 : parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
7037 : : }
7038 : : else
7039 : 4990 : gfc_conv_expr_reference (&parmse, e);
7040 : :
7041 : 5518 : if (e->ts.type == BT_CHARACTER && !e->rank
7042 : 174 : && e->expr_type == EXPR_FUNCTION)
7043 : 12 : parmse.expr = build_fold_indirect_ref_loc (input_location,
7044 : : parmse.expr);
7045 : :
7046 : 5468 : if (fsym && fsym->ts.type == BT_DERIVED
7047 : 6858 : && gfc_is_class_container_ref (e))
7048 : : {
7049 : 24 : parmse.expr = gfc_class_data_get (parmse.expr);
7050 : :
7051 : 24 : if (fsym->attr.optional && e->expr_type == EXPR_VARIABLE
7052 : 24 : && e->symtree->n.sym->attr.optional)
7053 : : {
7054 : 0 : tree cond = gfc_conv_expr_present (e->symtree->n.sym);
7055 : 0 : parmse.expr = build3_loc (input_location, COND_EXPR,
7056 : 0 : TREE_TYPE (parmse.expr),
7057 : : cond, parmse.expr,
7058 : 0 : fold_convert (TREE_TYPE (parmse.expr),
7059 : : null_pointer_node));
7060 : : }
7061 : : }
7062 : :
7063 : : /* Scalar dummy arguments of intrinsic type or derived type with
7064 : : VALUE attribute. */
7065 : 5518 : if (fsym
7066 : 5468 : && fsym->attr.value
7067 : 234 : && fsym->ts.type != BT_CLASS)
7068 : 234 : conv_dummy_value (&parmse, e, fsym, optionalargs);
7069 : :
7070 : : /* If we are passing an absent array as optional dummy to an
7071 : : elemental procedure, make sure that we pass NULL when the data
7072 : : pointer is NULL. We need this extra conditional because of
7073 : : scalarization which passes arrays elements to the procedure,
7074 : : ignoring the fact that the array can be absent/unallocated/... */
7075 : 5284 : else if (ss->info->can_be_null_ref
7076 : 415 : && ss->info->type != GFC_SS_REFERENCE)
7077 : : {
7078 : 193 : tree descriptor_data;
7079 : :
7080 : 193 : descriptor_data = ss->info->data.array.data;
7081 : 193 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7082 : : descriptor_data,
7083 : 193 : fold_convert (TREE_TYPE (descriptor_data),
7084 : : null_pointer_node));
7085 : 193 : parmse.expr
7086 : 386 : = fold_build3_loc (input_location, COND_EXPR,
7087 : 193 : TREE_TYPE (parmse.expr),
7088 : : gfc_unlikely (tmp, PRED_FORTRAN_ABSENT_DUMMY),
7089 : 193 : fold_convert (TREE_TYPE (parmse.expr),
7090 : : null_pointer_node),
7091 : : parmse.expr);
7092 : : }
7093 : :
7094 : : /* The scalarizer does not repackage the reference to a class
7095 : : array - instead it returns a pointer to the data element. */
7096 : 5518 : if (fsym && fsym->ts.type == BT_CLASS && e->ts.type == BT_CLASS)
7097 : 156 : gfc_conv_class_to_class (&parmse, e, fsym->ts, true,
7098 : 156 : fsym->attr.intent != INTENT_IN
7099 : 156 : && (CLASS_DATA (fsym)->attr.class_pointer
7100 : 12 : || CLASS_DATA (fsym)->attr.allocatable),
7101 : 156 : fsym->attr.optional
7102 : 0 : && e->expr_type == EXPR_VARIABLE
7103 : 156 : && e->symtree->n.sym->attr.optional,
7104 : 156 : CLASS_DATA (fsym)->attr.class_pointer
7105 : 156 : || CLASS_DATA (fsym)->attr.allocatable);
7106 : : }
7107 : : else
7108 : : {
7109 : 241916 : bool scalar;
7110 : 241916 : gfc_ss *argss;
7111 : :
7112 : 241916 : gfc_init_se (&parmse, NULL);
7113 : :
7114 : : /* Check whether the expression is a scalar or not; we cannot use
7115 : : e->rank as it can be nonzero for functions arguments. */
7116 : 241916 : argss = gfc_walk_expr (e);
7117 : 241916 : scalar = argss == gfc_ss_terminator;
7118 : 241916 : if (!scalar)
7119 : 59644 : gfc_free_ss_chain (argss);
7120 : :
7121 : : /* Special handling for passing scalar polymorphic coarrays;
7122 : : otherwise one passes "class->_data.data" instead of "&class". */
7123 : 241916 : if (e->rank == 0 && e->ts.type == BT_CLASS
7124 : 3518 : && fsym && fsym->ts.type == BT_CLASS
7125 : 3096 : && CLASS_DATA (fsym)->attr.codimension
7126 : 51 : && !CLASS_DATA (fsym)->attr.dimension)
7127 : : {
7128 : 51 : gfc_add_class_array_ref (e);
7129 : 51 : parmse.want_coarray = 1;
7130 : 51 : scalar = false;
7131 : : }
7132 : :
7133 : : /* A scalar or transformational function. */
7134 : 241916 : if (scalar)
7135 : : {
7136 : 182221 : if (e->expr_type == EXPR_VARIABLE
7137 : 54109 : && e->symtree->n.sym->attr.cray_pointee
7138 : 390 : && fsym && fsym->attr.flavor == FL_PROCEDURE)
7139 : : {
7140 : : /* The Cray pointer needs to be converted to a pointer to
7141 : : a type given by the expression. */
7142 : 6 : gfc_conv_expr (&parmse, e);
7143 : 6 : type = build_pointer_type (TREE_TYPE (parmse.expr));
7144 : 6 : tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
7145 : 6 : parmse.expr = convert (type, tmp);
7146 : : }
7147 : :
7148 : 182215 : else if (sym->attr.is_bind_c && e && is_CFI_desc (fsym, NULL))
7149 : : /* Implement F2018, 18.3.6, list item (5), bullet point 2. */
7150 : 686 : gfc_conv_gfc_desc_to_cfi_desc (&parmse, e, fsym);
7151 : :
7152 : 181529 : else if (fsym && fsym->attr.value)
7153 : : {
7154 : 21650 : if (fsym->ts.type == BT_CHARACTER
7155 : 537 : && fsym->ts.is_c_interop
7156 : 180 : && fsym->ns->proc_name != NULL
7157 : 180 : && fsym->ns->proc_name->attr.is_bind_c)
7158 : : {
7159 : 171 : parmse.expr = NULL;
7160 : 171 : conv_scalar_char_value (fsym, &parmse, &e);
7161 : 171 : if (parmse.expr == NULL)
7162 : 165 : gfc_conv_expr (&parmse, e);
7163 : : }
7164 : : else
7165 : : {
7166 : 21479 : gfc_conv_expr (&parmse, e);
7167 : 21479 : conv_dummy_value (&parmse, e, fsym, optionalargs);
7168 : : }
7169 : : }
7170 : :
7171 : 159879 : else if (arg->name && arg->name[0] == '%')
7172 : : /* Argument list functions %VAL, %LOC and %REF are signalled
7173 : : through arg->name. */
7174 : 5822 : conv_arglist_function (&parmse, arg->expr, arg->name);
7175 : 154057 : else if ((e->expr_type == EXPR_FUNCTION)
7176 : 8171 : && ((e->value.function.esym
7177 : 2153 : && e->value.function.esym->result->attr.pointer)
7178 : 8076 : || (!e->value.function.esym
7179 : 6018 : && e->symtree->n.sym->attr.pointer))
7180 : 95 : && fsym && fsym->attr.target)
7181 : : /* Make sure the function only gets called once. */
7182 : 8 : gfc_conv_expr_reference (&parmse, e);
7183 : 154049 : else if (e->expr_type == EXPR_FUNCTION
7184 : 8163 : && e->symtree->n.sym->result
7185 : 7134 : && e->symtree->n.sym->result != e->symtree->n.sym
7186 : 136 : && e->symtree->n.sym->result->attr.proc_pointer)
7187 : : {
7188 : : /* Functions returning procedure pointers. */
7189 : 18 : gfc_conv_expr (&parmse, e);
7190 : 18 : if (fsym && fsym->attr.proc_pointer)
7191 : 6 : parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
7192 : : }
7193 : :
7194 : : else
7195 : : {
7196 : 154031 : bool defer_to_dealloc_blk = false;
7197 : 154031 : if (e->ts.type == BT_CLASS && fsym
7198 : 3455 : && fsym->ts.type == BT_CLASS
7199 : 3033 : && (!CLASS_DATA (fsym)->as
7200 : 356 : || CLASS_DATA (fsym)->as->type != AS_ASSUMED_RANK)
7201 : 2677 : && CLASS_DATA (e)->attr.codimension)
7202 : : {
7203 : 48 : gcc_assert (!CLASS_DATA (fsym)->attr.codimension);
7204 : 48 : gcc_assert (!CLASS_DATA (fsym)->as);
7205 : 48 : gfc_add_class_array_ref (e);
7206 : 48 : parmse.want_coarray = 1;
7207 : 48 : gfc_conv_expr_reference (&parmse, e);
7208 : 48 : class_scalar_coarray_to_class (&parmse, e, fsym->ts,
7209 : 48 : fsym->attr.optional
7210 : 48 : && e->expr_type == EXPR_VARIABLE);
7211 : : }
7212 : 153983 : else if (e->ts.type == BT_CLASS && fsym
7213 : 3407 : && fsym->ts.type == BT_CLASS
7214 : 2985 : && !CLASS_DATA (fsym)->as
7215 : 2629 : && !CLASS_DATA (e)->as
7216 : 2519 : && strcmp (fsym->ts.u.derived->name,
7217 : : e->ts.u.derived->name))
7218 : : {
7219 : 1602 : type = gfc_typenode_for_spec (&fsym->ts);
7220 : 1602 : var = gfc_create_var (type, fsym->name);
7221 : 1602 : gfc_conv_expr (&parmse, e);
7222 : 1602 : if (fsym->attr.optional
7223 : 153 : && e->expr_type == EXPR_VARIABLE
7224 : 153 : && e->symtree->n.sym->attr.optional)
7225 : : {
7226 : 66 : stmtblock_t block;
7227 : 66 : tree cond;
7228 : 66 : tmp = gfc_build_addr_expr (NULL_TREE, parmse.expr);
7229 : 66 : cond = fold_build2_loc (input_location, NE_EXPR,
7230 : : logical_type_node, tmp,
7231 : 66 : fold_convert (TREE_TYPE (tmp),
7232 : : null_pointer_node));
7233 : 66 : gfc_start_block (&block);
7234 : 66 : gfc_add_modify (&block, var,
7235 : : fold_build1_loc (input_location,
7236 : : VIEW_CONVERT_EXPR,
7237 : : type, parmse.expr));
7238 : 66 : gfc_add_expr_to_block (&parmse.pre,
7239 : : fold_build3_loc (input_location,
7240 : : COND_EXPR, void_type_node,
7241 : : cond, gfc_finish_block (&block),
7242 : : build_empty_stmt (input_location)));
7243 : 66 : parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
7244 : 132 : parmse.expr = build3_loc (input_location, COND_EXPR,
7245 : 66 : TREE_TYPE (parmse.expr),
7246 : : cond, parmse.expr,
7247 : 66 : fold_convert (TREE_TYPE (parmse.expr),
7248 : : null_pointer_node));
7249 : 66 : }
7250 : : else
7251 : : {
7252 : : /* Since the internal representation of unlimited
7253 : : polymorphic expressions includes an extra field
7254 : : that other class objects do not, a cast to the
7255 : : formal type does not work. */
7256 : 1536 : if (!UNLIMITED_POLY (e) && UNLIMITED_POLY (fsym))
7257 : : {
7258 : 91 : tree efield;
7259 : :
7260 : : /* Evaluate arguments just once, when they have
7261 : : side effects. */
7262 : 91 : if (TREE_SIDE_EFFECTS (parmse.expr))
7263 : : {
7264 : 25 : tree cldata, zero;
7265 : :
7266 : 25 : parmse.expr = gfc_evaluate_now (parmse.expr,
7267 : : &parmse.pre);
7268 : :
7269 : : /* Prevent memory leak, when old component
7270 : : was allocated already. */
7271 : 25 : cldata = gfc_class_data_get (parmse.expr);
7272 : 25 : zero = build_int_cst (TREE_TYPE (cldata),
7273 : : 0);
7274 : 25 : tmp = fold_build2_loc (input_location, NE_EXPR,
7275 : : logical_type_node,
7276 : : cldata, zero);
7277 : 25 : tmp = build3_v (COND_EXPR, tmp,
7278 : : gfc_call_free (cldata),
7279 : : build_empty_stmt (
7280 : : input_location));
7281 : 25 : gfc_add_expr_to_block (&parmse.finalblock,
7282 : : tmp);
7283 : 25 : gfc_add_modify (&parmse.finalblock,
7284 : : cldata, zero);
7285 : : }
7286 : :
7287 : : /* Set the _data field. */
7288 : 91 : tmp = gfc_class_data_get (var);
7289 : 91 : efield = fold_convert (TREE_TYPE (tmp),
7290 : : gfc_class_data_get (parmse.expr));
7291 : 91 : gfc_add_modify (&parmse.pre, tmp, efield);
7292 : :
7293 : : /* Set the _vptr field. */
7294 : 91 : tmp = gfc_class_vptr_get (var);
7295 : 91 : efield = fold_convert (TREE_TYPE (tmp),
7296 : : gfc_class_vptr_get (parmse.expr));
7297 : 91 : gfc_add_modify (&parmse.pre, tmp, efield);
7298 : :
7299 : : /* Set the _len field. */
7300 : 91 : tmp = gfc_class_len_get (var);
7301 : 91 : gfc_add_modify (&parmse.pre, tmp,
7302 : 91 : build_int_cst (TREE_TYPE (tmp), 0));
7303 : 91 : }
7304 : : else
7305 : : {
7306 : 1445 : tmp = fold_build1_loc (input_location,
7307 : : VIEW_CONVERT_EXPR,
7308 : : type, parmse.expr);
7309 : 1445 : gfc_add_modify (&parmse.pre, var, tmp);
7310 : 1536 : ;
7311 : : }
7312 : 1536 : parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
7313 : : }
7314 : : }
7315 : : else
7316 : : {
7317 : 152381 : gfc_conv_expr_reference (&parmse, e);
7318 : :
7319 : 152381 : gfc_symbol *dsym = fsym;
7320 : 152381 : gfc_dummy_arg *dummy;
7321 : :
7322 : : /* Use associated dummy as fallback for formal
7323 : : argument if there is no explicit interface. */
7324 : 152381 : if (dsym == NULL
7325 : 27410 : && (dummy = arg->associated_dummy)
7326 : 24916 : && dummy->intrinsicness == GFC_NON_INTRINSIC_DUMMY_ARG
7327 : 175855 : && dummy->u.non_intrinsic->sym)
7328 : : dsym = dummy->u.non_intrinsic->sym;
7329 : :
7330 : 152381 : if (dsym
7331 : 148445 : && dsym->attr.intent == INTENT_OUT
7332 : 3215 : && !dsym->attr.allocatable
7333 : 3074 : && !dsym->attr.pointer
7334 : 3056 : && e->expr_type == EXPR_VARIABLE
7335 : 3055 : && e->ref == NULL
7336 : 2948 : && e->symtree
7337 : 2948 : && e->symtree->n.sym
7338 : 2948 : && !e->symtree->n.sym->attr.dimension
7339 : 2948 : && e->ts.type != BT_CHARACTER
7340 : 2846 : && e->ts.type != BT_CLASS
7341 : 2616 : && (e->ts.type != BT_DERIVED
7342 : 492 : || (dsym->ts.type == BT_DERIVED
7343 : 492 : && e->ts.u.derived == dsym->ts.u.derived
7344 : : /* Types with allocatable components are
7345 : : excluded from clobbering because we need
7346 : : the unclobbered pointers to free the
7347 : : allocatable components in the callee.
7348 : : Same goes for finalizable types or types
7349 : : with finalizable components, we need to
7350 : : pass the unclobbered values to the
7351 : : finalization routines.
7352 : : For parameterized types, it's less clear
7353 : : but they may not have a constant size
7354 : : so better exclude them in any case. */
7355 : 477 : && !e->ts.u.derived->attr.alloc_comp
7356 : 351 : && !e->ts.u.derived->attr.pdt_type
7357 : 351 : && !gfc_is_finalizable (e->ts.u.derived, NULL)))
7358 : 154814 : && !sym->attr.elemental)
7359 : : {
7360 : 1100 : tree var;
7361 : 1100 : var = build_fold_indirect_ref_loc (input_location,
7362 : : parmse.expr);
7363 : 1100 : tree clobber = build_clobber (TREE_TYPE (var));
7364 : 1100 : gfc_add_modify (&clobbers, var, clobber);
7365 : : }
7366 : : }
7367 : : /* Catch base objects that are not variables. */
7368 : 154031 : if (e->ts.type == BT_CLASS
7369 : 3455 : && e->expr_type != EXPR_VARIABLE
7370 : 306 : && expr && e == expr->base_expr)
7371 : 80 : base_object = build_fold_indirect_ref_loc (input_location,
7372 : : parmse.expr);
7373 : :
7374 : : /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
7375 : : allocated on entry, it must be deallocated. */
7376 : 126621 : if (fsym && fsym->attr.intent == INTENT_OUT
7377 : 3144 : && (fsym->attr.allocatable
7378 : 3003 : || (fsym->ts.type == BT_CLASS
7379 : 259 : && CLASS_DATA (fsym)->attr.allocatable))
7380 : 154321 : && !is_CFI_desc (fsym, NULL))
7381 : : {
7382 : 290 : stmtblock_t block;
7383 : 290 : tree ptr;
7384 : :
7385 : 290 : defer_to_dealloc_blk = true;
7386 : :
7387 : 290 : parmse.expr = gfc_evaluate_data_ref_now (parmse.expr,
7388 : : &parmse.pre);
7389 : :
7390 : 290 : if (parmse.class_container != NULL_TREE)
7391 : 156 : parmse.class_container
7392 : 156 : = gfc_evaluate_data_ref_now (parmse.class_container,
7393 : : &parmse.pre);
7394 : :
7395 : 290 : gfc_init_block (&block);
7396 : 290 : ptr = parmse.expr;
7397 : 290 : if (e->ts.type == BT_CLASS)
7398 : 156 : ptr = gfc_class_data_get (ptr);
7399 : :
7400 : 290 : tree cls = parmse.class_container;
7401 : 290 : tmp = gfc_deallocate_scalar_with_status (ptr, NULL_TREE,
7402 : : NULL_TREE, true,
7403 : : e, e->ts, cls);
7404 : 290 : gfc_add_expr_to_block (&block, tmp);
7405 : 290 : gfc_add_modify (&block, ptr,
7406 : 290 : fold_convert (TREE_TYPE (ptr),
7407 : : null_pointer_node));
7408 : :
7409 : 290 : if (fsym->ts.type == BT_CLASS)
7410 : 149 : gfc_reset_vptr (&block, nullptr,
7411 : : build_fold_indirect_ref (parmse.expr),
7412 : 149 : fsym->ts.u.derived);
7413 : :
7414 : 290 : if (fsym->attr.optional
7415 : 42 : && e->expr_type == EXPR_VARIABLE
7416 : 42 : && e->symtree->n.sym->attr.optional)
7417 : : {
7418 : 36 : tmp = fold_build3_loc (input_location, COND_EXPR,
7419 : : void_type_node,
7420 : 18 : gfc_conv_expr_present (e->symtree->n.sym),
7421 : : gfc_finish_block (&block),
7422 : : build_empty_stmt (input_location));
7423 : : }
7424 : : else
7425 : 272 : tmp = gfc_finish_block (&block);
7426 : :
7427 : 290 : gfc_add_expr_to_block (&dealloc_blk, tmp);
7428 : : }
7429 : :
7430 : : /* A class array element needs converting back to be a
7431 : : class object, if the formal argument is a class object. */
7432 : 154031 : if (fsym && fsym->ts.type == BT_CLASS
7433 : 3057 : && e->ts.type == BT_CLASS
7434 : 3033 : && ((CLASS_DATA (fsym)->as
7435 : 356 : && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
7436 : 2677 : || CLASS_DATA (e)->attr.dimension))
7437 : : {
7438 : 466 : gfc_se class_se = parmse;
7439 : 466 : gfc_init_block (&class_se.pre);
7440 : 466 : gfc_init_block (&class_se.post);
7441 : :
7442 : 466 : gfc_conv_class_to_class (&class_se, e, fsym->ts, false,
7443 : 466 : fsym->attr.intent != INTENT_IN
7444 : 466 : && (CLASS_DATA (fsym)->attr.class_pointer
7445 : 267 : || CLASS_DATA (fsym)->attr.allocatable),
7446 : 466 : fsym->attr.optional
7447 : 198 : && e->expr_type == EXPR_VARIABLE
7448 : 664 : && e->symtree->n.sym->attr.optional,
7449 : 466 : CLASS_DATA (fsym)->attr.class_pointer
7450 : 466 : || CLASS_DATA (fsym)->attr.allocatable);
7451 : :
7452 : 466 : parmse.expr = class_se.expr;
7453 : 442 : stmtblock_t *class_pre_block = defer_to_dealloc_blk
7454 : 466 : ? &dealloc_blk
7455 : : : &parmse.pre;
7456 : 466 : gfc_add_block_to_block (class_pre_block, &class_se.pre);
7457 : 466 : gfc_add_block_to_block (&parmse.post, &class_se.post);
7458 : : }
7459 : :
7460 : 126621 : if (fsym && (fsym->ts.type == BT_DERIVED
7461 : 114990 : || fsym->ts.type == BT_ASSUMED)
7462 : 12482 : && e->ts.type == BT_CLASS
7463 : 410 : && !CLASS_DATA (e)->attr.dimension
7464 : 374 : && !CLASS_DATA (e)->attr.codimension)
7465 : : {
7466 : 374 : parmse.expr = gfc_class_data_get (parmse.expr);
7467 : : /* The result is a class temporary, whose _data component
7468 : : must be freed to avoid a memory leak. */
7469 : 374 : if (e->expr_type == EXPR_FUNCTION
7470 : 23 : && CLASS_DATA (e)->attr.allocatable)
7471 : : {
7472 : 19 : tree zero;
7473 : :
7474 : : /* Finalize the expression. */
7475 : 19 : gfc_finalize_tree_expr (&parmse, NULL,
7476 : : gfc_expr_attr (e), e->rank);
7477 : 19 : gfc_add_block_to_block (&parmse.post,
7478 : : &parmse.finalblock);
7479 : :
7480 : : /* Then free the class _data. */
7481 : 19 : zero = build_int_cst (TREE_TYPE (parmse.expr), 0);
7482 : 19 : tmp = fold_build2_loc (input_location, NE_EXPR,
7483 : : logical_type_node,
7484 : : parmse.expr, zero);
7485 : 19 : tmp = build3_v (COND_EXPR, tmp,
7486 : : gfc_call_free (parmse.expr),
7487 : : build_empty_stmt (input_location));
7488 : 19 : gfc_add_expr_to_block (&parmse.post, tmp);
7489 : 19 : gfc_add_modify (&parmse.post, parmse.expr, zero);
7490 : : }
7491 : : }
7492 : :
7493 : : /* Wrap scalar variable in a descriptor. We need to convert
7494 : : the address of a pointer back to the pointer itself before,
7495 : : we can assign it to the data field. */
7496 : :
7497 : 126621 : if (fsym && fsym->as && fsym->as->type == AS_ASSUMED_RANK
7498 : 1293 : && fsym->ts.type != BT_CLASS && e->expr_type != EXPR_NULL)
7499 : : {
7500 : 1221 : tmp = parmse.expr;
7501 : 1221 : if (TREE_CODE (tmp) == ADDR_EXPR)
7502 : 715 : tmp = TREE_OPERAND (tmp, 0);
7503 : 1221 : parmse.expr = gfc_conv_scalar_to_descriptor (&parmse, tmp,
7504 : : fsym->attr);
7505 : 1221 : parmse.expr = gfc_build_addr_expr (NULL_TREE,
7506 : : parmse.expr);
7507 : : }
7508 : 125400 : else if (fsym && e->expr_type != EXPR_NULL
7509 : 125102 : && ((fsym->attr.pointer
7510 : 1734 : && fsym->attr.flavor != FL_PROCEDURE)
7511 : 123374 : || (fsym->attr.proc_pointer
7512 : 157 : && !(e->expr_type == EXPR_VARIABLE
7513 : 157 : && e->symtree->n.sym->attr.dummy))
7514 : 123229 : || (fsym->attr.proc_pointer
7515 : 12 : && e->expr_type == EXPR_VARIABLE
7516 : 12 : && gfc_is_proc_ptr_comp (e))
7517 : 123223 : || (fsym->attr.allocatable
7518 : 1007 : && fsym->attr.flavor != FL_PROCEDURE)))
7519 : : {
7520 : : /* Scalar pointer dummy args require an extra level of
7521 : : indirection. The null pointer already contains
7522 : : this level of indirection. */
7523 : 2880 : parm_kind = SCALAR_POINTER;
7524 : 2880 : parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
7525 : : }
7526 : : }
7527 : : }
7528 : 59695 : else if (e->ts.type == BT_CLASS
7529 : 2567 : && fsym && fsym->ts.type == BT_CLASS
7530 : 2221 : && (CLASS_DATA (fsym)->attr.dimension
7531 : 51 : || CLASS_DATA (fsym)->attr.codimension))
7532 : : {
7533 : : /* Pass a class array. */
7534 : 2221 : parmse.use_offset = 1;
7535 : 2221 : gfc_conv_expr_descriptor (&parmse, e);
7536 : 2221 : bool defer_to_dealloc_blk = false;
7537 : :
7538 : 2221 : if (fsym->attr.optional
7539 : 798 : && e->expr_type == EXPR_VARIABLE
7540 : 798 : && e->symtree->n.sym->attr.optional)
7541 : : {
7542 : 438 : stmtblock_t block;
7543 : :
7544 : 438 : gfc_init_block (&block);
7545 : 438 : gfc_add_block_to_block (&block, &parmse.pre);
7546 : :
7547 : 876 : tree t = fold_build3_loc (input_location, COND_EXPR,
7548 : : void_type_node,
7549 : 438 : gfc_conv_expr_present (e->symtree->n.sym),
7550 : : gfc_finish_block (&block),
7551 : : build_empty_stmt (input_location));
7552 : :
7553 : 438 : gfc_add_expr_to_block (&parmse.pre, t);
7554 : : }
7555 : :
7556 : : /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
7557 : : allocated on entry, it must be deallocated. */
7558 : 2221 : if (fsym->attr.intent == INTENT_OUT
7559 : 141 : && CLASS_DATA (fsym)->attr.allocatable)
7560 : : {
7561 : 110 : stmtblock_t block;
7562 : 110 : tree ptr;
7563 : :
7564 : : /* In case the data reference to deallocate is dependent on
7565 : : its own content, save the resulting pointer to a variable
7566 : : and only use that variable from now on, before the
7567 : : expression becomes invalid. */
7568 : 110 : parmse.expr = gfc_evaluate_data_ref_now (parmse.expr,
7569 : : &parmse.pre);
7570 : :
7571 : 110 : if (parmse.class_container != NULL_TREE)
7572 : 110 : parmse.class_container
7573 : 110 : = gfc_evaluate_data_ref_now (parmse.class_container,
7574 : : &parmse.pre);
7575 : :
7576 : 110 : gfc_init_block (&block);
7577 : 110 : ptr = parmse.expr;
7578 : 110 : ptr = gfc_class_data_get (ptr);
7579 : :
7580 : 110 : tree cls = parmse.class_container;
7581 : 110 : tmp = gfc_deallocate_with_status (ptr, NULL_TREE,
7582 : : NULL_TREE, NULL_TREE,
7583 : : NULL_TREE, true, e,
7584 : : GFC_CAF_COARRAY_NOCOARRAY,
7585 : : cls);
7586 : 110 : gfc_add_expr_to_block (&block, tmp);
7587 : 110 : tmp = fold_build2_loc (input_location, MODIFY_EXPR,
7588 : : void_type_node, ptr,
7589 : : null_pointer_node);
7590 : 110 : gfc_add_expr_to_block (&block, tmp);
7591 : 110 : gfc_reset_vptr (&block, e, parmse.class_container);
7592 : :
7593 : 110 : if (fsym->attr.optional
7594 : 30 : && e->expr_type == EXPR_VARIABLE
7595 : 30 : && (!e->ref
7596 : 30 : || (e->ref->type == REF_ARRAY
7597 : 0 : && e->ref->u.ar.type != AR_FULL))
7598 : 0 : && e->symtree->n.sym->attr.optional)
7599 : : {
7600 : 0 : tmp = fold_build3_loc (input_location, COND_EXPR,
7601 : : void_type_node,
7602 : 0 : gfc_conv_expr_present (e->symtree->n.sym),
7603 : : gfc_finish_block (&block),
7604 : : build_empty_stmt (input_location));
7605 : : }
7606 : : else
7607 : 110 : tmp = gfc_finish_block (&block);
7608 : :
7609 : 110 : gfc_add_expr_to_block (&dealloc_blk, tmp);
7610 : 110 : defer_to_dealloc_blk = true;
7611 : : }
7612 : :
7613 : 2221 : gfc_se class_se = parmse;
7614 : 2221 : gfc_init_block (&class_se.pre);
7615 : 2221 : gfc_init_block (&class_se.post);
7616 : :
7617 : : /* The conversion does not repackage the reference to a class
7618 : : array - _data descriptor. */
7619 : 2221 : gfc_conv_class_to_class (&class_se, e, fsym->ts, false,
7620 : 2221 : fsym->attr.intent != INTENT_IN
7621 : 2221 : && (CLASS_DATA (fsym)->attr.class_pointer
7622 : 1187 : || CLASS_DATA (fsym)->attr.allocatable),
7623 : 2221 : fsym->attr.optional
7624 : 798 : && e->expr_type == EXPR_VARIABLE
7625 : 3019 : && e->symtree->n.sym->attr.optional,
7626 : 2221 : CLASS_DATA (fsym)->attr.class_pointer
7627 : 2221 : || CLASS_DATA (fsym)->attr.allocatable);
7628 : :
7629 : 2221 : parmse.expr = class_se.expr;
7630 : 2111 : stmtblock_t *class_pre_block = defer_to_dealloc_blk
7631 : 2221 : ? &dealloc_blk
7632 : : : &parmse.pre;
7633 : 2221 : gfc_add_block_to_block (class_pre_block, &class_se.pre);
7634 : 2221 : gfc_add_block_to_block (&parmse.post, &class_se.post);
7635 : 2221 : }
7636 : : else
7637 : : {
7638 : : /* If the argument is a function call that may not create
7639 : : a temporary for the result, we have to check that we
7640 : : can do it, i.e. that there is no alias between this
7641 : : argument and another one. */
7642 : 57474 : if (gfc_get_noncopying_intrinsic_argument (e) != NULL)
7643 : : {
7644 : 358 : gfc_expr *iarg;
7645 : 358 : sym_intent intent;
7646 : :
7647 : 358 : if (fsym != NULL)
7648 : 349 : intent = fsym->attr.intent;
7649 : : else
7650 : : intent = INTENT_UNKNOWN;
7651 : :
7652 : 358 : if (gfc_check_fncall_dependency (e, intent, sym, args,
7653 : : NOT_ELEMENTAL))
7654 : 21 : parmse.force_tmp = 1;
7655 : :
7656 : 358 : iarg = e->value.function.actual->expr;
7657 : :
7658 : : /* Temporary needed if aliasing due to host association. */
7659 : 358 : if (sym->attr.contained
7660 : 114 : && !sym->attr.pure
7661 : 114 : && !sym->attr.implicit_pure
7662 : 36 : && !sym->attr.use_assoc
7663 : 36 : && iarg->expr_type == EXPR_VARIABLE
7664 : 36 : && sym->ns == iarg->symtree->n.sym->ns)
7665 : 36 : parmse.force_tmp = 1;
7666 : :
7667 : : /* Ditto within module. */
7668 : 358 : if (sym->attr.use_assoc
7669 : 6 : && !sym->attr.pure
7670 : 6 : && !sym->attr.implicit_pure
7671 : 0 : && iarg->expr_type == EXPR_VARIABLE
7672 : 0 : && sym->module == iarg->symtree->n.sym->module)
7673 : 0 : parmse.force_tmp = 1;
7674 : : }
7675 : :
7676 : : /* Special case for assumed-rank arrays: when passing an
7677 : : argument to a nonallocatable/nonpointer dummy, the bounds have
7678 : : to be reset as otherwise a last-dim ubound of -1 is
7679 : : indistinguishable from an assumed-size array in the callee. */
7680 : 57474 : if (!sym->attr.is_bind_c && e && fsym && fsym->as
7681 : 34060 : && fsym->as->type == AS_ASSUMED_RANK
7682 : 11839 : && e->rank != -1
7683 : 11550 : && e->expr_type == EXPR_VARIABLE
7684 : 11109 : && ((fsym->ts.type == BT_CLASS
7685 : 0 : && !CLASS_DATA (fsym)->attr.class_pointer
7686 : 0 : && !CLASS_DATA (fsym)->attr.allocatable)
7687 : 11109 : || (fsym->ts.type != BT_CLASS
7688 : 11109 : && !fsym->attr.pointer && !fsym->attr.allocatable)))
7689 : : {
7690 : : /* Change AR_FULL to a (:,:,:) ref to force bounds update. */
7691 : 10566 : gfc_ref *ref;
7692 : 10812 : for (ref = e->ref; ref->next; ref = ref->next)
7693 : : {
7694 : 318 : if (ref->next->type == REF_INQUIRY)
7695 : : break;
7696 : 270 : if (ref->type == REF_ARRAY
7697 : 24 : && ref->u.ar.type != AR_ELEMENT)
7698 : : break;
7699 : 10566 : };
7700 : 10566 : if (ref->u.ar.type == AR_FULL
7701 : 9840 : && ref->u.ar.as->type != AS_ASSUMED_SIZE)
7702 : 9720 : ref->u.ar.type = AR_SECTION;
7703 : : }
7704 : :
7705 : 57474 : if (sym->attr.is_bind_c && e && is_CFI_desc (fsym, NULL))
7706 : : /* Implement F2018, 18.3.6, list item (5), bullet point 2. */
7707 : 5850 : gfc_conv_gfc_desc_to_cfi_desc (&parmse, e, fsym);
7708 : :
7709 : 51624 : else if (e->expr_type == EXPR_VARIABLE
7710 : 39926 : && is_subref_array (e)
7711 : 52352 : && !(fsym && fsym->attr.pointer))
7712 : : /* The actual argument is a component reference to an
7713 : : array of derived types. In this case, the argument
7714 : : is converted to a temporary, which is passed and then
7715 : : written back after the procedure call. */
7716 : 523 : gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
7717 : 481 : fsym ? fsym->attr.intent : INTENT_INOUT,
7718 : 523 : fsym && fsym->attr.pointer);
7719 : :
7720 : 51101 : else if (e->ts.type == BT_CLASS && CLASS_DATA (e)->as
7721 : 345 : && CLASS_DATA (e)->as->type == AS_ASSUMED_SIZE
7722 : 18 : && nodesc_arg && fsym->ts.type == BT_DERIVED)
7723 : : /* An assumed size class actual argument being passed to
7724 : : a 'no descriptor' formal argument just requires the
7725 : : data pointer to be passed. For class dummy arguments
7726 : : this is stored in the symbol backend decl.. */
7727 : 6 : parmse.expr = e->symtree->n.sym->backend_decl;
7728 : :
7729 : 51095 : else if (gfc_is_class_array_ref (e, NULL)
7730 : 51095 : && fsym && fsym->ts.type == BT_DERIVED)
7731 : : /* The actual argument is a component reference to an
7732 : : array of derived types. In this case, the argument
7733 : : is converted to a temporary, which is passed and then
7734 : : written back after the procedure call.
7735 : : OOP-TODO: Insert code so that if the dynamic type is
7736 : : the same as the declared type, copy-in/copy-out does
7737 : : not occur. */
7738 : 108 : gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
7739 : 108 : fsym->attr.intent,
7740 : 108 : fsym->attr.pointer);
7741 : :
7742 : 50987 : else if (gfc_is_class_array_function (e)
7743 : 50987 : && fsym && fsym->ts.type == BT_DERIVED)
7744 : : /* See previous comment. For function actual argument,
7745 : : the write out is not needed so the intent is set as
7746 : : intent in. */
7747 : : {
7748 : 13 : e->must_finalize = 1;
7749 : 13 : gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
7750 : 13 : INTENT_IN, fsym->attr.pointer);
7751 : : }
7752 : 47393 : else if (fsym && fsym->attr.contiguous
7753 : 60 : && (fsym->attr.target
7754 : 1674 : ? gfc_is_not_contiguous (e)
7755 : 1614 : : !gfc_is_simply_contiguous (e, false, true))
7756 : 52963 : && gfc_expr_is_variable (e))
7757 : : {
7758 : 303 : gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
7759 : 303 : fsym->attr.intent,
7760 : 303 : fsym->attr.pointer);
7761 : : }
7762 : : else
7763 : : /* This is where we introduce a temporary to store the
7764 : : result of a non-lvalue array expression. */
7765 : 50671 : gfc_conv_array_parameter (&parmse, e, nodesc_arg, fsym,
7766 : : sym->name, NULL);
7767 : :
7768 : : /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
7769 : : allocated on entry, it must be deallocated.
7770 : : CFI descriptors are handled elsewhere. */
7771 : 53851 : if (fsym && fsym->attr.allocatable
7772 : 1717 : && fsym->attr.intent == INTENT_OUT
7773 : 57237 : && !is_CFI_desc (fsym, NULL))
7774 : : {
7775 : 145 : if (fsym->ts.type == BT_DERIVED
7776 : 45 : && fsym->ts.u.derived->attr.alloc_comp)
7777 : : {
7778 : : // deallocate the components first
7779 : 9 : tmp = gfc_deallocate_alloc_comp (fsym->ts.u.derived,
7780 : : parmse.expr, e->rank);
7781 : : /* But check whether dummy argument is optional. */
7782 : 9 : if (tmp != NULL_TREE
7783 : 9 : && fsym->attr.optional
7784 : 6 : && e->expr_type == EXPR_VARIABLE
7785 : 6 : && e->symtree->n.sym->attr.optional)
7786 : : {
7787 : 6 : tree present;
7788 : 6 : present = gfc_conv_expr_present (e->symtree->n.sym);
7789 : 6 : tmp = build3_v (COND_EXPR, present, tmp,
7790 : : build_empty_stmt (input_location));
7791 : : }
7792 : 9 : if (tmp != NULL_TREE)
7793 : 9 : gfc_add_expr_to_block (&dealloc_blk, tmp);
7794 : : }
7795 : :
7796 : 145 : tmp = parmse.expr;
7797 : : /* With bind(C), the actual argument is replaced by a bind-C
7798 : : descriptor; in this case, the data component arrives here,
7799 : : which shall not be dereferenced, but still freed and
7800 : : nullified. */
7801 : 145 : if (TREE_TYPE(tmp) != pvoid_type_node)
7802 : 145 : tmp = build_fold_indirect_ref_loc (input_location,
7803 : : parmse.expr);
7804 : 145 : tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
7805 : : NULL_TREE, NULL_TREE, true,
7806 : : e,
7807 : : GFC_CAF_COARRAY_NOCOARRAY);
7808 : 145 : if (fsym->attr.optional
7809 : 48 : && e->expr_type == EXPR_VARIABLE
7810 : 48 : && e->symtree->n.sym->attr.optional)
7811 : 48 : tmp = fold_build3_loc (input_location, COND_EXPR,
7812 : : void_type_node,
7813 : 24 : gfc_conv_expr_present (e->symtree->n.sym),
7814 : : tmp, build_empty_stmt (input_location));
7815 : 145 : gfc_add_expr_to_block (&dealloc_blk, tmp);
7816 : : }
7817 : : }
7818 : : }
7819 : : /* Special case for an assumed-rank dummy argument. */
7820 : 266338 : if (!sym->attr.is_bind_c && e && fsym && e->rank > 0
7821 : 55557 : && (fsym->ts.type == BT_CLASS
7822 : 55557 : ? (CLASS_DATA (fsym)->as
7823 : 4177 : && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
7824 : 51380 : : (fsym->as && fsym->as->type == AS_ASSUMED_RANK)))
7825 : : {
7826 : 12689 : if (fsym->ts.type == BT_CLASS
7827 : 12689 : ? (CLASS_DATA (fsym)->attr.class_pointer
7828 : 1055 : || CLASS_DATA (fsym)->attr.allocatable)
7829 : 11634 : : (fsym->attr.pointer || fsym->attr.allocatable))
7830 : : {
7831 : : /* Unallocated allocatable arrays and unassociated pointer
7832 : : arrays need their dtype setting if they are argument
7833 : : associated with assumed rank dummies to set the rank. */
7834 : 891 : set_dtype_for_unallocated (&parmse, e);
7835 : : }
7836 : 11798 : else if (e->expr_type == EXPR_VARIABLE
7837 : 11319 : && e->symtree->n.sym->attr.dummy
7838 : 698 : && (e->ts.type == BT_CLASS
7839 : 891 : ? (e->ref && e->ref->next
7840 : 193 : && e->ref->next->type == REF_ARRAY
7841 : 193 : && e->ref->next->u.ar.type == AR_FULL
7842 : 386 : && e->ref->next->u.ar.as->type == AS_ASSUMED_SIZE)
7843 : 505 : : (e->ref && e->ref->type == REF_ARRAY
7844 : 505 : && e->ref->u.ar.type == AR_FULL
7845 : 733 : && e->ref->u.ar.as->type == AS_ASSUMED_SIZE)))
7846 : : {
7847 : : /* Assumed-size actual to assumed-rank dummy requires
7848 : : dim[rank-1].ubound = -1. */
7849 : 180 : tree minus_one;
7850 : 180 : tmp = build_fold_indirect_ref_loc (input_location, parmse.expr);
7851 : 180 : if (fsym->ts.type == BT_CLASS)
7852 : 60 : tmp = gfc_class_data_get (tmp);
7853 : 180 : minus_one = build_int_cst (gfc_array_index_type, -1);
7854 : 180 : gfc_conv_descriptor_ubound_set (&parmse.pre, tmp,
7855 : 180 : gfc_rank_cst[e->rank - 1],
7856 : : minus_one);
7857 : : }
7858 : : }
7859 : :
7860 : : /* The case with fsym->attr.optional is that of a user subroutine
7861 : : with an interface indicating an optional argument. When we call
7862 : : an intrinsic subroutine, however, fsym is NULL, but we might still
7863 : : have an optional argument, so we proceed to the substitution
7864 : : just in case. Arguments passed to bind(c) procedures via CFI
7865 : : descriptors are handled elsewhere. */
7866 : 252923 : if (e && (fsym == NULL || fsym->attr.optional)
7867 : 326693 : && !(sym->attr.is_bind_c && is_CFI_desc (fsym, NULL)))
7868 : : {
7869 : : /* If an optional argument is itself an optional dummy argument,
7870 : : check its presence and substitute a null if absent. This is
7871 : : only needed when passing an array to an elemental procedure
7872 : : as then array elements are accessed - or no NULL pointer is
7873 : : allowed and a "1" or "0" should be passed if not present.
7874 : : When passing a non-array-descriptor full array to a
7875 : : non-array-descriptor dummy, no check is needed. For
7876 : : array-descriptor actual to array-descriptor dummy, see
7877 : : PR 41911 for why a check has to be inserted.
7878 : : fsym == NULL is checked as intrinsics required the descriptor
7879 : : but do not always set fsym.
7880 : : Also, it is necessary to pass a NULL pointer to library routines
7881 : : which usually ignore optional arguments, so they can handle
7882 : : these themselves. */
7883 : 59261 : if (e->expr_type == EXPR_VARIABLE
7884 : 26419 : && e->symtree->n.sym->attr.optional
7885 : 2414 : && (((e->rank != 0 && elemental_proc)
7886 : 2239 : || e->representation.length || e->ts.type == BT_CHARACTER
7887 : 2013 : || (e->rank == 0 && e->symtree->n.sym->attr.value)
7888 : 1903 : || (e->rank != 0
7889 : 1069 : && (fsym == NULL
7890 : 1033 : || (fsym->as
7891 : 271 : && (fsym->as->type == AS_ASSUMED_SHAPE
7892 : 235 : || fsym->as->type == AS_ASSUMED_RANK
7893 : 117 : || fsym->as->type == AS_DEFERRED)))))
7894 : 1679 : || se->ignore_optional))
7895 : 763 : gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
7896 : 763 : e->representation.length);
7897 : : }
7898 : :
7899 : : /* Make the class container for the first argument available with class
7900 : : valued transformational functions. */
7901 : 266338 : if (argc == 0 && e && e->ts.type == BT_CLASS
7902 : 4800 : && isym && isym->transformational
7903 : 84 : && se->ss && se->ss->info)
7904 : : {
7905 : 84 : arg1_cntnr = parmse.expr;
7906 : 84 : if (POINTER_TYPE_P (TREE_TYPE (arg1_cntnr)))
7907 : 84 : arg1_cntnr = build_fold_indirect_ref_loc (input_location, arg1_cntnr);
7908 : 84 : arg1_cntnr = gfc_get_class_from_expr (arg1_cntnr);
7909 : 84 : se->ss->info->class_container = arg1_cntnr;
7910 : : }
7911 : :
7912 : 266338 : if (fsym && e)
7913 : : {
7914 : : /* Obtain the character length of an assumed character length
7915 : : length procedure from the typespec. */
7916 : 221032 : if (fsym->ts.type == BT_CHARACTER
7917 : 31478 : && parmse.string_length == NULL_TREE
7918 : 3508 : && e->ts.type == BT_PROCEDURE
7919 : 15 : && e->symtree->n.sym->ts.type == BT_CHARACTER
7920 : 15 : && e->symtree->n.sym->ts.u.cl->length != NULL
7921 : 15 : && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7922 : : {
7923 : 8 : gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
7924 : 8 : parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
7925 : : }
7926 : :
7927 : : /* Obtain the character length for a NULL() actual with a character
7928 : : MOLD argument. Otherwise substitute a suitable dummy length.
7929 : : Here we handle non-optional dummies of non-bind(c) procedures. */
7930 : 221032 : if (e->expr_type == EXPR_NULL
7931 : 745 : && fsym->ts.type == BT_CHARACTER
7932 : 296 : && !fsym->attr.optional
7933 : 221250 : && !(sym->attr.is_bind_c && is_CFI_desc (fsym, NULL)))
7934 : 216 : conv_null_actual (&parmse, e, fsym);
7935 : : }
7936 : :
7937 : : /* If any actual argument of the procedure is allocatable and passed
7938 : : to an allocatable dummy with INTENT(OUT), we conservatively
7939 : : evaluate actual argument expressions before deallocations are
7940 : : performed and the procedure is executed. May create temporaries.
7941 : : This ensures we conform to F2023:15.5.3, 15.5.4. */
7942 : 252923 : if (e && fsym && force_eval_args
7943 : 1078 : && fsym->attr.intent != INTENT_OUT
7944 : 266735 : && !gfc_is_constant_expr (e))
7945 : 256 : parmse.expr = gfc_evaluate_now (parmse.expr, &parmse.pre);
7946 : :
7947 : 266338 : if (fsym && need_interface_mapping && e)
7948 : 40583 : gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
7949 : :
7950 : 266338 : gfc_add_block_to_block (&se->pre, &parmse.pre);
7951 : 266338 : gfc_add_block_to_block (&post, &parmse.post);
7952 : 266338 : gfc_add_block_to_block (&se->finalblock, &parmse.finalblock);
7953 : :
7954 : : /* Allocated allocatable components of derived types must be
7955 : : deallocated for non-variable scalars, array arguments to elemental
7956 : : procedures, and array arguments with descriptor to non-elemental
7957 : : procedures. As bounds information for descriptorless arrays is no
7958 : : longer available here, they are dealt with in trans-array.cc
7959 : : (gfc_conv_array_parameter). */
7960 : 252923 : if (e && (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS)
7961 : 26962 : && e->ts.u.derived->attr.alloc_comp
7962 : 7399 : && (e->rank == 0 || elemental_proc || !nodesc_arg)
7963 : 273619 : && !expr_may_alias_variables (e, elemental_proc))
7964 : : {
7965 : 328 : int parm_rank;
7966 : : /* It is known the e returns a structure type with at least one
7967 : : allocatable component. When e is a function, ensure that the
7968 : : function is called once only by using a temporary variable. */
7969 : 328 : if (!DECL_P (parmse.expr) && e->expr_type == EXPR_FUNCTION)
7970 : 139 : parmse.expr = gfc_evaluate_now_loc (input_location,
7971 : : parmse.expr, &se->pre);
7972 : :
7973 : 328 : if ((fsym && fsym->attr.value) || e->expr_type == EXPR_ARRAY)
7974 : 116 : tmp = parmse.expr;
7975 : : else
7976 : 212 : tmp = build_fold_indirect_ref_loc (input_location,
7977 : : parmse.expr);
7978 : :
7979 : 328 : parm_rank = e->rank;
7980 : 328 : switch (parm_kind)
7981 : : {
7982 : : case (ELEMENTAL):
7983 : : case (SCALAR):
7984 : 328 : parm_rank = 0;
7985 : : break;
7986 : :
7987 : 0 : case (SCALAR_POINTER):
7988 : 0 : tmp = build_fold_indirect_ref_loc (input_location,
7989 : : tmp);
7990 : 0 : break;
7991 : : }
7992 : :
7993 : 328 : if (e->ts.type == BT_DERIVED && fsym && fsym->ts.type == BT_CLASS)
7994 : : {
7995 : : /* The derived type is passed to gfc_deallocate_alloc_comp.
7996 : : Therefore, class actuals can be handled correctly but derived
7997 : : types passed to class formals need the _data component. */
7998 : 81 : tmp = gfc_class_data_get (tmp);
7999 : 81 : if (!CLASS_DATA (fsym)->attr.dimension)
8000 : : {
8001 : 55 : if (UNLIMITED_POLY (fsym))
8002 : : {
8003 : 12 : tree type = gfc_typenode_for_spec (&e->ts);
8004 : 12 : type = build_pointer_type (type);
8005 : 12 : tmp = fold_convert (type, tmp);
8006 : : }
8007 : 55 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
8008 : : }
8009 : : }
8010 : :
8011 : 328 : if (e->expr_type == EXPR_OP
8012 : 24 : && e->value.op.op == INTRINSIC_PARENTHESES
8013 : 24 : && e->value.op.op1->expr_type == EXPR_VARIABLE)
8014 : : {
8015 : 24 : tree local_tmp;
8016 : 24 : local_tmp = gfc_evaluate_now (tmp, &se->pre);
8017 : 24 : local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp,
8018 : : parm_rank, 0);
8019 : 24 : gfc_add_expr_to_block (&se->post, local_tmp);
8020 : : }
8021 : :
8022 : : /* Items of array expressions passed to a polymorphic formal arguments
8023 : : create their own clean up, so prevent double free. */
8024 : 328 : if (!finalized && !e->must_finalize
8025 : 327 : && !(e->expr_type == EXPR_ARRAY && fsym
8026 : 50 : && fsym->ts.type == BT_CLASS))
8027 : : {
8028 : 307 : bool scalar_res_outside_loop;
8029 : 909 : scalar_res_outside_loop = e->expr_type == EXPR_FUNCTION
8030 : 150 : && parm_rank == 0
8031 : 445 : && parmse.loop;
8032 : :
8033 : : /* Scalars passed to an assumed rank argument are converted to
8034 : : a descriptor. Obtain the data field before deallocating any
8035 : : allocatable components. */
8036 : 278 : if (parm_rank == 0 && e->expr_type != EXPR_ARRAY
8037 : 560 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
8038 : 19 : tmp = gfc_conv_descriptor_data_get (tmp);
8039 : :
8040 : 307 : if (scalar_res_outside_loop)
8041 : : {
8042 : : /* Go through the ss chain to find the argument and use
8043 : : the stored value. */
8044 : 30 : gfc_ss *tmp_ss = parmse.loop->ss;
8045 : 72 : for (; tmp_ss; tmp_ss = tmp_ss->next)
8046 : 60 : if (tmp_ss->info
8047 : 48 : && tmp_ss->info->expr == e
8048 : 18 : && tmp_ss->info->data.scalar.value != NULL_TREE)
8049 : : {
8050 : 18 : tmp = tmp_ss->info->data.scalar.value;
8051 : 18 : break;
8052 : : }
8053 : : }
8054 : :
8055 : 307 : STRIP_NOPS (tmp);
8056 : :
8057 : 307 : if (derived_array != NULL_TREE)
8058 : 0 : tmp = gfc_deallocate_alloc_comp (e->ts.u.derived,
8059 : : derived_array,
8060 : : parm_rank);
8061 : 307 : else if ((e->ts.type == BT_CLASS
8062 : 24 : && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
8063 : 307 : || e->ts.type == BT_DERIVED)
8064 : 307 : tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp,
8065 : : parm_rank);
8066 : 0 : else if (e->ts.type == BT_CLASS)
8067 : 0 : tmp = gfc_deallocate_alloc_comp (CLASS_DATA (e)->ts.u.derived,
8068 : : tmp, parm_rank);
8069 : :
8070 : 307 : if (scalar_res_outside_loop)
8071 : 30 : gfc_add_expr_to_block (&parmse.loop->post, tmp);
8072 : : else
8073 : 277 : gfc_prepend_expr_to_block (&post, tmp);
8074 : : }
8075 : : }
8076 : :
8077 : : /* Add argument checking of passing an unallocated/NULL actual to
8078 : : a nonallocatable/nonpointer dummy. */
8079 : :
8080 : 266338 : if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
8081 : : {
8082 : 6534 : symbol_attribute attr;
8083 : 6534 : char *msg;
8084 : 6534 : tree cond;
8085 : 6534 : tree tmp;
8086 : 6534 : symbol_attribute fsym_attr;
8087 : :
8088 : 6534 : if (fsym)
8089 : : {
8090 : 6373 : if (fsym->ts.type == BT_CLASS)
8091 : : {
8092 : 321 : fsym_attr = CLASS_DATA (fsym)->attr;
8093 : 321 : fsym_attr.pointer = fsym_attr.class_pointer;
8094 : : }
8095 : : else
8096 : 6052 : fsym_attr = fsym->attr;
8097 : : }
8098 : :
8099 : 6534 : if (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
8100 : 4085 : attr = gfc_expr_attr (e);
8101 : : else
8102 : 6070 : goto end_pointer_check;
8103 : :
8104 : : /* In Fortran 2008 it's allowed to pass a NULL pointer/nonallocated
8105 : : allocatable to an optional dummy, cf. 12.5.2.12. */
8106 : 4085 : if (fsym != NULL && fsym->attr.optional && !attr.proc_pointer
8107 : 1038 : && (gfc_option.allow_std & GFC_STD_F2008) != 0)
8108 : 1032 : goto end_pointer_check;
8109 : :
8110 : 3053 : if (attr.optional)
8111 : : {
8112 : : /* If the actual argument is an optional pointer/allocatable and
8113 : : the formal argument takes an nonpointer optional value,
8114 : : it is invalid to pass a non-present argument on, even
8115 : : though there is no technical reason for this in gfortran.
8116 : : See Fortran 2003, Section 12.4.1.6 item (7)+(8). */
8117 : 60 : tree present, null_ptr, type;
8118 : :
8119 : 60 : if (attr.allocatable
8120 : 0 : && (fsym == NULL || !fsym_attr.allocatable))
8121 : 0 : msg = xasprintf ("Allocatable actual argument '%s' is not "
8122 : : "allocated or not present",
8123 : 0 : e->symtree->n.sym->name);
8124 : 60 : else if (attr.pointer
8125 : 12 : && (fsym == NULL || !fsym_attr.pointer))
8126 : 12 : msg = xasprintf ("Pointer actual argument '%s' is not "
8127 : : "associated or not present",
8128 : 12 : e->symtree->n.sym->name);
8129 : 48 : else if (attr.proc_pointer && !e->value.function.actual
8130 : 0 : && (fsym == NULL || !fsym_attr.proc_pointer))
8131 : 0 : msg = xasprintf ("Proc-pointer actual argument '%s' is not "
8132 : : "associated or not present",
8133 : 0 : e->symtree->n.sym->name);
8134 : : else
8135 : 48 : goto end_pointer_check;
8136 : :
8137 : 12 : present = gfc_conv_expr_present (e->symtree->n.sym);
8138 : 12 : type = TREE_TYPE (present);
8139 : 12 : present = fold_build2_loc (input_location, EQ_EXPR,
8140 : : logical_type_node, present,
8141 : : fold_convert (type,
8142 : : null_pointer_node));
8143 : 12 : type = TREE_TYPE (parmse.expr);
8144 : 12 : null_ptr = fold_build2_loc (input_location, EQ_EXPR,
8145 : : logical_type_node, parmse.expr,
8146 : : fold_convert (type,
8147 : : null_pointer_node));
8148 : 12 : cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
8149 : : logical_type_node, present, null_ptr);
8150 : : }
8151 : : else
8152 : : {
8153 : 2993 : if (attr.allocatable
8154 : 256 : && (fsym == NULL || !fsym_attr.allocatable))
8155 : 190 : msg = xasprintf ("Allocatable actual argument '%s' is not "
8156 : 190 : "allocated", e->symtree->n.sym->name);
8157 : 2803 : else if (attr.pointer
8158 : 265 : && (fsym == NULL || !fsym_attr.pointer))
8159 : 184 : msg = xasprintf ("Pointer actual argument '%s' is not "
8160 : 184 : "associated", e->symtree->n.sym->name);
8161 : 2619 : else if (attr.proc_pointer && !e->value.function.actual
8162 : 78 : && (fsym == NULL || !fsym_attr.proc_pointer))
8163 : 78 : msg = xasprintf ("Proc-pointer actual argument '%s' is not "
8164 : 78 : "associated", e->symtree->n.sym->name);
8165 : : else
8166 : 2541 : goto end_pointer_check;
8167 : :
8168 : 452 : tmp = parmse.expr;
8169 : 452 : if (fsym && fsym->ts.type == BT_CLASS && !attr.proc_pointer)
8170 : : {
8171 : 76 : if (POINTER_TYPE_P (TREE_TYPE (tmp)))
8172 : 70 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
8173 : 76 : tmp = gfc_class_data_get (tmp);
8174 : 76 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
8175 : 3 : tmp = gfc_conv_descriptor_data_get (tmp);
8176 : : }
8177 : :
8178 : : /* If the argument is passed by value, we need to strip the
8179 : : INDIRECT_REF. */
8180 : 452 : if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
8181 : 12 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
8182 : :
8183 : 452 : cond = fold_build2_loc (input_location, EQ_EXPR,
8184 : : logical_type_node, tmp,
8185 : 452 : fold_convert (TREE_TYPE (tmp),
8186 : : null_pointer_node));
8187 : : }
8188 : :
8189 : 464 : gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
8190 : : msg);
8191 : 464 : free (msg);
8192 : : }
8193 : 259804 : end_pointer_check:
8194 : :
8195 : : /* Deferred length dummies pass the character length by reference
8196 : : so that the value can be returned. */
8197 : 266338 : if (parmse.string_length && fsym && fsym->ts.deferred)
8198 : : {
8199 : 781 : if (INDIRECT_REF_P (parmse.string_length))
8200 : : {
8201 : : /* In chains of functions/procedure calls the string_length already
8202 : : is a pointer to the variable holding the length. Therefore
8203 : : remove the deref on call. */
8204 : 90 : tmp = parmse.string_length;
8205 : 90 : parmse.string_length = TREE_OPERAND (parmse.string_length, 0);
8206 : : }
8207 : : else
8208 : : {
8209 : 691 : tmp = parmse.string_length;
8210 : 691 : if (!VAR_P (tmp) && TREE_CODE (tmp) != COMPONENT_REF)
8211 : 61 : tmp = gfc_evaluate_now (parmse.string_length, &se->pre);
8212 : 691 : parmse.string_length = gfc_build_addr_expr (NULL_TREE, tmp);
8213 : : }
8214 : :
8215 : 781 : if (e && e->expr_type == EXPR_VARIABLE
8216 : 624 : && fsym->attr.allocatable
8217 : 360 : && e->ts.u.cl->backend_decl
8218 : 360 : && VAR_P (e->ts.u.cl->backend_decl))
8219 : : {
8220 : 276 : if (INDIRECT_REF_P (tmp))
8221 : 0 : tmp = TREE_OPERAND (tmp, 0);
8222 : 276 : gfc_add_modify (&se->post, e->ts.u.cl->backend_decl,
8223 : : fold_convert (gfc_charlen_type_node, tmp));
8224 : : }
8225 : : }
8226 : :
8227 : : /* Character strings are passed as two parameters, a length and a
8228 : : pointer - except for Bind(c) and c_ptrs which only passe the pointer.
8229 : : An unlimited polymorphic formal argument likewise does not
8230 : : need the length. */
8231 : 266338 : if (parmse.string_length != NULL_TREE
8232 : 36674 : && !sym->attr.is_bind_c
8233 : 35978 : && !(fsym && fsym->ts.type == BT_DERIVED && fsym->ts.u.derived
8234 : 6 : && fsym->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
8235 : 6 : && fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING )
8236 : 30089 : && !(fsym && fsym->ts.type == BT_ASSUMED)
8237 : 29980 : && !(fsym && UNLIMITED_POLY (fsym)))
8238 : 35688 : vec_safe_push (stringargs, parmse.string_length);
8239 : :
8240 : : /* When calling __copy for character expressions to unlimited
8241 : : polymorphic entities, the dst argument needs a string length. */
8242 : 52361 : if (sym->name[0] == '_' && e && e->ts.type == BT_CHARACTER
8243 : 5335 : && startswith (sym->name, "__vtab_CHARACTER")
8244 : 0 : && arg->next && arg->next->expr
8245 : 0 : && (arg->next->expr->ts.type == BT_DERIVED
8246 : 0 : || arg->next->expr->ts.type == BT_CLASS)
8247 : 266338 : && arg->next->expr->ts.u.derived->attr.unlimited_polymorphic)
8248 : 0 : vec_safe_push (stringargs, parmse.string_length);
8249 : :
8250 : : /* For descriptorless coarrays and assumed-shape coarray dummies, we
8251 : : pass the token and the offset as additional arguments. */
8252 : 266338 : if (fsym && e == NULL && flag_coarray == GFC_FCOARRAY_LIB
8253 : 74 : && attr->codimension && !attr->allocatable)
8254 : : {
8255 : : /* Token and offset. */
8256 : 5 : vec_safe_push (stringargs, null_pointer_node);
8257 : 5 : vec_safe_push (stringargs, build_int_cst (gfc_array_index_type, 0));
8258 : 5 : gcc_assert (fsym->attr.optional);
8259 : : }
8260 : 233384 : else if (fsym && flag_coarray == GFC_FCOARRAY_LIB && attr->codimension
8261 : 93 : && !attr->allocatable)
8262 : : {
8263 : 78 : tree caf_decl, caf_type, caf_desc = NULL_TREE;
8264 : 78 : tree offset, tmp2;
8265 : :
8266 : 78 : caf_decl = gfc_get_tree_for_caf_expr (e);
8267 : 78 : caf_type = TREE_TYPE (caf_decl);
8268 : 78 : if (POINTER_TYPE_P (caf_type)
8269 : 78 : && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_type)))
8270 : 2 : caf_desc = TREE_TYPE (caf_type);
8271 : 76 : else if (GFC_DESCRIPTOR_TYPE_P (caf_type))
8272 : : caf_desc = caf_type;
8273 : :
8274 : 32 : if (caf_desc
8275 : 32 : && (GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_ALLOCATABLE
8276 : 0 : || GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_POINTER))
8277 : : {
8278 : 64 : tmp = POINTER_TYPE_P (TREE_TYPE (caf_decl))
8279 : 34 : ? build_fold_indirect_ref (caf_decl)
8280 : : : caf_decl;
8281 : 32 : tmp = gfc_conv_descriptor_token (tmp);
8282 : : }
8283 : 46 : else if (DECL_LANG_SPECIFIC (caf_decl)
8284 : 46 : && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
8285 : 9 : tmp = GFC_DECL_TOKEN (caf_decl);
8286 : : else
8287 : : {
8288 : 37 : gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
8289 : : && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
8290 : 37 : tmp = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
8291 : : }
8292 : :
8293 : 78 : vec_safe_push (stringargs, tmp);
8294 : :
8295 : 78 : if (caf_desc
8296 : 78 : && GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_ALLOCATABLE)
8297 : 32 : offset = build_int_cst (gfc_array_index_type, 0);
8298 : 46 : else if (DECL_LANG_SPECIFIC (caf_decl)
8299 : 46 : && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
8300 : 9 : offset = GFC_DECL_CAF_OFFSET (caf_decl);
8301 : 37 : else if (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) != NULL_TREE)
8302 : 0 : offset = GFC_TYPE_ARRAY_CAF_OFFSET (caf_type);
8303 : : else
8304 : 37 : offset = build_int_cst (gfc_array_index_type, 0);
8305 : :
8306 : 78 : if (caf_desc)
8307 : : {
8308 : 64 : tmp = POINTER_TYPE_P (TREE_TYPE (caf_decl))
8309 : 34 : ? build_fold_indirect_ref (caf_decl)
8310 : : : caf_decl;
8311 : 32 : tmp = gfc_conv_descriptor_data_get (tmp);
8312 : : }
8313 : : else
8314 : : {
8315 : 46 : gcc_assert (POINTER_TYPE_P (caf_type));
8316 : 46 : tmp = caf_decl;
8317 : : }
8318 : :
8319 : 69 : tmp2 = fsym->ts.type == BT_CLASS
8320 : 78 : ? gfc_class_data_get (parmse.expr) : parmse.expr;
8321 : 78 : if ((fsym->ts.type != BT_CLASS
8322 : 69 : && (fsym->as->type == AS_ASSUMED_SHAPE
8323 : 40 : || fsym->as->type == AS_ASSUMED_RANK))
8324 : 49 : || (fsym->ts.type == BT_CLASS
8325 : 9 : && (CLASS_DATA (fsym)->as->type == AS_ASSUMED_SHAPE
8326 : 6 : || CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)))
8327 : : {
8328 : 32 : if (fsym->ts.type == BT_CLASS)
8329 : 3 : gcc_assert (!POINTER_TYPE_P (TREE_TYPE (tmp2)));
8330 : : else
8331 : : {
8332 : 29 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
8333 : 29 : tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
8334 : : }
8335 : 32 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)));
8336 : 32 : tmp2 = gfc_conv_descriptor_data_get (tmp2);
8337 : : }
8338 : 46 : else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
8339 : 6 : tmp2 = gfc_conv_descriptor_data_get (tmp2);
8340 : : else
8341 : : {
8342 : 40 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
8343 : : }
8344 : :
8345 : 78 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
8346 : : gfc_array_index_type,
8347 : : fold_convert (gfc_array_index_type, tmp2),
8348 : : fold_convert (gfc_array_index_type, tmp));
8349 : 78 : offset = fold_build2_loc (input_location, PLUS_EXPR,
8350 : : gfc_array_index_type, offset, tmp);
8351 : :
8352 : 78 : vec_safe_push (stringargs, offset);
8353 : : }
8354 : :
8355 : 266338 : vec_safe_push (arglist, parmse.expr);
8356 : : }
8357 : :
8358 : 126981 : gfc_add_block_to_block (&se->pre, &dealloc_blk);
8359 : 126981 : gfc_add_block_to_block (&se->pre, &clobbers);
8360 : 126981 : gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
8361 : :
8362 : 126981 : if (comp)
8363 : 1793 : ts = comp->ts;
8364 : 125188 : else if (sym->ts.type == BT_CLASS)
8365 : 836 : ts = CLASS_DATA (sym)->ts;
8366 : : else
8367 : 124352 : ts = sym->ts;
8368 : :
8369 : 126981 : if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
8370 : 186 : se->string_length = build_int_cst (gfc_charlen_type_node, 1);
8371 : 126795 : else if (ts.type == BT_CHARACTER)
8372 : : {
8373 : 4733 : if (ts.u.cl->length == NULL)
8374 : : {
8375 : : /* Assumed character length results are not allowed by C418 of the 2003
8376 : : standard and are trapped in resolve.cc; except in the case of SPREAD
8377 : : (and other intrinsics?) and dummy functions. In the case of SPREAD,
8378 : : we take the character length of the first argument for the result.
8379 : : For dummies, we have to look through the formal argument list for
8380 : : this function and use the character length found there.
8381 : : Likewise, we handle the case of deferred-length character dummy
8382 : : arguments to intrinsics that determine the characteristics of
8383 : : the result, which cannot be deferred-length. */
8384 : 2213 : if (expr->value.function.isym)
8385 : 1701 : ts.deferred = false;
8386 : 2213 : if (ts.deferred)
8387 : 507 : cl.backend_decl = gfc_create_var (gfc_charlen_type_node, "slen");
8388 : 1706 : else if (!sym->attr.dummy)
8389 : 1701 : cl.backend_decl = (*stringargs)[0];
8390 : : else
8391 : : {
8392 : 5 : formal = gfc_sym_get_dummy_args (sym->ns->proc_name);
8393 : 18 : for (; formal; formal = formal->next)
8394 : 8 : if (strcmp (formal->sym->name, sym->name) == 0)
8395 : 5 : cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
8396 : : }
8397 : 2213 : len = cl.backend_decl;
8398 : : }
8399 : : else
8400 : : {
8401 : 2520 : tree tmp;
8402 : :
8403 : : /* Calculate the length of the returned string. */
8404 : 2520 : gfc_init_se (&parmse, NULL);
8405 : 2520 : if (need_interface_mapping)
8406 : 1867 : gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
8407 : : else
8408 : 653 : gfc_conv_expr (&parmse, ts.u.cl->length);
8409 : 2520 : gfc_add_block_to_block (&se->pre, &parmse.pre);
8410 : 2520 : gfc_add_block_to_block (&se->post, &parmse.post);
8411 : 2520 : tmp = parmse.expr;
8412 : : /* TODO: It would be better to have the charlens as
8413 : : gfc_charlen_type_node already when the interface is
8414 : : created instead of converting it here (see PR 84615). */
8415 : 2520 : tmp = fold_build2_loc (input_location, MAX_EXPR,
8416 : : gfc_charlen_type_node,
8417 : : fold_convert (gfc_charlen_type_node, tmp),
8418 : : build_zero_cst (gfc_charlen_type_node));
8419 : 2520 : cl.backend_decl = tmp;
8420 : : }
8421 : :
8422 : : /* Set up a charlen structure for it. */
8423 : 4733 : cl.next = NULL;
8424 : 4733 : cl.length = NULL;
8425 : 4733 : ts.u.cl = &cl;
8426 : :
8427 : 4733 : len = cl.backend_decl;
8428 : : }
8429 : :
8430 : 1793 : byref = (comp && (comp->attr.dimension
8431 : 1724 : || (comp->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)))
8432 : 126981 : || (!comp && gfc_return_by_reference (sym));
8433 : :
8434 : 18533 : if (byref)
8435 : : {
8436 : 18533 : if (se->direct_byref)
8437 : : {
8438 : : /* Sometimes, too much indirection can be applied; e.g. for
8439 : : function_result = array_valued_recursive_function. */
8440 : 7206 : if (TREE_TYPE (TREE_TYPE (se->expr))
8441 : 7206 : && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
8442 : 7224 : && GFC_DESCRIPTOR_TYPE_P
8443 : : (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
8444 : 18 : se->expr = build_fold_indirect_ref_loc (input_location,
8445 : : se->expr);
8446 : :
8447 : : /* If the lhs of an assignment x = f(..) is allocatable and
8448 : : f2003 is allowed, we must do the automatic reallocation.
8449 : : TODO - deal with intrinsics, without using a temporary. */
8450 : 7206 : if (flag_realloc_lhs
8451 : 7131 : && se->ss && se->ss->loop_chain
8452 : 166 : && se->ss->loop_chain->is_alloc_lhs
8453 : 166 : && !expr->value.function.isym
8454 : 166 : && sym->result->as != NULL)
8455 : : {
8456 : : /* Evaluate the bounds of the result, if known. */
8457 : 166 : gfc_set_loop_bounds_from_array_spec (&mapping, se,
8458 : : sym->result->as);
8459 : :
8460 : : /* Perform the automatic reallocation. */
8461 : 166 : tmp = gfc_alloc_allocatable_for_assignment (se->loop,
8462 : : expr, NULL);
8463 : 166 : gfc_add_expr_to_block (&se->pre, tmp);
8464 : :
8465 : : /* Pass the temporary as the first argument. */
8466 : 166 : result = info->descriptor;
8467 : : }
8468 : : else
8469 : 7040 : result = build_fold_indirect_ref_loc (input_location,
8470 : : se->expr);
8471 : 7206 : vec_safe_push (retargs, se->expr);
8472 : : }
8473 : 11327 : else if (comp && comp->attr.dimension)
8474 : : {
8475 : 66 : gcc_assert (se->loop && info);
8476 : :
8477 : : /* Set the type of the array. vtable charlens are not always reliable.
8478 : : Use the interface, if possible. */
8479 : 66 : if (comp->ts.type == BT_CHARACTER
8480 : 1 : && expr->symtree->n.sym->ts.type == BT_CLASS
8481 : 1 : && comp->ts.interface && comp->ts.interface->result)
8482 : 1 : tmp = gfc_typenode_for_spec (&comp->ts.interface->result->ts);
8483 : : else
8484 : 65 : tmp = gfc_typenode_for_spec (&comp->ts);
8485 : 66 : gcc_assert (se->ss->dimen == se->loop->dimen);
8486 : :
8487 : : /* Evaluate the bounds of the result, if known. */
8488 : 66 : gfc_set_loop_bounds_from_array_spec (&mapping, se, comp->as);
8489 : :
8490 : : /* If the lhs of an assignment x = f(..) is allocatable and
8491 : : f2003 is allowed, we must not generate the function call
8492 : : here but should just send back the results of the mapping.
8493 : : This is signalled by the function ss being flagged. */
8494 : 66 : if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
8495 : : {
8496 : 0 : gfc_free_interface_mapping (&mapping);
8497 : 0 : return has_alternate_specifier;
8498 : : }
8499 : :
8500 : : /* Create a temporary to store the result. In case the function
8501 : : returns a pointer, the temporary will be a shallow copy and
8502 : : mustn't be deallocated. */
8503 : 66 : callee_alloc = comp->attr.allocatable || comp->attr.pointer;
8504 : 66 : gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
8505 : : tmp, NULL_TREE, false,
8506 : : !comp->attr.pointer, callee_alloc,
8507 : 66 : &se->ss->info->expr->where);
8508 : :
8509 : : /* Pass the temporary as the first argument. */
8510 : 66 : result = info->descriptor;
8511 : 66 : tmp = gfc_build_addr_expr (NULL_TREE, result);
8512 : 66 : vec_safe_push (retargs, tmp);
8513 : : }
8514 : 11194 : else if (!comp && sym->result->attr.dimension)
8515 : : {
8516 : 8288 : gcc_assert (se->loop && info);
8517 : :
8518 : : /* Set the type of the array. */
8519 : 8288 : tmp = gfc_typenode_for_spec (&ts);
8520 : 8288 : tmp = arg1_cntnr ? TREE_TYPE (arg1_cntnr) : tmp;
8521 : 8288 : gcc_assert (se->ss->dimen == se->loop->dimen);
8522 : :
8523 : : /* Evaluate the bounds of the result, if known. */
8524 : 8288 : gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
8525 : :
8526 : : /* If the lhs of an assignment x = f(..) is allocatable and
8527 : : f2003 is allowed, we must not generate the function call
8528 : : here but should just send back the results of the mapping.
8529 : : This is signalled by the function ss being flagged. */
8530 : 8288 : if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
8531 : : {
8532 : 0 : gfc_free_interface_mapping (&mapping);
8533 : 0 : return has_alternate_specifier;
8534 : : }
8535 : :
8536 : : /* Create a temporary to store the result. In case the function
8537 : : returns a pointer, the temporary will be a shallow copy and
8538 : : mustn't be deallocated. */
8539 : 8288 : callee_alloc = sym->attr.allocatable || sym->attr.pointer;
8540 : 8288 : gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
8541 : : tmp, NULL_TREE, false,
8542 : : !sym->attr.pointer, callee_alloc,
8543 : 8288 : &se->ss->info->expr->where);
8544 : :
8545 : : /* Pass the temporary as the first argument. */
8546 : 8288 : result = info->descriptor;
8547 : 8288 : tmp = gfc_build_addr_expr (NULL_TREE, result);
8548 : 8288 : vec_safe_push (retargs, tmp);
8549 : : }
8550 : 2973 : else if (ts.type == BT_CHARACTER)
8551 : : {
8552 : : /* Pass the string length. */
8553 : 2912 : type = gfc_get_character_type (ts.kind, ts.u.cl);
8554 : 2912 : type = build_pointer_type (type);
8555 : :
8556 : : /* Emit a DECL_EXPR for the VLA type. */
8557 : 2912 : tmp = TREE_TYPE (type);
8558 : 2912 : if (TYPE_SIZE (tmp)
8559 : 2912 : && TREE_CODE (TYPE_SIZE (tmp)) != INTEGER_CST)
8560 : : {
8561 : 1835 : tmp = build_decl (input_location, TYPE_DECL, NULL_TREE, tmp);
8562 : 1835 : DECL_ARTIFICIAL (tmp) = 1;
8563 : 1835 : DECL_IGNORED_P (tmp) = 1;
8564 : 1835 : tmp = fold_build1_loc (input_location, DECL_EXPR,
8565 : 1835 : TREE_TYPE (tmp), tmp);
8566 : 1835 : gfc_add_expr_to_block (&se->pre, tmp);
8567 : : }
8568 : :
8569 : : /* Return an address to a char[0:len-1]* temporary for
8570 : : character pointers. */
8571 : 2912 : if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
8572 : 67 : || (comp && (comp->attr.pointer || comp->attr.allocatable)))
8573 : : {
8574 : 550 : var = gfc_create_var (type, "pstr");
8575 : :
8576 : 550 : if ((!comp && sym->attr.allocatable)
8577 : 21 : || (comp && comp->attr.allocatable))
8578 : : {
8579 : 265 : gfc_add_modify (&se->pre, var,
8580 : 265 : fold_convert (TREE_TYPE (var),
8581 : : null_pointer_node));
8582 : 265 : tmp = gfc_call_free (var);
8583 : 265 : gfc_add_expr_to_block (&se->post, tmp);
8584 : : }
8585 : :
8586 : : /* Provide an address expression for the function arguments. */
8587 : 550 : var = gfc_build_addr_expr (NULL_TREE, var);
8588 : : }
8589 : : else
8590 : 2362 : var = gfc_conv_string_tmp (se, type, len);
8591 : :
8592 : 2912 : vec_safe_push (retargs, var);
8593 : : }
8594 : : else
8595 : : {
8596 : 61 : gcc_assert (flag_f2c && ts.type == BT_COMPLEX);
8597 : :
8598 : 61 : type = gfc_get_complex_type (ts.kind);
8599 : 61 : var = gfc_build_addr_expr (NULL_TREE, gfc_create_var (type, "cmplx"));
8600 : 61 : vec_safe_push (retargs, var);
8601 : : }
8602 : :
8603 : : /* Add the string length to the argument list. */
8604 : 18533 : if (ts.type == BT_CHARACTER && ts.deferred)
8605 : : {
8606 : 507 : tmp = len;
8607 : 507 : if (!VAR_P (tmp))
8608 : 0 : tmp = gfc_evaluate_now (len, &se->pre);
8609 : 507 : TREE_STATIC (tmp) = 1;
8610 : 507 : gfc_add_modify (&se->pre, tmp,
8611 : 507 : build_int_cst (TREE_TYPE (tmp), 0));
8612 : 507 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
8613 : 507 : vec_safe_push (retargs, tmp);
8614 : : }
8615 : 18026 : else if (ts.type == BT_CHARACTER)
8616 : 4226 : vec_safe_push (retargs, len);
8617 : : }
8618 : :
8619 : 126981 : gfc_free_interface_mapping (&mapping);
8620 : :
8621 : : /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS. */
8622 : 236742 : arglen = (vec_safe_length (arglist) + vec_safe_length (optionalargs)
8623 : 152093 : + vec_safe_length (stringargs) + vec_safe_length (append_args));
8624 : 126981 : vec_safe_reserve (retargs, arglen);
8625 : :
8626 : : /* Add the return arguments. */
8627 : 126981 : vec_safe_splice (retargs, arglist);
8628 : :
8629 : : /* Add the hidden present status for optional+value to the arguments. */
8630 : 126981 : vec_safe_splice (retargs, optionalargs);
8631 : :
8632 : : /* Add the hidden string length parameters to the arguments. */
8633 : 126981 : vec_safe_splice (retargs, stringargs);
8634 : :
8635 : : /* We may want to append extra arguments here. This is used e.g. for
8636 : : calls to libgfortran_matmul_??, which need extra information. */
8637 : 126981 : vec_safe_splice (retargs, append_args);
8638 : :
8639 : 126981 : arglist = retargs;
8640 : :
8641 : : /* Generate the actual call. */
8642 : 126981 : is_builtin = false;
8643 : 126981 : if (base_object == NULL_TREE)
8644 : 126901 : conv_function_val (se, &is_builtin, sym, expr, args);
8645 : : else
8646 : 80 : conv_base_obj_fcn_val (se, base_object, expr);
8647 : :
8648 : : /* If there are alternate return labels, function type should be
8649 : : integer. Can't modify the type in place though, since it can be shared
8650 : : with other functions. For dummy arguments, the typing is done to
8651 : : this result, even if it has to be repeated for each call. */
8652 : 126981 : if (has_alternate_specifier
8653 : 126981 : && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
8654 : : {
8655 : 7 : if (!sym->attr.dummy)
8656 : : {
8657 : 0 : TREE_TYPE (sym->backend_decl)
8658 : 0 : = build_function_type (integer_type_node,
8659 : 0 : TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
8660 : 0 : se->expr = gfc_build_addr_expr (NULL_TREE, sym->backend_decl);
8661 : : }
8662 : : else
8663 : 7 : TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
8664 : : }
8665 : :
8666 : 126981 : fntype = TREE_TYPE (TREE_TYPE (se->expr));
8667 : 126981 : se->expr = build_call_vec (TREE_TYPE (fntype), se->expr, arglist);
8668 : :
8669 : 126981 : if (is_builtin)
8670 : 507 : se->expr = update_builtin_function (se->expr, sym);
8671 : :
8672 : : /* Allocatable scalar function results must be freed and nullified
8673 : : after use. This necessitates the creation of a temporary to
8674 : : hold the result to prevent duplicate calls. */
8675 : 126981 : symbol_attribute attr = comp ? comp->attr : sym->attr;
8676 : 126981 : bool allocatable = attr.allocatable && !attr.dimension;
8677 : 129918 : gfc_symbol *der = comp ?
8678 : 1793 : comp->ts.type == BT_DERIVED ? comp->ts.u.derived : NULL
8679 : : :
8680 : 125188 : sym->ts.type == BT_DERIVED ? sym->ts.u.derived : NULL;
8681 : 2937 : bool finalizable = der != NULL && der->ns->proc_name
8682 : 5871 : && gfc_is_finalizable (der, NULL);
8683 : :
8684 : 126981 : if (!byref && finalizable)
8685 : 161 : gfc_finalize_tree_expr (se, der, attr, expr->rank);
8686 : :
8687 : 126981 : if (!byref && sym->ts.type != BT_CHARACTER
8688 : 108262 : && allocatable && !finalizable)
8689 : : {
8690 : 224 : tmp = gfc_create_var (TREE_TYPE (se->expr), NULL);
8691 : 224 : gfc_add_modify (&se->pre, tmp, se->expr);
8692 : 224 : se->expr = tmp;
8693 : 224 : tmp = gfc_call_free (tmp);
8694 : 224 : gfc_add_expr_to_block (&post, tmp);
8695 : 224 : gfc_add_modify (&post, se->expr, build_int_cst (TREE_TYPE (se->expr), 0));
8696 : : }
8697 : :
8698 : : /* If we have a pointer function, but we don't want a pointer, e.g.
8699 : : something like
8700 : : x = f()
8701 : : where f is pointer valued, we have to dereference the result. */
8702 : 126981 : if (!se->want_pointer && !byref
8703 : 107873 : && ((!comp && (sym->attr.pointer || sym->attr.allocatable))
8704 : 1613 : || (comp && (comp->attr.pointer || comp->attr.allocatable))))
8705 : 450 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
8706 : :
8707 : : /* f2c calling conventions require a scalar default real function to
8708 : : return a double precision result. Convert this back to default
8709 : : real. We only care about the cases that can happen in Fortran 77.
8710 : : */
8711 : 126981 : if (flag_f2c && sym->ts.type == BT_REAL
8712 : 97 : && sym->ts.kind == gfc_default_real_kind
8713 : 73 : && !sym->attr.pointer
8714 : 54 : && !sym->attr.allocatable
8715 : 42 : && !sym->attr.always_explicit)
8716 : 42 : se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
8717 : :
8718 : : /* A pure function may still have side-effects - it may modify its
8719 : : parameters. */
8720 : 126981 : TREE_SIDE_EFFECTS (se->expr) = 1;
8721 : : #if 0
8722 : : if (!sym->attr.pure)
8723 : : TREE_SIDE_EFFECTS (se->expr) = 1;
8724 : : #endif
8725 : :
8726 : 126981 : if (byref)
8727 : : {
8728 : : /* Add the function call to the pre chain. There is no expression. */
8729 : 18533 : gfc_add_expr_to_block (&se->pre, se->expr);
8730 : 18533 : se->expr = NULL_TREE;
8731 : :
8732 : 18533 : if (!se->direct_byref)
8733 : : {
8734 : 11327 : if ((sym->attr.dimension && !comp) || (comp && comp->attr.dimension))
8735 : : {
8736 : 8354 : if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
8737 : : {
8738 : : /* Check the data pointer hasn't been modified. This would
8739 : : happen in a function returning a pointer. */
8740 : 251 : tmp = gfc_conv_descriptor_data_get (info->descriptor);
8741 : 251 : tmp = fold_build2_loc (input_location, NE_EXPR,
8742 : : logical_type_node,
8743 : : tmp, info->data);
8744 : 251 : gfc_trans_runtime_check (true, false, tmp, &se->pre, NULL,
8745 : : gfc_msg_fault);
8746 : : }
8747 : 8354 : se->expr = info->descriptor;
8748 : : /* Bundle in the string length. */
8749 : 8354 : se->string_length = len;
8750 : :
8751 : 8354 : if (finalizable)
8752 : 6 : gfc_finalize_tree_expr (se, der, attr, expr->rank);
8753 : : }
8754 : 2973 : else if (ts.type == BT_CHARACTER)
8755 : : {
8756 : : /* Dereference for character pointer results. */
8757 : 2912 : if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
8758 : 67 : || (comp && (comp->attr.pointer || comp->attr.allocatable)))
8759 : 550 : se->expr = build_fold_indirect_ref_loc (input_location, var);
8760 : : else
8761 : 2362 : se->expr = var;
8762 : :
8763 : 2912 : se->string_length = len;
8764 : : }
8765 : : else
8766 : : {
8767 : 61 : gcc_assert (ts.type == BT_COMPLEX && flag_f2c);
8768 : 61 : se->expr = build_fold_indirect_ref_loc (input_location, var);
8769 : : }
8770 : : }
8771 : : }
8772 : :
8773 : : /* Associate the rhs class object's meta-data with the result, when the
8774 : : result is a temporary. */
8775 : 109766 : if (args && args->expr && args->expr->ts.type == BT_CLASS
8776 : 4800 : && sym->ts.type == BT_CLASS && result != NULL_TREE && DECL_P (result)
8777 : 127013 : && !GFC_CLASS_TYPE_P (TREE_TYPE (result)))
8778 : : {
8779 : 32 : gfc_se parmse;
8780 : 32 : gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (args->expr);
8781 : :
8782 : 32 : gfc_init_se (&parmse, NULL);
8783 : 32 : parmse.data_not_needed = 1;
8784 : 32 : gfc_conv_expr (&parmse, class_expr);
8785 : 32 : if (!DECL_LANG_SPECIFIC (result))
8786 : 32 : gfc_allocate_lang_decl (result);
8787 : 32 : GFC_DECL_SAVED_DESCRIPTOR (result) = parmse.expr;
8788 : 32 : gfc_free_expr (class_expr);
8789 : : /* -fcheck= can add diagnostic code, which has to be placed before
8790 : : the call. */
8791 : 32 : if (parmse.pre.head != NULL)
8792 : 12 : gfc_add_expr_to_block (&se->pre, parmse.pre.head);
8793 : 32 : gcc_assert (parmse.post.head == NULL_TREE);
8794 : : }
8795 : :
8796 : : /* Follow the function call with the argument post block. */
8797 : 126981 : if (byref)
8798 : : {
8799 : 18533 : gfc_add_block_to_block (&se->pre, &post);
8800 : :
8801 : : /* Transformational functions of derived types with allocatable
8802 : : components must have the result allocatable components copied when the
8803 : : argument is actually given. This is unnecessry for REDUCE because the
8804 : : wrapper for the OPERATION function takes care of this. */
8805 : 18533 : arg = expr->value.function.actual;
8806 : 18533 : if (result && arg && expr->rank
8807 : 14762 : && isym && isym->transformational
8808 : 13206 : && isym->id != GFC_ISYM_REDUCE
8809 : 13080 : && arg->expr
8810 : 13038 : && arg->expr->ts.type == BT_DERIVED
8811 : 221 : && arg->expr->ts.u.derived->attr.alloc_comp)
8812 : : {
8813 : 36 : tree tmp2;
8814 : : /* Copy the allocatable components. We have to use a
8815 : : temporary here to prevent source allocatable components
8816 : : from being corrupted. */
8817 : 36 : tmp2 = gfc_evaluate_now (result, &se->pre);
8818 : 36 : tmp = gfc_copy_alloc_comp (arg->expr->ts.u.derived,
8819 : : result, tmp2, expr->rank, 0);
8820 : 36 : gfc_add_expr_to_block (&se->pre, tmp);
8821 : 36 : tmp = gfc_copy_allocatable_data (result, tmp2, TREE_TYPE(tmp2),
8822 : : expr->rank);
8823 : 36 : gfc_add_expr_to_block (&se->pre, tmp);
8824 : :
8825 : : /* Finally free the temporary's data field. */
8826 : 36 : tmp = gfc_conv_descriptor_data_get (tmp2);
8827 : 36 : tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
8828 : : NULL_TREE, NULL_TREE, true,
8829 : : NULL, GFC_CAF_COARRAY_NOCOARRAY);
8830 : 36 : gfc_add_expr_to_block (&se->pre, tmp);
8831 : : }
8832 : : }
8833 : : else
8834 : : {
8835 : : /* For a function with a class array result, save the result as
8836 : : a temporary, set the info fields needed by the scalarizer and
8837 : : call the finalization function of the temporary. Note that the
8838 : : nullification of allocatable components needed by the result
8839 : : is done in gfc_trans_assignment_1. */
8840 : 33660 : if (expr && (gfc_is_class_array_function (expr)
8841 : 33351 : || gfc_is_alloc_class_scalar_function (expr))
8842 : 828 : && se->expr && GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
8843 : 109264 : && expr->must_finalize)
8844 : : {
8845 : 315 : int n;
8846 : 315 : if (se->ss && se->ss->loop)
8847 : : {
8848 : 177 : gfc_add_block_to_block (&se->ss->loop->pre, &se->pre);
8849 : 177 : se->expr = gfc_evaluate_now (se->expr, &se->ss->loop->pre);
8850 : 177 : tmp = gfc_class_data_get (se->expr);
8851 : 177 : info->descriptor = tmp;
8852 : 177 : info->data = gfc_conv_descriptor_data_get (tmp);
8853 : 177 : info->offset = gfc_conv_descriptor_offset_get (tmp);
8854 : 366 : for (n = 0; n < se->ss->loop->dimen; n++)
8855 : : {
8856 : 189 : tree dim = gfc_rank_cst[n];
8857 : 189 : se->ss->loop->to[n] = gfc_conv_descriptor_ubound_get (tmp, dim);
8858 : 189 : se->ss->loop->from[n] = gfc_conv_descriptor_lbound_get (tmp, dim);
8859 : : }
8860 : : }
8861 : : else
8862 : : {
8863 : : /* TODO Eliminate the doubling of temporaries. This
8864 : : one is necessary to ensure no memory leakage. */
8865 : 138 : se->expr = gfc_evaluate_now (se->expr, &se->pre);
8866 : : }
8867 : :
8868 : : /* Finalize the result, if necessary. */
8869 : 630 : attr = expr->value.function.esym
8870 : 315 : ? CLASS_DATA (expr->value.function.esym->result)->attr
8871 : 14 : : CLASS_DATA (expr)->attr;
8872 : 315 : if (!((gfc_is_class_array_function (expr)
8873 : 108 : || gfc_is_alloc_class_scalar_function (expr))
8874 : 315 : && attr.pointer))
8875 : 270 : gfc_finalize_tree_expr (se, NULL, attr, expr->rank);
8876 : : }
8877 : 108448 : gfc_add_block_to_block (&se->post, &post);
8878 : : }
8879 : :
8880 : : return has_alternate_specifier;
8881 : : }
8882 : :
8883 : :
8884 : : /* Fill a character string with spaces. */
8885 : :
8886 : : static tree
8887 : 28598 : fill_with_spaces (tree start, tree type, tree size)
8888 : : {
8889 : 28598 : stmtblock_t block, loop;
8890 : 28598 : tree i, el, exit_label, cond, tmp;
8891 : :
8892 : : /* For a simple char type, we can call memset(). */
8893 : 28598 : if (compare_tree_int (TYPE_SIZE_UNIT (type), 1) == 0)
8894 : 47700 : return build_call_expr_loc (input_location,
8895 : : builtin_decl_explicit (BUILT_IN_MEMSET),
8896 : : 3, start,
8897 : : build_int_cst (gfc_get_int_type (gfc_c_int_kind),
8898 : 23850 : lang_hooks.to_target_charset (' ')),
8899 : : fold_convert (size_type_node, size));
8900 : :
8901 : : /* Otherwise, we use a loop:
8902 : : for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
8903 : : *el = (type) ' ';
8904 : : */
8905 : :
8906 : : /* Initialize variables. */
8907 : 4748 : gfc_init_block (&block);
8908 : 4748 : i = gfc_create_var (sizetype, "i");
8909 : 4748 : gfc_add_modify (&block, i, fold_convert (sizetype, size));
8910 : 4748 : el = gfc_create_var (build_pointer_type (type), "el");
8911 : 4748 : gfc_add_modify (&block, el, fold_convert (TREE_TYPE (el), start));
8912 : 4748 : exit_label = gfc_build_label_decl (NULL_TREE);
8913 : 4748 : TREE_USED (exit_label) = 1;
8914 : :
8915 : :
8916 : : /* Loop body. */
8917 : 4748 : gfc_init_block (&loop);
8918 : :
8919 : : /* Exit condition. */
8920 : 4748 : cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, i,
8921 : : build_zero_cst (sizetype));
8922 : 4748 : tmp = build1_v (GOTO_EXPR, exit_label);
8923 : 4748 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
8924 : : build_empty_stmt (input_location));
8925 : 4748 : gfc_add_expr_to_block (&loop, tmp);
8926 : :
8927 : : /* Assignment. */
8928 : 4748 : gfc_add_modify (&loop,
8929 : : fold_build1_loc (input_location, INDIRECT_REF, type, el),
8930 : 4748 : build_int_cst (type, lang_hooks.to_target_charset (' ')));
8931 : :
8932 : : /* Increment loop variables. */
8933 : 4748 : gfc_add_modify (&loop, i,
8934 : : fold_build2_loc (input_location, MINUS_EXPR, sizetype, i,
8935 : 4748 : TYPE_SIZE_UNIT (type)));
8936 : 4748 : gfc_add_modify (&loop, el,
8937 : : fold_build_pointer_plus_loc (input_location,
8938 : 4748 : el, TYPE_SIZE_UNIT (type)));
8939 : :
8940 : : /* Making the loop... actually loop! */
8941 : 4748 : tmp = gfc_finish_block (&loop);
8942 : 4748 : tmp = build1_v (LOOP_EXPR, tmp);
8943 : 4748 : gfc_add_expr_to_block (&block, tmp);
8944 : :
8945 : : /* The exit label. */
8946 : 4748 : tmp = build1_v (LABEL_EXPR, exit_label);
8947 : 4748 : gfc_add_expr_to_block (&block, tmp);
8948 : :
8949 : :
8950 : 4748 : return gfc_finish_block (&block);
8951 : : }
8952 : :
8953 : :
8954 : : /* Generate code to copy a string. */
8955 : :
8956 : : void
8957 : 33521 : gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
8958 : : int dkind, tree slength, tree src, int skind)
8959 : : {
8960 : 33521 : tree tmp, dlen, slen;
8961 : 33521 : tree dsc;
8962 : 33521 : tree ssc;
8963 : 33521 : tree cond;
8964 : 33521 : tree cond2;
8965 : 33521 : tree tmp2;
8966 : 33521 : tree tmp3;
8967 : 33521 : tree tmp4;
8968 : 33521 : tree chartype;
8969 : 33521 : stmtblock_t tempblock;
8970 : :
8971 : 33521 : gcc_assert (dkind == skind);
8972 : :
8973 : 33521 : if (slength != NULL_TREE)
8974 : : {
8975 : 33521 : slen = gfc_evaluate_now (fold_convert (gfc_charlen_type_node, slength), block);
8976 : 33521 : ssc = gfc_string_to_single_character (slen, src, skind);
8977 : : }
8978 : : else
8979 : : {
8980 : 0 : slen = build_one_cst (gfc_charlen_type_node);
8981 : 0 : ssc = src;
8982 : : }
8983 : :
8984 : 33521 : if (dlength != NULL_TREE)
8985 : : {
8986 : 33521 : dlen = gfc_evaluate_now (fold_convert (gfc_charlen_type_node, dlength), block);
8987 : 33521 : dsc = gfc_string_to_single_character (dlen, dest, dkind);
8988 : : }
8989 : : else
8990 : : {
8991 : 0 : dlen = build_one_cst (gfc_charlen_type_node);
8992 : 0 : dsc = dest;
8993 : : }
8994 : :
8995 : : /* Assign directly if the types are compatible. */
8996 : 33521 : if (dsc != NULL_TREE && ssc != NULL_TREE
8997 : 33521 : && TREE_TYPE (dsc) == TREE_TYPE (ssc))
8998 : : {
8999 : 4923 : gfc_add_modify (block, dsc, ssc);
9000 : 4923 : return;
9001 : : }
9002 : :
9003 : : /* The string copy algorithm below generates code like
9004 : :
9005 : : if (destlen > 0)
9006 : : {
9007 : : if (srclen < destlen)
9008 : : {
9009 : : memmove (dest, src, srclen);
9010 : : // Pad with spaces.
9011 : : memset (&dest[srclen], ' ', destlen - srclen);
9012 : : }
9013 : : else
9014 : : {
9015 : : // Truncate if too long.
9016 : : memmove (dest, src, destlen);
9017 : : }
9018 : : }
9019 : : */
9020 : :
9021 : : /* Do nothing if the destination length is zero. */
9022 : 28598 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node, dlen,
9023 : 28598 : build_zero_cst (TREE_TYPE (dlen)));
9024 : :
9025 : : /* For non-default character kinds, we have to multiply the string
9026 : : length by the base type size. */
9027 : 28598 : chartype = gfc_get_char_type (dkind);
9028 : 28598 : slen = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (slen),
9029 : : slen,
9030 : 28598 : fold_convert (TREE_TYPE (slen),
9031 : : TYPE_SIZE_UNIT (chartype)));
9032 : 28598 : dlen = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (dlen),
9033 : : dlen,
9034 : 28598 : fold_convert (TREE_TYPE (dlen),
9035 : : TYPE_SIZE_UNIT (chartype)));
9036 : :
9037 : 28598 : if (dlength && POINTER_TYPE_P (TREE_TYPE (dest)))
9038 : 28550 : dest = fold_convert (pvoid_type_node, dest);
9039 : : else
9040 : 48 : dest = gfc_build_addr_expr (pvoid_type_node, dest);
9041 : :
9042 : 28598 : if (slength && POINTER_TYPE_P (TREE_TYPE (src)))
9043 : 28594 : src = fold_convert (pvoid_type_node, src);
9044 : : else
9045 : 4 : src = gfc_build_addr_expr (pvoid_type_node, src);
9046 : :
9047 : : /* Truncate string if source is too long. */
9048 : 28598 : cond2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node, slen,
9049 : : dlen);
9050 : :
9051 : : /* Pre-evaluate pointers unless one of the IF arms will be optimized away. */
9052 : 28598 : if (!CONSTANT_CLASS_P (cond2))
9053 : : {
9054 : 8871 : dest = gfc_evaluate_now (dest, block);
9055 : 8871 : src = gfc_evaluate_now (src, block);
9056 : : }
9057 : :
9058 : : /* Copy and pad with spaces. */
9059 : 28598 : tmp3 = build_call_expr_loc (input_location,
9060 : : builtin_decl_explicit (BUILT_IN_MEMMOVE),
9061 : : 3, dest, src,
9062 : : fold_convert (size_type_node, slen));
9063 : :
9064 : : /* Wstringop-overflow appears at -O3 even though this warning is not
9065 : : explicitly available in fortran nor can it be switched off. If the
9066 : : source length is a constant, its negative appears as a very large
9067 : : positive number and triggers the warning in BUILTIN_MEMSET. Fixing
9068 : : the result of the MINUS_EXPR suppresses this spurious warning. */
9069 : 28598 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
9070 : 28598 : TREE_TYPE(dlen), dlen, slen);
9071 : 28598 : if (slength && TREE_CONSTANT (slength))
9072 : 25238 : tmp = gfc_evaluate_now (tmp, block);
9073 : :
9074 : 28598 : tmp4 = fold_build_pointer_plus_loc (input_location, dest, slen);
9075 : 28598 : tmp4 = fill_with_spaces (tmp4, chartype, tmp);
9076 : :
9077 : 28598 : gfc_init_block (&tempblock);
9078 : 28598 : gfc_add_expr_to_block (&tempblock, tmp3);
9079 : 28598 : gfc_add_expr_to_block (&tempblock, tmp4);
9080 : 28598 : tmp3 = gfc_finish_block (&tempblock);
9081 : :
9082 : : /* The truncated memmove if the slen >= dlen. */
9083 : 28598 : tmp2 = build_call_expr_loc (input_location,
9084 : : builtin_decl_explicit (BUILT_IN_MEMMOVE),
9085 : : 3, dest, src,
9086 : : fold_convert (size_type_node, dlen));
9087 : :
9088 : : /* The whole copy_string function is there. */
9089 : 28598 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
9090 : : tmp3, tmp2);
9091 : 28598 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
9092 : : build_empty_stmt (input_location));
9093 : 28598 : gfc_add_expr_to_block (block, tmp);
9094 : : }
9095 : :
9096 : :
9097 : : /* Translate a statement function.
9098 : : The value of a statement function reference is obtained by evaluating the
9099 : : expression using the values of the actual arguments for the values of the
9100 : : corresponding dummy arguments. */
9101 : :
9102 : : static void
9103 : 269 : gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
9104 : : {
9105 : 269 : gfc_symbol *sym;
9106 : 269 : gfc_symbol *fsym;
9107 : 269 : gfc_formal_arglist *fargs;
9108 : 269 : gfc_actual_arglist *args;
9109 : 269 : gfc_se lse;
9110 : 269 : gfc_se rse;
9111 : 269 : gfc_saved_var *saved_vars;
9112 : 269 : tree *temp_vars;
9113 : 269 : tree type;
9114 : 269 : tree tmp;
9115 : 269 : int n;
9116 : :
9117 : 269 : sym = expr->symtree->n.sym;
9118 : 269 : args = expr->value.function.actual;
9119 : 269 : gfc_init_se (&lse, NULL);
9120 : 269 : gfc_init_se (&rse, NULL);
9121 : :
9122 : 269 : n = 0;
9123 : 727 : for (fargs = gfc_sym_get_dummy_args (sym); fargs; fargs = fargs->next)
9124 : 458 : n++;
9125 : 269 : saved_vars = XCNEWVEC (gfc_saved_var, n);
9126 : 269 : temp_vars = XCNEWVEC (tree, n);
9127 : :
9128 : 727 : for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
9129 : 458 : fargs = fargs->next, n++)
9130 : : {
9131 : : /* Each dummy shall be specified, explicitly or implicitly, to be
9132 : : scalar. */
9133 : 458 : gcc_assert (fargs->sym->attr.dimension == 0);
9134 : 458 : fsym = fargs->sym;
9135 : :
9136 : 458 : if (fsym->ts.type == BT_CHARACTER)
9137 : : {
9138 : : /* Copy string arguments. */
9139 : 48 : tree arglen;
9140 : :
9141 : 48 : gcc_assert (fsym->ts.u.cl && fsym->ts.u.cl->length
9142 : : && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT);
9143 : :
9144 : : /* Create a temporary to hold the value. */
9145 : 48 : if (fsym->ts.u.cl->backend_decl == NULL_TREE)
9146 : 1 : fsym->ts.u.cl->backend_decl
9147 : 1 : = gfc_conv_constant_to_tree (fsym->ts.u.cl->length);
9148 : :
9149 : 48 : type = gfc_get_character_type (fsym->ts.kind, fsym->ts.u.cl);
9150 : 48 : temp_vars[n] = gfc_create_var (type, fsym->name);
9151 : :
9152 : 48 : arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
9153 : :
9154 : 48 : gfc_conv_expr (&rse, args->expr);
9155 : 48 : gfc_conv_string_parameter (&rse);
9156 : 48 : gfc_add_block_to_block (&se->pre, &lse.pre);
9157 : 48 : gfc_add_block_to_block (&se->pre, &rse.pre);
9158 : :
9159 : 48 : gfc_trans_string_copy (&se->pre, arglen, temp_vars[n], fsym->ts.kind,
9160 : : rse.string_length, rse.expr, fsym->ts.kind);
9161 : 48 : gfc_add_block_to_block (&se->pre, &lse.post);
9162 : 48 : gfc_add_block_to_block (&se->pre, &rse.post);
9163 : : }
9164 : : else
9165 : : {
9166 : : /* For everything else, just evaluate the expression. */
9167 : :
9168 : : /* Create a temporary to hold the value. */
9169 : 410 : type = gfc_typenode_for_spec (&fsym->ts);
9170 : 410 : temp_vars[n] = gfc_create_var (type, fsym->name);
9171 : :
9172 : 410 : gfc_conv_expr (&lse, args->expr);
9173 : :
9174 : 410 : gfc_add_block_to_block (&se->pre, &lse.pre);
9175 : 410 : gfc_add_modify (&se->pre, temp_vars[n], lse.expr);
9176 : 410 : gfc_add_block_to_block (&se->pre, &lse.post);
9177 : : }
9178 : :
9179 : 458 : args = args->next;
9180 : : }
9181 : :
9182 : : /* Use the temporary variables in place of the real ones. */
9183 : 727 : for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
9184 : 458 : fargs = fargs->next, n++)
9185 : 458 : gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
9186 : :
9187 : 269 : gfc_conv_expr (se, sym->value);
9188 : :
9189 : 269 : if (sym->ts.type == BT_CHARACTER)
9190 : : {
9191 : 55 : gfc_conv_const_charlen (sym->ts.u.cl);
9192 : :
9193 : : /* Force the expression to the correct length. */
9194 : 55 : if (!INTEGER_CST_P (se->string_length)
9195 : 101 : || tree_int_cst_lt (se->string_length,
9196 : 46 : sym->ts.u.cl->backend_decl))
9197 : : {
9198 : 31 : type = gfc_get_character_type (sym->ts.kind, sym->ts.u.cl);
9199 : 31 : tmp = gfc_create_var (type, sym->name);
9200 : 31 : tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
9201 : 31 : gfc_trans_string_copy (&se->pre, sym->ts.u.cl->backend_decl, tmp,
9202 : : sym->ts.kind, se->string_length, se->expr,
9203 : : sym->ts.kind);
9204 : 31 : se->expr = tmp;
9205 : : }
9206 : 55 : se->string_length = sym->ts.u.cl->backend_decl;
9207 : : }
9208 : :
9209 : : /* Restore the original variables. */
9210 : 727 : for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
9211 : 458 : fargs = fargs->next, n++)
9212 : 458 : gfc_restore_sym (fargs->sym, &saved_vars[n]);
9213 : 269 : free (temp_vars);
9214 : 269 : free (saved_vars);
9215 : 269 : }
9216 : :
9217 : :
9218 : : /* Translate a function expression. */
9219 : :
9220 : : static void
9221 : 298238 : gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
9222 : : {
9223 : 298238 : gfc_symbol *sym;
9224 : :
9225 : 298238 : if (expr->value.function.isym)
9226 : : {
9227 : 249052 : gfc_conv_intrinsic_function (se, expr);
9228 : 249052 : return;
9229 : : }
9230 : :
9231 : : /* expr.value.function.esym is the resolved (specific) function symbol for
9232 : : most functions. However this isn't set for dummy procedures. */
9233 : 49186 : sym = expr->value.function.esym;
9234 : 49186 : if (!sym)
9235 : 1437 : sym = expr->symtree->n.sym;
9236 : :
9237 : : /* The IEEE_ARITHMETIC functions are caught here. */
9238 : 49186 : if (sym->from_intmod == INTMOD_IEEE_ARITHMETIC)
9239 : 13939 : if (gfc_conv_ieee_arithmetic_function (se, expr))
9240 : : return;
9241 : :
9242 : : /* We distinguish statement functions from general functions to improve
9243 : : runtime performance. */
9244 : 36729 : if (sym->attr.proc == PROC_ST_FUNCTION)
9245 : : {
9246 : 269 : gfc_conv_statement_function (se, expr);
9247 : 269 : return;
9248 : : }
9249 : :
9250 : 36460 : gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
9251 : : NULL);
9252 : : }
9253 : :
9254 : :
9255 : : /* Determine whether the given EXPR_CONSTANT is a zero initializer. */
9256 : :
9257 : : static bool
9258 : 38128 : is_zero_initializer_p (gfc_expr * expr)
9259 : : {
9260 : 38128 : if (expr->expr_type != EXPR_CONSTANT)
9261 : : return false;
9262 : :
9263 : : /* We ignore constants with prescribed memory representations for now. */
9264 : 11239 : if (expr->representation.string)
9265 : : return false;
9266 : :
9267 : 11221 : switch (expr->ts.type)
9268 : : {
9269 : 5109 : case BT_INTEGER:
9270 : 5109 : return mpz_cmp_si (expr->value.integer, 0) == 0;
9271 : :
9272 : 4813 : case BT_REAL:
9273 : 4813 : return mpfr_zero_p (expr->value.real)
9274 : 4813 : && MPFR_SIGN (expr->value.real) >= 0;
9275 : :
9276 : 925 : case BT_LOGICAL:
9277 : 925 : return expr->value.logical == 0;
9278 : :
9279 : 240 : case BT_COMPLEX:
9280 : 240 : return mpfr_zero_p (mpc_realref (expr->value.complex))
9281 : 154 : && MPFR_SIGN (mpc_realref (expr->value.complex)) >= 0
9282 : 154 : && mpfr_zero_p (mpc_imagref (expr->value.complex))
9283 : 382 : && MPFR_SIGN (mpc_imagref (expr->value.complex)) >= 0;
9284 : :
9285 : : default:
9286 : : break;
9287 : : }
9288 : : return false;
9289 : : }
9290 : :
9291 : :
9292 : : static void
9293 : 34543 : gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
9294 : : {
9295 : 34543 : gfc_ss *ss;
9296 : :
9297 : 34543 : ss = se->ss;
9298 : 34543 : gcc_assert (ss != NULL && ss != gfc_ss_terminator);
9299 : 34543 : gcc_assert (ss->info->expr == expr && ss->info->type == GFC_SS_CONSTRUCTOR);
9300 : :
9301 : 34543 : gfc_conv_tmp_array_ref (se);
9302 : 34543 : }
9303 : :
9304 : :
9305 : : /* Build a static initializer. EXPR is the expression for the initial value.
9306 : : The other parameters describe the variable of the component being
9307 : : initialized. EXPR may be null. */
9308 : :
9309 : : tree
9310 : 132786 : gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
9311 : : bool array, bool pointer, bool procptr)
9312 : : {
9313 : 132786 : gfc_se se;
9314 : :
9315 : 132786 : if (flag_coarray != GFC_FCOARRAY_LIB && ts->type == BT_DERIVED
9316 : 42313 : && ts->u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
9317 : 165 : && ts->u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
9318 : 57 : return build_constructor (type, NULL);
9319 : :
9320 : 132729 : if (!(expr || pointer || procptr))
9321 : : return NULL_TREE;
9322 : :
9323 : : /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
9324 : : (these are the only two iso_c_binding derived types that can be
9325 : : used as initialization expressions). If so, we need to modify
9326 : : the 'expr' to be that for a (void *). */
9327 : 124594 : if (expr != NULL && expr->ts.type == BT_DERIVED
9328 : 38138 : && expr->ts.is_iso_c && expr->ts.u.derived)
9329 : : {
9330 : 186 : if (TREE_CODE (type) == ARRAY_TYPE)
9331 : 4 : return build_constructor (type, NULL);
9332 : 182 : else if (POINTER_TYPE_P (type))
9333 : 182 : return build_int_cst (type, 0);
9334 : : else
9335 : 0 : gcc_unreachable ();
9336 : : }
9337 : :
9338 : 124408 : if (array && !procptr)
9339 : : {
9340 : 8334 : tree ctor;
9341 : : /* Arrays need special handling. */
9342 : 8334 : if (pointer)
9343 : 711 : ctor = gfc_build_null_descriptor (type);
9344 : : /* Special case assigning an array to zero. */
9345 : 7623 : else if (is_zero_initializer_p (expr))
9346 : 215 : ctor = build_constructor (type, NULL);
9347 : : else
9348 : 7408 : ctor = gfc_conv_array_initializer (type, expr);
9349 : 8334 : TREE_STATIC (ctor) = 1;
9350 : 8334 : return ctor;
9351 : : }
9352 : 116074 : else if (pointer || procptr)
9353 : : {
9354 : 56004 : if (ts->type == BT_CLASS && !procptr)
9355 : : {
9356 : 1696 : gfc_init_se (&se, NULL);
9357 : 1696 : gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
9358 : 1696 : gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
9359 : 1696 : TREE_STATIC (se.expr) = 1;
9360 : 1696 : return se.expr;
9361 : : }
9362 : 54308 : else if (!expr || expr->expr_type == EXPR_NULL)
9363 : 29519 : return fold_convert (type, null_pointer_node);
9364 : : else
9365 : : {
9366 : 24789 : gfc_init_se (&se, NULL);
9367 : 24789 : se.want_pointer = 1;
9368 : 24789 : gfc_conv_expr (&se, expr);
9369 : 24789 : gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
9370 : : return se.expr;
9371 : : }
9372 : : }
9373 : : else
9374 : : {
9375 : 60070 : switch (ts->type)
9376 : : {
9377 : 17802 : case_bt_struct:
9378 : 17802 : case BT_CLASS:
9379 : 17802 : gfc_init_se (&se, NULL);
9380 : 17802 : if (ts->type == BT_CLASS && expr->expr_type == EXPR_NULL)
9381 : 739 : gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
9382 : : else
9383 : 17063 : gfc_conv_structure (&se, expr, 1);
9384 : 17802 : gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
9385 : 17802 : TREE_STATIC (se.expr) = 1;
9386 : 17802 : return se.expr;
9387 : :
9388 : 2575 : case BT_CHARACTER:
9389 : 2575 : if (expr->expr_type == EXPR_CONSTANT)
9390 : : {
9391 : 2574 : tree ctor = gfc_conv_string_init (ts->u.cl->backend_decl, expr);
9392 : 2574 : TREE_STATIC (ctor) = 1;
9393 : 2574 : return ctor;
9394 : : }
9395 : :
9396 : : /* Fallthrough. */
9397 : 39694 : default:
9398 : 39694 : gfc_init_se (&se, NULL);
9399 : 39694 : gfc_conv_constant (&se, expr);
9400 : 39694 : gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
9401 : : return se.expr;
9402 : : }
9403 : : }
9404 : : }
9405 : :
9406 : : static tree
9407 : 933 : gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
9408 : : {
9409 : 933 : gfc_se rse;
9410 : 933 : gfc_se lse;
9411 : 933 : gfc_ss *rss;
9412 : 933 : gfc_ss *lss;
9413 : 933 : gfc_array_info *lss_array;
9414 : 933 : stmtblock_t body;
9415 : 933 : stmtblock_t block;
9416 : 933 : gfc_loopinfo loop;
9417 : 933 : int n;
9418 : 933 : tree tmp;
9419 : :
9420 : 933 : gfc_start_block (&block);
9421 : :
9422 : : /* Initialize the scalarizer. */
9423 : 933 : gfc_init_loopinfo (&loop);
9424 : :
9425 : 933 : gfc_init_se (&lse, NULL);
9426 : 933 : gfc_init_se (&rse, NULL);
9427 : :
9428 : : /* Walk the rhs. */
9429 : 933 : rss = gfc_walk_expr (expr);
9430 : 933 : if (rss == gfc_ss_terminator)
9431 : : /* The rhs is scalar. Add a ss for the expression. */
9432 : 201 : rss = gfc_get_scalar_ss (gfc_ss_terminator, expr);
9433 : :
9434 : : /* Create a SS for the destination. */
9435 : 933 : lss = gfc_get_array_ss (gfc_ss_terminator, NULL, cm->as->rank,
9436 : : GFC_SS_COMPONENT);
9437 : 933 : lss_array = &lss->info->data.array;
9438 : 933 : lss_array->shape = gfc_get_shape (cm->as->rank);
9439 : 933 : lss_array->descriptor = dest;
9440 : 933 : lss_array->data = gfc_conv_array_data (dest);
9441 : 933 : lss_array->offset = gfc_conv_array_offset (dest);
9442 : 1923 : for (n = 0; n < cm->as->rank; n++)
9443 : : {
9444 : 990 : lss_array->start[n] = gfc_conv_array_lbound (dest, n);
9445 : 990 : lss_array->stride[n] = gfc_index_one_node;
9446 : :
9447 : 990 : mpz_init (lss_array->shape[n]);
9448 : 990 : mpz_sub (lss_array->shape[n], cm->as->upper[n]->value.integer,
9449 : 990 : cm->as->lower[n]->value.integer);
9450 : 990 : mpz_add_ui (lss_array->shape[n], lss_array->shape[n], 1);
9451 : : }
9452 : :
9453 : : /* Associate the SS with the loop. */
9454 : 933 : gfc_add_ss_to_loop (&loop, lss);
9455 : 933 : gfc_add_ss_to_loop (&loop, rss);
9456 : :
9457 : : /* Calculate the bounds of the scalarization. */
9458 : 933 : gfc_conv_ss_startstride (&loop);
9459 : :
9460 : : /* Setup the scalarizing loops. */
9461 : 933 : gfc_conv_loop_setup (&loop, &expr->where);
9462 : :
9463 : : /* Setup the gfc_se structures. */
9464 : 933 : gfc_copy_loopinfo_to_se (&lse, &loop);
9465 : 933 : gfc_copy_loopinfo_to_se (&rse, &loop);
9466 : :
9467 : 933 : rse.ss = rss;
9468 : 933 : gfc_mark_ss_chain_used (rss, 1);
9469 : 933 : lse.ss = lss;
9470 : 933 : gfc_mark_ss_chain_used (lss, 1);
9471 : :
9472 : : /* Start the scalarized loop body. */
9473 : 933 : gfc_start_scalarized_body (&loop, &body);
9474 : :
9475 : 933 : gfc_conv_tmp_array_ref (&lse);
9476 : 933 : if (cm->ts.type == BT_CHARACTER)
9477 : 176 : lse.string_length = cm->ts.u.cl->backend_decl;
9478 : :
9479 : 933 : gfc_conv_expr (&rse, expr);
9480 : :
9481 : 933 : tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false);
9482 : 933 : gfc_add_expr_to_block (&body, tmp);
9483 : :
9484 : 933 : gcc_assert (rse.ss == gfc_ss_terminator);
9485 : :
9486 : : /* Generate the copying loops. */
9487 : 933 : gfc_trans_scalarizing_loops (&loop, &body);
9488 : :
9489 : : /* Wrap the whole thing up. */
9490 : 933 : gfc_add_block_to_block (&block, &loop.pre);
9491 : 933 : gfc_add_block_to_block (&block, &loop.post);
9492 : :
9493 : 933 : gcc_assert (lss_array->shape != NULL);
9494 : 933 : gfc_free_shape (&lss_array->shape, cm->as->rank);
9495 : 933 : gfc_cleanup_loop (&loop);
9496 : :
9497 : 933 : return gfc_finish_block (&block);
9498 : : }
9499 : :
9500 : :
9501 : : static tree
9502 : 968 : gfc_trans_alloc_subarray_assign (tree dest, gfc_component * cm,
9503 : : gfc_expr * expr)
9504 : : {
9505 : 968 : gfc_se se;
9506 : 968 : stmtblock_t block;
9507 : 968 : tree offset;
9508 : 968 : int n;
9509 : 968 : tree tmp;
9510 : 968 : tree tmp2;
9511 : 968 : gfc_array_spec *as;
9512 : 968 : gfc_expr *arg = NULL;
9513 : :
9514 : 968 : gfc_start_block (&block);
9515 : 968 : gfc_init_se (&se, NULL);
9516 : :
9517 : : /* Get the descriptor for the expressions. */
9518 : 968 : se.want_pointer = 0;
9519 : 968 : gfc_conv_expr_descriptor (&se, expr);
9520 : 968 : gfc_add_block_to_block (&block, &se.pre);
9521 : 968 : gfc_add_modify (&block, dest, se.expr);
9522 : 968 : if (cm->ts.type == BT_CHARACTER
9523 : 968 : && gfc_deferred_strlen (cm, &tmp))
9524 : : {
9525 : 30 : tmp = fold_build3_loc (input_location, COMPONENT_REF,
9526 : 30 : TREE_TYPE (tmp),
9527 : 30 : TREE_OPERAND (dest, 0),
9528 : : tmp, NULL_TREE);
9529 : 30 : gfc_add_modify (&block, tmp,
9530 : 30 : fold_convert (TREE_TYPE (tmp),
9531 : : se.string_length));
9532 : 30 : cm->ts.u.cl->backend_decl = gfc_create_var (gfc_charlen_type_node,
9533 : : "slen");
9534 : 30 : gfc_add_modify (&block, cm->ts.u.cl->backend_decl, se.string_length);
9535 : : }
9536 : :
9537 : : /* Deal with arrays of derived types with allocatable components. */
9538 : 968 : if (gfc_bt_struct (cm->ts.type)
9539 : 150 : && cm->ts.u.derived->attr.alloc_comp)
9540 : : // TODO: Fix caf_mode
9541 : 106 : tmp = gfc_copy_alloc_comp (cm->ts.u.derived,
9542 : : se.expr, dest,
9543 : 106 : cm->as->rank, 0);
9544 : 862 : else if (cm->ts.type == BT_CLASS && expr->ts.type == BT_DERIVED
9545 : 36 : && CLASS_DATA(cm)->attr.allocatable)
9546 : : {
9547 : 36 : if (cm->ts.u.derived->attr.alloc_comp)
9548 : : // TODO: Fix caf_mode
9549 : 0 : tmp = gfc_copy_alloc_comp (expr->ts.u.derived,
9550 : : se.expr, dest,
9551 : : expr->rank, 0);
9552 : : else
9553 : : {
9554 : 36 : tmp = TREE_TYPE (dest);
9555 : 36 : tmp = gfc_duplicate_allocatable (dest, se.expr,
9556 : : tmp, expr->rank, NULL_TREE);
9557 : : }
9558 : : }
9559 : 826 : else if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
9560 : 30 : tmp = gfc_duplicate_allocatable (dest, se.expr,
9561 : : gfc_typenode_for_spec (&cm->ts),
9562 : 30 : cm->as->rank, NULL_TREE);
9563 : : else
9564 : 796 : tmp = gfc_duplicate_allocatable (dest, se.expr,
9565 : 796 : TREE_TYPE(cm->backend_decl),
9566 : 796 : cm->as->rank, NULL_TREE);
9567 : :
9568 : :
9569 : 968 : gfc_add_expr_to_block (&block, tmp);
9570 : 968 : gfc_add_block_to_block (&block, &se.post);
9571 : :
9572 : 968 : if (expr->expr_type != EXPR_VARIABLE)
9573 : 857 : gfc_conv_descriptor_data_set (&block, se.expr,
9574 : : null_pointer_node);
9575 : :
9576 : : /* We need to know if the argument of a conversion function is a
9577 : : variable, so that the correct lower bound can be used. */
9578 : 968 : if (expr->expr_type == EXPR_FUNCTION
9579 : 50 : && expr->value.function.isym
9580 : 38 : && expr->value.function.isym->conversion
9581 : 38 : && expr->value.function.actual->expr
9582 : 38 : && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE)
9583 : 38 : arg = expr->value.function.actual->expr;
9584 : :
9585 : : /* Obtain the array spec of full array references. */
9586 : 38 : if (arg)
9587 : 38 : as = gfc_get_full_arrayspec_from_expr (arg);
9588 : : else
9589 : 930 : as = gfc_get_full_arrayspec_from_expr (expr);
9590 : :
9591 : : /* Shift the lbound and ubound of temporaries to being unity,
9592 : : rather than zero, based. Always calculate the offset. */
9593 : 968 : offset = gfc_conv_descriptor_offset_get (dest);
9594 : 968 : gfc_add_modify (&block, offset, gfc_index_zero_node);
9595 : 968 : tmp2 =gfc_create_var (gfc_array_index_type, NULL);
9596 : :
9597 : 1992 : for (n = 0; n < expr->rank; n++)
9598 : : {
9599 : 1024 : tree span;
9600 : 1024 : tree lbound;
9601 : :
9602 : : /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
9603 : : TODO It looks as if gfc_conv_expr_descriptor should return
9604 : : the correct bounds and that the following should not be
9605 : : necessary. This would simplify gfc_conv_intrinsic_bound
9606 : : as well. */
9607 : 1024 : if (as && as->lower[n])
9608 : : {
9609 : 54 : gfc_se lbse;
9610 : 54 : gfc_init_se (&lbse, NULL);
9611 : 54 : gfc_conv_expr (&lbse, as->lower[n]);
9612 : 54 : gfc_add_block_to_block (&block, &lbse.pre);
9613 : 54 : lbound = gfc_evaluate_now (lbse.expr, &block);
9614 : 54 : }
9615 : 970 : else if (as && arg)
9616 : : {
9617 : 46 : tmp = gfc_get_symbol_decl (arg->symtree->n.sym);
9618 : 46 : lbound = gfc_conv_descriptor_lbound_get (tmp,
9619 : : gfc_rank_cst[n]);
9620 : : }
9621 : 924 : else if (as)
9622 : 63 : lbound = gfc_conv_descriptor_lbound_get (dest,
9623 : : gfc_rank_cst[n]);
9624 : : else
9625 : 861 : lbound = gfc_index_one_node;
9626 : :
9627 : 1024 : lbound = fold_convert (gfc_array_index_type, lbound);
9628 : :
9629 : : /* Shift the bounds and set the offset accordingly. */
9630 : 1024 : tmp = gfc_conv_descriptor_ubound_get (dest, gfc_rank_cst[n]);
9631 : 1024 : span = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
9632 : : tmp, gfc_conv_descriptor_lbound_get (dest, gfc_rank_cst[n]));
9633 : 1024 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
9634 : : span, lbound);
9635 : 1024 : gfc_conv_descriptor_ubound_set (&block, dest,
9636 : : gfc_rank_cst[n], tmp);
9637 : 1024 : gfc_conv_descriptor_lbound_set (&block, dest,
9638 : : gfc_rank_cst[n], lbound);
9639 : :
9640 : 1024 : tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
9641 : : gfc_conv_descriptor_lbound_get (dest,
9642 : : gfc_rank_cst[n]),
9643 : : gfc_conv_descriptor_stride_get (dest,
9644 : : gfc_rank_cst[n]));
9645 : 1024 : gfc_add_modify (&block, tmp2, tmp);
9646 : 1024 : tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
9647 : : offset, tmp2);
9648 : 1024 : gfc_conv_descriptor_offset_set (&block, dest, tmp);
9649 : : }
9650 : :
9651 : 968 : if (arg)
9652 : : {
9653 : : /* If a conversion expression has a null data pointer
9654 : : argument, nullify the allocatable component. */
9655 : 38 : tree non_null_expr;
9656 : 38 : tree null_expr;
9657 : :
9658 : 38 : if (arg->symtree->n.sym->attr.allocatable
9659 : 0 : || arg->symtree->n.sym->attr.pointer)
9660 : : {
9661 : 38 : non_null_expr = gfc_finish_block (&block);
9662 : 38 : gfc_start_block (&block);
9663 : 38 : gfc_conv_descriptor_data_set (&block, dest,
9664 : : null_pointer_node);
9665 : 38 : null_expr = gfc_finish_block (&block);
9666 : 38 : tmp = gfc_conv_descriptor_data_get (arg->symtree->n.sym->backend_decl);
9667 : 38 : tmp = build2_loc (input_location, EQ_EXPR, logical_type_node, tmp,
9668 : 38 : fold_convert (TREE_TYPE (tmp), null_pointer_node));
9669 : 38 : return build3_v (COND_EXPR, tmp,
9670 : : null_expr, non_null_expr);
9671 : : }
9672 : : }
9673 : :
9674 : 930 : return gfc_finish_block (&block);
9675 : : }
9676 : :
9677 : :
9678 : : /* Allocate or reallocate scalar component, as necessary. */
9679 : :
9680 : : static void
9681 : 351 : alloc_scalar_allocatable_subcomponent (stmtblock_t *block, tree comp,
9682 : : gfc_component *cm, gfc_expr *expr2,
9683 : : tree slen)
9684 : : {
9685 : 351 : tree tmp;
9686 : 351 : tree ptr;
9687 : 351 : tree size;
9688 : 351 : tree size_in_bytes;
9689 : 351 : tree lhs_cl_size = NULL_TREE;
9690 : 351 : gfc_se se;
9691 : :
9692 : 351 : if (!comp)
9693 : 0 : return;
9694 : :
9695 : 351 : if (!expr2 || expr2->rank)
9696 : : return;
9697 : :
9698 : 351 : realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
9699 : :
9700 : 351 : if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
9701 : : {
9702 : 127 : gcc_assert (expr2->ts.type == BT_CHARACTER);
9703 : 127 : size = expr2->ts.u.cl->backend_decl;
9704 : 127 : if (!size || !VAR_P (size))
9705 : 127 : size = gfc_create_var (TREE_TYPE (slen), "slen");
9706 : 127 : gfc_add_modify (block, size, slen);
9707 : :
9708 : 127 : gfc_deferred_strlen (cm, &tmp);
9709 : 127 : lhs_cl_size = fold_build3_loc (input_location, COMPONENT_REF,
9710 : : gfc_charlen_type_node,
9711 : 127 : TREE_OPERAND (comp, 0),
9712 : : tmp, NULL_TREE);
9713 : :
9714 : 127 : tmp = TREE_TYPE (gfc_typenode_for_spec (&cm->ts));
9715 : 127 : tmp = TYPE_SIZE_UNIT (tmp);
9716 : 254 : size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
9717 : 127 : TREE_TYPE (tmp), tmp,
9718 : 127 : fold_convert (TREE_TYPE (tmp), size));
9719 : : }
9720 : 224 : else if (cm->ts.type == BT_CLASS)
9721 : : {
9722 : 78 : if (expr2->ts.type != BT_CLASS)
9723 : : {
9724 : 78 : if (expr2->ts.type == BT_CHARACTER)
9725 : : {
9726 : 18 : gfc_init_se (&se, NULL);
9727 : 18 : gfc_conv_expr (&se, expr2);
9728 : 18 : size = build_int_cst (gfc_charlen_type_node, expr2->ts.kind);
9729 : 18 : size = fold_build2_loc (input_location, MULT_EXPR,
9730 : : gfc_charlen_type_node,
9731 : : se.string_length, size);
9732 : 18 : size = fold_convert (size_type_node, size);
9733 : : }
9734 : : else
9735 : : {
9736 : 60 : if (expr2->ts.type == BT_DERIVED)
9737 : 48 : tmp = gfc_get_symbol_decl (expr2->ts.u.derived);
9738 : : else
9739 : 12 : tmp = gfc_typenode_for_spec (&expr2->ts);
9740 : 60 : size = TYPE_SIZE_UNIT (tmp);
9741 : : }
9742 : : }
9743 : : else
9744 : : {
9745 : 0 : gfc_expr *e2vtab;
9746 : 0 : e2vtab = gfc_find_and_cut_at_last_class_ref (expr2);
9747 : 0 : gfc_add_vptr_component (e2vtab);
9748 : 0 : gfc_add_size_component (e2vtab);
9749 : 0 : gfc_init_se (&se, NULL);
9750 : 0 : gfc_conv_expr (&se, e2vtab);
9751 : 0 : gfc_add_block_to_block (block, &se.pre);
9752 : 0 : size = fold_convert (size_type_node, se.expr);
9753 : 0 : gfc_free_expr (e2vtab);
9754 : : }
9755 : : size_in_bytes = size;
9756 : : }
9757 : : else
9758 : : {
9759 : : /* Otherwise use the length in bytes of the rhs. */
9760 : 146 : size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&cm->ts));
9761 : 146 : size_in_bytes = size;
9762 : : }
9763 : :
9764 : 351 : size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
9765 : : size_in_bytes, size_one_node);
9766 : :
9767 : 351 : if (cm->ts.type == BT_DERIVED && cm->ts.u.derived->attr.alloc_comp)
9768 : : {
9769 : 0 : tmp = build_call_expr_loc (input_location,
9770 : : builtin_decl_explicit (BUILT_IN_CALLOC),
9771 : : 2, build_one_cst (size_type_node),
9772 : : size_in_bytes);
9773 : 0 : tmp = fold_convert (TREE_TYPE (comp), tmp);
9774 : 0 : gfc_add_modify (block, comp, tmp);
9775 : : }
9776 : : else
9777 : : {
9778 : 351 : tmp = build_call_expr_loc (input_location,
9779 : : builtin_decl_explicit (BUILT_IN_MALLOC),
9780 : : 1, size_in_bytes);
9781 : 351 : if (GFC_CLASS_TYPE_P (TREE_TYPE (comp)))
9782 : 78 : ptr = gfc_class_data_get (comp);
9783 : : else
9784 : : ptr = comp;
9785 : 351 : tmp = fold_convert (TREE_TYPE (ptr), tmp);
9786 : 351 : gfc_add_modify (block, ptr, tmp);
9787 : : }
9788 : :
9789 : 351 : if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
9790 : : /* Update the lhs character length. */
9791 : 127 : gfc_add_modify (block, lhs_cl_size,
9792 : 127 : fold_convert (TREE_TYPE (lhs_cl_size), size));
9793 : : }
9794 : :
9795 : :
9796 : : /* Assign a single component of a derived type constructor. */
9797 : :
9798 : : static tree
9799 : 27097 : gfc_trans_subcomponent_assign (tree dest, gfc_component * cm,
9800 : : gfc_expr * expr, bool init)
9801 : : {
9802 : 27097 : gfc_se se;
9803 : 27097 : gfc_se lse;
9804 : 27097 : stmtblock_t block;
9805 : 27097 : tree tmp;
9806 : 27097 : tree vtab;
9807 : :
9808 : 27097 : gfc_start_block (&block);
9809 : :
9810 : 27097 : if (cm->attr.pointer || cm->attr.proc_pointer)
9811 : : {
9812 : : /* Only care about pointers here, not about allocatables. */
9813 : 2535 : gfc_init_se (&se, NULL);
9814 : : /* Pointer component. */
9815 : 2535 : if ((cm->attr.dimension || cm->attr.codimension)
9816 : 646 : && !cm->attr.proc_pointer)
9817 : : {
9818 : : /* Array pointer. */
9819 : 630 : if (expr->expr_type == EXPR_NULL)
9820 : 624 : gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
9821 : : else
9822 : : {
9823 : 6 : se.direct_byref = 1;
9824 : 6 : se.expr = dest;
9825 : 6 : gfc_conv_expr_descriptor (&se, expr);
9826 : 6 : gfc_add_block_to_block (&block, &se.pre);
9827 : 6 : gfc_add_block_to_block (&block, &se.post);
9828 : : }
9829 : : }
9830 : : else
9831 : : {
9832 : : /* Scalar pointers. */
9833 : 1905 : se.want_pointer = 1;
9834 : 1905 : gfc_conv_expr (&se, expr);
9835 : 1905 : gfc_add_block_to_block (&block, &se.pre);
9836 : :
9837 : 1905 : if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
9838 : 12 : && expr->symtree->n.sym->attr.dummy)
9839 : 12 : se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
9840 : :
9841 : 1905 : gfc_add_modify (&block, dest,
9842 : 1905 : fold_convert (TREE_TYPE (dest), se.expr));
9843 : 1905 : gfc_add_block_to_block (&block, &se.post);
9844 : : }
9845 : : }
9846 : 24562 : else if (cm->ts.type == BT_CLASS && expr->expr_type == EXPR_NULL)
9847 : : {
9848 : : /* NULL initialization for CLASS components. */
9849 : 916 : tmp = gfc_trans_structure_assign (dest,
9850 : : gfc_class_initializer (&cm->ts, expr),
9851 : : false);
9852 : 916 : gfc_add_expr_to_block (&block, tmp);
9853 : : }
9854 : 23646 : else if ((cm->attr.dimension || cm->attr.codimension)
9855 : : && !cm->attr.proc_pointer)
9856 : : {
9857 : 4210 : if (cm->attr.allocatable && expr->expr_type == EXPR_NULL)
9858 : : {
9859 : 2345 : gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
9860 : 2345 : if (cm->attr.codimension && flag_coarray == GFC_FCOARRAY_LIB)
9861 : 1 : gfc_add_modify (&block, gfc_conv_descriptor_token (dest),
9862 : : null_pointer_node);
9863 : : }
9864 : 1865 : else if (cm->attr.allocatable || cm->attr.pdt_array)
9865 : : {
9866 : 932 : tmp = gfc_trans_alloc_subarray_assign (dest, cm, expr);
9867 : 932 : gfc_add_expr_to_block (&block, tmp);
9868 : : }
9869 : : else
9870 : : {
9871 : 933 : tmp = gfc_trans_subarray_assign (dest, cm, expr);
9872 : 933 : gfc_add_expr_to_block (&block, tmp);
9873 : : }
9874 : : }
9875 : 19436 : else if (cm->ts.type == BT_CLASS
9876 : 120 : && CLASS_DATA (cm)->attr.dimension
9877 : 36 : && CLASS_DATA (cm)->attr.allocatable
9878 : 36 : && expr->ts.type == BT_DERIVED)
9879 : : {
9880 : 36 : vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
9881 : 36 : vtab = gfc_build_addr_expr (NULL_TREE, vtab);
9882 : 36 : tmp = gfc_class_vptr_get (dest);
9883 : 36 : gfc_add_modify (&block, tmp,
9884 : 36 : fold_convert (TREE_TYPE (tmp), vtab));
9885 : 36 : tmp = gfc_class_data_get (dest);
9886 : 36 : tmp = gfc_trans_alloc_subarray_assign (tmp, cm, expr);
9887 : 36 : gfc_add_expr_to_block (&block, tmp);
9888 : : }
9889 : 19400 : else if (cm->attr.allocatable && expr->expr_type == EXPR_NULL
9890 : 1642 : && (init
9891 : 1516 : || (cm->ts.type == BT_CHARACTER
9892 : 125 : && !(cm->ts.deferred || cm->attr.pdt_string))))
9893 : : {
9894 : : /* NULL initialization for allocatable components.
9895 : : Deferred-length character is dealt with later. */
9896 : 144 : gfc_add_modify (&block, dest, fold_convert (TREE_TYPE (dest),
9897 : : null_pointer_node));
9898 : : }
9899 : 19256 : else if (init && (cm->attr.allocatable
9900 : 12780 : || (cm->ts.type == BT_CLASS && CLASS_DATA (cm)->attr.allocatable
9901 : 84 : && expr->ts.type != BT_CLASS)))
9902 : : {
9903 : 351 : tree size;
9904 : :
9905 : 351 : gfc_init_se (&se, NULL);
9906 : 351 : gfc_conv_expr (&se, expr);
9907 : :
9908 : : /* The remainder of these instructions follow the if (cm->attr.pointer)
9909 : : if (!cm->attr.dimension) part above. */
9910 : 351 : gfc_add_block_to_block (&block, &se.pre);
9911 : : /* Take care about non-array allocatable components here. The alloc_*
9912 : : routine below is motivated by the alloc_scalar_allocatable_for_
9913 : : assignment() routine, but with the realloc portions removed and
9914 : : different input. */
9915 : 351 : alloc_scalar_allocatable_subcomponent (&block, dest, cm, expr,
9916 : : se.string_length);
9917 : :
9918 : 351 : if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
9919 : 0 : && expr->symtree->n.sym->attr.dummy)
9920 : 0 : se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
9921 : :
9922 : 351 : if (cm->ts.type == BT_CLASS)
9923 : : {
9924 : 78 : tmp = gfc_class_data_get (dest);
9925 : 78 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
9926 : 78 : vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
9927 : 78 : vtab = gfc_build_addr_expr (NULL_TREE, vtab);
9928 : 78 : gfc_add_modify (&block, gfc_class_vptr_get (dest),
9929 : 78 : fold_convert (TREE_TYPE (gfc_class_vptr_get (dest)), vtab));
9930 : : }
9931 : : else
9932 : 273 : tmp = build_fold_indirect_ref_loc (input_location, dest);
9933 : :
9934 : : /* For deferred strings insert a memcpy. */
9935 : 351 : if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
9936 : : {
9937 : 127 : gcc_assert (se.string_length || expr->ts.u.cl->backend_decl);
9938 : 127 : size = size_of_string_in_bytes (cm->ts.kind, se.string_length
9939 : : ? se.string_length
9940 : 0 : : expr->ts.u.cl->backend_decl);
9941 : 127 : tmp = gfc_build_memcpy_call (tmp, se.expr, size);
9942 : 127 : gfc_add_expr_to_block (&block, tmp);
9943 : : }
9944 : 224 : else if (cm->ts.type == BT_CLASS)
9945 : : {
9946 : : /* Fix the expression for memcpy. */
9947 : 78 : if (expr->expr_type != EXPR_VARIABLE)
9948 : 48 : se.expr = gfc_evaluate_now (se.expr, &block);
9949 : :
9950 : 78 : if (expr->ts.type == BT_CHARACTER)
9951 : : {
9952 : 18 : size = build_int_cst (gfc_charlen_type_node, expr->ts.kind);
9953 : 18 : size = fold_build2_loc (input_location, MULT_EXPR,
9954 : : gfc_charlen_type_node,
9955 : : se.string_length, size);
9956 : 18 : size = fold_convert (size_type_node, size);
9957 : : }
9958 : : else
9959 : 60 : size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr->ts));
9960 : :
9961 : : /* Now copy the expression to the constructor component _data. */
9962 : 78 : gfc_add_expr_to_block (&block,
9963 : : gfc_build_memcpy_call (tmp, se.expr, size));
9964 : :
9965 : : /* Fill the unlimited polymorphic _len field. */
9966 : 78 : if (UNLIMITED_POLY (cm) && expr->ts.type == BT_CHARACTER)
9967 : : {
9968 : 18 : tmp = gfc_class_len_get (gfc_get_class_from_expr (tmp));
9969 : 18 : gfc_add_modify (&block, tmp,
9970 : 18 : fold_convert (TREE_TYPE (tmp),
9971 : : se.string_length));
9972 : : }
9973 : : }
9974 : : else
9975 : 146 : gfc_add_modify (&block, tmp,
9976 : 146 : fold_convert (TREE_TYPE (tmp), se.expr));
9977 : 351 : gfc_add_block_to_block (&block, &se.post);
9978 : 351 : }
9979 : 18905 : else if (expr->ts.type == BT_UNION)
9980 : : {
9981 : 13 : tree tmp;
9982 : 13 : gfc_constructor *c = gfc_constructor_first (expr->value.constructor);
9983 : : /* We mark that the entire union should be initialized with a contrived
9984 : : EXPR_NULL expression at the beginning. */
9985 : 13 : if (c != NULL && c->n.component == NULL
9986 : 7 : && c->expr != NULL && c->expr->expr_type == EXPR_NULL)
9987 : : {
9988 : 6 : tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
9989 : 6 : dest, build_constructor (TREE_TYPE (dest), NULL));
9990 : 6 : gfc_add_expr_to_block (&block, tmp);
9991 : 6 : c = gfc_constructor_next (c);
9992 : : }
9993 : : /* The following constructor expression, if any, represents a specific
9994 : : map intializer, as given by the user. */
9995 : 13 : if (c != NULL && c->expr != NULL)
9996 : : {
9997 : 6 : gcc_assert (expr->expr_type == EXPR_STRUCTURE);
9998 : 6 : tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
9999 : 6 : gfc_add_expr_to_block (&block, tmp);
10000 : : }
10001 : : }
10002 : 18892 : else if (expr->ts.type == BT_DERIVED && expr->ts.f90_type != BT_VOID)
10003 : : {
10004 : 2931 : if (expr->expr_type != EXPR_STRUCTURE)
10005 : : {
10006 : 373 : tree dealloc = NULL_TREE;
10007 : 373 : gfc_init_se (&se, NULL);
10008 : 373 : gfc_conv_expr (&se, expr);
10009 : 373 : gfc_add_block_to_block (&block, &se.pre);
10010 : : /* Prevent repeat evaluations in gfc_copy_alloc_comp by fixing the
10011 : : expression in a temporary variable and deallocate the allocatable
10012 : : components. Then we can the copy the expression to the result. */
10013 : 373 : if (cm->ts.u.derived->attr.alloc_comp
10014 : 251 : && expr->expr_type != EXPR_VARIABLE)
10015 : : {
10016 : 221 : se.expr = gfc_evaluate_now (se.expr, &block);
10017 : 221 : dealloc = gfc_deallocate_alloc_comp (cm->ts.u.derived, se.expr,
10018 : : expr->rank);
10019 : : }
10020 : 373 : gfc_add_modify (&block, dest,
10021 : 373 : fold_convert (TREE_TYPE (dest), se.expr));
10022 : 373 : if (cm->ts.u.derived->attr.alloc_comp
10023 : 251 : && expr->expr_type != EXPR_NULL)
10024 : : {
10025 : : // TODO: Fix caf_mode
10026 : 48 : tmp = gfc_copy_alloc_comp (cm->ts.u.derived, se.expr,
10027 : : dest, expr->rank, 0);
10028 : 48 : gfc_add_expr_to_block (&block, tmp);
10029 : 48 : if (dealloc != NULL_TREE)
10030 : 18 : gfc_add_expr_to_block (&block, dealloc);
10031 : : }
10032 : 373 : gfc_add_block_to_block (&block, &se.post);
10033 : : }
10034 : : else
10035 : : {
10036 : : /* Nested constructors. */
10037 : 2558 : tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
10038 : 2558 : gfc_add_expr_to_block (&block, tmp);
10039 : : }
10040 : : }
10041 : 15961 : else if (gfc_deferred_strlen (cm, &tmp))
10042 : : {
10043 : 119 : tree strlen;
10044 : 119 : strlen = tmp;
10045 : 119 : gcc_assert (strlen);
10046 : 119 : strlen = fold_build3_loc (input_location, COMPONENT_REF,
10047 : 119 : TREE_TYPE (strlen),
10048 : 119 : TREE_OPERAND (dest, 0),
10049 : : strlen, NULL_TREE);
10050 : :
10051 : 119 : if (expr->expr_type == EXPR_NULL)
10052 : : {
10053 : 107 : tmp = build_int_cst (TREE_TYPE (cm->backend_decl), 0);
10054 : 107 : gfc_add_modify (&block, dest, tmp);
10055 : 107 : tmp = build_int_cst (TREE_TYPE (strlen), 0);
10056 : 107 : gfc_add_modify (&block, strlen, tmp);
10057 : : }
10058 : : else
10059 : : {
10060 : 12 : tree size;
10061 : 12 : gfc_init_se (&se, NULL);
10062 : 12 : gfc_conv_expr (&se, expr);
10063 : 12 : size = size_of_string_in_bytes (cm->ts.kind, se.string_length);
10064 : 12 : size = fold_convert (size_type_node, size);
10065 : 12 : tmp = build_call_expr_loc (input_location,
10066 : : builtin_decl_explicit (BUILT_IN_MALLOC),
10067 : : 1, size);
10068 : 12 : gfc_add_modify (&block, dest,
10069 : 12 : fold_convert (TREE_TYPE (dest), tmp));
10070 : 12 : gfc_add_modify (&block, strlen,
10071 : 12 : fold_convert (TREE_TYPE (strlen), se.string_length));
10072 : 12 : tmp = gfc_build_memcpy_call (dest, se.expr, size);
10073 : 12 : gfc_add_expr_to_block (&block, tmp);
10074 : : }
10075 : : }
10076 : 15842 : else if (!cm->attr.artificial)
10077 : : {
10078 : : /* Scalar component (excluding deferred parameters). */
10079 : 15727 : gfc_init_se (&se, NULL);
10080 : 15727 : gfc_init_se (&lse, NULL);
10081 : :
10082 : 15727 : gfc_conv_expr (&se, expr);
10083 : 15727 : if (cm->ts.type == BT_CHARACTER)
10084 : 1047 : lse.string_length = cm->ts.u.cl->backend_decl;
10085 : 15727 : lse.expr = dest;
10086 : 15727 : tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, false, false);
10087 : 15727 : gfc_add_expr_to_block (&block, tmp);
10088 : : }
10089 : 27097 : return gfc_finish_block (&block);
10090 : : }
10091 : :
10092 : : /* Assign a derived type constructor to a variable. */
10093 : :
10094 : : tree
10095 : 19125 : gfc_trans_structure_assign (tree dest, gfc_expr * expr, bool init, bool coarray)
10096 : : {
10097 : 19125 : gfc_constructor *c;
10098 : 19125 : gfc_component *cm;
10099 : 19125 : stmtblock_t block;
10100 : 19125 : tree field;
10101 : 19125 : tree tmp;
10102 : 19125 : gfc_se se;
10103 : :
10104 : 19125 : gfc_start_block (&block);
10105 : :
10106 : 19125 : if (expr->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
10107 : 172 : && (expr->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
10108 : 9 : || expr->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
10109 : : {
10110 : 172 : gfc_se lse;
10111 : :
10112 : 172 : gfc_init_se (&se, NULL);
10113 : 172 : gfc_init_se (&lse, NULL);
10114 : 172 : gfc_conv_expr (&se, gfc_constructor_first (expr->value.constructor)->expr);
10115 : 172 : lse.expr = dest;
10116 : 172 : gfc_add_modify (&block, lse.expr,
10117 : 172 : fold_convert (TREE_TYPE (lse.expr), se.expr));
10118 : :
10119 : 172 : return gfc_finish_block (&block);
10120 : : }
10121 : :
10122 : : /* Make sure that the derived type has been completely built. */
10123 : 18953 : if (!expr->ts.u.derived->backend_decl
10124 : 18953 : || !TYPE_FIELDS (expr->ts.u.derived->backend_decl))
10125 : : {
10126 : 223 : tmp = gfc_typenode_for_spec (&expr->ts);
10127 : 223 : gcc_assert (tmp);
10128 : : }
10129 : :
10130 : 18953 : cm = expr->ts.u.derived->components;
10131 : :
10132 : :
10133 : 18953 : if (coarray)
10134 : 169 : gfc_init_se (&se, NULL);
10135 : :
10136 : 18953 : for (c = gfc_constructor_first (expr->value.constructor);
10137 : 49002 : c; c = gfc_constructor_next (c), cm = cm->next)
10138 : : {
10139 : : /* Skip absent members in default initializers. */
10140 : 30049 : if (!c->expr && !cm->attr.allocatable)
10141 : 2952 : continue;
10142 : :
10143 : : /* Register the component with the caf-lib before it is initialized.
10144 : : Register only allocatable components, that are not coarray'ed
10145 : : components (%comp[*]). Only register when the constructor is the
10146 : : null-expression. */
10147 : 27097 : if (coarray && !cm->attr.codimension
10148 : 386 : && (cm->attr.allocatable || cm->attr.pointer)
10149 : 169 : && (!c->expr || c->expr->expr_type == EXPR_NULL))
10150 : : {
10151 : 168 : tree token, desc, size;
10152 : 336 : bool is_array = cm->ts.type == BT_CLASS
10153 : 168 : ? CLASS_DATA (cm)->attr.dimension : cm->attr.dimension;
10154 : :
10155 : 168 : field = cm->backend_decl;
10156 : 168 : field = fold_build3_loc (input_location, COMPONENT_REF,
10157 : 168 : TREE_TYPE (field), dest, field, NULL_TREE);
10158 : 168 : if (cm->ts.type == BT_CLASS)
10159 : 0 : field = gfc_class_data_get (field);
10160 : :
10161 : 168 : token
10162 : : = is_array
10163 : 168 : ? gfc_conv_descriptor_token (field)
10164 : 49 : : fold_build3_loc (input_location, COMPONENT_REF,
10165 : 49 : TREE_TYPE (gfc_comp_caf_token (cm)), dest,
10166 : 49 : gfc_comp_caf_token (cm), NULL_TREE);
10167 : :
10168 : 168 : if (is_array)
10169 : : {
10170 : : /* The _caf_register routine looks at the rank of the array
10171 : : descriptor to decide whether the data registered is an array
10172 : : or not. */
10173 : 119 : int rank = cm->ts.type == BT_CLASS ? CLASS_DATA (cm)->as->rank
10174 : 119 : : cm->as->rank;
10175 : : /* When the rank is not known just set a positive rank, which
10176 : : suffices to recognize the data as array. */
10177 : 119 : if (rank < 0)
10178 : 0 : rank = 1;
10179 : 119 : size = build_zero_cst (size_type_node);
10180 : 119 : desc = field;
10181 : 119 : gfc_add_modify (&block, gfc_conv_descriptor_rank (desc),
10182 : 119 : build_int_cst (signed_char_type_node, rank));
10183 : : }
10184 : : else
10185 : : {
10186 : 49 : desc = gfc_conv_scalar_to_descriptor (&se, field,
10187 : 49 : cm->ts.type == BT_CLASS
10188 : 49 : ? CLASS_DATA (cm)->attr
10189 : : : cm->attr);
10190 : 49 : size = TYPE_SIZE_UNIT (TREE_TYPE (field));
10191 : : }
10192 : 168 : gfc_add_block_to_block (&block, &se.pre);
10193 : 168 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_register,
10194 : : 7, size, build_int_cst (
10195 : : integer_type_node,
10196 : : GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY),
10197 : : gfc_build_addr_expr (pvoid_type_node,
10198 : : token),
10199 : : gfc_build_addr_expr (NULL_TREE, desc),
10200 : : null_pointer_node, null_pointer_node,
10201 : : integer_zero_node);
10202 : 168 : gfc_add_expr_to_block (&block, tmp);
10203 : : }
10204 : 27097 : field = cm->backend_decl;
10205 : 27097 : gcc_assert(field);
10206 : 27097 : tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
10207 : : dest, field, NULL_TREE);
10208 : 27097 : if (!c->expr)
10209 : : {
10210 : 0 : gfc_expr *e = gfc_get_null_expr (NULL);
10211 : 0 : tmp = gfc_trans_subcomponent_assign (tmp, cm, e, init);
10212 : 0 : gfc_free_expr (e);
10213 : : }
10214 : : else
10215 : 27097 : tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr, init);
10216 : 27097 : gfc_add_expr_to_block (&block, tmp);
10217 : : }
10218 : 18953 : return gfc_finish_block (&block);
10219 : : }
10220 : :
10221 : : static void
10222 : 21 : gfc_conv_union_initializer (vec<constructor_elt, va_gc> *&v,
10223 : : gfc_component *un, gfc_expr *init)
10224 : : {
10225 : 21 : gfc_constructor *ctor;
10226 : :
10227 : 21 : if (un->ts.type != BT_UNION || un == NULL || init == NULL)
10228 : : return;
10229 : :
10230 : 21 : ctor = gfc_constructor_first (init->value.constructor);
10231 : :
10232 : 21 : if (ctor == NULL || ctor->expr == NULL)
10233 : : return;
10234 : :
10235 : 21 : gcc_assert (init->expr_type == EXPR_STRUCTURE);
10236 : :
10237 : : /* If we have an 'initialize all' constructor, do it first. */
10238 : 21 : if (ctor->expr->expr_type == EXPR_NULL)
10239 : : {
10240 : 9 : tree union_type = TREE_TYPE (un->backend_decl);
10241 : 9 : tree val = build_constructor (union_type, NULL);
10242 : 9 : CONSTRUCTOR_APPEND_ELT (v, un->backend_decl, val);
10243 : 9 : ctor = gfc_constructor_next (ctor);
10244 : : }
10245 : :
10246 : : /* Add the map initializer on top. */
10247 : 21 : if (ctor != NULL && ctor->expr != NULL)
10248 : : {
10249 : 12 : gcc_assert (ctor->expr->expr_type == EXPR_STRUCTURE);
10250 : 12 : tree val = gfc_conv_initializer (ctor->expr, &un->ts,
10251 : 12 : TREE_TYPE (un->backend_decl),
10252 : 12 : un->attr.dimension, un->attr.pointer,
10253 : 12 : un->attr.proc_pointer);
10254 : 12 : CONSTRUCTOR_APPEND_ELT (v, un->backend_decl, val);
10255 : : }
10256 : : }
10257 : :
10258 : : /* Build an expression for a constructor. If init is nonzero then
10259 : : this is part of a static variable initializer. */
10260 : :
10261 : : void
10262 : 36516 : gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
10263 : : {
10264 : 36516 : gfc_constructor *c;
10265 : 36516 : gfc_component *cm;
10266 : 36516 : tree val;
10267 : 36516 : tree type;
10268 : 36516 : tree tmp;
10269 : 36516 : vec<constructor_elt, va_gc> *v = NULL;
10270 : :
10271 : 36516 : gcc_assert (se->ss == NULL);
10272 : 36516 : gcc_assert (expr->expr_type == EXPR_STRUCTURE);
10273 : 36516 : type = gfc_typenode_for_spec (&expr->ts);
10274 : :
10275 : 36516 : if (!init)
10276 : : {
10277 : : /* Create a temporary variable and fill it in. */
10278 : 15050 : se->expr = gfc_create_var (type, expr->ts.u.derived->name);
10279 : : /* The symtree in expr is NULL, if the code to generate is for
10280 : : initializing the static members only. */
10281 : 30100 : tmp = gfc_trans_structure_assign (se->expr, expr, expr->symtree != NULL,
10282 : 15050 : se->want_coarray);
10283 : 15050 : gfc_add_expr_to_block (&se->pre, tmp);
10284 : 15050 : return;
10285 : : }
10286 : :
10287 : 21466 : cm = expr->ts.u.derived->components;
10288 : :
10289 : 21466 : for (c = gfc_constructor_first (expr->value.constructor);
10290 : 113214 : c && cm; c = gfc_constructor_next (c), cm = cm->next)
10291 : : {
10292 : : /* Skip absent members in default initializers and allocatable
10293 : : components. Although the latter have a default initializer
10294 : : of EXPR_NULL,... by default, the static nullify is not needed
10295 : : since this is done every time we come into scope. */
10296 : 91748 : if (!c->expr || (cm->attr.allocatable && cm->attr.flavor != FL_PROCEDURE))
10297 : 7768 : continue;
10298 : :
10299 : 83980 : if (cm->initializer && cm->initializer->expr_type != EXPR_NULL
10300 : 48466 : && strcmp (cm->name, "_extends") == 0
10301 : 1254 : && cm->initializer->symtree)
10302 : : {
10303 : 1254 : tree vtab;
10304 : 1254 : gfc_symbol *vtabs;
10305 : 1254 : vtabs = cm->initializer->symtree->n.sym;
10306 : 1254 : vtab = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtabs));
10307 : 1254 : vtab = unshare_expr_without_location (vtab);
10308 : 1254 : CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, vtab);
10309 : 1254 : }
10310 : 82726 : else if (cm->ts.u.derived && strcmp (cm->name, "_size") == 0)
10311 : : {
10312 : 9159 : val = TYPE_SIZE_UNIT (gfc_get_derived_type (cm->ts.u.derived));
10313 : 9159 : CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
10314 : : fold_convert (TREE_TYPE (cm->backend_decl),
10315 : : val));
10316 : 9159 : }
10317 : 73567 : else if (cm->ts.type == BT_INTEGER && strcmp (cm->name, "_len") == 0)
10318 : 389 : CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
10319 : : fold_convert (TREE_TYPE (cm->backend_decl),
10320 : 389 : integer_zero_node));
10321 : 73178 : else if (cm->ts.type == BT_UNION)
10322 : 21 : gfc_conv_union_initializer (v, cm, c->expr);
10323 : : else
10324 : : {
10325 : 73157 : val = gfc_conv_initializer (c->expr, &cm->ts,
10326 : 73157 : TREE_TYPE (cm->backend_decl),
10327 : : cm->attr.dimension, cm->attr.pointer,
10328 : 73157 : cm->attr.proc_pointer);
10329 : 73157 : val = unshare_expr_without_location (val);
10330 : :
10331 : : /* Append it to the constructor list. */
10332 : 164905 : CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
10333 : : }
10334 : : }
10335 : :
10336 : 21466 : se->expr = build_constructor (type, v);
10337 : 21466 : if (init)
10338 : 21466 : TREE_CONSTANT (se->expr) = 1;
10339 : : }
10340 : :
10341 : :
10342 : : /* Translate a substring expression. */
10343 : :
10344 : : static void
10345 : 258 : gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
10346 : : {
10347 : 258 : gfc_ref *ref;
10348 : :
10349 : 258 : ref = expr->ref;
10350 : :
10351 : 258 : gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
10352 : :
10353 : 516 : se->expr = gfc_build_wide_string_const (expr->ts.kind,
10354 : 258 : expr->value.character.length,
10355 : 258 : expr->value.character.string);
10356 : :
10357 : 258 : se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
10358 : 258 : TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
10359 : :
10360 : 258 : if (ref)
10361 : 258 : gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
10362 : 258 : }
10363 : :
10364 : :
10365 : : /* Entry point for expression translation. Evaluates a scalar quantity.
10366 : : EXPR is the expression to be translated, and SE is the state structure if
10367 : : called from within the scalarized. */
10368 : :
10369 : : void
10370 : 3507976 : gfc_conv_expr (gfc_se * se, gfc_expr * expr)
10371 : : {
10372 : 3507976 : gfc_ss *ss;
10373 : :
10374 : 3507976 : ss = se->ss;
10375 : 3507976 : if (ss && ss->info->expr == expr
10376 : 229779 : && (ss->info->type == GFC_SS_SCALAR
10377 : : || ss->info->type == GFC_SS_REFERENCE))
10378 : : {
10379 : 38774 : gfc_ss_info *ss_info;
10380 : :
10381 : 38774 : ss_info = ss->info;
10382 : : /* Substitute a scalar expression evaluated outside the scalarization
10383 : : loop. */
10384 : 38774 : se->expr = ss_info->data.scalar.value;
10385 : 38774 : if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
10386 : 824 : se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
10387 : :
10388 : 38774 : se->string_length = ss_info->string_length;
10389 : 38774 : gfc_advance_se_ss_chain (se);
10390 : 38774 : return;
10391 : : }
10392 : :
10393 : : /* We need to convert the expressions for the iso_c_binding derived types.
10394 : : C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
10395 : : null_pointer_node. C_PTR and C_FUNPTR are converted to match the
10396 : : typespec for the C_PTR and C_FUNPTR symbols, which has already been
10397 : : updated to be an integer with a kind equal to the size of a (void *). */
10398 : 3469202 : if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->ts.f90_type == BT_VOID
10399 : 15697 : && expr->ts.u.derived->attr.is_bind_c)
10400 : : {
10401 : 14860 : if (expr->expr_type == EXPR_VARIABLE
10402 : 10644 : && (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
10403 : 10644 : || expr->symtree->n.sym->intmod_sym_id
10404 : : == ISOCBINDING_NULL_FUNPTR))
10405 : : {
10406 : : /* Set expr_type to EXPR_NULL, which will result in
10407 : : null_pointer_node being used below. */
10408 : 0 : expr->expr_type = EXPR_NULL;
10409 : : }
10410 : : else
10411 : : {
10412 : : /* Update the type/kind of the expression to be what the new
10413 : : type/kind are for the updated symbols of C_PTR/C_FUNPTR. */
10414 : 14860 : expr->ts.type = BT_INTEGER;
10415 : 14860 : expr->ts.f90_type = BT_VOID;
10416 : 14860 : expr->ts.kind = gfc_index_integer_kind;
10417 : : }
10418 : : }
10419 : :
10420 : 3469202 : gfc_fix_class_refs (expr);
10421 : :
10422 : 3469202 : switch (expr->expr_type)
10423 : : {
10424 : 490458 : case EXPR_OP:
10425 : 490458 : gfc_conv_expr_op (se, expr);
10426 : 490458 : break;
10427 : :
10428 : 291121 : case EXPR_FUNCTION:
10429 : 291121 : gfc_conv_function_expr (se, expr);
10430 : 291121 : break;
10431 : :
10432 : 1095761 : case EXPR_CONSTANT:
10433 : 1095761 : gfc_conv_constant (se, expr);
10434 : 1095761 : break;
10435 : :
10436 : 1537998 : case EXPR_VARIABLE:
10437 : 1537998 : gfc_conv_variable (se, expr);
10438 : 1537998 : break;
10439 : :
10440 : 4013 : case EXPR_NULL:
10441 : 4013 : se->expr = null_pointer_node;
10442 : 4013 : break;
10443 : :
10444 : 258 : case EXPR_SUBSTRING:
10445 : 258 : gfc_conv_substring_expr (se, expr);
10446 : 258 : break;
10447 : :
10448 : 15050 : case EXPR_STRUCTURE:
10449 : 15050 : gfc_conv_structure (se, expr, 0);
10450 : : /* F2008 4.5.6.3 para 5: If an executable construct references a
10451 : : structure constructor or array constructor, the entity created by
10452 : : the constructor is finalized after execution of the innermost
10453 : : executable construct containing the reference. This, in fact,
10454 : : was later deleted by the Combined Techical Corrigenda 1 TO 4 for
10455 : : fortran 2008 (f08/0011). */
10456 : 15050 : if ((gfc_option.allow_std & (GFC_STD_F2008 | GFC_STD_F2003))
10457 : 15050 : && !(gfc_option.allow_std & GFC_STD_GNU)
10458 : 139 : && expr->must_finalize
10459 : 15062 : && gfc_may_be_finalized (expr->ts))
10460 : : {
10461 : 12 : locus loc;
10462 : 12 : gfc_locus_from_location (&loc, input_location);
10463 : 12 : gfc_warning (0, "The structure constructor at %L has been"
10464 : : " finalized. This feature was removed by f08/0011."
10465 : : " Use -std=f2018 or -std=gnu to eliminate the"
10466 : : " finalization.", &loc);
10467 : 12 : symbol_attribute attr;
10468 : 12 : attr.allocatable = attr.pointer = 0;
10469 : 12 : gfc_finalize_tree_expr (se, expr->ts.u.derived, attr, 0);
10470 : 12 : gfc_add_block_to_block (&se->post, &se->finalblock);
10471 : : }
10472 : : break;
10473 : :
10474 : 34543 : case EXPR_ARRAY:
10475 : 34543 : gfc_conv_array_constructor_expr (se, expr);
10476 : 34543 : gfc_add_block_to_block (&se->post, &se->finalblock);
10477 : 34543 : break;
10478 : :
10479 : 0 : default:
10480 : 0 : gcc_unreachable ();
10481 : 3507976 : break;
10482 : : }
10483 : : }
10484 : :
10485 : : /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
10486 : : of an assignment. */
10487 : : void
10488 : 357290 : gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
10489 : : {
10490 : 357290 : gfc_conv_expr (se, expr);
10491 : : /* All numeric lvalues should have empty post chains. If not we need to
10492 : : figure out a way of rewriting an lvalue so that it has no post chain. */
10493 : 357290 : gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
10494 : 357290 : }
10495 : :
10496 : : /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
10497 : : numeric expressions. Used for scalar values where inserting cleanup code
10498 : : is inconvenient. */
10499 : : void
10500 : 994171 : gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
10501 : : {
10502 : 994171 : tree val;
10503 : :
10504 : 994171 : gcc_assert (expr->ts.type != BT_CHARACTER);
10505 : 994171 : gfc_conv_expr (se, expr);
10506 : 994171 : if (se->post.head)
10507 : : {
10508 : 2435 : val = gfc_create_var (TREE_TYPE (se->expr), NULL);
10509 : 2435 : gfc_add_modify (&se->pre, val, se->expr);
10510 : 2435 : se->expr = val;
10511 : 2435 : gfc_add_block_to_block (&se->pre, &se->post);
10512 : : }
10513 : 994171 : }
10514 : :
10515 : : /* Helper to translate an expression and convert it to a particular type. */
10516 : : void
10517 : 275154 : gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
10518 : : {
10519 : 275154 : gfc_conv_expr_val (se, expr);
10520 : 275154 : se->expr = convert (type, se->expr);
10521 : 275154 : }
10522 : :
10523 : :
10524 : : /* Converts an expression so that it can be passed by reference. Scalar
10525 : : values only. */
10526 : :
10527 : : void
10528 : 220657 : gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
10529 : : {
10530 : 220657 : gfc_ss *ss;
10531 : 220657 : tree var;
10532 : :
10533 : 220657 : ss = se->ss;
10534 : 220657 : if (ss && ss->info->expr == expr
10535 : 7154 : && ss->info->type == GFC_SS_REFERENCE)
10536 : : {
10537 : : /* Returns a reference to the scalar evaluated outside the loop
10538 : : for this case. */
10539 : 906 : gfc_conv_expr (se, expr);
10540 : :
10541 : 906 : if (expr->ts.type == BT_CHARACTER
10542 : 114 : && expr->expr_type != EXPR_FUNCTION)
10543 : 102 : gfc_conv_string_parameter (se);
10544 : : else
10545 : 804 : se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
10546 : :
10547 : 906 : return;
10548 : : }
10549 : :
10550 : 219751 : if (expr->ts.type == BT_CHARACTER)
10551 : : {
10552 : 48650 : gfc_conv_expr (se, expr);
10553 : 48650 : gfc_conv_string_parameter (se);
10554 : 48650 : return;
10555 : : }
10556 : :
10557 : 171101 : if (expr->expr_type == EXPR_VARIABLE)
10558 : : {
10559 : 67794 : se->want_pointer = 1;
10560 : 67794 : gfc_conv_expr (se, expr);
10561 : 67794 : if (se->post.head)
10562 : : {
10563 : 0 : var = gfc_create_var (TREE_TYPE (se->expr), NULL);
10564 : 0 : gfc_add_modify (&se->pre, var, se->expr);
10565 : 0 : gfc_add_block_to_block (&se->pre, &se->post);
10566 : 0 : se->expr = var;
10567 : : }
10568 : 67794 : return;
10569 : : }
10570 : :
10571 : 103307 : if (expr->expr_type == EXPR_FUNCTION
10572 : 13016 : && ((expr->value.function.esym
10573 : 2038 : && expr->value.function.esym->result
10574 : 2037 : && expr->value.function.esym->result->attr.pointer
10575 : 71 : && !expr->value.function.esym->result->attr.dimension)
10576 : 12951 : || (!expr->value.function.esym && !expr->ref
10577 : 10872 : && expr->symtree->n.sym->attr.pointer
10578 : 0 : && !expr->symtree->n.sym->attr.dimension)))
10579 : : {
10580 : 65 : se->want_pointer = 1;
10581 : 65 : gfc_conv_expr (se, expr);
10582 : 65 : var = gfc_create_var (TREE_TYPE (se->expr), NULL);
10583 : 65 : gfc_add_modify (&se->pre, var, se->expr);
10584 : 65 : se->expr = var;
10585 : 65 : return;
10586 : : }
10587 : :
10588 : 103242 : gfc_conv_expr (se, expr);
10589 : :
10590 : : /* Create a temporary var to hold the value. */
10591 : 103242 : if (TREE_CONSTANT (se->expr))
10592 : : {
10593 : : tree tmp = se->expr;
10594 : 82396 : STRIP_TYPE_NOPS (tmp);
10595 : 82396 : var = build_decl (input_location,
10596 : 82396 : CONST_DECL, NULL, TREE_TYPE (tmp));
10597 : 82396 : DECL_INITIAL (var) = tmp;
10598 : 82396 : TREE_STATIC (var) = 1;
10599 : 82396 : pushdecl (var);
10600 : : }
10601 : : else
10602 : : {
10603 : 20846 : var = gfc_create_var (TREE_TYPE (se->expr), NULL);
10604 : 20846 : gfc_add_modify (&se->pre, var, se->expr);
10605 : : }
10606 : :
10607 : 103242 : if (!expr->must_finalize)
10608 : 103152 : gfc_add_block_to_block (&se->pre, &se->post);
10609 : :
10610 : : /* Take the address of that value. */
10611 : 103242 : se->expr = gfc_build_addr_expr (NULL_TREE, var);
10612 : : }
10613 : :
10614 : :
10615 : : /* Get the _len component for an unlimited polymorphic expression. */
10616 : :
10617 : : static tree
10618 : 1660 : trans_get_upoly_len (stmtblock_t *block, gfc_expr *expr)
10619 : : {
10620 : 1660 : gfc_se se;
10621 : 1660 : gfc_ref *ref = expr->ref;
10622 : :
10623 : 1660 : gfc_init_se (&se, NULL);
10624 : 3410 : while (ref && ref->next)
10625 : : ref = ref->next;
10626 : 1660 : gfc_add_len_component (expr);
10627 : 1660 : gfc_conv_expr (&se, expr);
10628 : 1660 : gfc_add_block_to_block (block, &se.pre);
10629 : 1660 : gcc_assert (se.post.head == NULL_TREE);
10630 : 1660 : if (ref)
10631 : : {
10632 : 238 : gfc_free_ref_list (ref->next);
10633 : 238 : ref->next = NULL;
10634 : : }
10635 : : else
10636 : : {
10637 : 1422 : gfc_free_ref_list (expr->ref);
10638 : 1422 : expr->ref = NULL;
10639 : : }
10640 : 1660 : return se.expr;
10641 : : }
10642 : :
10643 : :
10644 : : /* Assign _vptr and _len components as appropriate. BLOCK should be a
10645 : : statement-list outside of the scalarizer-loop. When code is generated, that
10646 : : depends on the scalarized expression, it is added to RSE.PRE.
10647 : : Returns le's _vptr tree and when set the len expressions in to_lenp and
10648 : : from_lenp to form a le%_vptr%_copy (re, le, [from_lenp, to_lenp])
10649 : : expression. */
10650 : :
10651 : : static tree
10652 : 4333 : trans_class_vptr_len_assignment (stmtblock_t *block, gfc_expr * le,
10653 : : gfc_expr * re, gfc_se *rse,
10654 : : tree * to_lenp, tree * from_lenp,
10655 : : tree * from_vptrp)
10656 : : {
10657 : 4333 : gfc_se se;
10658 : 4333 : gfc_expr * vptr_expr;
10659 : 4333 : tree tmp, to_len = NULL_TREE, from_len = NULL_TREE, lhs_vptr;
10660 : 4333 : bool set_vptr = false, temp_rhs = false;
10661 : 4333 : stmtblock_t *pre = block;
10662 : 4333 : tree class_expr = NULL_TREE;
10663 : 4333 : tree from_vptr = NULL_TREE;
10664 : :
10665 : : /* Create a temporary for complicated expressions. */
10666 : 4333 : if (re->expr_type != EXPR_VARIABLE && re->expr_type != EXPR_NULL
10667 : 1178 : && rse->expr != NULL_TREE)
10668 : : {
10669 : 1178 : if (!DECL_P (rse->expr))
10670 : : {
10671 : 330 : if (re->ts.type == BT_CLASS && !GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
10672 : 37 : class_expr = gfc_get_class_from_expr (rse->expr);
10673 : :
10674 : 330 : if (rse->loop)
10675 : 111 : pre = &rse->loop->pre;
10676 : : else
10677 : 219 : pre = &rse->pre;
10678 : :
10679 : 330 : if (class_expr != NULL_TREE && UNLIMITED_POLY (re))
10680 : 37 : tmp = gfc_evaluate_now (TREE_OPERAND (rse->expr, 0), &rse->pre);
10681 : : else
10682 : 293 : tmp = gfc_evaluate_now (rse->expr, &rse->pre);
10683 : :
10684 : 330 : rse->expr = tmp;
10685 : : }
10686 : : else
10687 : 848 : pre = &rse->pre;
10688 : :
10689 : : temp_rhs = true;
10690 : : }
10691 : :
10692 : : /* Get the _vptr for the left-hand side expression. */
10693 : 4333 : gfc_init_se (&se, NULL);
10694 : 4333 : vptr_expr = gfc_find_and_cut_at_last_class_ref (le);
10695 : 4333 : if (vptr_expr != NULL && gfc_expr_attr (vptr_expr).class_ok)
10696 : : {
10697 : : /* Care about _len for unlimited polymorphic entities. */
10698 : 4315 : if (UNLIMITED_POLY (vptr_expr)
10699 : 3423 : || (vptr_expr->ts.type == BT_DERIVED
10700 : 2433 : && vptr_expr->ts.u.derived->attr.unlimited_polymorphic))
10701 : 1376 : to_len = trans_get_upoly_len (block, vptr_expr);
10702 : 4315 : gfc_add_vptr_component (vptr_expr);
10703 : 4315 : set_vptr = true;
10704 : : }
10705 : : else
10706 : 18 : vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&le->ts));
10707 : 4333 : se.want_pointer = 1;
10708 : 4333 : gfc_conv_expr (&se, vptr_expr);
10709 : 4333 : gfc_free_expr (vptr_expr);
10710 : 4333 : gfc_add_block_to_block (block, &se.pre);
10711 : 4333 : gcc_assert (se.post.head == NULL_TREE);
10712 : 4333 : lhs_vptr = se.expr;
10713 : 4333 : STRIP_NOPS (lhs_vptr);
10714 : :
10715 : : /* Set the _vptr only when the left-hand side of the assignment is a
10716 : : class-object. */
10717 : 4333 : if (set_vptr)
10718 : : {
10719 : : /* Get the vptr from the rhs expression only, when it is variable.
10720 : : Functions are expected to be assigned to a temporary beforehand. */
10721 : 3023 : vptr_expr = (re->expr_type == EXPR_VARIABLE && re->ts.type == BT_CLASS)
10722 : 5064 : ? gfc_find_and_cut_at_last_class_ref (re)
10723 : : : NULL;
10724 : 749 : if (vptr_expr != NULL && vptr_expr->ts.type == BT_CLASS)
10725 : : {
10726 : 749 : if (to_len != NULL_TREE)
10727 : : {
10728 : : /* Get the _len information from the rhs. */
10729 : 299 : if (UNLIMITED_POLY (vptr_expr)
10730 : : || (vptr_expr->ts.type == BT_DERIVED
10731 : : && vptr_expr->ts.u.derived->attr.unlimited_polymorphic))
10732 : 272 : from_len = trans_get_upoly_len (block, vptr_expr);
10733 : : }
10734 : 749 : gfc_add_vptr_component (vptr_expr);
10735 : : }
10736 : : else
10737 : : {
10738 : 3566 : if (re->expr_type == EXPR_VARIABLE
10739 : 2274 : && DECL_P (re->symtree->n.sym->backend_decl)
10740 : 2274 : && DECL_LANG_SPECIFIC (re->symtree->n.sym->backend_decl)
10741 : 819 : && GFC_DECL_SAVED_DESCRIPTOR (re->symtree->n.sym->backend_decl)
10742 : 3633 : && GFC_CLASS_TYPE_P (TREE_TYPE (GFC_DECL_SAVED_DESCRIPTOR (
10743 : : re->symtree->n.sym->backend_decl))))
10744 : : {
10745 : 43 : vptr_expr = NULL;
10746 : 43 : se.expr = gfc_class_vptr_get (GFC_DECL_SAVED_DESCRIPTOR (
10747 : : re->symtree->n.sym->backend_decl));
10748 : 43 : if (to_len && UNLIMITED_POLY (re))
10749 : 0 : from_len = gfc_class_len_get (GFC_DECL_SAVED_DESCRIPTOR (
10750 : : re->symtree->n.sym->backend_decl));
10751 : : }
10752 : 3523 : else if (temp_rhs && re->ts.type == BT_CLASS)
10753 : : {
10754 : 213 : vptr_expr = NULL;
10755 : 213 : if (class_expr)
10756 : : tmp = class_expr;
10757 : 176 : else if (!GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
10758 : 0 : tmp = gfc_get_class_from_expr (rse->expr);
10759 : : else
10760 : : tmp = rse->expr;
10761 : :
10762 : 213 : se.expr = gfc_class_vptr_get (tmp);
10763 : 213 : from_vptr = se.expr;
10764 : 213 : if (UNLIMITED_POLY (re))
10765 : 73 : from_len = gfc_class_len_get (tmp);
10766 : :
10767 : : }
10768 : 3310 : else if (re->expr_type != EXPR_NULL)
10769 : : /* Only when rhs is non-NULL use its declared type for vptr
10770 : : initialisation. */
10771 : 3184 : vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&re->ts));
10772 : : else
10773 : : /* When the rhs is NULL use the vtab of lhs' declared type. */
10774 : 126 : vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&le->ts));
10775 : : }
10776 : :
10777 : 4132 : if (vptr_expr)
10778 : : {
10779 : 4059 : gfc_init_se (&se, NULL);
10780 : 4059 : se.want_pointer = 1;
10781 : 4059 : gfc_conv_expr (&se, vptr_expr);
10782 : 4059 : gfc_free_expr (vptr_expr);
10783 : 4059 : gfc_add_block_to_block (block, &se.pre);
10784 : 4059 : gcc_assert (se.post.head == NULL_TREE);
10785 : 4059 : from_vptr = se.expr;
10786 : : }
10787 : 4315 : gfc_add_modify (pre, lhs_vptr, fold_convert (TREE_TYPE (lhs_vptr),
10788 : : se.expr));
10789 : :
10790 : 4315 : if (to_len != NULL_TREE)
10791 : : {
10792 : : /* The _len component needs to be set. Figure how to get the
10793 : : value of the right-hand side. */
10794 : 1376 : if (from_len == NULL_TREE)
10795 : : {
10796 : 1031 : if (rse->string_length != NULL_TREE)
10797 : : from_len = rse->string_length;
10798 : 591 : else if (re->ts.type == BT_CHARACTER && re->ts.u.cl->length)
10799 : : {
10800 : 0 : gfc_init_se (&se, NULL);
10801 : 0 : gfc_conv_expr (&se, re->ts.u.cl->length);
10802 : 0 : gfc_add_block_to_block (block, &se.pre);
10803 : 0 : gcc_assert (se.post.head == NULL_TREE);
10804 : 0 : from_len = gfc_evaluate_now (se.expr, block);
10805 : : }
10806 : : else
10807 : 591 : from_len = build_zero_cst (gfc_charlen_type_node);
10808 : : }
10809 : 1376 : gfc_add_modify (pre, to_len, fold_convert (TREE_TYPE (to_len),
10810 : : from_len));
10811 : : }
10812 : : }
10813 : :
10814 : : /* Return the _len and _vptr trees only, when requested. */
10815 : 4333 : if (to_lenp)
10816 : 3189 : *to_lenp = to_len;
10817 : 4333 : if (from_lenp)
10818 : 3189 : *from_lenp = from_len;
10819 : 4333 : if (from_vptrp)
10820 : 3189 : *from_vptrp = from_vptr;
10821 : 4333 : return lhs_vptr;
10822 : : }
10823 : :
10824 : :
10825 : : /* Assign tokens for pointer components. */
10826 : :
10827 : : static void
10828 : 6 : trans_caf_token_assign (gfc_se *lse, gfc_se *rse, gfc_expr *expr1,
10829 : : gfc_expr *expr2)
10830 : : {
10831 : 6 : symbol_attribute lhs_attr, rhs_attr;
10832 : 6 : tree tmp, lhs_tok, rhs_tok;
10833 : : /* Flag to indicated component refs on the rhs. */
10834 : 6 : bool rhs_cr;
10835 : :
10836 : 6 : lhs_attr = gfc_caf_attr (expr1);
10837 : 6 : if (expr2->expr_type != EXPR_NULL)
10838 : : {
10839 : 4 : rhs_attr = gfc_caf_attr (expr2, false, &rhs_cr);
10840 : 4 : if (lhs_attr.codimension && rhs_attr.codimension)
10841 : : {
10842 : 2 : lhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (lse, expr1);
10843 : 2 : lhs_tok = build_fold_indirect_ref (lhs_tok);
10844 : :
10845 : 2 : if (rhs_cr)
10846 : 0 : rhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (rse, expr2);
10847 : : else
10848 : : {
10849 : 2 : tree caf_decl;
10850 : 2 : caf_decl = gfc_get_tree_for_caf_expr (expr2);
10851 : 2 : gfc_get_caf_token_offset (rse, &rhs_tok, NULL, caf_decl,
10852 : : NULL_TREE, NULL);
10853 : : }
10854 : 2 : tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
10855 : : lhs_tok,
10856 : 2 : fold_convert (TREE_TYPE (lhs_tok), rhs_tok));
10857 : 2 : gfc_prepend_expr_to_block (&lse->post, tmp);
10858 : : }
10859 : : }
10860 : 2 : else if (lhs_attr.codimension)
10861 : : {
10862 : 2 : lhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (lse, expr1);
10863 : 2 : if (!lhs_tok)
10864 : : {
10865 : 1 : lhs_tok = gfc_get_tree_for_caf_expr (expr1);
10866 : 1 : lhs_tok = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (lhs_tok));
10867 : : }
10868 : : else
10869 : 1 : lhs_tok = build_fold_indirect_ref (lhs_tok);
10870 : 2 : tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
10871 : : lhs_tok, null_pointer_node);
10872 : 2 : gfc_prepend_expr_to_block (&lse->post, tmp);
10873 : : }
10874 : 6 : }
10875 : :
10876 : :
10877 : : /* Do everything that is needed for a CLASS function expr2. */
10878 : :
10879 : : static tree
10880 : 18 : trans_class_pointer_fcn (stmtblock_t *block, gfc_se *lse, gfc_se *rse,
10881 : : gfc_expr *expr1, gfc_expr *expr2)
10882 : : {
10883 : 18 : tree expr1_vptr = NULL_TREE;
10884 : 18 : tree tmp;
10885 : :
10886 : 18 : gfc_conv_function_expr (rse, expr2);
10887 : 18 : rse->expr = gfc_evaluate_now (rse->expr, &rse->pre);
10888 : :
10889 : 18 : if (expr1->ts.type != BT_CLASS)
10890 : 12 : rse->expr = gfc_class_data_get (rse->expr);
10891 : : else
10892 : : {
10893 : 6 : expr1_vptr = trans_class_vptr_len_assignment (block, expr1,
10894 : : expr2, rse,
10895 : : NULL, NULL, NULL);
10896 : 6 : gfc_add_block_to_block (block, &rse->pre);
10897 : 6 : tmp = gfc_create_var (TREE_TYPE (rse->expr), "ptrtemp");
10898 : 6 : gfc_add_modify (&lse->pre, tmp, rse->expr);
10899 : :
10900 : 12 : gfc_add_modify (&lse->pre, expr1_vptr,
10901 : 6 : fold_convert (TREE_TYPE (expr1_vptr),
10902 : : gfc_class_vptr_get (tmp)));
10903 : 6 : rse->expr = gfc_class_data_get (tmp);
10904 : : }
10905 : :
10906 : 18 : return expr1_vptr;
10907 : : }
10908 : :
10909 : :
10910 : : tree
10911 : 9812 : gfc_trans_pointer_assign (gfc_code * code)
10912 : : {
10913 : 9812 : return gfc_trans_pointer_assignment (code->expr1, code->expr2);
10914 : : }
10915 : :
10916 : :
10917 : : /* Generate code for a pointer assignment. */
10918 : :
10919 : : tree
10920 : 9867 : gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
10921 : : {
10922 : 9867 : gfc_se lse;
10923 : 9867 : gfc_se rse;
10924 : 9867 : stmtblock_t block;
10925 : 9867 : tree desc;
10926 : 9867 : tree tmp;
10927 : 9867 : tree expr1_vptr = NULL_TREE;
10928 : 9867 : bool scalar, non_proc_ptr_assign;
10929 : 9867 : gfc_ss *ss;
10930 : :
10931 : 9867 : gfc_start_block (&block);
10932 : :
10933 : 9867 : gfc_init_se (&lse, NULL);
10934 : :
10935 : : /* Usually testing whether this is not a proc pointer assignment. */
10936 : 9867 : non_proc_ptr_assign
10937 : 9867 : = !(gfc_expr_attr (expr1).proc_pointer
10938 : 1165 : && ((expr2->expr_type == EXPR_VARIABLE
10939 : 934 : && expr2->symtree->n.sym->attr.flavor == FL_PROCEDURE)
10940 : 281 : || expr2->expr_type == EXPR_NULL));
10941 : :
10942 : : /* Check whether the expression is a scalar or not; we cannot use
10943 : : expr1->rank as it can be nonzero for proc pointers. */
10944 : 9867 : ss = gfc_walk_expr (expr1);
10945 : 9867 : scalar = ss == gfc_ss_terminator;
10946 : 9867 : if (!scalar)
10947 : 4221 : gfc_free_ss_chain (ss);
10948 : :
10949 : 9867 : if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS
10950 : 84 : && expr2->expr_type != EXPR_FUNCTION && non_proc_ptr_assign)
10951 : : {
10952 : 66 : gfc_add_data_component (expr2);
10953 : : /* The following is required as gfc_add_data_component doesn't
10954 : : update ts.type if there is a trailing REF_ARRAY. */
10955 : 66 : expr2->ts.type = BT_DERIVED;
10956 : : }
10957 : :
10958 : 9867 : if (scalar)
10959 : : {
10960 : : /* Scalar pointers. */
10961 : 5646 : lse.want_pointer = 1;
10962 : 5646 : gfc_conv_expr (&lse, expr1);
10963 : 5646 : gfc_init_se (&rse, NULL);
10964 : 5646 : rse.want_pointer = 1;
10965 : 5646 : if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
10966 : 6 : trans_class_pointer_fcn (&block, &lse, &rse, expr1, expr2);
10967 : : else
10968 : 5640 : gfc_conv_expr (&rse, expr2);
10969 : :
10970 : 5646 : if (non_proc_ptr_assign && expr1->ts.type == BT_CLASS)
10971 : : {
10972 : 763 : trans_class_vptr_len_assignment (&block, expr1, expr2, &rse, NULL,
10973 : : NULL, NULL);
10974 : 763 : lse.expr = gfc_class_data_get (lse.expr);
10975 : : }
10976 : :
10977 : 5646 : if (expr1->symtree->n.sym->attr.proc_pointer
10978 : 850 : && expr1->symtree->n.sym->attr.dummy)
10979 : 49 : lse.expr = build_fold_indirect_ref_loc (input_location,
10980 : : lse.expr);
10981 : :
10982 : 5646 : if (expr2->symtree && expr2->symtree->n.sym->attr.proc_pointer
10983 : 47 : && expr2->symtree->n.sym->attr.dummy)
10984 : 20 : rse.expr = build_fold_indirect_ref_loc (input_location,
10985 : : rse.expr);
10986 : :
10987 : 5646 : gfc_add_block_to_block (&block, &lse.pre);
10988 : 5646 : gfc_add_block_to_block (&block, &rse.pre);
10989 : :
10990 : : /* Check character lengths if character expression. The test is only
10991 : : really added if -fbounds-check is enabled. Exclude deferred
10992 : : character length lefthand sides. */
10993 : 921 : if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL
10994 : 747 : && !expr1->ts.deferred
10995 : 333 : && !expr1->symtree->n.sym->attr.proc_pointer
10996 : 5972 : && !gfc_is_proc_ptr_comp (expr1))
10997 : : {
10998 : 307 : gcc_assert (expr2->ts.type == BT_CHARACTER);
10999 : 307 : gcc_assert (lse.string_length && rse.string_length);
11000 : 307 : gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
11001 : : lse.string_length, rse.string_length,
11002 : : &block);
11003 : : }
11004 : :
11005 : : /* The assignment to an deferred character length sets the string
11006 : : length to that of the rhs. */
11007 : 5646 : if (expr1->ts.deferred)
11008 : : {
11009 : 529 : if (expr2->expr_type != EXPR_NULL && lse.string_length != NULL)
11010 : 412 : gfc_add_modify (&block, lse.string_length,
11011 : 412 : fold_convert (TREE_TYPE (lse.string_length),
11012 : : rse.string_length));
11013 : 117 : else if (lse.string_length != NULL)
11014 : 115 : gfc_add_modify (&block, lse.string_length,
11015 : 115 : build_zero_cst (TREE_TYPE (lse.string_length)));
11016 : : }
11017 : :
11018 : 5646 : gfc_add_modify (&block, lse.expr,
11019 : 5646 : fold_convert (TREE_TYPE (lse.expr), rse.expr));
11020 : :
11021 : 5646 : if (flag_coarray == GFC_FCOARRAY_LIB)
11022 : : {
11023 : 250 : if (expr1->ref)
11024 : : /* Also set the tokens for pointer components in derived typed
11025 : : coarrays. */
11026 : 6 : trans_caf_token_assign (&lse, &rse, expr1, expr2);
11027 : 244 : else if (gfc_caf_attr (expr1).codimension)
11028 : : {
11029 : 0 : tree lhs_caf_decl, rhs_caf_decl, lhs_tok, rhs_tok;
11030 : :
11031 : 0 : lhs_caf_decl = gfc_get_tree_for_caf_expr (expr1);
11032 : 0 : rhs_caf_decl = gfc_get_tree_for_caf_expr (expr2);
11033 : 0 : gfc_get_caf_token_offset (&lse, &lhs_tok, nullptr, lhs_caf_decl,
11034 : : NULL_TREE, expr1);
11035 : 0 : gfc_get_caf_token_offset (&rse, &rhs_tok, nullptr, rhs_caf_decl,
11036 : : NULL_TREE, expr2);
11037 : 0 : gfc_add_modify (&block, lhs_tok, rhs_tok);
11038 : : }
11039 : : }
11040 : :
11041 : 5646 : gfc_add_block_to_block (&block, &rse.post);
11042 : 5646 : gfc_add_block_to_block (&block, &lse.post);
11043 : : }
11044 : : else
11045 : : {
11046 : 4221 : gfc_ref* remap;
11047 : 4221 : bool rank_remap;
11048 : 4221 : tree strlen_lhs;
11049 : 4221 : tree strlen_rhs = NULL_TREE;
11050 : :
11051 : : /* Array pointer. Find the last reference on the LHS and if it is an
11052 : : array section ref, we're dealing with bounds remapping. In this case,
11053 : : set it to AR_FULL so that gfc_conv_expr_descriptor does
11054 : : not see it and process the bounds remapping afterwards explicitly. */
11055 : 13614 : for (remap = expr1->ref; remap; remap = remap->next)
11056 : 5503 : if (!remap->next && remap->type == REF_ARRAY
11057 : 4221 : && remap->u.ar.type == AR_SECTION)
11058 : : break;
11059 : 4221 : rank_remap = (remap && remap->u.ar.end[0]);
11060 : :
11061 : 331 : if (remap && expr2->expr_type == EXPR_NULL)
11062 : : {
11063 : 2 : gfc_error ("If bounds remapping is specified at %L, "
11064 : : "the pointer target shall not be NULL", &expr1->where);
11065 : 2 : return NULL_TREE;
11066 : : }
11067 : :
11068 : 4219 : gfc_init_se (&lse, NULL);
11069 : 4219 : if (remap)
11070 : 329 : lse.descriptor_only = 1;
11071 : 4219 : gfc_conv_expr_descriptor (&lse, expr1);
11072 : 4219 : strlen_lhs = lse.string_length;
11073 : 4219 : desc = lse.expr;
11074 : :
11075 : 4219 : if (expr2->expr_type == EXPR_NULL)
11076 : : {
11077 : : /* Just set the data pointer to null. */
11078 : 679 : gfc_conv_descriptor_data_set (&lse.pre, lse.expr, null_pointer_node);
11079 : : }
11080 : 3540 : else if (rank_remap)
11081 : : {
11082 : : /* If we are rank-remapping, just get the RHS's descriptor and
11083 : : process this later on. */
11084 : 206 : gfc_init_se (&rse, NULL);
11085 : 206 : rse.direct_byref = 1;
11086 : 206 : rse.byref_noassign = 1;
11087 : :
11088 : 206 : if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
11089 : 12 : expr1_vptr = trans_class_pointer_fcn (&block, &lse, &rse,
11090 : : expr1, expr2);
11091 : 194 : else if (expr2->expr_type == EXPR_FUNCTION)
11092 : : {
11093 : : tree bound[GFC_MAX_DIMENSIONS];
11094 : : int i;
11095 : :
11096 : 26 : for (i = 0; i < expr2->rank; i++)
11097 : 13 : bound[i] = NULL_TREE;
11098 : 13 : tmp = gfc_typenode_for_spec (&expr2->ts);
11099 : 13 : tmp = gfc_get_array_type_bounds (tmp, expr2->rank, 0,
11100 : : bound, bound, 0,
11101 : : GFC_ARRAY_POINTER_CONT, false);
11102 : 13 : tmp = gfc_create_var (tmp, "ptrtemp");
11103 : 13 : rse.descriptor_only = 0;
11104 : 13 : rse.expr = tmp;
11105 : 13 : rse.direct_byref = 1;
11106 : 13 : gfc_conv_expr_descriptor (&rse, expr2);
11107 : 13 : strlen_rhs = rse.string_length;
11108 : 13 : rse.expr = tmp;
11109 : : }
11110 : : else
11111 : : {
11112 : 181 : gfc_conv_expr_descriptor (&rse, expr2);
11113 : 181 : strlen_rhs = rse.string_length;
11114 : 181 : if (expr1->ts.type == BT_CLASS)
11115 : 36 : expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
11116 : : expr2, &rse,
11117 : : NULL, NULL,
11118 : : NULL);
11119 : : }
11120 : : }
11121 : 3334 : else if (expr2->expr_type == EXPR_VARIABLE)
11122 : : {
11123 : : /* Assign directly to the LHS's descriptor. */
11124 : 3208 : lse.descriptor_only = 0;
11125 : 3208 : lse.direct_byref = 1;
11126 : 3208 : gfc_conv_expr_descriptor (&lse, expr2);
11127 : 3208 : strlen_rhs = lse.string_length;
11128 : 3208 : gfc_init_se (&rse, NULL);
11129 : :
11130 : 3208 : if (expr1->ts.type == BT_CLASS)
11131 : : {
11132 : 326 : rse.expr = NULL_TREE;
11133 : 326 : rse.string_length = strlen_rhs;
11134 : 326 : trans_class_vptr_len_assignment (&block, expr1, expr2, &rse,
11135 : : NULL, NULL, NULL);
11136 : : }
11137 : :
11138 : 3208 : if (remap == NULL)
11139 : : {
11140 : : /* If the target is not a whole array, use the target array
11141 : : reference for remap. */
11142 : 6631 : for (remap = expr2->ref; remap; remap = remap->next)
11143 : 3647 : if (remap->type == REF_ARRAY
11144 : 3145 : && remap->u.ar.type == AR_FULL
11145 : 2477 : && remap->next)
11146 : : break;
11147 : : }
11148 : : }
11149 : 126 : else if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
11150 : : {
11151 : 19 : gfc_init_se (&rse, NULL);
11152 : 19 : rse.want_pointer = 1;
11153 : 19 : gfc_conv_function_expr (&rse, expr2);
11154 : 19 : if (expr1->ts.type != BT_CLASS)
11155 : : {
11156 : 6 : rse.expr = gfc_class_data_get (rse.expr);
11157 : 6 : gfc_add_modify (&lse.pre, desc, rse.expr);
11158 : : /* Set the lhs span. */
11159 : 6 : tmp = TREE_TYPE (rse.expr);
11160 : 6 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (tmp));
11161 : 6 : tmp = fold_convert (gfc_array_index_type, tmp);
11162 : 6 : gfc_conv_descriptor_span_set (&lse.pre, desc, tmp);
11163 : : }
11164 : : else
11165 : : {
11166 : 13 : expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
11167 : : expr2, &rse, NULL,
11168 : : NULL, NULL);
11169 : 13 : gfc_add_block_to_block (&block, &rse.pre);
11170 : 13 : tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
11171 : 13 : gfc_add_modify (&lse.pre, tmp, rse.expr);
11172 : :
11173 : 26 : gfc_add_modify (&lse.pre, expr1_vptr,
11174 : 13 : fold_convert (TREE_TYPE (expr1_vptr),
11175 : : gfc_class_vptr_get (tmp)));
11176 : 13 : rse.expr = gfc_class_data_get (tmp);
11177 : 13 : gfc_add_modify (&lse.pre, desc, rse.expr);
11178 : : }
11179 : : }
11180 : : else
11181 : : {
11182 : : /* Assign to a temporary descriptor and then copy that
11183 : : temporary to the pointer. */
11184 : 107 : tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
11185 : 107 : lse.descriptor_only = 0;
11186 : 107 : lse.expr = tmp;
11187 : 107 : lse.direct_byref = 1;
11188 : 107 : gfc_conv_expr_descriptor (&lse, expr2);
11189 : 107 : strlen_rhs = lse.string_length;
11190 : 107 : gfc_add_modify (&lse.pre, desc, tmp);
11191 : : }
11192 : :
11193 : 4219 : if (expr1->ts.type == BT_CHARACTER
11194 : 572 : && expr1->ts.deferred)
11195 : : {
11196 : 326 : gfc_symbol *psym = expr1->symtree->n.sym;
11197 : 326 : tmp = NULL_TREE;
11198 : 326 : if (psym->ts.type == BT_CHARACTER
11199 : 325 : && psym->ts.u.cl->backend_decl)
11200 : 325 : tmp = psym->ts.u.cl->backend_decl;
11201 : 1 : else if (expr1->ts.u.cl->backend_decl
11202 : 1 : && VAR_P (expr1->ts.u.cl->backend_decl))
11203 : 0 : tmp = expr1->ts.u.cl->backend_decl;
11204 : 1 : else if (TREE_CODE (lse.expr) == COMPONENT_REF)
11205 : : {
11206 : 1 : gfc_ref *ref = expr1->ref;
11207 : 3 : for (;ref; ref = ref->next)
11208 : : {
11209 : 2 : if (ref->type == REF_COMPONENT
11210 : 1 : && ref->u.c.component->ts.type == BT_CHARACTER
11211 : 3 : && gfc_deferred_strlen (ref->u.c.component, &tmp))
11212 : 1 : tmp = fold_build3_loc (input_location, COMPONENT_REF,
11213 : 1 : TREE_TYPE (tmp),
11214 : 1 : TREE_OPERAND (lse.expr, 0),
11215 : : tmp, NULL_TREE);
11216 : : }
11217 : : }
11218 : :
11219 : 326 : gcc_assert (tmp);
11220 : :
11221 : 326 : if (expr2->expr_type != EXPR_NULL)
11222 : 314 : gfc_add_modify (&block, tmp,
11223 : 314 : fold_convert (TREE_TYPE (tmp), strlen_rhs));
11224 : : else
11225 : 12 : gfc_add_modify (&block, tmp, build_zero_cst (TREE_TYPE (tmp)));
11226 : : }
11227 : :
11228 : 4219 : gfc_add_block_to_block (&block, &lse.pre);
11229 : 4219 : if (rank_remap)
11230 : 206 : gfc_add_block_to_block (&block, &rse.pre);
11231 : :
11232 : : /* If we do bounds remapping, update LHS descriptor accordingly. */
11233 : 4219 : if (remap)
11234 : : {
11235 : 430 : int dim;
11236 : 430 : gcc_assert (remap->u.ar.dimen == expr1->rank);
11237 : :
11238 : 430 : if (rank_remap)
11239 : : {
11240 : : /* Do rank remapping. We already have the RHS's descriptor
11241 : : converted in rse and now have to build the correct LHS
11242 : : descriptor for it. */
11243 : :
11244 : 206 : tree dtype, data, span;
11245 : 206 : tree offs, stride;
11246 : 206 : tree lbound, ubound;
11247 : :
11248 : : /* Set dtype. */
11249 : 206 : dtype = gfc_conv_descriptor_dtype (desc);
11250 : 206 : tmp = gfc_get_dtype (TREE_TYPE (desc));
11251 : 206 : gfc_add_modify (&block, dtype, tmp);
11252 : :
11253 : : /* Copy data pointer. */
11254 : 206 : data = gfc_conv_descriptor_data_get (rse.expr);
11255 : 206 : gfc_conv_descriptor_data_set (&block, desc, data);
11256 : :
11257 : : /* Copy the span. */
11258 : 206 : if (VAR_P (rse.expr)
11259 : 206 : && GFC_DECL_PTR_ARRAY_P (rse.expr))
11260 : 12 : span = gfc_conv_descriptor_span_get (rse.expr);
11261 : : else
11262 : : {
11263 : 194 : tmp = TREE_TYPE (rse.expr);
11264 : 194 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (tmp));
11265 : 194 : span = fold_convert (gfc_array_index_type, tmp);
11266 : : }
11267 : 206 : gfc_conv_descriptor_span_set (&block, desc, span);
11268 : :
11269 : : /* Copy offset but adjust it such that it would correspond
11270 : : to a lbound of zero. */
11271 : 206 : if (expr2->rank == -1)
11272 : 42 : gfc_conv_descriptor_offset_set (&block, desc,
11273 : : gfc_index_zero_node);
11274 : : else
11275 : : {
11276 : 164 : offs = gfc_conv_descriptor_offset_get (rse.expr);
11277 : 510 : for (dim = 0; dim < expr2->rank; ++dim)
11278 : : {
11279 : 182 : stride = gfc_conv_descriptor_stride_get (rse.expr,
11280 : : gfc_rank_cst[dim]);
11281 : 182 : lbound = gfc_conv_descriptor_lbound_get (rse.expr,
11282 : : gfc_rank_cst[dim]);
11283 : 182 : tmp = fold_build2_loc (input_location, MULT_EXPR,
11284 : : gfc_array_index_type, stride,
11285 : : lbound);
11286 : 182 : offs = fold_build2_loc (input_location, PLUS_EXPR,
11287 : : gfc_array_index_type, offs, tmp);
11288 : : }
11289 : 164 : gfc_conv_descriptor_offset_set (&block, desc, offs);
11290 : : }
11291 : : /* Set the bounds as declared for the LHS and calculate strides as
11292 : : well as another offset update accordingly. */
11293 : 206 : stride = gfc_conv_descriptor_stride_get (rse.expr,
11294 : : gfc_rank_cst[0]);
11295 : 545 : for (dim = 0; dim < expr1->rank; ++dim)
11296 : : {
11297 : 339 : gfc_se lower_se;
11298 : 339 : gfc_se upper_se;
11299 : :
11300 : 339 : gcc_assert (remap->u.ar.start[dim] && remap->u.ar.end[dim]);
11301 : :
11302 : 339 : if (remap->u.ar.start[dim]->expr_type != EXPR_CONSTANT
11303 : : || remap->u.ar.start[dim]->expr_type != EXPR_VARIABLE)
11304 : 339 : gfc_resolve_expr (remap->u.ar.start[dim]);
11305 : 339 : if (remap->u.ar.end[dim]->expr_type != EXPR_CONSTANT
11306 : : || remap->u.ar.end[dim]->expr_type != EXPR_VARIABLE)
11307 : 339 : gfc_resolve_expr (remap->u.ar.end[dim]);
11308 : :
11309 : : /* Convert declared bounds. */
11310 : 339 : gfc_init_se (&lower_se, NULL);
11311 : 339 : gfc_init_se (&upper_se, NULL);
11312 : 339 : gfc_conv_expr (&lower_se, remap->u.ar.start[dim]);
11313 : 339 : gfc_conv_expr (&upper_se, remap->u.ar.end[dim]);
11314 : :
11315 : 339 : gfc_add_block_to_block (&block, &lower_se.pre);
11316 : 339 : gfc_add_block_to_block (&block, &upper_se.pre);
11317 : :
11318 : 339 : lbound = fold_convert (gfc_array_index_type, lower_se.expr);
11319 : 339 : ubound = fold_convert (gfc_array_index_type, upper_se.expr);
11320 : :
11321 : 339 : lbound = gfc_evaluate_now (lbound, &block);
11322 : 339 : ubound = gfc_evaluate_now (ubound, &block);
11323 : :
11324 : 339 : gfc_add_block_to_block (&block, &lower_se.post);
11325 : 339 : gfc_add_block_to_block (&block, &upper_se.post);
11326 : :
11327 : : /* Set bounds in descriptor. */
11328 : 339 : gfc_conv_descriptor_lbound_set (&block, desc,
11329 : : gfc_rank_cst[dim], lbound);
11330 : 339 : gfc_conv_descriptor_ubound_set (&block, desc,
11331 : : gfc_rank_cst[dim], ubound);
11332 : :
11333 : : /* Set stride. */
11334 : 339 : stride = gfc_evaluate_now (stride, &block);
11335 : 339 : gfc_conv_descriptor_stride_set (&block, desc,
11336 : : gfc_rank_cst[dim], stride);
11337 : :
11338 : : /* Update offset. */
11339 : 339 : offs = gfc_conv_descriptor_offset_get (desc);
11340 : 339 : tmp = fold_build2_loc (input_location, MULT_EXPR,
11341 : : gfc_array_index_type, lbound, stride);
11342 : 339 : offs = fold_build2_loc (input_location, MINUS_EXPR,
11343 : : gfc_array_index_type, offs, tmp);
11344 : 339 : offs = gfc_evaluate_now (offs, &block);
11345 : 339 : gfc_conv_descriptor_offset_set (&block, desc, offs);
11346 : :
11347 : : /* Update stride. */
11348 : 339 : tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
11349 : 339 : stride = fold_build2_loc (input_location, MULT_EXPR,
11350 : : gfc_array_index_type, stride, tmp);
11351 : : }
11352 : : }
11353 : : else
11354 : : {
11355 : : /* Bounds remapping. Just shift the lower bounds. */
11356 : :
11357 : 224 : gcc_assert (expr1->rank == expr2->rank);
11358 : :
11359 : 556 : for (dim = 0; dim < remap->u.ar.dimen; ++dim)
11360 : : {
11361 : 332 : gfc_se lbound_se;
11362 : :
11363 : 332 : gcc_assert (!remap->u.ar.end[dim]);
11364 : 332 : gfc_init_se (&lbound_se, NULL);
11365 : 332 : if (remap->u.ar.start[dim])
11366 : : {
11367 : 225 : gfc_conv_expr (&lbound_se, remap->u.ar.start[dim]);
11368 : 225 : gfc_add_block_to_block (&block, &lbound_se.pre);
11369 : : }
11370 : : else
11371 : : /* This remap arises from a target that is not a whole
11372 : : array. The start expressions will be NULL but we need
11373 : : the lbounds to be one. */
11374 : 107 : lbound_se.expr = gfc_index_one_node;
11375 : 332 : gfc_conv_shift_descriptor_lbound (&block, desc,
11376 : : dim, lbound_se.expr);
11377 : 332 : gfc_add_block_to_block (&block, &lbound_se.post);
11378 : : }
11379 : : }
11380 : : }
11381 : :
11382 : : /* If rank remapping was done, check with -fcheck=bounds that
11383 : : the target is at least as large as the pointer. */
11384 : 4219 : if (rank_remap && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
11385 : 72 : && expr2->rank != -1)
11386 : : {
11387 : 54 : tree lsize, rsize;
11388 : 54 : tree fault;
11389 : 54 : const char* msg;
11390 : :
11391 : 54 : lsize = gfc_conv_descriptor_size (lse.expr, expr1->rank);
11392 : 54 : rsize = gfc_conv_descriptor_size (rse.expr, expr2->rank);
11393 : :
11394 : 54 : lsize = gfc_evaluate_now (lsize, &block);
11395 : 54 : rsize = gfc_evaluate_now (rsize, &block);
11396 : 54 : fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
11397 : : rsize, lsize);
11398 : :
11399 : 54 : msg = _("Target of rank remapping is too small (%ld < %ld)");
11400 : 54 : gfc_trans_runtime_check (true, false, fault, &block, &expr2->where,
11401 : : msg, rsize, lsize);
11402 : : }
11403 : :
11404 : : /* Check string lengths if applicable. The check is only really added
11405 : : to the output code if -fbounds-check is enabled. */
11406 : 4219 : if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
11407 : : {
11408 : 506 : gcc_assert (expr2->ts.type == BT_CHARACTER);
11409 : 506 : gcc_assert (strlen_lhs && strlen_rhs);
11410 : 506 : gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
11411 : : strlen_lhs, strlen_rhs, &block);
11412 : : }
11413 : :
11414 : 4219 : gfc_add_block_to_block (&block, &lse.post);
11415 : 4219 : if (rank_remap)
11416 : 206 : gfc_add_block_to_block (&block, &rse.post);
11417 : : }
11418 : :
11419 : 9865 : return gfc_finish_block (&block);
11420 : : }
11421 : :
11422 : :
11423 : : /* Makes sure se is suitable for passing as a function string parameter. */
11424 : : /* TODO: Need to check all callers of this function. It may be abused. */
11425 : :
11426 : : void
11427 : 231541 : gfc_conv_string_parameter (gfc_se * se)
11428 : : {
11429 : 231541 : tree type;
11430 : :
11431 : 231541 : if (TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE
11432 : 231541 : && integer_onep (se->string_length))
11433 : : {
11434 : 667 : se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
11435 : 667 : return;
11436 : : }
11437 : :
11438 : 230874 : if (TREE_CODE (se->expr) == STRING_CST)
11439 : : {
11440 : 97245 : type = TREE_TYPE (TREE_TYPE (se->expr));
11441 : 97245 : se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
11442 : 97245 : return;
11443 : : }
11444 : :
11445 : 133629 : if ((TREE_CODE (TREE_TYPE (se->expr)) == ARRAY_TYPE
11446 : 51960 : || TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE)
11447 : 133725 : && TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
11448 : : {
11449 : 81765 : type = TREE_TYPE (se->expr);
11450 : 81765 : if (TREE_CODE (se->expr) != INDIRECT_REF)
11451 : 76890 : se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
11452 : : else
11453 : : {
11454 : 4875 : if (TREE_CODE (type) == ARRAY_TYPE)
11455 : 4875 : type = TREE_TYPE (type);
11456 : 4875 : type = gfc_get_character_type_len_for_eltype (type,
11457 : : se->string_length);
11458 : 4875 : type = build_pointer_type (type);
11459 : 4875 : se->expr = gfc_build_addr_expr (type, se->expr);
11460 : : }
11461 : : }
11462 : :
11463 : 133629 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
11464 : : }
11465 : :
11466 : :
11467 : : /* Generate code for assignment of scalar variables. Includes character
11468 : : strings and derived types with allocatable components.
11469 : : If you know that the LHS has no allocations, set dealloc to false.
11470 : :
11471 : : DEEP_COPY has no effect if the typespec TS is not a derived type with
11472 : : allocatable components. Otherwise, if it is set, an explicit copy of each
11473 : : allocatable component is made. This is necessary as a simple copy of the
11474 : : whole object would copy array descriptors as is, so that the lhs's
11475 : : allocatable components would point to the rhs's after the assignment.
11476 : : Typically, setting DEEP_COPY is necessary if the rhs is a variable, and not
11477 : : necessary if the rhs is a non-pointer function, as the allocatable components
11478 : : are not accessible by other means than the function's result after the
11479 : : function has returned. It is even more subtle when temporaries are involved,
11480 : : as the two following examples show:
11481 : : 1. When we evaluate an array constructor, a temporary is created. Thus
11482 : : there is theoretically no alias possible. However, no deep copy is
11483 : : made for this temporary, so that if the constructor is made of one or
11484 : : more variable with allocatable components, those components still point
11485 : : to the variable's: DEEP_COPY should be set for the assignment from the
11486 : : temporary to the lhs in that case.
11487 : : 2. When assigning a scalar to an array, we evaluate the scalar value out
11488 : : of the loop, store it into a temporary variable, and assign from that.
11489 : : In that case, deep copying when assigning to the temporary would be a
11490 : : waste of resources; however deep copies should happen when assigning from
11491 : : the temporary to each array element: again DEEP_COPY should be set for
11492 : : the assignment from the temporary to the lhs. */
11493 : :
11494 : : tree
11495 : 324016 : gfc_trans_scalar_assign (gfc_se *lse, gfc_se *rse, gfc_typespec ts,
11496 : : bool deep_copy, bool dealloc, bool in_coarray,
11497 : : bool assoc_assign)
11498 : : {
11499 : 324016 : stmtblock_t block;
11500 : 324016 : tree tmp;
11501 : 324016 : tree cond;
11502 : :
11503 : 324016 : gfc_init_block (&block);
11504 : :
11505 : 324016 : if (ts.type == BT_CHARACTER)
11506 : : {
11507 : 31265 : tree rlen = NULL;
11508 : 31265 : tree llen = NULL;
11509 : :
11510 : 31265 : if (lse->string_length != NULL_TREE)
11511 : : {
11512 : 31265 : gfc_conv_string_parameter (lse);
11513 : 31265 : gfc_add_block_to_block (&block, &lse->pre);
11514 : 31265 : llen = lse->string_length;
11515 : : }
11516 : :
11517 : 31265 : if (rse->string_length != NULL_TREE)
11518 : : {
11519 : 31265 : gfc_conv_string_parameter (rse);
11520 : 31265 : gfc_add_block_to_block (&block, &rse->pre);
11521 : 31265 : rlen = rse->string_length;
11522 : : }
11523 : :
11524 : 31265 : gfc_trans_string_copy (&block, llen, lse->expr, ts.kind, rlen,
11525 : : rse->expr, ts.kind);
11526 : : }
11527 : 292751 : else if (gfc_bt_struct (ts.type)
11528 : 17111 : && (ts.u.derived->attr.alloc_comp
11529 : 11536 : || (deep_copy && ts.u.derived->attr.pdt_type)))
11530 : : {
11531 : 5646 : tree tmp_var = NULL_TREE;
11532 : 5646 : cond = NULL_TREE;
11533 : :
11534 : : /* Are the rhs and the lhs the same? */
11535 : 5646 : if (deep_copy)
11536 : : {
11537 : 3258 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
11538 : : gfc_build_addr_expr (NULL_TREE, lse->expr),
11539 : : gfc_build_addr_expr (NULL_TREE, rse->expr));
11540 : 3258 : cond = gfc_evaluate_now (cond, &lse->pre);
11541 : : }
11542 : :
11543 : : /* Deallocate the lhs allocated components as long as it is not
11544 : : the same as the rhs. This must be done following the assignment
11545 : : to prevent deallocating data that could be used in the rhs
11546 : : expression. */
11547 : 5646 : if (dealloc)
11548 : : {
11549 : 1570 : tmp_var = gfc_evaluate_now (lse->expr, &lse->pre);
11550 : 1570 : tmp = gfc_deallocate_alloc_comp_no_caf (ts.u.derived, tmp_var,
11551 : 1570 : 0, gfc_may_be_finalized (ts));
11552 : 1570 : if (deep_copy)
11553 : 635 : tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
11554 : : tmp);
11555 : 1570 : gfc_add_expr_to_block (&lse->post, tmp);
11556 : : }
11557 : :
11558 : 5646 : gfc_add_block_to_block (&block, &rse->pre);
11559 : 5646 : gfc_add_block_to_block (&block, &lse->finalblock);
11560 : 5646 : gfc_add_block_to_block (&block, &lse->pre);
11561 : :
11562 : 5646 : gfc_add_modify (&block, lse->expr,
11563 : 5646 : fold_convert (TREE_TYPE (lse->expr), rse->expr));
11564 : :
11565 : : /* Restore pointer address of coarray components. */
11566 : 5646 : if (ts.u.derived->attr.coarray_comp && deep_copy && tmp_var != NULL_TREE)
11567 : : {
11568 : 4 : tmp = gfc_reassign_alloc_comp_caf (ts.u.derived, tmp_var, lse->expr);
11569 : 4 : tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
11570 : : tmp);
11571 : 4 : gfc_add_expr_to_block (&block, tmp);
11572 : : }
11573 : :
11574 : : /* Do a deep copy if the rhs is a variable, if it is not the
11575 : : same as the lhs. */
11576 : 5646 : if (deep_copy)
11577 : : {
11578 : 3258 : int caf_mode = in_coarray ? (GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
11579 : : | GFC_STRUCTURE_CAF_MODE_IN_COARRAY) : 0;
11580 : 3258 : tmp = gfc_copy_alloc_comp (ts.u.derived, rse->expr, lse->expr, 0,
11581 : : caf_mode);
11582 : 3258 : tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
11583 : : tmp);
11584 : 3258 : gfc_add_expr_to_block (&block, tmp);
11585 : : }
11586 : : }
11587 : 287105 : else if (gfc_bt_struct (ts.type))
11588 : : {
11589 : 11465 : gfc_add_block_to_block (&block, &rse->pre);
11590 : 11465 : gfc_add_block_to_block (&block, &lse->finalblock);
11591 : 11465 : gfc_add_block_to_block (&block, &lse->pre);
11592 : 11465 : tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
11593 : 11465 : TREE_TYPE (lse->expr), rse->expr);
11594 : 11465 : gfc_add_modify (&block, lse->expr, tmp);
11595 : : }
11596 : : /* If possible use the rhs vptr copy with trans_scalar_class_assign.... */
11597 : 275640 : else if (ts.type == BT_CLASS)
11598 : : {
11599 : 746 : gfc_add_block_to_block (&block, &lse->pre);
11600 : 746 : gfc_add_block_to_block (&block, &rse->pre);
11601 : 746 : gfc_add_block_to_block (&block, &lse->finalblock);
11602 : :
11603 : 746 : if (!trans_scalar_class_assign (&block, lse, rse))
11604 : : {
11605 : : /* ...otherwise assignment suffices. Note the use of VIEW_CONVERT_EXPR
11606 : : for the lhs which ensures that class data rhs cast as a string assigns
11607 : : correctly. */
11608 : 612 : tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
11609 : 612 : TREE_TYPE (rse->expr), lse->expr);
11610 : 612 : gfc_add_modify (&block, tmp, rse->expr);
11611 : : }
11612 : : }
11613 : 274894 : else if (ts.type != BT_CLASS)
11614 : : {
11615 : 274894 : gfc_add_block_to_block (&block, &lse->pre);
11616 : 274894 : gfc_add_block_to_block (&block, &rse->pre);
11617 : :
11618 : 274894 : if (in_coarray)
11619 : : {
11620 : 683 : if (flag_coarray == GFC_FCOARRAY_LIB && assoc_assign)
11621 : : {
11622 : 0 : gfc_add_modify (&block, gfc_conv_descriptor_token (lse->expr),
11623 : 0 : TYPE_LANG_SPECIFIC (
11624 : : TREE_TYPE (TREE_TYPE (rse->expr)))
11625 : : ->caf_token);
11626 : : }
11627 : 683 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (lse->expr)))
11628 : 0 : lse->expr = gfc_conv_array_data (lse->expr);
11629 : 270 : if (flag_coarray == GFC_FCOARRAY_SINGLE && assoc_assign
11630 : 683 : && !POINTER_TYPE_P (TREE_TYPE (rse->expr)))
11631 : 0 : rse->expr = gfc_build_addr_expr (NULL_TREE, rse->expr);
11632 : : }
11633 : 274894 : gfc_add_modify (&block, lse->expr,
11634 : 274894 : fold_convert (TREE_TYPE (lse->expr), rse->expr));
11635 : : }
11636 : :
11637 : 324016 : gfc_add_block_to_block (&block, &lse->post);
11638 : 324016 : gfc_add_block_to_block (&block, &rse->post);
11639 : :
11640 : 324016 : return gfc_finish_block (&block);
11641 : : }
11642 : :
11643 : :
11644 : : /* There are quite a lot of restrictions on the optimisation in using an
11645 : : array function assign without a temporary. */
11646 : :
11647 : : static bool
11648 : 14577 : arrayfunc_assign_needs_temporary (gfc_expr * expr1, gfc_expr * expr2)
11649 : : {
11650 : 14577 : gfc_ref * ref;
11651 : 14577 : bool seen_array_ref;
11652 : 14577 : bool c = false;
11653 : 14577 : gfc_symbol *sym = expr1->symtree->n.sym;
11654 : :
11655 : : /* Play it safe with class functions assigned to a derived type. */
11656 : 14577 : if (gfc_is_class_array_function (expr2)
11657 : 14577 : && expr1->ts.type == BT_DERIVED)
11658 : : return true;
11659 : :
11660 : : /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
11661 : 14553 : if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
11662 : : return true;
11663 : :
11664 : : /* Elemental functions are scalarized so that they don't need a
11665 : : temporary in gfc_trans_assignment_1, so return a true. Otherwise,
11666 : : they would need special treatment in gfc_trans_arrayfunc_assign. */
11667 : 8711 : if (expr2->value.function.esym != NULL
11668 : 1514 : && expr2->value.function.esym->attr.elemental)
11669 : : return true;
11670 : :
11671 : : /* Need a temporary if rhs is not FULL or a contiguous section. */
11672 : 8371 : if (expr1->ref && !(gfc_full_array_ref_p (expr1->ref, &c) || c))
11673 : : return true;
11674 : :
11675 : : /* Need a temporary if EXPR1 can't be expressed as a descriptor. */
11676 : 8134 : if (gfc_ref_needs_temporary_p (expr1->ref))
11677 : : return true;
11678 : :
11679 : : /* Functions returning pointers or allocatables need temporaries. */
11680 : 8122 : if (gfc_expr_attr (expr2).pointer
11681 : 8122 : || gfc_expr_attr (expr2).allocatable)
11682 : 381 : return true;
11683 : :
11684 : : /* Character array functions need temporaries unless the
11685 : : character lengths are the same. */
11686 : 7741 : if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
11687 : : {
11688 : 562 : if (UNLIMITED_POLY (expr1))
11689 : : return true;
11690 : :
11691 : 556 : if (expr1->ts.u.cl->length == NULL
11692 : 507 : || expr1->ts.u.cl->length->expr_type != EXPR_CONSTANT)
11693 : : return true;
11694 : :
11695 : 493 : if (expr2->ts.u.cl->length == NULL
11696 : 487 : || expr2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
11697 : : return true;
11698 : :
11699 : 475 : if (mpz_cmp (expr1->ts.u.cl->length->value.integer,
11700 : 475 : expr2->ts.u.cl->length->value.integer) != 0)
11701 : : return true;
11702 : : }
11703 : :
11704 : : /* Check that no LHS component references appear during an array
11705 : : reference. This is needed because we do not have the means to
11706 : : span any arbitrary stride with an array descriptor. This check
11707 : : is not needed for the rhs because the function result has to be
11708 : : a complete type. */
11709 : 7648 : seen_array_ref = false;
11710 : 15296 : for (ref = expr1->ref; ref; ref = ref->next)
11711 : : {
11712 : 7661 : if (ref->type == REF_ARRAY)
11713 : : seen_array_ref= true;
11714 : 13 : else if (ref->type == REF_COMPONENT && seen_array_ref)
11715 : : return true;
11716 : : }
11717 : :
11718 : : /* Check for a dependency. */
11719 : 7635 : if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
11720 : : expr2->value.function.esym,
11721 : : expr2->value.function.actual,
11722 : : NOT_ELEMENTAL))
11723 : : return true;
11724 : :
11725 : : /* If we have reached here with an intrinsic function, we do not
11726 : : need a temporary except in the particular case that reallocation
11727 : : on assignment is active and the lhs is allocatable and a target,
11728 : : or a pointer which may be a subref pointer. FIXME: The last
11729 : : condition can go away when we use span in the intrinsics
11730 : : directly.*/
11731 : 7198 : if (expr2->value.function.isym)
11732 : 6363 : return (flag_realloc_lhs && sym->attr.allocatable && sym->attr.target)
11733 : 12813 : || (sym->attr.pointer && sym->attr.subref_array_pointer);
11734 : :
11735 : : /* If the LHS is a dummy, we need a temporary if it is not
11736 : : INTENT(OUT). */
11737 : 760 : if (sym->attr.dummy && sym->attr.intent != INTENT_OUT)
11738 : : return true;
11739 : :
11740 : : /* If the lhs has been host_associated, is in common, a pointer or is
11741 : : a target and the function is not using a RESULT variable, aliasing
11742 : : can occur and a temporary is needed. */
11743 : 754 : if ((sym->attr.host_assoc
11744 : 700 : || sym->attr.in_common
11745 : 694 : || sym->attr.pointer
11746 : 688 : || sym->attr.cray_pointee
11747 : 688 : || sym->attr.target)
11748 : 66 : && expr2->symtree != NULL
11749 : 66 : && expr2->symtree->n.sym == expr2->symtree->n.sym->result)
11750 : : return true;
11751 : :
11752 : : /* A PURE function can unconditionally be called without a temporary. */
11753 : 712 : if (expr2->value.function.esym != NULL
11754 : 687 : && expr2->value.function.esym->attr.pure)
11755 : : return false;
11756 : :
11757 : : /* Implicit_pure functions are those which could legally be declared
11758 : : to be PURE. */
11759 : 684 : if (expr2->value.function.esym != NULL
11760 : 659 : && expr2->value.function.esym->attr.implicit_pure)
11761 : : return false;
11762 : :
11763 : 408 : if (!sym->attr.use_assoc
11764 : 408 : && !sym->attr.in_common
11765 : 408 : && !sym->attr.pointer
11766 : 402 : && !sym->attr.target
11767 : 402 : && !sym->attr.cray_pointee
11768 : 402 : && expr2->value.function.esym)
11769 : : {
11770 : : /* A temporary is not needed if the function is not contained and
11771 : : the variable is local or host associated and not a pointer or
11772 : : a target. */
11773 : 377 : if (!expr2->value.function.esym->attr.contained)
11774 : : return false;
11775 : :
11776 : : /* A temporary is not needed if the lhs has never been host
11777 : : associated and the procedure is contained. */
11778 : 146 : else if (!sym->attr.host_assoc)
11779 : : return false;
11780 : :
11781 : : /* A temporary is not needed if the variable is local and not
11782 : : a pointer, a target or a result. */
11783 : 6 : if (sym->ns->parent
11784 : 0 : && expr2->value.function.esym->ns == sym->ns->parent)
11785 : : return false;
11786 : : }
11787 : :
11788 : : /* Default to temporary use. */
11789 : : return true;
11790 : : }
11791 : :
11792 : :
11793 : : /* Provide the loop info so that the lhs descriptor can be built for
11794 : : reallocatable assignments from extrinsic function calls. */
11795 : :
11796 : : static void
11797 : 166 : realloc_lhs_loop_for_fcn_call (gfc_se *se, locus *where, gfc_ss **ss,
11798 : : gfc_loopinfo *loop)
11799 : : {
11800 : : /* Signal that the function call should not be made by
11801 : : gfc_conv_loop_setup. */
11802 : 166 : se->ss->is_alloc_lhs = 1;
11803 : 166 : gfc_init_loopinfo (loop);
11804 : 166 : gfc_add_ss_to_loop (loop, *ss);
11805 : 166 : gfc_add_ss_to_loop (loop, se->ss);
11806 : 166 : gfc_conv_ss_startstride (loop);
11807 : 166 : gfc_conv_loop_setup (loop, where);
11808 : 166 : gfc_copy_loopinfo_to_se (se, loop);
11809 : 166 : gfc_add_block_to_block (&se->pre, &loop->pre);
11810 : 166 : gfc_add_block_to_block (&se->pre, &loop->post);
11811 : 166 : se->ss->is_alloc_lhs = 0;
11812 : 166 : }
11813 : :
11814 : :
11815 : : /* For assignment to a reallocatable lhs from intrinsic functions,
11816 : : replace the se.expr (ie. the result) with a temporary descriptor.
11817 : : Null the data field so that the library allocates space for the
11818 : : result. Free the data of the original descriptor after the function,
11819 : : in case it appears in an argument expression and transfer the
11820 : : result to the original descriptor. */
11821 : :
11822 : : static void
11823 : 2109 : fcncall_realloc_result (gfc_se *se, int rank, tree dtype)
11824 : : {
11825 : 2109 : tree desc;
11826 : 2109 : tree res_desc;
11827 : 2109 : tree tmp;
11828 : 2109 : tree offset;
11829 : 2109 : tree zero_cond;
11830 : 2109 : tree not_same_shape;
11831 : 2109 : stmtblock_t shape_block;
11832 : 2109 : int n;
11833 : :
11834 : : /* Use the allocation done by the library. Substitute the lhs
11835 : : descriptor with a copy, whose data field is nulled.*/
11836 : 2109 : desc = build_fold_indirect_ref_loc (input_location, se->expr);
11837 : 2109 : if (POINTER_TYPE_P (TREE_TYPE (desc)))
11838 : 9 : desc = build_fold_indirect_ref_loc (input_location, desc);
11839 : :
11840 : : /* Unallocated, the descriptor does not have a dtype. */
11841 : 2109 : tmp = gfc_conv_descriptor_dtype (desc);
11842 : 2109 : if (dtype != NULL_TREE)
11843 : 13 : gfc_add_modify (&se->pre, tmp, dtype);
11844 : : else
11845 : 2096 : gfc_add_modify (&se->pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
11846 : :
11847 : 2109 : res_desc = gfc_evaluate_now (desc, &se->pre);
11848 : 2109 : gfc_conv_descriptor_data_set (&se->pre, res_desc, null_pointer_node);
11849 : 2109 : se->expr = gfc_build_addr_expr (NULL_TREE, res_desc);
11850 : :
11851 : : /* Free the lhs after the function call and copy the result data to
11852 : : the lhs descriptor. */
11853 : 2109 : tmp = gfc_conv_descriptor_data_get (desc);
11854 : 2109 : zero_cond = fold_build2_loc (input_location, EQ_EXPR,
11855 : : logical_type_node, tmp,
11856 : 2109 : build_int_cst (TREE_TYPE (tmp), 0));
11857 : 2109 : zero_cond = gfc_evaluate_now (zero_cond, &se->post);
11858 : 2109 : tmp = gfc_call_free (tmp);
11859 : 2109 : gfc_add_expr_to_block (&se->post, tmp);
11860 : :
11861 : 2109 : tmp = gfc_conv_descriptor_data_get (res_desc);
11862 : 2109 : gfc_conv_descriptor_data_set (&se->post, desc, tmp);
11863 : :
11864 : : /* Check that the shapes are the same between lhs and expression.
11865 : : The evaluation of the shape is done in 'shape_block' to avoid
11866 : : unitialized warnings from the lhs bounds. */
11867 : 2109 : not_same_shape = boolean_false_node;
11868 : 2109 : gfc_start_block (&shape_block);
11869 : 6801 : for (n = 0 ; n < rank; n++)
11870 : : {
11871 : 4692 : tree tmp1;
11872 : 4692 : tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
11873 : 4692 : tmp1 = gfc_conv_descriptor_lbound_get (res_desc, gfc_rank_cst[n]);
11874 : 4692 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
11875 : : gfc_array_index_type, tmp, tmp1);
11876 : 4692 : tmp1 = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
11877 : 4692 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
11878 : : gfc_array_index_type, tmp, tmp1);
11879 : 4692 : tmp1 = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
11880 : 4692 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
11881 : : gfc_array_index_type, tmp, tmp1);
11882 : 4692 : tmp = fold_build2_loc (input_location, NE_EXPR,
11883 : : logical_type_node, tmp,
11884 : : gfc_index_zero_node);
11885 : 4692 : tmp = gfc_evaluate_now (tmp, &shape_block);
11886 : 4692 : if (n == 0)
11887 : : not_same_shape = tmp;
11888 : : else
11889 : 2583 : not_same_shape = fold_build2_loc (input_location, TRUTH_OR_EXPR,
11890 : : logical_type_node, tmp,
11891 : : not_same_shape);
11892 : : }
11893 : :
11894 : : /* 'zero_cond' being true is equal to lhs not being allocated or the
11895 : : shapes being different. */
11896 : 2109 : tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR, logical_type_node,
11897 : : zero_cond, not_same_shape);
11898 : 2109 : gfc_add_modify (&shape_block, zero_cond, tmp);
11899 : 2109 : tmp = gfc_finish_block (&shape_block);
11900 : 2109 : tmp = build3_v (COND_EXPR, zero_cond,
11901 : : build_empty_stmt (input_location), tmp);
11902 : 2109 : gfc_add_expr_to_block (&se->post, tmp);
11903 : :
11904 : : /* Now reset the bounds returned from the function call to bounds based
11905 : : on the lhs lbounds, except where the lhs is not allocated or the shapes
11906 : : of 'variable and 'expr' are different. Set the offset accordingly. */
11907 : 2109 : offset = gfc_index_zero_node;
11908 : 6801 : for (n = 0 ; n < rank; n++)
11909 : : {
11910 : 4692 : tree lbound;
11911 : :
11912 : 4692 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
11913 : 4692 : lbound = fold_build3_loc (input_location, COND_EXPR,
11914 : : gfc_array_index_type, zero_cond,
11915 : : gfc_index_one_node, lbound);
11916 : 4692 : lbound = gfc_evaluate_now (lbound, &se->post);
11917 : :
11918 : 4692 : tmp = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
11919 : 4692 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
11920 : : gfc_array_index_type, tmp, lbound);
11921 : 4692 : gfc_conv_descriptor_lbound_set (&se->post, desc,
11922 : : gfc_rank_cst[n], lbound);
11923 : 4692 : gfc_conv_descriptor_ubound_set (&se->post, desc,
11924 : : gfc_rank_cst[n], tmp);
11925 : :
11926 : : /* Set stride and accumulate the offset. */
11927 : 4692 : tmp = gfc_conv_descriptor_stride_get (res_desc, gfc_rank_cst[n]);
11928 : 4692 : gfc_conv_descriptor_stride_set (&se->post, desc,
11929 : : gfc_rank_cst[n], tmp);
11930 : 4692 : tmp = fold_build2_loc (input_location, MULT_EXPR,
11931 : : gfc_array_index_type, lbound, tmp);
11932 : 4692 : offset = fold_build2_loc (input_location, MINUS_EXPR,
11933 : : gfc_array_index_type, offset, tmp);
11934 : 4692 : offset = gfc_evaluate_now (offset, &se->post);
11935 : : }
11936 : :
11937 : 2109 : gfc_conv_descriptor_offset_set (&se->post, desc, offset);
11938 : 2109 : }
11939 : :
11940 : :
11941 : :
11942 : : /* Try to translate array(:) = func (...), where func is a transformational
11943 : : array function, without using a temporary. Returns NULL if this isn't the
11944 : : case. */
11945 : :
11946 : : static tree
11947 : 14577 : gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
11948 : : {
11949 : 14577 : gfc_se se;
11950 : 14577 : gfc_ss *ss = NULL;
11951 : 14577 : gfc_component *comp = NULL;
11952 : 14577 : gfc_loopinfo loop;
11953 : 14577 : tree tmp;
11954 : 14577 : tree lhs;
11955 : 14577 : gfc_se final_se;
11956 : 14577 : gfc_symbol *sym = expr1->symtree->n.sym;
11957 : 14577 : bool finalizable = gfc_may_be_finalized (expr1->ts);
11958 : :
11959 : 14577 : if (arrayfunc_assign_needs_temporary (expr1, expr2))
11960 : : return NULL;
11961 : :
11962 : : /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
11963 : : functions. */
11964 : 7080 : comp = gfc_get_proc_ptr_comp (expr2);
11965 : :
11966 : 7080 : if (!(expr2->value.function.isym
11967 : 675 : || (comp && comp->attr.dimension)
11968 : 675 : || (!comp && gfc_return_by_reference (expr2->value.function.esym)
11969 : 675 : && expr2->value.function.esym->result->attr.dimension)))
11970 : 0 : return NULL;
11971 : :
11972 : 7080 : gfc_init_se (&se, NULL);
11973 : 7080 : gfc_start_block (&se.pre);
11974 : 7080 : se.want_pointer = 1;
11975 : :
11976 : : /* First the lhs must be finalized, if necessary. We use a copy of the symbol
11977 : : backend decl, stash the original away for the finalization so that the
11978 : : value used is that before the assignment. This is necessary because
11979 : : evaluation of the rhs expression using direct by reference can change
11980 : : the value. However, the standard mandates that the finalization must occur
11981 : : after evaluation of the rhs. */
11982 : 7080 : gfc_init_se (&final_se, NULL);
11983 : :
11984 : 7080 : if (finalizable)
11985 : : {
11986 : 33 : tmp = sym->backend_decl;
11987 : 33 : lhs = sym->backend_decl;
11988 : 33 : if (INDIRECT_REF_P (tmp))
11989 : 0 : tmp = TREE_OPERAND (tmp, 0);
11990 : 33 : sym->backend_decl = gfc_create_var (TREE_TYPE (tmp), "lhs");
11991 : 33 : gfc_add_modify (&se.pre, sym->backend_decl, tmp);
11992 : 33 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
11993 : : {
11994 : 0 : tmp = gfc_copy_alloc_comp (expr1->ts.u.derived, tmp, sym->backend_decl,
11995 : : expr1->rank, 0);
11996 : 0 : gfc_add_expr_to_block (&final_se.pre, tmp);
11997 : : }
11998 : : }
11999 : :
12000 : 33 : if (finalizable && gfc_assignment_finalizer_call (&final_se, expr1, false))
12001 : : {
12002 : 33 : gfc_add_block_to_block (&se.pre, &final_se.pre);
12003 : 33 : gfc_add_block_to_block (&se.post, &final_se.finalblock);
12004 : : }
12005 : :
12006 : 7080 : if (finalizable)
12007 : 33 : sym->backend_decl = lhs;
12008 : :
12009 : 7080 : gfc_conv_array_parameter (&se, expr1, false, NULL, NULL, NULL);
12010 : :
12011 : 7080 : if (expr1->ts.type == BT_DERIVED
12012 : 228 : && expr1->ts.u.derived->attr.alloc_comp)
12013 : : {
12014 : 80 : tmp = build_fold_indirect_ref_loc (input_location, se.expr);
12015 : 80 : tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, tmp,
12016 : : expr1->rank);
12017 : 80 : gfc_add_expr_to_block (&se.pre, tmp);
12018 : : }
12019 : :
12020 : 7080 : se.direct_byref = 1;
12021 : 7080 : se.ss = gfc_walk_expr (expr2);
12022 : 7080 : gcc_assert (se.ss != gfc_ss_terminator);
12023 : :
12024 : : /* Since this is a direct by reference call, references to the lhs can be
12025 : : used for finalization of the function result just as long as the blocks
12026 : : from final_se are added at the right time. */
12027 : 7080 : gfc_init_se (&final_se, NULL);
12028 : 7080 : if (finalizable && expr2->value.function.esym)
12029 : : {
12030 : 20 : final_se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
12031 : 20 : gfc_finalize_tree_expr (&final_se, expr2->ts.u.derived,
12032 : 20 : expr2->value.function.esym->attr,
12033 : : expr2->rank);
12034 : : }
12035 : :
12036 : : /* Reallocate on assignment needs the loopinfo for extrinsic functions.
12037 : : This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
12038 : : Clearly, this cannot be done for an allocatable function result, since
12039 : : the shape of the result is unknown and, in any case, the function must
12040 : : correctly take care of the reallocation internally. For intrinsic
12041 : : calls, the array data is freed and the library takes care of allocation.
12042 : : TODO: Add logic of trans-array.cc: gfc_alloc_allocatable_for_assignment
12043 : : to the library. */
12044 : 7080 : if (flag_realloc_lhs
12045 : 7005 : && gfc_is_reallocatable_lhs (expr1)
12046 : 9355 : && !gfc_expr_attr (expr1).codimension
12047 : 2275 : && !gfc_is_coindexed (expr1)
12048 : 9355 : && !(expr2->value.function.esym
12049 : 166 : && expr2->value.function.esym->result->attr.allocatable))
12050 : : {
12051 : 2275 : realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
12052 : :
12053 : 2275 : if (!expr2->value.function.isym)
12054 : : {
12055 : 166 : ss = gfc_walk_expr (expr1);
12056 : 166 : gcc_assert (ss != gfc_ss_terminator);
12057 : :
12058 : 166 : realloc_lhs_loop_for_fcn_call (&se, &expr1->where, &ss, &loop);
12059 : 166 : ss->is_alloc_lhs = 1;
12060 : : }
12061 : : else
12062 : : {
12063 : 2109 : tree dtype = NULL_TREE;
12064 : 2109 : tree type = gfc_typenode_for_spec (&expr2->ts);
12065 : 2109 : if (expr1->ts.type == BT_CLASS)
12066 : : {
12067 : 13 : tmp = gfc_class_vptr_get (sym->backend_decl);
12068 : 13 : tree tmp2 = gfc_get_symbol_decl (gfc_find_vtab (&expr2->ts));
12069 : 13 : tmp2 = gfc_build_addr_expr (TREE_TYPE (tmp), tmp2);
12070 : 13 : gfc_add_modify (&se.pre, tmp, tmp2);
12071 : 13 : dtype = gfc_get_dtype_rank_type (expr1->rank,type);
12072 : : }
12073 : 2109 : fcncall_realloc_result (&se, expr1->rank, dtype);
12074 : : }
12075 : : }
12076 : :
12077 : 7080 : gfc_conv_function_expr (&se, expr2);
12078 : :
12079 : : /* Fix the result. */
12080 : 7080 : gfc_add_block_to_block (&se.pre, &se.post);
12081 : 7080 : if (finalizable)
12082 : 33 : gfc_add_block_to_block (&se.pre, &final_se.pre);
12083 : :
12084 : : /* Do the finalization, including final calls from function arguments. */
12085 : 33 : if (finalizable)
12086 : : {
12087 : 33 : gfc_add_block_to_block (&se.pre, &final_se.post);
12088 : 33 : gfc_add_block_to_block (&se.pre, &se.finalblock);
12089 : 33 : gfc_add_block_to_block (&se.pre, &final_se.finalblock);
12090 : : }
12091 : :
12092 : 7080 : if (ss)
12093 : 166 : gfc_cleanup_loop (&loop);
12094 : : else
12095 : 6914 : gfc_free_ss_chain (se.ss);
12096 : :
12097 : 7080 : return gfc_finish_block (&se.pre);
12098 : : }
12099 : :
12100 : :
12101 : : /* Try to efficiently translate array(:) = 0. Return NULL if this
12102 : : can't be done. */
12103 : :
12104 : : static tree
12105 : 3924 : gfc_trans_zero_assign (gfc_expr * expr)
12106 : : {
12107 : 3924 : tree dest, len, type;
12108 : 3924 : tree tmp;
12109 : 3924 : gfc_symbol *sym;
12110 : :
12111 : 3924 : sym = expr->symtree->n.sym;
12112 : 3924 : dest = gfc_get_symbol_decl (sym);
12113 : :
12114 : 3924 : type = TREE_TYPE (dest);
12115 : 3924 : if (POINTER_TYPE_P (type))
12116 : 247 : type = TREE_TYPE (type);
12117 : 3924 : if (GFC_ARRAY_TYPE_P (type))
12118 : : {
12119 : : /* Determine the length of the array. */
12120 : 2749 : len = GFC_TYPE_ARRAY_SIZE (type);
12121 : 2749 : if (!len || TREE_CODE (len) != INTEGER_CST)
12122 : : return NULL_TREE;
12123 : : }
12124 : 1175 : else if (GFC_DESCRIPTOR_TYPE_P (type)
12125 : 1175 : && gfc_is_simply_contiguous (expr, false, false))
12126 : : {
12127 : 1075 : if (POINTER_TYPE_P (TREE_TYPE (dest)))
12128 : 4 : dest = build_fold_indirect_ref_loc (input_location, dest);
12129 : 1075 : len = gfc_conv_descriptor_size (dest, GFC_TYPE_ARRAY_RANK (type));
12130 : 1075 : dest = gfc_conv_descriptor_data_get (dest);
12131 : : }
12132 : : else
12133 : 100 : return NULL_TREE;
12134 : :
12135 : : /* If we are zeroing a local array avoid taking its address by emitting
12136 : : a = {} instead. */
12137 : 3646 : if (!POINTER_TYPE_P (TREE_TYPE (dest)))
12138 : 2529 : return build2_loc (input_location, MODIFY_EXPR, void_type_node,
12139 : 2529 : dest, build_constructor (TREE_TYPE (dest),
12140 : 2529 : NULL));
12141 : :
12142 : : /* Multiply len by element size. */
12143 : 1117 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
12144 : 1117 : len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
12145 : : len, fold_convert (gfc_array_index_type, tmp));
12146 : :
12147 : : /* Convert arguments to the correct types. */
12148 : 1117 : dest = fold_convert (pvoid_type_node, dest);
12149 : 1117 : len = fold_convert (size_type_node, len);
12150 : :
12151 : : /* Construct call to __builtin_memset. */
12152 : 1117 : tmp = build_call_expr_loc (input_location,
12153 : : builtin_decl_explicit (BUILT_IN_MEMSET),
12154 : : 3, dest, integer_zero_node, len);
12155 : 1117 : return fold_convert (void_type_node, tmp);
12156 : : }
12157 : :
12158 : :
12159 : : /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
12160 : : that constructs the call to __builtin_memcpy. */
12161 : :
12162 : : tree
12163 : 7014 : gfc_build_memcpy_call (tree dst, tree src, tree len)
12164 : : {
12165 : 7014 : tree tmp;
12166 : :
12167 : : /* Convert arguments to the correct types. */
12168 : 7014 : if (!POINTER_TYPE_P (TREE_TYPE (dst)))
12169 : 6781 : dst = gfc_build_addr_expr (pvoid_type_node, dst);
12170 : : else
12171 : 233 : dst = fold_convert (pvoid_type_node, dst);
12172 : :
12173 : 7014 : if (!POINTER_TYPE_P (TREE_TYPE (src)))
12174 : 6680 : src = gfc_build_addr_expr (pvoid_type_node, src);
12175 : : else
12176 : 334 : src = fold_convert (pvoid_type_node, src);
12177 : :
12178 : 7014 : len = fold_convert (size_type_node, len);
12179 : :
12180 : : /* Construct call to __builtin_memcpy. */
12181 : 7014 : tmp = build_call_expr_loc (input_location,
12182 : : builtin_decl_explicit (BUILT_IN_MEMCPY),
12183 : : 3, dst, src, len);
12184 : 7014 : return fold_convert (void_type_node, tmp);
12185 : : }
12186 : :
12187 : :
12188 : : /* Try to efficiently translate dst(:) = src(:). Return NULL if this
12189 : : can't be done. EXPR1 is the destination/lhs and EXPR2 is the
12190 : : source/rhs, both are gfc_full_array_ref_p which have been checked for
12191 : : dependencies. */
12192 : :
12193 : : static tree
12194 : 2503 : gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
12195 : : {
12196 : 2503 : tree dst, dlen, dtype;
12197 : 2503 : tree src, slen, stype;
12198 : 2503 : tree tmp;
12199 : :
12200 : 2503 : dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
12201 : 2503 : src = gfc_get_symbol_decl (expr2->symtree->n.sym);
12202 : :
12203 : 2503 : dtype = TREE_TYPE (dst);
12204 : 2503 : if (POINTER_TYPE_P (dtype))
12205 : 223 : dtype = TREE_TYPE (dtype);
12206 : 2503 : stype = TREE_TYPE (src);
12207 : 2503 : if (POINTER_TYPE_P (stype))
12208 : 265 : stype = TREE_TYPE (stype);
12209 : :
12210 : 2503 : if (!GFC_ARRAY_TYPE_P (dtype) || !GFC_ARRAY_TYPE_P (stype))
12211 : : return NULL_TREE;
12212 : :
12213 : : /* Determine the lengths of the arrays. */
12214 : 1533 : dlen = GFC_TYPE_ARRAY_SIZE (dtype);
12215 : 1533 : if (!dlen || TREE_CODE (dlen) != INTEGER_CST)
12216 : : return NULL_TREE;
12217 : 1444 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
12218 : 1444 : dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
12219 : : dlen, fold_convert (gfc_array_index_type, tmp));
12220 : :
12221 : 1444 : slen = GFC_TYPE_ARRAY_SIZE (stype);
12222 : 1444 : if (!slen || TREE_CODE (slen) != INTEGER_CST)
12223 : : return NULL_TREE;
12224 : 1438 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (stype));
12225 : 1438 : slen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
12226 : : slen, fold_convert (gfc_array_index_type, tmp));
12227 : :
12228 : : /* Sanity check that they are the same. This should always be
12229 : : the case, as we should already have checked for conformance. */
12230 : 1438 : if (!tree_int_cst_equal (slen, dlen))
12231 : : return NULL_TREE;
12232 : :
12233 : 1438 : return gfc_build_memcpy_call (dst, src, dlen);
12234 : : }
12235 : :
12236 : :
12237 : : /* Try to efficiently translate array(:) = (/ ... /). Return NULL if
12238 : : this can't be done. EXPR1 is the destination/lhs for which
12239 : : gfc_full_array_ref_p is true, and EXPR2 is the source/rhs. */
12240 : :
12241 : : static tree
12242 : 7466 : gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
12243 : : {
12244 : 7466 : unsigned HOST_WIDE_INT nelem;
12245 : 7466 : tree dst, dtype;
12246 : 7466 : tree src, stype;
12247 : 7466 : tree len;
12248 : 7466 : tree tmp;
12249 : :
12250 : 7466 : nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
12251 : 7466 : if (nelem == 0)
12252 : : return NULL_TREE;
12253 : :
12254 : 5836 : dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
12255 : 5836 : dtype = TREE_TYPE (dst);
12256 : 5836 : if (POINTER_TYPE_P (dtype))
12257 : 252 : dtype = TREE_TYPE (dtype);
12258 : 5836 : if (!GFC_ARRAY_TYPE_P (dtype))
12259 : : return NULL_TREE;
12260 : :
12261 : : /* Determine the lengths of the array. */
12262 : 5154 : len = GFC_TYPE_ARRAY_SIZE (dtype);
12263 : 5154 : if (!len || TREE_CODE (len) != INTEGER_CST)
12264 : : return NULL_TREE;
12265 : :
12266 : : /* Confirm that the constructor is the same size. */
12267 : 5056 : if (compare_tree_int (len, nelem) != 0)
12268 : : return NULL_TREE;
12269 : :
12270 : 5056 : tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
12271 : 5056 : len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
12272 : : fold_convert (gfc_array_index_type, tmp));
12273 : :
12274 : 5056 : stype = gfc_typenode_for_spec (&expr2->ts);
12275 : 5056 : src = gfc_build_constant_array_constructor (expr2, stype);
12276 : :
12277 : 5056 : return gfc_build_memcpy_call (dst, src, len);
12278 : : }
12279 : :
12280 : :
12281 : : /* Tells whether the expression is to be treated as a variable reference. */
12282 : :
12283 : : bool
12284 : 300829 : gfc_expr_is_variable (gfc_expr *expr)
12285 : : {
12286 : 301089 : gfc_expr *arg;
12287 : 301089 : gfc_component *comp;
12288 : 301089 : gfc_symbol *func_ifc;
12289 : :
12290 : 301089 : if (expr->expr_type == EXPR_VARIABLE)
12291 : : return true;
12292 : :
12293 : 268089 : arg = gfc_get_noncopying_intrinsic_argument (expr);
12294 : 268089 : if (arg)
12295 : : {
12296 : 260 : gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
12297 : : return gfc_expr_is_variable (arg);
12298 : : }
12299 : :
12300 : : /* A data-pointer-returning function should be considered as a variable
12301 : : too. */
12302 : 267829 : if (expr->expr_type == EXPR_FUNCTION
12303 : 35355 : && expr->ref == NULL)
12304 : : {
12305 : 34988 : if (expr->value.function.isym != NULL)
12306 : : return false;
12307 : :
12308 : 8980 : if (expr->value.function.esym != NULL)
12309 : : {
12310 : 8971 : func_ifc = expr->value.function.esym;
12311 : 8971 : goto found_ifc;
12312 : : }
12313 : 9 : gcc_assert (expr->symtree);
12314 : 9 : func_ifc = expr->symtree->n.sym;
12315 : 9 : goto found_ifc;
12316 : : }
12317 : :
12318 : 232841 : comp = gfc_get_proc_ptr_comp (expr);
12319 : 232841 : if ((expr->expr_type == EXPR_PPC || expr->expr_type == EXPR_FUNCTION)
12320 : 367 : && comp)
12321 : : {
12322 : 265 : func_ifc = comp->ts.interface;
12323 : 265 : goto found_ifc;
12324 : : }
12325 : :
12326 : 232576 : if (expr->expr_type == EXPR_COMPCALL)
12327 : : {
12328 : 0 : gcc_assert (!expr->value.compcall.tbp->is_generic);
12329 : 0 : func_ifc = expr->value.compcall.tbp->u.specific->n.sym;
12330 : 0 : goto found_ifc;
12331 : : }
12332 : :
12333 : : return false;
12334 : :
12335 : 9245 : found_ifc:
12336 : 9245 : gcc_assert (func_ifc->attr.function
12337 : : && func_ifc->result != NULL);
12338 : 9245 : return func_ifc->result->attr.pointer;
12339 : : }
12340 : :
12341 : :
12342 : : /* Is the lhs OK for automatic reallocation? */
12343 : :
12344 : : static bool
12345 : 255778 : is_scalar_reallocatable_lhs (gfc_expr *expr)
12346 : : {
12347 : 255778 : gfc_ref * ref;
12348 : :
12349 : : /* An allocatable variable with no reference. */
12350 : 255778 : if (expr->symtree->n.sym->attr.allocatable
12351 : 6543 : && !expr->ref)
12352 : : return true;
12353 : :
12354 : : /* All that can be left are allocatable components. However, we do
12355 : : not check for allocatable components here because the expression
12356 : : could be an allocatable component of a pointer component. */
12357 : 253150 : if (expr->symtree->n.sym->ts.type != BT_DERIVED
12358 : 232423 : && expr->symtree->n.sym->ts.type != BT_CLASS)
12359 : : return false;
12360 : :
12361 : : /* Find an allocatable component ref last. */
12362 : 36915 : for (ref = expr->ref; ref; ref = ref->next)
12363 : 15223 : if (ref->type == REF_COMPONENT
12364 : 11342 : && !ref->next
12365 : 8840 : && ref->u.c.component->attr.allocatable)
12366 : : return true;
12367 : :
12368 : : return false;
12369 : : }
12370 : :
12371 : :
12372 : : /* Allocate or reallocate scalar lhs, as necessary. */
12373 : :
12374 : : static void
12375 : 3365 : alloc_scalar_allocatable_for_assignment (stmtblock_t *block,
12376 : : tree string_length,
12377 : : gfc_expr *expr1,
12378 : : gfc_expr *expr2)
12379 : :
12380 : : {
12381 : 3365 : tree cond;
12382 : 3365 : tree tmp;
12383 : 3365 : tree size;
12384 : 3365 : tree size_in_bytes;
12385 : 3365 : tree jump_label1;
12386 : 3365 : tree jump_label2;
12387 : 3365 : gfc_se lse;
12388 : 3365 : gfc_ref *ref;
12389 : :
12390 : 3365 : if (!expr1 || expr1->rank)
12391 : 0 : return;
12392 : :
12393 : 3365 : if (!expr2 || expr2->rank)
12394 : : return;
12395 : :
12396 : 4589 : for (ref = expr1->ref; ref; ref = ref->next)
12397 : 1224 : if (ref->type == REF_SUBSTRING)
12398 : : return;
12399 : :
12400 : 3365 : realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
12401 : :
12402 : : /* Since this is a scalar lhs, we can afford to do this. That is,
12403 : : there is no risk of side effects being repeated. */
12404 : 3365 : gfc_init_se (&lse, NULL);
12405 : 3365 : lse.want_pointer = 1;
12406 : 3365 : gfc_conv_expr (&lse, expr1);
12407 : :
12408 : 3365 : jump_label1 = gfc_build_label_decl (NULL_TREE);
12409 : 3365 : jump_label2 = gfc_build_label_decl (NULL_TREE);
12410 : :
12411 : : /* Do the allocation if the lhs is NULL. Otherwise go to label 1. */
12412 : 3365 : tmp = build_int_cst (TREE_TYPE (lse.expr), 0);
12413 : 3365 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
12414 : : lse.expr, tmp);
12415 : 3365 : tmp = build3_v (COND_EXPR, cond,
12416 : : build1_v (GOTO_EXPR, jump_label1),
12417 : : build_empty_stmt (input_location));
12418 : 3365 : gfc_add_expr_to_block (block, tmp);
12419 : :
12420 : 3365 : if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
12421 : : {
12422 : : /* Use the rhs string length and the lhs element size. Note that 'size' is
12423 : : used below for the string-length comparison, only. */
12424 : 1365 : size = string_length;
12425 : 1365 : tmp = TYPE_SIZE_UNIT (gfc_get_char_type (expr1->ts.kind));
12426 : 2730 : size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
12427 : 1365 : TREE_TYPE (tmp), tmp,
12428 : 1365 : fold_convert (TREE_TYPE (tmp), size));
12429 : : }
12430 : : else
12431 : : {
12432 : : /* Otherwise use the length in bytes of the rhs. */
12433 : 2000 : size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr1->ts));
12434 : 2000 : size_in_bytes = size;
12435 : : }
12436 : :
12437 : 3365 : size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
12438 : : size_in_bytes, size_one_node);
12439 : :
12440 : 3365 : if (gfc_caf_attr (expr1).codimension && flag_coarray == GFC_FCOARRAY_LIB)
12441 : : {
12442 : 31 : tree caf_decl, token;
12443 : 31 : gfc_se caf_se;
12444 : 31 : symbol_attribute attr;
12445 : :
12446 : 31 : gfc_clear_attr (&attr);
12447 : 31 : gfc_init_se (&caf_se, NULL);
12448 : :
12449 : 31 : caf_decl = gfc_get_tree_for_caf_expr (expr1);
12450 : 31 : gfc_get_caf_token_offset (&caf_se, &token, NULL, caf_decl, NULL_TREE,
12451 : : NULL);
12452 : 31 : gfc_add_block_to_block (block, &caf_se.pre);
12453 : 31 : gfc_allocate_allocatable (block, lse.expr, size_in_bytes,
12454 : : gfc_build_addr_expr (NULL_TREE, token),
12455 : : NULL_TREE, NULL_TREE, NULL_TREE, jump_label1,
12456 : : expr1, 1);
12457 : : }
12458 : 3334 : else if (expr1->ts.type == BT_DERIVED && expr1->ts.u.derived->attr.alloc_comp)
12459 : : {
12460 : 34 : tmp = build_call_expr_loc (input_location,
12461 : : builtin_decl_explicit (BUILT_IN_CALLOC),
12462 : : 2, build_one_cst (size_type_node),
12463 : : size_in_bytes);
12464 : 34 : tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
12465 : 34 : gfc_add_modify (block, lse.expr, tmp);
12466 : : }
12467 : : else
12468 : : {
12469 : 3300 : tmp = build_call_expr_loc (input_location,
12470 : : builtin_decl_explicit (BUILT_IN_MALLOC),
12471 : : 1, size_in_bytes);
12472 : 3300 : tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
12473 : 3300 : gfc_add_modify (block, lse.expr, tmp);
12474 : : }
12475 : :
12476 : 3365 : if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
12477 : : {
12478 : : /* Deferred characters need checking for lhs and rhs string
12479 : : length. Other deferred parameter variables will have to
12480 : : come here too. */
12481 : 1365 : tmp = build1_v (GOTO_EXPR, jump_label2);
12482 : 1365 : gfc_add_expr_to_block (block, tmp);
12483 : : }
12484 : 3365 : tmp = build1_v (LABEL_EXPR, jump_label1);
12485 : 3365 : gfc_add_expr_to_block (block, tmp);
12486 : :
12487 : : /* For a deferred length character, reallocate if lengths of lhs and
12488 : : rhs are different. */
12489 : 3365 : if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
12490 : : {
12491 : 1365 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
12492 : : lse.string_length,
12493 : 1365 : fold_convert (TREE_TYPE (lse.string_length),
12494 : : size));
12495 : : /* Jump past the realloc if the lengths are the same. */
12496 : 1365 : tmp = build3_v (COND_EXPR, cond,
12497 : : build1_v (GOTO_EXPR, jump_label2),
12498 : : build_empty_stmt (input_location));
12499 : 1365 : gfc_add_expr_to_block (block, tmp);
12500 : 1365 : tmp = build_call_expr_loc (input_location,
12501 : : builtin_decl_explicit (BUILT_IN_REALLOC),
12502 : : 2, fold_convert (pvoid_type_node, lse.expr),
12503 : : size_in_bytes);
12504 : 1365 : tree omp_cond = NULL_TREE;
12505 : 1365 : if (flag_openmp_allocators)
12506 : : {
12507 : 1 : tree omp_tmp;
12508 : 1 : omp_cond = gfc_omp_call_is_alloc (lse.expr);
12509 : 1 : omp_cond = gfc_evaluate_now (omp_cond, block);
12510 : :
12511 : 1 : omp_tmp = builtin_decl_explicit (BUILT_IN_GOMP_REALLOC);
12512 : 1 : omp_tmp = build_call_expr_loc (input_location, omp_tmp, 4,
12513 : : fold_convert (pvoid_type_node,
12514 : : lse.expr), size_in_bytes,
12515 : : build_zero_cst (ptr_type_node),
12516 : : build_zero_cst (ptr_type_node));
12517 : 1 : tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
12518 : : omp_cond, omp_tmp, tmp);
12519 : : }
12520 : 1365 : tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
12521 : 1365 : gfc_add_modify (block, lse.expr, tmp);
12522 : 1365 : if (omp_cond)
12523 : 1 : gfc_add_expr_to_block (block,
12524 : : build3_loc (input_location, COND_EXPR,
12525 : : void_type_node, omp_cond,
12526 : : gfc_omp_call_add_alloc (lse.expr),
12527 : : build_empty_stmt (input_location)));
12528 : 1365 : tmp = build1_v (LABEL_EXPR, jump_label2);
12529 : 1365 : gfc_add_expr_to_block (block, tmp);
12530 : :
12531 : : /* Update the lhs character length. */
12532 : 1365 : size = string_length;
12533 : 1365 : gfc_add_modify (block, lse.string_length,
12534 : 1365 : fold_convert (TREE_TYPE (lse.string_length), size));
12535 : : }
12536 : : }
12537 : :
12538 : : /* Check for assignments of the type
12539 : :
12540 : : a = a + 4
12541 : :
12542 : : to make sure we do not check for reallocation unneccessarily. */
12543 : :
12544 : :
12545 : : static bool
12546 : 6455 : is_runtime_conformable (gfc_expr *expr1, gfc_expr *expr2)
12547 : : {
12548 : 6760 : gfc_actual_arglist *a;
12549 : 6760 : gfc_expr *e1, *e2;
12550 : :
12551 : 6760 : switch (expr2->expr_type)
12552 : : {
12553 : 1776 : case EXPR_VARIABLE:
12554 : 1776 : return gfc_dep_compare_expr (expr1, expr2) == 0;
12555 : :
12556 : 2772 : case EXPR_FUNCTION:
12557 : 2772 : if (expr2->value.function.esym
12558 : 269 : && expr2->value.function.esym->attr.elemental)
12559 : : {
12560 : 51 : for (a = expr2->value.function.actual; a != NULL; a = a->next)
12561 : : {
12562 : 50 : e1 = a->expr;
12563 : 50 : if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
12564 : : return false;
12565 : : }
12566 : : return true;
12567 : : }
12568 : 2734 : else if (expr2->value.function.isym
12569 : 2489 : && expr2->value.function.isym->elemental)
12570 : : {
12571 : 324 : for (a = expr2->value.function.actual; a != NULL; a = a->next)
12572 : : {
12573 : 314 : e1 = a->expr;
12574 : 314 : if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
12575 : : return false;
12576 : : }
12577 : : return true;
12578 : : }
12579 : :
12580 : : break;
12581 : :
12582 : 499 : case EXPR_OP:
12583 : 499 : switch (expr2->value.op.op)
12584 : : {
12585 : 34 : case INTRINSIC_NOT:
12586 : 34 : case INTRINSIC_UPLUS:
12587 : 34 : case INTRINSIC_UMINUS:
12588 : 34 : case INTRINSIC_PARENTHESES:
12589 : 34 : return is_runtime_conformable (expr1, expr2->value.op.op1);
12590 : :
12591 : 440 : case INTRINSIC_PLUS:
12592 : 440 : case INTRINSIC_MINUS:
12593 : 440 : case INTRINSIC_TIMES:
12594 : 440 : case INTRINSIC_DIVIDE:
12595 : 440 : case INTRINSIC_POWER:
12596 : 440 : case INTRINSIC_AND:
12597 : 440 : case INTRINSIC_OR:
12598 : 440 : case INTRINSIC_EQV:
12599 : 440 : case INTRINSIC_NEQV:
12600 : 440 : case INTRINSIC_EQ:
12601 : 440 : case INTRINSIC_NE:
12602 : 440 : case INTRINSIC_GT:
12603 : 440 : case INTRINSIC_GE:
12604 : 440 : case INTRINSIC_LT:
12605 : 440 : case INTRINSIC_LE:
12606 : 440 : case INTRINSIC_EQ_OS:
12607 : 440 : case INTRINSIC_NE_OS:
12608 : 440 : case INTRINSIC_GT_OS:
12609 : 440 : case INTRINSIC_GE_OS:
12610 : 440 : case INTRINSIC_LT_OS:
12611 : 440 : case INTRINSIC_LE_OS:
12612 : :
12613 : 440 : e1 = expr2->value.op.op1;
12614 : 440 : e2 = expr2->value.op.op2;
12615 : :
12616 : 440 : if (e1->rank == 0 && e2->rank > 0)
12617 : : return is_runtime_conformable (expr1, e2);
12618 : 382 : else if (e1->rank > 0 && e2->rank == 0)
12619 : : return is_runtime_conformable (expr1, e1);
12620 : 169 : else if (e1->rank > 0 && e2->rank > 0)
12621 : 169 : return is_runtime_conformable (expr1, e1)
12622 : 169 : && is_runtime_conformable (expr1, e2);
12623 : : break;
12624 : :
12625 : : default:
12626 : : break;
12627 : :
12628 : : }
12629 : :
12630 : : break;
12631 : :
12632 : : default:
12633 : : break;
12634 : : }
12635 : : return false;
12636 : : }
12637 : :
12638 : :
12639 : : static tree
12640 : 3189 : trans_class_assignment (stmtblock_t *block, gfc_expr *lhs, gfc_expr *rhs,
12641 : : gfc_se *lse, gfc_se *rse, bool use_vptr_copy,
12642 : : bool class_realloc)
12643 : : {
12644 : 3189 : tree tmp, fcn, stdcopy, to_len, from_len, vptr, old_vptr, rhs_vptr;
12645 : 3189 : vec<tree, va_gc> *args = NULL;
12646 : 3189 : bool final_expr;
12647 : :
12648 : 3189 : final_expr = gfc_assignment_finalizer_call (lse, lhs, false);
12649 : 3189 : if (final_expr)
12650 : : {
12651 : 432 : if (rse->loop)
12652 : 172 : gfc_prepend_expr_to_block (&rse->loop->pre,
12653 : : gfc_finish_block (&lse->finalblock));
12654 : : else
12655 : 260 : gfc_add_block_to_block (block, &lse->finalblock);
12656 : : }
12657 : :
12658 : : /* Store the old vptr so that dynamic types can be compared for
12659 : : reallocation to occur or not. */
12660 : 3189 : if (class_realloc)
12661 : : {
12662 : 272 : tmp = lse->expr;
12663 : 272 : if (!GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
12664 : 18 : tmp = gfc_get_class_from_expr (tmp);
12665 : : }
12666 : :
12667 : 3189 : vptr = trans_class_vptr_len_assignment (block, lhs, rhs, rse, &to_len,
12668 : : &from_len, &rhs_vptr);
12669 : 3189 : if (rhs_vptr == NULL_TREE)
12670 : 61 : rhs_vptr = vptr;
12671 : :
12672 : : /* Generate (re)allocation of the lhs. */
12673 : 3189 : if (class_realloc)
12674 : : {
12675 : 272 : stmtblock_t alloc, re_alloc;
12676 : 272 : tree class_han, re, size;
12677 : :
12678 : 272 : if (tmp && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
12679 : 254 : old_vptr = gfc_evaluate_now (gfc_class_vptr_get (tmp), block);
12680 : : else
12681 : 18 : old_vptr = build_int_cst (TREE_TYPE (vptr), 0);
12682 : :
12683 : 272 : size = gfc_vptr_size_get (rhs_vptr);
12684 : :
12685 : : /* Take into account _len of unlimited polymorphic entities.
12686 : : TODO: handle class(*) allocatable function results on rhs. */
12687 : 272 : if (UNLIMITED_POLY (rhs))
12688 : : {
12689 : 18 : tree len;
12690 : 18 : if (rhs->expr_type == EXPR_VARIABLE)
12691 : 12 : len = trans_get_upoly_len (block, rhs);
12692 : : else
12693 : 6 : len = gfc_class_len_get (tmp);
12694 : 18 : len = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
12695 : : fold_convert (size_type_node, len),
12696 : : size_one_node);
12697 : 18 : size = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (size),
12698 : 18 : size, fold_convert (TREE_TYPE (size), len));
12699 : 18 : }
12700 : 254 : else if (rhs->ts.type == BT_CHARACTER && rse->string_length)
12701 : 21 : size = fold_build2_loc (input_location, MULT_EXPR,
12702 : : gfc_charlen_type_node, size,
12703 : : rse->string_length);
12704 : :
12705 : :
12706 : 272 : tmp = lse->expr;
12707 : 272 : class_han = GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
12708 : 272 : ? gfc_class_data_get (tmp) : tmp;
12709 : :
12710 : 272 : if (!POINTER_TYPE_P (TREE_TYPE (class_han)))
12711 : 18 : class_han = gfc_build_addr_expr (NULL_TREE, class_han);
12712 : :
12713 : : /* Allocate block. */
12714 : 272 : gfc_init_block (&alloc);
12715 : 272 : gfc_allocate_using_malloc (&alloc, class_han, size, NULL_TREE);
12716 : :
12717 : : /* Reallocate if dynamic types are different. */
12718 : 272 : gfc_init_block (&re_alloc);
12719 : 272 : if (UNLIMITED_POLY (lhs) && rhs->ts.type == BT_CHARACTER)
12720 : : {
12721 : 21 : gfc_add_expr_to_block (&re_alloc, gfc_call_free (class_han));
12722 : 21 : gfc_allocate_using_malloc (&re_alloc, class_han, size, NULL_TREE);
12723 : : }
12724 : : else
12725 : : {
12726 : 251 : tmp = fold_convert (pvoid_type_node, class_han);
12727 : 251 : re = build_call_expr_loc (input_location,
12728 : : builtin_decl_explicit (BUILT_IN_REALLOC),
12729 : : 2, tmp, size);
12730 : 251 : re = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (tmp),
12731 : : tmp, re);
12732 : 251 : tmp = fold_build2_loc (input_location, NE_EXPR,
12733 : : logical_type_node, rhs_vptr, old_vptr);
12734 : 251 : re = fold_build3_loc (input_location, COND_EXPR, void_type_node,
12735 : : tmp, re, build_empty_stmt (input_location));
12736 : 251 : gfc_add_expr_to_block (&re_alloc, re);
12737 : : }
12738 : 272 : tree realloc_expr = lhs->ts.type == BT_CLASS ?
12739 : 254 : gfc_finish_block (&re_alloc) :
12740 : 18 : build_empty_stmt (input_location);
12741 : :
12742 : : /* Allocate if _data is NULL, reallocate otherwise. */
12743 : 272 : tmp = fold_build2_loc (input_location, EQ_EXPR,
12744 : : logical_type_node, class_han,
12745 : : build_int_cst (prvoid_type_node, 0));
12746 : 272 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
12747 : : gfc_unlikely (tmp,
12748 : : PRED_FORTRAN_FAIL_ALLOC),
12749 : : gfc_finish_block (&alloc),
12750 : : realloc_expr);
12751 : 272 : gfc_add_expr_to_block (&lse->pre, tmp);
12752 : : }
12753 : :
12754 : 3189 : fcn = gfc_vptr_copy_get (vptr);
12755 : :
12756 : 3189 : tmp = GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr))
12757 : 3189 : ? gfc_class_data_get (rse->expr) : rse->expr;
12758 : 3189 : if (use_vptr_copy)
12759 : : {
12760 : 5362 : if (!POINTER_TYPE_P (TREE_TYPE (tmp))
12761 : 518 : || INDIRECT_REF_P (tmp)
12762 : 397 : || (rhs->ts.type == BT_DERIVED
12763 : 0 : && rhs->ts.u.derived->attr.unlimited_polymorphic
12764 : 0 : && !rhs->ts.u.derived->attr.pointer
12765 : 0 : && !rhs->ts.u.derived->attr.allocatable)
12766 : 3334 : || (UNLIMITED_POLY (rhs)
12767 : 134 : && !CLASS_DATA (rhs)->attr.pointer
12768 : 43 : && !CLASS_DATA (rhs)->attr.allocatable))
12769 : 2540 : vec_safe_push (args, gfc_build_addr_expr (NULL_TREE, tmp));
12770 : : else
12771 : 397 : vec_safe_push (args, tmp);
12772 : 2937 : tmp = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
12773 : 2937 : ? gfc_class_data_get (lse->expr) : lse->expr;
12774 : 5177 : if (!POINTER_TYPE_P (TREE_TYPE (tmp))
12775 : 697 : || INDIRECT_REF_P (tmp)
12776 : 254 : || (lhs->ts.type == BT_DERIVED
12777 : 0 : && lhs->ts.u.derived->attr.unlimited_polymorphic
12778 : 0 : && !lhs->ts.u.derived->attr.pointer
12779 : 0 : && !lhs->ts.u.derived->attr.allocatable)
12780 : 3191 : || (UNLIMITED_POLY (lhs)
12781 : 95 : && !CLASS_DATA (lhs)->attr.pointer
12782 : 95 : && !CLASS_DATA (lhs)->attr.allocatable))
12783 : 2683 : vec_safe_push (args, gfc_build_addr_expr (NULL_TREE, tmp));
12784 : : else
12785 : 254 : vec_safe_push (args, tmp);
12786 : :
12787 : 2937 : stdcopy = build_call_vec (TREE_TYPE (TREE_TYPE (fcn)), fcn, args);
12788 : :
12789 : 2937 : if (to_len != NULL_TREE && !integer_zerop (from_len))
12790 : : {
12791 : 400 : tree extcopy;
12792 : 400 : vec_safe_push (args, from_len);
12793 : 400 : vec_safe_push (args, to_len);
12794 : 400 : extcopy = build_call_vec (TREE_TYPE (TREE_TYPE (fcn)), fcn, args);
12795 : :
12796 : 400 : tmp = fold_build2_loc (input_location, GT_EXPR,
12797 : : logical_type_node, from_len,
12798 : 400 : build_zero_cst (TREE_TYPE (from_len)));
12799 : 400 : return fold_build3_loc (input_location, COND_EXPR,
12800 : : void_type_node, tmp,
12801 : 400 : extcopy, stdcopy);
12802 : : }
12803 : : else
12804 : 2537 : return stdcopy;
12805 : : }
12806 : : else
12807 : : {
12808 : 252 : tree rhst = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
12809 : 252 : ? gfc_class_data_get (lse->expr) : lse->expr;
12810 : 252 : stmtblock_t tblock;
12811 : 252 : gfc_init_block (&tblock);
12812 : 252 : if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
12813 : 0 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
12814 : 252 : if (!POINTER_TYPE_P (TREE_TYPE (rhst)))
12815 : 0 : rhst = gfc_build_addr_expr (NULL_TREE, rhst);
12816 : : /* When coming from a ptr_copy lhs and rhs are swapped. */
12817 : 252 : gfc_add_modify_loc (input_location, &tblock, rhst,
12818 : 252 : fold_convert (TREE_TYPE (rhst), tmp));
12819 : 252 : return gfc_finish_block (&tblock);
12820 : : }
12821 : : }
12822 : :
12823 : : bool
12824 : 296116 : is_assoc_assign (gfc_expr *lhs, gfc_expr *rhs)
12825 : : {
12826 : 296116 : if (lhs->expr_type != EXPR_VARIABLE || rhs->expr_type != EXPR_VARIABLE)
12827 : : return false;
12828 : :
12829 : 30145 : return lhs->symtree->n.sym->assoc
12830 : 30145 : && lhs->symtree->n.sym->assoc->target == rhs;
12831 : : }
12832 : :
12833 : : /* Subroutine of gfc_trans_assignment that actually scalarizes the
12834 : : assignment. EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
12835 : : init_flag indicates initialization expressions and dealloc that no
12836 : : deallocate prior assignment is needed (if in doubt, set true).
12837 : : When PTR_COPY is set and expr1 is a class type, then use the _vptr-copy
12838 : : routine instead of a pointer assignment. Alias resolution is only done,
12839 : : when MAY_ALIAS is set (the default). This flag is used by ALLOCATE()
12840 : : where it is known, that newly allocated memory on the lhs can never be
12841 : : an alias of the rhs. */
12842 : :
12843 : : static tree
12844 : 296116 : gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
12845 : : bool dealloc, bool use_vptr_copy, bool may_alias)
12846 : : {
12847 : 296116 : gfc_se lse;
12848 : 296116 : gfc_se rse;
12849 : 296116 : gfc_ss *lss;
12850 : 296116 : gfc_ss *lss_section;
12851 : 296116 : gfc_ss *rss;
12852 : 296116 : gfc_loopinfo loop;
12853 : 296116 : tree tmp;
12854 : 296116 : stmtblock_t block;
12855 : 296116 : stmtblock_t body;
12856 : 296116 : bool final_expr;
12857 : 296116 : bool l_is_temp;
12858 : 296116 : bool scalar_to_array;
12859 : 296116 : tree string_length;
12860 : 296116 : int n;
12861 : 296116 : bool maybe_workshare = false, lhs_refs_comp = false, rhs_refs_comp = false;
12862 : 296116 : symbol_attribute lhs_caf_attr, rhs_caf_attr, lhs_attr;
12863 : 296116 : bool is_poly_assign;
12864 : 296116 : bool realloc_flag;
12865 : 296116 : bool assoc_assign = false;
12866 : :
12867 : : /* Assignment of the form lhs = rhs. */
12868 : 296116 : gfc_start_block (&block);
12869 : :
12870 : 296116 : gfc_init_se (&lse, NULL);
12871 : 296116 : gfc_init_se (&rse, NULL);
12872 : :
12873 : : /* Walk the lhs. */
12874 : 296116 : lss = gfc_walk_expr (expr1);
12875 : 296116 : if (gfc_is_reallocatable_lhs (expr1))
12876 : : {
12877 : 10420 : lss->no_bounds_check = 1;
12878 : 10420 : if (!(expr2->expr_type == EXPR_FUNCTION
12879 : 2587 : && expr2->value.function.isym != NULL
12880 : 2300 : && !(expr2->value.function.isym->elemental
12881 : 2015 : || expr2->value.function.isym->conversion)))
12882 : 8405 : lss->is_alloc_lhs = 1;
12883 : : }
12884 : : else
12885 : 285696 : lss->no_bounds_check = expr1->no_bounds_check;
12886 : :
12887 : 296116 : rss = NULL;
12888 : :
12889 : 296116 : if (expr2->expr_type != EXPR_VARIABLE
12890 : 296116 : && expr2->expr_type != EXPR_CONSTANT
12891 : 296116 : && (expr2->ts.type == BT_CLASS || gfc_may_be_finalized (expr2->ts)))
12892 : : {
12893 : 719 : expr2->must_finalize = 1;
12894 : : /* F2023 7.5.6.3: If an executable construct references a nonpointer
12895 : : function, the result is finalized after execution of the innermost
12896 : : executable construct containing the reference. */
12897 : 719 : if (expr2->expr_type == EXPR_FUNCTION
12898 : 719 : && (gfc_expr_attr (expr2).pointer
12899 : 291 : || (expr2->ts.type == BT_CLASS && CLASS_DATA (expr2)->attr.class_pointer)))
12900 : 145 : expr2->must_finalize = 0;
12901 : : /* F2008 4.5.6.3 para 5: If an executable construct references a
12902 : : structure constructor or array constructor, the entity created by
12903 : : the constructor is finalized after execution of the innermost
12904 : : executable construct containing the reference.
12905 : : These finalizations were later deleted by the Combined Techical
12906 : : Corrigenda 1 TO 4 for fortran 2008 (f08/0011). */
12907 : 574 : else if (gfc_notification_std (GFC_STD_F2018_DEL)
12908 : 574 : && (expr2->expr_type == EXPR_STRUCTURE
12909 : 531 : || expr2->expr_type == EXPR_ARRAY))
12910 : 251 : expr2->must_finalize = 0;
12911 : : }
12912 : :
12913 : :
12914 : : /* Checking whether a class assignment is desired is quite complicated and
12915 : : needed at two locations, so do it once only before the information is
12916 : : needed. */
12917 : 296116 : lhs_attr = gfc_expr_attr (expr1);
12918 : :
12919 : 296116 : is_poly_assign
12920 : 296116 : = (use_vptr_copy
12921 : 280585 : || ((lhs_attr.pointer || lhs_attr.allocatable) && !lhs_attr.dimension))
12922 : 20948 : && (expr1->ts.type == BT_CLASS || gfc_is_class_array_ref (expr1, NULL)
12923 : 19006 : || gfc_is_class_scalar_expr (expr1)
12924 : 17710 : || gfc_is_class_array_ref (expr2, NULL)
12925 : 17710 : || gfc_is_class_scalar_expr (expr2))
12926 : 299372 : && lhs_attr.flavor != FL_PROCEDURE;
12927 : :
12928 : 296116 : assoc_assign = is_assoc_assign (expr1, expr2);
12929 : :
12930 : 592232 : realloc_flag = flag_realloc_lhs
12931 : 290360 : && gfc_is_reallocatable_lhs (expr1)
12932 : 7243 : && expr2->rank
12933 : 301943 : && !is_runtime_conformable (expr1, expr2);
12934 : :
12935 : : /* Only analyze the expressions for coarray properties, when in coarray-lib
12936 : : mode. Avoid false-positive uninitialized diagnostics with initializing
12937 : : the codimension flag unconditionally. */
12938 : 296116 : lhs_caf_attr.codimension = false;
12939 : 296116 : rhs_caf_attr.codimension = false;
12940 : 296116 : if (flag_coarray == GFC_FCOARRAY_LIB)
12941 : : {
12942 : 4512 : lhs_caf_attr = gfc_caf_attr (expr1, false, &lhs_refs_comp);
12943 : 4512 : rhs_caf_attr = gfc_caf_attr (expr2, false, &rhs_refs_comp);
12944 : : }
12945 : :
12946 : 296116 : if (lss != gfc_ss_terminator)
12947 : : {
12948 : : /* The assignment needs scalarization. */
12949 : : lss_section = lss;
12950 : :
12951 : : /* Find a non-scalar SS from the lhs. */
12952 : : while (lss_section != gfc_ss_terminator
12953 : 37680 : && lss_section->info->type != GFC_SS_SECTION)
12954 : 0 : lss_section = lss_section->next;
12955 : :
12956 : 37680 : gcc_assert (lss_section != gfc_ss_terminator);
12957 : :
12958 : : /* Initialize the scalarizer. */
12959 : 37680 : gfc_init_loopinfo (&loop);
12960 : :
12961 : : /* Walk the rhs. */
12962 : 37680 : rss = gfc_walk_expr (expr2);
12963 : 37680 : if (rss == gfc_ss_terminator)
12964 : : /* The rhs is scalar. Add a ss for the expression. */
12965 : 14079 : rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
12966 : : /* When doing a class assign, then the handle to the rhs needs to be a
12967 : : pointer to allow for polymorphism. */
12968 : 37680 : if (is_poly_assign && expr2->rank == 0 && !UNLIMITED_POLY (expr2))
12969 : 484 : rss->info->type = GFC_SS_REFERENCE;
12970 : :
12971 : 37680 : rss->no_bounds_check = expr2->no_bounds_check;
12972 : : /* Associate the SS with the loop. */
12973 : 37680 : gfc_add_ss_to_loop (&loop, lss);
12974 : 37680 : gfc_add_ss_to_loop (&loop, rss);
12975 : :
12976 : : /* Calculate the bounds of the scalarization. */
12977 : 37680 : gfc_conv_ss_startstride (&loop);
12978 : : /* Enable loop reversal. */
12979 : 640560 : for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
12980 : 565200 : loop.reverse[n] = GFC_ENABLE_REVERSE;
12981 : : /* Resolve any data dependencies in the statement. */
12982 : 37680 : if (may_alias)
12983 : 35500 : gfc_conv_resolve_dependencies (&loop, lss, rss);
12984 : : /* Setup the scalarizing loops. */
12985 : 37680 : gfc_conv_loop_setup (&loop, &expr2->where);
12986 : :
12987 : : /* Setup the gfc_se structures. */
12988 : 37680 : gfc_copy_loopinfo_to_se (&lse, &loop);
12989 : 37680 : gfc_copy_loopinfo_to_se (&rse, &loop);
12990 : :
12991 : 37680 : rse.ss = rss;
12992 : 37680 : gfc_mark_ss_chain_used (rss, 1);
12993 : 37680 : if (loop.temp_ss == NULL)
12994 : : {
12995 : 36690 : lse.ss = lss;
12996 : 36690 : gfc_mark_ss_chain_used (lss, 1);
12997 : : }
12998 : : else
12999 : : {
13000 : 990 : lse.ss = loop.temp_ss;
13001 : 990 : gfc_mark_ss_chain_used (lss, 3);
13002 : 990 : gfc_mark_ss_chain_used (loop.temp_ss, 3);
13003 : : }
13004 : :
13005 : : /* Allow the scalarizer to workshare array assignments. */
13006 : 37680 : if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_BODY))
13007 : : == OMPWS_WORKSHARE_FLAG
13008 : 85 : && loop.temp_ss == NULL)
13009 : : {
13010 : 73 : maybe_workshare = true;
13011 : 73 : ompws_flags |= OMPWS_SCALARIZER_WS | OMPWS_SCALARIZER_BODY;
13012 : : }
13013 : :
13014 : : /* Start the scalarized loop body. */
13015 : 37680 : gfc_start_scalarized_body (&loop, &body);
13016 : : }
13017 : : else
13018 : 258436 : gfc_init_block (&body);
13019 : :
13020 : 296116 : l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
13021 : :
13022 : : /* Translate the expression. */
13023 : 592232 : rse.want_coarray = flag_coarray == GFC_FCOARRAY_LIB
13024 : 296116 : && (init_flag || assoc_assign) && lhs_caf_attr.codimension;
13025 : 296116 : rse.want_pointer = rse.want_coarray && !init_flag && !lhs_caf_attr.dimension;
13026 : 296116 : gfc_conv_expr (&rse, expr2);
13027 : :
13028 : : /* Deal with the case of a scalar class function assigned to a derived type.
13029 : : */
13030 : 296116 : if (gfc_is_alloc_class_scalar_function (expr2)
13031 : 296116 : && expr1->ts.type == BT_DERIVED)
13032 : : {
13033 : 60 : rse.expr = gfc_class_data_get (rse.expr);
13034 : 60 : rse.expr = build_fold_indirect_ref_loc (input_location, rse.expr);
13035 : : }
13036 : :
13037 : : /* Stabilize a string length for temporaries. */
13038 : 296116 : if (expr2->ts.type == BT_CHARACTER && !expr1->ts.deferred
13039 : 22952 : && !(VAR_P (rse.string_length)
13040 : : || TREE_CODE (rse.string_length) == PARM_DECL
13041 : : || INDIRECT_REF_P (rse.string_length)))
13042 : 22183 : string_length = gfc_evaluate_now (rse.string_length, &rse.pre);
13043 : 273933 : else if (expr2->ts.type == BT_CHARACTER)
13044 : : {
13045 : 3940 : if (expr1->ts.deferred
13046 : 6126 : && gfc_expr_attr (expr1).allocatable
13047 : 6240 : && gfc_check_dependency (expr1, expr2, true))
13048 : 114 : rse.string_length =
13049 : 114 : gfc_evaluate_now_function_scope (rse.string_length, &rse.pre);
13050 : 3940 : string_length = rse.string_length;
13051 : : }
13052 : : else
13053 : : string_length = NULL_TREE;
13054 : :
13055 : 296116 : if (l_is_temp)
13056 : : {
13057 : 990 : gfc_conv_tmp_array_ref (&lse);
13058 : 990 : if (expr2->ts.type == BT_CHARACTER)
13059 : 123 : lse.string_length = string_length;
13060 : : }
13061 : : else
13062 : : {
13063 : 295126 : gfc_conv_expr (&lse, expr1);
13064 : : /* For some expression (e.g. complex numbers) fold_convert uses a
13065 : : SAVE_EXPR, which is hazardous on the lhs, because the value is
13066 : : not updated when assigned to. */
13067 : 295126 : if (TREE_CODE (lse.expr) == SAVE_EXPR)
13068 : 5 : lse.expr = TREE_OPERAND (lse.expr, 0);
13069 : :
13070 : 6138 : if (gfc_option.rtcheck & GFC_RTCHECK_MEM && !init_flag
13071 : 301264 : && gfc_expr_attr (expr1).allocatable && expr1->rank && !expr2->rank)
13072 : : {
13073 : 36 : tree cond;
13074 : 36 : const char* msg;
13075 : :
13076 : 36 : tmp = INDIRECT_REF_P (lse.expr)
13077 : 36 : ? gfc_build_addr_expr (NULL_TREE, lse.expr) : lse.expr;
13078 : 36 : STRIP_NOPS (tmp);
13079 : :
13080 : : /* We should only get array references here. */
13081 : 36 : gcc_assert (TREE_CODE (tmp) == POINTER_PLUS_EXPR
13082 : : || TREE_CODE (tmp) == ARRAY_REF);
13083 : :
13084 : : /* 'tmp' is either the pointer to the array(POINTER_PLUS_EXPR)
13085 : : or the array itself(ARRAY_REF). */
13086 : 36 : tmp = TREE_OPERAND (tmp, 0);
13087 : :
13088 : : /* Provide the address of the array. */
13089 : 36 : if (TREE_CODE (lse.expr) == ARRAY_REF)
13090 : 18 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
13091 : :
13092 : 36 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
13093 : 36 : tmp, build_int_cst (TREE_TYPE (tmp), 0));
13094 : 36 : msg = _("Assignment of scalar to unallocated array");
13095 : 36 : gfc_trans_runtime_check (true, false, cond, &loop.pre,
13096 : : &expr1->where, msg);
13097 : : }
13098 : :
13099 : : /* Deallocate the lhs parameterized components if required. */
13100 : 295126 : if (dealloc && expr2->expr_type == EXPR_FUNCTION
13101 : 35011 : && !expr1->symtree->n.sym->attr.associate_var)
13102 : : {
13103 : 34922 : if (expr1->ts.type == BT_DERIVED
13104 : 1949 : && expr1->ts.u.derived
13105 : 1949 : && expr1->ts.u.derived->attr.pdt_type)
13106 : : {
13107 : 12 : tmp = gfc_deallocate_pdt_comp (expr1->ts.u.derived, lse.expr,
13108 : : expr1->rank);
13109 : 12 : gfc_add_expr_to_block (&lse.pre, tmp);
13110 : : }
13111 : 34910 : else if (expr1->ts.type == BT_CLASS
13112 : 349 : && CLASS_DATA (expr1)->ts.u.derived
13113 : 349 : && CLASS_DATA (expr1)->ts.u.derived->attr.pdt_type)
13114 : : {
13115 : 0 : tmp = gfc_class_data_get (lse.expr);
13116 : 0 : tmp = gfc_deallocate_pdt_comp (CLASS_DATA (expr1)->ts.u.derived,
13117 : : tmp, expr1->rank);
13118 : 0 : gfc_add_expr_to_block (&lse.pre, tmp);
13119 : : }
13120 : : }
13121 : : }
13122 : :
13123 : : /* Assignments of scalar derived types with allocatable components
13124 : : to arrays must be done with a deep copy and the rhs temporary
13125 : : must have its components deallocated afterwards. */
13126 : 592232 : scalar_to_array = (expr2->ts.type == BT_DERIVED
13127 : 17536 : && expr2->ts.u.derived->attr.alloc_comp
13128 : 5684 : && !gfc_expr_is_variable (expr2)
13129 : 299269 : && expr1->rank && !expr2->rank);
13130 : 592232 : scalar_to_array |= (expr1->ts.type == BT_DERIVED
13131 : 17821 : && expr1->rank
13132 : 3364 : && expr1->ts.u.derived->attr.alloc_comp
13133 : 297275 : && gfc_is_alloc_class_scalar_function (expr2));
13134 : 296116 : if (scalar_to_array && dealloc)
13135 : : {
13136 : 52 : tmp = gfc_deallocate_alloc_comp_no_caf (expr2->ts.u.derived, rse.expr, 0);
13137 : 52 : gfc_prepend_expr_to_block (&loop.post, tmp);
13138 : : }
13139 : :
13140 : : /* When assigning a character function result to a deferred-length variable,
13141 : : the function call must happen before the (re)allocation of the lhs -
13142 : : otherwise the character length of the result is not known.
13143 : : NOTE 1: This relies on having the exact dependence of the length type
13144 : : parameter available to the caller; gfortran saves it in the .mod files.
13145 : : NOTE 2: Vector array references generate an index temporary that must
13146 : : not go outside the loop. Otherwise, variables should not generate
13147 : : a pre block.
13148 : : NOTE 3: The concatenation operation generates a temporary pointer,
13149 : : whose allocation must go to the innermost loop.
13150 : : NOTE 4: Elemental functions may generate a temporary, too. */
13151 : 296116 : if (flag_realloc_lhs
13152 : 290360 : && expr2->ts.type == BT_CHARACTER && expr1->ts.deferred
13153 : 2671 : && !(lss != gfc_ss_terminator
13154 : 794 : && rss != gfc_ss_terminator
13155 : 794 : && ((expr2->expr_type == EXPR_VARIABLE && expr2->rank)
13156 : 619 : || (expr2->expr_type == EXPR_FUNCTION
13157 : 142 : && expr2->value.function.esym != NULL
13158 : 26 : && expr2->value.function.esym->attr.elemental)
13159 : 606 : || (expr2->expr_type == EXPR_FUNCTION
13160 : 129 : && expr2->value.function.isym != NULL
13161 : 116 : && expr2->value.function.isym->elemental)
13162 : 568 : || (expr2->expr_type == EXPR_OP
13163 : 31 : && expr2->value.op.op == INTRINSIC_CONCAT))))
13164 : 2420 : gfc_add_block_to_block (&block, &rse.pre);
13165 : :
13166 : : /* Nullify the allocatable components corresponding to those of the lhs
13167 : : derived type, so that the finalization of the function result does not
13168 : : affect the lhs of the assignment. Prepend is used to ensure that the
13169 : : nullification occurs before the call to the finalizer. In the case of
13170 : : a scalar to array assignment, this is done in gfc_trans_scalar_assign
13171 : : as part of the deep copy. */
13172 : 295447 : if (!scalar_to_array && expr1->ts.type == BT_DERIVED
13173 : 313268 : && (gfc_is_class_array_function (expr2)
13174 : 17128 : || gfc_is_alloc_class_scalar_function (expr2)))
13175 : : {
13176 : 78 : tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, rse.expr, 0);
13177 : 78 : gfc_prepend_expr_to_block (&rse.post, tmp);
13178 : 78 : if (lss != gfc_ss_terminator && rss == gfc_ss_terminator)
13179 : 0 : gfc_add_block_to_block (&loop.post, &rse.post);
13180 : : }
13181 : :
13182 : 296116 : tmp = NULL_TREE;
13183 : :
13184 : 296116 : if (is_poly_assign)
13185 : : {
13186 : 3189 : tmp = trans_class_assignment (&body, expr1, expr2, &lse, &rse,
13187 : 3189 : use_vptr_copy || (lhs_attr.allocatable
13188 : 272 : && !lhs_attr.dimension),
13189 : 2987 : !realloc_flag && flag_realloc_lhs
13190 : 3713 : && !lhs_attr.pointer);
13191 : 3189 : if (expr2->expr_type == EXPR_FUNCTION
13192 : 230 : && expr2->ts.type == BT_DERIVED
13193 : 30 : && expr2->ts.u.derived->attr.alloc_comp)
13194 : : {
13195 : 18 : tree tmp2 = gfc_deallocate_alloc_comp (expr2->ts.u.derived,
13196 : : rse.expr, expr2->rank);
13197 : 18 : if (lss == gfc_ss_terminator)
13198 : 18 : gfc_add_expr_to_block (&rse.post, tmp2);
13199 : : else
13200 : 0 : gfc_add_expr_to_block (&loop.post, tmp2);
13201 : : }
13202 : :
13203 : 3189 : expr1->must_finalize = 0;
13204 : : }
13205 : 292927 : else if (!is_poly_assign && expr2->must_finalize
13206 : 348 : && expr1->ts.type == BT_CLASS
13207 : 126 : && expr2->ts.type == BT_CLASS)
13208 : : {
13209 : : /* This case comes about when the scalarizer provides array element
13210 : : references. Use the vptr copy function, since this does a deep
13211 : : copy of allocatable components, without which the finalizer call
13212 : : will deallocate the components. */
13213 : 120 : tmp = gfc_get_vptr_from_expr (rse.expr);
13214 : 120 : if (tmp != NULL_TREE)
13215 : : {
13216 : 120 : tree fcn = gfc_vptr_copy_get (tmp);
13217 : 120 : if (POINTER_TYPE_P (TREE_TYPE (fcn)))
13218 : 120 : fcn = build_fold_indirect_ref_loc (input_location, fcn);
13219 : 120 : tmp = build_call_expr_loc (input_location,
13220 : : fcn, 2,
13221 : : gfc_build_addr_expr (NULL, rse.expr),
13222 : : gfc_build_addr_expr (NULL, lse.expr));
13223 : : }
13224 : : }
13225 : :
13226 : : /* Comply with F2018 (7.5.6.3). Make sure that any finalization code is added
13227 : : after evaluation of the rhs and before reallocation. */
13228 : 296116 : final_expr = gfc_assignment_finalizer_call (&lse, expr1, init_flag);
13229 : 296116 : if (final_expr && !(expr2->expr_type == EXPR_VARIABLE
13230 : 169 : && expr2->symtree->n.sym->attr.artificial))
13231 : : {
13232 : 551 : if (lss == gfc_ss_terminator)
13233 : : {
13234 : 164 : gfc_add_block_to_block (&block, &rse.pre);
13235 : 164 : gfc_add_block_to_block (&block, &lse.finalblock);
13236 : : }
13237 : : else
13238 : : {
13239 : 387 : gfc_add_block_to_block (&body, &rse.pre);
13240 : 387 : gfc_add_block_to_block (&loop.code[expr1->rank - 1],
13241 : : &lse.finalblock);
13242 : : }
13243 : : }
13244 : : else
13245 : 295565 : gfc_add_block_to_block (&body, &rse.pre);
13246 : :
13247 : 296116 : if (flag_coarray != GFC_FCOARRAY_NONE && expr1->ts.type == BT_CHARACTER
13248 : 2001 : && assoc_assign)
13249 : 0 : tmp = gfc_trans_pointer_assignment (expr1, expr2);
13250 : :
13251 : : /* If nothing else works, do it the old fashioned way! */
13252 : 296116 : if (tmp == NULL_TREE)
13253 : 292807 : tmp
13254 : 292807 : = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
13255 : 557222 : gfc_expr_is_variable (expr2) || scalar_to_array
13256 : 556637 : || expr2->expr_type == EXPR_ARRAY,
13257 : 292807 : !(l_is_temp || init_flag) && dealloc,
13258 : 292807 : expr1->symtree->n.sym->attr.codimension,
13259 : : assoc_assign);
13260 : :
13261 : : /* Add the lse pre block to the body */
13262 : 296116 : gfc_add_block_to_block (&body, &lse.pre);
13263 : 296116 : gfc_add_expr_to_block (&body, tmp);
13264 : :
13265 : : /* Add the post blocks to the body. Scalar finalization must appear before
13266 : : the post block in case any dellocations are done. */
13267 : 296116 : if (rse.finalblock.head
13268 : 296116 : && (!l_is_temp || (expr2->expr_type == EXPR_FUNCTION
13269 : 14 : && gfc_expr_attr (expr2).elemental)))
13270 : : {
13271 : 135 : gfc_add_block_to_block (&body, &rse.finalblock);
13272 : 135 : gfc_add_block_to_block (&body, &rse.post);
13273 : : }
13274 : : else
13275 : 295981 : gfc_add_block_to_block (&body, &rse.post);
13276 : :
13277 : 296116 : gfc_add_block_to_block (&body, &lse.post);
13278 : :
13279 : 296116 : if (lss == gfc_ss_terminator)
13280 : : {
13281 : : /* F2003: Add the code for reallocation on assignment. */
13282 : 255778 : if (flag_realloc_lhs && is_scalar_reallocatable_lhs (expr1)
13283 : 261819 : && !is_poly_assign)
13284 : 3365 : alloc_scalar_allocatable_for_assignment (&block, string_length,
13285 : : expr1, expr2);
13286 : :
13287 : : /* Use the scalar assignment as is. */
13288 : 258436 : gfc_add_block_to_block (&block, &body);
13289 : : }
13290 : : else
13291 : : {
13292 : 37680 : gcc_assert (lse.ss == gfc_ss_terminator
13293 : : && rse.ss == gfc_ss_terminator);
13294 : :
13295 : 37680 : if (l_is_temp)
13296 : : {
13297 : 990 : gfc_trans_scalarized_loop_boundary (&loop, &body);
13298 : :
13299 : : /* We need to copy the temporary to the actual lhs. */
13300 : 990 : gfc_init_se (&lse, NULL);
13301 : 990 : gfc_init_se (&rse, NULL);
13302 : 990 : gfc_copy_loopinfo_to_se (&lse, &loop);
13303 : 990 : gfc_copy_loopinfo_to_se (&rse, &loop);
13304 : :
13305 : 990 : rse.ss = loop.temp_ss;
13306 : 990 : lse.ss = lss;
13307 : :
13308 : 990 : gfc_conv_tmp_array_ref (&rse);
13309 : 990 : gfc_conv_expr (&lse, expr1);
13310 : :
13311 : 990 : gcc_assert (lse.ss == gfc_ss_terminator
13312 : : && rse.ss == gfc_ss_terminator);
13313 : :
13314 : 990 : if (expr2->ts.type == BT_CHARACTER)
13315 : 123 : rse.string_length = string_length;
13316 : :
13317 : 990 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
13318 : : false, dealloc);
13319 : 990 : gfc_add_expr_to_block (&body, tmp);
13320 : : }
13321 : :
13322 : : /* F2003: Allocate or reallocate lhs of allocatable array. */
13323 : 37680 : if (realloc_flag)
13324 : : {
13325 : 5630 : realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
13326 : 5630 : ompws_flags &= ~OMPWS_SCALARIZER_WS;
13327 : 5630 : tmp = gfc_alloc_allocatable_for_assignment (&loop, expr1, expr2);
13328 : 5630 : if (tmp != NULL_TREE)
13329 : 5630 : gfc_add_expr_to_block (&loop.code[expr1->rank - 1], tmp);
13330 : : }
13331 : :
13332 : 37680 : if (maybe_workshare)
13333 : 73 : ompws_flags &= ~OMPWS_SCALARIZER_BODY;
13334 : :
13335 : : /* Generate the copying loops. */
13336 : 37680 : gfc_trans_scalarizing_loops (&loop, &body);
13337 : :
13338 : : /* Wrap the whole thing up. */
13339 : 37680 : gfc_add_block_to_block (&block, &loop.pre);
13340 : 37680 : gfc_add_block_to_block (&block, &loop.post);
13341 : :
13342 : 37680 : gfc_cleanup_loop (&loop);
13343 : : }
13344 : :
13345 : 296116 : return gfc_finish_block (&block);
13346 : : }
13347 : :
13348 : :
13349 : : /* Check whether EXPR is a copyable array. */
13350 : :
13351 : : static bool
13352 : 936897 : copyable_array_p (gfc_expr * expr)
13353 : : {
13354 : 936897 : if (expr->expr_type != EXPR_VARIABLE)
13355 : : return false;
13356 : :
13357 : : /* First check it's an array. */
13358 : 914248 : if (expr->rank < 1 || !expr->ref || expr->ref->next)
13359 : : return false;
13360 : :
13361 : 136996 : if (!gfc_full_array_ref_p (expr->ref, NULL))
13362 : : return false;
13363 : :
13364 : : /* Next check that it's of a simple enough type. */
13365 : 109741 : switch (expr->ts.type)
13366 : : {
13367 : : case BT_INTEGER:
13368 : : case BT_REAL:
13369 : : case BT_COMPLEX:
13370 : : case BT_LOGICAL:
13371 : : return true;
13372 : :
13373 : : case BT_CHARACTER:
13374 : : return false;
13375 : :
13376 : 5854 : case_bt_struct:
13377 : 5854 : return !expr->ts.u.derived->attr.alloc_comp;
13378 : :
13379 : : default:
13380 : : break;
13381 : : }
13382 : :
13383 : : return false;
13384 : : }
13385 : :
13386 : : /* Translate an assignment. */
13387 : :
13388 : : tree
13389 : 313336 : gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
13390 : : bool dealloc, bool use_vptr_copy, bool may_alias)
13391 : : {
13392 : 313336 : tree tmp;
13393 : :
13394 : : /* Special case a single function returning an array. */
13395 : 313336 : if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
13396 : : {
13397 : 14577 : tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
13398 : 14577 : if (tmp)
13399 : : return tmp;
13400 : : }
13401 : :
13402 : : /* Special case assigning an array to zero. */
13403 : 306256 : if (copyable_array_p (expr1)
13404 : 306256 : && is_zero_initializer_p (expr2))
13405 : : {
13406 : 3924 : tmp = gfc_trans_zero_assign (expr1);
13407 : 3924 : if (tmp)
13408 : : return tmp;
13409 : : }
13410 : :
13411 : : /* Special case copying one array to another. */
13412 : 302610 : if (copyable_array_p (expr1)
13413 : 26859 : && copyable_array_p (expr2)
13414 : 2599 : && gfc_compare_types (&expr1->ts, &expr2->ts)
13415 : 305209 : && !gfc_check_dependency (expr1, expr2, 0))
13416 : : {
13417 : 2503 : tmp = gfc_trans_array_copy (expr1, expr2);
13418 : 2503 : if (tmp)
13419 : : return tmp;
13420 : : }
13421 : :
13422 : : /* Special case initializing an array from a constant array constructor. */
13423 : 301172 : if (copyable_array_p (expr1)
13424 : 25421 : && expr2->expr_type == EXPR_ARRAY
13425 : 308638 : && gfc_compare_types (&expr1->ts, &expr2->ts))
13426 : : {
13427 : 7466 : tmp = gfc_trans_array_constructor_copy (expr1, expr2);
13428 : 7466 : if (tmp)
13429 : : return tmp;
13430 : : }
13431 : :
13432 : 296116 : if (UNLIMITED_POLY (expr1) && expr1->rank)
13433 : 296116 : use_vptr_copy = true;
13434 : :
13435 : : /* Fallback to the scalarizer to generate explicit loops. */
13436 : 296116 : return gfc_trans_assignment_1 (expr1, expr2, init_flag, dealloc,
13437 : 296116 : use_vptr_copy, may_alias);
13438 : : }
13439 : :
13440 : : tree
13441 : 11894 : gfc_trans_init_assign (gfc_code * code)
13442 : : {
13443 : 11894 : return gfc_trans_assignment (code->expr1, code->expr2, true, false, true);
13444 : : }
13445 : :
13446 : : tree
13447 : 293501 : gfc_trans_assign (gfc_code * code)
13448 : : {
13449 : 293501 : return gfc_trans_assignment (code->expr1, code->expr2, false, true);
13450 : : }
13451 : :
13452 : : /* Generate a simple loop for internal use of the form
13453 : : for (var = begin; var <cond> end; var += step)
13454 : : body; */
13455 : : void
13456 : 12144 : gfc_simple_for_loop (stmtblock_t *block, tree var, tree begin, tree end,
13457 : : enum tree_code cond, tree step, tree body)
13458 : : {
13459 : 12144 : tree tmp;
13460 : :
13461 : : /* var = begin. */
13462 : 12144 : gfc_add_modify (block, var, begin);
13463 : :
13464 : : /* Loop: for (var = begin; var <cond> end; var += step). */
13465 : 12144 : tree label_loop = gfc_build_label_decl (NULL_TREE);
13466 : 12144 : tree label_cond = gfc_build_label_decl (NULL_TREE);
13467 : 12144 : TREE_USED (label_loop) = 1;
13468 : 12144 : TREE_USED (label_cond) = 1;
13469 : :
13470 : 12144 : gfc_add_expr_to_block (block, build1_v (GOTO_EXPR, label_cond));
13471 : 12144 : gfc_add_expr_to_block (block, build1_v (LABEL_EXPR, label_loop));
13472 : :
13473 : : /* Loop body. */
13474 : 12144 : gfc_add_expr_to_block (block, body);
13475 : :
13476 : : /* End of loop body. */
13477 : 12144 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (var), var, step);
13478 : 12144 : gfc_add_modify (block, var, tmp);
13479 : 12144 : gfc_add_expr_to_block (block, build1_v (LABEL_EXPR, label_cond));
13480 : 12144 : tmp = fold_build2_loc (input_location, cond, boolean_type_node, var, end);
13481 : 12144 : tmp = build3_v (COND_EXPR, tmp, build1_v (GOTO_EXPR, label_loop),
13482 : : build_empty_stmt (input_location));
13483 : 12144 : gfc_add_expr_to_block (block, tmp);
13484 : 12144 : }
|