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