* fi.po: Update.
[official-gcc.git] / gcc / fortran / trans-expr.c
blobb9c134a11d4387b6bedfe79ec458c2b8651c53c8
1 /* Expression translation
2 Copyright (C) 2002-2017 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 (CONSTANT_CLASS_P (scalar))
77 tree tmp;
78 tmp = gfc_create_var (TREE_TYPE (scalar), "scalar");
79 gfc_add_modify (&se->pre, tmp, scalar);
80 scalar = tmp;
82 if (!POINTER_TYPE_P (TREE_TYPE (scalar)))
83 scalar = gfc_build_addr_expr (NULL_TREE, scalar);
84 gfc_add_modify (&se->pre, gfc_conv_descriptor_dtype (desc),
85 gfc_get_dtype (type));
86 gfc_conv_descriptor_data_set (&se->pre, desc, scalar);
88 /* Copy pointer address back - but only if it could have changed and
89 if the actual argument is a pointer and not, e.g., NULL(). */
90 if ((attr.pointer || attr.allocatable) && attr.intent != INTENT_IN)
91 gfc_add_modify (&se->post, scalar,
92 fold_convert (TREE_TYPE (scalar),
93 gfc_conv_descriptor_data_get (desc)));
94 return desc;
98 /* Get the coarray token from the ultimate array or component ref.
99 Returns a NULL_TREE, when the ref object is not allocatable or pointer. */
101 tree
102 gfc_get_ultimate_alloc_ptr_comps_caf_token (gfc_se *outerse, gfc_expr *expr)
104 gfc_symbol *sym = expr->symtree->n.sym;
105 bool is_coarray = sym->attr.codimension;
106 gfc_expr *caf_expr = gfc_copy_expr (expr);
107 gfc_ref *ref = caf_expr->ref, *last_caf_ref = NULL;
109 while (ref)
111 if (ref->type == REF_COMPONENT
112 && (ref->u.c.component->attr.allocatable
113 || ref->u.c.component->attr.pointer)
114 && (is_coarray || ref->u.c.component->attr.codimension))
115 last_caf_ref = ref;
116 ref = ref->next;
119 if (last_caf_ref == NULL)
120 return NULL_TREE;
122 tree comp = last_caf_ref->u.c.component->caf_token, caf;
123 gfc_se se;
124 bool comp_ref = !last_caf_ref->u.c.component->attr.dimension;
125 if (comp == NULL_TREE && comp_ref)
126 return NULL_TREE;
127 gfc_init_se (&se, outerse);
128 gfc_free_ref_list (last_caf_ref->next);
129 last_caf_ref->next = NULL;
130 caf_expr->rank = comp_ref ? 0 : last_caf_ref->u.c.component->as->rank;
131 se.want_pointer = comp_ref;
132 gfc_conv_expr (&se, caf_expr);
133 gfc_add_block_to_block (&outerse->pre, &se.pre);
135 if (TREE_CODE (se.expr) == COMPONENT_REF && comp_ref)
136 se.expr = TREE_OPERAND (se.expr, 0);
137 gfc_free_expr (caf_expr);
139 if (comp_ref)
140 caf = fold_build3_loc (input_location, COMPONENT_REF,
141 TREE_TYPE (comp), se.expr, comp, NULL_TREE);
142 else
143 caf = gfc_conv_descriptor_token (se.expr);
144 return gfc_build_addr_expr (NULL_TREE, caf);
148 /* This is the seed for an eventual trans-class.c
150 The following parameters should not be used directly since they might
151 in future implementations. Use the corresponding APIs. */
152 #define CLASS_DATA_FIELD 0
153 #define CLASS_VPTR_FIELD 1
154 #define CLASS_LEN_FIELD 2
155 #define VTABLE_HASH_FIELD 0
156 #define VTABLE_SIZE_FIELD 1
157 #define VTABLE_EXTENDS_FIELD 2
158 #define VTABLE_DEF_INIT_FIELD 3
159 #define VTABLE_COPY_FIELD 4
160 #define VTABLE_FINAL_FIELD 5
161 #define VTABLE_DEALLOCATE_FIELD 6
164 tree
165 gfc_class_set_static_fields (tree decl, tree vptr, tree data)
167 tree tmp;
168 tree field;
169 vec<constructor_elt, va_gc> *init = NULL;
171 field = TYPE_FIELDS (TREE_TYPE (decl));
172 tmp = gfc_advance_chain (field, CLASS_DATA_FIELD);
173 CONSTRUCTOR_APPEND_ELT (init, tmp, data);
175 tmp = gfc_advance_chain (field, CLASS_VPTR_FIELD);
176 CONSTRUCTOR_APPEND_ELT (init, tmp, vptr);
178 return build_constructor (TREE_TYPE (decl), init);
182 tree
183 gfc_class_data_get (tree decl)
185 tree data;
186 if (POINTER_TYPE_P (TREE_TYPE (decl)))
187 decl = build_fold_indirect_ref_loc (input_location, decl);
188 data = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
189 CLASS_DATA_FIELD);
190 return fold_build3_loc (input_location, COMPONENT_REF,
191 TREE_TYPE (data), decl, data,
192 NULL_TREE);
196 tree
197 gfc_class_vptr_get (tree decl)
199 tree vptr;
200 /* For class arrays decl may be a temporary descriptor handle, the vptr is
201 then available through the saved descriptor. */
202 if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
203 && GFC_DECL_SAVED_DESCRIPTOR (decl))
204 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
205 if (POINTER_TYPE_P (TREE_TYPE (decl)))
206 decl = build_fold_indirect_ref_loc (input_location, decl);
207 vptr = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
208 CLASS_VPTR_FIELD);
209 return fold_build3_loc (input_location, COMPONENT_REF,
210 TREE_TYPE (vptr), decl, vptr,
211 NULL_TREE);
215 tree
216 gfc_class_len_get (tree decl)
218 tree len;
219 /* For class arrays decl may be a temporary descriptor handle, the len is
220 then available through the saved descriptor. */
221 if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
222 && GFC_DECL_SAVED_DESCRIPTOR (decl))
223 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
224 if (POINTER_TYPE_P (TREE_TYPE (decl)))
225 decl = build_fold_indirect_ref_loc (input_location, decl);
226 len = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
227 CLASS_LEN_FIELD);
228 return fold_build3_loc (input_location, COMPONENT_REF,
229 TREE_TYPE (len), decl, len,
230 NULL_TREE);
234 /* Try to get the _len component of a class. When the class is not unlimited
235 poly, i.e. no _len field exists, then return a zero node. */
237 tree
238 gfc_class_len_or_zero_get (tree decl)
240 tree len;
241 /* For class arrays decl may be a temporary descriptor handle, the vptr is
242 then available through the saved descriptor. */
243 if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
244 && GFC_DECL_SAVED_DESCRIPTOR (decl))
245 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
246 if (POINTER_TYPE_P (TREE_TYPE (decl)))
247 decl = build_fold_indirect_ref_loc (input_location, decl);
248 len = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
249 CLASS_LEN_FIELD);
250 return len != NULL_TREE ? fold_build3_loc (input_location, COMPONENT_REF,
251 TREE_TYPE (len), decl, len,
252 NULL_TREE)
253 : integer_zero_node;
257 /* Get the specified FIELD from the VPTR. */
259 static tree
260 vptr_field_get (tree vptr, int fieldno)
262 tree field;
263 vptr = build_fold_indirect_ref_loc (input_location, vptr);
264 field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (vptr)),
265 fieldno);
266 field = fold_build3_loc (input_location, COMPONENT_REF,
267 TREE_TYPE (field), vptr, field,
268 NULL_TREE);
269 gcc_assert (field);
270 return field;
274 /* Get the field from the class' vptr. */
276 static tree
277 class_vtab_field_get (tree decl, int fieldno)
279 tree vptr;
280 vptr = gfc_class_vptr_get (decl);
281 return vptr_field_get (vptr, fieldno);
285 /* Define a macro for creating the class_vtab_* and vptr_* accessors in
286 unison. */
287 #define VTAB_GET_FIELD_GEN(name, field) tree \
288 gfc_class_vtab_## name ##_get (tree cl) \
290 return class_vtab_field_get (cl, field); \
293 tree \
294 gfc_vptr_## name ##_get (tree vptr) \
296 return vptr_field_get (vptr, field); \
299 VTAB_GET_FIELD_GEN (hash, VTABLE_HASH_FIELD)
300 VTAB_GET_FIELD_GEN (extends, VTABLE_EXTENDS_FIELD)
301 VTAB_GET_FIELD_GEN (def_init, VTABLE_DEF_INIT_FIELD)
302 VTAB_GET_FIELD_GEN (copy, VTABLE_COPY_FIELD)
303 VTAB_GET_FIELD_GEN (final, VTABLE_FINAL_FIELD)
304 VTAB_GET_FIELD_GEN (deallocate, VTABLE_DEALLOCATE_FIELD)
307 /* The size field is returned as an array index type. Therefore treat
308 it and only it specially. */
310 tree
311 gfc_class_vtab_size_get (tree cl)
313 tree size;
314 size = class_vtab_field_get (cl, VTABLE_SIZE_FIELD);
315 /* Always return size as an array index type. */
316 size = fold_convert (gfc_array_index_type, size);
317 gcc_assert (size);
318 return size;
321 tree
322 gfc_vptr_size_get (tree vptr)
324 tree size;
325 size = vptr_field_get (vptr, VTABLE_SIZE_FIELD);
326 /* Always return size as an array index type. */
327 size = fold_convert (gfc_array_index_type, size);
328 gcc_assert (size);
329 return size;
333 #undef CLASS_DATA_FIELD
334 #undef CLASS_VPTR_FIELD
335 #undef CLASS_LEN_FIELD
336 #undef VTABLE_HASH_FIELD
337 #undef VTABLE_SIZE_FIELD
338 #undef VTABLE_EXTENDS_FIELD
339 #undef VTABLE_DEF_INIT_FIELD
340 #undef VTABLE_COPY_FIELD
341 #undef VTABLE_FINAL_FIELD
344 /* Search for the last _class ref in the chain of references of this
345 expression and cut the chain there. Albeit this routine is similiar
346 to class.c::gfc_add_component_ref (), is there a significant
347 difference: gfc_add_component_ref () concentrates on an array ref to
348 be the last ref in the chain. This routine is oblivious to the kind
349 of refs following. */
351 gfc_expr *
352 gfc_find_and_cut_at_last_class_ref (gfc_expr *e)
354 gfc_expr *base_expr;
355 gfc_ref *ref, *class_ref, *tail = NULL, *array_ref;
357 /* Find the last class reference. */
358 class_ref = NULL;
359 array_ref = NULL;
360 for (ref = e->ref; ref; ref = ref->next)
362 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
363 array_ref = ref;
365 if (ref->type == REF_COMPONENT
366 && ref->u.c.component->ts.type == BT_CLASS)
368 /* Component to the right of a part reference with nonzero rank
369 must not have the ALLOCATABLE attribute. If attempts are
370 made to reference such a component reference, an error results
371 followed by an ICE. */
372 if (array_ref && CLASS_DATA (ref->u.c.component)->attr.allocatable)
373 return NULL;
374 class_ref = ref;
377 if (ref->next == NULL)
378 break;
381 /* Remove and store all subsequent references after the
382 CLASS reference. */
383 if (class_ref)
385 tail = class_ref->next;
386 class_ref->next = NULL;
388 else if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
390 tail = e->ref;
391 e->ref = NULL;
394 base_expr = gfc_expr_to_initialize (e);
396 /* Restore the original tail expression. */
397 if (class_ref)
399 gfc_free_ref_list (class_ref->next);
400 class_ref->next = tail;
402 else if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
404 gfc_free_ref_list (e->ref);
405 e->ref = tail;
407 return base_expr;
411 /* Reset the vptr to the declared type, e.g. after deallocation. */
413 void
414 gfc_reset_vptr (stmtblock_t *block, gfc_expr *e)
416 gfc_symbol *vtab;
417 tree vptr;
418 tree vtable;
419 gfc_se se;
421 /* Evaluate the expression and obtain the vptr from it. */
422 gfc_init_se (&se, NULL);
423 if (e->rank)
424 gfc_conv_expr_descriptor (&se, e);
425 else
426 gfc_conv_expr (&se, e);
427 gfc_add_block_to_block (block, &se.pre);
428 vptr = gfc_get_vptr_from_expr (se.expr);
430 /* If a vptr is not found, we can do nothing more. */
431 if (vptr == NULL_TREE)
432 return;
434 if (UNLIMITED_POLY (e))
435 gfc_add_modify (block, vptr, build_int_cst (TREE_TYPE (vptr), 0));
436 else
438 /* Return the vptr to the address of the declared type. */
439 vtab = gfc_find_derived_vtab (e->ts.u.derived);
440 vtable = vtab->backend_decl;
441 if (vtable == NULL_TREE)
442 vtable = gfc_get_symbol_decl (vtab);
443 vtable = gfc_build_addr_expr (NULL, vtable);
444 vtable = fold_convert (TREE_TYPE (vptr), vtable);
445 gfc_add_modify (block, vptr, vtable);
450 /* Reset the len for unlimited polymorphic objects. */
452 void
453 gfc_reset_len (stmtblock_t *block, gfc_expr *expr)
455 gfc_expr *e;
456 gfc_se se_len;
457 e = gfc_find_and_cut_at_last_class_ref (expr);
458 if (e == NULL)
459 return;
460 gfc_add_len_component (e);
461 gfc_init_se (&se_len, NULL);
462 gfc_conv_expr (&se_len, e);
463 gfc_add_modify (block, se_len.expr,
464 fold_convert (TREE_TYPE (se_len.expr), integer_zero_node));
465 gfc_free_expr (e);
469 /* Obtain the vptr of the last class reference in an expression.
470 Return NULL_TREE if no class reference is found. */
472 tree
473 gfc_get_vptr_from_expr (tree expr)
475 tree tmp;
476 tree type;
478 for (tmp = expr; tmp; tmp = TREE_OPERAND (tmp, 0))
480 type = TREE_TYPE (tmp);
481 while (type)
483 if (GFC_CLASS_TYPE_P (type))
484 return gfc_class_vptr_get (tmp);
485 if (type != TYPE_CANONICAL (type))
486 type = TYPE_CANONICAL (type);
487 else
488 type = NULL_TREE;
490 if (VAR_P (tmp) || TREE_CODE (tmp) == PARM_DECL)
491 break;
494 if (POINTER_TYPE_P (TREE_TYPE (tmp)))
495 tmp = build_fold_indirect_ref_loc (input_location, tmp);
497 if (GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
498 return gfc_class_vptr_get (tmp);
500 return NULL_TREE;
504 static void
505 class_array_data_assign (stmtblock_t *block, tree lhs_desc, tree rhs_desc,
506 bool lhs_type)
508 tree tmp, tmp2, type;
510 gfc_conv_descriptor_data_set (block, lhs_desc,
511 gfc_conv_descriptor_data_get (rhs_desc));
512 gfc_conv_descriptor_offset_set (block, lhs_desc,
513 gfc_conv_descriptor_offset_get (rhs_desc));
515 gfc_add_modify (block, gfc_conv_descriptor_dtype (lhs_desc),
516 gfc_conv_descriptor_dtype (rhs_desc));
518 /* Assign the dimension as range-ref. */
519 tmp = gfc_get_descriptor_dimension (lhs_desc);
520 tmp2 = gfc_get_descriptor_dimension (rhs_desc);
522 type = lhs_type ? TREE_TYPE (tmp) : TREE_TYPE (tmp2);
523 tmp = build4_loc (input_location, ARRAY_RANGE_REF, type, tmp,
524 gfc_index_zero_node, NULL_TREE, NULL_TREE);
525 tmp2 = build4_loc (input_location, ARRAY_RANGE_REF, type, tmp2,
526 gfc_index_zero_node, NULL_TREE, NULL_TREE);
527 gfc_add_modify (block, tmp, tmp2);
531 /* Takes a derived type expression and returns the address of a temporary
532 class object of the 'declared' type. If vptr is not NULL, this is
533 used for the temporary class object.
534 optional_alloc_ptr is false when the dummy is neither allocatable
535 nor a pointer; that's only relevant for the optional handling. */
536 void
537 gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e,
538 gfc_typespec class_ts, tree vptr, bool optional,
539 bool optional_alloc_ptr)
541 gfc_symbol *vtab;
542 tree cond_optional = NULL_TREE;
543 gfc_ss *ss;
544 tree ctree;
545 tree var;
546 tree tmp;
548 /* The derived type needs to be converted to a temporary
549 CLASS object. */
550 tmp = gfc_typenode_for_spec (&class_ts);
551 var = gfc_create_var (tmp, "class");
553 /* Set the vptr. */
554 ctree = gfc_class_vptr_get (var);
556 if (vptr != NULL_TREE)
558 /* Use the dynamic vptr. */
559 tmp = vptr;
561 else
563 /* In this case the vtab corresponds to the derived type and the
564 vptr must point to it. */
565 vtab = gfc_find_derived_vtab (e->ts.u.derived);
566 gcc_assert (vtab);
567 tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
569 gfc_add_modify (&parmse->pre, ctree,
570 fold_convert (TREE_TYPE (ctree), tmp));
572 /* Now set the data field. */
573 ctree = gfc_class_data_get (var);
575 if (optional)
576 cond_optional = gfc_conv_expr_present (e->symtree->n.sym);
578 if (parmse->expr && POINTER_TYPE_P (TREE_TYPE (parmse->expr)))
580 /* If there is a ready made pointer to a derived type, use it
581 rather than evaluating the expression again. */
582 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
583 gfc_add_modify (&parmse->pre, ctree, tmp);
585 else if (parmse->ss && parmse->ss->info && parmse->ss->info->useflags)
587 /* For an array reference in an elemental procedure call we need
588 to retain the ss to provide the scalarized array reference. */
589 gfc_conv_expr_reference (parmse, e);
590 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
591 if (optional)
592 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
593 cond_optional, tmp,
594 fold_convert (TREE_TYPE (tmp), null_pointer_node));
595 gfc_add_modify (&parmse->pre, ctree, tmp);
597 else
599 ss = gfc_walk_expr (e);
600 if (ss == gfc_ss_terminator)
602 parmse->ss = NULL;
603 gfc_conv_expr_reference (parmse, e);
605 /* Scalar to an assumed-rank array. */
606 if (class_ts.u.derived->components->as)
608 tree type;
609 type = get_scalar_to_descriptor_type (parmse->expr,
610 gfc_expr_attr (e));
611 gfc_add_modify (&parmse->pre, gfc_conv_descriptor_dtype (ctree),
612 gfc_get_dtype (type));
613 if (optional)
614 parmse->expr = build3_loc (input_location, COND_EXPR,
615 TREE_TYPE (parmse->expr),
616 cond_optional, parmse->expr,
617 fold_convert (TREE_TYPE (parmse->expr),
618 null_pointer_node));
619 gfc_conv_descriptor_data_set (&parmse->pre, ctree, parmse->expr);
621 else
623 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
624 if (optional)
625 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
626 cond_optional, tmp,
627 fold_convert (TREE_TYPE (tmp),
628 null_pointer_node));
629 gfc_add_modify (&parmse->pre, ctree, tmp);
632 else
634 stmtblock_t block;
635 gfc_init_block (&block);
637 parmse->ss = ss;
638 gfc_conv_expr_descriptor (parmse, e);
640 if (e->rank != class_ts.u.derived->components->as->rank)
642 gcc_assert (class_ts.u.derived->components->as->type
643 == AS_ASSUMED_RANK);
644 class_array_data_assign (&block, ctree, parmse->expr, false);
646 else
648 if (gfc_expr_attr (e).codimension)
649 parmse->expr = fold_build1_loc (input_location,
650 VIEW_CONVERT_EXPR,
651 TREE_TYPE (ctree),
652 parmse->expr);
653 gfc_add_modify (&block, ctree, parmse->expr);
656 if (optional)
658 tmp = gfc_finish_block (&block);
660 gfc_init_block (&block);
661 gfc_conv_descriptor_data_set (&block, ctree, null_pointer_node);
663 tmp = build3_v (COND_EXPR, cond_optional, tmp,
664 gfc_finish_block (&block));
665 gfc_add_expr_to_block (&parmse->pre, tmp);
667 else
668 gfc_add_block_to_block (&parmse->pre, &block);
672 if (class_ts.u.derived->components->ts.type == BT_DERIVED
673 && class_ts.u.derived->components->ts.u.derived
674 ->attr.unlimited_polymorphic)
676 /* Take care about initializing the _len component correctly. */
677 ctree = gfc_class_len_get (var);
678 if (UNLIMITED_POLY (e))
680 gfc_expr *len;
681 gfc_se se;
683 len = gfc_copy_expr (e);
684 gfc_add_len_component (len);
685 gfc_init_se (&se, NULL);
686 gfc_conv_expr (&se, len);
687 if (optional)
688 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se.expr),
689 cond_optional, se.expr,
690 fold_convert (TREE_TYPE (se.expr),
691 integer_zero_node));
692 else
693 tmp = se.expr;
695 else
696 tmp = integer_zero_node;
697 gfc_add_modify (&parmse->pre, ctree, fold_convert (TREE_TYPE (ctree),
698 tmp));
700 /* Pass the address of the class object. */
701 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
703 if (optional && optional_alloc_ptr)
704 parmse->expr = build3_loc (input_location, COND_EXPR,
705 TREE_TYPE (parmse->expr),
706 cond_optional, parmse->expr,
707 fold_convert (TREE_TYPE (parmse->expr),
708 null_pointer_node));
712 /* Create a new class container, which is required as scalar coarrays
713 have an array descriptor while normal scalars haven't. Optionally,
714 NULL pointer checks are added if the argument is OPTIONAL. */
716 static void
717 class_scalar_coarray_to_class (gfc_se *parmse, gfc_expr *e,
718 gfc_typespec class_ts, bool optional)
720 tree var, ctree, tmp;
721 stmtblock_t block;
722 gfc_ref *ref;
723 gfc_ref *class_ref;
725 gfc_init_block (&block);
727 class_ref = NULL;
728 for (ref = e->ref; ref; ref = ref->next)
730 if (ref->type == REF_COMPONENT
731 && ref->u.c.component->ts.type == BT_CLASS)
732 class_ref = ref;
735 if (class_ref == NULL
736 && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
737 tmp = e->symtree->n.sym->backend_decl;
738 else
740 /* Remove everything after the last class reference, convert the
741 expression and then recover its tailend once more. */
742 gfc_se tmpse;
743 ref = class_ref->next;
744 class_ref->next = NULL;
745 gfc_init_se (&tmpse, NULL);
746 gfc_conv_expr (&tmpse, e);
747 class_ref->next = ref;
748 tmp = tmpse.expr;
751 var = gfc_typenode_for_spec (&class_ts);
752 var = gfc_create_var (var, "class");
754 ctree = gfc_class_vptr_get (var);
755 gfc_add_modify (&block, ctree,
756 fold_convert (TREE_TYPE (ctree), gfc_class_vptr_get (tmp)));
758 ctree = gfc_class_data_get (var);
759 tmp = gfc_conv_descriptor_data_get (gfc_class_data_get (tmp));
760 gfc_add_modify (&block, ctree, fold_convert (TREE_TYPE (ctree), tmp));
762 /* Pass the address of the class object. */
763 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
765 if (optional)
767 tree cond = gfc_conv_expr_present (e->symtree->n.sym);
768 tree tmp2;
770 tmp = gfc_finish_block (&block);
772 gfc_init_block (&block);
773 tmp2 = gfc_class_data_get (var);
774 gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
775 null_pointer_node));
776 tmp2 = gfc_finish_block (&block);
778 tmp = build3_loc (input_location, COND_EXPR, void_type_node,
779 cond, tmp, tmp2);
780 gfc_add_expr_to_block (&parmse->pre, tmp);
782 else
783 gfc_add_block_to_block (&parmse->pre, &block);
787 /* Takes an intrinsic type expression and returns the address of a temporary
788 class object of the 'declared' type. */
789 void
790 gfc_conv_intrinsic_to_class (gfc_se *parmse, gfc_expr *e,
791 gfc_typespec class_ts)
793 gfc_symbol *vtab;
794 gfc_ss *ss;
795 tree ctree;
796 tree var;
797 tree tmp;
799 /* The intrinsic type needs to be converted to a temporary
800 CLASS object. */
801 tmp = gfc_typenode_for_spec (&class_ts);
802 var = gfc_create_var (tmp, "class");
804 /* Set the vptr. */
805 ctree = gfc_class_vptr_get (var);
807 vtab = gfc_find_vtab (&e->ts);
808 gcc_assert (vtab);
809 tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
810 gfc_add_modify (&parmse->pre, ctree,
811 fold_convert (TREE_TYPE (ctree), tmp));
813 /* Now set the data field. */
814 ctree = gfc_class_data_get (var);
815 if (parmse->ss && parmse->ss->info->useflags)
817 /* For an array reference in an elemental procedure call we need
818 to retain the ss to provide the scalarized array reference. */
819 gfc_conv_expr_reference (parmse, e);
820 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
821 gfc_add_modify (&parmse->pre, ctree, tmp);
823 else
825 ss = gfc_walk_expr (e);
826 if (ss == gfc_ss_terminator)
828 parmse->ss = NULL;
829 gfc_conv_expr_reference (parmse, e);
830 if (class_ts.u.derived->components->as
831 && class_ts.u.derived->components->as->type == AS_ASSUMED_RANK)
833 tmp = gfc_conv_scalar_to_descriptor (parmse, parmse->expr,
834 gfc_expr_attr (e));
835 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
836 TREE_TYPE (ctree), tmp);
838 else
839 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
840 gfc_add_modify (&parmse->pre, ctree, tmp);
842 else
844 parmse->ss = ss;
845 parmse->use_offset = 1;
846 gfc_conv_expr_descriptor (parmse, e);
847 if (class_ts.u.derived->components->as->rank != e->rank)
849 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
850 TREE_TYPE (ctree), parmse->expr);
851 gfc_add_modify (&parmse->pre, ctree, tmp);
853 else
854 gfc_add_modify (&parmse->pre, ctree, parmse->expr);
858 gcc_assert (class_ts.type == BT_CLASS);
859 if (class_ts.u.derived->components->ts.type == BT_DERIVED
860 && class_ts.u.derived->components->ts.u.derived
861 ->attr.unlimited_polymorphic)
863 ctree = gfc_class_len_get (var);
864 /* When the actual arg is a char array, then set the _len component of the
865 unlimited polymorphic entity to the length of the string. */
866 if (e->ts.type == BT_CHARACTER)
868 /* Start with parmse->string_length because this seems to be set to a
869 correct value more often. */
870 if (parmse->string_length)
871 tmp = parmse->string_length;
872 /* When the string_length is not yet set, then try the backend_decl of
873 the cl. */
874 else if (e->ts.u.cl->backend_decl)
875 tmp = e->ts.u.cl->backend_decl;
876 /* If both of the above approaches fail, then try to generate an
877 expression from the input, which is only feasible currently, when the
878 expression can be evaluated to a constant one. */
879 else
881 /* Try to simplify the expression. */
882 gfc_simplify_expr (e, 0);
883 if (e->expr_type == EXPR_CONSTANT && !e->ts.u.cl->resolved)
885 /* Amazingly all data is present to compute the length of a
886 constant string, but the expression is not yet there. */
887 e->ts.u.cl->length = gfc_get_constant_expr (BT_INTEGER, 4,
888 &e->where);
889 mpz_set_ui (e->ts.u.cl->length->value.integer,
890 e->value.character.length);
891 gfc_conv_const_charlen (e->ts.u.cl);
892 e->ts.u.cl->resolved = 1;
893 tmp = e->ts.u.cl->backend_decl;
895 else
897 gfc_error ("Can't compute the length of the char array at %L.",
898 &e->where);
902 else
903 tmp = integer_zero_node;
905 gfc_add_modify (&parmse->pre, ctree, tmp);
907 else if (class_ts.type == BT_CLASS
908 && class_ts.u.derived->components
909 && class_ts.u.derived->components->ts.u
910 .derived->attr.unlimited_polymorphic)
912 ctree = gfc_class_len_get (var);
913 gfc_add_modify (&parmse->pre, ctree,
914 fold_convert (TREE_TYPE (ctree),
915 integer_zero_node));
917 /* Pass the address of the class object. */
918 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
922 /* Takes a scalarized class array expression and returns the
923 address of a temporary scalar class object of the 'declared'
924 type.
925 OOP-TODO: This could be improved by adding code that branched on
926 the dynamic type being the same as the declared type. In this case
927 the original class expression can be passed directly.
928 optional_alloc_ptr is false when the dummy is neither allocatable
929 nor a pointer; that's relevant for the optional handling.
930 Set copyback to true if class container's _data and _vtab pointers
931 might get modified. */
933 void
934 gfc_conv_class_to_class (gfc_se *parmse, gfc_expr *e, gfc_typespec class_ts,
935 bool elemental, bool copyback, bool optional,
936 bool optional_alloc_ptr)
938 tree ctree;
939 tree var;
940 tree tmp;
941 tree vptr;
942 tree cond = NULL_TREE;
943 tree slen = NULL_TREE;
944 gfc_ref *ref;
945 gfc_ref *class_ref;
946 stmtblock_t block;
947 bool full_array = false;
949 gfc_init_block (&block);
951 class_ref = NULL;
952 for (ref = e->ref; ref; ref = ref->next)
954 if (ref->type == REF_COMPONENT
955 && ref->u.c.component->ts.type == BT_CLASS)
956 class_ref = ref;
958 if (ref->next == NULL)
959 break;
962 if ((ref == NULL || class_ref == ref)
963 && (!class_ts.u.derived->components->as
964 || class_ts.u.derived->components->as->rank != -1))
965 return;
967 /* Test for FULL_ARRAY. */
968 if (e->rank == 0 && gfc_expr_attr (e).codimension
969 && gfc_expr_attr (e).dimension)
970 full_array = true;
971 else
972 gfc_is_class_array_ref (e, &full_array);
974 /* The derived type needs to be converted to a temporary
975 CLASS object. */
976 tmp = gfc_typenode_for_spec (&class_ts);
977 var = gfc_create_var (tmp, "class");
979 /* Set the data. */
980 ctree = gfc_class_data_get (var);
981 if (class_ts.u.derived->components->as
982 && e->rank != class_ts.u.derived->components->as->rank)
984 if (e->rank == 0)
986 tree type = get_scalar_to_descriptor_type (parmse->expr,
987 gfc_expr_attr (e));
988 gfc_add_modify (&block, gfc_conv_descriptor_dtype (ctree),
989 gfc_get_dtype (type));
991 tmp = gfc_class_data_get (parmse->expr);
992 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
993 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
995 gfc_conv_descriptor_data_set (&block, ctree, tmp);
997 else
998 class_array_data_assign (&block, ctree, parmse->expr, false);
1000 else
1002 if (TREE_TYPE (parmse->expr) != TREE_TYPE (ctree))
1003 parmse->expr = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
1004 TREE_TYPE (ctree), parmse->expr);
1005 gfc_add_modify (&block, ctree, parmse->expr);
1008 /* Return the data component, except in the case of scalarized array
1009 references, where nullification of the cannot occur and so there
1010 is no need. */
1011 if (!elemental && full_array && copyback)
1013 if (class_ts.u.derived->components->as
1014 && e->rank != class_ts.u.derived->components->as->rank)
1016 if (e->rank == 0)
1017 gfc_add_modify (&parmse->post, gfc_class_data_get (parmse->expr),
1018 gfc_conv_descriptor_data_get (ctree));
1019 else
1020 class_array_data_assign (&parmse->post, parmse->expr, ctree, true);
1022 else
1023 gfc_add_modify (&parmse->post, parmse->expr, ctree);
1026 /* Set the vptr. */
1027 ctree = gfc_class_vptr_get (var);
1029 /* The vptr is the second field of the actual argument.
1030 First we have to find the corresponding class reference. */
1032 tmp = NULL_TREE;
1033 if (class_ref == NULL
1034 && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
1036 tmp = e->symtree->n.sym->backend_decl;
1038 if (TREE_CODE (tmp) == FUNCTION_DECL)
1039 tmp = gfc_get_fake_result_decl (e->symtree->n.sym, 0);
1041 if (DECL_LANG_SPECIFIC (tmp) && GFC_DECL_SAVED_DESCRIPTOR (tmp))
1042 tmp = GFC_DECL_SAVED_DESCRIPTOR (tmp);
1044 slen = integer_zero_node;
1046 else
1048 /* Remove everything after the last class reference, convert the
1049 expression and then recover its tailend once more. */
1050 gfc_se tmpse;
1051 ref = class_ref->next;
1052 class_ref->next = NULL;
1053 gfc_init_se (&tmpse, NULL);
1054 gfc_conv_expr (&tmpse, e);
1055 class_ref->next = ref;
1056 tmp = tmpse.expr;
1057 slen = tmpse.string_length;
1060 gcc_assert (tmp != NULL_TREE);
1062 /* Dereference if needs be. */
1063 if (TREE_CODE (TREE_TYPE (tmp)) == REFERENCE_TYPE)
1064 tmp = build_fold_indirect_ref_loc (input_location, tmp);
1066 vptr = gfc_class_vptr_get (tmp);
1067 gfc_add_modify (&block, ctree,
1068 fold_convert (TREE_TYPE (ctree), vptr));
1070 /* Return the vptr component, except in the case of scalarized array
1071 references, where the dynamic type cannot change. */
1072 if (!elemental && full_array && copyback)
1073 gfc_add_modify (&parmse->post, vptr,
1074 fold_convert (TREE_TYPE (vptr), ctree));
1076 /* For unlimited polymorphic objects also set the _len component. */
1077 if (class_ts.type == BT_CLASS
1078 && class_ts.u.derived->components
1079 && class_ts.u.derived->components->ts.u
1080 .derived->attr.unlimited_polymorphic)
1082 ctree = gfc_class_len_get (var);
1083 if (UNLIMITED_POLY (e))
1084 tmp = gfc_class_len_get (tmp);
1085 else if (e->ts.type == BT_CHARACTER)
1087 gcc_assert (slen != NULL_TREE);
1088 tmp = slen;
1090 else
1091 tmp = integer_zero_node;
1092 gfc_add_modify (&parmse->pre, ctree,
1093 fold_convert (TREE_TYPE (ctree), tmp));
1095 /* Return the len component, except in the case of scalarized array
1096 references, where the dynamic type cannot change. */
1097 if (!elemental && full_array && copyback)
1098 gfc_add_modify (&parmse->post, tmp,
1099 fold_convert (TREE_TYPE (tmp), ctree));
1102 if (optional)
1104 tree tmp2;
1106 cond = gfc_conv_expr_present (e->symtree->n.sym);
1107 /* parmse->pre may contain some preparatory instructions for the
1108 temporary array descriptor. Those may only be executed when the
1109 optional argument is set, therefore add parmse->pre's instructions
1110 to block, which is later guarded by an if (optional_arg_given). */
1111 gfc_add_block_to_block (&parmse->pre, &block);
1112 block.head = parmse->pre.head;
1113 parmse->pre.head = NULL_TREE;
1114 tmp = gfc_finish_block (&block);
1116 if (optional_alloc_ptr)
1117 tmp2 = build_empty_stmt (input_location);
1118 else
1120 gfc_init_block (&block);
1122 tmp2 = gfc_conv_descriptor_data_get (gfc_class_data_get (var));
1123 gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
1124 null_pointer_node));
1125 tmp2 = gfc_finish_block (&block);
1128 tmp = build3_loc (input_location, COND_EXPR, void_type_node,
1129 cond, tmp, tmp2);
1130 gfc_add_expr_to_block (&parmse->pre, tmp);
1132 else
1133 gfc_add_block_to_block (&parmse->pre, &block);
1135 /* Pass the address of the class object. */
1136 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
1138 if (optional && optional_alloc_ptr)
1139 parmse->expr = build3_loc (input_location, COND_EXPR,
1140 TREE_TYPE (parmse->expr),
1141 cond, parmse->expr,
1142 fold_convert (TREE_TYPE (parmse->expr),
1143 null_pointer_node));
1147 /* Given a class array declaration and an index, returns the address
1148 of the referenced element. */
1150 tree
1151 gfc_get_class_array_ref (tree index, tree class_decl, tree data_comp)
1153 tree data = data_comp != NULL_TREE ? data_comp :
1154 gfc_class_data_get (class_decl);
1155 tree size = gfc_class_vtab_size_get (class_decl);
1156 tree offset = fold_build2_loc (input_location, MULT_EXPR,
1157 gfc_array_index_type,
1158 index, size);
1159 tree ptr;
1160 data = gfc_conv_descriptor_data_get (data);
1161 ptr = fold_convert (pvoid_type_node, data);
1162 ptr = fold_build_pointer_plus_loc (input_location, ptr, offset);
1163 return fold_convert (TREE_TYPE (data), ptr);
1167 /* Copies one class expression to another, assuming that if either
1168 'to' or 'from' are arrays they are packed. Should 'from' be
1169 NULL_TREE, the initialization expression for 'to' is used, assuming
1170 that the _vptr is set. */
1172 tree
1173 gfc_copy_class_to_class (tree from, tree to, tree nelems, bool unlimited)
1175 tree fcn;
1176 tree fcn_type;
1177 tree from_data;
1178 tree from_len;
1179 tree to_data;
1180 tree to_len;
1181 tree to_ref;
1182 tree from_ref;
1183 vec<tree, va_gc> *args;
1184 tree tmp;
1185 tree stdcopy;
1186 tree extcopy;
1187 tree index;
1188 bool is_from_desc = false, is_to_class = false;
1190 args = NULL;
1191 /* To prevent warnings on uninitialized variables. */
1192 from_len = to_len = NULL_TREE;
1194 if (from != NULL_TREE)
1195 fcn = gfc_class_vtab_copy_get (from);
1196 else
1197 fcn = gfc_class_vtab_copy_get (to);
1199 fcn_type = TREE_TYPE (TREE_TYPE (fcn));
1201 if (from != NULL_TREE)
1203 is_from_desc = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from));
1204 if (is_from_desc)
1206 from_data = from;
1207 from = GFC_DECL_SAVED_DESCRIPTOR (from);
1209 else
1211 /* Check that from is a class. When the class is part of a coarray,
1212 then from is a common pointer and is to be used as is. */
1213 tmp = POINTER_TYPE_P (TREE_TYPE (from))
1214 ? build_fold_indirect_ref (from) : from;
1215 from_data =
1216 (GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
1217 || (DECL_P (tmp) && GFC_DECL_CLASS (tmp)))
1218 ? gfc_class_data_get (from) : from;
1219 is_from_desc = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data));
1222 else
1223 from_data = gfc_class_vtab_def_init_get (to);
1225 if (unlimited)
1227 if (from != NULL_TREE && unlimited)
1228 from_len = gfc_class_len_or_zero_get (from);
1229 else
1230 from_len = integer_zero_node;
1233 if (GFC_CLASS_TYPE_P (TREE_TYPE (to)))
1235 is_to_class = true;
1236 to_data = gfc_class_data_get (to);
1237 if (unlimited)
1238 to_len = gfc_class_len_get (to);
1240 else
1241 /* When to is a BT_DERIVED and not a BT_CLASS, then to_data == to. */
1242 to_data = to;
1244 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (to_data)))
1246 stmtblock_t loopbody;
1247 stmtblock_t body;
1248 stmtblock_t ifbody;
1249 gfc_loopinfo loop;
1250 tree orig_nelems = nelems; /* Needed for bounds check. */
1252 gfc_init_block (&body);
1253 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1254 gfc_array_index_type, nelems,
1255 gfc_index_one_node);
1256 nelems = gfc_evaluate_now (tmp, &body);
1257 index = gfc_create_var (gfc_array_index_type, "S");
1259 if (is_from_desc)
1261 from_ref = gfc_get_class_array_ref (index, from, from_data);
1262 vec_safe_push (args, from_ref);
1264 else
1265 vec_safe_push (args, from_data);
1267 if (is_to_class)
1268 to_ref = gfc_get_class_array_ref (index, to, to_data);
1269 else
1271 tmp = gfc_conv_array_data (to);
1272 tmp = build_fold_indirect_ref_loc (input_location, tmp);
1273 to_ref = gfc_build_addr_expr (NULL_TREE,
1274 gfc_build_array_ref (tmp, index, to));
1276 vec_safe_push (args, to_ref);
1278 /* Add bounds check. */
1279 if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) > 0 && is_from_desc)
1281 char *msg;
1282 const char *name = "<<unknown>>";
1283 tree from_len;
1285 if (DECL_P (to))
1286 name = (const char *)(DECL_NAME (to)->identifier.id.str);
1288 from_len = gfc_conv_descriptor_size (from_data, 1);
1289 tmp = fold_build2_loc (input_location, NE_EXPR,
1290 boolean_type_node, from_len, orig_nelems);
1291 msg = xasprintf ("Array bound mismatch for dimension %d "
1292 "of array '%s' (%%ld/%%ld)",
1293 1, name);
1295 gfc_trans_runtime_check (true, false, tmp, &body,
1296 &gfc_current_locus, msg,
1297 fold_convert (long_integer_type_node, orig_nelems),
1298 fold_convert (long_integer_type_node, from_len));
1300 free (msg);
1303 tmp = build_call_vec (fcn_type, fcn, args);
1305 /* Build the body of the loop. */
1306 gfc_init_block (&loopbody);
1307 gfc_add_expr_to_block (&loopbody, tmp);
1309 /* Build the loop and return. */
1310 gfc_init_loopinfo (&loop);
1311 loop.dimen = 1;
1312 loop.from[0] = gfc_index_zero_node;
1313 loop.loopvar[0] = index;
1314 loop.to[0] = nelems;
1315 gfc_trans_scalarizing_loops (&loop, &loopbody);
1316 gfc_init_block (&ifbody);
1317 gfc_add_block_to_block (&ifbody, &loop.pre);
1318 stdcopy = gfc_finish_block (&ifbody);
1319 /* In initialization mode from_len is a constant zero. */
1320 if (unlimited && !integer_zerop (from_len))
1322 vec_safe_push (args, from_len);
1323 vec_safe_push (args, to_len);
1324 tmp = build_call_vec (fcn_type, fcn, args);
1325 /* Build the body of the loop. */
1326 gfc_init_block (&loopbody);
1327 gfc_add_expr_to_block (&loopbody, tmp);
1329 /* Build the loop and return. */
1330 gfc_init_loopinfo (&loop);
1331 loop.dimen = 1;
1332 loop.from[0] = gfc_index_zero_node;
1333 loop.loopvar[0] = index;
1334 loop.to[0] = nelems;
1335 gfc_trans_scalarizing_loops (&loop, &loopbody);
1336 gfc_init_block (&ifbody);
1337 gfc_add_block_to_block (&ifbody, &loop.pre);
1338 extcopy = gfc_finish_block (&ifbody);
1340 tmp = fold_build2_loc (input_location, GT_EXPR,
1341 boolean_type_node, from_len,
1342 integer_zero_node);
1343 tmp = fold_build3_loc (input_location, COND_EXPR,
1344 void_type_node, tmp, extcopy, stdcopy);
1345 gfc_add_expr_to_block (&body, tmp);
1346 tmp = gfc_finish_block (&body);
1348 else
1350 gfc_add_expr_to_block (&body, stdcopy);
1351 tmp = gfc_finish_block (&body);
1353 gfc_cleanup_loop (&loop);
1355 else
1357 gcc_assert (!is_from_desc);
1358 vec_safe_push (args, from_data);
1359 vec_safe_push (args, to_data);
1360 stdcopy = build_call_vec (fcn_type, fcn, args);
1362 /* In initialization mode from_len is a constant zero. */
1363 if (unlimited && !integer_zerop (from_len))
1365 vec_safe_push (args, from_len);
1366 vec_safe_push (args, to_len);
1367 extcopy = build_call_vec (fcn_type, fcn, args);
1368 tmp = fold_build2_loc (input_location, GT_EXPR,
1369 boolean_type_node, from_len,
1370 integer_zero_node);
1371 tmp = fold_build3_loc (input_location, COND_EXPR,
1372 void_type_node, tmp, extcopy, stdcopy);
1374 else
1375 tmp = stdcopy;
1378 /* Only copy _def_init to to_data, when it is not a NULL-pointer. */
1379 if (from == NULL_TREE)
1381 tree cond;
1382 cond = fold_build2_loc (input_location, NE_EXPR,
1383 boolean_type_node,
1384 from_data, null_pointer_node);
1385 tmp = fold_build3_loc (input_location, COND_EXPR,
1386 void_type_node, cond,
1387 tmp, build_empty_stmt (input_location));
1390 return tmp;
1394 static tree
1395 gfc_trans_class_array_init_assign (gfc_expr *rhs, gfc_expr *lhs, gfc_expr *obj)
1397 gfc_actual_arglist *actual;
1398 gfc_expr *ppc;
1399 gfc_code *ppc_code;
1400 tree res;
1402 actual = gfc_get_actual_arglist ();
1403 actual->expr = gfc_copy_expr (rhs);
1404 actual->next = gfc_get_actual_arglist ();
1405 actual->next->expr = gfc_copy_expr (lhs);
1406 ppc = gfc_copy_expr (obj);
1407 gfc_add_vptr_component (ppc);
1408 gfc_add_component_ref (ppc, "_copy");
1409 ppc_code = gfc_get_code (EXEC_CALL);
1410 ppc_code->resolved_sym = ppc->symtree->n.sym;
1411 /* Although '_copy' is set to be elemental in class.c, it is
1412 not staying that way. Find out why, sometime.... */
1413 ppc_code->resolved_sym->attr.elemental = 1;
1414 ppc_code->ext.actual = actual;
1415 ppc_code->expr1 = ppc;
1416 /* Since '_copy' is elemental, the scalarizer will take care
1417 of arrays in gfc_trans_call. */
1418 res = gfc_trans_call (ppc_code, false, NULL, NULL, false);
1419 gfc_free_statements (ppc_code);
1421 if (UNLIMITED_POLY(obj))
1423 /* Check if rhs is non-NULL. */
1424 gfc_se src;
1425 gfc_init_se (&src, NULL);
1426 gfc_conv_expr (&src, rhs);
1427 src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
1428 tree cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
1429 src.expr, fold_convert (TREE_TYPE (src.expr),
1430 null_pointer_node));
1431 res = build3_loc (input_location, COND_EXPR, TREE_TYPE (res), cond, res,
1432 build_empty_stmt (input_location));
1435 return res;
1438 /* Special case for initializing a polymorphic dummy with INTENT(OUT).
1439 A MEMCPY is needed to copy the full data from the default initializer
1440 of the dynamic type. */
1442 tree
1443 gfc_trans_class_init_assign (gfc_code *code)
1445 stmtblock_t block;
1446 tree tmp;
1447 gfc_se dst,src,memsz;
1448 gfc_expr *lhs, *rhs, *sz;
1450 gfc_start_block (&block);
1452 lhs = gfc_copy_expr (code->expr1);
1453 gfc_add_data_component (lhs);
1455 rhs = gfc_copy_expr (code->expr1);
1456 gfc_add_vptr_component (rhs);
1458 /* Make sure that the component backend_decls have been built, which
1459 will not have happened if the derived types concerned have not
1460 been referenced. */
1461 gfc_get_derived_type (rhs->ts.u.derived);
1462 gfc_add_def_init_component (rhs);
1463 /* The _def_init is always scalar. */
1464 rhs->rank = 0;
1466 if (code->expr1->ts.type == BT_CLASS
1467 && CLASS_DATA (code->expr1)->attr.dimension)
1469 gfc_array_spec *tmparr = gfc_get_array_spec ();
1470 *tmparr = *CLASS_DATA (code->expr1)->as;
1471 gfc_add_full_array_ref (lhs, tmparr);
1472 tmp = gfc_trans_class_array_init_assign (rhs, lhs, code->expr1);
1474 else
1476 sz = gfc_copy_expr (code->expr1);
1477 gfc_add_vptr_component (sz);
1478 gfc_add_size_component (sz);
1480 gfc_init_se (&dst, NULL);
1481 gfc_init_se (&src, NULL);
1482 gfc_init_se (&memsz, NULL);
1483 gfc_conv_expr (&dst, lhs);
1484 gfc_conv_expr (&src, rhs);
1485 gfc_conv_expr (&memsz, sz);
1486 gfc_add_block_to_block (&block, &src.pre);
1487 src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
1489 tmp = gfc_build_memcpy_call (dst.expr, src.expr, memsz.expr);
1491 if (UNLIMITED_POLY(code->expr1))
1493 /* Check if _def_init is non-NULL. */
1494 tree cond = fold_build2_loc (input_location, NE_EXPR,
1495 boolean_type_node, src.expr,
1496 fold_convert (TREE_TYPE (src.expr),
1497 null_pointer_node));
1498 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
1499 tmp, build_empty_stmt (input_location));
1503 if (code->expr1->symtree->n.sym->attr.optional
1504 || code->expr1->symtree->n.sym->ns->proc_name->attr.entry_master)
1506 tree present = gfc_conv_expr_present (code->expr1->symtree->n.sym);
1507 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
1508 present, tmp,
1509 build_empty_stmt (input_location));
1512 gfc_add_expr_to_block (&block, tmp);
1514 return gfc_finish_block (&block);
1518 /* End of prototype trans-class.c */
1521 static void
1522 realloc_lhs_warning (bt type, bool array, locus *where)
1524 if (array && type != BT_CLASS && type != BT_DERIVED && warn_realloc_lhs)
1525 gfc_warning (OPT_Wrealloc_lhs,
1526 "Code for reallocating the allocatable array at %L will "
1527 "be added", where);
1528 else if (warn_realloc_lhs_all)
1529 gfc_warning (OPT_Wrealloc_lhs_all,
1530 "Code for reallocating the allocatable variable at %L "
1531 "will be added", where);
1535 static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
1536 gfc_expr *);
1538 /* Copy the scalarization loop variables. */
1540 static void
1541 gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
1543 dest->ss = src->ss;
1544 dest->loop = src->loop;
1548 /* Initialize a simple expression holder.
1550 Care must be taken when multiple se are created with the same parent.
1551 The child se must be kept in sync. The easiest way is to delay creation
1552 of a child se until after after the previous se has been translated. */
1554 void
1555 gfc_init_se (gfc_se * se, gfc_se * parent)
1557 memset (se, 0, sizeof (gfc_se));
1558 gfc_init_block (&se->pre);
1559 gfc_init_block (&se->post);
1561 se->parent = parent;
1563 if (parent)
1564 gfc_copy_se_loopvars (se, parent);
1568 /* Advances to the next SS in the chain. Use this rather than setting
1569 se->ss = se->ss->next because all the parents needs to be kept in sync.
1570 See gfc_init_se. */
1572 void
1573 gfc_advance_se_ss_chain (gfc_se * se)
1575 gfc_se *p;
1576 gfc_ss *ss;
1578 gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
1580 p = se;
1581 /* Walk down the parent chain. */
1582 while (p != NULL)
1584 /* Simple consistency check. */
1585 gcc_assert (p->parent == NULL || p->parent->ss == p->ss
1586 || p->parent->ss->nested_ss == p->ss);
1588 /* If we were in a nested loop, the next scalarized expression can be
1589 on the parent ss' next pointer. Thus we should not take the next
1590 pointer blindly, but rather go up one nest level as long as next
1591 is the end of chain. */
1592 ss = p->ss;
1593 while (ss->next == gfc_ss_terminator && ss->parent != NULL)
1594 ss = ss->parent;
1596 p->ss = ss->next;
1598 p = p->parent;
1603 /* Ensures the result of the expression as either a temporary variable
1604 or a constant so that it can be used repeatedly. */
1606 void
1607 gfc_make_safe_expr (gfc_se * se)
1609 tree var;
1611 if (CONSTANT_CLASS_P (se->expr))
1612 return;
1614 /* We need a temporary for this result. */
1615 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
1616 gfc_add_modify (&se->pre, var, se->expr);
1617 se->expr = var;
1621 /* Return an expression which determines if a dummy parameter is present.
1622 Also used for arguments to procedures with multiple entry points. */
1624 tree
1625 gfc_conv_expr_present (gfc_symbol * sym)
1627 tree decl, cond;
1629 gcc_assert (sym->attr.dummy);
1630 decl = gfc_get_symbol_decl (sym);
1632 /* Intrinsic scalars with VALUE attribute which are passed by value
1633 use a hidden argument to denote the present status. */
1634 if (sym->attr.value && sym->ts.type != BT_CHARACTER
1635 && sym->ts.type != BT_CLASS && sym->ts.type != BT_DERIVED
1636 && !sym->attr.dimension)
1638 char name[GFC_MAX_SYMBOL_LEN + 2];
1639 tree tree_name;
1641 gcc_assert (TREE_CODE (decl) == PARM_DECL);
1642 name[0] = '_';
1643 strcpy (&name[1], sym->name);
1644 tree_name = get_identifier (name);
1646 /* Walk function argument list to find hidden arg. */
1647 cond = DECL_ARGUMENTS (DECL_CONTEXT (decl));
1648 for ( ; cond != NULL_TREE; cond = TREE_CHAIN (cond))
1649 if (DECL_NAME (cond) == tree_name)
1650 break;
1652 gcc_assert (cond);
1653 return cond;
1656 if (TREE_CODE (decl) != PARM_DECL)
1658 /* Array parameters use a temporary descriptor, we want the real
1659 parameter. */
1660 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
1661 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
1662 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
1665 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, decl,
1666 fold_convert (TREE_TYPE (decl), null_pointer_node));
1668 /* Fortran 2008 allows to pass null pointers and non-associated pointers
1669 as actual argument to denote absent dummies. For array descriptors,
1670 we thus also need to check the array descriptor. For BT_CLASS, it
1671 can also occur for scalars and F2003 due to type->class wrapping and
1672 class->class wrapping. Note further that BT_CLASS always uses an
1673 array descriptor for arrays, also for explicit-shape/assumed-size. */
1675 if (!sym->attr.allocatable
1676 && ((sym->ts.type != BT_CLASS && !sym->attr.pointer)
1677 || (sym->ts.type == BT_CLASS
1678 && !CLASS_DATA (sym)->attr.allocatable
1679 && !CLASS_DATA (sym)->attr.class_pointer))
1680 && ((gfc_option.allow_std & GFC_STD_F2008) != 0
1681 || sym->ts.type == BT_CLASS))
1683 tree tmp;
1685 if ((sym->as && (sym->as->type == AS_ASSUMED_SHAPE
1686 || sym->as->type == AS_ASSUMED_RANK
1687 || sym->attr.codimension))
1688 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
1690 tmp = build_fold_indirect_ref_loc (input_location, decl);
1691 if (sym->ts.type == BT_CLASS)
1692 tmp = gfc_class_data_get (tmp);
1693 tmp = gfc_conv_array_data (tmp);
1695 else if (sym->ts.type == BT_CLASS)
1696 tmp = gfc_class_data_get (decl);
1697 else
1698 tmp = NULL_TREE;
1700 if (tmp != NULL_TREE)
1702 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, tmp,
1703 fold_convert (TREE_TYPE (tmp), null_pointer_node));
1704 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
1705 boolean_type_node, cond, tmp);
1709 return cond;
1713 /* Converts a missing, dummy argument into a null or zero. */
1715 void
1716 gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
1718 tree present;
1719 tree tmp;
1721 present = gfc_conv_expr_present (arg->symtree->n.sym);
1723 if (kind > 0)
1725 /* Create a temporary and convert it to the correct type. */
1726 tmp = gfc_get_int_type (kind);
1727 tmp = fold_convert (tmp, build_fold_indirect_ref_loc (input_location,
1728 se->expr));
1730 /* Test for a NULL value. */
1731 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
1732 tmp, fold_convert (TREE_TYPE (tmp), integer_one_node));
1733 tmp = gfc_evaluate_now (tmp, &se->pre);
1734 se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
1736 else
1738 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
1739 present, se->expr,
1740 build_zero_cst (TREE_TYPE (se->expr)));
1741 tmp = gfc_evaluate_now (tmp, &se->pre);
1742 se->expr = tmp;
1745 if (ts.type == BT_CHARACTER)
1747 tmp = build_int_cst (gfc_charlen_type_node, 0);
1748 tmp = fold_build3_loc (input_location, COND_EXPR, gfc_charlen_type_node,
1749 present, se->string_length, tmp);
1750 tmp = gfc_evaluate_now (tmp, &se->pre);
1751 se->string_length = tmp;
1753 return;
1757 /* Get the character length of an expression, looking through gfc_refs
1758 if necessary. */
1760 tree
1761 gfc_get_expr_charlen (gfc_expr *e)
1763 gfc_ref *r;
1764 tree length;
1766 gcc_assert (e->expr_type == EXPR_VARIABLE
1767 && e->ts.type == BT_CHARACTER);
1769 length = NULL; /* To silence compiler warning. */
1771 if (is_subref_array (e) && e->ts.u.cl->length)
1773 gfc_se tmpse;
1774 gfc_init_se (&tmpse, NULL);
1775 gfc_conv_expr_type (&tmpse, e->ts.u.cl->length, gfc_charlen_type_node);
1776 e->ts.u.cl->backend_decl = tmpse.expr;
1777 return tmpse.expr;
1780 /* First candidate: if the variable is of type CHARACTER, the
1781 expression's length could be the length of the character
1782 variable. */
1783 if (e->symtree->n.sym->ts.type == BT_CHARACTER)
1784 length = e->symtree->n.sym->ts.u.cl->backend_decl;
1786 /* Look through the reference chain for component references. */
1787 for (r = e->ref; r; r = r->next)
1789 switch (r->type)
1791 case REF_COMPONENT:
1792 if (r->u.c.component->ts.type == BT_CHARACTER)
1793 length = r->u.c.component->ts.u.cl->backend_decl;
1794 break;
1796 case REF_ARRAY:
1797 /* Do nothing. */
1798 break;
1800 default:
1801 /* We should never got substring references here. These will be
1802 broken down by the scalarizer. */
1803 gcc_unreachable ();
1804 break;
1808 gcc_assert (length != NULL);
1809 return length;
1813 /* Return for an expression the backend decl of the coarray. */
1815 tree
1816 gfc_get_tree_for_caf_expr (gfc_expr *expr)
1818 tree caf_decl;
1819 bool found = false;
1820 gfc_ref *ref;
1822 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE);
1824 /* Not-implemented diagnostic. */
1825 if (expr->symtree->n.sym->ts.type == BT_CLASS
1826 && UNLIMITED_POLY (expr->symtree->n.sym)
1827 && CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
1828 gfc_error ("Sorry, coindexed access to an unlimited polymorphic object at "
1829 "%L is not supported", &expr->where);
1831 for (ref = expr->ref; ref; ref = ref->next)
1832 if (ref->type == REF_COMPONENT)
1834 if (ref->u.c.component->ts.type == BT_CLASS
1835 && UNLIMITED_POLY (ref->u.c.component)
1836 && CLASS_DATA (ref->u.c.component)->attr.codimension)
1837 gfc_error ("Sorry, coindexed access to an unlimited polymorphic "
1838 "component at %L is not supported", &expr->where);
1841 caf_decl = expr->symtree->n.sym->backend_decl;
1842 gcc_assert (caf_decl);
1843 if (expr->symtree->n.sym->ts.type == BT_CLASS)
1845 if (expr->ref && expr->ref->type == REF_ARRAY)
1847 caf_decl = gfc_class_data_get (caf_decl);
1848 if (CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
1849 return caf_decl;
1851 for (ref = expr->ref; ref; ref = ref->next)
1853 if (ref->type == REF_COMPONENT
1854 && strcmp (ref->u.c.component->name, "_data") != 0)
1856 caf_decl = gfc_class_data_get (caf_decl);
1857 if (CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
1858 return caf_decl;
1859 break;
1861 else if (ref->type == REF_ARRAY && ref->u.ar.dimen)
1862 break;
1865 if (expr->symtree->n.sym->attr.codimension)
1866 return caf_decl;
1868 /* The following code assumes that the coarray is a component reachable via
1869 only scalar components/variables; the Fortran standard guarantees this. */
1871 for (ref = expr->ref; ref; ref = ref->next)
1872 if (ref->type == REF_COMPONENT)
1874 gfc_component *comp = ref->u.c.component;
1876 if (POINTER_TYPE_P (TREE_TYPE (caf_decl)))
1877 caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
1878 caf_decl = fold_build3_loc (input_location, COMPONENT_REF,
1879 TREE_TYPE (comp->backend_decl), caf_decl,
1880 comp->backend_decl, NULL_TREE);
1881 if (comp->ts.type == BT_CLASS)
1883 caf_decl = gfc_class_data_get (caf_decl);
1884 if (CLASS_DATA (comp)->attr.codimension)
1886 found = true;
1887 break;
1890 if (comp->attr.codimension)
1892 found = true;
1893 break;
1896 gcc_assert (found && caf_decl);
1897 return caf_decl;
1901 /* Obtain the Coarray token - and optionally also the offset. */
1903 void
1904 gfc_get_caf_token_offset (gfc_se *se, tree *token, tree *offset, tree caf_decl,
1905 tree se_expr, gfc_expr *expr)
1907 tree tmp;
1909 /* Coarray token. */
1910 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
1912 gcc_assert (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl))
1913 == GFC_ARRAY_ALLOCATABLE
1914 || expr->symtree->n.sym->attr.select_type_temporary);
1915 *token = gfc_conv_descriptor_token (caf_decl);
1917 else if (DECL_LANG_SPECIFIC (caf_decl)
1918 && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
1919 *token = GFC_DECL_TOKEN (caf_decl);
1920 else
1922 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (caf_decl))
1923 && GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl)) != NULL_TREE);
1924 *token = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl));
1927 if (offset == NULL)
1928 return;
1930 /* Offset between the coarray base address and the address wanted. */
1931 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl))
1932 && (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_ALLOCATABLE
1933 || GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_POINTER))
1934 *offset = build_int_cst (gfc_array_index_type, 0);
1935 else if (DECL_LANG_SPECIFIC (caf_decl)
1936 && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
1937 *offset = GFC_DECL_CAF_OFFSET (caf_decl);
1938 else if (GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl)) != NULL_TREE)
1939 *offset = GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl));
1940 else
1941 *offset = build_int_cst (gfc_array_index_type, 0);
1943 if (POINTER_TYPE_P (TREE_TYPE (se_expr))
1944 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se_expr))))
1946 tmp = build_fold_indirect_ref_loc (input_location, se_expr);
1947 tmp = gfc_conv_descriptor_data_get (tmp);
1949 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se_expr)))
1950 tmp = gfc_conv_descriptor_data_get (se_expr);
1951 else
1953 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se_expr)));
1954 tmp = se_expr;
1957 *offset = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1958 *offset, fold_convert (gfc_array_index_type, tmp));
1960 if (expr->symtree->n.sym->ts.type == BT_DERIVED
1961 && expr->symtree->n.sym->attr.codimension
1962 && expr->symtree->n.sym->ts.u.derived->attr.alloc_comp)
1964 gfc_expr *base_expr = gfc_copy_expr (expr);
1965 gfc_ref *ref = base_expr->ref;
1966 gfc_se base_se;
1968 // Iterate through the refs until the last one.
1969 while (ref->next)
1970 ref = ref->next;
1972 if (ref->type == REF_ARRAY
1973 && ref->u.ar.type != AR_FULL)
1975 const int ranksum = ref->u.ar.dimen + ref->u.ar.codimen;
1976 int i;
1977 for (i = 0; i < ranksum; ++i)
1979 ref->u.ar.start[i] = NULL;
1980 ref->u.ar.end[i] = NULL;
1982 ref->u.ar.type = AR_FULL;
1984 gfc_init_se (&base_se, NULL);
1985 if (gfc_caf_attr (base_expr).dimension)
1987 gfc_conv_expr_descriptor (&base_se, base_expr);
1988 tmp = gfc_conv_descriptor_data_get (base_se.expr);
1990 else
1992 gfc_conv_expr (&base_se, base_expr);
1993 tmp = base_se.expr;
1996 gfc_free_expr (base_expr);
1997 gfc_add_block_to_block (&se->pre, &base_se.pre);
1998 gfc_add_block_to_block (&se->post, &base_se.post);
2000 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
2001 tmp = gfc_conv_descriptor_data_get (caf_decl);
2002 else
2004 gcc_assert (POINTER_TYPE_P (TREE_TYPE (caf_decl)));
2005 tmp = caf_decl;
2008 *offset = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2009 fold_convert (gfc_array_index_type, *offset),
2010 fold_convert (gfc_array_index_type, tmp));
2014 /* Convert the coindex of a coarray into an image index; the result is
2015 image_num = (idx(1)-lcobound(1)+1) + (idx(2)-lcobound(2))*extent(1)
2016 + (idx(3)-lcobound(3))*extend(1)*extent(2) + ... */
2018 tree
2019 gfc_caf_get_image_index (stmtblock_t *block, gfc_expr *e, tree desc)
2021 gfc_ref *ref;
2022 tree lbound, ubound, extent, tmp, img_idx;
2023 gfc_se se;
2024 int i;
2026 for (ref = e->ref; ref; ref = ref->next)
2027 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
2028 break;
2029 gcc_assert (ref != NULL);
2031 if (ref->u.ar.dimen_type[ref->u.ar.dimen] == DIMEN_THIS_IMAGE)
2033 return build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
2034 integer_zero_node);
2037 img_idx = integer_zero_node;
2038 extent = integer_one_node;
2039 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
2040 for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
2042 gfc_init_se (&se, NULL);
2043 gfc_conv_expr_type (&se, ref->u.ar.start[i], integer_type_node);
2044 gfc_add_block_to_block (block, &se.pre);
2045 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
2046 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2047 integer_type_node, se.expr,
2048 fold_convert(integer_type_node, lbound));
2049 tmp = fold_build2_loc (input_location, MULT_EXPR, integer_type_node,
2050 extent, tmp);
2051 img_idx = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
2052 img_idx, tmp);
2053 if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
2055 ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
2056 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
2057 tmp = fold_convert (integer_type_node, tmp);
2058 extent = fold_build2_loc (input_location, MULT_EXPR,
2059 integer_type_node, extent, tmp);
2062 else
2063 for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
2065 gfc_init_se (&se, NULL);
2066 gfc_conv_expr_type (&se, ref->u.ar.start[i], integer_type_node);
2067 gfc_add_block_to_block (block, &se.pre);
2068 lbound = GFC_TYPE_ARRAY_LBOUND (TREE_TYPE (desc), i);
2069 lbound = fold_convert (integer_type_node, lbound);
2070 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2071 integer_type_node, se.expr, lbound);
2072 tmp = fold_build2_loc (input_location, MULT_EXPR, integer_type_node,
2073 extent, tmp);
2074 img_idx = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
2075 img_idx, tmp);
2076 if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
2078 ubound = GFC_TYPE_ARRAY_UBOUND (TREE_TYPE (desc), i);
2079 ubound = fold_convert (integer_type_node, ubound);
2080 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2081 integer_type_node, ubound, lbound);
2082 tmp = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
2083 tmp, integer_one_node);
2084 extent = fold_build2_loc (input_location, MULT_EXPR,
2085 integer_type_node, extent, tmp);
2088 img_idx = fold_build2_loc (input_location, PLUS_EXPR, integer_type_node,
2089 img_idx, integer_one_node);
2090 return img_idx;
2094 /* For each character array constructor subexpression without a ts.u.cl->length,
2095 replace it by its first element (if there aren't any elements, the length
2096 should already be set to zero). */
2098 static void
2099 flatten_array_ctors_without_strlen (gfc_expr* e)
2101 gfc_actual_arglist* arg;
2102 gfc_constructor* c;
2104 if (!e)
2105 return;
2107 switch (e->expr_type)
2110 case EXPR_OP:
2111 flatten_array_ctors_without_strlen (e->value.op.op1);
2112 flatten_array_ctors_without_strlen (e->value.op.op2);
2113 break;
2115 case EXPR_COMPCALL:
2116 /* TODO: Implement as with EXPR_FUNCTION when needed. */
2117 gcc_unreachable ();
2119 case EXPR_FUNCTION:
2120 for (arg = e->value.function.actual; arg; arg = arg->next)
2121 flatten_array_ctors_without_strlen (arg->expr);
2122 break;
2124 case EXPR_ARRAY:
2126 /* We've found what we're looking for. */
2127 if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
2129 gfc_constructor *c;
2130 gfc_expr* new_expr;
2132 gcc_assert (e->value.constructor);
2134 c = gfc_constructor_first (e->value.constructor);
2135 new_expr = c->expr;
2136 c->expr = NULL;
2138 flatten_array_ctors_without_strlen (new_expr);
2139 gfc_replace_expr (e, new_expr);
2140 break;
2143 /* Otherwise, fall through to handle constructor elements. */
2144 gcc_fallthrough ();
2145 case EXPR_STRUCTURE:
2146 for (c = gfc_constructor_first (e->value.constructor);
2147 c; c = gfc_constructor_next (c))
2148 flatten_array_ctors_without_strlen (c->expr);
2149 break;
2151 default:
2152 break;
2158 /* Generate code to initialize a string length variable. Returns the
2159 value. For array constructors, cl->length might be NULL and in this case,
2160 the first element of the constructor is needed. expr is the original
2161 expression so we can access it but can be NULL if this is not needed. */
2163 void
2164 gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
2166 gfc_se se;
2168 gfc_init_se (&se, NULL);
2170 if (!cl->length && cl->backend_decl && VAR_P (cl->backend_decl))
2171 return;
2173 /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
2174 "flatten" array constructors by taking their first element; all elements
2175 should be the same length or a cl->length should be present. */
2176 if (!cl->length)
2178 gfc_expr* expr_flat;
2179 gcc_assert (expr);
2180 expr_flat = gfc_copy_expr (expr);
2181 flatten_array_ctors_without_strlen (expr_flat);
2182 gfc_resolve_expr (expr_flat);
2184 gfc_conv_expr (&se, expr_flat);
2185 gfc_add_block_to_block (pblock, &se.pre);
2186 cl->backend_decl = convert (gfc_charlen_type_node, se.string_length);
2188 gfc_free_expr (expr_flat);
2189 return;
2192 /* Convert cl->length. */
2194 gcc_assert (cl->length);
2196 gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
2197 se.expr = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
2198 se.expr, build_int_cst (gfc_charlen_type_node, 0));
2199 gfc_add_block_to_block (pblock, &se.pre);
2201 if (cl->backend_decl)
2202 gfc_add_modify (pblock, cl->backend_decl, se.expr);
2203 else
2204 cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
2208 static void
2209 gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
2210 const char *name, locus *where)
2212 tree tmp;
2213 tree type;
2214 tree fault;
2215 gfc_se start;
2216 gfc_se end;
2217 char *msg;
2218 mpz_t length;
2220 type = gfc_get_character_type (kind, ref->u.ss.length);
2221 type = build_pointer_type (type);
2223 gfc_init_se (&start, se);
2224 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
2225 gfc_add_block_to_block (&se->pre, &start.pre);
2227 if (integer_onep (start.expr))
2228 gfc_conv_string_parameter (se);
2229 else
2231 tmp = start.expr;
2232 STRIP_NOPS (tmp);
2233 /* Avoid multiple evaluation of substring start. */
2234 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
2235 start.expr = gfc_evaluate_now (start.expr, &se->pre);
2237 /* Change the start of the string. */
2238 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
2239 tmp = se->expr;
2240 else
2241 tmp = build_fold_indirect_ref_loc (input_location,
2242 se->expr);
2243 tmp = gfc_build_array_ref (tmp, start.expr, NULL);
2244 se->expr = gfc_build_addr_expr (type, tmp);
2247 /* Length = end + 1 - start. */
2248 gfc_init_se (&end, se);
2249 if (ref->u.ss.end == NULL)
2250 end.expr = se->string_length;
2251 else
2253 gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
2254 gfc_add_block_to_block (&se->pre, &end.pre);
2256 tmp = end.expr;
2257 STRIP_NOPS (tmp);
2258 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
2259 end.expr = gfc_evaluate_now (end.expr, &se->pre);
2261 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
2263 tree nonempty = fold_build2_loc (input_location, LE_EXPR,
2264 boolean_type_node, start.expr,
2265 end.expr);
2267 /* Check lower bound. */
2268 fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
2269 start.expr,
2270 build_int_cst (gfc_charlen_type_node, 1));
2271 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2272 boolean_type_node, nonempty, fault);
2273 if (name)
2274 msg = xasprintf ("Substring out of bounds: lower bound (%%ld) of '%s' "
2275 "is less than one", name);
2276 else
2277 msg = xasprintf ("Substring out of bounds: lower bound (%%ld)"
2278 "is less than one");
2279 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
2280 fold_convert (long_integer_type_node,
2281 start.expr));
2282 free (msg);
2284 /* Check upper bound. */
2285 fault = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
2286 end.expr, se->string_length);
2287 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
2288 boolean_type_node, nonempty, fault);
2289 if (name)
2290 msg = xasprintf ("Substring out of bounds: upper bound (%%ld) of '%s' "
2291 "exceeds string length (%%ld)", name);
2292 else
2293 msg = xasprintf ("Substring out of bounds: upper bound (%%ld) "
2294 "exceeds string length (%%ld)");
2295 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
2296 fold_convert (long_integer_type_node, end.expr),
2297 fold_convert (long_integer_type_node,
2298 se->string_length));
2299 free (msg);
2302 /* Try to calculate the length from the start and end expressions. */
2303 if (ref->u.ss.end
2304 && gfc_dep_difference (ref->u.ss.end, ref->u.ss.start, &length))
2306 int i_len;
2308 i_len = mpz_get_si (length) + 1;
2309 if (i_len < 0)
2310 i_len = 0;
2312 tmp = build_int_cst (gfc_charlen_type_node, i_len);
2313 mpz_clear (length); /* Was initialized by gfc_dep_difference. */
2315 else
2317 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
2318 end.expr, start.expr);
2319 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
2320 build_int_cst (gfc_charlen_type_node, 1), tmp);
2321 tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
2322 tmp, build_int_cst (gfc_charlen_type_node, 0));
2325 se->string_length = tmp;
2329 /* Convert a derived type component reference. */
2331 static void
2332 gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
2334 gfc_component *c;
2335 tree tmp;
2336 tree decl;
2337 tree field;
2338 tree context;
2340 c = ref->u.c.component;
2342 if (c->backend_decl == NULL_TREE
2343 && ref->u.c.sym != NULL)
2344 gfc_get_derived_type (ref->u.c.sym);
2346 field = c->backend_decl;
2347 gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
2348 decl = se->expr;
2349 context = DECL_FIELD_CONTEXT (field);
2351 /* Components can correspond to fields of different containing
2352 types, as components are created without context, whereas
2353 a concrete use of a component has the type of decl as context.
2354 So, if the type doesn't match, we search the corresponding
2355 FIELD_DECL in the parent type. To not waste too much time
2356 we cache this result in norestrict_decl.
2357 On the other hand, if the context is a UNION or a MAP (a
2358 RECORD_TYPE within a UNION_TYPE) always use the given FIELD_DECL. */
2360 if (context != TREE_TYPE (decl)
2361 && !( TREE_CODE (TREE_TYPE (field)) == UNION_TYPE /* Field is union */
2362 || TREE_CODE (context) == UNION_TYPE)) /* Field is map */
2364 tree f2 = c->norestrict_decl;
2365 if (!f2 || DECL_FIELD_CONTEXT (f2) != TREE_TYPE (decl))
2366 for (f2 = TYPE_FIELDS (TREE_TYPE (decl)); f2; f2 = DECL_CHAIN (f2))
2367 if (TREE_CODE (f2) == FIELD_DECL
2368 && DECL_NAME (f2) == DECL_NAME (field))
2369 break;
2370 gcc_assert (f2);
2371 c->norestrict_decl = f2;
2372 field = f2;
2375 if (ref->u.c.sym && ref->u.c.sym->ts.type == BT_CLASS
2376 && strcmp ("_data", c->name) == 0)
2378 /* Found a ref to the _data component. Store the associated ref to
2379 the vptr in se->class_vptr. */
2380 se->class_vptr = gfc_class_vptr_get (decl);
2382 else
2383 se->class_vptr = NULL_TREE;
2385 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
2386 decl, field, NULL_TREE);
2388 se->expr = tmp;
2390 /* Allocatable deferred char arrays are to be handled by the gfc_deferred_
2391 strlen () conditional below. */
2392 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
2393 && !(c->attr.allocatable && c->ts.deferred))
2395 tmp = c->ts.u.cl->backend_decl;
2396 /* Components must always be constant length. */
2397 gcc_assert (tmp && INTEGER_CST_P (tmp));
2398 se->string_length = tmp;
2401 if (gfc_deferred_strlen (c, &field))
2403 tmp = fold_build3_loc (input_location, COMPONENT_REF,
2404 TREE_TYPE (field),
2405 decl, field, NULL_TREE);
2406 se->string_length = tmp;
2409 if (((c->attr.pointer || c->attr.allocatable)
2410 && (!c->attr.dimension && !c->attr.codimension)
2411 && c->ts.type != BT_CHARACTER)
2412 || c->attr.proc_pointer)
2413 se->expr = build_fold_indirect_ref_loc (input_location,
2414 se->expr);
2418 /* This function deals with component references to components of the
2419 parent type for derived type extensions. */
2420 static void
2421 conv_parent_component_references (gfc_se * se, gfc_ref * ref)
2423 gfc_component *c;
2424 gfc_component *cmp;
2425 gfc_symbol *dt;
2426 gfc_ref parent;
2428 dt = ref->u.c.sym;
2429 c = ref->u.c.component;
2431 /* Return if the component is in the parent type. */
2432 for (cmp = dt->components; cmp; cmp = cmp->next)
2433 if (strcmp (c->name, cmp->name) == 0)
2434 return;
2436 /* Build a gfc_ref to recursively call gfc_conv_component_ref. */
2437 parent.type = REF_COMPONENT;
2438 parent.next = NULL;
2439 parent.u.c.sym = dt;
2440 parent.u.c.component = dt->components;
2442 if (dt->backend_decl == NULL)
2443 gfc_get_derived_type (dt);
2445 /* Build the reference and call self. */
2446 gfc_conv_component_ref (se, &parent);
2447 parent.u.c.sym = dt->components->ts.u.derived;
2448 parent.u.c.component = c;
2449 conv_parent_component_references (se, &parent);
2452 /* Return the contents of a variable. Also handles reference/pointer
2453 variables (all Fortran pointer references are implicit). */
2455 static void
2456 gfc_conv_variable (gfc_se * se, gfc_expr * expr)
2458 gfc_ss *ss;
2459 gfc_ref *ref;
2460 gfc_symbol *sym;
2461 tree parent_decl = NULL_TREE;
2462 int parent_flag;
2463 bool return_value;
2464 bool alternate_entry;
2465 bool entry_master;
2466 bool is_classarray;
2467 bool first_time = true;
2469 sym = expr->symtree->n.sym;
2470 is_classarray = IS_CLASS_ARRAY (sym);
2471 ss = se->ss;
2472 if (ss != NULL)
2474 gfc_ss_info *ss_info = ss->info;
2476 /* Check that something hasn't gone horribly wrong. */
2477 gcc_assert (ss != gfc_ss_terminator);
2478 gcc_assert (ss_info->expr == expr);
2480 /* A scalarized term. We already know the descriptor. */
2481 se->expr = ss_info->data.array.descriptor;
2482 se->string_length = ss_info->string_length;
2483 ref = ss_info->data.array.ref;
2484 if (ref)
2485 gcc_assert (ref->type == REF_ARRAY
2486 && ref->u.ar.type != AR_ELEMENT);
2487 else
2488 gfc_conv_tmp_array_ref (se);
2490 else
2492 tree se_expr = NULL_TREE;
2494 se->expr = gfc_get_symbol_decl (sym);
2496 /* Deal with references to a parent results or entries by storing
2497 the current_function_decl and moving to the parent_decl. */
2498 return_value = sym->attr.function && sym->result == sym;
2499 alternate_entry = sym->attr.function && sym->attr.entry
2500 && sym->result == sym;
2501 entry_master = sym->attr.result
2502 && sym->ns->proc_name->attr.entry_master
2503 && !gfc_return_by_reference (sym->ns->proc_name);
2504 if (current_function_decl)
2505 parent_decl = DECL_CONTEXT (current_function_decl);
2507 if ((se->expr == parent_decl && return_value)
2508 || (sym->ns && sym->ns->proc_name
2509 && parent_decl
2510 && sym->ns->proc_name->backend_decl == parent_decl
2511 && (alternate_entry || entry_master)))
2512 parent_flag = 1;
2513 else
2514 parent_flag = 0;
2516 /* Special case for assigning the return value of a function.
2517 Self recursive functions must have an explicit return value. */
2518 if (return_value && (se->expr == current_function_decl || parent_flag))
2519 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
2521 /* Similarly for alternate entry points. */
2522 else if (alternate_entry
2523 && (sym->ns->proc_name->backend_decl == current_function_decl
2524 || parent_flag))
2526 gfc_entry_list *el = NULL;
2528 for (el = sym->ns->entries; el; el = el->next)
2529 if (sym == el->sym)
2531 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
2532 break;
2536 else if (entry_master
2537 && (sym->ns->proc_name->backend_decl == current_function_decl
2538 || parent_flag))
2539 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
2541 if (se_expr)
2542 se->expr = se_expr;
2544 /* Procedure actual arguments. */
2545 else if (sym->attr.flavor == FL_PROCEDURE
2546 && se->expr != current_function_decl)
2548 if (!sym->attr.dummy && !sym->attr.proc_pointer)
2550 gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
2551 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
2553 return;
2557 /* Dereference the expression, where needed. Since characters
2558 are entirely different from other types, they are treated
2559 separately. */
2560 if (sym->ts.type == BT_CHARACTER)
2562 /* Dereference character pointer dummy arguments
2563 or results. */
2564 if ((sym->attr.pointer || sym->attr.allocatable)
2565 && (sym->attr.dummy
2566 || sym->attr.function
2567 || sym->attr.result))
2568 se->expr = build_fold_indirect_ref_loc (input_location,
2569 se->expr);
2572 else if (!sym->attr.value)
2574 /* Dereference temporaries for class array dummy arguments. */
2575 if (sym->attr.dummy && is_classarray
2576 && GFC_ARRAY_TYPE_P (TREE_TYPE (se->expr)))
2578 if (!se->descriptor_only)
2579 se->expr = GFC_DECL_SAVED_DESCRIPTOR (se->expr);
2581 se->expr = build_fold_indirect_ref_loc (input_location,
2582 se->expr);
2585 /* Dereference non-character scalar dummy arguments. */
2586 if (sym->attr.dummy && !sym->attr.dimension
2587 && !(sym->attr.codimension && sym->attr.allocatable)
2588 && (sym->ts.type != BT_CLASS
2589 || (!CLASS_DATA (sym)->attr.dimension
2590 && !(CLASS_DATA (sym)->attr.codimension
2591 && CLASS_DATA (sym)->attr.allocatable))))
2592 se->expr = build_fold_indirect_ref_loc (input_location,
2593 se->expr);
2595 /* Dereference scalar hidden result. */
2596 if (flag_f2c && sym->ts.type == BT_COMPLEX
2597 && (sym->attr.function || sym->attr.result)
2598 && !sym->attr.dimension && !sym->attr.pointer
2599 && !sym->attr.always_explicit)
2600 se->expr = build_fold_indirect_ref_loc (input_location,
2601 se->expr);
2603 /* Dereference non-character, non-class pointer variables.
2604 These must be dummies, results, or scalars. */
2605 if (!is_classarray
2606 && (sym->attr.pointer || sym->attr.allocatable
2607 || gfc_is_associate_pointer (sym)
2608 || (sym->as && sym->as->type == AS_ASSUMED_RANK))
2609 && (sym->attr.dummy
2610 || sym->attr.function
2611 || sym->attr.result
2612 || (!sym->attr.dimension
2613 && (!sym->attr.codimension || !sym->attr.allocatable))))
2614 se->expr = build_fold_indirect_ref_loc (input_location,
2615 se->expr);
2616 /* Now treat the class array pointer variables accordingly. */
2617 else if (sym->ts.type == BT_CLASS
2618 && sym->attr.dummy
2619 && (CLASS_DATA (sym)->attr.dimension
2620 || CLASS_DATA (sym)->attr.codimension)
2621 && ((CLASS_DATA (sym)->as
2622 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
2623 || CLASS_DATA (sym)->attr.allocatable
2624 || CLASS_DATA (sym)->attr.class_pointer))
2625 se->expr = build_fold_indirect_ref_loc (input_location,
2626 se->expr);
2627 /* And the case where a non-dummy, non-result, non-function,
2628 non-allotable and non-pointer classarray is present. This case was
2629 previously covered by the first if, but with introducing the
2630 condition !is_classarray there, that case has to be covered
2631 explicitly. */
2632 else if (sym->ts.type == BT_CLASS
2633 && !sym->attr.dummy
2634 && !sym->attr.function
2635 && !sym->attr.result
2636 && (CLASS_DATA (sym)->attr.dimension
2637 || CLASS_DATA (sym)->attr.codimension)
2638 && (sym->assoc
2639 || !CLASS_DATA (sym)->attr.allocatable)
2640 && !CLASS_DATA (sym)->attr.class_pointer)
2641 se->expr = build_fold_indirect_ref_loc (input_location,
2642 se->expr);
2645 ref = expr->ref;
2648 /* For character variables, also get the length. */
2649 if (sym->ts.type == BT_CHARACTER)
2651 /* If the character length of an entry isn't set, get the length from
2652 the master function instead. */
2653 if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
2654 se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
2655 else
2656 se->string_length = sym->ts.u.cl->backend_decl;
2657 gcc_assert (se->string_length);
2660 while (ref)
2662 switch (ref->type)
2664 case REF_ARRAY:
2665 /* Return the descriptor if that's what we want and this is an array
2666 section reference. */
2667 if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
2668 return;
2669 /* TODO: Pointers to single elements of array sections, eg elemental subs. */
2670 /* Return the descriptor for array pointers and allocations. */
2671 if (se->want_pointer
2672 && ref->next == NULL && (se->descriptor_only))
2673 return;
2675 gfc_conv_array_ref (se, &ref->u.ar, expr, &expr->where);
2676 /* Return a pointer to an element. */
2677 break;
2679 case REF_COMPONENT:
2680 if (first_time && is_classarray && sym->attr.dummy
2681 && se->descriptor_only
2682 && !CLASS_DATA (sym)->attr.allocatable
2683 && !CLASS_DATA (sym)->attr.class_pointer
2684 && CLASS_DATA (sym)->as
2685 && CLASS_DATA (sym)->as->type != AS_ASSUMED_RANK
2686 && strcmp ("_data", ref->u.c.component->name) == 0)
2687 /* Skip the first ref of a _data component, because for class
2688 arrays that one is already done by introducing a temporary
2689 array descriptor. */
2690 break;
2692 if (ref->u.c.sym->attr.extension)
2693 conv_parent_component_references (se, ref);
2695 gfc_conv_component_ref (se, ref);
2696 if (!ref->next && ref->u.c.sym->attr.codimension
2697 && se->want_pointer && se->descriptor_only)
2698 return;
2700 break;
2702 case REF_SUBSTRING:
2703 gfc_conv_substring (se, ref, expr->ts.kind,
2704 expr->symtree->name, &expr->where);
2705 break;
2707 default:
2708 gcc_unreachable ();
2709 break;
2711 first_time = false;
2712 ref = ref->next;
2714 /* Pointer assignment, allocation or pass by reference. Arrays are handled
2715 separately. */
2716 if (se->want_pointer)
2718 if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr))
2719 gfc_conv_string_parameter (se);
2720 else
2721 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
2726 /* Unary ops are easy... Or they would be if ! was a valid op. */
2728 static void
2729 gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
2731 gfc_se operand;
2732 tree type;
2734 gcc_assert (expr->ts.type != BT_CHARACTER);
2735 /* Initialize the operand. */
2736 gfc_init_se (&operand, se);
2737 gfc_conv_expr_val (&operand, expr->value.op.op1);
2738 gfc_add_block_to_block (&se->pre, &operand.pre);
2740 type = gfc_typenode_for_spec (&expr->ts);
2742 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
2743 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
2744 All other unary operators have an equivalent GIMPLE unary operator. */
2745 if (code == TRUTH_NOT_EXPR)
2746 se->expr = fold_build2_loc (input_location, EQ_EXPR, type, operand.expr,
2747 build_int_cst (type, 0));
2748 else
2749 se->expr = fold_build1_loc (input_location, code, type, operand.expr);
2753 /* Expand power operator to optimal multiplications when a value is raised
2754 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
2755 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
2756 Programming", 3rd Edition, 1998. */
2758 /* This code is mostly duplicated from expand_powi in the backend.
2759 We establish the "optimal power tree" lookup table with the defined size.
2760 The items in the table are the exponents used to calculate the index
2761 exponents. Any integer n less than the value can get an "addition chain",
2762 with the first node being one. */
2763 #define POWI_TABLE_SIZE 256
2765 /* The table is from builtins.c. */
2766 static const unsigned char powi_table[POWI_TABLE_SIZE] =
2768 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
2769 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
2770 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
2771 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
2772 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
2773 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
2774 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
2775 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
2776 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
2777 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
2778 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
2779 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
2780 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
2781 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
2782 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
2783 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
2784 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
2785 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
2786 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
2787 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
2788 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
2789 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
2790 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
2791 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
2792 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
2793 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
2794 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
2795 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
2796 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
2797 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
2798 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
2799 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
2802 /* If n is larger than lookup table's max index, we use the "window
2803 method". */
2804 #define POWI_WINDOW_SIZE 3
2806 /* Recursive function to expand the power operator. The temporary
2807 values are put in tmpvar. The function returns tmpvar[1] ** n. */
2808 static tree
2809 gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
2811 tree op0;
2812 tree op1;
2813 tree tmp;
2814 int digit;
2816 if (n < POWI_TABLE_SIZE)
2818 if (tmpvar[n])
2819 return tmpvar[n];
2821 op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
2822 op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
2824 else if (n & 1)
2826 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
2827 op0 = gfc_conv_powi (se, n - digit, tmpvar);
2828 op1 = gfc_conv_powi (se, digit, tmpvar);
2830 else
2832 op0 = gfc_conv_powi (se, n >> 1, tmpvar);
2833 op1 = op0;
2836 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
2837 tmp = gfc_evaluate_now (tmp, &se->pre);
2839 if (n < POWI_TABLE_SIZE)
2840 tmpvar[n] = tmp;
2842 return tmp;
2846 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
2847 return 1. Else return 0 and a call to runtime library functions
2848 will have to be built. */
2849 static int
2850 gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
2852 tree cond;
2853 tree tmp;
2854 tree type;
2855 tree vartmp[POWI_TABLE_SIZE];
2856 HOST_WIDE_INT m;
2857 unsigned HOST_WIDE_INT n;
2858 int sgn;
2859 wide_int wrhs = rhs;
2861 /* If exponent is too large, we won't expand it anyway, so don't bother
2862 with large integer values. */
2863 if (!wi::fits_shwi_p (wrhs))
2864 return 0;
2866 m = wrhs.to_shwi ();
2867 /* Use the wide_int's routine to reliably get the absolute value on all
2868 platforms. Then convert it to a HOST_WIDE_INT like above. */
2869 n = wi::abs (wrhs).to_shwi ();
2871 type = TREE_TYPE (lhs);
2872 sgn = tree_int_cst_sgn (rhs);
2874 if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
2875 || optimize_size) && (m > 2 || m < -1))
2876 return 0;
2878 /* rhs == 0 */
2879 if (sgn == 0)
2881 se->expr = gfc_build_const (type, integer_one_node);
2882 return 1;
2885 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
2886 if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
2888 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2889 lhs, build_int_cst (TREE_TYPE (lhs), -1));
2890 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2891 lhs, build_int_cst (TREE_TYPE (lhs), 1));
2893 /* If rhs is even,
2894 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
2895 if ((n & 1) == 0)
2897 tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
2898 boolean_type_node, tmp, cond);
2899 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
2900 tmp, build_int_cst (type, 1),
2901 build_int_cst (type, 0));
2902 return 1;
2904 /* If rhs is odd,
2905 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
2906 tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp,
2907 build_int_cst (type, -1),
2908 build_int_cst (type, 0));
2909 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
2910 cond, build_int_cst (type, 1), tmp);
2911 return 1;
2914 memset (vartmp, 0, sizeof (vartmp));
2915 vartmp[1] = lhs;
2916 if (sgn == -1)
2918 tmp = gfc_build_const (type, integer_one_node);
2919 vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
2920 vartmp[1]);
2923 se->expr = gfc_conv_powi (se, n, vartmp);
2925 return 1;
2929 /* Power op (**). Constant integer exponent has special handling. */
2931 static void
2932 gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
2934 tree gfc_int4_type_node;
2935 int kind;
2936 int ikind;
2937 int res_ikind_1, res_ikind_2;
2938 gfc_se lse;
2939 gfc_se rse;
2940 tree fndecl = NULL;
2942 gfc_init_se (&lse, se);
2943 gfc_conv_expr_val (&lse, expr->value.op.op1);
2944 lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
2945 gfc_add_block_to_block (&se->pre, &lse.pre);
2947 gfc_init_se (&rse, se);
2948 gfc_conv_expr_val (&rse, expr->value.op.op2);
2949 gfc_add_block_to_block (&se->pre, &rse.pre);
2951 if (expr->value.op.op2->ts.type == BT_INTEGER
2952 && expr->value.op.op2->expr_type == EXPR_CONSTANT)
2953 if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
2954 return;
2956 gfc_int4_type_node = gfc_get_int_type (4);
2958 /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
2959 library routine. But in the end, we have to convert the result back
2960 if this case applies -- with res_ikind_K, we keep track whether operand K
2961 falls into this case. */
2962 res_ikind_1 = -1;
2963 res_ikind_2 = -1;
2965 kind = expr->value.op.op1->ts.kind;
2966 switch (expr->value.op.op2->ts.type)
2968 case BT_INTEGER:
2969 ikind = expr->value.op.op2->ts.kind;
2970 switch (ikind)
2972 case 1:
2973 case 2:
2974 rse.expr = convert (gfc_int4_type_node, rse.expr);
2975 res_ikind_2 = ikind;
2976 /* Fall through. */
2978 case 4:
2979 ikind = 0;
2980 break;
2982 case 8:
2983 ikind = 1;
2984 break;
2986 case 16:
2987 ikind = 2;
2988 break;
2990 default:
2991 gcc_unreachable ();
2993 switch (kind)
2995 case 1:
2996 case 2:
2997 if (expr->value.op.op1->ts.type == BT_INTEGER)
2999 lse.expr = convert (gfc_int4_type_node, lse.expr);
3000 res_ikind_1 = kind;
3002 else
3003 gcc_unreachable ();
3004 /* Fall through. */
3006 case 4:
3007 kind = 0;
3008 break;
3010 case 8:
3011 kind = 1;
3012 break;
3014 case 10:
3015 kind = 2;
3016 break;
3018 case 16:
3019 kind = 3;
3020 break;
3022 default:
3023 gcc_unreachable ();
3026 switch (expr->value.op.op1->ts.type)
3028 case BT_INTEGER:
3029 if (kind == 3) /* Case 16 was not handled properly above. */
3030 kind = 2;
3031 fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
3032 break;
3034 case BT_REAL:
3035 /* Use builtins for real ** int4. */
3036 if (ikind == 0)
3038 switch (kind)
3040 case 0:
3041 fndecl = builtin_decl_explicit (BUILT_IN_POWIF);
3042 break;
3044 case 1:
3045 fndecl = builtin_decl_explicit (BUILT_IN_POWI);
3046 break;
3048 case 2:
3049 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
3050 break;
3052 case 3:
3053 /* Use the __builtin_powil() only if real(kind=16) is
3054 actually the C long double type. */
3055 if (!gfc_real16_is_float128)
3056 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
3057 break;
3059 default:
3060 gcc_unreachable ();
3064 /* If we don't have a good builtin for this, go for the
3065 library function. */
3066 if (!fndecl)
3067 fndecl = gfor_fndecl_math_powi[kind][ikind].real;
3068 break;
3070 case BT_COMPLEX:
3071 fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
3072 break;
3074 default:
3075 gcc_unreachable ();
3077 break;
3079 case BT_REAL:
3080 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
3081 break;
3083 case BT_COMPLEX:
3084 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
3085 break;
3087 default:
3088 gcc_unreachable ();
3089 break;
3092 se->expr = build_call_expr_loc (input_location,
3093 fndecl, 2, lse.expr, rse.expr);
3095 /* Convert the result back if it is of wrong integer kind. */
3096 if (res_ikind_1 != -1 && res_ikind_2 != -1)
3098 /* We want the maximum of both operand kinds as result. */
3099 if (res_ikind_1 < res_ikind_2)
3100 res_ikind_1 = res_ikind_2;
3101 se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr);
3106 /* Generate code to allocate a string temporary. */
3108 tree
3109 gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
3111 tree var;
3112 tree tmp;
3114 if (gfc_can_put_var_on_stack (len))
3116 /* Create a temporary variable to hold the result. */
3117 tmp = fold_build2_loc (input_location, MINUS_EXPR,
3118 gfc_charlen_type_node, len,
3119 build_int_cst (gfc_charlen_type_node, 1));
3120 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
3122 if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
3123 tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
3124 else
3125 tmp = build_array_type (TREE_TYPE (type), tmp);
3127 var = gfc_create_var (tmp, "str");
3128 var = gfc_build_addr_expr (type, var);
3130 else
3132 /* Allocate a temporary to hold the result. */
3133 var = gfc_create_var (type, "pstr");
3134 gcc_assert (POINTER_TYPE_P (type));
3135 tmp = TREE_TYPE (type);
3136 if (TREE_CODE (tmp) == ARRAY_TYPE)
3137 tmp = TREE_TYPE (tmp);
3138 tmp = TYPE_SIZE_UNIT (tmp);
3139 tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
3140 fold_convert (size_type_node, len),
3141 fold_convert (size_type_node, tmp));
3142 tmp = gfc_call_malloc (&se->pre, type, tmp);
3143 gfc_add_modify (&se->pre, var, tmp);
3145 /* Free the temporary afterwards. */
3146 tmp = gfc_call_free (var);
3147 gfc_add_expr_to_block (&se->post, tmp);
3150 return var;
3154 /* Handle a string concatenation operation. A temporary will be allocated to
3155 hold the result. */
3157 static void
3158 gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
3160 gfc_se lse, rse;
3161 tree len, type, var, tmp, fndecl;
3163 gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
3164 && expr->value.op.op2->ts.type == BT_CHARACTER);
3165 gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
3167 gfc_init_se (&lse, se);
3168 gfc_conv_expr (&lse, expr->value.op.op1);
3169 gfc_conv_string_parameter (&lse);
3170 gfc_init_se (&rse, se);
3171 gfc_conv_expr (&rse, expr->value.op.op2);
3172 gfc_conv_string_parameter (&rse);
3174 gfc_add_block_to_block (&se->pre, &lse.pre);
3175 gfc_add_block_to_block (&se->pre, &rse.pre);
3177 type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
3178 len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
3179 if (len == NULL_TREE)
3181 len = fold_build2_loc (input_location, PLUS_EXPR,
3182 TREE_TYPE (lse.string_length),
3183 lse.string_length, rse.string_length);
3186 type = build_pointer_type (type);
3188 var = gfc_conv_string_tmp (se, type, len);
3190 /* Do the actual concatenation. */
3191 if (expr->ts.kind == 1)
3192 fndecl = gfor_fndecl_concat_string;
3193 else if (expr->ts.kind == 4)
3194 fndecl = gfor_fndecl_concat_string_char4;
3195 else
3196 gcc_unreachable ();
3198 tmp = build_call_expr_loc (input_location,
3199 fndecl, 6, len, var, lse.string_length, lse.expr,
3200 rse.string_length, rse.expr);
3201 gfc_add_expr_to_block (&se->pre, tmp);
3203 /* Add the cleanup for the operands. */
3204 gfc_add_block_to_block (&se->pre, &rse.post);
3205 gfc_add_block_to_block (&se->pre, &lse.post);
3207 se->expr = var;
3208 se->string_length = len;
3211 /* Translates an op expression. Common (binary) cases are handled by this
3212 function, others are passed on. Recursion is used in either case.
3213 We use the fact that (op1.ts == op2.ts) (except for the power
3214 operator **).
3215 Operators need no special handling for scalarized expressions as long as
3216 they call gfc_conv_simple_val to get their operands.
3217 Character strings get special handling. */
3219 static void
3220 gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
3222 enum tree_code code;
3223 gfc_se lse;
3224 gfc_se rse;
3225 tree tmp, type;
3226 int lop;
3227 int checkstring;
3229 checkstring = 0;
3230 lop = 0;
3231 switch (expr->value.op.op)
3233 case INTRINSIC_PARENTHESES:
3234 if ((expr->ts.type == BT_REAL || expr->ts.type == BT_COMPLEX)
3235 && flag_protect_parens)
3237 gfc_conv_unary_op (PAREN_EXPR, se, expr);
3238 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
3239 return;
3242 /* Fallthrough. */
3243 case INTRINSIC_UPLUS:
3244 gfc_conv_expr (se, expr->value.op.op1);
3245 return;
3247 case INTRINSIC_UMINUS:
3248 gfc_conv_unary_op (NEGATE_EXPR, se, expr);
3249 return;
3251 case INTRINSIC_NOT:
3252 gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
3253 return;
3255 case INTRINSIC_PLUS:
3256 code = PLUS_EXPR;
3257 break;
3259 case INTRINSIC_MINUS:
3260 code = MINUS_EXPR;
3261 break;
3263 case INTRINSIC_TIMES:
3264 code = MULT_EXPR;
3265 break;
3267 case INTRINSIC_DIVIDE:
3268 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
3269 an integer, we must round towards zero, so we use a
3270 TRUNC_DIV_EXPR. */
3271 if (expr->ts.type == BT_INTEGER)
3272 code = TRUNC_DIV_EXPR;
3273 else
3274 code = RDIV_EXPR;
3275 break;
3277 case INTRINSIC_POWER:
3278 gfc_conv_power_op (se, expr);
3279 return;
3281 case INTRINSIC_CONCAT:
3282 gfc_conv_concat_op (se, expr);
3283 return;
3285 case INTRINSIC_AND:
3286 code = TRUTH_ANDIF_EXPR;
3287 lop = 1;
3288 break;
3290 case INTRINSIC_OR:
3291 code = TRUTH_ORIF_EXPR;
3292 lop = 1;
3293 break;
3295 /* EQV and NEQV only work on logicals, but since we represent them
3296 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
3297 case INTRINSIC_EQ:
3298 case INTRINSIC_EQ_OS:
3299 case INTRINSIC_EQV:
3300 code = EQ_EXPR;
3301 checkstring = 1;
3302 lop = 1;
3303 break;
3305 case INTRINSIC_NE:
3306 case INTRINSIC_NE_OS:
3307 case INTRINSIC_NEQV:
3308 code = NE_EXPR;
3309 checkstring = 1;
3310 lop = 1;
3311 break;
3313 case INTRINSIC_GT:
3314 case INTRINSIC_GT_OS:
3315 code = GT_EXPR;
3316 checkstring = 1;
3317 lop = 1;
3318 break;
3320 case INTRINSIC_GE:
3321 case INTRINSIC_GE_OS:
3322 code = GE_EXPR;
3323 checkstring = 1;
3324 lop = 1;
3325 break;
3327 case INTRINSIC_LT:
3328 case INTRINSIC_LT_OS:
3329 code = LT_EXPR;
3330 checkstring = 1;
3331 lop = 1;
3332 break;
3334 case INTRINSIC_LE:
3335 case INTRINSIC_LE_OS:
3336 code = LE_EXPR;
3337 checkstring = 1;
3338 lop = 1;
3339 break;
3341 case INTRINSIC_USER:
3342 case INTRINSIC_ASSIGN:
3343 /* These should be converted into function calls by the frontend. */
3344 gcc_unreachable ();
3346 default:
3347 fatal_error (input_location, "Unknown intrinsic op");
3348 return;
3351 /* The only exception to this is **, which is handled separately anyway. */
3352 gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
3354 if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
3355 checkstring = 0;
3357 /* lhs */
3358 gfc_init_se (&lse, se);
3359 gfc_conv_expr (&lse, expr->value.op.op1);
3360 gfc_add_block_to_block (&se->pre, &lse.pre);
3362 /* rhs */
3363 gfc_init_se (&rse, se);
3364 gfc_conv_expr (&rse, expr->value.op.op2);
3365 gfc_add_block_to_block (&se->pre, &rse.pre);
3367 if (checkstring)
3369 gfc_conv_string_parameter (&lse);
3370 gfc_conv_string_parameter (&rse);
3372 lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
3373 rse.string_length, rse.expr,
3374 expr->value.op.op1->ts.kind,
3375 code);
3376 rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
3377 gfc_add_block_to_block (&lse.post, &rse.post);
3380 type = gfc_typenode_for_spec (&expr->ts);
3382 if (lop)
3384 /* The result of logical ops is always boolean_type_node. */
3385 tmp = fold_build2_loc (input_location, code, boolean_type_node,
3386 lse.expr, rse.expr);
3387 se->expr = convert (type, tmp);
3389 else
3390 se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
3392 /* Add the post blocks. */
3393 gfc_add_block_to_block (&se->post, &rse.post);
3394 gfc_add_block_to_block (&se->post, &lse.post);
3397 /* If a string's length is one, we convert it to a single character. */
3399 tree
3400 gfc_string_to_single_character (tree len, tree str, int kind)
3403 if (len == NULL
3404 || !tree_fits_uhwi_p (len)
3405 || !POINTER_TYPE_P (TREE_TYPE (str)))
3406 return NULL_TREE;
3408 if (TREE_INT_CST_LOW (len) == 1)
3410 str = fold_convert (gfc_get_pchar_type (kind), str);
3411 return build_fold_indirect_ref_loc (input_location, str);
3414 if (kind == 1
3415 && TREE_CODE (str) == ADDR_EXPR
3416 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
3417 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
3418 && array_ref_low_bound (TREE_OPERAND (str, 0))
3419 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
3420 && TREE_INT_CST_LOW (len) > 1
3421 && TREE_INT_CST_LOW (len)
3422 == (unsigned HOST_WIDE_INT)
3423 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
3425 tree ret = fold_convert (gfc_get_pchar_type (kind), str);
3426 ret = build_fold_indirect_ref_loc (input_location, ret);
3427 if (TREE_CODE (ret) == INTEGER_CST)
3429 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
3430 int i, length = TREE_STRING_LENGTH (string_cst);
3431 const char *ptr = TREE_STRING_POINTER (string_cst);
3433 for (i = 1; i < length; i++)
3434 if (ptr[i] != ' ')
3435 return NULL_TREE;
3437 return ret;
3441 return NULL_TREE;
3445 void
3446 gfc_conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
3449 if (sym->backend_decl)
3451 /* This becomes the nominal_type in
3452 function.c:assign_parm_find_data_types. */
3453 TREE_TYPE (sym->backend_decl) = unsigned_char_type_node;
3454 /* This becomes the passed_type in
3455 function.c:assign_parm_find_data_types. C promotes char to
3456 integer for argument passing. */
3457 DECL_ARG_TYPE (sym->backend_decl) = unsigned_type_node;
3459 DECL_BY_REFERENCE (sym->backend_decl) = 0;
3462 if (expr != NULL)
3464 /* If we have a constant character expression, make it into an
3465 integer. */
3466 if ((*expr)->expr_type == EXPR_CONSTANT)
3468 gfc_typespec ts;
3469 gfc_clear_ts (&ts);
3471 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL,
3472 (int)(*expr)->value.character.string[0]);
3473 if ((*expr)->ts.kind != gfc_c_int_kind)
3475 /* The expr needs to be compatible with a C int. If the
3476 conversion fails, then the 2 causes an ICE. */
3477 ts.type = BT_INTEGER;
3478 ts.kind = gfc_c_int_kind;
3479 gfc_convert_type (*expr, &ts, 2);
3482 else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
3484 if ((*expr)->ref == NULL)
3486 se->expr = gfc_string_to_single_character
3487 (build_int_cst (integer_type_node, 1),
3488 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
3489 gfc_get_symbol_decl
3490 ((*expr)->symtree->n.sym)),
3491 (*expr)->ts.kind);
3493 else
3495 gfc_conv_variable (se, *expr);
3496 se->expr = gfc_string_to_single_character
3497 (build_int_cst (integer_type_node, 1),
3498 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
3499 se->expr),
3500 (*expr)->ts.kind);
3506 /* Helper function for gfc_build_compare_string. Return LEN_TRIM value
3507 if STR is a string literal, otherwise return -1. */
3509 static int
3510 gfc_optimize_len_trim (tree len, tree str, int kind)
3512 if (kind == 1
3513 && TREE_CODE (str) == ADDR_EXPR
3514 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
3515 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
3516 && array_ref_low_bound (TREE_OPERAND (str, 0))
3517 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
3518 && tree_fits_uhwi_p (len)
3519 && tree_to_uhwi (len) >= 1
3520 && tree_to_uhwi (len)
3521 == (unsigned HOST_WIDE_INT)
3522 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
3524 tree folded = fold_convert (gfc_get_pchar_type (kind), str);
3525 folded = build_fold_indirect_ref_loc (input_location, folded);
3526 if (TREE_CODE (folded) == INTEGER_CST)
3528 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
3529 int length = TREE_STRING_LENGTH (string_cst);
3530 const char *ptr = TREE_STRING_POINTER (string_cst);
3532 for (; length > 0; length--)
3533 if (ptr[length - 1] != ' ')
3534 break;
3536 return length;
3539 return -1;
3542 /* Helper to build a call to memcmp. */
3544 static tree
3545 build_memcmp_call (tree s1, tree s2, tree n)
3547 tree tmp;
3549 if (!POINTER_TYPE_P (TREE_TYPE (s1)))
3550 s1 = gfc_build_addr_expr (pvoid_type_node, s1);
3551 else
3552 s1 = fold_convert (pvoid_type_node, s1);
3554 if (!POINTER_TYPE_P (TREE_TYPE (s2)))
3555 s2 = gfc_build_addr_expr (pvoid_type_node, s2);
3556 else
3557 s2 = fold_convert (pvoid_type_node, s2);
3559 n = fold_convert (size_type_node, n);
3561 tmp = build_call_expr_loc (input_location,
3562 builtin_decl_explicit (BUILT_IN_MEMCMP),
3563 3, s1, s2, n);
3565 return fold_convert (integer_type_node, tmp);
3568 /* Compare two strings. If they are all single characters, the result is the
3569 subtraction of them. Otherwise, we build a library call. */
3571 tree
3572 gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
3573 enum tree_code code)
3575 tree sc1;
3576 tree sc2;
3577 tree fndecl;
3579 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
3580 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
3582 sc1 = gfc_string_to_single_character (len1, str1, kind);
3583 sc2 = gfc_string_to_single_character (len2, str2, kind);
3585 if (sc1 != NULL_TREE && sc2 != NULL_TREE)
3587 /* Deal with single character specially. */
3588 sc1 = fold_convert (integer_type_node, sc1);
3589 sc2 = fold_convert (integer_type_node, sc2);
3590 return fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
3591 sc1, sc2);
3594 if ((code == EQ_EXPR || code == NE_EXPR)
3595 && optimize
3596 && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
3598 /* If one string is a string literal with LEN_TRIM longer
3599 than the length of the second string, the strings
3600 compare unequal. */
3601 int len = gfc_optimize_len_trim (len1, str1, kind);
3602 if (len > 0 && compare_tree_int (len2, len) < 0)
3603 return integer_one_node;
3604 len = gfc_optimize_len_trim (len2, str2, kind);
3605 if (len > 0 && compare_tree_int (len1, len) < 0)
3606 return integer_one_node;
3609 /* We can compare via memcpy if the strings are known to be equal
3610 in length and they are
3611 - kind=1
3612 - kind=4 and the comparison is for (in)equality. */
3614 if (INTEGER_CST_P (len1) && INTEGER_CST_P (len2)
3615 && tree_int_cst_equal (len1, len2)
3616 && (kind == 1 || code == EQ_EXPR || code == NE_EXPR))
3618 tree tmp;
3619 tree chartype;
3621 chartype = gfc_get_char_type (kind);
3622 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE(len1),
3623 fold_convert (TREE_TYPE(len1),
3624 TYPE_SIZE_UNIT(chartype)),
3625 len1);
3626 return build_memcmp_call (str1, str2, tmp);
3629 /* Build a call for the comparison. */
3630 if (kind == 1)
3631 fndecl = gfor_fndecl_compare_string;
3632 else if (kind == 4)
3633 fndecl = gfor_fndecl_compare_string_char4;
3634 else
3635 gcc_unreachable ();
3637 return build_call_expr_loc (input_location, fndecl, 4,
3638 len1, str1, len2, str2);
3642 /* Return the backend_decl for a procedure pointer component. */
3644 static tree
3645 get_proc_ptr_comp (gfc_expr *e)
3647 gfc_se comp_se;
3648 gfc_expr *e2;
3649 expr_t old_type;
3651 gfc_init_se (&comp_se, NULL);
3652 e2 = gfc_copy_expr (e);
3653 /* We have to restore the expr type later so that gfc_free_expr frees
3654 the exact same thing that was allocated.
3655 TODO: This is ugly. */
3656 old_type = e2->expr_type;
3657 e2->expr_type = EXPR_VARIABLE;
3658 gfc_conv_expr (&comp_se, e2);
3659 e2->expr_type = old_type;
3660 gfc_free_expr (e2);
3661 return build_fold_addr_expr_loc (input_location, comp_se.expr);
3665 /* Convert a typebound function reference from a class object. */
3666 static void
3667 conv_base_obj_fcn_val (gfc_se * se, tree base_object, gfc_expr * expr)
3669 gfc_ref *ref;
3670 tree var;
3672 if (!VAR_P (base_object))
3674 var = gfc_create_var (TREE_TYPE (base_object), NULL);
3675 gfc_add_modify (&se->pre, var, base_object);
3677 se->expr = gfc_class_vptr_get (base_object);
3678 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
3679 ref = expr->ref;
3680 while (ref && ref->next)
3681 ref = ref->next;
3682 gcc_assert (ref && ref->type == REF_COMPONENT);
3683 if (ref->u.c.sym->attr.extension)
3684 conv_parent_component_references (se, ref);
3685 gfc_conv_component_ref (se, ref);
3686 se->expr = build_fold_addr_expr_loc (input_location, se->expr);
3690 static void
3691 conv_function_val (gfc_se * se, gfc_symbol * sym, gfc_expr * expr)
3693 tree tmp;
3695 if (gfc_is_proc_ptr_comp (expr))
3696 tmp = get_proc_ptr_comp (expr);
3697 else if (sym->attr.dummy)
3699 tmp = gfc_get_symbol_decl (sym);
3700 if (sym->attr.proc_pointer)
3701 tmp = build_fold_indirect_ref_loc (input_location,
3702 tmp);
3703 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
3704 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
3706 else
3708 if (!sym->backend_decl)
3709 sym->backend_decl = gfc_get_extern_function_decl (sym);
3711 TREE_USED (sym->backend_decl) = 1;
3713 tmp = sym->backend_decl;
3715 if (sym->attr.cray_pointee)
3717 /* TODO - make the cray pointee a pointer to a procedure,
3718 assign the pointer to it and use it for the call. This
3719 will do for now! */
3720 tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
3721 gfc_get_symbol_decl (sym->cp_pointer));
3722 tmp = gfc_evaluate_now (tmp, &se->pre);
3725 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
3727 gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
3728 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
3731 se->expr = tmp;
3735 /* Initialize MAPPING. */
3737 void
3738 gfc_init_interface_mapping (gfc_interface_mapping * mapping)
3740 mapping->syms = NULL;
3741 mapping->charlens = NULL;
3745 /* Free all memory held by MAPPING (but not MAPPING itself). */
3747 void
3748 gfc_free_interface_mapping (gfc_interface_mapping * mapping)
3750 gfc_interface_sym_mapping *sym;
3751 gfc_interface_sym_mapping *nextsym;
3752 gfc_charlen *cl;
3753 gfc_charlen *nextcl;
3755 for (sym = mapping->syms; sym; sym = nextsym)
3757 nextsym = sym->next;
3758 sym->new_sym->n.sym->formal = NULL;
3759 gfc_free_symbol (sym->new_sym->n.sym);
3760 gfc_free_expr (sym->expr);
3761 free (sym->new_sym);
3762 free (sym);
3764 for (cl = mapping->charlens; cl; cl = nextcl)
3766 nextcl = cl->next;
3767 gfc_free_expr (cl->length);
3768 free (cl);
3773 /* Return a copy of gfc_charlen CL. Add the returned structure to
3774 MAPPING so that it will be freed by gfc_free_interface_mapping. */
3776 static gfc_charlen *
3777 gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
3778 gfc_charlen * cl)
3780 gfc_charlen *new_charlen;
3782 new_charlen = gfc_get_charlen ();
3783 new_charlen->next = mapping->charlens;
3784 new_charlen->length = gfc_copy_expr (cl->length);
3786 mapping->charlens = new_charlen;
3787 return new_charlen;
3791 /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
3792 array variable that can be used as the actual argument for dummy
3793 argument SYM. Add any initialization code to BLOCK. PACKED is as
3794 for gfc_get_nodesc_array_type and DATA points to the first element
3795 in the passed array. */
3797 static tree
3798 gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
3799 gfc_packed packed, tree data)
3801 tree type;
3802 tree var;
3804 type = gfc_typenode_for_spec (&sym->ts);
3805 type = gfc_get_nodesc_array_type (type, sym->as, packed,
3806 !sym->attr.target && !sym->attr.pointer
3807 && !sym->attr.proc_pointer);
3809 var = gfc_create_var (type, "ifm");
3810 gfc_add_modify (block, var, fold_convert (type, data));
3812 return var;
3816 /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
3817 and offset of descriptorless array type TYPE given that it has the same
3818 size as DESC. Add any set-up code to BLOCK. */
3820 static void
3821 gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
3823 int n;
3824 tree dim;
3825 tree offset;
3826 tree tmp;
3828 offset = gfc_index_zero_node;
3829 for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
3831 dim = gfc_rank_cst[n];
3832 GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
3833 if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
3835 GFC_TYPE_ARRAY_LBOUND (type, n)
3836 = gfc_conv_descriptor_lbound_get (desc, dim);
3837 GFC_TYPE_ARRAY_UBOUND (type, n)
3838 = gfc_conv_descriptor_ubound_get (desc, dim);
3840 else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
3842 tmp = fold_build2_loc (input_location, MINUS_EXPR,
3843 gfc_array_index_type,
3844 gfc_conv_descriptor_ubound_get (desc, dim),
3845 gfc_conv_descriptor_lbound_get (desc, dim));
3846 tmp = fold_build2_loc (input_location, PLUS_EXPR,
3847 gfc_array_index_type,
3848 GFC_TYPE_ARRAY_LBOUND (type, n), tmp);
3849 tmp = gfc_evaluate_now (tmp, block);
3850 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
3852 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
3853 GFC_TYPE_ARRAY_LBOUND (type, n),
3854 GFC_TYPE_ARRAY_STRIDE (type, n));
3855 offset = fold_build2_loc (input_location, MINUS_EXPR,
3856 gfc_array_index_type, offset, tmp);
3858 offset = gfc_evaluate_now (offset, block);
3859 GFC_TYPE_ARRAY_OFFSET (type) = offset;
3863 /* Extend MAPPING so that it maps dummy argument SYM to the value stored
3864 in SE. The caller may still use se->expr and se->string_length after
3865 calling this function. */
3867 void
3868 gfc_add_interface_mapping (gfc_interface_mapping * mapping,
3869 gfc_symbol * sym, gfc_se * se,
3870 gfc_expr *expr)
3872 gfc_interface_sym_mapping *sm;
3873 tree desc;
3874 tree tmp;
3875 tree value;
3876 gfc_symbol *new_sym;
3877 gfc_symtree *root;
3878 gfc_symtree *new_symtree;
3880 /* Create a new symbol to represent the actual argument. */
3881 new_sym = gfc_new_symbol (sym->name, NULL);
3882 new_sym->ts = sym->ts;
3883 new_sym->as = gfc_copy_array_spec (sym->as);
3884 new_sym->attr.referenced = 1;
3885 new_sym->attr.dimension = sym->attr.dimension;
3886 new_sym->attr.contiguous = sym->attr.contiguous;
3887 new_sym->attr.codimension = sym->attr.codimension;
3888 new_sym->attr.pointer = sym->attr.pointer;
3889 new_sym->attr.allocatable = sym->attr.allocatable;
3890 new_sym->attr.flavor = sym->attr.flavor;
3891 new_sym->attr.function = sym->attr.function;
3893 /* Ensure that the interface is available and that
3894 descriptors are passed for array actual arguments. */
3895 if (sym->attr.flavor == FL_PROCEDURE)
3897 new_sym->formal = expr->symtree->n.sym->formal;
3898 new_sym->attr.always_explicit
3899 = expr->symtree->n.sym->attr.always_explicit;
3902 /* Create a fake symtree for it. */
3903 root = NULL;
3904 new_symtree = gfc_new_symtree (&root, sym->name);
3905 new_symtree->n.sym = new_sym;
3906 gcc_assert (new_symtree == root);
3908 /* Create a dummy->actual mapping. */
3909 sm = XCNEW (gfc_interface_sym_mapping);
3910 sm->next = mapping->syms;
3911 sm->old = sym;
3912 sm->new_sym = new_symtree;
3913 sm->expr = gfc_copy_expr (expr);
3914 mapping->syms = sm;
3916 /* Stabilize the argument's value. */
3917 if (!sym->attr.function && se)
3918 se->expr = gfc_evaluate_now (se->expr, &se->pre);
3920 if (sym->ts.type == BT_CHARACTER)
3922 /* Create a copy of the dummy argument's length. */
3923 new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
3924 sm->expr->ts.u.cl = new_sym->ts.u.cl;
3926 /* If the length is specified as "*", record the length that
3927 the caller is passing. We should use the callee's length
3928 in all other cases. */
3929 if (!new_sym->ts.u.cl->length && se)
3931 se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
3932 new_sym->ts.u.cl->backend_decl = se->string_length;
3936 if (!se)
3937 return;
3939 /* Use the passed value as-is if the argument is a function. */
3940 if (sym->attr.flavor == FL_PROCEDURE)
3941 value = se->expr;
3943 /* If the argument is a pass-by-value scalar, use the value as is. */
3944 else if (!sym->attr.dimension && sym->attr.value)
3945 value = se->expr;
3947 /* If the argument is either a string or a pointer to a string,
3948 convert it to a boundless character type. */
3949 else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
3951 tmp = gfc_get_character_type_len (sym->ts.kind, NULL);
3952 tmp = build_pointer_type (tmp);
3953 if (sym->attr.pointer)
3954 value = build_fold_indirect_ref_loc (input_location,
3955 se->expr);
3956 else
3957 value = se->expr;
3958 value = fold_convert (tmp, value);
3961 /* If the argument is a scalar, a pointer to an array or an allocatable,
3962 dereference it. */
3963 else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
3964 value = build_fold_indirect_ref_loc (input_location,
3965 se->expr);
3967 /* For character(*), use the actual argument's descriptor. */
3968 else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
3969 value = build_fold_indirect_ref_loc (input_location,
3970 se->expr);
3972 /* If the argument is an array descriptor, use it to determine
3973 information about the actual argument's shape. */
3974 else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
3975 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
3977 /* Get the actual argument's descriptor. */
3978 desc = build_fold_indirect_ref_loc (input_location,
3979 se->expr);
3981 /* Create the replacement variable. */
3982 tmp = gfc_conv_descriptor_data_get (desc);
3983 value = gfc_get_interface_mapping_array (&se->pre, sym,
3984 PACKED_NO, tmp);
3986 /* Use DESC to work out the upper bounds, strides and offset. */
3987 gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
3989 else
3990 /* Otherwise we have a packed array. */
3991 value = gfc_get_interface_mapping_array (&se->pre, sym,
3992 PACKED_FULL, se->expr);
3994 new_sym->backend_decl = value;
3998 /* Called once all dummy argument mappings have been added to MAPPING,
3999 but before the mapping is used to evaluate expressions. Pre-evaluate
4000 the length of each argument, adding any initialization code to PRE and
4001 any finalization code to POST. */
4003 void
4004 gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
4005 stmtblock_t * pre, stmtblock_t * post)
4007 gfc_interface_sym_mapping *sym;
4008 gfc_expr *expr;
4009 gfc_se se;
4011 for (sym = mapping->syms; sym; sym = sym->next)
4012 if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
4013 && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
4015 expr = sym->new_sym->n.sym->ts.u.cl->length;
4016 gfc_apply_interface_mapping_to_expr (mapping, expr);
4017 gfc_init_se (&se, NULL);
4018 gfc_conv_expr (&se, expr);
4019 se.expr = fold_convert (gfc_charlen_type_node, se.expr);
4020 se.expr = gfc_evaluate_now (se.expr, &se.pre);
4021 gfc_add_block_to_block (pre, &se.pre);
4022 gfc_add_block_to_block (post, &se.post);
4024 sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
4029 /* Like gfc_apply_interface_mapping_to_expr, but applied to
4030 constructor C. */
4032 static void
4033 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
4034 gfc_constructor_base base)
4036 gfc_constructor *c;
4037 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
4039 gfc_apply_interface_mapping_to_expr (mapping, c->expr);
4040 if (c->iterator)
4042 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
4043 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
4044 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
4050 /* Like gfc_apply_interface_mapping_to_expr, but applied to
4051 reference REF. */
4053 static void
4054 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
4055 gfc_ref * ref)
4057 int n;
4059 for (; ref; ref = ref->next)
4060 switch (ref->type)
4062 case REF_ARRAY:
4063 for (n = 0; n < ref->u.ar.dimen; n++)
4065 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
4066 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
4067 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
4069 break;
4071 case REF_COMPONENT:
4072 break;
4074 case REF_SUBSTRING:
4075 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
4076 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
4077 break;
4082 /* Convert intrinsic function calls into result expressions. */
4084 static bool
4085 gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
4087 gfc_symbol *sym;
4088 gfc_expr *new_expr;
4089 gfc_expr *arg1;
4090 gfc_expr *arg2;
4091 int d, dup;
4093 arg1 = expr->value.function.actual->expr;
4094 if (expr->value.function.actual->next)
4095 arg2 = expr->value.function.actual->next->expr;
4096 else
4097 arg2 = NULL;
4099 sym = arg1->symtree->n.sym;
4101 if (sym->attr.dummy)
4102 return false;
4104 new_expr = NULL;
4106 switch (expr->value.function.isym->id)
4108 case GFC_ISYM_LEN:
4109 /* TODO figure out why this condition is necessary. */
4110 if (sym->attr.function
4111 && (arg1->ts.u.cl->length == NULL
4112 || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
4113 && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
4114 return false;
4116 new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
4117 break;
4119 case GFC_ISYM_LEN_TRIM:
4120 new_expr = gfc_copy_expr (arg1);
4121 gfc_apply_interface_mapping_to_expr (mapping, new_expr);
4123 if (!new_expr)
4124 return false;
4126 gfc_replace_expr (arg1, new_expr);
4127 return true;
4129 case GFC_ISYM_SIZE:
4130 if (!sym->as || sym->as->rank == 0)
4131 return false;
4133 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
4135 dup = mpz_get_si (arg2->value.integer);
4136 d = dup - 1;
4138 else
4140 dup = sym->as->rank;
4141 d = 0;
4144 for (; d < dup; d++)
4146 gfc_expr *tmp;
4148 if (!sym->as->upper[d] || !sym->as->lower[d])
4150 gfc_free_expr (new_expr);
4151 return false;
4154 tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
4155 gfc_get_int_expr (gfc_default_integer_kind,
4156 NULL, 1));
4157 tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
4158 if (new_expr)
4159 new_expr = gfc_multiply (new_expr, tmp);
4160 else
4161 new_expr = tmp;
4163 break;
4165 case GFC_ISYM_LBOUND:
4166 case GFC_ISYM_UBOUND:
4167 /* TODO These implementations of lbound and ubound do not limit if
4168 the size < 0, according to F95's 13.14.53 and 13.14.113. */
4170 if (!sym->as || sym->as->rank == 0)
4171 return false;
4173 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
4174 d = mpz_get_si (arg2->value.integer) - 1;
4175 else
4176 /* TODO: If the need arises, this could produce an array of
4177 ubound/lbounds. */
4178 gcc_unreachable ();
4180 if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
4182 if (sym->as->lower[d])
4183 new_expr = gfc_copy_expr (sym->as->lower[d]);
4185 else
4187 if (sym->as->upper[d])
4188 new_expr = gfc_copy_expr (sym->as->upper[d]);
4190 break;
4192 default:
4193 break;
4196 gfc_apply_interface_mapping_to_expr (mapping, new_expr);
4197 if (!new_expr)
4198 return false;
4200 gfc_replace_expr (expr, new_expr);
4201 return true;
4205 static void
4206 gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
4207 gfc_interface_mapping * mapping)
4209 gfc_formal_arglist *f;
4210 gfc_actual_arglist *actual;
4212 actual = expr->value.function.actual;
4213 f = gfc_sym_get_dummy_args (map_expr->symtree->n.sym);
4215 for (; f && actual; f = f->next, actual = actual->next)
4217 if (!actual->expr)
4218 continue;
4220 gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
4223 if (map_expr->symtree->n.sym->attr.dimension)
4225 int d;
4226 gfc_array_spec *as;
4228 as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
4230 for (d = 0; d < as->rank; d++)
4232 gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
4233 gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
4236 expr->value.function.esym->as = as;
4239 if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
4241 expr->value.function.esym->ts.u.cl->length
4242 = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
4244 gfc_apply_interface_mapping_to_expr (mapping,
4245 expr->value.function.esym->ts.u.cl->length);
4250 /* EXPR is a copy of an expression that appeared in the interface
4251 associated with MAPPING. Walk it recursively looking for references to
4252 dummy arguments that MAPPING maps to actual arguments. Replace each such
4253 reference with a reference to the associated actual argument. */
4255 static void
4256 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
4257 gfc_expr * expr)
4259 gfc_interface_sym_mapping *sym;
4260 gfc_actual_arglist *actual;
4262 if (!expr)
4263 return;
4265 /* Copying an expression does not copy its length, so do that here. */
4266 if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
4268 expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
4269 gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
4272 /* Apply the mapping to any references. */
4273 gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
4275 /* ...and to the expression's symbol, if it has one. */
4276 /* TODO Find out why the condition on expr->symtree had to be moved into
4277 the loop rather than being outside it, as originally. */
4278 for (sym = mapping->syms; sym; sym = sym->next)
4279 if (expr->symtree && sym->old == expr->symtree->n.sym)
4281 if (sym->new_sym->n.sym->backend_decl)
4282 expr->symtree = sym->new_sym;
4283 else if (sym->expr)
4284 gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
4287 /* ...and to subexpressions in expr->value. */
4288 switch (expr->expr_type)
4290 case EXPR_VARIABLE:
4291 case EXPR_CONSTANT:
4292 case EXPR_NULL:
4293 case EXPR_SUBSTRING:
4294 break;
4296 case EXPR_OP:
4297 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
4298 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
4299 break;
4301 case EXPR_FUNCTION:
4302 for (actual = expr->value.function.actual; actual; actual = actual->next)
4303 gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
4305 if (expr->value.function.esym == NULL
4306 && expr->value.function.isym != NULL
4307 && expr->value.function.actual->expr->symtree
4308 && gfc_map_intrinsic_function (expr, mapping))
4309 break;
4311 for (sym = mapping->syms; sym; sym = sym->next)
4312 if (sym->old == expr->value.function.esym)
4314 expr->value.function.esym = sym->new_sym->n.sym;
4315 gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
4316 expr->value.function.esym->result = sym->new_sym->n.sym;
4318 break;
4320 case EXPR_ARRAY:
4321 case EXPR_STRUCTURE:
4322 gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
4323 break;
4325 case EXPR_COMPCALL:
4326 case EXPR_PPC:
4327 gcc_unreachable ();
4328 break;
4331 return;
4335 /* Evaluate interface expression EXPR using MAPPING. Store the result
4336 in SE. */
4338 void
4339 gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
4340 gfc_se * se, gfc_expr * expr)
4342 expr = gfc_copy_expr (expr);
4343 gfc_apply_interface_mapping_to_expr (mapping, expr);
4344 gfc_conv_expr (se, expr);
4345 se->expr = gfc_evaluate_now (se->expr, &se->pre);
4346 gfc_free_expr (expr);
4350 /* Returns a reference to a temporary array into which a component of
4351 an actual argument derived type array is copied and then returned
4352 after the function call. */
4353 void
4354 gfc_conv_subref_array_arg (gfc_se * parmse, gfc_expr * expr, int g77,
4355 sym_intent intent, bool formal_ptr)
4357 gfc_se lse;
4358 gfc_se rse;
4359 gfc_ss *lss;
4360 gfc_ss *rss;
4361 gfc_loopinfo loop;
4362 gfc_loopinfo loop2;
4363 gfc_array_info *info;
4364 tree offset;
4365 tree tmp_index;
4366 tree tmp;
4367 tree base_type;
4368 tree size;
4369 stmtblock_t body;
4370 int n;
4371 int dimen;
4373 gfc_init_se (&lse, NULL);
4374 gfc_init_se (&rse, NULL);
4376 /* Walk the argument expression. */
4377 rss = gfc_walk_expr (expr);
4379 gcc_assert (rss != gfc_ss_terminator);
4381 /* Initialize the scalarizer. */
4382 gfc_init_loopinfo (&loop);
4383 gfc_add_ss_to_loop (&loop, rss);
4385 /* Calculate the bounds of the scalarization. */
4386 gfc_conv_ss_startstride (&loop);
4388 /* Build an ss for the temporary. */
4389 if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
4390 gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
4392 base_type = gfc_typenode_for_spec (&expr->ts);
4393 if (GFC_ARRAY_TYPE_P (base_type)
4394 || GFC_DESCRIPTOR_TYPE_P (base_type))
4395 base_type = gfc_get_element_type (base_type);
4397 if (expr->ts.type == BT_CLASS)
4398 base_type = gfc_typenode_for_spec (&CLASS_DATA (expr)->ts);
4400 loop.temp_ss = gfc_get_temp_ss (base_type, ((expr->ts.type == BT_CHARACTER)
4401 ? expr->ts.u.cl->backend_decl
4402 : NULL),
4403 loop.dimen);
4405 parmse->string_length = loop.temp_ss->info->string_length;
4407 /* Associate the SS with the loop. */
4408 gfc_add_ss_to_loop (&loop, loop.temp_ss);
4410 /* Setup the scalarizing loops. */
4411 gfc_conv_loop_setup (&loop, &expr->where);
4413 /* Pass the temporary descriptor back to the caller. */
4414 info = &loop.temp_ss->info->data.array;
4415 parmse->expr = info->descriptor;
4417 /* Setup the gfc_se structures. */
4418 gfc_copy_loopinfo_to_se (&lse, &loop);
4419 gfc_copy_loopinfo_to_se (&rse, &loop);
4421 rse.ss = rss;
4422 lse.ss = loop.temp_ss;
4423 gfc_mark_ss_chain_used (rss, 1);
4424 gfc_mark_ss_chain_used (loop.temp_ss, 1);
4426 /* Start the scalarized loop body. */
4427 gfc_start_scalarized_body (&loop, &body);
4429 /* Translate the expression. */
4430 gfc_conv_expr (&rse, expr);
4432 /* Reset the offset for the function call since the loop
4433 is zero based on the data pointer. Note that the temp
4434 comes first in the loop chain since it is added second. */
4435 if (gfc_is_alloc_class_array_function (expr))
4437 tmp = loop.ss->loop_chain->info->data.array.descriptor;
4438 gfc_conv_descriptor_offset_set (&loop.pre, tmp,
4439 gfc_index_zero_node);
4442 gfc_conv_tmp_array_ref (&lse);
4444 if (intent != INTENT_OUT)
4446 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false);
4447 gfc_add_expr_to_block (&body, tmp);
4448 gcc_assert (rse.ss == gfc_ss_terminator);
4449 gfc_trans_scalarizing_loops (&loop, &body);
4451 else
4453 /* Make sure that the temporary declaration survives by merging
4454 all the loop declarations into the current context. */
4455 for (n = 0; n < loop.dimen; n++)
4457 gfc_merge_block_scope (&body);
4458 body = loop.code[loop.order[n]];
4460 gfc_merge_block_scope (&body);
4463 /* Add the post block after the second loop, so that any
4464 freeing of allocated memory is done at the right time. */
4465 gfc_add_block_to_block (&parmse->pre, &loop.pre);
4467 /**********Copy the temporary back again.*********/
4469 gfc_init_se (&lse, NULL);
4470 gfc_init_se (&rse, NULL);
4472 /* Walk the argument expression. */
4473 lss = gfc_walk_expr (expr);
4474 rse.ss = loop.temp_ss;
4475 lse.ss = lss;
4477 /* Initialize the scalarizer. */
4478 gfc_init_loopinfo (&loop2);
4479 gfc_add_ss_to_loop (&loop2, lss);
4481 dimen = rse.ss->dimen;
4483 /* Skip the write-out loop for this case. */
4484 if (gfc_is_alloc_class_array_function (expr))
4485 goto class_array_fcn;
4487 /* Calculate the bounds of the scalarization. */
4488 gfc_conv_ss_startstride (&loop2);
4490 /* Setup the scalarizing loops. */
4491 gfc_conv_loop_setup (&loop2, &expr->where);
4493 gfc_copy_loopinfo_to_se (&lse, &loop2);
4494 gfc_copy_loopinfo_to_se (&rse, &loop2);
4496 gfc_mark_ss_chain_used (lss, 1);
4497 gfc_mark_ss_chain_used (loop.temp_ss, 1);
4499 /* Declare the variable to hold the temporary offset and start the
4500 scalarized loop body. */
4501 offset = gfc_create_var (gfc_array_index_type, NULL);
4502 gfc_start_scalarized_body (&loop2, &body);
4504 /* Build the offsets for the temporary from the loop variables. The
4505 temporary array has lbounds of zero and strides of one in all
4506 dimensions, so this is very simple. The offset is only computed
4507 outside the innermost loop, so the overall transfer could be
4508 optimized further. */
4509 info = &rse.ss->info->data.array;
4511 tmp_index = gfc_index_zero_node;
4512 for (n = dimen - 1; n > 0; n--)
4514 tree tmp_str;
4515 tmp = rse.loop->loopvar[n];
4516 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
4517 tmp, rse.loop->from[n]);
4518 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4519 tmp, tmp_index);
4521 tmp_str = fold_build2_loc (input_location, MINUS_EXPR,
4522 gfc_array_index_type,
4523 rse.loop->to[n-1], rse.loop->from[n-1]);
4524 tmp_str = fold_build2_loc (input_location, PLUS_EXPR,
4525 gfc_array_index_type,
4526 tmp_str, gfc_index_one_node);
4528 tmp_index = fold_build2_loc (input_location, MULT_EXPR,
4529 gfc_array_index_type, tmp, tmp_str);
4532 tmp_index = fold_build2_loc (input_location, MINUS_EXPR,
4533 gfc_array_index_type,
4534 tmp_index, rse.loop->from[0]);
4535 gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
4537 tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
4538 gfc_array_index_type,
4539 rse.loop->loopvar[0], offset);
4541 /* Now use the offset for the reference. */
4542 tmp = build_fold_indirect_ref_loc (input_location,
4543 info->data);
4544 rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
4546 if (expr->ts.type == BT_CHARACTER)
4547 rse.string_length = expr->ts.u.cl->backend_decl;
4549 gfc_conv_expr (&lse, expr);
4551 gcc_assert (lse.ss == gfc_ss_terminator);
4553 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, true);
4554 gfc_add_expr_to_block (&body, tmp);
4556 /* Generate the copying loops. */
4557 gfc_trans_scalarizing_loops (&loop2, &body);
4559 /* Wrap the whole thing up by adding the second loop to the post-block
4560 and following it by the post-block of the first loop. In this way,
4561 if the temporary needs freeing, it is done after use! */
4562 if (intent != INTENT_IN)
4564 gfc_add_block_to_block (&parmse->post, &loop2.pre);
4565 gfc_add_block_to_block (&parmse->post, &loop2.post);
4568 class_array_fcn:
4570 gfc_add_block_to_block (&parmse->post, &loop.post);
4572 gfc_cleanup_loop (&loop);
4573 gfc_cleanup_loop (&loop2);
4575 /* Pass the string length to the argument expression. */
4576 if (expr->ts.type == BT_CHARACTER)
4577 parmse->string_length = expr->ts.u.cl->backend_decl;
4579 /* Determine the offset for pointer formal arguments and set the
4580 lbounds to one. */
4581 if (formal_ptr)
4583 size = gfc_index_one_node;
4584 offset = gfc_index_zero_node;
4585 for (n = 0; n < dimen; n++)
4587 tmp = gfc_conv_descriptor_ubound_get (parmse->expr,
4588 gfc_rank_cst[n]);
4589 tmp = fold_build2_loc (input_location, PLUS_EXPR,
4590 gfc_array_index_type, tmp,
4591 gfc_index_one_node);
4592 gfc_conv_descriptor_ubound_set (&parmse->pre,
4593 parmse->expr,
4594 gfc_rank_cst[n],
4595 tmp);
4596 gfc_conv_descriptor_lbound_set (&parmse->pre,
4597 parmse->expr,
4598 gfc_rank_cst[n],
4599 gfc_index_one_node);
4600 size = gfc_evaluate_now (size, &parmse->pre);
4601 offset = fold_build2_loc (input_location, MINUS_EXPR,
4602 gfc_array_index_type,
4603 offset, size);
4604 offset = gfc_evaluate_now (offset, &parmse->pre);
4605 tmp = fold_build2_loc (input_location, MINUS_EXPR,
4606 gfc_array_index_type,
4607 rse.loop->to[n], rse.loop->from[n]);
4608 tmp = fold_build2_loc (input_location, PLUS_EXPR,
4609 gfc_array_index_type,
4610 tmp, gfc_index_one_node);
4611 size = fold_build2_loc (input_location, MULT_EXPR,
4612 gfc_array_index_type, size, tmp);
4615 gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
4616 offset);
4619 /* We want either the address for the data or the address of the descriptor,
4620 depending on the mode of passing array arguments. */
4621 if (g77)
4622 parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
4623 else
4624 parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
4626 return;
4630 /* Generate the code for argument list functions. */
4632 static void
4633 conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
4635 /* Pass by value for g77 %VAL(arg), pass the address
4636 indirectly for %LOC, else by reference. Thus %REF
4637 is a "do-nothing" and %LOC is the same as an F95
4638 pointer. */
4639 if (strncmp (name, "%VAL", 4) == 0)
4640 gfc_conv_expr (se, expr);
4641 else if (strncmp (name, "%LOC", 4) == 0)
4643 gfc_conv_expr_reference (se, expr);
4644 se->expr = gfc_build_addr_expr (NULL, se->expr);
4646 else if (strncmp (name, "%REF", 4) == 0)
4647 gfc_conv_expr_reference (se, expr);
4648 else
4649 gfc_error ("Unknown argument list function at %L", &expr->where);
4653 /* This function tells whether the middle-end representation of the expression
4654 E given as input may point to data otherwise accessible through a variable
4655 (sub-)reference.
4656 It is assumed that the only expressions that may alias are variables,
4657 and array constructors if ARRAY_MAY_ALIAS is true and some of its elements
4658 may alias.
4659 This function is used to decide whether freeing an expression's allocatable
4660 components is safe or should be avoided.
4662 If ARRAY_MAY_ALIAS is true, an array constructor may alias if some of
4663 its elements are copied from a variable. This ARRAY_MAY_ALIAS trick
4664 is necessary because for array constructors, aliasing depends on how
4665 the array is used:
4666 - If E is an array constructor used as argument to an elemental procedure,
4667 the array, which is generated through shallow copy by the scalarizer,
4668 is used directly and can alias the expressions it was copied from.
4669 - If E is an array constructor used as argument to a non-elemental
4670 procedure,the scalarizer is used in gfc_conv_expr_descriptor to generate
4671 the array as in the previous case, but then that array is used
4672 to initialize a new descriptor through deep copy. There is no alias
4673 possible in that case.
4674 Thus, the ARRAY_MAY_ALIAS flag is necessary to distinguish the two cases
4675 above. */
4677 static bool
4678 expr_may_alias_variables (gfc_expr *e, bool array_may_alias)
4680 gfc_constructor *c;
4682 if (e->expr_type == EXPR_VARIABLE)
4683 return true;
4684 else if (e->expr_type == EXPR_FUNCTION)
4686 gfc_symbol *proc_ifc = gfc_get_proc_ifc_for_expr (e);
4688 if (proc_ifc->result != NULL
4689 && ((proc_ifc->result->ts.type == BT_CLASS
4690 && proc_ifc->result->ts.u.derived->attr.is_class
4691 && CLASS_DATA (proc_ifc->result)->attr.class_pointer)
4692 || proc_ifc->result->attr.pointer))
4693 return true;
4694 else
4695 return false;
4697 else if (e->expr_type != EXPR_ARRAY || !array_may_alias)
4698 return false;
4700 for (c = gfc_constructor_first (e->value.constructor);
4701 c; c = gfc_constructor_next (c))
4702 if (c->expr
4703 && expr_may_alias_variables (c->expr, array_may_alias))
4704 return true;
4706 return false;
4710 /* Generate code for a procedure call. Note can return se->post != NULL.
4711 If se->direct_byref is set then se->expr contains the return parameter.
4712 Return nonzero, if the call has alternate specifiers.
4713 'expr' is only needed for procedure pointer components. */
4716 gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
4717 gfc_actual_arglist * args, gfc_expr * expr,
4718 vec<tree, va_gc> *append_args)
4720 gfc_interface_mapping mapping;
4721 vec<tree, va_gc> *arglist;
4722 vec<tree, va_gc> *retargs;
4723 tree tmp;
4724 tree fntype;
4725 gfc_se parmse;
4726 gfc_array_info *info;
4727 int byref;
4728 int parm_kind;
4729 tree type;
4730 tree var;
4731 tree len;
4732 tree base_object;
4733 vec<tree, va_gc> *stringargs;
4734 vec<tree, va_gc> *optionalargs;
4735 tree result = NULL;
4736 gfc_formal_arglist *formal;
4737 gfc_actual_arglist *arg;
4738 int has_alternate_specifier = 0;
4739 bool need_interface_mapping;
4740 bool callee_alloc;
4741 bool ulim_copy;
4742 gfc_typespec ts;
4743 gfc_charlen cl;
4744 gfc_expr *e;
4745 gfc_symbol *fsym;
4746 stmtblock_t post;
4747 enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
4748 gfc_component *comp = NULL;
4749 int arglen;
4750 unsigned int argc;
4752 arglist = NULL;
4753 retargs = NULL;
4754 stringargs = NULL;
4755 optionalargs = NULL;
4756 var = NULL_TREE;
4757 len = NULL_TREE;
4758 gfc_clear_ts (&ts);
4760 comp = gfc_get_proc_ptr_comp (expr);
4762 bool elemental_proc = (comp
4763 && comp->ts.interface
4764 && comp->ts.interface->attr.elemental)
4765 || (comp && comp->attr.elemental)
4766 || sym->attr.elemental;
4768 if (se->ss != NULL)
4770 if (!elemental_proc)
4772 gcc_assert (se->ss->info->type == GFC_SS_FUNCTION);
4773 if (se->ss->info->useflags)
4775 gcc_assert ((!comp && gfc_return_by_reference (sym)
4776 && sym->result->attr.dimension)
4777 || (comp && comp->attr.dimension)
4778 || gfc_is_alloc_class_array_function (expr));
4779 gcc_assert (se->loop != NULL);
4780 /* Access the previously obtained result. */
4781 gfc_conv_tmp_array_ref (se);
4782 return 0;
4785 info = &se->ss->info->data.array;
4787 else
4788 info = NULL;
4790 gfc_init_block (&post);
4791 gfc_init_interface_mapping (&mapping);
4792 if (!comp)
4794 formal = gfc_sym_get_dummy_args (sym);
4795 need_interface_mapping = sym->attr.dimension ||
4796 (sym->ts.type == BT_CHARACTER
4797 && sym->ts.u.cl->length
4798 && sym->ts.u.cl->length->expr_type
4799 != EXPR_CONSTANT);
4801 else
4803 formal = comp->ts.interface ? comp->ts.interface->formal : NULL;
4804 need_interface_mapping = comp->attr.dimension ||
4805 (comp->ts.type == BT_CHARACTER
4806 && comp->ts.u.cl->length
4807 && comp->ts.u.cl->length->expr_type
4808 != EXPR_CONSTANT);
4811 base_object = NULL_TREE;
4812 /* For _vprt->_copy () routines no formal symbol is present. Nevertheless
4813 is the third and fourth argument to such a function call a value
4814 denoting the number of elements to copy (i.e., most of the time the
4815 length of a deferred length string). */
4816 ulim_copy = (formal == NULL)
4817 && UNLIMITED_POLY (sym)
4818 && comp && (strcmp ("_copy", comp->name) == 0);
4820 /* Evaluate the arguments. */
4821 for (arg = args, argc = 0; arg != NULL;
4822 arg = arg->next, formal = formal ? formal->next : NULL, ++argc)
4824 e = arg->expr;
4825 fsym = formal ? formal->sym : NULL;
4826 parm_kind = MISSING;
4828 /* If the procedure requires an explicit interface, the actual
4829 argument is passed according to the corresponding formal
4830 argument. If the corresponding formal argument is a POINTER,
4831 ALLOCATABLE or assumed shape, we do not use g77's calling
4832 convention, and pass the address of the array descriptor
4833 instead. Otherwise we use g77's calling convention, in other words
4834 pass the array data pointer without descriptor. */
4835 bool nodesc_arg = fsym != NULL
4836 && !(fsym->attr.pointer || fsym->attr.allocatable)
4837 && fsym->as
4838 && fsym->as->type != AS_ASSUMED_SHAPE
4839 && fsym->as->type != AS_ASSUMED_RANK;
4840 if (comp)
4841 nodesc_arg = nodesc_arg || !comp->attr.always_explicit;
4842 else
4843 nodesc_arg = nodesc_arg || !sym->attr.always_explicit;
4845 /* Class array expressions are sometimes coming completely unadorned
4846 with either arrayspec or _data component. Correct that here.
4847 OOP-TODO: Move this to the frontend. */
4848 if (e && e->expr_type == EXPR_VARIABLE
4849 && !e->ref
4850 && e->ts.type == BT_CLASS
4851 && (CLASS_DATA (e)->attr.codimension
4852 || CLASS_DATA (e)->attr.dimension))
4854 gfc_typespec temp_ts = e->ts;
4855 gfc_add_class_array_ref (e);
4856 e->ts = temp_ts;
4859 if (e == NULL)
4861 if (se->ignore_optional)
4863 /* Some intrinsics have already been resolved to the correct
4864 parameters. */
4865 continue;
4867 else if (arg->label)
4869 has_alternate_specifier = 1;
4870 continue;
4872 else
4874 gfc_init_se (&parmse, NULL);
4876 /* For scalar arguments with VALUE attribute which are passed by
4877 value, pass "0" and a hidden argument gives the optional
4878 status. */
4879 if (fsym && fsym->attr.optional && fsym->attr.value
4880 && !fsym->attr.dimension && fsym->ts.type != BT_CHARACTER
4881 && fsym->ts.type != BT_CLASS && fsym->ts.type != BT_DERIVED)
4883 parmse.expr = fold_convert (gfc_sym_type (fsym),
4884 integer_zero_node);
4885 vec_safe_push (optionalargs, boolean_false_node);
4887 else
4889 /* Pass a NULL pointer for an absent arg. */
4890 parmse.expr = null_pointer_node;
4891 if (arg->missing_arg_type == BT_CHARACTER)
4892 parmse.string_length = build_int_cst (gfc_charlen_type_node,
4897 else if (arg->expr->expr_type == EXPR_NULL
4898 && fsym && !fsym->attr.pointer
4899 && (fsym->ts.type != BT_CLASS
4900 || !CLASS_DATA (fsym)->attr.class_pointer))
4902 /* Pass a NULL pointer to denote an absent arg. */
4903 gcc_assert (fsym->attr.optional && !fsym->attr.allocatable
4904 && (fsym->ts.type != BT_CLASS
4905 || !CLASS_DATA (fsym)->attr.allocatable));
4906 gfc_init_se (&parmse, NULL);
4907 parmse.expr = null_pointer_node;
4908 if (arg->missing_arg_type == BT_CHARACTER)
4909 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
4911 else if (fsym && fsym->ts.type == BT_CLASS
4912 && e->ts.type == BT_DERIVED)
4914 /* The derived type needs to be converted to a temporary
4915 CLASS object. */
4916 gfc_init_se (&parmse, se);
4917 gfc_conv_derived_to_class (&parmse, e, fsym->ts, NULL,
4918 fsym->attr.optional
4919 && e->expr_type == EXPR_VARIABLE
4920 && e->symtree->n.sym->attr.optional,
4921 CLASS_DATA (fsym)->attr.class_pointer
4922 || CLASS_DATA (fsym)->attr.allocatable);
4924 else if (UNLIMITED_POLY (fsym) && e->ts.type != BT_CLASS)
4926 /* The intrinsic type needs to be converted to a temporary
4927 CLASS object for the unlimited polymorphic formal. */
4928 gfc_init_se (&parmse, se);
4929 gfc_conv_intrinsic_to_class (&parmse, e, fsym->ts);
4931 else if (se->ss && se->ss->info->useflags)
4933 gfc_ss *ss;
4935 ss = se->ss;
4937 /* An elemental function inside a scalarized loop. */
4938 gfc_init_se (&parmse, se);
4939 parm_kind = ELEMENTAL;
4941 /* When no fsym is present, ulim_copy is set and this is a third or
4942 fourth argument, use call-by-value instead of by reference to
4943 hand the length properties to the copy routine (i.e., most of the
4944 time this will be a call to a __copy_character_* routine where the
4945 third and fourth arguments are the lengths of a deferred length
4946 char array). */
4947 if ((fsym && fsym->attr.value)
4948 || (ulim_copy && (argc == 2 || argc == 3)))
4949 gfc_conv_expr (&parmse, e);
4950 else
4951 gfc_conv_expr_reference (&parmse, e);
4953 if (e->ts.type == BT_CHARACTER && !e->rank
4954 && e->expr_type == EXPR_FUNCTION)
4955 parmse.expr = build_fold_indirect_ref_loc (input_location,
4956 parmse.expr);
4958 if (fsym && fsym->ts.type == BT_DERIVED
4959 && gfc_is_class_container_ref (e))
4961 parmse.expr = gfc_class_data_get (parmse.expr);
4963 if (fsym->attr.optional && e->expr_type == EXPR_VARIABLE
4964 && e->symtree->n.sym->attr.optional)
4966 tree cond = gfc_conv_expr_present (e->symtree->n.sym);
4967 parmse.expr = build3_loc (input_location, COND_EXPR,
4968 TREE_TYPE (parmse.expr),
4969 cond, parmse.expr,
4970 fold_convert (TREE_TYPE (parmse.expr),
4971 null_pointer_node));
4975 /* If we are passing an absent array as optional dummy to an
4976 elemental procedure, make sure that we pass NULL when the data
4977 pointer is NULL. We need this extra conditional because of
4978 scalarization which passes arrays elements to the procedure,
4979 ignoring the fact that the array can be absent/unallocated/... */
4980 if (ss->info->can_be_null_ref && ss->info->type != GFC_SS_REFERENCE)
4982 tree descriptor_data;
4984 descriptor_data = ss->info->data.array.data;
4985 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
4986 descriptor_data,
4987 fold_convert (TREE_TYPE (descriptor_data),
4988 null_pointer_node));
4989 parmse.expr
4990 = fold_build3_loc (input_location, COND_EXPR,
4991 TREE_TYPE (parmse.expr),
4992 gfc_unlikely (tmp, PRED_FORTRAN_ABSENT_DUMMY),
4993 fold_convert (TREE_TYPE (parmse.expr),
4994 null_pointer_node),
4995 parmse.expr);
4998 /* The scalarizer does not repackage the reference to a class
4999 array - instead it returns a pointer to the data element. */
5000 if (fsym && fsym->ts.type == BT_CLASS && e->ts.type == BT_CLASS)
5001 gfc_conv_class_to_class (&parmse, e, fsym->ts, true,
5002 fsym->attr.intent != INTENT_IN
5003 && (CLASS_DATA (fsym)->attr.class_pointer
5004 || CLASS_DATA (fsym)->attr.allocatable),
5005 fsym->attr.optional
5006 && e->expr_type == EXPR_VARIABLE
5007 && e->symtree->n.sym->attr.optional,
5008 CLASS_DATA (fsym)->attr.class_pointer
5009 || CLASS_DATA (fsym)->attr.allocatable);
5011 else
5013 bool scalar;
5014 gfc_ss *argss;
5016 gfc_init_se (&parmse, NULL);
5018 /* Check whether the expression is a scalar or not; we cannot use
5019 e->rank as it can be nonzero for functions arguments. */
5020 argss = gfc_walk_expr (e);
5021 scalar = argss == gfc_ss_terminator;
5022 if (!scalar)
5023 gfc_free_ss_chain (argss);
5025 /* Special handling for passing scalar polymorphic coarrays;
5026 otherwise one passes "class->_data.data" instead of "&class". */
5027 if (e->rank == 0 && e->ts.type == BT_CLASS
5028 && fsym && fsym->ts.type == BT_CLASS
5029 && CLASS_DATA (fsym)->attr.codimension
5030 && !CLASS_DATA (fsym)->attr.dimension)
5032 gfc_add_class_array_ref (e);
5033 parmse.want_coarray = 1;
5034 scalar = false;
5037 /* A scalar or transformational function. */
5038 if (scalar)
5040 if (e->expr_type == EXPR_VARIABLE
5041 && e->symtree->n.sym->attr.cray_pointee
5042 && fsym && fsym->attr.flavor == FL_PROCEDURE)
5044 /* The Cray pointer needs to be converted to a pointer to
5045 a type given by the expression. */
5046 gfc_conv_expr (&parmse, e);
5047 type = build_pointer_type (TREE_TYPE (parmse.expr));
5048 tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
5049 parmse.expr = convert (type, tmp);
5051 else if (fsym && fsym->attr.value)
5053 if (fsym->ts.type == BT_CHARACTER
5054 && fsym->ts.is_c_interop
5055 && fsym->ns->proc_name != NULL
5056 && fsym->ns->proc_name->attr.is_bind_c)
5058 parmse.expr = NULL;
5059 gfc_conv_scalar_char_value (fsym, &parmse, &e);
5060 if (parmse.expr == NULL)
5061 gfc_conv_expr (&parmse, e);
5063 else
5065 gfc_conv_expr (&parmse, e);
5066 if (fsym->attr.optional
5067 && fsym->ts.type != BT_CLASS
5068 && fsym->ts.type != BT_DERIVED)
5070 if (e->expr_type != EXPR_VARIABLE
5071 || !e->symtree->n.sym->attr.optional
5072 || e->ref != NULL)
5073 vec_safe_push (optionalargs, boolean_true_node);
5074 else
5076 tmp = gfc_conv_expr_present (e->symtree->n.sym);
5077 if (!e->symtree->n.sym->attr.value)
5078 parmse.expr
5079 = fold_build3_loc (input_location, COND_EXPR,
5080 TREE_TYPE (parmse.expr),
5081 tmp, parmse.expr,
5082 fold_convert (TREE_TYPE (parmse.expr),
5083 integer_zero_node));
5085 vec_safe_push (optionalargs, tmp);
5090 else if (arg->name && arg->name[0] == '%')
5091 /* Argument list functions %VAL, %LOC and %REF are signalled
5092 through arg->name. */
5093 conv_arglist_function (&parmse, arg->expr, arg->name);
5094 else if ((e->expr_type == EXPR_FUNCTION)
5095 && ((e->value.function.esym
5096 && e->value.function.esym->result->attr.pointer)
5097 || (!e->value.function.esym
5098 && e->symtree->n.sym->attr.pointer))
5099 && fsym && fsym->attr.target)
5101 gfc_conv_expr (&parmse, e);
5102 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
5104 else if (e->expr_type == EXPR_FUNCTION
5105 && e->symtree->n.sym->result
5106 && e->symtree->n.sym->result != e->symtree->n.sym
5107 && e->symtree->n.sym->result->attr.proc_pointer)
5109 /* Functions returning procedure pointers. */
5110 gfc_conv_expr (&parmse, e);
5111 if (fsym && fsym->attr.proc_pointer)
5112 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
5114 else
5116 if (e->ts.type == BT_CLASS && fsym
5117 && fsym->ts.type == BT_CLASS
5118 && (!CLASS_DATA (fsym)->as
5119 || CLASS_DATA (fsym)->as->type != AS_ASSUMED_RANK)
5120 && CLASS_DATA (e)->attr.codimension)
5122 gcc_assert (!CLASS_DATA (fsym)->attr.codimension);
5123 gcc_assert (!CLASS_DATA (fsym)->as);
5124 gfc_add_class_array_ref (e);
5125 parmse.want_coarray = 1;
5126 gfc_conv_expr_reference (&parmse, e);
5127 class_scalar_coarray_to_class (&parmse, e, fsym->ts,
5128 fsym->attr.optional
5129 && e->expr_type == EXPR_VARIABLE);
5131 else if (e->ts.type == BT_CLASS && fsym
5132 && fsym->ts.type == BT_CLASS
5133 && !CLASS_DATA (fsym)->as
5134 && !CLASS_DATA (e)->as
5135 && strcmp (fsym->ts.u.derived->name,
5136 e->ts.u.derived->name))
5138 type = gfc_typenode_for_spec (&fsym->ts);
5139 var = gfc_create_var (type, fsym->name);
5140 gfc_conv_expr (&parmse, e);
5141 if (fsym->attr.optional
5142 && e->expr_type == EXPR_VARIABLE
5143 && e->symtree->n.sym->attr.optional)
5145 stmtblock_t block;
5146 tree cond;
5147 tmp = gfc_build_addr_expr (NULL_TREE, parmse.expr);
5148 cond = fold_build2_loc (input_location, NE_EXPR,
5149 boolean_type_node, tmp,
5150 fold_convert (TREE_TYPE (tmp),
5151 null_pointer_node));
5152 gfc_start_block (&block);
5153 gfc_add_modify (&block, var,
5154 fold_build1_loc (input_location,
5155 VIEW_CONVERT_EXPR,
5156 type, parmse.expr));
5157 gfc_add_expr_to_block (&parmse.pre,
5158 fold_build3_loc (input_location,
5159 COND_EXPR, void_type_node,
5160 cond, gfc_finish_block (&block),
5161 build_empty_stmt (input_location)));
5162 parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
5163 parmse.expr = build3_loc (input_location, COND_EXPR,
5164 TREE_TYPE (parmse.expr),
5165 cond, parmse.expr,
5166 fold_convert (TREE_TYPE (parmse.expr),
5167 null_pointer_node));
5169 else
5171 gfc_add_modify (&parmse.pre, var,
5172 fold_build1_loc (input_location,
5173 VIEW_CONVERT_EXPR,
5174 type, parmse.expr));
5175 parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
5178 else
5179 gfc_conv_expr_reference (&parmse, e);
5181 /* Catch base objects that are not variables. */
5182 if (e->ts.type == BT_CLASS
5183 && e->expr_type != EXPR_VARIABLE
5184 && expr && e == expr->base_expr)
5185 base_object = build_fold_indirect_ref_loc (input_location,
5186 parmse.expr);
5188 /* A class array element needs converting back to be a
5189 class object, if the formal argument is a class object. */
5190 if (fsym && fsym->ts.type == BT_CLASS
5191 && e->ts.type == BT_CLASS
5192 && ((CLASS_DATA (fsym)->as
5193 && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
5194 || CLASS_DATA (e)->attr.dimension))
5195 gfc_conv_class_to_class (&parmse, e, fsym->ts, false,
5196 fsym->attr.intent != INTENT_IN
5197 && (CLASS_DATA (fsym)->attr.class_pointer
5198 || CLASS_DATA (fsym)->attr.allocatable),
5199 fsym->attr.optional
5200 && e->expr_type == EXPR_VARIABLE
5201 && e->symtree->n.sym->attr.optional,
5202 CLASS_DATA (fsym)->attr.class_pointer
5203 || CLASS_DATA (fsym)->attr.allocatable);
5205 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
5206 allocated on entry, it must be deallocated. */
5207 if (fsym && fsym->attr.intent == INTENT_OUT
5208 && (fsym->attr.allocatable
5209 || (fsym->ts.type == BT_CLASS
5210 && CLASS_DATA (fsym)->attr.allocatable)))
5212 stmtblock_t block;
5213 tree ptr;
5215 gfc_init_block (&block);
5216 ptr = parmse.expr;
5217 if (e->ts.type == BT_CLASS)
5218 ptr = gfc_class_data_get (ptr);
5220 tmp = gfc_deallocate_scalar_with_status (ptr, NULL_TREE,
5221 NULL_TREE, true,
5222 e, e->ts);
5223 gfc_add_expr_to_block (&block, tmp);
5224 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
5225 void_type_node, ptr,
5226 null_pointer_node);
5227 gfc_add_expr_to_block (&block, tmp);
5229 if (fsym->ts.type == BT_CLASS && UNLIMITED_POLY (fsym))
5231 gfc_add_modify (&block, ptr,
5232 fold_convert (TREE_TYPE (ptr),
5233 null_pointer_node));
5234 gfc_add_expr_to_block (&block, tmp);
5236 else if (fsym->ts.type == BT_CLASS)
5238 gfc_symbol *vtab;
5239 vtab = gfc_find_derived_vtab (fsym->ts.u.derived);
5240 tmp = gfc_get_symbol_decl (vtab);
5241 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
5242 ptr = gfc_class_vptr_get (parmse.expr);
5243 gfc_add_modify (&block, ptr,
5244 fold_convert (TREE_TYPE (ptr), tmp));
5245 gfc_add_expr_to_block (&block, tmp);
5248 if (fsym->attr.optional
5249 && e->expr_type == EXPR_VARIABLE
5250 && e->symtree->n.sym->attr.optional)
5252 tmp = fold_build3_loc (input_location, COND_EXPR,
5253 void_type_node,
5254 gfc_conv_expr_present (e->symtree->n.sym),
5255 gfc_finish_block (&block),
5256 build_empty_stmt (input_location));
5258 else
5259 tmp = gfc_finish_block (&block);
5261 gfc_add_expr_to_block (&se->pre, tmp);
5264 if (fsym && (fsym->ts.type == BT_DERIVED
5265 || fsym->ts.type == BT_ASSUMED)
5266 && e->ts.type == BT_CLASS
5267 && !CLASS_DATA (e)->attr.dimension
5268 && !CLASS_DATA (e)->attr.codimension)
5269 parmse.expr = gfc_class_data_get (parmse.expr);
5271 /* Wrap scalar variable in a descriptor. We need to convert
5272 the address of a pointer back to the pointer itself before,
5273 we can assign it to the data field. */
5275 if (fsym && fsym->as && fsym->as->type == AS_ASSUMED_RANK
5276 && fsym->ts.type != BT_CLASS && e->expr_type != EXPR_NULL)
5278 tmp = parmse.expr;
5279 if (TREE_CODE (tmp) == ADDR_EXPR
5280 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp, 0))))
5281 tmp = TREE_OPERAND (tmp, 0);
5282 parmse.expr = gfc_conv_scalar_to_descriptor (&parmse, tmp,
5283 fsym->attr);
5284 parmse.expr = gfc_build_addr_expr (NULL_TREE,
5285 parmse.expr);
5287 else if (fsym && e->expr_type != EXPR_NULL
5288 && ((fsym->attr.pointer
5289 && fsym->attr.flavor != FL_PROCEDURE)
5290 || (fsym->attr.proc_pointer
5291 && !(e->expr_type == EXPR_VARIABLE
5292 && e->symtree->n.sym->attr.dummy))
5293 || (fsym->attr.proc_pointer
5294 && e->expr_type == EXPR_VARIABLE
5295 && gfc_is_proc_ptr_comp (e))
5296 || (fsym->attr.allocatable
5297 && fsym->attr.flavor != FL_PROCEDURE)))
5299 /* Scalar pointer dummy args require an extra level of
5300 indirection. The null pointer already contains
5301 this level of indirection. */
5302 parm_kind = SCALAR_POINTER;
5303 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
5307 else if (e->ts.type == BT_CLASS
5308 && fsym && fsym->ts.type == BT_CLASS
5309 && (CLASS_DATA (fsym)->attr.dimension
5310 || CLASS_DATA (fsym)->attr.codimension))
5312 /* Pass a class array. */
5313 parmse.use_offset = 1;
5314 gfc_conv_expr_descriptor (&parmse, e);
5316 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
5317 allocated on entry, it must be deallocated. */
5318 if (fsym->attr.intent == INTENT_OUT
5319 && CLASS_DATA (fsym)->attr.allocatable)
5321 stmtblock_t block;
5322 tree ptr;
5324 gfc_init_block (&block);
5325 ptr = parmse.expr;
5326 ptr = gfc_class_data_get (ptr);
5328 tmp = gfc_deallocate_with_status (ptr, NULL_TREE,
5329 NULL_TREE, NULL_TREE,
5330 NULL_TREE, true, e,
5331 GFC_CAF_COARRAY_NOCOARRAY);
5332 gfc_add_expr_to_block (&block, tmp);
5333 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
5334 void_type_node, ptr,
5335 null_pointer_node);
5336 gfc_add_expr_to_block (&block, tmp);
5337 gfc_reset_vptr (&block, e);
5339 if (fsym->attr.optional
5340 && e->expr_type == EXPR_VARIABLE
5341 && (!e->ref
5342 || (e->ref->type == REF_ARRAY
5343 && e->ref->u.ar.type != AR_FULL))
5344 && e->symtree->n.sym->attr.optional)
5346 tmp = fold_build3_loc (input_location, COND_EXPR,
5347 void_type_node,
5348 gfc_conv_expr_present (e->symtree->n.sym),
5349 gfc_finish_block (&block),
5350 build_empty_stmt (input_location));
5352 else
5353 tmp = gfc_finish_block (&block);
5355 gfc_add_expr_to_block (&se->pre, tmp);
5358 /* The conversion does not repackage the reference to a class
5359 array - _data descriptor. */
5360 gfc_conv_class_to_class (&parmse, e, fsym->ts, false,
5361 fsym->attr.intent != INTENT_IN
5362 && (CLASS_DATA (fsym)->attr.class_pointer
5363 || CLASS_DATA (fsym)->attr.allocatable),
5364 fsym->attr.optional
5365 && e->expr_type == EXPR_VARIABLE
5366 && e->symtree->n.sym->attr.optional,
5367 CLASS_DATA (fsym)->attr.class_pointer
5368 || CLASS_DATA (fsym)->attr.allocatable);
5370 else
5372 /* If the argument is a function call that may not create
5373 a temporary for the result, we have to check that we
5374 can do it, i.e. that there is no alias between this
5375 argument and another one. */
5376 if (gfc_get_noncopying_intrinsic_argument (e) != NULL)
5378 gfc_expr *iarg;
5379 sym_intent intent;
5381 if (fsym != NULL)
5382 intent = fsym->attr.intent;
5383 else
5384 intent = INTENT_UNKNOWN;
5386 if (gfc_check_fncall_dependency (e, intent, sym, args,
5387 NOT_ELEMENTAL))
5388 parmse.force_tmp = 1;
5390 iarg = e->value.function.actual->expr;
5392 /* Temporary needed if aliasing due to host association. */
5393 if (sym->attr.contained
5394 && !sym->attr.pure
5395 && !sym->attr.implicit_pure
5396 && !sym->attr.use_assoc
5397 && iarg->expr_type == EXPR_VARIABLE
5398 && sym->ns == iarg->symtree->n.sym->ns)
5399 parmse.force_tmp = 1;
5401 /* Ditto within module. */
5402 if (sym->attr.use_assoc
5403 && !sym->attr.pure
5404 && !sym->attr.implicit_pure
5405 && iarg->expr_type == EXPR_VARIABLE
5406 && sym->module == iarg->symtree->n.sym->module)
5407 parmse.force_tmp = 1;
5410 if (e->expr_type == EXPR_VARIABLE
5411 && is_subref_array (e))
5412 /* The actual argument is a component reference to an
5413 array of derived types. In this case, the argument
5414 is converted to a temporary, which is passed and then
5415 written back after the procedure call. */
5416 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
5417 fsym ? fsym->attr.intent : INTENT_INOUT,
5418 fsym && fsym->attr.pointer);
5419 else if (gfc_is_class_array_ref (e, NULL)
5420 && fsym && fsym->ts.type == BT_DERIVED)
5421 /* The actual argument is a component reference to an
5422 array of derived types. In this case, the argument
5423 is converted to a temporary, which is passed and then
5424 written back after the procedure call.
5425 OOP-TODO: Insert code so that if the dynamic type is
5426 the same as the declared type, copy-in/copy-out does
5427 not occur. */
5428 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
5429 fsym ? fsym->attr.intent : INTENT_INOUT,
5430 fsym && fsym->attr.pointer);
5432 else if (gfc_is_alloc_class_array_function (e)
5433 && fsym && fsym->ts.type == BT_DERIVED)
5434 /* See previous comment. For function actual argument,
5435 the write out is not needed so the intent is set as
5436 intent in. */
5438 e->must_finalize = 1;
5439 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
5440 INTENT_IN,
5441 fsym && fsym->attr.pointer);
5443 else
5444 gfc_conv_array_parameter (&parmse, e, nodesc_arg, fsym,
5445 sym->name, NULL);
5447 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
5448 allocated on entry, it must be deallocated. */
5449 if (fsym && fsym->attr.allocatable
5450 && fsym->attr.intent == INTENT_OUT)
5452 tmp = build_fold_indirect_ref_loc (input_location,
5453 parmse.expr);
5454 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
5455 tmp = gfc_conv_descriptor_data_get (tmp);
5456 tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
5457 NULL_TREE, NULL_TREE, true,
5459 GFC_CAF_COARRAY_NOCOARRAY);
5460 if (fsym->attr.optional
5461 && e->expr_type == EXPR_VARIABLE
5462 && e->symtree->n.sym->attr.optional)
5463 tmp = fold_build3_loc (input_location, COND_EXPR,
5464 void_type_node,
5465 gfc_conv_expr_present (e->symtree->n.sym),
5466 tmp, build_empty_stmt (input_location));
5467 gfc_add_expr_to_block (&se->pre, tmp);
5472 /* The case with fsym->attr.optional is that of a user subroutine
5473 with an interface indicating an optional argument. When we call
5474 an intrinsic subroutine, however, fsym is NULL, but we might still
5475 have an optional argument, so we proceed to the substitution
5476 just in case. */
5477 if (e && (fsym == NULL || fsym->attr.optional))
5479 /* If an optional argument is itself an optional dummy argument,
5480 check its presence and substitute a null if absent. This is
5481 only needed when passing an array to an elemental procedure
5482 as then array elements are accessed - or no NULL pointer is
5483 allowed and a "1" or "0" should be passed if not present.
5484 When passing a non-array-descriptor full array to a
5485 non-array-descriptor dummy, no check is needed. For
5486 array-descriptor actual to array-descriptor dummy, see
5487 PR 41911 for why a check has to be inserted.
5488 fsym == NULL is checked as intrinsics required the descriptor
5489 but do not always set fsym. */
5490 if (e->expr_type == EXPR_VARIABLE
5491 && e->symtree->n.sym->attr.optional
5492 && ((e->rank != 0 && elemental_proc)
5493 || e->representation.length || e->ts.type == BT_CHARACTER
5494 || (e->rank != 0
5495 && (fsym == NULL
5496 || (fsym-> as
5497 && (fsym->as->type == AS_ASSUMED_SHAPE
5498 || fsym->as->type == AS_ASSUMED_RANK
5499 || fsym->as->type == AS_DEFERRED))))))
5500 gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
5501 e->representation.length);
5504 if (fsym && e)
5506 /* Obtain the character length of an assumed character length
5507 length procedure from the typespec. */
5508 if (fsym->ts.type == BT_CHARACTER
5509 && parmse.string_length == NULL_TREE
5510 && e->ts.type == BT_PROCEDURE
5511 && e->symtree->n.sym->ts.type == BT_CHARACTER
5512 && e->symtree->n.sym->ts.u.cl->length != NULL
5513 && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
5515 gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
5516 parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
5520 if (fsym && need_interface_mapping && e)
5521 gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
5523 gfc_add_block_to_block (&se->pre, &parmse.pre);
5524 gfc_add_block_to_block (&post, &parmse.post);
5526 /* Allocated allocatable components of derived types must be
5527 deallocated for non-variable scalars, array arguments to elemental
5528 procedures, and array arguments with descriptor to non-elemental
5529 procedures. As bounds information for descriptorless arrays is no
5530 longer available here, they are dealt with in trans-array.c
5531 (gfc_conv_array_parameter). */
5532 if (e && (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS)
5533 && e->ts.u.derived->attr.alloc_comp
5534 && (e->rank == 0 || elemental_proc || !nodesc_arg)
5535 && !expr_may_alias_variables (e, elemental_proc))
5537 int parm_rank;
5538 /* It is known the e returns a structure type with at least one
5539 allocatable component. When e is a function, ensure that the
5540 function is called once only by using a temporary variable. */
5541 if (!DECL_P (parmse.expr))
5542 parmse.expr = gfc_evaluate_now_loc (input_location,
5543 parmse.expr, &se->pre);
5545 if (fsym && fsym->attr.value)
5546 tmp = parmse.expr;
5547 else
5548 tmp = build_fold_indirect_ref_loc (input_location,
5549 parmse.expr);
5551 parm_rank = e->rank;
5552 switch (parm_kind)
5554 case (ELEMENTAL):
5555 case (SCALAR):
5556 parm_rank = 0;
5557 break;
5559 case (SCALAR_POINTER):
5560 tmp = build_fold_indirect_ref_loc (input_location,
5561 tmp);
5562 break;
5565 if (e->expr_type == EXPR_OP
5566 && e->value.op.op == INTRINSIC_PARENTHESES
5567 && e->value.op.op1->expr_type == EXPR_VARIABLE)
5569 tree local_tmp;
5570 local_tmp = gfc_evaluate_now (tmp, &se->pre);
5571 local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp,
5572 parm_rank, 0);
5573 gfc_add_expr_to_block (&se->post, local_tmp);
5576 if (e->ts.type == BT_DERIVED && fsym && fsym->ts.type == BT_CLASS)
5578 /* The derived type is passed to gfc_deallocate_alloc_comp.
5579 Therefore, class actuals can handled correctly but derived
5580 types passed to class formals need the _data component. */
5581 tmp = gfc_class_data_get (tmp);
5582 if (!CLASS_DATA (fsym)->attr.dimension)
5583 tmp = build_fold_indirect_ref_loc (input_location, tmp);
5586 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp, parm_rank);
5588 gfc_prepend_expr_to_block (&post, tmp);
5591 /* Add argument checking of passing an unallocated/NULL actual to
5592 a nonallocatable/nonpointer dummy. */
5594 if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
5596 symbol_attribute attr;
5597 char *msg;
5598 tree cond;
5600 if (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
5601 attr = gfc_expr_attr (e);
5602 else
5603 goto end_pointer_check;
5605 /* In Fortran 2008 it's allowed to pass a NULL pointer/nonallocated
5606 allocatable to an optional dummy, cf. 12.5.2.12. */
5607 if (fsym != NULL && fsym->attr.optional && !attr.proc_pointer
5608 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
5609 goto end_pointer_check;
5611 if (attr.optional)
5613 /* If the actual argument is an optional pointer/allocatable and
5614 the formal argument takes an nonpointer optional value,
5615 it is invalid to pass a non-present argument on, even
5616 though there is no technical reason for this in gfortran.
5617 See Fortran 2003, Section 12.4.1.6 item (7)+(8). */
5618 tree present, null_ptr, type;
5620 if (attr.allocatable
5621 && (fsym == NULL || !fsym->attr.allocatable))
5622 msg = xasprintf ("Allocatable actual argument '%s' is not "
5623 "allocated or not present",
5624 e->symtree->n.sym->name);
5625 else if (attr.pointer
5626 && (fsym == NULL || !fsym->attr.pointer))
5627 msg = xasprintf ("Pointer actual argument '%s' is not "
5628 "associated or not present",
5629 e->symtree->n.sym->name);
5630 else if (attr.proc_pointer
5631 && (fsym == NULL || !fsym->attr.proc_pointer))
5632 msg = xasprintf ("Proc-pointer actual argument '%s' is not "
5633 "associated or not present",
5634 e->symtree->n.sym->name);
5635 else
5636 goto end_pointer_check;
5638 present = gfc_conv_expr_present (e->symtree->n.sym);
5639 type = TREE_TYPE (present);
5640 present = fold_build2_loc (input_location, EQ_EXPR,
5641 boolean_type_node, present,
5642 fold_convert (type,
5643 null_pointer_node));
5644 type = TREE_TYPE (parmse.expr);
5645 null_ptr = fold_build2_loc (input_location, EQ_EXPR,
5646 boolean_type_node, parmse.expr,
5647 fold_convert (type,
5648 null_pointer_node));
5649 cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
5650 boolean_type_node, present, null_ptr);
5652 else
5654 if (attr.allocatable
5655 && (fsym == NULL || !fsym->attr.allocatable))
5656 msg = xasprintf ("Allocatable actual argument '%s' is not "
5657 "allocated", e->symtree->n.sym->name);
5658 else if (attr.pointer
5659 && (fsym == NULL || !fsym->attr.pointer))
5660 msg = xasprintf ("Pointer actual argument '%s' is not "
5661 "associated", e->symtree->n.sym->name);
5662 else if (attr.proc_pointer
5663 && (fsym == NULL || !fsym->attr.proc_pointer))
5664 msg = xasprintf ("Proc-pointer actual argument '%s' is not "
5665 "associated", e->symtree->n.sym->name);
5666 else
5667 goto end_pointer_check;
5669 tmp = parmse.expr;
5671 /* If the argument is passed by value, we need to strip the
5672 INDIRECT_REF. */
5673 if (!POINTER_TYPE_P (TREE_TYPE (parmse.expr)))
5674 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
5676 cond = fold_build2_loc (input_location, EQ_EXPR,
5677 boolean_type_node, tmp,
5678 fold_convert (TREE_TYPE (tmp),
5679 null_pointer_node));
5682 gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
5683 msg);
5684 free (msg);
5686 end_pointer_check:
5688 /* Deferred length dummies pass the character length by reference
5689 so that the value can be returned. */
5690 if (parmse.string_length && fsym && fsym->ts.deferred)
5692 if (INDIRECT_REF_P (parmse.string_length))
5693 /* In chains of functions/procedure calls the string_length already
5694 is a pointer to the variable holding the length. Therefore
5695 remove the deref on call. */
5696 parmse.string_length = TREE_OPERAND (parmse.string_length, 0);
5697 else
5699 tmp = parmse.string_length;
5700 if (!VAR_P (tmp) && TREE_CODE (tmp) != COMPONENT_REF)
5701 tmp = gfc_evaluate_now (parmse.string_length, &se->pre);
5702 parmse.string_length = gfc_build_addr_expr (NULL_TREE, tmp);
5706 /* Character strings are passed as two parameters, a length and a
5707 pointer - except for Bind(c) which only passes the pointer.
5708 An unlimited polymorphic formal argument likewise does not
5709 need the length. */
5710 if (parmse.string_length != NULL_TREE
5711 && !sym->attr.is_bind_c
5712 && !(fsym && UNLIMITED_POLY (fsym)))
5713 vec_safe_push (stringargs, parmse.string_length);
5715 /* When calling __copy for character expressions to unlimited
5716 polymorphic entities, the dst argument needs a string length. */
5717 if (sym->name[0] == '_' && e && e->ts.type == BT_CHARACTER
5718 && strncmp (sym->name, "__vtab_CHARACTER", 16) == 0
5719 && arg->next && arg->next->expr
5720 && (arg->next->expr->ts.type == BT_DERIVED
5721 || arg->next->expr->ts.type == BT_CLASS)
5722 && arg->next->expr->ts.u.derived->attr.unlimited_polymorphic)
5723 vec_safe_push (stringargs, parmse.string_length);
5725 /* For descriptorless coarrays and assumed-shape coarray dummies, we
5726 pass the token and the offset as additional arguments. */
5727 if (fsym && e == NULL && flag_coarray == GFC_FCOARRAY_LIB
5728 && ((fsym->ts.type != BT_CLASS && fsym->attr.codimension
5729 && !fsym->attr.allocatable)
5730 || (fsym->ts.type == BT_CLASS
5731 && CLASS_DATA (fsym)->attr.codimension
5732 && !CLASS_DATA (fsym)->attr.allocatable)))
5734 /* Token and offset. */
5735 vec_safe_push (stringargs, null_pointer_node);
5736 vec_safe_push (stringargs, build_int_cst (gfc_array_index_type, 0));
5737 gcc_assert (fsym->attr.optional);
5739 else if (fsym && flag_coarray == GFC_FCOARRAY_LIB
5740 && ((fsym->ts.type != BT_CLASS && fsym->attr.codimension
5741 && !fsym->attr.allocatable)
5742 || (fsym->ts.type == BT_CLASS
5743 && CLASS_DATA (fsym)->attr.codimension
5744 && !CLASS_DATA (fsym)->attr.allocatable)))
5746 tree caf_decl, caf_type;
5747 tree offset, tmp2;
5749 caf_decl = gfc_get_tree_for_caf_expr (e);
5750 caf_type = TREE_TYPE (caf_decl);
5752 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
5753 && (GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE
5754 || GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_POINTER))
5755 tmp = gfc_conv_descriptor_token (caf_decl);
5756 else if (DECL_LANG_SPECIFIC (caf_decl)
5757 && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
5758 tmp = GFC_DECL_TOKEN (caf_decl);
5759 else
5761 gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
5762 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
5763 tmp = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
5766 vec_safe_push (stringargs, tmp);
5768 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
5769 && GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE)
5770 offset = build_int_cst (gfc_array_index_type, 0);
5771 else if (DECL_LANG_SPECIFIC (caf_decl)
5772 && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
5773 offset = GFC_DECL_CAF_OFFSET (caf_decl);
5774 else if (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) != NULL_TREE)
5775 offset = GFC_TYPE_ARRAY_CAF_OFFSET (caf_type);
5776 else
5777 offset = build_int_cst (gfc_array_index_type, 0);
5779 if (GFC_DESCRIPTOR_TYPE_P (caf_type))
5780 tmp = gfc_conv_descriptor_data_get (caf_decl);
5781 else
5783 gcc_assert (POINTER_TYPE_P (caf_type));
5784 tmp = caf_decl;
5787 tmp2 = fsym->ts.type == BT_CLASS
5788 ? gfc_class_data_get (parmse.expr) : parmse.expr;
5789 if ((fsym->ts.type != BT_CLASS
5790 && (fsym->as->type == AS_ASSUMED_SHAPE
5791 || fsym->as->type == AS_ASSUMED_RANK))
5792 || (fsym->ts.type == BT_CLASS
5793 && (CLASS_DATA (fsym)->as->type == AS_ASSUMED_SHAPE
5794 || CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)))
5796 if (fsym->ts.type == BT_CLASS)
5797 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (tmp2)));
5798 else
5800 gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
5801 tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
5803 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)));
5804 tmp2 = gfc_conv_descriptor_data_get (tmp2);
5806 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
5807 tmp2 = gfc_conv_descriptor_data_get (tmp2);
5808 else
5810 gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
5813 tmp = fold_build2_loc (input_location, MINUS_EXPR,
5814 gfc_array_index_type,
5815 fold_convert (gfc_array_index_type, tmp2),
5816 fold_convert (gfc_array_index_type, tmp));
5817 offset = fold_build2_loc (input_location, PLUS_EXPR,
5818 gfc_array_index_type, offset, tmp);
5820 vec_safe_push (stringargs, offset);
5823 vec_safe_push (arglist, parmse.expr);
5825 gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
5827 if (comp)
5828 ts = comp->ts;
5829 else if (sym->ts.type == BT_CLASS)
5830 ts = CLASS_DATA (sym)->ts;
5831 else
5832 ts = sym->ts;
5834 if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
5835 se->string_length = build_int_cst (gfc_charlen_type_node, 1);
5836 else if (ts.type == BT_CHARACTER)
5838 if (ts.u.cl->length == NULL)
5840 /* Assumed character length results are not allowed by 5.1.1.5 of the
5841 standard and are trapped in resolve.c; except in the case of SPREAD
5842 (and other intrinsics?) and dummy functions. In the case of SPREAD,
5843 we take the character length of the first argument for the result.
5844 For dummies, we have to look through the formal argument list for
5845 this function and use the character length found there.*/
5846 if (ts.deferred)
5847 cl.backend_decl = gfc_create_var (gfc_charlen_type_node, "slen");
5848 else if (!sym->attr.dummy)
5849 cl.backend_decl = (*stringargs)[0];
5850 else
5852 formal = gfc_sym_get_dummy_args (sym->ns->proc_name);
5853 for (; formal; formal = formal->next)
5854 if (strcmp (formal->sym->name, sym->name) == 0)
5855 cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
5857 len = cl.backend_decl;
5859 else
5861 tree tmp;
5863 /* Calculate the length of the returned string. */
5864 gfc_init_se (&parmse, NULL);
5865 if (need_interface_mapping)
5866 gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
5867 else
5868 gfc_conv_expr (&parmse, ts.u.cl->length);
5869 gfc_add_block_to_block (&se->pre, &parmse.pre);
5870 gfc_add_block_to_block (&se->post, &parmse.post);
5872 tmp = fold_convert (gfc_charlen_type_node, parmse.expr);
5873 tmp = fold_build2_loc (input_location, MAX_EXPR,
5874 gfc_charlen_type_node, tmp,
5875 build_int_cst (gfc_charlen_type_node, 0));
5876 cl.backend_decl = tmp;
5879 /* Set up a charlen structure for it. */
5880 cl.next = NULL;
5881 cl.length = NULL;
5882 ts.u.cl = &cl;
5884 len = cl.backend_decl;
5887 byref = (comp && (comp->attr.dimension
5888 || (comp->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)))
5889 || (!comp && gfc_return_by_reference (sym));
5890 if (byref)
5892 if (se->direct_byref)
5894 /* Sometimes, too much indirection can be applied; e.g. for
5895 function_result = array_valued_recursive_function. */
5896 if (TREE_TYPE (TREE_TYPE (se->expr))
5897 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
5898 && GFC_DESCRIPTOR_TYPE_P
5899 (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
5900 se->expr = build_fold_indirect_ref_loc (input_location,
5901 se->expr);
5903 /* If the lhs of an assignment x = f(..) is allocatable and
5904 f2003 is allowed, we must do the automatic reallocation.
5905 TODO - deal with intrinsics, without using a temporary. */
5906 if (flag_realloc_lhs
5907 && se->ss && se->ss->loop_chain
5908 && se->ss->loop_chain->is_alloc_lhs
5909 && !expr->value.function.isym
5910 && sym->result->as != NULL)
5912 /* Evaluate the bounds of the result, if known. */
5913 gfc_set_loop_bounds_from_array_spec (&mapping, se,
5914 sym->result->as);
5916 /* Perform the automatic reallocation. */
5917 tmp = gfc_alloc_allocatable_for_assignment (se->loop,
5918 expr, NULL);
5919 gfc_add_expr_to_block (&se->pre, tmp);
5921 /* Pass the temporary as the first argument. */
5922 result = info->descriptor;
5924 else
5925 result = build_fold_indirect_ref_loc (input_location,
5926 se->expr);
5927 vec_safe_push (retargs, se->expr);
5929 else if (comp && comp->attr.dimension)
5931 gcc_assert (se->loop && info);
5933 /* Set the type of the array. */
5934 tmp = gfc_typenode_for_spec (&comp->ts);
5935 gcc_assert (se->ss->dimen == se->loop->dimen);
5937 /* Evaluate the bounds of the result, if known. */
5938 gfc_set_loop_bounds_from_array_spec (&mapping, se, comp->as);
5940 /* If the lhs of an assignment x = f(..) is allocatable and
5941 f2003 is allowed, we must not generate the function call
5942 here but should just send back the results of the mapping.
5943 This is signalled by the function ss being flagged. */
5944 if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
5946 gfc_free_interface_mapping (&mapping);
5947 return has_alternate_specifier;
5950 /* Create a temporary to store the result. In case the function
5951 returns a pointer, the temporary will be a shallow copy and
5952 mustn't be deallocated. */
5953 callee_alloc = comp->attr.allocatable || comp->attr.pointer;
5954 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
5955 tmp, NULL_TREE, false,
5956 !comp->attr.pointer, callee_alloc,
5957 &se->ss->info->expr->where);
5959 /* Pass the temporary as the first argument. */
5960 result = info->descriptor;
5961 tmp = gfc_build_addr_expr (NULL_TREE, result);
5962 vec_safe_push (retargs, tmp);
5964 else if (!comp && sym->result->attr.dimension)
5966 gcc_assert (se->loop && info);
5968 /* Set the type of the array. */
5969 tmp = gfc_typenode_for_spec (&ts);
5970 gcc_assert (se->ss->dimen == se->loop->dimen);
5972 /* Evaluate the bounds of the result, if known. */
5973 gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
5975 /* If the lhs of an assignment x = f(..) is allocatable and
5976 f2003 is allowed, we must not generate the function call
5977 here but should just send back the results of the mapping.
5978 This is signalled by the function ss being flagged. */
5979 if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
5981 gfc_free_interface_mapping (&mapping);
5982 return has_alternate_specifier;
5985 /* Create a temporary to store the result. In case the function
5986 returns a pointer, the temporary will be a shallow copy and
5987 mustn't be deallocated. */
5988 callee_alloc = sym->attr.allocatable || sym->attr.pointer;
5989 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
5990 tmp, NULL_TREE, false,
5991 !sym->attr.pointer, callee_alloc,
5992 &se->ss->info->expr->where);
5994 /* Pass the temporary as the first argument. */
5995 result = info->descriptor;
5996 tmp = gfc_build_addr_expr (NULL_TREE, result);
5997 vec_safe_push (retargs, tmp);
5999 else if (ts.type == BT_CHARACTER)
6001 /* Pass the string length. */
6002 type = gfc_get_character_type (ts.kind, ts.u.cl);
6003 type = build_pointer_type (type);
6005 /* Emit a DECL_EXPR for the VLA type. */
6006 tmp = TREE_TYPE (type);
6007 if (TYPE_SIZE (tmp)
6008 && TREE_CODE (TYPE_SIZE (tmp)) != INTEGER_CST)
6010 tmp = build_decl (input_location, TYPE_DECL, NULL_TREE, tmp);
6011 DECL_ARTIFICIAL (tmp) = 1;
6012 DECL_IGNORED_P (tmp) = 1;
6013 tmp = fold_build1_loc (input_location, DECL_EXPR,
6014 TREE_TYPE (tmp), tmp);
6015 gfc_add_expr_to_block (&se->pre, tmp);
6018 /* Return an address to a char[0:len-1]* temporary for
6019 character pointers. */
6020 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
6021 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
6023 var = gfc_create_var (type, "pstr");
6025 if ((!comp && sym->attr.allocatable)
6026 || (comp && comp->attr.allocatable))
6028 gfc_add_modify (&se->pre, var,
6029 fold_convert (TREE_TYPE (var),
6030 null_pointer_node));
6031 tmp = gfc_call_free (var);
6032 gfc_add_expr_to_block (&se->post, tmp);
6035 /* Provide an address expression for the function arguments. */
6036 var = gfc_build_addr_expr (NULL_TREE, var);
6038 else
6039 var = gfc_conv_string_tmp (se, type, len);
6041 vec_safe_push (retargs, var);
6043 else
6045 gcc_assert (flag_f2c && ts.type == BT_COMPLEX);
6047 type = gfc_get_complex_type (ts.kind);
6048 var = gfc_build_addr_expr (NULL_TREE, gfc_create_var (type, "cmplx"));
6049 vec_safe_push (retargs, var);
6052 /* Add the string length to the argument list. */
6053 if (ts.type == BT_CHARACTER && ts.deferred)
6055 tmp = len;
6056 if (!VAR_P (tmp))
6057 tmp = gfc_evaluate_now (len, &se->pre);
6058 TREE_STATIC (tmp) = 1;
6059 gfc_add_modify (&se->pre, tmp,
6060 build_int_cst (TREE_TYPE (tmp), 0));
6061 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
6062 vec_safe_push (retargs, tmp);
6064 else if (ts.type == BT_CHARACTER)
6065 vec_safe_push (retargs, len);
6067 gfc_free_interface_mapping (&mapping);
6069 /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS. */
6070 arglen = (vec_safe_length (arglist) + vec_safe_length (optionalargs)
6071 + vec_safe_length (stringargs) + vec_safe_length (append_args));
6072 vec_safe_reserve (retargs, arglen);
6074 /* Add the return arguments. */
6075 vec_safe_splice (retargs, arglist);
6077 /* Add the hidden present status for optional+value to the arguments. */
6078 vec_safe_splice (retargs, optionalargs);
6080 /* Add the hidden string length parameters to the arguments. */
6081 vec_safe_splice (retargs, stringargs);
6083 /* We may want to append extra arguments here. This is used e.g. for
6084 calls to libgfortran_matmul_??, which need extra information. */
6085 vec_safe_splice (retargs, append_args);
6087 arglist = retargs;
6089 /* Generate the actual call. */
6090 if (base_object == NULL_TREE)
6091 conv_function_val (se, sym, expr);
6092 else
6093 conv_base_obj_fcn_val (se, base_object, expr);
6095 /* If there are alternate return labels, function type should be
6096 integer. Can't modify the type in place though, since it can be shared
6097 with other functions. For dummy arguments, the typing is done to
6098 this result, even if it has to be repeated for each call. */
6099 if (has_alternate_specifier
6100 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
6102 if (!sym->attr.dummy)
6104 TREE_TYPE (sym->backend_decl)
6105 = build_function_type (integer_type_node,
6106 TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
6107 se->expr = gfc_build_addr_expr (NULL_TREE, sym->backend_decl);
6109 else
6110 TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
6113 fntype = TREE_TYPE (TREE_TYPE (se->expr));
6114 se->expr = build_call_vec (TREE_TYPE (fntype), se->expr, arglist);
6116 /* Allocatable scalar function results must be freed and nullified
6117 after use. This necessitates the creation of a temporary to
6118 hold the result to prevent duplicate calls. */
6119 if (!byref && sym->ts.type != BT_CHARACTER
6120 && sym->attr.allocatable && !sym->attr.dimension)
6122 tmp = gfc_create_var (TREE_TYPE (se->expr), NULL);
6123 gfc_add_modify (&se->pre, tmp, se->expr);
6124 se->expr = tmp;
6125 tmp = gfc_call_free (tmp);
6126 gfc_add_expr_to_block (&post, tmp);
6127 gfc_add_modify (&post, se->expr, build_int_cst (TREE_TYPE (se->expr), 0));
6130 /* If we have a pointer function, but we don't want a pointer, e.g.
6131 something like
6132 x = f()
6133 where f is pointer valued, we have to dereference the result. */
6134 if (!se->want_pointer && !byref
6135 && ((!comp && (sym->attr.pointer || sym->attr.allocatable))
6136 || (comp && (comp->attr.pointer || comp->attr.allocatable))))
6137 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
6139 /* f2c calling conventions require a scalar default real function to
6140 return a double precision result. Convert this back to default
6141 real. We only care about the cases that can happen in Fortran 77.
6143 if (flag_f2c && sym->ts.type == BT_REAL
6144 && sym->ts.kind == gfc_default_real_kind
6145 && !sym->attr.always_explicit)
6146 se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
6148 /* A pure function may still have side-effects - it may modify its
6149 parameters. */
6150 TREE_SIDE_EFFECTS (se->expr) = 1;
6151 #if 0
6152 if (!sym->attr.pure)
6153 TREE_SIDE_EFFECTS (se->expr) = 1;
6154 #endif
6156 if (byref)
6158 /* Add the function call to the pre chain. There is no expression. */
6159 gfc_add_expr_to_block (&se->pre, se->expr);
6160 se->expr = NULL_TREE;
6162 if (!se->direct_byref)
6164 if ((sym->attr.dimension && !comp) || (comp && comp->attr.dimension))
6166 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
6168 /* Check the data pointer hasn't been modified. This would
6169 happen in a function returning a pointer. */
6170 tmp = gfc_conv_descriptor_data_get (info->descriptor);
6171 tmp = fold_build2_loc (input_location, NE_EXPR,
6172 boolean_type_node,
6173 tmp, info->data);
6174 gfc_trans_runtime_check (true, false, tmp, &se->pre, NULL,
6175 gfc_msg_fault);
6177 se->expr = info->descriptor;
6178 /* Bundle in the string length. */
6179 se->string_length = len;
6181 else if (ts.type == BT_CHARACTER)
6183 /* Dereference for character pointer results. */
6184 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
6185 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
6186 se->expr = build_fold_indirect_ref_loc (input_location, var);
6187 else
6188 se->expr = var;
6190 se->string_length = len;
6192 else
6194 gcc_assert (ts.type == BT_COMPLEX && flag_f2c);
6195 se->expr = build_fold_indirect_ref_loc (input_location, var);
6200 /* Associate the rhs class object's meta-data with the result, when the
6201 result is a temporary. */
6202 if (args && args->expr && args->expr->ts.type == BT_CLASS
6203 && sym->ts.type == BT_CLASS && result != NULL_TREE && DECL_P (result)
6204 && !GFC_CLASS_TYPE_P (TREE_TYPE (result)))
6206 gfc_se parmse;
6207 gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (args->expr);
6209 gfc_init_se (&parmse, NULL);
6210 parmse.data_not_needed = 1;
6211 gfc_conv_expr (&parmse, class_expr);
6212 if (!DECL_LANG_SPECIFIC (result))
6213 gfc_allocate_lang_decl (result);
6214 GFC_DECL_SAVED_DESCRIPTOR (result) = parmse.expr;
6215 gfc_free_expr (class_expr);
6216 gcc_assert (parmse.pre.head == NULL_TREE
6217 && parmse.post.head == NULL_TREE);
6220 /* Follow the function call with the argument post block. */
6221 if (byref)
6223 gfc_add_block_to_block (&se->pre, &post);
6225 /* Transformational functions of derived types with allocatable
6226 components must have the result allocatable components copied. */
6227 arg = expr->value.function.actual;
6228 if (result && arg && expr->rank
6229 && expr->value.function.isym
6230 && expr->value.function.isym->transformational
6231 && arg->expr->ts.type == BT_DERIVED
6232 && arg->expr->ts.u.derived->attr.alloc_comp)
6234 tree tmp2;
6235 /* Copy the allocatable components. We have to use a
6236 temporary here to prevent source allocatable components
6237 from being corrupted. */
6238 tmp2 = gfc_evaluate_now (result, &se->pre);
6239 tmp = gfc_copy_alloc_comp (arg->expr->ts.u.derived,
6240 result, tmp2, expr->rank, 0);
6241 gfc_add_expr_to_block (&se->pre, tmp);
6242 tmp = gfc_copy_allocatable_data (result, tmp2, TREE_TYPE(tmp2),
6243 expr->rank);
6244 gfc_add_expr_to_block (&se->pre, tmp);
6246 /* Finally free the temporary's data field. */
6247 tmp = gfc_conv_descriptor_data_get (tmp2);
6248 tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
6249 NULL_TREE, NULL_TREE, true,
6250 NULL, GFC_CAF_COARRAY_NOCOARRAY);
6251 gfc_add_expr_to_block (&se->pre, tmp);
6254 else
6256 /* For a function with a class array result, save the result as
6257 a temporary, set the info fields needed by the scalarizer and
6258 call the finalization function of the temporary. Note that the
6259 nullification of allocatable components needed by the result
6260 is done in gfc_trans_assignment_1. */
6261 if (expr && ((gfc_is_alloc_class_array_function (expr)
6262 && se->ss && se->ss->loop)
6263 || gfc_is_alloc_class_scalar_function (expr))
6264 && se->expr && GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
6265 && expr->must_finalize)
6267 tree final_fndecl;
6268 tree is_final;
6269 int n;
6270 if (se->ss && se->ss->loop)
6272 se->expr = gfc_evaluate_now (se->expr, &se->ss->loop->pre);
6273 tmp = gfc_class_data_get (se->expr);
6274 info->descriptor = tmp;
6275 info->data = gfc_conv_descriptor_data_get (tmp);
6276 info->offset = gfc_conv_descriptor_offset_get (tmp);
6277 for (n = 0; n < se->ss->loop->dimen; n++)
6279 tree dim = gfc_rank_cst[n];
6280 se->ss->loop->to[n] = gfc_conv_descriptor_ubound_get (tmp, dim);
6281 se->ss->loop->from[n] = gfc_conv_descriptor_lbound_get (tmp, dim);
6284 else
6286 /* TODO Eliminate the doubling of temporaries. This
6287 one is necessary to ensure no memory leakage. */
6288 se->expr = gfc_evaluate_now (se->expr, &se->pre);
6289 tmp = gfc_class_data_get (se->expr);
6290 tmp = gfc_conv_scalar_to_descriptor (se, tmp,
6291 CLASS_DATA (expr->value.function.esym->result)->attr);
6294 final_fndecl = gfc_class_vtab_final_get (se->expr);
6295 is_final = fold_build2_loc (input_location, NE_EXPR,
6296 boolean_type_node,
6297 final_fndecl,
6298 fold_convert (TREE_TYPE (final_fndecl),
6299 null_pointer_node));
6300 final_fndecl = build_fold_indirect_ref_loc (input_location,
6301 final_fndecl);
6302 tmp = build_call_expr_loc (input_location,
6303 final_fndecl, 3,
6304 gfc_build_addr_expr (NULL, tmp),
6305 gfc_class_vtab_size_get (se->expr),
6306 boolean_false_node);
6307 tmp = fold_build3_loc (input_location, COND_EXPR,
6308 void_type_node, is_final, tmp,
6309 build_empty_stmt (input_location));
6311 if (se->ss && se->ss->loop)
6313 gfc_add_expr_to_block (&se->ss->loop->post, tmp);
6314 tmp = gfc_call_free (info->data);
6315 gfc_add_expr_to_block (&se->ss->loop->post, tmp);
6317 else
6319 gfc_add_expr_to_block (&se->post, tmp);
6320 tmp = gfc_class_data_get (se->expr);
6321 tmp = gfc_call_free (tmp);
6322 gfc_add_expr_to_block (&se->post, tmp);
6324 expr->must_finalize = 0;
6327 gfc_add_block_to_block (&se->post, &post);
6330 return has_alternate_specifier;
6334 /* Fill a character string with spaces. */
6336 static tree
6337 fill_with_spaces (tree start, tree type, tree size)
6339 stmtblock_t block, loop;
6340 tree i, el, exit_label, cond, tmp;
6342 /* For a simple char type, we can call memset(). */
6343 if (compare_tree_int (TYPE_SIZE_UNIT (type), 1) == 0)
6344 return build_call_expr_loc (input_location,
6345 builtin_decl_explicit (BUILT_IN_MEMSET),
6346 3, start,
6347 build_int_cst (gfc_get_int_type (gfc_c_int_kind),
6348 lang_hooks.to_target_charset (' ')),
6349 size);
6351 /* Otherwise, we use a loop:
6352 for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
6353 *el = (type) ' ';
6356 /* Initialize variables. */
6357 gfc_init_block (&block);
6358 i = gfc_create_var (sizetype, "i");
6359 gfc_add_modify (&block, i, fold_convert (sizetype, size));
6360 el = gfc_create_var (build_pointer_type (type), "el");
6361 gfc_add_modify (&block, el, fold_convert (TREE_TYPE (el), start));
6362 exit_label = gfc_build_label_decl (NULL_TREE);
6363 TREE_USED (exit_label) = 1;
6366 /* Loop body. */
6367 gfc_init_block (&loop);
6369 /* Exit condition. */
6370 cond = fold_build2_loc (input_location, LE_EXPR, boolean_type_node, i,
6371 build_zero_cst (sizetype));
6372 tmp = build1_v (GOTO_EXPR, exit_label);
6373 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
6374 build_empty_stmt (input_location));
6375 gfc_add_expr_to_block (&loop, tmp);
6377 /* Assignment. */
6378 gfc_add_modify (&loop,
6379 fold_build1_loc (input_location, INDIRECT_REF, type, el),
6380 build_int_cst (type, lang_hooks.to_target_charset (' ')));
6382 /* Increment loop variables. */
6383 gfc_add_modify (&loop, i,
6384 fold_build2_loc (input_location, MINUS_EXPR, sizetype, i,
6385 TYPE_SIZE_UNIT (type)));
6386 gfc_add_modify (&loop, el,
6387 fold_build_pointer_plus_loc (input_location,
6388 el, TYPE_SIZE_UNIT (type)));
6390 /* Making the loop... actually loop! */
6391 tmp = gfc_finish_block (&loop);
6392 tmp = build1_v (LOOP_EXPR, tmp);
6393 gfc_add_expr_to_block (&block, tmp);
6395 /* The exit label. */
6396 tmp = build1_v (LABEL_EXPR, exit_label);
6397 gfc_add_expr_to_block (&block, tmp);
6400 return gfc_finish_block (&block);
6404 /* Generate code to copy a string. */
6406 void
6407 gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
6408 int dkind, tree slength, tree src, int skind)
6410 tree tmp, dlen, slen;
6411 tree dsc;
6412 tree ssc;
6413 tree cond;
6414 tree cond2;
6415 tree tmp2;
6416 tree tmp3;
6417 tree tmp4;
6418 tree chartype;
6419 stmtblock_t tempblock;
6421 gcc_assert (dkind == skind);
6423 if (slength != NULL_TREE)
6425 slen = fold_convert (size_type_node, gfc_evaluate_now (slength, block));
6426 ssc = gfc_string_to_single_character (slen, src, skind);
6428 else
6430 slen = build_int_cst (size_type_node, 1);
6431 ssc = src;
6434 if (dlength != NULL_TREE)
6436 dlen = fold_convert (size_type_node, gfc_evaluate_now (dlength, block));
6437 dsc = gfc_string_to_single_character (dlen, dest, dkind);
6439 else
6441 dlen = build_int_cst (size_type_node, 1);
6442 dsc = dest;
6445 /* Assign directly if the types are compatible. */
6446 if (dsc != NULL_TREE && ssc != NULL_TREE
6447 && TREE_TYPE (dsc) == TREE_TYPE (ssc))
6449 gfc_add_modify (block, dsc, ssc);
6450 return;
6453 /* The string copy algorithm below generates code like
6455 if (dlen > 0) {
6456 memmove (dest, src, min(dlen, slen));
6457 if (slen < dlen)
6458 memset(&dest[slen], ' ', dlen - slen);
6462 /* Do nothing if the destination length is zero. */
6463 cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node, dlen,
6464 build_int_cst (size_type_node, 0));
6466 /* For non-default character kinds, we have to multiply the string
6467 length by the base type size. */
6468 chartype = gfc_get_char_type (dkind);
6469 slen = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
6470 fold_convert (size_type_node, slen),
6471 fold_convert (size_type_node,
6472 TYPE_SIZE_UNIT (chartype)));
6473 dlen = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
6474 fold_convert (size_type_node, dlen),
6475 fold_convert (size_type_node,
6476 TYPE_SIZE_UNIT (chartype)));
6478 if (dlength && POINTER_TYPE_P (TREE_TYPE (dest)))
6479 dest = fold_convert (pvoid_type_node, dest);
6480 else
6481 dest = gfc_build_addr_expr (pvoid_type_node, dest);
6483 if (slength && POINTER_TYPE_P (TREE_TYPE (src)))
6484 src = fold_convert (pvoid_type_node, src);
6485 else
6486 src = gfc_build_addr_expr (pvoid_type_node, src);
6488 /* First do the memmove. */
6489 tmp2 = fold_build2_loc (input_location, MIN_EXPR, TREE_TYPE (dlen), dlen,
6490 slen);
6491 tmp2 = build_call_expr_loc (input_location,
6492 builtin_decl_explicit (BUILT_IN_MEMMOVE),
6493 3, dest, src, tmp2);
6494 stmtblock_t tmpblock2;
6495 gfc_init_block (&tmpblock2);
6496 gfc_add_expr_to_block (&tmpblock2, tmp2);
6498 /* If the destination is longer, fill the end with spaces. */
6499 cond2 = fold_build2_loc (input_location, LT_EXPR, boolean_type_node, slen,
6500 dlen);
6502 /* Wstringop-overflow appears at -O3 even though this warning is not
6503 explicitly available in fortran nor can it be switched off. If the
6504 source length is a constant, its negative appears as a very large
6505 postive number and triggers the warning in BUILTIN_MEMSET. Fixing
6506 the result of the MINUS_EXPR suppresses this spurious warning. */
6507 tmp = fold_build2_loc (input_location, MINUS_EXPR,
6508 TREE_TYPE(dlen), dlen, slen);
6509 if (slength && TREE_CONSTANT (slength))
6510 tmp = gfc_evaluate_now (tmp, block);
6512 tmp4 = fold_build_pointer_plus_loc (input_location, dest, slen);
6513 tmp4 = fill_with_spaces (tmp4, chartype, tmp);
6515 gfc_init_block (&tempblock);
6516 gfc_add_expr_to_block (&tempblock, tmp4);
6517 tmp3 = gfc_finish_block (&tempblock);
6519 /* The whole copy_string function is there. */
6520 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
6521 tmp3, build_empty_stmt (input_location));
6522 gfc_add_expr_to_block (&tmpblock2, tmp);
6523 tmp = gfc_finish_block (&tmpblock2);
6524 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
6525 build_empty_stmt (input_location));
6526 gfc_add_expr_to_block (block, tmp);
6530 /* Translate a statement function.
6531 The value of a statement function reference is obtained by evaluating the
6532 expression using the values of the actual arguments for the values of the
6533 corresponding dummy arguments. */
6535 static void
6536 gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
6538 gfc_symbol *sym;
6539 gfc_symbol *fsym;
6540 gfc_formal_arglist *fargs;
6541 gfc_actual_arglist *args;
6542 gfc_se lse;
6543 gfc_se rse;
6544 gfc_saved_var *saved_vars;
6545 tree *temp_vars;
6546 tree type;
6547 tree tmp;
6548 int n;
6550 sym = expr->symtree->n.sym;
6551 args = expr->value.function.actual;
6552 gfc_init_se (&lse, NULL);
6553 gfc_init_se (&rse, NULL);
6555 n = 0;
6556 for (fargs = gfc_sym_get_dummy_args (sym); fargs; fargs = fargs->next)
6557 n++;
6558 saved_vars = XCNEWVEC (gfc_saved_var, n);
6559 temp_vars = XCNEWVEC (tree, n);
6561 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
6562 fargs = fargs->next, n++)
6564 /* Each dummy shall be specified, explicitly or implicitly, to be
6565 scalar. */
6566 gcc_assert (fargs->sym->attr.dimension == 0);
6567 fsym = fargs->sym;
6569 if (fsym->ts.type == BT_CHARACTER)
6571 /* Copy string arguments. */
6572 tree arglen;
6574 gcc_assert (fsym->ts.u.cl && fsym->ts.u.cl->length
6575 && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT);
6577 /* Create a temporary to hold the value. */
6578 if (fsym->ts.u.cl->backend_decl == NULL_TREE)
6579 fsym->ts.u.cl->backend_decl
6580 = gfc_conv_constant_to_tree (fsym->ts.u.cl->length);
6582 type = gfc_get_character_type (fsym->ts.kind, fsym->ts.u.cl);
6583 temp_vars[n] = gfc_create_var (type, fsym->name);
6585 arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
6587 gfc_conv_expr (&rse, args->expr);
6588 gfc_conv_string_parameter (&rse);
6589 gfc_add_block_to_block (&se->pre, &lse.pre);
6590 gfc_add_block_to_block (&se->pre, &rse.pre);
6592 gfc_trans_string_copy (&se->pre, arglen, temp_vars[n], fsym->ts.kind,
6593 rse.string_length, rse.expr, fsym->ts.kind);
6594 gfc_add_block_to_block (&se->pre, &lse.post);
6595 gfc_add_block_to_block (&se->pre, &rse.post);
6597 else
6599 /* For everything else, just evaluate the expression. */
6601 /* Create a temporary to hold the value. */
6602 type = gfc_typenode_for_spec (&fsym->ts);
6603 temp_vars[n] = gfc_create_var (type, fsym->name);
6605 gfc_conv_expr (&lse, args->expr);
6607 gfc_add_block_to_block (&se->pre, &lse.pre);
6608 gfc_add_modify (&se->pre, temp_vars[n], lse.expr);
6609 gfc_add_block_to_block (&se->pre, &lse.post);
6612 args = args->next;
6615 /* Use the temporary variables in place of the real ones. */
6616 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
6617 fargs = fargs->next, n++)
6618 gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
6620 gfc_conv_expr (se, sym->value);
6622 if (sym->ts.type == BT_CHARACTER)
6624 gfc_conv_const_charlen (sym->ts.u.cl);
6626 /* Force the expression to the correct length. */
6627 if (!INTEGER_CST_P (se->string_length)
6628 || tree_int_cst_lt (se->string_length,
6629 sym->ts.u.cl->backend_decl))
6631 type = gfc_get_character_type (sym->ts.kind, sym->ts.u.cl);
6632 tmp = gfc_create_var (type, sym->name);
6633 tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
6634 gfc_trans_string_copy (&se->pre, sym->ts.u.cl->backend_decl, tmp,
6635 sym->ts.kind, se->string_length, se->expr,
6636 sym->ts.kind);
6637 se->expr = tmp;
6639 se->string_length = sym->ts.u.cl->backend_decl;
6642 /* Restore the original variables. */
6643 for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
6644 fargs = fargs->next, n++)
6645 gfc_restore_sym (fargs->sym, &saved_vars[n]);
6646 free (temp_vars);
6647 free (saved_vars);
6651 /* Translate a function expression. */
6653 static void
6654 gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
6656 gfc_symbol *sym;
6658 if (expr->value.function.isym)
6660 gfc_conv_intrinsic_function (se, expr);
6661 return;
6664 /* expr.value.function.esym is the resolved (specific) function symbol for
6665 most functions. However this isn't set for dummy procedures. */
6666 sym = expr->value.function.esym;
6667 if (!sym)
6668 sym = expr->symtree->n.sym;
6670 /* The IEEE_ARITHMETIC functions are caught here. */
6671 if (sym->from_intmod == INTMOD_IEEE_ARITHMETIC)
6672 if (gfc_conv_ieee_arithmetic_function (se, expr))
6673 return;
6675 /* We distinguish statement functions from general functions to improve
6676 runtime performance. */
6677 if (sym->attr.proc == PROC_ST_FUNCTION)
6679 gfc_conv_statement_function (se, expr);
6680 return;
6683 gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
6684 NULL);
6688 /* Determine whether the given EXPR_CONSTANT is a zero initializer. */
6690 static bool
6691 is_zero_initializer_p (gfc_expr * expr)
6693 if (expr->expr_type != EXPR_CONSTANT)
6694 return false;
6696 /* We ignore constants with prescribed memory representations for now. */
6697 if (expr->representation.string)
6698 return false;
6700 switch (expr->ts.type)
6702 case BT_INTEGER:
6703 return mpz_cmp_si (expr->value.integer, 0) == 0;
6705 case BT_REAL:
6706 return mpfr_zero_p (expr->value.real)
6707 && MPFR_SIGN (expr->value.real) >= 0;
6709 case BT_LOGICAL:
6710 return expr->value.logical == 0;
6712 case BT_COMPLEX:
6713 return mpfr_zero_p (mpc_realref (expr->value.complex))
6714 && MPFR_SIGN (mpc_realref (expr->value.complex)) >= 0
6715 && mpfr_zero_p (mpc_imagref (expr->value.complex))
6716 && MPFR_SIGN (mpc_imagref (expr->value.complex)) >= 0;
6718 default:
6719 break;
6721 return false;
6725 static void
6726 gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
6728 gfc_ss *ss;
6730 ss = se->ss;
6731 gcc_assert (ss != NULL && ss != gfc_ss_terminator);
6732 gcc_assert (ss->info->expr == expr && ss->info->type == GFC_SS_CONSTRUCTOR);
6734 gfc_conv_tmp_array_ref (se);
6738 /* Build a static initializer. EXPR is the expression for the initial value.
6739 The other parameters describe the variable of the component being
6740 initialized. EXPR may be null. */
6742 tree
6743 gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
6744 bool array, bool pointer, bool procptr)
6746 gfc_se se;
6748 if (flag_coarray != GFC_FCOARRAY_LIB && ts->type == BT_DERIVED
6749 && ts->u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
6750 && ts->u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
6751 return build_constructor (type, NULL);
6753 if (!(expr || pointer || procptr))
6754 return NULL_TREE;
6756 /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
6757 (these are the only two iso_c_binding derived types that can be
6758 used as initialization expressions). If so, we need to modify
6759 the 'expr' to be that for a (void *). */
6760 if (expr != NULL && expr->ts.type == BT_DERIVED
6761 && expr->ts.is_iso_c && expr->ts.u.derived)
6763 gfc_symbol *derived = expr->ts.u.derived;
6765 /* The derived symbol has already been converted to a (void *). Use
6766 its kind. */
6767 expr = gfc_get_int_expr (derived->ts.kind, NULL, 0);
6768 expr->ts.f90_type = derived->ts.f90_type;
6770 gfc_init_se (&se, NULL);
6771 gfc_conv_constant (&se, expr);
6772 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
6773 return se.expr;
6776 if (array && !procptr)
6778 tree ctor;
6779 /* Arrays need special handling. */
6780 if (pointer)
6781 ctor = gfc_build_null_descriptor (type);
6782 /* Special case assigning an array to zero. */
6783 else if (is_zero_initializer_p (expr))
6784 ctor = build_constructor (type, NULL);
6785 else
6786 ctor = gfc_conv_array_initializer (type, expr);
6787 TREE_STATIC (ctor) = 1;
6788 return ctor;
6790 else if (pointer || procptr)
6792 if (ts->type == BT_CLASS && !procptr)
6794 gfc_init_se (&se, NULL);
6795 gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
6796 gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
6797 TREE_STATIC (se.expr) = 1;
6798 return se.expr;
6800 else if (!expr || expr->expr_type == EXPR_NULL)
6801 return fold_convert (type, null_pointer_node);
6802 else
6804 gfc_init_se (&se, NULL);
6805 se.want_pointer = 1;
6806 gfc_conv_expr (&se, expr);
6807 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
6808 return se.expr;
6811 else
6813 switch (ts->type)
6815 case_bt_struct:
6816 case BT_CLASS:
6817 gfc_init_se (&se, NULL);
6818 if (ts->type == BT_CLASS && expr->expr_type == EXPR_NULL)
6819 gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
6820 else
6821 gfc_conv_structure (&se, expr, 1);
6822 gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
6823 TREE_STATIC (se.expr) = 1;
6824 return se.expr;
6826 case BT_CHARACTER:
6828 tree ctor = gfc_conv_string_init (ts->u.cl->backend_decl,expr);
6829 TREE_STATIC (ctor) = 1;
6830 return ctor;
6833 default:
6834 gfc_init_se (&se, NULL);
6835 gfc_conv_constant (&se, expr);
6836 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
6837 return se.expr;
6842 static tree
6843 gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
6845 gfc_se rse;
6846 gfc_se lse;
6847 gfc_ss *rss;
6848 gfc_ss *lss;
6849 gfc_array_info *lss_array;
6850 stmtblock_t body;
6851 stmtblock_t block;
6852 gfc_loopinfo loop;
6853 int n;
6854 tree tmp;
6856 gfc_start_block (&block);
6858 /* Initialize the scalarizer. */
6859 gfc_init_loopinfo (&loop);
6861 gfc_init_se (&lse, NULL);
6862 gfc_init_se (&rse, NULL);
6864 /* Walk the rhs. */
6865 rss = gfc_walk_expr (expr);
6866 if (rss == gfc_ss_terminator)
6867 /* The rhs is scalar. Add a ss for the expression. */
6868 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr);
6870 /* Create a SS for the destination. */
6871 lss = gfc_get_array_ss (gfc_ss_terminator, NULL, cm->as->rank,
6872 GFC_SS_COMPONENT);
6873 lss_array = &lss->info->data.array;
6874 lss_array->shape = gfc_get_shape (cm->as->rank);
6875 lss_array->descriptor = dest;
6876 lss_array->data = gfc_conv_array_data (dest);
6877 lss_array->offset = gfc_conv_array_offset (dest);
6878 for (n = 0; n < cm->as->rank; n++)
6880 lss_array->start[n] = gfc_conv_array_lbound (dest, n);
6881 lss_array->stride[n] = gfc_index_one_node;
6883 mpz_init (lss_array->shape[n]);
6884 mpz_sub (lss_array->shape[n], cm->as->upper[n]->value.integer,
6885 cm->as->lower[n]->value.integer);
6886 mpz_add_ui (lss_array->shape[n], lss_array->shape[n], 1);
6889 /* Associate the SS with the loop. */
6890 gfc_add_ss_to_loop (&loop, lss);
6891 gfc_add_ss_to_loop (&loop, rss);
6893 /* Calculate the bounds of the scalarization. */
6894 gfc_conv_ss_startstride (&loop);
6896 /* Setup the scalarizing loops. */
6897 gfc_conv_loop_setup (&loop, &expr->where);
6899 /* Setup the gfc_se structures. */
6900 gfc_copy_loopinfo_to_se (&lse, &loop);
6901 gfc_copy_loopinfo_to_se (&rse, &loop);
6903 rse.ss = rss;
6904 gfc_mark_ss_chain_used (rss, 1);
6905 lse.ss = lss;
6906 gfc_mark_ss_chain_used (lss, 1);
6908 /* Start the scalarized loop body. */
6909 gfc_start_scalarized_body (&loop, &body);
6911 gfc_conv_tmp_array_ref (&lse);
6912 if (cm->ts.type == BT_CHARACTER)
6913 lse.string_length = cm->ts.u.cl->backend_decl;
6915 gfc_conv_expr (&rse, expr);
6917 tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false);
6918 gfc_add_expr_to_block (&body, tmp);
6920 gcc_assert (rse.ss == gfc_ss_terminator);
6922 /* Generate the copying loops. */
6923 gfc_trans_scalarizing_loops (&loop, &body);
6925 /* Wrap the whole thing up. */
6926 gfc_add_block_to_block (&block, &loop.pre);
6927 gfc_add_block_to_block (&block, &loop.post);
6929 gcc_assert (lss_array->shape != NULL);
6930 gfc_free_shape (&lss_array->shape, cm->as->rank);
6931 gfc_cleanup_loop (&loop);
6933 return gfc_finish_block (&block);
6937 static tree
6938 gfc_trans_alloc_subarray_assign (tree dest, gfc_component * cm,
6939 gfc_expr * expr)
6941 gfc_se se;
6942 stmtblock_t block;
6943 tree offset;
6944 int n;
6945 tree tmp;
6946 tree tmp2;
6947 gfc_array_spec *as;
6948 gfc_expr *arg = NULL;
6950 gfc_start_block (&block);
6951 gfc_init_se (&se, NULL);
6953 /* Get the descriptor for the expressions. */
6954 se.want_pointer = 0;
6955 gfc_conv_expr_descriptor (&se, expr);
6956 gfc_add_block_to_block (&block, &se.pre);
6957 gfc_add_modify (&block, dest, se.expr);
6959 /* Deal with arrays of derived types with allocatable components. */
6960 if (gfc_bt_struct (cm->ts.type)
6961 && cm->ts.u.derived->attr.alloc_comp)
6962 // TODO: Fix caf_mode
6963 tmp = gfc_copy_alloc_comp (cm->ts.u.derived,
6964 se.expr, dest,
6965 cm->as->rank, 0);
6966 else if (cm->ts.type == BT_CLASS && expr->ts.type == BT_DERIVED
6967 && CLASS_DATA(cm)->attr.allocatable)
6969 if (cm->ts.u.derived->attr.alloc_comp)
6970 // TODO: Fix caf_mode
6971 tmp = gfc_copy_alloc_comp (expr->ts.u.derived,
6972 se.expr, dest,
6973 expr->rank, 0);
6974 else
6976 tmp = TREE_TYPE (dest);
6977 tmp = gfc_duplicate_allocatable (dest, se.expr,
6978 tmp, expr->rank, NULL_TREE);
6981 else
6982 tmp = gfc_duplicate_allocatable (dest, se.expr,
6983 TREE_TYPE(cm->backend_decl),
6984 cm->as->rank, NULL_TREE);
6986 gfc_add_expr_to_block (&block, tmp);
6987 gfc_add_block_to_block (&block, &se.post);
6989 if (expr->expr_type != EXPR_VARIABLE)
6990 gfc_conv_descriptor_data_set (&block, se.expr,
6991 null_pointer_node);
6993 /* We need to know if the argument of a conversion function is a
6994 variable, so that the correct lower bound can be used. */
6995 if (expr->expr_type == EXPR_FUNCTION
6996 && expr->value.function.isym
6997 && expr->value.function.isym->conversion
6998 && expr->value.function.actual->expr
6999 && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE)
7000 arg = expr->value.function.actual->expr;
7002 /* Obtain the array spec of full array references. */
7003 if (arg)
7004 as = gfc_get_full_arrayspec_from_expr (arg);
7005 else
7006 as = gfc_get_full_arrayspec_from_expr (expr);
7008 /* Shift the lbound and ubound of temporaries to being unity,
7009 rather than zero, based. Always calculate the offset. */
7010 offset = gfc_conv_descriptor_offset_get (dest);
7011 gfc_add_modify (&block, offset, gfc_index_zero_node);
7012 tmp2 =gfc_create_var (gfc_array_index_type, NULL);
7014 for (n = 0; n < expr->rank; n++)
7016 tree span;
7017 tree lbound;
7019 /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
7020 TODO It looks as if gfc_conv_expr_descriptor should return
7021 the correct bounds and that the following should not be
7022 necessary. This would simplify gfc_conv_intrinsic_bound
7023 as well. */
7024 if (as && as->lower[n])
7026 gfc_se lbse;
7027 gfc_init_se (&lbse, NULL);
7028 gfc_conv_expr (&lbse, as->lower[n]);
7029 gfc_add_block_to_block (&block, &lbse.pre);
7030 lbound = gfc_evaluate_now (lbse.expr, &block);
7032 else if (as && arg)
7034 tmp = gfc_get_symbol_decl (arg->symtree->n.sym);
7035 lbound = gfc_conv_descriptor_lbound_get (tmp,
7036 gfc_rank_cst[n]);
7038 else if (as)
7039 lbound = gfc_conv_descriptor_lbound_get (dest,
7040 gfc_rank_cst[n]);
7041 else
7042 lbound = gfc_index_one_node;
7044 lbound = fold_convert (gfc_array_index_type, lbound);
7046 /* Shift the bounds and set the offset accordingly. */
7047 tmp = gfc_conv_descriptor_ubound_get (dest, gfc_rank_cst[n]);
7048 span = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
7049 tmp, gfc_conv_descriptor_lbound_get (dest, gfc_rank_cst[n]));
7050 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
7051 span, lbound);
7052 gfc_conv_descriptor_ubound_set (&block, dest,
7053 gfc_rank_cst[n], tmp);
7054 gfc_conv_descriptor_lbound_set (&block, dest,
7055 gfc_rank_cst[n], lbound);
7057 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
7058 gfc_conv_descriptor_lbound_get (dest,
7059 gfc_rank_cst[n]),
7060 gfc_conv_descriptor_stride_get (dest,
7061 gfc_rank_cst[n]));
7062 gfc_add_modify (&block, tmp2, tmp);
7063 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
7064 offset, tmp2);
7065 gfc_conv_descriptor_offset_set (&block, dest, tmp);
7068 if (arg)
7070 /* If a conversion expression has a null data pointer
7071 argument, nullify the allocatable component. */
7072 tree non_null_expr;
7073 tree null_expr;
7075 if (arg->symtree->n.sym->attr.allocatable
7076 || arg->symtree->n.sym->attr.pointer)
7078 non_null_expr = gfc_finish_block (&block);
7079 gfc_start_block (&block);
7080 gfc_conv_descriptor_data_set (&block, dest,
7081 null_pointer_node);
7082 null_expr = gfc_finish_block (&block);
7083 tmp = gfc_conv_descriptor_data_get (arg->symtree->n.sym->backend_decl);
7084 tmp = build2_loc (input_location, EQ_EXPR, boolean_type_node, tmp,
7085 fold_convert (TREE_TYPE (tmp), null_pointer_node));
7086 return build3_v (COND_EXPR, tmp,
7087 null_expr, non_null_expr);
7091 return gfc_finish_block (&block);
7095 /* Allocate or reallocate scalar component, as necessary. */
7097 static void
7098 alloc_scalar_allocatable_for_subcomponent_assignment (stmtblock_t *block,
7099 tree comp,
7100 gfc_component *cm,
7101 gfc_expr *expr2,
7102 gfc_symbol *sym)
7104 tree tmp;
7105 tree ptr;
7106 tree size;
7107 tree size_in_bytes;
7108 tree lhs_cl_size = NULL_TREE;
7110 if (!comp)
7111 return;
7113 if (!expr2 || expr2->rank)
7114 return;
7116 realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
7118 if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
7120 char name[GFC_MAX_SYMBOL_LEN+9];
7121 gfc_component *strlen;
7122 /* Use the rhs string length and the lhs element size. */
7123 gcc_assert (expr2->ts.type == BT_CHARACTER);
7124 if (!expr2->ts.u.cl->backend_decl)
7126 gfc_conv_string_length (expr2->ts.u.cl, expr2, block);
7127 gcc_assert (expr2->ts.u.cl->backend_decl);
7130 size = expr2->ts.u.cl->backend_decl;
7132 /* Ensure that cm->ts.u.cl->backend_decl is a componentref to _%s_length
7133 component. */
7134 sprintf (name, "_%s_length", cm->name);
7135 strlen = gfc_find_component (sym, name, true, true, NULL);
7136 lhs_cl_size = fold_build3_loc (input_location, COMPONENT_REF,
7137 gfc_charlen_type_node,
7138 TREE_OPERAND (comp, 0),
7139 strlen->backend_decl, NULL_TREE);
7141 tmp = TREE_TYPE (gfc_typenode_for_spec (&cm->ts));
7142 tmp = TYPE_SIZE_UNIT (tmp);
7143 size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
7144 TREE_TYPE (tmp), tmp,
7145 fold_convert (TREE_TYPE (tmp), size));
7147 else if (cm->ts.type == BT_CLASS)
7149 gcc_assert (expr2->ts.type == BT_CLASS || expr2->ts.type == BT_DERIVED);
7150 if (expr2->ts.type == BT_DERIVED)
7152 tmp = gfc_get_symbol_decl (expr2->ts.u.derived);
7153 size = TYPE_SIZE_UNIT (tmp);
7155 else
7157 gfc_expr *e2vtab;
7158 gfc_se se;
7159 e2vtab = gfc_find_and_cut_at_last_class_ref (expr2);
7160 gfc_add_vptr_component (e2vtab);
7161 gfc_add_size_component (e2vtab);
7162 gfc_init_se (&se, NULL);
7163 gfc_conv_expr (&se, e2vtab);
7164 gfc_add_block_to_block (block, &se.pre);
7165 size = fold_convert (size_type_node, se.expr);
7166 gfc_free_expr (e2vtab);
7168 size_in_bytes = size;
7170 else
7172 /* Otherwise use the length in bytes of the rhs. */
7173 size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&cm->ts));
7174 size_in_bytes = size;
7177 size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
7178 size_in_bytes, size_one_node);
7180 if (cm->ts.type == BT_DERIVED && cm->ts.u.derived->attr.alloc_comp)
7182 tmp = build_call_expr_loc (input_location,
7183 builtin_decl_explicit (BUILT_IN_CALLOC),
7184 2, build_one_cst (size_type_node),
7185 size_in_bytes);
7186 tmp = fold_convert (TREE_TYPE (comp), tmp);
7187 gfc_add_modify (block, comp, tmp);
7189 else
7191 tmp = build_call_expr_loc (input_location,
7192 builtin_decl_explicit (BUILT_IN_MALLOC),
7193 1, size_in_bytes);
7194 if (GFC_CLASS_TYPE_P (TREE_TYPE (comp)))
7195 ptr = gfc_class_data_get (comp);
7196 else
7197 ptr = comp;
7198 tmp = fold_convert (TREE_TYPE (ptr), tmp);
7199 gfc_add_modify (block, ptr, tmp);
7202 if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
7203 /* Update the lhs character length. */
7204 gfc_add_modify (block, lhs_cl_size, size);
7208 /* Assign a single component of a derived type constructor. */
7210 static tree
7211 gfc_trans_subcomponent_assign (tree dest, gfc_component * cm, gfc_expr * expr,
7212 gfc_symbol *sym, bool init)
7214 gfc_se se;
7215 gfc_se lse;
7216 stmtblock_t block;
7217 tree tmp;
7218 tree vtab;
7220 gfc_start_block (&block);
7222 if (cm->attr.pointer || cm->attr.proc_pointer)
7224 /* Only care about pointers here, not about allocatables. */
7225 gfc_init_se (&se, NULL);
7226 /* Pointer component. */
7227 if ((cm->attr.dimension || cm->attr.codimension)
7228 && !cm->attr.proc_pointer)
7230 /* Array pointer. */
7231 if (expr->expr_type == EXPR_NULL)
7232 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
7233 else
7235 se.direct_byref = 1;
7236 se.expr = dest;
7237 gfc_conv_expr_descriptor (&se, expr);
7238 gfc_add_block_to_block (&block, &se.pre);
7239 gfc_add_block_to_block (&block, &se.post);
7242 else
7244 /* Scalar pointers. */
7245 se.want_pointer = 1;
7246 gfc_conv_expr (&se, expr);
7247 gfc_add_block_to_block (&block, &se.pre);
7249 if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
7250 && expr->symtree->n.sym->attr.dummy)
7251 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
7253 gfc_add_modify (&block, dest,
7254 fold_convert (TREE_TYPE (dest), se.expr));
7255 gfc_add_block_to_block (&block, &se.post);
7258 else if (cm->ts.type == BT_CLASS && expr->expr_type == EXPR_NULL)
7260 /* NULL initialization for CLASS components. */
7261 tmp = gfc_trans_structure_assign (dest,
7262 gfc_class_initializer (&cm->ts, expr),
7263 false);
7264 gfc_add_expr_to_block (&block, tmp);
7266 else if ((cm->attr.dimension || cm->attr.codimension)
7267 && !cm->attr.proc_pointer)
7269 if (cm->attr.allocatable && expr->expr_type == EXPR_NULL)
7270 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
7271 else if (cm->attr.allocatable)
7273 tmp = gfc_trans_alloc_subarray_assign (dest, cm, expr);
7274 gfc_add_expr_to_block (&block, tmp);
7276 else
7278 tmp = gfc_trans_subarray_assign (dest, cm, expr);
7279 gfc_add_expr_to_block (&block, tmp);
7282 else if (cm->ts.type == BT_CLASS
7283 && CLASS_DATA (cm)->attr.dimension
7284 && CLASS_DATA (cm)->attr.allocatable
7285 && expr->ts.type == BT_DERIVED)
7287 vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
7288 vtab = gfc_build_addr_expr (NULL_TREE, vtab);
7289 tmp = gfc_class_vptr_get (dest);
7290 gfc_add_modify (&block, tmp,
7291 fold_convert (TREE_TYPE (tmp), vtab));
7292 tmp = gfc_class_data_get (dest);
7293 tmp = gfc_trans_alloc_subarray_assign (tmp, cm, expr);
7294 gfc_add_expr_to_block (&block, tmp);
7296 else if (init && cm->attr.allocatable && expr->expr_type == EXPR_NULL)
7298 /* NULL initialization for allocatable components. */
7299 gfc_add_modify (&block, dest, fold_convert (TREE_TYPE (dest),
7300 null_pointer_node));
7302 else if (init && (cm->attr.allocatable
7303 || (cm->ts.type == BT_CLASS && CLASS_DATA (cm)->attr.allocatable
7304 && expr->ts.type != BT_CLASS)))
7306 /* Take care about non-array allocatable components here. The alloc_*
7307 routine below is motivated by the alloc_scalar_allocatable_for_
7308 assignment() routine, but with the realloc portions removed and
7309 different input. */
7310 alloc_scalar_allocatable_for_subcomponent_assignment (&block,
7311 dest,
7313 expr,
7314 sym);
7315 /* The remainder of these instructions follow the if (cm->attr.pointer)
7316 if (!cm->attr.dimension) part above. */
7317 gfc_init_se (&se, NULL);
7318 gfc_conv_expr (&se, expr);
7319 gfc_add_block_to_block (&block, &se.pre);
7321 if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
7322 && expr->symtree->n.sym->attr.dummy)
7323 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
7325 if (cm->ts.type == BT_CLASS && expr->ts.type == BT_DERIVED)
7327 tmp = gfc_class_data_get (dest);
7328 tmp = build_fold_indirect_ref_loc (input_location, tmp);
7329 vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
7330 vtab = gfc_build_addr_expr (NULL_TREE, vtab);
7331 gfc_add_modify (&block, gfc_class_vptr_get (dest),
7332 fold_convert (TREE_TYPE (gfc_class_vptr_get (dest)), vtab));
7334 else
7335 tmp = build_fold_indirect_ref_loc (input_location, dest);
7337 /* For deferred strings insert a memcpy. */
7338 if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
7340 tree size;
7341 gcc_assert (se.string_length || expr->ts.u.cl->backend_decl);
7342 size = size_of_string_in_bytes (cm->ts.kind, se.string_length
7343 ? se.string_length
7344 : expr->ts.u.cl->backend_decl);
7345 tmp = gfc_build_memcpy_call (tmp, se.expr, size);
7346 gfc_add_expr_to_block (&block, tmp);
7348 else
7349 gfc_add_modify (&block, tmp,
7350 fold_convert (TREE_TYPE (tmp), se.expr));
7351 gfc_add_block_to_block (&block, &se.post);
7353 else if (expr->ts.type == BT_UNION)
7355 tree tmp;
7356 gfc_constructor *c = gfc_constructor_first (expr->value.constructor);
7357 /* We mark that the entire union should be initialized with a contrived
7358 EXPR_NULL expression at the beginning. */
7359 if (c != NULL && c->n.component == NULL
7360 && c->expr != NULL && c->expr->expr_type == EXPR_NULL)
7362 tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
7363 dest, build_constructor (TREE_TYPE (dest), NULL));
7364 gfc_add_expr_to_block (&block, tmp);
7365 c = gfc_constructor_next (c);
7367 /* The following constructor expression, if any, represents a specific
7368 map intializer, as given by the user. */
7369 if (c != NULL && c->expr != NULL)
7371 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
7372 tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
7373 gfc_add_expr_to_block (&block, tmp);
7376 else if (expr->ts.type == BT_DERIVED && expr->ts.f90_type != BT_VOID)
7378 if (expr->expr_type != EXPR_STRUCTURE)
7380 tree dealloc = NULL_TREE;
7381 gfc_init_se (&se, NULL);
7382 gfc_conv_expr (&se, expr);
7383 gfc_add_block_to_block (&block, &se.pre);
7384 /* Prevent repeat evaluations in gfc_copy_alloc_comp by fixing the
7385 expression in a temporary variable and deallocate the allocatable
7386 components. Then we can the copy the expression to the result. */
7387 if (cm->ts.u.derived->attr.alloc_comp
7388 && expr->expr_type != EXPR_VARIABLE)
7390 se.expr = gfc_evaluate_now (se.expr, &block);
7391 dealloc = gfc_deallocate_alloc_comp (cm->ts.u.derived, se.expr,
7392 expr->rank);
7394 gfc_add_modify (&block, dest,
7395 fold_convert (TREE_TYPE (dest), se.expr));
7396 if (cm->ts.u.derived->attr.alloc_comp
7397 && expr->expr_type != EXPR_NULL)
7399 // TODO: Fix caf_mode
7400 tmp = gfc_copy_alloc_comp (cm->ts.u.derived, se.expr,
7401 dest, expr->rank, 0);
7402 gfc_add_expr_to_block (&block, tmp);
7403 if (dealloc != NULL_TREE)
7404 gfc_add_expr_to_block (&block, dealloc);
7406 gfc_add_block_to_block (&block, &se.post);
7408 else
7410 /* Nested constructors. */
7411 tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
7412 gfc_add_expr_to_block (&block, tmp);
7415 else if (gfc_deferred_strlen (cm, &tmp))
7417 tree strlen;
7418 strlen = tmp;
7419 gcc_assert (strlen);
7420 strlen = fold_build3_loc (input_location, COMPONENT_REF,
7421 TREE_TYPE (strlen),
7422 TREE_OPERAND (dest, 0),
7423 strlen, NULL_TREE);
7425 if (expr->expr_type == EXPR_NULL)
7427 tmp = build_int_cst (TREE_TYPE (cm->backend_decl), 0);
7428 gfc_add_modify (&block, dest, tmp);
7429 tmp = build_int_cst (TREE_TYPE (strlen), 0);
7430 gfc_add_modify (&block, strlen, tmp);
7432 else
7434 tree size;
7435 gfc_init_se (&se, NULL);
7436 gfc_conv_expr (&se, expr);
7437 size = size_of_string_in_bytes (cm->ts.kind, se.string_length);
7438 tmp = build_call_expr_loc (input_location,
7439 builtin_decl_explicit (BUILT_IN_MALLOC),
7440 1, size);
7441 gfc_add_modify (&block, dest,
7442 fold_convert (TREE_TYPE (dest), tmp));
7443 gfc_add_modify (&block, strlen, se.string_length);
7444 tmp = gfc_build_memcpy_call (dest, se.expr, size);
7445 gfc_add_expr_to_block (&block, tmp);
7448 else if (!cm->attr.artificial)
7450 /* Scalar component (excluding deferred parameters). */
7451 gfc_init_se (&se, NULL);
7452 gfc_init_se (&lse, NULL);
7454 gfc_conv_expr (&se, expr);
7455 if (cm->ts.type == BT_CHARACTER)
7456 lse.string_length = cm->ts.u.cl->backend_decl;
7457 lse.expr = dest;
7458 tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, false, false);
7459 gfc_add_expr_to_block (&block, tmp);
7461 return gfc_finish_block (&block);
7464 /* Assign a derived type constructor to a variable. */
7466 tree
7467 gfc_trans_structure_assign (tree dest, gfc_expr * expr, bool init, bool coarray)
7469 gfc_constructor *c;
7470 gfc_component *cm;
7471 stmtblock_t block;
7472 tree field;
7473 tree tmp;
7474 gfc_se se;
7476 gfc_start_block (&block);
7477 cm = expr->ts.u.derived->components;
7479 if (expr->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
7480 && (expr->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
7481 || expr->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
7483 gfc_se lse;
7485 gfc_init_se (&se, NULL);
7486 gfc_init_se (&lse, NULL);
7487 gfc_conv_expr (&se, gfc_constructor_first (expr->value.constructor)->expr);
7488 lse.expr = dest;
7489 gfc_add_modify (&block, lse.expr,
7490 fold_convert (TREE_TYPE (lse.expr), se.expr));
7492 return gfc_finish_block (&block);
7495 if (coarray)
7496 gfc_init_se (&se, NULL);
7498 for (c = gfc_constructor_first (expr->value.constructor);
7499 c; c = gfc_constructor_next (c), cm = cm->next)
7501 /* Skip absent members in default initializers. */
7502 if (!c->expr && !cm->attr.allocatable)
7503 continue;
7505 /* Register the component with the caf-lib before it is initialized.
7506 Register only allocatable components, that are not coarray'ed
7507 components (%comp[*]). Only register when the constructor is not the
7508 null-expression. */
7509 if (coarray && !cm->attr.codimension && cm->attr.allocatable
7510 && (!c->expr || c->expr->expr_type == EXPR_NULL))
7512 tree token, desc, size;
7513 symbol_attribute attr;
7514 bool is_array = cm->ts.type == BT_CLASS
7515 ? CLASS_DATA (cm)->attr.dimension : cm->attr.dimension;
7517 field = cm->backend_decl;
7518 field = fold_build3_loc (input_location, COMPONENT_REF,
7519 TREE_TYPE (field), dest, field, NULL_TREE);
7520 if (cm->ts.type == BT_CLASS)
7521 field = gfc_class_data_get (field);
7523 token = is_array ? gfc_conv_descriptor_token (field)
7524 : fold_build3_loc (input_location, COMPONENT_REF,
7525 TREE_TYPE (cm->caf_token), dest,
7526 cm->caf_token, NULL_TREE);
7528 if (is_array)
7530 /* The _caf_register routine looks at the rank of the array
7531 descriptor to decide whether the data registered is an array
7532 or not. */
7533 int rank = cm->ts.type == BT_CLASS ? CLASS_DATA (cm)->as->rank
7534 : cm->as->rank;
7535 /* When the rank is not known just set a positive rank, which
7536 suffices to recognize the data as array. */
7537 if (rank < 0)
7538 rank = 1;
7539 size = integer_zero_node;
7540 desc = field;
7541 gfc_add_modify (&block, gfc_conv_descriptor_dtype (desc),
7542 build_int_cst (gfc_array_index_type, rank));
7544 else
7546 desc = gfc_conv_scalar_to_descriptor (&se, field, attr);
7547 size = TYPE_SIZE_UNIT (TREE_TYPE (field));
7549 gfc_add_block_to_block (&block, &se.pre);
7550 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_register,
7551 7, size, build_int_cst (
7552 integer_type_node,
7553 GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY),
7554 gfc_build_addr_expr (pvoid_type_node,
7555 token),
7556 gfc_build_addr_expr (NULL_TREE, desc),
7557 null_pointer_node, null_pointer_node,
7558 integer_zero_node);
7559 gfc_add_expr_to_block (&block, tmp);
7561 field = cm->backend_decl;
7562 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
7563 dest, field, NULL_TREE);
7564 if (!c->expr)
7566 gfc_expr *e = gfc_get_null_expr (NULL);
7567 tmp = gfc_trans_subcomponent_assign (tmp, cm, e, expr->ts.u.derived,
7568 init);
7569 gfc_free_expr (e);
7571 else
7572 tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr,
7573 expr->ts.u.derived, init);
7574 gfc_add_expr_to_block (&block, tmp);
7576 return gfc_finish_block (&block);
7579 void
7580 gfc_conv_union_initializer (vec<constructor_elt, va_gc> *v,
7581 gfc_component *un, gfc_expr *init)
7583 gfc_constructor *ctor;
7585 if (un->ts.type != BT_UNION || un == NULL || init == NULL)
7586 return;
7588 ctor = gfc_constructor_first (init->value.constructor);
7590 if (ctor == NULL || ctor->expr == NULL)
7591 return;
7593 gcc_assert (init->expr_type == EXPR_STRUCTURE);
7595 /* If we have an 'initialize all' constructor, do it first. */
7596 if (ctor->expr->expr_type == EXPR_NULL)
7598 tree union_type = TREE_TYPE (un->backend_decl);
7599 tree val = build_constructor (union_type, NULL);
7600 CONSTRUCTOR_APPEND_ELT (v, un->backend_decl, val);
7601 ctor = gfc_constructor_next (ctor);
7604 /* Add the map initializer on top. */
7605 if (ctor != NULL && ctor->expr != NULL)
7607 gcc_assert (ctor->expr->expr_type == EXPR_STRUCTURE);
7608 tree val = gfc_conv_initializer (ctor->expr, &un->ts,
7609 TREE_TYPE (un->backend_decl),
7610 un->attr.dimension, un->attr.pointer,
7611 un->attr.proc_pointer);
7612 CONSTRUCTOR_APPEND_ELT (v, un->backend_decl, val);
7616 /* Build an expression for a constructor. If init is nonzero then
7617 this is part of a static variable initializer. */
7619 void
7620 gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
7622 gfc_constructor *c;
7623 gfc_component *cm;
7624 tree val;
7625 tree type;
7626 tree tmp;
7627 vec<constructor_elt, va_gc> *v = NULL;
7629 gcc_assert (se->ss == NULL);
7630 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
7631 type = gfc_typenode_for_spec (&expr->ts);
7633 if (!init)
7635 /* Create a temporary variable and fill it in. */
7636 se->expr = gfc_create_var (type, expr->ts.u.derived->name);
7637 /* The symtree in expr is NULL, if the code to generate is for
7638 initializing the static members only. */
7639 tmp = gfc_trans_structure_assign (se->expr, expr, expr->symtree != NULL,
7640 se->want_coarray);
7641 gfc_add_expr_to_block (&se->pre, tmp);
7642 return;
7645 cm = expr->ts.u.derived->components;
7647 for (c = gfc_constructor_first (expr->value.constructor);
7648 c; c = gfc_constructor_next (c), cm = cm->next)
7650 /* Skip absent members in default initializers and allocatable
7651 components. Although the latter have a default initializer
7652 of EXPR_NULL,... by default, the static nullify is not needed
7653 since this is done every time we come into scope. */
7654 if (!c->expr || (cm->attr.allocatable && cm->attr.flavor != FL_PROCEDURE))
7655 continue;
7657 if (cm->initializer && cm->initializer->expr_type != EXPR_NULL
7658 && strcmp (cm->name, "_extends") == 0
7659 && cm->initializer->symtree)
7661 tree vtab;
7662 gfc_symbol *vtabs;
7663 vtabs = cm->initializer->symtree->n.sym;
7664 vtab = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtabs));
7665 vtab = unshare_expr_without_location (vtab);
7666 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, vtab);
7668 else if (cm->ts.u.derived && strcmp (cm->name, "_size") == 0)
7670 val = TYPE_SIZE_UNIT (gfc_get_derived_type (cm->ts.u.derived));
7671 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
7672 fold_convert (TREE_TYPE (cm->backend_decl),
7673 val));
7675 else if (cm->ts.type == BT_INTEGER && strcmp (cm->name, "_len") == 0)
7676 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
7677 fold_convert (TREE_TYPE (cm->backend_decl),
7678 integer_zero_node));
7679 else if (cm->ts.type == BT_UNION)
7680 gfc_conv_union_initializer (v, cm, c->expr);
7681 else
7683 val = gfc_conv_initializer (c->expr, &cm->ts,
7684 TREE_TYPE (cm->backend_decl),
7685 cm->attr.dimension, cm->attr.pointer,
7686 cm->attr.proc_pointer);
7687 val = unshare_expr_without_location (val);
7689 /* Append it to the constructor list. */
7690 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
7694 se->expr = build_constructor (type, v);
7695 if (init)
7696 TREE_CONSTANT (se->expr) = 1;
7700 /* Translate a substring expression. */
7702 static void
7703 gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
7705 gfc_ref *ref;
7707 ref = expr->ref;
7709 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
7711 se->expr = gfc_build_wide_string_const (expr->ts.kind,
7712 expr->value.character.length,
7713 expr->value.character.string);
7715 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
7716 TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
7718 if (ref)
7719 gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
7723 /* Entry point for expression translation. Evaluates a scalar quantity.
7724 EXPR is the expression to be translated, and SE is the state structure if
7725 called from within the scalarized. */
7727 void
7728 gfc_conv_expr (gfc_se * se, gfc_expr * expr)
7730 gfc_ss *ss;
7732 ss = se->ss;
7733 if (ss && ss->info->expr == expr
7734 && (ss->info->type == GFC_SS_SCALAR
7735 || ss->info->type == GFC_SS_REFERENCE))
7737 gfc_ss_info *ss_info;
7739 ss_info = ss->info;
7740 /* Substitute a scalar expression evaluated outside the scalarization
7741 loop. */
7742 se->expr = ss_info->data.scalar.value;
7743 if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
7744 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
7746 se->string_length = ss_info->string_length;
7747 gfc_advance_se_ss_chain (se);
7748 return;
7751 /* We need to convert the expressions for the iso_c_binding derived types.
7752 C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
7753 null_pointer_node. C_PTR and C_FUNPTR are converted to match the
7754 typespec for the C_PTR and C_FUNPTR symbols, which has already been
7755 updated to be an integer with a kind equal to the size of a (void *). */
7756 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->ts.f90_type == BT_VOID
7757 && expr->ts.u.derived->attr.is_bind_c)
7759 if (expr->expr_type == EXPR_VARIABLE
7760 && (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
7761 || expr->symtree->n.sym->intmod_sym_id
7762 == ISOCBINDING_NULL_FUNPTR))
7764 /* Set expr_type to EXPR_NULL, which will result in
7765 null_pointer_node being used below. */
7766 expr->expr_type = EXPR_NULL;
7768 else
7770 /* Update the type/kind of the expression to be what the new
7771 type/kind are for the updated symbols of C_PTR/C_FUNPTR. */
7772 expr->ts.type = BT_INTEGER;
7773 expr->ts.f90_type = BT_VOID;
7774 expr->ts.kind = gfc_index_integer_kind;
7778 gfc_fix_class_refs (expr);
7780 switch (expr->expr_type)
7782 case EXPR_OP:
7783 gfc_conv_expr_op (se, expr);
7784 break;
7786 case EXPR_FUNCTION:
7787 gfc_conv_function_expr (se, expr);
7788 break;
7790 case EXPR_CONSTANT:
7791 gfc_conv_constant (se, expr);
7792 break;
7794 case EXPR_VARIABLE:
7795 gfc_conv_variable (se, expr);
7796 break;
7798 case EXPR_NULL:
7799 se->expr = null_pointer_node;
7800 break;
7802 case EXPR_SUBSTRING:
7803 gfc_conv_substring_expr (se, expr);
7804 break;
7806 case EXPR_STRUCTURE:
7807 gfc_conv_structure (se, expr, 0);
7808 break;
7810 case EXPR_ARRAY:
7811 gfc_conv_array_constructor_expr (se, expr);
7812 break;
7814 default:
7815 gcc_unreachable ();
7816 break;
7820 /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
7821 of an assignment. */
7822 void
7823 gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
7825 gfc_conv_expr (se, expr);
7826 /* All numeric lvalues should have empty post chains. If not we need to
7827 figure out a way of rewriting an lvalue so that it has no post chain. */
7828 gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
7831 /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
7832 numeric expressions. Used for scalar values where inserting cleanup code
7833 is inconvenient. */
7834 void
7835 gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
7837 tree val;
7839 gcc_assert (expr->ts.type != BT_CHARACTER);
7840 gfc_conv_expr (se, expr);
7841 if (se->post.head)
7843 val = gfc_create_var (TREE_TYPE (se->expr), NULL);
7844 gfc_add_modify (&se->pre, val, se->expr);
7845 se->expr = val;
7846 gfc_add_block_to_block (&se->pre, &se->post);
7850 /* Helper to translate an expression and convert it to a particular type. */
7851 void
7852 gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
7854 gfc_conv_expr_val (se, expr);
7855 se->expr = convert (type, se->expr);
7859 /* Converts an expression so that it can be passed by reference. Scalar
7860 values only. */
7862 void
7863 gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
7865 gfc_ss *ss;
7866 tree var;
7868 ss = se->ss;
7869 if (ss && ss->info->expr == expr
7870 && ss->info->type == GFC_SS_REFERENCE)
7872 /* Returns a reference to the scalar evaluated outside the loop
7873 for this case. */
7874 gfc_conv_expr (se, expr);
7876 if (expr->ts.type == BT_CHARACTER
7877 && expr->expr_type != EXPR_FUNCTION)
7878 gfc_conv_string_parameter (se);
7879 else
7880 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
7882 return;
7885 if (expr->ts.type == BT_CHARACTER)
7887 gfc_conv_expr (se, expr);
7888 gfc_conv_string_parameter (se);
7889 return;
7892 if (expr->expr_type == EXPR_VARIABLE)
7894 se->want_pointer = 1;
7895 gfc_conv_expr (se, expr);
7896 if (se->post.head)
7898 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
7899 gfc_add_modify (&se->pre, var, se->expr);
7900 gfc_add_block_to_block (&se->pre, &se->post);
7901 se->expr = var;
7903 return;
7906 if (expr->expr_type == EXPR_FUNCTION
7907 && ((expr->value.function.esym
7908 && expr->value.function.esym->result->attr.pointer
7909 && !expr->value.function.esym->result->attr.dimension)
7910 || (!expr->value.function.esym && !expr->ref
7911 && expr->symtree->n.sym->attr.pointer
7912 && !expr->symtree->n.sym->attr.dimension)))
7914 se->want_pointer = 1;
7915 gfc_conv_expr (se, expr);
7916 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
7917 gfc_add_modify (&se->pre, var, se->expr);
7918 se->expr = var;
7919 return;
7922 gfc_conv_expr (se, expr);
7924 /* Create a temporary var to hold the value. */
7925 if (TREE_CONSTANT (se->expr))
7927 tree tmp = se->expr;
7928 STRIP_TYPE_NOPS (tmp);
7929 var = build_decl (input_location,
7930 CONST_DECL, NULL, TREE_TYPE (tmp));
7931 DECL_INITIAL (var) = tmp;
7932 TREE_STATIC (var) = 1;
7933 pushdecl (var);
7935 else
7937 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
7938 gfc_add_modify (&se->pre, var, se->expr);
7940 gfc_add_block_to_block (&se->pre, &se->post);
7942 /* Take the address of that value. */
7943 se->expr = gfc_build_addr_expr (NULL_TREE, var);
7947 /* Get the _len component for an unlimited polymorphic expression. */
7949 static tree
7950 trans_get_upoly_len (stmtblock_t *block, gfc_expr *expr)
7952 gfc_se se;
7953 gfc_ref *ref = expr->ref;
7955 gfc_init_se (&se, NULL);
7956 while (ref && ref->next)
7957 ref = ref->next;
7958 gfc_add_len_component (expr);
7959 gfc_conv_expr (&se, expr);
7960 gfc_add_block_to_block (block, &se.pre);
7961 gcc_assert (se.post.head == NULL_TREE);
7962 if (ref)
7964 gfc_free_ref_list (ref->next);
7965 ref->next = NULL;
7967 else
7969 gfc_free_ref_list (expr->ref);
7970 expr->ref = NULL;
7972 return se.expr;
7976 /* Assign _vptr and _len components as appropriate. BLOCK should be a
7977 statement-list outside of the scalarizer-loop. When code is generated, that
7978 depends on the scalarized expression, it is added to RSE.PRE.
7979 Returns le's _vptr tree and when set the len expressions in to_lenp and
7980 from_lenp to form a le%_vptr%_copy (re, le, [from_lenp, to_lenp])
7981 expression. */
7983 static tree
7984 trans_class_vptr_len_assignment (stmtblock_t *block, gfc_expr * le,
7985 gfc_expr * re, gfc_se *rse,
7986 tree * to_lenp, tree * from_lenp)
7988 gfc_se se;
7989 gfc_expr * vptr_expr;
7990 tree tmp, to_len = NULL_TREE, from_len = NULL_TREE, lhs_vptr;
7991 bool set_vptr = false, temp_rhs = false;
7992 stmtblock_t *pre = block;
7994 /* Create a temporary for complicated expressions. */
7995 if (re->expr_type != EXPR_VARIABLE && re->expr_type != EXPR_NULL
7996 && rse->expr != NULL_TREE && !DECL_P (rse->expr))
7998 tmp = gfc_create_var (TREE_TYPE (rse->expr), "rhs");
7999 pre = &rse->pre;
8000 gfc_add_modify (&rse->pre, tmp, rse->expr);
8001 rse->expr = tmp;
8002 temp_rhs = true;
8005 /* Get the _vptr for the left-hand side expression. */
8006 gfc_init_se (&se, NULL);
8007 vptr_expr = gfc_find_and_cut_at_last_class_ref (le);
8008 if (vptr_expr != NULL && gfc_expr_attr (vptr_expr).class_ok)
8010 /* Care about _len for unlimited polymorphic entities. */
8011 if (UNLIMITED_POLY (vptr_expr)
8012 || (vptr_expr->ts.type == BT_DERIVED
8013 && vptr_expr->ts.u.derived->attr.unlimited_polymorphic))
8014 to_len = trans_get_upoly_len (block, vptr_expr);
8015 gfc_add_vptr_component (vptr_expr);
8016 set_vptr = true;
8018 else
8019 vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&le->ts));
8020 se.want_pointer = 1;
8021 gfc_conv_expr (&se, vptr_expr);
8022 gfc_free_expr (vptr_expr);
8023 gfc_add_block_to_block (block, &se.pre);
8024 gcc_assert (se.post.head == NULL_TREE);
8025 lhs_vptr = se.expr;
8026 STRIP_NOPS (lhs_vptr);
8028 /* Set the _vptr only when the left-hand side of the assignment is a
8029 class-object. */
8030 if (set_vptr)
8032 /* Get the vptr from the rhs expression only, when it is variable.
8033 Functions are expected to be assigned to a temporary beforehand. */
8034 vptr_expr = re->expr_type == EXPR_VARIABLE
8035 ? gfc_find_and_cut_at_last_class_ref (re)
8036 : NULL;
8037 if (vptr_expr != NULL && vptr_expr->ts.type == BT_CLASS)
8039 if (to_len != NULL_TREE)
8041 /* Get the _len information from the rhs. */
8042 if (UNLIMITED_POLY (vptr_expr)
8043 || (vptr_expr->ts.type == BT_DERIVED
8044 && vptr_expr->ts.u.derived->attr.unlimited_polymorphic))
8045 from_len = trans_get_upoly_len (block, vptr_expr);
8047 gfc_add_vptr_component (vptr_expr);
8049 else
8051 if (re->expr_type == EXPR_VARIABLE
8052 && DECL_P (re->symtree->n.sym->backend_decl)
8053 && DECL_LANG_SPECIFIC (re->symtree->n.sym->backend_decl)
8054 && GFC_DECL_SAVED_DESCRIPTOR (re->symtree->n.sym->backend_decl)
8055 && GFC_CLASS_TYPE_P (TREE_TYPE (GFC_DECL_SAVED_DESCRIPTOR (
8056 re->symtree->n.sym->backend_decl))))
8058 vptr_expr = NULL;
8059 se.expr = gfc_class_vptr_get (GFC_DECL_SAVED_DESCRIPTOR (
8060 re->symtree->n.sym->backend_decl));
8061 if (to_len)
8062 from_len = gfc_class_len_get (GFC_DECL_SAVED_DESCRIPTOR (
8063 re->symtree->n.sym->backend_decl));
8065 else if (temp_rhs && re->ts.type == BT_CLASS)
8067 vptr_expr = NULL;
8068 se.expr = gfc_class_vptr_get (rse->expr);
8070 else if (re->expr_type != EXPR_NULL)
8071 /* Only when rhs is non-NULL use its declared type for vptr
8072 initialisation. */
8073 vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&re->ts));
8074 else
8075 /* When the rhs is NULL use the vtab of lhs' declared type. */
8076 vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&le->ts));
8079 if (vptr_expr)
8081 gfc_init_se (&se, NULL);
8082 se.want_pointer = 1;
8083 gfc_conv_expr (&se, vptr_expr);
8084 gfc_free_expr (vptr_expr);
8085 gfc_add_block_to_block (block, &se.pre);
8086 gcc_assert (se.post.head == NULL_TREE);
8088 gfc_add_modify (pre, lhs_vptr, fold_convert (TREE_TYPE (lhs_vptr),
8089 se.expr));
8091 if (to_len != NULL_TREE)
8093 /* The _len component needs to be set. Figure how to get the
8094 value of the right-hand side. */
8095 if (from_len == NULL_TREE)
8097 if (rse->string_length != NULL_TREE)
8098 from_len = rse->string_length;
8099 else if (re->ts.type == BT_CHARACTER && re->ts.u.cl->length)
8101 from_len = gfc_get_expr_charlen (re);
8102 gfc_init_se (&se, NULL);
8103 gfc_conv_expr (&se, re->ts.u.cl->length);
8104 gfc_add_block_to_block (block, &se.pre);
8105 gcc_assert (se.post.head == NULL_TREE);
8106 from_len = gfc_evaluate_now (se.expr, block);
8108 else
8109 from_len = integer_zero_node;
8111 gfc_add_modify (pre, to_len, fold_convert (TREE_TYPE (to_len),
8112 from_len));
8116 /* Return the _len trees only, when requested. */
8117 if (to_lenp)
8118 *to_lenp = to_len;
8119 if (from_lenp)
8120 *from_lenp = from_len;
8121 return lhs_vptr;
8124 /* Indentify class valued proc_pointer assignments. */
8126 static bool
8127 pointer_assignment_is_proc_pointer (gfc_expr * expr1, gfc_expr * expr2)
8129 gfc_ref * ref;
8131 ref = expr1->ref;
8132 while (ref && ref->next)
8133 ref = ref->next;
8135 return ref && ref->type == REF_COMPONENT
8136 && ref->u.c.component->attr.proc_pointer
8137 && expr2->expr_type == EXPR_VARIABLE
8138 && expr2->symtree->n.sym->attr.flavor == FL_PROCEDURE;
8142 tree
8143 gfc_trans_pointer_assign (gfc_code * code)
8145 return gfc_trans_pointer_assignment (code->expr1, code->expr2);
8149 /* Generate code for a pointer assignment. */
8151 tree
8152 gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
8154 gfc_se lse;
8155 gfc_se rse;
8156 stmtblock_t block;
8157 tree desc;
8158 tree tmp;
8159 tree decl;
8160 bool scalar, non_proc_pointer_assign;
8161 gfc_ss *ss;
8163 gfc_start_block (&block);
8165 gfc_init_se (&lse, NULL);
8167 /* Usually testing whether this is not a proc pointer assignment. */
8168 non_proc_pointer_assign = !pointer_assignment_is_proc_pointer (expr1, expr2);
8170 /* Check whether the expression is a scalar or not; we cannot use
8171 expr1->rank as it can be nonzero for proc pointers. */
8172 ss = gfc_walk_expr (expr1);
8173 scalar = ss == gfc_ss_terminator;
8174 if (!scalar)
8175 gfc_free_ss_chain (ss);
8177 if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS
8178 && expr2->expr_type != EXPR_FUNCTION && non_proc_pointer_assign)
8180 gfc_add_data_component (expr2);
8181 /* The following is required as gfc_add_data_component doesn't
8182 update ts.type if there is a tailing REF_ARRAY. */
8183 expr2->ts.type = BT_DERIVED;
8186 if (scalar)
8188 /* Scalar pointers. */
8189 lse.want_pointer = 1;
8190 gfc_conv_expr (&lse, expr1);
8191 gfc_init_se (&rse, NULL);
8192 rse.want_pointer = 1;
8193 gfc_conv_expr (&rse, expr2);
8195 if (non_proc_pointer_assign && expr1->ts.type == BT_CLASS)
8197 trans_class_vptr_len_assignment (&block, expr1, expr2, &rse, NULL,
8198 NULL);
8199 lse.expr = gfc_class_data_get (lse.expr);
8202 if (expr1->symtree->n.sym->attr.proc_pointer
8203 && expr1->symtree->n.sym->attr.dummy)
8204 lse.expr = build_fold_indirect_ref_loc (input_location,
8205 lse.expr);
8207 if (expr2->symtree && expr2->symtree->n.sym->attr.proc_pointer
8208 && expr2->symtree->n.sym->attr.dummy)
8209 rse.expr = build_fold_indirect_ref_loc (input_location,
8210 rse.expr);
8212 gfc_add_block_to_block (&block, &lse.pre);
8213 gfc_add_block_to_block (&block, &rse.pre);
8215 /* Check character lengths if character expression. The test is only
8216 really added if -fbounds-check is enabled. Exclude deferred
8217 character length lefthand sides. */
8218 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL
8219 && !expr1->ts.deferred
8220 && !expr1->symtree->n.sym->attr.proc_pointer
8221 && !gfc_is_proc_ptr_comp (expr1))
8223 gcc_assert (expr2->ts.type == BT_CHARACTER);
8224 gcc_assert (lse.string_length && rse.string_length);
8225 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
8226 lse.string_length, rse.string_length,
8227 &block);
8230 /* The assignment to an deferred character length sets the string
8231 length to that of the rhs. */
8232 if (expr1->ts.deferred)
8234 if (expr2->expr_type != EXPR_NULL && lse.string_length != NULL)
8235 gfc_add_modify (&block, lse.string_length, rse.string_length);
8236 else if (lse.string_length != NULL)
8237 gfc_add_modify (&block, lse.string_length,
8238 build_int_cst (gfc_charlen_type_node, 0));
8241 gfc_add_modify (&block, lse.expr,
8242 fold_convert (TREE_TYPE (lse.expr), rse.expr));
8244 gfc_add_block_to_block (&block, &rse.post);
8245 gfc_add_block_to_block (&block, &lse.post);
8247 else
8249 gfc_ref* remap;
8250 bool rank_remap;
8251 tree expr1_vptr = NULL_TREE;
8252 tree strlen_lhs;
8253 tree strlen_rhs = NULL_TREE;
8255 /* Array pointer. Find the last reference on the LHS and if it is an
8256 array section ref, we're dealing with bounds remapping. In this case,
8257 set it to AR_FULL so that gfc_conv_expr_descriptor does
8258 not see it and process the bounds remapping afterwards explicitly. */
8259 for (remap = expr1->ref; remap; remap = remap->next)
8260 if (!remap->next && remap->type == REF_ARRAY
8261 && remap->u.ar.type == AR_SECTION)
8262 break;
8263 rank_remap = (remap && remap->u.ar.end[0]);
8265 gfc_init_se (&lse, NULL);
8266 if (remap)
8267 lse.descriptor_only = 1;
8268 gfc_conv_expr_descriptor (&lse, expr1);
8269 strlen_lhs = lse.string_length;
8270 desc = lse.expr;
8272 if (expr2->expr_type == EXPR_NULL)
8274 /* Just set the data pointer to null. */
8275 gfc_conv_descriptor_data_set (&lse.pre, lse.expr, null_pointer_node);
8277 else if (rank_remap)
8279 /* If we are rank-remapping, just get the RHS's descriptor and
8280 process this later on. */
8281 gfc_init_se (&rse, NULL);
8282 rse.direct_byref = 1;
8283 rse.byref_noassign = 1;
8285 if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
8287 gfc_conv_function_expr (&rse, expr2);
8289 if (expr1->ts.type != BT_CLASS)
8290 rse.expr = gfc_class_data_get (rse.expr);
8291 else
8293 expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
8294 expr2, &rse,
8295 NULL, NULL);
8296 gfc_add_block_to_block (&block, &rse.pre);
8297 tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
8298 gfc_add_modify (&lse.pre, tmp, rse.expr);
8300 gfc_add_modify (&lse.pre, expr1_vptr,
8301 fold_convert (TREE_TYPE (expr1_vptr),
8302 gfc_class_vptr_get (tmp)));
8303 rse.expr = gfc_class_data_get (tmp);
8306 else if (expr2->expr_type == EXPR_FUNCTION)
8308 tree bound[GFC_MAX_DIMENSIONS];
8309 int i;
8311 for (i = 0; i < expr2->rank; i++)
8312 bound[i] = NULL_TREE;
8313 tmp = gfc_typenode_for_spec (&expr2->ts);
8314 tmp = gfc_get_array_type_bounds (tmp, expr2->rank, 0,
8315 bound, bound, 0,
8316 GFC_ARRAY_POINTER_CONT, false);
8317 tmp = gfc_create_var (tmp, "ptrtemp");
8318 rse.descriptor_only = 0;
8319 rse.expr = tmp;
8320 rse.direct_byref = 1;
8321 gfc_conv_expr_descriptor (&rse, expr2);
8322 strlen_rhs = rse.string_length;
8323 rse.expr = tmp;
8325 else
8327 gfc_conv_expr_descriptor (&rse, expr2);
8328 strlen_rhs = rse.string_length;
8329 if (expr1->ts.type == BT_CLASS)
8330 expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
8331 expr2, &rse,
8332 NULL, NULL);
8335 else if (expr2->expr_type == EXPR_VARIABLE)
8337 /* Assign directly to the LHS's descriptor. */
8338 lse.descriptor_only = 0;
8339 lse.direct_byref = 1;
8340 gfc_conv_expr_descriptor (&lse, expr2);
8341 strlen_rhs = lse.string_length;
8343 /* If this is a subreference array pointer assignment, use the rhs
8344 descriptor element size for the lhs span. */
8345 if (expr1->symtree->n.sym->attr.subref_array_pointer)
8347 decl = expr1->symtree->n.sym->backend_decl;
8348 gfc_init_se (&rse, NULL);
8349 rse.descriptor_only = 1;
8350 gfc_conv_expr (&rse, expr2);
8351 if (expr1->ts.type == BT_CLASS)
8352 trans_class_vptr_len_assignment (&block, expr1, expr2, &rse,
8353 NULL, NULL);
8354 tmp = gfc_get_element_type (TREE_TYPE (rse.expr));
8355 tmp = fold_convert (gfc_array_index_type, size_in_bytes (tmp));
8356 if (!INTEGER_CST_P (tmp))
8357 gfc_add_block_to_block (&lse.post, &rse.pre);
8358 gfc_add_modify (&lse.post, GFC_DECL_SPAN(decl), tmp);
8360 else if (expr1->ts.type == BT_CLASS)
8362 rse.expr = NULL_TREE;
8363 rse.string_length = NULL_TREE;
8364 trans_class_vptr_len_assignment (&block, expr1, expr2, &rse,
8365 NULL, NULL);
8368 else if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
8370 gfc_init_se (&rse, NULL);
8371 rse.want_pointer = 1;
8372 gfc_conv_function_expr (&rse, expr2);
8373 if (expr1->ts.type != BT_CLASS)
8375 rse.expr = gfc_class_data_get (rse.expr);
8376 gfc_add_modify (&lse.pre, desc, rse.expr);
8378 else
8380 expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
8381 expr2, &rse, NULL,
8382 NULL);
8383 gfc_add_block_to_block (&block, &rse.pre);
8384 tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
8385 gfc_add_modify (&lse.pre, tmp, rse.expr);
8387 gfc_add_modify (&lse.pre, expr1_vptr,
8388 fold_convert (TREE_TYPE (expr1_vptr),
8389 gfc_class_vptr_get (tmp)));
8390 rse.expr = gfc_class_data_get (tmp);
8391 gfc_add_modify (&lse.pre, desc, rse.expr);
8394 else
8396 /* Assign to a temporary descriptor and then copy that
8397 temporary to the pointer. */
8398 tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
8399 lse.descriptor_only = 0;
8400 lse.expr = tmp;
8401 lse.direct_byref = 1;
8402 gfc_conv_expr_descriptor (&lse, expr2);
8403 strlen_rhs = lse.string_length;
8404 gfc_add_modify (&lse.pre, desc, tmp);
8407 gfc_add_block_to_block (&block, &lse.pre);
8408 if (rank_remap)
8409 gfc_add_block_to_block (&block, &rse.pre);
8411 /* If we do bounds remapping, update LHS descriptor accordingly. */
8412 if (remap)
8414 int dim;
8415 gcc_assert (remap->u.ar.dimen == expr1->rank);
8417 if (rank_remap)
8419 /* Do rank remapping. We already have the RHS's descriptor
8420 converted in rse and now have to build the correct LHS
8421 descriptor for it. */
8423 tree dtype, data;
8424 tree offs, stride;
8425 tree lbound, ubound;
8427 /* Set dtype. */
8428 dtype = gfc_conv_descriptor_dtype (desc);
8429 tmp = gfc_get_dtype (TREE_TYPE (desc));
8430 gfc_add_modify (&block, dtype, tmp);
8432 /* Copy data pointer. */
8433 data = gfc_conv_descriptor_data_get (rse.expr);
8434 gfc_conv_descriptor_data_set (&block, desc, data);
8436 /* Copy offset but adjust it such that it would correspond
8437 to a lbound of zero. */
8438 offs = gfc_conv_descriptor_offset_get (rse.expr);
8439 for (dim = 0; dim < expr2->rank; ++dim)
8441 stride = gfc_conv_descriptor_stride_get (rse.expr,
8442 gfc_rank_cst[dim]);
8443 lbound = gfc_conv_descriptor_lbound_get (rse.expr,
8444 gfc_rank_cst[dim]);
8445 tmp = fold_build2_loc (input_location, MULT_EXPR,
8446 gfc_array_index_type, stride, lbound);
8447 offs = fold_build2_loc (input_location, PLUS_EXPR,
8448 gfc_array_index_type, offs, tmp);
8450 gfc_conv_descriptor_offset_set (&block, desc, offs);
8452 /* Set the bounds as declared for the LHS and calculate strides as
8453 well as another offset update accordingly. */
8454 stride = gfc_conv_descriptor_stride_get (rse.expr,
8455 gfc_rank_cst[0]);
8456 for (dim = 0; dim < expr1->rank; ++dim)
8458 gfc_se lower_se;
8459 gfc_se upper_se;
8461 gcc_assert (remap->u.ar.start[dim] && remap->u.ar.end[dim]);
8463 /* Convert declared bounds. */
8464 gfc_init_se (&lower_se, NULL);
8465 gfc_init_se (&upper_se, NULL);
8466 gfc_conv_expr (&lower_se, remap->u.ar.start[dim]);
8467 gfc_conv_expr (&upper_se, remap->u.ar.end[dim]);
8469 gfc_add_block_to_block (&block, &lower_se.pre);
8470 gfc_add_block_to_block (&block, &upper_se.pre);
8472 lbound = fold_convert (gfc_array_index_type, lower_se.expr);
8473 ubound = fold_convert (gfc_array_index_type, upper_se.expr);
8475 lbound = gfc_evaluate_now (lbound, &block);
8476 ubound = gfc_evaluate_now (ubound, &block);
8478 gfc_add_block_to_block (&block, &lower_se.post);
8479 gfc_add_block_to_block (&block, &upper_se.post);
8481 /* Set bounds in descriptor. */
8482 gfc_conv_descriptor_lbound_set (&block, desc,
8483 gfc_rank_cst[dim], lbound);
8484 gfc_conv_descriptor_ubound_set (&block, desc,
8485 gfc_rank_cst[dim], ubound);
8487 /* Set stride. */
8488 stride = gfc_evaluate_now (stride, &block);
8489 gfc_conv_descriptor_stride_set (&block, desc,
8490 gfc_rank_cst[dim], stride);
8492 /* Update offset. */
8493 offs = gfc_conv_descriptor_offset_get (desc);
8494 tmp = fold_build2_loc (input_location, MULT_EXPR,
8495 gfc_array_index_type, lbound, stride);
8496 offs = fold_build2_loc (input_location, MINUS_EXPR,
8497 gfc_array_index_type, offs, tmp);
8498 offs = gfc_evaluate_now (offs, &block);
8499 gfc_conv_descriptor_offset_set (&block, desc, offs);
8501 /* Update stride. */
8502 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
8503 stride = fold_build2_loc (input_location, MULT_EXPR,
8504 gfc_array_index_type, stride, tmp);
8507 else
8509 /* Bounds remapping. Just shift the lower bounds. */
8511 gcc_assert (expr1->rank == expr2->rank);
8513 for (dim = 0; dim < remap->u.ar.dimen; ++dim)
8515 gfc_se lbound_se;
8517 gcc_assert (remap->u.ar.start[dim]);
8518 gcc_assert (!remap->u.ar.end[dim]);
8519 gfc_init_se (&lbound_se, NULL);
8520 gfc_conv_expr (&lbound_se, remap->u.ar.start[dim]);
8522 gfc_add_block_to_block (&block, &lbound_se.pre);
8523 gfc_conv_shift_descriptor_lbound (&block, desc,
8524 dim, lbound_se.expr);
8525 gfc_add_block_to_block (&block, &lbound_se.post);
8530 /* Check string lengths if applicable. The check is only really added
8531 to the output code if -fbounds-check is enabled. */
8532 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
8534 gcc_assert (expr2->ts.type == BT_CHARACTER);
8535 gcc_assert (strlen_lhs && strlen_rhs);
8536 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
8537 strlen_lhs, strlen_rhs, &block);
8540 /* If rank remapping was done, check with -fcheck=bounds that
8541 the target is at least as large as the pointer. */
8542 if (rank_remap && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
8544 tree lsize, rsize;
8545 tree fault;
8546 const char* msg;
8548 lsize = gfc_conv_descriptor_size (lse.expr, expr1->rank);
8549 rsize = gfc_conv_descriptor_size (rse.expr, expr2->rank);
8551 lsize = gfc_evaluate_now (lsize, &block);
8552 rsize = gfc_evaluate_now (rsize, &block);
8553 fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
8554 rsize, lsize);
8556 msg = _("Target of rank remapping is too small (%ld < %ld)");
8557 gfc_trans_runtime_check (true, false, fault, &block, &expr2->where,
8558 msg, rsize, lsize);
8561 gfc_add_block_to_block (&block, &lse.post);
8562 if (rank_remap)
8563 gfc_add_block_to_block (&block, &rse.post);
8566 return gfc_finish_block (&block);
8570 /* Makes sure se is suitable for passing as a function string parameter. */
8571 /* TODO: Need to check all callers of this function. It may be abused. */
8573 void
8574 gfc_conv_string_parameter (gfc_se * se)
8576 tree type;
8578 if (TREE_CODE (se->expr) == STRING_CST)
8580 type = TREE_TYPE (TREE_TYPE (se->expr));
8581 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
8582 return;
8585 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
8587 if (TREE_CODE (se->expr) != INDIRECT_REF)
8589 type = TREE_TYPE (se->expr);
8590 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
8592 else
8594 type = gfc_get_character_type_len (gfc_default_character_kind,
8595 se->string_length);
8596 type = build_pointer_type (type);
8597 se->expr = gfc_build_addr_expr (type, se->expr);
8601 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
8605 /* Generate code for assignment of scalar variables. Includes character
8606 strings and derived types with allocatable components.
8607 If you know that the LHS has no allocations, set dealloc to false.
8609 DEEP_COPY has no effect if the typespec TS is not a derived type with
8610 allocatable components. Otherwise, if it is set, an explicit copy of each
8611 allocatable component is made. This is necessary as a simple copy of the
8612 whole object would copy array descriptors as is, so that the lhs's
8613 allocatable components would point to the rhs's after the assignment.
8614 Typically, setting DEEP_COPY is necessary if the rhs is a variable, and not
8615 necessary if the rhs is a non-pointer function, as the allocatable components
8616 are not accessible by other means than the function's result after the
8617 function has returned. It is even more subtle when temporaries are involved,
8618 as the two following examples show:
8619 1. When we evaluate an array constructor, a temporary is created. Thus
8620 there is theoretically no alias possible. However, no deep copy is
8621 made for this temporary, so that if the constructor is made of one or
8622 more variable with allocatable components, those components still point
8623 to the variable's: DEEP_COPY should be set for the assignment from the
8624 temporary to the lhs in that case.
8625 2. When assigning a scalar to an array, we evaluate the scalar value out
8626 of the loop, store it into a temporary variable, and assign from that.
8627 In that case, deep copying when assigning to the temporary would be a
8628 waste of resources; however deep copies should happen when assigning from
8629 the temporary to each array element: again DEEP_COPY should be set for
8630 the assignment from the temporary to the lhs. */
8632 tree
8633 gfc_trans_scalar_assign (gfc_se * lse, gfc_se * rse, gfc_typespec ts,
8634 bool deep_copy, bool dealloc, bool in_coarray)
8636 stmtblock_t block;
8637 tree tmp;
8638 tree cond;
8640 gfc_init_block (&block);
8642 if (ts.type == BT_CHARACTER)
8644 tree rlen = NULL;
8645 tree llen = NULL;
8647 if (lse->string_length != NULL_TREE)
8649 gfc_conv_string_parameter (lse);
8650 gfc_add_block_to_block (&block, &lse->pre);
8651 llen = lse->string_length;
8654 if (rse->string_length != NULL_TREE)
8656 gfc_conv_string_parameter (rse);
8657 gfc_add_block_to_block (&block, &rse->pre);
8658 rlen = rse->string_length;
8661 gfc_trans_string_copy (&block, llen, lse->expr, ts.kind, rlen,
8662 rse->expr, ts.kind);
8664 else if (gfc_bt_struct (ts.type) && ts.u.derived->attr.alloc_comp)
8666 tree tmp_var = NULL_TREE;
8667 cond = NULL_TREE;
8669 /* Are the rhs and the lhs the same? */
8670 if (deep_copy)
8672 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
8673 gfc_build_addr_expr (NULL_TREE, lse->expr),
8674 gfc_build_addr_expr (NULL_TREE, rse->expr));
8675 cond = gfc_evaluate_now (cond, &lse->pre);
8678 /* Deallocate the lhs allocated components as long as it is not
8679 the same as the rhs. This must be done following the assignment
8680 to prevent deallocating data that could be used in the rhs
8681 expression. */
8682 if (dealloc)
8684 tmp_var = gfc_evaluate_now (lse->expr, &lse->pre);
8685 tmp = gfc_deallocate_alloc_comp_no_caf (ts.u.derived, tmp_var, 0);
8686 if (deep_copy)
8687 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
8688 tmp);
8689 gfc_add_expr_to_block (&lse->post, tmp);
8692 gfc_add_block_to_block (&block, &rse->pre);
8693 gfc_add_block_to_block (&block, &lse->pre);
8695 gfc_add_modify (&block, lse->expr,
8696 fold_convert (TREE_TYPE (lse->expr), rse->expr));
8698 /* Restore pointer address of coarray components. */
8699 if (ts.u.derived->attr.coarray_comp && deep_copy && tmp_var != NULL_TREE)
8701 tmp = gfc_reassign_alloc_comp_caf (ts.u.derived, tmp_var, lse->expr);
8702 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
8703 tmp);
8704 gfc_add_expr_to_block (&block, tmp);
8707 /* Do a deep copy if the rhs is a variable, if it is not the
8708 same as the lhs. */
8709 if (deep_copy)
8711 int caf_mode = in_coarray ? (GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
8712 | GFC_STRUCTURE_CAF_MODE_IN_COARRAY) : 0;
8713 tmp = gfc_copy_alloc_comp (ts.u.derived, rse->expr, lse->expr, 0,
8714 caf_mode);
8715 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
8716 tmp);
8717 gfc_add_expr_to_block (&block, tmp);
8720 else if (gfc_bt_struct (ts.type) || ts.type == BT_CLASS)
8722 gfc_add_block_to_block (&block, &lse->pre);
8723 gfc_add_block_to_block (&block, &rse->pre);
8724 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
8725 TREE_TYPE (lse->expr), rse->expr);
8726 gfc_add_modify (&block, lse->expr, tmp);
8728 else
8730 gfc_add_block_to_block (&block, &lse->pre);
8731 gfc_add_block_to_block (&block, &rse->pre);
8733 gfc_add_modify (&block, lse->expr,
8734 fold_convert (TREE_TYPE (lse->expr), rse->expr));
8737 gfc_add_block_to_block (&block, &lse->post);
8738 gfc_add_block_to_block (&block, &rse->post);
8740 return gfc_finish_block (&block);
8744 /* There are quite a lot of restrictions on the optimisation in using an
8745 array function assign without a temporary. */
8747 static bool
8748 arrayfunc_assign_needs_temporary (gfc_expr * expr1, gfc_expr * expr2)
8750 gfc_ref * ref;
8751 bool seen_array_ref;
8752 bool c = false;
8753 gfc_symbol *sym = expr1->symtree->n.sym;
8755 /* Play it safe with class functions assigned to a derived type. */
8756 if (gfc_is_alloc_class_array_function (expr2)
8757 && expr1->ts.type == BT_DERIVED)
8758 return true;
8760 /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
8761 if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
8762 return true;
8764 /* Elemental functions are scalarized so that they don't need a
8765 temporary in gfc_trans_assignment_1, so return a true. Otherwise,
8766 they would need special treatment in gfc_trans_arrayfunc_assign. */
8767 if (expr2->value.function.esym != NULL
8768 && expr2->value.function.esym->attr.elemental)
8769 return true;
8771 /* Need a temporary if rhs is not FULL or a contiguous section. */
8772 if (expr1->ref && !(gfc_full_array_ref_p (expr1->ref, &c) || c))
8773 return true;
8775 /* Need a temporary if EXPR1 can't be expressed as a descriptor. */
8776 if (gfc_ref_needs_temporary_p (expr1->ref))
8777 return true;
8779 /* Functions returning pointers or allocatables need temporaries. */
8780 c = expr2->value.function.esym
8781 ? (expr2->value.function.esym->attr.pointer
8782 || expr2->value.function.esym->attr.allocatable)
8783 : (expr2->symtree->n.sym->attr.pointer
8784 || expr2->symtree->n.sym->attr.allocatable);
8785 if (c)
8786 return true;
8788 /* Character array functions need temporaries unless the
8789 character lengths are the same. */
8790 if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
8792 if (expr1->ts.u.cl->length == NULL
8793 || expr1->ts.u.cl->length->expr_type != EXPR_CONSTANT)
8794 return true;
8796 if (expr2->ts.u.cl->length == NULL
8797 || expr2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
8798 return true;
8800 if (mpz_cmp (expr1->ts.u.cl->length->value.integer,
8801 expr2->ts.u.cl->length->value.integer) != 0)
8802 return true;
8805 /* Check that no LHS component references appear during an array
8806 reference. This is needed because we do not have the means to
8807 span any arbitrary stride with an array descriptor. This check
8808 is not needed for the rhs because the function result has to be
8809 a complete type. */
8810 seen_array_ref = false;
8811 for (ref = expr1->ref; ref; ref = ref->next)
8813 if (ref->type == REF_ARRAY)
8814 seen_array_ref= true;
8815 else if (ref->type == REF_COMPONENT && seen_array_ref)
8816 return true;
8819 /* Check for a dependency. */
8820 if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
8821 expr2->value.function.esym,
8822 expr2->value.function.actual,
8823 NOT_ELEMENTAL))
8824 return true;
8826 /* If we have reached here with an intrinsic function, we do not
8827 need a temporary except in the particular case that reallocation
8828 on assignment is active and the lhs is allocatable and a target. */
8829 if (expr2->value.function.isym)
8830 return (flag_realloc_lhs && sym->attr.allocatable && sym->attr.target);
8832 /* If the LHS is a dummy, we need a temporary if it is not
8833 INTENT(OUT). */
8834 if (sym->attr.dummy && sym->attr.intent != INTENT_OUT)
8835 return true;
8837 /* If the lhs has been host_associated, is in common, a pointer or is
8838 a target and the function is not using a RESULT variable, aliasing
8839 can occur and a temporary is needed. */
8840 if ((sym->attr.host_assoc
8841 || sym->attr.in_common
8842 || sym->attr.pointer
8843 || sym->attr.cray_pointee
8844 || sym->attr.target)
8845 && expr2->symtree != NULL
8846 && expr2->symtree->n.sym == expr2->symtree->n.sym->result)
8847 return true;
8849 /* A PURE function can unconditionally be called without a temporary. */
8850 if (expr2->value.function.esym != NULL
8851 && expr2->value.function.esym->attr.pure)
8852 return false;
8854 /* Implicit_pure functions are those which could legally be declared
8855 to be PURE. */
8856 if (expr2->value.function.esym != NULL
8857 && expr2->value.function.esym->attr.implicit_pure)
8858 return false;
8860 if (!sym->attr.use_assoc
8861 && !sym->attr.in_common
8862 && !sym->attr.pointer
8863 && !sym->attr.target
8864 && !sym->attr.cray_pointee
8865 && expr2->value.function.esym)
8867 /* A temporary is not needed if the function is not contained and
8868 the variable is local or host associated and not a pointer or
8869 a target. */
8870 if (!expr2->value.function.esym->attr.contained)
8871 return false;
8873 /* A temporary is not needed if the lhs has never been host
8874 associated and the procedure is contained. */
8875 else if (!sym->attr.host_assoc)
8876 return false;
8878 /* A temporary is not needed if the variable is local and not
8879 a pointer, a target or a result. */
8880 if (sym->ns->parent
8881 && expr2->value.function.esym->ns == sym->ns->parent)
8882 return false;
8885 /* Default to temporary use. */
8886 return true;
8890 /* Provide the loop info so that the lhs descriptor can be built for
8891 reallocatable assignments from extrinsic function calls. */
8893 static void
8894 realloc_lhs_loop_for_fcn_call (gfc_se *se, locus *where, gfc_ss **ss,
8895 gfc_loopinfo *loop)
8897 /* Signal that the function call should not be made by
8898 gfc_conv_loop_setup. */
8899 se->ss->is_alloc_lhs = 1;
8900 gfc_init_loopinfo (loop);
8901 gfc_add_ss_to_loop (loop, *ss);
8902 gfc_add_ss_to_loop (loop, se->ss);
8903 gfc_conv_ss_startstride (loop);
8904 gfc_conv_loop_setup (loop, where);
8905 gfc_copy_loopinfo_to_se (se, loop);
8906 gfc_add_block_to_block (&se->pre, &loop->pre);
8907 gfc_add_block_to_block (&se->pre, &loop->post);
8908 se->ss->is_alloc_lhs = 0;
8912 /* For assignment to a reallocatable lhs from intrinsic functions,
8913 replace the se.expr (ie. the result) with a temporary descriptor.
8914 Null the data field so that the library allocates space for the
8915 result. Free the data of the original descriptor after the function,
8916 in case it appears in an argument expression and transfer the
8917 result to the original descriptor. */
8919 static void
8920 fcncall_realloc_result (gfc_se *se, int rank)
8922 tree desc;
8923 tree res_desc;
8924 tree tmp;
8925 tree offset;
8926 tree zero_cond;
8927 int n;
8929 /* Use the allocation done by the library. Substitute the lhs
8930 descriptor with a copy, whose data field is nulled.*/
8931 desc = build_fold_indirect_ref_loc (input_location, se->expr);
8932 if (POINTER_TYPE_P (TREE_TYPE (desc)))
8933 desc = build_fold_indirect_ref_loc (input_location, desc);
8935 /* Unallocated, the descriptor does not have a dtype. */
8936 tmp = gfc_conv_descriptor_dtype (desc);
8937 gfc_add_modify (&se->pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
8939 res_desc = gfc_evaluate_now (desc, &se->pre);
8940 gfc_conv_descriptor_data_set (&se->pre, res_desc, null_pointer_node);
8941 se->expr = gfc_build_addr_expr (NULL_TREE, res_desc);
8943 /* Free the lhs after the function call and copy the result data to
8944 the lhs descriptor. */
8945 tmp = gfc_conv_descriptor_data_get (desc);
8946 zero_cond = fold_build2_loc (input_location, EQ_EXPR,
8947 boolean_type_node, tmp,
8948 build_int_cst (TREE_TYPE (tmp), 0));
8949 zero_cond = gfc_evaluate_now (zero_cond, &se->post);
8950 tmp = gfc_call_free (tmp);
8951 gfc_add_expr_to_block (&se->post, tmp);
8953 tmp = gfc_conv_descriptor_data_get (res_desc);
8954 gfc_conv_descriptor_data_set (&se->post, desc, tmp);
8956 /* Check that the shapes are the same between lhs and expression. */
8957 for (n = 0 ; n < rank; n++)
8959 tree tmp1;
8960 tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
8961 tmp1 = gfc_conv_descriptor_lbound_get (res_desc, gfc_rank_cst[n]);
8962 tmp = fold_build2_loc (input_location, MINUS_EXPR,
8963 gfc_array_index_type, tmp, tmp1);
8964 tmp1 = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
8965 tmp = fold_build2_loc (input_location, MINUS_EXPR,
8966 gfc_array_index_type, tmp, tmp1);
8967 tmp1 = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
8968 tmp = fold_build2_loc (input_location, PLUS_EXPR,
8969 gfc_array_index_type, tmp, tmp1);
8970 tmp = fold_build2_loc (input_location, NE_EXPR,
8971 boolean_type_node, tmp,
8972 gfc_index_zero_node);
8973 tmp = gfc_evaluate_now (tmp, &se->post);
8974 zero_cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
8975 boolean_type_node, tmp,
8976 zero_cond);
8979 /* 'zero_cond' being true is equal to lhs not being allocated or the
8980 shapes being different. */
8981 zero_cond = gfc_evaluate_now (zero_cond, &se->post);
8983 /* Now reset the bounds returned from the function call to bounds based
8984 on the lhs lbounds, except where the lhs is not allocated or the shapes
8985 of 'variable and 'expr' are different. Set the offset accordingly. */
8986 offset = gfc_index_zero_node;
8987 for (n = 0 ; n < rank; n++)
8989 tree lbound;
8991 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
8992 lbound = fold_build3_loc (input_location, COND_EXPR,
8993 gfc_array_index_type, zero_cond,
8994 gfc_index_one_node, lbound);
8995 lbound = gfc_evaluate_now (lbound, &se->post);
8997 tmp = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
8998 tmp = fold_build2_loc (input_location, PLUS_EXPR,
8999 gfc_array_index_type, tmp, lbound);
9000 gfc_conv_descriptor_lbound_set (&se->post, desc,
9001 gfc_rank_cst[n], lbound);
9002 gfc_conv_descriptor_ubound_set (&se->post, desc,
9003 gfc_rank_cst[n], tmp);
9005 /* Set stride and accumulate the offset. */
9006 tmp = gfc_conv_descriptor_stride_get (res_desc, gfc_rank_cst[n]);
9007 gfc_conv_descriptor_stride_set (&se->post, desc,
9008 gfc_rank_cst[n], tmp);
9009 tmp = fold_build2_loc (input_location, MULT_EXPR,
9010 gfc_array_index_type, lbound, tmp);
9011 offset = fold_build2_loc (input_location, MINUS_EXPR,
9012 gfc_array_index_type, offset, tmp);
9013 offset = gfc_evaluate_now (offset, &se->post);
9016 gfc_conv_descriptor_offset_set (&se->post, desc, offset);
9021 /* Try to translate array(:) = func (...), where func is a transformational
9022 array function, without using a temporary. Returns NULL if this isn't the
9023 case. */
9025 static tree
9026 gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
9028 gfc_se se;
9029 gfc_ss *ss = NULL;
9030 gfc_component *comp = NULL;
9031 gfc_loopinfo loop;
9033 if (arrayfunc_assign_needs_temporary (expr1, expr2))
9034 return NULL;
9036 /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
9037 functions. */
9038 comp = gfc_get_proc_ptr_comp (expr2);
9039 gcc_assert (expr2->value.function.isym
9040 || (comp && comp->attr.dimension)
9041 || (!comp && gfc_return_by_reference (expr2->value.function.esym)
9042 && expr2->value.function.esym->result->attr.dimension));
9044 gfc_init_se (&se, NULL);
9045 gfc_start_block (&se.pre);
9046 se.want_pointer = 1;
9048 gfc_conv_array_parameter (&se, expr1, false, NULL, NULL, NULL);
9050 if (expr1->ts.type == BT_DERIVED
9051 && expr1->ts.u.derived->attr.alloc_comp)
9053 tree tmp;
9054 tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, se.expr,
9055 expr1->rank);
9056 gfc_add_expr_to_block (&se.pre, tmp);
9059 se.direct_byref = 1;
9060 se.ss = gfc_walk_expr (expr2);
9061 gcc_assert (se.ss != gfc_ss_terminator);
9063 /* Reallocate on assignment needs the loopinfo for extrinsic functions.
9064 This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
9065 Clearly, this cannot be done for an allocatable function result, since
9066 the shape of the result is unknown and, in any case, the function must
9067 correctly take care of the reallocation internally. For intrinsic
9068 calls, the array data is freed and the library takes care of allocation.
9069 TODO: Add logic of trans-array.c: gfc_alloc_allocatable_for_assignment
9070 to the library. */
9071 if (flag_realloc_lhs
9072 && gfc_is_reallocatable_lhs (expr1)
9073 && !gfc_expr_attr (expr1).codimension
9074 && !gfc_is_coindexed (expr1)
9075 && !(expr2->value.function.esym
9076 && expr2->value.function.esym->result->attr.allocatable))
9078 realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
9080 if (!expr2->value.function.isym)
9082 ss = gfc_walk_expr (expr1);
9083 gcc_assert (ss != gfc_ss_terminator);
9085 realloc_lhs_loop_for_fcn_call (&se, &expr1->where, &ss, &loop);
9086 ss->is_alloc_lhs = 1;
9088 else
9089 fcncall_realloc_result (&se, expr1->rank);
9092 gfc_conv_function_expr (&se, expr2);
9093 gfc_add_block_to_block (&se.pre, &se.post);
9095 if (ss)
9096 gfc_cleanup_loop (&loop);
9097 else
9098 gfc_free_ss_chain (se.ss);
9100 return gfc_finish_block (&se.pre);
9104 /* Try to efficiently translate array(:) = 0. Return NULL if this
9105 can't be done. */
9107 static tree
9108 gfc_trans_zero_assign (gfc_expr * expr)
9110 tree dest, len, type;
9111 tree tmp;
9112 gfc_symbol *sym;
9114 sym = expr->symtree->n.sym;
9115 dest = gfc_get_symbol_decl (sym);
9117 type = TREE_TYPE (dest);
9118 if (POINTER_TYPE_P (type))
9119 type = TREE_TYPE (type);
9120 if (!GFC_ARRAY_TYPE_P (type))
9121 return NULL_TREE;
9123 /* Determine the length of the array. */
9124 len = GFC_TYPE_ARRAY_SIZE (type);
9125 if (!len || TREE_CODE (len) != INTEGER_CST)
9126 return NULL_TREE;
9128 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
9129 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
9130 fold_convert (gfc_array_index_type, tmp));
9132 /* If we are zeroing a local array avoid taking its address by emitting
9133 a = {} instead. */
9134 if (!POINTER_TYPE_P (TREE_TYPE (dest)))
9135 return build2_loc (input_location, MODIFY_EXPR, void_type_node,
9136 dest, build_constructor (TREE_TYPE (dest),
9137 NULL));
9139 /* Convert arguments to the correct types. */
9140 dest = fold_convert (pvoid_type_node, dest);
9141 len = fold_convert (size_type_node, len);
9143 /* Construct call to __builtin_memset. */
9144 tmp = build_call_expr_loc (input_location,
9145 builtin_decl_explicit (BUILT_IN_MEMSET),
9146 3, dest, integer_zero_node, len);
9147 return fold_convert (void_type_node, tmp);
9151 /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
9152 that constructs the call to __builtin_memcpy. */
9154 tree
9155 gfc_build_memcpy_call (tree dst, tree src, tree len)
9157 tree tmp;
9159 /* Convert arguments to the correct types. */
9160 if (!POINTER_TYPE_P (TREE_TYPE (dst)))
9161 dst = gfc_build_addr_expr (pvoid_type_node, dst);
9162 else
9163 dst = fold_convert (pvoid_type_node, dst);
9165 if (!POINTER_TYPE_P (TREE_TYPE (src)))
9166 src = gfc_build_addr_expr (pvoid_type_node, src);
9167 else
9168 src = fold_convert (pvoid_type_node, src);
9170 len = fold_convert (size_type_node, len);
9172 /* Construct call to __builtin_memcpy. */
9173 tmp = build_call_expr_loc (input_location,
9174 builtin_decl_explicit (BUILT_IN_MEMCPY),
9175 3, dst, src, len);
9176 return fold_convert (void_type_node, tmp);
9180 /* Try to efficiently translate dst(:) = src(:). Return NULL if this
9181 can't be done. EXPR1 is the destination/lhs and EXPR2 is the
9182 source/rhs, both are gfc_full_array_ref_p which have been checked for
9183 dependencies. */
9185 static tree
9186 gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
9188 tree dst, dlen, dtype;
9189 tree src, slen, stype;
9190 tree tmp;
9192 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
9193 src = gfc_get_symbol_decl (expr2->symtree->n.sym);
9195 dtype = TREE_TYPE (dst);
9196 if (POINTER_TYPE_P (dtype))
9197 dtype = TREE_TYPE (dtype);
9198 stype = TREE_TYPE (src);
9199 if (POINTER_TYPE_P (stype))
9200 stype = TREE_TYPE (stype);
9202 if (!GFC_ARRAY_TYPE_P (dtype) || !GFC_ARRAY_TYPE_P (stype))
9203 return NULL_TREE;
9205 /* Determine the lengths of the arrays. */
9206 dlen = GFC_TYPE_ARRAY_SIZE (dtype);
9207 if (!dlen || TREE_CODE (dlen) != INTEGER_CST)
9208 return NULL_TREE;
9209 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
9210 dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
9211 dlen, fold_convert (gfc_array_index_type, tmp));
9213 slen = GFC_TYPE_ARRAY_SIZE (stype);
9214 if (!slen || TREE_CODE (slen) != INTEGER_CST)
9215 return NULL_TREE;
9216 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (stype));
9217 slen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
9218 slen, fold_convert (gfc_array_index_type, tmp));
9220 /* Sanity check that they are the same. This should always be
9221 the case, as we should already have checked for conformance. */
9222 if (!tree_int_cst_equal (slen, dlen))
9223 return NULL_TREE;
9225 return gfc_build_memcpy_call (dst, src, dlen);
9229 /* Try to efficiently translate array(:) = (/ ... /). Return NULL if
9230 this can't be done. EXPR1 is the destination/lhs for which
9231 gfc_full_array_ref_p is true, and EXPR2 is the source/rhs. */
9233 static tree
9234 gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
9236 unsigned HOST_WIDE_INT nelem;
9237 tree dst, dtype;
9238 tree src, stype;
9239 tree len;
9240 tree tmp;
9242 nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
9243 if (nelem == 0)
9244 return NULL_TREE;
9246 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
9247 dtype = TREE_TYPE (dst);
9248 if (POINTER_TYPE_P (dtype))
9249 dtype = TREE_TYPE (dtype);
9250 if (!GFC_ARRAY_TYPE_P (dtype))
9251 return NULL_TREE;
9253 /* Determine the lengths of the array. */
9254 len = GFC_TYPE_ARRAY_SIZE (dtype);
9255 if (!len || TREE_CODE (len) != INTEGER_CST)
9256 return NULL_TREE;
9258 /* Confirm that the constructor is the same size. */
9259 if (compare_tree_int (len, nelem) != 0)
9260 return NULL_TREE;
9262 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
9263 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
9264 fold_convert (gfc_array_index_type, tmp));
9266 stype = gfc_typenode_for_spec (&expr2->ts);
9267 src = gfc_build_constant_array_constructor (expr2, stype);
9269 stype = TREE_TYPE (src);
9270 if (POINTER_TYPE_P (stype))
9271 stype = TREE_TYPE (stype);
9273 return gfc_build_memcpy_call (dst, src, len);
9277 /* Tells whether the expression is to be treated as a variable reference. */
9279 bool
9280 gfc_expr_is_variable (gfc_expr *expr)
9282 gfc_expr *arg;
9283 gfc_component *comp;
9284 gfc_symbol *func_ifc;
9286 if (expr->expr_type == EXPR_VARIABLE)
9287 return true;
9289 arg = gfc_get_noncopying_intrinsic_argument (expr);
9290 if (arg)
9292 gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
9293 return gfc_expr_is_variable (arg);
9296 /* A data-pointer-returning function should be considered as a variable
9297 too. */
9298 if (expr->expr_type == EXPR_FUNCTION
9299 && expr->ref == NULL)
9301 if (expr->value.function.isym != NULL)
9302 return false;
9304 if (expr->value.function.esym != NULL)
9306 func_ifc = expr->value.function.esym;
9307 goto found_ifc;
9309 else
9311 gcc_assert (expr->symtree);
9312 func_ifc = expr->symtree->n.sym;
9313 goto found_ifc;
9316 gcc_unreachable ();
9319 comp = gfc_get_proc_ptr_comp (expr);
9320 if ((expr->expr_type == EXPR_PPC || expr->expr_type == EXPR_FUNCTION)
9321 && comp)
9323 func_ifc = comp->ts.interface;
9324 goto found_ifc;
9327 if (expr->expr_type == EXPR_COMPCALL)
9329 gcc_assert (!expr->value.compcall.tbp->is_generic);
9330 func_ifc = expr->value.compcall.tbp->u.specific->n.sym;
9331 goto found_ifc;
9334 return false;
9336 found_ifc:
9337 gcc_assert (func_ifc->attr.function
9338 && func_ifc->result != NULL);
9339 return func_ifc->result->attr.pointer;
9343 /* Is the lhs OK for automatic reallocation? */
9345 static bool
9346 is_scalar_reallocatable_lhs (gfc_expr *expr)
9348 gfc_ref * ref;
9350 /* An allocatable variable with no reference. */
9351 if (expr->symtree->n.sym->attr.allocatable
9352 && !expr->ref)
9353 return true;
9355 /* All that can be left are allocatable components. However, we do
9356 not check for allocatable components here because the expression
9357 could be an allocatable component of a pointer component. */
9358 if (expr->symtree->n.sym->ts.type != BT_DERIVED
9359 && expr->symtree->n.sym->ts.type != BT_CLASS)
9360 return false;
9362 /* Find an allocatable component ref last. */
9363 for (ref = expr->ref; ref; ref = ref->next)
9364 if (ref->type == REF_COMPONENT
9365 && !ref->next
9366 && ref->u.c.component->attr.allocatable)
9367 return true;
9369 return false;
9373 /* Allocate or reallocate scalar lhs, as necessary. */
9375 static void
9376 alloc_scalar_allocatable_for_assignment (stmtblock_t *block,
9377 tree string_length,
9378 gfc_expr *expr1,
9379 gfc_expr *expr2)
9382 tree cond;
9383 tree tmp;
9384 tree size;
9385 tree size_in_bytes;
9386 tree jump_label1;
9387 tree jump_label2;
9388 gfc_se lse;
9389 gfc_ref *ref;
9391 if (!expr1 || expr1->rank)
9392 return;
9394 if (!expr2 || expr2->rank)
9395 return;
9397 for (ref = expr1->ref; ref; ref = ref->next)
9398 if (ref->type == REF_SUBSTRING)
9399 return;
9401 realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
9403 /* Since this is a scalar lhs, we can afford to do this. That is,
9404 there is no risk of side effects being repeated. */
9405 gfc_init_se (&lse, NULL);
9406 lse.want_pointer = 1;
9407 gfc_conv_expr (&lse, expr1);
9409 jump_label1 = gfc_build_label_decl (NULL_TREE);
9410 jump_label2 = gfc_build_label_decl (NULL_TREE);
9412 /* Do the allocation if the lhs is NULL. Otherwise go to label 1. */
9413 tmp = build_int_cst (TREE_TYPE (lse.expr), 0);
9414 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
9415 lse.expr, tmp);
9416 tmp = build3_v (COND_EXPR, cond,
9417 build1_v (GOTO_EXPR, jump_label1),
9418 build_empty_stmt (input_location));
9419 gfc_add_expr_to_block (block, tmp);
9421 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
9423 /* Use the rhs string length and the lhs element size. */
9424 size = string_length;
9425 tmp = TREE_TYPE (gfc_typenode_for_spec (&expr1->ts));
9426 tmp = TYPE_SIZE_UNIT (tmp);
9427 size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
9428 TREE_TYPE (tmp), tmp,
9429 fold_convert (TREE_TYPE (tmp), size));
9431 else
9433 /* Otherwise use the length in bytes of the rhs. */
9434 size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr1->ts));
9435 size_in_bytes = size;
9438 size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
9439 size_in_bytes, size_one_node);
9441 if (gfc_caf_attr (expr1).codimension && flag_coarray == GFC_FCOARRAY_LIB)
9443 tree caf_decl, token;
9444 gfc_se caf_se;
9445 symbol_attribute attr;
9447 gfc_clear_attr (&attr);
9448 gfc_init_se (&caf_se, NULL);
9450 caf_decl = gfc_get_tree_for_caf_expr (expr1);
9451 gfc_get_caf_token_offset (&caf_se, &token, NULL, caf_decl, NULL_TREE,
9452 NULL);
9453 gfc_add_block_to_block (block, &caf_se.pre);
9454 gfc_allocate_allocatable (block, lse.expr, size_in_bytes,
9455 gfc_build_addr_expr (NULL_TREE, token),
9456 NULL_TREE, NULL_TREE, NULL_TREE, jump_label1,
9457 expr1, 1);
9459 else if (expr1->ts.type == BT_DERIVED && expr1->ts.u.derived->attr.alloc_comp)
9461 tmp = build_call_expr_loc (input_location,
9462 builtin_decl_explicit (BUILT_IN_CALLOC),
9463 2, build_one_cst (size_type_node),
9464 size_in_bytes);
9465 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
9466 gfc_add_modify (block, lse.expr, tmp);
9468 else
9470 tmp = build_call_expr_loc (input_location,
9471 builtin_decl_explicit (BUILT_IN_MALLOC),
9472 1, size_in_bytes);
9473 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
9474 gfc_add_modify (block, lse.expr, tmp);
9477 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
9479 /* Deferred characters need checking for lhs and rhs string
9480 length. Other deferred parameter variables will have to
9481 come here too. */
9482 tmp = build1_v (GOTO_EXPR, jump_label2);
9483 gfc_add_expr_to_block (block, tmp);
9485 tmp = build1_v (LABEL_EXPR, jump_label1);
9486 gfc_add_expr_to_block (block, tmp);
9488 /* For a deferred length character, reallocate if lengths of lhs and
9489 rhs are different. */
9490 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
9492 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
9493 lse.string_length, size);
9494 /* Jump past the realloc if the lengths are the same. */
9495 tmp = build3_v (COND_EXPR, cond,
9496 build1_v (GOTO_EXPR, jump_label2),
9497 build_empty_stmt (input_location));
9498 gfc_add_expr_to_block (block, tmp);
9499 tmp = build_call_expr_loc (input_location,
9500 builtin_decl_explicit (BUILT_IN_REALLOC),
9501 2, fold_convert (pvoid_type_node, lse.expr),
9502 size_in_bytes);
9503 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
9504 gfc_add_modify (block, lse.expr, tmp);
9505 tmp = build1_v (LABEL_EXPR, jump_label2);
9506 gfc_add_expr_to_block (block, tmp);
9508 /* Update the lhs character length. */
9509 size = string_length;
9510 gfc_add_modify (block, lse.string_length, size);
9514 /* Check for assignments of the type
9516 a = a + 4
9518 to make sure we do not check for reallocation unneccessarily. */
9521 static bool
9522 is_runtime_conformable (gfc_expr *expr1, gfc_expr *expr2)
9524 gfc_actual_arglist *a;
9525 gfc_expr *e1, *e2;
9527 switch (expr2->expr_type)
9529 case EXPR_VARIABLE:
9530 return gfc_dep_compare_expr (expr1, expr2) == 0;
9532 case EXPR_FUNCTION:
9533 if (expr2->value.function.esym
9534 && expr2->value.function.esym->attr.elemental)
9536 for (a = expr2->value.function.actual; a != NULL; a = a->next)
9538 e1 = a->expr;
9539 if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
9540 return false;
9542 return true;
9544 else if (expr2->value.function.isym
9545 && expr2->value.function.isym->elemental)
9547 for (a = expr2->value.function.actual; a != NULL; a = a->next)
9549 e1 = a->expr;
9550 if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
9551 return false;
9553 return true;
9556 break;
9558 case EXPR_OP:
9559 switch (expr2->value.op.op)
9561 case INTRINSIC_NOT:
9562 case INTRINSIC_UPLUS:
9563 case INTRINSIC_UMINUS:
9564 case INTRINSIC_PARENTHESES:
9565 return is_runtime_conformable (expr1, expr2->value.op.op1);
9567 case INTRINSIC_PLUS:
9568 case INTRINSIC_MINUS:
9569 case INTRINSIC_TIMES:
9570 case INTRINSIC_DIVIDE:
9571 case INTRINSIC_POWER:
9572 case INTRINSIC_AND:
9573 case INTRINSIC_OR:
9574 case INTRINSIC_EQV:
9575 case INTRINSIC_NEQV:
9576 case INTRINSIC_EQ:
9577 case INTRINSIC_NE:
9578 case INTRINSIC_GT:
9579 case INTRINSIC_GE:
9580 case INTRINSIC_LT:
9581 case INTRINSIC_LE:
9582 case INTRINSIC_EQ_OS:
9583 case INTRINSIC_NE_OS:
9584 case INTRINSIC_GT_OS:
9585 case INTRINSIC_GE_OS:
9586 case INTRINSIC_LT_OS:
9587 case INTRINSIC_LE_OS:
9589 e1 = expr2->value.op.op1;
9590 e2 = expr2->value.op.op2;
9592 if (e1->rank == 0 && e2->rank > 0)
9593 return is_runtime_conformable (expr1, e2);
9594 else if (e1->rank > 0 && e2->rank == 0)
9595 return is_runtime_conformable (expr1, e1);
9596 else if (e1->rank > 0 && e2->rank > 0)
9597 return is_runtime_conformable (expr1, e1)
9598 && is_runtime_conformable (expr1, e2);
9599 break;
9601 default:
9602 break;
9606 break;
9608 default:
9609 break;
9611 return false;
9615 static tree
9616 trans_class_assignment (stmtblock_t *block, gfc_expr *lhs, gfc_expr *rhs,
9617 gfc_se *lse, gfc_se *rse, bool use_vptr_copy,
9618 bool class_realloc)
9620 tree tmp, fcn, stdcopy, to_len, from_len, vptr;
9621 vec<tree, va_gc> *args = NULL;
9623 vptr = trans_class_vptr_len_assignment (block, lhs, rhs, rse, &to_len,
9624 &from_len);
9626 /* Generate allocation of the lhs. */
9627 if (class_realloc)
9629 stmtblock_t alloc;
9630 tree class_han;
9632 tmp = gfc_vptr_size_get (vptr);
9633 class_han = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
9634 ? gfc_class_data_get (lse->expr) : lse->expr;
9635 gfc_init_block (&alloc);
9636 gfc_allocate_using_malloc (&alloc, class_han, tmp, NULL_TREE);
9637 tmp = fold_build2_loc (input_location, EQ_EXPR,
9638 boolean_type_node, class_han,
9639 build_int_cst (prvoid_type_node, 0));
9640 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
9641 gfc_unlikely (tmp,
9642 PRED_FORTRAN_FAIL_ALLOC),
9643 gfc_finish_block (&alloc),
9644 build_empty_stmt (input_location));
9645 gfc_add_expr_to_block (&lse->pre, tmp);
9648 fcn = gfc_vptr_copy_get (vptr);
9650 tmp = GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr))
9651 ? gfc_class_data_get (rse->expr) : rse->expr;
9652 if (use_vptr_copy)
9654 if (!POINTER_TYPE_P (TREE_TYPE (tmp))
9655 || INDIRECT_REF_P (tmp)
9656 || (rhs->ts.type == BT_DERIVED
9657 && rhs->ts.u.derived->attr.unlimited_polymorphic
9658 && !rhs->ts.u.derived->attr.pointer
9659 && !rhs->ts.u.derived->attr.allocatable)
9660 || (UNLIMITED_POLY (rhs)
9661 && !CLASS_DATA (rhs)->attr.pointer
9662 && !CLASS_DATA (rhs)->attr.allocatable))
9663 vec_safe_push (args, gfc_build_addr_expr (NULL_TREE, tmp));
9664 else
9665 vec_safe_push (args, tmp);
9666 tmp = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
9667 ? gfc_class_data_get (lse->expr) : lse->expr;
9668 if (!POINTER_TYPE_P (TREE_TYPE (tmp))
9669 || INDIRECT_REF_P (tmp)
9670 || (lhs->ts.type == BT_DERIVED
9671 && lhs->ts.u.derived->attr.unlimited_polymorphic
9672 && !lhs->ts.u.derived->attr.pointer
9673 && !lhs->ts.u.derived->attr.allocatable)
9674 || (UNLIMITED_POLY (lhs)
9675 && !CLASS_DATA (lhs)->attr.pointer
9676 && !CLASS_DATA (lhs)->attr.allocatable))
9677 vec_safe_push (args, gfc_build_addr_expr (NULL_TREE, tmp));
9678 else
9679 vec_safe_push (args, tmp);
9681 stdcopy = build_call_vec (TREE_TYPE (TREE_TYPE (fcn)), fcn, args);
9683 if (to_len != NULL_TREE && !integer_zerop (from_len))
9685 tree extcopy;
9686 vec_safe_push (args, from_len);
9687 vec_safe_push (args, to_len);
9688 extcopy = build_call_vec (TREE_TYPE (TREE_TYPE (fcn)), fcn, args);
9690 tmp = fold_build2_loc (input_location, GT_EXPR,
9691 boolean_type_node, from_len,
9692 integer_zero_node);
9693 return fold_build3_loc (input_location, COND_EXPR,
9694 void_type_node, tmp,
9695 extcopy, stdcopy);
9697 else
9698 return stdcopy;
9700 else
9702 tree rhst = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
9703 ? gfc_class_data_get (lse->expr) : lse->expr;
9704 stmtblock_t tblock;
9705 gfc_init_block (&tblock);
9706 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
9707 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
9708 if (!POINTER_TYPE_P (TREE_TYPE (rhst)))
9709 rhst = gfc_build_addr_expr (NULL_TREE, rhst);
9710 /* When coming from a ptr_copy lhs and rhs are swapped. */
9711 gfc_add_modify_loc (input_location, &tblock, rhst,
9712 fold_convert (TREE_TYPE (rhst), tmp));
9713 return gfc_finish_block (&tblock);
9717 /* Subroutine of gfc_trans_assignment that actually scalarizes the
9718 assignment. EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
9719 init_flag indicates initialization expressions and dealloc that no
9720 deallocate prior assignment is needed (if in doubt, set true).
9721 When PTR_COPY is set and expr1 is a class type, then use the _vptr-copy
9722 routine instead of a pointer assignment. Alias resolution is only done,
9723 when MAY_ALIAS is set (the default). This flag is used by ALLOCATE()
9724 where it is known, that newly allocated memory on the lhs can never be
9725 an alias of the rhs. */
9727 static tree
9728 gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
9729 bool dealloc, bool use_vptr_copy, bool may_alias)
9731 gfc_se lse;
9732 gfc_se rse;
9733 gfc_ss *lss;
9734 gfc_ss *lss_section;
9735 gfc_ss *rss;
9736 gfc_loopinfo loop;
9737 tree tmp;
9738 stmtblock_t block;
9739 stmtblock_t body;
9740 bool l_is_temp;
9741 bool scalar_to_array;
9742 tree string_length;
9743 int n;
9744 bool maybe_workshare = false, lhs_refs_comp = false, rhs_refs_comp = false;
9745 symbol_attribute lhs_caf_attr, rhs_caf_attr, lhs_attr;
9746 bool is_poly_assign;
9748 /* Assignment of the form lhs = rhs. */
9749 gfc_start_block (&block);
9751 gfc_init_se (&lse, NULL);
9752 gfc_init_se (&rse, NULL);
9754 /* Walk the lhs. */
9755 lss = gfc_walk_expr (expr1);
9756 if (gfc_is_reallocatable_lhs (expr1)
9757 && !(expr2->expr_type == EXPR_FUNCTION
9758 && expr2->value.function.isym != NULL))
9759 lss->is_alloc_lhs = 1;
9760 rss = NULL;
9762 if ((expr1->ts.type == BT_DERIVED)
9763 && (gfc_is_alloc_class_array_function (expr2)
9764 || gfc_is_alloc_class_scalar_function (expr2)))
9765 expr2->must_finalize = 1;
9767 /* Checking whether a class assignment is desired is quite complicated and
9768 needed at two locations, so do it once only before the information is
9769 needed. */
9770 lhs_attr = gfc_expr_attr (expr1);
9771 is_poly_assign = (use_vptr_copy || lhs_attr.pointer
9772 || (lhs_attr.allocatable && !lhs_attr.dimension))
9773 && (expr1->ts.type == BT_CLASS
9774 || gfc_is_class_array_ref (expr1, NULL)
9775 || gfc_is_class_scalar_expr (expr1)
9776 || gfc_is_class_array_ref (expr2, NULL)
9777 || gfc_is_class_scalar_expr (expr2));
9780 /* Only analyze the expressions for coarray properties, when in coarray-lib
9781 mode. */
9782 if (flag_coarray == GFC_FCOARRAY_LIB)
9784 lhs_caf_attr = gfc_caf_attr (expr1, false, &lhs_refs_comp);
9785 rhs_caf_attr = gfc_caf_attr (expr2, false, &rhs_refs_comp);
9788 if (lss != gfc_ss_terminator)
9790 /* The assignment needs scalarization. */
9791 lss_section = lss;
9793 /* Find a non-scalar SS from the lhs. */
9794 while (lss_section != gfc_ss_terminator
9795 && lss_section->info->type != GFC_SS_SECTION)
9796 lss_section = lss_section->next;
9798 gcc_assert (lss_section != gfc_ss_terminator);
9800 /* Initialize the scalarizer. */
9801 gfc_init_loopinfo (&loop);
9803 /* Walk the rhs. */
9804 rss = gfc_walk_expr (expr2);
9805 if (rss == gfc_ss_terminator)
9806 /* The rhs is scalar. Add a ss for the expression. */
9807 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
9808 /* When doing a class assign, then the handle to the rhs needs to be a
9809 pointer to allow for polymorphism. */
9810 if (is_poly_assign && expr2->rank == 0 && !UNLIMITED_POLY (expr2))
9811 rss->info->type = GFC_SS_REFERENCE;
9813 /* Associate the SS with the loop. */
9814 gfc_add_ss_to_loop (&loop, lss);
9815 gfc_add_ss_to_loop (&loop, rss);
9817 /* Calculate the bounds of the scalarization. */
9818 gfc_conv_ss_startstride (&loop);
9819 /* Enable loop reversal. */
9820 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
9821 loop.reverse[n] = GFC_ENABLE_REVERSE;
9822 /* Resolve any data dependencies in the statement. */
9823 if (may_alias)
9824 gfc_conv_resolve_dependencies (&loop, lss, rss);
9825 /* Setup the scalarizing loops. */
9826 gfc_conv_loop_setup (&loop, &expr2->where);
9828 /* Setup the gfc_se structures. */
9829 gfc_copy_loopinfo_to_se (&lse, &loop);
9830 gfc_copy_loopinfo_to_se (&rse, &loop);
9832 rse.ss = rss;
9833 gfc_mark_ss_chain_used (rss, 1);
9834 if (loop.temp_ss == NULL)
9836 lse.ss = lss;
9837 gfc_mark_ss_chain_used (lss, 1);
9839 else
9841 lse.ss = loop.temp_ss;
9842 gfc_mark_ss_chain_used (lss, 3);
9843 gfc_mark_ss_chain_used (loop.temp_ss, 3);
9846 /* Allow the scalarizer to workshare array assignments. */
9847 if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_BODY))
9848 == OMPWS_WORKSHARE_FLAG
9849 && loop.temp_ss == NULL)
9851 maybe_workshare = true;
9852 ompws_flags |= OMPWS_SCALARIZER_WS | OMPWS_SCALARIZER_BODY;
9855 /* Start the scalarized loop body. */
9856 gfc_start_scalarized_body (&loop, &body);
9858 else
9859 gfc_init_block (&body);
9861 l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
9863 /* Translate the expression. */
9864 rse.want_coarray = flag_coarray == GFC_FCOARRAY_LIB && init_flag
9865 && lhs_caf_attr.codimension;
9866 gfc_conv_expr (&rse, expr2);
9868 /* Deal with the case of a scalar class function assigned to a derived type. */
9869 if (gfc_is_alloc_class_scalar_function (expr2)
9870 && expr1->ts.type == BT_DERIVED)
9872 rse.expr = gfc_class_data_get (rse.expr);
9873 rse.expr = build_fold_indirect_ref_loc (input_location, rse.expr);
9876 /* Stabilize a string length for temporaries. */
9877 if (expr2->ts.type == BT_CHARACTER && !expr1->ts.deferred
9878 && !(VAR_P (rse.string_length)
9879 || TREE_CODE (rse.string_length) == PARM_DECL
9880 || TREE_CODE (rse.string_length) == INDIRECT_REF))
9881 string_length = gfc_evaluate_now (rse.string_length, &rse.pre);
9882 else if (expr2->ts.type == BT_CHARACTER)
9883 string_length = rse.string_length;
9884 else
9885 string_length = NULL_TREE;
9887 if (l_is_temp)
9889 gfc_conv_tmp_array_ref (&lse);
9890 if (expr2->ts.type == BT_CHARACTER)
9891 lse.string_length = string_length;
9893 else
9895 gfc_conv_expr (&lse, expr1);
9896 if (gfc_option.rtcheck & GFC_RTCHECK_MEM
9897 && !init_flag
9898 && gfc_expr_attr (expr1).allocatable
9899 && expr1->rank
9900 && !expr2->rank)
9902 tree cond;
9903 const char* msg;
9905 /* We should only get array references here. */
9906 gcc_assert (TREE_CODE (lse.expr) == POINTER_PLUS_EXPR
9907 || TREE_CODE (lse.expr) == ARRAY_REF);
9909 /* 'tmp' is either the pointer to the array(POINTER_PLUS_EXPR)
9910 or the array itself(ARRAY_REF). */
9911 tmp = TREE_OPERAND (lse.expr, 0);
9913 /* Provide the address of the array. */
9914 if (TREE_CODE (lse.expr) == ARRAY_REF)
9915 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
9917 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
9918 tmp, build_int_cst (TREE_TYPE (tmp), 0));
9919 msg = _("Assignment of scalar to unallocated array");
9920 gfc_trans_runtime_check (true, false, cond, &loop.pre,
9921 &expr1->where, msg);
9925 /* Assignments of scalar derived types with allocatable components
9926 to arrays must be done with a deep copy and the rhs temporary
9927 must have its components deallocated afterwards. */
9928 scalar_to_array = (expr2->ts.type == BT_DERIVED
9929 && expr2->ts.u.derived->attr.alloc_comp
9930 && !gfc_expr_is_variable (expr2)
9931 && expr1->rank && !expr2->rank);
9932 scalar_to_array |= (expr1->ts.type == BT_DERIVED
9933 && expr1->rank
9934 && expr1->ts.u.derived->attr.alloc_comp
9935 && gfc_is_alloc_class_scalar_function (expr2));
9936 if (scalar_to_array && dealloc)
9938 tmp = gfc_deallocate_alloc_comp_no_caf (expr2->ts.u.derived, rse.expr, 0);
9939 gfc_prepend_expr_to_block (&loop.post, tmp);
9942 /* When assigning a character function result to a deferred-length variable,
9943 the function call must happen before the (re)allocation of the lhs -
9944 otherwise the character length of the result is not known.
9945 NOTE: This relies on having the exact dependence of the length type
9946 parameter available to the caller; gfortran saves it in the .mod files.
9947 NOTE ALSO: The concatenation operation generates a temporary pointer,
9948 whose allocation must go to the innermost loop. */
9949 if (flag_realloc_lhs
9950 && expr2->ts.type == BT_CHARACTER && expr1->ts.deferred
9951 && !(lss != gfc_ss_terminator
9952 && expr2->expr_type == EXPR_OP
9953 && expr2->value.op.op == INTRINSIC_CONCAT))
9954 gfc_add_block_to_block (&block, &rse.pre);
9956 /* Nullify the allocatable components corresponding to those of the lhs
9957 derived type, so that the finalization of the function result does not
9958 affect the lhs of the assignment. Prepend is used to ensure that the
9959 nullification occurs before the call to the finalizer. In the case of
9960 a scalar to array assignment, this is done in gfc_trans_scalar_assign
9961 as part of the deep copy. */
9962 if (!scalar_to_array && expr1->ts.type == BT_DERIVED
9963 && (gfc_is_alloc_class_array_function (expr2)
9964 || gfc_is_alloc_class_scalar_function (expr2)))
9966 tmp = rse.expr;
9967 tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, rse.expr, 0);
9968 gfc_prepend_expr_to_block (&rse.post, tmp);
9969 if (lss != gfc_ss_terminator && rss == gfc_ss_terminator)
9970 gfc_add_block_to_block (&loop.post, &rse.post);
9973 if (is_poly_assign)
9974 tmp = trans_class_assignment (&body, expr1, expr2, &lse, &rse,
9975 use_vptr_copy || (lhs_attr.allocatable
9976 && !lhs_attr.dimension),
9977 flag_realloc_lhs && !lhs_attr.pointer);
9978 else if (flag_coarray == GFC_FCOARRAY_LIB
9979 && lhs_caf_attr.codimension && rhs_caf_attr.codimension
9980 && ((lhs_caf_attr.allocatable && lhs_refs_comp)
9981 || (rhs_caf_attr.allocatable && rhs_refs_comp)))
9983 /* Only detour to caf_send[get][_by_ref] () when the lhs or rhs is an
9984 allocatable component, because those need to be accessed via the
9985 caf-runtime. No need to check for coindexes here, because resolve
9986 has rewritten those already. */
9987 gfc_code code;
9988 gfc_actual_arglist a1, a2;
9989 /* Clear the structures to prevent accessing garbage. */
9990 memset (&code, '\0', sizeof (gfc_code));
9991 memset (&a1, '\0', sizeof (gfc_actual_arglist));
9992 memset (&a2, '\0', sizeof (gfc_actual_arglist));
9993 a1.expr = expr1;
9994 a1.next = &a2;
9995 a2.expr = expr2;
9996 a2.next = NULL;
9997 code.ext.actual = &a1;
9998 code.resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
9999 tmp = gfc_conv_intrinsic_subroutine (&code);
10001 else
10002 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
10003 gfc_expr_is_variable (expr2)
10004 || scalar_to_array
10005 || expr2->expr_type == EXPR_ARRAY,
10006 !(l_is_temp || init_flag) && dealloc,
10007 expr1->symtree->n.sym->attr.codimension);
10008 /* Add the pre blocks to the body. */
10009 gfc_add_block_to_block (&body, &rse.pre);
10010 gfc_add_block_to_block (&body, &lse.pre);
10011 gfc_add_expr_to_block (&body, tmp);
10012 /* Add the post blocks to the body. */
10013 gfc_add_block_to_block (&body, &rse.post);
10014 gfc_add_block_to_block (&body, &lse.post);
10016 if (lss == gfc_ss_terminator)
10018 /* F2003: Add the code for reallocation on assignment. */
10019 if (flag_realloc_lhs && is_scalar_reallocatable_lhs (expr1)
10020 && !is_poly_assign)
10021 alloc_scalar_allocatable_for_assignment (&block, string_length,
10022 expr1, expr2);
10024 /* Use the scalar assignment as is. */
10025 gfc_add_block_to_block (&block, &body);
10027 else
10029 gcc_assert (lse.ss == gfc_ss_terminator
10030 && rse.ss == gfc_ss_terminator);
10032 if (l_is_temp)
10034 gfc_trans_scalarized_loop_boundary (&loop, &body);
10036 /* We need to copy the temporary to the actual lhs. */
10037 gfc_init_se (&lse, NULL);
10038 gfc_init_se (&rse, NULL);
10039 gfc_copy_loopinfo_to_se (&lse, &loop);
10040 gfc_copy_loopinfo_to_se (&rse, &loop);
10042 rse.ss = loop.temp_ss;
10043 lse.ss = lss;
10045 gfc_conv_tmp_array_ref (&rse);
10046 gfc_conv_expr (&lse, expr1);
10048 gcc_assert (lse.ss == gfc_ss_terminator
10049 && rse.ss == gfc_ss_terminator);
10051 if (expr2->ts.type == BT_CHARACTER)
10052 rse.string_length = string_length;
10054 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
10055 false, dealloc);
10056 gfc_add_expr_to_block (&body, tmp);
10059 /* F2003: Allocate or reallocate lhs of allocatable array. */
10060 if (flag_realloc_lhs
10061 && gfc_is_reallocatable_lhs (expr1)
10062 && expr2->rank
10063 && !is_runtime_conformable (expr1, expr2))
10065 realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
10066 ompws_flags &= ~OMPWS_SCALARIZER_WS;
10067 tmp = gfc_alloc_allocatable_for_assignment (&loop, expr1, expr2);
10068 if (tmp != NULL_TREE)
10069 gfc_add_expr_to_block (&loop.code[expr1->rank - 1], tmp);
10072 if (maybe_workshare)
10073 ompws_flags &= ~OMPWS_SCALARIZER_BODY;
10075 /* Generate the copying loops. */
10076 gfc_trans_scalarizing_loops (&loop, &body);
10078 /* Wrap the whole thing up. */
10079 gfc_add_block_to_block (&block, &loop.pre);
10080 gfc_add_block_to_block (&block, &loop.post);
10082 gfc_cleanup_loop (&loop);
10085 return gfc_finish_block (&block);
10089 /* Check whether EXPR is a copyable array. */
10091 static bool
10092 copyable_array_p (gfc_expr * expr)
10094 if (expr->expr_type != EXPR_VARIABLE)
10095 return false;
10097 /* First check it's an array. */
10098 if (expr->rank < 1 || !expr->ref || expr->ref->next)
10099 return false;
10101 if (!gfc_full_array_ref_p (expr->ref, NULL))
10102 return false;
10104 /* Next check that it's of a simple enough type. */
10105 switch (expr->ts.type)
10107 case BT_INTEGER:
10108 case BT_REAL:
10109 case BT_COMPLEX:
10110 case BT_LOGICAL:
10111 return true;
10113 case BT_CHARACTER:
10114 return false;
10116 case_bt_struct:
10117 return !expr->ts.u.derived->attr.alloc_comp;
10119 default:
10120 break;
10123 return false;
10126 /* Translate an assignment. */
10128 tree
10129 gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
10130 bool dealloc, bool use_vptr_copy, bool may_alias)
10132 tree tmp;
10134 /* Special case a single function returning an array. */
10135 if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
10137 tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
10138 if (tmp)
10139 return tmp;
10142 /* Special case assigning an array to zero. */
10143 if (copyable_array_p (expr1)
10144 && is_zero_initializer_p (expr2))
10146 tmp = gfc_trans_zero_assign (expr1);
10147 if (tmp)
10148 return tmp;
10151 /* Special case copying one array to another. */
10152 if (copyable_array_p (expr1)
10153 && copyable_array_p (expr2)
10154 && gfc_compare_types (&expr1->ts, &expr2->ts)
10155 && !gfc_check_dependency (expr1, expr2, 0))
10157 tmp = gfc_trans_array_copy (expr1, expr2);
10158 if (tmp)
10159 return tmp;
10162 /* Special case initializing an array from a constant array constructor. */
10163 if (copyable_array_p (expr1)
10164 && expr2->expr_type == EXPR_ARRAY
10165 && gfc_compare_types (&expr1->ts, &expr2->ts))
10167 tmp = gfc_trans_array_constructor_copy (expr1, expr2);
10168 if (tmp)
10169 return tmp;
10172 /* Fallback to the scalarizer to generate explicit loops. */
10173 return gfc_trans_assignment_1 (expr1, expr2, init_flag, dealloc,
10174 use_vptr_copy, may_alias);
10177 tree
10178 gfc_trans_init_assign (gfc_code * code)
10180 return gfc_trans_assignment (code->expr1, code->expr2, true, false, true);
10183 tree
10184 gfc_trans_assign (gfc_code * code)
10186 return gfc_trans_assignment (code->expr1, code->expr2, false, true);