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