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