]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/fortran/trans-expr.c
030edc1e5ceb58eb5b13e9331aeca69a31318cb8
[thirdparty/gcc.git] / gcc / fortran / trans-expr.c
1 /* Expression translation
2 Copyright (C) 2002-2020 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.c-- generate GENERIC trees for gfc_expr. */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "options.h"
28 #include "tree.h"
29 #include "gfortran.h"
30 #include "trans.h"
31 #include "stringpool.h"
32 #include "diagnostic-core.h" /* For fatal_error. */
33 #include "fold-const.h"
34 #include "langhooks.h"
35 #include "arith.h"
36 #include "constructor.h"
37 #include "trans-const.h"
38 #include "trans-types.h"
39 #include "trans-array.h"
40 /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
41 #include "trans-stmt.h"
42 #include "dependency.h"
43 #include "gimplify.h"
44
45 /* Convert a scalar to an array descriptor. To be used for assumed-rank
46 arrays. */
47
48 static tree
49 get_scalar_to_descriptor_type (tree scalar, symbol_attribute attr)
50 {
51 enum gfc_array_kind akind;
52
53 if (attr.pointer)
54 akind = GFC_ARRAY_POINTER_CONT;
55 else if (attr.allocatable)
56 akind = GFC_ARRAY_ALLOCATABLE;
57 else
58 akind = GFC_ARRAY_ASSUMED_SHAPE_CONT;
59
60 if (POINTER_TYPE_P (TREE_TYPE (scalar)))
61 scalar = TREE_TYPE (scalar);
62 return gfc_get_array_type_bounds (TREE_TYPE (scalar), 0, 0, NULL, NULL, 1,
63 akind, !(attr.pointer || attr.target));
64 }
65
66 tree
67 gfc_conv_scalar_to_descriptor (gfc_se *se, tree scalar, symbol_attribute attr)
68 {
69 tree desc, type, etype;
70
71 type = get_scalar_to_descriptor_type (scalar, attr);
72 etype = TREE_TYPE (scalar);
73 desc = gfc_create_var (type, "desc");
74 DECL_ARTIFICIAL (desc) = 1;
75
76 if (CONSTANT_CLASS_P (scalar))
77 {
78 tree tmp;
79 tmp = gfc_create_var (TREE_TYPE (scalar), "scalar");
80 gfc_add_modify (&se->pre, tmp, scalar);
81 scalar = tmp;
82 }
83 if (!POINTER_TYPE_P (TREE_TYPE (scalar)))
84 scalar = gfc_build_addr_expr (NULL_TREE, scalar);
85 else if (TREE_TYPE (etype) && TREE_CODE (TREE_TYPE (etype)) == ARRAY_TYPE)
86 etype = TREE_TYPE (etype);
87 gfc_add_modify (&se->pre, gfc_conv_descriptor_dtype (desc),
88 gfc_get_dtype_rank_type (0, etype));
89 gfc_conv_descriptor_data_set (&se->pre, desc, scalar);
90
91 /* Copy pointer address back - but only if it could have changed and
92 if the actual argument is a pointer and not, e.g., NULL(). */
93 if ((attr.pointer || attr.allocatable) && attr.intent != INTENT_IN)
94 gfc_add_modify (&se->post, scalar,
95 fold_convert (TREE_TYPE (scalar),
96 gfc_conv_descriptor_data_get (desc)));
97 return desc;
98 }
99
100
101 /* Get the coarray token from the ultimate array or component ref.
102 Returns a NULL_TREE, when the ref object is not allocatable or pointer. */
103
104 tree
105 gfc_get_ultimate_alloc_ptr_comps_caf_token (gfc_se *outerse, gfc_expr *expr)
106 {
107 gfc_symbol *sym = expr->symtree->n.sym;
108 bool is_coarray = sym->attr.codimension;
109 gfc_expr *caf_expr = gfc_copy_expr (expr);
110 gfc_ref *ref = caf_expr->ref, *last_caf_ref = NULL;
111
112 while (ref)
113 {
114 if (ref->type == REF_COMPONENT
115 && (ref->u.c.component->attr.allocatable
116 || ref->u.c.component->attr.pointer)
117 && (is_coarray || ref->u.c.component->attr.codimension))
118 last_caf_ref = ref;
119 ref = ref->next;
120 }
121
122 if (last_caf_ref == NULL)
123 return NULL_TREE;
124
125 tree comp = last_caf_ref->u.c.component->caf_token, caf;
126 gfc_se se;
127 bool comp_ref = !last_caf_ref->u.c.component->attr.dimension;
128 if (comp == NULL_TREE && comp_ref)
129 return NULL_TREE;
130 gfc_init_se (&se, outerse);
131 gfc_free_ref_list (last_caf_ref->next);
132 last_caf_ref->next = NULL;
133 caf_expr->rank = comp_ref ? 0 : last_caf_ref->u.c.component->as->rank;
134 se.want_pointer = comp_ref;
135 gfc_conv_expr (&se, caf_expr);
136 gfc_add_block_to_block (&outerse->pre, &se.pre);
137
138 if (TREE_CODE (se.expr) == COMPONENT_REF && comp_ref)
139 se.expr = TREE_OPERAND (se.expr, 0);
140 gfc_free_expr (caf_expr);
141
142 if (comp_ref)
143 caf = fold_build3_loc (input_location, COMPONENT_REF,
144 TREE_TYPE (comp), se.expr, comp, NULL_TREE);
145 else
146 caf = gfc_conv_descriptor_token (se.expr);
147 return gfc_build_addr_expr (NULL_TREE, caf);
148 }
149
150
151 /* This is the seed for an eventual trans-class.c
152
153 The following parameters should not be used directly since they might
154 in future implementations. Use the corresponding APIs. */
155 #define CLASS_DATA_FIELD 0
156 #define CLASS_VPTR_FIELD 1
157 #define CLASS_LEN_FIELD 2
158 #define VTABLE_HASH_FIELD 0
159 #define VTABLE_SIZE_FIELD 1
160 #define VTABLE_EXTENDS_FIELD 2
161 #define VTABLE_DEF_INIT_FIELD 3
162 #define VTABLE_COPY_FIELD 4
163 #define VTABLE_FINAL_FIELD 5
164 #define VTABLE_DEALLOCATE_FIELD 6
165
166
167 tree
168 gfc_class_set_static_fields (tree decl, tree vptr, tree data)
169 {
170 tree tmp;
171 tree field;
172 vec<constructor_elt, va_gc> *init = NULL;
173
174 field = TYPE_FIELDS (TREE_TYPE (decl));
175 tmp = gfc_advance_chain (field, CLASS_DATA_FIELD);
176 CONSTRUCTOR_APPEND_ELT (init, tmp, data);
177
178 tmp = gfc_advance_chain (field, CLASS_VPTR_FIELD);
179 CONSTRUCTOR_APPEND_ELT (init, tmp, vptr);
180
181 return build_constructor (TREE_TYPE (decl), init);
182 }
183
184
185 tree
186 gfc_class_data_get (tree decl)
187 {
188 tree data;
189 if (POINTER_TYPE_P (TREE_TYPE (decl)))
190 decl = build_fold_indirect_ref_loc (input_location, decl);
191 data = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
192 CLASS_DATA_FIELD);
193 return fold_build3_loc (input_location, COMPONENT_REF,
194 TREE_TYPE (data), decl, data,
195 NULL_TREE);
196 }
197
198
199 tree
200 gfc_class_vptr_get (tree decl)
201 {
202 tree vptr;
203 /* For class arrays decl may be a temporary descriptor handle, the vptr is
204 then available through the saved descriptor. */
205 if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
206 && GFC_DECL_SAVED_DESCRIPTOR (decl))
207 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
208 if (POINTER_TYPE_P (TREE_TYPE (decl)))
209 decl = build_fold_indirect_ref_loc (input_location, decl);
210 vptr = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
211 CLASS_VPTR_FIELD);
212 return fold_build3_loc (input_location, COMPONENT_REF,
213 TREE_TYPE (vptr), decl, vptr,
214 NULL_TREE);
215 }
216
217
218 tree
219 gfc_class_len_get (tree decl)
220 {
221 tree len;
222 /* For class arrays decl may be a temporary descriptor handle, the len is
223 then available through the saved descriptor. */
224 if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
225 && GFC_DECL_SAVED_DESCRIPTOR (decl))
226 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
227 if (POINTER_TYPE_P (TREE_TYPE (decl)))
228 decl = build_fold_indirect_ref_loc (input_location, decl);
229 len = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
230 CLASS_LEN_FIELD);
231 return fold_build3_loc (input_location, COMPONENT_REF,
232 TREE_TYPE (len), decl, len,
233 NULL_TREE);
234 }
235
236
237 /* Try to get the _len component of a class. When the class is not unlimited
238 poly, i.e. no _len field exists, then return a zero node. */
239
240 tree
241 gfc_class_len_or_zero_get (tree decl)
242 {
243 tree len;
244 /* For class arrays decl may be a temporary descriptor handle, the vptr is
245 then available through the saved descriptor. */
246 if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
247 && GFC_DECL_SAVED_DESCRIPTOR (decl))
248 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
249 if (POINTER_TYPE_P (TREE_TYPE (decl)))
250 decl = build_fold_indirect_ref_loc (input_location, decl);
251 len = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
252 CLASS_LEN_FIELD);
253 return len != NULL_TREE ? fold_build3_loc (input_location, COMPONENT_REF,
254 TREE_TYPE (len), decl, len,
255 NULL_TREE)
256 : build_zero_cst (gfc_charlen_type_node);
257 }
258
259
260 /* Get the specified FIELD from the VPTR. */
261
262 static tree
263 vptr_field_get (tree vptr, int fieldno)
264 {
265 tree field;
266 vptr = build_fold_indirect_ref_loc (input_location, vptr);
267 field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (vptr)),
268 fieldno);
269 field = fold_build3_loc (input_location, COMPONENT_REF,
270 TREE_TYPE (field), vptr, field,
271 NULL_TREE);
272 gcc_assert (field);
273 return field;
274 }
275
276
277 /* Get the field from the class' vptr. */
278
279 static tree
280 class_vtab_field_get (tree decl, int fieldno)
281 {
282 tree vptr;
283 vptr = gfc_class_vptr_get (decl);
284 return vptr_field_get (vptr, fieldno);
285 }
286
287
288 /* Define a macro for creating the class_vtab_* and vptr_* accessors in
289 unison. */
290 #define VTAB_GET_FIELD_GEN(name, field) tree \
291 gfc_class_vtab_## name ##_get (tree cl) \
292 { \
293 return class_vtab_field_get (cl, field); \
294 } \
295 \
296 tree \
297 gfc_vptr_## name ##_get (tree vptr) \
298 { \
299 return vptr_field_get (vptr, field); \
300 }
301
302 VTAB_GET_FIELD_GEN (hash, VTABLE_HASH_FIELD)
303 VTAB_GET_FIELD_GEN (extends, VTABLE_EXTENDS_FIELD)
304 VTAB_GET_FIELD_GEN (def_init, VTABLE_DEF_INIT_FIELD)
305 VTAB_GET_FIELD_GEN (copy, VTABLE_COPY_FIELD)
306 VTAB_GET_FIELD_GEN (final, VTABLE_FINAL_FIELD)
307 VTAB_GET_FIELD_GEN (deallocate, VTABLE_DEALLOCATE_FIELD)
308
309
310 /* The size field is returned as an array index type. Therefore treat
311 it and only it specially. */
312
313 tree
314 gfc_class_vtab_size_get (tree cl)
315 {
316 tree size;
317 size = class_vtab_field_get (cl, VTABLE_SIZE_FIELD);
318 /* Always return size as an array index type. */
319 size = fold_convert (gfc_array_index_type, size);
320 gcc_assert (size);
321 return size;
322 }
323
324 tree
325 gfc_vptr_size_get (tree vptr)
326 {
327 tree size;
328 size = vptr_field_get (vptr, VTABLE_SIZE_FIELD);
329 /* Always return size as an array index type. */
330 size = fold_convert (gfc_array_index_type, size);
331 gcc_assert (size);
332 return size;
333 }
334
335
336 #undef CLASS_DATA_FIELD
337 #undef CLASS_VPTR_FIELD
338 #undef CLASS_LEN_FIELD
339 #undef VTABLE_HASH_FIELD
340 #undef VTABLE_SIZE_FIELD
341 #undef VTABLE_EXTENDS_FIELD
342 #undef VTABLE_DEF_INIT_FIELD
343 #undef VTABLE_COPY_FIELD
344 #undef VTABLE_FINAL_FIELD
345
346
347 /* Search for the last _class ref in the chain of references of this
348 expression and cut the chain there. Albeit this routine is similiar
349 to class.c::gfc_add_component_ref (), is there a significant
350 difference: gfc_add_component_ref () concentrates on an array ref to
351 be the last ref in the chain. This routine is oblivious to the kind
352 of refs following. */
353
354 gfc_expr *
355 gfc_find_and_cut_at_last_class_ref (gfc_expr *e, bool is_mold)
356 {
357 gfc_expr *base_expr;
358 gfc_ref *ref, *class_ref, *tail = NULL, *array_ref;
359
360 /* Find the last class reference. */
361 class_ref = NULL;
362 array_ref = NULL;
363 for (ref = e->ref; ref; ref = ref->next)
364 {
365 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
366 array_ref = ref;
367
368 if (ref->type == REF_COMPONENT
369 && ref->u.c.component->ts.type == BT_CLASS)
370 {
371 /* Component to the right of a part reference with nonzero rank
372 must not have the ALLOCATABLE attribute. If attempts are
373 made to reference such a component reference, an error results
374 followed by an ICE. */
375 if (array_ref && CLASS_DATA (ref->u.c.component)->attr.allocatable)
376 return NULL;
377 class_ref = ref;
378 }
379
380 if (ref->next == NULL)
381 break;
382 }
383
384 /* Remove and store all subsequent references after the
385 CLASS reference. */
386 if (class_ref)
387 {
388 tail = class_ref->next;
389 class_ref->next = NULL;
390 }
391 else if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
392 {
393 tail = e->ref;
394 e->ref = NULL;
395 }
396
397 if (is_mold)
398 base_expr = gfc_expr_to_initialize (e);
399 else
400 base_expr = gfc_copy_expr (e);
401
402 /* Restore the original tail expression. */
403 if (class_ref)
404 {
405 gfc_free_ref_list (class_ref->next);
406 class_ref->next = tail;
407 }
408 else if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
409 {
410 gfc_free_ref_list (e->ref);
411 e->ref = tail;
412 }
413 return base_expr;
414 }
415
416
417 /* Reset the vptr to the declared type, e.g. after deallocation. */
418
419 void
420 gfc_reset_vptr (stmtblock_t *block, gfc_expr *e)
421 {
422 gfc_symbol *vtab;
423 tree vptr;
424 tree vtable;
425 gfc_se se;
426
427 /* Evaluate the expression and obtain the vptr from it. */
428 gfc_init_se (&se, NULL);
429 if (e->rank)
430 gfc_conv_expr_descriptor (&se, e);
431 else
432 gfc_conv_expr (&se, e);
433 gfc_add_block_to_block (block, &se.pre);
434 vptr = gfc_get_vptr_from_expr (se.expr);
435
436 /* If a vptr is not found, we can do nothing more. */
437 if (vptr == NULL_TREE)
438 return;
439
440 if (UNLIMITED_POLY (e))
441 gfc_add_modify (block, vptr, build_int_cst (TREE_TYPE (vptr), 0));
442 else
443 {
444 /* Return the vptr to the address of the declared type. */
445 vtab = gfc_find_derived_vtab (e->ts.u.derived);
446 vtable = vtab->backend_decl;
447 if (vtable == NULL_TREE)
448 vtable = gfc_get_symbol_decl (vtab);
449 vtable = gfc_build_addr_expr (NULL, vtable);
450 vtable = fold_convert (TREE_TYPE (vptr), vtable);
451 gfc_add_modify (block, vptr, vtable);
452 }
453 }
454
455
456 /* Reset the len for unlimited polymorphic objects. */
457
458 void
459 gfc_reset_len (stmtblock_t *block, gfc_expr *expr)
460 {
461 gfc_expr *e;
462 gfc_se se_len;
463 e = gfc_find_and_cut_at_last_class_ref (expr);
464 if (e == NULL)
465 return;
466 gfc_add_len_component (e);
467 gfc_init_se (&se_len, NULL);
468 gfc_conv_expr (&se_len, e);
469 gfc_add_modify (block, se_len.expr,
470 fold_convert (TREE_TYPE (se_len.expr), integer_zero_node));
471 gfc_free_expr (e);
472 }
473
474
475 /* Obtain the last class reference in an expression.
476 Return NULL_TREE if no class reference is found. */
477
478 tree
479 gfc_get_class_from_expr (tree expr)
480 {
481 tree tmp;
482 tree type;
483
484 for (tmp = expr; tmp; tmp = TREE_OPERAND (tmp, 0))
485 {
486 type = TREE_TYPE (tmp);
487 while (type)
488 {
489 if (GFC_CLASS_TYPE_P (type))
490 return tmp;
491 if (type != TYPE_CANONICAL (type))
492 type = TYPE_CANONICAL (type);
493 else
494 type = NULL_TREE;
495 }
496 if (VAR_P (tmp) || TREE_CODE (tmp) == PARM_DECL)
497 break;
498 }
499
500 if (POINTER_TYPE_P (TREE_TYPE (tmp)))
501 tmp = build_fold_indirect_ref_loc (input_location, tmp);
502
503 if (GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
504 return tmp;
505
506 return NULL_TREE;
507 }
508
509
510 /* Obtain the vptr of the last class reference in an expression.
511 Return NULL_TREE if no class reference is found. */
512
513 tree
514 gfc_get_vptr_from_expr (tree expr)
515 {
516 tree tmp;
517
518 tmp = gfc_get_class_from_expr (expr);
519
520 if (tmp != NULL_TREE)
521 return gfc_class_vptr_get (tmp);
522
523 return NULL_TREE;
524 }
525
526
527 static void
528 class_array_data_assign (stmtblock_t *block, tree lhs_desc, tree rhs_desc,
529 bool lhs_type)
530 {
531 tree tmp, tmp2, type;
532
533 gfc_conv_descriptor_data_set (block, lhs_desc,
534 gfc_conv_descriptor_data_get (rhs_desc));
535 gfc_conv_descriptor_offset_set (block, lhs_desc,
536 gfc_conv_descriptor_offset_get (rhs_desc));
537
538 gfc_add_modify (block, gfc_conv_descriptor_dtype (lhs_desc),
539 gfc_conv_descriptor_dtype (rhs_desc));
540
541 /* Assign the dimension as range-ref. */
542 tmp = gfc_get_descriptor_dimension (lhs_desc);
543 tmp2 = gfc_get_descriptor_dimension (rhs_desc);
544
545 type = lhs_type ? TREE_TYPE (tmp) : TREE_TYPE (tmp2);
546 tmp = build4_loc (input_location, ARRAY_RANGE_REF, type, tmp,
547 gfc_index_zero_node, NULL_TREE, NULL_TREE);
548 tmp2 = build4_loc (input_location, ARRAY_RANGE_REF, type, tmp2,
549 gfc_index_zero_node, NULL_TREE, NULL_TREE);
550 gfc_add_modify (block, tmp, tmp2);
551 }
552
553
554 /* Takes a derived type expression and returns the address of a temporary
555 class object of the 'declared' type. If vptr is not NULL, this is
556 used for the temporary class object.
557 optional_alloc_ptr is false when the dummy is neither allocatable
558 nor a pointer; that's only relevant for the optional handling. */
559 void
560 gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e,
561 gfc_typespec class_ts, tree vptr, bool optional,
562 bool optional_alloc_ptr)
563 {
564 gfc_symbol *vtab;
565 tree cond_optional = NULL_TREE;
566 gfc_ss *ss;
567 tree ctree;
568 tree var;
569 tree tmp;
570 int dim;
571
572 /* The derived type needs to be converted to a temporary
573 CLASS object. */
574 tmp = gfc_typenode_for_spec (&class_ts);
575 var = gfc_create_var (tmp, "class");
576
577 /* Set the vptr. */
578 ctree = gfc_class_vptr_get (var);
579
580 if (vptr != NULL_TREE)
581 {
582 /* Use the dynamic vptr. */
583 tmp = vptr;
584 }
585 else
586 {
587 /* In this case the vtab corresponds to the derived type and the
588 vptr must point to it. */
589 vtab = gfc_find_derived_vtab (e->ts.u.derived);
590 gcc_assert (vtab);
591 tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
592 }
593 gfc_add_modify (&parmse->pre, ctree,
594 fold_convert (TREE_TYPE (ctree), tmp));
595
596 /* Now set the data field. */
597 ctree = gfc_class_data_get (var);
598
599 if (optional)
600 cond_optional = gfc_conv_expr_present (e->symtree->n.sym);
601
602 if (parmse->expr && POINTER_TYPE_P (TREE_TYPE (parmse->expr)))
603 {
604 /* If there is a ready made pointer to a derived type, use it
605 rather than evaluating the expression again. */
606 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
607 gfc_add_modify (&parmse->pre, ctree, tmp);
608 }
609 else if (parmse->ss && parmse->ss->info && parmse->ss->info->useflags)
610 {
611 /* For an array reference in an elemental procedure call we need
612 to retain the ss to provide the scalarized array reference. */
613 gfc_conv_expr_reference (parmse, e);
614 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
615 if (optional)
616 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
617 cond_optional, tmp,
618 fold_convert (TREE_TYPE (tmp), null_pointer_node));
619 gfc_add_modify (&parmse->pre, ctree, tmp);
620 }
621 else
622 {
623 ss = gfc_walk_expr (e);
624 if (ss == gfc_ss_terminator)
625 {
626 parmse->ss = NULL;
627 gfc_conv_expr_reference (parmse, e);
628
629 /* Scalar to an assumed-rank array. */
630 if (class_ts.u.derived->components->as)
631 {
632 tree type;
633 type = get_scalar_to_descriptor_type (parmse->expr,
634 gfc_expr_attr (e));
635 gfc_add_modify (&parmse->pre, gfc_conv_descriptor_dtype (ctree),
636 gfc_get_dtype (type));
637 if (optional)
638 parmse->expr = build3_loc (input_location, COND_EXPR,
639 TREE_TYPE (parmse->expr),
640 cond_optional, parmse->expr,
641 fold_convert (TREE_TYPE (parmse->expr),
642 null_pointer_node));
643 gfc_conv_descriptor_data_set (&parmse->pre, ctree, parmse->expr);
644 }
645 else
646 {
647 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
648 if (optional)
649 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
650 cond_optional, tmp,
651 fold_convert (TREE_TYPE (tmp),
652 null_pointer_node));
653 gfc_add_modify (&parmse->pre, ctree, tmp);
654 }
655 }
656 else
657 {
658 stmtblock_t block;
659 gfc_init_block (&block);
660 gfc_ref *ref;
661
662 parmse->ss = ss;
663 parmse->use_offset = 1;
664 gfc_conv_expr_descriptor (parmse, e);
665
666 /* Detect any array references with vector subscripts. */
667 for (ref = e->ref; ref; ref = ref->next)
668 if (ref->type == REF_ARRAY
669 && ref->u.ar.type != AR_ELEMENT
670 && ref->u.ar.type != AR_FULL)
671 {
672 for (dim = 0; dim < ref->u.ar.dimen; dim++)
673 if (ref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
674 break;
675 if (dim < ref->u.ar.dimen)
676 break;
677 }
678
679 /* Array references with vector subscripts and non-variable expressions
680 need be converted to a one-based descriptor. */
681 if (ref || e->expr_type != EXPR_VARIABLE)
682 {
683 for (dim = 0; dim < e->rank; ++dim)
684 gfc_conv_shift_descriptor_lbound (&block, parmse->expr, dim,
685 gfc_index_one_node);
686 }
687
688 if (e->rank != class_ts.u.derived->components->as->rank)
689 {
690 gcc_assert (class_ts.u.derived->components->as->type
691 == AS_ASSUMED_RANK);
692 class_array_data_assign (&block, ctree, parmse->expr, false);
693 }
694 else
695 {
696 if (gfc_expr_attr (e).codimension)
697 parmse->expr = fold_build1_loc (input_location,
698 VIEW_CONVERT_EXPR,
699 TREE_TYPE (ctree),
700 parmse->expr);
701 gfc_add_modify (&block, ctree, parmse->expr);
702 }
703
704 if (optional)
705 {
706 tmp = gfc_finish_block (&block);
707
708 gfc_init_block (&block);
709 gfc_conv_descriptor_data_set (&block, ctree, null_pointer_node);
710
711 tmp = build3_v (COND_EXPR, cond_optional, tmp,
712 gfc_finish_block (&block));
713 gfc_add_expr_to_block (&parmse->pre, tmp);
714 }
715 else
716 gfc_add_block_to_block (&parmse->pre, &block);
717 }
718 }
719
720 if (class_ts.u.derived->components->ts.type == BT_DERIVED
721 && class_ts.u.derived->components->ts.u.derived
722 ->attr.unlimited_polymorphic)
723 {
724 /* Take care about initializing the _len component correctly. */
725 ctree = gfc_class_len_get (var);
726 if (UNLIMITED_POLY (e))
727 {
728 gfc_expr *len;
729 gfc_se se;
730
731 len = gfc_copy_expr (e);
732 gfc_add_len_component (len);
733 gfc_init_se (&se, NULL);
734 gfc_conv_expr (&se, len);
735 if (optional)
736 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se.expr),
737 cond_optional, se.expr,
738 fold_convert (TREE_TYPE (se.expr),
739 integer_zero_node));
740 else
741 tmp = se.expr;
742 }
743 else
744 tmp = integer_zero_node;
745 gfc_add_modify (&parmse->pre, ctree, fold_convert (TREE_TYPE (ctree),
746 tmp));
747 }
748 /* Pass the address of the class object. */
749 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
750
751 if (optional && optional_alloc_ptr)
752 parmse->expr = build3_loc (input_location, COND_EXPR,
753 TREE_TYPE (parmse->expr),
754 cond_optional, parmse->expr,
755 fold_convert (TREE_TYPE (parmse->expr),
756 null_pointer_node));
757 }
758
759
760 /* Create a new class container, which is required as scalar coarrays
761 have an array descriptor while normal scalars haven't. Optionally,
762 NULL pointer checks are added if the argument is OPTIONAL. */
763
764 static void
765 class_scalar_coarray_to_class (gfc_se *parmse, gfc_expr *e,
766 gfc_typespec class_ts, bool optional)
767 {
768 tree var, ctree, tmp;
769 stmtblock_t block;
770 gfc_ref *ref;
771 gfc_ref *class_ref;
772
773 gfc_init_block (&block);
774
775 class_ref = NULL;
776 for (ref = e->ref; ref; ref = ref->next)
777 {
778 if (ref->type == REF_COMPONENT
779 && ref->u.c.component->ts.type == BT_CLASS)
780 class_ref = ref;
781 }
782
783 if (class_ref == NULL
784 && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
785 tmp = e->symtree->n.sym->backend_decl;
786 else
787 {
788 /* Remove everything after the last class reference, convert the
789 expression and then recover its tailend once more. */
790 gfc_se tmpse;
791 ref = class_ref->next;
792 class_ref->next = NULL;
793 gfc_init_se (&tmpse, NULL);
794 gfc_conv_expr (&tmpse, e);
795 class_ref->next = ref;
796 tmp = tmpse.expr;
797 }
798
799 var = gfc_typenode_for_spec (&class_ts);
800 var = gfc_create_var (var, "class");
801
802 ctree = gfc_class_vptr_get (var);
803 gfc_add_modify (&block, ctree,
804 fold_convert (TREE_TYPE (ctree), gfc_class_vptr_get (tmp)));
805
806 ctree = gfc_class_data_get (var);
807 tmp = gfc_conv_descriptor_data_get (gfc_class_data_get (tmp));
808 gfc_add_modify (&block, ctree, fold_convert (TREE_TYPE (ctree), tmp));
809
810 /* Pass the address of the class object. */
811 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
812
813 if (optional)
814 {
815 tree cond = gfc_conv_expr_present (e->symtree->n.sym);
816 tree tmp2;
817
818 tmp = gfc_finish_block (&block);
819
820 gfc_init_block (&block);
821 tmp2 = gfc_class_data_get (var);
822 gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
823 null_pointer_node));
824 tmp2 = gfc_finish_block (&block);
825
826 tmp = build3_loc (input_location, COND_EXPR, void_type_node,
827 cond, tmp, tmp2);
828 gfc_add_expr_to_block (&parmse->pre, tmp);
829 }
830 else
831 gfc_add_block_to_block (&parmse->pre, &block);
832 }
833
834
835 /* Takes an intrinsic type expression and returns the address of a temporary
836 class object of the 'declared' type. */
837 void
838 gfc_conv_intrinsic_to_class (gfc_se *parmse, gfc_expr *e,
839 gfc_typespec class_ts)
840 {
841 gfc_symbol *vtab;
842 gfc_ss *ss;
843 tree ctree;
844 tree var;
845 tree tmp;
846 int dim;
847
848 /* The intrinsic type needs to be converted to a temporary
849 CLASS object. */
850 tmp = gfc_typenode_for_spec (&class_ts);
851 var = gfc_create_var (tmp, "class");
852
853 /* Set the vptr. */
854 ctree = gfc_class_vptr_get (var);
855
856 vtab = gfc_find_vtab (&e->ts);
857 gcc_assert (vtab);
858 tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
859 gfc_add_modify (&parmse->pre, ctree,
860 fold_convert (TREE_TYPE (ctree), tmp));
861
862 /* Now set the data field. */
863 ctree = gfc_class_data_get (var);
864 if (parmse->ss && parmse->ss->info->useflags)
865 {
866 /* For an array reference in an elemental procedure call we need
867 to retain the ss to provide the scalarized array reference. */
868 gfc_conv_expr_reference (parmse, e);
869 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
870 gfc_add_modify (&parmse->pre, ctree, tmp);
871 }
872 else
873 {
874 ss = gfc_walk_expr (e);
875 if (ss == gfc_ss_terminator)
876 {
877 parmse->ss = NULL;
878 gfc_conv_expr_reference (parmse, e);
879 if (class_ts.u.derived->components->as
880 && class_ts.u.derived->components->as->type == AS_ASSUMED_RANK)
881 {
882 tmp = gfc_conv_scalar_to_descriptor (parmse, parmse->expr,
883 gfc_expr_attr (e));
884 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
885 TREE_TYPE (ctree), tmp);
886 }
887 else
888 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
889 gfc_add_modify (&parmse->pre, ctree, tmp);
890 }
891 else
892 {
893 parmse->ss = ss;
894 parmse->use_offset = 1;
895 gfc_conv_expr_descriptor (parmse, e);
896
897 /* Array references with vector subscripts and non-variable expressions
898 need be converted to a one-based descriptor. */
899 if (e->expr_type != EXPR_VARIABLE)
900 {
901 for (dim = 0; dim < e->rank; ++dim)
902 gfc_conv_shift_descriptor_lbound (&parmse->pre, parmse->expr,
903 dim, gfc_index_one_node);
904 }
905
906 if (class_ts.u.derived->components->as->rank != e->rank)
907 {
908 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
909 TREE_TYPE (ctree), parmse->expr);
910 gfc_add_modify (&parmse->pre, ctree, tmp);
911 }
912 else
913 gfc_add_modify (&parmse->pre, ctree, parmse->expr);
914 }
915 }
916
917 gcc_assert (class_ts.type == BT_CLASS);
918 if (class_ts.u.derived->components->ts.type == BT_DERIVED
919 && class_ts.u.derived->components->ts.u.derived
920 ->attr.unlimited_polymorphic)
921 {
922 ctree = gfc_class_len_get (var);
923 /* When the actual arg is a char array, then set the _len component of the
924 unlimited polymorphic entity to the length of the string. */
925 if (e->ts.type == BT_CHARACTER)
926 {
927 /* Start with parmse->string_length because this seems to be set to a
928 correct value more often. */
929 if (parmse->string_length)
930 tmp = parmse->string_length;
931 /* When the string_length is not yet set, then try the backend_decl of
932 the cl. */
933 else if (e->ts.u.cl->backend_decl)
934 tmp = e->ts.u.cl->backend_decl;
935 /* If both of the above approaches fail, then try to generate an
936 expression from the input, which is only feasible currently, when the
937 expression can be evaluated to a constant one. */
938 else
939 {
940 /* Try to simplify the expression. */
941 gfc_simplify_expr (e, 0);
942 if (e->expr_type == EXPR_CONSTANT && !e->ts.u.cl->resolved)
943 {
944 /* Amazingly all data is present to compute the length of a
945 constant string, but the expression is not yet there. */
946 e->ts.u.cl->length = gfc_get_constant_expr (BT_INTEGER,
947 gfc_charlen_int_kind,
948 &e->where);
949 mpz_set_ui (e->ts.u.cl->length->value.integer,
950 e->value.character.length);
951 gfc_conv_const_charlen (e->ts.u.cl);
952 e->ts.u.cl->resolved = 1;
953 tmp = e->ts.u.cl->backend_decl;
954 }
955 else
956 {
957 gfc_error ("Cannot compute the length of the char array "
958 "at %L.", &e->where);
959 }
960 }
961 }
962 else
963 tmp = integer_zero_node;
964
965 gfc_add_modify (&parmse->pre, ctree, fold_convert (TREE_TYPE (ctree), tmp));
966 }
967 else if (class_ts.type == BT_CLASS
968 && class_ts.u.derived->components
969 && class_ts.u.derived->components->ts.u
970 .derived->attr.unlimited_polymorphic)
971 {
972 ctree = gfc_class_len_get (var);
973 gfc_add_modify (&parmse->pre, ctree,
974 fold_convert (TREE_TYPE (ctree),
975 integer_zero_node));
976 }
977 /* Pass the address of the class object. */
978 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
979 }
980
981
982 /* Takes a scalarized class array expression and returns the
983 address of a temporary scalar class object of the 'declared'
984 type.
985 OOP-TODO: This could be improved by adding code that branched on
986 the dynamic type being the same as the declared type. In this case
987 the original class expression can be passed directly.
988 optional_alloc_ptr is false when the dummy is neither allocatable
989 nor a pointer; that's relevant for the optional handling.
990 Set copyback to true if class container's _data and _vtab pointers
991 might get modified. */
992
993 void
994 gfc_conv_class_to_class (gfc_se *parmse, gfc_expr *e, gfc_typespec class_ts,
995 bool elemental, bool copyback, bool optional,
996 bool optional_alloc_ptr)
997 {
998 tree ctree;
999 tree var;
1000 tree tmp;
1001 tree vptr;
1002 tree cond = NULL_TREE;
1003 tree slen = NULL_TREE;
1004 gfc_ref *ref;
1005 gfc_ref *class_ref;
1006 stmtblock_t block;
1007 bool full_array = false;
1008
1009 gfc_init_block (&block);
1010
1011 class_ref = NULL;
1012 for (ref = e->ref; ref; ref = ref->next)
1013 {
1014 if (ref->type == REF_COMPONENT
1015 && ref->u.c.component->ts.type == BT_CLASS)
1016 class_ref = ref;
1017
1018 if (ref->next == NULL)
1019 break;
1020 }
1021
1022 if ((ref == NULL || class_ref == ref)
1023 && !(gfc_is_class_array_function (e) && parmse->class_vptr != NULL_TREE)
1024 && (!class_ts.u.derived->components->as
1025 || class_ts.u.derived->components->as->rank != -1))
1026 return;
1027
1028 /* Test for FULL_ARRAY. */
1029 if (e->rank == 0 && gfc_expr_attr (e).codimension
1030 && gfc_expr_attr (e).dimension)
1031 full_array = true;
1032 else
1033 gfc_is_class_array_ref (e, &full_array);
1034
1035 /* The derived type needs to be converted to a temporary
1036 CLASS object. */
1037 tmp = gfc_typenode_for_spec (&class_ts);
1038 var = gfc_create_var (tmp, "class");
1039
1040 /* Set the data. */
1041 ctree = gfc_class_data_get (var);
1042 if (class_ts.u.derived->components->as
1043 && e->rank != class_ts.u.derived->components->as->rank)
1044 {
1045 if (e->rank == 0)
1046 {
1047 tree type = get_scalar_to_descriptor_type (parmse->expr,
1048 gfc_expr_attr (e));
1049 gfc_add_modify (&block, gfc_conv_descriptor_dtype (ctree),
1050 gfc_get_dtype (type));
1051
1052 tmp = gfc_class_data_get (parmse->expr);
1053 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
1054 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
1055
1056 gfc_conv_descriptor_data_set (&block, ctree, tmp);
1057 }
1058 else
1059 class_array_data_assign (&block, ctree, parmse->expr, false);
1060 }
1061 else
1062 {
1063 if (TREE_TYPE (parmse->expr) != TREE_TYPE (ctree))
1064 parmse->expr = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
1065 TREE_TYPE (ctree), parmse->expr);
1066 gfc_add_modify (&block, ctree, parmse->expr);
1067 }
1068
1069 /* Return the data component, except in the case of scalarized array
1070 references, where nullification of the cannot occur and so there
1071 is no need. */
1072 if (!elemental && full_array && copyback)
1073 {
1074 if (class_ts.u.derived->components->as
1075 && e->rank != class_ts.u.derived->components->as->rank)
1076 {
1077 if (e->rank == 0)
1078 gfc_add_modify (&parmse->post, gfc_class_data_get (parmse->expr),
1079 gfc_conv_descriptor_data_get (ctree));
1080 else
1081 class_array_data_assign (&parmse->post, parmse->expr, ctree, true);
1082 }
1083 else
1084 gfc_add_modify (&parmse->post, parmse->expr, ctree);
1085 }
1086
1087 /* Set the vptr. */
1088 ctree = gfc_class_vptr_get (var);
1089
1090 /* The vptr is the second field of the actual argument.
1091 First we have to find the corresponding class reference. */
1092
1093 tmp = NULL_TREE;
1094 if (gfc_is_class_array_function (e)
1095 && parmse->class_vptr != NULL_TREE)
1096 tmp = parmse->class_vptr;
1097 else if (class_ref == NULL
1098 && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
1099 {
1100 tmp = e->symtree->n.sym->backend_decl;
1101
1102 if (TREE_CODE (tmp) == FUNCTION_DECL)
1103 tmp = gfc_get_fake_result_decl (e->symtree->n.sym, 0);
1104
1105 if (DECL_LANG_SPECIFIC (tmp) && GFC_DECL_SAVED_DESCRIPTOR (tmp))
1106 tmp = GFC_DECL_SAVED_DESCRIPTOR (tmp);
1107
1108 slen = build_zero_cst (size_type_node);
1109 }
1110 else
1111 {
1112 /* Remove everything after the last class reference, convert the
1113 expression and then recover its tailend once more. */
1114 gfc_se tmpse;
1115 ref = class_ref->next;
1116 class_ref->next = NULL;
1117 gfc_init_se (&tmpse, NULL);
1118 gfc_conv_expr (&tmpse, e);
1119 class_ref->next = ref;
1120 tmp = tmpse.expr;
1121 slen = tmpse.string_length;
1122 }
1123
1124 gcc_assert (tmp != NULL_TREE);
1125
1126 /* Dereference if needs be. */
1127 if (TREE_CODE (TREE_TYPE (tmp)) == REFERENCE_TYPE)
1128 tmp = build_fold_indirect_ref_loc (input_location, tmp);
1129
1130 if (!(gfc_is_class_array_function (e) && parmse->class_vptr))
1131 vptr = gfc_class_vptr_get (tmp);
1132 else
1133 vptr = tmp;
1134
1135 gfc_add_modify (&block, ctree,
1136 fold_convert (TREE_TYPE (ctree), vptr));
1137
1138 /* Return the vptr component, except in the case of scalarized array
1139 references, where the dynamic type cannot change. */
1140 if (!elemental && full_array && copyback)
1141 gfc_add_modify (&parmse->post, vptr,
1142 fold_convert (TREE_TYPE (vptr), ctree));
1143
1144 /* For unlimited polymorphic objects also set the _len component. */
1145 if (class_ts.type == BT_CLASS
1146 && class_ts.u.derived->components
1147 && class_ts.u.derived->components->ts.u
1148 .derived->attr.unlimited_polymorphic)
1149 {
1150 ctree = gfc_class_len_get (var);
1151 if (UNLIMITED_POLY (e))
1152 tmp = gfc_class_len_get (tmp);
1153 else if (e->ts.type == BT_CHARACTER)
1154 {
1155 gcc_assert (slen != NULL_TREE);
1156 tmp = slen;
1157 }
1158 else
1159 tmp = build_zero_cst (size_type_node);
1160 gfc_add_modify (&parmse->pre, ctree,
1161 fold_convert (TREE_TYPE (ctree), tmp));
1162
1163 /* Return the len component, except in the case of scalarized array
1164 references, where the dynamic type cannot change. */
1165 if (!elemental && full_array && copyback
1166 && (UNLIMITED_POLY (e) || VAR_P (tmp)))
1167 gfc_add_modify (&parmse->post, tmp,
1168 fold_convert (TREE_TYPE (tmp), ctree));
1169 }
1170
1171 if (optional)
1172 {
1173 tree tmp2;
1174
1175 cond = gfc_conv_expr_present (e->symtree->n.sym);
1176 /* parmse->pre may contain some preparatory instructions for the
1177 temporary array descriptor. Those may only be executed when the
1178 optional argument is set, therefore add parmse->pre's instructions
1179 to block, which is later guarded by an if (optional_arg_given). */
1180 gfc_add_block_to_block (&parmse->pre, &block);
1181 block.head = parmse->pre.head;
1182 parmse->pre.head = NULL_TREE;
1183 tmp = gfc_finish_block (&block);
1184
1185 if (optional_alloc_ptr)
1186 tmp2 = build_empty_stmt (input_location);
1187 else
1188 {
1189 gfc_init_block (&block);
1190
1191 tmp2 = gfc_conv_descriptor_data_get (gfc_class_data_get (var));
1192 gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
1193 null_pointer_node));
1194 tmp2 = gfc_finish_block (&block);
1195 }
1196
1197 tmp = build3_loc (input_location, COND_EXPR, void_type_node,
1198 cond, tmp, tmp2);
1199 gfc_add_expr_to_block (&parmse->pre, tmp);
1200 }
1201 else
1202 gfc_add_block_to_block (&parmse->pre, &block);
1203
1204 /* Pass the address of the class object. */
1205 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
1206
1207 if (optional && optional_alloc_ptr)
1208 parmse->expr = build3_loc (input_location, COND_EXPR,
1209 TREE_TYPE (parmse->expr),
1210 cond, parmse->expr,
1211 fold_convert (TREE_TYPE (parmse->expr),
1212 null_pointer_node));
1213 }
1214
1215
1216 /* Given a class array declaration and an index, returns the address
1217 of the referenced element. */
1218
1219 tree
1220 gfc_get_class_array_ref (tree index, tree class_decl, tree data_comp,
1221 bool unlimited)
1222 {
1223 tree data, size, tmp, ctmp, offset, ptr;
1224
1225 data = data_comp != NULL_TREE ? data_comp :
1226 gfc_class_data_get (class_decl);
1227 size = gfc_class_vtab_size_get (class_decl);
1228
1229 if (unlimited)
1230 {
1231 tmp = fold_convert (gfc_array_index_type,
1232 gfc_class_len_get (class_decl));
1233 ctmp = fold_build2_loc (input_location, MULT_EXPR,
1234 gfc_array_index_type, size, tmp);
1235 tmp = fold_build2_loc (input_location, GT_EXPR,
1236 logical_type_node, tmp,
1237 build_zero_cst (TREE_TYPE (tmp)));
1238 size = fold_build3_loc (input_location, COND_EXPR,
1239 gfc_array_index_type, tmp, ctmp, size);
1240 }
1241
1242 offset = fold_build2_loc (input_location, MULT_EXPR,
1243 gfc_array_index_type,
1244 index, size);
1245
1246 data = gfc_conv_descriptor_data_get (data);
1247 ptr = fold_convert (pvoid_type_node, data);
1248 ptr = fold_build_pointer_plus_loc (input_location, ptr, offset);
1249 return fold_convert (TREE_TYPE (data), ptr);
1250 }
1251
1252
1253 /* Copies one class expression to another, assuming that if either
1254 'to' or 'from' are arrays they are packed. Should 'from' be
1255 NULL_TREE, the initialization expression for 'to' is used, assuming
1256 that the _vptr is set. */
1257
1258 tree
1259 gfc_copy_class_to_class (tree from, tree to, tree nelems, bool unlimited)
1260 {
1261 tree fcn;
1262 tree fcn_type;
1263 tree from_data;
1264 tree from_len;
1265 tree to_data;
1266 tree to_len;
1267 tree to_ref;
1268 tree from_ref;
1269 vec<tree, va_gc> *args;
1270 tree tmp;
1271 tree stdcopy;
1272 tree extcopy;
1273 tree index;
1274 bool is_from_desc = false, is_to_class = false;
1275
1276 args = NULL;
1277 /* To prevent warnings on uninitialized variables. */
1278 from_len = to_len = NULL_TREE;
1279
1280 if (from != NULL_TREE)
1281 fcn = gfc_class_vtab_copy_get (from);
1282 else
1283 fcn = gfc_class_vtab_copy_get (to);
1284
1285 fcn_type = TREE_TYPE (TREE_TYPE (fcn));
1286
1287 if (from != NULL_TREE)
1288 {
1289 is_from_desc = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from));
1290 if (is_from_desc)
1291 {
1292 from_data = from;
1293 from = GFC_DECL_SAVED_DESCRIPTOR (from);
1294 }
1295 else
1296 {
1297 /* Check that from is a class. When the class is part of a coarray,
1298 then from is a common pointer and is to be used as is. */
1299 tmp = POINTER_TYPE_P (TREE_TYPE (from))
1300 ? build_fold_indirect_ref (from) : from;
1301 from_data =
1302 (GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
1303 || (DECL_P (tmp) && GFC_DECL_CLASS (tmp)))
1304 ? gfc_class_data_get (from) : from;
1305 is_from_desc = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data));
1306 }
1307 }
1308 else
1309 from_data = gfc_class_vtab_def_init_get (to);
1310
1311 if (unlimited)
1312 {
1313 if (from != NULL_TREE && unlimited)
1314 from_len = gfc_class_len_or_zero_get (from);
1315 else
1316 from_len = build_zero_cst (size_type_node);
1317 }
1318
1319 if (GFC_CLASS_TYPE_P (TREE_TYPE (to)))
1320 {
1321 is_to_class = true;
1322 to_data = gfc_class_data_get (to);
1323 if (unlimited)
1324 to_len = gfc_class_len_get (to);
1325 }
1326 else
1327 /* When to is a BT_DERIVED and not a BT_CLASS, then to_data == to. */
1328 to_data = to;
1329
1330 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (to_data)))
1331 {
1332 stmtblock_t loopbody;
1333 stmtblock_t body;
1334 stmtblock_t ifbody;
1335 gfc_loopinfo loop;
1336 tree orig_nelems = nelems; /* Needed for bounds check. */
1337
1338 gfc_init_block (&body);
1339 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1340 gfc_array_index_type, nelems,
1341 gfc_index_one_node);
1342 nelems = gfc_evaluate_now (tmp, &body);
1343 index = gfc_create_var (gfc_array_index_type, "S");
1344
1345 if (is_from_desc)
1346 {
1347 from_ref = gfc_get_class_array_ref (index, from, from_data,
1348 unlimited);
1349 vec_safe_push (args, from_ref);
1350 }
1351 else
1352 vec_safe_push (args, from_data);
1353
1354 if (is_to_class)
1355 to_ref = gfc_get_class_array_ref (index, to, to_data, unlimited);
1356 else
1357 {
1358 tmp = gfc_conv_array_data (to);
1359 tmp = build_fold_indirect_ref_loc (input_location, tmp);
1360 to_ref = gfc_build_addr_expr (NULL_TREE,
1361 gfc_build_array_ref (tmp, index, to));
1362 }
1363 vec_safe_push (args, to_ref);
1364
1365 /* Add bounds check. */
1366 if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) > 0 && is_from_desc)
1367 {
1368 char *msg;
1369 const char *name = "<<unknown>>";
1370 tree from_len;
1371
1372 if (DECL_P (to))
1373 name = (const char *)(DECL_NAME (to)->identifier.id.str);
1374
1375 from_len = gfc_conv_descriptor_size (from_data, 1);
1376 tmp = fold_build2_loc (input_location, NE_EXPR,
1377 logical_type_node, from_len, orig_nelems);
1378 msg = xasprintf ("Array bound mismatch for dimension %d "
1379 "of array '%s' (%%ld/%%ld)",
1380 1, name);
1381
1382 gfc_trans_runtime_check (true, false, tmp, &body,
1383 &gfc_current_locus, msg,
1384 fold_convert (long_integer_type_node, orig_nelems),
1385 fold_convert (long_integer_type_node, from_len));
1386
1387 free (msg);
1388 }
1389
1390 tmp = build_call_vec (fcn_type, fcn, args);
1391
1392 /* Build the body of the loop. */
1393 gfc_init_block (&loopbody);
1394 gfc_add_expr_to_block (&loopbody, tmp);
1395
1396 /* Build the loop and return. */
1397 gfc_init_loopinfo (&loop);
1398 loop.dimen = 1;
1399 loop.from[0] = gfc_index_zero_node;
1400 loop.loopvar[0] = index;
1401 loop.to[0] = nelems;
1402 gfc_trans_scalarizing_loops (&loop, &loopbody);
1403 gfc_init_block (&ifbody);
1404 gfc_add_block_to_block (&ifbody, &loop.pre);
1405 stdcopy = gfc_finish_block (&ifbody);
1406 /* In initialization mode from_len is a constant zero. */
1407 if (unlimited && !integer_zerop (from_len))
1408 {
1409 vec_safe_push (args, from_len);
1410 vec_safe_push (args, to_len);
1411 tmp = build_call_vec (fcn_type, fcn, args);
1412 /* Build the body of the loop. */
1413 gfc_init_block (&loopbody);
1414 gfc_add_expr_to_block (&loopbody, tmp);
1415
1416 /* Build the loop and return. */
1417 gfc_init_loopinfo (&loop);
1418 loop.dimen = 1;
1419 loop.from[0] = gfc_index_zero_node;
1420 loop.loopvar[0] = index;
1421 loop.to[0] = nelems;
1422 gfc_trans_scalarizing_loops (&loop, &loopbody);
1423 gfc_init_block (&ifbody);
1424 gfc_add_block_to_block (&ifbody, &loop.pre);
1425 extcopy = gfc_finish_block (&ifbody);
1426
1427 tmp = fold_build2_loc (input_location, GT_EXPR,
1428 logical_type_node, from_len,
1429 build_zero_cst (TREE_TYPE (from_len)));
1430 tmp = fold_build3_loc (input_location, COND_EXPR,
1431 void_type_node, tmp, extcopy, stdcopy);
1432 gfc_add_expr_to_block (&body, tmp);
1433 tmp = gfc_finish_block (&body);
1434 }
1435 else
1436 {
1437 gfc_add_expr_to_block (&body, stdcopy);
1438 tmp = gfc_finish_block (&body);
1439 }
1440 gfc_cleanup_loop (&loop);
1441 }
1442 else
1443 {
1444 gcc_assert (!is_from_desc);
1445 vec_safe_push (args, from_data);
1446 vec_safe_push (args, to_data);
1447 stdcopy = build_call_vec (fcn_type, fcn, args);
1448
1449 /* In initialization mode from_len is a constant zero. */
1450 if (unlimited && !integer_zerop (from_len))
1451 {
1452 vec_safe_push (args, from_len);
1453 vec_safe_push (args, to_len);
1454 extcopy = build_call_vec (fcn_type, fcn, args);
1455 tmp = fold_build2_loc (input_location, GT_EXPR,
1456 logical_type_node, from_len,
1457 build_zero_cst (TREE_TYPE (from_len)));
1458 tmp = fold_build3_loc (input_location, COND_EXPR,
1459 void_type_node, tmp, extcopy, stdcopy);
1460 }
1461 else
1462 tmp = stdcopy;
1463 }
1464
1465 /* Only copy _def_init to to_data, when it is not a NULL-pointer. */
1466 if (from == NULL_TREE)
1467 {
1468 tree cond;
1469 cond = fold_build2_loc (input_location, NE_EXPR,
1470 logical_type_node,
1471 from_data, null_pointer_node);
1472 tmp = fold_build3_loc (input_location, COND_EXPR,
1473 void_type_node, cond,
1474 tmp, build_empty_stmt (input_location));
1475 }
1476
1477 return tmp;
1478 }
1479
1480
1481 static tree
1482 gfc_trans_class_array_init_assign (gfc_expr *rhs, gfc_expr *lhs, gfc_expr *obj)
1483 {
1484 gfc_actual_arglist *actual;
1485 gfc_expr *ppc;
1486 gfc_code *ppc_code;
1487 tree res;
1488
1489 actual = gfc_get_actual_arglist ();
1490 actual->expr = gfc_copy_expr (rhs);
1491 actual->next = gfc_get_actual_arglist ();
1492 actual->next->expr = gfc_copy_expr (lhs);
1493 ppc = gfc_copy_expr (obj);
1494 gfc_add_vptr_component (ppc);
1495 gfc_add_component_ref (ppc, "_copy");
1496 ppc_code = gfc_get_code (EXEC_CALL);
1497 ppc_code->resolved_sym = ppc->symtree->n.sym;
1498 /* Although '_copy' is set to be elemental in class.c, it is
1499 not staying that way. Find out why, sometime.... */
1500 ppc_code->resolved_sym->attr.elemental = 1;
1501 ppc_code->ext.actual = actual;
1502 ppc_code->expr1 = ppc;
1503 /* Since '_copy' is elemental, the scalarizer will take care
1504 of arrays in gfc_trans_call. */
1505 res = gfc_trans_call (ppc_code, false, NULL, NULL, false);
1506 gfc_free_statements (ppc_code);
1507
1508 if (UNLIMITED_POLY(obj))
1509 {
1510 /* Check if rhs is non-NULL. */
1511 gfc_se src;
1512 gfc_init_se (&src, NULL);
1513 gfc_conv_expr (&src, rhs);
1514 src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
1515 tree cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1516 src.expr, fold_convert (TREE_TYPE (src.expr),
1517 null_pointer_node));
1518 res = build3_loc (input_location, COND_EXPR, TREE_TYPE (res), cond, res,
1519 build_empty_stmt (input_location));
1520 }
1521
1522 return res;
1523 }
1524
1525 /* Special case for initializing a polymorphic dummy with INTENT(OUT).
1526 A MEMCPY is needed to copy the full data from the default initializer
1527 of the dynamic type. */
1528
1529 tree
1530 gfc_trans_class_init_assign (gfc_code *code)
1531 {
1532 stmtblock_t block;
1533 tree tmp;
1534 gfc_se dst,src,memsz;
1535 gfc_expr *lhs, *rhs, *sz;
1536
1537 gfc_start_block (&block);
1538
1539 lhs = gfc_copy_expr (code->expr1);
1540
1541 rhs = gfc_copy_expr (code->expr1);
1542 gfc_add_vptr_component (rhs);
1543
1544 /* Make sure that the component backend_decls have been built, which
1545 will not have happened if the derived types concerned have not
1546 been referenced. */
1547 gfc_get_derived_type (rhs->ts.u.derived);
1548 gfc_add_def_init_component (rhs);
1549 /* The _def_init is always scalar. */
1550 rhs->rank = 0;
1551
1552 if (code->expr1->ts.type == BT_CLASS
1553 && CLASS_DATA (code->expr1)->attr.dimension)
1554 {
1555 gfc_array_spec *tmparr = gfc_get_array_spec ();
1556 *tmparr = *CLASS_DATA (code->expr1)->as;
1557 /* Adding the array ref to the class expression results in correct
1558 indexing to the dynamic type. */
1559 gfc_add_full_array_ref (lhs, tmparr);
1560 tmp = gfc_trans_class_array_init_assign (rhs, lhs, code->expr1);
1561 }
1562 else
1563 {
1564 /* Scalar initialization needs the _data component. */
1565 gfc_add_data_component (lhs);
1566 sz = gfc_copy_expr (code->expr1);
1567 gfc_add_vptr_component (sz);
1568 gfc_add_size_component (sz);
1569
1570 gfc_init_se (&dst, NULL);
1571 gfc_init_se (&src, NULL);
1572 gfc_init_se (&memsz, NULL);
1573 gfc_conv_expr (&dst, lhs);
1574 gfc_conv_expr (&src, rhs);
1575 gfc_conv_expr (&memsz, sz);
1576 gfc_add_block_to_block (&block, &src.pre);
1577 src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
1578
1579 tmp = gfc_build_memcpy_call (dst.expr, src.expr, memsz.expr);
1580
1581 if (UNLIMITED_POLY(code->expr1))
1582 {
1583 /* Check if _def_init is non-NULL. */
1584 tree cond = fold_build2_loc (input_location, NE_EXPR,
1585 logical_type_node, src.expr,
1586 fold_convert (TREE_TYPE (src.expr),
1587 null_pointer_node));
1588 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
1589 tmp, build_empty_stmt (input_location));
1590 }
1591 }
1592
1593 if (code->expr1->symtree->n.sym->attr.optional
1594 || code->expr1->symtree->n.sym->ns->proc_name->attr.entry_master)
1595 {
1596 tree present = gfc_conv_expr_present (code->expr1->symtree->n.sym);
1597 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
1598 present, tmp,
1599 build_empty_stmt (input_location));
1600 }
1601
1602 gfc_add_expr_to_block (&block, tmp);
1603
1604 return gfc_finish_block (&block);
1605 }
1606
1607
1608 /* End of prototype trans-class.c */
1609
1610
1611 static void
1612 realloc_lhs_warning (bt type, bool array, locus *where)
1613 {
1614 if (array && type != BT_CLASS && type != BT_DERIVED && warn_realloc_lhs)
1615 gfc_warning (OPT_Wrealloc_lhs,
1616 "Code for reallocating the allocatable array at %L will "
1617 "be added", where);
1618 else if (warn_realloc_lhs_all)
1619 gfc_warning (OPT_Wrealloc_lhs_all,
1620 "Code for reallocating the allocatable variable at %L "
1621 "will be added", where);
1622 }
1623
1624
1625 static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
1626 gfc_expr *);
1627
1628 /* Copy the scalarization loop variables. */
1629
1630 static void
1631 gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
1632 {
1633 dest->ss = src->ss;
1634 dest->loop = src->loop;
1635 }
1636
1637
1638 /* Initialize a simple expression holder.
1639
1640 Care must be taken when multiple se are created with the same parent.
1641 The child se must be kept in sync. The easiest way is to delay creation
1642 of a child se until after the previous se has been translated. */
1643
1644 void
1645 gfc_init_se (gfc_se * se, gfc_se * parent)
1646 {
1647 memset (se, 0, sizeof (gfc_se));
1648 gfc_init_block (&se->pre);
1649 gfc_init_block (&se->post);
1650
1651 se->parent = parent;
1652
1653 if (parent)
1654 gfc_copy_se_loopvars (se, parent);
1655 }
1656
1657
1658 /* Advances to the next SS in the chain. Use this rather than setting
1659 se->ss = se->ss->next because all the parents needs to be kept in sync.
1660 See gfc_init_se. */
1661
1662 void
1663 gfc_advance_se_ss_chain (gfc_se * se)
1664 {
1665 gfc_se *p;
1666 gfc_ss *ss;
1667
1668 gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
1669
1670 p = se;
1671 /* Walk down the parent chain. */
1672 while (p != NULL)
1673 {
1674 /* Simple consistency check. */
1675 gcc_assert (p->parent == NULL || p->parent->ss == p->ss
1676 || p->parent->ss->nested_ss == p->ss);
1677
1678 /* If we were in a nested loop, the next scalarized expression can be
1679 on the parent ss' next pointer. Thus we should not take the next
1680 pointer blindly, but rather go up one nest level as long as next
1681 is the end of chain. */
1682 ss = p->ss;
1683 while (ss->next == gfc_ss_terminator && ss->parent != NULL)
1684 ss = ss->parent;
1685
1686 p->ss = ss->next;
1687
1688 p = p->parent;
1689 }
1690 }
1691
1692
1693 /* Ensures the result of the expression as either a temporary variable
1694 or a constant so that it can be used repeatedly. */
1695
1696 void
1697 gfc_make_safe_expr (gfc_se * se)
1698 {
1699 tree var;
1700
1701 if (CONSTANT_CLASS_P (se->expr))
1702 return;
1703
1704 /* We need a temporary for this result. */
1705 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
1706 gfc_add_modify (&se->pre, var, se->expr);
1707 se->expr = var;
1708 }
1709
1710
1711 /* Return an expression which determines if a dummy parameter is present.
1712 Also used for arguments to procedures with multiple entry points. */
1713
1714 tree
1715 gfc_conv_expr_present (gfc_symbol * sym)
1716 {
1717 tree decl, cond;
1718
1719 gcc_assert (sym->attr.dummy);
1720 decl = gfc_get_symbol_decl (sym);
1721
1722 /* Intrinsic scalars with VALUE attribute which are passed by value
1723 use a hidden argument to denote the present status. */
1724 if (sym->attr.value && sym->ts.type != BT_CHARACTER
1725 && sym->ts.type != BT_CLASS && sym->ts.type != BT_DERIVED
1726 && !sym->attr.dimension)
1727 {
1728 char name[GFC_MAX_SYMBOL_LEN + 2];
1729 tree tree_name;
1730
1731 gcc_assert (TREE_CODE (decl) == PARM_DECL);
1732 name[0] = '_';
1733 strcpy (&name[1], sym->name);
1734 tree_name = get_identifier (name);
1735
1736 /* Walk function argument list to find hidden arg. */
1737 cond = DECL_ARGUMENTS (DECL_CONTEXT (decl));
1738 for ( ; cond != NULL_TREE; cond = TREE_CHAIN (cond))
1739 if (DECL_NAME (cond) == tree_name
1740 && DECL_ARTIFICIAL (cond))
1741 break;
1742
1743 gcc_assert (cond);
1744 return cond;
1745 }
1746
1747 if (TREE_CODE (decl) != PARM_DECL)
1748 {
1749 /* Array parameters use a temporary descriptor, we want the real
1750 parameter. */
1751 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
1752 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
1753 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
1754 }
1755
1756 cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, decl,
1757 fold_convert (TREE_TYPE (decl), null_pointer_node));
1758
1759 /* Fortran 2008 allows to pass null pointers and non-associated pointers
1760 as actual argument to denote absent dummies. For array descriptors,
1761 we thus also need to check the array descriptor. For BT_CLASS, it
1762 can also occur for scalars and F2003 due to type->class wrapping and
1763 class->class wrapping. Note further that BT_CLASS always uses an
1764 array descriptor for arrays, also for explicit-shape/assumed-size. */
1765
1766 if (!sym->attr.allocatable
1767 && ((sym->ts.type != BT_CLASS && !sym->attr.pointer)
1768 || (sym->ts.type == BT_CLASS
1769 && !CLASS_DATA (sym)->attr.allocatable
1770 && !CLASS_DATA (sym)->attr.class_pointer))
1771 && ((gfc_option.allow_std & GFC_STD_F2008) != 0
1772 || sym->ts.type == BT_CLASS))
1773 {
1774 tree tmp;
1775
1776 if ((sym->as && (sym->as->type == AS_ASSUMED_SHAPE
1777 || sym->as->type == AS_ASSUMED_RANK
1778 || sym->attr.codimension))
1779 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
1780 {
1781 tmp = build_fold_indirect_ref_loc (input_location, decl);
1782 if (sym->ts.type == BT_CLASS)
1783 tmp = gfc_class_data_get (tmp);
1784 tmp = gfc_conv_array_data (tmp);
1785 }
1786 else if (sym->ts.type == BT_CLASS)
1787 tmp = gfc_class_data_get (decl);
1788 else
1789 tmp = NULL_TREE;
1790
1791 if (tmp != NULL_TREE)
1792 {
1793 tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
1794 fold_convert (TREE_TYPE (tmp), null_pointer_node));
1795 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1796 logical_type_node, cond, tmp);
1797 }
1798 }
1799
1800 return cond;
1801 }
1802
1803
1804 /* Converts a missing, dummy argument into a null or zero. */
1805
1806 void
1807 gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
1808 {
1809 tree present;
1810 tree tmp;
1811
1812 present = gfc_conv_expr_present (arg->symtree->n.sym);
1813
1814 if (kind > 0)
1815 {
1816 /* Create a temporary and convert it to the correct type. */
1817 tmp = gfc_get_int_type (kind);
1818 tmp = fold_convert (tmp, build_fold_indirect_ref_loc (input_location,
1819 se->expr));
1820
1821 /* Test for a NULL value. */
1822 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
1823 tmp, fold_convert (TREE_TYPE (tmp), integer_one_node));
1824 tmp = gfc_evaluate_now (tmp, &se->pre);
1825 se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
1826 }
1827 else
1828 {
1829 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
1830 present, se->expr,
1831 build_zero_cst (TREE_TYPE (se->expr)));
1832 tmp = gfc_evaluate_now (tmp, &se->pre);
1833 se->expr = tmp;
1834 }
1835
1836 if (ts.type == BT_CHARACTER)
1837 {
1838 tmp = build_int_cst (gfc_charlen_type_node, 0);
1839 tmp = fold_build3_loc (input_location, COND_EXPR, gfc_charlen_type_node,
1840 present, se->string_length, tmp);
1841 tmp = gfc_evaluate_now (tmp, &se->pre);
1842 se->string_length = tmp;
1843 }
1844 return;
1845 }
1846
1847
1848 /* Get the character length of an expression, looking through gfc_refs
1849 if necessary. */
1850
1851 tree
1852 gfc_get_expr_charlen (gfc_expr *e)
1853 {
1854 gfc_ref *r;
1855 tree length;
1856 gfc_se se;
1857
1858 gcc_assert (e->expr_type == EXPR_VARIABLE
1859 && e->ts.type == BT_CHARACTER);
1860
1861 length = NULL; /* To silence compiler warning. */
1862
1863 if (is_subref_array (e) && e->ts.u.cl->length)
1864 {
1865 gfc_se tmpse;
1866 gfc_init_se (&tmpse, NULL);
1867 gfc_conv_expr_type (&tmpse, e->ts.u.cl->length, gfc_charlen_type_node);
1868 e->ts.u.cl->backend_decl = tmpse.expr;
1869 return tmpse.expr;
1870 }
1871
1872 /* First candidate: if the variable is of type CHARACTER, the
1873 expression's length could be the length of the character
1874 variable. */
1875 if (e->symtree->n.sym->ts.type == BT_CHARACTER)
1876 length = e->symtree->n.sym->ts.u.cl->backend_decl;
1877
1878 /* Look through the reference chain for component references. */
1879 for (r = e->ref; r; r = r->next)
1880 {
1881 switch (r->type)
1882 {
1883 case REF_COMPONENT:
1884 if (r->u.c.component->ts.type == BT_CHARACTER)
1885 length = r->u.c.component->ts.u.cl->backend_decl;
1886 break;
1887
1888 case REF_ARRAY:
1889 /* Do nothing. */
1890 break;
1891
1892 case REF_SUBSTRING:
1893 gfc_init_se (&se, NULL);
1894 gfc_conv_expr_type (&se, r->u.ss.start, gfc_charlen_type_node);
1895 length = se.expr;
1896 gfc_conv_expr_type (&se, r->u.ss.end, gfc_charlen_type_node);
1897 length = fold_build2_loc (input_location, MINUS_EXPR,
1898 gfc_charlen_type_node,
1899 se.expr, length);
1900 length = fold_build2_loc (input_location, PLUS_EXPR,
1901 gfc_charlen_type_node, length,
1902 gfc_index_one_node);
1903 break;
1904
1905 default:
1906 gcc_unreachable ();
1907 break;
1908 }
1909 }
1910
1911 gcc_assert (length != NULL);
1912 return length;
1913 }
1914
1915
1916 /* Return for an expression the backend decl of the coarray. */
1917
1918 tree
1919 gfc_get_tree_for_caf_expr (gfc_expr *expr)
1920 {
1921 tree caf_decl;
1922 bool found = false;
1923 gfc_ref *ref;
1924
1925 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE);
1926
1927 /* Not-implemented diagnostic. */
1928 if (expr->symtree->n.sym->ts.type == BT_CLASS
1929 && UNLIMITED_POLY (expr->symtree->n.sym)
1930 && CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
1931 gfc_error ("Sorry, coindexed access to an unlimited polymorphic object at "
1932 "%L is not supported", &expr->where);
1933
1934 for (ref = expr->ref; ref; ref = ref->next)
1935 if (ref->type == REF_COMPONENT)
1936 {
1937 if (ref->u.c.component->ts.type == BT_CLASS
1938 && UNLIMITED_POLY (ref->u.c.component)
1939 && CLASS_DATA (ref->u.c.component)->attr.codimension)
1940 gfc_error ("Sorry, coindexed access to an unlimited polymorphic "
1941 "component at %L is not supported", &expr->where);
1942 }
1943
1944 /* Make sure the backend_decl is present before accessing it. */
1945 caf_decl = expr->symtree->n.sym->backend_decl == NULL_TREE
1946 ? gfc_get_symbol_decl (expr->symtree->n.sym)
1947 : expr->symtree->n.sym->backend_decl;
1948
1949 if (expr->symtree->n.sym->ts.type == BT_CLASS)
1950 {
1951 if (expr->ref && expr->ref->type == REF_ARRAY)
1952 {
1953 caf_decl = gfc_class_data_get (caf_decl);
1954 if (CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
1955 return caf_decl;
1956 }
1957 for (ref = expr->ref; ref; ref = ref->next)
1958 {
1959 if (ref->type == REF_COMPONENT
1960 && strcmp (ref->u.c.component->name, "_data") != 0)
1961 {
1962 caf_decl = gfc_class_data_get (caf_decl);
1963 if (CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
1964 return caf_decl;
1965 break;
1966 }
1967 else if (ref->type == REF_ARRAY && ref->u.ar.dimen)
1968 break;
1969 }
1970 }
1971 if (expr->symtree->n.sym->attr.codimension)
1972 return caf_decl;
1973
1974 /* The following code assumes that the coarray is a component reachable via
1975 only scalar components/variables; the Fortran standard guarantees this. */
1976
1977 for (ref = expr->ref; ref; ref = ref->next)
1978 if (ref->type == REF_COMPONENT)
1979 {
1980 gfc_component *comp = ref->u.c.component;
1981
1982 if (POINTER_TYPE_P (TREE_TYPE (caf_decl)))
1983 caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1984 caf_decl = fold_build3_loc (input_location, COMPONENT_REF,
1985 TREE_TYPE (comp->backend_decl), caf_decl,
1986 comp->backend_decl, NULL_TREE);
1987 if (comp->ts.type == BT_CLASS)
1988 {
1989 caf_decl = gfc_class_data_get (caf_decl);
1990 if (CLASS_DATA (comp)->attr.codimension)
1991 {
1992 found = true;
1993 break;
1994 }
1995 }
1996 if (comp->attr.codimension)
1997 {
1998 found = true;
1999 break;
2000 }
2001 }
2002 gcc_assert (found && caf_decl);
2003 return caf_decl;
2004 }
2005
2006
2007 /* Obtain the Coarray token - and optionally also the offset. */
2008
2009 void
2010 gfc_get_caf_token_offset (gfc_se *se, tree *token, tree *offset, tree caf_decl,
2011 tree se_expr, gfc_expr *expr)
2012 {
2013 tree tmp;
2014
2015 /* Coarray token. */
2016 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
2017 {
2018 gcc_assert (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl))
2019 == GFC_ARRAY_ALLOCATABLE
2020 || expr->symtree->n.sym->attr.select_type_temporary);
2021 *token = gfc_conv_descriptor_token (caf_decl);
2022 }
2023 else if (DECL_LANG_SPECIFIC (caf_decl)
2024 && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
2025 *token = GFC_DECL_TOKEN (caf_decl);
2026 else
2027 {
2028 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (caf_decl))
2029 && GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl)) != NULL_TREE);
2030 *token = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl));
2031 }
2032
2033 if (offset == NULL)
2034 return;
2035
2036 /* Offset between the coarray base address and the address wanted. */
2037 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl))
2038 && (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_ALLOCATABLE
2039 || GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_POINTER))
2040 *offset = build_int_cst (gfc_array_index_type, 0);
2041 else if (DECL_LANG_SPECIFIC (caf_decl)
2042 && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
2043 *offset = GFC_DECL_CAF_OFFSET (caf_decl);
2044 else if (GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl)) != NULL_TREE)
2045 *offset = GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl));
2046 else
2047 *offset = build_int_cst (gfc_array_index_type, 0);
2048
2049 if (POINTER_TYPE_P (TREE_TYPE (se_expr))
2050 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se_expr))))
2051 {
2052 tmp = build_fold_indirect_ref_loc (input_location, se_expr);
2053 tmp = gfc_conv_descriptor_data_get (tmp);
2054 }
2055 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se_expr)))
2056 tmp = gfc_conv_descriptor_data_get (se_expr);
2057 else
2058 {
2059 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se_expr)));
2060 tmp = se_expr;
2061 }
2062
2063 *offset = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
2064 *offset, fold_convert (gfc_array_index_type, tmp));
2065
2066 if (expr->symtree->n.sym->ts.type == BT_DERIVED
2067 && expr->symtree->n.sym->attr.codimension
2068 && expr->symtree->n.sym->ts.u.derived->attr.alloc_comp)
2069 {
2070 gfc_expr *base_expr = gfc_copy_expr (expr);
2071 gfc_ref *ref = base_expr->ref;
2072 gfc_se base_se;
2073
2074 // Iterate through the refs until the last one.
2075 while (ref->next)
2076 ref = ref->next;
2077
2078 if (ref->type == REF_ARRAY
2079 && ref->u.ar.type != AR_FULL)
2080 {
2081 const int ranksum = ref->u.ar.dimen + ref->u.ar.codimen;
2082 int i;
2083 for (i = 0; i < ranksum; ++i)
2084 {
2085 ref->u.ar.start[i] = NULL;
2086 ref->u.ar.end[i] = NULL;
2087 }
2088 ref->u.ar.type = AR_FULL;
2089 }
2090 gfc_init_se (&base_se, NULL);
2091 if (gfc_caf_attr (base_expr).dimension)
2092 {
2093 gfc_conv_expr_descriptor (&base_se, base_expr);
2094 tmp = gfc_conv_descriptor_data_get (base_se.expr);
2095 }
2096 else
2097 {
2098 gfc_conv_expr (&base_se, base_expr);
2099 tmp = base_se.expr;
2100 }
2101
2102 gfc_free_expr (base_expr);
2103 gfc_add_block_to_block (&se->pre, &base_se.pre);
2104 gfc_add_block_to_block (&se->post, &base_se.post);
2105 }
2106 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
2107 tmp = gfc_conv_descriptor_data_get (caf_decl);
2108 else
2109 {
2110 gcc_assert (POINTER_TYPE_P (TREE_TYPE (caf_decl)));
2111 tmp = caf_decl;
2112 }
2113
2114 *offset = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2115 fold_convert (gfc_array_index_type, *offset),
2116 fold_convert (gfc_array_index_type, tmp));
2117 }
2118
2119
2120 /* Convert the coindex of a coarray into an image index; the result is
2121 image_num = (idx(1)-lcobound(1)+1) + (idx(2)-lcobound(2))*extent(1)
2122 + (idx(3)-lcobound(3))*extend(1)*extent(2) + ... */
2123
2124 tree
2125 gfc_caf_get_image_index (stmtblock_t *block, gfc_expr *e, tree desc)
2126 {
2127 gfc_ref *ref;
2128 tree lbound, ubound, extent, tmp, img_idx;
2129 gfc_se se;
2130 int i;
2131
2132 for (ref = e->ref; ref; ref = ref->next)
2133 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
2134 break;
2135 gcc_assert (ref != NULL);
2136
2137 if (ref->u.ar.dimen_type[ref->u.ar.dimen] == DIMEN_THIS_IMAGE)
2138 {
2139 return build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
2140 integer_zero_node);
2141 }
2142
2143 img_idx = build_zero_cst (gfc_array_index_type);
2144 extent = build_one_cst (gfc_array_index_type);
2145 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
2146 for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
2147 {
2148 gfc_init_se (&se, NULL);
2149 gfc_conv_expr_type (&se, ref->u.ar.start[i], gfc_array_index_type);
2150 gfc_add_block_to_block (block, &se.pre);
2151 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
2152 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2153 TREE_TYPE (lbound), se.expr, lbound);
2154 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
2155 extent, tmp);
2156 img_idx = fold_build2_loc (input_location, PLUS_EXPR,
2157 TREE_TYPE (tmp), img_idx, tmp);
2158 if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
2159 {
2160 ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
2161 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
2162 extent = fold_build2_loc (input_location, MULT_EXPR,
2163 TREE_TYPE (tmp), extent, tmp);
2164 }
2165 }
2166 else
2167 for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
2168 {
2169 gfc_init_se (&se, NULL);
2170 gfc_conv_expr_type (&se, ref->u.ar.start[i], gfc_array_index_type);
2171 gfc_add_block_to_block (block, &se.pre);
2172 lbound = GFC_TYPE_ARRAY_LBOUND (TREE_TYPE (desc), i);
2173 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2174 TREE_TYPE (lbound), se.expr, lbound);
2175 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
2176 extent, tmp);
2177 img_idx = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
2178 img_idx, tmp);
2179 if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
2180 {
2181 ubound = GFC_TYPE_ARRAY_UBOUND (TREE_TYPE (desc), i);
2182 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2183 TREE_TYPE (ubound), ubound, lbound);
2184 tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
2185 tmp, build_one_cst (TREE_TYPE (tmp)));
2186 extent = fold_build2_loc (input_location, MULT_EXPR,
2187 TREE_TYPE (tmp), extent, tmp);
2188 }
2189 }
2190 img_idx = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (img_idx),
2191 img_idx, build_one_cst (TREE_TYPE (img_idx)));
2192 return fold_convert (integer_type_node, img_idx);
2193 }
2194
2195
2196 /* For each character array constructor subexpression without a ts.u.cl->length,
2197 replace it by its first element (if there aren't any elements, the length
2198 should already be set to zero). */
2199
2200 static void
2201 flatten_array_ctors_without_strlen (gfc_expr* e)
2202 {
2203 gfc_actual_arglist* arg;
2204 gfc_constructor* c;
2205
2206 if (!e)
2207 return;
2208
2209 switch (e->expr_type)
2210 {
2211
2212 case EXPR_OP:
2213 flatten_array_ctors_without_strlen (e->value.op.op1);
2214 flatten_array_ctors_without_strlen (e->value.op.op2);
2215 break;
2216
2217 case EXPR_COMPCALL:
2218 /* TODO: Implement as with EXPR_FUNCTION when needed. */
2219 gcc_unreachable ();
2220
2221 case EXPR_FUNCTION:
2222 for (arg = e->value.function.actual; arg; arg = arg->next)
2223 flatten_array_ctors_without_strlen (arg->expr);
2224 break;
2225
2226 case EXPR_ARRAY:
2227
2228 /* We've found what we're looking for. */
2229 if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
2230 {
2231 gfc_constructor *c;
2232 gfc_expr* new_expr;
2233
2234 gcc_assert (e->value.constructor);
2235
2236 c = gfc_constructor_first (e->value.constructor);
2237 new_expr = c->expr;
2238 c->expr = NULL;
2239
2240 flatten_array_ctors_without_strlen (new_expr);
2241 gfc_replace_expr (e, new_expr);
2242 break;
2243 }
2244
2245 /* Otherwise, fall through to handle constructor elements. */
2246 gcc_fallthrough ();
2247 case EXPR_STRUCTURE:
2248 for (c = gfc_constructor_first (e->value.constructor);
2249 c; c = gfc_constructor_next (c))
2250 flatten_array_ctors_without_strlen (c->expr);
2251 break;
2252
2253 default:
2254 break;
2255
2256 }
2257 }
2258
2259
2260 /* Generate code to initialize a string length variable. Returns the
2261 value. For array constructors, cl->length might be NULL and in this case,
2262 the first element of the constructor is needed. expr is the original
2263 expression so we can access it but can be NULL if this is not needed. */
2264
2265 void
2266 gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
2267 {
2268 gfc_se se;
2269
2270 gfc_init_se (&se, NULL);
2271
2272 if (!cl->length && cl->backend_decl && VAR_P (cl->backend_decl))
2273 return;
2274
2275 /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
2276 "flatten" array constructors by taking their first element; all elements
2277 should be the same length or a cl->length should be present. */
2278 if (!cl->length)
2279 {
2280 gfc_expr* expr_flat;
2281 if (!expr)
2282 return;
2283 expr_flat = gfc_copy_expr (expr);
2284 flatten_array_ctors_without_strlen (expr_flat);
2285 gfc_resolve_expr (expr_flat);
2286
2287 gfc_conv_expr (&se, expr_flat);
2288 gfc_add_block_to_block (pblock, &se.pre);
2289 cl->backend_decl = convert (gfc_charlen_type_node, se.string_length);
2290
2291 gfc_free_expr (expr_flat);
2292 return;
2293 }
2294
2295 /* Convert cl->length. */
2296
2297 gcc_assert (cl->length);
2298
2299 gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
2300 se.expr = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
2301 se.expr, build_zero_cst (TREE_TYPE (se.expr)));
2302 gfc_add_block_to_block (pblock, &se.pre);
2303
2304 if (cl->backend_decl)
2305 gfc_add_modify (pblock, cl->backend_decl, se.expr);
2306 else
2307 cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
2308 }
2309
2310
2311 static void
2312 gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
2313 const char *name, locus *where)
2314 {
2315 tree tmp;
2316 tree type;
2317 tree fault;
2318 gfc_se start;
2319 gfc_se end;
2320 char *msg;
2321 mpz_t length;
2322
2323 type = gfc_get_character_type (kind, ref->u.ss.length);
2324 type = build_pointer_type (type);
2325
2326 gfc_init_se (&start, se);
2327 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
2328 gfc_add_block_to_block (&se->pre, &start.pre);
2329
2330 if (integer_onep (start.expr))
2331 gfc_conv_string_parameter (se);
2332 else
2333 {
2334 tmp = start.expr;
2335 STRIP_NOPS (tmp);
2336 /* Avoid multiple evaluation of substring start. */
2337 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
2338 start.expr = gfc_evaluate_now (start.expr, &se->pre);
2339
2340 /* Change the start of the string. */
2341 if ((TREE_CODE (TREE_TYPE (se->expr)) == ARRAY_TYPE
2342 || TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE)
2343 && TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
2344 tmp = se->expr;
2345 else
2346 tmp = build_fold_indirect_ref_loc (input_location,
2347 se->expr);
2348 /* For BIND(C), a BT_CHARACTER is not an ARRAY_TYPE. */
2349 if (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE)
2350 {
2351 tmp = gfc_build_array_ref (tmp, start.expr, NULL);
2352 se->expr = gfc_build_addr_expr (type, tmp);
2353 }
2354 }
2355
2356 /* Length = end + 1 - start. */
2357 gfc_init_se (&end, se);
2358 if (ref->u.ss.end == NULL)
2359 end.expr = se->string_length;
2360 else
2361 {
2362 gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
2363 gfc_add_block_to_block (&se->pre, &end.pre);
2364 }
2365 tmp = end.expr;
2366 STRIP_NOPS (tmp);
2367 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
2368 end.expr = gfc_evaluate_now (end.expr, &se->pre);
2369
2370 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2371 {
2372 tree nonempty = fold_build2_loc (input_location, LE_EXPR,
2373 logical_type_node, start.expr,
2374 end.expr);
2375
2376 /* Check lower bound. */
2377 fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
2378 start.expr,
2379 build_one_cst (TREE_TYPE (start.expr)));
2380 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2381 logical_type_node, nonempty, fault);
2382 if (name)
2383 msg = xasprintf ("Substring out of bounds: lower bound (%%ld) of '%s' "
2384 "is less than one", name);
2385 else
2386 msg = xasprintf ("Substring out of bounds: lower bound (%%ld) "
2387 "is less than one");
2388 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
2389 fold_convert (long_integer_type_node,
2390 start.expr));
2391 free (msg);
2392
2393 /* Check upper bound. */
2394 fault = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
2395 end.expr, se->string_length);
2396 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2397 logical_type_node, nonempty, fault);
2398 if (name)
2399 msg = xasprintf ("Substring out of bounds: upper bound (%%ld) of '%s' "
2400 "exceeds string length (%%ld)", name);
2401 else
2402 msg = xasprintf ("Substring out of bounds: upper bound (%%ld) "
2403 "exceeds string length (%%ld)");
2404 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
2405 fold_convert (long_integer_type_node, end.expr),
2406 fold_convert (long_integer_type_node,
2407 se->string_length));
2408 free (msg);
2409 }
2410
2411 /* Try to calculate the length from the start and end expressions. */
2412 if (ref->u.ss.end
2413 && gfc_dep_difference (ref->u.ss.end, ref->u.ss.start, &length))
2414 {
2415 HOST_WIDE_INT i_len;
2416
2417 i_len = gfc_mpz_get_hwi (length) + 1;
2418 if (i_len < 0)
2419 i_len = 0;
2420
2421 tmp = build_int_cst (gfc_charlen_type_node, i_len);
2422 mpz_clear (length); /* Was initialized by gfc_dep_difference. */
2423 }
2424 else
2425 {
2426 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
2427 fold_convert (gfc_charlen_type_node, end.expr),
2428 fold_convert (gfc_charlen_type_node, start.expr));
2429 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
2430 build_int_cst (gfc_charlen_type_node, 1), tmp);
2431 tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
2432 tmp, build_int_cst (gfc_charlen_type_node, 0));
2433 }
2434
2435 se->string_length = tmp;
2436 }
2437
2438
2439 /* Convert a derived type component reference. */
2440
2441 void
2442 gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
2443 {
2444 gfc_component *c;
2445 tree tmp;
2446 tree decl;
2447 tree field;
2448 tree context;
2449
2450 c = ref->u.c.component;
2451
2452 if (c->backend_decl == NULL_TREE
2453 && ref->u.c.sym != NULL)
2454 gfc_get_derived_type (ref->u.c.sym);
2455
2456 field = c->backend_decl;
2457 gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
2458 decl = se->expr;
2459 context = DECL_FIELD_CONTEXT (field);
2460
2461 /* Components can correspond to fields of different containing
2462 types, as components are created without context, whereas
2463 a concrete use of a component has the type of decl as context.
2464 So, if the type doesn't match, we search the corresponding
2465 FIELD_DECL in the parent type. To not waste too much time
2466 we cache this result in norestrict_decl.
2467 On the other hand, if the context is a UNION or a MAP (a
2468 RECORD_TYPE within a UNION_TYPE) always use the given FIELD_DECL. */
2469
2470 if (context != TREE_TYPE (decl)
2471 && !( TREE_CODE (TREE_TYPE (field)) == UNION_TYPE /* Field is union */
2472 || TREE_CODE (context) == UNION_TYPE)) /* Field is map */
2473 {
2474 tree f2 = c->norestrict_decl;
2475 if (!f2 || DECL_FIELD_CONTEXT (f2) != TREE_TYPE (decl))
2476 for (f2 = TYPE_FIELDS (TREE_TYPE (decl)); f2; f2 = DECL_CHAIN (f2))
2477 if (TREE_CODE (f2) == FIELD_DECL
2478 && DECL_NAME (f2) == DECL_NAME (field))
2479 break;
2480 gcc_assert (f2);
2481 c->norestrict_decl = f2;
2482 field = f2;
2483 }
2484
2485 if (ref->u.c.sym && ref->u.c.sym->ts.type == BT_CLASS
2486 && strcmp ("_data", c->name) == 0)
2487 {
2488 /* Found a ref to the _data component. Store the associated ref to
2489 the vptr in se->class_vptr. */
2490 se->class_vptr = gfc_class_vptr_get (decl);
2491 }
2492 else
2493 se->class_vptr = NULL_TREE;
2494
2495 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
2496 decl, field, NULL_TREE);
2497
2498 se->expr = tmp;
2499
2500 /* Allocatable deferred char arrays are to be handled by the gfc_deferred_
2501 strlen () conditional below. */
2502 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
2503 && !(c->attr.allocatable && c->ts.deferred)
2504 && !c->attr.pdt_string)
2505 {
2506 tmp = c->ts.u.cl->backend_decl;
2507 /* Components must always be constant length. */
2508 gcc_assert (tmp && INTEGER_CST_P (tmp));
2509 se->string_length = tmp;
2510 }
2511
2512 if (gfc_deferred_strlen (c, &field))
2513 {
2514 tmp = fold_build3_loc (input_location, COMPONENT_REF,
2515 TREE_TYPE (field),
2516 decl, field, NULL_TREE);
2517 se->string_length = tmp;
2518 }
2519
2520 if (((c->attr.pointer || c->attr.allocatable)
2521 && (!c->attr.dimension && !c->attr.codimension)
2522 && c->ts.type != BT_CHARACTER)
2523 || c->attr.proc_pointer)
2524 se->expr = build_fold_indirect_ref_loc (input_location,
2525 se->expr);
2526 }
2527
2528
2529 /* This function deals with component references to components of the
2530 parent type for derived type extensions. */
2531 void
2532 conv_parent_component_references (gfc_se * se, gfc_ref * ref)
2533 {
2534 gfc_component *c;
2535 gfc_component *cmp;
2536 gfc_symbol *dt;
2537 gfc_ref parent;
2538
2539 dt = ref->u.c.sym;
2540 c = ref->u.c.component;
2541
2542 /* Return if the component is in the parent type. */
2543 for (cmp = dt->components; cmp; cmp = cmp->next)
2544 if (strcmp (c->name, cmp->name) == 0)
2545 return;
2546
2547 /* Build a gfc_ref to recursively call gfc_conv_component_ref. */
2548 parent.type = REF_COMPONENT;
2549 parent.next = NULL;
2550 parent.u.c.sym = dt;
2551 parent.u.c.component = dt->components;
2552
2553 if (dt->backend_decl == NULL)
2554 gfc_get_derived_type (dt);
2555
2556 /* Build the reference and call self. */
2557 gfc_conv_component_ref (se, &parent);
2558 parent.u.c.sym = dt->components->ts.u.derived;
2559 parent.u.c.component = c;
2560 conv_parent_component_references (se, &parent);
2561 }
2562
2563
2564 static void
2565 conv_inquiry (gfc_se * se, gfc_ref * ref, gfc_expr *expr, gfc_typespec *ts)
2566 {
2567 tree res = se->expr;
2568
2569 switch (ref->u.i)
2570 {
2571 case INQUIRY_RE:
2572 res = fold_build1_loc (input_location, REALPART_EXPR,
2573 TREE_TYPE (TREE_TYPE (res)), res);
2574 break;
2575
2576 case INQUIRY_IM:
2577 res = fold_build1_loc (input_location, IMAGPART_EXPR,
2578 TREE_TYPE (TREE_TYPE (res)), res);
2579 break;
2580
2581 case INQUIRY_KIND:
2582 res = build_int_cst (gfc_typenode_for_spec (&expr->ts),
2583 ts->kind);
2584 break;
2585
2586 case INQUIRY_LEN:
2587 res = fold_convert (gfc_typenode_for_spec (&expr->ts),
2588 se->string_length);
2589 break;
2590
2591 default:
2592 gcc_unreachable ();
2593 }
2594 se->expr = res;
2595 }
2596
2597 /* Dereference VAR where needed if it is a pointer, reference, etc.
2598 according to Fortran semantics. */
2599
2600 tree
2601 gfc_maybe_dereference_var (gfc_symbol *sym, tree var, bool descriptor_only_p,
2602 bool is_classarray)
2603 {
2604 /* Characters are entirely different from other types, they are treated
2605 separately. */
2606 if (sym->ts.type == BT_CHARACTER)
2607 {
2608 /* Dereference character pointer dummy arguments
2609 or results. */
2610 if ((sym->attr.pointer || sym->attr.allocatable)
2611 && (sym->attr.dummy
2612 || sym->attr.function
2613 || sym->attr.result))
2614 var = build_fold_indirect_ref_loc (input_location, var);
2615 }
2616 else if (!sym->attr.value)
2617 {
2618 /* Dereference temporaries for class array dummy arguments. */
2619 if (sym->attr.dummy && is_classarray
2620 && GFC_ARRAY_TYPE_P (TREE_TYPE (var)))
2621 {
2622 if (!descriptor_only_p)
2623 var = GFC_DECL_SAVED_DESCRIPTOR (var);
2624
2625 var = build_fold_indirect_ref_loc (input_location, var);
2626 }
2627
2628 /* Dereference non-character scalar dummy arguments. */
2629 if (sym->attr.dummy && !sym->attr.dimension
2630 && !(sym->attr.codimension && sym->attr.allocatable)
2631 && (sym->ts.type != BT_CLASS
2632 || (!CLASS_DATA (sym)->attr.dimension
2633 && !(CLASS_DATA (sym)->attr.codimension
2634 && CLASS_DATA (sym)->attr.allocatable))))
2635 var = build_fold_indirect_ref_loc (input_location, var);
2636
2637 /* Dereference scalar hidden result. */
2638 if (flag_f2c && sym->ts.type == BT_COMPLEX
2639 && (sym->attr.function || sym->attr.result)
2640 && !sym->attr.dimension && !sym->attr.pointer
2641 && !sym->attr.always_explicit)
2642 var = build_fold_indirect_ref_loc (input_location, var);
2643
2644 /* Dereference non-character, non-class pointer variables.
2645 These must be dummies, results, or scalars. */
2646 if (!is_classarray
2647 && (sym->attr.pointer || sym->attr.allocatable
2648 || gfc_is_associate_pointer (sym)
2649 || (sym->as && sym->as->type == AS_ASSUMED_RANK))
2650 && (sym->attr.dummy
2651 || sym->attr.function
2652 || sym->attr.result
2653 || (!sym->attr.dimension
2654 && (!sym->attr.codimension || !sym->attr.allocatable))))
2655 var = build_fold_indirect_ref_loc (input_location, var);
2656 /* Now treat the class array pointer variables accordingly. */
2657 else if (sym->ts.type == BT_CLASS
2658 && sym->attr.dummy
2659 && (CLASS_DATA (sym)->attr.dimension
2660 || CLASS_DATA (sym)->attr.codimension)
2661 && ((CLASS_DATA (sym)->as
2662 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
2663 || CLASS_DATA (sym)->attr.allocatable
2664 || CLASS_DATA (sym)->attr.class_pointer))
2665 var = build_fold_indirect_ref_loc (input_location, var);
2666 /* And the case where a non-dummy, non-result, non-function,
2667 non-allotable and non-pointer classarray is present. This case was
2668 previously covered by the first if, but with introducing the
2669 condition !is_classarray there, that case has to be covered
2670 explicitly. */
2671 else if (sym->ts.type == BT_CLASS
2672 && !sym->attr.dummy
2673 && !sym->attr.function
2674 && !sym->attr.result
2675 && (CLASS_DATA (sym)->attr.dimension
2676 || CLASS_DATA (sym)->attr.codimension)
2677 && (sym->assoc
2678 || !CLASS_DATA (sym)->attr.allocatable)
2679 && !CLASS_DATA (sym)->attr.class_pointer)
2680 var = build_fold_indirect_ref_loc (input_location, var);
2681 }
2682
2683 return var;
2684 }
2685
2686 /* Return the contents of a variable. Also handles reference/pointer
2687 variables (all Fortran pointer references are implicit). */
2688
2689 static void
2690 gfc_conv_variable (gfc_se * se, gfc_expr * expr)
2691 {
2692 gfc_ss *ss;
2693 gfc_ref *ref;
2694 gfc_symbol *sym;
2695 tree parent_decl = NULL_TREE;
2696 int parent_flag;
2697 bool return_value;
2698 bool alternate_entry;
2699 bool entry_master;
2700 bool is_classarray;
2701 bool first_time = true;
2702
2703 sym = expr->symtree->n.sym;
2704 is_classarray = IS_CLASS_ARRAY (sym);
2705 ss = se->ss;
2706 if (ss != NULL)
2707 {
2708 gfc_ss_info *ss_info = ss->info;
2709
2710 /* Check that something hasn't gone horribly wrong. */
2711 gcc_assert (ss != gfc_ss_terminator);
2712 gcc_assert (ss_info->expr == expr);
2713
2714 /* A scalarized term. We already know the descriptor. */
2715 se->expr = ss_info->data.array.descriptor;
2716 se->string_length = ss_info->string_length;
2717 ref = ss_info->data.array.ref;
2718 if (ref)
2719 gcc_assert (ref->type == REF_ARRAY
2720 && ref->u.ar.type != AR_ELEMENT);
2721 else
2722 gfc_conv_tmp_array_ref (se);
2723 }
2724 else
2725 {
2726 tree se_expr = NULL_TREE;
2727
2728 se->expr = gfc_get_symbol_decl (sym);
2729
2730 /* Deal with references to a parent results or entries by storing
2731 the current_function_decl and moving to the parent_decl. */
2732 return_value = sym->attr.function && sym->result == sym;
2733 alternate_entry = sym->attr.function && sym->attr.entry
2734 && sym->result == sym;
2735 entry_master = sym->attr.result
2736 && sym->ns->proc_name->attr.entry_master
2737 && !gfc_return_by_reference (sym->ns->proc_name);
2738 if (current_function_decl)
2739 parent_decl = DECL_CONTEXT (current_function_decl);
2740
2741 if ((se->expr == parent_decl && return_value)
2742 || (sym->ns && sym->ns->proc_name
2743 && parent_decl
2744 && sym->ns->proc_name->backend_decl == parent_decl
2745 && (alternate_entry || entry_master)))
2746 parent_flag = 1;
2747 else
2748 parent_flag = 0;
2749
2750 /* Special case for assigning the return value of a function.
2751 Self recursive functions must have an explicit return value. */
2752 if (return_value && (se->expr == current_function_decl || parent_flag))
2753 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
2754
2755 /* Similarly for alternate entry points. */
2756 else if (alternate_entry
2757 && (sym->ns->proc_name->backend_decl == current_function_decl
2758 || parent_flag))
2759 {
2760 gfc_entry_list *el = NULL;
2761
2762 for (el = sym->ns->entries; el; el = el->next)
2763 if (sym == el->sym)
2764 {
2765 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
2766 break;
2767 }
2768 }
2769
2770 else if (entry_master
2771 && (sym->ns->proc_name->backend_decl == current_function_decl
2772 || parent_flag))
2773 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
2774
2775 if (se_expr)
2776 se->expr = se_expr;
2777
2778 /* Procedure actual arguments. Look out for temporary variables
2779 with the same attributes as function values. */
2780 else if (!sym->attr.temporary
2781 && sym->attr.flavor == FL_PROCEDURE
2782 && se->expr != current_function_decl)
2783 {
2784 if (!sym->attr.dummy && !sym->attr.proc_pointer)
2785 {
2786 gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
2787 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
2788 }
2789 return;
2790 }
2791
2792 /* Dereference the expression, where needed. */
2793 se->expr = gfc_maybe_dereference_var (sym, se->expr, se->descriptor_only,
2794 is_classarray);
2795
2796 ref = expr->ref;
2797 }
2798
2799 /* For character variables, also get the length. */
2800 if (sym->ts.type == BT_CHARACTER)
2801 {
2802 /* If the character length of an entry isn't set, get the length from
2803 the master function instead. */
2804 if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
2805 se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
2806 else
2807 se->string_length = sym->ts.u.cl->backend_decl;
2808 gcc_assert (se->string_length);
2809 }
2810
2811 gfc_typespec *ts = &sym->ts;
2812 while (ref)
2813 {
2814 switch (ref->type)
2815 {
2816 case REF_ARRAY:
2817 /* Return the descriptor if that's what we want and this is an array
2818 section reference. */
2819 if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
2820 return;
2821 /* TODO: Pointers to single elements of array sections, eg elemental subs. */
2822 /* Return the descriptor for array pointers and allocations. */
2823 if (se->want_pointer
2824 && ref->next == NULL && (se->descriptor_only))
2825 return;
2826
2827 gfc_conv_array_ref (se, &ref->u.ar, expr, &expr->where);
2828 /* Return a pointer to an element. */
2829 break;
2830
2831 case REF_COMPONENT:
2832 ts = &ref->u.c.component->ts;
2833 if (first_time && is_classarray && sym->attr.dummy
2834 && se->descriptor_only
2835 && !CLASS_DATA (sym)->attr.allocatable
2836 && !CLASS_DATA (sym)->attr.class_pointer
2837 && CLASS_DATA (sym)->as
2838 && CLASS_DATA (sym)->as->type != AS_ASSUMED_RANK
2839 && strcmp ("_data", ref->u.c.component->name) == 0)
2840 /* Skip the first ref of a _data component, because for class
2841 arrays that one is already done by introducing a temporary
2842 array descriptor. */
2843 break;
2844
2845 if (ref->u.c.sym->attr.extension)
2846 conv_parent_component_references (se, ref);
2847
2848 gfc_conv_component_ref (se, ref);
2849 if (!ref->next && ref->u.c.sym->attr.codimension
2850 && se->want_pointer && se->descriptor_only)
2851 return;
2852
2853 break;
2854
2855 case REF_SUBSTRING:
2856 gfc_conv_substring (se, ref, expr->ts.kind,
2857 expr->symtree->name, &expr->where);
2858 break;
2859
2860 case REF_INQUIRY:
2861 conv_inquiry (se, ref, expr, ts);
2862 break;
2863
2864 default:
2865 gcc_unreachable ();
2866 break;
2867 }
2868 first_time = false;
2869 ref = ref->next;
2870 }
2871 /* Pointer assignment, allocation or pass by reference. Arrays are handled
2872 separately. */
2873 if (se->want_pointer)
2874 {
2875 if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr))
2876 gfc_conv_string_parameter (se);
2877 else
2878 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
2879 }
2880 }
2881
2882
2883 /* Unary ops are easy... Or they would be if ! was a valid op. */
2884
2885 static void
2886 gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
2887 {
2888 gfc_se operand;
2889 tree type;
2890
2891 gcc_assert (expr->ts.type != BT_CHARACTER);
2892 /* Initialize the operand. */
2893 gfc_init_se (&operand, se);
2894 gfc_conv_expr_val (&operand, expr->value.op.op1);
2895 gfc_add_block_to_block (&se->pre, &operand.pre);
2896
2897 type = gfc_typenode_for_spec (&expr->ts);
2898
2899 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
2900 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
2901 All other unary operators have an equivalent GIMPLE unary operator. */
2902 if (code == TRUTH_NOT_EXPR)
2903 se->expr = fold_build2_loc (input_location, EQ_EXPR, type, operand.expr,
2904 build_int_cst (type, 0));
2905 else
2906 se->expr = fold_build1_loc (input_location, code, type, operand.expr);
2907
2908 }
2909
2910 /* Expand power operator to optimal multiplications when a value is raised
2911 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
2912 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
2913 Programming", 3rd Edition, 1998. */
2914
2915 /* This code is mostly duplicated from expand_powi in the backend.
2916 We establish the "optimal power tree" lookup table with the defined size.
2917 The items in the table are the exponents used to calculate the index
2918 exponents. Any integer n less than the value can get an "addition chain",
2919 with the first node being one. */
2920 #define POWI_TABLE_SIZE 256
2921
2922 /* The table is from builtins.c. */
2923 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2924 {
2925 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2926 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2927 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2928 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2929 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2930 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2931 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2932 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2933 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2934 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2935 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2936 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2937 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2938 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2939 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2940 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2941 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2942 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2943 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2944 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2945 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2946 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2947 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2948 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2949 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2950 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2951 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2952 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2953 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2954 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2955 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2956 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2957 };
2958
2959 /* If n is larger than lookup table's max index, we use the "window
2960 method". */
2961 #define POWI_WINDOW_SIZE 3
2962
2963 /* Recursive function to expand the power operator. The temporary
2964 values are put in tmpvar. The function returns tmpvar[1] ** n. */
2965 static tree
2966 gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
2967 {
2968 tree op0;
2969 tree op1;
2970 tree tmp;
2971 int digit;
2972
2973 if (n < POWI_TABLE_SIZE)
2974 {
2975 if (tmpvar[n])
2976 return tmpvar[n];
2977
2978 op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
2979 op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
2980 }
2981 else if (n & 1)
2982 {
2983 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2984 op0 = gfc_conv_powi (se, n - digit, tmpvar);
2985 op1 = gfc_conv_powi (se, digit, tmpvar);
2986 }
2987 else
2988 {
2989 op0 = gfc_conv_powi (se, n >> 1, tmpvar);
2990 op1 = op0;
2991 }
2992
2993 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
2994 tmp = gfc_evaluate_now (tmp, &se->pre);
2995
2996 if (n < POWI_TABLE_SIZE)
2997 tmpvar[n] = tmp;
2998
2999 return tmp;
3000 }
3001
3002
3003 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
3004 return 1. Else return 0 and a call to runtime library functions
3005 will have to be built. */
3006 static int
3007 gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
3008 {
3009 tree cond;
3010 tree tmp;
3011 tree type;
3012 tree vartmp[POWI_TABLE_SIZE];
3013 HOST_WIDE_INT m;
3014 unsigned HOST_WIDE_INT n;
3015 int sgn;
3016 wi::tree_to_wide_ref wrhs = wi::to_wide (rhs);
3017
3018 /* If exponent is too large, we won't expand it anyway, so don't bother
3019 with large integer values. */
3020 if (!wi::fits_shwi_p (wrhs))
3021 return 0;
3022
3023 m = wrhs.to_shwi ();
3024 /* Use the wide_int's routine to reliably get the absolute value on all
3025 platforms. Then convert it to a HOST_WIDE_INT like above. */
3026 n = wi::abs (wrhs).to_shwi ();
3027
3028 type = TREE_TYPE (lhs);
3029 sgn = tree_int_cst_sgn (rhs);
3030
3031 if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
3032 || optimize_size) && (m > 2 || m < -1))
3033 return 0;
3034
3035 /* rhs == 0 */
3036 if (sgn == 0)
3037 {
3038 se->expr = gfc_build_const (type, integer_one_node);
3039 return 1;
3040 }
3041
3042 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
3043 if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
3044 {
3045 tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3046 lhs, build_int_cst (TREE_TYPE (lhs), -1));
3047 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3048 lhs, build_int_cst (TREE_TYPE (lhs), 1));
3049
3050 /* If rhs is even,
3051 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
3052 if ((n & 1) == 0)
3053 {
3054 tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
3055 logical_type_node, tmp, cond);
3056 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
3057 tmp, build_int_cst (type, 1),
3058 build_int_cst (type, 0));
3059 return 1;
3060 }
3061 /* If rhs is odd,
3062 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
3063 tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp,
3064 build_int_cst (type, -1),
3065 build_int_cst (type, 0));
3066 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
3067 cond, build_int_cst (type, 1), tmp);
3068 return 1;
3069 }
3070
3071 memset (vartmp, 0, sizeof (vartmp));
3072 vartmp[1] = lhs;
3073 if (sgn == -1)
3074 {
3075 tmp = gfc_build_const (type, integer_one_node);
3076 vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
3077 vartmp[1]);
3078 }
3079
3080 se->expr = gfc_conv_powi (se, n, vartmp);
3081
3082 return 1;
3083 }
3084
3085
3086 /* Power op (**). Constant integer exponent has special handling. */
3087
3088 static void
3089 gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
3090 {
3091 tree gfc_int4_type_node;
3092 int kind;
3093 int ikind;
3094 int res_ikind_1, res_ikind_2;
3095 gfc_se lse;
3096 gfc_se rse;
3097 tree fndecl = NULL;
3098
3099 gfc_init_se (&lse, se);
3100 gfc_conv_expr_val (&lse, expr->value.op.op1);
3101 lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
3102 gfc_add_block_to_block (&se->pre, &lse.pre);
3103
3104 gfc_init_se (&rse, se);
3105 gfc_conv_expr_val (&rse, expr->value.op.op2);
3106 gfc_add_block_to_block (&se->pre, &rse.pre);
3107
3108 if (expr->value.op.op2->ts.type == BT_INTEGER
3109 && expr->value.op.op2->expr_type == EXPR_CONSTANT)
3110 if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
3111 return;
3112
3113 if (INTEGER_CST_P (lse.expr)
3114 && TREE_CODE (TREE_TYPE (rse.expr)) == INTEGER_TYPE)
3115 {
3116 wi::tree_to_wide_ref wlhs = wi::to_wide (lse.expr);
3117 HOST_WIDE_INT v, w;
3118 int kind, ikind, bit_size;
3119
3120 v = wlhs.to_shwi ();
3121 w = abs (v);
3122
3123 kind = expr->value.op.op1->ts.kind;
3124 ikind = gfc_validate_kind (BT_INTEGER, kind, false);
3125 bit_size = gfc_integer_kinds[ikind].bit_size;
3126
3127 if (v == 1)
3128 {
3129 /* 1**something is always 1. */
3130 se->expr = build_int_cst (TREE_TYPE (lse.expr), 1);
3131 return;
3132 }
3133 else if (v == -1)
3134 {
3135 /* (-1)**n is 1 - ((n & 1) << 1) */
3136 tree type;
3137 tree tmp;
3138
3139 type = TREE_TYPE (lse.expr);
3140 tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type,
3141 rse.expr, build_int_cst (type, 1));
3142 tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3143 tmp, build_int_cst (type, 1));
3144 tmp = fold_build2_loc (input_location, MINUS_EXPR, type,
3145 build_int_cst (type, 1), tmp);
3146 se->expr = tmp;
3147 return;
3148 }
3149 else if (w > 0 && ((w & (w-1)) == 0) && ((w >> (bit_size-1)) == 0))
3150 {
3151 /* Here v is +/- 2**e. The further simplification uses
3152 2**n = 1<<n, 4**n = 1<<(n+n), 8**n = 1 <<(3*n), 16**n =
3153 1<<(4*n), etc., but we have to make sure to return zero
3154 if the number of bits is too large. */
3155 tree lshift;
3156 tree type;
3157 tree shift;
3158 tree ge;
3159 tree cond;
3160 tree num_bits;
3161 tree cond2;
3162 tree tmp1;
3163
3164 type = TREE_TYPE (lse.expr);
3165
3166 if (w == 2)
3167 shift = rse.expr;
3168 else if (w == 4)
3169 shift = fold_build2_loc (input_location, PLUS_EXPR,
3170 TREE_TYPE (rse.expr),
3171 rse.expr, rse.expr);
3172 else
3173 {
3174 /* use popcount for fast log2(w) */
3175 int e = wi::popcount (w-1);
3176 shift = fold_build2_loc (input_location, MULT_EXPR,
3177 TREE_TYPE (rse.expr),
3178 build_int_cst (TREE_TYPE (rse.expr), e),
3179 rse.expr);
3180 }
3181
3182 lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3183 build_int_cst (type, 1), shift);
3184 ge = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
3185 rse.expr, build_int_cst (type, 0));
3186 cond = fold_build3_loc (input_location, COND_EXPR, type, ge, lshift,
3187 build_int_cst (type, 0));
3188 num_bits = build_int_cst (TREE_TYPE (rse.expr), TYPE_PRECISION (type));
3189 cond2 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
3190 rse.expr, num_bits);
3191 tmp1 = fold_build3_loc (input_location, COND_EXPR, type, cond2,
3192 build_int_cst (type, 0), cond);
3193 if (v > 0)
3194 {
3195 se->expr = tmp1;
3196 }
3197 else
3198 {
3199 /* for v < 0, calculate v**n = |v|**n * (-1)**n */
3200 tree tmp2;
3201 tmp2 = fold_build2_loc (input_location, BIT_AND_EXPR, type,
3202 rse.expr, build_int_cst (type, 1));
3203 tmp2 = fold_build2_loc (input_location, LSHIFT_EXPR, type,
3204 tmp2, build_int_cst (type, 1));
3205 tmp2 = fold_build2_loc (input_location, MINUS_EXPR, type,
3206 build_int_cst (type, 1), tmp2);
3207 se->expr = fold_build2_loc (input_location, MULT_EXPR, type,
3208 tmp1, tmp2);
3209 }
3210 return;
3211 }
3212 }
3213
3214 gfc_int4_type_node = gfc_get_int_type (4);
3215
3216 /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
3217 library routine. But in the end, we have to convert the result back
3218 if this case applies -- with res_ikind_K, we keep track whether operand K
3219 falls into this case. */
3220 res_ikind_1 = -1;
3221 res_ikind_2 = -1;
3222
3223 kind = expr->value.op.op1->ts.kind;
3224 switch (expr->value.op.op2->ts.type)
3225 {
3226 case BT_INTEGER:
3227 ikind = expr->value.op.op2->ts.kind;
3228 switch (ikind)
3229 {
3230 case 1:
3231 case 2:
3232 rse.expr = convert (gfc_int4_type_node, rse.expr);
3233 res_ikind_2 = ikind;
3234 /* Fall through. */
3235
3236 case 4:
3237 ikind = 0;
3238 break;
3239
3240 case 8:
3241 ikind = 1;
3242 break;
3243
3244 case 16:
3245 ikind = 2;
3246 break;
3247
3248 default:
3249 gcc_unreachable ();
3250 }
3251 switch (kind)
3252 {
3253 case 1:
3254 case 2:
3255 if (expr->value.op.op1->ts.type == BT_INTEGER)
3256 {
3257 lse.expr = convert (gfc_int4_type_node, lse.expr);
3258 res_ikind_1 = kind;
3259 }
3260 else
3261 gcc_unreachable ();
3262 /* Fall through. */
3263
3264 case 4:
3265 kind = 0;
3266 break;
3267
3268 case 8:
3269 kind = 1;
3270 break;
3271
3272 case 10:
3273 kind = 2;
3274 break;
3275
3276 case 16:
3277 kind = 3;
3278 break;
3279
3280 default:
3281 gcc_unreachable ();
3282 }
3283
3284 switch (expr->value.op.op1->ts.type)
3285 {
3286 case BT_INTEGER:
3287 if (kind == 3) /* Case 16 was not handled properly above. */
3288 kind = 2;
3289 fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
3290 break;
3291
3292 case BT_REAL:
3293 /* Use builtins for real ** int4. */
3294 if (ikind == 0)
3295 {
3296 switch (kind)
3297 {
3298 case 0:
3299 fndecl = builtin_decl_explicit (BUILT_IN_POWIF);
3300 break;
3301
3302 case 1:
3303 fndecl = builtin_decl_explicit (BUILT_IN_POWI);
3304 break;
3305
3306 case 2:
3307 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
3308 break;
3309
3310 case 3:
3311 /* Use the __builtin_powil() only if real(kind=16) is
3312 actually the C long double type. */
3313 if (!gfc_real16_is_float128)
3314 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
3315 break;
3316
3317 default:
3318 gcc_unreachable ();
3319 }
3320 }
3321
3322 /* If we don't have a good builtin for this, go for the
3323 library function. */
3324 if (!fndecl)
3325 fndecl = gfor_fndecl_math_powi[kind][ikind].real;
3326 break;
3327
3328 case BT_COMPLEX:
3329 fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
3330 break;
3331
3332 default:
3333 gcc_unreachable ();
3334 }
3335 break;
3336
3337 case BT_REAL:
3338 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
3339 break;
3340
3341 case BT_COMPLEX:
3342 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
3343 break;
3344
3345 default:
3346 gcc_unreachable ();
3347 break;
3348 }
3349
3350 se->expr = build_call_expr_loc (input_location,
3351 fndecl, 2, lse.expr, rse.expr);
3352
3353 /* Convert the result back if it is of wrong integer kind. */
3354 if (res_ikind_1 != -1 && res_ikind_2 != -1)
3355 {
3356 /* We want the maximum of both operand kinds as result. */
3357 if (res_ikind_1 < res_ikind_2)
3358 res_ikind_1 = res_ikind_2;
3359 se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr);
3360 }
3361 }
3362
3363
3364 /* Generate code to allocate a string temporary. */
3365
3366 tree
3367 gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
3368 {
3369 tree var;
3370 tree tmp;
3371
3372 if (gfc_can_put_var_on_stack (len))
3373 {
3374 /* Create a temporary variable to hold the result. */
3375 tmp = fold_build2_loc (input_location, MINUS_EXPR,
3376 TREE_TYPE (len), len,
3377 build_int_cst (TREE_TYPE (len), 1));
3378 tmp = build_range_type (gfc_charlen_type_node, size_zero_node, tmp);
3379
3380 if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
3381 tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
3382 else
3383 tmp = build_array_type (TREE_TYPE (type), tmp);
3384
3385 var = gfc_create_var (tmp, "str");
3386 var = gfc_build_addr_expr (type, var);
3387 }
3388 else
3389 {
3390 /* Allocate a temporary to hold the result. */
3391 var = gfc_create_var (type, "pstr");
3392 gcc_assert (POINTER_TYPE_P (type));
3393 tmp = TREE_TYPE (type);
3394 if (TREE_CODE (tmp) == ARRAY_TYPE)
3395 tmp = TREE_TYPE (tmp);
3396 tmp = TYPE_SIZE_UNIT (tmp);
3397 tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
3398 fold_convert (size_type_node, len),
3399 fold_convert (size_type_node, tmp));
3400 tmp = gfc_call_malloc (&se->pre, type, tmp);
3401 gfc_add_modify (&se->pre, var, tmp);
3402
3403 /* Free the temporary afterwards. */
3404 tmp = gfc_call_free (var);
3405 gfc_add_expr_to_block (&se->post, tmp);
3406 }
3407
3408 return var;
3409 }
3410
3411
3412 /* Handle a string concatenation operation. A temporary will be allocated to
3413 hold the result. */
3414
3415 static void
3416 gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
3417 {
3418 gfc_se lse, rse;
3419 tree len, type, var, tmp, fndecl;
3420
3421 gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
3422 && expr->value.op.op2->ts.type == BT_CHARACTER);
3423 gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
3424
3425 gfc_init_se (&lse, se);
3426 gfc_conv_expr (&lse, expr->value.op.op1);
3427 gfc_conv_string_parameter (&lse);
3428 gfc_init_se (&rse, se);
3429 gfc_conv_expr (&rse, expr->value.op.op2);
3430 gfc_conv_string_parameter (&rse);
3431
3432 gfc_add_block_to_block (&se->pre, &lse.pre);
3433 gfc_add_block_to_block (&se->pre, &rse.pre);
3434
3435 type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
3436 len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
3437 if (len == NULL_TREE)
3438 {
3439 len = fold_build2_loc (input_location, PLUS_EXPR,
3440 gfc_charlen_type_node,
3441 fold_convert (gfc_charlen_type_node,
3442 lse.string_length),
3443 fold_convert (gfc_charlen_type_node,
3444 rse.string_length));
3445 }
3446
3447 type = build_pointer_type (type);
3448
3449 var = gfc_conv_string_tmp (se, type, len);
3450
3451 /* Do the actual concatenation. */
3452 if (expr->ts.kind == 1)
3453 fndecl = gfor_fndecl_concat_string;
3454 else if (expr->ts.kind == 4)
3455 fndecl = gfor_fndecl_concat_string_char4;
3456 else
3457 gcc_unreachable ();
3458
3459 tmp = build_call_expr_loc (input_location,
3460 fndecl, 6, len, var, lse.string_length, lse.expr,
3461 rse.string_length, rse.expr);
3462 gfc_add_expr_to_block (&se->pre, tmp);
3463
3464 /* Add the cleanup for the operands. */
3465 gfc_add_block_to_block (&se->pre, &rse.post);
3466 gfc_add_block_to_block (&se->pre, &lse.post);
3467
3468 se->expr = var;
3469 se->string_length = len;
3470 }
3471
3472 /* Translates an op expression. Common (binary) cases are handled by this
3473 function, others are passed on. Recursion is used in either case.
3474 We use the fact that (op1.ts == op2.ts) (except for the power
3475 operator **).
3476 Operators need no special handling for scalarized expressions as long as
3477 they call gfc_conv_simple_val to get their operands.
3478 Character strings get special handling. */
3479
3480 static void
3481 gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
3482 {
3483 enum tree_code code;
3484 gfc_se lse;
3485 gfc_se rse;
3486 tree tmp, type;
3487 int lop;
3488 int checkstring;
3489
3490 checkstring = 0;
3491 lop = 0;
3492 switch (expr->value.op.op)
3493 {
3494 case INTRINSIC_PARENTHESES:
3495 if ((expr->ts.type == BT_REAL || expr->ts.type == BT_COMPLEX)
3496 && flag_protect_parens)
3497 {
3498 gfc_conv_unary_op (PAREN_EXPR, se, expr);
3499 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
3500 return;
3501 }
3502
3503 /* Fallthrough. */
3504 case INTRINSIC_UPLUS:
3505 gfc_conv_expr (se, expr->value.op.op1);
3506 return;
3507
3508 case INTRINSIC_UMINUS:
3509 gfc_conv_unary_op (NEGATE_EXPR, se, expr);
3510 return;
3511
3512 case INTRINSIC_NOT:
3513 gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
3514 return;
3515
3516 case INTRINSIC_PLUS:
3517 code = PLUS_EXPR;
3518 break;
3519
3520 case INTRINSIC_MINUS:
3521 code = MINUS_EXPR;
3522 break;
3523
3524 case INTRINSIC_TIMES:
3525 code = MULT_EXPR;
3526 break;
3527
3528 case INTRINSIC_DIVIDE:
3529 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
3530 an integer, we must round towards zero, so we use a
3531 TRUNC_DIV_EXPR. */
3532 if (expr->ts.type == BT_INTEGER)
3533 code = TRUNC_DIV_EXPR;
3534 else
3535 code = RDIV_EXPR;
3536 break;
3537
3538 case INTRINSIC_POWER:
3539 gfc_conv_power_op (se, expr);
3540 return;
3541
3542 case INTRINSIC_CONCAT:
3543 gfc_conv_concat_op (se, expr);
3544 return;
3545
3546 case INTRINSIC_AND:
3547 code = flag_frontend_optimize ? TRUTH_ANDIF_EXPR : TRUTH_AND_EXPR;
3548 lop = 1;
3549 break;
3550
3551 case INTRINSIC_OR:
3552 code = flag_frontend_optimize ? TRUTH_ORIF_EXPR : TRUTH_OR_EXPR;
3553 lop = 1;
3554 break;
3555
3556 /* EQV and NEQV only work on logicals, but since we represent them
3557 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
3558 case INTRINSIC_EQ:
3559 case INTRINSIC_EQ_OS:
3560 case INTRINSIC_EQV:
3561 code = EQ_EXPR;
3562 checkstring = 1;
3563 lop = 1;
3564 break;
3565
3566 case INTRINSIC_NE:
3567 case INTRINSIC_NE_OS:
3568 case INTRINSIC_NEQV:
3569 code = NE_EXPR;
3570 checkstring = 1;
3571 lop = 1;
3572 break;
3573
3574 case INTRINSIC_GT:
3575 case INTRINSIC_GT_OS:
3576 code = GT_EXPR;
3577 checkstring = 1;
3578 lop = 1;
3579 break;
3580
3581 case INTRINSIC_GE:
3582 case INTRINSIC_GE_OS:
3583 code = GE_EXPR;
3584 checkstring = 1;
3585 lop = 1;
3586 break;
3587
3588 case INTRINSIC_LT:
3589 case INTRINSIC_LT_OS:
3590 code = LT_EXPR;
3591 checkstring = 1;
3592 lop = 1;
3593 break;
3594
3595 case INTRINSIC_LE:
3596 case INTRINSIC_LE_OS:
3597 code = LE_EXPR;
3598 checkstring = 1;
3599 lop = 1;
3600 break;
3601
3602 case INTRINSIC_USER:
3603 case INTRINSIC_ASSIGN:
3604 /* These should be converted into function calls by the frontend. */
3605 gcc_unreachable ();
3606
3607 default:
3608 fatal_error (input_location, "Unknown intrinsic op");
3609 return;
3610 }
3611
3612 /* The only exception to this is **, which is handled separately anyway. */
3613 gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
3614
3615 if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
3616 checkstring = 0;
3617
3618 /* lhs */
3619 gfc_init_se (&lse, se);
3620 gfc_conv_expr (&lse, expr->value.op.op1);
3621 gfc_add_block_to_block (&se->pre, &lse.pre);
3622
3623 /* rhs */
3624 gfc_init_se (&rse, se);
3625 gfc_conv_expr (&rse, expr->value.op.op2);
3626 gfc_add_block_to_block (&se->pre, &rse.pre);
3627
3628 if (checkstring)
3629 {
3630 gfc_conv_string_parameter (&lse);
3631 gfc_conv_string_parameter (&rse);
3632
3633 lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
3634 rse.string_length, rse.expr,
3635 expr->value.op.op1->ts.kind,
3636 code);
3637 rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
3638 gfc_add_block_to_block (&lse.post, &rse.post);
3639 }
3640
3641 type = gfc_typenode_for_spec (&expr->ts);
3642
3643 if (lop)
3644 {
3645 /* The result of logical ops is always logical_type_node. */
3646 tmp = fold_build2_loc (input_location, code, logical_type_node,
3647 lse.expr, rse.expr);
3648 se->expr = convert (type, tmp);
3649 }
3650 else
3651 se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
3652
3653 /* Add the post blocks. */
3654 gfc_add_block_to_block (&se->post, &rse.post);
3655 gfc_add_block_to_block (&se->post, &lse.post);
3656 }
3657
3658 /* If a string's length is one, we convert it to a single character. */
3659
3660 tree
3661 gfc_string_to_single_character (tree len, tree str, int kind)
3662 {
3663
3664 if (len == NULL
3665 || !tree_fits_uhwi_p (len)
3666 || !POINTER_TYPE_P (TREE_TYPE (str)))
3667 return NULL_TREE;
3668
3669 if (TREE_INT_CST_LOW (len) == 1)
3670 {
3671 str = fold_convert (gfc_get_pchar_type (kind), str);
3672 return build_fold_indirect_ref_loc (input_location, str);
3673 }
3674
3675 if (kind == 1
3676 && TREE_CODE (str) == ADDR_EXPR
3677 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
3678 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
3679 && array_ref_low_bound (TREE_OPERAND (str, 0))
3680 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
3681 && TREE_INT_CST_LOW (len) > 1
3682 && TREE_INT_CST_LOW (len)
3683 == (unsigned HOST_WIDE_INT)
3684 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
3685 {
3686 tree ret = fold_convert (gfc_get_pchar_type (kind), str);
3687 ret = build_fold_indirect_ref_loc (input_location, ret);
3688 if (TREE_CODE (ret) == INTEGER_CST)
3689 {
3690 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
3691 int i, length = TREE_STRING_LENGTH (string_cst);
3692 const char *ptr = TREE_STRING_POINTER (string_cst);
3693
3694 for (i = 1; i < length; i++)
3695 if (ptr[i] != ' ')
3696 return NULL_TREE;
3697
3698 return ret;
3699 }
3700 }
3701
3702 return NULL_TREE;
3703 }
3704
3705
3706 void
3707 gfc_conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
3708 {
3709
3710 if (sym->backend_decl)
3711 {
3712 /* This becomes the nominal_type in
3713 function.c:assign_parm_find_data_types. */
3714 TREE_TYPE (sym->backend_decl) = unsigned_char_type_node;
3715 /* This becomes the passed_type in
3716 function.c:assign_parm_find_data_types. C promotes char to
3717 integer for argument passing. */
3718 DECL_ARG_TYPE (sym->backend_decl) = unsigned_type_node;
3719
3720 DECL_BY_REFERENCE (sym->backend_decl) = 0;
3721 }
3722
3723 if (expr != NULL)
3724 {
3725 /* If we have a constant character expression, make it into an
3726 integer. */
3727 if ((*expr)->expr_type == EXPR_CONSTANT)
3728 {
3729 gfc_typespec ts;
3730 gfc_clear_ts (&ts);
3731
3732 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL,
3733 (int)(*expr)->value.character.string[0]);
3734 if ((*expr)->ts.kind != gfc_c_int_kind)
3735 {
3736 /* The expr needs to be compatible with a C int. If the
3737 conversion fails, then the 2 causes an ICE. */
3738 ts.type = BT_INTEGER;
3739 ts.kind = gfc_c_int_kind;
3740 gfc_convert_type (*expr, &ts, 2);
3741 }
3742 }
3743 else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
3744 {
3745 if ((*expr)->ref == NULL)
3746 {
3747 se->expr = gfc_string_to_single_character
3748 (build_int_cst (integer_type_node, 1),
3749 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
3750 gfc_get_symbol_decl
3751 ((*expr)->symtree->n.sym)),
3752 (*expr)->ts.kind);
3753 }
3754 else
3755 {
3756 gfc_conv_variable (se, *expr);
3757 se->expr = gfc_string_to_single_character
3758 (build_int_cst (integer_type_node, 1),
3759 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
3760 se->expr),
3761 (*expr)->ts.kind);
3762 }
3763 }
3764 }
3765 }
3766
3767 /* Helper function for gfc_build_compare_string. Return LEN_TRIM value
3768 if STR is a string literal, otherwise return -1. */
3769
3770 static int
3771 gfc_optimize_len_trim (tree len, tree str, int kind)
3772 {
3773 if (kind == 1
3774 && TREE_CODE (str) == ADDR_EXPR
3775 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
3776 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
3777 && array_ref_low_bound (TREE_OPERAND (str, 0))
3778 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
3779 && tree_fits_uhwi_p (len)
3780 && tree_to_uhwi (len) >= 1
3781 && tree_to_uhwi (len)
3782 == (unsigned HOST_WIDE_INT)
3783 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
3784 {
3785 tree folded = fold_convert (gfc_get_pchar_type (kind), str);
3786 folded = build_fold_indirect_ref_loc (input_location, folded);
3787 if (TREE_CODE (folded) == INTEGER_CST)
3788 {
3789 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
3790 int length = TREE_STRING_LENGTH (string_cst);
3791 const char *ptr = TREE_STRING_POINTER (string_cst);
3792
3793 for (; length > 0; length--)
3794 if (ptr[length - 1] != ' ')
3795 break;
3796
3797 return length;
3798 }
3799 }
3800 return -1;
3801 }
3802
3803 /* Helper to build a call to memcmp. */
3804
3805 static tree
3806 build_memcmp_call (tree s1, tree s2, tree n)
3807 {
3808 tree tmp;
3809
3810 if (!POINTER_TYPE_P (TREE_TYPE (s1)))
3811 s1 = gfc_build_addr_expr (pvoid_type_node, s1);
3812 else
3813 s1 = fold_convert (pvoid_type_node, s1);
3814
3815 if (!POINTER_TYPE_P (TREE_TYPE (s2)))
3816 s2 = gfc_build_addr_expr (pvoid_type_node, s2);
3817 else
3818 s2 = fold_convert (pvoid_type_node, s2);
3819
3820 n = fold_convert (size_type_node, n);
3821
3822 tmp = build_call_expr_loc (input_location,
3823 builtin_decl_explicit (BUILT_IN_MEMCMP),
3824 3, s1, s2, n);
3825
3826 return fold_convert (integer_type_node, tmp);
3827 }
3828
3829 /* Compare two strings. If they are all single characters, the result is the
3830 subtraction of them. Otherwise, we build a library call. */
3831
3832 tree
3833 gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
3834 enum tree_code code)
3835 {
3836 tree sc1;
3837 tree sc2;
3838 tree fndecl;
3839
3840 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
3841 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
3842
3843 sc1 = gfc_string_to_single_character (len1, str1, kind);
3844 sc2 = gfc_string_to_single_character (len2, str2, kind);
3845
3846 if (sc1 != NULL_TREE && sc2 != NULL_TREE)
3847 {
3848 /* Deal with single character specially. */
3849 sc1 = fold_convert (integer_type_node, sc1);
3850 sc2 = fold_convert (integer_type_node, sc2);
3851 return fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
3852 sc1, sc2);
3853 }
3854
3855 if ((code == EQ_EXPR || code == NE_EXPR)
3856 && optimize
3857 && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
3858 {
3859 /* If one string is a string literal with LEN_TRIM longer
3860 than the length of the second string, the strings
3861 compare unequal. */
3862 int len = gfc_optimize_len_trim (len1, str1, kind);
3863 if (len > 0 && compare_tree_int (len2, len) < 0)
3864 return integer_one_node;
3865 len = gfc_optimize_len_trim (len2, str2, kind);
3866 if (len > 0 && compare_tree_int (len1, len) < 0)
3867 return integer_one_node;
3868 }
3869
3870 /* We can compare via memcpy if the strings are known to be equal
3871 in length and they are
3872 - kind=1
3873 - kind=4 and the comparison is for (in)equality. */
3874
3875 if (INTEGER_CST_P (len1) && INTEGER_CST_P (len2)
3876 && tree_int_cst_equal (len1, len2)
3877 && (kind == 1 || code == EQ_EXPR || code == NE_EXPR))
3878 {
3879 tree tmp;
3880 tree chartype;
3881
3882 chartype = gfc_get_char_type (kind);
3883 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE(len1),
3884 fold_convert (TREE_TYPE(len1),
3885 TYPE_SIZE_UNIT(chartype)),
3886 len1);
3887 return build_memcmp_call (str1, str2, tmp);
3888 }
3889
3890 /* Build a call for the comparison. */
3891 if (kind == 1)
3892 fndecl = gfor_fndecl_compare_string;
3893 else if (kind == 4)
3894 fndecl = gfor_fndecl_compare_string_char4;
3895 else
3896 gcc_unreachable ();
3897
3898 return build_call_expr_loc (input_location, fndecl, 4,
3899 len1, str1, len2, str2);
3900 }
3901
3902
3903 /* Return the backend_decl for a procedure pointer component. */
3904
3905 static tree
3906 get_proc_ptr_comp (gfc_expr *e)
3907 {
3908 gfc_se comp_se;
3909 gfc_expr *e2;
3910 expr_t old_type;
3911
3912 gfc_init_se (&comp_se, NULL);
3913 e2 = gfc_copy_expr (e);
3914 /* We have to restore the expr type later so that gfc_free_expr frees
3915 the exact same thing that was allocated.
3916 TODO: This is ugly. */
3917 old_type = e2->expr_type;
3918 e2->expr_type = EXPR_VARIABLE;
3919 gfc_conv_expr (&comp_se, e2);
3920 e2->expr_type = old_type;
3921 gfc_free_expr (e2);
3922 return build_fold_addr_expr_loc (input_location, comp_se.expr);
3923 }
3924
3925
3926 /* Convert a typebound function reference from a class object. */
3927 static void
3928 conv_base_obj_fcn_val (gfc_se * se, tree base_object, gfc_expr * expr)
3929 {
3930 gfc_ref *ref;
3931 tree var;
3932
3933 if (!VAR_P (base_object))
3934 {
3935 var = gfc_create_var (TREE_TYPE (base_object), NULL);
3936 gfc_add_modify (&se->pre, var, base_object);
3937 }
3938 se->expr = gfc_class_vptr_get (base_object);
3939 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
3940 ref = expr->ref;
3941 while (ref && ref->next)
3942 ref = ref->next;
3943 gcc_assert (ref && ref->type == REF_COMPONENT);
3944 if (ref->u.c.sym->attr.extension)
3945 conv_parent_component_references (se, ref);
3946 gfc_conv_component_ref (se, ref);
3947 se->expr = build_fold_addr_expr_loc (input_location, se->expr);
3948 }
3949
3950
3951 static void
3952 conv_function_val (gfc_se * se, gfc_symbol * sym, gfc_expr * expr,
3953 gfc_actual_arglist *actual_args)
3954 {
3955 tree tmp;
3956
3957 if (gfc_is_proc_ptr_comp (expr))
3958 tmp = get_proc_ptr_comp (expr);
3959 else if (sym->attr.dummy)
3960 {
3961 tmp = gfc_get_symbol_decl (sym);
3962 if (sym->attr.proc_pointer)
3963 tmp = build_fold_indirect_ref_loc (input_location,
3964 tmp);
3965 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
3966 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
3967 }
3968 else
3969 {
3970 if (!sym->backend_decl)
3971 sym->backend_decl = gfc_get_extern_function_decl (sym, actual_args);
3972
3973 TREE_USED (sym->backend_decl) = 1;
3974
3975 tmp = sym->backend_decl;
3976
3977 if (sym->attr.cray_pointee)
3978 {
3979 /* TODO - make the cray pointee a pointer to a procedure,
3980 assign the pointer to it and use it for the call. This
3981 will do for now! */
3982 tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
3983 gfc_get_symbol_decl (sym->cp_pointer));
3984 tmp = gfc_evaluate_now (tmp, &se->pre);
3985 }
3986
3987 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
3988 {
3989 gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
3990 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
3991 }
3992 }
3993 se->expr = tmp;
3994 }
3995
3996
3997 /* Initialize MAPPING. */
3998
3999 void
4000 gfc_init_interface_mapping (gfc_interface_mapping * mapping)
4001 {
4002 mapping->syms = NULL;
4003 mapping->charlens = NULL;
4004 }
4005
4006
4007 /* Free all memory held by MAPPING (but not MAPPING itself). */
4008
4009 void
4010 gfc_free_interface_mapping (gfc_interface_mapping * mapping)
4011 {
4012 gfc_interface_sym_mapping *sym;
4013 gfc_interface_sym_mapping *nextsym;
4014 gfc_charlen *cl;
4015 gfc_charlen *nextcl;
4016
4017 for (sym = mapping->syms; sym; sym = nextsym)
4018 {
4019 nextsym = sym->next;
4020 sym->new_sym->n.sym->formal = NULL;
4021 gfc_free_symbol (sym->new_sym->n.sym);
4022 gfc_free_expr (sym->expr);
4023 free (sym->new_sym);
4024 free (sym);
4025 }
4026 for (cl = mapping->charlens; cl; cl = nextcl)
4027 {
4028 nextcl = cl->next;
4029 gfc_free_expr (cl->length);
4030 free (cl);
4031 }
4032 }
4033
4034
4035 /* Return a copy of gfc_charlen CL. Add the returned structure to
4036 MAPPING so that it will be freed by gfc_free_interface_mapping. */
4037
4038 static gfc_charlen *
4039 gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
4040 gfc_charlen * cl)
4041 {
4042 gfc_charlen *new_charlen;
4043
4044 new_charlen = gfc_get_charlen ();
4045 new_charlen->next = mapping->charlens;
4046 new_charlen->length = gfc_copy_expr (cl->length);
4047
4048 mapping->charlens = new_charlen;
4049 return new_charlen;
4050 }
4051
4052
4053 /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
4054 array variable that can be used as the actual argument for dummy
4055 argument SYM. Add any initialization code to BLOCK. PACKED is as
4056 for gfc_get_nodesc_array_type and DATA points to the first element
4057 in the passed array. */
4058
4059 static tree
4060 gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
4061 gfc_packed packed, tree data)
4062 {
4063 tree type;
4064 tree var;
4065
4066 type = gfc_typenode_for_spec (&sym->ts);
4067 type = gfc_get_nodesc_array_type (type, sym->as, packed,
4068 !sym->attr.target && !sym->attr.pointer
4069 && !sym->attr.proc_pointer);
4070
4071 var = gfc_create_var (type, "ifm");
4072 gfc_add_modify (block, var, fold_convert (type, data));
4073
4074 return var;
4075 }
4076
4077
4078 /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
4079 and offset of descriptorless array type TYPE given that it has the same
4080 size as DESC. Add any set-up code to BLOCK. */
4081
4082 static void
4083 gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
4084 {
4085 int n;
4086 tree dim;
4087 tree offset;
4088 tree tmp;
4089
4090 offset = gfc_index_zero_node;
4091 for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
4092 {
4093 dim = gfc_rank_cst[n];
4094 GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
4095 if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
4096 {
4097 GFC_TYPE_ARRAY_LBOUND (type, n)
4098 = gfc_conv_descriptor_lbound_get (desc, dim);
4099 GFC_TYPE_ARRAY_UBOUND (type, n)
4100 = gfc_conv_descriptor_ubound_get (desc, dim);
4101 }
4102 else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
4103 {
4104 tmp = fold_build2_loc (input_location, MINUS_EXPR,
4105 gfc_array_index_type,
4106 gfc_conv_descriptor_ubound_get (desc, dim),
4107 gfc_conv_descriptor_lbound_get (desc, dim));
4108 tmp = fold_build2_loc (input_location, PLUS_EXPR,
4109 gfc_array_index_type,
4110 GFC_TYPE_ARRAY_LBOUND (type, n), tmp);
4111 tmp = gfc_evaluate_now (tmp, block);
4112 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
4113 }
4114 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
4115 GFC_TYPE_ARRAY_LBOUND (type, n),
4116 GFC_TYPE_ARRAY_STRIDE (type, n));
4117 offset = fold_build2_loc (input_location, MINUS_EXPR,
4118 gfc_array_index_type, offset, tmp);
4119 }
4120 offset = gfc_evaluate_now (offset, block);
4121 GFC_TYPE_ARRAY_OFFSET (type) = offset;
4122 }
4123
4124
4125 /* Extend MAPPING so that it maps dummy argument SYM to the value stored
4126 in SE. The caller may still use se->expr and se->string_length after
4127 calling this function. */
4128
4129 void
4130 gfc_add_interface_mapping (gfc_interface_mapping * mapping,
4131 gfc_symbol * sym, gfc_se * se,
4132 gfc_expr *expr)
4133 {
4134 gfc_interface_sym_mapping *sm;
4135 tree desc;
4136 tree tmp;
4137 tree value;
4138 gfc_symbol *new_sym;
4139 gfc_symtree *root;
4140 gfc_symtree *new_symtree;
4141
4142 /* Create a new symbol to represent the actual argument. */
4143 new_sym = gfc_new_symbol (sym->name, NULL);
4144 new_sym->ts = sym->ts;
4145 new_sym->as = gfc_copy_array_spec (sym->as);
4146 new_sym->attr.referenced = 1;
4147 new_sym->attr.dimension = sym->attr.dimension;
4148 new_sym->attr.contiguous = sym->attr.contiguous;
4149 new_sym->attr.codimension = sym->attr.codimension;
4150 new_sym->attr.pointer = sym->attr.pointer;
4151 new_sym->attr.allocatable = sym->attr.allocatable;
4152 new_sym->attr.flavor = sym->attr.flavor;
4153 new_sym->attr.function = sym->attr.function;
4154
4155 /* Ensure that the interface is available and that
4156 descriptors are passed for array actual arguments. */
4157 if (sym->attr.flavor == FL_PROCEDURE)
4158 {
4159 new_sym->formal = expr->symtree->n.sym->formal;
4160 new_sym->attr.always_explicit
4161 = expr->symtree->n.sym->attr.always_explicit;
4162 }
4163
4164 /* Create a fake symtree for it. */
4165 root = NULL;
4166 new_symtree = gfc_new_symtree (&root, sym->name);
4167 new_symtree->n.sym = new_sym;
4168 gcc_assert (new_symtree == root);
4169
4170 /* Create a dummy->actual mapping. */
4171 sm = XCNEW (gfc_interface_sym_mapping);
4172 sm->next = mapping->syms;
4173 sm->old = sym;
4174 sm->new_sym = new_symtree;
4175 sm->expr = gfc_copy_expr (expr);
4176 mapping->syms = sm;
4177
4178 /* Stabilize the argument's value. */
4179 if (!sym->attr.function && se)
4180 se->expr = gfc_evaluate_now (se->expr, &se->pre);
4181
4182 if (sym->ts.type == BT_CHARACTER)
4183 {
4184 /* Create a copy of the dummy argument's length. */
4185 new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
4186 sm->expr->ts.u.cl = new_sym->ts.u.cl;
4187
4188 /* If the length is specified as "*", record the length that
4189 the caller is passing. We should use the callee's length
4190 in all other cases. */
4191 if (!new_sym->ts.u.cl->length && se)
4192 {
4193 se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
4194 new_sym->ts.u.cl->backend_decl = se->string_length;
4195 }
4196 }
4197
4198 if (!se)
4199 return;
4200
4201 /* Use the passed value as-is if the argument is a function. */
4202 if (sym->attr.flavor == FL_PROCEDURE)
4203 value = se->expr;
4204
4205 /* If the argument is a pass-by-value scalar, use the value as is. */
4206 else if (!sym->attr.dimension && sym->attr.value)
4207 value = se->expr;
4208
4209 /* If the argument is either a string or a pointer to a string,
4210 convert it to a boundless character type. */
4211 else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
4212 {
4213 tmp = gfc_get_character_type_len (sym->ts.kind, NULL);
4214 tmp = build_pointer_type (tmp);
4215 if (sym->attr.pointer)
4216 value = build_fold_indirect_ref_loc (input_location,
4217 se->expr);
4218 else
4219 value = se->expr;
4220 value = fold_convert (tmp, value);
4221 }
4222
4223 /* If the argument is a scalar, a pointer to an array or an allocatable,
4224 dereference it. */
4225 else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
4226 value = build_fold_indirect_ref_loc (input_location,
4227 se->expr);
4228
4229 /* For character(*), use the actual argument's descriptor. */
4230 else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
4231 value = build_fold_indirect_ref_loc (input_location,
4232 se->expr);
4233
4234 /* If the argument is an array descriptor, use it to determine
4235 information about the actual argument's shape. */
4236 else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
4237 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
4238 {
4239 /* Get the actual argument's descriptor. */
4240 desc = build_fold_indirect_ref_loc (input_location,
4241 se->expr);
4242
4243 /* Create the replacement variable. */
4244 tmp = gfc_conv_descriptor_data_get (desc);
4245 value = gfc_get_interface_mapping_array (&se->pre, sym,
4246 PACKED_NO, tmp);
4247
4248 /* Use DESC to work out the upper bounds, strides and offset. */
4249 gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
4250 }
4251 else
4252 /* Otherwise we have a packed array. */
4253 value = gfc_get_interface_mapping_array (&se->pre, sym,
4254 PACKED_FULL, se->expr);
4255
4256 new_sym->backend_decl = value;
4257 }
4258
4259
4260 /* Called once all dummy argument mappings have been added to MAPPING,
4261 but before the mapping is used to evaluate expressions. Pre-evaluate
4262 the length of each argument, adding any initialization code to PRE and
4263 any finalization code to POST. */
4264
4265 void
4266 gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
4267 stmtblock_t * pre, stmtblock_t * post)
4268 {
4269 gfc_interface_sym_mapping *sym;
4270 gfc_expr *expr;
4271 gfc_se se;
4272
4273 for (sym = mapping->syms; sym; sym = sym->next)
4274 if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
4275 && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
4276 {
4277 expr = sym->new_sym->n.sym->ts.u.cl->length;
4278 gfc_apply_interface_mapping_to_expr (mapping, expr);
4279 gfc_init_se (&se, NULL);
4280 gfc_conv_expr (&se, expr);
4281 se.expr = fold_convert (gfc_charlen_type_node, se.expr);
4282 se.expr = gfc_evaluate_now (se.expr, &se.pre);
4283 gfc_add_block_to_block (pre, &se.pre);
4284 gfc_add_block_to_block (post, &se.post);
4285
4286 sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
4287 }
4288 }
4289
4290
4291 /* Like gfc_apply_interface_mapping_to_expr, but applied to
4292 constructor C. */
4293
4294 static void
4295 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
4296 gfc_constructor_base base)
4297 {
4298 gfc_constructor *c;
4299 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
4300 {
4301 gfc_apply_interface_mapping_to_expr (mapping, c->expr);
4302 if (c->iterator)
4303 {
4304 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
4305 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
4306 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
4307 }
4308 }
4309 }
4310
4311
4312 /* Like gfc_apply_interface_mapping_to_expr, but applied to
4313 reference REF. */
4314
4315 static void
4316 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
4317 gfc_ref * ref)
4318 {
4319 int n;
4320
4321 for (; ref; ref = ref->next)
4322 switch (ref->type)
4323 {
4324 case REF_ARRAY:
4325 for (n = 0; n < ref->u.ar.dimen; n++)
4326 {
4327 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
4328 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
4329 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
4330 }
4331 break;
4332
4333 case REF_COMPONENT:
4334 case REF_INQUIRY:
4335 break;
4336
4337 case REF_SUBSTRING:
4338 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
4339 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
4340 break;
4341 }
4342 }
4343
4344
4345 /* Convert intrinsic function calls into result expressions. */
4346
4347 static bool
4348 gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
4349 {
4350 gfc_symbol *sym;
4351 gfc_expr *new_expr;
4352 gfc_expr *arg1;
4353 gfc_expr *arg2;
4354 int d, dup;
4355
4356 arg1 = expr->value.function.actual->expr;
4357 if (expr->value.function.actual->next)
4358 arg2 = expr->value.function.actual->next->expr;
4359 else
4360 arg2 = NULL;
4361
4362 sym = arg1->symtree->n.sym;
4363
4364 if (sym->attr.dummy)
4365 return false;
4366
4367 new_expr = NULL;
4368
4369 switch (expr->value.function.isym->id)
4370 {
4371 case GFC_ISYM_LEN:
4372 /* TODO figure out why this condition is necessary. */
4373 if (sym->attr.function
4374 && (arg1->ts.u.cl->length == NULL
4375 || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
4376 && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
4377 return false;
4378
4379 new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
4380 break;
4381
4382 case GFC_ISYM_LEN_TRIM:
4383 new_expr = gfc_copy_expr (arg1);
4384 gfc_apply_interface_mapping_to_expr (mapping, new_expr);
4385
4386 if (!new_expr)
4387 return false;
4388
4389 gfc_replace_expr (arg1, new_expr);
4390 return true;
4391
4392 case GFC_ISYM_SIZE:
4393 if (!sym->as || sym->as->rank == 0)
4394 return false;
4395
4396 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
4397 {
4398 dup = mpz_get_si (arg2->value.integer);
4399 d = dup - 1;
4400 }
4401 else
4402 {
4403 dup = sym->as->rank;
4404 d = 0;
4405 }
4406
4407 for (; d < dup; d++)
4408 {
4409 gfc_expr *tmp;
4410
4411 if (!sym->as->upper[d] || !sym->as->lower[d])
4412 {
4413 gfc_free_expr (new_expr);
4414 return false;
4415 }
4416
4417 tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
4418 gfc_get_int_expr (gfc_default_integer_kind,
4419 NULL, 1));
4420 tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
4421 if (new_expr)
4422 new_expr = gfc_multiply (new_expr, tmp);
4423 else
4424 new_expr = tmp;
4425 }
4426 break;
4427
4428 case GFC_ISYM_LBOUND:
4429 case GFC_ISYM_UBOUND:
4430 /* TODO These implementations of lbound and ubound do not limit if
4431 the size < 0, according to F95's 13.14.53 and 13.14.113. */
4432
4433 if (!sym->as || sym->as->rank == 0)
4434 return false;
4435
4436 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
4437 d = mpz_get_si (arg2->value.integer) - 1;
4438 else
4439 return false;
4440
4441 if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
4442 {
4443 if (sym->as->lower[d])
4444 new_expr = gfc_copy_expr (sym->as->lower[d]);
4445 }
4446 else
4447 {
4448 if (sym->as->upper[d])
4449 new_expr = gfc_copy_expr (sym->as->upper[d]);
4450 }
4451 break;
4452
4453 default:
4454 break;
4455 }
4456
4457 gfc_apply_interface_mapping_to_expr (mapping, new_expr);
4458 if (!new_expr)
4459 return false;
4460
4461 gfc_replace_expr (expr, new_expr);
4462 return true;
4463 }
4464
4465
4466 static void
4467 gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
4468 gfc_interface_mapping * mapping)
4469 {
4470 gfc_formal_arglist *f;
4471 gfc_actual_arglist *actual;
4472
4473 actual = expr->value.function.actual;
4474 f = gfc_sym_get_dummy_args (map_expr->symtree->n.sym);
4475
4476 for (; f && actual; f = f->next, actual = actual->next)
4477 {
4478 if (!actual->expr)
4479 continue;
4480
4481 gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
4482 }
4483
4484 if (map_expr->symtree->n.sym->attr.dimension)
4485 {
4486 int d;
4487 gfc_array_spec *as;
4488
4489 as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
4490
4491 for (d = 0; d < as->rank; d++)
4492 {
4493 gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
4494 gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
4495 }
4496
4497 expr->value.function.esym->as = as;
4498 }
4499
4500 if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
4501 {
4502 expr->value.function.esym->ts.u.cl->length
4503 = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
4504
4505 gfc_apply_interface_mapping_to_expr (mapping,
4506 expr->value.function.esym->ts.u.cl->length);
4507 }
4508 }
4509
4510
4511 /* EXPR is a copy of an expression that appeared in the interface
4512 associated with MAPPING. Walk it recursively looking for references to
4513 dummy arguments that MAPPING maps to actual arguments. Replace each such
4514 reference with a reference to the associated actual argument. */
4515
4516 static void
4517 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
4518 gfc_expr * expr)
4519 {
4520 gfc_interface_sym_mapping *sym;
4521 gfc_actual_arglist *actual;
4522
4523 if (!expr)
4524 return;
4525
4526 /* Copying an expression does not copy its length, so do that here. */
4527 if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
4528 {
4529 expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
4530 gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
4531 }
4532
4533 /* Apply the mapping to any references. */
4534 gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
4535
4536 /* ...and to the expression's symbol, if it has one. */
4537 /* TODO Find out why the condition on expr->symtree had to be moved into
4538 the loop rather than being outside it, as originally. */
4539 for (sym = mapping->syms; sym; sym = sym->next)
4540 if (expr->symtree && sym->old == expr->symtree->n.sym)
4541 {
4542 if (sym->new_sym->n.sym->backend_decl)
4543 expr->symtree = sym->new_sym;
4544 else if (sym->expr)
4545 gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
4546 }
4547
4548 /* ...and to subexpressions in expr->value. */
4549 switch (expr->expr_type)
4550 {
4551 case EXPR_VARIABLE:
4552 case EXPR_CONSTANT:
4553 case EXPR_NULL:
4554 case EXPR_SUBSTRING:
4555 break;
4556
4557 case EXPR_OP:
4558 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
4559 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
4560 break;
4561
4562 case EXPR_FUNCTION:
4563 for (actual = expr->value.function.actual; actual; actual = actual->next)
4564 gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
4565
4566 if (expr->value.function.esym == NULL
4567 && expr->value.function.isym != NULL
4568 && expr->value.function.actual
4569 && expr->value.function.actual->expr
4570 && expr->value.function.actual->expr->symtree
4571 && gfc_map_intrinsic_function (expr, mapping))
4572 break;
4573
4574 for (sym = mapping->syms; sym; sym = sym->next)
4575 if (sym->old == expr->value.function.esym)
4576 {
4577 expr->value.function.esym = sym->new_sym->n.sym;
4578 gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
4579 expr->value.function.esym->result = sym->new_sym->n.sym;
4580 }
4581 break;
4582
4583 case EXPR_ARRAY:
4584 case EXPR_STRUCTURE:
4585 gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
4586 break;
4587
4588 case EXPR_COMPCALL:
4589 case EXPR_PPC:
4590 case EXPR_UNKNOWN:
4591 gcc_unreachable ();
4592 break;
4593 }
4594
4595 return;
4596 }
4597
4598
4599 /* Evaluate interface expression EXPR using MAPPING. Store the result
4600 in SE. */
4601
4602 void
4603 gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
4604 gfc_se * se, gfc_expr * expr)
4605 {
4606 expr = gfc_copy_expr (expr);
4607 gfc_apply_interface_mapping_to_expr (mapping, expr);
4608 gfc_conv_expr (se, expr);
4609 se->expr = gfc_evaluate_now (se->expr, &se->pre);
4610 gfc_free_expr (expr);
4611 }
4612
4613
4614 /* Returns a reference to a temporary array into which a component of
4615 an actual argument derived type array is copied and then returned
4616 after the function call. */
4617 void
4618 gfc_conv_subref_array_arg (gfc_se *se, gfc_expr * expr, int g77,
4619 sym_intent intent, bool formal_ptr,
4620 const gfc_symbol *fsym, const char *proc_name,
4621 gfc_symbol *sym, bool check_contiguous)
4622 {
4623 gfc_se lse;
4624 gfc_se rse;
4625 gfc_ss *lss;
4626 gfc_ss *rss;
4627 gfc_loopinfo loop;
4628 gfc_loopinfo loop2;
4629 gfc_array_info *info;
4630 tree offset;
4631 tree tmp_index;
4632 tree tmp;
4633 tree base_type;
4634 tree size;
4635 stmtblock_t body;
4636 int n;
4637 int dimen;
4638 gfc_se work_se;
4639 gfc_se *parmse;
4640 bool pass_optional;
4641
4642 pass_optional = fsym && fsym->attr.optional && sym && sym->attr.optional;
4643
4644 if (pass_optional || check_contiguous)
4645 {
4646 gfc_init_se (&work_se, NULL);
4647 parmse = &work_se;
4648 }
4649 else
4650 parmse = se;
4651
4652 if (gfc_option.rtcheck & GFC_RTCHECK_ARRAY_TEMPS)
4653 {
4654 /* We will create a temporary array, so let us warn. */
4655 char * msg;
4656
4657 if (fsym && proc_name)
4658 msg = xasprintf ("An array temporary was created for argument "
4659 "'%s' of procedure '%s'", fsym->name, proc_name);
4660 else
4661 msg = xasprintf ("An array temporary was created");
4662
4663 tmp = build_int_cst (logical_type_node, 1);
4664 gfc_trans_runtime_check (false, true, tmp, &parmse->pre,
4665 &expr->where, msg);
4666 free (msg);
4667 }
4668
4669 gfc_init_se (&lse, NULL);
4670 gfc_init_se (&rse, NULL);
4671
4672 /* Walk the argument expression. */
4673 rss = gfc_walk_expr (expr);
4674
4675 gcc_assert (rss != gfc_ss_terminator);
4676
4677 /* Initialize the scalarizer. */
4678 gfc_init_loopinfo (&loop);
4679 gfc_add_ss_to_loop (&loop, rss);
4680
4681 /* Calculate the bounds of the scalarization. */
4682 gfc_conv_ss_startstride (&loop);
4683
4684 /* Build an ss for the temporary. */
4685 if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
4686 gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
4687
4688 base_type = gfc_typenode_for_spec (&expr->ts);
4689 if (GFC_ARRAY_TYPE_P (base_type)
4690 || GFC_DESCRIPTOR_TYPE_P (base_type))
4691 base_type = gfc_get_element_type (base_type);
4692
4693 if (expr->ts.type == BT_CLASS)
4694 base_type = gfc_typenode_for_spec (&CLASS_DATA (expr)->ts);
4695
4696 loop.temp_ss = gfc_get_temp_ss (base_type, ((expr->ts.type == BT_CHARACTER)
4697 ? expr->ts.u.cl->backend_decl
4698 : NULL),
4699 loop.dimen);
4700
4701 parmse->string_length = loop.temp_ss->info->string_length;
4702
4703 /* Associate the SS with the loop. */
4704 gfc_add_ss_to_loop (&loop, loop.temp_ss);
4705
4706 /* Setup the scalarizing loops. */
4707 gfc_conv_loop_setup (&loop, &expr->where);
4708
4709 /* Pass the temporary descriptor back to the caller. */
4710 info = &loop.temp_ss->info->data.array;
4711 parmse->expr = info->descriptor;
4712
4713 /* Setup the gfc_se structures. */
4714 gfc_copy_loopinfo_to_se (&lse, &loop);
4715 gfc_copy_loopinfo_to_se (&rse, &loop);
4716
4717 rse.ss = rss;
4718 lse.ss = loop.temp_ss;
4719 gfc_mark_ss_chain_used (rss, 1);
4720 gfc_mark_ss_chain_used (loop.temp_ss, 1);
4721
4722 /* Start the scalarized loop body. */
4723 gfc_start_scalarized_body (&loop, &body);
4724
4725 /* Translate the expression. */
4726 gfc_conv_expr (&rse, expr);
4727
4728 /* Reset the offset for the function call since the loop
4729 is zero based on the data pointer. Note that the temp
4730 comes first in the loop chain since it is added second. */
4731 if (gfc_is_class_array_function (expr))
4732 {
4733 tmp = loop.ss->loop_chain->info->data.array.descriptor;
4734 gfc_conv_descriptor_offset_set (&loop.pre, tmp,
4735 gfc_index_zero_node);
4736 }
4737
4738 gfc_conv_tmp_array_ref (&lse);
4739
4740 if (intent != INTENT_OUT)
4741 {
4742 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false);
4743 gfc_add_expr_to_block (&body, tmp);
4744 gcc_assert (rse.ss == gfc_ss_terminator);
4745 gfc_trans_scalarizing_loops (&loop, &body);
4746 }
4747 else
4748 {
4749 /* Make sure that the temporary declaration survives by merging
4750 all the loop declarations into the current context. */
4751 for (n = 0; n < loop.dimen; n++)
4752 {
4753 gfc_merge_block_scope (&body);
4754 body = loop.code[loop.order[n]];
4755 }
4756 gfc_merge_block_scope (&body);
4757 }
4758
4759 /* Add the post block after the second loop, so that any
4760 freeing of allocated memory is done at the right time. */
4761 gfc_add_block_to_block (&parmse->pre, &loop.pre);
4762
4763 /**********Copy the temporary back again.*********/
4764
4765 gfc_init_se (&lse, NULL);
4766 gfc_init_se (&rse, NULL);
4767
4768 /* Walk the argument expression. */
4769 lss = gfc_walk_expr (expr);
4770 rse.ss = loop.temp_ss;
4771 lse.ss = lss;
4772
4773 /* Initialize the scalarizer. */
4774 gfc_init_loopinfo (&loop2);
4775 gfc_add_ss_to_loop (&loop2, lss);
4776
4777 dimen = rse.ss->dimen;
4778
4779 /* Skip the write-out loop for this case. */
4780 if (gfc_is_class_array_function (expr))
4781 goto class_array_fcn;
4782
4783 /* Calculate the bounds of the scalarization. */
4784 gfc_conv_ss_startstride (&loop2);
4785
4786 /* Setup the scalarizing loops. */
4787 gfc_conv_loop_setup (&loop2, &expr->where);
4788
4789 gfc_copy_loopinfo_to_se (&lse, &loop2);
4790 gfc_copy_loopinfo_to_se (&rse, &loop2);
4791
4792 gfc_mark_ss_chain_used (lss, 1);
4793 gfc_mark_ss_chain_used (loop.temp_ss, 1);
4794
4795 /* Declare the variable to hold the temporary offset and start the
4796 scalarized loop body. */
4797 offset = gfc_create_var (gfc_array_index_type, NULL);
4798 gfc_start_scalarized_body (&loop2, &body);
4799
4800 /* Build the offsets for the temporary from the loop variables. The
4801 temporary array has lbounds of zero and strides of one in all
4802 dimensions, so this is very simple. The offset is only computed
4803 outside the innermost loop, so the overall transfer could be
4804 optimized further. */
4805 info = &rse.ss->info->data.array;
4806
4807 tmp_index = gfc_index_zero_node;
4808 for (n = dimen - 1; n > 0; n--)
4809 {
4810 tree tmp_str;
4811 tmp = rse.loop->loopvar[n];
4812 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
4813 tmp, rse.loop->from[n]);
4814 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4815 tmp, tmp_index);
4816
4817 tmp_str = fold_build2_loc (input_location, MINUS_EXPR,
4818 gfc_array_index_type,
4819 rse.loop->to[n-1], rse.loop->from[n-1]);
4820 tmp_str = fold_build2_loc (input_location, PLUS_EXPR,
4821 gfc_array_index_type,
4822 tmp_str, gfc_index_one_node);
4823
4824 tmp_index = fold_build2_loc (input_location, MULT_EXPR,
4825 gfc_array_index_type, tmp, tmp_str);
4826 }
4827
4828 tmp_index = fold_build2_loc (input_location, MINUS_EXPR,
4829 gfc_array_index_type,
4830 tmp_index, rse.loop->from[0]);
4831 gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
4832
4833 tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
4834 gfc_array_index_type,
4835 rse.loop->loopvar[0], offset);
4836
4837 /* Now use the offset for the reference. */
4838 tmp = build_fold_indirect_ref_loc (input_location,
4839 info->data);
4840 rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
4841
4842 if (expr->ts.type == BT_CHARACTER)
4843 rse.string_length = expr->ts.u.cl->backend_decl;
4844
4845 gfc_conv_expr (&lse, expr);
4846
4847 gcc_assert (lse.ss == gfc_ss_terminator);
4848
4849 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, true);
4850 gfc_add_expr_to_block (&body, tmp);
4851
4852 /* Generate the copying loops. */
4853 gfc_trans_scalarizing_loops (&loop2, &body);
4854
4855 /* Wrap the whole thing up by adding the second loop to the post-block
4856 and following it by the post-block of the first loop. In this way,
4857 if the temporary needs freeing, it is done after use! */
4858 if (intent != INTENT_IN)
4859 {
4860 gfc_add_block_to_block (&parmse->post, &loop2.pre);
4861 gfc_add_block_to_block (&parmse->post, &loop2.post);
4862 }
4863
4864 class_array_fcn:
4865
4866 gfc_add_block_to_block (&parmse->post, &loop.post);
4867
4868 gfc_cleanup_loop (&loop);
4869 gfc_cleanup_loop (&loop2);
4870
4871 /* Pass the string length to the argument expression. */
4872 if (expr->ts.type == BT_CHARACTER)
4873 parmse->string_length = expr->ts.u.cl->backend_decl;
4874
4875 /* Determine the offset for pointer formal arguments and set the
4876 lbounds to one. */
4877 if (formal_ptr)
4878 {
4879 size = gfc_index_one_node;
4880 offset = gfc_index_zero_node;
4881 for (n = 0; n < dimen; n++)
4882 {
4883 tmp = gfc_conv_descriptor_ubound_get (parmse->expr,
4884 gfc_rank_cst[n]);
4885 tmp = fold_build2_loc (input_location, PLUS_EXPR,
4886 gfc_array_index_type, tmp,
4887 gfc_index_one_node);
4888 gfc_conv_descriptor_ubound_set (&parmse->pre,
4889 parmse->expr,
4890 gfc_rank_cst[n],
4891 tmp);
4892 gfc_conv_descriptor_lbound_set (&parmse->pre,
4893 parmse->expr,
4894 gfc_rank_cst[n],
4895 gfc_index_one_node);
4896 size = gfc_evaluate_now (size, &parmse->pre);
4897 offset = fold_build2_loc (input_location, MINUS_EXPR,
4898 gfc_array_index_type,
4899 offset, size);
4900 offset = gfc_evaluate_now (offset, &parmse->pre);
4901 tmp = fold_build2_loc (input_location, MINUS_EXPR,
4902 gfc_array_index_type,
4903 rse.loop->to[n], rse.loop->from[n]);
4904 tmp = fold_build2_loc (input_location, PLUS_EXPR,
4905 gfc_array_index_type,
4906 tmp, gfc_index_one_node);
4907 size = fold_build2_loc (input_location, MULT_EXPR,
4908 gfc_array_index_type, size, tmp);
4909 }
4910
4911 gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
4912 offset);
4913 }
4914
4915 /* We want either the address for the data or the address of the descriptor,
4916 depending on the mode of passing array arguments. */
4917 if (g77)
4918 parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
4919 else
4920 parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
4921
4922 /* Basically make this into
4923
4924 if (present)
4925 {
4926 if (contiguous)
4927 {
4928 pointer = a;
4929 }
4930 else
4931 {
4932 parmse->pre();
4933 pointer = parmse->expr;
4934 }
4935 }
4936 else
4937 pointer = NULL;
4938
4939 foo (pointer);
4940 if (present && !contiguous)
4941 se->post();
4942
4943 */
4944
4945 if (pass_optional || check_contiguous)
4946 {
4947 tree type;
4948 stmtblock_t else_block;
4949 tree pre_stmts, post_stmts;
4950 tree pointer;
4951 tree else_stmt;
4952 tree present_var = NULL_TREE;
4953 tree cont_var = NULL_TREE;
4954 tree post_cond;
4955
4956 type = TREE_TYPE (parmse->expr);
4957 pointer = gfc_create_var (type, "arg_ptr");
4958
4959 if (check_contiguous)
4960 {
4961 gfc_se cont_se, array_se;
4962 stmtblock_t if_block, else_block;
4963 tree if_stmt, else_stmt;
4964 mpz_t size;
4965 bool size_set;
4966
4967 cont_var = gfc_create_var (boolean_type_node, "contiguous");
4968
4969 /* If the size is known to be one at compile-time, set
4970 cont_var to true unconditionally. This may look
4971 inelegant, but we're only doing this during
4972 optimization, so the statements will be optimized away,
4973 and this saves complexity here. */
4974
4975 size_set = gfc_array_size (expr, &size);
4976 if (size_set && mpz_cmp_ui (size, 1) == 0)
4977 {
4978 gfc_add_modify (&se->pre, cont_var,
4979 build_one_cst (boolean_type_node));
4980 }
4981 else
4982 {
4983 /* cont_var = is_contiguous (expr); . */
4984 gfc_init_se (&cont_se, parmse);
4985 gfc_conv_is_contiguous_expr (&cont_se, expr);
4986 gfc_add_block_to_block (&se->pre, &(&cont_se)->pre);
4987 gfc_add_modify (&se->pre, cont_var, cont_se.expr);
4988 gfc_add_block_to_block (&se->pre, &(&cont_se)->post);
4989 }
4990
4991 if (size_set)
4992 mpz_clear (size);
4993
4994 /* arrayse->expr = descriptor of a. */
4995 gfc_init_se (&array_se, se);
4996 gfc_conv_expr_descriptor (&array_se, expr);
4997 gfc_add_block_to_block (&se->pre, &(&array_se)->pre);
4998 gfc_add_block_to_block (&se->pre, &(&array_se)->post);
4999
5000 /* if_stmt = { pointer = &a[0]; } . */
5001 gfc_init_block (&if_block);
5002 tmp = gfc_conv_array_data (array_se.expr);
5003 tmp = fold_convert (type, tmp);
5004 gfc_add_modify (&if_block, pointer, tmp);
5005 if_stmt = gfc_finish_block (&if_block);
5006
5007 /* else_stmt = { parmse->pre(); pointer = parmse->expr; } . */
5008 gfc_init_block (&else_block);
5009 gfc_add_block_to_block (&else_block, &parmse->pre);
5010 gfc_add_modify (&else_block, pointer, parmse->expr);
5011 else_stmt = gfc_finish_block (&else_block);
5012
5013 /* And put the above into an if statement. */
5014 pre_stmts = fold_build3_loc (input_location, COND_EXPR, void_type_node,
5015 gfc_likely (cont_var,
5016 PRED_FORTRAN_CONTIGUOUS),
5017 if_stmt, else_stmt);
5018 }
5019 else
5020 {
5021 /* pointer = pramse->expr; . */
5022 gfc_add_modify (&parmse->pre, pointer, parmse->expr);
5023 pre_stmts = gfc_finish_block (&parmse->pre);
5024 }
5025
5026 if (pass_optional)
5027 {
5028 present_var = gfc_create_var (boolean_type_node, "present");
5029
5030 /* present_var = present(sym); . */
5031 tmp = gfc_conv_expr_present (sym);
5032 tmp = fold_convert (boolean_type_node, tmp);
5033 gfc_add_modify (&se->pre, present_var, tmp);
5034
5035 /* else_stmt = { pointer = NULL; } . */
5036 gfc_init_block (&else_block);
5037 gfc_add_modify (&else_block, pointer, build_int_cst (type, 0));
5038 else_stmt = gfc_finish_block (&else_block);
5039
5040 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
5041 gfc_likely (present_var,
5042 PRED_FORTRAN_ABSENT_DUMMY),
5043 pre_stmts, else_stmt);
5044 gfc_add_expr_to_block (&se->pre, tmp);
5045 }
5046 else
5047 gfc_add_expr_to_block (&se->pre, pre_stmts);
5048
5049 post_stmts = gfc_finish_block (&parmse->post);
5050
5051 /* Put together the post stuff, plus the optional
5052 deallocation. */
5053 if (check_contiguous)
5054 {
5055 /* !cont_var. */
5056 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
5057 cont_var,
5058 build_zero_cst (boolean_type_node));
5059 tmp = gfc_unlikely (tmp, PRED_FORTRAN_CONTIGUOUS);
5060
5061 if (pass_optional)
5062 {
5063 tree present_likely = gfc_likely (present_var,
5064 PRED_FORTRAN_ABSENT_DUMMY);
5065 post_cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
5066 boolean_type_node, present_likely,
5067 tmp);
5068 }
5069 else
5070 post_cond = tmp;
5071 }
5072 else
5073 {
5074 gcc_assert (pass_optional);
5075 post_cond = present_var;
5076 }
5077
5078 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, post_cond,
5079 post_stmts, build_empty_stmt (input_location));
5080 gfc_add_expr_to_block (&se->post, tmp);
5081 se->expr = pointer;
5082 }
5083
5084 return;
5085 }
5086
5087
5088 /* Generate the code for argument list functions. */
5089
5090 static void
5091 conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
5092 {
5093 /* Pass by value for g77 %VAL(arg), pass the address
5094 indirectly for %LOC, else by reference. Thus %REF
5095 is a "do-nothing" and %LOC is the same as an F95
5096 pointer. */
5097 if (strcmp (name, "%VAL") == 0)
5098 gfc_conv_expr (se, expr);
5099 else if (strcmp (name, "%LOC") == 0)
5100 {
5101 gfc_conv_expr_reference (se, expr);
5102 se->expr = gfc_build_addr_expr (NULL, se->expr);
5103 }
5104 else if (strcmp (name, "%REF") == 0)
5105 gfc_conv_expr_reference (se, expr);
5106 else
5107 gfc_error ("Unknown argument list function at %L", &expr->where);
5108 }
5109
5110
5111 /* This function tells whether the middle-end representation of the expression
5112 E given as input may point to data otherwise accessible through a variable
5113 (sub-)reference.
5114 It is assumed that the only expressions that may alias are variables,
5115 and array constructors if ARRAY_MAY_ALIAS is true and some of its elements
5116 may alias.
5117 This function is used to decide whether freeing an expression's allocatable
5118 components is safe or should be avoided.
5119
5120 If ARRAY_MAY_ALIAS is true, an array constructor may alias if some of
5121 its elements are copied from a variable. This ARRAY_MAY_ALIAS trick
5122 is necessary because for array constructors, aliasing depends on how
5123 the array is used:
5124 - If E is an array constructor used as argument to an elemental procedure,
5125 the array, which is generated through shallow copy by the scalarizer,
5126 is used directly and can alias the expressions it was copied from.
5127 - If E is an array constructor used as argument to a non-elemental
5128 procedure,the scalarizer is used in gfc_conv_expr_descriptor to generate
5129 the array as in the previous case, but then that array is used
5130 to initialize a new descriptor through deep copy. There is no alias
5131 possible in that case.
5132 Thus, the ARRAY_MAY_ALIAS flag is necessary to distinguish the two cases
5133 above. */
5134
5135 static bool
5136 expr_may_alias_variables (gfc_expr *e, bool array_may_alias)
5137 {
5138 gfc_constructor *c;
5139
5140 if (e->expr_type == EXPR_VARIABLE)
5141 return true;
5142 else if (e->expr_type == EXPR_FUNCTION)
5143 {
5144 gfc_symbol *proc_ifc = gfc_get_proc_ifc_for_expr (e);
5145
5146 if (proc_ifc->result != NULL
5147 && ((proc_ifc->result->ts.type == BT_CLASS
5148 && proc_ifc->result->ts.u.derived->attr.is_class
5149 && CLASS_DATA (proc_ifc->result)->attr.class_pointer)
5150 || proc_ifc->result->attr.pointer))
5151 return true;
5152 else
5153 return false;
5154 }
5155 else if (e->expr_type != EXPR_ARRAY || !array_may_alias)
5156 return false;
5157
5158 for (c = gfc_constructor_first (e->value.constructor);
5159 c; c = gfc_constructor_next (c))
5160 if (c->expr
5161 && expr_may_alias_variables (c->expr, array_may_alias))
5162 return true;
5163
5164 return false;
5165 }
5166
5167
5168 /* A helper function to set the dtype for unallocated or unassociated
5169 entities. */
5170
5171 static void
5172 set_dtype_for_unallocated (gfc_se *parmse, gfc_expr *e)
5173 {
5174 tree tmp;
5175 tree desc;
5176 tree cond;
5177 tree type;
5178 stmtblock_t block;
5179
5180 /* TODO Figure out how to handle optional dummies. */
5181 if (e && e->expr_type == EXPR_VARIABLE
5182 && e->symtree->n.sym->attr.optional)
5183 return;
5184
5185 desc = parmse->expr;
5186 if (desc == NULL_TREE)
5187 return;
5188
5189 if (POINTER_TYPE_P (TREE_TYPE (desc)))
5190 desc = build_fold_indirect_ref_loc (input_location, desc);
5191
5192 if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
5193 return;
5194
5195 gfc_init_block (&block);
5196 tmp = gfc_conv_descriptor_data_get (desc);
5197 cond = fold_build2_loc (input_location, EQ_EXPR,
5198 logical_type_node, tmp,
5199 build_int_cst (TREE_TYPE (tmp), 0));
5200 tmp = gfc_conv_descriptor_dtype (desc);
5201 type = gfc_get_element_type (TREE_TYPE (desc));
5202 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
5203 TREE_TYPE (tmp), tmp,
5204 gfc_get_dtype_rank_type (e->rank, type));
5205 gfc_add_expr_to_block (&block, tmp);
5206 cond = build3_v (COND_EXPR, cond,
5207 gfc_finish_block (&block),
5208 build_empty_stmt (input_location));
5209 gfc_add_expr_to_block (&parmse->pre, cond);
5210 }
5211
5212
5213
5214 /* Provide an interface between gfortran array descriptors and the F2018:18.4
5215 ISO_Fortran_binding array descriptors. */
5216
5217 static void
5218 gfc_conv_gfc_desc_to_cfi_desc (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym)
5219 {
5220 tree tmp;
5221 tree cfi_desc_ptr;
5222 tree gfc_desc_ptr;
5223 tree type;
5224 tree cond;
5225 tree desc_attr;
5226 int attribute;
5227 int cfi_attribute;
5228 symbol_attribute attr = gfc_expr_attr (e);
5229
5230 /* If this is a full array or a scalar, the allocatable and pointer
5231 attributes can be passed. Otherwise it is 'CFI_attribute_other'*/
5232 attribute = 2;
5233 if (!e->rank || gfc_get_full_arrayspec_from_expr (e))
5234 {
5235 if (attr.pointer)
5236 attribute = 0;
5237 else if (attr.allocatable)
5238 attribute = 1;
5239 }
5240
5241 /* If the formal argument is assumed shape and neither a pointer nor
5242 allocatable, it is unconditionally CFI_attribute_other. */
5243 if (fsym->as->type == AS_ASSUMED_SHAPE
5244 && !fsym->attr.pointer && !fsym->attr.allocatable)
5245 cfi_attribute = 2;
5246 else
5247 cfi_attribute = attribute;
5248
5249 if (e->rank != 0)
5250 {
5251 parmse->force_no_tmp = 1;
5252 if (fsym->attr.contiguous
5253 && !gfc_is_simply_contiguous (e, false, true))
5254 gfc_conv_subref_array_arg (parmse, e, false, fsym->attr.intent,
5255 fsym->attr.pointer);
5256 else
5257 gfc_conv_expr_descriptor (parmse, e);
5258
5259 if (POINTER_TYPE_P (TREE_TYPE (parmse->expr)))
5260 parmse->expr = build_fold_indirect_ref_loc (input_location,
5261 parmse->expr);
5262 bool is_artificial = (INDIRECT_REF_P (parmse->expr)
5263 ? DECL_ARTIFICIAL (TREE_OPERAND (parmse->expr, 0))
5264 : DECL_ARTIFICIAL (parmse->expr));
5265
5266 /* Unallocated allocatable arrays and unassociated pointer arrays
5267 need their dtype setting if they are argument associated with
5268 assumed rank dummies. */
5269 if (fsym && fsym->as
5270 && (gfc_expr_attr (e).pointer
5271 || gfc_expr_attr (e).allocatable))
5272 set_dtype_for_unallocated (parmse, e);
5273
5274 /* All the temporary descriptors are marked as DECL_ARTIFICIAL. If
5275 the expression type is different from the descriptor type, then
5276 the offset must be found (eg. to a component ref or substring)
5277 and the dtype updated. Assumed type entities are only allowed
5278 to be dummies in Fortran. They therefore lack the decl specific
5279 appendiges and so must be treated differently from other fortran
5280 entities passed to CFI descriptors in the interface decl. */
5281 type = e->ts.type != BT_ASSUMED ? gfc_typenode_for_spec (&e->ts) :
5282 NULL_TREE;
5283
5284 if (type && is_artificial
5285 && type != gfc_get_element_type (TREE_TYPE (parmse->expr)))
5286 {
5287 /* Obtain the offset to the data. */
5288 gfc_get_dataptr_offset (&parmse->pre, parmse->expr, parmse->expr,
5289 gfc_index_zero_node, true, e);
5290
5291 /* Update the dtype. */
5292 gfc_add_modify (&parmse->pre,
5293 gfc_conv_descriptor_dtype (parmse->expr),
5294 gfc_get_dtype_rank_type (e->rank, type));
5295 }
5296 else if (type == NULL_TREE
5297 || (!is_subref_array (e) && !is_artificial))
5298 {
5299 /* Make sure that the span is set for expressions where it
5300 might not have been done already. */
5301 tmp = gfc_conv_descriptor_elem_len (parmse->expr);
5302 tmp = fold_convert (gfc_array_index_type, tmp);
5303 gfc_conv_descriptor_span_set (&parmse->pre, parmse->expr, tmp);
5304 }
5305 }
5306 else
5307 {
5308 gfc_conv_expr (parmse, e);
5309
5310 if (POINTER_TYPE_P (TREE_TYPE (parmse->expr)))
5311 parmse->expr = build_fold_indirect_ref_loc (input_location,
5312 parmse->expr);
5313
5314 parmse->expr = gfc_conv_scalar_to_descriptor (parmse,
5315 parmse->expr, attr);
5316 }
5317
5318 /* Set the CFI attribute field through a temporary value for the
5319 gfc attribute. */
5320 desc_attr = gfc_conv_descriptor_attribute (parmse->expr);
5321 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
5322 void_type_node, desc_attr,
5323 build_int_cst (TREE_TYPE (desc_attr), cfi_attribute));
5324 gfc_add_expr_to_block (&parmse->pre, tmp);
5325
5326 /* Now pass the gfc_descriptor by reference. */
5327 parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
5328
5329 /* Variables to point to the gfc and CFI descriptors; cfi = NULL implies
5330 that the CFI descriptor is allocated by the gfor_fndecl_gfc_to_cfi call. */
5331 gfc_desc_ptr = parmse->expr;
5332 cfi_desc_ptr = gfc_create_var (pvoid_type_node, "cfi");
5333 gfc_add_modify (&parmse->pre, cfi_desc_ptr, null_pointer_node);
5334
5335 /* Allocate the CFI descriptor itself and fill the fields. */
5336 tmp = gfc_build_addr_expr (NULL_TREE, cfi_desc_ptr);
5337 tmp = build_call_expr_loc (input_location,
5338 gfor_fndecl_gfc_to_cfi, 2, tmp, gfc_desc_ptr);
5339 gfc_add_expr_to_block (&parmse->pre, tmp);
5340
5341 /* Now set the gfc descriptor attribute. */
5342 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
5343 void_type_node, desc_attr,
5344 build_int_cst (TREE_TYPE (desc_attr), attribute));
5345 gfc_add_expr_to_block (&parmse->pre, tmp);
5346
5347 /* The CFI descriptor is passed to the bind_C procedure. */
5348 parmse->expr = cfi_desc_ptr;
5349
5350 /* Free the CFI descriptor. */
5351 tmp = gfc_call_free (cfi_desc_ptr);
5352 gfc_prepend_expr_to_block (&parmse->post, tmp);
5353
5354 /* Transfer values back to gfc descriptor. */
5355 tmp = gfc_build_addr_expr (NULL_TREE, parmse->expr);
5356 tmp = build_call_expr_loc (input_location,
5357 gfor_fndecl_cfi_to_gfc, 2, gfc_desc_ptr, tmp);
5358 gfc_prepend_expr_to_block (&parmse->post, tmp);
5359
5360 /* Deal with an optional dummy being passed to an optional formal arg
5361 by finishing the pre and post blocks and making their execution
5362 conditional on the dummy being present. */
5363 if (fsym->attr.optional && e->expr_type == EXPR_VARIABLE
5364 && e->symtree->n.sym->attr.optional)
5365 {
5366 cond = gfc_conv_expr_present (e->symtree->n.sym);
5367 tmp = fold_build2 (MODIFY_EXPR, void_type_node,
5368 cfi_desc_ptr,
5369 build_int_cst (pvoid_type_node, 0));
5370 tmp = build3_v (COND_EXPR, cond,
5371 gfc_finish_block (&parmse->pre), tmp);
5372 gfc_add_expr_to_block (&parmse->pre, tmp);
5373 tmp = build3_v (COND_EXPR, cond,
5374 gfc_finish_block (&parmse->post),
5375 build_empty_stmt (input_location));
5376 gfc_add_expr_to_block (&parmse->post, tmp);
5377 }
5378 }
5379
5380
5381 /* Generate code for a procedure call. Note can return se->post != NULL.
5382 If se->direct_byref is set then se->expr contains the return parameter.
5383 Return nonzero, if the call has alternate specifiers.
5384 'expr' is only needed for procedure pointer components. */
5385
5386 int
5387 gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
5388 gfc_actual_arglist * args, gfc_expr * expr,
5389 vec<tree, va_gc> *append_args)
5390 {
5391 gfc_interface_mapping mapping;
5392 vec<tree, va_gc> *arglist;
5393 vec<tree, va_gc> *retargs;
5394 tree tmp;
5395 tree fntype;
5396 gfc_se parmse;
5397 gfc_array_info *info;
5398 int byref;
5399 int parm_kind;
5400 tree type;
5401 tree var;
5402 tree len;
5403 tree base_object;
5404 vec<tree, va_gc> *stringargs;
5405 vec<tree, va_gc> *optionalargs;
5406 tree result = NULL;
5407 gfc_formal_arglist *formal;
5408 gfc_actual_arglist *arg;
5409 int has_alternate_specifier = 0;
5410 bool need_interface_mapping;
5411 bool callee_alloc;
5412 bool ulim_copy;
5413 gfc_typespec ts;
5414 gfc_charlen cl;
5415 gfc_expr *e;
5416 gfc_symbol *fsym;
5417 stmtblock_t post;
5418 enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
5419 gfc_component *comp = NULL;
5420 int arglen;
5421 unsigned int argc;
5422
5423 arglist = NULL;
5424 retargs = NULL;
5425 stringargs = NULL;
5426 optionalargs = NULL;
5427 var = NULL_TREE;
5428 len = NULL_TREE;
5429 gfc_clear_ts (&ts);
5430
5431 comp = gfc_get_proc_ptr_comp (expr);
5432
5433 bool elemental_proc = (comp
5434 && comp->ts.interface
5435 && comp->ts.interface->attr.elemental)
5436 || (comp && comp->attr.elemental)
5437 || sym->attr.elemental;
5438
5439 if (se->ss != NULL)
5440 {
5441 if (!elemental_proc)
5442 {
5443 gcc_assert (se->ss->info->type == GFC_SS_FUNCTION);
5444 if (se->ss->info->useflags)
5445 {
5446 gcc_assert ((!comp && gfc_return_by_reference (sym)
5447 && sym->result->attr.dimension)
5448 || (comp && comp->attr.dimension)
5449 || gfc_is_class_array_function (expr));
5450 gcc_assert (se->loop != NULL);
5451 /* Access the previously obtained result. */
5452 gfc_conv_tmp_array_ref (se);
5453 return 0;
5454 }
5455 }
5456 info = &se->ss->info->data.array;
5457 }
5458 else
5459 info = NULL;
5460
5461 gfc_init_block (&post);
5462 gfc_init_interface_mapping (&mapping);
5463 if (!comp)
5464 {
5465 formal = gfc_sym_get_dummy_args (sym);
5466 need_interface_mapping = sym->attr.dimension ||
5467 (sym->ts.type == BT_CHARACTER
5468 && sym->ts.u.cl->length
5469 && sym->ts.u.cl->length->expr_type
5470 != EXPR_CONSTANT);
5471 }
5472 else
5473 {
5474 formal = comp->ts.interface ? comp->ts.interface->formal : NULL;
5475 need_interface_mapping = comp->attr.dimension ||
5476 (comp->ts.type == BT_CHARACTER
5477 && comp->ts.u.cl->length
5478 && comp->ts.u.cl->length->expr_type
5479 != EXPR_CONSTANT);
5480 }
5481
5482 base_object = NULL_TREE;
5483 /* For _vprt->_copy () routines no formal symbol is present. Nevertheless
5484 is the third and fourth argument to such a function call a value
5485 denoting the number of elements to copy (i.e., most of the time the
5486 length of a deferred length string). */
5487 ulim_copy = (formal == NULL)
5488 && UNLIMITED_POLY (sym)
5489 && comp && (strcmp ("_copy", comp->name) == 0);
5490
5491 /* Evaluate the arguments. */
5492 for (arg = args, argc = 0; arg != NULL;
5493 arg = arg->next, formal = formal ? formal->next : NULL, ++argc)
5494 {
5495 bool finalized = false;
5496 bool non_unity_length_string = false;
5497
5498 e = arg->expr;
5499 fsym = formal ? formal->sym : NULL;
5500 parm_kind = MISSING;
5501
5502 if (fsym && fsym->ts.type == BT_CHARACTER && fsym->ts.u.cl
5503 && (!fsym->ts.u.cl->length
5504 || fsym->ts.u.cl->length->expr_type != EXPR_CONSTANT
5505 || mpz_cmp_si (fsym->ts.u.cl->length->value.integer, 1) != 0))
5506 non_unity_length_string = true;
5507
5508 /* If the procedure requires an explicit interface, the actual
5509 argument is passed according to the corresponding formal
5510 argument. If the corresponding formal argument is a POINTER,
5511 ALLOCATABLE or assumed shape, we do not use g77's calling
5512 convention, and pass the address of the array descriptor
5513 instead. Otherwise we use g77's calling convention, in other words
5514 pass the array data pointer without descriptor. */
5515 bool nodesc_arg = fsym != NULL
5516 && !(fsym->attr.pointer || fsym->attr.allocatable)
5517 && fsym->as
5518 && fsym->as->type != AS_ASSUMED_SHAPE
5519 && fsym->as->type != AS_ASSUMED_RANK;
5520 if (comp)
5521 nodesc_arg = nodesc_arg || !comp->attr.always_explicit;
5522 else
5523 nodesc_arg = nodesc_arg || !sym->attr.always_explicit;
5524
5525 /* Class array expressions are sometimes coming completely unadorned
5526 with either arrayspec or _data component. Correct that here.
5527 OOP-TODO: Move this to the frontend. */
5528 if (e && e->expr_type == EXPR_VARIABLE
5529 && !e->ref
5530 && e->ts.type == BT_CLASS
5531 && (CLASS_DATA (e)->attr.codimension
5532 || CLASS_DATA (e)->attr.dimension))
5533 {
5534 gfc_typespec temp_ts = e->ts;
5535 gfc_add_class_array_ref (e);
5536 e->ts = temp_ts;
5537 }
5538
5539 if (e == NULL)
5540 {
5541 if (se->ignore_optional)
5542 {
5543 /* Some intrinsics have already been resolved to the correct
5544 parameters. */
5545 continue;
5546 }
5547 else if (arg->label)
5548 {
5549 has_alternate_specifier = 1;
5550 continue;
5551 }
5552 else
5553 {
5554 gfc_init_se (&parmse, NULL);
5555
5556 /* For scalar arguments with VALUE attribute which are passed by
5557 value, pass "0" and a hidden argument gives the optional
5558 status. */
5559 if (fsym && fsym->attr.optional && fsym->attr.value
5560 && !fsym->attr.dimension && fsym->ts.type != BT_CHARACTER
5561 && fsym->ts.type != BT_CLASS && fsym->ts.type != BT_DERIVED)
5562 {
5563 parmse.expr = fold_convert (gfc_sym_type (fsym),
5564 integer_zero_node);
5565 vec_safe_push (optionalargs, boolean_false_node);
5566 }
5567 else
5568 {
5569 /* Pass a NULL pointer for an absent arg. */
5570 parmse.expr = null_pointer_node;
5571 if (arg->missing_arg_type == BT_CHARACTER)
5572 parmse.string_length = build_int_cst (gfc_charlen_type_node,
5573 0);
5574 }
5575 }
5576 }
5577 else if (arg->expr->expr_type == EXPR_NULL
5578 && fsym && !fsym->attr.pointer
5579 && (fsym->ts.type != BT_CLASS
5580 || !CLASS_DATA (fsym)->attr.class_pointer))
5581 {
5582 /* Pass a NULL pointer to denote an absent arg. */
5583 gcc_assert (fsym->attr.optional && !fsym->attr.allocatable
5584 && (fsym->ts.type != BT_CLASS
5585 || !CLASS_DATA (fsym)->attr.allocatable));
5586 gfc_init_se (&parmse, NULL);
5587 parmse.expr = null_pointer_node;
5588 if (arg->missing_arg_type == BT_CHARACTER)
5589 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
5590 }
5591 else if (fsym && fsym->ts.type == BT_CLASS
5592 && e->ts.type == BT_DERIVED)
5593 {
5594 /* The derived type needs to be converted to a temporary
5595 CLASS object. */
5596 gfc_init_se (&parmse, se);
5597 gfc_conv_derived_to_class (&parmse, e, fsym->ts, NULL,
5598 fsym->attr.optional
5599 && e->expr_type == EXPR_VARIABLE
5600 && e->symtree->n.sym->attr.optional,
5601 CLASS_DATA (fsym)->attr.class_pointer
5602 || CLASS_DATA (fsym)->attr.allocatable);
5603 }
5604 else if (UNLIMITED_POLY (fsym) && e->ts.type != BT_CLASS)
5605 {
5606 /* The intrinsic type needs to be converted to a temporary
5607 CLASS object for the unlimited polymorphic formal. */
5608 gfc_init_se (&parmse, se);
5609 gfc_conv_intrinsic_to_class (&parmse, e, fsym->ts);
5610 }
5611 else if (se->ss && se->ss->info->useflags)
5612 {
5613 gfc_ss *ss;
5614
5615 ss = se->ss;
5616
5617 /* An elemental function inside a scalarized loop. */
5618 gfc_init_se (&parmse, se);
5619 parm_kind = ELEMENTAL;
5620
5621 /* When no fsym is present, ulim_copy is set and this is a third or
5622 fourth argument, use call-by-value instead of by reference to
5623 hand the length properties to the copy routine (i.e., most of the
5624 time this will be a call to a __copy_character_* routine where the
5625 third and fourth arguments are the lengths of a deferred length
5626 char array). */
5627 if ((fsym && fsym->attr.value)
5628 || (ulim_copy && (argc == 2 || argc == 3)))
5629 gfc_conv_expr (&parmse, e);
5630 else
5631 gfc_conv_expr_reference (&parmse, e);
5632
5633 if (e->ts.type == BT_CHARACTER && !e->rank
5634 && e->expr_type == EXPR_FUNCTION)
5635 parmse.expr = build_fold_indirect_ref_loc (input_location,
5636 parmse.expr);
5637
5638 if (fsym && fsym->ts.type == BT_DERIVED
5639 && gfc_is_class_container_ref (e))
5640 {
5641 parmse.expr = gfc_class_data_get (parmse.expr);
5642
5643 if (fsym->attr.optional && e->expr_type == EXPR_VARIABLE
5644 && e->symtree->n.sym->attr.optional)
5645 {
5646 tree cond = gfc_conv_expr_present (e->symtree->n.sym);
5647 parmse.expr = build3_loc (input_location, COND_EXPR,
5648 TREE_TYPE (parmse.expr),
5649 cond, parmse.expr,
5650 fold_convert (TREE_TYPE (parmse.expr),
5651 null_pointer_node));
5652 }
5653 }
5654
5655 /* If we are passing an absent array as optional dummy to an
5656 elemental procedure, make sure that we pass NULL when the data
5657 pointer is NULL. We need this extra conditional because of
5658 scalarization which passes arrays elements to the procedure,
5659 ignoring the fact that the array can be absent/unallocated/... */
5660 if (ss->info->can_be_null_ref && ss->info->type != GFC_SS_REFERENCE)
5661 {
5662 tree descriptor_data;
5663
5664 descriptor_data = ss->info->data.array.data;
5665 tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
5666 descriptor_data,
5667 fold_convert (TREE_TYPE (descriptor_data),
5668 null_pointer_node));
5669 parmse.expr
5670 = fold_build3_loc (input_location, COND_EXPR,
5671 TREE_TYPE (parmse.expr),
5672 gfc_unlikely (tmp, PRED_FORTRAN_ABSENT_DUMMY),
5673 fold_convert (TREE_TYPE (parmse.expr),
5674 null_pointer_node),
5675 parmse.expr);
5676 }
5677
5678 /* The scalarizer does not repackage the reference to a class
5679 array - instead it returns a pointer to the data element. */
5680 if (fsym && fsym->ts.type == BT_CLASS && e->ts.type == BT_CLASS)
5681 gfc_conv_class_to_class (&parmse, e, fsym->ts, true,
5682 fsym->attr.intent != INTENT_IN
5683 && (CLASS_DATA (fsym)->attr.class_pointer
5684 || CLASS_DATA (fsym)->attr.allocatable),
5685 fsym->attr.optional
5686 && e->expr_type == EXPR_VARIABLE
5687 && e->symtree->n.sym->attr.optional,
5688 CLASS_DATA (fsym)->attr.class_pointer
5689 || CLASS_DATA (fsym)->attr.allocatable);
5690 }
5691 else
5692 {
5693 bool scalar;
5694 gfc_ss *argss;
5695
5696 gfc_init_se (&parmse, NULL);
5697
5698 /* Check whether the expression is a scalar or not; we cannot use
5699 e->rank as it can be nonzero for functions arguments. */
5700 argss = gfc_walk_expr (e);
5701 scalar = argss == gfc_ss_terminator;
5702 if (!scalar)
5703 gfc_free_ss_chain (argss);
5704
5705 /* Special handling for passing scalar polymorphic coarrays;
5706 otherwise one passes "class->_data.data" instead of "&class". */
5707 if (e->rank == 0 && e->ts.type == BT_CLASS
5708 && fsym && fsym->ts.type == BT_CLASS
5709 && CLASS_DATA (fsym)->attr.codimension
5710 && !CLASS_DATA (fsym)->attr.dimension)
5711 {
5712 gfc_add_class_array_ref (e);
5713 parmse.want_coarray = 1;
5714 scalar = false;
5715 }
5716
5717 /* A scalar or transformational function. */
5718 if (scalar)
5719 {
5720 if (e->expr_type == EXPR_VARIABLE
5721 && e->symtree->n.sym->attr.cray_pointee
5722 && fsym && fsym->attr.flavor == FL_PROCEDURE)
5723 {
5724 /* The Cray pointer needs to be converted to a pointer to
5725 a type given by the expression. */
5726 gfc_conv_expr (&parmse, e);
5727 type = build_pointer_type (TREE_TYPE (parmse.expr));
5728 tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
5729 parmse.expr = convert (type, tmp);
5730 }
5731
5732 else if (sym->attr.is_bind_c && e
5733 && (is_CFI_desc (fsym, NULL)
5734 || non_unity_length_string))
5735 /* Implement F2018, C.12.6.1: paragraph (2). */
5736 gfc_conv_gfc_desc_to_cfi_desc (&parmse, e, fsym);
5737
5738 else if (fsym && fsym->attr.value)
5739 {
5740 if (fsym->ts.type == BT_CHARACTER
5741 && fsym->ts.is_c_interop
5742 && fsym->ns->proc_name != NULL
5743 && fsym->ns->proc_name->attr.is_bind_c)
5744 {
5745 parmse.expr = NULL;
5746 gfc_conv_scalar_char_value (fsym, &parmse, &e);
5747 if (parmse.expr == NULL)
5748 gfc_conv_expr (&parmse, e);
5749 }
5750 else
5751 {
5752 gfc_conv_expr (&parmse, e);
5753 if (fsym->attr.optional
5754 && fsym->ts.type != BT_CLASS
5755 && fsym->ts.type != BT_DERIVED)
5756 {
5757 if (e->expr_type != EXPR_VARIABLE
5758 || !e->symtree->n.sym->attr.optional
5759 || e->ref != NULL)
5760 vec_safe_push (optionalargs, boolean_true_node);
5761 else
5762 {
5763 tmp = gfc_conv_expr_present (e->symtree->n.sym);
5764 if (!e->symtree->n.sym->attr.value)
5765 parmse.expr
5766 = fold_build3_loc (input_location, COND_EXPR,
5767 TREE_TYPE (parmse.expr),
5768 tmp, parmse.expr,
5769 fold_convert (TREE_TYPE (parmse.expr),
5770 integer_zero_node));
5771
5772 vec_safe_push (optionalargs,
5773 fold_convert (boolean_type_node,
5774 tmp));
5775 }
5776 }
5777 }
5778 }
5779
5780 else if (arg->name && arg->name[0] == '%')
5781 /* Argument list functions %VAL, %LOC and %REF are signalled
5782 through arg->name. */
5783 conv_arglist_function (&parmse, arg->expr, arg->name);
5784 else if ((e->expr_type == EXPR_FUNCTION)
5785 && ((e->value.function.esym
5786 && e->value.function.esym->result->attr.pointer)
5787 || (!e->value.function.esym
5788 && e->symtree->n.sym->attr.pointer))
5789 && fsym && fsym->attr.target)
5790 {
5791 gfc_conv_expr (&parmse, e);
5792 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
5793 }
5794
5795 else if (e->expr_type == EXPR_FUNCTION
5796 && e->symtree->n.sym->result
5797 && e->symtree->n.sym->result != e->symtree->n.sym
5798 && e->symtree->n.sym->result->attr.proc_pointer)
5799 {
5800 /* Functions returning procedure pointers. */
5801 gfc_conv_expr (&parmse, e);
5802 if (fsym && fsym->attr.proc_pointer)
5803 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
5804 }
5805
5806 else
5807 {
5808 if (e->ts.type == BT_CLASS && fsym
5809 && fsym->ts.type == BT_CLASS
5810 && (!CLASS_DATA (fsym)->as
5811 || CLASS_DATA (fsym)->as->type != AS_ASSUMED_RANK)
5812 && CLASS_DATA (e)->attr.codimension)
5813 {
5814 gcc_assert (!CLASS_DATA (fsym)->attr.codimension);
5815 gcc_assert (!CLASS_DATA (fsym)->as);
5816 gfc_add_class_array_ref (e);
5817 parmse.want_coarray = 1;
5818 gfc_conv_expr_reference (&parmse, e);
5819 class_scalar_coarray_to_class (&parmse, e, fsym->ts,
5820 fsym->attr.optional
5821 && e->expr_type == EXPR_VARIABLE);
5822 }
5823 else if (e->ts.type == BT_CLASS && fsym
5824 && fsym->ts.type == BT_CLASS
5825 && !CLASS_DATA (fsym)->as
5826 && !CLASS_DATA (e)->as
5827 && strcmp (fsym->ts.u.derived->name,
5828 e->ts.u.derived->name))
5829 {
5830 type = gfc_typenode_for_spec (&fsym->ts);
5831 var = gfc_create_var (type, fsym->name);
5832 gfc_conv_expr (&parmse, e);
5833 if (fsym->attr.optional
5834 && e->expr_type == EXPR_VARIABLE
5835 && e->symtree->n.sym->attr.optional)
5836 {
5837 stmtblock_t block;
5838 tree cond;
5839 tmp = gfc_build_addr_expr (NULL_TREE, parmse.expr);
5840 cond = fold_build2_loc (input_location, NE_EXPR,
5841 logical_type_node, tmp,
5842 fold_convert (TREE_TYPE (tmp),
5843 null_pointer_node));
5844 gfc_start_block (&block);
5845 gfc_add_modify (&block, var,
5846 fold_build1_loc (input_location,
5847 VIEW_CONVERT_EXPR,
5848 type, parmse.expr));
5849 gfc_add_expr_to_block (&parmse.pre,
5850 fold_build3_loc (input_location,
5851 COND_EXPR, void_type_node,
5852 cond, gfc_finish_block (&block),
5853 build_empty_stmt (input_location)));
5854 parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
5855 parmse.expr = build3_loc (input_location, COND_EXPR,
5856 TREE_TYPE (parmse.expr),
5857 cond, parmse.expr,
5858 fold_convert (TREE_TYPE (parmse.expr),
5859 null_pointer_node));
5860 }
5861 else
5862 {
5863 /* Since the internal representation of unlimited
5864 polymorphic expressions includes an extra field
5865 that other class objects do not, a cast to the
5866 formal type does not work. */
5867 if (!UNLIMITED_POLY (e) && UNLIMITED_POLY (fsym))
5868 {
5869 tree efield;
5870
5871 /* Set the _data field. */
5872 tmp = gfc_class_data_get (var);
5873 efield = fold_convert (TREE_TYPE (tmp),
5874 gfc_class_data_get (parmse.expr));
5875 gfc_add_modify (&parmse.pre, tmp, efield);
5876
5877 /* Set the _vptr field. */
5878 tmp = gfc_class_vptr_get (var);
5879 efield = fold_convert (TREE_TYPE (tmp),
5880 gfc_class_vptr_get (parmse.expr));
5881 gfc_add_modify (&parmse.pre, tmp, efield);
5882
5883 /* Set the _len field. */
5884 tmp = gfc_class_len_get (var);
5885 gfc_add_modify (&parmse.pre, tmp,
5886 build_int_cst (TREE_TYPE (tmp), 0));
5887 }
5888 else
5889 {
5890 tmp = fold_build1_loc (input_location,
5891 VIEW_CONVERT_EXPR,
5892 type, parmse.expr);
5893 gfc_add_modify (&parmse.pre, var, tmp);
5894 ;
5895 }
5896 parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
5897 }
5898 }
5899 else
5900 {
5901 bool add_clobber;
5902 add_clobber = fsym && fsym->attr.intent == INTENT_OUT
5903 && !fsym->attr.allocatable && !fsym->attr.pointer
5904 && !e->symtree->n.sym->attr.dimension
5905 && !e->symtree->n.sym->attr.pointer
5906 /* See PR 41453. */
5907 && !e->symtree->n.sym->attr.dummy
5908 /* FIXME - PR 87395 and PR 41453 */
5909 && e->symtree->n.sym->attr.save == SAVE_NONE
5910 && !e->symtree->n.sym->attr.associate_var
5911 && e->ts.type != BT_CHARACTER && e->ts.type != BT_DERIVED
5912 && e->ts.type != BT_CLASS && !sym->attr.elemental;
5913
5914 gfc_conv_expr_reference (&parmse, e, add_clobber);
5915 }
5916 /* Catch base objects that are not variables. */
5917 if (e->ts.type == BT_CLASS
5918 && e->expr_type != EXPR_VARIABLE
5919 && expr && e == expr->base_expr)
5920 base_object = build_fold_indirect_ref_loc (input_location,
5921 parmse.expr);
5922
5923 /* A class array element needs converting back to be a
5924 class object, if the formal argument is a class object. */
5925 if (fsym && fsym->ts.type == BT_CLASS
5926 && e->ts.type == BT_CLASS
5927 && ((CLASS_DATA (fsym)->as
5928 && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
5929 || CLASS_DATA (e)->attr.dimension))
5930 gfc_conv_class_to_class (&parmse, e, fsym->ts, false,
5931 fsym->attr.intent != INTENT_IN
5932 && (CLASS_DATA (fsym)->attr.class_pointer
5933 || CLASS_DATA (fsym)->attr.allocatable),
5934 fsym->attr.optional
5935 && e->expr_type == EXPR_VARIABLE
5936 && e->symtree->n.sym->attr.optional,
5937 CLASS_DATA (fsym)->attr.class_pointer
5938 || CLASS_DATA (fsym)->attr.allocatable);
5939
5940 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
5941 allocated on entry, it must be deallocated. */
5942 if (fsym && fsym->attr.intent == INTENT_OUT
5943 && (fsym->attr.allocatable
5944 || (fsym->ts.type == BT_CLASS
5945 && CLASS_DATA (fsym)->attr.allocatable)))
5946 {
5947 stmtblock_t block;
5948 tree ptr;
5949
5950 gfc_init_block (&block);
5951 ptr = parmse.expr;
5952 if (e->ts.type == BT_CLASS)
5953 ptr = gfc_class_data_get (ptr);
5954
5955 tmp = gfc_deallocate_scalar_with_status (ptr, NULL_TREE,
5956 NULL_TREE, true,
5957 e, e->ts);
5958 gfc_add_expr_to_block (&block, tmp);
5959 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
5960 void_type_node, ptr,
5961 null_pointer_node);
5962 gfc_add_expr_to_block (&block, tmp);
5963
5964 if (fsym->ts.type == BT_CLASS && UNLIMITED_POLY (fsym))
5965 {
5966 gfc_add_modify (&block, ptr,
5967 fold_convert (TREE_TYPE (ptr),
5968 null_pointer_node));
5969 gfc_add_expr_to_block (&block, tmp);
5970 }
5971 else if (fsym->ts.type == BT_CLASS)
5972 {
5973 gfc_symbol *vtab;
5974 vtab = gfc_find_derived_vtab (fsym->ts.u.derived);
5975 tmp = gfc_get_symbol_decl (vtab);
5976 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
5977 ptr = gfc_class_vptr_get (parmse.expr);
5978 gfc_add_modify (&block, ptr,
5979 fold_convert (TREE_TYPE (ptr), tmp));
5980 gfc_add_expr_to_block (&block, tmp);
5981 }
5982
5983 if (fsym->attr.optional
5984 && e->expr_type == EXPR_VARIABLE
5985 && e->symtree->n.sym->attr.optional)
5986 {
5987 tmp = fold_build3_loc (input_location, COND_EXPR,
5988 void_type_node,
5989 gfc_conv_expr_present (e->symtree->n.sym),
5990 gfc_finish_block (&block),
5991 build_empty_stmt (input_location));
5992 }
5993 else
5994 tmp = gfc_finish_block (&block);
5995
5996 gfc_add_expr_to_block (&se->pre, tmp);
5997 }
5998
5999 if (fsym && (fsym->ts.type == BT_DERIVED
6000 || fsym->ts.type == BT_ASSUMED)
6001 && e->ts.type == BT_CLASS
6002 && !CLASS_DATA (e)->attr.dimension
6003 && !CLASS_DATA (e)->attr.codimension)
6004 {
6005 parmse.expr = gfc_class_data_get (parmse.expr);
6006 /* The result is a class temporary, whose _data component
6007 must be freed to avoid a memory leak. */
6008 if (e->expr_type == EXPR_FUNCTION
6009 && CLASS_DATA (e)->attr.allocatable)
6010 {
6011 tree zero;
6012
6013 gfc_expr *var;
6014
6015 /* Borrow the function symbol to make a call to
6016 gfc_add_finalizer_call and then restore it. */
6017 tmp = e->symtree->n.sym->backend_decl;
6018 e->symtree->n.sym->backend_decl
6019 = TREE_OPERAND (parmse.expr, 0);
6020 e->symtree->n.sym->attr.flavor = FL_VARIABLE;
6021 var = gfc_lval_expr_from_sym (e->symtree->n.sym);
6022 finalized = gfc_add_finalizer_call (&parmse.post,
6023 var);
6024 gfc_free_expr (var);
6025 e->symtree->n.sym->backend_decl = tmp;
6026 e->symtree->n.sym->attr.flavor = FL_PROCEDURE;
6027
6028 /* Then free the class _data. */
6029 zero = build_int_cst (TREE_TYPE (parmse.expr), 0);
6030 tmp = fold_build2_loc (input_location, NE_EXPR,
6031 logical_type_node,
6032 parmse.expr, zero);
6033 tmp = build3_v (COND_EXPR, tmp,
6034 gfc_call_free (parmse.expr),
6035 build_empty_stmt (input_location));
6036 gfc_add_expr_to_block (&parmse.post, tmp);
6037 gfc_add_modify (&parmse.post, parmse.expr, zero);
6038 }
6039 }
6040
6041 /* Wrap scalar variable in a descriptor. We need to convert
6042 the address of a pointer back to the pointer itself before,
6043 we can assign it to the data field. */
6044
6045 if (fsym && fsym->as && fsym->as->type == AS_ASSUMED_RANK
6046 && fsym->ts.type != BT_CLASS && e->expr_type != EXPR_NULL)
6047 {
6048 tmp = parmse.expr;
6049 if (TREE_CODE (tmp) == ADDR_EXPR)
6050 tmp = build_fold_indirect_ref_loc (input_location, tmp);
6051 parmse.expr = gfc_conv_scalar_to_descriptor (&parmse, tmp,
6052 fsym->attr);
6053 parmse.expr = gfc_build_addr_expr (NULL_TREE,
6054 parmse.expr);
6055 }
6056 else if (fsym && e->expr_type != EXPR_NULL
6057 && ((fsym->attr.pointer
6058 && fsym->attr.flavor != FL_PROCEDURE)
6059 || (fsym->attr.proc_pointer
6060 && !(e->expr_type == EXPR_VARIABLE
6061 && e->symtree->n.sym->attr.dummy))
6062 || (fsym->attr.proc_pointer
6063 && e->expr_type == EXPR_VARIABLE
6064 && gfc_is_proc_ptr_comp (e))
6065 || (fsym->attr.allocatable
6066 && fsym->attr.flavor != FL_PROCEDURE)))
6067 {
6068 /* Scalar pointer dummy args require an extra level of
6069 indirection. The null pointer already contains
6070 this level of indirection. */
6071 parm_kind = SCALAR_POINTER;
6072 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
6073 }
6074 }
6075 }
6076 else if (e->ts.type == BT_CLASS
6077 && fsym && fsym->ts.type == BT_CLASS
6078 && (CLASS_DATA (fsym)->attr.dimension
6079 || CLASS_DATA (fsym)->attr.codimension))
6080 {
6081 /* Pass a class array. */
6082 parmse.use_offset = 1;
6083 gfc_conv_expr_descriptor (&parmse, e);
6084
6085 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
6086 allocated on entry, it must be deallocated. */
6087 if (fsym->attr.intent == INTENT_OUT
6088 && CLASS_DATA (fsym)->attr.allocatable)
6089 {
6090 stmtblock_t block;
6091 tree ptr;
6092
6093 gfc_init_block (&block);
6094 ptr = parmse.expr;
6095 ptr = gfc_class_data_get (ptr);
6096
6097 tmp = gfc_deallocate_with_status (ptr, NULL_TREE,
6098 NULL_TREE, NULL_TREE,
6099 NULL_TREE, true, e,
6100 GFC_CAF_COARRAY_NOCOARRAY);
6101 gfc_add_expr_to_block (&block, tmp);
6102 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
6103 void_type_node, ptr,
6104 null_pointer_node);
6105 gfc_add_expr_to_block (&block, tmp);
6106 gfc_reset_vptr (&block, e);
6107
6108 if (fsym->attr.optional
6109 && e->expr_type == EXPR_VARIABLE
6110 && (!e->ref
6111 || (e->ref->type == REF_ARRAY
6112 && e->ref->u.ar.type != AR_FULL))
6113 && e->symtree->n.sym->attr.optional)
6114 {
6115 tmp = fold_build3_loc (input_location, COND_EXPR,
6116 void_type_node,
6117 gfc_conv_expr_present (e->symtree->n.sym),
6118 gfc_finish_block (&block),
6119 build_empty_stmt (input_location));
6120 }
6121 else
6122 tmp = gfc_finish_block (&block);
6123
6124 gfc_add_expr_to_block (&se->pre, tmp);
6125 }
6126
6127 /* The conversion does not repackage the reference to a class
6128 array - _data descriptor. */
6129 gfc_conv_class_to_class (&parmse, e, fsym->ts, false,
6130 fsym->attr.intent != INTENT_IN
6131 && (CLASS_DATA (fsym)->attr.class_pointer
6132 || CLASS_DATA (fsym)->attr.allocatable),
6133 fsym->attr.optional
6134 && e->expr_type == EXPR_VARIABLE
6135 && e->symtree->n.sym->attr.optional,
6136 CLASS_DATA (fsym)->attr.class_pointer
6137 || CLASS_DATA (fsym)->attr.allocatable);
6138 }
6139 else
6140 {
6141 /* If the argument is a function call that may not create
6142 a temporary for the result, we have to check that we
6143 can do it, i.e. that there is no alias between this
6144 argument and another one. */
6145 if (gfc_get_noncopying_intrinsic_argument (e) != NULL)
6146 {
6147 gfc_expr *iarg;
6148 sym_intent intent;
6149
6150 if (fsym != NULL)
6151 intent = fsym->attr.intent;
6152 else
6153 intent = INTENT_UNKNOWN;
6154
6155 if (gfc_check_fncall_dependency (e, intent, sym, args,
6156 NOT_ELEMENTAL))
6157 parmse.force_tmp = 1;
6158
6159 iarg = e->value.function.actual->expr;
6160
6161 /* Temporary needed if aliasing due to host association. */
6162 if (sym->attr.contained
6163 && !sym->attr.pure
6164 && !sym->attr.implicit_pure
6165 && !sym->attr.use_assoc
6166 && iarg->expr_type == EXPR_VARIABLE
6167 && sym->ns == iarg->symtree->n.sym->ns)
6168 parmse.force_tmp = 1;
6169
6170 /* Ditto within module. */
6171 if (sym->attr.use_assoc
6172 && !sym->attr.pure
6173 && !sym->attr.implicit_pure
6174 && iarg->expr_type == EXPR_VARIABLE
6175 && sym->module == iarg->symtree->n.sym->module)
6176 parmse.force_tmp = 1;
6177 }
6178
6179 if (sym->attr.is_bind_c && e
6180 && (is_CFI_desc (fsym, NULL) || non_unity_length_string))
6181 /* Implement F2018, C.12.6.1: paragraph (2). */
6182 gfc_conv_gfc_desc_to_cfi_desc (&parmse, e, fsym);
6183
6184 else if (e->expr_type == EXPR_VARIABLE
6185 && is_subref_array (e)
6186 && !(fsym && fsym->attr.pointer))
6187 /* The actual argument is a component reference to an
6188 array of derived types. In this case, the argument
6189 is converted to a temporary, which is passed and then
6190 written back after the procedure call. */
6191 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
6192 fsym ? fsym->attr.intent : INTENT_INOUT,
6193 fsym && fsym->attr.pointer);
6194
6195 else if (gfc_is_class_array_ref (e, NULL)
6196 && fsym && fsym->ts.type == BT_DERIVED)
6197 /* The actual argument is a component reference to an
6198 array of derived types. In this case, the argument
6199 is converted to a temporary, which is passed and then
6200 written back after the procedure call.
6201 OOP-TODO: Insert code so that if the dynamic type is
6202 the same as the declared type, copy-in/copy-out does
6203 not occur. */
6204 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
6205 fsym->attr.intent,
6206 fsym->attr.pointer);
6207
6208 else if (gfc_is_class_array_function (e)
6209 && fsym && fsym->ts.type == BT_DERIVED)
6210 /* See previous comment. For function actual argument,
6211 the write out is not needed so the intent is set as
6212 intent in. */
6213 {
6214 e->must_finalize = 1;
6215 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
6216 INTENT_IN, fsym->attr.pointer);
6217 }
6218 else if (fsym && fsym->attr.contiguous
6219 && !gfc_is_simply_contiguous (e, false, true)
6220 && gfc_expr_is_variable (e))
6221 {
6222 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
6223 fsym->attr.intent,
6224 fsym->attr.pointer);
6225 }
6226 else
6227 gfc_conv_array_parameter (&parmse, e, nodesc_arg, fsym,
6228 sym->name, NULL);
6229
6230 /* Unallocated allocatable arrays and unassociated pointer arrays
6231 need their dtype setting if they are argument associated with
6232 assumed rank dummies. */
6233 if (!sym->attr.is_bind_c && e && fsym && fsym->as
6234 && fsym->as->type == AS_ASSUMED_RANK)
6235 {
6236 if (gfc_expr_attr (e).pointer
6237 || gfc_expr_attr (e).allocatable)
6238 set_dtype_for_unallocated (&parmse, e);
6239 else if (e->expr_type == EXPR_VARIABLE
6240 && e->symtree->n.sym->attr.dummy
6241 && e->symtree->n.sym->as
6242 && e->symtree->n.sym->as->type == AS_ASSUMED_SIZE)
6243 {
6244 tree minus_one;
6245 tmp = build_fold_indirect_ref_loc (input_location,
6246 parmse.expr);
6247 minus_one = build_int_cst (gfc_array_index_type, -1);
6248 gfc_conv_descriptor_ubound_set (&parmse.pre, tmp,
6249 gfc_rank_cst[e->rank - 1],
6250 minus_one);
6251 }
6252 }
6253
6254 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
6255 allocated on entry, it must be deallocated. */
6256 if (fsym && fsym->attr.allocatable
6257 && fsym->attr.intent == INTENT_OUT)
6258 {
6259 if (fsym->ts.type == BT_DERIVED
6260 && fsym->ts.u.derived->attr.alloc_comp)
6261 {
6262 // deallocate the components first
6263 tmp = gfc_deallocate_alloc_comp (fsym->ts.u.derived,
6264 parmse.expr, e->rank);
6265 if (tmp != NULL_TREE)
6266 gfc_add_expr_to_block (&se->pre, tmp);
6267 }
6268
6269 tmp = parmse.expr;
6270 /* With bind(C), the actual argument is replaced by a bind-C
6271 descriptor; in this case, the data component arrives here,
6272 which shall not be dereferenced, but still freed and
6273 nullified. */
6274 if (TREE_TYPE(tmp) != pvoid_type_node)
6275 tmp = build_fold_indirect_ref_loc (input_location,
6276 parmse.expr);
6277 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
6278 tmp = gfc_conv_descriptor_data_get (tmp);
6279 tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
6280 NULL_TREE, NULL_TREE, true,
6281 e,
6282 GFC_CAF_COARRAY_NOCOARRAY);
6283 if (fsym->attr.optional
6284 && e->expr_type == EXPR_VARIABLE
6285 && e->symtree->n.sym->attr.optional)
6286 tmp = fold_build3_loc (input_location, COND_EXPR,
6287 void_type_node,
6288 gfc_conv_expr_present (e->symtree->n.sym),
6289 tmp, build_empty_stmt (input_location));
6290 gfc_add_expr_to_block (&se->pre, tmp);
6291 }
6292 }
6293 }
6294
6295 /* The case with fsym->attr.optional is that of a user subroutine
6296 with an interface indicating an optional argument. When we call
6297 an intrinsic subroutine, however, fsym is NULL, but we might still
6298 have an optional argument, so we proceed to the substitution
6299 just in case. */
6300 if (e && (fsym == NULL || fsym->attr.optional))
6301 {
6302 /* If an optional argument is itself an optional dummy argument,
6303 check its presence and substitute a null if absent. This is
6304 only needed when passing an array to an elemental procedure
6305 as then array elements are accessed - or no NULL pointer is
6306 allowed and a "1" or "0" should be passed if not present.
6307 When passing a non-array-descriptor full array to a
6308 non-array-descriptor dummy, no check is needed. For
6309 array-descriptor actual to array-descriptor dummy, see
6310 PR 41911 for why a check has to be inserted.
6311 fsym == NULL is checked as intrinsics required the descriptor
6312 but do not always set fsym.
6313 Also, it is necessary to pass a NULL pointer to library routines
6314 which usually ignore optional arguments, so they can handle
6315 these themselves. */
6316 if (e->expr_type == EXPR_VARIABLE
6317 && e->symtree->n.sym->attr.optional
6318 && (((e->rank != 0 && elemental_proc)
6319 || e->representation.length || e->ts.type == BT_CHARACTER
6320 || (e->rank != 0
6321 && (fsym == NULL
6322 || (fsym->as
6323 && (fsym->as->type == AS_ASSUMED_SHAPE
6324 || fsym->as->type == AS_ASSUMED_RANK
6325 || fsym->as->type == AS_DEFERRED)))))
6326 || se->ignore_optional))
6327 gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
6328 e->representation.length);
6329 }
6330
6331 if (fsym && e)
6332 {
6333 /* Obtain the character length of an assumed character length
6334 length procedure from the typespec. */
6335 if (fsym->ts.type == BT_CHARACTER
6336 && parmse.string_length == NULL_TREE
6337 && e->ts.type == BT_PROCEDURE
6338 && e->symtree->n.sym->ts.type == BT_CHARACTER
6339 && e->symtree->n.sym->ts.u.cl->length != NULL
6340 && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
6341 {
6342 gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
6343 parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
6344 }
6345 }
6346
6347 if (fsym && need_interface_mapping && e)
6348 gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
6349
6350 gfc_add_block_to_block (&se->pre, &parmse.pre);
6351 gfc_add_block_to_block (&post, &parmse.post);
6352
6353 /* Allocated allocatable components of derived types must be
6354 deallocated for non-variable scalars, array arguments to elemental
6355 procedures, and array arguments with descriptor to non-elemental
6356 procedures. As bounds information for descriptorless arrays is no
6357 longer available here, they are dealt with in trans-array.c
6358 (gfc_conv_array_parameter). */
6359 if (e && (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS)
6360 && e->ts.u.derived->attr.alloc_comp
6361 && (e->rank == 0 || elemental_proc || !nodesc_arg)
6362 && !expr_may_alias_variables (e, elemental_proc))
6363 {
6364 int parm_rank;
6365 /* It is known the e returns a structure type with at least one
6366 allocatable component. When e is a function, ensure that the
6367 function is called once only by using a temporary variable. */
6368 if (!DECL_P (parmse.expr))
6369 parmse.expr = gfc_evaluate_now_loc (input_location,
6370 parmse.expr, &se->pre);
6371
6372 if (fsym && fsym->attr.value)
6373 tmp = parmse.expr;
6374 else
6375 tmp = build_fold_indirect_ref_loc (input_location,
6376 parmse.expr);
6377
6378 parm_rank = e->rank;
6379 switch (parm_kind)
6380 {
6381 case (ELEMENTAL):
6382 case (SCALAR):
6383 parm_rank = 0;
6384 break;
6385
6386 case (SCALAR_POINTER):
6387 tmp = build_fold_indirect_ref_loc (input_location,
6388 tmp);
6389 break;
6390 }
6391
6392 if (e->ts.type == BT_DERIVED && fsym && fsym->ts.type == BT_CLASS)
6393 {
6394 /* The derived type is passed to gfc_deallocate_alloc_comp.
6395 Therefore, class actuals can be handled correctly but derived
6396 types passed to class formals need the _data component. */
6397 tmp = gfc_class_data_get (tmp);
6398 if (!CLASS_DATA (fsym)->attr.dimension)
6399 tmp = build_fold_indirect_ref_loc (input_location, tmp);
6400 }
6401
6402 if (e->expr_type == EXPR_OP
6403 && e->value.op.op == INTRINSIC_PARENTHESES
6404 && e->value.op.op1->expr_type == EXPR_VARIABLE)
6405 {
6406 tree local_tmp;
6407 local_tmp = gfc_evaluate_now (tmp, &se->pre);
6408 local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp,
6409 parm_rank, 0);
6410 gfc_add_expr_to_block (&se->post, local_tmp);
6411 }
6412
6413 if (!finalized && !e->must_finalize)
6414 {
6415 if ((e->ts.type == BT_CLASS
6416 && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
6417 || e->ts.type == BT_DERIVED)
6418 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp,
6419 parm_rank);
6420 else if (e->ts.type == BT_CLASS)
6421 tmp = gfc_deallocate_alloc_comp (CLASS_DATA (e)->ts.u.derived,
6422 tmp, parm_rank);
6423 gfc_prepend_expr_to_block (&post, tmp);
6424 }
6425 }
6426
6427 /* Add argument checking of passing an unallocated/NULL actual to
6428 a nonallocatable/nonpointer dummy. */
6429
6430 if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
6431 {
6432 symbol_attribute attr;
6433 char *msg;
6434 tree cond;
6435
6436 if (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
6437 attr = gfc_expr_attr (e);
6438 else
6439 goto end_pointer_check;
6440
6441 /* In Fortran 2008 it's allowed to pass a NULL pointer/nonallocated
6442 allocatable to an optional dummy, cf. 12.5.2.12. */
6443 if (fsym != NULL && fsym->attr.optional && !attr.proc_pointer
6444 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
6445 goto end_pointer_check;
6446
6447 if (attr.optional)
6448 {
6449 /* If the actual argument is an optional pointer/allocatable and
6450 the formal argument takes an nonpointer optional value,
6451 it is invalid to pass a non-present argument on, even
6452 though there is no technical reason for this in gfortran.
6453 See Fortran 2003, Section 12.4.1.6 item (7)+(8). */
6454 tree present, null_ptr, type;
6455
6456 if (attr.allocatable
6457 && (fsym == NULL || !fsym->attr.allocatable))
6458 msg = xasprintf ("Allocatable actual argument '%s' is not "
6459 "allocated or not present",
6460 e->symtree->n.sym->name);
6461 else if (attr.pointer
6462 && (fsym == NULL || !fsym->attr.pointer))
6463 msg = xasprintf ("Pointer actual argument '%s' is not "
6464 "associated or not present",
6465 e->symtree->n.sym->name);
6466 else if (attr.proc_pointer
6467 && (fsym == NULL || !fsym->attr.proc_pointer))
6468 msg = xasprintf ("Proc-pointer actual argument '%s' is not "
6469 "associated or not present",
6470 e->symtree->n.sym->name);
6471 else
6472 goto end_pointer_check;
6473
6474 present = gfc_conv_expr_present (e->symtree->n.sym);
6475 type = TREE_TYPE (present);
6476 present = fold_build2_loc (input_location, EQ_EXPR,
6477 logical_type_node, present,
6478 fold_convert (type,
6479 null_pointer_node));
6480 type = TREE_TYPE (parmse.expr);
6481 null_ptr = fold_build2_loc (input_location, EQ_EXPR,
6482 logical_type_node, parmse.expr,
6483 fold_convert (type,
6484 null_pointer_node));
6485 cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
6486 logical_type_node, present, null_ptr);
6487 }
6488 else
6489 {
6490 if (attr.allocatable
6491 && (fsym == NULL || !fsym->attr.allocatable))
6492 msg = xasprintf ("Allocatable actual argument '%s' is not "
6493 "allocated", e->symtree->n.sym->name);
6494 else if (attr.pointer
6495 && (fsym == NULL || !fsym->attr.pointer))
6496 msg = xasprintf ("Pointer actual argument '%s' is not "
6497 "associated", e->symtree->n.sym->name);
6498 else if (attr.proc_pointer
6499 && (fsym == NULL || !fsym->attr.proc_pointer))
6500 msg = xasprintf ("Proc-pointer actual argument '%s' is not "
6501 "associated", e->symtree->n.sym->name);
6502 else
6503 goto end_pointer_check;
6504
6505 tmp = parmse.expr;
6506
6507 /* If the argument is passed by value, we need to strip the
6508 INDIRECT_REF. */
6509 if (!POINTER_TYPE_P (TREE_TYPE (parmse.expr)))
6510 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
6511
6512 cond = fold_build2_loc (input_location, EQ_EXPR,
6513 logical_type_node, tmp,
6514 fold_convert (TREE_TYPE (tmp),
6515 null_pointer_node));
6516 }
6517
6518 gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
6519 msg);
6520 free (msg);
6521 }
6522 end_pointer_check:
6523
6524 /* Deferred length dummies pass the character length by reference
6525 so that the value can be returned. */
6526 if (parmse.string_length && fsym && fsym->ts.deferred)
6527 {
6528 if (INDIRECT_REF_P (parmse.string_length))
6529 /* In chains of functions/procedure calls the string_length already
6530 is a pointer to the variable holding the length. Therefore
6531 remove the deref on call. */
6532 parmse.string_length = TREE_OPERAND (parmse.string_length, 0);
6533 else
6534 {
6535 tmp = parmse.string_length;
6536 if (!VAR_P (tmp) && TREE_CODE (tmp) != COMPONENT_REF)
6537 tmp = gfc_evaluate_now (parmse.string_length, &se->pre);
6538 parmse.string_length = gfc_build_addr_expr (NULL_TREE, tmp);
6539 }
6540 }
6541
6542 /* Character strings are passed as two parameters, a length and a
6543 pointer - except for Bind(c) which only passes the pointer.
6544 An unlimited polymorphic formal argument likewise does not
6545 need the length. */
6546 if (parmse.string_length != NULL_TREE
6547 && !sym->attr.is_bind_c
6548 && !(fsym && UNLIMITED_POLY (fsym)))
6549 vec_safe_push (stringargs, parmse.string_length);
6550
6551 /* When calling __copy for character expressions to unlimited
6552 polymorphic entities, the dst argument needs a string length. */
6553 if (sym->name[0] == '_' && e && e->ts.type == BT_CHARACTER
6554 && gfc_str_startswith (sym->name, "__vtab_CHARACTER")
6555 && arg->next && arg->next->expr
6556 && (arg->next->expr->ts.type == BT_DERIVED
6557 || arg->next->expr->ts.type == BT_CLASS)
6558 && arg->next->expr->ts.u.derived->attr.unlimited_polymorphic)
6559 vec_safe_push (stringargs, parmse.string_length);
6560
6561 /* For descriptorless coarrays and assumed-shape coarray dummies, we
6562 pass the token and the offset as additional arguments. */
6563 if (fsym && e == NULL && flag_coarray == GFC_FCOARRAY_LIB
6564 && ((fsym->ts.type != BT_CLASS && fsym->attr.codimension
6565 && !fsym->attr.allocatable)
6566 || (fsym->ts.type == BT_CLASS
6567 && CLASS_DATA (fsym)->attr.codimension
6568 && !CLASS_DATA (fsym)->attr.allocatable)))
6569 {
6570 /* Token and offset. */
6571 vec_safe_push (stringargs, null_pointer_node);
6572 vec_safe_push (stringargs, build_int_cst (gfc_array_index_type, 0));
6573 gcc_assert (fsym->attr.optional);
6574 }
6575 else if (fsym && flag_coarray == GFC_FCOARRAY_LIB
6576 && ((fsym->ts.type != BT_CLASS && fsym->attr.codimension
6577 && !fsym->attr.allocatable)
6578 || (fsym->ts.type == BT_CLASS
6579 && CLASS_DATA (fsym)->attr.codimension
6580 && !CLASS_DATA (fsym)->attr.allocatable)))
6581 {
6582 tree caf_decl, caf_type;
6583 tree offset, tmp2;
6584
6585 caf_decl = gfc_get_tree_for_caf_expr (e);
6586 caf_type = TREE_TYPE (caf_decl);
6587
6588 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
6589 && (GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE
6590 || GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_POINTER))
6591 tmp = gfc_conv_descriptor_token (caf_decl);
6592 else if (DECL_LANG_SPECIFIC (caf_decl)
6593 && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
6594 tmp = GFC_DECL_TOKEN (caf_decl);
6595 else
6596 {
6597 gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
6598 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
6599 tmp = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
6600 }
6601
6602 vec_safe_push (stringargs, tmp);
6603
6604 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
6605 && GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE)
6606 offset = build_int_cst (gfc_array_index_type, 0);
6607 else if (DECL_LANG_SPECIFIC (caf_decl)
6608 && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
6609 offset = GFC_DECL_CAF_OFFSET (caf_decl);
6610 else if (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) != NULL_TREE)
6611 offset = GFC_TYPE_ARRAY_CAF_OFFSET (caf_type);
6612 else
6613 offset = build_int_cst (gfc_array_index_type, 0);
6614
6615 if (GFC_DESCRIPTOR_TYPE_P (caf_type))
6616 tmp = gfc_conv_descriptor_data_get (caf_decl);
6617 else
6618 {
6619 gcc_assert (POINTER_TYPE_P (caf_type));
6620 tmp = caf_decl;
6621 }
6622
6623 tmp2 = fsym->ts.type == BT_CLASS
6624 ? gfc_class_data_get (parmse.expr) : parmse.expr;
6625 if ((fsym->ts.type != BT_CLASS
6626 && (fsym->as->type == AS_ASSUMED_SHAPE
6627 || fsym->as->type == AS_ASSUMED_RANK))
6628 || (fsym->ts.type == BT_CLASS
6629 && (CLASS_DATA (fsym)->as->type == AS_ASSUMED_SHAPE
6630 || CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)))
6631 {
6632 if (fsym->ts.type == BT_CLASS)
6633 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (tmp2)));
6634 else
6635 {
6636 gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
6637 tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
6638 }
6639 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)));
6640 tmp2 = gfc_conv_descriptor_data_get (tmp2);
6641 }
6642 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
6643 tmp2 = gfc_conv_descriptor_data_get (tmp2);
6644 else
6645 {
6646 gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
6647 }
6648
6649 tmp = fold_build2_loc (input_location, MINUS_EXPR,
6650 gfc_array_index_type,
6651 fold_convert (gfc_array_index_type, tmp2),
6652 fold_convert (gfc_array_index_type, tmp));
6653 offset = fold_build2_loc (input_location, PLUS_EXPR,
6654 gfc_array_index_type, offset, tmp);
6655
6656 vec_safe_push (stringargs, offset);
6657 }
6658
6659 vec_safe_push (arglist, parmse.expr);
6660 }
6661 gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
6662
6663 if (comp)
6664 ts = comp->ts;
6665 else if (sym->ts.type == BT_CLASS)
6666 ts = CLASS_DATA (sym)->ts;
6667 else
6668 ts = sym->ts;
6669
6670 if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
6671 se->string_length = build_int_cst (gfc_charlen_type_node, 1);
6672 else if (ts.type == BT_CHARACTER)
6673 {
6674 if (ts.u.cl->length == NULL)
6675 {
6676 /* Assumed character length results are not allowed by C418 of the 2003
6677 standard and are trapped in resolve.c; except in the case of SPREAD
6678 (and other intrinsics?) and dummy functions. In the case of SPREAD,
6679 we take the character length of the first argument for the result.
6680 For dummies, we have to look through the formal argument list for
6681 this function and use the character length found there.*/
6682 if (ts.deferred)
6683 cl.backend_decl = gfc_create_var (gfc_charlen_type_node, "slen");
6684 else if (!sym->attr.dummy)
6685 cl.backend_decl = (*stringargs)[0];
6686 else
6687 {
6688 formal = gfc_sym_get_dummy_args (sym->ns->proc_name);
6689 for (; formal; formal = formal->next)
6690 if (strcmp (formal->sym->name, sym->name) == 0)
6691 cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
6692 }
6693 len = cl.backend_decl;
6694 }
6695 else
6696 {
6697 tree tmp;
6698
6699 /* Calculate the length of the returned string. */
6700 gfc_init_se (&parmse, NULL);
6701 if (need_interface_mapping)
6702 gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
6703 else
6704 gfc_conv_expr (&parmse, ts.u.cl->length);
6705 gfc_add_block_to_block (&se->pre, &parmse.pre);
6706 gfc_add_block_to_block (&se->post, &parmse.post);
6707 tmp = parmse.expr;
6708 /* TODO: It would be better to have the charlens as
6709 gfc_charlen_type_node already when the interface is
6710 created instead of converting it here (see PR 84615). */
6711 tmp = fold_build2_loc (input_location, MAX_EXPR,
6712 gfc_charlen_type_node,
6713 fold_convert (gfc_charlen_type_node, tmp),
6714 build_zero_cst (gfc_charlen_type_node));
6715 cl.backend_decl = tmp;
6716 }
6717
6718 /* Set up a charlen structure for it. */
6719 cl.next = NULL;
6720 cl.length = NULL;
6721 ts.u.cl = &cl;
6722
6723 len = cl.backend_decl;
6724 }
6725
6726 byref = (comp && (comp->attr.dimension
6727 || (comp->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)))
6728 || (!comp && gfc_return_by_reference (sym));
6729 if (byref)
6730 {
6731 if (se->direct_byref)
6732 {
6733 /* Sometimes, too much indirection can be applied; e.g. for
6734 function_result = array_valued_recursive_function. */
6735 if (TREE_TYPE (TREE_TYPE (se->expr))
6736 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
6737 && GFC_DESCRIPTOR_TYPE_P
6738 (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
6739 se->expr = build_fold_indirect_ref_loc (input_location,
6740 se->expr);
6741
6742 /* If the lhs of an assignment x = f(..) is allocatable and
6743 f2003 is allowed, we must do the automatic reallocation.
6744 TODO - deal with intrinsics, without using a temporary. */
6745 if (flag_realloc_lhs
6746 && se->ss && se->ss->loop_chain
6747 && se->ss->loop_chain->is_alloc_lhs
6748 && !expr->value.function.isym
6749 && sym->result->as != NULL)
6750 {
6751 /* Evaluate the bounds of the result, if known. */
6752 gfc_set_loop_bounds_from_array_spec (&mapping, se,
6753 sym->result->as);
6754
6755 /* Perform the automatic reallocation. */
6756 tmp = gfc_alloc_allocatable_for_assignment (se->loop,
6757 expr, NULL);
6758 gfc_add_expr_to_block (&se->pre, tmp);
6759
6760 /* Pass the temporary as the first argument. */
6761 result = info->descriptor;
6762 }
6763 else
6764 result = build_fold_indirect_ref_loc (input_location,
6765 se->expr);
6766 vec_safe_push (retargs, se->expr);
6767 }
6768 else if (comp && comp->attr.dimension)
6769 {
6770 gcc_assert (se->loop && info);
6771
6772 /* Set the type of the array. */
6773 tmp = gfc_typenode_for_spec (&comp->ts);
6774 gcc_assert (se->ss->dimen == se->loop->dimen);
6775
6776 /* Evaluate the bounds of the result, if known. */
6777 gfc_set_loop_bounds_from_array_spec (&mapping, se, comp->as);
6778
6779 /* If the lhs of an assignment x = f(..) is allocatable and
6780 f2003 is allowed, we must not generate the function call
6781 here but should just send back the results of the mapping.
6782 This is signalled by the function ss being flagged. */
6783 if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
6784 {
6785 gfc_free_interface_mapping (&mapping);
6786 return has_alternate_specifier;
6787 }
6788
6789 /* Create a temporary to store the result. In case the function
6790 returns a pointer, the temporary will be a shallow copy and
6791 mustn't be deallocated. */
6792 callee_alloc = comp->attr.allocatable || comp->attr.pointer;
6793 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
6794 tmp, NULL_TREE, false,
6795 !comp->attr.pointer, callee_alloc,
6796 &se->ss->info->expr->where);
6797
6798 /* Pass the temporary as the first argument. */
6799 result = info->descriptor;
6800 tmp = gfc_build_addr_expr (NULL_TREE, result);
6801 vec_safe_push (retargs, tmp);
6802 }
6803 else if (!comp && sym->result->attr.dimension)
6804 {
6805 gcc_assert (se->loop && info);
6806
6807 /* Set the type of the array. */
6808 tmp = gfc_typenode_for_spec (&ts);
6809 gcc_assert (se->ss->dimen == se->loop->dimen);
6810
6811 /* Evaluate the bounds of the result, if known. */
6812 gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
6813
6814 /* If the lhs of an assignment x = f(..) is allocatable and
6815 f2003 is allowed, we must not generate the function call
6816 here but should just send back the results of the mapping.
6817 This is signalled by the function ss being flagged. */
6818 if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
6819 {
6820 gfc_free_interface_mapping (&mapping);
6821 return has_alternate_specifier;
6822 }
6823
6824 /* Create a temporary to store the result. In case the function
6825 returns a pointer, the temporary will be a shallow copy and
6826 mustn't be deallocated. */
6827 callee_alloc = sym->attr.allocatable || sym->attr.pointer;
6828 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
6829 tmp, NULL_TREE, false,
6830 !sym->attr.pointer, callee_alloc,
6831 &se->ss->info->expr->where);
6832
6833 /* Pass the temporary as the first argument. */
6834 result = info->descriptor;
6835 tmp = gfc_build_addr_expr (NULL_TREE, result);
6836 vec_safe_push (retargs, tmp);
6837 }
6838 else if (ts.type == BT_CHARACTER)
6839 {
6840 /* Pass the string length. */
6841 type = gfc_get_character_type (ts.kind, ts.u.cl);
6842 type = build_pointer_type (type);
6843
6844 /* Emit a DECL_EXPR for the VLA type. */
6845 tmp = TREE_TYPE (type);
6846 if (TYPE_SIZE (tmp)
6847 && TREE_CODE (TYPE_SIZE (tmp)) != INTEGER_CST)
6848 {
6849 tmp = build_decl (input_location, TYPE_DECL, NULL_TREE, tmp);
6850 DECL_ARTIFICIAL (tmp) = 1;
6851 DECL_IGNORED_P (tmp) = 1;
6852 tmp = fold_build1_loc (input_location, DECL_EXPR,
6853 TREE_TYPE (tmp), tmp);
6854 gfc_add_expr_to_block (&se->pre, tmp);
6855 }
6856
6857 /* Return an address to a char[0:len-1]* temporary for
6858 character pointers. */
6859 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
6860 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
6861 {
6862 var = gfc_create_var (type, "pstr");
6863
6864 if ((!comp && sym->attr.allocatable)
6865 || (comp && comp->attr.allocatable))
6866 {
6867 gfc_add_modify (&se->pre, var,
6868 fold_convert (TREE_TYPE (var),
6869 null_pointer_node));
6870 tmp = gfc_call_free (var);
6871 gfc_add_expr_to_block (&se->post, tmp);
6872 }
6873
6874 /* Provide an address expression for the function arguments. */
6875 var = gfc_build_addr_expr (NULL_TREE, var);
6876 }
6877 else
6878 var = gfc_conv_string_tmp (se, type, len);
6879
6880 vec_safe_push (retargs, var);
6881 }
6882 else
6883 {
6884 gcc_assert (flag_f2c && ts.type == BT_COMPLEX);
6885
6886 type = gfc_get_complex_type (ts.kind);
6887 var = gfc_build_addr_expr (NULL_TREE, gfc_create_var (type, "cmplx"));
6888 vec_safe_push (retargs, var);
6889 }
6890
6891 /* Add the string length to the argument list. */
6892 if (ts.type == BT_CHARACTER && ts.deferred)
6893 {
6894 tmp = len;
6895 if (!VAR_P (tmp))
6896 tmp = gfc_evaluate_now (len, &se->pre);
6897 TREE_STATIC (tmp) = 1;
6898 gfc_add_modify (&se->pre, tmp,
6899 build_int_cst (TREE_TYPE (tmp), 0));
6900 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
6901 vec_safe_push (retargs, tmp);
6902 }
6903 else if (ts.type == BT_CHARACTER)
6904 vec_safe_push (retargs, len);
6905 }
6906 gfc_free_interface_mapping (&mapping);
6907
6908 /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS. */
6909 arglen = (vec_safe_length (arglist) + vec_safe_length (optionalargs)
6910 + vec_safe_length (stringargs) + vec_safe_length (append_args));
6911 vec_safe_reserve (retargs, arglen);
6912
6913 /* Add the return arguments. */
6914 vec_safe_splice (retargs, arglist);
6915
6916 /* Add the hidden present status for optional+value to the arguments. */
6917 vec_safe_splice (retargs, optionalargs);
6918
6919 /* Add the hidden string length parameters to the arguments. */
6920 vec_safe_splice (retargs, stringargs);
6921
6922 /* We may want to append extra arguments here. This is used e.g. for
6923 calls to libgfortran_matmul_??, which need extra information. */
6924 vec_safe_splice (retargs, append_args);
6925
6926 arglist = retargs;
6927
6928 /* Generate the actual call. */
6929 if (base_object == NULL_TREE)
6930 conv_function_val (se, sym, expr, args);
6931 else
6932 conv_base_obj_fcn_val (se, base_object, expr);
6933
6934 /* If there are alternate return labels, function type should be
6935 integer. Can't modify the type in place though, since it can be shared
6936 with other functions. For dummy arguments, the typing is done to
6937 this result, even if it has to be repeated for each call. */
6938 if (has_alternate_specifier
6939 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
6940 {
6941 if (!sym->attr.dummy)
6942 {
6943 TREE_TYPE (sym->backend_decl)
6944 = build_function_type (integer_type_node,
6945 TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
6946 se->expr = gfc_build_addr_expr (NULL_TREE, sym->backend_decl);
6947 }
6948 else
6949 TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
6950 }
6951
6952 fntype = TREE_TYPE (TREE_TYPE (se->expr));
6953 se->expr = build_call_vec (TREE_TYPE (fntype), se->expr, arglist);
6954
6955 /* Allocatable scalar function results must be freed and nullified
6956 after use. This necessitates the creation of a temporary to
6957 hold the result to prevent duplicate calls. */
6958 if (!byref && sym->ts.type != BT_CHARACTER
6959 && ((sym->attr.allocatable && !sym->attr.dimension && !comp)
6960 || (comp && comp->attr.allocatable && !comp->attr.dimension)))
6961 {
6962 tmp = gfc_create_var (TREE_TYPE (se->expr), NULL);
6963 gfc_add_modify (&se->pre, tmp, se->expr);
6964 se->expr = tmp;
6965 tmp = gfc_call_free (tmp);
6966 gfc_add_expr_to_block (&post, tmp);
6967 gfc_add_modify (&post, se->expr, build_int_cst (TREE_TYPE (se->expr), 0));
6968 }
6969
6970 /* If we have a pointer function, but we don't want a pointer, e.g.
6971 something like
6972 x = f()
6973 where f is pointer valued, we have to dereference the result. */
6974 if (!se->want_pointer && !byref
6975 && ((!comp && (sym->attr.pointer || sym->attr.allocatable))
6976 || (comp && (comp->attr.pointer || comp->attr.allocatable))))
6977 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
6978
6979 /* f2c calling conventions require a scalar default real function to
6980 return a double precision result. Convert this back to default
6981 real. We only care about the cases that can happen in Fortran 77.
6982 */
6983 if (flag_f2c && sym->ts.type == BT_REAL
6984 && sym->ts.kind == gfc_default_real_kind
6985 && !sym->attr.always_explicit)
6986 se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
6987
6988 /* A pure function may still have side-effects - it may modify its
6989 parameters. */
6990 TREE_SIDE_EFFECTS (se->expr) = 1;
6991 #if 0
6992 if (!sym->attr.pure)
6993 TREE_SIDE_EFFECTS (se->expr) = 1;
6994 #endif
6995
6996 if (byref)
6997 {
6998 /* Add the function call to the pre chain. There is no expression. */
6999 gfc_add_expr_to_block (&se->pre, se->expr);
7000 se->expr = NULL_TREE;
7001
7002 if (!se->direct_byref)
7003 {
7004 if ((sym->attr.dimension && !comp) || (comp && comp->attr.dimension))
7005 {
7006 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
7007 {
7008 /* Check the data pointer hasn't been modified. This would
7009 happen in a function returning a pointer. */
7010 tmp = gfc_conv_descriptor_data_get (info->descriptor);
7011 tmp = fold_build2_loc (input_location, NE_EXPR,
7012 logical_type_node,
7013 tmp, info->data);
7014 gfc_trans_runtime_check (true, false, tmp, &se->pre, NULL,
7015 gfc_msg_fault);
7016 }
7017 se->expr = info->descriptor;
7018 /* Bundle in the string length. */
7019 se->string_length = len;
7020 }
7021 else if (ts.type == BT_CHARACTER)
7022 {
7023 /* Dereference for character pointer results. */
7024 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
7025 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
7026 se->expr = build_fold_indirect_ref_loc (input_location, var);
7027 else
7028 se->expr = var;
7029
7030 se->string_length = len;
7031 }
7032 else
7033 {
7034 gcc_assert (ts.type == BT_COMPLEX && flag_f2c);
7035 se->expr = build_fold_indirect_ref_loc (input_location, var);
7036 }
7037 }
7038 }
7039
7040 /* Associate the rhs class object's meta-data with the result, when the
7041 result is a temporary. */
7042 if (args && args->expr && args->expr->ts.type == BT_CLASS
7043 && sym->ts.type == BT_CLASS && result != NULL_TREE && DECL_P (result)
7044 && !GFC_CLASS_TYPE_P (TREE_TYPE (result)))
7045 {
7046 gfc_se parmse;
7047 gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (args->expr);
7048
7049 gfc_init_se (&parmse, NULL);
7050 parmse.data_not_needed = 1;
7051 gfc_conv_expr (&parmse, class_expr);
7052 if (!DECL_LANG_SPECIFIC (result))
7053 gfc_allocate_lang_decl (result);
7054 GFC_DECL_SAVED_DESCRIPTOR (result) = parmse.expr;
7055 gfc_free_expr (class_expr);
7056 /* -fcheck= can add diagnostic code, which has to be placed before
7057 the call. */
7058 if (parmse.pre.head != NULL)
7059 gfc_add_expr_to_block (&se->pre, parmse.pre.head);
7060 gcc_assert (parmse.post.head == NULL_TREE);
7061 }
7062
7063 /* Follow the function call with the argument post block. */
7064 if (byref)
7065 {
7066 gfc_add_block_to_block (&se->pre, &post);
7067
7068 /* Transformational functions of derived types with allocatable
7069 components must have the result allocatable components copied when the
7070 argument is actually given. */
7071 arg = expr->value.function.actual;
7072 if (result && arg && expr->rank
7073 && expr->value.function.isym
7074 && expr->value.function.isym->transformational
7075 && arg->expr
7076 && arg->expr->ts.type == BT_DERIVED
7077 && arg->expr->ts.u.derived->attr.alloc_comp)
7078 {
7079 tree tmp2;
7080 /* Copy the allocatable components. We have to use a
7081 temporary here to prevent source allocatable components
7082 from being corrupted. */
7083 tmp2 = gfc_evaluate_now (result, &se->pre);
7084 tmp = gfc_copy_alloc_comp (arg->expr->ts.u.derived,
7085 result, tmp2, expr->rank, 0);
7086 gfc_add_expr_to_block (&se->pre, tmp);
7087 tmp = gfc_copy_allocatable_data (result, tmp2, TREE_TYPE(tmp2),
7088 expr->rank);
7089 gfc_add_expr_to_block (&se->pre, tmp);
7090
7091 /* Finally free the temporary's data field. */
7092 tmp = gfc_conv_descriptor_data_get (tmp2);
7093 tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
7094 NULL_TREE, NULL_TREE, true,
7095 NULL, GFC_CAF_COARRAY_NOCOARRAY);
7096 gfc_add_expr_to_block (&se->pre, tmp);
7097 }
7098 }
7099 else
7100 {
7101 /* For a function with a class array result, save the result as
7102 a temporary, set the info fields needed by the scalarizer and
7103 call the finalization function of the temporary. Note that the
7104 nullification of allocatable components needed by the result
7105 is done in gfc_trans_assignment_1. */
7106 if (expr && ((gfc_is_class_array_function (expr)
7107 && se->ss && se->ss->loop)
7108 || gfc_is_alloc_class_scalar_function (expr))
7109 && se->expr && GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
7110 && expr->must_finalize)
7111 {
7112 tree final_fndecl;
7113 tree is_final;
7114 int n;
7115 if (se->ss && se->ss->loop)
7116 {
7117 gfc_add_block_to_block (&se->ss->loop->pre, &se->pre);
7118 se->expr = gfc_evaluate_now (se->expr, &se->ss->loop->pre);
7119 tmp = gfc_class_data_get (se->expr);
7120 info->descriptor = tmp;
7121 info->data = gfc_conv_descriptor_data_get (tmp);
7122 info->offset = gfc_conv_descriptor_offset_get (tmp);
7123 for (n = 0; n < se->ss->loop->dimen; n++)
7124 {
7125 tree dim = gfc_rank_cst[n];
7126 se->ss->loop->to[n] = gfc_conv_descriptor_ubound_get (tmp, dim);
7127 se->ss->loop->from[n] = gfc_conv_descriptor_lbound_get (tmp, dim);
7128 }
7129 }
7130 else
7131 {
7132 /* TODO Eliminate the doubling of temporaries. This
7133 one is necessary to ensure no memory leakage. */
7134 se->expr = gfc_evaluate_now (se->expr, &se->pre);
7135 tmp = gfc_class_data_get (se->expr);
7136 tmp = gfc_conv_scalar_to_descriptor (se, tmp,
7137 CLASS_DATA (expr->value.function.esym->result)->attr);
7138 }
7139
7140 if ((gfc_is_class_array_function (expr)
7141 || gfc_is_alloc_class_scalar_function (expr))
7142 && CLASS_DATA (expr->value.function.esym->result)->attr.pointer)
7143 goto no_finalization;
7144
7145 final_fndecl = gfc_class_vtab_final_get (se->expr);
7146 is_final = fold_build2_loc (input_location, NE_EXPR,
7147 logical_type_node,
7148 final_fndecl,
7149 fold_convert (TREE_TYPE (final_fndecl),
7150 null_pointer_node));
7151 final_fndecl = build_fold_indirect_ref_loc (input_location,
7152 final_fndecl);
7153 tmp = build_call_expr_loc (input_location,
7154 final_fndecl, 3,
7155 gfc_build_addr_expr (NULL, tmp),
7156 gfc_class_vtab_size_get (se->expr),
7157 boolean_false_node);
7158 tmp = fold_build3_loc (input_location, COND_EXPR,
7159 void_type_node, is_final, tmp,
7160 build_empty_stmt (input_location));
7161
7162 if (se->ss && se->ss->loop)
7163 {
7164 gfc_prepend_expr_to_block (&se->ss->loop->post, tmp);
7165 tmp = fold_build2_loc (input_location, NE_EXPR,
7166 logical_type_node,
7167 info->data,
7168 fold_convert (TREE_TYPE (info->data),
7169 null_pointer_node));
7170 tmp = fold_build3_loc (input_location, COND_EXPR,
7171 void_type_node, tmp,
7172 gfc_call_free (info->data),
7173 build_empty_stmt (input_location));
7174 gfc_add_expr_to_block (&se->ss->loop->post, tmp);
7175 }
7176 else
7177 {
7178 tree classdata;
7179 gfc_prepend_expr_to_block (&se->post, tmp);
7180 classdata = gfc_class_data_get (se->expr);
7181 tmp = fold_build2_loc (input_location, NE_EXPR,
7182 logical_type_node,
7183 classdata,
7184 fold_convert (TREE_TYPE (classdata),
7185 null_pointer_node));
7186 tmp = fold_build3_loc (input_location, COND_EXPR,
7187 void_type_node, tmp,
7188 gfc_call_free (classdata),
7189 build_empty_stmt (input_location));
7190 gfc_add_expr_to_block (&se->post, tmp);
7191 }
7192 }
7193
7194 no_finalization:
7195 gfc_add_block_to_block (&se->post, &post);
7196 }
7197
7198 return has_alternate_specifier;
7199 }
7200
7201
7202 /* Fill a character string with spaces. */
7203
7204 static tree
7205 fill_with_spaces (tree start, tree type, tree size)
7206 {
7207 stmtblock_t block, loop;
7208 tree i, el, exit_label, cond, tmp;
7209
7210 /* For a simple char type, we can call memset(). */
7211 if (compare_tree_int (TYPE_SIZE_UNIT (type), 1) == 0)
7212 return build_call_expr_loc (input_location,
7213 builtin_decl_explicit (BUILT_IN_MEMSET),
7214 3, start,
7215 build_int_cst (gfc_get_int_type (gfc_c_int_kind),
7216 lang_hooks.to_target_charset (' ')),
7217 fold_convert (size_type_node, size));
7218
7219 /* Otherwise, we use a loop:
7220 for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
7221 *el = (type) ' ';
7222 */
7223
7224 /* Initialize variables. */
7225 gfc_init_block (&block);
7226 i = gfc_create_var (sizetype, "i");
7227 gfc_add_modify (&block, i, fold_convert (sizetype, size));
7228 el = gfc_create_var (build_pointer_type (type), "el");
7229 gfc_add_modify (&block, el, fold_convert (TREE_TYPE (el), start));
7230 exit_label = gfc_build_label_decl (NULL_TREE);
7231 TREE_USED (exit_label) = 1;
7232
7233
7234 /* Loop body. */
7235 gfc_init_block (&loop);
7236
7237 /* Exit condition. */
7238 cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, i,
7239 build_zero_cst (sizetype));
7240 tmp = build1_v (GOTO_EXPR, exit_label);
7241 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
7242 build_empty_stmt (input_location));
7243 gfc_add_expr_to_block (&loop, tmp);
7244
7245 /* Assignment. */
7246 gfc_add_modify (&loop,
7247 fold_build1_loc (input_location, INDIRECT_REF, type, el),
7248 build_int_cst (type, lang_hooks.to_target_charset (' ')));
7249
7250 /* Increment loop variables. */
7251 gfc_add_modify (&loop, i,
7252 fold_build2_loc (input_location, MINUS_EXPR, sizetype, i,
7253 TYPE_SIZE_UNIT (type)));
7254 gfc_add_modify (&loop, el,
7255 fold_build_pointer_plus_loc (input_location,
7256 el, TYPE_SIZE_UNIT (type)));
7257
7258 /* Making the loop... actually loop! */
7259 tmp = gfc_finish_block (&loop);
7260 tmp = build1_v (LOOP_EXPR, tmp);
7261 gfc_add_expr_to_block (&block, tmp);
7262
7263 /* The exit label. */
7264 tmp = build1_v (LABEL_EXPR, exit_label);
7265 gfc_add_expr_to_block (&block, tmp);
7266
7267
7268 return gfc_finish_block (&block);
7269 }
7270
7271
7272 /* Generate code to copy a string. */
7273
7274 void
7275 gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
7276 int dkind, tree slength, tree src, int skind)
7277 {
7278 tree tmp, dlen, slen;
7279 tree dsc;
7280 tree ssc;
7281 tree cond;
7282 tree cond2;
7283 tree tmp2;
7284 tree tmp3;
7285 tree tmp4;
7286 tree chartype;
7287 stmtblock_t tempblock;
7288
7289 gcc_assert (dkind == skind);
7290
7291 if (slength != NULL_TREE)
7292 {
7293 slen = gfc_evaluate_now (fold_convert (gfc_charlen_type_node, slength), block);
7294 ssc = gfc_string_to_single_character (slen, src, skind);
7295 }
7296 else
7297 {
7298 slen = build_one_cst (gfc_charlen_type_node);
7299 ssc = src;
7300 }
7301
7302 if (dlength != NULL_TREE)
7303 {
7304 dlen = gfc_evaluate_now (fold_convert (gfc_charlen_type_node, dlength), block);
7305 dsc = gfc_string_to_single_character (dlen, dest, dkind);
7306 }
7307 else
7308 {
7309 dlen = build_one_cst (gfc_charlen_type_node);
7310 dsc = dest;
7311 }
7312
7313 /* Assign directly if the types are compatible. */
7314 if (dsc != NULL_TREE && ssc != NULL_TREE
7315 && TREE_TYPE (dsc) == TREE_TYPE (ssc))
7316 {
7317 gfc_add_modify (block, dsc, ssc);
7318 return;
7319 }
7320
7321 /* The string copy algorithm below generates code like
7322
7323 if (destlen > 0)
7324 {
7325 if (srclen < destlen)
7326 {
7327 memmove (dest, src, srclen);
7328 // Pad with spaces.
7329 memset (&dest[srclen], ' ', destlen - srclen);
7330 }
7331 else
7332 {
7333 // Truncate if too long.
7334 memmove (dest, src, destlen);
7335 }
7336 }
7337 */
7338
7339 /* Do nothing if the destination length is zero. */
7340 cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node, dlen,
7341 build_zero_cst (TREE_TYPE (dlen)));
7342
7343 /* For non-default character kinds, we have to multiply the string
7344 length by the base type size. */
7345 chartype = gfc_get_char_type (dkind);
7346 slen = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (slen),
7347 slen,
7348 fold_convert (TREE_TYPE (slen),
7349 TYPE_SIZE_UNIT (chartype)));
7350 dlen = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (dlen),
7351 dlen,
7352 fold_convert (TREE_TYPE (dlen),
7353 TYPE_SIZE_UNIT (chartype)));
7354
7355 if (dlength && POINTER_TYPE_P (TREE_TYPE (dest)))
7356 dest = fold_convert (pvoid_type_node, dest);
7357 else
7358 dest = gfc_build_addr_expr (pvoid_type_node, dest);
7359
7360 if (slength && POINTER_TYPE_P (TREE_TYPE (src)))
7361 src = fold_convert (pvoid_type_node, src);
7362 else
7363 src = gfc_build_addr_expr (pvoid_type_node, src);
7364
7365 /* Truncate string if source is too long. */
7366 cond2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node, slen,
7367 dlen);
7368
7369 /* Copy and pad with spaces. */
7370 tmp3 = build_call_expr_loc (input_location,
7371 builtin_decl_explicit (BUILT_IN_MEMMOVE),
7372 3, dest, src,
7373 fold_convert (size_type_node, slen));
7374
7375 /* Wstringop-overflow appears at -O3 even though this warning is not
7376 explicitly available in fortran nor can it be switched off. If the
7377 source length is a constant, its negative appears as a very large
7378 postive number and triggers the warning in BUILTIN_MEMSET. Fixing
7379 the result of the MINUS_EXPR suppresses this spurious warning. */
7380 tmp = fold_build2_loc (input_location, MINUS_EXPR,
7381 TREE_TYPE(dlen), dlen, slen);
7382 if (slength && TREE_CONSTANT (slength))
7383 tmp = gfc_evaluate_now (tmp, block);
7384
7385 tmp4 = fold_build_pointer_plus_loc (input_location, dest, slen);
7386 tmp4 = fill_with_spaces (tmp4, chartype, tmp);
7387
7388 gfc_init_block (&tempblock);
7389 gfc_add_expr_to_block (&tempblock, tmp3);
7390 gfc_add_expr_to_block (&tempblock, tmp4);
7391 tmp3 = gfc_finish_block (&tempblock);
7392
7393 /* The truncated memmove if the slen >= dlen. */
7394 tmp2 = build_call_expr_loc (input_location,
7395 builtin_decl_explicit (BUILT_IN_MEMMOVE),
7396 3, dest, src,
7397 fold_convert (size_type_node, dlen));
7398
7399 /* The whole copy_string function is there. */
7400 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
7401 tmp3, tmp2);
7402 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
7403 build_empty_stmt (input_location));
7404 gfc_add_expr_to_block (block, tmp);
7405 }
7406
7407
7408 /* Translate a statement function.
7409 The value of a statement function reference is obtained by evaluating the
7410 expression using the values of the actual arguments for the values of the
7411 corresponding dummy arguments. */
7412
7413 static void
7414 gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
7415 {
7416 gfc_symbol *sym;
7417 gfc_symbol *fsym;
7418 gfc_formal_arglist *fargs;
7419 gfc_actual_arglist *args;
7420 gfc_se lse;
7421 gfc_se rse;
7422 gfc_saved_var *saved_vars;
7423 tree *temp_vars;
7424 tree type;
7425 tree tmp;
7426 int n;
7427
7428 sym = expr->symtree->n.sym;
7429 args = expr->value.function.actual;
7430 gfc_init_se (&lse, NULL);
7431 gfc_init_se (&rse, NULL);
7432
7433 n = 0;
7434 for (fargs = gfc_sym_get_dummy_args (sym); fargs; fargs = fargs->next)
7435 n++;
7436 saved_vars = XCNEWVEC (gfc_saved_var, n);
7437 temp_vars = XCNEWVEC (tree, n);
7438
7439 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
7440 fargs = fargs->next, n++)
7441 {
7442 /* Each dummy shall be specified, explicitly or implicitly, to be
7443 scalar. */
7444 gcc_assert (fargs->sym->attr.dimension == 0);
7445 fsym = fargs->sym;
7446
7447 if (fsym->ts.type == BT_CHARACTER)
7448 {
7449 /* Copy string arguments. */
7450 tree arglen;
7451
7452 gcc_assert (fsym->ts.u.cl && fsym->ts.u.cl->length
7453 && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT);
7454
7455 /* Create a temporary to hold the value. */
7456 if (fsym->ts.u.cl->backend_decl == NULL_TREE)
7457 fsym->ts.u.cl->backend_decl
7458 = gfc_conv_constant_to_tree (fsym->ts.u.cl->length);
7459
7460 type = gfc_get_character_type (fsym->ts.kind, fsym->ts.u.cl);
7461 temp_vars[n] = gfc_create_var (type, fsym->name);
7462
7463 arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
7464
7465 gfc_conv_expr (&rse, args->expr);
7466 gfc_conv_string_parameter (&rse);
7467 gfc_add_block_to_block (&se->pre, &lse.pre);
7468 gfc_add_block_to_block (&se->pre, &rse.pre);
7469
7470 gfc_trans_string_copy (&se->pre, arglen, temp_vars[n], fsym->ts.kind,
7471 rse.string_length, rse.expr, fsym->ts.kind);
7472 gfc_add_block_to_block (&se->pre, &lse.post);
7473 gfc_add_block_to_block (&se->pre, &rse.post);
7474 }
7475 else
7476 {
7477 /* For everything else, just evaluate the expression. */
7478
7479 /* Create a temporary to hold the value. */
7480 type = gfc_typenode_for_spec (&fsym->ts);
7481 temp_vars[n] = gfc_create_var (type, fsym->name);
7482
7483 gfc_conv_expr (&lse, args->expr);
7484
7485 gfc_add_block_to_block (&se->pre, &lse.pre);
7486 gfc_add_modify (&se->pre, temp_vars[n], lse.expr);
7487 gfc_add_block_to_block (&se->pre, &lse.post);
7488 }
7489
7490 args = args->next;
7491 }
7492
7493 /* Use the temporary variables in place of the real ones. */
7494 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
7495 fargs = fargs->next, n++)
7496 gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
7497
7498 gfc_conv_expr (se, sym->value);
7499
7500 if (sym->ts.type == BT_CHARACTER)
7501 {
7502 gfc_conv_const_charlen (sym->ts.u.cl);
7503
7504 /* Force the expression to the correct length. */
7505 if (!INTEGER_CST_P (se->string_length)
7506 || tree_int_cst_lt (se->string_length,
7507 sym->ts.u.cl->backend_decl))
7508 {
7509 type = gfc_get_character_type (sym->ts.kind, sym->ts.u.cl);
7510 tmp = gfc_create_var (type, sym->name);
7511 tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
7512 gfc_trans_string_copy (&se->pre, sym->ts.u.cl->backend_decl, tmp,
7513 sym->ts.kind, se->string_length, se->expr,
7514 sym->ts.kind);
7515 se->expr = tmp;
7516 }
7517 se->string_length = sym->ts.u.cl->backend_decl;
7518 }
7519
7520 /* Restore the original variables. */
7521 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
7522 fargs = fargs->next, n++)
7523 gfc_restore_sym (fargs->sym, &saved_vars[n]);
7524 free (temp_vars);
7525 free (saved_vars);
7526 }
7527
7528
7529 /* Translate a function expression. */
7530
7531 static void
7532 gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
7533 {
7534 gfc_symbol *sym;
7535
7536 if (expr->value.function.isym)
7537 {
7538 gfc_conv_intrinsic_function (se, expr);
7539 return;
7540 }
7541
7542 /* expr.value.function.esym is the resolved (specific) function symbol for
7543 most functions. However this isn't set for dummy procedures. */
7544 sym = expr->value.function.esym;
7545 if (!sym)
7546 sym = expr->symtree->n.sym;
7547
7548 /* The IEEE_ARITHMETIC functions are caught here. */
7549 if (sym->from_intmod == INTMOD_IEEE_ARITHMETIC)
7550 if (gfc_conv_ieee_arithmetic_function (se, expr))
7551 return;
7552
7553 /* We distinguish statement functions from general functions to improve
7554 runtime performance. */
7555 if (sym->attr.proc == PROC_ST_FUNCTION)
7556 {
7557 gfc_conv_statement_function (se, expr);
7558 return;
7559 }
7560
7561 gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
7562 NULL);
7563 }
7564
7565
7566 /* Determine whether the given EXPR_CONSTANT is a zero initializer. */
7567
7568 static bool
7569 is_zero_initializer_p (gfc_expr * expr)
7570 {
7571 if (expr->expr_type != EXPR_CONSTANT)
7572 return false;
7573
7574 /* We ignore constants with prescribed memory representations for now. */
7575 if (expr->representation.string)
7576 return false;
7577
7578 switch (expr->ts.type)
7579 {
7580 case BT_INTEGER:
7581 return mpz_cmp_si (expr->value.integer, 0) == 0;
7582
7583 case BT_REAL:
7584 return mpfr_zero_p (expr->value.real)
7585 && MPFR_SIGN (expr->value.real) >= 0;
7586
7587 case BT_LOGICAL:
7588 return expr->value.logical == 0;
7589
7590 case BT_COMPLEX:
7591 return mpfr_zero_p (mpc_realref (expr->value.complex))
7592 && MPFR_SIGN (mpc_realref (expr->value.complex)) >= 0
7593 && mpfr_zero_p (mpc_imagref (expr->value.complex))
7594 && MPFR_SIGN (mpc_imagref (expr->value.complex)) >= 0;
7595
7596 default:
7597 break;
7598 }
7599 return false;
7600 }
7601
7602
7603 static void
7604 gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
7605 {
7606 gfc_ss *ss;
7607
7608 ss = se->ss;
7609 gcc_assert (ss != NULL && ss != gfc_ss_terminator);
7610 gcc_assert (ss->info->expr == expr && ss->info->type == GFC_SS_CONSTRUCTOR);
7611
7612 gfc_conv_tmp_array_ref (se);
7613 }
7614
7615
7616 /* Build a static initializer. EXPR is the expression for the initial value.
7617 The other parameters describe the variable of the component being
7618 initialized. EXPR may be null. */
7619
7620 tree
7621 gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
7622 bool array, bool pointer, bool procptr)
7623 {
7624 gfc_se se;
7625
7626 if (flag_coarray != GFC_FCOARRAY_LIB && ts->type == BT_DERIVED
7627 && ts->u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
7628 && ts->u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
7629 return build_constructor (type, NULL);
7630
7631 if (!(expr || pointer || procptr))
7632 return NULL_TREE;
7633
7634 /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
7635 (these are the only two iso_c_binding derived types that can be
7636 used as initialization expressions). If so, we need to modify
7637 the 'expr' to be that for a (void *). */
7638 if (expr != NULL && expr->ts.type == BT_DERIVED
7639 && expr->ts.is_iso_c && expr->ts.u.derived)
7640 {
7641 if (TREE_CODE (type) == ARRAY_TYPE)
7642 return build_constructor (type, NULL);
7643 else if (POINTER_TYPE_P (type))
7644 return build_int_cst (type, 0);
7645 else
7646 gcc_unreachable ();
7647 }
7648
7649 if (array && !procptr)
7650 {
7651 tree ctor;
7652 /* Arrays need special handling. */
7653 if (pointer)
7654 ctor = gfc_build_null_descriptor (type);
7655 /* Special case assigning an array to zero. */
7656 else if (is_zero_initializer_p (expr))
7657 ctor = build_constructor (type, NULL);
7658 else
7659 ctor = gfc_conv_array_initializer (type, expr);
7660 TREE_STATIC (ctor) = 1;
7661 return ctor;
7662 }
7663 else if (pointer || procptr)
7664 {
7665 if (ts->type == BT_CLASS && !procptr)
7666 {
7667 gfc_init_se (&se, NULL);
7668 gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
7669 gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
7670 TREE_STATIC (se.expr) = 1;
7671 return se.expr;
7672 }
7673 else if (!expr || expr->expr_type == EXPR_NULL)
7674 return fold_convert (type, null_pointer_node);
7675 else
7676 {
7677 gfc_init_se (&se, NULL);
7678 se.want_pointer = 1;
7679 gfc_conv_expr (&se, expr);
7680 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
7681 return se.expr;
7682 }
7683 }
7684 else
7685 {
7686 switch (ts->type)
7687 {
7688 case_bt_struct:
7689 case BT_CLASS:
7690 gfc_init_se (&se, NULL);
7691 if (ts->type == BT_CLASS && expr->expr_type == EXPR_NULL)
7692 gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
7693 else
7694 gfc_conv_structure (&se, expr, 1);
7695 gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
7696 TREE_STATIC (se.expr) = 1;
7697 return se.expr;
7698
7699 case BT_CHARACTER:
7700 {
7701 tree ctor = gfc_conv_string_init (ts->u.cl->backend_decl,expr);
7702 TREE_STATIC (ctor) = 1;
7703 return ctor;
7704 }
7705
7706 default:
7707 gfc_init_se (&se, NULL);
7708 gfc_conv_constant (&se, expr);
7709 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
7710 return se.expr;
7711 }
7712 }
7713 }
7714
7715 static tree
7716 gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
7717 {
7718 gfc_se rse;
7719 gfc_se lse;
7720 gfc_ss *rss;
7721 gfc_ss *lss;
7722 gfc_array_info *lss_array;
7723 stmtblock_t body;
7724 stmtblock_t block;
7725 gfc_loopinfo loop;
7726 int n;
7727 tree tmp;
7728
7729 gfc_start_block (&block);
7730
7731 /* Initialize the scalarizer. */
7732 gfc_init_loopinfo (&loop);
7733
7734 gfc_init_se (&lse, NULL);
7735 gfc_init_se (&rse, NULL);
7736
7737 /* Walk the rhs. */
7738 rss = gfc_walk_expr (expr);
7739 if (rss == gfc_ss_terminator)
7740 /* The rhs is scalar. Add a ss for the expression. */
7741 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr);
7742
7743 /* Create a SS for the destination. */
7744 lss = gfc_get_array_ss (gfc_ss_terminator, NULL, cm->as->rank,
7745 GFC_SS_COMPONENT);
7746 lss_array = &lss->info->data.array;
7747 lss_array->shape = gfc_get_shape (cm->as->rank);
7748 lss_array->descriptor = dest;
7749 lss_array->data = gfc_conv_array_data (dest);
7750 lss_array->offset = gfc_conv_array_offset (dest);
7751 for (n = 0; n < cm->as->rank; n++)
7752 {
7753 lss_array->start[n] = gfc_conv_array_lbound (dest, n);
7754 lss_array->stride[n] = gfc_index_one_node;
7755
7756 mpz_init (lss_array->shape[n]);
7757 mpz_sub (lss_array->shape[n], cm->as->upper[n]->value.integer,
7758 cm->as->lower[n]->value.integer);
7759 mpz_add_ui (lss_array->shape[n], lss_array->shape[n], 1);
7760 }
7761
7762 /* Associate the SS with the loop. */
7763 gfc_add_ss_to_loop (&loop, lss);
7764 gfc_add_ss_to_loop (&loop, rss);
7765
7766 /* Calculate the bounds of the scalarization. */
7767 gfc_conv_ss_startstride (&loop);
7768
7769 /* Setup the scalarizing loops. */
7770 gfc_conv_loop_setup (&loop, &expr->where);
7771
7772 /* Setup the gfc_se structures. */
7773 gfc_copy_loopinfo_to_se (&lse, &loop);
7774 gfc_copy_loopinfo_to_se (&rse, &loop);
7775
7776 rse.ss = rss;
7777 gfc_mark_ss_chain_used (rss, 1);
7778 lse.ss = lss;
7779 gfc_mark_ss_chain_used (lss, 1);
7780
7781 /* Start the scalarized loop body. */
7782 gfc_start_scalarized_body (&loop, &body);
7783
7784 gfc_conv_tmp_array_ref (&lse);
7785 if (cm->ts.type == BT_CHARACTER)
7786 lse.string_length = cm->ts.u.cl->backend_decl;
7787
7788 gfc_conv_expr (&rse, expr);
7789
7790 tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false);
7791 gfc_add_expr_to_block (&body, tmp);
7792
7793 gcc_assert (rse.ss == gfc_ss_terminator);
7794
7795 /* Generate the copying loops. */
7796 gfc_trans_scalarizing_loops (&loop, &body);
7797
7798 /* Wrap the whole thing up. */
7799 gfc_add_block_to_block (&block, &loop.pre);
7800 gfc_add_block_to_block (&block, &loop.post);
7801
7802 gcc_assert (lss_array->shape != NULL);
7803 gfc_free_shape (&lss_array->shape, cm->as->rank);
7804 gfc_cleanup_loop (&loop);
7805
7806 return gfc_finish_block (&block);
7807 }
7808
7809
7810 static tree
7811 gfc_trans_alloc_subarray_assign (tree dest, gfc_component * cm,
7812 gfc_expr * expr)
7813 {
7814 gfc_se se;
7815 stmtblock_t block;
7816 tree offset;
7817 int n;
7818 tree tmp;
7819 tree tmp2;
7820 gfc_array_spec *as;
7821 gfc_expr *arg = NULL;
7822
7823 gfc_start_block (&block);
7824 gfc_init_se (&se, NULL);
7825
7826 /* Get the descriptor for the expressions. */
7827 se.want_pointer = 0;
7828 gfc_conv_expr_descriptor (&se, expr);
7829 gfc_add_block_to_block (&block, &se.pre);
7830 gfc_add_modify (&block, dest, se.expr);
7831
7832 /* Deal with arrays of derived types with allocatable components. */
7833 if (gfc_bt_struct (cm->ts.type)
7834 && cm->ts.u.derived->attr.alloc_comp)
7835 // TODO: Fix caf_mode
7836 tmp = gfc_copy_alloc_comp (cm->ts.u.derived,
7837 se.expr, dest,
7838 cm->as->rank, 0);
7839 else if (cm->ts.type == BT_CLASS && expr->ts.type == BT_DERIVED
7840 && CLASS_DATA(cm)->attr.allocatable)
7841 {
7842 if (cm->ts.u.derived->attr.alloc_comp)
7843 // TODO: Fix caf_mode
7844 tmp = gfc_copy_alloc_comp (expr->ts.u.derived,
7845 se.expr, dest,
7846 expr->rank, 0);
7847 else
7848 {
7849 tmp = TREE_TYPE (dest);
7850 tmp = gfc_duplicate_allocatable (dest, se.expr,
7851 tmp, expr->rank, NULL_TREE);
7852 }
7853 }
7854 else
7855 tmp = gfc_duplicate_allocatable (dest, se.expr,
7856 TREE_TYPE(cm->backend_decl),
7857 cm->as->rank, NULL_TREE);
7858
7859 gfc_add_expr_to_block (&block, tmp);
7860 gfc_add_block_to_block (&block, &se.post);
7861
7862 if (expr->expr_type != EXPR_VARIABLE)
7863 gfc_conv_descriptor_data_set (&block, se.expr,
7864 null_pointer_node);
7865
7866 /* We need to know if the argument of a conversion function is a
7867 variable, so that the correct lower bound can be used. */
7868 if (expr->expr_type == EXPR_FUNCTION
7869 && expr->value.function.isym
7870 && expr->value.function.isym->conversion
7871 && expr->value.function.actual->expr
7872 && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE)
7873 arg = expr->value.function.actual->expr;
7874
7875 /* Obtain the array spec of full array references. */
7876 if (arg)
7877 as = gfc_get_full_arrayspec_from_expr (arg);
7878 else
7879 as = gfc_get_full_arrayspec_from_expr (expr);
7880
7881 /* Shift the lbound and ubound of temporaries to being unity,
7882 rather than zero, based. Always calculate the offset. */
7883 offset = gfc_conv_descriptor_offset_get (dest);
7884 gfc_add_modify (&block, offset, gfc_index_zero_node);
7885 tmp2 =gfc_create_var (gfc_array_index_type, NULL);
7886
7887 for (n = 0; n < expr->rank; n++)
7888 {
7889 tree span;
7890 tree lbound;
7891
7892 /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
7893 TODO It looks as if gfc_conv_expr_descriptor should return
7894 the correct bounds and that the following should not be
7895 necessary. This would simplify gfc_conv_intrinsic_bound
7896 as well. */
7897 if (as && as->lower[n])
7898 {
7899 gfc_se lbse;
7900 gfc_init_se (&lbse, NULL);
7901 gfc_conv_expr (&lbse, as->lower[n]);
7902 gfc_add_block_to_block (&block, &lbse.pre);
7903 lbound = gfc_evaluate_now (lbse.expr, &block);
7904 }
7905 else if (as && arg)
7906 {
7907 tmp = gfc_get_symbol_decl (arg->symtree->n.sym);
7908 lbound = gfc_conv_descriptor_lbound_get (tmp,
7909 gfc_rank_cst[n]);
7910 }
7911 else if (as)
7912 lbound = gfc_conv_descriptor_lbound_get (dest,
7913 gfc_rank_cst[n]);
7914 else
7915 lbound = gfc_index_one_node;
7916
7917 lbound = fold_convert (gfc_array_index_type, lbound);
7918
7919 /* Shift the bounds and set the offset accordingly. */
7920 tmp = gfc_conv_descriptor_ubound_get (dest, gfc_rank_cst[n]);
7921 span = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
7922 tmp, gfc_conv_descriptor_lbound_get (dest, gfc_rank_cst[n]));
7923 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
7924 span, lbound);
7925 gfc_conv_descriptor_ubound_set (&block, dest,
7926 gfc_rank_cst[n], tmp);
7927 gfc_conv_descriptor_lbound_set (&block, dest,
7928 gfc_rank_cst[n], lbound);
7929
7930 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7931 gfc_conv_descriptor_lbound_get (dest,
7932 gfc_rank_cst[n]),
7933 gfc_conv_descriptor_stride_get (dest,
7934 gfc_rank_cst[n]));
7935 gfc_add_modify (&block, tmp2, tmp);
7936 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
7937 offset, tmp2);
7938 gfc_conv_descriptor_offset_set (&block, dest, tmp);
7939 }
7940
7941 if (arg)
7942 {
7943 /* If a conversion expression has a null data pointer
7944 argument, nullify the allocatable component. */
7945 tree non_null_expr;
7946 tree null_expr;
7947
7948 if (arg->symtree->n.sym->attr.allocatable
7949 || arg->symtree->n.sym->attr.pointer)
7950 {
7951 non_null_expr = gfc_finish_block (&block);
7952 gfc_start_block (&block);
7953 gfc_conv_descriptor_data_set (&block, dest,
7954 null_pointer_node);
7955 null_expr = gfc_finish_block (&block);
7956 tmp = gfc_conv_descriptor_data_get (arg->symtree->n.sym->backend_decl);
7957 tmp = build2_loc (input_location, EQ_EXPR, logical_type_node, tmp,
7958 fold_convert (TREE_TYPE (tmp), null_pointer_node));
7959 return build3_v (COND_EXPR, tmp,
7960 null_expr, non_null_expr);
7961 }
7962 }
7963
7964 return gfc_finish_block (&block);
7965 }
7966
7967
7968 /* Allocate or reallocate scalar component, as necessary. */
7969
7970 static void
7971 alloc_scalar_allocatable_for_subcomponent_assignment (stmtblock_t *block,
7972 tree comp,
7973 gfc_component *cm,
7974 gfc_expr *expr2,
7975 gfc_symbol *sym)
7976 {
7977 tree tmp;
7978 tree ptr;
7979 tree size;
7980 tree size_in_bytes;
7981 tree lhs_cl_size = NULL_TREE;
7982
7983 if (!comp)
7984 return;
7985
7986 if (!expr2 || expr2->rank)
7987 return;
7988
7989 realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
7990
7991 if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
7992 {
7993 char name[GFC_MAX_SYMBOL_LEN+9];
7994 gfc_component *strlen;
7995 /* Use the rhs string length and the lhs element size. */
7996 gcc_assert (expr2->ts.type == BT_CHARACTER);
7997 if (!expr2->ts.u.cl->backend_decl)
7998 {
7999 gfc_conv_string_length (expr2->ts.u.cl, expr2, block);
8000 gcc_assert (expr2->ts.u.cl->backend_decl);
8001 }
8002
8003 size = expr2->ts.u.cl->backend_decl;
8004
8005 /* Ensure that cm->ts.u.cl->backend_decl is a componentref to _%s_length
8006 component. */
8007 sprintf (name, "_%s_length", cm->name);
8008 strlen = gfc_find_component (sym, name, true, true, NULL);
8009 lhs_cl_size = fold_build3_loc (input_location, COMPONENT_REF,
8010 gfc_charlen_type_node,
8011 TREE_OPERAND (comp, 0),
8012 strlen->backend_decl, NULL_TREE);
8013
8014 tmp = TREE_TYPE (gfc_typenode_for_spec (&cm->ts));
8015 tmp = TYPE_SIZE_UNIT (tmp);
8016 size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
8017 TREE_TYPE (tmp), tmp,
8018 fold_convert (TREE_TYPE (tmp), size));
8019 }
8020 else if (cm->ts.type == BT_CLASS)
8021 {
8022 gcc_assert (expr2->ts.type == BT_CLASS || expr2->ts.type == BT_DERIVED);
8023 if (expr2->ts.type == BT_DERIVED)
8024 {
8025 tmp = gfc_get_symbol_decl (expr2->ts.u.derived);
8026 size = TYPE_SIZE_UNIT (tmp);
8027 }
8028 else
8029 {
8030 gfc_expr *e2vtab;
8031 gfc_se se;
8032 e2vtab = gfc_find_and_cut_at_last_class_ref (expr2);
8033 gfc_add_vptr_component (e2vtab);
8034 gfc_add_size_component (e2vtab);
8035 gfc_init_se (&se, NULL);
8036 gfc_conv_expr (&se, e2vtab);
8037 gfc_add_block_to_block (block, &se.pre);
8038 size = fold_convert (size_type_node, se.expr);
8039 gfc_free_expr (e2vtab);
8040 }
8041 size_in_bytes = size;
8042 }
8043 else
8044 {
8045 /* Otherwise use the length in bytes of the rhs. */
8046 size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&cm->ts));
8047 size_in_bytes = size;
8048 }
8049
8050 size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
8051 size_in_bytes, size_one_node);
8052
8053 if (cm->ts.type == BT_DERIVED && cm->ts.u.derived->attr.alloc_comp)
8054 {
8055 tmp = build_call_expr_loc (input_location,
8056 builtin_decl_explicit (BUILT_IN_CALLOC),
8057 2, build_one_cst (size_type_node),
8058 size_in_bytes);
8059 tmp = fold_convert (TREE_TYPE (comp), tmp);
8060 gfc_add_modify (block, comp, tmp);
8061 }
8062 else
8063 {
8064 tmp = build_call_expr_loc (input_location,
8065 builtin_decl_explicit (BUILT_IN_MALLOC),
8066 1, size_in_bytes);
8067 if (GFC_CLASS_TYPE_P (TREE_TYPE (comp)))
8068 ptr = gfc_class_data_get (comp);
8069 else
8070 ptr = comp;
8071 tmp = fold_convert (TREE_TYPE (ptr), tmp);
8072 gfc_add_modify (block, ptr, tmp);
8073 }
8074
8075 if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
8076 /* Update the lhs character length. */
8077 gfc_add_modify (block, lhs_cl_size,
8078 fold_convert (TREE_TYPE (lhs_cl_size), size));
8079 }
8080
8081
8082 /* Assign a single component of a derived type constructor. */
8083
8084 static tree
8085 gfc_trans_subcomponent_assign (tree dest, gfc_component * cm, gfc_expr * expr,
8086 gfc_symbol *sym, bool init)
8087 {
8088 gfc_se se;
8089 gfc_se lse;
8090 stmtblock_t block;
8091 tree tmp;
8092 tree vtab;
8093
8094 gfc_start_block (&block);
8095
8096 if (cm->attr.pointer || cm->attr.proc_pointer)
8097 {
8098 /* Only care about pointers here, not about allocatables. */
8099 gfc_init_se (&se, NULL);
8100 /* Pointer component. */
8101 if ((cm->attr.dimension || cm->attr.codimension)
8102 && !cm->attr.proc_pointer)
8103 {
8104 /* Array pointer. */
8105 if (expr->expr_type == EXPR_NULL)
8106 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
8107 else
8108 {
8109 se.direct_byref = 1;
8110 se.expr = dest;
8111 gfc_conv_expr_descriptor (&se, expr);
8112 gfc_add_block_to_block (&block, &se.pre);
8113 gfc_add_block_to_block (&block, &se.post);
8114 }
8115 }
8116 else
8117 {
8118 /* Scalar pointers. */
8119 se.want_pointer = 1;
8120 gfc_conv_expr (&se, expr);
8121 gfc_add_block_to_block (&block, &se.pre);
8122
8123 if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
8124 && expr->symtree->n.sym->attr.dummy)
8125 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
8126
8127 gfc_add_modify (&block, dest,
8128 fold_convert (TREE_TYPE (dest), se.expr));
8129 gfc_add_block_to_block (&block, &se.post);
8130 }
8131 }
8132 else if (cm->ts.type == BT_CLASS && expr->expr_type == EXPR_NULL)
8133 {
8134 /* NULL initialization for CLASS components. */
8135 tmp = gfc_trans_structure_assign (dest,
8136 gfc_class_initializer (&cm->ts, expr),
8137 false);
8138 gfc_add_expr_to_block (&block, tmp);
8139 }
8140 else if ((cm->attr.dimension || cm->attr.codimension)
8141 && !cm->attr.proc_pointer)
8142 {
8143 if (cm->attr.allocatable && expr->expr_type == EXPR_NULL)
8144 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
8145 else if (cm->attr.allocatable || cm->attr.pdt_array)
8146 {
8147 tmp = gfc_trans_alloc_subarray_assign (dest, cm, expr);
8148 gfc_add_expr_to_block (&block, tmp);
8149 }
8150 else
8151 {
8152 tmp = gfc_trans_subarray_assign (dest, cm, expr);
8153 gfc_add_expr_to_block (&block, tmp);
8154 }
8155 }
8156 else if (cm->ts.type == BT_CLASS
8157 && CLASS_DATA (cm)->attr.dimension
8158 && CLASS_DATA (cm)->attr.allocatable
8159 && expr->ts.type == BT_DERIVED)
8160 {
8161 vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
8162 vtab = gfc_build_addr_expr (NULL_TREE, vtab);
8163 tmp = gfc_class_vptr_get (dest);
8164 gfc_add_modify (&block, tmp,
8165 fold_convert (TREE_TYPE (tmp), vtab));
8166 tmp = gfc_class_data_get (dest);
8167 tmp = gfc_trans_alloc_subarray_assign (tmp, cm, expr);
8168 gfc_add_expr_to_block (&block, tmp);
8169 }
8170 else if (init && cm->attr.allocatable && expr->expr_type == EXPR_NULL)
8171 {
8172 /* NULL initialization for allocatable components. */
8173 gfc_add_modify (&block, dest, fold_convert (TREE_TYPE (dest),
8174 null_pointer_node));
8175 }
8176 else if (init && (cm->attr.allocatable
8177 || (cm->ts.type == BT_CLASS && CLASS_DATA (cm)->attr.allocatable
8178 && expr->ts.type != BT_CLASS)))
8179 {
8180 /* Take care about non-array allocatable components here. The alloc_*
8181 routine below is motivated by the alloc_scalar_allocatable_for_
8182 assignment() routine, but with the realloc portions removed and
8183 different input. */
8184 alloc_scalar_allocatable_for_subcomponent_assignment (&block,
8185 dest,
8186 cm,
8187 expr,
8188 sym);
8189 /* The remainder of these instructions follow the if (cm->attr.pointer)
8190 if (!cm->attr.dimension) part above. */
8191 gfc_init_se (&se, NULL);
8192 gfc_conv_expr (&se, expr);
8193 gfc_add_block_to_block (&block, &se.pre);
8194
8195 if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
8196 && expr->symtree->n.sym->attr.dummy)
8197 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
8198
8199 if (cm->ts.type == BT_CLASS && expr->ts.type == BT_DERIVED)
8200 {
8201 tmp = gfc_class_data_get (dest);
8202 tmp = build_fold_indirect_ref_loc (input_location, tmp);
8203 vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
8204 vtab = gfc_build_addr_expr (NULL_TREE, vtab);
8205 gfc_add_modify (&block, gfc_class_vptr_get (dest),
8206 fold_convert (TREE_TYPE (gfc_class_vptr_get (dest)), vtab));
8207 }
8208 else
8209 tmp = build_fold_indirect_ref_loc (input_location, dest);
8210
8211 /* For deferred strings insert a memcpy. */
8212 if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
8213 {
8214 tree size;
8215 gcc_assert (se.string_length || expr->ts.u.cl->backend_decl);
8216 size = size_of_string_in_bytes (cm->ts.kind, se.string_length
8217 ? se.string_length
8218 : expr->ts.u.cl->backend_decl);
8219 tmp = gfc_build_memcpy_call (tmp, se.expr, size);
8220 gfc_add_expr_to_block (&block, tmp);
8221 }
8222 else
8223 gfc_add_modify (&block, tmp,
8224 fold_convert (TREE_TYPE (tmp), se.expr));
8225 gfc_add_block_to_block (&block, &se.post);
8226 }
8227 else if (expr->ts.type == BT_UNION)
8228 {
8229 tree tmp;
8230 gfc_constructor *c = gfc_constructor_first (expr->value.constructor);
8231 /* We mark that the entire union should be initialized with a contrived
8232 EXPR_NULL expression at the beginning. */
8233 if (c != NULL && c->n.component == NULL
8234 && c->expr != NULL && c->expr->expr_type == EXPR_NULL)
8235 {
8236 tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
8237 dest, build_constructor (TREE_TYPE (dest), NULL));
8238 gfc_add_expr_to_block (&block, tmp);
8239 c = gfc_constructor_next (c);
8240 }
8241 /* The following constructor expression, if any, represents a specific
8242 map intializer, as given by the user. */
8243 if (c != NULL && c->expr != NULL)
8244 {
8245 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
8246 tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
8247 gfc_add_expr_to_block (&block, tmp);
8248 }
8249 }
8250 else if (expr->ts.type == BT_DERIVED && expr->ts.f90_type != BT_VOID)
8251 {
8252 if (expr->expr_type != EXPR_STRUCTURE)
8253 {
8254 tree dealloc = NULL_TREE;
8255 gfc_init_se (&se, NULL);
8256 gfc_conv_expr (&se, expr);
8257 gfc_add_block_to_block (&block, &se.pre);
8258 /* Prevent repeat evaluations in gfc_copy_alloc_comp by fixing the
8259 expression in a temporary variable and deallocate the allocatable
8260 components. Then we can the copy the expression to the result. */
8261 if (cm->ts.u.derived->attr.alloc_comp
8262 && expr->expr_type != EXPR_VARIABLE)
8263 {
8264 se.expr = gfc_evaluate_now (se.expr, &block);
8265 dealloc = gfc_deallocate_alloc_comp (cm->ts.u.derived, se.expr,
8266 expr->rank);
8267 }
8268 gfc_add_modify (&block, dest,
8269 fold_convert (TREE_TYPE (dest), se.expr));
8270 if (cm->ts.u.derived->attr.alloc_comp
8271 && expr->expr_type != EXPR_NULL)
8272 {
8273 // TODO: Fix caf_mode
8274 tmp = gfc_copy_alloc_comp (cm->ts.u.derived, se.expr,
8275 dest, expr->rank, 0);
8276 gfc_add_expr_to_block (&block, tmp);
8277 if (dealloc != NULL_TREE)
8278 gfc_add_expr_to_block (&block, dealloc);
8279 }
8280 gfc_add_block_to_block (&block, &se.post);
8281 }
8282 else
8283 {
8284 /* Nested constructors. */
8285 tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
8286 gfc_add_expr_to_block (&block, tmp);
8287 }
8288 }
8289 else if (gfc_deferred_strlen (cm, &tmp))
8290 {
8291 tree strlen;
8292 strlen = tmp;
8293 gcc_assert (strlen);
8294 strlen = fold_build3_loc (input_location, COMPONENT_REF,
8295 TREE_TYPE (strlen),
8296 TREE_OPERAND (dest, 0),
8297 strlen, NULL_TREE);
8298
8299 if (expr->expr_type == EXPR_NULL)
8300 {
8301 tmp = build_int_cst (TREE_TYPE (cm->backend_decl), 0);
8302 gfc_add_modify (&block, dest, tmp);
8303 tmp = build_int_cst (TREE_TYPE (strlen), 0);
8304 gfc_add_modify (&block, strlen, tmp);
8305 }
8306 else
8307 {
8308 tree size;
8309 gfc_init_se (&se, NULL);
8310 gfc_conv_expr (&se, expr);
8311 size = size_of_string_in_bytes (cm->ts.kind, se.string_length);
8312 tmp = build_call_expr_loc (input_location,
8313 builtin_decl_explicit (BUILT_IN_MALLOC),
8314 1, size);
8315 gfc_add_modify (&block, dest,
8316 fold_convert (TREE_TYPE (dest), tmp));
8317 gfc_add_modify (&block, strlen,
8318 fold_convert (TREE_TYPE (strlen), se.string_length));
8319 tmp = gfc_build_memcpy_call (dest, se.expr, size);
8320 gfc_add_expr_to_block (&block, tmp);
8321 }
8322 }
8323 else if (!cm->attr.artificial)
8324 {
8325 /* Scalar component (excluding deferred parameters). */
8326 gfc_init_se (&se, NULL);
8327 gfc_init_se (&lse, NULL);
8328
8329 gfc_conv_expr (&se, expr);
8330 if (cm->ts.type == BT_CHARACTER)
8331 lse.string_length = cm->ts.u.cl->backend_decl;
8332 lse.expr = dest;
8333 tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, false, false);
8334 gfc_add_expr_to_block (&block, tmp);
8335 }
8336 return gfc_finish_block (&block);
8337 }
8338
8339 /* Assign a derived type constructor to a variable. */
8340
8341 tree
8342 gfc_trans_structure_assign (tree dest, gfc_expr * expr, bool init, bool coarray)
8343 {
8344 gfc_constructor *c;
8345 gfc_component *cm;
8346 stmtblock_t block;
8347 tree field;
8348 tree tmp;
8349 gfc_se se;
8350
8351 gfc_start_block (&block);
8352
8353 if (expr->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
8354 && (expr->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
8355 || expr->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
8356 {
8357 gfc_se lse;
8358
8359 gfc_init_se (&se, NULL);
8360 gfc_init_se (&lse, NULL);
8361 gfc_conv_expr (&se, gfc_constructor_first (expr->value.constructor)->expr);
8362 lse.expr = dest;
8363 gfc_add_modify (&block, lse.expr,
8364 fold_convert (TREE_TYPE (lse.expr), se.expr));
8365
8366 return gfc_finish_block (&block);
8367 }
8368
8369 /* Make sure that the derived type has been completely built. */
8370 if (!expr->ts.u.derived->backend_decl
8371 || !TYPE_FIELDS (expr->ts.u.derived->backend_decl))
8372 {
8373 tmp = gfc_typenode_for_spec (&expr->ts);
8374 gcc_assert (tmp);
8375 }
8376
8377 cm = expr->ts.u.derived->components;
8378
8379
8380 if (coarray)
8381 gfc_init_se (&se, NULL);
8382
8383 for (c = gfc_constructor_first (expr->value.constructor);
8384 c; c = gfc_constructor_next (c), cm = cm->next)
8385 {
8386 /* Skip absent members in default initializers. */
8387 if (!c->expr && !cm->attr.allocatable)
8388 continue;
8389
8390 /* Register the component with the caf-lib before it is initialized.
8391 Register only allocatable components, that are not coarray'ed
8392 components (%comp[*]). Only register when the constructor is not the
8393 null-expression. */
8394 if (coarray && !cm->attr.codimension
8395 && (cm->attr.allocatable || cm->attr.pointer)
8396 && (!c->expr || c->expr->expr_type == EXPR_NULL))
8397 {
8398 tree token, desc, size;
8399 bool is_array = cm->ts.type == BT_CLASS
8400 ? CLASS_DATA (cm)->attr.dimension : cm->attr.dimension;
8401
8402 field = cm->backend_decl;
8403 field = fold_build3_loc (input_location, COMPONENT_REF,
8404 TREE_TYPE (field), dest, field, NULL_TREE);
8405 if (cm->ts.type == BT_CLASS)
8406 field = gfc_class_data_get (field);
8407
8408 token = is_array ? gfc_conv_descriptor_token (field)
8409 : fold_build3_loc (input_location, COMPONENT_REF,
8410 TREE_TYPE (cm->caf_token), dest,
8411 cm->caf_token, NULL_TREE);
8412
8413 if (is_array)
8414 {
8415 /* The _caf_register routine looks at the rank of the array
8416 descriptor to decide whether the data registered is an array
8417 or not. */
8418 int rank = cm->ts.type == BT_CLASS ? CLASS_DATA (cm)->as->rank
8419 : cm->as->rank;
8420 /* When the rank is not known just set a positive rank, which
8421 suffices to recognize the data as array. */
8422 if (rank < 0)
8423 rank = 1;
8424 size = build_zero_cst (size_type_node);
8425 desc = field;
8426 gfc_add_modify (&block, gfc_conv_descriptor_rank (desc),
8427 build_int_cst (signed_char_type_node, rank));
8428 }
8429 else
8430 {
8431 desc = gfc_conv_scalar_to_descriptor (&se, field,
8432 cm->ts.type == BT_CLASS
8433 ? CLASS_DATA (cm)->attr
8434 : cm->attr);
8435 size = TYPE_SIZE_UNIT (TREE_TYPE (field));
8436 }
8437 gfc_add_block_to_block (&block, &se.pre);
8438 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_register,
8439 7, size, build_int_cst (
8440 integer_type_node,
8441 GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY),
8442 gfc_build_addr_expr (pvoid_type_node,
8443 token),
8444 gfc_build_addr_expr (NULL_TREE, desc),
8445 null_pointer_node, null_pointer_node,
8446 integer_zero_node);
8447 gfc_add_expr_to_block (&block, tmp);
8448 }
8449 field = cm->backend_decl;
8450 gcc_assert(field);
8451 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
8452 dest, field, NULL_TREE);
8453 if (!c->expr)
8454 {
8455 gfc_expr *e = gfc_get_null_expr (NULL);
8456 tmp = gfc_trans_subcomponent_assign (tmp, cm, e, expr->ts.u.derived,
8457 init);
8458 gfc_free_expr (e);
8459 }
8460 else
8461 tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr,
8462 expr->ts.u.derived, init);
8463 gfc_add_expr_to_block (&block, tmp);
8464 }
8465 return gfc_finish_block (&block);
8466 }
8467
8468 void
8469 gfc_conv_union_initializer (vec<constructor_elt, va_gc> *v,
8470 gfc_component *un, gfc_expr *init)
8471 {
8472 gfc_constructor *ctor;
8473
8474 if (un->ts.type != BT_UNION || un == NULL || init == NULL)
8475 return;
8476
8477 ctor = gfc_constructor_first (init->value.constructor);
8478
8479 if (ctor == NULL || ctor->expr == NULL)
8480 return;
8481
8482 gcc_assert (init->expr_type == EXPR_STRUCTURE);
8483
8484 /* If we have an 'initialize all' constructor, do it first. */
8485 if (ctor->expr->expr_type == EXPR_NULL)
8486 {
8487 tree union_type = TREE_TYPE (un->backend_decl);
8488 tree val = build_constructor (union_type, NULL);
8489 CONSTRUCTOR_APPEND_ELT (v, un->backend_decl, val);
8490 ctor = gfc_constructor_next (ctor);
8491 }
8492
8493 /* Add the map initializer on top. */
8494 if (ctor != NULL && ctor->expr != NULL)
8495 {
8496 gcc_assert (ctor->expr->expr_type == EXPR_STRUCTURE);
8497 tree val = gfc_conv_initializer (ctor->expr, &un->ts,
8498 TREE_TYPE (un->backend_decl),
8499 un->attr.dimension, un->attr.pointer,
8500 un->attr.proc_pointer);
8501 CONSTRUCTOR_APPEND_ELT (v, un->backend_decl, val);
8502 }
8503 }
8504
8505 /* Build an expression for a constructor. If init is nonzero then
8506 this is part of a static variable initializer. */
8507
8508 void
8509 gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
8510 {
8511 gfc_constructor *c;
8512 gfc_component *cm;
8513 tree val;
8514 tree type;
8515 tree tmp;
8516 vec<constructor_elt, va_gc> *v = NULL;
8517
8518 gcc_assert (se->ss == NULL);
8519 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
8520 type = gfc_typenode_for_spec (&expr->ts);
8521
8522 if (!init)
8523 {
8524 /* Create a temporary variable and fill it in. */
8525 se->expr = gfc_create_var (type, expr->ts.u.derived->name);
8526 /* The symtree in expr is NULL, if the code to generate is for
8527 initializing the static members only. */
8528 tmp = gfc_trans_structure_assign (se->expr, expr, expr->symtree != NULL,
8529 se->want_coarray);
8530 gfc_add_expr_to_block (&se->pre, tmp);
8531 return;
8532 }
8533
8534 cm = expr->ts.u.derived->components;
8535
8536 for (c = gfc_constructor_first (expr->value.constructor);
8537 c; c = gfc_constructor_next (c), cm = cm->next)
8538 {
8539 /* Skip absent members in default initializers and allocatable
8540 components. Although the latter have a default initializer
8541 of EXPR_NULL,... by default, the static nullify is not needed
8542 since this is done every time we come into scope. */
8543 if (!c->expr || (cm->attr.allocatable && cm->attr.flavor != FL_PROCEDURE))
8544 continue;
8545
8546 if (cm->initializer && cm->initializer->expr_type != EXPR_NULL
8547 && strcmp (cm->name, "_extends") == 0
8548 && cm->initializer->symtree)
8549 {
8550 tree vtab;
8551 gfc_symbol *vtabs;
8552 vtabs = cm->initializer->symtree->n.sym;
8553 vtab = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtabs));
8554 vtab = unshare_expr_without_location (vtab);
8555 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, vtab);
8556 }
8557 else if (cm->ts.u.derived && strcmp (cm->name, "_size") == 0)
8558 {
8559 val = TYPE_SIZE_UNIT (gfc_get_derived_type (cm->ts.u.derived));
8560 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
8561 fold_convert (TREE_TYPE (cm->backend_decl),
8562 val));
8563 }
8564 else if (cm->ts.type == BT_INTEGER && strcmp (cm->name, "_len") == 0)
8565 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
8566 fold_convert (TREE_TYPE (cm->backend_decl),
8567 integer_zero_node));
8568 else if (cm->ts.type == BT_UNION)
8569 gfc_conv_union_initializer (v, cm, c->expr);
8570 else
8571 {
8572 val = gfc_conv_initializer (c->expr, &cm->ts,
8573 TREE_TYPE (cm->backend_decl),
8574 cm->attr.dimension, cm->attr.pointer,
8575 cm->attr.proc_pointer);
8576 val = unshare_expr_without_location (val);
8577
8578 /* Append it to the constructor list. */
8579 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
8580 }
8581 }
8582
8583 se->expr = build_constructor (type, v);
8584 if (init)
8585 TREE_CONSTANT (se->expr) = 1;
8586 }
8587
8588
8589 /* Translate a substring expression. */
8590
8591 static void
8592 gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
8593 {
8594 gfc_ref *ref;
8595
8596 ref = expr->ref;
8597
8598 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
8599
8600 se->expr = gfc_build_wide_string_const (expr->ts.kind,
8601 expr->value.character.length,
8602 expr->value.character.string);
8603
8604 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
8605 TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
8606
8607 if (ref)
8608 gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
8609 }
8610
8611
8612 /* Entry point for expression translation. Evaluates a scalar quantity.
8613 EXPR is the expression to be translated, and SE is the state structure if
8614 called from within the scalarized. */
8615
8616 void
8617 gfc_conv_expr (gfc_se * se, gfc_expr * expr)
8618 {
8619 gfc_ss *ss;
8620
8621 ss = se->ss;
8622 if (ss && ss->info->expr == expr
8623 && (ss->info->type == GFC_SS_SCALAR
8624 || ss->info->type == GFC_SS_REFERENCE))
8625 {
8626 gfc_ss_info *ss_info;
8627
8628 ss_info = ss->info;
8629 /* Substitute a scalar expression evaluated outside the scalarization
8630 loop. */
8631 se->expr = ss_info->data.scalar.value;
8632 if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
8633 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
8634
8635 se->string_length = ss_info->string_length;
8636 gfc_advance_se_ss_chain (se);
8637 return;
8638 }
8639
8640 /* We need to convert the expressions for the iso_c_binding derived types.
8641 C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
8642 null_pointer_node. C_PTR and C_FUNPTR are converted to match the
8643 typespec for the C_PTR and C_FUNPTR symbols, which has already been
8644 updated to be an integer with a kind equal to the size of a (void *). */
8645 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->ts.f90_type == BT_VOID
8646 && expr->ts.u.derived->attr.is_bind_c)
8647 {
8648 if (expr->expr_type == EXPR_VARIABLE
8649 && (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
8650 || expr->symtree->n.sym->intmod_sym_id
8651 == ISOCBINDING_NULL_FUNPTR))
8652 {
8653 /* Set expr_type to EXPR_NULL, which will result in
8654 null_pointer_node being used below. */
8655 expr->expr_type = EXPR_NULL;
8656 }
8657 else
8658 {
8659 /* Update the type/kind of the expression to be what the new
8660 type/kind are for the updated symbols of C_PTR/C_FUNPTR. */
8661 expr->ts.type = BT_INTEGER;
8662 expr->ts.f90_type = BT_VOID;
8663 expr->ts.kind = gfc_index_integer_kind;
8664 }
8665 }
8666
8667 gfc_fix_class_refs (expr);
8668
8669 switch (expr->expr_type)
8670 {
8671 case EXPR_OP:
8672 gfc_conv_expr_op (se, expr);
8673 break;
8674
8675 case EXPR_FUNCTION:
8676 gfc_conv_function_expr (se, expr);
8677 break;
8678
8679 case EXPR_CONSTANT:
8680 gfc_conv_constant (se, expr);
8681 break;
8682
8683 case EXPR_VARIABLE:
8684 gfc_conv_variable (se, expr);
8685 break;
8686
8687 case EXPR_NULL:
8688 se->expr = null_pointer_node;
8689 break;
8690
8691 case EXPR_SUBSTRING:
8692 gfc_conv_substring_expr (se, expr);
8693 break;
8694
8695 case EXPR_STRUCTURE:
8696 gfc_conv_structure (se, expr, 0);
8697 break;
8698
8699 case EXPR_ARRAY:
8700 gfc_conv_array_constructor_expr (se, expr);
8701 break;
8702
8703 default:
8704 gcc_unreachable ();
8705 break;
8706 }
8707 }
8708
8709 /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
8710 of an assignment. */
8711 void
8712 gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
8713 {
8714 gfc_conv_expr (se, expr);
8715 /* All numeric lvalues should have empty post chains. If not we need to
8716 figure out a way of rewriting an lvalue so that it has no post chain. */
8717 gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
8718 }
8719
8720 /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
8721 numeric expressions. Used for scalar values where inserting cleanup code
8722 is inconvenient. */
8723 void
8724 gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
8725 {
8726 tree val;
8727
8728 gcc_assert (expr->ts.type != BT_CHARACTER);
8729 gfc_conv_expr (se, expr);
8730 if (se->post.head)
8731 {
8732 val = gfc_create_var (TREE_TYPE (se->expr), NULL);
8733 gfc_add_modify (&se->pre, val, se->expr);
8734 se->expr = val;
8735 gfc_add_block_to_block (&se->pre, &se->post);
8736 }
8737 }
8738
8739 /* Helper to translate an expression and convert it to a particular type. */
8740 void
8741 gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
8742 {
8743 gfc_conv_expr_val (se, expr);
8744 se->expr = convert (type, se->expr);
8745 }
8746
8747
8748 /* Converts an expression so that it can be passed by reference. Scalar
8749 values only. */
8750
8751 void
8752 gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr, bool add_clobber)
8753 {
8754 gfc_ss *ss;
8755 tree var;
8756
8757 ss = se->ss;
8758 if (ss && ss->info->expr == expr
8759 && ss->info->type == GFC_SS_REFERENCE)
8760 {
8761 /* Returns a reference to the scalar evaluated outside the loop
8762 for this case. */
8763 gfc_conv_expr (se, expr);
8764
8765 if (expr->ts.type == BT_CHARACTER
8766 && expr->expr_type != EXPR_FUNCTION)
8767 gfc_conv_string_parameter (se);
8768 else
8769 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
8770
8771 return;
8772 }
8773
8774 if (expr->ts.type == BT_CHARACTER)
8775 {
8776 gfc_conv_expr (se, expr);
8777 gfc_conv_string_parameter (se);
8778 return;
8779 }
8780
8781 if (expr->expr_type == EXPR_VARIABLE)
8782 {
8783 se->want_pointer = 1;
8784 gfc_conv_expr (se, expr);
8785 if (se->post.head)
8786 {
8787 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
8788 gfc_add_modify (&se->pre, var, se->expr);
8789 gfc_add_block_to_block (&se->pre, &se->post);
8790 se->expr = var;
8791 }
8792 else if (add_clobber && expr->ref == NULL)
8793 {
8794 tree clobber;
8795 tree var;
8796 /* FIXME: This fails if var is passed by reference, see PR
8797 41453. */
8798 var = expr->symtree->n.sym->backend_decl;
8799 clobber = build_clobber (TREE_TYPE (var));
8800 gfc_add_modify (&se->pre, var, clobber);
8801 }
8802 return;
8803 }
8804
8805 if (expr->expr_type == EXPR_FUNCTION
8806 && ((expr->value.function.esym
8807 && expr->value.function.esym->result->attr.pointer
8808 && !expr->value.function.esym->result->attr.dimension)
8809 || (!expr->value.function.esym && !expr->ref
8810 && expr->symtree->n.sym->attr.pointer
8811 && !expr->symtree->n.sym->attr.dimension)))
8812 {
8813 se->want_pointer = 1;
8814 gfc_conv_expr (se, expr);
8815 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
8816 gfc_add_modify (&se->pre, var, se->expr);
8817 se->expr = var;
8818 return;
8819 }
8820
8821 gfc_conv_expr (se, expr);
8822
8823 /* Create a temporary var to hold the value. */
8824 if (TREE_CONSTANT (se->expr))
8825 {
8826 tree tmp = se->expr;
8827 STRIP_TYPE_NOPS (tmp);
8828 var = build_decl (input_location,
8829 CONST_DECL, NULL, TREE_TYPE (tmp));
8830 DECL_INITIAL (var) = tmp;
8831 TREE_STATIC (var) = 1;
8832 pushdecl (var);
8833 }
8834 else
8835 {
8836 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
8837 gfc_add_modify (&se->pre, var, se->expr);
8838 }
8839
8840 if (!expr->must_finalize)
8841 gfc_add_block_to_block (&se->pre, &se->post);
8842
8843 /* Take the address of that value. */
8844 se->expr = gfc_build_addr_expr (NULL_TREE, var);
8845 }
8846
8847
8848 /* Get the _len component for an unlimited polymorphic expression. */
8849
8850 static tree
8851 trans_get_upoly_len (stmtblock_t *block, gfc_expr *expr)
8852 {
8853 gfc_se se;
8854 gfc_ref *ref = expr->ref;
8855
8856 gfc_init_se (&se, NULL);
8857 while (ref && ref->next)
8858 ref = ref->next;
8859 gfc_add_len_component (expr);
8860 gfc_conv_expr (&se, expr);
8861 gfc_add_block_to_block (block, &se.pre);
8862 gcc_assert (se.post.head == NULL_TREE);
8863 if (ref)
8864 {
8865 gfc_free_ref_list (ref->next);
8866 ref->next = NULL;
8867 }
8868 else
8869 {
8870 gfc_free_ref_list (expr->ref);
8871 expr->ref = NULL;
8872 }
8873 return se.expr;
8874 }
8875
8876
8877 /* Assign _vptr and _len components as appropriate. BLOCK should be a
8878 statement-list outside of the scalarizer-loop. When code is generated, that
8879 depends on the scalarized expression, it is added to RSE.PRE.
8880 Returns le's _vptr tree and when set the len expressions in to_lenp and
8881 from_lenp to form a le%_vptr%_copy (re, le, [from_lenp, to_lenp])
8882 expression. */
8883
8884 static tree
8885 trans_class_vptr_len_assignment (stmtblock_t *block, gfc_expr * le,
8886 gfc_expr * re, gfc_se *rse,
8887 tree * to_lenp, tree * from_lenp)
8888 {
8889 gfc_se se;
8890 gfc_expr * vptr_expr;
8891 tree tmp, to_len = NULL_TREE, from_len = NULL_TREE, lhs_vptr;
8892 bool set_vptr = false, temp_rhs = false;
8893 stmtblock_t *pre = block;
8894
8895 /* Create a temporary for complicated expressions. */
8896 if (re->expr_type != EXPR_VARIABLE && re->expr_type != EXPR_NULL
8897 && rse->expr != NULL_TREE && !DECL_P (rse->expr))
8898 {
8899 tmp = gfc_create_var (TREE_TYPE (rse->expr), "rhs");
8900 pre = &rse->pre;
8901 gfc_add_modify (&rse->pre, tmp, rse->expr);
8902 rse->expr = tmp;
8903 temp_rhs = true;
8904 }
8905
8906 /* Get the _vptr for the left-hand side expression. */
8907 gfc_init_se (&se, NULL);
8908 vptr_expr = gfc_find_and_cut_at_last_class_ref (le);
8909 if (vptr_expr != NULL && gfc_expr_attr (vptr_expr).class_ok)
8910 {
8911 /* Care about _len for unlimited polymorphic entities. */
8912 if (UNLIMITED_POLY (vptr_expr)
8913 || (vptr_expr->ts.type == BT_DERIVED
8914 && vptr_expr->ts.u.derived->attr.unlimited_polymorphic))
8915 to_len = trans_get_upoly_len (block, vptr_expr);
8916 gfc_add_vptr_component (vptr_expr);
8917 set_vptr = true;
8918 }
8919 else
8920 vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&le->ts));
8921 se.want_pointer = 1;
8922 gfc_conv_expr (&se, vptr_expr);
8923 gfc_free_expr (vptr_expr);
8924 gfc_add_block_to_block (block, &se.pre);
8925 gcc_assert (se.post.head == NULL_TREE);
8926 lhs_vptr = se.expr;
8927 STRIP_NOPS (lhs_vptr);
8928
8929 /* Set the _vptr only when the left-hand side of the assignment is a
8930 class-object. */
8931 if (set_vptr)
8932 {
8933 /* Get the vptr from the rhs expression only, when it is variable.
8934 Functions are expected to be assigned to a temporary beforehand. */
8935 vptr_expr = (re->expr_type == EXPR_VARIABLE && re->ts.type == BT_CLASS)
8936 ? gfc_find_and_cut_at_last_class_ref (re)
8937 : NULL;
8938 if (vptr_expr != NULL && vptr_expr->ts.type == BT_CLASS)
8939 {
8940 if (to_len != NULL_TREE)
8941 {
8942 /* Get the _len information from the rhs. */
8943 if (UNLIMITED_POLY (vptr_expr)
8944 || (vptr_expr->ts.type == BT_DERIVED
8945 && vptr_expr->ts.u.derived->attr.unlimited_polymorphic))
8946 from_len = trans_get_upoly_len (block, vptr_expr);
8947 }
8948 gfc_add_vptr_component (vptr_expr);
8949 }
8950 else
8951 {
8952 if (re->expr_type == EXPR_VARIABLE
8953 && DECL_P (re->symtree->n.sym->backend_decl)
8954 && DECL_LANG_SPECIFIC (re->symtree->n.sym->backend_decl)
8955 && GFC_DECL_SAVED_DESCRIPTOR (re->symtree->n.sym->backend_decl)
8956 && GFC_CLASS_TYPE_P (TREE_TYPE (GFC_DECL_SAVED_DESCRIPTOR (
8957 re->symtree->n.sym->backend_decl))))
8958 {
8959 vptr_expr = NULL;
8960 se.expr = gfc_class_vptr_get (GFC_DECL_SAVED_DESCRIPTOR (
8961 re->symtree->n.sym->backend_decl));
8962 if (to_len)
8963 from_len = gfc_class_len_get (GFC_DECL_SAVED_DESCRIPTOR (
8964 re->symtree->n.sym->backend_decl));
8965 }
8966 else if (temp_rhs && re->ts.type == BT_CLASS)
8967 {
8968 vptr_expr = NULL;
8969 se.expr = gfc_class_vptr_get (rse->expr);
8970 if (UNLIMITED_POLY (re))
8971 from_len = gfc_class_len_get (rse->expr);
8972 }
8973 else if (re->expr_type != EXPR_NULL)
8974 /* Only when rhs is non-NULL use its declared type for vptr
8975 initialisation. */
8976 vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&re->ts));
8977 else
8978 /* When the rhs is NULL use the vtab of lhs' declared type. */
8979 vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&le->ts));
8980 }
8981
8982 if (vptr_expr)
8983 {
8984 gfc_init_se (&se, NULL);
8985 se.want_pointer = 1;
8986 gfc_conv_expr (&se, vptr_expr);
8987 gfc_free_expr (vptr_expr);
8988 gfc_add_block_to_block (block, &se.pre);
8989 gcc_assert (se.post.head == NULL_TREE);
8990 }
8991 gfc_add_modify (pre, lhs_vptr, fold_convert (TREE_TYPE (lhs_vptr),
8992 se.expr));
8993
8994 if (to_len != NULL_TREE)
8995 {
8996 /* The _len component needs to be set. Figure how to get the
8997 value of the right-hand side. */
8998 if (from_len == NULL_TREE)
8999 {
9000 if (rse->string_length != NULL_TREE)
9001 from_len = rse->string_length;
9002 else if (re->ts.type == BT_CHARACTER && re->ts.u.cl->length)
9003 {
9004 gfc_init_se (&se, NULL);
9005 gfc_conv_expr (&se, re->ts.u.cl->length);
9006 gfc_add_block_to_block (block, &se.pre);
9007 gcc_assert (se.post.head == NULL_TREE);
9008 from_len = gfc_evaluate_now (se.expr, block);
9009 }
9010 else
9011 from_len = build_zero_cst (gfc_charlen_type_node);
9012 }
9013 gfc_add_modify (pre, to_len, fold_convert (TREE_TYPE (to_len),
9014 from_len));
9015 }
9016 }
9017
9018 /* Return the _len trees only, when requested. */
9019 if (to_lenp)
9020 *to_lenp = to_len;
9021 if (from_lenp)
9022 *from_lenp = from_len;
9023 return lhs_vptr;
9024 }
9025
9026
9027 /* Assign tokens for pointer components. */
9028
9029 static void
9030 trans_caf_token_assign (gfc_se *lse, gfc_se *rse, gfc_expr *expr1,
9031 gfc_expr *expr2)
9032 {
9033 symbol_attribute lhs_attr, rhs_attr;
9034 tree tmp, lhs_tok, rhs_tok;
9035 /* Flag to indicated component refs on the rhs. */
9036 bool rhs_cr;
9037
9038 lhs_attr = gfc_caf_attr (expr1);
9039 if (expr2->expr_type != EXPR_NULL)
9040 {
9041 rhs_attr = gfc_caf_attr (expr2, false, &rhs_cr);
9042 if (lhs_attr.codimension && rhs_attr.codimension)
9043 {
9044 lhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (lse, expr1);
9045 lhs_tok = build_fold_indirect_ref (lhs_tok);
9046
9047 if (rhs_cr)
9048 rhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (rse, expr2);
9049 else
9050 {
9051 tree caf_decl;
9052 caf_decl = gfc_get_tree_for_caf_expr (expr2);
9053 gfc_get_caf_token_offset (rse, &rhs_tok, NULL, caf_decl,
9054 NULL_TREE, NULL);
9055 }
9056 tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
9057 lhs_tok,
9058 fold_convert (TREE_TYPE (lhs_tok), rhs_tok));
9059 gfc_prepend_expr_to_block (&lse->post, tmp);
9060 }
9061 }
9062 else if (lhs_attr.codimension)
9063 {
9064 lhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (lse, expr1);
9065 lhs_tok = build_fold_indirect_ref (lhs_tok);
9066 tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
9067 lhs_tok, null_pointer_node);
9068 gfc_prepend_expr_to_block (&lse->post, tmp);
9069 }
9070 }
9071
9072
9073 /* Do everything that is needed for a CLASS function expr2. */
9074
9075 static tree
9076 trans_class_pointer_fcn (stmtblock_t *block, gfc_se *lse, gfc_se *rse,
9077 gfc_expr *expr1, gfc_expr *expr2)
9078 {
9079 tree expr1_vptr = NULL_TREE;
9080 tree tmp;
9081
9082 gfc_conv_function_expr (rse, expr2);
9083 rse->expr = gfc_evaluate_now (rse->expr, &rse->pre);
9084
9085 if (expr1->ts.type != BT_CLASS)
9086 rse->expr = gfc_class_data_get (rse->expr);
9087 else
9088 {
9089 expr1_vptr = trans_class_vptr_len_assignment (block, expr1,
9090 expr2, rse,
9091 NULL, NULL);
9092 gfc_add_block_to_block (block, &rse->pre);
9093 tmp = gfc_create_var (TREE_TYPE (rse->expr), "ptrtemp");
9094 gfc_add_modify (&lse->pre, tmp, rse->expr);
9095
9096 gfc_add_modify (&lse->pre, expr1_vptr,
9097 fold_convert (TREE_TYPE (expr1_vptr),
9098 gfc_class_vptr_get (tmp)));
9099 rse->expr = gfc_class_data_get (tmp);
9100 }
9101
9102 return expr1_vptr;
9103 }
9104
9105
9106 tree
9107 gfc_trans_pointer_assign (gfc_code * code)
9108 {
9109 return gfc_trans_pointer_assignment (code->expr1, code->expr2);
9110 }
9111
9112
9113 /* Generate code for a pointer assignment. */
9114
9115 tree
9116 gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
9117 {
9118 gfc_se lse;
9119 gfc_se rse;
9120 stmtblock_t block;
9121 tree desc;
9122 tree tmp;
9123 tree expr1_vptr = NULL_TREE;
9124 bool scalar, non_proc_ptr_assign;
9125 gfc_ss *ss;
9126
9127 gfc_start_block (&block);
9128
9129 gfc_init_se (&lse, NULL);
9130
9131 /* Usually testing whether this is not a proc pointer assignment. */
9132 non_proc_ptr_assign = !(gfc_expr_attr (expr1).proc_pointer
9133 && expr2->expr_type == EXPR_VARIABLE
9134 && expr2->symtree->n.sym->attr.flavor == FL_PROCEDURE);
9135
9136 /* Check whether the expression is a scalar or not; we cannot use
9137 expr1->rank as it can be nonzero for proc pointers. */
9138 ss = gfc_walk_expr (expr1);
9139 scalar = ss == gfc_ss_terminator;
9140 if (!scalar)
9141 gfc_free_ss_chain (ss);
9142
9143 if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS
9144 && expr2->expr_type != EXPR_FUNCTION && non_proc_ptr_assign)
9145 {
9146 gfc_add_data_component (expr2);
9147 /* The following is required as gfc_add_data_component doesn't
9148 update ts.type if there is a tailing REF_ARRAY. */
9149 expr2->ts.type = BT_DERIVED;
9150 }
9151
9152 if (scalar)
9153 {
9154 /* Scalar pointers. */
9155 lse.want_pointer = 1;
9156 gfc_conv_expr (&lse, expr1);
9157 gfc_init_se (&rse, NULL);
9158 rse.want_pointer = 1;
9159 if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
9160 trans_class_pointer_fcn (&block, &lse, &rse, expr1, expr2);
9161 else
9162 gfc_conv_expr (&rse, expr2);
9163
9164 if (non_proc_ptr_assign && expr1->ts.type == BT_CLASS)
9165 {
9166 trans_class_vptr_len_assignment (&block, expr1, expr2, &rse, NULL,
9167 NULL);
9168 lse.expr = gfc_class_data_get (lse.expr);
9169 }
9170
9171 if (expr1->symtree->n.sym->attr.proc_pointer
9172 && expr1->symtree->n.sym->attr.dummy)
9173 lse.expr = build_fold_indirect_ref_loc (input_location,
9174 lse.expr);
9175
9176 if (expr2->symtree && expr2->symtree->n.sym->attr.proc_pointer
9177 && expr2->symtree->n.sym->attr.dummy)
9178 rse.expr = build_fold_indirect_ref_loc (input_location,
9179 rse.expr);
9180
9181 gfc_add_block_to_block (&block, &lse.pre);
9182 gfc_add_block_to_block (&block, &rse.pre);
9183
9184 /* Check character lengths if character expression. The test is only
9185 really added if -fbounds-check is enabled. Exclude deferred
9186 character length lefthand sides. */
9187 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL
9188 && !expr1->ts.deferred
9189 && !expr1->symtree->n.sym->attr.proc_pointer
9190 && !gfc_is_proc_ptr_comp (expr1))
9191 {
9192 gcc_assert (expr2->ts.type == BT_CHARACTER);
9193 gcc_assert (lse.string_length && rse.string_length);
9194 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
9195 lse.string_length, rse.string_length,
9196 &block);
9197 }
9198
9199 /* The assignment to an deferred character length sets the string
9200 length to that of the rhs. */
9201 if (expr1->ts.deferred)
9202 {
9203 if (expr2->expr_type != EXPR_NULL && lse.string_length != NULL)
9204 gfc_add_modify (&block, lse.string_length,
9205 fold_convert (TREE_TYPE (lse.string_length),
9206 rse.string_length));
9207 else if (lse.string_length != NULL)
9208 gfc_add_modify (&block, lse.string_length,
9209 build_zero_cst (TREE_TYPE (lse.string_length)));
9210 }
9211
9212 gfc_add_modify (&block, lse.expr,
9213 fold_convert (TREE_TYPE (lse.expr), rse.expr));
9214
9215 /* Also set the tokens for pointer components in derived typed
9216 coarrays. */
9217 if (flag_coarray == GFC_FCOARRAY_LIB)
9218 trans_caf_token_assign (&lse, &rse, expr1, expr2);
9219
9220 gfc_add_block_to_block (&block, &rse.post);
9221 gfc_add_block_to_block (&block, &lse.post);
9222 }
9223 else
9224 {
9225 gfc_ref* remap;
9226 bool rank_remap;
9227 tree strlen_lhs;
9228 tree strlen_rhs = NULL_TREE;
9229
9230 /* Array pointer. Find the last reference on the LHS and if it is an
9231 array section ref, we're dealing with bounds remapping. In this case,
9232 set it to AR_FULL so that gfc_conv_expr_descriptor does
9233 not see it and process the bounds remapping afterwards explicitly. */
9234 for (remap = expr1->ref; remap; remap = remap->next)
9235 if (!remap->next && remap->type == REF_ARRAY
9236 && remap->u.ar.type == AR_SECTION)
9237 break;
9238 rank_remap = (remap && remap->u.ar.end[0]);
9239
9240 if (remap && expr2->expr_type == EXPR_NULL)
9241 {
9242 gfc_error ("If bounds remapping is specified at %L, "
9243 "the pointer target shall not be NULL", &expr1->where);
9244 return NULL_TREE;
9245 }
9246
9247 gfc_init_se (&lse, NULL);
9248 if (remap)
9249 lse.descriptor_only = 1;
9250 gfc_conv_expr_descriptor (&lse, expr1);
9251 strlen_lhs = lse.string_length;
9252 desc = lse.expr;
9253
9254 if (expr2->expr_type == EXPR_NULL)
9255 {
9256 /* Just set the data pointer to null. */
9257 gfc_conv_descriptor_data_set (&lse.pre, lse.expr, null_pointer_node);
9258 }
9259 else if (rank_remap)
9260 {
9261 /* If we are rank-remapping, just get the RHS's descriptor and
9262 process this later on. */
9263 gfc_init_se (&rse, NULL);
9264 rse.direct_byref = 1;
9265 rse.byref_noassign = 1;
9266
9267 if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
9268 expr1_vptr = trans_class_pointer_fcn (&block, &lse, &rse,
9269 expr1, expr2);
9270 else if (expr2->expr_type == EXPR_FUNCTION)
9271 {
9272 tree bound[GFC_MAX_DIMENSIONS];
9273 int i;
9274
9275 for (i = 0; i < expr2->rank; i++)
9276 bound[i] = NULL_TREE;
9277 tmp = gfc_typenode_for_spec (&expr2->ts);
9278 tmp = gfc_get_array_type_bounds (tmp, expr2->rank, 0,
9279 bound, bound, 0,
9280 GFC_ARRAY_POINTER_CONT, false);
9281 tmp = gfc_create_var (tmp, "ptrtemp");
9282 rse.descriptor_only = 0;
9283 rse.expr = tmp;
9284 rse.direct_byref = 1;
9285 gfc_conv_expr_descriptor (&rse, expr2);
9286 strlen_rhs = rse.string_length;
9287 rse.expr = tmp;
9288 }
9289 else
9290 {
9291 gfc_conv_expr_descriptor (&rse, expr2);
9292 strlen_rhs = rse.string_length;
9293 if (expr1->ts.type == BT_CLASS)
9294 expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
9295 expr2, &rse,
9296 NULL, NULL);
9297 }
9298 }
9299 else if (expr2->expr_type == EXPR_VARIABLE)
9300 {
9301 /* Assign directly to the LHS's descriptor. */
9302 lse.descriptor_only = 0;
9303 lse.direct_byref = 1;
9304 gfc_conv_expr_descriptor (&lse, expr2);
9305 strlen_rhs = lse.string_length;
9306
9307 if (expr1->ts.type == BT_CLASS)
9308 {
9309 rse.expr = NULL_TREE;
9310 rse.string_length = NULL_TREE;
9311 trans_class_vptr_len_assignment (&block, expr1, expr2, &rse,
9312 NULL, NULL);
9313 }
9314
9315 if (remap == NULL)
9316 {
9317 /* If the target is not a whole array, use the target array
9318 reference for remap. */
9319 for (remap = expr2->ref; remap; remap = remap->next)
9320 if (remap->type == REF_ARRAY
9321 && remap->u.ar.type == AR_FULL
9322 && remap->next)
9323 break;
9324 }
9325 }
9326 else if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
9327 {
9328 gfc_init_se (&rse, NULL);
9329 rse.want_pointer = 1;
9330 gfc_conv_function_expr (&rse, expr2);
9331 if (expr1->ts.type != BT_CLASS)
9332 {
9333 rse.expr = gfc_class_data_get (rse.expr);
9334 gfc_add_modify (&lse.pre, desc, rse.expr);
9335 /* Set the lhs span. */
9336 tmp = TREE_TYPE (rse.expr);
9337 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (tmp));
9338 tmp = fold_convert (gfc_array_index_type, tmp);
9339 gfc_conv_descriptor_span_set (&lse.pre, desc, tmp);
9340 }
9341 else
9342 {
9343 expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
9344 expr2, &rse, NULL,
9345 NULL);
9346 gfc_add_block_to_block (&block, &rse.pre);
9347 tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
9348 gfc_add_modify (&lse.pre, tmp, rse.expr);
9349
9350 gfc_add_modify (&lse.pre, expr1_vptr,
9351 fold_convert (TREE_TYPE (expr1_vptr),
9352 gfc_class_vptr_get (tmp)));
9353 rse.expr = gfc_class_data_get (tmp);
9354 gfc_add_modify (&lse.pre, desc, rse.expr);
9355 }
9356 }
9357 else
9358 {
9359 /* Assign to a temporary descriptor and then copy that
9360 temporary to the pointer. */
9361 tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
9362 lse.descriptor_only = 0;
9363 lse.expr = tmp;
9364 lse.direct_byref = 1;
9365 gfc_conv_expr_descriptor (&lse, expr2);
9366 strlen_rhs = lse.string_length;
9367 gfc_add_modify (&lse.pre, desc, tmp);
9368 }
9369
9370 gfc_add_block_to_block (&block, &lse.pre);
9371 if (rank_remap)
9372 gfc_add_block_to_block (&block, &rse.pre);
9373
9374 /* If we do bounds remapping, update LHS descriptor accordingly. */
9375 if (remap)
9376 {
9377 int dim;
9378 gcc_assert (remap->u.ar.dimen == expr1->rank);
9379
9380 if (rank_remap)
9381 {
9382 /* Do rank remapping. We already have the RHS's descriptor
9383 converted in rse and now have to build the correct LHS
9384 descriptor for it. */
9385
9386 tree dtype, data, span;
9387 tree offs, stride;
9388 tree lbound, ubound;
9389
9390 /* Set dtype. */
9391 dtype = gfc_conv_descriptor_dtype (desc);
9392 tmp = gfc_get_dtype (TREE_TYPE (desc));
9393 gfc_add_modify (&block, dtype, tmp);
9394
9395 /* Copy data pointer. */
9396 data = gfc_conv_descriptor_data_get (rse.expr);
9397 gfc_conv_descriptor_data_set (&block, desc, data);
9398
9399 /* Copy the span. */
9400 if (TREE_CODE (rse.expr) == VAR_DECL
9401 && GFC_DECL_PTR_ARRAY_P (rse.expr))
9402 span = gfc_conv_descriptor_span_get (rse.expr);
9403 else
9404 {
9405 tmp = TREE_TYPE (rse.expr);
9406 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (tmp));
9407 span = fold_convert (gfc_array_index_type, tmp);
9408 }
9409 gfc_conv_descriptor_span_set (&block, desc, span);
9410
9411 /* Copy offset but adjust it such that it would correspond
9412 to a lbound of zero. */
9413 offs = gfc_conv_descriptor_offset_get (rse.expr);
9414 for (dim = 0; dim < expr2->rank; ++dim)
9415 {
9416 stride = gfc_conv_descriptor_stride_get (rse.expr,
9417 gfc_rank_cst[dim]);
9418 lbound = gfc_conv_descriptor_lbound_get (rse.expr,
9419 gfc_rank_cst[dim]);
9420 tmp = fold_build2_loc (input_location, MULT_EXPR,
9421 gfc_array_index_type, stride, lbound);
9422 offs = fold_build2_loc (input_location, PLUS_EXPR,
9423 gfc_array_index_type, offs, tmp);
9424 }
9425 gfc_conv_descriptor_offset_set (&block, desc, offs);
9426
9427 /* Set the bounds as declared for the LHS and calculate strides as
9428 well as another offset update accordingly. */
9429 stride = gfc_conv_descriptor_stride_get (rse.expr,
9430 gfc_rank_cst[0]);
9431 for (dim = 0; dim < expr1->rank; ++dim)
9432 {
9433 gfc_se lower_se;
9434 gfc_se upper_se;
9435
9436 gcc_assert (remap->u.ar.start[dim] && remap->u.ar.end[dim]);
9437
9438 /* Convert declared bounds. */
9439 gfc_init_se (&lower_se, NULL);
9440 gfc_init_se (&upper_se, NULL);
9441 gfc_conv_expr (&lower_se, remap->u.ar.start[dim]);
9442 gfc_conv_expr (&upper_se, remap->u.ar.end[dim]);
9443
9444 gfc_add_block_to_block (&block, &lower_se.pre);
9445 gfc_add_block_to_block (&block, &upper_se.pre);
9446
9447 lbound = fold_convert (gfc_array_index_type, lower_se.expr);
9448 ubound = fold_convert (gfc_array_index_type, upper_se.expr);
9449
9450 lbound = gfc_evaluate_now (lbound, &block);
9451 ubound = gfc_evaluate_now (ubound, &block);
9452
9453 gfc_add_block_to_block (&block, &lower_se.post);
9454 gfc_add_block_to_block (&block, &upper_se.post);
9455
9456 /* Set bounds in descriptor. */
9457 gfc_conv_descriptor_lbound_set (&block, desc,
9458 gfc_rank_cst[dim], lbound);
9459 gfc_conv_descriptor_ubound_set (&block, desc,
9460 gfc_rank_cst[dim], ubound);
9461
9462 /* Set stride. */
9463 stride = gfc_evaluate_now (stride, &block);
9464 gfc_conv_descriptor_stride_set (&block, desc,
9465 gfc_rank_cst[dim], stride);
9466
9467 /* Update offset. */
9468 offs = gfc_conv_descriptor_offset_get (desc);
9469 tmp = fold_build2_loc (input_location, MULT_EXPR,
9470 gfc_array_index_type, lbound, stride);
9471 offs = fold_build2_loc (input_location, MINUS_EXPR,
9472 gfc_array_index_type, offs, tmp);
9473 offs = gfc_evaluate_now (offs, &block);
9474 gfc_conv_descriptor_offset_set (&block, desc, offs);
9475
9476 /* Update stride. */
9477 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
9478 stride = fold_build2_loc (input_location, MULT_EXPR,
9479 gfc_array_index_type, stride, tmp);
9480 }
9481 }
9482 else
9483 {
9484 /* Bounds remapping. Just shift the lower bounds. */
9485
9486 gcc_assert (expr1->rank == expr2->rank);
9487
9488 for (dim = 0; dim < remap->u.ar.dimen; ++dim)
9489 {
9490 gfc_se lbound_se;
9491
9492 gcc_assert (!remap->u.ar.end[dim]);
9493 gfc_init_se (&lbound_se, NULL);
9494 if (remap->u.ar.start[dim])
9495 {
9496 gfc_conv_expr (&lbound_se, remap->u.ar.start[dim]);
9497 gfc_add_block_to_block (&block, &lbound_se.pre);
9498 }
9499 else
9500 /* This remap arises from a target that is not a whole
9501 array. The start expressions will be NULL but we need
9502 the lbounds to be one. */
9503 lbound_se.expr = gfc_index_one_node;
9504 gfc_conv_shift_descriptor_lbound (&block, desc,
9505 dim, lbound_se.expr);
9506 gfc_add_block_to_block (&block, &lbound_se.post);
9507 }
9508 }
9509 }
9510
9511 /* If rank remapping was done, check with -fcheck=bounds that
9512 the target is at least as large as the pointer. */
9513 if (rank_remap && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
9514 {
9515 tree lsize, rsize;
9516 tree fault;
9517 const char* msg;
9518
9519 lsize = gfc_conv_descriptor_size (lse.expr, expr1->rank);
9520 rsize = gfc_conv_descriptor_size (rse.expr, expr2->rank);
9521
9522 lsize = gfc_evaluate_now (lsize, &block);
9523 rsize = gfc_evaluate_now (rsize, &block);
9524 fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
9525 rsize, lsize);
9526
9527 msg = _("Target of rank remapping is too small (%ld < %ld)");
9528 gfc_trans_runtime_check (true, false, fault, &block, &expr2->where,
9529 msg, rsize, lsize);
9530 }
9531
9532 if (expr1->ts.type == BT_CHARACTER
9533 && expr1->symtree->n.sym->ts.deferred
9534 && expr1->symtree->n.sym->ts.u.cl->backend_decl
9535 && VAR_P (expr1->symtree->n.sym->ts.u.cl->backend_decl))
9536 {
9537 tmp = expr1->symtree->n.sym->ts.u.cl->backend_decl;
9538 if (expr2->expr_type != EXPR_NULL)
9539 gfc_add_modify (&block, tmp,
9540 fold_convert (TREE_TYPE (tmp), strlen_rhs));
9541 else
9542 gfc_add_modify (&block, tmp, build_zero_cst (TREE_TYPE (tmp)));
9543 }
9544
9545 /* Check string lengths if applicable. The check is only really added
9546 to the output code if -fbounds-check is enabled. */
9547 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
9548 {
9549 gcc_assert (expr2->ts.type == BT_CHARACTER);
9550 gcc_assert (strlen_lhs && strlen_rhs);
9551 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
9552 strlen_lhs, strlen_rhs, &block);
9553 }
9554
9555 gfc_add_block_to_block (&block, &lse.post);
9556 if (rank_remap)
9557 gfc_add_block_to_block (&block, &rse.post);
9558 }
9559
9560 return gfc_finish_block (&block);
9561 }
9562
9563
9564 /* Makes sure se is suitable for passing as a function string parameter. */
9565 /* TODO: Need to check all callers of this function. It may be abused. */
9566
9567 void
9568 gfc_conv_string_parameter (gfc_se * se)
9569 {
9570 tree type;
9571
9572 if (TREE_CODE (se->expr) == STRING_CST)
9573 {
9574 type = TREE_TYPE (TREE_TYPE (se->expr));
9575 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
9576 return;
9577 }
9578
9579 if ((TREE_CODE (TREE_TYPE (se->expr)) == ARRAY_TYPE
9580 || TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE)
9581 && TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
9582 {
9583 if (TREE_CODE (se->expr) != INDIRECT_REF)
9584 {
9585 type = TREE_TYPE (se->expr);
9586 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
9587 }
9588 else
9589 {
9590 type = gfc_get_character_type_len (gfc_default_character_kind,
9591 se->string_length);
9592 type = build_pointer_type (type);
9593 se->expr = gfc_build_addr_expr (type, se->expr);
9594 }
9595 }
9596
9597 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
9598 }
9599
9600
9601 /* Generate code for assignment of scalar variables. Includes character
9602 strings and derived types with allocatable components.
9603 If you know that the LHS has no allocations, set dealloc to false.
9604
9605 DEEP_COPY has no effect if the typespec TS is not a derived type with
9606 allocatable components. Otherwise, if it is set, an explicit copy of each
9607 allocatable component is made. This is necessary as a simple copy of the
9608 whole object would copy array descriptors as is, so that the lhs's
9609 allocatable components would point to the rhs's after the assignment.
9610 Typically, setting DEEP_COPY is necessary if the rhs is a variable, and not
9611 necessary if the rhs is a non-pointer function, as the allocatable components
9612 are not accessible by other means than the function's result after the
9613 function has returned. It is even more subtle when temporaries are involved,
9614 as the two following examples show:
9615 1. When we evaluate an array constructor, a temporary is created. Thus
9616 there is theoretically no alias possible. However, no deep copy is
9617 made for this temporary, so that if the constructor is made of one or
9618 more variable with allocatable components, those components still point
9619 to the variable's: DEEP_COPY should be set for the assignment from the
9620 temporary to the lhs in that case.
9621 2. When assigning a scalar to an array, we evaluate the scalar value out
9622 of the loop, store it into a temporary variable, and assign from that.
9623 In that case, deep copying when assigning to the temporary would be a
9624 waste of resources; however deep copies should happen when assigning from
9625 the temporary to each array element: again DEEP_COPY should be set for
9626 the assignment from the temporary to the lhs. */
9627
9628 tree
9629 gfc_trans_scalar_assign (gfc_se * lse, gfc_se * rse, gfc_typespec ts,
9630 bool deep_copy, bool dealloc, bool in_coarray)
9631 {
9632 stmtblock_t block;
9633 tree tmp;
9634 tree cond;
9635
9636 gfc_init_block (&block);
9637
9638 if (ts.type == BT_CHARACTER)
9639 {
9640 tree rlen = NULL;
9641 tree llen = NULL;
9642
9643 if (lse->string_length != NULL_TREE)
9644 {
9645 gfc_conv_string_parameter (lse);
9646 gfc_add_block_to_block (&block, &lse->pre);
9647 llen = lse->string_length;
9648 }
9649
9650 if (rse->string_length != NULL_TREE)
9651 {
9652 gfc_conv_string_parameter (rse);
9653 gfc_add_block_to_block (&block, &rse->pre);
9654 rlen = rse->string_length;
9655 }
9656
9657 gfc_trans_string_copy (&block, llen, lse->expr, ts.kind, rlen,
9658 rse->expr, ts.kind);
9659 }
9660 else if (gfc_bt_struct (ts.type)
9661 && (ts.u.derived->attr.alloc_comp
9662 || (deep_copy && ts.u.derived->attr.pdt_type)))
9663 {
9664 tree tmp_var = NULL_TREE;
9665 cond = NULL_TREE;
9666
9667 /* Are the rhs and the lhs the same? */
9668 if (deep_copy)
9669 {
9670 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
9671 gfc_build_addr_expr (NULL_TREE, lse->expr),
9672 gfc_build_addr_expr (NULL_TREE, rse->expr));
9673 cond = gfc_evaluate_now (cond, &lse->pre);
9674 }
9675
9676 /* Deallocate the lhs allocated components as long as it is not
9677 the same as the rhs. This must be done following the assignment
9678 to prevent deallocating data that could be used in the rhs
9679 expression. */
9680 if (dealloc)
9681 {
9682 tmp_var = gfc_evaluate_now (lse->expr, &lse->pre);
9683 tmp = gfc_deallocate_alloc_comp_no_caf (ts.u.derived, tmp_var, 0);
9684 if (deep_copy)
9685 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
9686 tmp);
9687 gfc_add_expr_to_block (&lse->post, tmp);
9688 }
9689
9690 gfc_add_block_to_block (&block, &rse->pre);
9691 gfc_add_block_to_block (&block, &lse->pre);
9692
9693 gfc_add_modify (&block, lse->expr,
9694 fold_convert (TREE_TYPE (lse->expr), rse->expr));
9695
9696 /* Restore pointer address of coarray components. */
9697 if (ts.u.derived->attr.coarray_comp && deep_copy && tmp_var != NULL_TREE)
9698 {
9699 tmp = gfc_reassign_alloc_comp_caf (ts.u.derived, tmp_var, lse->expr);
9700 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
9701 tmp);
9702 gfc_add_expr_to_block (&block, tmp);
9703 }
9704
9705 /* Do a deep copy if the rhs is a variable, if it is not the
9706 same as the lhs. */
9707 if (deep_copy)
9708 {
9709 int caf_mode = in_coarray ? (GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
9710 | GFC_STRUCTURE_CAF_MODE_IN_COARRAY) : 0;
9711 tmp = gfc_copy_alloc_comp (ts.u.derived, rse->expr, lse->expr, 0,
9712 caf_mode);
9713 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
9714 tmp);
9715 gfc_add_expr_to_block (&block, tmp);
9716 }
9717 }
9718 else if (gfc_bt_struct (ts.type) || ts.type == BT_CLASS)
9719 {
9720 gfc_add_block_to_block (&block, &lse->pre);
9721 gfc_add_block_to_block (&block, &rse->pre);
9722 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
9723 TREE_TYPE (lse->expr), rse->expr);
9724 gfc_add_modify (&block, lse->expr, tmp);
9725 }
9726 else
9727 {
9728 gfc_add_block_to_block (&block, &lse->pre);
9729 gfc_add_block_to_block (&block, &rse->pre);
9730
9731 gfc_add_modify (&block, lse->expr,
9732 fold_convert (TREE_TYPE (lse->expr), rse->expr));
9733 }
9734
9735 gfc_add_block_to_block (&block, &lse->post);
9736 gfc_add_block_to_block (&block, &rse->post);
9737
9738 return gfc_finish_block (&block);
9739 }
9740
9741
9742 /* There are quite a lot of restrictions on the optimisation in using an
9743 array function assign without a temporary. */
9744
9745 static bool
9746 arrayfunc_assign_needs_temporary (gfc_expr * expr1, gfc_expr * expr2)
9747 {
9748 gfc_ref * ref;
9749 bool seen_array_ref;
9750 bool c = false;
9751 gfc_symbol *sym = expr1->symtree->n.sym;
9752
9753 /* Play it safe with class functions assigned to a derived type. */
9754 if (gfc_is_class_array_function (expr2)
9755 && expr1->ts.type == BT_DERIVED)
9756 return true;
9757
9758 /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
9759 if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
9760 return true;
9761
9762 /* Elemental functions are scalarized so that they don't need a
9763 temporary in gfc_trans_assignment_1, so return a true. Otherwise,
9764 they would need special treatment in gfc_trans_arrayfunc_assign. */
9765 if (expr2->value.function.esym != NULL
9766 && expr2->value.function.esym->attr.elemental)
9767 return true;
9768
9769 /* Need a temporary if rhs is not FULL or a contiguous section. */
9770 if (expr1->ref && !(gfc_full_array_ref_p (expr1->ref, &c) || c))
9771 return true;
9772
9773 /* Need a temporary if EXPR1 can't be expressed as a descriptor. */
9774 if (gfc_ref_needs_temporary_p (expr1->ref))
9775 return true;
9776
9777 /* Functions returning pointers or allocatables need temporaries. */
9778 c = expr2->value.function.esym
9779 ? (expr2->value.function.esym->attr.pointer
9780 || expr2->value.function.esym->attr.allocatable)
9781 : (expr2->symtree->n.sym->attr.pointer
9782 || expr2->symtree->n.sym->attr.allocatable);
9783 if (c)
9784 return true;
9785
9786 /* Character array functions need temporaries unless the
9787 character lengths are the same. */
9788 if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
9789 {
9790 if (expr1->ts.u.cl->length == NULL
9791 || expr1->ts.u.cl->length->expr_type != EXPR_CONSTANT)
9792 return true;
9793
9794 if (expr2->ts.u.cl->length == NULL
9795 || expr2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
9796 return true;
9797
9798 if (mpz_cmp (expr1->ts.u.cl->length->value.integer,
9799 expr2->ts.u.cl->length->value.integer) != 0)
9800 return true;
9801 }
9802
9803 /* Check that no LHS component references appear during an array
9804 reference. This is needed because we do not have the means to
9805 span any arbitrary stride with an array descriptor. This check
9806 is not needed for the rhs because the function result has to be
9807 a complete type. */
9808 seen_array_ref = false;
9809 for (ref = expr1->ref; ref; ref = ref->next)
9810 {
9811 if (ref->type == REF_ARRAY)
9812 seen_array_ref= true;
9813 else if (ref->type == REF_COMPONENT && seen_array_ref)
9814 return true;
9815 }
9816
9817 /* Check for a dependency. */
9818 if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
9819 expr2->value.function.esym,
9820 expr2->value.function.actual,
9821 NOT_ELEMENTAL))
9822 return true;
9823
9824 /* If we have reached here with an intrinsic function, we do not
9825 need a temporary except in the particular case that reallocation
9826 on assignment is active and the lhs is allocatable and a target,
9827 or a pointer which may be a subref pointer. FIXME: The last
9828 condition can go away when we use span in the intrinsics
9829 directly.*/
9830 if (expr2->value.function.isym)
9831 return (flag_realloc_lhs && sym->attr.allocatable && sym->attr.target)
9832 || (sym->attr.pointer && sym->attr.subref_array_pointer);
9833
9834 /* If the LHS is a dummy, we need a temporary if it is not
9835 INTENT(OUT). */
9836 if (sym->attr.dummy && sym->attr.intent != INTENT_OUT)
9837 return true;
9838
9839 /* If the lhs has been host_associated, is in common, a pointer or is
9840 a target and the function is not using a RESULT variable, aliasing
9841 can occur and a temporary is needed. */
9842 if ((sym->attr.host_assoc
9843 || sym->attr.in_common
9844 || sym->attr.pointer
9845 || sym->attr.cray_pointee
9846 || sym->attr.target)
9847 && expr2->symtree != NULL
9848 && expr2->symtree->n.sym == expr2->symtree->n.sym->result)
9849 return true;
9850
9851 /* A PURE function can unconditionally be called without a temporary. */
9852 if (expr2->value.function.esym != NULL
9853 && expr2->value.function.esym->attr.pure)
9854 return false;
9855
9856 /* Implicit_pure functions are those which could legally be declared
9857 to be PURE. */
9858 if (expr2->value.function.esym != NULL
9859 && expr2->value.function.esym->attr.implicit_pure)
9860 return false;
9861
9862 if (!sym->attr.use_assoc
9863 && !sym->attr.in_common
9864 && !sym->attr.pointer
9865 && !sym->attr.target
9866 && !sym->attr.cray_pointee
9867 && expr2->value.function.esym)
9868 {
9869 /* A temporary is not needed if the function is not contained and
9870 the variable is local or host associated and not a pointer or
9871 a target. */
9872 if (!expr2->value.function.esym->attr.contained)
9873 return false;
9874
9875 /* A temporary is not needed if the lhs has never been host
9876 associated and the procedure is contained. */
9877 else if (!sym->attr.host_assoc)
9878 return false;
9879
9880 /* A temporary is not needed if the variable is local and not
9881 a pointer, a target or a result. */
9882 if (sym->ns->parent
9883 && expr2->value.function.esym->ns == sym->ns->parent)
9884 return false;
9885 }
9886
9887 /* Default to temporary use. */
9888 return true;
9889 }
9890
9891
9892 /* Provide the loop info so that the lhs descriptor can be built for
9893 reallocatable assignments from extrinsic function calls. */
9894
9895 static void
9896 realloc_lhs_loop_for_fcn_call (gfc_se *se, locus *where, gfc_ss **ss,
9897 gfc_loopinfo *loop)
9898 {
9899 /* Signal that the function call should not be made by
9900 gfc_conv_loop_setup. */
9901 se->ss->is_alloc_lhs = 1;
9902 gfc_init_loopinfo (loop);
9903 gfc_add_ss_to_loop (loop, *ss);
9904 gfc_add_ss_to_loop (loop, se->ss);
9905 gfc_conv_ss_startstride (loop);
9906 gfc_conv_loop_setup (loop, where);
9907 gfc_copy_loopinfo_to_se (se, loop);
9908 gfc_add_block_to_block (&se->pre, &loop->pre);
9909 gfc_add_block_to_block (&se->pre, &loop->post);
9910 se->ss->is_alloc_lhs = 0;
9911 }
9912
9913
9914 /* For assignment to a reallocatable lhs from intrinsic functions,
9915 replace the se.expr (ie. the result) with a temporary descriptor.
9916 Null the data field so that the library allocates space for the
9917 result. Free the data of the original descriptor after the function,
9918 in case it appears in an argument expression and transfer the
9919 result to the original descriptor. */
9920
9921 static void
9922 fcncall_realloc_result (gfc_se *se, int rank)
9923 {
9924 tree desc;
9925 tree res_desc;
9926 tree tmp;
9927 tree offset;
9928 tree zero_cond;
9929 int n;
9930
9931 /* Use the allocation done by the library. Substitute the lhs
9932 descriptor with a copy, whose data field is nulled.*/
9933 desc = build_fold_indirect_ref_loc (input_location, se->expr);
9934 if (POINTER_TYPE_P (TREE_TYPE (desc)))
9935 desc = build_fold_indirect_ref_loc (input_location, desc);
9936
9937 /* Unallocated, the descriptor does not have a dtype. */
9938 tmp = gfc_conv_descriptor_dtype (desc);
9939 gfc_add_modify (&se->pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
9940
9941 res_desc = gfc_evaluate_now (desc, &se->pre);
9942 gfc_conv_descriptor_data_set (&se->pre, res_desc, null_pointer_node);
9943 se->expr = gfc_build_addr_expr (NULL_TREE, res_desc);
9944
9945 /* Free the lhs after the function call and copy the result data to
9946 the lhs descriptor. */
9947 tmp = gfc_conv_descriptor_data_get (desc);
9948 zero_cond = fold_build2_loc (input_location, EQ_EXPR,
9949 logical_type_node, tmp,
9950 build_int_cst (TREE_TYPE (tmp), 0));
9951 zero_cond = gfc_evaluate_now (zero_cond, &se->post);
9952 tmp = gfc_call_free (tmp);
9953 gfc_add_expr_to_block (&se->post, tmp);
9954
9955 tmp = gfc_conv_descriptor_data_get (res_desc);
9956 gfc_conv_descriptor_data_set (&se->post, desc, tmp);
9957
9958 /* Check that the shapes are the same between lhs and expression. */
9959 for (n = 0 ; n < rank; n++)
9960 {
9961 tree tmp1;
9962 tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
9963 tmp1 = gfc_conv_descriptor_lbound_get (res_desc, gfc_rank_cst[n]);
9964 tmp = fold_build2_loc (input_location, MINUS_EXPR,
9965 gfc_array_index_type, tmp, tmp1);
9966 tmp1 = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
9967 tmp = fold_build2_loc (input_location, MINUS_EXPR,
9968 gfc_array_index_type, tmp, tmp1);
9969 tmp1 = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
9970 tmp = fold_build2_loc (input_location, PLUS_EXPR,
9971 gfc_array_index_type, tmp, tmp1);
9972 tmp = fold_build2_loc (input_location, NE_EXPR,
9973 logical_type_node, tmp,
9974 gfc_index_zero_node);
9975 tmp = gfc_evaluate_now (tmp, &se->post);
9976 zero_cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
9977 logical_type_node, tmp,
9978 zero_cond);
9979 }
9980
9981 /* 'zero_cond' being true is equal to lhs not being allocated or the
9982 shapes being different. */
9983 zero_cond = gfc_evaluate_now (zero_cond, &se->post);
9984
9985 /* Now reset the bounds returned from the function call to bounds based
9986 on the lhs lbounds, except where the lhs is not allocated or the shapes
9987 of 'variable and 'expr' are different. Set the offset accordingly. */
9988 offset = gfc_index_zero_node;
9989 for (n = 0 ; n < rank; n++)
9990 {
9991 tree lbound;
9992
9993 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
9994 lbound = fold_build3_loc (input_location, COND_EXPR,
9995 gfc_array_index_type, zero_cond,
9996 gfc_index_one_node, lbound);
9997 lbound = gfc_evaluate_now (lbound, &se->post);
9998
9999 tmp = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
10000 tmp = fold_build2_loc (input_location, PLUS_EXPR,
10001 gfc_array_index_type, tmp, lbound);
10002 gfc_conv_descriptor_lbound_set (&se->post, desc,
10003 gfc_rank_cst[n], lbound);
10004 gfc_conv_descriptor_ubound_set (&se->post, desc,
10005 gfc_rank_cst[n], tmp);
10006
10007 /* Set stride and accumulate the offset. */
10008 tmp = gfc_conv_descriptor_stride_get (res_desc, gfc_rank_cst[n]);
10009 gfc_conv_descriptor_stride_set (&se->post, desc,
10010 gfc_rank_cst[n], tmp);
10011 tmp = fold_build2_loc (input_location, MULT_EXPR,
10012 gfc_array_index_type, lbound, tmp);
10013 offset = fold_build2_loc (input_location, MINUS_EXPR,
10014 gfc_array_index_type, offset, tmp);
10015 offset = gfc_evaluate_now (offset, &se->post);
10016 }
10017
10018 gfc_conv_descriptor_offset_set (&se->post, desc, offset);
10019 }
10020
10021
10022
10023 /* Try to translate array(:) = func (...), where func is a transformational
10024 array function, without using a temporary. Returns NULL if this isn't the
10025 case. */
10026
10027 static tree
10028 gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
10029 {
10030 gfc_se se;
10031 gfc_ss *ss = NULL;
10032 gfc_component *comp = NULL;
10033 gfc_loopinfo loop;
10034
10035 if (arrayfunc_assign_needs_temporary (expr1, expr2))
10036 return NULL;
10037
10038 /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
10039 functions. */
10040 comp = gfc_get_proc_ptr_comp (expr2);
10041
10042 if (!(expr2->value.function.isym
10043 || (comp && comp->attr.dimension)
10044 || (!comp && gfc_return_by_reference (expr2->value.function.esym)
10045 && expr2->value.function.esym->result->attr.dimension)))
10046 return NULL;
10047
10048 gfc_init_se (&se, NULL);
10049 gfc_start_block (&se.pre);
10050 se.want_pointer = 1;
10051
10052 gfc_conv_array_parameter (&se, expr1, false, NULL, NULL, NULL);
10053
10054 if (expr1->ts.type == BT_DERIVED
10055 && expr1->ts.u.derived->attr.alloc_comp)
10056 {
10057 tree tmp;
10058 tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, se.expr,
10059 expr1->rank);
10060 gfc_add_expr_to_block (&se.pre, tmp);
10061 }
10062
10063 se.direct_byref = 1;
10064 se.ss = gfc_walk_expr (expr2);
10065 gcc_assert (se.ss != gfc_ss_terminator);
10066
10067 /* Reallocate on assignment needs the loopinfo for extrinsic functions.
10068 This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
10069 Clearly, this cannot be done for an allocatable function result, since
10070 the shape of the result is unknown and, in any case, the function must
10071 correctly take care of the reallocation internally. For intrinsic
10072 calls, the array data is freed and the library takes care of allocation.
10073 TODO: Add logic of trans-array.c: gfc_alloc_allocatable_for_assignment
10074 to the library. */
10075 if (flag_realloc_lhs
10076 && gfc_is_reallocatable_lhs (expr1)
10077 && !gfc_expr_attr (expr1).codimension
10078 && !gfc_is_coindexed (expr1)
10079 && !(expr2->value.function.esym
10080 && expr2->value.function.esym->result->attr.allocatable))
10081 {
10082 realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
10083
10084 if (!expr2->value.function.isym)
10085 {
10086 ss = gfc_walk_expr (expr1);
10087 gcc_assert (ss != gfc_ss_terminator);
10088
10089 realloc_lhs_loop_for_fcn_call (&se, &expr1->where, &ss, &loop);
10090 ss->is_alloc_lhs = 1;
10091 }
10092 else
10093 fcncall_realloc_result (&se, expr1->rank);
10094 }
10095
10096 gfc_conv_function_expr (&se, expr2);
10097 gfc_add_block_to_block (&se.pre, &se.post);
10098
10099 if (ss)
10100 gfc_cleanup_loop (&loop);
10101 else
10102 gfc_free_ss_chain (se.ss);
10103
10104 return gfc_finish_block (&se.pre);
10105 }
10106
10107
10108 /* Try to efficiently translate array(:) = 0. Return NULL if this
10109 can't be done. */
10110
10111 static tree
10112 gfc_trans_zero_assign (gfc_expr * expr)
10113 {
10114 tree dest, len, type;
10115 tree tmp;
10116 gfc_symbol *sym;
10117
10118 sym = expr->symtree->n.sym;
10119 dest = gfc_get_symbol_decl (sym);
10120
10121 type = TREE_TYPE (dest);
10122 if (POINTER_TYPE_P (type))
10123 type = TREE_TYPE (type);
10124 if (!GFC_ARRAY_TYPE_P (type))
10125 return NULL_TREE;
10126
10127 /* Determine the length of the array. */
10128 len = GFC_TYPE_ARRAY_SIZE (type);
10129 if (!len || TREE_CODE (len) != INTEGER_CST)
10130 return NULL_TREE;
10131
10132 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
10133 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
10134 fold_convert (gfc_array_index_type, tmp));
10135
10136 /* If we are zeroing a local array avoid taking its address by emitting
10137 a = {} instead. */
10138 if (!POINTER_TYPE_P (TREE_TYPE (dest)))
10139 return build2_loc (input_location, MODIFY_EXPR, void_type_node,
10140 dest, build_constructor (TREE_TYPE (dest),
10141 NULL));
10142
10143 /* Convert arguments to the correct types. */
10144 dest = fold_convert (pvoid_type_node, dest);
10145 len = fold_convert (size_type_node, len);
10146
10147 /* Construct call to __builtin_memset. */
10148 tmp = build_call_expr_loc (input_location,
10149 builtin_decl_explicit (BUILT_IN_MEMSET),
10150 3, dest, integer_zero_node, len);
10151 return fold_convert (void_type_node, tmp);
10152 }
10153
10154
10155 /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
10156 that constructs the call to __builtin_memcpy. */
10157
10158 tree
10159 gfc_build_memcpy_call (tree dst, tree src, tree len)
10160 {
10161 tree tmp;
10162
10163 /* Convert arguments to the correct types. */
10164 if (!POINTER_TYPE_P (TREE_TYPE (dst)))
10165 dst = gfc_build_addr_expr (pvoid_type_node, dst);
10166 else
10167 dst = fold_convert (pvoid_type_node, dst);
10168
10169 if (!POINTER_TYPE_P (TREE_TYPE (src)))
10170 src = gfc_build_addr_expr (pvoid_type_node, src);
10171 else
10172 src = fold_convert (pvoid_type_node, src);
10173
10174 len = fold_convert (size_type_node, len);
10175
10176 /* Construct call to __builtin_memcpy. */
10177 tmp = build_call_expr_loc (input_location,
10178 builtin_decl_explicit (BUILT_IN_MEMCPY),
10179 3, dst, src, len);
10180 return fold_convert (void_type_node, tmp);
10181 }
10182
10183
10184 /* Try to efficiently translate dst(:) = src(:). Return NULL if this
10185 can't be done. EXPR1 is the destination/lhs and EXPR2 is the
10186 source/rhs, both are gfc_full_array_ref_p which have been checked for
10187 dependencies. */
10188
10189 static tree
10190 gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
10191 {
10192 tree dst, dlen, dtype;
10193 tree src, slen, stype;
10194 tree tmp;
10195
10196 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
10197 src = gfc_get_symbol_decl (expr2->symtree->n.sym);
10198
10199 dtype = TREE_TYPE (dst);
10200 if (POINTER_TYPE_P (dtype))
10201 dtype = TREE_TYPE (dtype);
10202 stype = TREE_TYPE (src);
10203 if (POINTER_TYPE_P (stype))
10204 stype = TREE_TYPE (stype);
10205
10206 if (!GFC_ARRAY_TYPE_P (dtype) || !GFC_ARRAY_TYPE_P (stype))
10207 return NULL_TREE;
10208
10209 /* Determine the lengths of the arrays. */
10210 dlen = GFC_TYPE_ARRAY_SIZE (dtype);
10211 if (!dlen || TREE_CODE (dlen) != INTEGER_CST)
10212 return NULL_TREE;
10213 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
10214 dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
10215 dlen, fold_convert (gfc_array_index_type, tmp));
10216
10217 slen = GFC_TYPE_ARRAY_SIZE (stype);
10218 if (!slen || TREE_CODE (slen) != INTEGER_CST)
10219 return NULL_TREE;
10220 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (stype));
10221 slen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
10222 slen, fold_convert (gfc_array_index_type, tmp));
10223
10224 /* Sanity check that they are the same. This should always be
10225 the case, as we should already have checked for conformance. */
10226 if (!tree_int_cst_equal (slen, dlen))
10227 return NULL_TREE;
10228
10229 return gfc_build_memcpy_call (dst, src, dlen);
10230 }
10231
10232
10233 /* Try to efficiently translate array(:) = (/ ... /). Return NULL if
10234 this can't be done. EXPR1 is the destination/lhs for which
10235 gfc_full_array_ref_p is true, and EXPR2 is the source/rhs. */
10236
10237 static tree
10238 gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
10239 {
10240 unsigned HOST_WIDE_INT nelem;
10241 tree dst, dtype;
10242 tree src, stype;
10243 tree len;
10244 tree tmp;
10245
10246 nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
10247 if (nelem == 0)
10248 return NULL_TREE;
10249
10250 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
10251 dtype = TREE_TYPE (dst);
10252 if (POINTER_TYPE_P (dtype))
10253 dtype = TREE_TYPE (dtype);
10254 if (!GFC_ARRAY_TYPE_P (dtype))
10255 return NULL_TREE;
10256
10257 /* Determine the lengths of the array. */
10258 len = GFC_TYPE_ARRAY_SIZE (dtype);
10259 if (!len || TREE_CODE (len) != INTEGER_CST)
10260 return NULL_TREE;
10261
10262 /* Confirm that the constructor is the same size. */
10263 if (compare_tree_int (len, nelem) != 0)
10264 return NULL_TREE;
10265
10266 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
10267 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
10268 fold_convert (gfc_array_index_type, tmp));
10269
10270 stype = gfc_typenode_for_spec (&expr2->ts);
10271 src = gfc_build_constant_array_constructor (expr2, stype);
10272
10273 return gfc_build_memcpy_call (dst, src, len);
10274 }
10275
10276
10277 /* Tells whether the expression is to be treated as a variable reference. */
10278
10279 bool
10280 gfc_expr_is_variable (gfc_expr *expr)
10281 {
10282 gfc_expr *arg;
10283 gfc_component *comp;
10284 gfc_symbol *func_ifc;
10285
10286 if (expr->expr_type == EXPR_VARIABLE)
10287 return true;
10288
10289 arg = gfc_get_noncopying_intrinsic_argument (expr);
10290 if (arg)
10291 {
10292 gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
10293 return gfc_expr_is_variable (arg);
10294 }
10295
10296 /* A data-pointer-returning function should be considered as a variable
10297 too. */
10298 if (expr->expr_type == EXPR_FUNCTION
10299 && expr->ref == NULL)
10300 {
10301 if (expr->value.function.isym != NULL)
10302 return false;
10303
10304 if (expr->value.function.esym != NULL)
10305 {
10306 func_ifc = expr->value.function.esym;
10307 goto found_ifc;
10308 }
10309 else
10310 {
10311 gcc_assert (expr->symtree);
10312 func_ifc = expr->symtree->n.sym;
10313 goto found_ifc;
10314 }
10315
10316 gcc_unreachable ();
10317 }
10318
10319 comp = gfc_get_proc_ptr_comp (expr);
10320 if ((expr->expr_type == EXPR_PPC || expr->expr_type == EXPR_FUNCTION)
10321 && comp)
10322 {
10323 func_ifc = comp->ts.interface;
10324 goto found_ifc;
10325 }
10326
10327 if (expr->expr_type == EXPR_COMPCALL)
10328 {
10329 gcc_assert (!expr->value.compcall.tbp->is_generic);
10330 func_ifc = expr->value.compcall.tbp->u.specific->n.sym;
10331 goto found_ifc;
10332 }
10333
10334 return false;
10335
10336 found_ifc:
10337 gcc_assert (func_ifc->attr.function
10338 && func_ifc->result != NULL);
10339 return func_ifc->result->attr.pointer;
10340 }
10341
10342
10343 /* Is the lhs OK for automatic reallocation? */
10344
10345 static bool
10346 is_scalar_reallocatable_lhs (gfc_expr *expr)
10347 {
10348 gfc_ref * ref;
10349
10350 /* An allocatable variable with no reference. */
10351 if (expr->symtree->n.sym->attr.allocatable
10352 && !expr->ref)
10353 return true;
10354
10355 /* All that can be left are allocatable components. However, we do
10356 not check for allocatable components here because the expression
10357 could be an allocatable component of a pointer component. */
10358 if (expr->symtree->n.sym->ts.type != BT_DERIVED
10359 && expr->symtree->n.sym->ts.type != BT_CLASS)
10360 return false;
10361
10362 /* Find an allocatable component ref last. */
10363 for (ref = expr->ref; ref; ref = ref->next)
10364 if (ref->type == REF_COMPONENT
10365 && !ref->next
10366 && ref->u.c.component->attr.allocatable)
10367 return true;
10368
10369 return false;
10370 }
10371
10372
10373 /* Allocate or reallocate scalar lhs, as necessary. */
10374
10375 static void
10376 alloc_scalar_allocatable_for_assignment (stmtblock_t *block,
10377 tree string_length,
10378 gfc_expr *expr1,
10379 gfc_expr *expr2)
10380
10381 {
10382 tree cond;
10383 tree tmp;
10384 tree size;
10385 tree size_in_bytes;
10386 tree jump_label1;
10387 tree jump_label2;
10388 gfc_se lse;
10389 gfc_ref *ref;
10390
10391 if (!expr1 || expr1->rank)
10392 return;
10393
10394 if (!expr2 || expr2->rank)
10395 return;
10396
10397 for (ref = expr1->ref; ref; ref = ref->next)
10398 if (ref->type == REF_SUBSTRING)
10399 return;
10400
10401 realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
10402
10403 /* Since this is a scalar lhs, we can afford to do this. That is,
10404 there is no risk of side effects being repeated. */
10405 gfc_init_se (&lse, NULL);
10406 lse.want_pointer = 1;
10407 gfc_conv_expr (&lse, expr1);
10408
10409 jump_label1 = gfc_build_label_decl (NULL_TREE);
10410 jump_label2 = gfc_build_label_decl (NULL_TREE);
10411
10412 /* Do the allocation if the lhs is NULL. Otherwise go to label 1. */
10413 tmp = build_int_cst (TREE_TYPE (lse.expr), 0);
10414 cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
10415 lse.expr, tmp);
10416 tmp = build3_v (COND_EXPR, cond,
10417 build1_v (GOTO_EXPR, jump_label1),
10418 build_empty_stmt (input_location));
10419 gfc_add_expr_to_block (block, tmp);
10420
10421 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
10422 {
10423 /* Use the rhs string length and the lhs element size. */
10424 size = string_length;
10425 tmp = TREE_TYPE (gfc_typenode_for_spec (&expr1->ts));
10426 tmp = TYPE_SIZE_UNIT (tmp);
10427 size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
10428 TREE_TYPE (tmp), tmp,
10429 fold_convert (TREE_TYPE (tmp), size));
10430 }
10431 else
10432 {
10433 /* Otherwise use the length in bytes of the rhs. */
10434 size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr1->ts));
10435 size_in_bytes = size;
10436 }
10437
10438 size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
10439 size_in_bytes, size_one_node);
10440
10441 if (gfc_caf_attr (expr1).codimension && flag_coarray == GFC_FCOARRAY_LIB)
10442 {
10443 tree caf_decl, token;
10444 gfc_se caf_se;
10445 symbol_attribute attr;
10446
10447 gfc_clear_attr (&attr);
10448 gfc_init_se (&caf_se, NULL);
10449
10450 caf_decl = gfc_get_tree_for_caf_expr (expr1);
10451 gfc_get_caf_token_offset (&caf_se, &token, NULL, caf_decl, NULL_TREE,
10452 NULL);
10453 gfc_add_block_to_block (block, &caf_se.pre);
10454 gfc_allocate_allocatable (block, lse.expr, size_in_bytes,
10455 gfc_build_addr_expr (NULL_TREE, token),
10456 NULL_TREE, NULL_TREE, NULL_TREE, jump_label1,
10457 expr1, 1);
10458 }
10459 else if (expr1->ts.type == BT_DERIVED && expr1->ts.u.derived->attr.alloc_comp)
10460 {
10461 tmp = build_call_expr_loc (input_location,
10462 builtin_decl_explicit (BUILT_IN_CALLOC),
10463 2, build_one_cst (size_type_node),
10464 size_in_bytes);
10465 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
10466 gfc_add_modify (block, lse.expr, tmp);
10467 }
10468 else
10469 {
10470 tmp = build_call_expr_loc (input_location,
10471 builtin_decl_explicit (BUILT_IN_MALLOC),
10472 1, size_in_bytes);
10473 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
10474 gfc_add_modify (block, lse.expr, tmp);
10475 }
10476
10477 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
10478 {
10479 /* Deferred characters need checking for lhs and rhs string
10480 length. Other deferred parameter variables will have to
10481 come here too. */
10482 tmp = build1_v (GOTO_EXPR, jump_label2);
10483 gfc_add_expr_to_block (block, tmp);
10484 }
10485 tmp = build1_v (LABEL_EXPR, jump_label1);
10486 gfc_add_expr_to_block (block, tmp);
10487
10488 /* For a deferred length character, reallocate if lengths of lhs and
10489 rhs are different. */
10490 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
10491 {
10492 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
10493 lse.string_length,
10494 fold_convert (TREE_TYPE (lse.string_length),
10495 size));
10496 /* Jump past the realloc if the lengths are the same. */
10497 tmp = build3_v (COND_EXPR, cond,
10498 build1_v (GOTO_EXPR, jump_label2),
10499 build_empty_stmt (input_location));
10500 gfc_add_expr_to_block (block, tmp);
10501 tmp = build_call_expr_loc (input_location,
10502 builtin_decl_explicit (BUILT_IN_REALLOC),
10503 2, fold_convert (pvoid_type_node, lse.expr),
10504 size_in_bytes);
10505 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
10506 gfc_add_modify (block, lse.expr, tmp);
10507 tmp = build1_v (LABEL_EXPR, jump_label2);
10508 gfc_add_expr_to_block (block, tmp);
10509
10510 /* Update the lhs character length. */
10511 size = string_length;
10512 gfc_add_modify (block, lse.string_length,
10513 fold_convert (TREE_TYPE (lse.string_length), size));
10514 }
10515 }
10516
10517 /* Check for assignments of the type
10518
10519 a = a + 4
10520
10521 to make sure we do not check for reallocation unneccessarily. */
10522
10523
10524 static bool
10525 is_runtime_conformable (gfc_expr *expr1, gfc_expr *expr2)
10526 {
10527 gfc_actual_arglist *a;
10528 gfc_expr *e1, *e2;
10529
10530 switch (expr2->expr_type)
10531 {
10532 case EXPR_VARIABLE:
10533 return gfc_dep_compare_expr (expr1, expr2) == 0;
10534
10535 case EXPR_FUNCTION:
10536 if (expr2->value.function.esym
10537 && expr2->value.function.esym->attr.elemental)
10538 {
10539 for (a = expr2->value.function.actual; a != NULL; a = a->next)
10540 {
10541 e1 = a->expr;
10542 if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
10543 return false;
10544 }
10545 return true;
10546 }
10547 else if (expr2->value.function.isym
10548 && expr2->value.function.isym->elemental)
10549 {
10550 for (a = expr2->value.function.actual; a != NULL; a = a->next)
10551 {
10552 e1 = a->expr;
10553 if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
10554 return false;
10555 }
10556 return true;
10557 }
10558
10559 break;
10560
10561 case EXPR_OP:
10562 switch (expr2->value.op.op)
10563 {
10564 case INTRINSIC_NOT:
10565 case INTRINSIC_UPLUS:
10566 case INTRINSIC_UMINUS:
10567 case INTRINSIC_PARENTHESES:
10568 return is_runtime_conformable (expr1, expr2->value.op.op1);
10569
10570 case INTRINSIC_PLUS:
10571 case INTRINSIC_MINUS:
10572 case INTRINSIC_TIMES:
10573 case INTRINSIC_DIVIDE:
10574 case INTRINSIC_POWER:
10575 case INTRINSIC_AND:
10576 case INTRINSIC_OR:
10577 case INTRINSIC_EQV:
10578 case INTRINSIC_NEQV:
10579 case INTRINSIC_EQ:
10580 case INTRINSIC_NE:
10581 case INTRINSIC_GT:
10582 case INTRINSIC_GE:
10583 case INTRINSIC_LT:
10584 case INTRINSIC_LE:
10585 case INTRINSIC_EQ_OS:
10586 case INTRINSIC_NE_OS:
10587 case INTRINSIC_GT_OS:
10588 case INTRINSIC_GE_OS:
10589 case INTRINSIC_LT_OS:
10590 case INTRINSIC_LE_OS:
10591
10592 e1 = expr2->value.op.op1;
10593 e2 = expr2->value.op.op2;
10594
10595 if (e1->rank == 0 && e2->rank > 0)
10596 return is_runtime_conformable (expr1, e2);
10597 else if (e1->rank > 0 && e2->rank == 0)
10598 return is_runtime_conformable (expr1, e1);
10599 else if (e1->rank > 0 && e2->rank > 0)
10600 return is_runtime_conformable (expr1, e1)
10601 && is_runtime_conformable (expr1, e2);
10602 break;
10603
10604 default:
10605 break;
10606
10607 }
10608
10609 break;
10610
10611 default:
10612 break;
10613 }
10614 return false;
10615 }
10616
10617
10618 static tree
10619 trans_class_assignment (stmtblock_t *block, gfc_expr *lhs, gfc_expr *rhs,
10620 gfc_se *lse, gfc_se *rse, bool use_vptr_copy,
10621 bool class_realloc)
10622 {
10623 tree tmp, fcn, stdcopy, to_len, from_len, vptr;
10624 vec<tree, va_gc> *args = NULL;
10625
10626 vptr = trans_class_vptr_len_assignment (block, lhs, rhs, rse, &to_len,
10627 &from_len);
10628
10629 /* Generate allocation of the lhs. */
10630 if (class_realloc)
10631 {
10632 stmtblock_t alloc;
10633 tree class_han;
10634
10635 tmp = gfc_vptr_size_get (vptr);
10636 class_han = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
10637 ? gfc_class_data_get (lse->expr) : lse->expr;
10638 gfc_init_block (&alloc);
10639 gfc_allocate_using_malloc (&alloc, class_han, tmp, NULL_TREE);
10640 tmp = fold_build2_loc (input_location, EQ_EXPR,
10641 logical_type_node, class_han,
10642 build_int_cst (prvoid_type_node, 0));
10643 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
10644 gfc_unlikely (tmp,
10645 PRED_FORTRAN_FAIL_ALLOC),
10646 gfc_finish_block (&alloc),
10647 build_empty_stmt (input_location));
10648 gfc_add_expr_to_block (&lse->pre, tmp);
10649 }
10650
10651 fcn = gfc_vptr_copy_get (vptr);
10652
10653 tmp = GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr))
10654 ? gfc_class_data_get (rse->expr) : rse->expr;
10655 if (use_vptr_copy)
10656 {
10657 if (!POINTER_TYPE_P (TREE_TYPE (tmp))
10658 || INDIRECT_REF_P (tmp)
10659 || (rhs->ts.type == BT_DERIVED
10660 && rhs->ts.u.derived->attr.unlimited_polymorphic
10661 && !rhs->ts.u.derived->attr.pointer
10662 && !rhs->ts.u.derived->attr.allocatable)
10663 || (UNLIMITED_POLY (rhs)
10664 && !CLASS_DATA (rhs)->attr.pointer
10665 && !CLASS_DATA (rhs)->attr.allocatable))
10666 vec_safe_push (args, gfc_build_addr_expr (NULL_TREE, tmp));
10667 else
10668 vec_safe_push (args, tmp);
10669 tmp = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
10670 ? gfc_class_data_get (lse->expr) : lse->expr;
10671 if (!POINTER_TYPE_P (TREE_TYPE (tmp))
10672 || INDIRECT_REF_P (tmp)
10673 || (lhs->ts.type == BT_DERIVED
10674 && lhs->ts.u.derived->attr.unlimited_polymorphic
10675 && !lhs->ts.u.derived->attr.pointer
10676 && !lhs->ts.u.derived->attr.allocatable)
10677 || (UNLIMITED_POLY (lhs)
10678 && !CLASS_DATA (lhs)->attr.pointer
10679 && !CLASS_DATA (lhs)->attr.allocatable))
10680 vec_safe_push (args, gfc_build_addr_expr (NULL_TREE, tmp));
10681 else
10682 vec_safe_push (args, tmp);
10683
10684 stdcopy = build_call_vec (TREE_TYPE (TREE_TYPE (fcn)), fcn, args);
10685
10686 if (to_len != NULL_TREE && !integer_zerop (from_len))
10687 {
10688 tree extcopy;
10689 vec_safe_push (args, from_len);
10690 vec_safe_push (args, to_len);
10691 extcopy = build_call_vec (TREE_TYPE (TREE_TYPE (fcn)), fcn, args);
10692
10693 tmp = fold_build2_loc (input_location, GT_EXPR,
10694 logical_type_node, from_len,
10695 build_zero_cst (TREE_TYPE (from_len)));
10696 return fold_build3_loc (input_location, COND_EXPR,
10697 void_type_node, tmp,
10698 extcopy, stdcopy);
10699 }
10700 else
10701 return stdcopy;
10702 }
10703 else
10704 {
10705 tree rhst = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
10706 ? gfc_class_data_get (lse->expr) : lse->expr;
10707 stmtblock_t tblock;
10708 gfc_init_block (&tblock);
10709 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
10710 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
10711 if (!POINTER_TYPE_P (TREE_TYPE (rhst)))
10712 rhst = gfc_build_addr_expr (NULL_TREE, rhst);
10713 /* When coming from a ptr_copy lhs and rhs are swapped. */
10714 gfc_add_modify_loc (input_location, &tblock, rhst,
10715 fold_convert (TREE_TYPE (rhst), tmp));
10716 return gfc_finish_block (&tblock);
10717 }
10718 }
10719
10720 /* Subroutine of gfc_trans_assignment that actually scalarizes the
10721 assignment. EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
10722 init_flag indicates initialization expressions and dealloc that no
10723 deallocate prior assignment is needed (if in doubt, set true).
10724 When PTR_COPY is set and expr1 is a class type, then use the _vptr-copy
10725 routine instead of a pointer assignment. Alias resolution is only done,
10726 when MAY_ALIAS is set (the default). This flag is used by ALLOCATE()
10727 where it is known, that newly allocated memory on the lhs can never be
10728 an alias of the rhs. */
10729
10730 static tree
10731 gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
10732 bool dealloc, bool use_vptr_copy, bool may_alias)
10733 {
10734 gfc_se lse;
10735 gfc_se rse;
10736 gfc_ss *lss;
10737 gfc_ss *lss_section;
10738 gfc_ss *rss;
10739 gfc_loopinfo loop;
10740 tree tmp;
10741 stmtblock_t block;
10742 stmtblock_t body;
10743 bool l_is_temp;
10744 bool scalar_to_array;
10745 tree string_length;
10746 int n;
10747 bool maybe_workshare = false, lhs_refs_comp = false, rhs_refs_comp = false;
10748 symbol_attribute lhs_caf_attr, rhs_caf_attr, lhs_attr;
10749 bool is_poly_assign;
10750
10751 /* Assignment of the form lhs = rhs. */
10752 gfc_start_block (&block);
10753
10754 gfc_init_se (&lse, NULL);
10755 gfc_init_se (&rse, NULL);
10756
10757 /* Walk the lhs. */
10758 lss = gfc_walk_expr (expr1);
10759 if (gfc_is_reallocatable_lhs (expr1))
10760 {
10761 lss->no_bounds_check = 1;
10762 if (!(expr2->expr_type == EXPR_FUNCTION
10763 && expr2->value.function.isym != NULL
10764 && !(expr2->value.function.isym->elemental
10765 || expr2->value.function.isym->conversion)))
10766 lss->is_alloc_lhs = 1;
10767 }
10768 else
10769 lss->no_bounds_check = expr1->no_bounds_check;
10770
10771 rss = NULL;
10772
10773 if ((expr1->ts.type == BT_DERIVED)
10774 && (gfc_is_class_array_function (expr2)
10775 || gfc_is_alloc_class_scalar_function (expr2)))
10776 expr2->must_finalize = 1;
10777
10778 /* Checking whether a class assignment is desired is quite complicated and
10779 needed at two locations, so do it once only before the information is
10780 needed. */
10781 lhs_attr = gfc_expr_attr (expr1);
10782 is_poly_assign = (use_vptr_copy || lhs_attr.pointer
10783 || (lhs_attr.allocatable && !lhs_attr.dimension))
10784 && (expr1->ts.type == BT_CLASS
10785 || gfc_is_class_array_ref (expr1, NULL)
10786 || gfc_is_class_scalar_expr (expr1)
10787 || gfc_is_class_array_ref (expr2, NULL)
10788 || gfc_is_class_scalar_expr (expr2));
10789
10790
10791 /* Only analyze the expressions for coarray properties, when in coarray-lib
10792 mode. */
10793 if (flag_coarray == GFC_FCOARRAY_LIB)
10794 {
10795 lhs_caf_attr = gfc_caf_attr (expr1, false, &lhs_refs_comp);
10796 rhs_caf_attr = gfc_caf_attr (expr2, false, &rhs_refs_comp);
10797 }
10798
10799 if (lss != gfc_ss_terminator)
10800 {
10801 /* The assignment needs scalarization. */
10802 lss_section = lss;
10803
10804 /* Find a non-scalar SS from the lhs. */
10805 while (lss_section != gfc_ss_terminator
10806 && lss_section->info->type != GFC_SS_SECTION)
10807 lss_section = lss_section->next;
10808
10809 gcc_assert (lss_section != gfc_ss_terminator);
10810
10811 /* Initialize the scalarizer. */
10812 gfc_init_loopinfo (&loop);
10813
10814 /* Walk the rhs. */
10815 rss = gfc_walk_expr (expr2);
10816 if (rss == gfc_ss_terminator)
10817 /* The rhs is scalar. Add a ss for the expression. */
10818 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
10819 /* When doing a class assign, then the handle to the rhs needs to be a
10820 pointer to allow for polymorphism. */
10821 if (is_poly_assign && expr2->rank == 0 && !UNLIMITED_POLY (expr2))
10822 rss->info->type = GFC_SS_REFERENCE;
10823
10824 rss->no_bounds_check = expr2->no_bounds_check;
10825 /* Associate the SS with the loop. */
10826 gfc_add_ss_to_loop (&loop, lss);
10827 gfc_add_ss_to_loop (&loop, rss);
10828
10829 /* Calculate the bounds of the scalarization. */
10830 gfc_conv_ss_startstride (&loop);
10831 /* Enable loop reversal. */
10832 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
10833 loop.reverse[n] = GFC_ENABLE_REVERSE;
10834 /* Resolve any data dependencies in the statement. */
10835 if (may_alias)
10836 gfc_conv_resolve_dependencies (&loop, lss, rss);
10837 /* Setup the scalarizing loops. */
10838 gfc_conv_loop_setup (&loop, &expr2->where);
10839
10840 /* Setup the gfc_se structures. */
10841 gfc_copy_loopinfo_to_se (&lse, &loop);
10842 gfc_copy_loopinfo_to_se (&rse, &loop);
10843
10844 rse.ss = rss;
10845 gfc_mark_ss_chain_used (rss, 1);
10846 if (loop.temp_ss == NULL)
10847 {
10848 lse.ss = lss;
10849 gfc_mark_ss_chain_used (lss, 1);
10850 }
10851 else
10852 {
10853 lse.ss = loop.temp_ss;
10854 gfc_mark_ss_chain_used (lss, 3);
10855 gfc_mark_ss_chain_used (loop.temp_ss, 3);
10856 }
10857
10858 /* Allow the scalarizer to workshare array assignments. */
10859 if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_BODY))
10860 == OMPWS_WORKSHARE_FLAG
10861 && loop.temp_ss == NULL)
10862 {
10863 maybe_workshare = true;
10864 ompws_flags |= OMPWS_SCALARIZER_WS | OMPWS_SCALARIZER_BODY;
10865 }
10866
10867 /* Start the scalarized loop body. */
10868 gfc_start_scalarized_body (&loop, &body);
10869 }
10870 else
10871 gfc_init_block (&body);
10872
10873 l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
10874
10875 /* Translate the expression. */
10876 rse.want_coarray = flag_coarray == GFC_FCOARRAY_LIB && init_flag
10877 && lhs_caf_attr.codimension;
10878 gfc_conv_expr (&rse, expr2);
10879
10880 /* Deal with the case of a scalar class function assigned to a derived type. */
10881 if (gfc_is_alloc_class_scalar_function (expr2)
10882 && expr1->ts.type == BT_DERIVED)
10883 {
10884 rse.expr = gfc_class_data_get (rse.expr);
10885 rse.expr = build_fold_indirect_ref_loc (input_location, rse.expr);
10886 }
10887
10888 /* Stabilize a string length for temporaries. */
10889 if (expr2->ts.type == BT_CHARACTER && !expr1->ts.deferred
10890 && !(VAR_P (rse.string_length)
10891 || TREE_CODE (rse.string_length) == PARM_DECL
10892 || TREE_CODE (rse.string_length) == INDIRECT_REF))
10893 string_length = gfc_evaluate_now (rse.string_length, &rse.pre);
10894 else if (expr2->ts.type == BT_CHARACTER)
10895 {
10896 if (expr1->ts.deferred
10897 && gfc_expr_attr (expr1).allocatable
10898 && gfc_check_dependency (expr1, expr2, true))
10899 rse.string_length =
10900 gfc_evaluate_now_function_scope (rse.string_length, &rse.pre);
10901 string_length = rse.string_length;
10902 }
10903 else
10904 string_length = NULL_TREE;
10905
10906 if (l_is_temp)
10907 {
10908 gfc_conv_tmp_array_ref (&lse);
10909 if (expr2->ts.type == BT_CHARACTER)
10910 lse.string_length = string_length;
10911 }
10912 else
10913 {
10914 gfc_conv_expr (&lse, expr1);
10915 if (gfc_option.rtcheck & GFC_RTCHECK_MEM
10916 && !init_flag
10917 && gfc_expr_attr (expr1).allocatable
10918 && expr1->rank
10919 && !expr2->rank)
10920 {
10921 tree cond;
10922 const char* msg;
10923
10924 tmp = INDIRECT_REF_P (lse.expr)
10925 ? gfc_build_addr_expr (NULL_TREE, lse.expr) : lse.expr;
10926
10927 /* We should only get array references here. */
10928 gcc_assert (TREE_CODE (tmp) == POINTER_PLUS_EXPR
10929 || TREE_CODE (tmp) == ARRAY_REF);
10930
10931 /* 'tmp' is either the pointer to the array(POINTER_PLUS_EXPR)
10932 or the array itself(ARRAY_REF). */
10933 tmp = TREE_OPERAND (tmp, 0);
10934
10935 /* Provide the address of the array. */
10936 if (TREE_CODE (lse.expr) == ARRAY_REF)
10937 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
10938
10939 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
10940 tmp, build_int_cst (TREE_TYPE (tmp), 0));
10941 msg = _("Assignment of scalar to unallocated array");
10942 gfc_trans_runtime_check (true, false, cond, &loop.pre,
10943 &expr1->where, msg);
10944 }
10945
10946 /* Deallocate the lhs parameterized components if required. */
10947 if (dealloc && expr2->expr_type == EXPR_FUNCTION
10948 && !expr1->symtree->n.sym->attr.associate_var)
10949 {
10950 if (expr1->ts.type == BT_DERIVED
10951 && expr1->ts.u.derived
10952 && expr1->ts.u.derived->attr.pdt_type)
10953 {
10954 tmp = gfc_deallocate_pdt_comp (expr1->ts.u.derived, lse.expr,
10955 expr1->rank);
10956 gfc_add_expr_to_block (&lse.pre, tmp);
10957 }
10958 else if (expr1->ts.type == BT_CLASS
10959 && CLASS_DATA (expr1)->ts.u.derived
10960 && CLASS_DATA (expr1)->ts.u.derived->attr.pdt_type)
10961 {
10962 tmp = gfc_class_data_get (lse.expr);
10963 tmp = gfc_deallocate_pdt_comp (CLASS_DATA (expr1)->ts.u.derived,
10964 tmp, expr1->rank);
10965 gfc_add_expr_to_block (&lse.pre, tmp);
10966 }
10967 }
10968 }
10969
10970 /* Assignments of scalar derived types with allocatable components
10971 to arrays must be done with a deep copy and the rhs temporary
10972 must have its components deallocated afterwards. */
10973 scalar_to_array = (expr2->ts.type == BT_DERIVED
10974 && expr2->ts.u.derived->attr.alloc_comp
10975 && !gfc_expr_is_variable (expr2)
10976 && expr1->rank && !expr2->rank);
10977 scalar_to_array |= (expr1->ts.type == BT_DERIVED
10978 && expr1->rank
10979 && expr1->ts.u.derived->attr.alloc_comp
10980 && gfc_is_alloc_class_scalar_function (expr2));
10981 if (scalar_to_array && dealloc)
10982 {
10983 tmp = gfc_deallocate_alloc_comp_no_caf (expr2->ts.u.derived, rse.expr, 0);
10984 gfc_prepend_expr_to_block (&loop.post, tmp);
10985 }
10986
10987 /* When assigning a character function result to a deferred-length variable,
10988 the function call must happen before the (re)allocation of the lhs -
10989 otherwise the character length of the result is not known.
10990 NOTE 1: This relies on having the exact dependence of the length type
10991 parameter available to the caller; gfortran saves it in the .mod files.
10992 NOTE 2: Vector array references generate an index temporary that must
10993 not go outside the loop. Otherwise, variables should not generate
10994 a pre block.
10995 NOTE 3: The concatenation operation generates a temporary pointer,
10996 whose allocation must go to the innermost loop.
10997 NOTE 4: Elemental functions may generate a temporary, too. */
10998 if (flag_realloc_lhs
10999 && expr2->ts.type == BT_CHARACTER && expr1->ts.deferred
11000 && !(lss != gfc_ss_terminator
11001 && rss != gfc_ss_terminator
11002 && ((expr2->expr_type == EXPR_VARIABLE && expr2->rank)
11003 || (expr2->expr_type == EXPR_FUNCTION
11004 && expr2->value.function.esym != NULL
11005 && expr2->value.function.esym->attr.elemental)
11006 || (expr2->expr_type == EXPR_FUNCTION
11007 && expr2->value.function.isym != NULL
11008 && expr2->value.function.isym->elemental)
11009 || (expr2->expr_type == EXPR_OP
11010 && expr2->value.op.op == INTRINSIC_CONCAT))))
11011 gfc_add_block_to_block (&block, &rse.pre);
11012
11013 /* Nullify the allocatable components corresponding to those of the lhs
11014 derived type, so that the finalization of the function result does not
11015 affect the lhs of the assignment. Prepend is used to ensure that the
11016 nullification occurs before the call to the finalizer. In the case of
11017 a scalar to array assignment, this is done in gfc_trans_scalar_assign
11018 as part of the deep copy. */
11019 if (!scalar_to_array && expr1->ts.type == BT_DERIVED
11020 && (gfc_is_class_array_function (expr2)
11021 || gfc_is_alloc_class_scalar_function (expr2)))
11022 {
11023 tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, rse.expr, 0);
11024 gfc_prepend_expr_to_block (&rse.post, tmp);
11025 if (lss != gfc_ss_terminator && rss == gfc_ss_terminator)
11026 gfc_add_block_to_block (&loop.post, &rse.post);
11027 }
11028
11029 tmp = NULL_TREE;
11030
11031 if (is_poly_assign)
11032 tmp = trans_class_assignment (&body, expr1, expr2, &lse, &rse,
11033 use_vptr_copy || (lhs_attr.allocatable
11034 && !lhs_attr.dimension),
11035 flag_realloc_lhs && !lhs_attr.pointer);
11036 else if (flag_coarray == GFC_FCOARRAY_LIB
11037 && lhs_caf_attr.codimension && rhs_caf_attr.codimension
11038 && ((lhs_caf_attr.allocatable && lhs_refs_comp)
11039 || (rhs_caf_attr.allocatable && rhs_refs_comp)))
11040 {
11041 /* Only detour to caf_send[get][_by_ref] () when the lhs or rhs is an
11042 allocatable component, because those need to be accessed via the
11043 caf-runtime. No need to check for coindexes here, because resolve
11044 has rewritten those already. */
11045 gfc_code code;
11046 gfc_actual_arglist a1, a2;
11047 /* Clear the structures to prevent accessing garbage. */
11048 memset (&code, '\0', sizeof (gfc_code));
11049 memset (&a1, '\0', sizeof (gfc_actual_arglist));
11050 memset (&a2, '\0', sizeof (gfc_actual_arglist));
11051 a1.expr = expr1;
11052 a1.next = &a2;
11053 a2.expr = expr2;
11054 a2.next = NULL;
11055 code.ext.actual = &a1;
11056 code.resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
11057 tmp = gfc_conv_intrinsic_subroutine (&code);
11058 }
11059 else if (!is_poly_assign && expr2->must_finalize
11060 && expr1->ts.type == BT_CLASS
11061 && expr2->ts.type == BT_CLASS)
11062 {
11063 /* This case comes about when the scalarizer provides array element
11064 references. Use the vptr copy function, since this does a deep
11065 copy of allocatable components, without which the finalizer call */
11066 tmp = gfc_get_vptr_from_expr (rse.expr);
11067 if (tmp != NULL_TREE)
11068 {
11069 tree fcn = gfc_vptr_copy_get (tmp);
11070 if (POINTER_TYPE_P (TREE_TYPE (fcn)))
11071 fcn = build_fold_indirect_ref_loc (input_location, fcn);
11072 tmp = build_call_expr_loc (input_location,
11073 fcn, 2,
11074 gfc_build_addr_expr (NULL, rse.expr),
11075 gfc_build_addr_expr (NULL, lse.expr));
11076 }
11077 }
11078
11079 /* If nothing else works, do it the old fashioned way! */
11080 if (tmp == NULL_TREE)
11081 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
11082 gfc_expr_is_variable (expr2)
11083 || scalar_to_array
11084 || expr2->expr_type == EXPR_ARRAY,
11085 !(l_is_temp || init_flag) && dealloc,
11086 expr1->symtree->n.sym->attr.codimension);
11087
11088 /* Add the pre blocks to the body. */
11089 gfc_add_block_to_block (&body, &rse.pre);
11090 gfc_add_block_to_block (&body, &lse.pre);
11091 gfc_add_expr_to_block (&body, tmp);
11092 /* Add the post blocks to the body. */
11093 gfc_add_block_to_block (&body, &rse.post);
11094 gfc_add_block_to_block (&body, &lse.post);
11095
11096 if (lss == gfc_ss_terminator)
11097 {
11098 /* F2003: Add the code for reallocation on assignment. */
11099 if (flag_realloc_lhs && is_scalar_reallocatable_lhs (expr1)
11100 && !is_poly_assign)
11101 alloc_scalar_allocatable_for_assignment (&block, string_length,
11102 expr1, expr2);
11103
11104 /* Use the scalar assignment as is. */
11105 gfc_add_block_to_block (&block, &body);
11106 }
11107 else
11108 {
11109 gcc_assert (lse.ss == gfc_ss_terminator
11110 && rse.ss == gfc_ss_terminator);
11111
11112 if (l_is_temp)
11113 {
11114 gfc_trans_scalarized_loop_boundary (&loop, &body);
11115
11116 /* We need to copy the temporary to the actual lhs. */
11117 gfc_init_se (&lse, NULL);
11118 gfc_init_se (&rse, NULL);
11119 gfc_copy_loopinfo_to_se (&lse, &loop);
11120 gfc_copy_loopinfo_to_se (&rse, &loop);
11121
11122 rse.ss = loop.temp_ss;
11123 lse.ss = lss;
11124
11125 gfc_conv_tmp_array_ref (&rse);
11126 gfc_conv_expr (&lse, expr1);
11127
11128 gcc_assert (lse.ss == gfc_ss_terminator
11129 && rse.ss == gfc_ss_terminator);
11130
11131 if (expr2->ts.type == BT_CHARACTER)
11132 rse.string_length = string_length;
11133
11134 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
11135 false, dealloc);
11136 gfc_add_expr_to_block (&body, tmp);
11137 }
11138
11139 /* F2003: Allocate or reallocate lhs of allocatable array. */
11140 if (flag_realloc_lhs
11141 && gfc_is_reallocatable_lhs (expr1)
11142 && expr2->rank
11143 && !is_runtime_conformable (expr1, expr2))
11144 {
11145 realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
11146 ompws_flags &= ~OMPWS_SCALARIZER_WS;
11147 tmp = gfc_alloc_allocatable_for_assignment (&loop, expr1, expr2);
11148 if (tmp != NULL_TREE)
11149 gfc_add_expr_to_block (&loop.code[expr1->rank - 1], tmp);
11150 }
11151
11152 if (maybe_workshare)
11153 ompws_flags &= ~OMPWS_SCALARIZER_BODY;
11154
11155 /* Generate the copying loops. */
11156 gfc_trans_scalarizing_loops (&loop, &body);
11157
11158 /* Wrap the whole thing up. */
11159 gfc_add_block_to_block (&block, &loop.pre);
11160 gfc_add_block_to_block (&block, &loop.post);
11161
11162 gfc_cleanup_loop (&loop);
11163 }
11164
11165 return gfc_finish_block (&block);
11166 }
11167
11168
11169 /* Check whether EXPR is a copyable array. */
11170
11171 static bool
11172 copyable_array_p (gfc_expr * expr)
11173 {
11174 if (expr->expr_type != EXPR_VARIABLE)
11175 return false;
11176
11177 /* First check it's an array. */
11178 if (expr->rank < 1 || !expr->ref || expr->ref->next)
11179 return false;
11180
11181 if (!gfc_full_array_ref_p (expr->ref, NULL))
11182 return false;
11183
11184 /* Next check that it's of a simple enough type. */
11185 switch (expr->ts.type)
11186 {
11187 case BT_INTEGER:
11188 case BT_REAL:
11189 case BT_COMPLEX:
11190 case BT_LOGICAL:
11191 return true;
11192
11193 case BT_CHARACTER:
11194 return false;
11195
11196 case_bt_struct:
11197 return !expr->ts.u.derived->attr.alloc_comp;
11198
11199 default:
11200 break;
11201 }
11202
11203 return false;
11204 }
11205
11206 /* Translate an assignment. */
11207
11208 tree
11209 gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
11210 bool dealloc, bool use_vptr_copy, bool may_alias)
11211 {
11212 tree tmp;
11213
11214 /* Special case a single function returning an array. */
11215 if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
11216 {
11217 tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
11218 if (tmp)
11219 return tmp;
11220 }
11221
11222 /* Special case assigning an array to zero. */
11223 if (copyable_array_p (expr1)
11224 && is_zero_initializer_p (expr2))
11225 {
11226 tmp = gfc_trans_zero_assign (expr1);
11227 if (tmp)
11228 return tmp;
11229 }
11230
11231 /* Special case copying one array to another. */
11232 if (copyable_array_p (expr1)
11233 && copyable_array_p (expr2)
11234 && gfc_compare_types (&expr1->ts, &expr2->ts)
11235 && !gfc_check_dependency (expr1, expr2, 0))
11236 {
11237 tmp = gfc_trans_array_copy (expr1, expr2);
11238 if (tmp)
11239 return tmp;
11240 }
11241
11242 /* Special case initializing an array from a constant array constructor. */
11243 if (copyable_array_p (expr1)
11244 && expr2->expr_type == EXPR_ARRAY
11245 && gfc_compare_types (&expr1->ts, &expr2->ts))
11246 {
11247 tmp = gfc_trans_array_constructor_copy (expr1, expr2);
11248 if (tmp)
11249 return tmp;
11250 }
11251
11252 if (UNLIMITED_POLY (expr1) && expr1->rank
11253 && expr2->ts.type != BT_CLASS)
11254 use_vptr_copy = true;
11255
11256 /* Fallback to the scalarizer to generate explicit loops. */
11257 return gfc_trans_assignment_1 (expr1, expr2, init_flag, dealloc,
11258 use_vptr_copy, may_alias);
11259 }
11260
11261 tree
11262 gfc_trans_init_assign (gfc_code * code)
11263 {
11264 return gfc_trans_assignment (code->expr1, code->expr2, true, false, true);
11265 }
11266
11267 tree
11268 gfc_trans_assign (gfc_code * code)
11269 {
11270 return gfc_trans_assignment (code->expr1, code->expr2, false, true);
11271 }