2016-01-27 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / trans-expr.c
blobc5ae4c53910dcc150d13b1e01fae48383fed3b91
1 /* Expression translation
2 Copyright (C) 2002-2016 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
4 and Steven Bosscher <s.bosscher@student.tudelft.nl>
6 This file is part of GCC.
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.
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.
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/>. */
22 /* trans-expr.c-- generate GENERIC trees for gfc_expr. */
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"
45 /* Convert a scalar to an array descriptor. To be used for assumed-rank
46 arrays. */
48 static tree
49 get_scalar_to_descriptor_type (tree scalar, symbol_attribute attr)
51 enum gfc_array_kind akind;
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;
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));
66 tree
67 gfc_conv_scalar_to_descriptor (gfc_se *se, tree scalar, symbol_attribute attr)
69 tree desc, type;
71 type = get_scalar_to_descriptor_type (scalar, attr);
72 desc = gfc_create_var (type, "desc");
73 DECL_ARTIFICIAL (desc) = 1;
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);
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;
91 /* This is the seed for an eventual trans-class.c
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
106 tree
107 gfc_class_set_static_fields (tree decl, tree vptr, tree data)
109 tree tmp;
110 tree field;
111 vec<constructor_elt, va_gc> *init = NULL;
113 field = TYPE_FIELDS (TREE_TYPE (decl));
114 tmp = gfc_advance_chain (field, CLASS_DATA_FIELD);
115 CONSTRUCTOR_APPEND_ELT (init, tmp, data);
117 tmp = gfc_advance_chain (field, CLASS_VPTR_FIELD);
118 CONSTRUCTOR_APPEND_ELT (init, tmp, vptr);
120 return build_constructor (TREE_TYPE (decl), init);
124 tree
125 gfc_class_data_get (tree decl)
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);
138 tree
139 gfc_class_vptr_get (tree decl)
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);
157 tree
158 gfc_class_len_get (tree decl)
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);
176 /* Get the specified FIELD from the VPTR. */
178 static tree
179 vptr_field_get (tree vptr, int fieldno)
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;
193 /* Get the field from the class' vptr. */
195 static tree
196 class_vtab_field_get (tree decl, int fieldno)
198 tree vptr;
199 vptr = gfc_class_vptr_get (decl);
200 return vptr_field_get (vptr, fieldno);
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) \
209 return class_vtab_field_get (cl, field); \
212 tree \
213 gfc_vptr_## name ##_get (tree vptr) \
215 return vptr_field_get (vptr, field); \
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)
225 /* The size field is returned as an array index type. Therefore treat
226 it and only it specially. */
228 tree
229 gfc_class_vtab_size_get (tree cl)
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;
239 tree
240 gfc_vptr_size_get (tree vptr)
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;
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
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. */
268 gfc_expr *
269 gfc_find_and_cut_at_last_class_ref (gfc_expr *e)
271 gfc_expr *base_expr;
272 gfc_ref *ref, *class_ref, *tail, *array_ref;
274 /* Find the last class reference. */
275 class_ref = NULL;
276 array_ref = NULL;
277 for (ref = e->ref; ref; ref = ref->next)
279 if (ref->type == REF_ARRAY
280 && ref->u.ar.type != AR_ELEMENT)
281 array_ref = ref;
283 if (ref->type == REF_COMPONENT
284 && ref->u.c.component->ts.type == BT_CLASS)
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;
296 if (ref->next == NULL)
297 break;
300 /* Remove and store all subsequent references after the
301 CLASS reference. */
302 if (class_ref)
304 tail = class_ref->next;
305 class_ref->next = NULL;
307 else
309 tail = e->ref;
310 e->ref = NULL;
313 base_expr = gfc_expr_to_initialize (e);
315 /* Restore the original tail expression. */
316 if (class_ref)
318 gfc_free_ref_list (class_ref->next);
319 class_ref->next = tail;
321 else
323 gfc_free_ref_list (e->ref);
324 e->ref = tail;
326 return base_expr;
330 /* Reset the vptr to the declared type, e.g. after deallocation. */
332 void
333 gfc_reset_vptr (stmtblock_t *block, gfc_expr *e)
335 gfc_symbol *vtab;
336 tree vptr;
337 tree vtable;
338 gfc_se se;
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);
349 /* If a vptr is not found, we can do nothing more. */
350 if (vptr == NULL_TREE)
351 return;
353 if (UNLIMITED_POLY (e))
354 gfc_add_modify (block, vptr, build_int_cst (TREE_TYPE (vptr), 0));
355 else
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);
369 /* Reset the len for unlimited polymorphic objects. */
371 void
372 gfc_reset_len (stmtblock_t *block, gfc_expr *expr)
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);
388 /* Obtain the vptr of the last class reference in an expression.
389 Return NULL_TREE if no class reference is found. */
391 tree
392 gfc_get_vptr_from_expr (tree expr)
394 tree tmp;
395 tree type;
397 for (tmp = expr; tmp; tmp = TREE_OPERAND (tmp, 0))
399 type = TREE_TYPE (tmp);
400 while (type)
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;
409 if (TREE_CODE (tmp) == VAR_DECL)
410 break;
412 return NULL_TREE;
416 static void
417 class_array_data_assign (stmtblock_t *block, tree lhs_desc, tree rhs_desc,
418 bool lhs_type)
420 tree tmp, tmp2, type;
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));
427 gfc_add_modify (block, gfc_conv_descriptor_dtype (lhs_desc),
428 gfc_conv_descriptor_dtype (rhs_desc));
430 /* Assign the dimension as range-ref. */
431 tmp = gfc_get_descriptor_dimension (lhs_desc);
432 tmp2 = gfc_get_descriptor_dimension (rhs_desc);
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);
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)
453 gfc_symbol *vtab;
454 tree cond_optional = NULL_TREE;
455 gfc_ss *ss;
456 tree ctree;
457 tree var;
458 tree tmp;
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");
465 /* Set the vptr. */
466 ctree = gfc_class_vptr_get (var);
468 if (vptr != NULL_TREE)
470 /* Use the dynamic vptr. */
471 tmp = vptr;
473 else
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));
481 gfc_add_modify (&parmse->pre, ctree,
482 fold_convert (TREE_TYPE (ctree), tmp));
484 /* Now set the data field. */
485 ctree = gfc_class_data_get (var);
487 if (optional)
488 cond_optional = gfc_conv_expr_present (e->symtree->n.sym);
490 if (parmse->ss && parmse->ss->info->useflags)
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);
503 else
505 ss = gfc_walk_expr (e);
506 if (ss == gfc_ss_terminator)
508 parmse->ss = NULL;
509 gfc_conv_expr_reference (parmse, e);
511 /* Scalar to an assumed-rank array. */
512 if (class_ts.u.derived->components->as)
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);
527 else
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);
538 else
540 stmtblock_t block;
541 gfc_init_block (&block);
543 parmse->ss = ss;
544 gfc_conv_expr_descriptor (parmse, e);
546 if (e->rank != class_ts.u.derived->components->as->rank)
548 gcc_assert (class_ts.u.derived->components->as->type
549 == AS_ASSUMED_RANK);
550 class_array_data_assign (&block, ctree, parmse->expr, false);
552 else
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);
562 if (optional)
564 tmp = gfc_finish_block (&block);
566 gfc_init_block (&block);
567 gfc_conv_descriptor_data_set (&block, ctree, null_pointer_node);
569 tmp = build3_v (COND_EXPR, cond_optional, tmp,
570 gfc_finish_block (&block));
571 gfc_add_expr_to_block (&parmse->pre, tmp);
573 else
574 gfc_add_block_to_block (&parmse->pre, &block);
578 if (class_ts.u.derived->components->ts.type == BT_DERIVED
579 && class_ts.u.derived->components->ts.u.derived
580 ->attr.unlimited_polymorphic)
582 /* Take care about initializing the _len component correctly. */
583 ctree = gfc_class_len_get (var);
584 if (UNLIMITED_POLY (e))
586 gfc_expr *len;
587 gfc_se se;
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;
601 else
602 tmp = integer_zero_node;
603 gfc_add_modify (&parmse->pre, ctree, fold_convert (TREE_TYPE (ctree),
604 tmp));
606 /* Pass the address of the class object. */
607 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
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));
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. */
622 static void
623 class_scalar_coarray_to_class (gfc_se *parmse, gfc_expr *e,
624 gfc_typespec class_ts, bool optional)
626 tree var, ctree, tmp;
627 stmtblock_t block;
628 gfc_ref *ref;
629 gfc_ref *class_ref;
631 gfc_init_block (&block);
633 class_ref = NULL;
634 for (ref = e->ref; ref; ref = ref->next)
636 if (ref->type == REF_COMPONENT
637 && ref->u.c.component->ts.type == BT_CLASS)
638 class_ref = ref;
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
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;
657 var = gfc_typenode_for_spec (&class_ts);
658 var = gfc_create_var (var, "class");
660 ctree = gfc_class_vptr_get (var);
661 gfc_add_modify (&block, ctree,
662 fold_convert (TREE_TYPE (ctree), gfc_class_vptr_get (tmp)));
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));
668 /* Pass the address of the class object. */
669 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
671 if (optional)
673 tree cond = gfc_conv_expr_present (e->symtree->n.sym);
674 tree tmp2;
676 tmp = gfc_finish_block (&block);
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);
684 tmp = build3_loc (input_location, COND_EXPR, void_type_node,
685 cond, tmp, tmp2);
686 gfc_add_expr_to_block (&parmse->pre, tmp);
688 else
689 gfc_add_block_to_block (&parmse->pre, &block);
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)
699 gfc_symbol *vtab;
700 gfc_ss *ss;
701 tree ctree;
702 tree var;
703 tree tmp;
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");
710 /* Set the vptr. */
711 ctree = gfc_class_vptr_get (var);
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));
719 /* Now set the data field. */
720 ctree = gfc_class_data_get (var);
721 if (parmse->ss && parmse->ss->info->useflags)
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);
729 else
731 ss = gfc_walk_expr (e);
732 if (ss == gfc_ss_terminator)
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)
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);
744 else
745 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
746 gfc_add_modify (&parmse->pre, ctree, tmp);
748 else
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)
755 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
756 TREE_TYPE (ctree), parmse->expr);
757 gfc_add_modify (&parmse->pre, ctree, tmp);
759 else
760 gfc_add_modify (&parmse->pre, ctree, parmse->expr);
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)
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)
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
787 /* Try to simplify the expression. */
788 gfc_simplify_expr (e, 0);
789 if (e->expr_type == EXPR_CONSTANT && !e->ts.u.cl->resolved)
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;
801 else
803 gfc_error ("Can't compute the length of the char array at %L.",
804 &e->where);
808 else
809 tmp = integer_zero_node;
811 gfc_add_modify (&parmse->pre, ctree, tmp);
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)
818 ctree = gfc_class_len_get (var);
819 gfc_add_modify (&parmse->pre, ctree,
820 fold_convert (TREE_TYPE (ctree),
821 integer_zero_node));
823 /* Pass the address of the class object. */
824 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
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. */
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)
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;
855 gfc_init_block (&block);
857 class_ref = NULL;
858 for (ref = e->ref; ref; ref = ref->next)
860 if (ref->type == REF_COMPONENT
861 && ref->u.c.component->ts.type == BT_CLASS)
862 class_ref = ref;
864 if (ref->next == NULL)
865 break;
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;
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);
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");
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)
890 if (e->rank == 0)
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));
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);
901 gfc_conv_descriptor_data_set (&block, ctree, tmp);
903 else
904 class_array_data_assign (&block, ctree, parmse->expr, false);
906 else
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);
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)
919 if (class_ts.u.derived->components->as
920 && e->rank != class_ts.u.derived->components->as->rank)
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);
928 else
929 gfc_add_modify (&parmse->post, parmse->expr, ctree);
932 /* Set the vptr. */
933 ctree = gfc_class_vptr_get (var);
935 /* The vptr is the second field of the actual argument.
936 First we have to find the corresponding class reference. */
938 tmp = NULL_TREE;
939 if (class_ref == NULL
940 && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
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;
947 else
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;
961 gcc_assert (tmp != NULL_TREE);
963 /* Dereference if needs be. */
964 if (TREE_CODE (TREE_TYPE (tmp)) == REFERENCE_TYPE)
965 tmp = build_fold_indirect_ref_loc (input_location, tmp);
967 vptr = gfc_class_vptr_get (tmp);
968 gfc_add_modify (&block, ctree,
969 fold_convert (TREE_TYPE (ctree), vptr));
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));
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)
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)
988 gcc_assert (slen != NULL_TREE);
989 tmp = slen;
991 else
992 tmp = integer_zero_node;
993 gfc_add_modify (&parmse->pre, ctree,
994 fold_convert (TREE_TYPE (ctree), tmp));
997 if (optional)
999 tree tmp2;
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);
1011 if (optional_alloc_ptr)
1012 tmp2 = build_empty_stmt (input_location);
1013 else
1015 gfc_init_block (&block);
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);
1023 tmp = build3_loc (input_location, COND_EXPR, void_type_node,
1024 cond, tmp, tmp2);
1025 gfc_add_expr_to_block (&parmse->pre, tmp);
1027 else
1028 gfc_add_block_to_block (&parmse->pre, &block);
1030 /* Pass the address of the class object. */
1031 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
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));
1042 /* Given a class array declaration and an index, returns the address
1043 of the referenced element. */
1045 tree
1046 gfc_get_class_array_ref (tree index, tree class_decl, tree data_comp)
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);
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. */
1067 tree
1068 gfc_copy_class_to_class (tree from, tree to, tree nelems, bool unlimited)
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;
1085 args = NULL;
1086 /* To prevent warnings on uninitialized variables. */
1087 from_len = to_len = NULL_TREE;
1089 if (from != NULL_TREE)
1090 fcn = gfc_class_vtab_copy_get (from);
1091 else
1092 fcn = gfc_class_vtab_copy_get (to);
1094 fcn_type = TREE_TYPE (TREE_TYPE (fcn));
1096 if (from != NULL_TREE)
1098 is_from_desc = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from));
1099 if (is_from_desc)
1101 from_data = from;
1102 from = GFC_DECL_SAVED_DESCRIPTOR (from);
1104 else
1106 from_data = gfc_class_data_get (from);
1107 is_from_desc = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data));
1110 else
1111 from_data = gfc_class_vtab_def_init_get (to);
1113 if (unlimited)
1115 if (from != NULL_TREE && unlimited)
1116 from_len = gfc_class_len_get (from);
1117 else
1118 from_len = integer_zero_node;
1121 if (GFC_CLASS_TYPE_P (TREE_TYPE (to)))
1123 is_to_class = true;
1124 to_data = gfc_class_data_get (to);
1125 if (unlimited)
1126 to_len = gfc_class_len_get (to);
1128 else
1129 /* When to is a BT_DERIVED and not a BT_CLASS, then to_data == to. */
1130 to_data = to;
1132 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (to_data)))
1134 stmtblock_t loopbody;
1135 stmtblock_t body;
1136 stmtblock_t ifbody;
1137 gfc_loopinfo loop;
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");
1146 if (is_from_desc)
1148 from_ref = gfc_get_class_array_ref (index, from, from_data);
1149 vec_safe_push (args, from_ref);
1151 else
1152 vec_safe_push (args, from_data);
1154 if (is_to_class)
1155 to_ref = gfc_get_class_array_ref (index, to, to_data);
1156 else
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));
1163 vec_safe_push (args, to_ref);
1165 tmp = build_call_vec (fcn_type, fcn, args);
1167 /* Build the body of the loop. */
1168 gfc_init_block (&loopbody);
1169 gfc_add_expr_to_block (&loopbody, tmp);
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))
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);
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);
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);
1210 else
1212 gfc_add_expr_to_block (&body, stdcopy);
1213 tmp = gfc_finish_block (&body);
1215 gfc_cleanup_loop (&loop);
1217 else
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);
1224 /* In initialization mode from_len is a constant zero. */
1225 if (unlimited && !integer_zerop (from_len))
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);
1236 else
1237 tmp = stdcopy;
1240 /* Only copy _def_init to to_data, when it is not a NULL-pointer. */
1241 if (from == NULL_TREE)
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));
1252 return tmp;
1256 static tree
1257 gfc_trans_class_array_init_assign (gfc_expr *rhs, gfc_expr *lhs, gfc_expr *obj)
1259 gfc_actual_arglist *actual;
1260 gfc_expr *ppc;
1261 gfc_code *ppc_code;
1262 tree res;
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);
1283 if (UNLIMITED_POLY(obj))
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));
1297 return res;
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. */
1304 tree
1305 gfc_trans_class_init_assign (gfc_code *code)
1307 stmtblock_t block;
1308 tree tmp;
1309 gfc_se dst,src,memsz;
1310 gfc_expr *lhs, *rhs, *sz;
1312 gfc_start_block (&block);
1314 lhs = gfc_copy_expr (code->expr1);
1315 gfc_add_data_component (lhs);
1317 rhs = gfc_copy_expr (code->expr1);
1318 gfc_add_vptr_component (rhs);
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;
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
1333 sz = gfc_copy_expr (code->expr1);
1334 gfc_add_vptr_component (sz);
1335 gfc_add_size_component (sz);
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);
1346 tmp = gfc_build_memcpy_call (dst.expr, src.expr, memsz.expr);
1348 if (UNLIMITED_POLY(code->expr1))
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));
1360 if (code->expr1->symtree->n.sym->attr.optional
1361 || code->expr1->symtree->n.sym->ns->proc_name->attr.entry_master)
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));
1369 gfc_add_expr_to_block (&block, tmp);
1371 return gfc_finish_block (&block);
1375 /* Translate an assignment to a CLASS object
1376 (pointer or ordinary assignment). */
1378 tree
1379 gfc_trans_class_assign (gfc_expr *expr1, gfc_expr *expr2, gfc_exec_op op)
1381 stmtblock_t block;
1382 tree tmp;
1383 gfc_expr *lhs;
1384 gfc_expr *rhs;
1385 gfc_ref *ref;
1387 gfc_start_block (&block);
1389 ref = expr1->ref;
1390 while (ref && ref->next)
1391 ref = ref->next;
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;
1402 if (expr2->ts.type != BT_CLASS)
1404 /* Insert an additional assignment which sets the '_vptr' field. */
1405 gfc_symbol *vtab = NULL;
1406 gfc_symtree *st;
1408 lhs = gfc_copy_expr (expr1);
1409 gfc_add_vptr_component (lhs);
1411 if (UNLIMITED_POLY (expr1)
1412 && expr2->expr_type == EXPR_NULL && expr2->ts.type == BT_UNKNOWN)
1414 rhs = gfc_get_null_expr (&expr2->where);
1415 goto assign_vptr;
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);
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);
1433 gfc_free_expr (lhs);
1434 gfc_free_expr (rhs);
1436 else if (expr1->ts.type == BT_DERIVED && UNLIMITED_POLY (expr2))
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;
1444 else if (CLASS_DATA (expr2)->attr.dimension && expr2->expr_type != EXPR_FUNCTION)
1446 /* Insert an additional assignment which sets the '_vptr' field. */
1447 lhs = gfc_copy_expr (expr1);
1448 gfc_add_vptr_component (lhs);
1450 rhs = gfc_copy_expr (expr2);
1451 gfc_add_vptr_component (rhs);
1453 tmp = gfc_trans_pointer_assignment (lhs, rhs);
1454 gfc_add_expr_to_block (&block, tmp);
1456 gfc_free_expr (lhs);
1457 gfc_free_expr (rhs);
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);
1468 assign:
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();
1477 gfc_add_expr_to_block (&block, tmp);
1479 return gfc_finish_block (&block);
1483 /* End of prototype trans-class.c */
1486 static void
1487 realloc_lhs_warning (bt type, bool array, locus *where)
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);
1500 static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
1501 gfc_expr *);
1503 /* Copy the scalarization loop variables. */
1505 static void
1506 gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
1508 dest->ss = src->ss;
1509 dest->loop = src->loop;
1513 /* Initialize a simple expression holder.
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. */
1519 void
1520 gfc_init_se (gfc_se * se, gfc_se * parent)
1522 memset (se, 0, sizeof (gfc_se));
1523 gfc_init_block (&se->pre);
1524 gfc_init_block (&se->post);
1526 se->parent = parent;
1528 if (parent)
1529 gfc_copy_se_loopvars (se, parent);
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. */
1537 void
1538 gfc_advance_se_ss_chain (gfc_se * se)
1540 gfc_se *p;
1541 gfc_ss *ss;
1543 gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
1545 p = se;
1546 /* Walk down the parent chain. */
1547 while (p != NULL)
1549 /* Simple consistency check. */
1550 gcc_assert (p->parent == NULL || p->parent->ss == p->ss
1551 || p->parent->ss->nested_ss == p->ss);
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;
1561 p->ss = ss->next;
1563 p = p->parent;
1568 /* Ensures the result of the expression as either a temporary variable
1569 or a constant so that it can be used repeatedly. */
1571 void
1572 gfc_make_safe_expr (gfc_se * se)
1574 tree var;
1576 if (CONSTANT_CLASS_P (se->expr))
1577 return;
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;
1586 /* Return an expression which determines if a dummy parameter is present.
1587 Also used for arguments to procedures with multiple entry points. */
1589 tree
1590 gfc_conv_expr_present (gfc_symbol * sym)
1592 tree decl, cond;
1594 gcc_assert (sym->attr.dummy);
1595 decl = gfc_get_symbol_decl (sym);
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)
1603 char name[GFC_MAX_SYMBOL_LEN + 2];
1604 tree tree_name;
1606 gcc_assert (TREE_CODE (decl) == PARM_DECL);
1607 name[0] = '_';
1608 strcpy (&name[1], sym->name);
1609 tree_name = get_identifier (name);
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;
1617 gcc_assert (cond);
1618 return cond;
1621 if (TREE_CODE (decl) != PARM_DECL)
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);
1630 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, decl,
1631 fold_convert (TREE_TYPE (decl), null_pointer_node));
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. */
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))
1648 tree tmp;
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))
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);
1660 else if (sym->ts.type == BT_CLASS)
1661 tmp = gfc_class_data_get (decl);
1662 else
1663 tmp = NULL_TREE;
1665 if (tmp != NULL_TREE)
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);
1674 return cond;
1678 /* Converts a missing, dummy argument into a null or zero. */
1680 void
1681 gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
1683 tree present;
1684 tree tmp;
1686 present = gfc_conv_expr_present (arg->symtree->n.sym);
1688 if (kind > 0)
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));
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);
1701 else
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;
1710 if (ts.type == BT_CHARACTER)
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;
1718 return;
1722 /* Get the character length of an expression, looking through gfc_refs
1723 if necessary. */
1725 tree
1726 gfc_get_expr_charlen (gfc_expr *e)
1728 gfc_ref *r;
1729 tree length;
1731 gcc_assert (e->expr_type == EXPR_VARIABLE
1732 && e->ts.type == BT_CHARACTER);
1734 length = NULL; /* To silence compiler warning. */
1736 if (is_subref_array (e) && e->ts.u.cl->length)
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;
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;
1751 /* Look through the reference chain for component references. */
1752 for (r = e->ref; r; r = r->next)
1754 switch (r->type)
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;
1761 case REF_ARRAY:
1762 /* Do nothing. */
1763 break;
1765 default:
1766 /* We should never got substring references here. These will be
1767 broken down by the scalarizer. */
1768 gcc_unreachable ();
1769 break;
1773 gcc_assert (length != NULL);
1774 return length;
1778 /* Return for an expression the backend decl of the coarray. */
1780 tree
1781 gfc_get_tree_for_caf_expr (gfc_expr *expr)
1783 tree caf_decl;
1784 bool found = false;
1785 gfc_ref *ref, *comp_ref = NULL;
1787 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE);
1789 /* Not-implemented diagnostic. */
1790 for (ref = expr->ref; ref; ref = ref->next)
1791 if (ref->type == REF_COMPONENT)
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);
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);
1819 if (expr->rank)
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);
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;
1851 /* The following code assumes that the coarray is a component reachable via
1852 only scalar components/variables; the Fortran standard guarantees this. */
1854 for (ref = expr->ref; ref; ref = ref->next)
1855 if (ref->type == REF_COMPONENT)
1857 gfc_component *comp = ref->u.c.component;
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)
1868 found = true;
1869 break;
1872 gcc_assert (found && caf_decl);
1873 return caf_decl;
1877 /* Obtain the Coarray token - and optionally also the offset. */
1879 void
1880 gfc_get_caf_token_offset (tree *token, tree *offset, tree caf_decl, tree se_expr,
1881 gfc_expr *expr)
1883 tree tmp;
1885 /* Coarray token. */
1886 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
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);
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
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));
1903 if (offset == NULL)
1904 return;
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);
1919 if (POINTER_TYPE_P (TREE_TYPE (se_expr))
1920 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se_expr))))
1922 tmp = build_fold_indirect_ref_loc (input_location, se_expr);
1923 tmp = gfc_conv_descriptor_data_get (tmp);
1925 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se_expr)))
1926 tmp = gfc_conv_descriptor_data_get (se_expr);
1927 else
1929 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se_expr)));
1930 tmp = se_expr;
1933 *offset = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1934 *offset, fold_convert (gfc_array_index_type, tmp));
1936 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
1937 tmp = gfc_conv_descriptor_data_get (caf_decl);
1938 else
1940 gcc_assert (POINTER_TYPE_P (TREE_TYPE (caf_decl)));
1941 tmp = caf_decl;
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));
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) + ... */
1954 tree
1955 gfc_caf_get_image_index (stmtblock_t *block, gfc_expr *e, tree desc)
1957 gfc_ref *ref;
1958 tree lbound, ubound, extent, tmp, img_idx;
1959 gfc_se se;
1960 int i;
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);
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++)
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)
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);
1992 else
1993 for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
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)
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);
2018 img_idx = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
2019 img_idx, integer_one_node);
2020 return img_idx;
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). */
2028 static void
2029 flatten_array_ctors_without_strlen (gfc_expr* e)
2031 gfc_actual_arglist* arg;
2032 gfc_constructor* c;
2034 if (!e)
2035 return;
2037 switch (e->expr_type)
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;
2045 case EXPR_COMPCALL:
2046 /* TODO: Implement as with EXPR_FUNCTION when needed. */
2047 gcc_unreachable ();
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;
2054 case EXPR_ARRAY:
2056 /* We've found what we're looking for. */
2057 if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
2059 gfc_constructor *c;
2060 gfc_expr* new_expr;
2062 gcc_assert (e->value.constructor);
2064 c = gfc_constructor_first (e->value.constructor);
2065 new_expr = c->expr;
2066 c->expr = NULL;
2068 flatten_array_ctors_without_strlen (new_expr);
2069 gfc_replace_expr (e, new_expr);
2070 break;
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;
2080 default:
2081 break;
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. */
2092 void
2093 gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
2095 gfc_se se;
2097 gfc_init_se (&se, NULL);
2099 if (!cl->length
2100 && cl->backend_decl
2101 && TREE_CODE (cl->backend_decl) == VAR_DECL)
2102 return;
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)
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);
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);
2119 gfc_free_expr (expr_flat);
2120 return;
2123 /* Convert cl->length. */
2125 gcc_assert (cl->length);
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);
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);
2139 static void
2140 gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
2141 const char *name, locus *where)
2143 tree tmp;
2144 tree type;
2145 tree fault;
2146 gfc_se start;
2147 gfc_se end;
2148 char *msg;
2149 mpz_t length;
2151 type = gfc_get_character_type (kind, ref->u.ss.length);
2152 type = build_pointer_type (type);
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);
2158 if (integer_onep (start.expr))
2159 gfc_conv_string_parameter (se);
2160 else
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);
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);
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
2184 gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
2185 gfc_add_block_to_block (&se->pre, &end.pre);
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);
2192 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2194 tree nonempty = fold_build2_loc (input_location, LE_EXPR,
2195 boolean_type_node, start.expr,
2196 end.expr);
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);
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);
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))
2237 int i_len;
2239 i_len = mpz_get_si (length) + 1;
2240 if (i_len < 0)
2241 i_len = 0;
2243 tmp = build_int_cst (gfc_charlen_type_node, i_len);
2244 mpz_clear (length); /* Was initialized by gfc_dep_difference. */
2246 else
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));
2256 se->string_length = tmp;
2260 /* Convert a derived type component reference. */
2262 static void
2263 gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
2265 gfc_component *c;
2266 tree tmp;
2267 tree decl;
2268 tree field;
2270 c = ref->u.c.component;
2272 if (c->backend_decl == NULL_TREE
2273 && ref->u.c.sym != NULL)
2274 gfc_get_derived_type (ref->u.c.sym);
2276 field = c->backend_decl;
2277 gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
2278 decl = se->expr;
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. */
2287 if (DECL_FIELD_CONTEXT (field) != TREE_TYPE (decl))
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;
2300 if (ref->u.c.sym && ref->u.c.sym->ts.type == BT_CLASS
2301 && strcmp ("_data", c->name) == 0)
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);
2307 else
2308 se->class_vptr = NULL_TREE;
2310 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
2311 decl, field, NULL_TREE);
2313 se->expr = tmp;
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))
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;
2326 if (gfc_deferred_strlen (c, &field))
2328 tmp = fold_build3_loc (input_location, COMPONENT_REF,
2329 TREE_TYPE (field),
2330 decl, field, NULL_TREE);
2331 se->string_length = tmp;
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);
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)
2348 gfc_component *c;
2349 gfc_component *cmp;
2350 gfc_symbol *dt;
2351 gfc_ref parent;
2353 dt = ref->u.c.sym;
2354 c = ref->u.c.component;
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;
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;
2367 if (dt->backend_decl == NULL)
2368 gfc_get_derived_type (dt);
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);
2377 /* Return the contents of a variable. Also handles reference/pointer
2378 variables (all Fortran pointer references are implicit). */
2380 static void
2381 gfc_conv_variable (gfc_se * se, gfc_expr * expr)
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;
2394 sym = expr->symtree->n.sym;
2395 is_classarray = IS_CLASS_ARRAY (sym);
2396 ss = se->ss;
2397 if (ss != NULL)
2399 gfc_ss_info *ss_info = ss->info;
2401 /* Check that something hasn't gone horribly wrong. */
2402 gcc_assert (ss != gfc_ss_terminator);
2403 gcc_assert (ss_info->expr == expr);
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);
2415 else
2417 tree se_expr = NULL_TREE;
2419 se->expr = gfc_get_symbol_decl (sym);
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);
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;
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);
2446 /* Similarly for alternate entry points. */
2447 else if (alternate_entry
2448 && (sym->ns->proc_name->backend_decl == current_function_decl
2449 || parent_flag))
2451 gfc_entry_list *el = NULL;
2453 for (el = sym->ns->entries; el; el = el->next)
2454 if (sym == el->sym)
2456 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
2457 break;
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);
2466 if (se_expr)
2467 se->expr = se_expr;
2469 /* Procedure actual arguments. */
2470 else if (sym->attr.flavor == FL_PROCEDURE
2471 && se->expr != current_function_decl)
2473 if (!sym->attr.dummy && !sym->attr.proc_pointer)
2475 gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
2476 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
2478 return;
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)
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);
2497 else if (!sym->attr.value)
2499 /* Dereference temporaries for class array dummy arguments. */
2500 if (sym->attr.dummy && is_classarray
2501 && GFC_ARRAY_TYPE_P (TREE_TYPE (se->expr)))
2503 if (!se->descriptor_only)
2504 se->expr = GFC_DECL_SAVED_DESCRIPTOR (se->expr);
2506 se->expr = build_fold_indirect_ref_loc (input_location,
2507 se->expr);
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);
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);
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);
2570 ref = expr->ref;
2573 /* For character variables, also get the length. */
2574 if (sym->ts.type == BT_CHARACTER)
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);
2585 while (ref)
2587 switch (ref->type)
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;
2600 gfc_conv_array_ref (se, &ref->u.ar, expr, &expr->where);
2601 /* Return a pointer to an element. */
2602 break;
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;
2617 if (ref->u.c.sym->attr.extension)
2618 conv_parent_component_references (se, ref);
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;
2625 break;
2627 case REF_SUBSTRING:
2628 gfc_conv_substring (se, ref, expr->ts.kind,
2629 expr->symtree->name, &expr->where);
2630 break;
2632 default:
2633 gcc_unreachable ();
2634 break;
2636 first_time = false;
2637 ref = ref->next;
2639 /* Pointer assignment, allocation or pass by reference. Arrays are handled
2640 separately. */
2641 if (se->want_pointer)
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);
2651 /* Unary ops are easy... Or they would be if ! was a valid op. */
2653 static void
2654 gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
2656 gfc_se operand;
2657 tree type;
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);
2665 type = gfc_typenode_for_spec (&expr->ts);
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);
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. */
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
2690 /* The table is from builtins.c. */
2691 static const unsigned char powi_table[POWI_TABLE_SIZE] =
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 */
2727 /* If n is larger than lookup table's max index, we use the "window
2728 method". */
2729 #define POWI_WINDOW_SIZE 3
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)
2736 tree op0;
2737 tree op1;
2738 tree tmp;
2739 int digit;
2741 if (n < POWI_TABLE_SIZE)
2743 if (tmpvar[n])
2744 return tmpvar[n];
2746 op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
2747 op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
2749 else if (n & 1)
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);
2755 else
2757 op0 = gfc_conv_powi (se, n >> 1, tmpvar);
2758 op1 = op0;
2761 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
2762 tmp = gfc_evaluate_now (tmp, &se->pre);
2764 if (n < POWI_TABLE_SIZE)
2765 tmpvar[n] = tmp;
2767 return tmp;
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)
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;
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;
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);
2796 type = TREE_TYPE (lhs);
2797 sgn = tree_int_cst_sgn (rhs);
2799 if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
2800 || optimize_size) && (m > 2 || m < -1))
2801 return 0;
2803 /* rhs == 0 */
2804 if (sgn == 0)
2806 se->expr = gfc_build_const (type, integer_one_node);
2807 return 1;
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))
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));
2818 /* If rhs is even,
2819 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
2820 if ((n & 1) == 0)
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;
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;
2839 memset (vartmp, 0, sizeof (vartmp));
2840 vartmp[1] = lhs;
2841 if (sgn == -1)
2843 tmp = gfc_build_const (type, integer_one_node);
2844 vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
2845 vartmp[1]);
2848 se->expr = gfc_conv_powi (se, n, vartmp);
2850 return 1;
2854 /* Power op (**). Constant integer exponent has special handling. */
2856 static void
2857 gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
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;
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);
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);
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;
2881 gfc_int4_type_node = gfc_get_int_type (4);
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;
2890 kind = expr->value.op.op1->ts.kind;
2891 switch (expr->value.op.op2->ts.type)
2893 case BT_INTEGER:
2894 ikind = expr->value.op.op2->ts.kind;
2895 switch (ikind)
2897 case 1:
2898 case 2:
2899 rse.expr = convert (gfc_int4_type_node, rse.expr);
2900 res_ikind_2 = ikind;
2901 /* Fall through. */
2903 case 4:
2904 ikind = 0;
2905 break;
2907 case 8:
2908 ikind = 1;
2909 break;
2911 case 16:
2912 ikind = 2;
2913 break;
2915 default:
2916 gcc_unreachable ();
2918 switch (kind)
2920 case 1:
2921 case 2:
2922 if (expr->value.op.op1->ts.type == BT_INTEGER)
2924 lse.expr = convert (gfc_int4_type_node, lse.expr);
2925 res_ikind_1 = kind;
2927 else
2928 gcc_unreachable ();
2929 /* Fall through. */
2931 case 4:
2932 kind = 0;
2933 break;
2935 case 8:
2936 kind = 1;
2937 break;
2939 case 10:
2940 kind = 2;
2941 break;
2943 case 16:
2944 kind = 3;
2945 break;
2947 default:
2948 gcc_unreachable ();
2951 switch (expr->value.op.op1->ts.type)
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;
2959 case BT_REAL:
2960 /* Use builtins for real ** int4. */
2961 if (ikind == 0)
2963 switch (kind)
2965 case 0:
2966 fndecl = builtin_decl_explicit (BUILT_IN_POWIF);
2967 break;
2969 case 1:
2970 fndecl = builtin_decl_explicit (BUILT_IN_POWI);
2971 break;
2973 case 2:
2974 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
2975 break;
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;
2984 default:
2985 gcc_unreachable ();
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;
2995 case BT_COMPLEX:
2996 fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
2997 break;
2999 default:
3000 gcc_unreachable ();
3002 break;
3004 case BT_REAL:
3005 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
3006 break;
3008 case BT_COMPLEX:
3009 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
3010 break;
3012 default:
3013 gcc_unreachable ();
3014 break;
3017 se->expr = build_call_expr_loc (input_location,
3018 fndecl, 2, lse.expr, rse.expr);
3020 /* Convert the result back if it is of wrong integer kind. */
3021 if (res_ikind_1 != -1 && res_ikind_2 != -1)
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);
3031 /* Generate code to allocate a string temporary. */
3033 tree
3034 gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
3036 tree var;
3037 tree tmp;
3039 if (gfc_can_put_var_on_stack (len))
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);
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);
3052 var = gfc_create_var (tmp, "str");
3053 var = gfc_build_addr_expr (type, var);
3055 else
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);
3070 /* Free the temporary afterwards. */
3071 tmp = gfc_call_free (var);
3072 gfc_add_expr_to_block (&se->post, tmp);
3075 return var;
3079 /* Handle a string concatenation operation. A temporary will be allocated to
3080 hold the result. */
3082 static void
3083 gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
3085 gfc_se lse, rse;
3086 tree len, type, var, tmp, fndecl;
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);
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);
3099 gfc_add_block_to_block (&se->pre, &lse.pre);
3100 gfc_add_block_to_block (&se->pre, &rse.pre);
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)
3106 len = fold_build2_loc (input_location, PLUS_EXPR,
3107 TREE_TYPE (lse.string_length),
3108 lse.string_length, rse.string_length);
3111 type = build_pointer_type (type);
3113 var = gfc_conv_string_tmp (se, type, len);
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 ();
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);
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);
3132 se->expr = var;
3133 se->string_length = len;
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. */
3144 static void
3145 gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
3147 enum tree_code code;
3148 gfc_se lse;
3149 gfc_se rse;
3150 tree tmp, type;
3151 int lop;
3152 int checkstring;
3154 checkstring = 0;
3155 lop = 0;
3156 switch (expr->value.op.op)
3158 case INTRINSIC_PARENTHESES:
3159 if ((expr->ts.type == BT_REAL || expr->ts.type == BT_COMPLEX)
3160 && flag_protect_parens)
3162 gfc_conv_unary_op (PAREN_EXPR, se, expr);
3163 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
3164 return;
3167 /* Fallthrough. */
3168 case INTRINSIC_UPLUS:
3169 gfc_conv_expr (se, expr->value.op.op1);
3170 return;
3172 case INTRINSIC_UMINUS:
3173 gfc_conv_unary_op (NEGATE_EXPR, se, expr);
3174 return;
3176 case INTRINSIC_NOT:
3177 gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
3178 return;
3180 case INTRINSIC_PLUS:
3181 code = PLUS_EXPR;
3182 break;
3184 case INTRINSIC_MINUS:
3185 code = MINUS_EXPR;
3186 break;
3188 case INTRINSIC_TIMES:
3189 code = MULT_EXPR;
3190 break;
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;
3202 case INTRINSIC_POWER:
3203 gfc_conv_power_op (se, expr);
3204 return;
3206 case INTRINSIC_CONCAT:
3207 gfc_conv_concat_op (se, expr);
3208 return;
3210 case INTRINSIC_AND:
3211 code = TRUTH_ANDIF_EXPR;
3212 lop = 1;
3213 break;
3215 case INTRINSIC_OR:
3216 code = TRUTH_ORIF_EXPR;
3217 lop = 1;
3218 break;
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;
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;
3238 case INTRINSIC_GT:
3239 case INTRINSIC_GT_OS:
3240 code = GT_EXPR;
3241 checkstring = 1;
3242 lop = 1;
3243 break;
3245 case INTRINSIC_GE:
3246 case INTRINSIC_GE_OS:
3247 code = GE_EXPR;
3248 checkstring = 1;
3249 lop = 1;
3250 break;
3252 case INTRINSIC_LT:
3253 case INTRINSIC_LT_OS:
3254 code = LT_EXPR;
3255 checkstring = 1;
3256 lop = 1;
3257 break;
3259 case INTRINSIC_LE:
3260 case INTRINSIC_LE_OS:
3261 code = LE_EXPR;
3262 checkstring = 1;
3263 lop = 1;
3264 break;
3266 case INTRINSIC_USER:
3267 case INTRINSIC_ASSIGN:
3268 /* These should be converted into function calls by the frontend. */
3269 gcc_unreachable ();
3271 default:
3272 fatal_error (input_location, "Unknown intrinsic op");
3273 return;
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);
3279 if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
3280 checkstring = 0;
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);
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);
3292 if (checkstring)
3294 gfc_conv_string_parameter (&lse);
3295 gfc_conv_string_parameter (&rse);
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);
3305 type = gfc_typenode_for_spec (&expr->ts);
3307 if (lop)
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);
3314 else
3315 se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
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);
3322 /* If a string's length is one, we convert it to a single character. */
3324 tree
3325 gfc_string_to_single_character (tree len, tree str, int kind)
3328 if (len == NULL
3329 || !tree_fits_uhwi_p (len)
3330 || !POINTER_TYPE_P (TREE_TYPE (str)))
3331 return NULL_TREE;
3333 if (TREE_INT_CST_LOW (len) == 1)
3335 str = fold_convert (gfc_get_pchar_type (kind), str);
3336 return build_fold_indirect_ref_loc (input_location, str);
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)))
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)
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);
3358 for (i = 1; i < length; i++)
3359 if (ptr[i] != ' ')
3360 return NULL_TREE;
3362 return ret;
3366 return NULL_TREE;
3370 void
3371 gfc_conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
3374 if (sym->backend_decl)
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;
3384 DECL_BY_REFERENCE (sym->backend_decl) = 0;
3387 if (expr != NULL)
3389 /* If we have a constant character expression, make it into an
3390 integer. */
3391 if ((*expr)->expr_type == EXPR_CONSTANT)
3393 gfc_typespec ts;
3394 gfc_clear_ts (&ts);
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)
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);
3407 else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
3409 if ((*expr)->ref == NULL)
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);
3418 else
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);
3431 /* Helper function for gfc_build_compare_string. Return LEN_TRIM value
3432 if STR is a string literal, otherwise return -1. */
3434 static int
3435 gfc_optimize_len_trim (tree len, tree str, int kind)
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)))
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)
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);
3457 for (; length > 0; length--)
3458 if (ptr[length - 1] != ' ')
3459 break;
3461 return length;
3464 return -1;
3467 /* Helper to build a call to memcmp. */
3469 static tree
3470 build_memcmp_call (tree s1, tree s2, tree n)
3472 tree tmp;
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);
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);
3484 n = fold_convert (size_type_node, n);
3486 tmp = build_call_expr_loc (input_location,
3487 builtin_decl_explicit (BUILT_IN_MEMCMP),
3488 3, s1, s2, n);
3490 return fold_convert (integer_type_node, tmp);
3493 /* Compare two strings. If they are all single characters, the result is the
3494 subtraction of them. Otherwise, we build a library call. */
3496 tree
3497 gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
3498 enum tree_code code)
3500 tree sc1;
3501 tree sc2;
3502 tree fndecl;
3504 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
3505 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
3507 sc1 = gfc_string_to_single_character (len1, str1, kind);
3508 sc2 = gfc_string_to_single_character (len2, str2, kind);
3510 if (sc1 != NULL_TREE && sc2 != NULL_TREE)
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);
3519 if ((code == EQ_EXPR || code == NE_EXPR)
3520 && optimize
3521 && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
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;
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. */
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))
3543 tree tmp;
3544 tree chartype;
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);
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 ();
3562 return build_call_expr_loc (input_location, fndecl, 4,
3563 len1, str1, len2, str2);
3567 /* Return the backend_decl for a procedure pointer component. */
3569 static tree
3570 get_proc_ptr_comp (gfc_expr *e)
3572 gfc_se comp_se;
3573 gfc_expr *e2;
3574 expr_t old_type;
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);
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)
3594 gfc_ref *ref;
3595 tree var;
3597 if (TREE_CODE (base_object) != VAR_DECL)
3599 var = gfc_create_var (TREE_TYPE (base_object), NULL);
3600 gfc_add_modify (&se->pre, var, base_object);
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);
3615 static void
3616 conv_function_val (gfc_se * se, gfc_symbol * sym, gfc_expr * expr)
3618 tree tmp;
3620 if (gfc_is_proc_ptr_comp (expr))
3621 tmp = get_proc_ptr_comp (expr);
3622 else if (sym->attr.dummy)
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);
3631 else
3633 if (!sym->backend_decl)
3634 sym->backend_decl = gfc_get_extern_function_decl (sym);
3636 TREE_USED (sym->backend_decl) = 1;
3638 tmp = sym->backend_decl;
3640 if (sym->attr.cray_pointee)
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);
3650 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
3652 gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
3653 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
3656 se->expr = tmp;
3660 /* Initialize MAPPING. */
3662 void
3663 gfc_init_interface_mapping (gfc_interface_mapping * mapping)
3665 mapping->syms = NULL;
3666 mapping->charlens = NULL;
3670 /* Free all memory held by MAPPING (but not MAPPING itself). */
3672 void
3673 gfc_free_interface_mapping (gfc_interface_mapping * mapping)
3675 gfc_interface_sym_mapping *sym;
3676 gfc_interface_sym_mapping *nextsym;
3677 gfc_charlen *cl;
3678 gfc_charlen *nextcl;
3680 for (sym = mapping->syms; sym; sym = nextsym)
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);
3689 for (cl = mapping->charlens; cl; cl = nextcl)
3691 nextcl = cl->next;
3692 gfc_free_expr (cl->length);
3693 free (cl);
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. */
3701 static gfc_charlen *
3702 gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
3703 gfc_charlen * cl)
3705 gfc_charlen *new_charlen;
3707 new_charlen = gfc_get_charlen ();
3708 new_charlen->next = mapping->charlens;
3709 new_charlen->length = gfc_copy_expr (cl->length);
3711 mapping->charlens = new_charlen;
3712 return new_charlen;
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. */
3722 static tree
3723 gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
3724 gfc_packed packed, tree data)
3726 tree type;
3727 tree var;
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);
3734 var = gfc_create_var (type, "ifm");
3735 gfc_add_modify (block, var, fold_convert (type, data));
3737 return var;
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. */
3745 static void
3746 gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
3748 int n;
3749 tree dim;
3750 tree offset;
3751 tree tmp;
3753 offset = gfc_index_zero_node;
3754 for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
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)
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);
3765 else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
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;
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);
3783 offset = gfc_evaluate_now (offset, block);
3784 GFC_TYPE_ARRAY_OFFSET (type) = offset;
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. */
3792 void
3793 gfc_add_interface_mapping (gfc_interface_mapping * mapping,
3794 gfc_symbol * sym, gfc_se * se,
3795 gfc_expr *expr)
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;
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;
3818 /* Ensure that the interface is available and that
3819 descriptors are passed for array actual arguments. */
3820 if (sym->attr.flavor == FL_PROCEDURE)
3822 new_sym->formal = expr->symtree->n.sym->formal;
3823 new_sym->attr.always_explicit
3824 = expr->symtree->n.sym->attr.always_explicit;
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);
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;
3841 /* Stabilize the argument's value. */
3842 if (!sym->attr.function && se)
3843 se->expr = gfc_evaluate_now (se->expr, &se->pre);
3845 if (sym->ts.type == BT_CHARACTER)
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;
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)
3856 se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
3857 new_sym->ts.u.cl->backend_decl = se->string_length;
3861 if (!se)
3862 return;
3864 /* Use the passed value as-is if the argument is a function. */
3865 if (sym->attr.flavor == FL_PROCEDURE)
3866 value = se->expr;
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)
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);
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);
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);
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))))
3898 /* Get the actual argument's descriptor. */
3899 desc = build_fold_indirect_ref_loc (input_location,
3900 se->expr);
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);
3907 /* Use DESC to work out the upper bounds, strides and offset. */
3908 gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
3910 else
3911 /* Otherwise we have a packed array. */
3912 value = gfc_get_interface_mapping_array (&se->pre, sym,
3913 PACKED_FULL, se->expr);
3915 new_sym->backend_decl = value;
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. */
3924 void
3925 gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
3926 stmtblock_t * pre, stmtblock_t * post)
3928 gfc_interface_sym_mapping *sym;
3929 gfc_expr *expr;
3930 gfc_se se;
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)
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);
3945 sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
3950 /* Like gfc_apply_interface_mapping_to_expr, but applied to
3951 constructor C. */
3953 static void
3954 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
3955 gfc_constructor_base base)
3957 gfc_constructor *c;
3958 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
3960 gfc_apply_interface_mapping_to_expr (mapping, c->expr);
3961 if (c->iterator)
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);
3971 /* Like gfc_apply_interface_mapping_to_expr, but applied to
3972 reference REF. */
3974 static void
3975 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
3976 gfc_ref * ref)
3978 int n;
3980 for (; ref; ref = ref->next)
3981 switch (ref->type)
3983 case REF_ARRAY:
3984 for (n = 0; n < ref->u.ar.dimen; n++)
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]);
3990 break;
3992 case REF_COMPONENT:
3993 break;
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;
4003 /* Convert intrinsic function calls into result expressions. */
4005 static bool
4006 gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
4008 gfc_symbol *sym;
4009 gfc_expr *new_expr;
4010 gfc_expr *arg1;
4011 gfc_expr *arg2;
4012 int d, dup;
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;
4020 sym = arg1->symtree->n.sym;
4022 if (sym->attr.dummy)
4023 return false;
4025 new_expr = NULL;
4027 switch (expr->value.function.isym->id)
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;
4037 new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
4038 break;
4040 case GFC_ISYM_SIZE:
4041 if (!sym->as || sym->as->rank == 0)
4042 return false;
4044 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
4046 dup = mpz_get_si (arg2->value.integer);
4047 d = dup - 1;
4049 else
4051 dup = sym->as->rank;
4052 d = 0;
4055 for (; d < dup; d++)
4057 gfc_expr *tmp;
4059 if (!sym->as->upper[d] || !sym->as->lower[d])
4061 gfc_free_expr (new_expr);
4062 return false;
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;
4074 break;
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. */
4081 if (!sym->as || sym->as->rank == 0)
4082 return false;
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 ();
4091 if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
4093 if (sym->as->lower[d])
4094 new_expr = gfc_copy_expr (sym->as->lower[d]);
4096 else
4098 if (sym->as->upper[d])
4099 new_expr = gfc_copy_expr (sym->as->upper[d]);
4101 break;
4103 default:
4104 break;
4107 gfc_apply_interface_mapping_to_expr (mapping, new_expr);
4108 if (!new_expr)
4109 return false;
4111 gfc_replace_expr (expr, new_expr);
4112 return true;
4116 static void
4117 gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
4118 gfc_interface_mapping * mapping)
4120 gfc_formal_arglist *f;
4121 gfc_actual_arglist *actual;
4123 actual = expr->value.function.actual;
4124 f = gfc_sym_get_dummy_args (map_expr->symtree->n.sym);
4126 for (; f && actual; f = f->next, actual = actual->next)
4128 if (!actual->expr)
4129 continue;
4131 gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
4134 if (map_expr->symtree->n.sym->attr.dimension)
4136 int d;
4137 gfc_array_spec *as;
4139 as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
4141 for (d = 0; d < as->rank; d++)
4143 gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
4144 gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
4147 expr->value.function.esym->as = as;
4150 if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
4152 expr->value.function.esym->ts.u.cl->length
4153 = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
4155 gfc_apply_interface_mapping_to_expr (mapping,
4156 expr->value.function.esym->ts.u.cl->length);
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. */
4166 static void
4167 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
4168 gfc_expr * expr)
4170 gfc_interface_sym_mapping *sym;
4171 gfc_actual_arglist *actual;
4173 if (!expr)
4174 return;
4176 /* Copying an expression does not copy its length, so do that here. */
4177 if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
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);
4183 /* Apply the mapping to any references. */
4184 gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
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)
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));
4198 /* ...and to subexpressions in expr->value. */
4199 switch (expr->expr_type)
4201 case EXPR_VARIABLE:
4202 case EXPR_CONSTANT:
4203 case EXPR_NULL:
4204 case EXPR_SUBSTRING:
4205 break;
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;
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);
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;
4222 for (sym = mapping->syms; sym; sym = sym->next)
4223 if (sym->old == expr->value.function.esym)
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;
4229 break;
4231 case EXPR_ARRAY:
4232 case EXPR_STRUCTURE:
4233 gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
4234 break;
4236 case EXPR_COMPCALL:
4237 case EXPR_PPC:
4238 gcc_unreachable ();
4239 break;
4242 return;
4246 /* Evaluate interface expression EXPR using MAPPING. Store the result
4247 in SE. */
4249 void
4250 gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
4251 gfc_se * se, gfc_expr * expr)
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);
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)
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;
4284 gfc_init_se (&lse, NULL);
4285 gfc_init_se (&rse, NULL);
4287 /* Walk the argument expression. */
4288 rss = gfc_walk_expr (expr);
4290 gcc_assert (rss != gfc_ss_terminator);
4292 /* Initialize the scalarizer. */
4293 gfc_init_loopinfo (&loop);
4294 gfc_add_ss_to_loop (&loop, rss);
4296 /* Calculate the bounds of the scalarization. */
4297 gfc_conv_ss_startstride (&loop);
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);
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);
4308 if (expr->ts.type == BT_CLASS)
4309 base_type = gfc_typenode_for_spec (&CLASS_DATA (expr)->ts);
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);
4316 parmse->string_length = loop.temp_ss->info->string_length;
4318 /* Associate the SS with the loop. */
4319 gfc_add_ss_to_loop (&loop, loop.temp_ss);
4321 /* Setup the scalarizing loops. */
4322 gfc_conv_loop_setup (&loop, &expr->where);
4324 /* Pass the temporary descriptor back to the caller. */
4325 info = &loop.temp_ss->info->data.array;
4326 parmse->expr = info->descriptor;
4328 /* Setup the gfc_se structures. */
4329 gfc_copy_loopinfo_to_se (&lse, &loop);
4330 gfc_copy_loopinfo_to_se (&rse, &loop);
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);
4337 /* Start the scalarized loop body. */
4338 gfc_start_scalarized_body (&loop, &body);
4340 /* Translate the expression. */
4341 gfc_conv_expr (&rse, expr);
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))
4348 tmp = loop.ss->loop_chain->info->data.array.descriptor;
4349 gfc_conv_descriptor_offset_set (&loop.pre, tmp,
4350 gfc_index_zero_node);
4353 gfc_conv_tmp_array_ref (&lse);
4355 if (intent != INTENT_OUT)
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);
4362 else
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++)
4368 gfc_merge_block_scope (&body);
4369 body = loop.code[loop.order[n]];
4371 gfc_merge_block_scope (&body);
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);
4378 /**********Copy the temporary back again.*********/
4380 gfc_init_se (&lse, NULL);
4381 gfc_init_se (&rse, NULL);
4383 /* Walk the argument expression. */
4384 lss = gfc_walk_expr (expr);
4385 rse.ss = loop.temp_ss;
4386 lse.ss = lss;
4388 /* Initialize the scalarizer. */
4389 gfc_init_loopinfo (&loop2);
4390 gfc_add_ss_to_loop (&loop2, lss);
4392 dimen = rse.ss->dimen;
4394 /* Skip the write-out loop for this case. */
4395 if (gfc_is_alloc_class_array_function (expr))
4396 goto class_array_fcn;
4398 /* Calculate the bounds of the scalarization. */
4399 gfc_conv_ss_startstride (&loop2);
4401 /* Setup the scalarizing loops. */
4402 gfc_conv_loop_setup (&loop2, &expr->where);
4404 gfc_copy_loopinfo_to_se (&lse, &loop2);
4405 gfc_copy_loopinfo_to_se (&rse, &loop2);
4407 gfc_mark_ss_chain_used (lss, 1);
4408 gfc_mark_ss_chain_used (loop.temp_ss, 1);
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);
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;
4422 tmp_index = gfc_index_zero_node;
4423 for (n = dimen - 1; n > 0; n--)
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);
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);
4439 tmp_index = fold_build2_loc (input_location, MULT_EXPR,
4440 gfc_array_index_type, tmp, tmp_str);
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);
4448 tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
4449 gfc_array_index_type,
4450 rse.loop->loopvar[0], offset);
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);
4457 if (expr->ts.type == BT_CHARACTER)
4458 rse.string_length = expr->ts.u.cl->backend_decl;
4460 gfc_conv_expr (&lse, expr);
4462 gcc_assert (lse.ss == gfc_ss_terminator);
4464 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, true);
4465 gfc_add_expr_to_block (&body, tmp);
4467 /* Generate the copying loops. */
4468 gfc_trans_scalarizing_loops (&loop2, &body);
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)
4475 gfc_add_block_to_block (&parmse->post, &loop2.pre);
4476 gfc_add_block_to_block (&parmse->post, &loop2.post);
4479 class_array_fcn:
4481 gfc_add_block_to_block (&parmse->post, &loop.post);
4483 gfc_cleanup_loop (&loop);
4484 gfc_cleanup_loop (&loop2);
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;
4490 /* Determine the offset for pointer formal arguments and set the
4491 lbounds to one. */
4492 if (formal_ptr)
4494 size = gfc_index_one_node;
4495 offset = gfc_index_zero_node;
4496 for (n = 0; n < dimen; n++)
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);
4526 gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
4527 offset);
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);
4537 return;
4541 /* Generate the code for argument list functions. */
4543 static void
4544 conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
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)
4554 gfc_conv_expr_reference (se, expr);
4555 se->expr = gfc_build_addr_expr (NULL, se->expr);
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);
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.
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. */
4588 static bool
4589 expr_may_alias_variables (gfc_expr *e, bool array_may_alias)
4591 gfc_constructor *c;
4593 if (e->expr_type == EXPR_VARIABLE)
4594 return true;
4595 else if (e->expr_type == EXPR_FUNCTION)
4597 gfc_symbol *proc_ifc = gfc_get_proc_ifc_for_expr (e);
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;
4607 else if (e->expr_type != EXPR_ARRAY || !array_may_alias)
4608 return false;
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;
4616 return false;
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. */
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)
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;
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);
4670 comp = gfc_get_proc_ptr_comp (expr);
4672 bool elemental_proc = (comp
4673 && comp->ts.interface
4674 && comp->ts.interface->attr.elemental)
4675 || (comp && comp->attr.elemental)
4676 || sym->attr.elemental;
4678 if (se->ss != NULL)
4680 if (!elemental_proc)
4682 gcc_assert (se->ss->info->type == GFC_SS_FUNCTION);
4683 if (se->ss->info->useflags)
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;
4695 info = &se->ss->info->data.array;
4697 else
4698 info = NULL;
4700 gfc_init_block (&post);
4701 gfc_init_interface_mapping (&mapping);
4702 if (!comp)
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);
4711 else
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);
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;
4729 /* Evaluate the arguments. */
4730 for (arg = args, argc = 0; arg != NULL;
4731 arg = arg->next, formal = formal ? formal->next : NULL, ++argc)
4733 e = arg->expr;
4734 fsym = formal ? formal->sym : NULL;
4735 parm_kind = MISSING;
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;
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))
4763 gfc_typespec temp_ts = e->ts;
4764 gfc_add_class_array_ref (e);
4765 e->ts = temp_ts;
4768 if (e == NULL)
4770 if (se->ignore_optional)
4772 /* Some intrinsics have already been resolved to the correct
4773 parameters. */
4774 continue;
4776 else if (arg->label)
4778 has_alternate_specifier = 1;
4779 continue;
4781 else
4783 gfc_init_se (&parmse, NULL);
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)
4792 parmse.expr = fold_convert (gfc_sym_type (fsym),
4793 integer_zero_node);
4794 vec_safe_push (optionalargs, boolean_false_node);
4796 else
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,
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))
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);
4820 else if (fsym && fsym->ts.type == BT_CLASS
4821 && e->ts.type == BT_DERIVED)
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);
4833 else if (UNLIMITED_POLY (fsym) && e->ts.type != BT_CLASS)
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);
4840 else if (se->ss && se->ss->info->useflags)
4842 gfc_ss *ss;
4844 ss = se->ss;
4846 /* An elemental function inside a scalarized loop. */
4847 gfc_init_se (&parmse, se);
4848 parm_kind = ELEMENTAL;
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);
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);
4867 if (fsym && fsym->ts.type == BT_DERIVED
4868 && gfc_is_class_container_ref (e))
4870 parmse.expr = gfc_class_data_get (parmse.expr);
4872 if (fsym->attr.optional && e->expr_type == EXPR_VARIABLE
4873 && e->symtree->n.sym->attr.optional)
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));
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)
4891 tree descriptor_data;
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);
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);
4920 else
4922 bool scalar;
4923 gfc_ss *argss;
4925 gfc_init_se (&parmse, NULL);
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);
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)
4941 gfc_add_class_array_ref (e);
4942 parmse.want_coarray = 1;
4943 scalar = false;
4946 /* A scalar or transformational function. */
4947 if (scalar)
4949 if (e->expr_type == EXPR_VARIABLE
4950 && e->symtree->n.sym->attr.cray_pointee
4951 && fsym && fsym->attr.flavor == FL_PROCEDURE)
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);
4960 else if (fsym && fsym->attr.value)
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)
4967 parmse.expr = NULL;
4968 gfc_conv_scalar_char_value (fsym, &parmse, &e);
4969 if (parmse.expr == NULL)
4970 gfc_conv_expr (&parmse, e);
4972 else
4974 gfc_conv_expr (&parmse, e);
4975 if (fsym->attr.optional
4976 && fsym->ts.type != BT_CLASS
4977 && fsym->ts.type != BT_DERIVED)
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
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));
4994 vec_safe_push (optionalargs, tmp);
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)
5010 gfc_conv_expr (&parmse, e);
5011 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
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)
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);
5023 else
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)
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);
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))
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)
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));
5078 else
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);
5087 else
5088 gfc_conv_expr_reference (&parmse, e);
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);
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);
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)))
5121 stmtblock_t block;
5122 tree ptr;
5124 gfc_init_block (&block);
5125 ptr = parmse.expr;
5126 if (e->ts.type == BT_CLASS)
5127 ptr = gfc_class_data_get (ptr);
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);
5137 if (fsym->ts.type == BT_CLASS && UNLIMITED_POLY (fsym))
5139 gfc_add_modify (&block, ptr,
5140 fold_convert (TREE_TYPE (ptr),
5141 null_pointer_node));
5142 gfc_add_expr_to_block (&block, tmp);
5144 else if (fsym->ts.type == BT_CLASS)
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);
5156 if (fsym->attr.optional
5157 && e->expr_type == EXPR_VARIABLE
5158 && e->symtree->n.sym->attr.optional)
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));
5166 else
5167 tmp = gfc_finish_block (&block);
5169 gfc_add_expr_to_block (&se->pre, tmp);
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);
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. */
5183 if (fsym && fsym->as && fsym->as->type == AS_ASSUMED_RANK
5184 && fsym->ts.type != BT_CLASS && e->expr_type != EXPR_NULL)
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);
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)))
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);
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))
5220 /* Pass a class array. */
5221 parmse.use_offset = 1;
5222 gfc_conv_expr_descriptor (&parmse, e);
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)
5229 stmtblock_t block;
5230 tree ptr;
5232 gfc_init_block (&block);
5233 ptr = parmse.expr;
5234 ptr = gfc_class_data_get (ptr);
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);
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)
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));
5260 else
5261 tmp = gfc_finish_block (&block);
5263 gfc_add_expr_to_block (&se->pre, tmp);
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);
5278 else
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)
5286 gfc_expr *iarg;
5287 sym_intent intent;
5289 if (fsym != NULL)
5290 intent = fsym->attr.intent;
5291 else
5292 intent = INTENT_UNKNOWN;
5294 if (gfc_check_fncall_dependency (e, intent, sym, args,
5295 NOT_ELEMENTAL))
5296 parmse.force_tmp = 1;
5298 iarg = e->value.function.actual->expr;
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;
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;
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);
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. */
5346 e->must_finalize = 1;
5347 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
5348 INTENT_IN,
5349 fsym && fsym->attr.pointer);
5351 else
5352 gfc_conv_array_parameter (&parmse, e, nodesc_arg, fsym,
5353 sym->name, NULL);
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)
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);
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))
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);
5407 if (fsym && e)
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)
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;
5423 if (fsym && need_interface_mapping && e)
5424 gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
5426 gfc_add_block_to_block (&se->pre, &parmse.pre);
5427 gfc_add_block_to_block (&post, &parmse.post);
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))
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);
5448 if (fsym && fsym->attr.value)
5449 tmp = parmse.expr;
5450 else
5451 tmp = build_fold_indirect_ref_loc (input_location,
5452 parmse.expr);
5454 parm_rank = e->rank;
5455 switch (parm_kind)
5457 case (ELEMENTAL):
5458 case (SCALAR):
5459 parm_rank = 0;
5460 break;
5462 case (SCALAR_POINTER):
5463 tmp = build_fold_indirect_ref_loc (input_location,
5464 tmp);
5465 break;
5468 if (e->expr_type == EXPR_OP
5469 && e->value.op.op == INTRINSIC_PARENTHESES
5470 && e->value.op.op1->expr_type == EXPR_VARIABLE)
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);
5478 if (e->ts.type == BT_DERIVED && fsym && fsym->ts.type == BT_CLASS)
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);
5488 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp, parm_rank);
5490 gfc_add_expr_to_block (&se->post, tmp);
5493 /* Add argument checking of passing an unallocated/NULL actual to
5494 a nonallocatable/nonpointer dummy. */
5496 if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
5498 symbol_attribute attr;
5499 char *msg;
5500 tree cond;
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;
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;
5513 if (attr.optional)
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;
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;
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);
5554 else
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;
5571 tmp = parmse.expr;
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);
5578 cond = fold_build2_loc (input_location, EQ_EXPR,
5579 boolean_type_node, tmp,
5580 fold_convert (TREE_TYPE (tmp),
5581 null_pointer_node));
5584 gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
5585 msg);
5586 free (msg);
5588 end_pointer_check:
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)
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
5601 tmp = parmse.string_length;
5602 if (TREE_CODE (tmp) != VAR_DECL
5603 && TREE_CODE (tmp) != COMPONENT_REF)
5604 tmp = gfc_evaluate_now (parmse.string_length, &se->pre);
5605 parmse.string_length = gfc_build_addr_expr (NULL_TREE, tmp);
5609 /* Character strings are passed as two parameters, a length and a
5610 pointer - except for Bind(c) which only passes the pointer.
5611 An unlimited polymorphic formal argument likewise does not
5612 need the length. */
5613 if (parmse.string_length != NULL_TREE
5614 && !sym->attr.is_bind_c
5615 && !(fsym && UNLIMITED_POLY (fsym)))
5616 vec_safe_push (stringargs, parmse.string_length);
5618 /* When calling __copy for character expressions to unlimited
5619 polymorphic entities, the dst argument needs a string length. */
5620 if (sym->name[0] == '_' && e && e->ts.type == BT_CHARACTER
5621 && strncmp (sym->name, "__vtab_CHARACTER", 16) == 0
5622 && arg->next && arg->next->expr
5623 && arg->next->expr->ts.type == BT_DERIVED
5624 && arg->next->expr->ts.u.derived->attr.unlimited_polymorphic)
5625 vec_safe_push (stringargs, parmse.string_length);
5627 /* For descriptorless coarrays and assumed-shape coarray dummies, we
5628 pass the token and the offset as additional arguments. */
5629 if (fsym && e == NULL && flag_coarray == GFC_FCOARRAY_LIB
5630 && ((fsym->ts.type != BT_CLASS && fsym->attr.codimension
5631 && !fsym->attr.allocatable)
5632 || (fsym->ts.type == BT_CLASS
5633 && CLASS_DATA (fsym)->attr.codimension
5634 && !CLASS_DATA (fsym)->attr.allocatable)))
5636 /* Token and offset. */
5637 vec_safe_push (stringargs, null_pointer_node);
5638 vec_safe_push (stringargs, build_int_cst (gfc_array_index_type, 0));
5639 gcc_assert (fsym->attr.optional);
5641 else if (fsym && flag_coarray == GFC_FCOARRAY_LIB
5642 && ((fsym->ts.type != BT_CLASS && fsym->attr.codimension
5643 && !fsym->attr.allocatable)
5644 || (fsym->ts.type == BT_CLASS
5645 && CLASS_DATA (fsym)->attr.codimension
5646 && !CLASS_DATA (fsym)->attr.allocatable)))
5648 tree caf_decl, caf_type;
5649 tree offset, tmp2;
5651 caf_decl = gfc_get_tree_for_caf_expr (e);
5652 caf_type = TREE_TYPE (caf_decl);
5654 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
5655 && (GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE
5656 || GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_POINTER))
5657 tmp = gfc_conv_descriptor_token (caf_decl);
5658 else if (DECL_LANG_SPECIFIC (caf_decl)
5659 && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
5660 tmp = GFC_DECL_TOKEN (caf_decl);
5661 else
5663 gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
5664 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
5665 tmp = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
5668 vec_safe_push (stringargs, tmp);
5670 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
5671 && GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE)
5672 offset = build_int_cst (gfc_array_index_type, 0);
5673 else if (DECL_LANG_SPECIFIC (caf_decl)
5674 && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
5675 offset = GFC_DECL_CAF_OFFSET (caf_decl);
5676 else if (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) != NULL_TREE)
5677 offset = GFC_TYPE_ARRAY_CAF_OFFSET (caf_type);
5678 else
5679 offset = build_int_cst (gfc_array_index_type, 0);
5681 if (GFC_DESCRIPTOR_TYPE_P (caf_type))
5682 tmp = gfc_conv_descriptor_data_get (caf_decl);
5683 else
5685 gcc_assert (POINTER_TYPE_P (caf_type));
5686 tmp = caf_decl;
5689 tmp2 = fsym->ts.type == BT_CLASS
5690 ? gfc_class_data_get (parmse.expr) : parmse.expr;
5691 if ((fsym->ts.type != BT_CLASS
5692 && (fsym->as->type == AS_ASSUMED_SHAPE
5693 || fsym->as->type == AS_ASSUMED_RANK))
5694 || (fsym->ts.type == BT_CLASS
5695 && (CLASS_DATA (fsym)->as->type == AS_ASSUMED_SHAPE
5696 || CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)))
5698 if (fsym->ts.type == BT_CLASS)
5699 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (tmp2)));
5700 else
5702 gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
5703 tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
5705 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)));
5706 tmp2 = gfc_conv_descriptor_data_get (tmp2);
5708 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
5709 tmp2 = gfc_conv_descriptor_data_get (tmp2);
5710 else
5712 gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
5715 tmp = fold_build2_loc (input_location, MINUS_EXPR,
5716 gfc_array_index_type,
5717 fold_convert (gfc_array_index_type, tmp2),
5718 fold_convert (gfc_array_index_type, tmp));
5719 offset = fold_build2_loc (input_location, PLUS_EXPR,
5720 gfc_array_index_type, offset, tmp);
5722 vec_safe_push (stringargs, offset);
5725 vec_safe_push (arglist, parmse.expr);
5727 gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
5729 if (comp)
5730 ts = comp->ts;
5731 else
5732 ts = sym->ts;
5734 if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
5735 se->string_length = build_int_cst (gfc_charlen_type_node, 1);
5736 else if (ts.type == BT_CHARACTER)
5738 if (ts.u.cl->length == NULL)
5740 /* Assumed character length results are not allowed by 5.1.1.5 of the
5741 standard and are trapped in resolve.c; except in the case of SPREAD
5742 (and other intrinsics?) and dummy functions. In the case of SPREAD,
5743 we take the character length of the first argument for the result.
5744 For dummies, we have to look through the formal argument list for
5745 this function and use the character length found there.*/
5746 if (ts.deferred)
5747 cl.backend_decl = gfc_create_var (gfc_charlen_type_node, "slen");
5748 else if (!sym->attr.dummy)
5749 cl.backend_decl = (*stringargs)[0];
5750 else
5752 formal = gfc_sym_get_dummy_args (sym->ns->proc_name);
5753 for (; formal; formal = formal->next)
5754 if (strcmp (formal->sym->name, sym->name) == 0)
5755 cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
5757 len = cl.backend_decl;
5759 else
5761 tree tmp;
5763 /* Calculate the length of the returned string. */
5764 gfc_init_se (&parmse, NULL);
5765 if (need_interface_mapping)
5766 gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
5767 else
5768 gfc_conv_expr (&parmse, ts.u.cl->length);
5769 gfc_add_block_to_block (&se->pre, &parmse.pre);
5770 gfc_add_block_to_block (&se->post, &parmse.post);
5772 tmp = fold_convert (gfc_charlen_type_node, parmse.expr);
5773 tmp = fold_build2_loc (input_location, MAX_EXPR,
5774 gfc_charlen_type_node, tmp,
5775 build_int_cst (gfc_charlen_type_node, 0));
5776 cl.backend_decl = tmp;
5779 /* Set up a charlen structure for it. */
5780 cl.next = NULL;
5781 cl.length = NULL;
5782 ts.u.cl = &cl;
5784 len = cl.backend_decl;
5787 byref = (comp && (comp->attr.dimension
5788 || (comp->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)))
5789 || (!comp && gfc_return_by_reference (sym));
5790 if (byref)
5792 if (se->direct_byref)
5794 /* Sometimes, too much indirection can be applied; e.g. for
5795 function_result = array_valued_recursive_function. */
5796 if (TREE_TYPE (TREE_TYPE (se->expr))
5797 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
5798 && GFC_DESCRIPTOR_TYPE_P
5799 (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
5800 se->expr = build_fold_indirect_ref_loc (input_location,
5801 se->expr);
5803 /* If the lhs of an assignment x = f(..) is allocatable and
5804 f2003 is allowed, we must do the automatic reallocation.
5805 TODO - deal with intrinsics, without using a temporary. */
5806 if (flag_realloc_lhs
5807 && se->ss && se->ss->loop_chain
5808 && se->ss->loop_chain->is_alloc_lhs
5809 && !expr->value.function.isym
5810 && sym->result->as != NULL)
5812 /* Evaluate the bounds of the result, if known. */
5813 gfc_set_loop_bounds_from_array_spec (&mapping, se,
5814 sym->result->as);
5816 /* Perform the automatic reallocation. */
5817 tmp = gfc_alloc_allocatable_for_assignment (se->loop,
5818 expr, NULL);
5819 gfc_add_expr_to_block (&se->pre, tmp);
5821 /* Pass the temporary as the first argument. */
5822 result = info->descriptor;
5824 else
5825 result = build_fold_indirect_ref_loc (input_location,
5826 se->expr);
5827 vec_safe_push (retargs, se->expr);
5829 else if (comp && comp->attr.dimension)
5831 gcc_assert (se->loop && info);
5833 /* Set the type of the array. */
5834 tmp = gfc_typenode_for_spec (&comp->ts);
5835 gcc_assert (se->ss->dimen == se->loop->dimen);
5837 /* Evaluate the bounds of the result, if known. */
5838 gfc_set_loop_bounds_from_array_spec (&mapping, se, comp->as);
5840 /* If the lhs of an assignment x = f(..) is allocatable and
5841 f2003 is allowed, we must not generate the function call
5842 here but should just send back the results of the mapping.
5843 This is signalled by the function ss being flagged. */
5844 if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
5846 gfc_free_interface_mapping (&mapping);
5847 return has_alternate_specifier;
5850 /* Create a temporary to store the result. In case the function
5851 returns a pointer, the temporary will be a shallow copy and
5852 mustn't be deallocated. */
5853 callee_alloc = comp->attr.allocatable || comp->attr.pointer;
5854 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
5855 tmp, NULL_TREE, false,
5856 !comp->attr.pointer, callee_alloc,
5857 &se->ss->info->expr->where);
5859 /* Pass the temporary as the first argument. */
5860 result = info->descriptor;
5861 tmp = gfc_build_addr_expr (NULL_TREE, result);
5862 vec_safe_push (retargs, tmp);
5864 else if (!comp && sym->result->attr.dimension)
5866 gcc_assert (se->loop && info);
5868 /* Set the type of the array. */
5869 tmp = gfc_typenode_for_spec (&ts);
5870 gcc_assert (se->ss->dimen == se->loop->dimen);
5872 /* Evaluate the bounds of the result, if known. */
5873 gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
5875 /* If the lhs of an assignment x = f(..) is allocatable and
5876 f2003 is allowed, we must not generate the function call
5877 here but should just send back the results of the mapping.
5878 This is signalled by the function ss being flagged. */
5879 if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
5881 gfc_free_interface_mapping (&mapping);
5882 return has_alternate_specifier;
5885 /* Create a temporary to store the result. In case the function
5886 returns a pointer, the temporary will be a shallow copy and
5887 mustn't be deallocated. */
5888 callee_alloc = sym->attr.allocatable || sym->attr.pointer;
5889 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
5890 tmp, NULL_TREE, false,
5891 !sym->attr.pointer, callee_alloc,
5892 &se->ss->info->expr->where);
5894 /* Pass the temporary as the first argument. */
5895 result = info->descriptor;
5896 tmp = gfc_build_addr_expr (NULL_TREE, result);
5897 vec_safe_push (retargs, tmp);
5899 else if (ts.type == BT_CHARACTER)
5901 /* Pass the string length. */
5902 type = gfc_get_character_type (ts.kind, ts.u.cl);
5903 type = build_pointer_type (type);
5905 /* Return an address to a char[0:len-1]* temporary for
5906 character pointers. */
5907 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
5908 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
5910 var = gfc_create_var (type, "pstr");
5912 if ((!comp && sym->attr.allocatable)
5913 || (comp && comp->attr.allocatable))
5915 gfc_add_modify (&se->pre, var,
5916 fold_convert (TREE_TYPE (var),
5917 null_pointer_node));
5918 tmp = gfc_call_free (var);
5919 gfc_add_expr_to_block (&se->post, tmp);
5922 /* Provide an address expression for the function arguments. */
5923 var = gfc_build_addr_expr (NULL_TREE, var);
5925 else
5926 var = gfc_conv_string_tmp (se, type, len);
5928 vec_safe_push (retargs, var);
5930 else
5932 gcc_assert (flag_f2c && ts.type == BT_COMPLEX);
5934 type = gfc_get_complex_type (ts.kind);
5935 var = gfc_build_addr_expr (NULL_TREE, gfc_create_var (type, "cmplx"));
5936 vec_safe_push (retargs, var);
5939 /* Add the string length to the argument list. */
5940 if (ts.type == BT_CHARACTER && ts.deferred)
5942 tmp = len;
5943 if (TREE_CODE (tmp) != VAR_DECL)
5944 tmp = gfc_evaluate_now (len, &se->pre);
5945 TREE_STATIC (tmp) = 1;
5946 gfc_add_modify (&se->pre, tmp,
5947 build_int_cst (TREE_TYPE (tmp), 0));
5948 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
5949 vec_safe_push (retargs, tmp);
5951 else if (ts.type == BT_CHARACTER)
5952 vec_safe_push (retargs, len);
5954 gfc_free_interface_mapping (&mapping);
5956 /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS. */
5957 arglen = (vec_safe_length (arglist) + vec_safe_length (optionalargs)
5958 + vec_safe_length (stringargs) + vec_safe_length (append_args));
5959 vec_safe_reserve (retargs, arglen);
5961 /* Add the return arguments. */
5962 vec_safe_splice (retargs, arglist);
5964 /* Add the hidden present status for optional+value to the arguments. */
5965 vec_safe_splice (retargs, optionalargs);
5967 /* Add the hidden string length parameters to the arguments. */
5968 vec_safe_splice (retargs, stringargs);
5970 /* We may want to append extra arguments here. This is used e.g. for
5971 calls to libgfortran_matmul_??, which need extra information. */
5972 vec_safe_splice (retargs, append_args);
5974 arglist = retargs;
5976 /* Generate the actual call. */
5977 if (base_object == NULL_TREE)
5978 conv_function_val (se, sym, expr);
5979 else
5980 conv_base_obj_fcn_val (se, base_object, expr);
5982 /* If there are alternate return labels, function type should be
5983 integer. Can't modify the type in place though, since it can be shared
5984 with other functions. For dummy arguments, the typing is done to
5985 this result, even if it has to be repeated for each call. */
5986 if (has_alternate_specifier
5987 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
5989 if (!sym->attr.dummy)
5991 TREE_TYPE (sym->backend_decl)
5992 = build_function_type (integer_type_node,
5993 TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
5994 se->expr = gfc_build_addr_expr (NULL_TREE, sym->backend_decl);
5996 else
5997 TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
6000 fntype = TREE_TYPE (TREE_TYPE (se->expr));
6001 se->expr = build_call_vec (TREE_TYPE (fntype), se->expr, arglist);
6003 /* Allocatable scalar function results must be freed and nullified
6004 after use. This necessitates the creation of a temporary to
6005 hold the result to prevent duplicate calls. */
6006 if (!byref && sym->ts.type != BT_CHARACTER
6007 && sym->attr.allocatable && !sym->attr.dimension)
6009 tmp = gfc_create_var (TREE_TYPE (se->expr), NULL);
6010 gfc_add_modify (&se->pre, tmp, se->expr);
6011 se->expr = tmp;
6012 tmp = gfc_call_free (tmp);
6013 gfc_add_expr_to_block (&post, tmp);
6014 gfc_add_modify (&post, se->expr, build_int_cst (TREE_TYPE (se->expr), 0));
6017 /* If we have a pointer function, but we don't want a pointer, e.g.
6018 something like
6019 x = f()
6020 where f is pointer valued, we have to dereference the result. */
6021 if (!se->want_pointer && !byref
6022 && ((!comp && (sym->attr.pointer || sym->attr.allocatable))
6023 || (comp && (comp->attr.pointer || comp->attr.allocatable))))
6024 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
6026 /* f2c calling conventions require a scalar default real function to
6027 return a double precision result. Convert this back to default
6028 real. We only care about the cases that can happen in Fortran 77.
6030 if (flag_f2c && sym->ts.type == BT_REAL
6031 && sym->ts.kind == gfc_default_real_kind
6032 && !sym->attr.always_explicit)
6033 se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
6035 /* A pure function may still have side-effects - it may modify its
6036 parameters. */
6037 TREE_SIDE_EFFECTS (se->expr) = 1;
6038 #if 0
6039 if (!sym->attr.pure)
6040 TREE_SIDE_EFFECTS (se->expr) = 1;
6041 #endif
6043 if (byref)
6045 /* Add the function call to the pre chain. There is no expression. */
6046 gfc_add_expr_to_block (&se->pre, se->expr);
6047 se->expr = NULL_TREE;
6049 if (!se->direct_byref)
6051 if ((sym->attr.dimension && !comp) || (comp && comp->attr.dimension))
6053 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
6055 /* Check the data pointer hasn't been modified. This would
6056 happen in a function returning a pointer. */
6057 tmp = gfc_conv_descriptor_data_get (info->descriptor);
6058 tmp = fold_build2_loc (input_location, NE_EXPR,
6059 boolean_type_node,
6060 tmp, info->data);
6061 gfc_trans_runtime_check (true, false, tmp, &se->pre, NULL,
6062 gfc_msg_fault);
6064 se->expr = info->descriptor;
6065 /* Bundle in the string length. */
6066 se->string_length = len;
6068 else if (ts.type == BT_CHARACTER)
6070 /* Dereference for character pointer results. */
6071 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
6072 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
6073 se->expr = build_fold_indirect_ref_loc (input_location, var);
6074 else
6075 se->expr = var;
6077 se->string_length = len;
6079 else
6081 gcc_assert (ts.type == BT_COMPLEX && flag_f2c);
6082 se->expr = build_fold_indirect_ref_loc (input_location, var);
6087 /* Follow the function call with the argument post block. */
6088 if (byref)
6090 gfc_add_block_to_block (&se->pre, &post);
6092 /* Transformational functions of derived types with allocatable
6093 components must have the result allocatable components copied. */
6094 arg = expr->value.function.actual;
6095 if (result && arg && expr->rank
6096 && expr->value.function.isym
6097 && expr->value.function.isym->transformational
6098 && arg->expr->ts.type == BT_DERIVED
6099 && arg->expr->ts.u.derived->attr.alloc_comp)
6101 tree tmp2;
6102 /* Copy the allocatable components. We have to use a
6103 temporary here to prevent source allocatable components
6104 from being corrupted. */
6105 tmp2 = gfc_evaluate_now (result, &se->pre);
6106 tmp = gfc_copy_alloc_comp (arg->expr->ts.u.derived,
6107 result, tmp2, expr->rank);
6108 gfc_add_expr_to_block (&se->pre, tmp);
6109 tmp = gfc_copy_allocatable_data (result, tmp2, TREE_TYPE(tmp2),
6110 expr->rank);
6111 gfc_add_expr_to_block (&se->pre, tmp);
6113 /* Finally free the temporary's data field. */
6114 tmp = gfc_conv_descriptor_data_get (tmp2);
6115 tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
6116 NULL_TREE, NULL_TREE, true,
6117 NULL, false);
6118 gfc_add_expr_to_block (&se->pre, tmp);
6121 else
6123 /* For a function with a class array result, save the result as
6124 a temporary, set the info fields needed by the scalarizer and
6125 call the finalization function of the temporary. Note that the
6126 nullification of allocatable components needed by the result
6127 is done in gfc_trans_assignment_1. */
6128 if (expr && ((gfc_is_alloc_class_array_function (expr)
6129 && se->ss && se->ss->loop)
6130 || gfc_is_alloc_class_scalar_function (expr))
6131 && se->expr && GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
6132 && expr->must_finalize)
6134 tree final_fndecl;
6135 tree is_final;
6136 int n;
6137 if (se->ss && se->ss->loop)
6139 se->expr = gfc_evaluate_now (se->expr, &se->ss->loop->pre);
6140 tmp = gfc_class_data_get (se->expr);
6141 info->descriptor = tmp;
6142 info->data = gfc_conv_descriptor_data_get (tmp);
6143 info->offset = gfc_conv_descriptor_offset_get (tmp);
6144 for (n = 0; n < se->ss->loop->dimen; n++)
6146 tree dim = gfc_rank_cst[n];
6147 se->ss->loop->to[n] = gfc_conv_descriptor_ubound_get (tmp, dim);
6148 se->ss->loop->from[n] = gfc_conv_descriptor_lbound_get (tmp, dim);
6151 else
6153 /* TODO Eliminate the doubling of temporaries. This
6154 one is necessary to ensure no memory leakage. */
6155 se->expr = gfc_evaluate_now (se->expr, &se->pre);
6156 tmp = gfc_class_data_get (se->expr);
6157 tmp = gfc_conv_scalar_to_descriptor (se, tmp,
6158 CLASS_DATA (expr->value.function.esym->result)->attr);
6161 final_fndecl = gfc_class_vtab_final_get (se->expr);
6162 is_final = fold_build2_loc (input_location, NE_EXPR,
6163 boolean_type_node,
6164 final_fndecl,
6165 fold_convert (TREE_TYPE (final_fndecl),
6166 null_pointer_node));
6167 final_fndecl = build_fold_indirect_ref_loc (input_location,
6168 final_fndecl);
6169 tmp = build_call_expr_loc (input_location,
6170 final_fndecl, 3,
6171 gfc_build_addr_expr (NULL, tmp),
6172 gfc_class_vtab_size_get (se->expr),
6173 boolean_false_node);
6174 tmp = fold_build3_loc (input_location, COND_EXPR,
6175 void_type_node, is_final, tmp,
6176 build_empty_stmt (input_location));
6178 if (se->ss && se->ss->loop)
6180 gfc_add_expr_to_block (&se->ss->loop->post, tmp);
6181 tmp = gfc_call_free (info->data);
6182 gfc_add_expr_to_block (&se->ss->loop->post, tmp);
6184 else
6186 gfc_add_expr_to_block (&se->post, tmp);
6187 tmp = gfc_class_data_get (se->expr);
6188 tmp = gfc_call_free (tmp);
6189 gfc_add_expr_to_block (&se->post, tmp);
6191 expr->must_finalize = 0;
6194 gfc_add_block_to_block (&se->post, &post);
6197 return has_alternate_specifier;
6201 /* Fill a character string with spaces. */
6203 static tree
6204 fill_with_spaces (tree start, tree type, tree size)
6206 stmtblock_t block, loop;
6207 tree i, el, exit_label, cond, tmp;
6209 /* For a simple char type, we can call memset(). */
6210 if (compare_tree_int (TYPE_SIZE_UNIT (type), 1) == 0)
6211 return build_call_expr_loc (input_location,
6212 builtin_decl_explicit (BUILT_IN_MEMSET),
6213 3, start,
6214 build_int_cst (gfc_get_int_type (gfc_c_int_kind),
6215 lang_hooks.to_target_charset (' ')),
6216 size);
6218 /* Otherwise, we use a loop:
6219 for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
6220 *el = (type) ' ';
6223 /* Initialize variables. */
6224 gfc_init_block (&block);
6225 i = gfc_create_var (sizetype, "i");
6226 gfc_add_modify (&block, i, fold_convert (sizetype, size));
6227 el = gfc_create_var (build_pointer_type (type), "el");
6228 gfc_add_modify (&block, el, fold_convert (TREE_TYPE (el), start));
6229 exit_label = gfc_build_label_decl (NULL_TREE);
6230 TREE_USED (exit_label) = 1;
6233 /* Loop body. */
6234 gfc_init_block (&loop);
6236 /* Exit condition. */
6237 cond = fold_build2_loc (input_location, LE_EXPR, boolean_type_node, i,
6238 build_zero_cst (sizetype));
6239 tmp = build1_v (GOTO_EXPR, exit_label);
6240 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
6241 build_empty_stmt (input_location));
6242 gfc_add_expr_to_block (&loop, tmp);
6244 /* Assignment. */
6245 gfc_add_modify (&loop,
6246 fold_build1_loc (input_location, INDIRECT_REF, type, el),
6247 build_int_cst (type, lang_hooks.to_target_charset (' ')));
6249 /* Increment loop variables. */
6250 gfc_add_modify (&loop, i,
6251 fold_build2_loc (input_location, MINUS_EXPR, sizetype, i,
6252 TYPE_SIZE_UNIT (type)));
6253 gfc_add_modify (&loop, el,
6254 fold_build_pointer_plus_loc (input_location,
6255 el, TYPE_SIZE_UNIT (type)));
6257 /* Making the loop... actually loop! */
6258 tmp = gfc_finish_block (&loop);
6259 tmp = build1_v (LOOP_EXPR, tmp);
6260 gfc_add_expr_to_block (&block, tmp);
6262 /* The exit label. */
6263 tmp = build1_v (LABEL_EXPR, exit_label);
6264 gfc_add_expr_to_block (&block, tmp);
6267 return gfc_finish_block (&block);
6271 /* Generate code to copy a string. */
6273 void
6274 gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
6275 int dkind, tree slength, tree src, int skind)
6277 tree tmp, dlen, slen;
6278 tree dsc;
6279 tree ssc;
6280 tree cond;
6281 tree cond2;
6282 tree tmp2;
6283 tree tmp3;
6284 tree tmp4;
6285 tree chartype;
6286 stmtblock_t tempblock;
6288 gcc_assert (dkind == skind);
6290 if (slength != NULL_TREE)
6292 slen = fold_convert (size_type_node, gfc_evaluate_now (slength, block));
6293 ssc = gfc_string_to_single_character (slen, src, skind);
6295 else
6297 slen = build_int_cst (size_type_node, 1);
6298 ssc = src;
6301 if (dlength != NULL_TREE)
6303 dlen = fold_convert (size_type_node, gfc_evaluate_now (dlength, block));
6304 dsc = gfc_string_to_single_character (dlen, dest, dkind);
6306 else
6308 dlen = build_int_cst (size_type_node, 1);
6309 dsc = dest;
6312 /* Assign directly if the types are compatible. */
6313 if (dsc != NULL_TREE && ssc != NULL_TREE
6314 && TREE_TYPE (dsc) == TREE_TYPE (ssc))
6316 gfc_add_modify (block, dsc, ssc);
6317 return;
6320 /* Do nothing if the destination length is zero. */
6321 cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node, dlen,
6322 build_int_cst (size_type_node, 0));
6324 /* The following code was previously in _gfortran_copy_string:
6326 // The two strings may overlap so we use memmove.
6327 void
6328 copy_string (GFC_INTEGER_4 destlen, char * dest,
6329 GFC_INTEGER_4 srclen, const char * src)
6331 if (srclen >= destlen)
6333 // This will truncate if too long.
6334 memmove (dest, src, destlen);
6336 else
6338 memmove (dest, src, srclen);
6339 // Pad with spaces.
6340 memset (&dest[srclen], ' ', destlen - srclen);
6344 We're now doing it here for better optimization, but the logic
6345 is the same. */
6347 /* For non-default character kinds, we have to multiply the string
6348 length by the base type size. */
6349 chartype = gfc_get_char_type (dkind);
6350 slen = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
6351 fold_convert (size_type_node, slen),
6352 fold_convert (size_type_node,
6353 TYPE_SIZE_UNIT (chartype)));
6354 dlen = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
6355 fold_convert (size_type_node, dlen),
6356 fold_convert (size_type_node,
6357 TYPE_SIZE_UNIT (chartype)));
6359 if (dlength && POINTER_TYPE_P (TREE_TYPE (dest)))
6360 dest = fold_convert (pvoid_type_node, dest);
6361 else
6362 dest = gfc_build_addr_expr (pvoid_type_node, dest);
6364 if (slength && POINTER_TYPE_P (TREE_TYPE (src)))
6365 src = fold_convert (pvoid_type_node, src);
6366 else
6367 src = gfc_build_addr_expr (pvoid_type_node, src);
6369 /* Truncate string if source is too long. */
6370 cond2 = fold_build2_loc (input_location, GE_EXPR, boolean_type_node, slen,
6371 dlen);
6372 tmp2 = build_call_expr_loc (input_location,
6373 builtin_decl_explicit (BUILT_IN_MEMMOVE),
6374 3, dest, src, dlen);
6376 /* Else copy and pad with spaces. */
6377 tmp3 = build_call_expr_loc (input_location,
6378 builtin_decl_explicit (BUILT_IN_MEMMOVE),
6379 3, dest, src, slen);
6381 tmp4 = fold_build_pointer_plus_loc (input_location, dest, slen);
6382 tmp4 = fill_with_spaces (tmp4, chartype,
6383 fold_build2_loc (input_location, MINUS_EXPR,
6384 TREE_TYPE(dlen), dlen, slen));
6386 gfc_init_block (&tempblock);
6387 gfc_add_expr_to_block (&tempblock, tmp3);
6388 gfc_add_expr_to_block (&tempblock, tmp4);
6389 tmp3 = gfc_finish_block (&tempblock);
6391 /* The whole copy_string function is there. */
6392 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
6393 tmp2, tmp3);
6394 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
6395 build_empty_stmt (input_location));
6396 gfc_add_expr_to_block (block, tmp);
6400 /* Translate a statement function.
6401 The value of a statement function reference is obtained by evaluating the
6402 expression using the values of the actual arguments for the values of the
6403 corresponding dummy arguments. */
6405 static void
6406 gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
6408 gfc_symbol *sym;
6409 gfc_symbol *fsym;
6410 gfc_formal_arglist *fargs;
6411 gfc_actual_arglist *args;
6412 gfc_se lse;
6413 gfc_se rse;
6414 gfc_saved_var *saved_vars;
6415 tree *temp_vars;
6416 tree type;
6417 tree tmp;
6418 int n;
6420 sym = expr->symtree->n.sym;
6421 args = expr->value.function.actual;
6422 gfc_init_se (&lse, NULL);
6423 gfc_init_se (&rse, NULL);
6425 n = 0;
6426 for (fargs = gfc_sym_get_dummy_args (sym); fargs; fargs = fargs->next)
6427 n++;
6428 saved_vars = XCNEWVEC (gfc_saved_var, n);
6429 temp_vars = XCNEWVEC (tree, n);
6431 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
6432 fargs = fargs->next, n++)
6434 /* Each dummy shall be specified, explicitly or implicitly, to be
6435 scalar. */
6436 gcc_assert (fargs->sym->attr.dimension == 0);
6437 fsym = fargs->sym;
6439 if (fsym->ts.type == BT_CHARACTER)
6441 /* Copy string arguments. */
6442 tree arglen;
6444 gcc_assert (fsym->ts.u.cl && fsym->ts.u.cl->length
6445 && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT);
6447 /* Create a temporary to hold the value. */
6448 if (fsym->ts.u.cl->backend_decl == NULL_TREE)
6449 fsym->ts.u.cl->backend_decl
6450 = gfc_conv_constant_to_tree (fsym->ts.u.cl->length);
6452 type = gfc_get_character_type (fsym->ts.kind, fsym->ts.u.cl);
6453 temp_vars[n] = gfc_create_var (type, fsym->name);
6455 arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
6457 gfc_conv_expr (&rse, args->expr);
6458 gfc_conv_string_parameter (&rse);
6459 gfc_add_block_to_block (&se->pre, &lse.pre);
6460 gfc_add_block_to_block (&se->pre, &rse.pre);
6462 gfc_trans_string_copy (&se->pre, arglen, temp_vars[n], fsym->ts.kind,
6463 rse.string_length, rse.expr, fsym->ts.kind);
6464 gfc_add_block_to_block (&se->pre, &lse.post);
6465 gfc_add_block_to_block (&se->pre, &rse.post);
6467 else
6469 /* For everything else, just evaluate the expression. */
6471 /* Create a temporary to hold the value. */
6472 type = gfc_typenode_for_spec (&fsym->ts);
6473 temp_vars[n] = gfc_create_var (type, fsym->name);
6475 gfc_conv_expr (&lse, args->expr);
6477 gfc_add_block_to_block (&se->pre, &lse.pre);
6478 gfc_add_modify (&se->pre, temp_vars[n], lse.expr);
6479 gfc_add_block_to_block (&se->pre, &lse.post);
6482 args = args->next;
6485 /* Use the temporary variables in place of the real ones. */
6486 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
6487 fargs = fargs->next, n++)
6488 gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
6490 gfc_conv_expr (se, sym->value);
6492 if (sym->ts.type == BT_CHARACTER)
6494 gfc_conv_const_charlen (sym->ts.u.cl);
6496 /* Force the expression to the correct length. */
6497 if (!INTEGER_CST_P (se->string_length)
6498 || tree_int_cst_lt (se->string_length,
6499 sym->ts.u.cl->backend_decl))
6501 type = gfc_get_character_type (sym->ts.kind, sym->ts.u.cl);
6502 tmp = gfc_create_var (type, sym->name);
6503 tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
6504 gfc_trans_string_copy (&se->pre, sym->ts.u.cl->backend_decl, tmp,
6505 sym->ts.kind, se->string_length, se->expr,
6506 sym->ts.kind);
6507 se->expr = tmp;
6509 se->string_length = sym->ts.u.cl->backend_decl;
6512 /* Restore the original variables. */
6513 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
6514 fargs = fargs->next, n++)
6515 gfc_restore_sym (fargs->sym, &saved_vars[n]);
6516 free (temp_vars);
6517 free (saved_vars);
6521 /* Translate a function expression. */
6523 static void
6524 gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
6526 gfc_symbol *sym;
6528 if (expr->value.function.isym)
6530 gfc_conv_intrinsic_function (se, expr);
6531 return;
6534 /* expr.value.function.esym is the resolved (specific) function symbol for
6535 most functions. However this isn't set for dummy procedures. */
6536 sym = expr->value.function.esym;
6537 if (!sym)
6538 sym = expr->symtree->n.sym;
6540 /* The IEEE_ARITHMETIC functions are caught here. */
6541 if (sym->from_intmod == INTMOD_IEEE_ARITHMETIC)
6542 if (gfc_conv_ieee_arithmetic_function (se, expr))
6543 return;
6545 /* We distinguish statement functions from general functions to improve
6546 runtime performance. */
6547 if (sym->attr.proc == PROC_ST_FUNCTION)
6549 gfc_conv_statement_function (se, expr);
6550 return;
6553 gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
6554 NULL);
6558 /* Determine whether the given EXPR_CONSTANT is a zero initializer. */
6560 static bool
6561 is_zero_initializer_p (gfc_expr * expr)
6563 if (expr->expr_type != EXPR_CONSTANT)
6564 return false;
6566 /* We ignore constants with prescribed memory representations for now. */
6567 if (expr->representation.string)
6568 return false;
6570 switch (expr->ts.type)
6572 case BT_INTEGER:
6573 return mpz_cmp_si (expr->value.integer, 0) == 0;
6575 case BT_REAL:
6576 return mpfr_zero_p (expr->value.real)
6577 && MPFR_SIGN (expr->value.real) >= 0;
6579 case BT_LOGICAL:
6580 return expr->value.logical == 0;
6582 case BT_COMPLEX:
6583 return mpfr_zero_p (mpc_realref (expr->value.complex))
6584 && MPFR_SIGN (mpc_realref (expr->value.complex)) >= 0
6585 && mpfr_zero_p (mpc_imagref (expr->value.complex))
6586 && MPFR_SIGN (mpc_imagref (expr->value.complex)) >= 0;
6588 default:
6589 break;
6591 return false;
6595 static void
6596 gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
6598 gfc_ss *ss;
6600 ss = se->ss;
6601 gcc_assert (ss != NULL && ss != gfc_ss_terminator);
6602 gcc_assert (ss->info->expr == expr && ss->info->type == GFC_SS_CONSTRUCTOR);
6604 gfc_conv_tmp_array_ref (se);
6608 /* Build a static initializer. EXPR is the expression for the initial value.
6609 The other parameters describe the variable of the component being
6610 initialized. EXPR may be null. */
6612 tree
6613 gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
6614 bool array, bool pointer, bool procptr)
6616 gfc_se se;
6618 if (flag_coarray != GFC_FCOARRAY_LIB && ts->type == BT_DERIVED
6619 && ts->u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
6620 && ts->u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
6621 return build_constructor (type, NULL);
6623 if (!(expr || pointer || procptr))
6624 return NULL_TREE;
6626 /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
6627 (these are the only two iso_c_binding derived types that can be
6628 used as initialization expressions). If so, we need to modify
6629 the 'expr' to be that for a (void *). */
6630 if (expr != NULL && expr->ts.type == BT_DERIVED
6631 && expr->ts.is_iso_c && expr->ts.u.derived)
6633 gfc_symbol *derived = expr->ts.u.derived;
6635 /* The derived symbol has already been converted to a (void *). Use
6636 its kind. */
6637 expr = gfc_get_int_expr (derived->ts.kind, NULL, 0);
6638 expr->ts.f90_type = derived->ts.f90_type;
6640 gfc_init_se (&se, NULL);
6641 gfc_conv_constant (&se, expr);
6642 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
6643 return se.expr;
6646 if (array && !procptr)
6648 tree ctor;
6649 /* Arrays need special handling. */
6650 if (pointer)
6651 ctor = gfc_build_null_descriptor (type);
6652 /* Special case assigning an array to zero. */
6653 else if (is_zero_initializer_p (expr))
6654 ctor = build_constructor (type, NULL);
6655 else
6656 ctor = gfc_conv_array_initializer (type, expr);
6657 TREE_STATIC (ctor) = 1;
6658 return ctor;
6660 else if (pointer || procptr)
6662 if (ts->type == BT_CLASS && !procptr)
6664 gfc_init_se (&se, NULL);
6665 gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
6666 gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
6667 TREE_STATIC (se.expr) = 1;
6668 return se.expr;
6670 else if (!expr || expr->expr_type == EXPR_NULL)
6671 return fold_convert (type, null_pointer_node);
6672 else
6674 gfc_init_se (&se, NULL);
6675 se.want_pointer = 1;
6676 gfc_conv_expr (&se, expr);
6677 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
6678 return se.expr;
6681 else
6683 switch (ts->type)
6685 case BT_DERIVED:
6686 case BT_CLASS:
6687 gfc_init_se (&se, NULL);
6688 if (ts->type == BT_CLASS && expr->expr_type == EXPR_NULL)
6689 gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
6690 else
6691 gfc_conv_structure (&se, expr, 1);
6692 gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
6693 TREE_STATIC (se.expr) = 1;
6694 return se.expr;
6696 case BT_CHARACTER:
6698 tree ctor = gfc_conv_string_init (ts->u.cl->backend_decl,expr);
6699 TREE_STATIC (ctor) = 1;
6700 return ctor;
6703 default:
6704 gfc_init_se (&se, NULL);
6705 gfc_conv_constant (&se, expr);
6706 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
6707 return se.expr;
6712 static tree
6713 gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
6715 gfc_se rse;
6716 gfc_se lse;
6717 gfc_ss *rss;
6718 gfc_ss *lss;
6719 gfc_array_info *lss_array;
6720 stmtblock_t body;
6721 stmtblock_t block;
6722 gfc_loopinfo loop;
6723 int n;
6724 tree tmp;
6726 gfc_start_block (&block);
6728 /* Initialize the scalarizer. */
6729 gfc_init_loopinfo (&loop);
6731 gfc_init_se (&lse, NULL);
6732 gfc_init_se (&rse, NULL);
6734 /* Walk the rhs. */
6735 rss = gfc_walk_expr (expr);
6736 if (rss == gfc_ss_terminator)
6737 /* The rhs is scalar. Add a ss for the expression. */
6738 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr);
6740 /* Create a SS for the destination. */
6741 lss = gfc_get_array_ss (gfc_ss_terminator, NULL, cm->as->rank,
6742 GFC_SS_COMPONENT);
6743 lss_array = &lss->info->data.array;
6744 lss_array->shape = gfc_get_shape (cm->as->rank);
6745 lss_array->descriptor = dest;
6746 lss_array->data = gfc_conv_array_data (dest);
6747 lss_array->offset = gfc_conv_array_offset (dest);
6748 for (n = 0; n < cm->as->rank; n++)
6750 lss_array->start[n] = gfc_conv_array_lbound (dest, n);
6751 lss_array->stride[n] = gfc_index_one_node;
6753 mpz_init (lss_array->shape[n]);
6754 mpz_sub (lss_array->shape[n], cm->as->upper[n]->value.integer,
6755 cm->as->lower[n]->value.integer);
6756 mpz_add_ui (lss_array->shape[n], lss_array->shape[n], 1);
6759 /* Associate the SS with the loop. */
6760 gfc_add_ss_to_loop (&loop, lss);
6761 gfc_add_ss_to_loop (&loop, rss);
6763 /* Calculate the bounds of the scalarization. */
6764 gfc_conv_ss_startstride (&loop);
6766 /* Setup the scalarizing loops. */
6767 gfc_conv_loop_setup (&loop, &expr->where);
6769 /* Setup the gfc_se structures. */
6770 gfc_copy_loopinfo_to_se (&lse, &loop);
6771 gfc_copy_loopinfo_to_se (&rse, &loop);
6773 rse.ss = rss;
6774 gfc_mark_ss_chain_used (rss, 1);
6775 lse.ss = lss;
6776 gfc_mark_ss_chain_used (lss, 1);
6778 /* Start the scalarized loop body. */
6779 gfc_start_scalarized_body (&loop, &body);
6781 gfc_conv_tmp_array_ref (&lse);
6782 if (cm->ts.type == BT_CHARACTER)
6783 lse.string_length = cm->ts.u.cl->backend_decl;
6785 gfc_conv_expr (&rse, expr);
6787 tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false);
6788 gfc_add_expr_to_block (&body, tmp);
6790 gcc_assert (rse.ss == gfc_ss_terminator);
6792 /* Generate the copying loops. */
6793 gfc_trans_scalarizing_loops (&loop, &body);
6795 /* Wrap the whole thing up. */
6796 gfc_add_block_to_block (&block, &loop.pre);
6797 gfc_add_block_to_block (&block, &loop.post);
6799 gcc_assert (lss_array->shape != NULL);
6800 gfc_free_shape (&lss_array->shape, cm->as->rank);
6801 gfc_cleanup_loop (&loop);
6803 return gfc_finish_block (&block);
6807 static tree
6808 gfc_trans_alloc_subarray_assign (tree dest, gfc_component * cm,
6809 gfc_expr * expr)
6811 gfc_se se;
6812 stmtblock_t block;
6813 tree offset;
6814 int n;
6815 tree tmp;
6816 tree tmp2;
6817 gfc_array_spec *as;
6818 gfc_expr *arg = NULL;
6820 gfc_start_block (&block);
6821 gfc_init_se (&se, NULL);
6823 /* Get the descriptor for the expressions. */
6824 se.want_pointer = 0;
6825 gfc_conv_expr_descriptor (&se, expr);
6826 gfc_add_block_to_block (&block, &se.pre);
6827 gfc_add_modify (&block, dest, se.expr);
6829 /* Deal with arrays of derived types with allocatable components. */
6830 if (cm->ts.type == BT_DERIVED
6831 && cm->ts.u.derived->attr.alloc_comp)
6832 tmp = gfc_copy_alloc_comp (cm->ts.u.derived,
6833 se.expr, dest,
6834 cm->as->rank);
6835 else if (cm->ts.type == BT_CLASS && expr->ts.type == BT_DERIVED
6836 && CLASS_DATA(cm)->attr.allocatable)
6838 if (cm->ts.u.derived->attr.alloc_comp)
6839 tmp = gfc_copy_alloc_comp (expr->ts.u.derived,
6840 se.expr, dest,
6841 expr->rank);
6842 else
6844 tmp = TREE_TYPE (dest);
6845 tmp = gfc_duplicate_allocatable (dest, se.expr,
6846 tmp, expr->rank, NULL_TREE);
6849 else
6850 tmp = gfc_duplicate_allocatable (dest, se.expr,
6851 TREE_TYPE(cm->backend_decl),
6852 cm->as->rank, NULL_TREE);
6854 gfc_add_expr_to_block (&block, tmp);
6855 gfc_add_block_to_block (&block, &se.post);
6857 if (expr->expr_type != EXPR_VARIABLE)
6858 gfc_conv_descriptor_data_set (&block, se.expr,
6859 null_pointer_node);
6861 /* We need to know if the argument of a conversion function is a
6862 variable, so that the correct lower bound can be used. */
6863 if (expr->expr_type == EXPR_FUNCTION
6864 && expr->value.function.isym
6865 && expr->value.function.isym->conversion
6866 && expr->value.function.actual->expr
6867 && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE)
6868 arg = expr->value.function.actual->expr;
6870 /* Obtain the array spec of full array references. */
6871 if (arg)
6872 as = gfc_get_full_arrayspec_from_expr (arg);
6873 else
6874 as = gfc_get_full_arrayspec_from_expr (expr);
6876 /* Shift the lbound and ubound of temporaries to being unity,
6877 rather than zero, based. Always calculate the offset. */
6878 offset = gfc_conv_descriptor_offset_get (dest);
6879 gfc_add_modify (&block, offset, gfc_index_zero_node);
6880 tmp2 =gfc_create_var (gfc_array_index_type, NULL);
6882 for (n = 0; n < expr->rank; n++)
6884 tree span;
6885 tree lbound;
6887 /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
6888 TODO It looks as if gfc_conv_expr_descriptor should return
6889 the correct bounds and that the following should not be
6890 necessary. This would simplify gfc_conv_intrinsic_bound
6891 as well. */
6892 if (as && as->lower[n])
6894 gfc_se lbse;
6895 gfc_init_se (&lbse, NULL);
6896 gfc_conv_expr (&lbse, as->lower[n]);
6897 gfc_add_block_to_block (&block, &lbse.pre);
6898 lbound = gfc_evaluate_now (lbse.expr, &block);
6900 else if (as && arg)
6902 tmp = gfc_get_symbol_decl (arg->symtree->n.sym);
6903 lbound = gfc_conv_descriptor_lbound_get (tmp,
6904 gfc_rank_cst[n]);
6906 else if (as)
6907 lbound = gfc_conv_descriptor_lbound_get (dest,
6908 gfc_rank_cst[n]);
6909 else
6910 lbound = gfc_index_one_node;
6912 lbound = fold_convert (gfc_array_index_type, lbound);
6914 /* Shift the bounds and set the offset accordingly. */
6915 tmp = gfc_conv_descriptor_ubound_get (dest, gfc_rank_cst[n]);
6916 span = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6917 tmp, gfc_conv_descriptor_lbound_get (dest, gfc_rank_cst[n]));
6918 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
6919 span, lbound);
6920 gfc_conv_descriptor_ubound_set (&block, dest,
6921 gfc_rank_cst[n], tmp);
6922 gfc_conv_descriptor_lbound_set (&block, dest,
6923 gfc_rank_cst[n], lbound);
6925 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
6926 gfc_conv_descriptor_lbound_get (dest,
6927 gfc_rank_cst[n]),
6928 gfc_conv_descriptor_stride_get (dest,
6929 gfc_rank_cst[n]));
6930 gfc_add_modify (&block, tmp2, tmp);
6931 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
6932 offset, tmp2);
6933 gfc_conv_descriptor_offset_set (&block, dest, tmp);
6936 if (arg)
6938 /* If a conversion expression has a null data pointer
6939 argument, nullify the allocatable component. */
6940 tree non_null_expr;
6941 tree null_expr;
6943 if (arg->symtree->n.sym->attr.allocatable
6944 || arg->symtree->n.sym->attr.pointer)
6946 non_null_expr = gfc_finish_block (&block);
6947 gfc_start_block (&block);
6948 gfc_conv_descriptor_data_set (&block, dest,
6949 null_pointer_node);
6950 null_expr = gfc_finish_block (&block);
6951 tmp = gfc_conv_descriptor_data_get (arg->symtree->n.sym->backend_decl);
6952 tmp = build2_loc (input_location, EQ_EXPR, boolean_type_node, tmp,
6953 fold_convert (TREE_TYPE (tmp), null_pointer_node));
6954 return build3_v (COND_EXPR, tmp,
6955 null_expr, non_null_expr);
6959 return gfc_finish_block (&block);
6963 /* Allocate or reallocate scalar component, as necessary. */
6965 static void
6966 alloc_scalar_allocatable_for_subcomponent_assignment (stmtblock_t *block,
6967 tree comp,
6968 gfc_component *cm,
6969 gfc_expr *expr2,
6970 gfc_symbol *sym)
6972 tree tmp;
6973 tree ptr;
6974 tree size;
6975 tree size_in_bytes;
6976 tree lhs_cl_size = NULL_TREE;
6978 if (!comp)
6979 return;
6981 if (!expr2 || expr2->rank)
6982 return;
6984 realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
6986 if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
6988 char name[GFC_MAX_SYMBOL_LEN+9];
6989 gfc_component *strlen;
6990 /* Use the rhs string length and the lhs element size. */
6991 gcc_assert (expr2->ts.type == BT_CHARACTER);
6992 if (!expr2->ts.u.cl->backend_decl)
6994 gfc_conv_string_length (expr2->ts.u.cl, expr2, block);
6995 gcc_assert (expr2->ts.u.cl->backend_decl);
6998 size = expr2->ts.u.cl->backend_decl;
7000 /* Ensure that cm->ts.u.cl->backend_decl is a componentref to _%s_length
7001 component. */
7002 sprintf (name, "_%s_length", cm->name);
7003 strlen = gfc_find_component (sym, name, true, true);
7004 lhs_cl_size = fold_build3_loc (input_location, COMPONENT_REF,
7005 gfc_charlen_type_node,
7006 TREE_OPERAND (comp, 0),
7007 strlen->backend_decl, NULL_TREE);
7009 tmp = TREE_TYPE (gfc_typenode_for_spec (&cm->ts));
7010 tmp = TYPE_SIZE_UNIT (tmp);
7011 size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
7012 TREE_TYPE (tmp), tmp,
7013 fold_convert (TREE_TYPE (tmp), size));
7015 else if (cm->ts.type == BT_CLASS)
7017 gcc_assert (expr2->ts.type == BT_CLASS || expr2->ts.type == BT_DERIVED);
7018 if (expr2->ts.type == BT_DERIVED)
7020 tmp = gfc_get_symbol_decl (expr2->ts.u.derived);
7021 size = TYPE_SIZE_UNIT (tmp);
7023 else
7025 gfc_expr *e2vtab;
7026 gfc_se se;
7027 e2vtab = gfc_find_and_cut_at_last_class_ref (expr2);
7028 gfc_add_vptr_component (e2vtab);
7029 gfc_add_size_component (e2vtab);
7030 gfc_init_se (&se, NULL);
7031 gfc_conv_expr (&se, e2vtab);
7032 gfc_add_block_to_block (block, &se.pre);
7033 size = fold_convert (size_type_node, se.expr);
7034 gfc_free_expr (e2vtab);
7036 size_in_bytes = size;
7038 else
7040 /* Otherwise use the length in bytes of the rhs. */
7041 size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&cm->ts));
7042 size_in_bytes = size;
7045 size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
7046 size_in_bytes, size_one_node);
7048 if (cm->ts.type == BT_DERIVED && cm->ts.u.derived->attr.alloc_comp)
7050 tmp = build_call_expr_loc (input_location,
7051 builtin_decl_explicit (BUILT_IN_CALLOC),
7052 2, build_one_cst (size_type_node),
7053 size_in_bytes);
7054 tmp = fold_convert (TREE_TYPE (comp), tmp);
7055 gfc_add_modify (block, comp, tmp);
7057 else
7059 tmp = build_call_expr_loc (input_location,
7060 builtin_decl_explicit (BUILT_IN_MALLOC),
7061 1, size_in_bytes);
7062 if (GFC_CLASS_TYPE_P (TREE_TYPE (comp)))
7063 ptr = gfc_class_data_get (comp);
7064 else
7065 ptr = comp;
7066 tmp = fold_convert (TREE_TYPE (ptr), tmp);
7067 gfc_add_modify (block, ptr, tmp);
7070 if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
7071 /* Update the lhs character length. */
7072 gfc_add_modify (block, lhs_cl_size, size);
7076 /* Assign a single component of a derived type constructor. */
7078 static tree
7079 gfc_trans_subcomponent_assign (tree dest, gfc_component * cm, gfc_expr * expr,
7080 gfc_symbol *sym, bool init)
7082 gfc_se se;
7083 gfc_se lse;
7084 stmtblock_t block;
7085 tree tmp;
7086 tree vtab;
7088 gfc_start_block (&block);
7090 if (cm->attr.pointer || cm->attr.proc_pointer)
7092 /* Only care about pointers here, not about allocatables. */
7093 gfc_init_se (&se, NULL);
7094 /* Pointer component. */
7095 if ((cm->attr.dimension || cm->attr.codimension)
7096 && !cm->attr.proc_pointer)
7098 /* Array pointer. */
7099 if (expr->expr_type == EXPR_NULL)
7100 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
7101 else
7103 se.direct_byref = 1;
7104 se.expr = dest;
7105 gfc_conv_expr_descriptor (&se, expr);
7106 gfc_add_block_to_block (&block, &se.pre);
7107 gfc_add_block_to_block (&block, &se.post);
7110 else
7112 /* Scalar pointers. */
7113 se.want_pointer = 1;
7114 gfc_conv_expr (&se, expr);
7115 gfc_add_block_to_block (&block, &se.pre);
7117 if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
7118 && expr->symtree->n.sym->attr.dummy)
7119 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
7121 gfc_add_modify (&block, dest,
7122 fold_convert (TREE_TYPE (dest), se.expr));
7123 gfc_add_block_to_block (&block, &se.post);
7126 else if (cm->ts.type == BT_CLASS && expr->expr_type == EXPR_NULL)
7128 /* NULL initialization for CLASS components. */
7129 tmp = gfc_trans_structure_assign (dest,
7130 gfc_class_initializer (&cm->ts, expr),
7131 false);
7132 gfc_add_expr_to_block (&block, tmp);
7134 else if ((cm->attr.dimension || cm->attr.codimension)
7135 && !cm->attr.proc_pointer)
7137 if (cm->attr.allocatable && expr->expr_type == EXPR_NULL)
7138 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
7139 else if (cm->attr.allocatable)
7141 tmp = gfc_trans_alloc_subarray_assign (dest, cm, expr);
7142 gfc_add_expr_to_block (&block, tmp);
7144 else
7146 tmp = gfc_trans_subarray_assign (dest, cm, expr);
7147 gfc_add_expr_to_block (&block, tmp);
7150 else if (cm->ts.type == BT_CLASS
7151 && CLASS_DATA (cm)->attr.dimension
7152 && CLASS_DATA (cm)->attr.allocatable
7153 && expr->ts.type == BT_DERIVED)
7155 vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
7156 vtab = gfc_build_addr_expr (NULL_TREE, vtab);
7157 tmp = gfc_class_vptr_get (dest);
7158 gfc_add_modify (&block, tmp,
7159 fold_convert (TREE_TYPE (tmp), vtab));
7160 tmp = gfc_class_data_get (dest);
7161 tmp = gfc_trans_alloc_subarray_assign (tmp, cm, expr);
7162 gfc_add_expr_to_block (&block, tmp);
7164 else if (init && (cm->attr.allocatable
7165 || (cm->ts.type == BT_CLASS && CLASS_DATA (cm)->attr.allocatable
7166 && expr->ts.type != BT_CLASS)))
7168 /* Take care about non-array allocatable components here. The alloc_*
7169 routine below is motivated by the alloc_scalar_allocatable_for_
7170 assignment() routine, but with the realloc portions removed and
7171 different input. */
7172 alloc_scalar_allocatable_for_subcomponent_assignment (&block,
7173 dest,
7175 expr,
7176 sym);
7177 /* The remainder of these instructions follow the if (cm->attr.pointer)
7178 if (!cm->attr.dimension) part above. */
7179 gfc_init_se (&se, NULL);
7180 gfc_conv_expr (&se, expr);
7181 gfc_add_block_to_block (&block, &se.pre);
7183 if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
7184 && expr->symtree->n.sym->attr.dummy)
7185 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
7187 if (cm->ts.type == BT_CLASS && expr->ts.type == BT_DERIVED)
7189 tmp = gfc_class_data_get (dest);
7190 tmp = build_fold_indirect_ref_loc (input_location, tmp);
7191 vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
7192 vtab = gfc_build_addr_expr (NULL_TREE, vtab);
7193 gfc_add_modify (&block, gfc_class_vptr_get (dest),
7194 fold_convert (TREE_TYPE (gfc_class_vptr_get (dest)), vtab));
7196 else
7197 tmp = build_fold_indirect_ref_loc (input_location, dest);
7199 /* For deferred strings insert a memcpy. */
7200 if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
7202 tree size;
7203 gcc_assert (se.string_length || expr->ts.u.cl->backend_decl);
7204 size = size_of_string_in_bytes (cm->ts.kind, se.string_length
7205 ? se.string_length
7206 : expr->ts.u.cl->backend_decl);
7207 tmp = gfc_build_memcpy_call (tmp, se.expr, size);
7208 gfc_add_expr_to_block (&block, tmp);
7210 else
7211 gfc_add_modify (&block, tmp,
7212 fold_convert (TREE_TYPE (tmp), se.expr));
7213 gfc_add_block_to_block (&block, &se.post);
7215 else if (expr->ts.type == BT_DERIVED && expr->ts.f90_type != BT_VOID)
7217 if (expr->expr_type != EXPR_STRUCTURE)
7219 tree dealloc = NULL_TREE;
7220 gfc_init_se (&se, NULL);
7221 gfc_conv_expr (&se, expr);
7222 gfc_add_block_to_block (&block, &se.pre);
7223 /* Prevent repeat evaluations in gfc_copy_alloc_comp by fixing the
7224 expression in a temporary variable and deallocate the allocatable
7225 components. Then we can the copy the expression to the result. */
7226 if (cm->ts.u.derived->attr.alloc_comp
7227 && expr->expr_type != EXPR_VARIABLE)
7229 se.expr = gfc_evaluate_now (se.expr, &block);
7230 dealloc = gfc_deallocate_alloc_comp (cm->ts.u.derived, se.expr,
7231 expr->rank);
7233 gfc_add_modify (&block, dest,
7234 fold_convert (TREE_TYPE (dest), se.expr));
7235 if (cm->ts.u.derived->attr.alloc_comp
7236 && expr->expr_type != EXPR_NULL)
7238 tmp = gfc_copy_alloc_comp (cm->ts.u.derived, se.expr,
7239 dest, expr->rank);
7240 gfc_add_expr_to_block (&block, tmp);
7241 if (dealloc != NULL_TREE)
7242 gfc_add_expr_to_block (&block, dealloc);
7244 gfc_add_block_to_block (&block, &se.post);
7246 else
7248 /* Nested constructors. */
7249 tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
7250 gfc_add_expr_to_block (&block, tmp);
7253 else if (gfc_deferred_strlen (cm, &tmp))
7255 tree strlen;
7256 strlen = tmp;
7257 gcc_assert (strlen);
7258 strlen = fold_build3_loc (input_location, COMPONENT_REF,
7259 TREE_TYPE (strlen),
7260 TREE_OPERAND (dest, 0),
7261 strlen, NULL_TREE);
7263 if (expr->expr_type == EXPR_NULL)
7265 tmp = build_int_cst (TREE_TYPE (cm->backend_decl), 0);
7266 gfc_add_modify (&block, dest, tmp);
7267 tmp = build_int_cst (TREE_TYPE (strlen), 0);
7268 gfc_add_modify (&block, strlen, tmp);
7270 else
7272 tree size;
7273 gfc_init_se (&se, NULL);
7274 gfc_conv_expr (&se, expr);
7275 size = size_of_string_in_bytes (cm->ts.kind, se.string_length);
7276 tmp = build_call_expr_loc (input_location,
7277 builtin_decl_explicit (BUILT_IN_MALLOC),
7278 1, size);
7279 gfc_add_modify (&block, dest,
7280 fold_convert (TREE_TYPE (dest), tmp));
7281 gfc_add_modify (&block, strlen, se.string_length);
7282 tmp = gfc_build_memcpy_call (dest, se.expr, size);
7283 gfc_add_expr_to_block (&block, tmp);
7286 else if (!cm->attr.artificial)
7288 /* Scalar component (excluding deferred parameters). */
7289 gfc_init_se (&se, NULL);
7290 gfc_init_se (&lse, NULL);
7292 gfc_conv_expr (&se, expr);
7293 if (cm->ts.type == BT_CHARACTER)
7294 lse.string_length = cm->ts.u.cl->backend_decl;
7295 lse.expr = dest;
7296 tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, false, false);
7297 gfc_add_expr_to_block (&block, tmp);
7299 return gfc_finish_block (&block);
7302 /* Assign a derived type constructor to a variable. */
7304 tree
7305 gfc_trans_structure_assign (tree dest, gfc_expr * expr, bool init)
7307 gfc_constructor *c;
7308 gfc_component *cm;
7309 stmtblock_t block;
7310 tree field;
7311 tree tmp;
7313 gfc_start_block (&block);
7314 cm = expr->ts.u.derived->components;
7316 if (expr->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
7317 && (expr->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
7318 || expr->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
7320 gfc_se se, lse;
7322 gcc_assert (cm->backend_decl == NULL);
7323 gfc_init_se (&se, NULL);
7324 gfc_init_se (&lse, NULL);
7325 gfc_conv_expr (&se, gfc_constructor_first (expr->value.constructor)->expr);
7326 lse.expr = dest;
7327 gfc_add_modify (&block, lse.expr,
7328 fold_convert (TREE_TYPE (lse.expr), se.expr));
7330 return gfc_finish_block (&block);
7333 for (c = gfc_constructor_first (expr->value.constructor);
7334 c; c = gfc_constructor_next (c), cm = cm->next)
7336 /* Skip absent members in default initializers. */
7337 if (!c->expr && !cm->attr.allocatable)
7338 continue;
7340 field = cm->backend_decl;
7341 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
7342 dest, field, NULL_TREE);
7343 if (!c->expr)
7345 gfc_expr *e = gfc_get_null_expr (NULL);
7346 tmp = gfc_trans_subcomponent_assign (tmp, cm, e, expr->ts.u.derived,
7347 init);
7348 gfc_free_expr (e);
7350 else
7351 tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr,
7352 expr->ts.u.derived, init);
7353 gfc_add_expr_to_block (&block, tmp);
7355 return gfc_finish_block (&block);
7358 /* Build an expression for a constructor. If init is nonzero then
7359 this is part of a static variable initializer. */
7361 void
7362 gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
7364 gfc_constructor *c;
7365 gfc_component *cm;
7366 tree val;
7367 tree type;
7368 tree tmp;
7369 vec<constructor_elt, va_gc> *v = NULL;
7371 gcc_assert (se->ss == NULL);
7372 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
7373 type = gfc_typenode_for_spec (&expr->ts);
7375 if (!init)
7377 /* Create a temporary variable and fill it in. */
7378 se->expr = gfc_create_var (type, expr->ts.u.derived->name);
7379 /* The symtree in expr is NULL, if the code to generate is for
7380 initializing the static members only. */
7381 tmp = gfc_trans_structure_assign (se->expr, expr, expr->symtree != NULL);
7382 gfc_add_expr_to_block (&se->pre, tmp);
7383 return;
7386 cm = expr->ts.u.derived->components;
7388 for (c = gfc_constructor_first (expr->value.constructor);
7389 c; c = gfc_constructor_next (c), cm = cm->next)
7391 /* Skip absent members in default initializers and allocatable
7392 components. Although the latter have a default initializer
7393 of EXPR_NULL,... by default, the static nullify is not needed
7394 since this is done every time we come into scope. */
7395 if (!c->expr || (cm->attr.allocatable && cm->attr.flavor != FL_PROCEDURE))
7396 continue;
7398 if (cm->initializer && cm->initializer->expr_type != EXPR_NULL
7399 && strcmp (cm->name, "_extends") == 0
7400 && cm->initializer->symtree)
7402 tree vtab;
7403 gfc_symbol *vtabs;
7404 vtabs = cm->initializer->symtree->n.sym;
7405 vtab = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtabs));
7406 vtab = unshare_expr_without_location (vtab);
7407 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, vtab);
7409 else if (cm->ts.u.derived && strcmp (cm->name, "_size") == 0)
7411 val = TYPE_SIZE_UNIT (gfc_get_derived_type (cm->ts.u.derived));
7412 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
7413 fold_convert (TREE_TYPE (cm->backend_decl),
7414 val));
7416 else if (cm->ts.type == BT_INTEGER && strcmp (cm->name, "_len") == 0)
7417 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
7418 fold_convert (TREE_TYPE (cm->backend_decl),
7419 integer_zero_node));
7420 else
7422 val = gfc_conv_initializer (c->expr, &cm->ts,
7423 TREE_TYPE (cm->backend_decl),
7424 cm->attr.dimension, cm->attr.pointer,
7425 cm->attr.proc_pointer);
7426 val = unshare_expr_without_location (val);
7428 /* Append it to the constructor list. */
7429 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
7432 se->expr = build_constructor (type, v);
7433 if (init)
7434 TREE_CONSTANT (se->expr) = 1;
7438 /* Translate a substring expression. */
7440 static void
7441 gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
7443 gfc_ref *ref;
7445 ref = expr->ref;
7447 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
7449 se->expr = gfc_build_wide_string_const (expr->ts.kind,
7450 expr->value.character.length,
7451 expr->value.character.string);
7453 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
7454 TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
7456 if (ref)
7457 gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
7461 /* Entry point for expression translation. Evaluates a scalar quantity.
7462 EXPR is the expression to be translated, and SE is the state structure if
7463 called from within the scalarized. */
7465 void
7466 gfc_conv_expr (gfc_se * se, gfc_expr * expr)
7468 gfc_ss *ss;
7470 ss = se->ss;
7471 if (ss && ss->info->expr == expr
7472 && (ss->info->type == GFC_SS_SCALAR
7473 || ss->info->type == GFC_SS_REFERENCE))
7475 gfc_ss_info *ss_info;
7477 ss_info = ss->info;
7478 /* Substitute a scalar expression evaluated outside the scalarization
7479 loop. */
7480 se->expr = ss_info->data.scalar.value;
7481 if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
7482 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
7484 se->string_length = ss_info->string_length;
7485 gfc_advance_se_ss_chain (se);
7486 return;
7489 /* We need to convert the expressions for the iso_c_binding derived types.
7490 C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
7491 null_pointer_node. C_PTR and C_FUNPTR are converted to match the
7492 typespec for the C_PTR and C_FUNPTR symbols, which has already been
7493 updated to be an integer with a kind equal to the size of a (void *). */
7494 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->ts.f90_type == BT_VOID
7495 && expr->ts.u.derived->attr.is_bind_c)
7497 if (expr->expr_type == EXPR_VARIABLE
7498 && (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
7499 || expr->symtree->n.sym->intmod_sym_id
7500 == ISOCBINDING_NULL_FUNPTR))
7502 /* Set expr_type to EXPR_NULL, which will result in
7503 null_pointer_node being used below. */
7504 expr->expr_type = EXPR_NULL;
7506 else
7508 /* Update the type/kind of the expression to be what the new
7509 type/kind are for the updated symbols of C_PTR/C_FUNPTR. */
7510 expr->ts.type = BT_INTEGER;
7511 expr->ts.f90_type = BT_VOID;
7512 expr->ts.kind = gfc_index_integer_kind;
7516 gfc_fix_class_refs (expr);
7518 switch (expr->expr_type)
7520 case EXPR_OP:
7521 gfc_conv_expr_op (se, expr);
7522 break;
7524 case EXPR_FUNCTION:
7525 gfc_conv_function_expr (se, expr);
7526 break;
7528 case EXPR_CONSTANT:
7529 gfc_conv_constant (se, expr);
7530 break;
7532 case EXPR_VARIABLE:
7533 gfc_conv_variable (se, expr);
7534 break;
7536 case EXPR_NULL:
7537 se->expr = null_pointer_node;
7538 break;
7540 case EXPR_SUBSTRING:
7541 gfc_conv_substring_expr (se, expr);
7542 break;
7544 case EXPR_STRUCTURE:
7545 gfc_conv_structure (se, expr, 0);
7546 break;
7548 case EXPR_ARRAY:
7549 gfc_conv_array_constructor_expr (se, expr);
7550 break;
7552 default:
7553 gcc_unreachable ();
7554 break;
7558 /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
7559 of an assignment. */
7560 void
7561 gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
7563 gfc_conv_expr (se, expr);
7564 /* All numeric lvalues should have empty post chains. If not we need to
7565 figure out a way of rewriting an lvalue so that it has no post chain. */
7566 gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
7569 /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
7570 numeric expressions. Used for scalar values where inserting cleanup code
7571 is inconvenient. */
7572 void
7573 gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
7575 tree val;
7577 gcc_assert (expr->ts.type != BT_CHARACTER);
7578 gfc_conv_expr (se, expr);
7579 if (se->post.head)
7581 val = gfc_create_var (TREE_TYPE (se->expr), NULL);
7582 gfc_add_modify (&se->pre, val, se->expr);
7583 se->expr = val;
7584 gfc_add_block_to_block (&se->pre, &se->post);
7588 /* Helper to translate an expression and convert it to a particular type. */
7589 void
7590 gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
7592 gfc_conv_expr_val (se, expr);
7593 se->expr = convert (type, se->expr);
7597 /* Converts an expression so that it can be passed by reference. Scalar
7598 values only. */
7600 void
7601 gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
7603 gfc_ss *ss;
7604 tree var;
7606 ss = se->ss;
7607 if (ss && ss->info->expr == expr
7608 && ss->info->type == GFC_SS_REFERENCE)
7610 /* Returns a reference to the scalar evaluated outside the loop
7611 for this case. */
7612 gfc_conv_expr (se, expr);
7614 if (expr->ts.type == BT_CHARACTER
7615 && expr->expr_type != EXPR_FUNCTION)
7616 gfc_conv_string_parameter (se);
7617 else
7618 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
7620 return;
7623 if (expr->ts.type == BT_CHARACTER)
7625 gfc_conv_expr (se, expr);
7626 gfc_conv_string_parameter (se);
7627 return;
7630 if (expr->expr_type == EXPR_VARIABLE)
7632 se->want_pointer = 1;
7633 gfc_conv_expr (se, expr);
7634 if (se->post.head)
7636 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
7637 gfc_add_modify (&se->pre, var, se->expr);
7638 gfc_add_block_to_block (&se->pre, &se->post);
7639 se->expr = var;
7641 return;
7644 if (expr->expr_type == EXPR_FUNCTION
7645 && ((expr->value.function.esym
7646 && expr->value.function.esym->result->attr.pointer
7647 && !expr->value.function.esym->result->attr.dimension)
7648 || (!expr->value.function.esym && !expr->ref
7649 && expr->symtree->n.sym->attr.pointer
7650 && !expr->symtree->n.sym->attr.dimension)))
7652 se->want_pointer = 1;
7653 gfc_conv_expr (se, expr);
7654 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
7655 gfc_add_modify (&se->pre, var, se->expr);
7656 se->expr = var;
7657 return;
7660 gfc_conv_expr (se, expr);
7662 /* Create a temporary var to hold the value. */
7663 if (TREE_CONSTANT (se->expr))
7665 tree tmp = se->expr;
7666 STRIP_TYPE_NOPS (tmp);
7667 var = build_decl (input_location,
7668 CONST_DECL, NULL, TREE_TYPE (tmp));
7669 DECL_INITIAL (var) = tmp;
7670 TREE_STATIC (var) = 1;
7671 pushdecl (var);
7673 else
7675 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
7676 gfc_add_modify (&se->pre, var, se->expr);
7678 gfc_add_block_to_block (&se->pre, &se->post);
7680 /* Take the address of that value. */
7681 se->expr = gfc_build_addr_expr (NULL_TREE, var);
7685 tree
7686 gfc_trans_pointer_assign (gfc_code * code)
7688 return gfc_trans_pointer_assignment (code->expr1, code->expr2);
7692 /* Generate code for a pointer assignment. */
7694 tree
7695 gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
7697 gfc_expr *expr1_vptr = NULL;
7698 gfc_se lse;
7699 gfc_se rse;
7700 stmtblock_t block;
7701 tree desc;
7702 tree tmp;
7703 tree decl;
7704 bool scalar;
7705 gfc_ss *ss;
7707 gfc_start_block (&block);
7709 gfc_init_se (&lse, NULL);
7711 /* Check whether the expression is a scalar or not; we cannot use
7712 expr1->rank as it can be nonzero for proc pointers. */
7713 ss = gfc_walk_expr (expr1);
7714 scalar = ss == gfc_ss_terminator;
7715 if (!scalar)
7716 gfc_free_ss_chain (ss);
7718 if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS
7719 && expr2->expr_type != EXPR_FUNCTION)
7721 gfc_add_data_component (expr2);
7722 /* The following is required as gfc_add_data_component doesn't
7723 update ts.type if there is a tailing REF_ARRAY. */
7724 expr2->ts.type = BT_DERIVED;
7727 if (scalar)
7729 /* Scalar pointers. */
7730 lse.want_pointer = 1;
7731 gfc_conv_expr (&lse, expr1);
7732 gfc_init_se (&rse, NULL);
7733 rse.want_pointer = 1;
7734 gfc_conv_expr (&rse, expr2);
7736 if (expr1->symtree->n.sym->attr.proc_pointer
7737 && expr1->symtree->n.sym->attr.dummy)
7738 lse.expr = build_fold_indirect_ref_loc (input_location,
7739 lse.expr);
7741 if (expr2->symtree && expr2->symtree->n.sym->attr.proc_pointer
7742 && expr2->symtree->n.sym->attr.dummy)
7743 rse.expr = build_fold_indirect_ref_loc (input_location,
7744 rse.expr);
7746 gfc_add_block_to_block (&block, &lse.pre);
7747 gfc_add_block_to_block (&block, &rse.pre);
7749 /* For string assignments to unlimited polymorphic pointers add an
7750 assignment of the string_length to the _len component of the
7751 pointer. */
7752 if ((expr1->ts.type == BT_CLASS || expr1->ts.type == BT_DERIVED)
7753 && expr1->ts.u.derived->attr.unlimited_polymorphic
7754 && (expr2->ts.type == BT_CHARACTER ||
7755 ((expr2->ts.type == BT_DERIVED || expr2->ts.type == BT_CLASS)
7756 && expr2->ts.u.derived->attr.unlimited_polymorphic)))
7758 gfc_expr *len_comp;
7759 gfc_se se;
7760 len_comp = gfc_get_len_component (expr1);
7761 gfc_init_se (&se, NULL);
7762 gfc_conv_expr (&se, len_comp);
7764 /* ptr % _len = len (str) */
7765 gfc_add_modify (&block, se.expr, rse.string_length);
7766 lse.string_length = se.expr;
7767 gfc_free_expr (len_comp);
7770 /* Check character lengths if character expression. The test is only
7771 really added if -fbounds-check is enabled. Exclude deferred
7772 character length lefthand sides. */
7773 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL
7774 && !expr1->ts.deferred
7775 && !expr1->symtree->n.sym->attr.proc_pointer
7776 && !gfc_is_proc_ptr_comp (expr1))
7778 gcc_assert (expr2->ts.type == BT_CHARACTER);
7779 gcc_assert (lse.string_length && rse.string_length);
7780 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
7781 lse.string_length, rse.string_length,
7782 &block);
7785 /* The assignment to an deferred character length sets the string
7786 length to that of the rhs. */
7787 if (expr1->ts.deferred)
7789 if (expr2->expr_type != EXPR_NULL && lse.string_length != NULL)
7790 gfc_add_modify (&block, lse.string_length, rse.string_length);
7791 else if (lse.string_length != NULL)
7792 gfc_add_modify (&block, lse.string_length,
7793 build_int_cst (gfc_charlen_type_node, 0));
7796 if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS)
7797 rse.expr = gfc_class_data_get (rse.expr);
7799 gfc_add_modify (&block, lse.expr,
7800 fold_convert (TREE_TYPE (lse.expr), rse.expr));
7802 gfc_add_block_to_block (&block, &rse.post);
7803 gfc_add_block_to_block (&block, &lse.post);
7805 else
7807 gfc_ref* remap;
7808 bool rank_remap;
7809 tree strlen_lhs;
7810 tree strlen_rhs = NULL_TREE;
7812 /* Array pointer. Find the last reference on the LHS and if it is an
7813 array section ref, we're dealing with bounds remapping. In this case,
7814 set it to AR_FULL so that gfc_conv_expr_descriptor does
7815 not see it and process the bounds remapping afterwards explicitly. */
7816 for (remap = expr1->ref; remap; remap = remap->next)
7817 if (!remap->next && remap->type == REF_ARRAY
7818 && remap->u.ar.type == AR_SECTION)
7819 break;
7820 rank_remap = (remap && remap->u.ar.end[0]);
7822 gfc_init_se (&lse, NULL);
7823 if (remap)
7824 lse.descriptor_only = 1;
7825 if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS
7826 && expr1->ts.type == BT_CLASS)
7827 expr1_vptr = gfc_copy_expr (expr1);
7828 gfc_conv_expr_descriptor (&lse, expr1);
7829 strlen_lhs = lse.string_length;
7830 desc = lse.expr;
7832 if (expr2->expr_type == EXPR_NULL)
7834 /* Just set the data pointer to null. */
7835 gfc_conv_descriptor_data_set (&lse.pre, lse.expr, null_pointer_node);
7837 else if (rank_remap)
7839 /* If we are rank-remapping, just get the RHS's descriptor and
7840 process this later on. */
7841 gfc_init_se (&rse, NULL);
7842 rse.direct_byref = 1;
7843 rse.byref_noassign = 1;
7845 if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
7847 gfc_conv_function_expr (&rse, expr2);
7849 if (expr1->ts.type != BT_CLASS)
7850 rse.expr = gfc_class_data_get (rse.expr);
7851 else
7853 gfc_add_block_to_block (&block, &rse.pre);
7854 tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
7855 gfc_add_modify (&lse.pre, tmp, rse.expr);
7857 gfc_add_vptr_component (expr1_vptr);
7858 gfc_init_se (&rse, NULL);
7859 rse.want_pointer = 1;
7860 gfc_conv_expr (&rse, expr1_vptr);
7861 gfc_add_modify (&lse.pre, rse.expr,
7862 fold_convert (TREE_TYPE (rse.expr),
7863 gfc_class_vptr_get (tmp)));
7864 rse.expr = gfc_class_data_get (tmp);
7867 else if (expr2->expr_type == EXPR_FUNCTION)
7869 tree bound[GFC_MAX_DIMENSIONS];
7870 int i;
7872 for (i = 0; i < expr2->rank; i++)
7873 bound[i] = NULL_TREE;
7874 tmp = gfc_typenode_for_spec (&expr2->ts);
7875 tmp = gfc_get_array_type_bounds (tmp, expr2->rank, 0,
7876 bound, bound, 0,
7877 GFC_ARRAY_POINTER_CONT, false);
7878 tmp = gfc_create_var (tmp, "ptrtemp");
7879 lse.descriptor_only = 0;
7880 lse.expr = tmp;
7881 lse.direct_byref = 1;
7882 gfc_conv_expr_descriptor (&lse, expr2);
7883 strlen_rhs = lse.string_length;
7884 rse.expr = tmp;
7886 else
7888 gfc_conv_expr_descriptor (&rse, expr2);
7889 strlen_rhs = rse.string_length;
7892 else if (expr2->expr_type == EXPR_VARIABLE)
7894 /* Assign directly to the LHS's descriptor. */
7895 lse.descriptor_only = 0;
7896 lse.direct_byref = 1;
7897 gfc_conv_expr_descriptor (&lse, expr2);
7898 strlen_rhs = lse.string_length;
7900 /* If this is a subreference array pointer assignment, use the rhs
7901 descriptor element size for the lhs span. */
7902 if (expr1->symtree->n.sym->attr.subref_array_pointer)
7904 decl = expr1->symtree->n.sym->backend_decl;
7905 gfc_init_se (&rse, NULL);
7906 rse.descriptor_only = 1;
7907 gfc_conv_expr (&rse, expr2);
7908 tmp = gfc_get_element_type (TREE_TYPE (rse.expr));
7909 tmp = fold_convert (gfc_array_index_type, size_in_bytes (tmp));
7910 if (!INTEGER_CST_P (tmp))
7911 gfc_add_block_to_block (&lse.post, &rse.pre);
7912 gfc_add_modify (&lse.post, GFC_DECL_SPAN(decl), tmp);
7915 else if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
7917 gfc_init_se (&rse, NULL);
7918 rse.want_pointer = 1;
7919 gfc_conv_function_expr (&rse, expr2);
7920 if (expr1->ts.type != BT_CLASS)
7922 rse.expr = gfc_class_data_get (rse.expr);
7923 gfc_add_modify (&lse.pre, desc, rse.expr);
7925 else
7927 gfc_add_block_to_block (&block, &rse.pre);
7928 tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
7929 gfc_add_modify (&lse.pre, tmp, rse.expr);
7931 gfc_add_vptr_component (expr1_vptr);
7932 gfc_init_se (&rse, NULL);
7933 rse.want_pointer = 1;
7934 gfc_conv_expr (&rse, expr1_vptr);
7935 gfc_add_modify (&lse.pre, rse.expr,
7936 fold_convert (TREE_TYPE (rse.expr),
7937 gfc_class_vptr_get (tmp)));
7938 rse.expr = gfc_class_data_get (tmp);
7939 gfc_add_modify (&lse.pre, desc, rse.expr);
7942 else
7944 /* Assign to a temporary descriptor and then copy that
7945 temporary to the pointer. */
7946 tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
7947 lse.descriptor_only = 0;
7948 lse.expr = tmp;
7949 lse.direct_byref = 1;
7950 gfc_conv_expr_descriptor (&lse, expr2);
7951 strlen_rhs = lse.string_length;
7952 gfc_add_modify (&lse.pre, desc, tmp);
7955 if (expr1_vptr)
7956 gfc_free_expr (expr1_vptr);
7958 gfc_add_block_to_block (&block, &lse.pre);
7959 if (rank_remap)
7960 gfc_add_block_to_block (&block, &rse.pre);
7962 /* If we do bounds remapping, update LHS descriptor accordingly. */
7963 if (remap)
7965 int dim;
7966 gcc_assert (remap->u.ar.dimen == expr1->rank);
7968 if (rank_remap)
7970 /* Do rank remapping. We already have the RHS's descriptor
7971 converted in rse and now have to build the correct LHS
7972 descriptor for it. */
7974 tree dtype, data;
7975 tree offs, stride;
7976 tree lbound, ubound;
7978 /* Set dtype. */
7979 dtype = gfc_conv_descriptor_dtype (desc);
7980 tmp = gfc_get_dtype (TREE_TYPE (desc));
7981 gfc_add_modify (&block, dtype, tmp);
7983 /* Copy data pointer. */
7984 data = gfc_conv_descriptor_data_get (rse.expr);
7985 gfc_conv_descriptor_data_set (&block, desc, data);
7987 /* Copy offset but adjust it such that it would correspond
7988 to a lbound of zero. */
7989 offs = gfc_conv_descriptor_offset_get (rse.expr);
7990 for (dim = 0; dim < expr2->rank; ++dim)
7992 stride = gfc_conv_descriptor_stride_get (rse.expr,
7993 gfc_rank_cst[dim]);
7994 lbound = gfc_conv_descriptor_lbound_get (rse.expr,
7995 gfc_rank_cst[dim]);
7996 tmp = fold_build2_loc (input_location, MULT_EXPR,
7997 gfc_array_index_type, stride, lbound);
7998 offs = fold_build2_loc (input_location, PLUS_EXPR,
7999 gfc_array_index_type, offs, tmp);
8001 gfc_conv_descriptor_offset_set (&block, desc, offs);
8003 /* Set the bounds as declared for the LHS and calculate strides as
8004 well as another offset update accordingly. */
8005 stride = gfc_conv_descriptor_stride_get (rse.expr,
8006 gfc_rank_cst[0]);
8007 for (dim = 0; dim < expr1->rank; ++dim)
8009 gfc_se lower_se;
8010 gfc_se upper_se;
8012 gcc_assert (remap->u.ar.start[dim] && remap->u.ar.end[dim]);
8014 /* Convert declared bounds. */
8015 gfc_init_se (&lower_se, NULL);
8016 gfc_init_se (&upper_se, NULL);
8017 gfc_conv_expr (&lower_se, remap->u.ar.start[dim]);
8018 gfc_conv_expr (&upper_se, remap->u.ar.end[dim]);
8020 gfc_add_block_to_block (&block, &lower_se.pre);
8021 gfc_add_block_to_block (&block, &upper_se.pre);
8023 lbound = fold_convert (gfc_array_index_type, lower_se.expr);
8024 ubound = fold_convert (gfc_array_index_type, upper_se.expr);
8026 lbound = gfc_evaluate_now (lbound, &block);
8027 ubound = gfc_evaluate_now (ubound, &block);
8029 gfc_add_block_to_block (&block, &lower_se.post);
8030 gfc_add_block_to_block (&block, &upper_se.post);
8032 /* Set bounds in descriptor. */
8033 gfc_conv_descriptor_lbound_set (&block, desc,
8034 gfc_rank_cst[dim], lbound);
8035 gfc_conv_descriptor_ubound_set (&block, desc,
8036 gfc_rank_cst[dim], ubound);
8038 /* Set stride. */
8039 stride = gfc_evaluate_now (stride, &block);
8040 gfc_conv_descriptor_stride_set (&block, desc,
8041 gfc_rank_cst[dim], stride);
8043 /* Update offset. */
8044 offs = gfc_conv_descriptor_offset_get (desc);
8045 tmp = fold_build2_loc (input_location, MULT_EXPR,
8046 gfc_array_index_type, lbound, stride);
8047 offs = fold_build2_loc (input_location, MINUS_EXPR,
8048 gfc_array_index_type, offs, tmp);
8049 offs = gfc_evaluate_now (offs, &block);
8050 gfc_conv_descriptor_offset_set (&block, desc, offs);
8052 /* Update stride. */
8053 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
8054 stride = fold_build2_loc (input_location, MULT_EXPR,
8055 gfc_array_index_type, stride, tmp);
8058 else
8060 /* Bounds remapping. Just shift the lower bounds. */
8062 gcc_assert (expr1->rank == expr2->rank);
8064 for (dim = 0; dim < remap->u.ar.dimen; ++dim)
8066 gfc_se lbound_se;
8068 gcc_assert (remap->u.ar.start[dim]);
8069 gcc_assert (!remap->u.ar.end[dim]);
8070 gfc_init_se (&lbound_se, NULL);
8071 gfc_conv_expr (&lbound_se, remap->u.ar.start[dim]);
8073 gfc_add_block_to_block (&block, &lbound_se.pre);
8074 gfc_conv_shift_descriptor_lbound (&block, desc,
8075 dim, lbound_se.expr);
8076 gfc_add_block_to_block (&block, &lbound_se.post);
8081 /* Check string lengths if applicable. The check is only really added
8082 to the output code if -fbounds-check is enabled. */
8083 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
8085 gcc_assert (expr2->ts.type == BT_CHARACTER);
8086 gcc_assert (strlen_lhs && strlen_rhs);
8087 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
8088 strlen_lhs, strlen_rhs, &block);
8091 /* If rank remapping was done, check with -fcheck=bounds that
8092 the target is at least as large as the pointer. */
8093 if (rank_remap && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
8095 tree lsize, rsize;
8096 tree fault;
8097 const char* msg;
8099 lsize = gfc_conv_descriptor_size (lse.expr, expr1->rank);
8100 rsize = gfc_conv_descriptor_size (rse.expr, expr2->rank);
8102 lsize = gfc_evaluate_now (lsize, &block);
8103 rsize = gfc_evaluate_now (rsize, &block);
8104 fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
8105 rsize, lsize);
8107 msg = _("Target of rank remapping is too small (%ld < %ld)");
8108 gfc_trans_runtime_check (true, false, fault, &block, &expr2->where,
8109 msg, rsize, lsize);
8112 gfc_add_block_to_block (&block, &lse.post);
8113 if (rank_remap)
8114 gfc_add_block_to_block (&block, &rse.post);
8117 return gfc_finish_block (&block);
8121 /* Makes sure se is suitable for passing as a function string parameter. */
8122 /* TODO: Need to check all callers of this function. It may be abused. */
8124 void
8125 gfc_conv_string_parameter (gfc_se * se)
8127 tree type;
8129 if (TREE_CODE (se->expr) == STRING_CST)
8131 type = TREE_TYPE (TREE_TYPE (se->expr));
8132 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
8133 return;
8136 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
8138 if (TREE_CODE (se->expr) != INDIRECT_REF)
8140 type = TREE_TYPE (se->expr);
8141 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
8143 else
8145 type = gfc_get_character_type_len (gfc_default_character_kind,
8146 se->string_length);
8147 type = build_pointer_type (type);
8148 se->expr = gfc_build_addr_expr (type, se->expr);
8152 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
8156 /* Generate code for assignment of scalar variables. Includes character
8157 strings and derived types with allocatable components.
8158 If you know that the LHS has no allocations, set dealloc to false.
8160 DEEP_COPY has no effect if the typespec TS is not a derived type with
8161 allocatable components. Otherwise, if it is set, an explicit copy of each
8162 allocatable component is made. This is necessary as a simple copy of the
8163 whole object would copy array descriptors as is, so that the lhs's
8164 allocatable components would point to the rhs's after the assignment.
8165 Typically, setting DEEP_COPY is necessary if the rhs is a variable, and not
8166 necessary if the rhs is a non-pointer function, as the allocatable components
8167 are not accessible by other means than the function's result after the
8168 function has returned. It is even more subtle when temporaries are involved,
8169 as the two following examples show:
8170 1. When we evaluate an array constructor, a temporary is created. Thus
8171 there is theoretically no alias possible. However, no deep copy is
8172 made for this temporary, so that if the constructor is made of one or
8173 more variable with allocatable components, those components still point
8174 to the variable's: DEEP_COPY should be set for the assignment from the
8175 temporary to the lhs in that case.
8176 2. When assigning a scalar to an array, we evaluate the scalar value out
8177 of the loop, store it into a temporary variable, and assign from that.
8178 In that case, deep copying when assigning to the temporary would be a
8179 waste of resources; however deep copies should happen when assigning from
8180 the temporary to each array element: again DEEP_COPY should be set for
8181 the assignment from the temporary to the lhs. */
8183 tree
8184 gfc_trans_scalar_assign (gfc_se * lse, gfc_se * rse, gfc_typespec ts,
8185 bool deep_copy, bool dealloc)
8187 stmtblock_t block;
8188 tree tmp;
8189 tree cond;
8191 gfc_init_block (&block);
8193 if (ts.type == BT_CHARACTER)
8195 tree rlen = NULL;
8196 tree llen = NULL;
8198 if (lse->string_length != NULL_TREE)
8200 gfc_conv_string_parameter (lse);
8201 gfc_add_block_to_block (&block, &lse->pre);
8202 llen = lse->string_length;
8205 if (rse->string_length != NULL_TREE)
8207 gcc_assert (rse->string_length != NULL_TREE);
8208 gfc_conv_string_parameter (rse);
8209 gfc_add_block_to_block (&block, &rse->pre);
8210 rlen = rse->string_length;
8213 gfc_trans_string_copy (&block, llen, lse->expr, ts.kind, rlen,
8214 rse->expr, ts.kind);
8216 else if (ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp)
8218 tree tmp_var = NULL_TREE;
8219 cond = NULL_TREE;
8221 /* Are the rhs and the lhs the same? */
8222 if (deep_copy)
8224 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
8225 gfc_build_addr_expr (NULL_TREE, lse->expr),
8226 gfc_build_addr_expr (NULL_TREE, rse->expr));
8227 cond = gfc_evaluate_now (cond, &lse->pre);
8230 /* Deallocate the lhs allocated components as long as it is not
8231 the same as the rhs. This must be done following the assignment
8232 to prevent deallocating data that could be used in the rhs
8233 expression. */
8234 if (dealloc)
8236 tmp_var = gfc_evaluate_now (lse->expr, &lse->pre);
8237 tmp = gfc_deallocate_alloc_comp_no_caf (ts.u.derived, tmp_var, 0);
8238 if (deep_copy)
8239 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
8240 tmp);
8241 gfc_add_expr_to_block (&lse->post, tmp);
8244 gfc_add_block_to_block (&block, &rse->pre);
8245 gfc_add_block_to_block (&block, &lse->pre);
8247 gfc_add_modify (&block, lse->expr,
8248 fold_convert (TREE_TYPE (lse->expr), rse->expr));
8250 /* Restore pointer address of coarray components. */
8251 if (ts.u.derived->attr.coarray_comp && deep_copy && tmp_var != NULL_TREE)
8253 tmp = gfc_reassign_alloc_comp_caf (ts.u.derived, tmp_var, lse->expr);
8254 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
8255 tmp);
8256 gfc_add_expr_to_block (&block, tmp);
8259 /* Do a deep copy if the rhs is a variable, if it is not the
8260 same as the lhs. */
8261 if (deep_copy)
8263 tmp = gfc_copy_alloc_comp (ts.u.derived, rse->expr, lse->expr, 0);
8264 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
8265 tmp);
8266 gfc_add_expr_to_block (&block, tmp);
8269 else if (ts.type == BT_DERIVED || ts.type == BT_CLASS)
8271 gfc_add_block_to_block (&block, &lse->pre);
8272 gfc_add_block_to_block (&block, &rse->pre);
8273 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
8274 TREE_TYPE (lse->expr), rse->expr);
8275 gfc_add_modify (&block, lse->expr, tmp);
8277 else
8279 gfc_add_block_to_block (&block, &lse->pre);
8280 gfc_add_block_to_block (&block, &rse->pre);
8282 gfc_add_modify (&block, lse->expr,
8283 fold_convert (TREE_TYPE (lse->expr), rse->expr));
8286 gfc_add_block_to_block (&block, &lse->post);
8287 gfc_add_block_to_block (&block, &rse->post);
8289 return gfc_finish_block (&block);
8293 /* There are quite a lot of restrictions on the optimisation in using an
8294 array function assign without a temporary. */
8296 static bool
8297 arrayfunc_assign_needs_temporary (gfc_expr * expr1, gfc_expr * expr2)
8299 gfc_ref * ref;
8300 bool seen_array_ref;
8301 bool c = false;
8302 gfc_symbol *sym = expr1->symtree->n.sym;
8304 /* Play it safe with class functions assigned to a derived type. */
8305 if (gfc_is_alloc_class_array_function (expr2)
8306 && expr1->ts.type == BT_DERIVED)
8307 return true;
8309 /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
8310 if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
8311 return true;
8313 /* Elemental functions are scalarized so that they don't need a
8314 temporary in gfc_trans_assignment_1, so return a true. Otherwise,
8315 they would need special treatment in gfc_trans_arrayfunc_assign. */
8316 if (expr2->value.function.esym != NULL
8317 && expr2->value.function.esym->attr.elemental)
8318 return true;
8320 /* Need a temporary if rhs is not FULL or a contiguous section. */
8321 if (expr1->ref && !(gfc_full_array_ref_p (expr1->ref, &c) || c))
8322 return true;
8324 /* Need a temporary if EXPR1 can't be expressed as a descriptor. */
8325 if (gfc_ref_needs_temporary_p (expr1->ref))
8326 return true;
8328 /* Functions returning pointers or allocatables need temporaries. */
8329 c = expr2->value.function.esym
8330 ? (expr2->value.function.esym->attr.pointer
8331 || expr2->value.function.esym->attr.allocatable)
8332 : (expr2->symtree->n.sym->attr.pointer
8333 || expr2->symtree->n.sym->attr.allocatable);
8334 if (c)
8335 return true;
8337 /* Character array functions need temporaries unless the
8338 character lengths are the same. */
8339 if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
8341 if (expr1->ts.u.cl->length == NULL
8342 || expr1->ts.u.cl->length->expr_type != EXPR_CONSTANT)
8343 return true;
8345 if (expr2->ts.u.cl->length == NULL
8346 || expr2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
8347 return true;
8349 if (mpz_cmp (expr1->ts.u.cl->length->value.integer,
8350 expr2->ts.u.cl->length->value.integer) != 0)
8351 return true;
8354 /* Check that no LHS component references appear during an array
8355 reference. This is needed because we do not have the means to
8356 span any arbitrary stride with an array descriptor. This check
8357 is not needed for the rhs because the function result has to be
8358 a complete type. */
8359 seen_array_ref = false;
8360 for (ref = expr1->ref; ref; ref = ref->next)
8362 if (ref->type == REF_ARRAY)
8363 seen_array_ref= true;
8364 else if (ref->type == REF_COMPONENT && seen_array_ref)
8365 return true;
8368 /* Check for a dependency. */
8369 if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
8370 expr2->value.function.esym,
8371 expr2->value.function.actual,
8372 NOT_ELEMENTAL))
8373 return true;
8375 /* If we have reached here with an intrinsic function, we do not
8376 need a temporary except in the particular case that reallocation
8377 on assignment is active and the lhs is allocatable and a target. */
8378 if (expr2->value.function.isym)
8379 return (flag_realloc_lhs && sym->attr.allocatable && sym->attr.target);
8381 /* If the LHS is a dummy, we need a temporary if it is not
8382 INTENT(OUT). */
8383 if (sym->attr.dummy && sym->attr.intent != INTENT_OUT)
8384 return true;
8386 /* If the lhs has been host_associated, is in common, a pointer or is
8387 a target and the function is not using a RESULT variable, aliasing
8388 can occur and a temporary is needed. */
8389 if ((sym->attr.host_assoc
8390 || sym->attr.in_common
8391 || sym->attr.pointer
8392 || sym->attr.cray_pointee
8393 || sym->attr.target)
8394 && expr2->symtree != NULL
8395 && expr2->symtree->n.sym == expr2->symtree->n.sym->result)
8396 return true;
8398 /* A PURE function can unconditionally be called without a temporary. */
8399 if (expr2->value.function.esym != NULL
8400 && expr2->value.function.esym->attr.pure)
8401 return false;
8403 /* Implicit_pure functions are those which could legally be declared
8404 to be PURE. */
8405 if (expr2->value.function.esym != NULL
8406 && expr2->value.function.esym->attr.implicit_pure)
8407 return false;
8409 if (!sym->attr.use_assoc
8410 && !sym->attr.in_common
8411 && !sym->attr.pointer
8412 && !sym->attr.target
8413 && !sym->attr.cray_pointee
8414 && expr2->value.function.esym)
8416 /* A temporary is not needed if the function is not contained and
8417 the variable is local or host associated and not a pointer or
8418 a target. */
8419 if (!expr2->value.function.esym->attr.contained)
8420 return false;
8422 /* A temporary is not needed if the lhs has never been host
8423 associated and the procedure is contained. */
8424 else if (!sym->attr.host_assoc)
8425 return false;
8427 /* A temporary is not needed if the variable is local and not
8428 a pointer, a target or a result. */
8429 if (sym->ns->parent
8430 && expr2->value.function.esym->ns == sym->ns->parent)
8431 return false;
8434 /* Default to temporary use. */
8435 return true;
8439 /* Provide the loop info so that the lhs descriptor can be built for
8440 reallocatable assignments from extrinsic function calls. */
8442 static void
8443 realloc_lhs_loop_for_fcn_call (gfc_se *se, locus *where, gfc_ss **ss,
8444 gfc_loopinfo *loop)
8446 /* Signal that the function call should not be made by
8447 gfc_conv_loop_setup. */
8448 se->ss->is_alloc_lhs = 1;
8449 gfc_init_loopinfo (loop);
8450 gfc_add_ss_to_loop (loop, *ss);
8451 gfc_add_ss_to_loop (loop, se->ss);
8452 gfc_conv_ss_startstride (loop);
8453 gfc_conv_loop_setup (loop, where);
8454 gfc_copy_loopinfo_to_se (se, loop);
8455 gfc_add_block_to_block (&se->pre, &loop->pre);
8456 gfc_add_block_to_block (&se->pre, &loop->post);
8457 se->ss->is_alloc_lhs = 0;
8461 /* For assignment to a reallocatable lhs from intrinsic functions,
8462 replace the se.expr (ie. the result) with a temporary descriptor.
8463 Null the data field so that the library allocates space for the
8464 result. Free the data of the original descriptor after the function,
8465 in case it appears in an argument expression and transfer the
8466 result to the original descriptor. */
8468 static void
8469 fcncall_realloc_result (gfc_se *se, int rank)
8471 tree desc;
8472 tree res_desc;
8473 tree tmp;
8474 tree offset;
8475 tree zero_cond;
8476 int n;
8478 /* Use the allocation done by the library. Substitute the lhs
8479 descriptor with a copy, whose data field is nulled.*/
8480 desc = build_fold_indirect_ref_loc (input_location, se->expr);
8481 if (POINTER_TYPE_P (TREE_TYPE (desc)))
8482 desc = build_fold_indirect_ref_loc (input_location, desc);
8484 /* Unallocated, the descriptor does not have a dtype. */
8485 tmp = gfc_conv_descriptor_dtype (desc);
8486 gfc_add_modify (&se->pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
8488 res_desc = gfc_evaluate_now (desc, &se->pre);
8489 gfc_conv_descriptor_data_set (&se->pre, res_desc, null_pointer_node);
8490 se->expr = gfc_build_addr_expr (NULL_TREE, res_desc);
8492 /* Free the lhs after the function call and copy the result data to
8493 the lhs descriptor. */
8494 tmp = gfc_conv_descriptor_data_get (desc);
8495 zero_cond = fold_build2_loc (input_location, EQ_EXPR,
8496 boolean_type_node, tmp,
8497 build_int_cst (TREE_TYPE (tmp), 0));
8498 zero_cond = gfc_evaluate_now (zero_cond, &se->post);
8499 tmp = gfc_call_free (tmp);
8500 gfc_add_expr_to_block (&se->post, tmp);
8502 tmp = gfc_conv_descriptor_data_get (res_desc);
8503 gfc_conv_descriptor_data_set (&se->post, desc, tmp);
8505 /* Check that the shapes are the same between lhs and expression. */
8506 for (n = 0 ; n < rank; n++)
8508 tree tmp1;
8509 tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
8510 tmp1 = gfc_conv_descriptor_lbound_get (res_desc, gfc_rank_cst[n]);
8511 tmp = fold_build2_loc (input_location, MINUS_EXPR,
8512 gfc_array_index_type, tmp, tmp1);
8513 tmp1 = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
8514 tmp = fold_build2_loc (input_location, MINUS_EXPR,
8515 gfc_array_index_type, tmp, tmp1);
8516 tmp1 = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
8517 tmp = fold_build2_loc (input_location, PLUS_EXPR,
8518 gfc_array_index_type, tmp, tmp1);
8519 tmp = fold_build2_loc (input_location, NE_EXPR,
8520 boolean_type_node, tmp,
8521 gfc_index_zero_node);
8522 tmp = gfc_evaluate_now (tmp, &se->post);
8523 zero_cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
8524 boolean_type_node, tmp,
8525 zero_cond);
8528 /* 'zero_cond' being true is equal to lhs not being allocated or the
8529 shapes being different. */
8530 zero_cond = gfc_evaluate_now (zero_cond, &se->post);
8532 /* Now reset the bounds returned from the function call to bounds based
8533 on the lhs lbounds, except where the lhs is not allocated or the shapes
8534 of 'variable and 'expr' are different. Set the offset accordingly. */
8535 offset = gfc_index_zero_node;
8536 for (n = 0 ; n < rank; n++)
8538 tree lbound;
8540 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
8541 lbound = fold_build3_loc (input_location, COND_EXPR,
8542 gfc_array_index_type, zero_cond,
8543 gfc_index_one_node, lbound);
8544 lbound = gfc_evaluate_now (lbound, &se->post);
8546 tmp = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
8547 tmp = fold_build2_loc (input_location, PLUS_EXPR,
8548 gfc_array_index_type, tmp, lbound);
8549 gfc_conv_descriptor_lbound_set (&se->post, desc,
8550 gfc_rank_cst[n], lbound);
8551 gfc_conv_descriptor_ubound_set (&se->post, desc,
8552 gfc_rank_cst[n], tmp);
8554 /* Set stride and accumulate the offset. */
8555 tmp = gfc_conv_descriptor_stride_get (res_desc, gfc_rank_cst[n]);
8556 gfc_conv_descriptor_stride_set (&se->post, desc,
8557 gfc_rank_cst[n], tmp);
8558 tmp = fold_build2_loc (input_location, MULT_EXPR,
8559 gfc_array_index_type, lbound, tmp);
8560 offset = fold_build2_loc (input_location, MINUS_EXPR,
8561 gfc_array_index_type, offset, tmp);
8562 offset = gfc_evaluate_now (offset, &se->post);
8565 gfc_conv_descriptor_offset_set (&se->post, desc, offset);
8570 /* Try to translate array(:) = func (...), where func is a transformational
8571 array function, without using a temporary. Returns NULL if this isn't the
8572 case. */
8574 static tree
8575 gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
8577 gfc_se se;
8578 gfc_ss *ss = NULL;
8579 gfc_component *comp = NULL;
8580 gfc_loopinfo loop;
8582 if (arrayfunc_assign_needs_temporary (expr1, expr2))
8583 return NULL;
8585 /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
8586 functions. */
8587 comp = gfc_get_proc_ptr_comp (expr2);
8588 gcc_assert (expr2->value.function.isym
8589 || (comp && comp->attr.dimension)
8590 || (!comp && gfc_return_by_reference (expr2->value.function.esym)
8591 && expr2->value.function.esym->result->attr.dimension));
8593 gfc_init_se (&se, NULL);
8594 gfc_start_block (&se.pre);
8595 se.want_pointer = 1;
8597 gfc_conv_array_parameter (&se, expr1, false, NULL, NULL, NULL);
8599 if (expr1->ts.type == BT_DERIVED
8600 && expr1->ts.u.derived->attr.alloc_comp)
8602 tree tmp;
8603 tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, se.expr,
8604 expr1->rank);
8605 gfc_add_expr_to_block (&se.pre, tmp);
8608 se.direct_byref = 1;
8609 se.ss = gfc_walk_expr (expr2);
8610 gcc_assert (se.ss != gfc_ss_terminator);
8612 /* Reallocate on assignment needs the loopinfo for extrinsic functions.
8613 This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
8614 Clearly, this cannot be done for an allocatable function result, since
8615 the shape of the result is unknown and, in any case, the function must
8616 correctly take care of the reallocation internally. For intrinsic
8617 calls, the array data is freed and the library takes care of allocation.
8618 TODO: Add logic of trans-array.c: gfc_alloc_allocatable_for_assignment
8619 to the library. */
8620 if (flag_realloc_lhs
8621 && gfc_is_reallocatable_lhs (expr1)
8622 && !gfc_expr_attr (expr1).codimension
8623 && !gfc_is_coindexed (expr1)
8624 && !(expr2->value.function.esym
8625 && expr2->value.function.esym->result->attr.allocatable))
8627 realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
8629 if (!expr2->value.function.isym)
8631 ss = gfc_walk_expr (expr1);
8632 gcc_assert (ss != gfc_ss_terminator);
8634 realloc_lhs_loop_for_fcn_call (&se, &expr1->where, &ss, &loop);
8635 ss->is_alloc_lhs = 1;
8637 else
8638 fcncall_realloc_result (&se, expr1->rank);
8641 gfc_conv_function_expr (&se, expr2);
8642 gfc_add_block_to_block (&se.pre, &se.post);
8644 if (ss)
8645 gfc_cleanup_loop (&loop);
8646 else
8647 gfc_free_ss_chain (se.ss);
8649 return gfc_finish_block (&se.pre);
8653 /* Try to efficiently translate array(:) = 0. Return NULL if this
8654 can't be done. */
8656 static tree
8657 gfc_trans_zero_assign (gfc_expr * expr)
8659 tree dest, len, type;
8660 tree tmp;
8661 gfc_symbol *sym;
8663 sym = expr->symtree->n.sym;
8664 dest = gfc_get_symbol_decl (sym);
8666 type = TREE_TYPE (dest);
8667 if (POINTER_TYPE_P (type))
8668 type = TREE_TYPE (type);
8669 if (!GFC_ARRAY_TYPE_P (type))
8670 return NULL_TREE;
8672 /* Determine the length of the array. */
8673 len = GFC_TYPE_ARRAY_SIZE (type);
8674 if (!len || TREE_CODE (len) != INTEGER_CST)
8675 return NULL_TREE;
8677 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
8678 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
8679 fold_convert (gfc_array_index_type, tmp));
8681 /* If we are zeroing a local array avoid taking its address by emitting
8682 a = {} instead. */
8683 if (!POINTER_TYPE_P (TREE_TYPE (dest)))
8684 return build2_loc (input_location, MODIFY_EXPR, void_type_node,
8685 dest, build_constructor (TREE_TYPE (dest),
8686 NULL));
8688 /* Convert arguments to the correct types. */
8689 dest = fold_convert (pvoid_type_node, dest);
8690 len = fold_convert (size_type_node, len);
8692 /* Construct call to __builtin_memset. */
8693 tmp = build_call_expr_loc (input_location,
8694 builtin_decl_explicit (BUILT_IN_MEMSET),
8695 3, dest, integer_zero_node, len);
8696 return fold_convert (void_type_node, tmp);
8700 /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
8701 that constructs the call to __builtin_memcpy. */
8703 tree
8704 gfc_build_memcpy_call (tree dst, tree src, tree len)
8706 tree tmp;
8708 /* Convert arguments to the correct types. */
8709 if (!POINTER_TYPE_P (TREE_TYPE (dst)))
8710 dst = gfc_build_addr_expr (pvoid_type_node, dst);
8711 else
8712 dst = fold_convert (pvoid_type_node, dst);
8714 if (!POINTER_TYPE_P (TREE_TYPE (src)))
8715 src = gfc_build_addr_expr (pvoid_type_node, src);
8716 else
8717 src = fold_convert (pvoid_type_node, src);
8719 len = fold_convert (size_type_node, len);
8721 /* Construct call to __builtin_memcpy. */
8722 tmp = build_call_expr_loc (input_location,
8723 builtin_decl_explicit (BUILT_IN_MEMCPY),
8724 3, dst, src, len);
8725 return fold_convert (void_type_node, tmp);
8729 /* Try to efficiently translate dst(:) = src(:). Return NULL if this
8730 can't be done. EXPR1 is the destination/lhs and EXPR2 is the
8731 source/rhs, both are gfc_full_array_ref_p which have been checked for
8732 dependencies. */
8734 static tree
8735 gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
8737 tree dst, dlen, dtype;
8738 tree src, slen, stype;
8739 tree tmp;
8741 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
8742 src = gfc_get_symbol_decl (expr2->symtree->n.sym);
8744 dtype = TREE_TYPE (dst);
8745 if (POINTER_TYPE_P (dtype))
8746 dtype = TREE_TYPE (dtype);
8747 stype = TREE_TYPE (src);
8748 if (POINTER_TYPE_P (stype))
8749 stype = TREE_TYPE (stype);
8751 if (!GFC_ARRAY_TYPE_P (dtype) || !GFC_ARRAY_TYPE_P (stype))
8752 return NULL_TREE;
8754 /* Determine the lengths of the arrays. */
8755 dlen = GFC_TYPE_ARRAY_SIZE (dtype);
8756 if (!dlen || TREE_CODE (dlen) != INTEGER_CST)
8757 return NULL_TREE;
8758 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
8759 dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8760 dlen, fold_convert (gfc_array_index_type, tmp));
8762 slen = GFC_TYPE_ARRAY_SIZE (stype);
8763 if (!slen || TREE_CODE (slen) != INTEGER_CST)
8764 return NULL_TREE;
8765 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (stype));
8766 slen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
8767 slen, fold_convert (gfc_array_index_type, tmp));
8769 /* Sanity check that they are the same. This should always be
8770 the case, as we should already have checked for conformance. */
8771 if (!tree_int_cst_equal (slen, dlen))
8772 return NULL_TREE;
8774 return gfc_build_memcpy_call (dst, src, dlen);
8778 /* Try to efficiently translate array(:) = (/ ... /). Return NULL if
8779 this can't be done. EXPR1 is the destination/lhs for which
8780 gfc_full_array_ref_p is true, and EXPR2 is the source/rhs. */
8782 static tree
8783 gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
8785 unsigned HOST_WIDE_INT nelem;
8786 tree dst, dtype;
8787 tree src, stype;
8788 tree len;
8789 tree tmp;
8791 nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
8792 if (nelem == 0)
8793 return NULL_TREE;
8795 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
8796 dtype = TREE_TYPE (dst);
8797 if (POINTER_TYPE_P (dtype))
8798 dtype = TREE_TYPE (dtype);
8799 if (!GFC_ARRAY_TYPE_P (dtype))
8800 return NULL_TREE;
8802 /* Determine the lengths of the array. */
8803 len = GFC_TYPE_ARRAY_SIZE (dtype);
8804 if (!len || TREE_CODE (len) != INTEGER_CST)
8805 return NULL_TREE;
8807 /* Confirm that the constructor is the same size. */
8808 if (compare_tree_int (len, nelem) != 0)
8809 return NULL_TREE;
8811 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
8812 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
8813 fold_convert (gfc_array_index_type, tmp));
8815 stype = gfc_typenode_for_spec (&expr2->ts);
8816 src = gfc_build_constant_array_constructor (expr2, stype);
8818 stype = TREE_TYPE (src);
8819 if (POINTER_TYPE_P (stype))
8820 stype = TREE_TYPE (stype);
8822 return gfc_build_memcpy_call (dst, src, len);
8826 /* Tells whether the expression is to be treated as a variable reference. */
8828 static bool
8829 expr_is_variable (gfc_expr *expr)
8831 gfc_expr *arg;
8832 gfc_component *comp;
8833 gfc_symbol *func_ifc;
8835 if (expr->expr_type == EXPR_VARIABLE)
8836 return true;
8838 arg = gfc_get_noncopying_intrinsic_argument (expr);
8839 if (arg)
8841 gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
8842 return expr_is_variable (arg);
8845 /* A data-pointer-returning function should be considered as a variable
8846 too. */
8847 if (expr->expr_type == EXPR_FUNCTION
8848 && expr->ref == NULL)
8850 if (expr->value.function.isym != NULL)
8851 return false;
8853 if (expr->value.function.esym != NULL)
8855 func_ifc = expr->value.function.esym;
8856 goto found_ifc;
8858 else
8860 gcc_assert (expr->symtree);
8861 func_ifc = expr->symtree->n.sym;
8862 goto found_ifc;
8865 gcc_unreachable ();
8868 comp = gfc_get_proc_ptr_comp (expr);
8869 if ((expr->expr_type == EXPR_PPC || expr->expr_type == EXPR_FUNCTION)
8870 && comp)
8872 func_ifc = comp->ts.interface;
8873 goto found_ifc;
8876 if (expr->expr_type == EXPR_COMPCALL)
8878 gcc_assert (!expr->value.compcall.tbp->is_generic);
8879 func_ifc = expr->value.compcall.tbp->u.specific->n.sym;
8880 goto found_ifc;
8883 return false;
8885 found_ifc:
8886 gcc_assert (func_ifc->attr.function
8887 && func_ifc->result != NULL);
8888 return func_ifc->result->attr.pointer;
8892 /* Is the lhs OK for automatic reallocation? */
8894 static bool
8895 is_scalar_reallocatable_lhs (gfc_expr *expr)
8897 gfc_ref * ref;
8899 /* An allocatable variable with no reference. */
8900 if (expr->symtree->n.sym->attr.allocatable
8901 && !expr->ref)
8902 return true;
8904 /* All that can be left are allocatable components. However, we do
8905 not check for allocatable components here because the expression
8906 could be an allocatable component of a pointer component. */
8907 if (expr->symtree->n.sym->ts.type != BT_DERIVED
8908 && expr->symtree->n.sym->ts.type != BT_CLASS)
8909 return false;
8911 /* Find an allocatable component ref last. */
8912 for (ref = expr->ref; ref; ref = ref->next)
8913 if (ref->type == REF_COMPONENT
8914 && !ref->next
8915 && ref->u.c.component->attr.allocatable)
8916 return true;
8918 return false;
8922 /* Allocate or reallocate scalar lhs, as necessary. */
8924 static void
8925 alloc_scalar_allocatable_for_assignment (stmtblock_t *block,
8926 tree string_length,
8927 gfc_expr *expr1,
8928 gfc_expr *expr2)
8931 tree cond;
8932 tree tmp;
8933 tree size;
8934 tree size_in_bytes;
8935 tree jump_label1;
8936 tree jump_label2;
8937 gfc_se lse;
8938 gfc_ref *ref;
8940 if (!expr1 || expr1->rank)
8941 return;
8943 if (!expr2 || expr2->rank)
8944 return;
8946 for (ref = expr1->ref; ref; ref = ref->next)
8947 if (ref->type == REF_SUBSTRING)
8948 return;
8950 realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
8952 /* Since this is a scalar lhs, we can afford to do this. That is,
8953 there is no risk of side effects being repeated. */
8954 gfc_init_se (&lse, NULL);
8955 lse.want_pointer = 1;
8956 gfc_conv_expr (&lse, expr1);
8958 jump_label1 = gfc_build_label_decl (NULL_TREE);
8959 jump_label2 = gfc_build_label_decl (NULL_TREE);
8961 /* Do the allocation if the lhs is NULL. Otherwise go to label 1. */
8962 tmp = build_int_cst (TREE_TYPE (lse.expr), 0);
8963 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
8964 lse.expr, tmp);
8965 tmp = build3_v (COND_EXPR, cond,
8966 build1_v (GOTO_EXPR, jump_label1),
8967 build_empty_stmt (input_location));
8968 gfc_add_expr_to_block (block, tmp);
8970 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
8972 /* Use the rhs string length and the lhs element size. */
8973 size = string_length;
8974 tmp = TREE_TYPE (gfc_typenode_for_spec (&expr1->ts));
8975 tmp = TYPE_SIZE_UNIT (tmp);
8976 size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
8977 TREE_TYPE (tmp), tmp,
8978 fold_convert (TREE_TYPE (tmp), size));
8980 else
8982 /* Otherwise use the length in bytes of the rhs. */
8983 size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr1->ts));
8984 size_in_bytes = size;
8987 size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
8988 size_in_bytes, size_one_node);
8990 if (expr1->ts.type == BT_DERIVED && expr1->ts.u.derived->attr.alloc_comp)
8992 tmp = build_call_expr_loc (input_location,
8993 builtin_decl_explicit (BUILT_IN_CALLOC),
8994 2, build_one_cst (size_type_node),
8995 size_in_bytes);
8996 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
8997 gfc_add_modify (block, lse.expr, tmp);
8999 else
9001 tmp = build_call_expr_loc (input_location,
9002 builtin_decl_explicit (BUILT_IN_MALLOC),
9003 1, size_in_bytes);
9004 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
9005 gfc_add_modify (block, lse.expr, tmp);
9008 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
9010 /* Deferred characters need checking for lhs and rhs string
9011 length. Other deferred parameter variables will have to
9012 come here too. */
9013 tmp = build1_v (GOTO_EXPR, jump_label2);
9014 gfc_add_expr_to_block (block, tmp);
9016 tmp = build1_v (LABEL_EXPR, jump_label1);
9017 gfc_add_expr_to_block (block, tmp);
9019 /* For a deferred length character, reallocate if lengths of lhs and
9020 rhs are different. */
9021 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
9023 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
9024 lse.string_length, size);
9025 /* Jump past the realloc if the lengths are the same. */
9026 tmp = build3_v (COND_EXPR, cond,
9027 build1_v (GOTO_EXPR, jump_label2),
9028 build_empty_stmt (input_location));
9029 gfc_add_expr_to_block (block, tmp);
9030 tmp = build_call_expr_loc (input_location,
9031 builtin_decl_explicit (BUILT_IN_REALLOC),
9032 2, fold_convert (pvoid_type_node, lse.expr),
9033 size_in_bytes);
9034 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
9035 gfc_add_modify (block, lse.expr, tmp);
9036 tmp = build1_v (LABEL_EXPR, jump_label2);
9037 gfc_add_expr_to_block (block, tmp);
9039 /* Update the lhs character length. */
9040 size = string_length;
9041 gfc_add_modify (block, lse.string_length, size);
9045 /* Check for assignments of the type
9047 a = a + 4
9049 to make sure we do not check for reallocation unneccessarily. */
9052 static bool
9053 is_runtime_conformable (gfc_expr *expr1, gfc_expr *expr2)
9055 gfc_actual_arglist *a;
9056 gfc_expr *e1, *e2;
9058 switch (expr2->expr_type)
9060 case EXPR_VARIABLE:
9061 return gfc_dep_compare_expr (expr1, expr2) == 0;
9063 case EXPR_FUNCTION:
9064 if (expr2->value.function.esym
9065 && expr2->value.function.esym->attr.elemental)
9067 for (a = expr2->value.function.actual; a != NULL; a = a->next)
9069 e1 = a->expr;
9070 if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
9071 return false;
9073 return true;
9075 else if (expr2->value.function.isym
9076 && expr2->value.function.isym->elemental)
9078 for (a = expr2->value.function.actual; a != NULL; a = a->next)
9080 e1 = a->expr;
9081 if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
9082 return false;
9084 return true;
9087 break;
9089 case EXPR_OP:
9090 switch (expr2->value.op.op)
9092 case INTRINSIC_NOT:
9093 case INTRINSIC_UPLUS:
9094 case INTRINSIC_UMINUS:
9095 case INTRINSIC_PARENTHESES:
9096 return is_runtime_conformable (expr1, expr2->value.op.op1);
9098 case INTRINSIC_PLUS:
9099 case INTRINSIC_MINUS:
9100 case INTRINSIC_TIMES:
9101 case INTRINSIC_DIVIDE:
9102 case INTRINSIC_POWER:
9103 case INTRINSIC_AND:
9104 case INTRINSIC_OR:
9105 case INTRINSIC_EQV:
9106 case INTRINSIC_NEQV:
9107 case INTRINSIC_EQ:
9108 case INTRINSIC_NE:
9109 case INTRINSIC_GT:
9110 case INTRINSIC_GE:
9111 case INTRINSIC_LT:
9112 case INTRINSIC_LE:
9113 case INTRINSIC_EQ_OS:
9114 case INTRINSIC_NE_OS:
9115 case INTRINSIC_GT_OS:
9116 case INTRINSIC_GE_OS:
9117 case INTRINSIC_LT_OS:
9118 case INTRINSIC_LE_OS:
9120 e1 = expr2->value.op.op1;
9121 e2 = expr2->value.op.op2;
9123 if (e1->rank == 0 && e2->rank > 0)
9124 return is_runtime_conformable (expr1, e2);
9125 else if (e1->rank > 0 && e2->rank == 0)
9126 return is_runtime_conformable (expr1, e1);
9127 else if (e1->rank > 0 && e2->rank > 0)
9128 return is_runtime_conformable (expr1, e1)
9129 && is_runtime_conformable (expr1, e2);
9130 break;
9132 default:
9133 break;
9137 break;
9139 default:
9140 break;
9142 return false;
9145 /* Subroutine of gfc_trans_assignment that actually scalarizes the
9146 assignment. EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
9147 init_flag indicates initialization expressions and dealloc that no
9148 deallocate prior assignment is needed (if in doubt, set true). */
9150 static tree
9151 gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
9152 bool dealloc)
9154 gfc_se lse;
9155 gfc_se rse;
9156 gfc_ss *lss;
9157 gfc_ss *lss_section;
9158 gfc_ss *rss;
9159 gfc_loopinfo loop;
9160 tree tmp;
9161 stmtblock_t block;
9162 stmtblock_t body;
9163 bool l_is_temp;
9164 bool scalar_to_array;
9165 tree string_length;
9166 int n;
9167 bool maybe_workshare = false;
9169 /* Assignment of the form lhs = rhs. */
9170 gfc_start_block (&block);
9172 gfc_init_se (&lse, NULL);
9173 gfc_init_se (&rse, NULL);
9175 /* Walk the lhs. */
9176 lss = gfc_walk_expr (expr1);
9177 if (gfc_is_reallocatable_lhs (expr1)
9178 && !(expr2->expr_type == EXPR_FUNCTION
9179 && expr2->value.function.isym != NULL))
9180 lss->is_alloc_lhs = 1;
9181 rss = NULL;
9183 if ((expr1->ts.type == BT_DERIVED)
9184 && (gfc_is_alloc_class_array_function (expr2)
9185 || gfc_is_alloc_class_scalar_function (expr2)))
9186 expr2->must_finalize = 1;
9188 if (lss != gfc_ss_terminator)
9190 /* The assignment needs scalarization. */
9191 lss_section = lss;
9193 /* Find a non-scalar SS from the lhs. */
9194 while (lss_section != gfc_ss_terminator
9195 && lss_section->info->type != GFC_SS_SECTION)
9196 lss_section = lss_section->next;
9198 gcc_assert (lss_section != gfc_ss_terminator);
9200 /* Initialize the scalarizer. */
9201 gfc_init_loopinfo (&loop);
9203 /* Walk the rhs. */
9204 rss = gfc_walk_expr (expr2);
9205 if (rss == gfc_ss_terminator)
9206 /* The rhs is scalar. Add a ss for the expression. */
9207 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
9209 /* Associate the SS with the loop. */
9210 gfc_add_ss_to_loop (&loop, lss);
9211 gfc_add_ss_to_loop (&loop, rss);
9213 /* Calculate the bounds of the scalarization. */
9214 gfc_conv_ss_startstride (&loop);
9215 /* Enable loop reversal. */
9216 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
9217 loop.reverse[n] = GFC_ENABLE_REVERSE;
9218 /* Resolve any data dependencies in the statement. */
9219 gfc_conv_resolve_dependencies (&loop, lss, rss);
9220 /* Setup the scalarizing loops. */
9221 gfc_conv_loop_setup (&loop, &expr2->where);
9223 /* Setup the gfc_se structures. */
9224 gfc_copy_loopinfo_to_se (&lse, &loop);
9225 gfc_copy_loopinfo_to_se (&rse, &loop);
9227 rse.ss = rss;
9228 gfc_mark_ss_chain_used (rss, 1);
9229 if (loop.temp_ss == NULL)
9231 lse.ss = lss;
9232 gfc_mark_ss_chain_used (lss, 1);
9234 else
9236 lse.ss = loop.temp_ss;
9237 gfc_mark_ss_chain_used (lss, 3);
9238 gfc_mark_ss_chain_used (loop.temp_ss, 3);
9241 /* Allow the scalarizer to workshare array assignments. */
9242 if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_BODY))
9243 == OMPWS_WORKSHARE_FLAG
9244 && loop.temp_ss == NULL)
9246 maybe_workshare = true;
9247 ompws_flags |= OMPWS_SCALARIZER_WS | OMPWS_SCALARIZER_BODY;
9250 /* Start the scalarized loop body. */
9251 gfc_start_scalarized_body (&loop, &body);
9253 else
9254 gfc_init_block (&body);
9256 l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
9258 /* Translate the expression. */
9259 gfc_conv_expr (&rse, expr2);
9261 /* Deal with the case of a scalar class function assigned to a derived type. */
9262 if (gfc_is_alloc_class_scalar_function (expr2)
9263 && expr1->ts.type == BT_DERIVED)
9265 rse.expr = gfc_class_data_get (rse.expr);
9266 rse.expr = build_fold_indirect_ref_loc (input_location, rse.expr);
9269 /* Stabilize a string length for temporaries. */
9270 if (expr2->ts.type == BT_CHARACTER && !expr1->ts.deferred
9271 && !(TREE_CODE (rse.string_length) == VAR_DECL
9272 || TREE_CODE (rse.string_length) == PARM_DECL
9273 || TREE_CODE (rse.string_length) == INDIRECT_REF))
9274 string_length = gfc_evaluate_now (rse.string_length, &rse.pre);
9275 else if (expr2->ts.type == BT_CHARACTER)
9276 string_length = rse.string_length;
9277 else
9278 string_length = NULL_TREE;
9280 if (l_is_temp)
9282 gfc_conv_tmp_array_ref (&lse);
9283 if (expr2->ts.type == BT_CHARACTER)
9284 lse.string_length = string_length;
9286 else
9288 gfc_conv_expr (&lse, expr1);
9289 if (gfc_option.rtcheck & GFC_RTCHECK_MEM
9290 && !init_flag
9291 && gfc_expr_attr (expr1).allocatable
9292 && expr1->rank
9293 && !expr2->rank)
9295 tree cond;
9296 const char* msg;
9298 /* We should only get array references here. */
9299 gcc_assert (TREE_CODE (lse.expr) == POINTER_PLUS_EXPR
9300 || TREE_CODE (lse.expr) == ARRAY_REF);
9302 /* 'tmp' is either the pointer to the array(POINTER_PLUS_EXPR)
9303 or the array itself(ARRAY_REF). */
9304 tmp = TREE_OPERAND (lse.expr, 0);
9306 /* Provide the address of the array. */
9307 if (TREE_CODE (lse.expr) == ARRAY_REF)
9308 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
9310 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
9311 tmp, build_int_cst (TREE_TYPE (tmp), 0));
9312 msg = _("Assignment of scalar to unallocated array");
9313 gfc_trans_runtime_check (true, false, cond, &loop.pre,
9314 &expr1->where, msg);
9318 /* Assignments of scalar derived types with allocatable components
9319 to arrays must be done with a deep copy and the rhs temporary
9320 must have its components deallocated afterwards. */
9321 scalar_to_array = (expr2->ts.type == BT_DERIVED
9322 && expr2->ts.u.derived->attr.alloc_comp
9323 && !expr_is_variable (expr2)
9324 && expr1->rank && !expr2->rank);
9325 scalar_to_array |= (expr1->ts.type == BT_DERIVED
9326 && expr1->rank
9327 && expr1->ts.u.derived->attr.alloc_comp
9328 && gfc_is_alloc_class_scalar_function (expr2));
9329 if (scalar_to_array && dealloc)
9331 tmp = gfc_deallocate_alloc_comp_no_caf (expr2->ts.u.derived, rse.expr, 0);
9332 gfc_prepend_expr_to_block (&loop.post, tmp);
9335 /* When assigning a character function result to a deferred-length variable,
9336 the function call must happen before the (re)allocation of the lhs -
9337 otherwise the character length of the result is not known.
9338 NOTE: This relies on having the exact dependence of the length type
9339 parameter available to the caller; gfortran saves it in the .mod files.
9340 NOTE ALSO: The concatenation operation generates a temporary pointer,
9341 whose allocation must go to the innermost loop. */
9342 if (flag_realloc_lhs
9343 && expr2->ts.type == BT_CHARACTER && expr1->ts.deferred
9344 && !(lss != gfc_ss_terminator
9345 && expr2->expr_type == EXPR_OP
9346 && expr2->value.op.op == INTRINSIC_CONCAT))
9347 gfc_add_block_to_block (&block, &rse.pre);
9349 /* Nullify the allocatable components corresponding to those of the lhs
9350 derived type, so that the finalization of the function result does not
9351 affect the lhs of the assignment. Prepend is used to ensure that the
9352 nullification occurs before the call to the finalizer. In the case of
9353 a scalar to array assignment, this is done in gfc_trans_scalar_assign
9354 as part of the deep copy. */
9355 if (!scalar_to_array && (expr1->ts.type == BT_DERIVED)
9356 && (gfc_is_alloc_class_array_function (expr2)
9357 || gfc_is_alloc_class_scalar_function (expr2)))
9359 tmp = rse.expr;
9360 tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, rse.expr, 0);
9361 gfc_prepend_expr_to_block (&rse.post, tmp);
9362 if (lss != gfc_ss_terminator && rss == gfc_ss_terminator)
9363 gfc_add_block_to_block (&loop.post, &rse.post);
9366 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
9367 expr_is_variable (expr2) || scalar_to_array
9368 || expr2->expr_type == EXPR_ARRAY,
9369 !(l_is_temp || init_flag) && dealloc);
9370 gfc_add_expr_to_block (&body, tmp);
9372 if (lss == gfc_ss_terminator)
9374 /* F2003: Add the code for reallocation on assignment. */
9375 if (flag_realloc_lhs && is_scalar_reallocatable_lhs (expr1))
9376 alloc_scalar_allocatable_for_assignment (&block, string_length,
9377 expr1, expr2);
9379 /* Use the scalar assignment as is. */
9380 gfc_add_block_to_block (&block, &body);
9382 else
9384 gcc_assert (lse.ss == gfc_ss_terminator
9385 && rse.ss == gfc_ss_terminator);
9387 if (l_is_temp)
9389 gfc_trans_scalarized_loop_boundary (&loop, &body);
9391 /* We need to copy the temporary to the actual lhs. */
9392 gfc_init_se (&lse, NULL);
9393 gfc_init_se (&rse, NULL);
9394 gfc_copy_loopinfo_to_se (&lse, &loop);
9395 gfc_copy_loopinfo_to_se (&rse, &loop);
9397 rse.ss = loop.temp_ss;
9398 lse.ss = lss;
9400 gfc_conv_tmp_array_ref (&rse);
9401 gfc_conv_expr (&lse, expr1);
9403 gcc_assert (lse.ss == gfc_ss_terminator
9404 && rse.ss == gfc_ss_terminator);
9406 if (expr2->ts.type == BT_CHARACTER)
9407 rse.string_length = string_length;
9409 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
9410 false, dealloc);
9411 gfc_add_expr_to_block (&body, tmp);
9414 /* F2003: Allocate or reallocate lhs of allocatable array. */
9415 if (flag_realloc_lhs
9416 && gfc_is_reallocatable_lhs (expr1)
9417 && !gfc_expr_attr (expr1).codimension
9418 && !gfc_is_coindexed (expr1)
9419 && expr2->rank
9420 && !is_runtime_conformable (expr1, expr2))
9422 realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
9423 ompws_flags &= ~OMPWS_SCALARIZER_WS;
9424 tmp = gfc_alloc_allocatable_for_assignment (&loop, expr1, expr2);
9425 if (tmp != NULL_TREE)
9426 gfc_add_expr_to_block (&loop.code[expr1->rank - 1], tmp);
9429 if (maybe_workshare)
9430 ompws_flags &= ~OMPWS_SCALARIZER_BODY;
9432 /* Generate the copying loops. */
9433 gfc_trans_scalarizing_loops (&loop, &body);
9435 /* Wrap the whole thing up. */
9436 gfc_add_block_to_block (&block, &loop.pre);
9437 gfc_add_block_to_block (&block, &loop.post);
9439 gfc_cleanup_loop (&loop);
9442 return gfc_finish_block (&block);
9446 /* Check whether EXPR is a copyable array. */
9448 static bool
9449 copyable_array_p (gfc_expr * expr)
9451 if (expr->expr_type != EXPR_VARIABLE)
9452 return false;
9454 /* First check it's an array. */
9455 if (expr->rank < 1 || !expr->ref || expr->ref->next)
9456 return false;
9458 if (!gfc_full_array_ref_p (expr->ref, NULL))
9459 return false;
9461 /* Next check that it's of a simple enough type. */
9462 switch (expr->ts.type)
9464 case BT_INTEGER:
9465 case BT_REAL:
9466 case BT_COMPLEX:
9467 case BT_LOGICAL:
9468 return true;
9470 case BT_CHARACTER:
9471 return false;
9473 case BT_DERIVED:
9474 return !expr->ts.u.derived->attr.alloc_comp;
9476 default:
9477 break;
9480 return false;
9483 /* Translate an assignment. */
9485 tree
9486 gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
9487 bool dealloc)
9489 tree tmp;
9491 /* Special case a single function returning an array. */
9492 if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
9494 tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
9495 if (tmp)
9496 return tmp;
9499 /* Special case assigning an array to zero. */
9500 if (copyable_array_p (expr1)
9501 && is_zero_initializer_p (expr2))
9503 tmp = gfc_trans_zero_assign (expr1);
9504 if (tmp)
9505 return tmp;
9508 /* Special case copying one array to another. */
9509 if (copyable_array_p (expr1)
9510 && copyable_array_p (expr2)
9511 && gfc_compare_types (&expr1->ts, &expr2->ts)
9512 && !gfc_check_dependency (expr1, expr2, 0))
9514 tmp = gfc_trans_array_copy (expr1, expr2);
9515 if (tmp)
9516 return tmp;
9519 /* Special case initializing an array from a constant array constructor. */
9520 if (copyable_array_p (expr1)
9521 && expr2->expr_type == EXPR_ARRAY
9522 && gfc_compare_types (&expr1->ts, &expr2->ts))
9524 tmp = gfc_trans_array_constructor_copy (expr1, expr2);
9525 if (tmp)
9526 return tmp;
9529 /* Fallback to the scalarizer to generate explicit loops. */
9530 return gfc_trans_assignment_1 (expr1, expr2, init_flag, dealloc);
9533 tree
9534 gfc_trans_init_assign (gfc_code * code)
9536 return gfc_trans_assignment (code->expr1, code->expr2, true, false);
9539 tree
9540 gfc_trans_assign (gfc_code * code)
9542 return gfc_trans_assignment (code->expr1, code->expr2, false, true);